blob: d1ecb60bbf42f66111dbd52e94f6bb51211659cf [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();
Bram Moolenaareda1da02019-11-17 17:06:33 +010061 ui_delay(1002L, TRUE); // give the user time to think about it
Bram Moolenaar3f86ca02019-05-11 18:30:00 +020062 }
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.
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100118 if (need_wait_return && emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200119 {
120 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +0100121 ui_delay(2002L, TRUE);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200122 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;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200149 need_maketitle = TRUE; // set window title later
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200150}
151
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200152#ifdef FEAT_EVAL
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200153static long next_listener_id = 0;
154
155/*
Bram Moolenaar4a0a1612019-07-17 22:00:19 +0200156 * Check if the change at "lnum" is above or overlaps with an existing
157 * change. If above then flush changes and invoke listeners.
Bram Moolenaardda41442019-05-16 22:11:47 +0200158 */
Bram Moolenaar55737c22022-02-14 14:51:22 +0000159 static void
Bram Moolenaardda41442019-05-16 22:11:47 +0200160check_recorded_changes(
161 buf_T *buf,
162 linenr_T lnum,
Bram Moolenaardda41442019-05-16 22:11:47 +0200163 linenr_T lnume,
Bram Moolenaar4a0a1612019-07-17 22:00:19 +0200164 long xtra)
Bram Moolenaardda41442019-05-16 22:11:47 +0200165{
166 if (buf->b_recorded_changes != NULL && xtra != 0)
167 {
168 listitem_T *li;
Bram Moolenaar7b31a182019-05-24 21:39:27 +0200169 linenr_T prev_lnum;
170 linenr_T prev_lnume;
Bram Moolenaardda41442019-05-16 22:11:47 +0200171
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200172 FOR_ALL_LIST_ITEMS(buf->b_recorded_changes, li)
Bram Moolenaardda41442019-05-16 22:11:47 +0200173 {
Bram Moolenaar7b31a182019-05-24 21:39:27 +0200174 prev_lnum = (linenr_T)dict_get_number(
Bram Moolenaardda41442019-05-16 22:11:47 +0200175 li->li_tv.vval.v_dict, (char_u *)"lnum");
Bram Moolenaar7b31a182019-05-24 21:39:27 +0200176 prev_lnume = (linenr_T)dict_get_number(
177 li->li_tv.vval.v_dict, (char_u *)"end");
Bram Moolenaar4a0a1612019-07-17 22:00:19 +0200178 if (prev_lnum >= lnum || prev_lnum > lnume || prev_lnume >= lnum)
Bram Moolenaardda41442019-05-16 22:11:47 +0200179 {
Bram Moolenaar4a0a1612019-07-17 22:00:19 +0200180 // the current change is going to make the line number in
181 // the older change invalid, flush now
182 invoke_listeners(curbuf);
183 break;
Bram Moolenaardda41442019-05-16 22:11:47 +0200184 }
185 }
186 }
Bram Moolenaardda41442019-05-16 22:11:47 +0200187}
188
189/*
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200190 * Record a change for listeners added with listener_add().
Bram Moolenaardda41442019-05-16 22:11:47 +0200191 * Always for the current buffer.
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200192 */
193 static void
194may_record_change(
195 linenr_T lnum,
196 colnr_T col,
197 linenr_T lnume,
198 long xtra)
199{
200 dict_T *dict;
201
202 if (curbuf->b_listener == NULL)
203 return;
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200204
205 // If the new change is going to change the line numbers in already listed
206 // changes, then flush.
Bram Moolenaar55737c22022-02-14 14:51:22 +0000207 check_recorded_changes(curbuf, lnum, lnume, xtra);
Bram Moolenaardda41442019-05-16 22:11:47 +0200208
209 if (curbuf->b_recorded_changes == NULL)
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200210 {
Bram Moolenaardda41442019-05-16 22:11:47 +0200211 curbuf->b_recorded_changes = list_alloc();
212 if (curbuf->b_recorded_changes == NULL) // out of memory
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200213 return;
Bram Moolenaardda41442019-05-16 22:11:47 +0200214 ++curbuf->b_recorded_changes->lv_refcount;
215 curbuf->b_recorded_changes->lv_lock = VAR_FIXED;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200216 }
217
218 dict = dict_alloc();
219 if (dict == NULL)
220 return;
221 dict_add_number(dict, "lnum", (varnumber_T)lnum);
222 dict_add_number(dict, "end", (varnumber_T)lnume);
223 dict_add_number(dict, "added", (varnumber_T)xtra);
Bram Moolenaara3347722019-05-11 21:14:24 +0200224 dict_add_number(dict, "col", (varnumber_T)col + 1);
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200225
Bram Moolenaardda41442019-05-16 22:11:47 +0200226 list_append_dict(curbuf->b_recorded_changes, dict);
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200227}
228
229/*
230 * listener_add() function
231 */
232 void
233f_listener_add(typval_T *argvars, typval_T *rettv)
234{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200235 callback_T callback;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200236 listener_T *lnr;
Bram Moolenaara3347722019-05-11 21:14:24 +0200237 buf_T *buf = curbuf;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200238
Yegappan Lakshmanan5bca9062021-07-24 21:33:26 +0200239 if (in_vim9script() && check_for_opt_buffer_arg(argvars, 1) == FAIL)
240 return;
241
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200242 callback = get_callback(&argvars[0]);
243 if (callback.cb_name == NULL)
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200244 return;
245
Bram Moolenaara3347722019-05-11 21:14:24 +0200246 if (argvars[1].v_type != VAR_UNKNOWN)
247 {
248 buf = get_buf_arg(&argvars[1]);
249 if (buf == NULL)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200250 {
251 free_callback(&callback);
Bram Moolenaara3347722019-05-11 21:14:24 +0200252 return;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200253 }
Bram Moolenaara3347722019-05-11 21:14:24 +0200254 }
255
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200256 lnr = ALLOC_CLEAR_ONE(listener_T);
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200257 if (lnr == NULL)
258 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200259 free_callback(&callback);
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200260 return;
261 }
Bram Moolenaara3347722019-05-11 21:14:24 +0200262 lnr->lr_next = buf->b_listener;
263 buf->b_listener = lnr;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200264
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200265 set_callback(&lnr->lr_callback, &callback);
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200266
267 lnr->lr_id = ++next_listener_id;
268 rettv->vval.v_number = lnr->lr_id;
269}
270
271/*
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200272 * listener_flush() function
273 */
274 void
275f_listener_flush(typval_T *argvars, typval_T *rettv UNUSED)
276{
277 buf_T *buf = curbuf;
278
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200279 if (in_vim9script() && check_for_opt_buffer_arg(argvars, 0) == FAIL)
280 return;
281
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200282 if (argvars[0].v_type != VAR_UNKNOWN)
283 {
284 buf = get_buf_arg(&argvars[0]);
285 if (buf == NULL)
286 return;
287 }
288 invoke_listeners(buf);
289}
290
Bram Moolenaar4b4b1b82021-09-11 15:06:44 +0200291
292 static void
293remove_listener(buf_T *buf, listener_T *lnr, listener_T *prev)
294{
295 if (prev != NULL)
296 prev->lr_next = lnr->lr_next;
297 else
298 buf->b_listener = lnr->lr_next;
299 free_callback(&lnr->lr_callback);
300 vim_free(lnr);
301}
302
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200303/*
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200304 * listener_remove() function
305 */
306 void
Bram Moolenaar7b73f912019-07-13 13:03:02 +0200307f_listener_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200308{
309 listener_T *lnr;
310 listener_T *next;
Bram Moolenaar7b73f912019-07-13 13:03:02 +0200311 listener_T *prev;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200312 int id;
Bram Moolenaara3347722019-05-11 21:14:24 +0200313 buf_T *buf;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200314
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200315 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
316 return;
317
318 id = tv_get_number(argvars);
Bram Moolenaar86173482019-10-01 17:02:16 +0200319 FOR_ALL_BUFFERS(buf)
Bram Moolenaar7b73f912019-07-13 13:03:02 +0200320 {
321 prev = NULL;
Bram Moolenaara3347722019-05-11 21:14:24 +0200322 for (lnr = buf->b_listener; lnr != NULL; lnr = next)
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200323 {
Bram Moolenaara3347722019-05-11 21:14:24 +0200324 next = lnr->lr_next;
325 if (lnr->lr_id == id)
326 {
Bram Moolenaar4b4b1b82021-09-11 15:06:44 +0200327 if (textwinlock > 0)
328 {
329 // in invoke_listeners(), clear ID and delete later
330 lnr->lr_id = 0;
331 return;
332 }
333 remove_listener(buf, lnr, prev);
Bram Moolenaar7b73f912019-07-13 13:03:02 +0200334 rettv->vval.v_number = 1;
335 return;
Bram Moolenaara3347722019-05-11 21:14:24 +0200336 }
337 prev = lnr;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200338 }
Bram Moolenaar7b73f912019-07-13 13:03:02 +0200339 }
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200340}
341
342/*
Bram Moolenaardda41442019-05-16 22:11:47 +0200343 * Called before inserting a line above "lnum"/"lnum3" or deleting line "lnum"
344 * to "lnume".
345 */
346 void
347may_invoke_listeners(buf_T *buf, linenr_T lnum, linenr_T lnume, int added)
348{
Bram Moolenaar4a0a1612019-07-17 22:00:19 +0200349 check_recorded_changes(buf, lnum, lnume, added);
Bram Moolenaardda41442019-05-16 22:11:47 +0200350}
351
352/*
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200353 * Called when a sequence of changes is done: invoke listeners added with
354 * listener_add().
355 */
356 void
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200357invoke_listeners(buf_T *buf)
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200358{
359 listener_T *lnr;
360 typval_T rettv;
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200361 typval_T argv[6];
362 listitem_T *li;
363 linenr_T start = MAXLNUM;
364 linenr_T end = 0;
365 linenr_T added = 0;
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200366 int save_updating_screen = updating_screen;
367 static int recursive = FALSE;
Bram Moolenaar4b4b1b82021-09-11 15:06:44 +0200368 listener_T *next;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200369
Bram Moolenaardda41442019-05-16 22:11:47 +0200370 if (buf->b_recorded_changes == NULL // nothing changed
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200371 || buf->b_listener == NULL // no listeners
372 || recursive) // already busy
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200373 return;
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200374 recursive = TRUE;
375
376 // Block messages on channels from being handled, so that they don't make
377 // text changes here.
378 ++updating_screen;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200379
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200380 argv[0].v_type = VAR_NUMBER;
381 argv[0].vval.v_number = buf->b_fnum; // a:bufnr
382
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200383 FOR_ALL_LIST_ITEMS(buf->b_recorded_changes, li)
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200384 {
385 varnumber_T lnum;
386
387 lnum = dict_get_number(li->li_tv.vval.v_dict, (char_u *)"lnum");
388 if (start > lnum)
389 start = lnum;
390 lnum = dict_get_number(li->li_tv.vval.v_dict, (char_u *)"end");
Bram Moolenaar336bf2b2019-10-24 20:07:07 +0200391 if (end < lnum)
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200392 end = lnum;
Bram Moolenaar336bf2b2019-10-24 20:07:07 +0200393 added += dict_get_number(li->li_tv.vval.v_dict, (char_u *)"added");
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200394 }
395 argv[1].v_type = VAR_NUMBER;
396 argv[1].vval.v_number = start;
397 argv[2].v_type = VAR_NUMBER;
398 argv[2].vval.v_number = end;
399 argv[3].v_type = VAR_NUMBER;
400 argv[3].vval.v_number = added;
401
402 argv[4].v_type = VAR_LIST;
Bram Moolenaardda41442019-05-16 22:11:47 +0200403 argv[4].vval.v_list = buf->b_recorded_changes;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200404 ++textwinlock;
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200405
406 for (lnr = buf->b_listener; lnr != NULL; lnr = lnr->lr_next)
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200407 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200408 call_callback(&lnr->lr_callback, -1, &rettv, 5, argv);
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200409 clear_tv(&rettv);
410 }
411
Bram Moolenaar4b4b1b82021-09-11 15:06:44 +0200412 // If f_listener_remove() was called may have to remove a listener now.
413 for (lnr = buf->b_listener; lnr != NULL; lnr = next)
414 {
415 listener_T *prev = NULL;
416
417 next = lnr->lr_next;
418 if (lnr->lr_id == 0)
419 remove_listener(buf, lnr, prev);
420 else
421 prev = lnr;
422 }
423
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200424 --textwinlock;
Bram Moolenaardda41442019-05-16 22:11:47 +0200425 list_unref(buf->b_recorded_changes);
426 buf->b_recorded_changes = NULL;
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200427
428 if (save_updating_screen)
429 updating_screen = TRUE;
430 else
431 after_updating_screen(TRUE);
432 recursive = FALSE;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200433}
Bram Moolenaar86173482019-10-01 17:02:16 +0200434
435/*
436 * Remove all listeners associated with "buf".
437 */
438 void
439remove_listeners(buf_T *buf)
440{
441 listener_T *lnr;
442 listener_T *next;
443
444 for (lnr = buf->b_listener; lnr != NULL; lnr = next)
445 {
446 next = lnr->lr_next;
447 free_callback(&lnr->lr_callback);
448 vim_free(lnr);
449 }
450 buf->b_listener = NULL;
451}
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200452#endif
453
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200454/*
455 * Common code for when a change was made.
456 * See changed_lines() for the arguments.
457 * Careful: may trigger autocommands that reload the buffer.
458 */
459 static void
460changed_common(
461 linenr_T lnum,
462 colnr_T col,
463 linenr_T lnume,
464 long xtra)
465{
466 win_T *wp;
467 tabpage_T *tp;
468 int i;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200469 int cols;
470 pos_T *p;
471 int add;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200472
473 // mark the buffer as modified
474 changed();
475
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200476#ifdef FEAT_EVAL
477 may_record_change(lnum, col, lnume, xtra);
478#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200479#ifdef FEAT_DIFF
480 if (curwin->w_p_diff && diff_internal())
481 curtab->tp_diff_update = TRUE;
482#endif
483
484 // set the '. mark
Bram Moolenaare1004402020-10-24 20:49:43 +0200485 if ((cmdmod.cmod_flags & CMOD_KEEPJUMPS) == 0)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200486 {
487 curbuf->b_last_change.lnum = lnum;
488 curbuf->b_last_change.col = col;
489
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200490 // Create a new entry if a new undo-able change was started or we
491 // don't have an entry yet.
492 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
493 {
494 if (curbuf->b_changelistlen == 0)
495 add = TRUE;
496 else
497 {
498 // Don't create a new entry when the line number is the same
499 // as the last one and the column is not too far away. Avoids
500 // creating many entries for typing "xxxxx".
501 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
502 if (p->lnum != lnum)
503 add = TRUE;
504 else
505 {
506 cols = comp_textwidth(FALSE);
507 if (cols == 0)
508 cols = 79;
509 add = (p->col + cols < col || col + cols < p->col);
510 }
511 }
512 if (add)
513 {
514 // This is the first of a new sequence of undo-able changes
515 // and it's at some distance of the last change. Use a new
516 // position in the changelist.
517 curbuf->b_new_change = FALSE;
518
519 if (curbuf->b_changelistlen == JUMPLISTSIZE)
520 {
521 // changelist is full: remove oldest entry
522 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
523 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
524 sizeof(pos_T) * (JUMPLISTSIZE - 1));
525 FOR_ALL_TAB_WINDOWS(tp, wp)
526 {
527 // Correct position in changelist for other windows on
528 // this buffer.
529 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
530 --wp->w_changelistidx;
531 }
532 }
533 FOR_ALL_TAB_WINDOWS(tp, wp)
534 {
535 // For other windows, if the position in the changelist is
536 // at the end it stays at the end.
537 if (wp->w_buffer == curbuf
538 && wp->w_changelistidx == curbuf->b_changelistlen)
539 ++wp->w_changelistidx;
540 }
541 ++curbuf->b_changelistlen;
542 }
543 }
544 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
545 curbuf->b_last_change;
546 // The current window is always after the last change, so that "g,"
547 // takes you back to it.
548 curwin->w_changelistidx = curbuf->b_changelistlen;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200549 }
550
551 FOR_ALL_TAB_WINDOWS(tp, wp)
552 {
553 if (wp->w_buffer == curbuf)
554 {
Bram Moolenaar94377372022-02-16 20:30:52 +0000555 linenr_T last = lnume + xtra - 1; // last line after the change
556
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200557 // Mark this window to be redrawn later.
558 if (wp->w_redr_type < VALID)
559 wp->w_redr_type = VALID;
560
561 // Check if a change in the buffer has invalidated the cached
562 // values for the cursor.
563#ifdef FEAT_FOLDING
564 // Update the folds for this window. Can't postpone this, because
565 // a following operator might work on the whole fold: ">>dd".
Bram Moolenaar94377372022-02-16 20:30:52 +0000566 foldUpdate(wp, lnum, last);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200567
568 // The change may cause lines above or below the change to become
569 // included in a fold. Set lnum/lnume to the first/last line that
570 // might be displayed differently.
571 // Set w_cline_folded here as an efficient way to update it when
572 // inserting lines just above a closed fold.
573 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
574 if (wp->w_cursor.lnum == lnum)
575 wp->w_cline_folded = i;
Bram Moolenaar94377372022-02-16 20:30:52 +0000576 i = hasFoldingWin(wp, last, NULL, &last, FALSE, NULL);
577 if (wp->w_cursor.lnum == last)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200578 wp->w_cline_folded = i;
579
580 // If the changed line is in a range of previously folded lines,
581 // compare with the first line in that range.
582 if (wp->w_cursor.lnum <= lnum)
583 {
584 i = find_wl_entry(wp, lnum);
585 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
586 changed_line_abv_curs_win(wp);
587 }
588#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200589 if (wp->w_cursor.lnum > lnum)
590 changed_line_abv_curs_win(wp);
591 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
592 changed_cline_bef_curs_win(wp);
593 if (wp->w_botline >= lnum)
594 {
Bram Moolenaar5bea41d2021-07-13 22:21:44 +0200595 if (xtra < 0)
596 invalidate_botline_win(wp);
597 else
598 // Assume that botline doesn't change (inserted lines make
599 // other lines scroll down below botline).
600 approximate_botline_win(wp);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200601 }
602
603 // Check if any w_lines[] entries have become invalid.
604 // For entries below the change: Correct the lnums for
605 // inserted/deleted lines. Makes it possible to stop displaying
606 // after the change.
607 for (i = 0; i < wp->w_lines_valid; ++i)
608 if (wp->w_lines[i].wl_valid)
609 {
610 if (wp->w_lines[i].wl_lnum >= lnum)
611 {
612 if (wp->w_lines[i].wl_lnum < lnume)
613 {
614 // line included in change
615 wp->w_lines[i].wl_valid = FALSE;
616 }
617 else if (xtra != 0)
618 {
619 // line below change
620 wp->w_lines[i].wl_lnum += xtra;
621#ifdef FEAT_FOLDING
622 wp->w_lines[i].wl_lastlnum += xtra;
623#endif
624 }
625 }
626#ifdef FEAT_FOLDING
627 else if (wp->w_lines[i].wl_lastlnum >= lnum)
628 {
629 // change somewhere inside this range of folded lines,
630 // may need to be redrawn
631 wp->w_lines[i].wl_valid = FALSE;
632 }
633#endif
634 }
635
636#ifdef FEAT_FOLDING
637 // Take care of side effects for setting w_topline when folds have
638 // changed. Esp. when the buffer was changed in another window.
639 if (hasAnyFolding(wp))
640 set_topline(wp, wp->w_topline);
641#endif
Bram Moolenaar11a58af2019-10-24 22:32:31 +0200642 // Relative numbering may require updating more.
643 if (wp->w_p_rnu)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200644 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar11a58af2019-10-24 22:32:31 +0200645#ifdef FEAT_SYN_HL
646 // Cursor line highlighting probably need to be updated with
647 // "VALID" if it's below the change.
648 // If the cursor line is inside the change we need to redraw more.
649 if (wp->w_p_cul)
650 {
651 if (xtra == 0)
652 redraw_win_later(wp, VALID);
653 else if (lnum <= wp->w_last_cursorline)
654 redraw_win_later(wp, SOME_VALID);
655 }
656#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200657 }
658 }
659
660 // Call update_screen() later, which checks out what needs to be redrawn,
661 // since it notices b_mod_set and then uses b_mod_*.
662 if (must_redraw < VALID)
663 must_redraw = VALID;
664
665 // when the cursor line is changed always trigger CursorMoved
666 if (lnum <= curwin->w_cursor.lnum
667 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
668 last_cursormoved.lnum = 0;
669}
670
671 static void
672changedOneline(buf_T *buf, linenr_T lnum)
673{
674 if (buf->b_mod_set)
675 {
676 // find the maximum area that must be redisplayed
677 if (lnum < buf->b_mod_top)
678 buf->b_mod_top = lnum;
679 else if (lnum >= buf->b_mod_bot)
680 buf->b_mod_bot = lnum + 1;
681 }
682 else
683 {
684 // set the area that must be redisplayed to one line
685 buf->b_mod_set = TRUE;
686 buf->b_mod_top = lnum;
687 buf->b_mod_bot = lnum + 1;
688 buf->b_mod_xlines = 0;
689 }
690}
691
692/*
693 * Changed bytes within a single line for the current buffer.
694 * - marks the windows on this buffer to be redisplayed
695 * - marks the buffer changed by calling changed()
696 * - invalidates cached values
697 * Careful: may trigger autocommands that reload the buffer.
698 */
699 void
700changed_bytes(linenr_T lnum, colnr_T col)
701{
702 changedOneline(curbuf, lnum);
703 changed_common(lnum, col, lnum + 1, 0L);
704
705#ifdef FEAT_DIFF
706 // Diff highlighting in other diff windows may need to be updated too.
707 if (curwin->w_p_diff)
708 {
709 win_T *wp;
710 linenr_T wlnum;
711
712 FOR_ALL_WINDOWS(wp)
713 if (wp->w_p_diff && wp != curwin)
714 {
715 redraw_win_later(wp, VALID);
716 wlnum = diff_lnum_win(lnum, wp);
717 if (wlnum > 0)
718 changedOneline(wp->w_buffer, wlnum);
719 }
720 }
721#endif
722}
723
724/*
725 * Like changed_bytes() but also adjust text properties for "added" bytes.
726 * When "added" is negative text was deleted.
727 */
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200728 void
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200729inserted_bytes(linenr_T lnum, colnr_T col, int added UNUSED)
730{
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100731#ifdef FEAT_PROP_POPUP
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200732 if (curbuf->b_has_textprop && added != 0)
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200733 adjust_prop_columns(lnum, col, added, 0);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200734#endif
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200735
736 changed_bytes(lnum, col);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200737}
738
739/*
740 * Appended "count" lines below line "lnum" in the current buffer.
741 * Must be called AFTER the change and after mark_adjust().
742 * Takes care of marking the buffer to be redrawn and sets the changed flag.
743 */
744 void
745appended_lines(linenr_T lnum, long count)
746{
747 changed_lines(lnum + 1, 0, lnum + 1, count);
748}
749
750/*
751 * Like appended_lines(), but adjust marks first.
752 */
753 void
754appended_lines_mark(linenr_T lnum, long count)
755{
756 // Skip mark_adjust when adding a line after the last one, there can't
757 // be marks there. But it's still needed in diff mode.
758 if (lnum + count < curbuf->b_ml.ml_line_count
759#ifdef FEAT_DIFF
760 || curwin->w_p_diff
761#endif
762 )
763 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
764 changed_lines(lnum + 1, 0, lnum + 1, count);
765}
766
767/*
768 * Deleted "count" lines at line "lnum" in the current buffer.
769 * Must be called AFTER the change and after mark_adjust().
770 * Takes care of marking the buffer to be redrawn and sets the changed flag.
771 */
772 void
773deleted_lines(linenr_T lnum, long count)
774{
775 changed_lines(lnum, 0, lnum + count, -count);
776}
777
778/*
779 * Like deleted_lines(), but adjust marks first.
780 * Make sure the cursor is on a valid line before calling, a GUI callback may
781 * be triggered to display the cursor.
782 */
783 void
784deleted_lines_mark(linenr_T lnum, long count)
785{
786 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
787 changed_lines(lnum, 0, lnum + count, -count);
788}
789
790/*
791 * Marks the area to be redrawn after a change.
792 */
Bram Moolenaar1764faa2021-05-16 20:18:57 +0200793 void
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200794changed_lines_buf(
795 buf_T *buf,
796 linenr_T lnum, // first line with change
797 linenr_T lnume, // line below last changed line
798 long xtra) // number of extra lines (negative when deleting)
799{
800 if (buf->b_mod_set)
801 {
802 // find the maximum area that must be redisplayed
803 if (lnum < buf->b_mod_top)
804 buf->b_mod_top = lnum;
805 if (lnum < buf->b_mod_bot)
806 {
807 // adjust old bot position for xtra lines
808 buf->b_mod_bot += xtra;
809 if (buf->b_mod_bot < lnum)
810 buf->b_mod_bot = lnum;
811 }
812 if (lnume + xtra > buf->b_mod_bot)
813 buf->b_mod_bot = lnume + xtra;
814 buf->b_mod_xlines += xtra;
815 }
816 else
817 {
818 // set the area that must be redisplayed
819 buf->b_mod_set = TRUE;
820 buf->b_mod_top = lnum;
821 buf->b_mod_bot = lnume + xtra;
822 buf->b_mod_xlines = xtra;
823 }
824}
825
826/*
827 * Changed lines for the current buffer.
828 * Must be called AFTER the change and after mark_adjust().
829 * - mark the buffer changed by calling changed()
830 * - mark the windows on this buffer to be redisplayed
831 * - invalidate cached values
832 * "lnum" is the first line that needs displaying, "lnume" the first line
833 * below the changed lines (BEFORE the change).
834 * When only inserting lines, "lnum" and "lnume" are equal.
835 * Takes care of calling changed() and updating b_mod_*.
836 * Careful: may trigger autocommands that reload the buffer.
837 */
838 void
839changed_lines(
840 linenr_T lnum, // first line with change
841 colnr_T col, // column in first line with change
842 linenr_T lnume, // line below last changed line
843 long xtra) // number of extra lines (negative when deleting)
844{
845 changed_lines_buf(curbuf, lnum, lnume, xtra);
846
847#ifdef FEAT_DIFF
848 if (xtra == 0 && curwin->w_p_diff && !diff_internal())
849 {
850 // When the number of lines doesn't change then mark_adjust() isn't
851 // called and other diff buffers still need to be marked for
852 // displaying.
853 win_T *wp;
854 linenr_T wlnum;
855
856 FOR_ALL_WINDOWS(wp)
857 if (wp->w_p_diff && wp != curwin)
858 {
859 redraw_win_later(wp, VALID);
860 wlnum = diff_lnum_win(lnum, wp);
861 if (wlnum > 0)
862 changed_lines_buf(wp->w_buffer, wlnum,
863 lnume - lnum + wlnum, 0L);
864 }
865 }
866#endif
867
868 changed_common(lnum, col, lnume, xtra);
869}
870
871/*
872 * Called when the changed flag must be reset for buffer "buf".
873 * When "ff" is TRUE also reset 'fileformat'.
Bram Moolenaarc024b462019-06-08 18:07:21 +0200874 * When "always_inc_changedtick" is TRUE b:changedtick is incremented also when
875 * the changed flag was off.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200876 */
877 void
Bram Moolenaarc024b462019-06-08 18:07:21 +0200878unchanged(buf_T *buf, int ff, int always_inc_changedtick)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200879{
880 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
881 {
882 buf->b_changed = 0;
883 ml_setflags(buf);
884 if (ff)
885 save_file_ff(buf);
886 check_status(buf);
887 redraw_tabline = TRUE;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200888 need_maketitle = TRUE; // set window title later
Bram Moolenaarc024b462019-06-08 18:07:21 +0200889 ++CHANGEDTICK(buf);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200890 }
Bram Moolenaarc024b462019-06-08 18:07:21 +0200891 else if (always_inc_changedtick)
892 ++CHANGEDTICK(buf);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200893#ifdef FEAT_NETBEANS_INTG
894 netbeans_unmodified(buf);
895#endif
896}
897
898/*
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100899 * Save the current values of 'fileformat' and 'fileencoding', so that we know
900 * the file must be considered changed when the value is different.
901 */
902 void
903save_file_ff(buf_T *buf)
904{
905 buf->b_start_ffc = *buf->b_p_ff;
906 buf->b_start_eol = buf->b_p_eol;
907 buf->b_start_bomb = buf->b_p_bomb;
908
Bram Moolenaarc667da52019-11-30 20:52:27 +0100909 // Only use free/alloc when necessary, they take time.
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100910 if (buf->b_start_fenc == NULL
911 || STRCMP(buf->b_start_fenc, buf->b_p_fenc) != 0)
912 {
913 vim_free(buf->b_start_fenc);
914 buf->b_start_fenc = vim_strsave(buf->b_p_fenc);
915 }
916}
917
918/*
919 * Return TRUE if 'fileformat' and/or 'fileencoding' has a different value
920 * from when editing started (save_file_ff() called).
921 * Also when 'endofline' was changed and 'binary' is set, or when 'bomb' was
922 * changed and 'binary' is not set.
923 * Also when 'endofline' was changed and 'fixeol' is not set.
924 * When "ignore_empty" is true don't consider a new, empty buffer to be
925 * changed.
926 */
927 int
928file_ff_differs(buf_T *buf, int ignore_empty)
929{
Bram Moolenaarc667da52019-11-30 20:52:27 +0100930 // In a buffer that was never loaded the options are not valid.
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100931 if (buf->b_flags & BF_NEVERLOADED)
932 return FALSE;
933 if (ignore_empty
934 && (buf->b_flags & BF_NEW)
935 && buf->b_ml.ml_line_count == 1
936 && *ml_get_buf(buf, (linenr_T)1, FALSE) == NUL)
937 return FALSE;
938 if (buf->b_start_ffc != *buf->b_p_ff)
939 return TRUE;
940 if ((buf->b_p_bin || !buf->b_p_fixeol) && buf->b_start_eol != buf->b_p_eol)
941 return TRUE;
942 if (!buf->b_p_bin && buf->b_start_bomb != buf->b_p_bomb)
943 return TRUE;
944 if (buf->b_start_fenc == NULL)
945 return (*buf->b_p_fenc != NUL);
946 return (STRCMP(buf->b_start_fenc, buf->b_p_fenc) != 0);
947}
948
949/*
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200950 * Insert string "p" at the cursor position. Stops at a NUL byte.
951 * Handles Replace mode and multi-byte characters.
952 */
953 void
954ins_bytes(char_u *p)
955{
956 ins_bytes_len(p, (int)STRLEN(p));
957}
958
959/*
960 * Insert string "p" with length "len" at the cursor position.
961 * Handles Replace mode and multi-byte characters.
962 */
963 void
964ins_bytes_len(char_u *p, int len)
965{
966 int i;
967 int n;
968
969 if (has_mbyte)
970 for (i = 0; i < len; i += n)
971 {
972 if (enc_utf8)
973 // avoid reading past p[len]
974 n = utfc_ptr2len_len(p + i, len - i);
975 else
976 n = (*mb_ptr2len)(p + i);
977 ins_char_bytes(p + i, n);
978 }
979 else
980 for (i = 0; i < len; ++i)
981 ins_char(p[i]);
982}
983
984/*
985 * Insert or replace a single character at the cursor position.
986 * When in REPLACE or VREPLACE mode, replace any existing character.
987 * Caller must have prepared for undo.
988 * For multi-byte characters we get the whole character, the caller must
989 * convert bytes to a character.
990 */
991 void
992ins_char(int c)
993{
994 char_u buf[MB_MAXBYTES + 1];
995 int n = (*mb_char2bytes)(c, buf);
996
997 // When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
998 // Happens for CTRL-Vu9900.
999 if (buf[0] == 0)
1000 buf[0] = '\n';
1001
1002 ins_char_bytes(buf, n);
1003}
1004
1005 void
1006ins_char_bytes(char_u *buf, int charlen)
1007{
1008 int c = buf[0];
1009 int newlen; // nr of bytes inserted
1010 int oldlen; // nr of bytes deleted (0 when not replacing)
1011 char_u *p;
1012 char_u *newp;
1013 char_u *oldp;
1014 int linelen; // length of old line including NUL
1015 colnr_T col;
1016 linenr_T lnum = curwin->w_cursor.lnum;
1017 int i;
1018
1019 // Break tabs if needed.
1020 if (virtual_active() && curwin->w_cursor.coladd > 0)
1021 coladvance_force(getviscol());
1022
1023 col = curwin->w_cursor.col;
1024 oldp = ml_get(lnum);
1025 linelen = (int)STRLEN(oldp) + 1;
1026
1027 // The lengths default to the values for when not replacing.
1028 oldlen = 0;
1029 newlen = charlen;
1030
1031 if (State & REPLACE_FLAG)
1032 {
1033 if (State & VREPLACE_FLAG)
1034 {
1035 colnr_T new_vcol = 0; // init for GCC
1036 colnr_T vcol;
1037 int old_list;
1038
1039 // Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
1040 // Returns the old value of list, so when finished,
1041 // curwin->w_p_list should be set back to this.
1042 old_list = curwin->w_p_list;
1043 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
1044 curwin->w_p_list = FALSE;
1045
1046 // In virtual replace mode each character may replace one or more
1047 // characters (zero if it's a TAB). Count the number of bytes to
1048 // be deleted to make room for the new character, counting screen
1049 // cells. May result in adding spaces to fill a gap.
1050 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
1051 new_vcol = vcol + chartabsize(buf, vcol);
1052 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
1053 {
1054 vcol += chartabsize(oldp + col + oldlen, vcol);
1055 // Don't need to remove a TAB that takes us to the right
1056 // position.
1057 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
1058 break;
1059 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
1060 // Deleted a bit too much, insert spaces.
1061 if (vcol > new_vcol)
1062 newlen += vcol - new_vcol;
1063 }
1064 curwin->w_p_list = old_list;
1065 }
1066 else if (oldp[col] != NUL)
1067 {
1068 // normal replace
1069 oldlen = (*mb_ptr2len)(oldp + col);
1070 }
1071
1072
1073 // Push the replaced bytes onto the replace stack, so that they can be
1074 // put back when BS is used. The bytes of a multi-byte character are
1075 // done the other way around, so that the first byte is popped off
1076 // first (it tells the byte length of the character).
1077 replace_push(NUL);
1078 for (i = 0; i < oldlen; ++i)
1079 {
1080 if (has_mbyte)
1081 i += replace_push_mb(oldp + col + i) - 1;
1082 else
1083 replace_push(oldp[col + i]);
1084 }
1085 }
1086
Bram Moolenaar964b3742019-05-24 18:54:09 +02001087 newp = alloc(linelen + newlen - oldlen);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001088 if (newp == NULL)
1089 return;
1090
1091 // Copy bytes before the cursor.
1092 if (col > 0)
1093 mch_memmove(newp, oldp, (size_t)col);
1094
1095 // Copy bytes after the changed character(s).
1096 p = newp + col;
1097 if (linelen > col + oldlen)
1098 mch_memmove(p + newlen, oldp + col + oldlen,
1099 (size_t)(linelen - col - oldlen));
1100
1101 // Insert or overwrite the new character.
1102 mch_memmove(p, buf, charlen);
1103 i = charlen;
1104
1105 // Fill with spaces when necessary.
1106 while (i < newlen)
1107 p[i++] = ' ';
1108
1109 // Replace the line in the buffer.
1110 ml_replace(lnum, newp, FALSE);
1111
1112 // mark the buffer as changed and prepare for displaying
1113 inserted_bytes(lnum, col, newlen - oldlen);
1114
1115 // If we're in Insert or Replace mode and 'showmatch' is set, then briefly
1116 // show the match for right parens and braces.
1117 if (p_sm && (State & INSERT)
1118 && msg_silent == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001119 && !ins_compl_active())
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001120 {
1121 if (has_mbyte)
1122 showmatch(mb_ptr2char(buf));
1123 else
1124 showmatch(c);
1125 }
1126
1127#ifdef FEAT_RIGHTLEFT
1128 if (!p_ri || (State & REPLACE_FLAG))
1129#endif
1130 {
1131 // Normal insert: move cursor right
1132 curwin->w_cursor.col += charlen;
1133 }
1134
1135 // TODO: should try to update w_row here, to avoid recomputing it later.
1136}
1137
1138/*
1139 * Insert a string at the cursor position.
1140 * Note: Does NOT handle Replace mode.
1141 * Caller must have prepared for undo.
1142 */
1143 void
1144ins_str(char_u *s)
1145{
1146 char_u *oldp, *newp;
1147 int newlen = (int)STRLEN(s);
1148 int oldlen;
1149 colnr_T col;
1150 linenr_T lnum = curwin->w_cursor.lnum;
1151
1152 if (virtual_active() && curwin->w_cursor.coladd > 0)
1153 coladvance_force(getviscol());
1154
1155 col = curwin->w_cursor.col;
1156 oldp = ml_get(lnum);
1157 oldlen = (int)STRLEN(oldp);
1158
Bram Moolenaar964b3742019-05-24 18:54:09 +02001159 newp = alloc(oldlen + newlen + 1);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001160 if (newp == NULL)
1161 return;
1162 if (col > 0)
1163 mch_memmove(newp, oldp, (size_t)col);
1164 mch_memmove(newp + col, s, (size_t)newlen);
1165 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
1166 ml_replace(lnum, newp, FALSE);
1167 inserted_bytes(lnum, col, newlen);
1168 curwin->w_cursor.col += newlen;
1169}
1170
1171/*
1172 * Delete one character under the cursor.
1173 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
1174 * Caller must have prepared for undo.
1175 *
1176 * return FAIL for failure, OK otherwise
1177 */
1178 int
1179del_char(int fixpos)
1180{
1181 if (has_mbyte)
1182 {
1183 // Make sure the cursor is at the start of a character.
1184 mb_adjust_cursor();
1185 if (*ml_get_cursor() == NUL)
1186 return FAIL;
1187 return del_chars(1L, fixpos);
1188 }
1189 return del_bytes(1L, fixpos, TRUE);
1190}
1191
1192/*
1193 * Like del_bytes(), but delete characters instead of bytes.
1194 */
1195 int
1196del_chars(long count, int fixpos)
1197{
1198 long bytes = 0;
1199 long i;
1200 char_u *p;
1201 int l;
1202
1203 p = ml_get_cursor();
1204 for (i = 0; i < count && *p != NUL; ++i)
1205 {
1206 l = (*mb_ptr2len)(p);
1207 bytes += l;
1208 p += l;
1209 }
1210 return del_bytes(bytes, fixpos, TRUE);
1211}
1212
1213/*
1214 * Delete "count" bytes under the cursor.
1215 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
1216 * Caller must have prepared for undo.
1217 *
1218 * Return FAIL for failure, OK otherwise.
1219 */
1220 int
1221del_bytes(
1222 long count,
1223 int fixpos_arg,
1224 int use_delcombine UNUSED) // 'delcombine' option applies
1225{
1226 char_u *oldp, *newp;
1227 colnr_T oldlen;
1228 colnr_T newlen;
1229 linenr_T lnum = curwin->w_cursor.lnum;
1230 colnr_T col = curwin->w_cursor.col;
1231 int alloc_newp;
1232 long movelen;
1233 int fixpos = fixpos_arg;
1234
1235 oldp = ml_get(lnum);
1236 oldlen = (int)STRLEN(oldp);
1237
1238 // Can't do anything when the cursor is on the NUL after the line.
1239 if (col >= oldlen)
1240 return FAIL;
1241
1242 // If "count" is zero there is nothing to do.
1243 if (count == 0)
1244 return OK;
1245
1246 // If "count" is negative the caller must be doing something wrong.
1247 if (count < 1)
1248 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001249 siemsg(e_invalid_count_for_del_bytes_nr, count);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001250 return FAIL;
1251 }
1252
1253 // If 'delcombine' is set and deleting (less than) one character, only
1254 // delete the last combining character.
1255 if (p_deco && use_delcombine && enc_utf8
1256 && utfc_ptr2len(oldp + col) >= count)
1257 {
1258 int cc[MAX_MCO];
1259 int n;
1260
1261 (void)utfc_ptr2char(oldp + col, cc);
1262 if (cc[0] != NUL)
1263 {
1264 // Find the last composing char, there can be several.
1265 n = col;
1266 do
1267 {
1268 col = n;
1269 count = utf_ptr2len(oldp + n);
1270 n += count;
1271 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
1272 fixpos = 0;
1273 }
1274 }
1275
1276 // When count is too big, reduce it.
1277 movelen = (long)oldlen - (long)col - count + 1; // includes trailing NUL
1278 if (movelen <= 1)
1279 {
1280 // If we just took off the last character of a non-blank line, and
1281 // fixpos is TRUE, we don't want to end up positioned at the NUL,
1282 // unless "restart_edit" is set or 'virtualedit' contains "onemore".
1283 if (col > 0 && fixpos && restart_edit == 0
Gary Johnson53ba05b2021-07-26 22:19:10 +02001284 && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001285 {
1286 --curwin->w_cursor.col;
1287 curwin->w_cursor.coladd = 0;
1288 if (has_mbyte)
1289 curwin->w_cursor.col -=
1290 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
1291 }
1292 count = oldlen - col;
1293 movelen = 1;
1294 }
1295 newlen = oldlen - count;
1296
1297 // If the old line has been allocated the deletion can be done in the
1298 // existing line. Otherwise a new line has to be allocated
1299 // Can't do this when using Netbeans, because we would need to invoke
1300 // netbeans_removed(), which deallocates the line. Let ml_replace() take
1301 // care of notifying Netbeans.
1302#ifdef FEAT_NETBEANS_INTG
1303 if (netbeans_active())
1304 alloc_newp = TRUE;
1305 else
1306#endif
1307 alloc_newp = !ml_line_alloced(); // check if oldp was allocated
1308 if (!alloc_newp)
1309 newp = oldp; // use same allocated memory
1310 else
1311 { // need to allocate a new line
Bram Moolenaar964b3742019-05-24 18:54:09 +02001312 newp = alloc(newlen + 1);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001313 if (newp == NULL)
1314 return FAIL;
1315 mch_memmove(newp, oldp, (size_t)col);
1316 }
1317 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
1318 if (alloc_newp)
1319 ml_replace(lnum, newp, FALSE);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001320#ifdef FEAT_PROP_POPUP
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001321 else
1322 {
1323 // Also move any following text properties.
1324 if (oldlen + 1 < curbuf->b_ml.ml_line_len)
1325 mch_memmove(newp + newlen + 1, oldp + oldlen + 1,
1326 (size_t)curbuf->b_ml.ml_line_len - oldlen - 1);
1327 curbuf->b_ml.ml_line_len -= count;
1328 }
1329#endif
1330
1331 // mark the buffer as changed and prepare for displaying
Bram Moolenaar12f20032020-02-26 22:06:00 +01001332 inserted_bytes(lnum, col, -count);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001333
1334 return OK;
1335}
1336
1337/*
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001338 * open_line: Add a new line below or above the current line.
1339 *
1340 * For VREPLACE mode, we only add a new line when we get to the end of the
1341 * file, otherwise we just start replacing the next line.
1342 *
1343 * Caller must take care of undo. Since VREPLACE may affect any number of
1344 * lines however, it may call u_save_cursor() again when starting to change a
1345 * new line.
1346 * "flags": OPENLINE_DELSPACES delete spaces after cursor
1347 * OPENLINE_DO_COM format comments
1348 * OPENLINE_KEEPTRAIL keep trailing spaces
1349 * OPENLINE_MARKFIX adjust mark positions after the line break
1350 * OPENLINE_COM_LIST format comments with list or 2nd line indent
1351 *
1352 * "second_line_indent": indent for after ^^D in Insert mode or if flag
1353 * OPENLINE_COM_LIST
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001354 * "did_do_comment" is set to TRUE when intentionally putting the comment
1355 * leader in fromt of the new line.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001356 *
1357 * Return OK for success, FAIL for failure
1358 */
1359 int
1360open_line(
1361 int dir, // FORWARD or BACKWARD
1362 int flags,
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001363 int second_line_indent,
1364 int *did_do_comment UNUSED)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001365{
1366 char_u *saved_line; // copy of the original line
1367 char_u *next_line = NULL; // copy of the next line
1368 char_u *p_extra = NULL; // what goes to next line
1369 int less_cols = 0; // less columns for mark in new line
1370 int less_cols_off = 0; // columns to skip for mark adjust
1371 pos_T old_cursor; // old cursor position
1372 int newcol = 0; // new cursor column
1373 int newindent = 0; // auto-indent of the new line
1374 int n;
1375 int trunc_line = FALSE; // truncate current line afterwards
1376 int retval = FAIL; // return value
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001377 int extra_len = 0; // length of p_extra string
1378 int lead_len; // length of comment leader
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001379 int comment_start = 0; // start index of the comment leader
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001380 char_u *lead_flags; // position in 'comments' for comment leader
1381 char_u *leader = NULL; // copy of comment leader
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001382 char_u *allocated = NULL; // allocated memory
1383 char_u *p;
1384 int saved_char = NUL; // init for GCC
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001385 pos_T *pos;
Bram Moolenaard2439e02021-12-12 19:10:44 +00001386#ifdef FEAT_CINDENT
1387 int do_cindent;
1388#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001389#ifdef FEAT_SMARTINDENT
1390 int do_si = (!p_paste && curbuf->b_p_si
1391# ifdef FEAT_CINDENT
1392 && !curbuf->b_p_cin
1393# endif
1394# ifdef FEAT_EVAL
1395 && *curbuf->b_p_inde == NUL
1396# endif
1397 );
1398 int no_si = FALSE; // reset did_si afterwards
1399 int first_char = NUL; // init for GCC
1400#endif
1401#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1402 int vreplace_mode;
1403#endif
1404 int did_append; // appended a new line
1405 int saved_pi = curbuf->b_p_pi; // copy of preserveindent setting
1406
1407 // make a copy of the current line so we can mess with it
1408 saved_line = vim_strsave(ml_get_curline());
Bram Moolenaarc667da52019-11-30 20:52:27 +01001409 if (saved_line == NULL) // out of memory!
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001410 return FALSE;
1411
1412 if (State & VREPLACE_FLAG)
1413 {
1414 // With VREPLACE we make a copy of the next line, which we will be
1415 // starting to replace. First make the new line empty and let vim play
1416 // with the indenting and comment leader to its heart's content. Then
1417 // we grab what it ended up putting on the new line, put back the
1418 // original line, and call ins_char() to put each new character onto
1419 // the line, replacing what was there before and pushing the right
1420 // stuff onto the replace stack. -- webb.
1421 if (curwin->w_cursor.lnum < orig_line_count)
1422 next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1));
1423 else
1424 next_line = vim_strsave((char_u *)"");
1425 if (next_line == NULL) // out of memory!
1426 goto theend;
1427
1428 // In VREPLACE mode, a NL replaces the rest of the line, and starts
1429 // replacing the next line, so push all of the characters left on the
1430 // line onto the replace stack. We'll push any other characters that
1431 // might be replaced at the start of the next line (due to autoindent
1432 // etc) a bit later.
1433 replace_push(NUL); // Call twice because BS over NL expects it
1434 replace_push(NUL);
1435 p = saved_line + curwin->w_cursor.col;
1436 while (*p != NUL)
1437 {
1438 if (has_mbyte)
1439 p += replace_push_mb(p);
1440 else
1441 replace_push(*p++);
1442 }
1443 saved_line[curwin->w_cursor.col] = NUL;
1444 }
1445
1446 if ((State & INSERT) && !(State & VREPLACE_FLAG))
1447 {
1448 p_extra = saved_line + curwin->w_cursor.col;
1449#ifdef FEAT_SMARTINDENT
1450 if (do_si) // need first char after new line break
1451 {
1452 p = skipwhite(p_extra);
1453 first_char = *p;
1454 }
1455#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001456 extra_len = (int)STRLEN(p_extra);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001457 saved_char = *p_extra;
1458 *p_extra = NUL;
1459 }
1460
1461 u_clearline(); // cannot do "U" command when adding lines
1462#ifdef FEAT_SMARTINDENT
1463 did_si = FALSE;
1464#endif
1465 ai_col = 0;
1466
1467 // If we just did an auto-indent, then we didn't type anything on
1468 // the prior line, and it should be truncated. Do this even if 'ai' is not
1469 // set because automatically inserting a comment leader also sets did_ai.
1470 if (dir == FORWARD && did_ai)
1471 trunc_line = TRUE;
1472
1473 // If 'autoindent' and/or 'smartindent' is set, try to figure out what
1474 // indent to use for the new line.
1475 if (curbuf->b_p_ai
1476#ifdef FEAT_SMARTINDENT
1477 || do_si
1478#endif
1479 )
1480 {
1481 // count white space on current line
1482#ifdef FEAT_VARTABS
1483 newindent = get_indent_str_vtab(saved_line, curbuf->b_p_ts,
1484 curbuf->b_p_vts_array, FALSE);
1485#else
1486 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts, FALSE);
1487#endif
1488 if (newindent == 0 && !(flags & OPENLINE_COM_LIST))
1489 newindent = second_line_indent; // for ^^D command in insert mode
1490
1491#ifdef FEAT_SMARTINDENT
1492 // Do smart indenting.
1493 // In insert/replace mode (only when dir == FORWARD)
1494 // we may move some text to the next line. If it starts with '{'
1495 // don't add an indent. Fixes inserting a NL before '{' in line
1496 // "if (condition) {"
1497 if (!trunc_line && do_si && *saved_line != NUL
1498 && (p_extra == NULL || first_char != '{'))
1499 {
1500 char_u *ptr;
1501 char_u last_char;
1502
1503 old_cursor = curwin->w_cursor;
1504 ptr = saved_line;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001505 if (flags & OPENLINE_DO_COM)
1506 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
1507 else
1508 lead_len = 0;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001509 if (dir == FORWARD)
1510 {
1511 // Skip preprocessor directives, unless they are
1512 // recognised as comments.
Bram Moolenaar8c96af92019-09-28 19:05:57 +02001513 if ( lead_len == 0 && ptr[0] == '#')
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001514 {
1515 while (ptr[0] == '#' && curwin->w_cursor.lnum > 1)
1516 ptr = ml_get(--curwin->w_cursor.lnum);
1517 newindent = get_indent();
1518 }
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001519 if (flags & OPENLINE_DO_COM)
1520 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
1521 else
1522 lead_len = 0;
1523 if (lead_len > 0)
1524 {
1525 // This case gets the following right:
1526 // /*
1527 // * A comment (read '\' as '/').
1528 // */
1529 // #define IN_THE_WAY
1530 // This should line up here;
1531 p = skipwhite(ptr);
1532 if (p[0] == '/' && p[1] == '*')
1533 p++;
1534 if (p[0] == '*')
1535 {
1536 for (p++; *p; p++)
1537 {
1538 if (p[0] == '/' && p[-1] == '*')
1539 {
1540 // End of C comment, indent should line up
1541 // with the line containing the start of
1542 // the comment
1543 curwin->w_cursor.col = (colnr_T)(p - ptr);
1544 if ((pos = findmatch(NULL, NUL)) != NULL)
1545 {
1546 curwin->w_cursor.lnum = pos->lnum;
1547 newindent = get_indent();
1548 }
1549 }
1550 }
1551 }
1552 }
1553 else // Not a comment line
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001554 {
1555 // Find last non-blank in line
1556 p = ptr + STRLEN(ptr) - 1;
1557 while (p > ptr && VIM_ISWHITE(*p))
1558 --p;
1559 last_char = *p;
1560
1561 // find the character just before the '{' or ';'
1562 if (last_char == '{' || last_char == ';')
1563 {
1564 if (p > ptr)
1565 --p;
1566 while (p > ptr && VIM_ISWHITE(*p))
1567 --p;
1568 }
1569 // Try to catch lines that are split over multiple
1570 // lines. eg:
1571 // if (condition &&
1572 // condition) {
1573 // Should line up here!
1574 // }
1575 if (*p == ')')
1576 {
1577 curwin->w_cursor.col = (colnr_T)(p - ptr);
1578 if ((pos = findmatch(NULL, '(')) != NULL)
1579 {
1580 curwin->w_cursor.lnum = pos->lnum;
1581 newindent = get_indent();
1582 ptr = ml_get_curline();
1583 }
1584 }
1585 // If last character is '{' do indent, without
1586 // checking for "if" and the like.
1587 if (last_char == '{')
1588 {
1589 did_si = TRUE; // do indent
1590 no_si = TRUE; // don't delete it when '{' typed
1591 }
1592 // Look for "if" and the like, use 'cinwords'.
1593 // Don't do this if the previous line ended in ';' or
1594 // '}'.
1595 else if (last_char != ';' && last_char != '}'
1596 && cin_is_cinword(ptr))
1597 did_si = TRUE;
1598 }
1599 }
1600 else // dir == BACKWARD
1601 {
1602 // Skip preprocessor directives, unless they are
1603 // recognised as comments.
Bram Moolenaar8c96af92019-09-28 19:05:57 +02001604 if (lead_len == 0 && ptr[0] == '#')
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001605 {
1606 int was_backslashed = FALSE;
1607
1608 while ((ptr[0] == '#' || was_backslashed) &&
1609 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
1610 {
1611 if (*ptr && ptr[STRLEN(ptr) - 1] == '\\')
1612 was_backslashed = TRUE;
1613 else
1614 was_backslashed = FALSE;
1615 ptr = ml_get(++curwin->w_cursor.lnum);
1616 }
1617 if (was_backslashed)
1618 newindent = 0; // Got to end of file
1619 else
1620 newindent = get_indent();
1621 }
1622 p = skipwhite(ptr);
1623 if (*p == '}') // if line starts with '}': do indent
1624 did_si = TRUE;
1625 else // can delete indent when '{' typed
1626 can_si_back = TRUE;
1627 }
1628 curwin->w_cursor = old_cursor;
1629 }
1630 if (do_si)
1631 can_si = TRUE;
1632#endif // FEAT_SMARTINDENT
1633
1634 did_ai = TRUE;
1635 }
1636
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001637#ifdef FEAT_CINDENT
1638 // May do indenting after opening a new line.
1639 do_cindent = !p_paste && (curbuf->b_p_cin
K.Takata6e1d31e2022-02-03 13:05:32 +00001640# ifdef FEAT_EVAL
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001641 || *curbuf->b_p_inde != NUL
K.Takata6e1d31e2022-02-03 13:05:32 +00001642# endif
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001643 )
1644 && in_cinkeys(dir == FORWARD
1645 ? KEY_OPEN_FORW
1646 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum));
1647#endif
1648
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001649 // Find out if the current line starts with a comment leader.
1650 // This may then be inserted in front of the new line.
1651 end_comment_pending = NUL;
1652 if (flags & OPENLINE_DO_COM)
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001653 {
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001654 lead_len = get_leader_len(saved_line, &lead_flags,
1655 dir == BACKWARD, TRUE);
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001656#ifdef FEAT_CINDENT
Bram Moolenaar5ea5f372021-12-29 15:15:47 +00001657 if (lead_len == 0 && do_cindent && dir == FORWARD)
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001658 {
Bram Moolenaar5ea5f372021-12-29 15:15:47 +00001659 // Check for a line comment after code.
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001660 comment_start = check_linecomment(saved_line);
1661 if (comment_start != MAXCOL)
1662 {
1663 lead_len = get_leader_len(saved_line + comment_start,
Bram Moolenaar5ea5f372021-12-29 15:15:47 +00001664 &lead_flags, FALSE, TRUE);
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001665 if (lead_len != 0)
1666 {
1667 lead_len += comment_start;
1668 if (did_do_comment != NULL)
1669 *did_do_comment = TRUE;
1670 }
1671 }
1672 }
1673#endif
1674 }
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001675 else
1676 lead_len = 0;
1677 if (lead_len > 0)
1678 {
1679 char_u *lead_repl = NULL; // replaces comment leader
1680 int lead_repl_len = 0; // length of *lead_repl
1681 char_u lead_middle[COM_MAX_LEN]; // middle-comment string
1682 char_u lead_end[COM_MAX_LEN]; // end-comment string
1683 char_u *comment_end = NULL; // where lead_end has been found
1684 int extra_space = FALSE; // append extra space
1685 int current_flag;
1686 int require_blank = FALSE; // requires blank after middle
1687 char_u *p2;
1688
1689 // If the comment leader has the start, middle or end flag, it may not
1690 // be used or may be replaced with the middle leader.
1691 for (p = lead_flags; *p && *p != ':'; ++p)
1692 {
1693 if (*p == COM_BLANK)
1694 {
1695 require_blank = TRUE;
1696 continue;
1697 }
1698 if (*p == COM_START || *p == COM_MIDDLE)
1699 {
1700 current_flag = *p;
1701 if (*p == COM_START)
1702 {
1703 // Doing "O" on a start of comment does not insert leader.
1704 if (dir == BACKWARD)
1705 {
1706 lead_len = 0;
1707 break;
1708 }
1709
1710 // find start of middle part
1711 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
1712 require_blank = FALSE;
1713 }
1714
1715 // Isolate the strings of the middle and end leader.
Bram Moolenaarc667da52019-11-30 20:52:27 +01001716 while (*p && p[-1] != ':') // find end of middle flags
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001717 {
1718 if (*p == COM_BLANK)
1719 require_blank = TRUE;
1720 ++p;
1721 }
1722 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
1723
1724 while (*p && p[-1] != ':') // find end of end flags
1725 {
1726 // Check whether we allow automatic ending of comments
1727 if (*p == COM_AUTO_END)
1728 end_comment_pending = -1; // means we want to set it
1729 ++p;
1730 }
1731 n = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
1732
1733 if (end_comment_pending == -1) // we can set it now
1734 end_comment_pending = lead_end[n - 1];
1735
1736 // If the end of the comment is in the same line, don't use
1737 // the comment leader.
1738 if (dir == FORWARD)
1739 {
1740 for (p = saved_line + lead_len; *p; ++p)
1741 if (STRNCMP(p, lead_end, n) == 0)
1742 {
1743 comment_end = p;
1744 lead_len = 0;
1745 break;
1746 }
1747 }
1748
1749 // Doing "o" on a start of comment inserts the middle leader.
1750 if (lead_len > 0)
1751 {
1752 if (current_flag == COM_START)
1753 {
1754 lead_repl = lead_middle;
1755 lead_repl_len = (int)STRLEN(lead_middle);
1756 }
1757
1758 // If we have hit RETURN immediately after the start
1759 // comment leader, then put a space after the middle
1760 // comment leader on the next line.
1761 if (!VIM_ISWHITE(saved_line[lead_len - 1])
1762 && ((p_extra != NULL
1763 && (int)curwin->w_cursor.col == lead_len)
1764 || (p_extra == NULL
1765 && saved_line[lead_len] == NUL)
1766 || require_blank))
1767 extra_space = TRUE;
1768 }
1769 break;
1770 }
1771 if (*p == COM_END)
1772 {
1773 // Doing "o" on the end of a comment does not insert leader.
1774 // Remember where the end is, might want to use it to find the
1775 // start (for C-comments).
1776 if (dir == FORWARD)
1777 {
1778 comment_end = skipwhite(saved_line);
1779 lead_len = 0;
1780 break;
1781 }
1782
1783 // Doing "O" on the end of a comment inserts the middle leader.
1784 // Find the string for the middle leader, searching backwards.
1785 while (p > curbuf->b_p_com && *p != ',')
1786 --p;
1787 for (lead_repl = p; lead_repl > curbuf->b_p_com
1788 && lead_repl[-1] != ':'; --lead_repl)
1789 ;
1790 lead_repl_len = (int)(p - lead_repl);
1791
1792 // We can probably always add an extra space when doing "O" on
1793 // the comment-end
1794 extra_space = TRUE;
1795
1796 // Check whether we allow automatic ending of comments
1797 for (p2 = p; *p2 && *p2 != ':'; p2++)
1798 {
1799 if (*p2 == COM_AUTO_END)
1800 end_comment_pending = -1; // means we want to set it
1801 }
1802 if (end_comment_pending == -1)
1803 {
1804 // Find last character in end-comment string
1805 while (*p2 && *p2 != ',')
1806 p2++;
1807 end_comment_pending = p2[-1];
1808 }
1809 break;
1810 }
1811 if (*p == COM_FIRST)
1812 {
1813 // Comment leader for first line only: Don't repeat leader
1814 // when using "O", blank out leader when using "o".
1815 if (dir == BACKWARD)
1816 lead_len = 0;
1817 else
1818 {
1819 lead_repl = (char_u *)"";
1820 lead_repl_len = 0;
1821 }
1822 break;
1823 }
1824 }
1825 if (lead_len)
1826 {
1827 // allocate buffer (may concatenate p_extra later)
1828 leader = alloc(lead_len + lead_repl_len + extra_space + extra_len
1829 + (second_line_indent > 0 ? second_line_indent : 0) + 1);
1830 allocated = leader; // remember to free it later
1831
1832 if (leader == NULL)
1833 lead_len = 0;
1834 else
1835 {
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001836 int li;
1837
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001838 vim_strncpy(leader, saved_line, lead_len);
1839
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001840 // TODO: handle multi-byte and double width chars
1841 for (li = 0; li < comment_start; ++li)
1842 if (!VIM_ISWHITE(leader[li]))
1843 leader[li] = ' ';
1844
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001845 // Replace leader with lead_repl, right or left adjusted
1846 if (lead_repl != NULL)
1847 {
1848 int c = 0;
1849 int off = 0;
1850
1851 for (p = lead_flags; *p != NUL && *p != ':'; )
1852 {
1853 if (*p == COM_RIGHT || *p == COM_LEFT)
1854 c = *p++;
1855 else if (VIM_ISDIGIT(*p) || *p == '-')
1856 off = getdigits(&p);
1857 else
1858 ++p;
1859 }
1860 if (c == COM_RIGHT) // right adjusted leader
1861 {
1862 // find last non-white in the leader to line up with
1863 for (p = leader + lead_len - 1; p > leader
1864 && VIM_ISWHITE(*p); --p)
1865 ;
1866 ++p;
1867
1868 // Compute the length of the replaced characters in
1869 // screen characters, not bytes.
1870 {
1871 int repl_size = vim_strnsize(lead_repl,
1872 lead_repl_len);
1873 int old_size = 0;
1874 char_u *endp = p;
1875 int l;
1876
1877 while (old_size < repl_size && p > leader)
1878 {
1879 MB_PTR_BACK(leader, p);
1880 old_size += ptr2cells(p);
1881 }
1882 l = lead_repl_len - (int)(endp - p);
1883 if (l != 0)
1884 mch_memmove(endp + l, endp,
1885 (size_t)((leader + lead_len) - endp));
1886 lead_len += l;
1887 }
1888 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1889 if (p + lead_repl_len > leader + lead_len)
1890 p[lead_repl_len] = NUL;
1891
1892 // blank-out any other chars from the old leader.
1893 while (--p >= leader)
1894 {
1895 int l = mb_head_off(leader, p);
1896
1897 if (l > 1)
1898 {
1899 p -= l;
1900 if (ptr2cells(p) > 1)
1901 {
1902 p[1] = ' ';
1903 --l;
1904 }
1905 mch_memmove(p + 1, p + l + 1,
1906 (size_t)((leader + lead_len) - (p + l + 1)));
1907 lead_len -= l;
1908 *p = ' ';
1909 }
1910 else if (!VIM_ISWHITE(*p))
1911 *p = ' ';
1912 }
1913 }
1914 else // left adjusted leader
1915 {
1916 p = skipwhite(leader);
1917
1918 // Compute the length of the replaced characters in
1919 // screen characters, not bytes. Move the part that is
1920 // not to be overwritten.
1921 {
1922 int repl_size = vim_strnsize(lead_repl,
1923 lead_repl_len);
1924 int i;
1925 int l;
1926
1927 for (i = 0; i < lead_len && p[i] != NUL; i += l)
1928 {
1929 l = (*mb_ptr2len)(p + i);
1930 if (vim_strnsize(p, i + l) > repl_size)
1931 break;
1932 }
1933 if (i != lead_repl_len)
1934 {
1935 mch_memmove(p + lead_repl_len, p + i,
1936 (size_t)(lead_len - i - (p - leader)));
1937 lead_len += lead_repl_len - i;
1938 }
1939 }
1940 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1941
1942 // Replace any remaining non-white chars in the old
1943 // leader by spaces. Keep Tabs, the indent must
1944 // remain the same.
1945 for (p += lead_repl_len; p < leader + lead_len; ++p)
1946 if (!VIM_ISWHITE(*p))
1947 {
1948 // Don't put a space before a TAB.
1949 if (p + 1 < leader + lead_len && p[1] == TAB)
1950 {
1951 --lead_len;
1952 mch_memmove(p, p + 1,
1953 (leader + lead_len) - p);
1954 }
1955 else
1956 {
1957 int l = (*mb_ptr2len)(p);
1958
1959 if (l > 1)
1960 {
1961 if (ptr2cells(p) > 1)
1962 {
1963 // Replace a double-wide char with
1964 // two spaces
1965 --l;
1966 *p++ = ' ';
1967 }
1968 mch_memmove(p + 1, p + l,
1969 (leader + lead_len) - p);
1970 lead_len -= l - 1;
1971 }
1972 *p = ' ';
1973 }
1974 }
1975 *p = NUL;
1976 }
1977
1978 // Recompute the indent, it may have changed.
1979 if (curbuf->b_p_ai
1980#ifdef FEAT_SMARTINDENT
1981 || do_si
1982#endif
1983 )
1984#ifdef FEAT_VARTABS
1985 newindent = get_indent_str_vtab(leader, curbuf->b_p_ts,
1986 curbuf->b_p_vts_array, FALSE);
1987#else
1988 newindent = get_indent_str(leader,
1989 (int)curbuf->b_p_ts, FALSE);
1990#endif
1991
1992 // Add the indent offset
1993 if (newindent + off < 0)
1994 {
1995 off = -newindent;
1996 newindent = 0;
1997 }
1998 else
1999 newindent += off;
2000
2001 // Correct trailing spaces for the shift, so that
2002 // alignment remains equal.
2003 while (off > 0 && lead_len > 0
2004 && leader[lead_len - 1] == ' ')
2005 {
2006 // Don't do it when there is a tab before the space
2007 if (vim_strchr(skipwhite(leader), '\t') != NULL)
2008 break;
2009 --lead_len;
2010 --off;
2011 }
2012
2013 // If the leader ends in white space, don't add an
2014 // extra space
2015 if (lead_len > 0 && VIM_ISWHITE(leader[lead_len - 1]))
2016 extra_space = FALSE;
2017 leader[lead_len] = NUL;
2018 }
2019
2020 if (extra_space)
2021 {
2022 leader[lead_len++] = ' ';
2023 leader[lead_len] = NUL;
2024 }
2025
2026 newcol = lead_len;
2027
2028 // if a new indent will be set below, remove the indent that
2029 // is in the comment leader
2030 if (newindent
2031#ifdef FEAT_SMARTINDENT
2032 || did_si
2033#endif
2034 )
2035 {
2036 while (lead_len && VIM_ISWHITE(*leader))
2037 {
2038 --lead_len;
2039 --newcol;
2040 ++leader;
2041 }
2042 }
2043
2044 }
2045#ifdef FEAT_SMARTINDENT
2046 did_si = can_si = FALSE;
2047#endif
2048 }
2049 else if (comment_end != NULL)
2050 {
2051 // We have finished a comment, so we don't use the leader.
2052 // If this was a C-comment and 'ai' or 'si' is set do a normal
2053 // indent to align with the line containing the start of the
2054 // comment.
2055 if (comment_end[0] == '*' && comment_end[1] == '/' &&
2056 (curbuf->b_p_ai
2057#ifdef FEAT_SMARTINDENT
2058 || do_si
2059#endif
2060 ))
2061 {
2062 old_cursor = curwin->w_cursor;
2063 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line);
2064 if ((pos = findmatch(NULL, NUL)) != NULL)
2065 {
2066 curwin->w_cursor.lnum = pos->lnum;
2067 newindent = get_indent();
2068 }
2069 curwin->w_cursor = old_cursor;
2070 }
2071 }
2072 }
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002073
2074 // (State == INSERT || State == REPLACE), only when dir == FORWARD
2075 if (p_extra != NULL)
2076 {
2077 *p_extra = saved_char; // restore char that NUL replaced
2078
2079 // When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first
2080 // non-blank.
2081 //
2082 // When in REPLACE mode, put the deleted blanks on the replace stack,
2083 // preceded by a NUL, so they can be put back when a BS is entered.
2084 if (REPLACE_NORMAL(State))
Bram Moolenaarc667da52019-11-30 20:52:27 +01002085 replace_push(NUL); // end of extra blanks
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002086 if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES))
2087 {
2088 while ((*p_extra == ' ' || *p_extra == '\t')
2089 && (!enc_utf8
2090 || !utf_iscomposing(utf_ptr2char(p_extra + 1))))
2091 {
2092 if (REPLACE_NORMAL(State))
2093 replace_push(*p_extra);
2094 ++p_extra;
2095 ++less_cols_off;
2096 }
2097 }
2098
2099 // columns for marks adjusted for removed columns
2100 less_cols = (int)(p_extra - saved_line);
2101 }
2102
2103 if (p_extra == NULL)
2104 p_extra = (char_u *)""; // append empty line
2105
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002106 // concatenate leader and p_extra, if there is a leader
2107 if (lead_len)
2108 {
2109 if (flags & OPENLINE_COM_LIST && second_line_indent > 0)
2110 {
2111 int i;
2112 int padding = second_line_indent
2113 - (newindent + (int)STRLEN(leader));
2114
2115 // Here whitespace is inserted after the comment char.
2116 // Below, set_indent(newindent, SIN_INSERT) will insert the
2117 // whitespace needed before the comment char.
2118 for (i = 0; i < padding; i++)
2119 {
2120 STRCAT(leader, " ");
2121 less_cols--;
2122 newcol++;
2123 }
2124 }
2125 STRCAT(leader, p_extra);
2126 p_extra = leader;
2127 did_ai = TRUE; // So truncating blanks works with comments
2128 less_cols -= lead_len;
2129 }
2130 else
2131 end_comment_pending = NUL; // turns out there was no leader
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002132
2133 old_cursor = curwin->w_cursor;
2134 if (dir == BACKWARD)
2135 --curwin->w_cursor.lnum;
2136 if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count)
2137 {
2138 if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE)
2139 == FAIL)
2140 goto theend;
2141 // Postpone calling changed_lines(), because it would mess up folding
2142 // with markers.
2143 // Skip mark_adjust when adding a line after the last one, there can't
2144 // be marks there. But still needed in diff mode.
2145 if (curwin->w_cursor.lnum + 1 < curbuf->b_ml.ml_line_count
2146#ifdef FEAT_DIFF
2147 || curwin->w_p_diff
2148#endif
2149 )
2150 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
2151 did_append = TRUE;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002152#ifdef FEAT_PROP_POPUP
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002153 if ((State & INSERT) && !(State & VREPLACE_FLAG))
2154 // properties after the split move to the next line
2155 adjust_props_for_split(curwin->w_cursor.lnum, curwin->w_cursor.lnum,
2156 curwin->w_cursor.col + 1, 0);
2157#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002158 }
2159 else
2160 {
2161 // In VREPLACE mode we are starting to replace the next line.
2162 curwin->w_cursor.lnum++;
2163 if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed)
2164 {
2165 // In case we NL to a new line, BS to the previous one, and NL
2166 // again, we don't want to save the new line for undo twice.
Bram Moolenaarc667da52019-11-30 20:52:27 +01002167 (void)u_save_cursor(); // errors are ignored!
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002168 vr_lines_changed++;
2169 }
2170 ml_replace(curwin->w_cursor.lnum, p_extra, TRUE);
2171 changed_bytes(curwin->w_cursor.lnum, 0);
2172 curwin->w_cursor.lnum--;
2173 did_append = FALSE;
2174 }
2175
2176 if (newindent
2177#ifdef FEAT_SMARTINDENT
2178 || did_si
2179#endif
2180 )
2181 {
2182 ++curwin->w_cursor.lnum;
2183#ifdef FEAT_SMARTINDENT
2184 if (did_si)
2185 {
2186 int sw = (int)get_sw_value(curbuf);
2187
2188 if (p_sr)
2189 newindent -= newindent % sw;
2190 newindent += sw;
2191 }
2192#endif
2193 // Copy the indent
2194 if (curbuf->b_p_ci)
2195 {
2196 (void)copy_indent(newindent, saved_line);
2197
2198 // Set the 'preserveindent' option so that any further screwing
2199 // with the line doesn't entirely destroy our efforts to preserve
2200 // it. It gets restored at the function end.
2201 curbuf->b_p_pi = TRUE;
2202 }
2203 else
2204 (void)set_indent(newindent, SIN_INSERT);
2205 less_cols -= curwin->w_cursor.col;
2206
2207 ai_col = curwin->w_cursor.col;
2208
2209 // In REPLACE mode, for each character in the new indent, there must
2210 // be a NUL on the replace stack, for when it is deleted with BS
2211 if (REPLACE_NORMAL(State))
2212 for (n = 0; n < (int)curwin->w_cursor.col; ++n)
2213 replace_push(NUL);
2214 newcol += curwin->w_cursor.col;
2215#ifdef FEAT_SMARTINDENT
2216 if (no_si)
2217 did_si = FALSE;
2218#endif
2219 }
2220
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002221 // In REPLACE mode, for each character in the extra leader, there must be
2222 // a NUL on the replace stack, for when it is deleted with BS.
2223 if (REPLACE_NORMAL(State))
2224 while (lead_len-- > 0)
2225 replace_push(NUL);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002226
2227 curwin->w_cursor = old_cursor;
2228
2229 if (dir == FORWARD)
2230 {
2231 if (trunc_line || (State & INSERT))
2232 {
2233 // truncate current line at cursor
2234 saved_line[curwin->w_cursor.col] = NUL;
2235 // Remove trailing white space, unless OPENLINE_KEEPTRAIL used.
2236 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL))
2237 truncate_spaces(saved_line);
2238 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE);
2239 saved_line = NULL;
2240 if (did_append)
2241 {
2242 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
2243 curwin->w_cursor.lnum + 1, 1L);
2244 did_append = FALSE;
2245
2246 // Move marks after the line break to the new line.
2247 if (flags & OPENLINE_MARKFIX)
2248 mark_col_adjust(curwin->w_cursor.lnum,
2249 curwin->w_cursor.col + less_cols_off,
2250 1L, (long)-less_cols, 0);
2251 }
2252 else
2253 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
2254 }
2255
2256 // Put the cursor on the new line. Careful: the scrollup() above may
2257 // have moved w_cursor, we must use old_cursor.
2258 curwin->w_cursor.lnum = old_cursor.lnum + 1;
2259 }
2260 if (did_append)
2261 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L);
2262
2263 curwin->w_cursor.col = newcol;
2264 curwin->w_cursor.coladd = 0;
2265
2266#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2267 // In VREPLACE mode, we are handling the replace stack ourselves, so stop
2268 // fixthisline() from doing it (via change_indent()) by telling it we're in
2269 // normal INSERT mode.
2270 if (State & VREPLACE_FLAG)
2271 {
2272 vreplace_mode = State; // So we know to put things right later
2273 State = INSERT;
2274 }
2275 else
2276 vreplace_mode = 0;
2277#endif
2278#ifdef FEAT_LISP
2279 // May do lisp indenting.
2280 if (!p_paste
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002281 && leader == NULL
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002282 && curbuf->b_p_lisp
2283 && curbuf->b_p_ai)
2284 {
2285 fixthisline(get_lisp_indent);
2286 ai_col = (colnr_T)getwhitecols_curline();
2287 }
2288#endif
2289#ifdef FEAT_CINDENT
2290 // May do indenting after opening a new line.
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00002291 if (do_cindent)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002292 {
2293 do_c_expr_indent();
2294 ai_col = (colnr_T)getwhitecols_curline();
2295 }
2296#endif
2297#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2298 if (vreplace_mode != 0)
2299 State = vreplace_mode;
2300#endif
2301
2302 // Finally, VREPLACE gets the stuff on the new line, then puts back the
2303 // original line, and inserts the new stuff char by char, pushing old stuff
2304 // onto the replace stack (via ins_char()).
2305 if (State & VREPLACE_FLAG)
2306 {
2307 // Put new line in p_extra
2308 p_extra = vim_strsave(ml_get_curline());
2309 if (p_extra == NULL)
2310 goto theend;
2311
2312 // Put back original line
2313 ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
2314
2315 // Insert new stuff into line again
2316 curwin->w_cursor.col = 0;
2317 curwin->w_cursor.coladd = 0;
2318 ins_bytes(p_extra); // will call changed_bytes()
2319 vim_free(p_extra);
2320 next_line = NULL;
2321 }
2322
2323 retval = OK; // success!
2324theend:
2325 curbuf->b_p_pi = saved_pi;
2326 vim_free(saved_line);
2327 vim_free(next_line);
2328 vim_free(allocated);
2329 return retval;
2330}
2331
2332/*
2333 * Delete from cursor to end of line.
2334 * Caller must have prepared for undo.
2335 * If "fixpos" is TRUE fix the cursor position when done.
2336 *
2337 * Return FAIL for failure, OK otherwise.
2338 */
2339 int
2340truncate_line(int fixpos)
2341{
2342 char_u *newp;
2343 linenr_T lnum = curwin->w_cursor.lnum;
2344 colnr_T col = curwin->w_cursor.col;
2345
2346 if (col == 0)
2347 newp = vim_strsave((char_u *)"");
2348 else
2349 newp = vim_strnsave(ml_get(lnum), col);
2350
2351 if (newp == NULL)
2352 return FAIL;
2353
2354 ml_replace(lnum, newp, FALSE);
2355
2356 // mark the buffer as changed and prepare for displaying
2357 changed_bytes(lnum, curwin->w_cursor.col);
2358
2359 // If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2360 if (fixpos && curwin->w_cursor.col > 0)
2361 --curwin->w_cursor.col;
2362
2363 return OK;
2364}
2365
2366/*
2367 * Delete "nlines" lines at the cursor.
2368 * Saves the lines for undo first if "undo" is TRUE.
2369 */
2370 void
2371del_lines(long nlines, int undo)
2372{
2373 long n;
2374 linenr_T first = curwin->w_cursor.lnum;
2375
2376 if (nlines <= 0)
2377 return;
2378
2379 // save the deleted lines for undo
2380 if (undo && u_savedel(first, nlines) == FAIL)
2381 return;
2382
2383 for (n = 0; n < nlines; )
2384 {
2385 if (curbuf->b_ml.ml_flags & ML_EMPTY) // nothing to delete
2386 break;
2387
Bram Moolenaarca70c072020-05-30 20:30:46 +02002388 ml_delete_flags(first, ML_DEL_MESSAGE);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002389 ++n;
2390
2391 // If we delete the last line in the file, stop
2392 if (first > curbuf->b_ml.ml_line_count)
2393 break;
2394 }
2395
2396 // Correct the cursor position before calling deleted_lines_mark(), it may
2397 // trigger a callback to display the cursor.
2398 curwin->w_cursor.col = 0;
2399 check_cursor_lnum();
2400
2401 // adjust marks, mark the buffer as changed and prepare for displaying
2402 deleted_lines_mark(first, n);
2403}