blob: 5159a34fcead2dbca7acfece96207f6e8e26f4fb [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 Moolenaar488c6512005-08-11 20:09:58 +00004500 compl_length = (int)curs_col - compl_col;
4501 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4502 if (compl_pattern == NULL)
4503#endif
4504 return FAIL;
4505 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004506 else
4507 {
4508 EMSG2(_(e_intern2), "ins_complete()");
4509 return FAIL;
4510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004512 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 {
4514 edit_submode_pre = (char_u *)_(" Adding");
4515 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4516 {
4517 /* Insert a new line, keep indentation but ignore 'comments' */
4518#ifdef FEAT_COMMENTS
4519 char_u *old = curbuf->b_p_com;
4520
4521 curbuf->b_p_com = (char_u *)"";
4522#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004523 compl_startpos.lnum = curwin->w_cursor.lnum;
4524 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 ins_eol('\r');
4526#ifdef FEAT_COMMENTS
4527 curbuf->b_p_com = old;
4528#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004529 compl_length = 0;
4530 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 }
4532 }
4533 else
4534 {
4535 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004536 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 }
4538
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004539 if (compl_cont_status & CONT_LOCAL)
4540 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 else
4542 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4543
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004544 /* Always add completion for the original text. */
4545 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004546 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4547 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004548 -1, FALSE, NULL, NULL, 0, ORIGINAL_TEXT) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004550 vim_free(compl_pattern);
4551 compl_pattern = NULL;
4552 vim_free(compl_orig_text);
4553 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 return FAIL;
4555 }
4556
4557 /* showmode might reset the internal line pointers, so it must
4558 * be called before line = ml_get(), or when this address is no
4559 * longer needed. -- Acevedo.
4560 */
4561 edit_submode_extra = (char_u *)_("-- Searching...");
4562 edit_submode_highl = HLF_COUNT;
4563 showmode();
4564 edit_submode_extra = NULL;
4565 out_flush();
4566 }
4567
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004568 compl_shown_match = compl_curr_match;
4569 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570
4571 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004572 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004574 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004576 /* may undisplay the popup menu */
4577 ins_compl_upd_pum();
4578
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004579 if (n > 1) /* all matches have been found */
4580 compl_matches = n;
4581 compl_curr_match = compl_shown_match;
4582 compl_direction = compl_shows_dir;
4583 compl_interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584
4585 /* eat the ESC to avoid leaving insert mode */
4586 if (got_int && !global_busy)
4587 {
4588 (void)vgetc();
4589 got_int = FALSE;
4590 }
4591
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004592 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004593 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004595 edit_submode_extra = (compl_cont_status & CONT_ADDING)
4596 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
4598 edit_submode_highl = HLF_E;
4599 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4600 * because we couldn't expand anything at first place, but if we used
4601 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4602 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004603 if ( compl_length > 1
4604 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 || (ctrl_x_mode != 0
4606 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
4607 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004608 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 }
4610
Bram Moolenaar572cb562005-08-05 21:35:02 +00004611 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004612 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004614 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615
4616 if (edit_submode_extra == NULL)
4617 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004618 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 {
4620 edit_submode_extra = (char_u *)_("Back at original");
4621 edit_submode_highl = HLF_W;
4622 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004623 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 {
4625 edit_submode_extra = (char_u *)_("Word from other line");
4626 edit_submode_highl = HLF_COUNT;
4627 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004628 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 {
4630 edit_submode_extra = (char_u *)_("The only match");
4631 edit_submode_highl = HLF_COUNT;
4632 }
4633 else
4634 {
4635 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004636 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004638 int number = 0;
4639 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004641 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 {
4643 /* search backwards for the first valid (!= -1) number.
4644 * This should normally succeed already at the first loop
4645 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004646 for (match = compl_curr_match->cp_prev; match != NULL
4647 && match != compl_first_match;
4648 match = match->cp_prev)
4649 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004651 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 break;
4653 }
4654 if (match != NULL)
4655 /* go up and assign all numbers which are not assigned
4656 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004657 for (match = match->cp_next;
4658 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004659 match = match->cp_next)
4660 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 }
4662 else /* BACKWARD */
4663 {
4664 /* search forwards (upwards) for the first valid (!= -1)
4665 * number. This should normally succeed already at the
4666 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004667 for (match = compl_curr_match->cp_next; match != NULL
4668 && match != compl_first_match;
4669 match = match->cp_next)
4670 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004672 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 break;
4674 }
4675 if (match != NULL)
4676 /* go down and assign all numbers which are not
4677 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004678 for (match = match->cp_prev; match
4679 && match->cp_number == -1;
4680 match = match->cp_prev)
4681 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 }
4683 }
4684
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004685 /* The match should always have a sequence number now, this is
4686 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004687 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 {
4689 /* Space for 10 text chars. + 2x10-digit no.s */
4690 static char_u match_ref[31];
4691
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004692 if (compl_matches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 sprintf((char *)IObuff, _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004694 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004696 sprintf((char *)IObuff, _("match %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004697 compl_curr_match->cp_number);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004698 vim_strncpy(match_ref, IObuff, 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 edit_submode_extra = match_ref;
4700 edit_submode_highl = HLF_R;
4701 if (dollar_vcol)
4702 curs_columns(FALSE);
4703 }
4704 }
4705 }
4706
4707 /* Show a message about what (completion) mode we're in. */
4708 showmode();
4709 if (edit_submode_extra != NULL)
4710 {
4711 if (!p_smd)
4712 msg_attr(edit_submode_extra,
4713 edit_submode_highl < HLF_COUNT
4714 ? hl_attr(edit_submode_highl) : 0);
4715 }
4716 else
4717 msg_clr_cmdline(); /* necessary for "noshowmode" */
4718
Bram Moolenaara94bc432006-03-10 21:42:59 +00004719 /* RedrawingDisabled may be set when invoked through complete(). */
4720 n = RedrawingDisabled;
4721 RedrawingDisabled = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004722 ins_compl_show_pum();
Bram Moolenaara94bc432006-03-10 21:42:59 +00004723 setcursor();
4724 RedrawingDisabled = n;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004725
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 return OK;
4727}
4728
4729/*
4730 * Looks in the first "len" chars. of "src" for search-metachars.
4731 * If dest is not NULL the chars. are copied there quoting (with
4732 * a backslash) the metachars, and dest would be NUL terminated.
4733 * Returns the length (needed) of dest
4734 */
4735 static int
4736quote_meta(dest, src, len)
4737 char_u *dest;
4738 char_u *src;
4739 int len;
4740{
4741 int m;
4742
4743 for (m = len; --len >= 0; src++)
4744 {
4745 switch (*src)
4746 {
4747 case '.':
4748 case '*':
4749 case '[':
4750 if (ctrl_x_mode == CTRL_X_DICTIONARY
4751 || ctrl_x_mode == CTRL_X_THESAURUS)
4752 break;
4753 case '~':
4754 if (!p_magic) /* quote these only if magic is set */
4755 break;
4756 case '\\':
4757 if (ctrl_x_mode == CTRL_X_DICTIONARY
4758 || ctrl_x_mode == CTRL_X_THESAURUS)
4759 break;
4760 case '^': /* currently it's not needed. */
4761 case '$':
4762 m++;
4763 if (dest != NULL)
4764 *dest++ = '\\';
4765 break;
4766 }
4767 if (dest != NULL)
4768 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004769# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 /* Copy remaining bytes of a multibyte character. */
4771 if (has_mbyte)
4772 {
4773 int i, mb_len;
4774
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004775 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 if (mb_len > 0 && len >= mb_len)
4777 for (i = 0; i < mb_len; ++i)
4778 {
4779 --len;
4780 ++src;
4781 if (dest != NULL)
4782 *dest++ = *src;
4783 }
4784 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004785# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 }
4787 if (dest != NULL)
4788 *dest = NUL;
4789
4790 return m;
4791}
4792#endif /* FEAT_INS_EXPAND */
4793
4794/*
4795 * Next character is interpreted literally.
4796 * A one, two or three digit decimal number is interpreted as its byte value.
4797 * If one or two digits are entered, the next character is given to vungetc().
4798 * For Unicode a character > 255 may be returned.
4799 */
4800 int
4801get_literal()
4802{
4803 int cc;
4804 int nc;
4805 int i;
4806 int hex = FALSE;
4807 int octal = FALSE;
4808#ifdef FEAT_MBYTE
4809 int unicode = 0;
4810#endif
4811
4812 if (got_int)
4813 return Ctrl_C;
4814
4815#ifdef FEAT_GUI
4816 /*
4817 * In GUI there is no point inserting the internal code for a special key.
4818 * It is more useful to insert the string "<KEY>" instead. This would
4819 * probably be useful in a text window too, but it would not be
4820 * vi-compatible (maybe there should be an option for it?) -- webb
4821 */
4822 if (gui.in_use)
4823 ++allow_keys;
4824#endif
4825#ifdef USE_ON_FLY_SCROLL
4826 dont_scroll = TRUE; /* disallow scrolling here */
4827#endif
4828 ++no_mapping; /* don't map the next key hits */
4829 cc = 0;
4830 i = 0;
4831 for (;;)
4832 {
4833 do
4834 nc = safe_vgetc();
4835 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
4836 || nc == K_HOR_SCROLLBAR);
4837#ifdef FEAT_CMDL_INFO
4838 if (!(State & CMDLINE)
4839# ifdef FEAT_MBYTE
4840 && MB_BYTE2LEN_CHECK(nc) == 1
4841# endif
4842 )
4843 add_to_showcmd(nc);
4844#endif
4845 if (nc == 'x' || nc == 'X')
4846 hex = TRUE;
4847 else if (nc == 'o' || nc == 'O')
4848 octal = TRUE;
4849#ifdef FEAT_MBYTE
4850 else if (nc == 'u' || nc == 'U')
4851 unicode = nc;
4852#endif
4853 else
4854 {
4855 if (hex
4856#ifdef FEAT_MBYTE
4857 || unicode != 0
4858#endif
4859 )
4860 {
4861 if (!vim_isxdigit(nc))
4862 break;
4863 cc = cc * 16 + hex2nr(nc);
4864 }
4865 else if (octal)
4866 {
4867 if (nc < '0' || nc > '7')
4868 break;
4869 cc = cc * 8 + nc - '0';
4870 }
4871 else
4872 {
4873 if (!VIM_ISDIGIT(nc))
4874 break;
4875 cc = cc * 10 + nc - '0';
4876 }
4877
4878 ++i;
4879 }
4880
4881 if (cc > 255
4882#ifdef FEAT_MBYTE
4883 && unicode == 0
4884#endif
4885 )
4886 cc = 255; /* limit range to 0-255 */
4887 nc = 0;
4888
4889 if (hex) /* hex: up to two chars */
4890 {
4891 if (i >= 2)
4892 break;
4893 }
4894#ifdef FEAT_MBYTE
4895 else if (unicode) /* Unicode: up to four or eight chars */
4896 {
4897 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
4898 break;
4899 }
4900#endif
4901 else if (i >= 3) /* decimal or octal: up to three chars */
4902 break;
4903 }
4904 if (i == 0) /* no number entered */
4905 {
4906 if (nc == K_ZERO) /* NUL is stored as NL */
4907 {
4908 cc = '\n';
4909 nc = 0;
4910 }
4911 else
4912 {
4913 cc = nc;
4914 nc = 0;
4915 }
4916 }
4917
4918 if (cc == 0) /* NUL is stored as NL */
4919 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00004920#ifdef FEAT_MBYTE
4921 if (enc_dbcs && (cc & 0xff) == 0)
4922 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
4923 second byte will cause trouble! */
4924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925
4926 --no_mapping;
4927#ifdef FEAT_GUI
4928 if (gui.in_use)
4929 --allow_keys;
4930#endif
4931 if (nc)
4932 vungetc(nc);
4933 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
4934 return cc;
4935}
4936
4937/*
4938 * Insert character, taking care of special keys and mod_mask
4939 */
4940 static void
4941insert_special(c, allow_modmask, ctrlv)
4942 int c;
4943 int allow_modmask;
4944 int ctrlv; /* c was typed after CTRL-V */
4945{
4946 char_u *p;
4947 int len;
4948
4949 /*
4950 * Special function key, translate into "<Key>". Up to the last '>' is
4951 * inserted with ins_str(), so as not to replace characters in replace
4952 * mode.
4953 * Only use mod_mask for special keys, to avoid things like <S-Space>,
4954 * unless 'allow_modmask' is TRUE.
4955 */
4956#ifdef MACOS
4957 /* Command-key never produces a normal key */
4958 if (mod_mask & MOD_MASK_CMD)
4959 allow_modmask = TRUE;
4960#endif
4961 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
4962 {
4963 p = get_special_key_name(c, mod_mask);
4964 len = (int)STRLEN(p);
4965 c = p[len - 1];
4966 if (len > 2)
4967 {
4968 if (stop_arrow() == FAIL)
4969 return;
4970 p[len - 1] = NUL;
4971 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00004972 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 ctrlv = FALSE;
4974 }
4975 }
4976 if (stop_arrow() == OK)
4977 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
4978}
4979
4980/*
4981 * Special characters in this context are those that need processing other
4982 * than the simple insertion that can be performed here. This includes ESC
4983 * which terminates the insert, and CR/NL which need special processing to
4984 * open up a new line. This routine tries to optimize insertions performed by
4985 * the "redo", "undo" or "put" commands, so it needs to know when it should
4986 * stop and defer processing to the "normal" mechanism.
4987 * '0' and '^' are special, because they can be followed by CTRL-D.
4988 */
4989#ifdef EBCDIC
4990# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
4991#else
4992# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
4993#endif
4994
4995#ifdef FEAT_MBYTE
4996# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
4997#else
4998# define WHITECHAR(cc) vim_iswhite(cc)
4999#endif
5000
5001 void
5002insertchar(c, flags, second_indent)
5003 int c; /* character to insert or NUL */
5004 int flags; /* INSCHAR_FORMAT, etc. */
5005 int second_indent; /* indent for second line if >= 0 */
5006{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 int textwidth;
5008#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012
5013 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5014 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015
5016 /*
5017 * Try to break the line in two or more pieces when:
5018 * - Always do this if we have been called to do formatting only.
5019 * - Always do this when 'formatoptions' has the 'a' flag and the line
5020 * ends in white space.
5021 * - Otherwise:
5022 * - Don't do this if inserting a blank
5023 * - Don't do this if an existing character is being replaced, unless
5024 * we're in VREPLACE mode.
5025 * - Do this if the cursor is not on the line where insert started
5026 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5027 * before the insert.
5028 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5029 * before 'textwidth'
5030 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005031 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 && ((flags & INSCHAR_FORMAT)
5033 || (!vim_iswhite(c)
5034 && !((State & REPLACE_FLAG)
5035#ifdef FEAT_VREPLACE
5036 && !(State & VREPLACE_FLAG)
5037#endif
5038 && *ml_get_cursor() != NUL)
5039 && (curwin->w_cursor.lnum != Insstart.lnum
5040 || ((!has_format_option(FO_INS_LONG)
5041 || Insstart_textlen <= (colnr_T)textwidth)
5042 && (!fo_ins_blank
5043 || Insstart_blank_vcol <= (colnr_T)textwidth
5044 ))))))
5045 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005046 /* Format with 'formatexpr' when it's set. Use internal formatting
5047 * when 'formatexpr' isn't set or it returns non-zero. */
5048#if defined(FEAT_EVAL)
5049 if (*curbuf->b_p_fex == NUL
5050 || fex_format(curwin->w_cursor.lnum, 1L) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005052 internal_format(textwidth, second_indent, flags, c == NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005054
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 if (c == NUL) /* only formatting was wanted */
5056 return;
5057
5058#ifdef FEAT_COMMENTS
5059 /* Check whether this character should end a comment. */
5060 if (did_ai && (int)c == end_comment_pending)
5061 {
5062 char_u *line;
5063 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5064 int middle_len, end_len;
5065 int i;
5066
5067 /*
5068 * Need to remove existing (middle) comment leader and insert end
5069 * comment leader. First, check what comment leader we can find.
5070 */
5071 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5072 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5073 {
5074 /* Skip middle-comment string */
5075 while (*p && p[-1] != ':') /* find end of middle flags */
5076 ++p;
5077 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5078 /* Don't count trailing white space for middle_len */
5079 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5080 --middle_len;
5081
5082 /* Find the end-comment string */
5083 while (*p && p[-1] != ':') /* find end of end flags */
5084 ++p;
5085 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5086
5087 /* Skip white space before the cursor */
5088 i = curwin->w_cursor.col;
5089 while (--i >= 0 && vim_iswhite(line[i]))
5090 ;
5091 i++;
5092
5093 /* Skip to before the middle leader */
5094 i -= middle_len;
5095
5096 /* Check some expected things before we go on */
5097 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5098 {
5099 /* Backspace over all the stuff we want to replace */
5100 backspace_until_column(i);
5101
5102 /*
5103 * Insert the end-comment string, except for the last
5104 * character, which will get inserted as normal later.
5105 */
5106 ins_bytes_len(lead_end, end_len - 1);
5107 }
5108 }
5109 }
5110 end_comment_pending = NUL;
5111#endif
5112
5113 did_ai = FALSE;
5114#ifdef FEAT_SMARTINDENT
5115 did_si = FALSE;
5116 can_si = FALSE;
5117 can_si_back = FALSE;
5118#endif
5119
5120 /*
5121 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5122 * This speeds up normal text input considerably.
5123 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5124 * need to re-indent at a ':', or any other character (but not what
5125 * 'paste' is set)..
5126 */
5127#ifdef USE_ON_FLY_SCROLL
5128 dont_scroll = FALSE; /* allow scrolling here */
5129#endif
5130
5131 if ( !ISSPECIAL(c)
5132#ifdef FEAT_MBYTE
5133 && (!has_mbyte || (*mb_char2len)(c) == 1)
5134#endif
5135 && vpeekc() != NUL
5136 && !(State & REPLACE_FLAG)
5137#ifdef FEAT_CINDENT
5138 && !cindent_on()
5139#endif
5140#ifdef FEAT_RIGHTLEFT
5141 && !p_ri
5142#endif
5143 )
5144 {
5145#define INPUT_BUFLEN 100
5146 char_u buf[INPUT_BUFLEN + 1];
5147 int i;
5148 colnr_T virtcol = 0;
5149
5150 buf[0] = c;
5151 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005152 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 virtcol = get_nolist_virtcol();
5154 /*
5155 * Stop the string when:
5156 * - no more chars available
5157 * - finding a special character (command key)
5158 * - buffer is full
5159 * - running into the 'textwidth' boundary
5160 * - need to check for abbreviation: A non-word char after a word-char
5161 */
5162 while ( (c = vpeekc()) != NUL
5163 && !ISSPECIAL(c)
5164#ifdef FEAT_MBYTE
5165 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5166#endif
5167 && i < INPUT_BUFLEN
5168 && (textwidth == 0
5169 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5170 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5171 {
5172#ifdef FEAT_RIGHTLEFT
5173 c = vgetc();
5174 if (p_hkmap && KeyTyped)
5175 c = hkmap(c); /* Hebrew mode mapping */
5176# ifdef FEAT_FKMAP
5177 if (p_fkmap && KeyTyped)
5178 c = fkmap(c); /* Farsi mode mapping */
5179# endif
5180 buf[i++] = c;
5181#else
5182 buf[i++] = vgetc();
5183#endif
5184 }
5185
5186#ifdef FEAT_DIGRAPHS
5187 do_digraph(-1); /* clear digraphs */
5188 do_digraph(buf[i-1]); /* may be the start of a digraph */
5189#endif
5190 buf[i] = NUL;
5191 ins_str(buf);
5192 if (flags & INSCHAR_CTRLV)
5193 {
5194 redo_literal(*buf);
5195 i = 1;
5196 }
5197 else
5198 i = 0;
5199 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005200 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 }
5202 else
5203 {
5204#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005205 int cc;
5206
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5208 {
5209 char_u buf[MB_MAXBYTES + 1];
5210
5211 (*mb_char2bytes)(c, buf);
5212 buf[cc] = NUL;
5213 ins_char_bytes(buf, cc);
5214 AppendCharToRedobuff(c);
5215 }
5216 else
5217#endif
5218 {
5219 ins_char(c);
5220 if (flags & INSCHAR_CTRLV)
5221 redo_literal(c);
5222 else
5223 AppendCharToRedobuff(c);
5224 }
5225 }
5226}
5227
5228/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005229 * Format text at the current insert position.
5230 */
5231 static void
5232internal_format(textwidth, second_indent, flags, format_only)
5233 int textwidth;
5234 int second_indent;
5235 int flags;
5236 int format_only;
5237{
5238 int cc;
5239 int save_char = NUL;
5240 int haveto_redraw = FALSE;
5241 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5242#ifdef FEAT_MBYTE
5243 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5244#endif
5245 int fo_white_par = has_format_option(FO_WHITE_PAR);
5246 int first_line = TRUE;
5247#ifdef FEAT_COMMENTS
5248 colnr_T leader_len;
5249 int no_leader = FALSE;
5250 int do_comments = (flags & INSCHAR_DO_COM);
5251#endif
5252
5253 /*
5254 * When 'ai' is off we don't want a space under the cursor to be
5255 * deleted. Replace it with an 'x' temporarily.
5256 */
5257 if (!curbuf->b_p_ai)
5258 {
5259 cc = gchar_cursor();
5260 if (vim_iswhite(cc))
5261 {
5262 save_char = cc;
5263 pchar_cursor('x');
5264 }
5265 }
5266
5267 /*
5268 * Repeat breaking lines, until the current line is not too long.
5269 */
5270 while (!got_int)
5271 {
5272 int startcol; /* Cursor column at entry */
5273 int wantcol; /* column at textwidth border */
5274 int foundcol; /* column for start of spaces */
5275 int end_foundcol = 0; /* column for start of word */
5276 colnr_T len;
5277 colnr_T virtcol;
5278#ifdef FEAT_VREPLACE
5279 int orig_col = 0;
5280 char_u *saved_text = NULL;
5281#endif
5282 colnr_T col;
5283
5284 virtcol = get_nolist_virtcol();
5285 if (virtcol < (colnr_T)textwidth)
5286 break;
5287
5288#ifdef FEAT_COMMENTS
5289 if (no_leader)
5290 do_comments = FALSE;
5291 else if (!(flags & INSCHAR_FORMAT)
5292 && has_format_option(FO_WRAP_COMS))
5293 do_comments = TRUE;
5294
5295 /* Don't break until after the comment leader */
5296 if (do_comments)
5297 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5298 else
5299 leader_len = 0;
5300
5301 /* If the line doesn't start with a comment leader, then don't
5302 * start one in a following broken line. Avoids that a %word
5303 * moved to the start of the next line causes all following lines
5304 * to start with %. */
5305 if (leader_len == 0)
5306 no_leader = TRUE;
5307#endif
5308 if (!(flags & INSCHAR_FORMAT)
5309#ifdef FEAT_COMMENTS
5310 && leader_len == 0
5311#endif
5312 && !has_format_option(FO_WRAP))
5313
5314 {
5315 textwidth = 0;
5316 break;
5317 }
5318 if ((startcol = curwin->w_cursor.col) == 0)
5319 break;
5320
5321 /* find column of textwidth border */
5322 coladvance((colnr_T)textwidth);
5323 wantcol = curwin->w_cursor.col;
5324
5325 curwin->w_cursor.col = startcol - 1;
5326#ifdef FEAT_MBYTE
5327 /* Correct cursor for multi-byte character. */
5328 if (has_mbyte)
5329 mb_adjust_cursor();
5330#endif
5331 foundcol = 0;
5332
5333 /*
5334 * Find position to break at.
5335 * Stop at first entered white when 'formatoptions' has 'v'
5336 */
5337 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5338 || curwin->w_cursor.lnum != Insstart.lnum
5339 || curwin->w_cursor.col >= Insstart.col)
5340 {
5341 cc = gchar_cursor();
5342 if (WHITECHAR(cc))
5343 {
5344 /* remember position of blank just before text */
5345 end_foundcol = curwin->w_cursor.col;
5346
5347 /* find start of sequence of blanks */
5348 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5349 {
5350 dec_cursor();
5351 cc = gchar_cursor();
5352 }
5353 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5354 break; /* only spaces in front of text */
5355#ifdef FEAT_COMMENTS
5356 /* Don't break until after the comment leader */
5357 if (curwin->w_cursor.col < leader_len)
5358 break;
5359#endif
5360 if (has_format_option(FO_ONE_LETTER))
5361 {
5362 /* do not break after one-letter words */
5363 if (curwin->w_cursor.col == 0)
5364 break; /* one-letter word at begin */
5365
5366 col = curwin->w_cursor.col;
5367 dec_cursor();
5368 cc = gchar_cursor();
5369
5370 if (WHITECHAR(cc))
5371 continue; /* one-letter, continue */
5372 curwin->w_cursor.col = col;
5373 }
5374#ifdef FEAT_MBYTE
5375 if (has_mbyte)
5376 foundcol = curwin->w_cursor.col
5377 + (*mb_ptr2len)(ml_get_cursor());
5378 else
5379#endif
5380 foundcol = curwin->w_cursor.col + 1;
5381 if (curwin->w_cursor.col < (colnr_T)wantcol)
5382 break;
5383 }
5384#ifdef FEAT_MBYTE
5385 else if (cc >= 0x100 && fo_multibyte
5386 && curwin->w_cursor.col <= (colnr_T)wantcol)
5387 {
5388 /* Break after or before a multi-byte character. */
5389 foundcol = curwin->w_cursor.col;
5390 if (curwin->w_cursor.col < (colnr_T)wantcol)
5391 foundcol += (*mb_char2len)(cc);
5392 end_foundcol = foundcol;
5393 break;
5394 }
5395#endif
5396 if (curwin->w_cursor.col == 0)
5397 break;
5398 dec_cursor();
5399 }
5400
5401 if (foundcol == 0) /* no spaces, cannot break line */
5402 {
5403 curwin->w_cursor.col = startcol;
5404 break;
5405 }
5406
5407 /* Going to break the line, remove any "$" now. */
5408 undisplay_dollar();
5409
5410 /*
5411 * Offset between cursor position and line break is used by replace
5412 * stack functions. VREPLACE does not use this, and backspaces
5413 * over the text instead.
5414 */
5415#ifdef FEAT_VREPLACE
5416 if (State & VREPLACE_FLAG)
5417 orig_col = startcol; /* Will start backspacing from here */
5418 else
5419#endif
5420 replace_offset = startcol - end_foundcol - 1;
5421
5422 /*
5423 * adjust startcol for spaces that will be deleted and
5424 * characters that will remain on top line
5425 */
5426 curwin->w_cursor.col = foundcol;
5427 while (cc = gchar_cursor(), WHITECHAR(cc))
5428 inc_cursor();
5429 startcol -= curwin->w_cursor.col;
5430 if (startcol < 0)
5431 startcol = 0;
5432
5433#ifdef FEAT_VREPLACE
5434 if (State & VREPLACE_FLAG)
5435 {
5436 /*
5437 * In VREPLACE mode, we will backspace over the text to be
5438 * wrapped, so save a copy now to put on the next line.
5439 */
5440 saved_text = vim_strsave(ml_get_cursor());
5441 curwin->w_cursor.col = orig_col;
5442 if (saved_text == NULL)
5443 break; /* Can't do it, out of memory */
5444 saved_text[startcol] = NUL;
5445
5446 /* Backspace over characters that will move to the next line */
5447 if (!fo_white_par)
5448 backspace_until_column(foundcol);
5449 }
5450 else
5451#endif
5452 {
5453 /* put cursor after pos. to break line */
5454 if (!fo_white_par)
5455 curwin->w_cursor.col = foundcol;
5456 }
5457
5458 /*
5459 * Split the line just before the margin.
5460 * Only insert/delete lines, but don't really redraw the window.
5461 */
5462 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
5463 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
5464#ifdef FEAT_COMMENTS
5465 + (do_comments ? OPENLINE_DO_COM : 0)
5466#endif
5467 , old_indent);
5468 old_indent = 0;
5469
5470 replace_offset = 0;
5471 if (first_line)
5472 {
5473 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
5474 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
5475 if (second_indent >= 0)
5476 {
5477#ifdef FEAT_VREPLACE
5478 if (State & VREPLACE_FLAG)
5479 change_indent(INDENT_SET, second_indent, FALSE, NUL);
5480 else
5481#endif
5482 (void)set_indent(second_indent, SIN_CHANGED);
5483 }
5484 first_line = FALSE;
5485 }
5486
5487#ifdef FEAT_VREPLACE
5488 if (State & VREPLACE_FLAG)
5489 {
5490 /*
5491 * In VREPLACE mode we have backspaced over the text to be
5492 * moved, now we re-insert it into the new line.
5493 */
5494 ins_bytes(saved_text);
5495 vim_free(saved_text);
5496 }
5497 else
5498#endif
5499 {
5500 /*
5501 * Check if cursor is not past the NUL off the line, cindent
5502 * may have added or removed indent.
5503 */
5504 curwin->w_cursor.col += startcol;
5505 len = (colnr_T)STRLEN(ml_get_curline());
5506 if (curwin->w_cursor.col > len)
5507 curwin->w_cursor.col = len;
5508 }
5509
5510 haveto_redraw = TRUE;
5511#ifdef FEAT_CINDENT
5512 can_cindent = TRUE;
5513#endif
5514 /* moved the cursor, don't autoindent or cindent now */
5515 did_ai = FALSE;
5516#ifdef FEAT_SMARTINDENT
5517 did_si = FALSE;
5518 can_si = FALSE;
5519 can_si_back = FALSE;
5520#endif
5521 line_breakcheck();
5522 }
5523
5524 if (save_char != NUL) /* put back space after cursor */
5525 pchar_cursor(save_char);
5526
5527 if (!format_only && haveto_redraw)
5528 {
5529 update_topline();
5530 redraw_curbuf_later(VALID);
5531 }
5532}
5533
5534/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 * Called after inserting or deleting text: When 'formatoptions' includes the
5536 * 'a' flag format from the current line until the end of the paragraph.
5537 * Keep the cursor at the same position relative to the text.
5538 * The caller must have saved the cursor line for undo, following ones will be
5539 * saved here.
5540 */
5541 void
5542auto_format(trailblank, prev_line)
5543 int trailblank; /* when TRUE also format with trailing blank */
5544 int prev_line; /* may start in previous line */
5545{
5546 pos_T pos;
5547 colnr_T len;
5548 char_u *old;
5549 char_u *new, *pnew;
5550 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005551 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552
5553 if (!has_format_option(FO_AUTO))
5554 return;
5555
5556 pos = curwin->w_cursor;
5557 old = ml_get_curline();
5558
5559 /* may remove added space */
5560 check_auto_format(FALSE);
5561
5562 /* Don't format in Insert mode when the cursor is on a trailing blank, the
5563 * user might insert normal text next. Also skip formatting when "1" is
5564 * in 'formatoptions' and there is a single character before the cursor.
5565 * Otherwise the line would be broken and when typing another non-white
5566 * next they are not joined back together. */
5567 wasatend = (pos.col == STRLEN(old));
5568 if (*old != NUL && !trailblank && wasatend)
5569 {
5570 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005571 cc = gchar_cursor();
5572 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
5573 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005575 cc = gchar_cursor();
5576 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 {
5578 curwin->w_cursor = pos;
5579 return;
5580 }
5581 curwin->w_cursor = pos;
5582 }
5583
5584#ifdef FEAT_COMMENTS
5585 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
5586 * comments. */
5587 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
5588 && get_leader_len(old, NULL, FALSE) == 0)
5589 return;
5590#endif
5591
5592 /*
5593 * May start formatting in a previous line, so that after "x" a word is
5594 * moved to the previous line if it fits there now. Only when this is not
5595 * the start of a paragraph.
5596 */
5597 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
5598 {
5599 --curwin->w_cursor.lnum;
5600 if (u_save_cursor() == FAIL)
5601 return;
5602 }
5603
5604 /*
5605 * Do the formatting and restore the cursor position. "saved_cursor" will
5606 * be adjusted for the text formatting.
5607 */
5608 saved_cursor = pos;
5609 format_lines((linenr_T)-1);
5610 curwin->w_cursor = saved_cursor;
5611 saved_cursor.lnum = 0;
5612
5613 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5614 {
5615 /* "cannot happen" */
5616 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5617 coladvance((colnr_T)MAXCOL);
5618 }
5619 else
5620 check_cursor_col();
5621
5622 /* Insert mode: If the cursor is now after the end of the line while it
5623 * previously wasn't, the line was broken. Because of the rule above we
5624 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
5625 * formatted. */
5626 if (!wasatend && has_format_option(FO_WHITE_PAR))
5627 {
5628 new = ml_get_curline();
5629 len = STRLEN(new);
5630 if (curwin->w_cursor.col == len)
5631 {
5632 pnew = vim_strnsave(new, len + 2);
5633 pnew[len] = ' ';
5634 pnew[len + 1] = NUL;
5635 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
5636 /* remove the space later */
5637 did_add_space = TRUE;
5638 }
5639 else
5640 /* may remove added space */
5641 check_auto_format(FALSE);
5642 }
5643
5644 check_cursor();
5645}
5646
5647/*
5648 * When an extra space was added to continue a paragraph for auto-formatting,
5649 * delete it now. The space must be under the cursor, just after the insert
5650 * position.
5651 */
5652 static void
5653check_auto_format(end_insert)
5654 int end_insert; /* TRUE when ending Insert mode */
5655{
5656 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005657 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658
5659 if (did_add_space)
5660 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005661 cc = gchar_cursor();
5662 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 /* Somehow the space was removed already. */
5664 did_add_space = FALSE;
5665 else
5666 {
5667 if (!end_insert)
5668 {
5669 inc_cursor();
5670 c = gchar_cursor();
5671 dec_cursor();
5672 }
5673 if (c != NUL)
5674 {
5675 /* The space is no longer at the end of the line, delete it. */
5676 del_char(FALSE);
5677 did_add_space = FALSE;
5678 }
5679 }
5680 }
5681}
5682
5683/*
5684 * Find out textwidth to be used for formatting:
5685 * if 'textwidth' option is set, use it
5686 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
5687 * if invalid value, use 0.
5688 * Set default to window width (maximum 79) for "gq" operator.
5689 */
5690 int
5691comp_textwidth(ff)
5692 int ff; /* force formatting (for "Q" command) */
5693{
5694 int textwidth;
5695
5696 textwidth = curbuf->b_p_tw;
5697 if (textwidth == 0 && curbuf->b_p_wm)
5698 {
5699 /* The width is the window width minus 'wrapmargin' minus all the
5700 * things that add to the margin. */
5701 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
5702#ifdef FEAT_CMDWIN
5703 if (cmdwin_type != 0)
5704 textwidth -= 1;
5705#endif
5706#ifdef FEAT_FOLDING
5707 textwidth -= curwin->w_p_fdc;
5708#endif
5709#ifdef FEAT_SIGNS
5710 if (curwin->w_buffer->b_signlist != NULL
5711# ifdef FEAT_NETBEANS_INTG
5712 || usingNetbeans
5713# endif
5714 )
5715 textwidth -= 1;
5716#endif
5717 if (curwin->w_p_nu)
5718 textwidth -= 8;
5719 }
5720 if (textwidth < 0)
5721 textwidth = 0;
5722 if (ff && textwidth == 0)
5723 {
5724 textwidth = W_WIDTH(curwin) - 1;
5725 if (textwidth > 79)
5726 textwidth = 79;
5727 }
5728 return textwidth;
5729}
5730
5731/*
5732 * Put a character in the redo buffer, for when just after a CTRL-V.
5733 */
5734 static void
5735redo_literal(c)
5736 int c;
5737{
5738 char_u buf[10];
5739
5740 /* Only digits need special treatment. Translate them into a string of
5741 * three digits. */
5742 if (VIM_ISDIGIT(c))
5743 {
5744 sprintf((char *)buf, "%03d", c);
5745 AppendToRedobuff(buf);
5746 }
5747 else
5748 AppendCharToRedobuff(c);
5749}
5750
5751/*
5752 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005753 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 */
5755 static void
5756start_arrow(end_insert_pos)
5757 pos_T *end_insert_pos;
5758{
5759 if (!arrow_used) /* something has been inserted */
5760 {
5761 AppendToRedobuff(ESC_STR);
5762 stop_insert(end_insert_pos, FALSE);
5763 arrow_used = TRUE; /* this means we stopped the current insert */
5764 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005765#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005766 check_spell_redraw();
5767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768}
5769
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005770#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005771/*
5772 * If we skipped highlighting word at cursor, do it now.
5773 * It may be skipped again, thus reset spell_redraw_lnum first.
5774 */
5775 static void
5776check_spell_redraw()
5777{
5778 if (spell_redraw_lnum != 0)
5779 {
5780 linenr_T lnum = spell_redraw_lnum;
5781
5782 spell_redraw_lnum = 0;
5783 redrawWinline(lnum, FALSE);
5784 }
5785}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005786
5787/*
5788 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5789 * spelled word, if there is one.
5790 */
5791 static void
5792spell_back_to_badword()
5793{
5794 pos_T tpos = curwin->w_cursor;
5795
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00005796 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005797 if (curwin->w_cursor.col != tpos.col)
5798 start_arrow(&tpos);
5799}
Bram Moolenaar217ad922005-03-20 22:37:15 +00005800#endif
5801
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802/*
5803 * stop_arrow() is called before a change is made in insert mode.
5804 * If an arrow key has been used, start a new insertion.
5805 * Returns FAIL if undo is impossible, shouldn't insert then.
5806 */
5807 int
5808stop_arrow()
5809{
5810 if (arrow_used)
5811 {
5812 if (u_save_cursor() == OK)
5813 {
5814 arrow_used = FALSE;
5815 ins_need_undo = FALSE;
5816 }
5817 Insstart = curwin->w_cursor; /* new insertion starts here */
5818 Insstart_textlen = linetabsize(ml_get_curline());
5819 ai_col = 0;
5820#ifdef FEAT_VREPLACE
5821 if (State & VREPLACE_FLAG)
5822 {
5823 orig_line_count = curbuf->b_ml.ml_line_count;
5824 vr_lines_changed = 1;
5825 }
5826#endif
5827 ResetRedobuff();
5828 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00005829 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 }
5831 else if (ins_need_undo)
5832 {
5833 if (u_save_cursor() == OK)
5834 ins_need_undo = FALSE;
5835 }
5836
5837#ifdef FEAT_FOLDING
5838 /* Always open fold at the cursor line when inserting something. */
5839 foldOpenCursor();
5840#endif
5841
5842 return (arrow_used || ins_need_undo ? FAIL : OK);
5843}
5844
5845/*
5846 * do a few things to stop inserting
5847 */
5848 static void
5849stop_insert(end_insert_pos, esc)
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005850 pos_T *end_insert_pos; /* where insert ended */
5851 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005853 int cc;
5854 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855
5856 stop_redo_ins();
5857 replace_flush(); /* abandon replace stack */
5858
5859 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005860 * Save the inserted text for later redo with ^@ and CTRL-A.
5861 * Don't do it when "restart_edit" was set and nothing was inserted,
5862 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005864 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00005865 if (did_restart_edit == 0 || (ptr != NULL
5866 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005867 {
5868 vim_free(last_insert);
5869 last_insert = ptr;
5870 last_insert_skip = new_insert_skip;
5871 }
5872 else
5873 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874
5875 if (!arrow_used)
5876 {
5877 /* Auto-format now. It may seem strange to do this when stopping an
5878 * insertion (or moving the cursor), but it's required when appending
5879 * a line and having it end in a space. But only do it when something
5880 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005881 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005883 pos_T tpos = curwin->w_cursor;
5884
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 /* When the cursor is at the end of the line after a space the
5886 * formatting will move it to the following word. Avoid that by
5887 * moving the cursor onto the space. */
5888 cc = 'x';
5889 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
5890 {
5891 dec_cursor();
5892 cc = gchar_cursor();
5893 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005894 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 }
5896
5897 auto_format(TRUE, FALSE);
5898
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005899 if (vim_iswhite(cc))
5900 {
5901 if (gchar_cursor() != NUL)
5902 inc_cursor();
5903#ifdef FEAT_VIRTUALEDIT
5904 /* If the cursor is still at the same character, also keep
5905 * the "coladd". */
5906 if (gchar_cursor() == NUL
5907 && curwin->w_cursor.lnum == tpos.lnum
5908 && curwin->w_cursor.col == tpos.col)
5909 curwin->w_cursor.coladd = tpos.coladd;
5910#endif
5911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 }
5913
5914 /* If a space was inserted for auto-formatting, remove it now. */
5915 check_auto_format(TRUE);
5916
5917 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005918 * of the line, and put the cursor back.
5919 * Do this when ESC was used or moving the cursor up/down. */
5920 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
5921 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005923 pos_T tpos = curwin->w_cursor;
5924
5925 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar39f05632006-03-19 22:15:26 +00005926 for (;;)
5927 {
5928 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
5929 --curwin->w_cursor.col;
5930 cc = gchar_cursor();
5931 if (!vim_iswhite(cc))
5932 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 (void)del_char(TRUE);
Bram Moolenaar39f05632006-03-19 22:15:26 +00005934 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005935 if (curwin->w_cursor.lnum != tpos.lnum)
5936 curwin->w_cursor = tpos;
5937 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 ++curwin->w_cursor.col; /* put cursor back on the NUL */
5939
5940#ifdef FEAT_VISUAL
5941 /* <C-S-Right> may have started Visual mode, adjust the position for
5942 * deleted characters. */
5943 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
5944 {
5945 cc = STRLEN(ml_get_curline());
5946 if (VIsual.col > (colnr_T)cc)
5947 {
5948 VIsual.col = cc;
5949# ifdef FEAT_VIRTUALEDIT
5950 VIsual.coladd = 0;
5951# endif
5952 }
5953 }
5954#endif
5955 }
5956 }
5957 did_ai = FALSE;
5958#ifdef FEAT_SMARTINDENT
5959 did_si = FALSE;
5960 can_si = FALSE;
5961 can_si_back = FALSE;
5962#endif
5963
5964 /* set '[ and '] to the inserted text */
5965 curbuf->b_op_start = Insstart;
5966 curbuf->b_op_end = *end_insert_pos;
5967}
5968
5969/*
5970 * Set the last inserted text to a single character.
5971 * Used for the replace command.
5972 */
5973 void
5974set_last_insert(c)
5975 int c;
5976{
5977 char_u *s;
5978
5979 vim_free(last_insert);
5980#ifdef FEAT_MBYTE
5981 last_insert = alloc(MB_MAXBYTES * 3 + 5);
5982#else
5983 last_insert = alloc(6);
5984#endif
5985 if (last_insert != NULL)
5986 {
5987 s = last_insert;
5988 /* Use the CTRL-V only when entering a special char */
5989 if (c < ' ' || c == DEL)
5990 *s++ = Ctrl_V;
5991 s = add_char2buf(c, s);
5992 *s++ = ESC;
5993 *s++ = NUL;
5994 last_insert_skip = 0;
5995 }
5996}
5997
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005998#if defined(EXITFREE) || defined(PROTO)
5999 void
6000free_last_insert()
6001{
6002 vim_free(last_insert);
6003 last_insert = NULL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006004 vim_free(compl_orig_text);
6005 compl_orig_text = NULL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006006}
6007#endif
6008
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009/*
6010 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6011 * and CSI. Handle multi-byte characters.
6012 * Returns a pointer to after the added bytes.
6013 */
6014 char_u *
6015add_char2buf(c, s)
6016 int c;
6017 char_u *s;
6018{
6019#ifdef FEAT_MBYTE
6020 char_u temp[MB_MAXBYTES];
6021 int i;
6022 int len;
6023
6024 len = (*mb_char2bytes)(c, temp);
6025 for (i = 0; i < len; ++i)
6026 {
6027 c = temp[i];
6028#endif
6029 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6030 if (c == K_SPECIAL)
6031 {
6032 *s++ = K_SPECIAL;
6033 *s++ = KS_SPECIAL;
6034 *s++ = KE_FILLER;
6035 }
6036#ifdef FEAT_GUI
6037 else if (c == CSI)
6038 {
6039 *s++ = CSI;
6040 *s++ = KS_EXTRA;
6041 *s++ = (int)KE_CSI;
6042 }
6043#endif
6044 else
6045 *s++ = c;
6046#ifdef FEAT_MBYTE
6047 }
6048#endif
6049 return s;
6050}
6051
6052/*
6053 * move cursor to start of line
6054 * if flags & BL_WHITE move to first non-white
6055 * if flags & BL_SOL move to first non-white if startofline is set,
6056 * otherwise keep "curswant" column
6057 * if flags & BL_FIX don't leave the cursor on a NUL.
6058 */
6059 void
6060beginline(flags)
6061 int flags;
6062{
6063 if ((flags & BL_SOL) && !p_sol)
6064 coladvance(curwin->w_curswant);
6065 else
6066 {
6067 curwin->w_cursor.col = 0;
6068#ifdef FEAT_VIRTUALEDIT
6069 curwin->w_cursor.coladd = 0;
6070#endif
6071
6072 if (flags & (BL_WHITE | BL_SOL))
6073 {
6074 char_u *ptr;
6075
6076 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6077 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6078 ++curwin->w_cursor.col;
6079 }
6080 curwin->w_set_curswant = TRUE;
6081 }
6082}
6083
6084/*
6085 * oneright oneleft cursor_down cursor_up
6086 *
6087 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006088 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 * Return OK when successful, FAIL when we hit a line of file boundary.
6090 */
6091
6092 int
6093oneright()
6094{
6095 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097
6098#ifdef FEAT_VIRTUALEDIT
6099 if (virtual_active())
6100 {
6101 pos_T prevpos = curwin->w_cursor;
6102
6103 /* Adjust for multi-wide char (excluding TAB) */
6104 ptr = ml_get_cursor();
6105 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006106# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006108# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006110# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 ))
6112 ? ptr2cells(ptr) : 1));
6113 curwin->w_set_curswant = TRUE;
6114 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6115 return (prevpos.col != curwin->w_cursor.col
6116 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
6117 }
6118#endif
6119
6120 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006121 if (*ptr == NUL)
6122 return FAIL; /* already at the very end */
6123
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006125 if (has_mbyte)
6126 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 else
6128#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006129 l = 1;
6130
6131 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6132 * contains "onemore". */
6133 if (ptr[l] == NUL
6134#ifdef FEAT_VIRTUALEDIT
6135 && (ve_flags & VE_ONEMORE) == 0
6136#endif
6137 )
6138 return FAIL;
6139 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140
6141 curwin->w_set_curswant = TRUE;
6142 return OK;
6143}
6144
6145 int
6146oneleft()
6147{
6148#ifdef FEAT_VIRTUALEDIT
6149 if (virtual_active())
6150 {
6151 int width;
6152 int v = getviscol();
6153
6154 if (v == 0)
6155 return FAIL;
6156
6157# ifdef FEAT_LINEBREAK
6158 /* We might get stuck on 'showbreak', skip over it. */
6159 width = 1;
6160 for (;;)
6161 {
6162 coladvance(v - width);
6163 /* getviscol() is slow, skip it when 'showbreak' is empty and
6164 * there are no multi-byte characters */
6165 if ((*p_sbr == NUL
6166# ifdef FEAT_MBYTE
6167 && !has_mbyte
6168# endif
6169 ) || getviscol() < v)
6170 break;
6171 ++width;
6172 }
6173# else
6174 coladvance(v - 1);
6175# endif
6176
6177 if (curwin->w_cursor.coladd == 1)
6178 {
6179 char_u *ptr;
6180
6181 /* Adjust for multi-wide char (not a TAB) */
6182 ptr = ml_get_cursor();
6183 if (*ptr != TAB && vim_isprintc(
6184# ifdef FEAT_MBYTE
6185 (*mb_ptr2char)(ptr)
6186# else
6187 *ptr
6188# endif
6189 ) && ptr2cells(ptr) > 1)
6190 curwin->w_cursor.coladd = 0;
6191 }
6192
6193 curwin->w_set_curswant = TRUE;
6194 return OK;
6195 }
6196#endif
6197
6198 if (curwin->w_cursor.col == 0)
6199 return FAIL;
6200
6201 curwin->w_set_curswant = TRUE;
6202 --curwin->w_cursor.col;
6203
6204#ifdef FEAT_MBYTE
6205 /* if the character on the left of the current cursor is a multi-byte
6206 * character, move to its first byte */
6207 if (has_mbyte)
6208 mb_adjust_cursor();
6209#endif
6210 return OK;
6211}
6212
6213 int
6214cursor_up(n, upd_topline)
6215 long n;
6216 int upd_topline; /* When TRUE: update topline */
6217{
6218 linenr_T lnum;
6219
6220 if (n > 0)
6221 {
6222 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006223 /* This fails if the cursor is already in the first line or the count
6224 * is larger than the line number and '-' is in 'cpoptions' */
6225 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 return FAIL;
6227 if (n >= lnum)
6228 lnum = 1;
6229 else
6230#ifdef FEAT_FOLDING
6231 if (hasAnyFolding(curwin))
6232 {
6233 /*
6234 * Count each sequence of folded lines as one logical line.
6235 */
6236 /* go to the the start of the current fold */
6237 (void)hasFolding(lnum, &lnum, NULL);
6238
6239 while (n--)
6240 {
6241 /* move up one line */
6242 --lnum;
6243 if (lnum <= 1)
6244 break;
6245 /* If we entered a fold, move to the beginning, unless in
6246 * Insert mode or when 'foldopen' contains "all": it will open
6247 * in a moment. */
6248 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6249 (void)hasFolding(lnum, &lnum, NULL);
6250 }
6251 if (lnum < 1)
6252 lnum = 1;
6253 }
6254 else
6255#endif
6256 lnum -= n;
6257 curwin->w_cursor.lnum = lnum;
6258 }
6259
6260 /* try to advance to the column we want to be at */
6261 coladvance(curwin->w_curswant);
6262
6263 if (upd_topline)
6264 update_topline(); /* make sure curwin->w_topline is valid */
6265
6266 return OK;
6267}
6268
6269/*
6270 * Cursor down a number of logical lines.
6271 */
6272 int
6273cursor_down(n, upd_topline)
6274 long n;
6275 int upd_topline; /* When TRUE: update topline */
6276{
6277 linenr_T lnum;
6278
6279 if (n > 0)
6280 {
6281 lnum = curwin->w_cursor.lnum;
6282#ifdef FEAT_FOLDING
6283 /* Move to last line of fold, will fail if it's the end-of-file. */
6284 (void)hasFolding(lnum, NULL, &lnum);
6285#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006286 /* This fails if the cursor is already in the last line or would move
6287 * beyound the last line and '-' is in 'cpoptions' */
6288 if (lnum >= curbuf->b_ml.ml_line_count
6289 || (lnum + n > curbuf->b_ml.ml_line_count
6290 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 return FAIL;
6292 if (lnum + n >= curbuf->b_ml.ml_line_count)
6293 lnum = curbuf->b_ml.ml_line_count;
6294 else
6295#ifdef FEAT_FOLDING
6296 if (hasAnyFolding(curwin))
6297 {
6298 linenr_T last;
6299
6300 /* count each sequence of folded lines as one logical line */
6301 while (n--)
6302 {
6303 if (hasFolding(lnum, NULL, &last))
6304 lnum = last + 1;
6305 else
6306 ++lnum;
6307 if (lnum >= curbuf->b_ml.ml_line_count)
6308 break;
6309 }
6310 if (lnum > curbuf->b_ml.ml_line_count)
6311 lnum = curbuf->b_ml.ml_line_count;
6312 }
6313 else
6314#endif
6315 lnum += n;
6316 curwin->w_cursor.lnum = lnum;
6317 }
6318
6319 /* try to advance to the column we want to be at */
6320 coladvance(curwin->w_curswant);
6321
6322 if (upd_topline)
6323 update_topline(); /* make sure curwin->w_topline is valid */
6324
6325 return OK;
6326}
6327
6328/*
6329 * Stuff the last inserted text in the read buffer.
6330 * Last_insert actually is a copy of the redo buffer, so we
6331 * first have to remove the command.
6332 */
6333 int
6334stuff_inserted(c, count, no_esc)
6335 int c; /* Command character to be inserted */
6336 long count; /* Repeat this many times */
6337 int no_esc; /* Don't add an ESC at the end */
6338{
6339 char_u *esc_ptr;
6340 char_u *ptr;
6341 char_u *last_ptr;
6342 char_u last = NUL;
6343
6344 ptr = get_last_insert();
6345 if (ptr == NULL)
6346 {
6347 EMSG(_(e_noinstext));
6348 return FAIL;
6349 }
6350
6351 /* may want to stuff the command character, to start Insert mode */
6352 if (c != NUL)
6353 stuffcharReadbuff(c);
6354 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
6355 *esc_ptr = NUL; /* remove the ESC */
6356
6357 /* when the last char is either "0" or "^" it will be quoted if no ESC
6358 * comes after it OR if it will inserted more than once and "ptr"
6359 * starts with ^D. -- Acevedo
6360 */
6361 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
6362 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
6363 && (no_esc || (*ptr == Ctrl_D && count > 1)))
6364 {
6365 last = *last_ptr;
6366 *last_ptr = NUL;
6367 }
6368
6369 do
6370 {
6371 stuffReadbuff(ptr);
6372 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6373 if (last)
6374 stuffReadbuff((char_u *)(last == '0'
6375 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
6376 : IF_EB("\026^", CTRL_V_STR "^")));
6377 }
6378 while (--count > 0);
6379
6380 if (last)
6381 *last_ptr = last;
6382
6383 if (esc_ptr != NULL)
6384 *esc_ptr = ESC; /* put the ESC back */
6385
6386 /* may want to stuff a trailing ESC, to get out of Insert mode */
6387 if (!no_esc)
6388 stuffcharReadbuff(ESC);
6389
6390 return OK;
6391}
6392
6393 char_u *
6394get_last_insert()
6395{
6396 if (last_insert == NULL)
6397 return NULL;
6398 return last_insert + last_insert_skip;
6399}
6400
6401/*
6402 * Get last inserted string, and remove trailing <Esc>.
6403 * Returns pointer to allocated memory (must be freed) or NULL.
6404 */
6405 char_u *
6406get_last_insert_save()
6407{
6408 char_u *s;
6409 int len;
6410
6411 if (last_insert == NULL)
6412 return NULL;
6413 s = vim_strsave(last_insert + last_insert_skip);
6414 if (s != NULL)
6415 {
6416 len = (int)STRLEN(s);
6417 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
6418 s[len - 1] = NUL;
6419 }
6420 return s;
6421}
6422
6423/*
6424 * Check the word in front of the cursor for an abbreviation.
6425 * Called when the non-id character "c" has been entered.
6426 * When an abbreviation is recognized it is removed from the text and
6427 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
6428 */
6429 static int
6430echeck_abbr(c)
6431 int c;
6432{
6433 /* Don't check for abbreviation in paste mode, when disabled and just
6434 * after moving around with cursor keys. */
6435 if (p_paste || no_abbr || arrow_used)
6436 return FALSE;
6437
6438 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
6439 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
6440}
6441
6442/*
6443 * replace-stack functions
6444 *
6445 * When replacing characters, the replaced characters are remembered for each
6446 * new character. This is used to re-insert the old text when backspacing.
6447 *
6448 * There is a NUL headed list of characters for each character that is
6449 * currently in the file after the insertion point. When BS is used, one NUL
6450 * headed list is put back for the deleted character.
6451 *
6452 * For a newline, there are two NUL headed lists. One contains the characters
6453 * that the NL replaced. The extra one stores the characters after the cursor
6454 * that were deleted (always white space).
6455 *
6456 * Replace_offset is normally 0, in which case replace_push will add a new
6457 * character at the end of the stack. If replace_offset is not 0, that many
6458 * characters will be left on the stack above the newly inserted character.
6459 */
6460
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00006461static char_u *replace_stack = NULL;
6462static long replace_stack_nr = 0; /* next entry in replace stack */
6463static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464
6465 void
6466replace_push(c)
6467 int c; /* character that is replaced (NUL is none) */
6468{
6469 char_u *p;
6470
6471 if (replace_stack_nr < replace_offset) /* nothing to do */
6472 return;
6473 if (replace_stack_len <= replace_stack_nr)
6474 {
6475 replace_stack_len += 50;
6476 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
6477 if (p == NULL) /* out of memory */
6478 {
6479 replace_stack_len -= 50;
6480 return;
6481 }
6482 if (replace_stack != NULL)
6483 {
6484 mch_memmove(p, replace_stack,
6485 (size_t)(replace_stack_nr * sizeof(char_u)));
6486 vim_free(replace_stack);
6487 }
6488 replace_stack = p;
6489 }
6490 p = replace_stack + replace_stack_nr - replace_offset;
6491 if (replace_offset)
6492 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
6493 *p = c;
6494 ++replace_stack_nr;
6495}
6496
6497/*
6498 * call replace_push(c) with replace_offset set to the first NUL.
6499 */
6500 static void
6501replace_push_off(c)
6502 int c;
6503{
6504 char_u *p;
6505
6506 p = replace_stack + replace_stack_nr;
6507 for (replace_offset = 1; replace_offset < replace_stack_nr;
6508 ++replace_offset)
6509 if (*--p == NUL)
6510 break;
6511 replace_push(c);
6512 replace_offset = 0;
6513}
6514
6515/*
6516 * Pop one item from the replace stack.
6517 * return -1 if stack empty
6518 * return replaced character or NUL otherwise
6519 */
6520 static int
6521replace_pop()
6522{
6523 if (replace_stack_nr == 0)
6524 return -1;
6525 return (int)replace_stack[--replace_stack_nr];
6526}
6527
6528/*
6529 * Join the top two items on the replace stack. This removes to "off"'th NUL
6530 * encountered.
6531 */
6532 static void
6533replace_join(off)
6534 int off; /* offset for which NUL to remove */
6535{
6536 int i;
6537
6538 for (i = replace_stack_nr; --i >= 0; )
6539 if (replace_stack[i] == NUL && off-- <= 0)
6540 {
6541 --replace_stack_nr;
6542 mch_memmove(replace_stack + i, replace_stack + i + 1,
6543 (size_t)(replace_stack_nr - i));
6544 return;
6545 }
6546}
6547
6548/*
6549 * Pop bytes from the replace stack until a NUL is found, and insert them
6550 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
6551 */
6552 static void
6553replace_pop_ins()
6554{
6555 int cc;
6556 int oldState = State;
6557
6558 State = NORMAL; /* don't want REPLACE here */
6559 while ((cc = replace_pop()) > 0)
6560 {
6561#ifdef FEAT_MBYTE
6562 mb_replace_pop_ins(cc);
6563#else
6564 ins_char(cc);
6565#endif
6566 dec_cursor();
6567 }
6568 State = oldState;
6569}
6570
6571#ifdef FEAT_MBYTE
6572/*
6573 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
6574 * indicates a multi-byte char, pop the other bytes too.
6575 */
6576 static void
6577mb_replace_pop_ins(cc)
6578 int cc;
6579{
6580 int n;
6581 char_u buf[MB_MAXBYTES];
6582 int i;
6583 int c;
6584
6585 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
6586 {
6587 buf[0] = cc;
6588 for (i = 1; i < n; ++i)
6589 buf[i] = replace_pop();
6590 ins_bytes_len(buf, n);
6591 }
6592 else
6593 ins_char(cc);
6594
6595 if (enc_utf8)
6596 /* Handle composing chars. */
6597 for (;;)
6598 {
6599 c = replace_pop();
6600 if (c == -1) /* stack empty */
6601 break;
6602 if ((n = MB_BYTE2LEN(c)) == 1)
6603 {
6604 /* Not a multi-byte char, put it back. */
6605 replace_push(c);
6606 break;
6607 }
6608 else
6609 {
6610 buf[0] = c;
6611 for (i = 1; i < n; ++i)
6612 buf[i] = replace_pop();
6613 if (utf_iscomposing(utf_ptr2char(buf)))
6614 ins_bytes_len(buf, n);
6615 else
6616 {
6617 /* Not a composing char, put it back. */
6618 for (i = n - 1; i >= 0; --i)
6619 replace_push(buf[i]);
6620 break;
6621 }
6622 }
6623 }
6624}
6625#endif
6626
6627/*
6628 * make the replace stack empty
6629 * (called when exiting replace mode)
6630 */
6631 static void
6632replace_flush()
6633{
6634 vim_free(replace_stack);
6635 replace_stack = NULL;
6636 replace_stack_len = 0;
6637 replace_stack_nr = 0;
6638}
6639
6640/*
6641 * Handle doing a BS for one character.
6642 * cc < 0: replace stack empty, just move cursor
6643 * cc == 0: character was inserted, delete it
6644 * cc > 0: character was replaced, put cc (first byte of original char) back
6645 * and check for more characters to be put back
6646 */
6647 static void
6648replace_do_bs()
6649{
6650 int cc;
6651#ifdef FEAT_VREPLACE
6652 int orig_len = 0;
6653 int ins_len;
6654 int orig_vcols = 0;
6655 colnr_T start_vcol;
6656 char_u *p;
6657 int i;
6658 int vcol;
6659#endif
6660
6661 cc = replace_pop();
6662 if (cc > 0)
6663 {
6664#ifdef FEAT_VREPLACE
6665 if (State & VREPLACE_FLAG)
6666 {
6667 /* Get the number of screen cells used by the character we are
6668 * going to delete. */
6669 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
6670 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
6671 }
6672#endif
6673#ifdef FEAT_MBYTE
6674 if (has_mbyte)
6675 {
6676 del_char(FALSE);
6677# ifdef FEAT_VREPLACE
6678 if (State & VREPLACE_FLAG)
6679 orig_len = STRLEN(ml_get_cursor());
6680# endif
6681 replace_push(cc);
6682 }
6683 else
6684#endif
6685 {
6686 pchar_cursor(cc);
6687#ifdef FEAT_VREPLACE
6688 if (State & VREPLACE_FLAG)
6689 orig_len = STRLEN(ml_get_cursor()) - 1;
6690#endif
6691 }
6692 replace_pop_ins();
6693
6694#ifdef FEAT_VREPLACE
6695 if (State & VREPLACE_FLAG)
6696 {
6697 /* Get the number of screen cells used by the inserted characters */
6698 p = ml_get_cursor();
6699 ins_len = STRLEN(p) - orig_len;
6700 vcol = start_vcol;
6701 for (i = 0; i < ins_len; ++i)
6702 {
6703 vcol += chartabsize(p + i, vcol);
6704#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006705 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706#endif
6707 }
6708 vcol -= start_vcol;
6709
6710 /* Delete spaces that were inserted after the cursor to keep the
6711 * text aligned. */
6712 curwin->w_cursor.col += ins_len;
6713 while (vcol > orig_vcols && gchar_cursor() == ' ')
6714 {
6715 del_char(FALSE);
6716 ++orig_vcols;
6717 }
6718 curwin->w_cursor.col -= ins_len;
6719 }
6720#endif
6721
6722 /* mark the buffer as changed and prepare for displaying */
6723 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
6724 }
6725 else if (cc == 0)
6726 (void)del_char(FALSE);
6727}
6728
6729#ifdef FEAT_CINDENT
6730/*
6731 * Return TRUE if C-indenting is on.
6732 */
6733 static int
6734cindent_on()
6735{
6736 return (!p_paste && (curbuf->b_p_cin
6737# ifdef FEAT_EVAL
6738 || *curbuf->b_p_inde != NUL
6739# endif
6740 ));
6741}
6742#endif
6743
6744#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
6745/*
6746 * Re-indent the current line, based on the current contents of it and the
6747 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
6748 * confused what all the part that handles Control-T is doing that I'm not.
6749 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
6750 */
6751
6752 void
6753fixthisline(get_the_indent)
6754 int (*get_the_indent) __ARGS((void));
6755{
6756 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
6757 if (linewhite(curwin->w_cursor.lnum))
6758 did_ai = TRUE; /* delete the indent if the line stays empty */
6759}
6760
6761 void
6762fix_indent()
6763{
6764 if (p_paste)
6765 return;
6766# ifdef FEAT_LISP
6767 if (curbuf->b_p_lisp && curbuf->b_p_ai)
6768 fixthisline(get_lisp_indent);
6769# endif
6770# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
6771 else
6772# endif
6773# ifdef FEAT_CINDENT
6774 if (cindent_on())
6775 do_c_expr_indent();
6776# endif
6777}
6778
6779#endif
6780
6781#ifdef FEAT_CINDENT
6782/*
6783 * return TRUE if 'cinkeys' contains the key "keytyped",
6784 * when == '*': Only if key is preceded with '*' (indent before insert)
6785 * when == '!': Only if key is prededed with '!' (don't insert)
6786 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
6787 *
6788 * "keytyped" can have a few special values:
6789 * KEY_OPEN_FORW
6790 * KEY_OPEN_BACK
6791 * KEY_COMPLETE just finished completion.
6792 *
6793 * If line_is_empty is TRUE accept keys with '0' before them.
6794 */
6795 int
6796in_cinkeys(keytyped, when, line_is_empty)
6797 int keytyped;
6798 int when;
6799 int line_is_empty;
6800{
6801 char_u *look;
6802 int try_match;
6803 int try_match_word;
6804 char_u *p;
6805 char_u *line;
6806 int icase;
6807 int i;
6808
6809#ifdef FEAT_EVAL
6810 if (*curbuf->b_p_inde != NUL)
6811 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
6812 else
6813#endif
6814 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
6815 while (*look)
6816 {
6817 /*
6818 * Find out if we want to try a match with this key, depending on
6819 * 'when' and a '*' or '!' before the key.
6820 */
6821 switch (when)
6822 {
6823 case '*': try_match = (*look == '*'); break;
6824 case '!': try_match = (*look == '!'); break;
6825 default: try_match = (*look != '*'); break;
6826 }
6827 if (*look == '*' || *look == '!')
6828 ++look;
6829
6830 /*
6831 * If there is a '0', only accept a match if the line is empty.
6832 * But may still match when typing last char of a word.
6833 */
6834 if (*look == '0')
6835 {
6836 try_match_word = try_match;
6837 if (!line_is_empty)
6838 try_match = FALSE;
6839 ++look;
6840 }
6841 else
6842 try_match_word = FALSE;
6843
6844 /*
6845 * does it look like a control character?
6846 */
6847 if (*look == '^'
6848#ifdef EBCDIC
6849 && (Ctrl_chr(look[1]) != 0)
6850#else
6851 && look[1] >= '?' && look[1] <= '_'
6852#endif
6853 )
6854 {
6855 if (try_match && keytyped == Ctrl_chr(look[1]))
6856 return TRUE;
6857 look += 2;
6858 }
6859 /*
6860 * 'o' means "o" command, open forward.
6861 * 'O' means "O" command, open backward.
6862 */
6863 else if (*look == 'o')
6864 {
6865 if (try_match && keytyped == KEY_OPEN_FORW)
6866 return TRUE;
6867 ++look;
6868 }
6869 else if (*look == 'O')
6870 {
6871 if (try_match && keytyped == KEY_OPEN_BACK)
6872 return TRUE;
6873 ++look;
6874 }
6875
6876 /*
6877 * 'e' means to check for "else" at start of line and just before the
6878 * cursor.
6879 */
6880 else if (*look == 'e')
6881 {
6882 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
6883 {
6884 p = ml_get_curline();
6885 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
6886 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
6887 return TRUE;
6888 }
6889 ++look;
6890 }
6891
6892 /*
6893 * ':' only causes an indent if it is at the end of a label or case
6894 * statement, or when it was before typing the ':' (to fix
6895 * class::method for C++).
6896 */
6897 else if (*look == ':')
6898 {
6899 if (try_match && keytyped == ':')
6900 {
6901 p = ml_get_curline();
6902 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
6903 return TRUE;
6904 if (curwin->w_cursor.col > 2
6905 && p[curwin->w_cursor.col - 1] == ':'
6906 && p[curwin->w_cursor.col - 2] == ':')
6907 {
6908 p[curwin->w_cursor.col - 1] = ' ';
6909 i = (cin_iscase(p) || cin_isscopedecl(p)
6910 || cin_islabel(30));
6911 p = ml_get_curline();
6912 p[curwin->w_cursor.col - 1] = ':';
6913 if (i)
6914 return TRUE;
6915 }
6916 }
6917 ++look;
6918 }
6919
6920
6921 /*
6922 * Is it a key in <>, maybe?
6923 */
6924 else if (*look == '<')
6925 {
6926 if (try_match)
6927 {
6928 /*
6929 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
6930 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
6931 * >, *, : and ! keys if they really really want to.
6932 */
6933 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
6934 && keytyped == look[1])
6935 return TRUE;
6936
6937 if (keytyped == get_special_key_code(look + 1))
6938 return TRUE;
6939 }
6940 while (*look && *look != '>')
6941 look++;
6942 while (*look == '>')
6943 look++;
6944 }
6945
6946 /*
6947 * Is it a word: "=word"?
6948 */
6949 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
6950 {
6951 ++look;
6952 if (*look == '~')
6953 {
6954 icase = TRUE;
6955 ++look;
6956 }
6957 else
6958 icase = FALSE;
6959 p = vim_strchr(look, ',');
6960 if (p == NULL)
6961 p = look + STRLEN(look);
6962 if ((try_match || try_match_word)
6963 && curwin->w_cursor.col >= (colnr_T)(p - look))
6964 {
6965 int match = FALSE;
6966
6967#ifdef FEAT_INS_EXPAND
6968 if (keytyped == KEY_COMPLETE)
6969 {
6970 char_u *s;
6971
6972 /* Just completed a word, check if it starts with "look".
6973 * search back for the start of a word. */
6974 line = ml_get_curline();
6975# ifdef FEAT_MBYTE
6976 if (has_mbyte)
6977 {
6978 char_u *n;
6979
6980 for (s = line + curwin->w_cursor.col; s > line; s = n)
6981 {
6982 n = mb_prevptr(line, s);
6983 if (!vim_iswordp(n))
6984 break;
6985 }
6986 }
6987 else
6988# endif
6989 for (s = line + curwin->w_cursor.col; s > line; --s)
6990 if (!vim_iswordc(s[-1]))
6991 break;
6992 if (s + (p - look) <= line + curwin->w_cursor.col
6993 && (icase
6994 ? MB_STRNICMP(s, look, p - look)
6995 : STRNCMP(s, look, p - look)) == 0)
6996 match = TRUE;
6997 }
6998 else
6999#endif
7000 /* TODO: multi-byte */
7001 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7002 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7003 {
7004 line = ml_get_cursor();
7005 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7006 || !vim_iswordc(line[-(p - look) - 1]))
7007 && (icase
7008 ? MB_STRNICMP(line - (p - look), look, p - look)
7009 : STRNCMP(line - (p - look), look, p - look))
7010 == 0)
7011 match = TRUE;
7012 }
7013 if (match && try_match_word && !try_match)
7014 {
7015 /* "0=word": Check if there are only blanks before the
7016 * word. */
7017 line = ml_get_curline();
7018 if ((int)(skipwhite(line) - line) !=
7019 (int)(curwin->w_cursor.col - (p - look)))
7020 match = FALSE;
7021 }
7022 if (match)
7023 return TRUE;
7024 }
7025 look = p;
7026 }
7027
7028 /*
7029 * ok, it's a boring generic character.
7030 */
7031 else
7032 {
7033 if (try_match && *look == keytyped)
7034 return TRUE;
7035 ++look;
7036 }
7037
7038 /*
7039 * Skip over ", ".
7040 */
7041 look = skip_to_option_part(look);
7042 }
7043 return FALSE;
7044}
7045#endif /* FEAT_CINDENT */
7046
7047#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7048/*
7049 * Map Hebrew keyboard when in hkmap mode.
7050 */
7051 int
7052hkmap(c)
7053 int c;
7054{
7055 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7056 {
7057 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7058 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7059 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7060 static char_u map[26] =
7061 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7062 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7063 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7064 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7065 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7066 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7067 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7068 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7069 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7070
7071 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7072 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7073 /* '-1'='sofit' */
7074 else if (c == 'x')
7075 return 'X';
7076 else if (c == 'q')
7077 return '\''; /* {geresh}={'} */
7078 else if (c == 246)
7079 return ' '; /* \"o --> ' ' for a german keyboard */
7080 else if (c == 228)
7081 return ' '; /* \"a --> ' ' -- / -- */
7082 else if (c == 252)
7083 return ' '; /* \"u --> ' ' -- / -- */
7084#ifdef EBCDIC
7085 else if (islower(c))
7086#else
7087 /* NOTE: islower() does not do the right thing for us on Linux so we
7088 * do this the same was as 5.7 and previous, so it works correctly on
7089 * all systems. Specifically, the e.g. Delete and Arrow keys are
7090 * munged and won't work if e.g. searching for Hebrew text.
7091 */
7092 else if (c >= 'a' && c <= 'z')
7093#endif
7094 return (int)(map[CharOrdLow(c)] + p_aleph);
7095 else
7096 return c;
7097 }
7098 else
7099 {
7100 switch (c)
7101 {
7102 case '`': return ';';
7103 case '/': return '.';
7104 case '\'': return ',';
7105 case 'q': return '/';
7106 case 'w': return '\'';
7107
7108 /* Hebrew letters - set offset from 'a' */
7109 case ',': c = '{'; break;
7110 case '.': c = 'v'; break;
7111 case ';': c = 't'; break;
7112 default: {
7113 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7114
7115#ifdef EBCDIC
7116 /* see note about islower() above */
7117 if (!islower(c))
7118#else
7119 if (c < 'a' || c > 'z')
7120#endif
7121 return c;
7122 c = str[CharOrdLow(c)];
7123 break;
7124 }
7125 }
7126
7127 return (int)(CharOrdLow(c) + p_aleph);
7128 }
7129}
7130#endif
7131
7132 static void
7133ins_reg()
7134{
7135 int need_redraw = FALSE;
7136 int regname;
7137 int literally = 0;
7138
7139 /*
7140 * If we are going to wait for a character, show a '"'.
7141 */
7142 pc_status = PC_STATUS_UNSET;
7143 if (redrawing() && !char_avail())
7144 {
7145 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007146 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147
7148 edit_putchar('"', TRUE);
7149#ifdef FEAT_CMDL_INFO
7150 add_to_showcmd_c(Ctrl_R);
7151#endif
7152 }
7153
7154#ifdef USE_ON_FLY_SCROLL
7155 dont_scroll = TRUE; /* disallow scrolling here */
7156#endif
7157
7158 /*
7159 * Don't map the register name. This also prevents the mode message to be
7160 * deleted when ESC is hit.
7161 */
7162 ++no_mapping;
7163 regname = safe_vgetc();
7164#ifdef FEAT_LANGMAP
7165 LANGMAP_ADJUST(regname, TRUE);
7166#endif
7167 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
7168 {
7169 /* Get a third key for literal register insertion */
7170 literally = regname;
7171#ifdef FEAT_CMDL_INFO
7172 add_to_showcmd_c(literally);
7173#endif
7174 regname = safe_vgetc();
7175#ifdef FEAT_LANGMAP
7176 LANGMAP_ADJUST(regname, TRUE);
7177#endif
7178 }
7179 --no_mapping;
7180
7181#ifdef FEAT_EVAL
7182 /*
7183 * Don't call u_sync() while getting the expression,
7184 * evaluating it or giving an error message for it!
7185 */
7186 ++no_u_sync;
7187 if (regname == '=')
7188 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007189# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007191# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007193# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 /* Restore the Input Method. */
7195 if (im_on)
7196 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007197# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00007199 if (regname == NUL || !valid_yank_reg(regname, FALSE))
7200 {
7201 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 else
7205 {
7206#endif
7207 if (literally == Ctrl_O || literally == Ctrl_P)
7208 {
7209 /* Append the command to the redo buffer. */
7210 AppendCharToRedobuff(Ctrl_R);
7211 AppendCharToRedobuff(literally);
7212 AppendCharToRedobuff(regname);
7213
7214 do_put(regname, BACKWARD, 1L,
7215 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
7216 }
7217 else if (insert_reg(regname, literally) == FAIL)
7218 {
7219 vim_beep();
7220 need_redraw = TRUE; /* remove the '"' */
7221 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007222 else if (stop_insert_mode)
7223 /* When the '=' register was used and a function was invoked that
7224 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7225 * insert anything, need to remove the '"' */
7226 need_redraw = TRUE;
7227
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228#ifdef FEAT_EVAL
7229 }
7230 --no_u_sync;
7231#endif
7232#ifdef FEAT_CMDL_INFO
7233 clear_showcmd();
7234#endif
7235
7236 /* If the inserted register is empty, we need to remove the '"' */
7237 if (need_redraw || stuff_empty())
7238 edit_unputchar();
7239}
7240
7241/*
7242 * CTRL-G commands in Insert mode.
7243 */
7244 static void
7245ins_ctrl_g()
7246{
7247 int c;
7248
7249#ifdef FEAT_INS_EXPAND
7250 /* Right after CTRL-X the cursor will be after the ruler. */
7251 setcursor();
7252#endif
7253
7254 /*
7255 * Don't map the second key. This also prevents the mode message to be
7256 * deleted when ESC is hit.
7257 */
7258 ++no_mapping;
7259 c = safe_vgetc();
7260 --no_mapping;
7261 switch (c)
7262 {
7263 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7264 case K_UP:
7265 case Ctrl_K:
7266 case 'k': ins_up(TRUE);
7267 break;
7268
7269 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7270 case K_DOWN:
7271 case Ctrl_J:
7272 case 'j': ins_down(TRUE);
7273 break;
7274
7275 /* CTRL-G u: start new undoable edit */
7276 case 'u': u_sync();
7277 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007278
7279 /* Need to reset Insstart, esp. because a BS that joins
7280 * aline to the previous one must save for undo. */
7281 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 break;
7283
7284 /* Unknown CTRL-G command, reserved for future expansion. */
7285 default: vim_beep();
7286 }
7287}
7288
7289/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007290 * CTRL-^ in Insert mode.
7291 */
7292 static void
7293ins_ctrl_hat()
7294{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00007295 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007296 {
7297 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7298 if (State & LANGMAP)
7299 {
7300 curbuf->b_p_iminsert = B_IMODE_NONE;
7301 State &= ~LANGMAP;
7302 }
7303 else
7304 {
7305 curbuf->b_p_iminsert = B_IMODE_LMAP;
7306 State |= LANGMAP;
7307#ifdef USE_IM_CONTROL
7308 im_set_active(FALSE);
7309#endif
7310 }
7311 }
7312#ifdef USE_IM_CONTROL
7313 else
7314 {
7315 /* There are no ":lmap" mappings, toggle IM */
7316 if (im_get_status())
7317 {
7318 curbuf->b_p_iminsert = B_IMODE_NONE;
7319 im_set_active(FALSE);
7320 }
7321 else
7322 {
7323 curbuf->b_p_iminsert = B_IMODE_IM;
7324 State &= ~LANGMAP;
7325 im_set_active(TRUE);
7326 }
7327 }
7328#endif
7329 set_iminsert_global();
7330 showmode();
7331#ifdef FEAT_GUI
7332 /* may show different cursor shape or color */
7333 if (gui.in_use)
7334 gui_update_cursor(TRUE, FALSE);
7335#endif
7336#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7337 /* Show/unshow value of 'keymap' in status lines. */
7338 status_redraw_curbuf();
7339#endif
7340}
7341
7342/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 * Handle ESC in insert mode.
7344 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7345 * insert.
7346 */
7347 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00007348ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 long *count;
7350 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00007351 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352{
7353 int temp;
7354 static int disabled_redraw = FALSE;
7355
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007356#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007357 check_spell_redraw();
7358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359#if defined(FEAT_HANGULIN)
7360# if defined(ESC_CHG_TO_ENG_MODE)
7361 hangul_input_state_set(0);
7362# endif
7363 if (composing_hangul)
7364 {
7365 push_raw_key(composing_hangul_buffer, 2);
7366 composing_hangul = 0;
7367 }
7368#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369
7370 temp = curwin->w_cursor.col;
7371 if (disabled_redraw)
7372 {
7373 --RedrawingDisabled;
7374 disabled_redraw = FALSE;
7375 }
7376 if (!arrow_used)
7377 {
7378 /*
7379 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00007380 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7381 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 */
7383 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00007384 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385
7386 /*
7387 * Repeating insert may take a long time. Check for
7388 * interrupt now and then.
7389 */
7390 if (*count > 0)
7391 {
7392 line_breakcheck();
7393 if (got_int)
7394 *count = 0;
7395 }
7396
7397 if (--*count > 0) /* repeat what was typed */
7398 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007399 /* Vi repeats the insert without replacing characters. */
7400 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
7401 State &= ~REPLACE_FLAG;
7402
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 (void)start_redo_ins();
7404 if (cmdchar == 'r' || cmdchar == 'v')
7405 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
7406 ++RedrawingDisabled;
7407 disabled_redraw = TRUE;
7408 return FALSE; /* repeat the insert */
7409 }
7410 stop_insert(&curwin->w_cursor, TRUE);
7411 undisplay_dollar();
7412 }
7413
7414 /* When an autoindent was removed, curswant stays after the
7415 * indent */
7416 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
7417 curwin->w_set_curswant = TRUE;
7418
7419 /* Remember the last Insert position in the '^ mark. */
7420 if (!cmdmod.keepjumps)
7421 curbuf->b_last_insert = curwin->w_cursor;
7422
7423 /*
7424 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00007425 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00007427 if (!nomove
7428 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429#ifdef FEAT_VIRTUALEDIT
7430 || curwin->w_cursor.coladd > 0
7431#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007432 )
7433 && (restart_edit == NUL
7434 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00007436 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007438 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439#ifdef FEAT_RIGHTLEFT
7440 && !revins_on
7441#endif
7442 )
7443 {
7444#ifdef FEAT_VIRTUALEDIT
7445 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
7446 {
7447 oneleft();
7448 if (restart_edit != NUL)
7449 ++curwin->w_cursor.coladd;
7450 }
7451 else
7452#endif
7453 {
7454 --curwin->w_cursor.col;
7455#ifdef FEAT_MBYTE
7456 /* Correct cursor for multi-byte character. */
7457 if (has_mbyte)
7458 mb_adjust_cursor();
7459#endif
7460 }
7461 }
7462
7463#ifdef USE_IM_CONTROL
7464 /* Disable IM to allow typing English directly for Normal mode commands.
7465 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
7466 * well). */
7467 if (!(State & LANGMAP))
7468 im_save_status(&curbuf->b_p_iminsert);
7469 im_set_active(FALSE);
7470#endif
7471
7472 State = NORMAL;
7473 /* need to position cursor again (e.g. when on a TAB ) */
7474 changed_cline_bef_curs();
7475
7476#ifdef FEAT_MOUSE
7477 setmouse();
7478#endif
7479#ifdef CURSOR_SHAPE
7480 ui_cursor_shape(); /* may show different cursor shape */
7481#endif
7482
7483 /*
7484 * When recording or for CTRL-O, need to display the new mode.
7485 * Otherwise remove the mode message.
7486 */
7487 if (Recording || restart_edit != NUL)
7488 showmode();
7489 else if (p_smd)
7490 MSG("");
7491
7492 return TRUE; /* exit Insert mode */
7493}
7494
7495#ifdef FEAT_RIGHTLEFT
7496/*
7497 * Toggle language: hkmap and revins_on.
7498 * Move to end of reverse inserted text.
7499 */
7500 static void
7501ins_ctrl_()
7502{
7503 if (revins_on && revins_chars && revins_scol >= 0)
7504 {
7505 while (gchar_cursor() != NUL && revins_chars--)
7506 ++curwin->w_cursor.col;
7507 }
7508 p_ri = !p_ri;
7509 revins_on = (State == INSERT && p_ri);
7510 if (revins_on)
7511 {
7512 revins_scol = curwin->w_cursor.col;
7513 revins_legal++;
7514 revins_chars = 0;
7515 undisplay_dollar();
7516 }
7517 else
7518 revins_scol = -1;
7519#ifdef FEAT_FKMAP
7520 if (p_altkeymap)
7521 {
7522 /*
7523 * to be consistent also for redo command, using '.'
7524 * set arrow_used to true and stop it - causing to redo
7525 * characters entered in one mode (normal/reverse insert).
7526 */
7527 arrow_used = TRUE;
7528 (void)stop_arrow();
7529 p_fkmap = curwin->w_p_rl ^ p_ri;
7530 if (p_fkmap && p_ri)
7531 State = INSERT;
7532 }
7533 else
7534#endif
7535 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
7536 showmode();
7537}
7538#endif
7539
7540#ifdef FEAT_VISUAL
7541/*
7542 * If 'keymodel' contains "startsel", may start selection.
7543 * Returns TRUE when a CTRL-O and other keys stuffed.
7544 */
7545 static int
7546ins_start_select(c)
7547 int c;
7548{
7549 if (km_startsel)
7550 switch (c)
7551 {
7552 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 case K_PAGEUP:
7555 case K_KPAGEUP:
7556 case K_PAGEDOWN:
7557 case K_KPAGEDOWN:
7558# ifdef MACOS
7559 case K_LEFT:
7560 case K_RIGHT:
7561 case K_UP:
7562 case K_DOWN:
7563 case K_END:
7564 case K_HOME:
7565# endif
7566 if (!(mod_mask & MOD_MASK_SHIFT))
7567 break;
7568 /* FALLTHROUGH */
7569 case K_S_LEFT:
7570 case K_S_RIGHT:
7571 case K_S_UP:
7572 case K_S_DOWN:
7573 case K_S_END:
7574 case K_S_HOME:
7575 /* Start selection right away, the cursor can move with
7576 * CTRL-O when beyond the end of the line. */
7577 start_selection();
7578
7579 /* Execute the key in (insert) Select mode. */
7580 stuffcharReadbuff(Ctrl_O);
7581 if (mod_mask)
7582 {
7583 char_u buf[4];
7584
7585 buf[0] = K_SPECIAL;
7586 buf[1] = KS_MODIFIER;
7587 buf[2] = mod_mask;
7588 buf[3] = NUL;
7589 stuffReadbuff(buf);
7590 }
7591 stuffcharReadbuff(c);
7592 return TRUE;
7593 }
7594 return FALSE;
7595}
7596#endif
7597
7598/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007599 * <Insert> key in Insert mode: toggle insert/remplace mode.
7600 */
7601 static void
7602ins_insert(replaceState)
7603 int replaceState;
7604{
7605#ifdef FEAT_FKMAP
7606 if (p_fkmap && p_ri)
7607 {
7608 beep_flush();
7609 EMSG(farsi_text_3); /* encoded in Farsi */
7610 return;
7611 }
7612#endif
7613
7614#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00007615# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007616 set_vim_var_string(VV_INSERTMODE,
7617 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007618# ifdef FEAT_VREPLACE
7619 replaceState == VREPLACE ? "v" :
7620# endif
7621 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00007622# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007623 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
7624#endif
7625 if (State & REPLACE_FLAG)
7626 State = INSERT | (State & LANGMAP);
7627 else
7628 State = replaceState | (State & LANGMAP);
7629 AppendCharToRedobuff(K_INS);
7630 showmode();
7631#ifdef CURSOR_SHAPE
7632 ui_cursor_shape(); /* may show different cursor shape */
7633#endif
7634}
7635
7636/*
7637 * Pressed CTRL-O in Insert mode.
7638 */
7639 static void
7640ins_ctrl_o()
7641{
7642#ifdef FEAT_VREPLACE
7643 if (State & VREPLACE_FLAG)
7644 restart_edit = 'V';
7645 else
7646#endif
7647 if (State & REPLACE_FLAG)
7648 restart_edit = 'R';
7649 else
7650 restart_edit = 'I';
7651#ifdef FEAT_VIRTUALEDIT
7652 if (virtual_active())
7653 ins_at_eol = FALSE; /* cursor always keeps its column */
7654 else
7655#endif
7656 ins_at_eol = (gchar_cursor() == NUL);
7657}
7658
7659/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 * If the cursor is on an indent, ^T/^D insert/delete one
7661 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
7662 * Always round the indent to 'shiftwith', this is compatible
7663 * with vi. But vi only supports ^T and ^D after an
7664 * autoindent, we support it everywhere.
7665 */
7666 static void
7667ins_shift(c, lastc)
7668 int c;
7669 int lastc;
7670{
7671 if (stop_arrow() == FAIL)
7672 return;
7673 AppendCharToRedobuff(c);
7674
7675 /*
7676 * 0^D and ^^D: remove all indent.
7677 */
7678 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
7679 {
7680 --curwin->w_cursor.col;
7681 (void)del_char(FALSE); /* delete the '^' or '0' */
7682 /* In Replace mode, restore the characters that '^' or '0' replaced. */
7683 if (State & REPLACE_FLAG)
7684 replace_pop_ins();
7685 if (lastc == '^')
7686 old_indent = get_indent(); /* remember curr. indent */
7687 change_indent(INDENT_SET, 0, TRUE, 0);
7688 }
7689 else
7690 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
7691
7692 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
7693 did_ai = FALSE;
7694#ifdef FEAT_SMARTINDENT
7695 did_si = FALSE;
7696 can_si = FALSE;
7697 can_si_back = FALSE;
7698#endif
7699#ifdef FEAT_CINDENT
7700 can_cindent = FALSE; /* no cindenting after ^D or ^T */
7701#endif
7702}
7703
7704 static void
7705ins_del()
7706{
7707 int temp;
7708
7709 if (stop_arrow() == FAIL)
7710 return;
7711 if (gchar_cursor() == NUL) /* delete newline */
7712 {
7713 temp = curwin->w_cursor.col;
7714 if (!can_bs(BS_EOL) /* only if "eol" included */
7715 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
7716 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
7717 || do_join(FALSE) == FAIL)
7718 vim_beep();
7719 else
7720 curwin->w_cursor.col = temp;
7721 }
7722 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
7723 vim_beep();
7724 did_ai = FALSE;
7725#ifdef FEAT_SMARTINDENT
7726 did_si = FALSE;
7727 can_si = FALSE;
7728 can_si_back = FALSE;
7729#endif
7730 AppendCharToRedobuff(K_DEL);
7731}
7732
7733/*
7734 * Handle Backspace, delete-word and delete-line in Insert mode.
7735 * Return TRUE when backspace was actually used.
7736 */
7737 static int
7738ins_bs(c, mode, inserted_space_p)
7739 int c;
7740 int mode;
7741 int *inserted_space_p;
7742{
7743 linenr_T lnum;
7744 int cc;
7745 int temp = 0; /* init for GCC */
7746 colnr_T mincol;
7747 int did_backspace = FALSE;
7748 int in_indent;
7749 int oldState;
7750#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007751 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752#endif
7753
7754 /*
7755 * can't delete anything in an empty file
7756 * can't backup past first character in buffer
7757 * can't backup past starting point unless 'backspace' > 1
7758 * can backup to a previous line if 'backspace' == 0
7759 */
7760 if ( bufempty()
7761 || (
7762#ifdef FEAT_RIGHTLEFT
7763 !revins_on &&
7764#endif
7765 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
7766 || (!can_bs(BS_START)
7767 && (arrow_used
7768 || (curwin->w_cursor.lnum == Insstart.lnum
7769 && curwin->w_cursor.col <= Insstart.col)))
7770 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
7771 && curwin->w_cursor.col <= ai_col)
7772 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
7773 {
7774 vim_beep();
7775 return FALSE;
7776 }
7777
7778 if (stop_arrow() == FAIL)
7779 return FALSE;
7780 in_indent = inindent(0);
7781#ifdef FEAT_CINDENT
7782 if (in_indent)
7783 can_cindent = FALSE;
7784#endif
7785#ifdef FEAT_COMMENTS
7786 end_comment_pending = NUL; /* After BS, don't auto-end comment */
7787#endif
7788#ifdef FEAT_RIGHTLEFT
7789 if (revins_on) /* put cursor after last inserted char */
7790 inc_cursor();
7791#endif
7792
7793#ifdef FEAT_VIRTUALEDIT
7794 /* Virtualedit:
7795 * BACKSPACE_CHAR eats a virtual space
7796 * BACKSPACE_WORD eats all coladd
7797 * BACKSPACE_LINE eats all coladd and keeps going
7798 */
7799 if (curwin->w_cursor.coladd > 0)
7800 {
7801 if (mode == BACKSPACE_CHAR)
7802 {
7803 --curwin->w_cursor.coladd;
7804 return TRUE;
7805 }
7806 if (mode == BACKSPACE_WORD)
7807 {
7808 curwin->w_cursor.coladd = 0;
7809 return TRUE;
7810 }
7811 curwin->w_cursor.coladd = 0;
7812 }
7813#endif
7814
7815 /*
7816 * delete newline!
7817 */
7818 if (curwin->w_cursor.col == 0)
7819 {
7820 lnum = Insstart.lnum;
7821 if (curwin->w_cursor.lnum == Insstart.lnum
7822#ifdef FEAT_RIGHTLEFT
7823 || revins_on
7824#endif
7825 )
7826 {
7827 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
7828 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
7829 return FALSE;
7830 --Insstart.lnum;
7831 Insstart.col = MAXCOL;
7832 }
7833 /*
7834 * In replace mode:
7835 * cc < 0: NL was inserted, delete it
7836 * cc >= 0: NL was replaced, put original characters back
7837 */
7838 cc = -1;
7839 if (State & REPLACE_FLAG)
7840 cc = replace_pop(); /* returns -1 if NL was inserted */
7841 /*
7842 * In replace mode, in the line we started replacing, we only move the
7843 * cursor.
7844 */
7845 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
7846 {
7847 dec_cursor();
7848 }
7849 else
7850 {
7851#ifdef FEAT_VREPLACE
7852 if (!(State & VREPLACE_FLAG)
7853 || curwin->w_cursor.lnum > orig_line_count)
7854#endif
7855 {
7856 temp = gchar_cursor(); /* remember current char */
7857 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00007858
7859 /* When "aw" is in 'formatoptions' we must delete the space at
7860 * the end of the line, otherwise the line will be broken
7861 * again when auto-formatting. */
7862 if (has_format_option(FO_AUTO)
7863 && has_format_option(FO_WHITE_PAR))
7864 {
7865 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
7866 TRUE);
7867 int len;
7868
7869 len = STRLEN(ptr);
7870 if (len > 0 && ptr[len - 1] == ' ')
7871 ptr[len - 1] = NUL;
7872 }
7873
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 (void)do_join(FALSE);
7875 if (temp == NUL && gchar_cursor() != NUL)
7876 inc_cursor();
7877 }
7878#ifdef FEAT_VREPLACE
7879 else
7880 dec_cursor();
7881#endif
7882
7883 /*
7884 * In REPLACE mode we have to put back the text that was replaced
7885 * by the NL. On the replace stack is first a NUL-terminated
7886 * sequence of characters that were deleted and then the
7887 * characters that NL replaced.
7888 */
7889 if (State & REPLACE_FLAG)
7890 {
7891 /*
7892 * Do the next ins_char() in NORMAL state, to
7893 * prevent ins_char() from replacing characters and
7894 * avoiding showmatch().
7895 */
7896 oldState = State;
7897 State = NORMAL;
7898 /*
7899 * restore characters (blanks) deleted after cursor
7900 */
7901 while (cc > 0)
7902 {
7903 temp = curwin->w_cursor.col;
7904#ifdef FEAT_MBYTE
7905 mb_replace_pop_ins(cc);
7906#else
7907 ins_char(cc);
7908#endif
7909 curwin->w_cursor.col = temp;
7910 cc = replace_pop();
7911 }
7912 /* restore the characters that NL replaced */
7913 replace_pop_ins();
7914 State = oldState;
7915 }
7916 }
7917 did_ai = FALSE;
7918 }
7919 else
7920 {
7921 /*
7922 * Delete character(s) before the cursor.
7923 */
7924#ifdef FEAT_RIGHTLEFT
7925 if (revins_on) /* put cursor on last inserted char */
7926 dec_cursor();
7927#endif
7928 mincol = 0;
7929 /* keep indent */
7930 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
7931#ifdef FEAT_RIGHTLEFT
7932 && !revins_on
7933#endif
7934 )
7935 {
7936 temp = curwin->w_cursor.col;
7937 beginline(BL_WHITE);
7938 if (curwin->w_cursor.col < (colnr_T)temp)
7939 mincol = curwin->w_cursor.col;
7940 curwin->w_cursor.col = temp;
7941 }
7942
7943 /*
7944 * Handle deleting one 'shiftwidth' or 'softtabstop'.
7945 */
7946 if ( mode == BACKSPACE_CHAR
7947 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00007948 || (curbuf->b_p_sts != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 && (*(ml_get_cursor() - 1) == TAB
7950 || (*(ml_get_cursor() - 1) == ' '
7951 && (!*inserted_space_p
7952 || arrow_used))))))
7953 {
7954 int ts;
7955 colnr_T vcol;
7956 colnr_T want_vcol;
7957 int extra = 0;
7958
7959 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00007960 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 ts = curbuf->b_p_sw;
7962 else
7963 ts = curbuf->b_p_sts;
7964 /* Compute the virtual column where we want to be. Since
7965 * 'showbreak' may get in the way, need to get the last column of
7966 * the previous character. */
7967 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7968 dec_cursor();
7969 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
7970 inc_cursor();
7971 want_vcol = (want_vcol / ts) * ts;
7972
7973 /* delete characters until we are at or before want_vcol */
7974 while (vcol > want_vcol
7975 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
7976 {
7977 dec_cursor();
7978 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7979 if (State & REPLACE_FLAG)
7980 {
7981 /* Don't delete characters before the insert point when in
7982 * Replace mode */
7983 if (curwin->w_cursor.lnum != Insstart.lnum
7984 || curwin->w_cursor.col >= Insstart.col)
7985 {
7986#if 0 /* what was this for? It causes problems when sw != ts. */
7987 if (State == REPLACE && (int)vcol < want_vcol)
7988 {
7989 (void)del_char(FALSE);
7990 extra = 2; /* don't pop too much */
7991 }
7992 else
7993#endif
7994 replace_do_bs();
7995 }
7996 }
7997 else
7998 (void)del_char(FALSE);
7999 }
8000
8001 /* insert extra spaces until we are at want_vcol */
8002 while (vcol < want_vcol)
8003 {
8004 /* Remember the first char we inserted */
8005 if (curwin->w_cursor.lnum == Insstart.lnum
8006 && curwin->w_cursor.col < Insstart.col)
8007 Insstart.col = curwin->w_cursor.col;
8008
8009#ifdef FEAT_VREPLACE
8010 if (State & VREPLACE_FLAG)
8011 ins_char(' ');
8012 else
8013#endif
8014 {
8015 ins_str((char_u *)" ");
8016 if ((State & REPLACE_FLAG) && extra <= 1)
8017 {
8018 if (extra)
8019 replace_push_off(NUL);
8020 else
8021 replace_push(NUL);
8022 }
8023 if (extra == 2)
8024 extra = 1;
8025 }
8026 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8027 }
8028 }
8029
8030 /*
8031 * Delete upto starting point, start of line or previous word.
8032 */
8033 else do
8034 {
8035#ifdef FEAT_RIGHTLEFT
8036 if (!revins_on) /* put cursor on char to be deleted */
8037#endif
8038 dec_cursor();
8039
8040 /* start of word? */
8041 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
8042 {
8043 mode = BACKSPACE_WORD_NOT_SPACE;
8044 temp = vim_iswordc(gchar_cursor());
8045 }
8046 /* end of word? */
8047 else if (mode == BACKSPACE_WORD_NOT_SPACE
8048 && (vim_isspace(cc = gchar_cursor())
8049 || vim_iswordc(cc) != temp))
8050 {
8051#ifdef FEAT_RIGHTLEFT
8052 if (!revins_on)
8053#endif
8054 inc_cursor();
8055#ifdef FEAT_RIGHTLEFT
8056 else if (State & REPLACE_FLAG)
8057 dec_cursor();
8058#endif
8059 break;
8060 }
8061 if (State & REPLACE_FLAG)
8062 replace_do_bs();
8063 else
8064 {
8065#ifdef FEAT_MBYTE
8066 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008067 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068#endif
8069 (void)del_char(FALSE);
8070#ifdef FEAT_MBYTE
8071 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008072 * If there are combining characters and 'delcombine' is set
8073 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 * character.
8075 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008076 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 inc_cursor();
8078#endif
8079#ifdef FEAT_RIGHTLEFT
8080 if (revins_chars)
8081 {
8082 revins_chars--;
8083 revins_legal++;
8084 }
8085 if (revins_on && gchar_cursor() == NUL)
8086 break;
8087#endif
8088 }
8089 /* Just a single backspace?: */
8090 if (mode == BACKSPACE_CHAR)
8091 break;
8092 } while (
8093#ifdef FEAT_RIGHTLEFT
8094 revins_on ||
8095#endif
8096 (curwin->w_cursor.col > mincol
8097 && (curwin->w_cursor.lnum != Insstart.lnum
8098 || curwin->w_cursor.col != Insstart.col)));
8099 did_backspace = TRUE;
8100 }
8101#ifdef FEAT_SMARTINDENT
8102 did_si = FALSE;
8103 can_si = FALSE;
8104 can_si_back = FALSE;
8105#endif
8106 if (curwin->w_cursor.col <= 1)
8107 did_ai = FALSE;
8108 /*
8109 * It's a little strange to put backspaces into the redo
8110 * buffer, but it makes auto-indent a lot easier to deal
8111 * with.
8112 */
8113 AppendCharToRedobuff(c);
8114
8115 /* If deleted before the insertion point, adjust it */
8116 if (curwin->w_cursor.lnum == Insstart.lnum
8117 && curwin->w_cursor.col < Insstart.col)
8118 Insstart.col = curwin->w_cursor.col;
8119
8120 /* vi behaviour: the cursor moves backward but the character that
8121 * was there remains visible
8122 * Vim behaviour: the cursor moves backward and the character that
8123 * was there is erased from the screen.
8124 * We can emulate the vi behaviour by pretending there is a dollar
8125 * displayed even when there isn't.
8126 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8127 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
8128 dollar_vcol = curwin->w_virtcol;
8129
8130 return did_backspace;
8131}
8132
8133#ifdef FEAT_MOUSE
8134 static void
8135ins_mouse(c)
8136 int c;
8137{
8138 pos_T tpos;
8139
8140# ifdef FEAT_GUI
8141 /* When GUI is active, also move/paste when 'mouse' is empty */
8142 if (!gui.in_use)
8143# endif
8144 if (!mouse_has(MOUSE_INSERT))
8145 return;
8146
8147 undisplay_dollar();
8148 tpos = curwin->w_cursor;
8149 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
8150 {
8151 start_arrow(&tpos);
8152# ifdef FEAT_CINDENT
8153 can_cindent = TRUE;
8154# endif
8155 }
8156
8157#ifdef FEAT_WINDOWS
8158 /* redraw status lines (in case another window became active) */
8159 redraw_statuslines();
8160#endif
8161}
8162
8163 static void
8164ins_mousescroll(up)
8165 int up;
8166{
8167 pos_T tpos;
8168# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8169 win_T *old_curwin;
8170# endif
8171
8172 tpos = curwin->w_cursor;
8173
8174# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8175 old_curwin = curwin;
8176
8177 /* Currently the mouse coordinates are only known in the GUI. */
8178 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
8179 {
8180 int row, col;
8181
8182 row = mouse_row;
8183 col = mouse_col;
8184
8185 /* find the window at the pointer coordinates */
8186 curwin = mouse_find_win(&row, &col);
8187 curbuf = curwin->w_buffer;
8188 }
8189 if (curwin == old_curwin)
8190# endif
8191 undisplay_dollar();
8192
8193 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8194 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
8195 else
8196 scroll_redraw(up, 3L);
8197
8198# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8199 curwin->w_redr_status = TRUE;
8200
8201 curwin = old_curwin;
8202 curbuf = curwin->w_buffer;
8203# endif
8204
8205 if (!equalpos(curwin->w_cursor, tpos))
8206 {
8207 start_arrow(&tpos);
8208# ifdef FEAT_CINDENT
8209 can_cindent = TRUE;
8210# endif
8211 }
8212}
8213#endif
8214
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008215#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00008216 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008217ins_tabline(c)
8218 int c;
8219{
8220 /* We will be leaving the current window, unless closing another tab. */
8221 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
8222 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
8223 {
8224 undisplay_dollar();
8225 start_arrow(&curwin->w_cursor);
8226# ifdef FEAT_CINDENT
8227 can_cindent = TRUE;
8228# endif
8229 }
8230
8231 if (c == K_TABLINE)
8232 goto_tabpage(current_tab);
8233 else
8234 handle_tabmenu();
8235
8236}
8237#endif
8238
8239#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 void
8241ins_scroll()
8242{
8243 pos_T tpos;
8244
8245 undisplay_dollar();
8246 tpos = curwin->w_cursor;
8247 if (gui_do_scroll())
8248 {
8249 start_arrow(&tpos);
8250# ifdef FEAT_CINDENT
8251 can_cindent = TRUE;
8252# endif
8253 }
8254}
8255
8256 void
8257ins_horscroll()
8258{
8259 pos_T tpos;
8260
8261 undisplay_dollar();
8262 tpos = curwin->w_cursor;
8263 if (gui_do_horiz_scroll())
8264 {
8265 start_arrow(&tpos);
8266# ifdef FEAT_CINDENT
8267 can_cindent = TRUE;
8268# endif
8269 }
8270}
8271#endif
8272
8273 static void
8274ins_left()
8275{
8276 pos_T tpos;
8277
8278#ifdef FEAT_FOLDING
8279 if ((fdo_flags & FDO_HOR) && KeyTyped)
8280 foldOpenCursor();
8281#endif
8282 undisplay_dollar();
8283 tpos = curwin->w_cursor;
8284 if (oneleft() == OK)
8285 {
8286 start_arrow(&tpos);
8287#ifdef FEAT_RIGHTLEFT
8288 /* If exit reversed string, position is fixed */
8289 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
8290 revins_legal++;
8291 revins_chars++;
8292#endif
8293 }
8294
8295 /*
8296 * if 'whichwrap' set for cursor in insert mode may go to
8297 * previous line
8298 */
8299 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
8300 {
8301 start_arrow(&tpos);
8302 --(curwin->w_cursor.lnum);
8303 coladvance((colnr_T)MAXCOL);
8304 curwin->w_set_curswant = TRUE; /* so we stay at the end */
8305 }
8306 else
8307 vim_beep();
8308}
8309
8310 static void
8311ins_home(c)
8312 int c;
8313{
8314 pos_T tpos;
8315
8316#ifdef FEAT_FOLDING
8317 if ((fdo_flags & FDO_HOR) && KeyTyped)
8318 foldOpenCursor();
8319#endif
8320 undisplay_dollar();
8321 tpos = curwin->w_cursor;
8322 if (c == K_C_HOME)
8323 curwin->w_cursor.lnum = 1;
8324 curwin->w_cursor.col = 0;
8325#ifdef FEAT_VIRTUALEDIT
8326 curwin->w_cursor.coladd = 0;
8327#endif
8328 curwin->w_curswant = 0;
8329 start_arrow(&tpos);
8330}
8331
8332 static void
8333ins_end(c)
8334 int c;
8335{
8336 pos_T tpos;
8337
8338#ifdef FEAT_FOLDING
8339 if ((fdo_flags & FDO_HOR) && KeyTyped)
8340 foldOpenCursor();
8341#endif
8342 undisplay_dollar();
8343 tpos = curwin->w_cursor;
8344 if (c == K_C_END)
8345 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
8346 coladvance((colnr_T)MAXCOL);
8347 curwin->w_curswant = MAXCOL;
8348
8349 start_arrow(&tpos);
8350}
8351
8352 static void
8353ins_s_left()
8354{
8355#ifdef FEAT_FOLDING
8356 if ((fdo_flags & FDO_HOR) && KeyTyped)
8357 foldOpenCursor();
8358#endif
8359 undisplay_dollar();
8360 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
8361 {
8362 start_arrow(&curwin->w_cursor);
8363 (void)bck_word(1L, FALSE, FALSE);
8364 curwin->w_set_curswant = TRUE;
8365 }
8366 else
8367 vim_beep();
8368}
8369
8370 static void
8371ins_right()
8372{
8373#ifdef FEAT_FOLDING
8374 if ((fdo_flags & FDO_HOR) && KeyTyped)
8375 foldOpenCursor();
8376#endif
8377 undisplay_dollar();
8378 if (gchar_cursor() != NUL || virtual_active()
8379 )
8380 {
8381 start_arrow(&curwin->w_cursor);
8382 curwin->w_set_curswant = TRUE;
8383#ifdef FEAT_VIRTUALEDIT
8384 if (virtual_active())
8385 oneright();
8386 else
8387#endif
8388 {
8389#ifdef FEAT_MBYTE
8390 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008391 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 else
8393#endif
8394 ++curwin->w_cursor.col;
8395 }
8396
8397#ifdef FEAT_RIGHTLEFT
8398 revins_legal++;
8399 if (revins_chars)
8400 revins_chars--;
8401#endif
8402 }
8403 /* if 'whichwrap' set for cursor in insert mode, may move the
8404 * cursor to the next line */
8405 else if (vim_strchr(p_ww, ']') != NULL
8406 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
8407 {
8408 start_arrow(&curwin->w_cursor);
8409 curwin->w_set_curswant = TRUE;
8410 ++curwin->w_cursor.lnum;
8411 curwin->w_cursor.col = 0;
8412 }
8413 else
8414 vim_beep();
8415}
8416
8417 static void
8418ins_s_right()
8419{
8420#ifdef FEAT_FOLDING
8421 if ((fdo_flags & FDO_HOR) && KeyTyped)
8422 foldOpenCursor();
8423#endif
8424 undisplay_dollar();
8425 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
8426 || gchar_cursor() != NUL)
8427 {
8428 start_arrow(&curwin->w_cursor);
8429 (void)fwd_word(1L, FALSE, 0);
8430 curwin->w_set_curswant = TRUE;
8431 }
8432 else
8433 vim_beep();
8434}
8435
8436 static void
8437ins_up(startcol)
8438 int startcol; /* when TRUE move to Insstart.col */
8439{
8440 pos_T tpos;
8441 linenr_T old_topline = curwin->w_topline;
8442#ifdef FEAT_DIFF
8443 int old_topfill = curwin->w_topfill;
8444#endif
8445
8446 undisplay_dollar();
8447 tpos = curwin->w_cursor;
8448 if (cursor_up(1L, TRUE) == OK)
8449 {
8450 if (startcol)
8451 coladvance(getvcol_nolist(&Insstart));
8452 if (old_topline != curwin->w_topline
8453#ifdef FEAT_DIFF
8454 || old_topfill != curwin->w_topfill
8455#endif
8456 )
8457 redraw_later(VALID);
8458 start_arrow(&tpos);
8459#ifdef FEAT_CINDENT
8460 can_cindent = TRUE;
8461#endif
8462 }
8463 else
8464 vim_beep();
8465}
8466
8467 static void
8468ins_pageup()
8469{
8470 pos_T tpos;
8471
8472 undisplay_dollar();
8473 tpos = curwin->w_cursor;
8474 if (onepage(BACKWARD, 1L) == OK)
8475 {
8476 start_arrow(&tpos);
8477#ifdef FEAT_CINDENT
8478 can_cindent = TRUE;
8479#endif
8480 }
8481 else
8482 vim_beep();
8483}
8484
8485 static void
8486ins_down(startcol)
8487 int startcol; /* when TRUE move to Insstart.col */
8488{
8489 pos_T tpos;
8490 linenr_T old_topline = curwin->w_topline;
8491#ifdef FEAT_DIFF
8492 int old_topfill = curwin->w_topfill;
8493#endif
8494
8495 undisplay_dollar();
8496 tpos = curwin->w_cursor;
8497 if (cursor_down(1L, TRUE) == OK)
8498 {
8499 if (startcol)
8500 coladvance(getvcol_nolist(&Insstart));
8501 if (old_topline != curwin->w_topline
8502#ifdef FEAT_DIFF
8503 || old_topfill != curwin->w_topfill
8504#endif
8505 )
8506 redraw_later(VALID);
8507 start_arrow(&tpos);
8508#ifdef FEAT_CINDENT
8509 can_cindent = TRUE;
8510#endif
8511 }
8512 else
8513 vim_beep();
8514}
8515
8516 static void
8517ins_pagedown()
8518{
8519 pos_T tpos;
8520
8521 undisplay_dollar();
8522 tpos = curwin->w_cursor;
8523 if (onepage(FORWARD, 1L) == OK)
8524 {
8525 start_arrow(&tpos);
8526#ifdef FEAT_CINDENT
8527 can_cindent = TRUE;
8528#endif
8529 }
8530 else
8531 vim_beep();
8532}
8533
8534#ifdef FEAT_DND
8535 static void
8536ins_drop()
8537{
8538 do_put('~', BACKWARD, 1L, PUT_CURSEND);
8539}
8540#endif
8541
8542/*
8543 * Handle TAB in Insert or Replace mode.
8544 * Return TRUE when the TAB needs to be inserted like a normal character.
8545 */
8546 static int
8547ins_tab()
8548{
8549 int ind;
8550 int i;
8551 int temp;
8552
8553 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
8554 Insstart_blank_vcol = get_nolist_virtcol();
8555 if (echeck_abbr(TAB + ABBR_OFF))
8556 return FALSE;
8557
8558 ind = inindent(0);
8559#ifdef FEAT_CINDENT
8560 if (ind)
8561 can_cindent = FALSE;
8562#endif
8563
8564 /*
8565 * When nothing special, insert TAB like a normal character
8566 */
8567 if (!curbuf->b_p_et
8568 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
8569 && curbuf->b_p_sts == 0)
8570 return TRUE;
8571
8572 if (stop_arrow() == FAIL)
8573 return TRUE;
8574
8575 did_ai = FALSE;
8576#ifdef FEAT_SMARTINDENT
8577 did_si = FALSE;
8578 can_si = FALSE;
8579 can_si_back = FALSE;
8580#endif
8581 AppendToRedobuff((char_u *)"\t");
8582
8583 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
8584 temp = (int)curbuf->b_p_sw;
8585 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
8586 temp = (int)curbuf->b_p_sts;
8587 else /* otherwise use 'tabstop' */
8588 temp = (int)curbuf->b_p_ts;
8589 temp -= get_nolist_virtcol() % temp;
8590
8591 /*
8592 * Insert the first space with ins_char(). It will delete one char in
8593 * replace mode. Insert the rest with ins_str(); it will not delete any
8594 * chars. For VREPLACE mode, we use ins_char() for all characters.
8595 */
8596 ins_char(' ');
8597 while (--temp > 0)
8598 {
8599#ifdef FEAT_VREPLACE
8600 if (State & VREPLACE_FLAG)
8601 ins_char(' ');
8602 else
8603#endif
8604 {
8605 ins_str((char_u *)" ");
8606 if (State & REPLACE_FLAG) /* no char replaced */
8607 replace_push(NUL);
8608 }
8609 }
8610
8611 /*
8612 * When 'expandtab' not set: Replace spaces by TABs where possible.
8613 */
8614 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
8615 {
8616 char_u *ptr;
8617#ifdef FEAT_VREPLACE
8618 char_u *saved_line = NULL; /* init for GCC */
8619 pos_T pos;
8620#endif
8621 pos_T fpos;
8622 pos_T *cursor;
8623 colnr_T want_vcol, vcol;
8624 int change_col = -1;
8625 int save_list = curwin->w_p_list;
8626
8627 /*
8628 * Get the current line. For VREPLACE mode, don't make real changes
8629 * yet, just work on a copy of the line.
8630 */
8631#ifdef FEAT_VREPLACE
8632 if (State & VREPLACE_FLAG)
8633 {
8634 pos = curwin->w_cursor;
8635 cursor = &pos;
8636 saved_line = vim_strsave(ml_get_curline());
8637 if (saved_line == NULL)
8638 return FALSE;
8639 ptr = saved_line + pos.col;
8640 }
8641 else
8642#endif
8643 {
8644 ptr = ml_get_cursor();
8645 cursor = &curwin->w_cursor;
8646 }
8647
8648 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
8649 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8650 curwin->w_p_list = FALSE;
8651
8652 /* Find first white before the cursor */
8653 fpos = curwin->w_cursor;
8654 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
8655 {
8656 --fpos.col;
8657 --ptr;
8658 }
8659
8660 /* In Replace mode, don't change characters before the insert point. */
8661 if ((State & REPLACE_FLAG)
8662 && fpos.lnum == Insstart.lnum
8663 && fpos.col < Insstart.col)
8664 {
8665 ptr += Insstart.col - fpos.col;
8666 fpos.col = Insstart.col;
8667 }
8668
8669 /* compute virtual column numbers of first white and cursor */
8670 getvcol(curwin, &fpos, &vcol, NULL, NULL);
8671 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
8672
8673 /* Use as many TABs as possible. Beware of 'showbreak' and
8674 * 'linebreak' adding extra virtual columns. */
8675 while (vim_iswhite(*ptr))
8676 {
8677 i = lbr_chartabsize((char_u *)"\t", vcol);
8678 if (vcol + i > want_vcol)
8679 break;
8680 if (*ptr != TAB)
8681 {
8682 *ptr = TAB;
8683 if (change_col < 0)
8684 {
8685 change_col = fpos.col; /* Column of first change */
8686 /* May have to adjust Insstart */
8687 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
8688 Insstart.col = fpos.col;
8689 }
8690 }
8691 ++fpos.col;
8692 ++ptr;
8693 vcol += i;
8694 }
8695
8696 if (change_col >= 0)
8697 {
8698 int repl_off = 0;
8699
8700 /* Skip over the spaces we need. */
8701 while (vcol < want_vcol && *ptr == ' ')
8702 {
8703 vcol += lbr_chartabsize(ptr, vcol);
8704 ++ptr;
8705 ++repl_off;
8706 }
8707 if (vcol > want_vcol)
8708 {
8709 /* Must have a char with 'showbreak' just before it. */
8710 --ptr;
8711 --repl_off;
8712 }
8713 fpos.col += repl_off;
8714
8715 /* Delete following spaces. */
8716 i = cursor->col - fpos.col;
8717 if (i > 0)
8718 {
8719 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
8720 /* correct replace stack. */
8721 if ((State & REPLACE_FLAG)
8722#ifdef FEAT_VREPLACE
8723 && !(State & VREPLACE_FLAG)
8724#endif
8725 )
8726 for (temp = i; --temp >= 0; )
8727 replace_join(repl_off);
8728 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00008729#ifdef FEAT_NETBEANS_INTG
8730 if (usingNetbeans)
8731 {
8732 netbeans_removed(curbuf, fpos.lnum, cursor->col,
8733 (long)(i + 1));
8734 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
8735 (char_u *)"\t", 1);
8736 }
8737#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 cursor->col -= i;
8739
8740#ifdef FEAT_VREPLACE
8741 /*
8742 * In VREPLACE mode, we haven't changed anything yet. Do it now by
8743 * backspacing over the changed spacing and then inserting the new
8744 * spacing.
8745 */
8746 if (State & VREPLACE_FLAG)
8747 {
8748 /* Backspace from real cursor to change_col */
8749 backspace_until_column(change_col);
8750
8751 /* Insert each char in saved_line from changed_col to
8752 * ptr-cursor */
8753 ins_bytes_len(saved_line + change_col,
8754 cursor->col - change_col);
8755 }
8756#endif
8757 }
8758
8759#ifdef FEAT_VREPLACE
8760 if (State & VREPLACE_FLAG)
8761 vim_free(saved_line);
8762#endif
8763 curwin->w_p_list = save_list;
8764 }
8765
8766 return FALSE;
8767}
8768
8769/*
8770 * Handle CR or NL in insert mode.
8771 * Return TRUE when out of memory or can't undo.
8772 */
8773 static int
8774ins_eol(c)
8775 int c;
8776{
8777 int i;
8778
8779 if (echeck_abbr(c + ABBR_OFF))
8780 return FALSE;
8781 if (stop_arrow() == FAIL)
8782 return TRUE;
8783 undisplay_dollar();
8784
8785 /*
8786 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
8787 * character under the cursor. Only push a NUL on the replace stack,
8788 * nothing to put back when the NL is deleted.
8789 */
8790 if ((State & REPLACE_FLAG)
8791#ifdef FEAT_VREPLACE
8792 && !(State & VREPLACE_FLAG)
8793#endif
8794 )
8795 replace_push(NUL);
8796
8797 /*
8798 * In VREPLACE mode, a NL replaces the rest of the line, and starts
8799 * replacing the next line, so we push all of the characters left on the
8800 * line onto the replace stack. This is not done here though, it is done
8801 * in open_line().
8802 */
8803
8804#ifdef FEAT_RIGHTLEFT
8805# ifdef FEAT_FKMAP
8806 if (p_altkeymap && p_fkmap)
8807 fkmap(NL);
8808# endif
8809 /* NL in reverse insert will always start in the end of
8810 * current line. */
8811 if (revins_on)
8812 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8813#endif
8814
8815 AppendToRedobuff(NL_STR);
8816 i = open_line(FORWARD,
8817#ifdef FEAT_COMMENTS
8818 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
8819#endif
8820 0, old_indent);
8821 old_indent = 0;
8822#ifdef FEAT_CINDENT
8823 can_cindent = TRUE;
8824#endif
8825
8826 return (!i);
8827}
8828
8829#ifdef FEAT_DIGRAPHS
8830/*
8831 * Handle digraph in insert mode.
8832 * Returns character still to be inserted, or NUL when nothing remaining to be
8833 * done.
8834 */
8835 static int
8836ins_digraph()
8837{
8838 int c;
8839 int cc;
8840
8841 pc_status = PC_STATUS_UNSET;
8842 if (redrawing() && !char_avail())
8843 {
8844 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008845 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846
8847 edit_putchar('?', TRUE);
8848#ifdef FEAT_CMDL_INFO
8849 add_to_showcmd_c(Ctrl_K);
8850#endif
8851 }
8852
8853#ifdef USE_ON_FLY_SCROLL
8854 dont_scroll = TRUE; /* disallow scrolling here */
8855#endif
8856
8857 /* don't map the digraph chars. This also prevents the
8858 * mode message to be deleted when ESC is hit */
8859 ++no_mapping;
8860 ++allow_keys;
8861 c = safe_vgetc();
8862 --no_mapping;
8863 --allow_keys;
8864 if (IS_SPECIAL(c) || mod_mask) /* special key */
8865 {
8866#ifdef FEAT_CMDL_INFO
8867 clear_showcmd();
8868#endif
8869 insert_special(c, TRUE, FALSE);
8870 return NUL;
8871 }
8872 if (c != ESC)
8873 {
8874 if (redrawing() && !char_avail())
8875 {
8876 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008877 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878
8879 if (char2cells(c) == 1)
8880 {
8881 /* first remove the '?', otherwise it's restored when typing
8882 * an ESC next */
8883 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00008884 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 edit_putchar(c, TRUE);
8886 }
8887#ifdef FEAT_CMDL_INFO
8888 add_to_showcmd_c(c);
8889#endif
8890 }
8891 ++no_mapping;
8892 ++allow_keys;
8893 cc = safe_vgetc();
8894 --no_mapping;
8895 --allow_keys;
8896 if (cc != ESC)
8897 {
8898 AppendToRedobuff((char_u *)CTRL_V_STR);
8899 c = getdigraph(c, cc, TRUE);
8900#ifdef FEAT_CMDL_INFO
8901 clear_showcmd();
8902#endif
8903 return c;
8904 }
8905 }
8906 edit_unputchar();
8907#ifdef FEAT_CMDL_INFO
8908 clear_showcmd();
8909#endif
8910 return NUL;
8911}
8912#endif
8913
8914/*
8915 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
8916 * Returns the char to be inserted, or NUL if none found.
8917 */
8918 static int
8919ins_copychar(lnum)
8920 linenr_T lnum;
8921{
8922 int c;
8923 int temp;
8924 char_u *ptr, *prev_ptr;
8925
8926 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8927 {
8928 vim_beep();
8929 return NUL;
8930 }
8931
8932 /* try to advance to the cursor column */
8933 temp = 0;
8934 ptr = ml_get(lnum);
8935 prev_ptr = ptr;
8936 validate_virtcol();
8937 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
8938 {
8939 prev_ptr = ptr;
8940 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
8941 }
8942 if ((colnr_T)temp > curwin->w_virtcol)
8943 ptr = prev_ptr;
8944
8945#ifdef FEAT_MBYTE
8946 c = (*mb_ptr2char)(ptr);
8947#else
8948 c = *ptr;
8949#endif
8950 if (c == NUL)
8951 vim_beep();
8952 return c;
8953}
8954
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008955/*
8956 * CTRL-Y or CTRL-E typed in Insert mode.
8957 */
8958 static int
8959ins_ctrl_ey(tc)
8960 int tc;
8961{
8962 int c = tc;
8963
8964#ifdef FEAT_INS_EXPAND
8965 if (ctrl_x_mode == CTRL_X_SCROLL)
8966 {
8967 if (c == Ctrl_Y)
8968 scrolldown_clamp();
8969 else
8970 scrollup_clamp();
8971 redraw_later(VALID);
8972 }
8973 else
8974#endif
8975 {
8976 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
8977 if (c != NUL)
8978 {
8979 long tw_save;
8980
8981 /* The character must be taken literally, insert like it
8982 * was typed after a CTRL-V, and pretend 'textwidth'
8983 * wasn't set. Digits, 'o' and 'x' are special after a
8984 * CTRL-V, don't use it for these. */
8985 if (c < 256 && !isalnum(c))
8986 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
8987 tw_save = curbuf->b_p_tw;
8988 curbuf->b_p_tw = -1;
8989 insert_special(c, TRUE, FALSE);
8990 curbuf->b_p_tw = tw_save;
8991#ifdef FEAT_RIGHTLEFT
8992 revins_chars++;
8993 revins_legal++;
8994#endif
8995 c = Ctrl_V; /* pretend CTRL-V is last character */
8996 auto_format(FALSE, TRUE);
8997 }
8998 }
8999 return c;
9000}
9001
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002#ifdef FEAT_SMARTINDENT
9003/*
9004 * Try to do some very smart auto-indenting.
9005 * Used when inserting a "normal" character.
9006 */
9007 static void
9008ins_try_si(c)
9009 int c;
9010{
9011 pos_T *pos, old_pos;
9012 char_u *ptr;
9013 int i;
9014 int temp;
9015
9016 /*
9017 * do some very smart indenting when entering '{' or '}'
9018 */
9019 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
9020 {
9021 /*
9022 * for '}' set indent equal to indent of line containing matching '{'
9023 */
9024 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
9025 {
9026 old_pos = curwin->w_cursor;
9027 /*
9028 * If the matching '{' has a ')' immediately before it (ignoring
9029 * white-space), then line up with the start of the line
9030 * containing the matching '(' if there is one. This handles the
9031 * case where an "if (..\n..) {" statement continues over multiple
9032 * lines -- webb
9033 */
9034 ptr = ml_get(pos->lnum);
9035 i = pos->col;
9036 if (i > 0) /* skip blanks before '{' */
9037 while (--i > 0 && vim_iswhite(ptr[i]))
9038 ;
9039 curwin->w_cursor.lnum = pos->lnum;
9040 curwin->w_cursor.col = i;
9041 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
9042 curwin->w_cursor = *pos;
9043 i = get_indent();
9044 curwin->w_cursor = old_pos;
9045#ifdef FEAT_VREPLACE
9046 if (State & VREPLACE_FLAG)
9047 change_indent(INDENT_SET, i, FALSE, NUL);
9048 else
9049#endif
9050 (void)set_indent(i, SIN_CHANGED);
9051 }
9052 else if (curwin->w_cursor.col > 0)
9053 {
9054 /*
9055 * when inserting '{' after "O" reduce indent, but not
9056 * more than indent of previous line
9057 */
9058 temp = TRUE;
9059 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
9060 {
9061 old_pos = curwin->w_cursor;
9062 i = get_indent();
9063 while (curwin->w_cursor.lnum > 1)
9064 {
9065 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
9066
9067 /* ignore empty lines and lines starting with '#'. */
9068 if (*ptr != '#' && *ptr != NUL)
9069 break;
9070 }
9071 if (get_indent() >= i)
9072 temp = FALSE;
9073 curwin->w_cursor = old_pos;
9074 }
9075 if (temp)
9076 shift_line(TRUE, FALSE, 1);
9077 }
9078 }
9079
9080 /*
9081 * set indent of '#' always to 0
9082 */
9083 if (curwin->w_cursor.col > 0 && can_si && c == '#')
9084 {
9085 /* remember current indent for next line */
9086 old_indent = get_indent();
9087 (void)set_indent(0, SIN_CHANGED);
9088 }
9089
9090 /* Adjust ai_col, the char at this position can be deleted. */
9091 if (ai_col > curwin->w_cursor.col)
9092 ai_col = curwin->w_cursor.col;
9093}
9094#endif
9095
9096/*
9097 * Get the value that w_virtcol would have when 'list' is off.
9098 * Unless 'cpo' contains the 'L' flag.
9099 */
9100 static colnr_T
9101get_nolist_virtcol()
9102{
9103 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9104 return getvcol_nolist(&curwin->w_cursor);
9105 validate_virtcol();
9106 return curwin->w_virtcol;
9107}