blob: bf0f01420dd564542ebd7a2163ad01b793581915 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * edit.c: functions for Insert mode
12 */
13
14#include "vim.h"
15
16#ifdef FEAT_INS_EXPAND
17/*
18 * definitions used for CTRL-X submode
19 */
20#define CTRL_X_WANT_IDENT 0x100
21
22#define CTRL_X_NOT_DEFINED_YET 1
23#define CTRL_X_SCROLL 2
24#define CTRL_X_WHOLE_LINE 3
25#define CTRL_X_FILES 4
26#define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
27#define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
28#define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
29#define CTRL_X_FINISHED 8
30#define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
31#define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
32#define CTRL_X_CMDLINE 11
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000033#define CTRL_X_FUNCTION 12
Bram Moolenaarf75a9632005-09-13 21:20:47 +000034#define CTRL_X_OMNI 13
Bram Moolenaar488c6512005-08-11 20:09:58 +000035#define CTRL_X_SPELL 14
36#define CTRL_X_LOCAL_MSG 15 /* only used in "ctrl_x_msgs" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
Bram Moolenaar071d4272004-06-13 20:20:40 +000038#define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
39
40static char *ctrl_x_msgs[] =
41{
42 N_(" Keyword completion (^N^P)"), /* ctrl_x_mode == 0, ^P/^N compl. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +000043 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000044 NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +000045 N_(" Whole line completion (^L^N^P)"),
46 N_(" File name completion (^F^N^P)"),
47 N_(" Tag completion (^]^N^P)"),
48 N_(" Path pattern completion (^N^P)"),
49 N_(" Definition completion (^D^N^P)"),
50 NULL,
51 N_(" Dictionary completion (^K^N^P)"),
52 N_(" Thesaurus completion (^T^N^P)"),
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000053 N_(" Command-line completion (^V^N^P)"),
54 N_(" User defined completion (^U^N^P)"),
Bram Moolenaarf75a9632005-09-13 21:20:47 +000055 N_(" Omni completion (^O^N^P)"),
Bram Moolenaar910f66f2006-04-05 20:41:53 +000056 N_(" Spelling suggestion (s^N^P)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000057 N_(" Keyword Local completion (^N^P)"),
Bram Moolenaar071d4272004-06-13 20:20:40 +000058};
59
60static char_u e_hitend[] = N_("Hit end of paragraph");
61
62/*
63 * Structure used to store one match for insert completion.
64 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000065typedef struct compl_S compl_T;
66struct compl_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000067{
Bram Moolenaar572cb562005-08-05 21:35:02 +000068 compl_T *cp_next;
69 compl_T *cp_prev;
70 char_u *cp_str; /* matched text */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000071 char cp_icase; /* TRUE or FALSE: ignore case */
Bram Moolenaar39f05632006-03-19 22:15:26 +000072 char_u *(cp_text[CPT_COUNT]); /* text for the menu */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000073 char_u *cp_fname; /* file containing the match, allocated when
74 * cp_flags has FREE_FNAME */
Bram Moolenaar572cb562005-08-05 21:35:02 +000075 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
76 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +000077};
78
Bram Moolenaar572cb562005-08-05 21:35:02 +000079#define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
Bram Moolenaar071d4272004-06-13 20:20:40 +000080#define FREE_FNAME (2)
81
82/*
83 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000084 * "compl_first_match" points to the start of the list.
85 * "compl_curr_match" points to the currently selected entry.
86 * "compl_shown_match" is different from compl_curr_match during
87 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000088 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000089static compl_T *compl_first_match = NULL;
90static compl_T *compl_curr_match = NULL;
91static compl_T *compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000092
Bram Moolenaar779b74b2006-04-10 14:55:34 +000093/* After using a cursor key <Enter> selects a match in the popup menu,
94 * otherwise it inserts a line break. */
95static int compl_enter_selects = FALSE;
96
Bram Moolenaara6557602006-02-04 22:43:20 +000097/* When "compl_leader" is not NULL only matches that start with this string
98 * are used. */
99static char_u *compl_leader = NULL;
100
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000101static int compl_get_longest = FALSE; /* put longest common string
102 in compl_leader */
103
Bram Moolenaara6557602006-02-04 22:43:20 +0000104static int compl_used_match; /* Selected one of the matches. When
105 FALSE the match was edited or using
106 the longest common string. */
107
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000108static int compl_was_interrupted = FALSE; /* didn't finish finding
109 completions. */
110
111static int compl_restarting = FALSE; /* don't insert match */
112
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000113/* When the first completion is done "compl_started" is set. When it's
114 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000115static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000116
Bram Moolenaar572cb562005-08-05 21:35:02 +0000117static int compl_matches = 0;
118static char_u *compl_pattern = NULL;
119static int compl_direction = FORWARD;
120static int compl_shows_dir = FORWARD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000121static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000122static pos_T compl_startpos;
123static colnr_T compl_col = 0; /* column where the text starts
124 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000125static char_u *compl_orig_text = NULL; /* text as it was before
126 * completion started */
127static int compl_cont_mode = 0;
128static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000130static void ins_ctrl_x __ARGS((void));
131static int has_compl_option __ARGS((int dict_opt));
Bram Moolenaar4a85b412006-04-23 22:40:29 +0000132static int ins_compl_add __ARGS((char_u *str, int len, int icase, char_u *fname, char_u **cptext, int cdir, int flags, int dup));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000133static int ins_compl_equal __ARGS((compl_T *match, char_u *str, int len));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000134static void ins_compl_longest_match __ARGS((compl_T *match));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000135static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches, int icase));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136static int ins_compl_make_cyclic __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000137static void ins_compl_upd_pum __ARGS((void));
138static void ins_compl_del_pum __ARGS((void));
Bram Moolenaar280f1262006-01-30 00:14:18 +0000139static int pum_wanted __ARGS((void));
Bram Moolenaar65c923a2006-03-03 22:56:30 +0000140static int pum_enough_matches __ARGS((void));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000141static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int flags, int thesaurus));
Bram Moolenaar0b238792006-03-02 22:49:12 +0000142static void ins_compl_files __ARGS((int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000143static char_u *find_line_end __ARGS((char_u *ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144static void ins_compl_free __ARGS((void));
145static void ins_compl_clear __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000146static int ins_compl_bs __ARGS((void));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000147static void ins_compl_new_leader __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000148static void ins_compl_addleader __ARGS((int c));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000149static void ins_compl_restart __ARGS((void));
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000150static void ins_compl_set_original_text __ARGS((char_u *str));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000151static void ins_compl_addfrommatch __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000152static int ins_compl_prep __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
Bram Moolenaara94bc432006-03-10 21:42:59 +0000154#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
155static void ins_compl_add_list __ARGS((list_T *list));
156#endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000157static int ins_compl_get_exp __ARGS((pos_T *ini));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158static void ins_compl_delete __ARGS((void));
159static void ins_compl_insert __ARGS((void));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000160static int ins_compl_next __ARGS((int allow_get_expansion, int count, int insert_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000161static int ins_compl_key2dir __ARGS((int c));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000162static int ins_compl_pum_key __ARGS((int c));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000163static int ins_compl_key2count __ARGS((int c));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000164static int ins_compl_use_match __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165static int ins_complete __ARGS((int c));
166static int quote_meta __ARGS((char_u *dest, char_u *str, int len));
167#endif /* FEAT_INS_EXPAND */
168
169#define BACKSPACE_CHAR 1
170#define BACKSPACE_WORD 2
171#define BACKSPACE_WORD_NOT_SPACE 3
172#define BACKSPACE_LINE 4
173
Bram Moolenaar754b5602006-02-09 23:53:20 +0000174static void ins_redraw __ARGS((int ready));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175static void ins_ctrl_v __ARGS((void));
176static void undisplay_dollar __ARGS((void));
177static void insert_special __ARGS((int, int, int));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000178static void internal_format __ARGS((int textwidth, int second_indent, int flags, int format_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179static void check_auto_format __ARGS((int));
180static void redo_literal __ARGS((int c));
181static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000182#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +0000183static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000184static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000185static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
188static int echeck_abbr __ARGS((int));
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000189#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190static void replace_push_off __ARGS((int c));
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192static int replace_pop __ARGS((void));
193static void replace_join __ARGS((int off));
194static void replace_pop_ins __ARGS((void));
195#ifdef FEAT_MBYTE
196static void mb_replace_pop_ins __ARGS((int cc));
197#endif
198static void replace_flush __ARGS((void));
199static void replace_do_bs __ARGS((void));
200#ifdef FEAT_CINDENT
201static int cindent_on __ARGS((void));
202#endif
203static void ins_reg __ARGS((void));
204static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000205static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000206static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207#ifdef FEAT_RIGHTLEFT
208static void ins_ctrl_ __ARGS((void));
209#endif
210#ifdef FEAT_VISUAL
211static int ins_start_select __ARGS((int c));
212#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000213static void ins_insert __ARGS((int replaceState));
214static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215static void ins_shift __ARGS((int c, int lastc));
216static void ins_del __ARGS((void));
217static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
218#ifdef FEAT_MOUSE
219static void ins_mouse __ARGS((int c));
220static void ins_mousescroll __ARGS((int up));
221#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000222#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
223static void ins_tabline __ARGS((int c));
224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225static void ins_left __ARGS((void));
226static void ins_home __ARGS((int c));
227static void ins_end __ARGS((int c));
228static void ins_s_left __ARGS((void));
229static void ins_right __ARGS((void));
230static void ins_s_right __ARGS((void));
231static void ins_up __ARGS((int startcol));
232static void ins_pageup __ARGS((void));
233static void ins_down __ARGS((int startcol));
234static void ins_pagedown __ARGS((void));
235#ifdef FEAT_DND
236static void ins_drop __ARGS((void));
237#endif
238static int ins_tab __ARGS((void));
239static int ins_eol __ARGS((int c));
240#ifdef FEAT_DIGRAPHS
241static int ins_digraph __ARGS((void));
242#endif
243static int ins_copychar __ARGS((linenr_T lnum));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000244static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245#ifdef FEAT_SMARTINDENT
246static void ins_try_si __ARGS((int c));
247#endif
248static colnr_T get_nolist_virtcol __ARGS((void));
249
250static colnr_T Insstart_textlen; /* length of line when insert started */
251static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
252
253static char_u *last_insert = NULL; /* the text of the previous insert,
254 K_SPECIAL and CSI are escaped */
255static int last_insert_skip; /* nr of chars in front of previous insert */
256static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000257static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258
259#ifdef FEAT_CINDENT
260static int can_cindent; /* may do cindenting on this line */
261#endif
262
263static int old_indent = 0; /* for ^^D command in insert mode */
264
265#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000266static int revins_on; /* reverse insert mode on */
267static int revins_chars; /* how much to skip after edit */
268static int revins_legal; /* was the last char 'legal'? */
269static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270#endif
271
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272static int ins_need_undo; /* call u_save() before inserting a
273 char. Set when edit() is called.
274 after that arrow_used is used. */
275
276static int did_add_space = FALSE; /* auto_format() added an extra space
277 under the cursor */
278
279/*
280 * edit(): Start inserting text.
281 *
282 * "cmdchar" can be:
283 * 'i' normal insert command
284 * 'a' normal append command
285 * 'R' replace command
286 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
287 * but still only one <CR> is inserted. The <Esc> is not used for redo.
288 * 'g' "gI" command.
289 * 'V' "gR" command for Virtual Replace mode.
290 * 'v' "gr" command for single character Virtual Replace mode.
291 *
292 * This function is not called recursively. For CTRL-O commands, it returns
293 * and lets the caller handle the Normal-mode command.
294 *
295 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
296 */
297 int
298edit(cmdchar, startln, count)
299 int cmdchar;
300 int startln; /* if set, insert at start of line */
301 long count;
302{
303 int c = 0;
304 char_u *ptr;
305 int lastc;
306 colnr_T mincol;
307 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 int i;
309 int did_backspace = TRUE; /* previous char was backspace */
310#ifdef FEAT_CINDENT
311 int line_is_white = FALSE; /* line is empty before insert */
312#endif
313 linenr_T old_topline = 0; /* topline before insertion */
314#ifdef FEAT_DIFF
315 int old_topfill = -1;
316#endif
317 int inserted_space = FALSE; /* just inserted a space */
318 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000319 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000321 /* Remember whether editing was restarted after CTRL-O. */
322 did_restart_edit = restart_edit;
323
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 /* sleep before redrawing, needed for "CTRL-O :" that results in an
325 * error message */
326 check_for_delay(TRUE);
327
328#ifdef HAVE_SANDBOX
329 /* Don't allow inserting in the sandbox. */
330 if (sandbox != 0)
331 {
332 EMSG(_(e_sandbox));
333 return FALSE;
334 }
335#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000336 /* Don't allow changes in the buffer while editing the cmdline. The
337 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000338 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000339 {
340 EMSG(_(e_secure));
341 return FALSE;
342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343
344#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000345 /* Don't allow recursive insert mode when busy with completion. */
346 if (compl_started || pum_visible())
347 {
348 EMSG(_(e_secure));
349 return FALSE;
350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 ins_compl_clear(); /* clear stuff for CTRL-X mode */
352#endif
353
Bram Moolenaar843ee412004-06-30 16:16:41 +0000354#ifdef FEAT_AUTOCMD
355 /*
356 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
357 */
358 if (cmdchar != 'r' && cmdchar != 'v')
359 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000360# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000361 if (cmdchar == 'R')
362 ptr = (char_u *)"r";
363 else if (cmdchar == 'V')
364 ptr = (char_u *)"v";
365 else
366 ptr = (char_u *)"i";
367 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +0000368# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000369 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
370 }
371#endif
372
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373#ifdef FEAT_MOUSE
374 /*
375 * When doing a paste with the middle mouse button, Insstart is set to
376 * where the paste started.
377 */
378 if (where_paste_started.lnum != 0)
379 Insstart = where_paste_started;
380 else
381#endif
382 {
383 Insstart = curwin->w_cursor;
384 if (startln)
385 Insstart.col = 0;
386 }
387 Insstart_textlen = linetabsize(ml_get_curline());
388 Insstart_blank_vcol = MAXCOL;
389 if (!did_ai)
390 ai_col = 0;
391
392 if (cmdchar != NUL && restart_edit == 0)
393 {
394 ResetRedobuff();
395 AppendNumberToRedobuff(count);
396#ifdef FEAT_VREPLACE
397 if (cmdchar == 'V' || cmdchar == 'v')
398 {
399 /* "gR" or "gr" command */
400 AppendCharToRedobuff('g');
401 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
402 }
403 else
404#endif
405 {
406 AppendCharToRedobuff(cmdchar);
407 if (cmdchar == 'g') /* "gI" command */
408 AppendCharToRedobuff('I');
409 else if (cmdchar == 'r') /* "r<CR>" command */
410 count = 1; /* insert only one <CR> */
411 }
412 }
413
414 if (cmdchar == 'R')
415 {
416#ifdef FEAT_FKMAP
417 if (p_fkmap && p_ri)
418 {
419 beep_flush();
420 EMSG(farsi_text_3); /* encoded in Farsi */
421 State = INSERT;
422 }
423 else
424#endif
425 State = REPLACE;
426 }
427#ifdef FEAT_VREPLACE
428 else if (cmdchar == 'V' || cmdchar == 'v')
429 {
430 State = VREPLACE;
431 replaceState = VREPLACE;
432 orig_line_count = curbuf->b_ml.ml_line_count;
433 vr_lines_changed = 1;
434 }
435#endif
436 else
437 State = INSERT;
438
439 stop_insert_mode = FALSE;
440
441 /*
442 * Need to recompute the cursor position, it might move when the cursor is
443 * on a TAB or special character.
444 */
445 curs_columns(TRUE);
446
447 /*
448 * Enable langmap or IME, indicated by 'iminsert'.
449 * Note that IME may enabled/disabled without us noticing here, thus the
450 * 'iminsert' value may not reflect what is actually used. It is updated
451 * when hitting <Esc>.
452 */
453 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
454 State |= LANGMAP;
455#ifdef USE_IM_CONTROL
456 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
457#endif
458
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459#ifdef FEAT_MOUSE
460 setmouse();
461#endif
462#ifdef FEAT_CMDL_INFO
463 clear_showcmd();
464#endif
465#ifdef FEAT_RIGHTLEFT
466 /* there is no reverse replace mode */
467 revins_on = (State == INSERT && p_ri);
468 if (revins_on)
469 undisplay_dollar();
470 revins_chars = 0;
471 revins_legal = 0;
472 revins_scol = -1;
473#endif
474
475 /*
476 * Handle restarting Insert mode.
477 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
478 * restart_edit non-zero, and something in the stuff buffer.
479 */
480 if (restart_edit != 0 && stuff_empty())
481 {
482#ifdef FEAT_MOUSE
483 /*
484 * After a paste we consider text typed to be part of the insert for
485 * the pasted text. You can backspace over the pasted text too.
486 */
487 if (where_paste_started.lnum)
488 arrow_used = FALSE;
489 else
490#endif
491 arrow_used = TRUE;
492 restart_edit = 0;
493
494 /*
495 * If the cursor was after the end-of-line before the CTRL-O and it is
496 * now at the end-of-line, put it after the end-of-line (this is not
497 * correct in very rare cases).
498 * Also do this if curswant is greater than the current virtual
499 * column. Eg after "^O$" or "^O80|".
500 */
501 validate_virtcol();
502 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000503 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 || curwin->w_curswant > curwin->w_virtcol)
505 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
506 {
507 if (ptr[1] == NUL)
508 ++curwin->w_cursor.col;
509#ifdef FEAT_MBYTE
510 else if (has_mbyte)
511 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000512 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 if (ptr[i] == NUL)
514 curwin->w_cursor.col += i;
515 }
516#endif
517 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000518 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 }
520 else
521 arrow_used = FALSE;
522
523 /* we are in insert mode now, don't need to start it anymore */
524 need_start_insertmode = FALSE;
525
526 /* Need to save the line for undo before inserting the first char. */
527 ins_need_undo = TRUE;
528
529#ifdef FEAT_MOUSE
530 where_paste_started.lnum = 0;
531#endif
532#ifdef FEAT_CINDENT
533 can_cindent = TRUE;
534#endif
535#ifdef FEAT_FOLDING
536 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
537 * restarting. */
538 if (!p_im && did_restart_edit == 0)
539 foldOpenCursor();
540#endif
541
542 /*
543 * If 'showmode' is set, show the current (insert/replace/..) mode.
544 * A warning message for changing a readonly file is given here, before
545 * actually changing anything. It's put after the mode, if any.
546 */
547 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000548 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 i = showmode();
550
551 if (!p_im && did_restart_edit == 0)
552 change_warning(i + 1);
553
554#ifdef CURSOR_SHAPE
555 ui_cursor_shape(); /* may show different cursor shape */
556#endif
557#ifdef FEAT_DIGRAPHS
558 do_digraph(-1); /* clear digraphs */
559#endif
560
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000561 /*
562 * Get the current length of the redo buffer, those characters have to be
563 * skipped if we want to get to the inserted characters.
564 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 ptr = get_inserted();
566 if (ptr == NULL)
567 new_insert_skip = 0;
568 else
569 {
570 new_insert_skip = (int)STRLEN(ptr);
571 vim_free(ptr);
572 }
573
574 old_indent = 0;
575
576 /*
577 * Main loop in Insert mode: repeat until Insert mode is left.
578 */
579 for (;;)
580 {
581#ifdef FEAT_RIGHTLEFT
582 if (!revins_legal)
583 revins_scol = -1; /* reset on illegal motions */
584 else
585 revins_legal = 0;
586#endif
587 if (arrow_used) /* don't repeat insert when arrow key used */
588 count = 0;
589
590 if (stop_insert_mode)
591 {
592 /* ":stopinsert" used or 'insertmode' reset */
593 count = 0;
594 goto doESCkey;
595 }
596
597 /* set curwin->w_curswant for next K_DOWN or K_UP */
598 if (!arrow_used)
599 curwin->w_set_curswant = TRUE;
600
601 /* If there is no typeahead may check for timestamps (e.g., for when a
602 * menu invoked a shell command). */
603 if (stuff_empty())
604 {
605 did_check_timestamps = FALSE;
606 if (need_check_timestamps)
607 check_timestamps(FALSE);
608 }
609
610 /*
611 * When emsg() was called msg_scroll will have been set.
612 */
613 msg_scroll = FALSE;
614
615#ifdef FEAT_GUI
616 /* When 'mousefocus' is set a mouse movement may have taken us to
617 * another window. "need_mouse_correct" may then be set because of an
618 * autocommand. */
619 if (need_mouse_correct)
620 gui_mouse_correct();
621#endif
622
623#ifdef FEAT_FOLDING
624 /* Open fold at the cursor line, according to 'foldopen'. */
625 if (fdo_flags & FDO_INSERT)
626 foldOpenCursor();
627 /* Close folds where the cursor isn't, according to 'foldclose' */
628 if (!char_avail())
629 foldCheckClose();
630#endif
631
632 /*
633 * If we inserted a character at the last position of the last line in
634 * the window, scroll the window one line up. This avoids an extra
635 * redraw.
636 * This is detected when the cursor column is smaller after inserting
637 * something.
638 * Don't do this when the topline changed already, it has
639 * already been adjusted (by insertchar() calling open_line())).
640 */
641 if (curbuf->b_mod_set
642 && curwin->w_p_wrap
643 && !did_backspace
644 && curwin->w_topline == old_topline
645#ifdef FEAT_DIFF
646 && curwin->w_topfill == old_topfill
647#endif
648 )
649 {
650 mincol = curwin->w_wcol;
651 validate_cursor_col();
652
653 if ((int)curwin->w_wcol < (int)mincol - curbuf->b_p_ts
654 && curwin->w_wrow == W_WINROW(curwin)
655 + curwin->w_height - 1 - p_so
656 && (curwin->w_cursor.lnum != curwin->w_topline
657#ifdef FEAT_DIFF
658 || curwin->w_topfill > 0
659#endif
660 ))
661 {
662#ifdef FEAT_DIFF
663 if (curwin->w_topfill > 0)
664 --curwin->w_topfill;
665 else
666#endif
667#ifdef FEAT_FOLDING
668 if (hasFolding(curwin->w_topline, NULL, &old_topline))
669 set_topline(curwin, old_topline + 1);
670 else
671#endif
672 set_topline(curwin, curwin->w_topline + 1);
673 }
674 }
675
676 /* May need to adjust w_topline to show the cursor. */
677 update_topline();
678
679 did_backspace = FALSE;
680
681 validate_cursor(); /* may set must_redraw */
682
683 /*
684 * Redraw the display when no characters are waiting.
685 * Also shows mode, ruler and positions cursor.
686 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000687 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688
689#ifdef FEAT_SCROLLBIND
690 if (curwin->w_p_scb)
691 do_check_scrollbind(TRUE);
692#endif
693
694 update_curswant();
695 old_topline = curwin->w_topline;
696#ifdef FEAT_DIFF
697 old_topfill = curwin->w_topfill;
698#endif
699
700#ifdef USE_ON_FLY_SCROLL
701 dont_scroll = FALSE; /* allow scrolling here */
702#endif
703
704 /*
705 * Get a character for Insert mode.
706 */
707 lastc = c; /* remember previous char for CTRL-D */
708 c = safe_vgetc();
709
710#ifdef FEAT_RIGHTLEFT
711 if (p_hkmap && KeyTyped)
712 c = hkmap(c); /* Hebrew mode mapping */
713#endif
714#ifdef FEAT_FKMAP
715 if (p_fkmap && KeyTyped)
716 c = fkmap(c); /* Farsi mode mapping */
717#endif
718
719#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000720 /*
721 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000722 * and the cursor is still in the completed word. Only when there is
723 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000724 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000725 if (compl_started
726 && pum_wanted()
727 && curwin->w_cursor.col >= compl_col
728 && (compl_shown_match == NULL
729 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000730 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000731 /* BS: Delete one character from "compl_leader". */
732 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000733 && curwin->w_cursor.col > compl_col
734 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000735 continue;
736
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000737 /* When no match was selected or it was edited. */
738 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000739 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000740 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000741 * "compl_leader". Except when at the original match and
742 * there is nothing to add, CTRL-L works like CTRL-P then. */
743 if (c == Ctrl_L
744 && (ctrl_x_mode != CTRL_X_WHOLE_LINE
745 || STRLEN(compl_shown_match->cp_str)
746 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000747 {
748 ins_compl_addfrommatch();
749 continue;
750 }
751
Bram Moolenaardf1bdc92006-02-23 21:32:16 +0000752 /* A printable, non-white character: Add to "compl_leader". */
753 if (vim_isprintc(c) && !vim_iswhite(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000754 {
755 ins_compl_addleader(c);
756 continue;
757 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000758
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000759 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000760 * compl_enter_selects is set the Enter key does the same. */
761 if (c == Ctrl_Y || (compl_enter_selects
762 && (c == CAR || c == K_KENTER || c == NL)))
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000763 {
764 ins_compl_delete();
765 ins_compl_insert();
766 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000767 }
768 }
769
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
771 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000772 compl_get_longest = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +0000773 if (c != K_IGNORE && ins_compl_prep(c))
774 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775#endif
776
Bram Moolenaar488c6512005-08-11 20:09:58 +0000777 /* CTRL-\ CTRL-N goes to Normal mode,
778 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
779 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 if (c == Ctrl_BSL)
781 {
782 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000783 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 ++no_mapping;
785 ++allow_keys;
786 c = safe_vgetc();
787 --no_mapping;
788 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000789 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000791 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 vungetc(c);
793 c = Ctrl_BSL;
794 }
795 else if (c == Ctrl_G && p_im)
796 continue;
797 else
798 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000799 if (c == Ctrl_O)
800 {
801 ins_ctrl_o();
802 ins_at_eol = FALSE; /* cursor keeps its column */
803 nomove = TRUE;
804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 count = 0;
806 goto doESCkey;
807 }
808 }
809
810#ifdef FEAT_DIGRAPHS
811 c = do_digraph(c);
812#endif
813
814#ifdef FEAT_INS_EXPAND
815 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
816 goto docomplete;
817#endif
818 if (c == Ctrl_V || c == Ctrl_Q)
819 {
820 ins_ctrl_v();
821 c = Ctrl_V; /* pretend CTRL-V is last typed character */
822 continue;
823 }
824
825#ifdef FEAT_CINDENT
826 if (cindent_on()
827# ifdef FEAT_INS_EXPAND
828 && ctrl_x_mode == 0
829# endif
830 )
831 {
832 /* A key name preceded by a bang means this key is not to be
833 * inserted. Skip ahead to the re-indenting below.
834 * A key name preceded by a star means that indenting has to be
835 * done before inserting the key. */
836 line_is_white = inindent(0);
837 if (in_cinkeys(c, '!', line_is_white))
838 goto force_cindent;
839 if (can_cindent && in_cinkeys(c, '*', line_is_white)
840 && stop_arrow() == OK)
841 do_c_expr_indent();
842 }
843#endif
844
845#ifdef FEAT_RIGHTLEFT
846 if (curwin->w_p_rl)
847 switch (c)
848 {
849 case K_LEFT: c = K_RIGHT; break;
850 case K_S_LEFT: c = K_S_RIGHT; break;
851 case K_C_LEFT: c = K_C_RIGHT; break;
852 case K_RIGHT: c = K_LEFT; break;
853 case K_S_RIGHT: c = K_S_LEFT; break;
854 case K_C_RIGHT: c = K_C_LEFT; break;
855 }
856#endif
857
858#ifdef FEAT_VISUAL
859 /*
860 * If 'keymodel' contains "startsel", may start selection. If it
861 * does, a CTRL-O and c will be stuffed, we need to get these
862 * characters.
863 */
864 if (ins_start_select(c))
865 continue;
866#endif
867
868 /*
869 * The big switch to handle a character in insert mode.
870 */
871 switch (c)
872 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000873 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 if (echeck_abbr(ESC + ABBR_OFF))
875 break;
876 /*FALLTHROUGH*/
877
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000878 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879#ifdef FEAT_CMDWIN
880 if (c == Ctrl_C && cmdwin_type != 0)
881 {
882 /* Close the cmdline window. */
883 cmdwin_result = K_IGNORE;
884 got_int = FALSE; /* don't stop executing autocommands et al. */
885 goto doESCkey;
886 }
887#endif
888
889#ifdef UNIX
890do_intr:
891#endif
892 /* when 'insertmode' set, and not halfway a mapping, don't leave
893 * Insert mode */
894 if (goto_im())
895 {
896 if (got_int)
897 {
898 (void)vgetc(); /* flush all buffers */
899 got_int = FALSE;
900 }
901 else
902 vim_beep();
903 break;
904 }
905doESCkey:
906 /*
907 * This is the ONLY return from edit()!
908 */
909 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
910 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000911 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 o_lnum = curwin->w_cursor.lnum;
913
Bram Moolenaar488c6512005-08-11 20:09:58 +0000914 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000915 {
916#ifdef FEAT_AUTOCMD
917 if (cmdchar != 'r' && cmdchar != 'v')
918 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
919 FALSE, curbuf);
920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 continue;
924
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000925 case Ctrl_Z: /* suspend when 'insertmode' set */
926 if (!p_im)
927 goto normalchar; /* insert CTRL-Z as normal char */
928 stuffReadbuff((char_u *)":st\r");
929 c = Ctrl_O;
930 /*FALLTHROUGH*/
931
932 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000933#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000934 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000935 goto docomplete;
936#endif
937 if (echeck_abbr(Ctrl_O + ABBR_OFF))
938 break;
939 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000940
941#ifdef FEAT_VIRTUALEDIT
942 /* don't move the cursor left when 'virtualedit' has "onemore". */
943 if (ve_flags & VE_ONEMORE)
944 {
945 ins_at_eol = FALSE;
946 nomove = TRUE;
947 }
948#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000949 count = 0;
950 goto doESCkey;
951
Bram Moolenaar572cb562005-08-05 21:35:02 +0000952 case K_INS: /* toggle insert/replace mode */
953 case K_KINS:
954 ins_insert(replaceState);
955 break;
956
957 case K_SELECT: /* end of Select mode mapping - ignore */
958 break;
959
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000960#ifdef FEAT_SNIFF
961 case K_SNIFF: /* Sniff command received */
962 stuffcharReadbuff(K_SNIFF);
963 goto doESCkey;
964#endif
965
966 case K_HELP: /* Help key works like <ESC> <Help> */
967 case K_F1:
968 case K_XF1:
969 stuffcharReadbuff(K_HELP);
970 if (p_im)
971 need_start_insertmode = TRUE;
972 goto doESCkey;
973
974#ifdef FEAT_NETBEANS_INTG
975 case K_F21: /* NetBeans command */
976 ++no_mapping; /* don't map the next key hits */
977 i = safe_vgetc();
978 --no_mapping;
979 netbeans_keycommand(i);
980 break;
981#endif
982
983 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 case NUL:
985 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000986 /* For ^@ the trailing ESC will end the insert, unless there is an
987 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
989 && c != Ctrl_A && !p_im)
990 goto doESCkey; /* quit insert mode */
991 inserted_space = FALSE;
992 break;
993
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000994 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 ins_reg();
996 auto_format(FALSE, TRUE);
997 inserted_space = FALSE;
998 break;
999
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001000 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 ins_ctrl_g();
1002 break;
1003
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001004 case Ctrl_HAT: /* switch input mode and/or langmap */
1005 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 break;
1007
1008#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001009 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 if (!p_ari)
1011 goto normalchar;
1012 ins_ctrl_();
1013 break;
1014#endif
1015
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001016 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1018 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1019 goto docomplete;
1020#endif
1021 /* FALLTHROUGH */
1022
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001023 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024# ifdef FEAT_INS_EXPAND
1025 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1026 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001027 if (has_compl_option(FALSE))
1028 goto docomplete;
1029 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 }
1031# endif
1032 ins_shift(c, lastc);
1033 auto_format(FALSE, TRUE);
1034 inserted_space = FALSE;
1035 break;
1036
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001037 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 case K_KDEL:
1039 ins_del();
1040 auto_format(FALSE, TRUE);
1041 break;
1042
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001043 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 case Ctrl_H:
1045 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1046 auto_format(FALSE, TRUE);
1047 break;
1048
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001049 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1051 auto_format(FALSE, TRUE);
1052 break;
1053
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001054 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001055# ifdef FEAT_COMPL_FUNC
1056 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001057 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001058 goto docomplete;
1059# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1061 auto_format(FALSE, TRUE);
1062 inserted_space = FALSE;
1063 break;
1064
1065#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001066 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 case K_LEFTMOUSE_NM:
1068 case K_LEFTDRAG:
1069 case K_LEFTRELEASE:
1070 case K_LEFTRELEASE_NM:
1071 case K_MIDDLEMOUSE:
1072 case K_MIDDLEDRAG:
1073 case K_MIDDLERELEASE:
1074 case K_RIGHTMOUSE:
1075 case K_RIGHTDRAG:
1076 case K_RIGHTRELEASE:
1077 case K_X1MOUSE:
1078 case K_X1DRAG:
1079 case K_X1RELEASE:
1080 case K_X2MOUSE:
1081 case K_X2DRAG:
1082 case K_X2RELEASE:
1083 ins_mouse(c);
1084 break;
1085
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001086 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 ins_mousescroll(FALSE);
1088 break;
1089
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001090 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 ins_mousescroll(TRUE);
1092 break;
1093#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001094#ifdef FEAT_GUI_TABLINE
1095 case K_TABLINE:
1096 case K_TABMENU:
1097 ins_tabline(c);
1098 break;
1099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001101 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 break;
1103
Bram Moolenaar754b5602006-02-09 23:53:20 +00001104#ifdef FEAT_AUTOCMD
1105 case K_CURSORHOLD: /* Didn't type something for a while. */
1106 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1107 did_cursorhold = TRUE;
1108 break;
1109#endif
1110
Bram Moolenaar4770d092006-01-12 23:22:24 +00001111#ifdef FEAT_GUI_W32
1112 /* On Win32 ignore <M-F4>, we get it when closing the window was
1113 * cancelled. */
1114 case K_F4:
1115 if (mod_mask != MOD_MASK_ALT)
1116 goto normalchar;
1117 break;
1118#endif
1119
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120#ifdef FEAT_GUI
1121 case K_VER_SCROLLBAR:
1122 ins_scroll();
1123 break;
1124
1125 case K_HOR_SCROLLBAR:
1126 ins_horscroll();
1127 break;
1128#endif
1129
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001130 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 case K_S_HOME:
1133 case K_C_HOME:
1134 ins_home(c);
1135 break;
1136
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001137 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 case K_S_END:
1140 case K_C_END:
1141 ins_end(c);
1142 break;
1143
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001144 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001145 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1146 ins_s_left();
1147 else
1148 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 break;
1150
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001151 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 case K_C_LEFT:
1153 ins_s_left();
1154 break;
1155
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001156 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001157 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1158 ins_s_right();
1159 else
1160 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 break;
1162
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001163 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 case K_C_RIGHT:
1165 ins_s_right();
1166 break;
1167
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001168 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001169#ifdef FEAT_INS_EXPAND
1170 if (pum_visible())
1171 goto docomplete;
1172#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001173 if (mod_mask & MOD_MASK_SHIFT)
1174 ins_pageup();
1175 else
1176 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 break;
1178
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001179 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 case K_PAGEUP:
1181 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001182#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001183 if (pum_visible())
1184 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001185#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 ins_pageup();
1187 break;
1188
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001189 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001190#ifdef FEAT_INS_EXPAND
1191 if (pum_visible())
1192 goto docomplete;
1193#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001194 if (mod_mask & MOD_MASK_SHIFT)
1195 ins_pagedown();
1196 else
1197 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 break;
1199
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001200 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 case K_PAGEDOWN:
1202 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001203#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001204 if (pum_visible())
1205 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 ins_pagedown();
1208 break;
1209
1210#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001211 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 ins_drop();
1213 break;
1214#endif
1215
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001216 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 c = TAB;
1218 /* FALLTHROUGH */
1219
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001220 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1222 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1223 goto docomplete;
1224#endif
1225 inserted_space = FALSE;
1226 if (ins_tab())
1227 goto normalchar; /* insert TAB as a normal char */
1228 auto_format(FALSE, TRUE);
1229 break;
1230
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001231 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 c = CAR;
1233 /* FALLTHROUGH */
1234 case CAR:
1235 case NL:
1236#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1237 /* In a quickfix window a <CR> jumps to the error under the
1238 * cursor. */
1239 if (bt_quickfix(curbuf) && c == CAR)
1240 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001241 if (curwin->w_llist_ref == NULL) /* quickfix window */
1242 do_cmdline_cmd((char_u *)".cc");
1243 else /* location list window */
1244 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 break;
1246 }
1247#endif
1248#ifdef FEAT_CMDWIN
1249 if (cmdwin_type != 0)
1250 {
1251 /* Execute the command in the cmdline window. */
1252 cmdwin_result = CAR;
1253 goto doESCkey;
1254 }
1255#endif
1256 if (ins_eol(c) && !p_im)
1257 goto doESCkey; /* out of memory */
1258 auto_format(FALSE, FALSE);
1259 inserted_space = FALSE;
1260 break;
1261
1262#if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001263 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264# ifdef FEAT_INS_EXPAND
1265 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1266 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001267 if (has_compl_option(TRUE))
1268 goto docomplete;
1269 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 }
1271# endif
1272# ifdef FEAT_DIGRAPHS
1273 c = ins_digraph();
1274 if (c == NUL)
1275 break;
1276# endif
1277 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279
1280#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001281 case Ctrl_X: /* Enter CTRL-X mode */
1282 ins_ctrl_x();
1283 break;
1284
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001285 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 if (ctrl_x_mode != CTRL_X_TAGS)
1287 goto normalchar;
1288 goto docomplete;
1289
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001290 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 if (ctrl_x_mode != CTRL_X_FILES)
1292 goto normalchar;
1293 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001294
1295 case 's': /* Spelling completion after ^X */
1296 case Ctrl_S:
1297 if (ctrl_x_mode != CTRL_X_SPELL)
1298 goto normalchar;
1299 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300#endif
1301
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001302 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303#ifdef FEAT_INS_EXPAND
1304 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1305#endif
1306 {
1307 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1308 if (p_im)
1309 {
1310 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1311 break;
1312 goto doESCkey;
1313 }
1314 goto normalchar;
1315 }
1316#ifdef FEAT_INS_EXPAND
1317 /* FALLTHROUGH */
1318
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001319 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 case Ctrl_N:
1321 /* if 'complete' is empty then plain ^P is no longer special,
1322 * but it is under other ^X modes */
1323 if (*curbuf->b_p_cpt == NUL
1324 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001325 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 goto normalchar;
1327
1328docomplete:
1329 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001330 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 break;
1332#endif /* FEAT_INS_EXPAND */
1333
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001334 case Ctrl_Y: /* copy from previous line or scroll down */
1335 case Ctrl_E: /* copy from next line or scroll up */
1336 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 break;
1338
1339 default:
1340#ifdef UNIX
1341 if (c == intr_char) /* special interrupt char */
1342 goto do_intr;
1343#endif
1344
1345 /*
1346 * Insert a nomal character.
1347 */
1348normalchar:
1349#ifdef FEAT_SMARTINDENT
1350 /* Try to perform smart-indenting. */
1351 ins_try_si(c);
1352#endif
1353
1354 if (c == ' ')
1355 {
1356 inserted_space = TRUE;
1357#ifdef FEAT_CINDENT
1358 if (inindent(0))
1359 can_cindent = FALSE;
1360#endif
1361 if (Insstart_blank_vcol == MAXCOL
1362 && curwin->w_cursor.lnum == Insstart.lnum)
1363 Insstart_blank_vcol = get_nolist_virtcol();
1364 }
1365
1366 if (vim_iswordc(c) || !echeck_abbr(
1367#ifdef FEAT_MBYTE
1368 /* Add ABBR_OFF for characters above 0x100, this is
1369 * what check_abbr() expects. */
1370 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1371#endif
1372 c))
1373 {
1374 insert_special(c, FALSE, FALSE);
1375#ifdef FEAT_RIGHTLEFT
1376 revins_legal++;
1377 revins_chars++;
1378#endif
1379 }
1380
1381 auto_format(FALSE, TRUE);
1382
1383#ifdef FEAT_FOLDING
1384 /* When inserting a character the cursor line must never be in a
1385 * closed fold. */
1386 foldOpenCursor();
1387#endif
1388 break;
1389 } /* end of switch (c) */
1390
1391 /* If the cursor was moved we didn't just insert a space */
1392 if (arrow_used)
1393 inserted_space = FALSE;
1394
1395#ifdef FEAT_CINDENT
1396 if (can_cindent && cindent_on()
1397# ifdef FEAT_INS_EXPAND
1398 && ctrl_x_mode == 0
1399# endif
1400 )
1401 {
1402force_cindent:
1403 /*
1404 * Indent now if a key was typed that is in 'cinkeys'.
1405 */
1406 if (in_cinkeys(c, ' ', line_is_white))
1407 {
1408 if (stop_arrow() == OK)
1409 /* re-indent the current line */
1410 do_c_expr_indent();
1411 }
1412 }
1413#endif /* FEAT_CINDENT */
1414
1415 } /* for (;;) */
1416 /* NOTREACHED */
1417}
1418
1419/*
1420 * Redraw for Insert mode.
1421 * This is postponed until getting the next character to make '$' in the 'cpo'
1422 * option work correctly.
1423 * Only redraw when there are no characters available. This speeds up
1424 * inserting sequences of characters (e.g., for CTRL-R).
1425 */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001426/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001428ins_redraw(ready)
1429 int ready; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430{
1431 if (!char_avail())
1432 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00001433#ifdef FEAT_AUTOCMD
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001434 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1435 * visible, the command might delete it. */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001436 if (ready && has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001437 && !equalpos(last_cursormoved, curwin->w_cursor)
1438# ifdef FEAT_INS_EXPAND
1439 && !pum_visible()
1440# endif
1441 )
Bram Moolenaar754b5602006-02-09 23:53:20 +00001442 {
1443 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1444 last_cursormoved = curwin->w_cursor;
1445 }
1446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 if (must_redraw)
1448 update_screen(0);
1449 else if (clear_cmdline || redraw_cmdline)
1450 showmode(); /* clear cmdline and show mode */
1451 showruler(FALSE);
1452 setcursor();
1453 emsg_on_display = FALSE; /* may remove error message now */
1454 }
1455}
1456
1457/*
1458 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1459 */
1460 static void
1461ins_ctrl_v()
1462{
1463 int c;
1464
1465 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001466 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467
1468 if (redrawing() && !char_avail())
1469 edit_putchar('^', TRUE);
1470 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1471
1472#ifdef FEAT_CMDL_INFO
1473 add_to_showcmd_c(Ctrl_V);
1474#endif
1475
1476 c = get_literal();
1477#ifdef FEAT_CMDL_INFO
1478 clear_showcmd();
1479#endif
1480 insert_special(c, FALSE, TRUE);
1481#ifdef FEAT_RIGHTLEFT
1482 revins_chars++;
1483 revins_legal++;
1484#endif
1485}
1486
1487/*
1488 * Put a character directly onto the screen. It's not stored in a buffer.
1489 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1490 */
1491static int pc_status;
1492#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1493#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1494#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1495#define PC_STATUS_SET 3 /* pc_bytes was filled */
1496#ifdef FEAT_MBYTE
1497static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1498#else
1499static char_u pc_bytes[2]; /* saved bytes */
1500#endif
1501static int pc_attr;
1502static int pc_row;
1503static int pc_col;
1504
1505 void
1506edit_putchar(c, highlight)
1507 int c;
1508 int highlight;
1509{
1510 int attr;
1511
1512 if (ScreenLines != NULL)
1513 {
1514 update_topline(); /* just in case w_topline isn't valid */
1515 validate_cursor();
1516 if (highlight)
1517 attr = hl_attr(HLF_8);
1518 else
1519 attr = 0;
1520 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1521 pc_col = W_WINCOL(curwin);
1522#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1523 pc_status = PC_STATUS_UNSET;
1524#endif
1525#ifdef FEAT_RIGHTLEFT
1526 if (curwin->w_p_rl)
1527 {
1528 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1529# ifdef FEAT_MBYTE
1530 if (has_mbyte)
1531 {
1532 int fix_col = mb_fix_col(pc_col, pc_row);
1533
1534 if (fix_col != pc_col)
1535 {
1536 screen_putchar(' ', pc_row, fix_col, attr);
1537 --curwin->w_wcol;
1538 pc_status = PC_STATUS_RIGHT;
1539 }
1540 }
1541# endif
1542 }
1543 else
1544#endif
1545 {
1546 pc_col += curwin->w_wcol;
1547#ifdef FEAT_MBYTE
1548 if (mb_lefthalve(pc_row, pc_col))
1549 pc_status = PC_STATUS_LEFT;
1550#endif
1551 }
1552
1553 /* save the character to be able to put it back */
1554#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1555 if (pc_status == PC_STATUS_UNSET)
1556#endif
1557 {
1558 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1559 pc_status = PC_STATUS_SET;
1560 }
1561 screen_putchar(c, pc_row, pc_col, attr);
1562 }
1563}
1564
1565/*
1566 * Undo the previous edit_putchar().
1567 */
1568 void
1569edit_unputchar()
1570{
1571 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1572 {
1573#if defined(FEAT_MBYTE)
1574 if (pc_status == PC_STATUS_RIGHT)
1575 ++curwin->w_wcol;
1576 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1577 redrawWinline(curwin->w_cursor.lnum, FALSE);
1578 else
1579#endif
1580 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1581 }
1582}
1583
1584/*
1585 * Called when p_dollar is set: display a '$' at the end of the changed text
1586 * Only works when cursor is in the line that changes.
1587 */
1588 void
1589display_dollar(col)
1590 colnr_T col;
1591{
1592 colnr_T save_col;
1593
1594 if (!redrawing())
1595 return;
1596
1597 cursor_off();
1598 save_col = curwin->w_cursor.col;
1599 curwin->w_cursor.col = col;
1600#ifdef FEAT_MBYTE
1601 if (has_mbyte)
1602 {
1603 char_u *p;
1604
1605 /* If on the last byte of a multi-byte move to the first byte. */
1606 p = ml_get_curline();
1607 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1608 }
1609#endif
1610 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1611 if (curwin->w_wcol < W_WIDTH(curwin))
1612 {
1613 edit_putchar('$', FALSE);
1614 dollar_vcol = curwin->w_virtcol;
1615 }
1616 curwin->w_cursor.col = save_col;
1617}
1618
1619/*
1620 * Call this function before moving the cursor from the normal insert position
1621 * in insert mode.
1622 */
1623 static void
1624undisplay_dollar()
1625{
1626 if (dollar_vcol)
1627 {
1628 dollar_vcol = 0;
1629 redrawWinline(curwin->w_cursor.lnum, FALSE);
1630 }
1631}
1632
1633/*
1634 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1635 * Keep the cursor on the same character.
1636 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1637 * type == INDENT_DEC decrease indent (for CTRL-D)
1638 * type == INDENT_SET set indent to "amount"
1639 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1640 */
1641 void
1642change_indent(type, amount, round, replaced)
1643 int type;
1644 int amount;
1645 int round;
1646 int replaced; /* replaced character, put on replace stack */
1647{
1648 int vcol;
1649 int last_vcol;
1650 int insstart_less; /* reduction for Insstart.col */
1651 int new_cursor_col;
1652 int i;
1653 char_u *ptr;
1654 int save_p_list;
1655 int start_col;
1656 colnr_T vc;
1657#ifdef FEAT_VREPLACE
1658 colnr_T orig_col = 0; /* init for GCC */
1659 char_u *new_line, *orig_line = NULL; /* init for GCC */
1660
1661 /* VREPLACE mode needs to know what the line was like before changing */
1662 if (State & VREPLACE_FLAG)
1663 {
1664 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1665 orig_col = curwin->w_cursor.col;
1666 }
1667#endif
1668
1669 /* for the following tricks we don't want list mode */
1670 save_p_list = curwin->w_p_list;
1671 curwin->w_p_list = FALSE;
1672 vc = getvcol_nolist(&curwin->w_cursor);
1673 vcol = vc;
1674
1675 /*
1676 * For Replace mode we need to fix the replace stack later, which is only
1677 * possible when the cursor is in the indent. Remember the number of
1678 * characters before the cursor if it's possible.
1679 */
1680 start_col = curwin->w_cursor.col;
1681
1682 /* determine offset from first non-blank */
1683 new_cursor_col = curwin->w_cursor.col;
1684 beginline(BL_WHITE);
1685 new_cursor_col -= curwin->w_cursor.col;
1686
1687 insstart_less = curwin->w_cursor.col;
1688
1689 /*
1690 * If the cursor is in the indent, compute how many screen columns the
1691 * cursor is to the left of the first non-blank.
1692 */
1693 if (new_cursor_col < 0)
1694 vcol = get_indent() - vcol;
1695
1696 if (new_cursor_col > 0) /* can't fix replace stack */
1697 start_col = -1;
1698
1699 /*
1700 * Set the new indent. The cursor will be put on the first non-blank.
1701 */
1702 if (type == INDENT_SET)
1703 (void)set_indent(amount, SIN_CHANGED);
1704 else
1705 {
1706#ifdef FEAT_VREPLACE
1707 int save_State = State;
1708
1709 /* Avoid being called recursively. */
1710 if (State & VREPLACE_FLAG)
1711 State = INSERT;
1712#endif
1713 shift_line(type == INDENT_DEC, round, 1);
1714#ifdef FEAT_VREPLACE
1715 State = save_State;
1716#endif
1717 }
1718 insstart_less -= curwin->w_cursor.col;
1719
1720 /*
1721 * Try to put cursor on same character.
1722 * If the cursor is at or after the first non-blank in the line,
1723 * compute the cursor column relative to the column of the first
1724 * non-blank character.
1725 * If we are not in insert mode, leave the cursor on the first non-blank.
1726 * If the cursor is before the first non-blank, position it relative
1727 * to the first non-blank, counted in screen columns.
1728 */
1729 if (new_cursor_col >= 0)
1730 {
1731 /*
1732 * When changing the indent while the cursor is touching it, reset
1733 * Insstart_col to 0.
1734 */
1735 if (new_cursor_col == 0)
1736 insstart_less = MAXCOL;
1737 new_cursor_col += curwin->w_cursor.col;
1738 }
1739 else if (!(State & INSERT))
1740 new_cursor_col = curwin->w_cursor.col;
1741 else
1742 {
1743 /*
1744 * Compute the screen column where the cursor should be.
1745 */
1746 vcol = get_indent() - vcol;
1747 curwin->w_virtcol = (vcol < 0) ? 0 : vcol;
1748
1749 /*
1750 * Advance the cursor until we reach the right screen column.
1751 */
1752 vcol = last_vcol = 0;
1753 new_cursor_col = -1;
1754 ptr = ml_get_curline();
1755 while (vcol <= (int)curwin->w_virtcol)
1756 {
1757 last_vcol = vcol;
1758#ifdef FEAT_MBYTE
1759 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001760 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 else
1762#endif
1763 ++new_cursor_col;
1764 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1765 }
1766 vcol = last_vcol;
1767
1768 /*
1769 * May need to insert spaces to be able to position the cursor on
1770 * the right screen column.
1771 */
1772 if (vcol != (int)curwin->w_virtcol)
1773 {
1774 curwin->w_cursor.col = new_cursor_col;
1775 i = (int)curwin->w_virtcol - vcol;
1776 ptr = alloc(i + 1);
1777 if (ptr != NULL)
1778 {
1779 new_cursor_col += i;
1780 ptr[i] = NUL;
1781 while (--i >= 0)
1782 ptr[i] = ' ';
1783 ins_str(ptr);
1784 vim_free(ptr);
1785 }
1786 }
1787
1788 /*
1789 * When changing the indent while the cursor is in it, reset
1790 * Insstart_col to 0.
1791 */
1792 insstart_less = MAXCOL;
1793 }
1794
1795 curwin->w_p_list = save_p_list;
1796
1797 if (new_cursor_col <= 0)
1798 curwin->w_cursor.col = 0;
1799 else
1800 curwin->w_cursor.col = new_cursor_col;
1801 curwin->w_set_curswant = TRUE;
1802 changed_cline_bef_curs();
1803
1804 /*
1805 * May have to adjust the start of the insert.
1806 */
1807 if (State & INSERT)
1808 {
1809 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1810 {
1811 if ((int)Insstart.col <= insstart_less)
1812 Insstart.col = 0;
1813 else
1814 Insstart.col -= insstart_less;
1815 }
1816 if ((int)ai_col <= insstart_less)
1817 ai_col = 0;
1818 else
1819 ai_col -= insstart_less;
1820 }
1821
1822 /*
1823 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1824 * If the number of characters before the cursor decreased, need to pop a
1825 * few characters from the replace stack.
1826 * If the number of characters before the cursor increased, need to push a
1827 * few NULs onto the replace stack.
1828 */
1829 if (REPLACE_NORMAL(State) && start_col >= 0)
1830 {
1831 while (start_col > (int)curwin->w_cursor.col)
1832 {
1833 replace_join(0); /* remove a NUL from the replace stack */
1834 --start_col;
1835 }
1836 while (start_col < (int)curwin->w_cursor.col || replaced)
1837 {
1838 replace_push(NUL);
1839 if (replaced)
1840 {
1841 replace_push(replaced);
1842 replaced = NUL;
1843 }
1844 ++start_col;
1845 }
1846 }
1847
1848#ifdef FEAT_VREPLACE
1849 /*
1850 * For VREPLACE mode, we also have to fix the replace stack. In this case
1851 * it is always possible because we backspace over the whole line and then
1852 * put it back again the way we wanted it.
1853 */
1854 if (State & VREPLACE_FLAG)
1855 {
1856 /* If orig_line didn't allocate, just return. At least we did the job,
1857 * even if you can't backspace. */
1858 if (orig_line == NULL)
1859 return;
1860
1861 /* Save new line */
1862 new_line = vim_strsave(ml_get_curline());
1863 if (new_line == NULL)
1864 return;
1865
1866 /* We only put back the new line up to the cursor */
1867 new_line[curwin->w_cursor.col] = NUL;
1868
1869 /* Put back original line */
1870 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1871 curwin->w_cursor.col = orig_col;
1872
1873 /* Backspace from cursor to start of line */
1874 backspace_until_column(0);
1875
1876 /* Insert new stuff into line again */
1877 ins_bytes(new_line);
1878
1879 vim_free(new_line);
1880 }
1881#endif
1882}
1883
1884/*
1885 * Truncate the space at the end of a line. This is to be used only in an
1886 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1887 * modes.
1888 */
1889 void
1890truncate_spaces(line)
1891 char_u *line;
1892{
1893 int i;
1894
1895 /* find start of trailing white space */
1896 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1897 {
1898 if (State & REPLACE_FLAG)
1899 replace_join(0); /* remove a NUL from the replace stack */
1900 }
1901 line[i + 1] = NUL;
1902}
1903
1904#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1905 || defined(FEAT_COMMENTS) || defined(PROTO)
1906/*
1907 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1908 * modes correctly. May also be used when not in insert mode at all.
1909 */
1910 void
1911backspace_until_column(col)
1912 int col;
1913{
1914 while ((int)curwin->w_cursor.col > col)
1915 {
1916 curwin->w_cursor.col--;
1917 if (State & REPLACE_FLAG)
1918 replace_do_bs();
1919 else
1920 (void)del_char(FALSE);
1921 }
1922}
1923#endif
1924
1925#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1926/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001927 * CTRL-X pressed in Insert mode.
1928 */
1929 static void
1930ins_ctrl_x()
1931{
1932 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
1933 * CTRL-V works like CTRL-N */
1934 if (ctrl_x_mode != CTRL_X_CMDLINE)
1935 {
1936 /* if the next ^X<> won't ADD nothing, then reset
1937 * compl_cont_status */
1938 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001939 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001940 else
1941 compl_cont_status = 0;
1942 /* We're not sure which CTRL-X mode it will be yet */
1943 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
1944 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
1945 edit_submode_pre = NULL;
1946 showmode();
1947 }
1948}
1949
1950/*
1951 * Return TRUE if the 'dict' or 'tsr' option can be used.
1952 */
1953 static int
1954has_compl_option(dict_opt)
1955 int dict_opt;
1956{
Bram Moolenaar0b238792006-03-02 22:49:12 +00001957 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00001958# ifdef FEAT_SPELL
1959 && !curwin->w_p_spell
1960# endif
1961 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001962 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
1963 {
1964 ctrl_x_mode = 0;
1965 edit_submode = NULL;
1966 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
1967 : (char_u *)_("'thesaurus' option is empty"),
1968 hl_attr(HLF_E));
1969 if (emsg_silent == 0)
1970 {
1971 vim_beep();
1972 setcursor();
1973 out_flush();
1974 ui_delay(2000L, FALSE);
1975 }
1976 return FALSE;
1977 }
1978 return TRUE;
1979}
1980
1981/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
1983 * This depends on the current mode.
1984 */
1985 int
1986vim_is_ctrl_x_key(c)
1987 int c;
1988{
1989 /* Always allow ^R - let it's results then be checked */
1990 if (c == Ctrl_R)
1991 return TRUE;
1992
Bram Moolenaare3226be2005-12-18 22:10:00 +00001993 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001994 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00001995 return TRUE;
1996
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 switch (ctrl_x_mode)
1998 {
1999 case 0: /* Not in any CTRL-X mode */
2000 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2001 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002002 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2004 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2005 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002006 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
2007 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 case CTRL_X_SCROLL:
2009 return (c == Ctrl_Y || c == Ctrl_E);
2010 case CTRL_X_WHOLE_LINE:
2011 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2012 case CTRL_X_FILES:
2013 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2014 case CTRL_X_DICTIONARY:
2015 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2016 case CTRL_X_THESAURUS:
2017 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2018 case CTRL_X_TAGS:
2019 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2020#ifdef FEAT_FIND_ID
2021 case CTRL_X_PATH_PATTERNS:
2022 return (c == Ctrl_P || c == Ctrl_N);
2023 case CTRL_X_PATH_DEFINES:
2024 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2025#endif
2026 case CTRL_X_CMDLINE:
2027 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2028 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002029#ifdef FEAT_COMPL_FUNC
2030 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002031 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002032 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002033 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002034#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002035 case CTRL_X_SPELL:
2036 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 }
2038 EMSG(_(e_internal));
2039 return FALSE;
2040}
2041
2042/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002043 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 * case of the originally typed text is used, and the case of the completed
2045 * text is infered, ie this tries to work out what case you probably wanted
2046 * the rest of the word to be in -- webb
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002047 * TODO: make this work for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 */
2049 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002050ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 char_u *str;
2052 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002053 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 char_u *fname;
2055 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002056 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057{
2058 int has_lower = FALSE;
2059 int was_letter = FALSE;
2060 int idx;
2061
2062 if (p_ic && curbuf->b_p_inf && len < IOSIZE)
2063 {
2064 /* Infer case of completed part -- webb */
2065 /* Use IObuff, str would change text in buffer! */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002066 vim_strncpy(IObuff, str, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067
2068 /* Rule 1: Were any chars converted to lower? */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002069 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002071 if (islower(compl_orig_text[idx]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 {
2073 has_lower = TRUE;
2074 if (isupper(IObuff[idx]))
2075 {
2076 /* Rule 1 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002077 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 IObuff[idx] = TOLOWER_LOC(IObuff[idx]);
2079 break;
2080 }
2081 }
2082 }
2083
2084 /*
2085 * Rule 2: No lower case, 2nd consecutive letter converted to
2086 * upper case.
2087 */
2088 if (!has_lower)
2089 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002090 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002092 if (was_letter && isupper(compl_orig_text[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 && islower(IObuff[idx]))
2094 {
2095 /* Rule 2 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002096 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 IObuff[idx] = TOUPPER_LOC(IObuff[idx]);
2098 break;
2099 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002100 was_letter = isalpha(compl_orig_text[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 }
2102 }
2103
2104 /* Copy the original case of the part we typed */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002105 STRNCPY(IObuff, compl_orig_text, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002107 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2108 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002110 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111}
2112
2113/*
2114 * Add a match to the list of matches.
2115 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002116 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002117 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002119 static int
2120ins_compl_add(str, len, icase, fname, cptext, cdir, flags, dup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 char_u *str;
2122 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002123 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 char_u *fname;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002125 char_u **cptext; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002126 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002127 int flags;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002128 int dup; /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002130 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002131 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132
2133 ui_breakcheck();
2134 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002135 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 if (len < 0)
2137 len = (int)STRLEN(str);
2138
2139 /*
2140 * If the same match is already present, don't add it.
2141 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002142 if (compl_first_match != NULL && !dup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002144 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 do
2146 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002147 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002148 && ins_compl_equal(match, str, len)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002149 && match->cp_str[len] == NUL)
2150 return NOTDONE;
2151 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002152 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 }
2154
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002155 /* Remove any popup menu before changing the list of matches. */
2156 ins_compl_del_pum();
2157
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 /*
2159 * Allocate a new match structure.
2160 * Copy the values to the new match structure.
2161 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002162 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002164 return FAIL;
2165 match->cp_number = -1;
2166 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002167 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002168 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 {
2170 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002171 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002173 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002174
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002176 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2178 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002179 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002180 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002181 && compl_curr_match->cp_fname != NULL
2182 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002183 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002184 else if (fname != NULL)
2185 {
2186 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002187 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002190 match->cp_fname = NULL;
2191 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002192
2193 if (cptext != NULL)
2194 {
2195 int i;
2196
2197 for (i = 0; i < CPT_COUNT; ++i)
2198 if (cptext[i] != NULL && *cptext[i] != NUL)
2199 match->cp_text[i] = vim_strsave(cptext[i]);
2200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201
2202 /*
2203 * Link the new match structure in the list of matches.
2204 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002205 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002206 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 else if (dir == FORWARD)
2208 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002209 match->cp_next = compl_curr_match->cp_next;
2210 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 }
2212 else /* BACKWARD */
2213 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002214 match->cp_next = compl_curr_match;
2215 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002217 if (match->cp_next)
2218 match->cp_next->cp_prev = match;
2219 if (match->cp_prev)
2220 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002222 compl_first_match = match;
2223 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002225 /*
2226 * Find the longest common string if still doing that.
2227 */
2228 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2229 ins_compl_longest_match(match);
2230
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 return OK;
2232}
2233
2234/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002235 * Return TRUE if "str[len]" matches with match->cp_str, considering
2236 * match->cp_icase.
2237 */
2238 static int
2239ins_compl_equal(match, str, len)
2240 compl_T *match;
2241 char_u *str;
2242 int len;
2243{
2244 if (match->cp_icase)
2245 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2246 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2247}
2248
2249/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002250 * Reduce the longest common string for match "match".
2251 */
2252 static void
2253ins_compl_longest_match(match)
2254 compl_T *match;
2255{
2256 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002257 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002258 int had_match;
2259
2260 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002261 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002262 /* First match, use it as a whole. */
2263 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002264 if (compl_leader != NULL)
2265 {
2266 had_match = (curwin->w_cursor.col > compl_col);
2267 ins_compl_delete();
2268 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2269 ins_redraw(FALSE);
2270
2271 /* When the match isn't there (to avoid matching itself) remove it
2272 * again after redrawing. */
2273 if (!had_match)
2274 ins_compl_delete();
2275 compl_used_match = FALSE;
2276 }
2277 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002278 else
2279 {
2280 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002281 p = compl_leader;
2282 s = match->cp_str;
2283 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002284 {
2285#ifdef FEAT_MBYTE
2286 if (has_mbyte)
2287 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002288 c1 = mb_ptr2char(p);
2289 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002290 }
2291 else
2292#endif
2293 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002294 c1 = *p;
2295 c2 = *s;
2296 }
2297 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2298 : (c1 != c2))
2299 break;
2300#ifdef FEAT_MBYTE
2301 if (has_mbyte)
2302 {
2303 mb_ptr_adv(p);
2304 mb_ptr_adv(s);
2305 }
2306 else
2307#endif
2308 {
2309 ++p;
2310 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002311 }
2312 }
2313
2314 if (*p != NUL)
2315 {
2316 /* Leader was shortened, need to change the inserted text. */
2317 *p = NUL;
2318 had_match = (curwin->w_cursor.col > compl_col);
2319 ins_compl_delete();
2320 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2321 ins_redraw(FALSE);
2322
2323 /* When the match isn't there (to avoid matching itself) remove it
2324 * again after redrawing. */
2325 if (!had_match)
2326 ins_compl_delete();
2327 }
2328
2329 compl_used_match = FALSE;
2330 }
2331}
2332
2333/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 * Add an array of matches to the list of matches.
2335 * Frees matches[].
2336 */
2337 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002338ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 int num_matches;
2340 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002341 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342{
2343 int i;
2344 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002345 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346
Bram Moolenaar572cb562005-08-05 21:35:02 +00002347 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002348 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002349 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002351 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 FreeWild(num_matches, matches);
2353}
2354
2355/* Make the completion list cyclic.
2356 * Return the number of matches (excluding the original).
2357 */
2358 static int
2359ins_compl_make_cyclic()
2360{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002361 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 int count = 0;
2363
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002364 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 {
2366 /*
2367 * Find the end of the list.
2368 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002369 match = compl_first_match;
2370 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002371 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002373 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 ++count;
2375 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002376 match->cp_next = compl_first_match;
2377 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 }
2379 return count;
2380}
2381
Bram Moolenaara94bc432006-03-10 21:42:59 +00002382/*
2383 * Start completion for the complete() function.
2384 * "startcol" is where the matched text starts (1 is first column).
2385 * "list" is the list of matches.
2386 */
2387 void
2388set_completion(startcol, list)
2389 int startcol;
2390 list_T *list;
2391{
2392 /* If already doing completions stop it. */
2393 if (ctrl_x_mode != 0)
2394 ins_compl_prep(' ');
2395 ins_compl_clear();
2396
2397 if (stop_arrow() == FAIL)
2398 return;
2399
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002400 if (startcol > (int)curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002401 startcol = curwin->w_cursor.col;
2402 compl_col = startcol;
2403 compl_length = curwin->w_cursor.col - startcol;
2404 /* compl_pattern doesn't need to be set */
2405 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2406 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002407 -1, FALSE, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002408 return;
2409
2410 /* Handle like dictionary completion. */
2411 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2412
2413 ins_compl_add_list(list);
2414 compl_matches = ins_compl_make_cyclic();
2415 compl_started = TRUE;
2416 compl_used_match = TRUE;
2417
2418 compl_curr_match = compl_first_match;
2419 ins_complete(Ctrl_N);
2420 out_flush();
2421}
2422
2423
Bram Moolenaar9372a112005-12-06 19:59:18 +00002424/* "compl_match_array" points the currently displayed list of entries in the
2425 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002426static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002427static int compl_match_arraysize;
2428
2429/*
2430 * Update the screen and when there is any scrolling remove the popup menu.
2431 */
2432 static void
2433ins_compl_upd_pum()
2434{
2435 int h;
2436
2437 if (compl_match_array != NULL)
2438 {
2439 h = curwin->w_cline_height;
2440 update_screen(0);
2441 if (h != curwin->w_cline_height)
2442 ins_compl_del_pum();
2443 }
2444}
2445
2446/*
2447 * Remove any popup menu.
2448 */
2449 static void
2450ins_compl_del_pum()
2451{
2452 if (compl_match_array != NULL)
2453 {
2454 pum_undisplay();
2455 vim_free(compl_match_array);
2456 compl_match_array = NULL;
2457 }
2458}
2459
2460/*
2461 * Return TRUE if the popup menu should be displayed.
2462 */
2463 static int
2464pum_wanted()
2465{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002466 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002467 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002468 return FALSE;
2469
2470 /* The display looks bad on a B&W display. */
2471 if (t_colors < 8
2472#ifdef FEAT_GUI
2473 && !gui.in_use
2474#endif
2475 )
2476 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002477 return TRUE;
2478}
2479
2480/*
2481 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002482 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002483 */
2484 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002485pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002486{
2487 compl_T *compl;
2488 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002489
2490 /* Don't display the popup menu if there are no matches or there is only
2491 * one (ignoring the original text). */
2492 compl = compl_first_match;
2493 i = 0;
2494 do
2495 {
2496 if (compl == NULL
2497 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2498 break;
2499 compl = compl->cp_next;
2500 } while (compl != compl_first_match);
2501
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002502 if (strstr((char *)p_cot, "menuone") != NULL)
2503 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002504 return (i >= 2);
2505}
2506
2507/*
2508 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002509 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002510 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002511 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002512ins_compl_show_pum()
2513{
2514 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002515 compl_T *shown_compl = NULL;
2516 int did_find_shown_match = FALSE;
2517 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002518 int i;
2519 int cur = -1;
2520 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002521 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002522
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002523 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002524 return;
2525
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002526#if defined(FEAT_EVAL)
2527 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2528 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2529#endif
2530
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002531 /* Update the screen before drawing the popup menu over it. */
2532 update_screen(0);
2533
2534 if (compl_match_array == NULL)
2535 {
2536 /* Need to build the popup menu list. */
2537 compl_match_arraysize = 0;
2538 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002539 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002540 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002541 do
2542 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002543 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2544 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002545 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002546 ++compl_match_arraysize;
2547 compl = compl->cp_next;
2548 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002549 if (compl_match_arraysize == 0)
2550 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002551 compl_match_array = (pumitem_T *)alloc_clear(
2552 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002553 * compl_match_arraysize));
2554 if (compl_match_array != NULL)
2555 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002556 /* If the current match is the original text don't find the first
2557 * match after it, don't highlight anything. */
2558 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2559 shown_match_ok = TRUE;
2560
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002561 i = 0;
2562 compl = compl_first_match;
2563 do
2564 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002565 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2566 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002567 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002568 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002569 if (!shown_match_ok)
2570 {
2571 if (compl == compl_shown_match || did_find_shown_match)
2572 {
2573 /* This item is the shown match or this is the
2574 * first displayed item after the shown match. */
2575 compl_shown_match = compl;
2576 did_find_shown_match = TRUE;
2577 shown_match_ok = TRUE;
2578 }
2579 else
2580 /* Remember this displayed match for when the
2581 * shown match is just below it. */
2582 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002583 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002584 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002585
2586 if (compl->cp_text[CPT_ABBR] != NULL)
2587 compl_match_array[i].pum_text =
2588 compl->cp_text[CPT_ABBR];
2589 else
2590 compl_match_array[i].pum_text = compl->cp_str;
2591 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2592 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2593 if (compl->cp_text[CPT_MENU] != NULL)
2594 compl_match_array[i++].pum_extra =
2595 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002596 else
2597 compl_match_array[i++].pum_extra = compl->cp_fname;
2598 }
2599
2600 if (compl == compl_shown_match)
2601 {
2602 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002603
2604 /* When the original text is the shown match don't set
2605 * compl_shown_match. */
2606 if (compl->cp_flags & ORIGINAL_TEXT)
2607 shown_match_ok = TRUE;
2608
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002609 if (!shown_match_ok && shown_compl != NULL)
2610 {
2611 /* The shown match isn't displayed, set it to the
2612 * previously displayed match. */
2613 compl_shown_match = shown_compl;
2614 shown_match_ok = TRUE;
2615 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002616 }
2617 compl = compl->cp_next;
2618 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002619
2620 if (!shown_match_ok) /* no displayed match at all */
2621 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002622 }
2623 }
2624 else
2625 {
2626 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002627 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00002628 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
2629 || compl_match_array[i].pum_text
2630 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002631 {
2632 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00002633 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002634 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002635 }
2636
2637 if (compl_match_array != NULL)
2638 {
2639 /* Compute the screen column of the start of the completed text.
2640 * Use the cursor to get all wrapping and other settings right. */
2641 col = curwin->w_cursor.col;
2642 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00002643 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002644 curwin->w_cursor.col = col;
2645 }
2646}
2647
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648#define DICT_FIRST (1) /* use just first element in "dict" */
2649#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002650
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00002652 * Add any identifiers that match the given pattern in the list of dictionary
2653 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 */
2655 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00002656ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
2657 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002659 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002660 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002662 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 char_u *ptr;
2664 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 char_u **files;
2667 int count;
2668 int i;
2669 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002670 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671
Bram Moolenaar0b238792006-03-02 22:49:12 +00002672 if (*dict == NUL)
2673 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002674#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002675 /* When 'dictionary' is empty and spell checking is enabled use
2676 * "spell". */
2677 if (!thesaurus && curwin->w_p_spell)
2678 dict = (char_u *)"spell";
2679 else
2680#endif
2681 return;
2682 }
2683
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002685 if (buf == NULL)
2686 return;
2687
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 /* If 'infercase' is set, don't use 'smartcase' here */
2689 save_p_scs = p_scs;
2690 if (curbuf->b_p_inf)
2691 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002692
2693 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
2694 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002695 * pattern. Also need to double backslashes. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002696 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2697 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002698 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
2699
2700 if (pat_esc == NULL)
2701 return ;
2702 i = (int)STRLEN(pat_esc) + 10;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002703 ptr = alloc(i);
2704 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002705 {
2706 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002707 return;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002708 }
2709 vim_snprintf((char *)ptr, i, "^\\s*\\zs\\V%s", pat_esc);
2710 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
2711 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002712 vim_free(ptr);
2713 }
2714 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00002715 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002716 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002717 if (regmatch.regprog == NULL)
2718 goto theend;
2719 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002720
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2722 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002723 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 {
2725 /* copy one dictionary file name into buf */
2726 if (flags == DICT_EXACT)
2727 {
2728 count = 1;
2729 files = &dict;
2730 }
2731 else
2732 {
2733 /* Expand wildcards in the dictionary name, but do not allow
2734 * backticks (for security, the 'dict' option may have been set in
2735 * a modeline). */
2736 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002737# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002738 if (!thesaurus && STRCMP(buf, "spell") == 0)
2739 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002740 else
2741# endif
2742 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 || expand_wildcards(1, &buf, &count, &files,
2744 EW_FILE|EW_SILENT) != OK)
2745 count = 0;
2746 }
2747
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002748# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002749 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00002751 /* Complete from active spelling. Skip "\<" in the pattern, we
2752 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002753 if (pat[0] == '\\' && pat[1] == '<')
2754 ptr = pat + 2;
2755 else
2756 ptr = pat;
2757 spell_dump_compl(curbuf, ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002759 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002760# endif
Bram Moolenaar0b238792006-03-02 22:49:12 +00002761 {
2762 ins_compl_files(count, files, thesaurus, flags,
2763 &regmatch, buf, &dir);
2764 if (flags != DICT_EXACT)
2765 FreeWild(count, files);
2766 }
2767 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 break;
2769 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002770
2771theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 p_scs = save_p_scs;
2773 vim_free(regmatch.regprog);
2774 vim_free(buf);
2775}
2776
Bram Moolenaar0b238792006-03-02 22:49:12 +00002777 static void
2778ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
2779 int count;
2780 char_u **files;
2781 int thesaurus;
2782 int flags;
2783 regmatch_T *regmatch;
2784 char_u *buf;
2785 int *dir;
2786{
2787 char_u *ptr;
2788 int i;
2789 FILE *fp;
2790 int add_r;
2791
2792 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
2793 {
2794 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2795 if (flags != DICT_EXACT)
2796 {
2797 vim_snprintf((char *)IObuff, IOSIZE,
2798 _("Scanning dictionary: %s"), (char *)files[i]);
2799 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2800 }
2801
2802 if (fp != NULL)
2803 {
2804 /*
2805 * Read dictionary file line by line.
2806 * Check each line for a match.
2807 */
2808 while (!got_int && !compl_interrupted
2809 && !vim_fgets(buf, LSIZE, fp))
2810 {
2811 ptr = buf;
2812 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
2813 {
2814 ptr = regmatch->startp[0];
2815 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2816 ptr = find_line_end(ptr);
2817 else
2818 ptr = find_word_end(ptr);
2819 add_r = ins_compl_add_infercase(regmatch->startp[0],
2820 (int)(ptr - regmatch->startp[0]),
Bram Moolenaar91170f82006-05-05 21:15:17 +00002821 FALSE, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002822 if (thesaurus)
2823 {
2824 char_u *wstart;
2825
2826 /*
2827 * Add the other matches on the line
2828 */
2829 while (!got_int)
2830 {
2831 /* Find start of the next word. Skip white
2832 * space and punctuation. */
2833 ptr = find_word_start(ptr);
2834 if (*ptr == NUL || *ptr == NL)
2835 break;
2836 wstart = ptr;
2837
2838 /* Find end of the word and add it. */
2839#ifdef FEAT_MBYTE
2840 if (has_mbyte)
2841 /* Japanese words may have characters in
2842 * different classes, only separate words
2843 * with single-byte non-word characters. */
2844 while (*ptr != NUL)
2845 {
2846 int l = (*mb_ptr2len)(ptr);
2847
2848 if (l < 2 && !vim_iswordc(*ptr))
2849 break;
2850 ptr += l;
2851 }
2852 else
2853#endif
2854 ptr = find_word_end(ptr);
2855 add_r = ins_compl_add_infercase(wstart,
2856 (int)(ptr - wstart),
Bram Moolenaar91170f82006-05-05 21:15:17 +00002857 FALSE, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002858 }
2859 }
2860 if (add_r == OK)
2861 /* if dir was BACKWARD then honor it just once */
2862 *dir = FORWARD;
2863 else if (add_r == FAIL)
2864 break;
2865 /* avoid expensive call to vim_regexec() when at end
2866 * of line */
2867 if (*ptr == '\n' || got_int)
2868 break;
2869 }
2870 line_breakcheck();
2871 ins_compl_check_keys(50);
2872 }
2873 fclose(fp);
2874 }
2875 }
2876}
2877
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878/*
2879 * Find the start of the next word.
2880 * Returns a pointer to the first char of the word. Also stops at a NUL.
2881 */
2882 char_u *
2883find_word_start(ptr)
2884 char_u *ptr;
2885{
2886#ifdef FEAT_MBYTE
2887 if (has_mbyte)
2888 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002889 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 else
2891#endif
2892 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
2893 ++ptr;
2894 return ptr;
2895}
2896
2897/*
2898 * Find the end of the word. Assumes it starts inside a word.
2899 * Returns a pointer to just after the word.
2900 */
2901 char_u *
2902find_word_end(ptr)
2903 char_u *ptr;
2904{
2905#ifdef FEAT_MBYTE
2906 int start_class;
2907
2908 if (has_mbyte)
2909 {
2910 start_class = mb_get_class(ptr);
2911 if (start_class > 1)
2912 while (*ptr != NUL)
2913 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002914 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 if (mb_get_class(ptr) != start_class)
2916 break;
2917 }
2918 }
2919 else
2920#endif
2921 while (vim_iswordc(*ptr))
2922 ++ptr;
2923 return ptr;
2924}
2925
2926/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002927 * Find the end of the line, omitting CR and NL at the end.
2928 * Returns a pointer to just after the line.
2929 */
2930 static char_u *
2931find_line_end(ptr)
2932 char_u *ptr;
2933{
2934 char_u *s;
2935
2936 s = ptr + STRLEN(ptr);
2937 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
2938 --s;
2939 return s;
2940}
2941
2942/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 * Free the list of completions
2944 */
2945 static void
2946ins_compl_free()
2947{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002948 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002949 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002951 vim_free(compl_pattern);
2952 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002953 vim_free(compl_leader);
2954 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002956 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002958
2959 ins_compl_del_pum();
2960 pum_clear();
2961
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002962 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 do
2964 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002965 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002966 compl_curr_match = compl_curr_match->cp_next;
2967 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002969 if (match->cp_flags & FREE_FNAME)
2970 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00002971 for (i = 0; i < CPT_COUNT; ++i)
2972 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002974 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
2975 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976}
2977
2978 static void
2979ins_compl_clear()
2980{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002981 compl_cont_status = 0;
2982 compl_started = FALSE;
2983 compl_matches = 0;
2984 vim_free(compl_pattern);
2985 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002986 vim_free(compl_leader);
2987 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002989 vim_free(compl_orig_text);
2990 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002991 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992}
2993
2994/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002995 * Return TRUE when Insert completion is active.
2996 */
2997 int
2998ins_compl_active()
2999{
3000 return compl_started;
3001}
3002
3003/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003004 * Delete one character before the cursor and show the subset of the matches
3005 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003006 * Returns the character to be used, NUL if the work is done and another char
3007 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003008 */
3009 static int
3010ins_compl_bs()
3011{
3012 char_u *line;
3013 char_u *p;
3014
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003015 line = ml_get_curline();
3016 p = line + curwin->w_cursor.col;
3017 mb_ptr_back(line, p);
3018
3019 /* Stop completion when the whole word was deleted. */
3020 if ((int)(p - line) - (int)compl_col <= 0)
3021 return K_BS;
3022
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003023 /* Deleted more than what was used to find matches or didn't finish
3024 * finding all matches: need to look for matches all over again. */
3025 if (curwin->w_cursor.col <= compl_col + compl_length
3026 || compl_was_interrupted)
3027 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003028
Bram Moolenaara6557602006-02-04 22:43:20 +00003029 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003030 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003031 if (compl_leader != NULL)
3032 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003033 ins_compl_new_leader();
3034 return NUL;
3035 }
3036 return K_BS;
3037}
Bram Moolenaara6557602006-02-04 22:43:20 +00003038
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003039/*
3040 * Called after changing "compl_leader".
3041 * Show the popup menu with a different set of matches.
3042 * May also search for matches again if the previous search was interrupted.
3043 */
3044 static void
3045ins_compl_new_leader()
3046{
3047 ins_compl_del_pum();
3048 ins_compl_delete();
3049 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3050 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003051
3052 if (compl_started)
3053 ins_compl_set_original_text(compl_leader);
3054 else
3055 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003056#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003057 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003058#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003059 /*
3060 * Matches were cleared, need to search for them now. First display
3061 * the changed text before the cursor. Set "compl_restarting" to
3062 * avoid that the first match is inserted.
3063 */
3064 update_screen(0);
3065#ifdef FEAT_GUI
3066 if (gui.in_use)
3067 {
3068 /* Show the cursor after the match, not after the redrawn text. */
3069 setcursor();
3070 out_flush();
3071 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003072 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003073#endif
3074 compl_restarting = TRUE;
3075 if (ins_complete(Ctrl_N) == FAIL)
3076 compl_cont_status = 0;
3077 compl_restarting = FALSE;
3078 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003079
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003080#if 0 /* disabled, made CTRL-L, BS and typing char jump to original text. */
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003081 if (!compl_used_match)
3082 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003083 /* Go to the original text, since none of the matches is inserted. */
3084 if (compl_first_match->cp_prev != NULL
3085 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3086 compl_shown_match = compl_first_match->cp_prev;
3087 else
3088 compl_shown_match = compl_first_match;
3089 compl_curr_match = compl_shown_match;
3090 compl_shows_dir = compl_direction;
Bram Moolenaara6557602006-02-04 22:43:20 +00003091 }
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003092#endif
3093 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003094
3095 /* Show the popup menu with a different set of matches. */
3096 ins_compl_show_pum();
Bram Moolenaara6557602006-02-04 22:43:20 +00003097}
3098
3099/*
3100 * Append one character to the match leader. May reduce the number of
3101 * matches.
3102 */
3103 static void
3104ins_compl_addleader(c)
3105 int c;
3106{
3107#ifdef FEAT_MBYTE
3108 int cc;
3109
3110 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3111 {
3112 char_u buf[MB_MAXBYTES + 1];
3113
3114 (*mb_char2bytes)(c, buf);
3115 buf[cc] = NUL;
3116 ins_char_bytes(buf, cc);
3117 }
3118 else
3119#endif
3120 ins_char(c);
3121
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003122 /* If we didn't complete finding matches we must search again. */
3123 if (compl_was_interrupted)
3124 ins_compl_restart();
3125
Bram Moolenaara6557602006-02-04 22:43:20 +00003126 vim_free(compl_leader);
3127 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
3128 curwin->w_cursor.col - compl_col);
3129 if (compl_leader != NULL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003130 ins_compl_new_leader();
3131}
3132
3133/*
3134 * Setup for finding completions again without leaving CTRL-X mode. Used when
3135 * BS or a key was typed while still searching for matches.
3136 */
3137 static void
3138ins_compl_restart()
3139{
3140 ins_compl_free();
3141 compl_started = FALSE;
3142 compl_matches = 0;
3143 compl_cont_status = 0;
3144 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003145}
3146
3147/*
3148 * Set the first match, the original text.
3149 */
3150 static void
3151ins_compl_set_original_text(str)
3152 char_u *str;
3153{
3154 char_u *p;
3155
3156 /* Replace the original text entry. */
3157 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3158 {
3159 p = vim_strsave(str);
3160 if (p != NULL)
3161 {
3162 vim_free(compl_first_match->cp_str);
3163 compl_first_match->cp_str = p;
3164 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003165 }
3166}
3167
3168/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003169 * Append one character to the match leader. May reduce the number of
3170 * matches.
3171 */
3172 static void
3173ins_compl_addfrommatch()
3174{
3175 char_u *p;
3176 int len = curwin->w_cursor.col - compl_col;
3177 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003178 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003179
3180 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003181 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003182 {
3183 /* When still at the original match use the first entry that matches
3184 * the leader. */
3185 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3186 {
3187 p = NULL;
3188 for (cp = compl_shown_match->cp_next; cp != NULL
3189 && cp != compl_first_match; cp = cp->cp_next)
3190 {
3191 if (ins_compl_equal(cp, compl_leader,
3192 (int)STRLEN(compl_leader)))
3193 {
3194 p = cp->cp_str;
3195 break;
3196 }
3197 }
3198 if (p == NULL || (int)STRLEN(p) <= len)
3199 return;
3200 }
3201 else
3202 return;
3203 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003204 p += len;
3205#ifdef FEAT_MBYTE
3206 c = mb_ptr2char(p);
3207#else
3208 c = *p;
3209#endif
3210 ins_compl_addleader(c);
3211}
3212
3213/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003215 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003216 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003218 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219ins_compl_prep(c)
3220 int c;
3221{
3222 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 int temp;
3224 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003225 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226
3227 /* Forget any previous 'special' messages if this is actually
3228 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3229 */
3230 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3231 edit_submode_extra = NULL;
3232
3233 /* Ignore end of Select mode mapping */
3234 if (c == K_SELECT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003235 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003237 /* Set "compl_get_longest" when finding the first matches. */
3238 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3239 || (ctrl_x_mode == 0 && !compl_started))
3240 {
3241 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3242 compl_used_match = TRUE;
3243 }
3244
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3246 {
3247 /*
3248 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3249 * it will be yet. Now we decide.
3250 */
3251 switch (c)
3252 {
3253 case Ctrl_E:
3254 case Ctrl_Y:
3255 ctrl_x_mode = CTRL_X_SCROLL;
3256 if (!(State & REPLACE_FLAG))
3257 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3258 else
3259 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3260 edit_submode_pre = NULL;
3261 showmode();
3262 break;
3263 case Ctrl_L:
3264 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3265 break;
3266 case Ctrl_F:
3267 ctrl_x_mode = CTRL_X_FILES;
3268 break;
3269 case Ctrl_K:
3270 ctrl_x_mode = CTRL_X_DICTIONARY;
3271 break;
3272 case Ctrl_R:
3273 /* Simply allow ^R to happen without affecting ^X mode */
3274 break;
3275 case Ctrl_T:
3276 ctrl_x_mode = CTRL_X_THESAURUS;
3277 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003278#ifdef FEAT_COMPL_FUNC
3279 case Ctrl_U:
3280 ctrl_x_mode = CTRL_X_FUNCTION;
3281 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003282 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003283 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003284 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003285#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003286 case 's':
3287 case Ctrl_S:
3288 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003289#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003290 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003291 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003292 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003293#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003294 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 case Ctrl_RSB:
3296 ctrl_x_mode = CTRL_X_TAGS;
3297 break;
3298#ifdef FEAT_FIND_ID
3299 case Ctrl_I:
3300 case K_S_TAB:
3301 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3302 break;
3303 case Ctrl_D:
3304 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3305 break;
3306#endif
3307 case Ctrl_V:
3308 case Ctrl_Q:
3309 ctrl_x_mode = CTRL_X_CMDLINE;
3310 break;
3311 case Ctrl_P:
3312 case Ctrl_N:
3313 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3314 * just started ^X mode, or there were enough ^X's to cancel
3315 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3316 * do normal expansion when interrupting a different mode (say
3317 * ^X^F^X^P or ^P^X^X^P, see below)
3318 * nothing changes if interrupting mode 0, (eg, the flag
3319 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003320 if (!(compl_cont_status & CONT_INTRPT))
3321 compl_cont_status |= CONT_LOCAL;
3322 else if (compl_cont_mode != 0)
3323 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 /* FALLTHROUGH */
3325 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003326 /* If we have typed at least 2 ^X's... for modes != 0, we set
3327 * compl_cont_status = 0 (eg, as if we had just started ^X
3328 * mode).
3329 * For mode 0, we set "compl_cont_mode" to an impossible
3330 * value, in both cases ^X^X can be used to restart the same
3331 * mode (avoiding ADDING mode).
3332 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3333 * 'complete' and local ^P expansions respectively.
3334 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3335 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 if (c == Ctrl_X)
3337 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003338 if (compl_cont_mode != 0)
3339 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003341 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 }
3343 ctrl_x_mode = 0;
3344 edit_submode = NULL;
3345 showmode();
3346 break;
3347 }
3348 }
3349 else if (ctrl_x_mode != 0)
3350 {
3351 /* We're already in CTRL-X mode, do we stay in it? */
3352 if (!vim_is_ctrl_x_key(c))
3353 {
3354 if (ctrl_x_mode == CTRL_X_SCROLL)
3355 ctrl_x_mode = 0;
3356 else
3357 ctrl_x_mode = CTRL_X_FINISHED;
3358 edit_submode = NULL;
3359 }
3360 showmode();
3361 }
3362
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003363 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 {
3365 /* Show error message from attempted keyword completion (probably
3366 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003367 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003369 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3370 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 || ctrl_x_mode == CTRL_X_FINISHED)
3372 {
3373 /* Get here when we have finished typing a sequence of ^N and
3374 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003375 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003376 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003378 char_u *p;
3379
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003381 * If any of the original typed text has been changed, eg when
3382 * ignorecase is set, we must add back-spaces to the redo
3383 * buffer. We add as few as necessary to delete just the part
3384 * of the original text that has changed.
3385 * When using the longest match, edited the match or used
3386 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003388 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3389 ptr = compl_curr_match->cp_str;
3390 else if (compl_leader != NULL)
3391 ptr = compl_leader;
3392 else
3393 ptr = compl_orig_text;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003394 if (compl_orig_text != NULL)
3395 {
3396 p = compl_orig_text;
3397 for (temp = 0; p[temp] != NUL && p[temp] == ptr[temp];
3398 ++temp)
3399 ;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003400#ifdef FEAT_MBYTE
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003401 if (temp > 0)
3402 temp -= (*mb_head_off)(compl_orig_text, p + temp);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003403#endif
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003404 for (p += temp; *p != NUL; mb_ptr_adv(p))
3405 AppendCharToRedobuff(K_BS);
3406 }
3407 if (ptr != NULL)
3408 AppendToRedobuffLit(ptr + temp, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 }
3410
3411#ifdef FEAT_CINDENT
3412 want_cindent = (can_cindent && cindent_on());
3413#endif
3414 /*
3415 * When completing whole lines: fix indent for 'cindent'.
3416 * Otherwise, break line if it's too long.
3417 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003418 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 {
3420#ifdef FEAT_CINDENT
3421 /* re-indent the current line */
3422 if (want_cindent)
3423 {
3424 do_c_expr_indent();
3425 want_cindent = FALSE; /* don't do it again */
3426 }
3427#endif
3428 }
3429 else
3430 {
3431 /* put the cursor on the last char, for 'tw' formatting */
3432 curwin->w_cursor.col--;
3433 if (stop_arrow() == OK)
3434 insertchar(NUL, 0, -1);
3435 curwin->w_cursor.col++;
3436 }
3437
3438 auto_format(FALSE, TRUE);
3439
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003440 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003441 * the selection without inserting anything. When
3442 * compl_enter_selects is set the Enter key does the same. */
3443 if ((c == Ctrl_Y || (compl_enter_selects
3444 && (c == CAR || c == K_KENTER || c == NL)))
3445 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003446 retval = TRUE;
3447
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003448 /* CTRL-E means completion is Ended, go back to the typed text. */
3449 if (c == Ctrl_E)
3450 {
3451 ins_compl_delete();
3452 if (compl_leader != NULL)
3453 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3454 else if (compl_first_match != NULL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003455 ins_bytes(compl_orig_text
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003456 + curwin->w_cursor.col - compl_col);
3457 retval = TRUE;
3458 }
3459
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003461 compl_started = FALSE;
3462 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 msg_clr_cmdline(); /* necessary for "noshowmode" */
3464 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003465 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 if (edit_submode != NULL)
3467 {
3468 edit_submode = NULL;
3469 showmode();
3470 }
3471
3472#ifdef FEAT_CINDENT
3473 /*
3474 * Indent now if a key was typed that is in 'cinkeys'.
3475 */
3476 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3477 do_c_expr_indent();
3478#endif
3479 }
3480 }
3481
3482 /* reset continue_* if we left expansion-mode, if we stay they'll be
3483 * (re)set properly in ins_complete() */
3484 if (!vim_is_ctrl_x_key(c))
3485 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003486 compl_cont_status = 0;
3487 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003489
3490 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491}
3492
3493/*
3494 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3495 * (depending on flag) starting from buf and looking for a non-scanned
3496 * buffer (other than curbuf). curbuf is special, if it is called with
3497 * buf=curbuf then it has to be the first call for a given flag/expansion.
3498 *
3499 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3500 */
3501 static buf_T *
3502ins_compl_next_buf(buf, flag)
3503 buf_T *buf;
3504 int flag;
3505{
3506#ifdef FEAT_WINDOWS
3507 static win_T *wp;
3508#endif
3509
3510 if (flag == 'w') /* just windows */
3511 {
3512#ifdef FEAT_WINDOWS
3513 if (buf == curbuf) /* first call for this flag/expansion */
3514 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003515 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 && wp->w_buffer->b_scanned)
3517 ;
3518 buf = wp->w_buffer;
3519#else
3520 buf = curbuf;
3521#endif
3522 }
3523 else
3524 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3525 * (unlisted buffers)
3526 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003527 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 && ((flag == 'U'
3529 ? buf->b_p_bl
3530 : (!buf->b_p_bl
3531 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003532 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 ;
3534 return buf;
3535}
3536
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003537#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003538static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003539
3540/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003541 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003542 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003543 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003544 static void
3545expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003546 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003547 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003548{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003549 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003550 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003551 char_u *funcname;
3552 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003553
Bram Moolenaare344bea2005-09-01 20:46:49 +00003554 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3555 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003556 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003557
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003558 /* Call 'completefunc' to obtain the list of matches. */
3559 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003560 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003561
Bram Moolenaare344bea2005-09-01 20:46:49 +00003562 pos = curwin->w_cursor;
3563 matchlist = call_func_retlist(funcname, 2, args, FALSE);
3564 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003565 if (matchlist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003566 return;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003567
Bram Moolenaara94bc432006-03-10 21:42:59 +00003568 ins_compl_add_list(matchlist);
3569 list_unref(matchlist);
3570}
3571#endif /* FEAT_COMPL_FUNC */
3572
Bram Moolenaar39f05632006-03-19 22:15:26 +00003573#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00003574/*
3575 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00003576 */
3577 static void
3578ins_compl_add_list(list)
3579 list_T *list;
3580{
3581 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003582 int dir = compl_direction;
3583
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003584 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003585 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003586 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00003587 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
3588 /* if dir was BACKWARD then honor it just once */
3589 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00003590 else if (did_emsg)
3591 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003592 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003593}
Bram Moolenaar39f05632006-03-19 22:15:26 +00003594
3595/*
3596 * Add a match to the list of matches from a typeval_T.
3597 * If the given string is already in the list of completions, then return
3598 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
3599 * maybe because alloc() returns NULL, then FAIL is returned.
3600 */
3601 int
3602ins_compl_add_tv(tv, dir)
3603 typval_T *tv;
3604 int dir;
3605{
3606 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00003607 int icase = FALSE;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003608 int dup = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003609 char_u *(cptext[CPT_COUNT]);
3610
3611 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3612 {
3613 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
3614 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
3615 (char_u *)"abbr", FALSE);
3616 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
3617 (char_u *)"menu", FALSE);
3618 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
3619 (char_u *)"kind", FALSE);
3620 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
3621 (char_u *)"info", FALSE);
3622 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
3623 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003624 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
3625 dup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar39f05632006-03-19 22:15:26 +00003626 }
3627 else
3628 {
3629 word = get_tv_string_chk(tv);
3630 vim_memset(cptext, 0, sizeof(cptext));
3631 }
3632 if (word == NULL || *word == NUL)
3633 return FAIL;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003634 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, dup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003635}
Bram Moolenaara94bc432006-03-10 21:42:59 +00003636#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003637
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003638/*
3639 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003640 * The search starts at position "ini" in curbuf and in the direction
3641 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003642 * When "compl_started" is FALSE start at that position, otherwise continue
3643 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003644 * This may return before finding all the matches.
3645 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 */
3647 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003648ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650{
3651 static pos_T first_match_pos;
3652 static pos_T last_match_pos;
3653 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003654 static int found_all = FALSE; /* Found all matches of a
3655 certain type. */
3656 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657
Bram Moolenaar572cb562005-08-05 21:35:02 +00003658 pos_T *pos;
3659 char_u **matches;
3660 int save_p_scs;
3661 int save_p_ws;
3662 int save_p_ic;
3663 int i;
3664 int num_matches;
3665 int len;
3666 int found_new_match;
3667 int type = ctrl_x_mode;
3668 char_u *ptr;
3669 char_u *dict = NULL;
3670 int dict_f = 0;
3671 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003673 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 {
3675 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3676 ins_buf->b_scanned = 0;
3677 found_all = FALSE;
3678 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003679 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003680 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 last_match_pos = first_match_pos = *ini;
3682 }
3683
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003684 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003685 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3687 for (;;)
3688 {
3689 found_new_match = FAIL;
3690
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003691 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 * or if found_all says this entry is done. For ^X^L only use the
3693 * entries from 'complete' that look in loaded buffers. */
3694 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003695 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 {
3697 found_all = FALSE;
3698 while (*e_cpt == ',' || *e_cpt == ' ')
3699 e_cpt++;
3700 if (*e_cpt == '.' && !curbuf->b_scanned)
3701 {
3702 ins_buf = curbuf;
3703 first_match_pos = *ini;
3704 /* So that ^N can match word immediately after cursor */
3705 if (ctrl_x_mode == 0)
3706 dec(&first_match_pos);
3707 last_match_pos = first_match_pos;
3708 type = 0;
3709 }
3710 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
3711 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
3712 {
3713 /* Scan a buffer, but not the current one. */
3714 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
3715 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003716 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 first_match_pos.col = last_match_pos.col = 0;
3718 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
3719 last_match_pos.lnum = 0;
3720 type = 0;
3721 }
3722 else /* unloaded buffer, scan like dictionary */
3723 {
3724 found_all = TRUE;
3725 if (ins_buf->b_fname == NULL)
3726 continue;
3727 type = CTRL_X_DICTIONARY;
3728 dict = ins_buf->b_fname;
3729 dict_f = DICT_EXACT;
3730 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00003731 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 ins_buf->b_fname == NULL
3733 ? buf_spname(ins_buf)
3734 : ins_buf->b_sfname == NULL
3735 ? (char *)ins_buf->b_fname
3736 : (char *)ins_buf->b_sfname);
3737 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3738 }
3739 else if (*e_cpt == NUL)
3740 break;
3741 else
3742 {
3743 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3744 type = -1;
3745 else if (*e_cpt == 'k' || *e_cpt == 's')
3746 {
3747 if (*e_cpt == 'k')
3748 type = CTRL_X_DICTIONARY;
3749 else
3750 type = CTRL_X_THESAURUS;
3751 if (*++e_cpt != ',' && *e_cpt != NUL)
3752 {
3753 dict = e_cpt;
3754 dict_f = DICT_FIRST;
3755 }
3756 }
3757#ifdef FEAT_FIND_ID
3758 else if (*e_cpt == 'i')
3759 type = CTRL_X_PATH_PATTERNS;
3760 else if (*e_cpt == 'd')
3761 type = CTRL_X_PATH_DEFINES;
3762#endif
3763 else if (*e_cpt == ']' || *e_cpt == 't')
3764 {
3765 type = CTRL_X_TAGS;
3766 sprintf((char*)IObuff, _("Scanning tags."));
3767 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3768 }
3769 else
3770 type = -1;
3771
3772 /* in any case e_cpt is advanced to the next entry */
3773 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
3774
3775 found_all = TRUE;
3776 if (type == -1)
3777 continue;
3778 }
3779 }
3780
3781 switch (type)
3782 {
3783 case -1:
3784 break;
3785#ifdef FEAT_FIND_ID
3786 case CTRL_X_PATH_PATTERNS:
3787 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003788 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003789 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003791 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3793 (linenr_T)1, (linenr_T)MAXLNUM);
3794 break;
3795#endif
3796
3797 case CTRL_X_DICTIONARY:
3798 case CTRL_X_THESAURUS:
3799 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00003800 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 : (type == CTRL_X_THESAURUS
3802 ? (*curbuf->b_p_tsr == NUL
3803 ? p_tsr
3804 : curbuf->b_p_tsr)
3805 : (*curbuf->b_p_dict == NUL
3806 ? p_dict
3807 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003808 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00003809 dict != NULL ? dict_f
3810 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 dict = NULL;
3812 break;
3813
3814 case CTRL_X_TAGS:
3815 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
3816 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003817 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818
3819 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003820 * of matches is found when compl_pattern is empty */
3821 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
3823 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
3824 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3825 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00003826 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 }
3828 p_ic = save_p_ic;
3829 break;
3830
3831 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003832 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
3834 {
3835
3836 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003837 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003838 ins_compl_add_matches(num_matches, matches,
3839#ifdef CASE_INSENSITIVE_FILENAME
3840 TRUE
3841#else
3842 FALSE
3843#endif
3844 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 }
3846 break;
3847
3848 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003849 if (expand_cmdline(&compl_xp, compl_pattern,
3850 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003852 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 break;
3854
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003855#ifdef FEAT_COMPL_FUNC
3856 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003857 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003858 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003859 break;
3860#endif
3861
Bram Moolenaar488c6512005-08-11 20:09:58 +00003862 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003863#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00003864 num_matches = expand_spelling(first_match_pos.lnum,
3865 first_match_pos.col, compl_pattern, &matches);
3866 if (num_matches > 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003867 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar488c6512005-08-11 20:09:58 +00003868#endif
3869 break;
3870
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 default: /* normal ^P/^N and ^X^L */
3872 /*
3873 * If 'infercase' is set, don't use 'smartcase' here
3874 */
3875 save_p_scs = p_scs;
3876 if (ins_buf->b_p_inf)
3877 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003878
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 /* buffers other than curbuf are scanned from the beginning or the
3880 * end but never from the middle, thus setting nowrapscan in this
3881 * buffers is a good idea, on the other hand, we always set
3882 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
3883 save_p_ws = p_ws;
3884 if (ins_buf != curbuf)
3885 p_ws = FALSE;
3886 else if (*e_cpt == '.')
3887 p_ws = TRUE;
3888 for (;;)
3889 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003890 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003892 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
3893 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003895 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003897 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003899 found_new_match = searchit(NULL, ins_buf, pos,
3900 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003901 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003902 RE_LAST, (linenr_T)0);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003903 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003905 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003906 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 first_match_pos = *pos;
3908 last_match_pos = *pos;
3909 }
3910 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003911 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 found_new_match = FAIL;
3913 if (found_new_match == FAIL)
3914 {
3915 if (ins_buf == curbuf)
3916 found_all = TRUE;
3917 break;
3918 }
3919
3920 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003921 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 && ini->lnum == pos->lnum
3923 && ini->col == pos->col)
3924 continue;
3925 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
3926 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3927 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003928 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 {
3930 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3931 continue;
3932 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3933 if (!p_paste)
3934 ptr = skipwhite(ptr);
3935 }
3936 len = (int)STRLEN(ptr);
3937 }
3938 else
3939 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003940 char_u *tmp_ptr = ptr;
3941
3942 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003944 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 /* Skip if already inside a word. */
3946 if (vim_iswordp(tmp_ptr))
3947 continue;
3948 /* Find start of next word. */
3949 tmp_ptr = find_word_start(tmp_ptr);
3950 }
3951 /* Find end of this word. */
3952 tmp_ptr = find_word_end(tmp_ptr);
3953 len = (int)(tmp_ptr - ptr);
3954
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003955 if ((compl_cont_status & CONT_ADDING)
3956 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 {
3958 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3959 {
3960 /* Try next line, if any. the new word will be
3961 * "join" as if the normal command "J" was used.
3962 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003963 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 * works -- Acevedo */
3965 STRNCPY(IObuff, ptr, len);
3966 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3967 tmp_ptr = ptr = skipwhite(ptr);
3968 /* Find start of next word. */
3969 tmp_ptr = find_word_start(tmp_ptr);
3970 /* Find end of next word. */
3971 tmp_ptr = find_word_end(tmp_ptr);
3972 if (tmp_ptr > ptr)
3973 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003974 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003976 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 IObuff[len++] = ' ';
3978 /* IObuf =~ "\k.* ", thus len >= 2 */
3979 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003980 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 || (vim_strchr(p_cpo, CPO_JOINSP)
3982 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003983 && (IObuff[len - 2] == '?'
3984 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 IObuff[len++] = ' ';
3986 }
3987 /* copy as much as posible of the new word */
3988 if (tmp_ptr - ptr >= IOSIZE - len)
3989 tmp_ptr = ptr + IOSIZE - len - 1;
3990 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3991 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00003992 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 }
3994 IObuff[len] = NUL;
3995 ptr = IObuff;
3996 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003997 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 continue;
3999 }
4000 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004001 if (ins_compl_add_infercase(ptr, len, FALSE,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004002 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004003 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 {
4005 found_new_match = OK;
4006 break;
4007 }
4008 }
4009 p_scs = save_p_scs;
4010 p_ws = save_p_ws;
4011 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004012
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004013 /* check if compl_curr_match has changed, (e.g. other type of
4014 * expansion added somenthing) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004015 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 found_new_match = OK;
4017
4018 /* break the loop for specialized modes (use 'complete' just for the
4019 * generic ctrl_x_mode == 0) or when we've found a new match */
4020 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004021 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004022 {
4023 if (got_int)
4024 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004025 /* Fill the popup menu as soon as possible. */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004026 if (pum_wanted() && type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004027 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004028
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004029 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
4030 || compl_interrupted)
4031 break;
4032 compl_started = TRUE;
4033 }
4034 else
4035 {
4036 /* Mark a buffer scanned when it has been scanned completely */
4037 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4038 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004040 compl_started = FALSE;
4041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004043 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044
4045 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
4046 && *e_cpt == NUL) /* Got to end of 'complete' */
4047 found_new_match = FAIL;
4048
4049 i = -1; /* total of matches, unknown */
4050 if (found_new_match == FAIL
4051 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
4052 i = ins_compl_make_cyclic();
4053
4054 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004055 * just been made cyclic then we have to move compl_curr_match to the next
4056 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004057 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4058 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004059 if (compl_curr_match == NULL)
4060 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 return i;
4062}
4063
4064/* Delete the old text being completed. */
4065 static void
4066ins_compl_delete()
4067{
4068 int i;
4069
4070 /*
4071 * In insert mode: Delete the typed part.
4072 * In replace mode: Put the old characters back, if any.
4073 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004074 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 backspace_until_column(i);
4076 changed_cline_bef_curs();
4077}
4078
4079/* Insert the new text being completed. */
4080 static void
4081ins_compl_insert()
4082{
Bram Moolenaar572cb562005-08-05 21:35:02 +00004083 ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004084 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4085 compl_used_match = FALSE;
4086 else
4087 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088}
4089
4090/*
4091 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004092 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4093 * get more completions. If it is FALSE, then we just do nothing when there
4094 * are no more completions in a given direction. The latter case is used when
4095 * we are still in the middle of finding completions, to allow browsing
4096 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 * Return the total number of matches, or -1 if still unknown -- webb.
4098 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004099 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4100 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 *
4102 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004103 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4104 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 */
4106 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004107ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004109 int count; /* repeat completion this many times; should
4110 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004111 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112{
4113 int num_matches = -1;
4114 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004115 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004116 compl_T *found_compl = NULL;
4117 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004118 int advance;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004120 if (compl_leader != NULL
4121 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004123 /* Set "compl_shown_match" to the actually shown match, it may differ
4124 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004125 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004126 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004127 && compl_shown_match->cp_next != NULL
4128 && compl_shown_match->cp_next != compl_first_match)
4129 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004130
4131 /* If we didn't find it searching forward, and compl_shows_dir is
4132 * backward, find the last match. */
4133 if (compl_shows_dir == BACKWARD
4134 && !ins_compl_equal(compl_shown_match,
4135 compl_leader, (int)STRLEN(compl_leader))
4136 && (compl_shown_match->cp_next == NULL
4137 || compl_shown_match->cp_next == compl_first_match))
4138 {
4139 while (!ins_compl_equal(compl_shown_match,
4140 compl_leader, (int)STRLEN(compl_leader))
4141 && compl_shown_match->cp_prev != NULL
4142 && compl_shown_match->cp_prev != compl_first_match)
4143 compl_shown_match = compl_shown_match->cp_prev;
4144 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004145 }
4146
4147 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004148 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 /* Delete old text to be replaced */
4150 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004151
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004152 /* When finding the longest common text we stick at the original text,
4153 * don't let CTRL-N or CTRL-P move to the first match. */
4154 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4155
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004156 /* When restarting the search don't insert the first match either. */
4157 if (compl_restarting)
4158 {
4159 advance = FALSE;
4160 compl_restarting = FALSE;
4161 }
4162
Bram Moolenaare3226be2005-12-18 22:10:00 +00004163 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4164 * around. */
4165 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004167 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004169 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004170 found_end = (compl_first_match != NULL
4171 && (compl_shown_match->cp_next == compl_first_match
4172 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004173 }
4174 else if (compl_shows_dir == BACKWARD
4175 && compl_shown_match->cp_prev != NULL)
4176 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004177 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004178 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004179 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 }
4181 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004182 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004183 if (!allow_get_expansion)
4184 {
4185 if (advance)
4186 {
4187 if (compl_shows_dir == BACKWARD)
4188 compl_pending -= todo + 1;
4189 else
4190 compl_pending += todo + 1;
4191 }
4192 return -1;
4193 }
4194
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004195 if (advance)
4196 {
4197 if (compl_shows_dir == BACKWARD)
4198 --compl_pending;
4199 else
4200 ++compl_pending;
4201 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004202
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004203 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004204 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004205
4206 /* handle any pending completions */
4207 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004208 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004209 {
4210 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4211 {
4212 compl_shown_match = compl_shown_match->cp_next;
4213 --compl_pending;
4214 }
4215 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4216 {
4217 compl_shown_match = compl_shown_match->cp_prev;
4218 ++compl_pending;
4219 }
4220 else
4221 break;
4222 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004223 found_end = FALSE;
4224 }
4225 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4226 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004227 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004228 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004229 ++todo;
4230 else
4231 /* Remember a matching item. */
4232 found_compl = compl_shown_match;
4233
4234 /* Stop at the end of the list when we found a usable match. */
4235 if (found_end)
4236 {
4237 if (found_compl != NULL)
4238 {
4239 compl_shown_match = found_compl;
4240 break;
4241 }
4242 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 }
4245
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004246 /* Insert the text of the new completion, or the compl_leader. */
4247 if (insert_match)
4248 {
4249 if (!compl_get_longest || compl_used_match)
4250 ins_compl_insert();
4251 else
4252 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
4253 }
4254 else
4255 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256
4257 if (!allow_get_expansion)
4258 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004259 /* may undisplay the popup menu first */
4260 ins_compl_upd_pum();
4261
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004262 /* redraw to show the user what was inserted */
4263 update_screen(0);
4264
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004265 /* display the updated popup menu */
4266 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004267#ifdef FEAT_GUI
4268 if (gui.in_use)
4269 {
4270 /* Show the cursor after the match, not after the redrawn text. */
4271 setcursor();
4272 out_flush();
4273 gui_update_cursor(FALSE, FALSE);
4274 }
4275#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004276
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 /* Delete old text to be replaced, since we're still searching and
4278 * don't want to match ourselves! */
4279 ins_compl_delete();
4280 }
4281
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004282 /* Enter will select a match when the match wasn't inserted and the popup
4283 * menu is visislbe. */
4284 compl_enter_selects = !insert_match && compl_match_array != NULL;
4285
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 /*
4287 * Show the file name for the match (if any)
4288 * Truncate the file name to avoid a wait for return.
4289 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004290 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 {
4292 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004293 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 if (i <= 0)
4295 i = 0;
4296 else
4297 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004298 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 msg(IObuff);
4300 redraw_cmdline = FALSE; /* don't overwrite! */
4301 }
4302
4303 return num_matches;
4304}
4305
4306/*
4307 * Call this while finding completions, to check whether the user has hit a key
4308 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004309 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004311 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 */
4313 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004314ins_compl_check_keys(frequency)
4315 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316{
4317 static int count = 0;
4318
4319 int c;
4320
4321 /* Don't check when reading keys from a script. That would break the test
4322 * scripts */
4323 if (using_script())
4324 return;
4325
4326 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004327 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 return;
4329 count = 0;
4330
Bram Moolenaara260a972006-06-23 19:36:29 +00004331 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4332 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 if (c != NUL)
4335 {
4336 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4337 {
4338 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004339 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004340 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4341 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004343 else
4344 {
4345 /* Need to get the character to have KeyTyped set. We'll put it
4346 * back with vungetc() below. */
4347 c = safe_vgetc();
4348
4349 /* Don't interrupt completion when the character wasn't typed,
4350 * e.g., when doing @q to replay keys. */
4351 if (c != Ctrl_R && KeyTyped)
4352 compl_interrupted = TRUE;
4353
4354 vungetc(c);
4355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004357 if (compl_pending != 0 && !got_int)
Bram Moolenaara260a972006-06-23 19:36:29 +00004358 {
4359 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4360
4361 compl_pending = 0;
4362 (void)ins_compl_next(FALSE, todo, TRUE);
4363 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004364}
4365
4366/*
4367 * Decide the direction of Insert mode complete from the key typed.
4368 * Returns BACKWARD or FORWARD.
4369 */
4370 static int
4371ins_compl_key2dir(c)
4372 int c;
4373{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004374 if (c == Ctrl_P || c == Ctrl_L
4375 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4376 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004377 return BACKWARD;
4378 return FORWARD;
4379}
4380
4381/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004382 * Return TRUE for keys that are used for completion only when the popup menu
4383 * is visible.
4384 */
4385 static int
4386ins_compl_pum_key(c)
4387 int c;
4388{
4389 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004390 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4391 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004392}
4393
4394/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004395 * Decide the number of completions to move forward.
4396 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4397 */
4398 static int
4399ins_compl_key2count(c)
4400 int c;
4401{
4402 int h;
4403
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004404 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004405 {
4406 h = pum_get_height();
4407 if (h > 3)
4408 h -= 2; /* keep some context */
4409 return h;
4410 }
4411 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412}
4413
4414/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004415 * Return TRUE if completion with "c" should insert the match, FALSE if only
4416 * to change the currently selected completion.
4417 */
4418 static int
4419ins_compl_use_match(c)
4420 int c;
4421{
4422 switch (c)
4423 {
4424 case K_UP:
4425 case K_DOWN:
4426 case K_PAGEDOWN:
4427 case K_KPAGEDOWN:
4428 case K_S_DOWN:
4429 case K_PAGEUP:
4430 case K_KPAGEUP:
4431 case K_S_UP:
4432 return FALSE;
4433 }
4434 return TRUE;
4435}
4436
4437/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 * Do Insert mode completion.
4439 * Called when character "c" was typed, which has a meaning for completion.
4440 * Returns OK if completion was done, FAIL if something failed (out of mem).
4441 */
4442 static int
4443ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004444 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004446 char_u *line;
4447 int startcol = 0; /* column where searched text starts */
4448 colnr_T curs_col; /* cursor column */
4449 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450
Bram Moolenaare3226be2005-12-18 22:10:00 +00004451 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004452 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 {
4454 /* First time we hit ^N or ^P (in a row, I mean) */
4455
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 did_ai = FALSE;
4457#ifdef FEAT_SMARTINDENT
4458 did_si = FALSE;
4459 can_si = FALSE;
4460 can_si_back = FALSE;
4461#endif
4462 if (stop_arrow() == FAIL)
4463 return FAIL;
4464
4465 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004466 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004467 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468
4469 /* if this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004470 * "compl_startpos" to the cursor as a pattern to add a new word
4471 * instead of expand the one before the cursor, in word-wise if
4472 * "compl_startpos"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 * is not in the same line as the cursor then fix it (the line has
4474 * been split because it was longer than 'tw'). if SOL is set then
4475 * skip the previous pattern, a word at the beginning of the line has
4476 * been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004477 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4478 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 {
4480 /*
4481 * it is a continued search
4482 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004483 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4485 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4486 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004487 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004489 /* line (probably) wrapped, set compl_startpos to the
4490 * first non_blank in the line, if it is not a wordchar
4491 * include it to get a better pattern, but then we don't
4492 * want the "\\<" prefix, check it bellow */
4493 compl_col = (colnr_T)(skipwhite(line) - line);
4494 compl_startpos.col = compl_col;
4495 compl_startpos.lnum = curwin->w_cursor.lnum;
4496 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 }
4498 else
4499 {
4500 /* S_IPOS was set when we inserted a word that was at the
4501 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004502 * mode but first we need to redefine compl_startpos */
4503 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004505 compl_cont_status |= CONT_SOL;
4506 compl_startpos.col = (colnr_T)(skipwhite(
4507 line + compl_length
4508 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004510 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004512 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004513 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 * have enough space? just being paranoic */
4515#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004516 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004518 compl_cont_status &= ~CONT_SOL;
4519 compl_length = (IOSIZE - MIN_SPACE);
4520 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004522 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4523 if (compl_length < 1)
4524 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 }
4526 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004527 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004529 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 }
4531 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004532 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004534 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004536 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004538 compl_cont_status = 0;
4539 compl_cont_status |= CONT_N_ADDS;
4540 compl_startpos = curwin->w_cursor;
4541 startcol = (int)curs_col;
4542 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 }
4544
4545 /* Work out completion pattern and original text -- webb */
4546 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4547 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004548 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4550 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004551 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004553 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004555 compl_col += ++startcol;
4556 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 }
4558 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004559 compl_pattern = str_foldcase(line + compl_col,
4560 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004562 compl_pattern = vim_strnsave(line + compl_col,
4563 compl_length);
4564 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 return FAIL;
4566 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004567 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 {
4569 char_u *prefix = (char_u *)"\\<";
4570
4571 /* we need 3 extra chars, 1 for the NUL and
4572 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004573 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4574 compl_length) + 3);
4575 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004577 if (!vim_iswordp(line + compl_col)
4578 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 && (
4580#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004581 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004583 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584#endif
4585 )))
4586 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004587 STRCPY((char *)compl_pattern, prefix);
4588 (void)quote_meta(compl_pattern + STRLEN(prefix),
4589 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004591 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004593 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004595 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596#endif
4597 )
4598 {
4599 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004600 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4601 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004603 compl_col += curs_col;
4604 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 }
4606 else
4607 {
4608#ifdef FEAT_MBYTE
4609 /* Search the point of change class of multibyte character
4610 * or not a word single byte character backward. */
4611 if (has_mbyte)
4612 {
4613 int base_class;
4614 int head_off;
4615
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004616 startcol -= (*mb_head_off)(line, line + startcol);
4617 base_class = mb_get_class(line + startcol);
4618 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004620 head_off = (*mb_head_off)(line, line + startcol);
4621 if (base_class != mb_get_class(line + startcol
4622 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004624 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 }
4626 }
4627 else
4628#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004629 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004631 compl_col += ++startcol;
4632 compl_length = (int)curs_col - startcol;
4633 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 {
4635 /* Only match word with at least two chars -- webb
4636 * there's no need to call quote_meta,
4637 * alloc(7) is enough -- Acevedo
4638 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004639 compl_pattern = alloc(7);
4640 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004642 STRCPY((char *)compl_pattern, "\\<");
4643 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4644 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 }
4646 else
4647 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004648 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4649 compl_length) + 3);
4650 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004652 STRCPY((char *)compl_pattern, "\\<");
4653 (void)quote_meta(compl_pattern + 2, line + compl_col,
4654 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 }
4656 }
4657 }
4658 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4659 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004660 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004661 compl_length = (int)curs_col - (int)compl_col;
4662 if (compl_length < 0) /* cursor in indent: empty pattern */
4663 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004665 compl_pattern = str_foldcase(line + compl_col, compl_length,
4666 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004668 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4669 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 return FAIL;
4671 }
4672 else if (ctrl_x_mode == CTRL_X_FILES)
4673 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004674 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004676 compl_col += ++startcol;
4677 compl_length = (int)curs_col - startcol;
4678 compl_pattern = addstar(line + compl_col, compl_length,
4679 EXPAND_FILES);
4680 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 return FAIL;
4682 }
4683 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4684 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004685 compl_pattern = vim_strnsave(line, curs_col);
4686 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004688 set_cmd_context(&compl_xp, compl_pattern,
4689 (int)STRLEN(compl_pattern), curs_col);
4690 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4691 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004692 {
4693 compl_col = curs_col;
4694 compl_length = 0;
4695 vim_free(compl_pattern);
4696 compl_pattern = NULL;
4697 }
4698 else
4699 {
4700 startcol = (int)(compl_xp.xp_pattern - compl_pattern);
4701 compl_col = startcol;
4702 compl_length = curs_col - startcol;
4703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004705 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004706 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00004707#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004708 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00004709 * Call user defined function 'completefunc' with "a:findstart"
4710 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004711 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00004712 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004713 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004714 char_u *funcname;
4715 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004716
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004717 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004718 * string */
4719 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4720 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4721 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004722 {
4723 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
4724 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004725 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004726 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004727
4728 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004729 args[1] = NULL;
4730 pos = curwin->w_cursor;
4731 col = call_func_retnr(funcname, 2, args, FALSE);
4732 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004733
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004734 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004735 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004736 compl_col = col;
4737 if ((colnr_T)compl_col > curs_col)
4738 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004739
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004740 /* Setup variables for completion. Need to obtain "line" again,
4741 * it may have become invalid. */
4742 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004743 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004744 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4745 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004746#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004747 return FAIL;
4748 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00004749 else if (ctrl_x_mode == CTRL_X_SPELL)
4750 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004751#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004752 if (spell_bad_len > 0)
4753 compl_col = curs_col - spell_bad_len;
4754 else
4755 compl_col = spell_word_start(startcol);
4756 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004757 {
4758 compl_length = 0;
4759 compl_col = curs_col;
4760 }
4761 else
4762 {
4763 spell_expand_check_cap(compl_col);
4764 compl_length = (int)curs_col - compl_col;
4765 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00004766 /* Need to obtain "line" again, it may have become invalid. */
4767 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004768 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4769 if (compl_pattern == NULL)
4770#endif
4771 return FAIL;
4772 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004773 else
4774 {
4775 EMSG2(_(e_intern2), "ins_complete()");
4776 return FAIL;
4777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004779 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 {
4781 edit_submode_pre = (char_u *)_(" Adding");
4782 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4783 {
4784 /* Insert a new line, keep indentation but ignore 'comments' */
4785#ifdef FEAT_COMMENTS
4786 char_u *old = curbuf->b_p_com;
4787
4788 curbuf->b_p_com = (char_u *)"";
4789#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004790 compl_startpos.lnum = curwin->w_cursor.lnum;
4791 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 ins_eol('\r');
4793#ifdef FEAT_COMMENTS
4794 curbuf->b_p_com = old;
4795#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004796 compl_length = 0;
4797 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 }
4799 }
4800 else
4801 {
4802 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004803 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 }
4805
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004806 if (compl_cont_status & CONT_LOCAL)
4807 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 else
4809 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4810
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004811 /* Always add completion for the original text. */
4812 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004813 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4814 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004815 -1, FALSE, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004817 vim_free(compl_pattern);
4818 compl_pattern = NULL;
4819 vim_free(compl_orig_text);
4820 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 return FAIL;
4822 }
4823
4824 /* showmode might reset the internal line pointers, so it must
4825 * be called before line = ml_get(), or when this address is no
4826 * longer needed. -- Acevedo.
4827 */
4828 edit_submode_extra = (char_u *)_("-- Searching...");
4829 edit_submode_highl = HLF_COUNT;
4830 showmode();
4831 edit_submode_extra = NULL;
4832 out_flush();
4833 }
4834
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004835 compl_shown_match = compl_curr_match;
4836 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837
4838 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004839 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004841 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004843 /* may undisplay the popup menu */
4844 ins_compl_upd_pum();
4845
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004846 if (n > 1) /* all matches have been found */
4847 compl_matches = n;
4848 compl_curr_match = compl_shown_match;
4849 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850
Bram Moolenaard68071d2006-05-02 22:08:30 +00004851 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
4852 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 if (got_int && !global_busy)
4854 {
4855 (void)vgetc();
4856 got_int = FALSE;
4857 }
4858
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004859 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004860 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004862 edit_submode_extra = (compl_cont_status & CONT_ADDING)
4863 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
4865 edit_submode_highl = HLF_E;
4866 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4867 * because we couldn't expand anything at first place, but if we used
4868 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4869 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004870 if ( compl_length > 1
4871 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 || (ctrl_x_mode != 0
4873 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
4874 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004875 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 }
4877
Bram Moolenaar572cb562005-08-05 21:35:02 +00004878 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004879 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004881 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882
4883 if (edit_submode_extra == NULL)
4884 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004885 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 {
4887 edit_submode_extra = (char_u *)_("Back at original");
4888 edit_submode_highl = HLF_W;
4889 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004890 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 {
4892 edit_submode_extra = (char_u *)_("Word from other line");
4893 edit_submode_highl = HLF_COUNT;
4894 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004895 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 {
4897 edit_submode_extra = (char_u *)_("The only match");
4898 edit_submode_highl = HLF_COUNT;
4899 }
4900 else
4901 {
4902 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004903 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004905 int number = 0;
4906 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004908 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 {
4910 /* search backwards for the first valid (!= -1) number.
4911 * This should normally succeed already at the first loop
4912 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004913 for (match = compl_curr_match->cp_prev; match != NULL
4914 && match != compl_first_match;
4915 match = match->cp_prev)
4916 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004918 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 break;
4920 }
4921 if (match != NULL)
4922 /* go up and assign all numbers which are not assigned
4923 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004924 for (match = match->cp_next;
4925 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004926 match = match->cp_next)
4927 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 }
4929 else /* BACKWARD */
4930 {
4931 /* search forwards (upwards) for the first valid (!= -1)
4932 * number. This should normally succeed already at the
4933 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004934 for (match = compl_curr_match->cp_next; match != NULL
4935 && match != compl_first_match;
4936 match = match->cp_next)
4937 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004939 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 break;
4941 }
4942 if (match != NULL)
4943 /* go down and assign all numbers which are not
4944 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004945 for (match = match->cp_prev; match
4946 && match->cp_number == -1;
4947 match = match->cp_prev)
4948 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 }
4950 }
4951
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004952 /* The match should always have a sequence number now, this is
4953 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004954 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 {
4956 /* Space for 10 text chars. + 2x10-digit no.s */
4957 static char_u match_ref[31];
4958
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004959 if (compl_matches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 sprintf((char *)IObuff, _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004961 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004963 sprintf((char *)IObuff, _("match %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004964 compl_curr_match->cp_number);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004965 vim_strncpy(match_ref, IObuff, 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 edit_submode_extra = match_ref;
4967 edit_submode_highl = HLF_R;
4968 if (dollar_vcol)
4969 curs_columns(FALSE);
4970 }
4971 }
4972 }
4973
4974 /* Show a message about what (completion) mode we're in. */
4975 showmode();
4976 if (edit_submode_extra != NULL)
4977 {
4978 if (!p_smd)
4979 msg_attr(edit_submode_extra,
4980 edit_submode_highl < HLF_COUNT
4981 ? hl_attr(edit_submode_highl) : 0);
4982 }
4983 else
4984 msg_clr_cmdline(); /* necessary for "noshowmode" */
4985
Bram Moolenaard68071d2006-05-02 22:08:30 +00004986 /* Show the popup menu, unless we got interrupted. */
4987 if (!compl_interrupted)
4988 {
4989 /* RedrawingDisabled may be set when invoked through complete(). */
4990 n = RedrawingDisabled;
4991 RedrawingDisabled = 0;
4992 ins_compl_show_pum();
4993 setcursor();
4994 RedrawingDisabled = n;
4995 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004996 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00004997 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004998
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 return OK;
5000}
5001
5002/*
5003 * Looks in the first "len" chars. of "src" for search-metachars.
5004 * If dest is not NULL the chars. are copied there quoting (with
5005 * a backslash) the metachars, and dest would be NUL terminated.
5006 * Returns the length (needed) of dest
5007 */
5008 static int
5009quote_meta(dest, src, len)
5010 char_u *dest;
5011 char_u *src;
5012 int len;
5013{
5014 int m;
5015
5016 for (m = len; --len >= 0; src++)
5017 {
5018 switch (*src)
5019 {
5020 case '.':
5021 case '*':
5022 case '[':
5023 if (ctrl_x_mode == CTRL_X_DICTIONARY
5024 || ctrl_x_mode == CTRL_X_THESAURUS)
5025 break;
5026 case '~':
5027 if (!p_magic) /* quote these only if magic is set */
5028 break;
5029 case '\\':
5030 if (ctrl_x_mode == CTRL_X_DICTIONARY
5031 || ctrl_x_mode == CTRL_X_THESAURUS)
5032 break;
5033 case '^': /* currently it's not needed. */
5034 case '$':
5035 m++;
5036 if (dest != NULL)
5037 *dest++ = '\\';
5038 break;
5039 }
5040 if (dest != NULL)
5041 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005042# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 /* Copy remaining bytes of a multibyte character. */
5044 if (has_mbyte)
5045 {
5046 int i, mb_len;
5047
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005048 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 if (mb_len > 0 && len >= mb_len)
5050 for (i = 0; i < mb_len; ++i)
5051 {
5052 --len;
5053 ++src;
5054 if (dest != NULL)
5055 *dest++ = *src;
5056 }
5057 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005058# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 }
5060 if (dest != NULL)
5061 *dest = NUL;
5062
5063 return m;
5064}
5065#endif /* FEAT_INS_EXPAND */
5066
5067/*
5068 * Next character is interpreted literally.
5069 * A one, two or three digit decimal number is interpreted as its byte value.
5070 * If one or two digits are entered, the next character is given to vungetc().
5071 * For Unicode a character > 255 may be returned.
5072 */
5073 int
5074get_literal()
5075{
5076 int cc;
5077 int nc;
5078 int i;
5079 int hex = FALSE;
5080 int octal = FALSE;
5081#ifdef FEAT_MBYTE
5082 int unicode = 0;
5083#endif
5084
5085 if (got_int)
5086 return Ctrl_C;
5087
5088#ifdef FEAT_GUI
5089 /*
5090 * In GUI there is no point inserting the internal code for a special key.
5091 * It is more useful to insert the string "<KEY>" instead. This would
5092 * probably be useful in a text window too, but it would not be
5093 * vi-compatible (maybe there should be an option for it?) -- webb
5094 */
5095 if (gui.in_use)
5096 ++allow_keys;
5097#endif
5098#ifdef USE_ON_FLY_SCROLL
5099 dont_scroll = TRUE; /* disallow scrolling here */
5100#endif
5101 ++no_mapping; /* don't map the next key hits */
5102 cc = 0;
5103 i = 0;
5104 for (;;)
5105 {
5106 do
5107 nc = safe_vgetc();
5108 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
5109 || nc == K_HOR_SCROLLBAR);
5110#ifdef FEAT_CMDL_INFO
5111 if (!(State & CMDLINE)
5112# ifdef FEAT_MBYTE
5113 && MB_BYTE2LEN_CHECK(nc) == 1
5114# endif
5115 )
5116 add_to_showcmd(nc);
5117#endif
5118 if (nc == 'x' || nc == 'X')
5119 hex = TRUE;
5120 else if (nc == 'o' || nc == 'O')
5121 octal = TRUE;
5122#ifdef FEAT_MBYTE
5123 else if (nc == 'u' || nc == 'U')
5124 unicode = nc;
5125#endif
5126 else
5127 {
5128 if (hex
5129#ifdef FEAT_MBYTE
5130 || unicode != 0
5131#endif
5132 )
5133 {
5134 if (!vim_isxdigit(nc))
5135 break;
5136 cc = cc * 16 + hex2nr(nc);
5137 }
5138 else if (octal)
5139 {
5140 if (nc < '0' || nc > '7')
5141 break;
5142 cc = cc * 8 + nc - '0';
5143 }
5144 else
5145 {
5146 if (!VIM_ISDIGIT(nc))
5147 break;
5148 cc = cc * 10 + nc - '0';
5149 }
5150
5151 ++i;
5152 }
5153
5154 if (cc > 255
5155#ifdef FEAT_MBYTE
5156 && unicode == 0
5157#endif
5158 )
5159 cc = 255; /* limit range to 0-255 */
5160 nc = 0;
5161
5162 if (hex) /* hex: up to two chars */
5163 {
5164 if (i >= 2)
5165 break;
5166 }
5167#ifdef FEAT_MBYTE
5168 else if (unicode) /* Unicode: up to four or eight chars */
5169 {
5170 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5171 break;
5172 }
5173#endif
5174 else if (i >= 3) /* decimal or octal: up to three chars */
5175 break;
5176 }
5177 if (i == 0) /* no number entered */
5178 {
5179 if (nc == K_ZERO) /* NUL is stored as NL */
5180 {
5181 cc = '\n';
5182 nc = 0;
5183 }
5184 else
5185 {
5186 cc = nc;
5187 nc = 0;
5188 }
5189 }
5190
5191 if (cc == 0) /* NUL is stored as NL */
5192 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005193#ifdef FEAT_MBYTE
5194 if (enc_dbcs && (cc & 0xff) == 0)
5195 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5196 second byte will cause trouble! */
5197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198
5199 --no_mapping;
5200#ifdef FEAT_GUI
5201 if (gui.in_use)
5202 --allow_keys;
5203#endif
5204 if (nc)
5205 vungetc(nc);
5206 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5207 return cc;
5208}
5209
5210/*
5211 * Insert character, taking care of special keys and mod_mask
5212 */
5213 static void
5214insert_special(c, allow_modmask, ctrlv)
5215 int c;
5216 int allow_modmask;
5217 int ctrlv; /* c was typed after CTRL-V */
5218{
5219 char_u *p;
5220 int len;
5221
5222 /*
5223 * Special function key, translate into "<Key>". Up to the last '>' is
5224 * inserted with ins_str(), so as not to replace characters in replace
5225 * mode.
5226 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5227 * unless 'allow_modmask' is TRUE.
5228 */
5229#ifdef MACOS
5230 /* Command-key never produces a normal key */
5231 if (mod_mask & MOD_MASK_CMD)
5232 allow_modmask = TRUE;
5233#endif
5234 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5235 {
5236 p = get_special_key_name(c, mod_mask);
5237 len = (int)STRLEN(p);
5238 c = p[len - 1];
5239 if (len > 2)
5240 {
5241 if (stop_arrow() == FAIL)
5242 return;
5243 p[len - 1] = NUL;
5244 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005245 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 ctrlv = FALSE;
5247 }
5248 }
5249 if (stop_arrow() == OK)
5250 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5251}
5252
5253/*
5254 * Special characters in this context are those that need processing other
5255 * than the simple insertion that can be performed here. This includes ESC
5256 * which terminates the insert, and CR/NL which need special processing to
5257 * open up a new line. This routine tries to optimize insertions performed by
5258 * the "redo", "undo" or "put" commands, so it needs to know when it should
5259 * stop and defer processing to the "normal" mechanism.
5260 * '0' and '^' are special, because they can be followed by CTRL-D.
5261 */
5262#ifdef EBCDIC
5263# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5264#else
5265# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5266#endif
5267
5268#ifdef FEAT_MBYTE
5269# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5270#else
5271# define WHITECHAR(cc) vim_iswhite(cc)
5272#endif
5273
5274 void
5275insertchar(c, flags, second_indent)
5276 int c; /* character to insert or NUL */
5277 int flags; /* INSCHAR_FORMAT, etc. */
5278 int second_indent; /* indent for second line if >= 0 */
5279{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 int textwidth;
5281#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285
5286 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5287 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288
5289 /*
5290 * Try to break the line in two or more pieces when:
5291 * - Always do this if we have been called to do formatting only.
5292 * - Always do this when 'formatoptions' has the 'a' flag and the line
5293 * ends in white space.
5294 * - Otherwise:
5295 * - Don't do this if inserting a blank
5296 * - Don't do this if an existing character is being replaced, unless
5297 * we're in VREPLACE mode.
5298 * - Do this if the cursor is not on the line where insert started
5299 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5300 * before the insert.
5301 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5302 * before 'textwidth'
5303 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005304 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 && ((flags & INSCHAR_FORMAT)
5306 || (!vim_iswhite(c)
5307 && !((State & REPLACE_FLAG)
5308#ifdef FEAT_VREPLACE
5309 && !(State & VREPLACE_FLAG)
5310#endif
5311 && *ml_get_cursor() != NUL)
5312 && (curwin->w_cursor.lnum != Insstart.lnum
5313 || ((!has_format_option(FO_INS_LONG)
5314 || Insstart_textlen <= (colnr_T)textwidth)
5315 && (!fo_ins_blank
5316 || Insstart_blank_vcol <= (colnr_T)textwidth
5317 ))))))
5318 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005319 /* Format with 'formatexpr' when it's set. Use internal formatting
5320 * when 'formatexpr' isn't set or it returns non-zero. */
5321#if defined(FEAT_EVAL)
5322 if (*curbuf->b_p_fex == NUL
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005323 || fex_format(curwin->w_cursor.lnum, 1L, c) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005325 internal_format(textwidth, second_indent, flags, c == NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005327
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 if (c == NUL) /* only formatting was wanted */
5329 return;
5330
5331#ifdef FEAT_COMMENTS
5332 /* Check whether this character should end a comment. */
5333 if (did_ai && (int)c == end_comment_pending)
5334 {
5335 char_u *line;
5336 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5337 int middle_len, end_len;
5338 int i;
5339
5340 /*
5341 * Need to remove existing (middle) comment leader and insert end
5342 * comment leader. First, check what comment leader we can find.
5343 */
5344 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5345 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5346 {
5347 /* Skip middle-comment string */
5348 while (*p && p[-1] != ':') /* find end of middle flags */
5349 ++p;
5350 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5351 /* Don't count trailing white space for middle_len */
5352 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5353 --middle_len;
5354
5355 /* Find the end-comment string */
5356 while (*p && p[-1] != ':') /* find end of end flags */
5357 ++p;
5358 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5359
5360 /* Skip white space before the cursor */
5361 i = curwin->w_cursor.col;
5362 while (--i >= 0 && vim_iswhite(line[i]))
5363 ;
5364 i++;
5365
5366 /* Skip to before the middle leader */
5367 i -= middle_len;
5368
5369 /* Check some expected things before we go on */
5370 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5371 {
5372 /* Backspace over all the stuff we want to replace */
5373 backspace_until_column(i);
5374
5375 /*
5376 * Insert the end-comment string, except for the last
5377 * character, which will get inserted as normal later.
5378 */
5379 ins_bytes_len(lead_end, end_len - 1);
5380 }
5381 }
5382 }
5383 end_comment_pending = NUL;
5384#endif
5385
5386 did_ai = FALSE;
5387#ifdef FEAT_SMARTINDENT
5388 did_si = FALSE;
5389 can_si = FALSE;
5390 can_si_back = FALSE;
5391#endif
5392
5393 /*
5394 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5395 * This speeds up normal text input considerably.
5396 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5397 * need to re-indent at a ':', or any other character (but not what
5398 * 'paste' is set)..
5399 */
5400#ifdef USE_ON_FLY_SCROLL
5401 dont_scroll = FALSE; /* allow scrolling here */
5402#endif
5403
5404 if ( !ISSPECIAL(c)
5405#ifdef FEAT_MBYTE
5406 && (!has_mbyte || (*mb_char2len)(c) == 1)
5407#endif
5408 && vpeekc() != NUL
5409 && !(State & REPLACE_FLAG)
5410#ifdef FEAT_CINDENT
5411 && !cindent_on()
5412#endif
5413#ifdef FEAT_RIGHTLEFT
5414 && !p_ri
5415#endif
5416 )
5417 {
5418#define INPUT_BUFLEN 100
5419 char_u buf[INPUT_BUFLEN + 1];
5420 int i;
5421 colnr_T virtcol = 0;
5422
5423 buf[0] = c;
5424 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005425 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 virtcol = get_nolist_virtcol();
5427 /*
5428 * Stop the string when:
5429 * - no more chars available
5430 * - finding a special character (command key)
5431 * - buffer is full
5432 * - running into the 'textwidth' boundary
5433 * - need to check for abbreviation: A non-word char after a word-char
5434 */
5435 while ( (c = vpeekc()) != NUL
5436 && !ISSPECIAL(c)
5437#ifdef FEAT_MBYTE
5438 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5439#endif
5440 && i < INPUT_BUFLEN
5441 && (textwidth == 0
5442 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5443 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5444 {
5445#ifdef FEAT_RIGHTLEFT
5446 c = vgetc();
5447 if (p_hkmap && KeyTyped)
5448 c = hkmap(c); /* Hebrew mode mapping */
5449# ifdef FEAT_FKMAP
5450 if (p_fkmap && KeyTyped)
5451 c = fkmap(c); /* Farsi mode mapping */
5452# endif
5453 buf[i++] = c;
5454#else
5455 buf[i++] = vgetc();
5456#endif
5457 }
5458
5459#ifdef FEAT_DIGRAPHS
5460 do_digraph(-1); /* clear digraphs */
5461 do_digraph(buf[i-1]); /* may be the start of a digraph */
5462#endif
5463 buf[i] = NUL;
5464 ins_str(buf);
5465 if (flags & INSCHAR_CTRLV)
5466 {
5467 redo_literal(*buf);
5468 i = 1;
5469 }
5470 else
5471 i = 0;
5472 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005473 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 }
5475 else
5476 {
5477#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005478 int cc;
5479
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5481 {
5482 char_u buf[MB_MAXBYTES + 1];
5483
5484 (*mb_char2bytes)(c, buf);
5485 buf[cc] = NUL;
5486 ins_char_bytes(buf, cc);
5487 AppendCharToRedobuff(c);
5488 }
5489 else
5490#endif
5491 {
5492 ins_char(c);
5493 if (flags & INSCHAR_CTRLV)
5494 redo_literal(c);
5495 else
5496 AppendCharToRedobuff(c);
5497 }
5498 }
5499}
5500
5501/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005502 * Format text at the current insert position.
5503 */
5504 static void
5505internal_format(textwidth, second_indent, flags, format_only)
5506 int textwidth;
5507 int second_indent;
5508 int flags;
5509 int format_only;
5510{
5511 int cc;
5512 int save_char = NUL;
5513 int haveto_redraw = FALSE;
5514 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5515#ifdef FEAT_MBYTE
5516 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5517#endif
5518 int fo_white_par = has_format_option(FO_WHITE_PAR);
5519 int first_line = TRUE;
5520#ifdef FEAT_COMMENTS
5521 colnr_T leader_len;
5522 int no_leader = FALSE;
5523 int do_comments = (flags & INSCHAR_DO_COM);
5524#endif
5525
5526 /*
5527 * When 'ai' is off we don't want a space under the cursor to be
5528 * deleted. Replace it with an 'x' temporarily.
5529 */
5530 if (!curbuf->b_p_ai)
5531 {
5532 cc = gchar_cursor();
5533 if (vim_iswhite(cc))
5534 {
5535 save_char = cc;
5536 pchar_cursor('x');
5537 }
5538 }
5539
5540 /*
5541 * Repeat breaking lines, until the current line is not too long.
5542 */
5543 while (!got_int)
5544 {
5545 int startcol; /* Cursor column at entry */
5546 int wantcol; /* column at textwidth border */
5547 int foundcol; /* column for start of spaces */
5548 int end_foundcol = 0; /* column for start of word */
5549 colnr_T len;
5550 colnr_T virtcol;
5551#ifdef FEAT_VREPLACE
5552 int orig_col = 0;
5553 char_u *saved_text = NULL;
5554#endif
5555 colnr_T col;
5556
5557 virtcol = get_nolist_virtcol();
5558 if (virtcol < (colnr_T)textwidth)
5559 break;
5560
5561#ifdef FEAT_COMMENTS
5562 if (no_leader)
5563 do_comments = FALSE;
5564 else if (!(flags & INSCHAR_FORMAT)
5565 && has_format_option(FO_WRAP_COMS))
5566 do_comments = TRUE;
5567
5568 /* Don't break until after the comment leader */
5569 if (do_comments)
5570 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5571 else
5572 leader_len = 0;
5573
5574 /* If the line doesn't start with a comment leader, then don't
5575 * start one in a following broken line. Avoids that a %word
5576 * moved to the start of the next line causes all following lines
5577 * to start with %. */
5578 if (leader_len == 0)
5579 no_leader = TRUE;
5580#endif
5581 if (!(flags & INSCHAR_FORMAT)
5582#ifdef FEAT_COMMENTS
5583 && leader_len == 0
5584#endif
5585 && !has_format_option(FO_WRAP))
5586
5587 {
5588 textwidth = 0;
5589 break;
5590 }
5591 if ((startcol = curwin->w_cursor.col) == 0)
5592 break;
5593
5594 /* find column of textwidth border */
5595 coladvance((colnr_T)textwidth);
5596 wantcol = curwin->w_cursor.col;
5597
5598 curwin->w_cursor.col = startcol - 1;
5599#ifdef FEAT_MBYTE
5600 /* Correct cursor for multi-byte character. */
5601 if (has_mbyte)
5602 mb_adjust_cursor();
5603#endif
5604 foundcol = 0;
5605
5606 /*
5607 * Find position to break at.
5608 * Stop at first entered white when 'formatoptions' has 'v'
5609 */
5610 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5611 || curwin->w_cursor.lnum != Insstart.lnum
5612 || curwin->w_cursor.col >= Insstart.col)
5613 {
5614 cc = gchar_cursor();
5615 if (WHITECHAR(cc))
5616 {
5617 /* remember position of blank just before text */
5618 end_foundcol = curwin->w_cursor.col;
5619
5620 /* find start of sequence of blanks */
5621 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5622 {
5623 dec_cursor();
5624 cc = gchar_cursor();
5625 }
5626 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5627 break; /* only spaces in front of text */
5628#ifdef FEAT_COMMENTS
5629 /* Don't break until after the comment leader */
5630 if (curwin->w_cursor.col < leader_len)
5631 break;
5632#endif
5633 if (has_format_option(FO_ONE_LETTER))
5634 {
5635 /* do not break after one-letter words */
5636 if (curwin->w_cursor.col == 0)
5637 break; /* one-letter word at begin */
5638
5639 col = curwin->w_cursor.col;
5640 dec_cursor();
5641 cc = gchar_cursor();
5642
5643 if (WHITECHAR(cc))
5644 continue; /* one-letter, continue */
5645 curwin->w_cursor.col = col;
5646 }
5647#ifdef FEAT_MBYTE
5648 if (has_mbyte)
5649 foundcol = curwin->w_cursor.col
5650 + (*mb_ptr2len)(ml_get_cursor());
5651 else
5652#endif
5653 foundcol = curwin->w_cursor.col + 1;
5654 if (curwin->w_cursor.col < (colnr_T)wantcol)
5655 break;
5656 }
5657#ifdef FEAT_MBYTE
5658 else if (cc >= 0x100 && fo_multibyte
5659 && curwin->w_cursor.col <= (colnr_T)wantcol)
5660 {
5661 /* Break after or before a multi-byte character. */
5662 foundcol = curwin->w_cursor.col;
5663 if (curwin->w_cursor.col < (colnr_T)wantcol)
5664 foundcol += (*mb_char2len)(cc);
5665 end_foundcol = foundcol;
5666 break;
5667 }
5668#endif
5669 if (curwin->w_cursor.col == 0)
5670 break;
5671 dec_cursor();
5672 }
5673
5674 if (foundcol == 0) /* no spaces, cannot break line */
5675 {
5676 curwin->w_cursor.col = startcol;
5677 break;
5678 }
5679
5680 /* Going to break the line, remove any "$" now. */
5681 undisplay_dollar();
5682
5683 /*
5684 * Offset between cursor position and line break is used by replace
5685 * stack functions. VREPLACE does not use this, and backspaces
5686 * over the text instead.
5687 */
5688#ifdef FEAT_VREPLACE
5689 if (State & VREPLACE_FLAG)
5690 orig_col = startcol; /* Will start backspacing from here */
5691 else
5692#endif
5693 replace_offset = startcol - end_foundcol - 1;
5694
5695 /*
5696 * adjust startcol for spaces that will be deleted and
5697 * characters that will remain on top line
5698 */
5699 curwin->w_cursor.col = foundcol;
5700 while (cc = gchar_cursor(), WHITECHAR(cc))
5701 inc_cursor();
5702 startcol -= curwin->w_cursor.col;
5703 if (startcol < 0)
5704 startcol = 0;
5705
5706#ifdef FEAT_VREPLACE
5707 if (State & VREPLACE_FLAG)
5708 {
5709 /*
5710 * In VREPLACE mode, we will backspace over the text to be
5711 * wrapped, so save a copy now to put on the next line.
5712 */
5713 saved_text = vim_strsave(ml_get_cursor());
5714 curwin->w_cursor.col = orig_col;
5715 if (saved_text == NULL)
5716 break; /* Can't do it, out of memory */
5717 saved_text[startcol] = NUL;
5718
5719 /* Backspace over characters that will move to the next line */
5720 if (!fo_white_par)
5721 backspace_until_column(foundcol);
5722 }
5723 else
5724#endif
5725 {
5726 /* put cursor after pos. to break line */
5727 if (!fo_white_par)
5728 curwin->w_cursor.col = foundcol;
5729 }
5730
5731 /*
5732 * Split the line just before the margin.
5733 * Only insert/delete lines, but don't really redraw the window.
5734 */
5735 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
5736 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
5737#ifdef FEAT_COMMENTS
5738 + (do_comments ? OPENLINE_DO_COM : 0)
5739#endif
5740 , old_indent);
5741 old_indent = 0;
5742
5743 replace_offset = 0;
5744 if (first_line)
5745 {
5746 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
5747 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
5748 if (second_indent >= 0)
5749 {
5750#ifdef FEAT_VREPLACE
5751 if (State & VREPLACE_FLAG)
5752 change_indent(INDENT_SET, second_indent, FALSE, NUL);
5753 else
5754#endif
5755 (void)set_indent(second_indent, SIN_CHANGED);
5756 }
5757 first_line = FALSE;
5758 }
5759
5760#ifdef FEAT_VREPLACE
5761 if (State & VREPLACE_FLAG)
5762 {
5763 /*
5764 * In VREPLACE mode we have backspaced over the text to be
5765 * moved, now we re-insert it into the new line.
5766 */
5767 ins_bytes(saved_text);
5768 vim_free(saved_text);
5769 }
5770 else
5771#endif
5772 {
5773 /*
5774 * Check if cursor is not past the NUL off the line, cindent
5775 * may have added or removed indent.
5776 */
5777 curwin->w_cursor.col += startcol;
5778 len = (colnr_T)STRLEN(ml_get_curline());
5779 if (curwin->w_cursor.col > len)
5780 curwin->w_cursor.col = len;
5781 }
5782
5783 haveto_redraw = TRUE;
5784#ifdef FEAT_CINDENT
5785 can_cindent = TRUE;
5786#endif
5787 /* moved the cursor, don't autoindent or cindent now */
5788 did_ai = FALSE;
5789#ifdef FEAT_SMARTINDENT
5790 did_si = FALSE;
5791 can_si = FALSE;
5792 can_si_back = FALSE;
5793#endif
5794 line_breakcheck();
5795 }
5796
5797 if (save_char != NUL) /* put back space after cursor */
5798 pchar_cursor(save_char);
5799
5800 if (!format_only && haveto_redraw)
5801 {
5802 update_topline();
5803 redraw_curbuf_later(VALID);
5804 }
5805}
5806
5807/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 * Called after inserting or deleting text: When 'formatoptions' includes the
5809 * 'a' flag format from the current line until the end of the paragraph.
5810 * Keep the cursor at the same position relative to the text.
5811 * The caller must have saved the cursor line for undo, following ones will be
5812 * saved here.
5813 */
5814 void
5815auto_format(trailblank, prev_line)
5816 int trailblank; /* when TRUE also format with trailing blank */
5817 int prev_line; /* may start in previous line */
5818{
5819 pos_T pos;
5820 colnr_T len;
5821 char_u *old;
5822 char_u *new, *pnew;
5823 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005824 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825
5826 if (!has_format_option(FO_AUTO))
5827 return;
5828
5829 pos = curwin->w_cursor;
5830 old = ml_get_curline();
5831
5832 /* may remove added space */
5833 check_auto_format(FALSE);
5834
5835 /* Don't format in Insert mode when the cursor is on a trailing blank, the
5836 * user might insert normal text next. Also skip formatting when "1" is
5837 * in 'formatoptions' and there is a single character before the cursor.
5838 * Otherwise the line would be broken and when typing another non-white
5839 * next they are not joined back together. */
5840 wasatend = (pos.col == STRLEN(old));
5841 if (*old != NUL && !trailblank && wasatend)
5842 {
5843 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005844 cc = gchar_cursor();
5845 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
5846 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005848 cc = gchar_cursor();
5849 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 {
5851 curwin->w_cursor = pos;
5852 return;
5853 }
5854 curwin->w_cursor = pos;
5855 }
5856
5857#ifdef FEAT_COMMENTS
5858 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
5859 * comments. */
5860 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
5861 && get_leader_len(old, NULL, FALSE) == 0)
5862 return;
5863#endif
5864
5865 /*
5866 * May start formatting in a previous line, so that after "x" a word is
5867 * moved to the previous line if it fits there now. Only when this is not
5868 * the start of a paragraph.
5869 */
5870 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
5871 {
5872 --curwin->w_cursor.lnum;
5873 if (u_save_cursor() == FAIL)
5874 return;
5875 }
5876
5877 /*
5878 * Do the formatting and restore the cursor position. "saved_cursor" will
5879 * be adjusted for the text formatting.
5880 */
5881 saved_cursor = pos;
5882 format_lines((linenr_T)-1);
5883 curwin->w_cursor = saved_cursor;
5884 saved_cursor.lnum = 0;
5885
5886 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5887 {
5888 /* "cannot happen" */
5889 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5890 coladvance((colnr_T)MAXCOL);
5891 }
5892 else
5893 check_cursor_col();
5894
5895 /* Insert mode: If the cursor is now after the end of the line while it
5896 * previously wasn't, the line was broken. Because of the rule above we
5897 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
5898 * formatted. */
5899 if (!wasatend && has_format_option(FO_WHITE_PAR))
5900 {
5901 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005902 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 if (curwin->w_cursor.col == len)
5904 {
5905 pnew = vim_strnsave(new, len + 2);
5906 pnew[len] = ' ';
5907 pnew[len + 1] = NUL;
5908 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
5909 /* remove the space later */
5910 did_add_space = TRUE;
5911 }
5912 else
5913 /* may remove added space */
5914 check_auto_format(FALSE);
5915 }
5916
5917 check_cursor();
5918}
5919
5920/*
5921 * When an extra space was added to continue a paragraph for auto-formatting,
5922 * delete it now. The space must be under the cursor, just after the insert
5923 * position.
5924 */
5925 static void
5926check_auto_format(end_insert)
5927 int end_insert; /* TRUE when ending Insert mode */
5928{
5929 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005930 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931
5932 if (did_add_space)
5933 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005934 cc = gchar_cursor();
5935 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 /* Somehow the space was removed already. */
5937 did_add_space = FALSE;
5938 else
5939 {
5940 if (!end_insert)
5941 {
5942 inc_cursor();
5943 c = gchar_cursor();
5944 dec_cursor();
5945 }
5946 if (c != NUL)
5947 {
5948 /* The space is no longer at the end of the line, delete it. */
5949 del_char(FALSE);
5950 did_add_space = FALSE;
5951 }
5952 }
5953 }
5954}
5955
5956/*
5957 * Find out textwidth to be used for formatting:
5958 * if 'textwidth' option is set, use it
5959 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
5960 * if invalid value, use 0.
5961 * Set default to window width (maximum 79) for "gq" operator.
5962 */
5963 int
5964comp_textwidth(ff)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005965 int ff; /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966{
5967 int textwidth;
5968
5969 textwidth = curbuf->b_p_tw;
5970 if (textwidth == 0 && curbuf->b_p_wm)
5971 {
5972 /* The width is the window width minus 'wrapmargin' minus all the
5973 * things that add to the margin. */
5974 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
5975#ifdef FEAT_CMDWIN
5976 if (cmdwin_type != 0)
5977 textwidth -= 1;
5978#endif
5979#ifdef FEAT_FOLDING
5980 textwidth -= curwin->w_p_fdc;
5981#endif
5982#ifdef FEAT_SIGNS
5983 if (curwin->w_buffer->b_signlist != NULL
5984# ifdef FEAT_NETBEANS_INTG
5985 || usingNetbeans
5986# endif
5987 )
5988 textwidth -= 1;
5989#endif
5990 if (curwin->w_p_nu)
5991 textwidth -= 8;
5992 }
5993 if (textwidth < 0)
5994 textwidth = 0;
5995 if (ff && textwidth == 0)
5996 {
5997 textwidth = W_WIDTH(curwin) - 1;
5998 if (textwidth > 79)
5999 textwidth = 79;
6000 }
6001 return textwidth;
6002}
6003
6004/*
6005 * Put a character in the redo buffer, for when just after a CTRL-V.
6006 */
6007 static void
6008redo_literal(c)
6009 int c;
6010{
6011 char_u buf[10];
6012
6013 /* Only digits need special treatment. Translate them into a string of
6014 * three digits. */
6015 if (VIM_ISDIGIT(c))
6016 {
6017 sprintf((char *)buf, "%03d", c);
6018 AppendToRedobuff(buf);
6019 }
6020 else
6021 AppendCharToRedobuff(c);
6022}
6023
6024/*
6025 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006026 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 */
6028 static void
6029start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006030 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031{
6032 if (!arrow_used) /* something has been inserted */
6033 {
6034 AppendToRedobuff(ESC_STR);
6035 stop_insert(end_insert_pos, FALSE);
6036 arrow_used = TRUE; /* this means we stopped the current insert */
6037 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006038#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006039 check_spell_redraw();
6040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041}
6042
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006043#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006044/*
6045 * If we skipped highlighting word at cursor, do it now.
6046 * It may be skipped again, thus reset spell_redraw_lnum first.
6047 */
6048 static void
6049check_spell_redraw()
6050{
6051 if (spell_redraw_lnum != 0)
6052 {
6053 linenr_T lnum = spell_redraw_lnum;
6054
6055 spell_redraw_lnum = 0;
6056 redrawWinline(lnum, FALSE);
6057 }
6058}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006059
6060/*
6061 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6062 * spelled word, if there is one.
6063 */
6064 static void
6065spell_back_to_badword()
6066{
6067 pos_T tpos = curwin->w_cursor;
6068
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006069 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006070 if (curwin->w_cursor.col != tpos.col)
6071 start_arrow(&tpos);
6072}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006073#endif
6074
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075/*
6076 * stop_arrow() is called before a change is made in insert mode.
6077 * If an arrow key has been used, start a new insertion.
6078 * Returns FAIL if undo is impossible, shouldn't insert then.
6079 */
6080 int
6081stop_arrow()
6082{
6083 if (arrow_used)
6084 {
6085 if (u_save_cursor() == OK)
6086 {
6087 arrow_used = FALSE;
6088 ins_need_undo = FALSE;
6089 }
6090 Insstart = curwin->w_cursor; /* new insertion starts here */
6091 Insstart_textlen = linetabsize(ml_get_curline());
6092 ai_col = 0;
6093#ifdef FEAT_VREPLACE
6094 if (State & VREPLACE_FLAG)
6095 {
6096 orig_line_count = curbuf->b_ml.ml_line_count;
6097 vr_lines_changed = 1;
6098 }
6099#endif
6100 ResetRedobuff();
6101 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006102 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 }
6104 else if (ins_need_undo)
6105 {
6106 if (u_save_cursor() == OK)
6107 ins_need_undo = FALSE;
6108 }
6109
6110#ifdef FEAT_FOLDING
6111 /* Always open fold at the cursor line when inserting something. */
6112 foldOpenCursor();
6113#endif
6114
6115 return (arrow_used || ins_need_undo ? FAIL : OK);
6116}
6117
6118/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006119 * Do a few things to stop inserting.
6120 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6121 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 */
6123 static void
6124stop_insert(end_insert_pos, esc)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006125 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006126 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006128 int cc;
6129 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130
6131 stop_redo_ins();
6132 replace_flush(); /* abandon replace stack */
6133
6134 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006135 * Save the inserted text for later redo with ^@ and CTRL-A.
6136 * Don't do it when "restart_edit" was set and nothing was inserted,
6137 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006139 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006140 if (did_restart_edit == 0 || (ptr != NULL
6141 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006142 {
6143 vim_free(last_insert);
6144 last_insert = ptr;
6145 last_insert_skip = new_insert_skip;
6146 }
6147 else
6148 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006150 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 {
6152 /* Auto-format now. It may seem strange to do this when stopping an
6153 * insertion (or moving the cursor), but it's required when appending
6154 * a line and having it end in a space. But only do it when something
6155 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006156 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006158 pos_T tpos = curwin->w_cursor;
6159
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 /* When the cursor is at the end of the line after a space the
6161 * formatting will move it to the following word. Avoid that by
6162 * moving the cursor onto the space. */
6163 cc = 'x';
6164 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6165 {
6166 dec_cursor();
6167 cc = gchar_cursor();
6168 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006169 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 }
6171
6172 auto_format(TRUE, FALSE);
6173
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006174 if (vim_iswhite(cc))
6175 {
6176 if (gchar_cursor() != NUL)
6177 inc_cursor();
6178#ifdef FEAT_VIRTUALEDIT
6179 /* If the cursor is still at the same character, also keep
6180 * the "coladd". */
6181 if (gchar_cursor() == NUL
6182 && curwin->w_cursor.lnum == tpos.lnum
6183 && curwin->w_cursor.col == tpos.col)
6184 curwin->w_cursor.coladd = tpos.coladd;
6185#endif
6186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 }
6188
6189 /* If a space was inserted for auto-formatting, remove it now. */
6190 check_auto_format(TRUE);
6191
6192 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006193 * of the line, and put the cursor back.
6194 * Do this when ESC was used or moving the cursor up/down. */
6195 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006196 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006198 pos_T tpos = curwin->w_cursor;
6199
6200 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar39f05632006-03-19 22:15:26 +00006201 for (;;)
6202 {
6203 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6204 --curwin->w_cursor.col;
6205 cc = gchar_cursor();
6206 if (!vim_iswhite(cc))
6207 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 (void)del_char(TRUE);
Bram Moolenaar39f05632006-03-19 22:15:26 +00006209 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006210 if (curwin->w_cursor.lnum != tpos.lnum)
6211 curwin->w_cursor = tpos;
6212 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6214
6215#ifdef FEAT_VISUAL
6216 /* <C-S-Right> may have started Visual mode, adjust the position for
6217 * deleted characters. */
6218 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
6219 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006220 cc = (int)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 if (VIsual.col > (colnr_T)cc)
6222 {
6223 VIsual.col = cc;
6224# ifdef FEAT_VIRTUALEDIT
6225 VIsual.coladd = 0;
6226# endif
6227 }
6228 }
6229#endif
6230 }
6231 }
6232 did_ai = FALSE;
6233#ifdef FEAT_SMARTINDENT
6234 did_si = FALSE;
6235 can_si = FALSE;
6236 can_si_back = FALSE;
6237#endif
6238
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006239 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6240 * now in a different buffer. */
6241 if (end_insert_pos != NULL)
6242 {
6243 curbuf->b_op_start = Insstart;
6244 curbuf->b_op_end = *end_insert_pos;
6245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246}
6247
6248/*
6249 * Set the last inserted text to a single character.
6250 * Used for the replace command.
6251 */
6252 void
6253set_last_insert(c)
6254 int c;
6255{
6256 char_u *s;
6257
6258 vim_free(last_insert);
6259#ifdef FEAT_MBYTE
6260 last_insert = alloc(MB_MAXBYTES * 3 + 5);
6261#else
6262 last_insert = alloc(6);
6263#endif
6264 if (last_insert != NULL)
6265 {
6266 s = last_insert;
6267 /* Use the CTRL-V only when entering a special char */
6268 if (c < ' ' || c == DEL)
6269 *s++ = Ctrl_V;
6270 s = add_char2buf(c, s);
6271 *s++ = ESC;
6272 *s++ = NUL;
6273 last_insert_skip = 0;
6274 }
6275}
6276
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006277#if defined(EXITFREE) || defined(PROTO)
6278 void
6279free_last_insert()
6280{
6281 vim_free(last_insert);
6282 last_insert = NULL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006283 vim_free(compl_orig_text);
6284 compl_orig_text = NULL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006285}
6286#endif
6287
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288/*
6289 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6290 * and CSI. Handle multi-byte characters.
6291 * Returns a pointer to after the added bytes.
6292 */
6293 char_u *
6294add_char2buf(c, s)
6295 int c;
6296 char_u *s;
6297{
6298#ifdef FEAT_MBYTE
6299 char_u temp[MB_MAXBYTES];
6300 int i;
6301 int len;
6302
6303 len = (*mb_char2bytes)(c, temp);
6304 for (i = 0; i < len; ++i)
6305 {
6306 c = temp[i];
6307#endif
6308 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6309 if (c == K_SPECIAL)
6310 {
6311 *s++ = K_SPECIAL;
6312 *s++ = KS_SPECIAL;
6313 *s++ = KE_FILLER;
6314 }
6315#ifdef FEAT_GUI
6316 else if (c == CSI)
6317 {
6318 *s++ = CSI;
6319 *s++ = KS_EXTRA;
6320 *s++ = (int)KE_CSI;
6321 }
6322#endif
6323 else
6324 *s++ = c;
6325#ifdef FEAT_MBYTE
6326 }
6327#endif
6328 return s;
6329}
6330
6331/*
6332 * move cursor to start of line
6333 * if flags & BL_WHITE move to first non-white
6334 * if flags & BL_SOL move to first non-white if startofline is set,
6335 * otherwise keep "curswant" column
6336 * if flags & BL_FIX don't leave the cursor on a NUL.
6337 */
6338 void
6339beginline(flags)
6340 int flags;
6341{
6342 if ((flags & BL_SOL) && !p_sol)
6343 coladvance(curwin->w_curswant);
6344 else
6345 {
6346 curwin->w_cursor.col = 0;
6347#ifdef FEAT_VIRTUALEDIT
6348 curwin->w_cursor.coladd = 0;
6349#endif
6350
6351 if (flags & (BL_WHITE | BL_SOL))
6352 {
6353 char_u *ptr;
6354
6355 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6356 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6357 ++curwin->w_cursor.col;
6358 }
6359 curwin->w_set_curswant = TRUE;
6360 }
6361}
6362
6363/*
6364 * oneright oneleft cursor_down cursor_up
6365 *
6366 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006367 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 * Return OK when successful, FAIL when we hit a line of file boundary.
6369 */
6370
6371 int
6372oneright()
6373{
6374 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376
6377#ifdef FEAT_VIRTUALEDIT
6378 if (virtual_active())
6379 {
6380 pos_T prevpos = curwin->w_cursor;
6381
6382 /* Adjust for multi-wide char (excluding TAB) */
6383 ptr = ml_get_cursor();
6384 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006385# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006387# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006389# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 ))
6391 ? ptr2cells(ptr) : 1));
6392 curwin->w_set_curswant = TRUE;
6393 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6394 return (prevpos.col != curwin->w_cursor.col
6395 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
6396 }
6397#endif
6398
6399 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006400 if (*ptr == NUL)
6401 return FAIL; /* already at the very end */
6402
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006404 if (has_mbyte)
6405 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 else
6407#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006408 l = 1;
6409
6410 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6411 * contains "onemore". */
6412 if (ptr[l] == NUL
6413#ifdef FEAT_VIRTUALEDIT
6414 && (ve_flags & VE_ONEMORE) == 0
6415#endif
6416 )
6417 return FAIL;
6418 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419
6420 curwin->w_set_curswant = TRUE;
6421 return OK;
6422}
6423
6424 int
6425oneleft()
6426{
6427#ifdef FEAT_VIRTUALEDIT
6428 if (virtual_active())
6429 {
6430 int width;
6431 int v = getviscol();
6432
6433 if (v == 0)
6434 return FAIL;
6435
6436# ifdef FEAT_LINEBREAK
6437 /* We might get stuck on 'showbreak', skip over it. */
6438 width = 1;
6439 for (;;)
6440 {
6441 coladvance(v - width);
6442 /* getviscol() is slow, skip it when 'showbreak' is empty and
6443 * there are no multi-byte characters */
6444 if ((*p_sbr == NUL
6445# ifdef FEAT_MBYTE
6446 && !has_mbyte
6447# endif
6448 ) || getviscol() < v)
6449 break;
6450 ++width;
6451 }
6452# else
6453 coladvance(v - 1);
6454# endif
6455
6456 if (curwin->w_cursor.coladd == 1)
6457 {
6458 char_u *ptr;
6459
6460 /* Adjust for multi-wide char (not a TAB) */
6461 ptr = ml_get_cursor();
6462 if (*ptr != TAB && vim_isprintc(
6463# ifdef FEAT_MBYTE
6464 (*mb_ptr2char)(ptr)
6465# else
6466 *ptr
6467# endif
6468 ) && ptr2cells(ptr) > 1)
6469 curwin->w_cursor.coladd = 0;
6470 }
6471
6472 curwin->w_set_curswant = TRUE;
6473 return OK;
6474 }
6475#endif
6476
6477 if (curwin->w_cursor.col == 0)
6478 return FAIL;
6479
6480 curwin->w_set_curswant = TRUE;
6481 --curwin->w_cursor.col;
6482
6483#ifdef FEAT_MBYTE
6484 /* if the character on the left of the current cursor is a multi-byte
6485 * character, move to its first byte */
6486 if (has_mbyte)
6487 mb_adjust_cursor();
6488#endif
6489 return OK;
6490}
6491
6492 int
6493cursor_up(n, upd_topline)
6494 long n;
6495 int upd_topline; /* When TRUE: update topline */
6496{
6497 linenr_T lnum;
6498
6499 if (n > 0)
6500 {
6501 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006502 /* This fails if the cursor is already in the first line or the count
6503 * is larger than the line number and '-' is in 'cpoptions' */
6504 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 return FAIL;
6506 if (n >= lnum)
6507 lnum = 1;
6508 else
6509#ifdef FEAT_FOLDING
6510 if (hasAnyFolding(curwin))
6511 {
6512 /*
6513 * Count each sequence of folded lines as one logical line.
6514 */
6515 /* go to the the start of the current fold */
6516 (void)hasFolding(lnum, &lnum, NULL);
6517
6518 while (n--)
6519 {
6520 /* move up one line */
6521 --lnum;
6522 if (lnum <= 1)
6523 break;
6524 /* If we entered a fold, move to the beginning, unless in
6525 * Insert mode or when 'foldopen' contains "all": it will open
6526 * in a moment. */
6527 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6528 (void)hasFolding(lnum, &lnum, NULL);
6529 }
6530 if (lnum < 1)
6531 lnum = 1;
6532 }
6533 else
6534#endif
6535 lnum -= n;
6536 curwin->w_cursor.lnum = lnum;
6537 }
6538
6539 /* try to advance to the column we want to be at */
6540 coladvance(curwin->w_curswant);
6541
6542 if (upd_topline)
6543 update_topline(); /* make sure curwin->w_topline is valid */
6544
6545 return OK;
6546}
6547
6548/*
6549 * Cursor down a number of logical lines.
6550 */
6551 int
6552cursor_down(n, upd_topline)
6553 long n;
6554 int upd_topline; /* When TRUE: update topline */
6555{
6556 linenr_T lnum;
6557
6558 if (n > 0)
6559 {
6560 lnum = curwin->w_cursor.lnum;
6561#ifdef FEAT_FOLDING
6562 /* Move to last line of fold, will fail if it's the end-of-file. */
6563 (void)hasFolding(lnum, NULL, &lnum);
6564#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006565 /* This fails if the cursor is already in the last line or would move
6566 * beyound the last line and '-' is in 'cpoptions' */
6567 if (lnum >= curbuf->b_ml.ml_line_count
6568 || (lnum + n > curbuf->b_ml.ml_line_count
6569 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 return FAIL;
6571 if (lnum + n >= curbuf->b_ml.ml_line_count)
6572 lnum = curbuf->b_ml.ml_line_count;
6573 else
6574#ifdef FEAT_FOLDING
6575 if (hasAnyFolding(curwin))
6576 {
6577 linenr_T last;
6578
6579 /* count each sequence of folded lines as one logical line */
6580 while (n--)
6581 {
6582 if (hasFolding(lnum, NULL, &last))
6583 lnum = last + 1;
6584 else
6585 ++lnum;
6586 if (lnum >= curbuf->b_ml.ml_line_count)
6587 break;
6588 }
6589 if (lnum > curbuf->b_ml.ml_line_count)
6590 lnum = curbuf->b_ml.ml_line_count;
6591 }
6592 else
6593#endif
6594 lnum += n;
6595 curwin->w_cursor.lnum = lnum;
6596 }
6597
6598 /* try to advance to the column we want to be at */
6599 coladvance(curwin->w_curswant);
6600
6601 if (upd_topline)
6602 update_topline(); /* make sure curwin->w_topline is valid */
6603
6604 return OK;
6605}
6606
6607/*
6608 * Stuff the last inserted text in the read buffer.
6609 * Last_insert actually is a copy of the redo buffer, so we
6610 * first have to remove the command.
6611 */
6612 int
6613stuff_inserted(c, count, no_esc)
6614 int c; /* Command character to be inserted */
6615 long count; /* Repeat this many times */
6616 int no_esc; /* Don't add an ESC at the end */
6617{
6618 char_u *esc_ptr;
6619 char_u *ptr;
6620 char_u *last_ptr;
6621 char_u last = NUL;
6622
6623 ptr = get_last_insert();
6624 if (ptr == NULL)
6625 {
6626 EMSG(_(e_noinstext));
6627 return FAIL;
6628 }
6629
6630 /* may want to stuff the command character, to start Insert mode */
6631 if (c != NUL)
6632 stuffcharReadbuff(c);
6633 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
6634 *esc_ptr = NUL; /* remove the ESC */
6635
6636 /* when the last char is either "0" or "^" it will be quoted if no ESC
6637 * comes after it OR if it will inserted more than once and "ptr"
6638 * starts with ^D. -- Acevedo
6639 */
6640 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
6641 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
6642 && (no_esc || (*ptr == Ctrl_D && count > 1)))
6643 {
6644 last = *last_ptr;
6645 *last_ptr = NUL;
6646 }
6647
6648 do
6649 {
6650 stuffReadbuff(ptr);
6651 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6652 if (last)
6653 stuffReadbuff((char_u *)(last == '0'
6654 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
6655 : IF_EB("\026^", CTRL_V_STR "^")));
6656 }
6657 while (--count > 0);
6658
6659 if (last)
6660 *last_ptr = last;
6661
6662 if (esc_ptr != NULL)
6663 *esc_ptr = ESC; /* put the ESC back */
6664
6665 /* may want to stuff a trailing ESC, to get out of Insert mode */
6666 if (!no_esc)
6667 stuffcharReadbuff(ESC);
6668
6669 return OK;
6670}
6671
6672 char_u *
6673get_last_insert()
6674{
6675 if (last_insert == NULL)
6676 return NULL;
6677 return last_insert + last_insert_skip;
6678}
6679
6680/*
6681 * Get last inserted string, and remove trailing <Esc>.
6682 * Returns pointer to allocated memory (must be freed) or NULL.
6683 */
6684 char_u *
6685get_last_insert_save()
6686{
6687 char_u *s;
6688 int len;
6689
6690 if (last_insert == NULL)
6691 return NULL;
6692 s = vim_strsave(last_insert + last_insert_skip);
6693 if (s != NULL)
6694 {
6695 len = (int)STRLEN(s);
6696 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
6697 s[len - 1] = NUL;
6698 }
6699 return s;
6700}
6701
6702/*
6703 * Check the word in front of the cursor for an abbreviation.
6704 * Called when the non-id character "c" has been entered.
6705 * When an abbreviation is recognized it is removed from the text and
6706 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
6707 */
6708 static int
6709echeck_abbr(c)
6710 int c;
6711{
6712 /* Don't check for abbreviation in paste mode, when disabled and just
6713 * after moving around with cursor keys. */
6714 if (p_paste || no_abbr || arrow_used)
6715 return FALSE;
6716
6717 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
6718 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
6719}
6720
6721/*
6722 * replace-stack functions
6723 *
6724 * When replacing characters, the replaced characters are remembered for each
6725 * new character. This is used to re-insert the old text when backspacing.
6726 *
6727 * There is a NUL headed list of characters for each character that is
6728 * currently in the file after the insertion point. When BS is used, one NUL
6729 * headed list is put back for the deleted character.
6730 *
6731 * For a newline, there are two NUL headed lists. One contains the characters
6732 * that the NL replaced. The extra one stores the characters after the cursor
6733 * that were deleted (always white space).
6734 *
6735 * Replace_offset is normally 0, in which case replace_push will add a new
6736 * character at the end of the stack. If replace_offset is not 0, that many
6737 * characters will be left on the stack above the newly inserted character.
6738 */
6739
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00006740static char_u *replace_stack = NULL;
6741static long replace_stack_nr = 0; /* next entry in replace stack */
6742static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743
6744 void
6745replace_push(c)
6746 int c; /* character that is replaced (NUL is none) */
6747{
6748 char_u *p;
6749
6750 if (replace_stack_nr < replace_offset) /* nothing to do */
6751 return;
6752 if (replace_stack_len <= replace_stack_nr)
6753 {
6754 replace_stack_len += 50;
6755 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
6756 if (p == NULL) /* out of memory */
6757 {
6758 replace_stack_len -= 50;
6759 return;
6760 }
6761 if (replace_stack != NULL)
6762 {
6763 mch_memmove(p, replace_stack,
6764 (size_t)(replace_stack_nr * sizeof(char_u)));
6765 vim_free(replace_stack);
6766 }
6767 replace_stack = p;
6768 }
6769 p = replace_stack + replace_stack_nr - replace_offset;
6770 if (replace_offset)
6771 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
6772 *p = c;
6773 ++replace_stack_nr;
6774}
6775
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006776#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777/*
6778 * call replace_push(c) with replace_offset set to the first NUL.
6779 */
6780 static void
6781replace_push_off(c)
6782 int c;
6783{
6784 char_u *p;
6785
6786 p = replace_stack + replace_stack_nr;
6787 for (replace_offset = 1; replace_offset < replace_stack_nr;
6788 ++replace_offset)
6789 if (*--p == NUL)
6790 break;
6791 replace_push(c);
6792 replace_offset = 0;
6793}
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795
6796/*
6797 * Pop one item from the replace stack.
6798 * return -1 if stack empty
6799 * return replaced character or NUL otherwise
6800 */
6801 static int
6802replace_pop()
6803{
6804 if (replace_stack_nr == 0)
6805 return -1;
6806 return (int)replace_stack[--replace_stack_nr];
6807}
6808
6809/*
6810 * Join the top two items on the replace stack. This removes to "off"'th NUL
6811 * encountered.
6812 */
6813 static void
6814replace_join(off)
6815 int off; /* offset for which NUL to remove */
6816{
6817 int i;
6818
6819 for (i = replace_stack_nr; --i >= 0; )
6820 if (replace_stack[i] == NUL && off-- <= 0)
6821 {
6822 --replace_stack_nr;
6823 mch_memmove(replace_stack + i, replace_stack + i + 1,
6824 (size_t)(replace_stack_nr - i));
6825 return;
6826 }
6827}
6828
6829/*
6830 * Pop bytes from the replace stack until a NUL is found, and insert them
6831 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
6832 */
6833 static void
6834replace_pop_ins()
6835{
6836 int cc;
6837 int oldState = State;
6838
6839 State = NORMAL; /* don't want REPLACE here */
6840 while ((cc = replace_pop()) > 0)
6841 {
6842#ifdef FEAT_MBYTE
6843 mb_replace_pop_ins(cc);
6844#else
6845 ins_char(cc);
6846#endif
6847 dec_cursor();
6848 }
6849 State = oldState;
6850}
6851
6852#ifdef FEAT_MBYTE
6853/*
6854 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
6855 * indicates a multi-byte char, pop the other bytes too.
6856 */
6857 static void
6858mb_replace_pop_ins(cc)
6859 int cc;
6860{
6861 int n;
6862 char_u buf[MB_MAXBYTES];
6863 int i;
6864 int c;
6865
6866 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
6867 {
6868 buf[0] = cc;
6869 for (i = 1; i < n; ++i)
6870 buf[i] = replace_pop();
6871 ins_bytes_len(buf, n);
6872 }
6873 else
6874 ins_char(cc);
6875
6876 if (enc_utf8)
6877 /* Handle composing chars. */
6878 for (;;)
6879 {
6880 c = replace_pop();
6881 if (c == -1) /* stack empty */
6882 break;
6883 if ((n = MB_BYTE2LEN(c)) == 1)
6884 {
6885 /* Not a multi-byte char, put it back. */
6886 replace_push(c);
6887 break;
6888 }
6889 else
6890 {
6891 buf[0] = c;
6892 for (i = 1; i < n; ++i)
6893 buf[i] = replace_pop();
6894 if (utf_iscomposing(utf_ptr2char(buf)))
6895 ins_bytes_len(buf, n);
6896 else
6897 {
6898 /* Not a composing char, put it back. */
6899 for (i = n - 1; i >= 0; --i)
6900 replace_push(buf[i]);
6901 break;
6902 }
6903 }
6904 }
6905}
6906#endif
6907
6908/*
6909 * make the replace stack empty
6910 * (called when exiting replace mode)
6911 */
6912 static void
6913replace_flush()
6914{
6915 vim_free(replace_stack);
6916 replace_stack = NULL;
6917 replace_stack_len = 0;
6918 replace_stack_nr = 0;
6919}
6920
6921/*
6922 * Handle doing a BS for one character.
6923 * cc < 0: replace stack empty, just move cursor
6924 * cc == 0: character was inserted, delete it
6925 * cc > 0: character was replaced, put cc (first byte of original char) back
6926 * and check for more characters to be put back
6927 */
6928 static void
6929replace_do_bs()
6930{
6931 int cc;
6932#ifdef FEAT_VREPLACE
6933 int orig_len = 0;
6934 int ins_len;
6935 int orig_vcols = 0;
6936 colnr_T start_vcol;
6937 char_u *p;
6938 int i;
6939 int vcol;
6940#endif
6941
6942 cc = replace_pop();
6943 if (cc > 0)
6944 {
6945#ifdef FEAT_VREPLACE
6946 if (State & VREPLACE_FLAG)
6947 {
6948 /* Get the number of screen cells used by the character we are
6949 * going to delete. */
6950 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
6951 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
6952 }
6953#endif
6954#ifdef FEAT_MBYTE
6955 if (has_mbyte)
6956 {
6957 del_char(FALSE);
6958# ifdef FEAT_VREPLACE
6959 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006960 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961# endif
6962 replace_push(cc);
6963 }
6964 else
6965#endif
6966 {
6967 pchar_cursor(cc);
6968#ifdef FEAT_VREPLACE
6969 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006970 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971#endif
6972 }
6973 replace_pop_ins();
6974
6975#ifdef FEAT_VREPLACE
6976 if (State & VREPLACE_FLAG)
6977 {
6978 /* Get the number of screen cells used by the inserted characters */
6979 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006980 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 vcol = start_vcol;
6982 for (i = 0; i < ins_len; ++i)
6983 {
6984 vcol += chartabsize(p + i, vcol);
6985#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006986 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987#endif
6988 }
6989 vcol -= start_vcol;
6990
6991 /* Delete spaces that were inserted after the cursor to keep the
6992 * text aligned. */
6993 curwin->w_cursor.col += ins_len;
6994 while (vcol > orig_vcols && gchar_cursor() == ' ')
6995 {
6996 del_char(FALSE);
6997 ++orig_vcols;
6998 }
6999 curwin->w_cursor.col -= ins_len;
7000 }
7001#endif
7002
7003 /* mark the buffer as changed and prepare for displaying */
7004 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7005 }
7006 else if (cc == 0)
7007 (void)del_char(FALSE);
7008}
7009
7010#ifdef FEAT_CINDENT
7011/*
7012 * Return TRUE if C-indenting is on.
7013 */
7014 static int
7015cindent_on()
7016{
7017 return (!p_paste && (curbuf->b_p_cin
7018# ifdef FEAT_EVAL
7019 || *curbuf->b_p_inde != NUL
7020# endif
7021 ));
7022}
7023#endif
7024
7025#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7026/*
7027 * Re-indent the current line, based on the current contents of it and the
7028 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7029 * confused what all the part that handles Control-T is doing that I'm not.
7030 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7031 */
7032
7033 void
7034fixthisline(get_the_indent)
7035 int (*get_the_indent) __ARGS((void));
7036{
7037 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
7038 if (linewhite(curwin->w_cursor.lnum))
7039 did_ai = TRUE; /* delete the indent if the line stays empty */
7040}
7041
7042 void
7043fix_indent()
7044{
7045 if (p_paste)
7046 return;
7047# ifdef FEAT_LISP
7048 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7049 fixthisline(get_lisp_indent);
7050# endif
7051# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7052 else
7053# endif
7054# ifdef FEAT_CINDENT
7055 if (cindent_on())
7056 do_c_expr_indent();
7057# endif
7058}
7059
7060#endif
7061
7062#ifdef FEAT_CINDENT
7063/*
7064 * return TRUE if 'cinkeys' contains the key "keytyped",
7065 * when == '*': Only if key is preceded with '*' (indent before insert)
7066 * when == '!': Only if key is prededed with '!' (don't insert)
7067 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7068 *
7069 * "keytyped" can have a few special values:
7070 * KEY_OPEN_FORW
7071 * KEY_OPEN_BACK
7072 * KEY_COMPLETE just finished completion.
7073 *
7074 * If line_is_empty is TRUE accept keys with '0' before them.
7075 */
7076 int
7077in_cinkeys(keytyped, when, line_is_empty)
7078 int keytyped;
7079 int when;
7080 int line_is_empty;
7081{
7082 char_u *look;
7083 int try_match;
7084 int try_match_word;
7085 char_u *p;
7086 char_u *line;
7087 int icase;
7088 int i;
7089
7090#ifdef FEAT_EVAL
7091 if (*curbuf->b_p_inde != NUL)
7092 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7093 else
7094#endif
7095 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7096 while (*look)
7097 {
7098 /*
7099 * Find out if we want to try a match with this key, depending on
7100 * 'when' and a '*' or '!' before the key.
7101 */
7102 switch (when)
7103 {
7104 case '*': try_match = (*look == '*'); break;
7105 case '!': try_match = (*look == '!'); break;
7106 default: try_match = (*look != '*'); break;
7107 }
7108 if (*look == '*' || *look == '!')
7109 ++look;
7110
7111 /*
7112 * If there is a '0', only accept a match if the line is empty.
7113 * But may still match when typing last char of a word.
7114 */
7115 if (*look == '0')
7116 {
7117 try_match_word = try_match;
7118 if (!line_is_empty)
7119 try_match = FALSE;
7120 ++look;
7121 }
7122 else
7123 try_match_word = FALSE;
7124
7125 /*
7126 * does it look like a control character?
7127 */
7128 if (*look == '^'
7129#ifdef EBCDIC
7130 && (Ctrl_chr(look[1]) != 0)
7131#else
7132 && look[1] >= '?' && look[1] <= '_'
7133#endif
7134 )
7135 {
7136 if (try_match && keytyped == Ctrl_chr(look[1]))
7137 return TRUE;
7138 look += 2;
7139 }
7140 /*
7141 * 'o' means "o" command, open forward.
7142 * 'O' means "O" command, open backward.
7143 */
7144 else if (*look == 'o')
7145 {
7146 if (try_match && keytyped == KEY_OPEN_FORW)
7147 return TRUE;
7148 ++look;
7149 }
7150 else if (*look == 'O')
7151 {
7152 if (try_match && keytyped == KEY_OPEN_BACK)
7153 return TRUE;
7154 ++look;
7155 }
7156
7157 /*
7158 * 'e' means to check for "else" at start of line and just before the
7159 * cursor.
7160 */
7161 else if (*look == 'e')
7162 {
7163 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7164 {
7165 p = ml_get_curline();
7166 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7167 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7168 return TRUE;
7169 }
7170 ++look;
7171 }
7172
7173 /*
7174 * ':' only causes an indent if it is at the end of a label or case
7175 * statement, or when it was before typing the ':' (to fix
7176 * class::method for C++).
7177 */
7178 else if (*look == ':')
7179 {
7180 if (try_match && keytyped == ':')
7181 {
7182 p = ml_get_curline();
7183 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
7184 return TRUE;
7185 if (curwin->w_cursor.col > 2
7186 && p[curwin->w_cursor.col - 1] == ':'
7187 && p[curwin->w_cursor.col - 2] == ':')
7188 {
7189 p[curwin->w_cursor.col - 1] = ' ';
7190 i = (cin_iscase(p) || cin_isscopedecl(p)
7191 || cin_islabel(30));
7192 p = ml_get_curline();
7193 p[curwin->w_cursor.col - 1] = ':';
7194 if (i)
7195 return TRUE;
7196 }
7197 }
7198 ++look;
7199 }
7200
7201
7202 /*
7203 * Is it a key in <>, maybe?
7204 */
7205 else if (*look == '<')
7206 {
7207 if (try_match)
7208 {
7209 /*
7210 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7211 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7212 * >, *, : and ! keys if they really really want to.
7213 */
7214 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
7215 && keytyped == look[1])
7216 return TRUE;
7217
7218 if (keytyped == get_special_key_code(look + 1))
7219 return TRUE;
7220 }
7221 while (*look && *look != '>')
7222 look++;
7223 while (*look == '>')
7224 look++;
7225 }
7226
7227 /*
7228 * Is it a word: "=word"?
7229 */
7230 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
7231 {
7232 ++look;
7233 if (*look == '~')
7234 {
7235 icase = TRUE;
7236 ++look;
7237 }
7238 else
7239 icase = FALSE;
7240 p = vim_strchr(look, ',');
7241 if (p == NULL)
7242 p = look + STRLEN(look);
7243 if ((try_match || try_match_word)
7244 && curwin->w_cursor.col >= (colnr_T)(p - look))
7245 {
7246 int match = FALSE;
7247
7248#ifdef FEAT_INS_EXPAND
7249 if (keytyped == KEY_COMPLETE)
7250 {
7251 char_u *s;
7252
7253 /* Just completed a word, check if it starts with "look".
7254 * search back for the start of a word. */
7255 line = ml_get_curline();
7256# ifdef FEAT_MBYTE
7257 if (has_mbyte)
7258 {
7259 char_u *n;
7260
7261 for (s = line + curwin->w_cursor.col; s > line; s = n)
7262 {
7263 n = mb_prevptr(line, s);
7264 if (!vim_iswordp(n))
7265 break;
7266 }
7267 }
7268 else
7269# endif
7270 for (s = line + curwin->w_cursor.col; s > line; --s)
7271 if (!vim_iswordc(s[-1]))
7272 break;
7273 if (s + (p - look) <= line + curwin->w_cursor.col
7274 && (icase
7275 ? MB_STRNICMP(s, look, p - look)
7276 : STRNCMP(s, look, p - look)) == 0)
7277 match = TRUE;
7278 }
7279 else
7280#endif
7281 /* TODO: multi-byte */
7282 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7283 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7284 {
7285 line = ml_get_cursor();
7286 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7287 || !vim_iswordc(line[-(p - look) - 1]))
7288 && (icase
7289 ? MB_STRNICMP(line - (p - look), look, p - look)
7290 : STRNCMP(line - (p - look), look, p - look))
7291 == 0)
7292 match = TRUE;
7293 }
7294 if (match && try_match_word && !try_match)
7295 {
7296 /* "0=word": Check if there are only blanks before the
7297 * word. */
7298 line = ml_get_curline();
7299 if ((int)(skipwhite(line) - line) !=
7300 (int)(curwin->w_cursor.col - (p - look)))
7301 match = FALSE;
7302 }
7303 if (match)
7304 return TRUE;
7305 }
7306 look = p;
7307 }
7308
7309 /*
7310 * ok, it's a boring generic character.
7311 */
7312 else
7313 {
7314 if (try_match && *look == keytyped)
7315 return TRUE;
7316 ++look;
7317 }
7318
7319 /*
7320 * Skip over ", ".
7321 */
7322 look = skip_to_option_part(look);
7323 }
7324 return FALSE;
7325}
7326#endif /* FEAT_CINDENT */
7327
7328#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7329/*
7330 * Map Hebrew keyboard when in hkmap mode.
7331 */
7332 int
7333hkmap(c)
7334 int c;
7335{
7336 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7337 {
7338 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7339 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7340 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7341 static char_u map[26] =
7342 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7343 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7344 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7345 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7346 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7347 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7348 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7349 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7350 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7351
7352 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7353 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7354 /* '-1'='sofit' */
7355 else if (c == 'x')
7356 return 'X';
7357 else if (c == 'q')
7358 return '\''; /* {geresh}={'} */
7359 else if (c == 246)
7360 return ' '; /* \"o --> ' ' for a german keyboard */
7361 else if (c == 228)
7362 return ' '; /* \"a --> ' ' -- / -- */
7363 else if (c == 252)
7364 return ' '; /* \"u --> ' ' -- / -- */
7365#ifdef EBCDIC
7366 else if (islower(c))
7367#else
7368 /* NOTE: islower() does not do the right thing for us on Linux so we
7369 * do this the same was as 5.7 and previous, so it works correctly on
7370 * all systems. Specifically, the e.g. Delete and Arrow keys are
7371 * munged and won't work if e.g. searching for Hebrew text.
7372 */
7373 else if (c >= 'a' && c <= 'z')
7374#endif
7375 return (int)(map[CharOrdLow(c)] + p_aleph);
7376 else
7377 return c;
7378 }
7379 else
7380 {
7381 switch (c)
7382 {
7383 case '`': return ';';
7384 case '/': return '.';
7385 case '\'': return ',';
7386 case 'q': return '/';
7387 case 'w': return '\'';
7388
7389 /* Hebrew letters - set offset from 'a' */
7390 case ',': c = '{'; break;
7391 case '.': c = 'v'; break;
7392 case ';': c = 't'; break;
7393 default: {
7394 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7395
7396#ifdef EBCDIC
7397 /* see note about islower() above */
7398 if (!islower(c))
7399#else
7400 if (c < 'a' || c > 'z')
7401#endif
7402 return c;
7403 c = str[CharOrdLow(c)];
7404 break;
7405 }
7406 }
7407
7408 return (int)(CharOrdLow(c) + p_aleph);
7409 }
7410}
7411#endif
7412
7413 static void
7414ins_reg()
7415{
7416 int need_redraw = FALSE;
7417 int regname;
7418 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007419#ifdef FEAT_VISUAL
7420 int vis_active = VIsual_active;
7421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422
7423 /*
7424 * If we are going to wait for a character, show a '"'.
7425 */
7426 pc_status = PC_STATUS_UNSET;
7427 if (redrawing() && !char_avail())
7428 {
7429 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007430 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431
7432 edit_putchar('"', TRUE);
7433#ifdef FEAT_CMDL_INFO
7434 add_to_showcmd_c(Ctrl_R);
7435#endif
7436 }
7437
7438#ifdef USE_ON_FLY_SCROLL
7439 dont_scroll = TRUE; /* disallow scrolling here */
7440#endif
7441
7442 /*
7443 * Don't map the register name. This also prevents the mode message to be
7444 * deleted when ESC is hit.
7445 */
7446 ++no_mapping;
7447 regname = safe_vgetc();
7448#ifdef FEAT_LANGMAP
7449 LANGMAP_ADJUST(regname, TRUE);
7450#endif
7451 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
7452 {
7453 /* Get a third key for literal register insertion */
7454 literally = regname;
7455#ifdef FEAT_CMDL_INFO
7456 add_to_showcmd_c(literally);
7457#endif
7458 regname = safe_vgetc();
7459#ifdef FEAT_LANGMAP
7460 LANGMAP_ADJUST(regname, TRUE);
7461#endif
7462 }
7463 --no_mapping;
7464
7465#ifdef FEAT_EVAL
7466 /*
7467 * Don't call u_sync() while getting the expression,
7468 * evaluating it or giving an error message for it!
7469 */
7470 ++no_u_sync;
7471 if (regname == '=')
7472 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007473# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007475# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007477# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 /* Restore the Input Method. */
7479 if (im_on)
7480 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007481# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00007483 if (regname == NUL || !valid_yank_reg(regname, FALSE))
7484 {
7485 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 else
7489 {
7490#endif
7491 if (literally == Ctrl_O || literally == Ctrl_P)
7492 {
7493 /* Append the command to the redo buffer. */
7494 AppendCharToRedobuff(Ctrl_R);
7495 AppendCharToRedobuff(literally);
7496 AppendCharToRedobuff(regname);
7497
7498 do_put(regname, BACKWARD, 1L,
7499 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
7500 }
7501 else if (insert_reg(regname, literally) == FAIL)
7502 {
7503 vim_beep();
7504 need_redraw = TRUE; /* remove the '"' */
7505 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007506 else if (stop_insert_mode)
7507 /* When the '=' register was used and a function was invoked that
7508 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7509 * insert anything, need to remove the '"' */
7510 need_redraw = TRUE;
7511
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512#ifdef FEAT_EVAL
7513 }
7514 --no_u_sync;
7515#endif
7516#ifdef FEAT_CMDL_INFO
7517 clear_showcmd();
7518#endif
7519
7520 /* If the inserted register is empty, we need to remove the '"' */
7521 if (need_redraw || stuff_empty())
7522 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007523
7524#ifdef FEAT_VISUAL
7525 /* Disallow starting Visual mode here, would get a weird mode. */
7526 if (!vis_active && VIsual_active)
7527 end_visual_mode();
7528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529}
7530
7531/*
7532 * CTRL-G commands in Insert mode.
7533 */
7534 static void
7535ins_ctrl_g()
7536{
7537 int c;
7538
7539#ifdef FEAT_INS_EXPAND
7540 /* Right after CTRL-X the cursor will be after the ruler. */
7541 setcursor();
7542#endif
7543
7544 /*
7545 * Don't map the second key. This also prevents the mode message to be
7546 * deleted when ESC is hit.
7547 */
7548 ++no_mapping;
7549 c = safe_vgetc();
7550 --no_mapping;
7551 switch (c)
7552 {
7553 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7554 case K_UP:
7555 case Ctrl_K:
7556 case 'k': ins_up(TRUE);
7557 break;
7558
7559 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7560 case K_DOWN:
7561 case Ctrl_J:
7562 case 'j': ins_down(TRUE);
7563 break;
7564
7565 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00007566 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007568
7569 /* Need to reset Insstart, esp. because a BS that joins
7570 * aline to the previous one must save for undo. */
7571 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 break;
7573
7574 /* Unknown CTRL-G command, reserved for future expansion. */
7575 default: vim_beep();
7576 }
7577}
7578
7579/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007580 * CTRL-^ in Insert mode.
7581 */
7582 static void
7583ins_ctrl_hat()
7584{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00007585 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007586 {
7587 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7588 if (State & LANGMAP)
7589 {
7590 curbuf->b_p_iminsert = B_IMODE_NONE;
7591 State &= ~LANGMAP;
7592 }
7593 else
7594 {
7595 curbuf->b_p_iminsert = B_IMODE_LMAP;
7596 State |= LANGMAP;
7597#ifdef USE_IM_CONTROL
7598 im_set_active(FALSE);
7599#endif
7600 }
7601 }
7602#ifdef USE_IM_CONTROL
7603 else
7604 {
7605 /* There are no ":lmap" mappings, toggle IM */
7606 if (im_get_status())
7607 {
7608 curbuf->b_p_iminsert = B_IMODE_NONE;
7609 im_set_active(FALSE);
7610 }
7611 else
7612 {
7613 curbuf->b_p_iminsert = B_IMODE_IM;
7614 State &= ~LANGMAP;
7615 im_set_active(TRUE);
7616 }
7617 }
7618#endif
7619 set_iminsert_global();
7620 showmode();
7621#ifdef FEAT_GUI
7622 /* may show different cursor shape or color */
7623 if (gui.in_use)
7624 gui_update_cursor(TRUE, FALSE);
7625#endif
7626#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7627 /* Show/unshow value of 'keymap' in status lines. */
7628 status_redraw_curbuf();
7629#endif
7630}
7631
7632/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 * Handle ESC in insert mode.
7634 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7635 * insert.
7636 */
7637 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00007638ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 long *count;
7640 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00007641 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642{
7643 int temp;
7644 static int disabled_redraw = FALSE;
7645
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007646#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007647 check_spell_redraw();
7648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649#if defined(FEAT_HANGULIN)
7650# if defined(ESC_CHG_TO_ENG_MODE)
7651 hangul_input_state_set(0);
7652# endif
7653 if (composing_hangul)
7654 {
7655 push_raw_key(composing_hangul_buffer, 2);
7656 composing_hangul = 0;
7657 }
7658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659
7660 temp = curwin->w_cursor.col;
7661 if (disabled_redraw)
7662 {
7663 --RedrawingDisabled;
7664 disabled_redraw = FALSE;
7665 }
7666 if (!arrow_used)
7667 {
7668 /*
7669 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00007670 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7671 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 */
7673 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00007674 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675
7676 /*
7677 * Repeating insert may take a long time. Check for
7678 * interrupt now and then.
7679 */
7680 if (*count > 0)
7681 {
7682 line_breakcheck();
7683 if (got_int)
7684 *count = 0;
7685 }
7686
7687 if (--*count > 0) /* repeat what was typed */
7688 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007689 /* Vi repeats the insert without replacing characters. */
7690 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
7691 State &= ~REPLACE_FLAG;
7692
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 (void)start_redo_ins();
7694 if (cmdchar == 'r' || cmdchar == 'v')
7695 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
7696 ++RedrawingDisabled;
7697 disabled_redraw = TRUE;
7698 return FALSE; /* repeat the insert */
7699 }
7700 stop_insert(&curwin->w_cursor, TRUE);
7701 undisplay_dollar();
7702 }
7703
7704 /* When an autoindent was removed, curswant stays after the
7705 * indent */
7706 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
7707 curwin->w_set_curswant = TRUE;
7708
7709 /* Remember the last Insert position in the '^ mark. */
7710 if (!cmdmod.keepjumps)
7711 curbuf->b_last_insert = curwin->w_cursor;
7712
7713 /*
7714 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00007715 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00007717 if (!nomove
7718 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719#ifdef FEAT_VIRTUALEDIT
7720 || curwin->w_cursor.coladd > 0
7721#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007722 )
7723 && (restart_edit == NUL
7724 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00007726 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007728 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729#ifdef FEAT_RIGHTLEFT
7730 && !revins_on
7731#endif
7732 )
7733 {
7734#ifdef FEAT_VIRTUALEDIT
7735 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
7736 {
7737 oneleft();
7738 if (restart_edit != NUL)
7739 ++curwin->w_cursor.coladd;
7740 }
7741 else
7742#endif
7743 {
7744 --curwin->w_cursor.col;
7745#ifdef FEAT_MBYTE
7746 /* Correct cursor for multi-byte character. */
7747 if (has_mbyte)
7748 mb_adjust_cursor();
7749#endif
7750 }
7751 }
7752
7753#ifdef USE_IM_CONTROL
7754 /* Disable IM to allow typing English directly for Normal mode commands.
7755 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
7756 * well). */
7757 if (!(State & LANGMAP))
7758 im_save_status(&curbuf->b_p_iminsert);
7759 im_set_active(FALSE);
7760#endif
7761
7762 State = NORMAL;
7763 /* need to position cursor again (e.g. when on a TAB ) */
7764 changed_cline_bef_curs();
7765
7766#ifdef FEAT_MOUSE
7767 setmouse();
7768#endif
7769#ifdef CURSOR_SHAPE
7770 ui_cursor_shape(); /* may show different cursor shape */
7771#endif
7772
7773 /*
7774 * When recording or for CTRL-O, need to display the new mode.
7775 * Otherwise remove the mode message.
7776 */
7777 if (Recording || restart_edit != NUL)
7778 showmode();
7779 else if (p_smd)
7780 MSG("");
7781
7782 return TRUE; /* exit Insert mode */
7783}
7784
7785#ifdef FEAT_RIGHTLEFT
7786/*
7787 * Toggle language: hkmap and revins_on.
7788 * Move to end of reverse inserted text.
7789 */
7790 static void
7791ins_ctrl_()
7792{
7793 if (revins_on && revins_chars && revins_scol >= 0)
7794 {
7795 while (gchar_cursor() != NUL && revins_chars--)
7796 ++curwin->w_cursor.col;
7797 }
7798 p_ri = !p_ri;
7799 revins_on = (State == INSERT && p_ri);
7800 if (revins_on)
7801 {
7802 revins_scol = curwin->w_cursor.col;
7803 revins_legal++;
7804 revins_chars = 0;
7805 undisplay_dollar();
7806 }
7807 else
7808 revins_scol = -1;
7809#ifdef FEAT_FKMAP
7810 if (p_altkeymap)
7811 {
7812 /*
7813 * to be consistent also for redo command, using '.'
7814 * set arrow_used to true and stop it - causing to redo
7815 * characters entered in one mode (normal/reverse insert).
7816 */
7817 arrow_used = TRUE;
7818 (void)stop_arrow();
7819 p_fkmap = curwin->w_p_rl ^ p_ri;
7820 if (p_fkmap && p_ri)
7821 State = INSERT;
7822 }
7823 else
7824#endif
7825 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
7826 showmode();
7827}
7828#endif
7829
7830#ifdef FEAT_VISUAL
7831/*
7832 * If 'keymodel' contains "startsel", may start selection.
7833 * Returns TRUE when a CTRL-O and other keys stuffed.
7834 */
7835 static int
7836ins_start_select(c)
7837 int c;
7838{
7839 if (km_startsel)
7840 switch (c)
7841 {
7842 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 case K_PAGEUP:
7845 case K_KPAGEUP:
7846 case K_PAGEDOWN:
7847 case K_KPAGEDOWN:
7848# ifdef MACOS
7849 case K_LEFT:
7850 case K_RIGHT:
7851 case K_UP:
7852 case K_DOWN:
7853 case K_END:
7854 case K_HOME:
7855# endif
7856 if (!(mod_mask & MOD_MASK_SHIFT))
7857 break;
7858 /* FALLTHROUGH */
7859 case K_S_LEFT:
7860 case K_S_RIGHT:
7861 case K_S_UP:
7862 case K_S_DOWN:
7863 case K_S_END:
7864 case K_S_HOME:
7865 /* Start selection right away, the cursor can move with
7866 * CTRL-O when beyond the end of the line. */
7867 start_selection();
7868
7869 /* Execute the key in (insert) Select mode. */
7870 stuffcharReadbuff(Ctrl_O);
7871 if (mod_mask)
7872 {
7873 char_u buf[4];
7874
7875 buf[0] = K_SPECIAL;
7876 buf[1] = KS_MODIFIER;
7877 buf[2] = mod_mask;
7878 buf[3] = NUL;
7879 stuffReadbuff(buf);
7880 }
7881 stuffcharReadbuff(c);
7882 return TRUE;
7883 }
7884 return FALSE;
7885}
7886#endif
7887
7888/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007889 * <Insert> key in Insert mode: toggle insert/remplace mode.
7890 */
7891 static void
7892ins_insert(replaceState)
7893 int replaceState;
7894{
7895#ifdef FEAT_FKMAP
7896 if (p_fkmap && p_ri)
7897 {
7898 beep_flush();
7899 EMSG(farsi_text_3); /* encoded in Farsi */
7900 return;
7901 }
7902#endif
7903
7904#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00007905# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007906 set_vim_var_string(VV_INSERTMODE,
7907 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007908# ifdef FEAT_VREPLACE
7909 replaceState == VREPLACE ? "v" :
7910# endif
7911 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00007912# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007913 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
7914#endif
7915 if (State & REPLACE_FLAG)
7916 State = INSERT | (State & LANGMAP);
7917 else
7918 State = replaceState | (State & LANGMAP);
7919 AppendCharToRedobuff(K_INS);
7920 showmode();
7921#ifdef CURSOR_SHAPE
7922 ui_cursor_shape(); /* may show different cursor shape */
7923#endif
7924}
7925
7926/*
7927 * Pressed CTRL-O in Insert mode.
7928 */
7929 static void
7930ins_ctrl_o()
7931{
7932#ifdef FEAT_VREPLACE
7933 if (State & VREPLACE_FLAG)
7934 restart_edit = 'V';
7935 else
7936#endif
7937 if (State & REPLACE_FLAG)
7938 restart_edit = 'R';
7939 else
7940 restart_edit = 'I';
7941#ifdef FEAT_VIRTUALEDIT
7942 if (virtual_active())
7943 ins_at_eol = FALSE; /* cursor always keeps its column */
7944 else
7945#endif
7946 ins_at_eol = (gchar_cursor() == NUL);
7947}
7948
7949/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 * If the cursor is on an indent, ^T/^D insert/delete one
7951 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
7952 * Always round the indent to 'shiftwith', this is compatible
7953 * with vi. But vi only supports ^T and ^D after an
7954 * autoindent, we support it everywhere.
7955 */
7956 static void
7957ins_shift(c, lastc)
7958 int c;
7959 int lastc;
7960{
7961 if (stop_arrow() == FAIL)
7962 return;
7963 AppendCharToRedobuff(c);
7964
7965 /*
7966 * 0^D and ^^D: remove all indent.
7967 */
7968 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
7969 {
7970 --curwin->w_cursor.col;
7971 (void)del_char(FALSE); /* delete the '^' or '0' */
7972 /* In Replace mode, restore the characters that '^' or '0' replaced. */
7973 if (State & REPLACE_FLAG)
7974 replace_pop_ins();
7975 if (lastc == '^')
7976 old_indent = get_indent(); /* remember curr. indent */
7977 change_indent(INDENT_SET, 0, TRUE, 0);
7978 }
7979 else
7980 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
7981
7982 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
7983 did_ai = FALSE;
7984#ifdef FEAT_SMARTINDENT
7985 did_si = FALSE;
7986 can_si = FALSE;
7987 can_si_back = FALSE;
7988#endif
7989#ifdef FEAT_CINDENT
7990 can_cindent = FALSE; /* no cindenting after ^D or ^T */
7991#endif
7992}
7993
7994 static void
7995ins_del()
7996{
7997 int temp;
7998
7999 if (stop_arrow() == FAIL)
8000 return;
8001 if (gchar_cursor() == NUL) /* delete newline */
8002 {
8003 temp = curwin->w_cursor.col;
8004 if (!can_bs(BS_EOL) /* only if "eol" included */
8005 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
8006 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
8007 || do_join(FALSE) == FAIL)
8008 vim_beep();
8009 else
8010 curwin->w_cursor.col = temp;
8011 }
8012 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8013 vim_beep();
8014 did_ai = FALSE;
8015#ifdef FEAT_SMARTINDENT
8016 did_si = FALSE;
8017 can_si = FALSE;
8018 can_si_back = FALSE;
8019#endif
8020 AppendCharToRedobuff(K_DEL);
8021}
8022
8023/*
8024 * Handle Backspace, delete-word and delete-line in Insert mode.
8025 * Return TRUE when backspace was actually used.
8026 */
8027 static int
8028ins_bs(c, mode, inserted_space_p)
8029 int c;
8030 int mode;
8031 int *inserted_space_p;
8032{
8033 linenr_T lnum;
8034 int cc;
8035 int temp = 0; /* init for GCC */
8036 colnr_T mincol;
8037 int did_backspace = FALSE;
8038 int in_indent;
8039 int oldState;
8040#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008041 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042#endif
8043
8044 /*
8045 * can't delete anything in an empty file
8046 * can't backup past first character in buffer
8047 * can't backup past starting point unless 'backspace' > 1
8048 * can backup to a previous line if 'backspace' == 0
8049 */
8050 if ( bufempty()
8051 || (
8052#ifdef FEAT_RIGHTLEFT
8053 !revins_on &&
8054#endif
8055 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8056 || (!can_bs(BS_START)
8057 && (arrow_used
8058 || (curwin->w_cursor.lnum == Insstart.lnum
8059 && curwin->w_cursor.col <= Insstart.col)))
8060 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8061 && curwin->w_cursor.col <= ai_col)
8062 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8063 {
8064 vim_beep();
8065 return FALSE;
8066 }
8067
8068 if (stop_arrow() == FAIL)
8069 return FALSE;
8070 in_indent = inindent(0);
8071#ifdef FEAT_CINDENT
8072 if (in_indent)
8073 can_cindent = FALSE;
8074#endif
8075#ifdef FEAT_COMMENTS
8076 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8077#endif
8078#ifdef FEAT_RIGHTLEFT
8079 if (revins_on) /* put cursor after last inserted char */
8080 inc_cursor();
8081#endif
8082
8083#ifdef FEAT_VIRTUALEDIT
8084 /* Virtualedit:
8085 * BACKSPACE_CHAR eats a virtual space
8086 * BACKSPACE_WORD eats all coladd
8087 * BACKSPACE_LINE eats all coladd and keeps going
8088 */
8089 if (curwin->w_cursor.coladd > 0)
8090 {
8091 if (mode == BACKSPACE_CHAR)
8092 {
8093 --curwin->w_cursor.coladd;
8094 return TRUE;
8095 }
8096 if (mode == BACKSPACE_WORD)
8097 {
8098 curwin->w_cursor.coladd = 0;
8099 return TRUE;
8100 }
8101 curwin->w_cursor.coladd = 0;
8102 }
8103#endif
8104
8105 /*
8106 * delete newline!
8107 */
8108 if (curwin->w_cursor.col == 0)
8109 {
8110 lnum = Insstart.lnum;
8111 if (curwin->w_cursor.lnum == Insstart.lnum
8112#ifdef FEAT_RIGHTLEFT
8113 || revins_on
8114#endif
8115 )
8116 {
8117 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8118 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8119 return FALSE;
8120 --Insstart.lnum;
8121 Insstart.col = MAXCOL;
8122 }
8123 /*
8124 * In replace mode:
8125 * cc < 0: NL was inserted, delete it
8126 * cc >= 0: NL was replaced, put original characters back
8127 */
8128 cc = -1;
8129 if (State & REPLACE_FLAG)
8130 cc = replace_pop(); /* returns -1 if NL was inserted */
8131 /*
8132 * In replace mode, in the line we started replacing, we only move the
8133 * cursor.
8134 */
8135 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8136 {
8137 dec_cursor();
8138 }
8139 else
8140 {
8141#ifdef FEAT_VREPLACE
8142 if (!(State & VREPLACE_FLAG)
8143 || curwin->w_cursor.lnum > orig_line_count)
8144#endif
8145 {
8146 temp = gchar_cursor(); /* remember current char */
8147 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008148
8149 /* When "aw" is in 'formatoptions' we must delete the space at
8150 * the end of the line, otherwise the line will be broken
8151 * again when auto-formatting. */
8152 if (has_format_option(FO_AUTO)
8153 && has_format_option(FO_WHITE_PAR))
8154 {
8155 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8156 TRUE);
8157 int len;
8158
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008159 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008160 if (len > 0 && ptr[len - 1] == ' ')
8161 ptr[len - 1] = NUL;
8162 }
8163
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 (void)do_join(FALSE);
8165 if (temp == NUL && gchar_cursor() != NUL)
8166 inc_cursor();
8167 }
8168#ifdef FEAT_VREPLACE
8169 else
8170 dec_cursor();
8171#endif
8172
8173 /*
8174 * In REPLACE mode we have to put back the text that was replaced
8175 * by the NL. On the replace stack is first a NUL-terminated
8176 * sequence of characters that were deleted and then the
8177 * characters that NL replaced.
8178 */
8179 if (State & REPLACE_FLAG)
8180 {
8181 /*
8182 * Do the next ins_char() in NORMAL state, to
8183 * prevent ins_char() from replacing characters and
8184 * avoiding showmatch().
8185 */
8186 oldState = State;
8187 State = NORMAL;
8188 /*
8189 * restore characters (blanks) deleted after cursor
8190 */
8191 while (cc > 0)
8192 {
8193 temp = curwin->w_cursor.col;
8194#ifdef FEAT_MBYTE
8195 mb_replace_pop_ins(cc);
8196#else
8197 ins_char(cc);
8198#endif
8199 curwin->w_cursor.col = temp;
8200 cc = replace_pop();
8201 }
8202 /* restore the characters that NL replaced */
8203 replace_pop_ins();
8204 State = oldState;
8205 }
8206 }
8207 did_ai = FALSE;
8208 }
8209 else
8210 {
8211 /*
8212 * Delete character(s) before the cursor.
8213 */
8214#ifdef FEAT_RIGHTLEFT
8215 if (revins_on) /* put cursor on last inserted char */
8216 dec_cursor();
8217#endif
8218 mincol = 0;
8219 /* keep indent */
8220 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
8221#ifdef FEAT_RIGHTLEFT
8222 && !revins_on
8223#endif
8224 )
8225 {
8226 temp = curwin->w_cursor.col;
8227 beginline(BL_WHITE);
8228 if (curwin->w_cursor.col < (colnr_T)temp)
8229 mincol = curwin->w_cursor.col;
8230 curwin->w_cursor.col = temp;
8231 }
8232
8233 /*
8234 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8235 */
8236 if ( mode == BACKSPACE_CHAR
8237 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00008238 || (curbuf->b_p_sts != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 && (*(ml_get_cursor() - 1) == TAB
8240 || (*(ml_get_cursor() - 1) == ' '
8241 && (!*inserted_space_p
8242 || arrow_used))))))
8243 {
8244 int ts;
8245 colnr_T vcol;
8246 colnr_T want_vcol;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008247#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 int extra = 0;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250
8251 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008252 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 ts = curbuf->b_p_sw;
8254 else
8255 ts = curbuf->b_p_sts;
8256 /* Compute the virtual column where we want to be. Since
8257 * 'showbreak' may get in the way, need to get the last column of
8258 * the previous character. */
8259 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8260 dec_cursor();
8261 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8262 inc_cursor();
8263 want_vcol = (want_vcol / ts) * ts;
8264
8265 /* delete characters until we are at or before want_vcol */
8266 while (vcol > want_vcol
8267 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
8268 {
8269 dec_cursor();
8270 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8271 if (State & REPLACE_FLAG)
8272 {
8273 /* Don't delete characters before the insert point when in
8274 * Replace mode */
8275 if (curwin->w_cursor.lnum != Insstart.lnum
8276 || curwin->w_cursor.col >= Insstart.col)
8277 {
8278#if 0 /* what was this for? It causes problems when sw != ts. */
8279 if (State == REPLACE && (int)vcol < want_vcol)
8280 {
8281 (void)del_char(FALSE);
8282 extra = 2; /* don't pop too much */
8283 }
8284 else
8285#endif
8286 replace_do_bs();
8287 }
8288 }
8289 else
8290 (void)del_char(FALSE);
8291 }
8292
8293 /* insert extra spaces until we are at want_vcol */
8294 while (vcol < want_vcol)
8295 {
8296 /* Remember the first char we inserted */
8297 if (curwin->w_cursor.lnum == Insstart.lnum
8298 && curwin->w_cursor.col < Insstart.col)
8299 Insstart.col = curwin->w_cursor.col;
8300
8301#ifdef FEAT_VREPLACE
8302 if (State & VREPLACE_FLAG)
8303 ins_char(' ');
8304 else
8305#endif
8306 {
8307 ins_str((char_u *)" ");
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008308 if ((State & REPLACE_FLAG) /* && extra <= 1 */)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008310#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 if (extra)
8312 replace_push_off(NUL);
8313 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 replace_push(NUL);
8316 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008317#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 if (extra == 2)
8319 extra = 1;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 }
8322 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8323 }
8324 }
8325
8326 /*
8327 * Delete upto starting point, start of line or previous word.
8328 */
8329 else do
8330 {
8331#ifdef FEAT_RIGHTLEFT
8332 if (!revins_on) /* put cursor on char to be deleted */
8333#endif
8334 dec_cursor();
8335
8336 /* start of word? */
8337 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
8338 {
8339 mode = BACKSPACE_WORD_NOT_SPACE;
8340 temp = vim_iswordc(gchar_cursor());
8341 }
8342 /* end of word? */
8343 else if (mode == BACKSPACE_WORD_NOT_SPACE
8344 && (vim_isspace(cc = gchar_cursor())
8345 || vim_iswordc(cc) != temp))
8346 {
8347#ifdef FEAT_RIGHTLEFT
8348 if (!revins_on)
8349#endif
8350 inc_cursor();
8351#ifdef FEAT_RIGHTLEFT
8352 else if (State & REPLACE_FLAG)
8353 dec_cursor();
8354#endif
8355 break;
8356 }
8357 if (State & REPLACE_FLAG)
8358 replace_do_bs();
8359 else
8360 {
8361#ifdef FEAT_MBYTE
8362 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008363 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364#endif
8365 (void)del_char(FALSE);
8366#ifdef FEAT_MBYTE
8367 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008368 * If there are combining characters and 'delcombine' is set
8369 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 * character.
8371 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008372 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 inc_cursor();
8374#endif
8375#ifdef FEAT_RIGHTLEFT
8376 if (revins_chars)
8377 {
8378 revins_chars--;
8379 revins_legal++;
8380 }
8381 if (revins_on && gchar_cursor() == NUL)
8382 break;
8383#endif
8384 }
8385 /* Just a single backspace?: */
8386 if (mode == BACKSPACE_CHAR)
8387 break;
8388 } while (
8389#ifdef FEAT_RIGHTLEFT
8390 revins_on ||
8391#endif
8392 (curwin->w_cursor.col > mincol
8393 && (curwin->w_cursor.lnum != Insstart.lnum
8394 || curwin->w_cursor.col != Insstart.col)));
8395 did_backspace = TRUE;
8396 }
8397#ifdef FEAT_SMARTINDENT
8398 did_si = FALSE;
8399 can_si = FALSE;
8400 can_si_back = FALSE;
8401#endif
8402 if (curwin->w_cursor.col <= 1)
8403 did_ai = FALSE;
8404 /*
8405 * It's a little strange to put backspaces into the redo
8406 * buffer, but it makes auto-indent a lot easier to deal
8407 * with.
8408 */
8409 AppendCharToRedobuff(c);
8410
8411 /* If deleted before the insertion point, adjust it */
8412 if (curwin->w_cursor.lnum == Insstart.lnum
8413 && curwin->w_cursor.col < Insstart.col)
8414 Insstart.col = curwin->w_cursor.col;
8415
8416 /* vi behaviour: the cursor moves backward but the character that
8417 * was there remains visible
8418 * Vim behaviour: the cursor moves backward and the character that
8419 * was there is erased from the screen.
8420 * We can emulate the vi behaviour by pretending there is a dollar
8421 * displayed even when there isn't.
8422 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8423 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
8424 dollar_vcol = curwin->w_virtcol;
8425
8426 return did_backspace;
8427}
8428
8429#ifdef FEAT_MOUSE
8430 static void
8431ins_mouse(c)
8432 int c;
8433{
8434 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008435 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436
8437# ifdef FEAT_GUI
8438 /* When GUI is active, also move/paste when 'mouse' is empty */
8439 if (!gui.in_use)
8440# endif
8441 if (!mouse_has(MOUSE_INSERT))
8442 return;
8443
8444 undisplay_dollar();
8445 tpos = curwin->w_cursor;
8446 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
8447 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008448#ifdef FEAT_WINDOWS
8449 win_T *new_curwin = curwin;
8450
8451 if (curwin != old_curwin && win_valid(old_curwin))
8452 {
8453 /* Mouse took us to another window. We need to go back to the
8454 * previous one to stop insert there properly. */
8455 curwin = old_curwin;
8456 curbuf = curwin->w_buffer;
8457 }
8458#endif
8459 start_arrow(curwin == old_curwin ? &tpos : NULL);
8460#ifdef FEAT_WINDOWS
8461 if (curwin != new_curwin && win_valid(new_curwin))
8462 {
8463 curwin = new_curwin;
8464 curbuf = curwin->w_buffer;
8465 }
8466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467# ifdef FEAT_CINDENT
8468 can_cindent = TRUE;
8469# endif
8470 }
8471
8472#ifdef FEAT_WINDOWS
8473 /* redraw status lines (in case another window became active) */
8474 redraw_statuslines();
8475#endif
8476}
8477
8478 static void
8479ins_mousescroll(up)
8480 int up;
8481{
8482 pos_T tpos;
8483# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8484 win_T *old_curwin;
8485# endif
8486
8487 tpos = curwin->w_cursor;
8488
8489# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8490 old_curwin = curwin;
8491
8492 /* Currently the mouse coordinates are only known in the GUI. */
8493 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
8494 {
8495 int row, col;
8496
8497 row = mouse_row;
8498 col = mouse_col;
8499
8500 /* find the window at the pointer coordinates */
8501 curwin = mouse_find_win(&row, &col);
8502 curbuf = curwin->w_buffer;
8503 }
8504 if (curwin == old_curwin)
8505# endif
8506 undisplay_dollar();
8507
8508 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8509 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
8510 else
8511 scroll_redraw(up, 3L);
8512
8513# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8514 curwin->w_redr_status = TRUE;
8515
8516 curwin = old_curwin;
8517 curbuf = curwin->w_buffer;
8518# endif
8519
8520 if (!equalpos(curwin->w_cursor, tpos))
8521 {
8522 start_arrow(&tpos);
8523# ifdef FEAT_CINDENT
8524 can_cindent = TRUE;
8525# endif
8526 }
8527}
8528#endif
8529
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008530#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00008531 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008532ins_tabline(c)
8533 int c;
8534{
8535 /* We will be leaving the current window, unless closing another tab. */
8536 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
8537 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
8538 {
8539 undisplay_dollar();
8540 start_arrow(&curwin->w_cursor);
8541# ifdef FEAT_CINDENT
8542 can_cindent = TRUE;
8543# endif
8544 }
8545
8546 if (c == K_TABLINE)
8547 goto_tabpage(current_tab);
8548 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008549 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008550 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008551 redraw_statuslines(); /* will redraw the tabline when needed */
8552 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008553}
8554#endif
8555
8556#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 void
8558ins_scroll()
8559{
8560 pos_T tpos;
8561
8562 undisplay_dollar();
8563 tpos = curwin->w_cursor;
8564 if (gui_do_scroll())
8565 {
8566 start_arrow(&tpos);
8567# ifdef FEAT_CINDENT
8568 can_cindent = TRUE;
8569# endif
8570 }
8571}
8572
8573 void
8574ins_horscroll()
8575{
8576 pos_T tpos;
8577
8578 undisplay_dollar();
8579 tpos = curwin->w_cursor;
8580 if (gui_do_horiz_scroll())
8581 {
8582 start_arrow(&tpos);
8583# ifdef FEAT_CINDENT
8584 can_cindent = TRUE;
8585# endif
8586 }
8587}
8588#endif
8589
8590 static void
8591ins_left()
8592{
8593 pos_T tpos;
8594
8595#ifdef FEAT_FOLDING
8596 if ((fdo_flags & FDO_HOR) && KeyTyped)
8597 foldOpenCursor();
8598#endif
8599 undisplay_dollar();
8600 tpos = curwin->w_cursor;
8601 if (oneleft() == OK)
8602 {
8603 start_arrow(&tpos);
8604#ifdef FEAT_RIGHTLEFT
8605 /* If exit reversed string, position is fixed */
8606 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
8607 revins_legal++;
8608 revins_chars++;
8609#endif
8610 }
8611
8612 /*
8613 * if 'whichwrap' set for cursor in insert mode may go to
8614 * previous line
8615 */
8616 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
8617 {
8618 start_arrow(&tpos);
8619 --(curwin->w_cursor.lnum);
8620 coladvance((colnr_T)MAXCOL);
8621 curwin->w_set_curswant = TRUE; /* so we stay at the end */
8622 }
8623 else
8624 vim_beep();
8625}
8626
8627 static void
8628ins_home(c)
8629 int c;
8630{
8631 pos_T tpos;
8632
8633#ifdef FEAT_FOLDING
8634 if ((fdo_flags & FDO_HOR) && KeyTyped)
8635 foldOpenCursor();
8636#endif
8637 undisplay_dollar();
8638 tpos = curwin->w_cursor;
8639 if (c == K_C_HOME)
8640 curwin->w_cursor.lnum = 1;
8641 curwin->w_cursor.col = 0;
8642#ifdef FEAT_VIRTUALEDIT
8643 curwin->w_cursor.coladd = 0;
8644#endif
8645 curwin->w_curswant = 0;
8646 start_arrow(&tpos);
8647}
8648
8649 static void
8650ins_end(c)
8651 int c;
8652{
8653 pos_T tpos;
8654
8655#ifdef FEAT_FOLDING
8656 if ((fdo_flags & FDO_HOR) && KeyTyped)
8657 foldOpenCursor();
8658#endif
8659 undisplay_dollar();
8660 tpos = curwin->w_cursor;
8661 if (c == K_C_END)
8662 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
8663 coladvance((colnr_T)MAXCOL);
8664 curwin->w_curswant = MAXCOL;
8665
8666 start_arrow(&tpos);
8667}
8668
8669 static void
8670ins_s_left()
8671{
8672#ifdef FEAT_FOLDING
8673 if ((fdo_flags & FDO_HOR) && KeyTyped)
8674 foldOpenCursor();
8675#endif
8676 undisplay_dollar();
8677 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
8678 {
8679 start_arrow(&curwin->w_cursor);
8680 (void)bck_word(1L, FALSE, FALSE);
8681 curwin->w_set_curswant = TRUE;
8682 }
8683 else
8684 vim_beep();
8685}
8686
8687 static void
8688ins_right()
8689{
8690#ifdef FEAT_FOLDING
8691 if ((fdo_flags & FDO_HOR) && KeyTyped)
8692 foldOpenCursor();
8693#endif
8694 undisplay_dollar();
8695 if (gchar_cursor() != NUL || virtual_active()
8696 )
8697 {
8698 start_arrow(&curwin->w_cursor);
8699 curwin->w_set_curswant = TRUE;
8700#ifdef FEAT_VIRTUALEDIT
8701 if (virtual_active())
8702 oneright();
8703 else
8704#endif
8705 {
8706#ifdef FEAT_MBYTE
8707 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008708 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 else
8710#endif
8711 ++curwin->w_cursor.col;
8712 }
8713
8714#ifdef FEAT_RIGHTLEFT
8715 revins_legal++;
8716 if (revins_chars)
8717 revins_chars--;
8718#endif
8719 }
8720 /* if 'whichwrap' set for cursor in insert mode, may move the
8721 * cursor to the next line */
8722 else if (vim_strchr(p_ww, ']') != NULL
8723 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
8724 {
8725 start_arrow(&curwin->w_cursor);
8726 curwin->w_set_curswant = TRUE;
8727 ++curwin->w_cursor.lnum;
8728 curwin->w_cursor.col = 0;
8729 }
8730 else
8731 vim_beep();
8732}
8733
8734 static void
8735ins_s_right()
8736{
8737#ifdef FEAT_FOLDING
8738 if ((fdo_flags & FDO_HOR) && KeyTyped)
8739 foldOpenCursor();
8740#endif
8741 undisplay_dollar();
8742 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
8743 || gchar_cursor() != NUL)
8744 {
8745 start_arrow(&curwin->w_cursor);
8746 (void)fwd_word(1L, FALSE, 0);
8747 curwin->w_set_curswant = TRUE;
8748 }
8749 else
8750 vim_beep();
8751}
8752
8753 static void
8754ins_up(startcol)
8755 int startcol; /* when TRUE move to Insstart.col */
8756{
8757 pos_T tpos;
8758 linenr_T old_topline = curwin->w_topline;
8759#ifdef FEAT_DIFF
8760 int old_topfill = curwin->w_topfill;
8761#endif
8762
8763 undisplay_dollar();
8764 tpos = curwin->w_cursor;
8765 if (cursor_up(1L, TRUE) == OK)
8766 {
8767 if (startcol)
8768 coladvance(getvcol_nolist(&Insstart));
8769 if (old_topline != curwin->w_topline
8770#ifdef FEAT_DIFF
8771 || old_topfill != curwin->w_topfill
8772#endif
8773 )
8774 redraw_later(VALID);
8775 start_arrow(&tpos);
8776#ifdef FEAT_CINDENT
8777 can_cindent = TRUE;
8778#endif
8779 }
8780 else
8781 vim_beep();
8782}
8783
8784 static void
8785ins_pageup()
8786{
8787 pos_T tpos;
8788
8789 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008790
8791#ifdef FEAT_WINDOWS
8792 if (mod_mask & MOD_MASK_CTRL)
8793 {
8794 /* <C-PageUp>: tab page back */
8795 goto_tabpage(-1);
8796 return;
8797 }
8798#endif
8799
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 tpos = curwin->w_cursor;
8801 if (onepage(BACKWARD, 1L) == OK)
8802 {
8803 start_arrow(&tpos);
8804#ifdef FEAT_CINDENT
8805 can_cindent = TRUE;
8806#endif
8807 }
8808 else
8809 vim_beep();
8810}
8811
8812 static void
8813ins_down(startcol)
8814 int startcol; /* when TRUE move to Insstart.col */
8815{
8816 pos_T tpos;
8817 linenr_T old_topline = curwin->w_topline;
8818#ifdef FEAT_DIFF
8819 int old_topfill = curwin->w_topfill;
8820#endif
8821
8822 undisplay_dollar();
8823 tpos = curwin->w_cursor;
8824 if (cursor_down(1L, TRUE) == OK)
8825 {
8826 if (startcol)
8827 coladvance(getvcol_nolist(&Insstart));
8828 if (old_topline != curwin->w_topline
8829#ifdef FEAT_DIFF
8830 || old_topfill != curwin->w_topfill
8831#endif
8832 )
8833 redraw_later(VALID);
8834 start_arrow(&tpos);
8835#ifdef FEAT_CINDENT
8836 can_cindent = TRUE;
8837#endif
8838 }
8839 else
8840 vim_beep();
8841}
8842
8843 static void
8844ins_pagedown()
8845{
8846 pos_T tpos;
8847
8848 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008849
8850#ifdef FEAT_WINDOWS
8851 if (mod_mask & MOD_MASK_CTRL)
8852 {
8853 /* <C-PageDown>: tab page forward */
8854 goto_tabpage(0);
8855 return;
8856 }
8857#endif
8858
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 tpos = curwin->w_cursor;
8860 if (onepage(FORWARD, 1L) == OK)
8861 {
8862 start_arrow(&tpos);
8863#ifdef FEAT_CINDENT
8864 can_cindent = TRUE;
8865#endif
8866 }
8867 else
8868 vim_beep();
8869}
8870
8871#ifdef FEAT_DND
8872 static void
8873ins_drop()
8874{
8875 do_put('~', BACKWARD, 1L, PUT_CURSEND);
8876}
8877#endif
8878
8879/*
8880 * Handle TAB in Insert or Replace mode.
8881 * Return TRUE when the TAB needs to be inserted like a normal character.
8882 */
8883 static int
8884ins_tab()
8885{
8886 int ind;
8887 int i;
8888 int temp;
8889
8890 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
8891 Insstart_blank_vcol = get_nolist_virtcol();
8892 if (echeck_abbr(TAB + ABBR_OFF))
8893 return FALSE;
8894
8895 ind = inindent(0);
8896#ifdef FEAT_CINDENT
8897 if (ind)
8898 can_cindent = FALSE;
8899#endif
8900
8901 /*
8902 * When nothing special, insert TAB like a normal character
8903 */
8904 if (!curbuf->b_p_et
8905 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
8906 && curbuf->b_p_sts == 0)
8907 return TRUE;
8908
8909 if (stop_arrow() == FAIL)
8910 return TRUE;
8911
8912 did_ai = FALSE;
8913#ifdef FEAT_SMARTINDENT
8914 did_si = FALSE;
8915 can_si = FALSE;
8916 can_si_back = FALSE;
8917#endif
8918 AppendToRedobuff((char_u *)"\t");
8919
8920 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
8921 temp = (int)curbuf->b_p_sw;
8922 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
8923 temp = (int)curbuf->b_p_sts;
8924 else /* otherwise use 'tabstop' */
8925 temp = (int)curbuf->b_p_ts;
8926 temp -= get_nolist_virtcol() % temp;
8927
8928 /*
8929 * Insert the first space with ins_char(). It will delete one char in
8930 * replace mode. Insert the rest with ins_str(); it will not delete any
8931 * chars. For VREPLACE mode, we use ins_char() for all characters.
8932 */
8933 ins_char(' ');
8934 while (--temp > 0)
8935 {
8936#ifdef FEAT_VREPLACE
8937 if (State & VREPLACE_FLAG)
8938 ins_char(' ');
8939 else
8940#endif
8941 {
8942 ins_str((char_u *)" ");
8943 if (State & REPLACE_FLAG) /* no char replaced */
8944 replace_push(NUL);
8945 }
8946 }
8947
8948 /*
8949 * When 'expandtab' not set: Replace spaces by TABs where possible.
8950 */
8951 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
8952 {
8953 char_u *ptr;
8954#ifdef FEAT_VREPLACE
8955 char_u *saved_line = NULL; /* init for GCC */
8956 pos_T pos;
8957#endif
8958 pos_T fpos;
8959 pos_T *cursor;
8960 colnr_T want_vcol, vcol;
8961 int change_col = -1;
8962 int save_list = curwin->w_p_list;
8963
8964 /*
8965 * Get the current line. For VREPLACE mode, don't make real changes
8966 * yet, just work on a copy of the line.
8967 */
8968#ifdef FEAT_VREPLACE
8969 if (State & VREPLACE_FLAG)
8970 {
8971 pos = curwin->w_cursor;
8972 cursor = &pos;
8973 saved_line = vim_strsave(ml_get_curline());
8974 if (saved_line == NULL)
8975 return FALSE;
8976 ptr = saved_line + pos.col;
8977 }
8978 else
8979#endif
8980 {
8981 ptr = ml_get_cursor();
8982 cursor = &curwin->w_cursor;
8983 }
8984
8985 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
8986 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8987 curwin->w_p_list = FALSE;
8988
8989 /* Find first white before the cursor */
8990 fpos = curwin->w_cursor;
8991 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
8992 {
8993 --fpos.col;
8994 --ptr;
8995 }
8996
8997 /* In Replace mode, don't change characters before the insert point. */
8998 if ((State & REPLACE_FLAG)
8999 && fpos.lnum == Insstart.lnum
9000 && fpos.col < Insstart.col)
9001 {
9002 ptr += Insstart.col - fpos.col;
9003 fpos.col = Insstart.col;
9004 }
9005
9006 /* compute virtual column numbers of first white and cursor */
9007 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9008 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9009
9010 /* Use as many TABs as possible. Beware of 'showbreak' and
9011 * 'linebreak' adding extra virtual columns. */
9012 while (vim_iswhite(*ptr))
9013 {
9014 i = lbr_chartabsize((char_u *)"\t", vcol);
9015 if (vcol + i > want_vcol)
9016 break;
9017 if (*ptr != TAB)
9018 {
9019 *ptr = TAB;
9020 if (change_col < 0)
9021 {
9022 change_col = fpos.col; /* Column of first change */
9023 /* May have to adjust Insstart */
9024 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9025 Insstart.col = fpos.col;
9026 }
9027 }
9028 ++fpos.col;
9029 ++ptr;
9030 vcol += i;
9031 }
9032
9033 if (change_col >= 0)
9034 {
9035 int repl_off = 0;
9036
9037 /* Skip over the spaces we need. */
9038 while (vcol < want_vcol && *ptr == ' ')
9039 {
9040 vcol += lbr_chartabsize(ptr, vcol);
9041 ++ptr;
9042 ++repl_off;
9043 }
9044 if (vcol > want_vcol)
9045 {
9046 /* Must have a char with 'showbreak' just before it. */
9047 --ptr;
9048 --repl_off;
9049 }
9050 fpos.col += repl_off;
9051
9052 /* Delete following spaces. */
9053 i = cursor->col - fpos.col;
9054 if (i > 0)
9055 {
9056 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
9057 /* correct replace stack. */
9058 if ((State & REPLACE_FLAG)
9059#ifdef FEAT_VREPLACE
9060 && !(State & VREPLACE_FLAG)
9061#endif
9062 )
9063 for (temp = i; --temp >= 0; )
9064 replace_join(repl_off);
9065 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009066#ifdef FEAT_NETBEANS_INTG
9067 if (usingNetbeans)
9068 {
9069 netbeans_removed(curbuf, fpos.lnum, cursor->col,
9070 (long)(i + 1));
9071 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9072 (char_u *)"\t", 1);
9073 }
9074#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075 cursor->col -= i;
9076
9077#ifdef FEAT_VREPLACE
9078 /*
9079 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9080 * backspacing over the changed spacing and then inserting the new
9081 * spacing.
9082 */
9083 if (State & VREPLACE_FLAG)
9084 {
9085 /* Backspace from real cursor to change_col */
9086 backspace_until_column(change_col);
9087
9088 /* Insert each char in saved_line from changed_col to
9089 * ptr-cursor */
9090 ins_bytes_len(saved_line + change_col,
9091 cursor->col - change_col);
9092 }
9093#endif
9094 }
9095
9096#ifdef FEAT_VREPLACE
9097 if (State & VREPLACE_FLAG)
9098 vim_free(saved_line);
9099#endif
9100 curwin->w_p_list = save_list;
9101 }
9102
9103 return FALSE;
9104}
9105
9106/*
9107 * Handle CR or NL in insert mode.
9108 * Return TRUE when out of memory or can't undo.
9109 */
9110 static int
9111ins_eol(c)
9112 int c;
9113{
9114 int i;
9115
9116 if (echeck_abbr(c + ABBR_OFF))
9117 return FALSE;
9118 if (stop_arrow() == FAIL)
9119 return TRUE;
9120 undisplay_dollar();
9121
9122 /*
9123 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9124 * character under the cursor. Only push a NUL on the replace stack,
9125 * nothing to put back when the NL is deleted.
9126 */
9127 if ((State & REPLACE_FLAG)
9128#ifdef FEAT_VREPLACE
9129 && !(State & VREPLACE_FLAG)
9130#endif
9131 )
9132 replace_push(NUL);
9133
9134 /*
9135 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9136 * replacing the next line, so we push all of the characters left on the
9137 * line onto the replace stack. This is not done here though, it is done
9138 * in open_line().
9139 */
9140
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009141#ifdef FEAT_VIRTUALEDIT
9142 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9143 * CTRL-O). */
9144 if (virtual_active() && curwin->w_cursor.coladd > 0)
9145 coladvance(getviscol());
9146#endif
9147
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148#ifdef FEAT_RIGHTLEFT
9149# ifdef FEAT_FKMAP
9150 if (p_altkeymap && p_fkmap)
9151 fkmap(NL);
9152# endif
9153 /* NL in reverse insert will always start in the end of
9154 * current line. */
9155 if (revins_on)
9156 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9157#endif
9158
9159 AppendToRedobuff(NL_STR);
9160 i = open_line(FORWARD,
9161#ifdef FEAT_COMMENTS
9162 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
9163#endif
9164 0, old_indent);
9165 old_indent = 0;
9166#ifdef FEAT_CINDENT
9167 can_cindent = TRUE;
9168#endif
9169
9170 return (!i);
9171}
9172
9173#ifdef FEAT_DIGRAPHS
9174/*
9175 * Handle digraph in insert mode.
9176 * Returns character still to be inserted, or NUL when nothing remaining to be
9177 * done.
9178 */
9179 static int
9180ins_digraph()
9181{
9182 int c;
9183 int cc;
9184
9185 pc_status = PC_STATUS_UNSET;
9186 if (redrawing() && !char_avail())
9187 {
9188 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009189 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190
9191 edit_putchar('?', TRUE);
9192#ifdef FEAT_CMDL_INFO
9193 add_to_showcmd_c(Ctrl_K);
9194#endif
9195 }
9196
9197#ifdef USE_ON_FLY_SCROLL
9198 dont_scroll = TRUE; /* disallow scrolling here */
9199#endif
9200
9201 /* don't map the digraph chars. This also prevents the
9202 * mode message to be deleted when ESC is hit */
9203 ++no_mapping;
9204 ++allow_keys;
9205 c = safe_vgetc();
9206 --no_mapping;
9207 --allow_keys;
9208 if (IS_SPECIAL(c) || mod_mask) /* special key */
9209 {
9210#ifdef FEAT_CMDL_INFO
9211 clear_showcmd();
9212#endif
9213 insert_special(c, TRUE, FALSE);
9214 return NUL;
9215 }
9216 if (c != ESC)
9217 {
9218 if (redrawing() && !char_avail())
9219 {
9220 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009221 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222
9223 if (char2cells(c) == 1)
9224 {
9225 /* first remove the '?', otherwise it's restored when typing
9226 * an ESC next */
9227 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00009228 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229 edit_putchar(c, TRUE);
9230 }
9231#ifdef FEAT_CMDL_INFO
9232 add_to_showcmd_c(c);
9233#endif
9234 }
9235 ++no_mapping;
9236 ++allow_keys;
9237 cc = safe_vgetc();
9238 --no_mapping;
9239 --allow_keys;
9240 if (cc != ESC)
9241 {
9242 AppendToRedobuff((char_u *)CTRL_V_STR);
9243 c = getdigraph(c, cc, TRUE);
9244#ifdef FEAT_CMDL_INFO
9245 clear_showcmd();
9246#endif
9247 return c;
9248 }
9249 }
9250 edit_unputchar();
9251#ifdef FEAT_CMDL_INFO
9252 clear_showcmd();
9253#endif
9254 return NUL;
9255}
9256#endif
9257
9258/*
9259 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
9260 * Returns the char to be inserted, or NUL if none found.
9261 */
9262 static int
9263ins_copychar(lnum)
9264 linenr_T lnum;
9265{
9266 int c;
9267 int temp;
9268 char_u *ptr, *prev_ptr;
9269
9270 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9271 {
9272 vim_beep();
9273 return NUL;
9274 }
9275
9276 /* try to advance to the cursor column */
9277 temp = 0;
9278 ptr = ml_get(lnum);
9279 prev_ptr = ptr;
9280 validate_virtcol();
9281 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
9282 {
9283 prev_ptr = ptr;
9284 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
9285 }
9286 if ((colnr_T)temp > curwin->w_virtcol)
9287 ptr = prev_ptr;
9288
9289#ifdef FEAT_MBYTE
9290 c = (*mb_ptr2char)(ptr);
9291#else
9292 c = *ptr;
9293#endif
9294 if (c == NUL)
9295 vim_beep();
9296 return c;
9297}
9298
Bram Moolenaar4be06f92005-07-29 22:36:03 +00009299/*
9300 * CTRL-Y or CTRL-E typed in Insert mode.
9301 */
9302 static int
9303ins_ctrl_ey(tc)
9304 int tc;
9305{
9306 int c = tc;
9307
9308#ifdef FEAT_INS_EXPAND
9309 if (ctrl_x_mode == CTRL_X_SCROLL)
9310 {
9311 if (c == Ctrl_Y)
9312 scrolldown_clamp();
9313 else
9314 scrollup_clamp();
9315 redraw_later(VALID);
9316 }
9317 else
9318#endif
9319 {
9320 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
9321 if (c != NUL)
9322 {
9323 long tw_save;
9324
9325 /* The character must be taken literally, insert like it
9326 * was typed after a CTRL-V, and pretend 'textwidth'
9327 * wasn't set. Digits, 'o' and 'x' are special after a
9328 * CTRL-V, don't use it for these. */
9329 if (c < 256 && !isalnum(c))
9330 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
9331 tw_save = curbuf->b_p_tw;
9332 curbuf->b_p_tw = -1;
9333 insert_special(c, TRUE, FALSE);
9334 curbuf->b_p_tw = tw_save;
9335#ifdef FEAT_RIGHTLEFT
9336 revins_chars++;
9337 revins_legal++;
9338#endif
9339 c = Ctrl_V; /* pretend CTRL-V is last character */
9340 auto_format(FALSE, TRUE);
9341 }
9342 }
9343 return c;
9344}
9345
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346#ifdef FEAT_SMARTINDENT
9347/*
9348 * Try to do some very smart auto-indenting.
9349 * Used when inserting a "normal" character.
9350 */
9351 static void
9352ins_try_si(c)
9353 int c;
9354{
9355 pos_T *pos, old_pos;
9356 char_u *ptr;
9357 int i;
9358 int temp;
9359
9360 /*
9361 * do some very smart indenting when entering '{' or '}'
9362 */
9363 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
9364 {
9365 /*
9366 * for '}' set indent equal to indent of line containing matching '{'
9367 */
9368 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
9369 {
9370 old_pos = curwin->w_cursor;
9371 /*
9372 * If the matching '{' has a ')' immediately before it (ignoring
9373 * white-space), then line up with the start of the line
9374 * containing the matching '(' if there is one. This handles the
9375 * case where an "if (..\n..) {" statement continues over multiple
9376 * lines -- webb
9377 */
9378 ptr = ml_get(pos->lnum);
9379 i = pos->col;
9380 if (i > 0) /* skip blanks before '{' */
9381 while (--i > 0 && vim_iswhite(ptr[i]))
9382 ;
9383 curwin->w_cursor.lnum = pos->lnum;
9384 curwin->w_cursor.col = i;
9385 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
9386 curwin->w_cursor = *pos;
9387 i = get_indent();
9388 curwin->w_cursor = old_pos;
9389#ifdef FEAT_VREPLACE
9390 if (State & VREPLACE_FLAG)
9391 change_indent(INDENT_SET, i, FALSE, NUL);
9392 else
9393#endif
9394 (void)set_indent(i, SIN_CHANGED);
9395 }
9396 else if (curwin->w_cursor.col > 0)
9397 {
9398 /*
9399 * when inserting '{' after "O" reduce indent, but not
9400 * more than indent of previous line
9401 */
9402 temp = TRUE;
9403 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
9404 {
9405 old_pos = curwin->w_cursor;
9406 i = get_indent();
9407 while (curwin->w_cursor.lnum > 1)
9408 {
9409 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
9410
9411 /* ignore empty lines and lines starting with '#'. */
9412 if (*ptr != '#' && *ptr != NUL)
9413 break;
9414 }
9415 if (get_indent() >= i)
9416 temp = FALSE;
9417 curwin->w_cursor = old_pos;
9418 }
9419 if (temp)
9420 shift_line(TRUE, FALSE, 1);
9421 }
9422 }
9423
9424 /*
9425 * set indent of '#' always to 0
9426 */
9427 if (curwin->w_cursor.col > 0 && can_si && c == '#')
9428 {
9429 /* remember current indent for next line */
9430 old_indent = get_indent();
9431 (void)set_indent(0, SIN_CHANGED);
9432 }
9433
9434 /* Adjust ai_col, the char at this position can be deleted. */
9435 if (ai_col > curwin->w_cursor.col)
9436 ai_col = curwin->w_cursor.col;
9437}
9438#endif
9439
9440/*
9441 * Get the value that w_virtcol would have when 'list' is off.
9442 * Unless 'cpo' contains the 'L' flag.
9443 */
9444 static colnr_T
9445get_nolist_virtcol()
9446{
9447 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9448 return getvcol_nolist(&curwin->w_cursor);
9449 validate_virtcol();
9450 return curwin->w_virtcol;
9451}