blob: 27ea9ac8fba7239b6b3e0feda167fb528bc0cd14 [file] [log] [blame]
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001/* vi:set ts=8 sts=4 sw=4 noet:
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 * change.c: functions related to changing text
12 */
13
14#include "vim.h"
15
16/*
17 * If the file is readonly, give a warning message with the first change.
18 * Don't do this for autocommands.
19 * Doesn't use emsg(), because it flushes the macro buffer.
20 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
21 * will be TRUE.
22 * "col" is the column for the message; non-zero when in insert mode and
23 * 'showmode' is on.
24 * Careful: may trigger autocommands that reload the buffer.
25 */
26 void
27change_warning(int col)
28{
29 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
30
31 if (curbuf->b_did_warn == FALSE
32 && curbufIsChanged() == 0
33 && !autocmd_busy
34 && curbuf->b_p_ro)
35 {
36 ++curbuf_lock;
37 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
38 --curbuf_lock;
39 if (!curbuf->b_p_ro)
40 return;
41
42 // Do what msg() does, but with a column offset if the warning should
43 // be after the mode message.
44 msg_start();
45 if (msg_row == Rows - 1)
46 msg_col = col;
47 msg_source(HL_ATTR(HLF_W));
48 msg_puts_attr(_(w_readonly), HL_ATTR(HLF_W) | MSG_HIST);
49#ifdef FEAT_EVAL
50 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
51#endif
52 msg_clr_eos();
53 (void)msg_end();
54 if (msg_silent == 0 && !silent_mode
55#ifdef FEAT_EVAL
56 && time_for_testing != 1
57#endif
58 )
59 {
60 out_flush();
61 ui_delay(1000L, TRUE); // give the user time to think about it
62 }
63 curbuf->b_did_warn = TRUE;
64 redraw_cmdline = FALSE; // don't redraw and erase the message
65 if (msg_row < Rows - 1)
66 showmode();
67 }
68}
69
70/*
71 * Call this function when something in the current buffer is changed.
72 *
73 * Most often called through changed_bytes() and changed_lines(), which also
74 * mark the area of the display to be redrawn.
75 *
76 * Careful: may trigger autocommands that reload the buffer.
77 */
78 void
79changed(void)
80{
81#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
82 if (p_imst == IM_ON_THE_SPOT)
83 {
84 // The text of the preediting area is inserted, but this doesn't
85 // mean a change of the buffer yet. That is delayed until the
86 // text is committed. (this means preedit becomes empty)
87 if (im_is_preediting() && !xim_changed_while_preediting)
88 return;
89 xim_changed_while_preediting = FALSE;
90 }
91#endif
92
93 if (!curbuf->b_changed)
94 {
95 int save_msg_scroll = msg_scroll;
96
97 // Give a warning about changing a read-only file. This may also
98 // check-out the file, thus change "curbuf"!
99 change_warning(0);
100
101 // Create a swap file if that is wanted.
102 // Don't do this for "nofile" and "nowrite" buffer types.
103 if (curbuf->b_may_swap
104#ifdef FEAT_QUICKFIX
105 && !bt_dontwrite(curbuf)
106#endif
107 )
108 {
109 int save_need_wait_return = need_wait_return;
110
111 need_wait_return = FALSE;
112 ml_open_file(curbuf);
113
114 // The ml_open_file() can cause an ATTENTION message.
115 // Wait two seconds, to make sure the user reads this unexpected
116 // message. Since we could be anywhere, call wait_return() now,
117 // and don't let the emsg() set msg_scroll.
118 if (need_wait_return && emsg_silent == 0)
119 {
120 out_flush();
121 ui_delay(2000L, TRUE);
122 wait_return(TRUE);
123 msg_scroll = save_msg_scroll;
124 }
125 else
126 need_wait_return = save_need_wait_return;
127 }
128 changed_internal();
129 }
130 ++CHANGEDTICK(curbuf);
131
132#ifdef FEAT_SEARCH_EXTRA
133 // If a pattern is highlighted, the position may now be invalid.
134 highlight_match = FALSE;
135#endif
136}
137
138/*
139 * Internal part of changed(), no user interaction.
140 * Also used for recovery.
141 */
142 void
143changed_internal(void)
144{
145 curbuf->b_changed = TRUE;
146 ml_setflags(curbuf);
147 check_status(curbuf);
148 redraw_tabline = TRUE;
149#ifdef FEAT_TITLE
150 need_maketitle = TRUE; // set window title later
151#endif
152}
153
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200154#ifdef FEAT_EVAL
155static list_T *recorded_changes = NULL;
156static long next_listener_id = 0;
157
158/*
159 * Record a change for listeners added with listener_add().
160 */
161 static void
162may_record_change(
163 linenr_T lnum,
164 colnr_T col,
165 linenr_T lnume,
166 long xtra)
167{
168 dict_T *dict;
169
170 if (curbuf->b_listener == NULL)
171 return;
172 if (recorded_changes == NULL)
173 {
174 recorded_changes = list_alloc();
175 if (recorded_changes == NULL) // out of memory
176 return;
177 ++recorded_changes->lv_refcount;
178 recorded_changes->lv_lock = VAR_FIXED;
179 }
180
181 dict = dict_alloc();
182 if (dict == NULL)
183 return;
184 dict_add_number(dict, "lnum", (varnumber_T)lnum);
185 dict_add_number(dict, "end", (varnumber_T)lnume);
186 dict_add_number(dict, "added", (varnumber_T)xtra);
Bram Moolenaara3347722019-05-11 21:14:24 +0200187 dict_add_number(dict, "col", (varnumber_T)col + 1);
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200188
189 list_append_dict(recorded_changes, dict);
190}
191
192/*
193 * listener_add() function
194 */
195 void
196f_listener_add(typval_T *argvars, typval_T *rettv)
197{
198 char_u *callback;
199 partial_T *partial;
200 listener_T *lnr;
Bram Moolenaara3347722019-05-11 21:14:24 +0200201 buf_T *buf = curbuf;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200202
203 callback = get_callback(&argvars[0], &partial);
204 if (callback == NULL)
205 return;
206
Bram Moolenaara3347722019-05-11 21:14:24 +0200207 if (argvars[1].v_type != VAR_UNKNOWN)
208 {
209 buf = get_buf_arg(&argvars[1]);
210 if (buf == NULL)
211 return;
212 }
213
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200214 lnr = (listener_T *)alloc_clear((sizeof(listener_T)));
215 if (lnr == NULL)
216 {
217 free_callback(callback, partial);
218 return;
219 }
Bram Moolenaara3347722019-05-11 21:14:24 +0200220 lnr->lr_next = buf->b_listener;
221 buf->b_listener = lnr;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200222
223 if (partial == NULL)
224 lnr->lr_callback = vim_strsave(callback);
225 else
226 lnr->lr_callback = callback; // pointer into the partial
227 lnr->lr_partial = partial;
228
229 lnr->lr_id = ++next_listener_id;
230 rettv->vval.v_number = lnr->lr_id;
231}
232
233/*
234 * listener_remove() function
235 */
236 void
237f_listener_remove(typval_T *argvars, typval_T *rettv UNUSED)
238{
239 listener_T *lnr;
240 listener_T *next;
241 listener_T *prev = NULL;
242 int id = tv_get_number(argvars);
Bram Moolenaara3347722019-05-11 21:14:24 +0200243 buf_T *buf;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200244
Bram Moolenaara3347722019-05-11 21:14:24 +0200245 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
246 for (lnr = buf->b_listener; lnr != NULL; lnr = next)
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200247 {
Bram Moolenaara3347722019-05-11 21:14:24 +0200248 next = lnr->lr_next;
249 if (lnr->lr_id == id)
250 {
251 if (prev != NULL)
252 prev->lr_next = lnr->lr_next;
253 else
254 buf->b_listener = lnr->lr_next;
255 free_callback(lnr->lr_callback, lnr->lr_partial);
256 vim_free(lnr);
257 }
258 prev = lnr;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200259 }
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200260}
261
262/*
263 * Called when a sequence of changes is done: invoke listeners added with
264 * listener_add().
265 */
266 void
267invoke_listeners(void)
268{
269 listener_T *lnr;
270 typval_T rettv;
271 int dummy;
272 typval_T argv[2];
273
274 if (recorded_changes == NULL) // nothing changed
275 return;
276 argv[0].v_type = VAR_LIST;
277 argv[0].vval.v_list = recorded_changes;
278
279 for (lnr = curbuf->b_listener; lnr != NULL; lnr = lnr->lr_next)
280 {
281 call_func(lnr->lr_callback, -1, &rettv,
282 1, argv, NULL, 0L, 0L, &dummy, TRUE, lnr->lr_partial, NULL);
283 clear_tv(&rettv);
284 }
285
286 list_unref(recorded_changes);
287 recorded_changes = NULL;
288}
289#endif
290
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200291/*
292 * Common code for when a change was made.
293 * See changed_lines() for the arguments.
294 * Careful: may trigger autocommands that reload the buffer.
295 */
296 static void
297changed_common(
298 linenr_T lnum,
299 colnr_T col,
300 linenr_T lnume,
301 long xtra)
302{
303 win_T *wp;
304 tabpage_T *tp;
305 int i;
306#ifdef FEAT_JUMPLIST
307 int cols;
308 pos_T *p;
309 int add;
310#endif
311
312 // mark the buffer as modified
313 changed();
314
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200315#ifdef FEAT_EVAL
316 may_record_change(lnum, col, lnume, xtra);
317#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200318#ifdef FEAT_DIFF
319 if (curwin->w_p_diff && diff_internal())
320 curtab->tp_diff_update = TRUE;
321#endif
322
323 // set the '. mark
324 if (!cmdmod.keepjumps)
325 {
326 curbuf->b_last_change.lnum = lnum;
327 curbuf->b_last_change.col = col;
328
329#ifdef FEAT_JUMPLIST
330 // Create a new entry if a new undo-able change was started or we
331 // don't have an entry yet.
332 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
333 {
334 if (curbuf->b_changelistlen == 0)
335 add = TRUE;
336 else
337 {
338 // Don't create a new entry when the line number is the same
339 // as the last one and the column is not too far away. Avoids
340 // creating many entries for typing "xxxxx".
341 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
342 if (p->lnum != lnum)
343 add = TRUE;
344 else
345 {
346 cols = comp_textwidth(FALSE);
347 if (cols == 0)
348 cols = 79;
349 add = (p->col + cols < col || col + cols < p->col);
350 }
351 }
352 if (add)
353 {
354 // This is the first of a new sequence of undo-able changes
355 // and it's at some distance of the last change. Use a new
356 // position in the changelist.
357 curbuf->b_new_change = FALSE;
358
359 if (curbuf->b_changelistlen == JUMPLISTSIZE)
360 {
361 // changelist is full: remove oldest entry
362 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
363 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
364 sizeof(pos_T) * (JUMPLISTSIZE - 1));
365 FOR_ALL_TAB_WINDOWS(tp, wp)
366 {
367 // Correct position in changelist for other windows on
368 // this buffer.
369 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
370 --wp->w_changelistidx;
371 }
372 }
373 FOR_ALL_TAB_WINDOWS(tp, wp)
374 {
375 // For other windows, if the position in the changelist is
376 // at the end it stays at the end.
377 if (wp->w_buffer == curbuf
378 && wp->w_changelistidx == curbuf->b_changelistlen)
379 ++wp->w_changelistidx;
380 }
381 ++curbuf->b_changelistlen;
382 }
383 }
384 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
385 curbuf->b_last_change;
386 // The current window is always after the last change, so that "g,"
387 // takes you back to it.
388 curwin->w_changelistidx = curbuf->b_changelistlen;
389#endif
390 }
391
392 FOR_ALL_TAB_WINDOWS(tp, wp)
393 {
394 if (wp->w_buffer == curbuf)
395 {
396 // Mark this window to be redrawn later.
397 if (wp->w_redr_type < VALID)
398 wp->w_redr_type = VALID;
399
400 // Check if a change in the buffer has invalidated the cached
401 // values for the cursor.
402#ifdef FEAT_FOLDING
403 // Update the folds for this window. Can't postpone this, because
404 // a following operator might work on the whole fold: ">>dd".
405 foldUpdate(wp, lnum, lnume + xtra - 1);
406
407 // The change may cause lines above or below the change to become
408 // included in a fold. Set lnum/lnume to the first/last line that
409 // might be displayed differently.
410 // Set w_cline_folded here as an efficient way to update it when
411 // inserting lines just above a closed fold.
412 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
413 if (wp->w_cursor.lnum == lnum)
414 wp->w_cline_folded = i;
415 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
416 if (wp->w_cursor.lnum == lnume)
417 wp->w_cline_folded = i;
418
419 // If the changed line is in a range of previously folded lines,
420 // compare with the first line in that range.
421 if (wp->w_cursor.lnum <= lnum)
422 {
423 i = find_wl_entry(wp, lnum);
424 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
425 changed_line_abv_curs_win(wp);
426 }
427#endif
428
429 if (wp->w_cursor.lnum > lnum)
430 changed_line_abv_curs_win(wp);
431 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
432 changed_cline_bef_curs_win(wp);
433 if (wp->w_botline >= lnum)
434 {
435 // Assume that botline doesn't change (inserted lines make
436 // other lines scroll down below botline).
437 approximate_botline_win(wp);
438 }
439
440 // Check if any w_lines[] entries have become invalid.
441 // For entries below the change: Correct the lnums for
442 // inserted/deleted lines. Makes it possible to stop displaying
443 // after the change.
444 for (i = 0; i < wp->w_lines_valid; ++i)
445 if (wp->w_lines[i].wl_valid)
446 {
447 if (wp->w_lines[i].wl_lnum >= lnum)
448 {
449 if (wp->w_lines[i].wl_lnum < lnume)
450 {
451 // line included in change
452 wp->w_lines[i].wl_valid = FALSE;
453 }
454 else if (xtra != 0)
455 {
456 // line below change
457 wp->w_lines[i].wl_lnum += xtra;
458#ifdef FEAT_FOLDING
459 wp->w_lines[i].wl_lastlnum += xtra;
460#endif
461 }
462 }
463#ifdef FEAT_FOLDING
464 else if (wp->w_lines[i].wl_lastlnum >= lnum)
465 {
466 // change somewhere inside this range of folded lines,
467 // may need to be redrawn
468 wp->w_lines[i].wl_valid = FALSE;
469 }
470#endif
471 }
472
473#ifdef FEAT_FOLDING
474 // Take care of side effects for setting w_topline when folds have
475 // changed. Esp. when the buffer was changed in another window.
476 if (hasAnyFolding(wp))
477 set_topline(wp, wp->w_topline);
478#endif
479 // relative numbering may require updating more
480 if (wp->w_p_rnu)
481 redraw_win_later(wp, SOME_VALID);
482 }
483 }
484
485 // Call update_screen() later, which checks out what needs to be redrawn,
486 // since it notices b_mod_set and then uses b_mod_*.
487 if (must_redraw < VALID)
488 must_redraw = VALID;
489
490 // when the cursor line is changed always trigger CursorMoved
491 if (lnum <= curwin->w_cursor.lnum
492 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
493 last_cursormoved.lnum = 0;
494}
495
496 static void
497changedOneline(buf_T *buf, linenr_T lnum)
498{
499 if (buf->b_mod_set)
500 {
501 // find the maximum area that must be redisplayed
502 if (lnum < buf->b_mod_top)
503 buf->b_mod_top = lnum;
504 else if (lnum >= buf->b_mod_bot)
505 buf->b_mod_bot = lnum + 1;
506 }
507 else
508 {
509 // set the area that must be redisplayed to one line
510 buf->b_mod_set = TRUE;
511 buf->b_mod_top = lnum;
512 buf->b_mod_bot = lnum + 1;
513 buf->b_mod_xlines = 0;
514 }
515}
516
517/*
518 * Changed bytes within a single line for the current buffer.
519 * - marks the windows on this buffer to be redisplayed
520 * - marks the buffer changed by calling changed()
521 * - invalidates cached values
522 * Careful: may trigger autocommands that reload the buffer.
523 */
524 void
525changed_bytes(linenr_T lnum, colnr_T col)
526{
527 changedOneline(curbuf, lnum);
528 changed_common(lnum, col, lnum + 1, 0L);
529
530#ifdef FEAT_DIFF
531 // Diff highlighting in other diff windows may need to be updated too.
532 if (curwin->w_p_diff)
533 {
534 win_T *wp;
535 linenr_T wlnum;
536
537 FOR_ALL_WINDOWS(wp)
538 if (wp->w_p_diff && wp != curwin)
539 {
540 redraw_win_later(wp, VALID);
541 wlnum = diff_lnum_win(lnum, wp);
542 if (wlnum > 0)
543 changedOneline(wp->w_buffer, wlnum);
544 }
545 }
546#endif
547}
548
549/*
550 * Like changed_bytes() but also adjust text properties for "added" bytes.
551 * When "added" is negative text was deleted.
552 */
553 void
554inserted_bytes(linenr_T lnum, colnr_T col, int added UNUSED)
555{
556 changed_bytes(lnum, col);
557
558#ifdef FEAT_TEXT_PROP
559 if (curbuf->b_has_textprop && added != 0)
560 adjust_prop_columns(lnum, col, added);
561#endif
562}
563
564/*
565 * Appended "count" lines below line "lnum" in the current buffer.
566 * Must be called AFTER the change and after mark_adjust().
567 * Takes care of marking the buffer to be redrawn and sets the changed flag.
568 */
569 void
570appended_lines(linenr_T lnum, long count)
571{
572 changed_lines(lnum + 1, 0, lnum + 1, count);
573}
574
575/*
576 * Like appended_lines(), but adjust marks first.
577 */
578 void
579appended_lines_mark(linenr_T lnum, long count)
580{
581 // Skip mark_adjust when adding a line after the last one, there can't
582 // be marks there. But it's still needed in diff mode.
583 if (lnum + count < curbuf->b_ml.ml_line_count
584#ifdef FEAT_DIFF
585 || curwin->w_p_diff
586#endif
587 )
588 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
589 changed_lines(lnum + 1, 0, lnum + 1, count);
590}
591
592/*
593 * Deleted "count" lines at line "lnum" in the current buffer.
594 * Must be called AFTER the change and after mark_adjust().
595 * Takes care of marking the buffer to be redrawn and sets the changed flag.
596 */
597 void
598deleted_lines(linenr_T lnum, long count)
599{
600 changed_lines(lnum, 0, lnum + count, -count);
601}
602
603/*
604 * Like deleted_lines(), but adjust marks first.
605 * Make sure the cursor is on a valid line before calling, a GUI callback may
606 * be triggered to display the cursor.
607 */
608 void
609deleted_lines_mark(linenr_T lnum, long count)
610{
611 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
612 changed_lines(lnum, 0, lnum + count, -count);
613}
614
615/*
616 * Marks the area to be redrawn after a change.
617 */
618 static void
619changed_lines_buf(
620 buf_T *buf,
621 linenr_T lnum, // first line with change
622 linenr_T lnume, // line below last changed line
623 long xtra) // number of extra lines (negative when deleting)
624{
625 if (buf->b_mod_set)
626 {
627 // find the maximum area that must be redisplayed
628 if (lnum < buf->b_mod_top)
629 buf->b_mod_top = lnum;
630 if (lnum < buf->b_mod_bot)
631 {
632 // adjust old bot position for xtra lines
633 buf->b_mod_bot += xtra;
634 if (buf->b_mod_bot < lnum)
635 buf->b_mod_bot = lnum;
636 }
637 if (lnume + xtra > buf->b_mod_bot)
638 buf->b_mod_bot = lnume + xtra;
639 buf->b_mod_xlines += xtra;
640 }
641 else
642 {
643 // set the area that must be redisplayed
644 buf->b_mod_set = TRUE;
645 buf->b_mod_top = lnum;
646 buf->b_mod_bot = lnume + xtra;
647 buf->b_mod_xlines = xtra;
648 }
649}
650
651/*
652 * Changed lines for the current buffer.
653 * Must be called AFTER the change and after mark_adjust().
654 * - mark the buffer changed by calling changed()
655 * - mark the windows on this buffer to be redisplayed
656 * - invalidate cached values
657 * "lnum" is the first line that needs displaying, "lnume" the first line
658 * below the changed lines (BEFORE the change).
659 * When only inserting lines, "lnum" and "lnume" are equal.
660 * Takes care of calling changed() and updating b_mod_*.
661 * Careful: may trigger autocommands that reload the buffer.
662 */
663 void
664changed_lines(
665 linenr_T lnum, // first line with change
666 colnr_T col, // column in first line with change
667 linenr_T lnume, // line below last changed line
668 long xtra) // number of extra lines (negative when deleting)
669{
670 changed_lines_buf(curbuf, lnum, lnume, xtra);
671
672#ifdef FEAT_DIFF
673 if (xtra == 0 && curwin->w_p_diff && !diff_internal())
674 {
675 // When the number of lines doesn't change then mark_adjust() isn't
676 // called and other diff buffers still need to be marked for
677 // displaying.
678 win_T *wp;
679 linenr_T wlnum;
680
681 FOR_ALL_WINDOWS(wp)
682 if (wp->w_p_diff && wp != curwin)
683 {
684 redraw_win_later(wp, VALID);
685 wlnum = diff_lnum_win(lnum, wp);
686 if (wlnum > 0)
687 changed_lines_buf(wp->w_buffer, wlnum,
688 lnume - lnum + wlnum, 0L);
689 }
690 }
691#endif
692
693 changed_common(lnum, col, lnume, xtra);
694}
695
696/*
697 * Called when the changed flag must be reset for buffer "buf".
698 * When "ff" is TRUE also reset 'fileformat'.
699 */
700 void
701unchanged(buf_T *buf, int ff)
702{
703 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
704 {
705 buf->b_changed = 0;
706 ml_setflags(buf);
707 if (ff)
708 save_file_ff(buf);
709 check_status(buf);
710 redraw_tabline = TRUE;
711#ifdef FEAT_TITLE
712 need_maketitle = TRUE; // set window title later
713#endif
714 }
715 ++CHANGEDTICK(buf);
716#ifdef FEAT_NETBEANS_INTG
717 netbeans_unmodified(buf);
718#endif
719}
720
721/*
722 * Insert string "p" at the cursor position. Stops at a NUL byte.
723 * Handles Replace mode and multi-byte characters.
724 */
725 void
726ins_bytes(char_u *p)
727{
728 ins_bytes_len(p, (int)STRLEN(p));
729}
730
731/*
732 * Insert string "p" with length "len" at the cursor position.
733 * Handles Replace mode and multi-byte characters.
734 */
735 void
736ins_bytes_len(char_u *p, int len)
737{
738 int i;
739 int n;
740
741 if (has_mbyte)
742 for (i = 0; i < len; i += n)
743 {
744 if (enc_utf8)
745 // avoid reading past p[len]
746 n = utfc_ptr2len_len(p + i, len - i);
747 else
748 n = (*mb_ptr2len)(p + i);
749 ins_char_bytes(p + i, n);
750 }
751 else
752 for (i = 0; i < len; ++i)
753 ins_char(p[i]);
754}
755
756/*
757 * Insert or replace a single character at the cursor position.
758 * When in REPLACE or VREPLACE mode, replace any existing character.
759 * Caller must have prepared for undo.
760 * For multi-byte characters we get the whole character, the caller must
761 * convert bytes to a character.
762 */
763 void
764ins_char(int c)
765{
766 char_u buf[MB_MAXBYTES + 1];
767 int n = (*mb_char2bytes)(c, buf);
768
769 // When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
770 // Happens for CTRL-Vu9900.
771 if (buf[0] == 0)
772 buf[0] = '\n';
773
774 ins_char_bytes(buf, n);
775}
776
777 void
778ins_char_bytes(char_u *buf, int charlen)
779{
780 int c = buf[0];
781 int newlen; // nr of bytes inserted
782 int oldlen; // nr of bytes deleted (0 when not replacing)
783 char_u *p;
784 char_u *newp;
785 char_u *oldp;
786 int linelen; // length of old line including NUL
787 colnr_T col;
788 linenr_T lnum = curwin->w_cursor.lnum;
789 int i;
790
791 // Break tabs if needed.
792 if (virtual_active() && curwin->w_cursor.coladd > 0)
793 coladvance_force(getviscol());
794
795 col = curwin->w_cursor.col;
796 oldp = ml_get(lnum);
797 linelen = (int)STRLEN(oldp) + 1;
798
799 // The lengths default to the values for when not replacing.
800 oldlen = 0;
801 newlen = charlen;
802
803 if (State & REPLACE_FLAG)
804 {
805 if (State & VREPLACE_FLAG)
806 {
807 colnr_T new_vcol = 0; // init for GCC
808 colnr_T vcol;
809 int old_list;
810
811 // Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
812 // Returns the old value of list, so when finished,
813 // curwin->w_p_list should be set back to this.
814 old_list = curwin->w_p_list;
815 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
816 curwin->w_p_list = FALSE;
817
818 // In virtual replace mode each character may replace one or more
819 // characters (zero if it's a TAB). Count the number of bytes to
820 // be deleted to make room for the new character, counting screen
821 // cells. May result in adding spaces to fill a gap.
822 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
823 new_vcol = vcol + chartabsize(buf, vcol);
824 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
825 {
826 vcol += chartabsize(oldp + col + oldlen, vcol);
827 // Don't need to remove a TAB that takes us to the right
828 // position.
829 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
830 break;
831 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
832 // Deleted a bit too much, insert spaces.
833 if (vcol > new_vcol)
834 newlen += vcol - new_vcol;
835 }
836 curwin->w_p_list = old_list;
837 }
838 else if (oldp[col] != NUL)
839 {
840 // normal replace
841 oldlen = (*mb_ptr2len)(oldp + col);
842 }
843
844
845 // Push the replaced bytes onto the replace stack, so that they can be
846 // put back when BS is used. The bytes of a multi-byte character are
847 // done the other way around, so that the first byte is popped off
848 // first (it tells the byte length of the character).
849 replace_push(NUL);
850 for (i = 0; i < oldlen; ++i)
851 {
852 if (has_mbyte)
853 i += replace_push_mb(oldp + col + i) - 1;
854 else
855 replace_push(oldp[col + i]);
856 }
857 }
858
859 newp = alloc_check((unsigned)(linelen + newlen - oldlen));
860 if (newp == NULL)
861 return;
862
863 // Copy bytes before the cursor.
864 if (col > 0)
865 mch_memmove(newp, oldp, (size_t)col);
866
867 // Copy bytes after the changed character(s).
868 p = newp + col;
869 if (linelen > col + oldlen)
870 mch_memmove(p + newlen, oldp + col + oldlen,
871 (size_t)(linelen - col - oldlen));
872
873 // Insert or overwrite the new character.
874 mch_memmove(p, buf, charlen);
875 i = charlen;
876
877 // Fill with spaces when necessary.
878 while (i < newlen)
879 p[i++] = ' ';
880
881 // Replace the line in the buffer.
882 ml_replace(lnum, newp, FALSE);
883
884 // mark the buffer as changed and prepare for displaying
885 inserted_bytes(lnum, col, newlen - oldlen);
886
887 // If we're in Insert or Replace mode and 'showmatch' is set, then briefly
888 // show the match for right parens and braces.
889 if (p_sm && (State & INSERT)
890 && msg_silent == 0
891#ifdef FEAT_INS_EXPAND
892 && !ins_compl_active()
893#endif
894 )
895 {
896 if (has_mbyte)
897 showmatch(mb_ptr2char(buf));
898 else
899 showmatch(c);
900 }
901
902#ifdef FEAT_RIGHTLEFT
903 if (!p_ri || (State & REPLACE_FLAG))
904#endif
905 {
906 // Normal insert: move cursor right
907 curwin->w_cursor.col += charlen;
908 }
909
910 // TODO: should try to update w_row here, to avoid recomputing it later.
911}
912
913/*
914 * Insert a string at the cursor position.
915 * Note: Does NOT handle Replace mode.
916 * Caller must have prepared for undo.
917 */
918 void
919ins_str(char_u *s)
920{
921 char_u *oldp, *newp;
922 int newlen = (int)STRLEN(s);
923 int oldlen;
924 colnr_T col;
925 linenr_T lnum = curwin->w_cursor.lnum;
926
927 if (virtual_active() && curwin->w_cursor.coladd > 0)
928 coladvance_force(getviscol());
929
930 col = curwin->w_cursor.col;
931 oldp = ml_get(lnum);
932 oldlen = (int)STRLEN(oldp);
933
934 newp = alloc_check((unsigned)(oldlen + newlen + 1));
935 if (newp == NULL)
936 return;
937 if (col > 0)
938 mch_memmove(newp, oldp, (size_t)col);
939 mch_memmove(newp + col, s, (size_t)newlen);
940 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
941 ml_replace(lnum, newp, FALSE);
942 inserted_bytes(lnum, col, newlen);
943 curwin->w_cursor.col += newlen;
944}
945
946/*
947 * Delete one character under the cursor.
948 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
949 * Caller must have prepared for undo.
950 *
951 * return FAIL for failure, OK otherwise
952 */
953 int
954del_char(int fixpos)
955{
956 if (has_mbyte)
957 {
958 // Make sure the cursor is at the start of a character.
959 mb_adjust_cursor();
960 if (*ml_get_cursor() == NUL)
961 return FAIL;
962 return del_chars(1L, fixpos);
963 }
964 return del_bytes(1L, fixpos, TRUE);
965}
966
967/*
968 * Like del_bytes(), but delete characters instead of bytes.
969 */
970 int
971del_chars(long count, int fixpos)
972{
973 long bytes = 0;
974 long i;
975 char_u *p;
976 int l;
977
978 p = ml_get_cursor();
979 for (i = 0; i < count && *p != NUL; ++i)
980 {
981 l = (*mb_ptr2len)(p);
982 bytes += l;
983 p += l;
984 }
985 return del_bytes(bytes, fixpos, TRUE);
986}
987
988/*
989 * Delete "count" bytes under the cursor.
990 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
991 * Caller must have prepared for undo.
992 *
993 * Return FAIL for failure, OK otherwise.
994 */
995 int
996del_bytes(
997 long count,
998 int fixpos_arg,
999 int use_delcombine UNUSED) // 'delcombine' option applies
1000{
1001 char_u *oldp, *newp;
1002 colnr_T oldlen;
1003 colnr_T newlen;
1004 linenr_T lnum = curwin->w_cursor.lnum;
1005 colnr_T col = curwin->w_cursor.col;
1006 int alloc_newp;
1007 long movelen;
1008 int fixpos = fixpos_arg;
1009
1010 oldp = ml_get(lnum);
1011 oldlen = (int)STRLEN(oldp);
1012
1013 // Can't do anything when the cursor is on the NUL after the line.
1014 if (col >= oldlen)
1015 return FAIL;
1016
1017 // If "count" is zero there is nothing to do.
1018 if (count == 0)
1019 return OK;
1020
1021 // If "count" is negative the caller must be doing something wrong.
1022 if (count < 1)
1023 {
1024 siemsg("E950: Invalid count for del_bytes(): %ld", count);
1025 return FAIL;
1026 }
1027
1028 // If 'delcombine' is set and deleting (less than) one character, only
1029 // delete the last combining character.
1030 if (p_deco && use_delcombine && enc_utf8
1031 && utfc_ptr2len(oldp + col) >= count)
1032 {
1033 int cc[MAX_MCO];
1034 int n;
1035
1036 (void)utfc_ptr2char(oldp + col, cc);
1037 if (cc[0] != NUL)
1038 {
1039 // Find the last composing char, there can be several.
1040 n = col;
1041 do
1042 {
1043 col = n;
1044 count = utf_ptr2len(oldp + n);
1045 n += count;
1046 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
1047 fixpos = 0;
1048 }
1049 }
1050
1051 // When count is too big, reduce it.
1052 movelen = (long)oldlen - (long)col - count + 1; // includes trailing NUL
1053 if (movelen <= 1)
1054 {
1055 // If we just took off the last character of a non-blank line, and
1056 // fixpos is TRUE, we don't want to end up positioned at the NUL,
1057 // unless "restart_edit" is set or 'virtualedit' contains "onemore".
1058 if (col > 0 && fixpos && restart_edit == 0
1059 && (ve_flags & VE_ONEMORE) == 0)
1060 {
1061 --curwin->w_cursor.col;
1062 curwin->w_cursor.coladd = 0;
1063 if (has_mbyte)
1064 curwin->w_cursor.col -=
1065 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
1066 }
1067 count = oldlen - col;
1068 movelen = 1;
1069 }
1070 newlen = oldlen - count;
1071
1072 // If the old line has been allocated the deletion can be done in the
1073 // existing line. Otherwise a new line has to be allocated
1074 // Can't do this when using Netbeans, because we would need to invoke
1075 // netbeans_removed(), which deallocates the line. Let ml_replace() take
1076 // care of notifying Netbeans.
1077#ifdef FEAT_NETBEANS_INTG
1078 if (netbeans_active())
1079 alloc_newp = TRUE;
1080 else
1081#endif
1082 alloc_newp = !ml_line_alloced(); // check if oldp was allocated
1083 if (!alloc_newp)
1084 newp = oldp; // use same allocated memory
1085 else
1086 { // need to allocate a new line
1087 newp = alloc((unsigned)(newlen + 1));
1088 if (newp == NULL)
1089 return FAIL;
1090 mch_memmove(newp, oldp, (size_t)col);
1091 }
1092 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
1093 if (alloc_newp)
1094 ml_replace(lnum, newp, FALSE);
1095#ifdef FEAT_TEXT_PROP
1096 else
1097 {
1098 // Also move any following text properties.
1099 if (oldlen + 1 < curbuf->b_ml.ml_line_len)
1100 mch_memmove(newp + newlen + 1, oldp + oldlen + 1,
1101 (size_t)curbuf->b_ml.ml_line_len - oldlen - 1);
1102 curbuf->b_ml.ml_line_len -= count;
1103 }
1104#endif
1105
1106 // mark the buffer as changed and prepare for displaying
1107 inserted_bytes(lnum, curwin->w_cursor.col, -count);
1108
1109 return OK;
1110}
1111
1112/*
1113 * Copy the indent from ptr to the current line (and fill to size)
1114 * Leaves the cursor on the first non-blank in the line.
1115 * Returns TRUE if the line was changed.
1116 */
1117 static int
1118copy_indent(int size, char_u *src)
1119{
1120 char_u *p = NULL;
1121 char_u *line = NULL;
1122 char_u *s;
1123 int todo;
1124 int ind_len;
1125 int line_len = 0;
1126 int tab_pad;
1127 int ind_done;
1128 int round;
1129#ifdef FEAT_VARTABS
1130 int ind_col;
1131#endif
1132
1133 // Round 1: compute the number of characters needed for the indent
1134 // Round 2: copy the characters.
1135 for (round = 1; round <= 2; ++round)
1136 {
1137 todo = size;
1138 ind_len = 0;
1139 ind_done = 0;
1140#ifdef FEAT_VARTABS
1141 ind_col = 0;
1142#endif
1143 s = src;
1144
1145 // Count/copy the usable portion of the source line
1146 while (todo > 0 && VIM_ISWHITE(*s))
1147 {
1148 if (*s == TAB)
1149 {
1150#ifdef FEAT_VARTABS
1151 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
1152 curbuf->b_p_vts_array);
1153#else
1154 tab_pad = (int)curbuf->b_p_ts
1155 - (ind_done % (int)curbuf->b_p_ts);
1156#endif
1157 // Stop if this tab will overshoot the target
1158 if (todo < tab_pad)
1159 break;
1160 todo -= tab_pad;
1161 ind_done += tab_pad;
1162#ifdef FEAT_VARTABS
1163 ind_col += tab_pad;
1164#endif
1165 }
1166 else
1167 {
1168 --todo;
1169 ++ind_done;
1170#ifdef FEAT_VARTABS
1171 ++ind_col;
1172#endif
1173 }
1174 ++ind_len;
1175 if (p != NULL)
1176 *p++ = *s;
1177 ++s;
1178 }
1179
1180 // Fill to next tabstop with a tab, if possible
1181#ifdef FEAT_VARTABS
1182 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
1183 curbuf->b_p_vts_array);
1184#else
1185 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
1186#endif
1187 if (todo >= tab_pad && !curbuf->b_p_et)
1188 {
1189 todo -= tab_pad;
1190 ++ind_len;
1191#ifdef FEAT_VARTABS
1192 ind_col += tab_pad;
1193#endif
1194 if (p != NULL)
1195 *p++ = TAB;
1196 }
1197
1198 // Add tabs required for indent
1199 if (!curbuf->b_p_et)
1200 {
1201#ifdef FEAT_VARTABS
1202 for (;;)
1203 {
1204 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
1205 curbuf->b_p_vts_array);
1206 if (todo < tab_pad)
1207 break;
1208 todo -= tab_pad;
1209 ++ind_len;
1210 ind_col += tab_pad;
1211 if (p != NULL)
1212 *p++ = TAB;
1213 }
1214#else
1215 while (todo >= (int)curbuf->b_p_ts)
1216 {
1217 todo -= (int)curbuf->b_p_ts;
1218 ++ind_len;
1219 if (p != NULL)
1220 *p++ = TAB;
1221 }
1222#endif
1223 }
1224
1225 // Count/add spaces required for indent
1226 while (todo > 0)
1227 {
1228 --todo;
1229 ++ind_len;
1230 if (p != NULL)
1231 *p++ = ' ';
1232 }
1233
1234 if (p == NULL)
1235 {
1236 // Allocate memory for the result: the copied indent, new indent
1237 // and the rest of the line.
1238 line_len = (int)STRLEN(ml_get_curline()) + 1;
1239 line = alloc(ind_len + line_len);
1240 if (line == NULL)
1241 return FALSE;
1242 p = line;
1243 }
1244 }
1245
1246 // Append the original line
1247 mch_memmove(p, ml_get_curline(), (size_t)line_len);
1248
1249 // Replace the line
1250 ml_replace(curwin->w_cursor.lnum, line, FALSE);
1251
1252 // Put the cursor after the indent.
1253 curwin->w_cursor.col = ind_len;
1254 return TRUE;
1255}
1256
1257/*
1258 * open_line: Add a new line below or above the current line.
1259 *
1260 * For VREPLACE mode, we only add a new line when we get to the end of the
1261 * file, otherwise we just start replacing the next line.
1262 *
1263 * Caller must take care of undo. Since VREPLACE may affect any number of
1264 * lines however, it may call u_save_cursor() again when starting to change a
1265 * new line.
1266 * "flags": OPENLINE_DELSPACES delete spaces after cursor
1267 * OPENLINE_DO_COM format comments
1268 * OPENLINE_KEEPTRAIL keep trailing spaces
1269 * OPENLINE_MARKFIX adjust mark positions after the line break
1270 * OPENLINE_COM_LIST format comments with list or 2nd line indent
1271 *
1272 * "second_line_indent": indent for after ^^D in Insert mode or if flag
1273 * OPENLINE_COM_LIST
1274 *
1275 * Return OK for success, FAIL for failure
1276 */
1277 int
1278open_line(
1279 int dir, // FORWARD or BACKWARD
1280 int flags,
1281 int second_line_indent)
1282{
1283 char_u *saved_line; // copy of the original line
1284 char_u *next_line = NULL; // copy of the next line
1285 char_u *p_extra = NULL; // what goes to next line
1286 int less_cols = 0; // less columns for mark in new line
1287 int less_cols_off = 0; // columns to skip for mark adjust
1288 pos_T old_cursor; // old cursor position
1289 int newcol = 0; // new cursor column
1290 int newindent = 0; // auto-indent of the new line
1291 int n;
1292 int trunc_line = FALSE; // truncate current line afterwards
1293 int retval = FAIL; // return value
1294#ifdef FEAT_COMMENTS
1295 int extra_len = 0; // length of p_extra string
1296 int lead_len; // length of comment leader
1297 char_u *lead_flags; // position in 'comments' for comment leader
1298 char_u *leader = NULL; // copy of comment leader
1299#endif
1300 char_u *allocated = NULL; // allocated memory
1301 char_u *p;
1302 int saved_char = NUL; // init for GCC
1303#if defined(FEAT_SMARTINDENT) || defined(FEAT_COMMENTS)
1304 pos_T *pos;
1305#endif
1306#ifdef FEAT_SMARTINDENT
1307 int do_si = (!p_paste && curbuf->b_p_si
1308# ifdef FEAT_CINDENT
1309 && !curbuf->b_p_cin
1310# endif
1311# ifdef FEAT_EVAL
1312 && *curbuf->b_p_inde == NUL
1313# endif
1314 );
1315 int no_si = FALSE; // reset did_si afterwards
1316 int first_char = NUL; // init for GCC
1317#endif
1318#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1319 int vreplace_mode;
1320#endif
1321 int did_append; // appended a new line
1322 int saved_pi = curbuf->b_p_pi; // copy of preserveindent setting
1323
1324 // make a copy of the current line so we can mess with it
1325 saved_line = vim_strsave(ml_get_curline());
1326 if (saved_line == NULL) /* out of memory! */
1327 return FALSE;
1328
1329 if (State & VREPLACE_FLAG)
1330 {
1331 // With VREPLACE we make a copy of the next line, which we will be
1332 // starting to replace. First make the new line empty and let vim play
1333 // with the indenting and comment leader to its heart's content. Then
1334 // we grab what it ended up putting on the new line, put back the
1335 // original line, and call ins_char() to put each new character onto
1336 // the line, replacing what was there before and pushing the right
1337 // stuff onto the replace stack. -- webb.
1338 if (curwin->w_cursor.lnum < orig_line_count)
1339 next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1));
1340 else
1341 next_line = vim_strsave((char_u *)"");
1342 if (next_line == NULL) // out of memory!
1343 goto theend;
1344
1345 // In VREPLACE mode, a NL replaces the rest of the line, and starts
1346 // replacing the next line, so push all of the characters left on the
1347 // line onto the replace stack. We'll push any other characters that
1348 // might be replaced at the start of the next line (due to autoindent
1349 // etc) a bit later.
1350 replace_push(NUL); // Call twice because BS over NL expects it
1351 replace_push(NUL);
1352 p = saved_line + curwin->w_cursor.col;
1353 while (*p != NUL)
1354 {
1355 if (has_mbyte)
1356 p += replace_push_mb(p);
1357 else
1358 replace_push(*p++);
1359 }
1360 saved_line[curwin->w_cursor.col] = NUL;
1361 }
1362
1363 if ((State & INSERT) && !(State & VREPLACE_FLAG))
1364 {
1365 p_extra = saved_line + curwin->w_cursor.col;
1366#ifdef FEAT_SMARTINDENT
1367 if (do_si) // need first char after new line break
1368 {
1369 p = skipwhite(p_extra);
1370 first_char = *p;
1371 }
1372#endif
1373#ifdef FEAT_COMMENTS
1374 extra_len = (int)STRLEN(p_extra);
1375#endif
1376 saved_char = *p_extra;
1377 *p_extra = NUL;
1378 }
1379
1380 u_clearline(); // cannot do "U" command when adding lines
1381#ifdef FEAT_SMARTINDENT
1382 did_si = FALSE;
1383#endif
1384 ai_col = 0;
1385
1386 // If we just did an auto-indent, then we didn't type anything on
1387 // the prior line, and it should be truncated. Do this even if 'ai' is not
1388 // set because automatically inserting a comment leader also sets did_ai.
1389 if (dir == FORWARD && did_ai)
1390 trunc_line = TRUE;
1391
1392 // If 'autoindent' and/or 'smartindent' is set, try to figure out what
1393 // indent to use for the new line.
1394 if (curbuf->b_p_ai
1395#ifdef FEAT_SMARTINDENT
1396 || do_si
1397#endif
1398 )
1399 {
1400 // count white space on current line
1401#ifdef FEAT_VARTABS
1402 newindent = get_indent_str_vtab(saved_line, curbuf->b_p_ts,
1403 curbuf->b_p_vts_array, FALSE);
1404#else
1405 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts, FALSE);
1406#endif
1407 if (newindent == 0 && !(flags & OPENLINE_COM_LIST))
1408 newindent = second_line_indent; // for ^^D command in insert mode
1409
1410#ifdef FEAT_SMARTINDENT
1411 // Do smart indenting.
1412 // In insert/replace mode (only when dir == FORWARD)
1413 // we may move some text to the next line. If it starts with '{'
1414 // don't add an indent. Fixes inserting a NL before '{' in line
1415 // "if (condition) {"
1416 if (!trunc_line && do_si && *saved_line != NUL
1417 && (p_extra == NULL || first_char != '{'))
1418 {
1419 char_u *ptr;
1420 char_u last_char;
1421
1422 old_cursor = curwin->w_cursor;
1423 ptr = saved_line;
1424# ifdef FEAT_COMMENTS
1425 if (flags & OPENLINE_DO_COM)
1426 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
1427 else
1428 lead_len = 0;
1429# endif
1430 if (dir == FORWARD)
1431 {
1432 // Skip preprocessor directives, unless they are
1433 // recognised as comments.
1434 if (
1435# ifdef FEAT_COMMENTS
1436 lead_len == 0 &&
1437# endif
1438 ptr[0] == '#')
1439 {
1440 while (ptr[0] == '#' && curwin->w_cursor.lnum > 1)
1441 ptr = ml_get(--curwin->w_cursor.lnum);
1442 newindent = get_indent();
1443 }
1444# ifdef FEAT_COMMENTS
1445 if (flags & OPENLINE_DO_COM)
1446 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
1447 else
1448 lead_len = 0;
1449 if (lead_len > 0)
1450 {
1451 // This case gets the following right:
1452 // /*
1453 // * A comment (read '\' as '/').
1454 // */
1455 // #define IN_THE_WAY
1456 // This should line up here;
1457 p = skipwhite(ptr);
1458 if (p[0] == '/' && p[1] == '*')
1459 p++;
1460 if (p[0] == '*')
1461 {
1462 for (p++; *p; p++)
1463 {
1464 if (p[0] == '/' && p[-1] == '*')
1465 {
1466 // End of C comment, indent should line up
1467 // with the line containing the start of
1468 // the comment
1469 curwin->w_cursor.col = (colnr_T)(p - ptr);
1470 if ((pos = findmatch(NULL, NUL)) != NULL)
1471 {
1472 curwin->w_cursor.lnum = pos->lnum;
1473 newindent = get_indent();
1474 }
1475 }
1476 }
1477 }
1478 }
1479 else // Not a comment line
1480# endif
1481 {
1482 // Find last non-blank in line
1483 p = ptr + STRLEN(ptr) - 1;
1484 while (p > ptr && VIM_ISWHITE(*p))
1485 --p;
1486 last_char = *p;
1487
1488 // find the character just before the '{' or ';'
1489 if (last_char == '{' || last_char == ';')
1490 {
1491 if (p > ptr)
1492 --p;
1493 while (p > ptr && VIM_ISWHITE(*p))
1494 --p;
1495 }
1496 // Try to catch lines that are split over multiple
1497 // lines. eg:
1498 // if (condition &&
1499 // condition) {
1500 // Should line up here!
1501 // }
1502 if (*p == ')')
1503 {
1504 curwin->w_cursor.col = (colnr_T)(p - ptr);
1505 if ((pos = findmatch(NULL, '(')) != NULL)
1506 {
1507 curwin->w_cursor.lnum = pos->lnum;
1508 newindent = get_indent();
1509 ptr = ml_get_curline();
1510 }
1511 }
1512 // If last character is '{' do indent, without
1513 // checking for "if" and the like.
1514 if (last_char == '{')
1515 {
1516 did_si = TRUE; // do indent
1517 no_si = TRUE; // don't delete it when '{' typed
1518 }
1519 // Look for "if" and the like, use 'cinwords'.
1520 // Don't do this if the previous line ended in ';' or
1521 // '}'.
1522 else if (last_char != ';' && last_char != '}'
1523 && cin_is_cinword(ptr))
1524 did_si = TRUE;
1525 }
1526 }
1527 else // dir == BACKWARD
1528 {
1529 // Skip preprocessor directives, unless they are
1530 // recognised as comments.
1531 if (
1532# ifdef FEAT_COMMENTS
1533 lead_len == 0 &&
1534# endif
1535 ptr[0] == '#')
1536 {
1537 int was_backslashed = FALSE;
1538
1539 while ((ptr[0] == '#' || was_backslashed) &&
1540 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
1541 {
1542 if (*ptr && ptr[STRLEN(ptr) - 1] == '\\')
1543 was_backslashed = TRUE;
1544 else
1545 was_backslashed = FALSE;
1546 ptr = ml_get(++curwin->w_cursor.lnum);
1547 }
1548 if (was_backslashed)
1549 newindent = 0; // Got to end of file
1550 else
1551 newindent = get_indent();
1552 }
1553 p = skipwhite(ptr);
1554 if (*p == '}') // if line starts with '}': do indent
1555 did_si = TRUE;
1556 else // can delete indent when '{' typed
1557 can_si_back = TRUE;
1558 }
1559 curwin->w_cursor = old_cursor;
1560 }
1561 if (do_si)
1562 can_si = TRUE;
1563#endif // FEAT_SMARTINDENT
1564
1565 did_ai = TRUE;
1566 }
1567
1568#ifdef FEAT_COMMENTS
1569 // Find out if the current line starts with a comment leader.
1570 // This may then be inserted in front of the new line.
1571 end_comment_pending = NUL;
1572 if (flags & OPENLINE_DO_COM)
1573 lead_len = get_leader_len(saved_line, &lead_flags,
1574 dir == BACKWARD, TRUE);
1575 else
1576 lead_len = 0;
1577 if (lead_len > 0)
1578 {
1579 char_u *lead_repl = NULL; // replaces comment leader
1580 int lead_repl_len = 0; // length of *lead_repl
1581 char_u lead_middle[COM_MAX_LEN]; // middle-comment string
1582 char_u lead_end[COM_MAX_LEN]; // end-comment string
1583 char_u *comment_end = NULL; // where lead_end has been found
1584 int extra_space = FALSE; // append extra space
1585 int current_flag;
1586 int require_blank = FALSE; // requires blank after middle
1587 char_u *p2;
1588
1589 // If the comment leader has the start, middle or end flag, it may not
1590 // be used or may be replaced with the middle leader.
1591 for (p = lead_flags; *p && *p != ':'; ++p)
1592 {
1593 if (*p == COM_BLANK)
1594 {
1595 require_blank = TRUE;
1596 continue;
1597 }
1598 if (*p == COM_START || *p == COM_MIDDLE)
1599 {
1600 current_flag = *p;
1601 if (*p == COM_START)
1602 {
1603 // Doing "O" on a start of comment does not insert leader.
1604 if (dir == BACKWARD)
1605 {
1606 lead_len = 0;
1607 break;
1608 }
1609
1610 // find start of middle part
1611 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
1612 require_blank = FALSE;
1613 }
1614
1615 // Isolate the strings of the middle and end leader.
1616 while (*p && p[-1] != ':') /* find end of middle flags */
1617 {
1618 if (*p == COM_BLANK)
1619 require_blank = TRUE;
1620 ++p;
1621 }
1622 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
1623
1624 while (*p && p[-1] != ':') // find end of end flags
1625 {
1626 // Check whether we allow automatic ending of comments
1627 if (*p == COM_AUTO_END)
1628 end_comment_pending = -1; // means we want to set it
1629 ++p;
1630 }
1631 n = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
1632
1633 if (end_comment_pending == -1) // we can set it now
1634 end_comment_pending = lead_end[n - 1];
1635
1636 // If the end of the comment is in the same line, don't use
1637 // the comment leader.
1638 if (dir == FORWARD)
1639 {
1640 for (p = saved_line + lead_len; *p; ++p)
1641 if (STRNCMP(p, lead_end, n) == 0)
1642 {
1643 comment_end = p;
1644 lead_len = 0;
1645 break;
1646 }
1647 }
1648
1649 // Doing "o" on a start of comment inserts the middle leader.
1650 if (lead_len > 0)
1651 {
1652 if (current_flag == COM_START)
1653 {
1654 lead_repl = lead_middle;
1655 lead_repl_len = (int)STRLEN(lead_middle);
1656 }
1657
1658 // If we have hit RETURN immediately after the start
1659 // comment leader, then put a space after the middle
1660 // comment leader on the next line.
1661 if (!VIM_ISWHITE(saved_line[lead_len - 1])
1662 && ((p_extra != NULL
1663 && (int)curwin->w_cursor.col == lead_len)
1664 || (p_extra == NULL
1665 && saved_line[lead_len] == NUL)
1666 || require_blank))
1667 extra_space = TRUE;
1668 }
1669 break;
1670 }
1671 if (*p == COM_END)
1672 {
1673 // Doing "o" on the end of a comment does not insert leader.
1674 // Remember where the end is, might want to use it to find the
1675 // start (for C-comments).
1676 if (dir == FORWARD)
1677 {
1678 comment_end = skipwhite(saved_line);
1679 lead_len = 0;
1680 break;
1681 }
1682
1683 // Doing "O" on the end of a comment inserts the middle leader.
1684 // Find the string for the middle leader, searching backwards.
1685 while (p > curbuf->b_p_com && *p != ',')
1686 --p;
1687 for (lead_repl = p; lead_repl > curbuf->b_p_com
1688 && lead_repl[-1] != ':'; --lead_repl)
1689 ;
1690 lead_repl_len = (int)(p - lead_repl);
1691
1692 // We can probably always add an extra space when doing "O" on
1693 // the comment-end
1694 extra_space = TRUE;
1695
1696 // Check whether we allow automatic ending of comments
1697 for (p2 = p; *p2 && *p2 != ':'; p2++)
1698 {
1699 if (*p2 == COM_AUTO_END)
1700 end_comment_pending = -1; // means we want to set it
1701 }
1702 if (end_comment_pending == -1)
1703 {
1704 // Find last character in end-comment string
1705 while (*p2 && *p2 != ',')
1706 p2++;
1707 end_comment_pending = p2[-1];
1708 }
1709 break;
1710 }
1711 if (*p == COM_FIRST)
1712 {
1713 // Comment leader for first line only: Don't repeat leader
1714 // when using "O", blank out leader when using "o".
1715 if (dir == BACKWARD)
1716 lead_len = 0;
1717 else
1718 {
1719 lead_repl = (char_u *)"";
1720 lead_repl_len = 0;
1721 }
1722 break;
1723 }
1724 }
1725 if (lead_len)
1726 {
1727 // allocate buffer (may concatenate p_extra later)
1728 leader = alloc(lead_len + lead_repl_len + extra_space + extra_len
1729 + (second_line_indent > 0 ? second_line_indent : 0) + 1);
1730 allocated = leader; // remember to free it later
1731
1732 if (leader == NULL)
1733 lead_len = 0;
1734 else
1735 {
1736 vim_strncpy(leader, saved_line, lead_len);
1737
1738 // Replace leader with lead_repl, right or left adjusted
1739 if (lead_repl != NULL)
1740 {
1741 int c = 0;
1742 int off = 0;
1743
1744 for (p = lead_flags; *p != NUL && *p != ':'; )
1745 {
1746 if (*p == COM_RIGHT || *p == COM_LEFT)
1747 c = *p++;
1748 else if (VIM_ISDIGIT(*p) || *p == '-')
1749 off = getdigits(&p);
1750 else
1751 ++p;
1752 }
1753 if (c == COM_RIGHT) // right adjusted leader
1754 {
1755 // find last non-white in the leader to line up with
1756 for (p = leader + lead_len - 1; p > leader
1757 && VIM_ISWHITE(*p); --p)
1758 ;
1759 ++p;
1760
1761 // Compute the length of the replaced characters in
1762 // screen characters, not bytes.
1763 {
1764 int repl_size = vim_strnsize(lead_repl,
1765 lead_repl_len);
1766 int old_size = 0;
1767 char_u *endp = p;
1768 int l;
1769
1770 while (old_size < repl_size && p > leader)
1771 {
1772 MB_PTR_BACK(leader, p);
1773 old_size += ptr2cells(p);
1774 }
1775 l = lead_repl_len - (int)(endp - p);
1776 if (l != 0)
1777 mch_memmove(endp + l, endp,
1778 (size_t)((leader + lead_len) - endp));
1779 lead_len += l;
1780 }
1781 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1782 if (p + lead_repl_len > leader + lead_len)
1783 p[lead_repl_len] = NUL;
1784
1785 // blank-out any other chars from the old leader.
1786 while (--p >= leader)
1787 {
1788 int l = mb_head_off(leader, p);
1789
1790 if (l > 1)
1791 {
1792 p -= l;
1793 if (ptr2cells(p) > 1)
1794 {
1795 p[1] = ' ';
1796 --l;
1797 }
1798 mch_memmove(p + 1, p + l + 1,
1799 (size_t)((leader + lead_len) - (p + l + 1)));
1800 lead_len -= l;
1801 *p = ' ';
1802 }
1803 else if (!VIM_ISWHITE(*p))
1804 *p = ' ';
1805 }
1806 }
1807 else // left adjusted leader
1808 {
1809 p = skipwhite(leader);
1810
1811 // Compute the length of the replaced characters in
1812 // screen characters, not bytes. Move the part that is
1813 // not to be overwritten.
1814 {
1815 int repl_size = vim_strnsize(lead_repl,
1816 lead_repl_len);
1817 int i;
1818 int l;
1819
1820 for (i = 0; i < lead_len && p[i] != NUL; i += l)
1821 {
1822 l = (*mb_ptr2len)(p + i);
1823 if (vim_strnsize(p, i + l) > repl_size)
1824 break;
1825 }
1826 if (i != lead_repl_len)
1827 {
1828 mch_memmove(p + lead_repl_len, p + i,
1829 (size_t)(lead_len - i - (p - leader)));
1830 lead_len += lead_repl_len - i;
1831 }
1832 }
1833 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1834
1835 // Replace any remaining non-white chars in the old
1836 // leader by spaces. Keep Tabs, the indent must
1837 // remain the same.
1838 for (p += lead_repl_len; p < leader + lead_len; ++p)
1839 if (!VIM_ISWHITE(*p))
1840 {
1841 // Don't put a space before a TAB.
1842 if (p + 1 < leader + lead_len && p[1] == TAB)
1843 {
1844 --lead_len;
1845 mch_memmove(p, p + 1,
1846 (leader + lead_len) - p);
1847 }
1848 else
1849 {
1850 int l = (*mb_ptr2len)(p);
1851
1852 if (l > 1)
1853 {
1854 if (ptr2cells(p) > 1)
1855 {
1856 // Replace a double-wide char with
1857 // two spaces
1858 --l;
1859 *p++ = ' ';
1860 }
1861 mch_memmove(p + 1, p + l,
1862 (leader + lead_len) - p);
1863 lead_len -= l - 1;
1864 }
1865 *p = ' ';
1866 }
1867 }
1868 *p = NUL;
1869 }
1870
1871 // Recompute the indent, it may have changed.
1872 if (curbuf->b_p_ai
1873#ifdef FEAT_SMARTINDENT
1874 || do_si
1875#endif
1876 )
1877#ifdef FEAT_VARTABS
1878 newindent = get_indent_str_vtab(leader, curbuf->b_p_ts,
1879 curbuf->b_p_vts_array, FALSE);
1880#else
1881 newindent = get_indent_str(leader,
1882 (int)curbuf->b_p_ts, FALSE);
1883#endif
1884
1885 // Add the indent offset
1886 if (newindent + off < 0)
1887 {
1888 off = -newindent;
1889 newindent = 0;
1890 }
1891 else
1892 newindent += off;
1893
1894 // Correct trailing spaces for the shift, so that
1895 // alignment remains equal.
1896 while (off > 0 && lead_len > 0
1897 && leader[lead_len - 1] == ' ')
1898 {
1899 // Don't do it when there is a tab before the space
1900 if (vim_strchr(skipwhite(leader), '\t') != NULL)
1901 break;
1902 --lead_len;
1903 --off;
1904 }
1905
1906 // If the leader ends in white space, don't add an
1907 // extra space
1908 if (lead_len > 0 && VIM_ISWHITE(leader[lead_len - 1]))
1909 extra_space = FALSE;
1910 leader[lead_len] = NUL;
1911 }
1912
1913 if (extra_space)
1914 {
1915 leader[lead_len++] = ' ';
1916 leader[lead_len] = NUL;
1917 }
1918
1919 newcol = lead_len;
1920
1921 // if a new indent will be set below, remove the indent that
1922 // is in the comment leader
1923 if (newindent
1924#ifdef FEAT_SMARTINDENT
1925 || did_si
1926#endif
1927 )
1928 {
1929 while (lead_len && VIM_ISWHITE(*leader))
1930 {
1931 --lead_len;
1932 --newcol;
1933 ++leader;
1934 }
1935 }
1936
1937 }
1938#ifdef FEAT_SMARTINDENT
1939 did_si = can_si = FALSE;
1940#endif
1941 }
1942 else if (comment_end != NULL)
1943 {
1944 // We have finished a comment, so we don't use the leader.
1945 // If this was a C-comment and 'ai' or 'si' is set do a normal
1946 // indent to align with the line containing the start of the
1947 // comment.
1948 if (comment_end[0] == '*' && comment_end[1] == '/' &&
1949 (curbuf->b_p_ai
1950#ifdef FEAT_SMARTINDENT
1951 || do_si
1952#endif
1953 ))
1954 {
1955 old_cursor = curwin->w_cursor;
1956 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line);
1957 if ((pos = findmatch(NULL, NUL)) != NULL)
1958 {
1959 curwin->w_cursor.lnum = pos->lnum;
1960 newindent = get_indent();
1961 }
1962 curwin->w_cursor = old_cursor;
1963 }
1964 }
1965 }
1966#endif
1967
1968 // (State == INSERT || State == REPLACE), only when dir == FORWARD
1969 if (p_extra != NULL)
1970 {
1971 *p_extra = saved_char; // restore char that NUL replaced
1972
1973 // When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first
1974 // non-blank.
1975 //
1976 // When in REPLACE mode, put the deleted blanks on the replace stack,
1977 // preceded by a NUL, so they can be put back when a BS is entered.
1978 if (REPLACE_NORMAL(State))
1979 replace_push(NUL); /* end of extra blanks */
1980 if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES))
1981 {
1982 while ((*p_extra == ' ' || *p_extra == '\t')
1983 && (!enc_utf8
1984 || !utf_iscomposing(utf_ptr2char(p_extra + 1))))
1985 {
1986 if (REPLACE_NORMAL(State))
1987 replace_push(*p_extra);
1988 ++p_extra;
1989 ++less_cols_off;
1990 }
1991 }
1992
1993 // columns for marks adjusted for removed columns
1994 less_cols = (int)(p_extra - saved_line);
1995 }
1996
1997 if (p_extra == NULL)
1998 p_extra = (char_u *)""; // append empty line
1999
2000#ifdef FEAT_COMMENTS
2001 // concatenate leader and p_extra, if there is a leader
2002 if (lead_len)
2003 {
2004 if (flags & OPENLINE_COM_LIST && second_line_indent > 0)
2005 {
2006 int i;
2007 int padding = second_line_indent
2008 - (newindent + (int)STRLEN(leader));
2009
2010 // Here whitespace is inserted after the comment char.
2011 // Below, set_indent(newindent, SIN_INSERT) will insert the
2012 // whitespace needed before the comment char.
2013 for (i = 0; i < padding; i++)
2014 {
2015 STRCAT(leader, " ");
2016 less_cols--;
2017 newcol++;
2018 }
2019 }
2020 STRCAT(leader, p_extra);
2021 p_extra = leader;
2022 did_ai = TRUE; // So truncating blanks works with comments
2023 less_cols -= lead_len;
2024 }
2025 else
2026 end_comment_pending = NUL; // turns out there was no leader
2027#endif
2028
2029 old_cursor = curwin->w_cursor;
2030 if (dir == BACKWARD)
2031 --curwin->w_cursor.lnum;
2032 if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count)
2033 {
2034 if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE)
2035 == FAIL)
2036 goto theend;
2037 // Postpone calling changed_lines(), because it would mess up folding
2038 // with markers.
2039 // Skip mark_adjust when adding a line after the last one, there can't
2040 // be marks there. But still needed in diff mode.
2041 if (curwin->w_cursor.lnum + 1 < curbuf->b_ml.ml_line_count
2042#ifdef FEAT_DIFF
2043 || curwin->w_p_diff
2044#endif
2045 )
2046 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
2047 did_append = TRUE;
2048 }
2049 else
2050 {
2051 // In VREPLACE mode we are starting to replace the next line.
2052 curwin->w_cursor.lnum++;
2053 if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed)
2054 {
2055 // In case we NL to a new line, BS to the previous one, and NL
2056 // again, we don't want to save the new line for undo twice.
2057 (void)u_save_cursor(); /* errors are ignored! */
2058 vr_lines_changed++;
2059 }
2060 ml_replace(curwin->w_cursor.lnum, p_extra, TRUE);
2061 changed_bytes(curwin->w_cursor.lnum, 0);
2062 curwin->w_cursor.lnum--;
2063 did_append = FALSE;
2064 }
2065
2066 if (newindent
2067#ifdef FEAT_SMARTINDENT
2068 || did_si
2069#endif
2070 )
2071 {
2072 ++curwin->w_cursor.lnum;
2073#ifdef FEAT_SMARTINDENT
2074 if (did_si)
2075 {
2076 int sw = (int)get_sw_value(curbuf);
2077
2078 if (p_sr)
2079 newindent -= newindent % sw;
2080 newindent += sw;
2081 }
2082#endif
2083 // Copy the indent
2084 if (curbuf->b_p_ci)
2085 {
2086 (void)copy_indent(newindent, saved_line);
2087
2088 // Set the 'preserveindent' option so that any further screwing
2089 // with the line doesn't entirely destroy our efforts to preserve
2090 // it. It gets restored at the function end.
2091 curbuf->b_p_pi = TRUE;
2092 }
2093 else
2094 (void)set_indent(newindent, SIN_INSERT);
2095 less_cols -= curwin->w_cursor.col;
2096
2097 ai_col = curwin->w_cursor.col;
2098
2099 // In REPLACE mode, for each character in the new indent, there must
2100 // be a NUL on the replace stack, for when it is deleted with BS
2101 if (REPLACE_NORMAL(State))
2102 for (n = 0; n < (int)curwin->w_cursor.col; ++n)
2103 replace_push(NUL);
2104 newcol += curwin->w_cursor.col;
2105#ifdef FEAT_SMARTINDENT
2106 if (no_si)
2107 did_si = FALSE;
2108#endif
2109 }
2110
2111#ifdef FEAT_COMMENTS
2112 // In REPLACE mode, for each character in the extra leader, there must be
2113 // a NUL on the replace stack, for when it is deleted with BS.
2114 if (REPLACE_NORMAL(State))
2115 while (lead_len-- > 0)
2116 replace_push(NUL);
2117#endif
2118
2119 curwin->w_cursor = old_cursor;
2120
2121 if (dir == FORWARD)
2122 {
2123 if (trunc_line || (State & INSERT))
2124 {
2125 // truncate current line at cursor
2126 saved_line[curwin->w_cursor.col] = NUL;
2127 // Remove trailing white space, unless OPENLINE_KEEPTRAIL used.
2128 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL))
2129 truncate_spaces(saved_line);
2130 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE);
2131 saved_line = NULL;
2132 if (did_append)
2133 {
2134 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
2135 curwin->w_cursor.lnum + 1, 1L);
2136 did_append = FALSE;
2137
2138 // Move marks after the line break to the new line.
2139 if (flags & OPENLINE_MARKFIX)
2140 mark_col_adjust(curwin->w_cursor.lnum,
2141 curwin->w_cursor.col + less_cols_off,
2142 1L, (long)-less_cols, 0);
2143 }
2144 else
2145 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
2146 }
2147
2148 // Put the cursor on the new line. Careful: the scrollup() above may
2149 // have moved w_cursor, we must use old_cursor.
2150 curwin->w_cursor.lnum = old_cursor.lnum + 1;
2151 }
2152 if (did_append)
2153 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L);
2154
2155 curwin->w_cursor.col = newcol;
2156 curwin->w_cursor.coladd = 0;
2157
2158#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2159 // In VREPLACE mode, we are handling the replace stack ourselves, so stop
2160 // fixthisline() from doing it (via change_indent()) by telling it we're in
2161 // normal INSERT mode.
2162 if (State & VREPLACE_FLAG)
2163 {
2164 vreplace_mode = State; // So we know to put things right later
2165 State = INSERT;
2166 }
2167 else
2168 vreplace_mode = 0;
2169#endif
2170#ifdef FEAT_LISP
2171 // May do lisp indenting.
2172 if (!p_paste
2173# ifdef FEAT_COMMENTS
2174 && leader == NULL
2175# endif
2176 && curbuf->b_p_lisp
2177 && curbuf->b_p_ai)
2178 {
2179 fixthisline(get_lisp_indent);
2180 ai_col = (colnr_T)getwhitecols_curline();
2181 }
2182#endif
2183#ifdef FEAT_CINDENT
2184 // May do indenting after opening a new line.
2185 if (!p_paste
2186 && (curbuf->b_p_cin
2187# ifdef FEAT_EVAL
2188 || *curbuf->b_p_inde != NUL
2189# endif
2190 )
2191 && in_cinkeys(dir == FORWARD
2192 ? KEY_OPEN_FORW
2193 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum)))
2194 {
2195 do_c_expr_indent();
2196 ai_col = (colnr_T)getwhitecols_curline();
2197 }
2198#endif
2199#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2200 if (vreplace_mode != 0)
2201 State = vreplace_mode;
2202#endif
2203
2204 // Finally, VREPLACE gets the stuff on the new line, then puts back the
2205 // original line, and inserts the new stuff char by char, pushing old stuff
2206 // onto the replace stack (via ins_char()).
2207 if (State & VREPLACE_FLAG)
2208 {
2209 // Put new line in p_extra
2210 p_extra = vim_strsave(ml_get_curline());
2211 if (p_extra == NULL)
2212 goto theend;
2213
2214 // Put back original line
2215 ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
2216
2217 // Insert new stuff into line again
2218 curwin->w_cursor.col = 0;
2219 curwin->w_cursor.coladd = 0;
2220 ins_bytes(p_extra); // will call changed_bytes()
2221 vim_free(p_extra);
2222 next_line = NULL;
2223 }
2224
2225 retval = OK; // success!
2226theend:
2227 curbuf->b_p_pi = saved_pi;
2228 vim_free(saved_line);
2229 vim_free(next_line);
2230 vim_free(allocated);
2231 return retval;
2232}
2233
2234/*
2235 * Delete from cursor to end of line.
2236 * Caller must have prepared for undo.
2237 * If "fixpos" is TRUE fix the cursor position when done.
2238 *
2239 * Return FAIL for failure, OK otherwise.
2240 */
2241 int
2242truncate_line(int fixpos)
2243{
2244 char_u *newp;
2245 linenr_T lnum = curwin->w_cursor.lnum;
2246 colnr_T col = curwin->w_cursor.col;
2247
2248 if (col == 0)
2249 newp = vim_strsave((char_u *)"");
2250 else
2251 newp = vim_strnsave(ml_get(lnum), col);
2252
2253 if (newp == NULL)
2254 return FAIL;
2255
2256 ml_replace(lnum, newp, FALSE);
2257
2258 // mark the buffer as changed and prepare for displaying
2259 changed_bytes(lnum, curwin->w_cursor.col);
2260
2261 // If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2262 if (fixpos && curwin->w_cursor.col > 0)
2263 --curwin->w_cursor.col;
2264
2265 return OK;
2266}
2267
2268/*
2269 * Delete "nlines" lines at the cursor.
2270 * Saves the lines for undo first if "undo" is TRUE.
2271 */
2272 void
2273del_lines(long nlines, int undo)
2274{
2275 long n;
2276 linenr_T first = curwin->w_cursor.lnum;
2277
2278 if (nlines <= 0)
2279 return;
2280
2281 // save the deleted lines for undo
2282 if (undo && u_savedel(first, nlines) == FAIL)
2283 return;
2284
2285 for (n = 0; n < nlines; )
2286 {
2287 if (curbuf->b_ml.ml_flags & ML_EMPTY) // nothing to delete
2288 break;
2289
2290 ml_delete(first, TRUE);
2291 ++n;
2292
2293 // If we delete the last line in the file, stop
2294 if (first > curbuf->b_ml.ml_line_count)
2295 break;
2296 }
2297
2298 // Correct the cursor position before calling deleted_lines_mark(), it may
2299 // trigger a callback to display the cursor.
2300 curwin->w_cursor.col = 0;
2301 check_cursor_lnum();
2302
2303 // adjust marks, mark the buffer as changed and prepare for displaying
2304 deleted_lines_mark(first, n);
2305}