blob: 08a24d7e77cb7b0201a56044f5416d855cb7e7f6 [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 Moolenaar4be06f92005-07-29 22:36:03 +0000108/* When the first completion is done "compl_started" is set. When it's
109 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000110static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000111
Bram Moolenaar572cb562005-08-05 21:35:02 +0000112static int compl_matches = 0;
113static char_u *compl_pattern = NULL;
114static int compl_direction = FORWARD;
115static int compl_shows_dir = FORWARD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000116static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000117static pos_T compl_startpos;
118static colnr_T compl_col = 0; /* column where the text starts
119 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000120static char_u *compl_orig_text = NULL; /* text as it was before
121 * completion started */
122static int compl_cont_mode = 0;
123static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000125static void ins_ctrl_x __ARGS((void));
126static int has_compl_option __ARGS((int dict_opt));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000127static int ins_compl_equal __ARGS((compl_T *match, char_u *str, int len));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000128static void ins_compl_longest_match __ARGS((compl_T *match));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000129static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches, int icase));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130static int ins_compl_make_cyclic __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000131static void ins_compl_upd_pum __ARGS((void));
132static void ins_compl_del_pum __ARGS((void));
Bram Moolenaar280f1262006-01-30 00:14:18 +0000133static int pum_wanted __ARGS((void));
Bram Moolenaar65c923a2006-03-03 22:56:30 +0000134static int pum_enough_matches __ARGS((void));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000135static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int flags, int thesaurus));
Bram Moolenaar0b238792006-03-02 22:49:12 +0000136static 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 +0000137static char_u *find_line_end __ARGS((char_u *ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138static void ins_compl_free __ARGS((void));
139static void ins_compl_clear __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000140static int ins_compl_bs __ARGS((void));
141static void ins_compl_addleader __ARGS((int c));
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000142static void ins_compl_set_original_text __ARGS((char_u *str));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000143static void ins_compl_addfrommatch __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000144static int ins_compl_prep __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
Bram Moolenaara94bc432006-03-10 21:42:59 +0000146#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
147static void ins_compl_add_list __ARGS((list_T *list));
148#endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000149static int ins_compl_get_exp __ARGS((pos_T *ini));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150static void ins_compl_delete __ARGS((void));
151static void ins_compl_insert __ARGS((void));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000152static int ins_compl_next __ARGS((int allow_get_expansion, int count, int insert_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000153static int ins_compl_key2dir __ARGS((int c));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000154static int ins_compl_pum_key __ARGS((int c));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000155static int ins_compl_key2count __ARGS((int c));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000156static int ins_compl_use_match __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157static int ins_complete __ARGS((int c));
158static int quote_meta __ARGS((char_u *dest, char_u *str, int len));
159#endif /* FEAT_INS_EXPAND */
160
161#define BACKSPACE_CHAR 1
162#define BACKSPACE_WORD 2
163#define BACKSPACE_WORD_NOT_SPACE 3
164#define BACKSPACE_LINE 4
165
Bram Moolenaar754b5602006-02-09 23:53:20 +0000166static void ins_redraw __ARGS((int ready));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167static void ins_ctrl_v __ARGS((void));
168static void undisplay_dollar __ARGS((void));
169static void insert_special __ARGS((int, int, int));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000170static void internal_format __ARGS((int textwidth, int second_indent, int flags, int format_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171static void check_auto_format __ARGS((int));
172static void redo_literal __ARGS((int c));
173static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000174#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +0000175static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000176static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000177static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
180static int echeck_abbr __ARGS((int));
181static void replace_push_off __ARGS((int c));
182static int replace_pop __ARGS((void));
183static void replace_join __ARGS((int off));
184static void replace_pop_ins __ARGS((void));
185#ifdef FEAT_MBYTE
186static void mb_replace_pop_ins __ARGS((int cc));
187#endif
188static void replace_flush __ARGS((void));
189static void replace_do_bs __ARGS((void));
190#ifdef FEAT_CINDENT
191static int cindent_on __ARGS((void));
192#endif
193static void ins_reg __ARGS((void));
194static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000195static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000196static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197#ifdef FEAT_RIGHTLEFT
198static void ins_ctrl_ __ARGS((void));
199#endif
200#ifdef FEAT_VISUAL
201static int ins_start_select __ARGS((int c));
202#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000203static void ins_insert __ARGS((int replaceState));
204static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205static void ins_shift __ARGS((int c, int lastc));
206static void ins_del __ARGS((void));
207static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
208#ifdef FEAT_MOUSE
209static void ins_mouse __ARGS((int c));
210static void ins_mousescroll __ARGS((int up));
211#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000212#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
213static void ins_tabline __ARGS((int c));
214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215static void ins_left __ARGS((void));
216static void ins_home __ARGS((int c));
217static void ins_end __ARGS((int c));
218static void ins_s_left __ARGS((void));
219static void ins_right __ARGS((void));
220static void ins_s_right __ARGS((void));
221static void ins_up __ARGS((int startcol));
222static void ins_pageup __ARGS((void));
223static void ins_down __ARGS((int startcol));
224static void ins_pagedown __ARGS((void));
225#ifdef FEAT_DND
226static void ins_drop __ARGS((void));
227#endif
228static int ins_tab __ARGS((void));
229static int ins_eol __ARGS((int c));
230#ifdef FEAT_DIGRAPHS
231static int ins_digraph __ARGS((void));
232#endif
233static int ins_copychar __ARGS((linenr_T lnum));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000234static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235#ifdef FEAT_SMARTINDENT
236static void ins_try_si __ARGS((int c));
237#endif
238static colnr_T get_nolist_virtcol __ARGS((void));
239
240static colnr_T Insstart_textlen; /* length of line when insert started */
241static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
242
243static char_u *last_insert = NULL; /* the text of the previous insert,
244 K_SPECIAL and CSI are escaped */
245static int last_insert_skip; /* nr of chars in front of previous insert */
246static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000247static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248
249#ifdef FEAT_CINDENT
250static int can_cindent; /* may do cindenting on this line */
251#endif
252
253static int old_indent = 0; /* for ^^D command in insert mode */
254
255#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000256static int revins_on; /* reverse insert mode on */
257static int revins_chars; /* how much to skip after edit */
258static int revins_legal; /* was the last char 'legal'? */
259static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260#endif
261
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262static int ins_need_undo; /* call u_save() before inserting a
263 char. Set when edit() is called.
264 after that arrow_used is used. */
265
266static int did_add_space = FALSE; /* auto_format() added an extra space
267 under the cursor */
268
269/*
270 * edit(): Start inserting text.
271 *
272 * "cmdchar" can be:
273 * 'i' normal insert command
274 * 'a' normal append command
275 * 'R' replace command
276 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
277 * but still only one <CR> is inserted. The <Esc> is not used for redo.
278 * 'g' "gI" command.
279 * 'V' "gR" command for Virtual Replace mode.
280 * 'v' "gr" command for single character Virtual Replace mode.
281 *
282 * This function is not called recursively. For CTRL-O commands, it returns
283 * and lets the caller handle the Normal-mode command.
284 *
285 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
286 */
287 int
288edit(cmdchar, startln, count)
289 int cmdchar;
290 int startln; /* if set, insert at start of line */
291 long count;
292{
293 int c = 0;
294 char_u *ptr;
295 int lastc;
296 colnr_T mincol;
297 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 int i;
299 int did_backspace = TRUE; /* previous char was backspace */
300#ifdef FEAT_CINDENT
301 int line_is_white = FALSE; /* line is empty before insert */
302#endif
303 linenr_T old_topline = 0; /* topline before insertion */
304#ifdef FEAT_DIFF
305 int old_topfill = -1;
306#endif
307 int inserted_space = FALSE; /* just inserted a space */
308 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000309 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000311 /* Remember whether editing was restarted after CTRL-O. */
312 did_restart_edit = restart_edit;
313
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 /* sleep before redrawing, needed for "CTRL-O :" that results in an
315 * error message */
316 check_for_delay(TRUE);
317
318#ifdef HAVE_SANDBOX
319 /* Don't allow inserting in the sandbox. */
320 if (sandbox != 0)
321 {
322 EMSG(_(e_sandbox));
323 return FALSE;
324 }
325#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000326 /* Don't allow changes in the buffer while editing the cmdline. The
327 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000328 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000329 {
330 EMSG(_(e_secure));
331 return FALSE;
332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333
334#ifdef FEAT_INS_EXPAND
335 ins_compl_clear(); /* clear stuff for CTRL-X mode */
336#endif
337
Bram Moolenaar843ee412004-06-30 16:16:41 +0000338#ifdef FEAT_AUTOCMD
339 /*
340 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
341 */
342 if (cmdchar != 'r' && cmdchar != 'v')
343 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000344# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000345 if (cmdchar == 'R')
346 ptr = (char_u *)"r";
347 else if (cmdchar == 'V')
348 ptr = (char_u *)"v";
349 else
350 ptr = (char_u *)"i";
351 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +0000352# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000353 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
354 }
355#endif
356
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357#ifdef FEAT_MOUSE
358 /*
359 * When doing a paste with the middle mouse button, Insstart is set to
360 * where the paste started.
361 */
362 if (where_paste_started.lnum != 0)
363 Insstart = where_paste_started;
364 else
365#endif
366 {
367 Insstart = curwin->w_cursor;
368 if (startln)
369 Insstart.col = 0;
370 }
371 Insstart_textlen = linetabsize(ml_get_curline());
372 Insstart_blank_vcol = MAXCOL;
373 if (!did_ai)
374 ai_col = 0;
375
376 if (cmdchar != NUL && restart_edit == 0)
377 {
378 ResetRedobuff();
379 AppendNumberToRedobuff(count);
380#ifdef FEAT_VREPLACE
381 if (cmdchar == 'V' || cmdchar == 'v')
382 {
383 /* "gR" or "gr" command */
384 AppendCharToRedobuff('g');
385 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
386 }
387 else
388#endif
389 {
390 AppendCharToRedobuff(cmdchar);
391 if (cmdchar == 'g') /* "gI" command */
392 AppendCharToRedobuff('I');
393 else if (cmdchar == 'r') /* "r<CR>" command */
394 count = 1; /* insert only one <CR> */
395 }
396 }
397
398 if (cmdchar == 'R')
399 {
400#ifdef FEAT_FKMAP
401 if (p_fkmap && p_ri)
402 {
403 beep_flush();
404 EMSG(farsi_text_3); /* encoded in Farsi */
405 State = INSERT;
406 }
407 else
408#endif
409 State = REPLACE;
410 }
411#ifdef FEAT_VREPLACE
412 else if (cmdchar == 'V' || cmdchar == 'v')
413 {
414 State = VREPLACE;
415 replaceState = VREPLACE;
416 orig_line_count = curbuf->b_ml.ml_line_count;
417 vr_lines_changed = 1;
418 }
419#endif
420 else
421 State = INSERT;
422
423 stop_insert_mode = FALSE;
424
425 /*
426 * Need to recompute the cursor position, it might move when the cursor is
427 * on a TAB or special character.
428 */
429 curs_columns(TRUE);
430
431 /*
432 * Enable langmap or IME, indicated by 'iminsert'.
433 * Note that IME may enabled/disabled without us noticing here, thus the
434 * 'iminsert' value may not reflect what is actually used. It is updated
435 * when hitting <Esc>.
436 */
437 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
438 State |= LANGMAP;
439#ifdef USE_IM_CONTROL
440 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
441#endif
442
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443#ifdef FEAT_MOUSE
444 setmouse();
445#endif
446#ifdef FEAT_CMDL_INFO
447 clear_showcmd();
448#endif
449#ifdef FEAT_RIGHTLEFT
450 /* there is no reverse replace mode */
451 revins_on = (State == INSERT && p_ri);
452 if (revins_on)
453 undisplay_dollar();
454 revins_chars = 0;
455 revins_legal = 0;
456 revins_scol = -1;
457#endif
458
459 /*
460 * Handle restarting Insert mode.
461 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
462 * restart_edit non-zero, and something in the stuff buffer.
463 */
464 if (restart_edit != 0 && stuff_empty())
465 {
466#ifdef FEAT_MOUSE
467 /*
468 * After a paste we consider text typed to be part of the insert for
469 * the pasted text. You can backspace over the pasted text too.
470 */
471 if (where_paste_started.lnum)
472 arrow_used = FALSE;
473 else
474#endif
475 arrow_used = TRUE;
476 restart_edit = 0;
477
478 /*
479 * If the cursor was after the end-of-line before the CTRL-O and it is
480 * now at the end-of-line, put it after the end-of-line (this is not
481 * correct in very rare cases).
482 * Also do this if curswant is greater than the current virtual
483 * column. Eg after "^O$" or "^O80|".
484 */
485 validate_virtcol();
486 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000487 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 || curwin->w_curswant > curwin->w_virtcol)
489 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
490 {
491 if (ptr[1] == NUL)
492 ++curwin->w_cursor.col;
493#ifdef FEAT_MBYTE
494 else if (has_mbyte)
495 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000496 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 if (ptr[i] == NUL)
498 curwin->w_cursor.col += i;
499 }
500#endif
501 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000502 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 }
504 else
505 arrow_used = FALSE;
506
507 /* we are in insert mode now, don't need to start it anymore */
508 need_start_insertmode = FALSE;
509
510 /* Need to save the line for undo before inserting the first char. */
511 ins_need_undo = TRUE;
512
513#ifdef FEAT_MOUSE
514 where_paste_started.lnum = 0;
515#endif
516#ifdef FEAT_CINDENT
517 can_cindent = TRUE;
518#endif
519#ifdef FEAT_FOLDING
520 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
521 * restarting. */
522 if (!p_im && did_restart_edit == 0)
523 foldOpenCursor();
524#endif
525
526 /*
527 * If 'showmode' is set, show the current (insert/replace/..) mode.
528 * A warning message for changing a readonly file is given here, before
529 * actually changing anything. It's put after the mode, if any.
530 */
531 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000532 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 i = showmode();
534
535 if (!p_im && did_restart_edit == 0)
536 change_warning(i + 1);
537
538#ifdef CURSOR_SHAPE
539 ui_cursor_shape(); /* may show different cursor shape */
540#endif
541#ifdef FEAT_DIGRAPHS
542 do_digraph(-1); /* clear digraphs */
543#endif
544
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000545 /*
546 * Get the current length of the redo buffer, those characters have to be
547 * skipped if we want to get to the inserted characters.
548 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 ptr = get_inserted();
550 if (ptr == NULL)
551 new_insert_skip = 0;
552 else
553 {
554 new_insert_skip = (int)STRLEN(ptr);
555 vim_free(ptr);
556 }
557
558 old_indent = 0;
559
560 /*
561 * Main loop in Insert mode: repeat until Insert mode is left.
562 */
563 for (;;)
564 {
565#ifdef FEAT_RIGHTLEFT
566 if (!revins_legal)
567 revins_scol = -1; /* reset on illegal motions */
568 else
569 revins_legal = 0;
570#endif
571 if (arrow_used) /* don't repeat insert when arrow key used */
572 count = 0;
573
574 if (stop_insert_mode)
575 {
576 /* ":stopinsert" used or 'insertmode' reset */
577 count = 0;
578 goto doESCkey;
579 }
580
581 /* set curwin->w_curswant for next K_DOWN or K_UP */
582 if (!arrow_used)
583 curwin->w_set_curswant = TRUE;
584
585 /* If there is no typeahead may check for timestamps (e.g., for when a
586 * menu invoked a shell command). */
587 if (stuff_empty())
588 {
589 did_check_timestamps = FALSE;
590 if (need_check_timestamps)
591 check_timestamps(FALSE);
592 }
593
594 /*
595 * When emsg() was called msg_scroll will have been set.
596 */
597 msg_scroll = FALSE;
598
599#ifdef FEAT_GUI
600 /* When 'mousefocus' is set a mouse movement may have taken us to
601 * another window. "need_mouse_correct" may then be set because of an
602 * autocommand. */
603 if (need_mouse_correct)
604 gui_mouse_correct();
605#endif
606
607#ifdef FEAT_FOLDING
608 /* Open fold at the cursor line, according to 'foldopen'. */
609 if (fdo_flags & FDO_INSERT)
610 foldOpenCursor();
611 /* Close folds where the cursor isn't, according to 'foldclose' */
612 if (!char_avail())
613 foldCheckClose();
614#endif
615
616 /*
617 * If we inserted a character at the last position of the last line in
618 * the window, scroll the window one line up. This avoids an extra
619 * redraw.
620 * This is detected when the cursor column is smaller after inserting
621 * something.
622 * Don't do this when the topline changed already, it has
623 * already been adjusted (by insertchar() calling open_line())).
624 */
625 if (curbuf->b_mod_set
626 && curwin->w_p_wrap
627 && !did_backspace
628 && curwin->w_topline == old_topline
629#ifdef FEAT_DIFF
630 && curwin->w_topfill == old_topfill
631#endif
632 )
633 {
634 mincol = curwin->w_wcol;
635 validate_cursor_col();
636
637 if ((int)curwin->w_wcol < (int)mincol - curbuf->b_p_ts
638 && curwin->w_wrow == W_WINROW(curwin)
639 + curwin->w_height - 1 - p_so
640 && (curwin->w_cursor.lnum != curwin->w_topline
641#ifdef FEAT_DIFF
642 || curwin->w_topfill > 0
643#endif
644 ))
645 {
646#ifdef FEAT_DIFF
647 if (curwin->w_topfill > 0)
648 --curwin->w_topfill;
649 else
650#endif
651#ifdef FEAT_FOLDING
652 if (hasFolding(curwin->w_topline, NULL, &old_topline))
653 set_topline(curwin, old_topline + 1);
654 else
655#endif
656 set_topline(curwin, curwin->w_topline + 1);
657 }
658 }
659
660 /* May need to adjust w_topline to show the cursor. */
661 update_topline();
662
663 did_backspace = FALSE;
664
665 validate_cursor(); /* may set must_redraw */
666
667 /*
668 * Redraw the display when no characters are waiting.
669 * Also shows mode, ruler and positions cursor.
670 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000671 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672
673#ifdef FEAT_SCROLLBIND
674 if (curwin->w_p_scb)
675 do_check_scrollbind(TRUE);
676#endif
677
678 update_curswant();
679 old_topline = curwin->w_topline;
680#ifdef FEAT_DIFF
681 old_topfill = curwin->w_topfill;
682#endif
683
684#ifdef USE_ON_FLY_SCROLL
685 dont_scroll = FALSE; /* allow scrolling here */
686#endif
687
688 /*
689 * Get a character for Insert mode.
690 */
691 lastc = c; /* remember previous char for CTRL-D */
692 c = safe_vgetc();
693
694#ifdef FEAT_RIGHTLEFT
695 if (p_hkmap && KeyTyped)
696 c = hkmap(c); /* Hebrew mode mapping */
697#endif
698#ifdef FEAT_FKMAP
699 if (p_fkmap && KeyTyped)
700 c = fkmap(c); /* Farsi mode mapping */
701#endif
702
703#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000704 /*
705 * Special handling of keys while the popup menu is visible or wanted
706 * and the cursor is still in the completed word.
707 */
708 if (compl_started && pum_wanted() && curwin->w_cursor.col >= compl_col)
Bram Moolenaara6557602006-02-04 22:43:20 +0000709 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000710 /* BS: Delete one character from "compl_leader". */
711 if ((c == K_BS || c == Ctrl_H)
712 && curwin->w_cursor.col > compl_col && ins_compl_bs())
Bram Moolenaara6557602006-02-04 22:43:20 +0000713 continue;
714
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000715 /* When no match was selected or it was edited. */
716 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000717 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000718 /* CTRL-L: Add one character from the current match to
719 * "compl_leader". */
720 if (c == Ctrl_L)
721 {
722 ins_compl_addfrommatch();
723 continue;
724 }
725
Bram Moolenaardf1bdc92006-02-23 21:32:16 +0000726 /* A printable, non-white character: Add to "compl_leader". */
727 if (vim_isprintc(c) && !vim_iswhite(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000728 {
729 ins_compl_addleader(c);
730 continue;
731 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000732
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000733 /* Pressing CTRL-Y selects the current match. Shen
734 * compl_enter_selects is set the Enter key does the same. */
735 if (c == Ctrl_Y || (compl_enter_selects
736 && (c == CAR || c == K_KENTER || c == NL)))
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000737 {
738 ins_compl_delete();
739 ins_compl_insert();
740 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000741 }
742 }
743
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
745 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000746 compl_get_longest = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +0000747 if (c != K_IGNORE && ins_compl_prep(c))
748 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749#endif
750
Bram Moolenaar488c6512005-08-11 20:09:58 +0000751 /* CTRL-\ CTRL-N goes to Normal mode,
752 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
753 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 if (c == Ctrl_BSL)
755 {
756 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000757 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 ++no_mapping;
759 ++allow_keys;
760 c = safe_vgetc();
761 --no_mapping;
762 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000763 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000765 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 vungetc(c);
767 c = Ctrl_BSL;
768 }
769 else if (c == Ctrl_G && p_im)
770 continue;
771 else
772 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000773 if (c == Ctrl_O)
774 {
775 ins_ctrl_o();
776 ins_at_eol = FALSE; /* cursor keeps its column */
777 nomove = TRUE;
778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 count = 0;
780 goto doESCkey;
781 }
782 }
783
784#ifdef FEAT_DIGRAPHS
785 c = do_digraph(c);
786#endif
787
788#ifdef FEAT_INS_EXPAND
789 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
790 goto docomplete;
791#endif
792 if (c == Ctrl_V || c == Ctrl_Q)
793 {
794 ins_ctrl_v();
795 c = Ctrl_V; /* pretend CTRL-V is last typed character */
796 continue;
797 }
798
799#ifdef FEAT_CINDENT
800 if (cindent_on()
801# ifdef FEAT_INS_EXPAND
802 && ctrl_x_mode == 0
803# endif
804 )
805 {
806 /* A key name preceded by a bang means this key is not to be
807 * inserted. Skip ahead to the re-indenting below.
808 * A key name preceded by a star means that indenting has to be
809 * done before inserting the key. */
810 line_is_white = inindent(0);
811 if (in_cinkeys(c, '!', line_is_white))
812 goto force_cindent;
813 if (can_cindent && in_cinkeys(c, '*', line_is_white)
814 && stop_arrow() == OK)
815 do_c_expr_indent();
816 }
817#endif
818
819#ifdef FEAT_RIGHTLEFT
820 if (curwin->w_p_rl)
821 switch (c)
822 {
823 case K_LEFT: c = K_RIGHT; break;
824 case K_S_LEFT: c = K_S_RIGHT; break;
825 case K_C_LEFT: c = K_C_RIGHT; break;
826 case K_RIGHT: c = K_LEFT; break;
827 case K_S_RIGHT: c = K_S_LEFT; break;
828 case K_C_RIGHT: c = K_C_LEFT; break;
829 }
830#endif
831
832#ifdef FEAT_VISUAL
833 /*
834 * If 'keymodel' contains "startsel", may start selection. If it
835 * does, a CTRL-O and c will be stuffed, we need to get these
836 * characters.
837 */
838 if (ins_start_select(c))
839 continue;
840#endif
841
842 /*
843 * The big switch to handle a character in insert mode.
844 */
845 switch (c)
846 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000847 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 if (echeck_abbr(ESC + ABBR_OFF))
849 break;
850 /*FALLTHROUGH*/
851
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000852 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853#ifdef FEAT_CMDWIN
854 if (c == Ctrl_C && cmdwin_type != 0)
855 {
856 /* Close the cmdline window. */
857 cmdwin_result = K_IGNORE;
858 got_int = FALSE; /* don't stop executing autocommands et al. */
859 goto doESCkey;
860 }
861#endif
862
863#ifdef UNIX
864do_intr:
865#endif
866 /* when 'insertmode' set, and not halfway a mapping, don't leave
867 * Insert mode */
868 if (goto_im())
869 {
870 if (got_int)
871 {
872 (void)vgetc(); /* flush all buffers */
873 got_int = FALSE;
874 }
875 else
876 vim_beep();
877 break;
878 }
879doESCkey:
880 /*
881 * This is the ONLY return from edit()!
882 */
883 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
884 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000885 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 o_lnum = curwin->w_cursor.lnum;
887
Bram Moolenaar488c6512005-08-11 20:09:58 +0000888 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000889 {
890#ifdef FEAT_AUTOCMD
891 if (cmdchar != 'r' && cmdchar != 'v')
892 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
893 FALSE, curbuf);
894#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 continue;
898
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000899 case Ctrl_Z: /* suspend when 'insertmode' set */
900 if (!p_im)
901 goto normalchar; /* insert CTRL-Z as normal char */
902 stuffReadbuff((char_u *)":st\r");
903 c = Ctrl_O;
904 /*FALLTHROUGH*/
905
906 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000907#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000908 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000909 goto docomplete;
910#endif
911 if (echeck_abbr(Ctrl_O + ABBR_OFF))
912 break;
913 ins_ctrl_o();
914 count = 0;
915 goto doESCkey;
916
Bram Moolenaar572cb562005-08-05 21:35:02 +0000917 case K_INS: /* toggle insert/replace mode */
918 case K_KINS:
919 ins_insert(replaceState);
920 break;
921
922 case K_SELECT: /* end of Select mode mapping - ignore */
923 break;
924
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000925#ifdef FEAT_SNIFF
926 case K_SNIFF: /* Sniff command received */
927 stuffcharReadbuff(K_SNIFF);
928 goto doESCkey;
929#endif
930
931 case K_HELP: /* Help key works like <ESC> <Help> */
932 case K_F1:
933 case K_XF1:
934 stuffcharReadbuff(K_HELP);
935 if (p_im)
936 need_start_insertmode = TRUE;
937 goto doESCkey;
938
939#ifdef FEAT_NETBEANS_INTG
940 case K_F21: /* NetBeans command */
941 ++no_mapping; /* don't map the next key hits */
942 i = safe_vgetc();
943 --no_mapping;
944 netbeans_keycommand(i);
945 break;
946#endif
947
948 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 case NUL:
950 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000951 /* For ^@ the trailing ESC will end the insert, unless there is an
952 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
954 && c != Ctrl_A && !p_im)
955 goto doESCkey; /* quit insert mode */
956 inserted_space = FALSE;
957 break;
958
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000959 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 ins_reg();
961 auto_format(FALSE, TRUE);
962 inserted_space = FALSE;
963 break;
964
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000965 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 ins_ctrl_g();
967 break;
968
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000969 case Ctrl_HAT: /* switch input mode and/or langmap */
970 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 break;
972
973#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000974 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 if (!p_ari)
976 goto normalchar;
977 ins_ctrl_();
978 break;
979#endif
980
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000981 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
983 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
984 goto docomplete;
985#endif
986 /* FALLTHROUGH */
987
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000988 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989# ifdef FEAT_INS_EXPAND
990 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
991 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000992 if (has_compl_option(FALSE))
993 goto docomplete;
994 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 }
996# endif
997 ins_shift(c, lastc);
998 auto_format(FALSE, TRUE);
999 inserted_space = FALSE;
1000 break;
1001
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001002 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 case K_KDEL:
1004 ins_del();
1005 auto_format(FALSE, TRUE);
1006 break;
1007
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001008 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 case Ctrl_H:
1010 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1011 auto_format(FALSE, TRUE);
1012 break;
1013
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001014 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1016 auto_format(FALSE, TRUE);
1017 break;
1018
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001019 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001020# ifdef FEAT_COMPL_FUNC
1021 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001022 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001023 goto docomplete;
1024# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1026 auto_format(FALSE, TRUE);
1027 inserted_space = FALSE;
1028 break;
1029
1030#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001031 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 case K_LEFTMOUSE_NM:
1033 case K_LEFTDRAG:
1034 case K_LEFTRELEASE:
1035 case K_LEFTRELEASE_NM:
1036 case K_MIDDLEMOUSE:
1037 case K_MIDDLEDRAG:
1038 case K_MIDDLERELEASE:
1039 case K_RIGHTMOUSE:
1040 case K_RIGHTDRAG:
1041 case K_RIGHTRELEASE:
1042 case K_X1MOUSE:
1043 case K_X1DRAG:
1044 case K_X1RELEASE:
1045 case K_X2MOUSE:
1046 case K_X2DRAG:
1047 case K_X2RELEASE:
1048 ins_mouse(c);
1049 break;
1050
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001051 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 ins_mousescroll(FALSE);
1053 break;
1054
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001055 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 ins_mousescroll(TRUE);
1057 break;
1058#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001059#ifdef FEAT_GUI_TABLINE
1060 case K_TABLINE:
1061 case K_TABMENU:
1062 ins_tabline(c);
1063 break;
1064#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001066 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 break;
1068
Bram Moolenaar754b5602006-02-09 23:53:20 +00001069#ifdef FEAT_AUTOCMD
1070 case K_CURSORHOLD: /* Didn't type something for a while. */
1071 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1072 did_cursorhold = TRUE;
1073 break;
1074#endif
1075
Bram Moolenaar4770d092006-01-12 23:22:24 +00001076#ifdef FEAT_GUI_W32
1077 /* On Win32 ignore <M-F4>, we get it when closing the window was
1078 * cancelled. */
1079 case K_F4:
1080 if (mod_mask != MOD_MASK_ALT)
1081 goto normalchar;
1082 break;
1083#endif
1084
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085#ifdef FEAT_GUI
1086 case K_VER_SCROLLBAR:
1087 ins_scroll();
1088 break;
1089
1090 case K_HOR_SCROLLBAR:
1091 ins_horscroll();
1092 break;
1093#endif
1094
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001095 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 case K_S_HOME:
1098 case K_C_HOME:
1099 ins_home(c);
1100 break;
1101
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001102 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 case K_S_END:
1105 case K_C_END:
1106 ins_end(c);
1107 break;
1108
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001109 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001110 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1111 ins_s_left();
1112 else
1113 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 break;
1115
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001116 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 case K_C_LEFT:
1118 ins_s_left();
1119 break;
1120
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001121 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001122 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1123 ins_s_right();
1124 else
1125 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 break;
1127
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001128 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 case K_C_RIGHT:
1130 ins_s_right();
1131 break;
1132
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001133 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001134#ifdef FEAT_INS_EXPAND
1135 if (pum_visible())
1136 goto docomplete;
1137#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001138 if (mod_mask & MOD_MASK_SHIFT)
1139 ins_pageup();
1140 else
1141 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 break;
1143
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001144 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 case K_PAGEUP:
1146 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001147#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001148 if (pum_visible())
1149 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 ins_pageup();
1152 break;
1153
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001154 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001155#ifdef FEAT_INS_EXPAND
1156 if (pum_visible())
1157 goto docomplete;
1158#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001159 if (mod_mask & MOD_MASK_SHIFT)
1160 ins_pagedown();
1161 else
1162 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 break;
1164
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001165 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 case K_PAGEDOWN:
1167 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001168#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001169 if (pum_visible())
1170 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 ins_pagedown();
1173 break;
1174
1175#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001176 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 ins_drop();
1178 break;
1179#endif
1180
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001181 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 c = TAB;
1183 /* FALLTHROUGH */
1184
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001185 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1187 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1188 goto docomplete;
1189#endif
1190 inserted_space = FALSE;
1191 if (ins_tab())
1192 goto normalchar; /* insert TAB as a normal char */
1193 auto_format(FALSE, TRUE);
1194 break;
1195
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001196 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 c = CAR;
1198 /* FALLTHROUGH */
1199 case CAR:
1200 case NL:
1201#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1202 /* In a quickfix window a <CR> jumps to the error under the
1203 * cursor. */
1204 if (bt_quickfix(curbuf) && c == CAR)
1205 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001206 if (curwin->w_llist_ref == NULL) /* quickfix window */
1207 do_cmdline_cmd((char_u *)".cc");
1208 else /* location list window */
1209 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 break;
1211 }
1212#endif
1213#ifdef FEAT_CMDWIN
1214 if (cmdwin_type != 0)
1215 {
1216 /* Execute the command in the cmdline window. */
1217 cmdwin_result = CAR;
1218 goto doESCkey;
1219 }
1220#endif
1221 if (ins_eol(c) && !p_im)
1222 goto doESCkey; /* out of memory */
1223 auto_format(FALSE, FALSE);
1224 inserted_space = FALSE;
1225 break;
1226
1227#if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001228 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229# ifdef FEAT_INS_EXPAND
1230 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1231 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001232 if (has_compl_option(TRUE))
1233 goto docomplete;
1234 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 }
1236# endif
1237# ifdef FEAT_DIGRAPHS
1238 c = ins_digraph();
1239 if (c == NUL)
1240 break;
1241# endif
1242 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244
1245#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001246 case Ctrl_X: /* Enter CTRL-X mode */
1247 ins_ctrl_x();
1248 break;
1249
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001250 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 if (ctrl_x_mode != CTRL_X_TAGS)
1252 goto normalchar;
1253 goto docomplete;
1254
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001255 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 if (ctrl_x_mode != CTRL_X_FILES)
1257 goto normalchar;
1258 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001259
1260 case 's': /* Spelling completion after ^X */
1261 case Ctrl_S:
1262 if (ctrl_x_mode != CTRL_X_SPELL)
1263 goto normalchar;
1264 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265#endif
1266
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001267 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268#ifdef FEAT_INS_EXPAND
1269 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1270#endif
1271 {
1272 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1273 if (p_im)
1274 {
1275 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1276 break;
1277 goto doESCkey;
1278 }
1279 goto normalchar;
1280 }
1281#ifdef FEAT_INS_EXPAND
1282 /* FALLTHROUGH */
1283
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001284 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 case Ctrl_N:
1286 /* if 'complete' is empty then plain ^P is no longer special,
1287 * but it is under other ^X modes */
1288 if (*curbuf->b_p_cpt == NUL
1289 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001290 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 goto normalchar;
1292
1293docomplete:
1294 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001295 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 break;
1297#endif /* FEAT_INS_EXPAND */
1298
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001299 case Ctrl_Y: /* copy from previous line or scroll down */
1300 case Ctrl_E: /* copy from next line or scroll up */
1301 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 break;
1303
1304 default:
1305#ifdef UNIX
1306 if (c == intr_char) /* special interrupt char */
1307 goto do_intr;
1308#endif
1309
1310 /*
1311 * Insert a nomal character.
1312 */
1313normalchar:
1314#ifdef FEAT_SMARTINDENT
1315 /* Try to perform smart-indenting. */
1316 ins_try_si(c);
1317#endif
1318
1319 if (c == ' ')
1320 {
1321 inserted_space = TRUE;
1322#ifdef FEAT_CINDENT
1323 if (inindent(0))
1324 can_cindent = FALSE;
1325#endif
1326 if (Insstart_blank_vcol == MAXCOL
1327 && curwin->w_cursor.lnum == Insstart.lnum)
1328 Insstart_blank_vcol = get_nolist_virtcol();
1329 }
1330
1331 if (vim_iswordc(c) || !echeck_abbr(
1332#ifdef FEAT_MBYTE
1333 /* Add ABBR_OFF for characters above 0x100, this is
1334 * what check_abbr() expects. */
1335 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1336#endif
1337 c))
1338 {
1339 insert_special(c, FALSE, FALSE);
1340#ifdef FEAT_RIGHTLEFT
1341 revins_legal++;
1342 revins_chars++;
1343#endif
1344 }
1345
1346 auto_format(FALSE, TRUE);
1347
1348#ifdef FEAT_FOLDING
1349 /* When inserting a character the cursor line must never be in a
1350 * closed fold. */
1351 foldOpenCursor();
1352#endif
1353 break;
1354 } /* end of switch (c) */
1355
1356 /* If the cursor was moved we didn't just insert a space */
1357 if (arrow_used)
1358 inserted_space = FALSE;
1359
1360#ifdef FEAT_CINDENT
1361 if (can_cindent && cindent_on()
1362# ifdef FEAT_INS_EXPAND
1363 && ctrl_x_mode == 0
1364# endif
1365 )
1366 {
1367force_cindent:
1368 /*
1369 * Indent now if a key was typed that is in 'cinkeys'.
1370 */
1371 if (in_cinkeys(c, ' ', line_is_white))
1372 {
1373 if (stop_arrow() == OK)
1374 /* re-indent the current line */
1375 do_c_expr_indent();
1376 }
1377 }
1378#endif /* FEAT_CINDENT */
1379
1380 } /* for (;;) */
1381 /* NOTREACHED */
1382}
1383
1384/*
1385 * Redraw for Insert mode.
1386 * This is postponed until getting the next character to make '$' in the 'cpo'
1387 * option work correctly.
1388 * Only redraw when there are no characters available. This speeds up
1389 * inserting sequences of characters (e.g., for CTRL-R).
1390 */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001391/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001393ins_redraw(ready)
1394 int ready; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395{
1396 if (!char_avail())
1397 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00001398#ifdef FEAT_AUTOCMD
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001399 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1400 * visible, the command might delete it. */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001401 if (ready && has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001402 && !equalpos(last_cursormoved, curwin->w_cursor)
1403# ifdef FEAT_INS_EXPAND
1404 && !pum_visible()
1405# endif
1406 )
Bram Moolenaar754b5602006-02-09 23:53:20 +00001407 {
1408 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1409 last_cursormoved = curwin->w_cursor;
1410 }
1411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 if (must_redraw)
1413 update_screen(0);
1414 else if (clear_cmdline || redraw_cmdline)
1415 showmode(); /* clear cmdline and show mode */
1416 showruler(FALSE);
1417 setcursor();
1418 emsg_on_display = FALSE; /* may remove error message now */
1419 }
1420}
1421
1422/*
1423 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1424 */
1425 static void
1426ins_ctrl_v()
1427{
1428 int c;
1429
1430 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001431 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432
1433 if (redrawing() && !char_avail())
1434 edit_putchar('^', TRUE);
1435 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1436
1437#ifdef FEAT_CMDL_INFO
1438 add_to_showcmd_c(Ctrl_V);
1439#endif
1440
1441 c = get_literal();
1442#ifdef FEAT_CMDL_INFO
1443 clear_showcmd();
1444#endif
1445 insert_special(c, FALSE, TRUE);
1446#ifdef FEAT_RIGHTLEFT
1447 revins_chars++;
1448 revins_legal++;
1449#endif
1450}
1451
1452/*
1453 * Put a character directly onto the screen. It's not stored in a buffer.
1454 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1455 */
1456static int pc_status;
1457#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1458#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1459#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1460#define PC_STATUS_SET 3 /* pc_bytes was filled */
1461#ifdef FEAT_MBYTE
1462static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1463#else
1464static char_u pc_bytes[2]; /* saved bytes */
1465#endif
1466static int pc_attr;
1467static int pc_row;
1468static int pc_col;
1469
1470 void
1471edit_putchar(c, highlight)
1472 int c;
1473 int highlight;
1474{
1475 int attr;
1476
1477 if (ScreenLines != NULL)
1478 {
1479 update_topline(); /* just in case w_topline isn't valid */
1480 validate_cursor();
1481 if (highlight)
1482 attr = hl_attr(HLF_8);
1483 else
1484 attr = 0;
1485 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1486 pc_col = W_WINCOL(curwin);
1487#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1488 pc_status = PC_STATUS_UNSET;
1489#endif
1490#ifdef FEAT_RIGHTLEFT
1491 if (curwin->w_p_rl)
1492 {
1493 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1494# ifdef FEAT_MBYTE
1495 if (has_mbyte)
1496 {
1497 int fix_col = mb_fix_col(pc_col, pc_row);
1498
1499 if (fix_col != pc_col)
1500 {
1501 screen_putchar(' ', pc_row, fix_col, attr);
1502 --curwin->w_wcol;
1503 pc_status = PC_STATUS_RIGHT;
1504 }
1505 }
1506# endif
1507 }
1508 else
1509#endif
1510 {
1511 pc_col += curwin->w_wcol;
1512#ifdef FEAT_MBYTE
1513 if (mb_lefthalve(pc_row, pc_col))
1514 pc_status = PC_STATUS_LEFT;
1515#endif
1516 }
1517
1518 /* save the character to be able to put it back */
1519#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1520 if (pc_status == PC_STATUS_UNSET)
1521#endif
1522 {
1523 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1524 pc_status = PC_STATUS_SET;
1525 }
1526 screen_putchar(c, pc_row, pc_col, attr);
1527 }
1528}
1529
1530/*
1531 * Undo the previous edit_putchar().
1532 */
1533 void
1534edit_unputchar()
1535{
1536 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1537 {
1538#if defined(FEAT_MBYTE)
1539 if (pc_status == PC_STATUS_RIGHT)
1540 ++curwin->w_wcol;
1541 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1542 redrawWinline(curwin->w_cursor.lnum, FALSE);
1543 else
1544#endif
1545 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1546 }
1547}
1548
1549/*
1550 * Called when p_dollar is set: display a '$' at the end of the changed text
1551 * Only works when cursor is in the line that changes.
1552 */
1553 void
1554display_dollar(col)
1555 colnr_T col;
1556{
1557 colnr_T save_col;
1558
1559 if (!redrawing())
1560 return;
1561
1562 cursor_off();
1563 save_col = curwin->w_cursor.col;
1564 curwin->w_cursor.col = col;
1565#ifdef FEAT_MBYTE
1566 if (has_mbyte)
1567 {
1568 char_u *p;
1569
1570 /* If on the last byte of a multi-byte move to the first byte. */
1571 p = ml_get_curline();
1572 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1573 }
1574#endif
1575 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1576 if (curwin->w_wcol < W_WIDTH(curwin))
1577 {
1578 edit_putchar('$', FALSE);
1579 dollar_vcol = curwin->w_virtcol;
1580 }
1581 curwin->w_cursor.col = save_col;
1582}
1583
1584/*
1585 * Call this function before moving the cursor from the normal insert position
1586 * in insert mode.
1587 */
1588 static void
1589undisplay_dollar()
1590{
1591 if (dollar_vcol)
1592 {
1593 dollar_vcol = 0;
1594 redrawWinline(curwin->w_cursor.lnum, FALSE);
1595 }
1596}
1597
1598/*
1599 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1600 * Keep the cursor on the same character.
1601 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1602 * type == INDENT_DEC decrease indent (for CTRL-D)
1603 * type == INDENT_SET set indent to "amount"
1604 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1605 */
1606 void
1607change_indent(type, amount, round, replaced)
1608 int type;
1609 int amount;
1610 int round;
1611 int replaced; /* replaced character, put on replace stack */
1612{
1613 int vcol;
1614 int last_vcol;
1615 int insstart_less; /* reduction for Insstart.col */
1616 int new_cursor_col;
1617 int i;
1618 char_u *ptr;
1619 int save_p_list;
1620 int start_col;
1621 colnr_T vc;
1622#ifdef FEAT_VREPLACE
1623 colnr_T orig_col = 0; /* init for GCC */
1624 char_u *new_line, *orig_line = NULL; /* init for GCC */
1625
1626 /* VREPLACE mode needs to know what the line was like before changing */
1627 if (State & VREPLACE_FLAG)
1628 {
1629 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1630 orig_col = curwin->w_cursor.col;
1631 }
1632#endif
1633
1634 /* for the following tricks we don't want list mode */
1635 save_p_list = curwin->w_p_list;
1636 curwin->w_p_list = FALSE;
1637 vc = getvcol_nolist(&curwin->w_cursor);
1638 vcol = vc;
1639
1640 /*
1641 * For Replace mode we need to fix the replace stack later, which is only
1642 * possible when the cursor is in the indent. Remember the number of
1643 * characters before the cursor if it's possible.
1644 */
1645 start_col = curwin->w_cursor.col;
1646
1647 /* determine offset from first non-blank */
1648 new_cursor_col = curwin->w_cursor.col;
1649 beginline(BL_WHITE);
1650 new_cursor_col -= curwin->w_cursor.col;
1651
1652 insstart_less = curwin->w_cursor.col;
1653
1654 /*
1655 * If the cursor is in the indent, compute how many screen columns the
1656 * cursor is to the left of the first non-blank.
1657 */
1658 if (new_cursor_col < 0)
1659 vcol = get_indent() - vcol;
1660
1661 if (new_cursor_col > 0) /* can't fix replace stack */
1662 start_col = -1;
1663
1664 /*
1665 * Set the new indent. The cursor will be put on the first non-blank.
1666 */
1667 if (type == INDENT_SET)
1668 (void)set_indent(amount, SIN_CHANGED);
1669 else
1670 {
1671#ifdef FEAT_VREPLACE
1672 int save_State = State;
1673
1674 /* Avoid being called recursively. */
1675 if (State & VREPLACE_FLAG)
1676 State = INSERT;
1677#endif
1678 shift_line(type == INDENT_DEC, round, 1);
1679#ifdef FEAT_VREPLACE
1680 State = save_State;
1681#endif
1682 }
1683 insstart_less -= curwin->w_cursor.col;
1684
1685 /*
1686 * Try to put cursor on same character.
1687 * If the cursor is at or after the first non-blank in the line,
1688 * compute the cursor column relative to the column of the first
1689 * non-blank character.
1690 * If we are not in insert mode, leave the cursor on the first non-blank.
1691 * If the cursor is before the first non-blank, position it relative
1692 * to the first non-blank, counted in screen columns.
1693 */
1694 if (new_cursor_col >= 0)
1695 {
1696 /*
1697 * When changing the indent while the cursor is touching it, reset
1698 * Insstart_col to 0.
1699 */
1700 if (new_cursor_col == 0)
1701 insstart_less = MAXCOL;
1702 new_cursor_col += curwin->w_cursor.col;
1703 }
1704 else if (!(State & INSERT))
1705 new_cursor_col = curwin->w_cursor.col;
1706 else
1707 {
1708 /*
1709 * Compute the screen column where the cursor should be.
1710 */
1711 vcol = get_indent() - vcol;
1712 curwin->w_virtcol = (vcol < 0) ? 0 : vcol;
1713
1714 /*
1715 * Advance the cursor until we reach the right screen column.
1716 */
1717 vcol = last_vcol = 0;
1718 new_cursor_col = -1;
1719 ptr = ml_get_curline();
1720 while (vcol <= (int)curwin->w_virtcol)
1721 {
1722 last_vcol = vcol;
1723#ifdef FEAT_MBYTE
1724 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001725 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 else
1727#endif
1728 ++new_cursor_col;
1729 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1730 }
1731 vcol = last_vcol;
1732
1733 /*
1734 * May need to insert spaces to be able to position the cursor on
1735 * the right screen column.
1736 */
1737 if (vcol != (int)curwin->w_virtcol)
1738 {
1739 curwin->w_cursor.col = new_cursor_col;
1740 i = (int)curwin->w_virtcol - vcol;
1741 ptr = alloc(i + 1);
1742 if (ptr != NULL)
1743 {
1744 new_cursor_col += i;
1745 ptr[i] = NUL;
1746 while (--i >= 0)
1747 ptr[i] = ' ';
1748 ins_str(ptr);
1749 vim_free(ptr);
1750 }
1751 }
1752
1753 /*
1754 * When changing the indent while the cursor is in it, reset
1755 * Insstart_col to 0.
1756 */
1757 insstart_less = MAXCOL;
1758 }
1759
1760 curwin->w_p_list = save_p_list;
1761
1762 if (new_cursor_col <= 0)
1763 curwin->w_cursor.col = 0;
1764 else
1765 curwin->w_cursor.col = new_cursor_col;
1766 curwin->w_set_curswant = TRUE;
1767 changed_cline_bef_curs();
1768
1769 /*
1770 * May have to adjust the start of the insert.
1771 */
1772 if (State & INSERT)
1773 {
1774 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1775 {
1776 if ((int)Insstart.col <= insstart_less)
1777 Insstart.col = 0;
1778 else
1779 Insstart.col -= insstart_less;
1780 }
1781 if ((int)ai_col <= insstart_less)
1782 ai_col = 0;
1783 else
1784 ai_col -= insstart_less;
1785 }
1786
1787 /*
1788 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1789 * If the number of characters before the cursor decreased, need to pop a
1790 * few characters from the replace stack.
1791 * If the number of characters before the cursor increased, need to push a
1792 * few NULs onto the replace stack.
1793 */
1794 if (REPLACE_NORMAL(State) && start_col >= 0)
1795 {
1796 while (start_col > (int)curwin->w_cursor.col)
1797 {
1798 replace_join(0); /* remove a NUL from the replace stack */
1799 --start_col;
1800 }
1801 while (start_col < (int)curwin->w_cursor.col || replaced)
1802 {
1803 replace_push(NUL);
1804 if (replaced)
1805 {
1806 replace_push(replaced);
1807 replaced = NUL;
1808 }
1809 ++start_col;
1810 }
1811 }
1812
1813#ifdef FEAT_VREPLACE
1814 /*
1815 * For VREPLACE mode, we also have to fix the replace stack. In this case
1816 * it is always possible because we backspace over the whole line and then
1817 * put it back again the way we wanted it.
1818 */
1819 if (State & VREPLACE_FLAG)
1820 {
1821 /* If orig_line didn't allocate, just return. At least we did the job,
1822 * even if you can't backspace. */
1823 if (orig_line == NULL)
1824 return;
1825
1826 /* Save new line */
1827 new_line = vim_strsave(ml_get_curline());
1828 if (new_line == NULL)
1829 return;
1830
1831 /* We only put back the new line up to the cursor */
1832 new_line[curwin->w_cursor.col] = NUL;
1833
1834 /* Put back original line */
1835 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1836 curwin->w_cursor.col = orig_col;
1837
1838 /* Backspace from cursor to start of line */
1839 backspace_until_column(0);
1840
1841 /* Insert new stuff into line again */
1842 ins_bytes(new_line);
1843
1844 vim_free(new_line);
1845 }
1846#endif
1847}
1848
1849/*
1850 * Truncate the space at the end of a line. This is to be used only in an
1851 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1852 * modes.
1853 */
1854 void
1855truncate_spaces(line)
1856 char_u *line;
1857{
1858 int i;
1859
1860 /* find start of trailing white space */
1861 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1862 {
1863 if (State & REPLACE_FLAG)
1864 replace_join(0); /* remove a NUL from the replace stack */
1865 }
1866 line[i + 1] = NUL;
1867}
1868
1869#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1870 || defined(FEAT_COMMENTS) || defined(PROTO)
1871/*
1872 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1873 * modes correctly. May also be used when not in insert mode at all.
1874 */
1875 void
1876backspace_until_column(col)
1877 int col;
1878{
1879 while ((int)curwin->w_cursor.col > col)
1880 {
1881 curwin->w_cursor.col--;
1882 if (State & REPLACE_FLAG)
1883 replace_do_bs();
1884 else
1885 (void)del_char(FALSE);
1886 }
1887}
1888#endif
1889
1890#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1891/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001892 * CTRL-X pressed in Insert mode.
1893 */
1894 static void
1895ins_ctrl_x()
1896{
1897 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
1898 * CTRL-V works like CTRL-N */
1899 if (ctrl_x_mode != CTRL_X_CMDLINE)
1900 {
1901 /* if the next ^X<> won't ADD nothing, then reset
1902 * compl_cont_status */
1903 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001904 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001905 else
1906 compl_cont_status = 0;
1907 /* We're not sure which CTRL-X mode it will be yet */
1908 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
1909 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
1910 edit_submode_pre = NULL;
1911 showmode();
1912 }
1913}
1914
1915/*
1916 * Return TRUE if the 'dict' or 'tsr' option can be used.
1917 */
1918 static int
1919has_compl_option(dict_opt)
1920 int dict_opt;
1921{
Bram Moolenaar0b238792006-03-02 22:49:12 +00001922 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00001923# ifdef FEAT_SPELL
1924 && !curwin->w_p_spell
1925# endif
1926 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001927 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
1928 {
1929 ctrl_x_mode = 0;
1930 edit_submode = NULL;
1931 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
1932 : (char_u *)_("'thesaurus' option is empty"),
1933 hl_attr(HLF_E));
1934 if (emsg_silent == 0)
1935 {
1936 vim_beep();
1937 setcursor();
1938 out_flush();
1939 ui_delay(2000L, FALSE);
1940 }
1941 return FALSE;
1942 }
1943 return TRUE;
1944}
1945
1946/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
1948 * This depends on the current mode.
1949 */
1950 int
1951vim_is_ctrl_x_key(c)
1952 int c;
1953{
1954 /* Always allow ^R - let it's results then be checked */
1955 if (c == Ctrl_R)
1956 return TRUE;
1957
Bram Moolenaare3226be2005-12-18 22:10:00 +00001958 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001959 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00001960 return TRUE;
1961
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 switch (ctrl_x_mode)
1963 {
1964 case 0: /* Not in any CTRL-X mode */
1965 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
1966 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001967 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
1969 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
1970 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00001971 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
1972 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 case CTRL_X_SCROLL:
1974 return (c == Ctrl_Y || c == Ctrl_E);
1975 case CTRL_X_WHOLE_LINE:
1976 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
1977 case CTRL_X_FILES:
1978 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
1979 case CTRL_X_DICTIONARY:
1980 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
1981 case CTRL_X_THESAURUS:
1982 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
1983 case CTRL_X_TAGS:
1984 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
1985#ifdef FEAT_FIND_ID
1986 case CTRL_X_PATH_PATTERNS:
1987 return (c == Ctrl_P || c == Ctrl_N);
1988 case CTRL_X_PATH_DEFINES:
1989 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
1990#endif
1991 case CTRL_X_CMDLINE:
1992 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
1993 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001994#ifdef FEAT_COMPL_FUNC
1995 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001996 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001997 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001998 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001999#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002000 case CTRL_X_SPELL:
2001 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 }
2003 EMSG(_(e_internal));
2004 return FALSE;
2005}
2006
2007/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002008 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 * case of the originally typed text is used, and the case of the completed
2010 * text is infered, ie this tries to work out what case you probably wanted
2011 * the rest of the word to be in -- webb
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002012 * TODO: make this work for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 */
2014 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002015ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 char_u *str;
2017 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002018 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 char_u *fname;
2020 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002021 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022{
2023 int has_lower = FALSE;
2024 int was_letter = FALSE;
2025 int idx;
2026
2027 if (p_ic && curbuf->b_p_inf && len < IOSIZE)
2028 {
2029 /* Infer case of completed part -- webb */
2030 /* Use IObuff, str would change text in buffer! */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002031 vim_strncpy(IObuff, str, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032
2033 /* Rule 1: Were any chars converted to lower? */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002034 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002036 if (islower(compl_orig_text[idx]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 {
2038 has_lower = TRUE;
2039 if (isupper(IObuff[idx]))
2040 {
2041 /* Rule 1 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002042 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 IObuff[idx] = TOLOWER_LOC(IObuff[idx]);
2044 break;
2045 }
2046 }
2047 }
2048
2049 /*
2050 * Rule 2: No lower case, 2nd consecutive letter converted to
2051 * upper case.
2052 */
2053 if (!has_lower)
2054 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002055 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002057 if (was_letter && isupper(compl_orig_text[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 && islower(IObuff[idx]))
2059 {
2060 /* Rule 2 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002061 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 IObuff[idx] = TOUPPER_LOC(IObuff[idx]);
2063 break;
2064 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002065 was_letter = isalpha(compl_orig_text[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 }
2067 }
2068
2069 /* Copy the original case of the part we typed */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002070 STRNCPY(IObuff, compl_orig_text, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071
Bram Moolenaar39f05632006-03-19 22:15:26 +00002072 return ins_compl_add(IObuff, len, icase, fname, NULL, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002074 return ins_compl_add(str, len, icase, fname, NULL, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075}
2076
2077/*
2078 * Add a match to the list of matches.
2079 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002080 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002081 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002083 int
Bram Moolenaar39f05632006-03-19 22:15:26 +00002084ins_compl_add(str, len, icase, fname, cptext, cdir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 char_u *str;
2086 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002087 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 char_u *fname;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002089 char_u **cptext; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002090 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002091 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002093 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002094 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095
2096 ui_breakcheck();
2097 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002098 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 if (len < 0)
2100 len = (int)STRLEN(str);
2101
2102 /*
2103 * If the same match is already present, don't add it.
2104 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002105 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002107 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 do
2109 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002110 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002111 && ins_compl_equal(match, str, len)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002112 && match->cp_str[len] == NUL)
2113 return NOTDONE;
2114 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002115 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 }
2117
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002118 /* Remove any popup menu before changing the list of matches. */
2119 ins_compl_del_pum();
2120
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 /*
2122 * Allocate a new match structure.
2123 * Copy the values to the new match structure.
2124 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002125 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002127 return FAIL;
2128 match->cp_number = -1;
2129 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002130 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002131 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 {
2133 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002134 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002136 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002137
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002139 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2141 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002142 if (fname != NULL
2143 && compl_curr_match
2144 && compl_curr_match->cp_fname != NULL
2145 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002146 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002147 else if (fname != NULL)
2148 {
2149 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002150 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002153 match->cp_fname = NULL;
2154 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002155
2156 if (cptext != NULL)
2157 {
2158 int i;
2159
2160 for (i = 0; i < CPT_COUNT; ++i)
2161 if (cptext[i] != NULL && *cptext[i] != NUL)
2162 match->cp_text[i] = vim_strsave(cptext[i]);
2163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164
2165 /*
2166 * Link the new match structure in the list of matches.
2167 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002168 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002169 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 else if (dir == FORWARD)
2171 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002172 match->cp_next = compl_curr_match->cp_next;
2173 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 }
2175 else /* BACKWARD */
2176 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002177 match->cp_next = compl_curr_match;
2178 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002180 if (match->cp_next)
2181 match->cp_next->cp_prev = match;
2182 if (match->cp_prev)
2183 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002185 compl_first_match = match;
2186 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002188 /*
2189 * Find the longest common string if still doing that.
2190 */
2191 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2192 ins_compl_longest_match(match);
2193
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 return OK;
2195}
2196
2197/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002198 * Return TRUE if "str[len]" matches with match->cp_str, considering
2199 * match->cp_icase.
2200 */
2201 static int
2202ins_compl_equal(match, str, len)
2203 compl_T *match;
2204 char_u *str;
2205 int len;
2206{
2207 if (match->cp_icase)
2208 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2209 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2210}
2211
2212/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002213 * Reduce the longest common string for match "match".
2214 */
2215 static void
2216ins_compl_longest_match(match)
2217 compl_T *match;
2218{
2219 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002220 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002221 int had_match;
2222
2223 if (compl_leader == NULL)
2224 /* First match, use it as a whole. */
2225 compl_leader = vim_strsave(match->cp_str);
2226 else
2227 {
2228 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002229 p = compl_leader;
2230 s = match->cp_str;
2231 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002232 {
2233#ifdef FEAT_MBYTE
2234 if (has_mbyte)
2235 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002236 c1 = mb_ptr2char(p);
2237 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002238 }
2239 else
2240#endif
2241 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002242 c1 = *p;
2243 c2 = *s;
2244 }
2245 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2246 : (c1 != c2))
2247 break;
2248#ifdef FEAT_MBYTE
2249 if (has_mbyte)
2250 {
2251 mb_ptr_adv(p);
2252 mb_ptr_adv(s);
2253 }
2254 else
2255#endif
2256 {
2257 ++p;
2258 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002259 }
2260 }
2261
2262 if (*p != NUL)
2263 {
2264 /* Leader was shortened, need to change the inserted text. */
2265 *p = NUL;
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 }
2276
2277 compl_used_match = FALSE;
2278 }
2279}
2280
2281/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 * Add an array of matches to the list of matches.
2283 * Frees matches[].
2284 */
2285 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002286ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 int num_matches;
2288 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002289 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290{
2291 int i;
2292 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002293 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294
Bram Moolenaar572cb562005-08-05 21:35:02 +00002295 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002296 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar39f05632006-03-19 22:15:26 +00002297 NULL, NULL, dir, 0)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002299 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 FreeWild(num_matches, matches);
2301}
2302
2303/* Make the completion list cyclic.
2304 * Return the number of matches (excluding the original).
2305 */
2306 static int
2307ins_compl_make_cyclic()
2308{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002309 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 int count = 0;
2311
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002312 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 {
2314 /*
2315 * Find the end of the list.
2316 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002317 match = compl_first_match;
2318 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002319 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002321 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 ++count;
2323 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002324 match->cp_next = compl_first_match;
2325 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 }
2327 return count;
2328}
2329
Bram Moolenaara94bc432006-03-10 21:42:59 +00002330/*
2331 * Start completion for the complete() function.
2332 * "startcol" is where the matched text starts (1 is first column).
2333 * "list" is the list of matches.
2334 */
2335 void
2336set_completion(startcol, list)
2337 int startcol;
2338 list_T *list;
2339{
2340 /* If already doing completions stop it. */
2341 if (ctrl_x_mode != 0)
2342 ins_compl_prep(' ');
2343 ins_compl_clear();
2344
2345 if (stop_arrow() == FAIL)
2346 return;
2347
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002348 if (startcol > (int)curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002349 startcol = curwin->w_cursor.col;
2350 compl_col = startcol;
2351 compl_length = curwin->w_cursor.col - startcol;
2352 /* compl_pattern doesn't need to be set */
2353 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2354 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar39f05632006-03-19 22:15:26 +00002355 -1, FALSE, NULL, NULL, 0, ORIGINAL_TEXT) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002356 return;
2357
2358 /* Handle like dictionary completion. */
2359 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2360
2361 ins_compl_add_list(list);
2362 compl_matches = ins_compl_make_cyclic();
2363 compl_started = TRUE;
2364 compl_used_match = TRUE;
2365
2366 compl_curr_match = compl_first_match;
2367 ins_complete(Ctrl_N);
2368 out_flush();
2369}
2370
2371
Bram Moolenaar9372a112005-12-06 19:59:18 +00002372/* "compl_match_array" points the currently displayed list of entries in the
2373 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002374static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002375static int compl_match_arraysize;
2376
2377/*
2378 * Update the screen and when there is any scrolling remove the popup menu.
2379 */
2380 static void
2381ins_compl_upd_pum()
2382{
2383 int h;
2384
2385 if (compl_match_array != NULL)
2386 {
2387 h = curwin->w_cline_height;
2388 update_screen(0);
2389 if (h != curwin->w_cline_height)
2390 ins_compl_del_pum();
2391 }
2392}
2393
2394/*
2395 * Remove any popup menu.
2396 */
2397 static void
2398ins_compl_del_pum()
2399{
2400 if (compl_match_array != NULL)
2401 {
2402 pum_undisplay();
2403 vim_free(compl_match_array);
2404 compl_match_array = NULL;
2405 }
2406}
2407
2408/*
2409 * Return TRUE if the popup menu should be displayed.
2410 */
2411 static int
2412pum_wanted()
2413{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002414 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002415 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002416 return FALSE;
2417
2418 /* The display looks bad on a B&W display. */
2419 if (t_colors < 8
2420#ifdef FEAT_GUI
2421 && !gui.in_use
2422#endif
2423 )
2424 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002425 return TRUE;
2426}
2427
2428/*
2429 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002430 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002431 */
2432 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002433pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002434{
2435 compl_T *compl;
2436 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002437
2438 /* Don't display the popup menu if there are no matches or there is only
2439 * one (ignoring the original text). */
2440 compl = compl_first_match;
2441 i = 0;
2442 do
2443 {
2444 if (compl == NULL
2445 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2446 break;
2447 compl = compl->cp_next;
2448 } while (compl != compl_first_match);
2449
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002450 if (strstr((char *)p_cot, "menuone") != NULL)
2451 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002452 return (i >= 2);
2453}
2454
2455/*
2456 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002457 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002458 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002459 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002460ins_compl_show_pum()
2461{
2462 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002463 compl_T *shown_compl = NULL;
2464 int did_find_shown_match = FALSE;
2465 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002466 int i;
2467 int cur = -1;
2468 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002469 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002470
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002471 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002472 return;
2473
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002474#if defined(FEAT_EVAL)
2475 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2476 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2477#endif
2478
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002479 /* Update the screen before drawing the popup menu over it. */
2480 update_screen(0);
2481
2482 if (compl_match_array == NULL)
2483 {
2484 /* Need to build the popup menu list. */
2485 compl_match_arraysize = 0;
2486 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002487 if (compl_leader != NULL)
2488 lead_len = STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002489 do
2490 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002491 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2492 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002493 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002494 ++compl_match_arraysize;
2495 compl = compl->cp_next;
2496 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002497 if (compl_match_arraysize == 0)
2498 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002499 compl_match_array = (pumitem_T *)alloc_clear(
2500 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002501 * compl_match_arraysize));
2502 if (compl_match_array != NULL)
2503 {
2504 i = 0;
2505 compl = compl_first_match;
2506 do
2507 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002508 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2509 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002510 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002511 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002512 if (!shown_match_ok)
2513 {
2514 if (compl == compl_shown_match || did_find_shown_match)
2515 {
2516 /* This item is the shown match or this is the
2517 * first displayed item after the shown match. */
2518 compl_shown_match = compl;
2519 did_find_shown_match = TRUE;
2520 shown_match_ok = TRUE;
2521 }
2522 else
2523 /* Remember this displayed match for when the
2524 * shown match is just below it. */
2525 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002526 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002527 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002528
2529 if (compl->cp_text[CPT_ABBR] != NULL)
2530 compl_match_array[i].pum_text =
2531 compl->cp_text[CPT_ABBR];
2532 else
2533 compl_match_array[i].pum_text = compl->cp_str;
2534 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2535 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2536 if (compl->cp_text[CPT_MENU] != NULL)
2537 compl_match_array[i++].pum_extra =
2538 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002539 else
2540 compl_match_array[i++].pum_extra = compl->cp_fname;
2541 }
2542
2543 if (compl == compl_shown_match)
2544 {
2545 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002546
2547 /* When the original text is the shown match don't set
2548 * compl_shown_match. */
2549 if (compl->cp_flags & ORIGINAL_TEXT)
2550 shown_match_ok = TRUE;
2551
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002552 if (!shown_match_ok && shown_compl != NULL)
2553 {
2554 /* The shown match isn't displayed, set it to the
2555 * previously displayed match. */
2556 compl_shown_match = shown_compl;
2557 shown_match_ok = TRUE;
2558 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002559 }
2560 compl = compl->cp_next;
2561 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002562
2563 if (!shown_match_ok) /* no displayed match at all */
2564 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002565 }
2566 }
2567 else
2568 {
2569 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002570 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00002571 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
2572 || compl_match_array[i].pum_text
2573 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaara6557602006-02-04 22:43:20 +00002574 break;
2575 cur = i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002576 }
2577
2578 if (compl_match_array != NULL)
2579 {
2580 /* Compute the screen column of the start of the completed text.
2581 * Use the cursor to get all wrapping and other settings right. */
2582 col = curwin->w_cursor.col;
2583 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00002584 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002585 curwin->w_cursor.col = col;
2586 }
2587}
2588
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589#define DICT_FIRST (1) /* use just first element in "dict" */
2590#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002591
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00002593 * Add any identifiers that match the given pattern in the list of dictionary
2594 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 */
2596 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00002597ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
2598 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002600 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002601 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002603 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 char_u *ptr;
2605 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 char_u **files;
2608 int count;
2609 int i;
2610 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002611 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612
Bram Moolenaar0b238792006-03-02 22:49:12 +00002613 if (*dict == NUL)
2614 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002615#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002616 /* When 'dictionary' is empty and spell checking is enabled use
2617 * "spell". */
2618 if (!thesaurus && curwin->w_p_spell)
2619 dict = (char_u *)"spell";
2620 else
2621#endif
2622 return;
2623 }
2624
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002626 if (buf == NULL)
2627 return;
2628
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 /* If 'infercase' is set, don't use 'smartcase' here */
2630 save_p_scs = p_scs;
2631 if (curbuf->b_p_inf)
2632 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002633
2634 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
2635 * to only match at the start of a line. Otherwise just match the
2636 * pattern. */
2637 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2638 {
2639 i = STRLEN(pat) + 8;
2640 ptr = alloc(i);
2641 if (ptr == NULL)
2642 return;
2643 vim_snprintf((char *)ptr, i, "^\\s*\\zs%s", pat);
2644 regmatch.regprog = vim_regcomp(ptr, p_magic ? RE_MAGIC : 0);
2645 vim_free(ptr);
2646 }
2647 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00002648 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002649 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002650 if (regmatch.regprog == NULL)
2651 goto theend;
2652 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002653
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2655 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002656 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 {
2658 /* copy one dictionary file name into buf */
2659 if (flags == DICT_EXACT)
2660 {
2661 count = 1;
2662 files = &dict;
2663 }
2664 else
2665 {
2666 /* Expand wildcards in the dictionary name, but do not allow
2667 * backticks (for security, the 'dict' option may have been set in
2668 * a modeline). */
2669 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002670# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002671 if (!thesaurus && STRCMP(buf, "spell") == 0)
2672 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002673 else
2674# endif
2675 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 || expand_wildcards(1, &buf, &count, &files,
2677 EW_FILE|EW_SILENT) != OK)
2678 count = 0;
2679 }
2680
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002681# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002682 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00002684 /* Complete from active spelling. Skip "\<" in the pattern, we
2685 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002686 if (pat[0] == '\\' && pat[1] == '<')
2687 ptr = pat + 2;
2688 else
2689 ptr = pat;
2690 spell_dump_compl(curbuf, ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002692 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002693# endif
Bram Moolenaar0b238792006-03-02 22:49:12 +00002694 {
2695 ins_compl_files(count, files, thesaurus, flags,
2696 &regmatch, buf, &dir);
2697 if (flags != DICT_EXACT)
2698 FreeWild(count, files);
2699 }
2700 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 break;
2702 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002703
2704theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 p_scs = save_p_scs;
2706 vim_free(regmatch.regprog);
2707 vim_free(buf);
2708}
2709
Bram Moolenaar0b238792006-03-02 22:49:12 +00002710 static void
2711ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
2712 int count;
2713 char_u **files;
2714 int thesaurus;
2715 int flags;
2716 regmatch_T *regmatch;
2717 char_u *buf;
2718 int *dir;
2719{
2720 char_u *ptr;
2721 int i;
2722 FILE *fp;
2723 int add_r;
2724
2725 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
2726 {
2727 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2728 if (flags != DICT_EXACT)
2729 {
2730 vim_snprintf((char *)IObuff, IOSIZE,
2731 _("Scanning dictionary: %s"), (char *)files[i]);
2732 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2733 }
2734
2735 if (fp != NULL)
2736 {
2737 /*
2738 * Read dictionary file line by line.
2739 * Check each line for a match.
2740 */
2741 while (!got_int && !compl_interrupted
2742 && !vim_fgets(buf, LSIZE, fp))
2743 {
2744 ptr = buf;
2745 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
2746 {
2747 ptr = regmatch->startp[0];
2748 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2749 ptr = find_line_end(ptr);
2750 else
2751 ptr = find_word_end(ptr);
2752 add_r = ins_compl_add_infercase(regmatch->startp[0],
2753 (int)(ptr - regmatch->startp[0]),
Bram Moolenaard289f132006-03-11 21:30:53 +00002754 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002755 if (thesaurus)
2756 {
2757 char_u *wstart;
2758
2759 /*
2760 * Add the other matches on the line
2761 */
2762 while (!got_int)
2763 {
2764 /* Find start of the next word. Skip white
2765 * space and punctuation. */
2766 ptr = find_word_start(ptr);
2767 if (*ptr == NUL || *ptr == NL)
2768 break;
2769 wstart = ptr;
2770
2771 /* Find end of the word and add it. */
2772#ifdef FEAT_MBYTE
2773 if (has_mbyte)
2774 /* Japanese words may have characters in
2775 * different classes, only separate words
2776 * with single-byte non-word characters. */
2777 while (*ptr != NUL)
2778 {
2779 int l = (*mb_ptr2len)(ptr);
2780
2781 if (l < 2 && !vim_iswordc(*ptr))
2782 break;
2783 ptr += l;
2784 }
2785 else
2786#endif
2787 ptr = find_word_end(ptr);
2788 add_r = ins_compl_add_infercase(wstart,
2789 (int)(ptr - wstart),
2790 p_ic, files[i], *dir, 0);
2791 }
2792 }
2793 if (add_r == OK)
2794 /* if dir was BACKWARD then honor it just once */
2795 *dir = FORWARD;
2796 else if (add_r == FAIL)
2797 break;
2798 /* avoid expensive call to vim_regexec() when at end
2799 * of line */
2800 if (*ptr == '\n' || got_int)
2801 break;
2802 }
2803 line_breakcheck();
2804 ins_compl_check_keys(50);
2805 }
2806 fclose(fp);
2807 }
2808 }
2809}
2810
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811/*
2812 * Find the start of the next word.
2813 * Returns a pointer to the first char of the word. Also stops at a NUL.
2814 */
2815 char_u *
2816find_word_start(ptr)
2817 char_u *ptr;
2818{
2819#ifdef FEAT_MBYTE
2820 if (has_mbyte)
2821 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002822 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 else
2824#endif
2825 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
2826 ++ptr;
2827 return ptr;
2828}
2829
2830/*
2831 * Find the end of the word. Assumes it starts inside a word.
2832 * Returns a pointer to just after the word.
2833 */
2834 char_u *
2835find_word_end(ptr)
2836 char_u *ptr;
2837{
2838#ifdef FEAT_MBYTE
2839 int start_class;
2840
2841 if (has_mbyte)
2842 {
2843 start_class = mb_get_class(ptr);
2844 if (start_class > 1)
2845 while (*ptr != NUL)
2846 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002847 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 if (mb_get_class(ptr) != start_class)
2849 break;
2850 }
2851 }
2852 else
2853#endif
2854 while (vim_iswordc(*ptr))
2855 ++ptr;
2856 return ptr;
2857}
2858
2859/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002860 * Find the end of the line, omitting CR and NL at the end.
2861 * Returns a pointer to just after the line.
2862 */
2863 static char_u *
2864find_line_end(ptr)
2865 char_u *ptr;
2866{
2867 char_u *s;
2868
2869 s = ptr + STRLEN(ptr);
2870 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
2871 --s;
2872 return s;
2873}
2874
2875/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 * Free the list of completions
2877 */
2878 static void
2879ins_compl_free()
2880{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002881 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002882 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002884 vim_free(compl_pattern);
2885 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002886 vim_free(compl_leader);
2887 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002889 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002891
2892 ins_compl_del_pum();
2893 pum_clear();
2894
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002895 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 do
2897 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002898 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002899 compl_curr_match = compl_curr_match->cp_next;
2900 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002902 if (match->cp_flags & FREE_FNAME)
2903 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00002904 for (i = 0; i < CPT_COUNT; ++i)
2905 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002907 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
2908 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909}
2910
2911 static void
2912ins_compl_clear()
2913{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002914 compl_cont_status = 0;
2915 compl_started = FALSE;
2916 compl_matches = 0;
2917 vim_free(compl_pattern);
2918 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002919 vim_free(compl_leader);
2920 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002922 vim_free(compl_orig_text);
2923 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002924 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925}
2926
2927/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002928 * Return TRUE when Insert completion is active.
2929 */
2930 int
2931ins_compl_active()
2932{
2933 return compl_started;
2934}
2935
2936/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002937 * Delete one character before the cursor and show the subset of the matches
2938 * that match the word that is now before the cursor.
Bram Moolenaara6557602006-02-04 22:43:20 +00002939 * Returns TRUE if the work is done and another char to be got from the user.
2940 */
2941 static int
2942ins_compl_bs()
2943{
2944 char_u *line;
2945 char_u *p;
2946
2947 if (curwin->w_cursor.col <= compl_col + compl_length)
2948 {
2949 /* Deleted more than what was used to find matches, need to look for
2950 * matches all over again. */
2951 ins_compl_free();
2952 compl_started = FALSE;
2953 compl_matches = 0;
2954 }
2955
2956 line = ml_get_curline();
2957 p = line + curwin->w_cursor.col;
2958 mb_ptr_back(line, p);
2959
2960 vim_free(compl_leader);
2961 compl_leader = vim_strnsave(line + compl_col, (p - line) - compl_col);
2962 if (compl_leader != NULL)
2963 {
2964 ins_compl_del_pum();
2965 ins_compl_delete();
2966 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2967
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002968 if (compl_started)
2969 ins_compl_set_original_text(compl_leader);
2970 else
Bram Moolenaara6557602006-02-04 22:43:20 +00002971 {
2972 /* Matches were cleared, need to search for them now. */
2973 if (ins_complete(Ctrl_N) == FAIL)
2974 compl_cont_status = 0;
2975 else
2976 {
2977 /* Remove the completed word again. */
2978 ins_compl_delete();
2979 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2980 }
2981 }
2982
2983 /* Show the popup menu with a different set of matches. */
2984 ins_compl_show_pum();
2985 compl_used_match = FALSE;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002986 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002987
2988 return TRUE;
2989 }
2990 return FALSE;
2991}
2992
2993/*
2994 * Append one character to the match leader. May reduce the number of
2995 * matches.
2996 */
2997 static void
2998ins_compl_addleader(c)
2999 int c;
3000{
3001#ifdef FEAT_MBYTE
3002 int cc;
3003
3004 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3005 {
3006 char_u buf[MB_MAXBYTES + 1];
3007
3008 (*mb_char2bytes)(c, buf);
3009 buf[cc] = NUL;
3010 ins_char_bytes(buf, cc);
3011 }
3012 else
3013#endif
3014 ins_char(c);
3015
3016 vim_free(compl_leader);
3017 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
3018 curwin->w_cursor.col - compl_col);
3019 if (compl_leader != NULL)
3020 {
3021 /* Show the popup menu with a different set of matches. */
3022 ins_compl_del_pum();
3023 ins_compl_show_pum();
3024 compl_used_match = FALSE;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003025 compl_enter_selects = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003026 ins_compl_set_original_text(compl_leader);
3027 }
3028}
3029
3030/*
3031 * Set the first match, the original text.
3032 */
3033 static void
3034ins_compl_set_original_text(str)
3035 char_u *str;
3036{
3037 char_u *p;
3038
3039 /* Replace the original text entry. */
3040 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3041 {
3042 p = vim_strsave(str);
3043 if (p != NULL)
3044 {
3045 vim_free(compl_first_match->cp_str);
3046 compl_first_match->cp_str = p;
3047 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003048 }
3049}
3050
3051/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003052 * Append one character to the match leader. May reduce the number of
3053 * matches.
3054 */
3055 static void
3056ins_compl_addfrommatch()
3057{
3058 char_u *p;
3059 int len = curwin->w_cursor.col - compl_col;
3060 int c;
3061
3062 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003063 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003064 return;
3065 p += len;
3066#ifdef FEAT_MBYTE
3067 c = mb_ptr2char(p);
3068#else
3069 c = *p;
3070#endif
3071 ins_compl_addleader(c);
3072}
3073
3074/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003076 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003077 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003079 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080ins_compl_prep(c)
3081 int c;
3082{
3083 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 int temp;
3085 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003086 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087
3088 /* Forget any previous 'special' messages if this is actually
3089 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3090 */
3091 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3092 edit_submode_extra = NULL;
3093
3094 /* Ignore end of Select mode mapping */
3095 if (c == K_SELECT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003096 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003098 /* Set "compl_get_longest" when finding the first matches. */
3099 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3100 || (ctrl_x_mode == 0 && !compl_started))
3101 {
3102 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3103 compl_used_match = TRUE;
3104 }
3105
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3107 {
3108 /*
3109 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3110 * it will be yet. Now we decide.
3111 */
3112 switch (c)
3113 {
3114 case Ctrl_E:
3115 case Ctrl_Y:
3116 ctrl_x_mode = CTRL_X_SCROLL;
3117 if (!(State & REPLACE_FLAG))
3118 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3119 else
3120 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3121 edit_submode_pre = NULL;
3122 showmode();
3123 break;
3124 case Ctrl_L:
3125 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3126 break;
3127 case Ctrl_F:
3128 ctrl_x_mode = CTRL_X_FILES;
3129 break;
3130 case Ctrl_K:
3131 ctrl_x_mode = CTRL_X_DICTIONARY;
3132 break;
3133 case Ctrl_R:
3134 /* Simply allow ^R to happen without affecting ^X mode */
3135 break;
3136 case Ctrl_T:
3137 ctrl_x_mode = CTRL_X_THESAURUS;
3138 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003139#ifdef FEAT_COMPL_FUNC
3140 case Ctrl_U:
3141 ctrl_x_mode = CTRL_X_FUNCTION;
3142 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003143 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003144 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003145 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003146#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003147 case 's':
3148 case Ctrl_S:
3149 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003150#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003151 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003152 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003153 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003154#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003155 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 case Ctrl_RSB:
3157 ctrl_x_mode = CTRL_X_TAGS;
3158 break;
3159#ifdef FEAT_FIND_ID
3160 case Ctrl_I:
3161 case K_S_TAB:
3162 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3163 break;
3164 case Ctrl_D:
3165 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3166 break;
3167#endif
3168 case Ctrl_V:
3169 case Ctrl_Q:
3170 ctrl_x_mode = CTRL_X_CMDLINE;
3171 break;
3172 case Ctrl_P:
3173 case Ctrl_N:
3174 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3175 * just started ^X mode, or there were enough ^X's to cancel
3176 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3177 * do normal expansion when interrupting a different mode (say
3178 * ^X^F^X^P or ^P^X^X^P, see below)
3179 * nothing changes if interrupting mode 0, (eg, the flag
3180 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003181 if (!(compl_cont_status & CONT_INTRPT))
3182 compl_cont_status |= CONT_LOCAL;
3183 else if (compl_cont_mode != 0)
3184 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 /* FALLTHROUGH */
3186 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003187 /* If we have typed at least 2 ^X's... for modes != 0, we set
3188 * compl_cont_status = 0 (eg, as if we had just started ^X
3189 * mode).
3190 * For mode 0, we set "compl_cont_mode" to an impossible
3191 * value, in both cases ^X^X can be used to restart the same
3192 * mode (avoiding ADDING mode).
3193 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3194 * 'complete' and local ^P expansions respectively.
3195 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3196 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 if (c == Ctrl_X)
3198 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003199 if (compl_cont_mode != 0)
3200 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003202 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 }
3204 ctrl_x_mode = 0;
3205 edit_submode = NULL;
3206 showmode();
3207 break;
3208 }
3209 }
3210 else if (ctrl_x_mode != 0)
3211 {
3212 /* We're already in CTRL-X mode, do we stay in it? */
3213 if (!vim_is_ctrl_x_key(c))
3214 {
3215 if (ctrl_x_mode == CTRL_X_SCROLL)
3216 ctrl_x_mode = 0;
3217 else
3218 ctrl_x_mode = CTRL_X_FINISHED;
3219 edit_submode = NULL;
3220 }
3221 showmode();
3222 }
3223
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003224 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 {
3226 /* Show error message from attempted keyword completion (probably
3227 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003228 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003230 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3231 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 || ctrl_x_mode == CTRL_X_FINISHED)
3233 {
3234 /* Get here when we have finished typing a sequence of ^N and
3235 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003236 * memory that was used, and make sure we can redo the insert. */
3237 if (compl_curr_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003239 char_u *p;
3240
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 /*
3242 * If any of the original typed text has been changed,
3243 * eg when ignorecase is set, we must add back-spaces to
3244 * the redo buffer. We add as few as necessary to delete
3245 * just the part of the original text that has changed.
3246 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003247 ptr = compl_curr_match->cp_str;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003248 p = compl_orig_text;
3249 while (*p && *p == *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003251 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 ++ptr;
3253 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003254 for (temp = 0; p[temp]; ++temp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 AppendCharToRedobuff(K_BS);
Bram Moolenaarebefac62005-12-28 22:39:57 +00003256 AppendToRedobuffLit(ptr, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 }
3258
3259#ifdef FEAT_CINDENT
3260 want_cindent = (can_cindent && cindent_on());
3261#endif
3262 /*
3263 * When completing whole lines: fix indent for 'cindent'.
3264 * Otherwise, break line if it's too long.
3265 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003266 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 {
3268#ifdef FEAT_CINDENT
3269 /* re-indent the current line */
3270 if (want_cindent)
3271 {
3272 do_c_expr_indent();
3273 want_cindent = FALSE; /* don't do it again */
3274 }
3275#endif
3276 }
3277 else
3278 {
3279 /* put the cursor on the last char, for 'tw' formatting */
3280 curwin->w_cursor.col--;
3281 if (stop_arrow() == OK)
3282 insertchar(NUL, 0, -1);
3283 curwin->w_cursor.col++;
3284 }
3285
3286 auto_format(FALSE, TRUE);
3287
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003288 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003289 * the selection without inserting anything. When
3290 * compl_enter_selects is set the Enter key does the same. */
3291 if ((c == Ctrl_Y || (compl_enter_selects
3292 && (c == CAR || c == K_KENTER || c == NL)))
3293 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003294 retval = TRUE;
3295
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003296 /* CTRL-E means completion is Ended, go back to the typed text. */
3297 if (c == Ctrl_E)
3298 {
3299 ins_compl_delete();
3300 if (compl_leader != NULL)
3301 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3302 else if (compl_first_match != NULL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003303 ins_bytes(compl_orig_text
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003304 + curwin->w_cursor.col - compl_col);
3305 retval = TRUE;
3306 }
3307
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003309 compl_started = FALSE;
3310 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 msg_clr_cmdline(); /* necessary for "noshowmode" */
3312 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003313 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 if (edit_submode != NULL)
3315 {
3316 edit_submode = NULL;
3317 showmode();
3318 }
3319
3320#ifdef FEAT_CINDENT
3321 /*
3322 * Indent now if a key was typed that is in 'cinkeys'.
3323 */
3324 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3325 do_c_expr_indent();
3326#endif
3327 }
3328 }
3329
3330 /* reset continue_* if we left expansion-mode, if we stay they'll be
3331 * (re)set properly in ins_complete() */
3332 if (!vim_is_ctrl_x_key(c))
3333 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003334 compl_cont_status = 0;
3335 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003337
3338 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339}
3340
3341/*
3342 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3343 * (depending on flag) starting from buf and looking for a non-scanned
3344 * buffer (other than curbuf). curbuf is special, if it is called with
3345 * buf=curbuf then it has to be the first call for a given flag/expansion.
3346 *
3347 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3348 */
3349 static buf_T *
3350ins_compl_next_buf(buf, flag)
3351 buf_T *buf;
3352 int flag;
3353{
3354#ifdef FEAT_WINDOWS
3355 static win_T *wp;
3356#endif
3357
3358 if (flag == 'w') /* just windows */
3359 {
3360#ifdef FEAT_WINDOWS
3361 if (buf == curbuf) /* first call for this flag/expansion */
3362 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003363 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 && wp->w_buffer->b_scanned)
3365 ;
3366 buf = wp->w_buffer;
3367#else
3368 buf = curbuf;
3369#endif
3370 }
3371 else
3372 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3373 * (unlisted buffers)
3374 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003375 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 && ((flag == 'U'
3377 ? buf->b_p_bl
3378 : (!buf->b_p_bl
3379 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003380 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 ;
3382 return buf;
3383}
3384
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003385#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003386static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003387
3388/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003389 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003390 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003391 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003392 static void
3393expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003394 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003395 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003396{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003397 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003398 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003399 char_u *funcname;
3400 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003401
Bram Moolenaare344bea2005-09-01 20:46:49 +00003402 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3403 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003404 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003405
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003406 /* Call 'completefunc' to obtain the list of matches. */
3407 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003408 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003409
Bram Moolenaare344bea2005-09-01 20:46:49 +00003410 pos = curwin->w_cursor;
3411 matchlist = call_func_retlist(funcname, 2, args, FALSE);
3412 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003413 if (matchlist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003414 return;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003415
Bram Moolenaara94bc432006-03-10 21:42:59 +00003416 ins_compl_add_list(matchlist);
3417 list_unref(matchlist);
3418}
3419#endif /* FEAT_COMPL_FUNC */
3420
Bram Moolenaar39f05632006-03-19 22:15:26 +00003421#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00003422/*
3423 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00003424 */
3425 static void
3426ins_compl_add_list(list)
3427 list_T *list;
3428{
3429 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003430 int dir = compl_direction;
3431
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003432 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003433 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003434 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00003435 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
3436 /* if dir was BACKWARD then honor it just once */
3437 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00003438 else if (did_emsg)
3439 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003440 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003441}
Bram Moolenaar39f05632006-03-19 22:15:26 +00003442
3443/*
3444 * Add a match to the list of matches from a typeval_T.
3445 * If the given string is already in the list of completions, then return
3446 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
3447 * maybe because alloc() returns NULL, then FAIL is returned.
3448 */
3449 int
3450ins_compl_add_tv(tv, dir)
3451 typval_T *tv;
3452 int dir;
3453{
3454 char_u *word;
3455 int icase = p_ic;
3456 char_u *(cptext[CPT_COUNT]);
3457
3458 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3459 {
3460 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
3461 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
3462 (char_u *)"abbr", FALSE);
3463 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
3464 (char_u *)"menu", FALSE);
3465 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
3466 (char_u *)"kind", FALSE);
3467 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
3468 (char_u *)"info", FALSE);
3469 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
3470 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
3471 }
3472 else
3473 {
3474 word = get_tv_string_chk(tv);
3475 vim_memset(cptext, 0, sizeof(cptext));
3476 }
3477 if (word == NULL || *word == NUL)
3478 return FAIL;
3479 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0);
3480}
Bram Moolenaara94bc432006-03-10 21:42:59 +00003481#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003482
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003483/*
3484 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003485 * The search starts at position "ini" in curbuf and in the direction
3486 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003487 * When "compl_started" is FALSE start at that position, otherwise continue
3488 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003489 * This may return before finding all the matches.
3490 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 */
3492 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003493ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495{
3496 static pos_T first_match_pos;
3497 static pos_T last_match_pos;
3498 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003499 static int found_all = FALSE; /* Found all matches of a
3500 certain type. */
3501 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502
Bram Moolenaar572cb562005-08-05 21:35:02 +00003503 pos_T *pos;
3504 char_u **matches;
3505 int save_p_scs;
3506 int save_p_ws;
3507 int save_p_ic;
3508 int i;
3509 int num_matches;
3510 int len;
3511 int found_new_match;
3512 int type = ctrl_x_mode;
3513 char_u *ptr;
3514 char_u *dict = NULL;
3515 int dict_f = 0;
3516 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003518 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 {
3520 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3521 ins_buf->b_scanned = 0;
3522 found_all = FALSE;
3523 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003524 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003525 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 last_match_pos = first_match_pos = *ini;
3527 }
3528
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003529 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003530 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3532 for (;;)
3533 {
3534 found_new_match = FAIL;
3535
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003536 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 * or if found_all says this entry is done. For ^X^L only use the
3538 * entries from 'complete' that look in loaded buffers. */
3539 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003540 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 {
3542 found_all = FALSE;
3543 while (*e_cpt == ',' || *e_cpt == ' ')
3544 e_cpt++;
3545 if (*e_cpt == '.' && !curbuf->b_scanned)
3546 {
3547 ins_buf = curbuf;
3548 first_match_pos = *ini;
3549 /* So that ^N can match word immediately after cursor */
3550 if (ctrl_x_mode == 0)
3551 dec(&first_match_pos);
3552 last_match_pos = first_match_pos;
3553 type = 0;
3554 }
3555 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
3556 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
3557 {
3558 /* Scan a buffer, but not the current one. */
3559 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
3560 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003561 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 first_match_pos.col = last_match_pos.col = 0;
3563 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
3564 last_match_pos.lnum = 0;
3565 type = 0;
3566 }
3567 else /* unloaded buffer, scan like dictionary */
3568 {
3569 found_all = TRUE;
3570 if (ins_buf->b_fname == NULL)
3571 continue;
3572 type = CTRL_X_DICTIONARY;
3573 dict = ins_buf->b_fname;
3574 dict_f = DICT_EXACT;
3575 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00003576 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 ins_buf->b_fname == NULL
3578 ? buf_spname(ins_buf)
3579 : ins_buf->b_sfname == NULL
3580 ? (char *)ins_buf->b_fname
3581 : (char *)ins_buf->b_sfname);
3582 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3583 }
3584 else if (*e_cpt == NUL)
3585 break;
3586 else
3587 {
3588 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3589 type = -1;
3590 else if (*e_cpt == 'k' || *e_cpt == 's')
3591 {
3592 if (*e_cpt == 'k')
3593 type = CTRL_X_DICTIONARY;
3594 else
3595 type = CTRL_X_THESAURUS;
3596 if (*++e_cpt != ',' && *e_cpt != NUL)
3597 {
3598 dict = e_cpt;
3599 dict_f = DICT_FIRST;
3600 }
3601 }
3602#ifdef FEAT_FIND_ID
3603 else if (*e_cpt == 'i')
3604 type = CTRL_X_PATH_PATTERNS;
3605 else if (*e_cpt == 'd')
3606 type = CTRL_X_PATH_DEFINES;
3607#endif
3608 else if (*e_cpt == ']' || *e_cpt == 't')
3609 {
3610 type = CTRL_X_TAGS;
3611 sprintf((char*)IObuff, _("Scanning tags."));
3612 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3613 }
3614 else
3615 type = -1;
3616
3617 /* in any case e_cpt is advanced to the next entry */
3618 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
3619
3620 found_all = TRUE;
3621 if (type == -1)
3622 continue;
3623 }
3624 }
3625
3626 switch (type)
3627 {
3628 case -1:
3629 break;
3630#ifdef FEAT_FIND_ID
3631 case CTRL_X_PATH_PATTERNS:
3632 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003633 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003634 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003636 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3638 (linenr_T)1, (linenr_T)MAXLNUM);
3639 break;
3640#endif
3641
3642 case CTRL_X_DICTIONARY:
3643 case CTRL_X_THESAURUS:
3644 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00003645 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 : (type == CTRL_X_THESAURUS
3647 ? (*curbuf->b_p_tsr == NUL
3648 ? p_tsr
3649 : curbuf->b_p_tsr)
3650 : (*curbuf->b_p_dict == NUL
3651 ? p_dict
3652 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003653 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00003654 dict != NULL ? dict_f
3655 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 dict = NULL;
3657 break;
3658
3659 case CTRL_X_TAGS:
3660 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
3661 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003662 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663
3664 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003665 * of matches is found when compl_pattern is empty */
3666 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
3668 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
3669 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3670 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003671 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 }
3673 p_ic = save_p_ic;
3674 break;
3675
3676 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003677 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
3679 {
3680
3681 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003682 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003683 ins_compl_add_matches(num_matches, matches,
3684#ifdef CASE_INSENSITIVE_FILENAME
3685 TRUE
3686#else
3687 FALSE
3688#endif
3689 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 }
3691 break;
3692
3693 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003694 if (expand_cmdline(&compl_xp, compl_pattern,
3695 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003697 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 break;
3699
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003700#ifdef FEAT_COMPL_FUNC
3701 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003702 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003703 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003704 break;
3705#endif
3706
Bram Moolenaar488c6512005-08-11 20:09:58 +00003707 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003708#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00003709 num_matches = expand_spelling(first_match_pos.lnum,
3710 first_match_pos.col, compl_pattern, &matches);
3711 if (num_matches > 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003712 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar488c6512005-08-11 20:09:58 +00003713#endif
3714 break;
3715
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 default: /* normal ^P/^N and ^X^L */
3717 /*
3718 * If 'infercase' is set, don't use 'smartcase' here
3719 */
3720 save_p_scs = p_scs;
3721 if (ins_buf->b_p_inf)
3722 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003723
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 /* buffers other than curbuf are scanned from the beginning or the
3725 * end but never from the middle, thus setting nowrapscan in this
3726 * buffers is a good idea, on the other hand, we always set
3727 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
3728 save_p_ws = p_ws;
3729 if (ins_buf != curbuf)
3730 p_ws = FALSE;
3731 else if (*e_cpt == '.')
3732 p_ws = TRUE;
3733 for (;;)
3734 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003735 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003737 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
3738 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003740 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003742 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003744 found_new_match = searchit(NULL, ins_buf, pos,
3745 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003746 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003747 RE_LAST, (linenr_T)0);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003748 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003750 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003751 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 first_match_pos = *pos;
3753 last_match_pos = *pos;
3754 }
3755 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003756 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 found_new_match = FAIL;
3758 if (found_new_match == FAIL)
3759 {
3760 if (ins_buf == curbuf)
3761 found_all = TRUE;
3762 break;
3763 }
3764
3765 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003766 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 && ini->lnum == pos->lnum
3768 && ini->col == pos->col)
3769 continue;
3770 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
3771 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3772 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003773 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 {
3775 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3776 continue;
3777 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3778 if (!p_paste)
3779 ptr = skipwhite(ptr);
3780 }
3781 len = (int)STRLEN(ptr);
3782 }
3783 else
3784 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003785 char_u *tmp_ptr = ptr;
3786
3787 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003789 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 /* Skip if already inside a word. */
3791 if (vim_iswordp(tmp_ptr))
3792 continue;
3793 /* Find start of next word. */
3794 tmp_ptr = find_word_start(tmp_ptr);
3795 }
3796 /* Find end of this word. */
3797 tmp_ptr = find_word_end(tmp_ptr);
3798 len = (int)(tmp_ptr - ptr);
3799
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003800 if ((compl_cont_status & CONT_ADDING)
3801 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 {
3803 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3804 {
3805 /* Try next line, if any. the new word will be
3806 * "join" as if the normal command "J" was used.
3807 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003808 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 * works -- Acevedo */
3810 STRNCPY(IObuff, ptr, len);
3811 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3812 tmp_ptr = ptr = skipwhite(ptr);
3813 /* Find start of next word. */
3814 tmp_ptr = find_word_start(tmp_ptr);
3815 /* Find end of next word. */
3816 tmp_ptr = find_word_end(tmp_ptr);
3817 if (tmp_ptr > ptr)
3818 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003819 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003821 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 IObuff[len++] = ' ';
3823 /* IObuf =~ "\k.* ", thus len >= 2 */
3824 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003825 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 || (vim_strchr(p_cpo, CPO_JOINSP)
3827 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003828 && (IObuff[len - 2] == '?'
3829 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 IObuff[len++] = ' ';
3831 }
3832 /* copy as much as posible of the new word */
3833 if (tmp_ptr - ptr >= IOSIZE - len)
3834 tmp_ptr = ptr + IOSIZE - len - 1;
3835 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3836 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00003837 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 }
3839 IObuff[len] = NUL;
3840 ptr = IObuff;
3841 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003842 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 continue;
3844 }
3845 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003846 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003847 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003848 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 {
3850 found_new_match = OK;
3851 break;
3852 }
3853 }
3854 p_scs = save_p_scs;
3855 p_ws = save_p_ws;
3856 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003857
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003858 /* check if compl_curr_match has changed, (e.g. other type of
3859 * expansion added somenthing) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003860 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 found_new_match = OK;
3862
3863 /* break the loop for specialized modes (use 'complete' just for the
3864 * generic ctrl_x_mode == 0) or when we've found a new match */
3865 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003866 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003867 {
3868 if (got_int)
3869 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003870 /* Fill the popup menu as soon as possible. */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003871 if (pum_wanted() && type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003872 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003873
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003874 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
3875 || compl_interrupted)
3876 break;
3877 compl_started = TRUE;
3878 }
3879 else
3880 {
3881 /* Mark a buffer scanned when it has been scanned completely */
3882 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
3883 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003885 compl_started = FALSE;
3886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003888 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889
3890 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
3891 && *e_cpt == NUL) /* Got to end of 'complete' */
3892 found_new_match = FAIL;
3893
3894 i = -1; /* total of matches, unknown */
3895 if (found_new_match == FAIL
3896 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
3897 i = ins_compl_make_cyclic();
3898
3899 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003900 * just been made cyclic then we have to move compl_curr_match to the next
3901 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003902 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
3903 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003904 if (compl_curr_match == NULL)
3905 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 return i;
3907}
3908
3909/* Delete the old text being completed. */
3910 static void
3911ins_compl_delete()
3912{
3913 int i;
3914
3915 /*
3916 * In insert mode: Delete the typed part.
3917 * In replace mode: Put the old characters back, if any.
3918 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003919 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 backspace_until_column(i);
3921 changed_cline_bef_curs();
3922}
3923
3924/* Insert the new text being completed. */
3925 static void
3926ins_compl_insert()
3927{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003928 ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003929 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3930 compl_used_match = FALSE;
3931 else
3932 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933}
3934
3935/*
3936 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003937 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
3938 * get more completions. If it is FALSE, then we just do nothing when there
3939 * are no more completions in a given direction. The latter case is used when
3940 * we are still in the middle of finding completions, to allow browsing
3941 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 * Return the total number of matches, or -1 if still unknown -- webb.
3943 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003944 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
3945 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 *
3947 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00003948 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
3949 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 */
3951 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003952ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003954 int count; /* repeat completion this many times; should
3955 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003956 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957{
3958 int num_matches = -1;
3959 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003960 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00003961 compl_T *found_compl = NULL;
3962 int found_end = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003964 if (compl_leader != NULL
3965 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003967 /* Set "compl_shown_match" to the actually shown match, it may differ
3968 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003969 while (!ins_compl_equal(compl_shown_match,
3970 compl_leader, STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003971 && compl_shown_match->cp_next != NULL
3972 && compl_shown_match->cp_next != compl_first_match)
3973 compl_shown_match = compl_shown_match->cp_next;
3974 }
3975
3976 if (allow_get_expansion && insert_match
3977 && (!compl_get_longest || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 /* Delete old text to be replaced */
3979 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003980
Bram Moolenaare3226be2005-12-18 22:10:00 +00003981 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
3982 * around. */
3983 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00003985 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003987 if (compl_pending != 0)
3988 --compl_pending;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003989 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00003990 found_end = (compl_first_match != NULL
3991 && (compl_shown_match->cp_next == compl_first_match
3992 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00003993 }
3994 else if (compl_shows_dir == BACKWARD
3995 && compl_shown_match->cp_prev != NULL)
3996 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003997 if (compl_pending != 0)
3998 ++compl_pending;
Bram Moolenaara6557602006-02-04 22:43:20 +00003999 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004000 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004001 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 }
4003 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004004 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004005 if (compl_shows_dir == BACKWARD)
4006 --compl_pending;
4007 else
4008 ++compl_pending;
Bram Moolenaara6557602006-02-04 22:43:20 +00004009 if (!allow_get_expansion)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004010 return -1;
Bram Moolenaara6557602006-02-04 22:43:20 +00004011
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004012 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004013 if (compl_pending != 0 && compl_direction == compl_shows_dir)
Bram Moolenaara6557602006-02-04 22:43:20 +00004014 compl_shown_match = compl_curr_match;
4015 found_end = FALSE;
4016 }
4017 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4018 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004019 && !ins_compl_equal(compl_shown_match,
4020 compl_leader, STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004021 ++todo;
4022 else
4023 /* Remember a matching item. */
4024 found_compl = compl_shown_match;
4025
4026 /* Stop at the end of the list when we found a usable match. */
4027 if (found_end)
4028 {
4029 if (found_compl != NULL)
4030 {
4031 compl_shown_match = found_compl;
4032 break;
4033 }
4034 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 }
4037
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004038 /* Insert the text of the new completion, or the compl_leader. */
4039 if (insert_match)
4040 {
4041 if (!compl_get_longest || compl_used_match)
4042 ins_compl_insert();
4043 else
4044 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
4045 }
4046 else
4047 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048
4049 if (!allow_get_expansion)
4050 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004051 /* may undisplay the popup menu first */
4052 ins_compl_upd_pum();
4053
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004054 /* redraw to show the user what was inserted */
4055 update_screen(0);
4056
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004057 /* display the updated popup menu */
4058 ins_compl_show_pum();
4059
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 /* Delete old text to be replaced, since we're still searching and
4061 * don't want to match ourselves! */
4062 ins_compl_delete();
4063 }
4064
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004065 /* Enter will select a match when the match wasn't inserted and the popup
4066 * menu is visislbe. */
4067 compl_enter_selects = !insert_match && compl_match_array != NULL;
4068
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 /*
4070 * Show the file name for the match (if any)
4071 * Truncate the file name to avoid a wait for return.
4072 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004073 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 {
4075 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004076 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 if (i <= 0)
4078 i = 0;
4079 else
4080 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004081 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 msg(IObuff);
4083 redraw_cmdline = FALSE; /* don't overwrite! */
4084 }
4085
4086 return num_matches;
4087}
4088
4089/*
4090 * Call this while finding completions, to check whether the user has hit a key
4091 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004092 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004094 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 */
4096 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004097ins_compl_check_keys(frequency)
4098 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099{
4100 static int count = 0;
4101
4102 int c;
4103
4104 /* Don't check when reading keys from a script. That would break the test
4105 * scripts */
4106 if (using_script())
4107 return;
4108
4109 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004110 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 return;
4112 count = 0;
4113
4114 ++no_mapping;
4115 c = vpeekc_any();
4116 --no_mapping;
4117 if (c != NUL)
4118 {
4119 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4120 {
4121 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004122 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004123 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4124 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 }
4126 else if (c != Ctrl_R)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004127 compl_interrupted = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004129 if (compl_pending != 0 && !got_int)
4130 (void)ins_compl_next(FALSE, compl_pending > 0
4131 ? compl_pending : -compl_pending, TRUE);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004132}
4133
4134/*
4135 * Decide the direction of Insert mode complete from the key typed.
4136 * Returns BACKWARD or FORWARD.
4137 */
4138 static int
4139ins_compl_key2dir(c)
4140 int c;
4141{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004142 if (c == Ctrl_P || c == Ctrl_L
4143 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4144 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004145 return BACKWARD;
4146 return FORWARD;
4147}
4148
4149/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004150 * Return TRUE for keys that are used for completion only when the popup menu
4151 * is visible.
4152 */
4153 static int
4154ins_compl_pum_key(c)
4155 int c;
4156{
4157 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004158 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4159 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004160}
4161
4162/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004163 * Decide the number of completions to move forward.
4164 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4165 */
4166 static int
4167ins_compl_key2count(c)
4168 int c;
4169{
4170 int h;
4171
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004172 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004173 {
4174 h = pum_get_height();
4175 if (h > 3)
4176 h -= 2; /* keep some context */
4177 return h;
4178 }
4179 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180}
4181
4182/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004183 * Return TRUE if completion with "c" should insert the match, FALSE if only
4184 * to change the currently selected completion.
4185 */
4186 static int
4187ins_compl_use_match(c)
4188 int c;
4189{
4190 switch (c)
4191 {
4192 case K_UP:
4193 case K_DOWN:
4194 case K_PAGEDOWN:
4195 case K_KPAGEDOWN:
4196 case K_S_DOWN:
4197 case K_PAGEUP:
4198 case K_KPAGEUP:
4199 case K_S_UP:
4200 return FALSE;
4201 }
4202 return TRUE;
4203}
4204
4205/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 * Do Insert mode completion.
4207 * Called when character "c" was typed, which has a meaning for completion.
4208 * Returns OK if completion was done, FAIL if something failed (out of mem).
4209 */
4210 static int
4211ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004212 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004214 char_u *line;
4215 int startcol = 0; /* column where searched text starts */
4216 colnr_T curs_col; /* cursor column */
4217 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218
Bram Moolenaare3226be2005-12-18 22:10:00 +00004219 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004220 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 {
4222 /* First time we hit ^N or ^P (in a row, I mean) */
4223
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 did_ai = FALSE;
4225#ifdef FEAT_SMARTINDENT
4226 did_si = FALSE;
4227 can_si = FALSE;
4228 can_si_back = FALSE;
4229#endif
4230 if (stop_arrow() == FAIL)
4231 return FAIL;
4232
4233 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004234 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004235 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236
4237 /* if this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004238 * "compl_startpos" to the cursor as a pattern to add a new word
4239 * instead of expand the one before the cursor, in word-wise if
4240 * "compl_startpos"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 * is not in the same line as the cursor then fix it (the line has
4242 * been split because it was longer than 'tw'). if SOL is set then
4243 * skip the previous pattern, a word at the beginning of the line has
4244 * been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004245 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4246 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 {
4248 /*
4249 * it is a continued search
4250 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004251 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4253 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4254 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004255 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004257 /* line (probably) wrapped, set compl_startpos to the
4258 * first non_blank in the line, if it is not a wordchar
4259 * include it to get a better pattern, but then we don't
4260 * want the "\\<" prefix, check it bellow */
4261 compl_col = (colnr_T)(skipwhite(line) - line);
4262 compl_startpos.col = compl_col;
4263 compl_startpos.lnum = curwin->w_cursor.lnum;
4264 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 }
4266 else
4267 {
4268 /* S_IPOS was set when we inserted a word that was at the
4269 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004270 * mode but first we need to redefine compl_startpos */
4271 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004273 compl_cont_status |= CONT_SOL;
4274 compl_startpos.col = (colnr_T)(skipwhite(
4275 line + compl_length
4276 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004278 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004280 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004281 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 * have enough space? just being paranoic */
4283#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004284 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004286 compl_cont_status &= ~CONT_SOL;
4287 compl_length = (IOSIZE - MIN_SPACE);
4288 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004290 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4291 if (compl_length < 1)
4292 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 }
4294 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004295 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004297 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 }
4299 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004300 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004302 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004304 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004306 compl_cont_status = 0;
4307 compl_cont_status |= CONT_N_ADDS;
4308 compl_startpos = curwin->w_cursor;
4309 startcol = (int)curs_col;
4310 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 }
4312
4313 /* Work out completion pattern and original text -- webb */
4314 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4315 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004316 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4318 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004319 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004321 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004323 compl_col += ++startcol;
4324 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 }
4326 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004327 compl_pattern = str_foldcase(line + compl_col,
4328 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004330 compl_pattern = vim_strnsave(line + compl_col,
4331 compl_length);
4332 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 return FAIL;
4334 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004335 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 {
4337 char_u *prefix = (char_u *)"\\<";
4338
4339 /* we need 3 extra chars, 1 for the NUL and
4340 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004341 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4342 compl_length) + 3);
4343 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004345 if (!vim_iswordp(line + compl_col)
4346 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 && (
4348#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004349 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004351 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352#endif
4353 )))
4354 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004355 STRCPY((char *)compl_pattern, prefix);
4356 (void)quote_meta(compl_pattern + STRLEN(prefix),
4357 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004359 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004361 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004363 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364#endif
4365 )
4366 {
4367 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004368 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4369 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004371 compl_col += curs_col;
4372 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 }
4374 else
4375 {
4376#ifdef FEAT_MBYTE
4377 /* Search the point of change class of multibyte character
4378 * or not a word single byte character backward. */
4379 if (has_mbyte)
4380 {
4381 int base_class;
4382 int head_off;
4383
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004384 startcol -= (*mb_head_off)(line, line + startcol);
4385 base_class = mb_get_class(line + startcol);
4386 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004388 head_off = (*mb_head_off)(line, line + startcol);
4389 if (base_class != mb_get_class(line + startcol
4390 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004392 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 }
4394 }
4395 else
4396#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004397 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004399 compl_col += ++startcol;
4400 compl_length = (int)curs_col - startcol;
4401 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 {
4403 /* Only match word with at least two chars -- webb
4404 * there's no need to call quote_meta,
4405 * alloc(7) is enough -- Acevedo
4406 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004407 compl_pattern = alloc(7);
4408 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004410 STRCPY((char *)compl_pattern, "\\<");
4411 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4412 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 }
4414 else
4415 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004416 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4417 compl_length) + 3);
4418 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004420 STRCPY((char *)compl_pattern, "\\<");
4421 (void)quote_meta(compl_pattern + 2, line + compl_col,
4422 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 }
4424 }
4425 }
4426 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4427 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004428 compl_col = skipwhite(line) - line;
4429 compl_length = (int)curs_col - (int)compl_col;
4430 if (compl_length < 0) /* cursor in indent: empty pattern */
4431 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004433 compl_pattern = str_foldcase(line + compl_col, compl_length,
4434 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004436 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4437 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 return FAIL;
4439 }
4440 else if (ctrl_x_mode == CTRL_X_FILES)
4441 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004442 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004444 compl_col += ++startcol;
4445 compl_length = (int)curs_col - startcol;
4446 compl_pattern = addstar(line + compl_col, compl_length,
4447 EXPAND_FILES);
4448 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 return FAIL;
4450 }
4451 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4452 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004453 compl_pattern = vim_strnsave(line, curs_col);
4454 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004456 set_cmd_context(&compl_xp, compl_pattern,
4457 (int)STRLEN(compl_pattern), curs_col);
4458 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4459 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004461 startcol = (int)(compl_xp.xp_pattern - compl_pattern);
4462 compl_col = startcol;
4463 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004465 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004466 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00004467#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004468 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00004469 * Call user defined function 'completefunc' with "a:findstart"
4470 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004471 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00004472 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004473 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004474 char_u *funcname;
4475 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004476
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004477 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004478 * string */
4479 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4480 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4481 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004482 {
4483 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
4484 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004485 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004486 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004487
4488 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004489 args[1] = NULL;
4490 pos = curwin->w_cursor;
4491 col = call_func_retnr(funcname, 2, args, FALSE);
4492 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004493
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004494 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004495 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004496 compl_col = col;
4497 if ((colnr_T)compl_col > curs_col)
4498 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004499
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004500 /* Setup variables for completion. Need to obtain "line" again,
4501 * it may have become invalid. */
4502 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004503 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004504 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4505 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004506#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004507 return FAIL;
4508 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00004509 else if (ctrl_x_mode == CTRL_X_SPELL)
4510 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004511#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004512 if (spell_bad_len > 0)
4513 compl_col = curs_col - spell_bad_len;
4514 else
4515 compl_col = spell_word_start(startcol);
4516 if (compl_col >= (colnr_T)startcol)
Bram Moolenaar488c6512005-08-11 20:09:58 +00004517 return FAIL;
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00004518 spell_expand_check_cap(compl_col);
Bram Moolenaare2f98b92006-03-29 21:18:24 +00004519 /* Need to obtain "line" again, it may have become invalid. */
4520 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004521 compl_length = (int)curs_col - compl_col;
4522 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4523 if (compl_pattern == NULL)
4524#endif
4525 return FAIL;
4526 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004527 else
4528 {
4529 EMSG2(_(e_intern2), "ins_complete()");
4530 return FAIL;
4531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004533 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 {
4535 edit_submode_pre = (char_u *)_(" Adding");
4536 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4537 {
4538 /* Insert a new line, keep indentation but ignore 'comments' */
4539#ifdef FEAT_COMMENTS
4540 char_u *old = curbuf->b_p_com;
4541
4542 curbuf->b_p_com = (char_u *)"";
4543#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004544 compl_startpos.lnum = curwin->w_cursor.lnum;
4545 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 ins_eol('\r');
4547#ifdef FEAT_COMMENTS
4548 curbuf->b_p_com = old;
4549#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004550 compl_length = 0;
4551 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 }
4553 }
4554 else
4555 {
4556 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004557 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 }
4559
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004560 if (compl_cont_status & CONT_LOCAL)
4561 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 else
4563 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4564
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004565 /* Always add completion for the original text. */
4566 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004567 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4568 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004569 -1, FALSE, NULL, NULL, 0, ORIGINAL_TEXT) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004571 vim_free(compl_pattern);
4572 compl_pattern = NULL;
4573 vim_free(compl_orig_text);
4574 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 return FAIL;
4576 }
4577
4578 /* showmode might reset the internal line pointers, so it must
4579 * be called before line = ml_get(), or when this address is no
4580 * longer needed. -- Acevedo.
4581 */
4582 edit_submode_extra = (char_u *)_("-- Searching...");
4583 edit_submode_highl = HLF_COUNT;
4584 showmode();
4585 edit_submode_extra = NULL;
4586 out_flush();
4587 }
4588
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004589 compl_shown_match = compl_curr_match;
4590 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591
4592 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004593 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004595 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004597 /* may undisplay the popup menu */
4598 ins_compl_upd_pum();
4599
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004600 if (n > 1) /* all matches have been found */
4601 compl_matches = n;
4602 compl_curr_match = compl_shown_match;
4603 compl_direction = compl_shows_dir;
4604 compl_interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605
4606 /* eat the ESC to avoid leaving insert mode */
4607 if (got_int && !global_busy)
4608 {
4609 (void)vgetc();
4610 got_int = FALSE;
4611 }
4612
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004613 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004614 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004616 edit_submode_extra = (compl_cont_status & CONT_ADDING)
4617 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
4619 edit_submode_highl = HLF_E;
4620 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4621 * because we couldn't expand anything at first place, but if we used
4622 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4623 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004624 if ( compl_length > 1
4625 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 || (ctrl_x_mode != 0
4627 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
4628 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004629 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 }
4631
Bram Moolenaar572cb562005-08-05 21:35:02 +00004632 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004633 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004635 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636
4637 if (edit_submode_extra == NULL)
4638 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004639 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 {
4641 edit_submode_extra = (char_u *)_("Back at original");
4642 edit_submode_highl = HLF_W;
4643 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004644 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 {
4646 edit_submode_extra = (char_u *)_("Word from other line");
4647 edit_submode_highl = HLF_COUNT;
4648 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004649 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 {
4651 edit_submode_extra = (char_u *)_("The only match");
4652 edit_submode_highl = HLF_COUNT;
4653 }
4654 else
4655 {
4656 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004657 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004659 int number = 0;
4660 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004662 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 {
4664 /* search backwards for the first valid (!= -1) number.
4665 * This should normally succeed already at the first loop
4666 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004667 for (match = compl_curr_match->cp_prev; match != NULL
4668 && match != compl_first_match;
4669 match = match->cp_prev)
4670 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004672 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 break;
4674 }
4675 if (match != NULL)
4676 /* go up and assign all numbers which are not assigned
4677 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004678 for (match = match->cp_next;
4679 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004680 match = match->cp_next)
4681 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 }
4683 else /* BACKWARD */
4684 {
4685 /* search forwards (upwards) for the first valid (!= -1)
4686 * number. This should normally succeed already at the
4687 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004688 for (match = compl_curr_match->cp_next; match != NULL
4689 && match != compl_first_match;
4690 match = match->cp_next)
4691 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004693 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 break;
4695 }
4696 if (match != NULL)
4697 /* go down and assign all numbers which are not
4698 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004699 for (match = match->cp_prev; match
4700 && match->cp_number == -1;
4701 match = match->cp_prev)
4702 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 }
4704 }
4705
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004706 /* The match should always have a sequence number now, this is
4707 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004708 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 {
4710 /* Space for 10 text chars. + 2x10-digit no.s */
4711 static char_u match_ref[31];
4712
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004713 if (compl_matches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 sprintf((char *)IObuff, _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004715 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004717 sprintf((char *)IObuff, _("match %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004718 compl_curr_match->cp_number);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004719 vim_strncpy(match_ref, IObuff, 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 edit_submode_extra = match_ref;
4721 edit_submode_highl = HLF_R;
4722 if (dollar_vcol)
4723 curs_columns(FALSE);
4724 }
4725 }
4726 }
4727
4728 /* Show a message about what (completion) mode we're in. */
4729 showmode();
4730 if (edit_submode_extra != NULL)
4731 {
4732 if (!p_smd)
4733 msg_attr(edit_submode_extra,
4734 edit_submode_highl < HLF_COUNT
4735 ? hl_attr(edit_submode_highl) : 0);
4736 }
4737 else
4738 msg_clr_cmdline(); /* necessary for "noshowmode" */
4739
Bram Moolenaara94bc432006-03-10 21:42:59 +00004740 /* RedrawingDisabled may be set when invoked through complete(). */
4741 n = RedrawingDisabled;
4742 RedrawingDisabled = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004743 ins_compl_show_pum();
Bram Moolenaara94bc432006-03-10 21:42:59 +00004744 setcursor();
4745 RedrawingDisabled = n;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004746
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 return OK;
4748}
4749
4750/*
4751 * Looks in the first "len" chars. of "src" for search-metachars.
4752 * If dest is not NULL the chars. are copied there quoting (with
4753 * a backslash) the metachars, and dest would be NUL terminated.
4754 * Returns the length (needed) of dest
4755 */
4756 static int
4757quote_meta(dest, src, len)
4758 char_u *dest;
4759 char_u *src;
4760 int len;
4761{
4762 int m;
4763
4764 for (m = len; --len >= 0; src++)
4765 {
4766 switch (*src)
4767 {
4768 case '.':
4769 case '*':
4770 case '[':
4771 if (ctrl_x_mode == CTRL_X_DICTIONARY
4772 || ctrl_x_mode == CTRL_X_THESAURUS)
4773 break;
4774 case '~':
4775 if (!p_magic) /* quote these only if magic is set */
4776 break;
4777 case '\\':
4778 if (ctrl_x_mode == CTRL_X_DICTIONARY
4779 || ctrl_x_mode == CTRL_X_THESAURUS)
4780 break;
4781 case '^': /* currently it's not needed. */
4782 case '$':
4783 m++;
4784 if (dest != NULL)
4785 *dest++ = '\\';
4786 break;
4787 }
4788 if (dest != NULL)
4789 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004790# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 /* Copy remaining bytes of a multibyte character. */
4792 if (has_mbyte)
4793 {
4794 int i, mb_len;
4795
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004796 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 if (mb_len > 0 && len >= mb_len)
4798 for (i = 0; i < mb_len; ++i)
4799 {
4800 --len;
4801 ++src;
4802 if (dest != NULL)
4803 *dest++ = *src;
4804 }
4805 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004806# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 }
4808 if (dest != NULL)
4809 *dest = NUL;
4810
4811 return m;
4812}
4813#endif /* FEAT_INS_EXPAND */
4814
4815/*
4816 * Next character is interpreted literally.
4817 * A one, two or three digit decimal number is interpreted as its byte value.
4818 * If one or two digits are entered, the next character is given to vungetc().
4819 * For Unicode a character > 255 may be returned.
4820 */
4821 int
4822get_literal()
4823{
4824 int cc;
4825 int nc;
4826 int i;
4827 int hex = FALSE;
4828 int octal = FALSE;
4829#ifdef FEAT_MBYTE
4830 int unicode = 0;
4831#endif
4832
4833 if (got_int)
4834 return Ctrl_C;
4835
4836#ifdef FEAT_GUI
4837 /*
4838 * In GUI there is no point inserting the internal code for a special key.
4839 * It is more useful to insert the string "<KEY>" instead. This would
4840 * probably be useful in a text window too, but it would not be
4841 * vi-compatible (maybe there should be an option for it?) -- webb
4842 */
4843 if (gui.in_use)
4844 ++allow_keys;
4845#endif
4846#ifdef USE_ON_FLY_SCROLL
4847 dont_scroll = TRUE; /* disallow scrolling here */
4848#endif
4849 ++no_mapping; /* don't map the next key hits */
4850 cc = 0;
4851 i = 0;
4852 for (;;)
4853 {
4854 do
4855 nc = safe_vgetc();
4856 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
4857 || nc == K_HOR_SCROLLBAR);
4858#ifdef FEAT_CMDL_INFO
4859 if (!(State & CMDLINE)
4860# ifdef FEAT_MBYTE
4861 && MB_BYTE2LEN_CHECK(nc) == 1
4862# endif
4863 )
4864 add_to_showcmd(nc);
4865#endif
4866 if (nc == 'x' || nc == 'X')
4867 hex = TRUE;
4868 else if (nc == 'o' || nc == 'O')
4869 octal = TRUE;
4870#ifdef FEAT_MBYTE
4871 else if (nc == 'u' || nc == 'U')
4872 unicode = nc;
4873#endif
4874 else
4875 {
4876 if (hex
4877#ifdef FEAT_MBYTE
4878 || unicode != 0
4879#endif
4880 )
4881 {
4882 if (!vim_isxdigit(nc))
4883 break;
4884 cc = cc * 16 + hex2nr(nc);
4885 }
4886 else if (octal)
4887 {
4888 if (nc < '0' || nc > '7')
4889 break;
4890 cc = cc * 8 + nc - '0';
4891 }
4892 else
4893 {
4894 if (!VIM_ISDIGIT(nc))
4895 break;
4896 cc = cc * 10 + nc - '0';
4897 }
4898
4899 ++i;
4900 }
4901
4902 if (cc > 255
4903#ifdef FEAT_MBYTE
4904 && unicode == 0
4905#endif
4906 )
4907 cc = 255; /* limit range to 0-255 */
4908 nc = 0;
4909
4910 if (hex) /* hex: up to two chars */
4911 {
4912 if (i >= 2)
4913 break;
4914 }
4915#ifdef FEAT_MBYTE
4916 else if (unicode) /* Unicode: up to four or eight chars */
4917 {
4918 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
4919 break;
4920 }
4921#endif
4922 else if (i >= 3) /* decimal or octal: up to three chars */
4923 break;
4924 }
4925 if (i == 0) /* no number entered */
4926 {
4927 if (nc == K_ZERO) /* NUL is stored as NL */
4928 {
4929 cc = '\n';
4930 nc = 0;
4931 }
4932 else
4933 {
4934 cc = nc;
4935 nc = 0;
4936 }
4937 }
4938
4939 if (cc == 0) /* NUL is stored as NL */
4940 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00004941#ifdef FEAT_MBYTE
4942 if (enc_dbcs && (cc & 0xff) == 0)
4943 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
4944 second byte will cause trouble! */
4945#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946
4947 --no_mapping;
4948#ifdef FEAT_GUI
4949 if (gui.in_use)
4950 --allow_keys;
4951#endif
4952 if (nc)
4953 vungetc(nc);
4954 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
4955 return cc;
4956}
4957
4958/*
4959 * Insert character, taking care of special keys and mod_mask
4960 */
4961 static void
4962insert_special(c, allow_modmask, ctrlv)
4963 int c;
4964 int allow_modmask;
4965 int ctrlv; /* c was typed after CTRL-V */
4966{
4967 char_u *p;
4968 int len;
4969
4970 /*
4971 * Special function key, translate into "<Key>". Up to the last '>' is
4972 * inserted with ins_str(), so as not to replace characters in replace
4973 * mode.
4974 * Only use mod_mask for special keys, to avoid things like <S-Space>,
4975 * unless 'allow_modmask' is TRUE.
4976 */
4977#ifdef MACOS
4978 /* Command-key never produces a normal key */
4979 if (mod_mask & MOD_MASK_CMD)
4980 allow_modmask = TRUE;
4981#endif
4982 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
4983 {
4984 p = get_special_key_name(c, mod_mask);
4985 len = (int)STRLEN(p);
4986 c = p[len - 1];
4987 if (len > 2)
4988 {
4989 if (stop_arrow() == FAIL)
4990 return;
4991 p[len - 1] = NUL;
4992 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00004993 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 ctrlv = FALSE;
4995 }
4996 }
4997 if (stop_arrow() == OK)
4998 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
4999}
5000
5001/*
5002 * Special characters in this context are those that need processing other
5003 * than the simple insertion that can be performed here. This includes ESC
5004 * which terminates the insert, and CR/NL which need special processing to
5005 * open up a new line. This routine tries to optimize insertions performed by
5006 * the "redo", "undo" or "put" commands, so it needs to know when it should
5007 * stop and defer processing to the "normal" mechanism.
5008 * '0' and '^' are special, because they can be followed by CTRL-D.
5009 */
5010#ifdef EBCDIC
5011# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5012#else
5013# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5014#endif
5015
5016#ifdef FEAT_MBYTE
5017# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5018#else
5019# define WHITECHAR(cc) vim_iswhite(cc)
5020#endif
5021
5022 void
5023insertchar(c, flags, second_indent)
5024 int c; /* character to insert or NUL */
5025 int flags; /* INSCHAR_FORMAT, etc. */
5026 int second_indent; /* indent for second line if >= 0 */
5027{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 int textwidth;
5029#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033
5034 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5035 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036
5037 /*
5038 * Try to break the line in two or more pieces when:
5039 * - Always do this if we have been called to do formatting only.
5040 * - Always do this when 'formatoptions' has the 'a' flag and the line
5041 * ends in white space.
5042 * - Otherwise:
5043 * - Don't do this if inserting a blank
5044 * - Don't do this if an existing character is being replaced, unless
5045 * we're in VREPLACE mode.
5046 * - Do this if the cursor is not on the line where insert started
5047 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5048 * before the insert.
5049 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5050 * before 'textwidth'
5051 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005052 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 && ((flags & INSCHAR_FORMAT)
5054 || (!vim_iswhite(c)
5055 && !((State & REPLACE_FLAG)
5056#ifdef FEAT_VREPLACE
5057 && !(State & VREPLACE_FLAG)
5058#endif
5059 && *ml_get_cursor() != NUL)
5060 && (curwin->w_cursor.lnum != Insstart.lnum
5061 || ((!has_format_option(FO_INS_LONG)
5062 || Insstart_textlen <= (colnr_T)textwidth)
5063 && (!fo_ins_blank
5064 || Insstart_blank_vcol <= (colnr_T)textwidth
5065 ))))))
5066 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005067 /* Format with 'formatexpr' when it's set. Use internal formatting
5068 * when 'formatexpr' isn't set or it returns non-zero. */
5069#if defined(FEAT_EVAL)
5070 if (*curbuf->b_p_fex == NUL
5071 || fex_format(curwin->w_cursor.lnum, 1L) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005073 internal_format(textwidth, second_indent, flags, c == NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005075
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 if (c == NUL) /* only formatting was wanted */
5077 return;
5078
5079#ifdef FEAT_COMMENTS
5080 /* Check whether this character should end a comment. */
5081 if (did_ai && (int)c == end_comment_pending)
5082 {
5083 char_u *line;
5084 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5085 int middle_len, end_len;
5086 int i;
5087
5088 /*
5089 * Need to remove existing (middle) comment leader and insert end
5090 * comment leader. First, check what comment leader we can find.
5091 */
5092 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5093 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5094 {
5095 /* Skip middle-comment string */
5096 while (*p && p[-1] != ':') /* find end of middle flags */
5097 ++p;
5098 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5099 /* Don't count trailing white space for middle_len */
5100 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5101 --middle_len;
5102
5103 /* Find the end-comment string */
5104 while (*p && p[-1] != ':') /* find end of end flags */
5105 ++p;
5106 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5107
5108 /* Skip white space before the cursor */
5109 i = curwin->w_cursor.col;
5110 while (--i >= 0 && vim_iswhite(line[i]))
5111 ;
5112 i++;
5113
5114 /* Skip to before the middle leader */
5115 i -= middle_len;
5116
5117 /* Check some expected things before we go on */
5118 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5119 {
5120 /* Backspace over all the stuff we want to replace */
5121 backspace_until_column(i);
5122
5123 /*
5124 * Insert the end-comment string, except for the last
5125 * character, which will get inserted as normal later.
5126 */
5127 ins_bytes_len(lead_end, end_len - 1);
5128 }
5129 }
5130 }
5131 end_comment_pending = NUL;
5132#endif
5133
5134 did_ai = FALSE;
5135#ifdef FEAT_SMARTINDENT
5136 did_si = FALSE;
5137 can_si = FALSE;
5138 can_si_back = FALSE;
5139#endif
5140
5141 /*
5142 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5143 * This speeds up normal text input considerably.
5144 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5145 * need to re-indent at a ':', or any other character (but not what
5146 * 'paste' is set)..
5147 */
5148#ifdef USE_ON_FLY_SCROLL
5149 dont_scroll = FALSE; /* allow scrolling here */
5150#endif
5151
5152 if ( !ISSPECIAL(c)
5153#ifdef FEAT_MBYTE
5154 && (!has_mbyte || (*mb_char2len)(c) == 1)
5155#endif
5156 && vpeekc() != NUL
5157 && !(State & REPLACE_FLAG)
5158#ifdef FEAT_CINDENT
5159 && !cindent_on()
5160#endif
5161#ifdef FEAT_RIGHTLEFT
5162 && !p_ri
5163#endif
5164 )
5165 {
5166#define INPUT_BUFLEN 100
5167 char_u buf[INPUT_BUFLEN + 1];
5168 int i;
5169 colnr_T virtcol = 0;
5170
5171 buf[0] = c;
5172 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005173 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 virtcol = get_nolist_virtcol();
5175 /*
5176 * Stop the string when:
5177 * - no more chars available
5178 * - finding a special character (command key)
5179 * - buffer is full
5180 * - running into the 'textwidth' boundary
5181 * - need to check for abbreviation: A non-word char after a word-char
5182 */
5183 while ( (c = vpeekc()) != NUL
5184 && !ISSPECIAL(c)
5185#ifdef FEAT_MBYTE
5186 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5187#endif
5188 && i < INPUT_BUFLEN
5189 && (textwidth == 0
5190 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5191 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5192 {
5193#ifdef FEAT_RIGHTLEFT
5194 c = vgetc();
5195 if (p_hkmap && KeyTyped)
5196 c = hkmap(c); /* Hebrew mode mapping */
5197# ifdef FEAT_FKMAP
5198 if (p_fkmap && KeyTyped)
5199 c = fkmap(c); /* Farsi mode mapping */
5200# endif
5201 buf[i++] = c;
5202#else
5203 buf[i++] = vgetc();
5204#endif
5205 }
5206
5207#ifdef FEAT_DIGRAPHS
5208 do_digraph(-1); /* clear digraphs */
5209 do_digraph(buf[i-1]); /* may be the start of a digraph */
5210#endif
5211 buf[i] = NUL;
5212 ins_str(buf);
5213 if (flags & INSCHAR_CTRLV)
5214 {
5215 redo_literal(*buf);
5216 i = 1;
5217 }
5218 else
5219 i = 0;
5220 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005221 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 }
5223 else
5224 {
5225#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005226 int cc;
5227
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5229 {
5230 char_u buf[MB_MAXBYTES + 1];
5231
5232 (*mb_char2bytes)(c, buf);
5233 buf[cc] = NUL;
5234 ins_char_bytes(buf, cc);
5235 AppendCharToRedobuff(c);
5236 }
5237 else
5238#endif
5239 {
5240 ins_char(c);
5241 if (flags & INSCHAR_CTRLV)
5242 redo_literal(c);
5243 else
5244 AppendCharToRedobuff(c);
5245 }
5246 }
5247}
5248
5249/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005250 * Format text at the current insert position.
5251 */
5252 static void
5253internal_format(textwidth, second_indent, flags, format_only)
5254 int textwidth;
5255 int second_indent;
5256 int flags;
5257 int format_only;
5258{
5259 int cc;
5260 int save_char = NUL;
5261 int haveto_redraw = FALSE;
5262 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5263#ifdef FEAT_MBYTE
5264 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5265#endif
5266 int fo_white_par = has_format_option(FO_WHITE_PAR);
5267 int first_line = TRUE;
5268#ifdef FEAT_COMMENTS
5269 colnr_T leader_len;
5270 int no_leader = FALSE;
5271 int do_comments = (flags & INSCHAR_DO_COM);
5272#endif
5273
5274 /*
5275 * When 'ai' is off we don't want a space under the cursor to be
5276 * deleted. Replace it with an 'x' temporarily.
5277 */
5278 if (!curbuf->b_p_ai)
5279 {
5280 cc = gchar_cursor();
5281 if (vim_iswhite(cc))
5282 {
5283 save_char = cc;
5284 pchar_cursor('x');
5285 }
5286 }
5287
5288 /*
5289 * Repeat breaking lines, until the current line is not too long.
5290 */
5291 while (!got_int)
5292 {
5293 int startcol; /* Cursor column at entry */
5294 int wantcol; /* column at textwidth border */
5295 int foundcol; /* column for start of spaces */
5296 int end_foundcol = 0; /* column for start of word */
5297 colnr_T len;
5298 colnr_T virtcol;
5299#ifdef FEAT_VREPLACE
5300 int orig_col = 0;
5301 char_u *saved_text = NULL;
5302#endif
5303 colnr_T col;
5304
5305 virtcol = get_nolist_virtcol();
5306 if (virtcol < (colnr_T)textwidth)
5307 break;
5308
5309#ifdef FEAT_COMMENTS
5310 if (no_leader)
5311 do_comments = FALSE;
5312 else if (!(flags & INSCHAR_FORMAT)
5313 && has_format_option(FO_WRAP_COMS))
5314 do_comments = TRUE;
5315
5316 /* Don't break until after the comment leader */
5317 if (do_comments)
5318 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5319 else
5320 leader_len = 0;
5321
5322 /* If the line doesn't start with a comment leader, then don't
5323 * start one in a following broken line. Avoids that a %word
5324 * moved to the start of the next line causes all following lines
5325 * to start with %. */
5326 if (leader_len == 0)
5327 no_leader = TRUE;
5328#endif
5329 if (!(flags & INSCHAR_FORMAT)
5330#ifdef FEAT_COMMENTS
5331 && leader_len == 0
5332#endif
5333 && !has_format_option(FO_WRAP))
5334
5335 {
5336 textwidth = 0;
5337 break;
5338 }
5339 if ((startcol = curwin->w_cursor.col) == 0)
5340 break;
5341
5342 /* find column of textwidth border */
5343 coladvance((colnr_T)textwidth);
5344 wantcol = curwin->w_cursor.col;
5345
5346 curwin->w_cursor.col = startcol - 1;
5347#ifdef FEAT_MBYTE
5348 /* Correct cursor for multi-byte character. */
5349 if (has_mbyte)
5350 mb_adjust_cursor();
5351#endif
5352 foundcol = 0;
5353
5354 /*
5355 * Find position to break at.
5356 * Stop at first entered white when 'formatoptions' has 'v'
5357 */
5358 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5359 || curwin->w_cursor.lnum != Insstart.lnum
5360 || curwin->w_cursor.col >= Insstart.col)
5361 {
5362 cc = gchar_cursor();
5363 if (WHITECHAR(cc))
5364 {
5365 /* remember position of blank just before text */
5366 end_foundcol = curwin->w_cursor.col;
5367
5368 /* find start of sequence of blanks */
5369 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5370 {
5371 dec_cursor();
5372 cc = gchar_cursor();
5373 }
5374 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5375 break; /* only spaces in front of text */
5376#ifdef FEAT_COMMENTS
5377 /* Don't break until after the comment leader */
5378 if (curwin->w_cursor.col < leader_len)
5379 break;
5380#endif
5381 if (has_format_option(FO_ONE_LETTER))
5382 {
5383 /* do not break after one-letter words */
5384 if (curwin->w_cursor.col == 0)
5385 break; /* one-letter word at begin */
5386
5387 col = curwin->w_cursor.col;
5388 dec_cursor();
5389 cc = gchar_cursor();
5390
5391 if (WHITECHAR(cc))
5392 continue; /* one-letter, continue */
5393 curwin->w_cursor.col = col;
5394 }
5395#ifdef FEAT_MBYTE
5396 if (has_mbyte)
5397 foundcol = curwin->w_cursor.col
5398 + (*mb_ptr2len)(ml_get_cursor());
5399 else
5400#endif
5401 foundcol = curwin->w_cursor.col + 1;
5402 if (curwin->w_cursor.col < (colnr_T)wantcol)
5403 break;
5404 }
5405#ifdef FEAT_MBYTE
5406 else if (cc >= 0x100 && fo_multibyte
5407 && curwin->w_cursor.col <= (colnr_T)wantcol)
5408 {
5409 /* Break after or before a multi-byte character. */
5410 foundcol = curwin->w_cursor.col;
5411 if (curwin->w_cursor.col < (colnr_T)wantcol)
5412 foundcol += (*mb_char2len)(cc);
5413 end_foundcol = foundcol;
5414 break;
5415 }
5416#endif
5417 if (curwin->w_cursor.col == 0)
5418 break;
5419 dec_cursor();
5420 }
5421
5422 if (foundcol == 0) /* no spaces, cannot break line */
5423 {
5424 curwin->w_cursor.col = startcol;
5425 break;
5426 }
5427
5428 /* Going to break the line, remove any "$" now. */
5429 undisplay_dollar();
5430
5431 /*
5432 * Offset between cursor position and line break is used by replace
5433 * stack functions. VREPLACE does not use this, and backspaces
5434 * over the text instead.
5435 */
5436#ifdef FEAT_VREPLACE
5437 if (State & VREPLACE_FLAG)
5438 orig_col = startcol; /* Will start backspacing from here */
5439 else
5440#endif
5441 replace_offset = startcol - end_foundcol - 1;
5442
5443 /*
5444 * adjust startcol for spaces that will be deleted and
5445 * characters that will remain on top line
5446 */
5447 curwin->w_cursor.col = foundcol;
5448 while (cc = gchar_cursor(), WHITECHAR(cc))
5449 inc_cursor();
5450 startcol -= curwin->w_cursor.col;
5451 if (startcol < 0)
5452 startcol = 0;
5453
5454#ifdef FEAT_VREPLACE
5455 if (State & VREPLACE_FLAG)
5456 {
5457 /*
5458 * In VREPLACE mode, we will backspace over the text to be
5459 * wrapped, so save a copy now to put on the next line.
5460 */
5461 saved_text = vim_strsave(ml_get_cursor());
5462 curwin->w_cursor.col = orig_col;
5463 if (saved_text == NULL)
5464 break; /* Can't do it, out of memory */
5465 saved_text[startcol] = NUL;
5466
5467 /* Backspace over characters that will move to the next line */
5468 if (!fo_white_par)
5469 backspace_until_column(foundcol);
5470 }
5471 else
5472#endif
5473 {
5474 /* put cursor after pos. to break line */
5475 if (!fo_white_par)
5476 curwin->w_cursor.col = foundcol;
5477 }
5478
5479 /*
5480 * Split the line just before the margin.
5481 * Only insert/delete lines, but don't really redraw the window.
5482 */
5483 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
5484 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
5485#ifdef FEAT_COMMENTS
5486 + (do_comments ? OPENLINE_DO_COM : 0)
5487#endif
5488 , old_indent);
5489 old_indent = 0;
5490
5491 replace_offset = 0;
5492 if (first_line)
5493 {
5494 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
5495 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
5496 if (second_indent >= 0)
5497 {
5498#ifdef FEAT_VREPLACE
5499 if (State & VREPLACE_FLAG)
5500 change_indent(INDENT_SET, second_indent, FALSE, NUL);
5501 else
5502#endif
5503 (void)set_indent(second_indent, SIN_CHANGED);
5504 }
5505 first_line = FALSE;
5506 }
5507
5508#ifdef FEAT_VREPLACE
5509 if (State & VREPLACE_FLAG)
5510 {
5511 /*
5512 * In VREPLACE mode we have backspaced over the text to be
5513 * moved, now we re-insert it into the new line.
5514 */
5515 ins_bytes(saved_text);
5516 vim_free(saved_text);
5517 }
5518 else
5519#endif
5520 {
5521 /*
5522 * Check if cursor is not past the NUL off the line, cindent
5523 * may have added or removed indent.
5524 */
5525 curwin->w_cursor.col += startcol;
5526 len = (colnr_T)STRLEN(ml_get_curline());
5527 if (curwin->w_cursor.col > len)
5528 curwin->w_cursor.col = len;
5529 }
5530
5531 haveto_redraw = TRUE;
5532#ifdef FEAT_CINDENT
5533 can_cindent = TRUE;
5534#endif
5535 /* moved the cursor, don't autoindent or cindent now */
5536 did_ai = FALSE;
5537#ifdef FEAT_SMARTINDENT
5538 did_si = FALSE;
5539 can_si = FALSE;
5540 can_si_back = FALSE;
5541#endif
5542 line_breakcheck();
5543 }
5544
5545 if (save_char != NUL) /* put back space after cursor */
5546 pchar_cursor(save_char);
5547
5548 if (!format_only && haveto_redraw)
5549 {
5550 update_topline();
5551 redraw_curbuf_later(VALID);
5552 }
5553}
5554
5555/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 * Called after inserting or deleting text: When 'formatoptions' includes the
5557 * 'a' flag format from the current line until the end of the paragraph.
5558 * Keep the cursor at the same position relative to the text.
5559 * The caller must have saved the cursor line for undo, following ones will be
5560 * saved here.
5561 */
5562 void
5563auto_format(trailblank, prev_line)
5564 int trailblank; /* when TRUE also format with trailing blank */
5565 int prev_line; /* may start in previous line */
5566{
5567 pos_T pos;
5568 colnr_T len;
5569 char_u *old;
5570 char_u *new, *pnew;
5571 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005572 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573
5574 if (!has_format_option(FO_AUTO))
5575 return;
5576
5577 pos = curwin->w_cursor;
5578 old = ml_get_curline();
5579
5580 /* may remove added space */
5581 check_auto_format(FALSE);
5582
5583 /* Don't format in Insert mode when the cursor is on a trailing blank, the
5584 * user might insert normal text next. Also skip formatting when "1" is
5585 * in 'formatoptions' and there is a single character before the cursor.
5586 * Otherwise the line would be broken and when typing another non-white
5587 * next they are not joined back together. */
5588 wasatend = (pos.col == STRLEN(old));
5589 if (*old != NUL && !trailblank && wasatend)
5590 {
5591 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005592 cc = gchar_cursor();
5593 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
5594 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005596 cc = gchar_cursor();
5597 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 {
5599 curwin->w_cursor = pos;
5600 return;
5601 }
5602 curwin->w_cursor = pos;
5603 }
5604
5605#ifdef FEAT_COMMENTS
5606 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
5607 * comments. */
5608 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
5609 && get_leader_len(old, NULL, FALSE) == 0)
5610 return;
5611#endif
5612
5613 /*
5614 * May start formatting in a previous line, so that after "x" a word is
5615 * moved to the previous line if it fits there now. Only when this is not
5616 * the start of a paragraph.
5617 */
5618 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
5619 {
5620 --curwin->w_cursor.lnum;
5621 if (u_save_cursor() == FAIL)
5622 return;
5623 }
5624
5625 /*
5626 * Do the formatting and restore the cursor position. "saved_cursor" will
5627 * be adjusted for the text formatting.
5628 */
5629 saved_cursor = pos;
5630 format_lines((linenr_T)-1);
5631 curwin->w_cursor = saved_cursor;
5632 saved_cursor.lnum = 0;
5633
5634 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5635 {
5636 /* "cannot happen" */
5637 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5638 coladvance((colnr_T)MAXCOL);
5639 }
5640 else
5641 check_cursor_col();
5642
5643 /* Insert mode: If the cursor is now after the end of the line while it
5644 * previously wasn't, the line was broken. Because of the rule above we
5645 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
5646 * formatted. */
5647 if (!wasatend && has_format_option(FO_WHITE_PAR))
5648 {
5649 new = ml_get_curline();
5650 len = STRLEN(new);
5651 if (curwin->w_cursor.col == len)
5652 {
5653 pnew = vim_strnsave(new, len + 2);
5654 pnew[len] = ' ';
5655 pnew[len + 1] = NUL;
5656 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
5657 /* remove the space later */
5658 did_add_space = TRUE;
5659 }
5660 else
5661 /* may remove added space */
5662 check_auto_format(FALSE);
5663 }
5664
5665 check_cursor();
5666}
5667
5668/*
5669 * When an extra space was added to continue a paragraph for auto-formatting,
5670 * delete it now. The space must be under the cursor, just after the insert
5671 * position.
5672 */
5673 static void
5674check_auto_format(end_insert)
5675 int end_insert; /* TRUE when ending Insert mode */
5676{
5677 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005678 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679
5680 if (did_add_space)
5681 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005682 cc = gchar_cursor();
5683 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 /* Somehow the space was removed already. */
5685 did_add_space = FALSE;
5686 else
5687 {
5688 if (!end_insert)
5689 {
5690 inc_cursor();
5691 c = gchar_cursor();
5692 dec_cursor();
5693 }
5694 if (c != NUL)
5695 {
5696 /* The space is no longer at the end of the line, delete it. */
5697 del_char(FALSE);
5698 did_add_space = FALSE;
5699 }
5700 }
5701 }
5702}
5703
5704/*
5705 * Find out textwidth to be used for formatting:
5706 * if 'textwidth' option is set, use it
5707 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
5708 * if invalid value, use 0.
5709 * Set default to window width (maximum 79) for "gq" operator.
5710 */
5711 int
5712comp_textwidth(ff)
5713 int ff; /* force formatting (for "Q" command) */
5714{
5715 int textwidth;
5716
5717 textwidth = curbuf->b_p_tw;
5718 if (textwidth == 0 && curbuf->b_p_wm)
5719 {
5720 /* The width is the window width minus 'wrapmargin' minus all the
5721 * things that add to the margin. */
5722 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
5723#ifdef FEAT_CMDWIN
5724 if (cmdwin_type != 0)
5725 textwidth -= 1;
5726#endif
5727#ifdef FEAT_FOLDING
5728 textwidth -= curwin->w_p_fdc;
5729#endif
5730#ifdef FEAT_SIGNS
5731 if (curwin->w_buffer->b_signlist != NULL
5732# ifdef FEAT_NETBEANS_INTG
5733 || usingNetbeans
5734# endif
5735 )
5736 textwidth -= 1;
5737#endif
5738 if (curwin->w_p_nu)
5739 textwidth -= 8;
5740 }
5741 if (textwidth < 0)
5742 textwidth = 0;
5743 if (ff && textwidth == 0)
5744 {
5745 textwidth = W_WIDTH(curwin) - 1;
5746 if (textwidth > 79)
5747 textwidth = 79;
5748 }
5749 return textwidth;
5750}
5751
5752/*
5753 * Put a character in the redo buffer, for when just after a CTRL-V.
5754 */
5755 static void
5756redo_literal(c)
5757 int c;
5758{
5759 char_u buf[10];
5760
5761 /* Only digits need special treatment. Translate them into a string of
5762 * three digits. */
5763 if (VIM_ISDIGIT(c))
5764 {
5765 sprintf((char *)buf, "%03d", c);
5766 AppendToRedobuff(buf);
5767 }
5768 else
5769 AppendCharToRedobuff(c);
5770}
5771
5772/*
5773 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005774 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 */
5776 static void
5777start_arrow(end_insert_pos)
5778 pos_T *end_insert_pos;
5779{
5780 if (!arrow_used) /* something has been inserted */
5781 {
5782 AppendToRedobuff(ESC_STR);
5783 stop_insert(end_insert_pos, FALSE);
5784 arrow_used = TRUE; /* this means we stopped the current insert */
5785 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005786#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005787 check_spell_redraw();
5788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789}
5790
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005791#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005792/*
5793 * If we skipped highlighting word at cursor, do it now.
5794 * It may be skipped again, thus reset spell_redraw_lnum first.
5795 */
5796 static void
5797check_spell_redraw()
5798{
5799 if (spell_redraw_lnum != 0)
5800 {
5801 linenr_T lnum = spell_redraw_lnum;
5802
5803 spell_redraw_lnum = 0;
5804 redrawWinline(lnum, FALSE);
5805 }
5806}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005807
5808/*
5809 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5810 * spelled word, if there is one.
5811 */
5812 static void
5813spell_back_to_badword()
5814{
5815 pos_T tpos = curwin->w_cursor;
5816
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00005817 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005818 if (curwin->w_cursor.col != tpos.col)
5819 start_arrow(&tpos);
5820}
Bram Moolenaar217ad922005-03-20 22:37:15 +00005821#endif
5822
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823/*
5824 * stop_arrow() is called before a change is made in insert mode.
5825 * If an arrow key has been used, start a new insertion.
5826 * Returns FAIL if undo is impossible, shouldn't insert then.
5827 */
5828 int
5829stop_arrow()
5830{
5831 if (arrow_used)
5832 {
5833 if (u_save_cursor() == OK)
5834 {
5835 arrow_used = FALSE;
5836 ins_need_undo = FALSE;
5837 }
5838 Insstart = curwin->w_cursor; /* new insertion starts here */
5839 Insstart_textlen = linetabsize(ml_get_curline());
5840 ai_col = 0;
5841#ifdef FEAT_VREPLACE
5842 if (State & VREPLACE_FLAG)
5843 {
5844 orig_line_count = curbuf->b_ml.ml_line_count;
5845 vr_lines_changed = 1;
5846 }
5847#endif
5848 ResetRedobuff();
5849 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00005850 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 }
5852 else if (ins_need_undo)
5853 {
5854 if (u_save_cursor() == OK)
5855 ins_need_undo = FALSE;
5856 }
5857
5858#ifdef FEAT_FOLDING
5859 /* Always open fold at the cursor line when inserting something. */
5860 foldOpenCursor();
5861#endif
5862
5863 return (arrow_used || ins_need_undo ? FAIL : OK);
5864}
5865
5866/*
5867 * do a few things to stop inserting
5868 */
5869 static void
5870stop_insert(end_insert_pos, esc)
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005871 pos_T *end_insert_pos; /* where insert ended */
5872 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005874 int cc;
5875 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876
5877 stop_redo_ins();
5878 replace_flush(); /* abandon replace stack */
5879
5880 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005881 * Save the inserted text for later redo with ^@ and CTRL-A.
5882 * Don't do it when "restart_edit" was set and nothing was inserted,
5883 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005885 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00005886 if (did_restart_edit == 0 || (ptr != NULL
5887 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005888 {
5889 vim_free(last_insert);
5890 last_insert = ptr;
5891 last_insert_skip = new_insert_skip;
5892 }
5893 else
5894 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895
5896 if (!arrow_used)
5897 {
5898 /* Auto-format now. It may seem strange to do this when stopping an
5899 * insertion (or moving the cursor), but it's required when appending
5900 * a line and having it end in a space. But only do it when something
5901 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005902 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005904 pos_T tpos = curwin->w_cursor;
5905
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 /* When the cursor is at the end of the line after a space the
5907 * formatting will move it to the following word. Avoid that by
5908 * moving the cursor onto the space. */
5909 cc = 'x';
5910 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
5911 {
5912 dec_cursor();
5913 cc = gchar_cursor();
5914 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005915 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 }
5917
5918 auto_format(TRUE, FALSE);
5919
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005920 if (vim_iswhite(cc))
5921 {
5922 if (gchar_cursor() != NUL)
5923 inc_cursor();
5924#ifdef FEAT_VIRTUALEDIT
5925 /* If the cursor is still at the same character, also keep
5926 * the "coladd". */
5927 if (gchar_cursor() == NUL
5928 && curwin->w_cursor.lnum == tpos.lnum
5929 && curwin->w_cursor.col == tpos.col)
5930 curwin->w_cursor.coladd = tpos.coladd;
5931#endif
5932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 }
5934
5935 /* If a space was inserted for auto-formatting, remove it now. */
5936 check_auto_format(TRUE);
5937
5938 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005939 * of the line, and put the cursor back.
5940 * Do this when ESC was used or moving the cursor up/down. */
5941 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
5942 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005944 pos_T tpos = curwin->w_cursor;
5945
5946 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar39f05632006-03-19 22:15:26 +00005947 for (;;)
5948 {
5949 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
5950 --curwin->w_cursor.col;
5951 cc = gchar_cursor();
5952 if (!vim_iswhite(cc))
5953 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 (void)del_char(TRUE);
Bram Moolenaar39f05632006-03-19 22:15:26 +00005955 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005956 if (curwin->w_cursor.lnum != tpos.lnum)
5957 curwin->w_cursor = tpos;
5958 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 ++curwin->w_cursor.col; /* put cursor back on the NUL */
5960
5961#ifdef FEAT_VISUAL
5962 /* <C-S-Right> may have started Visual mode, adjust the position for
5963 * deleted characters. */
5964 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
5965 {
5966 cc = STRLEN(ml_get_curline());
5967 if (VIsual.col > (colnr_T)cc)
5968 {
5969 VIsual.col = cc;
5970# ifdef FEAT_VIRTUALEDIT
5971 VIsual.coladd = 0;
5972# endif
5973 }
5974 }
5975#endif
5976 }
5977 }
5978 did_ai = FALSE;
5979#ifdef FEAT_SMARTINDENT
5980 did_si = FALSE;
5981 can_si = FALSE;
5982 can_si_back = FALSE;
5983#endif
5984
5985 /* set '[ and '] to the inserted text */
5986 curbuf->b_op_start = Insstart;
5987 curbuf->b_op_end = *end_insert_pos;
5988}
5989
5990/*
5991 * Set the last inserted text to a single character.
5992 * Used for the replace command.
5993 */
5994 void
5995set_last_insert(c)
5996 int c;
5997{
5998 char_u *s;
5999
6000 vim_free(last_insert);
6001#ifdef FEAT_MBYTE
6002 last_insert = alloc(MB_MAXBYTES * 3 + 5);
6003#else
6004 last_insert = alloc(6);
6005#endif
6006 if (last_insert != NULL)
6007 {
6008 s = last_insert;
6009 /* Use the CTRL-V only when entering a special char */
6010 if (c < ' ' || c == DEL)
6011 *s++ = Ctrl_V;
6012 s = add_char2buf(c, s);
6013 *s++ = ESC;
6014 *s++ = NUL;
6015 last_insert_skip = 0;
6016 }
6017}
6018
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006019#if defined(EXITFREE) || defined(PROTO)
6020 void
6021free_last_insert()
6022{
6023 vim_free(last_insert);
6024 last_insert = NULL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006025 vim_free(compl_orig_text);
6026 compl_orig_text = NULL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006027}
6028#endif
6029
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030/*
6031 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6032 * and CSI. Handle multi-byte characters.
6033 * Returns a pointer to after the added bytes.
6034 */
6035 char_u *
6036add_char2buf(c, s)
6037 int c;
6038 char_u *s;
6039{
6040#ifdef FEAT_MBYTE
6041 char_u temp[MB_MAXBYTES];
6042 int i;
6043 int len;
6044
6045 len = (*mb_char2bytes)(c, temp);
6046 for (i = 0; i < len; ++i)
6047 {
6048 c = temp[i];
6049#endif
6050 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6051 if (c == K_SPECIAL)
6052 {
6053 *s++ = K_SPECIAL;
6054 *s++ = KS_SPECIAL;
6055 *s++ = KE_FILLER;
6056 }
6057#ifdef FEAT_GUI
6058 else if (c == CSI)
6059 {
6060 *s++ = CSI;
6061 *s++ = KS_EXTRA;
6062 *s++ = (int)KE_CSI;
6063 }
6064#endif
6065 else
6066 *s++ = c;
6067#ifdef FEAT_MBYTE
6068 }
6069#endif
6070 return s;
6071}
6072
6073/*
6074 * move cursor to start of line
6075 * if flags & BL_WHITE move to first non-white
6076 * if flags & BL_SOL move to first non-white if startofline is set,
6077 * otherwise keep "curswant" column
6078 * if flags & BL_FIX don't leave the cursor on a NUL.
6079 */
6080 void
6081beginline(flags)
6082 int flags;
6083{
6084 if ((flags & BL_SOL) && !p_sol)
6085 coladvance(curwin->w_curswant);
6086 else
6087 {
6088 curwin->w_cursor.col = 0;
6089#ifdef FEAT_VIRTUALEDIT
6090 curwin->w_cursor.coladd = 0;
6091#endif
6092
6093 if (flags & (BL_WHITE | BL_SOL))
6094 {
6095 char_u *ptr;
6096
6097 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6098 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6099 ++curwin->w_cursor.col;
6100 }
6101 curwin->w_set_curswant = TRUE;
6102 }
6103}
6104
6105/*
6106 * oneright oneleft cursor_down cursor_up
6107 *
6108 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006109 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 * Return OK when successful, FAIL when we hit a line of file boundary.
6111 */
6112
6113 int
6114oneright()
6115{
6116 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118
6119#ifdef FEAT_VIRTUALEDIT
6120 if (virtual_active())
6121 {
6122 pos_T prevpos = curwin->w_cursor;
6123
6124 /* Adjust for multi-wide char (excluding TAB) */
6125 ptr = ml_get_cursor();
6126 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006127# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006129# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006131# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 ))
6133 ? ptr2cells(ptr) : 1));
6134 curwin->w_set_curswant = TRUE;
6135 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6136 return (prevpos.col != curwin->w_cursor.col
6137 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
6138 }
6139#endif
6140
6141 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006142 if (*ptr == NUL)
6143 return FAIL; /* already at the very end */
6144
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006146 if (has_mbyte)
6147 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 else
6149#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006150 l = 1;
6151
6152 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6153 * contains "onemore". */
6154 if (ptr[l] == NUL
6155#ifdef FEAT_VIRTUALEDIT
6156 && (ve_flags & VE_ONEMORE) == 0
6157#endif
6158 )
6159 return FAIL;
6160 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161
6162 curwin->w_set_curswant = TRUE;
6163 return OK;
6164}
6165
6166 int
6167oneleft()
6168{
6169#ifdef FEAT_VIRTUALEDIT
6170 if (virtual_active())
6171 {
6172 int width;
6173 int v = getviscol();
6174
6175 if (v == 0)
6176 return FAIL;
6177
6178# ifdef FEAT_LINEBREAK
6179 /* We might get stuck on 'showbreak', skip over it. */
6180 width = 1;
6181 for (;;)
6182 {
6183 coladvance(v - width);
6184 /* getviscol() is slow, skip it when 'showbreak' is empty and
6185 * there are no multi-byte characters */
6186 if ((*p_sbr == NUL
6187# ifdef FEAT_MBYTE
6188 && !has_mbyte
6189# endif
6190 ) || getviscol() < v)
6191 break;
6192 ++width;
6193 }
6194# else
6195 coladvance(v - 1);
6196# endif
6197
6198 if (curwin->w_cursor.coladd == 1)
6199 {
6200 char_u *ptr;
6201
6202 /* Adjust for multi-wide char (not a TAB) */
6203 ptr = ml_get_cursor();
6204 if (*ptr != TAB && vim_isprintc(
6205# ifdef FEAT_MBYTE
6206 (*mb_ptr2char)(ptr)
6207# else
6208 *ptr
6209# endif
6210 ) && ptr2cells(ptr) > 1)
6211 curwin->w_cursor.coladd = 0;
6212 }
6213
6214 curwin->w_set_curswant = TRUE;
6215 return OK;
6216 }
6217#endif
6218
6219 if (curwin->w_cursor.col == 0)
6220 return FAIL;
6221
6222 curwin->w_set_curswant = TRUE;
6223 --curwin->w_cursor.col;
6224
6225#ifdef FEAT_MBYTE
6226 /* if the character on the left of the current cursor is a multi-byte
6227 * character, move to its first byte */
6228 if (has_mbyte)
6229 mb_adjust_cursor();
6230#endif
6231 return OK;
6232}
6233
6234 int
6235cursor_up(n, upd_topline)
6236 long n;
6237 int upd_topline; /* When TRUE: update topline */
6238{
6239 linenr_T lnum;
6240
6241 if (n > 0)
6242 {
6243 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006244 /* This fails if the cursor is already in the first line or the count
6245 * is larger than the line number and '-' is in 'cpoptions' */
6246 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 return FAIL;
6248 if (n >= lnum)
6249 lnum = 1;
6250 else
6251#ifdef FEAT_FOLDING
6252 if (hasAnyFolding(curwin))
6253 {
6254 /*
6255 * Count each sequence of folded lines as one logical line.
6256 */
6257 /* go to the the start of the current fold */
6258 (void)hasFolding(lnum, &lnum, NULL);
6259
6260 while (n--)
6261 {
6262 /* move up one line */
6263 --lnum;
6264 if (lnum <= 1)
6265 break;
6266 /* If we entered a fold, move to the beginning, unless in
6267 * Insert mode or when 'foldopen' contains "all": it will open
6268 * in a moment. */
6269 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6270 (void)hasFolding(lnum, &lnum, NULL);
6271 }
6272 if (lnum < 1)
6273 lnum = 1;
6274 }
6275 else
6276#endif
6277 lnum -= n;
6278 curwin->w_cursor.lnum = lnum;
6279 }
6280
6281 /* try to advance to the column we want to be at */
6282 coladvance(curwin->w_curswant);
6283
6284 if (upd_topline)
6285 update_topline(); /* make sure curwin->w_topline is valid */
6286
6287 return OK;
6288}
6289
6290/*
6291 * Cursor down a number of logical lines.
6292 */
6293 int
6294cursor_down(n, upd_topline)
6295 long n;
6296 int upd_topline; /* When TRUE: update topline */
6297{
6298 linenr_T lnum;
6299
6300 if (n > 0)
6301 {
6302 lnum = curwin->w_cursor.lnum;
6303#ifdef FEAT_FOLDING
6304 /* Move to last line of fold, will fail if it's the end-of-file. */
6305 (void)hasFolding(lnum, NULL, &lnum);
6306#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006307 /* This fails if the cursor is already in the last line or would move
6308 * beyound the last line and '-' is in 'cpoptions' */
6309 if (lnum >= curbuf->b_ml.ml_line_count
6310 || (lnum + n > curbuf->b_ml.ml_line_count
6311 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 return FAIL;
6313 if (lnum + n >= curbuf->b_ml.ml_line_count)
6314 lnum = curbuf->b_ml.ml_line_count;
6315 else
6316#ifdef FEAT_FOLDING
6317 if (hasAnyFolding(curwin))
6318 {
6319 linenr_T last;
6320
6321 /* count each sequence of folded lines as one logical line */
6322 while (n--)
6323 {
6324 if (hasFolding(lnum, NULL, &last))
6325 lnum = last + 1;
6326 else
6327 ++lnum;
6328 if (lnum >= curbuf->b_ml.ml_line_count)
6329 break;
6330 }
6331 if (lnum > curbuf->b_ml.ml_line_count)
6332 lnum = curbuf->b_ml.ml_line_count;
6333 }
6334 else
6335#endif
6336 lnum += n;
6337 curwin->w_cursor.lnum = lnum;
6338 }
6339
6340 /* try to advance to the column we want to be at */
6341 coladvance(curwin->w_curswant);
6342
6343 if (upd_topline)
6344 update_topline(); /* make sure curwin->w_topline is valid */
6345
6346 return OK;
6347}
6348
6349/*
6350 * Stuff the last inserted text in the read buffer.
6351 * Last_insert actually is a copy of the redo buffer, so we
6352 * first have to remove the command.
6353 */
6354 int
6355stuff_inserted(c, count, no_esc)
6356 int c; /* Command character to be inserted */
6357 long count; /* Repeat this many times */
6358 int no_esc; /* Don't add an ESC at the end */
6359{
6360 char_u *esc_ptr;
6361 char_u *ptr;
6362 char_u *last_ptr;
6363 char_u last = NUL;
6364
6365 ptr = get_last_insert();
6366 if (ptr == NULL)
6367 {
6368 EMSG(_(e_noinstext));
6369 return FAIL;
6370 }
6371
6372 /* may want to stuff the command character, to start Insert mode */
6373 if (c != NUL)
6374 stuffcharReadbuff(c);
6375 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
6376 *esc_ptr = NUL; /* remove the ESC */
6377
6378 /* when the last char is either "0" or "^" it will be quoted if no ESC
6379 * comes after it OR if it will inserted more than once and "ptr"
6380 * starts with ^D. -- Acevedo
6381 */
6382 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
6383 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
6384 && (no_esc || (*ptr == Ctrl_D && count > 1)))
6385 {
6386 last = *last_ptr;
6387 *last_ptr = NUL;
6388 }
6389
6390 do
6391 {
6392 stuffReadbuff(ptr);
6393 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6394 if (last)
6395 stuffReadbuff((char_u *)(last == '0'
6396 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
6397 : IF_EB("\026^", CTRL_V_STR "^")));
6398 }
6399 while (--count > 0);
6400
6401 if (last)
6402 *last_ptr = last;
6403
6404 if (esc_ptr != NULL)
6405 *esc_ptr = ESC; /* put the ESC back */
6406
6407 /* may want to stuff a trailing ESC, to get out of Insert mode */
6408 if (!no_esc)
6409 stuffcharReadbuff(ESC);
6410
6411 return OK;
6412}
6413
6414 char_u *
6415get_last_insert()
6416{
6417 if (last_insert == NULL)
6418 return NULL;
6419 return last_insert + last_insert_skip;
6420}
6421
6422/*
6423 * Get last inserted string, and remove trailing <Esc>.
6424 * Returns pointer to allocated memory (must be freed) or NULL.
6425 */
6426 char_u *
6427get_last_insert_save()
6428{
6429 char_u *s;
6430 int len;
6431
6432 if (last_insert == NULL)
6433 return NULL;
6434 s = vim_strsave(last_insert + last_insert_skip);
6435 if (s != NULL)
6436 {
6437 len = (int)STRLEN(s);
6438 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
6439 s[len - 1] = NUL;
6440 }
6441 return s;
6442}
6443
6444/*
6445 * Check the word in front of the cursor for an abbreviation.
6446 * Called when the non-id character "c" has been entered.
6447 * When an abbreviation is recognized it is removed from the text and
6448 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
6449 */
6450 static int
6451echeck_abbr(c)
6452 int c;
6453{
6454 /* Don't check for abbreviation in paste mode, when disabled and just
6455 * after moving around with cursor keys. */
6456 if (p_paste || no_abbr || arrow_used)
6457 return FALSE;
6458
6459 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
6460 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
6461}
6462
6463/*
6464 * replace-stack functions
6465 *
6466 * When replacing characters, the replaced characters are remembered for each
6467 * new character. This is used to re-insert the old text when backspacing.
6468 *
6469 * There is a NUL headed list of characters for each character that is
6470 * currently in the file after the insertion point. When BS is used, one NUL
6471 * headed list is put back for the deleted character.
6472 *
6473 * For a newline, there are two NUL headed lists. One contains the characters
6474 * that the NL replaced. The extra one stores the characters after the cursor
6475 * that were deleted (always white space).
6476 *
6477 * Replace_offset is normally 0, in which case replace_push will add a new
6478 * character at the end of the stack. If replace_offset is not 0, that many
6479 * characters will be left on the stack above the newly inserted character.
6480 */
6481
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00006482static char_u *replace_stack = NULL;
6483static long replace_stack_nr = 0; /* next entry in replace stack */
6484static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485
6486 void
6487replace_push(c)
6488 int c; /* character that is replaced (NUL is none) */
6489{
6490 char_u *p;
6491
6492 if (replace_stack_nr < replace_offset) /* nothing to do */
6493 return;
6494 if (replace_stack_len <= replace_stack_nr)
6495 {
6496 replace_stack_len += 50;
6497 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
6498 if (p == NULL) /* out of memory */
6499 {
6500 replace_stack_len -= 50;
6501 return;
6502 }
6503 if (replace_stack != NULL)
6504 {
6505 mch_memmove(p, replace_stack,
6506 (size_t)(replace_stack_nr * sizeof(char_u)));
6507 vim_free(replace_stack);
6508 }
6509 replace_stack = p;
6510 }
6511 p = replace_stack + replace_stack_nr - replace_offset;
6512 if (replace_offset)
6513 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
6514 *p = c;
6515 ++replace_stack_nr;
6516}
6517
6518/*
6519 * call replace_push(c) with replace_offset set to the first NUL.
6520 */
6521 static void
6522replace_push_off(c)
6523 int c;
6524{
6525 char_u *p;
6526
6527 p = replace_stack + replace_stack_nr;
6528 for (replace_offset = 1; replace_offset < replace_stack_nr;
6529 ++replace_offset)
6530 if (*--p == NUL)
6531 break;
6532 replace_push(c);
6533 replace_offset = 0;
6534}
6535
6536/*
6537 * Pop one item from the replace stack.
6538 * return -1 if stack empty
6539 * return replaced character or NUL otherwise
6540 */
6541 static int
6542replace_pop()
6543{
6544 if (replace_stack_nr == 0)
6545 return -1;
6546 return (int)replace_stack[--replace_stack_nr];
6547}
6548
6549/*
6550 * Join the top two items on the replace stack. This removes to "off"'th NUL
6551 * encountered.
6552 */
6553 static void
6554replace_join(off)
6555 int off; /* offset for which NUL to remove */
6556{
6557 int i;
6558
6559 for (i = replace_stack_nr; --i >= 0; )
6560 if (replace_stack[i] == NUL && off-- <= 0)
6561 {
6562 --replace_stack_nr;
6563 mch_memmove(replace_stack + i, replace_stack + i + 1,
6564 (size_t)(replace_stack_nr - i));
6565 return;
6566 }
6567}
6568
6569/*
6570 * Pop bytes from the replace stack until a NUL is found, and insert them
6571 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
6572 */
6573 static void
6574replace_pop_ins()
6575{
6576 int cc;
6577 int oldState = State;
6578
6579 State = NORMAL; /* don't want REPLACE here */
6580 while ((cc = replace_pop()) > 0)
6581 {
6582#ifdef FEAT_MBYTE
6583 mb_replace_pop_ins(cc);
6584#else
6585 ins_char(cc);
6586#endif
6587 dec_cursor();
6588 }
6589 State = oldState;
6590}
6591
6592#ifdef FEAT_MBYTE
6593/*
6594 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
6595 * indicates a multi-byte char, pop the other bytes too.
6596 */
6597 static void
6598mb_replace_pop_ins(cc)
6599 int cc;
6600{
6601 int n;
6602 char_u buf[MB_MAXBYTES];
6603 int i;
6604 int c;
6605
6606 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
6607 {
6608 buf[0] = cc;
6609 for (i = 1; i < n; ++i)
6610 buf[i] = replace_pop();
6611 ins_bytes_len(buf, n);
6612 }
6613 else
6614 ins_char(cc);
6615
6616 if (enc_utf8)
6617 /* Handle composing chars. */
6618 for (;;)
6619 {
6620 c = replace_pop();
6621 if (c == -1) /* stack empty */
6622 break;
6623 if ((n = MB_BYTE2LEN(c)) == 1)
6624 {
6625 /* Not a multi-byte char, put it back. */
6626 replace_push(c);
6627 break;
6628 }
6629 else
6630 {
6631 buf[0] = c;
6632 for (i = 1; i < n; ++i)
6633 buf[i] = replace_pop();
6634 if (utf_iscomposing(utf_ptr2char(buf)))
6635 ins_bytes_len(buf, n);
6636 else
6637 {
6638 /* Not a composing char, put it back. */
6639 for (i = n - 1; i >= 0; --i)
6640 replace_push(buf[i]);
6641 break;
6642 }
6643 }
6644 }
6645}
6646#endif
6647
6648/*
6649 * make the replace stack empty
6650 * (called when exiting replace mode)
6651 */
6652 static void
6653replace_flush()
6654{
6655 vim_free(replace_stack);
6656 replace_stack = NULL;
6657 replace_stack_len = 0;
6658 replace_stack_nr = 0;
6659}
6660
6661/*
6662 * Handle doing a BS for one character.
6663 * cc < 0: replace stack empty, just move cursor
6664 * cc == 0: character was inserted, delete it
6665 * cc > 0: character was replaced, put cc (first byte of original char) back
6666 * and check for more characters to be put back
6667 */
6668 static void
6669replace_do_bs()
6670{
6671 int cc;
6672#ifdef FEAT_VREPLACE
6673 int orig_len = 0;
6674 int ins_len;
6675 int orig_vcols = 0;
6676 colnr_T start_vcol;
6677 char_u *p;
6678 int i;
6679 int vcol;
6680#endif
6681
6682 cc = replace_pop();
6683 if (cc > 0)
6684 {
6685#ifdef FEAT_VREPLACE
6686 if (State & VREPLACE_FLAG)
6687 {
6688 /* Get the number of screen cells used by the character we are
6689 * going to delete. */
6690 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
6691 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
6692 }
6693#endif
6694#ifdef FEAT_MBYTE
6695 if (has_mbyte)
6696 {
6697 del_char(FALSE);
6698# ifdef FEAT_VREPLACE
6699 if (State & VREPLACE_FLAG)
6700 orig_len = STRLEN(ml_get_cursor());
6701# endif
6702 replace_push(cc);
6703 }
6704 else
6705#endif
6706 {
6707 pchar_cursor(cc);
6708#ifdef FEAT_VREPLACE
6709 if (State & VREPLACE_FLAG)
6710 orig_len = STRLEN(ml_get_cursor()) - 1;
6711#endif
6712 }
6713 replace_pop_ins();
6714
6715#ifdef FEAT_VREPLACE
6716 if (State & VREPLACE_FLAG)
6717 {
6718 /* Get the number of screen cells used by the inserted characters */
6719 p = ml_get_cursor();
6720 ins_len = STRLEN(p) - orig_len;
6721 vcol = start_vcol;
6722 for (i = 0; i < ins_len; ++i)
6723 {
6724 vcol += chartabsize(p + i, vcol);
6725#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006726 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727#endif
6728 }
6729 vcol -= start_vcol;
6730
6731 /* Delete spaces that were inserted after the cursor to keep the
6732 * text aligned. */
6733 curwin->w_cursor.col += ins_len;
6734 while (vcol > orig_vcols && gchar_cursor() == ' ')
6735 {
6736 del_char(FALSE);
6737 ++orig_vcols;
6738 }
6739 curwin->w_cursor.col -= ins_len;
6740 }
6741#endif
6742
6743 /* mark the buffer as changed and prepare for displaying */
6744 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
6745 }
6746 else if (cc == 0)
6747 (void)del_char(FALSE);
6748}
6749
6750#ifdef FEAT_CINDENT
6751/*
6752 * Return TRUE if C-indenting is on.
6753 */
6754 static int
6755cindent_on()
6756{
6757 return (!p_paste && (curbuf->b_p_cin
6758# ifdef FEAT_EVAL
6759 || *curbuf->b_p_inde != NUL
6760# endif
6761 ));
6762}
6763#endif
6764
6765#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
6766/*
6767 * Re-indent the current line, based on the current contents of it and the
6768 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
6769 * confused what all the part that handles Control-T is doing that I'm not.
6770 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
6771 */
6772
6773 void
6774fixthisline(get_the_indent)
6775 int (*get_the_indent) __ARGS((void));
6776{
6777 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
6778 if (linewhite(curwin->w_cursor.lnum))
6779 did_ai = TRUE; /* delete the indent if the line stays empty */
6780}
6781
6782 void
6783fix_indent()
6784{
6785 if (p_paste)
6786 return;
6787# ifdef FEAT_LISP
6788 if (curbuf->b_p_lisp && curbuf->b_p_ai)
6789 fixthisline(get_lisp_indent);
6790# endif
6791# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
6792 else
6793# endif
6794# ifdef FEAT_CINDENT
6795 if (cindent_on())
6796 do_c_expr_indent();
6797# endif
6798}
6799
6800#endif
6801
6802#ifdef FEAT_CINDENT
6803/*
6804 * return TRUE if 'cinkeys' contains the key "keytyped",
6805 * when == '*': Only if key is preceded with '*' (indent before insert)
6806 * when == '!': Only if key is prededed with '!' (don't insert)
6807 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
6808 *
6809 * "keytyped" can have a few special values:
6810 * KEY_OPEN_FORW
6811 * KEY_OPEN_BACK
6812 * KEY_COMPLETE just finished completion.
6813 *
6814 * If line_is_empty is TRUE accept keys with '0' before them.
6815 */
6816 int
6817in_cinkeys(keytyped, when, line_is_empty)
6818 int keytyped;
6819 int when;
6820 int line_is_empty;
6821{
6822 char_u *look;
6823 int try_match;
6824 int try_match_word;
6825 char_u *p;
6826 char_u *line;
6827 int icase;
6828 int i;
6829
6830#ifdef FEAT_EVAL
6831 if (*curbuf->b_p_inde != NUL)
6832 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
6833 else
6834#endif
6835 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
6836 while (*look)
6837 {
6838 /*
6839 * Find out if we want to try a match with this key, depending on
6840 * 'when' and a '*' or '!' before the key.
6841 */
6842 switch (when)
6843 {
6844 case '*': try_match = (*look == '*'); break;
6845 case '!': try_match = (*look == '!'); break;
6846 default: try_match = (*look != '*'); break;
6847 }
6848 if (*look == '*' || *look == '!')
6849 ++look;
6850
6851 /*
6852 * If there is a '0', only accept a match if the line is empty.
6853 * But may still match when typing last char of a word.
6854 */
6855 if (*look == '0')
6856 {
6857 try_match_word = try_match;
6858 if (!line_is_empty)
6859 try_match = FALSE;
6860 ++look;
6861 }
6862 else
6863 try_match_word = FALSE;
6864
6865 /*
6866 * does it look like a control character?
6867 */
6868 if (*look == '^'
6869#ifdef EBCDIC
6870 && (Ctrl_chr(look[1]) != 0)
6871#else
6872 && look[1] >= '?' && look[1] <= '_'
6873#endif
6874 )
6875 {
6876 if (try_match && keytyped == Ctrl_chr(look[1]))
6877 return TRUE;
6878 look += 2;
6879 }
6880 /*
6881 * 'o' means "o" command, open forward.
6882 * 'O' means "O" command, open backward.
6883 */
6884 else if (*look == 'o')
6885 {
6886 if (try_match && keytyped == KEY_OPEN_FORW)
6887 return TRUE;
6888 ++look;
6889 }
6890 else if (*look == 'O')
6891 {
6892 if (try_match && keytyped == KEY_OPEN_BACK)
6893 return TRUE;
6894 ++look;
6895 }
6896
6897 /*
6898 * 'e' means to check for "else" at start of line and just before the
6899 * cursor.
6900 */
6901 else if (*look == 'e')
6902 {
6903 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
6904 {
6905 p = ml_get_curline();
6906 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
6907 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
6908 return TRUE;
6909 }
6910 ++look;
6911 }
6912
6913 /*
6914 * ':' only causes an indent if it is at the end of a label or case
6915 * statement, or when it was before typing the ':' (to fix
6916 * class::method for C++).
6917 */
6918 else if (*look == ':')
6919 {
6920 if (try_match && keytyped == ':')
6921 {
6922 p = ml_get_curline();
6923 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
6924 return TRUE;
6925 if (curwin->w_cursor.col > 2
6926 && p[curwin->w_cursor.col - 1] == ':'
6927 && p[curwin->w_cursor.col - 2] == ':')
6928 {
6929 p[curwin->w_cursor.col - 1] = ' ';
6930 i = (cin_iscase(p) || cin_isscopedecl(p)
6931 || cin_islabel(30));
6932 p = ml_get_curline();
6933 p[curwin->w_cursor.col - 1] = ':';
6934 if (i)
6935 return TRUE;
6936 }
6937 }
6938 ++look;
6939 }
6940
6941
6942 /*
6943 * Is it a key in <>, maybe?
6944 */
6945 else if (*look == '<')
6946 {
6947 if (try_match)
6948 {
6949 /*
6950 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
6951 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
6952 * >, *, : and ! keys if they really really want to.
6953 */
6954 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
6955 && keytyped == look[1])
6956 return TRUE;
6957
6958 if (keytyped == get_special_key_code(look + 1))
6959 return TRUE;
6960 }
6961 while (*look && *look != '>')
6962 look++;
6963 while (*look == '>')
6964 look++;
6965 }
6966
6967 /*
6968 * Is it a word: "=word"?
6969 */
6970 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
6971 {
6972 ++look;
6973 if (*look == '~')
6974 {
6975 icase = TRUE;
6976 ++look;
6977 }
6978 else
6979 icase = FALSE;
6980 p = vim_strchr(look, ',');
6981 if (p == NULL)
6982 p = look + STRLEN(look);
6983 if ((try_match || try_match_word)
6984 && curwin->w_cursor.col >= (colnr_T)(p - look))
6985 {
6986 int match = FALSE;
6987
6988#ifdef FEAT_INS_EXPAND
6989 if (keytyped == KEY_COMPLETE)
6990 {
6991 char_u *s;
6992
6993 /* Just completed a word, check if it starts with "look".
6994 * search back for the start of a word. */
6995 line = ml_get_curline();
6996# ifdef FEAT_MBYTE
6997 if (has_mbyte)
6998 {
6999 char_u *n;
7000
7001 for (s = line + curwin->w_cursor.col; s > line; s = n)
7002 {
7003 n = mb_prevptr(line, s);
7004 if (!vim_iswordp(n))
7005 break;
7006 }
7007 }
7008 else
7009# endif
7010 for (s = line + curwin->w_cursor.col; s > line; --s)
7011 if (!vim_iswordc(s[-1]))
7012 break;
7013 if (s + (p - look) <= line + curwin->w_cursor.col
7014 && (icase
7015 ? MB_STRNICMP(s, look, p - look)
7016 : STRNCMP(s, look, p - look)) == 0)
7017 match = TRUE;
7018 }
7019 else
7020#endif
7021 /* TODO: multi-byte */
7022 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7023 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7024 {
7025 line = ml_get_cursor();
7026 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7027 || !vim_iswordc(line[-(p - look) - 1]))
7028 && (icase
7029 ? MB_STRNICMP(line - (p - look), look, p - look)
7030 : STRNCMP(line - (p - look), look, p - look))
7031 == 0)
7032 match = TRUE;
7033 }
7034 if (match && try_match_word && !try_match)
7035 {
7036 /* "0=word": Check if there are only blanks before the
7037 * word. */
7038 line = ml_get_curline();
7039 if ((int)(skipwhite(line) - line) !=
7040 (int)(curwin->w_cursor.col - (p - look)))
7041 match = FALSE;
7042 }
7043 if (match)
7044 return TRUE;
7045 }
7046 look = p;
7047 }
7048
7049 /*
7050 * ok, it's a boring generic character.
7051 */
7052 else
7053 {
7054 if (try_match && *look == keytyped)
7055 return TRUE;
7056 ++look;
7057 }
7058
7059 /*
7060 * Skip over ", ".
7061 */
7062 look = skip_to_option_part(look);
7063 }
7064 return FALSE;
7065}
7066#endif /* FEAT_CINDENT */
7067
7068#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7069/*
7070 * Map Hebrew keyboard when in hkmap mode.
7071 */
7072 int
7073hkmap(c)
7074 int c;
7075{
7076 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7077 {
7078 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7079 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7080 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7081 static char_u map[26] =
7082 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7083 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7084 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7085 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7086 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7087 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7088 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7089 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7090 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7091
7092 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7093 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7094 /* '-1'='sofit' */
7095 else if (c == 'x')
7096 return 'X';
7097 else if (c == 'q')
7098 return '\''; /* {geresh}={'} */
7099 else if (c == 246)
7100 return ' '; /* \"o --> ' ' for a german keyboard */
7101 else if (c == 228)
7102 return ' '; /* \"a --> ' ' -- / -- */
7103 else if (c == 252)
7104 return ' '; /* \"u --> ' ' -- / -- */
7105#ifdef EBCDIC
7106 else if (islower(c))
7107#else
7108 /* NOTE: islower() does not do the right thing for us on Linux so we
7109 * do this the same was as 5.7 and previous, so it works correctly on
7110 * all systems. Specifically, the e.g. Delete and Arrow keys are
7111 * munged and won't work if e.g. searching for Hebrew text.
7112 */
7113 else if (c >= 'a' && c <= 'z')
7114#endif
7115 return (int)(map[CharOrdLow(c)] + p_aleph);
7116 else
7117 return c;
7118 }
7119 else
7120 {
7121 switch (c)
7122 {
7123 case '`': return ';';
7124 case '/': return '.';
7125 case '\'': return ',';
7126 case 'q': return '/';
7127 case 'w': return '\'';
7128
7129 /* Hebrew letters - set offset from 'a' */
7130 case ',': c = '{'; break;
7131 case '.': c = 'v'; break;
7132 case ';': c = 't'; break;
7133 default: {
7134 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7135
7136#ifdef EBCDIC
7137 /* see note about islower() above */
7138 if (!islower(c))
7139#else
7140 if (c < 'a' || c > 'z')
7141#endif
7142 return c;
7143 c = str[CharOrdLow(c)];
7144 break;
7145 }
7146 }
7147
7148 return (int)(CharOrdLow(c) + p_aleph);
7149 }
7150}
7151#endif
7152
7153 static void
7154ins_reg()
7155{
7156 int need_redraw = FALSE;
7157 int regname;
7158 int literally = 0;
7159
7160 /*
7161 * If we are going to wait for a character, show a '"'.
7162 */
7163 pc_status = PC_STATUS_UNSET;
7164 if (redrawing() && !char_avail())
7165 {
7166 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007167 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168
7169 edit_putchar('"', TRUE);
7170#ifdef FEAT_CMDL_INFO
7171 add_to_showcmd_c(Ctrl_R);
7172#endif
7173 }
7174
7175#ifdef USE_ON_FLY_SCROLL
7176 dont_scroll = TRUE; /* disallow scrolling here */
7177#endif
7178
7179 /*
7180 * Don't map the register name. This also prevents the mode message to be
7181 * deleted when ESC is hit.
7182 */
7183 ++no_mapping;
7184 regname = safe_vgetc();
7185#ifdef FEAT_LANGMAP
7186 LANGMAP_ADJUST(regname, TRUE);
7187#endif
7188 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
7189 {
7190 /* Get a third key for literal register insertion */
7191 literally = regname;
7192#ifdef FEAT_CMDL_INFO
7193 add_to_showcmd_c(literally);
7194#endif
7195 regname = safe_vgetc();
7196#ifdef FEAT_LANGMAP
7197 LANGMAP_ADJUST(regname, TRUE);
7198#endif
7199 }
7200 --no_mapping;
7201
7202#ifdef FEAT_EVAL
7203 /*
7204 * Don't call u_sync() while getting the expression,
7205 * evaluating it or giving an error message for it!
7206 */
7207 ++no_u_sync;
7208 if (regname == '=')
7209 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007210# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007212# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007214# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 /* Restore the Input Method. */
7216 if (im_on)
7217 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007218# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00007220 if (regname == NUL || !valid_yank_reg(regname, FALSE))
7221 {
7222 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 else
7226 {
7227#endif
7228 if (literally == Ctrl_O || literally == Ctrl_P)
7229 {
7230 /* Append the command to the redo buffer. */
7231 AppendCharToRedobuff(Ctrl_R);
7232 AppendCharToRedobuff(literally);
7233 AppendCharToRedobuff(regname);
7234
7235 do_put(regname, BACKWARD, 1L,
7236 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
7237 }
7238 else if (insert_reg(regname, literally) == FAIL)
7239 {
7240 vim_beep();
7241 need_redraw = TRUE; /* remove the '"' */
7242 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007243 else if (stop_insert_mode)
7244 /* When the '=' register was used and a function was invoked that
7245 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7246 * insert anything, need to remove the '"' */
7247 need_redraw = TRUE;
7248
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249#ifdef FEAT_EVAL
7250 }
7251 --no_u_sync;
7252#endif
7253#ifdef FEAT_CMDL_INFO
7254 clear_showcmd();
7255#endif
7256
7257 /* If the inserted register is empty, we need to remove the '"' */
7258 if (need_redraw || stuff_empty())
7259 edit_unputchar();
7260}
7261
7262/*
7263 * CTRL-G commands in Insert mode.
7264 */
7265 static void
7266ins_ctrl_g()
7267{
7268 int c;
7269
7270#ifdef FEAT_INS_EXPAND
7271 /* Right after CTRL-X the cursor will be after the ruler. */
7272 setcursor();
7273#endif
7274
7275 /*
7276 * Don't map the second key. This also prevents the mode message to be
7277 * deleted when ESC is hit.
7278 */
7279 ++no_mapping;
7280 c = safe_vgetc();
7281 --no_mapping;
7282 switch (c)
7283 {
7284 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7285 case K_UP:
7286 case Ctrl_K:
7287 case 'k': ins_up(TRUE);
7288 break;
7289
7290 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7291 case K_DOWN:
7292 case Ctrl_J:
7293 case 'j': ins_down(TRUE);
7294 break;
7295
7296 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00007297 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007299
7300 /* Need to reset Insstart, esp. because a BS that joins
7301 * aline to the previous one must save for undo. */
7302 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 break;
7304
7305 /* Unknown CTRL-G command, reserved for future expansion. */
7306 default: vim_beep();
7307 }
7308}
7309
7310/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007311 * CTRL-^ in Insert mode.
7312 */
7313 static void
7314ins_ctrl_hat()
7315{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00007316 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007317 {
7318 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7319 if (State & LANGMAP)
7320 {
7321 curbuf->b_p_iminsert = B_IMODE_NONE;
7322 State &= ~LANGMAP;
7323 }
7324 else
7325 {
7326 curbuf->b_p_iminsert = B_IMODE_LMAP;
7327 State |= LANGMAP;
7328#ifdef USE_IM_CONTROL
7329 im_set_active(FALSE);
7330#endif
7331 }
7332 }
7333#ifdef USE_IM_CONTROL
7334 else
7335 {
7336 /* There are no ":lmap" mappings, toggle IM */
7337 if (im_get_status())
7338 {
7339 curbuf->b_p_iminsert = B_IMODE_NONE;
7340 im_set_active(FALSE);
7341 }
7342 else
7343 {
7344 curbuf->b_p_iminsert = B_IMODE_IM;
7345 State &= ~LANGMAP;
7346 im_set_active(TRUE);
7347 }
7348 }
7349#endif
7350 set_iminsert_global();
7351 showmode();
7352#ifdef FEAT_GUI
7353 /* may show different cursor shape or color */
7354 if (gui.in_use)
7355 gui_update_cursor(TRUE, FALSE);
7356#endif
7357#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7358 /* Show/unshow value of 'keymap' in status lines. */
7359 status_redraw_curbuf();
7360#endif
7361}
7362
7363/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 * Handle ESC in insert mode.
7365 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7366 * insert.
7367 */
7368 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00007369ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 long *count;
7371 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00007372 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373{
7374 int temp;
7375 static int disabled_redraw = FALSE;
7376
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007377#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007378 check_spell_redraw();
7379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380#if defined(FEAT_HANGULIN)
7381# if defined(ESC_CHG_TO_ENG_MODE)
7382 hangul_input_state_set(0);
7383# endif
7384 if (composing_hangul)
7385 {
7386 push_raw_key(composing_hangul_buffer, 2);
7387 composing_hangul = 0;
7388 }
7389#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390
7391 temp = curwin->w_cursor.col;
7392 if (disabled_redraw)
7393 {
7394 --RedrawingDisabled;
7395 disabled_redraw = FALSE;
7396 }
7397 if (!arrow_used)
7398 {
7399 /*
7400 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00007401 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7402 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 */
7404 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00007405 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406
7407 /*
7408 * Repeating insert may take a long time. Check for
7409 * interrupt now and then.
7410 */
7411 if (*count > 0)
7412 {
7413 line_breakcheck();
7414 if (got_int)
7415 *count = 0;
7416 }
7417
7418 if (--*count > 0) /* repeat what was typed */
7419 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007420 /* Vi repeats the insert without replacing characters. */
7421 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
7422 State &= ~REPLACE_FLAG;
7423
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 (void)start_redo_ins();
7425 if (cmdchar == 'r' || cmdchar == 'v')
7426 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
7427 ++RedrawingDisabled;
7428 disabled_redraw = TRUE;
7429 return FALSE; /* repeat the insert */
7430 }
7431 stop_insert(&curwin->w_cursor, TRUE);
7432 undisplay_dollar();
7433 }
7434
7435 /* When an autoindent was removed, curswant stays after the
7436 * indent */
7437 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
7438 curwin->w_set_curswant = TRUE;
7439
7440 /* Remember the last Insert position in the '^ mark. */
7441 if (!cmdmod.keepjumps)
7442 curbuf->b_last_insert = curwin->w_cursor;
7443
7444 /*
7445 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00007446 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00007448 if (!nomove
7449 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450#ifdef FEAT_VIRTUALEDIT
7451 || curwin->w_cursor.coladd > 0
7452#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007453 )
7454 && (restart_edit == NUL
7455 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00007457 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007459 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460#ifdef FEAT_RIGHTLEFT
7461 && !revins_on
7462#endif
7463 )
7464 {
7465#ifdef FEAT_VIRTUALEDIT
7466 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
7467 {
7468 oneleft();
7469 if (restart_edit != NUL)
7470 ++curwin->w_cursor.coladd;
7471 }
7472 else
7473#endif
7474 {
7475 --curwin->w_cursor.col;
7476#ifdef FEAT_MBYTE
7477 /* Correct cursor for multi-byte character. */
7478 if (has_mbyte)
7479 mb_adjust_cursor();
7480#endif
7481 }
7482 }
7483
7484#ifdef USE_IM_CONTROL
7485 /* Disable IM to allow typing English directly for Normal mode commands.
7486 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
7487 * well). */
7488 if (!(State & LANGMAP))
7489 im_save_status(&curbuf->b_p_iminsert);
7490 im_set_active(FALSE);
7491#endif
7492
7493 State = NORMAL;
7494 /* need to position cursor again (e.g. when on a TAB ) */
7495 changed_cline_bef_curs();
7496
7497#ifdef FEAT_MOUSE
7498 setmouse();
7499#endif
7500#ifdef CURSOR_SHAPE
7501 ui_cursor_shape(); /* may show different cursor shape */
7502#endif
7503
7504 /*
7505 * When recording or for CTRL-O, need to display the new mode.
7506 * Otherwise remove the mode message.
7507 */
7508 if (Recording || restart_edit != NUL)
7509 showmode();
7510 else if (p_smd)
7511 MSG("");
7512
7513 return TRUE; /* exit Insert mode */
7514}
7515
7516#ifdef FEAT_RIGHTLEFT
7517/*
7518 * Toggle language: hkmap and revins_on.
7519 * Move to end of reverse inserted text.
7520 */
7521 static void
7522ins_ctrl_()
7523{
7524 if (revins_on && revins_chars && revins_scol >= 0)
7525 {
7526 while (gchar_cursor() != NUL && revins_chars--)
7527 ++curwin->w_cursor.col;
7528 }
7529 p_ri = !p_ri;
7530 revins_on = (State == INSERT && p_ri);
7531 if (revins_on)
7532 {
7533 revins_scol = curwin->w_cursor.col;
7534 revins_legal++;
7535 revins_chars = 0;
7536 undisplay_dollar();
7537 }
7538 else
7539 revins_scol = -1;
7540#ifdef FEAT_FKMAP
7541 if (p_altkeymap)
7542 {
7543 /*
7544 * to be consistent also for redo command, using '.'
7545 * set arrow_used to true and stop it - causing to redo
7546 * characters entered in one mode (normal/reverse insert).
7547 */
7548 arrow_used = TRUE;
7549 (void)stop_arrow();
7550 p_fkmap = curwin->w_p_rl ^ p_ri;
7551 if (p_fkmap && p_ri)
7552 State = INSERT;
7553 }
7554 else
7555#endif
7556 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
7557 showmode();
7558}
7559#endif
7560
7561#ifdef FEAT_VISUAL
7562/*
7563 * If 'keymodel' contains "startsel", may start selection.
7564 * Returns TRUE when a CTRL-O and other keys stuffed.
7565 */
7566 static int
7567ins_start_select(c)
7568 int c;
7569{
7570 if (km_startsel)
7571 switch (c)
7572 {
7573 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 case K_PAGEUP:
7576 case K_KPAGEUP:
7577 case K_PAGEDOWN:
7578 case K_KPAGEDOWN:
7579# ifdef MACOS
7580 case K_LEFT:
7581 case K_RIGHT:
7582 case K_UP:
7583 case K_DOWN:
7584 case K_END:
7585 case K_HOME:
7586# endif
7587 if (!(mod_mask & MOD_MASK_SHIFT))
7588 break;
7589 /* FALLTHROUGH */
7590 case K_S_LEFT:
7591 case K_S_RIGHT:
7592 case K_S_UP:
7593 case K_S_DOWN:
7594 case K_S_END:
7595 case K_S_HOME:
7596 /* Start selection right away, the cursor can move with
7597 * CTRL-O when beyond the end of the line. */
7598 start_selection();
7599
7600 /* Execute the key in (insert) Select mode. */
7601 stuffcharReadbuff(Ctrl_O);
7602 if (mod_mask)
7603 {
7604 char_u buf[4];
7605
7606 buf[0] = K_SPECIAL;
7607 buf[1] = KS_MODIFIER;
7608 buf[2] = mod_mask;
7609 buf[3] = NUL;
7610 stuffReadbuff(buf);
7611 }
7612 stuffcharReadbuff(c);
7613 return TRUE;
7614 }
7615 return FALSE;
7616}
7617#endif
7618
7619/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007620 * <Insert> key in Insert mode: toggle insert/remplace mode.
7621 */
7622 static void
7623ins_insert(replaceState)
7624 int replaceState;
7625{
7626#ifdef FEAT_FKMAP
7627 if (p_fkmap && p_ri)
7628 {
7629 beep_flush();
7630 EMSG(farsi_text_3); /* encoded in Farsi */
7631 return;
7632 }
7633#endif
7634
7635#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00007636# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007637 set_vim_var_string(VV_INSERTMODE,
7638 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007639# ifdef FEAT_VREPLACE
7640 replaceState == VREPLACE ? "v" :
7641# endif
7642 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00007643# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007644 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
7645#endif
7646 if (State & REPLACE_FLAG)
7647 State = INSERT | (State & LANGMAP);
7648 else
7649 State = replaceState | (State & LANGMAP);
7650 AppendCharToRedobuff(K_INS);
7651 showmode();
7652#ifdef CURSOR_SHAPE
7653 ui_cursor_shape(); /* may show different cursor shape */
7654#endif
7655}
7656
7657/*
7658 * Pressed CTRL-O in Insert mode.
7659 */
7660 static void
7661ins_ctrl_o()
7662{
7663#ifdef FEAT_VREPLACE
7664 if (State & VREPLACE_FLAG)
7665 restart_edit = 'V';
7666 else
7667#endif
7668 if (State & REPLACE_FLAG)
7669 restart_edit = 'R';
7670 else
7671 restart_edit = 'I';
7672#ifdef FEAT_VIRTUALEDIT
7673 if (virtual_active())
7674 ins_at_eol = FALSE; /* cursor always keeps its column */
7675 else
7676#endif
7677 ins_at_eol = (gchar_cursor() == NUL);
7678}
7679
7680/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 * If the cursor is on an indent, ^T/^D insert/delete one
7682 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
7683 * Always round the indent to 'shiftwith', this is compatible
7684 * with vi. But vi only supports ^T and ^D after an
7685 * autoindent, we support it everywhere.
7686 */
7687 static void
7688ins_shift(c, lastc)
7689 int c;
7690 int lastc;
7691{
7692 if (stop_arrow() == FAIL)
7693 return;
7694 AppendCharToRedobuff(c);
7695
7696 /*
7697 * 0^D and ^^D: remove all indent.
7698 */
7699 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
7700 {
7701 --curwin->w_cursor.col;
7702 (void)del_char(FALSE); /* delete the '^' or '0' */
7703 /* In Replace mode, restore the characters that '^' or '0' replaced. */
7704 if (State & REPLACE_FLAG)
7705 replace_pop_ins();
7706 if (lastc == '^')
7707 old_indent = get_indent(); /* remember curr. indent */
7708 change_indent(INDENT_SET, 0, TRUE, 0);
7709 }
7710 else
7711 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
7712
7713 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
7714 did_ai = FALSE;
7715#ifdef FEAT_SMARTINDENT
7716 did_si = FALSE;
7717 can_si = FALSE;
7718 can_si_back = FALSE;
7719#endif
7720#ifdef FEAT_CINDENT
7721 can_cindent = FALSE; /* no cindenting after ^D or ^T */
7722#endif
7723}
7724
7725 static void
7726ins_del()
7727{
7728 int temp;
7729
7730 if (stop_arrow() == FAIL)
7731 return;
7732 if (gchar_cursor() == NUL) /* delete newline */
7733 {
7734 temp = curwin->w_cursor.col;
7735 if (!can_bs(BS_EOL) /* only if "eol" included */
7736 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
7737 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
7738 || do_join(FALSE) == FAIL)
7739 vim_beep();
7740 else
7741 curwin->w_cursor.col = temp;
7742 }
7743 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
7744 vim_beep();
7745 did_ai = FALSE;
7746#ifdef FEAT_SMARTINDENT
7747 did_si = FALSE;
7748 can_si = FALSE;
7749 can_si_back = FALSE;
7750#endif
7751 AppendCharToRedobuff(K_DEL);
7752}
7753
7754/*
7755 * Handle Backspace, delete-word and delete-line in Insert mode.
7756 * Return TRUE when backspace was actually used.
7757 */
7758 static int
7759ins_bs(c, mode, inserted_space_p)
7760 int c;
7761 int mode;
7762 int *inserted_space_p;
7763{
7764 linenr_T lnum;
7765 int cc;
7766 int temp = 0; /* init for GCC */
7767 colnr_T mincol;
7768 int did_backspace = FALSE;
7769 int in_indent;
7770 int oldState;
7771#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007772 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773#endif
7774
7775 /*
7776 * can't delete anything in an empty file
7777 * can't backup past first character in buffer
7778 * can't backup past starting point unless 'backspace' > 1
7779 * can backup to a previous line if 'backspace' == 0
7780 */
7781 if ( bufempty()
7782 || (
7783#ifdef FEAT_RIGHTLEFT
7784 !revins_on &&
7785#endif
7786 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
7787 || (!can_bs(BS_START)
7788 && (arrow_used
7789 || (curwin->w_cursor.lnum == Insstart.lnum
7790 && curwin->w_cursor.col <= Insstart.col)))
7791 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
7792 && curwin->w_cursor.col <= ai_col)
7793 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
7794 {
7795 vim_beep();
7796 return FALSE;
7797 }
7798
7799 if (stop_arrow() == FAIL)
7800 return FALSE;
7801 in_indent = inindent(0);
7802#ifdef FEAT_CINDENT
7803 if (in_indent)
7804 can_cindent = FALSE;
7805#endif
7806#ifdef FEAT_COMMENTS
7807 end_comment_pending = NUL; /* After BS, don't auto-end comment */
7808#endif
7809#ifdef FEAT_RIGHTLEFT
7810 if (revins_on) /* put cursor after last inserted char */
7811 inc_cursor();
7812#endif
7813
7814#ifdef FEAT_VIRTUALEDIT
7815 /* Virtualedit:
7816 * BACKSPACE_CHAR eats a virtual space
7817 * BACKSPACE_WORD eats all coladd
7818 * BACKSPACE_LINE eats all coladd and keeps going
7819 */
7820 if (curwin->w_cursor.coladd > 0)
7821 {
7822 if (mode == BACKSPACE_CHAR)
7823 {
7824 --curwin->w_cursor.coladd;
7825 return TRUE;
7826 }
7827 if (mode == BACKSPACE_WORD)
7828 {
7829 curwin->w_cursor.coladd = 0;
7830 return TRUE;
7831 }
7832 curwin->w_cursor.coladd = 0;
7833 }
7834#endif
7835
7836 /*
7837 * delete newline!
7838 */
7839 if (curwin->w_cursor.col == 0)
7840 {
7841 lnum = Insstart.lnum;
7842 if (curwin->w_cursor.lnum == Insstart.lnum
7843#ifdef FEAT_RIGHTLEFT
7844 || revins_on
7845#endif
7846 )
7847 {
7848 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
7849 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
7850 return FALSE;
7851 --Insstart.lnum;
7852 Insstart.col = MAXCOL;
7853 }
7854 /*
7855 * In replace mode:
7856 * cc < 0: NL was inserted, delete it
7857 * cc >= 0: NL was replaced, put original characters back
7858 */
7859 cc = -1;
7860 if (State & REPLACE_FLAG)
7861 cc = replace_pop(); /* returns -1 if NL was inserted */
7862 /*
7863 * In replace mode, in the line we started replacing, we only move the
7864 * cursor.
7865 */
7866 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
7867 {
7868 dec_cursor();
7869 }
7870 else
7871 {
7872#ifdef FEAT_VREPLACE
7873 if (!(State & VREPLACE_FLAG)
7874 || curwin->w_cursor.lnum > orig_line_count)
7875#endif
7876 {
7877 temp = gchar_cursor(); /* remember current char */
7878 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00007879
7880 /* When "aw" is in 'formatoptions' we must delete the space at
7881 * the end of the line, otherwise the line will be broken
7882 * again when auto-formatting. */
7883 if (has_format_option(FO_AUTO)
7884 && has_format_option(FO_WHITE_PAR))
7885 {
7886 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
7887 TRUE);
7888 int len;
7889
7890 len = STRLEN(ptr);
7891 if (len > 0 && ptr[len - 1] == ' ')
7892 ptr[len - 1] = NUL;
7893 }
7894
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 (void)do_join(FALSE);
7896 if (temp == NUL && gchar_cursor() != NUL)
7897 inc_cursor();
7898 }
7899#ifdef FEAT_VREPLACE
7900 else
7901 dec_cursor();
7902#endif
7903
7904 /*
7905 * In REPLACE mode we have to put back the text that was replaced
7906 * by the NL. On the replace stack is first a NUL-terminated
7907 * sequence of characters that were deleted and then the
7908 * characters that NL replaced.
7909 */
7910 if (State & REPLACE_FLAG)
7911 {
7912 /*
7913 * Do the next ins_char() in NORMAL state, to
7914 * prevent ins_char() from replacing characters and
7915 * avoiding showmatch().
7916 */
7917 oldState = State;
7918 State = NORMAL;
7919 /*
7920 * restore characters (blanks) deleted after cursor
7921 */
7922 while (cc > 0)
7923 {
7924 temp = curwin->w_cursor.col;
7925#ifdef FEAT_MBYTE
7926 mb_replace_pop_ins(cc);
7927#else
7928 ins_char(cc);
7929#endif
7930 curwin->w_cursor.col = temp;
7931 cc = replace_pop();
7932 }
7933 /* restore the characters that NL replaced */
7934 replace_pop_ins();
7935 State = oldState;
7936 }
7937 }
7938 did_ai = FALSE;
7939 }
7940 else
7941 {
7942 /*
7943 * Delete character(s) before the cursor.
7944 */
7945#ifdef FEAT_RIGHTLEFT
7946 if (revins_on) /* put cursor on last inserted char */
7947 dec_cursor();
7948#endif
7949 mincol = 0;
7950 /* keep indent */
7951 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
7952#ifdef FEAT_RIGHTLEFT
7953 && !revins_on
7954#endif
7955 )
7956 {
7957 temp = curwin->w_cursor.col;
7958 beginline(BL_WHITE);
7959 if (curwin->w_cursor.col < (colnr_T)temp)
7960 mincol = curwin->w_cursor.col;
7961 curwin->w_cursor.col = temp;
7962 }
7963
7964 /*
7965 * Handle deleting one 'shiftwidth' or 'softtabstop'.
7966 */
7967 if ( mode == BACKSPACE_CHAR
7968 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00007969 || (curbuf->b_p_sts != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 && (*(ml_get_cursor() - 1) == TAB
7971 || (*(ml_get_cursor() - 1) == ' '
7972 && (!*inserted_space_p
7973 || arrow_used))))))
7974 {
7975 int ts;
7976 colnr_T vcol;
7977 colnr_T want_vcol;
7978 int extra = 0;
7979
7980 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00007981 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 ts = curbuf->b_p_sw;
7983 else
7984 ts = curbuf->b_p_sts;
7985 /* Compute the virtual column where we want to be. Since
7986 * 'showbreak' may get in the way, need to get the last column of
7987 * the previous character. */
7988 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7989 dec_cursor();
7990 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
7991 inc_cursor();
7992 want_vcol = (want_vcol / ts) * ts;
7993
7994 /* delete characters until we are at or before want_vcol */
7995 while (vcol > want_vcol
7996 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
7997 {
7998 dec_cursor();
7999 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8000 if (State & REPLACE_FLAG)
8001 {
8002 /* Don't delete characters before the insert point when in
8003 * Replace mode */
8004 if (curwin->w_cursor.lnum != Insstart.lnum
8005 || curwin->w_cursor.col >= Insstart.col)
8006 {
8007#if 0 /* what was this for? It causes problems when sw != ts. */
8008 if (State == REPLACE && (int)vcol < want_vcol)
8009 {
8010 (void)del_char(FALSE);
8011 extra = 2; /* don't pop too much */
8012 }
8013 else
8014#endif
8015 replace_do_bs();
8016 }
8017 }
8018 else
8019 (void)del_char(FALSE);
8020 }
8021
8022 /* insert extra spaces until we are at want_vcol */
8023 while (vcol < want_vcol)
8024 {
8025 /* Remember the first char we inserted */
8026 if (curwin->w_cursor.lnum == Insstart.lnum
8027 && curwin->w_cursor.col < Insstart.col)
8028 Insstart.col = curwin->w_cursor.col;
8029
8030#ifdef FEAT_VREPLACE
8031 if (State & VREPLACE_FLAG)
8032 ins_char(' ');
8033 else
8034#endif
8035 {
8036 ins_str((char_u *)" ");
8037 if ((State & REPLACE_FLAG) && extra <= 1)
8038 {
8039 if (extra)
8040 replace_push_off(NUL);
8041 else
8042 replace_push(NUL);
8043 }
8044 if (extra == 2)
8045 extra = 1;
8046 }
8047 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8048 }
8049 }
8050
8051 /*
8052 * Delete upto starting point, start of line or previous word.
8053 */
8054 else do
8055 {
8056#ifdef FEAT_RIGHTLEFT
8057 if (!revins_on) /* put cursor on char to be deleted */
8058#endif
8059 dec_cursor();
8060
8061 /* start of word? */
8062 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
8063 {
8064 mode = BACKSPACE_WORD_NOT_SPACE;
8065 temp = vim_iswordc(gchar_cursor());
8066 }
8067 /* end of word? */
8068 else if (mode == BACKSPACE_WORD_NOT_SPACE
8069 && (vim_isspace(cc = gchar_cursor())
8070 || vim_iswordc(cc) != temp))
8071 {
8072#ifdef FEAT_RIGHTLEFT
8073 if (!revins_on)
8074#endif
8075 inc_cursor();
8076#ifdef FEAT_RIGHTLEFT
8077 else if (State & REPLACE_FLAG)
8078 dec_cursor();
8079#endif
8080 break;
8081 }
8082 if (State & REPLACE_FLAG)
8083 replace_do_bs();
8084 else
8085 {
8086#ifdef FEAT_MBYTE
8087 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008088 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089#endif
8090 (void)del_char(FALSE);
8091#ifdef FEAT_MBYTE
8092 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008093 * If there are combining characters and 'delcombine' is set
8094 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 * character.
8096 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008097 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 inc_cursor();
8099#endif
8100#ifdef FEAT_RIGHTLEFT
8101 if (revins_chars)
8102 {
8103 revins_chars--;
8104 revins_legal++;
8105 }
8106 if (revins_on && gchar_cursor() == NUL)
8107 break;
8108#endif
8109 }
8110 /* Just a single backspace?: */
8111 if (mode == BACKSPACE_CHAR)
8112 break;
8113 } while (
8114#ifdef FEAT_RIGHTLEFT
8115 revins_on ||
8116#endif
8117 (curwin->w_cursor.col > mincol
8118 && (curwin->w_cursor.lnum != Insstart.lnum
8119 || curwin->w_cursor.col != Insstart.col)));
8120 did_backspace = TRUE;
8121 }
8122#ifdef FEAT_SMARTINDENT
8123 did_si = FALSE;
8124 can_si = FALSE;
8125 can_si_back = FALSE;
8126#endif
8127 if (curwin->w_cursor.col <= 1)
8128 did_ai = FALSE;
8129 /*
8130 * It's a little strange to put backspaces into the redo
8131 * buffer, but it makes auto-indent a lot easier to deal
8132 * with.
8133 */
8134 AppendCharToRedobuff(c);
8135
8136 /* If deleted before the insertion point, adjust it */
8137 if (curwin->w_cursor.lnum == Insstart.lnum
8138 && curwin->w_cursor.col < Insstart.col)
8139 Insstart.col = curwin->w_cursor.col;
8140
8141 /* vi behaviour: the cursor moves backward but the character that
8142 * was there remains visible
8143 * Vim behaviour: the cursor moves backward and the character that
8144 * was there is erased from the screen.
8145 * We can emulate the vi behaviour by pretending there is a dollar
8146 * displayed even when there isn't.
8147 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8148 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
8149 dollar_vcol = curwin->w_virtcol;
8150
8151 return did_backspace;
8152}
8153
8154#ifdef FEAT_MOUSE
8155 static void
8156ins_mouse(c)
8157 int c;
8158{
8159 pos_T tpos;
8160
8161# ifdef FEAT_GUI
8162 /* When GUI is active, also move/paste when 'mouse' is empty */
8163 if (!gui.in_use)
8164# endif
8165 if (!mouse_has(MOUSE_INSERT))
8166 return;
8167
8168 undisplay_dollar();
8169 tpos = curwin->w_cursor;
8170 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
8171 {
8172 start_arrow(&tpos);
8173# ifdef FEAT_CINDENT
8174 can_cindent = TRUE;
8175# endif
8176 }
8177
8178#ifdef FEAT_WINDOWS
8179 /* redraw status lines (in case another window became active) */
8180 redraw_statuslines();
8181#endif
8182}
8183
8184 static void
8185ins_mousescroll(up)
8186 int up;
8187{
8188 pos_T tpos;
8189# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8190 win_T *old_curwin;
8191# endif
8192
8193 tpos = curwin->w_cursor;
8194
8195# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8196 old_curwin = curwin;
8197
8198 /* Currently the mouse coordinates are only known in the GUI. */
8199 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
8200 {
8201 int row, col;
8202
8203 row = mouse_row;
8204 col = mouse_col;
8205
8206 /* find the window at the pointer coordinates */
8207 curwin = mouse_find_win(&row, &col);
8208 curbuf = curwin->w_buffer;
8209 }
8210 if (curwin == old_curwin)
8211# endif
8212 undisplay_dollar();
8213
8214 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8215 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
8216 else
8217 scroll_redraw(up, 3L);
8218
8219# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8220 curwin->w_redr_status = TRUE;
8221
8222 curwin = old_curwin;
8223 curbuf = curwin->w_buffer;
8224# endif
8225
8226 if (!equalpos(curwin->w_cursor, tpos))
8227 {
8228 start_arrow(&tpos);
8229# ifdef FEAT_CINDENT
8230 can_cindent = TRUE;
8231# endif
8232 }
8233}
8234#endif
8235
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008236#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00008237 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008238ins_tabline(c)
8239 int c;
8240{
8241 /* We will be leaving the current window, unless closing another tab. */
8242 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
8243 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
8244 {
8245 undisplay_dollar();
8246 start_arrow(&curwin->w_cursor);
8247# ifdef FEAT_CINDENT
8248 can_cindent = TRUE;
8249# endif
8250 }
8251
8252 if (c == K_TABLINE)
8253 goto_tabpage(current_tab);
8254 else
8255 handle_tabmenu();
8256
8257}
8258#endif
8259
8260#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 void
8262ins_scroll()
8263{
8264 pos_T tpos;
8265
8266 undisplay_dollar();
8267 tpos = curwin->w_cursor;
8268 if (gui_do_scroll())
8269 {
8270 start_arrow(&tpos);
8271# ifdef FEAT_CINDENT
8272 can_cindent = TRUE;
8273# endif
8274 }
8275}
8276
8277 void
8278ins_horscroll()
8279{
8280 pos_T tpos;
8281
8282 undisplay_dollar();
8283 tpos = curwin->w_cursor;
8284 if (gui_do_horiz_scroll())
8285 {
8286 start_arrow(&tpos);
8287# ifdef FEAT_CINDENT
8288 can_cindent = TRUE;
8289# endif
8290 }
8291}
8292#endif
8293
8294 static void
8295ins_left()
8296{
8297 pos_T tpos;
8298
8299#ifdef FEAT_FOLDING
8300 if ((fdo_flags & FDO_HOR) && KeyTyped)
8301 foldOpenCursor();
8302#endif
8303 undisplay_dollar();
8304 tpos = curwin->w_cursor;
8305 if (oneleft() == OK)
8306 {
8307 start_arrow(&tpos);
8308#ifdef FEAT_RIGHTLEFT
8309 /* If exit reversed string, position is fixed */
8310 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
8311 revins_legal++;
8312 revins_chars++;
8313#endif
8314 }
8315
8316 /*
8317 * if 'whichwrap' set for cursor in insert mode may go to
8318 * previous line
8319 */
8320 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
8321 {
8322 start_arrow(&tpos);
8323 --(curwin->w_cursor.lnum);
8324 coladvance((colnr_T)MAXCOL);
8325 curwin->w_set_curswant = TRUE; /* so we stay at the end */
8326 }
8327 else
8328 vim_beep();
8329}
8330
8331 static void
8332ins_home(c)
8333 int c;
8334{
8335 pos_T tpos;
8336
8337#ifdef FEAT_FOLDING
8338 if ((fdo_flags & FDO_HOR) && KeyTyped)
8339 foldOpenCursor();
8340#endif
8341 undisplay_dollar();
8342 tpos = curwin->w_cursor;
8343 if (c == K_C_HOME)
8344 curwin->w_cursor.lnum = 1;
8345 curwin->w_cursor.col = 0;
8346#ifdef FEAT_VIRTUALEDIT
8347 curwin->w_cursor.coladd = 0;
8348#endif
8349 curwin->w_curswant = 0;
8350 start_arrow(&tpos);
8351}
8352
8353 static void
8354ins_end(c)
8355 int c;
8356{
8357 pos_T tpos;
8358
8359#ifdef FEAT_FOLDING
8360 if ((fdo_flags & FDO_HOR) && KeyTyped)
8361 foldOpenCursor();
8362#endif
8363 undisplay_dollar();
8364 tpos = curwin->w_cursor;
8365 if (c == K_C_END)
8366 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
8367 coladvance((colnr_T)MAXCOL);
8368 curwin->w_curswant = MAXCOL;
8369
8370 start_arrow(&tpos);
8371}
8372
8373 static void
8374ins_s_left()
8375{
8376#ifdef FEAT_FOLDING
8377 if ((fdo_flags & FDO_HOR) && KeyTyped)
8378 foldOpenCursor();
8379#endif
8380 undisplay_dollar();
8381 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
8382 {
8383 start_arrow(&curwin->w_cursor);
8384 (void)bck_word(1L, FALSE, FALSE);
8385 curwin->w_set_curswant = TRUE;
8386 }
8387 else
8388 vim_beep();
8389}
8390
8391 static void
8392ins_right()
8393{
8394#ifdef FEAT_FOLDING
8395 if ((fdo_flags & FDO_HOR) && KeyTyped)
8396 foldOpenCursor();
8397#endif
8398 undisplay_dollar();
8399 if (gchar_cursor() != NUL || virtual_active()
8400 )
8401 {
8402 start_arrow(&curwin->w_cursor);
8403 curwin->w_set_curswant = TRUE;
8404#ifdef FEAT_VIRTUALEDIT
8405 if (virtual_active())
8406 oneright();
8407 else
8408#endif
8409 {
8410#ifdef FEAT_MBYTE
8411 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008412 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 else
8414#endif
8415 ++curwin->w_cursor.col;
8416 }
8417
8418#ifdef FEAT_RIGHTLEFT
8419 revins_legal++;
8420 if (revins_chars)
8421 revins_chars--;
8422#endif
8423 }
8424 /* if 'whichwrap' set for cursor in insert mode, may move the
8425 * cursor to the next line */
8426 else if (vim_strchr(p_ww, ']') != NULL
8427 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
8428 {
8429 start_arrow(&curwin->w_cursor);
8430 curwin->w_set_curswant = TRUE;
8431 ++curwin->w_cursor.lnum;
8432 curwin->w_cursor.col = 0;
8433 }
8434 else
8435 vim_beep();
8436}
8437
8438 static void
8439ins_s_right()
8440{
8441#ifdef FEAT_FOLDING
8442 if ((fdo_flags & FDO_HOR) && KeyTyped)
8443 foldOpenCursor();
8444#endif
8445 undisplay_dollar();
8446 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
8447 || gchar_cursor() != NUL)
8448 {
8449 start_arrow(&curwin->w_cursor);
8450 (void)fwd_word(1L, FALSE, 0);
8451 curwin->w_set_curswant = TRUE;
8452 }
8453 else
8454 vim_beep();
8455}
8456
8457 static void
8458ins_up(startcol)
8459 int startcol; /* when TRUE move to Insstart.col */
8460{
8461 pos_T tpos;
8462 linenr_T old_topline = curwin->w_topline;
8463#ifdef FEAT_DIFF
8464 int old_topfill = curwin->w_topfill;
8465#endif
8466
8467 undisplay_dollar();
8468 tpos = curwin->w_cursor;
8469 if (cursor_up(1L, TRUE) == OK)
8470 {
8471 if (startcol)
8472 coladvance(getvcol_nolist(&Insstart));
8473 if (old_topline != curwin->w_topline
8474#ifdef FEAT_DIFF
8475 || old_topfill != curwin->w_topfill
8476#endif
8477 )
8478 redraw_later(VALID);
8479 start_arrow(&tpos);
8480#ifdef FEAT_CINDENT
8481 can_cindent = TRUE;
8482#endif
8483 }
8484 else
8485 vim_beep();
8486}
8487
8488 static void
8489ins_pageup()
8490{
8491 pos_T tpos;
8492
8493 undisplay_dollar();
8494 tpos = curwin->w_cursor;
8495 if (onepage(BACKWARD, 1L) == OK)
8496 {
8497 start_arrow(&tpos);
8498#ifdef FEAT_CINDENT
8499 can_cindent = TRUE;
8500#endif
8501 }
8502 else
8503 vim_beep();
8504}
8505
8506 static void
8507ins_down(startcol)
8508 int startcol; /* when TRUE move to Insstart.col */
8509{
8510 pos_T tpos;
8511 linenr_T old_topline = curwin->w_topline;
8512#ifdef FEAT_DIFF
8513 int old_topfill = curwin->w_topfill;
8514#endif
8515
8516 undisplay_dollar();
8517 tpos = curwin->w_cursor;
8518 if (cursor_down(1L, TRUE) == OK)
8519 {
8520 if (startcol)
8521 coladvance(getvcol_nolist(&Insstart));
8522 if (old_topline != curwin->w_topline
8523#ifdef FEAT_DIFF
8524 || old_topfill != curwin->w_topfill
8525#endif
8526 )
8527 redraw_later(VALID);
8528 start_arrow(&tpos);
8529#ifdef FEAT_CINDENT
8530 can_cindent = TRUE;
8531#endif
8532 }
8533 else
8534 vim_beep();
8535}
8536
8537 static void
8538ins_pagedown()
8539{
8540 pos_T tpos;
8541
8542 undisplay_dollar();
8543 tpos = curwin->w_cursor;
8544 if (onepage(FORWARD, 1L) == OK)
8545 {
8546 start_arrow(&tpos);
8547#ifdef FEAT_CINDENT
8548 can_cindent = TRUE;
8549#endif
8550 }
8551 else
8552 vim_beep();
8553}
8554
8555#ifdef FEAT_DND
8556 static void
8557ins_drop()
8558{
8559 do_put('~', BACKWARD, 1L, PUT_CURSEND);
8560}
8561#endif
8562
8563/*
8564 * Handle TAB in Insert or Replace mode.
8565 * Return TRUE when the TAB needs to be inserted like a normal character.
8566 */
8567 static int
8568ins_tab()
8569{
8570 int ind;
8571 int i;
8572 int temp;
8573
8574 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
8575 Insstart_blank_vcol = get_nolist_virtcol();
8576 if (echeck_abbr(TAB + ABBR_OFF))
8577 return FALSE;
8578
8579 ind = inindent(0);
8580#ifdef FEAT_CINDENT
8581 if (ind)
8582 can_cindent = FALSE;
8583#endif
8584
8585 /*
8586 * When nothing special, insert TAB like a normal character
8587 */
8588 if (!curbuf->b_p_et
8589 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
8590 && curbuf->b_p_sts == 0)
8591 return TRUE;
8592
8593 if (stop_arrow() == FAIL)
8594 return TRUE;
8595
8596 did_ai = FALSE;
8597#ifdef FEAT_SMARTINDENT
8598 did_si = FALSE;
8599 can_si = FALSE;
8600 can_si_back = FALSE;
8601#endif
8602 AppendToRedobuff((char_u *)"\t");
8603
8604 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
8605 temp = (int)curbuf->b_p_sw;
8606 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
8607 temp = (int)curbuf->b_p_sts;
8608 else /* otherwise use 'tabstop' */
8609 temp = (int)curbuf->b_p_ts;
8610 temp -= get_nolist_virtcol() % temp;
8611
8612 /*
8613 * Insert the first space with ins_char(). It will delete one char in
8614 * replace mode. Insert the rest with ins_str(); it will not delete any
8615 * chars. For VREPLACE mode, we use ins_char() for all characters.
8616 */
8617 ins_char(' ');
8618 while (--temp > 0)
8619 {
8620#ifdef FEAT_VREPLACE
8621 if (State & VREPLACE_FLAG)
8622 ins_char(' ');
8623 else
8624#endif
8625 {
8626 ins_str((char_u *)" ");
8627 if (State & REPLACE_FLAG) /* no char replaced */
8628 replace_push(NUL);
8629 }
8630 }
8631
8632 /*
8633 * When 'expandtab' not set: Replace spaces by TABs where possible.
8634 */
8635 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
8636 {
8637 char_u *ptr;
8638#ifdef FEAT_VREPLACE
8639 char_u *saved_line = NULL; /* init for GCC */
8640 pos_T pos;
8641#endif
8642 pos_T fpos;
8643 pos_T *cursor;
8644 colnr_T want_vcol, vcol;
8645 int change_col = -1;
8646 int save_list = curwin->w_p_list;
8647
8648 /*
8649 * Get the current line. For VREPLACE mode, don't make real changes
8650 * yet, just work on a copy of the line.
8651 */
8652#ifdef FEAT_VREPLACE
8653 if (State & VREPLACE_FLAG)
8654 {
8655 pos = curwin->w_cursor;
8656 cursor = &pos;
8657 saved_line = vim_strsave(ml_get_curline());
8658 if (saved_line == NULL)
8659 return FALSE;
8660 ptr = saved_line + pos.col;
8661 }
8662 else
8663#endif
8664 {
8665 ptr = ml_get_cursor();
8666 cursor = &curwin->w_cursor;
8667 }
8668
8669 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
8670 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8671 curwin->w_p_list = FALSE;
8672
8673 /* Find first white before the cursor */
8674 fpos = curwin->w_cursor;
8675 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
8676 {
8677 --fpos.col;
8678 --ptr;
8679 }
8680
8681 /* In Replace mode, don't change characters before the insert point. */
8682 if ((State & REPLACE_FLAG)
8683 && fpos.lnum == Insstart.lnum
8684 && fpos.col < Insstart.col)
8685 {
8686 ptr += Insstart.col - fpos.col;
8687 fpos.col = Insstart.col;
8688 }
8689
8690 /* compute virtual column numbers of first white and cursor */
8691 getvcol(curwin, &fpos, &vcol, NULL, NULL);
8692 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
8693
8694 /* Use as many TABs as possible. Beware of 'showbreak' and
8695 * 'linebreak' adding extra virtual columns. */
8696 while (vim_iswhite(*ptr))
8697 {
8698 i = lbr_chartabsize((char_u *)"\t", vcol);
8699 if (vcol + i > want_vcol)
8700 break;
8701 if (*ptr != TAB)
8702 {
8703 *ptr = TAB;
8704 if (change_col < 0)
8705 {
8706 change_col = fpos.col; /* Column of first change */
8707 /* May have to adjust Insstart */
8708 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
8709 Insstart.col = fpos.col;
8710 }
8711 }
8712 ++fpos.col;
8713 ++ptr;
8714 vcol += i;
8715 }
8716
8717 if (change_col >= 0)
8718 {
8719 int repl_off = 0;
8720
8721 /* Skip over the spaces we need. */
8722 while (vcol < want_vcol && *ptr == ' ')
8723 {
8724 vcol += lbr_chartabsize(ptr, vcol);
8725 ++ptr;
8726 ++repl_off;
8727 }
8728 if (vcol > want_vcol)
8729 {
8730 /* Must have a char with 'showbreak' just before it. */
8731 --ptr;
8732 --repl_off;
8733 }
8734 fpos.col += repl_off;
8735
8736 /* Delete following spaces. */
8737 i = cursor->col - fpos.col;
8738 if (i > 0)
8739 {
8740 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
8741 /* correct replace stack. */
8742 if ((State & REPLACE_FLAG)
8743#ifdef FEAT_VREPLACE
8744 && !(State & VREPLACE_FLAG)
8745#endif
8746 )
8747 for (temp = i; --temp >= 0; )
8748 replace_join(repl_off);
8749 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00008750#ifdef FEAT_NETBEANS_INTG
8751 if (usingNetbeans)
8752 {
8753 netbeans_removed(curbuf, fpos.lnum, cursor->col,
8754 (long)(i + 1));
8755 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
8756 (char_u *)"\t", 1);
8757 }
8758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759 cursor->col -= i;
8760
8761#ifdef FEAT_VREPLACE
8762 /*
8763 * In VREPLACE mode, we haven't changed anything yet. Do it now by
8764 * backspacing over the changed spacing and then inserting the new
8765 * spacing.
8766 */
8767 if (State & VREPLACE_FLAG)
8768 {
8769 /* Backspace from real cursor to change_col */
8770 backspace_until_column(change_col);
8771
8772 /* Insert each char in saved_line from changed_col to
8773 * ptr-cursor */
8774 ins_bytes_len(saved_line + change_col,
8775 cursor->col - change_col);
8776 }
8777#endif
8778 }
8779
8780#ifdef FEAT_VREPLACE
8781 if (State & VREPLACE_FLAG)
8782 vim_free(saved_line);
8783#endif
8784 curwin->w_p_list = save_list;
8785 }
8786
8787 return FALSE;
8788}
8789
8790/*
8791 * Handle CR or NL in insert mode.
8792 * Return TRUE when out of memory or can't undo.
8793 */
8794 static int
8795ins_eol(c)
8796 int c;
8797{
8798 int i;
8799
8800 if (echeck_abbr(c + ABBR_OFF))
8801 return FALSE;
8802 if (stop_arrow() == FAIL)
8803 return TRUE;
8804 undisplay_dollar();
8805
8806 /*
8807 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
8808 * character under the cursor. Only push a NUL on the replace stack,
8809 * nothing to put back when the NL is deleted.
8810 */
8811 if ((State & REPLACE_FLAG)
8812#ifdef FEAT_VREPLACE
8813 && !(State & VREPLACE_FLAG)
8814#endif
8815 )
8816 replace_push(NUL);
8817
8818 /*
8819 * In VREPLACE mode, a NL replaces the rest of the line, and starts
8820 * replacing the next line, so we push all of the characters left on the
8821 * line onto the replace stack. This is not done here though, it is done
8822 * in open_line().
8823 */
8824
8825#ifdef FEAT_RIGHTLEFT
8826# ifdef FEAT_FKMAP
8827 if (p_altkeymap && p_fkmap)
8828 fkmap(NL);
8829# endif
8830 /* NL in reverse insert will always start in the end of
8831 * current line. */
8832 if (revins_on)
8833 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8834#endif
8835
8836 AppendToRedobuff(NL_STR);
8837 i = open_line(FORWARD,
8838#ifdef FEAT_COMMENTS
8839 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
8840#endif
8841 0, old_indent);
8842 old_indent = 0;
8843#ifdef FEAT_CINDENT
8844 can_cindent = TRUE;
8845#endif
8846
8847 return (!i);
8848}
8849
8850#ifdef FEAT_DIGRAPHS
8851/*
8852 * Handle digraph in insert mode.
8853 * Returns character still to be inserted, or NUL when nothing remaining to be
8854 * done.
8855 */
8856 static int
8857ins_digraph()
8858{
8859 int c;
8860 int cc;
8861
8862 pc_status = PC_STATUS_UNSET;
8863 if (redrawing() && !char_avail())
8864 {
8865 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008866 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867
8868 edit_putchar('?', TRUE);
8869#ifdef FEAT_CMDL_INFO
8870 add_to_showcmd_c(Ctrl_K);
8871#endif
8872 }
8873
8874#ifdef USE_ON_FLY_SCROLL
8875 dont_scroll = TRUE; /* disallow scrolling here */
8876#endif
8877
8878 /* don't map the digraph chars. This also prevents the
8879 * mode message to be deleted when ESC is hit */
8880 ++no_mapping;
8881 ++allow_keys;
8882 c = safe_vgetc();
8883 --no_mapping;
8884 --allow_keys;
8885 if (IS_SPECIAL(c) || mod_mask) /* special key */
8886 {
8887#ifdef FEAT_CMDL_INFO
8888 clear_showcmd();
8889#endif
8890 insert_special(c, TRUE, FALSE);
8891 return NUL;
8892 }
8893 if (c != ESC)
8894 {
8895 if (redrawing() && !char_avail())
8896 {
8897 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008898 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899
8900 if (char2cells(c) == 1)
8901 {
8902 /* first remove the '?', otherwise it's restored when typing
8903 * an ESC next */
8904 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00008905 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 edit_putchar(c, TRUE);
8907 }
8908#ifdef FEAT_CMDL_INFO
8909 add_to_showcmd_c(c);
8910#endif
8911 }
8912 ++no_mapping;
8913 ++allow_keys;
8914 cc = safe_vgetc();
8915 --no_mapping;
8916 --allow_keys;
8917 if (cc != ESC)
8918 {
8919 AppendToRedobuff((char_u *)CTRL_V_STR);
8920 c = getdigraph(c, cc, TRUE);
8921#ifdef FEAT_CMDL_INFO
8922 clear_showcmd();
8923#endif
8924 return c;
8925 }
8926 }
8927 edit_unputchar();
8928#ifdef FEAT_CMDL_INFO
8929 clear_showcmd();
8930#endif
8931 return NUL;
8932}
8933#endif
8934
8935/*
8936 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
8937 * Returns the char to be inserted, or NUL if none found.
8938 */
8939 static int
8940ins_copychar(lnum)
8941 linenr_T lnum;
8942{
8943 int c;
8944 int temp;
8945 char_u *ptr, *prev_ptr;
8946
8947 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8948 {
8949 vim_beep();
8950 return NUL;
8951 }
8952
8953 /* try to advance to the cursor column */
8954 temp = 0;
8955 ptr = ml_get(lnum);
8956 prev_ptr = ptr;
8957 validate_virtcol();
8958 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
8959 {
8960 prev_ptr = ptr;
8961 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
8962 }
8963 if ((colnr_T)temp > curwin->w_virtcol)
8964 ptr = prev_ptr;
8965
8966#ifdef FEAT_MBYTE
8967 c = (*mb_ptr2char)(ptr);
8968#else
8969 c = *ptr;
8970#endif
8971 if (c == NUL)
8972 vim_beep();
8973 return c;
8974}
8975
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008976/*
8977 * CTRL-Y or CTRL-E typed in Insert mode.
8978 */
8979 static int
8980ins_ctrl_ey(tc)
8981 int tc;
8982{
8983 int c = tc;
8984
8985#ifdef FEAT_INS_EXPAND
8986 if (ctrl_x_mode == CTRL_X_SCROLL)
8987 {
8988 if (c == Ctrl_Y)
8989 scrolldown_clamp();
8990 else
8991 scrollup_clamp();
8992 redraw_later(VALID);
8993 }
8994 else
8995#endif
8996 {
8997 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
8998 if (c != NUL)
8999 {
9000 long tw_save;
9001
9002 /* The character must be taken literally, insert like it
9003 * was typed after a CTRL-V, and pretend 'textwidth'
9004 * wasn't set. Digits, 'o' and 'x' are special after a
9005 * CTRL-V, don't use it for these. */
9006 if (c < 256 && !isalnum(c))
9007 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
9008 tw_save = curbuf->b_p_tw;
9009 curbuf->b_p_tw = -1;
9010 insert_special(c, TRUE, FALSE);
9011 curbuf->b_p_tw = tw_save;
9012#ifdef FEAT_RIGHTLEFT
9013 revins_chars++;
9014 revins_legal++;
9015#endif
9016 c = Ctrl_V; /* pretend CTRL-V is last character */
9017 auto_format(FALSE, TRUE);
9018 }
9019 }
9020 return c;
9021}
9022
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023#ifdef FEAT_SMARTINDENT
9024/*
9025 * Try to do some very smart auto-indenting.
9026 * Used when inserting a "normal" character.
9027 */
9028 static void
9029ins_try_si(c)
9030 int c;
9031{
9032 pos_T *pos, old_pos;
9033 char_u *ptr;
9034 int i;
9035 int temp;
9036
9037 /*
9038 * do some very smart indenting when entering '{' or '}'
9039 */
9040 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
9041 {
9042 /*
9043 * for '}' set indent equal to indent of line containing matching '{'
9044 */
9045 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
9046 {
9047 old_pos = curwin->w_cursor;
9048 /*
9049 * If the matching '{' has a ')' immediately before it (ignoring
9050 * white-space), then line up with the start of the line
9051 * containing the matching '(' if there is one. This handles the
9052 * case where an "if (..\n..) {" statement continues over multiple
9053 * lines -- webb
9054 */
9055 ptr = ml_get(pos->lnum);
9056 i = pos->col;
9057 if (i > 0) /* skip blanks before '{' */
9058 while (--i > 0 && vim_iswhite(ptr[i]))
9059 ;
9060 curwin->w_cursor.lnum = pos->lnum;
9061 curwin->w_cursor.col = i;
9062 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
9063 curwin->w_cursor = *pos;
9064 i = get_indent();
9065 curwin->w_cursor = old_pos;
9066#ifdef FEAT_VREPLACE
9067 if (State & VREPLACE_FLAG)
9068 change_indent(INDENT_SET, i, FALSE, NUL);
9069 else
9070#endif
9071 (void)set_indent(i, SIN_CHANGED);
9072 }
9073 else if (curwin->w_cursor.col > 0)
9074 {
9075 /*
9076 * when inserting '{' after "O" reduce indent, but not
9077 * more than indent of previous line
9078 */
9079 temp = TRUE;
9080 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
9081 {
9082 old_pos = curwin->w_cursor;
9083 i = get_indent();
9084 while (curwin->w_cursor.lnum > 1)
9085 {
9086 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
9087
9088 /* ignore empty lines and lines starting with '#'. */
9089 if (*ptr != '#' && *ptr != NUL)
9090 break;
9091 }
9092 if (get_indent() >= i)
9093 temp = FALSE;
9094 curwin->w_cursor = old_pos;
9095 }
9096 if (temp)
9097 shift_line(TRUE, FALSE, 1);
9098 }
9099 }
9100
9101 /*
9102 * set indent of '#' always to 0
9103 */
9104 if (curwin->w_cursor.col > 0 && can_si && c == '#')
9105 {
9106 /* remember current indent for next line */
9107 old_indent = get_indent();
9108 (void)set_indent(0, SIN_CHANGED);
9109 }
9110
9111 /* Adjust ai_col, the char at this position can be deleted. */
9112 if (ai_col > curwin->w_cursor.col)
9113 ai_col = curwin->w_cursor.col;
9114}
9115#endif
9116
9117/*
9118 * Get the value that w_virtcol would have when 'list' is off.
9119 * Unless 'cpo' contains the 'L' flag.
9120 */
9121 static colnr_T
9122get_nolist_virtcol()
9123{
9124 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9125 return getvcol_nolist(&curwin->w_cursor);
9126 validate_virtcol();
9127 return curwin->w_virtcol;
9128}