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