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