blob: 94456aea7fd706a0c95c4d6ac9baf6a160c4aaee [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * edit.c: functions for Insert mode
12 */
13
14#include "vim.h"
15
16#ifdef FEAT_INS_EXPAND
17/*
18 * definitions used for CTRL-X submode
19 */
20#define CTRL_X_WANT_IDENT 0x100
21
22#define CTRL_X_NOT_DEFINED_YET 1
23#define CTRL_X_SCROLL 2
24#define CTRL_X_WHOLE_LINE 3
25#define CTRL_X_FILES 4
26#define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
27#define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
28#define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
29#define CTRL_X_FINISHED 8
30#define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
31#define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
32#define CTRL_X_CMDLINE 11
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000033#define CTRL_X_FUNCTION 12
Bram Moolenaarf75a9632005-09-13 21:20:47 +000034#define CTRL_X_OMNI 13
Bram Moolenaar488c6512005-08-11 20:09:58 +000035#define CTRL_X_SPELL 14
36#define CTRL_X_LOCAL_MSG 15 /* only used in "ctrl_x_msgs" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
Bram Moolenaar071d4272004-06-13 20:20:40 +000038#define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
39
40static char *ctrl_x_msgs[] =
41{
42 N_(" Keyword completion (^N^P)"), /* ctrl_x_mode == 0, ^P/^N compl. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +000043 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000044 NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +000045 N_(" Whole line completion (^L^N^P)"),
46 N_(" File name completion (^F^N^P)"),
47 N_(" Tag completion (^]^N^P)"),
48 N_(" Path pattern completion (^N^P)"),
49 N_(" Definition completion (^D^N^P)"),
50 NULL,
51 N_(" Dictionary completion (^K^N^P)"),
52 N_(" Thesaurus completion (^T^N^P)"),
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000053 N_(" Command-line completion (^V^N^P)"),
54 N_(" User defined completion (^U^N^P)"),
Bram Moolenaarf75a9632005-09-13 21:20:47 +000055 N_(" Omni completion (^O^N^P)"),
Bram Moolenaar910f66f2006-04-05 20:41:53 +000056 N_(" Spelling suggestion (s^N^P)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000057 N_(" Keyword Local completion (^N^P)"),
Bram Moolenaar071d4272004-06-13 20:20:40 +000058};
59
60static char_u e_hitend[] = N_("Hit end of paragraph");
61
62/*
63 * Structure used to store one match for insert completion.
64 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000065typedef struct compl_S compl_T;
66struct compl_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000067{
Bram Moolenaar572cb562005-08-05 21:35:02 +000068 compl_T *cp_next;
69 compl_T *cp_prev;
70 char_u *cp_str; /* matched text */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000071 char cp_icase; /* TRUE or FALSE: ignore case */
Bram Moolenaar39f05632006-03-19 22:15:26 +000072 char_u *(cp_text[CPT_COUNT]); /* text for the menu */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000073 char_u *cp_fname; /* file containing the match, allocated when
74 * cp_flags has FREE_FNAME */
Bram Moolenaar572cb562005-08-05 21:35:02 +000075 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
76 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +000077};
78
Bram Moolenaar572cb562005-08-05 21:35:02 +000079#define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
Bram Moolenaar071d4272004-06-13 20:20:40 +000080#define FREE_FNAME (2)
81
82/*
83 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000084 * "compl_first_match" points to the start of the list.
85 * "compl_curr_match" points to the currently selected entry.
86 * "compl_shown_match" is different from compl_curr_match during
87 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000088 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000089static compl_T *compl_first_match = NULL;
90static compl_T *compl_curr_match = NULL;
91static compl_T *compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000092
Bram Moolenaar779b74b2006-04-10 14:55:34 +000093/* After using a cursor key <Enter> selects a match in the popup menu,
94 * otherwise it inserts a line break. */
95static int compl_enter_selects = FALSE;
96
Bram Moolenaara6557602006-02-04 22:43:20 +000097/* When "compl_leader" is not NULL only matches that start with this string
98 * are used. */
99static char_u *compl_leader = NULL;
100
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000101static int compl_get_longest = FALSE; /* put longest common string
102 in compl_leader */
103
Bram Moolenaara6557602006-02-04 22:43:20 +0000104static int compl_used_match; /* Selected one of the matches. When
105 FALSE the match was edited or using
106 the longest common string. */
107
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000108static int compl_was_interrupted = FALSE; /* didn't finish finding
109 completions. */
110
111static int compl_restarting = FALSE; /* don't insert match */
112
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000113/* When the first completion is done "compl_started" is set. When it's
114 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000115static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000116
Bram Moolenaar572cb562005-08-05 21:35:02 +0000117static int compl_matches = 0;
118static char_u *compl_pattern = NULL;
119static int compl_direction = FORWARD;
120static int compl_shows_dir = FORWARD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000121static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000122static pos_T compl_startpos;
123static colnr_T compl_col = 0; /* column where the text starts
124 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000125static char_u *compl_orig_text = NULL; /* text as it was before
126 * completion started */
127static int compl_cont_mode = 0;
128static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000130static void ins_ctrl_x __ARGS((void));
131static int has_compl_option __ARGS((int dict_opt));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000132static int ins_compl_add __ARGS((char_u *str, int len, int icase, char_u *fname, char_u **cptext, int cdir, int flags, int adup));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000133static int ins_compl_equal __ARGS((compl_T *match, char_u *str, int len));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000134static void ins_compl_longest_match __ARGS((compl_T *match));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000135static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches, int icase));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136static int ins_compl_make_cyclic __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000137static void ins_compl_upd_pum __ARGS((void));
138static void ins_compl_del_pum __ARGS((void));
Bram Moolenaar280f1262006-01-30 00:14:18 +0000139static int pum_wanted __ARGS((void));
Bram Moolenaar65c923a2006-03-03 22:56:30 +0000140static int pum_enough_matches __ARGS((void));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000141static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int flags, int thesaurus));
Bram Moolenaar0b238792006-03-02 22:49:12 +0000142static void ins_compl_files __ARGS((int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000143static char_u *find_line_end __ARGS((char_u *ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144static void ins_compl_free __ARGS((void));
145static void ins_compl_clear __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000146static int ins_compl_bs __ARGS((void));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000147static void ins_compl_new_leader __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000148static void ins_compl_addleader __ARGS((int c));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000149static void ins_compl_restart __ARGS((void));
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000150static void ins_compl_set_original_text __ARGS((char_u *str));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000151static void ins_compl_addfrommatch __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000152static int ins_compl_prep __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
Bram Moolenaara94bc432006-03-10 21:42:59 +0000154#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
155static void ins_compl_add_list __ARGS((list_T *list));
156#endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000157static int ins_compl_get_exp __ARGS((pos_T *ini));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158static void ins_compl_delete __ARGS((void));
159static void ins_compl_insert __ARGS((void));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000160static int ins_compl_next __ARGS((int allow_get_expansion, int count, int insert_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000161static int ins_compl_key2dir __ARGS((int c));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000162static int ins_compl_pum_key __ARGS((int c));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000163static int ins_compl_key2count __ARGS((int c));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000164static int ins_compl_use_match __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165static int ins_complete __ARGS((int c));
166static int quote_meta __ARGS((char_u *dest, char_u *str, int len));
167#endif /* FEAT_INS_EXPAND */
168
169#define BACKSPACE_CHAR 1
170#define BACKSPACE_WORD 2
171#define BACKSPACE_WORD_NOT_SPACE 3
172#define BACKSPACE_LINE 4
173
Bram Moolenaar754b5602006-02-09 23:53:20 +0000174static void ins_redraw __ARGS((int ready));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175static void ins_ctrl_v __ARGS((void));
176static void undisplay_dollar __ARGS((void));
177static void insert_special __ARGS((int, int, int));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000178static void internal_format __ARGS((int textwidth, int second_indent, int flags, int format_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179static void check_auto_format __ARGS((int));
180static void redo_literal __ARGS((int c));
181static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000182#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +0000183static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000184static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000185static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
188static int echeck_abbr __ARGS((int));
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000189#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190static void replace_push_off __ARGS((int c));
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192static int replace_pop __ARGS((void));
193static void replace_join __ARGS((int off));
194static void replace_pop_ins __ARGS((void));
195#ifdef FEAT_MBYTE
196static void mb_replace_pop_ins __ARGS((int cc));
197#endif
198static void replace_flush __ARGS((void));
199static void replace_do_bs __ARGS((void));
200#ifdef FEAT_CINDENT
201static int cindent_on __ARGS((void));
202#endif
203static void ins_reg __ARGS((void));
204static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000205static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000206static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207#ifdef FEAT_RIGHTLEFT
208static void ins_ctrl_ __ARGS((void));
209#endif
210#ifdef FEAT_VISUAL
211static int ins_start_select __ARGS((int c));
212#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000213static void ins_insert __ARGS((int replaceState));
214static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215static void ins_shift __ARGS((int c, int lastc));
216static void ins_del __ARGS((void));
217static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
218#ifdef FEAT_MOUSE
219static void ins_mouse __ARGS((int c));
220static void ins_mousescroll __ARGS((int up));
221#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000222#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
223static void ins_tabline __ARGS((int c));
224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225static void ins_left __ARGS((void));
226static void ins_home __ARGS((int c));
227static void ins_end __ARGS((int c));
228static void ins_s_left __ARGS((void));
229static void ins_right __ARGS((void));
230static void ins_s_right __ARGS((void));
231static void ins_up __ARGS((int startcol));
232static void ins_pageup __ARGS((void));
233static void ins_down __ARGS((int startcol));
234static void ins_pagedown __ARGS((void));
235#ifdef FEAT_DND
236static void ins_drop __ARGS((void));
237#endif
238static int ins_tab __ARGS((void));
239static int ins_eol __ARGS((int c));
240#ifdef FEAT_DIGRAPHS
241static int ins_digraph __ARGS((void));
242#endif
243static int ins_copychar __ARGS((linenr_T lnum));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000244static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245#ifdef FEAT_SMARTINDENT
246static void ins_try_si __ARGS((int c));
247#endif
248static colnr_T get_nolist_virtcol __ARGS((void));
249
250static colnr_T Insstart_textlen; /* length of line when insert started */
251static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
252
253static char_u *last_insert = NULL; /* the text of the previous insert,
254 K_SPECIAL and CSI are escaped */
255static int last_insert_skip; /* nr of chars in front of previous insert */
256static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000257static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258
259#ifdef FEAT_CINDENT
260static int can_cindent; /* may do cindenting on this line */
261#endif
262
263static int old_indent = 0; /* for ^^D command in insert mode */
264
265#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000266static int revins_on; /* reverse insert mode on */
267static int revins_chars; /* how much to skip after edit */
268static int revins_legal; /* was the last char 'legal'? */
269static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270#endif
271
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272static int ins_need_undo; /* call u_save() before inserting a
273 char. Set when edit() is called.
274 after that arrow_used is used. */
275
276static int did_add_space = FALSE; /* auto_format() added an extra space
277 under the cursor */
278
279/*
280 * edit(): Start inserting text.
281 *
282 * "cmdchar" can be:
283 * 'i' normal insert command
284 * 'a' normal append command
285 * 'R' replace command
286 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
287 * but still only one <CR> is inserted. The <Esc> is not used for redo.
288 * 'g' "gI" command.
289 * 'V' "gR" command for Virtual Replace mode.
290 * 'v' "gr" command for single character Virtual Replace mode.
291 *
292 * This function is not called recursively. For CTRL-O commands, it returns
293 * and lets the caller handle the Normal-mode command.
294 *
295 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
296 */
297 int
298edit(cmdchar, startln, count)
299 int cmdchar;
300 int startln; /* if set, insert at start of line */
301 long count;
302{
303 int c = 0;
304 char_u *ptr;
305 int lastc;
306 colnr_T mincol;
307 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 int i;
309 int did_backspace = TRUE; /* previous char was backspace */
310#ifdef FEAT_CINDENT
311 int line_is_white = FALSE; /* line is empty before insert */
312#endif
313 linenr_T old_topline = 0; /* topline before insertion */
314#ifdef FEAT_DIFF
315 int old_topfill = -1;
316#endif
317 int inserted_space = FALSE; /* just inserted a space */
318 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000319 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000321 /* Remember whether editing was restarted after CTRL-O. */
322 did_restart_edit = restart_edit;
323
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 /* sleep before redrawing, needed for "CTRL-O :" that results in an
325 * error message */
326 check_for_delay(TRUE);
327
328#ifdef HAVE_SANDBOX
329 /* Don't allow inserting in the sandbox. */
330 if (sandbox != 0)
331 {
332 EMSG(_(e_sandbox));
333 return FALSE;
334 }
335#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000336 /* Don't allow changes in the buffer while editing the cmdline. The
337 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000338 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000339 {
340 EMSG(_(e_secure));
341 return FALSE;
342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343
344#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000345 /* Don't allow recursive insert mode when busy with completion. */
346 if (compl_started || pum_visible())
347 {
348 EMSG(_(e_secure));
349 return FALSE;
350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 ins_compl_clear(); /* clear stuff for CTRL-X mode */
352#endif
353
Bram Moolenaar843ee412004-06-30 16:16:41 +0000354#ifdef FEAT_AUTOCMD
355 /*
356 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
357 */
358 if (cmdchar != 'r' && cmdchar != 'v')
359 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000360# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000361 if (cmdchar == 'R')
362 ptr = (char_u *)"r";
363 else if (cmdchar == 'V')
364 ptr = (char_u *)"v";
365 else
366 ptr = (char_u *)"i";
367 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +0000368# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000369 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
370 }
371#endif
372
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373#ifdef FEAT_MOUSE
374 /*
375 * When doing a paste with the middle mouse button, Insstart is set to
376 * where the paste started.
377 */
378 if (where_paste_started.lnum != 0)
379 Insstart = where_paste_started;
380 else
381#endif
382 {
383 Insstart = curwin->w_cursor;
384 if (startln)
385 Insstart.col = 0;
386 }
387 Insstart_textlen = linetabsize(ml_get_curline());
388 Insstart_blank_vcol = MAXCOL;
389 if (!did_ai)
390 ai_col = 0;
391
392 if (cmdchar != NUL && restart_edit == 0)
393 {
394 ResetRedobuff();
395 AppendNumberToRedobuff(count);
396#ifdef FEAT_VREPLACE
397 if (cmdchar == 'V' || cmdchar == 'v')
398 {
399 /* "gR" or "gr" command */
400 AppendCharToRedobuff('g');
401 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
402 }
403 else
404#endif
405 {
406 AppendCharToRedobuff(cmdchar);
407 if (cmdchar == 'g') /* "gI" command */
408 AppendCharToRedobuff('I');
409 else if (cmdchar == 'r') /* "r<CR>" command */
410 count = 1; /* insert only one <CR> */
411 }
412 }
413
414 if (cmdchar == 'R')
415 {
416#ifdef FEAT_FKMAP
417 if (p_fkmap && p_ri)
418 {
419 beep_flush();
420 EMSG(farsi_text_3); /* encoded in Farsi */
421 State = INSERT;
422 }
423 else
424#endif
425 State = REPLACE;
426 }
427#ifdef FEAT_VREPLACE
428 else if (cmdchar == 'V' || cmdchar == 'v')
429 {
430 State = VREPLACE;
431 replaceState = VREPLACE;
432 orig_line_count = curbuf->b_ml.ml_line_count;
433 vr_lines_changed = 1;
434 }
435#endif
436 else
437 State = INSERT;
438
439 stop_insert_mode = FALSE;
440
441 /*
442 * Need to recompute the cursor position, it might move when the cursor is
443 * on a TAB or special character.
444 */
445 curs_columns(TRUE);
446
447 /*
448 * Enable langmap or IME, indicated by 'iminsert'.
449 * Note that IME may enabled/disabled without us noticing here, thus the
450 * 'iminsert' value may not reflect what is actually used. It is updated
451 * when hitting <Esc>.
452 */
453 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
454 State |= LANGMAP;
455#ifdef USE_IM_CONTROL
456 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
457#endif
458
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459#ifdef FEAT_MOUSE
460 setmouse();
461#endif
462#ifdef FEAT_CMDL_INFO
463 clear_showcmd();
464#endif
465#ifdef FEAT_RIGHTLEFT
466 /* there is no reverse replace mode */
467 revins_on = (State == INSERT && p_ri);
468 if (revins_on)
469 undisplay_dollar();
470 revins_chars = 0;
471 revins_legal = 0;
472 revins_scol = -1;
473#endif
474
475 /*
476 * Handle restarting Insert mode.
477 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
478 * restart_edit non-zero, and something in the stuff buffer.
479 */
480 if (restart_edit != 0 && stuff_empty())
481 {
482#ifdef FEAT_MOUSE
483 /*
484 * After a paste we consider text typed to be part of the insert for
485 * the pasted text. You can backspace over the pasted text too.
486 */
487 if (where_paste_started.lnum)
488 arrow_used = FALSE;
489 else
490#endif
491 arrow_used = TRUE;
492 restart_edit = 0;
493
494 /*
495 * If the cursor was after the end-of-line before the CTRL-O and it is
496 * now at the end-of-line, put it after the end-of-line (this is not
497 * correct in very rare cases).
498 * Also do this if curswant is greater than the current virtual
499 * column. Eg after "^O$" or "^O80|".
500 */
501 validate_virtcol();
502 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000503 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 || curwin->w_curswant > curwin->w_virtcol)
505 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
506 {
507 if (ptr[1] == NUL)
508 ++curwin->w_cursor.col;
509#ifdef FEAT_MBYTE
510 else if (has_mbyte)
511 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000512 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 if (ptr[i] == NUL)
514 curwin->w_cursor.col += i;
515 }
516#endif
517 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000518 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 }
520 else
521 arrow_used = FALSE;
522
523 /* we are in insert mode now, don't need to start it anymore */
524 need_start_insertmode = FALSE;
525
526 /* Need to save the line for undo before inserting the first char. */
527 ins_need_undo = TRUE;
528
529#ifdef FEAT_MOUSE
530 where_paste_started.lnum = 0;
531#endif
532#ifdef FEAT_CINDENT
533 can_cindent = TRUE;
534#endif
535#ifdef FEAT_FOLDING
536 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
537 * restarting. */
538 if (!p_im && did_restart_edit == 0)
539 foldOpenCursor();
540#endif
541
542 /*
543 * If 'showmode' is set, show the current (insert/replace/..) mode.
544 * A warning message for changing a readonly file is given here, before
545 * actually changing anything. It's put after the mode, if any.
546 */
547 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000548 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 i = showmode();
550
551 if (!p_im && did_restart_edit == 0)
552 change_warning(i + 1);
553
554#ifdef CURSOR_SHAPE
555 ui_cursor_shape(); /* may show different cursor shape */
556#endif
557#ifdef FEAT_DIGRAPHS
558 do_digraph(-1); /* clear digraphs */
559#endif
560
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000561 /*
562 * Get the current length of the redo buffer, those characters have to be
563 * skipped if we want to get to the inserted characters.
564 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 ptr = get_inserted();
566 if (ptr == NULL)
567 new_insert_skip = 0;
568 else
569 {
570 new_insert_skip = (int)STRLEN(ptr);
571 vim_free(ptr);
572 }
573
574 old_indent = 0;
575
576 /*
577 * Main loop in Insert mode: repeat until Insert mode is left.
578 */
579 for (;;)
580 {
581#ifdef FEAT_RIGHTLEFT
582 if (!revins_legal)
583 revins_scol = -1; /* reset on illegal motions */
584 else
585 revins_legal = 0;
586#endif
587 if (arrow_used) /* don't repeat insert when arrow key used */
588 count = 0;
589
590 if (stop_insert_mode)
591 {
592 /* ":stopinsert" used or 'insertmode' reset */
593 count = 0;
594 goto doESCkey;
595 }
596
597 /* set curwin->w_curswant for next K_DOWN or K_UP */
598 if (!arrow_used)
599 curwin->w_set_curswant = TRUE;
600
601 /* If there is no typeahead may check for timestamps (e.g., for when a
602 * menu invoked a shell command). */
603 if (stuff_empty())
604 {
605 did_check_timestamps = FALSE;
606 if (need_check_timestamps)
607 check_timestamps(FALSE);
608 }
609
610 /*
611 * When emsg() was called msg_scroll will have been set.
612 */
613 msg_scroll = FALSE;
614
615#ifdef FEAT_GUI
616 /* When 'mousefocus' is set a mouse movement may have taken us to
617 * another window. "need_mouse_correct" may then be set because of an
618 * autocommand. */
619 if (need_mouse_correct)
620 gui_mouse_correct();
621#endif
622
623#ifdef FEAT_FOLDING
624 /* Open fold at the cursor line, according to 'foldopen'. */
625 if (fdo_flags & FDO_INSERT)
626 foldOpenCursor();
627 /* Close folds where the cursor isn't, according to 'foldclose' */
628 if (!char_avail())
629 foldCheckClose();
630#endif
631
632 /*
633 * If we inserted a character at the last position of the last line in
634 * the window, scroll the window one line up. This avoids an extra
635 * redraw.
636 * This is detected when the cursor column is smaller after inserting
637 * something.
638 * Don't do this when the topline changed already, it has
639 * already been adjusted (by insertchar() calling open_line())).
640 */
641 if (curbuf->b_mod_set
642 && curwin->w_p_wrap
643 && !did_backspace
644 && curwin->w_topline == old_topline
645#ifdef FEAT_DIFF
646 && curwin->w_topfill == old_topfill
647#endif
648 )
649 {
650 mincol = curwin->w_wcol;
651 validate_cursor_col();
652
653 if ((int)curwin->w_wcol < (int)mincol - curbuf->b_p_ts
654 && curwin->w_wrow == W_WINROW(curwin)
655 + curwin->w_height - 1 - p_so
656 && (curwin->w_cursor.lnum != curwin->w_topline
657#ifdef FEAT_DIFF
658 || curwin->w_topfill > 0
659#endif
660 ))
661 {
662#ifdef FEAT_DIFF
663 if (curwin->w_topfill > 0)
664 --curwin->w_topfill;
665 else
666#endif
667#ifdef FEAT_FOLDING
668 if (hasFolding(curwin->w_topline, NULL, &old_topline))
669 set_topline(curwin, old_topline + 1);
670 else
671#endif
672 set_topline(curwin, curwin->w_topline + 1);
673 }
674 }
675
676 /* May need to adjust w_topline to show the cursor. */
677 update_topline();
678
679 did_backspace = FALSE;
680
681 validate_cursor(); /* may set must_redraw */
682
683 /*
684 * Redraw the display when no characters are waiting.
685 * Also shows mode, ruler and positions cursor.
686 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000687 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688
689#ifdef FEAT_SCROLLBIND
690 if (curwin->w_p_scb)
691 do_check_scrollbind(TRUE);
692#endif
693
694 update_curswant();
695 old_topline = curwin->w_topline;
696#ifdef FEAT_DIFF
697 old_topfill = curwin->w_topfill;
698#endif
699
700#ifdef USE_ON_FLY_SCROLL
701 dont_scroll = FALSE; /* allow scrolling here */
702#endif
703
704 /*
705 * Get a character for Insert mode.
706 */
707 lastc = c; /* remember previous char for CTRL-D */
708 c = safe_vgetc();
709
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000710#ifdef FEAT_AUTOCMD
711 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
712 did_cursorhold = TRUE;
713#endif
714
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715#ifdef FEAT_RIGHTLEFT
716 if (p_hkmap && KeyTyped)
717 c = hkmap(c); /* Hebrew mode mapping */
718#endif
719#ifdef FEAT_FKMAP
720 if (p_fkmap && KeyTyped)
721 c = fkmap(c); /* Farsi mode mapping */
722#endif
723
724#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000725 /*
726 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000727 * and the cursor is still in the completed word. Only when there is
728 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000729 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000730 if (compl_started
731 && pum_wanted()
732 && curwin->w_cursor.col >= compl_col
733 && (compl_shown_match == NULL
734 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000735 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000736 /* BS: Delete one character from "compl_leader". */
737 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000738 && curwin->w_cursor.col > compl_col
739 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000740 continue;
741
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000742 /* When no match was selected or it was edited. */
743 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000744 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000745 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000746 * "compl_leader". Except when at the original match and
747 * there is nothing to add, CTRL-L works like CTRL-P then. */
748 if (c == Ctrl_L
749 && (ctrl_x_mode != CTRL_X_WHOLE_LINE
750 || STRLEN(compl_shown_match->cp_str)
751 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000752 {
753 ins_compl_addfrommatch();
754 continue;
755 }
756
Bram Moolenaardf1bdc92006-02-23 21:32:16 +0000757 /* A printable, non-white character: Add to "compl_leader". */
758 if (vim_isprintc(c) && !vim_iswhite(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000759 {
760 ins_compl_addleader(c);
761 continue;
762 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000763
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000764 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000765 * compl_enter_selects is set the Enter key does the same. */
766 if (c == Ctrl_Y || (compl_enter_selects
767 && (c == CAR || c == K_KENTER || c == NL)))
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000768 {
769 ins_compl_delete();
770 ins_compl_insert();
771 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000772 }
773 }
774
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
776 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000777 compl_get_longest = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +0000778 if (c != K_IGNORE && ins_compl_prep(c))
779 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780#endif
781
Bram Moolenaar488c6512005-08-11 20:09:58 +0000782 /* CTRL-\ CTRL-N goes to Normal mode,
783 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
784 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 if (c == Ctrl_BSL)
786 {
787 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000788 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 ++no_mapping;
790 ++allow_keys;
791 c = safe_vgetc();
792 --no_mapping;
793 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000794 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000796 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 vungetc(c);
798 c = Ctrl_BSL;
799 }
800 else if (c == Ctrl_G && p_im)
801 continue;
802 else
803 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000804 if (c == Ctrl_O)
805 {
806 ins_ctrl_o();
807 ins_at_eol = FALSE; /* cursor keeps its column */
808 nomove = TRUE;
809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 count = 0;
811 goto doESCkey;
812 }
813 }
814
815#ifdef FEAT_DIGRAPHS
816 c = do_digraph(c);
817#endif
818
819#ifdef FEAT_INS_EXPAND
820 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
821 goto docomplete;
822#endif
823 if (c == Ctrl_V || c == Ctrl_Q)
824 {
825 ins_ctrl_v();
826 c = Ctrl_V; /* pretend CTRL-V is last typed character */
827 continue;
828 }
829
830#ifdef FEAT_CINDENT
831 if (cindent_on()
832# ifdef FEAT_INS_EXPAND
833 && ctrl_x_mode == 0
834# endif
835 )
836 {
837 /* A key name preceded by a bang means this key is not to be
838 * inserted. Skip ahead to the re-indenting below.
839 * A key name preceded by a star means that indenting has to be
840 * done before inserting the key. */
841 line_is_white = inindent(0);
842 if (in_cinkeys(c, '!', line_is_white))
843 goto force_cindent;
844 if (can_cindent && in_cinkeys(c, '*', line_is_white)
845 && stop_arrow() == OK)
846 do_c_expr_indent();
847 }
848#endif
849
850#ifdef FEAT_RIGHTLEFT
851 if (curwin->w_p_rl)
852 switch (c)
853 {
854 case K_LEFT: c = K_RIGHT; break;
855 case K_S_LEFT: c = K_S_RIGHT; break;
856 case K_C_LEFT: c = K_C_RIGHT; break;
857 case K_RIGHT: c = K_LEFT; break;
858 case K_S_RIGHT: c = K_S_LEFT; break;
859 case K_C_RIGHT: c = K_C_LEFT; break;
860 }
861#endif
862
863#ifdef FEAT_VISUAL
864 /*
865 * If 'keymodel' contains "startsel", may start selection. If it
866 * does, a CTRL-O and c will be stuffed, we need to get these
867 * characters.
868 */
869 if (ins_start_select(c))
870 continue;
871#endif
872
873 /*
874 * The big switch to handle a character in insert mode.
875 */
876 switch (c)
877 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000878 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 if (echeck_abbr(ESC + ABBR_OFF))
880 break;
881 /*FALLTHROUGH*/
882
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000883 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884#ifdef FEAT_CMDWIN
885 if (c == Ctrl_C && cmdwin_type != 0)
886 {
887 /* Close the cmdline window. */
888 cmdwin_result = K_IGNORE;
889 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000890 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 goto doESCkey;
892 }
893#endif
894
895#ifdef UNIX
896do_intr:
897#endif
898 /* when 'insertmode' set, and not halfway a mapping, don't leave
899 * Insert mode */
900 if (goto_im())
901 {
902 if (got_int)
903 {
904 (void)vgetc(); /* flush all buffers */
905 got_int = FALSE;
906 }
907 else
908 vim_beep();
909 break;
910 }
911doESCkey:
912 /*
913 * This is the ONLY return from edit()!
914 */
915 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
916 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000917 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 o_lnum = curwin->w_cursor.lnum;
919
Bram Moolenaar488c6512005-08-11 20:09:58 +0000920 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000921 {
922#ifdef FEAT_AUTOCMD
923 if (cmdchar != 'r' && cmdchar != 'v')
924 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
925 FALSE, curbuf);
926#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 continue;
930
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000931 case Ctrl_Z: /* suspend when 'insertmode' set */
932 if (!p_im)
933 goto normalchar; /* insert CTRL-Z as normal char */
934 stuffReadbuff((char_u *)":st\r");
935 c = Ctrl_O;
936 /*FALLTHROUGH*/
937
938 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000939#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000940 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000941 goto docomplete;
942#endif
943 if (echeck_abbr(Ctrl_O + ABBR_OFF))
944 break;
945 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000946
947#ifdef FEAT_VIRTUALEDIT
948 /* don't move the cursor left when 'virtualedit' has "onemore". */
949 if (ve_flags & VE_ONEMORE)
950 {
951 ins_at_eol = FALSE;
952 nomove = TRUE;
953 }
954#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000955 count = 0;
956 goto doESCkey;
957
Bram Moolenaar572cb562005-08-05 21:35:02 +0000958 case K_INS: /* toggle insert/replace mode */
959 case K_KINS:
960 ins_insert(replaceState);
961 break;
962
963 case K_SELECT: /* end of Select mode mapping - ignore */
964 break;
965
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000966#ifdef FEAT_SNIFF
967 case K_SNIFF: /* Sniff command received */
968 stuffcharReadbuff(K_SNIFF);
969 goto doESCkey;
970#endif
971
972 case K_HELP: /* Help key works like <ESC> <Help> */
973 case K_F1:
974 case K_XF1:
975 stuffcharReadbuff(K_HELP);
976 if (p_im)
977 need_start_insertmode = TRUE;
978 goto doESCkey;
979
980#ifdef FEAT_NETBEANS_INTG
981 case K_F21: /* NetBeans command */
982 ++no_mapping; /* don't map the next key hits */
983 i = safe_vgetc();
984 --no_mapping;
985 netbeans_keycommand(i);
986 break;
987#endif
988
989 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 case NUL:
991 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000992 /* For ^@ the trailing ESC will end the insert, unless there is an
993 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
995 && c != Ctrl_A && !p_im)
996 goto doESCkey; /* quit insert mode */
997 inserted_space = FALSE;
998 break;
999
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001000 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 ins_reg();
1002 auto_format(FALSE, TRUE);
1003 inserted_space = FALSE;
1004 break;
1005
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001006 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 ins_ctrl_g();
1008 break;
1009
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001010 case Ctrl_HAT: /* switch input mode and/or langmap */
1011 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 break;
1013
1014#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001015 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 if (!p_ari)
1017 goto normalchar;
1018 ins_ctrl_();
1019 break;
1020#endif
1021
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001022 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1024 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1025 goto docomplete;
1026#endif
1027 /* FALLTHROUGH */
1028
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001029 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030# ifdef FEAT_INS_EXPAND
1031 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1032 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001033 if (has_compl_option(FALSE))
1034 goto docomplete;
1035 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 }
1037# endif
1038 ins_shift(c, lastc);
1039 auto_format(FALSE, TRUE);
1040 inserted_space = FALSE;
1041 break;
1042
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001043 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 case K_KDEL:
1045 ins_del();
1046 auto_format(FALSE, TRUE);
1047 break;
1048
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001049 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 case Ctrl_H:
1051 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1052 auto_format(FALSE, TRUE);
1053 break;
1054
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001055 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1057 auto_format(FALSE, TRUE);
1058 break;
1059
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001060 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001061# ifdef FEAT_COMPL_FUNC
1062 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001063 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001064 goto docomplete;
1065# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1067 auto_format(FALSE, TRUE);
1068 inserted_space = FALSE;
1069 break;
1070
1071#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001072 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 case K_LEFTMOUSE_NM:
1074 case K_LEFTDRAG:
1075 case K_LEFTRELEASE:
1076 case K_LEFTRELEASE_NM:
1077 case K_MIDDLEMOUSE:
1078 case K_MIDDLEDRAG:
1079 case K_MIDDLERELEASE:
1080 case K_RIGHTMOUSE:
1081 case K_RIGHTDRAG:
1082 case K_RIGHTRELEASE:
1083 case K_X1MOUSE:
1084 case K_X1DRAG:
1085 case K_X1RELEASE:
1086 case K_X2MOUSE:
1087 case K_X2DRAG:
1088 case K_X2RELEASE:
1089 ins_mouse(c);
1090 break;
1091
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001092 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 ins_mousescroll(FALSE);
1094 break;
1095
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001096 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 ins_mousescroll(TRUE);
1098 break;
1099#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001100#ifdef FEAT_GUI_TABLINE
1101 case K_TABLINE:
1102 case K_TABMENU:
1103 ins_tabline(c);
1104 break;
1105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001107 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 break;
1109
Bram Moolenaar754b5602006-02-09 23:53:20 +00001110#ifdef FEAT_AUTOCMD
1111 case K_CURSORHOLD: /* Didn't type something for a while. */
1112 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1113 did_cursorhold = TRUE;
1114 break;
1115#endif
1116
Bram Moolenaar4770d092006-01-12 23:22:24 +00001117#ifdef FEAT_GUI_W32
1118 /* On Win32 ignore <M-F4>, we get it when closing the window was
1119 * cancelled. */
1120 case K_F4:
1121 if (mod_mask != MOD_MASK_ALT)
1122 goto normalchar;
1123 break;
1124#endif
1125
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126#ifdef FEAT_GUI
1127 case K_VER_SCROLLBAR:
1128 ins_scroll();
1129 break;
1130
1131 case K_HOR_SCROLLBAR:
1132 ins_horscroll();
1133 break;
1134#endif
1135
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001136 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 case K_S_HOME:
1139 case K_C_HOME:
1140 ins_home(c);
1141 break;
1142
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001143 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 case K_S_END:
1146 case K_C_END:
1147 ins_end(c);
1148 break;
1149
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001150 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001151 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1152 ins_s_left();
1153 else
1154 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 break;
1156
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001157 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 case K_C_LEFT:
1159 ins_s_left();
1160 break;
1161
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001162 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001163 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1164 ins_s_right();
1165 else
1166 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 break;
1168
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001169 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 case K_C_RIGHT:
1171 ins_s_right();
1172 break;
1173
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001174 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001175#ifdef FEAT_INS_EXPAND
1176 if (pum_visible())
1177 goto docomplete;
1178#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001179 if (mod_mask & MOD_MASK_SHIFT)
1180 ins_pageup();
1181 else
1182 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 break;
1184
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001185 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 case K_PAGEUP:
1187 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001188#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001189 if (pum_visible())
1190 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 ins_pageup();
1193 break;
1194
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001195 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001196#ifdef FEAT_INS_EXPAND
1197 if (pum_visible())
1198 goto docomplete;
1199#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001200 if (mod_mask & MOD_MASK_SHIFT)
1201 ins_pagedown();
1202 else
1203 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 break;
1205
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001206 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 case K_PAGEDOWN:
1208 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001209#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001210 if (pum_visible())
1211 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 ins_pagedown();
1214 break;
1215
1216#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001217 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 ins_drop();
1219 break;
1220#endif
1221
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001222 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 c = TAB;
1224 /* FALLTHROUGH */
1225
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001226 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1228 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1229 goto docomplete;
1230#endif
1231 inserted_space = FALSE;
1232 if (ins_tab())
1233 goto normalchar; /* insert TAB as a normal char */
1234 auto_format(FALSE, TRUE);
1235 break;
1236
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001237 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 c = CAR;
1239 /* FALLTHROUGH */
1240 case CAR:
1241 case NL:
1242#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1243 /* In a quickfix window a <CR> jumps to the error under the
1244 * cursor. */
1245 if (bt_quickfix(curbuf) && c == CAR)
1246 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001247 if (curwin->w_llist_ref == NULL) /* quickfix window */
1248 do_cmdline_cmd((char_u *)".cc");
1249 else /* location list window */
1250 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 break;
1252 }
1253#endif
1254#ifdef FEAT_CMDWIN
1255 if (cmdwin_type != 0)
1256 {
1257 /* Execute the command in the cmdline window. */
1258 cmdwin_result = CAR;
1259 goto doESCkey;
1260 }
1261#endif
1262 if (ins_eol(c) && !p_im)
1263 goto doESCkey; /* out of memory */
1264 auto_format(FALSE, FALSE);
1265 inserted_space = FALSE;
1266 break;
1267
1268#if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001269 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270# ifdef FEAT_INS_EXPAND
1271 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1272 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001273 if (has_compl_option(TRUE))
1274 goto docomplete;
1275 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 }
1277# endif
1278# ifdef FEAT_DIGRAPHS
1279 c = ins_digraph();
1280 if (c == NUL)
1281 break;
1282# endif
1283 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285
1286#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001287 case Ctrl_X: /* Enter CTRL-X mode */
1288 ins_ctrl_x();
1289 break;
1290
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001291 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 if (ctrl_x_mode != CTRL_X_TAGS)
1293 goto normalchar;
1294 goto docomplete;
1295
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001296 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 if (ctrl_x_mode != CTRL_X_FILES)
1298 goto normalchar;
1299 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001300
1301 case 's': /* Spelling completion after ^X */
1302 case Ctrl_S:
1303 if (ctrl_x_mode != CTRL_X_SPELL)
1304 goto normalchar;
1305 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306#endif
1307
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001308 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309#ifdef FEAT_INS_EXPAND
1310 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1311#endif
1312 {
1313 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1314 if (p_im)
1315 {
1316 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1317 break;
1318 goto doESCkey;
1319 }
1320 goto normalchar;
1321 }
1322#ifdef FEAT_INS_EXPAND
1323 /* FALLTHROUGH */
1324
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001325 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 case Ctrl_N:
1327 /* if 'complete' is empty then plain ^P is no longer special,
1328 * but it is under other ^X modes */
1329 if (*curbuf->b_p_cpt == NUL
1330 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001331 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 goto normalchar;
1333
1334docomplete:
1335 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001336 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 break;
1338#endif /* FEAT_INS_EXPAND */
1339
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001340 case Ctrl_Y: /* copy from previous line or scroll down */
1341 case Ctrl_E: /* copy from next line or scroll up */
1342 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 break;
1344
1345 default:
1346#ifdef UNIX
1347 if (c == intr_char) /* special interrupt char */
1348 goto do_intr;
1349#endif
1350
1351 /*
1352 * Insert a nomal character.
1353 */
1354normalchar:
1355#ifdef FEAT_SMARTINDENT
1356 /* Try to perform smart-indenting. */
1357 ins_try_si(c);
1358#endif
1359
1360 if (c == ' ')
1361 {
1362 inserted_space = TRUE;
1363#ifdef FEAT_CINDENT
1364 if (inindent(0))
1365 can_cindent = FALSE;
1366#endif
1367 if (Insstart_blank_vcol == MAXCOL
1368 && curwin->w_cursor.lnum == Insstart.lnum)
1369 Insstart_blank_vcol = get_nolist_virtcol();
1370 }
1371
1372 if (vim_iswordc(c) || !echeck_abbr(
1373#ifdef FEAT_MBYTE
1374 /* Add ABBR_OFF for characters above 0x100, this is
1375 * what check_abbr() expects. */
1376 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1377#endif
1378 c))
1379 {
1380 insert_special(c, FALSE, FALSE);
1381#ifdef FEAT_RIGHTLEFT
1382 revins_legal++;
1383 revins_chars++;
1384#endif
1385 }
1386
1387 auto_format(FALSE, TRUE);
1388
1389#ifdef FEAT_FOLDING
1390 /* When inserting a character the cursor line must never be in a
1391 * closed fold. */
1392 foldOpenCursor();
1393#endif
1394 break;
1395 } /* end of switch (c) */
1396
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001397#ifdef FEAT_AUTOCMD
1398 /* If typed something may trigger CursorHoldI again. */
1399 if (c != K_CURSORHOLD)
1400 did_cursorhold = FALSE;
1401#endif
1402
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 /* If the cursor was moved we didn't just insert a space */
1404 if (arrow_used)
1405 inserted_space = FALSE;
1406
1407#ifdef FEAT_CINDENT
1408 if (can_cindent && cindent_on()
1409# ifdef FEAT_INS_EXPAND
1410 && ctrl_x_mode == 0
1411# endif
1412 )
1413 {
1414force_cindent:
1415 /*
1416 * Indent now if a key was typed that is in 'cinkeys'.
1417 */
1418 if (in_cinkeys(c, ' ', line_is_white))
1419 {
1420 if (stop_arrow() == OK)
1421 /* re-indent the current line */
1422 do_c_expr_indent();
1423 }
1424 }
1425#endif /* FEAT_CINDENT */
1426
1427 } /* for (;;) */
1428 /* NOTREACHED */
1429}
1430
1431/*
1432 * Redraw for Insert mode.
1433 * This is postponed until getting the next character to make '$' in the 'cpo'
1434 * option work correctly.
1435 * Only redraw when there are no characters available. This speeds up
1436 * inserting sequences of characters (e.g., for CTRL-R).
1437 */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001438/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001440ins_redraw(ready)
1441 int ready; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442{
1443 if (!char_avail())
1444 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00001445#ifdef FEAT_AUTOCMD
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001446 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1447 * visible, the command might delete it. */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001448 if (ready && has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001449 && !equalpos(last_cursormoved, curwin->w_cursor)
1450# ifdef FEAT_INS_EXPAND
1451 && !pum_visible()
1452# endif
1453 )
Bram Moolenaar754b5602006-02-09 23:53:20 +00001454 {
1455 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1456 last_cursormoved = curwin->w_cursor;
1457 }
1458#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 if (must_redraw)
1460 update_screen(0);
1461 else if (clear_cmdline || redraw_cmdline)
1462 showmode(); /* clear cmdline and show mode */
1463 showruler(FALSE);
1464 setcursor();
1465 emsg_on_display = FALSE; /* may remove error message now */
1466 }
1467}
1468
1469/*
1470 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1471 */
1472 static void
1473ins_ctrl_v()
1474{
1475 int c;
1476
1477 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001478 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479
1480 if (redrawing() && !char_avail())
1481 edit_putchar('^', TRUE);
1482 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1483
1484#ifdef FEAT_CMDL_INFO
1485 add_to_showcmd_c(Ctrl_V);
1486#endif
1487
1488 c = get_literal();
1489#ifdef FEAT_CMDL_INFO
1490 clear_showcmd();
1491#endif
1492 insert_special(c, FALSE, TRUE);
1493#ifdef FEAT_RIGHTLEFT
1494 revins_chars++;
1495 revins_legal++;
1496#endif
1497}
1498
1499/*
1500 * Put a character directly onto the screen. It's not stored in a buffer.
1501 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1502 */
1503static int pc_status;
1504#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1505#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1506#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1507#define PC_STATUS_SET 3 /* pc_bytes was filled */
1508#ifdef FEAT_MBYTE
1509static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1510#else
1511static char_u pc_bytes[2]; /* saved bytes */
1512#endif
1513static int pc_attr;
1514static int pc_row;
1515static int pc_col;
1516
1517 void
1518edit_putchar(c, highlight)
1519 int c;
1520 int highlight;
1521{
1522 int attr;
1523
1524 if (ScreenLines != NULL)
1525 {
1526 update_topline(); /* just in case w_topline isn't valid */
1527 validate_cursor();
1528 if (highlight)
1529 attr = hl_attr(HLF_8);
1530 else
1531 attr = 0;
1532 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1533 pc_col = W_WINCOL(curwin);
1534#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1535 pc_status = PC_STATUS_UNSET;
1536#endif
1537#ifdef FEAT_RIGHTLEFT
1538 if (curwin->w_p_rl)
1539 {
1540 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1541# ifdef FEAT_MBYTE
1542 if (has_mbyte)
1543 {
1544 int fix_col = mb_fix_col(pc_col, pc_row);
1545
1546 if (fix_col != pc_col)
1547 {
1548 screen_putchar(' ', pc_row, fix_col, attr);
1549 --curwin->w_wcol;
1550 pc_status = PC_STATUS_RIGHT;
1551 }
1552 }
1553# endif
1554 }
1555 else
1556#endif
1557 {
1558 pc_col += curwin->w_wcol;
1559#ifdef FEAT_MBYTE
1560 if (mb_lefthalve(pc_row, pc_col))
1561 pc_status = PC_STATUS_LEFT;
1562#endif
1563 }
1564
1565 /* save the character to be able to put it back */
1566#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1567 if (pc_status == PC_STATUS_UNSET)
1568#endif
1569 {
1570 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1571 pc_status = PC_STATUS_SET;
1572 }
1573 screen_putchar(c, pc_row, pc_col, attr);
1574 }
1575}
1576
1577/*
1578 * Undo the previous edit_putchar().
1579 */
1580 void
1581edit_unputchar()
1582{
1583 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1584 {
1585#if defined(FEAT_MBYTE)
1586 if (pc_status == PC_STATUS_RIGHT)
1587 ++curwin->w_wcol;
1588 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1589 redrawWinline(curwin->w_cursor.lnum, FALSE);
1590 else
1591#endif
1592 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1593 }
1594}
1595
1596/*
1597 * Called when p_dollar is set: display a '$' at the end of the changed text
1598 * Only works when cursor is in the line that changes.
1599 */
1600 void
1601display_dollar(col)
1602 colnr_T col;
1603{
1604 colnr_T save_col;
1605
1606 if (!redrawing())
1607 return;
1608
1609 cursor_off();
1610 save_col = curwin->w_cursor.col;
1611 curwin->w_cursor.col = col;
1612#ifdef FEAT_MBYTE
1613 if (has_mbyte)
1614 {
1615 char_u *p;
1616
1617 /* If on the last byte of a multi-byte move to the first byte. */
1618 p = ml_get_curline();
1619 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1620 }
1621#endif
1622 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1623 if (curwin->w_wcol < W_WIDTH(curwin))
1624 {
1625 edit_putchar('$', FALSE);
1626 dollar_vcol = curwin->w_virtcol;
1627 }
1628 curwin->w_cursor.col = save_col;
1629}
1630
1631/*
1632 * Call this function before moving the cursor from the normal insert position
1633 * in insert mode.
1634 */
1635 static void
1636undisplay_dollar()
1637{
1638 if (dollar_vcol)
1639 {
1640 dollar_vcol = 0;
1641 redrawWinline(curwin->w_cursor.lnum, FALSE);
1642 }
1643}
1644
1645/*
1646 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1647 * Keep the cursor on the same character.
1648 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1649 * type == INDENT_DEC decrease indent (for CTRL-D)
1650 * type == INDENT_SET set indent to "amount"
1651 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1652 */
1653 void
1654change_indent(type, amount, round, replaced)
1655 int type;
1656 int amount;
1657 int round;
1658 int replaced; /* replaced character, put on replace stack */
1659{
1660 int vcol;
1661 int last_vcol;
1662 int insstart_less; /* reduction for Insstart.col */
1663 int new_cursor_col;
1664 int i;
1665 char_u *ptr;
1666 int save_p_list;
1667 int start_col;
1668 colnr_T vc;
1669#ifdef FEAT_VREPLACE
1670 colnr_T orig_col = 0; /* init for GCC */
1671 char_u *new_line, *orig_line = NULL; /* init for GCC */
1672
1673 /* VREPLACE mode needs to know what the line was like before changing */
1674 if (State & VREPLACE_FLAG)
1675 {
1676 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1677 orig_col = curwin->w_cursor.col;
1678 }
1679#endif
1680
1681 /* for the following tricks we don't want list mode */
1682 save_p_list = curwin->w_p_list;
1683 curwin->w_p_list = FALSE;
1684 vc = getvcol_nolist(&curwin->w_cursor);
1685 vcol = vc;
1686
1687 /*
1688 * For Replace mode we need to fix the replace stack later, which is only
1689 * possible when the cursor is in the indent. Remember the number of
1690 * characters before the cursor if it's possible.
1691 */
1692 start_col = curwin->w_cursor.col;
1693
1694 /* determine offset from first non-blank */
1695 new_cursor_col = curwin->w_cursor.col;
1696 beginline(BL_WHITE);
1697 new_cursor_col -= curwin->w_cursor.col;
1698
1699 insstart_less = curwin->w_cursor.col;
1700
1701 /*
1702 * If the cursor is in the indent, compute how many screen columns the
1703 * cursor is to the left of the first non-blank.
1704 */
1705 if (new_cursor_col < 0)
1706 vcol = get_indent() - vcol;
1707
1708 if (new_cursor_col > 0) /* can't fix replace stack */
1709 start_col = -1;
1710
1711 /*
1712 * Set the new indent. The cursor will be put on the first non-blank.
1713 */
1714 if (type == INDENT_SET)
1715 (void)set_indent(amount, SIN_CHANGED);
1716 else
1717 {
1718#ifdef FEAT_VREPLACE
1719 int save_State = State;
1720
1721 /* Avoid being called recursively. */
1722 if (State & VREPLACE_FLAG)
1723 State = INSERT;
1724#endif
1725 shift_line(type == INDENT_DEC, round, 1);
1726#ifdef FEAT_VREPLACE
1727 State = save_State;
1728#endif
1729 }
1730 insstart_less -= curwin->w_cursor.col;
1731
1732 /*
1733 * Try to put cursor on same character.
1734 * If the cursor is at or after the first non-blank in the line,
1735 * compute the cursor column relative to the column of the first
1736 * non-blank character.
1737 * If we are not in insert mode, leave the cursor on the first non-blank.
1738 * If the cursor is before the first non-blank, position it relative
1739 * to the first non-blank, counted in screen columns.
1740 */
1741 if (new_cursor_col >= 0)
1742 {
1743 /*
1744 * When changing the indent while the cursor is touching it, reset
1745 * Insstart_col to 0.
1746 */
1747 if (new_cursor_col == 0)
1748 insstart_less = MAXCOL;
1749 new_cursor_col += curwin->w_cursor.col;
1750 }
1751 else if (!(State & INSERT))
1752 new_cursor_col = curwin->w_cursor.col;
1753 else
1754 {
1755 /*
1756 * Compute the screen column where the cursor should be.
1757 */
1758 vcol = get_indent() - vcol;
1759 curwin->w_virtcol = (vcol < 0) ? 0 : vcol;
1760
1761 /*
1762 * Advance the cursor until we reach the right screen column.
1763 */
1764 vcol = last_vcol = 0;
1765 new_cursor_col = -1;
1766 ptr = ml_get_curline();
1767 while (vcol <= (int)curwin->w_virtcol)
1768 {
1769 last_vcol = vcol;
1770#ifdef FEAT_MBYTE
1771 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001772 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 else
1774#endif
1775 ++new_cursor_col;
1776 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1777 }
1778 vcol = last_vcol;
1779
1780 /*
1781 * May need to insert spaces to be able to position the cursor on
1782 * the right screen column.
1783 */
1784 if (vcol != (int)curwin->w_virtcol)
1785 {
1786 curwin->w_cursor.col = new_cursor_col;
1787 i = (int)curwin->w_virtcol - vcol;
1788 ptr = alloc(i + 1);
1789 if (ptr != NULL)
1790 {
1791 new_cursor_col += i;
1792 ptr[i] = NUL;
1793 while (--i >= 0)
1794 ptr[i] = ' ';
1795 ins_str(ptr);
1796 vim_free(ptr);
1797 }
1798 }
1799
1800 /*
1801 * When changing the indent while the cursor is in it, reset
1802 * Insstart_col to 0.
1803 */
1804 insstart_less = MAXCOL;
1805 }
1806
1807 curwin->w_p_list = save_p_list;
1808
1809 if (new_cursor_col <= 0)
1810 curwin->w_cursor.col = 0;
1811 else
1812 curwin->w_cursor.col = new_cursor_col;
1813 curwin->w_set_curswant = TRUE;
1814 changed_cline_bef_curs();
1815
1816 /*
1817 * May have to adjust the start of the insert.
1818 */
1819 if (State & INSERT)
1820 {
1821 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1822 {
1823 if ((int)Insstart.col <= insstart_less)
1824 Insstart.col = 0;
1825 else
1826 Insstart.col -= insstart_less;
1827 }
1828 if ((int)ai_col <= insstart_less)
1829 ai_col = 0;
1830 else
1831 ai_col -= insstart_less;
1832 }
1833
1834 /*
1835 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1836 * If the number of characters before the cursor decreased, need to pop a
1837 * few characters from the replace stack.
1838 * If the number of characters before the cursor increased, need to push a
1839 * few NULs onto the replace stack.
1840 */
1841 if (REPLACE_NORMAL(State) && start_col >= 0)
1842 {
1843 while (start_col > (int)curwin->w_cursor.col)
1844 {
1845 replace_join(0); /* remove a NUL from the replace stack */
1846 --start_col;
1847 }
1848 while (start_col < (int)curwin->w_cursor.col || replaced)
1849 {
1850 replace_push(NUL);
1851 if (replaced)
1852 {
1853 replace_push(replaced);
1854 replaced = NUL;
1855 }
1856 ++start_col;
1857 }
1858 }
1859
1860#ifdef FEAT_VREPLACE
1861 /*
1862 * For VREPLACE mode, we also have to fix the replace stack. In this case
1863 * it is always possible because we backspace over the whole line and then
1864 * put it back again the way we wanted it.
1865 */
1866 if (State & VREPLACE_FLAG)
1867 {
1868 /* If orig_line didn't allocate, just return. At least we did the job,
1869 * even if you can't backspace. */
1870 if (orig_line == NULL)
1871 return;
1872
1873 /* Save new line */
1874 new_line = vim_strsave(ml_get_curline());
1875 if (new_line == NULL)
1876 return;
1877
1878 /* We only put back the new line up to the cursor */
1879 new_line[curwin->w_cursor.col] = NUL;
1880
1881 /* Put back original line */
1882 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1883 curwin->w_cursor.col = orig_col;
1884
1885 /* Backspace from cursor to start of line */
1886 backspace_until_column(0);
1887
1888 /* Insert new stuff into line again */
1889 ins_bytes(new_line);
1890
1891 vim_free(new_line);
1892 }
1893#endif
1894}
1895
1896/*
1897 * Truncate the space at the end of a line. This is to be used only in an
1898 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1899 * modes.
1900 */
1901 void
1902truncate_spaces(line)
1903 char_u *line;
1904{
1905 int i;
1906
1907 /* find start of trailing white space */
1908 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1909 {
1910 if (State & REPLACE_FLAG)
1911 replace_join(0); /* remove a NUL from the replace stack */
1912 }
1913 line[i + 1] = NUL;
1914}
1915
1916#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1917 || defined(FEAT_COMMENTS) || defined(PROTO)
1918/*
1919 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1920 * modes correctly. May also be used when not in insert mode at all.
1921 */
1922 void
1923backspace_until_column(col)
1924 int col;
1925{
1926 while ((int)curwin->w_cursor.col > col)
1927 {
1928 curwin->w_cursor.col--;
1929 if (State & REPLACE_FLAG)
1930 replace_do_bs();
1931 else
1932 (void)del_char(FALSE);
1933 }
1934}
1935#endif
1936
1937#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1938/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001939 * CTRL-X pressed in Insert mode.
1940 */
1941 static void
1942ins_ctrl_x()
1943{
1944 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
1945 * CTRL-V works like CTRL-N */
1946 if (ctrl_x_mode != CTRL_X_CMDLINE)
1947 {
1948 /* if the next ^X<> won't ADD nothing, then reset
1949 * compl_cont_status */
1950 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001951 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001952 else
1953 compl_cont_status = 0;
1954 /* We're not sure which CTRL-X mode it will be yet */
1955 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
1956 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
1957 edit_submode_pre = NULL;
1958 showmode();
1959 }
1960}
1961
1962/*
1963 * Return TRUE if the 'dict' or 'tsr' option can be used.
1964 */
1965 static int
1966has_compl_option(dict_opt)
1967 int dict_opt;
1968{
Bram Moolenaar0b238792006-03-02 22:49:12 +00001969 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00001970# ifdef FEAT_SPELL
1971 && !curwin->w_p_spell
1972# endif
1973 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001974 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
1975 {
1976 ctrl_x_mode = 0;
1977 edit_submode = NULL;
1978 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
1979 : (char_u *)_("'thesaurus' option is empty"),
1980 hl_attr(HLF_E));
1981 if (emsg_silent == 0)
1982 {
1983 vim_beep();
1984 setcursor();
1985 out_flush();
1986 ui_delay(2000L, FALSE);
1987 }
1988 return FALSE;
1989 }
1990 return TRUE;
1991}
1992
1993/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
1995 * This depends on the current mode.
1996 */
1997 int
1998vim_is_ctrl_x_key(c)
1999 int c;
2000{
2001 /* Always allow ^R - let it's results then be checked */
2002 if (c == Ctrl_R)
2003 return TRUE;
2004
Bram Moolenaare3226be2005-12-18 22:10:00 +00002005 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002006 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002007 return TRUE;
2008
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 switch (ctrl_x_mode)
2010 {
2011 case 0: /* Not in any CTRL-X mode */
2012 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2013 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002014 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2016 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2017 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002018 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
2019 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 case CTRL_X_SCROLL:
2021 return (c == Ctrl_Y || c == Ctrl_E);
2022 case CTRL_X_WHOLE_LINE:
2023 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2024 case CTRL_X_FILES:
2025 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2026 case CTRL_X_DICTIONARY:
2027 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2028 case CTRL_X_THESAURUS:
2029 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2030 case CTRL_X_TAGS:
2031 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2032#ifdef FEAT_FIND_ID
2033 case CTRL_X_PATH_PATTERNS:
2034 return (c == Ctrl_P || c == Ctrl_N);
2035 case CTRL_X_PATH_DEFINES:
2036 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2037#endif
2038 case CTRL_X_CMDLINE:
2039 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2040 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002041#ifdef FEAT_COMPL_FUNC
2042 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002043 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002044 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002045 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002046#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002047 case CTRL_X_SPELL:
2048 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 }
2050 EMSG(_(e_internal));
2051 return FALSE;
2052}
2053
2054/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002055 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 * case of the originally typed text is used, and the case of the completed
2057 * text is infered, ie this tries to work out what case you probably wanted
2058 * the rest of the word to be in -- webb
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002059 * TODO: make this work for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 */
2061 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002062ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 char_u *str;
2064 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002065 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 char_u *fname;
2067 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002068 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069{
2070 int has_lower = FALSE;
2071 int was_letter = FALSE;
2072 int idx;
2073
2074 if (p_ic && curbuf->b_p_inf && len < IOSIZE)
2075 {
2076 /* Infer case of completed part -- webb */
2077 /* Use IObuff, str would change text in buffer! */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002078 vim_strncpy(IObuff, str, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079
2080 /* Rule 1: Were any chars converted to lower? */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002081 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002083 if (islower(compl_orig_text[idx]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 {
2085 has_lower = TRUE;
2086 if (isupper(IObuff[idx]))
2087 {
2088 /* Rule 1 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002089 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 IObuff[idx] = TOLOWER_LOC(IObuff[idx]);
2091 break;
2092 }
2093 }
2094 }
2095
2096 /*
2097 * Rule 2: No lower case, 2nd consecutive letter converted to
2098 * upper case.
2099 */
2100 if (!has_lower)
2101 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002102 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002104 if (was_letter && isupper(compl_orig_text[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 && islower(IObuff[idx]))
2106 {
2107 /* Rule 2 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002108 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 IObuff[idx] = TOUPPER_LOC(IObuff[idx]);
2110 break;
2111 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002112 was_letter = isalpha(compl_orig_text[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 }
2114 }
2115
2116 /* Copy the original case of the part we typed */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002117 STRNCPY(IObuff, compl_orig_text, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002119 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2120 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002122 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123}
2124
2125/*
2126 * Add a match to the list of matches.
2127 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002128 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002129 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002131 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00002132ins_compl_add(str, len, icase, fname, cptext, cdir, flags, adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 char_u *str;
2134 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002135 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 char_u *fname;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002137 char_u **cptext; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002138 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002139 int flags;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002140 int adup; /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002142 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002143 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144
2145 ui_breakcheck();
2146 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002147 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 if (len < 0)
2149 len = (int)STRLEN(str);
2150
2151 /*
2152 * If the same match is already present, don't add it.
2153 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002154 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002156 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 do
2158 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002159 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002160 && ins_compl_equal(match, str, len)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002161 && match->cp_str[len] == NUL)
2162 return NOTDONE;
2163 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002164 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 }
2166
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002167 /* Remove any popup menu before changing the list of matches. */
2168 ins_compl_del_pum();
2169
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 /*
2171 * Allocate a new match structure.
2172 * Copy the values to the new match structure.
2173 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002174 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002176 return FAIL;
2177 match->cp_number = -1;
2178 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002179 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002180 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 {
2182 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002183 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002185 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002186
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002188 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2190 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002191 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002192 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002193 && compl_curr_match->cp_fname != NULL
2194 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002195 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002196 else if (fname != NULL)
2197 {
2198 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002199 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002202 match->cp_fname = NULL;
2203 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002204
2205 if (cptext != NULL)
2206 {
2207 int i;
2208
2209 for (i = 0; i < CPT_COUNT; ++i)
2210 if (cptext[i] != NULL && *cptext[i] != NUL)
2211 match->cp_text[i] = vim_strsave(cptext[i]);
2212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213
2214 /*
2215 * Link the new match structure in the list of matches.
2216 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002217 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002218 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 else if (dir == FORWARD)
2220 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002221 match->cp_next = compl_curr_match->cp_next;
2222 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 }
2224 else /* BACKWARD */
2225 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002226 match->cp_next = compl_curr_match;
2227 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002229 if (match->cp_next)
2230 match->cp_next->cp_prev = match;
2231 if (match->cp_prev)
2232 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002234 compl_first_match = match;
2235 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002237 /*
2238 * Find the longest common string if still doing that.
2239 */
2240 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2241 ins_compl_longest_match(match);
2242
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 return OK;
2244}
2245
2246/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002247 * Return TRUE if "str[len]" matches with match->cp_str, considering
2248 * match->cp_icase.
2249 */
2250 static int
2251ins_compl_equal(match, str, len)
2252 compl_T *match;
2253 char_u *str;
2254 int len;
2255{
2256 if (match->cp_icase)
2257 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2258 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2259}
2260
2261/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002262 * Reduce the longest common string for match "match".
2263 */
2264 static void
2265ins_compl_longest_match(match)
2266 compl_T *match;
2267{
2268 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002269 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002270 int had_match;
2271
2272 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002273 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002274 /* First match, use it as a whole. */
2275 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002276 if (compl_leader != NULL)
2277 {
2278 had_match = (curwin->w_cursor.col > compl_col);
2279 ins_compl_delete();
2280 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2281 ins_redraw(FALSE);
2282
2283 /* When the match isn't there (to avoid matching itself) remove it
2284 * again after redrawing. */
2285 if (!had_match)
2286 ins_compl_delete();
2287 compl_used_match = FALSE;
2288 }
2289 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002290 else
2291 {
2292 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002293 p = compl_leader;
2294 s = match->cp_str;
2295 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002296 {
2297#ifdef FEAT_MBYTE
2298 if (has_mbyte)
2299 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002300 c1 = mb_ptr2char(p);
2301 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002302 }
2303 else
2304#endif
2305 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002306 c1 = *p;
2307 c2 = *s;
2308 }
2309 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2310 : (c1 != c2))
2311 break;
2312#ifdef FEAT_MBYTE
2313 if (has_mbyte)
2314 {
2315 mb_ptr_adv(p);
2316 mb_ptr_adv(s);
2317 }
2318 else
2319#endif
2320 {
2321 ++p;
2322 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002323 }
2324 }
2325
2326 if (*p != NUL)
2327 {
2328 /* Leader was shortened, need to change the inserted text. */
2329 *p = NUL;
2330 had_match = (curwin->w_cursor.col > compl_col);
2331 ins_compl_delete();
2332 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2333 ins_redraw(FALSE);
2334
2335 /* When the match isn't there (to avoid matching itself) remove it
2336 * again after redrawing. */
2337 if (!had_match)
2338 ins_compl_delete();
2339 }
2340
2341 compl_used_match = FALSE;
2342 }
2343}
2344
2345/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 * Add an array of matches to the list of matches.
2347 * Frees matches[].
2348 */
2349 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002350ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 int num_matches;
2352 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002353 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354{
2355 int i;
2356 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002357 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358
Bram Moolenaar572cb562005-08-05 21:35:02 +00002359 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002360 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002361 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002363 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 FreeWild(num_matches, matches);
2365}
2366
2367/* Make the completion list cyclic.
2368 * Return the number of matches (excluding the original).
2369 */
2370 static int
2371ins_compl_make_cyclic()
2372{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002373 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 int count = 0;
2375
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002376 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 {
2378 /*
2379 * Find the end of the list.
2380 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002381 match = compl_first_match;
2382 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002383 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002385 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 ++count;
2387 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002388 match->cp_next = compl_first_match;
2389 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 }
2391 return count;
2392}
2393
Bram Moolenaara94bc432006-03-10 21:42:59 +00002394/*
2395 * Start completion for the complete() function.
2396 * "startcol" is where the matched text starts (1 is first column).
2397 * "list" is the list of matches.
2398 */
2399 void
2400set_completion(startcol, list)
2401 int startcol;
2402 list_T *list;
2403{
2404 /* If already doing completions stop it. */
2405 if (ctrl_x_mode != 0)
2406 ins_compl_prep(' ');
2407 ins_compl_clear();
2408
2409 if (stop_arrow() == FAIL)
2410 return;
2411
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002412 if (startcol > (int)curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002413 startcol = curwin->w_cursor.col;
2414 compl_col = startcol;
2415 compl_length = curwin->w_cursor.col - startcol;
2416 /* compl_pattern doesn't need to be set */
2417 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2418 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002419 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002420 return;
2421
2422 /* Handle like dictionary completion. */
2423 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2424
2425 ins_compl_add_list(list);
2426 compl_matches = ins_compl_make_cyclic();
2427 compl_started = TRUE;
2428 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002429 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002430
2431 compl_curr_match = compl_first_match;
2432 ins_complete(Ctrl_N);
2433 out_flush();
2434}
2435
2436
Bram Moolenaar9372a112005-12-06 19:59:18 +00002437/* "compl_match_array" points the currently displayed list of entries in the
2438 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002439static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002440static int compl_match_arraysize;
2441
2442/*
2443 * Update the screen and when there is any scrolling remove the popup menu.
2444 */
2445 static void
2446ins_compl_upd_pum()
2447{
2448 int h;
2449
2450 if (compl_match_array != NULL)
2451 {
2452 h = curwin->w_cline_height;
2453 update_screen(0);
2454 if (h != curwin->w_cline_height)
2455 ins_compl_del_pum();
2456 }
2457}
2458
2459/*
2460 * Remove any popup menu.
2461 */
2462 static void
2463ins_compl_del_pum()
2464{
2465 if (compl_match_array != NULL)
2466 {
2467 pum_undisplay();
2468 vim_free(compl_match_array);
2469 compl_match_array = NULL;
2470 }
2471}
2472
2473/*
2474 * Return TRUE if the popup menu should be displayed.
2475 */
2476 static int
2477pum_wanted()
2478{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002479 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002480 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002481 return FALSE;
2482
2483 /* The display looks bad on a B&W display. */
2484 if (t_colors < 8
2485#ifdef FEAT_GUI
2486 && !gui.in_use
2487#endif
2488 )
2489 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002490 return TRUE;
2491}
2492
2493/*
2494 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002495 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002496 */
2497 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002498pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002499{
2500 compl_T *compl;
2501 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002502
2503 /* Don't display the popup menu if there are no matches or there is only
2504 * one (ignoring the original text). */
2505 compl = compl_first_match;
2506 i = 0;
2507 do
2508 {
2509 if (compl == NULL
2510 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2511 break;
2512 compl = compl->cp_next;
2513 } while (compl != compl_first_match);
2514
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002515 if (strstr((char *)p_cot, "menuone") != NULL)
2516 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002517 return (i >= 2);
2518}
2519
2520/*
2521 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002522 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002523 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002524 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002525ins_compl_show_pum()
2526{
2527 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002528 compl_T *shown_compl = NULL;
2529 int did_find_shown_match = FALSE;
2530 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002531 int i;
2532 int cur = -1;
2533 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002534 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002535
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002536 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002537 return;
2538
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002539#if defined(FEAT_EVAL)
2540 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2541 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2542#endif
2543
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002544 /* Update the screen before drawing the popup menu over it. */
2545 update_screen(0);
2546
2547 if (compl_match_array == NULL)
2548 {
2549 /* Need to build the popup menu list. */
2550 compl_match_arraysize = 0;
2551 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002552 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002553 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002554 do
2555 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002556 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2557 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002558 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002559 ++compl_match_arraysize;
2560 compl = compl->cp_next;
2561 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002562 if (compl_match_arraysize == 0)
2563 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002564 compl_match_array = (pumitem_T *)alloc_clear(
2565 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002566 * compl_match_arraysize));
2567 if (compl_match_array != NULL)
2568 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002569 /* If the current match is the original text don't find the first
2570 * match after it, don't highlight anything. */
2571 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2572 shown_match_ok = TRUE;
2573
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002574 i = 0;
2575 compl = compl_first_match;
2576 do
2577 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002578 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2579 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002580 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002581 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002582 if (!shown_match_ok)
2583 {
2584 if (compl == compl_shown_match || did_find_shown_match)
2585 {
2586 /* This item is the shown match or this is the
2587 * first displayed item after the shown match. */
2588 compl_shown_match = compl;
2589 did_find_shown_match = TRUE;
2590 shown_match_ok = TRUE;
2591 }
2592 else
2593 /* Remember this displayed match for when the
2594 * shown match is just below it. */
2595 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002596 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002597 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002598
2599 if (compl->cp_text[CPT_ABBR] != NULL)
2600 compl_match_array[i].pum_text =
2601 compl->cp_text[CPT_ABBR];
2602 else
2603 compl_match_array[i].pum_text = compl->cp_str;
2604 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2605 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2606 if (compl->cp_text[CPT_MENU] != NULL)
2607 compl_match_array[i++].pum_extra =
2608 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002609 else
2610 compl_match_array[i++].pum_extra = compl->cp_fname;
2611 }
2612
2613 if (compl == compl_shown_match)
2614 {
2615 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002616
2617 /* When the original text is the shown match don't set
2618 * compl_shown_match. */
2619 if (compl->cp_flags & ORIGINAL_TEXT)
2620 shown_match_ok = TRUE;
2621
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002622 if (!shown_match_ok && shown_compl != NULL)
2623 {
2624 /* The shown match isn't displayed, set it to the
2625 * previously displayed match. */
2626 compl_shown_match = shown_compl;
2627 shown_match_ok = TRUE;
2628 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002629 }
2630 compl = compl->cp_next;
2631 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002632
2633 if (!shown_match_ok) /* no displayed match at all */
2634 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002635 }
2636 }
2637 else
2638 {
2639 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002640 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00002641 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
2642 || compl_match_array[i].pum_text
2643 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002644 {
2645 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00002646 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002647 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002648 }
2649
2650 if (compl_match_array != NULL)
2651 {
2652 /* Compute the screen column of the start of the completed text.
2653 * Use the cursor to get all wrapping and other settings right. */
2654 col = curwin->w_cursor.col;
2655 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00002656 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002657 curwin->w_cursor.col = col;
2658 }
2659}
2660
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661#define DICT_FIRST (1) /* use just first element in "dict" */
2662#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002663
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00002665 * Add any identifiers that match the given pattern in the list of dictionary
2666 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 */
2668 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00002669ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
2670 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002672 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002673 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002675 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 char_u *ptr;
2677 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 char_u **files;
2680 int count;
2681 int i;
2682 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002683 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684
Bram Moolenaar0b238792006-03-02 22:49:12 +00002685 if (*dict == NUL)
2686 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002687#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002688 /* When 'dictionary' is empty and spell checking is enabled use
2689 * "spell". */
2690 if (!thesaurus && curwin->w_p_spell)
2691 dict = (char_u *)"spell";
2692 else
2693#endif
2694 return;
2695 }
2696
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002698 if (buf == NULL)
2699 return;
2700
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 /* If 'infercase' is set, don't use 'smartcase' here */
2702 save_p_scs = p_scs;
2703 if (curbuf->b_p_inf)
2704 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002705
2706 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
2707 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002708 * pattern. Also need to double backslashes. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002709 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2710 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002711 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
2712
2713 if (pat_esc == NULL)
2714 return ;
2715 i = (int)STRLEN(pat_esc) + 10;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002716 ptr = alloc(i);
2717 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002718 {
2719 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002720 return;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002721 }
2722 vim_snprintf((char *)ptr, i, "^\\s*\\zs\\V%s", pat_esc);
2723 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
2724 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002725 vim_free(ptr);
2726 }
2727 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00002728 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002729 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002730 if (regmatch.regprog == NULL)
2731 goto theend;
2732 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002733
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2735 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002736 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 {
2738 /* copy one dictionary file name into buf */
2739 if (flags == DICT_EXACT)
2740 {
2741 count = 1;
2742 files = &dict;
2743 }
2744 else
2745 {
2746 /* Expand wildcards in the dictionary name, but do not allow
2747 * backticks (for security, the 'dict' option may have been set in
2748 * a modeline). */
2749 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002750# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002751 if (!thesaurus && STRCMP(buf, "spell") == 0)
2752 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002753 else
2754# endif
2755 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 || expand_wildcards(1, &buf, &count, &files,
2757 EW_FILE|EW_SILENT) != OK)
2758 count = 0;
2759 }
2760
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002761# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002762 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00002764 /* Complete from active spelling. Skip "\<" in the pattern, we
2765 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002766 if (pat[0] == '\\' && pat[1] == '<')
2767 ptr = pat + 2;
2768 else
2769 ptr = pat;
2770 spell_dump_compl(curbuf, ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002772 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002773# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00002774 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002775 {
2776 ins_compl_files(count, files, thesaurus, flags,
2777 &regmatch, buf, &dir);
2778 if (flags != DICT_EXACT)
2779 FreeWild(count, files);
2780 }
2781 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 break;
2783 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002784
2785theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 p_scs = save_p_scs;
2787 vim_free(regmatch.regprog);
2788 vim_free(buf);
2789}
2790
Bram Moolenaar0b238792006-03-02 22:49:12 +00002791 static void
2792ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
2793 int count;
2794 char_u **files;
2795 int thesaurus;
2796 int flags;
2797 regmatch_T *regmatch;
2798 char_u *buf;
2799 int *dir;
2800{
2801 char_u *ptr;
2802 int i;
2803 FILE *fp;
2804 int add_r;
2805
2806 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
2807 {
2808 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2809 if (flags != DICT_EXACT)
2810 {
2811 vim_snprintf((char *)IObuff, IOSIZE,
2812 _("Scanning dictionary: %s"), (char *)files[i]);
2813 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2814 }
2815
2816 if (fp != NULL)
2817 {
2818 /*
2819 * Read dictionary file line by line.
2820 * Check each line for a match.
2821 */
2822 while (!got_int && !compl_interrupted
2823 && !vim_fgets(buf, LSIZE, fp))
2824 {
2825 ptr = buf;
2826 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
2827 {
2828 ptr = regmatch->startp[0];
2829 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2830 ptr = find_line_end(ptr);
2831 else
2832 ptr = find_word_end(ptr);
2833 add_r = ins_compl_add_infercase(regmatch->startp[0],
2834 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002835 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002836 if (thesaurus)
2837 {
2838 char_u *wstart;
2839
2840 /*
2841 * Add the other matches on the line
2842 */
2843 while (!got_int)
2844 {
2845 /* Find start of the next word. Skip white
2846 * space and punctuation. */
2847 ptr = find_word_start(ptr);
2848 if (*ptr == NUL || *ptr == NL)
2849 break;
2850 wstart = ptr;
2851
2852 /* Find end of the word and add it. */
2853#ifdef FEAT_MBYTE
2854 if (has_mbyte)
2855 /* Japanese words may have characters in
2856 * different classes, only separate words
2857 * with single-byte non-word characters. */
2858 while (*ptr != NUL)
2859 {
2860 int l = (*mb_ptr2len)(ptr);
2861
2862 if (l < 2 && !vim_iswordc(*ptr))
2863 break;
2864 ptr += l;
2865 }
2866 else
2867#endif
2868 ptr = find_word_end(ptr);
2869 add_r = ins_compl_add_infercase(wstart,
2870 (int)(ptr - wstart),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002871 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002872 }
2873 }
2874 if (add_r == OK)
2875 /* if dir was BACKWARD then honor it just once */
2876 *dir = FORWARD;
2877 else if (add_r == FAIL)
2878 break;
2879 /* avoid expensive call to vim_regexec() when at end
2880 * of line */
2881 if (*ptr == '\n' || got_int)
2882 break;
2883 }
2884 line_breakcheck();
2885 ins_compl_check_keys(50);
2886 }
2887 fclose(fp);
2888 }
2889 }
2890}
2891
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892/*
2893 * Find the start of the next word.
2894 * Returns a pointer to the first char of the word. Also stops at a NUL.
2895 */
2896 char_u *
2897find_word_start(ptr)
2898 char_u *ptr;
2899{
2900#ifdef FEAT_MBYTE
2901 if (has_mbyte)
2902 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002903 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 else
2905#endif
2906 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
2907 ++ptr;
2908 return ptr;
2909}
2910
2911/*
2912 * Find the end of the word. Assumes it starts inside a word.
2913 * Returns a pointer to just after the word.
2914 */
2915 char_u *
2916find_word_end(ptr)
2917 char_u *ptr;
2918{
2919#ifdef FEAT_MBYTE
2920 int start_class;
2921
2922 if (has_mbyte)
2923 {
2924 start_class = mb_get_class(ptr);
2925 if (start_class > 1)
2926 while (*ptr != NUL)
2927 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002928 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 if (mb_get_class(ptr) != start_class)
2930 break;
2931 }
2932 }
2933 else
2934#endif
2935 while (vim_iswordc(*ptr))
2936 ++ptr;
2937 return ptr;
2938}
2939
2940/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002941 * Find the end of the line, omitting CR and NL at the end.
2942 * Returns a pointer to just after the line.
2943 */
2944 static char_u *
2945find_line_end(ptr)
2946 char_u *ptr;
2947{
2948 char_u *s;
2949
2950 s = ptr + STRLEN(ptr);
2951 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
2952 --s;
2953 return s;
2954}
2955
2956/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 * Free the list of completions
2958 */
2959 static void
2960ins_compl_free()
2961{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002962 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002963 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002965 vim_free(compl_pattern);
2966 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002967 vim_free(compl_leader);
2968 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002970 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002972
2973 ins_compl_del_pum();
2974 pum_clear();
2975
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002976 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 do
2978 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002979 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002980 compl_curr_match = compl_curr_match->cp_next;
2981 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002983 if (match->cp_flags & FREE_FNAME)
2984 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00002985 for (i = 0; i < CPT_COUNT; ++i)
2986 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002988 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
2989 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990}
2991
2992 static void
2993ins_compl_clear()
2994{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002995 compl_cont_status = 0;
2996 compl_started = FALSE;
2997 compl_matches = 0;
2998 vim_free(compl_pattern);
2999 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003000 vim_free(compl_leader);
3001 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003003 vim_free(compl_orig_text);
3004 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003005 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006}
3007
3008/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003009 * Return TRUE when Insert completion is active.
3010 */
3011 int
3012ins_compl_active()
3013{
3014 return compl_started;
3015}
3016
3017/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003018 * Delete one character before the cursor and show the subset of the matches
3019 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003020 * Returns the character to be used, NUL if the work is done and another char
3021 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003022 */
3023 static int
3024ins_compl_bs()
3025{
3026 char_u *line;
3027 char_u *p;
3028
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003029 line = ml_get_curline();
3030 p = line + curwin->w_cursor.col;
3031 mb_ptr_back(line, p);
3032
3033 /* Stop completion when the whole word was deleted. */
3034 if ((int)(p - line) - (int)compl_col <= 0)
3035 return K_BS;
3036
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003037 /* Deleted more than what was used to find matches or didn't finish
3038 * finding all matches: need to look for matches all over again. */
3039 if (curwin->w_cursor.col <= compl_col + compl_length
3040 || compl_was_interrupted)
3041 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003042
Bram Moolenaara6557602006-02-04 22:43:20 +00003043 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003044 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003045 if (compl_leader != NULL)
3046 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003047 ins_compl_new_leader();
3048 return NUL;
3049 }
3050 return K_BS;
3051}
Bram Moolenaara6557602006-02-04 22:43:20 +00003052
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003053/*
3054 * Called after changing "compl_leader".
3055 * Show the popup menu with a different set of matches.
3056 * May also search for matches again if the previous search was interrupted.
3057 */
3058 static void
3059ins_compl_new_leader()
3060{
3061 ins_compl_del_pum();
3062 ins_compl_delete();
3063 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3064 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003065
3066 if (compl_started)
3067 ins_compl_set_original_text(compl_leader);
3068 else
3069 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003070#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003071 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003072#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003073 /*
3074 * Matches were cleared, need to search for them now. First display
3075 * the changed text before the cursor. Set "compl_restarting" to
3076 * avoid that the first match is inserted.
3077 */
3078 update_screen(0);
3079#ifdef FEAT_GUI
3080 if (gui.in_use)
3081 {
3082 /* Show the cursor after the match, not after the redrawn text. */
3083 setcursor();
3084 out_flush();
3085 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003086 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003087#endif
3088 compl_restarting = TRUE;
3089 if (ins_complete(Ctrl_N) == FAIL)
3090 compl_cont_status = 0;
3091 compl_restarting = FALSE;
3092 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003093
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003094#if 0 /* disabled, made CTRL-L, BS and typing char jump to original text. */
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003095 if (!compl_used_match)
3096 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003097 /* Go to the original text, since none of the matches is inserted. */
3098 if (compl_first_match->cp_prev != NULL
3099 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3100 compl_shown_match = compl_first_match->cp_prev;
3101 else
3102 compl_shown_match = compl_first_match;
3103 compl_curr_match = compl_shown_match;
3104 compl_shows_dir = compl_direction;
Bram Moolenaara6557602006-02-04 22:43:20 +00003105 }
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003106#endif
3107 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003108
3109 /* Show the popup menu with a different set of matches. */
3110 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003111
3112 /* Don't let Enter select the original text when there is no popup menu. */
3113 if (compl_match_array == NULL)
3114 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003115}
3116
3117/*
3118 * Append one character to the match leader. May reduce the number of
3119 * matches.
3120 */
3121 static void
3122ins_compl_addleader(c)
3123 int c;
3124{
3125#ifdef FEAT_MBYTE
3126 int cc;
3127
3128 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3129 {
3130 char_u buf[MB_MAXBYTES + 1];
3131
3132 (*mb_char2bytes)(c, buf);
3133 buf[cc] = NUL;
3134 ins_char_bytes(buf, cc);
3135 }
3136 else
3137#endif
3138 ins_char(c);
3139
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003140 /* If we didn't complete finding matches we must search again. */
3141 if (compl_was_interrupted)
3142 ins_compl_restart();
3143
Bram Moolenaara6557602006-02-04 22:43:20 +00003144 vim_free(compl_leader);
3145 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
3146 curwin->w_cursor.col - compl_col);
3147 if (compl_leader != NULL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003148 ins_compl_new_leader();
3149}
3150
3151/*
3152 * Setup for finding completions again without leaving CTRL-X mode. Used when
3153 * BS or a key was typed while still searching for matches.
3154 */
3155 static void
3156ins_compl_restart()
3157{
3158 ins_compl_free();
3159 compl_started = FALSE;
3160 compl_matches = 0;
3161 compl_cont_status = 0;
3162 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003163}
3164
3165/*
3166 * Set the first match, the original text.
3167 */
3168 static void
3169ins_compl_set_original_text(str)
3170 char_u *str;
3171{
3172 char_u *p;
3173
3174 /* Replace the original text entry. */
3175 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3176 {
3177 p = vim_strsave(str);
3178 if (p != NULL)
3179 {
3180 vim_free(compl_first_match->cp_str);
3181 compl_first_match->cp_str = p;
3182 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003183 }
3184}
3185
3186/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003187 * Append one character to the match leader. May reduce the number of
3188 * matches.
3189 */
3190 static void
3191ins_compl_addfrommatch()
3192{
3193 char_u *p;
3194 int len = curwin->w_cursor.col - compl_col;
3195 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003196 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003197
3198 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003199 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003200 {
3201 /* When still at the original match use the first entry that matches
3202 * the leader. */
3203 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3204 {
3205 p = NULL;
3206 for (cp = compl_shown_match->cp_next; cp != NULL
3207 && cp != compl_first_match; cp = cp->cp_next)
3208 {
3209 if (ins_compl_equal(cp, compl_leader,
3210 (int)STRLEN(compl_leader)))
3211 {
3212 p = cp->cp_str;
3213 break;
3214 }
3215 }
3216 if (p == NULL || (int)STRLEN(p) <= len)
3217 return;
3218 }
3219 else
3220 return;
3221 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003222 p += len;
3223#ifdef FEAT_MBYTE
3224 c = mb_ptr2char(p);
3225#else
3226 c = *p;
3227#endif
3228 ins_compl_addleader(c);
3229}
3230
3231/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003233 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003234 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003236 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237ins_compl_prep(c)
3238 int c;
3239{
3240 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003242 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243
3244 /* Forget any previous 'special' messages if this is actually
3245 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3246 */
3247 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3248 edit_submode_extra = NULL;
3249
3250 /* Ignore end of Select mode mapping */
3251 if (c == K_SELECT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003252 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003254 /* Set "compl_get_longest" when finding the first matches. */
3255 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3256 || (ctrl_x_mode == 0 && !compl_started))
3257 {
3258 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3259 compl_used_match = TRUE;
3260 }
3261
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3263 {
3264 /*
3265 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3266 * it will be yet. Now we decide.
3267 */
3268 switch (c)
3269 {
3270 case Ctrl_E:
3271 case Ctrl_Y:
3272 ctrl_x_mode = CTRL_X_SCROLL;
3273 if (!(State & REPLACE_FLAG))
3274 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3275 else
3276 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3277 edit_submode_pre = NULL;
3278 showmode();
3279 break;
3280 case Ctrl_L:
3281 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3282 break;
3283 case Ctrl_F:
3284 ctrl_x_mode = CTRL_X_FILES;
3285 break;
3286 case Ctrl_K:
3287 ctrl_x_mode = CTRL_X_DICTIONARY;
3288 break;
3289 case Ctrl_R:
3290 /* Simply allow ^R to happen without affecting ^X mode */
3291 break;
3292 case Ctrl_T:
3293 ctrl_x_mode = CTRL_X_THESAURUS;
3294 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003295#ifdef FEAT_COMPL_FUNC
3296 case Ctrl_U:
3297 ctrl_x_mode = CTRL_X_FUNCTION;
3298 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003299 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003300 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003301 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003302#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003303 case 's':
3304 case Ctrl_S:
3305 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003306#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003307 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003308 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003309 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003310#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003311 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 case Ctrl_RSB:
3313 ctrl_x_mode = CTRL_X_TAGS;
3314 break;
3315#ifdef FEAT_FIND_ID
3316 case Ctrl_I:
3317 case K_S_TAB:
3318 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3319 break;
3320 case Ctrl_D:
3321 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3322 break;
3323#endif
3324 case Ctrl_V:
3325 case Ctrl_Q:
3326 ctrl_x_mode = CTRL_X_CMDLINE;
3327 break;
3328 case Ctrl_P:
3329 case Ctrl_N:
3330 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3331 * just started ^X mode, or there were enough ^X's to cancel
3332 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3333 * do normal expansion when interrupting a different mode (say
3334 * ^X^F^X^P or ^P^X^X^P, see below)
3335 * nothing changes if interrupting mode 0, (eg, the flag
3336 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003337 if (!(compl_cont_status & CONT_INTRPT))
3338 compl_cont_status |= CONT_LOCAL;
3339 else if (compl_cont_mode != 0)
3340 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 /* FALLTHROUGH */
3342 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003343 /* If we have typed at least 2 ^X's... for modes != 0, we set
3344 * compl_cont_status = 0 (eg, as if we had just started ^X
3345 * mode).
3346 * For mode 0, we set "compl_cont_mode" to an impossible
3347 * value, in both cases ^X^X can be used to restart the same
3348 * mode (avoiding ADDING mode).
3349 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3350 * 'complete' and local ^P expansions respectively.
3351 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3352 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 if (c == Ctrl_X)
3354 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003355 if (compl_cont_mode != 0)
3356 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003358 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 }
3360 ctrl_x_mode = 0;
3361 edit_submode = NULL;
3362 showmode();
3363 break;
3364 }
3365 }
3366 else if (ctrl_x_mode != 0)
3367 {
3368 /* We're already in CTRL-X mode, do we stay in it? */
3369 if (!vim_is_ctrl_x_key(c))
3370 {
3371 if (ctrl_x_mode == CTRL_X_SCROLL)
3372 ctrl_x_mode = 0;
3373 else
3374 ctrl_x_mode = CTRL_X_FINISHED;
3375 edit_submode = NULL;
3376 }
3377 showmode();
3378 }
3379
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003380 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 {
3382 /* Show error message from attempted keyword completion (probably
3383 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003384 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003386 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3387 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 || ctrl_x_mode == CTRL_X_FINISHED)
3389 {
3390 /* Get here when we have finished typing a sequence of ^N and
3391 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003392 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003393 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003395 char_u *p;
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003396 int temp = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003397
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003399 * If any of the original typed text has been changed, eg when
3400 * ignorecase is set, we must add back-spaces to the redo
3401 * buffer. We add as few as necessary to delete just the part
3402 * of the original text that has changed.
3403 * When using the longest match, edited the match or used
3404 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003406 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3407 ptr = compl_curr_match->cp_str;
3408 else if (compl_leader != NULL)
3409 ptr = compl_leader;
3410 else
3411 ptr = compl_orig_text;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003412 if (compl_orig_text != NULL)
3413 {
3414 p = compl_orig_text;
3415 for (temp = 0; p[temp] != NUL && p[temp] == ptr[temp];
3416 ++temp)
3417 ;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003418#ifdef FEAT_MBYTE
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003419 if (temp > 0)
3420 temp -= (*mb_head_off)(compl_orig_text, p + temp);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003421#endif
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003422 for (p += temp; *p != NUL; mb_ptr_adv(p))
3423 AppendCharToRedobuff(K_BS);
3424 }
3425 if (ptr != NULL)
3426 AppendToRedobuffLit(ptr + temp, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 }
3428
3429#ifdef FEAT_CINDENT
3430 want_cindent = (can_cindent && cindent_on());
3431#endif
3432 /*
3433 * When completing whole lines: fix indent for 'cindent'.
3434 * Otherwise, break line if it's too long.
3435 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003436 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 {
3438#ifdef FEAT_CINDENT
3439 /* re-indent the current line */
3440 if (want_cindent)
3441 {
3442 do_c_expr_indent();
3443 want_cindent = FALSE; /* don't do it again */
3444 }
3445#endif
3446 }
3447 else
3448 {
3449 /* put the cursor on the last char, for 'tw' formatting */
3450 curwin->w_cursor.col--;
3451 if (stop_arrow() == OK)
3452 insertchar(NUL, 0, -1);
3453 curwin->w_cursor.col++;
3454 }
3455
3456 auto_format(FALSE, TRUE);
3457
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003458 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003459 * the selection without inserting anything. When
3460 * compl_enter_selects is set the Enter key does the same. */
3461 if ((c == Ctrl_Y || (compl_enter_selects
3462 && (c == CAR || c == K_KENTER || c == NL)))
3463 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003464 retval = TRUE;
3465
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003466 /* CTRL-E means completion is Ended, go back to the typed text. */
3467 if (c == Ctrl_E)
3468 {
3469 ins_compl_delete();
3470 if (compl_leader != NULL)
3471 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3472 else if (compl_first_match != NULL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003473 ins_bytes(compl_orig_text
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003474 + curwin->w_cursor.col - compl_col);
3475 retval = TRUE;
3476 }
3477
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003479 compl_started = FALSE;
3480 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 msg_clr_cmdline(); /* necessary for "noshowmode" */
3482 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003483 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 if (edit_submode != NULL)
3485 {
3486 edit_submode = NULL;
3487 showmode();
3488 }
3489
3490#ifdef FEAT_CINDENT
3491 /*
3492 * Indent now if a key was typed that is in 'cinkeys'.
3493 */
3494 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3495 do_c_expr_indent();
3496#endif
3497 }
3498 }
3499
3500 /* reset continue_* if we left expansion-mode, if we stay they'll be
3501 * (re)set properly in ins_complete() */
3502 if (!vim_is_ctrl_x_key(c))
3503 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003504 compl_cont_status = 0;
3505 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003507
3508 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509}
3510
3511/*
3512 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3513 * (depending on flag) starting from buf and looking for a non-scanned
3514 * buffer (other than curbuf). curbuf is special, if it is called with
3515 * buf=curbuf then it has to be the first call for a given flag/expansion.
3516 *
3517 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3518 */
3519 static buf_T *
3520ins_compl_next_buf(buf, flag)
3521 buf_T *buf;
3522 int flag;
3523{
3524#ifdef FEAT_WINDOWS
3525 static win_T *wp;
3526#endif
3527
3528 if (flag == 'w') /* just windows */
3529 {
3530#ifdef FEAT_WINDOWS
3531 if (buf == curbuf) /* first call for this flag/expansion */
3532 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003533 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 && wp->w_buffer->b_scanned)
3535 ;
3536 buf = wp->w_buffer;
3537#else
3538 buf = curbuf;
3539#endif
3540 }
3541 else
3542 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3543 * (unlisted buffers)
3544 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003545 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 && ((flag == 'U'
3547 ? buf->b_p_bl
3548 : (!buf->b_p_bl
3549 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003550 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 ;
3552 return buf;
3553}
3554
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003555#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003556static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003557
3558/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003559 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003560 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003561 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003562 static void
3563expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003564 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003565 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003566{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003567 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003568 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003569 char_u *funcname;
3570 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003571
Bram Moolenaare344bea2005-09-01 20:46:49 +00003572 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3573 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003574 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003575
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003576 /* Call 'completefunc' to obtain the list of matches. */
3577 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003578 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003579
Bram Moolenaare344bea2005-09-01 20:46:49 +00003580 pos = curwin->w_cursor;
3581 matchlist = call_func_retlist(funcname, 2, args, FALSE);
3582 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003583 if (matchlist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003584 return;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003585
Bram Moolenaara94bc432006-03-10 21:42:59 +00003586 ins_compl_add_list(matchlist);
3587 list_unref(matchlist);
3588}
3589#endif /* FEAT_COMPL_FUNC */
3590
Bram Moolenaar39f05632006-03-19 22:15:26 +00003591#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00003592/*
3593 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00003594 */
3595 static void
3596ins_compl_add_list(list)
3597 list_T *list;
3598{
3599 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003600 int dir = compl_direction;
3601
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003602 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003603 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003604 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00003605 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
3606 /* if dir was BACKWARD then honor it just once */
3607 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00003608 else if (did_emsg)
3609 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003610 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003611}
Bram Moolenaar39f05632006-03-19 22:15:26 +00003612
3613/*
3614 * Add a match to the list of matches from a typeval_T.
3615 * If the given string is already in the list of completions, then return
3616 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
3617 * maybe because alloc() returns NULL, then FAIL is returned.
3618 */
3619 int
3620ins_compl_add_tv(tv, dir)
3621 typval_T *tv;
3622 int dir;
3623{
3624 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00003625 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00003626 int adup = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003627 char_u *(cptext[CPT_COUNT]);
3628
3629 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3630 {
3631 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
3632 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
3633 (char_u *)"abbr", FALSE);
3634 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
3635 (char_u *)"menu", FALSE);
3636 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
3637 (char_u *)"kind", FALSE);
3638 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
3639 (char_u *)"info", FALSE);
3640 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
3641 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003642 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00003643 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar39f05632006-03-19 22:15:26 +00003644 }
3645 else
3646 {
3647 word = get_tv_string_chk(tv);
3648 vim_memset(cptext, 0, sizeof(cptext));
3649 }
3650 if (word == NULL || *word == NUL)
3651 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00003652 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003653}
Bram Moolenaara94bc432006-03-10 21:42:59 +00003654#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003655
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003656/*
3657 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003658 * The search starts at position "ini" in curbuf and in the direction
3659 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003660 * When "compl_started" is FALSE start at that position, otherwise continue
3661 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003662 * This may return before finding all the matches.
3663 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 */
3665 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003666ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668{
3669 static pos_T first_match_pos;
3670 static pos_T last_match_pos;
3671 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003672 static int found_all = FALSE; /* Found all matches of a
3673 certain type. */
3674 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675
Bram Moolenaar572cb562005-08-05 21:35:02 +00003676 pos_T *pos;
3677 char_u **matches;
3678 int save_p_scs;
3679 int save_p_ws;
3680 int save_p_ic;
3681 int i;
3682 int num_matches;
3683 int len;
3684 int found_new_match;
3685 int type = ctrl_x_mode;
3686 char_u *ptr;
3687 char_u *dict = NULL;
3688 int dict_f = 0;
3689 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003691 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 {
3693 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3694 ins_buf->b_scanned = 0;
3695 found_all = FALSE;
3696 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003697 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003698 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 last_match_pos = first_match_pos = *ini;
3700 }
3701
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003702 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003703 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3705 for (;;)
3706 {
3707 found_new_match = FAIL;
3708
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003709 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 * or if found_all says this entry is done. For ^X^L only use the
3711 * entries from 'complete' that look in loaded buffers. */
3712 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003713 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 {
3715 found_all = FALSE;
3716 while (*e_cpt == ',' || *e_cpt == ' ')
3717 e_cpt++;
3718 if (*e_cpt == '.' && !curbuf->b_scanned)
3719 {
3720 ins_buf = curbuf;
3721 first_match_pos = *ini;
3722 /* So that ^N can match word immediately after cursor */
3723 if (ctrl_x_mode == 0)
3724 dec(&first_match_pos);
3725 last_match_pos = first_match_pos;
3726 type = 0;
3727 }
3728 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
3729 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
3730 {
3731 /* Scan a buffer, but not the current one. */
3732 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
3733 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003734 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 first_match_pos.col = last_match_pos.col = 0;
3736 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
3737 last_match_pos.lnum = 0;
3738 type = 0;
3739 }
3740 else /* unloaded buffer, scan like dictionary */
3741 {
3742 found_all = TRUE;
3743 if (ins_buf->b_fname == NULL)
3744 continue;
3745 type = CTRL_X_DICTIONARY;
3746 dict = ins_buf->b_fname;
3747 dict_f = DICT_EXACT;
3748 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00003749 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 ins_buf->b_fname == NULL
3751 ? buf_spname(ins_buf)
3752 : ins_buf->b_sfname == NULL
3753 ? (char *)ins_buf->b_fname
3754 : (char *)ins_buf->b_sfname);
3755 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3756 }
3757 else if (*e_cpt == NUL)
3758 break;
3759 else
3760 {
3761 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3762 type = -1;
3763 else if (*e_cpt == 'k' || *e_cpt == 's')
3764 {
3765 if (*e_cpt == 'k')
3766 type = CTRL_X_DICTIONARY;
3767 else
3768 type = CTRL_X_THESAURUS;
3769 if (*++e_cpt != ',' && *e_cpt != NUL)
3770 {
3771 dict = e_cpt;
3772 dict_f = DICT_FIRST;
3773 }
3774 }
3775#ifdef FEAT_FIND_ID
3776 else if (*e_cpt == 'i')
3777 type = CTRL_X_PATH_PATTERNS;
3778 else if (*e_cpt == 'd')
3779 type = CTRL_X_PATH_DEFINES;
3780#endif
3781 else if (*e_cpt == ']' || *e_cpt == 't')
3782 {
3783 type = CTRL_X_TAGS;
3784 sprintf((char*)IObuff, _("Scanning tags."));
3785 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3786 }
3787 else
3788 type = -1;
3789
3790 /* in any case e_cpt is advanced to the next entry */
3791 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
3792
3793 found_all = TRUE;
3794 if (type == -1)
3795 continue;
3796 }
3797 }
3798
3799 switch (type)
3800 {
3801 case -1:
3802 break;
3803#ifdef FEAT_FIND_ID
3804 case CTRL_X_PATH_PATTERNS:
3805 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003806 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003807 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003809 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3811 (linenr_T)1, (linenr_T)MAXLNUM);
3812 break;
3813#endif
3814
3815 case CTRL_X_DICTIONARY:
3816 case CTRL_X_THESAURUS:
3817 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00003818 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 : (type == CTRL_X_THESAURUS
3820 ? (*curbuf->b_p_tsr == NUL
3821 ? p_tsr
3822 : curbuf->b_p_tsr)
3823 : (*curbuf->b_p_dict == NUL
3824 ? p_dict
3825 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003826 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00003827 dict != NULL ? dict_f
3828 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 dict = NULL;
3830 break;
3831
3832 case CTRL_X_TAGS:
3833 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
3834 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003835 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836
3837 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003838 * of matches is found when compl_pattern is empty */
3839 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
3841 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
3842 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3843 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003844 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 }
3846 p_ic = save_p_ic;
3847 break;
3848
3849 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003850 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
3852 {
3853
3854 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003855 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003856 ins_compl_add_matches(num_matches, matches,
3857#ifdef CASE_INSENSITIVE_FILENAME
3858 TRUE
3859#else
3860 FALSE
3861#endif
3862 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 }
3864 break;
3865
3866 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003867 if (expand_cmdline(&compl_xp, compl_pattern,
3868 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003870 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 break;
3872
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003873#ifdef FEAT_COMPL_FUNC
3874 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003875 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003876 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003877 break;
3878#endif
3879
Bram Moolenaar488c6512005-08-11 20:09:58 +00003880 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003881#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00003882 num_matches = expand_spelling(first_match_pos.lnum,
3883 first_match_pos.col, compl_pattern, &matches);
3884 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003885 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00003886#endif
3887 break;
3888
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 default: /* normal ^P/^N and ^X^L */
3890 /*
3891 * If 'infercase' is set, don't use 'smartcase' here
3892 */
3893 save_p_scs = p_scs;
3894 if (ins_buf->b_p_inf)
3895 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003896
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 /* buffers other than curbuf are scanned from the beginning or the
3898 * end but never from the middle, thus setting nowrapscan in this
3899 * buffers is a good idea, on the other hand, we always set
3900 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
3901 save_p_ws = p_ws;
3902 if (ins_buf != curbuf)
3903 p_ws = FALSE;
3904 else if (*e_cpt == '.')
3905 p_ws = TRUE;
3906 for (;;)
3907 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003908 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003910 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
3911 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003913 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003915 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003917 found_new_match = searchit(NULL, ins_buf, pos,
3918 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003919 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003920 RE_LAST, (linenr_T)0);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003921 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003923 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003924 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 first_match_pos = *pos;
3926 last_match_pos = *pos;
3927 }
3928 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003929 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 found_new_match = FAIL;
3931 if (found_new_match == FAIL)
3932 {
3933 if (ins_buf == curbuf)
3934 found_all = TRUE;
3935 break;
3936 }
3937
3938 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003939 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 && ini->lnum == pos->lnum
3941 && ini->col == pos->col)
3942 continue;
3943 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
3944 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3945 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003946 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 {
3948 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3949 continue;
3950 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3951 if (!p_paste)
3952 ptr = skipwhite(ptr);
3953 }
3954 len = (int)STRLEN(ptr);
3955 }
3956 else
3957 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003958 char_u *tmp_ptr = ptr;
3959
3960 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003962 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 /* Skip if already inside a word. */
3964 if (vim_iswordp(tmp_ptr))
3965 continue;
3966 /* Find start of next word. */
3967 tmp_ptr = find_word_start(tmp_ptr);
3968 }
3969 /* Find end of this word. */
3970 tmp_ptr = find_word_end(tmp_ptr);
3971 len = (int)(tmp_ptr - ptr);
3972
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003973 if ((compl_cont_status & CONT_ADDING)
3974 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 {
3976 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3977 {
3978 /* Try next line, if any. the new word will be
3979 * "join" as if the normal command "J" was used.
3980 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003981 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 * works -- Acevedo */
3983 STRNCPY(IObuff, ptr, len);
3984 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3985 tmp_ptr = ptr = skipwhite(ptr);
3986 /* Find start of next word. */
3987 tmp_ptr = find_word_start(tmp_ptr);
3988 /* Find end of next word. */
3989 tmp_ptr = find_word_end(tmp_ptr);
3990 if (tmp_ptr > ptr)
3991 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003992 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003994 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 IObuff[len++] = ' ';
3996 /* IObuf =~ "\k.* ", thus len >= 2 */
3997 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003998 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 || (vim_strchr(p_cpo, CPO_JOINSP)
4000 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004001 && (IObuff[len - 2] == '?'
4002 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 IObuff[len++] = ' ';
4004 }
4005 /* copy as much as posible of the new word */
4006 if (tmp_ptr - ptr >= IOSIZE - len)
4007 tmp_ptr = ptr + IOSIZE - len - 1;
4008 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4009 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004010 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 }
4012 IObuff[len] = NUL;
4013 ptr = IObuff;
4014 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004015 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 continue;
4017 }
4018 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004019 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004020 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004021 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 {
4023 found_new_match = OK;
4024 break;
4025 }
4026 }
4027 p_scs = save_p_scs;
4028 p_ws = save_p_ws;
4029 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004030
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004031 /* check if compl_curr_match has changed, (e.g. other type of
4032 * expansion added somenthing) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004033 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 found_new_match = OK;
4035
4036 /* break the loop for specialized modes (use 'complete' just for the
4037 * generic ctrl_x_mode == 0) or when we've found a new match */
4038 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004039 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004040 {
4041 if (got_int)
4042 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004043 /* Fill the popup menu as soon as possible. */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004044 if (pum_wanted() && type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004045 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004046
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004047 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
4048 || compl_interrupted)
4049 break;
4050 compl_started = TRUE;
4051 }
4052 else
4053 {
4054 /* Mark a buffer scanned when it has been scanned completely */
4055 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4056 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004058 compl_started = FALSE;
4059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004061 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062
4063 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
4064 && *e_cpt == NUL) /* Got to end of 'complete' */
4065 found_new_match = FAIL;
4066
4067 i = -1; /* total of matches, unknown */
4068 if (found_new_match == FAIL
4069 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
4070 i = ins_compl_make_cyclic();
4071
4072 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004073 * just been made cyclic then we have to move compl_curr_match to the next
4074 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004075 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4076 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004077 if (compl_curr_match == NULL)
4078 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 return i;
4080}
4081
4082/* Delete the old text being completed. */
4083 static void
4084ins_compl_delete()
4085{
4086 int i;
4087
4088 /*
4089 * In insert mode: Delete the typed part.
4090 * In replace mode: Put the old characters back, if any.
4091 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004092 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 backspace_until_column(i);
4094 changed_cline_bef_curs();
4095}
4096
4097/* Insert the new text being completed. */
4098 static void
4099ins_compl_insert()
4100{
Bram Moolenaar572cb562005-08-05 21:35:02 +00004101 ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004102 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4103 compl_used_match = FALSE;
4104 else
4105 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106}
4107
4108/*
4109 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004110 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4111 * get more completions. If it is FALSE, then we just do nothing when there
4112 * are no more completions in a given direction. The latter case is used when
4113 * we are still in the middle of finding completions, to allow browsing
4114 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 * Return the total number of matches, or -1 if still unknown -- webb.
4116 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004117 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4118 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 *
4120 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004121 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4122 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 */
4124 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004125ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004127 int count; /* repeat completion this many times; should
4128 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004129 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130{
4131 int num_matches = -1;
4132 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004133 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004134 compl_T *found_compl = NULL;
4135 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004136 int advance;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004138 if (compl_leader != NULL
4139 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004141 /* Set "compl_shown_match" to the actually shown match, it may differ
4142 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004143 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004144 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004145 && compl_shown_match->cp_next != NULL
4146 && compl_shown_match->cp_next != compl_first_match)
4147 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004148
4149 /* If we didn't find it searching forward, and compl_shows_dir is
4150 * backward, find the last match. */
4151 if (compl_shows_dir == BACKWARD
4152 && !ins_compl_equal(compl_shown_match,
4153 compl_leader, (int)STRLEN(compl_leader))
4154 && (compl_shown_match->cp_next == NULL
4155 || compl_shown_match->cp_next == compl_first_match))
4156 {
4157 while (!ins_compl_equal(compl_shown_match,
4158 compl_leader, (int)STRLEN(compl_leader))
4159 && compl_shown_match->cp_prev != NULL
4160 && compl_shown_match->cp_prev != compl_first_match)
4161 compl_shown_match = compl_shown_match->cp_prev;
4162 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004163 }
4164
4165 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004166 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 /* Delete old text to be replaced */
4168 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004169
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004170 /* When finding the longest common text we stick at the original text,
4171 * don't let CTRL-N or CTRL-P move to the first match. */
4172 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4173
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004174 /* When restarting the search don't insert the first match either. */
4175 if (compl_restarting)
4176 {
4177 advance = FALSE;
4178 compl_restarting = FALSE;
4179 }
4180
Bram Moolenaare3226be2005-12-18 22:10:00 +00004181 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4182 * around. */
4183 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004185 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004187 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004188 found_end = (compl_first_match != NULL
4189 && (compl_shown_match->cp_next == compl_first_match
4190 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004191 }
4192 else if (compl_shows_dir == BACKWARD
4193 && compl_shown_match->cp_prev != NULL)
4194 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004195 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004196 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004197 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 }
4199 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004200 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004201 if (!allow_get_expansion)
4202 {
4203 if (advance)
4204 {
4205 if (compl_shows_dir == BACKWARD)
4206 compl_pending -= todo + 1;
4207 else
4208 compl_pending += todo + 1;
4209 }
4210 return -1;
4211 }
4212
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004213 if (advance)
4214 {
4215 if (compl_shows_dir == BACKWARD)
4216 --compl_pending;
4217 else
4218 ++compl_pending;
4219 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004220
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004221 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004222 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004223
4224 /* handle any pending completions */
4225 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004226 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004227 {
4228 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4229 {
4230 compl_shown_match = compl_shown_match->cp_next;
4231 --compl_pending;
4232 }
4233 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4234 {
4235 compl_shown_match = compl_shown_match->cp_prev;
4236 ++compl_pending;
4237 }
4238 else
4239 break;
4240 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004241 found_end = FALSE;
4242 }
4243 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4244 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004245 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004246 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004247 ++todo;
4248 else
4249 /* Remember a matching item. */
4250 found_compl = compl_shown_match;
4251
4252 /* Stop at the end of the list when we found a usable match. */
4253 if (found_end)
4254 {
4255 if (found_compl != NULL)
4256 {
4257 compl_shown_match = found_compl;
4258 break;
4259 }
4260 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 }
4263
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004264 /* Insert the text of the new completion, or the compl_leader. */
4265 if (insert_match)
4266 {
4267 if (!compl_get_longest || compl_used_match)
4268 ins_compl_insert();
4269 else
4270 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
4271 }
4272 else
4273 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274
4275 if (!allow_get_expansion)
4276 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004277 /* may undisplay the popup menu first */
4278 ins_compl_upd_pum();
4279
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004280 /* redraw to show the user what was inserted */
4281 update_screen(0);
4282
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004283 /* display the updated popup menu */
4284 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004285#ifdef FEAT_GUI
4286 if (gui.in_use)
4287 {
4288 /* Show the cursor after the match, not after the redrawn text. */
4289 setcursor();
4290 out_flush();
4291 gui_update_cursor(FALSE, FALSE);
4292 }
4293#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004294
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 /* Delete old text to be replaced, since we're still searching and
4296 * don't want to match ourselves! */
4297 ins_compl_delete();
4298 }
4299
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004300 /* Enter will select a match when the match wasn't inserted and the popup
4301 * menu is visislbe. */
4302 compl_enter_selects = !insert_match && compl_match_array != NULL;
4303
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 /*
4305 * Show the file name for the match (if any)
4306 * Truncate the file name to avoid a wait for return.
4307 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004308 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 {
4310 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004311 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 if (i <= 0)
4313 i = 0;
4314 else
4315 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004316 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 msg(IObuff);
4318 redraw_cmdline = FALSE; /* don't overwrite! */
4319 }
4320
4321 return num_matches;
4322}
4323
4324/*
4325 * Call this while finding completions, to check whether the user has hit a key
4326 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004327 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004329 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 */
4331 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004332ins_compl_check_keys(frequency)
4333 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334{
4335 static int count = 0;
4336
4337 int c;
4338
4339 /* Don't check when reading keys from a script. That would break the test
4340 * scripts */
4341 if (using_script())
4342 return;
4343
4344 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004345 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 return;
4347 count = 0;
4348
Bram Moolenaara260a972006-06-23 19:36:29 +00004349 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4350 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 if (c != NUL)
4353 {
4354 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4355 {
4356 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004357 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004358 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4359 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004361 else
4362 {
4363 /* Need to get the character to have KeyTyped set. We'll put it
4364 * back with vungetc() below. */
4365 c = safe_vgetc();
4366
4367 /* Don't interrupt completion when the character wasn't typed,
4368 * e.g., when doing @q to replay keys. */
4369 if (c != Ctrl_R && KeyTyped)
4370 compl_interrupted = TRUE;
4371
4372 vungetc(c);
4373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004375 if (compl_pending != 0 && !got_int)
Bram Moolenaara260a972006-06-23 19:36:29 +00004376 {
4377 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4378
4379 compl_pending = 0;
4380 (void)ins_compl_next(FALSE, todo, TRUE);
4381 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004382}
4383
4384/*
4385 * Decide the direction of Insert mode complete from the key typed.
4386 * Returns BACKWARD or FORWARD.
4387 */
4388 static int
4389ins_compl_key2dir(c)
4390 int c;
4391{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004392 if (c == Ctrl_P || c == Ctrl_L
4393 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4394 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004395 return BACKWARD;
4396 return FORWARD;
4397}
4398
4399/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004400 * Return TRUE for keys that are used for completion only when the popup menu
4401 * is visible.
4402 */
4403 static int
4404ins_compl_pum_key(c)
4405 int c;
4406{
4407 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004408 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4409 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004410}
4411
4412/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004413 * Decide the number of completions to move forward.
4414 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4415 */
4416 static int
4417ins_compl_key2count(c)
4418 int c;
4419{
4420 int h;
4421
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004422 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004423 {
4424 h = pum_get_height();
4425 if (h > 3)
4426 h -= 2; /* keep some context */
4427 return h;
4428 }
4429 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430}
4431
4432/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004433 * Return TRUE if completion with "c" should insert the match, FALSE if only
4434 * to change the currently selected completion.
4435 */
4436 static int
4437ins_compl_use_match(c)
4438 int c;
4439{
4440 switch (c)
4441 {
4442 case K_UP:
4443 case K_DOWN:
4444 case K_PAGEDOWN:
4445 case K_KPAGEDOWN:
4446 case K_S_DOWN:
4447 case K_PAGEUP:
4448 case K_KPAGEUP:
4449 case K_S_UP:
4450 return FALSE;
4451 }
4452 return TRUE;
4453}
4454
4455/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 * Do Insert mode completion.
4457 * Called when character "c" was typed, which has a meaning for completion.
4458 * Returns OK if completion was done, FAIL if something failed (out of mem).
4459 */
4460 static int
4461ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004462 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004464 char_u *line;
4465 int startcol = 0; /* column where searched text starts */
4466 colnr_T curs_col; /* cursor column */
4467 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468
Bram Moolenaare3226be2005-12-18 22:10:00 +00004469 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004470 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 {
4472 /* First time we hit ^N or ^P (in a row, I mean) */
4473
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 did_ai = FALSE;
4475#ifdef FEAT_SMARTINDENT
4476 did_si = FALSE;
4477 can_si = FALSE;
4478 can_si_back = FALSE;
4479#endif
4480 if (stop_arrow() == FAIL)
4481 return FAIL;
4482
4483 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004484 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004485 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486
4487 /* if this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004488 * "compl_startpos" to the cursor as a pattern to add a new word
4489 * instead of expand the one before the cursor, in word-wise if
4490 * "compl_startpos"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 * is not in the same line as the cursor then fix it (the line has
4492 * been split because it was longer than 'tw'). if SOL is set then
4493 * skip the previous pattern, a word at the beginning of the line has
4494 * been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004495 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4496 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 {
4498 /*
4499 * it is a continued search
4500 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004501 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4503 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4504 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004505 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004507 /* line (probably) wrapped, set compl_startpos to the
4508 * first non_blank in the line, if it is not a wordchar
4509 * include it to get a better pattern, but then we don't
4510 * want the "\\<" prefix, check it bellow */
4511 compl_col = (colnr_T)(skipwhite(line) - line);
4512 compl_startpos.col = compl_col;
4513 compl_startpos.lnum = curwin->w_cursor.lnum;
4514 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 }
4516 else
4517 {
4518 /* S_IPOS was set when we inserted a word that was at the
4519 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004520 * mode but first we need to redefine compl_startpos */
4521 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004523 compl_cont_status |= CONT_SOL;
4524 compl_startpos.col = (colnr_T)(skipwhite(
4525 line + compl_length
4526 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004528 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004530 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004531 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 * have enough space? just being paranoic */
4533#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004534 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004536 compl_cont_status &= ~CONT_SOL;
4537 compl_length = (IOSIZE - MIN_SPACE);
4538 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004540 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4541 if (compl_length < 1)
4542 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 }
4544 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004545 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004547 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 }
4549 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004550 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004552 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004554 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004556 compl_cont_status = 0;
4557 compl_cont_status |= CONT_N_ADDS;
4558 compl_startpos = curwin->w_cursor;
4559 startcol = (int)curs_col;
4560 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 }
4562
4563 /* Work out completion pattern and original text -- webb */
4564 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4565 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004566 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4568 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004569 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004571 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004573 compl_col += ++startcol;
4574 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 }
4576 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004577 compl_pattern = str_foldcase(line + compl_col,
4578 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004580 compl_pattern = vim_strnsave(line + compl_col,
4581 compl_length);
4582 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 return FAIL;
4584 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004585 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 {
4587 char_u *prefix = (char_u *)"\\<";
4588
4589 /* we need 3 extra chars, 1 for the NUL and
4590 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004591 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4592 compl_length) + 3);
4593 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004595 if (!vim_iswordp(line + compl_col)
4596 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 && (
4598#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004599 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004601 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602#endif
4603 )))
4604 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004605 STRCPY((char *)compl_pattern, prefix);
4606 (void)quote_meta(compl_pattern + STRLEN(prefix),
4607 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004609 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004611 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004613 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614#endif
4615 )
4616 {
4617 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004618 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4619 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004621 compl_col += curs_col;
4622 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 }
4624 else
4625 {
4626#ifdef FEAT_MBYTE
4627 /* Search the point of change class of multibyte character
4628 * or not a word single byte character backward. */
4629 if (has_mbyte)
4630 {
4631 int base_class;
4632 int head_off;
4633
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004634 startcol -= (*mb_head_off)(line, line + startcol);
4635 base_class = mb_get_class(line + startcol);
4636 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004638 head_off = (*mb_head_off)(line, line + startcol);
4639 if (base_class != mb_get_class(line + startcol
4640 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004642 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 }
4644 }
4645 else
4646#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004647 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004649 compl_col += ++startcol;
4650 compl_length = (int)curs_col - startcol;
4651 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 {
4653 /* Only match word with at least two chars -- webb
4654 * there's no need to call quote_meta,
4655 * alloc(7) is enough -- Acevedo
4656 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004657 compl_pattern = alloc(7);
4658 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004660 STRCPY((char *)compl_pattern, "\\<");
4661 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4662 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 }
4664 else
4665 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004666 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4667 compl_length) + 3);
4668 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004670 STRCPY((char *)compl_pattern, "\\<");
4671 (void)quote_meta(compl_pattern + 2, line + compl_col,
4672 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 }
4674 }
4675 }
4676 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4677 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004678 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004679 compl_length = (int)curs_col - (int)compl_col;
4680 if (compl_length < 0) /* cursor in indent: empty pattern */
4681 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004683 compl_pattern = str_foldcase(line + compl_col, compl_length,
4684 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004686 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4687 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 return FAIL;
4689 }
4690 else if (ctrl_x_mode == CTRL_X_FILES)
4691 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004692 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004694 compl_col += ++startcol;
4695 compl_length = (int)curs_col - startcol;
4696 compl_pattern = addstar(line + compl_col, compl_length,
4697 EXPAND_FILES);
4698 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 return FAIL;
4700 }
4701 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4702 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004703 compl_pattern = vim_strnsave(line, curs_col);
4704 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004706 set_cmd_context(&compl_xp, compl_pattern,
4707 (int)STRLEN(compl_pattern), curs_col);
4708 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4709 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00004710 /* No completion possible, use an empty pattern to get a
4711 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004712 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004713 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00004714 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
4715 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004717 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004718 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00004719#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004720 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00004721 * Call user defined function 'completefunc' with "a:findstart"
4722 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004723 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00004724 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004725 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004726 char_u *funcname;
4727 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004728
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004729 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004730 * string */
4731 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4732 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4733 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004734 {
4735 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
4736 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004737 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004738 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004739
4740 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004741 args[1] = NULL;
4742 pos = curwin->w_cursor;
4743 col = call_func_retnr(funcname, 2, args, FALSE);
4744 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004745
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004746 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004747 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004748 compl_col = col;
4749 if ((colnr_T)compl_col > curs_col)
4750 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004751
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004752 /* Setup variables for completion. Need to obtain "line" again,
4753 * it may have become invalid. */
4754 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004755 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004756 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4757 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004758#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004759 return FAIL;
4760 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00004761 else if (ctrl_x_mode == CTRL_X_SPELL)
4762 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004763#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004764 if (spell_bad_len > 0)
4765 compl_col = curs_col - spell_bad_len;
4766 else
4767 compl_col = spell_word_start(startcol);
4768 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004769 {
4770 compl_length = 0;
4771 compl_col = curs_col;
4772 }
4773 else
4774 {
4775 spell_expand_check_cap(compl_col);
4776 compl_length = (int)curs_col - compl_col;
4777 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00004778 /* Need to obtain "line" again, it may have become invalid. */
4779 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004780 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4781 if (compl_pattern == NULL)
4782#endif
4783 return FAIL;
4784 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004785 else
4786 {
4787 EMSG2(_(e_intern2), "ins_complete()");
4788 return FAIL;
4789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004791 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 {
4793 edit_submode_pre = (char_u *)_(" Adding");
4794 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4795 {
4796 /* Insert a new line, keep indentation but ignore 'comments' */
4797#ifdef FEAT_COMMENTS
4798 char_u *old = curbuf->b_p_com;
4799
4800 curbuf->b_p_com = (char_u *)"";
4801#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004802 compl_startpos.lnum = curwin->w_cursor.lnum;
4803 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 ins_eol('\r');
4805#ifdef FEAT_COMMENTS
4806 curbuf->b_p_com = old;
4807#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004808 compl_length = 0;
4809 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 }
4811 }
4812 else
4813 {
4814 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004815 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 }
4817
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004818 if (compl_cont_status & CONT_LOCAL)
4819 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 else
4821 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4822
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004823 /* Always add completion for the original text. */
4824 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004825 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4826 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004827 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004829 vim_free(compl_pattern);
4830 compl_pattern = NULL;
4831 vim_free(compl_orig_text);
4832 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 return FAIL;
4834 }
4835
4836 /* showmode might reset the internal line pointers, so it must
4837 * be called before line = ml_get(), or when this address is no
4838 * longer needed. -- Acevedo.
4839 */
4840 edit_submode_extra = (char_u *)_("-- Searching...");
4841 edit_submode_highl = HLF_COUNT;
4842 showmode();
4843 edit_submode_extra = NULL;
4844 out_flush();
4845 }
4846
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004847 compl_shown_match = compl_curr_match;
4848 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849
4850 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004851 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004853 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004855 /* may undisplay the popup menu */
4856 ins_compl_upd_pum();
4857
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004858 if (n > 1) /* all matches have been found */
4859 compl_matches = n;
4860 compl_curr_match = compl_shown_match;
4861 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862
Bram Moolenaard68071d2006-05-02 22:08:30 +00004863 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
4864 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 if (got_int && !global_busy)
4866 {
4867 (void)vgetc();
4868 got_int = FALSE;
4869 }
4870
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004871 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004872 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004874 edit_submode_extra = (compl_cont_status & CONT_ADDING)
4875 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
4877 edit_submode_highl = HLF_E;
4878 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4879 * because we couldn't expand anything at first place, but if we used
4880 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4881 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004882 if ( compl_length > 1
4883 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 || (ctrl_x_mode != 0
4885 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
4886 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004887 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 }
4889
Bram Moolenaar572cb562005-08-05 21:35:02 +00004890 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004891 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004893 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894
4895 if (edit_submode_extra == NULL)
4896 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004897 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 {
4899 edit_submode_extra = (char_u *)_("Back at original");
4900 edit_submode_highl = HLF_W;
4901 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004902 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 {
4904 edit_submode_extra = (char_u *)_("Word from other line");
4905 edit_submode_highl = HLF_COUNT;
4906 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004907 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 {
4909 edit_submode_extra = (char_u *)_("The only match");
4910 edit_submode_highl = HLF_COUNT;
4911 }
4912 else
4913 {
4914 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004915 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004917 int number = 0;
4918 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004920 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 {
4922 /* search backwards for the first valid (!= -1) number.
4923 * This should normally succeed already at the first loop
4924 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004925 for (match = compl_curr_match->cp_prev; match != NULL
4926 && match != compl_first_match;
4927 match = match->cp_prev)
4928 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004930 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 break;
4932 }
4933 if (match != NULL)
4934 /* go up and assign all numbers which are not assigned
4935 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004936 for (match = match->cp_next;
4937 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004938 match = match->cp_next)
4939 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 }
4941 else /* BACKWARD */
4942 {
4943 /* search forwards (upwards) for the first valid (!= -1)
4944 * number. This should normally succeed already at the
4945 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004946 for (match = compl_curr_match->cp_next; match != NULL
4947 && match != compl_first_match;
4948 match = match->cp_next)
4949 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004951 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 break;
4953 }
4954 if (match != NULL)
4955 /* go down and assign all numbers which are not
4956 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004957 for (match = match->cp_prev; match
4958 && match->cp_number == -1;
4959 match = match->cp_prev)
4960 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 }
4962 }
4963
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004964 /* The match should always have a sequence number now, this is
4965 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004966 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 {
4968 /* Space for 10 text chars. + 2x10-digit no.s */
4969 static char_u match_ref[31];
4970
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004971 if (compl_matches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 sprintf((char *)IObuff, _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004973 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004975 sprintf((char *)IObuff, _("match %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004976 compl_curr_match->cp_number);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004977 vim_strncpy(match_ref, IObuff, 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 edit_submode_extra = match_ref;
4979 edit_submode_highl = HLF_R;
4980 if (dollar_vcol)
4981 curs_columns(FALSE);
4982 }
4983 }
4984 }
4985
4986 /* Show a message about what (completion) mode we're in. */
4987 showmode();
4988 if (edit_submode_extra != NULL)
4989 {
4990 if (!p_smd)
4991 msg_attr(edit_submode_extra,
4992 edit_submode_highl < HLF_COUNT
4993 ? hl_attr(edit_submode_highl) : 0);
4994 }
4995 else
4996 msg_clr_cmdline(); /* necessary for "noshowmode" */
4997
Bram Moolenaard68071d2006-05-02 22:08:30 +00004998 /* Show the popup menu, unless we got interrupted. */
4999 if (!compl_interrupted)
5000 {
5001 /* RedrawingDisabled may be set when invoked through complete(). */
5002 n = RedrawingDisabled;
5003 RedrawingDisabled = 0;
5004 ins_compl_show_pum();
5005 setcursor();
5006 RedrawingDisabled = n;
5007 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005008 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005009 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005010
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 return OK;
5012}
5013
5014/*
5015 * Looks in the first "len" chars. of "src" for search-metachars.
5016 * If dest is not NULL the chars. are copied there quoting (with
5017 * a backslash) the metachars, and dest would be NUL terminated.
5018 * Returns the length (needed) of dest
5019 */
5020 static int
5021quote_meta(dest, src, len)
5022 char_u *dest;
5023 char_u *src;
5024 int len;
5025{
5026 int m;
5027
5028 for (m = len; --len >= 0; src++)
5029 {
5030 switch (*src)
5031 {
5032 case '.':
5033 case '*':
5034 case '[':
5035 if (ctrl_x_mode == CTRL_X_DICTIONARY
5036 || ctrl_x_mode == CTRL_X_THESAURUS)
5037 break;
5038 case '~':
5039 if (!p_magic) /* quote these only if magic is set */
5040 break;
5041 case '\\':
5042 if (ctrl_x_mode == CTRL_X_DICTIONARY
5043 || ctrl_x_mode == CTRL_X_THESAURUS)
5044 break;
5045 case '^': /* currently it's not needed. */
5046 case '$':
5047 m++;
5048 if (dest != NULL)
5049 *dest++ = '\\';
5050 break;
5051 }
5052 if (dest != NULL)
5053 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005054# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 /* Copy remaining bytes of a multibyte character. */
5056 if (has_mbyte)
5057 {
5058 int i, mb_len;
5059
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005060 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 if (mb_len > 0 && len >= mb_len)
5062 for (i = 0; i < mb_len; ++i)
5063 {
5064 --len;
5065 ++src;
5066 if (dest != NULL)
5067 *dest++ = *src;
5068 }
5069 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005070# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 }
5072 if (dest != NULL)
5073 *dest = NUL;
5074
5075 return m;
5076}
5077#endif /* FEAT_INS_EXPAND */
5078
5079/*
5080 * Next character is interpreted literally.
5081 * A one, two or three digit decimal number is interpreted as its byte value.
5082 * If one or two digits are entered, the next character is given to vungetc().
5083 * For Unicode a character > 255 may be returned.
5084 */
5085 int
5086get_literal()
5087{
5088 int cc;
5089 int nc;
5090 int i;
5091 int hex = FALSE;
5092 int octal = FALSE;
5093#ifdef FEAT_MBYTE
5094 int unicode = 0;
5095#endif
5096
5097 if (got_int)
5098 return Ctrl_C;
5099
5100#ifdef FEAT_GUI
5101 /*
5102 * In GUI there is no point inserting the internal code for a special key.
5103 * It is more useful to insert the string "<KEY>" instead. This would
5104 * probably be useful in a text window too, but it would not be
5105 * vi-compatible (maybe there should be an option for it?) -- webb
5106 */
5107 if (gui.in_use)
5108 ++allow_keys;
5109#endif
5110#ifdef USE_ON_FLY_SCROLL
5111 dont_scroll = TRUE; /* disallow scrolling here */
5112#endif
5113 ++no_mapping; /* don't map the next key hits */
5114 cc = 0;
5115 i = 0;
5116 for (;;)
5117 {
5118 do
5119 nc = safe_vgetc();
5120 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
5121 || nc == K_HOR_SCROLLBAR);
5122#ifdef FEAT_CMDL_INFO
5123 if (!(State & CMDLINE)
5124# ifdef FEAT_MBYTE
5125 && MB_BYTE2LEN_CHECK(nc) == 1
5126# endif
5127 )
5128 add_to_showcmd(nc);
5129#endif
5130 if (nc == 'x' || nc == 'X')
5131 hex = TRUE;
5132 else if (nc == 'o' || nc == 'O')
5133 octal = TRUE;
5134#ifdef FEAT_MBYTE
5135 else if (nc == 'u' || nc == 'U')
5136 unicode = nc;
5137#endif
5138 else
5139 {
5140 if (hex
5141#ifdef FEAT_MBYTE
5142 || unicode != 0
5143#endif
5144 )
5145 {
5146 if (!vim_isxdigit(nc))
5147 break;
5148 cc = cc * 16 + hex2nr(nc);
5149 }
5150 else if (octal)
5151 {
5152 if (nc < '0' || nc > '7')
5153 break;
5154 cc = cc * 8 + nc - '0';
5155 }
5156 else
5157 {
5158 if (!VIM_ISDIGIT(nc))
5159 break;
5160 cc = cc * 10 + nc - '0';
5161 }
5162
5163 ++i;
5164 }
5165
5166 if (cc > 255
5167#ifdef FEAT_MBYTE
5168 && unicode == 0
5169#endif
5170 )
5171 cc = 255; /* limit range to 0-255 */
5172 nc = 0;
5173
5174 if (hex) /* hex: up to two chars */
5175 {
5176 if (i >= 2)
5177 break;
5178 }
5179#ifdef FEAT_MBYTE
5180 else if (unicode) /* Unicode: up to four or eight chars */
5181 {
5182 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5183 break;
5184 }
5185#endif
5186 else if (i >= 3) /* decimal or octal: up to three chars */
5187 break;
5188 }
5189 if (i == 0) /* no number entered */
5190 {
5191 if (nc == K_ZERO) /* NUL is stored as NL */
5192 {
5193 cc = '\n';
5194 nc = 0;
5195 }
5196 else
5197 {
5198 cc = nc;
5199 nc = 0;
5200 }
5201 }
5202
5203 if (cc == 0) /* NUL is stored as NL */
5204 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005205#ifdef FEAT_MBYTE
5206 if (enc_dbcs && (cc & 0xff) == 0)
5207 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5208 second byte will cause trouble! */
5209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210
5211 --no_mapping;
5212#ifdef FEAT_GUI
5213 if (gui.in_use)
5214 --allow_keys;
5215#endif
5216 if (nc)
5217 vungetc(nc);
5218 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5219 return cc;
5220}
5221
5222/*
5223 * Insert character, taking care of special keys and mod_mask
5224 */
5225 static void
5226insert_special(c, allow_modmask, ctrlv)
5227 int c;
5228 int allow_modmask;
5229 int ctrlv; /* c was typed after CTRL-V */
5230{
5231 char_u *p;
5232 int len;
5233
5234 /*
5235 * Special function key, translate into "<Key>". Up to the last '>' is
5236 * inserted with ins_str(), so as not to replace characters in replace
5237 * mode.
5238 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5239 * unless 'allow_modmask' is TRUE.
5240 */
5241#ifdef MACOS
5242 /* Command-key never produces a normal key */
5243 if (mod_mask & MOD_MASK_CMD)
5244 allow_modmask = TRUE;
5245#endif
5246 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5247 {
5248 p = get_special_key_name(c, mod_mask);
5249 len = (int)STRLEN(p);
5250 c = p[len - 1];
5251 if (len > 2)
5252 {
5253 if (stop_arrow() == FAIL)
5254 return;
5255 p[len - 1] = NUL;
5256 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005257 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 ctrlv = FALSE;
5259 }
5260 }
5261 if (stop_arrow() == OK)
5262 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5263}
5264
5265/*
5266 * Special characters in this context are those that need processing other
5267 * than the simple insertion that can be performed here. This includes ESC
5268 * which terminates the insert, and CR/NL which need special processing to
5269 * open up a new line. This routine tries to optimize insertions performed by
5270 * the "redo", "undo" or "put" commands, so it needs to know when it should
5271 * stop and defer processing to the "normal" mechanism.
5272 * '0' and '^' are special, because they can be followed by CTRL-D.
5273 */
5274#ifdef EBCDIC
5275# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5276#else
5277# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5278#endif
5279
5280#ifdef FEAT_MBYTE
5281# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5282#else
5283# define WHITECHAR(cc) vim_iswhite(cc)
5284#endif
5285
5286 void
5287insertchar(c, flags, second_indent)
5288 int c; /* character to insert or NUL */
5289 int flags; /* INSCHAR_FORMAT, etc. */
5290 int second_indent; /* indent for second line if >= 0 */
5291{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 int textwidth;
5293#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297
5298 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5299 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300
5301 /*
5302 * Try to break the line in two or more pieces when:
5303 * - Always do this if we have been called to do formatting only.
5304 * - Always do this when 'formatoptions' has the 'a' flag and the line
5305 * ends in white space.
5306 * - Otherwise:
5307 * - Don't do this if inserting a blank
5308 * - Don't do this if an existing character is being replaced, unless
5309 * we're in VREPLACE mode.
5310 * - Do this if the cursor is not on the line where insert started
5311 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5312 * before the insert.
5313 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5314 * before 'textwidth'
5315 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005316 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 && ((flags & INSCHAR_FORMAT)
5318 || (!vim_iswhite(c)
5319 && !((State & REPLACE_FLAG)
5320#ifdef FEAT_VREPLACE
5321 && !(State & VREPLACE_FLAG)
5322#endif
5323 && *ml_get_cursor() != NUL)
5324 && (curwin->w_cursor.lnum != Insstart.lnum
5325 || ((!has_format_option(FO_INS_LONG)
5326 || Insstart_textlen <= (colnr_T)textwidth)
5327 && (!fo_ins_blank
5328 || Insstart_blank_vcol <= (colnr_T)textwidth
5329 ))))))
5330 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005331 /* Format with 'formatexpr' when it's set. Use internal formatting
5332 * when 'formatexpr' isn't set or it returns non-zero. */
5333#if defined(FEAT_EVAL)
5334 if (*curbuf->b_p_fex == NUL
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005335 || fex_format(curwin->w_cursor.lnum, 1L, c) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005337 internal_format(textwidth, second_indent, flags, c == NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005339
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 if (c == NUL) /* only formatting was wanted */
5341 return;
5342
5343#ifdef FEAT_COMMENTS
5344 /* Check whether this character should end a comment. */
5345 if (did_ai && (int)c == end_comment_pending)
5346 {
5347 char_u *line;
5348 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5349 int middle_len, end_len;
5350 int i;
5351
5352 /*
5353 * Need to remove existing (middle) comment leader and insert end
5354 * comment leader. First, check what comment leader we can find.
5355 */
5356 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5357 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5358 {
5359 /* Skip middle-comment string */
5360 while (*p && p[-1] != ':') /* find end of middle flags */
5361 ++p;
5362 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5363 /* Don't count trailing white space for middle_len */
5364 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5365 --middle_len;
5366
5367 /* Find the end-comment string */
5368 while (*p && p[-1] != ':') /* find end of end flags */
5369 ++p;
5370 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5371
5372 /* Skip white space before the cursor */
5373 i = curwin->w_cursor.col;
5374 while (--i >= 0 && vim_iswhite(line[i]))
5375 ;
5376 i++;
5377
5378 /* Skip to before the middle leader */
5379 i -= middle_len;
5380
5381 /* Check some expected things before we go on */
5382 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5383 {
5384 /* Backspace over all the stuff we want to replace */
5385 backspace_until_column(i);
5386
5387 /*
5388 * Insert the end-comment string, except for the last
5389 * character, which will get inserted as normal later.
5390 */
5391 ins_bytes_len(lead_end, end_len - 1);
5392 }
5393 }
5394 }
5395 end_comment_pending = NUL;
5396#endif
5397
5398 did_ai = FALSE;
5399#ifdef FEAT_SMARTINDENT
5400 did_si = FALSE;
5401 can_si = FALSE;
5402 can_si_back = FALSE;
5403#endif
5404
5405 /*
5406 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5407 * This speeds up normal text input considerably.
5408 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5409 * need to re-indent at a ':', or any other character (but not what
5410 * 'paste' is set)..
5411 */
5412#ifdef USE_ON_FLY_SCROLL
5413 dont_scroll = FALSE; /* allow scrolling here */
5414#endif
5415
5416 if ( !ISSPECIAL(c)
5417#ifdef FEAT_MBYTE
5418 && (!has_mbyte || (*mb_char2len)(c) == 1)
5419#endif
5420 && vpeekc() != NUL
5421 && !(State & REPLACE_FLAG)
5422#ifdef FEAT_CINDENT
5423 && !cindent_on()
5424#endif
5425#ifdef FEAT_RIGHTLEFT
5426 && !p_ri
5427#endif
5428 )
5429 {
5430#define INPUT_BUFLEN 100
5431 char_u buf[INPUT_BUFLEN + 1];
5432 int i;
5433 colnr_T virtcol = 0;
5434
5435 buf[0] = c;
5436 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005437 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 virtcol = get_nolist_virtcol();
5439 /*
5440 * Stop the string when:
5441 * - no more chars available
5442 * - finding a special character (command key)
5443 * - buffer is full
5444 * - running into the 'textwidth' boundary
5445 * - need to check for abbreviation: A non-word char after a word-char
5446 */
5447 while ( (c = vpeekc()) != NUL
5448 && !ISSPECIAL(c)
5449#ifdef FEAT_MBYTE
5450 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5451#endif
5452 && i < INPUT_BUFLEN
5453 && (textwidth == 0
5454 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5455 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5456 {
5457#ifdef FEAT_RIGHTLEFT
5458 c = vgetc();
5459 if (p_hkmap && KeyTyped)
5460 c = hkmap(c); /* Hebrew mode mapping */
5461# ifdef FEAT_FKMAP
5462 if (p_fkmap && KeyTyped)
5463 c = fkmap(c); /* Farsi mode mapping */
5464# endif
5465 buf[i++] = c;
5466#else
5467 buf[i++] = vgetc();
5468#endif
5469 }
5470
5471#ifdef FEAT_DIGRAPHS
5472 do_digraph(-1); /* clear digraphs */
5473 do_digraph(buf[i-1]); /* may be the start of a digraph */
5474#endif
5475 buf[i] = NUL;
5476 ins_str(buf);
5477 if (flags & INSCHAR_CTRLV)
5478 {
5479 redo_literal(*buf);
5480 i = 1;
5481 }
5482 else
5483 i = 0;
5484 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005485 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 }
5487 else
5488 {
5489#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005490 int cc;
5491
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5493 {
5494 char_u buf[MB_MAXBYTES + 1];
5495
5496 (*mb_char2bytes)(c, buf);
5497 buf[cc] = NUL;
5498 ins_char_bytes(buf, cc);
5499 AppendCharToRedobuff(c);
5500 }
5501 else
5502#endif
5503 {
5504 ins_char(c);
5505 if (flags & INSCHAR_CTRLV)
5506 redo_literal(c);
5507 else
5508 AppendCharToRedobuff(c);
5509 }
5510 }
5511}
5512
5513/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005514 * Format text at the current insert position.
5515 */
5516 static void
5517internal_format(textwidth, second_indent, flags, format_only)
5518 int textwidth;
5519 int second_indent;
5520 int flags;
5521 int format_only;
5522{
5523 int cc;
5524 int save_char = NUL;
5525 int haveto_redraw = FALSE;
5526 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5527#ifdef FEAT_MBYTE
5528 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5529#endif
5530 int fo_white_par = has_format_option(FO_WHITE_PAR);
5531 int first_line = TRUE;
5532#ifdef FEAT_COMMENTS
5533 colnr_T leader_len;
5534 int no_leader = FALSE;
5535 int do_comments = (flags & INSCHAR_DO_COM);
5536#endif
5537
5538 /*
5539 * When 'ai' is off we don't want a space under the cursor to be
5540 * deleted. Replace it with an 'x' temporarily.
5541 */
5542 if (!curbuf->b_p_ai)
5543 {
5544 cc = gchar_cursor();
5545 if (vim_iswhite(cc))
5546 {
5547 save_char = cc;
5548 pchar_cursor('x');
5549 }
5550 }
5551
5552 /*
5553 * Repeat breaking lines, until the current line is not too long.
5554 */
5555 while (!got_int)
5556 {
5557 int startcol; /* Cursor column at entry */
5558 int wantcol; /* column at textwidth border */
5559 int foundcol; /* column for start of spaces */
5560 int end_foundcol = 0; /* column for start of word */
5561 colnr_T len;
5562 colnr_T virtcol;
5563#ifdef FEAT_VREPLACE
5564 int orig_col = 0;
5565 char_u *saved_text = NULL;
5566#endif
5567 colnr_T col;
5568
5569 virtcol = get_nolist_virtcol();
5570 if (virtcol < (colnr_T)textwidth)
5571 break;
5572
5573#ifdef FEAT_COMMENTS
5574 if (no_leader)
5575 do_comments = FALSE;
5576 else if (!(flags & INSCHAR_FORMAT)
5577 && has_format_option(FO_WRAP_COMS))
5578 do_comments = TRUE;
5579
5580 /* Don't break until after the comment leader */
5581 if (do_comments)
5582 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5583 else
5584 leader_len = 0;
5585
5586 /* If the line doesn't start with a comment leader, then don't
5587 * start one in a following broken line. Avoids that a %word
5588 * moved to the start of the next line causes all following lines
5589 * to start with %. */
5590 if (leader_len == 0)
5591 no_leader = TRUE;
5592#endif
5593 if (!(flags & INSCHAR_FORMAT)
5594#ifdef FEAT_COMMENTS
5595 && leader_len == 0
5596#endif
5597 && !has_format_option(FO_WRAP))
5598
5599 {
5600 textwidth = 0;
5601 break;
5602 }
5603 if ((startcol = curwin->w_cursor.col) == 0)
5604 break;
5605
5606 /* find column of textwidth border */
5607 coladvance((colnr_T)textwidth);
5608 wantcol = curwin->w_cursor.col;
5609
5610 curwin->w_cursor.col = startcol - 1;
5611#ifdef FEAT_MBYTE
5612 /* Correct cursor for multi-byte character. */
5613 if (has_mbyte)
5614 mb_adjust_cursor();
5615#endif
5616 foundcol = 0;
5617
5618 /*
5619 * Find position to break at.
5620 * Stop at first entered white when 'formatoptions' has 'v'
5621 */
5622 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5623 || curwin->w_cursor.lnum != Insstart.lnum
5624 || curwin->w_cursor.col >= Insstart.col)
5625 {
5626 cc = gchar_cursor();
5627 if (WHITECHAR(cc))
5628 {
5629 /* remember position of blank just before text */
5630 end_foundcol = curwin->w_cursor.col;
5631
5632 /* find start of sequence of blanks */
5633 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5634 {
5635 dec_cursor();
5636 cc = gchar_cursor();
5637 }
5638 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5639 break; /* only spaces in front of text */
5640#ifdef FEAT_COMMENTS
5641 /* Don't break until after the comment leader */
5642 if (curwin->w_cursor.col < leader_len)
5643 break;
5644#endif
5645 if (has_format_option(FO_ONE_LETTER))
5646 {
5647 /* do not break after one-letter words */
5648 if (curwin->w_cursor.col == 0)
5649 break; /* one-letter word at begin */
5650
5651 col = curwin->w_cursor.col;
5652 dec_cursor();
5653 cc = gchar_cursor();
5654
5655 if (WHITECHAR(cc))
5656 continue; /* one-letter, continue */
5657 curwin->w_cursor.col = col;
5658 }
5659#ifdef FEAT_MBYTE
5660 if (has_mbyte)
5661 foundcol = curwin->w_cursor.col
5662 + (*mb_ptr2len)(ml_get_cursor());
5663 else
5664#endif
5665 foundcol = curwin->w_cursor.col + 1;
5666 if (curwin->w_cursor.col < (colnr_T)wantcol)
5667 break;
5668 }
5669#ifdef FEAT_MBYTE
5670 else if (cc >= 0x100 && fo_multibyte
5671 && curwin->w_cursor.col <= (colnr_T)wantcol)
5672 {
5673 /* Break after or before a multi-byte character. */
5674 foundcol = curwin->w_cursor.col;
5675 if (curwin->w_cursor.col < (colnr_T)wantcol)
5676 foundcol += (*mb_char2len)(cc);
5677 end_foundcol = foundcol;
5678 break;
5679 }
5680#endif
5681 if (curwin->w_cursor.col == 0)
5682 break;
5683 dec_cursor();
5684 }
5685
5686 if (foundcol == 0) /* no spaces, cannot break line */
5687 {
5688 curwin->w_cursor.col = startcol;
5689 break;
5690 }
5691
5692 /* Going to break the line, remove any "$" now. */
5693 undisplay_dollar();
5694
5695 /*
5696 * Offset between cursor position and line break is used by replace
5697 * stack functions. VREPLACE does not use this, and backspaces
5698 * over the text instead.
5699 */
5700#ifdef FEAT_VREPLACE
5701 if (State & VREPLACE_FLAG)
5702 orig_col = startcol; /* Will start backspacing from here */
5703 else
5704#endif
5705 replace_offset = startcol - end_foundcol - 1;
5706
5707 /*
5708 * adjust startcol for spaces that will be deleted and
5709 * characters that will remain on top line
5710 */
5711 curwin->w_cursor.col = foundcol;
5712 while (cc = gchar_cursor(), WHITECHAR(cc))
5713 inc_cursor();
5714 startcol -= curwin->w_cursor.col;
5715 if (startcol < 0)
5716 startcol = 0;
5717
5718#ifdef FEAT_VREPLACE
5719 if (State & VREPLACE_FLAG)
5720 {
5721 /*
5722 * In VREPLACE mode, we will backspace over the text to be
5723 * wrapped, so save a copy now to put on the next line.
5724 */
5725 saved_text = vim_strsave(ml_get_cursor());
5726 curwin->w_cursor.col = orig_col;
5727 if (saved_text == NULL)
5728 break; /* Can't do it, out of memory */
5729 saved_text[startcol] = NUL;
5730
5731 /* Backspace over characters that will move to the next line */
5732 if (!fo_white_par)
5733 backspace_until_column(foundcol);
5734 }
5735 else
5736#endif
5737 {
5738 /* put cursor after pos. to break line */
5739 if (!fo_white_par)
5740 curwin->w_cursor.col = foundcol;
5741 }
5742
5743 /*
5744 * Split the line just before the margin.
5745 * Only insert/delete lines, but don't really redraw the window.
5746 */
5747 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
5748 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
5749#ifdef FEAT_COMMENTS
5750 + (do_comments ? OPENLINE_DO_COM : 0)
5751#endif
5752 , old_indent);
5753 old_indent = 0;
5754
5755 replace_offset = 0;
5756 if (first_line)
5757 {
5758 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
5759 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
5760 if (second_indent >= 0)
5761 {
5762#ifdef FEAT_VREPLACE
5763 if (State & VREPLACE_FLAG)
5764 change_indent(INDENT_SET, second_indent, FALSE, NUL);
5765 else
5766#endif
5767 (void)set_indent(second_indent, SIN_CHANGED);
5768 }
5769 first_line = FALSE;
5770 }
5771
5772#ifdef FEAT_VREPLACE
5773 if (State & VREPLACE_FLAG)
5774 {
5775 /*
5776 * In VREPLACE mode we have backspaced over the text to be
5777 * moved, now we re-insert it into the new line.
5778 */
5779 ins_bytes(saved_text);
5780 vim_free(saved_text);
5781 }
5782 else
5783#endif
5784 {
5785 /*
5786 * Check if cursor is not past the NUL off the line, cindent
5787 * may have added or removed indent.
5788 */
5789 curwin->w_cursor.col += startcol;
5790 len = (colnr_T)STRLEN(ml_get_curline());
5791 if (curwin->w_cursor.col > len)
5792 curwin->w_cursor.col = len;
5793 }
5794
5795 haveto_redraw = TRUE;
5796#ifdef FEAT_CINDENT
5797 can_cindent = TRUE;
5798#endif
5799 /* moved the cursor, don't autoindent or cindent now */
5800 did_ai = FALSE;
5801#ifdef FEAT_SMARTINDENT
5802 did_si = FALSE;
5803 can_si = FALSE;
5804 can_si_back = FALSE;
5805#endif
5806 line_breakcheck();
5807 }
5808
5809 if (save_char != NUL) /* put back space after cursor */
5810 pchar_cursor(save_char);
5811
5812 if (!format_only && haveto_redraw)
5813 {
5814 update_topline();
5815 redraw_curbuf_later(VALID);
5816 }
5817}
5818
5819/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 * Called after inserting or deleting text: When 'formatoptions' includes the
5821 * 'a' flag format from the current line until the end of the paragraph.
5822 * Keep the cursor at the same position relative to the text.
5823 * The caller must have saved the cursor line for undo, following ones will be
5824 * saved here.
5825 */
5826 void
5827auto_format(trailblank, prev_line)
5828 int trailblank; /* when TRUE also format with trailing blank */
5829 int prev_line; /* may start in previous line */
5830{
5831 pos_T pos;
5832 colnr_T len;
5833 char_u *old;
5834 char_u *new, *pnew;
5835 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005836 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837
5838 if (!has_format_option(FO_AUTO))
5839 return;
5840
5841 pos = curwin->w_cursor;
5842 old = ml_get_curline();
5843
5844 /* may remove added space */
5845 check_auto_format(FALSE);
5846
5847 /* Don't format in Insert mode when the cursor is on a trailing blank, the
5848 * user might insert normal text next. Also skip formatting when "1" is
5849 * in 'formatoptions' and there is a single character before the cursor.
5850 * Otherwise the line would be broken and when typing another non-white
5851 * next they are not joined back together. */
5852 wasatend = (pos.col == STRLEN(old));
5853 if (*old != NUL && !trailblank && wasatend)
5854 {
5855 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005856 cc = gchar_cursor();
5857 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
5858 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005860 cc = gchar_cursor();
5861 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 {
5863 curwin->w_cursor = pos;
5864 return;
5865 }
5866 curwin->w_cursor = pos;
5867 }
5868
5869#ifdef FEAT_COMMENTS
5870 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
5871 * comments. */
5872 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
5873 && get_leader_len(old, NULL, FALSE) == 0)
5874 return;
5875#endif
5876
5877 /*
5878 * May start formatting in a previous line, so that after "x" a word is
5879 * moved to the previous line if it fits there now. Only when this is not
5880 * the start of a paragraph.
5881 */
5882 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
5883 {
5884 --curwin->w_cursor.lnum;
5885 if (u_save_cursor() == FAIL)
5886 return;
5887 }
5888
5889 /*
5890 * Do the formatting and restore the cursor position. "saved_cursor" will
5891 * be adjusted for the text formatting.
5892 */
5893 saved_cursor = pos;
5894 format_lines((linenr_T)-1);
5895 curwin->w_cursor = saved_cursor;
5896 saved_cursor.lnum = 0;
5897
5898 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5899 {
5900 /* "cannot happen" */
5901 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5902 coladvance((colnr_T)MAXCOL);
5903 }
5904 else
5905 check_cursor_col();
5906
5907 /* Insert mode: If the cursor is now after the end of the line while it
5908 * previously wasn't, the line was broken. Because of the rule above we
5909 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
5910 * formatted. */
5911 if (!wasatend && has_format_option(FO_WHITE_PAR))
5912 {
5913 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005914 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 if (curwin->w_cursor.col == len)
5916 {
5917 pnew = vim_strnsave(new, len + 2);
5918 pnew[len] = ' ';
5919 pnew[len + 1] = NUL;
5920 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
5921 /* remove the space later */
5922 did_add_space = TRUE;
5923 }
5924 else
5925 /* may remove added space */
5926 check_auto_format(FALSE);
5927 }
5928
5929 check_cursor();
5930}
5931
5932/*
5933 * When an extra space was added to continue a paragraph for auto-formatting,
5934 * delete it now. The space must be under the cursor, just after the insert
5935 * position.
5936 */
5937 static void
5938check_auto_format(end_insert)
5939 int end_insert; /* TRUE when ending Insert mode */
5940{
5941 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005942 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943
5944 if (did_add_space)
5945 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005946 cc = gchar_cursor();
5947 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 /* Somehow the space was removed already. */
5949 did_add_space = FALSE;
5950 else
5951 {
5952 if (!end_insert)
5953 {
5954 inc_cursor();
5955 c = gchar_cursor();
5956 dec_cursor();
5957 }
5958 if (c != NUL)
5959 {
5960 /* The space is no longer at the end of the line, delete it. */
5961 del_char(FALSE);
5962 did_add_space = FALSE;
5963 }
5964 }
5965 }
5966}
5967
5968/*
5969 * Find out textwidth to be used for formatting:
5970 * if 'textwidth' option is set, use it
5971 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
5972 * if invalid value, use 0.
5973 * Set default to window width (maximum 79) for "gq" operator.
5974 */
5975 int
5976comp_textwidth(ff)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005977 int ff; /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978{
5979 int textwidth;
5980
5981 textwidth = curbuf->b_p_tw;
5982 if (textwidth == 0 && curbuf->b_p_wm)
5983 {
5984 /* The width is the window width minus 'wrapmargin' minus all the
5985 * things that add to the margin. */
5986 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
5987#ifdef FEAT_CMDWIN
5988 if (cmdwin_type != 0)
5989 textwidth -= 1;
5990#endif
5991#ifdef FEAT_FOLDING
5992 textwidth -= curwin->w_p_fdc;
5993#endif
5994#ifdef FEAT_SIGNS
5995 if (curwin->w_buffer->b_signlist != NULL
5996# ifdef FEAT_NETBEANS_INTG
5997 || usingNetbeans
5998# endif
5999 )
6000 textwidth -= 1;
6001#endif
6002 if (curwin->w_p_nu)
6003 textwidth -= 8;
6004 }
6005 if (textwidth < 0)
6006 textwidth = 0;
6007 if (ff && textwidth == 0)
6008 {
6009 textwidth = W_WIDTH(curwin) - 1;
6010 if (textwidth > 79)
6011 textwidth = 79;
6012 }
6013 return textwidth;
6014}
6015
6016/*
6017 * Put a character in the redo buffer, for when just after a CTRL-V.
6018 */
6019 static void
6020redo_literal(c)
6021 int c;
6022{
6023 char_u buf[10];
6024
6025 /* Only digits need special treatment. Translate them into a string of
6026 * three digits. */
6027 if (VIM_ISDIGIT(c))
6028 {
6029 sprintf((char *)buf, "%03d", c);
6030 AppendToRedobuff(buf);
6031 }
6032 else
6033 AppendCharToRedobuff(c);
6034}
6035
6036/*
6037 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006038 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 */
6040 static void
6041start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006042 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043{
6044 if (!arrow_used) /* something has been inserted */
6045 {
6046 AppendToRedobuff(ESC_STR);
6047 stop_insert(end_insert_pos, FALSE);
6048 arrow_used = TRUE; /* this means we stopped the current insert */
6049 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006050#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006051 check_spell_redraw();
6052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053}
6054
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006055#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006056/*
6057 * If we skipped highlighting word at cursor, do it now.
6058 * It may be skipped again, thus reset spell_redraw_lnum first.
6059 */
6060 static void
6061check_spell_redraw()
6062{
6063 if (spell_redraw_lnum != 0)
6064 {
6065 linenr_T lnum = spell_redraw_lnum;
6066
6067 spell_redraw_lnum = 0;
6068 redrawWinline(lnum, FALSE);
6069 }
6070}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006071
6072/*
6073 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6074 * spelled word, if there is one.
6075 */
6076 static void
6077spell_back_to_badword()
6078{
6079 pos_T tpos = curwin->w_cursor;
6080
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006081 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006082 if (curwin->w_cursor.col != tpos.col)
6083 start_arrow(&tpos);
6084}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006085#endif
6086
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087/*
6088 * stop_arrow() is called before a change is made in insert mode.
6089 * If an arrow key has been used, start a new insertion.
6090 * Returns FAIL if undo is impossible, shouldn't insert then.
6091 */
6092 int
6093stop_arrow()
6094{
6095 if (arrow_used)
6096 {
6097 if (u_save_cursor() == OK)
6098 {
6099 arrow_used = FALSE;
6100 ins_need_undo = FALSE;
6101 }
6102 Insstart = curwin->w_cursor; /* new insertion starts here */
6103 Insstart_textlen = linetabsize(ml_get_curline());
6104 ai_col = 0;
6105#ifdef FEAT_VREPLACE
6106 if (State & VREPLACE_FLAG)
6107 {
6108 orig_line_count = curbuf->b_ml.ml_line_count;
6109 vr_lines_changed = 1;
6110 }
6111#endif
6112 ResetRedobuff();
6113 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006114 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 }
6116 else if (ins_need_undo)
6117 {
6118 if (u_save_cursor() == OK)
6119 ins_need_undo = FALSE;
6120 }
6121
6122#ifdef FEAT_FOLDING
6123 /* Always open fold at the cursor line when inserting something. */
6124 foldOpenCursor();
6125#endif
6126
6127 return (arrow_used || ins_need_undo ? FAIL : OK);
6128}
6129
6130/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006131 * Do a few things to stop inserting.
6132 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6133 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 */
6135 static void
6136stop_insert(end_insert_pos, esc)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006137 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006138 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006140 int cc;
6141 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142
6143 stop_redo_ins();
6144 replace_flush(); /* abandon replace stack */
6145
6146 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006147 * Save the inserted text for later redo with ^@ and CTRL-A.
6148 * Don't do it when "restart_edit" was set and nothing was inserted,
6149 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006151 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006152 if (did_restart_edit == 0 || (ptr != NULL
6153 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006154 {
6155 vim_free(last_insert);
6156 last_insert = ptr;
6157 last_insert_skip = new_insert_skip;
6158 }
6159 else
6160 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006162 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 {
6164 /* Auto-format now. It may seem strange to do this when stopping an
6165 * insertion (or moving the cursor), but it's required when appending
6166 * a line and having it end in a space. But only do it when something
6167 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006168 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006170 pos_T tpos = curwin->w_cursor;
6171
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 /* When the cursor is at the end of the line after a space the
6173 * formatting will move it to the following word. Avoid that by
6174 * moving the cursor onto the space. */
6175 cc = 'x';
6176 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6177 {
6178 dec_cursor();
6179 cc = gchar_cursor();
6180 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006181 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 }
6183
6184 auto_format(TRUE, FALSE);
6185
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006186 if (vim_iswhite(cc))
6187 {
6188 if (gchar_cursor() != NUL)
6189 inc_cursor();
6190#ifdef FEAT_VIRTUALEDIT
6191 /* If the cursor is still at the same character, also keep
6192 * the "coladd". */
6193 if (gchar_cursor() == NUL
6194 && curwin->w_cursor.lnum == tpos.lnum
6195 && curwin->w_cursor.col == tpos.col)
6196 curwin->w_cursor.coladd = tpos.coladd;
6197#endif
6198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 }
6200
6201 /* If a space was inserted for auto-formatting, remove it now. */
6202 check_auto_format(TRUE);
6203
6204 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006205 * of the line, and put the cursor back.
6206 * Do this when ESC was used or moving the cursor up/down. */
6207 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006208 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006210 pos_T tpos = curwin->w_cursor;
6211
6212 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar39f05632006-03-19 22:15:26 +00006213 for (;;)
6214 {
6215 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6216 --curwin->w_cursor.col;
6217 cc = gchar_cursor();
6218 if (!vim_iswhite(cc))
6219 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 (void)del_char(TRUE);
Bram Moolenaar39f05632006-03-19 22:15:26 +00006221 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006222 if (curwin->w_cursor.lnum != tpos.lnum)
6223 curwin->w_cursor = tpos;
6224 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6226
6227#ifdef FEAT_VISUAL
6228 /* <C-S-Right> may have started Visual mode, adjust the position for
6229 * deleted characters. */
6230 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
6231 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006232 cc = (int)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 if (VIsual.col > (colnr_T)cc)
6234 {
6235 VIsual.col = cc;
6236# ifdef FEAT_VIRTUALEDIT
6237 VIsual.coladd = 0;
6238# endif
6239 }
6240 }
6241#endif
6242 }
6243 }
6244 did_ai = FALSE;
6245#ifdef FEAT_SMARTINDENT
6246 did_si = FALSE;
6247 can_si = FALSE;
6248 can_si_back = FALSE;
6249#endif
6250
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006251 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6252 * now in a different buffer. */
6253 if (end_insert_pos != NULL)
6254 {
6255 curbuf->b_op_start = Insstart;
6256 curbuf->b_op_end = *end_insert_pos;
6257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258}
6259
6260/*
6261 * Set the last inserted text to a single character.
6262 * Used for the replace command.
6263 */
6264 void
6265set_last_insert(c)
6266 int c;
6267{
6268 char_u *s;
6269
6270 vim_free(last_insert);
6271#ifdef FEAT_MBYTE
6272 last_insert = alloc(MB_MAXBYTES * 3 + 5);
6273#else
6274 last_insert = alloc(6);
6275#endif
6276 if (last_insert != NULL)
6277 {
6278 s = last_insert;
6279 /* Use the CTRL-V only when entering a special char */
6280 if (c < ' ' || c == DEL)
6281 *s++ = Ctrl_V;
6282 s = add_char2buf(c, s);
6283 *s++ = ESC;
6284 *s++ = NUL;
6285 last_insert_skip = 0;
6286 }
6287}
6288
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006289#if defined(EXITFREE) || defined(PROTO)
6290 void
6291free_last_insert()
6292{
6293 vim_free(last_insert);
6294 last_insert = NULL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006295 vim_free(compl_orig_text);
6296 compl_orig_text = NULL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006297}
6298#endif
6299
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300/*
6301 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6302 * and CSI. Handle multi-byte characters.
6303 * Returns a pointer to after the added bytes.
6304 */
6305 char_u *
6306add_char2buf(c, s)
6307 int c;
6308 char_u *s;
6309{
6310#ifdef FEAT_MBYTE
6311 char_u temp[MB_MAXBYTES];
6312 int i;
6313 int len;
6314
6315 len = (*mb_char2bytes)(c, temp);
6316 for (i = 0; i < len; ++i)
6317 {
6318 c = temp[i];
6319#endif
6320 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6321 if (c == K_SPECIAL)
6322 {
6323 *s++ = K_SPECIAL;
6324 *s++ = KS_SPECIAL;
6325 *s++ = KE_FILLER;
6326 }
6327#ifdef FEAT_GUI
6328 else if (c == CSI)
6329 {
6330 *s++ = CSI;
6331 *s++ = KS_EXTRA;
6332 *s++ = (int)KE_CSI;
6333 }
6334#endif
6335 else
6336 *s++ = c;
6337#ifdef FEAT_MBYTE
6338 }
6339#endif
6340 return s;
6341}
6342
6343/*
6344 * move cursor to start of line
6345 * if flags & BL_WHITE move to first non-white
6346 * if flags & BL_SOL move to first non-white if startofline is set,
6347 * otherwise keep "curswant" column
6348 * if flags & BL_FIX don't leave the cursor on a NUL.
6349 */
6350 void
6351beginline(flags)
6352 int flags;
6353{
6354 if ((flags & BL_SOL) && !p_sol)
6355 coladvance(curwin->w_curswant);
6356 else
6357 {
6358 curwin->w_cursor.col = 0;
6359#ifdef FEAT_VIRTUALEDIT
6360 curwin->w_cursor.coladd = 0;
6361#endif
6362
6363 if (flags & (BL_WHITE | BL_SOL))
6364 {
6365 char_u *ptr;
6366
6367 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6368 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6369 ++curwin->w_cursor.col;
6370 }
6371 curwin->w_set_curswant = TRUE;
6372 }
6373}
6374
6375/*
6376 * oneright oneleft cursor_down cursor_up
6377 *
6378 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006379 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 * Return OK when successful, FAIL when we hit a line of file boundary.
6381 */
6382
6383 int
6384oneright()
6385{
6386 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388
6389#ifdef FEAT_VIRTUALEDIT
6390 if (virtual_active())
6391 {
6392 pos_T prevpos = curwin->w_cursor;
6393
6394 /* Adjust for multi-wide char (excluding TAB) */
6395 ptr = ml_get_cursor();
6396 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006397# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006399# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006401# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 ))
6403 ? ptr2cells(ptr) : 1));
6404 curwin->w_set_curswant = TRUE;
6405 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6406 return (prevpos.col != curwin->w_cursor.col
6407 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
6408 }
6409#endif
6410
6411 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006412 if (*ptr == NUL)
6413 return FAIL; /* already at the very end */
6414
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006416 if (has_mbyte)
6417 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 else
6419#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006420 l = 1;
6421
6422 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6423 * contains "onemore". */
6424 if (ptr[l] == NUL
6425#ifdef FEAT_VIRTUALEDIT
6426 && (ve_flags & VE_ONEMORE) == 0
6427#endif
6428 )
6429 return FAIL;
6430 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431
6432 curwin->w_set_curswant = TRUE;
6433 return OK;
6434}
6435
6436 int
6437oneleft()
6438{
6439#ifdef FEAT_VIRTUALEDIT
6440 if (virtual_active())
6441 {
6442 int width;
6443 int v = getviscol();
6444
6445 if (v == 0)
6446 return FAIL;
6447
6448# ifdef FEAT_LINEBREAK
6449 /* We might get stuck on 'showbreak', skip over it. */
6450 width = 1;
6451 for (;;)
6452 {
6453 coladvance(v - width);
6454 /* getviscol() is slow, skip it when 'showbreak' is empty and
6455 * there are no multi-byte characters */
6456 if ((*p_sbr == NUL
6457# ifdef FEAT_MBYTE
6458 && !has_mbyte
6459# endif
6460 ) || getviscol() < v)
6461 break;
6462 ++width;
6463 }
6464# else
6465 coladvance(v - 1);
6466# endif
6467
6468 if (curwin->w_cursor.coladd == 1)
6469 {
6470 char_u *ptr;
6471
6472 /* Adjust for multi-wide char (not a TAB) */
6473 ptr = ml_get_cursor();
6474 if (*ptr != TAB && vim_isprintc(
6475# ifdef FEAT_MBYTE
6476 (*mb_ptr2char)(ptr)
6477# else
6478 *ptr
6479# endif
6480 ) && ptr2cells(ptr) > 1)
6481 curwin->w_cursor.coladd = 0;
6482 }
6483
6484 curwin->w_set_curswant = TRUE;
6485 return OK;
6486 }
6487#endif
6488
6489 if (curwin->w_cursor.col == 0)
6490 return FAIL;
6491
6492 curwin->w_set_curswant = TRUE;
6493 --curwin->w_cursor.col;
6494
6495#ifdef FEAT_MBYTE
6496 /* if the character on the left of the current cursor is a multi-byte
6497 * character, move to its first byte */
6498 if (has_mbyte)
6499 mb_adjust_cursor();
6500#endif
6501 return OK;
6502}
6503
6504 int
6505cursor_up(n, upd_topline)
6506 long n;
6507 int upd_topline; /* When TRUE: update topline */
6508{
6509 linenr_T lnum;
6510
6511 if (n > 0)
6512 {
6513 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006514 /* This fails if the cursor is already in the first line or the count
6515 * is larger than the line number and '-' is in 'cpoptions' */
6516 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 return FAIL;
6518 if (n >= lnum)
6519 lnum = 1;
6520 else
6521#ifdef FEAT_FOLDING
6522 if (hasAnyFolding(curwin))
6523 {
6524 /*
6525 * Count each sequence of folded lines as one logical line.
6526 */
6527 /* go to the the start of the current fold */
6528 (void)hasFolding(lnum, &lnum, NULL);
6529
6530 while (n--)
6531 {
6532 /* move up one line */
6533 --lnum;
6534 if (lnum <= 1)
6535 break;
6536 /* If we entered a fold, move to the beginning, unless in
6537 * Insert mode or when 'foldopen' contains "all": it will open
6538 * in a moment. */
6539 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6540 (void)hasFolding(lnum, &lnum, NULL);
6541 }
6542 if (lnum < 1)
6543 lnum = 1;
6544 }
6545 else
6546#endif
6547 lnum -= n;
6548 curwin->w_cursor.lnum = lnum;
6549 }
6550
6551 /* try to advance to the column we want to be at */
6552 coladvance(curwin->w_curswant);
6553
6554 if (upd_topline)
6555 update_topline(); /* make sure curwin->w_topline is valid */
6556
6557 return OK;
6558}
6559
6560/*
6561 * Cursor down a number of logical lines.
6562 */
6563 int
6564cursor_down(n, upd_topline)
6565 long n;
6566 int upd_topline; /* When TRUE: update topline */
6567{
6568 linenr_T lnum;
6569
6570 if (n > 0)
6571 {
6572 lnum = curwin->w_cursor.lnum;
6573#ifdef FEAT_FOLDING
6574 /* Move to last line of fold, will fail if it's the end-of-file. */
6575 (void)hasFolding(lnum, NULL, &lnum);
6576#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006577 /* This fails if the cursor is already in the last line or would move
6578 * beyound the last line and '-' is in 'cpoptions' */
6579 if (lnum >= curbuf->b_ml.ml_line_count
6580 || (lnum + n > curbuf->b_ml.ml_line_count
6581 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 return FAIL;
6583 if (lnum + n >= curbuf->b_ml.ml_line_count)
6584 lnum = curbuf->b_ml.ml_line_count;
6585 else
6586#ifdef FEAT_FOLDING
6587 if (hasAnyFolding(curwin))
6588 {
6589 linenr_T last;
6590
6591 /* count each sequence of folded lines as one logical line */
6592 while (n--)
6593 {
6594 if (hasFolding(lnum, NULL, &last))
6595 lnum = last + 1;
6596 else
6597 ++lnum;
6598 if (lnum >= curbuf->b_ml.ml_line_count)
6599 break;
6600 }
6601 if (lnum > curbuf->b_ml.ml_line_count)
6602 lnum = curbuf->b_ml.ml_line_count;
6603 }
6604 else
6605#endif
6606 lnum += n;
6607 curwin->w_cursor.lnum = lnum;
6608 }
6609
6610 /* try to advance to the column we want to be at */
6611 coladvance(curwin->w_curswant);
6612
6613 if (upd_topline)
6614 update_topline(); /* make sure curwin->w_topline is valid */
6615
6616 return OK;
6617}
6618
6619/*
6620 * Stuff the last inserted text in the read buffer.
6621 * Last_insert actually is a copy of the redo buffer, so we
6622 * first have to remove the command.
6623 */
6624 int
6625stuff_inserted(c, count, no_esc)
6626 int c; /* Command character to be inserted */
6627 long count; /* Repeat this many times */
6628 int no_esc; /* Don't add an ESC at the end */
6629{
6630 char_u *esc_ptr;
6631 char_u *ptr;
6632 char_u *last_ptr;
6633 char_u last = NUL;
6634
6635 ptr = get_last_insert();
6636 if (ptr == NULL)
6637 {
6638 EMSG(_(e_noinstext));
6639 return FAIL;
6640 }
6641
6642 /* may want to stuff the command character, to start Insert mode */
6643 if (c != NUL)
6644 stuffcharReadbuff(c);
6645 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
6646 *esc_ptr = NUL; /* remove the ESC */
6647
6648 /* when the last char is either "0" or "^" it will be quoted if no ESC
6649 * comes after it OR if it will inserted more than once and "ptr"
6650 * starts with ^D. -- Acevedo
6651 */
6652 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
6653 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
6654 && (no_esc || (*ptr == Ctrl_D && count > 1)))
6655 {
6656 last = *last_ptr;
6657 *last_ptr = NUL;
6658 }
6659
6660 do
6661 {
6662 stuffReadbuff(ptr);
6663 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6664 if (last)
6665 stuffReadbuff((char_u *)(last == '0'
6666 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
6667 : IF_EB("\026^", CTRL_V_STR "^")));
6668 }
6669 while (--count > 0);
6670
6671 if (last)
6672 *last_ptr = last;
6673
6674 if (esc_ptr != NULL)
6675 *esc_ptr = ESC; /* put the ESC back */
6676
6677 /* may want to stuff a trailing ESC, to get out of Insert mode */
6678 if (!no_esc)
6679 stuffcharReadbuff(ESC);
6680
6681 return OK;
6682}
6683
6684 char_u *
6685get_last_insert()
6686{
6687 if (last_insert == NULL)
6688 return NULL;
6689 return last_insert + last_insert_skip;
6690}
6691
6692/*
6693 * Get last inserted string, and remove trailing <Esc>.
6694 * Returns pointer to allocated memory (must be freed) or NULL.
6695 */
6696 char_u *
6697get_last_insert_save()
6698{
6699 char_u *s;
6700 int len;
6701
6702 if (last_insert == NULL)
6703 return NULL;
6704 s = vim_strsave(last_insert + last_insert_skip);
6705 if (s != NULL)
6706 {
6707 len = (int)STRLEN(s);
6708 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
6709 s[len - 1] = NUL;
6710 }
6711 return s;
6712}
6713
6714/*
6715 * Check the word in front of the cursor for an abbreviation.
6716 * Called when the non-id character "c" has been entered.
6717 * When an abbreviation is recognized it is removed from the text and
6718 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
6719 */
6720 static int
6721echeck_abbr(c)
6722 int c;
6723{
6724 /* Don't check for abbreviation in paste mode, when disabled and just
6725 * after moving around with cursor keys. */
6726 if (p_paste || no_abbr || arrow_used)
6727 return FALSE;
6728
6729 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
6730 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
6731}
6732
6733/*
6734 * replace-stack functions
6735 *
6736 * When replacing characters, the replaced characters are remembered for each
6737 * new character. This is used to re-insert the old text when backspacing.
6738 *
6739 * There is a NUL headed list of characters for each character that is
6740 * currently in the file after the insertion point. When BS is used, one NUL
6741 * headed list is put back for the deleted character.
6742 *
6743 * For a newline, there are two NUL headed lists. One contains the characters
6744 * that the NL replaced. The extra one stores the characters after the cursor
6745 * that were deleted (always white space).
6746 *
6747 * Replace_offset is normally 0, in which case replace_push will add a new
6748 * character at the end of the stack. If replace_offset is not 0, that many
6749 * characters will be left on the stack above the newly inserted character.
6750 */
6751
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00006752static char_u *replace_stack = NULL;
6753static long replace_stack_nr = 0; /* next entry in replace stack */
6754static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755
6756 void
6757replace_push(c)
6758 int c; /* character that is replaced (NUL is none) */
6759{
6760 char_u *p;
6761
6762 if (replace_stack_nr < replace_offset) /* nothing to do */
6763 return;
6764 if (replace_stack_len <= replace_stack_nr)
6765 {
6766 replace_stack_len += 50;
6767 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
6768 if (p == NULL) /* out of memory */
6769 {
6770 replace_stack_len -= 50;
6771 return;
6772 }
6773 if (replace_stack != NULL)
6774 {
6775 mch_memmove(p, replace_stack,
6776 (size_t)(replace_stack_nr * sizeof(char_u)));
6777 vim_free(replace_stack);
6778 }
6779 replace_stack = p;
6780 }
6781 p = replace_stack + replace_stack_nr - replace_offset;
6782 if (replace_offset)
6783 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
6784 *p = c;
6785 ++replace_stack_nr;
6786}
6787
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006788#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789/*
6790 * call replace_push(c) with replace_offset set to the first NUL.
6791 */
6792 static void
6793replace_push_off(c)
6794 int c;
6795{
6796 char_u *p;
6797
6798 p = replace_stack + replace_stack_nr;
6799 for (replace_offset = 1; replace_offset < replace_stack_nr;
6800 ++replace_offset)
6801 if (*--p == NUL)
6802 break;
6803 replace_push(c);
6804 replace_offset = 0;
6805}
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006806#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807
6808/*
6809 * Pop one item from the replace stack.
6810 * return -1 if stack empty
6811 * return replaced character or NUL otherwise
6812 */
6813 static int
6814replace_pop()
6815{
6816 if (replace_stack_nr == 0)
6817 return -1;
6818 return (int)replace_stack[--replace_stack_nr];
6819}
6820
6821/*
6822 * Join the top two items on the replace stack. This removes to "off"'th NUL
6823 * encountered.
6824 */
6825 static void
6826replace_join(off)
6827 int off; /* offset for which NUL to remove */
6828{
6829 int i;
6830
6831 for (i = replace_stack_nr; --i >= 0; )
6832 if (replace_stack[i] == NUL && off-- <= 0)
6833 {
6834 --replace_stack_nr;
6835 mch_memmove(replace_stack + i, replace_stack + i + 1,
6836 (size_t)(replace_stack_nr - i));
6837 return;
6838 }
6839}
6840
6841/*
6842 * Pop bytes from the replace stack until a NUL is found, and insert them
6843 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
6844 */
6845 static void
6846replace_pop_ins()
6847{
6848 int cc;
6849 int oldState = State;
6850
6851 State = NORMAL; /* don't want REPLACE here */
6852 while ((cc = replace_pop()) > 0)
6853 {
6854#ifdef FEAT_MBYTE
6855 mb_replace_pop_ins(cc);
6856#else
6857 ins_char(cc);
6858#endif
6859 dec_cursor();
6860 }
6861 State = oldState;
6862}
6863
6864#ifdef FEAT_MBYTE
6865/*
6866 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
6867 * indicates a multi-byte char, pop the other bytes too.
6868 */
6869 static void
6870mb_replace_pop_ins(cc)
6871 int cc;
6872{
6873 int n;
6874 char_u buf[MB_MAXBYTES];
6875 int i;
6876 int c;
6877
6878 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
6879 {
6880 buf[0] = cc;
6881 for (i = 1; i < n; ++i)
6882 buf[i] = replace_pop();
6883 ins_bytes_len(buf, n);
6884 }
6885 else
6886 ins_char(cc);
6887
6888 if (enc_utf8)
6889 /* Handle composing chars. */
6890 for (;;)
6891 {
6892 c = replace_pop();
6893 if (c == -1) /* stack empty */
6894 break;
6895 if ((n = MB_BYTE2LEN(c)) == 1)
6896 {
6897 /* Not a multi-byte char, put it back. */
6898 replace_push(c);
6899 break;
6900 }
6901 else
6902 {
6903 buf[0] = c;
6904 for (i = 1; i < n; ++i)
6905 buf[i] = replace_pop();
6906 if (utf_iscomposing(utf_ptr2char(buf)))
6907 ins_bytes_len(buf, n);
6908 else
6909 {
6910 /* Not a composing char, put it back. */
6911 for (i = n - 1; i >= 0; --i)
6912 replace_push(buf[i]);
6913 break;
6914 }
6915 }
6916 }
6917}
6918#endif
6919
6920/*
6921 * make the replace stack empty
6922 * (called when exiting replace mode)
6923 */
6924 static void
6925replace_flush()
6926{
6927 vim_free(replace_stack);
6928 replace_stack = NULL;
6929 replace_stack_len = 0;
6930 replace_stack_nr = 0;
6931}
6932
6933/*
6934 * Handle doing a BS for one character.
6935 * cc < 0: replace stack empty, just move cursor
6936 * cc == 0: character was inserted, delete it
6937 * cc > 0: character was replaced, put cc (first byte of original char) back
6938 * and check for more characters to be put back
6939 */
6940 static void
6941replace_do_bs()
6942{
6943 int cc;
6944#ifdef FEAT_VREPLACE
6945 int orig_len = 0;
6946 int ins_len;
6947 int orig_vcols = 0;
6948 colnr_T start_vcol;
6949 char_u *p;
6950 int i;
6951 int vcol;
6952#endif
6953
6954 cc = replace_pop();
6955 if (cc > 0)
6956 {
6957#ifdef FEAT_VREPLACE
6958 if (State & VREPLACE_FLAG)
6959 {
6960 /* Get the number of screen cells used by the character we are
6961 * going to delete. */
6962 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
6963 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
6964 }
6965#endif
6966#ifdef FEAT_MBYTE
6967 if (has_mbyte)
6968 {
6969 del_char(FALSE);
6970# ifdef FEAT_VREPLACE
6971 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006972 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973# endif
6974 replace_push(cc);
6975 }
6976 else
6977#endif
6978 {
6979 pchar_cursor(cc);
6980#ifdef FEAT_VREPLACE
6981 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006982 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983#endif
6984 }
6985 replace_pop_ins();
6986
6987#ifdef FEAT_VREPLACE
6988 if (State & VREPLACE_FLAG)
6989 {
6990 /* Get the number of screen cells used by the inserted characters */
6991 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006992 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 vcol = start_vcol;
6994 for (i = 0; i < ins_len; ++i)
6995 {
6996 vcol += chartabsize(p + i, vcol);
6997#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006998 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999#endif
7000 }
7001 vcol -= start_vcol;
7002
7003 /* Delete spaces that were inserted after the cursor to keep the
7004 * text aligned. */
7005 curwin->w_cursor.col += ins_len;
7006 while (vcol > orig_vcols && gchar_cursor() == ' ')
7007 {
7008 del_char(FALSE);
7009 ++orig_vcols;
7010 }
7011 curwin->w_cursor.col -= ins_len;
7012 }
7013#endif
7014
7015 /* mark the buffer as changed and prepare for displaying */
7016 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7017 }
7018 else if (cc == 0)
7019 (void)del_char(FALSE);
7020}
7021
7022#ifdef FEAT_CINDENT
7023/*
7024 * Return TRUE if C-indenting is on.
7025 */
7026 static int
7027cindent_on()
7028{
7029 return (!p_paste && (curbuf->b_p_cin
7030# ifdef FEAT_EVAL
7031 || *curbuf->b_p_inde != NUL
7032# endif
7033 ));
7034}
7035#endif
7036
7037#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7038/*
7039 * Re-indent the current line, based on the current contents of it and the
7040 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7041 * confused what all the part that handles Control-T is doing that I'm not.
7042 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7043 */
7044
7045 void
7046fixthisline(get_the_indent)
7047 int (*get_the_indent) __ARGS((void));
7048{
7049 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
7050 if (linewhite(curwin->w_cursor.lnum))
7051 did_ai = TRUE; /* delete the indent if the line stays empty */
7052}
7053
7054 void
7055fix_indent()
7056{
7057 if (p_paste)
7058 return;
7059# ifdef FEAT_LISP
7060 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7061 fixthisline(get_lisp_indent);
7062# endif
7063# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7064 else
7065# endif
7066# ifdef FEAT_CINDENT
7067 if (cindent_on())
7068 do_c_expr_indent();
7069# endif
7070}
7071
7072#endif
7073
7074#ifdef FEAT_CINDENT
7075/*
7076 * return TRUE if 'cinkeys' contains the key "keytyped",
7077 * when == '*': Only if key is preceded with '*' (indent before insert)
7078 * when == '!': Only if key is prededed with '!' (don't insert)
7079 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7080 *
7081 * "keytyped" can have a few special values:
7082 * KEY_OPEN_FORW
7083 * KEY_OPEN_BACK
7084 * KEY_COMPLETE just finished completion.
7085 *
7086 * If line_is_empty is TRUE accept keys with '0' before them.
7087 */
7088 int
7089in_cinkeys(keytyped, when, line_is_empty)
7090 int keytyped;
7091 int when;
7092 int line_is_empty;
7093{
7094 char_u *look;
7095 int try_match;
7096 int try_match_word;
7097 char_u *p;
7098 char_u *line;
7099 int icase;
7100 int i;
7101
7102#ifdef FEAT_EVAL
7103 if (*curbuf->b_p_inde != NUL)
7104 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7105 else
7106#endif
7107 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7108 while (*look)
7109 {
7110 /*
7111 * Find out if we want to try a match with this key, depending on
7112 * 'when' and a '*' or '!' before the key.
7113 */
7114 switch (when)
7115 {
7116 case '*': try_match = (*look == '*'); break;
7117 case '!': try_match = (*look == '!'); break;
7118 default: try_match = (*look != '*'); break;
7119 }
7120 if (*look == '*' || *look == '!')
7121 ++look;
7122
7123 /*
7124 * If there is a '0', only accept a match if the line is empty.
7125 * But may still match when typing last char of a word.
7126 */
7127 if (*look == '0')
7128 {
7129 try_match_word = try_match;
7130 if (!line_is_empty)
7131 try_match = FALSE;
7132 ++look;
7133 }
7134 else
7135 try_match_word = FALSE;
7136
7137 /*
7138 * does it look like a control character?
7139 */
7140 if (*look == '^'
7141#ifdef EBCDIC
7142 && (Ctrl_chr(look[1]) != 0)
7143#else
7144 && look[1] >= '?' && look[1] <= '_'
7145#endif
7146 )
7147 {
7148 if (try_match && keytyped == Ctrl_chr(look[1]))
7149 return TRUE;
7150 look += 2;
7151 }
7152 /*
7153 * 'o' means "o" command, open forward.
7154 * 'O' means "O" command, open backward.
7155 */
7156 else if (*look == 'o')
7157 {
7158 if (try_match && keytyped == KEY_OPEN_FORW)
7159 return TRUE;
7160 ++look;
7161 }
7162 else if (*look == 'O')
7163 {
7164 if (try_match && keytyped == KEY_OPEN_BACK)
7165 return TRUE;
7166 ++look;
7167 }
7168
7169 /*
7170 * 'e' means to check for "else" at start of line and just before the
7171 * cursor.
7172 */
7173 else if (*look == 'e')
7174 {
7175 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7176 {
7177 p = ml_get_curline();
7178 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7179 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7180 return TRUE;
7181 }
7182 ++look;
7183 }
7184
7185 /*
7186 * ':' only causes an indent if it is at the end of a label or case
7187 * statement, or when it was before typing the ':' (to fix
7188 * class::method for C++).
7189 */
7190 else if (*look == ':')
7191 {
7192 if (try_match && keytyped == ':')
7193 {
7194 p = ml_get_curline();
7195 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
7196 return TRUE;
7197 if (curwin->w_cursor.col > 2
7198 && p[curwin->w_cursor.col - 1] == ':'
7199 && p[curwin->w_cursor.col - 2] == ':')
7200 {
7201 p[curwin->w_cursor.col - 1] = ' ';
7202 i = (cin_iscase(p) || cin_isscopedecl(p)
7203 || cin_islabel(30));
7204 p = ml_get_curline();
7205 p[curwin->w_cursor.col - 1] = ':';
7206 if (i)
7207 return TRUE;
7208 }
7209 }
7210 ++look;
7211 }
7212
7213
7214 /*
7215 * Is it a key in <>, maybe?
7216 */
7217 else if (*look == '<')
7218 {
7219 if (try_match)
7220 {
7221 /*
7222 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7223 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7224 * >, *, : and ! keys if they really really want to.
7225 */
7226 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
7227 && keytyped == look[1])
7228 return TRUE;
7229
7230 if (keytyped == get_special_key_code(look + 1))
7231 return TRUE;
7232 }
7233 while (*look && *look != '>')
7234 look++;
7235 while (*look == '>')
7236 look++;
7237 }
7238
7239 /*
7240 * Is it a word: "=word"?
7241 */
7242 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
7243 {
7244 ++look;
7245 if (*look == '~')
7246 {
7247 icase = TRUE;
7248 ++look;
7249 }
7250 else
7251 icase = FALSE;
7252 p = vim_strchr(look, ',');
7253 if (p == NULL)
7254 p = look + STRLEN(look);
7255 if ((try_match || try_match_word)
7256 && curwin->w_cursor.col >= (colnr_T)(p - look))
7257 {
7258 int match = FALSE;
7259
7260#ifdef FEAT_INS_EXPAND
7261 if (keytyped == KEY_COMPLETE)
7262 {
7263 char_u *s;
7264
7265 /* Just completed a word, check if it starts with "look".
7266 * search back for the start of a word. */
7267 line = ml_get_curline();
7268# ifdef FEAT_MBYTE
7269 if (has_mbyte)
7270 {
7271 char_u *n;
7272
7273 for (s = line + curwin->w_cursor.col; s > line; s = n)
7274 {
7275 n = mb_prevptr(line, s);
7276 if (!vim_iswordp(n))
7277 break;
7278 }
7279 }
7280 else
7281# endif
7282 for (s = line + curwin->w_cursor.col; s > line; --s)
7283 if (!vim_iswordc(s[-1]))
7284 break;
7285 if (s + (p - look) <= line + curwin->w_cursor.col
7286 && (icase
7287 ? MB_STRNICMP(s, look, p - look)
7288 : STRNCMP(s, look, p - look)) == 0)
7289 match = TRUE;
7290 }
7291 else
7292#endif
7293 /* TODO: multi-byte */
7294 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7295 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7296 {
7297 line = ml_get_cursor();
7298 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7299 || !vim_iswordc(line[-(p - look) - 1]))
7300 && (icase
7301 ? MB_STRNICMP(line - (p - look), look, p - look)
7302 : STRNCMP(line - (p - look), look, p - look))
7303 == 0)
7304 match = TRUE;
7305 }
7306 if (match && try_match_word && !try_match)
7307 {
7308 /* "0=word": Check if there are only blanks before the
7309 * word. */
7310 line = ml_get_curline();
7311 if ((int)(skipwhite(line) - line) !=
7312 (int)(curwin->w_cursor.col - (p - look)))
7313 match = FALSE;
7314 }
7315 if (match)
7316 return TRUE;
7317 }
7318 look = p;
7319 }
7320
7321 /*
7322 * ok, it's a boring generic character.
7323 */
7324 else
7325 {
7326 if (try_match && *look == keytyped)
7327 return TRUE;
7328 ++look;
7329 }
7330
7331 /*
7332 * Skip over ", ".
7333 */
7334 look = skip_to_option_part(look);
7335 }
7336 return FALSE;
7337}
7338#endif /* FEAT_CINDENT */
7339
7340#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7341/*
7342 * Map Hebrew keyboard when in hkmap mode.
7343 */
7344 int
7345hkmap(c)
7346 int c;
7347{
7348 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7349 {
7350 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7351 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7352 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7353 static char_u map[26] =
7354 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7355 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7356 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7357 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7358 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7359 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7360 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7361 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7362 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7363
7364 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7365 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7366 /* '-1'='sofit' */
7367 else if (c == 'x')
7368 return 'X';
7369 else if (c == 'q')
7370 return '\''; /* {geresh}={'} */
7371 else if (c == 246)
7372 return ' '; /* \"o --> ' ' for a german keyboard */
7373 else if (c == 228)
7374 return ' '; /* \"a --> ' ' -- / -- */
7375 else if (c == 252)
7376 return ' '; /* \"u --> ' ' -- / -- */
7377#ifdef EBCDIC
7378 else if (islower(c))
7379#else
7380 /* NOTE: islower() does not do the right thing for us on Linux so we
7381 * do this the same was as 5.7 and previous, so it works correctly on
7382 * all systems. Specifically, the e.g. Delete and Arrow keys are
7383 * munged and won't work if e.g. searching for Hebrew text.
7384 */
7385 else if (c >= 'a' && c <= 'z')
7386#endif
7387 return (int)(map[CharOrdLow(c)] + p_aleph);
7388 else
7389 return c;
7390 }
7391 else
7392 {
7393 switch (c)
7394 {
7395 case '`': return ';';
7396 case '/': return '.';
7397 case '\'': return ',';
7398 case 'q': return '/';
7399 case 'w': return '\'';
7400
7401 /* Hebrew letters - set offset from 'a' */
7402 case ',': c = '{'; break;
7403 case '.': c = 'v'; break;
7404 case ';': c = 't'; break;
7405 default: {
7406 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7407
7408#ifdef EBCDIC
7409 /* see note about islower() above */
7410 if (!islower(c))
7411#else
7412 if (c < 'a' || c > 'z')
7413#endif
7414 return c;
7415 c = str[CharOrdLow(c)];
7416 break;
7417 }
7418 }
7419
7420 return (int)(CharOrdLow(c) + p_aleph);
7421 }
7422}
7423#endif
7424
7425 static void
7426ins_reg()
7427{
7428 int need_redraw = FALSE;
7429 int regname;
7430 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007431#ifdef FEAT_VISUAL
7432 int vis_active = VIsual_active;
7433#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434
7435 /*
7436 * If we are going to wait for a character, show a '"'.
7437 */
7438 pc_status = PC_STATUS_UNSET;
7439 if (redrawing() && !char_avail())
7440 {
7441 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007442 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443
7444 edit_putchar('"', TRUE);
7445#ifdef FEAT_CMDL_INFO
7446 add_to_showcmd_c(Ctrl_R);
7447#endif
7448 }
7449
7450#ifdef USE_ON_FLY_SCROLL
7451 dont_scroll = TRUE; /* disallow scrolling here */
7452#endif
7453
7454 /*
7455 * Don't map the register name. This also prevents the mode message to be
7456 * deleted when ESC is hit.
7457 */
7458 ++no_mapping;
7459 regname = safe_vgetc();
7460#ifdef FEAT_LANGMAP
7461 LANGMAP_ADJUST(regname, TRUE);
7462#endif
7463 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
7464 {
7465 /* Get a third key for literal register insertion */
7466 literally = regname;
7467#ifdef FEAT_CMDL_INFO
7468 add_to_showcmd_c(literally);
7469#endif
7470 regname = safe_vgetc();
7471#ifdef FEAT_LANGMAP
7472 LANGMAP_ADJUST(regname, TRUE);
7473#endif
7474 }
7475 --no_mapping;
7476
7477#ifdef FEAT_EVAL
7478 /*
7479 * Don't call u_sync() while getting the expression,
7480 * evaluating it or giving an error message for it!
7481 */
7482 ++no_u_sync;
7483 if (regname == '=')
7484 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007485# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007487# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007489# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 /* Restore the Input Method. */
7491 if (im_on)
7492 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007493# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00007495 if (regname == NUL || !valid_yank_reg(regname, FALSE))
7496 {
7497 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 else
7501 {
7502#endif
7503 if (literally == Ctrl_O || literally == Ctrl_P)
7504 {
7505 /* Append the command to the redo buffer. */
7506 AppendCharToRedobuff(Ctrl_R);
7507 AppendCharToRedobuff(literally);
7508 AppendCharToRedobuff(regname);
7509
7510 do_put(regname, BACKWARD, 1L,
7511 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
7512 }
7513 else if (insert_reg(regname, literally) == FAIL)
7514 {
7515 vim_beep();
7516 need_redraw = TRUE; /* remove the '"' */
7517 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007518 else if (stop_insert_mode)
7519 /* When the '=' register was used and a function was invoked that
7520 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7521 * insert anything, need to remove the '"' */
7522 need_redraw = TRUE;
7523
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524#ifdef FEAT_EVAL
7525 }
7526 --no_u_sync;
7527#endif
7528#ifdef FEAT_CMDL_INFO
7529 clear_showcmd();
7530#endif
7531
7532 /* If the inserted register is empty, we need to remove the '"' */
7533 if (need_redraw || stuff_empty())
7534 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007535
7536#ifdef FEAT_VISUAL
7537 /* Disallow starting Visual mode here, would get a weird mode. */
7538 if (!vis_active && VIsual_active)
7539 end_visual_mode();
7540#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541}
7542
7543/*
7544 * CTRL-G commands in Insert mode.
7545 */
7546 static void
7547ins_ctrl_g()
7548{
7549 int c;
7550
7551#ifdef FEAT_INS_EXPAND
7552 /* Right after CTRL-X the cursor will be after the ruler. */
7553 setcursor();
7554#endif
7555
7556 /*
7557 * Don't map the second key. This also prevents the mode message to be
7558 * deleted when ESC is hit.
7559 */
7560 ++no_mapping;
7561 c = safe_vgetc();
7562 --no_mapping;
7563 switch (c)
7564 {
7565 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7566 case K_UP:
7567 case Ctrl_K:
7568 case 'k': ins_up(TRUE);
7569 break;
7570
7571 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7572 case K_DOWN:
7573 case Ctrl_J:
7574 case 'j': ins_down(TRUE);
7575 break;
7576
7577 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00007578 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007580
7581 /* Need to reset Insstart, esp. because a BS that joins
7582 * aline to the previous one must save for undo. */
7583 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 break;
7585
7586 /* Unknown CTRL-G command, reserved for future expansion. */
7587 default: vim_beep();
7588 }
7589}
7590
7591/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007592 * CTRL-^ in Insert mode.
7593 */
7594 static void
7595ins_ctrl_hat()
7596{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00007597 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007598 {
7599 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7600 if (State & LANGMAP)
7601 {
7602 curbuf->b_p_iminsert = B_IMODE_NONE;
7603 State &= ~LANGMAP;
7604 }
7605 else
7606 {
7607 curbuf->b_p_iminsert = B_IMODE_LMAP;
7608 State |= LANGMAP;
7609#ifdef USE_IM_CONTROL
7610 im_set_active(FALSE);
7611#endif
7612 }
7613 }
7614#ifdef USE_IM_CONTROL
7615 else
7616 {
7617 /* There are no ":lmap" mappings, toggle IM */
7618 if (im_get_status())
7619 {
7620 curbuf->b_p_iminsert = B_IMODE_NONE;
7621 im_set_active(FALSE);
7622 }
7623 else
7624 {
7625 curbuf->b_p_iminsert = B_IMODE_IM;
7626 State &= ~LANGMAP;
7627 im_set_active(TRUE);
7628 }
7629 }
7630#endif
7631 set_iminsert_global();
7632 showmode();
7633#ifdef FEAT_GUI
7634 /* may show different cursor shape or color */
7635 if (gui.in_use)
7636 gui_update_cursor(TRUE, FALSE);
7637#endif
7638#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7639 /* Show/unshow value of 'keymap' in status lines. */
7640 status_redraw_curbuf();
7641#endif
7642}
7643
7644/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 * Handle ESC in insert mode.
7646 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7647 * insert.
7648 */
7649 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00007650ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 long *count;
7652 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00007653 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654{
7655 int temp;
7656 static int disabled_redraw = FALSE;
7657
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007658#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007659 check_spell_redraw();
7660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661#if defined(FEAT_HANGULIN)
7662# if defined(ESC_CHG_TO_ENG_MODE)
7663 hangul_input_state_set(0);
7664# endif
7665 if (composing_hangul)
7666 {
7667 push_raw_key(composing_hangul_buffer, 2);
7668 composing_hangul = 0;
7669 }
7670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671
7672 temp = curwin->w_cursor.col;
7673 if (disabled_redraw)
7674 {
7675 --RedrawingDisabled;
7676 disabled_redraw = FALSE;
7677 }
7678 if (!arrow_used)
7679 {
7680 /*
7681 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00007682 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7683 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 */
7685 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00007686 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687
7688 /*
7689 * Repeating insert may take a long time. Check for
7690 * interrupt now and then.
7691 */
7692 if (*count > 0)
7693 {
7694 line_breakcheck();
7695 if (got_int)
7696 *count = 0;
7697 }
7698
7699 if (--*count > 0) /* repeat what was typed */
7700 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007701 /* Vi repeats the insert without replacing characters. */
7702 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
7703 State &= ~REPLACE_FLAG;
7704
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 (void)start_redo_ins();
7706 if (cmdchar == 'r' || cmdchar == 'v')
7707 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
7708 ++RedrawingDisabled;
7709 disabled_redraw = TRUE;
7710 return FALSE; /* repeat the insert */
7711 }
7712 stop_insert(&curwin->w_cursor, TRUE);
7713 undisplay_dollar();
7714 }
7715
7716 /* When an autoindent was removed, curswant stays after the
7717 * indent */
7718 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
7719 curwin->w_set_curswant = TRUE;
7720
7721 /* Remember the last Insert position in the '^ mark. */
7722 if (!cmdmod.keepjumps)
7723 curbuf->b_last_insert = curwin->w_cursor;
7724
7725 /*
7726 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00007727 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00007729 if (!nomove
7730 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731#ifdef FEAT_VIRTUALEDIT
7732 || curwin->w_cursor.coladd > 0
7733#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007734 )
7735 && (restart_edit == NUL
7736 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00007738 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007740 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741#ifdef FEAT_RIGHTLEFT
7742 && !revins_on
7743#endif
7744 )
7745 {
7746#ifdef FEAT_VIRTUALEDIT
7747 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
7748 {
7749 oneleft();
7750 if (restart_edit != NUL)
7751 ++curwin->w_cursor.coladd;
7752 }
7753 else
7754#endif
7755 {
7756 --curwin->w_cursor.col;
7757#ifdef FEAT_MBYTE
7758 /* Correct cursor for multi-byte character. */
7759 if (has_mbyte)
7760 mb_adjust_cursor();
7761#endif
7762 }
7763 }
7764
7765#ifdef USE_IM_CONTROL
7766 /* Disable IM to allow typing English directly for Normal mode commands.
7767 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
7768 * well). */
7769 if (!(State & LANGMAP))
7770 im_save_status(&curbuf->b_p_iminsert);
7771 im_set_active(FALSE);
7772#endif
7773
7774 State = NORMAL;
7775 /* need to position cursor again (e.g. when on a TAB ) */
7776 changed_cline_bef_curs();
7777
7778#ifdef FEAT_MOUSE
7779 setmouse();
7780#endif
7781#ifdef CURSOR_SHAPE
7782 ui_cursor_shape(); /* may show different cursor shape */
7783#endif
7784
7785 /*
7786 * When recording or for CTRL-O, need to display the new mode.
7787 * Otherwise remove the mode message.
7788 */
7789 if (Recording || restart_edit != NUL)
7790 showmode();
7791 else if (p_smd)
7792 MSG("");
7793
7794 return TRUE; /* exit Insert mode */
7795}
7796
7797#ifdef FEAT_RIGHTLEFT
7798/*
7799 * Toggle language: hkmap and revins_on.
7800 * Move to end of reverse inserted text.
7801 */
7802 static void
7803ins_ctrl_()
7804{
7805 if (revins_on && revins_chars && revins_scol >= 0)
7806 {
7807 while (gchar_cursor() != NUL && revins_chars--)
7808 ++curwin->w_cursor.col;
7809 }
7810 p_ri = !p_ri;
7811 revins_on = (State == INSERT && p_ri);
7812 if (revins_on)
7813 {
7814 revins_scol = curwin->w_cursor.col;
7815 revins_legal++;
7816 revins_chars = 0;
7817 undisplay_dollar();
7818 }
7819 else
7820 revins_scol = -1;
7821#ifdef FEAT_FKMAP
7822 if (p_altkeymap)
7823 {
7824 /*
7825 * to be consistent also for redo command, using '.'
7826 * set arrow_used to true and stop it - causing to redo
7827 * characters entered in one mode (normal/reverse insert).
7828 */
7829 arrow_used = TRUE;
7830 (void)stop_arrow();
7831 p_fkmap = curwin->w_p_rl ^ p_ri;
7832 if (p_fkmap && p_ri)
7833 State = INSERT;
7834 }
7835 else
7836#endif
7837 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
7838 showmode();
7839}
7840#endif
7841
7842#ifdef FEAT_VISUAL
7843/*
7844 * If 'keymodel' contains "startsel", may start selection.
7845 * Returns TRUE when a CTRL-O and other keys stuffed.
7846 */
7847 static int
7848ins_start_select(c)
7849 int c;
7850{
7851 if (km_startsel)
7852 switch (c)
7853 {
7854 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 case K_PAGEUP:
7857 case K_KPAGEUP:
7858 case K_PAGEDOWN:
7859 case K_KPAGEDOWN:
7860# ifdef MACOS
7861 case K_LEFT:
7862 case K_RIGHT:
7863 case K_UP:
7864 case K_DOWN:
7865 case K_END:
7866 case K_HOME:
7867# endif
7868 if (!(mod_mask & MOD_MASK_SHIFT))
7869 break;
7870 /* FALLTHROUGH */
7871 case K_S_LEFT:
7872 case K_S_RIGHT:
7873 case K_S_UP:
7874 case K_S_DOWN:
7875 case K_S_END:
7876 case K_S_HOME:
7877 /* Start selection right away, the cursor can move with
7878 * CTRL-O when beyond the end of the line. */
7879 start_selection();
7880
7881 /* Execute the key in (insert) Select mode. */
7882 stuffcharReadbuff(Ctrl_O);
7883 if (mod_mask)
7884 {
7885 char_u buf[4];
7886
7887 buf[0] = K_SPECIAL;
7888 buf[1] = KS_MODIFIER;
7889 buf[2] = mod_mask;
7890 buf[3] = NUL;
7891 stuffReadbuff(buf);
7892 }
7893 stuffcharReadbuff(c);
7894 return TRUE;
7895 }
7896 return FALSE;
7897}
7898#endif
7899
7900/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007901 * <Insert> key in Insert mode: toggle insert/remplace mode.
7902 */
7903 static void
7904ins_insert(replaceState)
7905 int replaceState;
7906{
7907#ifdef FEAT_FKMAP
7908 if (p_fkmap && p_ri)
7909 {
7910 beep_flush();
7911 EMSG(farsi_text_3); /* encoded in Farsi */
7912 return;
7913 }
7914#endif
7915
7916#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00007917# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007918 set_vim_var_string(VV_INSERTMODE,
7919 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007920# ifdef FEAT_VREPLACE
7921 replaceState == VREPLACE ? "v" :
7922# endif
7923 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00007924# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007925 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
7926#endif
7927 if (State & REPLACE_FLAG)
7928 State = INSERT | (State & LANGMAP);
7929 else
7930 State = replaceState | (State & LANGMAP);
7931 AppendCharToRedobuff(K_INS);
7932 showmode();
7933#ifdef CURSOR_SHAPE
7934 ui_cursor_shape(); /* may show different cursor shape */
7935#endif
7936}
7937
7938/*
7939 * Pressed CTRL-O in Insert mode.
7940 */
7941 static void
7942ins_ctrl_o()
7943{
7944#ifdef FEAT_VREPLACE
7945 if (State & VREPLACE_FLAG)
7946 restart_edit = 'V';
7947 else
7948#endif
7949 if (State & REPLACE_FLAG)
7950 restart_edit = 'R';
7951 else
7952 restart_edit = 'I';
7953#ifdef FEAT_VIRTUALEDIT
7954 if (virtual_active())
7955 ins_at_eol = FALSE; /* cursor always keeps its column */
7956 else
7957#endif
7958 ins_at_eol = (gchar_cursor() == NUL);
7959}
7960
7961/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 * If the cursor is on an indent, ^T/^D insert/delete one
7963 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
7964 * Always round the indent to 'shiftwith', this is compatible
7965 * with vi. But vi only supports ^T and ^D after an
7966 * autoindent, we support it everywhere.
7967 */
7968 static void
7969ins_shift(c, lastc)
7970 int c;
7971 int lastc;
7972{
7973 if (stop_arrow() == FAIL)
7974 return;
7975 AppendCharToRedobuff(c);
7976
7977 /*
7978 * 0^D and ^^D: remove all indent.
7979 */
7980 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
7981 {
7982 --curwin->w_cursor.col;
7983 (void)del_char(FALSE); /* delete the '^' or '0' */
7984 /* In Replace mode, restore the characters that '^' or '0' replaced. */
7985 if (State & REPLACE_FLAG)
7986 replace_pop_ins();
7987 if (lastc == '^')
7988 old_indent = get_indent(); /* remember curr. indent */
7989 change_indent(INDENT_SET, 0, TRUE, 0);
7990 }
7991 else
7992 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
7993
7994 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
7995 did_ai = FALSE;
7996#ifdef FEAT_SMARTINDENT
7997 did_si = FALSE;
7998 can_si = FALSE;
7999 can_si_back = FALSE;
8000#endif
8001#ifdef FEAT_CINDENT
8002 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8003#endif
8004}
8005
8006 static void
8007ins_del()
8008{
8009 int temp;
8010
8011 if (stop_arrow() == FAIL)
8012 return;
8013 if (gchar_cursor() == NUL) /* delete newline */
8014 {
8015 temp = curwin->w_cursor.col;
8016 if (!can_bs(BS_EOL) /* only if "eol" included */
8017 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
8018 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
8019 || do_join(FALSE) == FAIL)
8020 vim_beep();
8021 else
8022 curwin->w_cursor.col = temp;
8023 }
8024 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8025 vim_beep();
8026 did_ai = FALSE;
8027#ifdef FEAT_SMARTINDENT
8028 did_si = FALSE;
8029 can_si = FALSE;
8030 can_si_back = FALSE;
8031#endif
8032 AppendCharToRedobuff(K_DEL);
8033}
8034
8035/*
8036 * Handle Backspace, delete-word and delete-line in Insert mode.
8037 * Return TRUE when backspace was actually used.
8038 */
8039 static int
8040ins_bs(c, mode, inserted_space_p)
8041 int c;
8042 int mode;
8043 int *inserted_space_p;
8044{
8045 linenr_T lnum;
8046 int cc;
8047 int temp = 0; /* init for GCC */
8048 colnr_T mincol;
8049 int did_backspace = FALSE;
8050 int in_indent;
8051 int oldState;
8052#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008053 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054#endif
8055
8056 /*
8057 * can't delete anything in an empty file
8058 * can't backup past first character in buffer
8059 * can't backup past starting point unless 'backspace' > 1
8060 * can backup to a previous line if 'backspace' == 0
8061 */
8062 if ( bufempty()
8063 || (
8064#ifdef FEAT_RIGHTLEFT
8065 !revins_on &&
8066#endif
8067 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8068 || (!can_bs(BS_START)
8069 && (arrow_used
8070 || (curwin->w_cursor.lnum == Insstart.lnum
8071 && curwin->w_cursor.col <= Insstart.col)))
8072 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8073 && curwin->w_cursor.col <= ai_col)
8074 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8075 {
8076 vim_beep();
8077 return FALSE;
8078 }
8079
8080 if (stop_arrow() == FAIL)
8081 return FALSE;
8082 in_indent = inindent(0);
8083#ifdef FEAT_CINDENT
8084 if (in_indent)
8085 can_cindent = FALSE;
8086#endif
8087#ifdef FEAT_COMMENTS
8088 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8089#endif
8090#ifdef FEAT_RIGHTLEFT
8091 if (revins_on) /* put cursor after last inserted char */
8092 inc_cursor();
8093#endif
8094
8095#ifdef FEAT_VIRTUALEDIT
8096 /* Virtualedit:
8097 * BACKSPACE_CHAR eats a virtual space
8098 * BACKSPACE_WORD eats all coladd
8099 * BACKSPACE_LINE eats all coladd and keeps going
8100 */
8101 if (curwin->w_cursor.coladd > 0)
8102 {
8103 if (mode == BACKSPACE_CHAR)
8104 {
8105 --curwin->w_cursor.coladd;
8106 return TRUE;
8107 }
8108 if (mode == BACKSPACE_WORD)
8109 {
8110 curwin->w_cursor.coladd = 0;
8111 return TRUE;
8112 }
8113 curwin->w_cursor.coladd = 0;
8114 }
8115#endif
8116
8117 /*
8118 * delete newline!
8119 */
8120 if (curwin->w_cursor.col == 0)
8121 {
8122 lnum = Insstart.lnum;
8123 if (curwin->w_cursor.lnum == Insstart.lnum
8124#ifdef FEAT_RIGHTLEFT
8125 || revins_on
8126#endif
8127 )
8128 {
8129 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8130 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8131 return FALSE;
8132 --Insstart.lnum;
8133 Insstart.col = MAXCOL;
8134 }
8135 /*
8136 * In replace mode:
8137 * cc < 0: NL was inserted, delete it
8138 * cc >= 0: NL was replaced, put original characters back
8139 */
8140 cc = -1;
8141 if (State & REPLACE_FLAG)
8142 cc = replace_pop(); /* returns -1 if NL was inserted */
8143 /*
8144 * In replace mode, in the line we started replacing, we only move the
8145 * cursor.
8146 */
8147 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8148 {
8149 dec_cursor();
8150 }
8151 else
8152 {
8153#ifdef FEAT_VREPLACE
8154 if (!(State & VREPLACE_FLAG)
8155 || curwin->w_cursor.lnum > orig_line_count)
8156#endif
8157 {
8158 temp = gchar_cursor(); /* remember current char */
8159 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008160
8161 /* When "aw" is in 'formatoptions' we must delete the space at
8162 * the end of the line, otherwise the line will be broken
8163 * again when auto-formatting. */
8164 if (has_format_option(FO_AUTO)
8165 && has_format_option(FO_WHITE_PAR))
8166 {
8167 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8168 TRUE);
8169 int len;
8170
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008171 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008172 if (len > 0 && ptr[len - 1] == ' ')
8173 ptr[len - 1] = NUL;
8174 }
8175
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 (void)do_join(FALSE);
8177 if (temp == NUL && gchar_cursor() != NUL)
8178 inc_cursor();
8179 }
8180#ifdef FEAT_VREPLACE
8181 else
8182 dec_cursor();
8183#endif
8184
8185 /*
8186 * In REPLACE mode we have to put back the text that was replaced
8187 * by the NL. On the replace stack is first a NUL-terminated
8188 * sequence of characters that were deleted and then the
8189 * characters that NL replaced.
8190 */
8191 if (State & REPLACE_FLAG)
8192 {
8193 /*
8194 * Do the next ins_char() in NORMAL state, to
8195 * prevent ins_char() from replacing characters and
8196 * avoiding showmatch().
8197 */
8198 oldState = State;
8199 State = NORMAL;
8200 /*
8201 * restore characters (blanks) deleted after cursor
8202 */
8203 while (cc > 0)
8204 {
8205 temp = curwin->w_cursor.col;
8206#ifdef FEAT_MBYTE
8207 mb_replace_pop_ins(cc);
8208#else
8209 ins_char(cc);
8210#endif
8211 curwin->w_cursor.col = temp;
8212 cc = replace_pop();
8213 }
8214 /* restore the characters that NL replaced */
8215 replace_pop_ins();
8216 State = oldState;
8217 }
8218 }
8219 did_ai = FALSE;
8220 }
8221 else
8222 {
8223 /*
8224 * Delete character(s) before the cursor.
8225 */
8226#ifdef FEAT_RIGHTLEFT
8227 if (revins_on) /* put cursor on last inserted char */
8228 dec_cursor();
8229#endif
8230 mincol = 0;
8231 /* keep indent */
8232 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
8233#ifdef FEAT_RIGHTLEFT
8234 && !revins_on
8235#endif
8236 )
8237 {
8238 temp = curwin->w_cursor.col;
8239 beginline(BL_WHITE);
8240 if (curwin->w_cursor.col < (colnr_T)temp)
8241 mincol = curwin->w_cursor.col;
8242 curwin->w_cursor.col = temp;
8243 }
8244
8245 /*
8246 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8247 */
8248 if ( mode == BACKSPACE_CHAR
8249 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00008250 || (curbuf->b_p_sts != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 && (*(ml_get_cursor() - 1) == TAB
8252 || (*(ml_get_cursor() - 1) == ' '
8253 && (!*inserted_space_p
8254 || arrow_used))))))
8255 {
8256 int ts;
8257 colnr_T vcol;
8258 colnr_T want_vcol;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008259#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 int extra = 0;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262
8263 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008264 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 ts = curbuf->b_p_sw;
8266 else
8267 ts = curbuf->b_p_sts;
8268 /* Compute the virtual column where we want to be. Since
8269 * 'showbreak' may get in the way, need to get the last column of
8270 * the previous character. */
8271 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8272 dec_cursor();
8273 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8274 inc_cursor();
8275 want_vcol = (want_vcol / ts) * ts;
8276
8277 /* delete characters until we are at or before want_vcol */
8278 while (vcol > want_vcol
8279 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
8280 {
8281 dec_cursor();
8282 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8283 if (State & REPLACE_FLAG)
8284 {
8285 /* Don't delete characters before the insert point when in
8286 * Replace mode */
8287 if (curwin->w_cursor.lnum != Insstart.lnum
8288 || curwin->w_cursor.col >= Insstart.col)
8289 {
8290#if 0 /* what was this for? It causes problems when sw != ts. */
8291 if (State == REPLACE && (int)vcol < want_vcol)
8292 {
8293 (void)del_char(FALSE);
8294 extra = 2; /* don't pop too much */
8295 }
8296 else
8297#endif
8298 replace_do_bs();
8299 }
8300 }
8301 else
8302 (void)del_char(FALSE);
8303 }
8304
8305 /* insert extra spaces until we are at want_vcol */
8306 while (vcol < want_vcol)
8307 {
8308 /* Remember the first char we inserted */
8309 if (curwin->w_cursor.lnum == Insstart.lnum
8310 && curwin->w_cursor.col < Insstart.col)
8311 Insstart.col = curwin->w_cursor.col;
8312
8313#ifdef FEAT_VREPLACE
8314 if (State & VREPLACE_FLAG)
8315 ins_char(' ');
8316 else
8317#endif
8318 {
8319 ins_str((char_u *)" ");
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008320 if ((State & REPLACE_FLAG) /* && extra <= 1 */)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008322#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 if (extra)
8324 replace_push_off(NUL);
8325 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 replace_push(NUL);
8328 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008329#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 if (extra == 2)
8331 extra = 1;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008332#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 }
8334 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8335 }
8336 }
8337
8338 /*
8339 * Delete upto starting point, start of line or previous word.
8340 */
8341 else do
8342 {
8343#ifdef FEAT_RIGHTLEFT
8344 if (!revins_on) /* put cursor on char to be deleted */
8345#endif
8346 dec_cursor();
8347
8348 /* start of word? */
8349 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
8350 {
8351 mode = BACKSPACE_WORD_NOT_SPACE;
8352 temp = vim_iswordc(gchar_cursor());
8353 }
8354 /* end of word? */
8355 else if (mode == BACKSPACE_WORD_NOT_SPACE
8356 && (vim_isspace(cc = gchar_cursor())
8357 || vim_iswordc(cc) != temp))
8358 {
8359#ifdef FEAT_RIGHTLEFT
8360 if (!revins_on)
8361#endif
8362 inc_cursor();
8363#ifdef FEAT_RIGHTLEFT
8364 else if (State & REPLACE_FLAG)
8365 dec_cursor();
8366#endif
8367 break;
8368 }
8369 if (State & REPLACE_FLAG)
8370 replace_do_bs();
8371 else
8372 {
8373#ifdef FEAT_MBYTE
8374 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008375 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376#endif
8377 (void)del_char(FALSE);
8378#ifdef FEAT_MBYTE
8379 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008380 * If there are combining characters and 'delcombine' is set
8381 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 * character.
8383 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008384 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 inc_cursor();
8386#endif
8387#ifdef FEAT_RIGHTLEFT
8388 if (revins_chars)
8389 {
8390 revins_chars--;
8391 revins_legal++;
8392 }
8393 if (revins_on && gchar_cursor() == NUL)
8394 break;
8395#endif
8396 }
8397 /* Just a single backspace?: */
8398 if (mode == BACKSPACE_CHAR)
8399 break;
8400 } while (
8401#ifdef FEAT_RIGHTLEFT
8402 revins_on ||
8403#endif
8404 (curwin->w_cursor.col > mincol
8405 && (curwin->w_cursor.lnum != Insstart.lnum
8406 || curwin->w_cursor.col != Insstart.col)));
8407 did_backspace = TRUE;
8408 }
8409#ifdef FEAT_SMARTINDENT
8410 did_si = FALSE;
8411 can_si = FALSE;
8412 can_si_back = FALSE;
8413#endif
8414 if (curwin->w_cursor.col <= 1)
8415 did_ai = FALSE;
8416 /*
8417 * It's a little strange to put backspaces into the redo
8418 * buffer, but it makes auto-indent a lot easier to deal
8419 * with.
8420 */
8421 AppendCharToRedobuff(c);
8422
8423 /* If deleted before the insertion point, adjust it */
8424 if (curwin->w_cursor.lnum == Insstart.lnum
8425 && curwin->w_cursor.col < Insstart.col)
8426 Insstart.col = curwin->w_cursor.col;
8427
8428 /* vi behaviour: the cursor moves backward but the character that
8429 * was there remains visible
8430 * Vim behaviour: the cursor moves backward and the character that
8431 * was there is erased from the screen.
8432 * We can emulate the vi behaviour by pretending there is a dollar
8433 * displayed even when there isn't.
8434 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8435 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
8436 dollar_vcol = curwin->w_virtcol;
8437
8438 return did_backspace;
8439}
8440
8441#ifdef FEAT_MOUSE
8442 static void
8443ins_mouse(c)
8444 int c;
8445{
8446 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008447 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448
8449# ifdef FEAT_GUI
8450 /* When GUI is active, also move/paste when 'mouse' is empty */
8451 if (!gui.in_use)
8452# endif
8453 if (!mouse_has(MOUSE_INSERT))
8454 return;
8455
8456 undisplay_dollar();
8457 tpos = curwin->w_cursor;
8458 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
8459 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008460#ifdef FEAT_WINDOWS
8461 win_T *new_curwin = curwin;
8462
8463 if (curwin != old_curwin && win_valid(old_curwin))
8464 {
8465 /* Mouse took us to another window. We need to go back to the
8466 * previous one to stop insert there properly. */
8467 curwin = old_curwin;
8468 curbuf = curwin->w_buffer;
8469 }
8470#endif
8471 start_arrow(curwin == old_curwin ? &tpos : NULL);
8472#ifdef FEAT_WINDOWS
8473 if (curwin != new_curwin && win_valid(new_curwin))
8474 {
8475 curwin = new_curwin;
8476 curbuf = curwin->w_buffer;
8477 }
8478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479# ifdef FEAT_CINDENT
8480 can_cindent = TRUE;
8481# endif
8482 }
8483
8484#ifdef FEAT_WINDOWS
8485 /* redraw status lines (in case another window became active) */
8486 redraw_statuslines();
8487#endif
8488}
8489
8490 static void
8491ins_mousescroll(up)
8492 int up;
8493{
8494 pos_T tpos;
8495# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8496 win_T *old_curwin;
8497# endif
8498
8499 tpos = curwin->w_cursor;
8500
8501# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8502 old_curwin = curwin;
8503
8504 /* Currently the mouse coordinates are only known in the GUI. */
8505 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
8506 {
8507 int row, col;
8508
8509 row = mouse_row;
8510 col = mouse_col;
8511
8512 /* find the window at the pointer coordinates */
8513 curwin = mouse_find_win(&row, &col);
8514 curbuf = curwin->w_buffer;
8515 }
8516 if (curwin == old_curwin)
8517# endif
8518 undisplay_dollar();
8519
8520 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8521 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
8522 else
8523 scroll_redraw(up, 3L);
8524
8525# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8526 curwin->w_redr_status = TRUE;
8527
8528 curwin = old_curwin;
8529 curbuf = curwin->w_buffer;
8530# endif
8531
8532 if (!equalpos(curwin->w_cursor, tpos))
8533 {
8534 start_arrow(&tpos);
8535# ifdef FEAT_CINDENT
8536 can_cindent = TRUE;
8537# endif
8538 }
8539}
8540#endif
8541
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008542#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00008543 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008544ins_tabline(c)
8545 int c;
8546{
8547 /* We will be leaving the current window, unless closing another tab. */
8548 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
8549 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
8550 {
8551 undisplay_dollar();
8552 start_arrow(&curwin->w_cursor);
8553# ifdef FEAT_CINDENT
8554 can_cindent = TRUE;
8555# endif
8556 }
8557
8558 if (c == K_TABLINE)
8559 goto_tabpage(current_tab);
8560 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008561 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008562 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008563 redraw_statuslines(); /* will redraw the tabline when needed */
8564 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008565}
8566#endif
8567
8568#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 void
8570ins_scroll()
8571{
8572 pos_T tpos;
8573
8574 undisplay_dollar();
8575 tpos = curwin->w_cursor;
8576 if (gui_do_scroll())
8577 {
8578 start_arrow(&tpos);
8579# ifdef FEAT_CINDENT
8580 can_cindent = TRUE;
8581# endif
8582 }
8583}
8584
8585 void
8586ins_horscroll()
8587{
8588 pos_T tpos;
8589
8590 undisplay_dollar();
8591 tpos = curwin->w_cursor;
8592 if (gui_do_horiz_scroll())
8593 {
8594 start_arrow(&tpos);
8595# ifdef FEAT_CINDENT
8596 can_cindent = TRUE;
8597# endif
8598 }
8599}
8600#endif
8601
8602 static void
8603ins_left()
8604{
8605 pos_T tpos;
8606
8607#ifdef FEAT_FOLDING
8608 if ((fdo_flags & FDO_HOR) && KeyTyped)
8609 foldOpenCursor();
8610#endif
8611 undisplay_dollar();
8612 tpos = curwin->w_cursor;
8613 if (oneleft() == OK)
8614 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00008615#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
8616 /* Only call start_arrow() when not busy with preediting, it will
8617 * break undo. K_LEFT is inserted in im_correct_cursor(). */
8618 if (!im_is_preediting())
8619#endif
8620 start_arrow(&tpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621#ifdef FEAT_RIGHTLEFT
8622 /* If exit reversed string, position is fixed */
8623 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
8624 revins_legal++;
8625 revins_chars++;
8626#endif
8627 }
8628
8629 /*
8630 * if 'whichwrap' set for cursor in insert mode may go to
8631 * previous line
8632 */
8633 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
8634 {
8635 start_arrow(&tpos);
8636 --(curwin->w_cursor.lnum);
8637 coladvance((colnr_T)MAXCOL);
8638 curwin->w_set_curswant = TRUE; /* so we stay at the end */
8639 }
8640 else
8641 vim_beep();
8642}
8643
8644 static void
8645ins_home(c)
8646 int c;
8647{
8648 pos_T tpos;
8649
8650#ifdef FEAT_FOLDING
8651 if ((fdo_flags & FDO_HOR) && KeyTyped)
8652 foldOpenCursor();
8653#endif
8654 undisplay_dollar();
8655 tpos = curwin->w_cursor;
8656 if (c == K_C_HOME)
8657 curwin->w_cursor.lnum = 1;
8658 curwin->w_cursor.col = 0;
8659#ifdef FEAT_VIRTUALEDIT
8660 curwin->w_cursor.coladd = 0;
8661#endif
8662 curwin->w_curswant = 0;
8663 start_arrow(&tpos);
8664}
8665
8666 static void
8667ins_end(c)
8668 int c;
8669{
8670 pos_T tpos;
8671
8672#ifdef FEAT_FOLDING
8673 if ((fdo_flags & FDO_HOR) && KeyTyped)
8674 foldOpenCursor();
8675#endif
8676 undisplay_dollar();
8677 tpos = curwin->w_cursor;
8678 if (c == K_C_END)
8679 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
8680 coladvance((colnr_T)MAXCOL);
8681 curwin->w_curswant = MAXCOL;
8682
8683 start_arrow(&tpos);
8684}
8685
8686 static void
8687ins_s_left()
8688{
8689#ifdef FEAT_FOLDING
8690 if ((fdo_flags & FDO_HOR) && KeyTyped)
8691 foldOpenCursor();
8692#endif
8693 undisplay_dollar();
8694 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
8695 {
8696 start_arrow(&curwin->w_cursor);
8697 (void)bck_word(1L, FALSE, FALSE);
8698 curwin->w_set_curswant = TRUE;
8699 }
8700 else
8701 vim_beep();
8702}
8703
8704 static void
8705ins_right()
8706{
8707#ifdef FEAT_FOLDING
8708 if ((fdo_flags & FDO_HOR) && KeyTyped)
8709 foldOpenCursor();
8710#endif
8711 undisplay_dollar();
8712 if (gchar_cursor() != NUL || virtual_active()
8713 )
8714 {
8715 start_arrow(&curwin->w_cursor);
8716 curwin->w_set_curswant = TRUE;
8717#ifdef FEAT_VIRTUALEDIT
8718 if (virtual_active())
8719 oneright();
8720 else
8721#endif
8722 {
8723#ifdef FEAT_MBYTE
8724 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008725 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 else
8727#endif
8728 ++curwin->w_cursor.col;
8729 }
8730
8731#ifdef FEAT_RIGHTLEFT
8732 revins_legal++;
8733 if (revins_chars)
8734 revins_chars--;
8735#endif
8736 }
8737 /* if 'whichwrap' set for cursor in insert mode, may move the
8738 * cursor to the next line */
8739 else if (vim_strchr(p_ww, ']') != NULL
8740 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
8741 {
8742 start_arrow(&curwin->w_cursor);
8743 curwin->w_set_curswant = TRUE;
8744 ++curwin->w_cursor.lnum;
8745 curwin->w_cursor.col = 0;
8746 }
8747 else
8748 vim_beep();
8749}
8750
8751 static void
8752ins_s_right()
8753{
8754#ifdef FEAT_FOLDING
8755 if ((fdo_flags & FDO_HOR) && KeyTyped)
8756 foldOpenCursor();
8757#endif
8758 undisplay_dollar();
8759 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
8760 || gchar_cursor() != NUL)
8761 {
8762 start_arrow(&curwin->w_cursor);
8763 (void)fwd_word(1L, FALSE, 0);
8764 curwin->w_set_curswant = TRUE;
8765 }
8766 else
8767 vim_beep();
8768}
8769
8770 static void
8771ins_up(startcol)
8772 int startcol; /* when TRUE move to Insstart.col */
8773{
8774 pos_T tpos;
8775 linenr_T old_topline = curwin->w_topline;
8776#ifdef FEAT_DIFF
8777 int old_topfill = curwin->w_topfill;
8778#endif
8779
8780 undisplay_dollar();
8781 tpos = curwin->w_cursor;
8782 if (cursor_up(1L, TRUE) == OK)
8783 {
8784 if (startcol)
8785 coladvance(getvcol_nolist(&Insstart));
8786 if (old_topline != curwin->w_topline
8787#ifdef FEAT_DIFF
8788 || old_topfill != curwin->w_topfill
8789#endif
8790 )
8791 redraw_later(VALID);
8792 start_arrow(&tpos);
8793#ifdef FEAT_CINDENT
8794 can_cindent = TRUE;
8795#endif
8796 }
8797 else
8798 vim_beep();
8799}
8800
8801 static void
8802ins_pageup()
8803{
8804 pos_T tpos;
8805
8806 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008807
8808#ifdef FEAT_WINDOWS
8809 if (mod_mask & MOD_MASK_CTRL)
8810 {
8811 /* <C-PageUp>: tab page back */
8812 goto_tabpage(-1);
8813 return;
8814 }
8815#endif
8816
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 tpos = curwin->w_cursor;
8818 if (onepage(BACKWARD, 1L) == OK)
8819 {
8820 start_arrow(&tpos);
8821#ifdef FEAT_CINDENT
8822 can_cindent = TRUE;
8823#endif
8824 }
8825 else
8826 vim_beep();
8827}
8828
8829 static void
8830ins_down(startcol)
8831 int startcol; /* when TRUE move to Insstart.col */
8832{
8833 pos_T tpos;
8834 linenr_T old_topline = curwin->w_topline;
8835#ifdef FEAT_DIFF
8836 int old_topfill = curwin->w_topfill;
8837#endif
8838
8839 undisplay_dollar();
8840 tpos = curwin->w_cursor;
8841 if (cursor_down(1L, TRUE) == OK)
8842 {
8843 if (startcol)
8844 coladvance(getvcol_nolist(&Insstart));
8845 if (old_topline != curwin->w_topline
8846#ifdef FEAT_DIFF
8847 || old_topfill != curwin->w_topfill
8848#endif
8849 )
8850 redraw_later(VALID);
8851 start_arrow(&tpos);
8852#ifdef FEAT_CINDENT
8853 can_cindent = TRUE;
8854#endif
8855 }
8856 else
8857 vim_beep();
8858}
8859
8860 static void
8861ins_pagedown()
8862{
8863 pos_T tpos;
8864
8865 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008866
8867#ifdef FEAT_WINDOWS
8868 if (mod_mask & MOD_MASK_CTRL)
8869 {
8870 /* <C-PageDown>: tab page forward */
8871 goto_tabpage(0);
8872 return;
8873 }
8874#endif
8875
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 tpos = curwin->w_cursor;
8877 if (onepage(FORWARD, 1L) == OK)
8878 {
8879 start_arrow(&tpos);
8880#ifdef FEAT_CINDENT
8881 can_cindent = TRUE;
8882#endif
8883 }
8884 else
8885 vim_beep();
8886}
8887
8888#ifdef FEAT_DND
8889 static void
8890ins_drop()
8891{
8892 do_put('~', BACKWARD, 1L, PUT_CURSEND);
8893}
8894#endif
8895
8896/*
8897 * Handle TAB in Insert or Replace mode.
8898 * Return TRUE when the TAB needs to be inserted like a normal character.
8899 */
8900 static int
8901ins_tab()
8902{
8903 int ind;
8904 int i;
8905 int temp;
8906
8907 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
8908 Insstart_blank_vcol = get_nolist_virtcol();
8909 if (echeck_abbr(TAB + ABBR_OFF))
8910 return FALSE;
8911
8912 ind = inindent(0);
8913#ifdef FEAT_CINDENT
8914 if (ind)
8915 can_cindent = FALSE;
8916#endif
8917
8918 /*
8919 * When nothing special, insert TAB like a normal character
8920 */
8921 if (!curbuf->b_p_et
8922 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
8923 && curbuf->b_p_sts == 0)
8924 return TRUE;
8925
8926 if (stop_arrow() == FAIL)
8927 return TRUE;
8928
8929 did_ai = FALSE;
8930#ifdef FEAT_SMARTINDENT
8931 did_si = FALSE;
8932 can_si = FALSE;
8933 can_si_back = FALSE;
8934#endif
8935 AppendToRedobuff((char_u *)"\t");
8936
8937 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
8938 temp = (int)curbuf->b_p_sw;
8939 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
8940 temp = (int)curbuf->b_p_sts;
8941 else /* otherwise use 'tabstop' */
8942 temp = (int)curbuf->b_p_ts;
8943 temp -= get_nolist_virtcol() % temp;
8944
8945 /*
8946 * Insert the first space with ins_char(). It will delete one char in
8947 * replace mode. Insert the rest with ins_str(); it will not delete any
8948 * chars. For VREPLACE mode, we use ins_char() for all characters.
8949 */
8950 ins_char(' ');
8951 while (--temp > 0)
8952 {
8953#ifdef FEAT_VREPLACE
8954 if (State & VREPLACE_FLAG)
8955 ins_char(' ');
8956 else
8957#endif
8958 {
8959 ins_str((char_u *)" ");
8960 if (State & REPLACE_FLAG) /* no char replaced */
8961 replace_push(NUL);
8962 }
8963 }
8964
8965 /*
8966 * When 'expandtab' not set: Replace spaces by TABs where possible.
8967 */
8968 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
8969 {
8970 char_u *ptr;
8971#ifdef FEAT_VREPLACE
8972 char_u *saved_line = NULL; /* init for GCC */
8973 pos_T pos;
8974#endif
8975 pos_T fpos;
8976 pos_T *cursor;
8977 colnr_T want_vcol, vcol;
8978 int change_col = -1;
8979 int save_list = curwin->w_p_list;
8980
8981 /*
8982 * Get the current line. For VREPLACE mode, don't make real changes
8983 * yet, just work on a copy of the line.
8984 */
8985#ifdef FEAT_VREPLACE
8986 if (State & VREPLACE_FLAG)
8987 {
8988 pos = curwin->w_cursor;
8989 cursor = &pos;
8990 saved_line = vim_strsave(ml_get_curline());
8991 if (saved_line == NULL)
8992 return FALSE;
8993 ptr = saved_line + pos.col;
8994 }
8995 else
8996#endif
8997 {
8998 ptr = ml_get_cursor();
8999 cursor = &curwin->w_cursor;
9000 }
9001
9002 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9003 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9004 curwin->w_p_list = FALSE;
9005
9006 /* Find first white before the cursor */
9007 fpos = curwin->w_cursor;
9008 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
9009 {
9010 --fpos.col;
9011 --ptr;
9012 }
9013
9014 /* In Replace mode, don't change characters before the insert point. */
9015 if ((State & REPLACE_FLAG)
9016 && fpos.lnum == Insstart.lnum
9017 && fpos.col < Insstart.col)
9018 {
9019 ptr += Insstart.col - fpos.col;
9020 fpos.col = Insstart.col;
9021 }
9022
9023 /* compute virtual column numbers of first white and cursor */
9024 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9025 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9026
9027 /* Use as many TABs as possible. Beware of 'showbreak' and
9028 * 'linebreak' adding extra virtual columns. */
9029 while (vim_iswhite(*ptr))
9030 {
9031 i = lbr_chartabsize((char_u *)"\t", vcol);
9032 if (vcol + i > want_vcol)
9033 break;
9034 if (*ptr != TAB)
9035 {
9036 *ptr = TAB;
9037 if (change_col < 0)
9038 {
9039 change_col = fpos.col; /* Column of first change */
9040 /* May have to adjust Insstart */
9041 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9042 Insstart.col = fpos.col;
9043 }
9044 }
9045 ++fpos.col;
9046 ++ptr;
9047 vcol += i;
9048 }
9049
9050 if (change_col >= 0)
9051 {
9052 int repl_off = 0;
9053
9054 /* Skip over the spaces we need. */
9055 while (vcol < want_vcol && *ptr == ' ')
9056 {
9057 vcol += lbr_chartabsize(ptr, vcol);
9058 ++ptr;
9059 ++repl_off;
9060 }
9061 if (vcol > want_vcol)
9062 {
9063 /* Must have a char with 'showbreak' just before it. */
9064 --ptr;
9065 --repl_off;
9066 }
9067 fpos.col += repl_off;
9068
9069 /* Delete following spaces. */
9070 i = cursor->col - fpos.col;
9071 if (i > 0)
9072 {
9073 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
9074 /* correct replace stack. */
9075 if ((State & REPLACE_FLAG)
9076#ifdef FEAT_VREPLACE
9077 && !(State & VREPLACE_FLAG)
9078#endif
9079 )
9080 for (temp = i; --temp >= 0; )
9081 replace_join(repl_off);
9082 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009083#ifdef FEAT_NETBEANS_INTG
9084 if (usingNetbeans)
9085 {
9086 netbeans_removed(curbuf, fpos.lnum, cursor->col,
9087 (long)(i + 1));
9088 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9089 (char_u *)"\t", 1);
9090 }
9091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092 cursor->col -= i;
9093
9094#ifdef FEAT_VREPLACE
9095 /*
9096 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9097 * backspacing over the changed spacing and then inserting the new
9098 * spacing.
9099 */
9100 if (State & VREPLACE_FLAG)
9101 {
9102 /* Backspace from real cursor to change_col */
9103 backspace_until_column(change_col);
9104
9105 /* Insert each char in saved_line from changed_col to
9106 * ptr-cursor */
9107 ins_bytes_len(saved_line + change_col,
9108 cursor->col - change_col);
9109 }
9110#endif
9111 }
9112
9113#ifdef FEAT_VREPLACE
9114 if (State & VREPLACE_FLAG)
9115 vim_free(saved_line);
9116#endif
9117 curwin->w_p_list = save_list;
9118 }
9119
9120 return FALSE;
9121}
9122
9123/*
9124 * Handle CR or NL in insert mode.
9125 * Return TRUE when out of memory or can't undo.
9126 */
9127 static int
9128ins_eol(c)
9129 int c;
9130{
9131 int i;
9132
9133 if (echeck_abbr(c + ABBR_OFF))
9134 return FALSE;
9135 if (stop_arrow() == FAIL)
9136 return TRUE;
9137 undisplay_dollar();
9138
9139 /*
9140 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9141 * character under the cursor. Only push a NUL on the replace stack,
9142 * nothing to put back when the NL is deleted.
9143 */
9144 if ((State & REPLACE_FLAG)
9145#ifdef FEAT_VREPLACE
9146 && !(State & VREPLACE_FLAG)
9147#endif
9148 )
9149 replace_push(NUL);
9150
9151 /*
9152 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9153 * replacing the next line, so we push all of the characters left on the
9154 * line onto the replace stack. This is not done here though, it is done
9155 * in open_line().
9156 */
9157
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009158#ifdef FEAT_VIRTUALEDIT
9159 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9160 * CTRL-O). */
9161 if (virtual_active() && curwin->w_cursor.coladd > 0)
9162 coladvance(getviscol());
9163#endif
9164
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165#ifdef FEAT_RIGHTLEFT
9166# ifdef FEAT_FKMAP
9167 if (p_altkeymap && p_fkmap)
9168 fkmap(NL);
9169# endif
9170 /* NL in reverse insert will always start in the end of
9171 * current line. */
9172 if (revins_on)
9173 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9174#endif
9175
9176 AppendToRedobuff(NL_STR);
9177 i = open_line(FORWARD,
9178#ifdef FEAT_COMMENTS
9179 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
9180#endif
9181 0, old_indent);
9182 old_indent = 0;
9183#ifdef FEAT_CINDENT
9184 can_cindent = TRUE;
9185#endif
9186
9187 return (!i);
9188}
9189
9190#ifdef FEAT_DIGRAPHS
9191/*
9192 * Handle digraph in insert mode.
9193 * Returns character still to be inserted, or NUL when nothing remaining to be
9194 * done.
9195 */
9196 static int
9197ins_digraph()
9198{
9199 int c;
9200 int cc;
9201
9202 pc_status = PC_STATUS_UNSET;
9203 if (redrawing() && !char_avail())
9204 {
9205 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009206 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207
9208 edit_putchar('?', TRUE);
9209#ifdef FEAT_CMDL_INFO
9210 add_to_showcmd_c(Ctrl_K);
9211#endif
9212 }
9213
9214#ifdef USE_ON_FLY_SCROLL
9215 dont_scroll = TRUE; /* disallow scrolling here */
9216#endif
9217
9218 /* don't map the digraph chars. This also prevents the
9219 * mode message to be deleted when ESC is hit */
9220 ++no_mapping;
9221 ++allow_keys;
9222 c = safe_vgetc();
9223 --no_mapping;
9224 --allow_keys;
9225 if (IS_SPECIAL(c) || mod_mask) /* special key */
9226 {
9227#ifdef FEAT_CMDL_INFO
9228 clear_showcmd();
9229#endif
9230 insert_special(c, TRUE, FALSE);
9231 return NUL;
9232 }
9233 if (c != ESC)
9234 {
9235 if (redrawing() && !char_avail())
9236 {
9237 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009238 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239
9240 if (char2cells(c) == 1)
9241 {
9242 /* first remove the '?', otherwise it's restored when typing
9243 * an ESC next */
9244 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00009245 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 edit_putchar(c, TRUE);
9247 }
9248#ifdef FEAT_CMDL_INFO
9249 add_to_showcmd_c(c);
9250#endif
9251 }
9252 ++no_mapping;
9253 ++allow_keys;
9254 cc = safe_vgetc();
9255 --no_mapping;
9256 --allow_keys;
9257 if (cc != ESC)
9258 {
9259 AppendToRedobuff((char_u *)CTRL_V_STR);
9260 c = getdigraph(c, cc, TRUE);
9261#ifdef FEAT_CMDL_INFO
9262 clear_showcmd();
9263#endif
9264 return c;
9265 }
9266 }
9267 edit_unputchar();
9268#ifdef FEAT_CMDL_INFO
9269 clear_showcmd();
9270#endif
9271 return NUL;
9272}
9273#endif
9274
9275/*
9276 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
9277 * Returns the char to be inserted, or NUL if none found.
9278 */
9279 static int
9280ins_copychar(lnum)
9281 linenr_T lnum;
9282{
9283 int c;
9284 int temp;
9285 char_u *ptr, *prev_ptr;
9286
9287 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9288 {
9289 vim_beep();
9290 return NUL;
9291 }
9292
9293 /* try to advance to the cursor column */
9294 temp = 0;
9295 ptr = ml_get(lnum);
9296 prev_ptr = ptr;
9297 validate_virtcol();
9298 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
9299 {
9300 prev_ptr = ptr;
9301 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
9302 }
9303 if ((colnr_T)temp > curwin->w_virtcol)
9304 ptr = prev_ptr;
9305
9306#ifdef FEAT_MBYTE
9307 c = (*mb_ptr2char)(ptr);
9308#else
9309 c = *ptr;
9310#endif
9311 if (c == NUL)
9312 vim_beep();
9313 return c;
9314}
9315
Bram Moolenaar4be06f92005-07-29 22:36:03 +00009316/*
9317 * CTRL-Y or CTRL-E typed in Insert mode.
9318 */
9319 static int
9320ins_ctrl_ey(tc)
9321 int tc;
9322{
9323 int c = tc;
9324
9325#ifdef FEAT_INS_EXPAND
9326 if (ctrl_x_mode == CTRL_X_SCROLL)
9327 {
9328 if (c == Ctrl_Y)
9329 scrolldown_clamp();
9330 else
9331 scrollup_clamp();
9332 redraw_later(VALID);
9333 }
9334 else
9335#endif
9336 {
9337 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
9338 if (c != NUL)
9339 {
9340 long tw_save;
9341
9342 /* The character must be taken literally, insert like it
9343 * was typed after a CTRL-V, and pretend 'textwidth'
9344 * wasn't set. Digits, 'o' and 'x' are special after a
9345 * CTRL-V, don't use it for these. */
9346 if (c < 256 && !isalnum(c))
9347 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
9348 tw_save = curbuf->b_p_tw;
9349 curbuf->b_p_tw = -1;
9350 insert_special(c, TRUE, FALSE);
9351 curbuf->b_p_tw = tw_save;
9352#ifdef FEAT_RIGHTLEFT
9353 revins_chars++;
9354 revins_legal++;
9355#endif
9356 c = Ctrl_V; /* pretend CTRL-V is last character */
9357 auto_format(FALSE, TRUE);
9358 }
9359 }
9360 return c;
9361}
9362
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363#ifdef FEAT_SMARTINDENT
9364/*
9365 * Try to do some very smart auto-indenting.
9366 * Used when inserting a "normal" character.
9367 */
9368 static void
9369ins_try_si(c)
9370 int c;
9371{
9372 pos_T *pos, old_pos;
9373 char_u *ptr;
9374 int i;
9375 int temp;
9376
9377 /*
9378 * do some very smart indenting when entering '{' or '}'
9379 */
9380 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
9381 {
9382 /*
9383 * for '}' set indent equal to indent of line containing matching '{'
9384 */
9385 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
9386 {
9387 old_pos = curwin->w_cursor;
9388 /*
9389 * If the matching '{' has a ')' immediately before it (ignoring
9390 * white-space), then line up with the start of the line
9391 * containing the matching '(' if there is one. This handles the
9392 * case where an "if (..\n..) {" statement continues over multiple
9393 * lines -- webb
9394 */
9395 ptr = ml_get(pos->lnum);
9396 i = pos->col;
9397 if (i > 0) /* skip blanks before '{' */
9398 while (--i > 0 && vim_iswhite(ptr[i]))
9399 ;
9400 curwin->w_cursor.lnum = pos->lnum;
9401 curwin->w_cursor.col = i;
9402 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
9403 curwin->w_cursor = *pos;
9404 i = get_indent();
9405 curwin->w_cursor = old_pos;
9406#ifdef FEAT_VREPLACE
9407 if (State & VREPLACE_FLAG)
9408 change_indent(INDENT_SET, i, FALSE, NUL);
9409 else
9410#endif
9411 (void)set_indent(i, SIN_CHANGED);
9412 }
9413 else if (curwin->w_cursor.col > 0)
9414 {
9415 /*
9416 * when inserting '{' after "O" reduce indent, but not
9417 * more than indent of previous line
9418 */
9419 temp = TRUE;
9420 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
9421 {
9422 old_pos = curwin->w_cursor;
9423 i = get_indent();
9424 while (curwin->w_cursor.lnum > 1)
9425 {
9426 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
9427
9428 /* ignore empty lines and lines starting with '#'. */
9429 if (*ptr != '#' && *ptr != NUL)
9430 break;
9431 }
9432 if (get_indent() >= i)
9433 temp = FALSE;
9434 curwin->w_cursor = old_pos;
9435 }
9436 if (temp)
9437 shift_line(TRUE, FALSE, 1);
9438 }
9439 }
9440
9441 /*
9442 * set indent of '#' always to 0
9443 */
9444 if (curwin->w_cursor.col > 0 && can_si && c == '#')
9445 {
9446 /* remember current indent for next line */
9447 old_indent = get_indent();
9448 (void)set_indent(0, SIN_CHANGED);
9449 }
9450
9451 /* Adjust ai_col, the char at this position can be deleted. */
9452 if (ai_col > curwin->w_cursor.col)
9453 ai_col = curwin->w_cursor.col;
9454}
9455#endif
9456
9457/*
9458 * Get the value that w_virtcol would have when 'list' is off.
9459 * Unless 'cpo' contains the 'L' flag.
9460 */
9461 static colnr_T
9462get_nolist_virtcol()
9463{
9464 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9465 return getvcol_nolist(&curwin->w_cursor);
9466 validate_virtcol();
9467 return curwin->w_virtcol;
9468}