Mercurial > vim
annotate src/edit.c @ 6526:5b8d26b85437 v7.4.590
updated for version 7.4.590
Problem: Using ctrl_x_mode as if it contains flags.
Solution: Don't use AND with CTRL_X_OMNI. (Hirohito Higashi)
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Thu, 22 Jan 2015 22:40:20 +0100 |
parents | e6c5ff35500d |
children | 347747485343 |
rev | line source |
---|---|
7 | 1 /* 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 | |
12 | 33 #define CTRL_X_FUNCTION 12 |
523 | 34 #define CTRL_X_OMNI 13 |
477 | 35 #define CTRL_X_SPELL 14 |
36 #define CTRL_X_LOCAL_MSG 15 /* only used in "ctrl_x_msgs" */ | |
7 | 37 |
38 #define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT] | |
39 | |
40 static char *ctrl_x_msgs[] = | |
41 { | |
42 N_(" Keyword completion (^N^P)"), /* ctrl_x_mode == 0, ^P/^N compl. */ | |
819 | 43 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"), |
449 | 44 NULL, |
7 | 45 N_(" Whole line completion (^L^N^P)"), |
46 N_(" File name completion (^F^N^P)"), | |
47 N_(" Tag completion (^]^N^P)"), | |
48 N_(" Path pattern completion (^N^P)"), | |
49 N_(" Definition completion (^D^N^P)"), | |
50 NULL, | |
51 N_(" Dictionary completion (^K^N^P)"), | |
52 N_(" Thesaurus completion (^T^N^P)"), | |
12 | 53 N_(" Command-line completion (^V^N^P)"), |
54 N_(" User defined completion (^U^N^P)"), | |
523 | 55 N_(" Omni completion (^O^N^P)"), |
819 | 56 N_(" Spelling suggestion (s^N^P)"), |
449 | 57 N_(" Keyword Local completion (^N^P)"), |
7 | 58 }; |
59 | |
1869 | 60 static char e_hitend[] = N_("Hit end of paragraph"); |
2631 | 61 #ifdef FEAT_COMPL_FUNC |
62 static char e_complwin[] = N_("E839: Completion function changed window"); | |
63 static char e_compldel[] = N_("E840: Completion function deleted text"); | |
64 #endif | |
7 | 65 |
66 /* | |
67 * Structure used to store one match for insert completion. | |
68 */ | |
681 | 69 typedef struct compl_S compl_T; |
70 struct compl_S | |
7 | 71 { |
464 | 72 compl_T *cp_next; |
73 compl_T *cp_prev; | |
74 char_u *cp_str; /* matched text */ | |
681 | 75 char cp_icase; /* TRUE or FALSE: ignore case */ |
786 | 76 char_u *(cp_text[CPT_COUNT]); /* text for the menu */ |
659 | 77 char_u *cp_fname; /* file containing the match, allocated when |
78 * cp_flags has FREE_FNAME */ | |
464 | 79 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */ |
80 int cp_number; /* sequence number */ | |
7 | 81 }; |
82 | |
464 | 83 #define ORIGINAL_TEXT (1) /* the original text when the expansion begun */ |
7 | 84 #define FREE_FNAME (2) |
85 | |
86 /* | |
87 * All the current matches are stored in a list. | |
449 | 88 * "compl_first_match" points to the start of the list. |
89 * "compl_curr_match" points to the currently selected entry. | |
90 * "compl_shown_match" is different from compl_curr_match during | |
91 * ins_compl_get_exp(). | |
7 | 92 */ |
464 | 93 static compl_T *compl_first_match = NULL; |
94 static compl_T *compl_curr_match = NULL; | |
95 static compl_T *compl_shown_match = NULL; | |
449 | 96 |
825 | 97 /* After using a cursor key <Enter> selects a match in the popup menu, |
98 * otherwise it inserts a line break. */ | |
99 static int compl_enter_selects = FALSE; | |
100 | |
657 | 101 /* When "compl_leader" is not NULL only matches that start with this string |
102 * are used. */ | |
103 static char_u *compl_leader = NULL; | |
104 | |
665 | 105 static int compl_get_longest = FALSE; /* put longest common string |
106 in compl_leader */ | |
107 | |
657 | 108 static int compl_used_match; /* Selected one of the matches. When |
109 FALSE the match was edited or using | |
110 the longest common string. */ | |
111 | |
874 | 112 static int compl_was_interrupted = FALSE; /* didn't finish finding |
113 completions. */ | |
114 | |
115 static int compl_restarting = FALSE; /* don't insert match */ | |
116 | |
449 | 117 /* When the first completion is done "compl_started" is set. When it's |
118 * FALSE the word to be completed must be located. */ | |
644 | 119 static int compl_started = FALSE; |
449 | 120 |
1927 | 121 /* Set when doing something for completion that may call edit() recursively, |
122 * which is not allowed. */ | |
123 static int compl_busy = FALSE; | |
124 | |
464 | 125 static int compl_matches = 0; |
126 static char_u *compl_pattern = NULL; | |
127 static int compl_direction = FORWARD; | |
128 static int compl_shows_dir = FORWARD; | |
716 | 129 static int compl_pending = 0; /* > 1 for postponed CTRL-N */ |
464 | 130 static pos_T compl_startpos; |
131 static colnr_T compl_col = 0; /* column where the text starts | |
132 * that is being completed */ | |
133 static char_u *compl_orig_text = NULL; /* text as it was before | |
134 * completion started */ | |
135 static int compl_cont_mode = 0; | |
136 static expand_T compl_xp; | |
449 | 137 |
3078 | 138 static int compl_opt_refresh_always = FALSE; |
139 | |
449 | 140 static void ins_ctrl_x __ARGS((void)); |
141 static int has_compl_option __ARGS((int dict_opt)); | |
1430 | 142 static int ins_compl_accept_char __ARGS((int c)); |
944 | 143 static int ins_compl_add __ARGS((char_u *str, int len, int icase, char_u *fname, char_u **cptext, int cdir, int flags, int adup)); |
681 | 144 static int ins_compl_equal __ARGS((compl_T *match, char_u *str, int len)); |
665 | 145 static void ins_compl_longest_match __ARGS((compl_T *match)); |
681 | 146 static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches, int icase)); |
7 | 147 static int ins_compl_make_cyclic __ARGS((void)); |
540 | 148 static void ins_compl_upd_pum __ARGS((void)); |
149 static void ins_compl_del_pum __ARGS((void)); | |
648 | 150 static int pum_wanted __ARGS((void)); |
707 | 151 static int pum_enough_matches __ARGS((void)); |
659 | 152 static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int flags, int thesaurus)); |
703 | 153 static void ins_compl_files __ARGS((int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir)); |
667 | 154 static char_u *find_line_end __ARGS((char_u *ptr)); |
7 | 155 static void ins_compl_free __ARGS((void)); |
156 static void ins_compl_clear __ARGS((void)); | |
657 | 157 static int ins_compl_bs __ARGS((void)); |
3078 | 158 static int ins_compl_need_restart __ARGS((void)); |
874 | 159 static void ins_compl_new_leader __ARGS((void)); |
657 | 160 static void ins_compl_addleader __ARGS((int c)); |
3078 | 161 static int ins_compl_len __ARGS((void)); |
874 | 162 static void ins_compl_restart __ARGS((void)); |
694 | 163 static void ins_compl_set_original_text __ARGS((char_u *str)); |
659 | 164 static void ins_compl_addfrommatch __ARGS((void)); |
540 | 165 static int ins_compl_prep __ARGS((int c)); |
3095 | 166 static void ins_compl_fixRedoBufForLeader __ARGS((char_u *ptr_arg)); |
7 | 167 static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag)); |
724 | 168 #if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) |
169 static void ins_compl_add_list __ARGS((list_T *list)); | |
3078 | 170 static void ins_compl_add_dict __ARGS((dict_T *dict)); |
724 | 171 #endif |
659 | 172 static int ins_compl_get_exp __ARGS((pos_T *ini)); |
7 | 173 static void ins_compl_delete __ARGS((void)); |
174 static void ins_compl_insert __ARGS((void)); | |
665 | 175 static int ins_compl_next __ARGS((int allow_get_expansion, int count, int insert_match)); |
610 | 176 static int ins_compl_key2dir __ARGS((int c)); |
644 | 177 static int ins_compl_pum_key __ARGS((int c)); |
610 | 178 static int ins_compl_key2count __ARGS((int c)); |
681 | 179 static int ins_compl_use_match __ARGS((int c)); |
7 | 180 static int ins_complete __ARGS((int c)); |
1872 | 181 static unsigned quote_meta __ARGS((char_u *dest, char_u *str, int len)); |
7 | 182 #endif /* FEAT_INS_EXPAND */ |
183 | |
184 #define BACKSPACE_CHAR 1 | |
185 #define BACKSPACE_WORD 2 | |
186 #define BACKSPACE_WORD_NOT_SPACE 3 | |
187 #define BACKSPACE_LINE 4 | |
188 | |
661 | 189 static void ins_redraw __ARGS((int ready)); |
7 | 190 static void ins_ctrl_v __ARGS((void)); |
191 static void undisplay_dollar __ARGS((void)); | |
192 static void insert_special __ARGS((int, int, int)); | |
2004 | 193 static void internal_format __ARGS((int textwidth, int second_indent, int flags, int format_only, int c)); |
7 | 194 static void check_auto_format __ARGS((int)); |
195 static void redo_literal __ARGS((int c)); | |
196 static void start_arrow __ARGS((pos_T *end_insert_pos)); | |
744 | 197 #ifdef FEAT_SPELL |
221 | 198 static void check_spell_redraw __ARGS((void)); |
484 | 199 static void spell_back_to_badword __ARGS((void)); |
497 | 200 static int spell_bad_len = 0; /* length of located bad word */ |
221 | 201 #endif |
5434 | 202 static void stop_insert __ARGS((pos_T *end_insert_pos, int esc, int nomove)); |
7 | 203 static int echeck_abbr __ARGS((int)); |
204 static int replace_pop __ARGS((void)); | |
205 static void replace_join __ARGS((int off)); | |
206 static void replace_pop_ins __ARGS((void)); | |
207 #ifdef FEAT_MBYTE | |
208 static void mb_replace_pop_ins __ARGS((int cc)); | |
209 #endif | |
210 static void replace_flush __ARGS((void)); | |
1782 | 211 static void replace_do_bs __ARGS((int limit_col)); |
212 static int del_char_after_col __ARGS((int limit_col)); | |
7 | 213 #ifdef FEAT_CINDENT |
214 static int cindent_on __ARGS((void)); | |
215 #endif | |
216 static void ins_reg __ARGS((void)); | |
217 static void ins_ctrl_g __ARGS((void)); | |
449 | 218 static void ins_ctrl_hat __ARGS((void)); |
477 | 219 static int ins_esc __ARGS((long *count, int cmdchar, int nomove)); |
7 | 220 #ifdef FEAT_RIGHTLEFT |
221 static void ins_ctrl_ __ARGS((void)); | |
222 #endif | |
223 static int ins_start_select __ARGS((int c)); | |
449 | 224 static void ins_insert __ARGS((int replaceState)); |
225 static void ins_ctrl_o __ARGS((void)); | |
7 | 226 static void ins_shift __ARGS((int c, int lastc)); |
227 static void ins_del __ARGS((void)); | |
228 static int ins_bs __ARGS((int c, int mode, int *inserted_space_p)); | |
229 #ifdef FEAT_MOUSE | |
230 static void ins_mouse __ARGS((int c)); | |
2409
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2378
diff
changeset
|
231 static void ins_mousescroll __ARGS((int dir)); |
7 | 232 #endif |
692 | 233 #if defined(FEAT_GUI_TABLINE) || defined(PROTO) |
234 static void ins_tabline __ARGS((int c)); | |
235 #endif | |
7 | 236 static void ins_left __ARGS((void)); |
237 static void ins_home __ARGS((int c)); | |
238 static void ins_end __ARGS((int c)); | |
239 static void ins_s_left __ARGS((void)); | |
240 static void ins_right __ARGS((void)); | |
241 static void ins_s_right __ARGS((void)); | |
242 static void ins_up __ARGS((int startcol)); | |
243 static void ins_pageup __ARGS((void)); | |
244 static void ins_down __ARGS((int startcol)); | |
245 static void ins_pagedown __ARGS((void)); | |
246 #ifdef FEAT_DND | |
247 static void ins_drop __ARGS((void)); | |
248 #endif | |
249 static int ins_tab __ARGS((void)); | |
250 static int ins_eol __ARGS((int c)); | |
251 #ifdef FEAT_DIGRAPHS | |
252 static int ins_digraph __ARGS((void)); | |
253 #endif | |
449 | 254 static int ins_ctrl_ey __ARGS((int tc)); |
7 | 255 #ifdef FEAT_SMARTINDENT |
256 static void ins_try_si __ARGS((int c)); | |
257 #endif | |
258 static colnr_T get_nolist_virtcol __ARGS((void)); | |
3390 | 259 #ifdef FEAT_AUTOCMD |
260 static char_u *do_insert_char_pre __ARGS((int c)); | |
261 #endif | |
7 | 262 |
263 static colnr_T Insstart_textlen; /* length of line when insert started */ | |
264 static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */ | |
5680 | 265 static int update_Insstart_orig = TRUE; /* set Insstart_orig to Insstart */ |
7 | 266 |
267 static char_u *last_insert = NULL; /* the text of the previous insert, | |
268 K_SPECIAL and CSI are escaped */ | |
269 static int last_insert_skip; /* nr of chars in front of previous insert */ | |
270 static int new_insert_skip; /* nr of chars in front of current insert */ | |
603 | 271 static int did_restart_edit; /* "restart_edit" when calling edit() */ |
7 | 272 |
273 #ifdef FEAT_CINDENT | |
274 static int can_cindent; /* may do cindenting on this line */ | |
275 #endif | |
276 | |
277 static int old_indent = 0; /* for ^^D command in insert mode */ | |
278 | |
279 #ifdef FEAT_RIGHTLEFT | |
298 | 280 static int revins_on; /* reverse insert mode on */ |
281 static int revins_chars; /* how much to skip after edit */ | |
282 static int revins_legal; /* was the last char 'legal'? */ | |
283 static int revins_scol; /* start column of revins session */ | |
7 | 284 #endif |
285 | |
286 static int ins_need_undo; /* call u_save() before inserting a | |
287 char. Set when edit() is called. | |
288 after that arrow_used is used. */ | |
289 | |
290 static int did_add_space = FALSE; /* auto_format() added an extra space | |
291 under the cursor */ | |
292 | |
293 /* | |
294 * edit(): Start inserting text. | |
295 * | |
296 * "cmdchar" can be: | |
297 * 'i' normal insert command | |
298 * 'a' normal append command | |
299 * 'R' replace command | |
300 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo, | |
301 * but still only one <CR> is inserted. The <Esc> is not used for redo. | |
302 * 'g' "gI" command. | |
303 * 'V' "gR" command for Virtual Replace mode. | |
304 * 'v' "gr" command for single character Virtual Replace mode. | |
305 * | |
306 * This function is not called recursively. For CTRL-O commands, it returns | |
307 * and lets the caller handle the Normal-mode command. | |
308 * | |
309 * Return TRUE if a CTRL-O command caused the return (insert mode pending). | |
310 */ | |
311 int | |
312 edit(cmdchar, startln, count) | |
313 int cmdchar; | |
314 int startln; /* if set, insert at start of line */ | |
315 long count; | |
316 { | |
317 int c = 0; | |
318 char_u *ptr; | |
319 int lastc; | |
1869 | 320 int mincol; |
7 | 321 static linenr_T o_lnum = 0; |
322 int i; | |
323 int did_backspace = TRUE; /* previous char was backspace */ | |
324 #ifdef FEAT_CINDENT | |
325 int line_is_white = FALSE; /* line is empty before insert */ | |
326 #endif | |
327 linenr_T old_topline = 0; /* topline before insertion */ | |
328 #ifdef FEAT_DIFF | |
329 int old_topfill = -1; | |
330 #endif | |
331 int inserted_space = FALSE; /* just inserted a space */ | |
332 int replaceState = REPLACE; | |
477 | 333 int nomove = FALSE; /* don't move cursor on return */ |
7 | 334 |
603 | 335 /* Remember whether editing was restarted after CTRL-O. */ |
336 did_restart_edit = restart_edit; | |
337 | |
7 | 338 /* sleep before redrawing, needed for "CTRL-O :" that results in an |
339 * error message */ | |
340 check_for_delay(TRUE); | |
341 | |
5680 | 342 /* set Insstart_orig to Insstart */ |
343 update_Insstart_orig = TRUE; | |
344 | |
7 | 345 #ifdef HAVE_SANDBOX |
346 /* Don't allow inserting in the sandbox. */ | |
347 if (sandbox != 0) | |
348 { | |
349 EMSG(_(e_sandbox)); | |
350 return FALSE; | |
351 } | |
352 #endif | |
632 | 353 /* Don't allow changes in the buffer while editing the cmdline. The |
354 * caller of getcmdline() may get confused. */ | |
634 | 355 if (textlock != 0) |
632 | 356 { |
357 EMSG(_(e_secure)); | |
358 return FALSE; | |
359 } | |
7 | 360 |
361 #ifdef FEAT_INS_EXPAND | |
844 | 362 /* Don't allow recursive insert mode when busy with completion. */ |
1927 | 363 if (compl_started || compl_busy || pum_visible()) |
844 | 364 { |
365 EMSG(_(e_secure)); | |
366 return FALSE; | |
367 } | |
7 | 368 ins_compl_clear(); /* clear stuff for CTRL-X mode */ |
369 #endif | |
370 | |
11 | 371 #ifdef FEAT_AUTOCMD |
372 /* | |
373 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx". | |
374 */ | |
375 if (cmdchar != 'r' && cmdchar != 'v') | |
376 { | |
4027 | 377 pos_T save_cursor = curwin->w_cursor; |
378 | |
532 | 379 # ifdef FEAT_EVAL |
11 | 380 if (cmdchar == 'R') |
381 ptr = (char_u *)"r"; | |
382 else if (cmdchar == 'V') | |
383 ptr = (char_u *)"v"; | |
384 else | |
385 ptr = (char_u *)"i"; | |
386 set_vim_var_string(VV_INSERTMODE, ptr, 1); | |
4448 | 387 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */ |
532 | 388 # endif |
11 | 389 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf); |
4027 | 390 |
4448 | 391 /* Make sure the cursor didn't move. Do call check_cursor_col() in |
392 * case the text was modified. Since Insert mode was not started yet | |
393 * a call to check_cursor_col() may move the cursor, especially with | |
394 * the "A" command, thus set State to avoid that. Also check that the | |
395 * line number is still valid (lines may have been deleted). | |
396 * Do not restore if v:char was set to a non-empty string. */ | |
397 if (!equalpos(curwin->w_cursor, save_cursor) | |
398 # ifdef FEAT_EVAL | |
399 && *get_vim_var_str(VV_CHAR) == NUL | |
400 # endif | |
401 && save_cursor.lnum <= curbuf->b_ml.ml_line_count) | |
4027 | 402 { |
403 int save_state = State; | |
404 | |
405 curwin->w_cursor = save_cursor; | |
406 State = INSERT; | |
407 check_cursor_col(); | |
408 State = save_state; | |
409 } | |
11 | 410 } |
411 #endif | |
412 | |
2378
85b7dc8da5eb
Add the 'concealcursor' option to decide when the cursor line is to be
Bram Moolenaar <bram@vim.org>
parents:
2348
diff
changeset
|
413 #ifdef FEAT_CONCEAL |
85b7dc8da5eb
Add the 'concealcursor' option to decide when the cursor line is to be
Bram Moolenaar <bram@vim.org>
parents:
2348
diff
changeset
|
414 /* Check if the cursor line needs redrawing before changing State. If |
85b7dc8da5eb
Add the 'concealcursor' option to decide when the cursor line is to be
Bram Moolenaar <bram@vim.org>
parents:
2348
diff
changeset
|
415 * 'concealcursor' is "n" it needs to be redrawn without concealing. */ |
2428
33148c37f3c9
Changes for VMS. Mostly by Zoltan Arpadffy.
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
416 conceal_check_cursur_line(); |
2378
85b7dc8da5eb
Add the 'concealcursor' option to decide when the cursor line is to be
Bram Moolenaar <bram@vim.org>
parents:
2348
diff
changeset
|
417 #endif |
85b7dc8da5eb
Add the 'concealcursor' option to decide when the cursor line is to be
Bram Moolenaar <bram@vim.org>
parents:
2348
diff
changeset
|
418 |
7 | 419 #ifdef FEAT_MOUSE |
420 /* | |
421 * When doing a paste with the middle mouse button, Insstart is set to | |
422 * where the paste started. | |
423 */ | |
424 if (where_paste_started.lnum != 0) | |
425 Insstart = where_paste_started; | |
426 else | |
427 #endif | |
428 { | |
429 Insstart = curwin->w_cursor; | |
430 if (startln) | |
431 Insstart.col = 0; | |
432 } | |
1869 | 433 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline()); |
7 | 434 Insstart_blank_vcol = MAXCOL; |
435 if (!did_ai) | |
436 ai_col = 0; | |
437 | |
438 if (cmdchar != NUL && restart_edit == 0) | |
439 { | |
440 ResetRedobuff(); | |
441 AppendNumberToRedobuff(count); | |
442 #ifdef FEAT_VREPLACE | |
443 if (cmdchar == 'V' || cmdchar == 'v') | |
444 { | |
445 /* "gR" or "gr" command */ | |
446 AppendCharToRedobuff('g'); | |
447 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R'); | |
448 } | |
449 else | |
450 #endif | |
451 { | |
452 AppendCharToRedobuff(cmdchar); | |
453 if (cmdchar == 'g') /* "gI" command */ | |
454 AppendCharToRedobuff('I'); | |
455 else if (cmdchar == 'r') /* "r<CR>" command */ | |
456 count = 1; /* insert only one <CR> */ | |
457 } | |
458 } | |
459 | |
460 if (cmdchar == 'R') | |
461 { | |
462 #ifdef FEAT_FKMAP | |
463 if (p_fkmap && p_ri) | |
464 { | |
465 beep_flush(); | |
466 EMSG(farsi_text_3); /* encoded in Farsi */ | |
467 State = INSERT; | |
468 } | |
469 else | |
470 #endif | |
471 State = REPLACE; | |
472 } | |
473 #ifdef FEAT_VREPLACE | |
474 else if (cmdchar == 'V' || cmdchar == 'v') | |
475 { | |
476 State = VREPLACE; | |
477 replaceState = VREPLACE; | |
478 orig_line_count = curbuf->b_ml.ml_line_count; | |
479 vr_lines_changed = 1; | |
480 } | |
481 #endif | |
482 else | |
483 State = INSERT; | |
484 | |
485 stop_insert_mode = FALSE; | |
486 | |
487 /* | |
488 * Need to recompute the cursor position, it might move when the cursor is | |
489 * on a TAB or special character. | |
490 */ | |
491 curs_columns(TRUE); | |
492 | |
493 /* | |
494 * Enable langmap or IME, indicated by 'iminsert'. | |
495 * Note that IME may enabled/disabled without us noticing here, thus the | |
496 * 'iminsert' value may not reflect what is actually used. It is updated | |
497 * when hitting <Esc>. | |
498 */ | |
499 if (curbuf->b_p_iminsert == B_IMODE_LMAP) | |
500 State |= LANGMAP; | |
501 #ifdef USE_IM_CONTROL | |
502 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM); | |
503 #endif | |
504 | |
505 #ifdef FEAT_MOUSE | |
506 setmouse(); | |
507 #endif | |
508 #ifdef FEAT_CMDL_INFO | |
509 clear_showcmd(); | |
510 #endif | |
511 #ifdef FEAT_RIGHTLEFT | |
512 /* there is no reverse replace mode */ | |
513 revins_on = (State == INSERT && p_ri); | |
514 if (revins_on) | |
515 undisplay_dollar(); | |
516 revins_chars = 0; | |
517 revins_legal = 0; | |
518 revins_scol = -1; | |
519 #endif | |
520 | |
521 /* | |
522 * Handle restarting Insert mode. | |
523 * Don't do this for "CTRL-O ." (repeat an insert): we get here with | |
524 * restart_edit non-zero, and something in the stuff buffer. | |
525 */ | |
526 if (restart_edit != 0 && stuff_empty()) | |
527 { | |
528 #ifdef FEAT_MOUSE | |
529 /* | |
530 * After a paste we consider text typed to be part of the insert for | |
531 * the pasted text. You can backspace over the pasted text too. | |
532 */ | |
533 if (where_paste_started.lnum) | |
534 arrow_used = FALSE; | |
535 else | |
536 #endif | |
537 arrow_used = TRUE; | |
538 restart_edit = 0; | |
539 | |
540 /* | |
541 * If the cursor was after the end-of-line before the CTRL-O and it is | |
542 * now at the end-of-line, put it after the end-of-line (this is not | |
543 * correct in very rare cases). | |
544 * Also do this if curswant is greater than the current virtual | |
545 * column. Eg after "^O$" or "^O80|". | |
546 */ | |
547 validate_virtcol(); | |
548 update_curswant(); | |
230 | 549 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum) |
7 | 550 || curwin->w_curswant > curwin->w_virtcol) |
551 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL) | |
552 { | |
553 if (ptr[1] == NUL) | |
554 ++curwin->w_cursor.col; | |
555 #ifdef FEAT_MBYTE | |
556 else if (has_mbyte) | |
557 { | |
474 | 558 i = (*mb_ptr2len)(ptr); |
7 | 559 if (ptr[i] == NUL) |
560 curwin->w_cursor.col += i; | |
561 } | |
562 #endif | |
563 } | |
230 | 564 ins_at_eol = FALSE; |
7 | 565 } |
566 else | |
567 arrow_used = FALSE; | |
568 | |
569 /* we are in insert mode now, don't need to start it anymore */ | |
570 need_start_insertmode = FALSE; | |
571 | |
572 /* Need to save the line for undo before inserting the first char. */ | |
573 ins_need_undo = TRUE; | |
574 | |
575 #ifdef FEAT_MOUSE | |
576 where_paste_started.lnum = 0; | |
577 #endif | |
578 #ifdef FEAT_CINDENT | |
579 can_cindent = TRUE; | |
580 #endif | |
581 #ifdef FEAT_FOLDING | |
582 /* The cursor line is not in a closed fold, unless 'insertmode' is set or | |
583 * restarting. */ | |
584 if (!p_im && did_restart_edit == 0) | |
585 foldOpenCursor(); | |
586 #endif | |
587 | |
588 /* | |
589 * If 'showmode' is set, show the current (insert/replace/..) mode. | |
590 * A warning message for changing a readonly file is given here, before | |
591 * actually changing anything. It's put after the mode, if any. | |
592 */ | |
593 i = 0; | |
644 | 594 if (p_smd && msg_silent == 0) |
7 | 595 i = showmode(); |
596 | |
597 if (!p_im && did_restart_edit == 0) | |
1473 | 598 change_warning(i == 0 ? 0 : i + 1); |
7 | 599 |
600 #ifdef CURSOR_SHAPE | |
601 ui_cursor_shape(); /* may show different cursor shape */ | |
602 #endif | |
603 #ifdef FEAT_DIGRAPHS | |
604 do_digraph(-1); /* clear digraphs */ | |
605 #endif | |
606 | |
603 | 607 /* |
608 * Get the current length of the redo buffer, those characters have to be | |
609 * skipped if we want to get to the inserted characters. | |
610 */ | |
7 | 611 ptr = get_inserted(); |
612 if (ptr == NULL) | |
613 new_insert_skip = 0; | |
614 else | |
615 { | |
616 new_insert_skip = (int)STRLEN(ptr); | |
617 vim_free(ptr); | |
618 } | |
619 | |
620 old_indent = 0; | |
621 | |
622 /* | |
623 * Main loop in Insert mode: repeat until Insert mode is left. | |
624 */ | |
625 for (;;) | |
626 { | |
627 #ifdef FEAT_RIGHTLEFT | |
628 if (!revins_legal) | |
629 revins_scol = -1; /* reset on illegal motions */ | |
630 else | |
631 revins_legal = 0; | |
632 #endif | |
633 if (arrow_used) /* don't repeat insert when arrow key used */ | |
634 count = 0; | |
635 | |
5680 | 636 if (update_Insstart_orig) |
637 Insstart_orig = Insstart; | |
638 | |
7 | 639 if (stop_insert_mode) |
640 { | |
641 /* ":stopinsert" used or 'insertmode' reset */ | |
642 count = 0; | |
643 goto doESCkey; | |
644 } | |
645 | |
646 /* set curwin->w_curswant for next K_DOWN or K_UP */ | |
647 if (!arrow_used) | |
648 curwin->w_set_curswant = TRUE; | |
649 | |
650 /* If there is no typeahead may check for timestamps (e.g., for when a | |
651 * menu invoked a shell command). */ | |
652 if (stuff_empty()) | |
653 { | |
654 did_check_timestamps = FALSE; | |
655 if (need_check_timestamps) | |
656 check_timestamps(FALSE); | |
657 } | |
658 | |
659 /* | |
660 * When emsg() was called msg_scroll will have been set. | |
661 */ | |
662 msg_scroll = FALSE; | |
663 | |
664 #ifdef FEAT_GUI | |
665 /* When 'mousefocus' is set a mouse movement may have taken us to | |
666 * another window. "need_mouse_correct" may then be set because of an | |
667 * autocommand. */ | |
668 if (need_mouse_correct) | |
669 gui_mouse_correct(); | |
670 #endif | |
671 | |
672 #ifdef FEAT_FOLDING | |
673 /* Open fold at the cursor line, according to 'foldopen'. */ | |
674 if (fdo_flags & FDO_INSERT) | |
675 foldOpenCursor(); | |
676 /* Close folds where the cursor isn't, according to 'foldclose' */ | |
677 if (!char_avail()) | |
678 foldCheckClose(); | |
679 #endif | |
680 | |
681 /* | |
682 * If we inserted a character at the last position of the last line in | |
683 * the window, scroll the window one line up. This avoids an extra | |
684 * redraw. | |
685 * This is detected when the cursor column is smaller after inserting | |
686 * something. | |
687 * Don't do this when the topline changed already, it has | |
688 * already been adjusted (by insertchar() calling open_line())). | |
689 */ | |
690 if (curbuf->b_mod_set | |
691 && curwin->w_p_wrap | |
692 && !did_backspace | |
693 && curwin->w_topline == old_topline | |
694 #ifdef FEAT_DIFF | |
695 && curwin->w_topfill == old_topfill | |
696 #endif | |
697 ) | |
698 { | |
699 mincol = curwin->w_wcol; | |
700 validate_cursor_col(); | |
701 | |
1869 | 702 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts |
7 | 703 && curwin->w_wrow == W_WINROW(curwin) |
704 + curwin->w_height - 1 - p_so | |
705 && (curwin->w_cursor.lnum != curwin->w_topline | |
706 #ifdef FEAT_DIFF | |
707 || curwin->w_topfill > 0 | |
708 #endif | |
709 )) | |
710 { | |
711 #ifdef FEAT_DIFF | |
712 if (curwin->w_topfill > 0) | |
713 --curwin->w_topfill; | |
714 else | |
715 #endif | |
716 #ifdef FEAT_FOLDING | |
717 if (hasFolding(curwin->w_topline, NULL, &old_topline)) | |
718 set_topline(curwin, old_topline + 1); | |
719 else | |
720 #endif | |
721 set_topline(curwin, curwin->w_topline + 1); | |
722 } | |
723 } | |
724 | |
725 /* May need to adjust w_topline to show the cursor. */ | |
726 update_topline(); | |
727 | |
728 did_backspace = FALSE; | |
729 | |
730 validate_cursor(); /* may set must_redraw */ | |
731 | |
732 /* | |
733 * Redraw the display when no characters are waiting. | |
734 * Also shows mode, ruler and positions cursor. | |
735 */ | |
661 | 736 ins_redraw(TRUE); |
7 | 737 |
738 #ifdef FEAT_SCROLLBIND | |
739 if (curwin->w_p_scb) | |
740 do_check_scrollbind(TRUE); | |
741 #endif | |
742 | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2227
diff
changeset
|
743 #ifdef FEAT_CURSORBIND |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2227
diff
changeset
|
744 if (curwin->w_p_crb) |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2227
diff
changeset
|
745 do_check_cursorbind(); |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2227
diff
changeset
|
746 #endif |
7 | 747 update_curswant(); |
748 old_topline = curwin->w_topline; | |
749 #ifdef FEAT_DIFF | |
750 old_topfill = curwin->w_topfill; | |
751 #endif | |
752 | |
753 #ifdef USE_ON_FLY_SCROLL | |
754 dont_scroll = FALSE; /* allow scrolling here */ | |
755 #endif | |
756 | |
757 /* | |
1526 | 758 * Get a character for Insert mode. Ignore K_IGNORE. |
7 | 759 */ |
760 lastc = c; /* remember previous char for CTRL-D */ | |
1526 | 761 do |
762 { | |
763 c = safe_vgetc(); | |
764 } while (c == K_IGNORE); | |
7 | 765 |
978 | 766 #ifdef FEAT_AUTOCMD |
767 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */ | |
768 did_cursorhold = TRUE; | |
769 #endif | |
770 | |
7 | 771 #ifdef FEAT_RIGHTLEFT |
772 if (p_hkmap && KeyTyped) | |
773 c = hkmap(c); /* Hebrew mode mapping */ | |
774 #endif | |
775 #ifdef FEAT_FKMAP | |
776 if (p_fkmap && KeyTyped) | |
777 c = fkmap(c); /* Farsi mode mapping */ | |
778 #endif | |
779 | |
780 #ifdef FEAT_INS_EXPAND | |
659 | 781 /* |
782 * Special handling of keys while the popup menu is visible or wanted | |
897 | 783 * and the cursor is still in the completed word. Only when there is |
784 * a match, skip this when no matches were found. | |
659 | 785 */ |
897 | 786 if (compl_started |
787 && pum_wanted() | |
788 && curwin->w_cursor.col >= compl_col | |
789 && (compl_shown_match == NULL | |
790 || compl_shown_match != compl_shown_match->cp_next)) | |
659 | 791 { |
792 /* BS: Delete one character from "compl_leader". */ | |
793 if ((c == K_BS || c == Ctrl_H) | |
836 | 794 && curwin->w_cursor.col > compl_col |
795 && (c = ins_compl_bs()) == NUL) | |
657 | 796 continue; |
797 | |
659 | 798 /* When no match was selected or it was edited. */ |
799 if (!compl_used_match) | |
657 | 800 { |
659 | 801 /* CTRL-L: Add one character from the current match to |
836 | 802 * "compl_leader". Except when at the original match and |
803 * there is nothing to add, CTRL-L works like CTRL-P then. */ | |
804 if (c == Ctrl_L | |
805 && (ctrl_x_mode != CTRL_X_WHOLE_LINE | |
1872 | 806 || (int)STRLEN(compl_shown_match->cp_str) |
836 | 807 > curwin->w_cursor.col - compl_col)) |
659 | 808 { |
809 ins_compl_addfrommatch(); | |
810 continue; | |
811 } | |
812 | |
1430 | 813 /* A non-white character that fits in with the current |
814 * completion: Add to "compl_leader". */ | |
815 if (ins_compl_accept_char(c)) | |
659 | 816 { |
3390 | 817 #ifdef FEAT_AUTOCMD |
818 /* Trigger InsertCharPre. */ | |
819 char_u *str = do_insert_char_pre(c); | |
820 char_u *p; | |
821 | |
822 if (str != NULL) | |
823 { | |
824 for (p = str; *p != NUL; mb_ptr_adv(p)) | |
825 ins_compl_addleader(PTR2CHAR(p)); | |
826 vim_free(str); | |
827 } | |
828 else | |
829 #endif | |
830 ins_compl_addleader(c); | |
659 | 831 continue; |
832 } | |
665 | 833 |
887 | 834 /* Pressing CTRL-Y selects the current match. When |
825 | 835 * compl_enter_selects is set the Enter key does the same. */ |
836 if (c == Ctrl_Y || (compl_enter_selects | |
837 && (c == CAR || c == K_KENTER || c == NL))) | |
665 | 838 { |
839 ins_compl_delete(); | |
840 ins_compl_insert(); | |
841 } | |
657 | 842 } |
843 } | |
844 | |
7 | 845 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but |
846 * it does fix up the text when finishing completion. */ | |
665 | 847 compl_get_longest = FALSE; |
1526 | 848 if (ins_compl_prep(c)) |
657 | 849 continue; |
7 | 850 #endif |
851 | |
477 | 852 /* CTRL-\ CTRL-N goes to Normal mode, |
853 * CTRL-\ CTRL-G goes to mode selected with 'insertmode', | |
854 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */ | |
7 | 855 if (c == Ctrl_BSL) |
856 { | |
857 /* may need to redraw when no more chars available now */ | |
661 | 858 ins_redraw(FALSE); |
7 | 859 ++no_mapping; |
860 ++allow_keys; | |
1389 | 861 c = plain_vgetc(); |
7 | 862 --no_mapping; |
863 --allow_keys; | |
477 | 864 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O) |
7 | 865 { |
477 | 866 /* it's something else */ |
7 | 867 vungetc(c); |
868 c = Ctrl_BSL; | |
869 } | |
870 else if (c == Ctrl_G && p_im) | |
871 continue; | |
872 else | |
873 { | |
477 | 874 if (c == Ctrl_O) |
875 { | |
876 ins_ctrl_o(); | |
877 ins_at_eol = FALSE; /* cursor keeps its column */ | |
878 nomove = TRUE; | |
879 } | |
7 | 880 count = 0; |
881 goto doESCkey; | |
882 } | |
883 } | |
884 | |
885 #ifdef FEAT_DIGRAPHS | |
886 c = do_digraph(c); | |
887 #endif | |
888 | |
889 #ifdef FEAT_INS_EXPAND | |
890 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE) | |
891 goto docomplete; | |
892 #endif | |
893 if (c == Ctrl_V || c == Ctrl_Q) | |
894 { | |
895 ins_ctrl_v(); | |
896 c = Ctrl_V; /* pretend CTRL-V is last typed character */ | |
897 continue; | |
898 } | |
899 | |
900 #ifdef FEAT_CINDENT | |
901 if (cindent_on() | |
902 # ifdef FEAT_INS_EXPAND | |
903 && ctrl_x_mode == 0 | |
904 # endif | |
905 ) | |
906 { | |
907 /* A key name preceded by a bang means this key is not to be | |
908 * inserted. Skip ahead to the re-indenting below. | |
909 * A key name preceded by a star means that indenting has to be | |
910 * done before inserting the key. */ | |
911 line_is_white = inindent(0); | |
912 if (in_cinkeys(c, '!', line_is_white)) | |
913 goto force_cindent; | |
914 if (can_cindent && in_cinkeys(c, '*', line_is_white) | |
915 && stop_arrow() == OK) | |
916 do_c_expr_indent(); | |
917 } | |
918 #endif | |
919 | |
920 #ifdef FEAT_RIGHTLEFT | |
921 if (curwin->w_p_rl) | |
922 switch (c) | |
923 { | |
924 case K_LEFT: c = K_RIGHT; break; | |
925 case K_S_LEFT: c = K_S_RIGHT; break; | |
926 case K_C_LEFT: c = K_C_RIGHT; break; | |
927 case K_RIGHT: c = K_LEFT; break; | |
928 case K_S_RIGHT: c = K_S_LEFT; break; | |
929 case K_C_RIGHT: c = K_C_LEFT; break; | |
930 } | |
931 #endif | |
932 | |
933 /* | |
934 * If 'keymodel' contains "startsel", may start selection. If it | |
935 * does, a CTRL-O and c will be stuffed, we need to get these | |
936 * characters. | |
937 */ | |
938 if (ins_start_select(c)) | |
939 continue; | |
940 | |
941 /* | |
942 * The big switch to handle a character in insert mode. | |
943 */ | |
944 switch (c) | |
945 { | |
449 | 946 case ESC: /* End input mode */ |
7 | 947 if (echeck_abbr(ESC + ABBR_OFF)) |
948 break; | |
949 /*FALLTHROUGH*/ | |
950 | |
449 | 951 case Ctrl_C: /* End input mode */ |
7 | 952 #ifdef FEAT_CMDWIN |
953 if (c == Ctrl_C && cmdwin_type != 0) | |
954 { | |
955 /* Close the cmdline window. */ | |
956 cmdwin_result = K_IGNORE; | |
957 got_int = FALSE; /* don't stop executing autocommands et al. */ | |
929 | 958 nomove = TRUE; |
7 | 959 goto doESCkey; |
960 } | |
961 #endif | |
962 | |
963 #ifdef UNIX | |
964 do_intr: | |
965 #endif | |
966 /* when 'insertmode' set, and not halfway a mapping, don't leave | |
967 * Insert mode */ | |
968 if (goto_im()) | |
969 { | |
970 if (got_int) | |
971 { | |
972 (void)vgetc(); /* flush all buffers */ | |
973 got_int = FALSE; | |
974 } | |
975 else | |
976 vim_beep(); | |
977 break; | |
978 } | |
979 doESCkey: | |
980 /* | |
981 * This is the ONLY return from edit()! | |
982 */ | |
983 /* Always update o_lnum, so that a "CTRL-O ." that adds a line | |
984 * still puts the cursor back after the inserted text. */ | |
230 | 985 if (ins_at_eol && gchar_cursor() == NUL) |
7 | 986 o_lnum = curwin->w_cursor.lnum; |
987 | |
477 | 988 if (ins_esc(&count, cmdchar, nomove)) |
11 | 989 { |
990 #ifdef FEAT_AUTOCMD | |
991 if (cmdchar != 'r' && cmdchar != 'v') | |
992 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL, | |
993 FALSE, curbuf); | |
993 | 994 did_cursorhold = FALSE; |
11 | 995 #endif |
7 | 996 return (c == Ctrl_O); |
11 | 997 } |
7 | 998 continue; |
999 | |
449 | 1000 case Ctrl_Z: /* suspend when 'insertmode' set */ |
1001 if (!p_im) | |
1002 goto normalchar; /* insert CTRL-Z as normal char */ | |
1003 stuffReadbuff((char_u *)":st\r"); | |
1004 c = Ctrl_O; | |
1005 /*FALLTHROUGH*/ | |
1006 | |
1007 case Ctrl_O: /* execute one command */ | |
502 | 1008 #ifdef FEAT_COMPL_FUNC |
523 | 1009 if (ctrl_x_mode == CTRL_X_OMNI) |
449 | 1010 goto docomplete; |
1011 #endif | |
1012 if (echeck_abbr(Ctrl_O + ABBR_OFF)) | |
1013 break; | |
1014 ins_ctrl_o(); | |
853 | 1015 |
1016 #ifdef FEAT_VIRTUALEDIT | |
1017 /* don't move the cursor left when 'virtualedit' has "onemore". */ | |
1018 if (ve_flags & VE_ONEMORE) | |
1019 { | |
1020 ins_at_eol = FALSE; | |
1021 nomove = TRUE; | |
1022 } | |
1023 #endif | |
449 | 1024 count = 0; |
1025 goto doESCkey; | |
1026 | |
464 | 1027 case K_INS: /* toggle insert/replace mode */ |
1028 case K_KINS: | |
1029 ins_insert(replaceState); | |
1030 break; | |
1031 | |
1032 case K_SELECT: /* end of Select mode mapping - ignore */ | |
1033 break; | |
1034 | |
449 | 1035 #ifdef FEAT_SNIFF |
1036 case K_SNIFF: /* Sniff command received */ | |
1037 stuffcharReadbuff(K_SNIFF); | |
1038 goto doESCkey; | |
1039 #endif | |
1040 | |
1041 case K_HELP: /* Help key works like <ESC> <Help> */ | |
1042 case K_F1: | |
1043 case K_XF1: | |
1044 stuffcharReadbuff(K_HELP); | |
1045 if (p_im) | |
1046 need_start_insertmode = TRUE; | |
1047 goto doESCkey; | |
1048 | |
1049 #ifdef FEAT_NETBEANS_INTG | |
1050 case K_F21: /* NetBeans command */ | |
1051 ++no_mapping; /* don't map the next key hits */ | |
1389 | 1052 i = plain_vgetc(); |
449 | 1053 --no_mapping; |
1054 netbeans_keycommand(i); | |
1055 break; | |
1056 #endif | |
1057 | |
1058 case K_ZERO: /* Insert the previously inserted text. */ | |
7 | 1059 case NUL: |
1060 case Ctrl_A: | |
449 | 1061 /* For ^@ the trailing ESC will end the insert, unless there is an |
1062 * error. */ | |
7 | 1063 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL |
1064 && c != Ctrl_A && !p_im) | |
1065 goto doESCkey; /* quit insert mode */ | |
1066 inserted_space = FALSE; | |
1067 break; | |
1068 | |
449 | 1069 case Ctrl_R: /* insert the contents of a register */ |
7 | 1070 ins_reg(); |
1071 auto_format(FALSE, TRUE); | |
1072 inserted_space = FALSE; | |
1073 break; | |
1074 | |
449 | 1075 case Ctrl_G: /* commands starting with CTRL-G */ |
7 | 1076 ins_ctrl_g(); |
1077 break; | |
1078 | |
449 | 1079 case Ctrl_HAT: /* switch input mode and/or langmap */ |
1080 ins_ctrl_hat(); | |
7 | 1081 break; |
1082 | |
1083 #ifdef FEAT_RIGHTLEFT | |
449 | 1084 case Ctrl__: /* switch between languages */ |
7 | 1085 if (!p_ari) |
1086 goto normalchar; | |
1087 ins_ctrl_(); | |
1088 break; | |
1089 #endif | |
1090 | |
449 | 1091 case Ctrl_D: /* Make indent one shiftwidth smaller. */ |
7 | 1092 #if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID) |
1093 if (ctrl_x_mode == CTRL_X_PATH_DEFINES) | |
1094 goto docomplete; | |
1095 #endif | |
1096 /* FALLTHROUGH */ | |
1097 | |
449 | 1098 case Ctrl_T: /* Make indent one shiftwidth greater. */ |
7 | 1099 # ifdef FEAT_INS_EXPAND |
1100 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS) | |
1101 { | |
449 | 1102 if (has_compl_option(FALSE)) |
1103 goto docomplete; | |
1104 break; | |
7 | 1105 } |
1106 # endif | |
1107 ins_shift(c, lastc); | |
1108 auto_format(FALSE, TRUE); | |
1109 inserted_space = FALSE; | |
1110 break; | |
1111 | |
449 | 1112 case K_DEL: /* delete character under the cursor */ |
7 | 1113 case K_KDEL: |
1114 ins_del(); | |
1115 auto_format(FALSE, TRUE); | |
1116 break; | |
1117 | |
449 | 1118 case K_BS: /* delete character before the cursor */ |
7 | 1119 case Ctrl_H: |
1120 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space); | |
1121 auto_format(FALSE, TRUE); | |
1122 break; | |
1123 | |
449 | 1124 case Ctrl_W: /* delete word before the cursor */ |
7 | 1125 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space); |
1126 auto_format(FALSE, TRUE); | |
1127 break; | |
1128 | |
449 | 1129 case Ctrl_U: /* delete all inserted text in current line */ |
12 | 1130 # ifdef FEAT_COMPL_FUNC |
1131 /* CTRL-X CTRL-U completes with 'completefunc'. */ | |
449 | 1132 if (ctrl_x_mode == CTRL_X_FUNCTION) |
12 | 1133 goto docomplete; |
1134 # endif | |
7 | 1135 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space); |
1136 auto_format(FALSE, TRUE); | |
1137 inserted_space = FALSE; | |
1138 break; | |
1139 | |
1140 #ifdef FEAT_MOUSE | |
449 | 1141 case K_LEFTMOUSE: /* mouse keys */ |
7 | 1142 case K_LEFTMOUSE_NM: |
1143 case K_LEFTDRAG: | |
1144 case K_LEFTRELEASE: | |
1145 case K_LEFTRELEASE_NM: | |
1146 case K_MIDDLEMOUSE: | |
1147 case K_MIDDLEDRAG: | |
1148 case K_MIDDLERELEASE: | |
1149 case K_RIGHTMOUSE: | |
1150 case K_RIGHTDRAG: | |
1151 case K_RIGHTRELEASE: | |
1152 case K_X1MOUSE: | |
1153 case K_X1DRAG: | |
1154 case K_X1RELEASE: | |
1155 case K_X2MOUSE: | |
1156 case K_X2DRAG: | |
1157 case K_X2RELEASE: | |
1158 ins_mouse(c); | |
1159 break; | |
1160 | |
449 | 1161 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */ |
2409
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2378
diff
changeset
|
1162 ins_mousescroll(MSCR_DOWN); |
7 | 1163 break; |
1164 | |
449 | 1165 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */ |
2409
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2378
diff
changeset
|
1166 ins_mousescroll(MSCR_UP); |
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2378
diff
changeset
|
1167 break; |
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2378
diff
changeset
|
1168 |
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2378
diff
changeset
|
1169 case K_MOUSELEFT: /* Scroll wheel left */ |
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2378
diff
changeset
|
1170 ins_mousescroll(MSCR_LEFT); |
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2378
diff
changeset
|
1171 break; |
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2378
diff
changeset
|
1172 |
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2378
diff
changeset
|
1173 case K_MOUSERIGHT: /* Scroll wheel right */ |
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2378
diff
changeset
|
1174 ins_mousescroll(MSCR_RIGHT); |
7 | 1175 break; |
1176 #endif | |
692 | 1177 #ifdef FEAT_GUI_TABLINE |
1178 case K_TABLINE: | |
1179 case K_TABMENU: | |
1180 ins_tabline(c); | |
1181 break; | |
1182 #endif | |
7 | 1183 |
449 | 1184 case K_IGNORE: /* Something mapped to nothing */ |
7 | 1185 break; |
1186 | |
661 | 1187 #ifdef FEAT_AUTOCMD |
1188 case K_CURSORHOLD: /* Didn't type something for a while. */ | |
1189 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf); | |
1190 did_cursorhold = TRUE; | |
1191 break; | |
1192 #endif | |
1193 | |
625 | 1194 #ifdef FEAT_GUI_W32 |
1195 /* On Win32 ignore <M-F4>, we get it when closing the window was | |
1196 * cancelled. */ | |
1197 case K_F4: | |
1198 if (mod_mask != MOD_MASK_ALT) | |
1199 goto normalchar; | |
1200 break; | |
1201 #endif | |
1202 | |
7 | 1203 #ifdef FEAT_GUI |
1204 case K_VER_SCROLLBAR: | |
1205 ins_scroll(); | |
1206 break; | |
1207 | |
1208 case K_HOR_SCROLLBAR: | |
1209 ins_horscroll(); | |
1210 break; | |
1211 #endif | |
1212 | |
449 | 1213 case K_HOME: /* <Home> */ |
7 | 1214 case K_KHOME: |
1215 case K_S_HOME: | |
1216 case K_C_HOME: | |
1217 ins_home(c); | |
1218 break; | |
1219 | |
449 | 1220 case K_END: /* <End> */ |
7 | 1221 case K_KEND: |
1222 case K_S_END: | |
1223 case K_C_END: | |
1224 ins_end(c); | |
1225 break; | |
1226 | |
449 | 1227 case K_LEFT: /* <Left> */ |
180 | 1228 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)) |
1229 ins_s_left(); | |
1230 else | |
1231 ins_left(); | |
7 | 1232 break; |
1233 | |
449 | 1234 case K_S_LEFT: /* <S-Left> */ |
7 | 1235 case K_C_LEFT: |
1236 ins_s_left(); | |
1237 break; | |
1238 | |
449 | 1239 case K_RIGHT: /* <Right> */ |
180 | 1240 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)) |
1241 ins_s_right(); | |
1242 else | |
1243 ins_right(); | |
7 | 1244 break; |
1245 | |
449 | 1246 case K_S_RIGHT: /* <S-Right> */ |
7 | 1247 case K_C_RIGHT: |
1248 ins_s_right(); | |
1249 break; | |
1250 | |
449 | 1251 case K_UP: /* <Up> */ |
665 | 1252 #ifdef FEAT_INS_EXPAND |
1253 if (pum_visible()) | |
1254 goto docomplete; | |
1255 #endif | |
180 | 1256 if (mod_mask & MOD_MASK_SHIFT) |
1257 ins_pageup(); | |
1258 else | |
1259 ins_up(FALSE); | |
7 | 1260 break; |
1261 | |
449 | 1262 case K_S_UP: /* <S-Up> */ |
7 | 1263 case K_PAGEUP: |
1264 case K_KPAGEUP: | |
613 | 1265 #ifdef FEAT_INS_EXPAND |
610 | 1266 if (pum_visible()) |
1267 goto docomplete; | |
613 | 1268 #endif |
7 | 1269 ins_pageup(); |
1270 break; | |
1271 | |
449 | 1272 case K_DOWN: /* <Down> */ |
665 | 1273 #ifdef FEAT_INS_EXPAND |
1274 if (pum_visible()) | |
1275 goto docomplete; | |
1276 #endif | |
180 | 1277 if (mod_mask & MOD_MASK_SHIFT) |
1278 ins_pagedown(); | |
1279 else | |
1280 ins_down(FALSE); | |
7 | 1281 break; |
1282 | |
449 | 1283 case K_S_DOWN: /* <S-Down> */ |
7 | 1284 case K_PAGEDOWN: |
1285 case K_KPAGEDOWN: | |
613 | 1286 #ifdef FEAT_INS_EXPAND |
610 | 1287 if (pum_visible()) |
1288 goto docomplete; | |
613 | 1289 #endif |
7 | 1290 ins_pagedown(); |
1291 break; | |
1292 | |
1293 #ifdef FEAT_DND | |
449 | 1294 case K_DROP: /* drag-n-drop event */ |
7 | 1295 ins_drop(); |
1296 break; | |
1297 #endif | |
1298 | |
449 | 1299 case K_S_TAB: /* When not mapped, use like a normal TAB */ |
7 | 1300 c = TAB; |
1301 /* FALLTHROUGH */ | |
1302 | |
449 | 1303 case TAB: /* TAB or Complete patterns along path */ |
7 | 1304 #if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID) |
1305 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS) | |
1306 goto docomplete; | |
1307 #endif | |
1308 inserted_space = FALSE; | |
1309 if (ins_tab()) | |
1310 goto normalchar; /* insert TAB as a normal char */ | |
1311 auto_format(FALSE, TRUE); | |
1312 break; | |
1313 | |
449 | 1314 case K_KENTER: /* <Enter> */ |
7 | 1315 c = CAR; |
1316 /* FALLTHROUGH */ | |
1317 case CAR: | |
1318 case NL: | |
1319 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX) | |
1320 /* In a quickfix window a <CR> jumps to the error under the | |
1321 * cursor. */ | |
1322 if (bt_quickfix(curbuf) && c == CAR) | |
1323 { | |
644 | 1324 if (curwin->w_llist_ref == NULL) /* quickfix window */ |
1325 do_cmdline_cmd((char_u *)".cc"); | |
1326 else /* location list window */ | |
1327 do_cmdline_cmd((char_u *)".ll"); | |
7 | 1328 break; |
1329 } | |
1330 #endif | |
1331 #ifdef FEAT_CMDWIN | |
1332 if (cmdwin_type != 0) | |
1333 { | |
1334 /* Execute the command in the cmdline window. */ | |
1335 cmdwin_result = CAR; | |
1336 goto doESCkey; | |
1337 } | |
1338 #endif | |
1339 if (ins_eol(c) && !p_im) | |
1340 goto doESCkey; /* out of memory */ | |
1341 auto_format(FALSE, FALSE); | |
1342 inserted_space = FALSE; | |
1343 break; | |
1344 | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2227
diff
changeset
|
1345 #if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND) |
449 | 1346 case Ctrl_K: /* digraph or keyword completion */ |
7 | 1347 # ifdef FEAT_INS_EXPAND |
1348 if (ctrl_x_mode == CTRL_X_DICTIONARY) | |
1349 { | |
449 | 1350 if (has_compl_option(TRUE)) |
1351 goto docomplete; | |
1352 break; | |
7 | 1353 } |
1354 # endif | |
1355 # ifdef FEAT_DIGRAPHS | |
1356 c = ins_digraph(); | |
1357 if (c == NUL) | |
1358 break; | |
1359 # endif | |
1360 goto normalchar; | |
449 | 1361 #endif |
7 | 1362 |
1363 #ifdef FEAT_INS_EXPAND | |
464 | 1364 case Ctrl_X: /* Enter CTRL-X mode */ |
1365 ins_ctrl_x(); | |
1366 break; | |
1367 | |
449 | 1368 case Ctrl_RSB: /* Tag name completion after ^X */ |
7 | 1369 if (ctrl_x_mode != CTRL_X_TAGS) |
1370 goto normalchar; | |
1371 goto docomplete; | |
1372 | |
449 | 1373 case Ctrl_F: /* File name completion after ^X */ |
7 | 1374 if (ctrl_x_mode != CTRL_X_FILES) |
1375 goto normalchar; | |
1376 goto docomplete; | |
477 | 1377 |
1378 case 's': /* Spelling completion after ^X */ | |
1379 case Ctrl_S: | |
1380 if (ctrl_x_mode != CTRL_X_SPELL) | |
1381 goto normalchar; | |
1382 goto docomplete; | |
7 | 1383 #endif |
1384 | |
449 | 1385 case Ctrl_L: /* Whole line completion after ^X */ |
7 | 1386 #ifdef FEAT_INS_EXPAND |
1387 if (ctrl_x_mode != CTRL_X_WHOLE_LINE) | |
1388 #endif | |
1389 { | |
1390 /* CTRL-L with 'insertmode' set: Leave Insert mode */ | |
1391 if (p_im) | |
1392 { | |
1393 if (echeck_abbr(Ctrl_L + ABBR_OFF)) | |
1394 break; | |
1395 goto doESCkey; | |
1396 } | |
1397 goto normalchar; | |
1398 } | |
1399 #ifdef FEAT_INS_EXPAND | |
1400 /* FALLTHROUGH */ | |
1401 | |
449 | 1402 case Ctrl_P: /* Do previous/next pattern completion */ |
7 | 1403 case Ctrl_N: |
1404 /* if 'complete' is empty then plain ^P is no longer special, | |
1405 * but it is under other ^X modes */ | |
1406 if (*curbuf->b_p_cpt == NUL | |
1407 && ctrl_x_mode != 0 | |
449 | 1408 && !(compl_cont_status & CONT_LOCAL)) |
7 | 1409 goto normalchar; |
1410 | |
1411 docomplete: | |
1927 | 1412 compl_busy = TRUE; |
7 | 1413 if (ins_complete(c) == FAIL) |
449 | 1414 compl_cont_status = 0; |
1927 | 1415 compl_busy = FALSE; |
7 | 1416 break; |
1417 #endif /* FEAT_INS_EXPAND */ | |
1418 | |
449 | 1419 case Ctrl_Y: /* copy from previous line or scroll down */ |
1420 case Ctrl_E: /* copy from next line or scroll up */ | |
1421 c = ins_ctrl_ey(c); | |
7 | 1422 break; |
1423 | |
1424 default: | |
1425 #ifdef UNIX | |
1426 if (c == intr_char) /* special interrupt char */ | |
1427 goto do_intr; | |
1428 #endif | |
1429 | |
2845 | 1430 normalchar: |
7 | 1431 /* |
4352 | 1432 * Insert a normal character. |
7 | 1433 */ |
2845 | 1434 #ifdef FEAT_AUTOCMD |
1435 if (!p_paste) | |
1436 { | |
3390 | 1437 /* Trigger InsertCharPre. */ |
1438 char_u *str = do_insert_char_pre(c); | |
1439 char_u *p; | |
1440 | |
1441 if (str != NULL) | |
2845 | 1442 { |
3390 | 1443 if (*str != NUL && stop_arrow() != FAIL) |
2845 | 1444 { |
3390 | 1445 /* Insert the new value of v:char literally. */ |
1446 for (p = str; *p != NUL; mb_ptr_adv(p)) | |
2845 | 1447 { |
3390 | 1448 c = PTR2CHAR(p); |
1449 if (c == CAR || c == K_KENTER || c == NL) | |
1450 ins_eol(c); | |
1451 else | |
1452 ins_char(c); | |
2845 | 1453 } |
3390 | 1454 AppendToRedobuffLit(str, -1); |
2845 | 1455 } |
3390 | 1456 vim_free(str); |
1457 c = NUL; | |
2845 | 1458 } |
1459 | |
3390 | 1460 /* If the new value is already inserted or an empty string |
1461 * then don't insert any character. */ | |
2845 | 1462 if (c == NUL) |
1463 break; | |
1464 } | |
1465 #endif | |
7 | 1466 #ifdef FEAT_SMARTINDENT |
1467 /* Try to perform smart-indenting. */ | |
1468 ins_try_si(c); | |
1469 #endif | |
1470 | |
1471 if (c == ' ') | |
1472 { | |
1473 inserted_space = TRUE; | |
1474 #ifdef FEAT_CINDENT | |
1475 if (inindent(0)) | |
1476 can_cindent = FALSE; | |
1477 #endif | |
1478 if (Insstart_blank_vcol == MAXCOL | |
1479 && curwin->w_cursor.lnum == Insstart.lnum) | |
1480 Insstart_blank_vcol = get_nolist_virtcol(); | |
1481 } | |
1482 | |
3448 | 1483 /* Insert a normal character and check for abbreviations on a |
1484 * special character. Let CTRL-] expand abbreviations without | |
1485 * inserting it. */ | |
1486 if (vim_iswordc(c) || (!echeck_abbr( | |
7 | 1487 #ifdef FEAT_MBYTE |
1488 /* Add ABBR_OFF for characters above 0x100, this is | |
1489 * what check_abbr() expects. */ | |
1490 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : | |
1491 #endif | |
3584 | 1492 c) && c != Ctrl_RSB)) |
7 | 1493 { |
1494 insert_special(c, FALSE, FALSE); | |
1495 #ifdef FEAT_RIGHTLEFT | |
1496 revins_legal++; | |
1497 revins_chars++; | |
1498 #endif | |
1499 } | |
1500 | |
1501 auto_format(FALSE, TRUE); | |
1502 | |
1503 #ifdef FEAT_FOLDING | |
1504 /* When inserting a character the cursor line must never be in a | |
1505 * closed fold. */ | |
1506 foldOpenCursor(); | |
1507 #endif | |
1508 break; | |
1509 } /* end of switch (c) */ | |
1510 | |
978 | 1511 #ifdef FEAT_AUTOCMD |
1512 /* If typed something may trigger CursorHoldI again. */ | |
1513 if (c != K_CURSORHOLD) | |
1514 did_cursorhold = FALSE; | |
1515 #endif | |
1516 | |
7 | 1517 /* If the cursor was moved we didn't just insert a space */ |
1518 if (arrow_used) | |
1519 inserted_space = FALSE; | |
1520 | |
1521 #ifdef FEAT_CINDENT | |
1522 if (can_cindent && cindent_on() | |
1523 # ifdef FEAT_INS_EXPAND | |
1524 && ctrl_x_mode == 0 | |
1525 # endif | |
1526 ) | |
1527 { | |
1528 force_cindent: | |
1529 /* | |
1530 * Indent now if a key was typed that is in 'cinkeys'. | |
1531 */ | |
1532 if (in_cinkeys(c, ' ', line_is_white)) | |
1533 { | |
1534 if (stop_arrow() == OK) | |
1535 /* re-indent the current line */ | |
1536 do_c_expr_indent(); | |
1537 } | |
1538 } | |
1539 #endif /* FEAT_CINDENT */ | |
1540 | |
1541 } /* for (;;) */ | |
1542 /* NOTREACHED */ | |
1543 } | |
1544 | |
1545 /* | |
1546 * Redraw for Insert mode. | |
1547 * This is postponed until getting the next character to make '$' in the 'cpo' | |
1548 * option work correctly. | |
1549 * Only redraw when there are no characters available. This speeds up | |
1550 * inserting sequences of characters (e.g., for CTRL-R). | |
1551 */ | |
1552 static void | |
661 | 1553 ins_redraw(ready) |
1876 | 1554 int ready UNUSED; /* not busy with something */ |
7 | 1555 { |
2282
a888ed7ba375
Make updating text for conceal mode simpler. A few compiler warning fixes.
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
1556 #ifdef FEAT_CONCEAL |
a888ed7ba375
Make updating text for conceal mode simpler. A few compiler warning fixes.
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
1557 linenr_T conceal_old_cursor_line = 0; |
a888ed7ba375
Make updating text for conceal mode simpler. A few compiler warning fixes.
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
1558 linenr_T conceal_new_cursor_line = 0; |
a888ed7ba375
Make updating text for conceal mode simpler. A few compiler warning fixes.
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
1559 int conceal_update_lines = FALSE; |
a888ed7ba375
Make updating text for conceal mode simpler. A few compiler warning fixes.
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
1560 #endif |
a888ed7ba375
Make updating text for conceal mode simpler. A few compiler warning fixes.
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
1561 |
5592 | 1562 if (char_avail()) |
1563 return; | |
1564 | |
2282
a888ed7ba375
Make updating text for conceal mode simpler. A few compiler warning fixes.
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
1565 #if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL) |
5592 | 1566 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is |
1567 * visible, the command might delete it. */ | |
1568 if (ready && ( | |
2282
a888ed7ba375
Make updating text for conceal mode simpler. A few compiler warning fixes.
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
1569 # ifdef FEAT_AUTOCMD |
5592 | 1570 has_cursormovedI() |
2282
a888ed7ba375
Make updating text for conceal mode simpler. A few compiler warning fixes.
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
1571 # endif |
a888ed7ba375
Make updating text for conceal mode simpler. A few compiler warning fixes.
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
1572 # if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL) |
5592 | 1573 || |
2282
a888ed7ba375
Make updating text for conceal mode simpler. A few compiler warning fixes.
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
1574 # endif |
a888ed7ba375
Make updating text for conceal mode simpler. A few compiler warning fixes.
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
1575 # ifdef FEAT_CONCEAL |
5592 | 1576 curwin->w_p_cole > 0 |
1577 # endif | |
1578 ) | |
1579 && !equalpos(last_cursormoved, curwin->w_cursor) | |
1580 # ifdef FEAT_INS_EXPAND | |
1581 && !pum_visible() | |
1582 # endif | |
1583 ) | |
1584 { | |
1585 # ifdef FEAT_SYN_HL | |
1586 /* Need to update the screen first, to make sure syntax | |
1587 * highlighting is correct after making a change (e.g., inserting | |
1588 * a "(". The autocommand may also require a redraw, so it's done | |
1589 * again below, unfortunately. */ | |
1590 if (syntax_present(curwin) && must_redraw) | |
1591 update_screen(0); | |
2282
a888ed7ba375
Make updating text for conceal mode simpler. A few compiler warning fixes.
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
1592 # endif |
5592 | 1593 # ifdef FEAT_AUTOCMD |
1594 if (has_cursormovedI()) | |
1595 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf); | |
1596 # endif | |
1597 # ifdef FEAT_CONCEAL | |
1598 if (curwin->w_p_cole > 0) | |
1599 { | |
1600 conceal_old_cursor_line = last_cursormoved.lnum; | |
1601 conceal_new_cursor_line = curwin->w_cursor.lnum; | |
1602 conceal_update_lines = TRUE; | |
1603 } | |
1604 # endif | |
1605 last_cursormoved = curwin->w_cursor; | |
1606 } | |
1607 #endif | |
1608 | |
1609 #ifdef FEAT_AUTOCMD | |
1610 /* Trigger TextChangedI if b_changedtick differs. */ | |
1611 if (ready && has_textchangedI() | |
1612 && last_changedtick != curbuf->b_changedtick | |
794 | 1613 # ifdef FEAT_INS_EXPAND |
2282
a888ed7ba375
Make updating text for conceal mode simpler. A few compiler warning fixes.
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
1614 && !pum_visible() |
794 | 1615 # endif |
5592 | 1616 ) |
1617 { | |
1618 if (last_changedtick_buf == curbuf) | |
1619 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf); | |
1620 last_changedtick_buf = curbuf; | |
1621 last_changedtick = curbuf->b_changedtick; | |
1622 } | |
1623 #endif | |
1624 | |
1625 if (must_redraw) | |
1626 update_screen(0); | |
1627 else if (clear_cmdline || redraw_cmdline) | |
1628 showmode(); /* clear cmdline and show mode */ | |
1629 # if defined(FEAT_CONCEAL) | |
1630 if ((conceal_update_lines | |
1631 && (conceal_old_cursor_line != conceal_new_cursor_line | |
1632 || conceal_cursor_line(curwin))) | |
1633 || need_cursor_line_redraw) | |
1634 { | |
1635 if (conceal_old_cursor_line != conceal_new_cursor_line) | |
1636 update_single_line(curwin, conceal_old_cursor_line); | |
1637 update_single_line(curwin, conceal_new_cursor_line == 0 | |
1638 ? curwin->w_cursor.lnum : conceal_new_cursor_line); | |
1639 curwin->w_valid &= ~VALID_CROW; | |
1640 } | |
2282
a888ed7ba375
Make updating text for conceal mode simpler. A few compiler warning fixes.
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
1641 # endif |
5592 | 1642 showruler(FALSE); |
1643 setcursor(); | |
1644 emsg_on_display = FALSE; /* may remove error message now */ | |
7 | 1645 } |
1646 | |
1647 /* | |
1648 * Handle a CTRL-V or CTRL-Q typed in Insert mode. | |
1649 */ | |
1650 static void | |
1651 ins_ctrl_v() | |
1652 { | |
1653 int c; | |
2811 | 1654 int did_putchar = FALSE; |
7 | 1655 |
1656 /* may need to redraw when no more chars available now */ | |
661 | 1657 ins_redraw(FALSE); |
7 | 1658 |
1659 if (redrawing() && !char_avail()) | |
2811 | 1660 { |
7 | 1661 edit_putchar('^', TRUE); |
2811 | 1662 did_putchar = TRUE; |
1663 } | |
7 | 1664 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */ |
1665 | |
1666 #ifdef FEAT_CMDL_INFO | |
1667 add_to_showcmd_c(Ctrl_V); | |
1668 #endif | |
1669 | |
1670 c = get_literal(); | |
2811 | 1671 if (did_putchar) |
1672 /* when the line fits in 'columns' the '^' is at the start of the next | |
1673 * line and will not removed by the redraw */ | |
1674 edit_unputchar(); | |
7 | 1675 #ifdef FEAT_CMDL_INFO |
1676 clear_showcmd(); | |
1677 #endif | |
1678 insert_special(c, FALSE, TRUE); | |
1679 #ifdef FEAT_RIGHTLEFT | |
1680 revins_chars++; | |
1681 revins_legal++; | |
1682 #endif | |
1683 } | |
1684 | |
1685 /* | |
1686 * Put a character directly onto the screen. It's not stored in a buffer. | |
1687 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode. | |
1688 */ | |
1689 static int pc_status; | |
1690 #define PC_STATUS_UNSET 0 /* pc_bytes was not set */ | |
1691 #define PC_STATUS_RIGHT 1 /* right halve of double-wide char */ | |
1692 #define PC_STATUS_LEFT 2 /* left halve of double-wide char */ | |
1693 #define PC_STATUS_SET 3 /* pc_bytes was filled */ | |
1694 static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */ | |
1695 static int pc_attr; | |
1696 static int pc_row; | |
1697 static int pc_col; | |
1698 | |
1699 void | |
1700 edit_putchar(c, highlight) | |
1701 int c; | |
1702 int highlight; | |
1703 { | |
1704 int attr; | |
1705 | |
1706 if (ScreenLines != NULL) | |
1707 { | |
1708 update_topline(); /* just in case w_topline isn't valid */ | |
1709 validate_cursor(); | |
1710 if (highlight) | |
1711 attr = hl_attr(HLF_8); | |
1712 else | |
1713 attr = 0; | |
1714 pc_row = W_WINROW(curwin) + curwin->w_wrow; | |
1715 pc_col = W_WINCOL(curwin); | |
1716 #if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE) | |
1717 pc_status = PC_STATUS_UNSET; | |
1718 #endif | |
1719 #ifdef FEAT_RIGHTLEFT | |
1720 if (curwin->w_p_rl) | |
1721 { | |
1722 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol; | |
1723 # ifdef FEAT_MBYTE | |
1724 if (has_mbyte) | |
1725 { | |
1726 int fix_col = mb_fix_col(pc_col, pc_row); | |
1727 | |
1728 if (fix_col != pc_col) | |
1729 { | |
1730 screen_putchar(' ', pc_row, fix_col, attr); | |
1731 --curwin->w_wcol; | |
1732 pc_status = PC_STATUS_RIGHT; | |
1733 } | |
1734 } | |
1735 # endif | |
1736 } | |
1737 else | |
1738 #endif | |
1739 { | |
1740 pc_col += curwin->w_wcol; | |
1741 #ifdef FEAT_MBYTE | |
1742 if (mb_lefthalve(pc_row, pc_col)) | |
1743 pc_status = PC_STATUS_LEFT; | |
1744 #endif | |
1745 } | |
1746 | |
1747 /* save the character to be able to put it back */ | |
1748 #if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE) | |
1749 if (pc_status == PC_STATUS_UNSET) | |
1750 #endif | |
1751 { | |
1752 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr); | |
1753 pc_status = PC_STATUS_SET; | |
1754 } | |
1755 screen_putchar(c, pc_row, pc_col, attr); | |
1756 } | |
1757 } | |
1758 | |
1759 /* | |
1760 * Undo the previous edit_putchar(). | |
1761 */ | |
1762 void | |
1763 edit_unputchar() | |
1764 { | |
1765 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled) | |
1766 { | |
1767 #if defined(FEAT_MBYTE) | |
1768 if (pc_status == PC_STATUS_RIGHT) | |
1769 ++curwin->w_wcol; | |
1770 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT) | |
1771 redrawWinline(curwin->w_cursor.lnum, FALSE); | |
1772 else | |
1773 #endif | |
1774 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr); | |
1775 } | |
1776 } | |
1777 | |
1778 /* | |
1779 * Called when p_dollar is set: display a '$' at the end of the changed text | |
1780 * Only works when cursor is in the line that changes. | |
1781 */ | |
1782 void | |
1783 display_dollar(col) | |
1784 colnr_T col; | |
1785 { | |
1786 colnr_T save_col; | |
1787 | |
1788 if (!redrawing()) | |
1789 return; | |
1790 | |
1791 cursor_off(); | |
1792 save_col = curwin->w_cursor.col; | |
1793 curwin->w_cursor.col = col; | |
1794 #ifdef FEAT_MBYTE | |
1795 if (has_mbyte) | |
1796 { | |
1797 char_u *p; | |
1798 | |
1799 /* If on the last byte of a multi-byte move to the first byte. */ | |
1800 p = ml_get_curline(); | |
1801 curwin->w_cursor.col -= (*mb_head_off)(p, p + col); | |
1802 } | |
1803 #endif | |
1804 curs_columns(FALSE); /* recompute w_wrow and w_wcol */ | |
1805 if (curwin->w_wcol < W_WIDTH(curwin)) | |
1806 { | |
1807 edit_putchar('$', FALSE); | |
1808 dollar_vcol = curwin->w_virtcol; | |
1809 } | |
1810 curwin->w_cursor.col = save_col; | |
1811 } | |
1812 | |
1813 /* | |
1814 * Call this function before moving the cursor from the normal insert position | |
1815 * in insert mode. | |
1816 */ | |
1817 static void | |
1818 undisplay_dollar() | |
1819 { | |
3318 | 1820 if (dollar_vcol >= 0) |
1821 { | |
1822 dollar_vcol = -1; | |
7 | 1823 redrawWinline(curwin->w_cursor.lnum, FALSE); |
1824 } | |
1825 } | |
1826 | |
1827 /* | |
1828 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D). | |
1829 * Keep the cursor on the same character. | |
1830 * type == INDENT_INC increase indent (for CTRL-T or <Tab>) | |
1831 * type == INDENT_DEC decrease indent (for CTRL-D) | |
1832 * type == INDENT_SET set indent to "amount" | |
1833 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec). | |
1834 */ | |
1835 void | |
1516 | 1836 change_indent(type, amount, round, replaced, call_changed_bytes) |
7 | 1837 int type; |
1838 int amount; | |
1839 int round; | |
1840 int replaced; /* replaced character, put on replace stack */ | |
1516 | 1841 int call_changed_bytes; /* call changed_bytes() */ |
7 | 1842 { |
1843 int vcol; | |
1844 int last_vcol; | |
1845 int insstart_less; /* reduction for Insstart.col */ | |
1846 int new_cursor_col; | |
1847 int i; | |
1848 char_u *ptr; | |
1849 int save_p_list; | |
1850 int start_col; | |
1851 colnr_T vc; | |
1852 #ifdef FEAT_VREPLACE | |
1853 colnr_T orig_col = 0; /* init for GCC */ | |
1854 char_u *new_line, *orig_line = NULL; /* init for GCC */ | |
1855 | |
1856 /* VREPLACE mode needs to know what the line was like before changing */ | |
1857 if (State & VREPLACE_FLAG) | |
1858 { | |
1859 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */ | |
1860 orig_col = curwin->w_cursor.col; | |
1861 } | |
1862 #endif | |
1863 | |
1864 /* for the following tricks we don't want list mode */ | |
1865 save_p_list = curwin->w_p_list; | |
1866 curwin->w_p_list = FALSE; | |
1867 vc = getvcol_nolist(&curwin->w_cursor); | |
1868 vcol = vc; | |
1869 | |
1870 /* | |
1871 * For Replace mode we need to fix the replace stack later, which is only | |
1872 * possible when the cursor is in the indent. Remember the number of | |
1873 * characters before the cursor if it's possible. | |
1874 */ | |
1875 start_col = curwin->w_cursor.col; | |
1876 | |
1877 /* determine offset from first non-blank */ | |
1878 new_cursor_col = curwin->w_cursor.col; | |
1879 beginline(BL_WHITE); | |
1880 new_cursor_col -= curwin->w_cursor.col; | |
1881 | |
1882 insstart_less = curwin->w_cursor.col; | |
1883 | |
1884 /* | |
1885 * If the cursor is in the indent, compute how many screen columns the | |
1886 * cursor is to the left of the first non-blank. | |
1887 */ | |
1888 if (new_cursor_col < 0) | |
1889 vcol = get_indent() - vcol; | |
1890 | |
1891 if (new_cursor_col > 0) /* can't fix replace stack */ | |
1892 start_col = -1; | |
1893 | |
1894 /* | |
1895 * Set the new indent. The cursor will be put on the first non-blank. | |
1896 */ | |
1897 if (type == INDENT_SET) | |
1516 | 1898 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0); |
7 | 1899 else |
1900 { | |
1901 #ifdef FEAT_VREPLACE | |
1902 int save_State = State; | |
1903 | |
1904 /* Avoid being called recursively. */ | |
1905 if (State & VREPLACE_FLAG) | |
1906 State = INSERT; | |
1907 #endif | |
1516 | 1908 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes); |
7 | 1909 #ifdef FEAT_VREPLACE |
1910 State = save_State; | |
1911 #endif | |
1912 } | |
1913 insstart_less -= curwin->w_cursor.col; | |
1914 | |
1915 /* | |
1916 * Try to put cursor on same character. | |
1917 * If the cursor is at or after the first non-blank in the line, | |
1918 * compute the cursor column relative to the column of the first | |
1919 * non-blank character. | |
1920 * If we are not in insert mode, leave the cursor on the first non-blank. | |
1921 * If the cursor is before the first non-blank, position it relative | |
1922 * to the first non-blank, counted in screen columns. | |
1923 */ | |
1924 if (new_cursor_col >= 0) | |
1925 { | |
1926 /* | |
1927 * When changing the indent while the cursor is touching it, reset | |
1928 * Insstart_col to 0. | |
1929 */ | |
1930 if (new_cursor_col == 0) | |
1931 insstart_less = MAXCOL; | |
1932 new_cursor_col += curwin->w_cursor.col; | |
1933 } | |
1934 else if (!(State & INSERT)) | |
1935 new_cursor_col = curwin->w_cursor.col; | |
1936 else | |
1937 { | |
1938 /* | |
1939 * Compute the screen column where the cursor should be. | |
1940 */ | |
1941 vcol = get_indent() - vcol; | |
1869 | 1942 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol); |
7 | 1943 |
1944 /* | |
1945 * Advance the cursor until we reach the right screen column. | |
1946 */ | |
1947 vcol = last_vcol = 0; | |
1948 new_cursor_col = -1; | |
1949 ptr = ml_get_curline(); | |
1950 while (vcol <= (int)curwin->w_virtcol) | |
1951 { | |
1952 last_vcol = vcol; | |
1953 #ifdef FEAT_MBYTE | |
1954 if (has_mbyte && new_cursor_col >= 0) | |
474 | 1955 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col); |
7 | 1956 else |
1957 #endif | |
1958 ++new_cursor_col; | |
5995 | 1959 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol); |
7 | 1960 } |
1961 vcol = last_vcol; | |
1962 | |
1963 /* | |
1964 * May need to insert spaces to be able to position the cursor on | |
1965 * the right screen column. | |
1966 */ | |
1967 if (vcol != (int)curwin->w_virtcol) | |
1968 { | |
1869 | 1969 curwin->w_cursor.col = (colnr_T)new_cursor_col; |
7 | 1970 i = (int)curwin->w_virtcol - vcol; |
1869 | 1971 ptr = alloc((unsigned)(i + 1)); |
7 | 1972 if (ptr != NULL) |
1973 { | |
1974 new_cursor_col += i; | |
1975 ptr[i] = NUL; | |
1976 while (--i >= 0) | |
1977 ptr[i] = ' '; | |
1978 ins_str(ptr); | |
1979 vim_free(ptr); | |
1980 } | |
1981 } | |
1982 | |
1983 /* | |
1984 * When changing the indent while the cursor is in it, reset | |
1985 * Insstart_col to 0. | |
1986 */ | |
1987 insstart_less = MAXCOL; | |
1988 } | |
1989 | |
1990 curwin->w_p_list = save_p_list; | |
1991 | |
1992 if (new_cursor_col <= 0) | |
1993 curwin->w_cursor.col = 0; | |
1994 else | |
1869 | 1995 curwin->w_cursor.col = (colnr_T)new_cursor_col; |
7 | 1996 curwin->w_set_curswant = TRUE; |
1997 changed_cline_bef_curs(); | |
1998 | |
1999 /* | |
2000 * May have to adjust the start of the insert. | |
2001 */ | |
2002 if (State & INSERT) | |
2003 { | |
2004 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0) | |
2005 { | |
2006 if ((int)Insstart.col <= insstart_less) | |
2007 Insstart.col = 0; | |
2008 else | |
2009 Insstart.col -= insstart_less; | |
2010 } | |
2011 if ((int)ai_col <= insstart_less) | |
2012 ai_col = 0; | |
2013 else | |
2014 ai_col -= insstart_less; | |
2015 } | |
2016 | |
2017 /* | |
2018 * For REPLACE mode, may have to fix the replace stack, if it's possible. | |
2019 * If the number of characters before the cursor decreased, need to pop a | |
2020 * few characters from the replace stack. | |
2021 * If the number of characters before the cursor increased, need to push a | |
2022 * few NULs onto the replace stack. | |
2023 */ | |
2024 if (REPLACE_NORMAL(State) && start_col >= 0) | |
2025 { | |
2026 while (start_col > (int)curwin->w_cursor.col) | |
2027 { | |
2028 replace_join(0); /* remove a NUL from the replace stack */ | |
2029 --start_col; | |
2030 } | |
2031 while (start_col < (int)curwin->w_cursor.col || replaced) | |
2032 { | |
2033 replace_push(NUL); | |
2034 if (replaced) | |
2035 { | |
2036 replace_push(replaced); | |
2037 replaced = NUL; | |
2038 } | |
2039 ++start_col; | |
2040 } | |
2041 } | |
2042 | |
2043 #ifdef FEAT_VREPLACE | |
2044 /* | |
2045 * For VREPLACE mode, we also have to fix the replace stack. In this case | |
2046 * it is always possible because we backspace over the whole line and then | |
2047 * put it back again the way we wanted it. | |
2048 */ | |
2049 if (State & VREPLACE_FLAG) | |
2050 { | |
2051 /* If orig_line didn't allocate, just return. At least we did the job, | |
2052 * even if you can't backspace. */ | |
2053 if (orig_line == NULL) | |
2054 return; | |
2055 | |
2056 /* Save new line */ | |
2057 new_line = vim_strsave(ml_get_curline()); | |
2058 if (new_line == NULL) | |
2059 return; | |
2060 | |
2061 /* We only put back the new line up to the cursor */ | |
2062 new_line[curwin->w_cursor.col] = NUL; | |
2063 | |
2064 /* Put back original line */ | |
2065 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE); | |
2066 curwin->w_cursor.col = orig_col; | |
2067 | |
2068 /* Backspace from cursor to start of line */ | |
2069 backspace_until_column(0); | |
2070 | |
2071 /* Insert new stuff into line again */ | |
2072 ins_bytes(new_line); | |
2073 | |
2074 vim_free(new_line); | |
2075 } | |
2076 #endif | |
2077 } | |
2078 | |
2079 /* | |
2080 * Truncate the space at the end of a line. This is to be used only in an | |
2081 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE | |
2082 * modes. | |
2083 */ | |
2084 void | |
2085 truncate_spaces(line) | |
2086 char_u *line; | |
2087 { | |
2088 int i; | |
2089 | |
2090 /* find start of trailing white space */ | |
2091 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--) | |
2092 { | |
2093 if (State & REPLACE_FLAG) | |
2094 replace_join(0); /* remove a NUL from the replace stack */ | |
2095 } | |
2096 line[i + 1] = NUL; | |
2097 } | |
2098 | |
2099 #if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \ | |
2100 || defined(FEAT_COMMENTS) || defined(PROTO) | |
2101 /* | |
2102 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE | |
2103 * modes correctly. May also be used when not in insert mode at all. | |
1782 | 2104 * Will attempt not to go before "col" even when there is a composing |
2105 * character. | |
7 | 2106 */ |
2107 void | |
2108 backspace_until_column(col) | |
2109 int col; | |
2110 { | |
2111 while ((int)curwin->w_cursor.col > col) | |
2112 { | |
2113 curwin->w_cursor.col--; | |
2114 if (State & REPLACE_FLAG) | |
1782 | 2115 replace_do_bs(col); |
2116 else if (!del_char_after_col(col)) | |
2117 break; | |
2118 } | |
2119 } | |
2120 #endif | |
2121 | |
2122 /* | |
2123 * Like del_char(), but make sure not to go before column "limit_col". | |
2124 * Only matters when there are composing characters. | |
2125 * Return TRUE when something was deleted. | |
2126 */ | |
2127 static int | |
2128 del_char_after_col(limit_col) | |
1876 | 2129 int limit_col UNUSED; |
1782 | 2130 { |
2131 #ifdef FEAT_MBYTE | |
2132 if (enc_utf8 && limit_col >= 0) | |
2133 { | |
1869 | 2134 colnr_T ecol = curwin->w_cursor.col + 1; |
1782 | 2135 |
2136 /* Make sure the cursor is at the start of a character, but | |
2137 * skip forward again when going too far back because of a | |
2138 * composing character. */ | |
2139 mb_adjust_cursor(); | |
1796 | 2140 while (curwin->w_cursor.col < (colnr_T)limit_col) |
1782 | 2141 { |
2142 int l = utf_ptr2len(ml_get_cursor()); | |
2143 | |
2144 if (l == 0) /* end of line */ | |
2145 break; | |
2146 curwin->w_cursor.col += l; | |
2147 } | |
2148 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol) | |
2149 return FALSE; | |
1869 | 2150 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE); |
1782 | 2151 } |
2152 else | |
2153 #endif | |
2154 (void)del_char(FALSE); | |
2155 return TRUE; | |
2156 } | |
7 | 2157 |
2158 #if defined(FEAT_INS_EXPAND) || defined(PROTO) | |
2159 /* | |
449 | 2160 * CTRL-X pressed in Insert mode. |
2161 */ | |
2162 static void | |
2163 ins_ctrl_x() | |
2164 { | |
2165 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X | |
2166 * CTRL-V works like CTRL-N */ | |
2167 if (ctrl_x_mode != CTRL_X_CMDLINE) | |
2168 { | |
2169 /* if the next ^X<> won't ADD nothing, then reset | |
2170 * compl_cont_status */ | |
2171 if (compl_cont_status & CONT_N_ADDS) | |
665 | 2172 compl_cont_status |= CONT_INTRPT; |
449 | 2173 else |
2174 compl_cont_status = 0; | |
2175 /* We're not sure which CTRL-X mode it will be yet */ | |
2176 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET; | |
2177 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode)); | |
2178 edit_submode_pre = NULL; | |
2179 showmode(); | |
2180 } | |
2181 } | |
2182 | |
2183 /* | |
2184 * Return TRUE if the 'dict' or 'tsr' option can be used. | |
2185 */ | |
2186 static int | |
2187 has_compl_option(dict_opt) | |
2188 int dict_opt; | |
2189 { | |
703 | 2190 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL |
744 | 2191 # ifdef FEAT_SPELL |
2192 && !curwin->w_p_spell | |
2193 # endif | |
2194 ) | |
449 | 2195 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL)) |
2196 { | |
2197 ctrl_x_mode = 0; | |
2198 edit_submode = NULL; | |
2199 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty") | |
2200 : (char_u *)_("'thesaurus' option is empty"), | |
2201 hl_attr(HLF_E)); | |
2202 if (emsg_silent == 0) | |
2203 { | |
2204 vim_beep(); | |
2205 setcursor(); | |
2206 out_flush(); | |
2207 ui_delay(2000L, FALSE); | |
2208 } | |
2209 return FALSE; | |
2210 } | |
2211 return TRUE; | |
2212 } | |
2213 | |
2214 /* | |
7 | 2215 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode? |
2216 * This depends on the current mode. | |
2217 */ | |
2218 int | |
2219 vim_is_ctrl_x_key(c) | |
2220 int c; | |
2221 { | |
2222 /* Always allow ^R - let it's results then be checked */ | |
2223 if (c == Ctrl_R) | |
2224 return TRUE; | |
2225 | |
610 | 2226 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */ |
644 | 2227 if (ins_compl_pum_key(c)) |
610 | 2228 return TRUE; |
2229 | |
7 | 2230 switch (ctrl_x_mode) |
2231 { | |
2232 case 0: /* Not in any CTRL-X mode */ | |
2233 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X); | |
2234 case CTRL_X_NOT_DEFINED_YET: | |
449 | 2235 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E |
7 | 2236 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB |
2237 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P | |
2238 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V | |
477 | 2239 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O |
3229 | 2240 || c == Ctrl_S || c == Ctrl_K || c == 's'); |
7 | 2241 case CTRL_X_SCROLL: |
2242 return (c == Ctrl_Y || c == Ctrl_E); | |
2243 case CTRL_X_WHOLE_LINE: | |
2244 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N); | |
2245 case CTRL_X_FILES: | |
2246 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N); | |
2247 case CTRL_X_DICTIONARY: | |
2248 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N); | |
2249 case CTRL_X_THESAURUS: | |
2250 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N); | |
2251 case CTRL_X_TAGS: | |
2252 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N); | |
2253 #ifdef FEAT_FIND_ID | |
2254 case CTRL_X_PATH_PATTERNS: | |
2255 return (c == Ctrl_P || c == Ctrl_N); | |
2256 case CTRL_X_PATH_DEFINES: | |
2257 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N); | |
2258 #endif | |
2259 case CTRL_X_CMDLINE: | |
2260 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N | |
2261 || c == Ctrl_X); | |
12 | 2262 #ifdef FEAT_COMPL_FUNC |
2263 case CTRL_X_FUNCTION: | |
449 | 2264 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N); |
523 | 2265 case CTRL_X_OMNI: |
449 | 2266 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N); |
502 | 2267 #endif |
477 | 2268 case CTRL_X_SPELL: |
2269 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N); | |
7 | 2270 } |
2271 EMSG(_(e_internal)); | |
2272 return FALSE; | |
2273 } | |
2274 | |
2275 /* | |
1430 | 2276 * Return TRUE when character "c" is part of the item currently being |
2277 * completed. Used to decide whether to abandon complete mode when the menu | |
2278 * is visible. | |
2279 */ | |
2280 static int | |
2281 ins_compl_accept_char(c) | |
2282 int c; | |
2283 { | |
2284 if (ctrl_x_mode & CTRL_X_WANT_IDENT) | |
2285 /* When expanding an identifier only accept identifier chars. */ | |
2286 return vim_isIDc(c); | |
2287 | |
2288 switch (ctrl_x_mode) | |
2289 { | |
2290 case CTRL_X_FILES: | |
2291 /* When expanding file name only accept file name chars. But not | |
2292 * path separators, so that "proto/<Tab>" expands files in | |
2293 * "proto", not "proto/" as a whole */ | |
2294 return vim_isfilec(c) && !vim_ispathsep(c); | |
2295 | |
2296 case CTRL_X_CMDLINE: | |
2297 case CTRL_X_OMNI: | |
2298 /* Command line and Omni completion can work with just about any | |
2299 * printable character, but do stop at white space. */ | |
2300 return vim_isprintc(c) && !vim_iswhite(c); | |
2301 | |
2302 case CTRL_X_WHOLE_LINE: | |
2303 /* For while line completion a space can be part of the line. */ | |
2304 return vim_isprintc(c); | |
2305 } | |
2306 return vim_iswordc(c); | |
2307 } | |
2308 | |
2309 /* | |
659 | 2310 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the |
7 | 2311 * case of the originally typed text is used, and the case of the completed |
1219 | 2312 * text is inferred, ie this tries to work out what case you probably wanted |
7 | 2313 * the rest of the word to be in -- webb |
2314 */ | |
2315 int | |
681 | 2316 ins_compl_add_infercase(str, len, icase, fname, dir, flags) |
7 | 2317 char_u *str; |
2318 int len; | |
681 | 2319 int icase; |
7 | 2320 char_u *fname; |
2321 int dir; | |
464 | 2322 int flags; |
7 | 2323 { |
1353 | 2324 char_u *p; |
2325 int i, c; | |
2326 int actual_len; /* Take multi-byte characters */ | |
2327 int actual_compl_length; /* into account. */ | |
2177 | 2328 int min_len; |
2004 | 2329 int *wca; /* Wide character array. */ |
7 | 2330 int has_lower = FALSE; |
2331 int was_letter = FALSE; | |
1353 | 2332 |
1436 | 2333 if (p_ic && curbuf->b_p_inf && len > 0) |
1353 | 2334 { |
2335 /* Infer case of completed part. */ | |
2336 | |
2337 /* Find actual length of completion. */ | |
2338 #ifdef FEAT_MBYTE | |
2339 if (has_mbyte) | |
2340 { | |
2341 p = str; | |
2342 actual_len = 0; | |
2343 while (*p != NUL) | |
2344 { | |
2345 mb_ptr_adv(p); | |
2346 ++actual_len; | |
2347 } | |
2348 } | |
2349 else | |
2350 #endif | |
2351 actual_len = len; | |
2352 | |
2353 /* Find actual length of original text. */ | |
2354 #ifdef FEAT_MBYTE | |
2355 if (has_mbyte) | |
2356 { | |
2357 p = compl_orig_text; | |
2358 actual_compl_length = 0; | |
2359 while (*p != NUL) | |
7 | 2360 { |
1353 | 2361 mb_ptr_adv(p); |
2362 ++actual_compl_length; | |
2363 } | |
2364 } | |
2365 else | |
2366 #endif | |
2367 actual_compl_length = compl_length; | |
2368 | |
2177 | 2369 /* "actual_len" may be smaller than "actual_compl_length" when using |
2370 * thesaurus, only use the minimum when comparing. */ | |
2371 min_len = actual_len < actual_compl_length | |
2372 ? actual_len : actual_compl_length; | |
2373 | |
1353 | 2374 /* Allocate wide character array for the completion and fill it. */ |
1869 | 2375 wca = (int *)alloc((unsigned)(actual_len * sizeof(int))); |
1353 | 2376 if (wca != NULL) |
2377 { | |
2378 p = str; | |
2379 for (i = 0; i < actual_len; ++i) | |
2380 #ifdef FEAT_MBYTE | |
2381 if (has_mbyte) | |
2382 wca[i] = mb_ptr2char_adv(&p); | |
2383 else | |
2384 #endif | |
2385 wca[i] = *(p++); | |
2386 | |
2387 /* Rule 1: Were any chars converted to lower? */ | |
2388 p = compl_orig_text; | |
2177 | 2389 for (i = 0; i < min_len; ++i) |
1353 | 2390 { |
2391 #ifdef FEAT_MBYTE | |
2392 if (has_mbyte) | |
2393 c = mb_ptr2char_adv(&p); | |
2394 else | |
2395 #endif | |
2396 c = *(p++); | |
2397 if (MB_ISLOWER(c)) | |
7 | 2398 { |
1353 | 2399 has_lower = TRUE; |
2400 if (MB_ISUPPER(wca[i])) | |
2401 { | |
2402 /* Rule 1 is satisfied. */ | |
2403 for (i = actual_compl_length; i < actual_len; ++i) | |
2404 wca[i] = MB_TOLOWER(wca[i]); | |
2405 break; | |
2406 } | |
7 | 2407 } |
2408 } | |
1353 | 2409 |
2410 /* | |
2411 * Rule 2: No lower case, 2nd consecutive letter converted to | |
2412 * upper case. | |
2413 */ | |
2414 if (!has_lower) | |
2415 { | |
2416 p = compl_orig_text; | |
2177 | 2417 for (i = 0; i < min_len; ++i) |
1353 | 2418 { |
2419 #ifdef FEAT_MBYTE | |
2420 if (has_mbyte) | |
2421 c = mb_ptr2char_adv(&p); | |
2422 else | |
2423 #endif | |
2424 c = *(p++); | |
2425 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i])) | |
2426 { | |
2427 /* Rule 2 is satisfied. */ | |
2428 for (i = actual_compl_length; i < actual_len; ++i) | |
2429 wca[i] = MB_TOUPPER(wca[i]); | |
2430 break; | |
2431 } | |
2432 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c); | |
2433 } | |
2434 } | |
2435 | |
2436 /* Copy the original case of the part we typed. */ | |
2437 p = compl_orig_text; | |
2177 | 2438 for (i = 0; i < min_len; ++i) |
7 | 2439 { |
1353 | 2440 #ifdef FEAT_MBYTE |
2441 if (has_mbyte) | |
2442 c = mb_ptr2char_adv(&p); | |
2443 else | |
2444 #endif | |
2445 c = *(p++); | |
2446 if (MB_ISLOWER(c)) | |
2447 wca[i] = MB_TOLOWER(wca[i]); | |
2448 else if (MB_ISUPPER(c)) | |
2449 wca[i] = MB_TOUPPER(wca[i]); | |
7 | 2450 } |
1353 | 2451 |
1436 | 2452 /* |
1353 | 2453 * Generate encoding specific output from wide character array. |
2454 * Multi-byte characters can occupy up to five bytes more than | |
2455 * ASCII characters, and we also need one byte for NUL, so stay | |
2456 * six bytes away from the edge of IObuff. | |
2457 */ | |
2458 p = IObuff; | |
2459 i = 0; | |
2460 while (i < actual_len && (p - IObuff + 6) < IOSIZE) | |
2461 #ifdef FEAT_MBYTE | |
2462 if (has_mbyte) | |
1446 | 2463 p += (*mb_char2bytes)(wca[i++], p); |
1353 | 2464 else |
2465 #endif | |
2466 *(p++) = wca[i++]; | |
2467 *p = NUL; | |
2468 | |
2469 vim_free(wca); | |
2470 } | |
7 | 2471 |
841 | 2472 return ins_compl_add(IObuff, len, icase, fname, NULL, dir, |
2473 flags, FALSE); | |
2474 } | |
2475 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE); | |
7 | 2476 } |
2477 | |
2478 /* | |
2479 * Add a match to the list of matches. | |
2480 * If the given string is already in the list of completions, then return | |
464 | 2481 * NOTDONE, otherwise add it to the list and return OK. If there is an error, |
681 | 2482 * maybe because alloc() returns NULL, then FAIL is returned. |
7 | 2483 */ |
841 | 2484 static int |
944 | 2485 ins_compl_add(str, len, icase, fname, cptext, cdir, flags, adup) |
7 | 2486 char_u *str; |
2487 int len; | |
681 | 2488 int icase; |
7 | 2489 char_u *fname; |
841 | 2490 char_u **cptext; /* extra text for popup menu or NULL */ |
659 | 2491 int cdir; |
464 | 2492 int flags; |
944 | 2493 int adup; /* accept duplicate match */ |
7 | 2494 { |
464 | 2495 compl_T *match; |
659 | 2496 int dir = (cdir == 0 ? compl_direction : cdir); |
7 | 2497 |
2498 ui_breakcheck(); | |
2499 if (got_int) | |
464 | 2500 return FAIL; |
7 | 2501 if (len < 0) |
2502 len = (int)STRLEN(str); | |
2503 | |
2504 /* | |
2505 * If the same match is already present, don't add it. | |
2506 */ | |
944 | 2507 if (compl_first_match != NULL && !adup) |
449 | 2508 { |
2509 match = compl_first_match; | |
7 | 2510 do |
2511 { | |
464 | 2512 if ( !(match->cp_flags & ORIGINAL_TEXT) |
989 | 2513 && STRNCMP(match->cp_str, str, len) == 0 |
464 | 2514 && match->cp_str[len] == NUL) |
2515 return NOTDONE; | |
2516 match = match->cp_next; | |
449 | 2517 } while (match != NULL && match != compl_first_match); |
7 | 2518 } |
2519 | |
540 | 2520 /* Remove any popup menu before changing the list of matches. */ |
2521 ins_compl_del_pum(); | |
2522 | |
7 | 2523 /* |
2524 * Allocate a new match structure. | |
2525 * Copy the values to the new match structure. | |
2526 */ | |
659 | 2527 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T)); |
7 | 2528 if (match == NULL) |
464 | 2529 return FAIL; |
2530 match->cp_number = -1; | |
2531 if (flags & ORIGINAL_TEXT) | |
2532 match->cp_number = 0; | |
694 | 2533 if ((match->cp_str = vim_strnsave(str, len)) == NULL) |
7 | 2534 { |
2535 vim_free(match); | |
464 | 2536 return FAIL; |
7 | 2537 } |
681 | 2538 match->cp_icase = icase; |
659 | 2539 |
7 | 2540 /* match-fname is: |
464 | 2541 * - compl_curr_match->cp_fname if it is a string equal to fname. |
7 | 2542 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem. |
2543 * - NULL otherwise. --Acevedo */ | |
659 | 2544 if (fname != NULL |
829 | 2545 && compl_curr_match != NULL |
659 | 2546 && compl_curr_match->cp_fname != NULL |
2547 && STRCMP(fname, compl_curr_match->cp_fname) == 0) | |
464 | 2548 match->cp_fname = compl_curr_match->cp_fname; |
659 | 2549 else if (fname != NULL) |
2550 { | |
2551 match->cp_fname = vim_strsave(fname); | |
464 | 2552 flags |= FREE_FNAME; |
659 | 2553 } |
7 | 2554 else |
464 | 2555 match->cp_fname = NULL; |
2556 match->cp_flags = flags; | |
786 | 2557 |
2558 if (cptext != NULL) | |
2559 { | |
2560 int i; | |
2561 | |
2562 for (i = 0; i < CPT_COUNT; ++i) | |
2563 if (cptext[i] != NULL && *cptext[i] != NUL) | |
2564 match->cp_text[i] = vim_strsave(cptext[i]); | |
2565 } | |
7 | 2566 |
2567 /* | |
2568 * Link the new match structure in the list of matches. | |
2569 */ | |
449 | 2570 if (compl_first_match == NULL) |
464 | 2571 match->cp_next = match->cp_prev = NULL; |
7 | 2572 else if (dir == FORWARD) |
2573 { | |
464 | 2574 match->cp_next = compl_curr_match->cp_next; |
2575 match->cp_prev = compl_curr_match; | |
7 | 2576 } |
2577 else /* BACKWARD */ | |
2578 { | |
464 | 2579 match->cp_next = compl_curr_match; |
2580 match->cp_prev = compl_curr_match->cp_prev; | |
2581 } | |
2582 if (match->cp_next) | |
2583 match->cp_next->cp_prev = match; | |
2584 if (match->cp_prev) | |
2585 match->cp_prev->cp_next = match; | |
7 | 2586 else /* if there's nothing before, it is the first match */ |
449 | 2587 compl_first_match = match; |
2588 compl_curr_match = match; | |
7 | 2589 |
665 | 2590 /* |
2591 * Find the longest common string if still doing that. | |
2592 */ | |
2593 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0) | |
2594 ins_compl_longest_match(match); | |
2595 | |
7 | 2596 return OK; |
2597 } | |
2598 | |
2599 /* | |
681 | 2600 * Return TRUE if "str[len]" matches with match->cp_str, considering |
2601 * match->cp_icase. | |
2602 */ | |
2603 static int | |
2604 ins_compl_equal(match, str, len) | |
2605 compl_T *match; | |
2606 char_u *str; | |
2607 int len; | |
2608 { | |
2609 if (match->cp_icase) | |
2610 return STRNICMP(match->cp_str, str, (size_t)len) == 0; | |
2611 return STRNCMP(match->cp_str, str, (size_t)len) == 0; | |
2612 } | |
2613 | |
2614 /* | |
665 | 2615 * Reduce the longest common string for match "match". |
2616 */ | |
2617 static void | |
2618 ins_compl_longest_match(match) | |
2619 compl_T *match; | |
2620 { | |
2621 char_u *p, *s; | |
681 | 2622 int c1, c2; |
665 | 2623 int had_match; |
2624 | |
2625 if (compl_leader == NULL) | |
842 | 2626 { |
665 | 2627 /* First match, use it as a whole. */ |
2628 compl_leader = vim_strsave(match->cp_str); | |
842 | 2629 if (compl_leader != NULL) |
2630 { | |
2631 had_match = (curwin->w_cursor.col > compl_col); | |
2632 ins_compl_delete(); | |
1782 | 2633 ins_bytes(compl_leader + ins_compl_len()); |
842 | 2634 ins_redraw(FALSE); |
2635 | |
2636 /* When the match isn't there (to avoid matching itself) remove it | |
2637 * again after redrawing. */ | |
2638 if (!had_match) | |
2639 ins_compl_delete(); | |
2640 compl_used_match = FALSE; | |
2641 } | |
2642 } | |
665 | 2643 else |
2644 { | |
2645 /* Reduce the text if this match differs from compl_leader. */ | |
681 | 2646 p = compl_leader; |
2647 s = match->cp_str; | |
2648 while (*p != NUL) | |
665 | 2649 { |
2650 #ifdef FEAT_MBYTE | |
2651 if (has_mbyte) | |
2652 { | |
681 | 2653 c1 = mb_ptr2char(p); |
2654 c2 = mb_ptr2char(s); | |
665 | 2655 } |
2656 else | |
2657 #endif | |
2658 { | |
681 | 2659 c1 = *p; |
2660 c2 = *s; | |
2661 } | |
2662 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2)) | |
2663 : (c1 != c2)) | |
2664 break; | |
2665 #ifdef FEAT_MBYTE | |
2666 if (has_mbyte) | |
2667 { | |
2668 mb_ptr_adv(p); | |
2669 mb_ptr_adv(s); | |
2670 } | |
2671 else | |
2672 #endif | |
2673 { | |
2674 ++p; | |
2675 ++s; | |
665 | 2676 } |
2677 } | |
2678 | |
2679 if (*p != NUL) | |
2680 { | |
2681 /* Leader was shortened, need to change the inserted text. */ | |
2682 *p = NUL; | |
2683 had_match = (curwin->w_cursor.col > compl_col); | |
2684 ins_compl_delete(); | |
1782 | 2685 ins_bytes(compl_leader + ins_compl_len()); |
665 | 2686 ins_redraw(FALSE); |
2687 | |
2688 /* When the match isn't there (to avoid matching itself) remove it | |
2689 * again after redrawing. */ | |
2690 if (!had_match) | |
2691 ins_compl_delete(); | |
2692 } | |
2693 | |
2694 compl_used_match = FALSE; | |
2695 } | |
2696 } | |
2697 | |
2698 /* | |
7 | 2699 * Add an array of matches to the list of matches. |
2700 * Frees matches[]. | |
2701 */ | |
2702 static void | |
681 | 2703 ins_compl_add_matches(num_matches, matches, icase) |
7 | 2704 int num_matches; |
2705 char_u **matches; | |
681 | 2706 int icase; |
7 | 2707 { |
2708 int i; | |
2709 int add_r = OK; | |
659 | 2710 int dir = compl_direction; |
7 | 2711 |
464 | 2712 for (i = 0; i < num_matches && add_r != FAIL; i++) |
681 | 2713 if ((add_r = ins_compl_add(matches[i], -1, icase, |
841 | 2714 NULL, NULL, dir, 0, FALSE)) == OK) |
7 | 2715 /* if dir was BACKWARD then honor it just once */ |
659 | 2716 dir = FORWARD; |
7 | 2717 FreeWild(num_matches, matches); |
2718 } | |
2719 | |
2720 /* Make the completion list cyclic. | |
2721 * Return the number of matches (excluding the original). | |
2722 */ | |
2723 static int | |
2724 ins_compl_make_cyclic() | |
2725 { | |
464 | 2726 compl_T *match; |
7 | 2727 int count = 0; |
2728 | |
449 | 2729 if (compl_first_match != NULL) |
7 | 2730 { |
2731 /* | |
2732 * Find the end of the list. | |
2733 */ | |
449 | 2734 match = compl_first_match; |
2735 /* there's always an entry for the compl_orig_text, it doesn't count. */ | |
464 | 2736 while (match->cp_next != NULL && match->cp_next != compl_first_match) |
2737 { | |
2738 match = match->cp_next; | |
7 | 2739 ++count; |
2740 } | |
464 | 2741 match->cp_next = compl_first_match; |
2742 compl_first_match->cp_prev = match; | |
7 | 2743 } |
2744 return count; | |
2745 } | |
2746 | |
724 | 2747 /* |
2748 * Start completion for the complete() function. | |
2749 * "startcol" is where the matched text starts (1 is first column). | |
2750 * "list" is the list of matches. | |
2751 */ | |
2752 void | |
2753 set_completion(startcol, list) | |
1869 | 2754 colnr_T startcol; |
724 | 2755 list_T *list; |
2756 { | |
2757 /* If already doing completions stop it. */ | |
2758 if (ctrl_x_mode != 0) | |
2759 ins_compl_prep(' '); | |
2760 ins_compl_clear(); | |
2761 | |
2762 if (stop_arrow() == FAIL) | |
2763 return; | |
2764 | |
2632 | 2765 compl_direction = FORWARD; |
1869 | 2766 if (startcol > curwin->w_cursor.col) |
724 | 2767 startcol = curwin->w_cursor.col; |
2768 compl_col = startcol; | |
1869 | 2769 compl_length = (int)curwin->w_cursor.col - (int)startcol; |
724 | 2770 /* compl_pattern doesn't need to be set */ |
2771 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length); | |
2772 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text, | |
942 | 2773 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK) |
724 | 2774 return; |
2775 | |
2776 /* Handle like dictionary completion. */ | |
2777 ctrl_x_mode = CTRL_X_WHOLE_LINE; | |
2778 | |
2779 ins_compl_add_list(list); | |
2780 compl_matches = ins_compl_make_cyclic(); | |
2781 compl_started = TRUE; | |
2782 compl_used_match = TRUE; | |
929 | 2783 compl_cont_status = 0; |
724 | 2784 |
2785 compl_curr_match = compl_first_match; | |
2786 ins_complete(Ctrl_N); | |
2787 out_flush(); | |
2788 } | |
2789 | |
2790 | |
574 | 2791 /* "compl_match_array" points the currently displayed list of entries in the |
2792 * popup menu. It is NULL when there is no popup menu. */ | |
659 | 2793 static pumitem_T *compl_match_array = NULL; |
540 | 2794 static int compl_match_arraysize; |
2795 | |
2796 /* | |
2797 * Update the screen and when there is any scrolling remove the popup menu. | |
2798 */ | |
2799 static void | |
2800 ins_compl_upd_pum() | |
2801 { | |
2802 int h; | |
2803 | |
2804 if (compl_match_array != NULL) | |
2805 { | |
2806 h = curwin->w_cline_height; | |
2807 update_screen(0); | |
2808 if (h != curwin->w_cline_height) | |
2809 ins_compl_del_pum(); | |
2810 } | |
2811 } | |
2812 | |
2813 /* | |
2814 * Remove any popup menu. | |
2815 */ | |
2816 static void | |
2817 ins_compl_del_pum() | |
2818 { | |
2819 if (compl_match_array != NULL) | |
2820 { | |
2821 pum_undisplay(); | |
2822 vim_free(compl_match_array); | |
2823 compl_match_array = NULL; | |
2824 } | |
2825 } | |
2826 | |
2827 /* | |
2828 * Return TRUE if the popup menu should be displayed. | |
2829 */ | |
2830 static int | |
2831 pum_wanted() | |
2832 { | |
707 | 2833 /* 'completeopt' must contain "menu" or "menuone" */ |
665 | 2834 if (vim_strchr(p_cot, 'm') == NULL) |
540 | 2835 return FALSE; |
2836 | |
2837 /* The display looks bad on a B&W display. */ | |
2838 if (t_colors < 8 | |
2839 #ifdef FEAT_GUI | |
2840 && !gui.in_use | |
2841 #endif | |
2842 ) | |
2843 return FALSE; | |
657 | 2844 return TRUE; |
2845 } | |
2846 | |
2847 /* | |
2848 * Return TRUE if there are two or more matches to be shown in the popup menu. | |
707 | 2849 * One if 'completopt' contains "menuone". |
657 | 2850 */ |
2851 static int | |
707 | 2852 pum_enough_matches() |
657 | 2853 { |
2854 compl_T *compl; | |
2855 int i; | |
540 | 2856 |
2857 /* Don't display the popup menu if there are no matches or there is only | |
2858 * one (ignoring the original text). */ | |
2859 compl = compl_first_match; | |
2860 i = 0; | |
2861 do | |
2862 { | |
2863 if (compl == NULL | |
2864 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2)) | |
2865 break; | |
2866 compl = compl->cp_next; | |
2867 } while (compl != compl_first_match); | |
2868 | |
707 | 2869 if (strstr((char *)p_cot, "menuone") != NULL) |
2870 return (i >= 1); | |
540 | 2871 return (i >= 2); |
2872 } | |
2873 | |
2874 /* | |
2875 * Show the popup menu for the list of matches. | |
659 | 2876 * Also adjusts "compl_shown_match" to an entry that is actually displayed. |
540 | 2877 */ |
648 | 2878 void |
540 | 2879 ins_compl_show_pum() |
2880 { | |
2881 compl_T *compl; | |
659 | 2882 compl_T *shown_compl = NULL; |
2883 int did_find_shown_match = FALSE; | |
2884 int shown_match_ok = FALSE; | |
540 | 2885 int i; |
2886 int cur = -1; | |
2887 colnr_T col; | |
657 | 2888 int lead_len = 0; |
2889 | |
707 | 2890 if (!pum_wanted() || !pum_enough_matches()) |
540 | 2891 return; |
2892 | |
794 | 2893 #if defined(FEAT_EVAL) |
2894 /* Dirty hard-coded hack: remove any matchparen highlighting. */ | |
2895 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif"); | |
2896 #endif | |
2897 | |
540 | 2898 /* Update the screen before drawing the popup menu over it. */ |
2899 update_screen(0); | |
2900 | |
2901 if (compl_match_array == NULL) | |
2902 { | |
2903 /* Need to build the popup menu list. */ | |
2904 compl_match_arraysize = 0; | |
2905 compl = compl_first_match; | |
657 | 2906 if (compl_leader != NULL) |
835 | 2907 lead_len = (int)STRLEN(compl_leader); |
540 | 2908 do |
2909 { | |
657 | 2910 if ((compl->cp_flags & ORIGINAL_TEXT) == 0 |
2911 && (compl_leader == NULL | |
681 | 2912 || ins_compl_equal(compl, compl_leader, lead_len))) |
540 | 2913 ++compl_match_arraysize; |
2914 compl = compl->cp_next; | |
2915 } while (compl != NULL && compl != compl_first_match); | |
657 | 2916 if (compl_match_arraysize == 0) |
2917 return; | |
659 | 2918 compl_match_array = (pumitem_T *)alloc_clear( |
2919 (unsigned)(sizeof(pumitem_T) | |
540 | 2920 * compl_match_arraysize)); |
2921 if (compl_match_array != NULL) | |
2922 { | |
829 | 2923 /* If the current match is the original text don't find the first |
2924 * match after it, don't highlight anything. */ | |
2925 if (compl_shown_match->cp_flags & ORIGINAL_TEXT) | |
2926 shown_match_ok = TRUE; | |
2927 | |
540 | 2928 i = 0; |
2929 compl = compl_first_match; | |
2930 do | |
2931 { | |
657 | 2932 if ((compl->cp_flags & ORIGINAL_TEXT) == 0 |
2933 && (compl_leader == NULL | |
681 | 2934 || ins_compl_equal(compl, compl_leader, lead_len))) |
540 | 2935 { |
659 | 2936 if (!shown_match_ok) |
2937 { | |
2938 if (compl == compl_shown_match || did_find_shown_match) | |
2939 { | |
2940 /* This item is the shown match or this is the | |
2941 * first displayed item after the shown match. */ | |
2942 compl_shown_match = compl; | |
2943 did_find_shown_match = TRUE; | |
2944 shown_match_ok = TRUE; | |
2945 } | |
2946 else | |
2947 /* Remember this displayed match for when the | |
2948 * shown match is just below it. */ | |
2949 shown_compl = compl; | |
540 | 2950 cur = i; |
659 | 2951 } |
786 | 2952 |
2953 if (compl->cp_text[CPT_ABBR] != NULL) | |
2954 compl_match_array[i].pum_text = | |
2955 compl->cp_text[CPT_ABBR]; | |
2956 else | |
2957 compl_match_array[i].pum_text = compl->cp_str; | |
2958 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND]; | |
2959 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO]; | |
2960 if (compl->cp_text[CPT_MENU] != NULL) | |
2961 compl_match_array[i++].pum_extra = | |
2962 compl->cp_text[CPT_MENU]; | |
659 | 2963 else |
2964 compl_match_array[i++].pum_extra = compl->cp_fname; | |
2965 } | |
2966 | |
2967 if (compl == compl_shown_match) | |
2968 { | |
2969 did_find_shown_match = TRUE; | |
716 | 2970 |
2971 /* When the original text is the shown match don't set | |
2972 * compl_shown_match. */ | |
2973 if (compl->cp_flags & ORIGINAL_TEXT) | |
2974 shown_match_ok = TRUE; | |
2975 | |
659 | 2976 if (!shown_match_ok && shown_compl != NULL) |
2977 { | |
2978 /* The shown match isn't displayed, set it to the | |
2979 * previously displayed match. */ | |
2980 compl_shown_match = shown_compl; | |
2981 shown_match_ok = TRUE; | |
2982 } | |
540 | 2983 } |
2984 compl = compl->cp_next; | |
2985 } while (compl != NULL && compl != compl_first_match); | |
659 | 2986 |
2987 if (!shown_match_ok) /* no displayed match at all */ | |
2988 cur = -1; | |
540 | 2989 } |
2990 } | |
2991 else | |
2992 { | |
2993 /* popup menu already exists, only need to find the current item.*/ | |
657 | 2994 for (i = 0; i < compl_match_arraysize; ++i) |
786 | 2995 if (compl_match_array[i].pum_text == compl_shown_match->cp_str |
2996 || compl_match_array[i].pum_text | |
2997 == compl_shown_match->cp_text[CPT_ABBR]) | |
829 | 2998 { |
2999 cur = i; | |
657 | 3000 break; |
829 | 3001 } |
540 | 3002 } |
3003 | |
3004 if (compl_match_array != NULL) | |
3005 { | |
3006 /* Compute the screen column of the start of the completed text. | |
3007 * Use the cursor to get all wrapping and other settings right. */ | |
3008 col = curwin->w_cursor.col; | |
3009 curwin->w_cursor.col = compl_col; | |
732 | 3010 pum_display(compl_match_array, compl_match_arraysize, cur); |
540 | 3011 curwin->w_cursor.col = col; |
3012 } | |
3013 } | |
3014 | |
7 | 3015 #define DICT_FIRST (1) /* use just first element in "dict" */ |
3016 #define DICT_EXACT (2) /* "dict" is the exact name of a file */ | |
648 | 3017 |
7 | 3018 /* |
703 | 3019 * Add any identifiers that match the given pattern in the list of dictionary |
3020 * files "dict_start" to the list of completions. | |
7 | 3021 */ |
3022 static void | |
703 | 3023 ins_compl_dictionaries(dict_start, pat, flags, thesaurus) |
3024 char_u *dict_start; | |
7 | 3025 char_u *pat; |
667 | 3026 int flags; /* DICT_FIRST and/or DICT_EXACT */ |
703 | 3027 int thesaurus; /* Thesaurus completion */ |
3028 { | |
3029 char_u *dict = dict_start; | |
7 | 3030 char_u *ptr; |
3031 char_u *buf; | |
3032 regmatch_T regmatch; | |
3033 char_u **files; | |
3034 int count; | |
3035 int save_p_scs; | |
659 | 3036 int dir = compl_direction; |
7 | 3037 |
703 | 3038 if (*dict == NUL) |
3039 { | |
744 | 3040 #ifdef FEAT_SPELL |
703 | 3041 /* When 'dictionary' is empty and spell checking is enabled use |
3042 * "spell". */ | |
3043 if (!thesaurus && curwin->w_p_spell) | |
3044 dict = (char_u *)"spell"; | |
3045 else | |
3046 #endif | |
3047 return; | |
3048 } | |
3049 | |
7 | 3050 buf = alloc(LSIZE); |
703 | 3051 if (buf == NULL) |
3052 return; | |
1074 | 3053 regmatch.regprog = NULL; /* so that we can goto theend */ |
703 | 3054 |
7 | 3055 /* If 'infercase' is set, don't use 'smartcase' here */ |
3056 save_p_scs = p_scs; | |
3057 if (curbuf->b_p_inf) | |
3058 p_scs = FALSE; | |
667 | 3059 |
3060 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern | |
3061 * to only match at the start of a line. Otherwise just match the | |
842 | 3062 * pattern. Also need to double backslashes. */ |
667 | 3063 if (ctrl_x_mode == CTRL_X_WHOLE_LINE) |
3064 { | |
842 | 3065 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\"); |
1869 | 3066 size_t len; |
842 | 3067 |
3068 if (pat_esc == NULL) | |
1159 | 3069 goto theend; |
1869 | 3070 len = STRLEN(pat_esc) + 10; |
3071 ptr = alloc((unsigned)len); | |
667 | 3072 if (ptr == NULL) |
842 | 3073 { |
3074 vim_free(pat_esc); | |
1074 | 3075 goto theend; |
842 | 3076 } |
1869 | 3077 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc); |
842 | 3078 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC); |
3079 vim_free(pat_esc); | |
667 | 3080 vim_free(ptr); |
3081 } | |
3082 else | |
703 | 3083 { |
667 | 3084 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0); |
703 | 3085 if (regmatch.regprog == NULL) |
3086 goto theend; | |
3087 } | |
667 | 3088 |
7 | 3089 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */ |
3090 regmatch.rm_ic = ignorecase(pat); | |
703 | 3091 while (*dict != NUL && !got_int && !compl_interrupted) |
7 | 3092 { |
3093 /* copy one dictionary file name into buf */ | |
3094 if (flags == DICT_EXACT) | |
3095 { | |
3096 count = 1; | |
3097 files = &dict; | |
3098 } | |
3099 else | |
3100 { | |
3101 /* Expand wildcards in the dictionary name, but do not allow | |
3102 * backticks (for security, the 'dict' option may have been set in | |
3103 * a modeline). */ | |
3104 copy_option_part(&dict, buf, LSIZE, ","); | |
744 | 3105 # ifdef FEAT_SPELL |
703 | 3106 if (!thesaurus && STRCMP(buf, "spell") == 0) |
3107 count = -1; | |
744 | 3108 else |
3109 # endif | |
3110 if (vim_strchr(buf, '`') != NULL | |
7 | 3111 || expand_wildcards(1, &buf, &count, &files, |
3112 EW_FILE|EW_SILENT) != OK) | |
3113 count = 0; | |
3114 } | |
3115 | |
744 | 3116 # ifdef FEAT_SPELL |
703 | 3117 if (count == -1) |
3118 { | |
712 | 3119 /* Complete from active spelling. Skip "\<" in the pattern, we |
3120 * don't use it as a RE. */ | |
703 | 3121 if (pat[0] == '\\' && pat[1] == '<') |
3122 ptr = pat + 2; | |
3123 else | |
3124 ptr = pat; | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2227
diff
changeset
|
3125 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0); |
703 | 3126 } |
3127 else | |
744 | 3128 # endif |
938 | 3129 if (count > 0) /* avoid warning for using "files" uninit */ |
703 | 3130 { |
3131 ins_compl_files(count, files, thesaurus, flags, | |
3132 ®match, buf, &dir); | |
7 | 3133 if (flags != DICT_EXACT) |
703 | 3134 FreeWild(count, files); |
3135 } | |
3136 if (flags != 0) | |
7 | 3137 break; |
3138 } | |
703 | 3139 |
3140 theend: | |
7 | 3141 p_scs = save_p_scs; |
4805
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4448
diff
changeset
|
3142 vim_regfree(regmatch.regprog); |
7 | 3143 vim_free(buf); |
3144 } | |
3145 | |
703 | 3146 static void |
3147 ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir) | |
3148 int count; | |
3149 char_u **files; | |
3150 int thesaurus; | |
3151 int flags; | |
3152 regmatch_T *regmatch; | |
3153 char_u *buf; | |
3154 int *dir; | |
3155 { | |
3156 char_u *ptr; | |
3157 int i; | |
3158 FILE *fp; | |
3159 int add_r; | |
3160 | |
3161 for (i = 0; i < count && !got_int && !compl_interrupted; i++) | |
3162 { | |
3163 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */ | |
3164 if (flags != DICT_EXACT) | |
3165 { | |
3166 vim_snprintf((char *)IObuff, IOSIZE, | |
3167 _("Scanning dictionary: %s"), (char *)files[i]); | |
1869 | 3168 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R)); |
703 | 3169 } |
3170 | |
3171 if (fp != NULL) | |
3172 { | |
3173 /* | |
3174 * Read dictionary file line by line. | |
3175 * Check each line for a match. | |
3176 */ | |
3177 while (!got_int && !compl_interrupted | |
3178 && !vim_fgets(buf, LSIZE, fp)) | |
3179 { | |
3180 ptr = buf; | |
3181 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf))) | |
3182 { | |
3183 ptr = regmatch->startp[0]; | |
3184 if (ctrl_x_mode == CTRL_X_WHOLE_LINE) | |
3185 ptr = find_line_end(ptr); | |
3186 else | |
3187 ptr = find_word_end(ptr); | |
3188 add_r = ins_compl_add_infercase(regmatch->startp[0], | |
3189 (int)(ptr - regmatch->startp[0]), | |
942 | 3190 p_ic, files[i], *dir, 0); |
703 | 3191 if (thesaurus) |
3192 { | |
3193 char_u *wstart; | |
3194 | |
3195 /* | |
3196 * Add the other matches on the line | |
3197 */ | |
1353 | 3198 ptr = buf; |
703 | 3199 while (!got_int) |
3200 { | |
3201 /* Find start of the next word. Skip white | |
3202 * space and punctuation. */ | |
3203 ptr = find_word_start(ptr); | |
3204 if (*ptr == NUL || *ptr == NL) | |
3205 break; | |
3206 wstart = ptr; | |
3207 | |
1353 | 3208 /* Find end of the word. */ |
703 | 3209 #ifdef FEAT_MBYTE |
3210 if (has_mbyte) | |
3211 /* Japanese words may have characters in | |
3212 * different classes, only separate words | |
3213 * with single-byte non-word characters. */ | |
3214 while (*ptr != NUL) | |
3215 { | |
3216 int l = (*mb_ptr2len)(ptr); | |
3217 | |
3218 if (l < 2 && !vim_iswordc(*ptr)) | |
3219 break; | |
3220 ptr += l; | |
3221 } | |
3222 else | |
3223 #endif | |
3224 ptr = find_word_end(ptr); | |
1353 | 3225 |
3226 /* Add the word. Skip the regexp match. */ | |
3227 if (wstart != regmatch->startp[0]) | |
3228 add_r = ins_compl_add_infercase(wstart, | |
3229 (int)(ptr - wstart), | |
3230 p_ic, files[i], *dir, 0); | |
703 | 3231 } |
3232 } | |
3233 if (add_r == OK) | |
3234 /* if dir was BACKWARD then honor it just once */ | |
3235 *dir = FORWARD; | |
3236 else if (add_r == FAIL) | |
3237 break; | |
3238 /* avoid expensive call to vim_regexec() when at end | |
3239 * of line */ | |
3240 if (*ptr == '\n' || got_int) | |
3241 break; | |
3242 } | |
3243 line_breakcheck(); | |
3244 ins_compl_check_keys(50); | |
3245 } | |
3246 fclose(fp); | |
3247 } | |
3248 } | |
3249 } | |
3250 | |
7 | 3251 /* |
3252 * Find the start of the next word. | |
3253 * Returns a pointer to the first char of the word. Also stops at a NUL. | |
3254 */ | |
3255 char_u * | |
3256 find_word_start(ptr) | |
3257 char_u *ptr; | |
3258 { | |
3259 #ifdef FEAT_MBYTE | |
3260 if (has_mbyte) | |
3261 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1) | |
474 | 3262 ptr += (*mb_ptr2len)(ptr); |
7 | 3263 else |
3264 #endif | |
3265 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr)) | |
3266 ++ptr; | |
3267 return ptr; | |
3268 } | |
3269 | |
3270 /* | |
3271 * Find the end of the word. Assumes it starts inside a word. | |
3272 * Returns a pointer to just after the word. | |
3273 */ | |
3274 char_u * | |
3275 find_word_end(ptr) | |
3276 char_u *ptr; | |
3277 { | |
3278 #ifdef FEAT_MBYTE | |
3279 int start_class; | |
3280 | |
3281 if (has_mbyte) | |
3282 { | |
3283 start_class = mb_get_class(ptr); | |
3284 if (start_class > 1) | |
3285 while (*ptr != NUL) | |
3286 { | |
474 | 3287 ptr += (*mb_ptr2len)(ptr); |
7 | 3288 if (mb_get_class(ptr) != start_class) |
3289 break; | |
3290 } | |
3291 } | |
3292 else | |
3293 #endif | |
3294 while (vim_iswordc(*ptr)) | |
3295 ++ptr; | |
3296 return ptr; | |
3297 } | |
3298 | |
3299 /* | |
667 | 3300 * Find the end of the line, omitting CR and NL at the end. |
3301 * Returns a pointer to just after the line. | |
3302 */ | |
3303 static char_u * | |
3304 find_line_end(ptr) | |
3305 char_u *ptr; | |
3306 { | |
3307 char_u *s; | |
3308 | |
3309 s = ptr + STRLEN(ptr); | |
3310 while (s > ptr && (s[-1] == CAR || s[-1] == NL)) | |
3311 --s; | |
3312 return s; | |
3313 } | |
3314 | |
3315 /* | |
7 | 3316 * Free the list of completions |
3317 */ | |
3318 static void | |
3319 ins_compl_free() | |
3320 { | |
464 | 3321 compl_T *match; |
786 | 3322 int i; |
7 | 3323 |
449 | 3324 vim_free(compl_pattern); |
3325 compl_pattern = NULL; | |
657 | 3326 vim_free(compl_leader); |
3327 compl_leader = NULL; | |
449 | 3328 |
3329 if (compl_first_match == NULL) | |
7 | 3330 return; |
540 | 3331 |
3332 ins_compl_del_pum(); | |
3333 pum_clear(); | |
3334 | |
449 | 3335 compl_curr_match = compl_first_match; |
7 | 3336 do |
3337 { | |
449 | 3338 match = compl_curr_match; |
464 | 3339 compl_curr_match = compl_curr_match->cp_next; |
3340 vim_free(match->cp_str); | |
7 | 3341 /* several entries may use the same fname, free it just once. */ |
464 | 3342 if (match->cp_flags & FREE_FNAME) |
3343 vim_free(match->cp_fname); | |
786 | 3344 for (i = 0; i < CPT_COUNT; ++i) |
3345 vim_free(match->cp_text[i]); | |
7 | 3346 vim_free(match); |
449 | 3347 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match); |
3348 compl_first_match = compl_curr_match = NULL; | |
1927 | 3349 compl_shown_match = NULL; |
7 | 3350 } |
3351 | |
3352 static void | |
3353 ins_compl_clear() | |
3354 { | |
449 | 3355 compl_cont_status = 0; |
3356 compl_started = FALSE; | |
3357 compl_matches = 0; | |
3358 vim_free(compl_pattern); | |
3359 compl_pattern = NULL; | |
657 | 3360 vim_free(compl_leader); |
3361 compl_leader = NULL; | |
7 | 3362 edit_submode_extra = NULL; |
724 | 3363 vim_free(compl_orig_text); |
3364 compl_orig_text = NULL; | |
825 | 3365 compl_enter_selects = FALSE; |
7 | 3366 } |
3367 | |
3368 /* | |
674 | 3369 * Return TRUE when Insert completion is active. |
3370 */ | |
3371 int | |
3372 ins_compl_active() | |
3373 { | |
3374 return compl_started; | |
3375 } | |
3376 | |
3377 /* | |
659 | 3378 * Delete one character before the cursor and show the subset of the matches |
3379 * that match the word that is now before the cursor. | |
836 | 3380 * Returns the character to be used, NUL if the work is done and another char |
3381 * to be got from the user. | |
657 | 3382 */ |
3383 static int | |
3384 ins_compl_bs() | |
3385 { | |
3386 char_u *line; | |
3387 char_u *p; | |
3388 | |
836 | 3389 line = ml_get_curline(); |
3390 p = line + curwin->w_cursor.col; | |
3391 mb_ptr_back(line, p); | |
3392 | |
1430 | 3393 /* Stop completion when the whole word was deleted. For Omni completion |
3394 * allow the word to be deleted, we won't match everything. */ | |
3395 if ((int)(p - line) - (int)compl_col < 0 | |
3396 || ((int)(p - line) - (int)compl_col == 0 | |
6526 | 3397 && ctrl_x_mode != CTRL_X_OMNI)) |
836 | 3398 return K_BS; |
3399 | |
874 | 3400 /* Deleted more than what was used to find matches or didn't finish |
3401 * finding all matches: need to look for matches all over again. */ | |
3402 if (curwin->w_cursor.col <= compl_col + compl_length | |
3078 | 3403 || ins_compl_need_restart()) |
874 | 3404 ins_compl_restart(); |
657 | 3405 |
3406 vim_free(compl_leader); | |
835 | 3407 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col); |
657 | 3408 if (compl_leader != NULL) |
3409 { | |
874 | 3410 ins_compl_new_leader(); |
4205 | 3411 if (compl_shown_match != NULL) |
3412 /* Make sure current match is not a hidden item. */ | |
3413 compl_curr_match = compl_shown_match; | |
874 | 3414 return NUL; |
3415 } | |
3416 return K_BS; | |
3417 } | |
3418 | |
3419 /* | |
3078 | 3420 * Return TRUE when we need to find matches again, ins_compl_restart() is to |
3421 * be called. | |
3422 */ | |
3423 static int | |
3424 ins_compl_need_restart() | |
3425 { | |
3426 /* Return TRUE if we didn't complete finding matches or when the | |
3427 * 'completefunc' returned "always" in the "refresh" dictionary item. */ | |
3428 return compl_was_interrupted | |
3429 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI) | |
3430 && compl_opt_refresh_always); | |
3431 } | |
3432 | |
3433 /* | |
874 | 3434 * Called after changing "compl_leader". |
3435 * Show the popup menu with a different set of matches. | |
3436 * May also search for matches again if the previous search was interrupted. | |
3437 */ | |
3438 static void | |
3439 ins_compl_new_leader() | |
3440 { | |
3441 ins_compl_del_pum(); | |
3442 ins_compl_delete(); | |
1782 | 3443 ins_bytes(compl_leader + ins_compl_len()); |
874 | 3444 compl_used_match = FALSE; |
3445 | |
3446 if (compl_started) | |
3447 ins_compl_set_original_text(compl_leader); | |
3448 else | |
3449 { | |
826 | 3450 #ifdef FEAT_SPELL |
874 | 3451 spell_bad_len = 0; /* need to redetect bad word */ |
3452 #endif | |
3453 /* | |
3454 * Matches were cleared, need to search for them now. First display | |
3455 * the changed text before the cursor. Set "compl_restarting" to | |
3456 * avoid that the first match is inserted. | |
3457 */ | |
3458 update_screen(0); | |
3459 #ifdef FEAT_GUI | |
3460 if (gui.in_use) | |
3461 { | |
3462 /* Show the cursor after the match, not after the redrawn text. */ | |
3463 setcursor(); | |
3464 out_flush(); | |
3465 gui_update_cursor(FALSE, FALSE); | |
3466 } | |
3467 #endif | |
3468 compl_restarting = TRUE; | |
3469 if (ins_complete(Ctrl_N) == FAIL) | |
3470 compl_cont_status = 0; | |
3471 compl_restarting = FALSE; | |
3472 } | |
3473 | |
887 | 3474 compl_enter_selects = !compl_used_match; |
874 | 3475 |
3476 /* Show the popup menu with a different set of matches. */ | |
3477 ins_compl_show_pum(); | |
947 | 3478 |
3479 /* Don't let Enter select the original text when there is no popup menu. */ | |
3480 if (compl_match_array == NULL) | |
3481 compl_enter_selects = FALSE; | |
657 | 3482 } |
3483 | |
3484 /* | |
1782 | 3485 * Return the length of the completion, from the completion start column to |
3486 * the cursor column. Making sure it never goes below zero. | |
3487 */ | |
3488 static int | |
3489 ins_compl_len() | |
3490 { | |
1869 | 3491 int off = (int)curwin->w_cursor.col - (int)compl_col; |
1782 | 3492 |
3493 if (off < 0) | |
3494 return 0; | |
3495 return off; | |
3496 } | |
3497 | |
3498 /* | |
657 | 3499 * Append one character to the match leader. May reduce the number of |
3500 * matches. | |
3501 */ | |
3502 static void | |
3503 ins_compl_addleader(c) | |
3504 int c; | |
3505 { | |
3506 #ifdef FEAT_MBYTE | |
3507 int cc; | |
3508 | |
3509 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1) | |
3510 { | |
3511 char_u buf[MB_MAXBYTES + 1]; | |
3512 | |
3513 (*mb_char2bytes)(c, buf); | |
3514 buf[cc] = NUL; | |
3515 ins_char_bytes(buf, cc); | |
3612 | 3516 if (compl_opt_refresh_always) |
3517 AppendToRedobuff(buf); | |
657 | 3518 } |
3519 else | |
3520 #endif | |
3602 | 3521 { |
657 | 3522 ins_char(c); |
3612 | 3523 if (compl_opt_refresh_always) |
3524 AppendCharToRedobuff(c); | |
3602 | 3525 } |
657 | 3526 |
874 | 3527 /* If we didn't complete finding matches we must search again. */ |
3078 | 3528 if (ins_compl_need_restart()) |
874 | 3529 ins_compl_restart(); |
3530 | |
3271 | 3531 /* When 'always' is set, don't reset compl_leader. While completing, |
3612 | 3532 * cursor doesn't point original position, changing compl_leader would |
3271 | 3533 * break redo. */ |
3534 if (!compl_opt_refresh_always) | |
3535 { | |
3536 vim_free(compl_leader); | |
3537 compl_leader = vim_strnsave(ml_get_curline() + compl_col, | |
1869 | 3538 (int)(curwin->w_cursor.col - compl_col)); |
3271 | 3539 if (compl_leader != NULL) |
3540 ins_compl_new_leader(); | |
3541 } | |
874 | 3542 } |
3543 | |
3544 /* | |
3545 * Setup for finding completions again without leaving CTRL-X mode. Used when | |
3546 * BS or a key was typed while still searching for matches. | |
3547 */ | |
3548 static void | |
3549 ins_compl_restart() | |
3550 { | |
3551 ins_compl_free(); | |
3552 compl_started = FALSE; | |
3553 compl_matches = 0; | |
3554 compl_cont_status = 0; | |
3555 compl_cont_mode = 0; | |
694 | 3556 } |
3557 | |
3558 /* | |
3559 * Set the first match, the original text. | |
3560 */ | |
3561 static void | |
3562 ins_compl_set_original_text(str) | |
3563 char_u *str; | |
3564 { | |
3565 char_u *p; | |
3566 | |
3567 /* Replace the original text entry. */ | |
3568 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */ | |
3569 { | |
3570 p = vim_strsave(str); | |
3571 if (p != NULL) | |
3572 { | |
3573 vim_free(compl_first_match->cp_str); | |
3574 compl_first_match->cp_str = p; | |
3575 } | |
657 | 3576 } |
3577 } | |
3578 | |
3579 /* | |
659 | 3580 * Append one character to the match leader. May reduce the number of |
3581 * matches. | |
3582 */ | |
3583 static void | |
3584 ins_compl_addfrommatch() | |
3585 { | |
3586 char_u *p; | |
1869 | 3587 int len = (int)curwin->w_cursor.col - (int)compl_col; |
659 | 3588 int c; |
887 | 3589 compl_T *cp; |
659 | 3590 |
3591 p = compl_shown_match->cp_str; | |
677 | 3592 if ((int)STRLEN(p) <= len) /* the match is too short */ |
887 | 3593 { |
3594 /* When still at the original match use the first entry that matches | |
3595 * the leader. */ | |
3596 if (compl_shown_match->cp_flags & ORIGINAL_TEXT) | |
3597 { | |
3598 p = NULL; | |
3599 for (cp = compl_shown_match->cp_next; cp != NULL | |
3600 && cp != compl_first_match; cp = cp->cp_next) | |
3601 { | |
987 | 3602 if (compl_leader == NULL |
3603 || ins_compl_equal(cp, compl_leader, | |
887 | 3604 (int)STRLEN(compl_leader))) |
3605 { | |
3606 p = cp->cp_str; | |
3607 break; | |
3608 } | |
3609 } | |
3610 if (p == NULL || (int)STRLEN(p) <= len) | |
3611 return; | |
3612 } | |
3613 else | |
3614 return; | |
3615 } | |
659 | 3616 p += len; |
2845 | 3617 c = PTR2CHAR(p); |
659 | 3618 ins_compl_addleader(c); |
3619 } | |
3620 | |
3621 /* | |
7 | 3622 * Prepare for Insert mode completion, or stop it. |
464 | 3623 * Called just after typing a character in Insert mode. |
540 | 3624 * Returns TRUE when the character is not to be inserted; |
7 | 3625 */ |
540 | 3626 static int |
7 | 3627 ins_compl_prep(c) |
3628 int c; | |
3629 { | |
3630 char_u *ptr; | |
3631 int want_cindent; | |
540 | 3632 int retval = FALSE; |
7 | 3633 |
3634 /* Forget any previous 'special' messages if this is actually | |
3635 * a ^X mode key - bar ^R, in which case we wait to see what it gives us. | |
3636 */ | |
3637 if (c != Ctrl_R && vim_is_ctrl_x_key(c)) | |
3638 edit_submode_extra = NULL; | |
3639 | |
1434 | 3640 /* Ignore end of Select mode mapping and mouse scroll buttons. */ |
2409
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2378
diff
changeset
|
3641 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP |
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2378
diff
changeset
|
3642 || c == K_MOUSELEFT || c == K_MOUSERIGHT) |
540 | 3643 return retval; |
7 | 3644 |
665 | 3645 /* Set "compl_get_longest" when finding the first matches. */ |
3646 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET | |
3647 || (ctrl_x_mode == 0 && !compl_started)) | |
3648 { | |
3649 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL); | |
3650 compl_used_match = TRUE; | |
3651 } | |
3652 | |
7 | 3653 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET) |
3654 { | |
3655 /* | |
3656 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode | |
3657 * it will be yet. Now we decide. | |
3658 */ | |
3659 switch (c) | |
3660 { | |
3661 case Ctrl_E: | |
3662 case Ctrl_Y: | |
3663 ctrl_x_mode = CTRL_X_SCROLL; | |
3664 if (!(State & REPLACE_FLAG)) | |
3665 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)"); | |
3666 else | |
3667 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)"); | |
3668 edit_submode_pre = NULL; | |
3669 showmode(); | |
3670 break; | |
3671 case Ctrl_L: | |
3672 ctrl_x_mode = CTRL_X_WHOLE_LINE; | |
3673 break; | |
3674 case Ctrl_F: | |
3675 ctrl_x_mode = CTRL_X_FILES; | |
3676 break; | |
3677 case Ctrl_K: | |
3678 ctrl_x_mode = CTRL_X_DICTIONARY; | |
3679 break; | |
3680 case Ctrl_R: | |
3681 /* Simply allow ^R to happen without affecting ^X mode */ | |
3682 break; | |
3683 case Ctrl_T: | |
3684 ctrl_x_mode = CTRL_X_THESAURUS; | |
3685 break; | |
12 | 3686 #ifdef FEAT_COMPL_FUNC |
3687 case Ctrl_U: | |
3688 ctrl_x_mode = CTRL_X_FUNCTION; | |
3689 break; | |
449 | 3690 case Ctrl_O: |
523 | 3691 ctrl_x_mode = CTRL_X_OMNI; |
449 | 3692 break; |
502 | 3693 #endif |
477 | 3694 case 's': |
3695 case Ctrl_S: | |
3696 ctrl_x_mode = CTRL_X_SPELL; | |
744 | 3697 #ifdef FEAT_SPELL |
819 | 3698 ++emsg_off; /* Avoid getting the E756 error twice. */ |
484 | 3699 spell_back_to_badword(); |
819 | 3700 --emsg_off; |
484 | 3701 #endif |
477 | 3702 break; |
7 | 3703 case Ctrl_RSB: |
3704 ctrl_x_mode = CTRL_X_TAGS; | |
3705 break; | |
3706 #ifdef FEAT_FIND_ID | |
3707 case Ctrl_I: | |
3708 case K_S_TAB: | |
3709 ctrl_x_mode = CTRL_X_PATH_PATTERNS; | |
3710 break; | |
3711 case Ctrl_D: | |
3712 ctrl_x_mode = CTRL_X_PATH_DEFINES; | |
3713 break; | |
3714 #endif | |
3715 case Ctrl_V: | |
3716 case Ctrl_Q: | |
3717 ctrl_x_mode = CTRL_X_CMDLINE; | |
3718 break; | |
3719 case Ctrl_P: | |
3720 case Ctrl_N: | |
3721 /* ^X^P means LOCAL expansion if nothing interrupted (eg we | |
3722 * just started ^X mode, or there were enough ^X's to cancel | |
3723 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below) | |
3724 * do normal expansion when interrupting a different mode (say | |
3725 * ^X^F^X^P or ^P^X^X^P, see below) | |
3726 * nothing changes if interrupting mode 0, (eg, the flag | |
3727 * doesn't change when going to ADDING mode -- Acevedo */ | |
449 | 3728 if (!(compl_cont_status & CONT_INTRPT)) |
3729 compl_cont_status |= CONT_LOCAL; | |
3730 else if (compl_cont_mode != 0) | |
3731 compl_cont_status &= ~CONT_LOCAL; | |
7 | 3732 /* FALLTHROUGH */ |
3733 default: | |
449 | 3734 /* If we have typed at least 2 ^X's... for modes != 0, we set |
3735 * compl_cont_status = 0 (eg, as if we had just started ^X | |
3736 * mode). | |
3737 * For mode 0, we set "compl_cont_mode" to an impossible | |
3738 * value, in both cases ^X^X can be used to restart the same | |
3739 * mode (avoiding ADDING mode). | |
3740 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start | |
3741 * 'complete' and local ^P expansions respectively. | |
3742 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING | |
3743 * mode -- Acevedo */ | |
7 | 3744 if (c == Ctrl_X) |
3745 { | |
449 | 3746 if (compl_cont_mode != 0) |
3747 compl_cont_status = 0; | |
7 | 3748 else |
449 | 3749 compl_cont_mode = CTRL_X_NOT_DEFINED_YET; |
7 | 3750 } |
3751 ctrl_x_mode = 0; | |
3752 edit_submode = NULL; | |
3753 showmode(); | |
3754 break; | |
3755 } | |
3756 } | |
3757 else if (ctrl_x_mode != 0) | |
3758 { | |
3759 /* We're already in CTRL-X mode, do we stay in it? */ | |
3760 if (!vim_is_ctrl_x_key(c)) | |
3761 { | |
3762 if (ctrl_x_mode == CTRL_X_SCROLL) | |
3763 ctrl_x_mode = 0; | |
3764 else | |
3765 ctrl_x_mode = CTRL_X_FINISHED; | |
3766 edit_submode = NULL; | |
3767 } | |
3768 showmode(); | |
3769 } | |
3770 | |
449 | 3771 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED) |
7 | 3772 { |
3773 /* Show error message from attempted keyword completion (probably | |
3774 * 'Pattern not found') until another key is hit, then go back to | |
449 | 3775 * showing what mode we are in. */ |
7 | 3776 showmode(); |
644 | 3777 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R |
3778 && !ins_compl_pum_key(c)) | |
7 | 3779 || ctrl_x_mode == CTRL_X_FINISHED) |
3780 { | |
3781 /* Get here when we have finished typing a sequence of ^N and | |
3782 * ^P or other completion characters in CTRL-X mode. Free up | |
449 | 3783 * memory that was used, and make sure we can redo the insert. */ |
836 | 3784 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E) |
7 | 3785 { |
3786 /* | |
836 | 3787 * If any of the original typed text has been changed, eg when |
3788 * ignorecase is set, we must add back-spaces to the redo | |
3789 * buffer. We add as few as necessary to delete just the part | |
3790 * of the original text that has changed. | |
3791 * When using the longest match, edited the match or used | |
3792 * CTRL-E then don't use the current match. | |
7 | 3793 */ |
836 | 3794 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E) |
3795 ptr = compl_curr_match->cp_str; | |
3796 else | |
3095 | 3797 ptr = NULL; |
3798 ins_compl_fixRedoBufForLeader(ptr); | |
7 | 3799 } |
3800 | |
3801 #ifdef FEAT_CINDENT | |
3802 want_cindent = (can_cindent && cindent_on()); | |
3803 #endif | |
3804 /* | |
3805 * When completing whole lines: fix indent for 'cindent'. | |
3806 * Otherwise, break line if it's too long. | |
3807 */ | |
449 | 3808 if (compl_cont_mode == CTRL_X_WHOLE_LINE) |
7 | 3809 { |
3810 #ifdef FEAT_CINDENT | |
3811 /* re-indent the current line */ | |
3812 if (want_cindent) | |
3813 { | |
3814 do_c_expr_indent(); | |
3815 want_cindent = FALSE; /* don't do it again */ | |
3816 } | |
3817 #endif | |
3818 } | |
3819 else | |
3820 { | |
1073 | 3821 int prev_col = curwin->w_cursor.col; |
3822 | |
7 | 3823 /* put the cursor on the last char, for 'tw' formatting */ |
1073 | 3824 if (prev_col > 0) |
3825 dec_cursor(); | |
7 | 3826 if (stop_arrow() == OK) |
3827 insertchar(NUL, 0, -1); | |
1073 | 3828 if (prev_col > 0 |
3829 && ml_get_curline()[curwin->w_cursor.col] != NUL) | |
3830 inc_cursor(); | |
7 | 3831 } |
3832 | |
816 | 3833 /* If the popup menu is displayed pressing CTRL-Y means accepting |
825 | 3834 * the selection without inserting anything. When |
3835 * compl_enter_selects is set the Enter key does the same. */ | |
3836 if ((c == Ctrl_Y || (compl_enter_selects | |
3837 && (c == CAR || c == K_KENTER || c == NL))) | |
3838 && pum_visible()) | |
540 | 3839 retval = TRUE; |
3840 | |
816 | 3841 /* CTRL-E means completion is Ended, go back to the typed text. */ |
3842 if (c == Ctrl_E) | |
3843 { | |
3844 ins_compl_delete(); | |
3845 if (compl_leader != NULL) | |
1782 | 3846 ins_bytes(compl_leader + ins_compl_len()); |
816 | 3847 else if (compl_first_match != NULL) |
1782 | 3848 ins_bytes(compl_orig_text + ins_compl_len()); |
816 | 3849 retval = TRUE; |
3850 } | |
3851 | |
1698 | 3852 auto_format(FALSE, TRUE); |
3853 | |
7 | 3854 ins_compl_free(); |
449 | 3855 compl_started = FALSE; |
3856 compl_matches = 0; | |
6056 | 3857 if (!shortmess(SHM_COMPLETIONMENU)) |
3858 msg_clr_cmdline(); /* necessary for "noshowmode" */ | |
7 | 3859 ctrl_x_mode = 0; |
825 | 3860 compl_enter_selects = FALSE; |
7 | 3861 if (edit_submode != NULL) |
3862 { | |
3863 edit_submode = NULL; | |
3864 showmode(); | |
3865 } | |
3866 | |
3867 #ifdef FEAT_CINDENT | |
3868 /* | |
3869 * Indent now if a key was typed that is in 'cinkeys'. | |
3870 */ | |
3871 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0))) | |
3872 do_c_expr_indent(); | |
3873 #endif | |
3676 | 3874 #ifdef FEAT_AUTOCMD |
3875 /* Trigger the CompleteDone event to give scripts a chance to act | |
3876 * upon the completion. */ | |
3877 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf); | |
3878 #endif | |
7 | 3879 } |
3880 } | |
4120 | 3881 #ifdef FEAT_AUTOCMD |
3882 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG) | |
3883 /* Trigger the CompleteDone event to give scripts a chance to act | |
3884 * upon the (possibly failed) completion. */ | |
3885 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf); | |
3886 #endif | |
7 | 3887 |
3888 /* reset continue_* if we left expansion-mode, if we stay they'll be | |
3889 * (re)set properly in ins_complete() */ | |
3890 if (!vim_is_ctrl_x_key(c)) | |
3891 { | |
449 | 3892 compl_cont_status = 0; |
3893 compl_cont_mode = 0; | |
7 | 3894 } |
540 | 3895 |
3896 return retval; | |
7 | 3897 } |
3898 | |
3899 /* | |
3095 | 3900 * Fix the redo buffer for the completion leader replacing some of the typed |
3901 * text. This inserts backspaces and appends the changed text. | |
3902 * "ptr" is the known leader text or NUL. | |
3903 */ | |
3904 static void | |
3905 ins_compl_fixRedoBufForLeader(ptr_arg) | |
3906 char_u *ptr_arg; | |
3907 { | |
3908 int len; | |
3909 char_u *p; | |
3910 char_u *ptr = ptr_arg; | |
3911 | |
3912 if (ptr == NULL) | |
3913 { | |
3914 if (compl_leader != NULL) | |
3915 ptr = compl_leader; | |
3916 else | |
3917 return; /* nothing to do */ | |
3918 } | |
3919 if (compl_orig_text != NULL) | |
3920 { | |
3921 p = compl_orig_text; | |
3922 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len) | |
3923 ; | |
3924 #ifdef FEAT_MBYTE | |
3925 if (len > 0) | |
3926 len -= (*mb_head_off)(p, p + len); | |
3927 #endif | |
3928 for (p += len; *p != NUL; mb_ptr_adv(p)) | |
3929 AppendCharToRedobuff(K_BS); | |
3930 } | |
3931 else | |
3932 len = 0; | |
3933 if (ptr != NULL) | |
3934 AppendToRedobuffLit(ptr + len, -1); | |
3935 } | |
3936 | |
3937 /* | |
7 | 3938 * Loops through the list of windows, loaded-buffers or non-loaded-buffers |
3939 * (depending on flag) starting from buf and looking for a non-scanned | |
3940 * buffer (other than curbuf). curbuf is special, if it is called with | |
3941 * buf=curbuf then it has to be the first call for a given flag/expansion. | |
3942 * | |
3943 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo | |
3944 */ | |
3945 static buf_T * | |
3946 ins_compl_next_buf(buf, flag) | |
3947 buf_T *buf; | |
3948 int flag; | |
3949 { | |
3950 #ifdef FEAT_WINDOWS | |
3951 static win_T *wp; | |
3952 #endif | |
3953 | |
3954 if (flag == 'w') /* just windows */ | |
3955 { | |
3956 #ifdef FEAT_WINDOWS | |
3957 if (buf == curbuf) /* first call for this flag/expansion */ | |
3958 wp = curwin; | |
383 | 3959 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin |
7 | 3960 && wp->w_buffer->b_scanned) |
3961 ; | |
3962 buf = wp->w_buffer; | |
3963 #else | |
3964 buf = curbuf; | |
3965 #endif | |
3966 } | |
3967 else | |
3968 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U' | |
3969 * (unlisted buffers) | |
3970 * When completing whole lines skip unloaded buffers. */ | |
383 | 3971 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf |
7 | 3972 && ((flag == 'U' |
3973 ? buf->b_p_bl | |
3974 : (!buf->b_p_bl | |
3975 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u'))) | |
667 | 3976 || buf->b_scanned)) |
7 | 3977 ; |
3978 return buf; | |
3979 } | |
3980 | |
12 | 3981 #ifdef FEAT_COMPL_FUNC |
659 | 3982 static void expand_by_function __ARGS((int type, char_u *base)); |
12 | 3983 |
3984 /* | |
523 | 3985 * Execute user defined complete function 'completefunc' or 'omnifunc', and |
502 | 3986 * get matches in "matches". |
12 | 3987 */ |
659 | 3988 static void |
3989 expand_by_function(type, base) | |
523 | 3990 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */ |
12 | 3991 char_u *base; |
3992 { | |
3078 | 3993 list_T *matchlist = NULL; |
3994 dict_T *matchdict = NULL; | |
502 | 3995 char_u *args[2]; |
3996 char_u *funcname; | |
3997 pos_T pos; | |
2631 | 3998 win_T *curwin_save; |
3999 buf_T *curbuf_save; | |
3078 | 4000 typval_T rettv; |
502 | 4001 |
4002 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu; | |
4003 if (*funcname == NUL) | |
659 | 4004 return; |
452 | 4005 |
4006 /* Call 'completefunc' to obtain the list of matches. */ | |
4007 args[0] = (char_u *)"0"; | |
502 | 4008 args[1] = base; |
4009 | |
4010 pos = curwin->w_cursor; | |
2631 | 4011 curwin_save = curwin; |
4012 curbuf_save = curbuf; | |
3078 | 4013 |
4014 /* Call a function, which returns a list or dict. */ | |
3709 | 4015 if (call_vim_function(funcname, 2, args, FALSE, FALSE, &rettv) == OK) |
3078 | 4016 { |
4017 switch (rettv.v_type) | |
4018 { | |
4019 case VAR_LIST: | |
4020 matchlist = rettv.vval.v_list; | |
4021 break; | |
4022 case VAR_DICT: | |
4023 matchdict = rettv.vval.v_dict; | |
4024 break; | |
4025 default: | |
4026 /* TODO: Give error message? */ | |
4027 clear_tv(&rettv); | |
4028 break; | |
4029 } | |
4030 } | |
4031 | |
2631 | 4032 if (curwin_save != curwin || curbuf_save != curbuf) |
4033 { | |
4034 EMSG(_(e_complwin)); | |
4035 goto theend; | |
4036 } | |
502 | 4037 curwin->w_cursor = pos; /* restore the cursor position */ |
6209 | 4038 validate_cursor(); |
2631 | 4039 if (!equalpos(curwin->w_cursor, pos)) |
4040 { | |
4041 EMSG(_(e_compldel)); | |
4042 goto theend; | |
4043 } | |
3078 | 4044 |
2631 | 4045 if (matchlist != NULL) |
4046 ins_compl_add_list(matchlist); | |
3078 | 4047 else if (matchdict != NULL) |
4048 ins_compl_add_dict(matchdict); | |
2631 | 4049 |
4050 theend: | |
3078 | 4051 if (matchdict != NULL) |
4052 dict_unref(matchdict); | |
2631 | 4053 if (matchlist != NULL) |
4054 list_unref(matchlist); | |
724 | 4055 } |
4056 #endif /* FEAT_COMPL_FUNC */ | |
4057 | |
786 | 4058 #if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO) |
724 | 4059 /* |
4060 * Add completions from a list. | |
4061 */ | |
4062 static void | |
4063 ins_compl_add_list(list) | |
4064 list_T *list; | |
4065 { | |
4066 listitem_T *li; | |
4067 int dir = compl_direction; | |
4068 | |
659 | 4069 /* Go through the List with matches and add each of them. */ |
724 | 4070 for (li = list->lv_first; li != NULL; li = li->li_next) |
452 | 4071 { |
786 | 4072 if (ins_compl_add_tv(&li->li_tv, dir) == OK) |
4073 /* if dir was BACKWARD then honor it just once */ | |
4074 dir = FORWARD; | |
648 | 4075 else if (did_emsg) |
4076 break; | |
452 | 4077 } |
724 | 4078 } |
786 | 4079 |
4080 /* | |
3078 | 4081 * Add completions from a dict. |
4082 */ | |
4083 static void | |
4084 ins_compl_add_dict(dict) | |
4085 dict_T *dict; | |
4086 { | |
3263 | 4087 dictitem_T *di_refresh; |
4088 dictitem_T *di_words; | |
3078 | 4089 |
4090 /* Check for optional "refresh" item. */ | |
4091 compl_opt_refresh_always = FALSE; | |
3263 | 4092 di_refresh = dict_find(dict, (char_u *)"refresh", 7); |
4093 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING) | |
4094 { | |
4095 char_u *v = di_refresh->di_tv.vval.v_string; | |
3078 | 4096 |
4097 if (v != NULL && STRCMP(v, (char_u *)"always") == 0) | |
4098 compl_opt_refresh_always = TRUE; | |
4099 } | |
4100 | |
4101 /* Add completions from a "words" list. */ | |
3263 | 4102 di_words = dict_find(dict, (char_u *)"words", 5); |
4103 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST) | |
4104 ins_compl_add_list(di_words->di_tv.vval.v_list); | |
3078 | 4105 } |
4106 | |
4107 /* | |
786 | 4108 * Add a match to the list of matches from a typeval_T. |
4109 * If the given string is already in the list of completions, then return | |
4110 * NOTDONE, otherwise add it to the list and return OK. If there is an error, | |
4111 * maybe because alloc() returns NULL, then FAIL is returned. | |
4112 */ | |
4113 int | |
4114 ins_compl_add_tv(tv, dir) | |
4115 typval_T *tv; | |
4116 int dir; | |
4117 { | |
4118 char_u *word; | |
867 | 4119 int icase = FALSE; |
944 | 4120 int adup = FALSE; |
2632 | 4121 int aempty = FALSE; |
786 | 4122 char_u *(cptext[CPT_COUNT]); |
4123 | |
4124 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL) | |
4125 { | |
4126 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE); | |
4127 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict, | |
4128 (char_u *)"abbr", FALSE); | |
4129 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict, | |
4130 (char_u *)"menu", FALSE); | |
4131 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict, | |
4132 (char_u *)"kind", FALSE); | |
4133 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict, | |
4134 (char_u *)"info", FALSE); | |
4135 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL) | |
4136 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase"); | |
841 | 4137 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL) |
944 | 4138 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup"); |
2632 | 4139 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL) |
4140 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty"); | |
786 | 4141 } |
4142 else | |
4143 { | |
4144 word = get_tv_string_chk(tv); | |
4145 vim_memset(cptext, 0, sizeof(cptext)); | |
4146 } | |
2632 | 4147 if (word == NULL || (!aempty && *word == NUL)) |
786 | 4148 return FAIL; |
944 | 4149 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup); |
786 | 4150 } |
724 | 4151 #endif |
12 | 4152 |
449 | 4153 /* |
4154 * Get the next expansion(s), using "compl_pattern". | |
659 | 4155 * The search starts at position "ini" in curbuf and in the direction |
4156 * compl_direction. | |
667 | 4157 * When "compl_started" is FALSE start at that position, otherwise continue |
4158 * where we stopped searching before. | |
449 | 4159 * This may return before finding all the matches. |
4160 * Return the total number of matches or -1 if still unknown -- Acevedo | |
7 | 4161 */ |
4162 static int | |
659 | 4163 ins_compl_get_exp(ini) |
7 | 4164 pos_T *ini; |
4165 { | |
4166 static pos_T first_match_pos; | |
4167 static pos_T last_match_pos; | |
4168 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */ | |
449 | 4169 static int found_all = FALSE; /* Found all matches of a |
4170 certain type. */ | |
4171 static buf_T *ins_buf = NULL; /* buffer being scanned */ | |
7 | 4172 |
464 | 4173 pos_T *pos; |
4174 char_u **matches; | |
4175 int save_p_scs; | |
4176 int save_p_ws; | |
4177 int save_p_ic; | |
4178 int i; | |
4179 int num_matches; | |
4180 int len; | |
4181 int found_new_match; | |
4182 int type = ctrl_x_mode; | |
4183 char_u *ptr; | |
4184 char_u *dict = NULL; | |
4185 int dict_f = 0; | |
4186 compl_T *old_match; | |
5625 | 4187 int set_match_pos; |
7 | 4188 |
449 | 4189 if (!compl_started) |
7 | 4190 { |
4191 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next) | |
4192 ins_buf->b_scanned = 0; | |
4193 found_all = FALSE; | |
4194 ins_buf = curbuf; | |
449 | 4195 e_cpt = (compl_cont_status & CONT_LOCAL) |
12 | 4196 ? (char_u *)"." : curbuf->b_p_cpt; |
7 | 4197 last_match_pos = first_match_pos = *ini; |
4198 } | |
4199 | |
449 | 4200 old_match = compl_curr_match; /* remember the last current match */ |
659 | 4201 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos; |
7 | 4202 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */ |
4203 for (;;) | |
4204 { | |
4205 found_new_match = FAIL; | |
5625 | 4206 set_match_pos = FALSE; |
7 | 4207 |
449 | 4208 /* For ^N/^P pick a new entry from e_cpt if compl_started is off, |
7 | 4209 * or if found_all says this entry is done. For ^X^L only use the |
4210 * entries from 'complete' that look in loaded buffers. */ | |
4211 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE) | |
449 | 4212 && (!compl_started || found_all)) |
7 | 4213 { |
4214 found_all = FALSE; | |
4215 while (*e_cpt == ',' || *e_cpt == ' ') | |
4216 e_cpt++; | |
4217 if (*e_cpt == '.' && !curbuf->b_scanned) | |
4218 { | |
4219 ins_buf = curbuf; | |
4220 first_match_pos = *ini; | |
4221 /* So that ^N can match word immediately after cursor */ | |
4222 if (ctrl_x_mode == 0) | |
4223 dec(&first_match_pos); | |
4224 last_match_pos = first_match_pos; | |
4225 type = 0; | |
5625 | 4226 |
4227 /* Remember the first match so that the loop stops when we | |
4228 * wrap and come back there a second time. */ | |
4229 set_match_pos = TRUE; | |
7 | 4230 } |
4231 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL | |
4232 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf) | |
4233 { | |
4234 /* Scan a buffer, but not the current one. */ | |
4235 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */ | |
4236 { | |
449 | 4237 compl_started = TRUE; |
7 | 4238 first_match_pos.col = last_match_pos.col = 0; |
4239 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1; | |
4240 last_match_pos.lnum = 0; | |
4241 type = 0; | |
4242 } | |
4243 else /* unloaded buffer, scan like dictionary */ | |
4244 { | |
4245 found_all = TRUE; | |
4246 if (ins_buf->b_fname == NULL) | |
4247 continue; | |
4248 type = CTRL_X_DICTIONARY; | |
4249 dict = ins_buf->b_fname; | |
4250 dict_f = DICT_EXACT; | |
4251 } | |
274 | 4252 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"), |
7 | 4253 ins_buf->b_fname == NULL |
4254 ? buf_spname(ins_buf) | |
4255 : ins_buf->b_sfname == NULL | |
3850 | 4256 ? ins_buf->b_fname |
4257 : ins_buf->b_sfname); | |
1869 | 4258 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R)); |
7 | 4259 } |
4260 else if (*e_cpt == NUL) | |
4261 break; | |
4262 else | |
4263 { | |
4264 if (ctrl_x_mode == CTRL_X_WHOLE_LINE) | |
4265 type = -1; | |
4266 else if (*e_cpt == 'k' || *e_cpt == 's') | |
4267 { | |
4268 if (*e_cpt == 'k') | |
4269 type = CTRL_X_DICTIONARY; | |
4270 else | |
4271 type = CTRL_X_THESAURUS; | |
4272 if (*++e_cpt != ',' && *e_cpt != NUL) | |
4273 { | |
4274 dict = e_cpt; | |
4275 dict_f = DICT_FIRST; | |
4276 } | |
4277 } | |
4278 #ifdef FEAT_FIND_ID | |
4279 else if (*e_cpt == 'i') | |
4280 type = CTRL_X_PATH_PATTERNS; | |
4281 else if (*e_cpt == 'd') | |
4282 type = CTRL_X_PATH_DEFINES; | |
4283 #endif | |
4284 else if (*e_cpt == ']' || *e_cpt == 't') | |
4285 { | |
4286 type = CTRL_X_TAGS; | |
1872 | 4287 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags.")); |
1869 | 4288 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R)); |
7 | 4289 } |
4290 else | |
4291 type = -1; | |
4292 | |
4293 /* in any case e_cpt is advanced to the next entry */ | |
4294 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ","); | |
4295 | |
4296 found_all = TRUE; | |
4297 if (type == -1) | |
4298 continue; | |
4299 } | |
4300 } | |
4301 | |
4302 switch (type) | |
4303 { | |
4304 case -1: | |
4305 break; | |
4306 #ifdef FEAT_FIND_ID | |
4307 case CTRL_X_PATH_PATTERNS: | |
4308 case CTRL_X_PATH_DEFINES: | |
659 | 4309 find_pattern_in_path(compl_pattern, compl_direction, |
449 | 4310 (int)STRLEN(compl_pattern), FALSE, FALSE, |
7 | 4311 (type == CTRL_X_PATH_DEFINES |
449 | 4312 && !(compl_cont_status & CONT_SOL)) |
7 | 4313 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND, |
4314 (linenr_T)1, (linenr_T)MAXLNUM); | |
4315 break; | |
4316 #endif | |
4317 | |
4318 case CTRL_X_DICTIONARY: | |
4319 case CTRL_X_THESAURUS: | |
4320 ins_compl_dictionaries( | |
703 | 4321 dict != NULL ? dict |
7 | 4322 : (type == CTRL_X_THESAURUS |
4323 ? (*curbuf->b_p_tsr == NUL | |
4324 ? p_tsr | |
4325 : curbuf->b_p_tsr) | |
4326 : (*curbuf->b_p_dict == NUL | |
4327 ? p_dict | |
4328 : curbuf->b_p_dict)), | |
659 | 4329 compl_pattern, |
703 | 4330 dict != NULL ? dict_f |
4331 : 0, type == CTRL_X_THESAURUS); | |
7 | 4332 dict = NULL; |
4333 break; | |
4334 | |
4335 case CTRL_X_TAGS: | |
4336 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */ | |
4337 save_p_ic = p_ic; | |
449 | 4338 p_ic = ignorecase(compl_pattern); |
7 | 4339 |
2047
85da03763130
updated for version 7.2.333
Bram Moolenaar <bram@zimbu.org>
parents:
2025
diff
changeset
|
4340 /* Find up to TAG_MANY matches. Avoids that an enormous number |
449 | 4341 * of matches is found when compl_pattern is empty */ |
4342 if (find_tags(compl_pattern, &num_matches, &matches, | |
7 | 4343 TAG_REGEXP | TAG_NAMES | TAG_NOIC | |
4344 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0), | |
4345 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0) | |
4346 { | |
942 | 4347 ins_compl_add_matches(num_matches, matches, p_ic); |
7 | 4348 } |
4349 p_ic = save_p_ic; | |
4350 break; | |
4351 | |
4352 case CTRL_X_FILES: | |
449 | 4353 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches, |
7 | 4354 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK) |
4355 { | |
4356 | |
4357 /* May change home directory back to "~". */ | |
449 | 4358 tilde_replace(compl_pattern, num_matches, matches); |
4242 | 4359 ins_compl_add_matches(num_matches, matches, p_fic || p_wic); |
7 | 4360 } |
4361 break; | |
4362 | |
4363 case CTRL_X_CMDLINE: | |
449 | 4364 if (expand_cmdline(&compl_xp, compl_pattern, |
4365 (int)STRLEN(compl_pattern), | |
7 | 4366 &num_matches, &matches) == EXPAND_OK) |
681 | 4367 ins_compl_add_matches(num_matches, matches, FALSE); |
7 | 4368 break; |
4369 | |
12 | 4370 #ifdef FEAT_COMPL_FUNC |
4371 case CTRL_X_FUNCTION: | |
523 | 4372 case CTRL_X_OMNI: |
659 | 4373 expand_by_function(type, compl_pattern); |
12 | 4374 break; |
4375 #endif | |
4376 | |
477 | 4377 case CTRL_X_SPELL: |
744 | 4378 #ifdef FEAT_SPELL |
477 | 4379 num_matches = expand_spelling(first_match_pos.lnum, |
1872 | 4380 compl_pattern, &matches); |
477 | 4381 if (num_matches > 0) |
942 | 4382 ins_compl_add_matches(num_matches, matches, p_ic); |
477 | 4383 #endif |
4384 break; | |
4385 | |
7 | 4386 default: /* normal ^P/^N and ^X^L */ |
4387 /* | |
4388 * If 'infercase' is set, don't use 'smartcase' here | |
4389 */ | |
4390 save_p_scs = p_scs; | |
4391 if (ins_buf->b_p_inf) | |
4392 p_scs = FALSE; | |
449 | 4393 |
5625 | 4394 /* Buffers other than curbuf are scanned from the beginning or the |
7 | 4395 * end but never from the middle, thus setting nowrapscan in this |
4396 * buffers is a good idea, on the other hand, we always set | |
4397 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */ | |
4398 save_p_ws = p_ws; | |
4399 if (ins_buf != curbuf) | |
4400 p_ws = FALSE; | |
4401 else if (*e_cpt == '.') | |
4402 p_ws = TRUE; | |
4403 for (;;) | |
4404 { | |
464 | 4405 int flags = 0; |
7 | 4406 |
1007 | 4407 ++msg_silent; /* Don't want messages for wrapscan. */ |
4408 | |
540 | 4409 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that |
4410 * has added a word that was at the beginning of the line */ | |
7 | 4411 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE |
449 | 4412 || (compl_cont_status & CONT_SOL)) |
7 | 4413 found_new_match = search_for_exact_line(ins_buf, pos, |
659 | 4414 compl_direction, compl_pattern); |
7 | 4415 else |
659 | 4416 found_new_match = searchit(NULL, ins_buf, pos, |
4417 compl_direction, | |
449 | 4418 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG, |
1496 | 4419 RE_LAST, (linenr_T)0, NULL); |
1007 | 4420 --msg_silent; |
5625 | 4421 if (!compl_started || set_match_pos) |
7 | 4422 { |
667 | 4423 /* set "compl_started" even on fail */ |
449 | 4424 compl_started = TRUE; |
7 | 4425 first_match_pos = *pos; |
4426 last_match_pos = *pos; | |
5625 | 4427 set_match_pos = FALSE; |
7 | 4428 } |
4429 else if (first_match_pos.lnum == last_match_pos.lnum | |
665 | 4430 && first_match_pos.col == last_match_pos.col) |
7 | 4431 found_new_match = FAIL; |
4432 if (found_new_match == FAIL) | |
4433 { | |
4434 if (ins_buf == curbuf) | |
4435 found_all = TRUE; | |
4436 break; | |
4437 } | |
4438 | |
4439 /* when ADDING, the text before the cursor matches, skip it */ | |
449 | 4440 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf |
7 | 4441 && ini->lnum == pos->lnum |
4442 && ini->col == pos->col) | |
4443 continue; | |
4444 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col; | |
4445 if (ctrl_x_mode == CTRL_X_WHOLE_LINE) | |
4446 { | |
449 | 4447 if (compl_cont_status & CONT_ADDING) |
7 | 4448 { |
4449 if (pos->lnum >= ins_buf->b_ml.ml_line_count) | |
4450 continue; | |
4451 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE); | |
4452 if (!p_paste) | |
4453 ptr = skipwhite(ptr); | |
4454 } | |
4455 len = (int)STRLEN(ptr); | |
4456 } | |
4457 else | |
4458 { | |
449 | 4459 char_u *tmp_ptr = ptr; |
4460 | |
4461 if (compl_cont_status & CONT_ADDING) | |
7 | 4462 { |
449 | 4463 tmp_ptr += compl_length; |
7 | 4464 /* Skip if already inside a word. */ |
4465 if (vim_iswordp(tmp_ptr)) | |
4466 continue; | |
4467 /* Find start of next word. */ | |
4468 tmp_ptr = find_word_start(tmp_ptr); | |
4469 } | |
4470 /* Find end of this word. */ | |
4471 tmp_ptr = find_word_end(tmp_ptr); | |
4472 len = (int)(tmp_ptr - ptr); | |
4473 | |
449 | 4474 if ((compl_cont_status & CONT_ADDING) |
4475 && len == compl_length) | |
7 | 4476 { |
4477 if (pos->lnum < ins_buf->b_ml.ml_line_count) | |
4478 { | |
4479 /* Try next line, if any. the new word will be | |
4480 * "join" as if the normal command "J" was used. | |
4481 * IOSIZE is always greater than | |
449 | 4482 * compl_length, so the next STRNCPY always |
7 | 4483 * works -- Acevedo */ |
4484 STRNCPY(IObuff, ptr, len); | |
4485 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE); | |
4486 tmp_ptr = ptr = skipwhite(ptr); | |
4487 /* Find start of next word. */ | |
4488 tmp_ptr = find_word_start(tmp_ptr); | |
4489 /* Find end of next word. */ | |
4490 tmp_ptr = find_word_end(tmp_ptr); | |
4491 if (tmp_ptr > ptr) | |
4492 { | |
419 | 4493 if (*ptr != ')' && IObuff[len - 1] != TAB) |
7 | 4494 { |
419 | 4495 if (IObuff[len - 1] != ' ') |
7 | 4496 IObuff[len++] = ' '; |
4497 /* IObuf =~ "\k.* ", thus len >= 2 */ | |
4498 if (p_js | |
419 | 4499 && (IObuff[len - 2] == '.' |
7 | 4500 || (vim_strchr(p_cpo, CPO_JOINSP) |
4501 == NULL | |
419 | 4502 && (IObuff[len - 2] == '?' |
4503 || IObuff[len - 2] == '!')))) | |
7 | 4504 IObuff[len++] = ' '; |
4505 } | |
2047
85da03763130
updated for version 7.2.333
Bram Moolenaar <bram@zimbu.org>
parents:
2025
diff
changeset
|
4506 /* copy as much as possible of the new word */ |
7 | 4507 if (tmp_ptr - ptr >= IOSIZE - len) |
4508 tmp_ptr = ptr + IOSIZE - len - 1; | |
4509 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr); | |
4510 len += (int)(tmp_ptr - ptr); | |
464 | 4511 flags |= CONT_S_IPOS; |
7 | 4512 } |
4513 IObuff[len] = NUL; | |
4514 ptr = IObuff; | |
4515 } | |
449 | 4516 if (len == compl_length) |
7 | 4517 continue; |
4518 } | |
4519 } | |
942 | 4520 if (ins_compl_add_infercase(ptr, len, p_ic, |
540 | 4521 ins_buf == curbuf ? NULL : ins_buf->b_sfname, |
659 | 4522 0, flags) != NOTDONE) |
7 | 4523 { |
4524 found_new_match = OK; | |
4525 break; | |
4526 } | |
4527 } | |
4528 p_scs = save_p_scs; | |
4529 p_ws = save_p_ws; | |
4530 } | |
540 | 4531 |
449 | 4532 /* check if compl_curr_match has changed, (e.g. other type of |
1796 | 4533 * expansion added something) */ |
540 | 4534 if (type != 0 && compl_curr_match != old_match) |
7 | 4535 found_new_match = OK; |
4536 | |
4537 /* break the loop for specialized modes (use 'complete' just for the | |
4538 * generic ctrl_x_mode == 0) or when we've found a new match */ | |
4539 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE) | |
449 | 4540 || found_new_match != FAIL) |
540 | 4541 { |
4542 if (got_int) | |
4543 break; | |
665 | 4544 /* Fill the popup menu as soon as possible. */ |
989 | 4545 if (type != -1) |
540 | 4546 ins_compl_check_keys(0); |
665 | 4547 |
540 | 4548 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE) |
4549 || compl_interrupted) | |
4550 break; | |
4551 compl_started = TRUE; | |
4552 } | |
4553 else | |
4554 { | |
4555 /* Mark a buffer scanned when it has been scanned completely */ | |
4556 if (type == 0 || type == CTRL_X_PATH_PATTERNS) | |
4557 ins_buf->b_scanned = TRUE; | |
4558 | |
4559 compl_started = FALSE; | |
4560 } | |
449 | 4561 } |
4562 compl_started = TRUE; | |
7 | 4563 |
4564 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE) | |
4565 && *e_cpt == NUL) /* Got to end of 'complete' */ | |
4566 found_new_match = FAIL; | |
4567 | |
4568 i = -1; /* total of matches, unknown */ | |
4569 if (found_new_match == FAIL | |
4570 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)) | |
4571 i = ins_compl_make_cyclic(); | |
4572 | |
4573 /* If several matches were added (FORWARD) or the search failed and has | |
449 | 4574 * just been made cyclic then we have to move compl_curr_match to the next |
4575 * or previous entry (if any) -- Acevedo */ | |
724 | 4576 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next |
4577 : old_match->cp_prev; | |
449 | 4578 if (compl_curr_match == NULL) |
4579 compl_curr_match = old_match; | |
7 | 4580 return i; |
4581 } | |
4582 | |
4583 /* Delete the old text being completed. */ | |
4584 static void | |
4585 ins_compl_delete() | |
4586 { | |
4587 int i; | |
4588 | |
4589 /* | |
4590 * In insert mode: Delete the typed part. | |
4591 * In replace mode: Put the old characters back, if any. | |
4592 */ | |
449 | 4593 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0); |
7 | 4594 backspace_until_column(i); |
6056 | 4595 |
6075 | 4596 /* TODO: is this sufficient for redrawing? Redrawing everything causes |
4597 * flicker, thus we can't do that. */ | |
7 | 4598 changed_cline_bef_curs(); |
4599 } | |
4600 | |
4601 /* Insert the new text being completed. */ | |
4602 static void | |
4603 ins_compl_insert() | |
4604 { | |
1782 | 4605 ins_bytes(compl_shown_match->cp_str + ins_compl_len()); |
683 | 4606 if (compl_shown_match->cp_flags & ORIGINAL_TEXT) |
4607 compl_used_match = FALSE; | |
4608 else | |
4609 compl_used_match = TRUE; | |
7 | 4610 } |
4611 | |
4612 /* | |
4613 * Fill in the next completion in the current direction. | |
464 | 4614 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to |
4615 * get more completions. If it is FALSE, then we just do nothing when there | |
4616 * are no more completions in a given direction. The latter case is used when | |
4617 * we are still in the middle of finding completions, to allow browsing | |
4618 * through the ones found so far. | |
7 | 4619 * Return the total number of matches, or -1 if still unknown -- webb. |
4620 * | |
449 | 4621 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use |
4622 * compl_shown_match here. | |
7 | 4623 * |
4624 * Note that this function may be called recursively once only. First with | |
464 | 4625 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn |
4626 * calls this function with "allow_get_expansion" FALSE. | |
7 | 4627 */ |
4628 static int | |
665 | 4629 ins_compl_next(allow_get_expansion, count, insert_match) |
7 | 4630 int allow_get_expansion; |
610 | 4631 int count; /* repeat completion this many times; should |
4632 be at least 1 */ | |
665 | 4633 int insert_match; /* Insert the newly selected match */ |
7 | 4634 { |
4635 int num_matches = -1; | |
4636 int i; | |
610 | 4637 int todo = count; |
657 | 4638 compl_T *found_compl = NULL; |
4639 int found_end = FALSE; | |
836 | 4640 int advance; |
7 | 4641 |
3271 | 4642 /* When user complete function return -1 for findstart which is next |
4643 * time of 'always', compl_shown_match become NULL. */ | |
4644 if (compl_shown_match == NULL) | |
4645 return -1; | |
4646 | |
665 | 4647 if (compl_leader != NULL |
4648 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0) | |
4649 { | |
4650 /* Set "compl_shown_match" to the actually shown match, it may differ | |
4651 * when "compl_leader" is used to omit some of the matches. */ | |
681 | 4652 while (!ins_compl_equal(compl_shown_match, |
835 | 4653 compl_leader, (int)STRLEN(compl_leader)) |
665 | 4654 && compl_shown_match->cp_next != NULL |
4655 && compl_shown_match->cp_next != compl_first_match) | |
4656 compl_shown_match = compl_shown_match->cp_next; | |
887 | 4657 |
4658 /* If we didn't find it searching forward, and compl_shows_dir is | |
4659 * backward, find the last match. */ | |
4660 if (compl_shows_dir == BACKWARD | |
4661 && !ins_compl_equal(compl_shown_match, | |
4662 compl_leader, (int)STRLEN(compl_leader)) | |
4663 && (compl_shown_match->cp_next == NULL | |
4664 || compl_shown_match->cp_next == compl_first_match)) | |
4665 { | |
4666 while (!ins_compl_equal(compl_shown_match, | |
4667 compl_leader, (int)STRLEN(compl_leader)) | |
4668 && compl_shown_match->cp_prev != NULL | |
4669 && compl_shown_match->cp_prev != compl_first_match) | |
4670 compl_shown_match = compl_shown_match->cp_prev; | |
4671 } | |
665 | 4672 } |
4673 | |
4674 if (allow_get_expansion && insert_match | |
874 | 4675 && (!(compl_get_longest || compl_restarting) || compl_used_match)) |
7 | 4676 /* Delete old text to be replaced */ |
4677 ins_compl_delete(); | |
665 | 4678 |
836 | 4679 /* When finding the longest common text we stick at the original text, |
4680 * don't let CTRL-N or CTRL-P move to the first match. */ | |
4681 advance = count != 1 || !allow_get_expansion || !compl_get_longest; | |
4682 | |
874 | 4683 /* When restarting the search don't insert the first match either. */ |
4684 if (compl_restarting) | |
4685 { | |
4686 advance = FALSE; | |
4687 compl_restarting = FALSE; | |
4688 } | |
4689 | |
610 | 4690 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap |
4691 * around. */ | |
4692 while (--todo >= 0) | |
4693 { | |
4694 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL) | |
4695 { | |
4696 compl_shown_match = compl_shown_match->cp_next; | |
657 | 4697 found_end = (compl_first_match != NULL |
4698 && (compl_shown_match->cp_next == compl_first_match | |
4699 || compl_shown_match == compl_first_match)); | |
610 | 4700 } |
4701 else if (compl_shows_dir == BACKWARD | |
4702 && compl_shown_match->cp_prev != NULL) | |
4703 { | |
657 | 4704 found_end = (compl_shown_match == compl_first_match); |
610 | 4705 compl_shown_match = compl_shown_match->cp_prev; |
657 | 4706 found_end |= (compl_shown_match == compl_first_match); |
7 | 4707 } |
4708 else | |
610 | 4709 { |
909 | 4710 if (!allow_get_expansion) |
4711 { | |
4712 if (advance) | |
4713 { | |
4714 if (compl_shows_dir == BACKWARD) | |
4715 compl_pending -= todo + 1; | |
4716 else | |
4717 compl_pending += todo + 1; | |
4718 } | |
4719 return -1; | |
4720 } | |
4721 | |
836 | 4722 if (advance) |
4723 { | |
4724 if (compl_shows_dir == BACKWARD) | |
4725 --compl_pending; | |
4726 else | |
4727 ++compl_pending; | |
4728 } | |
657 | 4729 |
874 | 4730 /* Find matches. */ |
659 | 4731 num_matches = ins_compl_get_exp(&compl_startpos); |
909 | 4732 |
4733 /* handle any pending completions */ | |
4734 while (compl_pending != 0 && compl_direction == compl_shows_dir | |
836 | 4735 && advance) |
909 | 4736 { |
4737 if (compl_pending > 0 && compl_shown_match->cp_next != NULL) | |
4738 { | |
4739 compl_shown_match = compl_shown_match->cp_next; | |
4740 --compl_pending; | |
4741 } | |
4742 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL) | |
4743 { | |
4744 compl_shown_match = compl_shown_match->cp_prev; | |
4745 ++compl_pending; | |
4746 } | |
4747 else | |
4748 break; | |
4749 } | |
657 | 4750 found_end = FALSE; |
4751 } | |
4752 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0 | |
4753 && compl_leader != NULL | |
681 | 4754 && !ins_compl_equal(compl_shown_match, |
835 | 4755 compl_leader, (int)STRLEN(compl_leader))) |
657 | 4756 ++todo; |
4757 else | |
4758 /* Remember a matching item. */ | |
4759 found_compl = compl_shown_match; | |
4760 | |
4761 /* Stop at the end of the list when we found a usable match. */ | |
4762 if (found_end) | |
4763 { | |
4764 if (found_compl != NULL) | |
610 | 4765 { |
657 | 4766 compl_shown_match = found_compl; |
4767 break; | |
610 | 4768 } |
657 | 4769 todo = 1; /* use first usable match after wrapping around */ |
610 | 4770 } |
7 | 4771 } |
4772 | |
665 | 4773 /* Insert the text of the new completion, or the compl_leader. */ |
4774 if (insert_match) | |
4775 { | |
4776 if (!compl_get_longest || compl_used_match) | |
4777 ins_compl_insert(); | |
4778 else | |
1782 | 4779 ins_bytes(compl_leader + ins_compl_len()); |
665 | 4780 } |
4781 else | |
4782 compl_used_match = FALSE; | |
7 | 4783 |
4784 if (!allow_get_expansion) | |
4785 { | |
540 | 4786 /* may undisplay the popup menu first */ |
4787 ins_compl_upd_pum(); | |
4788 | |
665 | 4789 /* redraw to show the user what was inserted */ |
4790 update_screen(0); | |
4791 | |
540 | 4792 /* display the updated popup menu */ |
4793 ins_compl_show_pum(); | |
864 | 4794 #ifdef FEAT_GUI |
4795 if (gui.in_use) | |
4796 { | |
4797 /* Show the cursor after the match, not after the redrawn text. */ | |
4798 setcursor(); | |
4799 out_flush(); | |
4800 gui_update_cursor(FALSE, FALSE); | |
4801 } | |
4802 #endif | |
540 | 4803 |
7 | 4804 /* Delete old text to be replaced, since we're still searching and |
4805 * don't want to match ourselves! */ | |
4806 ins_compl_delete(); | |
4807 } | |
4808 | |
825 | 4809 /* Enter will select a match when the match wasn't inserted and the popup |
1219 | 4810 * menu is visible. */ |
825 | 4811 compl_enter_selects = !insert_match && compl_match_array != NULL; |
4812 | |
7 | 4813 /* |
4814 * Show the file name for the match (if any) | |
4815 * Truncate the file name to avoid a wait for return. | |
4816 */ | |
464 | 4817 if (compl_shown_match->cp_fname != NULL) |
7 | 4818 { |
4819 STRCPY(IObuff, "match in file "); | |
464 | 4820 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col; |
7 | 4821 if (i <= 0) |
4822 i = 0; | |
4823 else | |
4824 STRCAT(IObuff, "<"); | |
464 | 4825 STRCAT(IObuff, compl_shown_match->cp_fname + i); |
7 | 4826 msg(IObuff); |
4827 redraw_cmdline = FALSE; /* don't overwrite! */ | |
4828 } | |
4829 | |
4830 return num_matches; | |
4831 } | |
4832 | |
4833 /* | |
4834 * Call this while finding completions, to check whether the user has hit a key | |
4835 * that should change the currently displayed completion, or exit completion | |
716 | 4836 * mode. Also, when compl_pending is not zero, show a completion as soon as |
7 | 4837 * possible. -- webb |
464 | 4838 * "frequency" specifies out of how many calls we actually check. |
7 | 4839 */ |
4840 void | |
464 | 4841 ins_compl_check_keys(frequency) |
4842 int frequency; | |
7 | 4843 { |
4844 static int count = 0; | |
4845 | |
4846 int c; | |
4847 | |
4848 /* Don't check when reading keys from a script. That would break the test | |
4849 * scripts */ | |
4850 if (using_script()) | |
4851 return; | |
4852 | |
4853 /* Only do this at regular intervals */ | |
464 | 4854 if (++count < frequency) |
7 | 4855 return; |
4856 count = 0; | |
4857 | |
909 | 4858 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key() |
4859 * can't do its work correctly. */ | |
7 | 4860 c = vpeekc_any(); |
4861 if (c != NUL) | |
4862 { | |
4863 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R) | |
4864 { | |
4865 c = safe_vgetc(); /* Eat the character */ | |
610 | 4866 compl_shows_dir = ins_compl_key2dir(c); |
665 | 4867 (void)ins_compl_next(FALSE, ins_compl_key2count(c), |
4868 c != K_UP && c != K_DOWN); | |
7 | 4869 } |
909 | 4870 else |
4871 { | |
4872 /* Need to get the character to have KeyTyped set. We'll put it | |
1526 | 4873 * back with vungetc() below. But skip K_IGNORE. */ |
909 | 4874 c = safe_vgetc(); |
1526 | 4875 if (c != K_IGNORE) |
4876 { | |
4877 /* Don't interrupt completion when the character wasn't typed, | |
4878 * e.g., when doing @q to replay keys. */ | |
4879 if (c != Ctrl_R && KeyTyped) | |
4880 compl_interrupted = TRUE; | |
4881 | |
4882 vungetc(c); | |
4883 } | |
909 | 4884 } |
449 | 4885 } |
716 | 4886 if (compl_pending != 0 && !got_int) |
909 | 4887 { |
4888 int todo = compl_pending > 0 ? compl_pending : -compl_pending; | |
4889 | |
4890 compl_pending = 0; | |
4891 (void)ins_compl_next(FALSE, todo, TRUE); | |
4892 } | |
610 | 4893 } |
4894 | |
4895 /* | |
4896 * Decide the direction of Insert mode complete from the key typed. | |
4897 * Returns BACKWARD or FORWARD. | |
4898 */ | |
4899 static int | |
4900 ins_compl_key2dir(c) | |
4901 int c; | |
4902 { | |
665 | 4903 if (c == Ctrl_P || c == Ctrl_L |
4904 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP | |
4905 || c == K_S_UP || c == K_UP))) | |
610 | 4906 return BACKWARD; |
4907 return FORWARD; | |
4908 } | |
4909 | |
4910 /* | |
644 | 4911 * Return TRUE for keys that are used for completion only when the popup menu |
4912 * is visible. | |
4913 */ | |
4914 static int | |
4915 ins_compl_pum_key(c) | |
4916 int c; | |
4917 { | |
4918 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP | |
665 | 4919 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN |
4920 || c == K_UP || c == K_DOWN); | |
644 | 4921 } |
4922 | |
4923 /* | |
610 | 4924 * Decide the number of completions to move forward. |
4925 * Returns 1 for most keys, height of the popup menu for page-up/down keys. | |
4926 */ | |
4927 static int | |
4928 ins_compl_key2count(c) | |
4929 int c; | |
4930 { | |
4931 int h; | |
4932 | |
665 | 4933 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN) |
610 | 4934 { |
4935 h = pum_get_height(); | |
4936 if (h > 3) | |
4937 h -= 2; /* keep some context */ | |
4938 return h; | |
4939 } | |
4940 return 1; | |
7 | 4941 } |
4942 | |
4943 /* | |
681 | 4944 * Return TRUE if completion with "c" should insert the match, FALSE if only |
4945 * to change the currently selected completion. | |
4946 */ | |
4947 static int | |
4948 ins_compl_use_match(c) | |
4949 int c; | |
4950 { | |
4951 switch (c) | |
4952 { | |
4953 case K_UP: | |
4954 case K_DOWN: | |
4955 case K_PAGEDOWN: | |
4956 case K_KPAGEDOWN: | |
4957 case K_S_DOWN: | |
4958 case K_PAGEUP: | |
4959 case K_KPAGEUP: | |
4960 case K_S_UP: | |
4961 return FALSE; | |
4962 } | |
4963 return TRUE; | |
4964 } | |
4965 | |
4966 /* | |
7 | 4967 * Do Insert mode completion. |
4968 * Called when character "c" was typed, which has a meaning for completion. | |
4969 * Returns OK if completion was done, FAIL if something failed (out of mem). | |
4970 */ | |
4971 static int | |
4972 ins_complete(c) | |
449 | 4973 int c; |
7 | 4974 { |
449 | 4975 char_u *line; |
4976 int startcol = 0; /* column where searched text starts */ | |
4977 colnr_T curs_col; /* cursor column */ | |
4978 int n; | |
2107
4a4287c09953
updated for version 7.2.390
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
4979 int save_w_wrow; |
7 | 4980 |
610 | 4981 compl_direction = ins_compl_key2dir(c); |
449 | 4982 if (!compl_started) |
7 | 4983 { |
4984 /* First time we hit ^N or ^P (in a row, I mean) */ | |
4985 | |
4986 did_ai = FALSE; | |
4987 #ifdef FEAT_SMARTINDENT | |
4988 did_si = FALSE; | |
4989 can_si = FALSE; | |
4990 can_si_back = FALSE; | |
4991 #endif | |
4992 if (stop_arrow() == FAIL) | |
4993 return FAIL; | |
4994 | |
4995 line = ml_get(curwin->w_cursor.lnum); | |
449 | 4996 curs_col = curwin->w_cursor.col; |
716 | 4997 compl_pending = 0; |
7 | 4998 |
1430 | 4999 /* If this same ctrl_x_mode has been interrupted use the text from |
449 | 5000 * "compl_startpos" to the cursor as a pattern to add a new word |
5001 * instead of expand the one before the cursor, in word-wise if | |
1430 | 5002 * "compl_startpos" is not in the same line as the cursor then fix it |
5003 * (the line has been split because it was longer than 'tw'). if SOL | |
5004 * is set then skip the previous pattern, a word at the beginning of | |
5005 * the line has been inserted, we'll look for that -- Acevedo. */ | |
665 | 5006 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT |
5007 && compl_cont_mode == ctrl_x_mode) | |
7 | 5008 { |
5009 /* | |
5010 * it is a continued search | |
5011 */ | |
449 | 5012 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */ |
7 | 5013 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS |
5014 || ctrl_x_mode == CTRL_X_PATH_DEFINES) | |
5015 { | |
449 | 5016 if (compl_startpos.lnum != curwin->w_cursor.lnum) |
7 | 5017 { |
449 | 5018 /* line (probably) wrapped, set compl_startpos to the |
5019 * first non_blank in the line, if it is not a wordchar | |
5020 * include it to get a better pattern, but then we don't | |
5021 * want the "\\<" prefix, check it bellow */ | |
5022 compl_col = (colnr_T)(skipwhite(line) - line); | |
5023 compl_startpos.col = compl_col; | |
5024 compl_startpos.lnum = curwin->w_cursor.lnum; | |
5025 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */ | |
7 | 5026 } |
5027 else | |
5028 { | |
5029 /* S_IPOS was set when we inserted a word that was at the | |
5030 * beginning of the line, which means that we'll go to SOL | |
449 | 5031 * mode but first we need to redefine compl_startpos */ |
5032 if (compl_cont_status & CONT_S_IPOS) | |
7 | 5033 { |
449 | 5034 compl_cont_status |= CONT_SOL; |
5035 compl_startpos.col = (colnr_T)(skipwhite( | |
5036 line + compl_length | |
5037 + compl_startpos.col) - line); | |
7 | 5038 } |
449 | 5039 compl_col = compl_startpos.col; |
7 | 5040 } |
449 | 5041 compl_length = curwin->w_cursor.col - (int)compl_col; |
502 | 5042 /* IObuff is used to add a "word from the next line" would we |
1796 | 5043 * have enough space? just being paranoid */ |
7 | 5044 #define MIN_SPACE 75 |
449 | 5045 if (compl_length > (IOSIZE - MIN_SPACE)) |
7 | 5046 { |
449 | 5047 compl_cont_status &= ~CONT_SOL; |
5048 compl_length = (IOSIZE - MIN_SPACE); | |
5049 compl_col = curwin->w_cursor.col - compl_length; | |
7 | 5050 } |
449 | 5051 compl_cont_status |= CONT_ADDING | CONT_N_ADDS; |
5052 if (compl_length < 1) | |
5053 compl_cont_status &= CONT_LOCAL; | |
7 | 5054 } |
5055 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE) | |
449 | 5056 compl_cont_status = CONT_ADDING | CONT_N_ADDS; |
7 | 5057 else |
449 | 5058 compl_cont_status = 0; |
7 | 5059 } |
5060 else | |
449 | 5061 compl_cont_status &= CONT_LOCAL; |
5062 | |
5063 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */ | |
5064 { | |
5065 compl_cont_mode = ctrl_x_mode; | |
7 | 5066 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */ |
449 | 5067 compl_cont_status = 0; |
5068 compl_cont_status |= CONT_N_ADDS; | |
5069 compl_startpos = curwin->w_cursor; | |
5070 startcol = (int)curs_col; | |
5071 compl_col = 0; | |
7 | 5072 } |
5073 | |
5074 /* Work out completion pattern and original text -- webb */ | |
5075 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT)) | |
5076 { | |
449 | 5077 if ((compl_cont_status & CONT_SOL) |
7 | 5078 || ctrl_x_mode == CTRL_X_PATH_DEFINES) |
5079 { | |
449 | 5080 if (!(compl_cont_status & CONT_ADDING)) |
7 | 5081 { |
449 | 5082 while (--startcol >= 0 && vim_isIDc(line[startcol])) |
7 | 5083 ; |
449 | 5084 compl_col += ++startcol; |
5085 compl_length = curs_col - startcol; | |
7 | 5086 } |
5087 if (p_ic) | |
449 | 5088 compl_pattern = str_foldcase(line + compl_col, |
5089 compl_length, NULL, 0); | |
7 | 5090 else |
449 | 5091 compl_pattern = vim_strnsave(line + compl_col, |
5092 compl_length); | |
5093 if (compl_pattern == NULL) | |
7 | 5094 return FAIL; |
5095 } | |
449 | 5096 else if (compl_cont_status & CONT_ADDING) |
7 | 5097 { |
5098 char_u *prefix = (char_u *)"\\<"; | |
5099 | |
1872 | 5100 /* we need up to 2 extra chars for the prefix */ |
449 | 5101 compl_pattern = alloc(quote_meta(NULL, line + compl_col, |
1872 | 5102 compl_length) + 2); |
449 | 5103 if (compl_pattern == NULL) |
7 | 5104 return FAIL; |
449 | 5105 if (!vim_iswordp(line + compl_col) |
5106 || (compl_col > 0 | |
7 | 5107 && ( |
5108 #ifdef FEAT_MBYTE | |
449 | 5109 vim_iswordp(mb_prevptr(line, line + compl_col)) |
7 | 5110 #else |
449 | 5111 vim_iswordc(line[compl_col - 1]) |
7 | 5112 #endif |
5113 ))) | |
5114 prefix = (char_u *)""; | |
449 | 5115 STRCPY((char *)compl_pattern, prefix); |
5116 (void)quote_meta(compl_pattern + STRLEN(prefix), | |
5117 line + compl_col, compl_length); | |
7 | 5118 } |
449 | 5119 else if (--startcol < 0 || |
7 | 5120 #ifdef FEAT_MBYTE |
449 | 5121 !vim_iswordp(mb_prevptr(line, line + startcol + 1)) |
7 | 5122 #else |
449 | 5123 !vim_iswordc(line[startcol]) |
7 | 5124 #endif |
5125 ) | |
5126 { | |
5127 /* Match any word of at least two chars */ | |
449 | 5128 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k"); |
5129 if (compl_pattern == NULL) | |
7 | 5130 return FAIL; |
449 | 5131 compl_col += curs_col; |
5132 compl_length = 0; | |
7 | 5133 } |
5134 else | |
5135 { | |
5136 #ifdef FEAT_MBYTE | |
5137 /* Search the point of change class of multibyte character | |
5138 * or not a word single byte character backward. */ | |
5139 if (has_mbyte) | |
5140 { | |
5141 int base_class; | |
5142 int head_off; | |
5143 | |
449 | 5144 startcol -= (*mb_head_off)(line, line + startcol); |
5145 base_class = mb_get_class(line + startcol); | |
5146 while (--startcol >= 0) | |
7 | 5147 { |
449 | 5148 head_off = (*mb_head_off)(line, line + startcol); |
5149 if (base_class != mb_get_class(line + startcol | |
5150 - head_off)) | |
7 | 5151 break; |
449 | 5152 startcol -= head_off; |
7 | 5153 } |
5154 } | |
5155 else | |
5156 #endif | |
449 | 5157 while (--startcol >= 0 && vim_iswordc(line[startcol])) |
7 | 5158 ; |
449 | 5159 compl_col += ++startcol; |
5160 compl_length = (int)curs_col - startcol; | |
5161 if (compl_length == 1) | |
7 | 5162 { |
5163 /* Only match word with at least two chars -- webb | |
5164 * there's no need to call quote_meta, | |
5165 * alloc(7) is enough -- Acevedo | |
5166 */ | |
449 | 5167 compl_pattern = alloc(7); |
5168 if (compl_pattern == NULL) | |
7 | 5169 return FAIL; |
449 | 5170 STRCPY((char *)compl_pattern, "\\<"); |
5171 (void)quote_meta(compl_pattern + 2, line + compl_col, 1); | |
5172 STRCAT((char *)compl_pattern, "\\k"); | |
7 | 5173 } |
5174 else | |
5175 { | |
449 | 5176 compl_pattern = alloc(quote_meta(NULL, line + compl_col, |
1872 | 5177 compl_length) + 2); |
449 | 5178 if (compl_pattern == NULL) |
7 | 5179 return FAIL; |
449 | 5180 STRCPY((char *)compl_pattern, "\\<"); |
5181 (void)quote_meta(compl_pattern + 2, line + compl_col, | |
5182 compl_length); | |
7 | 5183 } |
5184 } | |
5185 } | |
5186 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE) | |
5187 { | |
835 | 5188 compl_col = (colnr_T)(skipwhite(line) - line); |
449 | 5189 compl_length = (int)curs_col - (int)compl_col; |
5190 if (compl_length < 0) /* cursor in indent: empty pattern */ | |
5191 compl_length = 0; | |
7 | 5192 if (p_ic) |
449 | 5193 compl_pattern = str_foldcase(line + compl_col, compl_length, |
5194 NULL, 0); | |
7 | 5195 else |
449 | 5196 compl_pattern = vim_strnsave(line + compl_col, compl_length); |
5197 if (compl_pattern == NULL) | |
7 | 5198 return FAIL; |
5199 } | |
5200 else if (ctrl_x_mode == CTRL_X_FILES) | |
5201 { | |
5332 | 5202 /* Go back to just before the first filename character. */ |
5349 | 5203 if (startcol > 0) |
5204 { | |
5205 char_u *p = line + startcol; | |
5206 | |
5332 | 5207 mb_ptr_back(line, p); |
5349 | 5208 while (p > line && vim_isfilec(PTR2CHAR(p))) |
5209 mb_ptr_back(line, p); | |
5210 if (p == line && vim_isfilec(PTR2CHAR(p))) | |
5211 startcol = 0; | |
5212 else | |
5213 startcol = (int)(p - line) + 1; | |
5214 } | |
5345 | 5215 |
5216 compl_col += startcol; | |
449 | 5217 compl_length = (int)curs_col - startcol; |
5218 compl_pattern = addstar(line + compl_col, compl_length, | |
5219 EXPAND_FILES); | |
5220 if (compl_pattern == NULL) | |
7 | 5221 return FAIL; |
5222 } | |
5223 else if (ctrl_x_mode == CTRL_X_CMDLINE) | |
5224 { | |
449 | 5225 compl_pattern = vim_strnsave(line, curs_col); |
5226 if (compl_pattern == NULL) | |
7 | 5227 return FAIL; |
449 | 5228 set_cmd_context(&compl_xp, compl_pattern, |
5229 (int)STRLEN(compl_pattern), curs_col); | |
5230 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL | |
5231 || compl_xp.xp_context == EXPAND_NOTHING) | |
935 | 5232 /* No completion possible, use an empty pattern to get a |
5233 * "pattern not found" message. */ | |
897 | 5234 compl_col = curs_col; |
5235 else | |
935 | 5236 compl_col = (int)(compl_xp.xp_pattern - compl_pattern); |
5237 compl_length = curs_col - compl_col; | |
7 | 5238 } |
523 | 5239 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI) |
502 | 5240 { |
12 | 5241 #ifdef FEAT_COMPL_FUNC |
5242 /* | |
502 | 5243 * Call user defined function 'completefunc' with "a:findstart" |
5244 * set to 1 to obtain the length of text to use for completion. | |
12 | 5245 */ |
502 | 5246 char_u *args[2]; |
452 | 5247 int col; |
502 | 5248 char_u *funcname; |
5249 pos_T pos; | |
2631 | 5250 win_T *curwin_save; |
5251 buf_T *curbuf_save; | |
502 | 5252 |
523 | 5253 /* Call 'completefunc' or 'omnifunc' and get pattern length as a |
502 | 5254 * string */ |
5255 funcname = ctrl_x_mode == CTRL_X_FUNCTION | |
5256 ? curbuf->b_p_cfu : curbuf->b_p_ofu; | |
5257 if (*funcname == NUL) | |
523 | 5258 { |
5259 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION | |
5260 ? "completefunc" : "omnifunc"); | |
12 | 5261 return FAIL; |
523 | 5262 } |
452 | 5263 |
5264 args[0] = (char_u *)"1"; | |
502 | 5265 args[1] = NULL; |
5266 pos = curwin->w_cursor; | |
2631 | 5267 curwin_save = curwin; |
5268 curbuf_save = curbuf; | |
502 | 5269 col = call_func_retnr(funcname, 2, args, FALSE); |
2631 | 5270 if (curwin_save != curwin || curbuf_save != curbuf) |
5271 { | |
5272 EMSG(_(e_complwin)); | |
5273 return FAIL; | |
5274 } | |
502 | 5275 curwin->w_cursor = pos; /* restore the cursor position */ |
6209 | 5276 validate_cursor(); |
2631 | 5277 if (!equalpos(curwin->w_cursor, pos)) |
5278 { | |
5279 EMSG(_(e_compldel)); | |
5280 return FAIL; | |
5281 } | |
502 | 5282 |
3302 | 5283 /* Return value -2 means the user complete function wants to |
3516 | 5284 * cancel the complete without an error. |
5285 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/ | |
3302 | 5286 if (col == -2) |
5287 return FAIL; | |
3516 | 5288 if (col == -3) |
5289 { | |
5290 ctrl_x_mode = 0; | |
5291 edit_submode = NULL; | |
5946 | 5292 if (!shortmess(SHM_COMPLETIONMENU)) |
5293 msg_clr_cmdline(); | |
3516 | 5294 return FAIL; |
5295 } | |
3302 | 5296 |
3078 | 5297 /* |
5298 * Reset extended parameters of completion, when start new | |
5299 * completion. | |
5300 */ | |
5301 compl_opt_refresh_always = FALSE; | |
5302 | |
452 | 5303 if (col < 0) |
523 | 5304 col = curs_col; |
452 | 5305 compl_col = col; |
1872 | 5306 if (compl_col > curs_col) |
452 | 5307 compl_col = curs_col; |
449 | 5308 |
5309 /* Setup variables for completion. Need to obtain "line" again, | |
5310 * it may have become invalid. */ | |
5311 line = ml_get(curwin->w_cursor.lnum); | |
452 | 5312 compl_length = curs_col - compl_col; |
449 | 5313 compl_pattern = vim_strnsave(line + compl_col, compl_length); |
5314 if (compl_pattern == NULL) | |
502 | 5315 #endif |
449 | 5316 return FAIL; |
5317 } | |
477 | 5318 else if (ctrl_x_mode == CTRL_X_SPELL) |
5319 { | |
744 | 5320 #ifdef FEAT_SPELL |
497 | 5321 if (spell_bad_len > 0) |
5322 compl_col = curs_col - spell_bad_len; | |
5323 else | |
5324 compl_col = spell_word_start(startcol); | |
5325 if (compl_col >= (colnr_T)startcol) | |
897 | 5326 { |
5327 compl_length = 0; | |
5328 compl_col = curs_col; | |
5329 } | |
5330 else | |
5331 { | |
5332 spell_expand_check_cap(compl_col); | |
5333 compl_length = (int)curs_col - compl_col; | |
5334 } | |
818 | 5335 /* Need to obtain "line" again, it may have become invalid. */ |
5336 line = ml_get(curwin->w_cursor.lnum); | |
477 | 5337 compl_pattern = vim_strnsave(line + compl_col, compl_length); |
5338 if (compl_pattern == NULL) | |
5339 #endif | |
5340 return FAIL; | |
5341 } | |
449 | 5342 else |
5343 { | |
5344 EMSG2(_(e_intern2), "ins_complete()"); | |
5345 return FAIL; | |
5346 } | |
5347 | |
5348 if (compl_cont_status & CONT_ADDING) | |
7 | 5349 { |
5350 edit_submode_pre = (char_u *)_(" Adding"); | |
5351 if (ctrl_x_mode == CTRL_X_WHOLE_LINE) | |
5352 { | |
5353 /* Insert a new line, keep indentation but ignore 'comments' */ | |
5354 #ifdef FEAT_COMMENTS | |
5355 char_u *old = curbuf->b_p_com; | |
5356 | |
5357 curbuf->b_p_com = (char_u *)""; | |
5358 #endif | |
449 | 5359 compl_startpos.lnum = curwin->w_cursor.lnum; |
5360 compl_startpos.col = compl_col; | |
7 | 5361 ins_eol('\r'); |
5362 #ifdef FEAT_COMMENTS | |
5363 curbuf->b_p_com = old; | |
5364 #endif | |
449 | 5365 compl_length = 0; |
5366 compl_col = curwin->w_cursor.col; | |
7 | 5367 } |
5368 } | |
5369 else | |
5370 { | |
5371 edit_submode_pre = NULL; | |
449 | 5372 compl_startpos.col = compl_col; |
5373 } | |
5374 | |
5375 if (compl_cont_status & CONT_LOCAL) | |
5376 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]); | |
7 | 5377 else |
5378 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode)); | |
5379 | |
3095 | 5380 /* If any of the original typed text has been changed we need to fix |
5381 * the redo buffer. */ | |
5382 ins_compl_fixRedoBufForLeader(NULL); | |
5383 | |
694 | 5384 /* Always add completion for the original text. */ |
5385 vim_free(compl_orig_text); | |
449 | 5386 compl_orig_text = vim_strnsave(line + compl_col, compl_length); |
5387 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text, | |
942 | 5388 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK) |
449 | 5389 { |
5390 vim_free(compl_pattern); | |
5391 compl_pattern = NULL; | |
5392 vim_free(compl_orig_text); | |
5393 compl_orig_text = NULL; | |
7 | 5394 return FAIL; |
5395 } | |
5396 | |
5397 /* showmode might reset the internal line pointers, so it must | |
5398 * be called before line = ml_get(), or when this address is no | |
5399 * longer needed. -- Acevedo. | |
5400 */ | |
5401 edit_submode_extra = (char_u *)_("-- Searching..."); | |
5402 edit_submode_highl = HLF_COUNT; | |
5403 showmode(); | |
5404 edit_submode_extra = NULL; | |
5405 out_flush(); | |
5406 } | |
5407 | |
449 | 5408 compl_shown_match = compl_curr_match; |
5409 compl_shows_dir = compl_direction; | |
7 | 5410 |
5411 /* | |
665 | 5412 * Find next match (and following matches). |
7 | 5413 */ |
2107
4a4287c09953
updated for version 7.2.390
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
5414 save_w_wrow = curwin->w_wrow; |
681 | 5415 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c)); |
449 | 5416 |
540 | 5417 /* may undisplay the popup menu */ |
5418 ins_compl_upd_pum(); | |
5419 | |
449 | 5420 if (n > 1) /* all matches have been found */ |
5421 compl_matches = n; | |
5422 compl_curr_match = compl_shown_match; | |
5423 compl_direction = compl_shows_dir; | |
857 | 5424 |
5425 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert | |
5426 * mode. */ | |
7 | 5427 if (got_int && !global_busy) |
5428 { | |
5429 (void)vgetc(); | |
5430 got_int = FALSE; | |
5431 } | |
5432 | |
449 | 5433 /* we found no match if the list has only the "compl_orig_text"-entry */ |
464 | 5434 if (compl_first_match == compl_first_match->cp_next) |
449 | 5435 { |
5436 edit_submode_extra = (compl_cont_status & CONT_ADDING) | |
5437 && compl_length > 1 | |
7 | 5438 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf); |
5439 edit_submode_highl = HLF_E; | |
5440 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode, | |
5441 * because we couldn't expand anything at first place, but if we used | |
5442 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word | |
5443 * (such as M in M'exico) if not tried already. -- Acevedo */ | |
449 | 5444 if ( compl_length > 1 |
5445 || (compl_cont_status & CONT_ADDING) | |
7 | 5446 || (ctrl_x_mode != 0 |
5447 && ctrl_x_mode != CTRL_X_PATH_PATTERNS | |
5448 && ctrl_x_mode != CTRL_X_PATH_DEFINES)) | |
449 | 5449 compl_cont_status &= ~CONT_N_ADDS; |
5450 } | |
5451 | |
464 | 5452 if (compl_curr_match->cp_flags & CONT_S_IPOS) |
449 | 5453 compl_cont_status |= CONT_S_IPOS; |
7 | 5454 else |
449 | 5455 compl_cont_status &= ~CONT_S_IPOS; |
7 | 5456 |
5457 if (edit_submode_extra == NULL) | |
5458 { | |
464 | 5459 if (compl_curr_match->cp_flags & ORIGINAL_TEXT) |
7 | 5460 { |
5461 edit_submode_extra = (char_u *)_("Back at original"); | |
5462 edit_submode_highl = HLF_W; | |
5463 } | |
449 | 5464 else if (compl_cont_status & CONT_S_IPOS) |
7 | 5465 { |
5466 edit_submode_extra = (char_u *)_("Word from other line"); | |
5467 edit_submode_highl = HLF_COUNT; | |
5468 } | |
464 | 5469 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev) |
7 | 5470 { |
5471 edit_submode_extra = (char_u *)_("The only match"); | |
5472 edit_submode_highl = HLF_COUNT; | |
5473 } | |
5474 else | |
5475 { | |
5476 /* Update completion sequence number when needed. */ | |
464 | 5477 if (compl_curr_match->cp_number == -1) |
7 | 5478 { |
464 | 5479 int number = 0; |
5480 compl_T *match; | |
7 | 5481 |
449 | 5482 if (compl_direction == FORWARD) |
7 | 5483 { |
5484 /* search backwards for the first valid (!= -1) number. | |
5485 * This should normally succeed already at the first loop | |
5486 * cycle, so it's fast! */ | |
464 | 5487 for (match = compl_curr_match->cp_prev; match != NULL |
5488 && match != compl_first_match; | |
5489 match = match->cp_prev) | |
5490 if (match->cp_number != -1) | |
7 | 5491 { |
464 | 5492 number = match->cp_number; |
7 | 5493 break; |
5494 } | |
5495 if (match != NULL) | |
5496 /* go up and assign all numbers which are not assigned | |
5497 * yet */ | |
540 | 5498 for (match = match->cp_next; |
5499 match != NULL && match->cp_number == -1; | |
464 | 5500 match = match->cp_next) |
5501 match->cp_number = ++number; | |
7 | 5502 } |
5503 else /* BACKWARD */ | |
5504 { | |
5505 /* search forwards (upwards) for the first valid (!= -1) | |
5506 * number. This should normally succeed already at the | |
5507 * first loop cycle, so it's fast! */ | |
464 | 5508 for (match = compl_curr_match->cp_next; match != NULL |
5509 && match != compl_first_match; | |
5510 match = match->cp_next) | |
5511 if (match->cp_number != -1) | |
7 | 5512 { |
464 | 5513 number = match->cp_number; |
7 | 5514 break; |
5515 } | |
5516 if (match != NULL) | |
5517 /* go down and assign all numbers which are not | |
5518 * assigned yet */ | |
464 | 5519 for (match = match->cp_prev; match |
5520 && match->cp_number == -1; | |
5521 match = match->cp_prev) | |
5522 match->cp_number = ++number; | |
7 | 5523 } |
5524 } | |
5525 | |
540 | 5526 /* The match should always have a sequence number now, this is |
5527 * just a safety check. */ | |
464 | 5528 if (compl_curr_match->cp_number != -1) |
7 | 5529 { |
1063 | 5530 /* Space for 10 text chars. + 2x10-digit no.s = 31. |
5531 * Translations may need more than twice that. */ | |
5532 static char_u match_ref[81]; | |
7 | 5533 |
449 | 5534 if (compl_matches > 0) |
1063 | 5535 vim_snprintf((char *)match_ref, sizeof(match_ref), |
5536 _("match %d of %d"), | |
464 | 5537 compl_curr_match->cp_number, compl_matches); |
7 | 5538 else |
1063 | 5539 vim_snprintf((char *)match_ref, sizeof(match_ref), |
5540 _("match %d"), | |
5541 compl_curr_match->cp_number); | |
7 | 5542 edit_submode_extra = match_ref; |
5543 edit_submode_highl = HLF_R; | |
3318 | 5544 if (dollar_vcol >= 0) |
7 | 5545 curs_columns(FALSE); |
5546 } | |
5547 } | |
5548 } | |
5549 | |
5550 /* Show a message about what (completion) mode we're in. */ | |
5551 showmode(); | |
5946 | 5552 if (!shortmess(SHM_COMPLETIONMENU)) |
5553 { | |
5554 if (edit_submode_extra != NULL) | |
5555 { | |
5556 if (!p_smd) | |
5557 msg_attr(edit_submode_extra, | |
5558 edit_submode_highl < HLF_COUNT | |
5559 ? hl_attr(edit_submode_highl) : 0); | |
5560 } | |
5561 else | |
5562 msg_clr_cmdline(); /* necessary for "noshowmode" */ | |
5563 } | |
7 | 5564 |
857 | 5565 /* Show the popup menu, unless we got interrupted. */ |
5566 if (!compl_interrupted) | |
5567 { | |
5568 /* RedrawingDisabled may be set when invoked through complete(). */ | |
5569 n = RedrawingDisabled; | |
5570 RedrawingDisabled = 0; | |
2107
4a4287c09953
updated for version 7.2.390
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
5571 |
4a4287c09953
updated for version 7.2.390
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
5572 /* If the cursor moved we need to remove the pum first. */ |
4a4287c09953
updated for version 7.2.390
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
5573 setcursor(); |
4a4287c09953
updated for version 7.2.390
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
5574 if (save_w_wrow != curwin->w_wrow) |
4a4287c09953
updated for version 7.2.390
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
5575 ins_compl_del_pum(); |
4a4287c09953
updated for version 7.2.390
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
5576 |
857 | 5577 ins_compl_show_pum(); |
5578 setcursor(); | |
5579 RedrawingDisabled = n; | |
5580 } | |
874 | 5581 compl_was_interrupted = compl_interrupted; |
857 | 5582 compl_interrupted = FALSE; |
540 | 5583 |
7 | 5584 return OK; |
5585 } | |
5586 | |
5587 /* | |
5588 * Looks in the first "len" chars. of "src" for search-metachars. | |
5589 * If dest is not NULL the chars. are copied there quoting (with | |
5590 * a backslash) the metachars, and dest would be NUL terminated. | |
5591 * Returns the length (needed) of dest | |
5592 */ | |
1872 | 5593 static unsigned |
7 | 5594 quote_meta(dest, src, len) |
5595 char_u *dest; | |
5596 char_u *src; | |
5597 int len; | |
5598 { | |
1872 | 5599 unsigned m = (unsigned)len + 1; /* one extra for the NUL */ |
5600 | |
5601 for ( ; --len >= 0; src++) | |
7 | 5602 { |
5603 switch (*src) | |
5604 { | |
5605 case '.': | |
5606 case '*': | |
5607 case '[': | |
5608 if (ctrl_x_mode == CTRL_X_DICTIONARY | |
5609 || ctrl_x_mode == CTRL_X_THESAURUS) | |
5610 break; | |
5611 case '~': | |
5612 if (!p_magic) /* quote these only if magic is set */ | |
5613 break; | |
5614 case '\\': | |
5615 if (ctrl_x_mode == CTRL_X_DICTIONARY | |
5616 || ctrl_x_mode == CTRL_X_THESAURUS) | |
5617 break; | |
5618 case '^': /* currently it's not needed. */ | |
5619 case '$': | |
5620 m++; | |
5621 if (dest != NULL) | |
5622 *dest++ = '\\'; | |
5623 break; | |
5624 } | |
5625 if (dest != NULL) | |
5626 *dest++ = *src; | |
464 | 5627 # ifdef FEAT_MBYTE |
7 | 5628 /* Copy remaining bytes of a multibyte character. */ |
5629 if (has_mbyte) | |
5630 { | |
5631 int i, mb_len; | |
5632 | |
474 | 5633 mb_len = (*mb_ptr2len)(src) - 1; |
7 | 5634 if (mb_len > 0 && len >= mb_len) |
5635 for (i = 0; i < mb_len; ++i) | |
5636 { | |
5637 --len; | |
5638 ++src; | |
5639 if (dest != NULL) | |
5640 *dest++ = *src; | |
5641 } | |
5642 } | |
464 | 5643 # endif |
7 | 5644 } |
5645 if (dest != NULL) | |
5646 *dest = NUL; | |
5647 | |
5648 return m; | |
5649 } | |
5650 #endif /* FEAT_INS_EXPAND */ | |
5651 | |
5652 /* | |
5653 * Next character is interpreted literally. | |
5654 * A one, two or three digit decimal number is interpreted as its byte value. | |
5655 * If one or two digits are entered, the next character is given to vungetc(). | |
5656 * For Unicode a character > 255 may be returned. | |
5657 */ | |
5658 int | |
5659 get_literal() | |
5660 { | |
5661 int cc; | |
5662 int nc; | |
5663 int i; | |
5664 int hex = FALSE; | |
5665 int octal = FALSE; | |
5666 #ifdef FEAT_MBYTE | |
5667 int unicode = 0; | |
5668 #endif | |
5669 | |
5670 if (got_int) | |
5671 return Ctrl_C; | |
5672 | |
5673 #ifdef FEAT_GUI | |
5674 /* | |
5675 * In GUI there is no point inserting the internal code for a special key. | |
5676 * It is more useful to insert the string "<KEY>" instead. This would | |
5677 * probably be useful in a text window too, but it would not be | |
5678 * vi-compatible (maybe there should be an option for it?) -- webb | |
5679 */ | |
5680 if (gui.in_use) | |
5681 ++allow_keys; | |
5682 #endif | |
5683 #ifdef USE_ON_FLY_SCROLL | |
5684 dont_scroll = TRUE; /* disallow scrolling here */ | |
5685 #endif | |
5686 ++no_mapping; /* don't map the next key hits */ | |
5687 cc = 0; | |
5688 i = 0; | |
5689 for (;;) | |
5690 { | |
1389 | 5691 nc = plain_vgetc(); |
7 | 5692 #ifdef FEAT_CMDL_INFO |
5693 if (!(State & CMDLINE) | |
5694 # ifdef FEAT_MBYTE | |
5695 && MB_BYTE2LEN_CHECK(nc) == 1 | |
5696 # endif | |
5697 ) | |
5698 add_to_showcmd(nc); | |
5699 #endif | |
5700 if (nc == 'x' || nc == 'X') | |
5701 hex = TRUE; | |
5702 else if (nc == 'o' || nc == 'O') | |
5703 octal = TRUE; | |
5704 #ifdef FEAT_MBYTE | |
5705 else if (nc == 'u' || nc == 'U') | |
5706 unicode = nc; | |
5707 #endif | |
5708 else | |
5709 { | |
5710 if (hex | |
5711 #ifdef FEAT_MBYTE | |
5712 || unicode != 0 | |
5713 #endif | |
5714 ) | |
5715 { | |
5716 if (!vim_isxdigit(nc)) | |
5717 break; | |
5718 cc = cc * 16 + hex2nr(nc); | |
5719 } | |
5720 else if (octal) | |
5721 { | |
5722 if (nc < '0' || nc > '7') | |
5723 break; | |
5724 cc = cc * 8 + nc - '0'; | |
5725 } | |
5726 else | |
5727 { | |
5728 if (!VIM_ISDIGIT(nc)) | |
5729 break; | |
5730 cc = cc * 10 + nc - '0'; | |
5731 } | |
5732 | |
5733 ++i; | |
5734 } | |
5735 | |
5736 if (cc > 255 | |
5737 #ifdef FEAT_MBYTE | |
5738 && unicode == 0 | |
5739 #endif | |
5740 ) | |
5741 cc = 255; /* limit range to 0-255 */ | |
5742 nc = 0; | |
5743 | |
5744 if (hex) /* hex: up to two chars */ | |
5745 { | |
5746 if (i >= 2) | |
5747 break; | |
5748 } | |
5749 #ifdef FEAT_MBYTE | |
5750 else if (unicode) /* Unicode: up to four or eight chars */ | |
5751 { | |
5752 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8)) | |
5753 break; | |
5754 } | |
5755 #endif | |
5756 else if (i >= 3) /* decimal or octal: up to three chars */ | |
5757 break; | |
5758 } | |
5759 if (i == 0) /* no number entered */ | |
5760 { | |
5761 if (nc == K_ZERO) /* NUL is stored as NL */ | |
5762 { | |
5763 cc = '\n'; | |
5764 nc = 0; | |
5765 } | |
5766 else | |
5767 { | |
5768 cc = nc; | |
5769 nc = 0; | |
5770 } | |
5771 } | |
5772 | |
5773 if (cc == 0) /* NUL is stored as NL */ | |
5774 cc = '\n'; | |
221 | 5775 #ifdef FEAT_MBYTE |
5776 if (enc_dbcs && (cc & 0xff) == 0) | |
5777 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the | |
5778 second byte will cause trouble! */ | |
5779 #endif | |
7 | 5780 |
5781 --no_mapping; | |
5782 #ifdef FEAT_GUI | |
5783 if (gui.in_use) | |
5784 --allow_keys; | |
5785 #endif | |
5786 if (nc) | |
5787 vungetc(nc); | |
5788 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */ | |
5789 return cc; | |
5790 } | |
5791 | |
5792 /* | |
5793 * Insert character, taking care of special keys and mod_mask | |
5794 */ | |
5795 static void | |
5796 insert_special(c, allow_modmask, ctrlv) | |
5797 int c; | |
5798 int allow_modmask; | |
5799 int ctrlv; /* c was typed after CTRL-V */ | |
5800 { | |
5801 char_u *p; | |
5802 int len; | |
5803 | |
5804 /* | |
5805 * Special function key, translate into "<Key>". Up to the last '>' is | |
5806 * inserted with ins_str(), so as not to replace characters in replace | |
5807 * mode. | |
5808 * Only use mod_mask for special keys, to avoid things like <S-Space>, | |
5809 * unless 'allow_modmask' is TRUE. | |
5810 */ | |
5811 #ifdef MACOS | |
5812 /* Command-key never produces a normal key */ | |
5813 if (mod_mask & MOD_MASK_CMD) | |
5814 allow_modmask = TRUE; | |
5815 #endif | |
5816 if (IS_SPECIAL(c) || (mod_mask && allow_modmask)) | |
5817 { | |
5818 p = get_special_key_name(c, mod_mask); | |
5819 len = (int)STRLEN(p); | |
5820 c = p[len - 1]; | |
5821 if (len > 2) | |
5822 { | |
5823 if (stop_arrow() == FAIL) | |
5824 return; | |
5825 p[len - 1] = NUL; | |
5826 ins_str(p); | |
620 | 5827 AppendToRedobuffLit(p, -1); |
7 | 5828 ctrlv = FALSE; |
5829 } | |
5830 } | |
5831 if (stop_arrow() == OK) | |
5832 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1); | |
5833 } | |
5834 | |
5835 /* | |
5836 * Special characters in this context are those that need processing other | |
5837 * than the simple insertion that can be performed here. This includes ESC | |
5838 * which terminates the insert, and CR/NL which need special processing to | |
5839 * open up a new line. This routine tries to optimize insertions performed by | |
5840 * the "redo", "undo" or "put" commands, so it needs to know when it should | |
5841 * stop and defer processing to the "normal" mechanism. | |
5842 * '0' and '^' are special, because they can be followed by CTRL-D. | |
5843 */ | |
5844 #ifdef EBCDIC | |
5845 # define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^') | |
5846 #else | |
5847 # define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^') | |
5848 #endif | |
5849 | |
5850 #ifdef FEAT_MBYTE | |
5851 # define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1)))) | |
5852 #else | |
5853 # define WHITECHAR(cc) vim_iswhite(cc) | |
5854 #endif | |
5855 | |
3584 | 5856 /* |
5857 * "flags": INSCHAR_FORMAT - force formatting | |
5858 * INSCHAR_CTRLV - char typed just after CTRL-V | |
5859 * INSCHAR_NO_FEX - don't use 'formatexpr' | |
5860 * | |
5861 * NOTE: passes the flags value straight through to internal_format() which, | |
5862 * beside INSCHAR_FORMAT (above), is also looking for these: | |
5863 * INSCHAR_DO_COM - format comments | |
5864 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent | |
5865 */ | |
7 | 5866 void |
5867 insertchar(c, flags, second_indent) | |
5868 int c; /* character to insert or NUL */ | |
5869 int flags; /* INSCHAR_FORMAT, etc. */ | |
5870 int second_indent; /* indent for second line if >= 0 */ | |
5871 { | |
5872 int textwidth; | |
5873 #ifdef FEAT_COMMENTS | |
5874 char_u *p; | |
667 | 5875 #endif |
7 | 5876 int fo_ins_blank; |
5877 | |
5878 textwidth = comp_textwidth(flags & INSCHAR_FORMAT); | |
5879 fo_ins_blank = has_format_option(FO_INS_BLANK); | |
5880 | |
5881 /* | |
5882 * Try to break the line in two or more pieces when: | |
5883 * - Always do this if we have been called to do formatting only. | |
5884 * - Always do this when 'formatoptions' has the 'a' flag and the line | |
5885 * ends in white space. | |
5886 * - Otherwise: | |
5887 * - Don't do this if inserting a blank | |
5888 * - Don't do this if an existing character is being replaced, unless | |
5889 * we're in VREPLACE mode. | |
5890 * - Do this if the cursor is not on the line where insert started | |
5891 * or - 'formatoptions' doesn't have 'l' or the line was not too long | |
5892 * before the insert. | |
5893 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or | |
5894 * before 'textwidth' | |
5895 */ | |
667 | 5896 if (textwidth > 0 |
7 | 5897 && ((flags & INSCHAR_FORMAT) |
5898 || (!vim_iswhite(c) | |
5899 && !((State & REPLACE_FLAG) | |
5900 #ifdef FEAT_VREPLACE | |
5901 && !(State & VREPLACE_FLAG) | |
5902 #endif | |
5903 && *ml_get_cursor() != NUL) | |
5904 && (curwin->w_cursor.lnum != Insstart.lnum | |
5905 || ((!has_format_option(FO_INS_LONG) | |
5906 || Insstart_textlen <= (colnr_T)textwidth) | |
5907 && (!fo_ins_blank | |
5908 || Insstart_blank_vcol <= (colnr_T)textwidth | |
5909 )))))) | |
5910 { | |
667 | 5911 /* Format with 'formatexpr' when it's set. Use internal formatting |
5912 * when 'formatexpr' isn't set or it returns non-zero. */ | |
5913 #if defined(FEAT_EVAL) | |
1000 | 5914 int do_internal = TRUE; |
5915 | |
1563 | 5916 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0) |
1000 | 5917 { |
5918 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0); | |
5919 /* It may be required to save for undo again, e.g. when setline() | |
5920 * was called. */ | |
5921 ins_need_undo = TRUE; | |
5922 } | |
5923 if (do_internal) | |
667 | 5924 #endif |
2004 | 5925 internal_format(textwidth, second_indent, flags, c == NUL, c); |
667 | 5926 } |
5927 | |
7 | 5928 if (c == NUL) /* only formatting was wanted */ |
5929 return; | |
5930 | |
5931 #ifdef FEAT_COMMENTS | |
5932 /* Check whether this character should end a comment. */ | |
5933 if (did_ai && (int)c == end_comment_pending) | |
5934 { | |
5935 char_u *line; | |
5936 char_u lead_end[COM_MAX_LEN]; /* end-comment string */ | |
5937 int middle_len, end_len; | |
5938 int i; | |
5939 | |
5940 /* | |
5941 * Need to remove existing (middle) comment leader and insert end | |
5942 * comment leader. First, check what comment leader we can find. | |
5943 */ | |
3562 | 5944 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE); |
7 | 5945 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */ |
5946 { | |
5947 /* Skip middle-comment string */ | |
5948 while (*p && p[-1] != ':') /* find end of middle flags */ | |
5949 ++p; | |
5950 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ","); | |
5951 /* Don't count trailing white space for middle_len */ | |
5952 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1])) | |
5953 --middle_len; | |
5954 | |
5955 /* Find the end-comment string */ | |
5956 while (*p && p[-1] != ':') /* find end of end flags */ | |
5957 ++p; | |
5958 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ","); | |
5959 | |
5960 /* Skip white space before the cursor */ | |
5961 i = curwin->w_cursor.col; | |
5962 while (--i >= 0 && vim_iswhite(line[i])) | |
5963 ; | |
5964 i++; | |
5965 | |
5966 /* Skip to before the middle leader */ | |
5967 i -= middle_len; | |
5968 | |
5969 /* Check some expected things before we go on */ | |
5970 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending) | |
5971 { | |
5972 /* Backspace over all the stuff we want to replace */ | |
5973 backspace_until_column(i); | |
5974 | |
5975 /* | |
5976 * Insert the end-comment string, except for the last | |
5977 * character, which will get inserted as normal later. | |
5978 */ | |
5979 ins_bytes_len(lead_end, end_len - 1); | |
5980 } | |
5981 } | |
5982 } | |
5983 end_comment_pending = NUL; | |
5984 #endif | |
5985 | |
5986 did_ai = FALSE; | |
5987 #ifdef FEAT_SMARTINDENT | |
5988 did_si = FALSE; | |
5989 can_si = FALSE; | |
5990 can_si_back = FALSE; | |
5991 #endif | |
5992 | |
5993 /* | |
5994 * If there's any pending input, grab up to INPUT_BUFLEN at once. | |
5995 * This speeds up normal text input considerably. | |
5996 * Don't do this when 'cindent' or 'indentexpr' is set, because we might | |
5997 * need to re-indent at a ':', or any other character (but not what | |
5998 * 'paste' is set).. | |
3390 | 5999 * Don't do this when there an InsertCharPre autocommand is defined, |
6000 * because we need to fire the event for every character. | |
7 | 6001 */ |
6002 #ifdef USE_ON_FLY_SCROLL | |
6003 dont_scroll = FALSE; /* allow scrolling here */ | |
6004 #endif | |
6005 | |
6006 if ( !ISSPECIAL(c) | |
6007 #ifdef FEAT_MBYTE | |
6008 && (!has_mbyte || (*mb_char2len)(c) == 1) | |
6009 #endif | |
6010 && vpeekc() != NUL | |
6011 && !(State & REPLACE_FLAG) | |
6012 #ifdef FEAT_CINDENT | |
6013 && !cindent_on() | |
6014 #endif | |
6015 #ifdef FEAT_RIGHTLEFT | |
6016 && !p_ri | |
6017 #endif | |
3390 | 6018 #ifdef FEAT_AUTOCMD |
6019 && !has_insertcharpre() | |
6020 #endif | |
7 | 6021 ) |
6022 { | |
6023 #define INPUT_BUFLEN 100 | |
6024 char_u buf[INPUT_BUFLEN + 1]; | |
6025 int i; | |
6026 colnr_T virtcol = 0; | |
6027 | |
6028 buf[0] = c; | |
6029 i = 1; | |
667 | 6030 if (textwidth > 0) |
7 | 6031 virtcol = get_nolist_virtcol(); |
6032 /* | |
6033 * Stop the string when: | |
6034 * - no more chars available | |
6035 * - finding a special character (command key) | |
6036 * - buffer is full | |
6037 * - running into the 'textwidth' boundary | |
6038 * - need to check for abbreviation: A non-word char after a word-char | |
6039 */ | |
6040 while ( (c = vpeekc()) != NUL | |
6041 && !ISSPECIAL(c) | |
6042 #ifdef FEAT_MBYTE | |
6043 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1) | |
6044 #endif | |
6045 && i < INPUT_BUFLEN | |
6046 && (textwidth == 0 | |
6047 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth) | |
6048 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1]))) | |
6049 { | |
6050 #ifdef FEAT_RIGHTLEFT | |
6051 c = vgetc(); | |
6052 if (p_hkmap && KeyTyped) | |
6053 c = hkmap(c); /* Hebrew mode mapping */ | |
6054 # ifdef FEAT_FKMAP | |
6055 if (p_fkmap && KeyTyped) | |
6056 c = fkmap(c); /* Farsi mode mapping */ | |
6057 # endif | |
6058 buf[i++] = c; | |
6059 #else | |
6060 buf[i++] = vgetc(); | |
6061 #endif | |
6062 } | |
6063 | |
6064 #ifdef FEAT_DIGRAPHS | |
6065 do_digraph(-1); /* clear digraphs */ | |
6066 do_digraph(buf[i-1]); /* may be the start of a digraph */ | |
6067 #endif | |
6068 buf[i] = NUL; | |
6069 ins_str(buf); | |
6070 if (flags & INSCHAR_CTRLV) | |
6071 { | |
6072 redo_literal(*buf); | |
6073 i = 1; | |
6074 } | |
6075 else | |
6076 i = 0; | |
6077 if (buf[i] != NUL) | |
620 | 6078 AppendToRedobuffLit(buf + i, -1); |
7 | 6079 } |
6080 else | |
6081 { | |
6082 #ifdef FEAT_MBYTE | |
667 | 6083 int cc; |
6084 | |
7 | 6085 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1) |
6086 { | |
6087 char_u buf[MB_MAXBYTES + 1]; | |
6088 | |
6089 (*mb_char2bytes)(c, buf); | |
6090 buf[cc] = NUL; | |
6091 ins_char_bytes(buf, cc); | |
6092 AppendCharToRedobuff(c); | |
6093 } | |
6094 else | |
6095 #endif | |
6096 { | |
6097 ins_char(c); | |
6098 if (flags & INSCHAR_CTRLV) | |
6099 redo_literal(c); | |
6100 else | |
6101 AppendCharToRedobuff(c); | |
6102 } | |
6103 } | |
6104 } | |
6105 | |
6106 /* | |
667 | 6107 * Format text at the current insert position. |
3584 | 6108 * |
6109 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent | |
6110 * will be the comment leader length sent to open_line(). | |
667 | 6111 */ |
6112 static void | |
2004 | 6113 internal_format(textwidth, second_indent, flags, format_only, c) |
667 | 6114 int textwidth; |
6115 int second_indent; | |
6116 int flags; | |
6117 int format_only; | |
2004 | 6118 int c; /* character to be inserted (can be NUL) */ |
667 | 6119 { |
6120 int cc; | |
6121 int save_char = NUL; | |
6122 int haveto_redraw = FALSE; | |
6123 int fo_ins_blank = has_format_option(FO_INS_BLANK); | |
6124 #ifdef FEAT_MBYTE | |
6125 int fo_multibyte = has_format_option(FO_MBYTE_BREAK); | |
6126 #endif | |
6127 int fo_white_par = has_format_option(FO_WHITE_PAR); | |
6128 int first_line = TRUE; | |
6129 #ifdef FEAT_COMMENTS | |
6130 colnr_T leader_len; | |
6131 int no_leader = FALSE; | |
6132 int do_comments = (flags & INSCHAR_DO_COM); | |
6133 #endif | |
6199 | 6134 #ifdef FEAT_LINEBREAK |
6135 int has_lbr = curwin->w_p_lbr; | |
6136 | |
6137 /* make sure win_lbr_chartabsize() counts correctly */ | |
6138 curwin->w_p_lbr = FALSE; | |
6139 #endif | |
667 | 6140 |
6141 /* | |
6142 * When 'ai' is off we don't want a space under the cursor to be | |
6143 * deleted. Replace it with an 'x' temporarily. | |
6144 */ | |
2004 | 6145 if (!curbuf->b_p_ai |
6146 #ifdef FEAT_VREPLACE | |
6147 && !(State & VREPLACE_FLAG) | |
6148 #endif | |
6149 ) | |
667 | 6150 { |
6151 cc = gchar_cursor(); | |
6152 if (vim_iswhite(cc)) | |
6153 { | |
6154 save_char = cc; | |
6155 pchar_cursor('x'); | |
6156 } | |
6157 } | |
6158 | |
6159 /* | |
6160 * Repeat breaking lines, until the current line is not too long. | |
6161 */ | |
6162 while (!got_int) | |
6163 { | |
6164 int startcol; /* Cursor column at entry */ | |
6165 int wantcol; /* column at textwidth border */ | |
6166 int foundcol; /* column for start of spaces */ | |
6167 int end_foundcol = 0; /* column for start of word */ | |
6168 colnr_T len; | |
6169 colnr_T virtcol; | |
6170 #ifdef FEAT_VREPLACE | |
6171 int orig_col = 0; | |
6172 char_u *saved_text = NULL; | |
6173 #endif | |
6174 colnr_T col; | |
2004 | 6175 colnr_T end_col; |
6176 | |
6177 virtcol = get_nolist_virtcol() | |
6178 + char2cells(c != NUL ? c : gchar_cursor()); | |
6179 if (virtcol <= (colnr_T)textwidth) | |
667 | 6180 break; |
6181 | |
6182 #ifdef FEAT_COMMENTS | |
6183 if (no_leader) | |
6184 do_comments = FALSE; | |
6185 else if (!(flags & INSCHAR_FORMAT) | |
6186 && has_format_option(FO_WRAP_COMS)) | |
6187 do_comments = TRUE; | |
6188 | |
6189 /* Don't break until after the comment leader */ | |
6190 if (do_comments) | |
3562 | 6191 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE); |
667 | 6192 else |
6193 leader_len = 0; | |
6194 | |
6195 /* If the line doesn't start with a comment leader, then don't | |
6196 * start one in a following broken line. Avoids that a %word | |
6197 * moved to the start of the next line causes all following lines | |
6198 * to start with %. */ | |
6199 if (leader_len == 0) | |
6200 no_leader = TRUE; | |
6201 #endif | |
6202 if (!(flags & INSCHAR_FORMAT) | |
6203 #ifdef FEAT_COMMENTS | |
6204 && leader_len == 0 | |
6205 #endif | |
6206 && !has_format_option(FO_WRAP)) | |
6207 | |
6208 break; | |
6209 if ((startcol = curwin->w_cursor.col) == 0) | |
6210 break; | |
6211 | |
6212 /* find column of textwidth border */ | |
6213 coladvance((colnr_T)textwidth); | |
6214 wantcol = curwin->w_cursor.col; | |
6215 | |
2004 | 6216 curwin->w_cursor.col = startcol; |
667 | 6217 foundcol = 0; |
6218 | |
6219 /* | |
6220 * Find position to break at. | |
6221 * Stop at first entered white when 'formatoptions' has 'v' | |
6222 */ | |
6223 while ((!fo_ins_blank && !has_format_option(FO_INS_VI)) | |
3162 | 6224 || (flags & INSCHAR_FORMAT) |
667 | 6225 || curwin->w_cursor.lnum != Insstart.lnum |
6226 || curwin->w_cursor.col >= Insstart.col) | |
6227 { | |
2004 | 6228 if (curwin->w_cursor.col == startcol && c != NUL) |
6229 cc = c; | |
6230 else | |
6231 cc = gchar_cursor(); | |
667 | 6232 if (WHITECHAR(cc)) |
6233 { | |
6234 /* remember position of blank just before text */ | |
2004 | 6235 end_col = curwin->w_cursor.col; |
667 | 6236 |
6237 /* find start of sequence of blanks */ | |
6238 while (curwin->w_cursor.col > 0 && WHITECHAR(cc)) | |
6239 { | |
6240 dec_cursor(); | |
6241 cc = gchar_cursor(); | |
6242 } | |
6243 if (curwin->w_cursor.col == 0 && WHITECHAR(cc)) | |
6244 break; /* only spaces in front of text */ | |
6245 #ifdef FEAT_COMMENTS | |
6246 /* Don't break until after the comment leader */ | |
6247 if (curwin->w_cursor.col < leader_len) | |
6248 break; | |
6249 #endif | |
6250 if (has_format_option(FO_ONE_LETTER)) | |
6251 { | |
6252 /* do not break after one-letter words */ | |
6253 if (curwin->w_cursor.col == 0) | |
6254 break; /* one-letter word at begin */ | |
2004 | 6255 #ifdef FEAT_COMMENTS |
6256 /* do not break "#a b" when 'tw' is 2 */ | |
6257 if (curwin->w_cursor.col <= leader_len) | |
6258 break; | |
6259 #endif | |
667 | 6260 col = curwin->w_cursor.col; |
6261 dec_cursor(); | |
6262 cc = gchar_cursor(); | |
6263 | |
6264 if (WHITECHAR(cc)) | |
6265 continue; /* one-letter, continue */ | |
6266 curwin->w_cursor.col = col; | |
6267 } | |
2004 | 6268 |
6269 inc_cursor(); | |
6270 | |
6271 end_foundcol = end_col + 1; | |
6272 foundcol = curwin->w_cursor.col; | |
6273 if (curwin->w_cursor.col <= (colnr_T)wantcol) | |
667 | 6274 break; |
6275 } | |
6276 #ifdef FEAT_MBYTE | |
2004 | 6277 else if (cc >= 0x100 && fo_multibyte) |
667 | 6278 { |
6279 /* Break after or before a multi-byte character. */ | |
2004 | 6280 if (curwin->w_cursor.col != startcol) |
6281 { | |
6282 #ifdef FEAT_COMMENTS | |
6283 /* Don't break until after the comment leader */ | |
6284 if (curwin->w_cursor.col < leader_len) | |
6285 break; | |
6286 #endif | |
6287 col = curwin->w_cursor.col; | |
6288 inc_cursor(); | |
6289 /* Don't change end_foundcol if already set. */ | |
6290 if (foundcol != curwin->w_cursor.col) | |
6291 { | |
6292 foundcol = curwin->w_cursor.col; | |
6293 end_foundcol = foundcol; | |
6294 if (curwin->w_cursor.col <= (colnr_T)wantcol) | |
6295 break; | |
6296 } | |
6297 curwin->w_cursor.col = col; | |
6298 } | |
6299 | |
6300 if (curwin->w_cursor.col == 0) | |
6301 break; | |
6302 | |
6303 col = curwin->w_cursor.col; | |
6304 | |
6305 dec_cursor(); | |
6306 cc = gchar_cursor(); | |
6307 | |
6308 if (WHITECHAR(cc)) | |
6309 continue; /* break with space */ | |
6310 #ifdef FEAT_COMMENTS | |
6311 /* Don't break until after the comment leader */ | |
6312 if (curwin->w_cursor.col < leader_len) | |
6313 break; | |
6314 #endif | |
6315 | |
6316 curwin->w_cursor.col = col; | |
6317 | |
667 | 6318 foundcol = curwin->w_cursor.col; |
6319 end_foundcol = foundcol; | |
2004 | 6320 if (curwin->w_cursor.col <= (colnr_T)wantcol) |
6321 break; | |
667 | 6322 } |
6323 #endif | |
6324 if (curwin->w_cursor.col == 0) | |
6325 break; | |
6326 dec_cursor(); | |
6327 } | |
6328 | |
6329 if (foundcol == 0) /* no spaces, cannot break line */ | |
6330 { | |
6331 curwin->w_cursor.col = startcol; | |
6332 break; | |
6333 } | |
6334 | |
6335 /* Going to break the line, remove any "$" now. */ | |
6336 undisplay_dollar(); | |
6337 | |
6338 /* | |
6339 * Offset between cursor position and line break is used by replace | |
6340 * stack functions. VREPLACE does not use this, and backspaces | |
6341 * over the text instead. | |
6342 */ | |
6343 #ifdef FEAT_VREPLACE | |
6344 if (State & VREPLACE_FLAG) | |
6345 orig_col = startcol; /* Will start backspacing from here */ | |
6346 else | |
6347 #endif | |
2004 | 6348 replace_offset = startcol - end_foundcol; |
667 | 6349 |
6350 /* | |
6351 * adjust startcol for spaces that will be deleted and | |
6352 * characters that will remain on top line | |
6353 */ | |
6354 curwin->w_cursor.col = foundcol; | |
2004 | 6355 while ((cc = gchar_cursor(), WHITECHAR(cc)) |
6356 && (!fo_white_par || curwin->w_cursor.col < startcol)) | |
667 | 6357 inc_cursor(); |
6358 startcol -= curwin->w_cursor.col; | |
6359 if (startcol < 0) | |
6360 startcol = 0; | |
6361 | |
6362 #ifdef FEAT_VREPLACE | |
6363 if (State & VREPLACE_FLAG) | |
6364 { | |
6365 /* | |
6366 * In VREPLACE mode, we will backspace over the text to be | |
6367 * wrapped, so save a copy now to put on the next line. | |
6368 */ | |
6369 saved_text = vim_strsave(ml_get_cursor()); | |
6370 curwin->w_cursor.col = orig_col; | |
6371 if (saved_text == NULL) | |
6372 break; /* Can't do it, out of memory */ | |
6373 saved_text[startcol] = NUL; | |
6374 | |
6375 /* Backspace over characters that will move to the next line */ | |
6376 if (!fo_white_par) | |
6377 backspace_until_column(foundcol); | |
6378 } | |
6379 else | |
6380 #endif | |
6381 { | |
6382 /* put cursor after pos. to break line */ | |
6383 if (!fo_white_par) | |
6384 curwin->w_cursor.col = foundcol; | |
6385 } | |
6386 | |
6387 /* | |
6388 * Split the line just before the margin. | |
6389 * Only insert/delete lines, but don't really redraw the window. | |
6390 */ | |
6391 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX | |
6392 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0) | |
6393 #ifdef FEAT_COMMENTS | |
6394 + (do_comments ? OPENLINE_DO_COM : 0) | |
3584 | 6395 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0) |
6396 #endif | |
6397 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent)); | |
6398 if (!(flags & INSCHAR_COM_LIST)) | |
6399 old_indent = 0; | |
667 | 6400 |
6401 replace_offset = 0; | |
6402 if (first_line) | |
6403 { | |
3584 | 6404 if (!(flags & INSCHAR_COM_LIST)) |
6405 { | |
6406 /* | |
3632 | 6407 * This section is for auto-wrap of numeric lists. When not |
6408 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST | |
6409 * flag will be set and open_line() will handle it (as seen | |
6410 * above). The code here (and in get_number_indent()) will | |
6411 * recognize comments if needed... | |
3584 | 6412 */ |
6413 if (second_indent < 0 && has_format_option(FO_Q_NUMBER)) | |
3632 | 6414 second_indent = |
6415 get_number_indent(curwin->w_cursor.lnum - 1); | |
3584 | 6416 if (second_indent >= 0) |
6417 { | |
667 | 6418 #ifdef FEAT_VREPLACE |
3584 | 6419 if (State & VREPLACE_FLAG) |
6420 change_indent(INDENT_SET, second_indent, | |
6421 FALSE, NUL, TRUE); | |
6422 else | |
6423 #endif | |
3632 | 6424 #ifdef FEAT_COMMENTS |
6425 if (leader_len > 0 && second_indent - leader_len > 0) | |
6426 { | |
6427 int i; | |
6428 int padding = second_indent - leader_len; | |
6429 | |
6430 /* We started at the first_line of a numbered list | |
6431 * that has a comment. the open_line() function has | |
6432 * inserted the proper comment leader and positioned | |
6433 * the cursor at the end of the split line. Now we | |
6434 * add the additional whitespace needed after the | |
6435 * comment leader for the numbered list. */ | |
6436 for (i = 0; i < padding; i++) | |
6437 ins_str((char_u *)" "); | |
3650 | 6438 changed_bytes(curwin->w_cursor.lnum, leader_len); |
3632 | 6439 } |
6440 else | |
6441 { | |
6442 #endif | |
3584 | 6443 (void)set_indent(second_indent, SIN_CHANGED); |
3632 | 6444 #ifdef FEAT_COMMENTS |
6445 } | |
6446 #endif | |
3584 | 6447 } |
667 | 6448 } |
6449 first_line = FALSE; | |
6450 } | |
6451 | |
6452 #ifdef FEAT_VREPLACE | |
6453 if (State & VREPLACE_FLAG) | |
6454 { | |
6455 /* | |
6456 * In VREPLACE mode we have backspaced over the text to be | |
6457 * moved, now we re-insert it into the new line. | |
6458 */ | |
6459 ins_bytes(saved_text); | |
6460 vim_free(saved_text); | |
6461 } | |
6462 else | |
6463 #endif | |
6464 { | |
6465 /* | |
6466 * Check if cursor is not past the NUL off the line, cindent | |
6467 * may have added or removed indent. | |
6468 */ | |
6469 curwin->w_cursor.col += startcol; | |
6470 len = (colnr_T)STRLEN(ml_get_curline()); | |
6471 if (curwin->w_cursor.col > len) | |
6472 curwin->w_cursor.col = len; | |
6473 } | |
6474 | |
6475 haveto_redraw = TRUE; | |
6476 #ifdef FEAT_CINDENT | |
6477 can_cindent = TRUE; | |
6478 #endif | |
6479 /* moved the cursor, don't autoindent or cindent now */ | |
6480 did_ai = FALSE; | |
6481 #ifdef FEAT_SMARTINDENT | |
6482 did_si = FALSE; | |
6483 can_si = FALSE; | |
6484 can_si_back = FALSE; | |
6485 #endif | |
6486 line_breakcheck(); | |
6487 } | |
6488 | |
6489 if (save_char != NUL) /* put back space after cursor */ | |
6490 pchar_cursor(save_char); | |
6491 | |
6199 | 6492 #ifdef FEAT_LINEBREAK |
6493 curwin->w_p_lbr = has_lbr; | |
6494 #endif | |
667 | 6495 if (!format_only && haveto_redraw) |
6496 { | |
6497 update_topline(); | |
6498 redraw_curbuf_later(VALID); | |
6499 } | |
6500 } | |
6501 | |
6502 /* | |
7 | 6503 * Called after inserting or deleting text: When 'formatoptions' includes the |
6504 * 'a' flag format from the current line until the end of the paragraph. | |
6505 * Keep the cursor at the same position relative to the text. | |
6506 * The caller must have saved the cursor line for undo, following ones will be | |
6507 * saved here. | |
6508 */ | |
6509 void | |
6510 auto_format(trailblank, prev_line) | |
6511 int trailblank; /* when TRUE also format with trailing blank */ | |
6512 int prev_line; /* may start in previous line */ | |
6513 { | |
6514 pos_T pos; | |
6515 colnr_T len; | |
6516 char_u *old; | |
6517 char_u *new, *pnew; | |
6518 int wasatend; | |
301 | 6519 int cc; |
7 | 6520 |
6521 if (!has_format_option(FO_AUTO)) | |
6522 return; | |
6523 | |
6524 pos = curwin->w_cursor; | |
6525 old = ml_get_curline(); | |
6526 | |
6527 /* may remove added space */ | |
6528 check_auto_format(FALSE); | |
6529 | |
6530 /* Don't format in Insert mode when the cursor is on a trailing blank, the | |
6531 * user might insert normal text next. Also skip formatting when "1" is | |
6532 * in 'formatoptions' and there is a single character before the cursor. | |
6533 * Otherwise the line would be broken and when typing another non-white | |
6534 * next they are not joined back together. */ | |
1872 | 6535 wasatend = (pos.col == (colnr_T)STRLEN(old)); |
7 | 6536 if (*old != NUL && !trailblank && wasatend) |
6537 { | |
6538 dec_cursor(); | |
301 | 6539 cc = gchar_cursor(); |
6540 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0 | |
6541 && has_format_option(FO_ONE_LETTER)) | |
7 | 6542 dec_cursor(); |
301 | 6543 cc = gchar_cursor(); |
6544 if (WHITECHAR(cc)) | |
7 | 6545 { |
6546 curwin->w_cursor = pos; | |
6547 return; | |
6548 } | |
6549 curwin->w_cursor = pos; | |
6550 } | |
6551 | |
6552 #ifdef FEAT_COMMENTS | |
6553 /* With the 'c' flag in 'formatoptions' and 't' missing: only format | |
6554 * comments. */ | |
6555 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP) | |
3562 | 6556 && get_leader_len(old, NULL, FALSE, TRUE) == 0) |
7 | 6557 return; |
6558 #endif | |
6559 | |
6560 /* | |
6561 * May start formatting in a previous line, so that after "x" a word is | |
6562 * moved to the previous line if it fits there now. Only when this is not | |
6563 * the start of a paragraph. | |
6564 */ | |
6565 if (prev_line && !paragraph_start(curwin->w_cursor.lnum)) | |
6566 { | |
6567 --curwin->w_cursor.lnum; | |
6568 if (u_save_cursor() == FAIL) | |
6569 return; | |
6570 } | |
6571 | |
6572 /* | |
6573 * Do the formatting and restore the cursor position. "saved_cursor" will | |
6574 * be adjusted for the text formatting. | |
6575 */ | |
6576 saved_cursor = pos; | |
1563 | 6577 format_lines((linenr_T)-1, FALSE); |
7 | 6578 curwin->w_cursor = saved_cursor; |
6579 saved_cursor.lnum = 0; | |
6580 | |
6581 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count) | |
6582 { | |
6583 /* "cannot happen" */ | |
6584 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; | |
6585 coladvance((colnr_T)MAXCOL); | |
6586 } | |
6587 else | |
6588 check_cursor_col(); | |
6589 | |
6590 /* Insert mode: If the cursor is now after the end of the line while it | |
6591 * previously wasn't, the line was broken. Because of the rule above we | |
6592 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph | |
6593 * formatted. */ | |
6594 if (!wasatend && has_format_option(FO_WHITE_PAR)) | |
6595 { | |
6596 new = ml_get_curline(); | |
835 | 6597 len = (colnr_T)STRLEN(new); |
7 | 6598 if (curwin->w_cursor.col == len) |
6599 { | |
6600 pnew = vim_strnsave(new, len + 2); | |
6601 pnew[len] = ' '; | |
6602 pnew[len + 1] = NUL; | |
6603 ml_replace(curwin->w_cursor.lnum, pnew, FALSE); | |
6604 /* remove the space later */ | |
6605 did_add_space = TRUE; | |
6606 } | |
6607 else | |
6608 /* may remove added space */ | |
6609 check_auto_format(FALSE); | |
6610 } | |
6611 | |
6612 check_cursor(); | |
6613 } | |
6614 | |
6615 /* | |
6616 * When an extra space was added to continue a paragraph for auto-formatting, | |
6617 * delete it now. The space must be under the cursor, just after the insert | |
6618 * position. | |
6619 */ | |
6620 static void | |
6621 check_auto_format(end_insert) | |
6622 int end_insert; /* TRUE when ending Insert mode */ | |
6623 { | |
6624 int c = ' '; | |
301 | 6625 int cc; |
7 | 6626 |
6627 if (did_add_space) | |
6628 { | |
301 | 6629 cc = gchar_cursor(); |
6630 if (!WHITECHAR(cc)) | |
7 | 6631 /* Somehow the space was removed already. */ |
6632 did_add_space = FALSE; | |
6633 else | |
6634 { | |
6635 if (!end_insert) | |
6636 { | |
6637 inc_cursor(); | |
6638 c = gchar_cursor(); | |
6639 dec_cursor(); | |
6640 } | |
6641 if (c != NUL) | |
6642 { | |
6643 /* The space is no longer at the end of the line, delete it. */ | |
6644 del_char(FALSE); | |
6645 did_add_space = FALSE; | |
6646 } | |
6647 } | |
6648 } | |
6649 } | |
6650 | |
6651 /* | |
6652 * Find out textwidth to be used for formatting: | |
6653 * if 'textwidth' option is set, use it | |
6654 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin' | |
6655 * if invalid value, use 0. | |
6656 * Set default to window width (maximum 79) for "gq" operator. | |
6657 */ | |
6658 int | |
6659 comp_textwidth(ff) | |
867 | 6660 int ff; /* force formatting (for "gq" command) */ |
7 | 6661 { |
6662 int textwidth; | |
6663 | |
6664 textwidth = curbuf->b_p_tw; | |
6665 if (textwidth == 0 && curbuf->b_p_wm) | |
6666 { | |
6667 /* The width is the window width minus 'wrapmargin' minus all the | |
6668 * things that add to the margin. */ | |
6669 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm; | |
6670 #ifdef FEAT_CMDWIN | |
6671 if (cmdwin_type != 0) | |
6672 textwidth -= 1; | |
6673 #endif | |
6674 #ifdef FEAT_FOLDING | |
6675 textwidth -= curwin->w_p_fdc; | |
6676 #endif | |
6677 #ifdef FEAT_SIGNS | |
6678 if (curwin->w_buffer->b_signlist != NULL | |
6679 # ifdef FEAT_NETBEANS_INTG | |
2210 | 6680 || netbeans_active() |
7 | 6681 # endif |
6682 ) | |
6683 textwidth -= 1; | |
6684 #endif | |
2178
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2107
diff
changeset
|
6685 if (curwin->w_p_nu || curwin->w_p_rnu) |
7 | 6686 textwidth -= 8; |
6687 } | |
6688 if (textwidth < 0) | |
6689 textwidth = 0; | |
6690 if (ff && textwidth == 0) | |
6691 { | |
6692 textwidth = W_WIDTH(curwin) - 1; | |
6693 if (textwidth > 79) | |
6694 textwidth = 79; | |
6695 } | |
6696 return textwidth; | |
6697 } | |
6698 | |
6699 /* | |
6700 * Put a character in the redo buffer, for when just after a CTRL-V. | |
6701 */ | |
6702 static void | |
6703 redo_literal(c) | |
6704 int c; | |
6705 { | |
6706 char_u buf[10]; | |
6707 | |
6708 /* Only digits need special treatment. Translate them into a string of | |
6709 * three digits. */ | |
6710 if (VIM_ISDIGIT(c)) | |
6711 { | |
1872 | 6712 vim_snprintf((char *)buf, sizeof(buf), "%03d", c); |
7 | 6713 AppendToRedobuff(buf); |
6714 } | |
6715 else | |
6716 AppendCharToRedobuff(c); | |
6717 } | |
6718 | |
6719 /* | |
6720 * start_arrow() is called when an arrow key is used in insert mode. | |
484 | 6721 * For undo/redo it resembles hitting the <ESC> key. |
7 | 6722 */ |
6723 static void | |
6724 start_arrow(end_insert_pos) | |
840 | 6725 pos_T *end_insert_pos; /* can be NULL */ |
7 | 6726 { |
6727 if (!arrow_used) /* something has been inserted */ | |
6728 { | |
6729 AppendToRedobuff(ESC_STR); | |
5434 | 6730 stop_insert(end_insert_pos, FALSE, FALSE); |
7 | 6731 arrow_used = TRUE; /* this means we stopped the current insert */ |
6732 } | |
744 | 6733 #ifdef FEAT_SPELL |
221 | 6734 check_spell_redraw(); |
6735 #endif | |
7 | 6736 } |
6737 | |
744 | 6738 #ifdef FEAT_SPELL |
221 | 6739 /* |
6740 * If we skipped highlighting word at cursor, do it now. | |
6741 * It may be skipped again, thus reset spell_redraw_lnum first. | |
6742 */ | |
6743 static void | |
6744 check_spell_redraw() | |
6745 { | |
6746 if (spell_redraw_lnum != 0) | |
6747 { | |
6748 linenr_T lnum = spell_redraw_lnum; | |
6749 | |
6750 spell_redraw_lnum = 0; | |
6751 redrawWinline(lnum, FALSE); | |
6752 } | |
6753 } | |
484 | 6754 |
6755 /* | |
6756 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly | |
6757 * spelled word, if there is one. | |
6758 */ | |
6759 static void | |
6760 spell_back_to_badword() | |
6761 { | |
6762 pos_T tpos = curwin->w_cursor; | |
6763 | |
499 | 6764 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL); |
484 | 6765 if (curwin->w_cursor.col != tpos.col) |
6766 start_arrow(&tpos); | |
6767 } | |
221 | 6768 #endif |
6769 | |
7 | 6770 /* |
6771 * stop_arrow() is called before a change is made in insert mode. | |
6772 * If an arrow key has been used, start a new insertion. | |
6773 * Returns FAIL if undo is impossible, shouldn't insert then. | |
6774 */ | |
6775 int | |
6776 stop_arrow() | |
6777 { | |
6778 if (arrow_used) | |
6779 { | |
6138 | 6780 Insstart = curwin->w_cursor; /* new insertion starts here */ |
6781 if (Insstart.col > Insstart_orig.col && !ins_need_undo) | |
6782 /* Don't update the original insert position when moved to the | |
6783 * right, except when nothing was inserted yet. */ | |
6784 update_Insstart_orig = FALSE; | |
6785 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline()); | |
6786 | |
7 | 6787 if (u_save_cursor() == OK) |
6788 { | |
6789 arrow_used = FALSE; | |
6790 ins_need_undo = FALSE; | |
6791 } | |
6138 | 6792 |
7 | 6793 ai_col = 0; |
6794 #ifdef FEAT_VREPLACE | |
6795 if (State & VREPLACE_FLAG) | |
6796 { | |
6797 orig_line_count = curbuf->b_ml.ml_line_count; | |
6798 vr_lines_changed = 1; | |
6799 } | |
6800 #endif | |
6801 ResetRedobuff(); | |
6802 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */ | |
613 | 6803 new_insert_skip = 2; |
7 | 6804 } |
6805 else if (ins_need_undo) | |
6806 { | |
6807 if (u_save_cursor() == OK) | |
6808 ins_need_undo = FALSE; | |
6809 } | |
6810 | |
6811 #ifdef FEAT_FOLDING | |
6812 /* Always open fold at the cursor line when inserting something. */ | |
6813 foldOpenCursor(); | |
6814 #endif | |
6815 | |
6816 return (arrow_used || ins_need_undo ? FAIL : OK); | |
6817 } | |
6818 | |
6819 /* | |
840 | 6820 * Do a few things to stop inserting. |
6821 * "end_insert_pos" is where insert ended. It is NULL when we already jumped | |
6822 * to another window/buffer. | |
7 | 6823 */ |
6824 static void | |
5434 | 6825 stop_insert(end_insert_pos, esc, nomove) |
840 | 6826 pos_T *end_insert_pos; |
603 | 6827 int esc; /* called by ins_esc() */ |
5434 | 6828 int nomove; /* <c-\><c-o>, don't move cursor */ |
7 | 6829 { |
603 | 6830 int cc; |
6831 char_u *ptr; | |
7 | 6832 |
6833 stop_redo_ins(); | |
6834 replace_flush(); /* abandon replace stack */ | |
6835 | |
6836 /* | |
603 | 6837 * Save the inserted text for later redo with ^@ and CTRL-A. |
6838 * Don't do it when "restart_edit" was set and nothing was inserted, | |
6839 * otherwise CTRL-O w and then <Left> will clear "last_insert". | |
7 | 6840 */ |
603 | 6841 ptr = get_inserted(); |
615 | 6842 if (did_restart_edit == 0 || (ptr != NULL |
6843 && (int)STRLEN(ptr) > new_insert_skip)) | |
603 | 6844 { |
6845 vim_free(last_insert); | |
6846 last_insert = ptr; | |
6847 last_insert_skip = new_insert_skip; | |
6848 } | |
6849 else | |
6850 vim_free(ptr); | |
7 | 6851 |
840 | 6852 if (!arrow_used && end_insert_pos != NULL) |
7 | 6853 { |
6854 /* Auto-format now. It may seem strange to do this when stopping an | |
6855 * insertion (or moving the cursor), but it's required when appending | |
6856 * a line and having it end in a space. But only do it when something | |
6857 * was actually inserted, otherwise undo won't work. */ | |
10 | 6858 if (!ins_need_undo && has_format_option(FO_AUTO)) |
7 | 6859 { |
10 | 6860 pos_T tpos = curwin->w_cursor; |
6861 | |
7 | 6862 /* When the cursor is at the end of the line after a space the |
6863 * formatting will move it to the following word. Avoid that by | |
6864 * moving the cursor onto the space. */ | |
6865 cc = 'x'; | |
6866 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL) | |
6867 { | |
6868 dec_cursor(); | |
6869 cc = gchar_cursor(); | |
6870 if (!vim_iswhite(cc)) | |
10 | 6871 curwin->w_cursor = tpos; |
7 | 6872 } |
6873 | |
6874 auto_format(TRUE, FALSE); | |
6875 | |
10 | 6876 if (vim_iswhite(cc)) |
6877 { | |
6878 if (gchar_cursor() != NUL) | |
6879 inc_cursor(); | |
6880 #ifdef FEAT_VIRTUALEDIT | |
6881 /* If the cursor is still at the same character, also keep | |
6882 * the "coladd". */ | |
6883 if (gchar_cursor() == NUL | |
6884 && curwin->w_cursor.lnum == tpos.lnum | |
6885 && curwin->w_cursor.col == tpos.col) | |
6886 curwin->w_cursor.coladd = tpos.coladd; | |
6887 #endif | |
6888 } | |
7 | 6889 } |
6890 | |
6891 /* If a space was inserted for auto-formatting, remove it now. */ | |
6892 check_auto_format(TRUE); | |
6893 | |
6894 /* If we just did an auto-indent, remove the white space from the end | |
10 | 6895 * of the line, and put the cursor back. |
1892 | 6896 * Do this when ESC was used or moving the cursor up/down. |
6897 * Check for the old position still being valid, just in case the text | |
6898 * got changed unexpectedly. */ | |
5434 | 6899 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL |
1892 | 6900 && curwin->w_cursor.lnum != end_insert_pos->lnum)) |
6901 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count) | |
7 | 6902 { |
10 | 6903 pos_T tpos = curwin->w_cursor; |
6904 | |
6905 curwin->w_cursor = *end_insert_pos; | |
1892 | 6906 check_cursor_col(); /* make sure it is not past the line */ |
786 | 6907 for (;;) |
6908 { | |
6909 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0) | |
6910 --curwin->w_cursor.col; | |
6911 cc = gchar_cursor(); | |
6912 if (!vim_iswhite(cc)) | |
6913 break; | |
1892 | 6914 if (del_char(TRUE) == FAIL) |
6915 break; /* should not happen */ | |
786 | 6916 } |
10 | 6917 if (curwin->w_cursor.lnum != tpos.lnum) |
6918 curwin->w_cursor = tpos; | |
6318 | 6919 else |
6920 { | |
6363 | 6921 /* reset tpos, could have been invalidated in the loop above */ |
6922 tpos = curwin->w_cursor; | |
6318 | 6923 tpos.col++; |
6924 if (cc != NUL && gchar_pos(&tpos) == NUL) | |
6925 ++curwin->w_cursor.col; /* put cursor back on the NUL */ | |
6926 } | |
7 | 6927 |
6928 /* <C-S-Right> may have started Visual mode, adjust the position for | |
6929 * deleted characters. */ | |
6930 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum) | |
6931 { | |
1872 | 6932 int len = (int)STRLEN(ml_get_curline()); |
6933 | |
6934 if (VIsual.col > len) | |
7 | 6935 { |
1872 | 6936 VIsual.col = len; |
5735 | 6937 #ifdef FEAT_VIRTUALEDIT |
7 | 6938 VIsual.coladd = 0; |
5735 | 6939 #endif |
7 | 6940 } |
6941 } | |
6942 } | |
6943 } | |
6944 did_ai = FALSE; | |
6945 #ifdef FEAT_SMARTINDENT | |
6946 did_si = FALSE; | |
6947 can_si = FALSE; | |
6948 can_si_back = FALSE; | |
6949 #endif | |
6950 | |
840 | 6951 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are |
6952 * now in a different buffer. */ | |
6953 if (end_insert_pos != NULL) | |
6954 { | |
6955 curbuf->b_op_start = Insstart; | |
5680 | 6956 curbuf->b_op_start_orig = Insstart_orig; |
840 | 6957 curbuf->b_op_end = *end_insert_pos; |
6958 } | |
7 | 6959 } |
6960 | |
6961 /* | |
6962 * Set the last inserted text to a single character. | |
6963 * Used for the replace command. | |
6964 */ | |
6965 void | |
6966 set_last_insert(c) | |
6967 int c; | |
6968 { | |
6969 char_u *s; | |
6970 | |
6971 vim_free(last_insert); | |
6972 last_insert = alloc(MB_MAXBYTES * 3 + 5); | |
6973 if (last_insert != NULL) | |
6974 { | |
6975 s = last_insert; | |
6976 /* Use the CTRL-V only when entering a special char */ | |
6977 if (c < ' ' || c == DEL) | |
6978 *s++ = Ctrl_V; | |
6979 s = add_char2buf(c, s); | |
6980 *s++ = ESC; | |
6981 *s++ = NUL; | |
6982 last_insert_skip = 0; | |
6983 } | |
6984 } | |
6985 | |
359 | 6986 #if defined(EXITFREE) || defined(PROTO) |
6987 void | |
6988 free_last_insert() | |
6989 { | |
6990 vim_free(last_insert); | |
6991 last_insert = NULL; | |
1446 | 6992 # ifdef FEAT_INS_EXPAND |
694 | 6993 vim_free(compl_orig_text); |
6994 compl_orig_text = NULL; | |
1446 | 6995 # endif |
359 | 6996 } |
6997 #endif | |
6998 | |
7 | 6999 /* |
7000 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL | |
7001 * and CSI. Handle multi-byte characters. | |
7002 * Returns a pointer to after the added bytes. | |
7003 */ | |
7004 char_u * | |
7005 add_char2buf(c, s) | |
7006 int c; | |
7007 char_u *s; | |
7008 { | |
7009 #ifdef FEAT_MBYTE | |
3549 | 7010 char_u temp[MB_MAXBYTES + 1]; |
7 | 7011 int i; |
7012 int len; | |
7013 | |
7014 len = (*mb_char2bytes)(c, temp); | |
7015 for (i = 0; i < len; ++i) | |
7016 { | |
7017 c = temp[i]; | |
7018 #endif | |
7019 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */ | |
7020 if (c == K_SPECIAL) | |
7021 { | |
7022 *s++ = K_SPECIAL; | |
7023 *s++ = KS_SPECIAL; | |
7024 *s++ = KE_FILLER; | |
7025 } | |
7026 #ifdef FEAT_GUI | |
7027 else if (c == CSI) | |
7028 { | |
7029 *s++ = CSI; | |
7030 *s++ = KS_EXTRA; | |
7031 *s++ = (int)KE_CSI; | |
7032 } | |
7033 #endif | |
7034 else | |
7035 *s++ = c; | |
7036 #ifdef FEAT_MBYTE | |
7037 } | |
7038 #endif | |
7039 return s; | |
7040 } | |
7041 | |
7042 /* | |
7043 * move cursor to start of line | |
7044 * if flags & BL_WHITE move to first non-white | |
7045 * if flags & BL_SOL move to first non-white if startofline is set, | |
7046 * otherwise keep "curswant" column | |
7047 * if flags & BL_FIX don't leave the cursor on a NUL. | |
7048 */ | |
7049 void | |
7050 beginline(flags) | |
7051 int flags; | |
7052 { | |
7053 if ((flags & BL_SOL) && !p_sol) | |
7054 coladvance(curwin->w_curswant); | |
7055 else | |
7056 { | |
7057 curwin->w_cursor.col = 0; | |
7058 #ifdef FEAT_VIRTUALEDIT | |
7059 curwin->w_cursor.coladd = 0; | |
7060 #endif | |
7061 | |
7062 if (flags & (BL_WHITE | BL_SOL)) | |
7063 { | |
7064 char_u *ptr; | |
7065 | |
7066 for (ptr = ml_get_curline(); vim_iswhite(*ptr) | |
7067 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr) | |
7068 ++curwin->w_cursor.col; | |
7069 } | |
7070 curwin->w_set_curswant = TRUE; | |
7071 } | |
7072 } | |
7073 | |
7074 /* | |
7075 * oneright oneleft cursor_down cursor_up | |
7076 * | |
7077 * Move one char {right,left,down,up}. | |
773 | 7078 * Doesn't move onto the NUL past the end of the line, unless it is allowed. |
7 | 7079 * Return OK when successful, FAIL when we hit a line of file boundary. |
7080 */ | |
7081 | |
7082 int | |
7083 oneright() | |
7084 { | |
7085 char_u *ptr; | |
7086 int l; | |
7087 | |
7088 #ifdef FEAT_VIRTUALEDIT | |
7089 if (virtual_active()) | |
7090 { | |
7091 pos_T prevpos = curwin->w_cursor; | |
7092 | |
7093 /* Adjust for multi-wide char (excluding TAB) */ | |
7094 ptr = ml_get_cursor(); | |
7095 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc( | |
773 | 7096 # ifdef FEAT_MBYTE |
7 | 7097 (*mb_ptr2char)(ptr) |
773 | 7098 # else |
7 | 7099 *ptr |
773 | 7100 # endif |
7 | 7101 )) |
7102 ? ptr2cells(ptr) : 1)); | |
7103 curwin->w_set_curswant = TRUE; | |
7104 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */ | |
7105 return (prevpos.col != curwin->w_cursor.col | |
7106 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL; | |
7107 } | |
7108 #endif | |
7109 | |
7110 ptr = ml_get_cursor(); | |
773 | 7111 if (*ptr == NUL) |
7112 return FAIL; /* already at the very end */ | |
7113 | |
7 | 7114 #ifdef FEAT_MBYTE |
773 | 7115 if (has_mbyte) |
7116 l = (*mb_ptr2len)(ptr); | |
7 | 7117 else |
7118 #endif | |
773 | 7119 l = 1; |
7120 | |
7121 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit' | |
7122 * contains "onemore". */ | |
7123 if (ptr[l] == NUL | |
7124 #ifdef FEAT_VIRTUALEDIT | |
7125 && (ve_flags & VE_ONEMORE) == 0 | |
7126 #endif | |
7127 ) | |
7128 return FAIL; | |
7129 curwin->w_cursor.col += l; | |
7 | 7130 |
7131 curwin->w_set_curswant = TRUE; | |
7132 return OK; | |
7133 } | |
7134 | |
7135 int | |
7136 oneleft() | |
7137 { | |
7138 #ifdef FEAT_VIRTUALEDIT | |
7139 if (virtual_active()) | |
7140 { | |
7141 int width; | |
7142 int v = getviscol(); | |
7143 | |
7144 if (v == 0) | |
7145 return FAIL; | |
7146 | |
7147 # ifdef FEAT_LINEBREAK | |
7148 /* We might get stuck on 'showbreak', skip over it. */ | |
7149 width = 1; | |
7150 for (;;) | |
7151 { | |
7152 coladvance(v - width); | |
5995 | 7153 /* getviscol() is slow, skip it when 'showbreak' is empty, |
7154 * 'breakindent' is not set and there are no multi-byte | |
7155 * characters */ | |
7156 if ((*p_sbr == NUL && !curwin->w_p_bri | |
7 | 7157 # ifdef FEAT_MBYTE |
7158 && !has_mbyte | |
7159 # endif | |
7160 ) || getviscol() < v) | |
7161 break; | |
7162 ++width; | |
7163 } | |
7164 # else | |
7165 coladvance(v - 1); | |
7166 # endif | |
7167 | |
7168 if (curwin->w_cursor.coladd == 1) | |
7169 { | |
7170 char_u *ptr; | |
7171 | |
7172 /* Adjust for multi-wide char (not a TAB) */ | |
7173 ptr = ml_get_cursor(); | |
7174 if (*ptr != TAB && vim_isprintc( | |
7175 # ifdef FEAT_MBYTE | |
7176 (*mb_ptr2char)(ptr) | |
7177 # else | |
7178 *ptr | |
7179 # endif | |
7180 ) && ptr2cells(ptr) > 1) | |
7181 curwin->w_cursor.coladd = 0; | |
7182 } | |
7183 | |
7184 curwin->w_set_curswant = TRUE; | |
7185 return OK; | |
7186 } | |
7187 #endif | |
7188 | |
7189 if (curwin->w_cursor.col == 0) | |
7190 return FAIL; | |
7191 | |
7192 curwin->w_set_curswant = TRUE; | |
7193 --curwin->w_cursor.col; | |
7194 | |
7195 #ifdef FEAT_MBYTE | |
7196 /* if the character on the left of the current cursor is a multi-byte | |
7197 * character, move to its first byte */ | |
7198 if (has_mbyte) | |
7199 mb_adjust_cursor(); | |
7200 #endif | |
7201 return OK; | |
7202 } | |
7203 | |
7204 int | |
7205 cursor_up(n, upd_topline) | |
7206 long n; | |
7207 int upd_topline; /* When TRUE: update topline */ | |
7208 { | |
7209 linenr_T lnum; | |
7210 | |
7211 if (n > 0) | |
7212 { | |
7213 lnum = curwin->w_cursor.lnum; | |
161 | 7214 /* This fails if the cursor is already in the first line or the count |
7215 * is larger than the line number and '-' is in 'cpoptions' */ | |
7216 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL)) | |
7 | 7217 return FAIL; |
7218 if (n >= lnum) | |
7219 lnum = 1; | |
7220 else | |
7221 #ifdef FEAT_FOLDING | |
7222 if (hasAnyFolding(curwin)) | |
7223 { | |
7224 /* | |
7225 * Count each sequence of folded lines as one logical line. | |
7226 */ | |
4352 | 7227 /* go to the start of the current fold */ |
7 | 7228 (void)hasFolding(lnum, &lnum, NULL); |
7229 | |
7230 while (n--) | |
7231 { | |
7232 /* move up one line */ | |
7233 --lnum; | |
7234 if (lnum <= 1) | |
7235 break; | |
7236 /* If we entered a fold, move to the beginning, unless in | |
7237 * Insert mode or when 'foldopen' contains "all": it will open | |
7238 * in a moment. */ | |
7239 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL))) | |
7240 (void)hasFolding(lnum, &lnum, NULL); | |
7241 } | |
7242 if (lnum < 1) | |
7243 lnum = 1; | |
7244 } | |
7245 else | |
7246 #endif | |
7247 lnum -= n; | |
7248 curwin->w_cursor.lnum = lnum; | |
7249 } | |
7250 | |
7251 /* try to advance to the column we want to be at */ | |
7252 coladvance(curwin->w_curswant); | |
7253 | |
7254 if (upd_topline) | |
7255 update_topline(); /* make sure curwin->w_topline is valid */ | |
7256 | |
7257 return OK; | |
7258 } | |
7259 | |
7260 /* | |
7261 * Cursor down a number of logical lines. | |
7262 */ | |
7263 int | |
7264 cursor_down(n, upd_topline) | |
7265 long n; | |
7266 int upd_topline; /* When TRUE: update topline */ | |
7267 { | |
7268 linenr_T lnum; | |
7269 | |
7270 if (n > 0) | |
7271 { | |
7272 lnum = curwin->w_cursor.lnum; | |
7273 #ifdef FEAT_FOLDING | |
7274 /* Move to last line of fold, will fail if it's the end-of-file. */ | |
7275 (void)hasFolding(lnum, NULL, &lnum); | |
7276 #endif | |
161 | 7277 /* This fails if the cursor is already in the last line or would move |
4352 | 7278 * beyond the last line and '-' is in 'cpoptions' */ |
161 | 7279 if (lnum >= curbuf->b_ml.ml_line_count |
7280 || (lnum + n > curbuf->b_ml.ml_line_count | |
7281 && vim_strchr(p_cpo, CPO_MINUS) != NULL)) | |
7 | 7282 return FAIL; |
7283 if (lnum + n >= curbuf->b_ml.ml_line_count) | |
7284 lnum = curbuf->b_ml.ml_line_count; | |
7285 else | |
7286 #ifdef FEAT_FOLDING | |
7287 if (hasAnyFolding(curwin)) | |
7288 { | |
7289 linenr_T last; | |
7290 | |
7291 /* count each sequence of folded lines as one logical line */ | |
7292 while (n--) | |
7293 { | |
7294 if (hasFolding(lnum, NULL, &last)) | |
7295 lnum = last + 1; | |
7296 else | |
7297 ++lnum; | |
7298 if (lnum >= curbuf->b_ml.ml_line_count) | |
7299 break; | |
7300 } | |
7301 if (lnum > curbuf->b_ml.ml_line_count) | |
7302 lnum = curbuf->b_ml.ml_line_count; | |
7303 } | |
7304 else | |
7305 #endif | |
7306 lnum += n; | |
7307 curwin->w_cursor.lnum = lnum; | |
7308 } | |
7309 | |
7310 /* try to advance to the column we want to be at */ | |
7311 coladvance(curwin->w_curswant); | |
7312 | |
7313 if (upd_topline) | |
7314 update_topline(); /* make sure curwin->w_topline is valid */ | |
7315 | |
7316 return OK; | |
7317 } | |
7318 | |
7319 /* | |
7320 * Stuff the last inserted text in the read buffer. | |
7321 * Last_insert actually is a copy of the redo buffer, so we | |
7322 * first have to remove the command. | |
7323 */ | |
7324 int | |
7325 stuff_inserted(c, count, no_esc) | |
7326 int c; /* Command character to be inserted */ | |
7327 long count; /* Repeat this many times */ | |
7328 int no_esc; /* Don't add an ESC at the end */ | |
7329 { | |
7330 char_u *esc_ptr; | |
7331 char_u *ptr; | |
7332 char_u *last_ptr; | |
7333 char_u last = NUL; | |
7334 | |
7335 ptr = get_last_insert(); | |
7336 if (ptr == NULL) | |
7337 { | |
7338 EMSG(_(e_noinstext)); | |
7339 return FAIL; | |
7340 } | |
7341 | |
7342 /* may want to stuff the command character, to start Insert mode */ | |
7343 if (c != NUL) | |
7344 stuffcharReadbuff(c); | |
7345 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL) | |
7346 *esc_ptr = NUL; /* remove the ESC */ | |
7347 | |
7348 /* when the last char is either "0" or "^" it will be quoted if no ESC | |
7349 * comes after it OR if it will inserted more than once and "ptr" | |
7350 * starts with ^D. -- Acevedo | |
7351 */ | |
7352 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1; | |
7353 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^') | |
7354 && (no_esc || (*ptr == Ctrl_D && count > 1))) | |
7355 { | |
7356 last = *last_ptr; | |
7357 *last_ptr = NUL; | |
7358 } | |
7359 | |
7360 do | |
7361 { | |
7362 stuffReadbuff(ptr); | |
7363 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */ | |
7364 if (last) | |
7365 stuffReadbuff((char_u *)(last == '0' | |
7366 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0") | |
7367 : IF_EB("\026^", CTRL_V_STR "^"))); | |
7368 } | |
7369 while (--count > 0); | |
7370 | |
7371 if (last) | |
7372 *last_ptr = last; | |
7373 | |
7374 if (esc_ptr != NULL) | |
7375 *esc_ptr = ESC; /* put the ESC back */ | |
7376 | |
7377 /* may want to stuff a trailing ESC, to get out of Insert mode */ | |
7378 if (!no_esc) | |
7379 stuffcharReadbuff(ESC); | |
7380 | |
7381 return OK; | |
7382 } | |
7383 | |
7384 char_u * | |
7385 get_last_insert() | |
7386 { | |
7387 if (last_insert == NULL) | |
7388 return NULL; | |
7389 return last_insert + last_insert_skip; | |
7390 } | |
7391 | |
7392 /* | |
7393 * Get last inserted string, and remove trailing <Esc>. | |
7394 * Returns pointer to allocated memory (must be freed) or NULL. | |
7395 */ | |
7396 char_u * | |
7397 get_last_insert_save() | |
7398 { | |
7399 char_u *s; | |
7400 int len; | |
7401 | |
7402 if (last_insert == NULL) | |
7403 return NULL; | |
7404 s = vim_strsave(last_insert + last_insert_skip); | |
7405 if (s != NULL) | |
7406 { | |
7407 len = (int)STRLEN(s); | |
7408 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */ | |
7409 s[len - 1] = NUL; | |
7410 } | |
7411 return s; | |
7412 } | |
7413 | |
7414 /* | |
7415 * Check the word in front of the cursor for an abbreviation. | |
7416 * Called when the non-id character "c" has been entered. | |
7417 * When an abbreviation is recognized it is removed from the text and | |
7418 * the replacement string is inserted in typebuf.tb_buf[], followed by "c". | |
7419 */ | |
7420 static int | |
7421 echeck_abbr(c) | |
7422 int c; | |
7423 { | |
7424 /* Don't check for abbreviation in paste mode, when disabled and just | |
7425 * after moving around with cursor keys. */ | |
7426 if (p_paste || no_abbr || arrow_used) | |
7427 return FALSE; | |
7428 | |
7429 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col, | |
7430 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0); | |
7431 } | |
7432 | |
7433 /* | |
7434 * replace-stack functions | |
7435 * | |
7436 * When replacing characters, the replaced characters are remembered for each | |
7437 * new character. This is used to re-insert the old text when backspacing. | |
7438 * | |
7439 * There is a NUL headed list of characters for each character that is | |
7440 * currently in the file after the insertion point. When BS is used, one NUL | |
7441 * headed list is put back for the deleted character. | |
7442 * | |
7443 * For a newline, there are two NUL headed lists. One contains the characters | |
7444 * that the NL replaced. The extra one stores the characters after the cursor | |
7445 * that were deleted (always white space). | |
7446 * | |
7447 * Replace_offset is normally 0, in which case replace_push will add a new | |
7448 * character at the end of the stack. If replace_offset is not 0, that many | |
7449 * characters will be left on the stack above the newly inserted character. | |
7450 */ | |
7451 | |
298 | 7452 static char_u *replace_stack = NULL; |
7453 static long replace_stack_nr = 0; /* next entry in replace stack */ | |
7454 static long replace_stack_len = 0; /* max. number of entries */ | |
7 | 7455 |
7456 void | |
7457 replace_push(c) | |
7458 int c; /* character that is replaced (NUL is none) */ | |
7459 { | |
7460 char_u *p; | |
7461 | |
7462 if (replace_stack_nr < replace_offset) /* nothing to do */ | |
7463 return; | |
7464 if (replace_stack_len <= replace_stack_nr) | |
7465 { | |
7466 replace_stack_len += 50; | |
7467 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE); | |
7468 if (p == NULL) /* out of memory */ | |
7469 { | |
7470 replace_stack_len -= 50; | |
7471 return; | |
7472 } | |
7473 if (replace_stack != NULL) | |
7474 { | |
7475 mch_memmove(p, replace_stack, | |
7476 (size_t)(replace_stack_nr * sizeof(char_u))); | |
7477 vim_free(replace_stack); | |
7478 } | |
7479 replace_stack = p; | |
7480 } | |
7481 p = replace_stack + replace_stack_nr - replace_offset; | |
7482 if (replace_offset) | |
7483 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u))); | |
7484 *p = c; | |
7485 ++replace_stack_nr; | |
7486 } | |
7487 | |
1470 | 7488 #if defined(FEAT_MBYTE) || defined(PROTO) |
7489 /* | |
7490 * Push a character onto the replace stack. Handles a multi-byte character in | |
7491 * reverse byte order, so that the first byte is popped off first. | |
7492 * Return the number of bytes done (includes composing characters). | |
7493 */ | |
7494 int | |
7495 replace_push_mb(p) | |
7496 char_u *p; | |
7497 { | |
7498 int l = (*mb_ptr2len)(p); | |
7499 int j; | |
7500 | |
7501 for (j = l - 1; j >= 0; --j) | |
7502 replace_push(p[j]); | |
7503 return l; | |
7504 } | |
7505 #endif | |
7506 | |
7 | 7507 /* |
7508 * Pop one item from the replace stack. | |
7509 * return -1 if stack empty | |
7510 * return replaced character or NUL otherwise | |
7511 */ | |
7512 static int | |
7513 replace_pop() | |
7514 { | |
7515 if (replace_stack_nr == 0) | |
7516 return -1; | |
7517 return (int)replace_stack[--replace_stack_nr]; | |
7518 } | |
7519 | |
7520 /* | |
7521 * Join the top two items on the replace stack. This removes to "off"'th NUL | |
7522 * encountered. | |
7523 */ | |
7524 static void | |
7525 replace_join(off) | |
7526 int off; /* offset for which NUL to remove */ | |
7527 { | |
7528 int i; | |
7529 | |
7530 for (i = replace_stack_nr; --i >= 0; ) | |
7531 if (replace_stack[i] == NUL && off-- <= 0) | |
7532 { | |
7533 --replace_stack_nr; | |
7534 mch_memmove(replace_stack + i, replace_stack + i + 1, | |
7535 (size_t)(replace_stack_nr - i)); | |
7536 return; | |
7537 } | |
7538 } | |
7539 | |
7540 /* | |
7541 * Pop bytes from the replace stack until a NUL is found, and insert them | |
7542 * before the cursor. Can only be used in REPLACE or VREPLACE mode. | |
7543 */ | |
7544 static void | |
7545 replace_pop_ins() | |
7546 { | |
7547 int cc; | |
7548 int oldState = State; | |
7549 | |
7550 State = NORMAL; /* don't want REPLACE here */ | |
7551 while ((cc = replace_pop()) > 0) | |
7552 { | |
7553 #ifdef FEAT_MBYTE | |
7554 mb_replace_pop_ins(cc); | |
7555 #else | |
7556 ins_char(cc); | |
7557 #endif | |
7558 dec_cursor(); | |
7559 } | |
7560 State = oldState; | |
7561 } | |
7562 | |
7563 #ifdef FEAT_MBYTE | |
7564 /* | |
7565 * Insert bytes popped from the replace stack. "cc" is the first byte. If it | |
7566 * indicates a multi-byte char, pop the other bytes too. | |
7567 */ | |
7568 static void | |
7569 mb_replace_pop_ins(cc) | |
7570 int cc; | |
7571 { | |
7572 int n; | |
3549 | 7573 char_u buf[MB_MAXBYTES + 1]; |
7 | 7574 int i; |
7575 int c; | |
7576 | |
7577 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1) | |
7578 { | |
7579 buf[0] = cc; | |
7580 for (i = 1; i < n; ++i) | |
7581 buf[i] = replace_pop(); | |
7582 ins_bytes_len(buf, n); | |
7583 } | |
7584 else | |
7585 ins_char(cc); | |
7586 | |
7587 if (enc_utf8) | |
7588 /* Handle composing chars. */ | |
7589 for (;;) | |
7590 { | |
7591 c = replace_pop(); | |
7592 if (c == -1) /* stack empty */ | |
7593 break; | |
7594 if ((n = MB_BYTE2LEN(c)) == 1) | |
7595 { | |
7596 /* Not a multi-byte char, put it back. */ | |
7597 replace_push(c); | |
7598 break; | |
7599 } | |
7600 else | |
7601 { | |
7602 buf[0] = c; | |
7603 for (i = 1; i < n; ++i) | |
7604 buf[i] = replace_pop(); | |
7605 if (utf_iscomposing(utf_ptr2char(buf))) | |
7606 ins_bytes_len(buf, n); | |
7607 else | |
7608 { | |
7609 /* Not a composing char, put it back. */ | |
7610 for (i = n - 1; i >= 0; --i) | |
7611 replace_push(buf[i]); | |
7612 break; | |
7613 } | |
7614 } | |
7615 } | |
7616 } | |
7617 #endif | |
7618 | |
7619 /* | |
7620 * make the replace stack empty | |
7621 * (called when exiting replace mode) | |
7622 */ | |
7623 static void | |
7624 replace_flush() | |
7625 { | |
7626 vim_free(replace_stack); | |
7627 replace_stack = NULL; | |
7628 replace_stack_len = 0; | |
7629 replace_stack_nr = 0; | |
7630 } | |
7631 | |
7632 /* | |
7633 * Handle doing a BS for one character. | |
7634 * cc < 0: replace stack empty, just move cursor | |
7635 * cc == 0: character was inserted, delete it | |
7636 * cc > 0: character was replaced, put cc (first byte of original char) back | |
7637 * and check for more characters to be put back | |
1782 | 7638 * When "limit_col" is >= 0, don't delete before this column. Matters when |
7639 * using composing characters, use del_char_after_col() instead of del_char(). | |
7 | 7640 */ |
7641 static void | |
1782 | 7642 replace_do_bs(limit_col) |
7643 int limit_col; | |
7 | 7644 { |
7645 int cc; | |
7646 #ifdef FEAT_VREPLACE | |
7647 int orig_len = 0; | |
7648 int ins_len; | |
7649 int orig_vcols = 0; | |
7650 colnr_T start_vcol; | |
7651 char_u *p; | |
7652 int i; | |
7653 int vcol; | |
7654 #endif | |
7655 | |
7656 cc = replace_pop(); | |
7657 if (cc > 0) | |
7658 { | |
7659 #ifdef FEAT_VREPLACE | |
7660 if (State & VREPLACE_FLAG) | |
7661 { | |
7662 /* Get the number of screen cells used by the character we are | |
7663 * going to delete. */ | |
7664 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL); | |
7665 orig_vcols = chartabsize(ml_get_cursor(), start_vcol); | |
7666 } | |
7667 #endif | |
7668 #ifdef FEAT_MBYTE | |
7669 if (has_mbyte) | |
7670 { | |
1782 | 7671 (void)del_char_after_col(limit_col); |
7 | 7672 # ifdef FEAT_VREPLACE |
7673 if (State & VREPLACE_FLAG) | |
835 | 7674 orig_len = (int)STRLEN(ml_get_cursor()); |
7 | 7675 # endif |
7676 replace_push(cc); | |
7677 } | |
7678 else | |
7679 #endif | |
7680 { | |
7681 pchar_cursor(cc); | |
7682 #ifdef FEAT_VREPLACE | |
7683 if (State & VREPLACE_FLAG) | |
835 | 7684 orig_len = (int)STRLEN(ml_get_cursor()) - 1; |
7 | 7685 #endif |
7686 } | |
7687 replace_pop_ins(); | |
7688 | |
7689 #ifdef FEAT_VREPLACE | |
7690 if (State & VREPLACE_FLAG) | |
7691 { | |
7692 /* Get the number of screen cells used by the inserted characters */ | |
7693 p = ml_get_cursor(); | |
835 | 7694 ins_len = (int)STRLEN(p) - orig_len; |
7 | 7695 vcol = start_vcol; |
7696 for (i = 0; i < ins_len; ++i) | |
7697 { | |
7698 vcol += chartabsize(p + i, vcol); | |
7699 #ifdef FEAT_MBYTE | |
474 | 7700 i += (*mb_ptr2len)(p) - 1; |
7 | 7701 #endif |
7702 } | |
7703 vcol -= start_vcol; | |
7704 | |
7705 /* Delete spaces that were inserted after the cursor to keep the | |
7706 * text aligned. */ | |
7707 curwin->w_cursor.col += ins_len; | |
7708 while (vcol > orig_vcols && gchar_cursor() == ' ') | |
7709 { | |
7710 del_char(FALSE); | |
7711 ++orig_vcols; | |
7712 } | |
7713 curwin->w_cursor.col -= ins_len; | |
7714 } | |
7715 #endif | |
7716 | |
7717 /* mark the buffer as changed and prepare for displaying */ | |
7718 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col); | |
7719 } | |
7720 else if (cc == 0) | |
1782 | 7721 (void)del_char_after_col(limit_col); |
7 | 7722 } |
7723 | |
7724 #ifdef FEAT_CINDENT | |
7725 /* | |
7726 * Return TRUE if C-indenting is on. | |
7727 */ | |
7728 static int | |
7729 cindent_on() | |
7730 { | |
7731 return (!p_paste && (curbuf->b_p_cin | |
7732 # ifdef FEAT_EVAL | |
7733 || *curbuf->b_p_inde != NUL | |
7734 # endif | |
7735 )); | |
7736 } | |
7737 #endif | |
7738 | |
7739 #if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO) | |
7740 /* | |
7741 * Re-indent the current line, based on the current contents of it and the | |
7742 * surrounding lines. Fixing the cursor position seems really easy -- I'm very | |
7743 * confused what all the part that handles Control-T is doing that I'm not. | |
7744 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent. | |
7745 */ | |
7746 | |
7747 void | |
7748 fixthisline(get_the_indent) | |
7749 int (*get_the_indent) __ARGS((void)); | |
7750 { | |
1516 | 7751 change_indent(INDENT_SET, get_the_indent(), FALSE, 0, TRUE); |
7 | 7752 if (linewhite(curwin->w_cursor.lnum)) |
7753 did_ai = TRUE; /* delete the indent if the line stays empty */ | |
7754 } | |
7755 | |
7756 void | |
7757 fix_indent() | |
7758 { | |
7759 if (p_paste) | |
7760 return; | |
7761 # ifdef FEAT_LISP | |
7762 if (curbuf->b_p_lisp && curbuf->b_p_ai) | |
7763 fixthisline(get_lisp_indent); | |
7764 # endif | |
7765 # if defined(FEAT_LISP) && defined(FEAT_CINDENT) | |
7766 else | |
7767 # endif | |
7768 # ifdef FEAT_CINDENT | |
7769 if (cindent_on()) | |
7770 do_c_expr_indent(); | |
7771 # endif | |
7772 } | |
7773 | |
7774 #endif | |
7775 | |
7776 #ifdef FEAT_CINDENT | |
7777 /* | |
7778 * return TRUE if 'cinkeys' contains the key "keytyped", | |
7779 * when == '*': Only if key is preceded with '*' (indent before insert) | |
4352 | 7780 * when == '!': Only if key is preceded with '!' (don't insert) |
7 | 7781 * when == ' ': Only if key is not preceded with '*'(indent afterwards) |
7782 * | |
7783 * "keytyped" can have a few special values: | |
7784 * KEY_OPEN_FORW | |
7785 * KEY_OPEN_BACK | |
7786 * KEY_COMPLETE just finished completion. | |
7787 * | |
7788 * If line_is_empty is TRUE accept keys with '0' before them. | |
7789 */ | |
7790 int | |
7791 in_cinkeys(keytyped, when, line_is_empty) | |
7792 int keytyped; | |
7793 int when; | |
7794 int line_is_empty; | |
7795 { | |
7796 char_u *look; | |
7797 int try_match; | |
7798 int try_match_word; | |
7799 char_u *p; | |
7800 char_u *line; | |
7801 int icase; | |
7802 int i; | |
7803 | |
2025 | 7804 if (keytyped == NUL) |
7805 /* Can happen with CTRL-Y and CTRL-E on a short line. */ | |
7806 return FALSE; | |
7807 | |
7 | 7808 #ifdef FEAT_EVAL |
7809 if (*curbuf->b_p_inde != NUL) | |
7810 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */ | |
7811 else | |
7812 #endif | |
7813 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */ | |
7814 while (*look) | |
7815 { | |
7816 /* | |
7817 * Find out if we want to try a match with this key, depending on | |
7818 * 'when' and a '*' or '!' before the key. | |
7819 */ | |
7820 switch (when) | |
7821 { | |
7822 case '*': try_match = (*look == '*'); break; | |
7823 case '!': try_match = (*look == '!'); break; | |
7824 default: try_match = (*look != '*'); break; | |
7825 } | |
7826 if (*look == '*' || *look == '!') | |
7827 ++look; | |
7828 | |
7829 /* | |
7830 * If there is a '0', only accept a match if the line is empty. | |
7831 * But may still match when typing last char of a word. | |
7832 */ | |
7833 if (*look == '0') | |
7834 { | |
7835 try_match_word = try_match; | |
7836 if (!line_is_empty) | |
7837 try_match = FALSE; | |
7838 ++look; | |
7839 } | |
7840 else | |
7841 try_match_word = FALSE; | |
7842 | |
7843 /* | |
7844 * does it look like a control character? | |
7845 */ | |
7846 if (*look == '^' | |
7847 #ifdef EBCDIC | |
7848 && (Ctrl_chr(look[1]) != 0) | |
7849 #else | |
7850 && look[1] >= '?' && look[1] <= '_' | |
7851 #endif | |
7852 ) | |
7853 { | |
7854 if (try_match && keytyped == Ctrl_chr(look[1])) | |
7855 return TRUE; | |
7856 look += 2; | |
7857 } | |
7858 /* | |
7859 * 'o' means "o" command, open forward. | |
7860 * 'O' means "O" command, open backward. | |
7861 */ | |
7862 else if (*look == 'o') | |
7863 { | |
7864 if (try_match && keytyped == KEY_OPEN_FORW) | |
7865 return TRUE; | |
7866 ++look; | |
7867 } | |
7868 else if (*look == 'O') | |
7869 { | |
7870 if (try_match && keytyped == KEY_OPEN_BACK) | |
7871 return TRUE; | |
7872 ++look; | |
7873 } | |
7874 | |
7875 /* | |
7876 * 'e' means to check for "else" at start of line and just before the | |
7877 * cursor. | |
7878 */ | |
7879 else if (*look == 'e') | |
7880 { | |
7881 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4) | |
7882 { | |
7883 p = ml_get_curline(); | |
7884 if (skipwhite(p) == p + curwin->w_cursor.col - 4 && | |
7885 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0) | |
7886 return TRUE; | |
7887 } | |
7888 ++look; | |
7889 } | |
7890 | |
7891 /* | |
7892 * ':' only causes an indent if it is at the end of a label or case | |
7893 * statement, or when it was before typing the ':' (to fix | |
7894 * class::method for C++). | |
7895 */ | |
7896 else if (*look == ':') | |
7897 { | |
7898 if (try_match && keytyped == ':') | |
7899 { | |
7900 p = ml_get_curline(); | |
5442 | 7901 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel()) |
7 | 7902 return TRUE; |
1300 | 7903 /* Need to get the line again after cin_islabel(). */ |
7904 p = ml_get_curline(); | |
7 | 7905 if (curwin->w_cursor.col > 2 |
7906 && p[curwin->w_cursor.col - 1] == ':' | |
7907 && p[curwin->w_cursor.col - 2] == ':') | |
7908 { | |
7909 p[curwin->w_cursor.col - 1] = ' '; | |
2297
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2294
diff
changeset
|
7910 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p) |
5442 | 7911 || cin_islabel()); |
7 | 7912 p = ml_get_curline(); |
7913 p[curwin->w_cursor.col - 1] = ':'; | |
7914 if (i) | |
7915 return TRUE; | |
7916 } | |
7917 } | |
7918 ++look; | |
7919 } | |
7920 | |
7921 | |
7922 /* | |
7923 * Is it a key in <>, maybe? | |
7924 */ | |
7925 else if (*look == '<') | |
7926 { | |
7927 if (try_match) | |
7928 { | |
7929 /* | |
7930 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>, | |
7931 * <:> and <!> so that people can re-indent on o, O, e, 0, <, | |
7932 * >, *, : and ! keys if they really really want to. | |
7933 */ | |
7934 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL | |
7935 && keytyped == look[1]) | |
7936 return TRUE; | |
7937 | |
7938 if (keytyped == get_special_key_code(look + 1)) | |
7939 return TRUE; | |
7940 } | |
7941 while (*look && *look != '>') | |
7942 look++; | |
7943 while (*look == '>') | |
7944 look++; | |
7945 } | |
7946 | |
7947 /* | |
7948 * Is it a word: "=word"? | |
7949 */ | |
7950 else if (*look == '=' && look[1] != ',' && look[1] != NUL) | |
7951 { | |
7952 ++look; | |
7953 if (*look == '~') | |
7954 { | |
7955 icase = TRUE; | |
7956 ++look; | |
7957 } | |
7958 else | |
7959 icase = FALSE; | |
7960 p = vim_strchr(look, ','); | |
7961 if (p == NULL) | |
7962 p = look + STRLEN(look); | |
7963 if ((try_match || try_match_word) | |
7964 && curwin->w_cursor.col >= (colnr_T)(p - look)) | |
7965 { | |
7966 int match = FALSE; | |
7967 | |
7968 #ifdef FEAT_INS_EXPAND | |
7969 if (keytyped == KEY_COMPLETE) | |
7970 { | |
7971 char_u *s; | |
7972 | |
7973 /* Just completed a word, check if it starts with "look". | |
7974 * search back for the start of a word. */ | |
7975 line = ml_get_curline(); | |
7976 # ifdef FEAT_MBYTE | |
7977 if (has_mbyte) | |
7978 { | |
7979 char_u *n; | |
7980 | |
7981 for (s = line + curwin->w_cursor.col; s > line; s = n) | |
7982 { | |
7983 n = mb_prevptr(line, s); | |
7984 if (!vim_iswordp(n)) | |
7985 break; | |
7986 } | |
7987 } | |
7988 else | |
7989 # endif | |
7990 for (s = line + curwin->w_cursor.col; s > line; --s) | |
7991 if (!vim_iswordc(s[-1])) | |
7992 break; | |
7993 if (s + (p - look) <= line + curwin->w_cursor.col | |
7994 && (icase | |
7995 ? MB_STRNICMP(s, look, p - look) | |
7996 : STRNCMP(s, look, p - look)) == 0) | |
7997 match = TRUE; | |
7998 } | |
7999 else | |
8000 #endif | |
8001 /* TODO: multi-byte */ | |
8002 if (keytyped == (int)p[-1] || (icase && keytyped < 256 | |
8003 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1]))) | |
8004 { | |
8005 line = ml_get_cursor(); | |
8006 if ((curwin->w_cursor.col == (colnr_T)(p - look) | |
8007 || !vim_iswordc(line[-(p - look) - 1])) | |
8008 && (icase | |
8009 ? MB_STRNICMP(line - (p - look), look, p - look) | |
8010 : STRNCMP(line - (p - look), look, p - look)) | |
8011 == 0) | |
8012 match = TRUE; | |
8013 } | |
8014 if (match && try_match_word && !try_match) | |
8015 { | |
8016 /* "0=word": Check if there are only blanks before the | |
8017 * word. */ | |
8018 line = ml_get_curline(); | |
8019 if ((int)(skipwhite(line) - line) != | |
8020 (int)(curwin->w_cursor.col - (p - look))) | |
8021 match = FALSE; | |
8022 } | |
8023 if (match) | |
8024 return TRUE; | |
8025 } | |
8026 look = p; | |
8027 } | |
8028 | |
8029 /* | |
8030 * ok, it's a boring generic character. | |
8031 */ | |
8032 else | |
8033 { | |
8034 if (try_match && *look == keytyped) | |
8035 return TRUE; | |
8036 ++look; | |
8037 } | |
8038 | |
8039 /* | |
8040 * Skip over ", ". | |
8041 */ | |
8042 look = skip_to_option_part(look); | |
8043 } | |
8044 return FALSE; | |
8045 } | |
8046 #endif /* FEAT_CINDENT */ | |
8047 | |
8048 #if defined(FEAT_RIGHTLEFT) || defined(PROTO) | |
8049 /* | |
8050 * Map Hebrew keyboard when in hkmap mode. | |
8051 */ | |
8052 int | |
8053 hkmap(c) | |
8054 int c; | |
8055 { | |
8056 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */ | |
8057 { | |
8058 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD, | |
8059 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN, | |
8060 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV}; | |
8061 static char_u map[26] = | |
8062 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/, | |
8063 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/, | |
8064 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/, | |
8065 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/, | |
8066 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/, | |
8067 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/, | |
8068 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/, | |
8069 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/, | |
8070 (char_u)AIN /*y*/, (char_u)ZADI /*z*/}; | |
8071 | |
8072 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z') | |
8073 return (int)(map[CharOrd(c)] - 1 + p_aleph); | |
8074 /* '-1'='sofit' */ | |
8075 else if (c == 'x') | |
8076 return 'X'; | |
8077 else if (c == 'q') | |
8078 return '\''; /* {geresh}={'} */ | |
8079 else if (c == 246) | |
8080 return ' '; /* \"o --> ' ' for a german keyboard */ | |
8081 else if (c == 228) | |
8082 return ' '; /* \"a --> ' ' -- / -- */ | |
8083 else if (c == 252) | |
8084 return ' '; /* \"u --> ' ' -- / -- */ | |
8085 #ifdef EBCDIC | |
8086 else if (islower(c)) | |
8087 #else | |
8088 /* NOTE: islower() does not do the right thing for us on Linux so we | |
8089 * do this the same was as 5.7 and previous, so it works correctly on | |
8090 * all systems. Specifically, the e.g. Delete and Arrow keys are | |
8091 * munged and won't work if e.g. searching for Hebrew text. | |
8092 */ | |
8093 else if (c >= 'a' && c <= 'z') | |
8094 #endif | |
8095 return (int)(map[CharOrdLow(c)] + p_aleph); | |
8096 else | |
8097 return c; | |
8098 } | |
8099 else | |
8100 { | |
8101 switch (c) | |
8102 { | |
8103 case '`': return ';'; | |
8104 case '/': return '.'; | |
8105 case '\'': return ','; | |
8106 case 'q': return '/'; | |
8107 case 'w': return '\''; | |
8108 | |
8109 /* Hebrew letters - set offset from 'a' */ | |
8110 case ',': c = '{'; break; | |
8111 case '.': c = 'v'; break; | |
8112 case ';': c = 't'; break; | |
8113 default: { | |
8114 static char str[] = "zqbcxlsjphmkwonu ydafe rig"; | |
8115 | |
8116 #ifdef EBCDIC | |
8117 /* see note about islower() above */ | |
8118 if (!islower(c)) | |
8119 #else | |
8120 if (c < 'a' || c > 'z') | |
8121 #endif | |
8122 return c; | |
8123 c = str[CharOrdLow(c)]; | |
8124 break; | |
8125 } | |
8126 } | |
8127 | |
8128 return (int)(CharOrdLow(c) + p_aleph); | |
8129 } | |
8130 } | |
8131 #endif | |
8132 | |
8133 static void | |
8134 ins_reg() | |
8135 { | |
8136 int need_redraw = FALSE; | |
8137 int regname; | |
8138 int literally = 0; | |
844 | 8139 int vis_active = VIsual_active; |
7 | 8140 |
8141 /* | |
8142 * If we are going to wait for a character, show a '"'. | |
8143 */ | |
8144 pc_status = PC_STATUS_UNSET; | |
8145 if (redrawing() && !char_avail()) | |
8146 { | |
8147 /* may need to redraw when no more chars available now */ | |
661 | 8148 ins_redraw(FALSE); |
7 | 8149 |
8150 edit_putchar('"', TRUE); | |
8151 #ifdef FEAT_CMDL_INFO | |
8152 add_to_showcmd_c(Ctrl_R); | |
8153 #endif | |
8154 } | |
8155 | |
8156 #ifdef USE_ON_FLY_SCROLL | |
8157 dont_scroll = TRUE; /* disallow scrolling here */ | |
8158 #endif | |
8159 | |
8160 /* | |
8161 * Don't map the register name. This also prevents the mode message to be | |
8162 * deleted when ESC is hit. | |
8163 */ | |
8164 ++no_mapping; | |
1389 | 8165 regname = plain_vgetc(); |
7 | 8166 LANGMAP_ADJUST(regname, TRUE); |
8167 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P) | |
8168 { | |
8169 /* Get a third key for literal register insertion */ | |
8170 literally = regname; | |
8171 #ifdef FEAT_CMDL_INFO | |
8172 add_to_showcmd_c(literally); | |
8173 #endif | |
1389 | 8174 regname = plain_vgetc(); |
7 | 8175 LANGMAP_ADJUST(regname, TRUE); |
8176 } | |
8177 --no_mapping; | |
8178 | |
8179 #ifdef FEAT_EVAL | |
4909
2945fe016b93
updated for version 7.3.1200
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
8180 /* Don't call u_sync() while typing the expression or giving an error |
2945fe016b93
updated for version 7.3.1200
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
8181 * message for it. Only call it explicitly. */ |
7 | 8182 ++no_u_sync; |
8183 if (regname == '=') | |
8184 { | |
133 | 8185 # ifdef USE_IM_CONTROL |
7 | 8186 int im_on = im_get_status(); |
133 | 8187 # endif |
5120
6e611380b4cf
updated for version 7.3.1303
Bram Moolenaar <bram@vim.org>
parents:
5094
diff
changeset
|
8188 /* Sync undo when evaluating the expression calls setline() or |
6e611380b4cf
updated for version 7.3.1303
Bram Moolenaar <bram@vim.org>
parents:
5094
diff
changeset
|
8189 * append(), so that it can be undone separately. */ |
6e611380b4cf
updated for version 7.3.1303
Bram Moolenaar <bram@vim.org>
parents:
5094
diff
changeset
|
8190 u_sync_once = 2; |
5018
d6a7dea44a86
updated for version 7.3.1253
Bram Moolenaar <bram@vim.org>
parents:
4909
diff
changeset
|
8191 |
7 | 8192 regname = get_expr_register(); |
133 | 8193 # ifdef USE_IM_CONTROL |
7 | 8194 /* Restore the Input Method. */ |
8195 if (im_on) | |
8196 im_set_active(TRUE); | |
133 | 8197 # endif |
7 | 8198 } |
140 | 8199 if (regname == NUL || !valid_yank_reg(regname, FALSE)) |
8200 { | |
8201 vim_beep(); | |
7 | 8202 need_redraw = TRUE; /* remove the '"' */ |
140 | 8203 } |
7 | 8204 else |
8205 { | |
8206 #endif | |
8207 if (literally == Ctrl_O || literally == Ctrl_P) | |
8208 { | |
8209 /* Append the command to the redo buffer. */ | |
8210 AppendCharToRedobuff(Ctrl_R); | |
8211 AppendCharToRedobuff(literally); | |
8212 AppendCharToRedobuff(regname); | |
8213 | |
8214 do_put(regname, BACKWARD, 1L, | |
8215 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND); | |
8216 } | |
8217 else if (insert_reg(regname, literally) == FAIL) | |
8218 { | |
8219 vim_beep(); | |
8220 need_redraw = TRUE; /* remove the '"' */ | |
8221 } | |
133 | 8222 else if (stop_insert_mode) |
8223 /* When the '=' register was used and a function was invoked that | |
8224 * did ":stopinsert" then stuff_empty() returns FALSE but we won't | |
8225 * insert anything, need to remove the '"' */ | |
8226 need_redraw = TRUE; | |
8227 | |
7 | 8228 #ifdef FEAT_EVAL |
8229 } | |
8230 --no_u_sync; | |
5120
6e611380b4cf
updated for version 7.3.1303
Bram Moolenaar <bram@vim.org>
parents:
5094
diff
changeset
|
8231 if (u_sync_once == 1) |
6e611380b4cf
updated for version 7.3.1303
Bram Moolenaar <bram@vim.org>
parents:
5094
diff
changeset
|
8232 ins_need_undo = TRUE; |
6e611380b4cf
updated for version 7.3.1303
Bram Moolenaar <bram@vim.org>
parents:
5094
diff
changeset
|
8233 u_sync_once = 0; |
7 | 8234 #endif |
8235 #ifdef FEAT_CMDL_INFO | |
8236 clear_showcmd(); | |
8237 #endif | |
8238 | |
8239 /* If the inserted register is empty, we need to remove the '"' */ | |
8240 if (need_redraw || stuff_empty()) | |
8241 edit_unputchar(); | |
844 | 8242 |
8243 /* Disallow starting Visual mode here, would get a weird mode. */ | |
8244 if (!vis_active && VIsual_active) | |
8245 end_visual_mode(); | |
7 | 8246 } |
8247 | |
8248 /* | |
8249 * CTRL-G commands in Insert mode. | |
8250 */ | |
8251 static void | |
8252 ins_ctrl_g() | |
8253 { | |
8254 int c; | |
8255 | |
8256 #ifdef FEAT_INS_EXPAND | |
8257 /* Right after CTRL-X the cursor will be after the ruler. */ | |
8258 setcursor(); | |
8259 #endif | |
8260 | |
8261 /* | |
8262 * Don't map the second key. This also prevents the mode message to be | |
8263 * deleted when ESC is hit. | |
8264 */ | |
8265 ++no_mapping; | |
1389 | 8266 c = plain_vgetc(); |
7 | 8267 --no_mapping; |
8268 switch (c) | |
8269 { | |
8270 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */ | |
8271 case K_UP: | |
8272 case Ctrl_K: | |
8273 case 'k': ins_up(TRUE); | |
8274 break; | |
8275 | |
8276 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */ | |
8277 case K_DOWN: | |
8278 case Ctrl_J: | |
8279 case 'j': ins_down(TRUE); | |
8280 break; | |
8281 | |
8282 /* CTRL-G u: start new undoable edit */ | |
825 | 8283 case 'u': u_sync(TRUE); |
7 | 8284 ins_need_undo = TRUE; |
626 | 8285 |
8286 /* Need to reset Insstart, esp. because a BS that joins | |
1219 | 8287 * a line to the previous one must save for undo. */ |
5680 | 8288 update_Insstart_orig = FALSE; |
626 | 8289 Insstart = curwin->w_cursor; |
7 | 8290 break; |
8291 | |
8292 /* Unknown CTRL-G command, reserved for future expansion. */ | |
8293 default: vim_beep(); | |
8294 } | |
8295 } | |
8296 | |
8297 /* | |
449 | 8298 * CTRL-^ in Insert mode. |
8299 */ | |
8300 static void | |
8301 ins_ctrl_hat() | |
8302 { | |
782 | 8303 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE)) |
449 | 8304 { |
8305 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */ | |
8306 if (State & LANGMAP) | |
8307 { | |
8308 curbuf->b_p_iminsert = B_IMODE_NONE; | |
8309 State &= ~LANGMAP; | |
8310 } | |
8311 else | |
8312 { | |
8313 curbuf->b_p_iminsert = B_IMODE_LMAP; | |
8314 State |= LANGMAP; | |
8315 #ifdef USE_IM_CONTROL | |
8316 im_set_active(FALSE); | |
8317 #endif | |
8318 } | |
8319 } | |
8320 #ifdef USE_IM_CONTROL | |
8321 else | |
8322 { | |
8323 /* There are no ":lmap" mappings, toggle IM */ | |
8324 if (im_get_status()) | |
8325 { | |
8326 curbuf->b_p_iminsert = B_IMODE_NONE; | |
8327 im_set_active(FALSE); | |
8328 } | |
8329 else | |
8330 { | |
8331 curbuf->b_p_iminsert = B_IMODE_IM; | |
8332 State &= ~LANGMAP; | |
8333 im_set_active(TRUE); | |
8334 } | |
8335 } | |
8336 #endif | |
8337 set_iminsert_global(); | |
8338 showmode(); | |
8339 #ifdef FEAT_GUI | |
8340 /* may show different cursor shape or color */ | |
8341 if (gui.in_use) | |
8342 gui_update_cursor(TRUE, FALSE); | |
8343 #endif | |
8344 #if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP) | |
8345 /* Show/unshow value of 'keymap' in status lines. */ | |
8346 status_redraw_curbuf(); | |
8347 #endif | |
8348 } | |
8349 | |
8350 /* | |
7 | 8351 * Handle ESC in insert mode. |
8352 * Returns TRUE when leaving insert mode, FALSE when going to repeat the | |
8353 * insert. | |
8354 */ | |
8355 static int | |
477 | 8356 ins_esc(count, cmdchar, nomove) |
7 | 8357 long *count; |
8358 int cmdchar; | |
477 | 8359 int nomove; /* don't move cursor */ |
7 | 8360 { |
8361 int temp; | |
8362 static int disabled_redraw = FALSE; | |
8363 | |
744 | 8364 #ifdef FEAT_SPELL |
449 | 8365 check_spell_redraw(); |
8366 #endif | |
7 | 8367 #if defined(FEAT_HANGULIN) |
8368 # if defined(ESC_CHG_TO_ENG_MODE) | |
8369 hangul_input_state_set(0); | |
8370 # endif | |
8371 if (composing_hangul) | |
8372 { | |
8373 push_raw_key(composing_hangul_buffer, 2); | |
8374 composing_hangul = 0; | |
8375 } | |
8376 #endif | |
8377 | |
8378 temp = curwin->w_cursor.col; | |
8379 if (disabled_redraw) | |
8380 { | |
8381 --RedrawingDisabled; | |
8382 disabled_redraw = FALSE; | |
8383 } | |
8384 if (!arrow_used) | |
8385 { | |
8386 /* | |
8387 * Don't append the ESC for "r<CR>" and "grx". | |
75 | 8388 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for |
8389 * when "count" is non-zero. | |
7 | 8390 */ |
8391 if (cmdchar != 'r' && cmdchar != 'v') | |
75 | 8392 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR); |
7 | 8393 |
8394 /* | |
8395 * Repeating insert may take a long time. Check for | |
8396 * interrupt now and then. | |
8397 */ | |
8398 if (*count > 0) | |
8399 { | |
8400 line_breakcheck(); | |
8401 if (got_int) | |
8402 *count = 0; | |
8403 } | |
8404 | |
8405 if (--*count > 0) /* repeat what was typed */ | |
8406 { | |
164 | 8407 /* Vi repeats the insert without replacing characters. */ |
8408 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL) | |
8409 State &= ~REPLACE_FLAG; | |
8410 | |
7 | 8411 (void)start_redo_ins(); |
8412 if (cmdchar == 'r' || cmdchar == 'v') | |
6098 | 8413 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */ |
7 | 8414 ++RedrawingDisabled; |
8415 disabled_redraw = TRUE; | |
8416 return FALSE; /* repeat the insert */ | |
8417 } | |
5434 | 8418 stop_insert(&curwin->w_cursor, TRUE, nomove); |
7 | 8419 undisplay_dollar(); |
8420 } | |
8421 | |
8422 /* When an autoindent was removed, curswant stays after the | |
8423 * indent */ | |
8424 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col) | |
8425 curwin->w_set_curswant = TRUE; | |
8426 | |
8427 /* Remember the last Insert position in the '^ mark. */ | |
8428 if (!cmdmod.keepjumps) | |
8429 curbuf->b_last_insert = curwin->w_cursor; | |
8430 | |
8431 /* | |
8432 * The cursor should end up on the last inserted character. | |
477 | 8433 * Don't do it for CTRL-O, unless past the end of the line. |
7 | 8434 */ |
477 | 8435 if (!nomove |
8436 && (curwin->w_cursor.col != 0 | |
7 | 8437 #ifdef FEAT_VIRTUALEDIT |
8438 || curwin->w_cursor.coladd > 0 | |
8439 #endif | |
477 | 8440 ) |
8441 && (restart_edit == NUL | |
5735 | 8442 || (gchar_cursor() == NUL && !VIsual_active)) |
7 | 8443 #ifdef FEAT_RIGHTLEFT |
8444 && !revins_on | |
8445 #endif | |
8446 ) | |
8447 { | |
8448 #ifdef FEAT_VIRTUALEDIT | |
8449 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL) | |
8450 { | |
8451 oneleft(); | |
8452 if (restart_edit != NUL) | |
8453 ++curwin->w_cursor.coladd; | |
8454 } | |
8455 else | |
8456 #endif | |
8457 { | |
8458 --curwin->w_cursor.col; | |
8459 #ifdef FEAT_MBYTE | |
8460 /* Correct cursor for multi-byte character. */ | |
8461 if (has_mbyte) | |
8462 mb_adjust_cursor(); | |
8463 #endif | |
8464 } | |
8465 } | |
8466 | |
8467 #ifdef USE_IM_CONTROL | |
8468 /* Disable IM to allow typing English directly for Normal mode commands. | |
8469 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as | |
8470 * well). */ | |
8471 if (!(State & LANGMAP)) | |
8472 im_save_status(&curbuf->b_p_iminsert); | |
8473 im_set_active(FALSE); | |
8474 #endif | |
8475 | |
8476 State = NORMAL; | |
8477 /* need to position cursor again (e.g. when on a TAB ) */ | |
8478 changed_cline_bef_curs(); | |
8479 | |
8480 #ifdef FEAT_MOUSE | |
8481 setmouse(); | |
8482 #endif | |
8483 #ifdef CURSOR_SHAPE | |
8484 ui_cursor_shape(); /* may show different cursor shape */ | |
8485 #endif | |
8486 | |
8487 /* | |
8488 * When recording or for CTRL-O, need to display the new mode. | |
8489 * Otherwise remove the mode message. | |
8490 */ | |
8491 if (Recording || restart_edit != NUL) | |
8492 showmode(); | |
8493 else if (p_smd) | |
8494 MSG(""); | |
8495 | |
8496 return TRUE; /* exit Insert mode */ | |
8497 } | |
8498 | |
8499 #ifdef FEAT_RIGHTLEFT | |
8500 /* | |
8501 * Toggle language: hkmap and revins_on. | |
8502 * Move to end of reverse inserted text. | |
8503 */ | |
8504 static void | |
8505 ins_ctrl_() | |
8506 { | |
8507 if (revins_on && revins_chars && revins_scol >= 0) | |
8508 { | |
8509 while (gchar_cursor() != NUL && revins_chars--) | |
8510 ++curwin->w_cursor.col; | |
8511 } | |
8512 p_ri = !p_ri; | |
8513 revins_on = (State == INSERT && p_ri); | |
8514 if (revins_on) | |
8515 { | |
8516 revins_scol = curwin->w_cursor.col; | |
8517 revins_legal++; | |
8518 revins_chars = 0; | |
8519 undisplay_dollar(); | |
8520 } | |
8521 else | |
8522 revins_scol = -1; | |
8523 #ifdef FEAT_FKMAP | |
8524 if (p_altkeymap) | |
8525 { | |
8526 /* | |
8527 * to be consistent also for redo command, using '.' | |
8528 * set arrow_used to true and stop it - causing to redo | |
8529 * characters entered in one mode (normal/reverse insert). | |
8530 */ | |
8531 arrow_used = TRUE; | |
8532 (void)stop_arrow(); | |
8533 p_fkmap = curwin->w_p_rl ^ p_ri; | |
8534 if (p_fkmap && p_ri) | |
8535 State = INSERT; | |
8536 } | |
8537 else | |
8538 #endif | |
8539 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */ | |
8540 showmode(); | |
8541 } | |
8542 #endif | |
8543 | |
8544 /* | |
8545 * If 'keymodel' contains "startsel", may start selection. | |
8546 * Returns TRUE when a CTRL-O and other keys stuffed. | |
8547 */ | |
8548 static int | |
8549 ins_start_select(c) | |
8550 int c; | |
8551 { | |
8552 if (km_startsel) | |
8553 switch (c) | |
8554 { | |
8555 case K_KHOME: | |
8556 case K_KEND: | |
8557 case K_PAGEUP: | |
8558 case K_KPAGEUP: | |
8559 case K_PAGEDOWN: | |
8560 case K_KPAGEDOWN: | |
8561 # ifdef MACOS | |
8562 case K_LEFT: | |
8563 case K_RIGHT: | |
8564 case K_UP: | |
8565 case K_DOWN: | |
8566 case K_END: | |
8567 case K_HOME: | |
8568 # endif | |
8569 if (!(mod_mask & MOD_MASK_SHIFT)) | |
8570 break; | |
8571 /* FALLTHROUGH */ | |
8572 case K_S_LEFT: | |
8573 case K_S_RIGHT: | |
8574 case K_S_UP: | |
8575 case K_S_DOWN: | |
8576 case K_S_END: | |
8577 case K_S_HOME: | |
8578 /* Start selection right away, the cursor can move with | |
8579 * CTRL-O when beyond the end of the line. */ | |
8580 start_selection(); | |
8581 | |
8582 /* Execute the key in (insert) Select mode. */ | |
8583 stuffcharReadbuff(Ctrl_O); | |
8584 if (mod_mask) | |
8585 { | |
8586 char_u buf[4]; | |
8587 | |
8588 buf[0] = K_SPECIAL; | |
8589 buf[1] = KS_MODIFIER; | |
8590 buf[2] = mod_mask; | |
8591 buf[3] = NUL; | |
8592 stuffReadbuff(buf); | |
8593 } | |
8594 stuffcharReadbuff(c); | |
8595 return TRUE; | |
8596 } | |
8597 return FALSE; | |
8598 } | |
8599 | |
8600 /* | |
4352 | 8601 * <Insert> key in Insert mode: toggle insert/replace mode. |
449 | 8602 */ |
8603 static void | |
8604 ins_insert(replaceState) | |
8605 int replaceState; | |
8606 { | |
8607 #ifdef FEAT_FKMAP | |
8608 if (p_fkmap && p_ri) | |
8609 { | |
8610 beep_flush(); | |
8611 EMSG(farsi_text_3); /* encoded in Farsi */ | |
8612 return; | |
8613 } | |
8614 #endif | |
8615 | |
8616 #ifdef FEAT_AUTOCMD | |
532 | 8617 # ifdef FEAT_EVAL |
449 | 8618 set_vim_var_string(VV_INSERTMODE, |
8619 (char_u *)((State & REPLACE_FLAG) ? "i" : | |
667 | 8620 # ifdef FEAT_VREPLACE |
8621 replaceState == VREPLACE ? "v" : | |
8622 # endif | |
8623 "r"), 1); | |
532 | 8624 # endif |
449 | 8625 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf); |
8626 #endif | |
8627 if (State & REPLACE_FLAG) | |
8628 State = INSERT | (State & LANGMAP); | |
8629 else | |
8630 State = replaceState | (State & LANGMAP); | |
8631 AppendCharToRedobuff(K_INS); | |
8632 showmode(); | |
8633 #ifdef CURSOR_SHAPE | |
8634 ui_cursor_shape(); /* may show different cursor shape */ | |
8635 #endif | |
8636 } | |
8637 | |
8638 /* | |
8639 * Pressed CTRL-O in Insert mode. | |
8640 */ | |
8641 static void | |
8642 ins_ctrl_o() | |
8643 { | |
8644 #ifdef FEAT_VREPLACE | |
8645 if (State & VREPLACE_FLAG) | |
8646 restart_edit = 'V'; | |
8647 else | |
8648 #endif | |
8649 if (State & REPLACE_FLAG) | |
8650 restart_edit = 'R'; | |
8651 else | |
8652 restart_edit = 'I'; | |
8653 #ifdef FEAT_VIRTUALEDIT | |
8654 if (virtual_active()) | |
8655 ins_at_eol = FALSE; /* cursor always keeps its column */ | |
8656 else | |
8657 #endif | |
8658 ins_at_eol = (gchar_cursor() == NUL); | |
8659 } | |
8660 | |
8661 /* | |
7 | 8662 * If the cursor is on an indent, ^T/^D insert/delete one |
8663 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>". | |
1796 | 8664 * Always round the indent to 'shiftwidth', this is compatible |
7 | 8665 * with vi. But vi only supports ^T and ^D after an |
8666 * autoindent, we support it everywhere. | |
8667 */ | |
8668 static void | |
8669 ins_shift(c, lastc) | |
8670 int c; | |
8671 int lastc; | |
8672 { | |
8673 if (stop_arrow() == FAIL) | |
8674 return; | |
8675 AppendCharToRedobuff(c); | |
8676 | |
8677 /* | |
8678 * 0^D and ^^D: remove all indent. | |
8679 */ | |
1330 | 8680 if (c == Ctrl_D && (lastc == '0' || lastc == '^') |
8681 && curwin->w_cursor.col > 0) | |
7 | 8682 { |
8683 --curwin->w_cursor.col; | |
8684 (void)del_char(FALSE); /* delete the '^' or '0' */ | |
8685 /* In Replace mode, restore the characters that '^' or '0' replaced. */ | |
8686 if (State & REPLACE_FLAG) | |
8687 replace_pop_ins(); | |
8688 if (lastc == '^') | |
8689 old_indent = get_indent(); /* remember curr. indent */ | |
1516 | 8690 change_indent(INDENT_SET, 0, TRUE, 0, TRUE); |
7 | 8691 } |
8692 else | |
1516 | 8693 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE); |
7 | 8694 |
8695 if (did_ai && *skipwhite(ml_get_curline()) != NUL) | |
8696 did_ai = FALSE; | |
8697 #ifdef FEAT_SMARTINDENT | |
8698 did_si = FALSE; | |
8699 can_si = FALSE; | |
8700 can_si_back = FALSE; | |
8701 #endif | |
8702 #ifdef FEAT_CINDENT | |
8703 can_cindent = FALSE; /* no cindenting after ^D or ^T */ | |
8704 #endif | |
8705 } | |
8706 | |
8707 static void | |
8708 ins_del() | |
8709 { | |
8710 int temp; | |
8711 | |
8712 if (stop_arrow() == FAIL) | |
8713 return; | |
8714 if (gchar_cursor() == NUL) /* delete newline */ | |
8715 { | |
8716 temp = curwin->w_cursor.col; | |
8717 if (!can_bs(BS_EOL) /* only if "eol" included */ | |
5848 | 8718 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL) |
7 | 8719 vim_beep(); |
8720 else | |
8721 curwin->w_cursor.col = temp; | |
8722 } | |
8723 else if (del_char(FALSE) == FAIL) /* delete char under cursor */ | |
8724 vim_beep(); | |
8725 did_ai = FALSE; | |
8726 #ifdef FEAT_SMARTINDENT | |
8727 did_si = FALSE; | |
8728 can_si = FALSE; | |
8729 can_si_back = FALSE; | |
8730 #endif | |
8731 AppendCharToRedobuff(K_DEL); | |
8732 } | |
8733 | |
1460 | 8734 static void ins_bs_one __ARGS((colnr_T *vcolp)); |
8735 | |
8736 /* | |
8737 * Delete one character for ins_bs(). | |
8738 */ | |
8739 static void | |
8740 ins_bs_one(vcolp) | |
8741 colnr_T *vcolp; | |
8742 { | |
8743 dec_cursor(); | |
8744 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL); | |
8745 if (State & REPLACE_FLAG) | |
8746 { | |
8747 /* Don't delete characters before the insert point when in | |
8748 * Replace mode */ | |
8749 if (curwin->w_cursor.lnum != Insstart.lnum | |
8750 || curwin->w_cursor.col >= Insstart.col) | |
1782 | 8751 replace_do_bs(-1); |
1460 | 8752 } |
8753 else | |
8754 (void)del_char(FALSE); | |
8755 } | |
8756 | |
7 | 8757 /* |
8758 * Handle Backspace, delete-word and delete-line in Insert mode. | |
8759 * Return TRUE when backspace was actually used. | |
8760 */ | |
8761 static int | |
8762 ins_bs(c, mode, inserted_space_p) | |
8763 int c; | |
8764 int mode; | |
8765 int *inserted_space_p; | |
8766 { | |
8767 linenr_T lnum; | |
8768 int cc; | |
8769 int temp = 0; /* init for GCC */ | |
1872 | 8770 colnr_T save_col; |
7 | 8771 colnr_T mincol; |
8772 int did_backspace = FALSE; | |
8773 int in_indent; | |
8774 int oldState; | |
8775 #ifdef FEAT_MBYTE | |
714 | 8776 int cpc[MAX_MCO]; /* composing characters */ |
7 | 8777 #endif |
8778 | |
8779 /* | |
8780 * can't delete anything in an empty file | |
8781 * can't backup past first character in buffer | |
8782 * can't backup past starting point unless 'backspace' > 1 | |
8783 * can backup to a previous line if 'backspace' == 0 | |
8784 */ | |
8785 if ( bufempty() | |
8786 || ( | |
8787 #ifdef FEAT_RIGHTLEFT | |
8788 !revins_on && | |
8789 #endif | |
8790 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0) | |
8791 || (!can_bs(BS_START) | |
8792 && (arrow_used | |
5852 | 8793 || (curwin->w_cursor.lnum == Insstart_orig.lnum |
8794 && curwin->w_cursor.col <= Insstart_orig.col))) | |
7 | 8795 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0 |
8796 && curwin->w_cursor.col <= ai_col) | |
8797 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0)))) | |
8798 { | |
8799 vim_beep(); | |
8800 return FALSE; | |
8801 } | |
8802 | |
8803 if (stop_arrow() == FAIL) | |
8804 return FALSE; | |
8805 in_indent = inindent(0); | |
8806 #ifdef FEAT_CINDENT | |
8807 if (in_indent) | |
8808 can_cindent = FALSE; | |
8809 #endif | |
8810 #ifdef FEAT_COMMENTS | |
8811 end_comment_pending = NUL; /* After BS, don't auto-end comment */ | |
8812 #endif | |
8813 #ifdef FEAT_RIGHTLEFT | |
8814 if (revins_on) /* put cursor after last inserted char */ | |
8815 inc_cursor(); | |
8816 #endif | |
8817 | |
8818 #ifdef FEAT_VIRTUALEDIT | |
8819 /* Virtualedit: | |
8820 * BACKSPACE_CHAR eats a virtual space | |
8821 * BACKSPACE_WORD eats all coladd | |
8822 * BACKSPACE_LINE eats all coladd and keeps going | |
8823 */ | |
8824 if (curwin->w_cursor.coladd > 0) | |
8825 { | |
8826 if (mode == BACKSPACE_CHAR) | |
8827 { | |
8828 --curwin->w_cursor.coladd; | |
8829 return TRUE; | |
8830 } | |
8831 if (mode == BACKSPACE_WORD) | |
8832 { | |
8833 curwin->w_cursor.coladd = 0; | |
8834 return TRUE; | |
8835 } | |
8836 curwin->w_cursor.coladd = 0; | |
8837 } | |
8838 #endif | |
8839 | |
8840 /* | |
8841 * delete newline! | |
8842 */ | |
8843 if (curwin->w_cursor.col == 0) | |
8844 { | |
5852 | 8845 lnum = Insstart_orig.lnum; |
8846 if (curwin->w_cursor.lnum == lnum | |
7 | 8847 #ifdef FEAT_RIGHTLEFT |
8848 || revins_on | |
8849 #endif | |
8850 ) | |
8851 { | |
8852 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2), | |
8853 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL) | |
8854 return FALSE; | |
5852 | 8855 --Insstart_orig.lnum; |
8856 Insstart_orig.col = MAXCOL; | |
6085 | 8857 Insstart = Insstart_orig; |
7 | 8858 } |
8859 /* | |
8860 * In replace mode: | |
8861 * cc < 0: NL was inserted, delete it | |
8862 * cc >= 0: NL was replaced, put original characters back | |
8863 */ | |
8864 cc = -1; | |
8865 if (State & REPLACE_FLAG) | |
8866 cc = replace_pop(); /* returns -1 if NL was inserted */ | |
8867 /* | |
8868 * In replace mode, in the line we started replacing, we only move the | |
8869 * cursor. | |
8870 */ | |
8871 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum) | |
8872 { | |
8873 dec_cursor(); | |
8874 } | |
8875 else | |
8876 { | |
8877 #ifdef FEAT_VREPLACE | |
8878 if (!(State & VREPLACE_FLAG) | |
8879 || curwin->w_cursor.lnum > orig_line_count) | |
8880 #endif | |
8881 { | |
8882 temp = gchar_cursor(); /* remember current char */ | |
8883 --curwin->w_cursor.lnum; | |
278 | 8884 |
8885 /* When "aw" is in 'formatoptions' we must delete the space at | |
8886 * the end of the line, otherwise the line will be broken | |
8887 * again when auto-formatting. */ | |
8888 if (has_format_option(FO_AUTO) | |
8889 && has_format_option(FO_WHITE_PAR)) | |
8890 { | |
8891 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, | |
8892 TRUE); | |
8893 int len; | |
8894 | |
835 | 8895 len = (int)STRLEN(ptr); |
278 | 8896 if (len > 0 && ptr[len - 1] == ' ') |
8897 ptr[len - 1] = NUL; | |
8898 } | |
8899 | |
5848 | 8900 (void)do_join(2, FALSE, FALSE, FALSE, FALSE); |
7 | 8901 if (temp == NUL && gchar_cursor() != NUL) |
8902 inc_cursor(); | |
8903 } | |
8904 #ifdef FEAT_VREPLACE | |
8905 else | |
8906 dec_cursor(); | |
8907 #endif | |
8908 | |
8909 /* | |
8910 * In REPLACE mode we have to put back the text that was replaced | |
8911 * by the NL. On the replace stack is first a NUL-terminated | |
8912 * sequence of characters that were deleted and then the | |
8913 * characters that NL replaced. | |
8914 */ | |
8915 if (State & REPLACE_FLAG) | |
8916 { | |
8917 /* | |
8918 * Do the next ins_char() in NORMAL state, to | |
8919 * prevent ins_char() from replacing characters and | |
8920 * avoiding showmatch(). | |
8921 */ | |
8922 oldState = State; | |
8923 State = NORMAL; | |
8924 /* | |
8925 * restore characters (blanks) deleted after cursor | |
8926 */ | |
8927 while (cc > 0) | |
8928 { | |
1872 | 8929 save_col = curwin->w_cursor.col; |
7 | 8930 #ifdef FEAT_MBYTE |
8931 mb_replace_pop_ins(cc); | |
8932 #else | |
8933 ins_char(cc); | |
8934 #endif | |
1872 | 8935 curwin->w_cursor.col = save_col; |
7 | 8936 cc = replace_pop(); |
8937 } | |
8938 /* restore the characters that NL replaced */ | |
8939 replace_pop_ins(); | |
8940 State = oldState; | |
8941 } | |
8942 } | |
8943 did_ai = FALSE; | |
8944 } | |
8945 else | |
8946 { | |
8947 /* | |
8948 * Delete character(s) before the cursor. | |
8949 */ | |
8950 #ifdef FEAT_RIGHTLEFT | |
8951 if (revins_on) /* put cursor on last inserted char */ | |
8952 dec_cursor(); | |
8953 #endif | |
8954 mincol = 0; | |
8955 /* keep indent */ | |
1085 | 8956 if (mode == BACKSPACE_LINE |
8957 && (curbuf->b_p_ai | |
8958 #ifdef FEAT_CINDENT | |
2004 | 8959 || cindent_on() |
1085 | 8960 #endif |
8961 ) | |
7 | 8962 #ifdef FEAT_RIGHTLEFT |
8963 && !revins_on | |
8964 #endif | |
8965 ) | |
8966 { | |
1872 | 8967 save_col = curwin->w_cursor.col; |
7 | 8968 beginline(BL_WHITE); |
1988 | 8969 if (curwin->w_cursor.col < save_col) |
7 | 8970 mincol = curwin->w_cursor.col; |
1872 | 8971 curwin->w_cursor.col = save_col; |
7 | 8972 } |
8973 | |
8974 /* | |
8975 * Handle deleting one 'shiftwidth' or 'softtabstop'. | |
8976 */ | |
8977 if ( mode == BACKSPACE_CHAR | |
8978 && ((p_sta && in_indent) | |
3873 | 8979 || (get_sts_value() != 0 |
1497 | 8980 && curwin->w_cursor.col > 0 |
7 | 8981 && (*(ml_get_cursor() - 1) == TAB |
8982 || (*(ml_get_cursor() - 1) == ' ' | |
8983 && (!*inserted_space_p | |
8984 || arrow_used)))))) | |
8985 { | |
8986 int ts; | |
8987 colnr_T vcol; | |
8988 colnr_T want_vcol; | |
1460 | 8989 colnr_T start_vcol; |
7 | 8990 |
8991 *inserted_space_p = FALSE; | |
648 | 8992 if (p_sta && in_indent) |
5438 | 8993 ts = (int)get_sw_value(curbuf); |
7 | 8994 else |
3873 | 8995 ts = (int)get_sts_value(); |
7 | 8996 /* Compute the virtual column where we want to be. Since |
8997 * 'showbreak' may get in the way, need to get the last column of | |
8998 * the previous character. */ | |
8999 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL); | |
1460 | 9000 start_vcol = vcol; |
7 | 9001 dec_cursor(); |
9002 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol); | |
9003 inc_cursor(); | |
9004 want_vcol = (want_vcol / ts) * ts; | |
9005 | |
9006 /* delete characters until we are at or before want_vcol */ | |
9007 while (vcol > want_vcol | |
9008 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc))) | |
1460 | 9009 ins_bs_one(&vcol); |
7 | 9010 |
9011 /* insert extra spaces until we are at want_vcol */ | |
9012 while (vcol < want_vcol) | |
9013 { | |
9014 /* Remember the first char we inserted */ | |
5852 | 9015 if (curwin->w_cursor.lnum == Insstart_orig.lnum |
9016 && curwin->w_cursor.col < Insstart_orig.col) | |
9017 Insstart_orig.col = curwin->w_cursor.col; | |
7 | 9018 |
9019 #ifdef FEAT_VREPLACE | |
9020 if (State & VREPLACE_FLAG) | |
9021 ins_char(' '); | |
9022 else | |
9023 #endif | |
9024 { | |
9025 ins_str((char_u *)" "); | |
1460 | 9026 if ((State & REPLACE_FLAG)) |
9027 replace_push(NUL); | |
7 | 9028 } |
9029 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL); | |
9030 } | |
1460 | 9031 |
9032 /* If we are now back where we started delete one character. Can | |
9033 * happen when using 'sts' and 'linebreak'. */ | |
9034 if (vcol >= start_vcol) | |
9035 ins_bs_one(&vcol); | |
7 | 9036 } |
9037 | |
9038 /* | |
9039 * Delete upto starting point, start of line or previous word. | |
9040 */ | |
9041 else do | |
9042 { | |
9043 #ifdef FEAT_RIGHTLEFT | |
9044 if (!revins_on) /* put cursor on char to be deleted */ | |
9045 #endif | |
9046 dec_cursor(); | |
9047 | |
9048 /* start of word? */ | |
9049 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor())) | |
9050 { | |
9051 mode = BACKSPACE_WORD_NOT_SPACE; | |
9052 temp = vim_iswordc(gchar_cursor()); | |
9053 } | |
9054 /* end of word? */ | |
9055 else if (mode == BACKSPACE_WORD_NOT_SPACE | |
9056 && (vim_isspace(cc = gchar_cursor()) | |
9057 || vim_iswordc(cc) != temp)) | |
9058 { | |
9059 #ifdef FEAT_RIGHTLEFT | |
9060 if (!revins_on) | |
9061 #endif | |
9062 inc_cursor(); | |
9063 #ifdef FEAT_RIGHTLEFT | |
9064 else if (State & REPLACE_FLAG) | |
9065 dec_cursor(); | |
9066 #endif | |
9067 break; | |
9068 } | |
9069 if (State & REPLACE_FLAG) | |
1782 | 9070 replace_do_bs(-1); |
7 | 9071 else |
9072 { | |
9073 #ifdef FEAT_MBYTE | |
9074 if (enc_utf8 && p_deco) | |
714 | 9075 (void)utfc_ptr2char(ml_get_cursor(), cpc); |
7 | 9076 #endif |
9077 (void)del_char(FALSE); | |
9078 #ifdef FEAT_MBYTE | |
9079 /* | |
714 | 9080 * If there are combining characters and 'delcombine' is set |
9081 * move the cursor back. Don't back up before the base | |
7 | 9082 * character. |
9083 */ | |
714 | 9084 if (enc_utf8 && p_deco && cpc[0] != NUL) |
7 | 9085 inc_cursor(); |
9086 #endif | |
9087 #ifdef FEAT_RIGHTLEFT | |
9088 if (revins_chars) | |
9089 { | |
9090 revins_chars--; | |
9091 revins_legal++; | |
9092 } | |
9093 if (revins_on && gchar_cursor() == NUL) | |
9094 break; | |
9095 #endif | |
9096 } | |
9097 /* Just a single backspace?: */ | |
9098 if (mode == BACKSPACE_CHAR) | |
9099 break; | |
9100 } while ( | |
9101 #ifdef FEAT_RIGHTLEFT | |
9102 revins_on || | |
9103 #endif | |
9104 (curwin->w_cursor.col > mincol | |
5852 | 9105 && (curwin->w_cursor.lnum != Insstart_orig.lnum |
9106 || curwin->w_cursor.col != Insstart_orig.col))); | |
7 | 9107 did_backspace = TRUE; |
9108 } | |
9109 #ifdef FEAT_SMARTINDENT | |
9110 did_si = FALSE; | |
9111 can_si = FALSE; | |
9112 can_si_back = FALSE; | |
9113 #endif | |
9114 if (curwin->w_cursor.col <= 1) | |
9115 did_ai = FALSE; | |
9116 /* | |
9117 * It's a little strange to put backspaces into the redo | |
9118 * buffer, but it makes auto-indent a lot easier to deal | |
9119 * with. | |
9120 */ | |
9121 AppendCharToRedobuff(c); | |
9122 | |
9123 /* If deleted before the insertion point, adjust it */ | |
5852 | 9124 if (curwin->w_cursor.lnum == Insstart_orig.lnum |
9125 && curwin->w_cursor.col < Insstart_orig.col) | |
9126 Insstart_orig.col = curwin->w_cursor.col; | |
7 | 9127 |
9128 /* vi behaviour: the cursor moves backward but the character that | |
9129 * was there remains visible | |
9130 * Vim behaviour: the cursor moves backward and the character that | |
9131 * was there is erased from the screen. | |
9132 * We can emulate the vi behaviour by pretending there is a dollar | |
9133 * displayed even when there isn't. | |
9134 * --pkv Sun Jan 19 01:56:40 EST 2003 */ | |
3318 | 9135 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1) |
7 | 9136 dollar_vcol = curwin->w_virtcol; |
9137 | |
1514 | 9138 #ifdef FEAT_FOLDING |
9139 /* When deleting a char the cursor line must never be in a closed fold. | |
9140 * E.g., when 'foldmethod' is indent and deleting the first non-white | |
9141 * char before a Tab. */ | |
9142 if (did_backspace) | |
9143 foldOpenCursor(); | |
9144 #endif | |
9145 | |
7 | 9146 return did_backspace; |
9147 } | |
9148 | |
9149 #ifdef FEAT_MOUSE | |
9150 static void | |
9151 ins_mouse(c) | |
9152 int c; | |
9153 { | |
9154 pos_T tpos; | |
840 | 9155 win_T *old_curwin = curwin; |
7 | 9156 |
9157 # ifdef FEAT_GUI | |
9158 /* When GUI is active, also move/paste when 'mouse' is empty */ | |
9159 if (!gui.in_use) | |
9160 # endif | |
9161 if (!mouse_has(MOUSE_INSERT)) | |
9162 return; | |
9163 | |
9164 undisplay_dollar(); | |
9165 tpos = curwin->w_cursor; | |
9166 if (do_mouse(NULL, c, BACKWARD, 1L, 0)) | |
9167 { | |
840 | 9168 #ifdef FEAT_WINDOWS |
9169 win_T *new_curwin = curwin; | |
9170 | |
9171 if (curwin != old_curwin && win_valid(old_curwin)) | |
9172 { | |
9173 /* Mouse took us to another window. We need to go back to the | |
9174 * previous one to stop insert there properly. */ | |
9175 curwin = old_curwin; | |
9176 curbuf = curwin->w_buffer; | |
9177 } | |
9178 #endif | |
9179 start_arrow(curwin == old_curwin ? &tpos : NULL); | |
9180 #ifdef FEAT_WINDOWS | |
9181 if (curwin != new_curwin && win_valid(new_curwin)) | |
9182 { | |
9183 curwin = new_curwin; | |
9184 curbuf = curwin->w_buffer; | |
9185 } | |
9186 #endif | |
7 | 9187 # ifdef FEAT_CINDENT |
9188 can_cindent = TRUE; | |
9189 # endif | |
9190 } | |
9191 | |
9192 #ifdef FEAT_WINDOWS | |
9193 /* redraw status lines (in case another window became active) */ | |
9194 redraw_statuslines(); | |
9195 #endif | |
9196 } | |
9197 | |
9198 static void | |
2409
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2378
diff
changeset
|
9199 ins_mousescroll(dir) |
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2378
diff
changeset
|
9200 int dir; |
7 | 9201 { |
9202 pos_T tpos; | |
1434 | 9203 # if defined(FEAT_WINDOWS) |
9204 win_T *old_curwin = curwin; | |
9205 # endif | |
9206 # ifdef FEAT_INS_EXPAND | |
9207 int did_scroll = FALSE; | |
7 | 9208 # endif |
9209 | |
9210 tpos = curwin->w_cursor; | |
9211 | |
4162 | 9212 # ifdef FEAT_WINDOWS |
9213 if (mouse_row >= 0 && mouse_col >= 0) | |
7 | 9214 { |
9215 int row, col; | |
9216 | |
9217 row = mouse_row; | |
9218 col = mouse_col; | |
9219 | |
9220 /* find the window at the pointer coordinates */ | |
9221 curwin = mouse_find_win(&row, &col); | |
9222 curbuf = curwin->w_buffer; | |
9223 } | |
9224 if (curwin == old_curwin) | |
9225 # endif | |
9226 undisplay_dollar(); | |
9227 | |
1434 | 9228 # ifdef FEAT_INS_EXPAND |
9229 /* Don't scroll the window in which completion is being done. */ | |
9230 if (!pum_visible() | |
9231 # if defined(FEAT_WINDOWS) | |
9232 || curwin != old_curwin | |
9233 # endif | |
9234 ) | |
9235 # endif | |
9236 { | |
2409
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2378
diff
changeset
|
9237 if (dir == MSCR_DOWN || dir == MSCR_UP) |
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2378
diff
changeset
|
9238 { |
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2378
diff
changeset
|
9239 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)) |
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2378
diff
changeset
|
9240 scroll_redraw(dir, |
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2378
diff
changeset
|
9241 (long)(curwin->w_botline - curwin->w_topline)); |
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2378
diff
changeset
|
9242 else |
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2378
diff
changeset
|
9243 scroll_redraw(dir, 3L); |
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2378
diff
changeset
|
9244 } |
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2378
diff
changeset
|
9245 #ifdef FEAT_GUI |
1434 | 9246 else |
2409
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2378
diff
changeset
|
9247 { |
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2378
diff
changeset
|
9248 int val, step = 6; |
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2378
diff
changeset
|
9249 |
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2378
diff
changeset
|
9250 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)) |
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2378
diff
changeset
|
9251 step = W_WIDTH(curwin); |
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2378
diff
changeset
|
9252 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step); |
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2378
diff
changeset
|
9253 if (val < 0) |
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2378
diff
changeset
|
9254 val = 0; |
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2378
diff
changeset
|
9255 gui_do_horiz_scroll(val, TRUE); |
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2378
diff
changeset
|
9256 } |
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2378
diff
changeset
|
9257 #endif |
1434 | 9258 # ifdef FEAT_INS_EXPAND |
9259 did_scroll = TRUE; | |
9260 # endif | |
9261 } | |
7 | 9262 |
4162 | 9263 # ifdef FEAT_WINDOWS |
7 | 9264 curwin->w_redr_status = TRUE; |
9265 | |
9266 curwin = old_curwin; | |
9267 curbuf = curwin->w_buffer; | |
9268 # endif | |
9269 | |
1434 | 9270 # ifdef FEAT_INS_EXPAND |
9271 /* The popup menu may overlay the window, need to redraw it. | |
9272 * TODO: Would be more efficient to only redraw the windows that are | |
9273 * overlapped by the popup menu. */ | |
9274 if (pum_visible() && did_scroll) | |
9275 { | |
9276 redraw_all_later(NOT_VALID); | |
9277 ins_compl_show_pum(); | |
9278 } | |
9279 # endif | |
9280 | |
7 | 9281 if (!equalpos(curwin->w_cursor, tpos)) |
9282 { | |
9283 start_arrow(&tpos); | |
9284 # ifdef FEAT_CINDENT | |
9285 can_cindent = TRUE; | |
9286 # endif | |
9287 } | |
9288 } | |
9289 #endif | |
9290 | |
692 | 9291 #if defined(FEAT_GUI_TABLINE) || defined(PROTO) |
724 | 9292 static void |
692 | 9293 ins_tabline(c) |
9294 int c; | |
9295 { | |
9296 /* We will be leaving the current window, unless closing another tab. */ | |
9297 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE | |
9298 || (current_tab != 0 && current_tab != tabpage_index(curtab))) | |
9299 { | |
9300 undisplay_dollar(); | |
9301 start_arrow(&curwin->w_cursor); | |
9302 # ifdef FEAT_CINDENT | |
9303 can_cindent = TRUE; | |
9304 # endif | |
9305 } | |
9306 | |
9307 if (c == K_TABLINE) | |
9308 goto_tabpage(current_tab); | |
9309 else | |
846 | 9310 { |
692 | 9311 handle_tabmenu(); |
846 | 9312 redraw_statuslines(); /* will redraw the tabline when needed */ |
9313 } | |
692 | 9314 } |
9315 #endif | |
9316 | |
9317 #if defined(FEAT_GUI) || defined(PROTO) | |
7 | 9318 void |
9319 ins_scroll() | |
9320 { | |
9321 pos_T tpos; | |
9322 | |
9323 undisplay_dollar(); | |
9324 tpos = curwin->w_cursor; | |
9325 if (gui_do_scroll()) | |
9326 { | |
9327 start_arrow(&tpos); | |
9328 # ifdef FEAT_CINDENT | |
9329 can_cindent = TRUE; | |
9330 # endif | |
9331 } | |
9332 } | |
9333 | |
9334 void | |
9335 ins_horscroll() | |
9336 { | |
9337 pos_T tpos; | |
9338 | |
9339 undisplay_dollar(); | |
9340 tpos = curwin->w_cursor; | |
2409
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2378
diff
changeset
|
9341 if (gui_do_horiz_scroll(scrollbar_value, FALSE)) |
7 | 9342 { |
9343 start_arrow(&tpos); | |
9344 # ifdef FEAT_CINDENT | |
9345 can_cindent = TRUE; | |
9346 # endif | |
9347 } | |
9348 } | |
9349 #endif | |
9350 | |
9351 static void | |
9352 ins_left() | |
9353 { | |
9354 pos_T tpos; | |
9355 | |
9356 #ifdef FEAT_FOLDING | |
9357 if ((fdo_flags & FDO_HOR) && KeyTyped) | |
9358 foldOpenCursor(); | |
9359 #endif | |
9360 undisplay_dollar(); | |
9361 tpos = curwin->w_cursor; | |
9362 if (oneleft() == OK) | |
9363 { | |
941 | 9364 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
9365 /* Only call start_arrow() when not busy with preediting, it will | |
9366 * break undo. K_LEFT is inserted in im_correct_cursor(). */ | |
9367 if (!im_is_preediting()) | |
9368 #endif | |
9369 start_arrow(&tpos); | |
7 | 9370 #ifdef FEAT_RIGHTLEFT |
9371 /* If exit reversed string, position is fixed */ | |
9372 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol) | |
9373 revins_legal++; | |
9374 revins_chars++; | |
9375 #endif | |
9376 } | |
9377 | |
9378 /* | |
9379 * if 'whichwrap' set for cursor in insert mode may go to | |
9380 * previous line | |
9381 */ | |
9382 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1) | |
9383 { | |
9384 start_arrow(&tpos); | |
9385 --(curwin->w_cursor.lnum); | |
9386 coladvance((colnr_T)MAXCOL); | |
9387 curwin->w_set_curswant = TRUE; /* so we stay at the end */ | |
9388 } | |
9389 else | |
9390 vim_beep(); | |
9391 } | |
9392 | |
9393 static void | |
9394 ins_home(c) | |
9395 int c; | |
9396 { | |
9397 pos_T tpos; | |
9398 | |
9399 #ifdef FEAT_FOLDING | |
9400 if ((fdo_flags & FDO_HOR) && KeyTyped) | |
9401 foldOpenCursor(); | |
9402 #endif | |
9403 undisplay_dollar(); | |
9404 tpos = curwin->w_cursor; | |
9405 if (c == K_C_HOME) | |
9406 curwin->w_cursor.lnum = 1; | |
9407 curwin->w_cursor.col = 0; | |
9408 #ifdef FEAT_VIRTUALEDIT | |
9409 curwin->w_cursor.coladd = 0; | |
9410 #endif | |
9411 curwin->w_curswant = 0; | |
9412 start_arrow(&tpos); | |
9413 } | |
9414 | |
9415 static void | |
9416 ins_end(c) | |
9417 int c; | |
9418 { | |
9419 pos_T tpos; | |
9420 | |
9421 #ifdef FEAT_FOLDING | |
9422 if ((fdo_flags & FDO_HOR) && KeyTyped) | |
9423 foldOpenCursor(); | |
9424 #endif | |
9425 undisplay_dollar(); | |
9426 tpos = curwin->w_cursor; | |
9427 if (c == K_C_END) | |
9428 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; | |
9429 coladvance((colnr_T)MAXCOL); | |
9430 curwin->w_curswant = MAXCOL; | |
9431 | |
9432 start_arrow(&tpos); | |
9433 } | |
9434 | |
9435 static void | |
9436 ins_s_left() | |
9437 { | |
9438 #ifdef FEAT_FOLDING | |
9439 if ((fdo_flags & FDO_HOR) && KeyTyped) | |
9440 foldOpenCursor(); | |
9441 #endif | |
9442 undisplay_dollar(); | |
9443 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0) | |
9444 { | |
9445 start_arrow(&curwin->w_cursor); | |
9446 (void)bck_word(1L, FALSE, FALSE); | |
9447 curwin->w_set_curswant = TRUE; | |
9448 } | |
9449 else | |
9450 vim_beep(); | |
9451 } | |
9452 | |
9453 static void | |
9454 ins_right() | |
9455 { | |
9456 #ifdef FEAT_FOLDING | |
9457 if ((fdo_flags & FDO_HOR) && KeyTyped) | |
9458 foldOpenCursor(); | |
9459 #endif | |
9460 undisplay_dollar(); | |
1877 | 9461 if (gchar_cursor() != NUL |
9462 #ifdef FEAT_VIRTUALEDIT | |
9463 || virtual_active() | |
9464 #endif | |
7 | 9465 ) |
9466 { | |
9467 start_arrow(&curwin->w_cursor); | |
9468 curwin->w_set_curswant = TRUE; | |
9469 #ifdef FEAT_VIRTUALEDIT | |
9470 if (virtual_active()) | |
9471 oneright(); | |
9472 else | |
9473 #endif | |
9474 { | |
9475 #ifdef FEAT_MBYTE | |
9476 if (has_mbyte) | |
474 | 9477 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor()); |
7 | 9478 else |
9479 #endif | |
9480 ++curwin->w_cursor.col; | |
9481 } | |
9482 | |
9483 #ifdef FEAT_RIGHTLEFT | |
9484 revins_legal++; | |
9485 if (revins_chars) | |
9486 revins_chars--; | |
9487 #endif | |
9488 } | |
9489 /* if 'whichwrap' set for cursor in insert mode, may move the | |
9490 * cursor to the next line */ | |
9491 else if (vim_strchr(p_ww, ']') != NULL | |
9492 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count) | |
9493 { | |
9494 start_arrow(&curwin->w_cursor); | |
9495 curwin->w_set_curswant = TRUE; | |
9496 ++curwin->w_cursor.lnum; | |
9497 curwin->w_cursor.col = 0; | |
9498 } | |
9499 else | |
9500 vim_beep(); | |
9501 } | |
9502 | |
9503 static void | |
9504 ins_s_right() | |
9505 { | |
9506 #ifdef FEAT_FOLDING | |
9507 if ((fdo_flags & FDO_HOR) && KeyTyped) | |
9508 foldOpenCursor(); | |
9509 #endif | |
9510 undisplay_dollar(); | |
9511 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count | |
9512 || gchar_cursor() != NUL) | |
9513 { | |
9514 start_arrow(&curwin->w_cursor); | |
9515 (void)fwd_word(1L, FALSE, 0); | |
9516 curwin->w_set_curswant = TRUE; | |
9517 } | |
9518 else | |
9519 vim_beep(); | |
9520 } | |
9521 | |
9522 static void | |
9523 ins_up(startcol) | |
9524 int startcol; /* when TRUE move to Insstart.col */ | |
9525 { | |
9526 pos_T tpos; | |
9527 linenr_T old_topline = curwin->w_topline; | |
9528 #ifdef FEAT_DIFF | |
9529 int old_topfill = curwin->w_topfill; | |
9530 #endif | |
9531 | |
9532 undisplay_dollar(); | |
9533 tpos = curwin->w_cursor; | |
9534 if (cursor_up(1L, TRUE) == OK) | |
9535 { | |
9536 if (startcol) | |
9537 coladvance(getvcol_nolist(&Insstart)); | |
9538 if (old_topline != curwin->w_topline | |
9539 #ifdef FEAT_DIFF | |
9540 || old_topfill != curwin->w_topfill | |
9541 #endif | |
9542 ) | |
9543 redraw_later(VALID); | |
9544 start_arrow(&tpos); | |
9545 #ifdef FEAT_CINDENT | |
9546 can_cindent = TRUE; | |
9547 #endif | |
9548 } | |
9549 else | |
9550 vim_beep(); | |
9551 } | |
9552 | |
9553 static void | |
9554 ins_pageup() | |
9555 { | |
9556 pos_T tpos; | |
9557 | |
9558 undisplay_dollar(); | |
828 | 9559 |
9560 #ifdef FEAT_WINDOWS | |
9561 if (mod_mask & MOD_MASK_CTRL) | |
9562 { | |
9563 /* <C-PageUp>: tab page back */ | |
1013 | 9564 if (first_tabpage->tp_next != NULL) |
9565 { | |
9566 start_arrow(&curwin->w_cursor); | |
9567 goto_tabpage(-1); | |
9568 } | |
828 | 9569 return; |
9570 } | |
9571 #endif | |
9572 | |
7 | 9573 tpos = curwin->w_cursor; |
9574 if (onepage(BACKWARD, 1L) == OK) | |
9575 { | |
9576 start_arrow(&tpos); | |
9577 #ifdef FEAT_CINDENT | |
9578 can_cindent = TRUE; | |
9579 #endif | |
9580 } | |
9581 else | |
9582 vim_beep(); | |
9583 } | |
9584 | |
9585 static void | |
9586 ins_down(startcol) | |
9587 int startcol; /* when TRUE move to Insstart.col */ | |
9588 { | |
9589 pos_T tpos; | |
9590 linenr_T old_topline = curwin->w_topline; | |
9591 #ifdef FEAT_DIFF | |
9592 int old_topfill = curwin->w_topfill; | |
9593 #endif | |
9594 | |
9595 undisplay_dollar(); | |
9596 tpos = curwin->w_cursor; | |
9597 if (cursor_down(1L, TRUE) == OK) | |
9598 { | |
9599 if (startcol) | |
9600 coladvance(getvcol_nolist(&Insstart)); | |
9601 if (old_topline != curwin->w_topline | |
9602 #ifdef FEAT_DIFF | |
9603 || old_topfill != curwin->w_topfill | |
9604 #endif | |
9605 ) | |
9606 redraw_later(VALID); | |
9607 start_arrow(&tpos); | |
9608 #ifdef FEAT_CINDENT | |
9609 can_cindent = TRUE; | |
9610 #endif | |
9611 } | |
9612 else | |
9613 vim_beep(); | |
9614 } | |
9615 | |
9616 static void | |
9617 ins_pagedown() | |
9618 { | |
9619 pos_T tpos; | |
9620 | |
9621 undisplay_dollar(); | |
828 | 9622 |
9623 #ifdef FEAT_WINDOWS | |
9624 if (mod_mask & MOD_MASK_CTRL) | |
9625 { | |
9626 /* <C-PageDown>: tab page forward */ | |
1013 | 9627 if (first_tabpage->tp_next != NULL) |
9628 { | |
9629 start_arrow(&curwin->w_cursor); | |
9630 goto_tabpage(0); | |
9631 } | |
828 | 9632 return; |
9633 } | |
9634 #endif | |
9635 | |
7 | 9636 tpos = curwin->w_cursor; |
9637 if (onepage(FORWARD, 1L) == OK) | |
9638 { | |
9639 start_arrow(&tpos); | |
9640 #ifdef FEAT_CINDENT | |
9641 can_cindent = TRUE; | |
9642 #endif | |
9643 } | |
9644 else | |
9645 vim_beep(); | |
9646 } | |
9647 | |
9648 #ifdef FEAT_DND | |
9649 static void | |
9650 ins_drop() | |
9651 { | |
9652 do_put('~', BACKWARD, 1L, PUT_CURSEND); | |
9653 } | |
9654 #endif | |
9655 | |
9656 /* | |
9657 * Handle TAB in Insert or Replace mode. | |
9658 * Return TRUE when the TAB needs to be inserted like a normal character. | |
9659 */ | |
9660 static int | |
9661 ins_tab() | |
9662 { | |
9663 int ind; | |
9664 int i; | |
9665 int temp; | |
9666 | |
9667 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum) | |
9668 Insstart_blank_vcol = get_nolist_virtcol(); | |
9669 if (echeck_abbr(TAB + ABBR_OFF)) | |
9670 return FALSE; | |
9671 | |
9672 ind = inindent(0); | |
9673 #ifdef FEAT_CINDENT | |
9674 if (ind) | |
9675 can_cindent = FALSE; | |
9676 #endif | |
9677 | |
9678 /* | |
9679 * When nothing special, insert TAB like a normal character | |
9680 */ | |
9681 if (!curbuf->b_p_et | |
5438 | 9682 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf)) |
3873 | 9683 && get_sts_value() == 0) |
7 | 9684 return TRUE; |
9685 | |
9686 if (stop_arrow() == FAIL) | |
9687 return TRUE; | |
9688 | |
9689 did_ai = FALSE; | |
9690 #ifdef FEAT_SMARTINDENT | |
9691 did_si = FALSE; | |
9692 can_si = FALSE; | |
9693 can_si_back = FALSE; | |
9694 #endif | |
9695 AppendToRedobuff((char_u *)"\t"); | |
9696 | |
9697 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */ | |
5438 | 9698 temp = (int)get_sw_value(curbuf); |
3873 | 9699 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */ |
9700 temp = (int)get_sts_value(); | |
7 | 9701 else /* otherwise use 'tabstop' */ |
9702 temp = (int)curbuf->b_p_ts; | |
9703 temp -= get_nolist_virtcol() % temp; | |
9704 | |
9705 /* | |
9706 * Insert the first space with ins_char(). It will delete one char in | |
9707 * replace mode. Insert the rest with ins_str(); it will not delete any | |
9708 * chars. For VREPLACE mode, we use ins_char() for all characters. | |
9709 */ | |
9710 ins_char(' '); | |
9711 while (--temp > 0) | |
9712 { | |
9713 #ifdef FEAT_VREPLACE | |
9714 if (State & VREPLACE_FLAG) | |
9715 ins_char(' '); | |
9716 else | |
9717 #endif | |
9718 { | |
9719 ins_str((char_u *)" "); | |
9720 if (State & REPLACE_FLAG) /* no char replaced */ | |
9721 replace_push(NUL); | |
9722 } | |
9723 } | |
9724 | |
9725 /* | |
9726 * When 'expandtab' not set: Replace spaces by TABs where possible. | |
9727 */ | |
3873 | 9728 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind))) |
7 | 9729 { |
9730 char_u *ptr; | |
9731 #ifdef FEAT_VREPLACE | |
9732 char_u *saved_line = NULL; /* init for GCC */ | |
9733 pos_T pos; | |
9734 #endif | |
9735 pos_T fpos; | |
9736 pos_T *cursor; | |
9737 colnr_T want_vcol, vcol; | |
9738 int change_col = -1; | |
9739 int save_list = curwin->w_p_list; | |
9740 | |
9741 /* | |
9742 * Get the current line. For VREPLACE mode, don't make real changes | |
9743 * yet, just work on a copy of the line. | |
9744 */ | |
9745 #ifdef FEAT_VREPLACE | |
9746 if (State & VREPLACE_FLAG) | |
9747 { | |
9748 pos = curwin->w_cursor; | |
9749 cursor = &pos; | |
9750 saved_line = vim_strsave(ml_get_curline()); | |
9751 if (saved_line == NULL) | |
9752 return FALSE; | |
9753 ptr = saved_line + pos.col; | |
9754 } | |
9755 else | |
9756 #endif | |
9757 { | |
9758 ptr = ml_get_cursor(); | |
9759 cursor = &curwin->w_cursor; | |
9760 } | |
9761 | |
9762 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */ | |
9763 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL) | |
9764 curwin->w_p_list = FALSE; | |
9765 | |
9766 /* Find first white before the cursor */ | |
9767 fpos = curwin->w_cursor; | |
9768 while (fpos.col > 0 && vim_iswhite(ptr[-1])) | |
9769 { | |
9770 --fpos.col; | |
9771 --ptr; | |
9772 } | |
9773 | |
9774 /* In Replace mode, don't change characters before the insert point. */ | |
9775 if ((State & REPLACE_FLAG) | |
9776 && fpos.lnum == Insstart.lnum | |
9777 && fpos.col < Insstart.col) | |
9778 { | |
9779 ptr += Insstart.col - fpos.col; | |
9780 fpos.col = Insstart.col; | |
9781 } | |
9782 | |
9783 /* compute virtual column numbers of first white and cursor */ | |
9784 getvcol(curwin, &fpos, &vcol, NULL, NULL); | |
9785 getvcol(curwin, cursor, &want_vcol, NULL, NULL); | |
9786 | |
5995 | 9787 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak' |
9788 * and 'linebreak' adding extra virtual columns. */ | |
7 | 9789 while (vim_iswhite(*ptr)) |
9790 { | |
5995 | 9791 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol); |
7 | 9792 if (vcol + i > want_vcol) |
9793 break; | |
9794 if (*ptr != TAB) | |
9795 { | |
9796 *ptr = TAB; | |
9797 if (change_col < 0) | |
9798 { | |
9799 change_col = fpos.col; /* Column of first change */ | |
9800 /* May have to adjust Insstart */ | |
9801 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col) | |
9802 Insstart.col = fpos.col; | |
9803 } | |
9804 } | |
9805 ++fpos.col; | |
9806 ++ptr; | |
9807 vcol += i; | |
9808 } | |
9809 | |
9810 if (change_col >= 0) | |
9811 { | |
9812 int repl_off = 0; | |
5995 | 9813 char_u *line = ptr; |
7 | 9814 |
9815 /* Skip over the spaces we need. */ | |
9816 while (vcol < want_vcol && *ptr == ' ') | |
9817 { | |
5995 | 9818 vcol += lbr_chartabsize(line, ptr, vcol); |
7 | 9819 ++ptr; |
9820 ++repl_off; | |
9821 } | |
9822 if (vcol > want_vcol) | |
9823 { | |
9824 /* Must have a char with 'showbreak' just before it. */ | |
9825 --ptr; | |
9826 --repl_off; | |
9827 } | |
9828 fpos.col += repl_off; | |
9829 | |
9830 /* Delete following spaces. */ | |
9831 i = cursor->col - fpos.col; | |
9832 if (i > 0) | |
9833 { | |
1622 | 9834 STRMOVE(ptr, ptr + i); |
7 | 9835 /* correct replace stack. */ |
9836 if ((State & REPLACE_FLAG) | |
9837 #ifdef FEAT_VREPLACE | |
9838 && !(State & VREPLACE_FLAG) | |
9839 #endif | |
9840 ) | |
9841 for (temp = i; --temp >= 0; ) | |
9842 replace_join(repl_off); | |
9843 } | |
33 | 9844 #ifdef FEAT_NETBEANS_INTG |
2210 | 9845 if (netbeans_active()) |
33 | 9846 { |
2209
d0ddf7ba1630
Included the patch to support netbeans in a terminal.
Bram Moolenaar <bram@vim.org>
parents:
2178
diff
changeset
|
9847 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1)); |
33 | 9848 netbeans_inserted(curbuf, fpos.lnum, cursor->col, |
9849 (char_u *)"\t", 1); | |
9850 } | |
9851 #endif | |
7 | 9852 cursor->col -= i; |
9853 | |
9854 #ifdef FEAT_VREPLACE | |
9855 /* | |
9856 * In VREPLACE mode, we haven't changed anything yet. Do it now by | |
9857 * backspacing over the changed spacing and then inserting the new | |
9858 * spacing. | |
9859 */ | |
9860 if (State & VREPLACE_FLAG) | |
9861 { | |
9862 /* Backspace from real cursor to change_col */ | |
9863 backspace_until_column(change_col); | |
9864 | |
9865 /* Insert each char in saved_line from changed_col to | |
9866 * ptr-cursor */ | |
9867 ins_bytes_len(saved_line + change_col, | |
9868 cursor->col - change_col); | |
9869 } | |
9870 #endif | |
9871 } | |
9872 | |
9873 #ifdef FEAT_VREPLACE | |
9874 if (State & VREPLACE_FLAG) | |
9875 vim_free(saved_line); | |
9876 #endif | |
9877 curwin->w_p_list = save_list; | |
9878 } | |
9879 | |
9880 return FALSE; | |
9881 } | |
9882 | |
9883 /* | |
9884 * Handle CR or NL in insert mode. | |
9885 * Return TRUE when out of memory or can't undo. | |
9886 */ | |
9887 static int | |
9888 ins_eol(c) | |
9889 int c; | |
9890 { | |
9891 int i; | |
9892 | |
9893 if (echeck_abbr(c + ABBR_OFF)) | |
9894 return FALSE; | |
9895 if (stop_arrow() == FAIL) | |
9896 return TRUE; | |
9897 undisplay_dollar(); | |
9898 | |
9899 /* | |
9900 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the | |
9901 * character under the cursor. Only push a NUL on the replace stack, | |
9902 * nothing to put back when the NL is deleted. | |
9903 */ | |
9904 if ((State & REPLACE_FLAG) | |
9905 #ifdef FEAT_VREPLACE | |
9906 && !(State & VREPLACE_FLAG) | |
9907 #endif | |
9908 ) | |
9909 replace_push(NUL); | |
9910 | |
9911 /* | |
9912 * In VREPLACE mode, a NL replaces the rest of the line, and starts | |
9913 * replacing the next line, so we push all of the characters left on the | |
9914 * line onto the replace stack. This is not done here though, it is done | |
9915 * in open_line(). | |
9916 */ | |
9917 | |
844 | 9918 #ifdef FEAT_VIRTUALEDIT |
9919 /* Put cursor on NUL if on the last char and coladd is 1 (happens after | |
9920 * CTRL-O). */ | |
9921 if (virtual_active() && curwin->w_cursor.coladd > 0) | |
9922 coladvance(getviscol()); | |
9923 #endif | |
9924 | |
7 | 9925 #ifdef FEAT_RIGHTLEFT |
9926 # ifdef FEAT_FKMAP | |
9927 if (p_altkeymap && p_fkmap) | |
9928 fkmap(NL); | |
9929 # endif | |
9930 /* NL in reverse insert will always start in the end of | |
9931 * current line. */ | |
9932 if (revins_on) | |
9933 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor()); | |
9934 #endif | |
9935 | |
9936 AppendToRedobuff(NL_STR); | |
9937 i = open_line(FORWARD, | |
9938 #ifdef FEAT_COMMENTS | |
9939 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM : | |
9940 #endif | |
9941 0, old_indent); | |
9942 old_indent = 0; | |
9943 #ifdef FEAT_CINDENT | |
9944 can_cindent = TRUE; | |
9945 #endif | |
1032 | 9946 #ifdef FEAT_FOLDING |
9947 /* When inserting a line the cursor line must never be in a closed fold. */ | |
9948 foldOpenCursor(); | |
9949 #endif | |
7 | 9950 |
9951 return (!i); | |
9952 } | |
9953 | |
9954 #ifdef FEAT_DIGRAPHS | |
9955 /* | |
9956 * Handle digraph in insert mode. | |
9957 * Returns character still to be inserted, or NUL when nothing remaining to be | |
9958 * done. | |
9959 */ | |
9960 static int | |
9961 ins_digraph() | |
9962 { | |
9963 int c; | |
9964 int cc; | |
2811 | 9965 int did_putchar = FALSE; |
7 | 9966 |
9967 pc_status = PC_STATUS_UNSET; | |
9968 if (redrawing() && !char_avail()) | |
9969 { | |
9970 /* may need to redraw when no more chars available now */ | |
661 | 9971 ins_redraw(FALSE); |
7 | 9972 |
9973 edit_putchar('?', TRUE); | |
2811 | 9974 did_putchar = TRUE; |
7 | 9975 #ifdef FEAT_CMDL_INFO |
9976 add_to_showcmd_c(Ctrl_K); | |
9977 #endif | |
9978 } | |
9979 | |
9980 #ifdef USE_ON_FLY_SCROLL | |
9981 dont_scroll = TRUE; /* disallow scrolling here */ | |
9982 #endif | |
9983 | |
9984 /* don't map the digraph chars. This also prevents the | |
9985 * mode message to be deleted when ESC is hit */ | |
9986 ++no_mapping; | |
9987 ++allow_keys; | |
1389 | 9988 c = plain_vgetc(); |
7 | 9989 --no_mapping; |
9990 --allow_keys; | |
2811 | 9991 if (did_putchar) |
9992 /* when the line fits in 'columns' the '?' is at the start of the next | |
9993 * line and will not be removed by the redraw */ | |
9994 edit_unputchar(); | |
2319
c79ccf947487
Fix: When entering a digraph or special character after a line that fits the
Bram Moolenaar <bram@vim.org>
parents:
2297
diff
changeset
|
9995 |
7 | 9996 if (IS_SPECIAL(c) || mod_mask) /* special key */ |
9997 { | |
9998 #ifdef FEAT_CMDL_INFO | |
9999 clear_showcmd(); | |
10000 #endif | |
10001 insert_special(c, TRUE, FALSE); | |
10002 return NUL; | |
10003 } | |
10004 if (c != ESC) | |
10005 { | |
2811 | 10006 did_putchar = FALSE; |
7 | 10007 if (redrawing() && !char_avail()) |
10008 { | |
10009 /* may need to redraw when no more chars available now */ | |
661 | 10010 ins_redraw(FALSE); |
7 | 10011 |
10012 if (char2cells(c) == 1) | |
10013 { | |
661 | 10014 ins_redraw(FALSE); |
7 | 10015 edit_putchar(c, TRUE); |
2811 | 10016 did_putchar = TRUE; |
7 | 10017 } |
10018 #ifdef FEAT_CMDL_INFO | |
10019 add_to_showcmd_c(c); | |
10020 #endif | |
10021 } | |
10022 ++no_mapping; | |
10023 ++allow_keys; | |
1389 | 10024 cc = plain_vgetc(); |
7 | 10025 --no_mapping; |
10026 --allow_keys; | |
2811 | 10027 if (did_putchar) |
10028 /* when the line fits in 'columns' the '?' is at the start of the | |
10029 * next line and will not be removed by a redraw */ | |
10030 edit_unputchar(); | |
7 | 10031 if (cc != ESC) |
10032 { | |
10033 AppendToRedobuff((char_u *)CTRL_V_STR); | |
10034 c = getdigraph(c, cc, TRUE); | |
10035 #ifdef FEAT_CMDL_INFO | |
10036 clear_showcmd(); | |
10037 #endif | |
10038 return c; | |
10039 } | |
10040 } | |
10041 #ifdef FEAT_CMDL_INFO | |
10042 clear_showcmd(); | |
10043 #endif | |
10044 return NUL; | |
10045 } | |
10046 #endif | |
10047 | |
10048 /* | |
10049 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line. | |
10050 * Returns the char to be inserted, or NUL if none found. | |
10051 */ | |
3501 | 10052 int |
7 | 10053 ins_copychar(lnum) |
10054 linenr_T lnum; | |
10055 { | |
10056 int c; | |
10057 int temp; | |
10058 char_u *ptr, *prev_ptr; | |
5995 | 10059 char_u *line; |
7 | 10060 |
10061 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) | |
10062 { | |
10063 vim_beep(); | |
10064 return NUL; | |
10065 } | |
10066 | |
10067 /* try to advance to the cursor column */ | |
10068 temp = 0; | |
5995 | 10069 line = ptr = ml_get(lnum); |
7 | 10070 prev_ptr = ptr; |
10071 validate_virtcol(); | |
10072 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL) | |
10073 { | |
10074 prev_ptr = ptr; | |
5995 | 10075 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp); |
7 | 10076 } |
10077 if ((colnr_T)temp > curwin->w_virtcol) | |
10078 ptr = prev_ptr; | |
10079 | |
10080 #ifdef FEAT_MBYTE | |
10081 c = (*mb_ptr2char)(ptr); | |
10082 #else | |
10083 c = *ptr; | |
10084 #endif | |
10085 if (c == NUL) | |
10086 vim_beep(); | |
10087 return c; | |
10088 } | |
10089 | |
449 | 10090 /* |
10091 * CTRL-Y or CTRL-E typed in Insert mode. | |
10092 */ | |
10093 static int | |
10094 ins_ctrl_ey(tc) | |
10095 int tc; | |
10096 { | |
10097 int c = tc; | |
10098 | |
10099 #ifdef FEAT_INS_EXPAND | |
10100 if (ctrl_x_mode == CTRL_X_SCROLL) | |
10101 { | |
10102 if (c == Ctrl_Y) | |
10103 scrolldown_clamp(); | |
10104 else | |
10105 scrollup_clamp(); | |
10106 redraw_later(VALID); | |
10107 } | |
10108 else | |
10109 #endif | |
10110 { | |
10111 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1)); | |
10112 if (c != NUL) | |
10113 { | |
10114 long tw_save; | |
10115 | |
10116 /* The character must be taken literally, insert like it | |
10117 * was typed after a CTRL-V, and pretend 'textwidth' | |
10118 * wasn't set. Digits, 'o' and 'x' are special after a | |
10119 * CTRL-V, don't use it for these. */ | |
10120 if (c < 256 && !isalnum(c)) | |
10121 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */ | |
10122 tw_save = curbuf->b_p_tw; | |
10123 curbuf->b_p_tw = -1; | |
10124 insert_special(c, TRUE, FALSE); | |
10125 curbuf->b_p_tw = tw_save; | |
10126 #ifdef FEAT_RIGHTLEFT | |
10127 revins_chars++; | |
10128 revins_legal++; | |
10129 #endif | |
10130 c = Ctrl_V; /* pretend CTRL-V is last character */ | |
10131 auto_format(FALSE, TRUE); | |
10132 } | |
10133 } | |
10134 return c; | |
10135 } | |
10136 | |
7 | 10137 #ifdef FEAT_SMARTINDENT |
10138 /* | |
10139 * Try to do some very smart auto-indenting. | |
10140 * Used when inserting a "normal" character. | |
10141 */ | |
10142 static void | |
10143 ins_try_si(c) | |
10144 int c; | |
10145 { | |
10146 pos_T *pos, old_pos; | |
10147 char_u *ptr; | |
10148 int i; | |
10149 int temp; | |
10150 | |
10151 /* | |
10152 * do some very smart indenting when entering '{' or '}' | |
10153 */ | |
10154 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}')) | |
10155 { | |
10156 /* | |
10157 * for '}' set indent equal to indent of line containing matching '{' | |
10158 */ | |
10159 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL) | |
10160 { | |
10161 old_pos = curwin->w_cursor; | |
10162 /* | |
10163 * If the matching '{' has a ')' immediately before it (ignoring | |
10164 * white-space), then line up with the start of the line | |
10165 * containing the matching '(' if there is one. This handles the | |
10166 * case where an "if (..\n..) {" statement continues over multiple | |
10167 * lines -- webb | |
10168 */ | |
10169 ptr = ml_get(pos->lnum); | |
10170 i = pos->col; | |
10171 if (i > 0) /* skip blanks before '{' */ | |
10172 while (--i > 0 && vim_iswhite(ptr[i])) | |
10173 ; | |
10174 curwin->w_cursor.lnum = pos->lnum; | |
10175 curwin->w_cursor.col = i; | |
10176 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL) | |
10177 curwin->w_cursor = *pos; | |
10178 i = get_indent(); | |
10179 curwin->w_cursor = old_pos; | |
10180 #ifdef FEAT_VREPLACE | |
10181 if (State & VREPLACE_FLAG) | |
1516 | 10182 change_indent(INDENT_SET, i, FALSE, NUL, TRUE); |
7 | 10183 else |
10184 #endif | |
10185 (void)set_indent(i, SIN_CHANGED); | |
10186 } | |
10187 else if (curwin->w_cursor.col > 0) | |
10188 { | |
10189 /* | |
10190 * when inserting '{' after "O" reduce indent, but not | |
10191 * more than indent of previous line | |
10192 */ | |
10193 temp = TRUE; | |
10194 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1) | |
10195 { | |
10196 old_pos = curwin->w_cursor; | |
10197 i = get_indent(); | |
10198 while (curwin->w_cursor.lnum > 1) | |
10199 { | |
10200 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum))); | |
10201 | |
10202 /* ignore empty lines and lines starting with '#'. */ | |
10203 if (*ptr != '#' && *ptr != NUL) | |
10204 break; | |
10205 } | |
10206 if (get_indent() >= i) | |
10207 temp = FALSE; | |
10208 curwin->w_cursor = old_pos; | |
10209 } | |
10210 if (temp) | |
1516 | 10211 shift_line(TRUE, FALSE, 1, TRUE); |
7 | 10212 } |
10213 } | |
10214 | |
10215 /* | |
10216 * set indent of '#' always to 0 | |
10217 */ | |
10218 if (curwin->w_cursor.col > 0 && can_si && c == '#') | |
10219 { | |
10220 /* remember current indent for next line */ | |
10221 old_indent = get_indent(); | |
10222 (void)set_indent(0, SIN_CHANGED); | |
10223 } | |
10224 | |
10225 /* Adjust ai_col, the char at this position can be deleted. */ | |
10226 if (ai_col > curwin->w_cursor.col) | |
10227 ai_col = curwin->w_cursor.col; | |
10228 } | |
10229 #endif | |
10230 | |
10231 /* | |
10232 * Get the value that w_virtcol would have when 'list' is off. | |
10233 * Unless 'cpo' contains the 'L' flag. | |
10234 */ | |
10235 static colnr_T | |
10236 get_nolist_virtcol() | |
10237 { | |
10238 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL) | |
10239 return getvcol_nolist(&curwin->w_cursor); | |
10240 validate_virtcol(); | |
10241 return curwin->w_virtcol; | |
10242 } | |
3390 | 10243 |
10244 #ifdef FEAT_AUTOCMD | |
10245 /* | |
10246 * Handle the InsertCharPre autocommand. | |
10247 * "c" is the character that was typed. | |
10248 * Return a pointer to allocated memory with the replacement string. | |
10249 * Return NULL to continue inserting "c". | |
10250 */ | |
10251 static char_u * | |
10252 do_insert_char_pre(c) | |
10253 int c; | |
10254 { | |
3547 | 10255 char_u *res; |
10256 char_u buf[MB_MAXBYTES + 1]; | |
3390 | 10257 |
10258 /* Return quickly when there is nothing to do. */ | |
10259 if (!has_insertcharpre()) | |
10260 return NULL; | |
10261 | |
3547 | 10262 #ifdef FEAT_MBYTE |
10263 if (has_mbyte) | |
10264 buf[(*mb_char2bytes)(c, buf)] = NUL; | |
10265 else | |
10266 #endif | |
10267 { | |
10268 buf[0] = c; | |
10269 buf[1] = NUL; | |
10270 } | |
10271 | |
3390 | 10272 /* Lock the text to avoid weird things from happening. */ |
10273 ++textlock; | |
3547 | 10274 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */ |
10275 | |
10276 res = NULL; | |
3390 | 10277 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf)) |
3547 | 10278 { |
10279 /* Get the value of v:char. It may be empty or more than one | |
10280 * character. Only use it when changed, otherwise continue with the | |
10281 * original character to avoid breaking autoindent. */ | |
10282 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0) | |
10283 res = vim_strsave(get_vim_var_str(VV_CHAR)); | |
10284 } | |
3390 | 10285 |
10286 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */ | |
10287 --textlock; | |
10288 | |
10289 return res; | |
10290 } | |
10291 #endif |