blob: da15010f5d35354e01ce3fb1bbe88d3690ea26af [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 Moolenaar488c6512005-08-11 20:09:58 +000043 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^P^S^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 Moolenaar488c6512005-08-11 20:09:58 +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 Moolenaara6557602006-02-04 22:43:20 +000093/* When "compl_leader" is not NULL only matches that start with this string
94 * are used. */
95static char_u *compl_leader = NULL;
96
Bram Moolenaarc7453f52006-02-10 23:20:28 +000097static int compl_get_longest = FALSE; /* put longest common string
98 in compl_leader */
99
Bram Moolenaara6557602006-02-04 22:43:20 +0000100static int compl_used_match; /* Selected one of the matches. When
101 FALSE the match was edited or using
102 the longest common string. */
103
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000104/* When the first completion is done "compl_started" is set. When it's
105 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000106static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000107
Bram Moolenaar572cb562005-08-05 21:35:02 +0000108static int compl_matches = 0;
109static char_u *compl_pattern = NULL;
110static int compl_direction = FORWARD;
111static int compl_shows_dir = FORWARD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000112static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000113static pos_T compl_startpos;
114static colnr_T compl_col = 0; /* column where the text starts
115 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000116static char_u *compl_orig_text = NULL; /* text as it was before
117 * completion started */
118static int compl_cont_mode = 0;
119static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000121static void ins_ctrl_x __ARGS((void));
122static int has_compl_option __ARGS((int dict_opt));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000123static int ins_compl_equal __ARGS((compl_T *match, char_u *str, int len));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000124static void ins_compl_longest_match __ARGS((compl_T *match));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000125static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches, int icase));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126static int ins_compl_make_cyclic __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000127static void ins_compl_upd_pum __ARGS((void));
128static void ins_compl_del_pum __ARGS((void));
Bram Moolenaar280f1262006-01-30 00:14:18 +0000129static int pum_wanted __ARGS((void));
Bram Moolenaar65c923a2006-03-03 22:56:30 +0000130static int pum_enough_matches __ARGS((void));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000131static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int flags, int thesaurus));
Bram Moolenaar0b238792006-03-02 22:49:12 +0000132static 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 +0000133static char_u *find_line_end __ARGS((char_u *ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134static void ins_compl_free __ARGS((void));
135static void ins_compl_clear __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000136static int ins_compl_bs __ARGS((void));
137static void ins_compl_addleader __ARGS((int c));
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000138static void ins_compl_set_original_text __ARGS((char_u *str));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000139static void ins_compl_addfrommatch __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000140static int ins_compl_prep __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
Bram Moolenaara94bc432006-03-10 21:42:59 +0000142#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
143static void ins_compl_add_list __ARGS((list_T *list));
144#endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000145static int ins_compl_get_exp __ARGS((pos_T *ini));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146static void ins_compl_delete __ARGS((void));
147static void ins_compl_insert __ARGS((void));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000148static int ins_compl_next __ARGS((int allow_get_expansion, int count, int insert_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000149static int ins_compl_key2dir __ARGS((int c));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000150static int ins_compl_pum_key __ARGS((int c));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000151static int ins_compl_key2count __ARGS((int c));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000152static int ins_compl_use_match __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153static int ins_complete __ARGS((int c));
154static int quote_meta __ARGS((char_u *dest, char_u *str, int len));
155#endif /* FEAT_INS_EXPAND */
156
157#define BACKSPACE_CHAR 1
158#define BACKSPACE_WORD 2
159#define BACKSPACE_WORD_NOT_SPACE 3
160#define BACKSPACE_LINE 4
161
Bram Moolenaar754b5602006-02-09 23:53:20 +0000162static void ins_redraw __ARGS((int ready));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163static void ins_ctrl_v __ARGS((void));
164static void undisplay_dollar __ARGS((void));
165static void insert_special __ARGS((int, int, int));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000166static void internal_format __ARGS((int textwidth, int second_indent, int flags, int format_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167static void check_auto_format __ARGS((int));
168static void redo_literal __ARGS((int c));
169static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000170#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +0000171static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000172static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000173static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
176static int echeck_abbr __ARGS((int));
177static void replace_push_off __ARGS((int c));
178static int replace_pop __ARGS((void));
179static void replace_join __ARGS((int off));
180static void replace_pop_ins __ARGS((void));
181#ifdef FEAT_MBYTE
182static void mb_replace_pop_ins __ARGS((int cc));
183#endif
184static void replace_flush __ARGS((void));
185static void replace_do_bs __ARGS((void));
186#ifdef FEAT_CINDENT
187static int cindent_on __ARGS((void));
188#endif
189static void ins_reg __ARGS((void));
190static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000191static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000192static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193#ifdef FEAT_RIGHTLEFT
194static void ins_ctrl_ __ARGS((void));
195#endif
196#ifdef FEAT_VISUAL
197static int ins_start_select __ARGS((int c));
198#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000199static void ins_insert __ARGS((int replaceState));
200static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201static void ins_shift __ARGS((int c, int lastc));
202static void ins_del __ARGS((void));
203static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
204#ifdef FEAT_MOUSE
205static void ins_mouse __ARGS((int c));
206static void ins_mousescroll __ARGS((int up));
207#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000208#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
209static void ins_tabline __ARGS((int c));
210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211static void ins_left __ARGS((void));
212static void ins_home __ARGS((int c));
213static void ins_end __ARGS((int c));
214static void ins_s_left __ARGS((void));
215static void ins_right __ARGS((void));
216static void ins_s_right __ARGS((void));
217static void ins_up __ARGS((int startcol));
218static void ins_pageup __ARGS((void));
219static void ins_down __ARGS((int startcol));
220static void ins_pagedown __ARGS((void));
221#ifdef FEAT_DND
222static void ins_drop __ARGS((void));
223#endif
224static int ins_tab __ARGS((void));
225static int ins_eol __ARGS((int c));
226#ifdef FEAT_DIGRAPHS
227static int ins_digraph __ARGS((void));
228#endif
229static int ins_copychar __ARGS((linenr_T lnum));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000230static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231#ifdef FEAT_SMARTINDENT
232static void ins_try_si __ARGS((int c));
233#endif
234static colnr_T get_nolist_virtcol __ARGS((void));
235
236static colnr_T Insstart_textlen; /* length of line when insert started */
237static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
238
239static char_u *last_insert = NULL; /* the text of the previous insert,
240 K_SPECIAL and CSI are escaped */
241static int last_insert_skip; /* nr of chars in front of previous insert */
242static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000243static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244
245#ifdef FEAT_CINDENT
246static int can_cindent; /* may do cindenting on this line */
247#endif
248
249static int old_indent = 0; /* for ^^D command in insert mode */
250
251#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000252static int revins_on; /* reverse insert mode on */
253static int revins_chars; /* how much to skip after edit */
254static int revins_legal; /* was the last char 'legal'? */
255static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256#endif
257
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258static int ins_need_undo; /* call u_save() before inserting a
259 char. Set when edit() is called.
260 after that arrow_used is used. */
261
262static int did_add_space = FALSE; /* auto_format() added an extra space
263 under the cursor */
264
265/*
266 * edit(): Start inserting text.
267 *
268 * "cmdchar" can be:
269 * 'i' normal insert command
270 * 'a' normal append command
271 * 'R' replace command
272 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
273 * but still only one <CR> is inserted. The <Esc> is not used for redo.
274 * 'g' "gI" command.
275 * 'V' "gR" command for Virtual Replace mode.
276 * 'v' "gr" command for single character Virtual Replace mode.
277 *
278 * This function is not called recursively. For CTRL-O commands, it returns
279 * and lets the caller handle the Normal-mode command.
280 *
281 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
282 */
283 int
284edit(cmdchar, startln, count)
285 int cmdchar;
286 int startln; /* if set, insert at start of line */
287 long count;
288{
289 int c = 0;
290 char_u *ptr;
291 int lastc;
292 colnr_T mincol;
293 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 int i;
295 int did_backspace = TRUE; /* previous char was backspace */
296#ifdef FEAT_CINDENT
297 int line_is_white = FALSE; /* line is empty before insert */
298#endif
299 linenr_T old_topline = 0; /* topline before insertion */
300#ifdef FEAT_DIFF
301 int old_topfill = -1;
302#endif
303 int inserted_space = FALSE; /* just inserted a space */
304 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000305 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000307 /* Remember whether editing was restarted after CTRL-O. */
308 did_restart_edit = restart_edit;
309
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310 /* sleep before redrawing, needed for "CTRL-O :" that results in an
311 * error message */
312 check_for_delay(TRUE);
313
314#ifdef HAVE_SANDBOX
315 /* Don't allow inserting in the sandbox. */
316 if (sandbox != 0)
317 {
318 EMSG(_(e_sandbox));
319 return FALSE;
320 }
321#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000322 /* Don't allow changes in the buffer while editing the cmdline. The
323 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000324 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000325 {
326 EMSG(_(e_secure));
327 return FALSE;
328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329
330#ifdef FEAT_INS_EXPAND
331 ins_compl_clear(); /* clear stuff for CTRL-X mode */
332#endif
333
Bram Moolenaar843ee412004-06-30 16:16:41 +0000334#ifdef FEAT_AUTOCMD
335 /*
336 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
337 */
338 if (cmdchar != 'r' && cmdchar != 'v')
339 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000340# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000341 if (cmdchar == 'R')
342 ptr = (char_u *)"r";
343 else if (cmdchar == 'V')
344 ptr = (char_u *)"v";
345 else
346 ptr = (char_u *)"i";
347 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +0000348# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000349 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
350 }
351#endif
352
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353#ifdef FEAT_MOUSE
354 /*
355 * When doing a paste with the middle mouse button, Insstart is set to
356 * where the paste started.
357 */
358 if (where_paste_started.lnum != 0)
359 Insstart = where_paste_started;
360 else
361#endif
362 {
363 Insstart = curwin->w_cursor;
364 if (startln)
365 Insstart.col = 0;
366 }
367 Insstart_textlen = linetabsize(ml_get_curline());
368 Insstart_blank_vcol = MAXCOL;
369 if (!did_ai)
370 ai_col = 0;
371
372 if (cmdchar != NUL && restart_edit == 0)
373 {
374 ResetRedobuff();
375 AppendNumberToRedobuff(count);
376#ifdef FEAT_VREPLACE
377 if (cmdchar == 'V' || cmdchar == 'v')
378 {
379 /* "gR" or "gr" command */
380 AppendCharToRedobuff('g');
381 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
382 }
383 else
384#endif
385 {
386 AppendCharToRedobuff(cmdchar);
387 if (cmdchar == 'g') /* "gI" command */
388 AppendCharToRedobuff('I');
389 else if (cmdchar == 'r') /* "r<CR>" command */
390 count = 1; /* insert only one <CR> */
391 }
392 }
393
394 if (cmdchar == 'R')
395 {
396#ifdef FEAT_FKMAP
397 if (p_fkmap && p_ri)
398 {
399 beep_flush();
400 EMSG(farsi_text_3); /* encoded in Farsi */
401 State = INSERT;
402 }
403 else
404#endif
405 State = REPLACE;
406 }
407#ifdef FEAT_VREPLACE
408 else if (cmdchar == 'V' || cmdchar == 'v')
409 {
410 State = VREPLACE;
411 replaceState = VREPLACE;
412 orig_line_count = curbuf->b_ml.ml_line_count;
413 vr_lines_changed = 1;
414 }
415#endif
416 else
417 State = INSERT;
418
419 stop_insert_mode = FALSE;
420
421 /*
422 * Need to recompute the cursor position, it might move when the cursor is
423 * on a TAB or special character.
424 */
425 curs_columns(TRUE);
426
427 /*
428 * Enable langmap or IME, indicated by 'iminsert'.
429 * Note that IME may enabled/disabled without us noticing here, thus the
430 * 'iminsert' value may not reflect what is actually used. It is updated
431 * when hitting <Esc>.
432 */
433 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
434 State |= LANGMAP;
435#ifdef USE_IM_CONTROL
436 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
437#endif
438
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439#ifdef FEAT_MOUSE
440 setmouse();
441#endif
442#ifdef FEAT_CMDL_INFO
443 clear_showcmd();
444#endif
445#ifdef FEAT_RIGHTLEFT
446 /* there is no reverse replace mode */
447 revins_on = (State == INSERT && p_ri);
448 if (revins_on)
449 undisplay_dollar();
450 revins_chars = 0;
451 revins_legal = 0;
452 revins_scol = -1;
453#endif
454
455 /*
456 * Handle restarting Insert mode.
457 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
458 * restart_edit non-zero, and something in the stuff buffer.
459 */
460 if (restart_edit != 0 && stuff_empty())
461 {
462#ifdef FEAT_MOUSE
463 /*
464 * After a paste we consider text typed to be part of the insert for
465 * the pasted text. You can backspace over the pasted text too.
466 */
467 if (where_paste_started.lnum)
468 arrow_used = FALSE;
469 else
470#endif
471 arrow_used = TRUE;
472 restart_edit = 0;
473
474 /*
475 * If the cursor was after the end-of-line before the CTRL-O and it is
476 * now at the end-of-line, put it after the end-of-line (this is not
477 * correct in very rare cases).
478 * Also do this if curswant is greater than the current virtual
479 * column. Eg after "^O$" or "^O80|".
480 */
481 validate_virtcol();
482 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000483 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 || curwin->w_curswant > curwin->w_virtcol)
485 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
486 {
487 if (ptr[1] == NUL)
488 ++curwin->w_cursor.col;
489#ifdef FEAT_MBYTE
490 else if (has_mbyte)
491 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000492 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 if (ptr[i] == NUL)
494 curwin->w_cursor.col += i;
495 }
496#endif
497 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000498 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 }
500 else
501 arrow_used = FALSE;
502
503 /* we are in insert mode now, don't need to start it anymore */
504 need_start_insertmode = FALSE;
505
506 /* Need to save the line for undo before inserting the first char. */
507 ins_need_undo = TRUE;
508
509#ifdef FEAT_MOUSE
510 where_paste_started.lnum = 0;
511#endif
512#ifdef FEAT_CINDENT
513 can_cindent = TRUE;
514#endif
515#ifdef FEAT_FOLDING
516 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
517 * restarting. */
518 if (!p_im && did_restart_edit == 0)
519 foldOpenCursor();
520#endif
521
522 /*
523 * If 'showmode' is set, show the current (insert/replace/..) mode.
524 * A warning message for changing a readonly file is given here, before
525 * actually changing anything. It's put after the mode, if any.
526 */
527 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000528 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 i = showmode();
530
531 if (!p_im && did_restart_edit == 0)
532 change_warning(i + 1);
533
534#ifdef CURSOR_SHAPE
535 ui_cursor_shape(); /* may show different cursor shape */
536#endif
537#ifdef FEAT_DIGRAPHS
538 do_digraph(-1); /* clear digraphs */
539#endif
540
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000541 /*
542 * Get the current length of the redo buffer, those characters have to be
543 * skipped if we want to get to the inserted characters.
544 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 ptr = get_inserted();
546 if (ptr == NULL)
547 new_insert_skip = 0;
548 else
549 {
550 new_insert_skip = (int)STRLEN(ptr);
551 vim_free(ptr);
552 }
553
554 old_indent = 0;
555
556 /*
557 * Main loop in Insert mode: repeat until Insert mode is left.
558 */
559 for (;;)
560 {
561#ifdef FEAT_RIGHTLEFT
562 if (!revins_legal)
563 revins_scol = -1; /* reset on illegal motions */
564 else
565 revins_legal = 0;
566#endif
567 if (arrow_used) /* don't repeat insert when arrow key used */
568 count = 0;
569
570 if (stop_insert_mode)
571 {
572 /* ":stopinsert" used or 'insertmode' reset */
573 count = 0;
574 goto doESCkey;
575 }
576
577 /* set curwin->w_curswant for next K_DOWN or K_UP */
578 if (!arrow_used)
579 curwin->w_set_curswant = TRUE;
580
581 /* If there is no typeahead may check for timestamps (e.g., for when a
582 * menu invoked a shell command). */
583 if (stuff_empty())
584 {
585 did_check_timestamps = FALSE;
586 if (need_check_timestamps)
587 check_timestamps(FALSE);
588 }
589
590 /*
591 * When emsg() was called msg_scroll will have been set.
592 */
593 msg_scroll = FALSE;
594
595#ifdef FEAT_GUI
596 /* When 'mousefocus' is set a mouse movement may have taken us to
597 * another window. "need_mouse_correct" may then be set because of an
598 * autocommand. */
599 if (need_mouse_correct)
600 gui_mouse_correct();
601#endif
602
603#ifdef FEAT_FOLDING
604 /* Open fold at the cursor line, according to 'foldopen'. */
605 if (fdo_flags & FDO_INSERT)
606 foldOpenCursor();
607 /* Close folds where the cursor isn't, according to 'foldclose' */
608 if (!char_avail())
609 foldCheckClose();
610#endif
611
612 /*
613 * If we inserted a character at the last position of the last line in
614 * the window, scroll the window one line up. This avoids an extra
615 * redraw.
616 * This is detected when the cursor column is smaller after inserting
617 * something.
618 * Don't do this when the topline changed already, it has
619 * already been adjusted (by insertchar() calling open_line())).
620 */
621 if (curbuf->b_mod_set
622 && curwin->w_p_wrap
623 && !did_backspace
624 && curwin->w_topline == old_topline
625#ifdef FEAT_DIFF
626 && curwin->w_topfill == old_topfill
627#endif
628 )
629 {
630 mincol = curwin->w_wcol;
631 validate_cursor_col();
632
633 if ((int)curwin->w_wcol < (int)mincol - curbuf->b_p_ts
634 && curwin->w_wrow == W_WINROW(curwin)
635 + curwin->w_height - 1 - p_so
636 && (curwin->w_cursor.lnum != curwin->w_topline
637#ifdef FEAT_DIFF
638 || curwin->w_topfill > 0
639#endif
640 ))
641 {
642#ifdef FEAT_DIFF
643 if (curwin->w_topfill > 0)
644 --curwin->w_topfill;
645 else
646#endif
647#ifdef FEAT_FOLDING
648 if (hasFolding(curwin->w_topline, NULL, &old_topline))
649 set_topline(curwin, old_topline + 1);
650 else
651#endif
652 set_topline(curwin, curwin->w_topline + 1);
653 }
654 }
655
656 /* May need to adjust w_topline to show the cursor. */
657 update_topline();
658
659 did_backspace = FALSE;
660
661 validate_cursor(); /* may set must_redraw */
662
663 /*
664 * Redraw the display when no characters are waiting.
665 * Also shows mode, ruler and positions cursor.
666 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000667 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668
669#ifdef FEAT_SCROLLBIND
670 if (curwin->w_p_scb)
671 do_check_scrollbind(TRUE);
672#endif
673
674 update_curswant();
675 old_topline = curwin->w_topline;
676#ifdef FEAT_DIFF
677 old_topfill = curwin->w_topfill;
678#endif
679
680#ifdef USE_ON_FLY_SCROLL
681 dont_scroll = FALSE; /* allow scrolling here */
682#endif
683
684 /*
685 * Get a character for Insert mode.
686 */
687 lastc = c; /* remember previous char for CTRL-D */
688 c = safe_vgetc();
689
690#ifdef FEAT_RIGHTLEFT
691 if (p_hkmap && KeyTyped)
692 c = hkmap(c); /* Hebrew mode mapping */
693#endif
694#ifdef FEAT_FKMAP
695 if (p_fkmap && KeyTyped)
696 c = fkmap(c); /* Farsi mode mapping */
697#endif
698
699#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000700 /*
701 * Special handling of keys while the popup menu is visible or wanted
702 * and the cursor is still in the completed word.
703 */
704 if (compl_started && pum_wanted() && curwin->w_cursor.col >= compl_col)
Bram Moolenaara6557602006-02-04 22:43:20 +0000705 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000706 /* BS: Delete one character from "compl_leader". */
707 if ((c == K_BS || c == Ctrl_H)
708 && curwin->w_cursor.col > compl_col && ins_compl_bs())
Bram Moolenaara6557602006-02-04 22:43:20 +0000709 continue;
710
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000711 /* When no match was selected or it was edited. */
712 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000713 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000714 /* CTRL-L: Add one character from the current match to
715 * "compl_leader". */
716 if (c == Ctrl_L)
717 {
718 ins_compl_addfrommatch();
719 continue;
720 }
721
Bram Moolenaardf1bdc92006-02-23 21:32:16 +0000722 /* A printable, non-white character: Add to "compl_leader". */
723 if (vim_isprintc(c) && !vim_iswhite(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000724 {
725 ins_compl_addleader(c);
726 continue;
727 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000728
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000729 /* Pressing CTRL-Y selects the current match. */
730 if (c == Ctrl_Y)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000731 {
732 ins_compl_delete();
733 ins_compl_insert();
734 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000735 }
736 }
737
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
739 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000740 compl_get_longest = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +0000741 if (c != K_IGNORE && ins_compl_prep(c))
742 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743#endif
744
Bram Moolenaar488c6512005-08-11 20:09:58 +0000745 /* CTRL-\ CTRL-N goes to Normal mode,
746 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
747 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 if (c == Ctrl_BSL)
749 {
750 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000751 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 ++no_mapping;
753 ++allow_keys;
754 c = safe_vgetc();
755 --no_mapping;
756 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000757 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000759 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 vungetc(c);
761 c = Ctrl_BSL;
762 }
763 else if (c == Ctrl_G && p_im)
764 continue;
765 else
766 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000767 if (c == Ctrl_O)
768 {
769 ins_ctrl_o();
770 ins_at_eol = FALSE; /* cursor keeps its column */
771 nomove = TRUE;
772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 count = 0;
774 goto doESCkey;
775 }
776 }
777
778#ifdef FEAT_DIGRAPHS
779 c = do_digraph(c);
780#endif
781
782#ifdef FEAT_INS_EXPAND
783 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
784 goto docomplete;
785#endif
786 if (c == Ctrl_V || c == Ctrl_Q)
787 {
788 ins_ctrl_v();
789 c = Ctrl_V; /* pretend CTRL-V is last typed character */
790 continue;
791 }
792
793#ifdef FEAT_CINDENT
794 if (cindent_on()
795# ifdef FEAT_INS_EXPAND
796 && ctrl_x_mode == 0
797# endif
798 )
799 {
800 /* A key name preceded by a bang means this key is not to be
801 * inserted. Skip ahead to the re-indenting below.
802 * A key name preceded by a star means that indenting has to be
803 * done before inserting the key. */
804 line_is_white = inindent(0);
805 if (in_cinkeys(c, '!', line_is_white))
806 goto force_cindent;
807 if (can_cindent && in_cinkeys(c, '*', line_is_white)
808 && stop_arrow() == OK)
809 do_c_expr_indent();
810 }
811#endif
812
813#ifdef FEAT_RIGHTLEFT
814 if (curwin->w_p_rl)
815 switch (c)
816 {
817 case K_LEFT: c = K_RIGHT; break;
818 case K_S_LEFT: c = K_S_RIGHT; break;
819 case K_C_LEFT: c = K_C_RIGHT; break;
820 case K_RIGHT: c = K_LEFT; break;
821 case K_S_RIGHT: c = K_S_LEFT; break;
822 case K_C_RIGHT: c = K_C_LEFT; break;
823 }
824#endif
825
826#ifdef FEAT_VISUAL
827 /*
828 * If 'keymodel' contains "startsel", may start selection. If it
829 * does, a CTRL-O and c will be stuffed, we need to get these
830 * characters.
831 */
832 if (ins_start_select(c))
833 continue;
834#endif
835
836 /*
837 * The big switch to handle a character in insert mode.
838 */
839 switch (c)
840 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000841 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 if (echeck_abbr(ESC + ABBR_OFF))
843 break;
844 /*FALLTHROUGH*/
845
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000846 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847#ifdef FEAT_CMDWIN
848 if (c == Ctrl_C && cmdwin_type != 0)
849 {
850 /* Close the cmdline window. */
851 cmdwin_result = K_IGNORE;
852 got_int = FALSE; /* don't stop executing autocommands et al. */
853 goto doESCkey;
854 }
855#endif
856
857#ifdef UNIX
858do_intr:
859#endif
860 /* when 'insertmode' set, and not halfway a mapping, don't leave
861 * Insert mode */
862 if (goto_im())
863 {
864 if (got_int)
865 {
866 (void)vgetc(); /* flush all buffers */
867 got_int = FALSE;
868 }
869 else
870 vim_beep();
871 break;
872 }
873doESCkey:
874 /*
875 * This is the ONLY return from edit()!
876 */
877 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
878 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000879 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 o_lnum = curwin->w_cursor.lnum;
881
Bram Moolenaar488c6512005-08-11 20:09:58 +0000882 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000883 {
884#ifdef FEAT_AUTOCMD
885 if (cmdchar != 'r' && cmdchar != 'v')
886 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
887 FALSE, curbuf);
888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 continue;
892
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000893 case Ctrl_Z: /* suspend when 'insertmode' set */
894 if (!p_im)
895 goto normalchar; /* insert CTRL-Z as normal char */
896 stuffReadbuff((char_u *)":st\r");
897 c = Ctrl_O;
898 /*FALLTHROUGH*/
899
900 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000901#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000902 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000903 goto docomplete;
904#endif
905 if (echeck_abbr(Ctrl_O + ABBR_OFF))
906 break;
907 ins_ctrl_o();
908 count = 0;
909 goto doESCkey;
910
Bram Moolenaar572cb562005-08-05 21:35:02 +0000911 case K_INS: /* toggle insert/replace mode */
912 case K_KINS:
913 ins_insert(replaceState);
914 break;
915
916 case K_SELECT: /* end of Select mode mapping - ignore */
917 break;
918
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000919#ifdef FEAT_SNIFF
920 case K_SNIFF: /* Sniff command received */
921 stuffcharReadbuff(K_SNIFF);
922 goto doESCkey;
923#endif
924
925 case K_HELP: /* Help key works like <ESC> <Help> */
926 case K_F1:
927 case K_XF1:
928 stuffcharReadbuff(K_HELP);
929 if (p_im)
930 need_start_insertmode = TRUE;
931 goto doESCkey;
932
933#ifdef FEAT_NETBEANS_INTG
934 case K_F21: /* NetBeans command */
935 ++no_mapping; /* don't map the next key hits */
936 i = safe_vgetc();
937 --no_mapping;
938 netbeans_keycommand(i);
939 break;
940#endif
941
942 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 case NUL:
944 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000945 /* For ^@ the trailing ESC will end the insert, unless there is an
946 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
948 && c != Ctrl_A && !p_im)
949 goto doESCkey; /* quit insert mode */
950 inserted_space = FALSE;
951 break;
952
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000953 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 ins_reg();
955 auto_format(FALSE, TRUE);
956 inserted_space = FALSE;
957 break;
958
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000959 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 ins_ctrl_g();
961 break;
962
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000963 case Ctrl_HAT: /* switch input mode and/or langmap */
964 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 break;
966
967#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000968 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 if (!p_ari)
970 goto normalchar;
971 ins_ctrl_();
972 break;
973#endif
974
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000975 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
977 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
978 goto docomplete;
979#endif
980 /* FALLTHROUGH */
981
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000982 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983# ifdef FEAT_INS_EXPAND
984 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
985 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000986 if (has_compl_option(FALSE))
987 goto docomplete;
988 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 }
990# endif
991 ins_shift(c, lastc);
992 auto_format(FALSE, TRUE);
993 inserted_space = FALSE;
994 break;
995
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000996 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 case K_KDEL:
998 ins_del();
999 auto_format(FALSE, TRUE);
1000 break;
1001
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001002 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 case Ctrl_H:
1004 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1005 auto_format(FALSE, TRUE);
1006 break;
1007
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001008 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1010 auto_format(FALSE, TRUE);
1011 break;
1012
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001013 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001014# ifdef FEAT_COMPL_FUNC
1015 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001016 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001017 goto docomplete;
1018# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1020 auto_format(FALSE, TRUE);
1021 inserted_space = FALSE;
1022 break;
1023
1024#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001025 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 case K_LEFTMOUSE_NM:
1027 case K_LEFTDRAG:
1028 case K_LEFTRELEASE:
1029 case K_LEFTRELEASE_NM:
1030 case K_MIDDLEMOUSE:
1031 case K_MIDDLEDRAG:
1032 case K_MIDDLERELEASE:
1033 case K_RIGHTMOUSE:
1034 case K_RIGHTDRAG:
1035 case K_RIGHTRELEASE:
1036 case K_X1MOUSE:
1037 case K_X1DRAG:
1038 case K_X1RELEASE:
1039 case K_X2MOUSE:
1040 case K_X2DRAG:
1041 case K_X2RELEASE:
1042 ins_mouse(c);
1043 break;
1044
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001045 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 ins_mousescroll(FALSE);
1047 break;
1048
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001049 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 ins_mousescroll(TRUE);
1051 break;
1052#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001053#ifdef FEAT_GUI_TABLINE
1054 case K_TABLINE:
1055 case K_TABMENU:
1056 ins_tabline(c);
1057 break;
1058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001060 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 break;
1062
Bram Moolenaar754b5602006-02-09 23:53:20 +00001063#ifdef FEAT_AUTOCMD
1064 case K_CURSORHOLD: /* Didn't type something for a while. */
1065 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1066 did_cursorhold = TRUE;
1067 break;
1068#endif
1069
Bram Moolenaar4770d092006-01-12 23:22:24 +00001070#ifdef FEAT_GUI_W32
1071 /* On Win32 ignore <M-F4>, we get it when closing the window was
1072 * cancelled. */
1073 case K_F4:
1074 if (mod_mask != MOD_MASK_ALT)
1075 goto normalchar;
1076 break;
1077#endif
1078
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079#ifdef FEAT_GUI
1080 case K_VER_SCROLLBAR:
1081 ins_scroll();
1082 break;
1083
1084 case K_HOR_SCROLLBAR:
1085 ins_horscroll();
1086 break;
1087#endif
1088
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001089 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 case K_S_HOME:
1092 case K_C_HOME:
1093 ins_home(c);
1094 break;
1095
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001096 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 case K_S_END:
1099 case K_C_END:
1100 ins_end(c);
1101 break;
1102
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001103 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001104 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1105 ins_s_left();
1106 else
1107 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 break;
1109
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001110 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 case K_C_LEFT:
1112 ins_s_left();
1113 break;
1114
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001115 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001116 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1117 ins_s_right();
1118 else
1119 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 break;
1121
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001122 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 case K_C_RIGHT:
1124 ins_s_right();
1125 break;
1126
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001127 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001128#ifdef FEAT_INS_EXPAND
1129 if (pum_visible())
1130 goto docomplete;
1131#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001132 if (mod_mask & MOD_MASK_SHIFT)
1133 ins_pageup();
1134 else
1135 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 break;
1137
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001138 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 case K_PAGEUP:
1140 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001141#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001142 if (pum_visible())
1143 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001144#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 ins_pageup();
1146 break;
1147
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001148 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001149#ifdef FEAT_INS_EXPAND
1150 if (pum_visible())
1151 goto docomplete;
1152#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001153 if (mod_mask & MOD_MASK_SHIFT)
1154 ins_pagedown();
1155 else
1156 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 break;
1158
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001159 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 case K_PAGEDOWN:
1161 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001162#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001163 if (pum_visible())
1164 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 ins_pagedown();
1167 break;
1168
1169#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001170 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 ins_drop();
1172 break;
1173#endif
1174
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001175 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 c = TAB;
1177 /* FALLTHROUGH */
1178
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001179 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1181 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1182 goto docomplete;
1183#endif
1184 inserted_space = FALSE;
1185 if (ins_tab())
1186 goto normalchar; /* insert TAB as a normal char */
1187 auto_format(FALSE, TRUE);
1188 break;
1189
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001190 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 c = CAR;
1192 /* FALLTHROUGH */
1193 case CAR:
1194 case NL:
1195#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1196 /* In a quickfix window a <CR> jumps to the error under the
1197 * cursor. */
1198 if (bt_quickfix(curbuf) && c == CAR)
1199 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001200 if (curwin->w_llist_ref == NULL) /* quickfix window */
1201 do_cmdline_cmd((char_u *)".cc");
1202 else /* location list window */
1203 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 break;
1205 }
1206#endif
1207#ifdef FEAT_CMDWIN
1208 if (cmdwin_type != 0)
1209 {
1210 /* Execute the command in the cmdline window. */
1211 cmdwin_result = CAR;
1212 goto doESCkey;
1213 }
1214#endif
1215 if (ins_eol(c) && !p_im)
1216 goto doESCkey; /* out of memory */
1217 auto_format(FALSE, FALSE);
1218 inserted_space = FALSE;
1219 break;
1220
1221#if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001222 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223# ifdef FEAT_INS_EXPAND
1224 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1225 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001226 if (has_compl_option(TRUE))
1227 goto docomplete;
1228 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 }
1230# endif
1231# ifdef FEAT_DIGRAPHS
1232 c = ins_digraph();
1233 if (c == NUL)
1234 break;
1235# endif
1236 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238
1239#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001240 case Ctrl_X: /* Enter CTRL-X mode */
1241 ins_ctrl_x();
1242 break;
1243
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001244 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 if (ctrl_x_mode != CTRL_X_TAGS)
1246 goto normalchar;
1247 goto docomplete;
1248
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001249 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 if (ctrl_x_mode != CTRL_X_FILES)
1251 goto normalchar;
1252 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001253
1254 case 's': /* Spelling completion after ^X */
1255 case Ctrl_S:
1256 if (ctrl_x_mode != CTRL_X_SPELL)
1257 goto normalchar;
1258 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259#endif
1260
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001261 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262#ifdef FEAT_INS_EXPAND
1263 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1264#endif
1265 {
1266 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1267 if (p_im)
1268 {
1269 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1270 break;
1271 goto doESCkey;
1272 }
1273 goto normalchar;
1274 }
1275#ifdef FEAT_INS_EXPAND
1276 /* FALLTHROUGH */
1277
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001278 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 case Ctrl_N:
1280 /* if 'complete' is empty then plain ^P is no longer special,
1281 * but it is under other ^X modes */
1282 if (*curbuf->b_p_cpt == NUL
1283 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001284 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 goto normalchar;
1286
1287docomplete:
1288 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001289 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 break;
1291#endif /* FEAT_INS_EXPAND */
1292
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001293 case Ctrl_Y: /* copy from previous line or scroll down */
1294 case Ctrl_E: /* copy from next line or scroll up */
1295 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 break;
1297
1298 default:
1299#ifdef UNIX
1300 if (c == intr_char) /* special interrupt char */
1301 goto do_intr;
1302#endif
1303
1304 /*
1305 * Insert a nomal character.
1306 */
1307normalchar:
1308#ifdef FEAT_SMARTINDENT
1309 /* Try to perform smart-indenting. */
1310 ins_try_si(c);
1311#endif
1312
1313 if (c == ' ')
1314 {
1315 inserted_space = TRUE;
1316#ifdef FEAT_CINDENT
1317 if (inindent(0))
1318 can_cindent = FALSE;
1319#endif
1320 if (Insstart_blank_vcol == MAXCOL
1321 && curwin->w_cursor.lnum == Insstart.lnum)
1322 Insstart_blank_vcol = get_nolist_virtcol();
1323 }
1324
1325 if (vim_iswordc(c) || !echeck_abbr(
1326#ifdef FEAT_MBYTE
1327 /* Add ABBR_OFF for characters above 0x100, this is
1328 * what check_abbr() expects. */
1329 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1330#endif
1331 c))
1332 {
1333 insert_special(c, FALSE, FALSE);
1334#ifdef FEAT_RIGHTLEFT
1335 revins_legal++;
1336 revins_chars++;
1337#endif
1338 }
1339
1340 auto_format(FALSE, TRUE);
1341
1342#ifdef FEAT_FOLDING
1343 /* When inserting a character the cursor line must never be in a
1344 * closed fold. */
1345 foldOpenCursor();
1346#endif
1347 break;
1348 } /* end of switch (c) */
1349
1350 /* If the cursor was moved we didn't just insert a space */
1351 if (arrow_used)
1352 inserted_space = FALSE;
1353
1354#ifdef FEAT_CINDENT
1355 if (can_cindent && cindent_on()
1356# ifdef FEAT_INS_EXPAND
1357 && ctrl_x_mode == 0
1358# endif
1359 )
1360 {
1361force_cindent:
1362 /*
1363 * Indent now if a key was typed that is in 'cinkeys'.
1364 */
1365 if (in_cinkeys(c, ' ', line_is_white))
1366 {
1367 if (stop_arrow() == OK)
1368 /* re-indent the current line */
1369 do_c_expr_indent();
1370 }
1371 }
1372#endif /* FEAT_CINDENT */
1373
1374 } /* for (;;) */
1375 /* NOTREACHED */
1376}
1377
1378/*
1379 * Redraw for Insert mode.
1380 * This is postponed until getting the next character to make '$' in the 'cpo'
1381 * option work correctly.
1382 * Only redraw when there are no characters available. This speeds up
1383 * inserting sequences of characters (e.g., for CTRL-R).
1384 */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001385/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001387ins_redraw(ready)
1388 int ready; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389{
1390 if (!char_avail())
1391 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00001392#ifdef FEAT_AUTOCMD
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001393 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1394 * visible, the command might delete it. */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001395 if (ready && has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001396 && !equalpos(last_cursormoved, curwin->w_cursor)
1397# ifdef FEAT_INS_EXPAND
1398 && !pum_visible()
1399# endif
1400 )
Bram Moolenaar754b5602006-02-09 23:53:20 +00001401 {
1402 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1403 last_cursormoved = curwin->w_cursor;
1404 }
1405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 if (must_redraw)
1407 update_screen(0);
1408 else if (clear_cmdline || redraw_cmdline)
1409 showmode(); /* clear cmdline and show mode */
1410 showruler(FALSE);
1411 setcursor();
1412 emsg_on_display = FALSE; /* may remove error message now */
1413 }
1414}
1415
1416/*
1417 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1418 */
1419 static void
1420ins_ctrl_v()
1421{
1422 int c;
1423
1424 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001425 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426
1427 if (redrawing() && !char_avail())
1428 edit_putchar('^', TRUE);
1429 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1430
1431#ifdef FEAT_CMDL_INFO
1432 add_to_showcmd_c(Ctrl_V);
1433#endif
1434
1435 c = get_literal();
1436#ifdef FEAT_CMDL_INFO
1437 clear_showcmd();
1438#endif
1439 insert_special(c, FALSE, TRUE);
1440#ifdef FEAT_RIGHTLEFT
1441 revins_chars++;
1442 revins_legal++;
1443#endif
1444}
1445
1446/*
1447 * Put a character directly onto the screen. It's not stored in a buffer.
1448 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1449 */
1450static int pc_status;
1451#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1452#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1453#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1454#define PC_STATUS_SET 3 /* pc_bytes was filled */
1455#ifdef FEAT_MBYTE
1456static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1457#else
1458static char_u pc_bytes[2]; /* saved bytes */
1459#endif
1460static int pc_attr;
1461static int pc_row;
1462static int pc_col;
1463
1464 void
1465edit_putchar(c, highlight)
1466 int c;
1467 int highlight;
1468{
1469 int attr;
1470
1471 if (ScreenLines != NULL)
1472 {
1473 update_topline(); /* just in case w_topline isn't valid */
1474 validate_cursor();
1475 if (highlight)
1476 attr = hl_attr(HLF_8);
1477 else
1478 attr = 0;
1479 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1480 pc_col = W_WINCOL(curwin);
1481#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1482 pc_status = PC_STATUS_UNSET;
1483#endif
1484#ifdef FEAT_RIGHTLEFT
1485 if (curwin->w_p_rl)
1486 {
1487 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1488# ifdef FEAT_MBYTE
1489 if (has_mbyte)
1490 {
1491 int fix_col = mb_fix_col(pc_col, pc_row);
1492
1493 if (fix_col != pc_col)
1494 {
1495 screen_putchar(' ', pc_row, fix_col, attr);
1496 --curwin->w_wcol;
1497 pc_status = PC_STATUS_RIGHT;
1498 }
1499 }
1500# endif
1501 }
1502 else
1503#endif
1504 {
1505 pc_col += curwin->w_wcol;
1506#ifdef FEAT_MBYTE
1507 if (mb_lefthalve(pc_row, pc_col))
1508 pc_status = PC_STATUS_LEFT;
1509#endif
1510 }
1511
1512 /* save the character to be able to put it back */
1513#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1514 if (pc_status == PC_STATUS_UNSET)
1515#endif
1516 {
1517 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1518 pc_status = PC_STATUS_SET;
1519 }
1520 screen_putchar(c, pc_row, pc_col, attr);
1521 }
1522}
1523
1524/*
1525 * Undo the previous edit_putchar().
1526 */
1527 void
1528edit_unputchar()
1529{
1530 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1531 {
1532#if defined(FEAT_MBYTE)
1533 if (pc_status == PC_STATUS_RIGHT)
1534 ++curwin->w_wcol;
1535 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1536 redrawWinline(curwin->w_cursor.lnum, FALSE);
1537 else
1538#endif
1539 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1540 }
1541}
1542
1543/*
1544 * Called when p_dollar is set: display a '$' at the end of the changed text
1545 * Only works when cursor is in the line that changes.
1546 */
1547 void
1548display_dollar(col)
1549 colnr_T col;
1550{
1551 colnr_T save_col;
1552
1553 if (!redrawing())
1554 return;
1555
1556 cursor_off();
1557 save_col = curwin->w_cursor.col;
1558 curwin->w_cursor.col = col;
1559#ifdef FEAT_MBYTE
1560 if (has_mbyte)
1561 {
1562 char_u *p;
1563
1564 /* If on the last byte of a multi-byte move to the first byte. */
1565 p = ml_get_curline();
1566 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1567 }
1568#endif
1569 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1570 if (curwin->w_wcol < W_WIDTH(curwin))
1571 {
1572 edit_putchar('$', FALSE);
1573 dollar_vcol = curwin->w_virtcol;
1574 }
1575 curwin->w_cursor.col = save_col;
1576}
1577
1578/*
1579 * Call this function before moving the cursor from the normal insert position
1580 * in insert mode.
1581 */
1582 static void
1583undisplay_dollar()
1584{
1585 if (dollar_vcol)
1586 {
1587 dollar_vcol = 0;
1588 redrawWinline(curwin->w_cursor.lnum, FALSE);
1589 }
1590}
1591
1592/*
1593 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1594 * Keep the cursor on the same character.
1595 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1596 * type == INDENT_DEC decrease indent (for CTRL-D)
1597 * type == INDENT_SET set indent to "amount"
1598 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1599 */
1600 void
1601change_indent(type, amount, round, replaced)
1602 int type;
1603 int amount;
1604 int round;
1605 int replaced; /* replaced character, put on replace stack */
1606{
1607 int vcol;
1608 int last_vcol;
1609 int insstart_less; /* reduction for Insstart.col */
1610 int new_cursor_col;
1611 int i;
1612 char_u *ptr;
1613 int save_p_list;
1614 int start_col;
1615 colnr_T vc;
1616#ifdef FEAT_VREPLACE
1617 colnr_T orig_col = 0; /* init for GCC */
1618 char_u *new_line, *orig_line = NULL; /* init for GCC */
1619
1620 /* VREPLACE mode needs to know what the line was like before changing */
1621 if (State & VREPLACE_FLAG)
1622 {
1623 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1624 orig_col = curwin->w_cursor.col;
1625 }
1626#endif
1627
1628 /* for the following tricks we don't want list mode */
1629 save_p_list = curwin->w_p_list;
1630 curwin->w_p_list = FALSE;
1631 vc = getvcol_nolist(&curwin->w_cursor);
1632 vcol = vc;
1633
1634 /*
1635 * For Replace mode we need to fix the replace stack later, which is only
1636 * possible when the cursor is in the indent. Remember the number of
1637 * characters before the cursor if it's possible.
1638 */
1639 start_col = curwin->w_cursor.col;
1640
1641 /* determine offset from first non-blank */
1642 new_cursor_col = curwin->w_cursor.col;
1643 beginline(BL_WHITE);
1644 new_cursor_col -= curwin->w_cursor.col;
1645
1646 insstart_less = curwin->w_cursor.col;
1647
1648 /*
1649 * If the cursor is in the indent, compute how many screen columns the
1650 * cursor is to the left of the first non-blank.
1651 */
1652 if (new_cursor_col < 0)
1653 vcol = get_indent() - vcol;
1654
1655 if (new_cursor_col > 0) /* can't fix replace stack */
1656 start_col = -1;
1657
1658 /*
1659 * Set the new indent. The cursor will be put on the first non-blank.
1660 */
1661 if (type == INDENT_SET)
1662 (void)set_indent(amount, SIN_CHANGED);
1663 else
1664 {
1665#ifdef FEAT_VREPLACE
1666 int save_State = State;
1667
1668 /* Avoid being called recursively. */
1669 if (State & VREPLACE_FLAG)
1670 State = INSERT;
1671#endif
1672 shift_line(type == INDENT_DEC, round, 1);
1673#ifdef FEAT_VREPLACE
1674 State = save_State;
1675#endif
1676 }
1677 insstart_less -= curwin->w_cursor.col;
1678
1679 /*
1680 * Try to put cursor on same character.
1681 * If the cursor is at or after the first non-blank in the line,
1682 * compute the cursor column relative to the column of the first
1683 * non-blank character.
1684 * If we are not in insert mode, leave the cursor on the first non-blank.
1685 * If the cursor is before the first non-blank, position it relative
1686 * to the first non-blank, counted in screen columns.
1687 */
1688 if (new_cursor_col >= 0)
1689 {
1690 /*
1691 * When changing the indent while the cursor is touching it, reset
1692 * Insstart_col to 0.
1693 */
1694 if (new_cursor_col == 0)
1695 insstart_less = MAXCOL;
1696 new_cursor_col += curwin->w_cursor.col;
1697 }
1698 else if (!(State & INSERT))
1699 new_cursor_col = curwin->w_cursor.col;
1700 else
1701 {
1702 /*
1703 * Compute the screen column where the cursor should be.
1704 */
1705 vcol = get_indent() - vcol;
1706 curwin->w_virtcol = (vcol < 0) ? 0 : vcol;
1707
1708 /*
1709 * Advance the cursor until we reach the right screen column.
1710 */
1711 vcol = last_vcol = 0;
1712 new_cursor_col = -1;
1713 ptr = ml_get_curline();
1714 while (vcol <= (int)curwin->w_virtcol)
1715 {
1716 last_vcol = vcol;
1717#ifdef FEAT_MBYTE
1718 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001719 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 else
1721#endif
1722 ++new_cursor_col;
1723 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1724 }
1725 vcol = last_vcol;
1726
1727 /*
1728 * May need to insert spaces to be able to position the cursor on
1729 * the right screen column.
1730 */
1731 if (vcol != (int)curwin->w_virtcol)
1732 {
1733 curwin->w_cursor.col = new_cursor_col;
1734 i = (int)curwin->w_virtcol - vcol;
1735 ptr = alloc(i + 1);
1736 if (ptr != NULL)
1737 {
1738 new_cursor_col += i;
1739 ptr[i] = NUL;
1740 while (--i >= 0)
1741 ptr[i] = ' ';
1742 ins_str(ptr);
1743 vim_free(ptr);
1744 }
1745 }
1746
1747 /*
1748 * When changing the indent while the cursor is in it, reset
1749 * Insstart_col to 0.
1750 */
1751 insstart_less = MAXCOL;
1752 }
1753
1754 curwin->w_p_list = save_p_list;
1755
1756 if (new_cursor_col <= 0)
1757 curwin->w_cursor.col = 0;
1758 else
1759 curwin->w_cursor.col = new_cursor_col;
1760 curwin->w_set_curswant = TRUE;
1761 changed_cline_bef_curs();
1762
1763 /*
1764 * May have to adjust the start of the insert.
1765 */
1766 if (State & INSERT)
1767 {
1768 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1769 {
1770 if ((int)Insstart.col <= insstart_less)
1771 Insstart.col = 0;
1772 else
1773 Insstart.col -= insstart_less;
1774 }
1775 if ((int)ai_col <= insstart_less)
1776 ai_col = 0;
1777 else
1778 ai_col -= insstart_less;
1779 }
1780
1781 /*
1782 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1783 * If the number of characters before the cursor decreased, need to pop a
1784 * few characters from the replace stack.
1785 * If the number of characters before the cursor increased, need to push a
1786 * few NULs onto the replace stack.
1787 */
1788 if (REPLACE_NORMAL(State) && start_col >= 0)
1789 {
1790 while (start_col > (int)curwin->w_cursor.col)
1791 {
1792 replace_join(0); /* remove a NUL from the replace stack */
1793 --start_col;
1794 }
1795 while (start_col < (int)curwin->w_cursor.col || replaced)
1796 {
1797 replace_push(NUL);
1798 if (replaced)
1799 {
1800 replace_push(replaced);
1801 replaced = NUL;
1802 }
1803 ++start_col;
1804 }
1805 }
1806
1807#ifdef FEAT_VREPLACE
1808 /*
1809 * For VREPLACE mode, we also have to fix the replace stack. In this case
1810 * it is always possible because we backspace over the whole line and then
1811 * put it back again the way we wanted it.
1812 */
1813 if (State & VREPLACE_FLAG)
1814 {
1815 /* If orig_line didn't allocate, just return. At least we did the job,
1816 * even if you can't backspace. */
1817 if (orig_line == NULL)
1818 return;
1819
1820 /* Save new line */
1821 new_line = vim_strsave(ml_get_curline());
1822 if (new_line == NULL)
1823 return;
1824
1825 /* We only put back the new line up to the cursor */
1826 new_line[curwin->w_cursor.col] = NUL;
1827
1828 /* Put back original line */
1829 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1830 curwin->w_cursor.col = orig_col;
1831
1832 /* Backspace from cursor to start of line */
1833 backspace_until_column(0);
1834
1835 /* Insert new stuff into line again */
1836 ins_bytes(new_line);
1837
1838 vim_free(new_line);
1839 }
1840#endif
1841}
1842
1843/*
1844 * Truncate the space at the end of a line. This is to be used only in an
1845 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1846 * modes.
1847 */
1848 void
1849truncate_spaces(line)
1850 char_u *line;
1851{
1852 int i;
1853
1854 /* find start of trailing white space */
1855 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1856 {
1857 if (State & REPLACE_FLAG)
1858 replace_join(0); /* remove a NUL from the replace stack */
1859 }
1860 line[i + 1] = NUL;
1861}
1862
1863#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1864 || defined(FEAT_COMMENTS) || defined(PROTO)
1865/*
1866 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1867 * modes correctly. May also be used when not in insert mode at all.
1868 */
1869 void
1870backspace_until_column(col)
1871 int col;
1872{
1873 while ((int)curwin->w_cursor.col > col)
1874 {
1875 curwin->w_cursor.col--;
1876 if (State & REPLACE_FLAG)
1877 replace_do_bs();
1878 else
1879 (void)del_char(FALSE);
1880 }
1881}
1882#endif
1883
1884#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1885/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001886 * CTRL-X pressed in Insert mode.
1887 */
1888 static void
1889ins_ctrl_x()
1890{
1891 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
1892 * CTRL-V works like CTRL-N */
1893 if (ctrl_x_mode != CTRL_X_CMDLINE)
1894 {
1895 /* if the next ^X<> won't ADD nothing, then reset
1896 * compl_cont_status */
1897 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001898 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001899 else
1900 compl_cont_status = 0;
1901 /* We're not sure which CTRL-X mode it will be yet */
1902 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
1903 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
1904 edit_submode_pre = NULL;
1905 showmode();
1906 }
1907}
1908
1909/*
1910 * Return TRUE if the 'dict' or 'tsr' option can be used.
1911 */
1912 static int
1913has_compl_option(dict_opt)
1914 int dict_opt;
1915{
Bram Moolenaar0b238792006-03-02 22:49:12 +00001916 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00001917# ifdef FEAT_SPELL
1918 && !curwin->w_p_spell
1919# endif
1920 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001921 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
1922 {
1923 ctrl_x_mode = 0;
1924 edit_submode = NULL;
1925 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
1926 : (char_u *)_("'thesaurus' option is empty"),
1927 hl_attr(HLF_E));
1928 if (emsg_silent == 0)
1929 {
1930 vim_beep();
1931 setcursor();
1932 out_flush();
1933 ui_delay(2000L, FALSE);
1934 }
1935 return FALSE;
1936 }
1937 return TRUE;
1938}
1939
1940/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
1942 * This depends on the current mode.
1943 */
1944 int
1945vim_is_ctrl_x_key(c)
1946 int c;
1947{
1948 /* Always allow ^R - let it's results then be checked */
1949 if (c == Ctrl_R)
1950 return TRUE;
1951
Bram Moolenaare3226be2005-12-18 22:10:00 +00001952 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001953 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00001954 return TRUE;
1955
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 switch (ctrl_x_mode)
1957 {
1958 case 0: /* Not in any CTRL-X mode */
1959 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
1960 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001961 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
1963 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
1964 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00001965 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
1966 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 case CTRL_X_SCROLL:
1968 return (c == Ctrl_Y || c == Ctrl_E);
1969 case CTRL_X_WHOLE_LINE:
1970 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
1971 case CTRL_X_FILES:
1972 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
1973 case CTRL_X_DICTIONARY:
1974 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
1975 case CTRL_X_THESAURUS:
1976 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
1977 case CTRL_X_TAGS:
1978 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
1979#ifdef FEAT_FIND_ID
1980 case CTRL_X_PATH_PATTERNS:
1981 return (c == Ctrl_P || c == Ctrl_N);
1982 case CTRL_X_PATH_DEFINES:
1983 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
1984#endif
1985 case CTRL_X_CMDLINE:
1986 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
1987 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001988#ifdef FEAT_COMPL_FUNC
1989 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001990 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001991 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001992 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001993#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00001994 case CTRL_X_SPELL:
1995 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 }
1997 EMSG(_(e_internal));
1998 return FALSE;
1999}
2000
2001/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002002 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 * case of the originally typed text is used, and the case of the completed
2004 * text is infered, ie this tries to work out what case you probably wanted
2005 * the rest of the word to be in -- webb
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002006 * TODO: make this work for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 */
2008 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002009ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 char_u *str;
2011 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002012 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 char_u *fname;
2014 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002015 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016{
2017 int has_lower = FALSE;
2018 int was_letter = FALSE;
2019 int idx;
2020
2021 if (p_ic && curbuf->b_p_inf && len < IOSIZE)
2022 {
2023 /* Infer case of completed part -- webb */
2024 /* Use IObuff, str would change text in buffer! */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002025 vim_strncpy(IObuff, str, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026
2027 /* Rule 1: Were any chars converted to lower? */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002028 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002030 if (islower(compl_orig_text[idx]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 {
2032 has_lower = TRUE;
2033 if (isupper(IObuff[idx]))
2034 {
2035 /* Rule 1 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002036 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 IObuff[idx] = TOLOWER_LOC(IObuff[idx]);
2038 break;
2039 }
2040 }
2041 }
2042
2043 /*
2044 * Rule 2: No lower case, 2nd consecutive letter converted to
2045 * upper case.
2046 */
2047 if (!has_lower)
2048 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002049 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002051 if (was_letter && isupper(compl_orig_text[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 && islower(IObuff[idx]))
2053 {
2054 /* Rule 2 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002055 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 IObuff[idx] = TOUPPER_LOC(IObuff[idx]);
2057 break;
2058 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002059 was_letter = isalpha(compl_orig_text[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 }
2061 }
2062
2063 /* Copy the original case of the part we typed */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002064 STRNCPY(IObuff, compl_orig_text, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065
Bram Moolenaar39f05632006-03-19 22:15:26 +00002066 return ins_compl_add(IObuff, len, icase, fname, NULL, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002068 return ins_compl_add(str, len, icase, fname, NULL, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069}
2070
2071/*
2072 * Add a match to the list of matches.
2073 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002074 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002075 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002077 int
Bram Moolenaar39f05632006-03-19 22:15:26 +00002078ins_compl_add(str, len, icase, fname, cptext, cdir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 char_u *str;
2080 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002081 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 char_u *fname;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002083 char_u **cptext; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002084 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002085 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002087 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002088 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089
2090 ui_breakcheck();
2091 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002092 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 if (len < 0)
2094 len = (int)STRLEN(str);
2095
2096 /*
2097 * If the same match is already present, don't add it.
2098 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002099 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002101 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 do
2103 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002104 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002105 && ins_compl_equal(match, str, len)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002106 && match->cp_str[len] == NUL)
2107 return NOTDONE;
2108 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002109 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 }
2111
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002112 /* Remove any popup menu before changing the list of matches. */
2113 ins_compl_del_pum();
2114
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 /*
2116 * Allocate a new match structure.
2117 * Copy the values to the new match structure.
2118 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002119 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002121 return FAIL;
2122 match->cp_number = -1;
2123 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002124 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002125 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 {
2127 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002128 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002130 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002131
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002133 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2135 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002136 if (fname != NULL
2137 && compl_curr_match
2138 && compl_curr_match->cp_fname != NULL
2139 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002140 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002141 else if (fname != NULL)
2142 {
2143 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002144 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002147 match->cp_fname = NULL;
2148 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002149
2150 if (cptext != NULL)
2151 {
2152 int i;
2153
2154 for (i = 0; i < CPT_COUNT; ++i)
2155 if (cptext[i] != NULL && *cptext[i] != NUL)
2156 match->cp_text[i] = vim_strsave(cptext[i]);
2157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158
2159 /*
2160 * Link the new match structure in the list of matches.
2161 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002162 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002163 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 else if (dir == FORWARD)
2165 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002166 match->cp_next = compl_curr_match->cp_next;
2167 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 }
2169 else /* BACKWARD */
2170 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002171 match->cp_next = compl_curr_match;
2172 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002174 if (match->cp_next)
2175 match->cp_next->cp_prev = match;
2176 if (match->cp_prev)
2177 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002179 compl_first_match = match;
2180 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002182 /*
2183 * Find the longest common string if still doing that.
2184 */
2185 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2186 ins_compl_longest_match(match);
2187
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 return OK;
2189}
2190
2191/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002192 * Return TRUE if "str[len]" matches with match->cp_str, considering
2193 * match->cp_icase.
2194 */
2195 static int
2196ins_compl_equal(match, str, len)
2197 compl_T *match;
2198 char_u *str;
2199 int len;
2200{
2201 if (match->cp_icase)
2202 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2203 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2204}
2205
2206/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002207 * Reduce the longest common string for match "match".
2208 */
2209 static void
2210ins_compl_longest_match(match)
2211 compl_T *match;
2212{
2213 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002214 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002215 int had_match;
2216
2217 if (compl_leader == NULL)
2218 /* First match, use it as a whole. */
2219 compl_leader = vim_strsave(match->cp_str);
2220 else
2221 {
2222 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002223 p = compl_leader;
2224 s = match->cp_str;
2225 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002226 {
2227#ifdef FEAT_MBYTE
2228 if (has_mbyte)
2229 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002230 c1 = mb_ptr2char(p);
2231 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002232 }
2233 else
2234#endif
2235 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002236 c1 = *p;
2237 c2 = *s;
2238 }
2239 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2240 : (c1 != c2))
2241 break;
2242#ifdef FEAT_MBYTE
2243 if (has_mbyte)
2244 {
2245 mb_ptr_adv(p);
2246 mb_ptr_adv(s);
2247 }
2248 else
2249#endif
2250 {
2251 ++p;
2252 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002253 }
2254 }
2255
2256 if (*p != NUL)
2257 {
2258 /* Leader was shortened, need to change the inserted text. */
2259 *p = NUL;
2260 had_match = (curwin->w_cursor.col > compl_col);
2261 ins_compl_delete();
2262 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2263 ins_redraw(FALSE);
2264
2265 /* When the match isn't there (to avoid matching itself) remove it
2266 * again after redrawing. */
2267 if (!had_match)
2268 ins_compl_delete();
2269 }
2270
2271 compl_used_match = FALSE;
2272 }
2273}
2274
2275/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 * Add an array of matches to the list of matches.
2277 * Frees matches[].
2278 */
2279 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002280ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 int num_matches;
2282 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002283 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284{
2285 int i;
2286 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002287 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288
Bram Moolenaar572cb562005-08-05 21:35:02 +00002289 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002290 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar39f05632006-03-19 22:15:26 +00002291 NULL, NULL, dir, 0)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002293 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 FreeWild(num_matches, matches);
2295}
2296
2297/* Make the completion list cyclic.
2298 * Return the number of matches (excluding the original).
2299 */
2300 static int
2301ins_compl_make_cyclic()
2302{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002303 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 int count = 0;
2305
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002306 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 {
2308 /*
2309 * Find the end of the list.
2310 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002311 match = compl_first_match;
2312 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002313 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002315 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 ++count;
2317 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002318 match->cp_next = compl_first_match;
2319 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 }
2321 return count;
2322}
2323
Bram Moolenaara94bc432006-03-10 21:42:59 +00002324/*
2325 * Start completion for the complete() function.
2326 * "startcol" is where the matched text starts (1 is first column).
2327 * "list" is the list of matches.
2328 */
2329 void
2330set_completion(startcol, list)
2331 int startcol;
2332 list_T *list;
2333{
2334 /* If already doing completions stop it. */
2335 if (ctrl_x_mode != 0)
2336 ins_compl_prep(' ');
2337 ins_compl_clear();
2338
2339 if (stop_arrow() == FAIL)
2340 return;
2341
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002342 if (startcol > (int)curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002343 startcol = curwin->w_cursor.col;
2344 compl_col = startcol;
2345 compl_length = curwin->w_cursor.col - startcol;
2346 /* compl_pattern doesn't need to be set */
2347 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2348 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar39f05632006-03-19 22:15:26 +00002349 -1, FALSE, NULL, NULL, 0, ORIGINAL_TEXT) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002350 return;
2351
2352 /* Handle like dictionary completion. */
2353 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2354
2355 ins_compl_add_list(list);
2356 compl_matches = ins_compl_make_cyclic();
2357 compl_started = TRUE;
2358 compl_used_match = TRUE;
2359
2360 compl_curr_match = compl_first_match;
2361 ins_complete(Ctrl_N);
2362 out_flush();
2363}
2364
2365
Bram Moolenaar9372a112005-12-06 19:59:18 +00002366/* "compl_match_array" points the currently displayed list of entries in the
2367 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002368static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002369static int compl_match_arraysize;
2370
2371/*
2372 * Update the screen and when there is any scrolling remove the popup menu.
2373 */
2374 static void
2375ins_compl_upd_pum()
2376{
2377 int h;
2378
2379 if (compl_match_array != NULL)
2380 {
2381 h = curwin->w_cline_height;
2382 update_screen(0);
2383 if (h != curwin->w_cline_height)
2384 ins_compl_del_pum();
2385 }
2386}
2387
2388/*
2389 * Remove any popup menu.
2390 */
2391 static void
2392ins_compl_del_pum()
2393{
2394 if (compl_match_array != NULL)
2395 {
2396 pum_undisplay();
2397 vim_free(compl_match_array);
2398 compl_match_array = NULL;
2399 }
2400}
2401
2402/*
2403 * Return TRUE if the popup menu should be displayed.
2404 */
2405 static int
2406pum_wanted()
2407{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002408 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002409 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002410 return FALSE;
2411
2412 /* The display looks bad on a B&W display. */
2413 if (t_colors < 8
2414#ifdef FEAT_GUI
2415 && !gui.in_use
2416#endif
2417 )
2418 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002419 return TRUE;
2420}
2421
2422/*
2423 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002424 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002425 */
2426 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002427pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002428{
2429 compl_T *compl;
2430 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002431
2432 /* Don't display the popup menu if there are no matches or there is only
2433 * one (ignoring the original text). */
2434 compl = compl_first_match;
2435 i = 0;
2436 do
2437 {
2438 if (compl == NULL
2439 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2440 break;
2441 compl = compl->cp_next;
2442 } while (compl != compl_first_match);
2443
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002444 if (strstr((char *)p_cot, "menuone") != NULL)
2445 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002446 return (i >= 2);
2447}
2448
2449/*
2450 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002451 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002452 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002453 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002454ins_compl_show_pum()
2455{
2456 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002457 compl_T *shown_compl = NULL;
2458 int did_find_shown_match = FALSE;
2459 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002460 int i;
2461 int cur = -1;
2462 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002463 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002464
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002465 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002466 return;
2467
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002468#if defined(FEAT_EVAL)
2469 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2470 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2471#endif
2472
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002473 /* Update the screen before drawing the popup menu over it. */
2474 update_screen(0);
2475
2476 if (compl_match_array == NULL)
2477 {
2478 /* Need to build the popup menu list. */
2479 compl_match_arraysize = 0;
2480 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002481 if (compl_leader != NULL)
2482 lead_len = STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002483 do
2484 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002485 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2486 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002487 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002488 ++compl_match_arraysize;
2489 compl = compl->cp_next;
2490 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002491 if (compl_match_arraysize == 0)
2492 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002493 compl_match_array = (pumitem_T *)alloc_clear(
2494 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002495 * compl_match_arraysize));
2496 if (compl_match_array != NULL)
2497 {
2498 i = 0;
2499 compl = compl_first_match;
2500 do
2501 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002502 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2503 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002504 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002505 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002506 if (!shown_match_ok)
2507 {
2508 if (compl == compl_shown_match || did_find_shown_match)
2509 {
2510 /* This item is the shown match or this is the
2511 * first displayed item after the shown match. */
2512 compl_shown_match = compl;
2513 did_find_shown_match = TRUE;
2514 shown_match_ok = TRUE;
2515 }
2516 else
2517 /* Remember this displayed match for when the
2518 * shown match is just below it. */
2519 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002520 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002521 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002522
2523 if (compl->cp_text[CPT_ABBR] != NULL)
2524 compl_match_array[i].pum_text =
2525 compl->cp_text[CPT_ABBR];
2526 else
2527 compl_match_array[i].pum_text = compl->cp_str;
2528 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2529 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2530 if (compl->cp_text[CPT_MENU] != NULL)
2531 compl_match_array[i++].pum_extra =
2532 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002533 else
2534 compl_match_array[i++].pum_extra = compl->cp_fname;
2535 }
2536
2537 if (compl == compl_shown_match)
2538 {
2539 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002540
2541 /* When the original text is the shown match don't set
2542 * compl_shown_match. */
2543 if (compl->cp_flags & ORIGINAL_TEXT)
2544 shown_match_ok = TRUE;
2545
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002546 if (!shown_match_ok && shown_compl != NULL)
2547 {
2548 /* The shown match isn't displayed, set it to the
2549 * previously displayed match. */
2550 compl_shown_match = shown_compl;
2551 shown_match_ok = TRUE;
2552 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002553 }
2554 compl = compl->cp_next;
2555 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002556
2557 if (!shown_match_ok) /* no displayed match at all */
2558 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002559 }
2560 }
2561 else
2562 {
2563 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002564 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00002565 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
2566 || compl_match_array[i].pum_text
2567 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaara6557602006-02-04 22:43:20 +00002568 break;
2569 cur = i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002570 }
2571
2572 if (compl_match_array != NULL)
2573 {
2574 /* Compute the screen column of the start of the completed text.
2575 * Use the cursor to get all wrapping and other settings right. */
2576 col = curwin->w_cursor.col;
2577 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00002578 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002579 curwin->w_cursor.col = col;
2580 }
2581}
2582
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583#define DICT_FIRST (1) /* use just first element in "dict" */
2584#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002585
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00002587 * Add any identifiers that match the given pattern in the list of dictionary
2588 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 */
2590 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00002591ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
2592 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002594 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002595 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002597 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 char_u *ptr;
2599 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 char_u **files;
2602 int count;
2603 int i;
2604 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002605 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606
Bram Moolenaar0b238792006-03-02 22:49:12 +00002607 if (*dict == NUL)
2608 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002609#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002610 /* When 'dictionary' is empty and spell checking is enabled use
2611 * "spell". */
2612 if (!thesaurus && curwin->w_p_spell)
2613 dict = (char_u *)"spell";
2614 else
2615#endif
2616 return;
2617 }
2618
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002620 if (buf == NULL)
2621 return;
2622
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 /* If 'infercase' is set, don't use 'smartcase' here */
2624 save_p_scs = p_scs;
2625 if (curbuf->b_p_inf)
2626 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002627
2628 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
2629 * to only match at the start of a line. Otherwise just match the
2630 * pattern. */
2631 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2632 {
2633 i = STRLEN(pat) + 8;
2634 ptr = alloc(i);
2635 if (ptr == NULL)
2636 return;
2637 vim_snprintf((char *)ptr, i, "^\\s*\\zs%s", pat);
2638 regmatch.regprog = vim_regcomp(ptr, p_magic ? RE_MAGIC : 0);
2639 vim_free(ptr);
2640 }
2641 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00002642 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002643 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002644 if (regmatch.regprog == NULL)
2645 goto theend;
2646 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002647
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2649 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002650 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 {
2652 /* copy one dictionary file name into buf */
2653 if (flags == DICT_EXACT)
2654 {
2655 count = 1;
2656 files = &dict;
2657 }
2658 else
2659 {
2660 /* Expand wildcards in the dictionary name, but do not allow
2661 * backticks (for security, the 'dict' option may have been set in
2662 * a modeline). */
2663 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002664# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002665 if (!thesaurus && STRCMP(buf, "spell") == 0)
2666 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002667 else
2668# endif
2669 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 || expand_wildcards(1, &buf, &count, &files,
2671 EW_FILE|EW_SILENT) != OK)
2672 count = 0;
2673 }
2674
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002675# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002676 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00002678 /* Complete from active spelling. Skip "\<" in the pattern, we
2679 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002680 if (pat[0] == '\\' && pat[1] == '<')
2681 ptr = pat + 2;
2682 else
2683 ptr = pat;
2684 spell_dump_compl(curbuf, ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002686 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002687# endif
Bram Moolenaar0b238792006-03-02 22:49:12 +00002688 {
2689 ins_compl_files(count, files, thesaurus, flags,
2690 &regmatch, buf, &dir);
2691 if (flags != DICT_EXACT)
2692 FreeWild(count, files);
2693 }
2694 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 break;
2696 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002697
2698theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 p_scs = save_p_scs;
2700 vim_free(regmatch.regprog);
2701 vim_free(buf);
2702}
2703
Bram Moolenaar0b238792006-03-02 22:49:12 +00002704 static void
2705ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
2706 int count;
2707 char_u **files;
2708 int thesaurus;
2709 int flags;
2710 regmatch_T *regmatch;
2711 char_u *buf;
2712 int *dir;
2713{
2714 char_u *ptr;
2715 int i;
2716 FILE *fp;
2717 int add_r;
2718
2719 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
2720 {
2721 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2722 if (flags != DICT_EXACT)
2723 {
2724 vim_snprintf((char *)IObuff, IOSIZE,
2725 _("Scanning dictionary: %s"), (char *)files[i]);
2726 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2727 }
2728
2729 if (fp != NULL)
2730 {
2731 /*
2732 * Read dictionary file line by line.
2733 * Check each line for a match.
2734 */
2735 while (!got_int && !compl_interrupted
2736 && !vim_fgets(buf, LSIZE, fp))
2737 {
2738 ptr = buf;
2739 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
2740 {
2741 ptr = regmatch->startp[0];
2742 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2743 ptr = find_line_end(ptr);
2744 else
2745 ptr = find_word_end(ptr);
2746 add_r = ins_compl_add_infercase(regmatch->startp[0],
2747 (int)(ptr - regmatch->startp[0]),
Bram Moolenaard289f132006-03-11 21:30:53 +00002748 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002749 if (thesaurus)
2750 {
2751 char_u *wstart;
2752
2753 /*
2754 * Add the other matches on the line
2755 */
2756 while (!got_int)
2757 {
2758 /* Find start of the next word. Skip white
2759 * space and punctuation. */
2760 ptr = find_word_start(ptr);
2761 if (*ptr == NUL || *ptr == NL)
2762 break;
2763 wstart = ptr;
2764
2765 /* Find end of the word and add it. */
2766#ifdef FEAT_MBYTE
2767 if (has_mbyte)
2768 /* Japanese words may have characters in
2769 * different classes, only separate words
2770 * with single-byte non-word characters. */
2771 while (*ptr != NUL)
2772 {
2773 int l = (*mb_ptr2len)(ptr);
2774
2775 if (l < 2 && !vim_iswordc(*ptr))
2776 break;
2777 ptr += l;
2778 }
2779 else
2780#endif
2781 ptr = find_word_end(ptr);
2782 add_r = ins_compl_add_infercase(wstart,
2783 (int)(ptr - wstart),
2784 p_ic, files[i], *dir, 0);
2785 }
2786 }
2787 if (add_r == OK)
2788 /* if dir was BACKWARD then honor it just once */
2789 *dir = FORWARD;
2790 else if (add_r == FAIL)
2791 break;
2792 /* avoid expensive call to vim_regexec() when at end
2793 * of line */
2794 if (*ptr == '\n' || got_int)
2795 break;
2796 }
2797 line_breakcheck();
2798 ins_compl_check_keys(50);
2799 }
2800 fclose(fp);
2801 }
2802 }
2803}
2804
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805/*
2806 * Find the start of the next word.
2807 * Returns a pointer to the first char of the word. Also stops at a NUL.
2808 */
2809 char_u *
2810find_word_start(ptr)
2811 char_u *ptr;
2812{
2813#ifdef FEAT_MBYTE
2814 if (has_mbyte)
2815 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002816 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 else
2818#endif
2819 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
2820 ++ptr;
2821 return ptr;
2822}
2823
2824/*
2825 * Find the end of the word. Assumes it starts inside a word.
2826 * Returns a pointer to just after the word.
2827 */
2828 char_u *
2829find_word_end(ptr)
2830 char_u *ptr;
2831{
2832#ifdef FEAT_MBYTE
2833 int start_class;
2834
2835 if (has_mbyte)
2836 {
2837 start_class = mb_get_class(ptr);
2838 if (start_class > 1)
2839 while (*ptr != NUL)
2840 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002841 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 if (mb_get_class(ptr) != start_class)
2843 break;
2844 }
2845 }
2846 else
2847#endif
2848 while (vim_iswordc(*ptr))
2849 ++ptr;
2850 return ptr;
2851}
2852
2853/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002854 * Find the end of the line, omitting CR and NL at the end.
2855 * Returns a pointer to just after the line.
2856 */
2857 static char_u *
2858find_line_end(ptr)
2859 char_u *ptr;
2860{
2861 char_u *s;
2862
2863 s = ptr + STRLEN(ptr);
2864 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
2865 --s;
2866 return s;
2867}
2868
2869/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 * Free the list of completions
2871 */
2872 static void
2873ins_compl_free()
2874{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002875 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002876 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002878 vim_free(compl_pattern);
2879 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002880 vim_free(compl_leader);
2881 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002883 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002885
2886 ins_compl_del_pum();
2887 pum_clear();
2888
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002889 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 do
2891 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002892 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002893 compl_curr_match = compl_curr_match->cp_next;
2894 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002896 if (match->cp_flags & FREE_FNAME)
2897 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00002898 for (i = 0; i < CPT_COUNT; ++i)
2899 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002901 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
2902 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903}
2904
2905 static void
2906ins_compl_clear()
2907{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002908 compl_cont_status = 0;
2909 compl_started = FALSE;
2910 compl_matches = 0;
2911 vim_free(compl_pattern);
2912 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002913 vim_free(compl_leader);
2914 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002916 vim_free(compl_orig_text);
2917 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918}
2919
2920/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002921 * Return TRUE when Insert completion is active.
2922 */
2923 int
2924ins_compl_active()
2925{
2926 return compl_started;
2927}
2928
2929/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002930 * Delete one character before the cursor and show the subset of the matches
2931 * that match the word that is now before the cursor.
Bram Moolenaara6557602006-02-04 22:43:20 +00002932 * Returns TRUE if the work is done and another char to be got from the user.
2933 */
2934 static int
2935ins_compl_bs()
2936{
2937 char_u *line;
2938 char_u *p;
2939
2940 if (curwin->w_cursor.col <= compl_col + compl_length)
2941 {
2942 /* Deleted more than what was used to find matches, need to look for
2943 * matches all over again. */
2944 ins_compl_free();
2945 compl_started = FALSE;
2946 compl_matches = 0;
2947 }
2948
2949 line = ml_get_curline();
2950 p = line + curwin->w_cursor.col;
2951 mb_ptr_back(line, p);
2952
2953 vim_free(compl_leader);
2954 compl_leader = vim_strnsave(line + compl_col, (p - line) - compl_col);
2955 if (compl_leader != NULL)
2956 {
2957 ins_compl_del_pum();
2958 ins_compl_delete();
2959 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2960
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002961 if (compl_started)
2962 ins_compl_set_original_text(compl_leader);
2963 else
Bram Moolenaara6557602006-02-04 22:43:20 +00002964 {
2965 /* Matches were cleared, need to search for them now. */
2966 if (ins_complete(Ctrl_N) == FAIL)
2967 compl_cont_status = 0;
2968 else
2969 {
2970 /* Remove the completed word again. */
2971 ins_compl_delete();
2972 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2973 }
2974 }
2975
2976 /* Show the popup menu with a different set of matches. */
2977 ins_compl_show_pum();
2978 compl_used_match = FALSE;
2979
2980 return TRUE;
2981 }
2982 return FALSE;
2983}
2984
2985/*
2986 * Append one character to the match leader. May reduce the number of
2987 * matches.
2988 */
2989 static void
2990ins_compl_addleader(c)
2991 int c;
2992{
2993#ifdef FEAT_MBYTE
2994 int cc;
2995
2996 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2997 {
2998 char_u buf[MB_MAXBYTES + 1];
2999
3000 (*mb_char2bytes)(c, buf);
3001 buf[cc] = NUL;
3002 ins_char_bytes(buf, cc);
3003 }
3004 else
3005#endif
3006 ins_char(c);
3007
3008 vim_free(compl_leader);
3009 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
3010 curwin->w_cursor.col - compl_col);
3011 if (compl_leader != NULL)
3012 {
3013 /* Show the popup menu with a different set of matches. */
3014 ins_compl_del_pum();
3015 ins_compl_show_pum();
3016 compl_used_match = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003017 ins_compl_set_original_text(compl_leader);
3018 }
3019}
3020
3021/*
3022 * Set the first match, the original text.
3023 */
3024 static void
3025ins_compl_set_original_text(str)
3026 char_u *str;
3027{
3028 char_u *p;
3029
3030 /* Replace the original text entry. */
3031 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3032 {
3033 p = vim_strsave(str);
3034 if (p != NULL)
3035 {
3036 vim_free(compl_first_match->cp_str);
3037 compl_first_match->cp_str = p;
3038 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003039 }
3040}
3041
3042/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003043 * Append one character to the match leader. May reduce the number of
3044 * matches.
3045 */
3046 static void
3047ins_compl_addfrommatch()
3048{
3049 char_u *p;
3050 int len = curwin->w_cursor.col - compl_col;
3051 int c;
3052
3053 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003054 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003055 return;
3056 p += len;
3057#ifdef FEAT_MBYTE
3058 c = mb_ptr2char(p);
3059#else
3060 c = *p;
3061#endif
3062 ins_compl_addleader(c);
3063}
3064
3065/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003067 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003068 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003070 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071ins_compl_prep(c)
3072 int c;
3073{
3074 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 int temp;
3076 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003077 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078
3079 /* Forget any previous 'special' messages if this is actually
3080 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3081 */
3082 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3083 edit_submode_extra = NULL;
3084
3085 /* Ignore end of Select mode mapping */
3086 if (c == K_SELECT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003087 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003089 /* Set "compl_get_longest" when finding the first matches. */
3090 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3091 || (ctrl_x_mode == 0 && !compl_started))
3092 {
3093 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3094 compl_used_match = TRUE;
3095 }
3096
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3098 {
3099 /*
3100 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3101 * it will be yet. Now we decide.
3102 */
3103 switch (c)
3104 {
3105 case Ctrl_E:
3106 case Ctrl_Y:
3107 ctrl_x_mode = CTRL_X_SCROLL;
3108 if (!(State & REPLACE_FLAG))
3109 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3110 else
3111 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3112 edit_submode_pre = NULL;
3113 showmode();
3114 break;
3115 case Ctrl_L:
3116 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3117 break;
3118 case Ctrl_F:
3119 ctrl_x_mode = CTRL_X_FILES;
3120 break;
3121 case Ctrl_K:
3122 ctrl_x_mode = CTRL_X_DICTIONARY;
3123 break;
3124 case Ctrl_R:
3125 /* Simply allow ^R to happen without affecting ^X mode */
3126 break;
3127 case Ctrl_T:
3128 ctrl_x_mode = CTRL_X_THESAURUS;
3129 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003130#ifdef FEAT_COMPL_FUNC
3131 case Ctrl_U:
3132 ctrl_x_mode = CTRL_X_FUNCTION;
3133 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003134 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003135 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003136 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003137#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003138 case 's':
3139 case Ctrl_S:
3140 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003141#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003142 spell_back_to_badword();
3143#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003144 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 case Ctrl_RSB:
3146 ctrl_x_mode = CTRL_X_TAGS;
3147 break;
3148#ifdef FEAT_FIND_ID
3149 case Ctrl_I:
3150 case K_S_TAB:
3151 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3152 break;
3153 case Ctrl_D:
3154 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3155 break;
3156#endif
3157 case Ctrl_V:
3158 case Ctrl_Q:
3159 ctrl_x_mode = CTRL_X_CMDLINE;
3160 break;
3161 case Ctrl_P:
3162 case Ctrl_N:
3163 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3164 * just started ^X mode, or there were enough ^X's to cancel
3165 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3166 * do normal expansion when interrupting a different mode (say
3167 * ^X^F^X^P or ^P^X^X^P, see below)
3168 * nothing changes if interrupting mode 0, (eg, the flag
3169 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003170 if (!(compl_cont_status & CONT_INTRPT))
3171 compl_cont_status |= CONT_LOCAL;
3172 else if (compl_cont_mode != 0)
3173 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 /* FALLTHROUGH */
3175 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003176 /* If we have typed at least 2 ^X's... for modes != 0, we set
3177 * compl_cont_status = 0 (eg, as if we had just started ^X
3178 * mode).
3179 * For mode 0, we set "compl_cont_mode" to an impossible
3180 * value, in both cases ^X^X can be used to restart the same
3181 * mode (avoiding ADDING mode).
3182 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3183 * 'complete' and local ^P expansions respectively.
3184 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3185 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 if (c == Ctrl_X)
3187 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003188 if (compl_cont_mode != 0)
3189 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003191 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 }
3193 ctrl_x_mode = 0;
3194 edit_submode = NULL;
3195 showmode();
3196 break;
3197 }
3198 }
3199 else if (ctrl_x_mode != 0)
3200 {
3201 /* We're already in CTRL-X mode, do we stay in it? */
3202 if (!vim_is_ctrl_x_key(c))
3203 {
3204 if (ctrl_x_mode == CTRL_X_SCROLL)
3205 ctrl_x_mode = 0;
3206 else
3207 ctrl_x_mode = CTRL_X_FINISHED;
3208 edit_submode = NULL;
3209 }
3210 showmode();
3211 }
3212
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003213 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 {
3215 /* Show error message from attempted keyword completion (probably
3216 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003217 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003219 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3220 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 || ctrl_x_mode == CTRL_X_FINISHED)
3222 {
3223 /* Get here when we have finished typing a sequence of ^N and
3224 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003225 * memory that was used, and make sure we can redo the insert. */
3226 if (compl_curr_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003228 char_u *p;
3229
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 /*
3231 * If any of the original typed text has been changed,
3232 * eg when ignorecase is set, we must add back-spaces to
3233 * the redo buffer. We add as few as necessary to delete
3234 * just the part of the original text that has changed.
3235 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003236 ptr = compl_curr_match->cp_str;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003237 p = compl_orig_text;
3238 while (*p && *p == *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003240 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 ++ptr;
3242 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003243 for (temp = 0; p[temp]; ++temp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 AppendCharToRedobuff(K_BS);
Bram Moolenaarebefac62005-12-28 22:39:57 +00003245 AppendToRedobuffLit(ptr, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 }
3247
3248#ifdef FEAT_CINDENT
3249 want_cindent = (can_cindent && cindent_on());
3250#endif
3251 /*
3252 * When completing whole lines: fix indent for 'cindent'.
3253 * Otherwise, break line if it's too long.
3254 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003255 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 {
3257#ifdef FEAT_CINDENT
3258 /* re-indent the current line */
3259 if (want_cindent)
3260 {
3261 do_c_expr_indent();
3262 want_cindent = FALSE; /* don't do it again */
3263 }
3264#endif
3265 }
3266 else
3267 {
3268 /* put the cursor on the last char, for 'tw' formatting */
3269 curwin->w_cursor.col--;
3270 if (stop_arrow() == OK)
3271 insertchar(NUL, 0, -1);
3272 curwin->w_cursor.col++;
3273 }
3274
3275 auto_format(FALSE, TRUE);
3276
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003277 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003278 * the selection without inserting anything. */
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003279 if (c == Ctrl_Y && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003280 retval = TRUE;
3281
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003282 /* CTRL-E means completion is Ended, go back to the typed text. */
3283 if (c == Ctrl_E)
3284 {
3285 ins_compl_delete();
3286 if (compl_leader != NULL)
3287 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
3288 else if (compl_first_match != NULL)
3289 ins_bytes(compl_first_match->cp_str
3290 + curwin->w_cursor.col - compl_col);
3291 retval = TRUE;
3292 }
3293
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003295 compl_started = FALSE;
3296 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 msg_clr_cmdline(); /* necessary for "noshowmode" */
3298 ctrl_x_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 if (edit_submode != NULL)
3300 {
3301 edit_submode = NULL;
3302 showmode();
3303 }
3304
3305#ifdef FEAT_CINDENT
3306 /*
3307 * Indent now if a key was typed that is in 'cinkeys'.
3308 */
3309 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3310 do_c_expr_indent();
3311#endif
3312 }
3313 }
3314
3315 /* reset continue_* if we left expansion-mode, if we stay they'll be
3316 * (re)set properly in ins_complete() */
3317 if (!vim_is_ctrl_x_key(c))
3318 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003319 compl_cont_status = 0;
3320 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003322
3323 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324}
3325
3326/*
3327 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3328 * (depending on flag) starting from buf and looking for a non-scanned
3329 * buffer (other than curbuf). curbuf is special, if it is called with
3330 * buf=curbuf then it has to be the first call for a given flag/expansion.
3331 *
3332 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3333 */
3334 static buf_T *
3335ins_compl_next_buf(buf, flag)
3336 buf_T *buf;
3337 int flag;
3338{
3339#ifdef FEAT_WINDOWS
3340 static win_T *wp;
3341#endif
3342
3343 if (flag == 'w') /* just windows */
3344 {
3345#ifdef FEAT_WINDOWS
3346 if (buf == curbuf) /* first call for this flag/expansion */
3347 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003348 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 && wp->w_buffer->b_scanned)
3350 ;
3351 buf = wp->w_buffer;
3352#else
3353 buf = curbuf;
3354#endif
3355 }
3356 else
3357 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3358 * (unlisted buffers)
3359 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003360 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 && ((flag == 'U'
3362 ? buf->b_p_bl
3363 : (!buf->b_p_bl
3364 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003365 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 ;
3367 return buf;
3368}
3369
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003370#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003371static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003372
3373/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003374 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003375 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003376 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003377 static void
3378expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003379 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003380 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003381{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003382 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003383 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003384 char_u *funcname;
3385 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003386
Bram Moolenaare344bea2005-09-01 20:46:49 +00003387 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3388 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003389 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003390
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003391 /* Call 'completefunc' to obtain the list of matches. */
3392 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003393 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003394
Bram Moolenaare344bea2005-09-01 20:46:49 +00003395 pos = curwin->w_cursor;
3396 matchlist = call_func_retlist(funcname, 2, args, FALSE);
3397 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003398 if (matchlist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003399 return;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003400
Bram Moolenaara94bc432006-03-10 21:42:59 +00003401 ins_compl_add_list(matchlist);
3402 list_unref(matchlist);
3403}
3404#endif /* FEAT_COMPL_FUNC */
3405
Bram Moolenaar39f05632006-03-19 22:15:26 +00003406#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00003407/*
3408 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00003409 */
3410 static void
3411ins_compl_add_list(list)
3412 list_T *list;
3413{
3414 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003415 int dir = compl_direction;
3416
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003417 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003418 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003419 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00003420 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
3421 /* if dir was BACKWARD then honor it just once */
3422 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00003423 else if (did_emsg)
3424 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003425 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003426}
Bram Moolenaar39f05632006-03-19 22:15:26 +00003427
3428/*
3429 * Add a match to the list of matches from a typeval_T.
3430 * If the given string is already in the list of completions, then return
3431 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
3432 * maybe because alloc() returns NULL, then FAIL is returned.
3433 */
3434 int
3435ins_compl_add_tv(tv, dir)
3436 typval_T *tv;
3437 int dir;
3438{
3439 char_u *word;
3440 int icase = p_ic;
3441 char_u *(cptext[CPT_COUNT]);
3442
3443 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3444 {
3445 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
3446 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
3447 (char_u *)"abbr", FALSE);
3448 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
3449 (char_u *)"menu", FALSE);
3450 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
3451 (char_u *)"kind", FALSE);
3452 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
3453 (char_u *)"info", FALSE);
3454 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
3455 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
3456 }
3457 else
3458 {
3459 word = get_tv_string_chk(tv);
3460 vim_memset(cptext, 0, sizeof(cptext));
3461 }
3462 if (word == NULL || *word == NUL)
3463 return FAIL;
3464 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0);
3465}
Bram Moolenaara94bc432006-03-10 21:42:59 +00003466#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003467
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003468/*
3469 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003470 * The search starts at position "ini" in curbuf and in the direction
3471 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003472 * When "compl_started" is FALSE start at that position, otherwise continue
3473 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003474 * This may return before finding all the matches.
3475 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 */
3477 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003478ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480{
3481 static pos_T first_match_pos;
3482 static pos_T last_match_pos;
3483 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003484 static int found_all = FALSE; /* Found all matches of a
3485 certain type. */
3486 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487
Bram Moolenaar572cb562005-08-05 21:35:02 +00003488 pos_T *pos;
3489 char_u **matches;
3490 int save_p_scs;
3491 int save_p_ws;
3492 int save_p_ic;
3493 int i;
3494 int num_matches;
3495 int len;
3496 int found_new_match;
3497 int type = ctrl_x_mode;
3498 char_u *ptr;
3499 char_u *dict = NULL;
3500 int dict_f = 0;
3501 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003503 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 {
3505 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3506 ins_buf->b_scanned = 0;
3507 found_all = FALSE;
3508 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003509 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003510 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 last_match_pos = first_match_pos = *ini;
3512 }
3513
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003514 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003515 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3517 for (;;)
3518 {
3519 found_new_match = FAIL;
3520
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003521 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 * or if found_all says this entry is done. For ^X^L only use the
3523 * entries from 'complete' that look in loaded buffers. */
3524 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003525 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 {
3527 found_all = FALSE;
3528 while (*e_cpt == ',' || *e_cpt == ' ')
3529 e_cpt++;
3530 if (*e_cpt == '.' && !curbuf->b_scanned)
3531 {
3532 ins_buf = curbuf;
3533 first_match_pos = *ini;
3534 /* So that ^N can match word immediately after cursor */
3535 if (ctrl_x_mode == 0)
3536 dec(&first_match_pos);
3537 last_match_pos = first_match_pos;
3538 type = 0;
3539 }
3540 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
3541 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
3542 {
3543 /* Scan a buffer, but not the current one. */
3544 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
3545 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003546 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 first_match_pos.col = last_match_pos.col = 0;
3548 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
3549 last_match_pos.lnum = 0;
3550 type = 0;
3551 }
3552 else /* unloaded buffer, scan like dictionary */
3553 {
3554 found_all = TRUE;
3555 if (ins_buf->b_fname == NULL)
3556 continue;
3557 type = CTRL_X_DICTIONARY;
3558 dict = ins_buf->b_fname;
3559 dict_f = DICT_EXACT;
3560 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00003561 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 ins_buf->b_fname == NULL
3563 ? buf_spname(ins_buf)
3564 : ins_buf->b_sfname == NULL
3565 ? (char *)ins_buf->b_fname
3566 : (char *)ins_buf->b_sfname);
3567 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3568 }
3569 else if (*e_cpt == NUL)
3570 break;
3571 else
3572 {
3573 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3574 type = -1;
3575 else if (*e_cpt == 'k' || *e_cpt == 's')
3576 {
3577 if (*e_cpt == 'k')
3578 type = CTRL_X_DICTIONARY;
3579 else
3580 type = CTRL_X_THESAURUS;
3581 if (*++e_cpt != ',' && *e_cpt != NUL)
3582 {
3583 dict = e_cpt;
3584 dict_f = DICT_FIRST;
3585 }
3586 }
3587#ifdef FEAT_FIND_ID
3588 else if (*e_cpt == 'i')
3589 type = CTRL_X_PATH_PATTERNS;
3590 else if (*e_cpt == 'd')
3591 type = CTRL_X_PATH_DEFINES;
3592#endif
3593 else if (*e_cpt == ']' || *e_cpt == 't')
3594 {
3595 type = CTRL_X_TAGS;
3596 sprintf((char*)IObuff, _("Scanning tags."));
3597 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3598 }
3599 else
3600 type = -1;
3601
3602 /* in any case e_cpt is advanced to the next entry */
3603 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
3604
3605 found_all = TRUE;
3606 if (type == -1)
3607 continue;
3608 }
3609 }
3610
3611 switch (type)
3612 {
3613 case -1:
3614 break;
3615#ifdef FEAT_FIND_ID
3616 case CTRL_X_PATH_PATTERNS:
3617 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003618 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003619 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003621 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3623 (linenr_T)1, (linenr_T)MAXLNUM);
3624 break;
3625#endif
3626
3627 case CTRL_X_DICTIONARY:
3628 case CTRL_X_THESAURUS:
3629 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00003630 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 : (type == CTRL_X_THESAURUS
3632 ? (*curbuf->b_p_tsr == NUL
3633 ? p_tsr
3634 : curbuf->b_p_tsr)
3635 : (*curbuf->b_p_dict == NUL
3636 ? p_dict
3637 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003638 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00003639 dict != NULL ? dict_f
3640 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 dict = NULL;
3642 break;
3643
3644 case CTRL_X_TAGS:
3645 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
3646 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003647 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648
3649 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003650 * of matches is found when compl_pattern is empty */
3651 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
3653 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
3654 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3655 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003656 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 }
3658 p_ic = save_p_ic;
3659 break;
3660
3661 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003662 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
3664 {
3665
3666 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003667 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003668 ins_compl_add_matches(num_matches, matches,
3669#ifdef CASE_INSENSITIVE_FILENAME
3670 TRUE
3671#else
3672 FALSE
3673#endif
3674 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 }
3676 break;
3677
3678 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003679 if (expand_cmdline(&compl_xp, compl_pattern,
3680 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003682 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 break;
3684
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003685#ifdef FEAT_COMPL_FUNC
3686 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003687 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003688 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003689 break;
3690#endif
3691
Bram Moolenaar488c6512005-08-11 20:09:58 +00003692 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003693#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00003694 num_matches = expand_spelling(first_match_pos.lnum,
3695 first_match_pos.col, compl_pattern, &matches);
3696 if (num_matches > 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003697 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar488c6512005-08-11 20:09:58 +00003698#endif
3699 break;
3700
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 default: /* normal ^P/^N and ^X^L */
3702 /*
3703 * If 'infercase' is set, don't use 'smartcase' here
3704 */
3705 save_p_scs = p_scs;
3706 if (ins_buf->b_p_inf)
3707 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003708
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 /* buffers other than curbuf are scanned from the beginning or the
3710 * end but never from the middle, thus setting nowrapscan in this
3711 * buffers is a good idea, on the other hand, we always set
3712 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
3713 save_p_ws = p_ws;
3714 if (ins_buf != curbuf)
3715 p_ws = FALSE;
3716 else if (*e_cpt == '.')
3717 p_ws = TRUE;
3718 for (;;)
3719 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003720 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003722 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
3723 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003725 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003727 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003729 found_new_match = searchit(NULL, ins_buf, pos,
3730 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003731 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003732 RE_LAST, (linenr_T)0);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003733 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003735 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003736 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 first_match_pos = *pos;
3738 last_match_pos = *pos;
3739 }
3740 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003741 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 found_new_match = FAIL;
3743 if (found_new_match == FAIL)
3744 {
3745 if (ins_buf == curbuf)
3746 found_all = TRUE;
3747 break;
3748 }
3749
3750 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003751 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 && ini->lnum == pos->lnum
3753 && ini->col == pos->col)
3754 continue;
3755 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
3756 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3757 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003758 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 {
3760 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3761 continue;
3762 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3763 if (!p_paste)
3764 ptr = skipwhite(ptr);
3765 }
3766 len = (int)STRLEN(ptr);
3767 }
3768 else
3769 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003770 char_u *tmp_ptr = ptr;
3771
3772 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003774 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 /* Skip if already inside a word. */
3776 if (vim_iswordp(tmp_ptr))
3777 continue;
3778 /* Find start of next word. */
3779 tmp_ptr = find_word_start(tmp_ptr);
3780 }
3781 /* Find end of this word. */
3782 tmp_ptr = find_word_end(tmp_ptr);
3783 len = (int)(tmp_ptr - ptr);
3784
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003785 if ((compl_cont_status & CONT_ADDING)
3786 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 {
3788 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3789 {
3790 /* Try next line, if any. the new word will be
3791 * "join" as if the normal command "J" was used.
3792 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003793 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 * works -- Acevedo */
3795 STRNCPY(IObuff, ptr, len);
3796 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3797 tmp_ptr = ptr = skipwhite(ptr);
3798 /* Find start of next word. */
3799 tmp_ptr = find_word_start(tmp_ptr);
3800 /* Find end of next word. */
3801 tmp_ptr = find_word_end(tmp_ptr);
3802 if (tmp_ptr > ptr)
3803 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003804 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003806 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 IObuff[len++] = ' ';
3808 /* IObuf =~ "\k.* ", thus len >= 2 */
3809 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003810 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 || (vim_strchr(p_cpo, CPO_JOINSP)
3812 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003813 && (IObuff[len - 2] == '?'
3814 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 IObuff[len++] = ' ';
3816 }
3817 /* copy as much as posible of the new word */
3818 if (tmp_ptr - ptr >= IOSIZE - len)
3819 tmp_ptr = ptr + IOSIZE - len - 1;
3820 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3821 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00003822 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 }
3824 IObuff[len] = NUL;
3825 ptr = IObuff;
3826 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003827 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 continue;
3829 }
3830 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003831 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003832 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003833 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 {
3835 found_new_match = OK;
3836 break;
3837 }
3838 }
3839 p_scs = save_p_scs;
3840 p_ws = save_p_ws;
3841 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003842
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003843 /* check if compl_curr_match has changed, (e.g. other type of
3844 * expansion added somenthing) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003845 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 found_new_match = OK;
3847
3848 /* break the loop for specialized modes (use 'complete' just for the
3849 * generic ctrl_x_mode == 0) or when we've found a new match */
3850 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003851 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003852 {
3853 if (got_int)
3854 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003855 /* Fill the popup menu as soon as possible. */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003856 if (pum_wanted() && type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003857 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003858
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003859 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
3860 || compl_interrupted)
3861 break;
3862 compl_started = TRUE;
3863 }
3864 else
3865 {
3866 /* Mark a buffer scanned when it has been scanned completely */
3867 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
3868 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003870 compl_started = FALSE;
3871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003873 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874
3875 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
3876 && *e_cpt == NUL) /* Got to end of 'complete' */
3877 found_new_match = FAIL;
3878
3879 i = -1; /* total of matches, unknown */
3880 if (found_new_match == FAIL
3881 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
3882 i = ins_compl_make_cyclic();
3883
3884 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003885 * just been made cyclic then we have to move compl_curr_match to the next
3886 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003887 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
3888 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003889 if (compl_curr_match == NULL)
3890 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 return i;
3892}
3893
3894/* Delete the old text being completed. */
3895 static void
3896ins_compl_delete()
3897{
3898 int i;
3899
3900 /*
3901 * In insert mode: Delete the typed part.
3902 * In replace mode: Put the old characters back, if any.
3903 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003904 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 backspace_until_column(i);
3906 changed_cline_bef_curs();
3907}
3908
3909/* Insert the new text being completed. */
3910 static void
3911ins_compl_insert()
3912{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003913 ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003914 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3915 compl_used_match = FALSE;
3916 else
3917 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918}
3919
3920/*
3921 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003922 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
3923 * get more completions. If it is FALSE, then we just do nothing when there
3924 * are no more completions in a given direction. The latter case is used when
3925 * we are still in the middle of finding completions, to allow browsing
3926 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 * Return the total number of matches, or -1 if still unknown -- webb.
3928 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003929 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
3930 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 *
3932 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00003933 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
3934 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 */
3936 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003937ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003939 int count; /* repeat completion this many times; should
3940 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003941 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942{
3943 int num_matches = -1;
3944 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003945 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00003946 compl_T *found_compl = NULL;
3947 int found_end = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003949 if (compl_leader != NULL
3950 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003952 /* Set "compl_shown_match" to the actually shown match, it may differ
3953 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003954 while (!ins_compl_equal(compl_shown_match,
3955 compl_leader, STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003956 && compl_shown_match->cp_next != NULL
3957 && compl_shown_match->cp_next != compl_first_match)
3958 compl_shown_match = compl_shown_match->cp_next;
3959 }
3960
3961 if (allow_get_expansion && insert_match
3962 && (!compl_get_longest || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 /* Delete old text to be replaced */
3964 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003965
Bram Moolenaare3226be2005-12-18 22:10:00 +00003966 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
3967 * around. */
3968 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00003970 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003972 if (compl_pending != 0)
3973 --compl_pending;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003974 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00003975 found_end = (compl_first_match != NULL
3976 && (compl_shown_match->cp_next == compl_first_match
3977 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00003978 }
3979 else if (compl_shows_dir == BACKWARD
3980 && compl_shown_match->cp_prev != NULL)
3981 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003982 if (compl_pending != 0)
3983 ++compl_pending;
Bram Moolenaara6557602006-02-04 22:43:20 +00003984 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00003985 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00003986 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 }
3988 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00003989 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003990 if (compl_shows_dir == BACKWARD)
3991 --compl_pending;
3992 else
3993 ++compl_pending;
Bram Moolenaara6557602006-02-04 22:43:20 +00003994 if (!allow_get_expansion)
Bram Moolenaare3226be2005-12-18 22:10:00 +00003995 return -1;
Bram Moolenaara6557602006-02-04 22:43:20 +00003996
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003997 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003998 if (compl_pending != 0 && compl_direction == compl_shows_dir)
Bram Moolenaara6557602006-02-04 22:43:20 +00003999 compl_shown_match = compl_curr_match;
4000 found_end = FALSE;
4001 }
4002 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4003 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004004 && !ins_compl_equal(compl_shown_match,
4005 compl_leader, STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004006 ++todo;
4007 else
4008 /* Remember a matching item. */
4009 found_compl = compl_shown_match;
4010
4011 /* Stop at the end of the list when we found a usable match. */
4012 if (found_end)
4013 {
4014 if (found_compl != NULL)
4015 {
4016 compl_shown_match = found_compl;
4017 break;
4018 }
4019 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 }
4022
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004023 /* Insert the text of the new completion, or the compl_leader. */
4024 if (insert_match)
4025 {
4026 if (!compl_get_longest || compl_used_match)
4027 ins_compl_insert();
4028 else
4029 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
4030 }
4031 else
4032 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033
4034 if (!allow_get_expansion)
4035 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004036 /* may undisplay the popup menu first */
4037 ins_compl_upd_pum();
4038
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004039 /* redraw to show the user what was inserted */
4040 update_screen(0);
4041
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004042 /* display the updated popup menu */
4043 ins_compl_show_pum();
4044
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 /* Delete old text to be replaced, since we're still searching and
4046 * don't want to match ourselves! */
4047 ins_compl_delete();
4048 }
4049
4050 /*
4051 * Show the file name for the match (if any)
4052 * Truncate the file name to avoid a wait for return.
4053 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004054 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 {
4056 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004057 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 if (i <= 0)
4059 i = 0;
4060 else
4061 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004062 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 msg(IObuff);
4064 redraw_cmdline = FALSE; /* don't overwrite! */
4065 }
4066
4067 return num_matches;
4068}
4069
4070/*
4071 * Call this while finding completions, to check whether the user has hit a key
4072 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004073 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004075 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 */
4077 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004078ins_compl_check_keys(frequency)
4079 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080{
4081 static int count = 0;
4082
4083 int c;
4084
4085 /* Don't check when reading keys from a script. That would break the test
4086 * scripts */
4087 if (using_script())
4088 return;
4089
4090 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004091 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 return;
4093 count = 0;
4094
4095 ++no_mapping;
4096 c = vpeekc_any();
4097 --no_mapping;
4098 if (c != NUL)
4099 {
4100 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4101 {
4102 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004103 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004104 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4105 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 }
4107 else if (c != Ctrl_R)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004108 compl_interrupted = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004110 if (compl_pending != 0 && !got_int)
4111 (void)ins_compl_next(FALSE, compl_pending > 0
4112 ? compl_pending : -compl_pending, TRUE);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004113}
4114
4115/*
4116 * Decide the direction of Insert mode complete from the key typed.
4117 * Returns BACKWARD or FORWARD.
4118 */
4119 static int
4120ins_compl_key2dir(c)
4121 int c;
4122{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004123 if (c == Ctrl_P || c == Ctrl_L
4124 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4125 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004126 return BACKWARD;
4127 return FORWARD;
4128}
4129
4130/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004131 * Return TRUE for keys that are used for completion only when the popup menu
4132 * is visible.
4133 */
4134 static int
4135ins_compl_pum_key(c)
4136 int c;
4137{
4138 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004139 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4140 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004141}
4142
4143/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004144 * Decide the number of completions to move forward.
4145 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4146 */
4147 static int
4148ins_compl_key2count(c)
4149 int c;
4150{
4151 int h;
4152
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004153 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004154 {
4155 h = pum_get_height();
4156 if (h > 3)
4157 h -= 2; /* keep some context */
4158 return h;
4159 }
4160 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161}
4162
4163/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004164 * Return TRUE if completion with "c" should insert the match, FALSE if only
4165 * to change the currently selected completion.
4166 */
4167 static int
4168ins_compl_use_match(c)
4169 int c;
4170{
4171 switch (c)
4172 {
4173 case K_UP:
4174 case K_DOWN:
4175 case K_PAGEDOWN:
4176 case K_KPAGEDOWN:
4177 case K_S_DOWN:
4178 case K_PAGEUP:
4179 case K_KPAGEUP:
4180 case K_S_UP:
4181 return FALSE;
4182 }
4183 return TRUE;
4184}
4185
4186/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 * Do Insert mode completion.
4188 * Called when character "c" was typed, which has a meaning for completion.
4189 * Returns OK if completion was done, FAIL if something failed (out of mem).
4190 */
4191 static int
4192ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004193 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004195 char_u *line;
4196 int startcol = 0; /* column where searched text starts */
4197 colnr_T curs_col; /* cursor column */
4198 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199
Bram Moolenaare3226be2005-12-18 22:10:00 +00004200 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004201 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 {
4203 /* First time we hit ^N or ^P (in a row, I mean) */
4204
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 did_ai = FALSE;
4206#ifdef FEAT_SMARTINDENT
4207 did_si = FALSE;
4208 can_si = FALSE;
4209 can_si_back = FALSE;
4210#endif
4211 if (stop_arrow() == FAIL)
4212 return FAIL;
4213
4214 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004215 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004216 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217
4218 /* if this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004219 * "compl_startpos" to the cursor as a pattern to add a new word
4220 * instead of expand the one before the cursor, in word-wise if
4221 * "compl_startpos"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 * is not in the same line as the cursor then fix it (the line has
4223 * been split because it was longer than 'tw'). if SOL is set then
4224 * skip the previous pattern, a word at the beginning of the line has
4225 * been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004226 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4227 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 {
4229 /*
4230 * it is a continued search
4231 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004232 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4234 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4235 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004236 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004238 /* line (probably) wrapped, set compl_startpos to the
4239 * first non_blank in the line, if it is not a wordchar
4240 * include it to get a better pattern, but then we don't
4241 * want the "\\<" prefix, check it bellow */
4242 compl_col = (colnr_T)(skipwhite(line) - line);
4243 compl_startpos.col = compl_col;
4244 compl_startpos.lnum = curwin->w_cursor.lnum;
4245 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 }
4247 else
4248 {
4249 /* S_IPOS was set when we inserted a word that was at the
4250 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004251 * mode but first we need to redefine compl_startpos */
4252 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004254 compl_cont_status |= CONT_SOL;
4255 compl_startpos.col = (colnr_T)(skipwhite(
4256 line + compl_length
4257 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004259 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004261 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004262 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 * have enough space? just being paranoic */
4264#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004265 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004267 compl_cont_status &= ~CONT_SOL;
4268 compl_length = (IOSIZE - MIN_SPACE);
4269 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004271 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4272 if (compl_length < 1)
4273 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 }
4275 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004276 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004278 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 }
4280 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004281 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004283 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004285 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004287 compl_cont_status = 0;
4288 compl_cont_status |= CONT_N_ADDS;
4289 compl_startpos = curwin->w_cursor;
4290 startcol = (int)curs_col;
4291 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 }
4293
4294 /* Work out completion pattern and original text -- webb */
4295 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4296 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004297 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4299 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004300 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004302 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004304 compl_col += ++startcol;
4305 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 }
4307 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004308 compl_pattern = str_foldcase(line + compl_col,
4309 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004311 compl_pattern = vim_strnsave(line + compl_col,
4312 compl_length);
4313 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 return FAIL;
4315 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004316 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 {
4318 char_u *prefix = (char_u *)"\\<";
4319
4320 /* we need 3 extra chars, 1 for the NUL and
4321 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004322 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4323 compl_length) + 3);
4324 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004326 if (!vim_iswordp(line + compl_col)
4327 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 && (
4329#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004330 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004332 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333#endif
4334 )))
4335 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004336 STRCPY((char *)compl_pattern, prefix);
4337 (void)quote_meta(compl_pattern + STRLEN(prefix),
4338 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004340 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004342 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004344 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345#endif
4346 )
4347 {
4348 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004349 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4350 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004352 compl_col += curs_col;
4353 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 }
4355 else
4356 {
4357#ifdef FEAT_MBYTE
4358 /* Search the point of change class of multibyte character
4359 * or not a word single byte character backward. */
4360 if (has_mbyte)
4361 {
4362 int base_class;
4363 int head_off;
4364
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004365 startcol -= (*mb_head_off)(line, line + startcol);
4366 base_class = mb_get_class(line + startcol);
4367 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004369 head_off = (*mb_head_off)(line, line + startcol);
4370 if (base_class != mb_get_class(line + startcol
4371 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004373 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 }
4375 }
4376 else
4377#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004378 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004380 compl_col += ++startcol;
4381 compl_length = (int)curs_col - startcol;
4382 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 {
4384 /* Only match word with at least two chars -- webb
4385 * there's no need to call quote_meta,
4386 * alloc(7) is enough -- Acevedo
4387 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004388 compl_pattern = alloc(7);
4389 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004391 STRCPY((char *)compl_pattern, "\\<");
4392 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4393 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 }
4395 else
4396 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004397 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4398 compl_length) + 3);
4399 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004401 STRCPY((char *)compl_pattern, "\\<");
4402 (void)quote_meta(compl_pattern + 2, line + compl_col,
4403 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 }
4405 }
4406 }
4407 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4408 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004409 compl_col = skipwhite(line) - line;
4410 compl_length = (int)curs_col - (int)compl_col;
4411 if (compl_length < 0) /* cursor in indent: empty pattern */
4412 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004414 compl_pattern = str_foldcase(line + compl_col, compl_length,
4415 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004417 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4418 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 return FAIL;
4420 }
4421 else if (ctrl_x_mode == CTRL_X_FILES)
4422 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004423 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004425 compl_col += ++startcol;
4426 compl_length = (int)curs_col - startcol;
4427 compl_pattern = addstar(line + compl_col, compl_length,
4428 EXPAND_FILES);
4429 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 return FAIL;
4431 }
4432 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4433 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004434 compl_pattern = vim_strnsave(line, curs_col);
4435 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004437 set_cmd_context(&compl_xp, compl_pattern,
4438 (int)STRLEN(compl_pattern), curs_col);
4439 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4440 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004442 startcol = (int)(compl_xp.xp_pattern - compl_pattern);
4443 compl_col = startcol;
4444 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004446 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004447 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00004448#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004449 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00004450 * Call user defined function 'completefunc' with "a:findstart"
4451 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004452 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00004453 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004454 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004455 char_u *funcname;
4456 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004457
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004458 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004459 * string */
4460 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4461 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4462 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004463 {
4464 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
4465 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004466 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004467 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004468
4469 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004470 args[1] = NULL;
4471 pos = curwin->w_cursor;
4472 col = call_func_retnr(funcname, 2, args, FALSE);
4473 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004474
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004475 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004476 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004477 compl_col = col;
4478 if ((colnr_T)compl_col > curs_col)
4479 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004480
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004481 /* Setup variables for completion. Need to obtain "line" again,
4482 * it may have become invalid. */
4483 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004484 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004485 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4486 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004487#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004488 return FAIL;
4489 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00004490 else if (ctrl_x_mode == CTRL_X_SPELL)
4491 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004492#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004493 if (spell_bad_len > 0)
4494 compl_col = curs_col - spell_bad_len;
4495 else
4496 compl_col = spell_word_start(startcol);
4497 if (compl_col >= (colnr_T)startcol)
Bram Moolenaar488c6512005-08-11 20:09:58 +00004498 return FAIL;
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00004499 spell_expand_check_cap(compl_col);
Bram Moolenaare2f98b92006-03-29 21:18:24 +00004500 /* Need to obtain "line" again, it may have become invalid. */
4501 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004502 compl_length = (int)curs_col - compl_col;
4503 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4504 if (compl_pattern == NULL)
4505#endif
4506 return FAIL;
4507 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004508 else
4509 {
4510 EMSG2(_(e_intern2), "ins_complete()");
4511 return FAIL;
4512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004514 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 {
4516 edit_submode_pre = (char_u *)_(" Adding");
4517 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4518 {
4519 /* Insert a new line, keep indentation but ignore 'comments' */
4520#ifdef FEAT_COMMENTS
4521 char_u *old = curbuf->b_p_com;
4522
4523 curbuf->b_p_com = (char_u *)"";
4524#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004525 compl_startpos.lnum = curwin->w_cursor.lnum;
4526 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 ins_eol('\r');
4528#ifdef FEAT_COMMENTS
4529 curbuf->b_p_com = old;
4530#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004531 compl_length = 0;
4532 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 }
4534 }
4535 else
4536 {
4537 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004538 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 }
4540
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004541 if (compl_cont_status & CONT_LOCAL)
4542 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 else
4544 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4545
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004546 /* Always add completion for the original text. */
4547 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004548 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4549 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004550 -1, FALSE, NULL, NULL, 0, ORIGINAL_TEXT) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004552 vim_free(compl_pattern);
4553 compl_pattern = NULL;
4554 vim_free(compl_orig_text);
4555 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 return FAIL;
4557 }
4558
4559 /* showmode might reset the internal line pointers, so it must
4560 * be called before line = ml_get(), or when this address is no
4561 * longer needed. -- Acevedo.
4562 */
4563 edit_submode_extra = (char_u *)_("-- Searching...");
4564 edit_submode_highl = HLF_COUNT;
4565 showmode();
4566 edit_submode_extra = NULL;
4567 out_flush();
4568 }
4569
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004570 compl_shown_match = compl_curr_match;
4571 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572
4573 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004574 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004576 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004578 /* may undisplay the popup menu */
4579 ins_compl_upd_pum();
4580
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004581 if (n > 1) /* all matches have been found */
4582 compl_matches = n;
4583 compl_curr_match = compl_shown_match;
4584 compl_direction = compl_shows_dir;
4585 compl_interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586
4587 /* eat the ESC to avoid leaving insert mode */
4588 if (got_int && !global_busy)
4589 {
4590 (void)vgetc();
4591 got_int = FALSE;
4592 }
4593
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004594 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004595 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004597 edit_submode_extra = (compl_cont_status & CONT_ADDING)
4598 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
4600 edit_submode_highl = HLF_E;
4601 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4602 * because we couldn't expand anything at first place, but if we used
4603 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4604 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004605 if ( compl_length > 1
4606 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 || (ctrl_x_mode != 0
4608 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
4609 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004610 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 }
4612
Bram Moolenaar572cb562005-08-05 21:35:02 +00004613 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004614 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004616 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617
4618 if (edit_submode_extra == NULL)
4619 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004620 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 {
4622 edit_submode_extra = (char_u *)_("Back at original");
4623 edit_submode_highl = HLF_W;
4624 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004625 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 {
4627 edit_submode_extra = (char_u *)_("Word from other line");
4628 edit_submode_highl = HLF_COUNT;
4629 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004630 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 {
4632 edit_submode_extra = (char_u *)_("The only match");
4633 edit_submode_highl = HLF_COUNT;
4634 }
4635 else
4636 {
4637 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004638 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004640 int number = 0;
4641 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004643 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 {
4645 /* search backwards for the first valid (!= -1) number.
4646 * This should normally succeed already at the first loop
4647 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004648 for (match = compl_curr_match->cp_prev; match != NULL
4649 && match != compl_first_match;
4650 match = match->cp_prev)
4651 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004653 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 break;
4655 }
4656 if (match != NULL)
4657 /* go up and assign all numbers which are not assigned
4658 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004659 for (match = match->cp_next;
4660 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004661 match = match->cp_next)
4662 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 }
4664 else /* BACKWARD */
4665 {
4666 /* search forwards (upwards) for the first valid (!= -1)
4667 * number. This should normally succeed already at the
4668 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004669 for (match = compl_curr_match->cp_next; match != NULL
4670 && match != compl_first_match;
4671 match = match->cp_next)
4672 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004674 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 break;
4676 }
4677 if (match != NULL)
4678 /* go down and assign all numbers which are not
4679 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004680 for (match = match->cp_prev; match
4681 && match->cp_number == -1;
4682 match = match->cp_prev)
4683 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 }
4685 }
4686
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004687 /* The match should always have a sequence number now, this is
4688 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004689 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 {
4691 /* Space for 10 text chars. + 2x10-digit no.s */
4692 static char_u match_ref[31];
4693
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004694 if (compl_matches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 sprintf((char *)IObuff, _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004696 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004698 sprintf((char *)IObuff, _("match %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004699 compl_curr_match->cp_number);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004700 vim_strncpy(match_ref, IObuff, 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 edit_submode_extra = match_ref;
4702 edit_submode_highl = HLF_R;
4703 if (dollar_vcol)
4704 curs_columns(FALSE);
4705 }
4706 }
4707 }
4708
4709 /* Show a message about what (completion) mode we're in. */
4710 showmode();
4711 if (edit_submode_extra != NULL)
4712 {
4713 if (!p_smd)
4714 msg_attr(edit_submode_extra,
4715 edit_submode_highl < HLF_COUNT
4716 ? hl_attr(edit_submode_highl) : 0);
4717 }
4718 else
4719 msg_clr_cmdline(); /* necessary for "noshowmode" */
4720
Bram Moolenaara94bc432006-03-10 21:42:59 +00004721 /* RedrawingDisabled may be set when invoked through complete(). */
4722 n = RedrawingDisabled;
4723 RedrawingDisabled = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004724 ins_compl_show_pum();
Bram Moolenaara94bc432006-03-10 21:42:59 +00004725 setcursor();
4726 RedrawingDisabled = n;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004727
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 return OK;
4729}
4730
4731/*
4732 * Looks in the first "len" chars. of "src" for search-metachars.
4733 * If dest is not NULL the chars. are copied there quoting (with
4734 * a backslash) the metachars, and dest would be NUL terminated.
4735 * Returns the length (needed) of dest
4736 */
4737 static int
4738quote_meta(dest, src, len)
4739 char_u *dest;
4740 char_u *src;
4741 int len;
4742{
4743 int m;
4744
4745 for (m = len; --len >= 0; src++)
4746 {
4747 switch (*src)
4748 {
4749 case '.':
4750 case '*':
4751 case '[':
4752 if (ctrl_x_mode == CTRL_X_DICTIONARY
4753 || ctrl_x_mode == CTRL_X_THESAURUS)
4754 break;
4755 case '~':
4756 if (!p_magic) /* quote these only if magic is set */
4757 break;
4758 case '\\':
4759 if (ctrl_x_mode == CTRL_X_DICTIONARY
4760 || ctrl_x_mode == CTRL_X_THESAURUS)
4761 break;
4762 case '^': /* currently it's not needed. */
4763 case '$':
4764 m++;
4765 if (dest != NULL)
4766 *dest++ = '\\';
4767 break;
4768 }
4769 if (dest != NULL)
4770 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004771# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 /* Copy remaining bytes of a multibyte character. */
4773 if (has_mbyte)
4774 {
4775 int i, mb_len;
4776
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004777 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 if (mb_len > 0 && len >= mb_len)
4779 for (i = 0; i < mb_len; ++i)
4780 {
4781 --len;
4782 ++src;
4783 if (dest != NULL)
4784 *dest++ = *src;
4785 }
4786 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004787# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 }
4789 if (dest != NULL)
4790 *dest = NUL;
4791
4792 return m;
4793}
4794#endif /* FEAT_INS_EXPAND */
4795
4796/*
4797 * Next character is interpreted literally.
4798 * A one, two or three digit decimal number is interpreted as its byte value.
4799 * If one or two digits are entered, the next character is given to vungetc().
4800 * For Unicode a character > 255 may be returned.
4801 */
4802 int
4803get_literal()
4804{
4805 int cc;
4806 int nc;
4807 int i;
4808 int hex = FALSE;
4809 int octal = FALSE;
4810#ifdef FEAT_MBYTE
4811 int unicode = 0;
4812#endif
4813
4814 if (got_int)
4815 return Ctrl_C;
4816
4817#ifdef FEAT_GUI
4818 /*
4819 * In GUI there is no point inserting the internal code for a special key.
4820 * It is more useful to insert the string "<KEY>" instead. This would
4821 * probably be useful in a text window too, but it would not be
4822 * vi-compatible (maybe there should be an option for it?) -- webb
4823 */
4824 if (gui.in_use)
4825 ++allow_keys;
4826#endif
4827#ifdef USE_ON_FLY_SCROLL
4828 dont_scroll = TRUE; /* disallow scrolling here */
4829#endif
4830 ++no_mapping; /* don't map the next key hits */
4831 cc = 0;
4832 i = 0;
4833 for (;;)
4834 {
4835 do
4836 nc = safe_vgetc();
4837 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
4838 || nc == K_HOR_SCROLLBAR);
4839#ifdef FEAT_CMDL_INFO
4840 if (!(State & CMDLINE)
4841# ifdef FEAT_MBYTE
4842 && MB_BYTE2LEN_CHECK(nc) == 1
4843# endif
4844 )
4845 add_to_showcmd(nc);
4846#endif
4847 if (nc == 'x' || nc == 'X')
4848 hex = TRUE;
4849 else if (nc == 'o' || nc == 'O')
4850 octal = TRUE;
4851#ifdef FEAT_MBYTE
4852 else if (nc == 'u' || nc == 'U')
4853 unicode = nc;
4854#endif
4855 else
4856 {
4857 if (hex
4858#ifdef FEAT_MBYTE
4859 || unicode != 0
4860#endif
4861 )
4862 {
4863 if (!vim_isxdigit(nc))
4864 break;
4865 cc = cc * 16 + hex2nr(nc);
4866 }
4867 else if (octal)
4868 {
4869 if (nc < '0' || nc > '7')
4870 break;
4871 cc = cc * 8 + nc - '0';
4872 }
4873 else
4874 {
4875 if (!VIM_ISDIGIT(nc))
4876 break;
4877 cc = cc * 10 + nc - '0';
4878 }
4879
4880 ++i;
4881 }
4882
4883 if (cc > 255
4884#ifdef FEAT_MBYTE
4885 && unicode == 0
4886#endif
4887 )
4888 cc = 255; /* limit range to 0-255 */
4889 nc = 0;
4890
4891 if (hex) /* hex: up to two chars */
4892 {
4893 if (i >= 2)
4894 break;
4895 }
4896#ifdef FEAT_MBYTE
4897 else if (unicode) /* Unicode: up to four or eight chars */
4898 {
4899 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
4900 break;
4901 }
4902#endif
4903 else if (i >= 3) /* decimal or octal: up to three chars */
4904 break;
4905 }
4906 if (i == 0) /* no number entered */
4907 {
4908 if (nc == K_ZERO) /* NUL is stored as NL */
4909 {
4910 cc = '\n';
4911 nc = 0;
4912 }
4913 else
4914 {
4915 cc = nc;
4916 nc = 0;
4917 }
4918 }
4919
4920 if (cc == 0) /* NUL is stored as NL */
4921 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00004922#ifdef FEAT_MBYTE
4923 if (enc_dbcs && (cc & 0xff) == 0)
4924 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
4925 second byte will cause trouble! */
4926#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927
4928 --no_mapping;
4929#ifdef FEAT_GUI
4930 if (gui.in_use)
4931 --allow_keys;
4932#endif
4933 if (nc)
4934 vungetc(nc);
4935 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
4936 return cc;
4937}
4938
4939/*
4940 * Insert character, taking care of special keys and mod_mask
4941 */
4942 static void
4943insert_special(c, allow_modmask, ctrlv)
4944 int c;
4945 int allow_modmask;
4946 int ctrlv; /* c was typed after CTRL-V */
4947{
4948 char_u *p;
4949 int len;
4950
4951 /*
4952 * Special function key, translate into "<Key>". Up to the last '>' is
4953 * inserted with ins_str(), so as not to replace characters in replace
4954 * mode.
4955 * Only use mod_mask for special keys, to avoid things like <S-Space>,
4956 * unless 'allow_modmask' is TRUE.
4957 */
4958#ifdef MACOS
4959 /* Command-key never produces a normal key */
4960 if (mod_mask & MOD_MASK_CMD)
4961 allow_modmask = TRUE;
4962#endif
4963 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
4964 {
4965 p = get_special_key_name(c, mod_mask);
4966 len = (int)STRLEN(p);
4967 c = p[len - 1];
4968 if (len > 2)
4969 {
4970 if (stop_arrow() == FAIL)
4971 return;
4972 p[len - 1] = NUL;
4973 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00004974 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 ctrlv = FALSE;
4976 }
4977 }
4978 if (stop_arrow() == OK)
4979 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
4980}
4981
4982/*
4983 * Special characters in this context are those that need processing other
4984 * than the simple insertion that can be performed here. This includes ESC
4985 * which terminates the insert, and CR/NL which need special processing to
4986 * open up a new line. This routine tries to optimize insertions performed by
4987 * the "redo", "undo" or "put" commands, so it needs to know when it should
4988 * stop and defer processing to the "normal" mechanism.
4989 * '0' and '^' are special, because they can be followed by CTRL-D.
4990 */
4991#ifdef EBCDIC
4992# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
4993#else
4994# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
4995#endif
4996
4997#ifdef FEAT_MBYTE
4998# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
4999#else
5000# define WHITECHAR(cc) vim_iswhite(cc)
5001#endif
5002
5003 void
5004insertchar(c, flags, second_indent)
5005 int c; /* character to insert or NUL */
5006 int flags; /* INSCHAR_FORMAT, etc. */
5007 int second_indent; /* indent for second line if >= 0 */
5008{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 int textwidth;
5010#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014
5015 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5016 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017
5018 /*
5019 * Try to break the line in two or more pieces when:
5020 * - Always do this if we have been called to do formatting only.
5021 * - Always do this when 'formatoptions' has the 'a' flag and the line
5022 * ends in white space.
5023 * - Otherwise:
5024 * - Don't do this if inserting a blank
5025 * - Don't do this if an existing character is being replaced, unless
5026 * we're in VREPLACE mode.
5027 * - Do this if the cursor is not on the line where insert started
5028 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5029 * before the insert.
5030 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5031 * before 'textwidth'
5032 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005033 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 && ((flags & INSCHAR_FORMAT)
5035 || (!vim_iswhite(c)
5036 && !((State & REPLACE_FLAG)
5037#ifdef FEAT_VREPLACE
5038 && !(State & VREPLACE_FLAG)
5039#endif
5040 && *ml_get_cursor() != NUL)
5041 && (curwin->w_cursor.lnum != Insstart.lnum
5042 || ((!has_format_option(FO_INS_LONG)
5043 || Insstart_textlen <= (colnr_T)textwidth)
5044 && (!fo_ins_blank
5045 || Insstart_blank_vcol <= (colnr_T)textwidth
5046 ))))))
5047 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005048 /* Format with 'formatexpr' when it's set. Use internal formatting
5049 * when 'formatexpr' isn't set or it returns non-zero. */
5050#if defined(FEAT_EVAL)
5051 if (*curbuf->b_p_fex == NUL
5052 || fex_format(curwin->w_cursor.lnum, 1L) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005054 internal_format(textwidth, second_indent, flags, c == NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005056
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 if (c == NUL) /* only formatting was wanted */
5058 return;
5059
5060#ifdef FEAT_COMMENTS
5061 /* Check whether this character should end a comment. */
5062 if (did_ai && (int)c == end_comment_pending)
5063 {
5064 char_u *line;
5065 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5066 int middle_len, end_len;
5067 int i;
5068
5069 /*
5070 * Need to remove existing (middle) comment leader and insert end
5071 * comment leader. First, check what comment leader we can find.
5072 */
5073 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5074 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5075 {
5076 /* Skip middle-comment string */
5077 while (*p && p[-1] != ':') /* find end of middle flags */
5078 ++p;
5079 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5080 /* Don't count trailing white space for middle_len */
5081 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5082 --middle_len;
5083
5084 /* Find the end-comment string */
5085 while (*p && p[-1] != ':') /* find end of end flags */
5086 ++p;
5087 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5088
5089 /* Skip white space before the cursor */
5090 i = curwin->w_cursor.col;
5091 while (--i >= 0 && vim_iswhite(line[i]))
5092 ;
5093 i++;
5094
5095 /* Skip to before the middle leader */
5096 i -= middle_len;
5097
5098 /* Check some expected things before we go on */
5099 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5100 {
5101 /* Backspace over all the stuff we want to replace */
5102 backspace_until_column(i);
5103
5104 /*
5105 * Insert the end-comment string, except for the last
5106 * character, which will get inserted as normal later.
5107 */
5108 ins_bytes_len(lead_end, end_len - 1);
5109 }
5110 }
5111 }
5112 end_comment_pending = NUL;
5113#endif
5114
5115 did_ai = FALSE;
5116#ifdef FEAT_SMARTINDENT
5117 did_si = FALSE;
5118 can_si = FALSE;
5119 can_si_back = FALSE;
5120#endif
5121
5122 /*
5123 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5124 * This speeds up normal text input considerably.
5125 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5126 * need to re-indent at a ':', or any other character (but not what
5127 * 'paste' is set)..
5128 */
5129#ifdef USE_ON_FLY_SCROLL
5130 dont_scroll = FALSE; /* allow scrolling here */
5131#endif
5132
5133 if ( !ISSPECIAL(c)
5134#ifdef FEAT_MBYTE
5135 && (!has_mbyte || (*mb_char2len)(c) == 1)
5136#endif
5137 && vpeekc() != NUL
5138 && !(State & REPLACE_FLAG)
5139#ifdef FEAT_CINDENT
5140 && !cindent_on()
5141#endif
5142#ifdef FEAT_RIGHTLEFT
5143 && !p_ri
5144#endif
5145 )
5146 {
5147#define INPUT_BUFLEN 100
5148 char_u buf[INPUT_BUFLEN + 1];
5149 int i;
5150 colnr_T virtcol = 0;
5151
5152 buf[0] = c;
5153 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005154 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 virtcol = get_nolist_virtcol();
5156 /*
5157 * Stop the string when:
5158 * - no more chars available
5159 * - finding a special character (command key)
5160 * - buffer is full
5161 * - running into the 'textwidth' boundary
5162 * - need to check for abbreviation: A non-word char after a word-char
5163 */
5164 while ( (c = vpeekc()) != NUL
5165 && !ISSPECIAL(c)
5166#ifdef FEAT_MBYTE
5167 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5168#endif
5169 && i < INPUT_BUFLEN
5170 && (textwidth == 0
5171 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5172 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5173 {
5174#ifdef FEAT_RIGHTLEFT
5175 c = vgetc();
5176 if (p_hkmap && KeyTyped)
5177 c = hkmap(c); /* Hebrew mode mapping */
5178# ifdef FEAT_FKMAP
5179 if (p_fkmap && KeyTyped)
5180 c = fkmap(c); /* Farsi mode mapping */
5181# endif
5182 buf[i++] = c;
5183#else
5184 buf[i++] = vgetc();
5185#endif
5186 }
5187
5188#ifdef FEAT_DIGRAPHS
5189 do_digraph(-1); /* clear digraphs */
5190 do_digraph(buf[i-1]); /* may be the start of a digraph */
5191#endif
5192 buf[i] = NUL;
5193 ins_str(buf);
5194 if (flags & INSCHAR_CTRLV)
5195 {
5196 redo_literal(*buf);
5197 i = 1;
5198 }
5199 else
5200 i = 0;
5201 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005202 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 }
5204 else
5205 {
5206#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005207 int cc;
5208
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5210 {
5211 char_u buf[MB_MAXBYTES + 1];
5212
5213 (*mb_char2bytes)(c, buf);
5214 buf[cc] = NUL;
5215 ins_char_bytes(buf, cc);
5216 AppendCharToRedobuff(c);
5217 }
5218 else
5219#endif
5220 {
5221 ins_char(c);
5222 if (flags & INSCHAR_CTRLV)
5223 redo_literal(c);
5224 else
5225 AppendCharToRedobuff(c);
5226 }
5227 }
5228}
5229
5230/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005231 * Format text at the current insert position.
5232 */
5233 static void
5234internal_format(textwidth, second_indent, flags, format_only)
5235 int textwidth;
5236 int second_indent;
5237 int flags;
5238 int format_only;
5239{
5240 int cc;
5241 int save_char = NUL;
5242 int haveto_redraw = FALSE;
5243 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5244#ifdef FEAT_MBYTE
5245 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5246#endif
5247 int fo_white_par = has_format_option(FO_WHITE_PAR);
5248 int first_line = TRUE;
5249#ifdef FEAT_COMMENTS
5250 colnr_T leader_len;
5251 int no_leader = FALSE;
5252 int do_comments = (flags & INSCHAR_DO_COM);
5253#endif
5254
5255 /*
5256 * When 'ai' is off we don't want a space under the cursor to be
5257 * deleted. Replace it with an 'x' temporarily.
5258 */
5259 if (!curbuf->b_p_ai)
5260 {
5261 cc = gchar_cursor();
5262 if (vim_iswhite(cc))
5263 {
5264 save_char = cc;
5265 pchar_cursor('x');
5266 }
5267 }
5268
5269 /*
5270 * Repeat breaking lines, until the current line is not too long.
5271 */
5272 while (!got_int)
5273 {
5274 int startcol; /* Cursor column at entry */
5275 int wantcol; /* column at textwidth border */
5276 int foundcol; /* column for start of spaces */
5277 int end_foundcol = 0; /* column for start of word */
5278 colnr_T len;
5279 colnr_T virtcol;
5280#ifdef FEAT_VREPLACE
5281 int orig_col = 0;
5282 char_u *saved_text = NULL;
5283#endif
5284 colnr_T col;
5285
5286 virtcol = get_nolist_virtcol();
5287 if (virtcol < (colnr_T)textwidth)
5288 break;
5289
5290#ifdef FEAT_COMMENTS
5291 if (no_leader)
5292 do_comments = FALSE;
5293 else if (!(flags & INSCHAR_FORMAT)
5294 && has_format_option(FO_WRAP_COMS))
5295 do_comments = TRUE;
5296
5297 /* Don't break until after the comment leader */
5298 if (do_comments)
5299 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5300 else
5301 leader_len = 0;
5302
5303 /* If the line doesn't start with a comment leader, then don't
5304 * start one in a following broken line. Avoids that a %word
5305 * moved to the start of the next line causes all following lines
5306 * to start with %. */
5307 if (leader_len == 0)
5308 no_leader = TRUE;
5309#endif
5310 if (!(flags & INSCHAR_FORMAT)
5311#ifdef FEAT_COMMENTS
5312 && leader_len == 0
5313#endif
5314 && !has_format_option(FO_WRAP))
5315
5316 {
5317 textwidth = 0;
5318 break;
5319 }
5320 if ((startcol = curwin->w_cursor.col) == 0)
5321 break;
5322
5323 /* find column of textwidth border */
5324 coladvance((colnr_T)textwidth);
5325 wantcol = curwin->w_cursor.col;
5326
5327 curwin->w_cursor.col = startcol - 1;
5328#ifdef FEAT_MBYTE
5329 /* Correct cursor for multi-byte character. */
5330 if (has_mbyte)
5331 mb_adjust_cursor();
5332#endif
5333 foundcol = 0;
5334
5335 /*
5336 * Find position to break at.
5337 * Stop at first entered white when 'formatoptions' has 'v'
5338 */
5339 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5340 || curwin->w_cursor.lnum != Insstart.lnum
5341 || curwin->w_cursor.col >= Insstart.col)
5342 {
5343 cc = gchar_cursor();
5344 if (WHITECHAR(cc))
5345 {
5346 /* remember position of blank just before text */
5347 end_foundcol = curwin->w_cursor.col;
5348
5349 /* find start of sequence of blanks */
5350 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5351 {
5352 dec_cursor();
5353 cc = gchar_cursor();
5354 }
5355 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5356 break; /* only spaces in front of text */
5357#ifdef FEAT_COMMENTS
5358 /* Don't break until after the comment leader */
5359 if (curwin->w_cursor.col < leader_len)
5360 break;
5361#endif
5362 if (has_format_option(FO_ONE_LETTER))
5363 {
5364 /* do not break after one-letter words */
5365 if (curwin->w_cursor.col == 0)
5366 break; /* one-letter word at begin */
5367
5368 col = curwin->w_cursor.col;
5369 dec_cursor();
5370 cc = gchar_cursor();
5371
5372 if (WHITECHAR(cc))
5373 continue; /* one-letter, continue */
5374 curwin->w_cursor.col = col;
5375 }
5376#ifdef FEAT_MBYTE
5377 if (has_mbyte)
5378 foundcol = curwin->w_cursor.col
5379 + (*mb_ptr2len)(ml_get_cursor());
5380 else
5381#endif
5382 foundcol = curwin->w_cursor.col + 1;
5383 if (curwin->w_cursor.col < (colnr_T)wantcol)
5384 break;
5385 }
5386#ifdef FEAT_MBYTE
5387 else if (cc >= 0x100 && fo_multibyte
5388 && curwin->w_cursor.col <= (colnr_T)wantcol)
5389 {
5390 /* Break after or before a multi-byte character. */
5391 foundcol = curwin->w_cursor.col;
5392 if (curwin->w_cursor.col < (colnr_T)wantcol)
5393 foundcol += (*mb_char2len)(cc);
5394 end_foundcol = foundcol;
5395 break;
5396 }
5397#endif
5398 if (curwin->w_cursor.col == 0)
5399 break;
5400 dec_cursor();
5401 }
5402
5403 if (foundcol == 0) /* no spaces, cannot break line */
5404 {
5405 curwin->w_cursor.col = startcol;
5406 break;
5407 }
5408
5409 /* Going to break the line, remove any "$" now. */
5410 undisplay_dollar();
5411
5412 /*
5413 * Offset between cursor position and line break is used by replace
5414 * stack functions. VREPLACE does not use this, and backspaces
5415 * over the text instead.
5416 */
5417#ifdef FEAT_VREPLACE
5418 if (State & VREPLACE_FLAG)
5419 orig_col = startcol; /* Will start backspacing from here */
5420 else
5421#endif
5422 replace_offset = startcol - end_foundcol - 1;
5423
5424 /*
5425 * adjust startcol for spaces that will be deleted and
5426 * characters that will remain on top line
5427 */
5428 curwin->w_cursor.col = foundcol;
5429 while (cc = gchar_cursor(), WHITECHAR(cc))
5430 inc_cursor();
5431 startcol -= curwin->w_cursor.col;
5432 if (startcol < 0)
5433 startcol = 0;
5434
5435#ifdef FEAT_VREPLACE
5436 if (State & VREPLACE_FLAG)
5437 {
5438 /*
5439 * In VREPLACE mode, we will backspace over the text to be
5440 * wrapped, so save a copy now to put on the next line.
5441 */
5442 saved_text = vim_strsave(ml_get_cursor());
5443 curwin->w_cursor.col = orig_col;
5444 if (saved_text == NULL)
5445 break; /* Can't do it, out of memory */
5446 saved_text[startcol] = NUL;
5447
5448 /* Backspace over characters that will move to the next line */
5449 if (!fo_white_par)
5450 backspace_until_column(foundcol);
5451 }
5452 else
5453#endif
5454 {
5455 /* put cursor after pos. to break line */
5456 if (!fo_white_par)
5457 curwin->w_cursor.col = foundcol;
5458 }
5459
5460 /*
5461 * Split the line just before the margin.
5462 * Only insert/delete lines, but don't really redraw the window.
5463 */
5464 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
5465 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
5466#ifdef FEAT_COMMENTS
5467 + (do_comments ? OPENLINE_DO_COM : 0)
5468#endif
5469 , old_indent);
5470 old_indent = 0;
5471
5472 replace_offset = 0;
5473 if (first_line)
5474 {
5475 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
5476 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
5477 if (second_indent >= 0)
5478 {
5479#ifdef FEAT_VREPLACE
5480 if (State & VREPLACE_FLAG)
5481 change_indent(INDENT_SET, second_indent, FALSE, NUL);
5482 else
5483#endif
5484 (void)set_indent(second_indent, SIN_CHANGED);
5485 }
5486 first_line = FALSE;
5487 }
5488
5489#ifdef FEAT_VREPLACE
5490 if (State & VREPLACE_FLAG)
5491 {
5492 /*
5493 * In VREPLACE mode we have backspaced over the text to be
5494 * moved, now we re-insert it into the new line.
5495 */
5496 ins_bytes(saved_text);
5497 vim_free(saved_text);
5498 }
5499 else
5500#endif
5501 {
5502 /*
5503 * Check if cursor is not past the NUL off the line, cindent
5504 * may have added or removed indent.
5505 */
5506 curwin->w_cursor.col += startcol;
5507 len = (colnr_T)STRLEN(ml_get_curline());
5508 if (curwin->w_cursor.col > len)
5509 curwin->w_cursor.col = len;
5510 }
5511
5512 haveto_redraw = TRUE;
5513#ifdef FEAT_CINDENT
5514 can_cindent = TRUE;
5515#endif
5516 /* moved the cursor, don't autoindent or cindent now */
5517 did_ai = FALSE;
5518#ifdef FEAT_SMARTINDENT
5519 did_si = FALSE;
5520 can_si = FALSE;
5521 can_si_back = FALSE;
5522#endif
5523 line_breakcheck();
5524 }
5525
5526 if (save_char != NUL) /* put back space after cursor */
5527 pchar_cursor(save_char);
5528
5529 if (!format_only && haveto_redraw)
5530 {
5531 update_topline();
5532 redraw_curbuf_later(VALID);
5533 }
5534}
5535
5536/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 * Called after inserting or deleting text: When 'formatoptions' includes the
5538 * 'a' flag format from the current line until the end of the paragraph.
5539 * Keep the cursor at the same position relative to the text.
5540 * The caller must have saved the cursor line for undo, following ones will be
5541 * saved here.
5542 */
5543 void
5544auto_format(trailblank, prev_line)
5545 int trailblank; /* when TRUE also format with trailing blank */
5546 int prev_line; /* may start in previous line */
5547{
5548 pos_T pos;
5549 colnr_T len;
5550 char_u *old;
5551 char_u *new, *pnew;
5552 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005553 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554
5555 if (!has_format_option(FO_AUTO))
5556 return;
5557
5558 pos = curwin->w_cursor;
5559 old = ml_get_curline();
5560
5561 /* may remove added space */
5562 check_auto_format(FALSE);
5563
5564 /* Don't format in Insert mode when the cursor is on a trailing blank, the
5565 * user might insert normal text next. Also skip formatting when "1" is
5566 * in 'formatoptions' and there is a single character before the cursor.
5567 * Otherwise the line would be broken and when typing another non-white
5568 * next they are not joined back together. */
5569 wasatend = (pos.col == STRLEN(old));
5570 if (*old != NUL && !trailblank && wasatend)
5571 {
5572 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005573 cc = gchar_cursor();
5574 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
5575 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005577 cc = gchar_cursor();
5578 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 {
5580 curwin->w_cursor = pos;
5581 return;
5582 }
5583 curwin->w_cursor = pos;
5584 }
5585
5586#ifdef FEAT_COMMENTS
5587 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
5588 * comments. */
5589 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
5590 && get_leader_len(old, NULL, FALSE) == 0)
5591 return;
5592#endif
5593
5594 /*
5595 * May start formatting in a previous line, so that after "x" a word is
5596 * moved to the previous line if it fits there now. Only when this is not
5597 * the start of a paragraph.
5598 */
5599 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
5600 {
5601 --curwin->w_cursor.lnum;
5602 if (u_save_cursor() == FAIL)
5603 return;
5604 }
5605
5606 /*
5607 * Do the formatting and restore the cursor position. "saved_cursor" will
5608 * be adjusted for the text formatting.
5609 */
5610 saved_cursor = pos;
5611 format_lines((linenr_T)-1);
5612 curwin->w_cursor = saved_cursor;
5613 saved_cursor.lnum = 0;
5614
5615 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5616 {
5617 /* "cannot happen" */
5618 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5619 coladvance((colnr_T)MAXCOL);
5620 }
5621 else
5622 check_cursor_col();
5623
5624 /* Insert mode: If the cursor is now after the end of the line while it
5625 * previously wasn't, the line was broken. Because of the rule above we
5626 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
5627 * formatted. */
5628 if (!wasatend && has_format_option(FO_WHITE_PAR))
5629 {
5630 new = ml_get_curline();
5631 len = STRLEN(new);
5632 if (curwin->w_cursor.col == len)
5633 {
5634 pnew = vim_strnsave(new, len + 2);
5635 pnew[len] = ' ';
5636 pnew[len + 1] = NUL;
5637 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
5638 /* remove the space later */
5639 did_add_space = TRUE;
5640 }
5641 else
5642 /* may remove added space */
5643 check_auto_format(FALSE);
5644 }
5645
5646 check_cursor();
5647}
5648
5649/*
5650 * When an extra space was added to continue a paragraph for auto-formatting,
5651 * delete it now. The space must be under the cursor, just after the insert
5652 * position.
5653 */
5654 static void
5655check_auto_format(end_insert)
5656 int end_insert; /* TRUE when ending Insert mode */
5657{
5658 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005659 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660
5661 if (did_add_space)
5662 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005663 cc = gchar_cursor();
5664 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 /* Somehow the space was removed already. */
5666 did_add_space = FALSE;
5667 else
5668 {
5669 if (!end_insert)
5670 {
5671 inc_cursor();
5672 c = gchar_cursor();
5673 dec_cursor();
5674 }
5675 if (c != NUL)
5676 {
5677 /* The space is no longer at the end of the line, delete it. */
5678 del_char(FALSE);
5679 did_add_space = FALSE;
5680 }
5681 }
5682 }
5683}
5684
5685/*
5686 * Find out textwidth to be used for formatting:
5687 * if 'textwidth' option is set, use it
5688 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
5689 * if invalid value, use 0.
5690 * Set default to window width (maximum 79) for "gq" operator.
5691 */
5692 int
5693comp_textwidth(ff)
5694 int ff; /* force formatting (for "Q" command) */
5695{
5696 int textwidth;
5697
5698 textwidth = curbuf->b_p_tw;
5699 if (textwidth == 0 && curbuf->b_p_wm)
5700 {
5701 /* The width is the window width minus 'wrapmargin' minus all the
5702 * things that add to the margin. */
5703 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
5704#ifdef FEAT_CMDWIN
5705 if (cmdwin_type != 0)
5706 textwidth -= 1;
5707#endif
5708#ifdef FEAT_FOLDING
5709 textwidth -= curwin->w_p_fdc;
5710#endif
5711#ifdef FEAT_SIGNS
5712 if (curwin->w_buffer->b_signlist != NULL
5713# ifdef FEAT_NETBEANS_INTG
5714 || usingNetbeans
5715# endif
5716 )
5717 textwidth -= 1;
5718#endif
5719 if (curwin->w_p_nu)
5720 textwidth -= 8;
5721 }
5722 if (textwidth < 0)
5723 textwidth = 0;
5724 if (ff && textwidth == 0)
5725 {
5726 textwidth = W_WIDTH(curwin) - 1;
5727 if (textwidth > 79)
5728 textwidth = 79;
5729 }
5730 return textwidth;
5731}
5732
5733/*
5734 * Put a character in the redo buffer, for when just after a CTRL-V.
5735 */
5736 static void
5737redo_literal(c)
5738 int c;
5739{
5740 char_u buf[10];
5741
5742 /* Only digits need special treatment. Translate them into a string of
5743 * three digits. */
5744 if (VIM_ISDIGIT(c))
5745 {
5746 sprintf((char *)buf, "%03d", c);
5747 AppendToRedobuff(buf);
5748 }
5749 else
5750 AppendCharToRedobuff(c);
5751}
5752
5753/*
5754 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005755 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 */
5757 static void
5758start_arrow(end_insert_pos)
5759 pos_T *end_insert_pos;
5760{
5761 if (!arrow_used) /* something has been inserted */
5762 {
5763 AppendToRedobuff(ESC_STR);
5764 stop_insert(end_insert_pos, FALSE);
5765 arrow_used = TRUE; /* this means we stopped the current insert */
5766 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005767#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005768 check_spell_redraw();
5769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770}
5771
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005772#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005773/*
5774 * If we skipped highlighting word at cursor, do it now.
5775 * It may be skipped again, thus reset spell_redraw_lnum first.
5776 */
5777 static void
5778check_spell_redraw()
5779{
5780 if (spell_redraw_lnum != 0)
5781 {
5782 linenr_T lnum = spell_redraw_lnum;
5783
5784 spell_redraw_lnum = 0;
5785 redrawWinline(lnum, FALSE);
5786 }
5787}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005788
5789/*
5790 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5791 * spelled word, if there is one.
5792 */
5793 static void
5794spell_back_to_badword()
5795{
5796 pos_T tpos = curwin->w_cursor;
5797
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00005798 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005799 if (curwin->w_cursor.col != tpos.col)
5800 start_arrow(&tpos);
5801}
Bram Moolenaar217ad922005-03-20 22:37:15 +00005802#endif
5803
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804/*
5805 * stop_arrow() is called before a change is made in insert mode.
5806 * If an arrow key has been used, start a new insertion.
5807 * Returns FAIL if undo is impossible, shouldn't insert then.
5808 */
5809 int
5810stop_arrow()
5811{
5812 if (arrow_used)
5813 {
5814 if (u_save_cursor() == OK)
5815 {
5816 arrow_used = FALSE;
5817 ins_need_undo = FALSE;
5818 }
5819 Insstart = curwin->w_cursor; /* new insertion starts here */
5820 Insstart_textlen = linetabsize(ml_get_curline());
5821 ai_col = 0;
5822#ifdef FEAT_VREPLACE
5823 if (State & VREPLACE_FLAG)
5824 {
5825 orig_line_count = curbuf->b_ml.ml_line_count;
5826 vr_lines_changed = 1;
5827 }
5828#endif
5829 ResetRedobuff();
5830 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00005831 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 }
5833 else if (ins_need_undo)
5834 {
5835 if (u_save_cursor() == OK)
5836 ins_need_undo = FALSE;
5837 }
5838
5839#ifdef FEAT_FOLDING
5840 /* Always open fold at the cursor line when inserting something. */
5841 foldOpenCursor();
5842#endif
5843
5844 return (arrow_used || ins_need_undo ? FAIL : OK);
5845}
5846
5847/*
5848 * do a few things to stop inserting
5849 */
5850 static void
5851stop_insert(end_insert_pos, esc)
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005852 pos_T *end_insert_pos; /* where insert ended */
5853 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005855 int cc;
5856 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857
5858 stop_redo_ins();
5859 replace_flush(); /* abandon replace stack */
5860
5861 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005862 * Save the inserted text for later redo with ^@ and CTRL-A.
5863 * Don't do it when "restart_edit" was set and nothing was inserted,
5864 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005866 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00005867 if (did_restart_edit == 0 || (ptr != NULL
5868 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005869 {
5870 vim_free(last_insert);
5871 last_insert = ptr;
5872 last_insert_skip = new_insert_skip;
5873 }
5874 else
5875 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876
5877 if (!arrow_used)
5878 {
5879 /* Auto-format now. It may seem strange to do this when stopping an
5880 * insertion (or moving the cursor), but it's required when appending
5881 * a line and having it end in a space. But only do it when something
5882 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005883 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005885 pos_T tpos = curwin->w_cursor;
5886
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 /* When the cursor is at the end of the line after a space the
5888 * formatting will move it to the following word. Avoid that by
5889 * moving the cursor onto the space. */
5890 cc = 'x';
5891 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
5892 {
5893 dec_cursor();
5894 cc = gchar_cursor();
5895 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005896 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 }
5898
5899 auto_format(TRUE, FALSE);
5900
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005901 if (vim_iswhite(cc))
5902 {
5903 if (gchar_cursor() != NUL)
5904 inc_cursor();
5905#ifdef FEAT_VIRTUALEDIT
5906 /* If the cursor is still at the same character, also keep
5907 * the "coladd". */
5908 if (gchar_cursor() == NUL
5909 && curwin->w_cursor.lnum == tpos.lnum
5910 && curwin->w_cursor.col == tpos.col)
5911 curwin->w_cursor.coladd = tpos.coladd;
5912#endif
5913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 }
5915
5916 /* If a space was inserted for auto-formatting, remove it now. */
5917 check_auto_format(TRUE);
5918
5919 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005920 * of the line, and put the cursor back.
5921 * Do this when ESC was used or moving the cursor up/down. */
5922 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
5923 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005925 pos_T tpos = curwin->w_cursor;
5926
5927 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar39f05632006-03-19 22:15:26 +00005928 for (;;)
5929 {
5930 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
5931 --curwin->w_cursor.col;
5932 cc = gchar_cursor();
5933 if (!vim_iswhite(cc))
5934 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 (void)del_char(TRUE);
Bram Moolenaar39f05632006-03-19 22:15:26 +00005936 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005937 if (curwin->w_cursor.lnum != tpos.lnum)
5938 curwin->w_cursor = tpos;
5939 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 ++curwin->w_cursor.col; /* put cursor back on the NUL */
5941
5942#ifdef FEAT_VISUAL
5943 /* <C-S-Right> may have started Visual mode, adjust the position for
5944 * deleted characters. */
5945 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
5946 {
5947 cc = STRLEN(ml_get_curline());
5948 if (VIsual.col > (colnr_T)cc)
5949 {
5950 VIsual.col = cc;
5951# ifdef FEAT_VIRTUALEDIT
5952 VIsual.coladd = 0;
5953# endif
5954 }
5955 }
5956#endif
5957 }
5958 }
5959 did_ai = FALSE;
5960#ifdef FEAT_SMARTINDENT
5961 did_si = FALSE;
5962 can_si = FALSE;
5963 can_si_back = FALSE;
5964#endif
5965
5966 /* set '[ and '] to the inserted text */
5967 curbuf->b_op_start = Insstart;
5968 curbuf->b_op_end = *end_insert_pos;
5969}
5970
5971/*
5972 * Set the last inserted text to a single character.
5973 * Used for the replace command.
5974 */
5975 void
5976set_last_insert(c)
5977 int c;
5978{
5979 char_u *s;
5980
5981 vim_free(last_insert);
5982#ifdef FEAT_MBYTE
5983 last_insert = alloc(MB_MAXBYTES * 3 + 5);
5984#else
5985 last_insert = alloc(6);
5986#endif
5987 if (last_insert != NULL)
5988 {
5989 s = last_insert;
5990 /* Use the CTRL-V only when entering a special char */
5991 if (c < ' ' || c == DEL)
5992 *s++ = Ctrl_V;
5993 s = add_char2buf(c, s);
5994 *s++ = ESC;
5995 *s++ = NUL;
5996 last_insert_skip = 0;
5997 }
5998}
5999
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006000#if defined(EXITFREE) || defined(PROTO)
6001 void
6002free_last_insert()
6003{
6004 vim_free(last_insert);
6005 last_insert = NULL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006006 vim_free(compl_orig_text);
6007 compl_orig_text = NULL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006008}
6009#endif
6010
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011/*
6012 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6013 * and CSI. Handle multi-byte characters.
6014 * Returns a pointer to after the added bytes.
6015 */
6016 char_u *
6017add_char2buf(c, s)
6018 int c;
6019 char_u *s;
6020{
6021#ifdef FEAT_MBYTE
6022 char_u temp[MB_MAXBYTES];
6023 int i;
6024 int len;
6025
6026 len = (*mb_char2bytes)(c, temp);
6027 for (i = 0; i < len; ++i)
6028 {
6029 c = temp[i];
6030#endif
6031 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6032 if (c == K_SPECIAL)
6033 {
6034 *s++ = K_SPECIAL;
6035 *s++ = KS_SPECIAL;
6036 *s++ = KE_FILLER;
6037 }
6038#ifdef FEAT_GUI
6039 else if (c == CSI)
6040 {
6041 *s++ = CSI;
6042 *s++ = KS_EXTRA;
6043 *s++ = (int)KE_CSI;
6044 }
6045#endif
6046 else
6047 *s++ = c;
6048#ifdef FEAT_MBYTE
6049 }
6050#endif
6051 return s;
6052}
6053
6054/*
6055 * move cursor to start of line
6056 * if flags & BL_WHITE move to first non-white
6057 * if flags & BL_SOL move to first non-white if startofline is set,
6058 * otherwise keep "curswant" column
6059 * if flags & BL_FIX don't leave the cursor on a NUL.
6060 */
6061 void
6062beginline(flags)
6063 int flags;
6064{
6065 if ((flags & BL_SOL) && !p_sol)
6066 coladvance(curwin->w_curswant);
6067 else
6068 {
6069 curwin->w_cursor.col = 0;
6070#ifdef FEAT_VIRTUALEDIT
6071 curwin->w_cursor.coladd = 0;
6072#endif
6073
6074 if (flags & (BL_WHITE | BL_SOL))
6075 {
6076 char_u *ptr;
6077
6078 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6079 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6080 ++curwin->w_cursor.col;
6081 }
6082 curwin->w_set_curswant = TRUE;
6083 }
6084}
6085
6086/*
6087 * oneright oneleft cursor_down cursor_up
6088 *
6089 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006090 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 * Return OK when successful, FAIL when we hit a line of file boundary.
6092 */
6093
6094 int
6095oneright()
6096{
6097 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099
6100#ifdef FEAT_VIRTUALEDIT
6101 if (virtual_active())
6102 {
6103 pos_T prevpos = curwin->w_cursor;
6104
6105 /* Adjust for multi-wide char (excluding TAB) */
6106 ptr = ml_get_cursor();
6107 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006108# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006110# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006112# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 ))
6114 ? ptr2cells(ptr) : 1));
6115 curwin->w_set_curswant = TRUE;
6116 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6117 return (prevpos.col != curwin->w_cursor.col
6118 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
6119 }
6120#endif
6121
6122 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006123 if (*ptr == NUL)
6124 return FAIL; /* already at the very end */
6125
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006127 if (has_mbyte)
6128 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 else
6130#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006131 l = 1;
6132
6133 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6134 * contains "onemore". */
6135 if (ptr[l] == NUL
6136#ifdef FEAT_VIRTUALEDIT
6137 && (ve_flags & VE_ONEMORE) == 0
6138#endif
6139 )
6140 return FAIL;
6141 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142
6143 curwin->w_set_curswant = TRUE;
6144 return OK;
6145}
6146
6147 int
6148oneleft()
6149{
6150#ifdef FEAT_VIRTUALEDIT
6151 if (virtual_active())
6152 {
6153 int width;
6154 int v = getviscol();
6155
6156 if (v == 0)
6157 return FAIL;
6158
6159# ifdef FEAT_LINEBREAK
6160 /* We might get stuck on 'showbreak', skip over it. */
6161 width = 1;
6162 for (;;)
6163 {
6164 coladvance(v - width);
6165 /* getviscol() is slow, skip it when 'showbreak' is empty and
6166 * there are no multi-byte characters */
6167 if ((*p_sbr == NUL
6168# ifdef FEAT_MBYTE
6169 && !has_mbyte
6170# endif
6171 ) || getviscol() < v)
6172 break;
6173 ++width;
6174 }
6175# else
6176 coladvance(v - 1);
6177# endif
6178
6179 if (curwin->w_cursor.coladd == 1)
6180 {
6181 char_u *ptr;
6182
6183 /* Adjust for multi-wide char (not a TAB) */
6184 ptr = ml_get_cursor();
6185 if (*ptr != TAB && vim_isprintc(
6186# ifdef FEAT_MBYTE
6187 (*mb_ptr2char)(ptr)
6188# else
6189 *ptr
6190# endif
6191 ) && ptr2cells(ptr) > 1)
6192 curwin->w_cursor.coladd = 0;
6193 }
6194
6195 curwin->w_set_curswant = TRUE;
6196 return OK;
6197 }
6198#endif
6199
6200 if (curwin->w_cursor.col == 0)
6201 return FAIL;
6202
6203 curwin->w_set_curswant = TRUE;
6204 --curwin->w_cursor.col;
6205
6206#ifdef FEAT_MBYTE
6207 /* if the character on the left of the current cursor is a multi-byte
6208 * character, move to its first byte */
6209 if (has_mbyte)
6210 mb_adjust_cursor();
6211#endif
6212 return OK;
6213}
6214
6215 int
6216cursor_up(n, upd_topline)
6217 long n;
6218 int upd_topline; /* When TRUE: update topline */
6219{
6220 linenr_T lnum;
6221
6222 if (n > 0)
6223 {
6224 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006225 /* This fails if the cursor is already in the first line or the count
6226 * is larger than the line number and '-' is in 'cpoptions' */
6227 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 return FAIL;
6229 if (n >= lnum)
6230 lnum = 1;
6231 else
6232#ifdef FEAT_FOLDING
6233 if (hasAnyFolding(curwin))
6234 {
6235 /*
6236 * Count each sequence of folded lines as one logical line.
6237 */
6238 /* go to the the start of the current fold */
6239 (void)hasFolding(lnum, &lnum, NULL);
6240
6241 while (n--)
6242 {
6243 /* move up one line */
6244 --lnum;
6245 if (lnum <= 1)
6246 break;
6247 /* If we entered a fold, move to the beginning, unless in
6248 * Insert mode or when 'foldopen' contains "all": it will open
6249 * in a moment. */
6250 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6251 (void)hasFolding(lnum, &lnum, NULL);
6252 }
6253 if (lnum < 1)
6254 lnum = 1;
6255 }
6256 else
6257#endif
6258 lnum -= n;
6259 curwin->w_cursor.lnum = lnum;
6260 }
6261
6262 /* try to advance to the column we want to be at */
6263 coladvance(curwin->w_curswant);
6264
6265 if (upd_topline)
6266 update_topline(); /* make sure curwin->w_topline is valid */
6267
6268 return OK;
6269}
6270
6271/*
6272 * Cursor down a number of logical lines.
6273 */
6274 int
6275cursor_down(n, upd_topline)
6276 long n;
6277 int upd_topline; /* When TRUE: update topline */
6278{
6279 linenr_T lnum;
6280
6281 if (n > 0)
6282 {
6283 lnum = curwin->w_cursor.lnum;
6284#ifdef FEAT_FOLDING
6285 /* Move to last line of fold, will fail if it's the end-of-file. */
6286 (void)hasFolding(lnum, NULL, &lnum);
6287#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006288 /* This fails if the cursor is already in the last line or would move
6289 * beyound the last line and '-' is in 'cpoptions' */
6290 if (lnum >= curbuf->b_ml.ml_line_count
6291 || (lnum + n > curbuf->b_ml.ml_line_count
6292 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 return FAIL;
6294 if (lnum + n >= curbuf->b_ml.ml_line_count)
6295 lnum = curbuf->b_ml.ml_line_count;
6296 else
6297#ifdef FEAT_FOLDING
6298 if (hasAnyFolding(curwin))
6299 {
6300 linenr_T last;
6301
6302 /* count each sequence of folded lines as one logical line */
6303 while (n--)
6304 {
6305 if (hasFolding(lnum, NULL, &last))
6306 lnum = last + 1;
6307 else
6308 ++lnum;
6309 if (lnum >= curbuf->b_ml.ml_line_count)
6310 break;
6311 }
6312 if (lnum > curbuf->b_ml.ml_line_count)
6313 lnum = curbuf->b_ml.ml_line_count;
6314 }
6315 else
6316#endif
6317 lnum += n;
6318 curwin->w_cursor.lnum = lnum;
6319 }
6320
6321 /* try to advance to the column we want to be at */
6322 coladvance(curwin->w_curswant);
6323
6324 if (upd_topline)
6325 update_topline(); /* make sure curwin->w_topline is valid */
6326
6327 return OK;
6328}
6329
6330/*
6331 * Stuff the last inserted text in the read buffer.
6332 * Last_insert actually is a copy of the redo buffer, so we
6333 * first have to remove the command.
6334 */
6335 int
6336stuff_inserted(c, count, no_esc)
6337 int c; /* Command character to be inserted */
6338 long count; /* Repeat this many times */
6339 int no_esc; /* Don't add an ESC at the end */
6340{
6341 char_u *esc_ptr;
6342 char_u *ptr;
6343 char_u *last_ptr;
6344 char_u last = NUL;
6345
6346 ptr = get_last_insert();
6347 if (ptr == NULL)
6348 {
6349 EMSG(_(e_noinstext));
6350 return FAIL;
6351 }
6352
6353 /* may want to stuff the command character, to start Insert mode */
6354 if (c != NUL)
6355 stuffcharReadbuff(c);
6356 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
6357 *esc_ptr = NUL; /* remove the ESC */
6358
6359 /* when the last char is either "0" or "^" it will be quoted if no ESC
6360 * comes after it OR if it will inserted more than once and "ptr"
6361 * starts with ^D. -- Acevedo
6362 */
6363 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
6364 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
6365 && (no_esc || (*ptr == Ctrl_D && count > 1)))
6366 {
6367 last = *last_ptr;
6368 *last_ptr = NUL;
6369 }
6370
6371 do
6372 {
6373 stuffReadbuff(ptr);
6374 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6375 if (last)
6376 stuffReadbuff((char_u *)(last == '0'
6377 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
6378 : IF_EB("\026^", CTRL_V_STR "^")));
6379 }
6380 while (--count > 0);
6381
6382 if (last)
6383 *last_ptr = last;
6384
6385 if (esc_ptr != NULL)
6386 *esc_ptr = ESC; /* put the ESC back */
6387
6388 /* may want to stuff a trailing ESC, to get out of Insert mode */
6389 if (!no_esc)
6390 stuffcharReadbuff(ESC);
6391
6392 return OK;
6393}
6394
6395 char_u *
6396get_last_insert()
6397{
6398 if (last_insert == NULL)
6399 return NULL;
6400 return last_insert + last_insert_skip;
6401}
6402
6403/*
6404 * Get last inserted string, and remove trailing <Esc>.
6405 * Returns pointer to allocated memory (must be freed) or NULL.
6406 */
6407 char_u *
6408get_last_insert_save()
6409{
6410 char_u *s;
6411 int len;
6412
6413 if (last_insert == NULL)
6414 return NULL;
6415 s = vim_strsave(last_insert + last_insert_skip);
6416 if (s != NULL)
6417 {
6418 len = (int)STRLEN(s);
6419 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
6420 s[len - 1] = NUL;
6421 }
6422 return s;
6423}
6424
6425/*
6426 * Check the word in front of the cursor for an abbreviation.
6427 * Called when the non-id character "c" has been entered.
6428 * When an abbreviation is recognized it is removed from the text and
6429 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
6430 */
6431 static int
6432echeck_abbr(c)
6433 int c;
6434{
6435 /* Don't check for abbreviation in paste mode, when disabled and just
6436 * after moving around with cursor keys. */
6437 if (p_paste || no_abbr || arrow_used)
6438 return FALSE;
6439
6440 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
6441 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
6442}
6443
6444/*
6445 * replace-stack functions
6446 *
6447 * When replacing characters, the replaced characters are remembered for each
6448 * new character. This is used to re-insert the old text when backspacing.
6449 *
6450 * There is a NUL headed list of characters for each character that is
6451 * currently in the file after the insertion point. When BS is used, one NUL
6452 * headed list is put back for the deleted character.
6453 *
6454 * For a newline, there are two NUL headed lists. One contains the characters
6455 * that the NL replaced. The extra one stores the characters after the cursor
6456 * that were deleted (always white space).
6457 *
6458 * Replace_offset is normally 0, in which case replace_push will add a new
6459 * character at the end of the stack. If replace_offset is not 0, that many
6460 * characters will be left on the stack above the newly inserted character.
6461 */
6462
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00006463static char_u *replace_stack = NULL;
6464static long replace_stack_nr = 0; /* next entry in replace stack */
6465static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466
6467 void
6468replace_push(c)
6469 int c; /* character that is replaced (NUL is none) */
6470{
6471 char_u *p;
6472
6473 if (replace_stack_nr < replace_offset) /* nothing to do */
6474 return;
6475 if (replace_stack_len <= replace_stack_nr)
6476 {
6477 replace_stack_len += 50;
6478 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
6479 if (p == NULL) /* out of memory */
6480 {
6481 replace_stack_len -= 50;
6482 return;
6483 }
6484 if (replace_stack != NULL)
6485 {
6486 mch_memmove(p, replace_stack,
6487 (size_t)(replace_stack_nr * sizeof(char_u)));
6488 vim_free(replace_stack);
6489 }
6490 replace_stack = p;
6491 }
6492 p = replace_stack + replace_stack_nr - replace_offset;
6493 if (replace_offset)
6494 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
6495 *p = c;
6496 ++replace_stack_nr;
6497}
6498
6499/*
6500 * call replace_push(c) with replace_offset set to the first NUL.
6501 */
6502 static void
6503replace_push_off(c)
6504 int c;
6505{
6506 char_u *p;
6507
6508 p = replace_stack + replace_stack_nr;
6509 for (replace_offset = 1; replace_offset < replace_stack_nr;
6510 ++replace_offset)
6511 if (*--p == NUL)
6512 break;
6513 replace_push(c);
6514 replace_offset = 0;
6515}
6516
6517/*
6518 * Pop one item from the replace stack.
6519 * return -1 if stack empty
6520 * return replaced character or NUL otherwise
6521 */
6522 static int
6523replace_pop()
6524{
6525 if (replace_stack_nr == 0)
6526 return -1;
6527 return (int)replace_stack[--replace_stack_nr];
6528}
6529
6530/*
6531 * Join the top two items on the replace stack. This removes to "off"'th NUL
6532 * encountered.
6533 */
6534 static void
6535replace_join(off)
6536 int off; /* offset for which NUL to remove */
6537{
6538 int i;
6539
6540 for (i = replace_stack_nr; --i >= 0; )
6541 if (replace_stack[i] == NUL && off-- <= 0)
6542 {
6543 --replace_stack_nr;
6544 mch_memmove(replace_stack + i, replace_stack + i + 1,
6545 (size_t)(replace_stack_nr - i));
6546 return;
6547 }
6548}
6549
6550/*
6551 * Pop bytes from the replace stack until a NUL is found, and insert them
6552 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
6553 */
6554 static void
6555replace_pop_ins()
6556{
6557 int cc;
6558 int oldState = State;
6559
6560 State = NORMAL; /* don't want REPLACE here */
6561 while ((cc = replace_pop()) > 0)
6562 {
6563#ifdef FEAT_MBYTE
6564 mb_replace_pop_ins(cc);
6565#else
6566 ins_char(cc);
6567#endif
6568 dec_cursor();
6569 }
6570 State = oldState;
6571}
6572
6573#ifdef FEAT_MBYTE
6574/*
6575 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
6576 * indicates a multi-byte char, pop the other bytes too.
6577 */
6578 static void
6579mb_replace_pop_ins(cc)
6580 int cc;
6581{
6582 int n;
6583 char_u buf[MB_MAXBYTES];
6584 int i;
6585 int c;
6586
6587 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
6588 {
6589 buf[0] = cc;
6590 for (i = 1; i < n; ++i)
6591 buf[i] = replace_pop();
6592 ins_bytes_len(buf, n);
6593 }
6594 else
6595 ins_char(cc);
6596
6597 if (enc_utf8)
6598 /* Handle composing chars. */
6599 for (;;)
6600 {
6601 c = replace_pop();
6602 if (c == -1) /* stack empty */
6603 break;
6604 if ((n = MB_BYTE2LEN(c)) == 1)
6605 {
6606 /* Not a multi-byte char, put it back. */
6607 replace_push(c);
6608 break;
6609 }
6610 else
6611 {
6612 buf[0] = c;
6613 for (i = 1; i < n; ++i)
6614 buf[i] = replace_pop();
6615 if (utf_iscomposing(utf_ptr2char(buf)))
6616 ins_bytes_len(buf, n);
6617 else
6618 {
6619 /* Not a composing char, put it back. */
6620 for (i = n - 1; i >= 0; --i)
6621 replace_push(buf[i]);
6622 break;
6623 }
6624 }
6625 }
6626}
6627#endif
6628
6629/*
6630 * make the replace stack empty
6631 * (called when exiting replace mode)
6632 */
6633 static void
6634replace_flush()
6635{
6636 vim_free(replace_stack);
6637 replace_stack = NULL;
6638 replace_stack_len = 0;
6639 replace_stack_nr = 0;
6640}
6641
6642/*
6643 * Handle doing a BS for one character.
6644 * cc < 0: replace stack empty, just move cursor
6645 * cc == 0: character was inserted, delete it
6646 * cc > 0: character was replaced, put cc (first byte of original char) back
6647 * and check for more characters to be put back
6648 */
6649 static void
6650replace_do_bs()
6651{
6652 int cc;
6653#ifdef FEAT_VREPLACE
6654 int orig_len = 0;
6655 int ins_len;
6656 int orig_vcols = 0;
6657 colnr_T start_vcol;
6658 char_u *p;
6659 int i;
6660 int vcol;
6661#endif
6662
6663 cc = replace_pop();
6664 if (cc > 0)
6665 {
6666#ifdef FEAT_VREPLACE
6667 if (State & VREPLACE_FLAG)
6668 {
6669 /* Get the number of screen cells used by the character we are
6670 * going to delete. */
6671 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
6672 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
6673 }
6674#endif
6675#ifdef FEAT_MBYTE
6676 if (has_mbyte)
6677 {
6678 del_char(FALSE);
6679# ifdef FEAT_VREPLACE
6680 if (State & VREPLACE_FLAG)
6681 orig_len = STRLEN(ml_get_cursor());
6682# endif
6683 replace_push(cc);
6684 }
6685 else
6686#endif
6687 {
6688 pchar_cursor(cc);
6689#ifdef FEAT_VREPLACE
6690 if (State & VREPLACE_FLAG)
6691 orig_len = STRLEN(ml_get_cursor()) - 1;
6692#endif
6693 }
6694 replace_pop_ins();
6695
6696#ifdef FEAT_VREPLACE
6697 if (State & VREPLACE_FLAG)
6698 {
6699 /* Get the number of screen cells used by the inserted characters */
6700 p = ml_get_cursor();
6701 ins_len = STRLEN(p) - orig_len;
6702 vcol = start_vcol;
6703 for (i = 0; i < ins_len; ++i)
6704 {
6705 vcol += chartabsize(p + i, vcol);
6706#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006707 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708#endif
6709 }
6710 vcol -= start_vcol;
6711
6712 /* Delete spaces that were inserted after the cursor to keep the
6713 * text aligned. */
6714 curwin->w_cursor.col += ins_len;
6715 while (vcol > orig_vcols && gchar_cursor() == ' ')
6716 {
6717 del_char(FALSE);
6718 ++orig_vcols;
6719 }
6720 curwin->w_cursor.col -= ins_len;
6721 }
6722#endif
6723
6724 /* mark the buffer as changed and prepare for displaying */
6725 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
6726 }
6727 else if (cc == 0)
6728 (void)del_char(FALSE);
6729}
6730
6731#ifdef FEAT_CINDENT
6732/*
6733 * Return TRUE if C-indenting is on.
6734 */
6735 static int
6736cindent_on()
6737{
6738 return (!p_paste && (curbuf->b_p_cin
6739# ifdef FEAT_EVAL
6740 || *curbuf->b_p_inde != NUL
6741# endif
6742 ));
6743}
6744#endif
6745
6746#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
6747/*
6748 * Re-indent the current line, based on the current contents of it and the
6749 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
6750 * confused what all the part that handles Control-T is doing that I'm not.
6751 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
6752 */
6753
6754 void
6755fixthisline(get_the_indent)
6756 int (*get_the_indent) __ARGS((void));
6757{
6758 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
6759 if (linewhite(curwin->w_cursor.lnum))
6760 did_ai = TRUE; /* delete the indent if the line stays empty */
6761}
6762
6763 void
6764fix_indent()
6765{
6766 if (p_paste)
6767 return;
6768# ifdef FEAT_LISP
6769 if (curbuf->b_p_lisp && curbuf->b_p_ai)
6770 fixthisline(get_lisp_indent);
6771# endif
6772# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
6773 else
6774# endif
6775# ifdef FEAT_CINDENT
6776 if (cindent_on())
6777 do_c_expr_indent();
6778# endif
6779}
6780
6781#endif
6782
6783#ifdef FEAT_CINDENT
6784/*
6785 * return TRUE if 'cinkeys' contains the key "keytyped",
6786 * when == '*': Only if key is preceded with '*' (indent before insert)
6787 * when == '!': Only if key is prededed with '!' (don't insert)
6788 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
6789 *
6790 * "keytyped" can have a few special values:
6791 * KEY_OPEN_FORW
6792 * KEY_OPEN_BACK
6793 * KEY_COMPLETE just finished completion.
6794 *
6795 * If line_is_empty is TRUE accept keys with '0' before them.
6796 */
6797 int
6798in_cinkeys(keytyped, when, line_is_empty)
6799 int keytyped;
6800 int when;
6801 int line_is_empty;
6802{
6803 char_u *look;
6804 int try_match;
6805 int try_match_word;
6806 char_u *p;
6807 char_u *line;
6808 int icase;
6809 int i;
6810
6811#ifdef FEAT_EVAL
6812 if (*curbuf->b_p_inde != NUL)
6813 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
6814 else
6815#endif
6816 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
6817 while (*look)
6818 {
6819 /*
6820 * Find out if we want to try a match with this key, depending on
6821 * 'when' and a '*' or '!' before the key.
6822 */
6823 switch (when)
6824 {
6825 case '*': try_match = (*look == '*'); break;
6826 case '!': try_match = (*look == '!'); break;
6827 default: try_match = (*look != '*'); break;
6828 }
6829 if (*look == '*' || *look == '!')
6830 ++look;
6831
6832 /*
6833 * If there is a '0', only accept a match if the line is empty.
6834 * But may still match when typing last char of a word.
6835 */
6836 if (*look == '0')
6837 {
6838 try_match_word = try_match;
6839 if (!line_is_empty)
6840 try_match = FALSE;
6841 ++look;
6842 }
6843 else
6844 try_match_word = FALSE;
6845
6846 /*
6847 * does it look like a control character?
6848 */
6849 if (*look == '^'
6850#ifdef EBCDIC
6851 && (Ctrl_chr(look[1]) != 0)
6852#else
6853 && look[1] >= '?' && look[1] <= '_'
6854#endif
6855 )
6856 {
6857 if (try_match && keytyped == Ctrl_chr(look[1]))
6858 return TRUE;
6859 look += 2;
6860 }
6861 /*
6862 * 'o' means "o" command, open forward.
6863 * 'O' means "O" command, open backward.
6864 */
6865 else if (*look == 'o')
6866 {
6867 if (try_match && keytyped == KEY_OPEN_FORW)
6868 return TRUE;
6869 ++look;
6870 }
6871 else if (*look == 'O')
6872 {
6873 if (try_match && keytyped == KEY_OPEN_BACK)
6874 return TRUE;
6875 ++look;
6876 }
6877
6878 /*
6879 * 'e' means to check for "else" at start of line and just before the
6880 * cursor.
6881 */
6882 else if (*look == 'e')
6883 {
6884 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
6885 {
6886 p = ml_get_curline();
6887 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
6888 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
6889 return TRUE;
6890 }
6891 ++look;
6892 }
6893
6894 /*
6895 * ':' only causes an indent if it is at the end of a label or case
6896 * statement, or when it was before typing the ':' (to fix
6897 * class::method for C++).
6898 */
6899 else if (*look == ':')
6900 {
6901 if (try_match && keytyped == ':')
6902 {
6903 p = ml_get_curline();
6904 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
6905 return TRUE;
6906 if (curwin->w_cursor.col > 2
6907 && p[curwin->w_cursor.col - 1] == ':'
6908 && p[curwin->w_cursor.col - 2] == ':')
6909 {
6910 p[curwin->w_cursor.col - 1] = ' ';
6911 i = (cin_iscase(p) || cin_isscopedecl(p)
6912 || cin_islabel(30));
6913 p = ml_get_curline();
6914 p[curwin->w_cursor.col - 1] = ':';
6915 if (i)
6916 return TRUE;
6917 }
6918 }
6919 ++look;
6920 }
6921
6922
6923 /*
6924 * Is it a key in <>, maybe?
6925 */
6926 else if (*look == '<')
6927 {
6928 if (try_match)
6929 {
6930 /*
6931 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
6932 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
6933 * >, *, : and ! keys if they really really want to.
6934 */
6935 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
6936 && keytyped == look[1])
6937 return TRUE;
6938
6939 if (keytyped == get_special_key_code(look + 1))
6940 return TRUE;
6941 }
6942 while (*look && *look != '>')
6943 look++;
6944 while (*look == '>')
6945 look++;
6946 }
6947
6948 /*
6949 * Is it a word: "=word"?
6950 */
6951 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
6952 {
6953 ++look;
6954 if (*look == '~')
6955 {
6956 icase = TRUE;
6957 ++look;
6958 }
6959 else
6960 icase = FALSE;
6961 p = vim_strchr(look, ',');
6962 if (p == NULL)
6963 p = look + STRLEN(look);
6964 if ((try_match || try_match_word)
6965 && curwin->w_cursor.col >= (colnr_T)(p - look))
6966 {
6967 int match = FALSE;
6968
6969#ifdef FEAT_INS_EXPAND
6970 if (keytyped == KEY_COMPLETE)
6971 {
6972 char_u *s;
6973
6974 /* Just completed a word, check if it starts with "look".
6975 * search back for the start of a word. */
6976 line = ml_get_curline();
6977# ifdef FEAT_MBYTE
6978 if (has_mbyte)
6979 {
6980 char_u *n;
6981
6982 for (s = line + curwin->w_cursor.col; s > line; s = n)
6983 {
6984 n = mb_prevptr(line, s);
6985 if (!vim_iswordp(n))
6986 break;
6987 }
6988 }
6989 else
6990# endif
6991 for (s = line + curwin->w_cursor.col; s > line; --s)
6992 if (!vim_iswordc(s[-1]))
6993 break;
6994 if (s + (p - look) <= line + curwin->w_cursor.col
6995 && (icase
6996 ? MB_STRNICMP(s, look, p - look)
6997 : STRNCMP(s, look, p - look)) == 0)
6998 match = TRUE;
6999 }
7000 else
7001#endif
7002 /* TODO: multi-byte */
7003 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7004 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7005 {
7006 line = ml_get_cursor();
7007 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7008 || !vim_iswordc(line[-(p - look) - 1]))
7009 && (icase
7010 ? MB_STRNICMP(line - (p - look), look, p - look)
7011 : STRNCMP(line - (p - look), look, p - look))
7012 == 0)
7013 match = TRUE;
7014 }
7015 if (match && try_match_word && !try_match)
7016 {
7017 /* "0=word": Check if there are only blanks before the
7018 * word. */
7019 line = ml_get_curline();
7020 if ((int)(skipwhite(line) - line) !=
7021 (int)(curwin->w_cursor.col - (p - look)))
7022 match = FALSE;
7023 }
7024 if (match)
7025 return TRUE;
7026 }
7027 look = p;
7028 }
7029
7030 /*
7031 * ok, it's a boring generic character.
7032 */
7033 else
7034 {
7035 if (try_match && *look == keytyped)
7036 return TRUE;
7037 ++look;
7038 }
7039
7040 /*
7041 * Skip over ", ".
7042 */
7043 look = skip_to_option_part(look);
7044 }
7045 return FALSE;
7046}
7047#endif /* FEAT_CINDENT */
7048
7049#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7050/*
7051 * Map Hebrew keyboard when in hkmap mode.
7052 */
7053 int
7054hkmap(c)
7055 int c;
7056{
7057 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7058 {
7059 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7060 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7061 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7062 static char_u map[26] =
7063 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7064 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7065 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7066 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7067 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7068 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7069 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7070 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7071 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7072
7073 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7074 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7075 /* '-1'='sofit' */
7076 else if (c == 'x')
7077 return 'X';
7078 else if (c == 'q')
7079 return '\''; /* {geresh}={'} */
7080 else if (c == 246)
7081 return ' '; /* \"o --> ' ' for a german keyboard */
7082 else if (c == 228)
7083 return ' '; /* \"a --> ' ' -- / -- */
7084 else if (c == 252)
7085 return ' '; /* \"u --> ' ' -- / -- */
7086#ifdef EBCDIC
7087 else if (islower(c))
7088#else
7089 /* NOTE: islower() does not do the right thing for us on Linux so we
7090 * do this the same was as 5.7 and previous, so it works correctly on
7091 * all systems. Specifically, the e.g. Delete and Arrow keys are
7092 * munged and won't work if e.g. searching for Hebrew text.
7093 */
7094 else if (c >= 'a' && c <= 'z')
7095#endif
7096 return (int)(map[CharOrdLow(c)] + p_aleph);
7097 else
7098 return c;
7099 }
7100 else
7101 {
7102 switch (c)
7103 {
7104 case '`': return ';';
7105 case '/': return '.';
7106 case '\'': return ',';
7107 case 'q': return '/';
7108 case 'w': return '\'';
7109
7110 /* Hebrew letters - set offset from 'a' */
7111 case ',': c = '{'; break;
7112 case '.': c = 'v'; break;
7113 case ';': c = 't'; break;
7114 default: {
7115 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7116
7117#ifdef EBCDIC
7118 /* see note about islower() above */
7119 if (!islower(c))
7120#else
7121 if (c < 'a' || c > 'z')
7122#endif
7123 return c;
7124 c = str[CharOrdLow(c)];
7125 break;
7126 }
7127 }
7128
7129 return (int)(CharOrdLow(c) + p_aleph);
7130 }
7131}
7132#endif
7133
7134 static void
7135ins_reg()
7136{
7137 int need_redraw = FALSE;
7138 int regname;
7139 int literally = 0;
7140
7141 /*
7142 * If we are going to wait for a character, show a '"'.
7143 */
7144 pc_status = PC_STATUS_UNSET;
7145 if (redrawing() && !char_avail())
7146 {
7147 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007148 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149
7150 edit_putchar('"', TRUE);
7151#ifdef FEAT_CMDL_INFO
7152 add_to_showcmd_c(Ctrl_R);
7153#endif
7154 }
7155
7156#ifdef USE_ON_FLY_SCROLL
7157 dont_scroll = TRUE; /* disallow scrolling here */
7158#endif
7159
7160 /*
7161 * Don't map the register name. This also prevents the mode message to be
7162 * deleted when ESC is hit.
7163 */
7164 ++no_mapping;
7165 regname = safe_vgetc();
7166#ifdef FEAT_LANGMAP
7167 LANGMAP_ADJUST(regname, TRUE);
7168#endif
7169 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
7170 {
7171 /* Get a third key for literal register insertion */
7172 literally = regname;
7173#ifdef FEAT_CMDL_INFO
7174 add_to_showcmd_c(literally);
7175#endif
7176 regname = safe_vgetc();
7177#ifdef FEAT_LANGMAP
7178 LANGMAP_ADJUST(regname, TRUE);
7179#endif
7180 }
7181 --no_mapping;
7182
7183#ifdef FEAT_EVAL
7184 /*
7185 * Don't call u_sync() while getting the expression,
7186 * evaluating it or giving an error message for it!
7187 */
7188 ++no_u_sync;
7189 if (regname == '=')
7190 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007191# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007193# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007195# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 /* Restore the Input Method. */
7197 if (im_on)
7198 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007199# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00007201 if (regname == NUL || !valid_yank_reg(regname, FALSE))
7202 {
7203 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 else
7207 {
7208#endif
7209 if (literally == Ctrl_O || literally == Ctrl_P)
7210 {
7211 /* Append the command to the redo buffer. */
7212 AppendCharToRedobuff(Ctrl_R);
7213 AppendCharToRedobuff(literally);
7214 AppendCharToRedobuff(regname);
7215
7216 do_put(regname, BACKWARD, 1L,
7217 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
7218 }
7219 else if (insert_reg(regname, literally) == FAIL)
7220 {
7221 vim_beep();
7222 need_redraw = TRUE; /* remove the '"' */
7223 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007224 else if (stop_insert_mode)
7225 /* When the '=' register was used and a function was invoked that
7226 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7227 * insert anything, need to remove the '"' */
7228 need_redraw = TRUE;
7229
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230#ifdef FEAT_EVAL
7231 }
7232 --no_u_sync;
7233#endif
7234#ifdef FEAT_CMDL_INFO
7235 clear_showcmd();
7236#endif
7237
7238 /* If the inserted register is empty, we need to remove the '"' */
7239 if (need_redraw || stuff_empty())
7240 edit_unputchar();
7241}
7242
7243/*
7244 * CTRL-G commands in Insert mode.
7245 */
7246 static void
7247ins_ctrl_g()
7248{
7249 int c;
7250
7251#ifdef FEAT_INS_EXPAND
7252 /* Right after CTRL-X the cursor will be after the ruler. */
7253 setcursor();
7254#endif
7255
7256 /*
7257 * Don't map the second key. This also prevents the mode message to be
7258 * deleted when ESC is hit.
7259 */
7260 ++no_mapping;
7261 c = safe_vgetc();
7262 --no_mapping;
7263 switch (c)
7264 {
7265 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7266 case K_UP:
7267 case Ctrl_K:
7268 case 'k': ins_up(TRUE);
7269 break;
7270
7271 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7272 case K_DOWN:
7273 case Ctrl_J:
7274 case 'j': ins_down(TRUE);
7275 break;
7276
7277 /* CTRL-G u: start new undoable edit */
7278 case 'u': u_sync();
7279 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007280
7281 /* Need to reset Insstart, esp. because a BS that joins
7282 * aline to the previous one must save for undo. */
7283 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 break;
7285
7286 /* Unknown CTRL-G command, reserved for future expansion. */
7287 default: vim_beep();
7288 }
7289}
7290
7291/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007292 * CTRL-^ in Insert mode.
7293 */
7294 static void
7295ins_ctrl_hat()
7296{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00007297 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007298 {
7299 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7300 if (State & LANGMAP)
7301 {
7302 curbuf->b_p_iminsert = B_IMODE_NONE;
7303 State &= ~LANGMAP;
7304 }
7305 else
7306 {
7307 curbuf->b_p_iminsert = B_IMODE_LMAP;
7308 State |= LANGMAP;
7309#ifdef USE_IM_CONTROL
7310 im_set_active(FALSE);
7311#endif
7312 }
7313 }
7314#ifdef USE_IM_CONTROL
7315 else
7316 {
7317 /* There are no ":lmap" mappings, toggle IM */
7318 if (im_get_status())
7319 {
7320 curbuf->b_p_iminsert = B_IMODE_NONE;
7321 im_set_active(FALSE);
7322 }
7323 else
7324 {
7325 curbuf->b_p_iminsert = B_IMODE_IM;
7326 State &= ~LANGMAP;
7327 im_set_active(TRUE);
7328 }
7329 }
7330#endif
7331 set_iminsert_global();
7332 showmode();
7333#ifdef FEAT_GUI
7334 /* may show different cursor shape or color */
7335 if (gui.in_use)
7336 gui_update_cursor(TRUE, FALSE);
7337#endif
7338#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7339 /* Show/unshow value of 'keymap' in status lines. */
7340 status_redraw_curbuf();
7341#endif
7342}
7343
7344/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 * Handle ESC in insert mode.
7346 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7347 * insert.
7348 */
7349 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00007350ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 long *count;
7352 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00007353 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354{
7355 int temp;
7356 static int disabled_redraw = FALSE;
7357
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007358#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007359 check_spell_redraw();
7360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361#if defined(FEAT_HANGULIN)
7362# if defined(ESC_CHG_TO_ENG_MODE)
7363 hangul_input_state_set(0);
7364# endif
7365 if (composing_hangul)
7366 {
7367 push_raw_key(composing_hangul_buffer, 2);
7368 composing_hangul = 0;
7369 }
7370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371
7372 temp = curwin->w_cursor.col;
7373 if (disabled_redraw)
7374 {
7375 --RedrawingDisabled;
7376 disabled_redraw = FALSE;
7377 }
7378 if (!arrow_used)
7379 {
7380 /*
7381 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00007382 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7383 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 */
7385 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00007386 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387
7388 /*
7389 * Repeating insert may take a long time. Check for
7390 * interrupt now and then.
7391 */
7392 if (*count > 0)
7393 {
7394 line_breakcheck();
7395 if (got_int)
7396 *count = 0;
7397 }
7398
7399 if (--*count > 0) /* repeat what was typed */
7400 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007401 /* Vi repeats the insert without replacing characters. */
7402 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
7403 State &= ~REPLACE_FLAG;
7404
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 (void)start_redo_ins();
7406 if (cmdchar == 'r' || cmdchar == 'v')
7407 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
7408 ++RedrawingDisabled;
7409 disabled_redraw = TRUE;
7410 return FALSE; /* repeat the insert */
7411 }
7412 stop_insert(&curwin->w_cursor, TRUE);
7413 undisplay_dollar();
7414 }
7415
7416 /* When an autoindent was removed, curswant stays after the
7417 * indent */
7418 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
7419 curwin->w_set_curswant = TRUE;
7420
7421 /* Remember the last Insert position in the '^ mark. */
7422 if (!cmdmod.keepjumps)
7423 curbuf->b_last_insert = curwin->w_cursor;
7424
7425 /*
7426 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00007427 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00007429 if (!nomove
7430 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431#ifdef FEAT_VIRTUALEDIT
7432 || curwin->w_cursor.coladd > 0
7433#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007434 )
7435 && (restart_edit == NUL
7436 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00007438 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007440 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441#ifdef FEAT_RIGHTLEFT
7442 && !revins_on
7443#endif
7444 )
7445 {
7446#ifdef FEAT_VIRTUALEDIT
7447 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
7448 {
7449 oneleft();
7450 if (restart_edit != NUL)
7451 ++curwin->w_cursor.coladd;
7452 }
7453 else
7454#endif
7455 {
7456 --curwin->w_cursor.col;
7457#ifdef FEAT_MBYTE
7458 /* Correct cursor for multi-byte character. */
7459 if (has_mbyte)
7460 mb_adjust_cursor();
7461#endif
7462 }
7463 }
7464
7465#ifdef USE_IM_CONTROL
7466 /* Disable IM to allow typing English directly for Normal mode commands.
7467 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
7468 * well). */
7469 if (!(State & LANGMAP))
7470 im_save_status(&curbuf->b_p_iminsert);
7471 im_set_active(FALSE);
7472#endif
7473
7474 State = NORMAL;
7475 /* need to position cursor again (e.g. when on a TAB ) */
7476 changed_cline_bef_curs();
7477
7478#ifdef FEAT_MOUSE
7479 setmouse();
7480#endif
7481#ifdef CURSOR_SHAPE
7482 ui_cursor_shape(); /* may show different cursor shape */
7483#endif
7484
7485 /*
7486 * When recording or for CTRL-O, need to display the new mode.
7487 * Otherwise remove the mode message.
7488 */
7489 if (Recording || restart_edit != NUL)
7490 showmode();
7491 else if (p_smd)
7492 MSG("");
7493
7494 return TRUE; /* exit Insert mode */
7495}
7496
7497#ifdef FEAT_RIGHTLEFT
7498/*
7499 * Toggle language: hkmap and revins_on.
7500 * Move to end of reverse inserted text.
7501 */
7502 static void
7503ins_ctrl_()
7504{
7505 if (revins_on && revins_chars && revins_scol >= 0)
7506 {
7507 while (gchar_cursor() != NUL && revins_chars--)
7508 ++curwin->w_cursor.col;
7509 }
7510 p_ri = !p_ri;
7511 revins_on = (State == INSERT && p_ri);
7512 if (revins_on)
7513 {
7514 revins_scol = curwin->w_cursor.col;
7515 revins_legal++;
7516 revins_chars = 0;
7517 undisplay_dollar();
7518 }
7519 else
7520 revins_scol = -1;
7521#ifdef FEAT_FKMAP
7522 if (p_altkeymap)
7523 {
7524 /*
7525 * to be consistent also for redo command, using '.'
7526 * set arrow_used to true and stop it - causing to redo
7527 * characters entered in one mode (normal/reverse insert).
7528 */
7529 arrow_used = TRUE;
7530 (void)stop_arrow();
7531 p_fkmap = curwin->w_p_rl ^ p_ri;
7532 if (p_fkmap && p_ri)
7533 State = INSERT;
7534 }
7535 else
7536#endif
7537 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
7538 showmode();
7539}
7540#endif
7541
7542#ifdef FEAT_VISUAL
7543/*
7544 * If 'keymodel' contains "startsel", may start selection.
7545 * Returns TRUE when a CTRL-O and other keys stuffed.
7546 */
7547 static int
7548ins_start_select(c)
7549 int c;
7550{
7551 if (km_startsel)
7552 switch (c)
7553 {
7554 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 case K_PAGEUP:
7557 case K_KPAGEUP:
7558 case K_PAGEDOWN:
7559 case K_KPAGEDOWN:
7560# ifdef MACOS
7561 case K_LEFT:
7562 case K_RIGHT:
7563 case K_UP:
7564 case K_DOWN:
7565 case K_END:
7566 case K_HOME:
7567# endif
7568 if (!(mod_mask & MOD_MASK_SHIFT))
7569 break;
7570 /* FALLTHROUGH */
7571 case K_S_LEFT:
7572 case K_S_RIGHT:
7573 case K_S_UP:
7574 case K_S_DOWN:
7575 case K_S_END:
7576 case K_S_HOME:
7577 /* Start selection right away, the cursor can move with
7578 * CTRL-O when beyond the end of the line. */
7579 start_selection();
7580
7581 /* Execute the key in (insert) Select mode. */
7582 stuffcharReadbuff(Ctrl_O);
7583 if (mod_mask)
7584 {
7585 char_u buf[4];
7586
7587 buf[0] = K_SPECIAL;
7588 buf[1] = KS_MODIFIER;
7589 buf[2] = mod_mask;
7590 buf[3] = NUL;
7591 stuffReadbuff(buf);
7592 }
7593 stuffcharReadbuff(c);
7594 return TRUE;
7595 }
7596 return FALSE;
7597}
7598#endif
7599
7600/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007601 * <Insert> key in Insert mode: toggle insert/remplace mode.
7602 */
7603 static void
7604ins_insert(replaceState)
7605 int replaceState;
7606{
7607#ifdef FEAT_FKMAP
7608 if (p_fkmap && p_ri)
7609 {
7610 beep_flush();
7611 EMSG(farsi_text_3); /* encoded in Farsi */
7612 return;
7613 }
7614#endif
7615
7616#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00007617# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007618 set_vim_var_string(VV_INSERTMODE,
7619 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007620# ifdef FEAT_VREPLACE
7621 replaceState == VREPLACE ? "v" :
7622# endif
7623 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00007624# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007625 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
7626#endif
7627 if (State & REPLACE_FLAG)
7628 State = INSERT | (State & LANGMAP);
7629 else
7630 State = replaceState | (State & LANGMAP);
7631 AppendCharToRedobuff(K_INS);
7632 showmode();
7633#ifdef CURSOR_SHAPE
7634 ui_cursor_shape(); /* may show different cursor shape */
7635#endif
7636}
7637
7638/*
7639 * Pressed CTRL-O in Insert mode.
7640 */
7641 static void
7642ins_ctrl_o()
7643{
7644#ifdef FEAT_VREPLACE
7645 if (State & VREPLACE_FLAG)
7646 restart_edit = 'V';
7647 else
7648#endif
7649 if (State & REPLACE_FLAG)
7650 restart_edit = 'R';
7651 else
7652 restart_edit = 'I';
7653#ifdef FEAT_VIRTUALEDIT
7654 if (virtual_active())
7655 ins_at_eol = FALSE; /* cursor always keeps its column */
7656 else
7657#endif
7658 ins_at_eol = (gchar_cursor() == NUL);
7659}
7660
7661/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 * If the cursor is on an indent, ^T/^D insert/delete one
7663 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
7664 * Always round the indent to 'shiftwith', this is compatible
7665 * with vi. But vi only supports ^T and ^D after an
7666 * autoindent, we support it everywhere.
7667 */
7668 static void
7669ins_shift(c, lastc)
7670 int c;
7671 int lastc;
7672{
7673 if (stop_arrow() == FAIL)
7674 return;
7675 AppendCharToRedobuff(c);
7676
7677 /*
7678 * 0^D and ^^D: remove all indent.
7679 */
7680 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
7681 {
7682 --curwin->w_cursor.col;
7683 (void)del_char(FALSE); /* delete the '^' or '0' */
7684 /* In Replace mode, restore the characters that '^' or '0' replaced. */
7685 if (State & REPLACE_FLAG)
7686 replace_pop_ins();
7687 if (lastc == '^')
7688 old_indent = get_indent(); /* remember curr. indent */
7689 change_indent(INDENT_SET, 0, TRUE, 0);
7690 }
7691 else
7692 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
7693
7694 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
7695 did_ai = FALSE;
7696#ifdef FEAT_SMARTINDENT
7697 did_si = FALSE;
7698 can_si = FALSE;
7699 can_si_back = FALSE;
7700#endif
7701#ifdef FEAT_CINDENT
7702 can_cindent = FALSE; /* no cindenting after ^D or ^T */
7703#endif
7704}
7705
7706 static void
7707ins_del()
7708{
7709 int temp;
7710
7711 if (stop_arrow() == FAIL)
7712 return;
7713 if (gchar_cursor() == NUL) /* delete newline */
7714 {
7715 temp = curwin->w_cursor.col;
7716 if (!can_bs(BS_EOL) /* only if "eol" included */
7717 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
7718 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
7719 || do_join(FALSE) == FAIL)
7720 vim_beep();
7721 else
7722 curwin->w_cursor.col = temp;
7723 }
7724 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
7725 vim_beep();
7726 did_ai = FALSE;
7727#ifdef FEAT_SMARTINDENT
7728 did_si = FALSE;
7729 can_si = FALSE;
7730 can_si_back = FALSE;
7731#endif
7732 AppendCharToRedobuff(K_DEL);
7733}
7734
7735/*
7736 * Handle Backspace, delete-word and delete-line in Insert mode.
7737 * Return TRUE when backspace was actually used.
7738 */
7739 static int
7740ins_bs(c, mode, inserted_space_p)
7741 int c;
7742 int mode;
7743 int *inserted_space_p;
7744{
7745 linenr_T lnum;
7746 int cc;
7747 int temp = 0; /* init for GCC */
7748 colnr_T mincol;
7749 int did_backspace = FALSE;
7750 int in_indent;
7751 int oldState;
7752#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007753 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754#endif
7755
7756 /*
7757 * can't delete anything in an empty file
7758 * can't backup past first character in buffer
7759 * can't backup past starting point unless 'backspace' > 1
7760 * can backup to a previous line if 'backspace' == 0
7761 */
7762 if ( bufempty()
7763 || (
7764#ifdef FEAT_RIGHTLEFT
7765 !revins_on &&
7766#endif
7767 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
7768 || (!can_bs(BS_START)
7769 && (arrow_used
7770 || (curwin->w_cursor.lnum == Insstart.lnum
7771 && curwin->w_cursor.col <= Insstart.col)))
7772 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
7773 && curwin->w_cursor.col <= ai_col)
7774 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
7775 {
7776 vim_beep();
7777 return FALSE;
7778 }
7779
7780 if (stop_arrow() == FAIL)
7781 return FALSE;
7782 in_indent = inindent(0);
7783#ifdef FEAT_CINDENT
7784 if (in_indent)
7785 can_cindent = FALSE;
7786#endif
7787#ifdef FEAT_COMMENTS
7788 end_comment_pending = NUL; /* After BS, don't auto-end comment */
7789#endif
7790#ifdef FEAT_RIGHTLEFT
7791 if (revins_on) /* put cursor after last inserted char */
7792 inc_cursor();
7793#endif
7794
7795#ifdef FEAT_VIRTUALEDIT
7796 /* Virtualedit:
7797 * BACKSPACE_CHAR eats a virtual space
7798 * BACKSPACE_WORD eats all coladd
7799 * BACKSPACE_LINE eats all coladd and keeps going
7800 */
7801 if (curwin->w_cursor.coladd > 0)
7802 {
7803 if (mode == BACKSPACE_CHAR)
7804 {
7805 --curwin->w_cursor.coladd;
7806 return TRUE;
7807 }
7808 if (mode == BACKSPACE_WORD)
7809 {
7810 curwin->w_cursor.coladd = 0;
7811 return TRUE;
7812 }
7813 curwin->w_cursor.coladd = 0;
7814 }
7815#endif
7816
7817 /*
7818 * delete newline!
7819 */
7820 if (curwin->w_cursor.col == 0)
7821 {
7822 lnum = Insstart.lnum;
7823 if (curwin->w_cursor.lnum == Insstart.lnum
7824#ifdef FEAT_RIGHTLEFT
7825 || revins_on
7826#endif
7827 )
7828 {
7829 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
7830 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
7831 return FALSE;
7832 --Insstart.lnum;
7833 Insstart.col = MAXCOL;
7834 }
7835 /*
7836 * In replace mode:
7837 * cc < 0: NL was inserted, delete it
7838 * cc >= 0: NL was replaced, put original characters back
7839 */
7840 cc = -1;
7841 if (State & REPLACE_FLAG)
7842 cc = replace_pop(); /* returns -1 if NL was inserted */
7843 /*
7844 * In replace mode, in the line we started replacing, we only move the
7845 * cursor.
7846 */
7847 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
7848 {
7849 dec_cursor();
7850 }
7851 else
7852 {
7853#ifdef FEAT_VREPLACE
7854 if (!(State & VREPLACE_FLAG)
7855 || curwin->w_cursor.lnum > orig_line_count)
7856#endif
7857 {
7858 temp = gchar_cursor(); /* remember current char */
7859 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00007860
7861 /* When "aw" is in 'formatoptions' we must delete the space at
7862 * the end of the line, otherwise the line will be broken
7863 * again when auto-formatting. */
7864 if (has_format_option(FO_AUTO)
7865 && has_format_option(FO_WHITE_PAR))
7866 {
7867 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
7868 TRUE);
7869 int len;
7870
7871 len = STRLEN(ptr);
7872 if (len > 0 && ptr[len - 1] == ' ')
7873 ptr[len - 1] = NUL;
7874 }
7875
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 (void)do_join(FALSE);
7877 if (temp == NUL && gchar_cursor() != NUL)
7878 inc_cursor();
7879 }
7880#ifdef FEAT_VREPLACE
7881 else
7882 dec_cursor();
7883#endif
7884
7885 /*
7886 * In REPLACE mode we have to put back the text that was replaced
7887 * by the NL. On the replace stack is first a NUL-terminated
7888 * sequence of characters that were deleted and then the
7889 * characters that NL replaced.
7890 */
7891 if (State & REPLACE_FLAG)
7892 {
7893 /*
7894 * Do the next ins_char() in NORMAL state, to
7895 * prevent ins_char() from replacing characters and
7896 * avoiding showmatch().
7897 */
7898 oldState = State;
7899 State = NORMAL;
7900 /*
7901 * restore characters (blanks) deleted after cursor
7902 */
7903 while (cc > 0)
7904 {
7905 temp = curwin->w_cursor.col;
7906#ifdef FEAT_MBYTE
7907 mb_replace_pop_ins(cc);
7908#else
7909 ins_char(cc);
7910#endif
7911 curwin->w_cursor.col = temp;
7912 cc = replace_pop();
7913 }
7914 /* restore the characters that NL replaced */
7915 replace_pop_ins();
7916 State = oldState;
7917 }
7918 }
7919 did_ai = FALSE;
7920 }
7921 else
7922 {
7923 /*
7924 * Delete character(s) before the cursor.
7925 */
7926#ifdef FEAT_RIGHTLEFT
7927 if (revins_on) /* put cursor on last inserted char */
7928 dec_cursor();
7929#endif
7930 mincol = 0;
7931 /* keep indent */
7932 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
7933#ifdef FEAT_RIGHTLEFT
7934 && !revins_on
7935#endif
7936 )
7937 {
7938 temp = curwin->w_cursor.col;
7939 beginline(BL_WHITE);
7940 if (curwin->w_cursor.col < (colnr_T)temp)
7941 mincol = curwin->w_cursor.col;
7942 curwin->w_cursor.col = temp;
7943 }
7944
7945 /*
7946 * Handle deleting one 'shiftwidth' or 'softtabstop'.
7947 */
7948 if ( mode == BACKSPACE_CHAR
7949 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00007950 || (curbuf->b_p_sts != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 && (*(ml_get_cursor() - 1) == TAB
7952 || (*(ml_get_cursor() - 1) == ' '
7953 && (!*inserted_space_p
7954 || arrow_used))))))
7955 {
7956 int ts;
7957 colnr_T vcol;
7958 colnr_T want_vcol;
7959 int extra = 0;
7960
7961 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00007962 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 ts = curbuf->b_p_sw;
7964 else
7965 ts = curbuf->b_p_sts;
7966 /* Compute the virtual column where we want to be. Since
7967 * 'showbreak' may get in the way, need to get the last column of
7968 * the previous character. */
7969 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7970 dec_cursor();
7971 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
7972 inc_cursor();
7973 want_vcol = (want_vcol / ts) * ts;
7974
7975 /* delete characters until we are at or before want_vcol */
7976 while (vcol > want_vcol
7977 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
7978 {
7979 dec_cursor();
7980 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7981 if (State & REPLACE_FLAG)
7982 {
7983 /* Don't delete characters before the insert point when in
7984 * Replace mode */
7985 if (curwin->w_cursor.lnum != Insstart.lnum
7986 || curwin->w_cursor.col >= Insstart.col)
7987 {
7988#if 0 /* what was this for? It causes problems when sw != ts. */
7989 if (State == REPLACE && (int)vcol < want_vcol)
7990 {
7991 (void)del_char(FALSE);
7992 extra = 2; /* don't pop too much */
7993 }
7994 else
7995#endif
7996 replace_do_bs();
7997 }
7998 }
7999 else
8000 (void)del_char(FALSE);
8001 }
8002
8003 /* insert extra spaces until we are at want_vcol */
8004 while (vcol < want_vcol)
8005 {
8006 /* Remember the first char we inserted */
8007 if (curwin->w_cursor.lnum == Insstart.lnum
8008 && curwin->w_cursor.col < Insstart.col)
8009 Insstart.col = curwin->w_cursor.col;
8010
8011#ifdef FEAT_VREPLACE
8012 if (State & VREPLACE_FLAG)
8013 ins_char(' ');
8014 else
8015#endif
8016 {
8017 ins_str((char_u *)" ");
8018 if ((State & REPLACE_FLAG) && extra <= 1)
8019 {
8020 if (extra)
8021 replace_push_off(NUL);
8022 else
8023 replace_push(NUL);
8024 }
8025 if (extra == 2)
8026 extra = 1;
8027 }
8028 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8029 }
8030 }
8031
8032 /*
8033 * Delete upto starting point, start of line or previous word.
8034 */
8035 else do
8036 {
8037#ifdef FEAT_RIGHTLEFT
8038 if (!revins_on) /* put cursor on char to be deleted */
8039#endif
8040 dec_cursor();
8041
8042 /* start of word? */
8043 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
8044 {
8045 mode = BACKSPACE_WORD_NOT_SPACE;
8046 temp = vim_iswordc(gchar_cursor());
8047 }
8048 /* end of word? */
8049 else if (mode == BACKSPACE_WORD_NOT_SPACE
8050 && (vim_isspace(cc = gchar_cursor())
8051 || vim_iswordc(cc) != temp))
8052 {
8053#ifdef FEAT_RIGHTLEFT
8054 if (!revins_on)
8055#endif
8056 inc_cursor();
8057#ifdef FEAT_RIGHTLEFT
8058 else if (State & REPLACE_FLAG)
8059 dec_cursor();
8060#endif
8061 break;
8062 }
8063 if (State & REPLACE_FLAG)
8064 replace_do_bs();
8065 else
8066 {
8067#ifdef FEAT_MBYTE
8068 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008069 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070#endif
8071 (void)del_char(FALSE);
8072#ifdef FEAT_MBYTE
8073 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008074 * If there are combining characters and 'delcombine' is set
8075 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 * character.
8077 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008078 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 inc_cursor();
8080#endif
8081#ifdef FEAT_RIGHTLEFT
8082 if (revins_chars)
8083 {
8084 revins_chars--;
8085 revins_legal++;
8086 }
8087 if (revins_on && gchar_cursor() == NUL)
8088 break;
8089#endif
8090 }
8091 /* Just a single backspace?: */
8092 if (mode == BACKSPACE_CHAR)
8093 break;
8094 } while (
8095#ifdef FEAT_RIGHTLEFT
8096 revins_on ||
8097#endif
8098 (curwin->w_cursor.col > mincol
8099 && (curwin->w_cursor.lnum != Insstart.lnum
8100 || curwin->w_cursor.col != Insstart.col)));
8101 did_backspace = TRUE;
8102 }
8103#ifdef FEAT_SMARTINDENT
8104 did_si = FALSE;
8105 can_si = FALSE;
8106 can_si_back = FALSE;
8107#endif
8108 if (curwin->w_cursor.col <= 1)
8109 did_ai = FALSE;
8110 /*
8111 * It's a little strange to put backspaces into the redo
8112 * buffer, but it makes auto-indent a lot easier to deal
8113 * with.
8114 */
8115 AppendCharToRedobuff(c);
8116
8117 /* If deleted before the insertion point, adjust it */
8118 if (curwin->w_cursor.lnum == Insstart.lnum
8119 && curwin->w_cursor.col < Insstart.col)
8120 Insstart.col = curwin->w_cursor.col;
8121
8122 /* vi behaviour: the cursor moves backward but the character that
8123 * was there remains visible
8124 * Vim behaviour: the cursor moves backward and the character that
8125 * was there is erased from the screen.
8126 * We can emulate the vi behaviour by pretending there is a dollar
8127 * displayed even when there isn't.
8128 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8129 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
8130 dollar_vcol = curwin->w_virtcol;
8131
8132 return did_backspace;
8133}
8134
8135#ifdef FEAT_MOUSE
8136 static void
8137ins_mouse(c)
8138 int c;
8139{
8140 pos_T tpos;
8141
8142# ifdef FEAT_GUI
8143 /* When GUI is active, also move/paste when 'mouse' is empty */
8144 if (!gui.in_use)
8145# endif
8146 if (!mouse_has(MOUSE_INSERT))
8147 return;
8148
8149 undisplay_dollar();
8150 tpos = curwin->w_cursor;
8151 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
8152 {
8153 start_arrow(&tpos);
8154# ifdef FEAT_CINDENT
8155 can_cindent = TRUE;
8156# endif
8157 }
8158
8159#ifdef FEAT_WINDOWS
8160 /* redraw status lines (in case another window became active) */
8161 redraw_statuslines();
8162#endif
8163}
8164
8165 static void
8166ins_mousescroll(up)
8167 int up;
8168{
8169 pos_T tpos;
8170# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8171 win_T *old_curwin;
8172# endif
8173
8174 tpos = curwin->w_cursor;
8175
8176# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8177 old_curwin = curwin;
8178
8179 /* Currently the mouse coordinates are only known in the GUI. */
8180 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
8181 {
8182 int row, col;
8183
8184 row = mouse_row;
8185 col = mouse_col;
8186
8187 /* find the window at the pointer coordinates */
8188 curwin = mouse_find_win(&row, &col);
8189 curbuf = curwin->w_buffer;
8190 }
8191 if (curwin == old_curwin)
8192# endif
8193 undisplay_dollar();
8194
8195 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8196 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
8197 else
8198 scroll_redraw(up, 3L);
8199
8200# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8201 curwin->w_redr_status = TRUE;
8202
8203 curwin = old_curwin;
8204 curbuf = curwin->w_buffer;
8205# endif
8206
8207 if (!equalpos(curwin->w_cursor, tpos))
8208 {
8209 start_arrow(&tpos);
8210# ifdef FEAT_CINDENT
8211 can_cindent = TRUE;
8212# endif
8213 }
8214}
8215#endif
8216
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008217#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00008218 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008219ins_tabline(c)
8220 int c;
8221{
8222 /* We will be leaving the current window, unless closing another tab. */
8223 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
8224 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
8225 {
8226 undisplay_dollar();
8227 start_arrow(&curwin->w_cursor);
8228# ifdef FEAT_CINDENT
8229 can_cindent = TRUE;
8230# endif
8231 }
8232
8233 if (c == K_TABLINE)
8234 goto_tabpage(current_tab);
8235 else
8236 handle_tabmenu();
8237
8238}
8239#endif
8240
8241#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 void
8243ins_scroll()
8244{
8245 pos_T tpos;
8246
8247 undisplay_dollar();
8248 tpos = curwin->w_cursor;
8249 if (gui_do_scroll())
8250 {
8251 start_arrow(&tpos);
8252# ifdef FEAT_CINDENT
8253 can_cindent = TRUE;
8254# endif
8255 }
8256}
8257
8258 void
8259ins_horscroll()
8260{
8261 pos_T tpos;
8262
8263 undisplay_dollar();
8264 tpos = curwin->w_cursor;
8265 if (gui_do_horiz_scroll())
8266 {
8267 start_arrow(&tpos);
8268# ifdef FEAT_CINDENT
8269 can_cindent = TRUE;
8270# endif
8271 }
8272}
8273#endif
8274
8275 static void
8276ins_left()
8277{
8278 pos_T tpos;
8279
8280#ifdef FEAT_FOLDING
8281 if ((fdo_flags & FDO_HOR) && KeyTyped)
8282 foldOpenCursor();
8283#endif
8284 undisplay_dollar();
8285 tpos = curwin->w_cursor;
8286 if (oneleft() == OK)
8287 {
8288 start_arrow(&tpos);
8289#ifdef FEAT_RIGHTLEFT
8290 /* If exit reversed string, position is fixed */
8291 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
8292 revins_legal++;
8293 revins_chars++;
8294#endif
8295 }
8296
8297 /*
8298 * if 'whichwrap' set for cursor in insert mode may go to
8299 * previous line
8300 */
8301 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
8302 {
8303 start_arrow(&tpos);
8304 --(curwin->w_cursor.lnum);
8305 coladvance((colnr_T)MAXCOL);
8306 curwin->w_set_curswant = TRUE; /* so we stay at the end */
8307 }
8308 else
8309 vim_beep();
8310}
8311
8312 static void
8313ins_home(c)
8314 int c;
8315{
8316 pos_T tpos;
8317
8318#ifdef FEAT_FOLDING
8319 if ((fdo_flags & FDO_HOR) && KeyTyped)
8320 foldOpenCursor();
8321#endif
8322 undisplay_dollar();
8323 tpos = curwin->w_cursor;
8324 if (c == K_C_HOME)
8325 curwin->w_cursor.lnum = 1;
8326 curwin->w_cursor.col = 0;
8327#ifdef FEAT_VIRTUALEDIT
8328 curwin->w_cursor.coladd = 0;
8329#endif
8330 curwin->w_curswant = 0;
8331 start_arrow(&tpos);
8332}
8333
8334 static void
8335ins_end(c)
8336 int c;
8337{
8338 pos_T tpos;
8339
8340#ifdef FEAT_FOLDING
8341 if ((fdo_flags & FDO_HOR) && KeyTyped)
8342 foldOpenCursor();
8343#endif
8344 undisplay_dollar();
8345 tpos = curwin->w_cursor;
8346 if (c == K_C_END)
8347 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
8348 coladvance((colnr_T)MAXCOL);
8349 curwin->w_curswant = MAXCOL;
8350
8351 start_arrow(&tpos);
8352}
8353
8354 static void
8355ins_s_left()
8356{
8357#ifdef FEAT_FOLDING
8358 if ((fdo_flags & FDO_HOR) && KeyTyped)
8359 foldOpenCursor();
8360#endif
8361 undisplay_dollar();
8362 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
8363 {
8364 start_arrow(&curwin->w_cursor);
8365 (void)bck_word(1L, FALSE, FALSE);
8366 curwin->w_set_curswant = TRUE;
8367 }
8368 else
8369 vim_beep();
8370}
8371
8372 static void
8373ins_right()
8374{
8375#ifdef FEAT_FOLDING
8376 if ((fdo_flags & FDO_HOR) && KeyTyped)
8377 foldOpenCursor();
8378#endif
8379 undisplay_dollar();
8380 if (gchar_cursor() != NUL || virtual_active()
8381 )
8382 {
8383 start_arrow(&curwin->w_cursor);
8384 curwin->w_set_curswant = TRUE;
8385#ifdef FEAT_VIRTUALEDIT
8386 if (virtual_active())
8387 oneright();
8388 else
8389#endif
8390 {
8391#ifdef FEAT_MBYTE
8392 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008393 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 else
8395#endif
8396 ++curwin->w_cursor.col;
8397 }
8398
8399#ifdef FEAT_RIGHTLEFT
8400 revins_legal++;
8401 if (revins_chars)
8402 revins_chars--;
8403#endif
8404 }
8405 /* if 'whichwrap' set for cursor in insert mode, may move the
8406 * cursor to the next line */
8407 else if (vim_strchr(p_ww, ']') != NULL
8408 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
8409 {
8410 start_arrow(&curwin->w_cursor);
8411 curwin->w_set_curswant = TRUE;
8412 ++curwin->w_cursor.lnum;
8413 curwin->w_cursor.col = 0;
8414 }
8415 else
8416 vim_beep();
8417}
8418
8419 static void
8420ins_s_right()
8421{
8422#ifdef FEAT_FOLDING
8423 if ((fdo_flags & FDO_HOR) && KeyTyped)
8424 foldOpenCursor();
8425#endif
8426 undisplay_dollar();
8427 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
8428 || gchar_cursor() != NUL)
8429 {
8430 start_arrow(&curwin->w_cursor);
8431 (void)fwd_word(1L, FALSE, 0);
8432 curwin->w_set_curswant = TRUE;
8433 }
8434 else
8435 vim_beep();
8436}
8437
8438 static void
8439ins_up(startcol)
8440 int startcol; /* when TRUE move to Insstart.col */
8441{
8442 pos_T tpos;
8443 linenr_T old_topline = curwin->w_topline;
8444#ifdef FEAT_DIFF
8445 int old_topfill = curwin->w_topfill;
8446#endif
8447
8448 undisplay_dollar();
8449 tpos = curwin->w_cursor;
8450 if (cursor_up(1L, TRUE) == OK)
8451 {
8452 if (startcol)
8453 coladvance(getvcol_nolist(&Insstart));
8454 if (old_topline != curwin->w_topline
8455#ifdef FEAT_DIFF
8456 || old_topfill != curwin->w_topfill
8457#endif
8458 )
8459 redraw_later(VALID);
8460 start_arrow(&tpos);
8461#ifdef FEAT_CINDENT
8462 can_cindent = TRUE;
8463#endif
8464 }
8465 else
8466 vim_beep();
8467}
8468
8469 static void
8470ins_pageup()
8471{
8472 pos_T tpos;
8473
8474 undisplay_dollar();
8475 tpos = curwin->w_cursor;
8476 if (onepage(BACKWARD, 1L) == OK)
8477 {
8478 start_arrow(&tpos);
8479#ifdef FEAT_CINDENT
8480 can_cindent = TRUE;
8481#endif
8482 }
8483 else
8484 vim_beep();
8485}
8486
8487 static void
8488ins_down(startcol)
8489 int startcol; /* when TRUE move to Insstart.col */
8490{
8491 pos_T tpos;
8492 linenr_T old_topline = curwin->w_topline;
8493#ifdef FEAT_DIFF
8494 int old_topfill = curwin->w_topfill;
8495#endif
8496
8497 undisplay_dollar();
8498 tpos = curwin->w_cursor;
8499 if (cursor_down(1L, TRUE) == OK)
8500 {
8501 if (startcol)
8502 coladvance(getvcol_nolist(&Insstart));
8503 if (old_topline != curwin->w_topline
8504#ifdef FEAT_DIFF
8505 || old_topfill != curwin->w_topfill
8506#endif
8507 )
8508 redraw_later(VALID);
8509 start_arrow(&tpos);
8510#ifdef FEAT_CINDENT
8511 can_cindent = TRUE;
8512#endif
8513 }
8514 else
8515 vim_beep();
8516}
8517
8518 static void
8519ins_pagedown()
8520{
8521 pos_T tpos;
8522
8523 undisplay_dollar();
8524 tpos = curwin->w_cursor;
8525 if (onepage(FORWARD, 1L) == OK)
8526 {
8527 start_arrow(&tpos);
8528#ifdef FEAT_CINDENT
8529 can_cindent = TRUE;
8530#endif
8531 }
8532 else
8533 vim_beep();
8534}
8535
8536#ifdef FEAT_DND
8537 static void
8538ins_drop()
8539{
8540 do_put('~', BACKWARD, 1L, PUT_CURSEND);
8541}
8542#endif
8543
8544/*
8545 * Handle TAB in Insert or Replace mode.
8546 * Return TRUE when the TAB needs to be inserted like a normal character.
8547 */
8548 static int
8549ins_tab()
8550{
8551 int ind;
8552 int i;
8553 int temp;
8554
8555 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
8556 Insstart_blank_vcol = get_nolist_virtcol();
8557 if (echeck_abbr(TAB + ABBR_OFF))
8558 return FALSE;
8559
8560 ind = inindent(0);
8561#ifdef FEAT_CINDENT
8562 if (ind)
8563 can_cindent = FALSE;
8564#endif
8565
8566 /*
8567 * When nothing special, insert TAB like a normal character
8568 */
8569 if (!curbuf->b_p_et
8570 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
8571 && curbuf->b_p_sts == 0)
8572 return TRUE;
8573
8574 if (stop_arrow() == FAIL)
8575 return TRUE;
8576
8577 did_ai = FALSE;
8578#ifdef FEAT_SMARTINDENT
8579 did_si = FALSE;
8580 can_si = FALSE;
8581 can_si_back = FALSE;
8582#endif
8583 AppendToRedobuff((char_u *)"\t");
8584
8585 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
8586 temp = (int)curbuf->b_p_sw;
8587 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
8588 temp = (int)curbuf->b_p_sts;
8589 else /* otherwise use 'tabstop' */
8590 temp = (int)curbuf->b_p_ts;
8591 temp -= get_nolist_virtcol() % temp;
8592
8593 /*
8594 * Insert the first space with ins_char(). It will delete one char in
8595 * replace mode. Insert the rest with ins_str(); it will not delete any
8596 * chars. For VREPLACE mode, we use ins_char() for all characters.
8597 */
8598 ins_char(' ');
8599 while (--temp > 0)
8600 {
8601#ifdef FEAT_VREPLACE
8602 if (State & VREPLACE_FLAG)
8603 ins_char(' ');
8604 else
8605#endif
8606 {
8607 ins_str((char_u *)" ");
8608 if (State & REPLACE_FLAG) /* no char replaced */
8609 replace_push(NUL);
8610 }
8611 }
8612
8613 /*
8614 * When 'expandtab' not set: Replace spaces by TABs where possible.
8615 */
8616 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
8617 {
8618 char_u *ptr;
8619#ifdef FEAT_VREPLACE
8620 char_u *saved_line = NULL; /* init for GCC */
8621 pos_T pos;
8622#endif
8623 pos_T fpos;
8624 pos_T *cursor;
8625 colnr_T want_vcol, vcol;
8626 int change_col = -1;
8627 int save_list = curwin->w_p_list;
8628
8629 /*
8630 * Get the current line. For VREPLACE mode, don't make real changes
8631 * yet, just work on a copy of the line.
8632 */
8633#ifdef FEAT_VREPLACE
8634 if (State & VREPLACE_FLAG)
8635 {
8636 pos = curwin->w_cursor;
8637 cursor = &pos;
8638 saved_line = vim_strsave(ml_get_curline());
8639 if (saved_line == NULL)
8640 return FALSE;
8641 ptr = saved_line + pos.col;
8642 }
8643 else
8644#endif
8645 {
8646 ptr = ml_get_cursor();
8647 cursor = &curwin->w_cursor;
8648 }
8649
8650 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
8651 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8652 curwin->w_p_list = FALSE;
8653
8654 /* Find first white before the cursor */
8655 fpos = curwin->w_cursor;
8656 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
8657 {
8658 --fpos.col;
8659 --ptr;
8660 }
8661
8662 /* In Replace mode, don't change characters before the insert point. */
8663 if ((State & REPLACE_FLAG)
8664 && fpos.lnum == Insstart.lnum
8665 && fpos.col < Insstart.col)
8666 {
8667 ptr += Insstart.col - fpos.col;
8668 fpos.col = Insstart.col;
8669 }
8670
8671 /* compute virtual column numbers of first white and cursor */
8672 getvcol(curwin, &fpos, &vcol, NULL, NULL);
8673 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
8674
8675 /* Use as many TABs as possible. Beware of 'showbreak' and
8676 * 'linebreak' adding extra virtual columns. */
8677 while (vim_iswhite(*ptr))
8678 {
8679 i = lbr_chartabsize((char_u *)"\t", vcol);
8680 if (vcol + i > want_vcol)
8681 break;
8682 if (*ptr != TAB)
8683 {
8684 *ptr = TAB;
8685 if (change_col < 0)
8686 {
8687 change_col = fpos.col; /* Column of first change */
8688 /* May have to adjust Insstart */
8689 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
8690 Insstart.col = fpos.col;
8691 }
8692 }
8693 ++fpos.col;
8694 ++ptr;
8695 vcol += i;
8696 }
8697
8698 if (change_col >= 0)
8699 {
8700 int repl_off = 0;
8701
8702 /* Skip over the spaces we need. */
8703 while (vcol < want_vcol && *ptr == ' ')
8704 {
8705 vcol += lbr_chartabsize(ptr, vcol);
8706 ++ptr;
8707 ++repl_off;
8708 }
8709 if (vcol > want_vcol)
8710 {
8711 /* Must have a char with 'showbreak' just before it. */
8712 --ptr;
8713 --repl_off;
8714 }
8715 fpos.col += repl_off;
8716
8717 /* Delete following spaces. */
8718 i = cursor->col - fpos.col;
8719 if (i > 0)
8720 {
8721 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
8722 /* correct replace stack. */
8723 if ((State & REPLACE_FLAG)
8724#ifdef FEAT_VREPLACE
8725 && !(State & VREPLACE_FLAG)
8726#endif
8727 )
8728 for (temp = i; --temp >= 0; )
8729 replace_join(repl_off);
8730 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00008731#ifdef FEAT_NETBEANS_INTG
8732 if (usingNetbeans)
8733 {
8734 netbeans_removed(curbuf, fpos.lnum, cursor->col,
8735 (long)(i + 1));
8736 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
8737 (char_u *)"\t", 1);
8738 }
8739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 cursor->col -= i;
8741
8742#ifdef FEAT_VREPLACE
8743 /*
8744 * In VREPLACE mode, we haven't changed anything yet. Do it now by
8745 * backspacing over the changed spacing and then inserting the new
8746 * spacing.
8747 */
8748 if (State & VREPLACE_FLAG)
8749 {
8750 /* Backspace from real cursor to change_col */
8751 backspace_until_column(change_col);
8752
8753 /* Insert each char in saved_line from changed_col to
8754 * ptr-cursor */
8755 ins_bytes_len(saved_line + change_col,
8756 cursor->col - change_col);
8757 }
8758#endif
8759 }
8760
8761#ifdef FEAT_VREPLACE
8762 if (State & VREPLACE_FLAG)
8763 vim_free(saved_line);
8764#endif
8765 curwin->w_p_list = save_list;
8766 }
8767
8768 return FALSE;
8769}
8770
8771/*
8772 * Handle CR or NL in insert mode.
8773 * Return TRUE when out of memory or can't undo.
8774 */
8775 static int
8776ins_eol(c)
8777 int c;
8778{
8779 int i;
8780
8781 if (echeck_abbr(c + ABBR_OFF))
8782 return FALSE;
8783 if (stop_arrow() == FAIL)
8784 return TRUE;
8785 undisplay_dollar();
8786
8787 /*
8788 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
8789 * character under the cursor. Only push a NUL on the replace stack,
8790 * nothing to put back when the NL is deleted.
8791 */
8792 if ((State & REPLACE_FLAG)
8793#ifdef FEAT_VREPLACE
8794 && !(State & VREPLACE_FLAG)
8795#endif
8796 )
8797 replace_push(NUL);
8798
8799 /*
8800 * In VREPLACE mode, a NL replaces the rest of the line, and starts
8801 * replacing the next line, so we push all of the characters left on the
8802 * line onto the replace stack. This is not done here though, it is done
8803 * in open_line().
8804 */
8805
8806#ifdef FEAT_RIGHTLEFT
8807# ifdef FEAT_FKMAP
8808 if (p_altkeymap && p_fkmap)
8809 fkmap(NL);
8810# endif
8811 /* NL in reverse insert will always start in the end of
8812 * current line. */
8813 if (revins_on)
8814 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8815#endif
8816
8817 AppendToRedobuff(NL_STR);
8818 i = open_line(FORWARD,
8819#ifdef FEAT_COMMENTS
8820 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
8821#endif
8822 0, old_indent);
8823 old_indent = 0;
8824#ifdef FEAT_CINDENT
8825 can_cindent = TRUE;
8826#endif
8827
8828 return (!i);
8829}
8830
8831#ifdef FEAT_DIGRAPHS
8832/*
8833 * Handle digraph in insert mode.
8834 * Returns character still to be inserted, or NUL when nothing remaining to be
8835 * done.
8836 */
8837 static int
8838ins_digraph()
8839{
8840 int c;
8841 int cc;
8842
8843 pc_status = PC_STATUS_UNSET;
8844 if (redrawing() && !char_avail())
8845 {
8846 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008847 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848
8849 edit_putchar('?', TRUE);
8850#ifdef FEAT_CMDL_INFO
8851 add_to_showcmd_c(Ctrl_K);
8852#endif
8853 }
8854
8855#ifdef USE_ON_FLY_SCROLL
8856 dont_scroll = TRUE; /* disallow scrolling here */
8857#endif
8858
8859 /* don't map the digraph chars. This also prevents the
8860 * mode message to be deleted when ESC is hit */
8861 ++no_mapping;
8862 ++allow_keys;
8863 c = safe_vgetc();
8864 --no_mapping;
8865 --allow_keys;
8866 if (IS_SPECIAL(c) || mod_mask) /* special key */
8867 {
8868#ifdef FEAT_CMDL_INFO
8869 clear_showcmd();
8870#endif
8871 insert_special(c, TRUE, FALSE);
8872 return NUL;
8873 }
8874 if (c != ESC)
8875 {
8876 if (redrawing() && !char_avail())
8877 {
8878 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008879 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880
8881 if (char2cells(c) == 1)
8882 {
8883 /* first remove the '?', otherwise it's restored when typing
8884 * an ESC next */
8885 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00008886 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 edit_putchar(c, TRUE);
8888 }
8889#ifdef FEAT_CMDL_INFO
8890 add_to_showcmd_c(c);
8891#endif
8892 }
8893 ++no_mapping;
8894 ++allow_keys;
8895 cc = safe_vgetc();
8896 --no_mapping;
8897 --allow_keys;
8898 if (cc != ESC)
8899 {
8900 AppendToRedobuff((char_u *)CTRL_V_STR);
8901 c = getdigraph(c, cc, TRUE);
8902#ifdef FEAT_CMDL_INFO
8903 clear_showcmd();
8904#endif
8905 return c;
8906 }
8907 }
8908 edit_unputchar();
8909#ifdef FEAT_CMDL_INFO
8910 clear_showcmd();
8911#endif
8912 return NUL;
8913}
8914#endif
8915
8916/*
8917 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
8918 * Returns the char to be inserted, or NUL if none found.
8919 */
8920 static int
8921ins_copychar(lnum)
8922 linenr_T lnum;
8923{
8924 int c;
8925 int temp;
8926 char_u *ptr, *prev_ptr;
8927
8928 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8929 {
8930 vim_beep();
8931 return NUL;
8932 }
8933
8934 /* try to advance to the cursor column */
8935 temp = 0;
8936 ptr = ml_get(lnum);
8937 prev_ptr = ptr;
8938 validate_virtcol();
8939 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
8940 {
8941 prev_ptr = ptr;
8942 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
8943 }
8944 if ((colnr_T)temp > curwin->w_virtcol)
8945 ptr = prev_ptr;
8946
8947#ifdef FEAT_MBYTE
8948 c = (*mb_ptr2char)(ptr);
8949#else
8950 c = *ptr;
8951#endif
8952 if (c == NUL)
8953 vim_beep();
8954 return c;
8955}
8956
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008957/*
8958 * CTRL-Y or CTRL-E typed in Insert mode.
8959 */
8960 static int
8961ins_ctrl_ey(tc)
8962 int tc;
8963{
8964 int c = tc;
8965
8966#ifdef FEAT_INS_EXPAND
8967 if (ctrl_x_mode == CTRL_X_SCROLL)
8968 {
8969 if (c == Ctrl_Y)
8970 scrolldown_clamp();
8971 else
8972 scrollup_clamp();
8973 redraw_later(VALID);
8974 }
8975 else
8976#endif
8977 {
8978 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
8979 if (c != NUL)
8980 {
8981 long tw_save;
8982
8983 /* The character must be taken literally, insert like it
8984 * was typed after a CTRL-V, and pretend 'textwidth'
8985 * wasn't set. Digits, 'o' and 'x' are special after a
8986 * CTRL-V, don't use it for these. */
8987 if (c < 256 && !isalnum(c))
8988 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
8989 tw_save = curbuf->b_p_tw;
8990 curbuf->b_p_tw = -1;
8991 insert_special(c, TRUE, FALSE);
8992 curbuf->b_p_tw = tw_save;
8993#ifdef FEAT_RIGHTLEFT
8994 revins_chars++;
8995 revins_legal++;
8996#endif
8997 c = Ctrl_V; /* pretend CTRL-V is last character */
8998 auto_format(FALSE, TRUE);
8999 }
9000 }
9001 return c;
9002}
9003
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004#ifdef FEAT_SMARTINDENT
9005/*
9006 * Try to do some very smart auto-indenting.
9007 * Used when inserting a "normal" character.
9008 */
9009 static void
9010ins_try_si(c)
9011 int c;
9012{
9013 pos_T *pos, old_pos;
9014 char_u *ptr;
9015 int i;
9016 int temp;
9017
9018 /*
9019 * do some very smart indenting when entering '{' or '}'
9020 */
9021 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
9022 {
9023 /*
9024 * for '}' set indent equal to indent of line containing matching '{'
9025 */
9026 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
9027 {
9028 old_pos = curwin->w_cursor;
9029 /*
9030 * If the matching '{' has a ')' immediately before it (ignoring
9031 * white-space), then line up with the start of the line
9032 * containing the matching '(' if there is one. This handles the
9033 * case where an "if (..\n..) {" statement continues over multiple
9034 * lines -- webb
9035 */
9036 ptr = ml_get(pos->lnum);
9037 i = pos->col;
9038 if (i > 0) /* skip blanks before '{' */
9039 while (--i > 0 && vim_iswhite(ptr[i]))
9040 ;
9041 curwin->w_cursor.lnum = pos->lnum;
9042 curwin->w_cursor.col = i;
9043 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
9044 curwin->w_cursor = *pos;
9045 i = get_indent();
9046 curwin->w_cursor = old_pos;
9047#ifdef FEAT_VREPLACE
9048 if (State & VREPLACE_FLAG)
9049 change_indent(INDENT_SET, i, FALSE, NUL);
9050 else
9051#endif
9052 (void)set_indent(i, SIN_CHANGED);
9053 }
9054 else if (curwin->w_cursor.col > 0)
9055 {
9056 /*
9057 * when inserting '{' after "O" reduce indent, but not
9058 * more than indent of previous line
9059 */
9060 temp = TRUE;
9061 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
9062 {
9063 old_pos = curwin->w_cursor;
9064 i = get_indent();
9065 while (curwin->w_cursor.lnum > 1)
9066 {
9067 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
9068
9069 /* ignore empty lines and lines starting with '#'. */
9070 if (*ptr != '#' && *ptr != NUL)
9071 break;
9072 }
9073 if (get_indent() >= i)
9074 temp = FALSE;
9075 curwin->w_cursor = old_pos;
9076 }
9077 if (temp)
9078 shift_line(TRUE, FALSE, 1);
9079 }
9080 }
9081
9082 /*
9083 * set indent of '#' always to 0
9084 */
9085 if (curwin->w_cursor.col > 0 && can_si && c == '#')
9086 {
9087 /* remember current indent for next line */
9088 old_indent = get_indent();
9089 (void)set_indent(0, SIN_CHANGED);
9090 }
9091
9092 /* Adjust ai_col, the char at this position can be deleted. */
9093 if (ai_col > curwin->w_cursor.col)
9094 ai_col = curwin->w_cursor.col;
9095}
9096#endif
9097
9098/*
9099 * Get the value that w_virtcol would have when 'list' is off.
9100 * Unless 'cpo' contains the 'L' flag.
9101 */
9102 static colnr_T
9103get_nolist_virtcol()
9104{
9105 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9106 return getvcol_nolist(&curwin->w_cursor);
9107 validate_virtcol();
9108 return curwin->w_virtcol;
9109}