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