blob: 01a6591f79b7f49ee3a90c27f0fad24e20adf24d [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 Moolenaar4be06f92005-07-29 22:36:03 +000034#define CTRL_X_OCCULT 13
35#define CTRL_X_LOCAL_MSG 14 /* only used in "ctrl_x_msgs" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000036
37#define CHECK_KEYS_TIME 30
38
39#define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
40
41static char *ctrl_x_msgs[] =
42{
43 N_(" Keyword completion (^N^P)"), /* ctrl_x_mode == 0, ^P/^N compl. */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000044 N_(" ^X mode (^E^Y^L^]^F^I^K^D^U^V^N^P)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000045 NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 N_(" Whole line completion (^L^N^P)"),
47 N_(" File name completion (^F^N^P)"),
48 N_(" Tag completion (^]^N^P)"),
49 N_(" Path pattern completion (^N^P)"),
50 N_(" Definition completion (^D^N^P)"),
51 NULL,
52 N_(" Dictionary completion (^K^N^P)"),
53 N_(" Thesaurus completion (^T^N^P)"),
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000054 N_(" Command-line completion (^V^N^P)"),
55 N_(" User defined completion (^U^N^P)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000056 N_(" Occult completion (^O^N^P)"),
57 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 */
65struct Completion
66{
67 struct Completion *next;
68 struct Completion *prev;
69 char_u *str; /* matched text */
70 char_u *fname; /* file containing the match */
71 int original; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
72 int number; /* sequence number */
73};
74
75/* the original text when the expansion begun */
76#define ORIGINAL_TEXT (1)
77#define FREE_FNAME (2)
78
79/*
80 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000081 * "compl_first_match" points to the start of the list.
82 * "compl_curr_match" points to the currently selected entry.
83 * "compl_shown_match" is different from compl_curr_match during
84 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000085 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000086static struct Completion *compl_first_match = NULL;
87static struct Completion *compl_curr_match = NULL;
88static struct Completion *compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000089
Bram Moolenaar4be06f92005-07-29 22:36:03 +000090/* When the first completion is done "compl_started" is set. When it's
91 * FALSE the word to be completed must be located. */
92static int compl_started = FALSE;
93
94static int compl_matches = 0;
95static char_u *compl_pattern = NULL;
96static int compl_direction = FORWARD;
97static int compl_shows_dir = FORWARD;
98static int compl_pending = FALSE;
99static pos_T compl_startpos;
100static colnr_T compl_col = 0; /* column where the text starts
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101 that is being completed */
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000102static int save_sm = -1;
103static char_u *compl_orig_text = NULL; /* text as it was before
104 completion started */
105static int compl_cont_mode = 0;
106static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000108static void ins_ctrl_x __ARGS((void));
109static int has_compl_option __ARGS((int dict_opt));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110static int ins_compl_add __ARGS((char_u *str, int len, char_u *, int dir, int reuse));
111static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches, int dir));
112static int ins_compl_make_cyclic __ARGS((void));
113static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int dir, int flags, int thesaurus));
114static void ins_compl_free __ARGS((void));
115static void ins_compl_clear __ARGS((void));
116static void ins_compl_prep __ARGS((int c));
117static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
118static int ins_compl_get_exp __ARGS((pos_T *ini, int dir));
119static void ins_compl_delete __ARGS((void));
120static void ins_compl_insert __ARGS((void));
121static int ins_compl_next __ARGS((int allow_get_expansion));
122static int ins_complete __ARGS((int c));
123static int quote_meta __ARGS((char_u *dest, char_u *str, int len));
124#endif /* FEAT_INS_EXPAND */
125
126#define BACKSPACE_CHAR 1
127#define BACKSPACE_WORD 2
128#define BACKSPACE_WORD_NOT_SPACE 3
129#define BACKSPACE_LINE 4
130
131static void ins_redraw __ARGS((void));
132static void ins_ctrl_v __ARGS((void));
133static void undisplay_dollar __ARGS((void));
134static void insert_special __ARGS((int, int, int));
135static void check_auto_format __ARGS((int));
136static void redo_literal __ARGS((int c));
137static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaar217ad922005-03-20 22:37:15 +0000138#ifdef FEAT_SYN_HL
139static void check_spell_redraw __ARGS((void));
140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
142static int echeck_abbr __ARGS((int));
143static void replace_push_off __ARGS((int c));
144static int replace_pop __ARGS((void));
145static void replace_join __ARGS((int off));
146static void replace_pop_ins __ARGS((void));
147#ifdef FEAT_MBYTE
148static void mb_replace_pop_ins __ARGS((int cc));
149#endif
150static void replace_flush __ARGS((void));
151static void replace_do_bs __ARGS((void));
152#ifdef FEAT_CINDENT
153static int cindent_on __ARGS((void));
154#endif
155static void ins_reg __ARGS((void));
156static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000157static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158static int ins_esc __ARGS((long *count, int cmdchar));
159#ifdef FEAT_RIGHTLEFT
160static void ins_ctrl_ __ARGS((void));
161#endif
162#ifdef FEAT_VISUAL
163static int ins_start_select __ARGS((int c));
164#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000165static void ins_insert __ARGS((int replaceState));
166static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167static void ins_shift __ARGS((int c, int lastc));
168static void ins_del __ARGS((void));
169static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
170#ifdef FEAT_MOUSE
171static void ins_mouse __ARGS((int c));
172static void ins_mousescroll __ARGS((int up));
173#endif
174static void ins_left __ARGS((void));
175static void ins_home __ARGS((int c));
176static void ins_end __ARGS((int c));
177static void ins_s_left __ARGS((void));
178static void ins_right __ARGS((void));
179static void ins_s_right __ARGS((void));
180static void ins_up __ARGS((int startcol));
181static void ins_pageup __ARGS((void));
182static void ins_down __ARGS((int startcol));
183static void ins_pagedown __ARGS((void));
184#ifdef FEAT_DND
185static void ins_drop __ARGS((void));
186#endif
187static int ins_tab __ARGS((void));
188static int ins_eol __ARGS((int c));
189#ifdef FEAT_DIGRAPHS
190static int ins_digraph __ARGS((void));
191#endif
192static int ins_copychar __ARGS((linenr_T lnum));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000193static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194#ifdef FEAT_SMARTINDENT
195static void ins_try_si __ARGS((int c));
196#endif
197static colnr_T get_nolist_virtcol __ARGS((void));
198
199static colnr_T Insstart_textlen; /* length of line when insert started */
200static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
201
202static char_u *last_insert = NULL; /* the text of the previous insert,
203 K_SPECIAL and CSI are escaped */
204static int last_insert_skip; /* nr of chars in front of previous insert */
205static int new_insert_skip; /* nr of chars in front of current insert */
206
207#ifdef FEAT_CINDENT
208static int can_cindent; /* may do cindenting on this line */
209#endif
210
211static int old_indent = 0; /* for ^^D command in insert mode */
212
213#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000214static int revins_on; /* reverse insert mode on */
215static int revins_chars; /* how much to skip after edit */
216static int revins_legal; /* was the last char 'legal'? */
217static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218#endif
219
220#if defined(FEAT_MBYTE) && defined(MACOS_CLASSIC)
221static short previous_script = smRoman;
222#endif
223
224static int ins_need_undo; /* call u_save() before inserting a
225 char. Set when edit() is called.
226 after that arrow_used is used. */
227
228static int did_add_space = FALSE; /* auto_format() added an extra space
229 under the cursor */
230
231/*
232 * edit(): Start inserting text.
233 *
234 * "cmdchar" can be:
235 * 'i' normal insert command
236 * 'a' normal append command
237 * 'R' replace command
238 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
239 * but still only one <CR> is inserted. The <Esc> is not used for redo.
240 * 'g' "gI" command.
241 * 'V' "gR" command for Virtual Replace mode.
242 * 'v' "gr" command for single character Virtual Replace mode.
243 *
244 * This function is not called recursively. For CTRL-O commands, it returns
245 * and lets the caller handle the Normal-mode command.
246 *
247 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
248 */
249 int
250edit(cmdchar, startln, count)
251 int cmdchar;
252 int startln; /* if set, insert at start of line */
253 long count;
254{
255 int c = 0;
256 char_u *ptr;
257 int lastc;
258 colnr_T mincol;
259 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 int i;
261 int did_backspace = TRUE; /* previous char was backspace */
262#ifdef FEAT_CINDENT
263 int line_is_white = FALSE; /* line is empty before insert */
264#endif
265 linenr_T old_topline = 0; /* topline before insertion */
266#ifdef FEAT_DIFF
267 int old_topfill = -1;
268#endif
269 int inserted_space = FALSE; /* just inserted a space */
270 int replaceState = REPLACE;
271 int did_restart_edit = restart_edit;
272
273 /* sleep before redrawing, needed for "CTRL-O :" that results in an
274 * error message */
275 check_for_delay(TRUE);
276
277#ifdef HAVE_SANDBOX
278 /* Don't allow inserting in the sandbox. */
279 if (sandbox != 0)
280 {
281 EMSG(_(e_sandbox));
282 return FALSE;
283 }
284#endif
285
286#ifdef FEAT_INS_EXPAND
287 ins_compl_clear(); /* clear stuff for CTRL-X mode */
288#endif
289
Bram Moolenaar843ee412004-06-30 16:16:41 +0000290#ifdef FEAT_AUTOCMD
291 /*
292 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
293 */
294 if (cmdchar != 'r' && cmdchar != 'v')
295 {
296 if (cmdchar == 'R')
297 ptr = (char_u *)"r";
298 else if (cmdchar == 'V')
299 ptr = (char_u *)"v";
300 else
301 ptr = (char_u *)"i";
302 set_vim_var_string(VV_INSERTMODE, ptr, 1);
303 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
304 }
305#endif
306
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307#ifdef FEAT_MOUSE
308 /*
309 * When doing a paste with the middle mouse button, Insstart is set to
310 * where the paste started.
311 */
312 if (where_paste_started.lnum != 0)
313 Insstart = where_paste_started;
314 else
315#endif
316 {
317 Insstart = curwin->w_cursor;
318 if (startln)
319 Insstart.col = 0;
320 }
321 Insstart_textlen = linetabsize(ml_get_curline());
322 Insstart_blank_vcol = MAXCOL;
323 if (!did_ai)
324 ai_col = 0;
325
326 if (cmdchar != NUL && restart_edit == 0)
327 {
328 ResetRedobuff();
329 AppendNumberToRedobuff(count);
330#ifdef FEAT_VREPLACE
331 if (cmdchar == 'V' || cmdchar == 'v')
332 {
333 /* "gR" or "gr" command */
334 AppendCharToRedobuff('g');
335 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
336 }
337 else
338#endif
339 {
340 AppendCharToRedobuff(cmdchar);
341 if (cmdchar == 'g') /* "gI" command */
342 AppendCharToRedobuff('I');
343 else if (cmdchar == 'r') /* "r<CR>" command */
344 count = 1; /* insert only one <CR> */
345 }
346 }
347
348 if (cmdchar == 'R')
349 {
350#ifdef FEAT_FKMAP
351 if (p_fkmap && p_ri)
352 {
353 beep_flush();
354 EMSG(farsi_text_3); /* encoded in Farsi */
355 State = INSERT;
356 }
357 else
358#endif
359 State = REPLACE;
360 }
361#ifdef FEAT_VREPLACE
362 else if (cmdchar == 'V' || cmdchar == 'v')
363 {
364 State = VREPLACE;
365 replaceState = VREPLACE;
366 orig_line_count = curbuf->b_ml.ml_line_count;
367 vr_lines_changed = 1;
368 }
369#endif
370 else
371 State = INSERT;
372
373 stop_insert_mode = FALSE;
374
375 /*
376 * Need to recompute the cursor position, it might move when the cursor is
377 * on a TAB or special character.
378 */
379 curs_columns(TRUE);
380
381 /*
382 * Enable langmap or IME, indicated by 'iminsert'.
383 * Note that IME may enabled/disabled without us noticing here, thus the
384 * 'iminsert' value may not reflect what is actually used. It is updated
385 * when hitting <Esc>.
386 */
387 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
388 State |= LANGMAP;
389#ifdef USE_IM_CONTROL
390 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
391#endif
392
393#if defined(FEAT_MBYTE) && defined(MACOS_CLASSIC)
394 KeyScript(previous_script);
395#endif
396
397#ifdef FEAT_MOUSE
398 setmouse();
399#endif
400#ifdef FEAT_CMDL_INFO
401 clear_showcmd();
402#endif
403#ifdef FEAT_RIGHTLEFT
404 /* there is no reverse replace mode */
405 revins_on = (State == INSERT && p_ri);
406 if (revins_on)
407 undisplay_dollar();
408 revins_chars = 0;
409 revins_legal = 0;
410 revins_scol = -1;
411#endif
412
413 /*
414 * Handle restarting Insert mode.
415 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
416 * restart_edit non-zero, and something in the stuff buffer.
417 */
418 if (restart_edit != 0 && stuff_empty())
419 {
420#ifdef FEAT_MOUSE
421 /*
422 * After a paste we consider text typed to be part of the insert for
423 * the pasted text. You can backspace over the pasted text too.
424 */
425 if (where_paste_started.lnum)
426 arrow_used = FALSE;
427 else
428#endif
429 arrow_used = TRUE;
430 restart_edit = 0;
431
432 /*
433 * If the cursor was after the end-of-line before the CTRL-O and it is
434 * now at the end-of-line, put it after the end-of-line (this is not
435 * correct in very rare cases).
436 * Also do this if curswant is greater than the current virtual
437 * column. Eg after "^O$" or "^O80|".
438 */
439 validate_virtcol();
440 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000441 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 || curwin->w_curswant > curwin->w_virtcol)
443 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
444 {
445 if (ptr[1] == NUL)
446 ++curwin->w_cursor.col;
447#ifdef FEAT_MBYTE
448 else if (has_mbyte)
449 {
450 i = (*mb_ptr2len_check)(ptr);
451 if (ptr[i] == NUL)
452 curwin->w_cursor.col += i;
453 }
454#endif
455 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000456 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 }
458 else
459 arrow_used = FALSE;
460
461 /* we are in insert mode now, don't need to start it anymore */
462 need_start_insertmode = FALSE;
463
464 /* Need to save the line for undo before inserting the first char. */
465 ins_need_undo = TRUE;
466
467#ifdef FEAT_MOUSE
468 where_paste_started.lnum = 0;
469#endif
470#ifdef FEAT_CINDENT
471 can_cindent = TRUE;
472#endif
473#ifdef FEAT_FOLDING
474 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
475 * restarting. */
476 if (!p_im && did_restart_edit == 0)
477 foldOpenCursor();
478#endif
479
480 /*
481 * If 'showmode' is set, show the current (insert/replace/..) mode.
482 * A warning message for changing a readonly file is given here, before
483 * actually changing anything. It's put after the mode, if any.
484 */
485 i = 0;
486 if (p_smd)
487 i = showmode();
488
489 if (!p_im && did_restart_edit == 0)
490 change_warning(i + 1);
491
492#ifdef CURSOR_SHAPE
493 ui_cursor_shape(); /* may show different cursor shape */
494#endif
495#ifdef FEAT_DIGRAPHS
496 do_digraph(-1); /* clear digraphs */
497#endif
498
499/*
500 * Get the current length of the redo buffer, those characters have to be
501 * skipped if we want to get to the inserted characters.
502 */
503 ptr = get_inserted();
504 if (ptr == NULL)
505 new_insert_skip = 0;
506 else
507 {
508 new_insert_skip = (int)STRLEN(ptr);
509 vim_free(ptr);
510 }
511
512 old_indent = 0;
513
514 /*
515 * Main loop in Insert mode: repeat until Insert mode is left.
516 */
517 for (;;)
518 {
519#ifdef FEAT_RIGHTLEFT
520 if (!revins_legal)
521 revins_scol = -1; /* reset on illegal motions */
522 else
523 revins_legal = 0;
524#endif
525 if (arrow_used) /* don't repeat insert when arrow key used */
526 count = 0;
527
528 if (stop_insert_mode)
529 {
530 /* ":stopinsert" used or 'insertmode' reset */
531 count = 0;
532 goto doESCkey;
533 }
534
535 /* set curwin->w_curswant for next K_DOWN or K_UP */
536 if (!arrow_used)
537 curwin->w_set_curswant = TRUE;
538
539 /* If there is no typeahead may check for timestamps (e.g., for when a
540 * menu invoked a shell command). */
541 if (stuff_empty())
542 {
543 did_check_timestamps = FALSE;
544 if (need_check_timestamps)
545 check_timestamps(FALSE);
546 }
547
548 /*
549 * When emsg() was called msg_scroll will have been set.
550 */
551 msg_scroll = FALSE;
552
553#ifdef FEAT_GUI
554 /* When 'mousefocus' is set a mouse movement may have taken us to
555 * another window. "need_mouse_correct" may then be set because of an
556 * autocommand. */
557 if (need_mouse_correct)
558 gui_mouse_correct();
559#endif
560
561#ifdef FEAT_FOLDING
562 /* Open fold at the cursor line, according to 'foldopen'. */
563 if (fdo_flags & FDO_INSERT)
564 foldOpenCursor();
565 /* Close folds where the cursor isn't, according to 'foldclose' */
566 if (!char_avail())
567 foldCheckClose();
568#endif
569
570 /*
571 * If we inserted a character at the last position of the last line in
572 * the window, scroll the window one line up. This avoids an extra
573 * redraw.
574 * This is detected when the cursor column is smaller after inserting
575 * something.
576 * Don't do this when the topline changed already, it has
577 * already been adjusted (by insertchar() calling open_line())).
578 */
579 if (curbuf->b_mod_set
580 && curwin->w_p_wrap
581 && !did_backspace
582 && curwin->w_topline == old_topline
583#ifdef FEAT_DIFF
584 && curwin->w_topfill == old_topfill
585#endif
586 )
587 {
588 mincol = curwin->w_wcol;
589 validate_cursor_col();
590
591 if ((int)curwin->w_wcol < (int)mincol - curbuf->b_p_ts
592 && curwin->w_wrow == W_WINROW(curwin)
593 + curwin->w_height - 1 - p_so
594 && (curwin->w_cursor.lnum != curwin->w_topline
595#ifdef FEAT_DIFF
596 || curwin->w_topfill > 0
597#endif
598 ))
599 {
600#ifdef FEAT_DIFF
601 if (curwin->w_topfill > 0)
602 --curwin->w_topfill;
603 else
604#endif
605#ifdef FEAT_FOLDING
606 if (hasFolding(curwin->w_topline, NULL, &old_topline))
607 set_topline(curwin, old_topline + 1);
608 else
609#endif
610 set_topline(curwin, curwin->w_topline + 1);
611 }
612 }
613
614 /* May need to adjust w_topline to show the cursor. */
615 update_topline();
616
617 did_backspace = FALSE;
618
619 validate_cursor(); /* may set must_redraw */
620
621 /*
622 * Redraw the display when no characters are waiting.
623 * Also shows mode, ruler and positions cursor.
624 */
625 ins_redraw();
626
627#ifdef FEAT_SCROLLBIND
628 if (curwin->w_p_scb)
629 do_check_scrollbind(TRUE);
630#endif
631
632 update_curswant();
633 old_topline = curwin->w_topline;
634#ifdef FEAT_DIFF
635 old_topfill = curwin->w_topfill;
636#endif
637
638#ifdef USE_ON_FLY_SCROLL
639 dont_scroll = FALSE; /* allow scrolling here */
640#endif
641
642 /*
643 * Get a character for Insert mode.
644 */
645 lastc = c; /* remember previous char for CTRL-D */
646 c = safe_vgetc();
647
648#ifdef FEAT_RIGHTLEFT
649 if (p_hkmap && KeyTyped)
650 c = hkmap(c); /* Hebrew mode mapping */
651#endif
652#ifdef FEAT_FKMAP
653 if (p_fkmap && KeyTyped)
654 c = fkmap(c); /* Farsi mode mapping */
655#endif
656
657#ifdef FEAT_INS_EXPAND
658 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
659 * it does fix up the text when finishing completion. */
660 ins_compl_prep(c);
661#endif
662
663 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to mode
664 * selected with 'insertmode'. */
665 if (c == Ctrl_BSL)
666 {
667 /* may need to redraw when no more chars available now */
668 ins_redraw();
669 ++no_mapping;
670 ++allow_keys;
671 c = safe_vgetc();
672 --no_mapping;
673 --allow_keys;
674 if (c != Ctrl_N && c != Ctrl_G) /* it's something else */
675 {
676 vungetc(c);
677 c = Ctrl_BSL;
678 }
679 else if (c == Ctrl_G && p_im)
680 continue;
681 else
682 {
683 count = 0;
684 goto doESCkey;
685 }
686 }
687
688#ifdef FEAT_DIGRAPHS
689 c = do_digraph(c);
690#endif
691
692#ifdef FEAT_INS_EXPAND
693 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
694 goto docomplete;
695#endif
696 if (c == Ctrl_V || c == Ctrl_Q)
697 {
698 ins_ctrl_v();
699 c = Ctrl_V; /* pretend CTRL-V is last typed character */
700 continue;
701 }
702
703#ifdef FEAT_CINDENT
704 if (cindent_on()
705# ifdef FEAT_INS_EXPAND
706 && ctrl_x_mode == 0
707# endif
708 )
709 {
710 /* A key name preceded by a bang means this key is not to be
711 * inserted. Skip ahead to the re-indenting below.
712 * A key name preceded by a star means that indenting has to be
713 * done before inserting the key. */
714 line_is_white = inindent(0);
715 if (in_cinkeys(c, '!', line_is_white))
716 goto force_cindent;
717 if (can_cindent && in_cinkeys(c, '*', line_is_white)
718 && stop_arrow() == OK)
719 do_c_expr_indent();
720 }
721#endif
722
723#ifdef FEAT_RIGHTLEFT
724 if (curwin->w_p_rl)
725 switch (c)
726 {
727 case K_LEFT: c = K_RIGHT; break;
728 case K_S_LEFT: c = K_S_RIGHT; break;
729 case K_C_LEFT: c = K_C_RIGHT; break;
730 case K_RIGHT: c = K_LEFT; break;
731 case K_S_RIGHT: c = K_S_LEFT; break;
732 case K_C_RIGHT: c = K_C_LEFT; break;
733 }
734#endif
735
736#ifdef FEAT_VISUAL
737 /*
738 * If 'keymodel' contains "startsel", may start selection. If it
739 * does, a CTRL-O and c will be stuffed, we need to get these
740 * characters.
741 */
742 if (ins_start_select(c))
743 continue;
744#endif
745
746 /*
747 * The big switch to handle a character in insert mode.
748 */
749 switch (c)
750 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000751 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 if (echeck_abbr(ESC + ABBR_OFF))
753 break;
754 /*FALLTHROUGH*/
755
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000756 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757#ifdef FEAT_CMDWIN
758 if (c == Ctrl_C && cmdwin_type != 0)
759 {
760 /* Close the cmdline window. */
761 cmdwin_result = K_IGNORE;
762 got_int = FALSE; /* don't stop executing autocommands et al. */
763 goto doESCkey;
764 }
765#endif
766
767#ifdef UNIX
768do_intr:
769#endif
770 /* when 'insertmode' set, and not halfway a mapping, don't leave
771 * Insert mode */
772 if (goto_im())
773 {
774 if (got_int)
775 {
776 (void)vgetc(); /* flush all buffers */
777 got_int = FALSE;
778 }
779 else
780 vim_beep();
781 break;
782 }
783doESCkey:
784 /*
785 * This is the ONLY return from edit()!
786 */
787 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
788 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000789 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 o_lnum = curwin->w_cursor.lnum;
791
792 if (ins_esc(&count, cmdchar))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000793 {
794#ifdef FEAT_AUTOCMD
795 if (cmdchar != 'r' && cmdchar != 'v')
796 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
797 FALSE, curbuf);
798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 continue;
802
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000803 case K_INS: /* toggle insert/replace mode */
804 case K_KINS:
805 ins_insert(replaceState);
806 break;
807
808#ifdef FEAT_INS_EXPAND
809 case Ctrl_X: /* Enter CTRL-X mode */
810 ins_ctrl_x();
811 break;
812#endif
813
814 case K_SELECT: /* end of Select mode mapping - ignore */
815 break;
816
817 case Ctrl_Z: /* suspend when 'insertmode' set */
818 if (!p_im)
819 goto normalchar; /* insert CTRL-Z as normal char */
820 stuffReadbuff((char_u *)":st\r");
821 c = Ctrl_O;
822 /*FALLTHROUGH*/
823
824 case Ctrl_O: /* execute one command */
825#ifdef FEAT_INS_EXPAND
826 if (ctrl_x_mode == CTRL_X_OCCULT)
827 goto docomplete;
828#endif
829 if (echeck_abbr(Ctrl_O + ABBR_OFF))
830 break;
831 ins_ctrl_o();
832 count = 0;
833 goto doESCkey;
834
835#ifdef FEAT_SNIFF
836 case K_SNIFF: /* Sniff command received */
837 stuffcharReadbuff(K_SNIFF);
838 goto doESCkey;
839#endif
840
841 case K_HELP: /* Help key works like <ESC> <Help> */
842 case K_F1:
843 case K_XF1:
844 stuffcharReadbuff(K_HELP);
845 if (p_im)
846 need_start_insertmode = TRUE;
847 goto doESCkey;
848
849#ifdef FEAT_NETBEANS_INTG
850 case K_F21: /* NetBeans command */
851 ++no_mapping; /* don't map the next key hits */
852 i = safe_vgetc();
853 --no_mapping;
854 netbeans_keycommand(i);
855 break;
856#endif
857
858 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 case NUL:
860 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000861 /* For ^@ the trailing ESC will end the insert, unless there is an
862 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
864 && c != Ctrl_A && !p_im)
865 goto doESCkey; /* quit insert mode */
866 inserted_space = FALSE;
867 break;
868
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000869 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 ins_reg();
871 auto_format(FALSE, TRUE);
872 inserted_space = FALSE;
873 break;
874
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000875 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 ins_ctrl_g();
877 break;
878
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000879 case Ctrl_HAT: /* switch input mode and/or langmap */
880 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 break;
882
883#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000884 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 if (!p_ari)
886 goto normalchar;
887 ins_ctrl_();
888 break;
889#endif
890
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000891 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
893 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
894 goto docomplete;
895#endif
896 /* FALLTHROUGH */
897
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000898 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899# ifdef FEAT_INS_EXPAND
900 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
901 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000902 if (has_compl_option(FALSE))
903 goto docomplete;
904 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 }
906# endif
907 ins_shift(c, lastc);
908 auto_format(FALSE, TRUE);
909 inserted_space = FALSE;
910 break;
911
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000912 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 case K_KDEL:
914 ins_del();
915 auto_format(FALSE, TRUE);
916 break;
917
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000918 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 case Ctrl_H:
920 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
921 auto_format(FALSE, TRUE);
922 break;
923
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000924 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
926 auto_format(FALSE, TRUE);
927 break;
928
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000929 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000930# ifdef FEAT_COMPL_FUNC
931 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000932 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000933 goto docomplete;
934# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
936 auto_format(FALSE, TRUE);
937 inserted_space = FALSE;
938 break;
939
940#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000941 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 case K_LEFTMOUSE_NM:
943 case K_LEFTDRAG:
944 case K_LEFTRELEASE:
945 case K_LEFTRELEASE_NM:
946 case K_MIDDLEMOUSE:
947 case K_MIDDLEDRAG:
948 case K_MIDDLERELEASE:
949 case K_RIGHTMOUSE:
950 case K_RIGHTDRAG:
951 case K_RIGHTRELEASE:
952 case K_X1MOUSE:
953 case K_X1DRAG:
954 case K_X1RELEASE:
955 case K_X2MOUSE:
956 case K_X2DRAG:
957 case K_X2RELEASE:
958 ins_mouse(c);
959 break;
960
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000961 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 ins_mousescroll(FALSE);
963 break;
964
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000965 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 ins_mousescroll(TRUE);
967 break;
968#endif
969
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000970 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 break;
972
973#ifdef FEAT_GUI
974 case K_VER_SCROLLBAR:
975 ins_scroll();
976 break;
977
978 case K_HOR_SCROLLBAR:
979 ins_horscroll();
980 break;
981#endif
982
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000983 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 case K_S_HOME:
986 case K_C_HOME:
987 ins_home(c);
988 break;
989
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000990 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 case K_S_END:
993 case K_C_END:
994 ins_end(c);
995 break;
996
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000997 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000998 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
999 ins_s_left();
1000 else
1001 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 break;
1003
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001004 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 case K_C_LEFT:
1006 ins_s_left();
1007 break;
1008
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001009 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001010 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1011 ins_s_right();
1012 else
1013 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 break;
1015
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001016 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 case K_C_RIGHT:
1018 ins_s_right();
1019 break;
1020
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001021 case K_UP: /* <Up> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001022 if (mod_mask & MOD_MASK_SHIFT)
1023 ins_pageup();
1024 else
1025 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 break;
1027
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001028 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 case K_PAGEUP:
1030 case K_KPAGEUP:
1031 ins_pageup();
1032 break;
1033
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001034 case K_DOWN: /* <Down> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001035 if (mod_mask & MOD_MASK_SHIFT)
1036 ins_pagedown();
1037 else
1038 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 break;
1040
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001041 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 case K_PAGEDOWN:
1043 case K_KPAGEDOWN:
1044 ins_pagedown();
1045 break;
1046
1047#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001048 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 ins_drop();
1050 break;
1051#endif
1052
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001053 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 c = TAB;
1055 /* FALLTHROUGH */
1056
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001057 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1059 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1060 goto docomplete;
1061#endif
1062 inserted_space = FALSE;
1063 if (ins_tab())
1064 goto normalchar; /* insert TAB as a normal char */
1065 auto_format(FALSE, TRUE);
1066 break;
1067
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001068 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 c = CAR;
1070 /* FALLTHROUGH */
1071 case CAR:
1072 case NL:
1073#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1074 /* In a quickfix window a <CR> jumps to the error under the
1075 * cursor. */
1076 if (bt_quickfix(curbuf) && c == CAR)
1077 {
1078 do_cmdline_cmd((char_u *)".cc");
1079 break;
1080 }
1081#endif
1082#ifdef FEAT_CMDWIN
1083 if (cmdwin_type != 0)
1084 {
1085 /* Execute the command in the cmdline window. */
1086 cmdwin_result = CAR;
1087 goto doESCkey;
1088 }
1089#endif
1090 if (ins_eol(c) && !p_im)
1091 goto doESCkey; /* out of memory */
1092 auto_format(FALSE, FALSE);
1093 inserted_space = FALSE;
1094 break;
1095
1096#if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001097 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098# ifdef FEAT_INS_EXPAND
1099 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1100 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001101 if (has_compl_option(TRUE))
1102 goto docomplete;
1103 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 }
1105# endif
1106# ifdef FEAT_DIGRAPHS
1107 c = ins_digraph();
1108 if (c == NUL)
1109 break;
1110# endif
1111 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001112#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113
1114#ifdef FEAT_INS_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001115 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 if (ctrl_x_mode != CTRL_X_TAGS)
1117 goto normalchar;
1118 goto docomplete;
1119
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001120 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 if (ctrl_x_mode != CTRL_X_FILES)
1122 goto normalchar;
1123 goto docomplete;
1124#endif
1125
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001126 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127#ifdef FEAT_INS_EXPAND
1128 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1129#endif
1130 {
1131 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1132 if (p_im)
1133 {
1134 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1135 break;
1136 goto doESCkey;
1137 }
1138 goto normalchar;
1139 }
1140#ifdef FEAT_INS_EXPAND
1141 /* FALLTHROUGH */
1142
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001143 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 case Ctrl_N:
1145 /* if 'complete' is empty then plain ^P is no longer special,
1146 * but it is under other ^X modes */
1147 if (*curbuf->b_p_cpt == NUL
1148 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001149 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 goto normalchar;
1151
1152docomplete:
1153 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001154 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 break;
1156#endif /* FEAT_INS_EXPAND */
1157
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001158 case Ctrl_Y: /* copy from previous line or scroll down */
1159 case Ctrl_E: /* copy from next line or scroll up */
1160 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 break;
1162
1163 default:
1164#ifdef UNIX
1165 if (c == intr_char) /* special interrupt char */
1166 goto do_intr;
1167#endif
1168
1169 /*
1170 * Insert a nomal character.
1171 */
1172normalchar:
1173#ifdef FEAT_SMARTINDENT
1174 /* Try to perform smart-indenting. */
1175 ins_try_si(c);
1176#endif
1177
1178 if (c == ' ')
1179 {
1180 inserted_space = TRUE;
1181#ifdef FEAT_CINDENT
1182 if (inindent(0))
1183 can_cindent = FALSE;
1184#endif
1185 if (Insstart_blank_vcol == MAXCOL
1186 && curwin->w_cursor.lnum == Insstart.lnum)
1187 Insstart_blank_vcol = get_nolist_virtcol();
1188 }
1189
1190 if (vim_iswordc(c) || !echeck_abbr(
1191#ifdef FEAT_MBYTE
1192 /* Add ABBR_OFF for characters above 0x100, this is
1193 * what check_abbr() expects. */
1194 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1195#endif
1196 c))
1197 {
1198 insert_special(c, FALSE, FALSE);
1199#ifdef FEAT_RIGHTLEFT
1200 revins_legal++;
1201 revins_chars++;
1202#endif
1203 }
1204
1205 auto_format(FALSE, TRUE);
1206
1207#ifdef FEAT_FOLDING
1208 /* When inserting a character the cursor line must never be in a
1209 * closed fold. */
1210 foldOpenCursor();
1211#endif
1212 break;
1213 } /* end of switch (c) */
1214
1215 /* If the cursor was moved we didn't just insert a space */
1216 if (arrow_used)
1217 inserted_space = FALSE;
1218
1219#ifdef FEAT_CINDENT
1220 if (can_cindent && cindent_on()
1221# ifdef FEAT_INS_EXPAND
1222 && ctrl_x_mode == 0
1223# endif
1224 )
1225 {
1226force_cindent:
1227 /*
1228 * Indent now if a key was typed that is in 'cinkeys'.
1229 */
1230 if (in_cinkeys(c, ' ', line_is_white))
1231 {
1232 if (stop_arrow() == OK)
1233 /* re-indent the current line */
1234 do_c_expr_indent();
1235 }
1236 }
1237#endif /* FEAT_CINDENT */
1238
1239 } /* for (;;) */
1240 /* NOTREACHED */
1241}
1242
1243/*
1244 * Redraw for Insert mode.
1245 * This is postponed until getting the next character to make '$' in the 'cpo'
1246 * option work correctly.
1247 * Only redraw when there are no characters available. This speeds up
1248 * inserting sequences of characters (e.g., for CTRL-R).
1249 */
1250 static void
1251ins_redraw()
1252{
1253 if (!char_avail())
1254 {
1255 if (must_redraw)
1256 update_screen(0);
1257 else if (clear_cmdline || redraw_cmdline)
1258 showmode(); /* clear cmdline and show mode */
1259 showruler(FALSE);
1260 setcursor();
1261 emsg_on_display = FALSE; /* may remove error message now */
1262 }
1263}
1264
1265/*
1266 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1267 */
1268 static void
1269ins_ctrl_v()
1270{
1271 int c;
1272
1273 /* may need to redraw when no more chars available now */
1274 ins_redraw();
1275
1276 if (redrawing() && !char_avail())
1277 edit_putchar('^', TRUE);
1278 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1279
1280#ifdef FEAT_CMDL_INFO
1281 add_to_showcmd_c(Ctrl_V);
1282#endif
1283
1284 c = get_literal();
1285#ifdef FEAT_CMDL_INFO
1286 clear_showcmd();
1287#endif
1288 insert_special(c, FALSE, TRUE);
1289#ifdef FEAT_RIGHTLEFT
1290 revins_chars++;
1291 revins_legal++;
1292#endif
1293}
1294
1295/*
1296 * Put a character directly onto the screen. It's not stored in a buffer.
1297 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1298 */
1299static int pc_status;
1300#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1301#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1302#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1303#define PC_STATUS_SET 3 /* pc_bytes was filled */
1304#ifdef FEAT_MBYTE
1305static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1306#else
1307static char_u pc_bytes[2]; /* saved bytes */
1308#endif
1309static int pc_attr;
1310static int pc_row;
1311static int pc_col;
1312
1313 void
1314edit_putchar(c, highlight)
1315 int c;
1316 int highlight;
1317{
1318 int attr;
1319
1320 if (ScreenLines != NULL)
1321 {
1322 update_topline(); /* just in case w_topline isn't valid */
1323 validate_cursor();
1324 if (highlight)
1325 attr = hl_attr(HLF_8);
1326 else
1327 attr = 0;
1328 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1329 pc_col = W_WINCOL(curwin);
1330#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1331 pc_status = PC_STATUS_UNSET;
1332#endif
1333#ifdef FEAT_RIGHTLEFT
1334 if (curwin->w_p_rl)
1335 {
1336 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1337# ifdef FEAT_MBYTE
1338 if (has_mbyte)
1339 {
1340 int fix_col = mb_fix_col(pc_col, pc_row);
1341
1342 if (fix_col != pc_col)
1343 {
1344 screen_putchar(' ', pc_row, fix_col, attr);
1345 --curwin->w_wcol;
1346 pc_status = PC_STATUS_RIGHT;
1347 }
1348 }
1349# endif
1350 }
1351 else
1352#endif
1353 {
1354 pc_col += curwin->w_wcol;
1355#ifdef FEAT_MBYTE
1356 if (mb_lefthalve(pc_row, pc_col))
1357 pc_status = PC_STATUS_LEFT;
1358#endif
1359 }
1360
1361 /* save the character to be able to put it back */
1362#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1363 if (pc_status == PC_STATUS_UNSET)
1364#endif
1365 {
1366 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1367 pc_status = PC_STATUS_SET;
1368 }
1369 screen_putchar(c, pc_row, pc_col, attr);
1370 }
1371}
1372
1373/*
1374 * Undo the previous edit_putchar().
1375 */
1376 void
1377edit_unputchar()
1378{
1379 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1380 {
1381#if defined(FEAT_MBYTE)
1382 if (pc_status == PC_STATUS_RIGHT)
1383 ++curwin->w_wcol;
1384 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1385 redrawWinline(curwin->w_cursor.lnum, FALSE);
1386 else
1387#endif
1388 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1389 }
1390}
1391
1392/*
1393 * Called when p_dollar is set: display a '$' at the end of the changed text
1394 * Only works when cursor is in the line that changes.
1395 */
1396 void
1397display_dollar(col)
1398 colnr_T col;
1399{
1400 colnr_T save_col;
1401
1402 if (!redrawing())
1403 return;
1404
1405 cursor_off();
1406 save_col = curwin->w_cursor.col;
1407 curwin->w_cursor.col = col;
1408#ifdef FEAT_MBYTE
1409 if (has_mbyte)
1410 {
1411 char_u *p;
1412
1413 /* If on the last byte of a multi-byte move to the first byte. */
1414 p = ml_get_curline();
1415 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1416 }
1417#endif
1418 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1419 if (curwin->w_wcol < W_WIDTH(curwin))
1420 {
1421 edit_putchar('$', FALSE);
1422 dollar_vcol = curwin->w_virtcol;
1423 }
1424 curwin->w_cursor.col = save_col;
1425}
1426
1427/*
1428 * Call this function before moving the cursor from the normal insert position
1429 * in insert mode.
1430 */
1431 static void
1432undisplay_dollar()
1433{
1434 if (dollar_vcol)
1435 {
1436 dollar_vcol = 0;
1437 redrawWinline(curwin->w_cursor.lnum, FALSE);
1438 }
1439}
1440
1441/*
1442 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1443 * Keep the cursor on the same character.
1444 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1445 * type == INDENT_DEC decrease indent (for CTRL-D)
1446 * type == INDENT_SET set indent to "amount"
1447 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1448 */
1449 void
1450change_indent(type, amount, round, replaced)
1451 int type;
1452 int amount;
1453 int round;
1454 int replaced; /* replaced character, put on replace stack */
1455{
1456 int vcol;
1457 int last_vcol;
1458 int insstart_less; /* reduction for Insstart.col */
1459 int new_cursor_col;
1460 int i;
1461 char_u *ptr;
1462 int save_p_list;
1463 int start_col;
1464 colnr_T vc;
1465#ifdef FEAT_VREPLACE
1466 colnr_T orig_col = 0; /* init for GCC */
1467 char_u *new_line, *orig_line = NULL; /* init for GCC */
1468
1469 /* VREPLACE mode needs to know what the line was like before changing */
1470 if (State & VREPLACE_FLAG)
1471 {
1472 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1473 orig_col = curwin->w_cursor.col;
1474 }
1475#endif
1476
1477 /* for the following tricks we don't want list mode */
1478 save_p_list = curwin->w_p_list;
1479 curwin->w_p_list = FALSE;
1480 vc = getvcol_nolist(&curwin->w_cursor);
1481 vcol = vc;
1482
1483 /*
1484 * For Replace mode we need to fix the replace stack later, which is only
1485 * possible when the cursor is in the indent. Remember the number of
1486 * characters before the cursor if it's possible.
1487 */
1488 start_col = curwin->w_cursor.col;
1489
1490 /* determine offset from first non-blank */
1491 new_cursor_col = curwin->w_cursor.col;
1492 beginline(BL_WHITE);
1493 new_cursor_col -= curwin->w_cursor.col;
1494
1495 insstart_less = curwin->w_cursor.col;
1496
1497 /*
1498 * If the cursor is in the indent, compute how many screen columns the
1499 * cursor is to the left of the first non-blank.
1500 */
1501 if (new_cursor_col < 0)
1502 vcol = get_indent() - vcol;
1503
1504 if (new_cursor_col > 0) /* can't fix replace stack */
1505 start_col = -1;
1506
1507 /*
1508 * Set the new indent. The cursor will be put on the first non-blank.
1509 */
1510 if (type == INDENT_SET)
1511 (void)set_indent(amount, SIN_CHANGED);
1512 else
1513 {
1514#ifdef FEAT_VREPLACE
1515 int save_State = State;
1516
1517 /* Avoid being called recursively. */
1518 if (State & VREPLACE_FLAG)
1519 State = INSERT;
1520#endif
1521 shift_line(type == INDENT_DEC, round, 1);
1522#ifdef FEAT_VREPLACE
1523 State = save_State;
1524#endif
1525 }
1526 insstart_less -= curwin->w_cursor.col;
1527
1528 /*
1529 * Try to put cursor on same character.
1530 * If the cursor is at or after the first non-blank in the line,
1531 * compute the cursor column relative to the column of the first
1532 * non-blank character.
1533 * If we are not in insert mode, leave the cursor on the first non-blank.
1534 * If the cursor is before the first non-blank, position it relative
1535 * to the first non-blank, counted in screen columns.
1536 */
1537 if (new_cursor_col >= 0)
1538 {
1539 /*
1540 * When changing the indent while the cursor is touching it, reset
1541 * Insstart_col to 0.
1542 */
1543 if (new_cursor_col == 0)
1544 insstart_less = MAXCOL;
1545 new_cursor_col += curwin->w_cursor.col;
1546 }
1547 else if (!(State & INSERT))
1548 new_cursor_col = curwin->w_cursor.col;
1549 else
1550 {
1551 /*
1552 * Compute the screen column where the cursor should be.
1553 */
1554 vcol = get_indent() - vcol;
1555 curwin->w_virtcol = (vcol < 0) ? 0 : vcol;
1556
1557 /*
1558 * Advance the cursor until we reach the right screen column.
1559 */
1560 vcol = last_vcol = 0;
1561 new_cursor_col = -1;
1562 ptr = ml_get_curline();
1563 while (vcol <= (int)curwin->w_virtcol)
1564 {
1565 last_vcol = vcol;
1566#ifdef FEAT_MBYTE
1567 if (has_mbyte && new_cursor_col >= 0)
1568 new_cursor_col += (*mb_ptr2len_check)(ptr + new_cursor_col);
1569 else
1570#endif
1571 ++new_cursor_col;
1572 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1573 }
1574 vcol = last_vcol;
1575
1576 /*
1577 * May need to insert spaces to be able to position the cursor on
1578 * the right screen column.
1579 */
1580 if (vcol != (int)curwin->w_virtcol)
1581 {
1582 curwin->w_cursor.col = new_cursor_col;
1583 i = (int)curwin->w_virtcol - vcol;
1584 ptr = alloc(i + 1);
1585 if (ptr != NULL)
1586 {
1587 new_cursor_col += i;
1588 ptr[i] = NUL;
1589 while (--i >= 0)
1590 ptr[i] = ' ';
1591 ins_str(ptr);
1592 vim_free(ptr);
1593 }
1594 }
1595
1596 /*
1597 * When changing the indent while the cursor is in it, reset
1598 * Insstart_col to 0.
1599 */
1600 insstart_less = MAXCOL;
1601 }
1602
1603 curwin->w_p_list = save_p_list;
1604
1605 if (new_cursor_col <= 0)
1606 curwin->w_cursor.col = 0;
1607 else
1608 curwin->w_cursor.col = new_cursor_col;
1609 curwin->w_set_curswant = TRUE;
1610 changed_cline_bef_curs();
1611
1612 /*
1613 * May have to adjust the start of the insert.
1614 */
1615 if (State & INSERT)
1616 {
1617 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1618 {
1619 if ((int)Insstart.col <= insstart_less)
1620 Insstart.col = 0;
1621 else
1622 Insstart.col -= insstart_less;
1623 }
1624 if ((int)ai_col <= insstart_less)
1625 ai_col = 0;
1626 else
1627 ai_col -= insstart_less;
1628 }
1629
1630 /*
1631 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1632 * If the number of characters before the cursor decreased, need to pop a
1633 * few characters from the replace stack.
1634 * If the number of characters before the cursor increased, need to push a
1635 * few NULs onto the replace stack.
1636 */
1637 if (REPLACE_NORMAL(State) && start_col >= 0)
1638 {
1639 while (start_col > (int)curwin->w_cursor.col)
1640 {
1641 replace_join(0); /* remove a NUL from the replace stack */
1642 --start_col;
1643 }
1644 while (start_col < (int)curwin->w_cursor.col || replaced)
1645 {
1646 replace_push(NUL);
1647 if (replaced)
1648 {
1649 replace_push(replaced);
1650 replaced = NUL;
1651 }
1652 ++start_col;
1653 }
1654 }
1655
1656#ifdef FEAT_VREPLACE
1657 /*
1658 * For VREPLACE mode, we also have to fix the replace stack. In this case
1659 * it is always possible because we backspace over the whole line and then
1660 * put it back again the way we wanted it.
1661 */
1662 if (State & VREPLACE_FLAG)
1663 {
1664 /* If orig_line didn't allocate, just return. At least we did the job,
1665 * even if you can't backspace. */
1666 if (orig_line == NULL)
1667 return;
1668
1669 /* Save new line */
1670 new_line = vim_strsave(ml_get_curline());
1671 if (new_line == NULL)
1672 return;
1673
1674 /* We only put back the new line up to the cursor */
1675 new_line[curwin->w_cursor.col] = NUL;
1676
1677 /* Put back original line */
1678 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1679 curwin->w_cursor.col = orig_col;
1680
1681 /* Backspace from cursor to start of line */
1682 backspace_until_column(0);
1683
1684 /* Insert new stuff into line again */
1685 ins_bytes(new_line);
1686
1687 vim_free(new_line);
1688 }
1689#endif
1690}
1691
1692/*
1693 * Truncate the space at the end of a line. This is to be used only in an
1694 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1695 * modes.
1696 */
1697 void
1698truncate_spaces(line)
1699 char_u *line;
1700{
1701 int i;
1702
1703 /* find start of trailing white space */
1704 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1705 {
1706 if (State & REPLACE_FLAG)
1707 replace_join(0); /* remove a NUL from the replace stack */
1708 }
1709 line[i + 1] = NUL;
1710}
1711
1712#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1713 || defined(FEAT_COMMENTS) || defined(PROTO)
1714/*
1715 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1716 * modes correctly. May also be used when not in insert mode at all.
1717 */
1718 void
1719backspace_until_column(col)
1720 int col;
1721{
1722 while ((int)curwin->w_cursor.col > col)
1723 {
1724 curwin->w_cursor.col--;
1725 if (State & REPLACE_FLAG)
1726 replace_do_bs();
1727 else
1728 (void)del_char(FALSE);
1729 }
1730}
1731#endif
1732
1733#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1734/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001735 * CTRL-X pressed in Insert mode.
1736 */
1737 static void
1738ins_ctrl_x()
1739{
1740 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
1741 * CTRL-V works like CTRL-N */
1742 if (ctrl_x_mode != CTRL_X_CMDLINE)
1743 {
1744 /* if the next ^X<> won't ADD nothing, then reset
1745 * compl_cont_status */
1746 if (compl_cont_status & CONT_N_ADDS)
1747 compl_cont_status = (compl_cont_status | CONT_INTRPT);
1748 else
1749 compl_cont_status = 0;
1750 /* We're not sure which CTRL-X mode it will be yet */
1751 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
1752 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
1753 edit_submode_pre = NULL;
1754 showmode();
1755 }
1756}
1757
1758/*
1759 * Return TRUE if the 'dict' or 'tsr' option can be used.
1760 */
1761 static int
1762has_compl_option(dict_opt)
1763 int dict_opt;
1764{
1765 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL)
1766 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
1767 {
1768 ctrl_x_mode = 0;
1769 edit_submode = NULL;
1770 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
1771 : (char_u *)_("'thesaurus' option is empty"),
1772 hl_attr(HLF_E));
1773 if (emsg_silent == 0)
1774 {
1775 vim_beep();
1776 setcursor();
1777 out_flush();
1778 ui_delay(2000L, FALSE);
1779 }
1780 return FALSE;
1781 }
1782 return TRUE;
1783}
1784
1785/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
1787 * This depends on the current mode.
1788 */
1789 int
1790vim_is_ctrl_x_key(c)
1791 int c;
1792{
1793 /* Always allow ^R - let it's results then be checked */
1794 if (c == Ctrl_R)
1795 return TRUE;
1796
1797 switch (ctrl_x_mode)
1798 {
1799 case 0: /* Not in any CTRL-X mode */
1800 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
1801 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001802 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
1804 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
1805 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
1806 || c == Ctrl_Q);
1807 case CTRL_X_SCROLL:
1808 return (c == Ctrl_Y || c == Ctrl_E);
1809 case CTRL_X_WHOLE_LINE:
1810 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
1811 case CTRL_X_FILES:
1812 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
1813 case CTRL_X_DICTIONARY:
1814 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
1815 case CTRL_X_THESAURUS:
1816 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
1817 case CTRL_X_TAGS:
1818 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
1819#ifdef FEAT_FIND_ID
1820 case CTRL_X_PATH_PATTERNS:
1821 return (c == Ctrl_P || c == Ctrl_N);
1822 case CTRL_X_PATH_DEFINES:
1823 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
1824#endif
1825 case CTRL_X_CMDLINE:
1826 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
1827 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001828#ifdef FEAT_COMPL_FUNC
1829 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001830 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001831#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001832 case CTRL_X_OCCULT:
1833 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 }
1835 EMSG(_(e_internal));
1836 return FALSE;
1837}
1838
1839/*
1840 * This is like ins_compl_add(), but if ic and inf are set, then the
1841 * case of the originally typed text is used, and the case of the completed
1842 * text is infered, ie this tries to work out what case you probably wanted
1843 * the rest of the word to be in -- webb
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001844 * TODO: make this work for multi-byte characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 */
1846 int
1847ins_compl_add_infercase(str, len, fname, dir, reuse)
1848 char_u *str;
1849 int len;
1850 char_u *fname;
1851 int dir;
1852 int reuse;
1853{
1854 int has_lower = FALSE;
1855 int was_letter = FALSE;
1856 int idx;
1857
1858 if (p_ic && curbuf->b_p_inf && len < IOSIZE)
1859 {
1860 /* Infer case of completed part -- webb */
1861 /* Use IObuff, str would change text in buffer! */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001862 vim_strncpy(IObuff, str, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863
1864 /* Rule 1: Were any chars converted to lower? */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001865 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001867 if (islower(compl_orig_text[idx]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 {
1869 has_lower = TRUE;
1870 if (isupper(IObuff[idx]))
1871 {
1872 /* Rule 1 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001873 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 IObuff[idx] = TOLOWER_LOC(IObuff[idx]);
1875 break;
1876 }
1877 }
1878 }
1879
1880 /*
1881 * Rule 2: No lower case, 2nd consecutive letter converted to
1882 * upper case.
1883 */
1884 if (!has_lower)
1885 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001886 for (idx = 0; idx < compl_length; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001888 if (was_letter && isupper(compl_orig_text[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 && islower(IObuff[idx]))
1890 {
1891 /* Rule 2 is satisfied */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001892 for (idx = compl_length; idx < len; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 IObuff[idx] = TOUPPER_LOC(IObuff[idx]);
1894 break;
1895 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001896 was_letter = isalpha(compl_orig_text[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 }
1898 }
1899
1900 /* Copy the original case of the part we typed */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001901 STRNCPY(IObuff, compl_orig_text, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902
1903 return ins_compl_add(IObuff, len, fname, dir, reuse);
1904 }
1905 return ins_compl_add(str, len, fname, dir, reuse);
1906}
1907
1908/*
1909 * Add a match to the list of matches.
1910 * If the given string is already in the list of completions, then return
1911 * FAIL, otherwise add it to the list and return OK. If there is an error,
1912 * maybe because alloc returns NULL, then RET_ERROR is returned -- webb.
1913 */
1914 static int
1915ins_compl_add(str, len, fname, dir, reuse)
1916 char_u *str;
1917 int len;
1918 char_u *fname;
1919 int dir;
1920 int reuse;
1921{
1922 struct Completion *match;
1923
1924 ui_breakcheck();
1925 if (got_int)
1926 return RET_ERROR;
1927 if (len < 0)
1928 len = (int)STRLEN(str);
1929
1930 /*
1931 * If the same match is already present, don't add it.
1932 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001933 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001935 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 do
1937 {
1938 if ( !(match->original & ORIGINAL_TEXT)
1939 && STRNCMP(match->str, str, (size_t)len) == 0
1940 && match->str[len] == NUL)
1941 return FAIL;
1942 match = match->next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001943 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 }
1945
1946 /*
1947 * Allocate a new match structure.
1948 * Copy the values to the new match structure.
1949 */
1950 match = (struct Completion *)alloc((unsigned)sizeof(struct Completion));
1951 if (match == NULL)
1952 return RET_ERROR;
1953 match->number = -1;
1954 if (reuse & ORIGINAL_TEXT)
1955 {
1956 match->number = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001957 match->str = compl_orig_text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 }
1959 else if ((match->str = vim_strnsave(str, len)) == NULL)
1960 {
1961 vim_free(match);
1962 return RET_ERROR;
1963 }
1964 /* match-fname is:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001965 * - compl_curr_match->fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
1967 * - NULL otherwise. --Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001968 if (fname && compl_curr_match && compl_curr_match->fname
1969 && STRCMP(fname, compl_curr_match->fname) == 0)
1970 match->fname = compl_curr_match->fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 else if (fname && (match->fname = vim_strsave(fname)) != NULL)
1972 reuse |= FREE_FNAME;
1973 else
1974 match->fname = NULL;
1975 match->original = reuse;
1976
1977 /*
1978 * Link the new match structure in the list of matches.
1979 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001980 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 match->next = match->prev = NULL;
1982 else if (dir == FORWARD)
1983 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001984 match->next = compl_curr_match->next;
1985 match->prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 }
1987 else /* BACKWARD */
1988 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001989 match->next = compl_curr_match;
1990 match->prev = compl_curr_match->prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 }
1992 if (match->next)
1993 match->next->prev = match;
1994 if (match->prev)
1995 match->prev->next = match;
1996 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001997 compl_first_match = match;
1998 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999
2000 return OK;
2001}
2002
2003/*
2004 * Add an array of matches to the list of matches.
2005 * Frees matches[].
2006 */
2007 static void
2008ins_compl_add_matches(num_matches, matches, dir)
2009 int num_matches;
2010 char_u **matches;
2011 int dir;
2012{
2013 int i;
2014 int add_r = OK;
2015 int ldir = dir;
2016
2017 for (i = 0; i < num_matches && add_r != RET_ERROR; i++)
2018 if ((add_r = ins_compl_add(matches[i], -1, NULL, ldir, 0)) == OK)
2019 /* if dir was BACKWARD then honor it just once */
2020 ldir = FORWARD;
2021 FreeWild(num_matches, matches);
2022}
2023
2024/* Make the completion list cyclic.
2025 * Return the number of matches (excluding the original).
2026 */
2027 static int
2028ins_compl_make_cyclic()
2029{
2030 struct Completion *match;
2031 int count = 0;
2032
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002033 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 {
2035 /*
2036 * Find the end of the list.
2037 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002038 match = compl_first_match;
2039 /* there's always an entry for the compl_orig_text, it doesn't count. */
2040 while (match->next != NULL && match->next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 {
2042 match = match->next;
2043 ++count;
2044 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002045 match->next = compl_first_match;
2046 compl_first_match->prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 }
2048 return count;
2049}
2050
2051#define DICT_FIRST (1) /* use just first element in "dict" */
2052#define DICT_EXACT (2) /* "dict" is the exact name of a file */
2053/*
2054 * Add any identifiers that match the given pattern to the list of
2055 * completions.
2056 */
2057 static void
2058ins_compl_dictionaries(dict, pat, dir, flags, thesaurus)
2059 char_u *dict;
2060 char_u *pat;
2061 int dir;
2062 int flags;
2063 int thesaurus;
2064{
2065 char_u *ptr;
2066 char_u *buf;
2067 FILE *fp;
2068 regmatch_T regmatch;
2069 int add_r;
2070 char_u **files;
2071 int count;
2072 int i;
2073 int save_p_scs;
2074
2075 buf = alloc(LSIZE);
2076 /* If 'infercase' is set, don't use 'smartcase' here */
2077 save_p_scs = p_scs;
2078 if (curbuf->b_p_inf)
2079 p_scs = FALSE;
2080 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
2081 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2082 regmatch.rm_ic = ignorecase(pat);
2083 while (buf != NULL && regmatch.regprog != NULL && *dict != NUL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002084 && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 {
2086 /* copy one dictionary file name into buf */
2087 if (flags == DICT_EXACT)
2088 {
2089 count = 1;
2090 files = &dict;
2091 }
2092 else
2093 {
2094 /* Expand wildcards in the dictionary name, but do not allow
2095 * backticks (for security, the 'dict' option may have been set in
2096 * a modeline). */
2097 copy_option_part(&dict, buf, LSIZE, ",");
2098 if (vim_strchr(buf, '`') != NULL
2099 || expand_wildcards(1, &buf, &count, &files,
2100 EW_FILE|EW_SILENT) != OK)
2101 count = 0;
2102 }
2103
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002104 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 {
2106 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2107 if (flags != DICT_EXACT)
2108 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00002109 vim_snprintf((char *)IObuff, IOSIZE,
2110 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2112 }
2113
2114 if (fp != NULL)
2115 {
2116 /*
2117 * Read dictionary file line by line.
2118 * Check each line for a match.
2119 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002120 while (!got_int && !compl_interrupted
2121 && !vim_fgets(buf, LSIZE, fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 {
2123 ptr = buf;
2124 while (vim_regexec(&regmatch, buf, (colnr_T)(ptr - buf)))
2125 {
2126 ptr = regmatch.startp[0];
2127 ptr = find_word_end(ptr);
2128 add_r = ins_compl_add_infercase(regmatch.startp[0],
2129 (int)(ptr - regmatch.startp[0]),
2130 files[i], dir, 0);
2131 if (thesaurus)
2132 {
2133 char_u *wstart;
2134
2135 /*
2136 * Add the other matches on the line
2137 */
2138 while (!got_int)
2139 {
2140 /* Find start of the next word. Skip white
2141 * space and punctuation. */
2142 ptr = find_word_start(ptr);
2143 if (*ptr == NUL || *ptr == NL)
2144 break;
2145 wstart = ptr;
2146
2147 /* Find end of the word and add it. */
2148#ifdef FEAT_MBYTE
2149 if (has_mbyte)
2150 /* Japanese words may have characters in
2151 * different classes, only separate words
2152 * with single-byte non-word characters. */
2153 while (*ptr != NUL)
2154 {
2155 int l = (*mb_ptr2len_check)(ptr);
2156
2157 if (l < 2 && !vim_iswordc(*ptr))
2158 break;
2159 ptr += l;
2160 }
2161 else
2162#endif
2163 ptr = find_word_end(ptr);
2164 add_r = ins_compl_add_infercase(wstart,
2165 (int)(ptr - wstart), files[i], dir, 0);
2166 }
2167 }
2168 if (add_r == OK)
2169 /* if dir was BACKWARD then honor it just once */
2170 dir = FORWARD;
2171 else if (add_r == RET_ERROR)
2172 break;
2173 /* avoid expensive call to vim_regexec() when at end
2174 * of line */
2175 if (*ptr == '\n' || got_int)
2176 break;
2177 }
2178 line_breakcheck();
2179 ins_compl_check_keys();
2180 }
2181 fclose(fp);
2182 }
2183 }
2184 if (flags != DICT_EXACT)
2185 FreeWild(count, files);
2186 if (flags)
2187 break;
2188 }
2189 p_scs = save_p_scs;
2190 vim_free(regmatch.regprog);
2191 vim_free(buf);
2192}
2193
2194/*
2195 * Find the start of the next word.
2196 * Returns a pointer to the first char of the word. Also stops at a NUL.
2197 */
2198 char_u *
2199find_word_start(ptr)
2200 char_u *ptr;
2201{
2202#ifdef FEAT_MBYTE
2203 if (has_mbyte)
2204 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
2205 ptr += (*mb_ptr2len_check)(ptr);
2206 else
2207#endif
2208 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
2209 ++ptr;
2210 return ptr;
2211}
2212
2213/*
2214 * Find the end of the word. Assumes it starts inside a word.
2215 * Returns a pointer to just after the word.
2216 */
2217 char_u *
2218find_word_end(ptr)
2219 char_u *ptr;
2220{
2221#ifdef FEAT_MBYTE
2222 int start_class;
2223
2224 if (has_mbyte)
2225 {
2226 start_class = mb_get_class(ptr);
2227 if (start_class > 1)
2228 while (*ptr != NUL)
2229 {
2230 ptr += (*mb_ptr2len_check)(ptr);
2231 if (mb_get_class(ptr) != start_class)
2232 break;
2233 }
2234 }
2235 else
2236#endif
2237 while (vim_iswordc(*ptr))
2238 ++ptr;
2239 return ptr;
2240}
2241
2242/*
2243 * Free the list of completions
2244 */
2245 static void
2246ins_compl_free()
2247{
2248 struct Completion *match;
2249
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002250 vim_free(compl_pattern);
2251 compl_pattern = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002253 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 return;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002255 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 do
2257 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002258 match = compl_curr_match;
2259 compl_curr_match = compl_curr_match->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 vim_free(match->str);
2261 /* several entries may use the same fname, free it just once. */
2262 if (match->original & FREE_FNAME)
2263 vim_free(match->fname);
2264 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002265 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
2266 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267}
2268
2269 static void
2270ins_compl_clear()
2271{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002272 compl_cont_status = 0;
2273 compl_started = FALSE;
2274 compl_matches = 0;
2275 vim_free(compl_pattern);
2276 compl_pattern = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 save_sm = -1;
2278 edit_submode_extra = NULL;
2279}
2280
2281/*
2282 * Prepare for Insert mode completion, or stop it.
2283 */
2284 static void
2285ins_compl_prep(c)
2286 int c;
2287{
2288 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 int temp;
2290 int want_cindent;
2291
2292 /* Forget any previous 'special' messages if this is actually
2293 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
2294 */
2295 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
2296 edit_submode_extra = NULL;
2297
2298 /* Ignore end of Select mode mapping */
2299 if (c == K_SELECT)
2300 return;
2301
2302 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
2303 {
2304 /*
2305 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
2306 * it will be yet. Now we decide.
2307 */
2308 switch (c)
2309 {
2310 case Ctrl_E:
2311 case Ctrl_Y:
2312 ctrl_x_mode = CTRL_X_SCROLL;
2313 if (!(State & REPLACE_FLAG))
2314 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
2315 else
2316 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
2317 edit_submode_pre = NULL;
2318 showmode();
2319 break;
2320 case Ctrl_L:
2321 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2322 break;
2323 case Ctrl_F:
2324 ctrl_x_mode = CTRL_X_FILES;
2325 break;
2326 case Ctrl_K:
2327 ctrl_x_mode = CTRL_X_DICTIONARY;
2328 break;
2329 case Ctrl_R:
2330 /* Simply allow ^R to happen without affecting ^X mode */
2331 break;
2332 case Ctrl_T:
2333 ctrl_x_mode = CTRL_X_THESAURUS;
2334 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002335#ifdef FEAT_COMPL_FUNC
2336 case Ctrl_U:
2337 ctrl_x_mode = CTRL_X_FUNCTION;
2338 break;
2339#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002340 case Ctrl_O:
2341 ctrl_x_mode = CTRL_X_OCCULT;
2342 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 case Ctrl_RSB:
2344 ctrl_x_mode = CTRL_X_TAGS;
2345 break;
2346#ifdef FEAT_FIND_ID
2347 case Ctrl_I:
2348 case K_S_TAB:
2349 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
2350 break;
2351 case Ctrl_D:
2352 ctrl_x_mode = CTRL_X_PATH_DEFINES;
2353 break;
2354#endif
2355 case Ctrl_V:
2356 case Ctrl_Q:
2357 ctrl_x_mode = CTRL_X_CMDLINE;
2358 break;
2359 case Ctrl_P:
2360 case Ctrl_N:
2361 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
2362 * just started ^X mode, or there were enough ^X's to cancel
2363 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
2364 * do normal expansion when interrupting a different mode (say
2365 * ^X^F^X^P or ^P^X^X^P, see below)
2366 * nothing changes if interrupting mode 0, (eg, the flag
2367 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002368 if (!(compl_cont_status & CONT_INTRPT))
2369 compl_cont_status |= CONT_LOCAL;
2370 else if (compl_cont_mode != 0)
2371 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 /* FALLTHROUGH */
2373 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002374 /* If we have typed at least 2 ^X's... for modes != 0, we set
2375 * compl_cont_status = 0 (eg, as if we had just started ^X
2376 * mode).
2377 * For mode 0, we set "compl_cont_mode" to an impossible
2378 * value, in both cases ^X^X can be used to restart the same
2379 * mode (avoiding ADDING mode).
2380 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2381 * 'complete' and local ^P expansions respectively.
2382 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2383 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 if (c == Ctrl_X)
2385 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002386 if (compl_cont_mode != 0)
2387 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002389 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 }
2391 ctrl_x_mode = 0;
2392 edit_submode = NULL;
2393 showmode();
2394 break;
2395 }
2396 }
2397 else if (ctrl_x_mode != 0)
2398 {
2399 /* We're already in CTRL-X mode, do we stay in it? */
2400 if (!vim_is_ctrl_x_key(c))
2401 {
2402 if (ctrl_x_mode == CTRL_X_SCROLL)
2403 ctrl_x_mode = 0;
2404 else
2405 ctrl_x_mode = CTRL_X_FINISHED;
2406 edit_submode = NULL;
2407 }
2408 showmode();
2409 }
2410
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002411 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 {
2413 /* Show error message from attempted keyword completion (probably
2414 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002415 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 showmode();
2417 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R)
2418 || ctrl_x_mode == CTRL_X_FINISHED)
2419 {
2420 /* Get here when we have finished typing a sequence of ^N and
2421 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002422 * memory that was used, and make sure we can redo the insert. */
2423 if (compl_curr_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002425 char_u *p;
2426
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 /*
2428 * If any of the original typed text has been changed,
2429 * eg when ignorecase is set, we must add back-spaces to
2430 * the redo buffer. We add as few as necessary to delete
2431 * just the part of the original text that has changed.
2432 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002433 ptr = compl_curr_match->str;
2434 p = compl_orig_text;
2435 while (*p && *p == *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002437 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 ++ptr;
2439 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002440 for (temp = 0; p[temp]; ++temp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 AppendCharToRedobuff(K_BS);
2442 AppendToRedobuffLit(ptr);
2443 }
2444
2445#ifdef FEAT_CINDENT
2446 want_cindent = (can_cindent && cindent_on());
2447#endif
2448 /*
2449 * When completing whole lines: fix indent for 'cindent'.
2450 * Otherwise, break line if it's too long.
2451 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002452 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 {
2454#ifdef FEAT_CINDENT
2455 /* re-indent the current line */
2456 if (want_cindent)
2457 {
2458 do_c_expr_indent();
2459 want_cindent = FALSE; /* don't do it again */
2460 }
2461#endif
2462 }
2463 else
2464 {
2465 /* put the cursor on the last char, for 'tw' formatting */
2466 curwin->w_cursor.col--;
2467 if (stop_arrow() == OK)
2468 insertchar(NUL, 0, -1);
2469 curwin->w_cursor.col++;
2470 }
2471
2472 auto_format(FALSE, TRUE);
2473
2474 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002475 compl_started = FALSE;
2476 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 msg_clr_cmdline(); /* necessary for "noshowmode" */
2478 ctrl_x_mode = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002479 if (save_sm >= 0)
2480 p_sm = save_sm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 if (edit_submode != NULL)
2482 {
2483 edit_submode = NULL;
2484 showmode();
2485 }
2486
2487#ifdef FEAT_CINDENT
2488 /*
2489 * Indent now if a key was typed that is in 'cinkeys'.
2490 */
2491 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2492 do_c_expr_indent();
2493#endif
2494 }
2495 }
2496
2497 /* reset continue_* if we left expansion-mode, if we stay they'll be
2498 * (re)set properly in ins_complete() */
2499 if (!vim_is_ctrl_x_key(c))
2500 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002501 compl_cont_status = 0;
2502 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 }
2504}
2505
2506/*
2507 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
2508 * (depending on flag) starting from buf and looking for a non-scanned
2509 * buffer (other than curbuf). curbuf is special, if it is called with
2510 * buf=curbuf then it has to be the first call for a given flag/expansion.
2511 *
2512 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
2513 */
2514 static buf_T *
2515ins_compl_next_buf(buf, flag)
2516 buf_T *buf;
2517 int flag;
2518{
2519#ifdef FEAT_WINDOWS
2520 static win_T *wp;
2521#endif
2522
2523 if (flag == 'w') /* just windows */
2524 {
2525#ifdef FEAT_WINDOWS
2526 if (buf == curbuf) /* first call for this flag/expansion */
2527 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00002528 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 && wp->w_buffer->b_scanned)
2530 ;
2531 buf = wp->w_buffer;
2532#else
2533 buf = curbuf;
2534#endif
2535 }
2536 else
2537 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
2538 * (unlisted buffers)
2539 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00002540 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 && ((flag == 'U'
2542 ? buf->b_p_bl
2543 : (!buf->b_p_bl
2544 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2545 || buf->b_scanned
2546 || (buf->b_ml.ml_mfp == NULL
2547 && ctrl_x_mode == CTRL_X_WHOLE_LINE)))
2548 ;
2549 return buf;
2550}
2551
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002552#ifdef FEAT_COMPL_FUNC
2553static char_u *call_completefunc __ARGS((char_u *line, char_u *base, int col, int preproc));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00002554static int expand_by_function __ARGS((linenr_T lnum, int col, char_u *base, char_u ***matches));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002555
2556/*
2557 * Execute user defined complete function 'completefunc'.
2558 * Return NULL if some error occurs.
2559 */
2560 static char_u *
2561call_completefunc(line, base, col, preproc)
2562 char_u *line;
2563 char_u *base;
2564 int col;
2565 int preproc;
2566{
2567 char_u colbuf[30];
2568 char_u *args[4];
2569
2570 /* Return NULL when 'completefunc' isn't set. */
2571 if (*curbuf->b_p_cfu == NUL)
2572 return NULL;
2573
2574 sprintf((char *)colbuf, "%d", col + (base ? (int)STRLEN(base) : 0));
2575 args[0] = line;
2576 args[1] = base;
2577 args[2] = colbuf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00002578 args[3] = (char_u *)(preproc ? "1" : "0");
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002579 return (char_u *)call_func_retstr(curbuf->b_p_cfu, 4, args, FALSE);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002580}
2581
2582/*
2583 * Execute user defined complete function 'completefunc', and get candidates
2584 * are separeted with "\n". Return value is number of candidates and array
2585 * of candidates as "matches".
2586 */
2587 static int
2588expand_by_function(lnum, col, base, matches)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00002589 linenr_T lnum;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002590 int col;
2591 char_u *base;
2592 char_u ***matches;
2593{
2594 char_u *matchstr = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002595 char_u *line_copy = vim_strsave(ml_get(lnum));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002596
2597 /* Execute 'completefunc' and get the result */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002598 matchstr = call_completefunc(line_copy, base, col, 0);
2599 vim_free(line_copy);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002600
2601 /* Parse returned string */
2602 if (matchstr != NULL)
2603 {
2604 garray_T ga;
2605 char_u *p, *pnext;
2606
2607 ga_init2(&ga, (int)sizeof(char*), 8);
2608 for (p = matchstr; *p != NUL; p = pnext)
2609 {
2610 int len;
2611
2612 pnext = vim_strchr(p, '\n');
2613 if (pnext == NULL)
2614 pnext = p + STRLEN(p);
2615 len = pnext - p;
2616 if (len > 0)
2617 {
2618 if (ga_grow(&ga, 1) == FAIL)
2619 break;
2620 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(p, len);
2621 ++ga.ga_len;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002622 }
2623 if (*pnext != NUL)
2624 ++pnext;
2625 }
2626 vim_free(matchstr);
2627 if (ga.ga_len > 0)
2628 *matches = (char_u**)ga.ga_data;
2629 return ga.ga_len;
2630 }
2631 return 0;
2632}
2633#endif /* FEAT_COMPL_FUNC */
2634
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002635static int expand_occult __ARGS((linenr_T lnum, int col, char_u *base, char_u ***matches));
2636
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002638 * Perform occult completion'
2639 * Return value is number of candidates and array of candidates as "matchp".
2640 */
2641 static int
2642expand_occult(lnum, col, pat, matchp)
2643 linenr_T lnum;
2644 int col;
2645 char_u *pat;
2646 char_u ***matchp;
2647{
2648 int num_matches;
2649
2650 /* Use tag completion for now. */
2651 if (find_tags(pat, &num_matches, matchp,
2652 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
2653 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
2654 TAG_MANY, curbuf->b_ffname) == FAIL)
2655 return 0;
2656 return num_matches;
2657}
2658
2659/*
2660 * Get the next expansion(s), using "compl_pattern".
2661 * The search starts at position "ini" in curbuf and in the direction dir.
2662 * When "compl_started" is FALSE start at that position, otherwise
2663 * continue where we stopped searching before.
2664 * This may return before finding all the matches.
2665 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 */
2667 static int
2668ins_compl_get_exp(ini, dir)
2669 pos_T *ini;
2670 int dir;
2671{
2672 static pos_T first_match_pos;
2673 static pos_T last_match_pos;
2674 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002675 static int found_all = FALSE; /* Found all matches of a
2676 certain type. */
2677 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678
2679 pos_T *pos;
2680 char_u **matches;
2681 int save_p_scs;
2682 int save_p_ws;
2683 int save_p_ic;
2684 int i;
2685 int num_matches;
2686 int len;
2687 int found_new_match;
2688 int type = ctrl_x_mode;
2689 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 char_u *dict = NULL;
2691 int dict_f = 0;
2692 struct Completion *old_match;
2693
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002694 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 {
2696 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
2697 ins_buf->b_scanned = 0;
2698 found_all = FALSE;
2699 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002700 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002701 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 last_match_pos = first_match_pos = *ini;
2703 }
2704
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002705 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 pos = (dir == FORWARD) ? &last_match_pos : &first_match_pos;
2707 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
2708 for (;;)
2709 {
2710 found_new_match = FAIL;
2711
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002712 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 * or if found_all says this entry is done. For ^X^L only use the
2714 * entries from 'complete' that look in loaded buffers. */
2715 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002716 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 {
2718 found_all = FALSE;
2719 while (*e_cpt == ',' || *e_cpt == ' ')
2720 e_cpt++;
2721 if (*e_cpt == '.' && !curbuf->b_scanned)
2722 {
2723 ins_buf = curbuf;
2724 first_match_pos = *ini;
2725 /* So that ^N can match word immediately after cursor */
2726 if (ctrl_x_mode == 0)
2727 dec(&first_match_pos);
2728 last_match_pos = first_match_pos;
2729 type = 0;
2730 }
2731 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
2732 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
2733 {
2734 /* Scan a buffer, but not the current one. */
2735 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
2736 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002737 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 first_match_pos.col = last_match_pos.col = 0;
2739 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
2740 last_match_pos.lnum = 0;
2741 type = 0;
2742 }
2743 else /* unloaded buffer, scan like dictionary */
2744 {
2745 found_all = TRUE;
2746 if (ins_buf->b_fname == NULL)
2747 continue;
2748 type = CTRL_X_DICTIONARY;
2749 dict = ins_buf->b_fname;
2750 dict_f = DICT_EXACT;
2751 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00002752 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 ins_buf->b_fname == NULL
2754 ? buf_spname(ins_buf)
2755 : ins_buf->b_sfname == NULL
2756 ? (char *)ins_buf->b_fname
2757 : (char *)ins_buf->b_sfname);
2758 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2759 }
2760 else if (*e_cpt == NUL)
2761 break;
2762 else
2763 {
2764 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2765 type = -1;
2766 else if (*e_cpt == 'k' || *e_cpt == 's')
2767 {
2768 if (*e_cpt == 'k')
2769 type = CTRL_X_DICTIONARY;
2770 else
2771 type = CTRL_X_THESAURUS;
2772 if (*++e_cpt != ',' && *e_cpt != NUL)
2773 {
2774 dict = e_cpt;
2775 dict_f = DICT_FIRST;
2776 }
2777 }
2778#ifdef FEAT_FIND_ID
2779 else if (*e_cpt == 'i')
2780 type = CTRL_X_PATH_PATTERNS;
2781 else if (*e_cpt == 'd')
2782 type = CTRL_X_PATH_DEFINES;
2783#endif
2784 else if (*e_cpt == ']' || *e_cpt == 't')
2785 {
2786 type = CTRL_X_TAGS;
2787 sprintf((char*)IObuff, _("Scanning tags."));
2788 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2789 }
2790 else
2791 type = -1;
2792
2793 /* in any case e_cpt is advanced to the next entry */
2794 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
2795
2796 found_all = TRUE;
2797 if (type == -1)
2798 continue;
2799 }
2800 }
2801
2802 switch (type)
2803 {
2804 case -1:
2805 break;
2806#ifdef FEAT_FIND_ID
2807 case CTRL_X_PATH_PATTERNS:
2808 case CTRL_X_PATH_DEFINES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002809 find_pattern_in_path(compl_pattern, dir,
2810 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002812 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
2814 (linenr_T)1, (linenr_T)MAXLNUM);
2815 break;
2816#endif
2817
2818 case CTRL_X_DICTIONARY:
2819 case CTRL_X_THESAURUS:
2820 ins_compl_dictionaries(
2821 dict ? dict
2822 : (type == CTRL_X_THESAURUS
2823 ? (*curbuf->b_p_tsr == NUL
2824 ? p_tsr
2825 : curbuf->b_p_tsr)
2826 : (*curbuf->b_p_dict == NUL
2827 ? p_dict
2828 : curbuf->b_p_dict)),
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002829 compl_pattern, dir,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 dict ? dict_f : 0, type == CTRL_X_THESAURUS);
2831 dict = NULL;
2832 break;
2833
2834 case CTRL_X_TAGS:
2835 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
2836 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002837 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838
2839 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002840 * of matches is found when compl_pattern is empty */
2841 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
2843 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
2844 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
2845 {
2846 ins_compl_add_matches(num_matches, matches, dir);
2847 }
2848 p_ic = save_p_ic;
2849 break;
2850
2851 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002852 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
2854 {
2855
2856 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002857 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 ins_compl_add_matches(num_matches, matches, dir);
2859 }
2860 break;
2861
2862 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002863 if (expand_cmdline(&compl_xp, compl_pattern,
2864 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 &num_matches, &matches) == EXPAND_OK)
2866 ins_compl_add_matches(num_matches, matches, dir);
2867 break;
2868
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002869#ifdef FEAT_COMPL_FUNC
2870 case CTRL_X_FUNCTION:
2871 num_matches = expand_by_function(first_match_pos.lnum,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002872 first_match_pos.col, compl_pattern, &matches);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002873 if (num_matches > 0)
2874 ins_compl_add_matches(num_matches, matches, dir);
2875 break;
2876#endif
2877
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002878 case CTRL_X_OCCULT:
2879 num_matches = expand_occult(first_match_pos.lnum,
2880 first_match_pos.col, compl_pattern, &matches);
2881 if (num_matches > 0)
2882 ins_compl_add_matches(num_matches, matches, dir);
2883 break;
2884
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 default: /* normal ^P/^N and ^X^L */
2886 /*
2887 * If 'infercase' is set, don't use 'smartcase' here
2888 */
2889 save_p_scs = p_scs;
2890 if (ins_buf->b_p_inf)
2891 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002892
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 /* buffers other than curbuf are scanned from the beginning or the
2894 * end but never from the middle, thus setting nowrapscan in this
2895 * buffers is a good idea, on the other hand, we always set
2896 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
2897 save_p_ws = p_ws;
2898 if (ins_buf != curbuf)
2899 p_ws = FALSE;
2900 else if (*e_cpt == '.')
2901 p_ws = TRUE;
2902 for (;;)
2903 {
2904 int reuse = 0;
2905
2906 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that has
2907 * added a word that was at the beginning of the line */
2908 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002909 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002911 dir, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 else
2913 found_new_match = searchit(NULL, ins_buf, pos, dir,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002914 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 RE_LAST);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002916 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002918 /* set compl_started even on fail */
2919 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 first_match_pos = *pos;
2921 last_match_pos = *pos;
2922 }
2923 else if (first_match_pos.lnum == last_match_pos.lnum
2924 && first_match_pos.col == last_match_pos.col)
2925 found_new_match = FAIL;
2926 if (found_new_match == FAIL)
2927 {
2928 if (ins_buf == curbuf)
2929 found_all = TRUE;
2930 break;
2931 }
2932
2933 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002934 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 && ini->lnum == pos->lnum
2936 && ini->col == pos->col)
2937 continue;
2938 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
2939 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2940 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002941 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 {
2943 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
2944 continue;
2945 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
2946 if (!p_paste)
2947 ptr = skipwhite(ptr);
2948 }
2949 len = (int)STRLEN(ptr);
2950 }
2951 else
2952 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002953 char_u *tmp_ptr = ptr;
2954
2955 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002957 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 /* Skip if already inside a word. */
2959 if (vim_iswordp(tmp_ptr))
2960 continue;
2961 /* Find start of next word. */
2962 tmp_ptr = find_word_start(tmp_ptr);
2963 }
2964 /* Find end of this word. */
2965 tmp_ptr = find_word_end(tmp_ptr);
2966 len = (int)(tmp_ptr - ptr);
2967
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002968 if ((compl_cont_status & CONT_ADDING)
2969 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 {
2971 if (pos->lnum < ins_buf->b_ml.ml_line_count)
2972 {
2973 /* Try next line, if any. the new word will be
2974 * "join" as if the normal command "J" was used.
2975 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002976 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 * works -- Acevedo */
2978 STRNCPY(IObuff, ptr, len);
2979 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
2980 tmp_ptr = ptr = skipwhite(ptr);
2981 /* Find start of next word. */
2982 tmp_ptr = find_word_start(tmp_ptr);
2983 /* Find end of next word. */
2984 tmp_ptr = find_word_end(tmp_ptr);
2985 if (tmp_ptr > ptr)
2986 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002987 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002989 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 IObuff[len++] = ' ';
2991 /* IObuf =~ "\k.* ", thus len >= 2 */
2992 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002993 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 || (vim_strchr(p_cpo, CPO_JOINSP)
2995 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002996 && (IObuff[len - 2] == '?'
2997 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 IObuff[len++] = ' ';
2999 }
3000 /* copy as much as posible of the new word */
3001 if (tmp_ptr - ptr >= IOSIZE - len)
3002 tmp_ptr = ptr + IOSIZE - len - 1;
3003 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3004 len += (int)(tmp_ptr - ptr);
3005 reuse |= CONT_S_IPOS;
3006 }
3007 IObuff[len] = NUL;
3008 ptr = IObuff;
3009 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003010 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 continue;
3012 }
3013 }
3014 if (ins_compl_add_infercase(ptr, len,
3015 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
3016 dir, reuse) != FAIL)
3017 {
3018 found_new_match = OK;
3019 break;
3020 }
3021 }
3022 p_scs = save_p_scs;
3023 p_ws = save_p_ws;
3024 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003025 /* check if compl_curr_match has changed, (e.g. other type of
3026 * expansion added somenthing) */
3027 if (compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 found_new_match = OK;
3029
3030 /* break the loop for specialized modes (use 'complete' just for the
3031 * generic ctrl_x_mode == 0) or when we've found a new match */
3032 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003033 || found_new_match != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 break;
3035
3036 /* Mark a buffer scanned when it has been scanned completely */
3037 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
3038 ins_buf->b_scanned = TRUE;
3039
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003040 compl_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003042 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043
3044 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
3045 && *e_cpt == NUL) /* Got to end of 'complete' */
3046 found_new_match = FAIL;
3047
3048 i = -1; /* total of matches, unknown */
3049 if (found_new_match == FAIL
3050 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
3051 i = ins_compl_make_cyclic();
3052
3053 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003054 * just been made cyclic then we have to move compl_curr_match to the next
3055 * or previous entry (if any) -- Acevedo */
3056 compl_curr_match = dir == FORWARD ? old_match->next : old_match->prev;
3057 if (compl_curr_match == NULL)
3058 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 return i;
3060}
3061
3062/* Delete the old text being completed. */
3063 static void
3064ins_compl_delete()
3065{
3066 int i;
3067
3068 /*
3069 * In insert mode: Delete the typed part.
3070 * In replace mode: Put the old characters back, if any.
3071 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003072 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 backspace_until_column(i);
3074 changed_cline_bef_curs();
3075}
3076
3077/* Insert the new text being completed. */
3078 static void
3079ins_compl_insert()
3080{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003081 ins_bytes(compl_shown_match->str + curwin->w_cursor.col - compl_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082}
3083
3084/*
3085 * Fill in the next completion in the current direction.
3086 * If allow_get_expansion is TRUE, then we may call ins_compl_get_exp() to get
3087 * more completions. If it is FALSE, then we just do nothing when there are
3088 * no more completions in a given direction. The latter case is used when we
3089 * are still in the middle of finding completions, to allow browsing through
3090 * the ones found so far.
3091 * Return the total number of matches, or -1 if still unknown -- webb.
3092 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003093 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
3094 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 *
3096 * Note that this function may be called recursively once only. First with
3097 * allow_get_expansion TRUE, which calls ins_compl_get_exp(), which in turn
3098 * calls this function with allow_get_expansion FALSE.
3099 */
3100 static int
3101ins_compl_next(allow_get_expansion)
3102 int allow_get_expansion;
3103{
3104 int num_matches = -1;
3105 int i;
3106
3107 if (allow_get_expansion)
3108 {
3109 /* Delete old text to be replaced */
3110 ins_compl_delete();
3111 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003112 compl_pending = FALSE;
3113 if (compl_shows_dir == FORWARD && compl_shown_match->next != NULL)
3114 compl_shown_match = compl_shown_match->next;
3115 else if (compl_shows_dir == BACKWARD && compl_shown_match->prev != NULL)
3116 compl_shown_match = compl_shown_match->prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 else
3118 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003119 compl_pending = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 if (allow_get_expansion)
3121 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003122 num_matches = ins_compl_get_exp(&compl_startpos,
3123 compl_direction);
3124 if (compl_pending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003126 if (compl_direction == compl_shows_dir)
3127 compl_shown_match = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 }
3129 }
3130 else
3131 return -1;
3132 }
3133
3134 /* Insert the text of the new completion */
3135 ins_compl_insert();
3136
3137 if (!allow_get_expansion)
3138 {
3139 /* Display the current match. */
3140 update_screen(0);
3141
3142 /* Delete old text to be replaced, since we're still searching and
3143 * don't want to match ourselves! */
3144 ins_compl_delete();
3145 }
3146
3147 /*
3148 * Show the file name for the match (if any)
3149 * Truncate the file name to avoid a wait for return.
3150 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003151 if (compl_shown_match->fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 {
3153 STRCPY(IObuff, "match in file ");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003154 i = (vim_strsize(compl_shown_match->fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 if (i <= 0)
3156 i = 0;
3157 else
3158 STRCAT(IObuff, "<");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003159 STRCAT(IObuff, compl_shown_match->fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 msg(IObuff);
3161 redraw_cmdline = FALSE; /* don't overwrite! */
3162 }
3163
3164 return num_matches;
3165}
3166
3167/*
3168 * Call this while finding completions, to check whether the user has hit a key
3169 * that should change the currently displayed completion, or exit completion
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003170 * mode. Also, when compl_pending is TRUE, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 * possible. -- webb
3172 */
3173 void
3174ins_compl_check_keys()
3175{
3176 static int count = 0;
3177
3178 int c;
3179
3180 /* Don't check when reading keys from a script. That would break the test
3181 * scripts */
3182 if (using_script())
3183 return;
3184
3185 /* Only do this at regular intervals */
3186 if (++count < CHECK_KEYS_TIME)
3187 return;
3188 count = 0;
3189
3190 ++no_mapping;
3191 c = vpeekc_any();
3192 --no_mapping;
3193 if (c != NUL)
3194 {
3195 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
3196 {
3197 c = safe_vgetc(); /* Eat the character */
3198 if (c == Ctrl_P || c == Ctrl_L)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003199 compl_shows_dir = BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003201 compl_shows_dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 (void)ins_compl_next(FALSE);
3203 }
3204 else if (c != Ctrl_R)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003205 compl_interrupted = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003207 if (compl_pending && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 (void)ins_compl_next(FALSE);
3209}
3210
3211/*
3212 * Do Insert mode completion.
3213 * Called when character "c" was typed, which has a meaning for completion.
3214 * Returns OK if completion was done, FAIL if something failed (out of mem).
3215 */
3216 static int
3217ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003218 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003220 char_u *line;
3221 int startcol = 0; /* column where searched text starts */
3222 colnr_T curs_col; /* cursor column */
3223 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224
3225 if (c == Ctrl_P || c == Ctrl_L)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003226 compl_direction = BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003228 compl_direction = FORWARD;
3229 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 {
3231 /* First time we hit ^N or ^P (in a row, I mean) */
3232
3233 /* Turn off 'sm' so we don't show matches with ^X^L */
3234 save_sm = p_sm;
3235 p_sm = FALSE;
3236
3237 did_ai = FALSE;
3238#ifdef FEAT_SMARTINDENT
3239 did_si = FALSE;
3240 can_si = FALSE;
3241 can_si_back = FALSE;
3242#endif
3243 if (stop_arrow() == FAIL)
3244 return FAIL;
3245
3246 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003247 curs_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248
3249 /* if this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003250 * "compl_startpos" to the cursor as a pattern to add a new word
3251 * instead of expand the one before the cursor, in word-wise if
3252 * "compl_startpos"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 * is not in the same line as the cursor then fix it (the line has
3254 * been split because it was longer than 'tw'). if SOL is set then
3255 * skip the previous pattern, a word at the beginning of the line has
3256 * been inserted, we'll look for that -- Acevedo. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003257 if ((compl_cont_status & CONT_INTRPT) && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 {
3259 /*
3260 * it is a continued search
3261 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003262 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
3264 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3265 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003266 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003268 /* line (probably) wrapped, set compl_startpos to the
3269 * first non_blank in the line, if it is not a wordchar
3270 * include it to get a better pattern, but then we don't
3271 * want the "\\<" prefix, check it bellow */
3272 compl_col = (colnr_T)(skipwhite(line) - line);
3273 compl_startpos.col = compl_col;
3274 compl_startpos.lnum = curwin->w_cursor.lnum;
3275 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 }
3277 else
3278 {
3279 /* S_IPOS was set when we inserted a word that was at the
3280 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003281 * mode but first we need to redefine compl_startpos */
3282 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003284 compl_cont_status |= CONT_SOL;
3285 compl_startpos.col = (colnr_T)(skipwhite(
3286 line + compl_length
3287 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003289 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003291 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 /* IObuf is used to add a "word from the next line" would we
3293 * have enough space? just being paranoic */
3294#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003295 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003297 compl_cont_status &= ~CONT_SOL;
3298 compl_length = (IOSIZE - MIN_SPACE);
3299 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003301 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
3302 if (compl_length < 1)
3303 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 }
3305 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003306 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003308 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 }
3310 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003311 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003313 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003315 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003317 compl_cont_status = 0;
3318 compl_cont_status |= CONT_N_ADDS;
3319 compl_startpos = curwin->w_cursor;
3320 startcol = (int)curs_col;
3321 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 }
3323
3324 /* Work out completion pattern and original text -- webb */
3325 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
3326 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003327 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
3329 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003330 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003332 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003334 compl_col += ++startcol;
3335 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 }
3337 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003338 compl_pattern = str_foldcase(line + compl_col,
3339 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003341 compl_pattern = vim_strnsave(line + compl_col,
3342 compl_length);
3343 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 return FAIL;
3345 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003346 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 {
3348 char_u *prefix = (char_u *)"\\<";
3349
3350 /* we need 3 extra chars, 1 for the NUL and
3351 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003352 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
3353 compl_length) + 3);
3354 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003356 if (!vim_iswordp(line + compl_col)
3357 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 && (
3359#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003360 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003362 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363#endif
3364 )))
3365 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003366 STRCPY((char *)compl_pattern, prefix);
3367 (void)quote_meta(compl_pattern + STRLEN(prefix),
3368 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003370 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003372 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003374 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375#endif
3376 )
3377 {
3378 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003379 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
3380 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003382 compl_col += curs_col;
3383 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 }
3385 else
3386 {
3387#ifdef FEAT_MBYTE
3388 /* Search the point of change class of multibyte character
3389 * or not a word single byte character backward. */
3390 if (has_mbyte)
3391 {
3392 int base_class;
3393 int head_off;
3394
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003395 startcol -= (*mb_head_off)(line, line + startcol);
3396 base_class = mb_get_class(line + startcol);
3397 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003399 head_off = (*mb_head_off)(line, line + startcol);
3400 if (base_class != mb_get_class(line + startcol
3401 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003403 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 }
3405 }
3406 else
3407#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003408 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003410 compl_col += ++startcol;
3411 compl_length = (int)curs_col - startcol;
3412 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 {
3414 /* Only match word with at least two chars -- webb
3415 * there's no need to call quote_meta,
3416 * alloc(7) is enough -- Acevedo
3417 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003418 compl_pattern = alloc(7);
3419 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003421 STRCPY((char *)compl_pattern, "\\<");
3422 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
3423 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 }
3425 else
3426 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003427 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
3428 compl_length) + 3);
3429 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003431 STRCPY((char *)compl_pattern, "\\<");
3432 (void)quote_meta(compl_pattern + 2, line + compl_col,
3433 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 }
3435 }
3436 }
3437 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3438 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003439 compl_col = skipwhite(line) - line;
3440 compl_length = (int)curs_col - (int)compl_col;
3441 if (compl_length < 0) /* cursor in indent: empty pattern */
3442 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003444 compl_pattern = str_foldcase(line + compl_col, compl_length,
3445 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003447 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3448 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 return FAIL;
3450 }
3451 else if (ctrl_x_mode == CTRL_X_FILES)
3452 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003453 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003455 compl_col += ++startcol;
3456 compl_length = (int)curs_col - startcol;
3457 compl_pattern = addstar(line + compl_col, compl_length,
3458 EXPAND_FILES);
3459 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 return FAIL;
3461 }
3462 else if (ctrl_x_mode == CTRL_X_CMDLINE)
3463 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003464 compl_pattern = vim_strnsave(line, curs_col);
3465 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003467 set_cmd_context(&compl_xp, compl_pattern,
3468 (int)STRLEN(compl_pattern), curs_col);
3469 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
3470 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003472 startcol = (int)(compl_xp.xp_pattern - compl_pattern);
3473 compl_col = startcol;
3474 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003476#ifdef FEAT_COMPL_FUNC
3477 else if (ctrl_x_mode == CTRL_X_FUNCTION)
3478 {
3479 /*
3480 * Call user defined function 'completefunc' with line content,
3481 * cursor column number and preproc is 1. Obtain length of text
3482 * to use for completion.
3483 */
3484 char_u *lenstr;
3485 int keeplen = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003486 char_u *line_copy = vim_strsave(line);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003487
3488 /* Call 'completefunc' and get pattern length as a string */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003489 lenstr = call_completefunc(line_copy, NULL, curs_col, 1);
3490 vim_free(line_copy);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003491 if (lenstr == NULL)
3492 return FAIL;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00003493 keeplen = atoi((char *)lenstr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003494 vim_free(lenstr);
3495 if (keeplen < 0)
3496 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003497 if ((colnr_T)keeplen > curs_col)
3498 keeplen = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003499
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003500 /* Setup variables for completion. Need to obtain "line" again,
3501 * it may have become invalid. */
3502 line = ml_get(curwin->w_cursor.lnum);
3503 compl_col = keeplen;
3504 compl_length = curs_col - keeplen;
3505 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3506 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003507 return FAIL;
3508 }
3509#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003510 else if (ctrl_x_mode == CTRL_X_OCCULT)
3511 {
3512 /* TODO: let language-specific function handle locating the text
3513 * to be completed or use 'coupler' option. */
3514 while (--startcol >= 0 && vim_isIDc(line[startcol]))
3515 ;
3516 compl_col += ++startcol;
3517 compl_length = (int)curs_col - startcol;
3518 compl_pattern = vim_strnsave(line + compl_col, compl_length);
3519 if (compl_pattern == NULL)
3520 return FAIL;
3521 }
3522 else
3523 {
3524 EMSG2(_(e_intern2), "ins_complete()");
3525 return FAIL;
3526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003528 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 {
3530 edit_submode_pre = (char_u *)_(" Adding");
3531 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3532 {
3533 /* Insert a new line, keep indentation but ignore 'comments' */
3534#ifdef FEAT_COMMENTS
3535 char_u *old = curbuf->b_p_com;
3536
3537 curbuf->b_p_com = (char_u *)"";
3538#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003539 compl_startpos.lnum = curwin->w_cursor.lnum;
3540 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 ins_eol('\r');
3542#ifdef FEAT_COMMENTS
3543 curbuf->b_p_com = old;
3544#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003545 compl_length = 0;
3546 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 }
3548 }
3549 else
3550 {
3551 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003552 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 }
3554
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003555 if (compl_cont_status & CONT_LOCAL)
3556 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 else
3558 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
3559
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 /* Always add completion for the original text. Note that
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003561 * "compl_orig_text" itself (not a copy) is added, it will be freed
3562 * when the list of matches is freed. */
3563 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
3564 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
3565 -1, NULL, 0, ORIGINAL_TEXT) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003567 vim_free(compl_pattern);
3568 compl_pattern = NULL;
3569 vim_free(compl_orig_text);
3570 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 return FAIL;
3572 }
3573
3574 /* showmode might reset the internal line pointers, so it must
3575 * be called before line = ml_get(), or when this address is no
3576 * longer needed. -- Acevedo.
3577 */
3578 edit_submode_extra = (char_u *)_("-- Searching...");
3579 edit_submode_highl = HLF_COUNT;
3580 showmode();
3581 edit_submode_extra = NULL;
3582 out_flush();
3583 }
3584
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003585 compl_shown_match = compl_curr_match;
3586 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587
3588 /*
3589 * Find next match.
3590 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003591 n = ins_compl_next(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003593 if (n > 1) /* all matches have been found */
3594 compl_matches = n;
3595 compl_curr_match = compl_shown_match;
3596 compl_direction = compl_shows_dir;
3597 compl_interrupted = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598
3599 /* eat the ESC to avoid leaving insert mode */
3600 if (got_int && !global_busy)
3601 {
3602 (void)vgetc();
3603 got_int = FALSE;
3604 }
3605
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003606 /* we found no match if the list has only the "compl_orig_text"-entry */
3607 if (compl_first_match == compl_first_match->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003609 edit_submode_extra = (compl_cont_status & CONT_ADDING)
3610 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
3612 edit_submode_highl = HLF_E;
3613 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
3614 * because we couldn't expand anything at first place, but if we used
3615 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
3616 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003617 if ( compl_length > 1
3618 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 || (ctrl_x_mode != 0
3620 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
3621 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003622 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 }
3624
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003625 if (compl_curr_match->original & CONT_S_IPOS)
3626 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003628 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629
3630 if (edit_submode_extra == NULL)
3631 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003632 if (compl_curr_match->original & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 {
3634 edit_submode_extra = (char_u *)_("Back at original");
3635 edit_submode_highl = HLF_W;
3636 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003637 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 {
3639 edit_submode_extra = (char_u *)_("Word from other line");
3640 edit_submode_highl = HLF_COUNT;
3641 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003642 else if (compl_curr_match->next == compl_curr_match->prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 {
3644 edit_submode_extra = (char_u *)_("The only match");
3645 edit_submode_highl = HLF_COUNT;
3646 }
3647 else
3648 {
3649 /* Update completion sequence number when needed. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003650 if (compl_curr_match->number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 {
3652 int number = 0;
3653 struct Completion *match;
3654
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003655 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 {
3657 /* search backwards for the first valid (!= -1) number.
3658 * This should normally succeed already at the first loop
3659 * cycle, so it's fast! */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003660 for (match = compl_curr_match->prev; match != NULL
3661 && match != compl_first_match; match = match->prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 if (match->number != -1)
3663 {
3664 number = match->number;
3665 break;
3666 }
3667 if (match != NULL)
3668 /* go up and assign all numbers which are not assigned
3669 * yet */
3670 for (match = match->next; match
3671 && match->number == -1; match = match->next)
3672 match->number = ++number;
3673 }
3674 else /* BACKWARD */
3675 {
3676 /* search forwards (upwards) for the first valid (!= -1)
3677 * number. This should normally succeed already at the
3678 * first loop cycle, so it's fast! */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003679 for (match = compl_curr_match->next; match != NULL
3680 && match != compl_first_match; match = match->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 if (match->number != -1)
3682 {
3683 number = match->number;
3684 break;
3685 }
3686 if (match != NULL)
3687 /* go down and assign all numbers which are not
3688 * assigned yet */
3689 for (match = match->prev; match
3690 && match->number == -1; match = match->prev)
3691 match->number = ++number;
3692 }
3693 }
3694
3695 /* The match should always have a sequnce number now, this is just
3696 * a safety check. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003697 if (compl_curr_match->number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 {
3699 /* Space for 10 text chars. + 2x10-digit no.s */
3700 static char_u match_ref[31];
3701
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003702 if (compl_matches > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 sprintf((char *)IObuff, _("match %d of %d"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003704 compl_curr_match->number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003706 sprintf((char *)IObuff, _("match %d"),
3707 compl_curr_match->number);
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003708 vim_strncpy(match_ref, IObuff, 30);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 edit_submode_extra = match_ref;
3710 edit_submode_highl = HLF_R;
3711 if (dollar_vcol)
3712 curs_columns(FALSE);
3713 }
3714 }
3715 }
3716
3717 /* Show a message about what (completion) mode we're in. */
3718 showmode();
3719 if (edit_submode_extra != NULL)
3720 {
3721 if (!p_smd)
3722 msg_attr(edit_submode_extra,
3723 edit_submode_highl < HLF_COUNT
3724 ? hl_attr(edit_submode_highl) : 0);
3725 }
3726 else
3727 msg_clr_cmdline(); /* necessary for "noshowmode" */
3728
3729 return OK;
3730}
3731
3732/*
3733 * Looks in the first "len" chars. of "src" for search-metachars.
3734 * If dest is not NULL the chars. are copied there quoting (with
3735 * a backslash) the metachars, and dest would be NUL terminated.
3736 * Returns the length (needed) of dest
3737 */
3738 static int
3739quote_meta(dest, src, len)
3740 char_u *dest;
3741 char_u *src;
3742 int len;
3743{
3744 int m;
3745
3746 for (m = len; --len >= 0; src++)
3747 {
3748 switch (*src)
3749 {
3750 case '.':
3751 case '*':
3752 case '[':
3753 if (ctrl_x_mode == CTRL_X_DICTIONARY
3754 || ctrl_x_mode == CTRL_X_THESAURUS)
3755 break;
3756 case '~':
3757 if (!p_magic) /* quote these only if magic is set */
3758 break;
3759 case '\\':
3760 if (ctrl_x_mode == CTRL_X_DICTIONARY
3761 || ctrl_x_mode == CTRL_X_THESAURUS)
3762 break;
3763 case '^': /* currently it's not needed. */
3764 case '$':
3765 m++;
3766 if (dest != NULL)
3767 *dest++ = '\\';
3768 break;
3769 }
3770 if (dest != NULL)
3771 *dest++ = *src;
3772#ifdef FEAT_MBYTE
3773 /* Copy remaining bytes of a multibyte character. */
3774 if (has_mbyte)
3775 {
3776 int i, mb_len;
3777
3778 mb_len = (*mb_ptr2len_check)(src) - 1;
3779 if (mb_len > 0 && len >= mb_len)
3780 for (i = 0; i < mb_len; ++i)
3781 {
3782 --len;
3783 ++src;
3784 if (dest != NULL)
3785 *dest++ = *src;
3786 }
3787 }
3788#endif
3789 }
3790 if (dest != NULL)
3791 *dest = NUL;
3792
3793 return m;
3794}
3795#endif /* FEAT_INS_EXPAND */
3796
3797/*
3798 * Next character is interpreted literally.
3799 * A one, two or three digit decimal number is interpreted as its byte value.
3800 * If one or two digits are entered, the next character is given to vungetc().
3801 * For Unicode a character > 255 may be returned.
3802 */
3803 int
3804get_literal()
3805{
3806 int cc;
3807 int nc;
3808 int i;
3809 int hex = FALSE;
3810 int octal = FALSE;
3811#ifdef FEAT_MBYTE
3812 int unicode = 0;
3813#endif
3814
3815 if (got_int)
3816 return Ctrl_C;
3817
3818#ifdef FEAT_GUI
3819 /*
3820 * In GUI there is no point inserting the internal code for a special key.
3821 * It is more useful to insert the string "<KEY>" instead. This would
3822 * probably be useful in a text window too, but it would not be
3823 * vi-compatible (maybe there should be an option for it?) -- webb
3824 */
3825 if (gui.in_use)
3826 ++allow_keys;
3827#endif
3828#ifdef USE_ON_FLY_SCROLL
3829 dont_scroll = TRUE; /* disallow scrolling here */
3830#endif
3831 ++no_mapping; /* don't map the next key hits */
3832 cc = 0;
3833 i = 0;
3834 for (;;)
3835 {
3836 do
3837 nc = safe_vgetc();
3838 while (nc == K_IGNORE || nc == K_VER_SCROLLBAR
3839 || nc == K_HOR_SCROLLBAR);
3840#ifdef FEAT_CMDL_INFO
3841 if (!(State & CMDLINE)
3842# ifdef FEAT_MBYTE
3843 && MB_BYTE2LEN_CHECK(nc) == 1
3844# endif
3845 )
3846 add_to_showcmd(nc);
3847#endif
3848 if (nc == 'x' || nc == 'X')
3849 hex = TRUE;
3850 else if (nc == 'o' || nc == 'O')
3851 octal = TRUE;
3852#ifdef FEAT_MBYTE
3853 else if (nc == 'u' || nc == 'U')
3854 unicode = nc;
3855#endif
3856 else
3857 {
3858 if (hex
3859#ifdef FEAT_MBYTE
3860 || unicode != 0
3861#endif
3862 )
3863 {
3864 if (!vim_isxdigit(nc))
3865 break;
3866 cc = cc * 16 + hex2nr(nc);
3867 }
3868 else if (octal)
3869 {
3870 if (nc < '0' || nc > '7')
3871 break;
3872 cc = cc * 8 + nc - '0';
3873 }
3874 else
3875 {
3876 if (!VIM_ISDIGIT(nc))
3877 break;
3878 cc = cc * 10 + nc - '0';
3879 }
3880
3881 ++i;
3882 }
3883
3884 if (cc > 255
3885#ifdef FEAT_MBYTE
3886 && unicode == 0
3887#endif
3888 )
3889 cc = 255; /* limit range to 0-255 */
3890 nc = 0;
3891
3892 if (hex) /* hex: up to two chars */
3893 {
3894 if (i >= 2)
3895 break;
3896 }
3897#ifdef FEAT_MBYTE
3898 else if (unicode) /* Unicode: up to four or eight chars */
3899 {
3900 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
3901 break;
3902 }
3903#endif
3904 else if (i >= 3) /* decimal or octal: up to three chars */
3905 break;
3906 }
3907 if (i == 0) /* no number entered */
3908 {
3909 if (nc == K_ZERO) /* NUL is stored as NL */
3910 {
3911 cc = '\n';
3912 nc = 0;
3913 }
3914 else
3915 {
3916 cc = nc;
3917 nc = 0;
3918 }
3919 }
3920
3921 if (cc == 0) /* NUL is stored as NL */
3922 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00003923#ifdef FEAT_MBYTE
3924 if (enc_dbcs && (cc & 0xff) == 0)
3925 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
3926 second byte will cause trouble! */
3927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928
3929 --no_mapping;
3930#ifdef FEAT_GUI
3931 if (gui.in_use)
3932 --allow_keys;
3933#endif
3934 if (nc)
3935 vungetc(nc);
3936 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
3937 return cc;
3938}
3939
3940/*
3941 * Insert character, taking care of special keys and mod_mask
3942 */
3943 static void
3944insert_special(c, allow_modmask, ctrlv)
3945 int c;
3946 int allow_modmask;
3947 int ctrlv; /* c was typed after CTRL-V */
3948{
3949 char_u *p;
3950 int len;
3951
3952 /*
3953 * Special function key, translate into "<Key>". Up to the last '>' is
3954 * inserted with ins_str(), so as not to replace characters in replace
3955 * mode.
3956 * Only use mod_mask for special keys, to avoid things like <S-Space>,
3957 * unless 'allow_modmask' is TRUE.
3958 */
3959#ifdef MACOS
3960 /* Command-key never produces a normal key */
3961 if (mod_mask & MOD_MASK_CMD)
3962 allow_modmask = TRUE;
3963#endif
3964 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
3965 {
3966 p = get_special_key_name(c, mod_mask);
3967 len = (int)STRLEN(p);
3968 c = p[len - 1];
3969 if (len > 2)
3970 {
3971 if (stop_arrow() == FAIL)
3972 return;
3973 p[len - 1] = NUL;
3974 ins_str(p);
3975 AppendToRedobuffLit(p);
3976 ctrlv = FALSE;
3977 }
3978 }
3979 if (stop_arrow() == OK)
3980 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
3981}
3982
3983/*
3984 * Special characters in this context are those that need processing other
3985 * than the simple insertion that can be performed here. This includes ESC
3986 * which terminates the insert, and CR/NL which need special processing to
3987 * open up a new line. This routine tries to optimize insertions performed by
3988 * the "redo", "undo" or "put" commands, so it needs to know when it should
3989 * stop and defer processing to the "normal" mechanism.
3990 * '0' and '^' are special, because they can be followed by CTRL-D.
3991 */
3992#ifdef EBCDIC
3993# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
3994#else
3995# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
3996#endif
3997
3998#ifdef FEAT_MBYTE
3999# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
4000#else
4001# define WHITECHAR(cc) vim_iswhite(cc)
4002#endif
4003
4004 void
4005insertchar(c, flags, second_indent)
4006 int c; /* character to insert or NUL */
4007 int flags; /* INSCHAR_FORMAT, etc. */
4008 int second_indent; /* indent for second line if >= 0 */
4009{
4010 int haveto_redraw = FALSE;
4011 int textwidth;
4012#ifdef FEAT_COMMENTS
4013 colnr_T leader_len;
4014 char_u *p;
4015 int no_leader = FALSE;
4016 int do_comments = (flags & INSCHAR_DO_COM);
4017#endif
4018 int fo_white_par;
4019 int first_line = TRUE;
4020 int fo_ins_blank;
4021#ifdef FEAT_MBYTE
4022 int fo_multibyte;
4023#endif
4024 int save_char = NUL;
4025 int cc;
4026
4027 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
4028 fo_ins_blank = has_format_option(FO_INS_BLANK);
4029#ifdef FEAT_MBYTE
4030 fo_multibyte = has_format_option(FO_MBYTE_BREAK);
4031#endif
4032 fo_white_par = has_format_option(FO_WHITE_PAR);
4033
4034 /*
4035 * Try to break the line in two or more pieces when:
4036 * - Always do this if we have been called to do formatting only.
4037 * - Always do this when 'formatoptions' has the 'a' flag and the line
4038 * ends in white space.
4039 * - Otherwise:
4040 * - Don't do this if inserting a blank
4041 * - Don't do this if an existing character is being replaced, unless
4042 * we're in VREPLACE mode.
4043 * - Do this if the cursor is not on the line where insert started
4044 * or - 'formatoptions' doesn't have 'l' or the line was not too long
4045 * before the insert.
4046 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
4047 * before 'textwidth'
4048 */
4049 if (textwidth
4050 && ((flags & INSCHAR_FORMAT)
4051 || (!vim_iswhite(c)
4052 && !((State & REPLACE_FLAG)
4053#ifdef FEAT_VREPLACE
4054 && !(State & VREPLACE_FLAG)
4055#endif
4056 && *ml_get_cursor() != NUL)
4057 && (curwin->w_cursor.lnum != Insstart.lnum
4058 || ((!has_format_option(FO_INS_LONG)
4059 || Insstart_textlen <= (colnr_T)textwidth)
4060 && (!fo_ins_blank
4061 || Insstart_blank_vcol <= (colnr_T)textwidth
4062 ))))))
4063 {
4064 /*
4065 * When 'ai' is off we don't want a space under the cursor to be
4066 * deleted. Replace it with an 'x' temporarily.
4067 */
4068 if (!curbuf->b_p_ai)
4069 {
4070 cc = gchar_cursor();
4071 if (vim_iswhite(cc))
4072 {
4073 save_char = cc;
4074 pchar_cursor('x');
4075 }
4076 }
4077
4078 /*
4079 * Repeat breaking lines, until the current line is not too long.
4080 */
4081 while (!got_int)
4082 {
4083 int startcol; /* Cursor column at entry */
4084 int wantcol; /* column at textwidth border */
4085 int foundcol; /* column for start of spaces */
4086 int end_foundcol = 0; /* column for start of word */
4087 colnr_T len;
4088 colnr_T virtcol;
4089#ifdef FEAT_VREPLACE
4090 int orig_col = 0;
4091 char_u *saved_text = NULL;
4092#endif
4093 colnr_T col;
4094
4095 virtcol = get_nolist_virtcol();
4096 if (virtcol < (colnr_T)textwidth)
4097 break;
4098
4099#ifdef FEAT_COMMENTS
4100 if (no_leader)
4101 do_comments = FALSE;
4102 else if (!(flags & INSCHAR_FORMAT)
4103 && has_format_option(FO_WRAP_COMS))
4104 do_comments = TRUE;
4105
4106 /* Don't break until after the comment leader */
4107 if (do_comments)
4108 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
4109 else
4110 leader_len = 0;
4111
4112 /* If the line doesn't start with a comment leader, then don't
4113 * start one in a following broken line. Avoids that a %word
4114 * moved to the start of the next line causes all following lines
4115 * to start with %. */
4116 if (leader_len == 0)
4117 no_leader = TRUE;
4118#endif
4119 if (!(flags & INSCHAR_FORMAT)
4120#ifdef FEAT_COMMENTS
4121 && leader_len == 0
4122#endif
4123 && !has_format_option(FO_WRAP))
4124
4125 {
4126 textwidth = 0;
4127 break;
4128 }
4129 if ((startcol = curwin->w_cursor.col) == 0)
4130 break;
4131
4132 /* find column of textwidth border */
4133 coladvance((colnr_T)textwidth);
4134 wantcol = curwin->w_cursor.col;
4135
4136 curwin->w_cursor.col = startcol - 1;
4137#ifdef FEAT_MBYTE
4138 /* Correct cursor for multi-byte character. */
4139 if (has_mbyte)
4140 mb_adjust_cursor();
4141#endif
4142 foundcol = 0;
4143
4144 /*
4145 * Find position to break at.
4146 * Stop at first entered white when 'formatoptions' has 'v'
4147 */
4148 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
4149 || curwin->w_cursor.lnum != Insstart.lnum
4150 || curwin->w_cursor.col >= Insstart.col)
4151 {
4152 cc = gchar_cursor();
4153 if (WHITECHAR(cc))
4154 {
4155 /* remember position of blank just before text */
4156 end_foundcol = curwin->w_cursor.col;
4157
4158 /* find start of sequence of blanks */
4159 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
4160 {
4161 dec_cursor();
4162 cc = gchar_cursor();
4163 }
4164 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
4165 break; /* only spaces in front of text */
4166#ifdef FEAT_COMMENTS
4167 /* Don't break until after the comment leader */
4168 if (curwin->w_cursor.col < leader_len)
4169 break;
4170#endif
4171 if (has_format_option(FO_ONE_LETTER))
4172 {
4173 /* do not break after one-letter words */
4174 if (curwin->w_cursor.col == 0)
4175 break; /* one-letter word at begin */
4176
4177 col = curwin->w_cursor.col;
4178 dec_cursor();
4179 cc = gchar_cursor();
4180
4181 if (WHITECHAR(cc))
4182 continue; /* one-letter, continue */
4183 curwin->w_cursor.col = col;
4184 }
4185#ifdef FEAT_MBYTE
4186 if (has_mbyte)
4187 foundcol = curwin->w_cursor.col
4188 + (*mb_ptr2len_check)(ml_get_cursor());
4189 else
4190#endif
4191 foundcol = curwin->w_cursor.col + 1;
4192 if (curwin->w_cursor.col < (colnr_T)wantcol)
4193 break;
4194 }
4195#ifdef FEAT_MBYTE
4196 else if (cc >= 0x100 && fo_multibyte
4197 && curwin->w_cursor.col <= (colnr_T)wantcol)
4198 {
4199 /* Break after or before a multi-byte character. */
4200 foundcol = curwin->w_cursor.col;
4201 if (curwin->w_cursor.col < (colnr_T)wantcol)
4202 foundcol += (*mb_char2len)(cc);
4203 end_foundcol = foundcol;
4204 break;
4205 }
4206#endif
4207 if (curwin->w_cursor.col == 0)
4208 break;
4209 dec_cursor();
4210 }
4211
4212 if (foundcol == 0) /* no spaces, cannot break line */
4213 {
4214 curwin->w_cursor.col = startcol;
4215 break;
4216 }
4217
4218 /* Going to break the line, remove any "$" now. */
4219 undisplay_dollar();
4220
4221 /*
4222 * Offset between cursor position and line break is used by replace
4223 * stack functions. VREPLACE does not use this, and backspaces
4224 * over the text instead.
4225 */
4226#ifdef FEAT_VREPLACE
4227 if (State & VREPLACE_FLAG)
4228 orig_col = startcol; /* Will start backspacing from here */
4229 else
4230#endif
4231 replace_offset = startcol - end_foundcol - 1;
4232
4233 /*
4234 * adjust startcol for spaces that will be deleted and
4235 * characters that will remain on top line
4236 */
4237 curwin->w_cursor.col = foundcol;
4238 while (cc = gchar_cursor(), WHITECHAR(cc))
4239 inc_cursor();
4240 startcol -= curwin->w_cursor.col;
4241 if (startcol < 0)
4242 startcol = 0;
4243
4244#ifdef FEAT_VREPLACE
4245 if (State & VREPLACE_FLAG)
4246 {
4247 /*
4248 * In VREPLACE mode, we will backspace over the text to be
4249 * wrapped, so save a copy now to put on the next line.
4250 */
4251 saved_text = vim_strsave(ml_get_cursor());
4252 curwin->w_cursor.col = orig_col;
4253 if (saved_text == NULL)
4254 break; /* Can't do it, out of memory */
4255 saved_text[startcol] = NUL;
4256
4257 /* Backspace over characters that will move to the next line */
4258 if (!fo_white_par)
4259 backspace_until_column(foundcol);
4260 }
4261 else
4262#endif
4263 {
4264 /* put cursor after pos. to break line */
4265 if (!fo_white_par)
4266 curwin->w_cursor.col = foundcol;
4267 }
4268
4269 /*
4270 * Split the line just before the margin.
4271 * Only insert/delete lines, but don't really redraw the window.
4272 */
4273 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
4274 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
4275#ifdef FEAT_COMMENTS
4276 + (do_comments ? OPENLINE_DO_COM : 0)
4277#endif
4278 , old_indent);
4279 old_indent = 0;
4280
4281 replace_offset = 0;
4282 if (first_line)
4283 {
4284 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
4285 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
4286 if (second_indent >= 0)
4287 {
4288#ifdef FEAT_VREPLACE
4289 if (State & VREPLACE_FLAG)
4290 change_indent(INDENT_SET, second_indent, FALSE, NUL);
4291 else
4292#endif
4293 (void)set_indent(second_indent, SIN_CHANGED);
4294 }
4295 first_line = FALSE;
4296 }
4297
4298#ifdef FEAT_VREPLACE
4299 if (State & VREPLACE_FLAG)
4300 {
4301 /*
4302 * In VREPLACE mode we have backspaced over the text to be
4303 * moved, now we re-insert it into the new line.
4304 */
4305 ins_bytes(saved_text);
4306 vim_free(saved_text);
4307 }
4308 else
4309#endif
4310 {
4311 /*
4312 * Check if cursor is not past the NUL off the line, cindent
4313 * may have added or removed indent.
4314 */
4315 curwin->w_cursor.col += startcol;
4316 len = (colnr_T)STRLEN(ml_get_curline());
4317 if (curwin->w_cursor.col > len)
4318 curwin->w_cursor.col = len;
4319 }
4320
4321 haveto_redraw = TRUE;
4322#ifdef FEAT_CINDENT
4323 can_cindent = TRUE;
4324#endif
4325 /* moved the cursor, don't autoindent or cindent now */
4326 did_ai = FALSE;
4327#ifdef FEAT_SMARTINDENT
4328 did_si = FALSE;
4329 can_si = FALSE;
4330 can_si_back = FALSE;
4331#endif
4332 line_breakcheck();
4333 }
4334
4335 if (save_char) /* put back space after cursor */
4336 pchar_cursor(save_char);
4337
4338 if (c == NUL) /* formatting only */
4339 return;
4340 if (haveto_redraw)
4341 {
4342 update_topline();
4343 redraw_curbuf_later(VALID);
4344 }
4345 }
4346 if (c == NUL) /* only formatting was wanted */
4347 return;
4348
4349#ifdef FEAT_COMMENTS
4350 /* Check whether this character should end a comment. */
4351 if (did_ai && (int)c == end_comment_pending)
4352 {
4353 char_u *line;
4354 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
4355 int middle_len, end_len;
4356 int i;
4357
4358 /*
4359 * Need to remove existing (middle) comment leader and insert end
4360 * comment leader. First, check what comment leader we can find.
4361 */
4362 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
4363 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
4364 {
4365 /* Skip middle-comment string */
4366 while (*p && p[-1] != ':') /* find end of middle flags */
4367 ++p;
4368 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
4369 /* Don't count trailing white space for middle_len */
4370 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
4371 --middle_len;
4372
4373 /* Find the end-comment string */
4374 while (*p && p[-1] != ':') /* find end of end flags */
4375 ++p;
4376 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
4377
4378 /* Skip white space before the cursor */
4379 i = curwin->w_cursor.col;
4380 while (--i >= 0 && vim_iswhite(line[i]))
4381 ;
4382 i++;
4383
4384 /* Skip to before the middle leader */
4385 i -= middle_len;
4386
4387 /* Check some expected things before we go on */
4388 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
4389 {
4390 /* Backspace over all the stuff we want to replace */
4391 backspace_until_column(i);
4392
4393 /*
4394 * Insert the end-comment string, except for the last
4395 * character, which will get inserted as normal later.
4396 */
4397 ins_bytes_len(lead_end, end_len - 1);
4398 }
4399 }
4400 }
4401 end_comment_pending = NUL;
4402#endif
4403
4404 did_ai = FALSE;
4405#ifdef FEAT_SMARTINDENT
4406 did_si = FALSE;
4407 can_si = FALSE;
4408 can_si_back = FALSE;
4409#endif
4410
4411 /*
4412 * If there's any pending input, grab up to INPUT_BUFLEN at once.
4413 * This speeds up normal text input considerably.
4414 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
4415 * need to re-indent at a ':', or any other character (but not what
4416 * 'paste' is set)..
4417 */
4418#ifdef USE_ON_FLY_SCROLL
4419 dont_scroll = FALSE; /* allow scrolling here */
4420#endif
4421
4422 if ( !ISSPECIAL(c)
4423#ifdef FEAT_MBYTE
4424 && (!has_mbyte || (*mb_char2len)(c) == 1)
4425#endif
4426 && vpeekc() != NUL
4427 && !(State & REPLACE_FLAG)
4428#ifdef FEAT_CINDENT
4429 && !cindent_on()
4430#endif
4431#ifdef FEAT_RIGHTLEFT
4432 && !p_ri
4433#endif
4434 )
4435 {
4436#define INPUT_BUFLEN 100
4437 char_u buf[INPUT_BUFLEN + 1];
4438 int i;
4439 colnr_T virtcol = 0;
4440
4441 buf[0] = c;
4442 i = 1;
4443 if (textwidth)
4444 virtcol = get_nolist_virtcol();
4445 /*
4446 * Stop the string when:
4447 * - no more chars available
4448 * - finding a special character (command key)
4449 * - buffer is full
4450 * - running into the 'textwidth' boundary
4451 * - need to check for abbreviation: A non-word char after a word-char
4452 */
4453 while ( (c = vpeekc()) != NUL
4454 && !ISSPECIAL(c)
4455#ifdef FEAT_MBYTE
4456 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
4457#endif
4458 && i < INPUT_BUFLEN
4459 && (textwidth == 0
4460 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
4461 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
4462 {
4463#ifdef FEAT_RIGHTLEFT
4464 c = vgetc();
4465 if (p_hkmap && KeyTyped)
4466 c = hkmap(c); /* Hebrew mode mapping */
4467# ifdef FEAT_FKMAP
4468 if (p_fkmap && KeyTyped)
4469 c = fkmap(c); /* Farsi mode mapping */
4470# endif
4471 buf[i++] = c;
4472#else
4473 buf[i++] = vgetc();
4474#endif
4475 }
4476
4477#ifdef FEAT_DIGRAPHS
4478 do_digraph(-1); /* clear digraphs */
4479 do_digraph(buf[i-1]); /* may be the start of a digraph */
4480#endif
4481 buf[i] = NUL;
4482 ins_str(buf);
4483 if (flags & INSCHAR_CTRLV)
4484 {
4485 redo_literal(*buf);
4486 i = 1;
4487 }
4488 else
4489 i = 0;
4490 if (buf[i] != NUL)
4491 AppendToRedobuffLit(buf + i);
4492 }
4493 else
4494 {
4495#ifdef FEAT_MBYTE
4496 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
4497 {
4498 char_u buf[MB_MAXBYTES + 1];
4499
4500 (*mb_char2bytes)(c, buf);
4501 buf[cc] = NUL;
4502 ins_char_bytes(buf, cc);
4503 AppendCharToRedobuff(c);
4504 }
4505 else
4506#endif
4507 {
4508 ins_char(c);
4509 if (flags & INSCHAR_CTRLV)
4510 redo_literal(c);
4511 else
4512 AppendCharToRedobuff(c);
4513 }
4514 }
4515}
4516
4517/*
4518 * Called after inserting or deleting text: When 'formatoptions' includes the
4519 * 'a' flag format from the current line until the end of the paragraph.
4520 * Keep the cursor at the same position relative to the text.
4521 * The caller must have saved the cursor line for undo, following ones will be
4522 * saved here.
4523 */
4524 void
4525auto_format(trailblank, prev_line)
4526 int trailblank; /* when TRUE also format with trailing blank */
4527 int prev_line; /* may start in previous line */
4528{
4529 pos_T pos;
4530 colnr_T len;
4531 char_u *old;
4532 char_u *new, *pnew;
4533 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004534 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535
4536 if (!has_format_option(FO_AUTO))
4537 return;
4538
4539 pos = curwin->w_cursor;
4540 old = ml_get_curline();
4541
4542 /* may remove added space */
4543 check_auto_format(FALSE);
4544
4545 /* Don't format in Insert mode when the cursor is on a trailing blank, the
4546 * user might insert normal text next. Also skip formatting when "1" is
4547 * in 'formatoptions' and there is a single character before the cursor.
4548 * Otherwise the line would be broken and when typing another non-white
4549 * next they are not joined back together. */
4550 wasatend = (pos.col == STRLEN(old));
4551 if (*old != NUL && !trailblank && wasatend)
4552 {
4553 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004554 cc = gchar_cursor();
4555 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
4556 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004558 cc = gchar_cursor();
4559 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 {
4561 curwin->w_cursor = pos;
4562 return;
4563 }
4564 curwin->w_cursor = pos;
4565 }
4566
4567#ifdef FEAT_COMMENTS
4568 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
4569 * comments. */
4570 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
4571 && get_leader_len(old, NULL, FALSE) == 0)
4572 return;
4573#endif
4574
4575 /*
4576 * May start formatting in a previous line, so that after "x" a word is
4577 * moved to the previous line if it fits there now. Only when this is not
4578 * the start of a paragraph.
4579 */
4580 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
4581 {
4582 --curwin->w_cursor.lnum;
4583 if (u_save_cursor() == FAIL)
4584 return;
4585 }
4586
4587 /*
4588 * Do the formatting and restore the cursor position. "saved_cursor" will
4589 * be adjusted for the text formatting.
4590 */
4591 saved_cursor = pos;
4592 format_lines((linenr_T)-1);
4593 curwin->w_cursor = saved_cursor;
4594 saved_cursor.lnum = 0;
4595
4596 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4597 {
4598 /* "cannot happen" */
4599 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4600 coladvance((colnr_T)MAXCOL);
4601 }
4602 else
4603 check_cursor_col();
4604
4605 /* Insert mode: If the cursor is now after the end of the line while it
4606 * previously wasn't, the line was broken. Because of the rule above we
4607 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
4608 * formatted. */
4609 if (!wasatend && has_format_option(FO_WHITE_PAR))
4610 {
4611 new = ml_get_curline();
4612 len = STRLEN(new);
4613 if (curwin->w_cursor.col == len)
4614 {
4615 pnew = vim_strnsave(new, len + 2);
4616 pnew[len] = ' ';
4617 pnew[len + 1] = NUL;
4618 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
4619 /* remove the space later */
4620 did_add_space = TRUE;
4621 }
4622 else
4623 /* may remove added space */
4624 check_auto_format(FALSE);
4625 }
4626
4627 check_cursor();
4628}
4629
4630/*
4631 * When an extra space was added to continue a paragraph for auto-formatting,
4632 * delete it now. The space must be under the cursor, just after the insert
4633 * position.
4634 */
4635 static void
4636check_auto_format(end_insert)
4637 int end_insert; /* TRUE when ending Insert mode */
4638{
4639 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004640 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641
4642 if (did_add_space)
4643 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004644 cc = gchar_cursor();
4645 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 /* Somehow the space was removed already. */
4647 did_add_space = FALSE;
4648 else
4649 {
4650 if (!end_insert)
4651 {
4652 inc_cursor();
4653 c = gchar_cursor();
4654 dec_cursor();
4655 }
4656 if (c != NUL)
4657 {
4658 /* The space is no longer at the end of the line, delete it. */
4659 del_char(FALSE);
4660 did_add_space = FALSE;
4661 }
4662 }
4663 }
4664}
4665
4666/*
4667 * Find out textwidth to be used for formatting:
4668 * if 'textwidth' option is set, use it
4669 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
4670 * if invalid value, use 0.
4671 * Set default to window width (maximum 79) for "gq" operator.
4672 */
4673 int
4674comp_textwidth(ff)
4675 int ff; /* force formatting (for "Q" command) */
4676{
4677 int textwidth;
4678
4679 textwidth = curbuf->b_p_tw;
4680 if (textwidth == 0 && curbuf->b_p_wm)
4681 {
4682 /* The width is the window width minus 'wrapmargin' minus all the
4683 * things that add to the margin. */
4684 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
4685#ifdef FEAT_CMDWIN
4686 if (cmdwin_type != 0)
4687 textwidth -= 1;
4688#endif
4689#ifdef FEAT_FOLDING
4690 textwidth -= curwin->w_p_fdc;
4691#endif
4692#ifdef FEAT_SIGNS
4693 if (curwin->w_buffer->b_signlist != NULL
4694# ifdef FEAT_NETBEANS_INTG
4695 || usingNetbeans
4696# endif
4697 )
4698 textwidth -= 1;
4699#endif
4700 if (curwin->w_p_nu)
4701 textwidth -= 8;
4702 }
4703 if (textwidth < 0)
4704 textwidth = 0;
4705 if (ff && textwidth == 0)
4706 {
4707 textwidth = W_WIDTH(curwin) - 1;
4708 if (textwidth > 79)
4709 textwidth = 79;
4710 }
4711 return textwidth;
4712}
4713
4714/*
4715 * Put a character in the redo buffer, for when just after a CTRL-V.
4716 */
4717 static void
4718redo_literal(c)
4719 int c;
4720{
4721 char_u buf[10];
4722
4723 /* Only digits need special treatment. Translate them into a string of
4724 * three digits. */
4725 if (VIM_ISDIGIT(c))
4726 {
4727 sprintf((char *)buf, "%03d", c);
4728 AppendToRedobuff(buf);
4729 }
4730 else
4731 AppendCharToRedobuff(c);
4732}
4733
4734/*
4735 * start_arrow() is called when an arrow key is used in insert mode.
4736 * It resembles hitting the <ESC> key.
4737 */
4738 static void
4739start_arrow(end_insert_pos)
4740 pos_T *end_insert_pos;
4741{
4742 if (!arrow_used) /* something has been inserted */
4743 {
4744 AppendToRedobuff(ESC_STR);
4745 stop_insert(end_insert_pos, FALSE);
4746 arrow_used = TRUE; /* this means we stopped the current insert */
4747 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004748#ifdef FEAT_SYN_HL
4749 check_spell_redraw();
4750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751}
4752
Bram Moolenaar217ad922005-03-20 22:37:15 +00004753#ifdef FEAT_SYN_HL
4754/*
4755 * If we skipped highlighting word at cursor, do it now.
4756 * It may be skipped again, thus reset spell_redraw_lnum first.
4757 */
4758 static void
4759check_spell_redraw()
4760{
4761 if (spell_redraw_lnum != 0)
4762 {
4763 linenr_T lnum = spell_redraw_lnum;
4764
4765 spell_redraw_lnum = 0;
4766 redrawWinline(lnum, FALSE);
4767 }
4768}
4769#endif
4770
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771/*
4772 * stop_arrow() is called before a change is made in insert mode.
4773 * If an arrow key has been used, start a new insertion.
4774 * Returns FAIL if undo is impossible, shouldn't insert then.
4775 */
4776 int
4777stop_arrow()
4778{
4779 if (arrow_used)
4780 {
4781 if (u_save_cursor() == OK)
4782 {
4783 arrow_used = FALSE;
4784 ins_need_undo = FALSE;
4785 }
4786 Insstart = curwin->w_cursor; /* new insertion starts here */
4787 Insstart_textlen = linetabsize(ml_get_curline());
4788 ai_col = 0;
4789#ifdef FEAT_VREPLACE
4790 if (State & VREPLACE_FLAG)
4791 {
4792 orig_line_count = curbuf->b_ml.ml_line_count;
4793 vr_lines_changed = 1;
4794 }
4795#endif
4796 ResetRedobuff();
4797 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
4798 }
4799 else if (ins_need_undo)
4800 {
4801 if (u_save_cursor() == OK)
4802 ins_need_undo = FALSE;
4803 }
4804
4805#ifdef FEAT_FOLDING
4806 /* Always open fold at the cursor line when inserting something. */
4807 foldOpenCursor();
4808#endif
4809
4810 return (arrow_used || ins_need_undo ? FAIL : OK);
4811}
4812
4813/*
4814 * do a few things to stop inserting
4815 */
4816 static void
4817stop_insert(end_insert_pos, esc)
4818 pos_T *end_insert_pos; /* where insert ended */
4819 int esc; /* called by ins_esc() */
4820{
4821 int cc;
4822
4823 stop_redo_ins();
4824 replace_flush(); /* abandon replace stack */
4825
4826 /*
4827 * save the inserted text for later redo with ^@
4828 */
4829 vim_free(last_insert);
4830 last_insert = get_inserted();
4831 last_insert_skip = new_insert_skip;
4832
4833 if (!arrow_used)
4834 {
4835 /* Auto-format now. It may seem strange to do this when stopping an
4836 * insertion (or moving the cursor), but it's required when appending
4837 * a line and having it end in a space. But only do it when something
4838 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004839 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004841 pos_T tpos = curwin->w_cursor;
4842
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 /* When the cursor is at the end of the line after a space the
4844 * formatting will move it to the following word. Avoid that by
4845 * moving the cursor onto the space. */
4846 cc = 'x';
4847 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
4848 {
4849 dec_cursor();
4850 cc = gchar_cursor();
4851 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004852 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 }
4854
4855 auto_format(TRUE, FALSE);
4856
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004857 if (vim_iswhite(cc))
4858 {
4859 if (gchar_cursor() != NUL)
4860 inc_cursor();
4861#ifdef FEAT_VIRTUALEDIT
4862 /* If the cursor is still at the same character, also keep
4863 * the "coladd". */
4864 if (gchar_cursor() == NUL
4865 && curwin->w_cursor.lnum == tpos.lnum
4866 && curwin->w_cursor.col == tpos.col)
4867 curwin->w_cursor.coladd = tpos.coladd;
4868#endif
4869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 }
4871
4872 /* If a space was inserted for auto-formatting, remove it now. */
4873 check_auto_format(TRUE);
4874
4875 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004876 * of the line, and put the cursor back.
4877 * Do this when ESC was used or moving the cursor up/down. */
4878 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
4879 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004881 pos_T tpos = curwin->w_cursor;
4882
4883 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
4885 --curwin->w_cursor.col;
4886 while (cc = gchar_cursor(), vim_iswhite(cc))
4887 (void)del_char(TRUE);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004888 if (curwin->w_cursor.lnum != tpos.lnum)
4889 curwin->w_cursor = tpos;
4890 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 ++curwin->w_cursor.col; /* put cursor back on the NUL */
4892
4893#ifdef FEAT_VISUAL
4894 /* <C-S-Right> may have started Visual mode, adjust the position for
4895 * deleted characters. */
4896 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
4897 {
4898 cc = STRLEN(ml_get_curline());
4899 if (VIsual.col > (colnr_T)cc)
4900 {
4901 VIsual.col = cc;
4902# ifdef FEAT_VIRTUALEDIT
4903 VIsual.coladd = 0;
4904# endif
4905 }
4906 }
4907#endif
4908 }
4909 }
4910 did_ai = FALSE;
4911#ifdef FEAT_SMARTINDENT
4912 did_si = FALSE;
4913 can_si = FALSE;
4914 can_si_back = FALSE;
4915#endif
4916
4917 /* set '[ and '] to the inserted text */
4918 curbuf->b_op_start = Insstart;
4919 curbuf->b_op_end = *end_insert_pos;
4920}
4921
4922/*
4923 * Set the last inserted text to a single character.
4924 * Used for the replace command.
4925 */
4926 void
4927set_last_insert(c)
4928 int c;
4929{
4930 char_u *s;
4931
4932 vim_free(last_insert);
4933#ifdef FEAT_MBYTE
4934 last_insert = alloc(MB_MAXBYTES * 3 + 5);
4935#else
4936 last_insert = alloc(6);
4937#endif
4938 if (last_insert != NULL)
4939 {
4940 s = last_insert;
4941 /* Use the CTRL-V only when entering a special char */
4942 if (c < ' ' || c == DEL)
4943 *s++ = Ctrl_V;
4944 s = add_char2buf(c, s);
4945 *s++ = ESC;
4946 *s++ = NUL;
4947 last_insert_skip = 0;
4948 }
4949}
4950
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004951#if defined(EXITFREE) || defined(PROTO)
4952 void
4953free_last_insert()
4954{
4955 vim_free(last_insert);
4956 last_insert = NULL;
4957}
4958#endif
4959
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960/*
4961 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
4962 * and CSI. Handle multi-byte characters.
4963 * Returns a pointer to after the added bytes.
4964 */
4965 char_u *
4966add_char2buf(c, s)
4967 int c;
4968 char_u *s;
4969{
4970#ifdef FEAT_MBYTE
4971 char_u temp[MB_MAXBYTES];
4972 int i;
4973 int len;
4974
4975 len = (*mb_char2bytes)(c, temp);
4976 for (i = 0; i < len; ++i)
4977 {
4978 c = temp[i];
4979#endif
4980 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
4981 if (c == K_SPECIAL)
4982 {
4983 *s++ = K_SPECIAL;
4984 *s++ = KS_SPECIAL;
4985 *s++ = KE_FILLER;
4986 }
4987#ifdef FEAT_GUI
4988 else if (c == CSI)
4989 {
4990 *s++ = CSI;
4991 *s++ = KS_EXTRA;
4992 *s++ = (int)KE_CSI;
4993 }
4994#endif
4995 else
4996 *s++ = c;
4997#ifdef FEAT_MBYTE
4998 }
4999#endif
5000 return s;
5001}
5002
5003/*
5004 * move cursor to start of line
5005 * if flags & BL_WHITE move to first non-white
5006 * if flags & BL_SOL move to first non-white if startofline is set,
5007 * otherwise keep "curswant" column
5008 * if flags & BL_FIX don't leave the cursor on a NUL.
5009 */
5010 void
5011beginline(flags)
5012 int flags;
5013{
5014 if ((flags & BL_SOL) && !p_sol)
5015 coladvance(curwin->w_curswant);
5016 else
5017 {
5018 curwin->w_cursor.col = 0;
5019#ifdef FEAT_VIRTUALEDIT
5020 curwin->w_cursor.coladd = 0;
5021#endif
5022
5023 if (flags & (BL_WHITE | BL_SOL))
5024 {
5025 char_u *ptr;
5026
5027 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
5028 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
5029 ++curwin->w_cursor.col;
5030 }
5031 curwin->w_set_curswant = TRUE;
5032 }
5033}
5034
5035/*
5036 * oneright oneleft cursor_down cursor_up
5037 *
5038 * Move one char {right,left,down,up}.
5039 * Doesn't move onto the NUL past the end of the line.
5040 * Return OK when successful, FAIL when we hit a line of file boundary.
5041 */
5042
5043 int
5044oneright()
5045{
5046 char_u *ptr;
5047#ifdef FEAT_MBYTE
5048 int l;
5049#endif
5050
5051#ifdef FEAT_VIRTUALEDIT
5052 if (virtual_active())
5053 {
5054 pos_T prevpos = curwin->w_cursor;
5055
5056 /* Adjust for multi-wide char (excluding TAB) */
5057 ptr = ml_get_cursor();
5058 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
5059#ifdef FEAT_MBYTE
5060 (*mb_ptr2char)(ptr)
5061#else
5062 *ptr
5063#endif
5064 ))
5065 ? ptr2cells(ptr) : 1));
5066 curwin->w_set_curswant = TRUE;
5067 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
5068 return (prevpos.col != curwin->w_cursor.col
5069 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
5070 }
5071#endif
5072
5073 ptr = ml_get_cursor();
5074#ifdef FEAT_MBYTE
5075 if (has_mbyte && (l = (*mb_ptr2len_check)(ptr)) > 1)
5076 {
5077 /* The character under the cursor is a multi-byte character, move
5078 * several bytes right, but don't end up on the NUL. */
5079 if (ptr[l] == NUL)
5080 return FAIL;
5081 curwin->w_cursor.col += l;
5082 }
5083 else
5084#endif
5085 {
5086 if (*ptr++ == NUL || *ptr == NUL)
5087 return FAIL;
5088 ++curwin->w_cursor.col;
5089 }
5090
5091 curwin->w_set_curswant = TRUE;
5092 return OK;
5093}
5094
5095 int
5096oneleft()
5097{
5098#ifdef FEAT_VIRTUALEDIT
5099 if (virtual_active())
5100 {
5101 int width;
5102 int v = getviscol();
5103
5104 if (v == 0)
5105 return FAIL;
5106
5107# ifdef FEAT_LINEBREAK
5108 /* We might get stuck on 'showbreak', skip over it. */
5109 width = 1;
5110 for (;;)
5111 {
5112 coladvance(v - width);
5113 /* getviscol() is slow, skip it when 'showbreak' is empty and
5114 * there are no multi-byte characters */
5115 if ((*p_sbr == NUL
5116# ifdef FEAT_MBYTE
5117 && !has_mbyte
5118# endif
5119 ) || getviscol() < v)
5120 break;
5121 ++width;
5122 }
5123# else
5124 coladvance(v - 1);
5125# endif
5126
5127 if (curwin->w_cursor.coladd == 1)
5128 {
5129 char_u *ptr;
5130
5131 /* Adjust for multi-wide char (not a TAB) */
5132 ptr = ml_get_cursor();
5133 if (*ptr != TAB && vim_isprintc(
5134# ifdef FEAT_MBYTE
5135 (*mb_ptr2char)(ptr)
5136# else
5137 *ptr
5138# endif
5139 ) && ptr2cells(ptr) > 1)
5140 curwin->w_cursor.coladd = 0;
5141 }
5142
5143 curwin->w_set_curswant = TRUE;
5144 return OK;
5145 }
5146#endif
5147
5148 if (curwin->w_cursor.col == 0)
5149 return FAIL;
5150
5151 curwin->w_set_curswant = TRUE;
5152 --curwin->w_cursor.col;
5153
5154#ifdef FEAT_MBYTE
5155 /* if the character on the left of the current cursor is a multi-byte
5156 * character, move to its first byte */
5157 if (has_mbyte)
5158 mb_adjust_cursor();
5159#endif
5160 return OK;
5161}
5162
5163 int
5164cursor_up(n, upd_topline)
5165 long n;
5166 int upd_topline; /* When TRUE: update topline */
5167{
5168 linenr_T lnum;
5169
5170 if (n > 0)
5171 {
5172 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00005173 /* This fails if the cursor is already in the first line or the count
5174 * is larger than the line number and '-' is in 'cpoptions' */
5175 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 return FAIL;
5177 if (n >= lnum)
5178 lnum = 1;
5179 else
5180#ifdef FEAT_FOLDING
5181 if (hasAnyFolding(curwin))
5182 {
5183 /*
5184 * Count each sequence of folded lines as one logical line.
5185 */
5186 /* go to the the start of the current fold */
5187 (void)hasFolding(lnum, &lnum, NULL);
5188
5189 while (n--)
5190 {
5191 /* move up one line */
5192 --lnum;
5193 if (lnum <= 1)
5194 break;
5195 /* If we entered a fold, move to the beginning, unless in
5196 * Insert mode or when 'foldopen' contains "all": it will open
5197 * in a moment. */
5198 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
5199 (void)hasFolding(lnum, &lnum, NULL);
5200 }
5201 if (lnum < 1)
5202 lnum = 1;
5203 }
5204 else
5205#endif
5206 lnum -= n;
5207 curwin->w_cursor.lnum = lnum;
5208 }
5209
5210 /* try to advance to the column we want to be at */
5211 coladvance(curwin->w_curswant);
5212
5213 if (upd_topline)
5214 update_topline(); /* make sure curwin->w_topline is valid */
5215
5216 return OK;
5217}
5218
5219/*
5220 * Cursor down a number of logical lines.
5221 */
5222 int
5223cursor_down(n, upd_topline)
5224 long n;
5225 int upd_topline; /* When TRUE: update topline */
5226{
5227 linenr_T lnum;
5228
5229 if (n > 0)
5230 {
5231 lnum = curwin->w_cursor.lnum;
5232#ifdef FEAT_FOLDING
5233 /* Move to last line of fold, will fail if it's the end-of-file. */
5234 (void)hasFolding(lnum, NULL, &lnum);
5235#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00005236 /* This fails if the cursor is already in the last line or would move
5237 * beyound the last line and '-' is in 'cpoptions' */
5238 if (lnum >= curbuf->b_ml.ml_line_count
5239 || (lnum + n > curbuf->b_ml.ml_line_count
5240 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 return FAIL;
5242 if (lnum + n >= curbuf->b_ml.ml_line_count)
5243 lnum = curbuf->b_ml.ml_line_count;
5244 else
5245#ifdef FEAT_FOLDING
5246 if (hasAnyFolding(curwin))
5247 {
5248 linenr_T last;
5249
5250 /* count each sequence of folded lines as one logical line */
5251 while (n--)
5252 {
5253 if (hasFolding(lnum, NULL, &last))
5254 lnum = last + 1;
5255 else
5256 ++lnum;
5257 if (lnum >= curbuf->b_ml.ml_line_count)
5258 break;
5259 }
5260 if (lnum > curbuf->b_ml.ml_line_count)
5261 lnum = curbuf->b_ml.ml_line_count;
5262 }
5263 else
5264#endif
5265 lnum += n;
5266 curwin->w_cursor.lnum = lnum;
5267 }
5268
5269 /* try to advance to the column we want to be at */
5270 coladvance(curwin->w_curswant);
5271
5272 if (upd_topline)
5273 update_topline(); /* make sure curwin->w_topline is valid */
5274
5275 return OK;
5276}
5277
5278/*
5279 * Stuff the last inserted text in the read buffer.
5280 * Last_insert actually is a copy of the redo buffer, so we
5281 * first have to remove the command.
5282 */
5283 int
5284stuff_inserted(c, count, no_esc)
5285 int c; /* Command character to be inserted */
5286 long count; /* Repeat this many times */
5287 int no_esc; /* Don't add an ESC at the end */
5288{
5289 char_u *esc_ptr;
5290 char_u *ptr;
5291 char_u *last_ptr;
5292 char_u last = NUL;
5293
5294 ptr = get_last_insert();
5295 if (ptr == NULL)
5296 {
5297 EMSG(_(e_noinstext));
5298 return FAIL;
5299 }
5300
5301 /* may want to stuff the command character, to start Insert mode */
5302 if (c != NUL)
5303 stuffcharReadbuff(c);
5304 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
5305 *esc_ptr = NUL; /* remove the ESC */
5306
5307 /* when the last char is either "0" or "^" it will be quoted if no ESC
5308 * comes after it OR if it will inserted more than once and "ptr"
5309 * starts with ^D. -- Acevedo
5310 */
5311 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
5312 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
5313 && (no_esc || (*ptr == Ctrl_D && count > 1)))
5314 {
5315 last = *last_ptr;
5316 *last_ptr = NUL;
5317 }
5318
5319 do
5320 {
5321 stuffReadbuff(ptr);
5322 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
5323 if (last)
5324 stuffReadbuff((char_u *)(last == '0'
5325 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
5326 : IF_EB("\026^", CTRL_V_STR "^")));
5327 }
5328 while (--count > 0);
5329
5330 if (last)
5331 *last_ptr = last;
5332
5333 if (esc_ptr != NULL)
5334 *esc_ptr = ESC; /* put the ESC back */
5335
5336 /* may want to stuff a trailing ESC, to get out of Insert mode */
5337 if (!no_esc)
5338 stuffcharReadbuff(ESC);
5339
5340 return OK;
5341}
5342
5343 char_u *
5344get_last_insert()
5345{
5346 if (last_insert == NULL)
5347 return NULL;
5348 return last_insert + last_insert_skip;
5349}
5350
5351/*
5352 * Get last inserted string, and remove trailing <Esc>.
5353 * Returns pointer to allocated memory (must be freed) or NULL.
5354 */
5355 char_u *
5356get_last_insert_save()
5357{
5358 char_u *s;
5359 int len;
5360
5361 if (last_insert == NULL)
5362 return NULL;
5363 s = vim_strsave(last_insert + last_insert_skip);
5364 if (s != NULL)
5365 {
5366 len = (int)STRLEN(s);
5367 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
5368 s[len - 1] = NUL;
5369 }
5370 return s;
5371}
5372
5373/*
5374 * Check the word in front of the cursor for an abbreviation.
5375 * Called when the non-id character "c" has been entered.
5376 * When an abbreviation is recognized it is removed from the text and
5377 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
5378 */
5379 static int
5380echeck_abbr(c)
5381 int c;
5382{
5383 /* Don't check for abbreviation in paste mode, when disabled and just
5384 * after moving around with cursor keys. */
5385 if (p_paste || no_abbr || arrow_used)
5386 return FALSE;
5387
5388 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
5389 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
5390}
5391
5392/*
5393 * replace-stack functions
5394 *
5395 * When replacing characters, the replaced characters are remembered for each
5396 * new character. This is used to re-insert the old text when backspacing.
5397 *
5398 * There is a NUL headed list of characters for each character that is
5399 * currently in the file after the insertion point. When BS is used, one NUL
5400 * headed list is put back for the deleted character.
5401 *
5402 * For a newline, there are two NUL headed lists. One contains the characters
5403 * that the NL replaced. The extra one stores the characters after the cursor
5404 * that were deleted (always white space).
5405 *
5406 * Replace_offset is normally 0, in which case replace_push will add a new
5407 * character at the end of the stack. If replace_offset is not 0, that many
5408 * characters will be left on the stack above the newly inserted character.
5409 */
5410
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00005411static char_u *replace_stack = NULL;
5412static long replace_stack_nr = 0; /* next entry in replace stack */
5413static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414
5415 void
5416replace_push(c)
5417 int c; /* character that is replaced (NUL is none) */
5418{
5419 char_u *p;
5420
5421 if (replace_stack_nr < replace_offset) /* nothing to do */
5422 return;
5423 if (replace_stack_len <= replace_stack_nr)
5424 {
5425 replace_stack_len += 50;
5426 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
5427 if (p == NULL) /* out of memory */
5428 {
5429 replace_stack_len -= 50;
5430 return;
5431 }
5432 if (replace_stack != NULL)
5433 {
5434 mch_memmove(p, replace_stack,
5435 (size_t)(replace_stack_nr * sizeof(char_u)));
5436 vim_free(replace_stack);
5437 }
5438 replace_stack = p;
5439 }
5440 p = replace_stack + replace_stack_nr - replace_offset;
5441 if (replace_offset)
5442 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
5443 *p = c;
5444 ++replace_stack_nr;
5445}
5446
5447/*
5448 * call replace_push(c) with replace_offset set to the first NUL.
5449 */
5450 static void
5451replace_push_off(c)
5452 int c;
5453{
5454 char_u *p;
5455
5456 p = replace_stack + replace_stack_nr;
5457 for (replace_offset = 1; replace_offset < replace_stack_nr;
5458 ++replace_offset)
5459 if (*--p == NUL)
5460 break;
5461 replace_push(c);
5462 replace_offset = 0;
5463}
5464
5465/*
5466 * Pop one item from the replace stack.
5467 * return -1 if stack empty
5468 * return replaced character or NUL otherwise
5469 */
5470 static int
5471replace_pop()
5472{
5473 if (replace_stack_nr == 0)
5474 return -1;
5475 return (int)replace_stack[--replace_stack_nr];
5476}
5477
5478/*
5479 * Join the top two items on the replace stack. This removes to "off"'th NUL
5480 * encountered.
5481 */
5482 static void
5483replace_join(off)
5484 int off; /* offset for which NUL to remove */
5485{
5486 int i;
5487
5488 for (i = replace_stack_nr; --i >= 0; )
5489 if (replace_stack[i] == NUL && off-- <= 0)
5490 {
5491 --replace_stack_nr;
5492 mch_memmove(replace_stack + i, replace_stack + i + 1,
5493 (size_t)(replace_stack_nr - i));
5494 return;
5495 }
5496}
5497
5498/*
5499 * Pop bytes from the replace stack until a NUL is found, and insert them
5500 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
5501 */
5502 static void
5503replace_pop_ins()
5504{
5505 int cc;
5506 int oldState = State;
5507
5508 State = NORMAL; /* don't want REPLACE here */
5509 while ((cc = replace_pop()) > 0)
5510 {
5511#ifdef FEAT_MBYTE
5512 mb_replace_pop_ins(cc);
5513#else
5514 ins_char(cc);
5515#endif
5516 dec_cursor();
5517 }
5518 State = oldState;
5519}
5520
5521#ifdef FEAT_MBYTE
5522/*
5523 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
5524 * indicates a multi-byte char, pop the other bytes too.
5525 */
5526 static void
5527mb_replace_pop_ins(cc)
5528 int cc;
5529{
5530 int n;
5531 char_u buf[MB_MAXBYTES];
5532 int i;
5533 int c;
5534
5535 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
5536 {
5537 buf[0] = cc;
5538 for (i = 1; i < n; ++i)
5539 buf[i] = replace_pop();
5540 ins_bytes_len(buf, n);
5541 }
5542 else
5543 ins_char(cc);
5544
5545 if (enc_utf8)
5546 /* Handle composing chars. */
5547 for (;;)
5548 {
5549 c = replace_pop();
5550 if (c == -1) /* stack empty */
5551 break;
5552 if ((n = MB_BYTE2LEN(c)) == 1)
5553 {
5554 /* Not a multi-byte char, put it back. */
5555 replace_push(c);
5556 break;
5557 }
5558 else
5559 {
5560 buf[0] = c;
5561 for (i = 1; i < n; ++i)
5562 buf[i] = replace_pop();
5563 if (utf_iscomposing(utf_ptr2char(buf)))
5564 ins_bytes_len(buf, n);
5565 else
5566 {
5567 /* Not a composing char, put it back. */
5568 for (i = n - 1; i >= 0; --i)
5569 replace_push(buf[i]);
5570 break;
5571 }
5572 }
5573 }
5574}
5575#endif
5576
5577/*
5578 * make the replace stack empty
5579 * (called when exiting replace mode)
5580 */
5581 static void
5582replace_flush()
5583{
5584 vim_free(replace_stack);
5585 replace_stack = NULL;
5586 replace_stack_len = 0;
5587 replace_stack_nr = 0;
5588}
5589
5590/*
5591 * Handle doing a BS for one character.
5592 * cc < 0: replace stack empty, just move cursor
5593 * cc == 0: character was inserted, delete it
5594 * cc > 0: character was replaced, put cc (first byte of original char) back
5595 * and check for more characters to be put back
5596 */
5597 static void
5598replace_do_bs()
5599{
5600 int cc;
5601#ifdef FEAT_VREPLACE
5602 int orig_len = 0;
5603 int ins_len;
5604 int orig_vcols = 0;
5605 colnr_T start_vcol;
5606 char_u *p;
5607 int i;
5608 int vcol;
5609#endif
5610
5611 cc = replace_pop();
5612 if (cc > 0)
5613 {
5614#ifdef FEAT_VREPLACE
5615 if (State & VREPLACE_FLAG)
5616 {
5617 /* Get the number of screen cells used by the character we are
5618 * going to delete. */
5619 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
5620 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
5621 }
5622#endif
5623#ifdef FEAT_MBYTE
5624 if (has_mbyte)
5625 {
5626 del_char(FALSE);
5627# ifdef FEAT_VREPLACE
5628 if (State & VREPLACE_FLAG)
5629 orig_len = STRLEN(ml_get_cursor());
5630# endif
5631 replace_push(cc);
5632 }
5633 else
5634#endif
5635 {
5636 pchar_cursor(cc);
5637#ifdef FEAT_VREPLACE
5638 if (State & VREPLACE_FLAG)
5639 orig_len = STRLEN(ml_get_cursor()) - 1;
5640#endif
5641 }
5642 replace_pop_ins();
5643
5644#ifdef FEAT_VREPLACE
5645 if (State & VREPLACE_FLAG)
5646 {
5647 /* Get the number of screen cells used by the inserted characters */
5648 p = ml_get_cursor();
5649 ins_len = STRLEN(p) - orig_len;
5650 vcol = start_vcol;
5651 for (i = 0; i < ins_len; ++i)
5652 {
5653 vcol += chartabsize(p + i, vcol);
5654#ifdef FEAT_MBYTE
5655 i += (*mb_ptr2len_check)(p) - 1;
5656#endif
5657 }
5658 vcol -= start_vcol;
5659
5660 /* Delete spaces that were inserted after the cursor to keep the
5661 * text aligned. */
5662 curwin->w_cursor.col += ins_len;
5663 while (vcol > orig_vcols && gchar_cursor() == ' ')
5664 {
5665 del_char(FALSE);
5666 ++orig_vcols;
5667 }
5668 curwin->w_cursor.col -= ins_len;
5669 }
5670#endif
5671
5672 /* mark the buffer as changed and prepare for displaying */
5673 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
5674 }
5675 else if (cc == 0)
5676 (void)del_char(FALSE);
5677}
5678
5679#ifdef FEAT_CINDENT
5680/*
5681 * Return TRUE if C-indenting is on.
5682 */
5683 static int
5684cindent_on()
5685{
5686 return (!p_paste && (curbuf->b_p_cin
5687# ifdef FEAT_EVAL
5688 || *curbuf->b_p_inde != NUL
5689# endif
5690 ));
5691}
5692#endif
5693
5694#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
5695/*
5696 * Re-indent the current line, based on the current contents of it and the
5697 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
5698 * confused what all the part that handles Control-T is doing that I'm not.
5699 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
5700 */
5701
5702 void
5703fixthisline(get_the_indent)
5704 int (*get_the_indent) __ARGS((void));
5705{
5706 change_indent(INDENT_SET, get_the_indent(), FALSE, 0);
5707 if (linewhite(curwin->w_cursor.lnum))
5708 did_ai = TRUE; /* delete the indent if the line stays empty */
5709}
5710
5711 void
5712fix_indent()
5713{
5714 if (p_paste)
5715 return;
5716# ifdef FEAT_LISP
5717 if (curbuf->b_p_lisp && curbuf->b_p_ai)
5718 fixthisline(get_lisp_indent);
5719# endif
5720# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
5721 else
5722# endif
5723# ifdef FEAT_CINDENT
5724 if (cindent_on())
5725 do_c_expr_indent();
5726# endif
5727}
5728
5729#endif
5730
5731#ifdef FEAT_CINDENT
5732/*
5733 * return TRUE if 'cinkeys' contains the key "keytyped",
5734 * when == '*': Only if key is preceded with '*' (indent before insert)
5735 * when == '!': Only if key is prededed with '!' (don't insert)
5736 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
5737 *
5738 * "keytyped" can have a few special values:
5739 * KEY_OPEN_FORW
5740 * KEY_OPEN_BACK
5741 * KEY_COMPLETE just finished completion.
5742 *
5743 * If line_is_empty is TRUE accept keys with '0' before them.
5744 */
5745 int
5746in_cinkeys(keytyped, when, line_is_empty)
5747 int keytyped;
5748 int when;
5749 int line_is_empty;
5750{
5751 char_u *look;
5752 int try_match;
5753 int try_match_word;
5754 char_u *p;
5755 char_u *line;
5756 int icase;
5757 int i;
5758
5759#ifdef FEAT_EVAL
5760 if (*curbuf->b_p_inde != NUL)
5761 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
5762 else
5763#endif
5764 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
5765 while (*look)
5766 {
5767 /*
5768 * Find out if we want to try a match with this key, depending on
5769 * 'when' and a '*' or '!' before the key.
5770 */
5771 switch (when)
5772 {
5773 case '*': try_match = (*look == '*'); break;
5774 case '!': try_match = (*look == '!'); break;
5775 default: try_match = (*look != '*'); break;
5776 }
5777 if (*look == '*' || *look == '!')
5778 ++look;
5779
5780 /*
5781 * If there is a '0', only accept a match if the line is empty.
5782 * But may still match when typing last char of a word.
5783 */
5784 if (*look == '0')
5785 {
5786 try_match_word = try_match;
5787 if (!line_is_empty)
5788 try_match = FALSE;
5789 ++look;
5790 }
5791 else
5792 try_match_word = FALSE;
5793
5794 /*
5795 * does it look like a control character?
5796 */
5797 if (*look == '^'
5798#ifdef EBCDIC
5799 && (Ctrl_chr(look[1]) != 0)
5800#else
5801 && look[1] >= '?' && look[1] <= '_'
5802#endif
5803 )
5804 {
5805 if (try_match && keytyped == Ctrl_chr(look[1]))
5806 return TRUE;
5807 look += 2;
5808 }
5809 /*
5810 * 'o' means "o" command, open forward.
5811 * 'O' means "O" command, open backward.
5812 */
5813 else if (*look == 'o')
5814 {
5815 if (try_match && keytyped == KEY_OPEN_FORW)
5816 return TRUE;
5817 ++look;
5818 }
5819 else if (*look == 'O')
5820 {
5821 if (try_match && keytyped == KEY_OPEN_BACK)
5822 return TRUE;
5823 ++look;
5824 }
5825
5826 /*
5827 * 'e' means to check for "else" at start of line and just before the
5828 * cursor.
5829 */
5830 else if (*look == 'e')
5831 {
5832 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
5833 {
5834 p = ml_get_curline();
5835 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
5836 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
5837 return TRUE;
5838 }
5839 ++look;
5840 }
5841
5842 /*
5843 * ':' only causes an indent if it is at the end of a label or case
5844 * statement, or when it was before typing the ':' (to fix
5845 * class::method for C++).
5846 */
5847 else if (*look == ':')
5848 {
5849 if (try_match && keytyped == ':')
5850 {
5851 p = ml_get_curline();
5852 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
5853 return TRUE;
5854 if (curwin->w_cursor.col > 2
5855 && p[curwin->w_cursor.col - 1] == ':'
5856 && p[curwin->w_cursor.col - 2] == ':')
5857 {
5858 p[curwin->w_cursor.col - 1] = ' ';
5859 i = (cin_iscase(p) || cin_isscopedecl(p)
5860 || cin_islabel(30));
5861 p = ml_get_curline();
5862 p[curwin->w_cursor.col - 1] = ':';
5863 if (i)
5864 return TRUE;
5865 }
5866 }
5867 ++look;
5868 }
5869
5870
5871 /*
5872 * Is it a key in <>, maybe?
5873 */
5874 else if (*look == '<')
5875 {
5876 if (try_match)
5877 {
5878 /*
5879 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
5880 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
5881 * >, *, : and ! keys if they really really want to.
5882 */
5883 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
5884 && keytyped == look[1])
5885 return TRUE;
5886
5887 if (keytyped == get_special_key_code(look + 1))
5888 return TRUE;
5889 }
5890 while (*look && *look != '>')
5891 look++;
5892 while (*look == '>')
5893 look++;
5894 }
5895
5896 /*
5897 * Is it a word: "=word"?
5898 */
5899 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
5900 {
5901 ++look;
5902 if (*look == '~')
5903 {
5904 icase = TRUE;
5905 ++look;
5906 }
5907 else
5908 icase = FALSE;
5909 p = vim_strchr(look, ',');
5910 if (p == NULL)
5911 p = look + STRLEN(look);
5912 if ((try_match || try_match_word)
5913 && curwin->w_cursor.col >= (colnr_T)(p - look))
5914 {
5915 int match = FALSE;
5916
5917#ifdef FEAT_INS_EXPAND
5918 if (keytyped == KEY_COMPLETE)
5919 {
5920 char_u *s;
5921
5922 /* Just completed a word, check if it starts with "look".
5923 * search back for the start of a word. */
5924 line = ml_get_curline();
5925# ifdef FEAT_MBYTE
5926 if (has_mbyte)
5927 {
5928 char_u *n;
5929
5930 for (s = line + curwin->w_cursor.col; s > line; s = n)
5931 {
5932 n = mb_prevptr(line, s);
5933 if (!vim_iswordp(n))
5934 break;
5935 }
5936 }
5937 else
5938# endif
5939 for (s = line + curwin->w_cursor.col; s > line; --s)
5940 if (!vim_iswordc(s[-1]))
5941 break;
5942 if (s + (p - look) <= line + curwin->w_cursor.col
5943 && (icase
5944 ? MB_STRNICMP(s, look, p - look)
5945 : STRNCMP(s, look, p - look)) == 0)
5946 match = TRUE;
5947 }
5948 else
5949#endif
5950 /* TODO: multi-byte */
5951 if (keytyped == (int)p[-1] || (icase && keytyped < 256
5952 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
5953 {
5954 line = ml_get_cursor();
5955 if ((curwin->w_cursor.col == (colnr_T)(p - look)
5956 || !vim_iswordc(line[-(p - look) - 1]))
5957 && (icase
5958 ? MB_STRNICMP(line - (p - look), look, p - look)
5959 : STRNCMP(line - (p - look), look, p - look))
5960 == 0)
5961 match = TRUE;
5962 }
5963 if (match && try_match_word && !try_match)
5964 {
5965 /* "0=word": Check if there are only blanks before the
5966 * word. */
5967 line = ml_get_curline();
5968 if ((int)(skipwhite(line) - line) !=
5969 (int)(curwin->w_cursor.col - (p - look)))
5970 match = FALSE;
5971 }
5972 if (match)
5973 return TRUE;
5974 }
5975 look = p;
5976 }
5977
5978 /*
5979 * ok, it's a boring generic character.
5980 */
5981 else
5982 {
5983 if (try_match && *look == keytyped)
5984 return TRUE;
5985 ++look;
5986 }
5987
5988 /*
5989 * Skip over ", ".
5990 */
5991 look = skip_to_option_part(look);
5992 }
5993 return FALSE;
5994}
5995#endif /* FEAT_CINDENT */
5996
5997#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
5998/*
5999 * Map Hebrew keyboard when in hkmap mode.
6000 */
6001 int
6002hkmap(c)
6003 int c;
6004{
6005 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
6006 {
6007 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
6008 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
6009 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
6010 static char_u map[26] =
6011 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
6012 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
6013 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
6014 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
6015 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
6016 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
6017 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
6018 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
6019 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
6020
6021 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
6022 return (int)(map[CharOrd(c)] - 1 + p_aleph);
6023 /* '-1'='sofit' */
6024 else if (c == 'x')
6025 return 'X';
6026 else if (c == 'q')
6027 return '\''; /* {geresh}={'} */
6028 else if (c == 246)
6029 return ' '; /* \"o --> ' ' for a german keyboard */
6030 else if (c == 228)
6031 return ' '; /* \"a --> ' ' -- / -- */
6032 else if (c == 252)
6033 return ' '; /* \"u --> ' ' -- / -- */
6034#ifdef EBCDIC
6035 else if (islower(c))
6036#else
6037 /* NOTE: islower() does not do the right thing for us on Linux so we
6038 * do this the same was as 5.7 and previous, so it works correctly on
6039 * all systems. Specifically, the e.g. Delete and Arrow keys are
6040 * munged and won't work if e.g. searching for Hebrew text.
6041 */
6042 else if (c >= 'a' && c <= 'z')
6043#endif
6044 return (int)(map[CharOrdLow(c)] + p_aleph);
6045 else
6046 return c;
6047 }
6048 else
6049 {
6050 switch (c)
6051 {
6052 case '`': return ';';
6053 case '/': return '.';
6054 case '\'': return ',';
6055 case 'q': return '/';
6056 case 'w': return '\'';
6057
6058 /* Hebrew letters - set offset from 'a' */
6059 case ',': c = '{'; break;
6060 case '.': c = 'v'; break;
6061 case ';': c = 't'; break;
6062 default: {
6063 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
6064
6065#ifdef EBCDIC
6066 /* see note about islower() above */
6067 if (!islower(c))
6068#else
6069 if (c < 'a' || c > 'z')
6070#endif
6071 return c;
6072 c = str[CharOrdLow(c)];
6073 break;
6074 }
6075 }
6076
6077 return (int)(CharOrdLow(c) + p_aleph);
6078 }
6079}
6080#endif
6081
6082 static void
6083ins_reg()
6084{
6085 int need_redraw = FALSE;
6086 int regname;
6087 int literally = 0;
6088
6089 /*
6090 * If we are going to wait for a character, show a '"'.
6091 */
6092 pc_status = PC_STATUS_UNSET;
6093 if (redrawing() && !char_avail())
6094 {
6095 /* may need to redraw when no more chars available now */
6096 ins_redraw();
6097
6098 edit_putchar('"', TRUE);
6099#ifdef FEAT_CMDL_INFO
6100 add_to_showcmd_c(Ctrl_R);
6101#endif
6102 }
6103
6104#ifdef USE_ON_FLY_SCROLL
6105 dont_scroll = TRUE; /* disallow scrolling here */
6106#endif
6107
6108 /*
6109 * Don't map the register name. This also prevents the mode message to be
6110 * deleted when ESC is hit.
6111 */
6112 ++no_mapping;
6113 regname = safe_vgetc();
6114#ifdef FEAT_LANGMAP
6115 LANGMAP_ADJUST(regname, TRUE);
6116#endif
6117 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
6118 {
6119 /* Get a third key for literal register insertion */
6120 literally = regname;
6121#ifdef FEAT_CMDL_INFO
6122 add_to_showcmd_c(literally);
6123#endif
6124 regname = safe_vgetc();
6125#ifdef FEAT_LANGMAP
6126 LANGMAP_ADJUST(regname, TRUE);
6127#endif
6128 }
6129 --no_mapping;
6130
6131#ifdef FEAT_EVAL
6132 /*
6133 * Don't call u_sync() while getting the expression,
6134 * evaluating it or giving an error message for it!
6135 */
6136 ++no_u_sync;
6137 if (regname == '=')
6138 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006139# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006141# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006143# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 /* Restore the Input Method. */
6145 if (im_on)
6146 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006147# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00006149 if (regname == NUL || !valid_yank_reg(regname, FALSE))
6150 {
6151 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00006153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 else
6155 {
6156#endif
6157 if (literally == Ctrl_O || literally == Ctrl_P)
6158 {
6159 /* Append the command to the redo buffer. */
6160 AppendCharToRedobuff(Ctrl_R);
6161 AppendCharToRedobuff(literally);
6162 AppendCharToRedobuff(regname);
6163
6164 do_put(regname, BACKWARD, 1L,
6165 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
6166 }
6167 else if (insert_reg(regname, literally) == FAIL)
6168 {
6169 vim_beep();
6170 need_redraw = TRUE; /* remove the '"' */
6171 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00006172 else if (stop_insert_mode)
6173 /* When the '=' register was used and a function was invoked that
6174 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
6175 * insert anything, need to remove the '"' */
6176 need_redraw = TRUE;
6177
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178#ifdef FEAT_EVAL
6179 }
6180 --no_u_sync;
6181#endif
6182#ifdef FEAT_CMDL_INFO
6183 clear_showcmd();
6184#endif
6185
6186 /* If the inserted register is empty, we need to remove the '"' */
6187 if (need_redraw || stuff_empty())
6188 edit_unputchar();
6189}
6190
6191/*
6192 * CTRL-G commands in Insert mode.
6193 */
6194 static void
6195ins_ctrl_g()
6196{
6197 int c;
6198
6199#ifdef FEAT_INS_EXPAND
6200 /* Right after CTRL-X the cursor will be after the ruler. */
6201 setcursor();
6202#endif
6203
6204 /*
6205 * Don't map the second key. This also prevents the mode message to be
6206 * deleted when ESC is hit.
6207 */
6208 ++no_mapping;
6209 c = safe_vgetc();
6210 --no_mapping;
6211 switch (c)
6212 {
6213 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
6214 case K_UP:
6215 case Ctrl_K:
6216 case 'k': ins_up(TRUE);
6217 break;
6218
6219 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
6220 case K_DOWN:
6221 case Ctrl_J:
6222 case 'j': ins_down(TRUE);
6223 break;
6224
6225 /* CTRL-G u: start new undoable edit */
6226 case 'u': u_sync();
6227 ins_need_undo = TRUE;
6228 break;
6229
6230 /* Unknown CTRL-G command, reserved for future expansion. */
6231 default: vim_beep();
6232 }
6233}
6234
6235/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006236 * CTRL-^ in Insert mode.
6237 */
6238 static void
6239ins_ctrl_hat()
6240{
6241 if (map_to_exists_mode((char_u *)"", LANGMAP))
6242 {
6243 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
6244 if (State & LANGMAP)
6245 {
6246 curbuf->b_p_iminsert = B_IMODE_NONE;
6247 State &= ~LANGMAP;
6248 }
6249 else
6250 {
6251 curbuf->b_p_iminsert = B_IMODE_LMAP;
6252 State |= LANGMAP;
6253#ifdef USE_IM_CONTROL
6254 im_set_active(FALSE);
6255#endif
6256 }
6257 }
6258#ifdef USE_IM_CONTROL
6259 else
6260 {
6261 /* There are no ":lmap" mappings, toggle IM */
6262 if (im_get_status())
6263 {
6264 curbuf->b_p_iminsert = B_IMODE_NONE;
6265 im_set_active(FALSE);
6266 }
6267 else
6268 {
6269 curbuf->b_p_iminsert = B_IMODE_IM;
6270 State &= ~LANGMAP;
6271 im_set_active(TRUE);
6272 }
6273 }
6274#endif
6275 set_iminsert_global();
6276 showmode();
6277#ifdef FEAT_GUI
6278 /* may show different cursor shape or color */
6279 if (gui.in_use)
6280 gui_update_cursor(TRUE, FALSE);
6281#endif
6282#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
6283 /* Show/unshow value of 'keymap' in status lines. */
6284 status_redraw_curbuf();
6285#endif
6286}
6287
6288/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 * Handle ESC in insert mode.
6290 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
6291 * insert.
6292 */
6293 static int
6294ins_esc(count, cmdchar)
6295 long *count;
6296 int cmdchar;
6297{
6298 int temp;
6299 static int disabled_redraw = FALSE;
6300
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006301#ifdef FEAT_SYN_HL
6302 check_spell_redraw();
6303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304#if defined(FEAT_HANGULIN)
6305# if defined(ESC_CHG_TO_ENG_MODE)
6306 hangul_input_state_set(0);
6307# endif
6308 if (composing_hangul)
6309 {
6310 push_raw_key(composing_hangul_buffer, 2);
6311 composing_hangul = 0;
6312 }
6313#endif
6314#if defined(FEAT_MBYTE) && defined(MACOS_CLASSIC)
6315 previous_script = GetScriptManagerVariable(smKeyScript);
6316 KeyScript(smKeyRoman); /* or smKeySysScript */
6317#endif
6318
6319 temp = curwin->w_cursor.col;
6320 if (disabled_redraw)
6321 {
6322 --RedrawingDisabled;
6323 disabled_redraw = FALSE;
6324 }
6325 if (!arrow_used)
6326 {
6327 /*
6328 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00006329 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
6330 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 */
6332 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00006333 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334
6335 /*
6336 * Repeating insert may take a long time. Check for
6337 * interrupt now and then.
6338 */
6339 if (*count > 0)
6340 {
6341 line_breakcheck();
6342 if (got_int)
6343 *count = 0;
6344 }
6345
6346 if (--*count > 0) /* repeat what was typed */
6347 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006348 /* Vi repeats the insert without replacing characters. */
6349 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
6350 State &= ~REPLACE_FLAG;
6351
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 (void)start_redo_ins();
6353 if (cmdchar == 'r' || cmdchar == 'v')
6354 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
6355 ++RedrawingDisabled;
6356 disabled_redraw = TRUE;
6357 return FALSE; /* repeat the insert */
6358 }
6359 stop_insert(&curwin->w_cursor, TRUE);
6360 undisplay_dollar();
6361 }
6362
6363 /* When an autoindent was removed, curswant stays after the
6364 * indent */
6365 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
6366 curwin->w_set_curswant = TRUE;
6367
6368 /* Remember the last Insert position in the '^ mark. */
6369 if (!cmdmod.keepjumps)
6370 curbuf->b_last_insert = curwin->w_cursor;
6371
6372 /*
6373 * The cursor should end up on the last inserted character.
6374 */
6375 if ((curwin->w_cursor.col != 0
6376#ifdef FEAT_VIRTUALEDIT
6377 || curwin->w_cursor.coladd > 0
6378#endif
6379 ) && (restart_edit == NUL
6380 || (gchar_cursor() == NUL
6381#ifdef FEAT_VISUAL
6382 && !VIsual_active
6383#endif
6384 ))
6385#ifdef FEAT_RIGHTLEFT
6386 && !revins_on
6387#endif
6388 )
6389 {
6390#ifdef FEAT_VIRTUALEDIT
6391 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
6392 {
6393 oneleft();
6394 if (restart_edit != NUL)
6395 ++curwin->w_cursor.coladd;
6396 }
6397 else
6398#endif
6399 {
6400 --curwin->w_cursor.col;
6401#ifdef FEAT_MBYTE
6402 /* Correct cursor for multi-byte character. */
6403 if (has_mbyte)
6404 mb_adjust_cursor();
6405#endif
6406 }
6407 }
6408
6409#ifdef USE_IM_CONTROL
6410 /* Disable IM to allow typing English directly for Normal mode commands.
6411 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
6412 * well). */
6413 if (!(State & LANGMAP))
6414 im_save_status(&curbuf->b_p_iminsert);
6415 im_set_active(FALSE);
6416#endif
6417
6418 State = NORMAL;
6419 /* need to position cursor again (e.g. when on a TAB ) */
6420 changed_cline_bef_curs();
6421
6422#ifdef FEAT_MOUSE
6423 setmouse();
6424#endif
6425#ifdef CURSOR_SHAPE
6426 ui_cursor_shape(); /* may show different cursor shape */
6427#endif
6428
6429 /*
6430 * When recording or for CTRL-O, need to display the new mode.
6431 * Otherwise remove the mode message.
6432 */
6433 if (Recording || restart_edit != NUL)
6434 showmode();
6435 else if (p_smd)
6436 MSG("");
6437
6438 return TRUE; /* exit Insert mode */
6439}
6440
6441#ifdef FEAT_RIGHTLEFT
6442/*
6443 * Toggle language: hkmap and revins_on.
6444 * Move to end of reverse inserted text.
6445 */
6446 static void
6447ins_ctrl_()
6448{
6449 if (revins_on && revins_chars && revins_scol >= 0)
6450 {
6451 while (gchar_cursor() != NUL && revins_chars--)
6452 ++curwin->w_cursor.col;
6453 }
6454 p_ri = !p_ri;
6455 revins_on = (State == INSERT && p_ri);
6456 if (revins_on)
6457 {
6458 revins_scol = curwin->w_cursor.col;
6459 revins_legal++;
6460 revins_chars = 0;
6461 undisplay_dollar();
6462 }
6463 else
6464 revins_scol = -1;
6465#ifdef FEAT_FKMAP
6466 if (p_altkeymap)
6467 {
6468 /*
6469 * to be consistent also for redo command, using '.'
6470 * set arrow_used to true and stop it - causing to redo
6471 * characters entered in one mode (normal/reverse insert).
6472 */
6473 arrow_used = TRUE;
6474 (void)stop_arrow();
6475 p_fkmap = curwin->w_p_rl ^ p_ri;
6476 if (p_fkmap && p_ri)
6477 State = INSERT;
6478 }
6479 else
6480#endif
6481 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
6482 showmode();
6483}
6484#endif
6485
6486#ifdef FEAT_VISUAL
6487/*
6488 * If 'keymodel' contains "startsel", may start selection.
6489 * Returns TRUE when a CTRL-O and other keys stuffed.
6490 */
6491 static int
6492ins_start_select(c)
6493 int c;
6494{
6495 if (km_startsel)
6496 switch (c)
6497 {
6498 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 case K_PAGEUP:
6501 case K_KPAGEUP:
6502 case K_PAGEDOWN:
6503 case K_KPAGEDOWN:
6504# ifdef MACOS
6505 case K_LEFT:
6506 case K_RIGHT:
6507 case K_UP:
6508 case K_DOWN:
6509 case K_END:
6510 case K_HOME:
6511# endif
6512 if (!(mod_mask & MOD_MASK_SHIFT))
6513 break;
6514 /* FALLTHROUGH */
6515 case K_S_LEFT:
6516 case K_S_RIGHT:
6517 case K_S_UP:
6518 case K_S_DOWN:
6519 case K_S_END:
6520 case K_S_HOME:
6521 /* Start selection right away, the cursor can move with
6522 * CTRL-O when beyond the end of the line. */
6523 start_selection();
6524
6525 /* Execute the key in (insert) Select mode. */
6526 stuffcharReadbuff(Ctrl_O);
6527 if (mod_mask)
6528 {
6529 char_u buf[4];
6530
6531 buf[0] = K_SPECIAL;
6532 buf[1] = KS_MODIFIER;
6533 buf[2] = mod_mask;
6534 buf[3] = NUL;
6535 stuffReadbuff(buf);
6536 }
6537 stuffcharReadbuff(c);
6538 return TRUE;
6539 }
6540 return FALSE;
6541}
6542#endif
6543
6544/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006545 * <Insert> key in Insert mode: toggle insert/remplace mode.
6546 */
6547 static void
6548ins_insert(replaceState)
6549 int replaceState;
6550{
6551#ifdef FEAT_FKMAP
6552 if (p_fkmap && p_ri)
6553 {
6554 beep_flush();
6555 EMSG(farsi_text_3); /* encoded in Farsi */
6556 return;
6557 }
6558#endif
6559
6560#ifdef FEAT_AUTOCMD
6561 set_vim_var_string(VV_INSERTMODE,
6562 (char_u *)((State & REPLACE_FLAG) ? "i" :
6563 replaceState == VREPLACE ? "v" : "r"), 1);
6564 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
6565#endif
6566 if (State & REPLACE_FLAG)
6567 State = INSERT | (State & LANGMAP);
6568 else
6569 State = replaceState | (State & LANGMAP);
6570 AppendCharToRedobuff(K_INS);
6571 showmode();
6572#ifdef CURSOR_SHAPE
6573 ui_cursor_shape(); /* may show different cursor shape */
6574#endif
6575}
6576
6577/*
6578 * Pressed CTRL-O in Insert mode.
6579 */
6580 static void
6581ins_ctrl_o()
6582{
6583#ifdef FEAT_VREPLACE
6584 if (State & VREPLACE_FLAG)
6585 restart_edit = 'V';
6586 else
6587#endif
6588 if (State & REPLACE_FLAG)
6589 restart_edit = 'R';
6590 else
6591 restart_edit = 'I';
6592#ifdef FEAT_VIRTUALEDIT
6593 if (virtual_active())
6594 ins_at_eol = FALSE; /* cursor always keeps its column */
6595 else
6596#endif
6597 ins_at_eol = (gchar_cursor() == NUL);
6598}
6599
6600/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 * If the cursor is on an indent, ^T/^D insert/delete one
6602 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
6603 * Always round the indent to 'shiftwith', this is compatible
6604 * with vi. But vi only supports ^T and ^D after an
6605 * autoindent, we support it everywhere.
6606 */
6607 static void
6608ins_shift(c, lastc)
6609 int c;
6610 int lastc;
6611{
6612 if (stop_arrow() == FAIL)
6613 return;
6614 AppendCharToRedobuff(c);
6615
6616 /*
6617 * 0^D and ^^D: remove all indent.
6618 */
6619 if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col)
6620 {
6621 --curwin->w_cursor.col;
6622 (void)del_char(FALSE); /* delete the '^' or '0' */
6623 /* In Replace mode, restore the characters that '^' or '0' replaced. */
6624 if (State & REPLACE_FLAG)
6625 replace_pop_ins();
6626 if (lastc == '^')
6627 old_indent = get_indent(); /* remember curr. indent */
6628 change_indent(INDENT_SET, 0, TRUE, 0);
6629 }
6630 else
6631 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0);
6632
6633 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
6634 did_ai = FALSE;
6635#ifdef FEAT_SMARTINDENT
6636 did_si = FALSE;
6637 can_si = FALSE;
6638 can_si_back = FALSE;
6639#endif
6640#ifdef FEAT_CINDENT
6641 can_cindent = FALSE; /* no cindenting after ^D or ^T */
6642#endif
6643}
6644
6645 static void
6646ins_del()
6647{
6648 int temp;
6649
6650 if (stop_arrow() == FAIL)
6651 return;
6652 if (gchar_cursor() == NUL) /* delete newline */
6653 {
6654 temp = curwin->w_cursor.col;
6655 if (!can_bs(BS_EOL) /* only if "eol" included */
6656 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
6657 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
6658 || do_join(FALSE) == FAIL)
6659 vim_beep();
6660 else
6661 curwin->w_cursor.col = temp;
6662 }
6663 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
6664 vim_beep();
6665 did_ai = FALSE;
6666#ifdef FEAT_SMARTINDENT
6667 did_si = FALSE;
6668 can_si = FALSE;
6669 can_si_back = FALSE;
6670#endif
6671 AppendCharToRedobuff(K_DEL);
6672}
6673
6674/*
6675 * Handle Backspace, delete-word and delete-line in Insert mode.
6676 * Return TRUE when backspace was actually used.
6677 */
6678 static int
6679ins_bs(c, mode, inserted_space_p)
6680 int c;
6681 int mode;
6682 int *inserted_space_p;
6683{
6684 linenr_T lnum;
6685 int cc;
6686 int temp = 0; /* init for GCC */
6687 colnr_T mincol;
6688 int did_backspace = FALSE;
6689 int in_indent;
6690 int oldState;
6691#ifdef FEAT_MBYTE
6692 int p1, p2;
6693#endif
6694
6695 /*
6696 * can't delete anything in an empty file
6697 * can't backup past first character in buffer
6698 * can't backup past starting point unless 'backspace' > 1
6699 * can backup to a previous line if 'backspace' == 0
6700 */
6701 if ( bufempty()
6702 || (
6703#ifdef FEAT_RIGHTLEFT
6704 !revins_on &&
6705#endif
6706 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
6707 || (!can_bs(BS_START)
6708 && (arrow_used
6709 || (curwin->w_cursor.lnum == Insstart.lnum
6710 && curwin->w_cursor.col <= Insstart.col)))
6711 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
6712 && curwin->w_cursor.col <= ai_col)
6713 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
6714 {
6715 vim_beep();
6716 return FALSE;
6717 }
6718
6719 if (stop_arrow() == FAIL)
6720 return FALSE;
6721 in_indent = inindent(0);
6722#ifdef FEAT_CINDENT
6723 if (in_indent)
6724 can_cindent = FALSE;
6725#endif
6726#ifdef FEAT_COMMENTS
6727 end_comment_pending = NUL; /* After BS, don't auto-end comment */
6728#endif
6729#ifdef FEAT_RIGHTLEFT
6730 if (revins_on) /* put cursor after last inserted char */
6731 inc_cursor();
6732#endif
6733
6734#ifdef FEAT_VIRTUALEDIT
6735 /* Virtualedit:
6736 * BACKSPACE_CHAR eats a virtual space
6737 * BACKSPACE_WORD eats all coladd
6738 * BACKSPACE_LINE eats all coladd and keeps going
6739 */
6740 if (curwin->w_cursor.coladd > 0)
6741 {
6742 if (mode == BACKSPACE_CHAR)
6743 {
6744 --curwin->w_cursor.coladd;
6745 return TRUE;
6746 }
6747 if (mode == BACKSPACE_WORD)
6748 {
6749 curwin->w_cursor.coladd = 0;
6750 return TRUE;
6751 }
6752 curwin->w_cursor.coladd = 0;
6753 }
6754#endif
6755
6756 /*
6757 * delete newline!
6758 */
6759 if (curwin->w_cursor.col == 0)
6760 {
6761 lnum = Insstart.lnum;
6762 if (curwin->w_cursor.lnum == Insstart.lnum
6763#ifdef FEAT_RIGHTLEFT
6764 || revins_on
6765#endif
6766 )
6767 {
6768 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
6769 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
6770 return FALSE;
6771 --Insstart.lnum;
6772 Insstart.col = MAXCOL;
6773 }
6774 /*
6775 * In replace mode:
6776 * cc < 0: NL was inserted, delete it
6777 * cc >= 0: NL was replaced, put original characters back
6778 */
6779 cc = -1;
6780 if (State & REPLACE_FLAG)
6781 cc = replace_pop(); /* returns -1 if NL was inserted */
6782 /*
6783 * In replace mode, in the line we started replacing, we only move the
6784 * cursor.
6785 */
6786 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
6787 {
6788 dec_cursor();
6789 }
6790 else
6791 {
6792#ifdef FEAT_VREPLACE
6793 if (!(State & VREPLACE_FLAG)
6794 || curwin->w_cursor.lnum > orig_line_count)
6795#endif
6796 {
6797 temp = gchar_cursor(); /* remember current char */
6798 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00006799
6800 /* When "aw" is in 'formatoptions' we must delete the space at
6801 * the end of the line, otherwise the line will be broken
6802 * again when auto-formatting. */
6803 if (has_format_option(FO_AUTO)
6804 && has_format_option(FO_WHITE_PAR))
6805 {
6806 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
6807 TRUE);
6808 int len;
6809
6810 len = STRLEN(ptr);
6811 if (len > 0 && ptr[len - 1] == ' ')
6812 ptr[len - 1] = NUL;
6813 }
6814
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 (void)do_join(FALSE);
6816 if (temp == NUL && gchar_cursor() != NUL)
6817 inc_cursor();
6818 }
6819#ifdef FEAT_VREPLACE
6820 else
6821 dec_cursor();
6822#endif
6823
6824 /*
6825 * In REPLACE mode we have to put back the text that was replaced
6826 * by the NL. On the replace stack is first a NUL-terminated
6827 * sequence of characters that were deleted and then the
6828 * characters that NL replaced.
6829 */
6830 if (State & REPLACE_FLAG)
6831 {
6832 /*
6833 * Do the next ins_char() in NORMAL state, to
6834 * prevent ins_char() from replacing characters and
6835 * avoiding showmatch().
6836 */
6837 oldState = State;
6838 State = NORMAL;
6839 /*
6840 * restore characters (blanks) deleted after cursor
6841 */
6842 while (cc > 0)
6843 {
6844 temp = curwin->w_cursor.col;
6845#ifdef FEAT_MBYTE
6846 mb_replace_pop_ins(cc);
6847#else
6848 ins_char(cc);
6849#endif
6850 curwin->w_cursor.col = temp;
6851 cc = replace_pop();
6852 }
6853 /* restore the characters that NL replaced */
6854 replace_pop_ins();
6855 State = oldState;
6856 }
6857 }
6858 did_ai = FALSE;
6859 }
6860 else
6861 {
6862 /*
6863 * Delete character(s) before the cursor.
6864 */
6865#ifdef FEAT_RIGHTLEFT
6866 if (revins_on) /* put cursor on last inserted char */
6867 dec_cursor();
6868#endif
6869 mincol = 0;
6870 /* keep indent */
6871 if (mode == BACKSPACE_LINE && curbuf->b_p_ai
6872#ifdef FEAT_RIGHTLEFT
6873 && !revins_on
6874#endif
6875 )
6876 {
6877 temp = curwin->w_cursor.col;
6878 beginline(BL_WHITE);
6879 if (curwin->w_cursor.col < (colnr_T)temp)
6880 mincol = curwin->w_cursor.col;
6881 curwin->w_cursor.col = temp;
6882 }
6883
6884 /*
6885 * Handle deleting one 'shiftwidth' or 'softtabstop'.
6886 */
6887 if ( mode == BACKSPACE_CHAR
6888 && ((p_sta && in_indent)
6889 || (curbuf->b_p_sts
6890 && (*(ml_get_cursor() - 1) == TAB
6891 || (*(ml_get_cursor() - 1) == ' '
6892 && (!*inserted_space_p
6893 || arrow_used))))))
6894 {
6895 int ts;
6896 colnr_T vcol;
6897 colnr_T want_vcol;
6898 int extra = 0;
6899
6900 *inserted_space_p = FALSE;
6901 if (p_sta)
6902 ts = curbuf->b_p_sw;
6903 else
6904 ts = curbuf->b_p_sts;
6905 /* Compute the virtual column where we want to be. Since
6906 * 'showbreak' may get in the way, need to get the last column of
6907 * the previous character. */
6908 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6909 dec_cursor();
6910 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
6911 inc_cursor();
6912 want_vcol = (want_vcol / ts) * ts;
6913
6914 /* delete characters until we are at or before want_vcol */
6915 while (vcol > want_vcol
6916 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
6917 {
6918 dec_cursor();
6919 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6920 if (State & REPLACE_FLAG)
6921 {
6922 /* Don't delete characters before the insert point when in
6923 * Replace mode */
6924 if (curwin->w_cursor.lnum != Insstart.lnum
6925 || curwin->w_cursor.col >= Insstart.col)
6926 {
6927#if 0 /* what was this for? It causes problems when sw != ts. */
6928 if (State == REPLACE && (int)vcol < want_vcol)
6929 {
6930 (void)del_char(FALSE);
6931 extra = 2; /* don't pop too much */
6932 }
6933 else
6934#endif
6935 replace_do_bs();
6936 }
6937 }
6938 else
6939 (void)del_char(FALSE);
6940 }
6941
6942 /* insert extra spaces until we are at want_vcol */
6943 while (vcol < want_vcol)
6944 {
6945 /* Remember the first char we inserted */
6946 if (curwin->w_cursor.lnum == Insstart.lnum
6947 && curwin->w_cursor.col < Insstart.col)
6948 Insstart.col = curwin->w_cursor.col;
6949
6950#ifdef FEAT_VREPLACE
6951 if (State & VREPLACE_FLAG)
6952 ins_char(' ');
6953 else
6954#endif
6955 {
6956 ins_str((char_u *)" ");
6957 if ((State & REPLACE_FLAG) && extra <= 1)
6958 {
6959 if (extra)
6960 replace_push_off(NUL);
6961 else
6962 replace_push(NUL);
6963 }
6964 if (extra == 2)
6965 extra = 1;
6966 }
6967 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6968 }
6969 }
6970
6971 /*
6972 * Delete upto starting point, start of line or previous word.
6973 */
6974 else do
6975 {
6976#ifdef FEAT_RIGHTLEFT
6977 if (!revins_on) /* put cursor on char to be deleted */
6978#endif
6979 dec_cursor();
6980
6981 /* start of word? */
6982 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
6983 {
6984 mode = BACKSPACE_WORD_NOT_SPACE;
6985 temp = vim_iswordc(gchar_cursor());
6986 }
6987 /* end of word? */
6988 else if (mode == BACKSPACE_WORD_NOT_SPACE
6989 && (vim_isspace(cc = gchar_cursor())
6990 || vim_iswordc(cc) != temp))
6991 {
6992#ifdef FEAT_RIGHTLEFT
6993 if (!revins_on)
6994#endif
6995 inc_cursor();
6996#ifdef FEAT_RIGHTLEFT
6997 else if (State & REPLACE_FLAG)
6998 dec_cursor();
6999#endif
7000 break;
7001 }
7002 if (State & REPLACE_FLAG)
7003 replace_do_bs();
7004 else
7005 {
7006#ifdef FEAT_MBYTE
7007 if (enc_utf8 && p_deco)
7008 (void)utfc_ptr2char(ml_get_cursor(), &p1, &p2);
7009#endif
7010 (void)del_char(FALSE);
7011#ifdef FEAT_MBYTE
7012 /*
7013 * If p1 or p2 is non-zero, there are combining characters we
7014 * need to take account of. Don't back up before the base
7015 * character.
7016 */
7017 if (enc_utf8 && p_deco && (p1 != NUL || p2 != NUL))
7018 inc_cursor();
7019#endif
7020#ifdef FEAT_RIGHTLEFT
7021 if (revins_chars)
7022 {
7023 revins_chars--;
7024 revins_legal++;
7025 }
7026 if (revins_on && gchar_cursor() == NUL)
7027 break;
7028#endif
7029 }
7030 /* Just a single backspace?: */
7031 if (mode == BACKSPACE_CHAR)
7032 break;
7033 } while (
7034#ifdef FEAT_RIGHTLEFT
7035 revins_on ||
7036#endif
7037 (curwin->w_cursor.col > mincol
7038 && (curwin->w_cursor.lnum != Insstart.lnum
7039 || curwin->w_cursor.col != Insstart.col)));
7040 did_backspace = TRUE;
7041 }
7042#ifdef FEAT_SMARTINDENT
7043 did_si = FALSE;
7044 can_si = FALSE;
7045 can_si_back = FALSE;
7046#endif
7047 if (curwin->w_cursor.col <= 1)
7048 did_ai = FALSE;
7049 /*
7050 * It's a little strange to put backspaces into the redo
7051 * buffer, but it makes auto-indent a lot easier to deal
7052 * with.
7053 */
7054 AppendCharToRedobuff(c);
7055
7056 /* If deleted before the insertion point, adjust it */
7057 if (curwin->w_cursor.lnum == Insstart.lnum
7058 && curwin->w_cursor.col < Insstart.col)
7059 Insstart.col = curwin->w_cursor.col;
7060
7061 /* vi behaviour: the cursor moves backward but the character that
7062 * was there remains visible
7063 * Vim behaviour: the cursor moves backward and the character that
7064 * was there is erased from the screen.
7065 * We can emulate the vi behaviour by pretending there is a dollar
7066 * displayed even when there isn't.
7067 * --pkv Sun Jan 19 01:56:40 EST 2003 */
7068 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
7069 dollar_vcol = curwin->w_virtcol;
7070
7071 return did_backspace;
7072}
7073
7074#ifdef FEAT_MOUSE
7075 static void
7076ins_mouse(c)
7077 int c;
7078{
7079 pos_T tpos;
7080
7081# ifdef FEAT_GUI
7082 /* When GUI is active, also move/paste when 'mouse' is empty */
7083 if (!gui.in_use)
7084# endif
7085 if (!mouse_has(MOUSE_INSERT))
7086 return;
7087
7088 undisplay_dollar();
7089 tpos = curwin->w_cursor;
7090 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
7091 {
7092 start_arrow(&tpos);
7093# ifdef FEAT_CINDENT
7094 can_cindent = TRUE;
7095# endif
7096 }
7097
7098#ifdef FEAT_WINDOWS
7099 /* redraw status lines (in case another window became active) */
7100 redraw_statuslines();
7101#endif
7102}
7103
7104 static void
7105ins_mousescroll(up)
7106 int up;
7107{
7108 pos_T tpos;
7109# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7110 win_T *old_curwin;
7111# endif
7112
7113 tpos = curwin->w_cursor;
7114
7115# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7116 old_curwin = curwin;
7117
7118 /* Currently the mouse coordinates are only known in the GUI. */
7119 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
7120 {
7121 int row, col;
7122
7123 row = mouse_row;
7124 col = mouse_col;
7125
7126 /* find the window at the pointer coordinates */
7127 curwin = mouse_find_win(&row, &col);
7128 curbuf = curwin->w_buffer;
7129 }
7130 if (curwin == old_curwin)
7131# endif
7132 undisplay_dollar();
7133
7134 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
7135 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
7136 else
7137 scroll_redraw(up, 3L);
7138
7139# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
7140 curwin->w_redr_status = TRUE;
7141
7142 curwin = old_curwin;
7143 curbuf = curwin->w_buffer;
7144# endif
7145
7146 if (!equalpos(curwin->w_cursor, tpos))
7147 {
7148 start_arrow(&tpos);
7149# ifdef FEAT_CINDENT
7150 can_cindent = TRUE;
7151# endif
7152 }
7153}
7154#endif
7155
7156#ifdef FEAT_GUI
7157 void
7158ins_scroll()
7159{
7160 pos_T tpos;
7161
7162 undisplay_dollar();
7163 tpos = curwin->w_cursor;
7164 if (gui_do_scroll())
7165 {
7166 start_arrow(&tpos);
7167# ifdef FEAT_CINDENT
7168 can_cindent = TRUE;
7169# endif
7170 }
7171}
7172
7173 void
7174ins_horscroll()
7175{
7176 pos_T tpos;
7177
7178 undisplay_dollar();
7179 tpos = curwin->w_cursor;
7180 if (gui_do_horiz_scroll())
7181 {
7182 start_arrow(&tpos);
7183# ifdef FEAT_CINDENT
7184 can_cindent = TRUE;
7185# endif
7186 }
7187}
7188#endif
7189
7190 static void
7191ins_left()
7192{
7193 pos_T tpos;
7194
7195#ifdef FEAT_FOLDING
7196 if ((fdo_flags & FDO_HOR) && KeyTyped)
7197 foldOpenCursor();
7198#endif
7199 undisplay_dollar();
7200 tpos = curwin->w_cursor;
7201 if (oneleft() == OK)
7202 {
7203 start_arrow(&tpos);
7204#ifdef FEAT_RIGHTLEFT
7205 /* If exit reversed string, position is fixed */
7206 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
7207 revins_legal++;
7208 revins_chars++;
7209#endif
7210 }
7211
7212 /*
7213 * if 'whichwrap' set for cursor in insert mode may go to
7214 * previous line
7215 */
7216 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
7217 {
7218 start_arrow(&tpos);
7219 --(curwin->w_cursor.lnum);
7220 coladvance((colnr_T)MAXCOL);
7221 curwin->w_set_curswant = TRUE; /* so we stay at the end */
7222 }
7223 else
7224 vim_beep();
7225}
7226
7227 static void
7228ins_home(c)
7229 int c;
7230{
7231 pos_T tpos;
7232
7233#ifdef FEAT_FOLDING
7234 if ((fdo_flags & FDO_HOR) && KeyTyped)
7235 foldOpenCursor();
7236#endif
7237 undisplay_dollar();
7238 tpos = curwin->w_cursor;
7239 if (c == K_C_HOME)
7240 curwin->w_cursor.lnum = 1;
7241 curwin->w_cursor.col = 0;
7242#ifdef FEAT_VIRTUALEDIT
7243 curwin->w_cursor.coladd = 0;
7244#endif
7245 curwin->w_curswant = 0;
7246 start_arrow(&tpos);
7247}
7248
7249 static void
7250ins_end(c)
7251 int c;
7252{
7253 pos_T tpos;
7254
7255#ifdef FEAT_FOLDING
7256 if ((fdo_flags & FDO_HOR) && KeyTyped)
7257 foldOpenCursor();
7258#endif
7259 undisplay_dollar();
7260 tpos = curwin->w_cursor;
7261 if (c == K_C_END)
7262 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7263 coladvance((colnr_T)MAXCOL);
7264 curwin->w_curswant = MAXCOL;
7265
7266 start_arrow(&tpos);
7267}
7268
7269 static void
7270ins_s_left()
7271{
7272#ifdef FEAT_FOLDING
7273 if ((fdo_flags & FDO_HOR) && KeyTyped)
7274 foldOpenCursor();
7275#endif
7276 undisplay_dollar();
7277 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
7278 {
7279 start_arrow(&curwin->w_cursor);
7280 (void)bck_word(1L, FALSE, FALSE);
7281 curwin->w_set_curswant = TRUE;
7282 }
7283 else
7284 vim_beep();
7285}
7286
7287 static void
7288ins_right()
7289{
7290#ifdef FEAT_FOLDING
7291 if ((fdo_flags & FDO_HOR) && KeyTyped)
7292 foldOpenCursor();
7293#endif
7294 undisplay_dollar();
7295 if (gchar_cursor() != NUL || virtual_active()
7296 )
7297 {
7298 start_arrow(&curwin->w_cursor);
7299 curwin->w_set_curswant = TRUE;
7300#ifdef FEAT_VIRTUALEDIT
7301 if (virtual_active())
7302 oneright();
7303 else
7304#endif
7305 {
7306#ifdef FEAT_MBYTE
7307 if (has_mbyte)
7308 curwin->w_cursor.col += (*mb_ptr2len_check)(ml_get_cursor());
7309 else
7310#endif
7311 ++curwin->w_cursor.col;
7312 }
7313
7314#ifdef FEAT_RIGHTLEFT
7315 revins_legal++;
7316 if (revins_chars)
7317 revins_chars--;
7318#endif
7319 }
7320 /* if 'whichwrap' set for cursor in insert mode, may move the
7321 * cursor to the next line */
7322 else if (vim_strchr(p_ww, ']') != NULL
7323 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7324 {
7325 start_arrow(&curwin->w_cursor);
7326 curwin->w_set_curswant = TRUE;
7327 ++curwin->w_cursor.lnum;
7328 curwin->w_cursor.col = 0;
7329 }
7330 else
7331 vim_beep();
7332}
7333
7334 static void
7335ins_s_right()
7336{
7337#ifdef FEAT_FOLDING
7338 if ((fdo_flags & FDO_HOR) && KeyTyped)
7339 foldOpenCursor();
7340#endif
7341 undisplay_dollar();
7342 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
7343 || gchar_cursor() != NUL)
7344 {
7345 start_arrow(&curwin->w_cursor);
7346 (void)fwd_word(1L, FALSE, 0);
7347 curwin->w_set_curswant = TRUE;
7348 }
7349 else
7350 vim_beep();
7351}
7352
7353 static void
7354ins_up(startcol)
7355 int startcol; /* when TRUE move to Insstart.col */
7356{
7357 pos_T tpos;
7358 linenr_T old_topline = curwin->w_topline;
7359#ifdef FEAT_DIFF
7360 int old_topfill = curwin->w_topfill;
7361#endif
7362
7363 undisplay_dollar();
7364 tpos = curwin->w_cursor;
7365 if (cursor_up(1L, TRUE) == OK)
7366 {
7367 if (startcol)
7368 coladvance(getvcol_nolist(&Insstart));
7369 if (old_topline != curwin->w_topline
7370#ifdef FEAT_DIFF
7371 || old_topfill != curwin->w_topfill
7372#endif
7373 )
7374 redraw_later(VALID);
7375 start_arrow(&tpos);
7376#ifdef FEAT_CINDENT
7377 can_cindent = TRUE;
7378#endif
7379 }
7380 else
7381 vim_beep();
7382}
7383
7384 static void
7385ins_pageup()
7386{
7387 pos_T tpos;
7388
7389 undisplay_dollar();
7390 tpos = curwin->w_cursor;
7391 if (onepage(BACKWARD, 1L) == OK)
7392 {
7393 start_arrow(&tpos);
7394#ifdef FEAT_CINDENT
7395 can_cindent = TRUE;
7396#endif
7397 }
7398 else
7399 vim_beep();
7400}
7401
7402 static void
7403ins_down(startcol)
7404 int startcol; /* when TRUE move to Insstart.col */
7405{
7406 pos_T tpos;
7407 linenr_T old_topline = curwin->w_topline;
7408#ifdef FEAT_DIFF
7409 int old_topfill = curwin->w_topfill;
7410#endif
7411
7412 undisplay_dollar();
7413 tpos = curwin->w_cursor;
7414 if (cursor_down(1L, TRUE) == OK)
7415 {
7416 if (startcol)
7417 coladvance(getvcol_nolist(&Insstart));
7418 if (old_topline != curwin->w_topline
7419#ifdef FEAT_DIFF
7420 || old_topfill != curwin->w_topfill
7421#endif
7422 )
7423 redraw_later(VALID);
7424 start_arrow(&tpos);
7425#ifdef FEAT_CINDENT
7426 can_cindent = TRUE;
7427#endif
7428 }
7429 else
7430 vim_beep();
7431}
7432
7433 static void
7434ins_pagedown()
7435{
7436 pos_T tpos;
7437
7438 undisplay_dollar();
7439 tpos = curwin->w_cursor;
7440 if (onepage(FORWARD, 1L) == OK)
7441 {
7442 start_arrow(&tpos);
7443#ifdef FEAT_CINDENT
7444 can_cindent = TRUE;
7445#endif
7446 }
7447 else
7448 vim_beep();
7449}
7450
7451#ifdef FEAT_DND
7452 static void
7453ins_drop()
7454{
7455 do_put('~', BACKWARD, 1L, PUT_CURSEND);
7456}
7457#endif
7458
7459/*
7460 * Handle TAB in Insert or Replace mode.
7461 * Return TRUE when the TAB needs to be inserted like a normal character.
7462 */
7463 static int
7464ins_tab()
7465{
7466 int ind;
7467 int i;
7468 int temp;
7469
7470 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
7471 Insstart_blank_vcol = get_nolist_virtcol();
7472 if (echeck_abbr(TAB + ABBR_OFF))
7473 return FALSE;
7474
7475 ind = inindent(0);
7476#ifdef FEAT_CINDENT
7477 if (ind)
7478 can_cindent = FALSE;
7479#endif
7480
7481 /*
7482 * When nothing special, insert TAB like a normal character
7483 */
7484 if (!curbuf->b_p_et
7485 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
7486 && curbuf->b_p_sts == 0)
7487 return TRUE;
7488
7489 if (stop_arrow() == FAIL)
7490 return TRUE;
7491
7492 did_ai = FALSE;
7493#ifdef FEAT_SMARTINDENT
7494 did_si = FALSE;
7495 can_si = FALSE;
7496 can_si_back = FALSE;
7497#endif
7498 AppendToRedobuff((char_u *)"\t");
7499
7500 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
7501 temp = (int)curbuf->b_p_sw;
7502 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
7503 temp = (int)curbuf->b_p_sts;
7504 else /* otherwise use 'tabstop' */
7505 temp = (int)curbuf->b_p_ts;
7506 temp -= get_nolist_virtcol() % temp;
7507
7508 /*
7509 * Insert the first space with ins_char(). It will delete one char in
7510 * replace mode. Insert the rest with ins_str(); it will not delete any
7511 * chars. For VREPLACE mode, we use ins_char() for all characters.
7512 */
7513 ins_char(' ');
7514 while (--temp > 0)
7515 {
7516#ifdef FEAT_VREPLACE
7517 if (State & VREPLACE_FLAG)
7518 ins_char(' ');
7519 else
7520#endif
7521 {
7522 ins_str((char_u *)" ");
7523 if (State & REPLACE_FLAG) /* no char replaced */
7524 replace_push(NUL);
7525 }
7526 }
7527
7528 /*
7529 * When 'expandtab' not set: Replace spaces by TABs where possible.
7530 */
7531 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
7532 {
7533 char_u *ptr;
7534#ifdef FEAT_VREPLACE
7535 char_u *saved_line = NULL; /* init for GCC */
7536 pos_T pos;
7537#endif
7538 pos_T fpos;
7539 pos_T *cursor;
7540 colnr_T want_vcol, vcol;
7541 int change_col = -1;
7542 int save_list = curwin->w_p_list;
7543
7544 /*
7545 * Get the current line. For VREPLACE mode, don't make real changes
7546 * yet, just work on a copy of the line.
7547 */
7548#ifdef FEAT_VREPLACE
7549 if (State & VREPLACE_FLAG)
7550 {
7551 pos = curwin->w_cursor;
7552 cursor = &pos;
7553 saved_line = vim_strsave(ml_get_curline());
7554 if (saved_line == NULL)
7555 return FALSE;
7556 ptr = saved_line + pos.col;
7557 }
7558 else
7559#endif
7560 {
7561 ptr = ml_get_cursor();
7562 cursor = &curwin->w_cursor;
7563 }
7564
7565 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
7566 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
7567 curwin->w_p_list = FALSE;
7568
7569 /* Find first white before the cursor */
7570 fpos = curwin->w_cursor;
7571 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
7572 {
7573 --fpos.col;
7574 --ptr;
7575 }
7576
7577 /* In Replace mode, don't change characters before the insert point. */
7578 if ((State & REPLACE_FLAG)
7579 && fpos.lnum == Insstart.lnum
7580 && fpos.col < Insstart.col)
7581 {
7582 ptr += Insstart.col - fpos.col;
7583 fpos.col = Insstart.col;
7584 }
7585
7586 /* compute virtual column numbers of first white and cursor */
7587 getvcol(curwin, &fpos, &vcol, NULL, NULL);
7588 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
7589
7590 /* Use as many TABs as possible. Beware of 'showbreak' and
7591 * 'linebreak' adding extra virtual columns. */
7592 while (vim_iswhite(*ptr))
7593 {
7594 i = lbr_chartabsize((char_u *)"\t", vcol);
7595 if (vcol + i > want_vcol)
7596 break;
7597 if (*ptr != TAB)
7598 {
7599 *ptr = TAB;
7600 if (change_col < 0)
7601 {
7602 change_col = fpos.col; /* Column of first change */
7603 /* May have to adjust Insstart */
7604 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
7605 Insstart.col = fpos.col;
7606 }
7607 }
7608 ++fpos.col;
7609 ++ptr;
7610 vcol += i;
7611 }
7612
7613 if (change_col >= 0)
7614 {
7615 int repl_off = 0;
7616
7617 /* Skip over the spaces we need. */
7618 while (vcol < want_vcol && *ptr == ' ')
7619 {
7620 vcol += lbr_chartabsize(ptr, vcol);
7621 ++ptr;
7622 ++repl_off;
7623 }
7624 if (vcol > want_vcol)
7625 {
7626 /* Must have a char with 'showbreak' just before it. */
7627 --ptr;
7628 --repl_off;
7629 }
7630 fpos.col += repl_off;
7631
7632 /* Delete following spaces. */
7633 i = cursor->col - fpos.col;
7634 if (i > 0)
7635 {
7636 mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1);
7637 /* correct replace stack. */
7638 if ((State & REPLACE_FLAG)
7639#ifdef FEAT_VREPLACE
7640 && !(State & VREPLACE_FLAG)
7641#endif
7642 )
7643 for (temp = i; --temp >= 0; )
7644 replace_join(repl_off);
7645 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00007646#ifdef FEAT_NETBEANS_INTG
7647 if (usingNetbeans)
7648 {
7649 netbeans_removed(curbuf, fpos.lnum, cursor->col,
7650 (long)(i + 1));
7651 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
7652 (char_u *)"\t", 1);
7653 }
7654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 cursor->col -= i;
7656
7657#ifdef FEAT_VREPLACE
7658 /*
7659 * In VREPLACE mode, we haven't changed anything yet. Do it now by
7660 * backspacing over the changed spacing and then inserting the new
7661 * spacing.
7662 */
7663 if (State & VREPLACE_FLAG)
7664 {
7665 /* Backspace from real cursor to change_col */
7666 backspace_until_column(change_col);
7667
7668 /* Insert each char in saved_line from changed_col to
7669 * ptr-cursor */
7670 ins_bytes_len(saved_line + change_col,
7671 cursor->col - change_col);
7672 }
7673#endif
7674 }
7675
7676#ifdef FEAT_VREPLACE
7677 if (State & VREPLACE_FLAG)
7678 vim_free(saved_line);
7679#endif
7680 curwin->w_p_list = save_list;
7681 }
7682
7683 return FALSE;
7684}
7685
7686/*
7687 * Handle CR or NL in insert mode.
7688 * Return TRUE when out of memory or can't undo.
7689 */
7690 static int
7691ins_eol(c)
7692 int c;
7693{
7694 int i;
7695
7696 if (echeck_abbr(c + ABBR_OFF))
7697 return FALSE;
7698 if (stop_arrow() == FAIL)
7699 return TRUE;
7700 undisplay_dollar();
7701
7702 /*
7703 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
7704 * character under the cursor. Only push a NUL on the replace stack,
7705 * nothing to put back when the NL is deleted.
7706 */
7707 if ((State & REPLACE_FLAG)
7708#ifdef FEAT_VREPLACE
7709 && !(State & VREPLACE_FLAG)
7710#endif
7711 )
7712 replace_push(NUL);
7713
7714 /*
7715 * In VREPLACE mode, a NL replaces the rest of the line, and starts
7716 * replacing the next line, so we push all of the characters left on the
7717 * line onto the replace stack. This is not done here though, it is done
7718 * in open_line().
7719 */
7720
7721#ifdef FEAT_RIGHTLEFT
7722# ifdef FEAT_FKMAP
7723 if (p_altkeymap && p_fkmap)
7724 fkmap(NL);
7725# endif
7726 /* NL in reverse insert will always start in the end of
7727 * current line. */
7728 if (revins_on)
7729 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
7730#endif
7731
7732 AppendToRedobuff(NL_STR);
7733 i = open_line(FORWARD,
7734#ifdef FEAT_COMMENTS
7735 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
7736#endif
7737 0, old_indent);
7738 old_indent = 0;
7739#ifdef FEAT_CINDENT
7740 can_cindent = TRUE;
7741#endif
7742
7743 return (!i);
7744}
7745
7746#ifdef FEAT_DIGRAPHS
7747/*
7748 * Handle digraph in insert mode.
7749 * Returns character still to be inserted, or NUL when nothing remaining to be
7750 * done.
7751 */
7752 static int
7753ins_digraph()
7754{
7755 int c;
7756 int cc;
7757
7758 pc_status = PC_STATUS_UNSET;
7759 if (redrawing() && !char_avail())
7760 {
7761 /* may need to redraw when no more chars available now */
7762 ins_redraw();
7763
7764 edit_putchar('?', TRUE);
7765#ifdef FEAT_CMDL_INFO
7766 add_to_showcmd_c(Ctrl_K);
7767#endif
7768 }
7769
7770#ifdef USE_ON_FLY_SCROLL
7771 dont_scroll = TRUE; /* disallow scrolling here */
7772#endif
7773
7774 /* don't map the digraph chars. This also prevents the
7775 * mode message to be deleted when ESC is hit */
7776 ++no_mapping;
7777 ++allow_keys;
7778 c = safe_vgetc();
7779 --no_mapping;
7780 --allow_keys;
7781 if (IS_SPECIAL(c) || mod_mask) /* special key */
7782 {
7783#ifdef FEAT_CMDL_INFO
7784 clear_showcmd();
7785#endif
7786 insert_special(c, TRUE, FALSE);
7787 return NUL;
7788 }
7789 if (c != ESC)
7790 {
7791 if (redrawing() && !char_avail())
7792 {
7793 /* may need to redraw when no more chars available now */
7794 ins_redraw();
7795
7796 if (char2cells(c) == 1)
7797 {
7798 /* first remove the '?', otherwise it's restored when typing
7799 * an ESC next */
7800 edit_unputchar();
7801 ins_redraw();
7802 edit_putchar(c, TRUE);
7803 }
7804#ifdef FEAT_CMDL_INFO
7805 add_to_showcmd_c(c);
7806#endif
7807 }
7808 ++no_mapping;
7809 ++allow_keys;
7810 cc = safe_vgetc();
7811 --no_mapping;
7812 --allow_keys;
7813 if (cc != ESC)
7814 {
7815 AppendToRedobuff((char_u *)CTRL_V_STR);
7816 c = getdigraph(c, cc, TRUE);
7817#ifdef FEAT_CMDL_INFO
7818 clear_showcmd();
7819#endif
7820 return c;
7821 }
7822 }
7823 edit_unputchar();
7824#ifdef FEAT_CMDL_INFO
7825 clear_showcmd();
7826#endif
7827 return NUL;
7828}
7829#endif
7830
7831/*
7832 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
7833 * Returns the char to be inserted, or NUL if none found.
7834 */
7835 static int
7836ins_copychar(lnum)
7837 linenr_T lnum;
7838{
7839 int c;
7840 int temp;
7841 char_u *ptr, *prev_ptr;
7842
7843 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
7844 {
7845 vim_beep();
7846 return NUL;
7847 }
7848
7849 /* try to advance to the cursor column */
7850 temp = 0;
7851 ptr = ml_get(lnum);
7852 prev_ptr = ptr;
7853 validate_virtcol();
7854 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
7855 {
7856 prev_ptr = ptr;
7857 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
7858 }
7859 if ((colnr_T)temp > curwin->w_virtcol)
7860 ptr = prev_ptr;
7861
7862#ifdef FEAT_MBYTE
7863 c = (*mb_ptr2char)(ptr);
7864#else
7865 c = *ptr;
7866#endif
7867 if (c == NUL)
7868 vim_beep();
7869 return c;
7870}
7871
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007872/*
7873 * CTRL-Y or CTRL-E typed in Insert mode.
7874 */
7875 static int
7876ins_ctrl_ey(tc)
7877 int tc;
7878{
7879 int c = tc;
7880
7881#ifdef FEAT_INS_EXPAND
7882 if (ctrl_x_mode == CTRL_X_SCROLL)
7883 {
7884 if (c == Ctrl_Y)
7885 scrolldown_clamp();
7886 else
7887 scrollup_clamp();
7888 redraw_later(VALID);
7889 }
7890 else
7891#endif
7892 {
7893 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
7894 if (c != NUL)
7895 {
7896 long tw_save;
7897
7898 /* The character must be taken literally, insert like it
7899 * was typed after a CTRL-V, and pretend 'textwidth'
7900 * wasn't set. Digits, 'o' and 'x' are special after a
7901 * CTRL-V, don't use it for these. */
7902 if (c < 256 && !isalnum(c))
7903 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
7904 tw_save = curbuf->b_p_tw;
7905 curbuf->b_p_tw = -1;
7906 insert_special(c, TRUE, FALSE);
7907 curbuf->b_p_tw = tw_save;
7908#ifdef FEAT_RIGHTLEFT
7909 revins_chars++;
7910 revins_legal++;
7911#endif
7912 c = Ctrl_V; /* pretend CTRL-V is last character */
7913 auto_format(FALSE, TRUE);
7914 }
7915 }
7916 return c;
7917}
7918
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919#ifdef FEAT_SMARTINDENT
7920/*
7921 * Try to do some very smart auto-indenting.
7922 * Used when inserting a "normal" character.
7923 */
7924 static void
7925ins_try_si(c)
7926 int c;
7927{
7928 pos_T *pos, old_pos;
7929 char_u *ptr;
7930 int i;
7931 int temp;
7932
7933 /*
7934 * do some very smart indenting when entering '{' or '}'
7935 */
7936 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
7937 {
7938 /*
7939 * for '}' set indent equal to indent of line containing matching '{'
7940 */
7941 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
7942 {
7943 old_pos = curwin->w_cursor;
7944 /*
7945 * If the matching '{' has a ')' immediately before it (ignoring
7946 * white-space), then line up with the start of the line
7947 * containing the matching '(' if there is one. This handles the
7948 * case where an "if (..\n..) {" statement continues over multiple
7949 * lines -- webb
7950 */
7951 ptr = ml_get(pos->lnum);
7952 i = pos->col;
7953 if (i > 0) /* skip blanks before '{' */
7954 while (--i > 0 && vim_iswhite(ptr[i]))
7955 ;
7956 curwin->w_cursor.lnum = pos->lnum;
7957 curwin->w_cursor.col = i;
7958 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
7959 curwin->w_cursor = *pos;
7960 i = get_indent();
7961 curwin->w_cursor = old_pos;
7962#ifdef FEAT_VREPLACE
7963 if (State & VREPLACE_FLAG)
7964 change_indent(INDENT_SET, i, FALSE, NUL);
7965 else
7966#endif
7967 (void)set_indent(i, SIN_CHANGED);
7968 }
7969 else if (curwin->w_cursor.col > 0)
7970 {
7971 /*
7972 * when inserting '{' after "O" reduce indent, but not
7973 * more than indent of previous line
7974 */
7975 temp = TRUE;
7976 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
7977 {
7978 old_pos = curwin->w_cursor;
7979 i = get_indent();
7980 while (curwin->w_cursor.lnum > 1)
7981 {
7982 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
7983
7984 /* ignore empty lines and lines starting with '#'. */
7985 if (*ptr != '#' && *ptr != NUL)
7986 break;
7987 }
7988 if (get_indent() >= i)
7989 temp = FALSE;
7990 curwin->w_cursor = old_pos;
7991 }
7992 if (temp)
7993 shift_line(TRUE, FALSE, 1);
7994 }
7995 }
7996
7997 /*
7998 * set indent of '#' always to 0
7999 */
8000 if (curwin->w_cursor.col > 0 && can_si && c == '#')
8001 {
8002 /* remember current indent for next line */
8003 old_indent = get_indent();
8004 (void)set_indent(0, SIN_CHANGED);
8005 }
8006
8007 /* Adjust ai_col, the char at this position can be deleted. */
8008 if (ai_col > curwin->w_cursor.col)
8009 ai_col = curwin->w_cursor.col;
8010}
8011#endif
8012
8013/*
8014 * Get the value that w_virtcol would have when 'list' is off.
8015 * Unless 'cpo' contains the 'L' flag.
8016 */
8017 static colnr_T
8018get_nolist_virtcol()
8019{
8020 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
8021 return getvcol_nolist(&curwin->w_cursor);
8022 validate_virtcol();
8023 return curwin->w_virtcol;
8024}