blob: 898f9235e1ee6e92768e1397c5d43cf5b17f6532 [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
729 /* Pressing Enter selects the current match. */
730 if (c == CAR || c == K_KENTER || c == NL)
731 {
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 Moolenaar5e3cb7e2006-02-27 23:58:35 +00001393 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001394 if (ready && has_cursormovedI()
1395 && !equalpos(last_cursormoved, curwin->w_cursor))
1396 {
1397 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1398 last_cursormoved = curwin->w_cursor;
1399 }
1400#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 if (must_redraw)
1402 update_screen(0);
1403 else if (clear_cmdline || redraw_cmdline)
1404 showmode(); /* clear cmdline and show mode */
1405 showruler(FALSE);
1406 setcursor();
1407 emsg_on_display = FALSE; /* may remove error message now */
1408 }
1409}
1410
1411/*
1412 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1413 */
1414 static void
1415ins_ctrl_v()
1416{
1417 int c;
1418
1419 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001420 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421
1422 if (redrawing() && !char_avail())
1423 edit_putchar('^', TRUE);
1424 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1425
1426#ifdef FEAT_CMDL_INFO
1427 add_to_showcmd_c(Ctrl_V);
1428#endif
1429
1430 c = get_literal();
1431#ifdef FEAT_CMDL_INFO
1432 clear_showcmd();
1433#endif
1434 insert_special(c, FALSE, TRUE);
1435#ifdef FEAT_RIGHTLEFT
1436 revins_chars++;
1437 revins_legal++;
1438#endif
1439}
1440
1441/*
1442 * Put a character directly onto the screen. It's not stored in a buffer.
1443 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1444 */
1445static int pc_status;
1446#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1447#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1448#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1449#define PC_STATUS_SET 3 /* pc_bytes was filled */
1450#ifdef FEAT_MBYTE
1451static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1452#else
1453static char_u pc_bytes[2]; /* saved bytes */
1454#endif
1455static int pc_attr;
1456static int pc_row;
1457static int pc_col;
1458
1459 void
1460edit_putchar(c, highlight)
1461 int c;
1462 int highlight;
1463{
1464 int attr;
1465
1466 if (ScreenLines != NULL)
1467 {
1468 update_topline(); /* just in case w_topline isn't valid */
1469 validate_cursor();
1470 if (highlight)
1471 attr = hl_attr(HLF_8);
1472 else
1473 attr = 0;
1474 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1475 pc_col = W_WINCOL(curwin);
1476#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1477 pc_status = PC_STATUS_UNSET;
1478#endif
1479#ifdef FEAT_RIGHTLEFT
1480 if (curwin->w_p_rl)
1481 {
1482 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1483# ifdef FEAT_MBYTE
1484 if (has_mbyte)
1485 {
1486 int fix_col = mb_fix_col(pc_col, pc_row);
1487
1488 if (fix_col != pc_col)
1489 {
1490 screen_putchar(' ', pc_row, fix_col, attr);
1491 --curwin->w_wcol;
1492 pc_status = PC_STATUS_RIGHT;
1493 }
1494 }
1495# endif
1496 }
1497 else
1498#endif
1499 {
1500 pc_col += curwin->w_wcol;
1501#ifdef FEAT_MBYTE
1502 if (mb_lefthalve(pc_row, pc_col))
1503 pc_status = PC_STATUS_LEFT;
1504#endif
1505 }
1506
1507 /* save the character to be able to put it back */
1508#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1509 if (pc_status == PC_STATUS_UNSET)
1510#endif
1511 {
1512 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1513 pc_status = PC_STATUS_SET;
1514 }
1515 screen_putchar(c, pc_row, pc_col, attr);
1516 }
1517}
1518
1519/*
1520 * Undo the previous edit_putchar().
1521 */
1522 void
1523edit_unputchar()
1524{
1525 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1526 {
1527#if defined(FEAT_MBYTE)
1528 if (pc_status == PC_STATUS_RIGHT)
1529 ++curwin->w_wcol;
1530 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1531 redrawWinline(curwin->w_cursor.lnum, FALSE);
1532 else
1533#endif
1534 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1535 }
1536}
1537
1538/*
1539 * Called when p_dollar is set: display a '$' at the end of the changed text
1540 * Only works when cursor is in the line that changes.
1541 */
1542 void
1543display_dollar(col)
1544 colnr_T col;
1545{
1546 colnr_T save_col;
1547
1548 if (!redrawing())
1549 return;
1550
1551 cursor_off();
1552 save_col = curwin->w_cursor.col;
1553 curwin->w_cursor.col = col;
1554#ifdef FEAT_MBYTE
1555 if (has_mbyte)
1556 {
1557 char_u *p;
1558
1559 /* If on the last byte of a multi-byte move to the first byte. */
1560 p = ml_get_curline();
1561 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1562 }
1563#endif
1564 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1565 if (curwin->w_wcol < W_WIDTH(curwin))
1566 {
1567 edit_putchar('$', FALSE);
1568 dollar_vcol = curwin->w_virtcol;
1569 }
1570 curwin->w_cursor.col = save_col;
1571}
1572
1573/*
1574 * Call this function before moving the cursor from the normal insert position
1575 * in insert mode.
1576 */
1577 static void
1578undisplay_dollar()
1579{
1580 if (dollar_vcol)
1581 {
1582 dollar_vcol = 0;
1583 redrawWinline(curwin->w_cursor.lnum, FALSE);
1584 }
1585}
1586
1587/*
1588 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1589 * Keep the cursor on the same character.
1590 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1591 * type == INDENT_DEC decrease indent (for CTRL-D)
1592 * type == INDENT_SET set indent to "amount"
1593 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1594 */
1595 void
1596change_indent(type, amount, round, replaced)
1597 int type;
1598 int amount;
1599 int round;
1600 int replaced; /* replaced character, put on replace stack */
1601{
1602 int vcol;
1603 int last_vcol;
1604 int insstart_less; /* reduction for Insstart.col */
1605 int new_cursor_col;
1606 int i;
1607 char_u *ptr;
1608 int save_p_list;
1609 int start_col;
1610 colnr_T vc;
1611#ifdef FEAT_VREPLACE
1612 colnr_T orig_col = 0; /* init for GCC */
1613 char_u *new_line, *orig_line = NULL; /* init for GCC */
1614
1615 /* VREPLACE mode needs to know what the line was like before changing */
1616 if (State & VREPLACE_FLAG)
1617 {
1618 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1619 orig_col = curwin->w_cursor.col;
1620 }
1621#endif
1622
1623 /* for the following tricks we don't want list mode */
1624 save_p_list = curwin->w_p_list;
1625 curwin->w_p_list = FALSE;
1626 vc = getvcol_nolist(&curwin->w_cursor);
1627 vcol = vc;
1628
1629 /*
1630 * For Replace mode we need to fix the replace stack later, which is only
1631 * possible when the cursor is in the indent. Remember the number of
1632 * characters before the cursor if it's possible.
1633 */
1634 start_col = curwin->w_cursor.col;
1635
1636 /* determine offset from first non-blank */
1637 new_cursor_col = curwin->w_cursor.col;
1638 beginline(BL_WHITE);
1639 new_cursor_col -= curwin->w_cursor.col;
1640
1641 insstart_less = curwin->w_cursor.col;
1642
1643 /*
1644 * If the cursor is in the indent, compute how many screen columns the
1645 * cursor is to the left of the first non-blank.
1646 */
1647 if (new_cursor_col < 0)
1648 vcol = get_indent() - vcol;
1649
1650 if (new_cursor_col > 0) /* can't fix replace stack */
1651 start_col = -1;
1652
1653 /*
1654 * Set the new indent. The cursor will be put on the first non-blank.
1655 */
1656 if (type == INDENT_SET)
1657 (void)set_indent(amount, SIN_CHANGED);
1658 else
1659 {
1660#ifdef FEAT_VREPLACE
1661 int save_State = State;
1662
1663 /* Avoid being called recursively. */
1664 if (State & VREPLACE_FLAG)
1665 State = INSERT;
1666#endif
1667 shift_line(type == INDENT_DEC, round, 1);
1668#ifdef FEAT_VREPLACE
1669 State = save_State;
1670#endif
1671 }
1672 insstart_less -= curwin->w_cursor.col;
1673
1674 /*
1675 * Try to put cursor on same character.
1676 * If the cursor is at or after the first non-blank in the line,
1677 * compute the cursor column relative to the column of the first
1678 * non-blank character.
1679 * If we are not in insert mode, leave the cursor on the first non-blank.
1680 * If the cursor is before the first non-blank, position it relative
1681 * to the first non-blank, counted in screen columns.
1682 */
1683 if (new_cursor_col >= 0)
1684 {
1685 /*
1686 * When changing the indent while the cursor is touching it, reset
1687 * Insstart_col to 0.
1688 */
1689 if (new_cursor_col == 0)
1690 insstart_less = MAXCOL;
1691 new_cursor_col += curwin->w_cursor.col;
1692 }
1693 else if (!(State & INSERT))
1694 new_cursor_col = curwin->w_cursor.col;
1695 else
1696 {
1697 /*
1698 * Compute the screen column where the cursor should be.
1699 */
1700 vcol = get_indent() - vcol;
1701 curwin->w_virtcol = (vcol < 0) ? 0 : vcol;
1702
1703 /*
1704 * Advance the cursor until we reach the right screen column.
1705 */
1706 vcol = last_vcol = 0;
1707 new_cursor_col = -1;
1708 ptr = ml_get_curline();
1709 while (vcol <= (int)curwin->w_virtcol)
1710 {
1711 last_vcol = vcol;
1712#ifdef FEAT_MBYTE
1713 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001714 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 else
1716#endif
1717 ++new_cursor_col;
1718 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1719 }
1720 vcol = last_vcol;
1721
1722 /*
1723 * May need to insert spaces to be able to position the cursor on
1724 * the right screen column.
1725 */
1726 if (vcol != (int)curwin->w_virtcol)
1727 {
1728 curwin->w_cursor.col = new_cursor_col;
1729 i = (int)curwin->w_virtcol - vcol;
1730 ptr = alloc(i + 1);
1731 if (ptr != NULL)
1732 {
1733 new_cursor_col += i;
1734 ptr[i] = NUL;
1735 while (--i >= 0)
1736 ptr[i] = ' ';
1737 ins_str(ptr);
1738 vim_free(ptr);
1739 }
1740 }
1741
1742 /*
1743 * When changing the indent while the cursor is in it, reset
1744 * Insstart_col to 0.
1745 */
1746 insstart_less = MAXCOL;
1747 }
1748
1749 curwin->w_p_list = save_p_list;
1750
1751 if (new_cursor_col <= 0)
1752 curwin->w_cursor.col = 0;
1753 else
1754 curwin->w_cursor.col = new_cursor_col;
1755 curwin->w_set_curswant = TRUE;
1756 changed_cline_bef_curs();
1757
1758 /*
1759 * May have to adjust the start of the insert.
1760 */
1761 if (State & INSERT)
1762 {
1763 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1764 {
1765 if ((int)Insstart.col <= insstart_less)
1766 Insstart.col = 0;
1767 else
1768 Insstart.col -= insstart_less;
1769 }
1770 if ((int)ai_col <= insstart_less)
1771 ai_col = 0;
1772 else
1773 ai_col -= insstart_less;
1774 }
1775
1776 /*
1777 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1778 * If the number of characters before the cursor decreased, need to pop a
1779 * few characters from the replace stack.
1780 * If the number of characters before the cursor increased, need to push a
1781 * few NULs onto the replace stack.
1782 */
1783 if (REPLACE_NORMAL(State) && start_col >= 0)
1784 {
1785 while (start_col > (int)curwin->w_cursor.col)
1786 {
1787 replace_join(0); /* remove a NUL from the replace stack */
1788 --start_col;
1789 }
1790 while (start_col < (int)curwin->w_cursor.col || replaced)
1791 {
1792 replace_push(NUL);
1793 if (replaced)
1794 {
1795 replace_push(replaced);
1796 replaced = NUL;
1797 }
1798 ++start_col;
1799 }
1800 }
1801
1802#ifdef FEAT_VREPLACE
1803 /*
1804 * For VREPLACE mode, we also have to fix the replace stack. In this case
1805 * it is always possible because we backspace over the whole line and then
1806 * put it back again the way we wanted it.
1807 */
1808 if (State & VREPLACE_FLAG)
1809 {
1810 /* If orig_line didn't allocate, just return. At least we did the job,
1811 * even if you can't backspace. */
1812 if (orig_line == NULL)
1813 return;
1814
1815 /* Save new line */
1816 new_line = vim_strsave(ml_get_curline());
1817 if (new_line == NULL)
1818 return;
1819
1820 /* We only put back the new line up to the cursor */
1821 new_line[curwin->w_cursor.col] = NUL;
1822
1823 /* Put back original line */
1824 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1825 curwin->w_cursor.col = orig_col;
1826
1827 /* Backspace from cursor to start of line */
1828 backspace_until_column(0);
1829
1830 /* Insert new stuff into line again */
1831 ins_bytes(new_line);
1832
1833 vim_free(new_line);
1834 }
1835#endif
1836}
1837
1838/*
1839 * Truncate the space at the end of a line. This is to be used only in an
1840 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1841 * modes.
1842 */
1843 void
1844truncate_spaces(line)
1845 char_u *line;
1846{
1847 int i;
1848
1849 /* find start of trailing white space */
1850 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1851 {
1852 if (State & REPLACE_FLAG)
1853 replace_join(0); /* remove a NUL from the replace stack */
1854 }
1855 line[i + 1] = NUL;
1856}
1857
1858#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1859 || defined(FEAT_COMMENTS) || defined(PROTO)
1860/*
1861 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1862 * modes correctly. May also be used when not in insert mode at all.
1863 */
1864 void
1865backspace_until_column(col)
1866 int col;
1867{
1868 while ((int)curwin->w_cursor.col > col)
1869 {
1870 curwin->w_cursor.col--;
1871 if (State & REPLACE_FLAG)
1872 replace_do_bs();
1873 else
1874 (void)del_char(FALSE);
1875 }
1876}
1877#endif
1878
1879#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1880/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001881 * CTRL-X pressed in Insert mode.
1882 */
1883 static void
1884ins_ctrl_x()
1885{
1886 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
1887 * CTRL-V works like CTRL-N */
1888 if (ctrl_x_mode != CTRL_X_CMDLINE)
1889 {
1890 /* if the next ^X<> won't ADD nothing, then reset
1891 * compl_cont_status */
1892 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001893 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001894 else
1895 compl_cont_status = 0;
1896 /* We're not sure which CTRL-X mode it will be yet */
1897 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
1898 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
1899 edit_submode_pre = NULL;
1900 showmode();
1901 }
1902}
1903
1904/*
1905 * Return TRUE if the 'dict' or 'tsr' option can be used.
1906 */
1907 static int
1908has_compl_option(dict_opt)
1909 int dict_opt;
1910{
Bram Moolenaar0b238792006-03-02 22:49:12 +00001911 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00001912# ifdef FEAT_SPELL
1913 && !curwin->w_p_spell
1914# endif
1915 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001916 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
1917 {
1918 ctrl_x_mode = 0;
1919 edit_submode = NULL;
1920 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
1921 : (char_u *)_("'thesaurus' option is empty"),
1922 hl_attr(HLF_E));
1923 if (emsg_silent == 0)
1924 {
1925 vim_beep();
1926 setcursor();
1927 out_flush();
1928 ui_delay(2000L, FALSE);
1929 }
1930 return FALSE;
1931 }
1932 return TRUE;
1933}
1934
1935/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
1937 * This depends on the current mode.
1938 */
1939 int
1940vim_is_ctrl_x_key(c)
1941 int c;
1942{
1943 /* Always allow ^R - let it's results then be checked */
1944 if (c == Ctrl_R)
1945 return TRUE;
1946
Bram Moolenaare3226be2005-12-18 22:10:00 +00001947 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001948 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00001949 return TRUE;
1950
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 switch (ctrl_x_mode)
1952 {
1953 case 0: /* Not in any CTRL-X mode */
1954 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
1955 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001956 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
1958 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
1959 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00001960 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
1961 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 case CTRL_X_SCROLL:
1963 return (c == Ctrl_Y || c == Ctrl_E);
1964 case CTRL_X_WHOLE_LINE:
1965 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
1966 case CTRL_X_FILES:
1967 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
1968 case CTRL_X_DICTIONARY:
1969 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
1970 case CTRL_X_THESAURUS:
1971 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
1972 case CTRL_X_TAGS:
1973 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
1974#ifdef FEAT_FIND_ID
1975 case CTRL_X_PATH_PATTERNS:
1976 return (c == Ctrl_P || c == Ctrl_N);
1977 case CTRL_X_PATH_DEFINES:
1978 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
1979#endif
1980 case CTRL_X_CMDLINE:
1981 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
1982 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001983#ifdef FEAT_COMPL_FUNC
1984 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001985 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001986 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001987 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001988#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00001989 case CTRL_X_SPELL:
1990 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 }
1992 EMSG(_(e_internal));
1993 return FALSE;
1994}
1995
1996/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001997 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 * case of the originally typed text is used, and the case of the completed
1999 * text is infered, ie this tries to work out what case you probably wanted
2000 * the rest of the word to be in -- webb
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002001 * TODO: make this work for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 */
2003 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002004ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 char_u *str;
2006 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002007 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 char_u *fname;
2009 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002010 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011{
2012 int has_lower = FALSE;
2013 int was_letter = FALSE;
2014 int idx;
2015
2016 if (p_ic && curbuf->b_p_inf && len < IOSIZE)
2017 {
2018 /* Infer case of completed part -- webb */
2019 /* Use IObuff, str would change text in buffer! */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002020 vim_strncpy(IObuff, str, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021
2022 /* Rule 1: Were any chars converted to lower? */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002023 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002025 if (islower(compl_orig_text[idx]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 {
2027 has_lower = TRUE;
2028 if (isupper(IObuff[idx]))
2029 {
2030 /* Rule 1 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002031 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 IObuff[idx] = TOLOWER_LOC(IObuff[idx]);
2033 break;
2034 }
2035 }
2036 }
2037
2038 /*
2039 * Rule 2: No lower case, 2nd consecutive letter converted to
2040 * upper case.
2041 */
2042 if (!has_lower)
2043 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002044 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002046 if (was_letter && isupper(compl_orig_text[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 && islower(IObuff[idx]))
2048 {
2049 /* Rule 2 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002050 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 IObuff[idx] = TOUPPER_LOC(IObuff[idx]);
2052 break;
2053 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002054 was_letter = isalpha(compl_orig_text[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 }
2056 }
2057
2058 /* Copy the original case of the part we typed */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002059 STRNCPY(IObuff, compl_orig_text, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060
Bram Moolenaar39f05632006-03-19 22:15:26 +00002061 return ins_compl_add(IObuff, len, icase, fname, NULL, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002063 return ins_compl_add(str, len, icase, fname, NULL, dir, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064}
2065
2066/*
2067 * Add a match to the list of matches.
2068 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002069 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002070 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002072 int
Bram Moolenaar39f05632006-03-19 22:15:26 +00002073ins_compl_add(str, len, icase, fname, cptext, cdir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 char_u *str;
2075 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002076 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 char_u *fname;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002078 char_u **cptext; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002079 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002080 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002082 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002083 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084
2085 ui_breakcheck();
2086 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002087 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 if (len < 0)
2089 len = (int)STRLEN(str);
2090
2091 /*
2092 * If the same match is already present, don't add it.
2093 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002094 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002096 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 do
2098 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002099 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002100 && ins_compl_equal(match, str, len)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002101 && match->cp_str[len] == NUL)
2102 return NOTDONE;
2103 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002104 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 }
2106
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002107 /* Remove any popup menu before changing the list of matches. */
2108 ins_compl_del_pum();
2109
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 /*
2111 * Allocate a new match structure.
2112 * Copy the values to the new match structure.
2113 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002114 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002116 return FAIL;
2117 match->cp_number = -1;
2118 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002119 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002120 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 {
2122 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002123 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002125 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002126
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002128 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2130 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002131 if (fname != NULL
2132 && compl_curr_match
2133 && compl_curr_match->cp_fname != NULL
2134 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002135 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002136 else if (fname != NULL)
2137 {
2138 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002139 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002142 match->cp_fname = NULL;
2143 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002144
2145 if (cptext != NULL)
2146 {
2147 int i;
2148
2149 for (i = 0; i < CPT_COUNT; ++i)
2150 if (cptext[i] != NULL && *cptext[i] != NUL)
2151 match->cp_text[i] = vim_strsave(cptext[i]);
2152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153
2154 /*
2155 * Link the new match structure in the list of matches.
2156 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002157 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002158 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 else if (dir == FORWARD)
2160 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002161 match->cp_next = compl_curr_match->cp_next;
2162 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 }
2164 else /* BACKWARD */
2165 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002166 match->cp_next = compl_curr_match;
2167 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002169 if (match->cp_next)
2170 match->cp_next->cp_prev = match;
2171 if (match->cp_prev)
2172 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002174 compl_first_match = match;
2175 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002177 /*
2178 * Find the longest common string if still doing that.
2179 */
2180 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2181 ins_compl_longest_match(match);
2182
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 return OK;
2184}
2185
2186/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002187 * Return TRUE if "str[len]" matches with match->cp_str, considering
2188 * match->cp_icase.
2189 */
2190 static int
2191ins_compl_equal(match, str, len)
2192 compl_T *match;
2193 char_u *str;
2194 int len;
2195{
2196 if (match->cp_icase)
2197 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2198 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2199}
2200
2201/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002202 * Reduce the longest common string for match "match".
2203 */
2204 static void
2205ins_compl_longest_match(match)
2206 compl_T *match;
2207{
2208 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002209 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002210 int had_match;
2211
2212 if (compl_leader == NULL)
2213 /* First match, use it as a whole. */
2214 compl_leader = vim_strsave(match->cp_str);
2215 else
2216 {
2217 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002218 p = compl_leader;
2219 s = match->cp_str;
2220 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002221 {
2222#ifdef FEAT_MBYTE
2223 if (has_mbyte)
2224 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002225 c1 = mb_ptr2char(p);
2226 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002227 }
2228 else
2229#endif
2230 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002231 c1 = *p;
2232 c2 = *s;
2233 }
2234 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2235 : (c1 != c2))
2236 break;
2237#ifdef FEAT_MBYTE
2238 if (has_mbyte)
2239 {
2240 mb_ptr_adv(p);
2241 mb_ptr_adv(s);
2242 }
2243 else
2244#endif
2245 {
2246 ++p;
2247 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002248 }
2249 }
2250
2251 if (*p != NUL)
2252 {
2253 /* Leader was shortened, need to change the inserted text. */
2254 *p = NUL;
2255 had_match = (curwin->w_cursor.col > compl_col);
2256 ins_compl_delete();
2257 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2258 ins_redraw(FALSE);
2259
2260 /* When the match isn't there (to avoid matching itself) remove it
2261 * again after redrawing. */
2262 if (!had_match)
2263 ins_compl_delete();
2264 }
2265
2266 compl_used_match = FALSE;
2267 }
2268}
2269
2270/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 * Add an array of matches to the list of matches.
2272 * Frees matches[].
2273 */
2274 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002275ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 int num_matches;
2277 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002278 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279{
2280 int i;
2281 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002282 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283
Bram Moolenaar572cb562005-08-05 21:35:02 +00002284 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002285 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar39f05632006-03-19 22:15:26 +00002286 NULL, NULL, dir, 0)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002288 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 FreeWild(num_matches, matches);
2290}
2291
2292/* Make the completion list cyclic.
2293 * Return the number of matches (excluding the original).
2294 */
2295 static int
2296ins_compl_make_cyclic()
2297{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002298 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 int count = 0;
2300
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002301 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 {
2303 /*
2304 * Find the end of the list.
2305 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002306 match = compl_first_match;
2307 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002308 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002310 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 ++count;
2312 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002313 match->cp_next = compl_first_match;
2314 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 }
2316 return count;
2317}
2318
Bram Moolenaara94bc432006-03-10 21:42:59 +00002319/*
2320 * Start completion for the complete() function.
2321 * "startcol" is where the matched text starts (1 is first column).
2322 * "list" is the list of matches.
2323 */
2324 void
2325set_completion(startcol, list)
2326 int startcol;
2327 list_T *list;
2328{
2329 /* If already doing completions stop it. */
2330 if (ctrl_x_mode != 0)
2331 ins_compl_prep(' ');
2332 ins_compl_clear();
2333
2334 if (stop_arrow() == FAIL)
2335 return;
2336
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002337 if (startcol > (int)curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002338 startcol = curwin->w_cursor.col;
2339 compl_col = startcol;
2340 compl_length = curwin->w_cursor.col - startcol;
2341 /* compl_pattern doesn't need to be set */
2342 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2343 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar39f05632006-03-19 22:15:26 +00002344 -1, FALSE, NULL, NULL, 0, ORIGINAL_TEXT) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002345 return;
2346
2347 /* Handle like dictionary completion. */
2348 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2349
2350 ins_compl_add_list(list);
2351 compl_matches = ins_compl_make_cyclic();
2352 compl_started = TRUE;
2353 compl_used_match = TRUE;
2354
2355 compl_curr_match = compl_first_match;
2356 ins_complete(Ctrl_N);
2357 out_flush();
2358}
2359
2360
Bram Moolenaar9372a112005-12-06 19:59:18 +00002361/* "compl_match_array" points the currently displayed list of entries in the
2362 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002363static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002364static int compl_match_arraysize;
2365
2366/*
2367 * Update the screen and when there is any scrolling remove the popup menu.
2368 */
2369 static void
2370ins_compl_upd_pum()
2371{
2372 int h;
2373
2374 if (compl_match_array != NULL)
2375 {
2376 h = curwin->w_cline_height;
2377 update_screen(0);
2378 if (h != curwin->w_cline_height)
2379 ins_compl_del_pum();
2380 }
2381}
2382
2383/*
2384 * Remove any popup menu.
2385 */
2386 static void
2387ins_compl_del_pum()
2388{
2389 if (compl_match_array != NULL)
2390 {
2391 pum_undisplay();
2392 vim_free(compl_match_array);
2393 compl_match_array = NULL;
2394 }
2395}
2396
2397/*
2398 * Return TRUE if the popup menu should be displayed.
2399 */
2400 static int
2401pum_wanted()
2402{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002403 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002404 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002405 return FALSE;
2406
2407 /* The display looks bad on a B&W display. */
2408 if (t_colors < 8
2409#ifdef FEAT_GUI
2410 && !gui.in_use
2411#endif
2412 )
2413 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002414 return TRUE;
2415}
2416
2417/*
2418 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002419 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002420 */
2421 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002422pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002423{
2424 compl_T *compl;
2425 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002426
2427 /* Don't display the popup menu if there are no matches or there is only
2428 * one (ignoring the original text). */
2429 compl = compl_first_match;
2430 i = 0;
2431 do
2432 {
2433 if (compl == NULL
2434 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2435 break;
2436 compl = compl->cp_next;
2437 } while (compl != compl_first_match);
2438
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002439 if (strstr((char *)p_cot, "menuone") != NULL)
2440 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002441 return (i >= 2);
2442}
2443
2444/*
2445 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002446 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002447 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002448 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002449ins_compl_show_pum()
2450{
2451 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002452 compl_T *shown_compl = NULL;
2453 int did_find_shown_match = FALSE;
2454 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002455 int i;
2456 int cur = -1;
2457 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002458 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002459
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002460 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002461 return;
2462
2463 /* Update the screen before drawing the popup menu over it. */
2464 update_screen(0);
2465
2466 if (compl_match_array == NULL)
2467 {
2468 /* Need to build the popup menu list. */
2469 compl_match_arraysize = 0;
2470 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002471 if (compl_leader != NULL)
2472 lead_len = STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002473 do
2474 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002475 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2476 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002477 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002478 ++compl_match_arraysize;
2479 compl = compl->cp_next;
2480 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002481 if (compl_match_arraysize == 0)
2482 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002483 compl_match_array = (pumitem_T *)alloc_clear(
2484 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002485 * compl_match_arraysize));
2486 if (compl_match_array != NULL)
2487 {
2488 i = 0;
2489 compl = compl_first_match;
2490 do
2491 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002492 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2493 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002494 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002495 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002496 if (!shown_match_ok)
2497 {
2498 if (compl == compl_shown_match || did_find_shown_match)
2499 {
2500 /* This item is the shown match or this is the
2501 * first displayed item after the shown match. */
2502 compl_shown_match = compl;
2503 did_find_shown_match = TRUE;
2504 shown_match_ok = TRUE;
2505 }
2506 else
2507 /* Remember this displayed match for when the
2508 * shown match is just below it. */
2509 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002510 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002511 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002512
2513 if (compl->cp_text[CPT_ABBR] != NULL)
2514 compl_match_array[i].pum_text =
2515 compl->cp_text[CPT_ABBR];
2516 else
2517 compl_match_array[i].pum_text = compl->cp_str;
2518 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2519 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2520 if (compl->cp_text[CPT_MENU] != NULL)
2521 compl_match_array[i++].pum_extra =
2522 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002523 else
2524 compl_match_array[i++].pum_extra = compl->cp_fname;
2525 }
2526
2527 if (compl == compl_shown_match)
2528 {
2529 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002530
2531 /* When the original text is the shown match don't set
2532 * compl_shown_match. */
2533 if (compl->cp_flags & ORIGINAL_TEXT)
2534 shown_match_ok = TRUE;
2535
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002536 if (!shown_match_ok && shown_compl != NULL)
2537 {
2538 /* The shown match isn't displayed, set it to the
2539 * previously displayed match. */
2540 compl_shown_match = shown_compl;
2541 shown_match_ok = TRUE;
2542 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002543 }
2544 compl = compl->cp_next;
2545 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002546
2547 if (!shown_match_ok) /* no displayed match at all */
2548 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002549 }
2550 }
2551 else
2552 {
2553 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002554 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00002555 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
2556 || compl_match_array[i].pum_text
2557 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaara6557602006-02-04 22:43:20 +00002558 break;
2559 cur = i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002560 }
2561
2562 if (compl_match_array != NULL)
2563 {
2564 /* Compute the screen column of the start of the completed text.
2565 * Use the cursor to get all wrapping and other settings right. */
2566 col = curwin->w_cursor.col;
2567 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00002568 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002569 curwin->w_cursor.col = col;
2570 }
2571}
2572
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573#define DICT_FIRST (1) /* use just first element in "dict" */
2574#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002575
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00002577 * Add any identifiers that match the given pattern in the list of dictionary
2578 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 */
2580 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00002581ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
2582 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002584 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002585 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002587 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 char_u *ptr;
2589 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 char_u **files;
2592 int count;
2593 int i;
2594 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002595 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596
Bram Moolenaar0b238792006-03-02 22:49:12 +00002597 if (*dict == NUL)
2598 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002599#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002600 /* When 'dictionary' is empty and spell checking is enabled use
2601 * "spell". */
2602 if (!thesaurus && curwin->w_p_spell)
2603 dict = (char_u *)"spell";
2604 else
2605#endif
2606 return;
2607 }
2608
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002610 if (buf == NULL)
2611 return;
2612
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 /* If 'infercase' is set, don't use 'smartcase' here */
2614 save_p_scs = p_scs;
2615 if (curbuf->b_p_inf)
2616 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002617
2618 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
2619 * to only match at the start of a line. Otherwise just match the
2620 * pattern. */
2621 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2622 {
2623 i = STRLEN(pat) + 8;
2624 ptr = alloc(i);
2625 if (ptr == NULL)
2626 return;
2627 vim_snprintf((char *)ptr, i, "^\\s*\\zs%s", pat);
2628 regmatch.regprog = vim_regcomp(ptr, p_magic ? RE_MAGIC : 0);
2629 vim_free(ptr);
2630 }
2631 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00002632 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002633 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002634 if (regmatch.regprog == NULL)
2635 goto theend;
2636 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002637
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2639 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002640 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 {
2642 /* copy one dictionary file name into buf */
2643 if (flags == DICT_EXACT)
2644 {
2645 count = 1;
2646 files = &dict;
2647 }
2648 else
2649 {
2650 /* Expand wildcards in the dictionary name, but do not allow
2651 * backticks (for security, the 'dict' option may have been set in
2652 * a modeline). */
2653 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002654# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002655 if (!thesaurus && STRCMP(buf, "spell") == 0)
2656 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002657 else
2658# endif
2659 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 || expand_wildcards(1, &buf, &count, &files,
2661 EW_FILE|EW_SILENT) != OK)
2662 count = 0;
2663 }
2664
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002665# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002666 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00002668 /* Complete from active spelling. Skip "\<" in the pattern, we
2669 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002670 if (pat[0] == '\\' && pat[1] == '<')
2671 ptr = pat + 2;
2672 else
2673 ptr = pat;
2674 spell_dump_compl(curbuf, ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002676 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002677# endif
Bram Moolenaar0b238792006-03-02 22:49:12 +00002678 {
2679 ins_compl_files(count, files, thesaurus, flags,
2680 &regmatch, buf, &dir);
2681 if (flags != DICT_EXACT)
2682 FreeWild(count, files);
2683 }
2684 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 break;
2686 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002687
2688theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 p_scs = save_p_scs;
2690 vim_free(regmatch.regprog);
2691 vim_free(buf);
2692}
2693
Bram Moolenaar0b238792006-03-02 22:49:12 +00002694 static void
2695ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
2696 int count;
2697 char_u **files;
2698 int thesaurus;
2699 int flags;
2700 regmatch_T *regmatch;
2701 char_u *buf;
2702 int *dir;
2703{
2704 char_u *ptr;
2705 int i;
2706 FILE *fp;
2707 int add_r;
2708
2709 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
2710 {
2711 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2712 if (flags != DICT_EXACT)
2713 {
2714 vim_snprintf((char *)IObuff, IOSIZE,
2715 _("Scanning dictionary: %s"), (char *)files[i]);
2716 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2717 }
2718
2719 if (fp != NULL)
2720 {
2721 /*
2722 * Read dictionary file line by line.
2723 * Check each line for a match.
2724 */
2725 while (!got_int && !compl_interrupted
2726 && !vim_fgets(buf, LSIZE, fp))
2727 {
2728 ptr = buf;
2729 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
2730 {
2731 ptr = regmatch->startp[0];
2732 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2733 ptr = find_line_end(ptr);
2734 else
2735 ptr = find_word_end(ptr);
2736 add_r = ins_compl_add_infercase(regmatch->startp[0],
2737 (int)(ptr - regmatch->startp[0]),
Bram Moolenaard289f132006-03-11 21:30:53 +00002738 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002739 if (thesaurus)
2740 {
2741 char_u *wstart;
2742
2743 /*
2744 * Add the other matches on the line
2745 */
2746 while (!got_int)
2747 {
2748 /* Find start of the next word. Skip white
2749 * space and punctuation. */
2750 ptr = find_word_start(ptr);
2751 if (*ptr == NUL || *ptr == NL)
2752 break;
2753 wstart = ptr;
2754
2755 /* Find end of the word and add it. */
2756#ifdef FEAT_MBYTE
2757 if (has_mbyte)
2758 /* Japanese words may have characters in
2759 * different classes, only separate words
2760 * with single-byte non-word characters. */
2761 while (*ptr != NUL)
2762 {
2763 int l = (*mb_ptr2len)(ptr);
2764
2765 if (l < 2 && !vim_iswordc(*ptr))
2766 break;
2767 ptr += l;
2768 }
2769 else
2770#endif
2771 ptr = find_word_end(ptr);
2772 add_r = ins_compl_add_infercase(wstart,
2773 (int)(ptr - wstart),
2774 p_ic, files[i], *dir, 0);
2775 }
2776 }
2777 if (add_r == OK)
2778 /* if dir was BACKWARD then honor it just once */
2779 *dir = FORWARD;
2780 else if (add_r == FAIL)
2781 break;
2782 /* avoid expensive call to vim_regexec() when at end
2783 * of line */
2784 if (*ptr == '\n' || got_int)
2785 break;
2786 }
2787 line_breakcheck();
2788 ins_compl_check_keys(50);
2789 }
2790 fclose(fp);
2791 }
2792 }
2793}
2794
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795/*
2796 * Find the start of the next word.
2797 * Returns a pointer to the first char of the word. Also stops at a NUL.
2798 */
2799 char_u *
2800find_word_start(ptr)
2801 char_u *ptr;
2802{
2803#ifdef FEAT_MBYTE
2804 if (has_mbyte)
2805 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002806 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 else
2808#endif
2809 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
2810 ++ptr;
2811 return ptr;
2812}
2813
2814/*
2815 * Find the end of the word. Assumes it starts inside a word.
2816 * Returns a pointer to just after the word.
2817 */
2818 char_u *
2819find_word_end(ptr)
2820 char_u *ptr;
2821{
2822#ifdef FEAT_MBYTE
2823 int start_class;
2824
2825 if (has_mbyte)
2826 {
2827 start_class = mb_get_class(ptr);
2828 if (start_class > 1)
2829 while (*ptr != NUL)
2830 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002831 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 if (mb_get_class(ptr) != start_class)
2833 break;
2834 }
2835 }
2836 else
2837#endif
2838 while (vim_iswordc(*ptr))
2839 ++ptr;
2840 return ptr;
2841}
2842
2843/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002844 * Find the end of the line, omitting CR and NL at the end.
2845 * Returns a pointer to just after the line.
2846 */
2847 static char_u *
2848find_line_end(ptr)
2849 char_u *ptr;
2850{
2851 char_u *s;
2852
2853 s = ptr + STRLEN(ptr);
2854 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
2855 --s;
2856 return s;
2857}
2858
2859/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 * Free the list of completions
2861 */
2862 static void
2863ins_compl_free()
2864{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002865 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002866 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002868 vim_free(compl_pattern);
2869 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002870 vim_free(compl_leader);
2871 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002873 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002875
2876 ins_compl_del_pum();
2877 pum_clear();
2878
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002879 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 do
2881 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002882 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002883 compl_curr_match = compl_curr_match->cp_next;
2884 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002886 if (match->cp_flags & FREE_FNAME)
2887 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00002888 for (i = 0; i < CPT_COUNT; ++i)
2889 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002891 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
2892 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893}
2894
2895 static void
2896ins_compl_clear()
2897{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002898 compl_cont_status = 0;
2899 compl_started = FALSE;
2900 compl_matches = 0;
2901 vim_free(compl_pattern);
2902 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00002903 vim_free(compl_leader);
2904 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002906 vim_free(compl_orig_text);
2907 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908}
2909
2910/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002911 * Return TRUE when Insert completion is active.
2912 */
2913 int
2914ins_compl_active()
2915{
2916 return compl_started;
2917}
2918
2919/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002920 * Delete one character before the cursor and show the subset of the matches
2921 * that match the word that is now before the cursor.
Bram Moolenaara6557602006-02-04 22:43:20 +00002922 * Returns TRUE if the work is done and another char to be got from the user.
2923 */
2924 static int
2925ins_compl_bs()
2926{
2927 char_u *line;
2928 char_u *p;
2929
2930 if (curwin->w_cursor.col <= compl_col + compl_length)
2931 {
2932 /* Deleted more than what was used to find matches, need to look for
2933 * matches all over again. */
2934 ins_compl_free();
2935 compl_started = FALSE;
2936 compl_matches = 0;
2937 }
2938
2939 line = ml_get_curline();
2940 p = line + curwin->w_cursor.col;
2941 mb_ptr_back(line, p);
2942
2943 vim_free(compl_leader);
2944 compl_leader = vim_strnsave(line + compl_col, (p - line) - compl_col);
2945 if (compl_leader != NULL)
2946 {
2947 ins_compl_del_pum();
2948 ins_compl_delete();
2949 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2950
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002951 if (compl_started)
2952 ins_compl_set_original_text(compl_leader);
2953 else
Bram Moolenaara6557602006-02-04 22:43:20 +00002954 {
2955 /* Matches were cleared, need to search for them now. */
2956 if (ins_complete(Ctrl_N) == FAIL)
2957 compl_cont_status = 0;
2958 else
2959 {
2960 /* Remove the completed word again. */
2961 ins_compl_delete();
2962 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
2963 }
2964 }
2965
2966 /* Show the popup menu with a different set of matches. */
2967 ins_compl_show_pum();
2968 compl_used_match = FALSE;
2969
2970 return TRUE;
2971 }
2972 return FALSE;
2973}
2974
2975/*
2976 * Append one character to the match leader. May reduce the number of
2977 * matches.
2978 */
2979 static void
2980ins_compl_addleader(c)
2981 int c;
2982{
2983#ifdef FEAT_MBYTE
2984 int cc;
2985
2986 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
2987 {
2988 char_u buf[MB_MAXBYTES + 1];
2989
2990 (*mb_char2bytes)(c, buf);
2991 buf[cc] = NUL;
2992 ins_char_bytes(buf, cc);
2993 }
2994 else
2995#endif
2996 ins_char(c);
2997
2998 vim_free(compl_leader);
2999 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
3000 curwin->w_cursor.col - compl_col);
3001 if (compl_leader != NULL)
3002 {
3003 /* Show the popup menu with a different set of matches. */
3004 ins_compl_del_pum();
3005 ins_compl_show_pum();
3006 compl_used_match = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003007 ins_compl_set_original_text(compl_leader);
3008 }
3009}
3010
3011/*
3012 * Set the first match, the original text.
3013 */
3014 static void
3015ins_compl_set_original_text(str)
3016 char_u *str;
3017{
3018 char_u *p;
3019
3020 /* Replace the original text entry. */
3021 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3022 {
3023 p = vim_strsave(str);
3024 if (p != NULL)
3025 {
3026 vim_free(compl_first_match->cp_str);
3027 compl_first_match->cp_str = p;
3028 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003029 }
3030}
3031
3032/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003033 * Append one character to the match leader. May reduce the number of
3034 * matches.
3035 */
3036 static void
3037ins_compl_addfrommatch()
3038{
3039 char_u *p;
3040 int len = curwin->w_cursor.col - compl_col;
3041 int c;
3042
3043 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003044 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003045 return;
3046 p += len;
3047#ifdef FEAT_MBYTE
3048 c = mb_ptr2char(p);
3049#else
3050 c = *p;
3051#endif
3052 ins_compl_addleader(c);
3053}
3054
3055/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003057 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003058 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003060 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061ins_compl_prep(c)
3062 int c;
3063{
3064 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 int temp;
3066 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003067 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068
3069 /* Forget any previous 'special' messages if this is actually
3070 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3071 */
3072 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3073 edit_submode_extra = NULL;
3074
3075 /* Ignore end of Select mode mapping */
3076 if (c == K_SELECT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003077 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003079 /* Set "compl_get_longest" when finding the first matches. */
3080 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3081 || (ctrl_x_mode == 0 && !compl_started))
3082 {
3083 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3084 compl_used_match = TRUE;
3085 }
3086
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3088 {
3089 /*
3090 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3091 * it will be yet. Now we decide.
3092 */
3093 switch (c)
3094 {
3095 case Ctrl_E:
3096 case Ctrl_Y:
3097 ctrl_x_mode = CTRL_X_SCROLL;
3098 if (!(State & REPLACE_FLAG))
3099 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3100 else
3101 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3102 edit_submode_pre = NULL;
3103 showmode();
3104 break;
3105 case Ctrl_L:
3106 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3107 break;
3108 case Ctrl_F:
3109 ctrl_x_mode = CTRL_X_FILES;
3110 break;
3111 case Ctrl_K:
3112 ctrl_x_mode = CTRL_X_DICTIONARY;
3113 break;
3114 case Ctrl_R:
3115 /* Simply allow ^R to happen without affecting ^X mode */
3116 break;
3117 case Ctrl_T:
3118 ctrl_x_mode = CTRL_X_THESAURUS;
3119 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003120#ifdef FEAT_COMPL_FUNC
3121 case Ctrl_U:
3122 ctrl_x_mode = CTRL_X_FUNCTION;
3123 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003124 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003125 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003126 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003127#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003128 case 's':
3129 case Ctrl_S:
3130 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003131#ifdef FEAT_SPELL
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003132 spell_back_to_badword();
3133#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003134 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 case Ctrl_RSB:
3136 ctrl_x_mode = CTRL_X_TAGS;
3137 break;
3138#ifdef FEAT_FIND_ID
3139 case Ctrl_I:
3140 case K_S_TAB:
3141 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3142 break;
3143 case Ctrl_D:
3144 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3145 break;
3146#endif
3147 case Ctrl_V:
3148 case Ctrl_Q:
3149 ctrl_x_mode = CTRL_X_CMDLINE;
3150 break;
3151 case Ctrl_P:
3152 case Ctrl_N:
3153 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3154 * just started ^X mode, or there were enough ^X's to cancel
3155 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3156 * do normal expansion when interrupting a different mode (say
3157 * ^X^F^X^P or ^P^X^X^P, see below)
3158 * nothing changes if interrupting mode 0, (eg, the flag
3159 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003160 if (!(compl_cont_status & CONT_INTRPT))
3161 compl_cont_status |= CONT_LOCAL;
3162 else if (compl_cont_mode != 0)
3163 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 /* FALLTHROUGH */
3165 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003166 /* If we have typed at least 2 ^X's... for modes != 0, we set
3167 * compl_cont_status = 0 (eg, as if we had just started ^X
3168 * mode).
3169 * For mode 0, we set "compl_cont_mode" to an impossible
3170 * value, in both cases ^X^X can be used to restart the same
3171 * mode (avoiding ADDING mode).
3172 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3173 * 'complete' and local ^P expansions respectively.
3174 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3175 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 if (c == Ctrl_X)
3177 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003178 if (compl_cont_mode != 0)
3179 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003181 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 }
3183 ctrl_x_mode = 0;
3184 edit_submode = NULL;
3185 showmode();
3186 break;
3187 }
3188 }
3189 else if (ctrl_x_mode != 0)
3190 {
3191 /* We're already in CTRL-X mode, do we stay in it? */
3192 if (!vim_is_ctrl_x_key(c))
3193 {
3194 if (ctrl_x_mode == CTRL_X_SCROLL)
3195 ctrl_x_mode = 0;
3196 else
3197 ctrl_x_mode = CTRL_X_FINISHED;
3198 edit_submode = NULL;
3199 }
3200 showmode();
3201 }
3202
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003203 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 {
3205 /* Show error message from attempted keyword completion (probably
3206 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003207 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003209 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3210 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 || ctrl_x_mode == CTRL_X_FINISHED)
3212 {
3213 /* Get here when we have finished typing a sequence of ^N and
3214 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003215 * memory that was used, and make sure we can redo the insert. */
3216 if (compl_curr_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003218 char_u *p;
3219
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 /*
3221 * If any of the original typed text has been changed,
3222 * eg when ignorecase is set, we must add back-spaces to
3223 * the redo buffer. We add as few as necessary to delete
3224 * just the part of the original text that has changed.
3225 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003226 ptr = compl_curr_match->cp_str;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003227 p = compl_orig_text;
3228 while (*p && *p == *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003230 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 ++ptr;
3232 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003233 for (temp = 0; p[temp]; ++temp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 AppendCharToRedobuff(K_BS);
Bram Moolenaarebefac62005-12-28 22:39:57 +00003235 AppendToRedobuffLit(ptr, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 }
3237
3238#ifdef FEAT_CINDENT
3239 want_cindent = (can_cindent && cindent_on());
3240#endif
3241 /*
3242 * When completing whole lines: fix indent for 'cindent'.
3243 * Otherwise, break line if it's too long.
3244 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003245 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 {
3247#ifdef FEAT_CINDENT
3248 /* re-indent the current line */
3249 if (want_cindent)
3250 {
3251 do_c_expr_indent();
3252 want_cindent = FALSE; /* don't do it again */
3253 }
3254#endif
3255 }
3256 else
3257 {
3258 /* put the cursor on the last char, for 'tw' formatting */
3259 curwin->w_cursor.col--;
3260 if (stop_arrow() == OK)
3261 insertchar(NUL, 0, -1);
3262 curwin->w_cursor.col++;
3263 }
3264
3265 auto_format(FALSE, TRUE);
3266
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003267 /* if the popup menu is displayed hitting Enter means accepting
3268 * the selection without inserting anything. */
3269 if ((c == CAR || c == K_KENTER || c == NL) && pum_visible())
3270 retval = TRUE;
3271
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003273 compl_started = FALSE;
3274 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 msg_clr_cmdline(); /* necessary for "noshowmode" */
3276 ctrl_x_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 if (edit_submode != NULL)
3278 {
3279 edit_submode = NULL;
3280 showmode();
3281 }
3282
3283#ifdef FEAT_CINDENT
3284 /*
3285 * Indent now if a key was typed that is in 'cinkeys'.
3286 */
3287 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3288 do_c_expr_indent();
3289#endif
3290 }
3291 }
3292
3293 /* reset continue_* if we left expansion-mode, if we stay they'll be
3294 * (re)set properly in ins_complete() */
3295 if (!vim_is_ctrl_x_key(c))
3296 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003297 compl_cont_status = 0;
3298 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003300
3301 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302}
3303
3304/*
3305 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3306 * (depending on flag) starting from buf and looking for a non-scanned
3307 * buffer (other than curbuf). curbuf is special, if it is called with
3308 * buf=curbuf then it has to be the first call for a given flag/expansion.
3309 *
3310 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3311 */
3312 static buf_T *
3313ins_compl_next_buf(buf, flag)
3314 buf_T *buf;
3315 int flag;
3316{
3317#ifdef FEAT_WINDOWS
3318 static win_T *wp;
3319#endif
3320
3321 if (flag == 'w') /* just windows */
3322 {
3323#ifdef FEAT_WINDOWS
3324 if (buf == curbuf) /* first call for this flag/expansion */
3325 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003326 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 && wp->w_buffer->b_scanned)
3328 ;
3329 buf = wp->w_buffer;
3330#else
3331 buf = curbuf;
3332#endif
3333 }
3334 else
3335 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3336 * (unlisted buffers)
3337 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003338 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 && ((flag == 'U'
3340 ? buf->b_p_bl
3341 : (!buf->b_p_bl
3342 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003343 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 ;
3345 return buf;
3346}
3347
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003348#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003349static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003350
3351/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003352 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003353 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003354 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003355 static void
3356expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003357 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003358 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003359{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003360 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003361 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003362 char_u *funcname;
3363 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003364
Bram Moolenaare344bea2005-09-01 20:46:49 +00003365 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3366 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003367 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003368
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003369 /* Call 'completefunc' to obtain the list of matches. */
3370 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003371 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003372
Bram Moolenaare344bea2005-09-01 20:46:49 +00003373 pos = curwin->w_cursor;
3374 matchlist = call_func_retlist(funcname, 2, args, FALSE);
3375 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003376 if (matchlist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003377 return;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003378
Bram Moolenaara94bc432006-03-10 21:42:59 +00003379 ins_compl_add_list(matchlist);
3380 list_unref(matchlist);
3381}
3382#endif /* FEAT_COMPL_FUNC */
3383
Bram Moolenaar39f05632006-03-19 22:15:26 +00003384#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00003385/*
3386 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00003387 */
3388 static void
3389ins_compl_add_list(list)
3390 list_T *list;
3391{
3392 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003393 int dir = compl_direction;
3394
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003395 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003396 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003397 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00003398 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
3399 /* if dir was BACKWARD then honor it just once */
3400 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00003401 else if (did_emsg)
3402 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003403 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003404}
Bram Moolenaar39f05632006-03-19 22:15:26 +00003405
3406/*
3407 * Add a match to the list of matches from a typeval_T.
3408 * If the given string is already in the list of completions, then return
3409 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
3410 * maybe because alloc() returns NULL, then FAIL is returned.
3411 */
3412 int
3413ins_compl_add_tv(tv, dir)
3414 typval_T *tv;
3415 int dir;
3416{
3417 char_u *word;
3418 int icase = p_ic;
3419 char_u *(cptext[CPT_COUNT]);
3420
3421 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3422 {
3423 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
3424 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
3425 (char_u *)"abbr", FALSE);
3426 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
3427 (char_u *)"menu", FALSE);
3428 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
3429 (char_u *)"kind", FALSE);
3430 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
3431 (char_u *)"info", FALSE);
3432 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
3433 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
3434 }
3435 else
3436 {
3437 word = get_tv_string_chk(tv);
3438 vim_memset(cptext, 0, sizeof(cptext));
3439 }
3440 if (word == NULL || *word == NUL)
3441 return FAIL;
3442 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0);
3443}
Bram Moolenaara94bc432006-03-10 21:42:59 +00003444#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003445
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003446/*
3447 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003448 * The search starts at position "ini" in curbuf and in the direction
3449 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003450 * When "compl_started" is FALSE start at that position, otherwise continue
3451 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003452 * This may return before finding all the matches.
3453 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 */
3455 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003456ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458{
3459 static pos_T first_match_pos;
3460 static pos_T last_match_pos;
3461 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003462 static int found_all = FALSE; /* Found all matches of a
3463 certain type. */
3464 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465
Bram Moolenaar572cb562005-08-05 21:35:02 +00003466 pos_T *pos;
3467 char_u **matches;
3468 int save_p_scs;
3469 int save_p_ws;
3470 int save_p_ic;
3471 int i;
3472 int num_matches;
3473 int len;
3474 int found_new_match;
3475 int type = ctrl_x_mode;
3476 char_u *ptr;
3477 char_u *dict = NULL;
3478 int dict_f = 0;
3479 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003481 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 {
3483 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3484 ins_buf->b_scanned = 0;
3485 found_all = FALSE;
3486 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003487 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003488 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 last_match_pos = first_match_pos = *ini;
3490 }
3491
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003492 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003493 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3495 for (;;)
3496 {
3497 found_new_match = FAIL;
3498
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003499 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 * or if found_all says this entry is done. For ^X^L only use the
3501 * entries from 'complete' that look in loaded buffers. */
3502 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003503 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 {
3505 found_all = FALSE;
3506 while (*e_cpt == ',' || *e_cpt == ' ')
3507 e_cpt++;
3508 if (*e_cpt == '.' && !curbuf->b_scanned)
3509 {
3510 ins_buf = curbuf;
3511 first_match_pos = *ini;
3512 /* So that ^N can match word immediately after cursor */
3513 if (ctrl_x_mode == 0)
3514 dec(&first_match_pos);
3515 last_match_pos = first_match_pos;
3516 type = 0;
3517 }
3518 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
3519 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
3520 {
3521 /* Scan a buffer, but not the current one. */
3522 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
3523 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003524 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 first_match_pos.col = last_match_pos.col = 0;
3526 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
3527 last_match_pos.lnum = 0;
3528 type = 0;
3529 }
3530 else /* unloaded buffer, scan like dictionary */
3531 {
3532 found_all = TRUE;
3533 if (ins_buf->b_fname == NULL)
3534 continue;
3535 type = CTRL_X_DICTIONARY;
3536 dict = ins_buf->b_fname;
3537 dict_f = DICT_EXACT;
3538 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00003539 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 ins_buf->b_fname == NULL
3541 ? buf_spname(ins_buf)
3542 : ins_buf->b_sfname == NULL
3543 ? (char *)ins_buf->b_fname
3544 : (char *)ins_buf->b_sfname);
3545 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3546 }
3547 else if (*e_cpt == NUL)
3548 break;
3549 else
3550 {
3551 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3552 type = -1;
3553 else if (*e_cpt == 'k' || *e_cpt == 's')
3554 {
3555 if (*e_cpt == 'k')
3556 type = CTRL_X_DICTIONARY;
3557 else
3558 type = CTRL_X_THESAURUS;
3559 if (*++e_cpt != ',' && *e_cpt != NUL)
3560 {
3561 dict = e_cpt;
3562 dict_f = DICT_FIRST;
3563 }
3564 }
3565#ifdef FEAT_FIND_ID
3566 else if (*e_cpt == 'i')
3567 type = CTRL_X_PATH_PATTERNS;
3568 else if (*e_cpt == 'd')
3569 type = CTRL_X_PATH_DEFINES;
3570#endif
3571 else if (*e_cpt == ']' || *e_cpt == 't')
3572 {
3573 type = CTRL_X_TAGS;
3574 sprintf((char*)IObuff, _("Scanning tags."));
3575 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3576 }
3577 else
3578 type = -1;
3579
3580 /* in any case e_cpt is advanced to the next entry */
3581 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
3582
3583 found_all = TRUE;
3584 if (type == -1)
3585 continue;
3586 }
3587 }
3588
3589 switch (type)
3590 {
3591 case -1:
3592 break;
3593#ifdef FEAT_FIND_ID
3594 case CTRL_X_PATH_PATTERNS:
3595 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003596 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003597 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003599 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
3601 (linenr_T)1, (linenr_T)MAXLNUM);
3602 break;
3603#endif
3604
3605 case CTRL_X_DICTIONARY:
3606 case CTRL_X_THESAURUS:
3607 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00003608 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 : (type == CTRL_X_THESAURUS
3610 ? (*curbuf->b_p_tsr == NUL
3611 ? p_tsr
3612 : curbuf->b_p_tsr)
3613 : (*curbuf->b_p_dict == NUL
3614 ? p_dict
3615 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003616 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00003617 dict != NULL ? dict_f
3618 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 dict = NULL;
3620 break;
3621
3622 case CTRL_X_TAGS:
3623 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
3624 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003625 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626
3627 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003628 * of matches is found when compl_pattern is empty */
3629 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
3631 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
3632 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
3633 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003634 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 }
3636 p_ic = save_p_ic;
3637 break;
3638
3639 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003640 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
3642 {
3643
3644 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003645 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003646 ins_compl_add_matches(num_matches, matches,
3647#ifdef CASE_INSENSITIVE_FILENAME
3648 TRUE
3649#else
3650 FALSE
3651#endif
3652 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 }
3654 break;
3655
3656 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003657 if (expand_cmdline(&compl_xp, compl_pattern,
3658 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003660 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 break;
3662
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003663#ifdef FEAT_COMPL_FUNC
3664 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003665 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003666 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003667 break;
3668#endif
3669
Bram Moolenaar488c6512005-08-11 20:09:58 +00003670 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003671#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00003672 num_matches = expand_spelling(first_match_pos.lnum,
3673 first_match_pos.col, compl_pattern, &matches);
3674 if (num_matches > 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003675 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar488c6512005-08-11 20:09:58 +00003676#endif
3677 break;
3678
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 default: /* normal ^P/^N and ^X^L */
3680 /*
3681 * If 'infercase' is set, don't use 'smartcase' here
3682 */
3683 save_p_scs = p_scs;
3684 if (ins_buf->b_p_inf)
3685 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003686
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 /* buffers other than curbuf are scanned from the beginning or the
3688 * end but never from the middle, thus setting nowrapscan in this
3689 * buffers is a good idea, on the other hand, we always set
3690 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
3691 save_p_ws = p_ws;
3692 if (ins_buf != curbuf)
3693 p_ws = FALSE;
3694 else if (*e_cpt == '.')
3695 p_ws = TRUE;
3696 for (;;)
3697 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00003698 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003700 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
3701 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003703 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003705 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003707 found_new_match = searchit(NULL, ins_buf, pos,
3708 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003709 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003710 RE_LAST, (linenr_T)0);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003711 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003713 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003714 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 first_match_pos = *pos;
3716 last_match_pos = *pos;
3717 }
3718 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003719 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 found_new_match = FAIL;
3721 if (found_new_match == FAIL)
3722 {
3723 if (ins_buf == curbuf)
3724 found_all = TRUE;
3725 break;
3726 }
3727
3728 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003729 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 && ini->lnum == pos->lnum
3731 && ini->col == pos->col)
3732 continue;
3733 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
3734 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3735 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003736 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 {
3738 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
3739 continue;
3740 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3741 if (!p_paste)
3742 ptr = skipwhite(ptr);
3743 }
3744 len = (int)STRLEN(ptr);
3745 }
3746 else
3747 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003748 char_u *tmp_ptr = ptr;
3749
3750 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003752 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 /* Skip if already inside a word. */
3754 if (vim_iswordp(tmp_ptr))
3755 continue;
3756 /* Find start of next word. */
3757 tmp_ptr = find_word_start(tmp_ptr);
3758 }
3759 /* Find end of this word. */
3760 tmp_ptr = find_word_end(tmp_ptr);
3761 len = (int)(tmp_ptr - ptr);
3762
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003763 if ((compl_cont_status & CONT_ADDING)
3764 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 {
3766 if (pos->lnum < ins_buf->b_ml.ml_line_count)
3767 {
3768 /* Try next line, if any. the new word will be
3769 * "join" as if the normal command "J" was used.
3770 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003771 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 * works -- Acevedo */
3773 STRNCPY(IObuff, ptr, len);
3774 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
3775 tmp_ptr = ptr = skipwhite(ptr);
3776 /* Find start of next word. */
3777 tmp_ptr = find_word_start(tmp_ptr);
3778 /* Find end of next word. */
3779 tmp_ptr = find_word_end(tmp_ptr);
3780 if (tmp_ptr > ptr)
3781 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003782 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003784 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 IObuff[len++] = ' ';
3786 /* IObuf =~ "\k.* ", thus len >= 2 */
3787 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003788 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 || (vim_strchr(p_cpo, CPO_JOINSP)
3790 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003791 && (IObuff[len - 2] == '?'
3792 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 IObuff[len++] = ' ';
3794 }
3795 /* copy as much as posible of the new word */
3796 if (tmp_ptr - ptr >= IOSIZE - len)
3797 tmp_ptr = ptr + IOSIZE - len - 1;
3798 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3799 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00003800 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 }
3802 IObuff[len] = NUL;
3803 ptr = IObuff;
3804 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003805 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 continue;
3807 }
3808 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003809 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003810 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003811 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 {
3813 found_new_match = OK;
3814 break;
3815 }
3816 }
3817 p_scs = save_p_scs;
3818 p_ws = save_p_ws;
3819 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003820
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003821 /* check if compl_curr_match has changed, (e.g. other type of
3822 * expansion added somenthing) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003823 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 found_new_match = OK;
3825
3826 /* break the loop for specialized modes (use 'complete' just for the
3827 * generic ctrl_x_mode == 0) or when we've found a new match */
3828 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003829 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003830 {
3831 if (got_int)
3832 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003833 /* Fill the popup menu as soon as possible. */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003834 if (pum_wanted() && type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003835 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003836
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003837 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
3838 || compl_interrupted)
3839 break;
3840 compl_started = TRUE;
3841 }
3842 else
3843 {
3844 /* Mark a buffer scanned when it has been scanned completely */
3845 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
3846 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003848 compl_started = FALSE;
3849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003851 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852
3853 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
3854 && *e_cpt == NUL) /* Got to end of 'complete' */
3855 found_new_match = FAIL;
3856
3857 i = -1; /* total of matches, unknown */
3858 if (found_new_match == FAIL
3859 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
3860 i = ins_compl_make_cyclic();
3861
3862 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003863 * just been made cyclic then we have to move compl_curr_match to the next
3864 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003865 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
3866 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003867 if (compl_curr_match == NULL)
3868 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 return i;
3870}
3871
3872/* Delete the old text being completed. */
3873 static void
3874ins_compl_delete()
3875{
3876 int i;
3877
3878 /*
3879 * In insert mode: Delete the typed part.
3880 * In replace mode: Put the old characters back, if any.
3881 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003882 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 backspace_until_column(i);
3884 changed_cline_bef_curs();
3885}
3886
3887/* Insert the new text being completed. */
3888 static void
3889ins_compl_insert()
3890{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003891 ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col);
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003892 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3893 compl_used_match = FALSE;
3894 else
3895 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896}
3897
3898/*
3899 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003900 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
3901 * get more completions. If it is FALSE, then we just do nothing when there
3902 * are no more completions in a given direction. The latter case is used when
3903 * we are still in the middle of finding completions, to allow browsing
3904 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 * Return the total number of matches, or -1 if still unknown -- webb.
3906 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003907 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
3908 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 *
3910 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00003911 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
3912 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 */
3914 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003915ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003917 int count; /* repeat completion this many times; should
3918 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003919 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920{
3921 int num_matches = -1;
3922 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003923 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00003924 compl_T *found_compl = NULL;
3925 int found_end = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003927 if (compl_leader != NULL
3928 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003930 /* Set "compl_shown_match" to the actually shown match, it may differ
3931 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003932 while (!ins_compl_equal(compl_shown_match,
3933 compl_leader, STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003934 && compl_shown_match->cp_next != NULL
3935 && compl_shown_match->cp_next != compl_first_match)
3936 compl_shown_match = compl_shown_match->cp_next;
3937 }
3938
3939 if (allow_get_expansion && insert_match
3940 && (!compl_get_longest || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 /* Delete old text to be replaced */
3942 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003943
Bram Moolenaare3226be2005-12-18 22:10:00 +00003944 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
3945 * around. */
3946 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00003948 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003950 if (compl_pending != 0)
3951 --compl_pending;
Bram Moolenaare3226be2005-12-18 22:10:00 +00003952 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00003953 found_end = (compl_first_match != NULL
3954 && (compl_shown_match->cp_next == compl_first_match
3955 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00003956 }
3957 else if (compl_shows_dir == BACKWARD
3958 && compl_shown_match->cp_prev != NULL)
3959 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003960 if (compl_pending != 0)
3961 ++compl_pending;
Bram Moolenaara6557602006-02-04 22:43:20 +00003962 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00003963 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00003964 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 }
3966 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00003967 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003968 if (compl_shows_dir == BACKWARD)
3969 --compl_pending;
3970 else
3971 ++compl_pending;
Bram Moolenaara6557602006-02-04 22:43:20 +00003972 if (!allow_get_expansion)
Bram Moolenaare3226be2005-12-18 22:10:00 +00003973 return -1;
Bram Moolenaara6557602006-02-04 22:43:20 +00003974
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003975 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003976 if (compl_pending != 0 && compl_direction == compl_shows_dir)
Bram Moolenaara6557602006-02-04 22:43:20 +00003977 compl_shown_match = compl_curr_match;
3978 found_end = FALSE;
3979 }
3980 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
3981 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003982 && !ins_compl_equal(compl_shown_match,
3983 compl_leader, STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00003984 ++todo;
3985 else
3986 /* Remember a matching item. */
3987 found_compl = compl_shown_match;
3988
3989 /* Stop at the end of the list when we found a usable match. */
3990 if (found_end)
3991 {
3992 if (found_compl != NULL)
3993 {
3994 compl_shown_match = found_compl;
3995 break;
3996 }
3997 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00003998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 }
4000
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004001 /* Insert the text of the new completion, or the compl_leader. */
4002 if (insert_match)
4003 {
4004 if (!compl_get_longest || compl_used_match)
4005 ins_compl_insert();
4006 else
4007 ins_bytes(compl_leader + curwin->w_cursor.col - compl_col);
4008 }
4009 else
4010 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011
4012 if (!allow_get_expansion)
4013 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004014 /* may undisplay the popup menu first */
4015 ins_compl_upd_pum();
4016
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004017 /* redraw to show the user what was inserted */
4018 update_screen(0);
4019
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004020 /* display the updated popup menu */
4021 ins_compl_show_pum();
4022
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 /* Delete old text to be replaced, since we're still searching and
4024 * don't want to match ourselves! */
4025 ins_compl_delete();
4026 }
4027
4028 /*
4029 * Show the file name for the match (if any)
4030 * Truncate the file name to avoid a wait for return.
4031 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004032 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 {
4034 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004035 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 if (i <= 0)
4037 i = 0;
4038 else
4039 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004040 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 msg(IObuff);
4042 redraw_cmdline = FALSE; /* don't overwrite! */
4043 }
4044
4045 return num_matches;
4046}
4047
4048/*
4049 * Call this while finding completions, to check whether the user has hit a key
4050 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004051 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004053 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 */
4055 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004056ins_compl_check_keys(frequency)
4057 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058{
4059 static int count = 0;
4060
4061 int c;
4062
4063 /* Don't check when reading keys from a script. That would break the test
4064 * scripts */
4065 if (using_script())
4066 return;
4067
4068 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004069 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 return;
4071 count = 0;
4072
4073 ++no_mapping;
4074 c = vpeekc_any();
4075 --no_mapping;
4076 if (c != NUL)
4077 {
4078 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4079 {
4080 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004081 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004082 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4083 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 }
4085 else if (c != Ctrl_R)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004086 compl_interrupted = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004088 if (compl_pending != 0 && !got_int)
4089 (void)ins_compl_next(FALSE, compl_pending > 0
4090 ? compl_pending : -compl_pending, TRUE);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004091}
4092
4093/*
4094 * Decide the direction of Insert mode complete from the key typed.
4095 * Returns BACKWARD or FORWARD.
4096 */
4097 static int
4098ins_compl_key2dir(c)
4099 int c;
4100{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004101 if (c == Ctrl_P || c == Ctrl_L
4102 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4103 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004104 return BACKWARD;
4105 return FORWARD;
4106}
4107
4108/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004109 * Return TRUE for keys that are used for completion only when the popup menu
4110 * is visible.
4111 */
4112 static int
4113ins_compl_pum_key(c)
4114 int c;
4115{
4116 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004117 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4118 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004119}
4120
4121/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004122 * Decide the number of completions to move forward.
4123 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4124 */
4125 static int
4126ins_compl_key2count(c)
4127 int c;
4128{
4129 int h;
4130
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004131 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004132 {
4133 h = pum_get_height();
4134 if (h > 3)
4135 h -= 2; /* keep some context */
4136 return h;
4137 }
4138 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139}
4140
4141/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004142 * Return TRUE if completion with "c" should insert the match, FALSE if only
4143 * to change the currently selected completion.
4144 */
4145 static int
4146ins_compl_use_match(c)
4147 int c;
4148{
4149 switch (c)
4150 {
4151 case K_UP:
4152 case K_DOWN:
4153 case K_PAGEDOWN:
4154 case K_KPAGEDOWN:
4155 case K_S_DOWN:
4156 case K_PAGEUP:
4157 case K_KPAGEUP:
4158 case K_S_UP:
4159 return FALSE;
4160 }
4161 return TRUE;
4162}
4163
4164/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 * Do Insert mode completion.
4166 * Called when character "c" was typed, which has a meaning for completion.
4167 * Returns OK if completion was done, FAIL if something failed (out of mem).
4168 */
4169 static int
4170ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004171 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004173 char_u *line;
4174 int startcol = 0; /* column where searched text starts */
4175 colnr_T curs_col; /* cursor column */
4176 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177
Bram Moolenaare3226be2005-12-18 22:10:00 +00004178 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004179 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 {
4181 /* First time we hit ^N or ^P (in a row, I mean) */
4182
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 did_ai = FALSE;
4184#ifdef FEAT_SMARTINDENT
4185 did_si = FALSE;
4186 can_si = FALSE;
4187 can_si_back = FALSE;
4188#endif
4189 if (stop_arrow() == FAIL)
4190 return FAIL;
4191
4192 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004193 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004194 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195
4196 /* if this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004197 * "compl_startpos" to the cursor as a pattern to add a new word
4198 * instead of expand the one before the cursor, in word-wise if
4199 * "compl_startpos"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 * is not in the same line as the cursor then fix it (the line has
4201 * been split because it was longer than 'tw'). if SOL is set then
4202 * skip the previous pattern, a word at the beginning of the line has
4203 * been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004204 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4205 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 {
4207 /*
4208 * it is a continued search
4209 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004210 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4212 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4213 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004214 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004216 /* line (probably) wrapped, set compl_startpos to the
4217 * first non_blank in the line, if it is not a wordchar
4218 * include it to get a better pattern, but then we don't
4219 * want the "\\<" prefix, check it bellow */
4220 compl_col = (colnr_T)(skipwhite(line) - line);
4221 compl_startpos.col = compl_col;
4222 compl_startpos.lnum = curwin->w_cursor.lnum;
4223 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 }
4225 else
4226 {
4227 /* S_IPOS was set when we inserted a word that was at the
4228 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004229 * mode but first we need to redefine compl_startpos */
4230 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004232 compl_cont_status |= CONT_SOL;
4233 compl_startpos.col = (colnr_T)(skipwhite(
4234 line + compl_length
4235 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004237 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004239 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004240 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 * have enough space? just being paranoic */
4242#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004243 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004245 compl_cont_status &= ~CONT_SOL;
4246 compl_length = (IOSIZE - MIN_SPACE);
4247 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004249 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4250 if (compl_length < 1)
4251 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 }
4253 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004254 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004256 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 }
4258 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004259 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004261 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004263 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004265 compl_cont_status = 0;
4266 compl_cont_status |= CONT_N_ADDS;
4267 compl_startpos = curwin->w_cursor;
4268 startcol = (int)curs_col;
4269 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 }
4271
4272 /* Work out completion pattern and original text -- webb */
4273 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4274 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004275 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4277 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004278 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004280 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004282 compl_col += ++startcol;
4283 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 }
4285 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004286 compl_pattern = str_foldcase(line + compl_col,
4287 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004289 compl_pattern = vim_strnsave(line + compl_col,
4290 compl_length);
4291 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 return FAIL;
4293 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004294 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 {
4296 char_u *prefix = (char_u *)"\\<";
4297
4298 /* we need 3 extra chars, 1 for the NUL and
4299 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004300 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4301 compl_length) + 3);
4302 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004304 if (!vim_iswordp(line + compl_col)
4305 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 && (
4307#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004308 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004310 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311#endif
4312 )))
4313 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004314 STRCPY((char *)compl_pattern, prefix);
4315 (void)quote_meta(compl_pattern + STRLEN(prefix),
4316 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004318 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004320 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004322 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323#endif
4324 )
4325 {
4326 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004327 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4328 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004330 compl_col += curs_col;
4331 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 }
4333 else
4334 {
4335#ifdef FEAT_MBYTE
4336 /* Search the point of change class of multibyte character
4337 * or not a word single byte character backward. */
4338 if (has_mbyte)
4339 {
4340 int base_class;
4341 int head_off;
4342
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004343 startcol -= (*mb_head_off)(line, line + startcol);
4344 base_class = mb_get_class(line + startcol);
4345 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004347 head_off = (*mb_head_off)(line, line + startcol);
4348 if (base_class != mb_get_class(line + startcol
4349 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004351 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 }
4353 }
4354 else
4355#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004356 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004358 compl_col += ++startcol;
4359 compl_length = (int)curs_col - startcol;
4360 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 {
4362 /* Only match word with at least two chars -- webb
4363 * there's no need to call quote_meta,
4364 * alloc(7) is enough -- Acevedo
4365 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004366 compl_pattern = alloc(7);
4367 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004369 STRCPY((char *)compl_pattern, "\\<");
4370 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4371 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 }
4373 else
4374 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004375 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4376 compl_length) + 3);
4377 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004379 STRCPY((char *)compl_pattern, "\\<");
4380 (void)quote_meta(compl_pattern + 2, line + compl_col,
4381 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 }
4383 }
4384 }
4385 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4386 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004387 compl_col = skipwhite(line) - line;
4388 compl_length = (int)curs_col - (int)compl_col;
4389 if (compl_length < 0) /* cursor in indent: empty pattern */
4390 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004392 compl_pattern = str_foldcase(line + compl_col, compl_length,
4393 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004395 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4396 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 return FAIL;
4398 }
4399 else if (ctrl_x_mode == CTRL_X_FILES)
4400 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004401 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004403 compl_col += ++startcol;
4404 compl_length = (int)curs_col - startcol;
4405 compl_pattern = addstar(line + compl_col, compl_length,
4406 EXPAND_FILES);
4407 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 return FAIL;
4409 }
4410 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4411 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004412 compl_pattern = vim_strnsave(line, curs_col);
4413 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004415 set_cmd_context(&compl_xp, compl_pattern,
4416 (int)STRLEN(compl_pattern), curs_col);
4417 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4418 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004420 startcol = (int)(compl_xp.xp_pattern - compl_pattern);
4421 compl_col = startcol;
4422 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004424 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004425 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00004426#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004427 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00004428 * Call user defined function 'completefunc' with "a:findstart"
4429 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004430 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00004431 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004432 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004433 char_u *funcname;
4434 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004435
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004436 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004437 * string */
4438 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4439 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4440 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004441 {
4442 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
4443 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004444 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004445 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004446
4447 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004448 args[1] = NULL;
4449 pos = curwin->w_cursor;
4450 col = call_func_retnr(funcname, 2, args, FALSE);
4451 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004452
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004453 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004454 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004455 compl_col = col;
4456 if ((colnr_T)compl_col > curs_col)
4457 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004458
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004459 /* Setup variables for completion. Need to obtain "line" again,
4460 * it may have become invalid. */
4461 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004462 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004463 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4464 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004465#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004466 return FAIL;
4467 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00004468 else if (ctrl_x_mode == CTRL_X_SPELL)
4469 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004470#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004471 if (spell_bad_len > 0)
4472 compl_col = curs_col - spell_bad_len;
4473 else
4474 compl_col = spell_word_start(startcol);
4475 if (compl_col >= (colnr_T)startcol)
Bram Moolenaar488c6512005-08-11 20:09:58 +00004476 return FAIL;
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00004477 spell_expand_check_cap(compl_col);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004478 compl_length = (int)curs_col - compl_col;
4479 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4480 if (compl_pattern == NULL)
4481#endif
4482 return FAIL;
4483 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004484 else
4485 {
4486 EMSG2(_(e_intern2), "ins_complete()");
4487 return FAIL;
4488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004490 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 {
4492 edit_submode_pre = (char_u *)_(" Adding");
4493 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4494 {
4495 /* Insert a new line, keep indentation but ignore 'comments' */
4496#ifdef FEAT_COMMENTS
4497 char_u *old = curbuf->b_p_com;
4498
4499 curbuf->b_p_com = (char_u *)"";
4500#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004501 compl_startpos.lnum = curwin->w_cursor.lnum;
4502 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 ins_eol('\r');
4504#ifdef FEAT_COMMENTS
4505 curbuf->b_p_com = old;
4506#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004507 compl_length = 0;
4508 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 }
4510 }
4511 else
4512 {
4513 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004514 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 }
4516
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004517 if (compl_cont_status & CONT_LOCAL)
4518 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 else
4520 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4521
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00004522 /* Always add completion for the original text. */
4523 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004524 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4525 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaar39f05632006-03-19 22:15:26 +00004526 -1, FALSE, NULL, NULL, 0, ORIGINAL_TEXT) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004528 vim_free(compl_pattern);
4529 compl_pattern = NULL;
4530 vim_free(compl_orig_text);
4531 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 return FAIL;
4533 }
4534
4535 /* showmode might reset the internal line pointers, so it must
4536 * be called before line = ml_get(), or when this address is no
4537 * longer needed. -- Acevedo.
4538 */
4539 edit_submode_extra = (char_u *)_("-- Searching...");
4540 edit_submode_highl = HLF_COUNT;
4541 showmode();
4542 edit_submode_extra = NULL;
4543 out_flush();
4544 }
4545
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004546 compl_shown_match = compl_curr_match;
4547 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548
4549 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004550 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004552 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004554 /* may undisplay the popup menu */
4555 ins_compl_upd_pum();
4556
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004557 if (n > 1) /* all matches have been found */
4558 compl_matches = n;
4559 compl_curr_match = compl_shown_match;
4560 compl_direction = compl_shows_dir;
4561 compl_interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562
4563 /* eat the ESC to avoid leaving insert mode */
4564 if (got_int && !global_busy)
4565 {
4566 (void)vgetc();
4567 got_int = FALSE;
4568 }
4569
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004570 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004571 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004573 edit_submode_extra = (compl_cont_status & CONT_ADDING)
4574 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
4576 edit_submode_highl = HLF_E;
4577 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4578 * because we couldn't expand anything at first place, but if we used
4579 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4580 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004581 if ( compl_length > 1
4582 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 || (ctrl_x_mode != 0
4584 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
4585 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004586 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 }
4588
Bram Moolenaar572cb562005-08-05 21:35:02 +00004589 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004590 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004592 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593
4594 if (edit_submode_extra == NULL)
4595 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004596 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 {
4598 edit_submode_extra = (char_u *)_("Back at original");
4599 edit_submode_highl = HLF_W;
4600 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004601 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 {
4603 edit_submode_extra = (char_u *)_("Word from other line");
4604 edit_submode_highl = HLF_COUNT;
4605 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004606 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 {
4608 edit_submode_extra = (char_u *)_("The only match");
4609 edit_submode_highl = HLF_COUNT;
4610 }
4611 else
4612 {
4613 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004614 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004616 int number = 0;
4617 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004619 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 {
4621 /* search backwards for the first valid (!= -1) number.
4622 * This should normally succeed already at the first loop
4623 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004624 for (match = compl_curr_match->cp_prev; match != NULL
4625 && match != compl_first_match;
4626 match = match->cp_prev)
4627 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004629 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 break;
4631 }
4632 if (match != NULL)
4633 /* go up and assign all numbers which are not assigned
4634 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004635 for (match = match->cp_next;
4636 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004637 match = match->cp_next)
4638 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 }
4640 else /* BACKWARD */
4641 {
4642 /* search forwards (upwards) for the first valid (!= -1)
4643 * number. This should normally succeed already at the
4644 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004645 for (match = compl_curr_match->cp_next; match != NULL
4646 && match != compl_first_match;
4647 match = match->cp_next)
4648 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004650 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 break;
4652 }
4653 if (match != NULL)
4654 /* go down and assign all numbers which are not
4655 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004656 for (match = match->cp_prev; match
4657 && match->cp_number == -1;
4658 match = match->cp_prev)
4659 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 }
4661 }
4662
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004663 /* The match should always have a sequence number now, this is
4664 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004665 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 {
4667 /* Space for 10 text chars. + 2x10-digit no.s */
4668 static char_u match_ref[31];
4669
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004670 if (compl_matches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 sprintf((char *)IObuff, _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004672 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004674 sprintf((char *)IObuff, _("match %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00004675 compl_curr_match->cp_number);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004676 vim_strncpy(match_ref, IObuff, 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 edit_submode_extra = match_ref;
4678 edit_submode_highl = HLF_R;
4679 if (dollar_vcol)
4680 curs_columns(FALSE);
4681 }
4682 }
4683 }
4684
4685 /* Show a message about what (completion) mode we're in. */
4686 showmode();
4687 if (edit_submode_extra != NULL)
4688 {
4689 if (!p_smd)
4690 msg_attr(edit_submode_extra,
4691 edit_submode_highl < HLF_COUNT
4692 ? hl_attr(edit_submode_highl) : 0);
4693 }
4694 else
4695 msg_clr_cmdline(); /* necessary for "noshowmode" */
4696
Bram Moolenaara94bc432006-03-10 21:42:59 +00004697 /* RedrawingDisabled may be set when invoked through complete(). */
4698 n = RedrawingDisabled;
4699 RedrawingDisabled = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004700 ins_compl_show_pum();
Bram Moolenaara94bc432006-03-10 21:42:59 +00004701 setcursor();
4702 RedrawingDisabled = n;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004703
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 return OK;
4705}
4706
4707/*
4708 * Looks in the first "len" chars. of "src" for search-metachars.
4709 * If dest is not NULL the chars. are copied there quoting (with
4710 * a backslash) the metachars, and dest would be NUL terminated.
4711 * Returns the length (needed) of dest
4712 */
4713 static int
4714quote_meta(dest, src, len)
4715 char_u *dest;
4716 char_u *src;
4717 int len;
4718{
4719 int m;
4720
4721 for (m = len; --len >= 0; src++)
4722 {
4723 switch (*src)
4724 {
4725 case '.':
4726 case '*':
4727 case '[':
4728 if (ctrl_x_mode == CTRL_X_DICTIONARY
4729 || ctrl_x_mode == CTRL_X_THESAURUS)
4730 break;
4731 case '~':
4732 if (!p_magic) /* quote these only if magic is set */
4733 break;
4734 case '\\':
4735 if (ctrl_x_mode == CTRL_X_DICTIONARY
4736 || ctrl_x_mode == CTRL_X_THESAURUS)
4737 break;
4738 case '^': /* currently it's not needed. */
4739 case '$':
4740 m++;
4741 if (dest != NULL)
4742 *dest++ = '\\';
4743 break;
4744 }
4745 if (dest != NULL)
4746 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004747# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 /* Copy remaining bytes of a multibyte character. */
4749 if (has_mbyte)
4750 {
4751 int i, mb_len;
4752
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004753 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 if (mb_len > 0 && len >= mb_len)
4755 for (i = 0; i < mb_len; ++i)
4756 {
4757 --len;
4758 ++src;
4759 if (dest != NULL)
4760 *dest++ = *src;
4761 }
4762 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00004763# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 }
4765 if (dest != NULL)
4766 *dest = NUL;
4767
4768 return m;
4769}
4770#endif /* FEAT_INS_EXPAND */
4771
4772/*
4773 * Next character is interpreted literally.
4774 * A one, two or three digit decimal number is interpreted as its byte value.
4775 * If one or two digits are entered, the next character is given to vungetc().
4776 * For Unicode a character > 255 may be returned.
4777 */
4778 int
4779get_literal()
4780{
4781 int cc;
4782 int nc;
4783 int i;
4784 int hex = FALSE;
4785 int octal = FALSE;
4786#ifdef FEAT_MBYTE
4787 int unicode = 0;
4788#endif
4789
4790 if (got_int)
4791 return Ctrl_C;
4792
4793#ifdef FEAT_GUI
4794 /*
4795 * In GUI there is no point inserting the internal code for a special key.
4796 * It is more useful to insert the string "<KEY>" instead. This would
4797 * probably be useful in a text window too, but it would not be
4798 * vi-compatible (maybe there should be an option for it?) -- webb
4799 */
4800 if (gui.in_use)
4801 ++allow_keys;
4802#endif
4803#ifdef USE_ON_FLY_SCROLL
4804 dont_scroll = TRUE; /* disallow scrolling here */
4805#endif
4806 ++no_mapping; /* don't map the next key hits */
4807 cc = 0;
4808 i = 0;
4809 for (;;)
4810 {
4811 do
4812 nc = safe_vgetc();
4813 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
4814 || nc == K_HOR_SCROLLBAR);
4815#ifdef FEAT_CMDL_INFO
4816 if (!(State & CMDLINE)
4817# ifdef FEAT_MBYTE
4818 && MB_BYTE2LEN_CHECK(nc) == 1
4819# endif
4820 )
4821 add_to_showcmd(nc);
4822#endif
4823 if (nc == 'x' || nc == 'X')
4824 hex = TRUE;
4825 else if (nc == 'o' || nc == 'O')
4826 octal = TRUE;
4827#ifdef FEAT_MBYTE
4828 else if (nc == 'u' || nc == 'U')
4829 unicode = nc;
4830#endif
4831 else
4832 {
4833 if (hex
4834#ifdef FEAT_MBYTE
4835 || unicode != 0
4836#endif
4837 )
4838 {
4839 if (!vim_isxdigit(nc))
4840 break;
4841 cc = cc * 16 + hex2nr(nc);
4842 }
4843 else if (octal)
4844 {
4845 if (nc < '0' || nc > '7')
4846 break;
4847 cc = cc * 8 + nc - '0';
4848 }
4849 else
4850 {
4851 if (!VIM_ISDIGIT(nc))
4852 break;
4853 cc = cc * 10 + nc - '0';
4854 }
4855
4856 ++i;
4857 }
4858
4859 if (cc > 255
4860#ifdef FEAT_MBYTE
4861 && unicode == 0
4862#endif
4863 )
4864 cc = 255; /* limit range to 0-255 */
4865 nc = 0;
4866
4867 if (hex) /* hex: up to two chars */
4868 {
4869 if (i >= 2)
4870 break;
4871 }
4872#ifdef FEAT_MBYTE
4873 else if (unicode) /* Unicode: up to four or eight chars */
4874 {
4875 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
4876 break;
4877 }
4878#endif
4879 else if (i >= 3) /* decimal or octal: up to three chars */
4880 break;
4881 }
4882 if (i == 0) /* no number entered */
4883 {
4884 if (nc == K_ZERO) /* NUL is stored as NL */
4885 {
4886 cc = '\n';
4887 nc = 0;
4888 }
4889 else
4890 {
4891 cc = nc;
4892 nc = 0;
4893 }
4894 }
4895
4896 if (cc == 0) /* NUL is stored as NL */
4897 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00004898#ifdef FEAT_MBYTE
4899 if (enc_dbcs && (cc & 0xff) == 0)
4900 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
4901 second byte will cause trouble! */
4902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903
4904 --no_mapping;
4905#ifdef FEAT_GUI
4906 if (gui.in_use)
4907 --allow_keys;
4908#endif
4909 if (nc)
4910 vungetc(nc);
4911 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
4912 return cc;
4913}
4914
4915/*
4916 * Insert character, taking care of special keys and mod_mask
4917 */
4918 static void
4919insert_special(c, allow_modmask, ctrlv)
4920 int c;
4921 int allow_modmask;
4922 int ctrlv; /* c was typed after CTRL-V */
4923{
4924 char_u *p;
4925 int len;
4926
4927 /*
4928 * Special function key, translate into "<Key>". Up to the last '>' is
4929 * inserted with ins_str(), so as not to replace characters in replace
4930 * mode.
4931 * Only use mod_mask for special keys, to avoid things like <S-Space>,
4932 * unless 'allow_modmask' is TRUE.
4933 */
4934#ifdef MACOS
4935 /* Command-key never produces a normal key */
4936 if (mod_mask & MOD_MASK_CMD)
4937 allow_modmask = TRUE;
4938#endif
4939 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
4940 {
4941 p = get_special_key_name(c, mod_mask);
4942 len = (int)STRLEN(p);
4943 c = p[len - 1];
4944 if (len > 2)
4945 {
4946 if (stop_arrow() == FAIL)
4947 return;
4948 p[len - 1] = NUL;
4949 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00004950 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 ctrlv = FALSE;
4952 }
4953 }
4954 if (stop_arrow() == OK)
4955 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
4956}
4957
4958/*
4959 * Special characters in this context are those that need processing other
4960 * than the simple insertion that can be performed here. This includes ESC
4961 * which terminates the insert, and CR/NL which need special processing to
4962 * open up a new line. This routine tries to optimize insertions performed by
4963 * the "redo", "undo" or "put" commands, so it needs to know when it should
4964 * stop and defer processing to the "normal" mechanism.
4965 * '0' and '^' are special, because they can be followed by CTRL-D.
4966 */
4967#ifdef EBCDIC
4968# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
4969#else
4970# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
4971#endif
4972
4973#ifdef FEAT_MBYTE
4974# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
4975#else
4976# define WHITECHAR(cc) vim_iswhite(cc)
4977#endif
4978
4979 void
4980insertchar(c, flags, second_indent)
4981 int c; /* character to insert or NUL */
4982 int flags; /* INSCHAR_FORMAT, etc. */
4983 int second_indent; /* indent for second line if >= 0 */
4984{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 int textwidth;
4986#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990
4991 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
4992 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993
4994 /*
4995 * Try to break the line in two or more pieces when:
4996 * - Always do this if we have been called to do formatting only.
4997 * - Always do this when 'formatoptions' has the 'a' flag and the line
4998 * ends in white space.
4999 * - Otherwise:
5000 * - Don't do this if inserting a blank
5001 * - Don't do this if an existing character is being replaced, unless
5002 * we're in VREPLACE mode.
5003 * - Do this if the cursor is not on the line where insert started
5004 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5005 * before the insert.
5006 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5007 * before 'textwidth'
5008 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005009 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 && ((flags & INSCHAR_FORMAT)
5011 || (!vim_iswhite(c)
5012 && !((State & REPLACE_FLAG)
5013#ifdef FEAT_VREPLACE
5014 && !(State & VREPLACE_FLAG)
5015#endif
5016 && *ml_get_cursor() != NUL)
5017 && (curwin->w_cursor.lnum != Insstart.lnum
5018 || ((!has_format_option(FO_INS_LONG)
5019 || Insstart_textlen <= (colnr_T)textwidth)
5020 && (!fo_ins_blank
5021 || Insstart_blank_vcol <= (colnr_T)textwidth
5022 ))))))
5023 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005024 /* Format with 'formatexpr' when it's set. Use internal formatting
5025 * when 'formatexpr' isn't set or it returns non-zero. */
5026#if defined(FEAT_EVAL)
5027 if (*curbuf->b_p_fex == NUL
5028 || fex_format(curwin->w_cursor.lnum, 1L) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005030 internal_format(textwidth, second_indent, flags, c == NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005032
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 if (c == NUL) /* only formatting was wanted */
5034 return;
5035
5036#ifdef FEAT_COMMENTS
5037 /* Check whether this character should end a comment. */
5038 if (did_ai && (int)c == end_comment_pending)
5039 {
5040 char_u *line;
5041 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5042 int middle_len, end_len;
5043 int i;
5044
5045 /*
5046 * Need to remove existing (middle) comment leader and insert end
5047 * comment leader. First, check what comment leader we can find.
5048 */
5049 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5050 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5051 {
5052 /* Skip middle-comment string */
5053 while (*p && p[-1] != ':') /* find end of middle flags */
5054 ++p;
5055 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5056 /* Don't count trailing white space for middle_len */
5057 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5058 --middle_len;
5059
5060 /* Find the end-comment string */
5061 while (*p && p[-1] != ':') /* find end of end flags */
5062 ++p;
5063 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5064
5065 /* Skip white space before the cursor */
5066 i = curwin->w_cursor.col;
5067 while (--i >= 0 && vim_iswhite(line[i]))
5068 ;
5069 i++;
5070
5071 /* Skip to before the middle leader */
5072 i -= middle_len;
5073
5074 /* Check some expected things before we go on */
5075 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5076 {
5077 /* Backspace over all the stuff we want to replace */
5078 backspace_until_column(i);
5079
5080 /*
5081 * Insert the end-comment string, except for the last
5082 * character, which will get inserted as normal later.
5083 */
5084 ins_bytes_len(lead_end, end_len - 1);
5085 }
5086 }
5087 }
5088 end_comment_pending = NUL;
5089#endif
5090
5091 did_ai = FALSE;
5092#ifdef FEAT_SMARTINDENT
5093 did_si = FALSE;
5094 can_si = FALSE;
5095 can_si_back = FALSE;
5096#endif
5097
5098 /*
5099 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5100 * This speeds up normal text input considerably.
5101 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5102 * need to re-indent at a ':', or any other character (but not what
5103 * 'paste' is set)..
5104 */
5105#ifdef USE_ON_FLY_SCROLL
5106 dont_scroll = FALSE; /* allow scrolling here */
5107#endif
5108
5109 if ( !ISSPECIAL(c)
5110#ifdef FEAT_MBYTE
5111 && (!has_mbyte || (*mb_char2len)(c) == 1)
5112#endif
5113 && vpeekc() != NUL
5114 && !(State & REPLACE_FLAG)
5115#ifdef FEAT_CINDENT
5116 && !cindent_on()
5117#endif
5118#ifdef FEAT_RIGHTLEFT
5119 && !p_ri
5120#endif
5121 )
5122 {
5123#define INPUT_BUFLEN 100
5124 char_u buf[INPUT_BUFLEN + 1];
5125 int i;
5126 colnr_T virtcol = 0;
5127
5128 buf[0] = c;
5129 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005130 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 virtcol = get_nolist_virtcol();
5132 /*
5133 * Stop the string when:
5134 * - no more chars available
5135 * - finding a special character (command key)
5136 * - buffer is full
5137 * - running into the 'textwidth' boundary
5138 * - need to check for abbreviation: A non-word char after a word-char
5139 */
5140 while ( (c = vpeekc()) != NUL
5141 && !ISSPECIAL(c)
5142#ifdef FEAT_MBYTE
5143 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5144#endif
5145 && i < INPUT_BUFLEN
5146 && (textwidth == 0
5147 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5148 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5149 {
5150#ifdef FEAT_RIGHTLEFT
5151 c = vgetc();
5152 if (p_hkmap && KeyTyped)
5153 c = hkmap(c); /* Hebrew mode mapping */
5154# ifdef FEAT_FKMAP
5155 if (p_fkmap && KeyTyped)
5156 c = fkmap(c); /* Farsi mode mapping */
5157# endif
5158 buf[i++] = c;
5159#else
5160 buf[i++] = vgetc();
5161#endif
5162 }
5163
5164#ifdef FEAT_DIGRAPHS
5165 do_digraph(-1); /* clear digraphs */
5166 do_digraph(buf[i-1]); /* may be the start of a digraph */
5167#endif
5168 buf[i] = NUL;
5169 ins_str(buf);
5170 if (flags & INSCHAR_CTRLV)
5171 {
5172 redo_literal(*buf);
5173 i = 1;
5174 }
5175 else
5176 i = 0;
5177 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005178 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 }
5180 else
5181 {
5182#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005183 int cc;
5184
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5186 {
5187 char_u buf[MB_MAXBYTES + 1];
5188
5189 (*mb_char2bytes)(c, buf);
5190 buf[cc] = NUL;
5191 ins_char_bytes(buf, cc);
5192 AppendCharToRedobuff(c);
5193 }
5194 else
5195#endif
5196 {
5197 ins_char(c);
5198 if (flags & INSCHAR_CTRLV)
5199 redo_literal(c);
5200 else
5201 AppendCharToRedobuff(c);
5202 }
5203 }
5204}
5205
5206/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005207 * Format text at the current insert position.
5208 */
5209 static void
5210internal_format(textwidth, second_indent, flags, format_only)
5211 int textwidth;
5212 int second_indent;
5213 int flags;
5214 int format_only;
5215{
5216 int cc;
5217 int save_char = NUL;
5218 int haveto_redraw = FALSE;
5219 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5220#ifdef FEAT_MBYTE
5221 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5222#endif
5223 int fo_white_par = has_format_option(FO_WHITE_PAR);
5224 int first_line = TRUE;
5225#ifdef FEAT_COMMENTS
5226 colnr_T leader_len;
5227 int no_leader = FALSE;
5228 int do_comments = (flags & INSCHAR_DO_COM);
5229#endif
5230
5231 /*
5232 * When 'ai' is off we don't want a space under the cursor to be
5233 * deleted. Replace it with an 'x' temporarily.
5234 */
5235 if (!curbuf->b_p_ai)
5236 {
5237 cc = gchar_cursor();
5238 if (vim_iswhite(cc))
5239 {
5240 save_char = cc;
5241 pchar_cursor('x');
5242 }
5243 }
5244
5245 /*
5246 * Repeat breaking lines, until the current line is not too long.
5247 */
5248 while (!got_int)
5249 {
5250 int startcol; /* Cursor column at entry */
5251 int wantcol; /* column at textwidth border */
5252 int foundcol; /* column for start of spaces */
5253 int end_foundcol = 0; /* column for start of word */
5254 colnr_T len;
5255 colnr_T virtcol;
5256#ifdef FEAT_VREPLACE
5257 int orig_col = 0;
5258 char_u *saved_text = NULL;
5259#endif
5260 colnr_T col;
5261
5262 virtcol = get_nolist_virtcol();
5263 if (virtcol < (colnr_T)textwidth)
5264 break;
5265
5266#ifdef FEAT_COMMENTS
5267 if (no_leader)
5268 do_comments = FALSE;
5269 else if (!(flags & INSCHAR_FORMAT)
5270 && has_format_option(FO_WRAP_COMS))
5271 do_comments = TRUE;
5272
5273 /* Don't break until after the comment leader */
5274 if (do_comments)
5275 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5276 else
5277 leader_len = 0;
5278
5279 /* If the line doesn't start with a comment leader, then don't
5280 * start one in a following broken line. Avoids that a %word
5281 * moved to the start of the next line causes all following lines
5282 * to start with %. */
5283 if (leader_len == 0)
5284 no_leader = TRUE;
5285#endif
5286 if (!(flags & INSCHAR_FORMAT)
5287#ifdef FEAT_COMMENTS
5288 && leader_len == 0
5289#endif
5290 && !has_format_option(FO_WRAP))
5291
5292 {
5293 textwidth = 0;
5294 break;
5295 }
5296 if ((startcol = curwin->w_cursor.col) == 0)
5297 break;
5298
5299 /* find column of textwidth border */
5300 coladvance((colnr_T)textwidth);
5301 wantcol = curwin->w_cursor.col;
5302
5303 curwin->w_cursor.col = startcol - 1;
5304#ifdef FEAT_MBYTE
5305 /* Correct cursor for multi-byte character. */
5306 if (has_mbyte)
5307 mb_adjust_cursor();
5308#endif
5309 foundcol = 0;
5310
5311 /*
5312 * Find position to break at.
5313 * Stop at first entered white when 'formatoptions' has 'v'
5314 */
5315 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5316 || curwin->w_cursor.lnum != Insstart.lnum
5317 || curwin->w_cursor.col >= Insstart.col)
5318 {
5319 cc = gchar_cursor();
5320 if (WHITECHAR(cc))
5321 {
5322 /* remember position of blank just before text */
5323 end_foundcol = curwin->w_cursor.col;
5324
5325 /* find start of sequence of blanks */
5326 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5327 {
5328 dec_cursor();
5329 cc = gchar_cursor();
5330 }
5331 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5332 break; /* only spaces in front of text */
5333#ifdef FEAT_COMMENTS
5334 /* Don't break until after the comment leader */
5335 if (curwin->w_cursor.col < leader_len)
5336 break;
5337#endif
5338 if (has_format_option(FO_ONE_LETTER))
5339 {
5340 /* do not break after one-letter words */
5341 if (curwin->w_cursor.col == 0)
5342 break; /* one-letter word at begin */
5343
5344 col = curwin->w_cursor.col;
5345 dec_cursor();
5346 cc = gchar_cursor();
5347
5348 if (WHITECHAR(cc))
5349 continue; /* one-letter, continue */
5350 curwin->w_cursor.col = col;
5351 }
5352#ifdef FEAT_MBYTE
5353 if (has_mbyte)
5354 foundcol = curwin->w_cursor.col
5355 + (*mb_ptr2len)(ml_get_cursor());
5356 else
5357#endif
5358 foundcol = curwin->w_cursor.col + 1;
5359 if (curwin->w_cursor.col < (colnr_T)wantcol)
5360 break;
5361 }
5362#ifdef FEAT_MBYTE
5363 else if (cc >= 0x100 && fo_multibyte
5364 && curwin->w_cursor.col <= (colnr_T)wantcol)
5365 {
5366 /* Break after or before a multi-byte character. */
5367 foundcol = curwin->w_cursor.col;
5368 if (curwin->w_cursor.col < (colnr_T)wantcol)
5369 foundcol += (*mb_char2len)(cc);
5370 end_foundcol = foundcol;
5371 break;
5372 }
5373#endif
5374 if (curwin->w_cursor.col == 0)
5375 break;
5376 dec_cursor();
5377 }
5378
5379 if (foundcol == 0) /* no spaces, cannot break line */
5380 {
5381 curwin->w_cursor.col = startcol;
5382 break;
5383 }
5384
5385 /* Going to break the line, remove any "$" now. */
5386 undisplay_dollar();
5387
5388 /*
5389 * Offset between cursor position and line break is used by replace
5390 * stack functions. VREPLACE does not use this, and backspaces
5391 * over the text instead.
5392 */
5393#ifdef FEAT_VREPLACE
5394 if (State & VREPLACE_FLAG)
5395 orig_col = startcol; /* Will start backspacing from here */
5396 else
5397#endif
5398 replace_offset = startcol - end_foundcol - 1;
5399
5400 /*
5401 * adjust startcol for spaces that will be deleted and
5402 * characters that will remain on top line
5403 */
5404 curwin->w_cursor.col = foundcol;
5405 while (cc = gchar_cursor(), WHITECHAR(cc))
5406 inc_cursor();
5407 startcol -= curwin->w_cursor.col;
5408 if (startcol < 0)
5409 startcol = 0;
5410
5411#ifdef FEAT_VREPLACE
5412 if (State & VREPLACE_FLAG)
5413 {
5414 /*
5415 * In VREPLACE mode, we will backspace over the text to be
5416 * wrapped, so save a copy now to put on the next line.
5417 */
5418 saved_text = vim_strsave(ml_get_cursor());
5419 curwin->w_cursor.col = orig_col;
5420 if (saved_text == NULL)
5421 break; /* Can't do it, out of memory */
5422 saved_text[startcol] = NUL;
5423
5424 /* Backspace over characters that will move to the next line */
5425 if (!fo_white_par)
5426 backspace_until_column(foundcol);
5427 }
5428 else
5429#endif
5430 {
5431 /* put cursor after pos. to break line */
5432 if (!fo_white_par)
5433 curwin->w_cursor.col = foundcol;
5434 }
5435
5436 /*
5437 * Split the line just before the margin.
5438 * Only insert/delete lines, but don't really redraw the window.
5439 */
5440 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
5441 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
5442#ifdef FEAT_COMMENTS
5443 + (do_comments ? OPENLINE_DO_COM : 0)
5444#endif
5445 , old_indent);
5446 old_indent = 0;
5447
5448 replace_offset = 0;
5449 if (first_line)
5450 {
5451 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
5452 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
5453 if (second_indent >= 0)
5454 {
5455#ifdef FEAT_VREPLACE
5456 if (State & VREPLACE_FLAG)
5457 change_indent(INDENT_SET, second_indent, FALSE, NUL);
5458 else
5459#endif
5460 (void)set_indent(second_indent, SIN_CHANGED);
5461 }
5462 first_line = FALSE;
5463 }
5464
5465#ifdef FEAT_VREPLACE
5466 if (State & VREPLACE_FLAG)
5467 {
5468 /*
5469 * In VREPLACE mode we have backspaced over the text to be
5470 * moved, now we re-insert it into the new line.
5471 */
5472 ins_bytes(saved_text);
5473 vim_free(saved_text);
5474 }
5475 else
5476#endif
5477 {
5478 /*
5479 * Check if cursor is not past the NUL off the line, cindent
5480 * may have added or removed indent.
5481 */
5482 curwin->w_cursor.col += startcol;
5483 len = (colnr_T)STRLEN(ml_get_curline());
5484 if (curwin->w_cursor.col > len)
5485 curwin->w_cursor.col = len;
5486 }
5487
5488 haveto_redraw = TRUE;
5489#ifdef FEAT_CINDENT
5490 can_cindent = TRUE;
5491#endif
5492 /* moved the cursor, don't autoindent or cindent now */
5493 did_ai = FALSE;
5494#ifdef FEAT_SMARTINDENT
5495 did_si = FALSE;
5496 can_si = FALSE;
5497 can_si_back = FALSE;
5498#endif
5499 line_breakcheck();
5500 }
5501
5502 if (save_char != NUL) /* put back space after cursor */
5503 pchar_cursor(save_char);
5504
5505 if (!format_only && haveto_redraw)
5506 {
5507 update_topline();
5508 redraw_curbuf_later(VALID);
5509 }
5510}
5511
5512/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 * Called after inserting or deleting text: When 'formatoptions' includes the
5514 * 'a' flag format from the current line until the end of the paragraph.
5515 * Keep the cursor at the same position relative to the text.
5516 * The caller must have saved the cursor line for undo, following ones will be
5517 * saved here.
5518 */
5519 void
5520auto_format(trailblank, prev_line)
5521 int trailblank; /* when TRUE also format with trailing blank */
5522 int prev_line; /* may start in previous line */
5523{
5524 pos_T pos;
5525 colnr_T len;
5526 char_u *old;
5527 char_u *new, *pnew;
5528 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005529 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530
5531 if (!has_format_option(FO_AUTO))
5532 return;
5533
5534 pos = curwin->w_cursor;
5535 old = ml_get_curline();
5536
5537 /* may remove added space */
5538 check_auto_format(FALSE);
5539
5540 /* Don't format in Insert mode when the cursor is on a trailing blank, the
5541 * user might insert normal text next. Also skip formatting when "1" is
5542 * in 'formatoptions' and there is a single character before the cursor.
5543 * Otherwise the line would be broken and when typing another non-white
5544 * next they are not joined back together. */
5545 wasatend = (pos.col == STRLEN(old));
5546 if (*old != NUL && !trailblank && wasatend)
5547 {
5548 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005549 cc = gchar_cursor();
5550 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
5551 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005553 cc = gchar_cursor();
5554 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 {
5556 curwin->w_cursor = pos;
5557 return;
5558 }
5559 curwin->w_cursor = pos;
5560 }
5561
5562#ifdef FEAT_COMMENTS
5563 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
5564 * comments. */
5565 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
5566 && get_leader_len(old, NULL, FALSE) == 0)
5567 return;
5568#endif
5569
5570 /*
5571 * May start formatting in a previous line, so that after "x" a word is
5572 * moved to the previous line if it fits there now. Only when this is not
5573 * the start of a paragraph.
5574 */
5575 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
5576 {
5577 --curwin->w_cursor.lnum;
5578 if (u_save_cursor() == FAIL)
5579 return;
5580 }
5581
5582 /*
5583 * Do the formatting and restore the cursor position. "saved_cursor" will
5584 * be adjusted for the text formatting.
5585 */
5586 saved_cursor = pos;
5587 format_lines((linenr_T)-1);
5588 curwin->w_cursor = saved_cursor;
5589 saved_cursor.lnum = 0;
5590
5591 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5592 {
5593 /* "cannot happen" */
5594 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5595 coladvance((colnr_T)MAXCOL);
5596 }
5597 else
5598 check_cursor_col();
5599
5600 /* Insert mode: If the cursor is now after the end of the line while it
5601 * previously wasn't, the line was broken. Because of the rule above we
5602 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
5603 * formatted. */
5604 if (!wasatend && has_format_option(FO_WHITE_PAR))
5605 {
5606 new = ml_get_curline();
5607 len = STRLEN(new);
5608 if (curwin->w_cursor.col == len)
5609 {
5610 pnew = vim_strnsave(new, len + 2);
5611 pnew[len] = ' ';
5612 pnew[len + 1] = NUL;
5613 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
5614 /* remove the space later */
5615 did_add_space = TRUE;
5616 }
5617 else
5618 /* may remove added space */
5619 check_auto_format(FALSE);
5620 }
5621
5622 check_cursor();
5623}
5624
5625/*
5626 * When an extra space was added to continue a paragraph for auto-formatting,
5627 * delete it now. The space must be under the cursor, just after the insert
5628 * position.
5629 */
5630 static void
5631check_auto_format(end_insert)
5632 int end_insert; /* TRUE when ending Insert mode */
5633{
5634 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005635 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636
5637 if (did_add_space)
5638 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005639 cc = gchar_cursor();
5640 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 /* Somehow the space was removed already. */
5642 did_add_space = FALSE;
5643 else
5644 {
5645 if (!end_insert)
5646 {
5647 inc_cursor();
5648 c = gchar_cursor();
5649 dec_cursor();
5650 }
5651 if (c != NUL)
5652 {
5653 /* The space is no longer at the end of the line, delete it. */
5654 del_char(FALSE);
5655 did_add_space = FALSE;
5656 }
5657 }
5658 }
5659}
5660
5661/*
5662 * Find out textwidth to be used for formatting:
5663 * if 'textwidth' option is set, use it
5664 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
5665 * if invalid value, use 0.
5666 * Set default to window width (maximum 79) for "gq" operator.
5667 */
5668 int
5669comp_textwidth(ff)
5670 int ff; /* force formatting (for "Q" command) */
5671{
5672 int textwidth;
5673
5674 textwidth = curbuf->b_p_tw;
5675 if (textwidth == 0 && curbuf->b_p_wm)
5676 {
5677 /* The width is the window width minus 'wrapmargin' minus all the
5678 * things that add to the margin. */
5679 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
5680#ifdef FEAT_CMDWIN
5681 if (cmdwin_type != 0)
5682 textwidth -= 1;
5683#endif
5684#ifdef FEAT_FOLDING
5685 textwidth -= curwin->w_p_fdc;
5686#endif
5687#ifdef FEAT_SIGNS
5688 if (curwin->w_buffer->b_signlist != NULL
5689# ifdef FEAT_NETBEANS_INTG
5690 || usingNetbeans
5691# endif
5692 )
5693 textwidth -= 1;
5694#endif
5695 if (curwin->w_p_nu)
5696 textwidth -= 8;
5697 }
5698 if (textwidth < 0)
5699 textwidth = 0;
5700 if (ff && textwidth == 0)
5701 {
5702 textwidth = W_WIDTH(curwin) - 1;
5703 if (textwidth > 79)
5704 textwidth = 79;
5705 }
5706 return textwidth;
5707}
5708
5709/*
5710 * Put a character in the redo buffer, for when just after a CTRL-V.
5711 */
5712 static void
5713redo_literal(c)
5714 int c;
5715{
5716 char_u buf[10];
5717
5718 /* Only digits need special treatment. Translate them into a string of
5719 * three digits. */
5720 if (VIM_ISDIGIT(c))
5721 {
5722 sprintf((char *)buf, "%03d", c);
5723 AppendToRedobuff(buf);
5724 }
5725 else
5726 AppendCharToRedobuff(c);
5727}
5728
5729/*
5730 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005731 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 */
5733 static void
5734start_arrow(end_insert_pos)
5735 pos_T *end_insert_pos;
5736{
5737 if (!arrow_used) /* something has been inserted */
5738 {
5739 AppendToRedobuff(ESC_STR);
5740 stop_insert(end_insert_pos, FALSE);
5741 arrow_used = TRUE; /* this means we stopped the current insert */
5742 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005743#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005744 check_spell_redraw();
5745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746}
5747
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005748#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005749/*
5750 * If we skipped highlighting word at cursor, do it now.
5751 * It may be skipped again, thus reset spell_redraw_lnum first.
5752 */
5753 static void
5754check_spell_redraw()
5755{
5756 if (spell_redraw_lnum != 0)
5757 {
5758 linenr_T lnum = spell_redraw_lnum;
5759
5760 spell_redraw_lnum = 0;
5761 redrawWinline(lnum, FALSE);
5762 }
5763}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005764
5765/*
5766 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
5767 * spelled word, if there is one.
5768 */
5769 static void
5770spell_back_to_badword()
5771{
5772 pos_T tpos = curwin->w_cursor;
5773
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00005774 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00005775 if (curwin->w_cursor.col != tpos.col)
5776 start_arrow(&tpos);
5777}
Bram Moolenaar217ad922005-03-20 22:37:15 +00005778#endif
5779
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780/*
5781 * stop_arrow() is called before a change is made in insert mode.
5782 * If an arrow key has been used, start a new insertion.
5783 * Returns FAIL if undo is impossible, shouldn't insert then.
5784 */
5785 int
5786stop_arrow()
5787{
5788 if (arrow_used)
5789 {
5790 if (u_save_cursor() == OK)
5791 {
5792 arrow_used = FALSE;
5793 ins_need_undo = FALSE;
5794 }
5795 Insstart = curwin->w_cursor; /* new insertion starts here */
5796 Insstart_textlen = linetabsize(ml_get_curline());
5797 ai_col = 0;
5798#ifdef FEAT_VREPLACE
5799 if (State & VREPLACE_FLAG)
5800 {
5801 orig_line_count = curbuf->b_ml.ml_line_count;
5802 vr_lines_changed = 1;
5803 }
5804#endif
5805 ResetRedobuff();
5806 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00005807 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 }
5809 else if (ins_need_undo)
5810 {
5811 if (u_save_cursor() == OK)
5812 ins_need_undo = FALSE;
5813 }
5814
5815#ifdef FEAT_FOLDING
5816 /* Always open fold at the cursor line when inserting something. */
5817 foldOpenCursor();
5818#endif
5819
5820 return (arrow_used || ins_need_undo ? FAIL : OK);
5821}
5822
5823/*
5824 * do a few things to stop inserting
5825 */
5826 static void
5827stop_insert(end_insert_pos, esc)
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005828 pos_T *end_insert_pos; /* where insert ended */
5829 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005831 int cc;
5832 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833
5834 stop_redo_ins();
5835 replace_flush(); /* abandon replace stack */
5836
5837 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005838 * Save the inserted text for later redo with ^@ and CTRL-A.
5839 * Don't do it when "restart_edit" was set and nothing was inserted,
5840 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005842 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00005843 if (did_restart_edit == 0 || (ptr != NULL
5844 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00005845 {
5846 vim_free(last_insert);
5847 last_insert = ptr;
5848 last_insert_skip = new_insert_skip;
5849 }
5850 else
5851 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852
5853 if (!arrow_used)
5854 {
5855 /* Auto-format now. It may seem strange to do this when stopping an
5856 * insertion (or moving the cursor), but it's required when appending
5857 * a line and having it end in a space. But only do it when something
5858 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005859 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005861 pos_T tpos = curwin->w_cursor;
5862
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 /* When the cursor is at the end of the line after a space the
5864 * formatting will move it to the following word. Avoid that by
5865 * moving the cursor onto the space. */
5866 cc = 'x';
5867 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
5868 {
5869 dec_cursor();
5870 cc = gchar_cursor();
5871 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005872 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 }
5874
5875 auto_format(TRUE, FALSE);
5876
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005877 if (vim_iswhite(cc))
5878 {
5879 if (gchar_cursor() != NUL)
5880 inc_cursor();
5881#ifdef FEAT_VIRTUALEDIT
5882 /* If the cursor is still at the same character, also keep
5883 * the "coladd". */
5884 if (gchar_cursor() == NUL
5885 && curwin->w_cursor.lnum == tpos.lnum
5886 && curwin->w_cursor.col == tpos.col)
5887 curwin->w_cursor.coladd = tpos.coladd;
5888#endif
5889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 }
5891
5892 /* If a space was inserted for auto-formatting, remove it now. */
5893 check_auto_format(TRUE);
5894
5895 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005896 * of the line, and put the cursor back.
5897 * Do this when ESC was used or moving the cursor up/down. */
5898 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
5899 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005901 pos_T tpos = curwin->w_cursor;
5902
5903 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar39f05632006-03-19 22:15:26 +00005904 for (;;)
5905 {
5906 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
5907 --curwin->w_cursor.col;
5908 cc = gchar_cursor();
5909 if (!vim_iswhite(cc))
5910 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 (void)del_char(TRUE);
Bram Moolenaar39f05632006-03-19 22:15:26 +00005912 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005913 if (curwin->w_cursor.lnum != tpos.lnum)
5914 curwin->w_cursor = tpos;
5915 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 ++curwin->w_cursor.col; /* put cursor back on the NUL */
5917
5918#ifdef FEAT_VISUAL
5919 /* <C-S-Right> may have started Visual mode, adjust the position for
5920 * deleted characters. */
5921 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
5922 {
5923 cc = STRLEN(ml_get_curline());
5924 if (VIsual.col > (colnr_T)cc)
5925 {
5926 VIsual.col = cc;
5927# ifdef FEAT_VIRTUALEDIT
5928 VIsual.coladd = 0;
5929# endif
5930 }
5931 }
5932#endif
5933 }
5934 }
5935 did_ai = FALSE;
5936#ifdef FEAT_SMARTINDENT
5937 did_si = FALSE;
5938 can_si = FALSE;
5939 can_si_back = FALSE;
5940#endif
5941
5942 /* set '[ and '] to the inserted text */
5943 curbuf->b_op_start = Insstart;
5944 curbuf->b_op_end = *end_insert_pos;
5945}
5946
5947/*
5948 * Set the last inserted text to a single character.
5949 * Used for the replace command.
5950 */
5951 void
5952set_last_insert(c)
5953 int c;
5954{
5955 char_u *s;
5956
5957 vim_free(last_insert);
5958#ifdef FEAT_MBYTE
5959 last_insert = alloc(MB_MAXBYTES * 3 + 5);
5960#else
5961 last_insert = alloc(6);
5962#endif
5963 if (last_insert != NULL)
5964 {
5965 s = last_insert;
5966 /* Use the CTRL-V only when entering a special char */
5967 if (c < ' ' || c == DEL)
5968 *s++ = Ctrl_V;
5969 s = add_char2buf(c, s);
5970 *s++ = ESC;
5971 *s++ = NUL;
5972 last_insert_skip = 0;
5973 }
5974}
5975
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005976#if defined(EXITFREE) || defined(PROTO)
5977 void
5978free_last_insert()
5979{
5980 vim_free(last_insert);
5981 last_insert = NULL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005982 vim_free(compl_orig_text);
5983 compl_orig_text = NULL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00005984}
5985#endif
5986
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987/*
5988 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
5989 * and CSI. Handle multi-byte characters.
5990 * Returns a pointer to after the added bytes.
5991 */
5992 char_u *
5993add_char2buf(c, s)
5994 int c;
5995 char_u *s;
5996{
5997#ifdef FEAT_MBYTE
5998 char_u temp[MB_MAXBYTES];
5999 int i;
6000 int len;
6001
6002 len = (*mb_char2bytes)(c, temp);
6003 for (i = 0; i < len; ++i)
6004 {
6005 c = temp[i];
6006#endif
6007 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6008 if (c == K_SPECIAL)
6009 {
6010 *s++ = K_SPECIAL;
6011 *s++ = KS_SPECIAL;
6012 *s++ = KE_FILLER;
6013 }
6014#ifdef FEAT_GUI
6015 else if (c == CSI)
6016 {
6017 *s++ = CSI;
6018 *s++ = KS_EXTRA;
6019 *s++ = (int)KE_CSI;
6020 }
6021#endif
6022 else
6023 *s++ = c;
6024#ifdef FEAT_MBYTE
6025 }
6026#endif
6027 return s;
6028}
6029
6030/*
6031 * move cursor to start of line
6032 * if flags & BL_WHITE move to first non-white
6033 * if flags & BL_SOL move to first non-white if startofline is set,
6034 * otherwise keep "curswant" column
6035 * if flags & BL_FIX don't leave the cursor on a NUL.
6036 */
6037 void
6038beginline(flags)
6039 int flags;
6040{
6041 if ((flags & BL_SOL) && !p_sol)
6042 coladvance(curwin->w_curswant);
6043 else
6044 {
6045 curwin->w_cursor.col = 0;
6046#ifdef FEAT_VIRTUALEDIT
6047 curwin->w_cursor.coladd = 0;
6048#endif
6049
6050 if (flags & (BL_WHITE | BL_SOL))
6051 {
6052 char_u *ptr;
6053
6054 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6055 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6056 ++curwin->w_cursor.col;
6057 }
6058 curwin->w_set_curswant = TRUE;
6059 }
6060}
6061
6062/*
6063 * oneright oneleft cursor_down cursor_up
6064 *
6065 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006066 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 * Return OK when successful, FAIL when we hit a line of file boundary.
6068 */
6069
6070 int
6071oneright()
6072{
6073 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075
6076#ifdef FEAT_VIRTUALEDIT
6077 if (virtual_active())
6078 {
6079 pos_T prevpos = curwin->w_cursor;
6080
6081 /* Adjust for multi-wide char (excluding TAB) */
6082 ptr = ml_get_cursor();
6083 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006084# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006086# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006088# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 ))
6090 ? ptr2cells(ptr) : 1));
6091 curwin->w_set_curswant = TRUE;
6092 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6093 return (prevpos.col != curwin->w_cursor.col
6094 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
6095 }
6096#endif
6097
6098 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006099 if (*ptr == NUL)
6100 return FAIL; /* already at the very end */
6101
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006103 if (has_mbyte)
6104 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 else
6106#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006107 l = 1;
6108
6109 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6110 * contains "onemore". */
6111 if (ptr[l] == NUL
6112#ifdef FEAT_VIRTUALEDIT
6113 && (ve_flags & VE_ONEMORE) == 0
6114#endif
6115 )
6116 return FAIL;
6117 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118
6119 curwin->w_set_curswant = TRUE;
6120 return OK;
6121}
6122
6123 int
6124oneleft()
6125{
6126#ifdef FEAT_VIRTUALEDIT
6127 if (virtual_active())
6128 {
6129 int width;
6130 int v = getviscol();
6131
6132 if (v == 0)
6133 return FAIL;
6134
6135# ifdef FEAT_LINEBREAK
6136 /* We might get stuck on 'showbreak', skip over it. */
6137 width = 1;
6138 for (;;)
6139 {
6140 coladvance(v - width);
6141 /* getviscol() is slow, skip it when 'showbreak' is empty and
6142 * there are no multi-byte characters */
6143 if ((*p_sbr == NUL
6144# ifdef FEAT_MBYTE
6145 && !has_mbyte
6146# endif
6147 ) || getviscol() < v)
6148 break;
6149 ++width;
6150 }
6151# else
6152 coladvance(v - 1);
6153# endif
6154
6155 if (curwin->w_cursor.coladd == 1)
6156 {
6157 char_u *ptr;
6158
6159 /* Adjust for multi-wide char (not a TAB) */
6160 ptr = ml_get_cursor();
6161 if (*ptr != TAB && vim_isprintc(
6162# ifdef FEAT_MBYTE
6163 (*mb_ptr2char)(ptr)
6164# else
6165 *ptr
6166# endif
6167 ) && ptr2cells(ptr) > 1)
6168 curwin->w_cursor.coladd = 0;
6169 }
6170
6171 curwin->w_set_curswant = TRUE;
6172 return OK;
6173 }
6174#endif
6175
6176 if (curwin->w_cursor.col == 0)
6177 return FAIL;
6178
6179 curwin->w_set_curswant = TRUE;
6180 --curwin->w_cursor.col;
6181
6182#ifdef FEAT_MBYTE
6183 /* if the character on the left of the current cursor is a multi-byte
6184 * character, move to its first byte */
6185 if (has_mbyte)
6186 mb_adjust_cursor();
6187#endif
6188 return OK;
6189}
6190
6191 int
6192cursor_up(n, upd_topline)
6193 long n;
6194 int upd_topline; /* When TRUE: update topline */
6195{
6196 linenr_T lnum;
6197
6198 if (n > 0)
6199 {
6200 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006201 /* This fails if the cursor is already in the first line or the count
6202 * is larger than the line number and '-' is in 'cpoptions' */
6203 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 return FAIL;
6205 if (n >= lnum)
6206 lnum = 1;
6207 else
6208#ifdef FEAT_FOLDING
6209 if (hasAnyFolding(curwin))
6210 {
6211 /*
6212 * Count each sequence of folded lines as one logical line.
6213 */
6214 /* go to the the start of the current fold */
6215 (void)hasFolding(lnum, &lnum, NULL);
6216
6217 while (n--)
6218 {
6219 /* move up one line */
6220 --lnum;
6221 if (lnum <= 1)
6222 break;
6223 /* If we entered a fold, move to the beginning, unless in
6224 * Insert mode or when 'foldopen' contains "all": it will open
6225 * in a moment. */
6226 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6227 (void)hasFolding(lnum, &lnum, NULL);
6228 }
6229 if (lnum < 1)
6230 lnum = 1;
6231 }
6232 else
6233#endif
6234 lnum -= n;
6235 curwin->w_cursor.lnum = lnum;
6236 }
6237
6238 /* try to advance to the column we want to be at */
6239 coladvance(curwin->w_curswant);
6240
6241 if (upd_topline)
6242 update_topline(); /* make sure curwin->w_topline is valid */
6243
6244 return OK;
6245}
6246
6247/*
6248 * Cursor down a number of logical lines.
6249 */
6250 int
6251cursor_down(n, upd_topline)
6252 long n;
6253 int upd_topline; /* When TRUE: update topline */
6254{
6255 linenr_T lnum;
6256
6257 if (n > 0)
6258 {
6259 lnum = curwin->w_cursor.lnum;
6260#ifdef FEAT_FOLDING
6261 /* Move to last line of fold, will fail if it's the end-of-file. */
6262 (void)hasFolding(lnum, NULL, &lnum);
6263#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006264 /* This fails if the cursor is already in the last line or would move
6265 * beyound the last line and '-' is in 'cpoptions' */
6266 if (lnum >= curbuf->b_ml.ml_line_count
6267 || (lnum + n > curbuf->b_ml.ml_line_count
6268 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 return FAIL;
6270 if (lnum + n >= curbuf->b_ml.ml_line_count)
6271 lnum = curbuf->b_ml.ml_line_count;
6272 else
6273#ifdef FEAT_FOLDING
6274 if (hasAnyFolding(curwin))
6275 {
6276 linenr_T last;
6277
6278 /* count each sequence of folded lines as one logical line */
6279 while (n--)
6280 {
6281 if (hasFolding(lnum, NULL, &last))
6282 lnum = last + 1;
6283 else
6284 ++lnum;
6285 if (lnum >= curbuf->b_ml.ml_line_count)
6286 break;
6287 }
6288 if (lnum > curbuf->b_ml.ml_line_count)
6289 lnum = curbuf->b_ml.ml_line_count;
6290 }
6291 else
6292#endif
6293 lnum += n;
6294 curwin->w_cursor.lnum = lnum;
6295 }
6296
6297 /* try to advance to the column we want to be at */
6298 coladvance(curwin->w_curswant);
6299
6300 if (upd_topline)
6301 update_topline(); /* make sure curwin->w_topline is valid */
6302
6303 return OK;
6304}
6305
6306/*
6307 * Stuff the last inserted text in the read buffer.
6308 * Last_insert actually is a copy of the redo buffer, so we
6309 * first have to remove the command.
6310 */
6311 int
6312stuff_inserted(c, count, no_esc)
6313 int c; /* Command character to be inserted */
6314 long count; /* Repeat this many times */
6315 int no_esc; /* Don't add an ESC at the end */
6316{
6317 char_u *esc_ptr;
6318 char_u *ptr;
6319 char_u *last_ptr;
6320 char_u last = NUL;
6321
6322 ptr = get_last_insert();
6323 if (ptr == NULL)
6324 {
6325 EMSG(_(e_noinstext));
6326 return FAIL;
6327 }
6328
6329 /* may want to stuff the command character, to start Insert mode */
6330 if (c != NUL)
6331 stuffcharReadbuff(c);
6332 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
6333 *esc_ptr = NUL; /* remove the ESC */
6334
6335 /* when the last char is either "0" or "^" it will be quoted if no ESC
6336 * comes after it OR if it will inserted more than once and "ptr"
6337 * starts with ^D. -- Acevedo
6338 */
6339 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
6340 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
6341 && (no_esc || (*ptr == Ctrl_D && count > 1)))
6342 {
6343 last = *last_ptr;
6344 *last_ptr = NUL;
6345 }
6346
6347 do
6348 {
6349 stuffReadbuff(ptr);
6350 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6351 if (last)
6352 stuffReadbuff((char_u *)(last == '0'
6353 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
6354 : IF_EB("\026^", CTRL_V_STR "^")));
6355 }
6356 while (--count > 0);
6357
6358 if (last)
6359 *last_ptr = last;
6360
6361 if (esc_ptr != NULL)
6362 *esc_ptr = ESC; /* put the ESC back */
6363
6364 /* may want to stuff a trailing ESC, to get out of Insert mode */
6365 if (!no_esc)
6366 stuffcharReadbuff(ESC);
6367
6368 return OK;
6369}
6370
6371 char_u *
6372get_last_insert()
6373{
6374 if (last_insert == NULL)
6375 return NULL;
6376 return last_insert + last_insert_skip;
6377}
6378
6379/*
6380 * Get last inserted string, and remove trailing <Esc>.
6381 * Returns pointer to allocated memory (must be freed) or NULL.
6382 */
6383 char_u *
6384get_last_insert_save()
6385{
6386 char_u *s;
6387 int len;
6388
6389 if (last_insert == NULL)
6390 return NULL;
6391 s = vim_strsave(last_insert + last_insert_skip);
6392 if (s != NULL)
6393 {
6394 len = (int)STRLEN(s);
6395 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
6396 s[len - 1] = NUL;
6397 }
6398 return s;
6399}
6400
6401/*
6402 * Check the word in front of the cursor for an abbreviation.
6403 * Called when the non-id character "c" has been entered.
6404 * When an abbreviation is recognized it is removed from the text and
6405 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
6406 */
6407 static int
6408echeck_abbr(c)
6409 int c;
6410{
6411 /* Don't check for abbreviation in paste mode, when disabled and just
6412 * after moving around with cursor keys. */
6413 if (p_paste || no_abbr || arrow_used)
6414 return FALSE;
6415
6416 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
6417 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
6418}
6419
6420/*
6421 * replace-stack functions
6422 *
6423 * When replacing characters, the replaced characters are remembered for each
6424 * new character. This is used to re-insert the old text when backspacing.
6425 *
6426 * There is a NUL headed list of characters for each character that is
6427 * currently in the file after the insertion point. When BS is used, one NUL
6428 * headed list is put back for the deleted character.
6429 *
6430 * For a newline, there are two NUL headed lists. One contains the characters
6431 * that the NL replaced. The extra one stores the characters after the cursor
6432 * that were deleted (always white space).
6433 *
6434 * Replace_offset is normally 0, in which case replace_push will add a new
6435 * character at the end of the stack. If replace_offset is not 0, that many
6436 * characters will be left on the stack above the newly inserted character.
6437 */
6438
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00006439static char_u *replace_stack = NULL;
6440static long replace_stack_nr = 0; /* next entry in replace stack */
6441static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442
6443 void
6444replace_push(c)
6445 int c; /* character that is replaced (NUL is none) */
6446{
6447 char_u *p;
6448
6449 if (replace_stack_nr < replace_offset) /* nothing to do */
6450 return;
6451 if (replace_stack_len <= replace_stack_nr)
6452 {
6453 replace_stack_len += 50;
6454 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
6455 if (p == NULL) /* out of memory */
6456 {
6457 replace_stack_len -= 50;
6458 return;
6459 }
6460 if (replace_stack != NULL)
6461 {
6462 mch_memmove(p, replace_stack,
6463 (size_t)(replace_stack_nr * sizeof(char_u)));
6464 vim_free(replace_stack);
6465 }
6466 replace_stack = p;
6467 }
6468 p = replace_stack + replace_stack_nr - replace_offset;
6469 if (replace_offset)
6470 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
6471 *p = c;
6472 ++replace_stack_nr;
6473}
6474
6475/*
6476 * call replace_push(c) with replace_offset set to the first NUL.
6477 */
6478 static void
6479replace_push_off(c)
6480 int c;
6481{
6482 char_u *p;
6483
6484 p = replace_stack + replace_stack_nr;
6485 for (replace_offset = 1; replace_offset < replace_stack_nr;
6486 ++replace_offset)
6487 if (*--p == NUL)
6488 break;
6489 replace_push(c);
6490 replace_offset = 0;
6491}
6492
6493/*
6494 * Pop one item from the replace stack.
6495 * return -1 if stack empty
6496 * return replaced character or NUL otherwise
6497 */
6498 static int
6499replace_pop()
6500{
6501 if (replace_stack_nr == 0)
6502 return -1;
6503 return (int)replace_stack[--replace_stack_nr];
6504}
6505
6506/*
6507 * Join the top two items on the replace stack. This removes to "off"'th NUL
6508 * encountered.
6509 */
6510 static void
6511replace_join(off)
6512 int off; /* offset for which NUL to remove */
6513{
6514 int i;
6515
6516 for (i = replace_stack_nr; --i >= 0; )
6517 if (replace_stack[i] == NUL && off-- <= 0)
6518 {
6519 --replace_stack_nr;
6520 mch_memmove(replace_stack + i, replace_stack + i + 1,
6521 (size_t)(replace_stack_nr - i));
6522 return;
6523 }
6524}
6525
6526/*
6527 * Pop bytes from the replace stack until a NUL is found, and insert them
6528 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
6529 */
6530 static void
6531replace_pop_ins()
6532{
6533 int cc;
6534 int oldState = State;
6535
6536 State = NORMAL; /* don't want REPLACE here */
6537 while ((cc = replace_pop()) > 0)
6538 {
6539#ifdef FEAT_MBYTE
6540 mb_replace_pop_ins(cc);
6541#else
6542 ins_char(cc);
6543#endif
6544 dec_cursor();
6545 }
6546 State = oldState;
6547}
6548
6549#ifdef FEAT_MBYTE
6550/*
6551 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
6552 * indicates a multi-byte char, pop the other bytes too.
6553 */
6554 static void
6555mb_replace_pop_ins(cc)
6556 int cc;
6557{
6558 int n;
6559 char_u buf[MB_MAXBYTES];
6560 int i;
6561 int c;
6562
6563 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
6564 {
6565 buf[0] = cc;
6566 for (i = 1; i < n; ++i)
6567 buf[i] = replace_pop();
6568 ins_bytes_len(buf, n);
6569 }
6570 else
6571 ins_char(cc);
6572
6573 if (enc_utf8)
6574 /* Handle composing chars. */
6575 for (;;)
6576 {
6577 c = replace_pop();
6578 if (c == -1) /* stack empty */
6579 break;
6580 if ((n = MB_BYTE2LEN(c)) == 1)
6581 {
6582 /* Not a multi-byte char, put it back. */
6583 replace_push(c);
6584 break;
6585 }
6586 else
6587 {
6588 buf[0] = c;
6589 for (i = 1; i < n; ++i)
6590 buf[i] = replace_pop();
6591 if (utf_iscomposing(utf_ptr2char(buf)))
6592 ins_bytes_len(buf, n);
6593 else
6594 {
6595 /* Not a composing char, put it back. */
6596 for (i = n - 1; i >= 0; --i)
6597 replace_push(buf[i]);
6598 break;
6599 }
6600 }
6601 }
6602}
6603#endif
6604
6605/*
6606 * make the replace stack empty
6607 * (called when exiting replace mode)
6608 */
6609 static void
6610replace_flush()
6611{
6612 vim_free(replace_stack);
6613 replace_stack = NULL;
6614 replace_stack_len = 0;
6615 replace_stack_nr = 0;
6616}
6617
6618/*
6619 * Handle doing a BS for one character.
6620 * cc < 0: replace stack empty, just move cursor
6621 * cc == 0: character was inserted, delete it
6622 * cc > 0: character was replaced, put cc (first byte of original char) back
6623 * and check for more characters to be put back
6624 */
6625 static void
6626replace_do_bs()
6627{
6628 int cc;
6629#ifdef FEAT_VREPLACE
6630 int orig_len = 0;
6631 int ins_len;
6632 int orig_vcols = 0;
6633 colnr_T start_vcol;
6634 char_u *p;
6635 int i;
6636 int vcol;
6637#endif
6638
6639 cc = replace_pop();
6640 if (cc > 0)
6641 {
6642#ifdef FEAT_VREPLACE
6643 if (State & VREPLACE_FLAG)
6644 {
6645 /* Get the number of screen cells used by the character we are
6646 * going to delete. */
6647 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
6648 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
6649 }
6650#endif
6651#ifdef FEAT_MBYTE
6652 if (has_mbyte)
6653 {
6654 del_char(FALSE);
6655# ifdef FEAT_VREPLACE
6656 if (State & VREPLACE_FLAG)
6657 orig_len = STRLEN(ml_get_cursor());
6658# endif
6659 replace_push(cc);
6660 }
6661 else
6662#endif
6663 {
6664 pchar_cursor(cc);
6665#ifdef FEAT_VREPLACE
6666 if (State & VREPLACE_FLAG)
6667 orig_len = STRLEN(ml_get_cursor()) - 1;
6668#endif
6669 }
6670 replace_pop_ins();
6671
6672#ifdef FEAT_VREPLACE
6673 if (State & VREPLACE_FLAG)
6674 {
6675 /* Get the number of screen cells used by the inserted characters */
6676 p = ml_get_cursor();
6677 ins_len = STRLEN(p) - orig_len;
6678 vcol = start_vcol;
6679 for (i = 0; i < ins_len; ++i)
6680 {
6681 vcol += chartabsize(p + i, vcol);
6682#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006683 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684#endif
6685 }
6686 vcol -= start_vcol;
6687
6688 /* Delete spaces that were inserted after the cursor to keep the
6689 * text aligned. */
6690 curwin->w_cursor.col += ins_len;
6691 while (vcol > orig_vcols && gchar_cursor() == ' ')
6692 {
6693 del_char(FALSE);
6694 ++orig_vcols;
6695 }
6696 curwin->w_cursor.col -= ins_len;
6697 }
6698#endif
6699
6700 /* mark the buffer as changed and prepare for displaying */
6701 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
6702 }
6703 else if (cc == 0)
6704 (void)del_char(FALSE);
6705}
6706
6707#ifdef FEAT_CINDENT
6708/*
6709 * Return TRUE if C-indenting is on.
6710 */
6711 static int
6712cindent_on()
6713{
6714 return (!p_paste && (curbuf->b_p_cin
6715# ifdef FEAT_EVAL
6716 || *curbuf->b_p_inde != NUL
6717# endif
6718 ));
6719}
6720#endif
6721
6722#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
6723/*
6724 * Re-indent the current line, based on the current contents of it and the
6725 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
6726 * confused what all the part that handles Control-T is doing that I'm not.
6727 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
6728 */
6729
6730 void
6731fixthisline(get_the_indent)
6732 int (*get_the_indent) __ARGS((void));
6733{
6734 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
6735 if (linewhite(curwin->w_cursor.lnum))
6736 did_ai = TRUE; /* delete the indent if the line stays empty */
6737}
6738
6739 void
6740fix_indent()
6741{
6742 if (p_paste)
6743 return;
6744# ifdef FEAT_LISP
6745 if (curbuf->b_p_lisp && curbuf->b_p_ai)
6746 fixthisline(get_lisp_indent);
6747# endif
6748# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
6749 else
6750# endif
6751# ifdef FEAT_CINDENT
6752 if (cindent_on())
6753 do_c_expr_indent();
6754# endif
6755}
6756
6757#endif
6758
6759#ifdef FEAT_CINDENT
6760/*
6761 * return TRUE if 'cinkeys' contains the key "keytyped",
6762 * when == '*': Only if key is preceded with '*' (indent before insert)
6763 * when == '!': Only if key is prededed with '!' (don't insert)
6764 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
6765 *
6766 * "keytyped" can have a few special values:
6767 * KEY_OPEN_FORW
6768 * KEY_OPEN_BACK
6769 * KEY_COMPLETE just finished completion.
6770 *
6771 * If line_is_empty is TRUE accept keys with '0' before them.
6772 */
6773 int
6774in_cinkeys(keytyped, when, line_is_empty)
6775 int keytyped;
6776 int when;
6777 int line_is_empty;
6778{
6779 char_u *look;
6780 int try_match;
6781 int try_match_word;
6782 char_u *p;
6783 char_u *line;
6784 int icase;
6785 int i;
6786
6787#ifdef FEAT_EVAL
6788 if (*curbuf->b_p_inde != NUL)
6789 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
6790 else
6791#endif
6792 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
6793 while (*look)
6794 {
6795 /*
6796 * Find out if we want to try a match with this key, depending on
6797 * 'when' and a '*' or '!' before the key.
6798 */
6799 switch (when)
6800 {
6801 case '*': try_match = (*look == '*'); break;
6802 case '!': try_match = (*look == '!'); break;
6803 default: try_match = (*look != '*'); break;
6804 }
6805 if (*look == '*' || *look == '!')
6806 ++look;
6807
6808 /*
6809 * If there is a '0', only accept a match if the line is empty.
6810 * But may still match when typing last char of a word.
6811 */
6812 if (*look == '0')
6813 {
6814 try_match_word = try_match;
6815 if (!line_is_empty)
6816 try_match = FALSE;
6817 ++look;
6818 }
6819 else
6820 try_match_word = FALSE;
6821
6822 /*
6823 * does it look like a control character?
6824 */
6825 if (*look == '^'
6826#ifdef EBCDIC
6827 && (Ctrl_chr(look[1]) != 0)
6828#else
6829 && look[1] >= '?' && look[1] <= '_'
6830#endif
6831 )
6832 {
6833 if (try_match && keytyped == Ctrl_chr(look[1]))
6834 return TRUE;
6835 look += 2;
6836 }
6837 /*
6838 * 'o' means "o" command, open forward.
6839 * 'O' means "O" command, open backward.
6840 */
6841 else if (*look == 'o')
6842 {
6843 if (try_match && keytyped == KEY_OPEN_FORW)
6844 return TRUE;
6845 ++look;
6846 }
6847 else if (*look == 'O')
6848 {
6849 if (try_match && keytyped == KEY_OPEN_BACK)
6850 return TRUE;
6851 ++look;
6852 }
6853
6854 /*
6855 * 'e' means to check for "else" at start of line and just before the
6856 * cursor.
6857 */
6858 else if (*look == 'e')
6859 {
6860 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
6861 {
6862 p = ml_get_curline();
6863 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
6864 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
6865 return TRUE;
6866 }
6867 ++look;
6868 }
6869
6870 /*
6871 * ':' only causes an indent if it is at the end of a label or case
6872 * statement, or when it was before typing the ':' (to fix
6873 * class::method for C++).
6874 */
6875 else if (*look == ':')
6876 {
6877 if (try_match && keytyped == ':')
6878 {
6879 p = ml_get_curline();
6880 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
6881 return TRUE;
6882 if (curwin->w_cursor.col > 2
6883 && p[curwin->w_cursor.col - 1] == ':'
6884 && p[curwin->w_cursor.col - 2] == ':')
6885 {
6886 p[curwin->w_cursor.col - 1] = ' ';
6887 i = (cin_iscase(p) || cin_isscopedecl(p)
6888 || cin_islabel(30));
6889 p = ml_get_curline();
6890 p[curwin->w_cursor.col - 1] = ':';
6891 if (i)
6892 return TRUE;
6893 }
6894 }
6895 ++look;
6896 }
6897
6898
6899 /*
6900 * Is it a key in <>, maybe?
6901 */
6902 else if (*look == '<')
6903 {
6904 if (try_match)
6905 {
6906 /*
6907 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
6908 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
6909 * >, *, : and ! keys if they really really want to.
6910 */
6911 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
6912 && keytyped == look[1])
6913 return TRUE;
6914
6915 if (keytyped == get_special_key_code(look + 1))
6916 return TRUE;
6917 }
6918 while (*look && *look != '>')
6919 look++;
6920 while (*look == '>')
6921 look++;
6922 }
6923
6924 /*
6925 * Is it a word: "=word"?
6926 */
6927 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
6928 {
6929 ++look;
6930 if (*look == '~')
6931 {
6932 icase = TRUE;
6933 ++look;
6934 }
6935 else
6936 icase = FALSE;
6937 p = vim_strchr(look, ',');
6938 if (p == NULL)
6939 p = look + STRLEN(look);
6940 if ((try_match || try_match_word)
6941 && curwin->w_cursor.col >= (colnr_T)(p - look))
6942 {
6943 int match = FALSE;
6944
6945#ifdef FEAT_INS_EXPAND
6946 if (keytyped == KEY_COMPLETE)
6947 {
6948 char_u *s;
6949
6950 /* Just completed a word, check if it starts with "look".
6951 * search back for the start of a word. */
6952 line = ml_get_curline();
6953# ifdef FEAT_MBYTE
6954 if (has_mbyte)
6955 {
6956 char_u *n;
6957
6958 for (s = line + curwin->w_cursor.col; s > line; s = n)
6959 {
6960 n = mb_prevptr(line, s);
6961 if (!vim_iswordp(n))
6962 break;
6963 }
6964 }
6965 else
6966# endif
6967 for (s = line + curwin->w_cursor.col; s > line; --s)
6968 if (!vim_iswordc(s[-1]))
6969 break;
6970 if (s + (p - look) <= line + curwin->w_cursor.col
6971 && (icase
6972 ? MB_STRNICMP(s, look, p - look)
6973 : STRNCMP(s, look, p - look)) == 0)
6974 match = TRUE;
6975 }
6976 else
6977#endif
6978 /* TODO: multi-byte */
6979 if (keytyped == (int)p[-1] || (icase && keytyped < 256
6980 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
6981 {
6982 line = ml_get_cursor();
6983 if ((curwin->w_cursor.col == (colnr_T)(p - look)
6984 || !vim_iswordc(line[-(p - look) - 1]))
6985 && (icase
6986 ? MB_STRNICMP(line - (p - look), look, p - look)
6987 : STRNCMP(line - (p - look), look, p - look))
6988 == 0)
6989 match = TRUE;
6990 }
6991 if (match && try_match_word && !try_match)
6992 {
6993 /* "0=word": Check if there are only blanks before the
6994 * word. */
6995 line = ml_get_curline();
6996 if ((int)(skipwhite(line) - line) !=
6997 (int)(curwin->w_cursor.col - (p - look)))
6998 match = FALSE;
6999 }
7000 if (match)
7001 return TRUE;
7002 }
7003 look = p;
7004 }
7005
7006 /*
7007 * ok, it's a boring generic character.
7008 */
7009 else
7010 {
7011 if (try_match && *look == keytyped)
7012 return TRUE;
7013 ++look;
7014 }
7015
7016 /*
7017 * Skip over ", ".
7018 */
7019 look = skip_to_option_part(look);
7020 }
7021 return FALSE;
7022}
7023#endif /* FEAT_CINDENT */
7024
7025#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7026/*
7027 * Map Hebrew keyboard when in hkmap mode.
7028 */
7029 int
7030hkmap(c)
7031 int c;
7032{
7033 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7034 {
7035 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7036 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7037 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7038 static char_u map[26] =
7039 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7040 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7041 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7042 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7043 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7044 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7045 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7046 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7047 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7048
7049 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7050 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7051 /* '-1'='sofit' */
7052 else if (c == 'x')
7053 return 'X';
7054 else if (c == 'q')
7055 return '\''; /* {geresh}={'} */
7056 else if (c == 246)
7057 return ' '; /* \"o --> ' ' for a german keyboard */
7058 else if (c == 228)
7059 return ' '; /* \"a --> ' ' -- / -- */
7060 else if (c == 252)
7061 return ' '; /* \"u --> ' ' -- / -- */
7062#ifdef EBCDIC
7063 else if (islower(c))
7064#else
7065 /* NOTE: islower() does not do the right thing for us on Linux so we
7066 * do this the same was as 5.7 and previous, so it works correctly on
7067 * all systems. Specifically, the e.g. Delete and Arrow keys are
7068 * munged and won't work if e.g. searching for Hebrew text.
7069 */
7070 else if (c >= 'a' && c <= 'z')
7071#endif
7072 return (int)(map[CharOrdLow(c)] + p_aleph);
7073 else
7074 return c;
7075 }
7076 else
7077 {
7078 switch (c)
7079 {
7080 case '`': return ';';
7081 case '/': return '.';
7082 case '\'': return ',';
7083 case 'q': return '/';
7084 case 'w': return '\'';
7085
7086 /* Hebrew letters - set offset from 'a' */
7087 case ',': c = '{'; break;
7088 case '.': c = 'v'; break;
7089 case ';': c = 't'; break;
7090 default: {
7091 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7092
7093#ifdef EBCDIC
7094 /* see note about islower() above */
7095 if (!islower(c))
7096#else
7097 if (c < 'a' || c > 'z')
7098#endif
7099 return c;
7100 c = str[CharOrdLow(c)];
7101 break;
7102 }
7103 }
7104
7105 return (int)(CharOrdLow(c) + p_aleph);
7106 }
7107}
7108#endif
7109
7110 static void
7111ins_reg()
7112{
7113 int need_redraw = FALSE;
7114 int regname;
7115 int literally = 0;
7116
7117 /*
7118 * If we are going to wait for a character, show a '"'.
7119 */
7120 pc_status = PC_STATUS_UNSET;
7121 if (redrawing() && !char_avail())
7122 {
7123 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007124 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125
7126 edit_putchar('"', TRUE);
7127#ifdef FEAT_CMDL_INFO
7128 add_to_showcmd_c(Ctrl_R);
7129#endif
7130 }
7131
7132#ifdef USE_ON_FLY_SCROLL
7133 dont_scroll = TRUE; /* disallow scrolling here */
7134#endif
7135
7136 /*
7137 * Don't map the register name. This also prevents the mode message to be
7138 * deleted when ESC is hit.
7139 */
7140 ++no_mapping;
7141 regname = safe_vgetc();
7142#ifdef FEAT_LANGMAP
7143 LANGMAP_ADJUST(regname, TRUE);
7144#endif
7145 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
7146 {
7147 /* Get a third key for literal register insertion */
7148 literally = regname;
7149#ifdef FEAT_CMDL_INFO
7150 add_to_showcmd_c(literally);
7151#endif
7152 regname = safe_vgetc();
7153#ifdef FEAT_LANGMAP
7154 LANGMAP_ADJUST(regname, TRUE);
7155#endif
7156 }
7157 --no_mapping;
7158
7159#ifdef FEAT_EVAL
7160 /*
7161 * Don't call u_sync() while getting the expression,
7162 * evaluating it or giving an error message for it!
7163 */
7164 ++no_u_sync;
7165 if (regname == '=')
7166 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007167# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007169# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007171# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 /* Restore the Input Method. */
7173 if (im_on)
7174 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007175# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00007177 if (regname == NUL || !valid_yank_reg(regname, FALSE))
7178 {
7179 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 else
7183 {
7184#endif
7185 if (literally == Ctrl_O || literally == Ctrl_P)
7186 {
7187 /* Append the command to the redo buffer. */
7188 AppendCharToRedobuff(Ctrl_R);
7189 AppendCharToRedobuff(literally);
7190 AppendCharToRedobuff(regname);
7191
7192 do_put(regname, BACKWARD, 1L,
7193 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
7194 }
7195 else if (insert_reg(regname, literally) == FAIL)
7196 {
7197 vim_beep();
7198 need_redraw = TRUE; /* remove the '"' */
7199 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007200 else if (stop_insert_mode)
7201 /* When the '=' register was used and a function was invoked that
7202 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7203 * insert anything, need to remove the '"' */
7204 need_redraw = TRUE;
7205
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206#ifdef FEAT_EVAL
7207 }
7208 --no_u_sync;
7209#endif
7210#ifdef FEAT_CMDL_INFO
7211 clear_showcmd();
7212#endif
7213
7214 /* If the inserted register is empty, we need to remove the '"' */
7215 if (need_redraw || stuff_empty())
7216 edit_unputchar();
7217}
7218
7219/*
7220 * CTRL-G commands in Insert mode.
7221 */
7222 static void
7223ins_ctrl_g()
7224{
7225 int c;
7226
7227#ifdef FEAT_INS_EXPAND
7228 /* Right after CTRL-X the cursor will be after the ruler. */
7229 setcursor();
7230#endif
7231
7232 /*
7233 * Don't map the second key. This also prevents the mode message to be
7234 * deleted when ESC is hit.
7235 */
7236 ++no_mapping;
7237 c = safe_vgetc();
7238 --no_mapping;
7239 switch (c)
7240 {
7241 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7242 case K_UP:
7243 case Ctrl_K:
7244 case 'k': ins_up(TRUE);
7245 break;
7246
7247 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7248 case K_DOWN:
7249 case Ctrl_J:
7250 case 'j': ins_down(TRUE);
7251 break;
7252
7253 /* CTRL-G u: start new undoable edit */
7254 case 'u': u_sync();
7255 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007256
7257 /* Need to reset Insstart, esp. because a BS that joins
7258 * aline to the previous one must save for undo. */
7259 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 break;
7261
7262 /* Unknown CTRL-G command, reserved for future expansion. */
7263 default: vim_beep();
7264 }
7265}
7266
7267/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007268 * CTRL-^ in Insert mode.
7269 */
7270 static void
7271ins_ctrl_hat()
7272{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00007273 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007274 {
7275 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7276 if (State & LANGMAP)
7277 {
7278 curbuf->b_p_iminsert = B_IMODE_NONE;
7279 State &= ~LANGMAP;
7280 }
7281 else
7282 {
7283 curbuf->b_p_iminsert = B_IMODE_LMAP;
7284 State |= LANGMAP;
7285#ifdef USE_IM_CONTROL
7286 im_set_active(FALSE);
7287#endif
7288 }
7289 }
7290#ifdef USE_IM_CONTROL
7291 else
7292 {
7293 /* There are no ":lmap" mappings, toggle IM */
7294 if (im_get_status())
7295 {
7296 curbuf->b_p_iminsert = B_IMODE_NONE;
7297 im_set_active(FALSE);
7298 }
7299 else
7300 {
7301 curbuf->b_p_iminsert = B_IMODE_IM;
7302 State &= ~LANGMAP;
7303 im_set_active(TRUE);
7304 }
7305 }
7306#endif
7307 set_iminsert_global();
7308 showmode();
7309#ifdef FEAT_GUI
7310 /* may show different cursor shape or color */
7311 if (gui.in_use)
7312 gui_update_cursor(TRUE, FALSE);
7313#endif
7314#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7315 /* Show/unshow value of 'keymap' in status lines. */
7316 status_redraw_curbuf();
7317#endif
7318}
7319
7320/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 * Handle ESC in insert mode.
7322 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7323 * insert.
7324 */
7325 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00007326ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 long *count;
7328 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00007329 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330{
7331 int temp;
7332 static int disabled_redraw = FALSE;
7333
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007334#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007335 check_spell_redraw();
7336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337#if defined(FEAT_HANGULIN)
7338# if defined(ESC_CHG_TO_ENG_MODE)
7339 hangul_input_state_set(0);
7340# endif
7341 if (composing_hangul)
7342 {
7343 push_raw_key(composing_hangul_buffer, 2);
7344 composing_hangul = 0;
7345 }
7346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347
7348 temp = curwin->w_cursor.col;
7349 if (disabled_redraw)
7350 {
7351 --RedrawingDisabled;
7352 disabled_redraw = FALSE;
7353 }
7354 if (!arrow_used)
7355 {
7356 /*
7357 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00007358 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7359 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 */
7361 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00007362 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363
7364 /*
7365 * Repeating insert may take a long time. Check for
7366 * interrupt now and then.
7367 */
7368 if (*count > 0)
7369 {
7370 line_breakcheck();
7371 if (got_int)
7372 *count = 0;
7373 }
7374
7375 if (--*count > 0) /* repeat what was typed */
7376 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007377 /* Vi repeats the insert without replacing characters. */
7378 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
7379 State &= ~REPLACE_FLAG;
7380
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 (void)start_redo_ins();
7382 if (cmdchar == 'r' || cmdchar == 'v')
7383 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
7384 ++RedrawingDisabled;
7385 disabled_redraw = TRUE;
7386 return FALSE; /* repeat the insert */
7387 }
7388 stop_insert(&curwin->w_cursor, TRUE);
7389 undisplay_dollar();
7390 }
7391
7392 /* When an autoindent was removed, curswant stays after the
7393 * indent */
7394 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
7395 curwin->w_set_curswant = TRUE;
7396
7397 /* Remember the last Insert position in the '^ mark. */
7398 if (!cmdmod.keepjumps)
7399 curbuf->b_last_insert = curwin->w_cursor;
7400
7401 /*
7402 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00007403 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00007405 if (!nomove
7406 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407#ifdef FEAT_VIRTUALEDIT
7408 || curwin->w_cursor.coladd > 0
7409#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007410 )
7411 && (restart_edit == NUL
7412 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00007414 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007416 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417#ifdef FEAT_RIGHTLEFT
7418 && !revins_on
7419#endif
7420 )
7421 {
7422#ifdef FEAT_VIRTUALEDIT
7423 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
7424 {
7425 oneleft();
7426 if (restart_edit != NUL)
7427 ++curwin->w_cursor.coladd;
7428 }
7429 else
7430#endif
7431 {
7432 --curwin->w_cursor.col;
7433#ifdef FEAT_MBYTE
7434 /* Correct cursor for multi-byte character. */
7435 if (has_mbyte)
7436 mb_adjust_cursor();
7437#endif
7438 }
7439 }
7440
7441#ifdef USE_IM_CONTROL
7442 /* Disable IM to allow typing English directly for Normal mode commands.
7443 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
7444 * well). */
7445 if (!(State & LANGMAP))
7446 im_save_status(&curbuf->b_p_iminsert);
7447 im_set_active(FALSE);
7448#endif
7449
7450 State = NORMAL;
7451 /* need to position cursor again (e.g. when on a TAB ) */
7452 changed_cline_bef_curs();
7453
7454#ifdef FEAT_MOUSE
7455 setmouse();
7456#endif
7457#ifdef CURSOR_SHAPE
7458 ui_cursor_shape(); /* may show different cursor shape */
7459#endif
7460
7461 /*
7462 * When recording or for CTRL-O, need to display the new mode.
7463 * Otherwise remove the mode message.
7464 */
7465 if (Recording || restart_edit != NUL)
7466 showmode();
7467 else if (p_smd)
7468 MSG("");
7469
7470 return TRUE; /* exit Insert mode */
7471}
7472
7473#ifdef FEAT_RIGHTLEFT
7474/*
7475 * Toggle language: hkmap and revins_on.
7476 * Move to end of reverse inserted text.
7477 */
7478 static void
7479ins_ctrl_()
7480{
7481 if (revins_on && revins_chars && revins_scol >= 0)
7482 {
7483 while (gchar_cursor() != NUL && revins_chars--)
7484 ++curwin->w_cursor.col;
7485 }
7486 p_ri = !p_ri;
7487 revins_on = (State == INSERT && p_ri);
7488 if (revins_on)
7489 {
7490 revins_scol = curwin->w_cursor.col;
7491 revins_legal++;
7492 revins_chars = 0;
7493 undisplay_dollar();
7494 }
7495 else
7496 revins_scol = -1;
7497#ifdef FEAT_FKMAP
7498 if (p_altkeymap)
7499 {
7500 /*
7501 * to be consistent also for redo command, using '.'
7502 * set arrow_used to true and stop it - causing to redo
7503 * characters entered in one mode (normal/reverse insert).
7504 */
7505 arrow_used = TRUE;
7506 (void)stop_arrow();
7507 p_fkmap = curwin->w_p_rl ^ p_ri;
7508 if (p_fkmap && p_ri)
7509 State = INSERT;
7510 }
7511 else
7512#endif
7513 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
7514 showmode();
7515}
7516#endif
7517
7518#ifdef FEAT_VISUAL
7519/*
7520 * If 'keymodel' contains "startsel", may start selection.
7521 * Returns TRUE when a CTRL-O and other keys stuffed.
7522 */
7523 static int
7524ins_start_select(c)
7525 int c;
7526{
7527 if (km_startsel)
7528 switch (c)
7529 {
7530 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 case K_PAGEUP:
7533 case K_KPAGEUP:
7534 case K_PAGEDOWN:
7535 case K_KPAGEDOWN:
7536# ifdef MACOS
7537 case K_LEFT:
7538 case K_RIGHT:
7539 case K_UP:
7540 case K_DOWN:
7541 case K_END:
7542 case K_HOME:
7543# endif
7544 if (!(mod_mask & MOD_MASK_SHIFT))
7545 break;
7546 /* FALLTHROUGH */
7547 case K_S_LEFT:
7548 case K_S_RIGHT:
7549 case K_S_UP:
7550 case K_S_DOWN:
7551 case K_S_END:
7552 case K_S_HOME:
7553 /* Start selection right away, the cursor can move with
7554 * CTRL-O when beyond the end of the line. */
7555 start_selection();
7556
7557 /* Execute the key in (insert) Select mode. */
7558 stuffcharReadbuff(Ctrl_O);
7559 if (mod_mask)
7560 {
7561 char_u buf[4];
7562
7563 buf[0] = K_SPECIAL;
7564 buf[1] = KS_MODIFIER;
7565 buf[2] = mod_mask;
7566 buf[3] = NUL;
7567 stuffReadbuff(buf);
7568 }
7569 stuffcharReadbuff(c);
7570 return TRUE;
7571 }
7572 return FALSE;
7573}
7574#endif
7575
7576/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007577 * <Insert> key in Insert mode: toggle insert/remplace mode.
7578 */
7579 static void
7580ins_insert(replaceState)
7581 int replaceState;
7582{
7583#ifdef FEAT_FKMAP
7584 if (p_fkmap && p_ri)
7585 {
7586 beep_flush();
7587 EMSG(farsi_text_3); /* encoded in Farsi */
7588 return;
7589 }
7590#endif
7591
7592#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00007593# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007594 set_vim_var_string(VV_INSERTMODE,
7595 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007596# ifdef FEAT_VREPLACE
7597 replaceState == VREPLACE ? "v" :
7598# endif
7599 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00007600# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007601 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
7602#endif
7603 if (State & REPLACE_FLAG)
7604 State = INSERT | (State & LANGMAP);
7605 else
7606 State = replaceState | (State & LANGMAP);
7607 AppendCharToRedobuff(K_INS);
7608 showmode();
7609#ifdef CURSOR_SHAPE
7610 ui_cursor_shape(); /* may show different cursor shape */
7611#endif
7612}
7613
7614/*
7615 * Pressed CTRL-O in Insert mode.
7616 */
7617 static void
7618ins_ctrl_o()
7619{
7620#ifdef FEAT_VREPLACE
7621 if (State & VREPLACE_FLAG)
7622 restart_edit = 'V';
7623 else
7624#endif
7625 if (State & REPLACE_FLAG)
7626 restart_edit = 'R';
7627 else
7628 restart_edit = 'I';
7629#ifdef FEAT_VIRTUALEDIT
7630 if (virtual_active())
7631 ins_at_eol = FALSE; /* cursor always keeps its column */
7632 else
7633#endif
7634 ins_at_eol = (gchar_cursor() == NUL);
7635}
7636
7637/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 * If the cursor is on an indent, ^T/^D insert/delete one
7639 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
7640 * Always round the indent to 'shiftwith', this is compatible
7641 * with vi. But vi only supports ^T and ^D after an
7642 * autoindent, we support it everywhere.
7643 */
7644 static void
7645ins_shift(c, lastc)
7646 int c;
7647 int lastc;
7648{
7649 if (stop_arrow() == FAIL)
7650 return;
7651 AppendCharToRedobuff(c);
7652
7653 /*
7654 * 0^D and ^^D: remove all indent.
7655 */
7656 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
7657 {
7658 --curwin->w_cursor.col;
7659 (void)del_char(FALSE); /* delete the '^' or '0' */
7660 /* In Replace mode, restore the characters that '^' or '0' replaced. */
7661 if (State & REPLACE_FLAG)
7662 replace_pop_ins();
7663 if (lastc == '^')
7664 old_indent = get_indent(); /* remember curr. indent */
7665 change_indent(INDENT_SET, 0, TRUE, 0);
7666 }
7667 else
7668 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
7669
7670 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
7671 did_ai = FALSE;
7672#ifdef FEAT_SMARTINDENT
7673 did_si = FALSE;
7674 can_si = FALSE;
7675 can_si_back = FALSE;
7676#endif
7677#ifdef FEAT_CINDENT
7678 can_cindent = FALSE; /* no cindenting after ^D or ^T */
7679#endif
7680}
7681
7682 static void
7683ins_del()
7684{
7685 int temp;
7686
7687 if (stop_arrow() == FAIL)
7688 return;
7689 if (gchar_cursor() == NUL) /* delete newline */
7690 {
7691 temp = curwin->w_cursor.col;
7692 if (!can_bs(BS_EOL) /* only if "eol" included */
7693 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
7694 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
7695 || do_join(FALSE) == FAIL)
7696 vim_beep();
7697 else
7698 curwin->w_cursor.col = temp;
7699 }
7700 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
7701 vim_beep();
7702 did_ai = FALSE;
7703#ifdef FEAT_SMARTINDENT
7704 did_si = FALSE;
7705 can_si = FALSE;
7706 can_si_back = FALSE;
7707#endif
7708 AppendCharToRedobuff(K_DEL);
7709}
7710
7711/*
7712 * Handle Backspace, delete-word and delete-line in Insert mode.
7713 * Return TRUE when backspace was actually used.
7714 */
7715 static int
7716ins_bs(c, mode, inserted_space_p)
7717 int c;
7718 int mode;
7719 int *inserted_space_p;
7720{
7721 linenr_T lnum;
7722 int cc;
7723 int temp = 0; /* init for GCC */
7724 colnr_T mincol;
7725 int did_backspace = FALSE;
7726 int in_indent;
7727 int oldState;
7728#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007729 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730#endif
7731
7732 /*
7733 * can't delete anything in an empty file
7734 * can't backup past first character in buffer
7735 * can't backup past starting point unless 'backspace' > 1
7736 * can backup to a previous line if 'backspace' == 0
7737 */
7738 if ( bufempty()
7739 || (
7740#ifdef FEAT_RIGHTLEFT
7741 !revins_on &&
7742#endif
7743 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
7744 || (!can_bs(BS_START)
7745 && (arrow_used
7746 || (curwin->w_cursor.lnum == Insstart.lnum
7747 && curwin->w_cursor.col <= Insstart.col)))
7748 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
7749 && curwin->w_cursor.col <= ai_col)
7750 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
7751 {
7752 vim_beep();
7753 return FALSE;
7754 }
7755
7756 if (stop_arrow() == FAIL)
7757 return FALSE;
7758 in_indent = inindent(0);
7759#ifdef FEAT_CINDENT
7760 if (in_indent)
7761 can_cindent = FALSE;
7762#endif
7763#ifdef FEAT_COMMENTS
7764 end_comment_pending = NUL; /* After BS, don't auto-end comment */
7765#endif
7766#ifdef FEAT_RIGHTLEFT
7767 if (revins_on) /* put cursor after last inserted char */
7768 inc_cursor();
7769#endif
7770
7771#ifdef FEAT_VIRTUALEDIT
7772 /* Virtualedit:
7773 * BACKSPACE_CHAR eats a virtual space
7774 * BACKSPACE_WORD eats all coladd
7775 * BACKSPACE_LINE eats all coladd and keeps going
7776 */
7777 if (curwin->w_cursor.coladd > 0)
7778 {
7779 if (mode == BACKSPACE_CHAR)
7780 {
7781 --curwin->w_cursor.coladd;
7782 return TRUE;
7783 }
7784 if (mode == BACKSPACE_WORD)
7785 {
7786 curwin->w_cursor.coladd = 0;
7787 return TRUE;
7788 }
7789 curwin->w_cursor.coladd = 0;
7790 }
7791#endif
7792
7793 /*
7794 * delete newline!
7795 */
7796 if (curwin->w_cursor.col == 0)
7797 {
7798 lnum = Insstart.lnum;
7799 if (curwin->w_cursor.lnum == Insstart.lnum
7800#ifdef FEAT_RIGHTLEFT
7801 || revins_on
7802#endif
7803 )
7804 {
7805 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
7806 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
7807 return FALSE;
7808 --Insstart.lnum;
7809 Insstart.col = MAXCOL;
7810 }
7811 /*
7812 * In replace mode:
7813 * cc < 0: NL was inserted, delete it
7814 * cc >= 0: NL was replaced, put original characters back
7815 */
7816 cc = -1;
7817 if (State & REPLACE_FLAG)
7818 cc = replace_pop(); /* returns -1 if NL was inserted */
7819 /*
7820 * In replace mode, in the line we started replacing, we only move the
7821 * cursor.
7822 */
7823 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
7824 {
7825 dec_cursor();
7826 }
7827 else
7828 {
7829#ifdef FEAT_VREPLACE
7830 if (!(State & VREPLACE_FLAG)
7831 || curwin->w_cursor.lnum > orig_line_count)
7832#endif
7833 {
7834 temp = gchar_cursor(); /* remember current char */
7835 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00007836
7837 /* When "aw" is in 'formatoptions' we must delete the space at
7838 * the end of the line, otherwise the line will be broken
7839 * again when auto-formatting. */
7840 if (has_format_option(FO_AUTO)
7841 && has_format_option(FO_WHITE_PAR))
7842 {
7843 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
7844 TRUE);
7845 int len;
7846
7847 len = STRLEN(ptr);
7848 if (len > 0 && ptr[len - 1] == ' ')
7849 ptr[len - 1] = NUL;
7850 }
7851
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 (void)do_join(FALSE);
7853 if (temp == NUL && gchar_cursor() != NUL)
7854 inc_cursor();
7855 }
7856#ifdef FEAT_VREPLACE
7857 else
7858 dec_cursor();
7859#endif
7860
7861 /*
7862 * In REPLACE mode we have to put back the text that was replaced
7863 * by the NL. On the replace stack is first a NUL-terminated
7864 * sequence of characters that were deleted and then the
7865 * characters that NL replaced.
7866 */
7867 if (State & REPLACE_FLAG)
7868 {
7869 /*
7870 * Do the next ins_char() in NORMAL state, to
7871 * prevent ins_char() from replacing characters and
7872 * avoiding showmatch().
7873 */
7874 oldState = State;
7875 State = NORMAL;
7876 /*
7877 * restore characters (blanks) deleted after cursor
7878 */
7879 while (cc > 0)
7880 {
7881 temp = curwin->w_cursor.col;
7882#ifdef FEAT_MBYTE
7883 mb_replace_pop_ins(cc);
7884#else
7885 ins_char(cc);
7886#endif
7887 curwin->w_cursor.col = temp;
7888 cc = replace_pop();
7889 }
7890 /* restore the characters that NL replaced */
7891 replace_pop_ins();
7892 State = oldState;
7893 }
7894 }
7895 did_ai = FALSE;
7896 }
7897 else
7898 {
7899 /*
7900 * Delete character(s) before the cursor.
7901 */
7902#ifdef FEAT_RIGHTLEFT
7903 if (revins_on) /* put cursor on last inserted char */
7904 dec_cursor();
7905#endif
7906 mincol = 0;
7907 /* keep indent */
7908 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
7909#ifdef FEAT_RIGHTLEFT
7910 && !revins_on
7911#endif
7912 )
7913 {
7914 temp = curwin->w_cursor.col;
7915 beginline(BL_WHITE);
7916 if (curwin->w_cursor.col < (colnr_T)temp)
7917 mincol = curwin->w_cursor.col;
7918 curwin->w_cursor.col = temp;
7919 }
7920
7921 /*
7922 * Handle deleting one 'shiftwidth' or 'softtabstop'.
7923 */
7924 if ( mode == BACKSPACE_CHAR
7925 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00007926 || (curbuf->b_p_sts != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 && (*(ml_get_cursor() - 1) == TAB
7928 || (*(ml_get_cursor() - 1) == ' '
7929 && (!*inserted_space_p
7930 || arrow_used))))))
7931 {
7932 int ts;
7933 colnr_T vcol;
7934 colnr_T want_vcol;
7935 int extra = 0;
7936
7937 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00007938 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 ts = curbuf->b_p_sw;
7940 else
7941 ts = curbuf->b_p_sts;
7942 /* Compute the virtual column where we want to be. Since
7943 * 'showbreak' may get in the way, need to get the last column of
7944 * the previous character. */
7945 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7946 dec_cursor();
7947 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
7948 inc_cursor();
7949 want_vcol = (want_vcol / ts) * ts;
7950
7951 /* delete characters until we are at or before want_vcol */
7952 while (vcol > want_vcol
7953 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
7954 {
7955 dec_cursor();
7956 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
7957 if (State & REPLACE_FLAG)
7958 {
7959 /* Don't delete characters before the insert point when in
7960 * Replace mode */
7961 if (curwin->w_cursor.lnum != Insstart.lnum
7962 || curwin->w_cursor.col >= Insstart.col)
7963 {
7964#if 0 /* what was this for? It causes problems when sw != ts. */
7965 if (State == REPLACE && (int)vcol < want_vcol)
7966 {
7967 (void)del_char(FALSE);
7968 extra = 2; /* don't pop too much */
7969 }
7970 else
7971#endif
7972 replace_do_bs();
7973 }
7974 }
7975 else
7976 (void)del_char(FALSE);
7977 }
7978
7979 /* insert extra spaces until we are at want_vcol */
7980 while (vcol < want_vcol)
7981 {
7982 /* Remember the first char we inserted */
7983 if (curwin->w_cursor.lnum == Insstart.lnum
7984 && curwin->w_cursor.col < Insstart.col)
7985 Insstart.col = curwin->w_cursor.col;
7986
7987#ifdef FEAT_VREPLACE
7988 if (State & VREPLACE_FLAG)
7989 ins_char(' ');
7990 else
7991#endif
7992 {
7993 ins_str((char_u *)" ");
7994 if ((State & REPLACE_FLAG) && extra <= 1)
7995 {
7996 if (extra)
7997 replace_push_off(NUL);
7998 else
7999 replace_push(NUL);
8000 }
8001 if (extra == 2)
8002 extra = 1;
8003 }
8004 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8005 }
8006 }
8007
8008 /*
8009 * Delete upto starting point, start of line or previous word.
8010 */
8011 else do
8012 {
8013#ifdef FEAT_RIGHTLEFT
8014 if (!revins_on) /* put cursor on char to be deleted */
8015#endif
8016 dec_cursor();
8017
8018 /* start of word? */
8019 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
8020 {
8021 mode = BACKSPACE_WORD_NOT_SPACE;
8022 temp = vim_iswordc(gchar_cursor());
8023 }
8024 /* end of word? */
8025 else if (mode == BACKSPACE_WORD_NOT_SPACE
8026 && (vim_isspace(cc = gchar_cursor())
8027 || vim_iswordc(cc) != temp))
8028 {
8029#ifdef FEAT_RIGHTLEFT
8030 if (!revins_on)
8031#endif
8032 inc_cursor();
8033#ifdef FEAT_RIGHTLEFT
8034 else if (State & REPLACE_FLAG)
8035 dec_cursor();
8036#endif
8037 break;
8038 }
8039 if (State & REPLACE_FLAG)
8040 replace_do_bs();
8041 else
8042 {
8043#ifdef FEAT_MBYTE
8044 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008045 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046#endif
8047 (void)del_char(FALSE);
8048#ifdef FEAT_MBYTE
8049 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008050 * If there are combining characters and 'delcombine' is set
8051 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 * character.
8053 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008054 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 inc_cursor();
8056#endif
8057#ifdef FEAT_RIGHTLEFT
8058 if (revins_chars)
8059 {
8060 revins_chars--;
8061 revins_legal++;
8062 }
8063 if (revins_on && gchar_cursor() == NUL)
8064 break;
8065#endif
8066 }
8067 /* Just a single backspace?: */
8068 if (mode == BACKSPACE_CHAR)
8069 break;
8070 } while (
8071#ifdef FEAT_RIGHTLEFT
8072 revins_on ||
8073#endif
8074 (curwin->w_cursor.col > mincol
8075 && (curwin->w_cursor.lnum != Insstart.lnum
8076 || curwin->w_cursor.col != Insstart.col)));
8077 did_backspace = TRUE;
8078 }
8079#ifdef FEAT_SMARTINDENT
8080 did_si = FALSE;
8081 can_si = FALSE;
8082 can_si_back = FALSE;
8083#endif
8084 if (curwin->w_cursor.col <= 1)
8085 did_ai = FALSE;
8086 /*
8087 * It's a little strange to put backspaces into the redo
8088 * buffer, but it makes auto-indent a lot easier to deal
8089 * with.
8090 */
8091 AppendCharToRedobuff(c);
8092
8093 /* If deleted before the insertion point, adjust it */
8094 if (curwin->w_cursor.lnum == Insstart.lnum
8095 && curwin->w_cursor.col < Insstart.col)
8096 Insstart.col = curwin->w_cursor.col;
8097
8098 /* vi behaviour: the cursor moves backward but the character that
8099 * was there remains visible
8100 * Vim behaviour: the cursor moves backward and the character that
8101 * was there is erased from the screen.
8102 * We can emulate the vi behaviour by pretending there is a dollar
8103 * displayed even when there isn't.
8104 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8105 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
8106 dollar_vcol = curwin->w_virtcol;
8107
8108 return did_backspace;
8109}
8110
8111#ifdef FEAT_MOUSE
8112 static void
8113ins_mouse(c)
8114 int c;
8115{
8116 pos_T tpos;
8117
8118# ifdef FEAT_GUI
8119 /* When GUI is active, also move/paste when 'mouse' is empty */
8120 if (!gui.in_use)
8121# endif
8122 if (!mouse_has(MOUSE_INSERT))
8123 return;
8124
8125 undisplay_dollar();
8126 tpos = curwin->w_cursor;
8127 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
8128 {
8129 start_arrow(&tpos);
8130# ifdef FEAT_CINDENT
8131 can_cindent = TRUE;
8132# endif
8133 }
8134
8135#ifdef FEAT_WINDOWS
8136 /* redraw status lines (in case another window became active) */
8137 redraw_statuslines();
8138#endif
8139}
8140
8141 static void
8142ins_mousescroll(up)
8143 int up;
8144{
8145 pos_T tpos;
8146# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8147 win_T *old_curwin;
8148# endif
8149
8150 tpos = curwin->w_cursor;
8151
8152# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8153 old_curwin = curwin;
8154
8155 /* Currently the mouse coordinates are only known in the GUI. */
8156 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
8157 {
8158 int row, col;
8159
8160 row = mouse_row;
8161 col = mouse_col;
8162
8163 /* find the window at the pointer coordinates */
8164 curwin = mouse_find_win(&row, &col);
8165 curbuf = curwin->w_buffer;
8166 }
8167 if (curwin == old_curwin)
8168# endif
8169 undisplay_dollar();
8170
8171 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8172 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
8173 else
8174 scroll_redraw(up, 3L);
8175
8176# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8177 curwin->w_redr_status = TRUE;
8178
8179 curwin = old_curwin;
8180 curbuf = curwin->w_buffer;
8181# endif
8182
8183 if (!equalpos(curwin->w_cursor, tpos))
8184 {
8185 start_arrow(&tpos);
8186# ifdef FEAT_CINDENT
8187 can_cindent = TRUE;
8188# endif
8189 }
8190}
8191#endif
8192
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008193#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00008194 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008195ins_tabline(c)
8196 int c;
8197{
8198 /* We will be leaving the current window, unless closing another tab. */
8199 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
8200 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
8201 {
8202 undisplay_dollar();
8203 start_arrow(&curwin->w_cursor);
8204# ifdef FEAT_CINDENT
8205 can_cindent = TRUE;
8206# endif
8207 }
8208
8209 if (c == K_TABLINE)
8210 goto_tabpage(current_tab);
8211 else
8212 handle_tabmenu();
8213
8214}
8215#endif
8216
8217#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 void
8219ins_scroll()
8220{
8221 pos_T tpos;
8222
8223 undisplay_dollar();
8224 tpos = curwin->w_cursor;
8225 if (gui_do_scroll())
8226 {
8227 start_arrow(&tpos);
8228# ifdef FEAT_CINDENT
8229 can_cindent = TRUE;
8230# endif
8231 }
8232}
8233
8234 void
8235ins_horscroll()
8236{
8237 pos_T tpos;
8238
8239 undisplay_dollar();
8240 tpos = curwin->w_cursor;
8241 if (gui_do_horiz_scroll())
8242 {
8243 start_arrow(&tpos);
8244# ifdef FEAT_CINDENT
8245 can_cindent = TRUE;
8246# endif
8247 }
8248}
8249#endif
8250
8251 static void
8252ins_left()
8253{
8254 pos_T tpos;
8255
8256#ifdef FEAT_FOLDING
8257 if ((fdo_flags & FDO_HOR) && KeyTyped)
8258 foldOpenCursor();
8259#endif
8260 undisplay_dollar();
8261 tpos = curwin->w_cursor;
8262 if (oneleft() == OK)
8263 {
8264 start_arrow(&tpos);
8265#ifdef FEAT_RIGHTLEFT
8266 /* If exit reversed string, position is fixed */
8267 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
8268 revins_legal++;
8269 revins_chars++;
8270#endif
8271 }
8272
8273 /*
8274 * if 'whichwrap' set for cursor in insert mode may go to
8275 * previous line
8276 */
8277 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
8278 {
8279 start_arrow(&tpos);
8280 --(curwin->w_cursor.lnum);
8281 coladvance((colnr_T)MAXCOL);
8282 curwin->w_set_curswant = TRUE; /* so we stay at the end */
8283 }
8284 else
8285 vim_beep();
8286}
8287
8288 static void
8289ins_home(c)
8290 int c;
8291{
8292 pos_T tpos;
8293
8294#ifdef FEAT_FOLDING
8295 if ((fdo_flags & FDO_HOR) && KeyTyped)
8296 foldOpenCursor();
8297#endif
8298 undisplay_dollar();
8299 tpos = curwin->w_cursor;
8300 if (c == K_C_HOME)
8301 curwin->w_cursor.lnum = 1;
8302 curwin->w_cursor.col = 0;
8303#ifdef FEAT_VIRTUALEDIT
8304 curwin->w_cursor.coladd = 0;
8305#endif
8306 curwin->w_curswant = 0;
8307 start_arrow(&tpos);
8308}
8309
8310 static void
8311ins_end(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_END)
8323 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
8324 coladvance((colnr_T)MAXCOL);
8325 curwin->w_curswant = MAXCOL;
8326
8327 start_arrow(&tpos);
8328}
8329
8330 static void
8331ins_s_left()
8332{
8333#ifdef FEAT_FOLDING
8334 if ((fdo_flags & FDO_HOR) && KeyTyped)
8335 foldOpenCursor();
8336#endif
8337 undisplay_dollar();
8338 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
8339 {
8340 start_arrow(&curwin->w_cursor);
8341 (void)bck_word(1L, FALSE, FALSE);
8342 curwin->w_set_curswant = TRUE;
8343 }
8344 else
8345 vim_beep();
8346}
8347
8348 static void
8349ins_right()
8350{
8351#ifdef FEAT_FOLDING
8352 if ((fdo_flags & FDO_HOR) && KeyTyped)
8353 foldOpenCursor();
8354#endif
8355 undisplay_dollar();
8356 if (gchar_cursor() != NUL || virtual_active()
8357 )
8358 {
8359 start_arrow(&curwin->w_cursor);
8360 curwin->w_set_curswant = TRUE;
8361#ifdef FEAT_VIRTUALEDIT
8362 if (virtual_active())
8363 oneright();
8364 else
8365#endif
8366 {
8367#ifdef FEAT_MBYTE
8368 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008369 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 else
8371#endif
8372 ++curwin->w_cursor.col;
8373 }
8374
8375#ifdef FEAT_RIGHTLEFT
8376 revins_legal++;
8377 if (revins_chars)
8378 revins_chars--;
8379#endif
8380 }
8381 /* if 'whichwrap' set for cursor in insert mode, may move the
8382 * cursor to the next line */
8383 else if (vim_strchr(p_ww, ']') != NULL
8384 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
8385 {
8386 start_arrow(&curwin->w_cursor);
8387 curwin->w_set_curswant = TRUE;
8388 ++curwin->w_cursor.lnum;
8389 curwin->w_cursor.col = 0;
8390 }
8391 else
8392 vim_beep();
8393}
8394
8395 static void
8396ins_s_right()
8397{
8398#ifdef FEAT_FOLDING
8399 if ((fdo_flags & FDO_HOR) && KeyTyped)
8400 foldOpenCursor();
8401#endif
8402 undisplay_dollar();
8403 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
8404 || gchar_cursor() != NUL)
8405 {
8406 start_arrow(&curwin->w_cursor);
8407 (void)fwd_word(1L, FALSE, 0);
8408 curwin->w_set_curswant = TRUE;
8409 }
8410 else
8411 vim_beep();
8412}
8413
8414 static void
8415ins_up(startcol)
8416 int startcol; /* when TRUE move to Insstart.col */
8417{
8418 pos_T tpos;
8419 linenr_T old_topline = curwin->w_topline;
8420#ifdef FEAT_DIFF
8421 int old_topfill = curwin->w_topfill;
8422#endif
8423
8424 undisplay_dollar();
8425 tpos = curwin->w_cursor;
8426 if (cursor_up(1L, TRUE) == OK)
8427 {
8428 if (startcol)
8429 coladvance(getvcol_nolist(&Insstart));
8430 if (old_topline != curwin->w_topline
8431#ifdef FEAT_DIFF
8432 || old_topfill != curwin->w_topfill
8433#endif
8434 )
8435 redraw_later(VALID);
8436 start_arrow(&tpos);
8437#ifdef FEAT_CINDENT
8438 can_cindent = TRUE;
8439#endif
8440 }
8441 else
8442 vim_beep();
8443}
8444
8445 static void
8446ins_pageup()
8447{
8448 pos_T tpos;
8449
8450 undisplay_dollar();
8451 tpos = curwin->w_cursor;
8452 if (onepage(BACKWARD, 1L) == OK)
8453 {
8454 start_arrow(&tpos);
8455#ifdef FEAT_CINDENT
8456 can_cindent = TRUE;
8457#endif
8458 }
8459 else
8460 vim_beep();
8461}
8462
8463 static void
8464ins_down(startcol)
8465 int startcol; /* when TRUE move to Insstart.col */
8466{
8467 pos_T tpos;
8468 linenr_T old_topline = curwin->w_topline;
8469#ifdef FEAT_DIFF
8470 int old_topfill = curwin->w_topfill;
8471#endif
8472
8473 undisplay_dollar();
8474 tpos = curwin->w_cursor;
8475 if (cursor_down(1L, TRUE) == OK)
8476 {
8477 if (startcol)
8478 coladvance(getvcol_nolist(&Insstart));
8479 if (old_topline != curwin->w_topline
8480#ifdef FEAT_DIFF
8481 || old_topfill != curwin->w_topfill
8482#endif
8483 )
8484 redraw_later(VALID);
8485 start_arrow(&tpos);
8486#ifdef FEAT_CINDENT
8487 can_cindent = TRUE;
8488#endif
8489 }
8490 else
8491 vim_beep();
8492}
8493
8494 static void
8495ins_pagedown()
8496{
8497 pos_T tpos;
8498
8499 undisplay_dollar();
8500 tpos = curwin->w_cursor;
8501 if (onepage(FORWARD, 1L) == OK)
8502 {
8503 start_arrow(&tpos);
8504#ifdef FEAT_CINDENT
8505 can_cindent = TRUE;
8506#endif
8507 }
8508 else
8509 vim_beep();
8510}
8511
8512#ifdef FEAT_DND
8513 static void
8514ins_drop()
8515{
8516 do_put('~', BACKWARD, 1L, PUT_CURSEND);
8517}
8518#endif
8519
8520/*
8521 * Handle TAB in Insert or Replace mode.
8522 * Return TRUE when the TAB needs to be inserted like a normal character.
8523 */
8524 static int
8525ins_tab()
8526{
8527 int ind;
8528 int i;
8529 int temp;
8530
8531 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
8532 Insstart_blank_vcol = get_nolist_virtcol();
8533 if (echeck_abbr(TAB + ABBR_OFF))
8534 return FALSE;
8535
8536 ind = inindent(0);
8537#ifdef FEAT_CINDENT
8538 if (ind)
8539 can_cindent = FALSE;
8540#endif
8541
8542 /*
8543 * When nothing special, insert TAB like a normal character
8544 */
8545 if (!curbuf->b_p_et
8546 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
8547 && curbuf->b_p_sts == 0)
8548 return TRUE;
8549
8550 if (stop_arrow() == FAIL)
8551 return TRUE;
8552
8553 did_ai = FALSE;
8554#ifdef FEAT_SMARTINDENT
8555 did_si = FALSE;
8556 can_si = FALSE;
8557 can_si_back = FALSE;
8558#endif
8559 AppendToRedobuff((char_u *)"\t");
8560
8561 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
8562 temp = (int)curbuf->b_p_sw;
8563 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
8564 temp = (int)curbuf->b_p_sts;
8565 else /* otherwise use 'tabstop' */
8566 temp = (int)curbuf->b_p_ts;
8567 temp -= get_nolist_virtcol() % temp;
8568
8569 /*
8570 * Insert the first space with ins_char(). It will delete one char in
8571 * replace mode. Insert the rest with ins_str(); it will not delete any
8572 * chars. For VREPLACE mode, we use ins_char() for all characters.
8573 */
8574 ins_char(' ');
8575 while (--temp > 0)
8576 {
8577#ifdef FEAT_VREPLACE
8578 if (State & VREPLACE_FLAG)
8579 ins_char(' ');
8580 else
8581#endif
8582 {
8583 ins_str((char_u *)" ");
8584 if (State & REPLACE_FLAG) /* no char replaced */
8585 replace_push(NUL);
8586 }
8587 }
8588
8589 /*
8590 * When 'expandtab' not set: Replace spaces by TABs where possible.
8591 */
8592 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
8593 {
8594 char_u *ptr;
8595#ifdef FEAT_VREPLACE
8596 char_u *saved_line = NULL; /* init for GCC */
8597 pos_T pos;
8598#endif
8599 pos_T fpos;
8600 pos_T *cursor;
8601 colnr_T want_vcol, vcol;
8602 int change_col = -1;
8603 int save_list = curwin->w_p_list;
8604
8605 /*
8606 * Get the current line. For VREPLACE mode, don't make real changes
8607 * yet, just work on a copy of the line.
8608 */
8609#ifdef FEAT_VREPLACE
8610 if (State & VREPLACE_FLAG)
8611 {
8612 pos = curwin->w_cursor;
8613 cursor = &pos;
8614 saved_line = vim_strsave(ml_get_curline());
8615 if (saved_line == NULL)
8616 return FALSE;
8617 ptr = saved_line + pos.col;
8618 }
8619 else
8620#endif
8621 {
8622 ptr = ml_get_cursor();
8623 cursor = &curwin->w_cursor;
8624 }
8625
8626 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
8627 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8628 curwin->w_p_list = FALSE;
8629
8630 /* Find first white before the cursor */
8631 fpos = curwin->w_cursor;
8632 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
8633 {
8634 --fpos.col;
8635 --ptr;
8636 }
8637
8638 /* In Replace mode, don't change characters before the insert point. */
8639 if ((State & REPLACE_FLAG)
8640 && fpos.lnum == Insstart.lnum
8641 && fpos.col < Insstart.col)
8642 {
8643 ptr += Insstart.col - fpos.col;
8644 fpos.col = Insstart.col;
8645 }
8646
8647 /* compute virtual column numbers of first white and cursor */
8648 getvcol(curwin, &fpos, &vcol, NULL, NULL);
8649 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
8650
8651 /* Use as many TABs as possible. Beware of 'showbreak' and
8652 * 'linebreak' adding extra virtual columns. */
8653 while (vim_iswhite(*ptr))
8654 {
8655 i = lbr_chartabsize((char_u *)"\t", vcol);
8656 if (vcol + i > want_vcol)
8657 break;
8658 if (*ptr != TAB)
8659 {
8660 *ptr = TAB;
8661 if (change_col < 0)
8662 {
8663 change_col = fpos.col; /* Column of first change */
8664 /* May have to adjust Insstart */
8665 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
8666 Insstart.col = fpos.col;
8667 }
8668 }
8669 ++fpos.col;
8670 ++ptr;
8671 vcol += i;
8672 }
8673
8674 if (change_col >= 0)
8675 {
8676 int repl_off = 0;
8677
8678 /* Skip over the spaces we need. */
8679 while (vcol < want_vcol && *ptr == ' ')
8680 {
8681 vcol += lbr_chartabsize(ptr, vcol);
8682 ++ptr;
8683 ++repl_off;
8684 }
8685 if (vcol > want_vcol)
8686 {
8687 /* Must have a char with 'showbreak' just before it. */
8688 --ptr;
8689 --repl_off;
8690 }
8691 fpos.col += repl_off;
8692
8693 /* Delete following spaces. */
8694 i = cursor->col - fpos.col;
8695 if (i > 0)
8696 {
8697 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
8698 /* correct replace stack. */
8699 if ((State & REPLACE_FLAG)
8700#ifdef FEAT_VREPLACE
8701 && !(State & VREPLACE_FLAG)
8702#endif
8703 )
8704 for (temp = i; --temp >= 0; )
8705 replace_join(repl_off);
8706 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00008707#ifdef FEAT_NETBEANS_INTG
8708 if (usingNetbeans)
8709 {
8710 netbeans_removed(curbuf, fpos.lnum, cursor->col,
8711 (long)(i + 1));
8712 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
8713 (char_u *)"\t", 1);
8714 }
8715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 cursor->col -= i;
8717
8718#ifdef FEAT_VREPLACE
8719 /*
8720 * In VREPLACE mode, we haven't changed anything yet. Do it now by
8721 * backspacing over the changed spacing and then inserting the new
8722 * spacing.
8723 */
8724 if (State & VREPLACE_FLAG)
8725 {
8726 /* Backspace from real cursor to change_col */
8727 backspace_until_column(change_col);
8728
8729 /* Insert each char in saved_line from changed_col to
8730 * ptr-cursor */
8731 ins_bytes_len(saved_line + change_col,
8732 cursor->col - change_col);
8733 }
8734#endif
8735 }
8736
8737#ifdef FEAT_VREPLACE
8738 if (State & VREPLACE_FLAG)
8739 vim_free(saved_line);
8740#endif
8741 curwin->w_p_list = save_list;
8742 }
8743
8744 return FALSE;
8745}
8746
8747/*
8748 * Handle CR or NL in insert mode.
8749 * Return TRUE when out of memory or can't undo.
8750 */
8751 static int
8752ins_eol(c)
8753 int c;
8754{
8755 int i;
8756
8757 if (echeck_abbr(c + ABBR_OFF))
8758 return FALSE;
8759 if (stop_arrow() == FAIL)
8760 return TRUE;
8761 undisplay_dollar();
8762
8763 /*
8764 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
8765 * character under the cursor. Only push a NUL on the replace stack,
8766 * nothing to put back when the NL is deleted.
8767 */
8768 if ((State & REPLACE_FLAG)
8769#ifdef FEAT_VREPLACE
8770 && !(State & VREPLACE_FLAG)
8771#endif
8772 )
8773 replace_push(NUL);
8774
8775 /*
8776 * In VREPLACE mode, a NL replaces the rest of the line, and starts
8777 * replacing the next line, so we push all of the characters left on the
8778 * line onto the replace stack. This is not done here though, it is done
8779 * in open_line().
8780 */
8781
8782#ifdef FEAT_RIGHTLEFT
8783# ifdef FEAT_FKMAP
8784 if (p_altkeymap && p_fkmap)
8785 fkmap(NL);
8786# endif
8787 /* NL in reverse insert will always start in the end of
8788 * current line. */
8789 if (revins_on)
8790 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8791#endif
8792
8793 AppendToRedobuff(NL_STR);
8794 i = open_line(FORWARD,
8795#ifdef FEAT_COMMENTS
8796 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
8797#endif
8798 0, old_indent);
8799 old_indent = 0;
8800#ifdef FEAT_CINDENT
8801 can_cindent = TRUE;
8802#endif
8803
8804 return (!i);
8805}
8806
8807#ifdef FEAT_DIGRAPHS
8808/*
8809 * Handle digraph in insert mode.
8810 * Returns character still to be inserted, or NUL when nothing remaining to be
8811 * done.
8812 */
8813 static int
8814ins_digraph()
8815{
8816 int c;
8817 int cc;
8818
8819 pc_status = PC_STATUS_UNSET;
8820 if (redrawing() && !char_avail())
8821 {
8822 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008823 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824
8825 edit_putchar('?', TRUE);
8826#ifdef FEAT_CMDL_INFO
8827 add_to_showcmd_c(Ctrl_K);
8828#endif
8829 }
8830
8831#ifdef USE_ON_FLY_SCROLL
8832 dont_scroll = TRUE; /* disallow scrolling here */
8833#endif
8834
8835 /* don't map the digraph chars. This also prevents the
8836 * mode message to be deleted when ESC is hit */
8837 ++no_mapping;
8838 ++allow_keys;
8839 c = safe_vgetc();
8840 --no_mapping;
8841 --allow_keys;
8842 if (IS_SPECIAL(c) || mod_mask) /* special key */
8843 {
8844#ifdef FEAT_CMDL_INFO
8845 clear_showcmd();
8846#endif
8847 insert_special(c, TRUE, FALSE);
8848 return NUL;
8849 }
8850 if (c != ESC)
8851 {
8852 if (redrawing() && !char_avail())
8853 {
8854 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008855 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856
8857 if (char2cells(c) == 1)
8858 {
8859 /* first remove the '?', otherwise it's restored when typing
8860 * an ESC next */
8861 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00008862 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 edit_putchar(c, TRUE);
8864 }
8865#ifdef FEAT_CMDL_INFO
8866 add_to_showcmd_c(c);
8867#endif
8868 }
8869 ++no_mapping;
8870 ++allow_keys;
8871 cc = safe_vgetc();
8872 --no_mapping;
8873 --allow_keys;
8874 if (cc != ESC)
8875 {
8876 AppendToRedobuff((char_u *)CTRL_V_STR);
8877 c = getdigraph(c, cc, TRUE);
8878#ifdef FEAT_CMDL_INFO
8879 clear_showcmd();
8880#endif
8881 return c;
8882 }
8883 }
8884 edit_unputchar();
8885#ifdef FEAT_CMDL_INFO
8886 clear_showcmd();
8887#endif
8888 return NUL;
8889}
8890#endif
8891
8892/*
8893 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
8894 * Returns the char to be inserted, or NUL if none found.
8895 */
8896 static int
8897ins_copychar(lnum)
8898 linenr_T lnum;
8899{
8900 int c;
8901 int temp;
8902 char_u *ptr, *prev_ptr;
8903
8904 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8905 {
8906 vim_beep();
8907 return NUL;
8908 }
8909
8910 /* try to advance to the cursor column */
8911 temp = 0;
8912 ptr = ml_get(lnum);
8913 prev_ptr = ptr;
8914 validate_virtcol();
8915 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
8916 {
8917 prev_ptr = ptr;
8918 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
8919 }
8920 if ((colnr_T)temp > curwin->w_virtcol)
8921 ptr = prev_ptr;
8922
8923#ifdef FEAT_MBYTE
8924 c = (*mb_ptr2char)(ptr);
8925#else
8926 c = *ptr;
8927#endif
8928 if (c == NUL)
8929 vim_beep();
8930 return c;
8931}
8932
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008933/*
8934 * CTRL-Y or CTRL-E typed in Insert mode.
8935 */
8936 static int
8937ins_ctrl_ey(tc)
8938 int tc;
8939{
8940 int c = tc;
8941
8942#ifdef FEAT_INS_EXPAND
8943 if (ctrl_x_mode == CTRL_X_SCROLL)
8944 {
8945 if (c == Ctrl_Y)
8946 scrolldown_clamp();
8947 else
8948 scrollup_clamp();
8949 redraw_later(VALID);
8950 }
8951 else
8952#endif
8953 {
8954 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
8955 if (c != NUL)
8956 {
8957 long tw_save;
8958
8959 /* The character must be taken literally, insert like it
8960 * was typed after a CTRL-V, and pretend 'textwidth'
8961 * wasn't set. Digits, 'o' and 'x' are special after a
8962 * CTRL-V, don't use it for these. */
8963 if (c < 256 && !isalnum(c))
8964 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
8965 tw_save = curbuf->b_p_tw;
8966 curbuf->b_p_tw = -1;
8967 insert_special(c, TRUE, FALSE);
8968 curbuf->b_p_tw = tw_save;
8969#ifdef FEAT_RIGHTLEFT
8970 revins_chars++;
8971 revins_legal++;
8972#endif
8973 c = Ctrl_V; /* pretend CTRL-V is last character */
8974 auto_format(FALSE, TRUE);
8975 }
8976 }
8977 return c;
8978}
8979
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980#ifdef FEAT_SMARTINDENT
8981/*
8982 * Try to do some very smart auto-indenting.
8983 * Used when inserting a "normal" character.
8984 */
8985 static void
8986ins_try_si(c)
8987 int c;
8988{
8989 pos_T *pos, old_pos;
8990 char_u *ptr;
8991 int i;
8992 int temp;
8993
8994 /*
8995 * do some very smart indenting when entering '{' or '}'
8996 */
8997 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
8998 {
8999 /*
9000 * for '}' set indent equal to indent of line containing matching '{'
9001 */
9002 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
9003 {
9004 old_pos = curwin->w_cursor;
9005 /*
9006 * If the matching '{' has a ')' immediately before it (ignoring
9007 * white-space), then line up with the start of the line
9008 * containing the matching '(' if there is one. This handles the
9009 * case where an "if (..\n..) {" statement continues over multiple
9010 * lines -- webb
9011 */
9012 ptr = ml_get(pos->lnum);
9013 i = pos->col;
9014 if (i > 0) /* skip blanks before '{' */
9015 while (--i > 0 && vim_iswhite(ptr[i]))
9016 ;
9017 curwin->w_cursor.lnum = pos->lnum;
9018 curwin->w_cursor.col = i;
9019 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
9020 curwin->w_cursor = *pos;
9021 i = get_indent();
9022 curwin->w_cursor = old_pos;
9023#ifdef FEAT_VREPLACE
9024 if (State & VREPLACE_FLAG)
9025 change_indent(INDENT_SET, i, FALSE, NUL);
9026 else
9027#endif
9028 (void)set_indent(i, SIN_CHANGED);
9029 }
9030 else if (curwin->w_cursor.col > 0)
9031 {
9032 /*
9033 * when inserting '{' after "O" reduce indent, but not
9034 * more than indent of previous line
9035 */
9036 temp = TRUE;
9037 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
9038 {
9039 old_pos = curwin->w_cursor;
9040 i = get_indent();
9041 while (curwin->w_cursor.lnum > 1)
9042 {
9043 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
9044
9045 /* ignore empty lines and lines starting with '#'. */
9046 if (*ptr != '#' && *ptr != NUL)
9047 break;
9048 }
9049 if (get_indent() >= i)
9050 temp = FALSE;
9051 curwin->w_cursor = old_pos;
9052 }
9053 if (temp)
9054 shift_line(TRUE, FALSE, 1);
9055 }
9056 }
9057
9058 /*
9059 * set indent of '#' always to 0
9060 */
9061 if (curwin->w_cursor.col > 0 && can_si && c == '#')
9062 {
9063 /* remember current indent for next line */
9064 old_indent = get_indent();
9065 (void)set_indent(0, SIN_CHANGED);
9066 }
9067
9068 /* Adjust ai_col, the char at this position can be deleted. */
9069 if (ai_col > curwin->w_cursor.col)
9070 ai_col = curwin->w_cursor.col;
9071}
9072#endif
9073
9074/*
9075 * Get the value that w_virtcol would have when 'list' is off.
9076 * Unless 'cpo' contains the 'L' flag.
9077 */
9078 static colnr_T
9079get_nolist_virtcol()
9080{
9081 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9082 return getvcol_nolist(&curwin->w_cursor);
9083 validate_virtcol();
9084 return curwin->w_virtcol;
9085}