blob: b00608554edb6a61996830ac131fb550a0f9583e [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 Moolenaar5948a572006-10-03 13:49:29 +00002160 && STRNCMP(match->cp_str, str, len) == 0
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 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003209 if (compl_leader == NULL
3210 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003211 (int)STRLEN(compl_leader)))
3212 {
3213 p = cp->cp_str;
3214 break;
3215 }
3216 }
3217 if (p == NULL || (int)STRLEN(p) <= len)
3218 return;
3219 }
3220 else
3221 return;
3222 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003223 p += len;
3224#ifdef FEAT_MBYTE
3225 c = mb_ptr2char(p);
3226#else
3227 c = *p;
3228#endif
3229 ins_compl_addleader(c);
3230}
3231
3232/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003234 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003235 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003237 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238ins_compl_prep(c)
3239 int c;
3240{
3241 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003243 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244
3245 /* Forget any previous 'special' messages if this is actually
3246 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3247 */
3248 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3249 edit_submode_extra = NULL;
3250
3251 /* Ignore end of Select mode mapping */
3252 if (c == K_SELECT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003253 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003255 /* Set "compl_get_longest" when finding the first matches. */
3256 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3257 || (ctrl_x_mode == 0 && !compl_started))
3258 {
3259 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3260 compl_used_match = TRUE;
3261 }
3262
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3264 {
3265 /*
3266 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3267 * it will be yet. Now we decide.
3268 */
3269 switch (c)
3270 {
3271 case Ctrl_E:
3272 case Ctrl_Y:
3273 ctrl_x_mode = CTRL_X_SCROLL;
3274 if (!(State & REPLACE_FLAG))
3275 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3276 else
3277 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3278 edit_submode_pre = NULL;
3279 showmode();
3280 break;
3281 case Ctrl_L:
3282 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3283 break;
3284 case Ctrl_F:
3285 ctrl_x_mode = CTRL_X_FILES;
3286 break;
3287 case Ctrl_K:
3288 ctrl_x_mode = CTRL_X_DICTIONARY;
3289 break;
3290 case Ctrl_R:
3291 /* Simply allow ^R to happen without affecting ^X mode */
3292 break;
3293 case Ctrl_T:
3294 ctrl_x_mode = CTRL_X_THESAURUS;
3295 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003296#ifdef FEAT_COMPL_FUNC
3297 case Ctrl_U:
3298 ctrl_x_mode = CTRL_X_FUNCTION;
3299 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003300 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003301 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003302 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003303#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003304 case 's':
3305 case Ctrl_S:
3306 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003307#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003308 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003309 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003310 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003311#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003312 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 case Ctrl_RSB:
3314 ctrl_x_mode = CTRL_X_TAGS;
3315 break;
3316#ifdef FEAT_FIND_ID
3317 case Ctrl_I:
3318 case K_S_TAB:
3319 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3320 break;
3321 case Ctrl_D:
3322 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3323 break;
3324#endif
3325 case Ctrl_V:
3326 case Ctrl_Q:
3327 ctrl_x_mode = CTRL_X_CMDLINE;
3328 break;
3329 case Ctrl_P:
3330 case Ctrl_N:
3331 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3332 * just started ^X mode, or there were enough ^X's to cancel
3333 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3334 * do normal expansion when interrupting a different mode (say
3335 * ^X^F^X^P or ^P^X^X^P, see below)
3336 * nothing changes if interrupting mode 0, (eg, the flag
3337 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003338 if (!(compl_cont_status & CONT_INTRPT))
3339 compl_cont_status |= CONT_LOCAL;
3340 else if (compl_cont_mode != 0)
3341 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 /* FALLTHROUGH */
3343 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003344 /* If we have typed at least 2 ^X's... for modes != 0, we set
3345 * compl_cont_status = 0 (eg, as if we had just started ^X
3346 * mode).
3347 * For mode 0, we set "compl_cont_mode" to an impossible
3348 * value, in both cases ^X^X can be used to restart the same
3349 * mode (avoiding ADDING mode).
3350 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3351 * 'complete' and local ^P expansions respectively.
3352 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3353 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 if (c == Ctrl_X)
3355 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003356 if (compl_cont_mode != 0)
3357 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003359 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 }
3361 ctrl_x_mode = 0;
3362 edit_submode = NULL;
3363 showmode();
3364 break;
3365 }
3366 }
3367 else if (ctrl_x_mode != 0)
3368 {
3369 /* We're already in CTRL-X mode, do we stay in it? */
3370 if (!vim_is_ctrl_x_key(c))
3371 {
3372 if (ctrl_x_mode == CTRL_X_SCROLL)
3373 ctrl_x_mode = 0;
3374 else
3375 ctrl_x_mode = CTRL_X_FINISHED;
3376 edit_submode = NULL;
3377 }
3378 showmode();
3379 }
3380
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003381 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 {
3383 /* Show error message from attempted keyword completion (probably
3384 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003385 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003387 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3388 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 || ctrl_x_mode == CTRL_X_FINISHED)
3390 {
3391 /* Get here when we have finished typing a sequence of ^N and
3392 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003393 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003394 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003396 char_u *p;
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003397 int temp = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003398
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003400 * If any of the original typed text has been changed, eg when
3401 * ignorecase is set, we must add back-spaces to the redo
3402 * buffer. We add as few as necessary to delete just the part
3403 * of the original text that has changed.
3404 * When using the longest match, edited the match or used
3405 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003407 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3408 ptr = compl_curr_match->cp_str;
3409 else if (compl_leader != NULL)
3410 ptr = compl_leader;
3411 else
3412 ptr = compl_orig_text;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003413 if (compl_orig_text != NULL)
3414 {
3415 p = compl_orig_text;
3416 for (temp = 0; p[temp] != NUL && p[temp] == ptr[temp];
3417 ++temp)
3418 ;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003419#ifdef FEAT_MBYTE
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003420 if (temp > 0)
3421 temp -= (*mb_head_off)(compl_orig_text, p + temp);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003422#endif
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003423 for (p += temp; *p != NUL; mb_ptr_adv(p))
3424 AppendCharToRedobuff(K_BS);
3425 }
3426 if (ptr != NULL)
3427 AppendToRedobuffLit(ptr + temp, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 }
3429
3430#ifdef FEAT_CINDENT
3431 want_cindent = (can_cindent && cindent_on());
3432#endif
3433 /*
3434 * When completing whole lines: fix indent for 'cindent'.
3435 * Otherwise, break line if it's too long.
3436 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003437 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 {
3439#ifdef FEAT_CINDENT
3440 /* re-indent the current line */
3441 if (want_cindent)
3442 {
3443 do_c_expr_indent();
3444 want_cindent = FALSE; /* don't do it again */
3445 }
3446#endif
3447 }
3448 else
3449 {
3450 /* put the cursor on the last char, for 'tw' formatting */
3451 curwin->w_cursor.col--;
3452 if (stop_arrow() == OK)
3453 insertchar(NUL, 0, -1);
3454 curwin->w_cursor.col++;
3455 }
3456
3457 auto_format(FALSE, TRUE);
3458
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003459 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003460 * the selection without inserting anything. When
3461 * compl_enter_selects is set the Enter key does the same. */
3462 if ((c == Ctrl_Y || (compl_enter_selects
3463 && (c == CAR || c == K_KENTER || c == NL)))
3464 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003465 retval = TRUE;
3466
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003467 /* CTRL-E means completion is Ended, go back to the typed text. */
3468 if (c == Ctrl_E)
3469 {
3470 ins_compl_delete();
3471 if (compl_leader != NULL)
3472 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3473 else if (compl_first_match != NULL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003474 ins_bytes(compl_orig_text
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003475 + curwin->w_cursor.col - compl_col);
3476 retval = TRUE;
3477 }
3478
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003480 compl_started = FALSE;
3481 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 msg_clr_cmdline(); /* necessary for "noshowmode" */
3483 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003484 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 if (edit_submode != NULL)
3486 {
3487 edit_submode = NULL;
3488 showmode();
3489 }
3490
3491#ifdef FEAT_CINDENT
3492 /*
3493 * Indent now if a key was typed that is in 'cinkeys'.
3494 */
3495 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3496 do_c_expr_indent();
3497#endif
3498 }
3499 }
3500
3501 /* reset continue_* if we left expansion-mode, if we stay they'll be
3502 * (re)set properly in ins_complete() */
3503 if (!vim_is_ctrl_x_key(c))
3504 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003505 compl_cont_status = 0;
3506 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003508
3509 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510}
3511
3512/*
3513 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3514 * (depending on flag) starting from buf and looking for a non-scanned
3515 * buffer (other than curbuf). curbuf is special, if it is called with
3516 * buf=curbuf then it has to be the first call for a given flag/expansion.
3517 *
3518 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3519 */
3520 static buf_T *
3521ins_compl_next_buf(buf, flag)
3522 buf_T *buf;
3523 int flag;
3524{
3525#ifdef FEAT_WINDOWS
3526 static win_T *wp;
3527#endif
3528
3529 if (flag == 'w') /* just windows */
3530 {
3531#ifdef FEAT_WINDOWS
3532 if (buf == curbuf) /* first call for this flag/expansion */
3533 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003534 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 && wp->w_buffer->b_scanned)
3536 ;
3537 buf = wp->w_buffer;
3538#else
3539 buf = curbuf;
3540#endif
3541 }
3542 else
3543 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3544 * (unlisted buffers)
3545 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003546 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 && ((flag == 'U'
3548 ? buf->b_p_bl
3549 : (!buf->b_p_bl
3550 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003551 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 ;
3553 return buf;
3554}
3555
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003556#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003557static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003558
3559/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003560 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003561 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003562 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003563 static void
3564expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003565 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003566 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003567{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003568 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003569 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003570 char_u *funcname;
3571 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003572
Bram Moolenaare344bea2005-09-01 20:46:49 +00003573 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3574 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003575 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003576
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003577 /* Call 'completefunc' to obtain the list of matches. */
3578 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003579 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003580
Bram Moolenaare344bea2005-09-01 20:46:49 +00003581 pos = curwin->w_cursor;
3582 matchlist = call_func_retlist(funcname, 2, args, FALSE);
3583 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003584 if (matchlist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003585 return;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003586
Bram Moolenaara94bc432006-03-10 21:42:59 +00003587 ins_compl_add_list(matchlist);
3588 list_unref(matchlist);
3589}
3590#endif /* FEAT_COMPL_FUNC */
3591
Bram Moolenaar39f05632006-03-19 22:15:26 +00003592#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00003593/*
3594 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00003595 */
3596 static void
3597ins_compl_add_list(list)
3598 list_T *list;
3599{
3600 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003601 int dir = compl_direction;
3602
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003603 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003604 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003605 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00003606 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
3607 /* if dir was BACKWARD then honor it just once */
3608 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00003609 else if (did_emsg)
3610 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003611 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003612}
Bram Moolenaar39f05632006-03-19 22:15:26 +00003613
3614/*
3615 * Add a match to the list of matches from a typeval_T.
3616 * If the given string is already in the list of completions, then return
3617 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
3618 * maybe because alloc() returns NULL, then FAIL is returned.
3619 */
3620 int
3621ins_compl_add_tv(tv, dir)
3622 typval_T *tv;
3623 int dir;
3624{
3625 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00003626 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00003627 int adup = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003628 char_u *(cptext[CPT_COUNT]);
3629
3630 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3631 {
3632 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
3633 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
3634 (char_u *)"abbr", FALSE);
3635 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
3636 (char_u *)"menu", FALSE);
3637 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
3638 (char_u *)"kind", FALSE);
3639 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
3640 (char_u *)"info", FALSE);
3641 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
3642 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003643 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00003644 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar39f05632006-03-19 22:15:26 +00003645 }
3646 else
3647 {
3648 word = get_tv_string_chk(tv);
3649 vim_memset(cptext, 0, sizeof(cptext));
3650 }
3651 if (word == NULL || *word == NUL)
3652 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00003653 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003654}
Bram Moolenaara94bc432006-03-10 21:42:59 +00003655#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003656
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003657/*
3658 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003659 * The search starts at position "ini" in curbuf and in the direction
3660 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003661 * When "compl_started" is FALSE start at that position, otherwise continue
3662 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003663 * This may return before finding all the matches.
3664 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 */
3666 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003667ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669{
3670 static pos_T first_match_pos;
3671 static pos_T last_match_pos;
3672 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003673 static int found_all = FALSE; /* Found all matches of a
3674 certain type. */
3675 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676
Bram Moolenaar572cb562005-08-05 21:35:02 +00003677 pos_T *pos;
3678 char_u **matches;
3679 int save_p_scs;
3680 int save_p_ws;
3681 int save_p_ic;
3682 int i;
3683 int num_matches;
3684 int len;
3685 int found_new_match;
3686 int type = ctrl_x_mode;
3687 char_u *ptr;
3688 char_u *dict = NULL;
3689 int dict_f = 0;
3690 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003692 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 {
3694 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3695 ins_buf->b_scanned = 0;
3696 found_all = FALSE;
3697 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003698 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003699 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 last_match_pos = first_match_pos = *ini;
3701 }
3702
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003703 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003704 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3706 for (;;)
3707 {
3708 found_new_match = FAIL;
3709
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003710 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 * or if found_all says this entry is done. For ^X^L only use the
3712 * entries from 'complete' that look in loaded buffers. */
3713 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003714 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 {
3716 found_all = FALSE;
3717 while (*e_cpt == ',' || *e_cpt == ' ')
3718 e_cpt++;
3719 if (*e_cpt == '.' && !curbuf->b_scanned)
3720 {
3721 ins_buf = curbuf;
3722 first_match_pos = *ini;
3723 /* So that ^N can match word immediately after cursor */
3724 if (ctrl_x_mode == 0)
3725 dec(&first_match_pos);
3726 last_match_pos = first_match_pos;
3727 type = 0;
3728 }
3729 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
3730 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
3731 {
3732 /* Scan a buffer, but not the current one. */
3733 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
3734 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003735 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 first_match_pos.col = last_match_pos.col = 0;
3737 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
3738 last_match_pos.lnum = 0;
3739 type = 0;
3740 }
3741 else /* unloaded buffer, scan like dictionary */
3742 {
3743 found_all = TRUE;
3744 if (ins_buf->b_fname == NULL)
3745 continue;
3746 type = CTRL_X_DICTIONARY;
3747 dict = ins_buf->b_fname;
3748 dict_f = DICT_EXACT;
3749 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00003750 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 ins_buf->b_fname == NULL
3752 ? buf_spname(ins_buf)
3753 : ins_buf->b_sfname == NULL
3754 ? (char *)ins_buf->b_fname
3755 : (char *)ins_buf->b_sfname);
3756 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3757 }
3758 else if (*e_cpt == NUL)
3759 break;
3760 else
3761 {
3762 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3763 type = -1;
3764 else if (*e_cpt == 'k' || *e_cpt == 's')
3765 {
3766 if (*e_cpt == 'k')
3767 type = CTRL_X_DICTIONARY;
3768 else
3769 type = CTRL_X_THESAURUS;
3770 if (*++e_cpt != ',' && *e_cpt != NUL)
3771 {
3772 dict = e_cpt;
3773 dict_f = DICT_FIRST;
3774 }
3775 }
3776#ifdef FEAT_FIND_ID
3777 else if (*e_cpt == 'i')
3778 type = CTRL_X_PATH_PATTERNS;
3779 else if (*e_cpt == 'd')
3780 type = CTRL_X_PATH_DEFINES;
3781#endif
3782 else if (*e_cpt == ']' || *e_cpt == 't')
3783 {
3784 type = CTRL_X_TAGS;
3785 sprintf((char*)IObuff, _("Scanning tags."));
3786 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3787 }
3788 else
3789 type = -1;
3790
3791 /* in any case e_cpt is advanced to the next entry */
3792 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
3793
3794 found_all = TRUE;
3795 if (type == -1)
3796 continue;
3797 }
3798 }
3799
3800 switch (type)
3801 {
3802 case -1:
3803 break;
3804#ifdef FEAT_FIND_ID
3805 case CTRL_X_PATH_PATTERNS:
3806 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003807 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003808 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003810 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3812 (linenr_T)1, (linenr_T)MAXLNUM);
3813 break;
3814#endif
3815
3816 case CTRL_X_DICTIONARY:
3817 case CTRL_X_THESAURUS:
3818 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00003819 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 : (type == CTRL_X_THESAURUS
3821 ? (*curbuf->b_p_tsr == NUL
3822 ? p_tsr
3823 : curbuf->b_p_tsr)
3824 : (*curbuf->b_p_dict == NUL
3825 ? p_dict
3826 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003827 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00003828 dict != NULL ? dict_f
3829 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 dict = NULL;
3831 break;
3832
3833 case CTRL_X_TAGS:
3834 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
3835 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003836 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837
3838 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003839 * of matches is found when compl_pattern is empty */
3840 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
3842 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
3843 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3844 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003845 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 }
3847 p_ic = save_p_ic;
3848 break;
3849
3850 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003851 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
3853 {
3854
3855 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003856 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003857 ins_compl_add_matches(num_matches, matches,
3858#ifdef CASE_INSENSITIVE_FILENAME
3859 TRUE
3860#else
3861 FALSE
3862#endif
3863 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 }
3865 break;
3866
3867 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003868 if (expand_cmdline(&compl_xp, compl_pattern,
3869 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003871 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 break;
3873
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003874#ifdef FEAT_COMPL_FUNC
3875 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003876 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003877 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003878 break;
3879#endif
3880
Bram Moolenaar488c6512005-08-11 20:09:58 +00003881 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003882#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00003883 num_matches = expand_spelling(first_match_pos.lnum,
3884 first_match_pos.col, compl_pattern, &matches);
3885 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003886 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00003887#endif
3888 break;
3889
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 default: /* normal ^P/^N and ^X^L */
3891 /*
3892 * If 'infercase' is set, don't use 'smartcase' here
3893 */
3894 save_p_scs = p_scs;
3895 if (ins_buf->b_p_inf)
3896 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003897
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 /* buffers other than curbuf are scanned from the beginning or the
3899 * end but never from the middle, thus setting nowrapscan in this
3900 * buffers is a good idea, on the other hand, we always set
3901 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
3902 save_p_ws = p_ws;
3903 if (ins_buf != curbuf)
3904 p_ws = FALSE;
3905 else if (*e_cpt == '.')
3906 p_ws = TRUE;
3907 for (;;)
3908 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003909 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003911 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
3912 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003914 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003916 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003918 found_new_match = searchit(NULL, ins_buf, pos,
3919 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003920 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003921 RE_LAST, (linenr_T)0);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003922 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003924 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003925 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 first_match_pos = *pos;
3927 last_match_pos = *pos;
3928 }
3929 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003930 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 found_new_match = FAIL;
3932 if (found_new_match == FAIL)
3933 {
3934 if (ins_buf == curbuf)
3935 found_all = TRUE;
3936 break;
3937 }
3938
3939 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003940 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 && ini->lnum == pos->lnum
3942 && ini->col == pos->col)
3943 continue;
3944 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
3945 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3946 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003947 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 {
3949 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3950 continue;
3951 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3952 if (!p_paste)
3953 ptr = skipwhite(ptr);
3954 }
3955 len = (int)STRLEN(ptr);
3956 }
3957 else
3958 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003959 char_u *tmp_ptr = ptr;
3960
3961 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003963 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 /* Skip if already inside a word. */
3965 if (vim_iswordp(tmp_ptr))
3966 continue;
3967 /* Find start of next word. */
3968 tmp_ptr = find_word_start(tmp_ptr);
3969 }
3970 /* Find end of this word. */
3971 tmp_ptr = find_word_end(tmp_ptr);
3972 len = (int)(tmp_ptr - ptr);
3973
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003974 if ((compl_cont_status & CONT_ADDING)
3975 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 {
3977 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3978 {
3979 /* Try next line, if any. the new word will be
3980 * "join" as if the normal command "J" was used.
3981 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003982 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 * works -- Acevedo */
3984 STRNCPY(IObuff, ptr, len);
3985 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3986 tmp_ptr = ptr = skipwhite(ptr);
3987 /* Find start of next word. */
3988 tmp_ptr = find_word_start(tmp_ptr);
3989 /* Find end of next word. */
3990 tmp_ptr = find_word_end(tmp_ptr);
3991 if (tmp_ptr > ptr)
3992 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003993 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003995 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 IObuff[len++] = ' ';
3997 /* IObuf =~ "\k.* ", thus len >= 2 */
3998 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003999 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 || (vim_strchr(p_cpo, CPO_JOINSP)
4001 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004002 && (IObuff[len - 2] == '?'
4003 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 IObuff[len++] = ' ';
4005 }
4006 /* copy as much as posible of the new word */
4007 if (tmp_ptr - ptr >= IOSIZE - len)
4008 tmp_ptr = ptr + IOSIZE - len - 1;
4009 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4010 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004011 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 }
4013 IObuff[len] = NUL;
4014 ptr = IObuff;
4015 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004016 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 continue;
4018 }
4019 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004020 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004021 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004022 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 {
4024 found_new_match = OK;
4025 break;
4026 }
4027 }
4028 p_scs = save_p_scs;
4029 p_ws = save_p_ws;
4030 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004031
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004032 /* check if compl_curr_match has changed, (e.g. other type of
4033 * expansion added somenthing) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004034 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 found_new_match = OK;
4036
4037 /* break the loop for specialized modes (use 'complete' just for the
4038 * generic ctrl_x_mode == 0) or when we've found a new match */
4039 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004040 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004041 {
4042 if (got_int)
4043 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004044 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004045 if (type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004046 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004047
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004048 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
4049 || compl_interrupted)
4050 break;
4051 compl_started = TRUE;
4052 }
4053 else
4054 {
4055 /* Mark a buffer scanned when it has been scanned completely */
4056 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4057 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004059 compl_started = FALSE;
4060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004062 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063
4064 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
4065 && *e_cpt == NUL) /* Got to end of 'complete' */
4066 found_new_match = FAIL;
4067
4068 i = -1; /* total of matches, unknown */
4069 if (found_new_match == FAIL
4070 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
4071 i = ins_compl_make_cyclic();
4072
4073 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004074 * just been made cyclic then we have to move compl_curr_match to the next
4075 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004076 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4077 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004078 if (compl_curr_match == NULL)
4079 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 return i;
4081}
4082
4083/* Delete the old text being completed. */
4084 static void
4085ins_compl_delete()
4086{
4087 int i;
4088
4089 /*
4090 * In insert mode: Delete the typed part.
4091 * In replace mode: Put the old characters back, if any.
4092 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004093 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 backspace_until_column(i);
4095 changed_cline_bef_curs();
4096}
4097
4098/* Insert the new text being completed. */
4099 static void
4100ins_compl_insert()
4101{
Bram Moolenaar572cb562005-08-05 21:35:02 +00004102 ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004103 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4104 compl_used_match = FALSE;
4105 else
4106 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107}
4108
4109/*
4110 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004111 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4112 * get more completions. If it is FALSE, then we just do nothing when there
4113 * are no more completions in a given direction. The latter case is used when
4114 * we are still in the middle of finding completions, to allow browsing
4115 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 * Return the total number of matches, or -1 if still unknown -- webb.
4117 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004118 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4119 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 *
4121 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004122 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4123 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 */
4125 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004126ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004128 int count; /* repeat completion this many times; should
4129 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004130 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131{
4132 int num_matches = -1;
4133 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004134 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004135 compl_T *found_compl = NULL;
4136 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004137 int advance;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004139 if (compl_leader != NULL
4140 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004142 /* Set "compl_shown_match" to the actually shown match, it may differ
4143 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004144 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004145 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004146 && compl_shown_match->cp_next != NULL
4147 && compl_shown_match->cp_next != compl_first_match)
4148 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004149
4150 /* If we didn't find it searching forward, and compl_shows_dir is
4151 * backward, find the last match. */
4152 if (compl_shows_dir == BACKWARD
4153 && !ins_compl_equal(compl_shown_match,
4154 compl_leader, (int)STRLEN(compl_leader))
4155 && (compl_shown_match->cp_next == NULL
4156 || compl_shown_match->cp_next == compl_first_match))
4157 {
4158 while (!ins_compl_equal(compl_shown_match,
4159 compl_leader, (int)STRLEN(compl_leader))
4160 && compl_shown_match->cp_prev != NULL
4161 && compl_shown_match->cp_prev != compl_first_match)
4162 compl_shown_match = compl_shown_match->cp_prev;
4163 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004164 }
4165
4166 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004167 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 /* Delete old text to be replaced */
4169 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004170
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004171 /* When finding the longest common text we stick at the original text,
4172 * don't let CTRL-N or CTRL-P move to the first match. */
4173 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4174
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004175 /* When restarting the search don't insert the first match either. */
4176 if (compl_restarting)
4177 {
4178 advance = FALSE;
4179 compl_restarting = FALSE;
4180 }
4181
Bram Moolenaare3226be2005-12-18 22:10:00 +00004182 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4183 * around. */
4184 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004186 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004188 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004189 found_end = (compl_first_match != NULL
4190 && (compl_shown_match->cp_next == compl_first_match
4191 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004192 }
4193 else if (compl_shows_dir == BACKWARD
4194 && compl_shown_match->cp_prev != NULL)
4195 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004196 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004197 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004198 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 }
4200 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004201 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004202 if (!allow_get_expansion)
4203 {
4204 if (advance)
4205 {
4206 if (compl_shows_dir == BACKWARD)
4207 compl_pending -= todo + 1;
4208 else
4209 compl_pending += todo + 1;
4210 }
4211 return -1;
4212 }
4213
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004214 if (advance)
4215 {
4216 if (compl_shows_dir == BACKWARD)
4217 --compl_pending;
4218 else
4219 ++compl_pending;
4220 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004221
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004222 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004223 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004224
4225 /* handle any pending completions */
4226 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004227 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004228 {
4229 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4230 {
4231 compl_shown_match = compl_shown_match->cp_next;
4232 --compl_pending;
4233 }
4234 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4235 {
4236 compl_shown_match = compl_shown_match->cp_prev;
4237 ++compl_pending;
4238 }
4239 else
4240 break;
4241 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004242 found_end = FALSE;
4243 }
4244 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4245 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004246 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004247 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004248 ++todo;
4249 else
4250 /* Remember a matching item. */
4251 found_compl = compl_shown_match;
4252
4253 /* Stop at the end of the list when we found a usable match. */
4254 if (found_end)
4255 {
4256 if (found_compl != NULL)
4257 {
4258 compl_shown_match = found_compl;
4259 break;
4260 }
4261 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 }
4264
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004265 /* Insert the text of the new completion, or the compl_leader. */
4266 if (insert_match)
4267 {
4268 if (!compl_get_longest || compl_used_match)
4269 ins_compl_insert();
4270 else
4271 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
4272 }
4273 else
4274 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275
4276 if (!allow_get_expansion)
4277 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004278 /* may undisplay the popup menu first */
4279 ins_compl_upd_pum();
4280
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004281 /* redraw to show the user what was inserted */
4282 update_screen(0);
4283
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004284 /* display the updated popup menu */
4285 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004286#ifdef FEAT_GUI
4287 if (gui.in_use)
4288 {
4289 /* Show the cursor after the match, not after the redrawn text. */
4290 setcursor();
4291 out_flush();
4292 gui_update_cursor(FALSE, FALSE);
4293 }
4294#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004295
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 /* Delete old text to be replaced, since we're still searching and
4297 * don't want to match ourselves! */
4298 ins_compl_delete();
4299 }
4300
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004301 /* Enter will select a match when the match wasn't inserted and the popup
4302 * menu is visislbe. */
4303 compl_enter_selects = !insert_match && compl_match_array != NULL;
4304
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 /*
4306 * Show the file name for the match (if any)
4307 * Truncate the file name to avoid a wait for return.
4308 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004309 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 {
4311 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004312 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 if (i <= 0)
4314 i = 0;
4315 else
4316 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004317 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 msg(IObuff);
4319 redraw_cmdline = FALSE; /* don't overwrite! */
4320 }
4321
4322 return num_matches;
4323}
4324
4325/*
4326 * Call this while finding completions, to check whether the user has hit a key
4327 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004328 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004330 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 */
4332 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004333ins_compl_check_keys(frequency)
4334 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335{
4336 static int count = 0;
4337
4338 int c;
4339
4340 /* Don't check when reading keys from a script. That would break the test
4341 * scripts */
4342 if (using_script())
4343 return;
4344
4345 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004346 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 return;
4348 count = 0;
4349
Bram Moolenaara260a972006-06-23 19:36:29 +00004350 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4351 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 if (c != NUL)
4354 {
4355 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4356 {
4357 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004358 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004359 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4360 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004362 else
4363 {
4364 /* Need to get the character to have KeyTyped set. We'll put it
4365 * back with vungetc() below. */
4366 c = safe_vgetc();
4367
4368 /* Don't interrupt completion when the character wasn't typed,
4369 * e.g., when doing @q to replay keys. */
4370 if (c != Ctrl_R && KeyTyped)
4371 compl_interrupted = TRUE;
4372
4373 vungetc(c);
4374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004376 if (compl_pending != 0 && !got_int)
Bram Moolenaara260a972006-06-23 19:36:29 +00004377 {
4378 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4379
4380 compl_pending = 0;
4381 (void)ins_compl_next(FALSE, todo, TRUE);
4382 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004383}
4384
4385/*
4386 * Decide the direction of Insert mode complete from the key typed.
4387 * Returns BACKWARD or FORWARD.
4388 */
4389 static int
4390ins_compl_key2dir(c)
4391 int c;
4392{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004393 if (c == Ctrl_P || c == Ctrl_L
4394 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4395 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004396 return BACKWARD;
4397 return FORWARD;
4398}
4399
4400/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004401 * Return TRUE for keys that are used for completion only when the popup menu
4402 * is visible.
4403 */
4404 static int
4405ins_compl_pum_key(c)
4406 int c;
4407{
4408 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004409 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4410 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004411}
4412
4413/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004414 * Decide the number of completions to move forward.
4415 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4416 */
4417 static int
4418ins_compl_key2count(c)
4419 int c;
4420{
4421 int h;
4422
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004423 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004424 {
4425 h = pum_get_height();
4426 if (h > 3)
4427 h -= 2; /* keep some context */
4428 return h;
4429 }
4430 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431}
4432
4433/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004434 * Return TRUE if completion with "c" should insert the match, FALSE if only
4435 * to change the currently selected completion.
4436 */
4437 static int
4438ins_compl_use_match(c)
4439 int c;
4440{
4441 switch (c)
4442 {
4443 case K_UP:
4444 case K_DOWN:
4445 case K_PAGEDOWN:
4446 case K_KPAGEDOWN:
4447 case K_S_DOWN:
4448 case K_PAGEUP:
4449 case K_KPAGEUP:
4450 case K_S_UP:
4451 return FALSE;
4452 }
4453 return TRUE;
4454}
4455
4456/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 * Do Insert mode completion.
4458 * Called when character "c" was typed, which has a meaning for completion.
4459 * Returns OK if completion was done, FAIL if something failed (out of mem).
4460 */
4461 static int
4462ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004463 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004465 char_u *line;
4466 int startcol = 0; /* column where searched text starts */
4467 colnr_T curs_col; /* cursor column */
4468 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469
Bram Moolenaare3226be2005-12-18 22:10:00 +00004470 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004471 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 {
4473 /* First time we hit ^N or ^P (in a row, I mean) */
4474
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 did_ai = FALSE;
4476#ifdef FEAT_SMARTINDENT
4477 did_si = FALSE;
4478 can_si = FALSE;
4479 can_si_back = FALSE;
4480#endif
4481 if (stop_arrow() == FAIL)
4482 return FAIL;
4483
4484 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004485 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004486 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487
4488 /* if this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004489 * "compl_startpos" to the cursor as a pattern to add a new word
4490 * instead of expand the one before the cursor, in word-wise if
4491 * "compl_startpos"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 * is not in the same line as the cursor then fix it (the line has
4493 * been split because it was longer than 'tw'). if SOL is set then
4494 * skip the previous pattern, a word at the beginning of the line has
4495 * been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004496 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4497 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 {
4499 /*
4500 * it is a continued search
4501 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004502 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4504 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4505 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004506 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004508 /* line (probably) wrapped, set compl_startpos to the
4509 * first non_blank in the line, if it is not a wordchar
4510 * include it to get a better pattern, but then we don't
4511 * want the "\\<" prefix, check it bellow */
4512 compl_col = (colnr_T)(skipwhite(line) - line);
4513 compl_startpos.col = compl_col;
4514 compl_startpos.lnum = curwin->w_cursor.lnum;
4515 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 }
4517 else
4518 {
4519 /* S_IPOS was set when we inserted a word that was at the
4520 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004521 * mode but first we need to redefine compl_startpos */
4522 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004524 compl_cont_status |= CONT_SOL;
4525 compl_startpos.col = (colnr_T)(skipwhite(
4526 line + compl_length
4527 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004529 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004531 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004532 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 * have enough space? just being paranoic */
4534#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004535 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004537 compl_cont_status &= ~CONT_SOL;
4538 compl_length = (IOSIZE - MIN_SPACE);
4539 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004541 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4542 if (compl_length < 1)
4543 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 }
4545 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004546 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004548 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 }
4550 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004551 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004553 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004555 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004557 compl_cont_status = 0;
4558 compl_cont_status |= CONT_N_ADDS;
4559 compl_startpos = curwin->w_cursor;
4560 startcol = (int)curs_col;
4561 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 }
4563
4564 /* Work out completion pattern and original text -- webb */
4565 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4566 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004567 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4569 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004570 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004572 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004574 compl_col += ++startcol;
4575 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 }
4577 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004578 compl_pattern = str_foldcase(line + compl_col,
4579 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004581 compl_pattern = vim_strnsave(line + compl_col,
4582 compl_length);
4583 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 return FAIL;
4585 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004586 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 {
4588 char_u *prefix = (char_u *)"\\<";
4589
4590 /* we need 3 extra chars, 1 for the NUL and
4591 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004592 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4593 compl_length) + 3);
4594 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004596 if (!vim_iswordp(line + compl_col)
4597 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 && (
4599#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004600 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004602 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603#endif
4604 )))
4605 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004606 STRCPY((char *)compl_pattern, prefix);
4607 (void)quote_meta(compl_pattern + STRLEN(prefix),
4608 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004610 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004612 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004614 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615#endif
4616 )
4617 {
4618 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004619 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4620 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004622 compl_col += curs_col;
4623 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 }
4625 else
4626 {
4627#ifdef FEAT_MBYTE
4628 /* Search the point of change class of multibyte character
4629 * or not a word single byte character backward. */
4630 if (has_mbyte)
4631 {
4632 int base_class;
4633 int head_off;
4634
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004635 startcol -= (*mb_head_off)(line, line + startcol);
4636 base_class = mb_get_class(line + startcol);
4637 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004639 head_off = (*mb_head_off)(line, line + startcol);
4640 if (base_class != mb_get_class(line + startcol
4641 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004643 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 }
4645 }
4646 else
4647#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004648 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004650 compl_col += ++startcol;
4651 compl_length = (int)curs_col - startcol;
4652 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 {
4654 /* Only match word with at least two chars -- webb
4655 * there's no need to call quote_meta,
4656 * alloc(7) is enough -- Acevedo
4657 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004658 compl_pattern = alloc(7);
4659 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004661 STRCPY((char *)compl_pattern, "\\<");
4662 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4663 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 }
4665 else
4666 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004667 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4668 compl_length) + 3);
4669 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004671 STRCPY((char *)compl_pattern, "\\<");
4672 (void)quote_meta(compl_pattern + 2, line + compl_col,
4673 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 }
4675 }
4676 }
4677 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4678 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004679 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004680 compl_length = (int)curs_col - (int)compl_col;
4681 if (compl_length < 0) /* cursor in indent: empty pattern */
4682 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004684 compl_pattern = str_foldcase(line + compl_col, compl_length,
4685 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004687 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4688 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 return FAIL;
4690 }
4691 else if (ctrl_x_mode == CTRL_X_FILES)
4692 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004693 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004695 compl_col += ++startcol;
4696 compl_length = (int)curs_col - startcol;
4697 compl_pattern = addstar(line + compl_col, compl_length,
4698 EXPAND_FILES);
4699 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 return FAIL;
4701 }
4702 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4703 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004704 compl_pattern = vim_strnsave(line, curs_col);
4705 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004707 set_cmd_context(&compl_xp, compl_pattern,
4708 (int)STRLEN(compl_pattern), curs_col);
4709 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4710 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00004711 /* No completion possible, use an empty pattern to get a
4712 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004713 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004714 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00004715 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
4716 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004718 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004719 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00004720#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004721 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00004722 * Call user defined function 'completefunc' with "a:findstart"
4723 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004724 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00004725 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004726 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004727 char_u *funcname;
4728 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004729
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004730 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004731 * string */
4732 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4733 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4734 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004735 {
4736 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
4737 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004738 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004739 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004740
4741 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004742 args[1] = NULL;
4743 pos = curwin->w_cursor;
4744 col = call_func_retnr(funcname, 2, args, FALSE);
4745 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004746
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004747 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004748 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004749 compl_col = col;
4750 if ((colnr_T)compl_col > curs_col)
4751 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004752
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004753 /* Setup variables for completion. Need to obtain "line" again,
4754 * it may have become invalid. */
4755 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004756 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004757 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4758 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004759#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004760 return FAIL;
4761 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00004762 else if (ctrl_x_mode == CTRL_X_SPELL)
4763 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004764#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004765 if (spell_bad_len > 0)
4766 compl_col = curs_col - spell_bad_len;
4767 else
4768 compl_col = spell_word_start(startcol);
4769 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004770 {
4771 compl_length = 0;
4772 compl_col = curs_col;
4773 }
4774 else
4775 {
4776 spell_expand_check_cap(compl_col);
4777 compl_length = (int)curs_col - compl_col;
4778 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00004779 /* Need to obtain "line" again, it may have become invalid. */
4780 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004781 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4782 if (compl_pattern == NULL)
4783#endif
4784 return FAIL;
4785 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004786 else
4787 {
4788 EMSG2(_(e_intern2), "ins_complete()");
4789 return FAIL;
4790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004792 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 {
4794 edit_submode_pre = (char_u *)_(" Adding");
4795 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4796 {
4797 /* Insert a new line, keep indentation but ignore 'comments' */
4798#ifdef FEAT_COMMENTS
4799 char_u *old = curbuf->b_p_com;
4800
4801 curbuf->b_p_com = (char_u *)"";
4802#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004803 compl_startpos.lnum = curwin->w_cursor.lnum;
4804 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 ins_eol('\r');
4806#ifdef FEAT_COMMENTS
4807 curbuf->b_p_com = old;
4808#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004809 compl_length = 0;
4810 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 }
4812 }
4813 else
4814 {
4815 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004816 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 }
4818
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004819 if (compl_cont_status & CONT_LOCAL)
4820 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 else
4822 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4823
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004824 /* Always add completion for the original text. */
4825 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004826 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4827 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004828 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004830 vim_free(compl_pattern);
4831 compl_pattern = NULL;
4832 vim_free(compl_orig_text);
4833 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 return FAIL;
4835 }
4836
4837 /* showmode might reset the internal line pointers, so it must
4838 * be called before line = ml_get(), or when this address is no
4839 * longer needed. -- Acevedo.
4840 */
4841 edit_submode_extra = (char_u *)_("-- Searching...");
4842 edit_submode_highl = HLF_COUNT;
4843 showmode();
4844 edit_submode_extra = NULL;
4845 out_flush();
4846 }
4847
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004848 compl_shown_match = compl_curr_match;
4849 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850
4851 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004852 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004854 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004856 /* may undisplay the popup menu */
4857 ins_compl_upd_pum();
4858
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004859 if (n > 1) /* all matches have been found */
4860 compl_matches = n;
4861 compl_curr_match = compl_shown_match;
4862 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863
Bram Moolenaard68071d2006-05-02 22:08:30 +00004864 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
4865 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 if (got_int && !global_busy)
4867 {
4868 (void)vgetc();
4869 got_int = FALSE;
4870 }
4871
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004872 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004873 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004875 edit_submode_extra = (compl_cont_status & CONT_ADDING)
4876 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
4878 edit_submode_highl = HLF_E;
4879 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4880 * because we couldn't expand anything at first place, but if we used
4881 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4882 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004883 if ( compl_length > 1
4884 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 || (ctrl_x_mode != 0
4886 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
4887 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004888 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 }
4890
Bram Moolenaar572cb562005-08-05 21:35:02 +00004891 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004892 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004894 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895
4896 if (edit_submode_extra == NULL)
4897 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004898 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 {
4900 edit_submode_extra = (char_u *)_("Back at original");
4901 edit_submode_highl = HLF_W;
4902 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004903 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 {
4905 edit_submode_extra = (char_u *)_("Word from other line");
4906 edit_submode_highl = HLF_COUNT;
4907 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004908 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 {
4910 edit_submode_extra = (char_u *)_("The only match");
4911 edit_submode_highl = HLF_COUNT;
4912 }
4913 else
4914 {
4915 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004916 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004918 int number = 0;
4919 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004921 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 {
4923 /* search backwards for the first valid (!= -1) number.
4924 * This should normally succeed already at the first loop
4925 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004926 for (match = compl_curr_match->cp_prev; match != NULL
4927 && match != compl_first_match;
4928 match = match->cp_prev)
4929 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004931 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 break;
4933 }
4934 if (match != NULL)
4935 /* go up and assign all numbers which are not assigned
4936 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004937 for (match = match->cp_next;
4938 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004939 match = match->cp_next)
4940 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 }
4942 else /* BACKWARD */
4943 {
4944 /* search forwards (upwards) for the first valid (!= -1)
4945 * number. This should normally succeed already at the
4946 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004947 for (match = compl_curr_match->cp_next; match != NULL
4948 && match != compl_first_match;
4949 match = match->cp_next)
4950 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004952 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 break;
4954 }
4955 if (match != NULL)
4956 /* go down and assign all numbers which are not
4957 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004958 for (match = match->cp_prev; match
4959 && match->cp_number == -1;
4960 match = match->cp_prev)
4961 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 }
4963 }
4964
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004965 /* The match should always have a sequence number now, this is
4966 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004967 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 {
4969 /* Space for 10 text chars. + 2x10-digit no.s */
4970 static char_u match_ref[31];
4971
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004972 if (compl_matches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 sprintf((char *)IObuff, _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004974 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004976 sprintf((char *)IObuff, _("match %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004977 compl_curr_match->cp_number);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004978 vim_strncpy(match_ref, IObuff, 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 edit_submode_extra = match_ref;
4980 edit_submode_highl = HLF_R;
4981 if (dollar_vcol)
4982 curs_columns(FALSE);
4983 }
4984 }
4985 }
4986
4987 /* Show a message about what (completion) mode we're in. */
4988 showmode();
4989 if (edit_submode_extra != NULL)
4990 {
4991 if (!p_smd)
4992 msg_attr(edit_submode_extra,
4993 edit_submode_highl < HLF_COUNT
4994 ? hl_attr(edit_submode_highl) : 0);
4995 }
4996 else
4997 msg_clr_cmdline(); /* necessary for "noshowmode" */
4998
Bram Moolenaard68071d2006-05-02 22:08:30 +00004999 /* Show the popup menu, unless we got interrupted. */
5000 if (!compl_interrupted)
5001 {
5002 /* RedrawingDisabled may be set when invoked through complete(). */
5003 n = RedrawingDisabled;
5004 RedrawingDisabled = 0;
5005 ins_compl_show_pum();
5006 setcursor();
5007 RedrawingDisabled = n;
5008 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005009 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005010 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005011
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 return OK;
5013}
5014
5015/*
5016 * Looks in the first "len" chars. of "src" for search-metachars.
5017 * If dest is not NULL the chars. are copied there quoting (with
5018 * a backslash) the metachars, and dest would be NUL terminated.
5019 * Returns the length (needed) of dest
5020 */
5021 static int
5022quote_meta(dest, src, len)
5023 char_u *dest;
5024 char_u *src;
5025 int len;
5026{
5027 int m;
5028
5029 for (m = len; --len >= 0; src++)
5030 {
5031 switch (*src)
5032 {
5033 case '.':
5034 case '*':
5035 case '[':
5036 if (ctrl_x_mode == CTRL_X_DICTIONARY
5037 || ctrl_x_mode == CTRL_X_THESAURUS)
5038 break;
5039 case '~':
5040 if (!p_magic) /* quote these only if magic is set */
5041 break;
5042 case '\\':
5043 if (ctrl_x_mode == CTRL_X_DICTIONARY
5044 || ctrl_x_mode == CTRL_X_THESAURUS)
5045 break;
5046 case '^': /* currently it's not needed. */
5047 case '$':
5048 m++;
5049 if (dest != NULL)
5050 *dest++ = '\\';
5051 break;
5052 }
5053 if (dest != NULL)
5054 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005055# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 /* Copy remaining bytes of a multibyte character. */
5057 if (has_mbyte)
5058 {
5059 int i, mb_len;
5060
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005061 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 if (mb_len > 0 && len >= mb_len)
5063 for (i = 0; i < mb_len; ++i)
5064 {
5065 --len;
5066 ++src;
5067 if (dest != NULL)
5068 *dest++ = *src;
5069 }
5070 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005071# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 }
5073 if (dest != NULL)
5074 *dest = NUL;
5075
5076 return m;
5077}
5078#endif /* FEAT_INS_EXPAND */
5079
5080/*
5081 * Next character is interpreted literally.
5082 * A one, two or three digit decimal number is interpreted as its byte value.
5083 * If one or two digits are entered, the next character is given to vungetc().
5084 * For Unicode a character > 255 may be returned.
5085 */
5086 int
5087get_literal()
5088{
5089 int cc;
5090 int nc;
5091 int i;
5092 int hex = FALSE;
5093 int octal = FALSE;
5094#ifdef FEAT_MBYTE
5095 int unicode = 0;
5096#endif
5097
5098 if (got_int)
5099 return Ctrl_C;
5100
5101#ifdef FEAT_GUI
5102 /*
5103 * In GUI there is no point inserting the internal code for a special key.
5104 * It is more useful to insert the string "<KEY>" instead. This would
5105 * probably be useful in a text window too, but it would not be
5106 * vi-compatible (maybe there should be an option for it?) -- webb
5107 */
5108 if (gui.in_use)
5109 ++allow_keys;
5110#endif
5111#ifdef USE_ON_FLY_SCROLL
5112 dont_scroll = TRUE; /* disallow scrolling here */
5113#endif
5114 ++no_mapping; /* don't map the next key hits */
5115 cc = 0;
5116 i = 0;
5117 for (;;)
5118 {
5119 do
5120 nc = safe_vgetc();
5121 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
5122 || nc == K_HOR_SCROLLBAR);
5123#ifdef FEAT_CMDL_INFO
5124 if (!(State & CMDLINE)
5125# ifdef FEAT_MBYTE
5126 && MB_BYTE2LEN_CHECK(nc) == 1
5127# endif
5128 )
5129 add_to_showcmd(nc);
5130#endif
5131 if (nc == 'x' || nc == 'X')
5132 hex = TRUE;
5133 else if (nc == 'o' || nc == 'O')
5134 octal = TRUE;
5135#ifdef FEAT_MBYTE
5136 else if (nc == 'u' || nc == 'U')
5137 unicode = nc;
5138#endif
5139 else
5140 {
5141 if (hex
5142#ifdef FEAT_MBYTE
5143 || unicode != 0
5144#endif
5145 )
5146 {
5147 if (!vim_isxdigit(nc))
5148 break;
5149 cc = cc * 16 + hex2nr(nc);
5150 }
5151 else if (octal)
5152 {
5153 if (nc < '0' || nc > '7')
5154 break;
5155 cc = cc * 8 + nc - '0';
5156 }
5157 else
5158 {
5159 if (!VIM_ISDIGIT(nc))
5160 break;
5161 cc = cc * 10 + nc - '0';
5162 }
5163
5164 ++i;
5165 }
5166
5167 if (cc > 255
5168#ifdef FEAT_MBYTE
5169 && unicode == 0
5170#endif
5171 )
5172 cc = 255; /* limit range to 0-255 */
5173 nc = 0;
5174
5175 if (hex) /* hex: up to two chars */
5176 {
5177 if (i >= 2)
5178 break;
5179 }
5180#ifdef FEAT_MBYTE
5181 else if (unicode) /* Unicode: up to four or eight chars */
5182 {
5183 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5184 break;
5185 }
5186#endif
5187 else if (i >= 3) /* decimal or octal: up to three chars */
5188 break;
5189 }
5190 if (i == 0) /* no number entered */
5191 {
5192 if (nc == K_ZERO) /* NUL is stored as NL */
5193 {
5194 cc = '\n';
5195 nc = 0;
5196 }
5197 else
5198 {
5199 cc = nc;
5200 nc = 0;
5201 }
5202 }
5203
5204 if (cc == 0) /* NUL is stored as NL */
5205 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005206#ifdef FEAT_MBYTE
5207 if (enc_dbcs && (cc & 0xff) == 0)
5208 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5209 second byte will cause trouble! */
5210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211
5212 --no_mapping;
5213#ifdef FEAT_GUI
5214 if (gui.in_use)
5215 --allow_keys;
5216#endif
5217 if (nc)
5218 vungetc(nc);
5219 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5220 return cc;
5221}
5222
5223/*
5224 * Insert character, taking care of special keys and mod_mask
5225 */
5226 static void
5227insert_special(c, allow_modmask, ctrlv)
5228 int c;
5229 int allow_modmask;
5230 int ctrlv; /* c was typed after CTRL-V */
5231{
5232 char_u *p;
5233 int len;
5234
5235 /*
5236 * Special function key, translate into "<Key>". Up to the last '>' is
5237 * inserted with ins_str(), so as not to replace characters in replace
5238 * mode.
5239 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5240 * unless 'allow_modmask' is TRUE.
5241 */
5242#ifdef MACOS
5243 /* Command-key never produces a normal key */
5244 if (mod_mask & MOD_MASK_CMD)
5245 allow_modmask = TRUE;
5246#endif
5247 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5248 {
5249 p = get_special_key_name(c, mod_mask);
5250 len = (int)STRLEN(p);
5251 c = p[len - 1];
5252 if (len > 2)
5253 {
5254 if (stop_arrow() == FAIL)
5255 return;
5256 p[len - 1] = NUL;
5257 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005258 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 ctrlv = FALSE;
5260 }
5261 }
5262 if (stop_arrow() == OK)
5263 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5264}
5265
5266/*
5267 * Special characters in this context are those that need processing other
5268 * than the simple insertion that can be performed here. This includes ESC
5269 * which terminates the insert, and CR/NL which need special processing to
5270 * open up a new line. This routine tries to optimize insertions performed by
5271 * the "redo", "undo" or "put" commands, so it needs to know when it should
5272 * stop and defer processing to the "normal" mechanism.
5273 * '0' and '^' are special, because they can be followed by CTRL-D.
5274 */
5275#ifdef EBCDIC
5276# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5277#else
5278# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5279#endif
5280
5281#ifdef FEAT_MBYTE
5282# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5283#else
5284# define WHITECHAR(cc) vim_iswhite(cc)
5285#endif
5286
5287 void
5288insertchar(c, flags, second_indent)
5289 int c; /* character to insert or NUL */
5290 int flags; /* INSCHAR_FORMAT, etc. */
5291 int second_indent; /* indent for second line if >= 0 */
5292{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 int textwidth;
5294#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298
5299 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5300 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301
5302 /*
5303 * Try to break the line in two or more pieces when:
5304 * - Always do this if we have been called to do formatting only.
5305 * - Always do this when 'formatoptions' has the 'a' flag and the line
5306 * ends in white space.
5307 * - Otherwise:
5308 * - Don't do this if inserting a blank
5309 * - Don't do this if an existing character is being replaced, unless
5310 * we're in VREPLACE mode.
5311 * - Do this if the cursor is not on the line where insert started
5312 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5313 * before the insert.
5314 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5315 * before 'textwidth'
5316 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005317 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 && ((flags & INSCHAR_FORMAT)
5319 || (!vim_iswhite(c)
5320 && !((State & REPLACE_FLAG)
5321#ifdef FEAT_VREPLACE
5322 && !(State & VREPLACE_FLAG)
5323#endif
5324 && *ml_get_cursor() != NUL)
5325 && (curwin->w_cursor.lnum != Insstart.lnum
5326 || ((!has_format_option(FO_INS_LONG)
5327 || Insstart_textlen <= (colnr_T)textwidth)
5328 && (!fo_ins_blank
5329 || Insstart_blank_vcol <= (colnr_T)textwidth
5330 ))))))
5331 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005332 /* Format with 'formatexpr' when it's set. Use internal formatting
5333 * when 'formatexpr' isn't set or it returns non-zero. */
5334#if defined(FEAT_EVAL)
5335 if (*curbuf->b_p_fex == NUL
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005336 || fex_format(curwin->w_cursor.lnum, 1L, c) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005338 internal_format(textwidth, second_indent, flags, c == NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005340
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 if (c == NUL) /* only formatting was wanted */
5342 return;
5343
5344#ifdef FEAT_COMMENTS
5345 /* Check whether this character should end a comment. */
5346 if (did_ai && (int)c == end_comment_pending)
5347 {
5348 char_u *line;
5349 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5350 int middle_len, end_len;
5351 int i;
5352
5353 /*
5354 * Need to remove existing (middle) comment leader and insert end
5355 * comment leader. First, check what comment leader we can find.
5356 */
5357 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5358 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5359 {
5360 /* Skip middle-comment string */
5361 while (*p && p[-1] != ':') /* find end of middle flags */
5362 ++p;
5363 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5364 /* Don't count trailing white space for middle_len */
5365 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5366 --middle_len;
5367
5368 /* Find the end-comment string */
5369 while (*p && p[-1] != ':') /* find end of end flags */
5370 ++p;
5371 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5372
5373 /* Skip white space before the cursor */
5374 i = curwin->w_cursor.col;
5375 while (--i >= 0 && vim_iswhite(line[i]))
5376 ;
5377 i++;
5378
5379 /* Skip to before the middle leader */
5380 i -= middle_len;
5381
5382 /* Check some expected things before we go on */
5383 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5384 {
5385 /* Backspace over all the stuff we want to replace */
5386 backspace_until_column(i);
5387
5388 /*
5389 * Insert the end-comment string, except for the last
5390 * character, which will get inserted as normal later.
5391 */
5392 ins_bytes_len(lead_end, end_len - 1);
5393 }
5394 }
5395 }
5396 end_comment_pending = NUL;
5397#endif
5398
5399 did_ai = FALSE;
5400#ifdef FEAT_SMARTINDENT
5401 did_si = FALSE;
5402 can_si = FALSE;
5403 can_si_back = FALSE;
5404#endif
5405
5406 /*
5407 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5408 * This speeds up normal text input considerably.
5409 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5410 * need to re-indent at a ':', or any other character (but not what
5411 * 'paste' is set)..
5412 */
5413#ifdef USE_ON_FLY_SCROLL
5414 dont_scroll = FALSE; /* allow scrolling here */
5415#endif
5416
5417 if ( !ISSPECIAL(c)
5418#ifdef FEAT_MBYTE
5419 && (!has_mbyte || (*mb_char2len)(c) == 1)
5420#endif
5421 && vpeekc() != NUL
5422 && !(State & REPLACE_FLAG)
5423#ifdef FEAT_CINDENT
5424 && !cindent_on()
5425#endif
5426#ifdef FEAT_RIGHTLEFT
5427 && !p_ri
5428#endif
5429 )
5430 {
5431#define INPUT_BUFLEN 100
5432 char_u buf[INPUT_BUFLEN + 1];
5433 int i;
5434 colnr_T virtcol = 0;
5435
5436 buf[0] = c;
5437 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005438 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 virtcol = get_nolist_virtcol();
5440 /*
5441 * Stop the string when:
5442 * - no more chars available
5443 * - finding a special character (command key)
5444 * - buffer is full
5445 * - running into the 'textwidth' boundary
5446 * - need to check for abbreviation: A non-word char after a word-char
5447 */
5448 while ( (c = vpeekc()) != NUL
5449 && !ISSPECIAL(c)
5450#ifdef FEAT_MBYTE
5451 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5452#endif
5453 && i < INPUT_BUFLEN
5454 && (textwidth == 0
5455 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5456 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5457 {
5458#ifdef FEAT_RIGHTLEFT
5459 c = vgetc();
5460 if (p_hkmap && KeyTyped)
5461 c = hkmap(c); /* Hebrew mode mapping */
5462# ifdef FEAT_FKMAP
5463 if (p_fkmap && KeyTyped)
5464 c = fkmap(c); /* Farsi mode mapping */
5465# endif
5466 buf[i++] = c;
5467#else
5468 buf[i++] = vgetc();
5469#endif
5470 }
5471
5472#ifdef FEAT_DIGRAPHS
5473 do_digraph(-1); /* clear digraphs */
5474 do_digraph(buf[i-1]); /* may be the start of a digraph */
5475#endif
5476 buf[i] = NUL;
5477 ins_str(buf);
5478 if (flags & INSCHAR_CTRLV)
5479 {
5480 redo_literal(*buf);
5481 i = 1;
5482 }
5483 else
5484 i = 0;
5485 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005486 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 }
5488 else
5489 {
5490#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005491 int cc;
5492
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5494 {
5495 char_u buf[MB_MAXBYTES + 1];
5496
5497 (*mb_char2bytes)(c, buf);
5498 buf[cc] = NUL;
5499 ins_char_bytes(buf, cc);
5500 AppendCharToRedobuff(c);
5501 }
5502 else
5503#endif
5504 {
5505 ins_char(c);
5506 if (flags & INSCHAR_CTRLV)
5507 redo_literal(c);
5508 else
5509 AppendCharToRedobuff(c);
5510 }
5511 }
5512}
5513
5514/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005515 * Format text at the current insert position.
5516 */
5517 static void
5518internal_format(textwidth, second_indent, flags, format_only)
5519 int textwidth;
5520 int second_indent;
5521 int flags;
5522 int format_only;
5523{
5524 int cc;
5525 int save_char = NUL;
5526 int haveto_redraw = FALSE;
5527 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5528#ifdef FEAT_MBYTE
5529 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5530#endif
5531 int fo_white_par = has_format_option(FO_WHITE_PAR);
5532 int first_line = TRUE;
5533#ifdef FEAT_COMMENTS
5534 colnr_T leader_len;
5535 int no_leader = FALSE;
5536 int do_comments = (flags & INSCHAR_DO_COM);
5537#endif
5538
5539 /*
5540 * When 'ai' is off we don't want a space under the cursor to be
5541 * deleted. Replace it with an 'x' temporarily.
5542 */
5543 if (!curbuf->b_p_ai)
5544 {
5545 cc = gchar_cursor();
5546 if (vim_iswhite(cc))
5547 {
5548 save_char = cc;
5549 pchar_cursor('x');
5550 }
5551 }
5552
5553 /*
5554 * Repeat breaking lines, until the current line is not too long.
5555 */
5556 while (!got_int)
5557 {
5558 int startcol; /* Cursor column at entry */
5559 int wantcol; /* column at textwidth border */
5560 int foundcol; /* column for start of spaces */
5561 int end_foundcol = 0; /* column for start of word */
5562 colnr_T len;
5563 colnr_T virtcol;
5564#ifdef FEAT_VREPLACE
5565 int orig_col = 0;
5566 char_u *saved_text = NULL;
5567#endif
5568 colnr_T col;
5569
5570 virtcol = get_nolist_virtcol();
5571 if (virtcol < (colnr_T)textwidth)
5572 break;
5573
5574#ifdef FEAT_COMMENTS
5575 if (no_leader)
5576 do_comments = FALSE;
5577 else if (!(flags & INSCHAR_FORMAT)
5578 && has_format_option(FO_WRAP_COMS))
5579 do_comments = TRUE;
5580
5581 /* Don't break until after the comment leader */
5582 if (do_comments)
5583 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5584 else
5585 leader_len = 0;
5586
5587 /* If the line doesn't start with a comment leader, then don't
5588 * start one in a following broken line. Avoids that a %word
5589 * moved to the start of the next line causes all following lines
5590 * to start with %. */
5591 if (leader_len == 0)
5592 no_leader = TRUE;
5593#endif
5594 if (!(flags & INSCHAR_FORMAT)
5595#ifdef FEAT_COMMENTS
5596 && leader_len == 0
5597#endif
5598 && !has_format_option(FO_WRAP))
5599
5600 {
5601 textwidth = 0;
5602 break;
5603 }
5604 if ((startcol = curwin->w_cursor.col) == 0)
5605 break;
5606
5607 /* find column of textwidth border */
5608 coladvance((colnr_T)textwidth);
5609 wantcol = curwin->w_cursor.col;
5610
5611 curwin->w_cursor.col = startcol - 1;
5612#ifdef FEAT_MBYTE
5613 /* Correct cursor for multi-byte character. */
5614 if (has_mbyte)
5615 mb_adjust_cursor();
5616#endif
5617 foundcol = 0;
5618
5619 /*
5620 * Find position to break at.
5621 * Stop at first entered white when 'formatoptions' has 'v'
5622 */
5623 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5624 || curwin->w_cursor.lnum != Insstart.lnum
5625 || curwin->w_cursor.col >= Insstart.col)
5626 {
5627 cc = gchar_cursor();
5628 if (WHITECHAR(cc))
5629 {
5630 /* remember position of blank just before text */
5631 end_foundcol = curwin->w_cursor.col;
5632
5633 /* find start of sequence of blanks */
5634 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5635 {
5636 dec_cursor();
5637 cc = gchar_cursor();
5638 }
5639 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5640 break; /* only spaces in front of text */
5641#ifdef FEAT_COMMENTS
5642 /* Don't break until after the comment leader */
5643 if (curwin->w_cursor.col < leader_len)
5644 break;
5645#endif
5646 if (has_format_option(FO_ONE_LETTER))
5647 {
5648 /* do not break after one-letter words */
5649 if (curwin->w_cursor.col == 0)
5650 break; /* one-letter word at begin */
5651
5652 col = curwin->w_cursor.col;
5653 dec_cursor();
5654 cc = gchar_cursor();
5655
5656 if (WHITECHAR(cc))
5657 continue; /* one-letter, continue */
5658 curwin->w_cursor.col = col;
5659 }
5660#ifdef FEAT_MBYTE
5661 if (has_mbyte)
5662 foundcol = curwin->w_cursor.col
5663 + (*mb_ptr2len)(ml_get_cursor());
5664 else
5665#endif
5666 foundcol = curwin->w_cursor.col + 1;
5667 if (curwin->w_cursor.col < (colnr_T)wantcol)
5668 break;
5669 }
5670#ifdef FEAT_MBYTE
5671 else if (cc >= 0x100 && fo_multibyte
5672 && curwin->w_cursor.col <= (colnr_T)wantcol)
5673 {
5674 /* Break after or before a multi-byte character. */
5675 foundcol = curwin->w_cursor.col;
5676 if (curwin->w_cursor.col < (colnr_T)wantcol)
5677 foundcol += (*mb_char2len)(cc);
5678 end_foundcol = foundcol;
5679 break;
5680 }
5681#endif
5682 if (curwin->w_cursor.col == 0)
5683 break;
5684 dec_cursor();
5685 }
5686
5687 if (foundcol == 0) /* no spaces, cannot break line */
5688 {
5689 curwin->w_cursor.col = startcol;
5690 break;
5691 }
5692
5693 /* Going to break the line, remove any "$" now. */
5694 undisplay_dollar();
5695
5696 /*
5697 * Offset between cursor position and line break is used by replace
5698 * stack functions. VREPLACE does not use this, and backspaces
5699 * over the text instead.
5700 */
5701#ifdef FEAT_VREPLACE
5702 if (State & VREPLACE_FLAG)
5703 orig_col = startcol; /* Will start backspacing from here */
5704 else
5705#endif
5706 replace_offset = startcol - end_foundcol - 1;
5707
5708 /*
5709 * adjust startcol for spaces that will be deleted and
5710 * characters that will remain on top line
5711 */
5712 curwin->w_cursor.col = foundcol;
5713 while (cc = gchar_cursor(), WHITECHAR(cc))
5714 inc_cursor();
5715 startcol -= curwin->w_cursor.col;
5716 if (startcol < 0)
5717 startcol = 0;
5718
5719#ifdef FEAT_VREPLACE
5720 if (State & VREPLACE_FLAG)
5721 {
5722 /*
5723 * In VREPLACE mode, we will backspace over the text to be
5724 * wrapped, so save a copy now to put on the next line.
5725 */
5726 saved_text = vim_strsave(ml_get_cursor());
5727 curwin->w_cursor.col = orig_col;
5728 if (saved_text == NULL)
5729 break; /* Can't do it, out of memory */
5730 saved_text[startcol] = NUL;
5731
5732 /* Backspace over characters that will move to the next line */
5733 if (!fo_white_par)
5734 backspace_until_column(foundcol);
5735 }
5736 else
5737#endif
5738 {
5739 /* put cursor after pos. to break line */
5740 if (!fo_white_par)
5741 curwin->w_cursor.col = foundcol;
5742 }
5743
5744 /*
5745 * Split the line just before the margin.
5746 * Only insert/delete lines, but don't really redraw the window.
5747 */
5748 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
5749 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
5750#ifdef FEAT_COMMENTS
5751 + (do_comments ? OPENLINE_DO_COM : 0)
5752#endif
5753 , old_indent);
5754 old_indent = 0;
5755
5756 replace_offset = 0;
5757 if (first_line)
5758 {
5759 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
5760 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
5761 if (second_indent >= 0)
5762 {
5763#ifdef FEAT_VREPLACE
5764 if (State & VREPLACE_FLAG)
5765 change_indent(INDENT_SET, second_indent, FALSE, NUL);
5766 else
5767#endif
5768 (void)set_indent(second_indent, SIN_CHANGED);
5769 }
5770 first_line = FALSE;
5771 }
5772
5773#ifdef FEAT_VREPLACE
5774 if (State & VREPLACE_FLAG)
5775 {
5776 /*
5777 * In VREPLACE mode we have backspaced over the text to be
5778 * moved, now we re-insert it into the new line.
5779 */
5780 ins_bytes(saved_text);
5781 vim_free(saved_text);
5782 }
5783 else
5784#endif
5785 {
5786 /*
5787 * Check if cursor is not past the NUL off the line, cindent
5788 * may have added or removed indent.
5789 */
5790 curwin->w_cursor.col += startcol;
5791 len = (colnr_T)STRLEN(ml_get_curline());
5792 if (curwin->w_cursor.col > len)
5793 curwin->w_cursor.col = len;
5794 }
5795
5796 haveto_redraw = TRUE;
5797#ifdef FEAT_CINDENT
5798 can_cindent = TRUE;
5799#endif
5800 /* moved the cursor, don't autoindent or cindent now */
5801 did_ai = FALSE;
5802#ifdef FEAT_SMARTINDENT
5803 did_si = FALSE;
5804 can_si = FALSE;
5805 can_si_back = FALSE;
5806#endif
5807 line_breakcheck();
5808 }
5809
5810 if (save_char != NUL) /* put back space after cursor */
5811 pchar_cursor(save_char);
5812
5813 if (!format_only && haveto_redraw)
5814 {
5815 update_topline();
5816 redraw_curbuf_later(VALID);
5817 }
5818}
5819
5820/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 * Called after inserting or deleting text: When 'formatoptions' includes the
5822 * 'a' flag format from the current line until the end of the paragraph.
5823 * Keep the cursor at the same position relative to the text.
5824 * The caller must have saved the cursor line for undo, following ones will be
5825 * saved here.
5826 */
5827 void
5828auto_format(trailblank, prev_line)
5829 int trailblank; /* when TRUE also format with trailing blank */
5830 int prev_line; /* may start in previous line */
5831{
5832 pos_T pos;
5833 colnr_T len;
5834 char_u *old;
5835 char_u *new, *pnew;
5836 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005837 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838
5839 if (!has_format_option(FO_AUTO))
5840 return;
5841
5842 pos = curwin->w_cursor;
5843 old = ml_get_curline();
5844
5845 /* may remove added space */
5846 check_auto_format(FALSE);
5847
5848 /* Don't format in Insert mode when the cursor is on a trailing blank, the
5849 * user might insert normal text next. Also skip formatting when "1" is
5850 * in 'formatoptions' and there is a single character before the cursor.
5851 * Otherwise the line would be broken and when typing another non-white
5852 * next they are not joined back together. */
5853 wasatend = (pos.col == STRLEN(old));
5854 if (*old != NUL && !trailblank && wasatend)
5855 {
5856 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005857 cc = gchar_cursor();
5858 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
5859 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005861 cc = gchar_cursor();
5862 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 {
5864 curwin->w_cursor = pos;
5865 return;
5866 }
5867 curwin->w_cursor = pos;
5868 }
5869
5870#ifdef FEAT_COMMENTS
5871 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
5872 * comments. */
5873 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
5874 && get_leader_len(old, NULL, FALSE) == 0)
5875 return;
5876#endif
5877
5878 /*
5879 * May start formatting in a previous line, so that after "x" a word is
5880 * moved to the previous line if it fits there now. Only when this is not
5881 * the start of a paragraph.
5882 */
5883 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
5884 {
5885 --curwin->w_cursor.lnum;
5886 if (u_save_cursor() == FAIL)
5887 return;
5888 }
5889
5890 /*
5891 * Do the formatting and restore the cursor position. "saved_cursor" will
5892 * be adjusted for the text formatting.
5893 */
5894 saved_cursor = pos;
5895 format_lines((linenr_T)-1);
5896 curwin->w_cursor = saved_cursor;
5897 saved_cursor.lnum = 0;
5898
5899 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5900 {
5901 /* "cannot happen" */
5902 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5903 coladvance((colnr_T)MAXCOL);
5904 }
5905 else
5906 check_cursor_col();
5907
5908 /* Insert mode: If the cursor is now after the end of the line while it
5909 * previously wasn't, the line was broken. Because of the rule above we
5910 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
5911 * formatted. */
5912 if (!wasatend && has_format_option(FO_WHITE_PAR))
5913 {
5914 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005915 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 if (curwin->w_cursor.col == len)
5917 {
5918 pnew = vim_strnsave(new, len + 2);
5919 pnew[len] = ' ';
5920 pnew[len + 1] = NUL;
5921 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
5922 /* remove the space later */
5923 did_add_space = TRUE;
5924 }
5925 else
5926 /* may remove added space */
5927 check_auto_format(FALSE);
5928 }
5929
5930 check_cursor();
5931}
5932
5933/*
5934 * When an extra space was added to continue a paragraph for auto-formatting,
5935 * delete it now. The space must be under the cursor, just after the insert
5936 * position.
5937 */
5938 static void
5939check_auto_format(end_insert)
5940 int end_insert; /* TRUE when ending Insert mode */
5941{
5942 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005943 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944
5945 if (did_add_space)
5946 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005947 cc = gchar_cursor();
5948 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 /* Somehow the space was removed already. */
5950 did_add_space = FALSE;
5951 else
5952 {
5953 if (!end_insert)
5954 {
5955 inc_cursor();
5956 c = gchar_cursor();
5957 dec_cursor();
5958 }
5959 if (c != NUL)
5960 {
5961 /* The space is no longer at the end of the line, delete it. */
5962 del_char(FALSE);
5963 did_add_space = FALSE;
5964 }
5965 }
5966 }
5967}
5968
5969/*
5970 * Find out textwidth to be used for formatting:
5971 * if 'textwidth' option is set, use it
5972 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
5973 * if invalid value, use 0.
5974 * Set default to window width (maximum 79) for "gq" operator.
5975 */
5976 int
5977comp_textwidth(ff)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005978 int ff; /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979{
5980 int textwidth;
5981
5982 textwidth = curbuf->b_p_tw;
5983 if (textwidth == 0 && curbuf->b_p_wm)
5984 {
5985 /* The width is the window width minus 'wrapmargin' minus all the
5986 * things that add to the margin. */
5987 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
5988#ifdef FEAT_CMDWIN
5989 if (cmdwin_type != 0)
5990 textwidth -= 1;
5991#endif
5992#ifdef FEAT_FOLDING
5993 textwidth -= curwin->w_p_fdc;
5994#endif
5995#ifdef FEAT_SIGNS
5996 if (curwin->w_buffer->b_signlist != NULL
5997# ifdef FEAT_NETBEANS_INTG
5998 || usingNetbeans
5999# endif
6000 )
6001 textwidth -= 1;
6002#endif
6003 if (curwin->w_p_nu)
6004 textwidth -= 8;
6005 }
6006 if (textwidth < 0)
6007 textwidth = 0;
6008 if (ff && textwidth == 0)
6009 {
6010 textwidth = W_WIDTH(curwin) - 1;
6011 if (textwidth > 79)
6012 textwidth = 79;
6013 }
6014 return textwidth;
6015}
6016
6017/*
6018 * Put a character in the redo buffer, for when just after a CTRL-V.
6019 */
6020 static void
6021redo_literal(c)
6022 int c;
6023{
6024 char_u buf[10];
6025
6026 /* Only digits need special treatment. Translate them into a string of
6027 * three digits. */
6028 if (VIM_ISDIGIT(c))
6029 {
6030 sprintf((char *)buf, "%03d", c);
6031 AppendToRedobuff(buf);
6032 }
6033 else
6034 AppendCharToRedobuff(c);
6035}
6036
6037/*
6038 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006039 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 */
6041 static void
6042start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006043 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044{
6045 if (!arrow_used) /* something has been inserted */
6046 {
6047 AppendToRedobuff(ESC_STR);
6048 stop_insert(end_insert_pos, FALSE);
6049 arrow_used = TRUE; /* this means we stopped the current insert */
6050 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006051#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006052 check_spell_redraw();
6053#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054}
6055
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006056#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006057/*
6058 * If we skipped highlighting word at cursor, do it now.
6059 * It may be skipped again, thus reset spell_redraw_lnum first.
6060 */
6061 static void
6062check_spell_redraw()
6063{
6064 if (spell_redraw_lnum != 0)
6065 {
6066 linenr_T lnum = spell_redraw_lnum;
6067
6068 spell_redraw_lnum = 0;
6069 redrawWinline(lnum, FALSE);
6070 }
6071}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006072
6073/*
6074 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6075 * spelled word, if there is one.
6076 */
6077 static void
6078spell_back_to_badword()
6079{
6080 pos_T tpos = curwin->w_cursor;
6081
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006082 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006083 if (curwin->w_cursor.col != tpos.col)
6084 start_arrow(&tpos);
6085}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006086#endif
6087
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088/*
6089 * stop_arrow() is called before a change is made in insert mode.
6090 * If an arrow key has been used, start a new insertion.
6091 * Returns FAIL if undo is impossible, shouldn't insert then.
6092 */
6093 int
6094stop_arrow()
6095{
6096 if (arrow_used)
6097 {
6098 if (u_save_cursor() == OK)
6099 {
6100 arrow_used = FALSE;
6101 ins_need_undo = FALSE;
6102 }
6103 Insstart = curwin->w_cursor; /* new insertion starts here */
6104 Insstart_textlen = linetabsize(ml_get_curline());
6105 ai_col = 0;
6106#ifdef FEAT_VREPLACE
6107 if (State & VREPLACE_FLAG)
6108 {
6109 orig_line_count = curbuf->b_ml.ml_line_count;
6110 vr_lines_changed = 1;
6111 }
6112#endif
6113 ResetRedobuff();
6114 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006115 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 }
6117 else if (ins_need_undo)
6118 {
6119 if (u_save_cursor() == OK)
6120 ins_need_undo = FALSE;
6121 }
6122
6123#ifdef FEAT_FOLDING
6124 /* Always open fold at the cursor line when inserting something. */
6125 foldOpenCursor();
6126#endif
6127
6128 return (arrow_used || ins_need_undo ? FAIL : OK);
6129}
6130
6131/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006132 * Do a few things to stop inserting.
6133 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6134 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 */
6136 static void
6137stop_insert(end_insert_pos, esc)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006138 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006139 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006141 int cc;
6142 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143
6144 stop_redo_ins();
6145 replace_flush(); /* abandon replace stack */
6146
6147 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006148 * Save the inserted text for later redo with ^@ and CTRL-A.
6149 * Don't do it when "restart_edit" was set and nothing was inserted,
6150 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006152 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006153 if (did_restart_edit == 0 || (ptr != NULL
6154 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006155 {
6156 vim_free(last_insert);
6157 last_insert = ptr;
6158 last_insert_skip = new_insert_skip;
6159 }
6160 else
6161 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006163 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 {
6165 /* Auto-format now. It may seem strange to do this when stopping an
6166 * insertion (or moving the cursor), but it's required when appending
6167 * a line and having it end in a space. But only do it when something
6168 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006169 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006171 pos_T tpos = curwin->w_cursor;
6172
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 /* When the cursor is at the end of the line after a space the
6174 * formatting will move it to the following word. Avoid that by
6175 * moving the cursor onto the space. */
6176 cc = 'x';
6177 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6178 {
6179 dec_cursor();
6180 cc = gchar_cursor();
6181 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006182 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 }
6184
6185 auto_format(TRUE, FALSE);
6186
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006187 if (vim_iswhite(cc))
6188 {
6189 if (gchar_cursor() != NUL)
6190 inc_cursor();
6191#ifdef FEAT_VIRTUALEDIT
6192 /* If the cursor is still at the same character, also keep
6193 * the "coladd". */
6194 if (gchar_cursor() == NUL
6195 && curwin->w_cursor.lnum == tpos.lnum
6196 && curwin->w_cursor.col == tpos.col)
6197 curwin->w_cursor.coladd = tpos.coladd;
6198#endif
6199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 }
6201
6202 /* If a space was inserted for auto-formatting, remove it now. */
6203 check_auto_format(TRUE);
6204
6205 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006206 * of the line, and put the cursor back.
6207 * Do this when ESC was used or moving the cursor up/down. */
6208 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006209 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006211 pos_T tpos = curwin->w_cursor;
6212
6213 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar39f05632006-03-19 22:15:26 +00006214 for (;;)
6215 {
6216 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6217 --curwin->w_cursor.col;
6218 cc = gchar_cursor();
6219 if (!vim_iswhite(cc))
6220 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 (void)del_char(TRUE);
Bram Moolenaar39f05632006-03-19 22:15:26 +00006222 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006223 if (curwin->w_cursor.lnum != tpos.lnum)
6224 curwin->w_cursor = tpos;
6225 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6227
6228#ifdef FEAT_VISUAL
6229 /* <C-S-Right> may have started Visual mode, adjust the position for
6230 * deleted characters. */
6231 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
6232 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006233 cc = (int)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 if (VIsual.col > (colnr_T)cc)
6235 {
6236 VIsual.col = cc;
6237# ifdef FEAT_VIRTUALEDIT
6238 VIsual.coladd = 0;
6239# endif
6240 }
6241 }
6242#endif
6243 }
6244 }
6245 did_ai = FALSE;
6246#ifdef FEAT_SMARTINDENT
6247 did_si = FALSE;
6248 can_si = FALSE;
6249 can_si_back = FALSE;
6250#endif
6251
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006252 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6253 * now in a different buffer. */
6254 if (end_insert_pos != NULL)
6255 {
6256 curbuf->b_op_start = Insstart;
6257 curbuf->b_op_end = *end_insert_pos;
6258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259}
6260
6261/*
6262 * Set the last inserted text to a single character.
6263 * Used for the replace command.
6264 */
6265 void
6266set_last_insert(c)
6267 int c;
6268{
6269 char_u *s;
6270
6271 vim_free(last_insert);
6272#ifdef FEAT_MBYTE
6273 last_insert = alloc(MB_MAXBYTES * 3 + 5);
6274#else
6275 last_insert = alloc(6);
6276#endif
6277 if (last_insert != NULL)
6278 {
6279 s = last_insert;
6280 /* Use the CTRL-V only when entering a special char */
6281 if (c < ' ' || c == DEL)
6282 *s++ = Ctrl_V;
6283 s = add_char2buf(c, s);
6284 *s++ = ESC;
6285 *s++ = NUL;
6286 last_insert_skip = 0;
6287 }
6288}
6289
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006290#if defined(EXITFREE) || defined(PROTO)
6291 void
6292free_last_insert()
6293{
6294 vim_free(last_insert);
6295 last_insert = NULL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006296 vim_free(compl_orig_text);
6297 compl_orig_text = NULL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006298}
6299#endif
6300
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301/*
6302 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6303 * and CSI. Handle multi-byte characters.
6304 * Returns a pointer to after the added bytes.
6305 */
6306 char_u *
6307add_char2buf(c, s)
6308 int c;
6309 char_u *s;
6310{
6311#ifdef FEAT_MBYTE
6312 char_u temp[MB_MAXBYTES];
6313 int i;
6314 int len;
6315
6316 len = (*mb_char2bytes)(c, temp);
6317 for (i = 0; i < len; ++i)
6318 {
6319 c = temp[i];
6320#endif
6321 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6322 if (c == K_SPECIAL)
6323 {
6324 *s++ = K_SPECIAL;
6325 *s++ = KS_SPECIAL;
6326 *s++ = KE_FILLER;
6327 }
6328#ifdef FEAT_GUI
6329 else if (c == CSI)
6330 {
6331 *s++ = CSI;
6332 *s++ = KS_EXTRA;
6333 *s++ = (int)KE_CSI;
6334 }
6335#endif
6336 else
6337 *s++ = c;
6338#ifdef FEAT_MBYTE
6339 }
6340#endif
6341 return s;
6342}
6343
6344/*
6345 * move cursor to start of line
6346 * if flags & BL_WHITE move to first non-white
6347 * if flags & BL_SOL move to first non-white if startofline is set,
6348 * otherwise keep "curswant" column
6349 * if flags & BL_FIX don't leave the cursor on a NUL.
6350 */
6351 void
6352beginline(flags)
6353 int flags;
6354{
6355 if ((flags & BL_SOL) && !p_sol)
6356 coladvance(curwin->w_curswant);
6357 else
6358 {
6359 curwin->w_cursor.col = 0;
6360#ifdef FEAT_VIRTUALEDIT
6361 curwin->w_cursor.coladd = 0;
6362#endif
6363
6364 if (flags & (BL_WHITE | BL_SOL))
6365 {
6366 char_u *ptr;
6367
6368 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6369 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6370 ++curwin->w_cursor.col;
6371 }
6372 curwin->w_set_curswant = TRUE;
6373 }
6374}
6375
6376/*
6377 * oneright oneleft cursor_down cursor_up
6378 *
6379 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006380 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 * Return OK when successful, FAIL when we hit a line of file boundary.
6382 */
6383
6384 int
6385oneright()
6386{
6387 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389
6390#ifdef FEAT_VIRTUALEDIT
6391 if (virtual_active())
6392 {
6393 pos_T prevpos = curwin->w_cursor;
6394
6395 /* Adjust for multi-wide char (excluding TAB) */
6396 ptr = ml_get_cursor();
6397 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006398# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006400# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006402# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 ))
6404 ? ptr2cells(ptr) : 1));
6405 curwin->w_set_curswant = TRUE;
6406 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6407 return (prevpos.col != curwin->w_cursor.col
6408 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
6409 }
6410#endif
6411
6412 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006413 if (*ptr == NUL)
6414 return FAIL; /* already at the very end */
6415
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006417 if (has_mbyte)
6418 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 else
6420#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006421 l = 1;
6422
6423 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6424 * contains "onemore". */
6425 if (ptr[l] == NUL
6426#ifdef FEAT_VIRTUALEDIT
6427 && (ve_flags & VE_ONEMORE) == 0
6428#endif
6429 )
6430 return FAIL;
6431 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432
6433 curwin->w_set_curswant = TRUE;
6434 return OK;
6435}
6436
6437 int
6438oneleft()
6439{
6440#ifdef FEAT_VIRTUALEDIT
6441 if (virtual_active())
6442 {
6443 int width;
6444 int v = getviscol();
6445
6446 if (v == 0)
6447 return FAIL;
6448
6449# ifdef FEAT_LINEBREAK
6450 /* We might get stuck on 'showbreak', skip over it. */
6451 width = 1;
6452 for (;;)
6453 {
6454 coladvance(v - width);
6455 /* getviscol() is slow, skip it when 'showbreak' is empty and
6456 * there are no multi-byte characters */
6457 if ((*p_sbr == NUL
6458# ifdef FEAT_MBYTE
6459 && !has_mbyte
6460# endif
6461 ) || getviscol() < v)
6462 break;
6463 ++width;
6464 }
6465# else
6466 coladvance(v - 1);
6467# endif
6468
6469 if (curwin->w_cursor.coladd == 1)
6470 {
6471 char_u *ptr;
6472
6473 /* Adjust for multi-wide char (not a TAB) */
6474 ptr = ml_get_cursor();
6475 if (*ptr != TAB && vim_isprintc(
6476# ifdef FEAT_MBYTE
6477 (*mb_ptr2char)(ptr)
6478# else
6479 *ptr
6480# endif
6481 ) && ptr2cells(ptr) > 1)
6482 curwin->w_cursor.coladd = 0;
6483 }
6484
6485 curwin->w_set_curswant = TRUE;
6486 return OK;
6487 }
6488#endif
6489
6490 if (curwin->w_cursor.col == 0)
6491 return FAIL;
6492
6493 curwin->w_set_curswant = TRUE;
6494 --curwin->w_cursor.col;
6495
6496#ifdef FEAT_MBYTE
6497 /* if the character on the left of the current cursor is a multi-byte
6498 * character, move to its first byte */
6499 if (has_mbyte)
6500 mb_adjust_cursor();
6501#endif
6502 return OK;
6503}
6504
6505 int
6506cursor_up(n, upd_topline)
6507 long n;
6508 int upd_topline; /* When TRUE: update topline */
6509{
6510 linenr_T lnum;
6511
6512 if (n > 0)
6513 {
6514 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006515 /* This fails if the cursor is already in the first line or the count
6516 * is larger than the line number and '-' is in 'cpoptions' */
6517 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 return FAIL;
6519 if (n >= lnum)
6520 lnum = 1;
6521 else
6522#ifdef FEAT_FOLDING
6523 if (hasAnyFolding(curwin))
6524 {
6525 /*
6526 * Count each sequence of folded lines as one logical line.
6527 */
6528 /* go to the the start of the current fold */
6529 (void)hasFolding(lnum, &lnum, NULL);
6530
6531 while (n--)
6532 {
6533 /* move up one line */
6534 --lnum;
6535 if (lnum <= 1)
6536 break;
6537 /* If we entered a fold, move to the beginning, unless in
6538 * Insert mode or when 'foldopen' contains "all": it will open
6539 * in a moment. */
6540 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6541 (void)hasFolding(lnum, &lnum, NULL);
6542 }
6543 if (lnum < 1)
6544 lnum = 1;
6545 }
6546 else
6547#endif
6548 lnum -= n;
6549 curwin->w_cursor.lnum = lnum;
6550 }
6551
6552 /* try to advance to the column we want to be at */
6553 coladvance(curwin->w_curswant);
6554
6555 if (upd_topline)
6556 update_topline(); /* make sure curwin->w_topline is valid */
6557
6558 return OK;
6559}
6560
6561/*
6562 * Cursor down a number of logical lines.
6563 */
6564 int
6565cursor_down(n, upd_topline)
6566 long n;
6567 int upd_topline; /* When TRUE: update topline */
6568{
6569 linenr_T lnum;
6570
6571 if (n > 0)
6572 {
6573 lnum = curwin->w_cursor.lnum;
6574#ifdef FEAT_FOLDING
6575 /* Move to last line of fold, will fail if it's the end-of-file. */
6576 (void)hasFolding(lnum, NULL, &lnum);
6577#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006578 /* This fails if the cursor is already in the last line or would move
6579 * beyound the last line and '-' is in 'cpoptions' */
6580 if (lnum >= curbuf->b_ml.ml_line_count
6581 || (lnum + n > curbuf->b_ml.ml_line_count
6582 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 return FAIL;
6584 if (lnum + n >= curbuf->b_ml.ml_line_count)
6585 lnum = curbuf->b_ml.ml_line_count;
6586 else
6587#ifdef FEAT_FOLDING
6588 if (hasAnyFolding(curwin))
6589 {
6590 linenr_T last;
6591
6592 /* count each sequence of folded lines as one logical line */
6593 while (n--)
6594 {
6595 if (hasFolding(lnum, NULL, &last))
6596 lnum = last + 1;
6597 else
6598 ++lnum;
6599 if (lnum >= curbuf->b_ml.ml_line_count)
6600 break;
6601 }
6602 if (lnum > curbuf->b_ml.ml_line_count)
6603 lnum = curbuf->b_ml.ml_line_count;
6604 }
6605 else
6606#endif
6607 lnum += n;
6608 curwin->w_cursor.lnum = lnum;
6609 }
6610
6611 /* try to advance to the column we want to be at */
6612 coladvance(curwin->w_curswant);
6613
6614 if (upd_topline)
6615 update_topline(); /* make sure curwin->w_topline is valid */
6616
6617 return OK;
6618}
6619
6620/*
6621 * Stuff the last inserted text in the read buffer.
6622 * Last_insert actually is a copy of the redo buffer, so we
6623 * first have to remove the command.
6624 */
6625 int
6626stuff_inserted(c, count, no_esc)
6627 int c; /* Command character to be inserted */
6628 long count; /* Repeat this many times */
6629 int no_esc; /* Don't add an ESC at the end */
6630{
6631 char_u *esc_ptr;
6632 char_u *ptr;
6633 char_u *last_ptr;
6634 char_u last = NUL;
6635
6636 ptr = get_last_insert();
6637 if (ptr == NULL)
6638 {
6639 EMSG(_(e_noinstext));
6640 return FAIL;
6641 }
6642
6643 /* may want to stuff the command character, to start Insert mode */
6644 if (c != NUL)
6645 stuffcharReadbuff(c);
6646 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
6647 *esc_ptr = NUL; /* remove the ESC */
6648
6649 /* when the last char is either "0" or "^" it will be quoted if no ESC
6650 * comes after it OR if it will inserted more than once and "ptr"
6651 * starts with ^D. -- Acevedo
6652 */
6653 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
6654 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
6655 && (no_esc || (*ptr == Ctrl_D && count > 1)))
6656 {
6657 last = *last_ptr;
6658 *last_ptr = NUL;
6659 }
6660
6661 do
6662 {
6663 stuffReadbuff(ptr);
6664 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6665 if (last)
6666 stuffReadbuff((char_u *)(last == '0'
6667 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
6668 : IF_EB("\026^", CTRL_V_STR "^")));
6669 }
6670 while (--count > 0);
6671
6672 if (last)
6673 *last_ptr = last;
6674
6675 if (esc_ptr != NULL)
6676 *esc_ptr = ESC; /* put the ESC back */
6677
6678 /* may want to stuff a trailing ESC, to get out of Insert mode */
6679 if (!no_esc)
6680 stuffcharReadbuff(ESC);
6681
6682 return OK;
6683}
6684
6685 char_u *
6686get_last_insert()
6687{
6688 if (last_insert == NULL)
6689 return NULL;
6690 return last_insert + last_insert_skip;
6691}
6692
6693/*
6694 * Get last inserted string, and remove trailing <Esc>.
6695 * Returns pointer to allocated memory (must be freed) or NULL.
6696 */
6697 char_u *
6698get_last_insert_save()
6699{
6700 char_u *s;
6701 int len;
6702
6703 if (last_insert == NULL)
6704 return NULL;
6705 s = vim_strsave(last_insert + last_insert_skip);
6706 if (s != NULL)
6707 {
6708 len = (int)STRLEN(s);
6709 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
6710 s[len - 1] = NUL;
6711 }
6712 return s;
6713}
6714
6715/*
6716 * Check the word in front of the cursor for an abbreviation.
6717 * Called when the non-id character "c" has been entered.
6718 * When an abbreviation is recognized it is removed from the text and
6719 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
6720 */
6721 static int
6722echeck_abbr(c)
6723 int c;
6724{
6725 /* Don't check for abbreviation in paste mode, when disabled and just
6726 * after moving around with cursor keys. */
6727 if (p_paste || no_abbr || arrow_used)
6728 return FALSE;
6729
6730 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
6731 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
6732}
6733
6734/*
6735 * replace-stack functions
6736 *
6737 * When replacing characters, the replaced characters are remembered for each
6738 * new character. This is used to re-insert the old text when backspacing.
6739 *
6740 * There is a NUL headed list of characters for each character that is
6741 * currently in the file after the insertion point. When BS is used, one NUL
6742 * headed list is put back for the deleted character.
6743 *
6744 * For a newline, there are two NUL headed lists. One contains the characters
6745 * that the NL replaced. The extra one stores the characters after the cursor
6746 * that were deleted (always white space).
6747 *
6748 * Replace_offset is normally 0, in which case replace_push will add a new
6749 * character at the end of the stack. If replace_offset is not 0, that many
6750 * characters will be left on the stack above the newly inserted character.
6751 */
6752
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00006753static char_u *replace_stack = NULL;
6754static long replace_stack_nr = 0; /* next entry in replace stack */
6755static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756
6757 void
6758replace_push(c)
6759 int c; /* character that is replaced (NUL is none) */
6760{
6761 char_u *p;
6762
6763 if (replace_stack_nr < replace_offset) /* nothing to do */
6764 return;
6765 if (replace_stack_len <= replace_stack_nr)
6766 {
6767 replace_stack_len += 50;
6768 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
6769 if (p == NULL) /* out of memory */
6770 {
6771 replace_stack_len -= 50;
6772 return;
6773 }
6774 if (replace_stack != NULL)
6775 {
6776 mch_memmove(p, replace_stack,
6777 (size_t)(replace_stack_nr * sizeof(char_u)));
6778 vim_free(replace_stack);
6779 }
6780 replace_stack = p;
6781 }
6782 p = replace_stack + replace_stack_nr - replace_offset;
6783 if (replace_offset)
6784 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
6785 *p = c;
6786 ++replace_stack_nr;
6787}
6788
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006789#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790/*
6791 * call replace_push(c) with replace_offset set to the first NUL.
6792 */
6793 static void
6794replace_push_off(c)
6795 int c;
6796{
6797 char_u *p;
6798
6799 p = replace_stack + replace_stack_nr;
6800 for (replace_offset = 1; replace_offset < replace_stack_nr;
6801 ++replace_offset)
6802 if (*--p == NUL)
6803 break;
6804 replace_push(c);
6805 replace_offset = 0;
6806}
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006807#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808
6809/*
6810 * Pop one item from the replace stack.
6811 * return -1 if stack empty
6812 * return replaced character or NUL otherwise
6813 */
6814 static int
6815replace_pop()
6816{
6817 if (replace_stack_nr == 0)
6818 return -1;
6819 return (int)replace_stack[--replace_stack_nr];
6820}
6821
6822/*
6823 * Join the top two items on the replace stack. This removes to "off"'th NUL
6824 * encountered.
6825 */
6826 static void
6827replace_join(off)
6828 int off; /* offset for which NUL to remove */
6829{
6830 int i;
6831
6832 for (i = replace_stack_nr; --i >= 0; )
6833 if (replace_stack[i] == NUL && off-- <= 0)
6834 {
6835 --replace_stack_nr;
6836 mch_memmove(replace_stack + i, replace_stack + i + 1,
6837 (size_t)(replace_stack_nr - i));
6838 return;
6839 }
6840}
6841
6842/*
6843 * Pop bytes from the replace stack until a NUL is found, and insert them
6844 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
6845 */
6846 static void
6847replace_pop_ins()
6848{
6849 int cc;
6850 int oldState = State;
6851
6852 State = NORMAL; /* don't want REPLACE here */
6853 while ((cc = replace_pop()) > 0)
6854 {
6855#ifdef FEAT_MBYTE
6856 mb_replace_pop_ins(cc);
6857#else
6858 ins_char(cc);
6859#endif
6860 dec_cursor();
6861 }
6862 State = oldState;
6863}
6864
6865#ifdef FEAT_MBYTE
6866/*
6867 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
6868 * indicates a multi-byte char, pop the other bytes too.
6869 */
6870 static void
6871mb_replace_pop_ins(cc)
6872 int cc;
6873{
6874 int n;
6875 char_u buf[MB_MAXBYTES];
6876 int i;
6877 int c;
6878
6879 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
6880 {
6881 buf[0] = cc;
6882 for (i = 1; i < n; ++i)
6883 buf[i] = replace_pop();
6884 ins_bytes_len(buf, n);
6885 }
6886 else
6887 ins_char(cc);
6888
6889 if (enc_utf8)
6890 /* Handle composing chars. */
6891 for (;;)
6892 {
6893 c = replace_pop();
6894 if (c == -1) /* stack empty */
6895 break;
6896 if ((n = MB_BYTE2LEN(c)) == 1)
6897 {
6898 /* Not a multi-byte char, put it back. */
6899 replace_push(c);
6900 break;
6901 }
6902 else
6903 {
6904 buf[0] = c;
6905 for (i = 1; i < n; ++i)
6906 buf[i] = replace_pop();
6907 if (utf_iscomposing(utf_ptr2char(buf)))
6908 ins_bytes_len(buf, n);
6909 else
6910 {
6911 /* Not a composing char, put it back. */
6912 for (i = n - 1; i >= 0; --i)
6913 replace_push(buf[i]);
6914 break;
6915 }
6916 }
6917 }
6918}
6919#endif
6920
6921/*
6922 * make the replace stack empty
6923 * (called when exiting replace mode)
6924 */
6925 static void
6926replace_flush()
6927{
6928 vim_free(replace_stack);
6929 replace_stack = NULL;
6930 replace_stack_len = 0;
6931 replace_stack_nr = 0;
6932}
6933
6934/*
6935 * Handle doing a BS for one character.
6936 * cc < 0: replace stack empty, just move cursor
6937 * cc == 0: character was inserted, delete it
6938 * cc > 0: character was replaced, put cc (first byte of original char) back
6939 * and check for more characters to be put back
6940 */
6941 static void
6942replace_do_bs()
6943{
6944 int cc;
6945#ifdef FEAT_VREPLACE
6946 int orig_len = 0;
6947 int ins_len;
6948 int orig_vcols = 0;
6949 colnr_T start_vcol;
6950 char_u *p;
6951 int i;
6952 int vcol;
6953#endif
6954
6955 cc = replace_pop();
6956 if (cc > 0)
6957 {
6958#ifdef FEAT_VREPLACE
6959 if (State & VREPLACE_FLAG)
6960 {
6961 /* Get the number of screen cells used by the character we are
6962 * going to delete. */
6963 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
6964 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
6965 }
6966#endif
6967#ifdef FEAT_MBYTE
6968 if (has_mbyte)
6969 {
6970 del_char(FALSE);
6971# ifdef FEAT_VREPLACE
6972 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006973 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974# endif
6975 replace_push(cc);
6976 }
6977 else
6978#endif
6979 {
6980 pchar_cursor(cc);
6981#ifdef FEAT_VREPLACE
6982 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006983 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984#endif
6985 }
6986 replace_pop_ins();
6987
6988#ifdef FEAT_VREPLACE
6989 if (State & VREPLACE_FLAG)
6990 {
6991 /* Get the number of screen cells used by the inserted characters */
6992 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006993 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 vcol = start_vcol;
6995 for (i = 0; i < ins_len; ++i)
6996 {
6997 vcol += chartabsize(p + i, vcol);
6998#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006999 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000#endif
7001 }
7002 vcol -= start_vcol;
7003
7004 /* Delete spaces that were inserted after the cursor to keep the
7005 * text aligned. */
7006 curwin->w_cursor.col += ins_len;
7007 while (vcol > orig_vcols && gchar_cursor() == ' ')
7008 {
7009 del_char(FALSE);
7010 ++orig_vcols;
7011 }
7012 curwin->w_cursor.col -= ins_len;
7013 }
7014#endif
7015
7016 /* mark the buffer as changed and prepare for displaying */
7017 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7018 }
7019 else if (cc == 0)
7020 (void)del_char(FALSE);
7021}
7022
7023#ifdef FEAT_CINDENT
7024/*
7025 * Return TRUE if C-indenting is on.
7026 */
7027 static int
7028cindent_on()
7029{
7030 return (!p_paste && (curbuf->b_p_cin
7031# ifdef FEAT_EVAL
7032 || *curbuf->b_p_inde != NUL
7033# endif
7034 ));
7035}
7036#endif
7037
7038#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7039/*
7040 * Re-indent the current line, based on the current contents of it and the
7041 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7042 * confused what all the part that handles Control-T is doing that I'm not.
7043 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7044 */
7045
7046 void
7047fixthisline(get_the_indent)
7048 int (*get_the_indent) __ARGS((void));
7049{
7050 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
7051 if (linewhite(curwin->w_cursor.lnum))
7052 did_ai = TRUE; /* delete the indent if the line stays empty */
7053}
7054
7055 void
7056fix_indent()
7057{
7058 if (p_paste)
7059 return;
7060# ifdef FEAT_LISP
7061 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7062 fixthisline(get_lisp_indent);
7063# endif
7064# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7065 else
7066# endif
7067# ifdef FEAT_CINDENT
7068 if (cindent_on())
7069 do_c_expr_indent();
7070# endif
7071}
7072
7073#endif
7074
7075#ifdef FEAT_CINDENT
7076/*
7077 * return TRUE if 'cinkeys' contains the key "keytyped",
7078 * when == '*': Only if key is preceded with '*' (indent before insert)
7079 * when == '!': Only if key is prededed with '!' (don't insert)
7080 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7081 *
7082 * "keytyped" can have a few special values:
7083 * KEY_OPEN_FORW
7084 * KEY_OPEN_BACK
7085 * KEY_COMPLETE just finished completion.
7086 *
7087 * If line_is_empty is TRUE accept keys with '0' before them.
7088 */
7089 int
7090in_cinkeys(keytyped, when, line_is_empty)
7091 int keytyped;
7092 int when;
7093 int line_is_empty;
7094{
7095 char_u *look;
7096 int try_match;
7097 int try_match_word;
7098 char_u *p;
7099 char_u *line;
7100 int icase;
7101 int i;
7102
7103#ifdef FEAT_EVAL
7104 if (*curbuf->b_p_inde != NUL)
7105 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7106 else
7107#endif
7108 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7109 while (*look)
7110 {
7111 /*
7112 * Find out if we want to try a match with this key, depending on
7113 * 'when' and a '*' or '!' before the key.
7114 */
7115 switch (when)
7116 {
7117 case '*': try_match = (*look == '*'); break;
7118 case '!': try_match = (*look == '!'); break;
7119 default: try_match = (*look != '*'); break;
7120 }
7121 if (*look == '*' || *look == '!')
7122 ++look;
7123
7124 /*
7125 * If there is a '0', only accept a match if the line is empty.
7126 * But may still match when typing last char of a word.
7127 */
7128 if (*look == '0')
7129 {
7130 try_match_word = try_match;
7131 if (!line_is_empty)
7132 try_match = FALSE;
7133 ++look;
7134 }
7135 else
7136 try_match_word = FALSE;
7137
7138 /*
7139 * does it look like a control character?
7140 */
7141 if (*look == '^'
7142#ifdef EBCDIC
7143 && (Ctrl_chr(look[1]) != 0)
7144#else
7145 && look[1] >= '?' && look[1] <= '_'
7146#endif
7147 )
7148 {
7149 if (try_match && keytyped == Ctrl_chr(look[1]))
7150 return TRUE;
7151 look += 2;
7152 }
7153 /*
7154 * 'o' means "o" command, open forward.
7155 * 'O' means "O" command, open backward.
7156 */
7157 else if (*look == 'o')
7158 {
7159 if (try_match && keytyped == KEY_OPEN_FORW)
7160 return TRUE;
7161 ++look;
7162 }
7163 else if (*look == 'O')
7164 {
7165 if (try_match && keytyped == KEY_OPEN_BACK)
7166 return TRUE;
7167 ++look;
7168 }
7169
7170 /*
7171 * 'e' means to check for "else" at start of line and just before the
7172 * cursor.
7173 */
7174 else if (*look == 'e')
7175 {
7176 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7177 {
7178 p = ml_get_curline();
7179 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7180 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7181 return TRUE;
7182 }
7183 ++look;
7184 }
7185
7186 /*
7187 * ':' only causes an indent if it is at the end of a label or case
7188 * statement, or when it was before typing the ':' (to fix
7189 * class::method for C++).
7190 */
7191 else if (*look == ':')
7192 {
7193 if (try_match && keytyped == ':')
7194 {
7195 p = ml_get_curline();
7196 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
7197 return TRUE;
7198 if (curwin->w_cursor.col > 2
7199 && p[curwin->w_cursor.col - 1] == ':'
7200 && p[curwin->w_cursor.col - 2] == ':')
7201 {
7202 p[curwin->w_cursor.col - 1] = ' ';
7203 i = (cin_iscase(p) || cin_isscopedecl(p)
7204 || cin_islabel(30));
7205 p = ml_get_curline();
7206 p[curwin->w_cursor.col - 1] = ':';
7207 if (i)
7208 return TRUE;
7209 }
7210 }
7211 ++look;
7212 }
7213
7214
7215 /*
7216 * Is it a key in <>, maybe?
7217 */
7218 else if (*look == '<')
7219 {
7220 if (try_match)
7221 {
7222 /*
7223 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7224 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7225 * >, *, : and ! keys if they really really want to.
7226 */
7227 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
7228 && keytyped == look[1])
7229 return TRUE;
7230
7231 if (keytyped == get_special_key_code(look + 1))
7232 return TRUE;
7233 }
7234 while (*look && *look != '>')
7235 look++;
7236 while (*look == '>')
7237 look++;
7238 }
7239
7240 /*
7241 * Is it a word: "=word"?
7242 */
7243 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
7244 {
7245 ++look;
7246 if (*look == '~')
7247 {
7248 icase = TRUE;
7249 ++look;
7250 }
7251 else
7252 icase = FALSE;
7253 p = vim_strchr(look, ',');
7254 if (p == NULL)
7255 p = look + STRLEN(look);
7256 if ((try_match || try_match_word)
7257 && curwin->w_cursor.col >= (colnr_T)(p - look))
7258 {
7259 int match = FALSE;
7260
7261#ifdef FEAT_INS_EXPAND
7262 if (keytyped == KEY_COMPLETE)
7263 {
7264 char_u *s;
7265
7266 /* Just completed a word, check if it starts with "look".
7267 * search back for the start of a word. */
7268 line = ml_get_curline();
7269# ifdef FEAT_MBYTE
7270 if (has_mbyte)
7271 {
7272 char_u *n;
7273
7274 for (s = line + curwin->w_cursor.col; s > line; s = n)
7275 {
7276 n = mb_prevptr(line, s);
7277 if (!vim_iswordp(n))
7278 break;
7279 }
7280 }
7281 else
7282# endif
7283 for (s = line + curwin->w_cursor.col; s > line; --s)
7284 if (!vim_iswordc(s[-1]))
7285 break;
7286 if (s + (p - look) <= line + curwin->w_cursor.col
7287 && (icase
7288 ? MB_STRNICMP(s, look, p - look)
7289 : STRNCMP(s, look, p - look)) == 0)
7290 match = TRUE;
7291 }
7292 else
7293#endif
7294 /* TODO: multi-byte */
7295 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7296 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7297 {
7298 line = ml_get_cursor();
7299 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7300 || !vim_iswordc(line[-(p - look) - 1]))
7301 && (icase
7302 ? MB_STRNICMP(line - (p - look), look, p - look)
7303 : STRNCMP(line - (p - look), look, p - look))
7304 == 0)
7305 match = TRUE;
7306 }
7307 if (match && try_match_word && !try_match)
7308 {
7309 /* "0=word": Check if there are only blanks before the
7310 * word. */
7311 line = ml_get_curline();
7312 if ((int)(skipwhite(line) - line) !=
7313 (int)(curwin->w_cursor.col - (p - look)))
7314 match = FALSE;
7315 }
7316 if (match)
7317 return TRUE;
7318 }
7319 look = p;
7320 }
7321
7322 /*
7323 * ok, it's a boring generic character.
7324 */
7325 else
7326 {
7327 if (try_match && *look == keytyped)
7328 return TRUE;
7329 ++look;
7330 }
7331
7332 /*
7333 * Skip over ", ".
7334 */
7335 look = skip_to_option_part(look);
7336 }
7337 return FALSE;
7338}
7339#endif /* FEAT_CINDENT */
7340
7341#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7342/*
7343 * Map Hebrew keyboard when in hkmap mode.
7344 */
7345 int
7346hkmap(c)
7347 int c;
7348{
7349 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7350 {
7351 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7352 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7353 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7354 static char_u map[26] =
7355 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7356 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7357 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7358 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7359 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7360 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7361 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7362 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7363 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7364
7365 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7366 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7367 /* '-1'='sofit' */
7368 else if (c == 'x')
7369 return 'X';
7370 else if (c == 'q')
7371 return '\''; /* {geresh}={'} */
7372 else if (c == 246)
7373 return ' '; /* \"o --> ' ' for a german keyboard */
7374 else if (c == 228)
7375 return ' '; /* \"a --> ' ' -- / -- */
7376 else if (c == 252)
7377 return ' '; /* \"u --> ' ' -- / -- */
7378#ifdef EBCDIC
7379 else if (islower(c))
7380#else
7381 /* NOTE: islower() does not do the right thing for us on Linux so we
7382 * do this the same was as 5.7 and previous, so it works correctly on
7383 * all systems. Specifically, the e.g. Delete and Arrow keys are
7384 * munged and won't work if e.g. searching for Hebrew text.
7385 */
7386 else if (c >= 'a' && c <= 'z')
7387#endif
7388 return (int)(map[CharOrdLow(c)] + p_aleph);
7389 else
7390 return c;
7391 }
7392 else
7393 {
7394 switch (c)
7395 {
7396 case '`': return ';';
7397 case '/': return '.';
7398 case '\'': return ',';
7399 case 'q': return '/';
7400 case 'w': return '\'';
7401
7402 /* Hebrew letters - set offset from 'a' */
7403 case ',': c = '{'; break;
7404 case '.': c = 'v'; break;
7405 case ';': c = 't'; break;
7406 default: {
7407 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7408
7409#ifdef EBCDIC
7410 /* see note about islower() above */
7411 if (!islower(c))
7412#else
7413 if (c < 'a' || c > 'z')
7414#endif
7415 return c;
7416 c = str[CharOrdLow(c)];
7417 break;
7418 }
7419 }
7420
7421 return (int)(CharOrdLow(c) + p_aleph);
7422 }
7423}
7424#endif
7425
7426 static void
7427ins_reg()
7428{
7429 int need_redraw = FALSE;
7430 int regname;
7431 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007432#ifdef FEAT_VISUAL
7433 int vis_active = VIsual_active;
7434#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435
7436 /*
7437 * If we are going to wait for a character, show a '"'.
7438 */
7439 pc_status = PC_STATUS_UNSET;
7440 if (redrawing() && !char_avail())
7441 {
7442 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007443 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444
7445 edit_putchar('"', TRUE);
7446#ifdef FEAT_CMDL_INFO
7447 add_to_showcmd_c(Ctrl_R);
7448#endif
7449 }
7450
7451#ifdef USE_ON_FLY_SCROLL
7452 dont_scroll = TRUE; /* disallow scrolling here */
7453#endif
7454
7455 /*
7456 * Don't map the register name. This also prevents the mode message to be
7457 * deleted when ESC is hit.
7458 */
7459 ++no_mapping;
7460 regname = safe_vgetc();
7461#ifdef FEAT_LANGMAP
7462 LANGMAP_ADJUST(regname, TRUE);
7463#endif
7464 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
7465 {
7466 /* Get a third key for literal register insertion */
7467 literally = regname;
7468#ifdef FEAT_CMDL_INFO
7469 add_to_showcmd_c(literally);
7470#endif
7471 regname = safe_vgetc();
7472#ifdef FEAT_LANGMAP
7473 LANGMAP_ADJUST(regname, TRUE);
7474#endif
7475 }
7476 --no_mapping;
7477
7478#ifdef FEAT_EVAL
7479 /*
7480 * Don't call u_sync() while getting the expression,
7481 * evaluating it or giving an error message for it!
7482 */
7483 ++no_u_sync;
7484 if (regname == '=')
7485 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007486# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007488# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007490# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 /* Restore the Input Method. */
7492 if (im_on)
7493 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007494# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00007496 if (regname == NUL || !valid_yank_reg(regname, FALSE))
7497 {
7498 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 else
7502 {
7503#endif
7504 if (literally == Ctrl_O || literally == Ctrl_P)
7505 {
7506 /* Append the command to the redo buffer. */
7507 AppendCharToRedobuff(Ctrl_R);
7508 AppendCharToRedobuff(literally);
7509 AppendCharToRedobuff(regname);
7510
7511 do_put(regname, BACKWARD, 1L,
7512 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
7513 }
7514 else if (insert_reg(regname, literally) == FAIL)
7515 {
7516 vim_beep();
7517 need_redraw = TRUE; /* remove the '"' */
7518 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007519 else if (stop_insert_mode)
7520 /* When the '=' register was used and a function was invoked that
7521 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7522 * insert anything, need to remove the '"' */
7523 need_redraw = TRUE;
7524
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525#ifdef FEAT_EVAL
7526 }
7527 --no_u_sync;
7528#endif
7529#ifdef FEAT_CMDL_INFO
7530 clear_showcmd();
7531#endif
7532
7533 /* If the inserted register is empty, we need to remove the '"' */
7534 if (need_redraw || stuff_empty())
7535 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007536
7537#ifdef FEAT_VISUAL
7538 /* Disallow starting Visual mode here, would get a weird mode. */
7539 if (!vis_active && VIsual_active)
7540 end_visual_mode();
7541#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542}
7543
7544/*
7545 * CTRL-G commands in Insert mode.
7546 */
7547 static void
7548ins_ctrl_g()
7549{
7550 int c;
7551
7552#ifdef FEAT_INS_EXPAND
7553 /* Right after CTRL-X the cursor will be after the ruler. */
7554 setcursor();
7555#endif
7556
7557 /*
7558 * Don't map the second key. This also prevents the mode message to be
7559 * deleted when ESC is hit.
7560 */
7561 ++no_mapping;
7562 c = safe_vgetc();
7563 --no_mapping;
7564 switch (c)
7565 {
7566 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7567 case K_UP:
7568 case Ctrl_K:
7569 case 'k': ins_up(TRUE);
7570 break;
7571
7572 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7573 case K_DOWN:
7574 case Ctrl_J:
7575 case 'j': ins_down(TRUE);
7576 break;
7577
7578 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00007579 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007581
7582 /* Need to reset Insstart, esp. because a BS that joins
7583 * aline to the previous one must save for undo. */
7584 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 break;
7586
7587 /* Unknown CTRL-G command, reserved for future expansion. */
7588 default: vim_beep();
7589 }
7590}
7591
7592/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007593 * CTRL-^ in Insert mode.
7594 */
7595 static void
7596ins_ctrl_hat()
7597{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00007598 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007599 {
7600 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7601 if (State & LANGMAP)
7602 {
7603 curbuf->b_p_iminsert = B_IMODE_NONE;
7604 State &= ~LANGMAP;
7605 }
7606 else
7607 {
7608 curbuf->b_p_iminsert = B_IMODE_LMAP;
7609 State |= LANGMAP;
7610#ifdef USE_IM_CONTROL
7611 im_set_active(FALSE);
7612#endif
7613 }
7614 }
7615#ifdef USE_IM_CONTROL
7616 else
7617 {
7618 /* There are no ":lmap" mappings, toggle IM */
7619 if (im_get_status())
7620 {
7621 curbuf->b_p_iminsert = B_IMODE_NONE;
7622 im_set_active(FALSE);
7623 }
7624 else
7625 {
7626 curbuf->b_p_iminsert = B_IMODE_IM;
7627 State &= ~LANGMAP;
7628 im_set_active(TRUE);
7629 }
7630 }
7631#endif
7632 set_iminsert_global();
7633 showmode();
7634#ifdef FEAT_GUI
7635 /* may show different cursor shape or color */
7636 if (gui.in_use)
7637 gui_update_cursor(TRUE, FALSE);
7638#endif
7639#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7640 /* Show/unshow value of 'keymap' in status lines. */
7641 status_redraw_curbuf();
7642#endif
7643}
7644
7645/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 * Handle ESC in insert mode.
7647 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7648 * insert.
7649 */
7650 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00007651ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 long *count;
7653 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00007654 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655{
7656 int temp;
7657 static int disabled_redraw = FALSE;
7658
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007659#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007660 check_spell_redraw();
7661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662#if defined(FEAT_HANGULIN)
7663# if defined(ESC_CHG_TO_ENG_MODE)
7664 hangul_input_state_set(0);
7665# endif
7666 if (composing_hangul)
7667 {
7668 push_raw_key(composing_hangul_buffer, 2);
7669 composing_hangul = 0;
7670 }
7671#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672
7673 temp = curwin->w_cursor.col;
7674 if (disabled_redraw)
7675 {
7676 --RedrawingDisabled;
7677 disabled_redraw = FALSE;
7678 }
7679 if (!arrow_used)
7680 {
7681 /*
7682 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00007683 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7684 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 */
7686 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00007687 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688
7689 /*
7690 * Repeating insert may take a long time. Check for
7691 * interrupt now and then.
7692 */
7693 if (*count > 0)
7694 {
7695 line_breakcheck();
7696 if (got_int)
7697 *count = 0;
7698 }
7699
7700 if (--*count > 0) /* repeat what was typed */
7701 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007702 /* Vi repeats the insert without replacing characters. */
7703 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
7704 State &= ~REPLACE_FLAG;
7705
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 (void)start_redo_ins();
7707 if (cmdchar == 'r' || cmdchar == 'v')
7708 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
7709 ++RedrawingDisabled;
7710 disabled_redraw = TRUE;
7711 return FALSE; /* repeat the insert */
7712 }
7713 stop_insert(&curwin->w_cursor, TRUE);
7714 undisplay_dollar();
7715 }
7716
7717 /* When an autoindent was removed, curswant stays after the
7718 * indent */
7719 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
7720 curwin->w_set_curswant = TRUE;
7721
7722 /* Remember the last Insert position in the '^ mark. */
7723 if (!cmdmod.keepjumps)
7724 curbuf->b_last_insert = curwin->w_cursor;
7725
7726 /*
7727 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00007728 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00007730 if (!nomove
7731 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732#ifdef FEAT_VIRTUALEDIT
7733 || curwin->w_cursor.coladd > 0
7734#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007735 )
7736 && (restart_edit == NUL
7737 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00007739 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007741 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742#ifdef FEAT_RIGHTLEFT
7743 && !revins_on
7744#endif
7745 )
7746 {
7747#ifdef FEAT_VIRTUALEDIT
7748 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
7749 {
7750 oneleft();
7751 if (restart_edit != NUL)
7752 ++curwin->w_cursor.coladd;
7753 }
7754 else
7755#endif
7756 {
7757 --curwin->w_cursor.col;
7758#ifdef FEAT_MBYTE
7759 /* Correct cursor for multi-byte character. */
7760 if (has_mbyte)
7761 mb_adjust_cursor();
7762#endif
7763 }
7764 }
7765
7766#ifdef USE_IM_CONTROL
7767 /* Disable IM to allow typing English directly for Normal mode commands.
7768 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
7769 * well). */
7770 if (!(State & LANGMAP))
7771 im_save_status(&curbuf->b_p_iminsert);
7772 im_set_active(FALSE);
7773#endif
7774
7775 State = NORMAL;
7776 /* need to position cursor again (e.g. when on a TAB ) */
7777 changed_cline_bef_curs();
7778
7779#ifdef FEAT_MOUSE
7780 setmouse();
7781#endif
7782#ifdef CURSOR_SHAPE
7783 ui_cursor_shape(); /* may show different cursor shape */
7784#endif
7785
7786 /*
7787 * When recording or for CTRL-O, need to display the new mode.
7788 * Otherwise remove the mode message.
7789 */
7790 if (Recording || restart_edit != NUL)
7791 showmode();
7792 else if (p_smd)
7793 MSG("");
7794
7795 return TRUE; /* exit Insert mode */
7796}
7797
7798#ifdef FEAT_RIGHTLEFT
7799/*
7800 * Toggle language: hkmap and revins_on.
7801 * Move to end of reverse inserted text.
7802 */
7803 static void
7804ins_ctrl_()
7805{
7806 if (revins_on && revins_chars && revins_scol >= 0)
7807 {
7808 while (gchar_cursor() != NUL && revins_chars--)
7809 ++curwin->w_cursor.col;
7810 }
7811 p_ri = !p_ri;
7812 revins_on = (State == INSERT && p_ri);
7813 if (revins_on)
7814 {
7815 revins_scol = curwin->w_cursor.col;
7816 revins_legal++;
7817 revins_chars = 0;
7818 undisplay_dollar();
7819 }
7820 else
7821 revins_scol = -1;
7822#ifdef FEAT_FKMAP
7823 if (p_altkeymap)
7824 {
7825 /*
7826 * to be consistent also for redo command, using '.'
7827 * set arrow_used to true and stop it - causing to redo
7828 * characters entered in one mode (normal/reverse insert).
7829 */
7830 arrow_used = TRUE;
7831 (void)stop_arrow();
7832 p_fkmap = curwin->w_p_rl ^ p_ri;
7833 if (p_fkmap && p_ri)
7834 State = INSERT;
7835 }
7836 else
7837#endif
7838 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
7839 showmode();
7840}
7841#endif
7842
7843#ifdef FEAT_VISUAL
7844/*
7845 * If 'keymodel' contains "startsel", may start selection.
7846 * Returns TRUE when a CTRL-O and other keys stuffed.
7847 */
7848 static int
7849ins_start_select(c)
7850 int c;
7851{
7852 if (km_startsel)
7853 switch (c)
7854 {
7855 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 case K_PAGEUP:
7858 case K_KPAGEUP:
7859 case K_PAGEDOWN:
7860 case K_KPAGEDOWN:
7861# ifdef MACOS
7862 case K_LEFT:
7863 case K_RIGHT:
7864 case K_UP:
7865 case K_DOWN:
7866 case K_END:
7867 case K_HOME:
7868# endif
7869 if (!(mod_mask & MOD_MASK_SHIFT))
7870 break;
7871 /* FALLTHROUGH */
7872 case K_S_LEFT:
7873 case K_S_RIGHT:
7874 case K_S_UP:
7875 case K_S_DOWN:
7876 case K_S_END:
7877 case K_S_HOME:
7878 /* Start selection right away, the cursor can move with
7879 * CTRL-O when beyond the end of the line. */
7880 start_selection();
7881
7882 /* Execute the key in (insert) Select mode. */
7883 stuffcharReadbuff(Ctrl_O);
7884 if (mod_mask)
7885 {
7886 char_u buf[4];
7887
7888 buf[0] = K_SPECIAL;
7889 buf[1] = KS_MODIFIER;
7890 buf[2] = mod_mask;
7891 buf[3] = NUL;
7892 stuffReadbuff(buf);
7893 }
7894 stuffcharReadbuff(c);
7895 return TRUE;
7896 }
7897 return FALSE;
7898}
7899#endif
7900
7901/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007902 * <Insert> key in Insert mode: toggle insert/remplace mode.
7903 */
7904 static void
7905ins_insert(replaceState)
7906 int replaceState;
7907{
7908#ifdef FEAT_FKMAP
7909 if (p_fkmap && p_ri)
7910 {
7911 beep_flush();
7912 EMSG(farsi_text_3); /* encoded in Farsi */
7913 return;
7914 }
7915#endif
7916
7917#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00007918# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007919 set_vim_var_string(VV_INSERTMODE,
7920 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007921# ifdef FEAT_VREPLACE
7922 replaceState == VREPLACE ? "v" :
7923# endif
7924 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00007925# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007926 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
7927#endif
7928 if (State & REPLACE_FLAG)
7929 State = INSERT | (State & LANGMAP);
7930 else
7931 State = replaceState | (State & LANGMAP);
7932 AppendCharToRedobuff(K_INS);
7933 showmode();
7934#ifdef CURSOR_SHAPE
7935 ui_cursor_shape(); /* may show different cursor shape */
7936#endif
7937}
7938
7939/*
7940 * Pressed CTRL-O in Insert mode.
7941 */
7942 static void
7943ins_ctrl_o()
7944{
7945#ifdef FEAT_VREPLACE
7946 if (State & VREPLACE_FLAG)
7947 restart_edit = 'V';
7948 else
7949#endif
7950 if (State & REPLACE_FLAG)
7951 restart_edit = 'R';
7952 else
7953 restart_edit = 'I';
7954#ifdef FEAT_VIRTUALEDIT
7955 if (virtual_active())
7956 ins_at_eol = FALSE; /* cursor always keeps its column */
7957 else
7958#endif
7959 ins_at_eol = (gchar_cursor() == NUL);
7960}
7961
7962/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 * If the cursor is on an indent, ^T/^D insert/delete one
7964 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
7965 * Always round the indent to 'shiftwith', this is compatible
7966 * with vi. But vi only supports ^T and ^D after an
7967 * autoindent, we support it everywhere.
7968 */
7969 static void
7970ins_shift(c, lastc)
7971 int c;
7972 int lastc;
7973{
7974 if (stop_arrow() == FAIL)
7975 return;
7976 AppendCharToRedobuff(c);
7977
7978 /*
7979 * 0^D and ^^D: remove all indent.
7980 */
7981 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
7982 {
7983 --curwin->w_cursor.col;
7984 (void)del_char(FALSE); /* delete the '^' or '0' */
7985 /* In Replace mode, restore the characters that '^' or '0' replaced. */
7986 if (State & REPLACE_FLAG)
7987 replace_pop_ins();
7988 if (lastc == '^')
7989 old_indent = get_indent(); /* remember curr. indent */
7990 change_indent(INDENT_SET, 0, TRUE, 0);
7991 }
7992 else
7993 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
7994
7995 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
7996 did_ai = FALSE;
7997#ifdef FEAT_SMARTINDENT
7998 did_si = FALSE;
7999 can_si = FALSE;
8000 can_si_back = FALSE;
8001#endif
8002#ifdef FEAT_CINDENT
8003 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8004#endif
8005}
8006
8007 static void
8008ins_del()
8009{
8010 int temp;
8011
8012 if (stop_arrow() == FAIL)
8013 return;
8014 if (gchar_cursor() == NUL) /* delete newline */
8015 {
8016 temp = curwin->w_cursor.col;
8017 if (!can_bs(BS_EOL) /* only if "eol" included */
8018 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
8019 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
8020 || do_join(FALSE) == FAIL)
8021 vim_beep();
8022 else
8023 curwin->w_cursor.col = temp;
8024 }
8025 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8026 vim_beep();
8027 did_ai = FALSE;
8028#ifdef FEAT_SMARTINDENT
8029 did_si = FALSE;
8030 can_si = FALSE;
8031 can_si_back = FALSE;
8032#endif
8033 AppendCharToRedobuff(K_DEL);
8034}
8035
8036/*
8037 * Handle Backspace, delete-word and delete-line in Insert mode.
8038 * Return TRUE when backspace was actually used.
8039 */
8040 static int
8041ins_bs(c, mode, inserted_space_p)
8042 int c;
8043 int mode;
8044 int *inserted_space_p;
8045{
8046 linenr_T lnum;
8047 int cc;
8048 int temp = 0; /* init for GCC */
8049 colnr_T mincol;
8050 int did_backspace = FALSE;
8051 int in_indent;
8052 int oldState;
8053#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008054 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055#endif
8056
8057 /*
8058 * can't delete anything in an empty file
8059 * can't backup past first character in buffer
8060 * can't backup past starting point unless 'backspace' > 1
8061 * can backup to a previous line if 'backspace' == 0
8062 */
8063 if ( bufempty()
8064 || (
8065#ifdef FEAT_RIGHTLEFT
8066 !revins_on &&
8067#endif
8068 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8069 || (!can_bs(BS_START)
8070 && (arrow_used
8071 || (curwin->w_cursor.lnum == Insstart.lnum
8072 && curwin->w_cursor.col <= Insstart.col)))
8073 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8074 && curwin->w_cursor.col <= ai_col)
8075 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8076 {
8077 vim_beep();
8078 return FALSE;
8079 }
8080
8081 if (stop_arrow() == FAIL)
8082 return FALSE;
8083 in_indent = inindent(0);
8084#ifdef FEAT_CINDENT
8085 if (in_indent)
8086 can_cindent = FALSE;
8087#endif
8088#ifdef FEAT_COMMENTS
8089 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8090#endif
8091#ifdef FEAT_RIGHTLEFT
8092 if (revins_on) /* put cursor after last inserted char */
8093 inc_cursor();
8094#endif
8095
8096#ifdef FEAT_VIRTUALEDIT
8097 /* Virtualedit:
8098 * BACKSPACE_CHAR eats a virtual space
8099 * BACKSPACE_WORD eats all coladd
8100 * BACKSPACE_LINE eats all coladd and keeps going
8101 */
8102 if (curwin->w_cursor.coladd > 0)
8103 {
8104 if (mode == BACKSPACE_CHAR)
8105 {
8106 --curwin->w_cursor.coladd;
8107 return TRUE;
8108 }
8109 if (mode == BACKSPACE_WORD)
8110 {
8111 curwin->w_cursor.coladd = 0;
8112 return TRUE;
8113 }
8114 curwin->w_cursor.coladd = 0;
8115 }
8116#endif
8117
8118 /*
8119 * delete newline!
8120 */
8121 if (curwin->w_cursor.col == 0)
8122 {
8123 lnum = Insstart.lnum;
8124 if (curwin->w_cursor.lnum == Insstart.lnum
8125#ifdef FEAT_RIGHTLEFT
8126 || revins_on
8127#endif
8128 )
8129 {
8130 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8131 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8132 return FALSE;
8133 --Insstart.lnum;
8134 Insstart.col = MAXCOL;
8135 }
8136 /*
8137 * In replace mode:
8138 * cc < 0: NL was inserted, delete it
8139 * cc >= 0: NL was replaced, put original characters back
8140 */
8141 cc = -1;
8142 if (State & REPLACE_FLAG)
8143 cc = replace_pop(); /* returns -1 if NL was inserted */
8144 /*
8145 * In replace mode, in the line we started replacing, we only move the
8146 * cursor.
8147 */
8148 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8149 {
8150 dec_cursor();
8151 }
8152 else
8153 {
8154#ifdef FEAT_VREPLACE
8155 if (!(State & VREPLACE_FLAG)
8156 || curwin->w_cursor.lnum > orig_line_count)
8157#endif
8158 {
8159 temp = gchar_cursor(); /* remember current char */
8160 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008161
8162 /* When "aw" is in 'formatoptions' we must delete the space at
8163 * the end of the line, otherwise the line will be broken
8164 * again when auto-formatting. */
8165 if (has_format_option(FO_AUTO)
8166 && has_format_option(FO_WHITE_PAR))
8167 {
8168 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8169 TRUE);
8170 int len;
8171
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008172 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008173 if (len > 0 && ptr[len - 1] == ' ')
8174 ptr[len - 1] = NUL;
8175 }
8176
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 (void)do_join(FALSE);
8178 if (temp == NUL && gchar_cursor() != NUL)
8179 inc_cursor();
8180 }
8181#ifdef FEAT_VREPLACE
8182 else
8183 dec_cursor();
8184#endif
8185
8186 /*
8187 * In REPLACE mode we have to put back the text that was replaced
8188 * by the NL. On the replace stack is first a NUL-terminated
8189 * sequence of characters that were deleted and then the
8190 * characters that NL replaced.
8191 */
8192 if (State & REPLACE_FLAG)
8193 {
8194 /*
8195 * Do the next ins_char() in NORMAL state, to
8196 * prevent ins_char() from replacing characters and
8197 * avoiding showmatch().
8198 */
8199 oldState = State;
8200 State = NORMAL;
8201 /*
8202 * restore characters (blanks) deleted after cursor
8203 */
8204 while (cc > 0)
8205 {
8206 temp = curwin->w_cursor.col;
8207#ifdef FEAT_MBYTE
8208 mb_replace_pop_ins(cc);
8209#else
8210 ins_char(cc);
8211#endif
8212 curwin->w_cursor.col = temp;
8213 cc = replace_pop();
8214 }
8215 /* restore the characters that NL replaced */
8216 replace_pop_ins();
8217 State = oldState;
8218 }
8219 }
8220 did_ai = FALSE;
8221 }
8222 else
8223 {
8224 /*
8225 * Delete character(s) before the cursor.
8226 */
8227#ifdef FEAT_RIGHTLEFT
8228 if (revins_on) /* put cursor on last inserted char */
8229 dec_cursor();
8230#endif
8231 mincol = 0;
8232 /* keep indent */
8233 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
8234#ifdef FEAT_RIGHTLEFT
8235 && !revins_on
8236#endif
8237 )
8238 {
8239 temp = curwin->w_cursor.col;
8240 beginline(BL_WHITE);
8241 if (curwin->w_cursor.col < (colnr_T)temp)
8242 mincol = curwin->w_cursor.col;
8243 curwin->w_cursor.col = temp;
8244 }
8245
8246 /*
8247 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8248 */
8249 if ( mode == BACKSPACE_CHAR
8250 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00008251 || (curbuf->b_p_sts != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 && (*(ml_get_cursor() - 1) == TAB
8253 || (*(ml_get_cursor() - 1) == ' '
8254 && (!*inserted_space_p
8255 || arrow_used))))))
8256 {
8257 int ts;
8258 colnr_T vcol;
8259 colnr_T want_vcol;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008260#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 int extra = 0;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263
8264 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008265 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 ts = curbuf->b_p_sw;
8267 else
8268 ts = curbuf->b_p_sts;
8269 /* Compute the virtual column where we want to be. Since
8270 * 'showbreak' may get in the way, need to get the last column of
8271 * the previous character. */
8272 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8273 dec_cursor();
8274 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8275 inc_cursor();
8276 want_vcol = (want_vcol / ts) * ts;
8277
8278 /* delete characters until we are at or before want_vcol */
8279 while (vcol > want_vcol
8280 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
8281 {
8282 dec_cursor();
8283 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8284 if (State & REPLACE_FLAG)
8285 {
8286 /* Don't delete characters before the insert point when in
8287 * Replace mode */
8288 if (curwin->w_cursor.lnum != Insstart.lnum
8289 || curwin->w_cursor.col >= Insstart.col)
8290 {
8291#if 0 /* what was this for? It causes problems when sw != ts. */
8292 if (State == REPLACE && (int)vcol < want_vcol)
8293 {
8294 (void)del_char(FALSE);
8295 extra = 2; /* don't pop too much */
8296 }
8297 else
8298#endif
8299 replace_do_bs();
8300 }
8301 }
8302 else
8303 (void)del_char(FALSE);
8304 }
8305
8306 /* insert extra spaces until we are at want_vcol */
8307 while (vcol < want_vcol)
8308 {
8309 /* Remember the first char we inserted */
8310 if (curwin->w_cursor.lnum == Insstart.lnum
8311 && curwin->w_cursor.col < Insstart.col)
8312 Insstart.col = curwin->w_cursor.col;
8313
8314#ifdef FEAT_VREPLACE
8315 if (State & VREPLACE_FLAG)
8316 ins_char(' ');
8317 else
8318#endif
8319 {
8320 ins_str((char_u *)" ");
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008321 if ((State & REPLACE_FLAG) /* && extra <= 1 */)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008323#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 if (extra)
8325 replace_push_off(NUL);
8326 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008327#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 replace_push(NUL);
8329 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008330#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 if (extra == 2)
8332 extra = 1;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008333#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 }
8335 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8336 }
8337 }
8338
8339 /*
8340 * Delete upto starting point, start of line or previous word.
8341 */
8342 else do
8343 {
8344#ifdef FEAT_RIGHTLEFT
8345 if (!revins_on) /* put cursor on char to be deleted */
8346#endif
8347 dec_cursor();
8348
8349 /* start of word? */
8350 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
8351 {
8352 mode = BACKSPACE_WORD_NOT_SPACE;
8353 temp = vim_iswordc(gchar_cursor());
8354 }
8355 /* end of word? */
8356 else if (mode == BACKSPACE_WORD_NOT_SPACE
8357 && (vim_isspace(cc = gchar_cursor())
8358 || vim_iswordc(cc) != temp))
8359 {
8360#ifdef FEAT_RIGHTLEFT
8361 if (!revins_on)
8362#endif
8363 inc_cursor();
8364#ifdef FEAT_RIGHTLEFT
8365 else if (State & REPLACE_FLAG)
8366 dec_cursor();
8367#endif
8368 break;
8369 }
8370 if (State & REPLACE_FLAG)
8371 replace_do_bs();
8372 else
8373 {
8374#ifdef FEAT_MBYTE
8375 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008376 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377#endif
8378 (void)del_char(FALSE);
8379#ifdef FEAT_MBYTE
8380 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008381 * If there are combining characters and 'delcombine' is set
8382 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 * character.
8384 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008385 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 inc_cursor();
8387#endif
8388#ifdef FEAT_RIGHTLEFT
8389 if (revins_chars)
8390 {
8391 revins_chars--;
8392 revins_legal++;
8393 }
8394 if (revins_on && gchar_cursor() == NUL)
8395 break;
8396#endif
8397 }
8398 /* Just a single backspace?: */
8399 if (mode == BACKSPACE_CHAR)
8400 break;
8401 } while (
8402#ifdef FEAT_RIGHTLEFT
8403 revins_on ||
8404#endif
8405 (curwin->w_cursor.col > mincol
8406 && (curwin->w_cursor.lnum != Insstart.lnum
8407 || curwin->w_cursor.col != Insstart.col)));
8408 did_backspace = TRUE;
8409 }
8410#ifdef FEAT_SMARTINDENT
8411 did_si = FALSE;
8412 can_si = FALSE;
8413 can_si_back = FALSE;
8414#endif
8415 if (curwin->w_cursor.col <= 1)
8416 did_ai = FALSE;
8417 /*
8418 * It's a little strange to put backspaces into the redo
8419 * buffer, but it makes auto-indent a lot easier to deal
8420 * with.
8421 */
8422 AppendCharToRedobuff(c);
8423
8424 /* If deleted before the insertion point, adjust it */
8425 if (curwin->w_cursor.lnum == Insstart.lnum
8426 && curwin->w_cursor.col < Insstart.col)
8427 Insstart.col = curwin->w_cursor.col;
8428
8429 /* vi behaviour: the cursor moves backward but the character that
8430 * was there remains visible
8431 * Vim behaviour: the cursor moves backward and the character that
8432 * was there is erased from the screen.
8433 * We can emulate the vi behaviour by pretending there is a dollar
8434 * displayed even when there isn't.
8435 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8436 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
8437 dollar_vcol = curwin->w_virtcol;
8438
8439 return did_backspace;
8440}
8441
8442#ifdef FEAT_MOUSE
8443 static void
8444ins_mouse(c)
8445 int c;
8446{
8447 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008448 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449
8450# ifdef FEAT_GUI
8451 /* When GUI is active, also move/paste when 'mouse' is empty */
8452 if (!gui.in_use)
8453# endif
8454 if (!mouse_has(MOUSE_INSERT))
8455 return;
8456
8457 undisplay_dollar();
8458 tpos = curwin->w_cursor;
8459 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
8460 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008461#ifdef FEAT_WINDOWS
8462 win_T *new_curwin = curwin;
8463
8464 if (curwin != old_curwin && win_valid(old_curwin))
8465 {
8466 /* Mouse took us to another window. We need to go back to the
8467 * previous one to stop insert there properly. */
8468 curwin = old_curwin;
8469 curbuf = curwin->w_buffer;
8470 }
8471#endif
8472 start_arrow(curwin == old_curwin ? &tpos : NULL);
8473#ifdef FEAT_WINDOWS
8474 if (curwin != new_curwin && win_valid(new_curwin))
8475 {
8476 curwin = new_curwin;
8477 curbuf = curwin->w_buffer;
8478 }
8479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480# ifdef FEAT_CINDENT
8481 can_cindent = TRUE;
8482# endif
8483 }
8484
8485#ifdef FEAT_WINDOWS
8486 /* redraw status lines (in case another window became active) */
8487 redraw_statuslines();
8488#endif
8489}
8490
8491 static void
8492ins_mousescroll(up)
8493 int up;
8494{
8495 pos_T tpos;
8496# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8497 win_T *old_curwin;
8498# endif
8499
8500 tpos = curwin->w_cursor;
8501
8502# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8503 old_curwin = curwin;
8504
8505 /* Currently the mouse coordinates are only known in the GUI. */
8506 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
8507 {
8508 int row, col;
8509
8510 row = mouse_row;
8511 col = mouse_col;
8512
8513 /* find the window at the pointer coordinates */
8514 curwin = mouse_find_win(&row, &col);
8515 curbuf = curwin->w_buffer;
8516 }
8517 if (curwin == old_curwin)
8518# endif
8519 undisplay_dollar();
8520
8521 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8522 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
8523 else
8524 scroll_redraw(up, 3L);
8525
8526# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8527 curwin->w_redr_status = TRUE;
8528
8529 curwin = old_curwin;
8530 curbuf = curwin->w_buffer;
8531# endif
8532
8533 if (!equalpos(curwin->w_cursor, tpos))
8534 {
8535 start_arrow(&tpos);
8536# ifdef FEAT_CINDENT
8537 can_cindent = TRUE;
8538# endif
8539 }
8540}
8541#endif
8542
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008543#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00008544 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008545ins_tabline(c)
8546 int c;
8547{
8548 /* We will be leaving the current window, unless closing another tab. */
8549 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
8550 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
8551 {
8552 undisplay_dollar();
8553 start_arrow(&curwin->w_cursor);
8554# ifdef FEAT_CINDENT
8555 can_cindent = TRUE;
8556# endif
8557 }
8558
8559 if (c == K_TABLINE)
8560 goto_tabpage(current_tab);
8561 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008562 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008563 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008564 redraw_statuslines(); /* will redraw the tabline when needed */
8565 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008566}
8567#endif
8568
8569#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570 void
8571ins_scroll()
8572{
8573 pos_T tpos;
8574
8575 undisplay_dollar();
8576 tpos = curwin->w_cursor;
8577 if (gui_do_scroll())
8578 {
8579 start_arrow(&tpos);
8580# ifdef FEAT_CINDENT
8581 can_cindent = TRUE;
8582# endif
8583 }
8584}
8585
8586 void
8587ins_horscroll()
8588{
8589 pos_T tpos;
8590
8591 undisplay_dollar();
8592 tpos = curwin->w_cursor;
8593 if (gui_do_horiz_scroll())
8594 {
8595 start_arrow(&tpos);
8596# ifdef FEAT_CINDENT
8597 can_cindent = TRUE;
8598# endif
8599 }
8600}
8601#endif
8602
8603 static void
8604ins_left()
8605{
8606 pos_T tpos;
8607
8608#ifdef FEAT_FOLDING
8609 if ((fdo_flags & FDO_HOR) && KeyTyped)
8610 foldOpenCursor();
8611#endif
8612 undisplay_dollar();
8613 tpos = curwin->w_cursor;
8614 if (oneleft() == OK)
8615 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00008616#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
8617 /* Only call start_arrow() when not busy with preediting, it will
8618 * break undo. K_LEFT is inserted in im_correct_cursor(). */
8619 if (!im_is_preediting())
8620#endif
8621 start_arrow(&tpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622#ifdef FEAT_RIGHTLEFT
8623 /* If exit reversed string, position is fixed */
8624 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
8625 revins_legal++;
8626 revins_chars++;
8627#endif
8628 }
8629
8630 /*
8631 * if 'whichwrap' set for cursor in insert mode may go to
8632 * previous line
8633 */
8634 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
8635 {
8636 start_arrow(&tpos);
8637 --(curwin->w_cursor.lnum);
8638 coladvance((colnr_T)MAXCOL);
8639 curwin->w_set_curswant = TRUE; /* so we stay at the end */
8640 }
8641 else
8642 vim_beep();
8643}
8644
8645 static void
8646ins_home(c)
8647 int c;
8648{
8649 pos_T tpos;
8650
8651#ifdef FEAT_FOLDING
8652 if ((fdo_flags & FDO_HOR) && KeyTyped)
8653 foldOpenCursor();
8654#endif
8655 undisplay_dollar();
8656 tpos = curwin->w_cursor;
8657 if (c == K_C_HOME)
8658 curwin->w_cursor.lnum = 1;
8659 curwin->w_cursor.col = 0;
8660#ifdef FEAT_VIRTUALEDIT
8661 curwin->w_cursor.coladd = 0;
8662#endif
8663 curwin->w_curswant = 0;
8664 start_arrow(&tpos);
8665}
8666
8667 static void
8668ins_end(c)
8669 int c;
8670{
8671 pos_T tpos;
8672
8673#ifdef FEAT_FOLDING
8674 if ((fdo_flags & FDO_HOR) && KeyTyped)
8675 foldOpenCursor();
8676#endif
8677 undisplay_dollar();
8678 tpos = curwin->w_cursor;
8679 if (c == K_C_END)
8680 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
8681 coladvance((colnr_T)MAXCOL);
8682 curwin->w_curswant = MAXCOL;
8683
8684 start_arrow(&tpos);
8685}
8686
8687 static void
8688ins_s_left()
8689{
8690#ifdef FEAT_FOLDING
8691 if ((fdo_flags & FDO_HOR) && KeyTyped)
8692 foldOpenCursor();
8693#endif
8694 undisplay_dollar();
8695 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
8696 {
8697 start_arrow(&curwin->w_cursor);
8698 (void)bck_word(1L, FALSE, FALSE);
8699 curwin->w_set_curswant = TRUE;
8700 }
8701 else
8702 vim_beep();
8703}
8704
8705 static void
8706ins_right()
8707{
8708#ifdef FEAT_FOLDING
8709 if ((fdo_flags & FDO_HOR) && KeyTyped)
8710 foldOpenCursor();
8711#endif
8712 undisplay_dollar();
8713 if (gchar_cursor() != NUL || virtual_active()
8714 )
8715 {
8716 start_arrow(&curwin->w_cursor);
8717 curwin->w_set_curswant = TRUE;
8718#ifdef FEAT_VIRTUALEDIT
8719 if (virtual_active())
8720 oneright();
8721 else
8722#endif
8723 {
8724#ifdef FEAT_MBYTE
8725 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008726 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 else
8728#endif
8729 ++curwin->w_cursor.col;
8730 }
8731
8732#ifdef FEAT_RIGHTLEFT
8733 revins_legal++;
8734 if (revins_chars)
8735 revins_chars--;
8736#endif
8737 }
8738 /* if 'whichwrap' set for cursor in insert mode, may move the
8739 * cursor to the next line */
8740 else if (vim_strchr(p_ww, ']') != NULL
8741 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
8742 {
8743 start_arrow(&curwin->w_cursor);
8744 curwin->w_set_curswant = TRUE;
8745 ++curwin->w_cursor.lnum;
8746 curwin->w_cursor.col = 0;
8747 }
8748 else
8749 vim_beep();
8750}
8751
8752 static void
8753ins_s_right()
8754{
8755#ifdef FEAT_FOLDING
8756 if ((fdo_flags & FDO_HOR) && KeyTyped)
8757 foldOpenCursor();
8758#endif
8759 undisplay_dollar();
8760 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
8761 || gchar_cursor() != NUL)
8762 {
8763 start_arrow(&curwin->w_cursor);
8764 (void)fwd_word(1L, FALSE, 0);
8765 curwin->w_set_curswant = TRUE;
8766 }
8767 else
8768 vim_beep();
8769}
8770
8771 static void
8772ins_up(startcol)
8773 int startcol; /* when TRUE move to Insstart.col */
8774{
8775 pos_T tpos;
8776 linenr_T old_topline = curwin->w_topline;
8777#ifdef FEAT_DIFF
8778 int old_topfill = curwin->w_topfill;
8779#endif
8780
8781 undisplay_dollar();
8782 tpos = curwin->w_cursor;
8783 if (cursor_up(1L, TRUE) == OK)
8784 {
8785 if (startcol)
8786 coladvance(getvcol_nolist(&Insstart));
8787 if (old_topline != curwin->w_topline
8788#ifdef FEAT_DIFF
8789 || old_topfill != curwin->w_topfill
8790#endif
8791 )
8792 redraw_later(VALID);
8793 start_arrow(&tpos);
8794#ifdef FEAT_CINDENT
8795 can_cindent = TRUE;
8796#endif
8797 }
8798 else
8799 vim_beep();
8800}
8801
8802 static void
8803ins_pageup()
8804{
8805 pos_T tpos;
8806
8807 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008808
8809#ifdef FEAT_WINDOWS
8810 if (mod_mask & MOD_MASK_CTRL)
8811 {
8812 /* <C-PageUp>: tab page back */
8813 goto_tabpage(-1);
8814 return;
8815 }
8816#endif
8817
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 tpos = curwin->w_cursor;
8819 if (onepage(BACKWARD, 1L) == OK)
8820 {
8821 start_arrow(&tpos);
8822#ifdef FEAT_CINDENT
8823 can_cindent = TRUE;
8824#endif
8825 }
8826 else
8827 vim_beep();
8828}
8829
8830 static void
8831ins_down(startcol)
8832 int startcol; /* when TRUE move to Insstart.col */
8833{
8834 pos_T tpos;
8835 linenr_T old_topline = curwin->w_topline;
8836#ifdef FEAT_DIFF
8837 int old_topfill = curwin->w_topfill;
8838#endif
8839
8840 undisplay_dollar();
8841 tpos = curwin->w_cursor;
8842 if (cursor_down(1L, TRUE) == OK)
8843 {
8844 if (startcol)
8845 coladvance(getvcol_nolist(&Insstart));
8846 if (old_topline != curwin->w_topline
8847#ifdef FEAT_DIFF
8848 || old_topfill != curwin->w_topfill
8849#endif
8850 )
8851 redraw_later(VALID);
8852 start_arrow(&tpos);
8853#ifdef FEAT_CINDENT
8854 can_cindent = TRUE;
8855#endif
8856 }
8857 else
8858 vim_beep();
8859}
8860
8861 static void
8862ins_pagedown()
8863{
8864 pos_T tpos;
8865
8866 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008867
8868#ifdef FEAT_WINDOWS
8869 if (mod_mask & MOD_MASK_CTRL)
8870 {
8871 /* <C-PageDown>: tab page forward */
8872 goto_tabpage(0);
8873 return;
8874 }
8875#endif
8876
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 tpos = curwin->w_cursor;
8878 if (onepage(FORWARD, 1L) == OK)
8879 {
8880 start_arrow(&tpos);
8881#ifdef FEAT_CINDENT
8882 can_cindent = TRUE;
8883#endif
8884 }
8885 else
8886 vim_beep();
8887}
8888
8889#ifdef FEAT_DND
8890 static void
8891ins_drop()
8892{
8893 do_put('~', BACKWARD, 1L, PUT_CURSEND);
8894}
8895#endif
8896
8897/*
8898 * Handle TAB in Insert or Replace mode.
8899 * Return TRUE when the TAB needs to be inserted like a normal character.
8900 */
8901 static int
8902ins_tab()
8903{
8904 int ind;
8905 int i;
8906 int temp;
8907
8908 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
8909 Insstart_blank_vcol = get_nolist_virtcol();
8910 if (echeck_abbr(TAB + ABBR_OFF))
8911 return FALSE;
8912
8913 ind = inindent(0);
8914#ifdef FEAT_CINDENT
8915 if (ind)
8916 can_cindent = FALSE;
8917#endif
8918
8919 /*
8920 * When nothing special, insert TAB like a normal character
8921 */
8922 if (!curbuf->b_p_et
8923 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
8924 && curbuf->b_p_sts == 0)
8925 return TRUE;
8926
8927 if (stop_arrow() == FAIL)
8928 return TRUE;
8929
8930 did_ai = FALSE;
8931#ifdef FEAT_SMARTINDENT
8932 did_si = FALSE;
8933 can_si = FALSE;
8934 can_si_back = FALSE;
8935#endif
8936 AppendToRedobuff((char_u *)"\t");
8937
8938 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
8939 temp = (int)curbuf->b_p_sw;
8940 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
8941 temp = (int)curbuf->b_p_sts;
8942 else /* otherwise use 'tabstop' */
8943 temp = (int)curbuf->b_p_ts;
8944 temp -= get_nolist_virtcol() % temp;
8945
8946 /*
8947 * Insert the first space with ins_char(). It will delete one char in
8948 * replace mode. Insert the rest with ins_str(); it will not delete any
8949 * chars. For VREPLACE mode, we use ins_char() for all characters.
8950 */
8951 ins_char(' ');
8952 while (--temp > 0)
8953 {
8954#ifdef FEAT_VREPLACE
8955 if (State & VREPLACE_FLAG)
8956 ins_char(' ');
8957 else
8958#endif
8959 {
8960 ins_str((char_u *)" ");
8961 if (State & REPLACE_FLAG) /* no char replaced */
8962 replace_push(NUL);
8963 }
8964 }
8965
8966 /*
8967 * When 'expandtab' not set: Replace spaces by TABs where possible.
8968 */
8969 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
8970 {
8971 char_u *ptr;
8972#ifdef FEAT_VREPLACE
8973 char_u *saved_line = NULL; /* init for GCC */
8974 pos_T pos;
8975#endif
8976 pos_T fpos;
8977 pos_T *cursor;
8978 colnr_T want_vcol, vcol;
8979 int change_col = -1;
8980 int save_list = curwin->w_p_list;
8981
8982 /*
8983 * Get the current line. For VREPLACE mode, don't make real changes
8984 * yet, just work on a copy of the line.
8985 */
8986#ifdef FEAT_VREPLACE
8987 if (State & VREPLACE_FLAG)
8988 {
8989 pos = curwin->w_cursor;
8990 cursor = &pos;
8991 saved_line = vim_strsave(ml_get_curline());
8992 if (saved_line == NULL)
8993 return FALSE;
8994 ptr = saved_line + pos.col;
8995 }
8996 else
8997#endif
8998 {
8999 ptr = ml_get_cursor();
9000 cursor = &curwin->w_cursor;
9001 }
9002
9003 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9004 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9005 curwin->w_p_list = FALSE;
9006
9007 /* Find first white before the cursor */
9008 fpos = curwin->w_cursor;
9009 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
9010 {
9011 --fpos.col;
9012 --ptr;
9013 }
9014
9015 /* In Replace mode, don't change characters before the insert point. */
9016 if ((State & REPLACE_FLAG)
9017 && fpos.lnum == Insstart.lnum
9018 && fpos.col < Insstart.col)
9019 {
9020 ptr += Insstart.col - fpos.col;
9021 fpos.col = Insstart.col;
9022 }
9023
9024 /* compute virtual column numbers of first white and cursor */
9025 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9026 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9027
9028 /* Use as many TABs as possible. Beware of 'showbreak' and
9029 * 'linebreak' adding extra virtual columns. */
9030 while (vim_iswhite(*ptr))
9031 {
9032 i = lbr_chartabsize((char_u *)"\t", vcol);
9033 if (vcol + i > want_vcol)
9034 break;
9035 if (*ptr != TAB)
9036 {
9037 *ptr = TAB;
9038 if (change_col < 0)
9039 {
9040 change_col = fpos.col; /* Column of first change */
9041 /* May have to adjust Insstart */
9042 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9043 Insstart.col = fpos.col;
9044 }
9045 }
9046 ++fpos.col;
9047 ++ptr;
9048 vcol += i;
9049 }
9050
9051 if (change_col >= 0)
9052 {
9053 int repl_off = 0;
9054
9055 /* Skip over the spaces we need. */
9056 while (vcol < want_vcol && *ptr == ' ')
9057 {
9058 vcol += lbr_chartabsize(ptr, vcol);
9059 ++ptr;
9060 ++repl_off;
9061 }
9062 if (vcol > want_vcol)
9063 {
9064 /* Must have a char with 'showbreak' just before it. */
9065 --ptr;
9066 --repl_off;
9067 }
9068 fpos.col += repl_off;
9069
9070 /* Delete following spaces. */
9071 i = cursor->col - fpos.col;
9072 if (i > 0)
9073 {
9074 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
9075 /* correct replace stack. */
9076 if ((State & REPLACE_FLAG)
9077#ifdef FEAT_VREPLACE
9078 && !(State & VREPLACE_FLAG)
9079#endif
9080 )
9081 for (temp = i; --temp >= 0; )
9082 replace_join(repl_off);
9083 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009084#ifdef FEAT_NETBEANS_INTG
9085 if (usingNetbeans)
9086 {
9087 netbeans_removed(curbuf, fpos.lnum, cursor->col,
9088 (long)(i + 1));
9089 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9090 (char_u *)"\t", 1);
9091 }
9092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093 cursor->col -= i;
9094
9095#ifdef FEAT_VREPLACE
9096 /*
9097 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9098 * backspacing over the changed spacing and then inserting the new
9099 * spacing.
9100 */
9101 if (State & VREPLACE_FLAG)
9102 {
9103 /* Backspace from real cursor to change_col */
9104 backspace_until_column(change_col);
9105
9106 /* Insert each char in saved_line from changed_col to
9107 * ptr-cursor */
9108 ins_bytes_len(saved_line + change_col,
9109 cursor->col - change_col);
9110 }
9111#endif
9112 }
9113
9114#ifdef FEAT_VREPLACE
9115 if (State & VREPLACE_FLAG)
9116 vim_free(saved_line);
9117#endif
9118 curwin->w_p_list = save_list;
9119 }
9120
9121 return FALSE;
9122}
9123
9124/*
9125 * Handle CR or NL in insert mode.
9126 * Return TRUE when out of memory or can't undo.
9127 */
9128 static int
9129ins_eol(c)
9130 int c;
9131{
9132 int i;
9133
9134 if (echeck_abbr(c + ABBR_OFF))
9135 return FALSE;
9136 if (stop_arrow() == FAIL)
9137 return TRUE;
9138 undisplay_dollar();
9139
9140 /*
9141 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9142 * character under the cursor. Only push a NUL on the replace stack,
9143 * nothing to put back when the NL is deleted.
9144 */
9145 if ((State & REPLACE_FLAG)
9146#ifdef FEAT_VREPLACE
9147 && !(State & VREPLACE_FLAG)
9148#endif
9149 )
9150 replace_push(NUL);
9151
9152 /*
9153 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9154 * replacing the next line, so we push all of the characters left on the
9155 * line onto the replace stack. This is not done here though, it is done
9156 * in open_line().
9157 */
9158
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009159#ifdef FEAT_VIRTUALEDIT
9160 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9161 * CTRL-O). */
9162 if (virtual_active() && curwin->w_cursor.coladd > 0)
9163 coladvance(getviscol());
9164#endif
9165
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166#ifdef FEAT_RIGHTLEFT
9167# ifdef FEAT_FKMAP
9168 if (p_altkeymap && p_fkmap)
9169 fkmap(NL);
9170# endif
9171 /* NL in reverse insert will always start in the end of
9172 * current line. */
9173 if (revins_on)
9174 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9175#endif
9176
9177 AppendToRedobuff(NL_STR);
9178 i = open_line(FORWARD,
9179#ifdef FEAT_COMMENTS
9180 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
9181#endif
9182 0, old_indent);
9183 old_indent = 0;
9184#ifdef FEAT_CINDENT
9185 can_cindent = TRUE;
9186#endif
9187
9188 return (!i);
9189}
9190
9191#ifdef FEAT_DIGRAPHS
9192/*
9193 * Handle digraph in insert mode.
9194 * Returns character still to be inserted, or NUL when nothing remaining to be
9195 * done.
9196 */
9197 static int
9198ins_digraph()
9199{
9200 int c;
9201 int cc;
9202
9203 pc_status = PC_STATUS_UNSET;
9204 if (redrawing() && !char_avail())
9205 {
9206 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009207 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208
9209 edit_putchar('?', TRUE);
9210#ifdef FEAT_CMDL_INFO
9211 add_to_showcmd_c(Ctrl_K);
9212#endif
9213 }
9214
9215#ifdef USE_ON_FLY_SCROLL
9216 dont_scroll = TRUE; /* disallow scrolling here */
9217#endif
9218
9219 /* don't map the digraph chars. This also prevents the
9220 * mode message to be deleted when ESC is hit */
9221 ++no_mapping;
9222 ++allow_keys;
9223 c = safe_vgetc();
9224 --no_mapping;
9225 --allow_keys;
9226 if (IS_SPECIAL(c) || mod_mask) /* special key */
9227 {
9228#ifdef FEAT_CMDL_INFO
9229 clear_showcmd();
9230#endif
9231 insert_special(c, TRUE, FALSE);
9232 return NUL;
9233 }
9234 if (c != ESC)
9235 {
9236 if (redrawing() && !char_avail())
9237 {
9238 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009239 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240
9241 if (char2cells(c) == 1)
9242 {
9243 /* first remove the '?', otherwise it's restored when typing
9244 * an ESC next */
9245 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00009246 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247 edit_putchar(c, TRUE);
9248 }
9249#ifdef FEAT_CMDL_INFO
9250 add_to_showcmd_c(c);
9251#endif
9252 }
9253 ++no_mapping;
9254 ++allow_keys;
9255 cc = safe_vgetc();
9256 --no_mapping;
9257 --allow_keys;
9258 if (cc != ESC)
9259 {
9260 AppendToRedobuff((char_u *)CTRL_V_STR);
9261 c = getdigraph(c, cc, TRUE);
9262#ifdef FEAT_CMDL_INFO
9263 clear_showcmd();
9264#endif
9265 return c;
9266 }
9267 }
9268 edit_unputchar();
9269#ifdef FEAT_CMDL_INFO
9270 clear_showcmd();
9271#endif
9272 return NUL;
9273}
9274#endif
9275
9276/*
9277 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
9278 * Returns the char to be inserted, or NUL if none found.
9279 */
9280 static int
9281ins_copychar(lnum)
9282 linenr_T lnum;
9283{
9284 int c;
9285 int temp;
9286 char_u *ptr, *prev_ptr;
9287
9288 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9289 {
9290 vim_beep();
9291 return NUL;
9292 }
9293
9294 /* try to advance to the cursor column */
9295 temp = 0;
9296 ptr = ml_get(lnum);
9297 prev_ptr = ptr;
9298 validate_virtcol();
9299 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
9300 {
9301 prev_ptr = ptr;
9302 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
9303 }
9304 if ((colnr_T)temp > curwin->w_virtcol)
9305 ptr = prev_ptr;
9306
9307#ifdef FEAT_MBYTE
9308 c = (*mb_ptr2char)(ptr);
9309#else
9310 c = *ptr;
9311#endif
9312 if (c == NUL)
9313 vim_beep();
9314 return c;
9315}
9316
Bram Moolenaar4be06f92005-07-29 22:36:03 +00009317/*
9318 * CTRL-Y or CTRL-E typed in Insert mode.
9319 */
9320 static int
9321ins_ctrl_ey(tc)
9322 int tc;
9323{
9324 int c = tc;
9325
9326#ifdef FEAT_INS_EXPAND
9327 if (ctrl_x_mode == CTRL_X_SCROLL)
9328 {
9329 if (c == Ctrl_Y)
9330 scrolldown_clamp();
9331 else
9332 scrollup_clamp();
9333 redraw_later(VALID);
9334 }
9335 else
9336#endif
9337 {
9338 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
9339 if (c != NUL)
9340 {
9341 long tw_save;
9342
9343 /* The character must be taken literally, insert like it
9344 * was typed after a CTRL-V, and pretend 'textwidth'
9345 * wasn't set. Digits, 'o' and 'x' are special after a
9346 * CTRL-V, don't use it for these. */
9347 if (c < 256 && !isalnum(c))
9348 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
9349 tw_save = curbuf->b_p_tw;
9350 curbuf->b_p_tw = -1;
9351 insert_special(c, TRUE, FALSE);
9352 curbuf->b_p_tw = tw_save;
9353#ifdef FEAT_RIGHTLEFT
9354 revins_chars++;
9355 revins_legal++;
9356#endif
9357 c = Ctrl_V; /* pretend CTRL-V is last character */
9358 auto_format(FALSE, TRUE);
9359 }
9360 }
9361 return c;
9362}
9363
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364#ifdef FEAT_SMARTINDENT
9365/*
9366 * Try to do some very smart auto-indenting.
9367 * Used when inserting a "normal" character.
9368 */
9369 static void
9370ins_try_si(c)
9371 int c;
9372{
9373 pos_T *pos, old_pos;
9374 char_u *ptr;
9375 int i;
9376 int temp;
9377
9378 /*
9379 * do some very smart indenting when entering '{' or '}'
9380 */
9381 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
9382 {
9383 /*
9384 * for '}' set indent equal to indent of line containing matching '{'
9385 */
9386 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
9387 {
9388 old_pos = curwin->w_cursor;
9389 /*
9390 * If the matching '{' has a ')' immediately before it (ignoring
9391 * white-space), then line up with the start of the line
9392 * containing the matching '(' if there is one. This handles the
9393 * case where an "if (..\n..) {" statement continues over multiple
9394 * lines -- webb
9395 */
9396 ptr = ml_get(pos->lnum);
9397 i = pos->col;
9398 if (i > 0) /* skip blanks before '{' */
9399 while (--i > 0 && vim_iswhite(ptr[i]))
9400 ;
9401 curwin->w_cursor.lnum = pos->lnum;
9402 curwin->w_cursor.col = i;
9403 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
9404 curwin->w_cursor = *pos;
9405 i = get_indent();
9406 curwin->w_cursor = old_pos;
9407#ifdef FEAT_VREPLACE
9408 if (State & VREPLACE_FLAG)
9409 change_indent(INDENT_SET, i, FALSE, NUL);
9410 else
9411#endif
9412 (void)set_indent(i, SIN_CHANGED);
9413 }
9414 else if (curwin->w_cursor.col > 0)
9415 {
9416 /*
9417 * when inserting '{' after "O" reduce indent, but not
9418 * more than indent of previous line
9419 */
9420 temp = TRUE;
9421 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
9422 {
9423 old_pos = curwin->w_cursor;
9424 i = get_indent();
9425 while (curwin->w_cursor.lnum > 1)
9426 {
9427 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
9428
9429 /* ignore empty lines and lines starting with '#'. */
9430 if (*ptr != '#' && *ptr != NUL)
9431 break;
9432 }
9433 if (get_indent() >= i)
9434 temp = FALSE;
9435 curwin->w_cursor = old_pos;
9436 }
9437 if (temp)
9438 shift_line(TRUE, FALSE, 1);
9439 }
9440 }
9441
9442 /*
9443 * set indent of '#' always to 0
9444 */
9445 if (curwin->w_cursor.col > 0 && can_si && c == '#')
9446 {
9447 /* remember current indent for next line */
9448 old_indent = get_indent();
9449 (void)set_indent(0, SIN_CHANGED);
9450 }
9451
9452 /* Adjust ai_col, the char at this position can be deleted. */
9453 if (ai_col > curwin->w_cursor.col)
9454 ai_col = curwin->w_cursor.col;
9455}
9456#endif
9457
9458/*
9459 * Get the value that w_virtcol would have when 'list' is off.
9460 * Unless 'cpo' contains the 'L' flag.
9461 */
9462 static colnr_T
9463get_nolist_virtcol()
9464{
9465 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9466 return getvcol_nolist(&curwin->w_cursor);
9467 validate_virtcol();
9468 return curwin->w_virtcol;
9469}