blob: 8fd9ed451f9cfeb86da775710f467150563d4730 [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
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002143 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002144 && 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 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002504 /* If the current match is the original text don't find the first
2505 * match after it, don't highlight anything. */
2506 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2507 shown_match_ok = TRUE;
2508
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002509 i = 0;
2510 compl = compl_first_match;
2511 do
2512 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002513 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2514 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002515 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002516 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002517 if (!shown_match_ok)
2518 {
2519 if (compl == compl_shown_match || did_find_shown_match)
2520 {
2521 /* This item is the shown match or this is the
2522 * first displayed item after the shown match. */
2523 compl_shown_match = compl;
2524 did_find_shown_match = TRUE;
2525 shown_match_ok = TRUE;
2526 }
2527 else
2528 /* Remember this displayed match for when the
2529 * shown match is just below it. */
2530 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002531 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002532 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002533
2534 if (compl->cp_text[CPT_ABBR] != NULL)
2535 compl_match_array[i].pum_text =
2536 compl->cp_text[CPT_ABBR];
2537 else
2538 compl_match_array[i].pum_text = compl->cp_str;
2539 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2540 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2541 if (compl->cp_text[CPT_MENU] != NULL)
2542 compl_match_array[i++].pum_extra =
2543 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002544 else
2545 compl_match_array[i++].pum_extra = compl->cp_fname;
2546 }
2547
2548 if (compl == compl_shown_match)
2549 {
2550 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002551
2552 /* When the original text is the shown match don't set
2553 * compl_shown_match. */
2554 if (compl->cp_flags & ORIGINAL_TEXT)
2555 shown_match_ok = TRUE;
2556
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002557 if (!shown_match_ok && shown_compl != NULL)
2558 {
2559 /* The shown match isn't displayed, set it to the
2560 * previously displayed match. */
2561 compl_shown_match = shown_compl;
2562 shown_match_ok = TRUE;
2563 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002564 }
2565 compl = compl->cp_next;
2566 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002567
2568 if (!shown_match_ok) /* no displayed match at all */
2569 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002570 }
2571 }
2572 else
2573 {
2574 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002575 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00002576 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
2577 || compl_match_array[i].pum_text
2578 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002579 {
2580 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00002581 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002582 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002583 }
2584
2585 if (compl_match_array != NULL)
2586 {
2587 /* Compute the screen column of the start of the completed text.
2588 * Use the cursor to get all wrapping and other settings right. */
2589 col = curwin->w_cursor.col;
2590 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00002591 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002592 curwin->w_cursor.col = col;
2593 }
2594}
2595
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596#define DICT_FIRST (1) /* use just first element in "dict" */
2597#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002598
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00002600 * Add any identifiers that match the given pattern in the list of dictionary
2601 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 */
2603 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00002604ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
2605 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002607 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002608 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002610 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 char_u *ptr;
2612 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 char_u **files;
2615 int count;
2616 int i;
2617 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002618 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619
Bram Moolenaar0b238792006-03-02 22:49:12 +00002620 if (*dict == NUL)
2621 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002622#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002623 /* When 'dictionary' is empty and spell checking is enabled use
2624 * "spell". */
2625 if (!thesaurus && curwin->w_p_spell)
2626 dict = (char_u *)"spell";
2627 else
2628#endif
2629 return;
2630 }
2631
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002633 if (buf == NULL)
2634 return;
2635
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 /* If 'infercase' is set, don't use 'smartcase' here */
2637 save_p_scs = p_scs;
2638 if (curbuf->b_p_inf)
2639 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002640
2641 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
2642 * to only match at the start of a line. Otherwise just match the
2643 * pattern. */
2644 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2645 {
2646 i = STRLEN(pat) + 8;
2647 ptr = alloc(i);
2648 if (ptr == NULL)
2649 return;
2650 vim_snprintf((char *)ptr, i, "^\\s*\\zs%s", pat);
2651 regmatch.regprog = vim_regcomp(ptr, p_magic ? RE_MAGIC : 0);
2652 vim_free(ptr);
2653 }
2654 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00002655 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002656 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002657 if (regmatch.regprog == NULL)
2658 goto theend;
2659 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002660
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2662 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002663 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 {
2665 /* copy one dictionary file name into buf */
2666 if (flags == DICT_EXACT)
2667 {
2668 count = 1;
2669 files = &dict;
2670 }
2671 else
2672 {
2673 /* Expand wildcards in the dictionary name, but do not allow
2674 * backticks (for security, the 'dict' option may have been set in
2675 * a modeline). */
2676 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002677# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002678 if (!thesaurus && STRCMP(buf, "spell") == 0)
2679 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002680 else
2681# endif
2682 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 || expand_wildcards(1, &buf, &count, &files,
2684 EW_FILE|EW_SILENT) != OK)
2685 count = 0;
2686 }
2687
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002688# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002689 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00002691 /* Complete from active spelling. Skip "\<" in the pattern, we
2692 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002693 if (pat[0] == '\\' && pat[1] == '<')
2694 ptr = pat + 2;
2695 else
2696 ptr = pat;
2697 spell_dump_compl(curbuf, ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002699 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002700# endif
Bram Moolenaar0b238792006-03-02 22:49:12 +00002701 {
2702 ins_compl_files(count, files, thesaurus, flags,
2703 &regmatch, buf, &dir);
2704 if (flags != DICT_EXACT)
2705 FreeWild(count, files);
2706 }
2707 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 break;
2709 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002710
2711theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 p_scs = save_p_scs;
2713 vim_free(regmatch.regprog);
2714 vim_free(buf);
2715}
2716
Bram Moolenaar0b238792006-03-02 22:49:12 +00002717 static void
2718ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
2719 int count;
2720 char_u **files;
2721 int thesaurus;
2722 int flags;
2723 regmatch_T *regmatch;
2724 char_u *buf;
2725 int *dir;
2726{
2727 char_u *ptr;
2728 int i;
2729 FILE *fp;
2730 int add_r;
2731
2732 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
2733 {
2734 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2735 if (flags != DICT_EXACT)
2736 {
2737 vim_snprintf((char *)IObuff, IOSIZE,
2738 _("Scanning dictionary: %s"), (char *)files[i]);
2739 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2740 }
2741
2742 if (fp != NULL)
2743 {
2744 /*
2745 * Read dictionary file line by line.
2746 * Check each line for a match.
2747 */
2748 while (!got_int && !compl_interrupted
2749 && !vim_fgets(buf, LSIZE, fp))
2750 {
2751 ptr = buf;
2752 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
2753 {
2754 ptr = regmatch->startp[0];
2755 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2756 ptr = find_line_end(ptr);
2757 else
2758 ptr = find_word_end(ptr);
2759 add_r = ins_compl_add_infercase(regmatch->startp[0],
2760 (int)(ptr - regmatch->startp[0]),
Bram Moolenaard289f132006-03-11 21:30:53 +00002761 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002762 if (thesaurus)
2763 {
2764 char_u *wstart;
2765
2766 /*
2767 * Add the other matches on the line
2768 */
2769 while (!got_int)
2770 {
2771 /* Find start of the next word. Skip white
2772 * space and punctuation. */
2773 ptr = find_word_start(ptr);
2774 if (*ptr == NUL || *ptr == NL)
2775 break;
2776 wstart = ptr;
2777
2778 /* Find end of the word and add it. */
2779#ifdef FEAT_MBYTE
2780 if (has_mbyte)
2781 /* Japanese words may have characters in
2782 * different classes, only separate words
2783 * with single-byte non-word characters. */
2784 while (*ptr != NUL)
2785 {
2786 int l = (*mb_ptr2len)(ptr);
2787
2788 if (l < 2 && !vim_iswordc(*ptr))
2789 break;
2790 ptr += l;
2791 }
2792 else
2793#endif
2794 ptr = find_word_end(ptr);
2795 add_r = ins_compl_add_infercase(wstart,
2796 (int)(ptr - wstart),
2797 p_ic, files[i], *dir, 0);
2798 }
2799 }
2800 if (add_r == OK)
2801 /* if dir was BACKWARD then honor it just once */
2802 *dir = FORWARD;
2803 else if (add_r == FAIL)
2804 break;
2805 /* avoid expensive call to vim_regexec() when at end
2806 * of line */
2807 if (*ptr == '\n' || got_int)
2808 break;
2809 }
2810 line_breakcheck();
2811 ins_compl_check_keys(50);
2812 }
2813 fclose(fp);
2814 }
2815 }
2816}
2817
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818/*
2819 * Find the start of the next word.
2820 * Returns a pointer to the first char of the word. Also stops at a NUL.
2821 */
2822 char_u *
2823find_word_start(ptr)
2824 char_u *ptr;
2825{
2826#ifdef FEAT_MBYTE
2827 if (has_mbyte)
2828 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002829 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 else
2831#endif
2832 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
2833 ++ptr;
2834 return ptr;
2835}
2836
2837/*
2838 * Find the end of the word. Assumes it starts inside a word.
2839 * Returns a pointer to just after the word.
2840 */
2841 char_u *
2842find_word_end(ptr)
2843 char_u *ptr;
2844{
2845#ifdef FEAT_MBYTE
2846 int start_class;
2847
2848 if (has_mbyte)
2849 {
2850 start_class = mb_get_class(ptr);
2851 if (start_class > 1)
2852 while (*ptr != NUL)
2853 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002854 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 if (mb_get_class(ptr) != start_class)
2856 break;
2857 }
2858 }
2859 else
2860#endif
2861 while (vim_iswordc(*ptr))
2862 ++ptr;
2863 return ptr;
2864}
2865
2866/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002867 * Find the end of the line, omitting CR and NL at the end.
2868 * Returns a pointer to just after the line.
2869 */
2870 static char_u *
2871find_line_end(ptr)
2872 char_u *ptr;
2873{
2874 char_u *s;
2875
2876 s = ptr + STRLEN(ptr);
2877 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
2878 --s;
2879 return s;
2880}
2881
2882/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 * Free the list of completions
2884 */
2885 static void
2886ins_compl_free()
2887{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002888 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002889 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002891 vim_free(compl_pattern);
2892 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002893 vim_free(compl_leader);
2894 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002896 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002898
2899 ins_compl_del_pum();
2900 pum_clear();
2901
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002902 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 do
2904 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002905 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002906 compl_curr_match = compl_curr_match->cp_next;
2907 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002909 if (match->cp_flags & FREE_FNAME)
2910 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00002911 for (i = 0; i < CPT_COUNT; ++i)
2912 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002914 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
2915 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916}
2917
2918 static void
2919ins_compl_clear()
2920{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002921 compl_cont_status = 0;
2922 compl_started = FALSE;
2923 compl_matches = 0;
2924 vim_free(compl_pattern);
2925 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002926 vim_free(compl_leader);
2927 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002929 vim_free(compl_orig_text);
2930 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002931 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932}
2933
2934/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002935 * Return TRUE when Insert completion is active.
2936 */
2937 int
2938ins_compl_active()
2939{
2940 return compl_started;
2941}
2942
2943/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002944 * Delete one character before the cursor and show the subset of the matches
2945 * that match the word that is now before the cursor.
Bram Moolenaara6557602006-02-04 22:43:20 +00002946 * Returns TRUE if the work is done and another char to be got from the user.
2947 */
2948 static int
2949ins_compl_bs()
2950{
2951 char_u *line;
2952 char_u *p;
2953
2954 if (curwin->w_cursor.col <= compl_col + compl_length)
2955 {
2956 /* Deleted more than what was used to find matches, need to look for
2957 * matches all over again. */
2958 ins_compl_free();
2959 compl_started = FALSE;
2960 compl_matches = 0;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002961 compl_cont_status = 0;
2962 compl_cont_mode = 0;
Bram Moolenaara6557602006-02-04 22:43:20 +00002963 }
2964
2965 line = ml_get_curline();
2966 p = line + curwin->w_cursor.col;
2967 mb_ptr_back(line, p);
2968
2969 vim_free(compl_leader);
2970 compl_leader = vim_strnsave(line + compl_col, (p - line) - compl_col);
2971 if (compl_leader != NULL)
2972 {
2973 ins_compl_del_pum();
2974 ins_compl_delete();
2975 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2976
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002977 if (compl_started)
2978 ins_compl_set_original_text(compl_leader);
2979 else
Bram Moolenaara6557602006-02-04 22:43:20 +00002980 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00002981#ifdef FEAT_SPELL
2982 spell_bad_len = 0; /* need to redetect bad word */
2983#endif
Bram Moolenaara6557602006-02-04 22:43:20 +00002984 /* Matches were cleared, need to search for them now. */
2985 if (ins_complete(Ctrl_N) == FAIL)
2986 compl_cont_status = 0;
2987 else
2988 {
2989 /* Remove the completed word again. */
2990 ins_compl_delete();
2991 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2992 }
2993 }
2994
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002995 /* Go to the original text, since none of the matches is inserted. */
2996 if (compl_first_match->cp_prev != NULL
2997 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
2998 compl_shown_match = compl_first_match->cp_prev;
2999 else
3000 compl_shown_match = compl_first_match;
3001 compl_curr_match = compl_shown_match;
3002 compl_shows_dir = compl_direction;
3003
Bram Moolenaara6557602006-02-04 22:43:20 +00003004 /* Show the popup menu with a different set of matches. */
3005 ins_compl_show_pum();
3006 compl_used_match = FALSE;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003007 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003008
3009 return TRUE;
3010 }
3011 return FALSE;
3012}
3013
3014/*
3015 * Append one character to the match leader. May reduce the number of
3016 * matches.
3017 */
3018 static void
3019ins_compl_addleader(c)
3020 int c;
3021{
3022#ifdef FEAT_MBYTE
3023 int cc;
3024
3025 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3026 {
3027 char_u buf[MB_MAXBYTES + 1];
3028
3029 (*mb_char2bytes)(c, buf);
3030 buf[cc] = NUL;
3031 ins_char_bytes(buf, cc);
3032 }
3033 else
3034#endif
3035 ins_char(c);
3036
3037 vim_free(compl_leader);
3038 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
3039 curwin->w_cursor.col - compl_col);
3040 if (compl_leader != NULL)
3041 {
3042 /* Show the popup menu with a different set of matches. */
3043 ins_compl_del_pum();
3044 ins_compl_show_pum();
3045 compl_used_match = FALSE;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003046 compl_enter_selects = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003047 ins_compl_set_original_text(compl_leader);
3048 }
3049}
3050
3051/*
3052 * Set the first match, the original text.
3053 */
3054 static void
3055ins_compl_set_original_text(str)
3056 char_u *str;
3057{
3058 char_u *p;
3059
3060 /* Replace the original text entry. */
3061 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3062 {
3063 p = vim_strsave(str);
3064 if (p != NULL)
3065 {
3066 vim_free(compl_first_match->cp_str);
3067 compl_first_match->cp_str = p;
3068 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003069 }
3070}
3071
3072/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003073 * Append one character to the match leader. May reduce the number of
3074 * matches.
3075 */
3076 static void
3077ins_compl_addfrommatch()
3078{
3079 char_u *p;
3080 int len = curwin->w_cursor.col - compl_col;
3081 int c;
3082
3083 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003084 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003085 return;
3086 p += len;
3087#ifdef FEAT_MBYTE
3088 c = mb_ptr2char(p);
3089#else
3090 c = *p;
3091#endif
3092 ins_compl_addleader(c);
3093}
3094
3095/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003097 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003098 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003100 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101ins_compl_prep(c)
3102 int c;
3103{
3104 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 int temp;
3106 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003107 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108
3109 /* Forget any previous 'special' messages if this is actually
3110 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3111 */
3112 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3113 edit_submode_extra = NULL;
3114
3115 /* Ignore end of Select mode mapping */
3116 if (c == K_SELECT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003117 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003119 /* Set "compl_get_longest" when finding the first matches. */
3120 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3121 || (ctrl_x_mode == 0 && !compl_started))
3122 {
3123 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3124 compl_used_match = TRUE;
3125 }
3126
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3128 {
3129 /*
3130 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3131 * it will be yet. Now we decide.
3132 */
3133 switch (c)
3134 {
3135 case Ctrl_E:
3136 case Ctrl_Y:
3137 ctrl_x_mode = CTRL_X_SCROLL;
3138 if (!(State & REPLACE_FLAG))
3139 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3140 else
3141 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3142 edit_submode_pre = NULL;
3143 showmode();
3144 break;
3145 case Ctrl_L:
3146 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3147 break;
3148 case Ctrl_F:
3149 ctrl_x_mode = CTRL_X_FILES;
3150 break;
3151 case Ctrl_K:
3152 ctrl_x_mode = CTRL_X_DICTIONARY;
3153 break;
3154 case Ctrl_R:
3155 /* Simply allow ^R to happen without affecting ^X mode */
3156 break;
3157 case Ctrl_T:
3158 ctrl_x_mode = CTRL_X_THESAURUS;
3159 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003160#ifdef FEAT_COMPL_FUNC
3161 case Ctrl_U:
3162 ctrl_x_mode = CTRL_X_FUNCTION;
3163 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003164 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003165 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003166 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003167#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003168 case 's':
3169 case Ctrl_S:
3170 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003171#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003172 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003173 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003174 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003175#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003176 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 case Ctrl_RSB:
3178 ctrl_x_mode = CTRL_X_TAGS;
3179 break;
3180#ifdef FEAT_FIND_ID
3181 case Ctrl_I:
3182 case K_S_TAB:
3183 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3184 break;
3185 case Ctrl_D:
3186 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3187 break;
3188#endif
3189 case Ctrl_V:
3190 case Ctrl_Q:
3191 ctrl_x_mode = CTRL_X_CMDLINE;
3192 break;
3193 case Ctrl_P:
3194 case Ctrl_N:
3195 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3196 * just started ^X mode, or there were enough ^X's to cancel
3197 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3198 * do normal expansion when interrupting a different mode (say
3199 * ^X^F^X^P or ^P^X^X^P, see below)
3200 * nothing changes if interrupting mode 0, (eg, the flag
3201 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003202 if (!(compl_cont_status & CONT_INTRPT))
3203 compl_cont_status |= CONT_LOCAL;
3204 else if (compl_cont_mode != 0)
3205 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 /* FALLTHROUGH */
3207 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003208 /* If we have typed at least 2 ^X's... for modes != 0, we set
3209 * compl_cont_status = 0 (eg, as if we had just started ^X
3210 * mode).
3211 * For mode 0, we set "compl_cont_mode" to an impossible
3212 * value, in both cases ^X^X can be used to restart the same
3213 * mode (avoiding ADDING mode).
3214 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3215 * 'complete' and local ^P expansions respectively.
3216 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3217 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 if (c == Ctrl_X)
3219 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003220 if (compl_cont_mode != 0)
3221 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003223 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 }
3225 ctrl_x_mode = 0;
3226 edit_submode = NULL;
3227 showmode();
3228 break;
3229 }
3230 }
3231 else if (ctrl_x_mode != 0)
3232 {
3233 /* We're already in CTRL-X mode, do we stay in it? */
3234 if (!vim_is_ctrl_x_key(c))
3235 {
3236 if (ctrl_x_mode == CTRL_X_SCROLL)
3237 ctrl_x_mode = 0;
3238 else
3239 ctrl_x_mode = CTRL_X_FINISHED;
3240 edit_submode = NULL;
3241 }
3242 showmode();
3243 }
3244
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003245 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 {
3247 /* Show error message from attempted keyword completion (probably
3248 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003249 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003251 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3252 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 || ctrl_x_mode == CTRL_X_FINISHED)
3254 {
3255 /* Get here when we have finished typing a sequence of ^N and
3256 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003257 * memory that was used, and make sure we can redo the insert. */
3258 if (compl_curr_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003260 char_u *p;
3261
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 /*
3263 * If any of the original typed text has been changed,
3264 * eg when ignorecase is set, we must add back-spaces to
3265 * the redo buffer. We add as few as necessary to delete
3266 * just the part of the original text that has changed.
3267 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003268 ptr = compl_curr_match->cp_str;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003269 p = compl_orig_text;
3270 while (*p && *p == *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003272 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 ++ptr;
3274 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003275 for (temp = 0; p[temp]; ++temp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 AppendCharToRedobuff(K_BS);
Bram Moolenaarebefac62005-12-28 22:39:57 +00003277 AppendToRedobuffLit(ptr, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 }
3279
3280#ifdef FEAT_CINDENT
3281 want_cindent = (can_cindent && cindent_on());
3282#endif
3283 /*
3284 * When completing whole lines: fix indent for 'cindent'.
3285 * Otherwise, break line if it's too long.
3286 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003287 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 {
3289#ifdef FEAT_CINDENT
3290 /* re-indent the current line */
3291 if (want_cindent)
3292 {
3293 do_c_expr_indent();
3294 want_cindent = FALSE; /* don't do it again */
3295 }
3296#endif
3297 }
3298 else
3299 {
3300 /* put the cursor on the last char, for 'tw' formatting */
3301 curwin->w_cursor.col--;
3302 if (stop_arrow() == OK)
3303 insertchar(NUL, 0, -1);
3304 curwin->w_cursor.col++;
3305 }
3306
3307 auto_format(FALSE, TRUE);
3308
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003309 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003310 * the selection without inserting anything. When
3311 * compl_enter_selects is set the Enter key does the same. */
3312 if ((c == Ctrl_Y || (compl_enter_selects
3313 && (c == CAR || c == K_KENTER || c == NL)))
3314 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003315 retval = TRUE;
3316
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003317 /* CTRL-E means completion is Ended, go back to the typed text. */
3318 if (c == Ctrl_E)
3319 {
3320 ins_compl_delete();
3321 if (compl_leader != NULL)
3322 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3323 else if (compl_first_match != NULL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003324 ins_bytes(compl_orig_text
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003325 + curwin->w_cursor.col - compl_col);
3326 retval = TRUE;
3327 }
3328
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003330 compl_started = FALSE;
3331 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 msg_clr_cmdline(); /* necessary for "noshowmode" */
3333 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003334 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 if (edit_submode != NULL)
3336 {
3337 edit_submode = NULL;
3338 showmode();
3339 }
3340
3341#ifdef FEAT_CINDENT
3342 /*
3343 * Indent now if a key was typed that is in 'cinkeys'.
3344 */
3345 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3346 do_c_expr_indent();
3347#endif
3348 }
3349 }
3350
3351 /* reset continue_* if we left expansion-mode, if we stay they'll be
3352 * (re)set properly in ins_complete() */
3353 if (!vim_is_ctrl_x_key(c))
3354 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003355 compl_cont_status = 0;
3356 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003358
3359 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360}
3361
3362/*
3363 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3364 * (depending on flag) starting from buf and looking for a non-scanned
3365 * buffer (other than curbuf). curbuf is special, if it is called with
3366 * buf=curbuf then it has to be the first call for a given flag/expansion.
3367 *
3368 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3369 */
3370 static buf_T *
3371ins_compl_next_buf(buf, flag)
3372 buf_T *buf;
3373 int flag;
3374{
3375#ifdef FEAT_WINDOWS
3376 static win_T *wp;
3377#endif
3378
3379 if (flag == 'w') /* just windows */
3380 {
3381#ifdef FEAT_WINDOWS
3382 if (buf == curbuf) /* first call for this flag/expansion */
3383 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003384 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 && wp->w_buffer->b_scanned)
3386 ;
3387 buf = wp->w_buffer;
3388#else
3389 buf = curbuf;
3390#endif
3391 }
3392 else
3393 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3394 * (unlisted buffers)
3395 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003396 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 && ((flag == 'U'
3398 ? buf->b_p_bl
3399 : (!buf->b_p_bl
3400 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003401 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 ;
3403 return buf;
3404}
3405
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003406#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003407static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003408
3409/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003410 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003411 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003412 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003413 static void
3414expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003415 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003416 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003417{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003418 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003419 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003420 char_u *funcname;
3421 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003422
Bram Moolenaare344bea2005-09-01 20:46:49 +00003423 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3424 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003425 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003426
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003427 /* Call 'completefunc' to obtain the list of matches. */
3428 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003429 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003430
Bram Moolenaare344bea2005-09-01 20:46:49 +00003431 pos = curwin->w_cursor;
3432 matchlist = call_func_retlist(funcname, 2, args, FALSE);
3433 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003434 if (matchlist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003435 return;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003436
Bram Moolenaara94bc432006-03-10 21:42:59 +00003437 ins_compl_add_list(matchlist);
3438 list_unref(matchlist);
3439}
3440#endif /* FEAT_COMPL_FUNC */
3441
Bram Moolenaar39f05632006-03-19 22:15:26 +00003442#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00003443/*
3444 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00003445 */
3446 static void
3447ins_compl_add_list(list)
3448 list_T *list;
3449{
3450 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003451 int dir = compl_direction;
3452
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003453 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003454 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003455 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00003456 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
3457 /* if dir was BACKWARD then honor it just once */
3458 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00003459 else if (did_emsg)
3460 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003461 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003462}
Bram Moolenaar39f05632006-03-19 22:15:26 +00003463
3464/*
3465 * Add a match to the list of matches from a typeval_T.
3466 * If the given string is already in the list of completions, then return
3467 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
3468 * maybe because alloc() returns NULL, then FAIL is returned.
3469 */
3470 int
3471ins_compl_add_tv(tv, dir)
3472 typval_T *tv;
3473 int dir;
3474{
3475 char_u *word;
3476 int icase = p_ic;
3477 char_u *(cptext[CPT_COUNT]);
3478
3479 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3480 {
3481 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
3482 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
3483 (char_u *)"abbr", FALSE);
3484 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
3485 (char_u *)"menu", FALSE);
3486 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
3487 (char_u *)"kind", FALSE);
3488 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
3489 (char_u *)"info", FALSE);
3490 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
3491 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
3492 }
3493 else
3494 {
3495 word = get_tv_string_chk(tv);
3496 vim_memset(cptext, 0, sizeof(cptext));
3497 }
3498 if (word == NULL || *word == NUL)
3499 return FAIL;
3500 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0);
3501}
Bram Moolenaara94bc432006-03-10 21:42:59 +00003502#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003503
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003504/*
3505 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003506 * The search starts at position "ini" in curbuf and in the direction
3507 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003508 * When "compl_started" is FALSE start at that position, otherwise continue
3509 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003510 * This may return before finding all the matches.
3511 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 */
3513 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003514ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516{
3517 static pos_T first_match_pos;
3518 static pos_T last_match_pos;
3519 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003520 static int found_all = FALSE; /* Found all matches of a
3521 certain type. */
3522 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523
Bram Moolenaar572cb562005-08-05 21:35:02 +00003524 pos_T *pos;
3525 char_u **matches;
3526 int save_p_scs;
3527 int save_p_ws;
3528 int save_p_ic;
3529 int i;
3530 int num_matches;
3531 int len;
3532 int found_new_match;
3533 int type = ctrl_x_mode;
3534 char_u *ptr;
3535 char_u *dict = NULL;
3536 int dict_f = 0;
3537 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003539 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 {
3541 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3542 ins_buf->b_scanned = 0;
3543 found_all = FALSE;
3544 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003545 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003546 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 last_match_pos = first_match_pos = *ini;
3548 }
3549
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003550 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003551 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3553 for (;;)
3554 {
3555 found_new_match = FAIL;
3556
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003557 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 * or if found_all says this entry is done. For ^X^L only use the
3559 * entries from 'complete' that look in loaded buffers. */
3560 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003561 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 {
3563 found_all = FALSE;
3564 while (*e_cpt == ',' || *e_cpt == ' ')
3565 e_cpt++;
3566 if (*e_cpt == '.' && !curbuf->b_scanned)
3567 {
3568 ins_buf = curbuf;
3569 first_match_pos = *ini;
3570 /* So that ^N can match word immediately after cursor */
3571 if (ctrl_x_mode == 0)
3572 dec(&first_match_pos);
3573 last_match_pos = first_match_pos;
3574 type = 0;
3575 }
3576 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
3577 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
3578 {
3579 /* Scan a buffer, but not the current one. */
3580 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
3581 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003582 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 first_match_pos.col = last_match_pos.col = 0;
3584 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
3585 last_match_pos.lnum = 0;
3586 type = 0;
3587 }
3588 else /* unloaded buffer, scan like dictionary */
3589 {
3590 found_all = TRUE;
3591 if (ins_buf->b_fname == NULL)
3592 continue;
3593 type = CTRL_X_DICTIONARY;
3594 dict = ins_buf->b_fname;
3595 dict_f = DICT_EXACT;
3596 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00003597 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 ins_buf->b_fname == NULL
3599 ? buf_spname(ins_buf)
3600 : ins_buf->b_sfname == NULL
3601 ? (char *)ins_buf->b_fname
3602 : (char *)ins_buf->b_sfname);
3603 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3604 }
3605 else if (*e_cpt == NUL)
3606 break;
3607 else
3608 {
3609 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3610 type = -1;
3611 else if (*e_cpt == 'k' || *e_cpt == 's')
3612 {
3613 if (*e_cpt == 'k')
3614 type = CTRL_X_DICTIONARY;
3615 else
3616 type = CTRL_X_THESAURUS;
3617 if (*++e_cpt != ',' && *e_cpt != NUL)
3618 {
3619 dict = e_cpt;
3620 dict_f = DICT_FIRST;
3621 }
3622 }
3623#ifdef FEAT_FIND_ID
3624 else if (*e_cpt == 'i')
3625 type = CTRL_X_PATH_PATTERNS;
3626 else if (*e_cpt == 'd')
3627 type = CTRL_X_PATH_DEFINES;
3628#endif
3629 else if (*e_cpt == ']' || *e_cpt == 't')
3630 {
3631 type = CTRL_X_TAGS;
3632 sprintf((char*)IObuff, _("Scanning tags."));
3633 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3634 }
3635 else
3636 type = -1;
3637
3638 /* in any case e_cpt is advanced to the next entry */
3639 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
3640
3641 found_all = TRUE;
3642 if (type == -1)
3643 continue;
3644 }
3645 }
3646
3647 switch (type)
3648 {
3649 case -1:
3650 break;
3651#ifdef FEAT_FIND_ID
3652 case CTRL_X_PATH_PATTERNS:
3653 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003654 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003655 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003657 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3659 (linenr_T)1, (linenr_T)MAXLNUM);
3660 break;
3661#endif
3662
3663 case CTRL_X_DICTIONARY:
3664 case CTRL_X_THESAURUS:
3665 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00003666 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 : (type == CTRL_X_THESAURUS
3668 ? (*curbuf->b_p_tsr == NUL
3669 ? p_tsr
3670 : curbuf->b_p_tsr)
3671 : (*curbuf->b_p_dict == NUL
3672 ? p_dict
3673 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003674 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00003675 dict != NULL ? dict_f
3676 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 dict = NULL;
3678 break;
3679
3680 case CTRL_X_TAGS:
3681 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
3682 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003683 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684
3685 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003686 * of matches is found when compl_pattern is empty */
3687 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
3689 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
3690 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3691 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003692 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 }
3694 p_ic = save_p_ic;
3695 break;
3696
3697 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003698 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
3700 {
3701
3702 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003703 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003704 ins_compl_add_matches(num_matches, matches,
3705#ifdef CASE_INSENSITIVE_FILENAME
3706 TRUE
3707#else
3708 FALSE
3709#endif
3710 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 }
3712 break;
3713
3714 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003715 if (expand_cmdline(&compl_xp, compl_pattern,
3716 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003718 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 break;
3720
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003721#ifdef FEAT_COMPL_FUNC
3722 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003723 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003724 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003725 break;
3726#endif
3727
Bram Moolenaar488c6512005-08-11 20:09:58 +00003728 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003729#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00003730 num_matches = expand_spelling(first_match_pos.lnum,
3731 first_match_pos.col, compl_pattern, &matches);
3732 if (num_matches > 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003733 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar488c6512005-08-11 20:09:58 +00003734#endif
3735 break;
3736
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 default: /* normal ^P/^N and ^X^L */
3738 /*
3739 * If 'infercase' is set, don't use 'smartcase' here
3740 */
3741 save_p_scs = p_scs;
3742 if (ins_buf->b_p_inf)
3743 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003744
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 /* buffers other than curbuf are scanned from the beginning or the
3746 * end but never from the middle, thus setting nowrapscan in this
3747 * buffers is a good idea, on the other hand, we always set
3748 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
3749 save_p_ws = p_ws;
3750 if (ins_buf != curbuf)
3751 p_ws = FALSE;
3752 else if (*e_cpt == '.')
3753 p_ws = TRUE;
3754 for (;;)
3755 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003756 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003758 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
3759 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003761 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003763 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003765 found_new_match = searchit(NULL, ins_buf, pos,
3766 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003767 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003768 RE_LAST, (linenr_T)0);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003769 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003771 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003772 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 first_match_pos = *pos;
3774 last_match_pos = *pos;
3775 }
3776 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003777 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 found_new_match = FAIL;
3779 if (found_new_match == FAIL)
3780 {
3781 if (ins_buf == curbuf)
3782 found_all = TRUE;
3783 break;
3784 }
3785
3786 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003787 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 && ini->lnum == pos->lnum
3789 && ini->col == pos->col)
3790 continue;
3791 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
3792 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3793 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003794 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 {
3796 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3797 continue;
3798 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3799 if (!p_paste)
3800 ptr = skipwhite(ptr);
3801 }
3802 len = (int)STRLEN(ptr);
3803 }
3804 else
3805 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003806 char_u *tmp_ptr = ptr;
3807
3808 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003810 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 /* Skip if already inside a word. */
3812 if (vim_iswordp(tmp_ptr))
3813 continue;
3814 /* Find start of next word. */
3815 tmp_ptr = find_word_start(tmp_ptr);
3816 }
3817 /* Find end of this word. */
3818 tmp_ptr = find_word_end(tmp_ptr);
3819 len = (int)(tmp_ptr - ptr);
3820
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003821 if ((compl_cont_status & CONT_ADDING)
3822 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 {
3824 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3825 {
3826 /* Try next line, if any. the new word will be
3827 * "join" as if the normal command "J" was used.
3828 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003829 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 * works -- Acevedo */
3831 STRNCPY(IObuff, ptr, len);
3832 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3833 tmp_ptr = ptr = skipwhite(ptr);
3834 /* Find start of next word. */
3835 tmp_ptr = find_word_start(tmp_ptr);
3836 /* Find end of next word. */
3837 tmp_ptr = find_word_end(tmp_ptr);
3838 if (tmp_ptr > ptr)
3839 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003840 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003842 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 IObuff[len++] = ' ';
3844 /* IObuf =~ "\k.* ", thus len >= 2 */
3845 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003846 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 || (vim_strchr(p_cpo, CPO_JOINSP)
3848 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003849 && (IObuff[len - 2] == '?'
3850 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 IObuff[len++] = ' ';
3852 }
3853 /* copy as much as posible of the new word */
3854 if (tmp_ptr - ptr >= IOSIZE - len)
3855 tmp_ptr = ptr + IOSIZE - len - 1;
3856 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3857 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00003858 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 }
3860 IObuff[len] = NUL;
3861 ptr = IObuff;
3862 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003863 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 continue;
3865 }
3866 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003867 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003868 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003869 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 {
3871 found_new_match = OK;
3872 break;
3873 }
3874 }
3875 p_scs = save_p_scs;
3876 p_ws = save_p_ws;
3877 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003878
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003879 /* check if compl_curr_match has changed, (e.g. other type of
3880 * expansion added somenthing) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003881 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 found_new_match = OK;
3883
3884 /* break the loop for specialized modes (use 'complete' just for the
3885 * generic ctrl_x_mode == 0) or when we've found a new match */
3886 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003887 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003888 {
3889 if (got_int)
3890 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003891 /* Fill the popup menu as soon as possible. */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003892 if (pum_wanted() && type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003893 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003894
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003895 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
3896 || compl_interrupted)
3897 break;
3898 compl_started = TRUE;
3899 }
3900 else
3901 {
3902 /* Mark a buffer scanned when it has been scanned completely */
3903 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
3904 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003906 compl_started = FALSE;
3907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003909 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910
3911 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
3912 && *e_cpt == NUL) /* Got to end of 'complete' */
3913 found_new_match = FAIL;
3914
3915 i = -1; /* total of matches, unknown */
3916 if (found_new_match == FAIL
3917 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
3918 i = ins_compl_make_cyclic();
3919
3920 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003921 * just been made cyclic then we have to move compl_curr_match to the next
3922 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003923 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
3924 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003925 if (compl_curr_match == NULL)
3926 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 return i;
3928}
3929
3930/* Delete the old text being completed. */
3931 static void
3932ins_compl_delete()
3933{
3934 int i;
3935
3936 /*
3937 * In insert mode: Delete the typed part.
3938 * In replace mode: Put the old characters back, if any.
3939 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003940 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 backspace_until_column(i);
3942 changed_cline_bef_curs();
3943}
3944
3945/* Insert the new text being completed. */
3946 static void
3947ins_compl_insert()
3948{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003949 ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003950 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3951 compl_used_match = FALSE;
3952 else
3953 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954}
3955
3956/*
3957 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003958 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
3959 * get more completions. If it is FALSE, then we just do nothing when there
3960 * are no more completions in a given direction. The latter case is used when
3961 * we are still in the middle of finding completions, to allow browsing
3962 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 * Return the total number of matches, or -1 if still unknown -- webb.
3964 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003965 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
3966 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 *
3968 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00003969 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
3970 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 */
3972 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003973ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003975 int count; /* repeat completion this many times; should
3976 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003977 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978{
3979 int num_matches = -1;
3980 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003981 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00003982 compl_T *found_compl = NULL;
3983 int found_end = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003985 if (compl_leader != NULL
3986 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003988 /* Set "compl_shown_match" to the actually shown match, it may differ
3989 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003990 while (!ins_compl_equal(compl_shown_match,
3991 compl_leader, STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003992 && compl_shown_match->cp_next != NULL
3993 && compl_shown_match->cp_next != compl_first_match)
3994 compl_shown_match = compl_shown_match->cp_next;
3995 }
3996
3997 if (allow_get_expansion && insert_match
3998 && (!compl_get_longest || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 /* Delete old text to be replaced */
4000 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004001
Bram Moolenaare3226be2005-12-18 22:10:00 +00004002 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4003 * around. */
4004 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004006 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004008 if (compl_pending != 0)
4009 --compl_pending;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004010 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004011 found_end = (compl_first_match != NULL
4012 && (compl_shown_match->cp_next == compl_first_match
4013 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004014 }
4015 else if (compl_shows_dir == BACKWARD
4016 && compl_shown_match->cp_prev != NULL)
4017 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004018 if (compl_pending != 0)
4019 ++compl_pending;
Bram Moolenaara6557602006-02-04 22:43:20 +00004020 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004021 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004022 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 }
4024 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004025 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004026 if (compl_shows_dir == BACKWARD)
4027 --compl_pending;
4028 else
4029 ++compl_pending;
Bram Moolenaara6557602006-02-04 22:43:20 +00004030 if (!allow_get_expansion)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004031 return -1;
Bram Moolenaara6557602006-02-04 22:43:20 +00004032
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004033 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004034 if (compl_pending != 0 && compl_direction == compl_shows_dir)
Bram Moolenaara6557602006-02-04 22:43:20 +00004035 compl_shown_match = compl_curr_match;
4036 found_end = FALSE;
4037 }
4038 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4039 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004040 && !ins_compl_equal(compl_shown_match,
4041 compl_leader, STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004042 ++todo;
4043 else
4044 /* Remember a matching item. */
4045 found_compl = compl_shown_match;
4046
4047 /* Stop at the end of the list when we found a usable match. */
4048 if (found_end)
4049 {
4050 if (found_compl != NULL)
4051 {
4052 compl_shown_match = found_compl;
4053 break;
4054 }
4055 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 }
4058
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004059 /* Insert the text of the new completion, or the compl_leader. */
4060 if (insert_match)
4061 {
4062 if (!compl_get_longest || compl_used_match)
4063 ins_compl_insert();
4064 else
4065 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
4066 }
4067 else
4068 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069
4070 if (!allow_get_expansion)
4071 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004072 /* may undisplay the popup menu first */
4073 ins_compl_upd_pum();
4074
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004075 /* redraw to show the user what was inserted */
4076 update_screen(0);
4077
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004078 /* display the updated popup menu */
4079 ins_compl_show_pum();
4080
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 /* Delete old text to be replaced, since we're still searching and
4082 * don't want to match ourselves! */
4083 ins_compl_delete();
4084 }
4085
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004086 /* Enter will select a match when the match wasn't inserted and the popup
4087 * menu is visislbe. */
4088 compl_enter_selects = !insert_match && compl_match_array != NULL;
4089
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 /*
4091 * Show the file name for the match (if any)
4092 * Truncate the file name to avoid a wait for return.
4093 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004094 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 {
4096 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004097 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 if (i <= 0)
4099 i = 0;
4100 else
4101 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004102 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 msg(IObuff);
4104 redraw_cmdline = FALSE; /* don't overwrite! */
4105 }
4106
4107 return num_matches;
4108}
4109
4110/*
4111 * Call this while finding completions, to check whether the user has hit a key
4112 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004113 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004115 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 */
4117 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004118ins_compl_check_keys(frequency)
4119 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120{
4121 static int count = 0;
4122
4123 int c;
4124
4125 /* Don't check when reading keys from a script. That would break the test
4126 * scripts */
4127 if (using_script())
4128 return;
4129
4130 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004131 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 return;
4133 count = 0;
4134
4135 ++no_mapping;
4136 c = vpeekc_any();
4137 --no_mapping;
4138 if (c != NUL)
4139 {
4140 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4141 {
4142 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004143 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004144 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4145 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 }
4147 else if (c != Ctrl_R)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004148 compl_interrupted = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004150 if (compl_pending != 0 && !got_int)
4151 (void)ins_compl_next(FALSE, compl_pending > 0
4152 ? compl_pending : -compl_pending, TRUE);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004153}
4154
4155/*
4156 * Decide the direction of Insert mode complete from the key typed.
4157 * Returns BACKWARD or FORWARD.
4158 */
4159 static int
4160ins_compl_key2dir(c)
4161 int c;
4162{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004163 if (c == Ctrl_P || c == Ctrl_L
4164 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4165 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004166 return BACKWARD;
4167 return FORWARD;
4168}
4169
4170/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004171 * Return TRUE for keys that are used for completion only when the popup menu
4172 * is visible.
4173 */
4174 static int
4175ins_compl_pum_key(c)
4176 int c;
4177{
4178 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004179 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4180 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004181}
4182
4183/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004184 * Decide the number of completions to move forward.
4185 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4186 */
4187 static int
4188ins_compl_key2count(c)
4189 int c;
4190{
4191 int h;
4192
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004193 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004194 {
4195 h = pum_get_height();
4196 if (h > 3)
4197 h -= 2; /* keep some context */
4198 return h;
4199 }
4200 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201}
4202
4203/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004204 * Return TRUE if completion with "c" should insert the match, FALSE if only
4205 * to change the currently selected completion.
4206 */
4207 static int
4208ins_compl_use_match(c)
4209 int c;
4210{
4211 switch (c)
4212 {
4213 case K_UP:
4214 case K_DOWN:
4215 case K_PAGEDOWN:
4216 case K_KPAGEDOWN:
4217 case K_S_DOWN:
4218 case K_PAGEUP:
4219 case K_KPAGEUP:
4220 case K_S_UP:
4221 return FALSE;
4222 }
4223 return TRUE;
4224}
4225
4226/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 * Do Insert mode completion.
4228 * Called when character "c" was typed, which has a meaning for completion.
4229 * Returns OK if completion was done, FAIL if something failed (out of mem).
4230 */
4231 static int
4232ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004233 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004235 char_u *line;
4236 int startcol = 0; /* column where searched text starts */
4237 colnr_T curs_col; /* cursor column */
4238 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239
Bram Moolenaare3226be2005-12-18 22:10:00 +00004240 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004241 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 {
4243 /* First time we hit ^N or ^P (in a row, I mean) */
4244
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 did_ai = FALSE;
4246#ifdef FEAT_SMARTINDENT
4247 did_si = FALSE;
4248 can_si = FALSE;
4249 can_si_back = FALSE;
4250#endif
4251 if (stop_arrow() == FAIL)
4252 return FAIL;
4253
4254 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004255 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004256 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257
4258 /* if this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004259 * "compl_startpos" to the cursor as a pattern to add a new word
4260 * instead of expand the one before the cursor, in word-wise if
4261 * "compl_startpos"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 * is not in the same line as the cursor then fix it (the line has
4263 * been split because it was longer than 'tw'). if SOL is set then
4264 * skip the previous pattern, a word at the beginning of the line has
4265 * been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004266 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4267 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 {
4269 /*
4270 * it is a continued search
4271 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004272 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4274 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4275 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004276 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004278 /* line (probably) wrapped, set compl_startpos to the
4279 * first non_blank in the line, if it is not a wordchar
4280 * include it to get a better pattern, but then we don't
4281 * want the "\\<" prefix, check it bellow */
4282 compl_col = (colnr_T)(skipwhite(line) - line);
4283 compl_startpos.col = compl_col;
4284 compl_startpos.lnum = curwin->w_cursor.lnum;
4285 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 }
4287 else
4288 {
4289 /* S_IPOS was set when we inserted a word that was at the
4290 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004291 * mode but first we need to redefine compl_startpos */
4292 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004294 compl_cont_status |= CONT_SOL;
4295 compl_startpos.col = (colnr_T)(skipwhite(
4296 line + compl_length
4297 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004299 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004301 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004302 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 * have enough space? just being paranoic */
4304#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004305 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004307 compl_cont_status &= ~CONT_SOL;
4308 compl_length = (IOSIZE - MIN_SPACE);
4309 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004311 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4312 if (compl_length < 1)
4313 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 }
4315 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004316 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004318 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 }
4320 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004321 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004323 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004325 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004327 compl_cont_status = 0;
4328 compl_cont_status |= CONT_N_ADDS;
4329 compl_startpos = curwin->w_cursor;
4330 startcol = (int)curs_col;
4331 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 }
4333
4334 /* Work out completion pattern and original text -- webb */
4335 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4336 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004337 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4339 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004340 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004342 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004344 compl_col += ++startcol;
4345 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 }
4347 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004348 compl_pattern = str_foldcase(line + compl_col,
4349 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004351 compl_pattern = vim_strnsave(line + compl_col,
4352 compl_length);
4353 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 return FAIL;
4355 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004356 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 {
4358 char_u *prefix = (char_u *)"\\<";
4359
4360 /* we need 3 extra chars, 1 for the NUL and
4361 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004362 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4363 compl_length) + 3);
4364 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004366 if (!vim_iswordp(line + compl_col)
4367 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 && (
4369#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004370 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004372 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373#endif
4374 )))
4375 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004376 STRCPY((char *)compl_pattern, prefix);
4377 (void)quote_meta(compl_pattern + STRLEN(prefix),
4378 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004380 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004382 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004384 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385#endif
4386 )
4387 {
4388 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004389 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4390 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004392 compl_col += curs_col;
4393 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 }
4395 else
4396 {
4397#ifdef FEAT_MBYTE
4398 /* Search the point of change class of multibyte character
4399 * or not a word single byte character backward. */
4400 if (has_mbyte)
4401 {
4402 int base_class;
4403 int head_off;
4404
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004405 startcol -= (*mb_head_off)(line, line + startcol);
4406 base_class = mb_get_class(line + startcol);
4407 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004409 head_off = (*mb_head_off)(line, line + startcol);
4410 if (base_class != mb_get_class(line + startcol
4411 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004413 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 }
4415 }
4416 else
4417#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004418 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004420 compl_col += ++startcol;
4421 compl_length = (int)curs_col - startcol;
4422 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 {
4424 /* Only match word with at least two chars -- webb
4425 * there's no need to call quote_meta,
4426 * alloc(7) is enough -- Acevedo
4427 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004428 compl_pattern = alloc(7);
4429 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004431 STRCPY((char *)compl_pattern, "\\<");
4432 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4433 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 }
4435 else
4436 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004437 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4438 compl_length) + 3);
4439 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004441 STRCPY((char *)compl_pattern, "\\<");
4442 (void)quote_meta(compl_pattern + 2, line + compl_col,
4443 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 }
4445 }
4446 }
4447 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4448 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004449 compl_col = skipwhite(line) - line;
4450 compl_length = (int)curs_col - (int)compl_col;
4451 if (compl_length < 0) /* cursor in indent: empty pattern */
4452 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004454 compl_pattern = str_foldcase(line + compl_col, compl_length,
4455 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004457 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4458 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 return FAIL;
4460 }
4461 else if (ctrl_x_mode == CTRL_X_FILES)
4462 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004463 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004465 compl_col += ++startcol;
4466 compl_length = (int)curs_col - startcol;
4467 compl_pattern = addstar(line + compl_col, compl_length,
4468 EXPAND_FILES);
4469 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 return FAIL;
4471 }
4472 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4473 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004474 compl_pattern = vim_strnsave(line, curs_col);
4475 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004477 set_cmd_context(&compl_xp, compl_pattern,
4478 (int)STRLEN(compl_pattern), curs_col);
4479 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4480 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004482 startcol = (int)(compl_xp.xp_pattern - compl_pattern);
4483 compl_col = startcol;
4484 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004486 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004487 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00004488#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004489 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00004490 * Call user defined function 'completefunc' with "a:findstart"
4491 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004492 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00004493 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004494 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004495 char_u *funcname;
4496 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004497
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004498 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004499 * string */
4500 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4501 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4502 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004503 {
4504 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
4505 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004506 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004507 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004508
4509 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004510 args[1] = NULL;
4511 pos = curwin->w_cursor;
4512 col = call_func_retnr(funcname, 2, args, FALSE);
4513 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004514
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004515 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004516 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004517 compl_col = col;
4518 if ((colnr_T)compl_col > curs_col)
4519 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004520
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004521 /* Setup variables for completion. Need to obtain "line" again,
4522 * it may have become invalid. */
4523 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004524 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004525 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4526 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004527#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004528 return FAIL;
4529 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00004530 else if (ctrl_x_mode == CTRL_X_SPELL)
4531 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004532#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004533 if (spell_bad_len > 0)
4534 compl_col = curs_col - spell_bad_len;
4535 else
4536 compl_col = spell_word_start(startcol);
4537 if (compl_col >= (colnr_T)startcol)
Bram Moolenaar488c6512005-08-11 20:09:58 +00004538 return FAIL;
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00004539 spell_expand_check_cap(compl_col);
Bram Moolenaare2f98b92006-03-29 21:18:24 +00004540 /* Need to obtain "line" again, it may have become invalid. */
4541 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004542 compl_length = (int)curs_col - compl_col;
4543 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4544 if (compl_pattern == NULL)
4545#endif
4546 return FAIL;
4547 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004548 else
4549 {
4550 EMSG2(_(e_intern2), "ins_complete()");
4551 return FAIL;
4552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004554 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 {
4556 edit_submode_pre = (char_u *)_(" Adding");
4557 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4558 {
4559 /* Insert a new line, keep indentation but ignore 'comments' */
4560#ifdef FEAT_COMMENTS
4561 char_u *old = curbuf->b_p_com;
4562
4563 curbuf->b_p_com = (char_u *)"";
4564#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004565 compl_startpos.lnum = curwin->w_cursor.lnum;
4566 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 ins_eol('\r');
4568#ifdef FEAT_COMMENTS
4569 curbuf->b_p_com = old;
4570#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004571 compl_length = 0;
4572 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 }
4574 }
4575 else
4576 {
4577 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004578 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 }
4580
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004581 if (compl_cont_status & CONT_LOCAL)
4582 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 else
4584 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4585
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004586 /* Always add completion for the original text. */
4587 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004588 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4589 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004590 -1, FALSE, NULL, NULL, 0, ORIGINAL_TEXT) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004592 vim_free(compl_pattern);
4593 compl_pattern = NULL;
4594 vim_free(compl_orig_text);
4595 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 return FAIL;
4597 }
4598
4599 /* showmode might reset the internal line pointers, so it must
4600 * be called before line = ml_get(), or when this address is no
4601 * longer needed. -- Acevedo.
4602 */
4603 edit_submode_extra = (char_u *)_("-- Searching...");
4604 edit_submode_highl = HLF_COUNT;
4605 showmode();
4606 edit_submode_extra = NULL;
4607 out_flush();
4608 }
4609
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004610 compl_shown_match = compl_curr_match;
4611 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612
4613 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004614 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004616 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004618 /* may undisplay the popup menu */
4619 ins_compl_upd_pum();
4620
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004621 if (n > 1) /* all matches have been found */
4622 compl_matches = n;
4623 compl_curr_match = compl_shown_match;
4624 compl_direction = compl_shows_dir;
4625 compl_interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626
4627 /* eat the ESC to avoid leaving insert mode */
4628 if (got_int && !global_busy)
4629 {
4630 (void)vgetc();
4631 got_int = FALSE;
4632 }
4633
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004634 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004635 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004637 edit_submode_extra = (compl_cont_status & CONT_ADDING)
4638 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
4640 edit_submode_highl = HLF_E;
4641 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4642 * because we couldn't expand anything at first place, but if we used
4643 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4644 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004645 if ( compl_length > 1
4646 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 || (ctrl_x_mode != 0
4648 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
4649 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004650 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 }
4652
Bram Moolenaar572cb562005-08-05 21:35:02 +00004653 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004654 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004656 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657
4658 if (edit_submode_extra == NULL)
4659 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004660 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 {
4662 edit_submode_extra = (char_u *)_("Back at original");
4663 edit_submode_highl = HLF_W;
4664 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004665 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 {
4667 edit_submode_extra = (char_u *)_("Word from other line");
4668 edit_submode_highl = HLF_COUNT;
4669 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004670 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 {
4672 edit_submode_extra = (char_u *)_("The only match");
4673 edit_submode_highl = HLF_COUNT;
4674 }
4675 else
4676 {
4677 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004678 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004680 int number = 0;
4681 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004683 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 {
4685 /* search backwards for the first valid (!= -1) number.
4686 * This should normally succeed already at the first loop
4687 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004688 for (match = compl_curr_match->cp_prev; match != NULL
4689 && match != compl_first_match;
4690 match = match->cp_prev)
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 up and assign all numbers which are not assigned
4698 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004699 for (match = match->cp_next;
4700 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004701 match = match->cp_next)
4702 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 }
4704 else /* BACKWARD */
4705 {
4706 /* search forwards (upwards) for the first valid (!= -1)
4707 * number. This should normally succeed already at the
4708 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004709 for (match = compl_curr_match->cp_next; match != NULL
4710 && match != compl_first_match;
4711 match = match->cp_next)
4712 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004714 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 break;
4716 }
4717 if (match != NULL)
4718 /* go down and assign all numbers which are not
4719 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004720 for (match = match->cp_prev; match
4721 && match->cp_number == -1;
4722 match = match->cp_prev)
4723 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 }
4725 }
4726
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004727 /* The match should always have a sequence number now, this is
4728 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004729 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 {
4731 /* Space for 10 text chars. + 2x10-digit no.s */
4732 static char_u match_ref[31];
4733
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004734 if (compl_matches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 sprintf((char *)IObuff, _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004736 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004738 sprintf((char *)IObuff, _("match %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004739 compl_curr_match->cp_number);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004740 vim_strncpy(match_ref, IObuff, 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 edit_submode_extra = match_ref;
4742 edit_submode_highl = HLF_R;
4743 if (dollar_vcol)
4744 curs_columns(FALSE);
4745 }
4746 }
4747 }
4748
4749 /* Show a message about what (completion) mode we're in. */
4750 showmode();
4751 if (edit_submode_extra != NULL)
4752 {
4753 if (!p_smd)
4754 msg_attr(edit_submode_extra,
4755 edit_submode_highl < HLF_COUNT
4756 ? hl_attr(edit_submode_highl) : 0);
4757 }
4758 else
4759 msg_clr_cmdline(); /* necessary for "noshowmode" */
4760
Bram Moolenaara94bc432006-03-10 21:42:59 +00004761 /* RedrawingDisabled may be set when invoked through complete(). */
4762 n = RedrawingDisabled;
4763 RedrawingDisabled = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004764 ins_compl_show_pum();
Bram Moolenaara94bc432006-03-10 21:42:59 +00004765 setcursor();
4766 RedrawingDisabled = n;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004767
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 return OK;
4769}
4770
4771/*
4772 * Looks in the first "len" chars. of "src" for search-metachars.
4773 * If dest is not NULL the chars. are copied there quoting (with
4774 * a backslash) the metachars, and dest would be NUL terminated.
4775 * Returns the length (needed) of dest
4776 */
4777 static int
4778quote_meta(dest, src, len)
4779 char_u *dest;
4780 char_u *src;
4781 int len;
4782{
4783 int m;
4784
4785 for (m = len; --len >= 0; src++)
4786 {
4787 switch (*src)
4788 {
4789 case '.':
4790 case '*':
4791 case '[':
4792 if (ctrl_x_mode == CTRL_X_DICTIONARY
4793 || ctrl_x_mode == CTRL_X_THESAURUS)
4794 break;
4795 case '~':
4796 if (!p_magic) /* quote these only if magic is set */
4797 break;
4798 case '\\':
4799 if (ctrl_x_mode == CTRL_X_DICTIONARY
4800 || ctrl_x_mode == CTRL_X_THESAURUS)
4801 break;
4802 case '^': /* currently it's not needed. */
4803 case '$':
4804 m++;
4805 if (dest != NULL)
4806 *dest++ = '\\';
4807 break;
4808 }
4809 if (dest != NULL)
4810 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004811# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 /* Copy remaining bytes of a multibyte character. */
4813 if (has_mbyte)
4814 {
4815 int i, mb_len;
4816
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004817 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 if (mb_len > 0 && len >= mb_len)
4819 for (i = 0; i < mb_len; ++i)
4820 {
4821 --len;
4822 ++src;
4823 if (dest != NULL)
4824 *dest++ = *src;
4825 }
4826 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004827# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 }
4829 if (dest != NULL)
4830 *dest = NUL;
4831
4832 return m;
4833}
4834#endif /* FEAT_INS_EXPAND */
4835
4836/*
4837 * Next character is interpreted literally.
4838 * A one, two or three digit decimal number is interpreted as its byte value.
4839 * If one or two digits are entered, the next character is given to vungetc().
4840 * For Unicode a character > 255 may be returned.
4841 */
4842 int
4843get_literal()
4844{
4845 int cc;
4846 int nc;
4847 int i;
4848 int hex = FALSE;
4849 int octal = FALSE;
4850#ifdef FEAT_MBYTE
4851 int unicode = 0;
4852#endif
4853
4854 if (got_int)
4855 return Ctrl_C;
4856
4857#ifdef FEAT_GUI
4858 /*
4859 * In GUI there is no point inserting the internal code for a special key.
4860 * It is more useful to insert the string "<KEY>" instead. This would
4861 * probably be useful in a text window too, but it would not be
4862 * vi-compatible (maybe there should be an option for it?) -- webb
4863 */
4864 if (gui.in_use)
4865 ++allow_keys;
4866#endif
4867#ifdef USE_ON_FLY_SCROLL
4868 dont_scroll = TRUE; /* disallow scrolling here */
4869#endif
4870 ++no_mapping; /* don't map the next key hits */
4871 cc = 0;
4872 i = 0;
4873 for (;;)
4874 {
4875 do
4876 nc = safe_vgetc();
4877 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
4878 || nc == K_HOR_SCROLLBAR);
4879#ifdef FEAT_CMDL_INFO
4880 if (!(State & CMDLINE)
4881# ifdef FEAT_MBYTE
4882 && MB_BYTE2LEN_CHECK(nc) == 1
4883# endif
4884 )
4885 add_to_showcmd(nc);
4886#endif
4887 if (nc == 'x' || nc == 'X')
4888 hex = TRUE;
4889 else if (nc == 'o' || nc == 'O')
4890 octal = TRUE;
4891#ifdef FEAT_MBYTE
4892 else if (nc == 'u' || nc == 'U')
4893 unicode = nc;
4894#endif
4895 else
4896 {
4897 if (hex
4898#ifdef FEAT_MBYTE
4899 || unicode != 0
4900#endif
4901 )
4902 {
4903 if (!vim_isxdigit(nc))
4904 break;
4905 cc = cc * 16 + hex2nr(nc);
4906 }
4907 else if (octal)
4908 {
4909 if (nc < '0' || nc > '7')
4910 break;
4911 cc = cc * 8 + nc - '0';
4912 }
4913 else
4914 {
4915 if (!VIM_ISDIGIT(nc))
4916 break;
4917 cc = cc * 10 + nc - '0';
4918 }
4919
4920 ++i;
4921 }
4922
4923 if (cc > 255
4924#ifdef FEAT_MBYTE
4925 && unicode == 0
4926#endif
4927 )
4928 cc = 255; /* limit range to 0-255 */
4929 nc = 0;
4930
4931 if (hex) /* hex: up to two chars */
4932 {
4933 if (i >= 2)
4934 break;
4935 }
4936#ifdef FEAT_MBYTE
4937 else if (unicode) /* Unicode: up to four or eight chars */
4938 {
4939 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
4940 break;
4941 }
4942#endif
4943 else if (i >= 3) /* decimal or octal: up to three chars */
4944 break;
4945 }
4946 if (i == 0) /* no number entered */
4947 {
4948 if (nc == K_ZERO) /* NUL is stored as NL */
4949 {
4950 cc = '\n';
4951 nc = 0;
4952 }
4953 else
4954 {
4955 cc = nc;
4956 nc = 0;
4957 }
4958 }
4959
4960 if (cc == 0) /* NUL is stored as NL */
4961 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00004962#ifdef FEAT_MBYTE
4963 if (enc_dbcs && (cc & 0xff) == 0)
4964 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
4965 second byte will cause trouble! */
4966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967
4968 --no_mapping;
4969#ifdef FEAT_GUI
4970 if (gui.in_use)
4971 --allow_keys;
4972#endif
4973 if (nc)
4974 vungetc(nc);
4975 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
4976 return cc;
4977}
4978
4979/*
4980 * Insert character, taking care of special keys and mod_mask
4981 */
4982 static void
4983insert_special(c, allow_modmask, ctrlv)
4984 int c;
4985 int allow_modmask;
4986 int ctrlv; /* c was typed after CTRL-V */
4987{
4988 char_u *p;
4989 int len;
4990
4991 /*
4992 * Special function key, translate into "<Key>". Up to the last '>' is
4993 * inserted with ins_str(), so as not to replace characters in replace
4994 * mode.
4995 * Only use mod_mask for special keys, to avoid things like <S-Space>,
4996 * unless 'allow_modmask' is TRUE.
4997 */
4998#ifdef MACOS
4999 /* Command-key never produces a normal key */
5000 if (mod_mask & MOD_MASK_CMD)
5001 allow_modmask = TRUE;
5002#endif
5003 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5004 {
5005 p = get_special_key_name(c, mod_mask);
5006 len = (int)STRLEN(p);
5007 c = p[len - 1];
5008 if (len > 2)
5009 {
5010 if (stop_arrow() == FAIL)
5011 return;
5012 p[len - 1] = NUL;
5013 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005014 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 ctrlv = FALSE;
5016 }
5017 }
5018 if (stop_arrow() == OK)
5019 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5020}
5021
5022/*
5023 * Special characters in this context are those that need processing other
5024 * than the simple insertion that can be performed here. This includes ESC
5025 * which terminates the insert, and CR/NL which need special processing to
5026 * open up a new line. This routine tries to optimize insertions performed by
5027 * the "redo", "undo" or "put" commands, so it needs to know when it should
5028 * stop and defer processing to the "normal" mechanism.
5029 * '0' and '^' are special, because they can be followed by CTRL-D.
5030 */
5031#ifdef EBCDIC
5032# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5033#else
5034# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5035#endif
5036
5037#ifdef FEAT_MBYTE
5038# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5039#else
5040# define WHITECHAR(cc) vim_iswhite(cc)
5041#endif
5042
5043 void
5044insertchar(c, flags, second_indent)
5045 int c; /* character to insert or NUL */
5046 int flags; /* INSCHAR_FORMAT, etc. */
5047 int second_indent; /* indent for second line if >= 0 */
5048{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 int textwidth;
5050#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054
5055 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5056 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057
5058 /*
5059 * Try to break the line in two or more pieces when:
5060 * - Always do this if we have been called to do formatting only.
5061 * - Always do this when 'formatoptions' has the 'a' flag and the line
5062 * ends in white space.
5063 * - Otherwise:
5064 * - Don't do this if inserting a blank
5065 * - Don't do this if an existing character is being replaced, unless
5066 * we're in VREPLACE mode.
5067 * - Do this if the cursor is not on the line where insert started
5068 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5069 * before the insert.
5070 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5071 * before 'textwidth'
5072 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005073 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 && ((flags & INSCHAR_FORMAT)
5075 || (!vim_iswhite(c)
5076 && !((State & REPLACE_FLAG)
5077#ifdef FEAT_VREPLACE
5078 && !(State & VREPLACE_FLAG)
5079#endif
5080 && *ml_get_cursor() != NUL)
5081 && (curwin->w_cursor.lnum != Insstart.lnum
5082 || ((!has_format_option(FO_INS_LONG)
5083 || Insstart_textlen <= (colnr_T)textwidth)
5084 && (!fo_ins_blank
5085 || Insstart_blank_vcol <= (colnr_T)textwidth
5086 ))))))
5087 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005088 /* Format with 'formatexpr' when it's set. Use internal formatting
5089 * when 'formatexpr' isn't set or it returns non-zero. */
5090#if defined(FEAT_EVAL)
5091 if (*curbuf->b_p_fex == NUL
5092 || fex_format(curwin->w_cursor.lnum, 1L) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005094 internal_format(textwidth, second_indent, flags, c == NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005096
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 if (c == NUL) /* only formatting was wanted */
5098 return;
5099
5100#ifdef FEAT_COMMENTS
5101 /* Check whether this character should end a comment. */
5102 if (did_ai && (int)c == end_comment_pending)
5103 {
5104 char_u *line;
5105 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5106 int middle_len, end_len;
5107 int i;
5108
5109 /*
5110 * Need to remove existing (middle) comment leader and insert end
5111 * comment leader. First, check what comment leader we can find.
5112 */
5113 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5114 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5115 {
5116 /* Skip middle-comment string */
5117 while (*p && p[-1] != ':') /* find end of middle flags */
5118 ++p;
5119 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5120 /* Don't count trailing white space for middle_len */
5121 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5122 --middle_len;
5123
5124 /* Find the end-comment string */
5125 while (*p && p[-1] != ':') /* find end of end flags */
5126 ++p;
5127 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5128
5129 /* Skip white space before the cursor */
5130 i = curwin->w_cursor.col;
5131 while (--i >= 0 && vim_iswhite(line[i]))
5132 ;
5133 i++;
5134
5135 /* Skip to before the middle leader */
5136 i -= middle_len;
5137
5138 /* Check some expected things before we go on */
5139 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5140 {
5141 /* Backspace over all the stuff we want to replace */
5142 backspace_until_column(i);
5143
5144 /*
5145 * Insert the end-comment string, except for the last
5146 * character, which will get inserted as normal later.
5147 */
5148 ins_bytes_len(lead_end, end_len - 1);
5149 }
5150 }
5151 }
5152 end_comment_pending = NUL;
5153#endif
5154
5155 did_ai = FALSE;
5156#ifdef FEAT_SMARTINDENT
5157 did_si = FALSE;
5158 can_si = FALSE;
5159 can_si_back = FALSE;
5160#endif
5161
5162 /*
5163 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5164 * This speeds up normal text input considerably.
5165 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5166 * need to re-indent at a ':', or any other character (but not what
5167 * 'paste' is set)..
5168 */
5169#ifdef USE_ON_FLY_SCROLL
5170 dont_scroll = FALSE; /* allow scrolling here */
5171#endif
5172
5173 if ( !ISSPECIAL(c)
5174#ifdef FEAT_MBYTE
5175 && (!has_mbyte || (*mb_char2len)(c) == 1)
5176#endif
5177 && vpeekc() != NUL
5178 && !(State & REPLACE_FLAG)
5179#ifdef FEAT_CINDENT
5180 && !cindent_on()
5181#endif
5182#ifdef FEAT_RIGHTLEFT
5183 && !p_ri
5184#endif
5185 )
5186 {
5187#define INPUT_BUFLEN 100
5188 char_u buf[INPUT_BUFLEN + 1];
5189 int i;
5190 colnr_T virtcol = 0;
5191
5192 buf[0] = c;
5193 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005194 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 virtcol = get_nolist_virtcol();
5196 /*
5197 * Stop the string when:
5198 * - no more chars available
5199 * - finding a special character (command key)
5200 * - buffer is full
5201 * - running into the 'textwidth' boundary
5202 * - need to check for abbreviation: A non-word char after a word-char
5203 */
5204 while ( (c = vpeekc()) != NUL
5205 && !ISSPECIAL(c)
5206#ifdef FEAT_MBYTE
5207 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5208#endif
5209 && i < INPUT_BUFLEN
5210 && (textwidth == 0
5211 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5212 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5213 {
5214#ifdef FEAT_RIGHTLEFT
5215 c = vgetc();
5216 if (p_hkmap && KeyTyped)
5217 c = hkmap(c); /* Hebrew mode mapping */
5218# ifdef FEAT_FKMAP
5219 if (p_fkmap && KeyTyped)
5220 c = fkmap(c); /* Farsi mode mapping */
5221# endif
5222 buf[i++] = c;
5223#else
5224 buf[i++] = vgetc();
5225#endif
5226 }
5227
5228#ifdef FEAT_DIGRAPHS
5229 do_digraph(-1); /* clear digraphs */
5230 do_digraph(buf[i-1]); /* may be the start of a digraph */
5231#endif
5232 buf[i] = NUL;
5233 ins_str(buf);
5234 if (flags & INSCHAR_CTRLV)
5235 {
5236 redo_literal(*buf);
5237 i = 1;
5238 }
5239 else
5240 i = 0;
5241 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005242 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 }
5244 else
5245 {
5246#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005247 int cc;
5248
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5250 {
5251 char_u buf[MB_MAXBYTES + 1];
5252
5253 (*mb_char2bytes)(c, buf);
5254 buf[cc] = NUL;
5255 ins_char_bytes(buf, cc);
5256 AppendCharToRedobuff(c);
5257 }
5258 else
5259#endif
5260 {
5261 ins_char(c);
5262 if (flags & INSCHAR_CTRLV)
5263 redo_literal(c);
5264 else
5265 AppendCharToRedobuff(c);
5266 }
5267 }
5268}
5269
5270/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005271 * Format text at the current insert position.
5272 */
5273 static void
5274internal_format(textwidth, second_indent, flags, format_only)
5275 int textwidth;
5276 int second_indent;
5277 int flags;
5278 int format_only;
5279{
5280 int cc;
5281 int save_char = NUL;
5282 int haveto_redraw = FALSE;
5283 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5284#ifdef FEAT_MBYTE
5285 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5286#endif
5287 int fo_white_par = has_format_option(FO_WHITE_PAR);
5288 int first_line = TRUE;
5289#ifdef FEAT_COMMENTS
5290 colnr_T leader_len;
5291 int no_leader = FALSE;
5292 int do_comments = (flags & INSCHAR_DO_COM);
5293#endif
5294
5295 /*
5296 * When 'ai' is off we don't want a space under the cursor to be
5297 * deleted. Replace it with an 'x' temporarily.
5298 */
5299 if (!curbuf->b_p_ai)
5300 {
5301 cc = gchar_cursor();
5302 if (vim_iswhite(cc))
5303 {
5304 save_char = cc;
5305 pchar_cursor('x');
5306 }
5307 }
5308
5309 /*
5310 * Repeat breaking lines, until the current line is not too long.
5311 */
5312 while (!got_int)
5313 {
5314 int startcol; /* Cursor column at entry */
5315 int wantcol; /* column at textwidth border */
5316 int foundcol; /* column for start of spaces */
5317 int end_foundcol = 0; /* column for start of word */
5318 colnr_T len;
5319 colnr_T virtcol;
5320#ifdef FEAT_VREPLACE
5321 int orig_col = 0;
5322 char_u *saved_text = NULL;
5323#endif
5324 colnr_T col;
5325
5326 virtcol = get_nolist_virtcol();
5327 if (virtcol < (colnr_T)textwidth)
5328 break;
5329
5330#ifdef FEAT_COMMENTS
5331 if (no_leader)
5332 do_comments = FALSE;
5333 else if (!(flags & INSCHAR_FORMAT)
5334 && has_format_option(FO_WRAP_COMS))
5335 do_comments = TRUE;
5336
5337 /* Don't break until after the comment leader */
5338 if (do_comments)
5339 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5340 else
5341 leader_len = 0;
5342
5343 /* If the line doesn't start with a comment leader, then don't
5344 * start one in a following broken line. Avoids that a %word
5345 * moved to the start of the next line causes all following lines
5346 * to start with %. */
5347 if (leader_len == 0)
5348 no_leader = TRUE;
5349#endif
5350 if (!(flags & INSCHAR_FORMAT)
5351#ifdef FEAT_COMMENTS
5352 && leader_len == 0
5353#endif
5354 && !has_format_option(FO_WRAP))
5355
5356 {
5357 textwidth = 0;
5358 break;
5359 }
5360 if ((startcol = curwin->w_cursor.col) == 0)
5361 break;
5362
5363 /* find column of textwidth border */
5364 coladvance((colnr_T)textwidth);
5365 wantcol = curwin->w_cursor.col;
5366
5367 curwin->w_cursor.col = startcol - 1;
5368#ifdef FEAT_MBYTE
5369 /* Correct cursor for multi-byte character. */
5370 if (has_mbyte)
5371 mb_adjust_cursor();
5372#endif
5373 foundcol = 0;
5374
5375 /*
5376 * Find position to break at.
5377 * Stop at first entered white when 'formatoptions' has 'v'
5378 */
5379 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5380 || curwin->w_cursor.lnum != Insstart.lnum
5381 || curwin->w_cursor.col >= Insstart.col)
5382 {
5383 cc = gchar_cursor();
5384 if (WHITECHAR(cc))
5385 {
5386 /* remember position of blank just before text */
5387 end_foundcol = curwin->w_cursor.col;
5388
5389 /* find start of sequence of blanks */
5390 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5391 {
5392 dec_cursor();
5393 cc = gchar_cursor();
5394 }
5395 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5396 break; /* only spaces in front of text */
5397#ifdef FEAT_COMMENTS
5398 /* Don't break until after the comment leader */
5399 if (curwin->w_cursor.col < leader_len)
5400 break;
5401#endif
5402 if (has_format_option(FO_ONE_LETTER))
5403 {
5404 /* do not break after one-letter words */
5405 if (curwin->w_cursor.col == 0)
5406 break; /* one-letter word at begin */
5407
5408 col = curwin->w_cursor.col;
5409 dec_cursor();
5410 cc = gchar_cursor();
5411
5412 if (WHITECHAR(cc))
5413 continue; /* one-letter, continue */
5414 curwin->w_cursor.col = col;
5415 }
5416#ifdef FEAT_MBYTE
5417 if (has_mbyte)
5418 foundcol = curwin->w_cursor.col
5419 + (*mb_ptr2len)(ml_get_cursor());
5420 else
5421#endif
5422 foundcol = curwin->w_cursor.col + 1;
5423 if (curwin->w_cursor.col < (colnr_T)wantcol)
5424 break;
5425 }
5426#ifdef FEAT_MBYTE
5427 else if (cc >= 0x100 && fo_multibyte
5428 && curwin->w_cursor.col <= (colnr_T)wantcol)
5429 {
5430 /* Break after or before a multi-byte character. */
5431 foundcol = curwin->w_cursor.col;
5432 if (curwin->w_cursor.col < (colnr_T)wantcol)
5433 foundcol += (*mb_char2len)(cc);
5434 end_foundcol = foundcol;
5435 break;
5436 }
5437#endif
5438 if (curwin->w_cursor.col == 0)
5439 break;
5440 dec_cursor();
5441 }
5442
5443 if (foundcol == 0) /* no spaces, cannot break line */
5444 {
5445 curwin->w_cursor.col = startcol;
5446 break;
5447 }
5448
5449 /* Going to break the line, remove any "$" now. */
5450 undisplay_dollar();
5451
5452 /*
5453 * Offset between cursor position and line break is used by replace
5454 * stack functions. VREPLACE does not use this, and backspaces
5455 * over the text instead.
5456 */
5457#ifdef FEAT_VREPLACE
5458 if (State & VREPLACE_FLAG)
5459 orig_col = startcol; /* Will start backspacing from here */
5460 else
5461#endif
5462 replace_offset = startcol - end_foundcol - 1;
5463
5464 /*
5465 * adjust startcol for spaces that will be deleted and
5466 * characters that will remain on top line
5467 */
5468 curwin->w_cursor.col = foundcol;
5469 while (cc = gchar_cursor(), WHITECHAR(cc))
5470 inc_cursor();
5471 startcol -= curwin->w_cursor.col;
5472 if (startcol < 0)
5473 startcol = 0;
5474
5475#ifdef FEAT_VREPLACE
5476 if (State & VREPLACE_FLAG)
5477 {
5478 /*
5479 * In VREPLACE mode, we will backspace over the text to be
5480 * wrapped, so save a copy now to put on the next line.
5481 */
5482 saved_text = vim_strsave(ml_get_cursor());
5483 curwin->w_cursor.col = orig_col;
5484 if (saved_text == NULL)
5485 break; /* Can't do it, out of memory */
5486 saved_text[startcol] = NUL;
5487
5488 /* Backspace over characters that will move to the next line */
5489 if (!fo_white_par)
5490 backspace_until_column(foundcol);
5491 }
5492 else
5493#endif
5494 {
5495 /* put cursor after pos. to break line */
5496 if (!fo_white_par)
5497 curwin->w_cursor.col = foundcol;
5498 }
5499
5500 /*
5501 * Split the line just before the margin.
5502 * Only insert/delete lines, but don't really redraw the window.
5503 */
5504 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
5505 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
5506#ifdef FEAT_COMMENTS
5507 + (do_comments ? OPENLINE_DO_COM : 0)
5508#endif
5509 , old_indent);
5510 old_indent = 0;
5511
5512 replace_offset = 0;
5513 if (first_line)
5514 {
5515 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
5516 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
5517 if (second_indent >= 0)
5518 {
5519#ifdef FEAT_VREPLACE
5520 if (State & VREPLACE_FLAG)
5521 change_indent(INDENT_SET, second_indent, FALSE, NUL);
5522 else
5523#endif
5524 (void)set_indent(second_indent, SIN_CHANGED);
5525 }
5526 first_line = FALSE;
5527 }
5528
5529#ifdef FEAT_VREPLACE
5530 if (State & VREPLACE_FLAG)
5531 {
5532 /*
5533 * In VREPLACE mode we have backspaced over the text to be
5534 * moved, now we re-insert it into the new line.
5535 */
5536 ins_bytes(saved_text);
5537 vim_free(saved_text);
5538 }
5539 else
5540#endif
5541 {
5542 /*
5543 * Check if cursor is not past the NUL off the line, cindent
5544 * may have added or removed indent.
5545 */
5546 curwin->w_cursor.col += startcol;
5547 len = (colnr_T)STRLEN(ml_get_curline());
5548 if (curwin->w_cursor.col > len)
5549 curwin->w_cursor.col = len;
5550 }
5551
5552 haveto_redraw = TRUE;
5553#ifdef FEAT_CINDENT
5554 can_cindent = TRUE;
5555#endif
5556 /* moved the cursor, don't autoindent or cindent now */
5557 did_ai = FALSE;
5558#ifdef FEAT_SMARTINDENT
5559 did_si = FALSE;
5560 can_si = FALSE;
5561 can_si_back = FALSE;
5562#endif
5563 line_breakcheck();
5564 }
5565
5566 if (save_char != NUL) /* put back space after cursor */
5567 pchar_cursor(save_char);
5568
5569 if (!format_only && haveto_redraw)
5570 {
5571 update_topline();
5572 redraw_curbuf_later(VALID);
5573 }
5574}
5575
5576/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 * Called after inserting or deleting text: When 'formatoptions' includes the
5578 * 'a' flag format from the current line until the end of the paragraph.
5579 * Keep the cursor at the same position relative to the text.
5580 * The caller must have saved the cursor line for undo, following ones will be
5581 * saved here.
5582 */
5583 void
5584auto_format(trailblank, prev_line)
5585 int trailblank; /* when TRUE also format with trailing blank */
5586 int prev_line; /* may start in previous line */
5587{
5588 pos_T pos;
5589 colnr_T len;
5590 char_u *old;
5591 char_u *new, *pnew;
5592 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005593 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594
5595 if (!has_format_option(FO_AUTO))
5596 return;
5597
5598 pos = curwin->w_cursor;
5599 old = ml_get_curline();
5600
5601 /* may remove added space */
5602 check_auto_format(FALSE);
5603
5604 /* Don't format in Insert mode when the cursor is on a trailing blank, the
5605 * user might insert normal text next. Also skip formatting when "1" is
5606 * in 'formatoptions' and there is a single character before the cursor.
5607 * Otherwise the line would be broken and when typing another non-white
5608 * next they are not joined back together. */
5609 wasatend = (pos.col == STRLEN(old));
5610 if (*old != NUL && !trailblank && wasatend)
5611 {
5612 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005613 cc = gchar_cursor();
5614 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
5615 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005617 cc = gchar_cursor();
5618 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 {
5620 curwin->w_cursor = pos;
5621 return;
5622 }
5623 curwin->w_cursor = pos;
5624 }
5625
5626#ifdef FEAT_COMMENTS
5627 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
5628 * comments. */
5629 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
5630 && get_leader_len(old, NULL, FALSE) == 0)
5631 return;
5632#endif
5633
5634 /*
5635 * May start formatting in a previous line, so that after "x" a word is
5636 * moved to the previous line if it fits there now. Only when this is not
5637 * the start of a paragraph.
5638 */
5639 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
5640 {
5641 --curwin->w_cursor.lnum;
5642 if (u_save_cursor() == FAIL)
5643 return;
5644 }
5645
5646 /*
5647 * Do the formatting and restore the cursor position. "saved_cursor" will
5648 * be adjusted for the text formatting.
5649 */
5650 saved_cursor = pos;
5651 format_lines((linenr_T)-1);
5652 curwin->w_cursor = saved_cursor;
5653 saved_cursor.lnum = 0;
5654
5655 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5656 {
5657 /* "cannot happen" */
5658 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5659 coladvance((colnr_T)MAXCOL);
5660 }
5661 else
5662 check_cursor_col();
5663
5664 /* Insert mode: If the cursor is now after the end of the line while it
5665 * previously wasn't, the line was broken. Because of the rule above we
5666 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
5667 * formatted. */
5668 if (!wasatend && has_format_option(FO_WHITE_PAR))
5669 {
5670 new = ml_get_curline();
5671 len = STRLEN(new);
5672 if (curwin->w_cursor.col == len)
5673 {
5674 pnew = vim_strnsave(new, len + 2);
5675 pnew[len] = ' ';
5676 pnew[len + 1] = NUL;
5677 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
5678 /* remove the space later */
5679 did_add_space = TRUE;
5680 }
5681 else
5682 /* may remove added space */
5683 check_auto_format(FALSE);
5684 }
5685
5686 check_cursor();
5687}
5688
5689/*
5690 * When an extra space was added to continue a paragraph for auto-formatting,
5691 * delete it now. The space must be under the cursor, just after the insert
5692 * position.
5693 */
5694 static void
5695check_auto_format(end_insert)
5696 int end_insert; /* TRUE when ending Insert mode */
5697{
5698 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005699 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700
5701 if (did_add_space)
5702 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005703 cc = gchar_cursor();
5704 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 /* Somehow the space was removed already. */
5706 did_add_space = FALSE;
5707 else
5708 {
5709 if (!end_insert)
5710 {
5711 inc_cursor();
5712 c = gchar_cursor();
5713 dec_cursor();
5714 }
5715 if (c != NUL)
5716 {
5717 /* The space is no longer at the end of the line, delete it. */
5718 del_char(FALSE);
5719 did_add_space = FALSE;
5720 }
5721 }
5722 }
5723}
5724
5725/*
5726 * Find out textwidth to be used for formatting:
5727 * if 'textwidth' option is set, use it
5728 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
5729 * if invalid value, use 0.
5730 * Set default to window width (maximum 79) for "gq" operator.
5731 */
5732 int
5733comp_textwidth(ff)
5734 int ff; /* force formatting (for "Q" command) */
5735{
5736 int textwidth;
5737
5738 textwidth = curbuf->b_p_tw;
5739 if (textwidth == 0 && curbuf->b_p_wm)
5740 {
5741 /* The width is the window width minus 'wrapmargin' minus all the
5742 * things that add to the margin. */
5743 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
5744#ifdef FEAT_CMDWIN
5745 if (cmdwin_type != 0)
5746 textwidth -= 1;
5747#endif
5748#ifdef FEAT_FOLDING
5749 textwidth -= curwin->w_p_fdc;
5750#endif
5751#ifdef FEAT_SIGNS
5752 if (curwin->w_buffer->b_signlist != NULL
5753# ifdef FEAT_NETBEANS_INTG
5754 || usingNetbeans
5755# endif
5756 )
5757 textwidth -= 1;
5758#endif
5759 if (curwin->w_p_nu)
5760 textwidth -= 8;
5761 }
5762 if (textwidth < 0)
5763 textwidth = 0;
5764 if (ff && textwidth == 0)
5765 {
5766 textwidth = W_WIDTH(curwin) - 1;
5767 if (textwidth > 79)
5768 textwidth = 79;
5769 }
5770 return textwidth;
5771}
5772
5773/*
5774 * Put a character in the redo buffer, for when just after a CTRL-V.
5775 */
5776 static void
5777redo_literal(c)
5778 int c;
5779{
5780 char_u buf[10];
5781
5782 /* Only digits need special treatment. Translate them into a string of
5783 * three digits. */
5784 if (VIM_ISDIGIT(c))
5785 {
5786 sprintf((char *)buf, "%03d", c);
5787 AppendToRedobuff(buf);
5788 }
5789 else
5790 AppendCharToRedobuff(c);
5791}
5792
5793/*
5794 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005795 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 */
5797 static void
5798start_arrow(end_insert_pos)
5799 pos_T *end_insert_pos;
5800{
5801 if (!arrow_used) /* something has been inserted */
5802 {
5803 AppendToRedobuff(ESC_STR);
5804 stop_insert(end_insert_pos, FALSE);
5805 arrow_used = TRUE; /* this means we stopped the current insert */
5806 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005807#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005808 check_spell_redraw();
5809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810}
5811
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005812#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005813/*
5814 * If we skipped highlighting word at cursor, do it now.
5815 * It may be skipped again, thus reset spell_redraw_lnum first.
5816 */
5817 static void
5818check_spell_redraw()
5819{
5820 if (spell_redraw_lnum != 0)
5821 {
5822 linenr_T lnum = spell_redraw_lnum;
5823
5824 spell_redraw_lnum = 0;
5825 redrawWinline(lnum, FALSE);
5826 }
5827}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005828
5829/*
5830 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5831 * spelled word, if there is one.
5832 */
5833 static void
5834spell_back_to_badword()
5835{
5836 pos_T tpos = curwin->w_cursor;
5837
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00005838 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005839 if (curwin->w_cursor.col != tpos.col)
5840 start_arrow(&tpos);
5841}
Bram Moolenaar217ad922005-03-20 22:37:15 +00005842#endif
5843
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844/*
5845 * stop_arrow() is called before a change is made in insert mode.
5846 * If an arrow key has been used, start a new insertion.
5847 * Returns FAIL if undo is impossible, shouldn't insert then.
5848 */
5849 int
5850stop_arrow()
5851{
5852 if (arrow_used)
5853 {
5854 if (u_save_cursor() == OK)
5855 {
5856 arrow_used = FALSE;
5857 ins_need_undo = FALSE;
5858 }
5859 Insstart = curwin->w_cursor; /* new insertion starts here */
5860 Insstart_textlen = linetabsize(ml_get_curline());
5861 ai_col = 0;
5862#ifdef FEAT_VREPLACE
5863 if (State & VREPLACE_FLAG)
5864 {
5865 orig_line_count = curbuf->b_ml.ml_line_count;
5866 vr_lines_changed = 1;
5867 }
5868#endif
5869 ResetRedobuff();
5870 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00005871 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 }
5873 else if (ins_need_undo)
5874 {
5875 if (u_save_cursor() == OK)
5876 ins_need_undo = FALSE;
5877 }
5878
5879#ifdef FEAT_FOLDING
5880 /* Always open fold at the cursor line when inserting something. */
5881 foldOpenCursor();
5882#endif
5883
5884 return (arrow_used || ins_need_undo ? FAIL : OK);
5885}
5886
5887/*
5888 * do a few things to stop inserting
5889 */
5890 static void
5891stop_insert(end_insert_pos, esc)
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005892 pos_T *end_insert_pos; /* where insert ended */
5893 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005895 int cc;
5896 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897
5898 stop_redo_ins();
5899 replace_flush(); /* abandon replace stack */
5900
5901 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005902 * Save the inserted text for later redo with ^@ and CTRL-A.
5903 * Don't do it when "restart_edit" was set and nothing was inserted,
5904 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005906 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00005907 if (did_restart_edit == 0 || (ptr != NULL
5908 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005909 {
5910 vim_free(last_insert);
5911 last_insert = ptr;
5912 last_insert_skip = new_insert_skip;
5913 }
5914 else
5915 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916
5917 if (!arrow_used)
5918 {
5919 /* Auto-format now. It may seem strange to do this when stopping an
5920 * insertion (or moving the cursor), but it's required when appending
5921 * a line and having it end in a space. But only do it when something
5922 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005923 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005925 pos_T tpos = curwin->w_cursor;
5926
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 /* When the cursor is at the end of the line after a space the
5928 * formatting will move it to the following word. Avoid that by
5929 * moving the cursor onto the space. */
5930 cc = 'x';
5931 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
5932 {
5933 dec_cursor();
5934 cc = gchar_cursor();
5935 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005936 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 }
5938
5939 auto_format(TRUE, FALSE);
5940
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005941 if (vim_iswhite(cc))
5942 {
5943 if (gchar_cursor() != NUL)
5944 inc_cursor();
5945#ifdef FEAT_VIRTUALEDIT
5946 /* If the cursor is still at the same character, also keep
5947 * the "coladd". */
5948 if (gchar_cursor() == NUL
5949 && curwin->w_cursor.lnum == tpos.lnum
5950 && curwin->w_cursor.col == tpos.col)
5951 curwin->w_cursor.coladd = tpos.coladd;
5952#endif
5953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 }
5955
5956 /* If a space was inserted for auto-formatting, remove it now. */
5957 check_auto_format(TRUE);
5958
5959 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005960 * of the line, and put the cursor back.
5961 * Do this when ESC was used or moving the cursor up/down. */
5962 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
5963 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005965 pos_T tpos = curwin->w_cursor;
5966
5967 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar39f05632006-03-19 22:15:26 +00005968 for (;;)
5969 {
5970 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
5971 --curwin->w_cursor.col;
5972 cc = gchar_cursor();
5973 if (!vim_iswhite(cc))
5974 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 (void)del_char(TRUE);
Bram Moolenaar39f05632006-03-19 22:15:26 +00005976 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005977 if (curwin->w_cursor.lnum != tpos.lnum)
5978 curwin->w_cursor = tpos;
5979 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 ++curwin->w_cursor.col; /* put cursor back on the NUL */
5981
5982#ifdef FEAT_VISUAL
5983 /* <C-S-Right> may have started Visual mode, adjust the position for
5984 * deleted characters. */
5985 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
5986 {
5987 cc = STRLEN(ml_get_curline());
5988 if (VIsual.col > (colnr_T)cc)
5989 {
5990 VIsual.col = cc;
5991# ifdef FEAT_VIRTUALEDIT
5992 VIsual.coladd = 0;
5993# endif
5994 }
5995 }
5996#endif
5997 }
5998 }
5999 did_ai = FALSE;
6000#ifdef FEAT_SMARTINDENT
6001 did_si = FALSE;
6002 can_si = FALSE;
6003 can_si_back = FALSE;
6004#endif
6005
6006 /* set '[ and '] to the inserted text */
6007 curbuf->b_op_start = Insstart;
6008 curbuf->b_op_end = *end_insert_pos;
6009}
6010
6011/*
6012 * Set the last inserted text to a single character.
6013 * Used for the replace command.
6014 */
6015 void
6016set_last_insert(c)
6017 int c;
6018{
6019 char_u *s;
6020
6021 vim_free(last_insert);
6022#ifdef FEAT_MBYTE
6023 last_insert = alloc(MB_MAXBYTES * 3 + 5);
6024#else
6025 last_insert = alloc(6);
6026#endif
6027 if (last_insert != NULL)
6028 {
6029 s = last_insert;
6030 /* Use the CTRL-V only when entering a special char */
6031 if (c < ' ' || c == DEL)
6032 *s++ = Ctrl_V;
6033 s = add_char2buf(c, s);
6034 *s++ = ESC;
6035 *s++ = NUL;
6036 last_insert_skip = 0;
6037 }
6038}
6039
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006040#if defined(EXITFREE) || defined(PROTO)
6041 void
6042free_last_insert()
6043{
6044 vim_free(last_insert);
6045 last_insert = NULL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006046 vim_free(compl_orig_text);
6047 compl_orig_text = NULL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006048}
6049#endif
6050
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051/*
6052 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6053 * and CSI. Handle multi-byte characters.
6054 * Returns a pointer to after the added bytes.
6055 */
6056 char_u *
6057add_char2buf(c, s)
6058 int c;
6059 char_u *s;
6060{
6061#ifdef FEAT_MBYTE
6062 char_u temp[MB_MAXBYTES];
6063 int i;
6064 int len;
6065
6066 len = (*mb_char2bytes)(c, temp);
6067 for (i = 0; i < len; ++i)
6068 {
6069 c = temp[i];
6070#endif
6071 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6072 if (c == K_SPECIAL)
6073 {
6074 *s++ = K_SPECIAL;
6075 *s++ = KS_SPECIAL;
6076 *s++ = KE_FILLER;
6077 }
6078#ifdef FEAT_GUI
6079 else if (c == CSI)
6080 {
6081 *s++ = CSI;
6082 *s++ = KS_EXTRA;
6083 *s++ = (int)KE_CSI;
6084 }
6085#endif
6086 else
6087 *s++ = c;
6088#ifdef FEAT_MBYTE
6089 }
6090#endif
6091 return s;
6092}
6093
6094/*
6095 * move cursor to start of line
6096 * if flags & BL_WHITE move to first non-white
6097 * if flags & BL_SOL move to first non-white if startofline is set,
6098 * otherwise keep "curswant" column
6099 * if flags & BL_FIX don't leave the cursor on a NUL.
6100 */
6101 void
6102beginline(flags)
6103 int flags;
6104{
6105 if ((flags & BL_SOL) && !p_sol)
6106 coladvance(curwin->w_curswant);
6107 else
6108 {
6109 curwin->w_cursor.col = 0;
6110#ifdef FEAT_VIRTUALEDIT
6111 curwin->w_cursor.coladd = 0;
6112#endif
6113
6114 if (flags & (BL_WHITE | BL_SOL))
6115 {
6116 char_u *ptr;
6117
6118 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6119 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6120 ++curwin->w_cursor.col;
6121 }
6122 curwin->w_set_curswant = TRUE;
6123 }
6124}
6125
6126/*
6127 * oneright oneleft cursor_down cursor_up
6128 *
6129 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006130 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 * Return OK when successful, FAIL when we hit a line of file boundary.
6132 */
6133
6134 int
6135oneright()
6136{
6137 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139
6140#ifdef FEAT_VIRTUALEDIT
6141 if (virtual_active())
6142 {
6143 pos_T prevpos = curwin->w_cursor;
6144
6145 /* Adjust for multi-wide char (excluding TAB) */
6146 ptr = ml_get_cursor();
6147 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006148# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006150# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006152# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 ))
6154 ? ptr2cells(ptr) : 1));
6155 curwin->w_set_curswant = TRUE;
6156 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6157 return (prevpos.col != curwin->w_cursor.col
6158 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
6159 }
6160#endif
6161
6162 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006163 if (*ptr == NUL)
6164 return FAIL; /* already at the very end */
6165
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006167 if (has_mbyte)
6168 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 else
6170#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006171 l = 1;
6172
6173 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6174 * contains "onemore". */
6175 if (ptr[l] == NUL
6176#ifdef FEAT_VIRTUALEDIT
6177 && (ve_flags & VE_ONEMORE) == 0
6178#endif
6179 )
6180 return FAIL;
6181 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182
6183 curwin->w_set_curswant = TRUE;
6184 return OK;
6185}
6186
6187 int
6188oneleft()
6189{
6190#ifdef FEAT_VIRTUALEDIT
6191 if (virtual_active())
6192 {
6193 int width;
6194 int v = getviscol();
6195
6196 if (v == 0)
6197 return FAIL;
6198
6199# ifdef FEAT_LINEBREAK
6200 /* We might get stuck on 'showbreak', skip over it. */
6201 width = 1;
6202 for (;;)
6203 {
6204 coladvance(v - width);
6205 /* getviscol() is slow, skip it when 'showbreak' is empty and
6206 * there are no multi-byte characters */
6207 if ((*p_sbr == NUL
6208# ifdef FEAT_MBYTE
6209 && !has_mbyte
6210# endif
6211 ) || getviscol() < v)
6212 break;
6213 ++width;
6214 }
6215# else
6216 coladvance(v - 1);
6217# endif
6218
6219 if (curwin->w_cursor.coladd == 1)
6220 {
6221 char_u *ptr;
6222
6223 /* Adjust for multi-wide char (not a TAB) */
6224 ptr = ml_get_cursor();
6225 if (*ptr != TAB && vim_isprintc(
6226# ifdef FEAT_MBYTE
6227 (*mb_ptr2char)(ptr)
6228# else
6229 *ptr
6230# endif
6231 ) && ptr2cells(ptr) > 1)
6232 curwin->w_cursor.coladd = 0;
6233 }
6234
6235 curwin->w_set_curswant = TRUE;
6236 return OK;
6237 }
6238#endif
6239
6240 if (curwin->w_cursor.col == 0)
6241 return FAIL;
6242
6243 curwin->w_set_curswant = TRUE;
6244 --curwin->w_cursor.col;
6245
6246#ifdef FEAT_MBYTE
6247 /* if the character on the left of the current cursor is a multi-byte
6248 * character, move to its first byte */
6249 if (has_mbyte)
6250 mb_adjust_cursor();
6251#endif
6252 return OK;
6253}
6254
6255 int
6256cursor_up(n, upd_topline)
6257 long n;
6258 int upd_topline; /* When TRUE: update topline */
6259{
6260 linenr_T lnum;
6261
6262 if (n > 0)
6263 {
6264 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006265 /* This fails if the cursor is already in the first line or the count
6266 * is larger than the line number and '-' is in 'cpoptions' */
6267 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 return FAIL;
6269 if (n >= lnum)
6270 lnum = 1;
6271 else
6272#ifdef FEAT_FOLDING
6273 if (hasAnyFolding(curwin))
6274 {
6275 /*
6276 * Count each sequence of folded lines as one logical line.
6277 */
6278 /* go to the the start of the current fold */
6279 (void)hasFolding(lnum, &lnum, NULL);
6280
6281 while (n--)
6282 {
6283 /* move up one line */
6284 --lnum;
6285 if (lnum <= 1)
6286 break;
6287 /* If we entered a fold, move to the beginning, unless in
6288 * Insert mode or when 'foldopen' contains "all": it will open
6289 * in a moment. */
6290 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6291 (void)hasFolding(lnum, &lnum, NULL);
6292 }
6293 if (lnum < 1)
6294 lnum = 1;
6295 }
6296 else
6297#endif
6298 lnum -= n;
6299 curwin->w_cursor.lnum = lnum;
6300 }
6301
6302 /* try to advance to the column we want to be at */
6303 coladvance(curwin->w_curswant);
6304
6305 if (upd_topline)
6306 update_topline(); /* make sure curwin->w_topline is valid */
6307
6308 return OK;
6309}
6310
6311/*
6312 * Cursor down a number of logical lines.
6313 */
6314 int
6315cursor_down(n, upd_topline)
6316 long n;
6317 int upd_topline; /* When TRUE: update topline */
6318{
6319 linenr_T lnum;
6320
6321 if (n > 0)
6322 {
6323 lnum = curwin->w_cursor.lnum;
6324#ifdef FEAT_FOLDING
6325 /* Move to last line of fold, will fail if it's the end-of-file. */
6326 (void)hasFolding(lnum, NULL, &lnum);
6327#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006328 /* This fails if the cursor is already in the last line or would move
6329 * beyound the last line and '-' is in 'cpoptions' */
6330 if (lnum >= curbuf->b_ml.ml_line_count
6331 || (lnum + n > curbuf->b_ml.ml_line_count
6332 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 return FAIL;
6334 if (lnum + n >= curbuf->b_ml.ml_line_count)
6335 lnum = curbuf->b_ml.ml_line_count;
6336 else
6337#ifdef FEAT_FOLDING
6338 if (hasAnyFolding(curwin))
6339 {
6340 linenr_T last;
6341
6342 /* count each sequence of folded lines as one logical line */
6343 while (n--)
6344 {
6345 if (hasFolding(lnum, NULL, &last))
6346 lnum = last + 1;
6347 else
6348 ++lnum;
6349 if (lnum >= curbuf->b_ml.ml_line_count)
6350 break;
6351 }
6352 if (lnum > curbuf->b_ml.ml_line_count)
6353 lnum = curbuf->b_ml.ml_line_count;
6354 }
6355 else
6356#endif
6357 lnum += n;
6358 curwin->w_cursor.lnum = lnum;
6359 }
6360
6361 /* try to advance to the column we want to be at */
6362 coladvance(curwin->w_curswant);
6363
6364 if (upd_topline)
6365 update_topline(); /* make sure curwin->w_topline is valid */
6366
6367 return OK;
6368}
6369
6370/*
6371 * Stuff the last inserted text in the read buffer.
6372 * Last_insert actually is a copy of the redo buffer, so we
6373 * first have to remove the command.
6374 */
6375 int
6376stuff_inserted(c, count, no_esc)
6377 int c; /* Command character to be inserted */
6378 long count; /* Repeat this many times */
6379 int no_esc; /* Don't add an ESC at the end */
6380{
6381 char_u *esc_ptr;
6382 char_u *ptr;
6383 char_u *last_ptr;
6384 char_u last = NUL;
6385
6386 ptr = get_last_insert();
6387 if (ptr == NULL)
6388 {
6389 EMSG(_(e_noinstext));
6390 return FAIL;
6391 }
6392
6393 /* may want to stuff the command character, to start Insert mode */
6394 if (c != NUL)
6395 stuffcharReadbuff(c);
6396 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
6397 *esc_ptr = NUL; /* remove the ESC */
6398
6399 /* when the last char is either "0" or "^" it will be quoted if no ESC
6400 * comes after it OR if it will inserted more than once and "ptr"
6401 * starts with ^D. -- Acevedo
6402 */
6403 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
6404 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
6405 && (no_esc || (*ptr == Ctrl_D && count > 1)))
6406 {
6407 last = *last_ptr;
6408 *last_ptr = NUL;
6409 }
6410
6411 do
6412 {
6413 stuffReadbuff(ptr);
6414 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6415 if (last)
6416 stuffReadbuff((char_u *)(last == '0'
6417 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
6418 : IF_EB("\026^", CTRL_V_STR "^")));
6419 }
6420 while (--count > 0);
6421
6422 if (last)
6423 *last_ptr = last;
6424
6425 if (esc_ptr != NULL)
6426 *esc_ptr = ESC; /* put the ESC back */
6427
6428 /* may want to stuff a trailing ESC, to get out of Insert mode */
6429 if (!no_esc)
6430 stuffcharReadbuff(ESC);
6431
6432 return OK;
6433}
6434
6435 char_u *
6436get_last_insert()
6437{
6438 if (last_insert == NULL)
6439 return NULL;
6440 return last_insert + last_insert_skip;
6441}
6442
6443/*
6444 * Get last inserted string, and remove trailing <Esc>.
6445 * Returns pointer to allocated memory (must be freed) or NULL.
6446 */
6447 char_u *
6448get_last_insert_save()
6449{
6450 char_u *s;
6451 int len;
6452
6453 if (last_insert == NULL)
6454 return NULL;
6455 s = vim_strsave(last_insert + last_insert_skip);
6456 if (s != NULL)
6457 {
6458 len = (int)STRLEN(s);
6459 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
6460 s[len - 1] = NUL;
6461 }
6462 return s;
6463}
6464
6465/*
6466 * Check the word in front of the cursor for an abbreviation.
6467 * Called when the non-id character "c" has been entered.
6468 * When an abbreviation is recognized it is removed from the text and
6469 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
6470 */
6471 static int
6472echeck_abbr(c)
6473 int c;
6474{
6475 /* Don't check for abbreviation in paste mode, when disabled and just
6476 * after moving around with cursor keys. */
6477 if (p_paste || no_abbr || arrow_used)
6478 return FALSE;
6479
6480 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
6481 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
6482}
6483
6484/*
6485 * replace-stack functions
6486 *
6487 * When replacing characters, the replaced characters are remembered for each
6488 * new character. This is used to re-insert the old text when backspacing.
6489 *
6490 * There is a NUL headed list of characters for each character that is
6491 * currently in the file after the insertion point. When BS is used, one NUL
6492 * headed list is put back for the deleted character.
6493 *
6494 * For a newline, there are two NUL headed lists. One contains the characters
6495 * that the NL replaced. The extra one stores the characters after the cursor
6496 * that were deleted (always white space).
6497 *
6498 * Replace_offset is normally 0, in which case replace_push will add a new
6499 * character at the end of the stack. If replace_offset is not 0, that many
6500 * characters will be left on the stack above the newly inserted character.
6501 */
6502
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00006503static char_u *replace_stack = NULL;
6504static long replace_stack_nr = 0; /* next entry in replace stack */
6505static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506
6507 void
6508replace_push(c)
6509 int c; /* character that is replaced (NUL is none) */
6510{
6511 char_u *p;
6512
6513 if (replace_stack_nr < replace_offset) /* nothing to do */
6514 return;
6515 if (replace_stack_len <= replace_stack_nr)
6516 {
6517 replace_stack_len += 50;
6518 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
6519 if (p == NULL) /* out of memory */
6520 {
6521 replace_stack_len -= 50;
6522 return;
6523 }
6524 if (replace_stack != NULL)
6525 {
6526 mch_memmove(p, replace_stack,
6527 (size_t)(replace_stack_nr * sizeof(char_u)));
6528 vim_free(replace_stack);
6529 }
6530 replace_stack = p;
6531 }
6532 p = replace_stack + replace_stack_nr - replace_offset;
6533 if (replace_offset)
6534 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
6535 *p = c;
6536 ++replace_stack_nr;
6537}
6538
6539/*
6540 * call replace_push(c) with replace_offset set to the first NUL.
6541 */
6542 static void
6543replace_push_off(c)
6544 int c;
6545{
6546 char_u *p;
6547
6548 p = replace_stack + replace_stack_nr;
6549 for (replace_offset = 1; replace_offset < replace_stack_nr;
6550 ++replace_offset)
6551 if (*--p == NUL)
6552 break;
6553 replace_push(c);
6554 replace_offset = 0;
6555}
6556
6557/*
6558 * Pop one item from the replace stack.
6559 * return -1 if stack empty
6560 * return replaced character or NUL otherwise
6561 */
6562 static int
6563replace_pop()
6564{
6565 if (replace_stack_nr == 0)
6566 return -1;
6567 return (int)replace_stack[--replace_stack_nr];
6568}
6569
6570/*
6571 * Join the top two items on the replace stack. This removes to "off"'th NUL
6572 * encountered.
6573 */
6574 static void
6575replace_join(off)
6576 int off; /* offset for which NUL to remove */
6577{
6578 int i;
6579
6580 for (i = replace_stack_nr; --i >= 0; )
6581 if (replace_stack[i] == NUL && off-- <= 0)
6582 {
6583 --replace_stack_nr;
6584 mch_memmove(replace_stack + i, replace_stack + i + 1,
6585 (size_t)(replace_stack_nr - i));
6586 return;
6587 }
6588}
6589
6590/*
6591 * Pop bytes from the replace stack until a NUL is found, and insert them
6592 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
6593 */
6594 static void
6595replace_pop_ins()
6596{
6597 int cc;
6598 int oldState = State;
6599
6600 State = NORMAL; /* don't want REPLACE here */
6601 while ((cc = replace_pop()) > 0)
6602 {
6603#ifdef FEAT_MBYTE
6604 mb_replace_pop_ins(cc);
6605#else
6606 ins_char(cc);
6607#endif
6608 dec_cursor();
6609 }
6610 State = oldState;
6611}
6612
6613#ifdef FEAT_MBYTE
6614/*
6615 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
6616 * indicates a multi-byte char, pop the other bytes too.
6617 */
6618 static void
6619mb_replace_pop_ins(cc)
6620 int cc;
6621{
6622 int n;
6623 char_u buf[MB_MAXBYTES];
6624 int i;
6625 int c;
6626
6627 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
6628 {
6629 buf[0] = cc;
6630 for (i = 1; i < n; ++i)
6631 buf[i] = replace_pop();
6632 ins_bytes_len(buf, n);
6633 }
6634 else
6635 ins_char(cc);
6636
6637 if (enc_utf8)
6638 /* Handle composing chars. */
6639 for (;;)
6640 {
6641 c = replace_pop();
6642 if (c == -1) /* stack empty */
6643 break;
6644 if ((n = MB_BYTE2LEN(c)) == 1)
6645 {
6646 /* Not a multi-byte char, put it back. */
6647 replace_push(c);
6648 break;
6649 }
6650 else
6651 {
6652 buf[0] = c;
6653 for (i = 1; i < n; ++i)
6654 buf[i] = replace_pop();
6655 if (utf_iscomposing(utf_ptr2char(buf)))
6656 ins_bytes_len(buf, n);
6657 else
6658 {
6659 /* Not a composing char, put it back. */
6660 for (i = n - 1; i >= 0; --i)
6661 replace_push(buf[i]);
6662 break;
6663 }
6664 }
6665 }
6666}
6667#endif
6668
6669/*
6670 * make the replace stack empty
6671 * (called when exiting replace mode)
6672 */
6673 static void
6674replace_flush()
6675{
6676 vim_free(replace_stack);
6677 replace_stack = NULL;
6678 replace_stack_len = 0;
6679 replace_stack_nr = 0;
6680}
6681
6682/*
6683 * Handle doing a BS for one character.
6684 * cc < 0: replace stack empty, just move cursor
6685 * cc == 0: character was inserted, delete it
6686 * cc > 0: character was replaced, put cc (first byte of original char) back
6687 * and check for more characters to be put back
6688 */
6689 static void
6690replace_do_bs()
6691{
6692 int cc;
6693#ifdef FEAT_VREPLACE
6694 int orig_len = 0;
6695 int ins_len;
6696 int orig_vcols = 0;
6697 colnr_T start_vcol;
6698 char_u *p;
6699 int i;
6700 int vcol;
6701#endif
6702
6703 cc = replace_pop();
6704 if (cc > 0)
6705 {
6706#ifdef FEAT_VREPLACE
6707 if (State & VREPLACE_FLAG)
6708 {
6709 /* Get the number of screen cells used by the character we are
6710 * going to delete. */
6711 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
6712 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
6713 }
6714#endif
6715#ifdef FEAT_MBYTE
6716 if (has_mbyte)
6717 {
6718 del_char(FALSE);
6719# ifdef FEAT_VREPLACE
6720 if (State & VREPLACE_FLAG)
6721 orig_len = STRLEN(ml_get_cursor());
6722# endif
6723 replace_push(cc);
6724 }
6725 else
6726#endif
6727 {
6728 pchar_cursor(cc);
6729#ifdef FEAT_VREPLACE
6730 if (State & VREPLACE_FLAG)
6731 orig_len = STRLEN(ml_get_cursor()) - 1;
6732#endif
6733 }
6734 replace_pop_ins();
6735
6736#ifdef FEAT_VREPLACE
6737 if (State & VREPLACE_FLAG)
6738 {
6739 /* Get the number of screen cells used by the inserted characters */
6740 p = ml_get_cursor();
6741 ins_len = STRLEN(p) - orig_len;
6742 vcol = start_vcol;
6743 for (i = 0; i < ins_len; ++i)
6744 {
6745 vcol += chartabsize(p + i, vcol);
6746#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006747 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748#endif
6749 }
6750 vcol -= start_vcol;
6751
6752 /* Delete spaces that were inserted after the cursor to keep the
6753 * text aligned. */
6754 curwin->w_cursor.col += ins_len;
6755 while (vcol > orig_vcols && gchar_cursor() == ' ')
6756 {
6757 del_char(FALSE);
6758 ++orig_vcols;
6759 }
6760 curwin->w_cursor.col -= ins_len;
6761 }
6762#endif
6763
6764 /* mark the buffer as changed and prepare for displaying */
6765 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
6766 }
6767 else if (cc == 0)
6768 (void)del_char(FALSE);
6769}
6770
6771#ifdef FEAT_CINDENT
6772/*
6773 * Return TRUE if C-indenting is on.
6774 */
6775 static int
6776cindent_on()
6777{
6778 return (!p_paste && (curbuf->b_p_cin
6779# ifdef FEAT_EVAL
6780 || *curbuf->b_p_inde != NUL
6781# endif
6782 ));
6783}
6784#endif
6785
6786#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
6787/*
6788 * Re-indent the current line, based on the current contents of it and the
6789 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
6790 * confused what all the part that handles Control-T is doing that I'm not.
6791 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
6792 */
6793
6794 void
6795fixthisline(get_the_indent)
6796 int (*get_the_indent) __ARGS((void));
6797{
6798 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
6799 if (linewhite(curwin->w_cursor.lnum))
6800 did_ai = TRUE; /* delete the indent if the line stays empty */
6801}
6802
6803 void
6804fix_indent()
6805{
6806 if (p_paste)
6807 return;
6808# ifdef FEAT_LISP
6809 if (curbuf->b_p_lisp && curbuf->b_p_ai)
6810 fixthisline(get_lisp_indent);
6811# endif
6812# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
6813 else
6814# endif
6815# ifdef FEAT_CINDENT
6816 if (cindent_on())
6817 do_c_expr_indent();
6818# endif
6819}
6820
6821#endif
6822
6823#ifdef FEAT_CINDENT
6824/*
6825 * return TRUE if 'cinkeys' contains the key "keytyped",
6826 * when == '*': Only if key is preceded with '*' (indent before insert)
6827 * when == '!': Only if key is prededed with '!' (don't insert)
6828 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
6829 *
6830 * "keytyped" can have a few special values:
6831 * KEY_OPEN_FORW
6832 * KEY_OPEN_BACK
6833 * KEY_COMPLETE just finished completion.
6834 *
6835 * If line_is_empty is TRUE accept keys with '0' before them.
6836 */
6837 int
6838in_cinkeys(keytyped, when, line_is_empty)
6839 int keytyped;
6840 int when;
6841 int line_is_empty;
6842{
6843 char_u *look;
6844 int try_match;
6845 int try_match_word;
6846 char_u *p;
6847 char_u *line;
6848 int icase;
6849 int i;
6850
6851#ifdef FEAT_EVAL
6852 if (*curbuf->b_p_inde != NUL)
6853 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
6854 else
6855#endif
6856 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
6857 while (*look)
6858 {
6859 /*
6860 * Find out if we want to try a match with this key, depending on
6861 * 'when' and a '*' or '!' before the key.
6862 */
6863 switch (when)
6864 {
6865 case '*': try_match = (*look == '*'); break;
6866 case '!': try_match = (*look == '!'); break;
6867 default: try_match = (*look != '*'); break;
6868 }
6869 if (*look == '*' || *look == '!')
6870 ++look;
6871
6872 /*
6873 * If there is a '0', only accept a match if the line is empty.
6874 * But may still match when typing last char of a word.
6875 */
6876 if (*look == '0')
6877 {
6878 try_match_word = try_match;
6879 if (!line_is_empty)
6880 try_match = FALSE;
6881 ++look;
6882 }
6883 else
6884 try_match_word = FALSE;
6885
6886 /*
6887 * does it look like a control character?
6888 */
6889 if (*look == '^'
6890#ifdef EBCDIC
6891 && (Ctrl_chr(look[1]) != 0)
6892#else
6893 && look[1] >= '?' && look[1] <= '_'
6894#endif
6895 )
6896 {
6897 if (try_match && keytyped == Ctrl_chr(look[1]))
6898 return TRUE;
6899 look += 2;
6900 }
6901 /*
6902 * 'o' means "o" command, open forward.
6903 * 'O' means "O" command, open backward.
6904 */
6905 else if (*look == 'o')
6906 {
6907 if (try_match && keytyped == KEY_OPEN_FORW)
6908 return TRUE;
6909 ++look;
6910 }
6911 else if (*look == 'O')
6912 {
6913 if (try_match && keytyped == KEY_OPEN_BACK)
6914 return TRUE;
6915 ++look;
6916 }
6917
6918 /*
6919 * 'e' means to check for "else" at start of line and just before the
6920 * cursor.
6921 */
6922 else if (*look == 'e')
6923 {
6924 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
6925 {
6926 p = ml_get_curline();
6927 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
6928 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
6929 return TRUE;
6930 }
6931 ++look;
6932 }
6933
6934 /*
6935 * ':' only causes an indent if it is at the end of a label or case
6936 * statement, or when it was before typing the ':' (to fix
6937 * class::method for C++).
6938 */
6939 else if (*look == ':')
6940 {
6941 if (try_match && keytyped == ':')
6942 {
6943 p = ml_get_curline();
6944 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
6945 return TRUE;
6946 if (curwin->w_cursor.col > 2
6947 && p[curwin->w_cursor.col - 1] == ':'
6948 && p[curwin->w_cursor.col - 2] == ':')
6949 {
6950 p[curwin->w_cursor.col - 1] = ' ';
6951 i = (cin_iscase(p) || cin_isscopedecl(p)
6952 || cin_islabel(30));
6953 p = ml_get_curline();
6954 p[curwin->w_cursor.col - 1] = ':';
6955 if (i)
6956 return TRUE;
6957 }
6958 }
6959 ++look;
6960 }
6961
6962
6963 /*
6964 * Is it a key in <>, maybe?
6965 */
6966 else if (*look == '<')
6967 {
6968 if (try_match)
6969 {
6970 /*
6971 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
6972 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
6973 * >, *, : and ! keys if they really really want to.
6974 */
6975 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
6976 && keytyped == look[1])
6977 return TRUE;
6978
6979 if (keytyped == get_special_key_code(look + 1))
6980 return TRUE;
6981 }
6982 while (*look && *look != '>')
6983 look++;
6984 while (*look == '>')
6985 look++;
6986 }
6987
6988 /*
6989 * Is it a word: "=word"?
6990 */
6991 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
6992 {
6993 ++look;
6994 if (*look == '~')
6995 {
6996 icase = TRUE;
6997 ++look;
6998 }
6999 else
7000 icase = FALSE;
7001 p = vim_strchr(look, ',');
7002 if (p == NULL)
7003 p = look + STRLEN(look);
7004 if ((try_match || try_match_word)
7005 && curwin->w_cursor.col >= (colnr_T)(p - look))
7006 {
7007 int match = FALSE;
7008
7009#ifdef FEAT_INS_EXPAND
7010 if (keytyped == KEY_COMPLETE)
7011 {
7012 char_u *s;
7013
7014 /* Just completed a word, check if it starts with "look".
7015 * search back for the start of a word. */
7016 line = ml_get_curline();
7017# ifdef FEAT_MBYTE
7018 if (has_mbyte)
7019 {
7020 char_u *n;
7021
7022 for (s = line + curwin->w_cursor.col; s > line; s = n)
7023 {
7024 n = mb_prevptr(line, s);
7025 if (!vim_iswordp(n))
7026 break;
7027 }
7028 }
7029 else
7030# endif
7031 for (s = line + curwin->w_cursor.col; s > line; --s)
7032 if (!vim_iswordc(s[-1]))
7033 break;
7034 if (s + (p - look) <= line + curwin->w_cursor.col
7035 && (icase
7036 ? MB_STRNICMP(s, look, p - look)
7037 : STRNCMP(s, look, p - look)) == 0)
7038 match = TRUE;
7039 }
7040 else
7041#endif
7042 /* TODO: multi-byte */
7043 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7044 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7045 {
7046 line = ml_get_cursor();
7047 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7048 || !vim_iswordc(line[-(p - look) - 1]))
7049 && (icase
7050 ? MB_STRNICMP(line - (p - look), look, p - look)
7051 : STRNCMP(line - (p - look), look, p - look))
7052 == 0)
7053 match = TRUE;
7054 }
7055 if (match && try_match_word && !try_match)
7056 {
7057 /* "0=word": Check if there are only blanks before the
7058 * word. */
7059 line = ml_get_curline();
7060 if ((int)(skipwhite(line) - line) !=
7061 (int)(curwin->w_cursor.col - (p - look)))
7062 match = FALSE;
7063 }
7064 if (match)
7065 return TRUE;
7066 }
7067 look = p;
7068 }
7069
7070 /*
7071 * ok, it's a boring generic character.
7072 */
7073 else
7074 {
7075 if (try_match && *look == keytyped)
7076 return TRUE;
7077 ++look;
7078 }
7079
7080 /*
7081 * Skip over ", ".
7082 */
7083 look = skip_to_option_part(look);
7084 }
7085 return FALSE;
7086}
7087#endif /* FEAT_CINDENT */
7088
7089#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7090/*
7091 * Map Hebrew keyboard when in hkmap mode.
7092 */
7093 int
7094hkmap(c)
7095 int c;
7096{
7097 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7098 {
7099 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7100 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7101 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7102 static char_u map[26] =
7103 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7104 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7105 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7106 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7107 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7108 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7109 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7110 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7111 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7112
7113 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7114 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7115 /* '-1'='sofit' */
7116 else if (c == 'x')
7117 return 'X';
7118 else if (c == 'q')
7119 return '\''; /* {geresh}={'} */
7120 else if (c == 246)
7121 return ' '; /* \"o --> ' ' for a german keyboard */
7122 else if (c == 228)
7123 return ' '; /* \"a --> ' ' -- / -- */
7124 else if (c == 252)
7125 return ' '; /* \"u --> ' ' -- / -- */
7126#ifdef EBCDIC
7127 else if (islower(c))
7128#else
7129 /* NOTE: islower() does not do the right thing for us on Linux so we
7130 * do this the same was as 5.7 and previous, so it works correctly on
7131 * all systems. Specifically, the e.g. Delete and Arrow keys are
7132 * munged and won't work if e.g. searching for Hebrew text.
7133 */
7134 else if (c >= 'a' && c <= 'z')
7135#endif
7136 return (int)(map[CharOrdLow(c)] + p_aleph);
7137 else
7138 return c;
7139 }
7140 else
7141 {
7142 switch (c)
7143 {
7144 case '`': return ';';
7145 case '/': return '.';
7146 case '\'': return ',';
7147 case 'q': return '/';
7148 case 'w': return '\'';
7149
7150 /* Hebrew letters - set offset from 'a' */
7151 case ',': c = '{'; break;
7152 case '.': c = 'v'; break;
7153 case ';': c = 't'; break;
7154 default: {
7155 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7156
7157#ifdef EBCDIC
7158 /* see note about islower() above */
7159 if (!islower(c))
7160#else
7161 if (c < 'a' || c > 'z')
7162#endif
7163 return c;
7164 c = str[CharOrdLow(c)];
7165 break;
7166 }
7167 }
7168
7169 return (int)(CharOrdLow(c) + p_aleph);
7170 }
7171}
7172#endif
7173
7174 static void
7175ins_reg()
7176{
7177 int need_redraw = FALSE;
7178 int regname;
7179 int literally = 0;
7180
7181 /*
7182 * If we are going to wait for a character, show a '"'.
7183 */
7184 pc_status = PC_STATUS_UNSET;
7185 if (redrawing() && !char_avail())
7186 {
7187 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007188 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189
7190 edit_putchar('"', TRUE);
7191#ifdef FEAT_CMDL_INFO
7192 add_to_showcmd_c(Ctrl_R);
7193#endif
7194 }
7195
7196#ifdef USE_ON_FLY_SCROLL
7197 dont_scroll = TRUE; /* disallow scrolling here */
7198#endif
7199
7200 /*
7201 * Don't map the register name. This also prevents the mode message to be
7202 * deleted when ESC is hit.
7203 */
7204 ++no_mapping;
7205 regname = safe_vgetc();
7206#ifdef FEAT_LANGMAP
7207 LANGMAP_ADJUST(regname, TRUE);
7208#endif
7209 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
7210 {
7211 /* Get a third key for literal register insertion */
7212 literally = regname;
7213#ifdef FEAT_CMDL_INFO
7214 add_to_showcmd_c(literally);
7215#endif
7216 regname = safe_vgetc();
7217#ifdef FEAT_LANGMAP
7218 LANGMAP_ADJUST(regname, TRUE);
7219#endif
7220 }
7221 --no_mapping;
7222
7223#ifdef FEAT_EVAL
7224 /*
7225 * Don't call u_sync() while getting the expression,
7226 * evaluating it or giving an error message for it!
7227 */
7228 ++no_u_sync;
7229 if (regname == '=')
7230 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007231# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007233# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007235# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 /* Restore the Input Method. */
7237 if (im_on)
7238 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007239# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00007241 if (regname == NUL || !valid_yank_reg(regname, FALSE))
7242 {
7243 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 else
7247 {
7248#endif
7249 if (literally == Ctrl_O || literally == Ctrl_P)
7250 {
7251 /* Append the command to the redo buffer. */
7252 AppendCharToRedobuff(Ctrl_R);
7253 AppendCharToRedobuff(literally);
7254 AppendCharToRedobuff(regname);
7255
7256 do_put(regname, BACKWARD, 1L,
7257 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
7258 }
7259 else if (insert_reg(regname, literally) == FAIL)
7260 {
7261 vim_beep();
7262 need_redraw = TRUE; /* remove the '"' */
7263 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007264 else if (stop_insert_mode)
7265 /* When the '=' register was used and a function was invoked that
7266 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7267 * insert anything, need to remove the '"' */
7268 need_redraw = TRUE;
7269
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270#ifdef FEAT_EVAL
7271 }
7272 --no_u_sync;
7273#endif
7274#ifdef FEAT_CMDL_INFO
7275 clear_showcmd();
7276#endif
7277
7278 /* If the inserted register is empty, we need to remove the '"' */
7279 if (need_redraw || stuff_empty())
7280 edit_unputchar();
7281}
7282
7283/*
7284 * CTRL-G commands in Insert mode.
7285 */
7286 static void
7287ins_ctrl_g()
7288{
7289 int c;
7290
7291#ifdef FEAT_INS_EXPAND
7292 /* Right after CTRL-X the cursor will be after the ruler. */
7293 setcursor();
7294#endif
7295
7296 /*
7297 * Don't map the second key. This also prevents the mode message to be
7298 * deleted when ESC is hit.
7299 */
7300 ++no_mapping;
7301 c = safe_vgetc();
7302 --no_mapping;
7303 switch (c)
7304 {
7305 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7306 case K_UP:
7307 case Ctrl_K:
7308 case 'k': ins_up(TRUE);
7309 break;
7310
7311 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7312 case K_DOWN:
7313 case Ctrl_J:
7314 case 'j': ins_down(TRUE);
7315 break;
7316
7317 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00007318 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007320
7321 /* Need to reset Insstart, esp. because a BS that joins
7322 * aline to the previous one must save for undo. */
7323 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 break;
7325
7326 /* Unknown CTRL-G command, reserved for future expansion. */
7327 default: vim_beep();
7328 }
7329}
7330
7331/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007332 * CTRL-^ in Insert mode.
7333 */
7334 static void
7335ins_ctrl_hat()
7336{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00007337 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007338 {
7339 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7340 if (State & LANGMAP)
7341 {
7342 curbuf->b_p_iminsert = B_IMODE_NONE;
7343 State &= ~LANGMAP;
7344 }
7345 else
7346 {
7347 curbuf->b_p_iminsert = B_IMODE_LMAP;
7348 State |= LANGMAP;
7349#ifdef USE_IM_CONTROL
7350 im_set_active(FALSE);
7351#endif
7352 }
7353 }
7354#ifdef USE_IM_CONTROL
7355 else
7356 {
7357 /* There are no ":lmap" mappings, toggle IM */
7358 if (im_get_status())
7359 {
7360 curbuf->b_p_iminsert = B_IMODE_NONE;
7361 im_set_active(FALSE);
7362 }
7363 else
7364 {
7365 curbuf->b_p_iminsert = B_IMODE_IM;
7366 State &= ~LANGMAP;
7367 im_set_active(TRUE);
7368 }
7369 }
7370#endif
7371 set_iminsert_global();
7372 showmode();
7373#ifdef FEAT_GUI
7374 /* may show different cursor shape or color */
7375 if (gui.in_use)
7376 gui_update_cursor(TRUE, FALSE);
7377#endif
7378#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7379 /* Show/unshow value of 'keymap' in status lines. */
7380 status_redraw_curbuf();
7381#endif
7382}
7383
7384/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 * Handle ESC in insert mode.
7386 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7387 * insert.
7388 */
7389 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00007390ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 long *count;
7392 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00007393 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394{
7395 int temp;
7396 static int disabled_redraw = FALSE;
7397
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007398#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007399 check_spell_redraw();
7400#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401#if defined(FEAT_HANGULIN)
7402# if defined(ESC_CHG_TO_ENG_MODE)
7403 hangul_input_state_set(0);
7404# endif
7405 if (composing_hangul)
7406 {
7407 push_raw_key(composing_hangul_buffer, 2);
7408 composing_hangul = 0;
7409 }
7410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411
7412 temp = curwin->w_cursor.col;
7413 if (disabled_redraw)
7414 {
7415 --RedrawingDisabled;
7416 disabled_redraw = FALSE;
7417 }
7418 if (!arrow_used)
7419 {
7420 /*
7421 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00007422 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7423 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 */
7425 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00007426 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427
7428 /*
7429 * Repeating insert may take a long time. Check for
7430 * interrupt now and then.
7431 */
7432 if (*count > 0)
7433 {
7434 line_breakcheck();
7435 if (got_int)
7436 *count = 0;
7437 }
7438
7439 if (--*count > 0) /* repeat what was typed */
7440 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007441 /* Vi repeats the insert without replacing characters. */
7442 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
7443 State &= ~REPLACE_FLAG;
7444
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 (void)start_redo_ins();
7446 if (cmdchar == 'r' || cmdchar == 'v')
7447 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
7448 ++RedrawingDisabled;
7449 disabled_redraw = TRUE;
7450 return FALSE; /* repeat the insert */
7451 }
7452 stop_insert(&curwin->w_cursor, TRUE);
7453 undisplay_dollar();
7454 }
7455
7456 /* When an autoindent was removed, curswant stays after the
7457 * indent */
7458 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
7459 curwin->w_set_curswant = TRUE;
7460
7461 /* Remember the last Insert position in the '^ mark. */
7462 if (!cmdmod.keepjumps)
7463 curbuf->b_last_insert = curwin->w_cursor;
7464
7465 /*
7466 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00007467 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00007469 if (!nomove
7470 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471#ifdef FEAT_VIRTUALEDIT
7472 || curwin->w_cursor.coladd > 0
7473#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007474 )
7475 && (restart_edit == NUL
7476 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00007478 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007480 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481#ifdef FEAT_RIGHTLEFT
7482 && !revins_on
7483#endif
7484 )
7485 {
7486#ifdef FEAT_VIRTUALEDIT
7487 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
7488 {
7489 oneleft();
7490 if (restart_edit != NUL)
7491 ++curwin->w_cursor.coladd;
7492 }
7493 else
7494#endif
7495 {
7496 --curwin->w_cursor.col;
7497#ifdef FEAT_MBYTE
7498 /* Correct cursor for multi-byte character. */
7499 if (has_mbyte)
7500 mb_adjust_cursor();
7501#endif
7502 }
7503 }
7504
7505#ifdef USE_IM_CONTROL
7506 /* Disable IM to allow typing English directly for Normal mode commands.
7507 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
7508 * well). */
7509 if (!(State & LANGMAP))
7510 im_save_status(&curbuf->b_p_iminsert);
7511 im_set_active(FALSE);
7512#endif
7513
7514 State = NORMAL;
7515 /* need to position cursor again (e.g. when on a TAB ) */
7516 changed_cline_bef_curs();
7517
7518#ifdef FEAT_MOUSE
7519 setmouse();
7520#endif
7521#ifdef CURSOR_SHAPE
7522 ui_cursor_shape(); /* may show different cursor shape */
7523#endif
7524
7525 /*
7526 * When recording or for CTRL-O, need to display the new mode.
7527 * Otherwise remove the mode message.
7528 */
7529 if (Recording || restart_edit != NUL)
7530 showmode();
7531 else if (p_smd)
7532 MSG("");
7533
7534 return TRUE; /* exit Insert mode */
7535}
7536
7537#ifdef FEAT_RIGHTLEFT
7538/*
7539 * Toggle language: hkmap and revins_on.
7540 * Move to end of reverse inserted text.
7541 */
7542 static void
7543ins_ctrl_()
7544{
7545 if (revins_on && revins_chars && revins_scol >= 0)
7546 {
7547 while (gchar_cursor() != NUL && revins_chars--)
7548 ++curwin->w_cursor.col;
7549 }
7550 p_ri = !p_ri;
7551 revins_on = (State == INSERT && p_ri);
7552 if (revins_on)
7553 {
7554 revins_scol = curwin->w_cursor.col;
7555 revins_legal++;
7556 revins_chars = 0;
7557 undisplay_dollar();
7558 }
7559 else
7560 revins_scol = -1;
7561#ifdef FEAT_FKMAP
7562 if (p_altkeymap)
7563 {
7564 /*
7565 * to be consistent also for redo command, using '.'
7566 * set arrow_used to true and stop it - causing to redo
7567 * characters entered in one mode (normal/reverse insert).
7568 */
7569 arrow_used = TRUE;
7570 (void)stop_arrow();
7571 p_fkmap = curwin->w_p_rl ^ p_ri;
7572 if (p_fkmap && p_ri)
7573 State = INSERT;
7574 }
7575 else
7576#endif
7577 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
7578 showmode();
7579}
7580#endif
7581
7582#ifdef FEAT_VISUAL
7583/*
7584 * If 'keymodel' contains "startsel", may start selection.
7585 * Returns TRUE when a CTRL-O and other keys stuffed.
7586 */
7587 static int
7588ins_start_select(c)
7589 int c;
7590{
7591 if (km_startsel)
7592 switch (c)
7593 {
7594 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 case K_PAGEUP:
7597 case K_KPAGEUP:
7598 case K_PAGEDOWN:
7599 case K_KPAGEDOWN:
7600# ifdef MACOS
7601 case K_LEFT:
7602 case K_RIGHT:
7603 case K_UP:
7604 case K_DOWN:
7605 case K_END:
7606 case K_HOME:
7607# endif
7608 if (!(mod_mask & MOD_MASK_SHIFT))
7609 break;
7610 /* FALLTHROUGH */
7611 case K_S_LEFT:
7612 case K_S_RIGHT:
7613 case K_S_UP:
7614 case K_S_DOWN:
7615 case K_S_END:
7616 case K_S_HOME:
7617 /* Start selection right away, the cursor can move with
7618 * CTRL-O when beyond the end of the line. */
7619 start_selection();
7620
7621 /* Execute the key in (insert) Select mode. */
7622 stuffcharReadbuff(Ctrl_O);
7623 if (mod_mask)
7624 {
7625 char_u buf[4];
7626
7627 buf[0] = K_SPECIAL;
7628 buf[1] = KS_MODIFIER;
7629 buf[2] = mod_mask;
7630 buf[3] = NUL;
7631 stuffReadbuff(buf);
7632 }
7633 stuffcharReadbuff(c);
7634 return TRUE;
7635 }
7636 return FALSE;
7637}
7638#endif
7639
7640/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007641 * <Insert> key in Insert mode: toggle insert/remplace mode.
7642 */
7643 static void
7644ins_insert(replaceState)
7645 int replaceState;
7646{
7647#ifdef FEAT_FKMAP
7648 if (p_fkmap && p_ri)
7649 {
7650 beep_flush();
7651 EMSG(farsi_text_3); /* encoded in Farsi */
7652 return;
7653 }
7654#endif
7655
7656#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00007657# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007658 set_vim_var_string(VV_INSERTMODE,
7659 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007660# ifdef FEAT_VREPLACE
7661 replaceState == VREPLACE ? "v" :
7662# endif
7663 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00007664# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007665 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
7666#endif
7667 if (State & REPLACE_FLAG)
7668 State = INSERT | (State & LANGMAP);
7669 else
7670 State = replaceState | (State & LANGMAP);
7671 AppendCharToRedobuff(K_INS);
7672 showmode();
7673#ifdef CURSOR_SHAPE
7674 ui_cursor_shape(); /* may show different cursor shape */
7675#endif
7676}
7677
7678/*
7679 * Pressed CTRL-O in Insert mode.
7680 */
7681 static void
7682ins_ctrl_o()
7683{
7684#ifdef FEAT_VREPLACE
7685 if (State & VREPLACE_FLAG)
7686 restart_edit = 'V';
7687 else
7688#endif
7689 if (State & REPLACE_FLAG)
7690 restart_edit = 'R';
7691 else
7692 restart_edit = 'I';
7693#ifdef FEAT_VIRTUALEDIT
7694 if (virtual_active())
7695 ins_at_eol = FALSE; /* cursor always keeps its column */
7696 else
7697#endif
7698 ins_at_eol = (gchar_cursor() == NUL);
7699}
7700
7701/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 * If the cursor is on an indent, ^T/^D insert/delete one
7703 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
7704 * Always round the indent to 'shiftwith', this is compatible
7705 * with vi. But vi only supports ^T and ^D after an
7706 * autoindent, we support it everywhere.
7707 */
7708 static void
7709ins_shift(c, lastc)
7710 int c;
7711 int lastc;
7712{
7713 if (stop_arrow() == FAIL)
7714 return;
7715 AppendCharToRedobuff(c);
7716
7717 /*
7718 * 0^D and ^^D: remove all indent.
7719 */
7720 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
7721 {
7722 --curwin->w_cursor.col;
7723 (void)del_char(FALSE); /* delete the '^' or '0' */
7724 /* In Replace mode, restore the characters that '^' or '0' replaced. */
7725 if (State & REPLACE_FLAG)
7726 replace_pop_ins();
7727 if (lastc == '^')
7728 old_indent = get_indent(); /* remember curr. indent */
7729 change_indent(INDENT_SET, 0, TRUE, 0);
7730 }
7731 else
7732 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
7733
7734 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
7735 did_ai = FALSE;
7736#ifdef FEAT_SMARTINDENT
7737 did_si = FALSE;
7738 can_si = FALSE;
7739 can_si_back = FALSE;
7740#endif
7741#ifdef FEAT_CINDENT
7742 can_cindent = FALSE; /* no cindenting after ^D or ^T */
7743#endif
7744}
7745
7746 static void
7747ins_del()
7748{
7749 int temp;
7750
7751 if (stop_arrow() == FAIL)
7752 return;
7753 if (gchar_cursor() == NUL) /* delete newline */
7754 {
7755 temp = curwin->w_cursor.col;
7756 if (!can_bs(BS_EOL) /* only if "eol" included */
7757 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
7758 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
7759 || do_join(FALSE) == FAIL)
7760 vim_beep();
7761 else
7762 curwin->w_cursor.col = temp;
7763 }
7764 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
7765 vim_beep();
7766 did_ai = FALSE;
7767#ifdef FEAT_SMARTINDENT
7768 did_si = FALSE;
7769 can_si = FALSE;
7770 can_si_back = FALSE;
7771#endif
7772 AppendCharToRedobuff(K_DEL);
7773}
7774
7775/*
7776 * Handle Backspace, delete-word and delete-line in Insert mode.
7777 * Return TRUE when backspace was actually used.
7778 */
7779 static int
7780ins_bs(c, mode, inserted_space_p)
7781 int c;
7782 int mode;
7783 int *inserted_space_p;
7784{
7785 linenr_T lnum;
7786 int cc;
7787 int temp = 0; /* init for GCC */
7788 colnr_T mincol;
7789 int did_backspace = FALSE;
7790 int in_indent;
7791 int oldState;
7792#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007793 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794#endif
7795
7796 /*
7797 * can't delete anything in an empty file
7798 * can't backup past first character in buffer
7799 * can't backup past starting point unless 'backspace' > 1
7800 * can backup to a previous line if 'backspace' == 0
7801 */
7802 if ( bufempty()
7803 || (
7804#ifdef FEAT_RIGHTLEFT
7805 !revins_on &&
7806#endif
7807 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
7808 || (!can_bs(BS_START)
7809 && (arrow_used
7810 || (curwin->w_cursor.lnum == Insstart.lnum
7811 && curwin->w_cursor.col <= Insstart.col)))
7812 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
7813 && curwin->w_cursor.col <= ai_col)
7814 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
7815 {
7816 vim_beep();
7817 return FALSE;
7818 }
7819
7820 if (stop_arrow() == FAIL)
7821 return FALSE;
7822 in_indent = inindent(0);
7823#ifdef FEAT_CINDENT
7824 if (in_indent)
7825 can_cindent = FALSE;
7826#endif
7827#ifdef FEAT_COMMENTS
7828 end_comment_pending = NUL; /* After BS, don't auto-end comment */
7829#endif
7830#ifdef FEAT_RIGHTLEFT
7831 if (revins_on) /* put cursor after last inserted char */
7832 inc_cursor();
7833#endif
7834
7835#ifdef FEAT_VIRTUALEDIT
7836 /* Virtualedit:
7837 * BACKSPACE_CHAR eats a virtual space
7838 * BACKSPACE_WORD eats all coladd
7839 * BACKSPACE_LINE eats all coladd and keeps going
7840 */
7841 if (curwin->w_cursor.coladd > 0)
7842 {
7843 if (mode == BACKSPACE_CHAR)
7844 {
7845 --curwin->w_cursor.coladd;
7846 return TRUE;
7847 }
7848 if (mode == BACKSPACE_WORD)
7849 {
7850 curwin->w_cursor.coladd = 0;
7851 return TRUE;
7852 }
7853 curwin->w_cursor.coladd = 0;
7854 }
7855#endif
7856
7857 /*
7858 * delete newline!
7859 */
7860 if (curwin->w_cursor.col == 0)
7861 {
7862 lnum = Insstart.lnum;
7863 if (curwin->w_cursor.lnum == Insstart.lnum
7864#ifdef FEAT_RIGHTLEFT
7865 || revins_on
7866#endif
7867 )
7868 {
7869 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
7870 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
7871 return FALSE;
7872 --Insstart.lnum;
7873 Insstart.col = MAXCOL;
7874 }
7875 /*
7876 * In replace mode:
7877 * cc < 0: NL was inserted, delete it
7878 * cc >= 0: NL was replaced, put original characters back
7879 */
7880 cc = -1;
7881 if (State & REPLACE_FLAG)
7882 cc = replace_pop(); /* returns -1 if NL was inserted */
7883 /*
7884 * In replace mode, in the line we started replacing, we only move the
7885 * cursor.
7886 */
7887 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
7888 {
7889 dec_cursor();
7890 }
7891 else
7892 {
7893#ifdef FEAT_VREPLACE
7894 if (!(State & VREPLACE_FLAG)
7895 || curwin->w_cursor.lnum > orig_line_count)
7896#endif
7897 {
7898 temp = gchar_cursor(); /* remember current char */
7899 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00007900
7901 /* When "aw" is in 'formatoptions' we must delete the space at
7902 * the end of the line, otherwise the line will be broken
7903 * again when auto-formatting. */
7904 if (has_format_option(FO_AUTO)
7905 && has_format_option(FO_WHITE_PAR))
7906 {
7907 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
7908 TRUE);
7909 int len;
7910
7911 len = STRLEN(ptr);
7912 if (len > 0 && ptr[len - 1] == ' ')
7913 ptr[len - 1] = NUL;
7914 }
7915
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 (void)do_join(FALSE);
7917 if (temp == NUL && gchar_cursor() != NUL)
7918 inc_cursor();
7919 }
7920#ifdef FEAT_VREPLACE
7921 else
7922 dec_cursor();
7923#endif
7924
7925 /*
7926 * In REPLACE mode we have to put back the text that was replaced
7927 * by the NL. On the replace stack is first a NUL-terminated
7928 * sequence of characters that were deleted and then the
7929 * characters that NL replaced.
7930 */
7931 if (State & REPLACE_FLAG)
7932 {
7933 /*
7934 * Do the next ins_char() in NORMAL state, to
7935 * prevent ins_char() from replacing characters and
7936 * avoiding showmatch().
7937 */
7938 oldState = State;
7939 State = NORMAL;
7940 /*
7941 * restore characters (blanks) deleted after cursor
7942 */
7943 while (cc > 0)
7944 {
7945 temp = curwin->w_cursor.col;
7946#ifdef FEAT_MBYTE
7947 mb_replace_pop_ins(cc);
7948#else
7949 ins_char(cc);
7950#endif
7951 curwin->w_cursor.col = temp;
7952 cc = replace_pop();
7953 }
7954 /* restore the characters that NL replaced */
7955 replace_pop_ins();
7956 State = oldState;
7957 }
7958 }
7959 did_ai = FALSE;
7960 }
7961 else
7962 {
7963 /*
7964 * Delete character(s) before the cursor.
7965 */
7966#ifdef FEAT_RIGHTLEFT
7967 if (revins_on) /* put cursor on last inserted char */
7968 dec_cursor();
7969#endif
7970 mincol = 0;
7971 /* keep indent */
7972 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
7973#ifdef FEAT_RIGHTLEFT
7974 && !revins_on
7975#endif
7976 )
7977 {
7978 temp = curwin->w_cursor.col;
7979 beginline(BL_WHITE);
7980 if (curwin->w_cursor.col < (colnr_T)temp)
7981 mincol = curwin->w_cursor.col;
7982 curwin->w_cursor.col = temp;
7983 }
7984
7985 /*
7986 * Handle deleting one 'shiftwidth' or 'softtabstop'.
7987 */
7988 if ( mode == BACKSPACE_CHAR
7989 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00007990 || (curbuf->b_p_sts != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 && (*(ml_get_cursor() - 1) == TAB
7992 || (*(ml_get_cursor() - 1) == ' '
7993 && (!*inserted_space_p
7994 || arrow_used))))))
7995 {
7996 int ts;
7997 colnr_T vcol;
7998 colnr_T want_vcol;
7999 int extra = 0;
8000
8001 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008002 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 ts = curbuf->b_p_sw;
8004 else
8005 ts = curbuf->b_p_sts;
8006 /* Compute the virtual column where we want to be. Since
8007 * 'showbreak' may get in the way, need to get the last column of
8008 * the previous character. */
8009 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8010 dec_cursor();
8011 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8012 inc_cursor();
8013 want_vcol = (want_vcol / ts) * ts;
8014
8015 /* delete characters until we are at or before want_vcol */
8016 while (vcol > want_vcol
8017 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
8018 {
8019 dec_cursor();
8020 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8021 if (State & REPLACE_FLAG)
8022 {
8023 /* Don't delete characters before the insert point when in
8024 * Replace mode */
8025 if (curwin->w_cursor.lnum != Insstart.lnum
8026 || curwin->w_cursor.col >= Insstart.col)
8027 {
8028#if 0 /* what was this for? It causes problems when sw != ts. */
8029 if (State == REPLACE && (int)vcol < want_vcol)
8030 {
8031 (void)del_char(FALSE);
8032 extra = 2; /* don't pop too much */
8033 }
8034 else
8035#endif
8036 replace_do_bs();
8037 }
8038 }
8039 else
8040 (void)del_char(FALSE);
8041 }
8042
8043 /* insert extra spaces until we are at want_vcol */
8044 while (vcol < want_vcol)
8045 {
8046 /* Remember the first char we inserted */
8047 if (curwin->w_cursor.lnum == Insstart.lnum
8048 && curwin->w_cursor.col < Insstart.col)
8049 Insstart.col = curwin->w_cursor.col;
8050
8051#ifdef FEAT_VREPLACE
8052 if (State & VREPLACE_FLAG)
8053 ins_char(' ');
8054 else
8055#endif
8056 {
8057 ins_str((char_u *)" ");
8058 if ((State & REPLACE_FLAG) && extra <= 1)
8059 {
8060 if (extra)
8061 replace_push_off(NUL);
8062 else
8063 replace_push(NUL);
8064 }
8065 if (extra == 2)
8066 extra = 1;
8067 }
8068 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8069 }
8070 }
8071
8072 /*
8073 * Delete upto starting point, start of line or previous word.
8074 */
8075 else do
8076 {
8077#ifdef FEAT_RIGHTLEFT
8078 if (!revins_on) /* put cursor on char to be deleted */
8079#endif
8080 dec_cursor();
8081
8082 /* start of word? */
8083 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
8084 {
8085 mode = BACKSPACE_WORD_NOT_SPACE;
8086 temp = vim_iswordc(gchar_cursor());
8087 }
8088 /* end of word? */
8089 else if (mode == BACKSPACE_WORD_NOT_SPACE
8090 && (vim_isspace(cc = gchar_cursor())
8091 || vim_iswordc(cc) != temp))
8092 {
8093#ifdef FEAT_RIGHTLEFT
8094 if (!revins_on)
8095#endif
8096 inc_cursor();
8097#ifdef FEAT_RIGHTLEFT
8098 else if (State & REPLACE_FLAG)
8099 dec_cursor();
8100#endif
8101 break;
8102 }
8103 if (State & REPLACE_FLAG)
8104 replace_do_bs();
8105 else
8106 {
8107#ifdef FEAT_MBYTE
8108 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008109 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110#endif
8111 (void)del_char(FALSE);
8112#ifdef FEAT_MBYTE
8113 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008114 * If there are combining characters and 'delcombine' is set
8115 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 * character.
8117 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008118 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 inc_cursor();
8120#endif
8121#ifdef FEAT_RIGHTLEFT
8122 if (revins_chars)
8123 {
8124 revins_chars--;
8125 revins_legal++;
8126 }
8127 if (revins_on && gchar_cursor() == NUL)
8128 break;
8129#endif
8130 }
8131 /* Just a single backspace?: */
8132 if (mode == BACKSPACE_CHAR)
8133 break;
8134 } while (
8135#ifdef FEAT_RIGHTLEFT
8136 revins_on ||
8137#endif
8138 (curwin->w_cursor.col > mincol
8139 && (curwin->w_cursor.lnum != Insstart.lnum
8140 || curwin->w_cursor.col != Insstart.col)));
8141 did_backspace = TRUE;
8142 }
8143#ifdef FEAT_SMARTINDENT
8144 did_si = FALSE;
8145 can_si = FALSE;
8146 can_si_back = FALSE;
8147#endif
8148 if (curwin->w_cursor.col <= 1)
8149 did_ai = FALSE;
8150 /*
8151 * It's a little strange to put backspaces into the redo
8152 * buffer, but it makes auto-indent a lot easier to deal
8153 * with.
8154 */
8155 AppendCharToRedobuff(c);
8156
8157 /* If deleted before the insertion point, adjust it */
8158 if (curwin->w_cursor.lnum == Insstart.lnum
8159 && curwin->w_cursor.col < Insstart.col)
8160 Insstart.col = curwin->w_cursor.col;
8161
8162 /* vi behaviour: the cursor moves backward but the character that
8163 * was there remains visible
8164 * Vim behaviour: the cursor moves backward and the character that
8165 * was there is erased from the screen.
8166 * We can emulate the vi behaviour by pretending there is a dollar
8167 * displayed even when there isn't.
8168 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8169 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
8170 dollar_vcol = curwin->w_virtcol;
8171
8172 return did_backspace;
8173}
8174
8175#ifdef FEAT_MOUSE
8176 static void
8177ins_mouse(c)
8178 int c;
8179{
8180 pos_T tpos;
8181
8182# ifdef FEAT_GUI
8183 /* When GUI is active, also move/paste when 'mouse' is empty */
8184 if (!gui.in_use)
8185# endif
8186 if (!mouse_has(MOUSE_INSERT))
8187 return;
8188
8189 undisplay_dollar();
8190 tpos = curwin->w_cursor;
8191 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
8192 {
8193 start_arrow(&tpos);
8194# ifdef FEAT_CINDENT
8195 can_cindent = TRUE;
8196# endif
8197 }
8198
8199#ifdef FEAT_WINDOWS
8200 /* redraw status lines (in case another window became active) */
8201 redraw_statuslines();
8202#endif
8203}
8204
8205 static void
8206ins_mousescroll(up)
8207 int up;
8208{
8209 pos_T tpos;
8210# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8211 win_T *old_curwin;
8212# endif
8213
8214 tpos = curwin->w_cursor;
8215
8216# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8217 old_curwin = curwin;
8218
8219 /* Currently the mouse coordinates are only known in the GUI. */
8220 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
8221 {
8222 int row, col;
8223
8224 row = mouse_row;
8225 col = mouse_col;
8226
8227 /* find the window at the pointer coordinates */
8228 curwin = mouse_find_win(&row, &col);
8229 curbuf = curwin->w_buffer;
8230 }
8231 if (curwin == old_curwin)
8232# endif
8233 undisplay_dollar();
8234
8235 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8236 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
8237 else
8238 scroll_redraw(up, 3L);
8239
8240# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8241 curwin->w_redr_status = TRUE;
8242
8243 curwin = old_curwin;
8244 curbuf = curwin->w_buffer;
8245# endif
8246
8247 if (!equalpos(curwin->w_cursor, tpos))
8248 {
8249 start_arrow(&tpos);
8250# ifdef FEAT_CINDENT
8251 can_cindent = TRUE;
8252# endif
8253 }
8254}
8255#endif
8256
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008257#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00008258 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008259ins_tabline(c)
8260 int c;
8261{
8262 /* We will be leaving the current window, unless closing another tab. */
8263 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
8264 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
8265 {
8266 undisplay_dollar();
8267 start_arrow(&curwin->w_cursor);
8268# ifdef FEAT_CINDENT
8269 can_cindent = TRUE;
8270# endif
8271 }
8272
8273 if (c == K_TABLINE)
8274 goto_tabpage(current_tab);
8275 else
8276 handle_tabmenu();
8277
8278}
8279#endif
8280
8281#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 void
8283ins_scroll()
8284{
8285 pos_T tpos;
8286
8287 undisplay_dollar();
8288 tpos = curwin->w_cursor;
8289 if (gui_do_scroll())
8290 {
8291 start_arrow(&tpos);
8292# ifdef FEAT_CINDENT
8293 can_cindent = TRUE;
8294# endif
8295 }
8296}
8297
8298 void
8299ins_horscroll()
8300{
8301 pos_T tpos;
8302
8303 undisplay_dollar();
8304 tpos = curwin->w_cursor;
8305 if (gui_do_horiz_scroll())
8306 {
8307 start_arrow(&tpos);
8308# ifdef FEAT_CINDENT
8309 can_cindent = TRUE;
8310# endif
8311 }
8312}
8313#endif
8314
8315 static void
8316ins_left()
8317{
8318 pos_T tpos;
8319
8320#ifdef FEAT_FOLDING
8321 if ((fdo_flags & FDO_HOR) && KeyTyped)
8322 foldOpenCursor();
8323#endif
8324 undisplay_dollar();
8325 tpos = curwin->w_cursor;
8326 if (oneleft() == OK)
8327 {
8328 start_arrow(&tpos);
8329#ifdef FEAT_RIGHTLEFT
8330 /* If exit reversed string, position is fixed */
8331 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
8332 revins_legal++;
8333 revins_chars++;
8334#endif
8335 }
8336
8337 /*
8338 * if 'whichwrap' set for cursor in insert mode may go to
8339 * previous line
8340 */
8341 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
8342 {
8343 start_arrow(&tpos);
8344 --(curwin->w_cursor.lnum);
8345 coladvance((colnr_T)MAXCOL);
8346 curwin->w_set_curswant = TRUE; /* so we stay at the end */
8347 }
8348 else
8349 vim_beep();
8350}
8351
8352 static void
8353ins_home(c)
8354 int c;
8355{
8356 pos_T tpos;
8357
8358#ifdef FEAT_FOLDING
8359 if ((fdo_flags & FDO_HOR) && KeyTyped)
8360 foldOpenCursor();
8361#endif
8362 undisplay_dollar();
8363 tpos = curwin->w_cursor;
8364 if (c == K_C_HOME)
8365 curwin->w_cursor.lnum = 1;
8366 curwin->w_cursor.col = 0;
8367#ifdef FEAT_VIRTUALEDIT
8368 curwin->w_cursor.coladd = 0;
8369#endif
8370 curwin->w_curswant = 0;
8371 start_arrow(&tpos);
8372}
8373
8374 static void
8375ins_end(c)
8376 int c;
8377{
8378 pos_T tpos;
8379
8380#ifdef FEAT_FOLDING
8381 if ((fdo_flags & FDO_HOR) && KeyTyped)
8382 foldOpenCursor();
8383#endif
8384 undisplay_dollar();
8385 tpos = curwin->w_cursor;
8386 if (c == K_C_END)
8387 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
8388 coladvance((colnr_T)MAXCOL);
8389 curwin->w_curswant = MAXCOL;
8390
8391 start_arrow(&tpos);
8392}
8393
8394 static void
8395ins_s_left()
8396{
8397#ifdef FEAT_FOLDING
8398 if ((fdo_flags & FDO_HOR) && KeyTyped)
8399 foldOpenCursor();
8400#endif
8401 undisplay_dollar();
8402 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
8403 {
8404 start_arrow(&curwin->w_cursor);
8405 (void)bck_word(1L, FALSE, FALSE);
8406 curwin->w_set_curswant = TRUE;
8407 }
8408 else
8409 vim_beep();
8410}
8411
8412 static void
8413ins_right()
8414{
8415#ifdef FEAT_FOLDING
8416 if ((fdo_flags & FDO_HOR) && KeyTyped)
8417 foldOpenCursor();
8418#endif
8419 undisplay_dollar();
8420 if (gchar_cursor() != NUL || virtual_active()
8421 )
8422 {
8423 start_arrow(&curwin->w_cursor);
8424 curwin->w_set_curswant = TRUE;
8425#ifdef FEAT_VIRTUALEDIT
8426 if (virtual_active())
8427 oneright();
8428 else
8429#endif
8430 {
8431#ifdef FEAT_MBYTE
8432 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008433 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 else
8435#endif
8436 ++curwin->w_cursor.col;
8437 }
8438
8439#ifdef FEAT_RIGHTLEFT
8440 revins_legal++;
8441 if (revins_chars)
8442 revins_chars--;
8443#endif
8444 }
8445 /* if 'whichwrap' set for cursor in insert mode, may move the
8446 * cursor to the next line */
8447 else if (vim_strchr(p_ww, ']') != NULL
8448 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
8449 {
8450 start_arrow(&curwin->w_cursor);
8451 curwin->w_set_curswant = TRUE;
8452 ++curwin->w_cursor.lnum;
8453 curwin->w_cursor.col = 0;
8454 }
8455 else
8456 vim_beep();
8457}
8458
8459 static void
8460ins_s_right()
8461{
8462#ifdef FEAT_FOLDING
8463 if ((fdo_flags & FDO_HOR) && KeyTyped)
8464 foldOpenCursor();
8465#endif
8466 undisplay_dollar();
8467 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
8468 || gchar_cursor() != NUL)
8469 {
8470 start_arrow(&curwin->w_cursor);
8471 (void)fwd_word(1L, FALSE, 0);
8472 curwin->w_set_curswant = TRUE;
8473 }
8474 else
8475 vim_beep();
8476}
8477
8478 static void
8479ins_up(startcol)
8480 int startcol; /* when TRUE move to Insstart.col */
8481{
8482 pos_T tpos;
8483 linenr_T old_topline = curwin->w_topline;
8484#ifdef FEAT_DIFF
8485 int old_topfill = curwin->w_topfill;
8486#endif
8487
8488 undisplay_dollar();
8489 tpos = curwin->w_cursor;
8490 if (cursor_up(1L, TRUE) == OK)
8491 {
8492 if (startcol)
8493 coladvance(getvcol_nolist(&Insstart));
8494 if (old_topline != curwin->w_topline
8495#ifdef FEAT_DIFF
8496 || old_topfill != curwin->w_topfill
8497#endif
8498 )
8499 redraw_later(VALID);
8500 start_arrow(&tpos);
8501#ifdef FEAT_CINDENT
8502 can_cindent = TRUE;
8503#endif
8504 }
8505 else
8506 vim_beep();
8507}
8508
8509 static void
8510ins_pageup()
8511{
8512 pos_T tpos;
8513
8514 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008515
8516#ifdef FEAT_WINDOWS
8517 if (mod_mask & MOD_MASK_CTRL)
8518 {
8519 /* <C-PageUp>: tab page back */
8520 goto_tabpage(-1);
8521 return;
8522 }
8523#endif
8524
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 tpos = curwin->w_cursor;
8526 if (onepage(BACKWARD, 1L) == OK)
8527 {
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_down(startcol)
8539 int startcol; /* when TRUE move to Insstart.col */
8540{
8541 pos_T tpos;
8542 linenr_T old_topline = curwin->w_topline;
8543#ifdef FEAT_DIFF
8544 int old_topfill = curwin->w_topfill;
8545#endif
8546
8547 undisplay_dollar();
8548 tpos = curwin->w_cursor;
8549 if (cursor_down(1L, TRUE) == OK)
8550 {
8551 if (startcol)
8552 coladvance(getvcol_nolist(&Insstart));
8553 if (old_topline != curwin->w_topline
8554#ifdef FEAT_DIFF
8555 || old_topfill != curwin->w_topfill
8556#endif
8557 )
8558 redraw_later(VALID);
8559 start_arrow(&tpos);
8560#ifdef FEAT_CINDENT
8561 can_cindent = TRUE;
8562#endif
8563 }
8564 else
8565 vim_beep();
8566}
8567
8568 static void
8569ins_pagedown()
8570{
8571 pos_T tpos;
8572
8573 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008574
8575#ifdef FEAT_WINDOWS
8576 if (mod_mask & MOD_MASK_CTRL)
8577 {
8578 /* <C-PageDown>: tab page forward */
8579 goto_tabpage(0);
8580 return;
8581 }
8582#endif
8583
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 tpos = curwin->w_cursor;
8585 if (onepage(FORWARD, 1L) == OK)
8586 {
8587 start_arrow(&tpos);
8588#ifdef FEAT_CINDENT
8589 can_cindent = TRUE;
8590#endif
8591 }
8592 else
8593 vim_beep();
8594}
8595
8596#ifdef FEAT_DND
8597 static void
8598ins_drop()
8599{
8600 do_put('~', BACKWARD, 1L, PUT_CURSEND);
8601}
8602#endif
8603
8604/*
8605 * Handle TAB in Insert or Replace mode.
8606 * Return TRUE when the TAB needs to be inserted like a normal character.
8607 */
8608 static int
8609ins_tab()
8610{
8611 int ind;
8612 int i;
8613 int temp;
8614
8615 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
8616 Insstart_blank_vcol = get_nolist_virtcol();
8617 if (echeck_abbr(TAB + ABBR_OFF))
8618 return FALSE;
8619
8620 ind = inindent(0);
8621#ifdef FEAT_CINDENT
8622 if (ind)
8623 can_cindent = FALSE;
8624#endif
8625
8626 /*
8627 * When nothing special, insert TAB like a normal character
8628 */
8629 if (!curbuf->b_p_et
8630 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
8631 && curbuf->b_p_sts == 0)
8632 return TRUE;
8633
8634 if (stop_arrow() == FAIL)
8635 return TRUE;
8636
8637 did_ai = FALSE;
8638#ifdef FEAT_SMARTINDENT
8639 did_si = FALSE;
8640 can_si = FALSE;
8641 can_si_back = FALSE;
8642#endif
8643 AppendToRedobuff((char_u *)"\t");
8644
8645 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
8646 temp = (int)curbuf->b_p_sw;
8647 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
8648 temp = (int)curbuf->b_p_sts;
8649 else /* otherwise use 'tabstop' */
8650 temp = (int)curbuf->b_p_ts;
8651 temp -= get_nolist_virtcol() % temp;
8652
8653 /*
8654 * Insert the first space with ins_char(). It will delete one char in
8655 * replace mode. Insert the rest with ins_str(); it will not delete any
8656 * chars. For VREPLACE mode, we use ins_char() for all characters.
8657 */
8658 ins_char(' ');
8659 while (--temp > 0)
8660 {
8661#ifdef FEAT_VREPLACE
8662 if (State & VREPLACE_FLAG)
8663 ins_char(' ');
8664 else
8665#endif
8666 {
8667 ins_str((char_u *)" ");
8668 if (State & REPLACE_FLAG) /* no char replaced */
8669 replace_push(NUL);
8670 }
8671 }
8672
8673 /*
8674 * When 'expandtab' not set: Replace spaces by TABs where possible.
8675 */
8676 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
8677 {
8678 char_u *ptr;
8679#ifdef FEAT_VREPLACE
8680 char_u *saved_line = NULL; /* init for GCC */
8681 pos_T pos;
8682#endif
8683 pos_T fpos;
8684 pos_T *cursor;
8685 colnr_T want_vcol, vcol;
8686 int change_col = -1;
8687 int save_list = curwin->w_p_list;
8688
8689 /*
8690 * Get the current line. For VREPLACE mode, don't make real changes
8691 * yet, just work on a copy of the line.
8692 */
8693#ifdef FEAT_VREPLACE
8694 if (State & VREPLACE_FLAG)
8695 {
8696 pos = curwin->w_cursor;
8697 cursor = &pos;
8698 saved_line = vim_strsave(ml_get_curline());
8699 if (saved_line == NULL)
8700 return FALSE;
8701 ptr = saved_line + pos.col;
8702 }
8703 else
8704#endif
8705 {
8706 ptr = ml_get_cursor();
8707 cursor = &curwin->w_cursor;
8708 }
8709
8710 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
8711 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8712 curwin->w_p_list = FALSE;
8713
8714 /* Find first white before the cursor */
8715 fpos = curwin->w_cursor;
8716 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
8717 {
8718 --fpos.col;
8719 --ptr;
8720 }
8721
8722 /* In Replace mode, don't change characters before the insert point. */
8723 if ((State & REPLACE_FLAG)
8724 && fpos.lnum == Insstart.lnum
8725 && fpos.col < Insstart.col)
8726 {
8727 ptr += Insstart.col - fpos.col;
8728 fpos.col = Insstart.col;
8729 }
8730
8731 /* compute virtual column numbers of first white and cursor */
8732 getvcol(curwin, &fpos, &vcol, NULL, NULL);
8733 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
8734
8735 /* Use as many TABs as possible. Beware of 'showbreak' and
8736 * 'linebreak' adding extra virtual columns. */
8737 while (vim_iswhite(*ptr))
8738 {
8739 i = lbr_chartabsize((char_u *)"\t", vcol);
8740 if (vcol + i > want_vcol)
8741 break;
8742 if (*ptr != TAB)
8743 {
8744 *ptr = TAB;
8745 if (change_col < 0)
8746 {
8747 change_col = fpos.col; /* Column of first change */
8748 /* May have to adjust Insstart */
8749 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
8750 Insstart.col = fpos.col;
8751 }
8752 }
8753 ++fpos.col;
8754 ++ptr;
8755 vcol += i;
8756 }
8757
8758 if (change_col >= 0)
8759 {
8760 int repl_off = 0;
8761
8762 /* Skip over the spaces we need. */
8763 while (vcol < want_vcol && *ptr == ' ')
8764 {
8765 vcol += lbr_chartabsize(ptr, vcol);
8766 ++ptr;
8767 ++repl_off;
8768 }
8769 if (vcol > want_vcol)
8770 {
8771 /* Must have a char with 'showbreak' just before it. */
8772 --ptr;
8773 --repl_off;
8774 }
8775 fpos.col += repl_off;
8776
8777 /* Delete following spaces. */
8778 i = cursor->col - fpos.col;
8779 if (i > 0)
8780 {
8781 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
8782 /* correct replace stack. */
8783 if ((State & REPLACE_FLAG)
8784#ifdef FEAT_VREPLACE
8785 && !(State & VREPLACE_FLAG)
8786#endif
8787 )
8788 for (temp = i; --temp >= 0; )
8789 replace_join(repl_off);
8790 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00008791#ifdef FEAT_NETBEANS_INTG
8792 if (usingNetbeans)
8793 {
8794 netbeans_removed(curbuf, fpos.lnum, cursor->col,
8795 (long)(i + 1));
8796 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
8797 (char_u *)"\t", 1);
8798 }
8799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 cursor->col -= i;
8801
8802#ifdef FEAT_VREPLACE
8803 /*
8804 * In VREPLACE mode, we haven't changed anything yet. Do it now by
8805 * backspacing over the changed spacing and then inserting the new
8806 * spacing.
8807 */
8808 if (State & VREPLACE_FLAG)
8809 {
8810 /* Backspace from real cursor to change_col */
8811 backspace_until_column(change_col);
8812
8813 /* Insert each char in saved_line from changed_col to
8814 * ptr-cursor */
8815 ins_bytes_len(saved_line + change_col,
8816 cursor->col - change_col);
8817 }
8818#endif
8819 }
8820
8821#ifdef FEAT_VREPLACE
8822 if (State & VREPLACE_FLAG)
8823 vim_free(saved_line);
8824#endif
8825 curwin->w_p_list = save_list;
8826 }
8827
8828 return FALSE;
8829}
8830
8831/*
8832 * Handle CR or NL in insert mode.
8833 * Return TRUE when out of memory or can't undo.
8834 */
8835 static int
8836ins_eol(c)
8837 int c;
8838{
8839 int i;
8840
8841 if (echeck_abbr(c + ABBR_OFF))
8842 return FALSE;
8843 if (stop_arrow() == FAIL)
8844 return TRUE;
8845 undisplay_dollar();
8846
8847 /*
8848 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
8849 * character under the cursor. Only push a NUL on the replace stack,
8850 * nothing to put back when the NL is deleted.
8851 */
8852 if ((State & REPLACE_FLAG)
8853#ifdef FEAT_VREPLACE
8854 && !(State & VREPLACE_FLAG)
8855#endif
8856 )
8857 replace_push(NUL);
8858
8859 /*
8860 * In VREPLACE mode, a NL replaces the rest of the line, and starts
8861 * replacing the next line, so we push all of the characters left on the
8862 * line onto the replace stack. This is not done here though, it is done
8863 * in open_line().
8864 */
8865
8866#ifdef FEAT_RIGHTLEFT
8867# ifdef FEAT_FKMAP
8868 if (p_altkeymap && p_fkmap)
8869 fkmap(NL);
8870# endif
8871 /* NL in reverse insert will always start in the end of
8872 * current line. */
8873 if (revins_on)
8874 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8875#endif
8876
8877 AppendToRedobuff(NL_STR);
8878 i = open_line(FORWARD,
8879#ifdef FEAT_COMMENTS
8880 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
8881#endif
8882 0, old_indent);
8883 old_indent = 0;
8884#ifdef FEAT_CINDENT
8885 can_cindent = TRUE;
8886#endif
8887
8888 return (!i);
8889}
8890
8891#ifdef FEAT_DIGRAPHS
8892/*
8893 * Handle digraph in insert mode.
8894 * Returns character still to be inserted, or NUL when nothing remaining to be
8895 * done.
8896 */
8897 static int
8898ins_digraph()
8899{
8900 int c;
8901 int cc;
8902
8903 pc_status = PC_STATUS_UNSET;
8904 if (redrawing() && !char_avail())
8905 {
8906 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008907 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908
8909 edit_putchar('?', TRUE);
8910#ifdef FEAT_CMDL_INFO
8911 add_to_showcmd_c(Ctrl_K);
8912#endif
8913 }
8914
8915#ifdef USE_ON_FLY_SCROLL
8916 dont_scroll = TRUE; /* disallow scrolling here */
8917#endif
8918
8919 /* don't map the digraph chars. This also prevents the
8920 * mode message to be deleted when ESC is hit */
8921 ++no_mapping;
8922 ++allow_keys;
8923 c = safe_vgetc();
8924 --no_mapping;
8925 --allow_keys;
8926 if (IS_SPECIAL(c) || mod_mask) /* special key */
8927 {
8928#ifdef FEAT_CMDL_INFO
8929 clear_showcmd();
8930#endif
8931 insert_special(c, TRUE, FALSE);
8932 return NUL;
8933 }
8934 if (c != ESC)
8935 {
8936 if (redrawing() && !char_avail())
8937 {
8938 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008939 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940
8941 if (char2cells(c) == 1)
8942 {
8943 /* first remove the '?', otherwise it's restored when typing
8944 * an ESC next */
8945 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00008946 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 edit_putchar(c, TRUE);
8948 }
8949#ifdef FEAT_CMDL_INFO
8950 add_to_showcmd_c(c);
8951#endif
8952 }
8953 ++no_mapping;
8954 ++allow_keys;
8955 cc = safe_vgetc();
8956 --no_mapping;
8957 --allow_keys;
8958 if (cc != ESC)
8959 {
8960 AppendToRedobuff((char_u *)CTRL_V_STR);
8961 c = getdigraph(c, cc, TRUE);
8962#ifdef FEAT_CMDL_INFO
8963 clear_showcmd();
8964#endif
8965 return c;
8966 }
8967 }
8968 edit_unputchar();
8969#ifdef FEAT_CMDL_INFO
8970 clear_showcmd();
8971#endif
8972 return NUL;
8973}
8974#endif
8975
8976/*
8977 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
8978 * Returns the char to be inserted, or NUL if none found.
8979 */
8980 static int
8981ins_copychar(lnum)
8982 linenr_T lnum;
8983{
8984 int c;
8985 int temp;
8986 char_u *ptr, *prev_ptr;
8987
8988 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8989 {
8990 vim_beep();
8991 return NUL;
8992 }
8993
8994 /* try to advance to the cursor column */
8995 temp = 0;
8996 ptr = ml_get(lnum);
8997 prev_ptr = ptr;
8998 validate_virtcol();
8999 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
9000 {
9001 prev_ptr = ptr;
9002 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
9003 }
9004 if ((colnr_T)temp > curwin->w_virtcol)
9005 ptr = prev_ptr;
9006
9007#ifdef FEAT_MBYTE
9008 c = (*mb_ptr2char)(ptr);
9009#else
9010 c = *ptr;
9011#endif
9012 if (c == NUL)
9013 vim_beep();
9014 return c;
9015}
9016
Bram Moolenaar4be06f92005-07-29 22:36:03 +00009017/*
9018 * CTRL-Y or CTRL-E typed in Insert mode.
9019 */
9020 static int
9021ins_ctrl_ey(tc)
9022 int tc;
9023{
9024 int c = tc;
9025
9026#ifdef FEAT_INS_EXPAND
9027 if (ctrl_x_mode == CTRL_X_SCROLL)
9028 {
9029 if (c == Ctrl_Y)
9030 scrolldown_clamp();
9031 else
9032 scrollup_clamp();
9033 redraw_later(VALID);
9034 }
9035 else
9036#endif
9037 {
9038 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
9039 if (c != NUL)
9040 {
9041 long tw_save;
9042
9043 /* The character must be taken literally, insert like it
9044 * was typed after a CTRL-V, and pretend 'textwidth'
9045 * wasn't set. Digits, 'o' and 'x' are special after a
9046 * CTRL-V, don't use it for these. */
9047 if (c < 256 && !isalnum(c))
9048 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
9049 tw_save = curbuf->b_p_tw;
9050 curbuf->b_p_tw = -1;
9051 insert_special(c, TRUE, FALSE);
9052 curbuf->b_p_tw = tw_save;
9053#ifdef FEAT_RIGHTLEFT
9054 revins_chars++;
9055 revins_legal++;
9056#endif
9057 c = Ctrl_V; /* pretend CTRL-V is last character */
9058 auto_format(FALSE, TRUE);
9059 }
9060 }
9061 return c;
9062}
9063
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064#ifdef FEAT_SMARTINDENT
9065/*
9066 * Try to do some very smart auto-indenting.
9067 * Used when inserting a "normal" character.
9068 */
9069 static void
9070ins_try_si(c)
9071 int c;
9072{
9073 pos_T *pos, old_pos;
9074 char_u *ptr;
9075 int i;
9076 int temp;
9077
9078 /*
9079 * do some very smart indenting when entering '{' or '}'
9080 */
9081 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
9082 {
9083 /*
9084 * for '}' set indent equal to indent of line containing matching '{'
9085 */
9086 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
9087 {
9088 old_pos = curwin->w_cursor;
9089 /*
9090 * If the matching '{' has a ')' immediately before it (ignoring
9091 * white-space), then line up with the start of the line
9092 * containing the matching '(' if there is one. This handles the
9093 * case where an "if (..\n..) {" statement continues over multiple
9094 * lines -- webb
9095 */
9096 ptr = ml_get(pos->lnum);
9097 i = pos->col;
9098 if (i > 0) /* skip blanks before '{' */
9099 while (--i > 0 && vim_iswhite(ptr[i]))
9100 ;
9101 curwin->w_cursor.lnum = pos->lnum;
9102 curwin->w_cursor.col = i;
9103 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
9104 curwin->w_cursor = *pos;
9105 i = get_indent();
9106 curwin->w_cursor = old_pos;
9107#ifdef FEAT_VREPLACE
9108 if (State & VREPLACE_FLAG)
9109 change_indent(INDENT_SET, i, FALSE, NUL);
9110 else
9111#endif
9112 (void)set_indent(i, SIN_CHANGED);
9113 }
9114 else if (curwin->w_cursor.col > 0)
9115 {
9116 /*
9117 * when inserting '{' after "O" reduce indent, but not
9118 * more than indent of previous line
9119 */
9120 temp = TRUE;
9121 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
9122 {
9123 old_pos = curwin->w_cursor;
9124 i = get_indent();
9125 while (curwin->w_cursor.lnum > 1)
9126 {
9127 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
9128
9129 /* ignore empty lines and lines starting with '#'. */
9130 if (*ptr != '#' && *ptr != NUL)
9131 break;
9132 }
9133 if (get_indent() >= i)
9134 temp = FALSE;
9135 curwin->w_cursor = old_pos;
9136 }
9137 if (temp)
9138 shift_line(TRUE, FALSE, 1);
9139 }
9140 }
9141
9142 /*
9143 * set indent of '#' always to 0
9144 */
9145 if (curwin->w_cursor.col > 0 && can_si && c == '#')
9146 {
9147 /* remember current indent for next line */
9148 old_indent = get_indent();
9149 (void)set_indent(0, SIN_CHANGED);
9150 }
9151
9152 /* Adjust ai_col, the char at this position can be deleted. */
9153 if (ai_col > curwin->w_cursor.col)
9154 ai_col = curwin->w_cursor.col;
9155}
9156#endif
9157
9158/*
9159 * Get the value that w_virtcol would have when 'list' is off.
9160 * Unless 'cpo' contains the 'L' flag.
9161 */
9162 static colnr_T
9163get_nolist_virtcol()
9164{
9165 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9166 return getvcol_nolist(&curwin->w_cursor);
9167 validate_virtcol();
9168 return curwin->w_virtcol;
9169}