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