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