blob: 9e1c1c9188e2d323118fe36e44d531f1327efe6c [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 Moolenaard61efa52022-07-23 09:52:04 +0100175 li->li_tv.vval.v_dict, "lnum");
Bram Moolenaar7b31a182019-05-24 21:39:27 +0200176 prev_lnume = (linenr_T)dict_get_number(
Bram Moolenaard61efa52022-07-23 09:52:04 +0100177 li->li_tv.vval.v_dict, "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 {
zeertzjqcfe45652022-05-27 17:26:55 +0100327 if (textlock > 0)
Bram Moolenaar4b4b1b82021-09-11 15:06:44 +0200328 {
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
Bram Moolenaard61efa52022-07-23 09:52:04 +0100387 lnum = dict_get_number(li->li_tv.vval.v_dict, "lnum");
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200388 if (start > lnum)
389 start = lnum;
Bram Moolenaard61efa52022-07-23 09:52:04 +0100390 lnum = dict_get_number(li->li_tv.vval.v_dict, "end");
Bram Moolenaar336bf2b2019-10-24 20:07:07 +0200391 if (end < lnum)
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200392 end = lnum;
Bram Moolenaard61efa52022-07-23 09:52:04 +0100393 added += dict_get_number(li->li_tv.vval.v_dict, "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;
zeertzjqcfe45652022-05-27 17:26:55 +0100404 ++textlock;
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
zeertzjqcfe45652022-05-27 17:26:55 +0100424 --textlock;
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
Bram Moolenaar7ce5b2b2022-05-16 19:40:59 +0100551 if (VIsual_active)
552 check_visual_pos();
553
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200554 FOR_ALL_TAB_WINDOWS(tp, wp)
555 {
556 if (wp->w_buffer == curbuf)
557 {
Bram Moolenaar8329ab72022-02-16 21:51:00 +0000558#ifdef FEAT_FOLDING
Bram Moolenaar94377372022-02-16 20:30:52 +0000559 linenr_T last = lnume + xtra - 1; // last line after the change
Bram Moolenaar8329ab72022-02-16 21:51:00 +0000560#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200561 // Mark this window to be redrawn later.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100562 if (wp->w_redr_type < UPD_VALID)
563 wp->w_redr_type = UPD_VALID;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200564
565 // Check if a change in the buffer has invalidated the cached
566 // values for the cursor.
567#ifdef FEAT_FOLDING
568 // Update the folds for this window. Can't postpone this, because
569 // a following operator might work on the whole fold: ">>dd".
Bram Moolenaar94377372022-02-16 20:30:52 +0000570 foldUpdate(wp, lnum, last);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200571
572 // The change may cause lines above or below the change to become
573 // included in a fold. Set lnum/lnume to the first/last line that
574 // might be displayed differently.
575 // Set w_cline_folded here as an efficient way to update it when
576 // inserting lines just above a closed fold.
577 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
578 if (wp->w_cursor.lnum == lnum)
579 wp->w_cline_folded = i;
Bram Moolenaar94377372022-02-16 20:30:52 +0000580 i = hasFoldingWin(wp, last, NULL, &last, FALSE, NULL);
581 if (wp->w_cursor.lnum == last)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200582 wp->w_cline_folded = i;
583
584 // If the changed line is in a range of previously folded lines,
585 // compare with the first line in that range.
586 if (wp->w_cursor.lnum <= lnum)
587 {
588 i = find_wl_entry(wp, lnum);
589 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
590 changed_line_abv_curs_win(wp);
591 }
592#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200593 if (wp->w_cursor.lnum > lnum)
594 changed_line_abv_curs_win(wp);
595 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
596 changed_cline_bef_curs_win(wp);
597 if (wp->w_botline >= lnum)
598 {
Bram Moolenaar5bea41d2021-07-13 22:21:44 +0200599 if (xtra < 0)
600 invalidate_botline_win(wp);
601 else
602 // Assume that botline doesn't change (inserted lines make
603 // other lines scroll down below botline).
604 approximate_botline_win(wp);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200605 }
606
607 // Check if any w_lines[] entries have become invalid.
608 // For entries below the change: Correct the lnums for
609 // inserted/deleted lines. Makes it possible to stop displaying
610 // after the change.
611 for (i = 0; i < wp->w_lines_valid; ++i)
612 if (wp->w_lines[i].wl_valid)
613 {
614 if (wp->w_lines[i].wl_lnum >= lnum)
615 {
616 if (wp->w_lines[i].wl_lnum < lnume)
617 {
618 // line included in change
619 wp->w_lines[i].wl_valid = FALSE;
620 }
621 else if (xtra != 0)
622 {
623 // line below change
624 wp->w_lines[i].wl_lnum += xtra;
625#ifdef FEAT_FOLDING
626 wp->w_lines[i].wl_lastlnum += xtra;
627#endif
628 }
629 }
630#ifdef FEAT_FOLDING
631 else if (wp->w_lines[i].wl_lastlnum >= lnum)
632 {
633 // change somewhere inside this range of folded lines,
634 // may need to be redrawn
635 wp->w_lines[i].wl_valid = FALSE;
636 }
637#endif
638 }
639
640#ifdef FEAT_FOLDING
641 // Take care of side effects for setting w_topline when folds have
642 // changed. Esp. when the buffer was changed in another window.
643 if (hasAnyFolding(wp))
644 set_topline(wp, wp->w_topline);
645#endif
zeertzjq8c979602022-04-07 15:08:01 +0100646 // If lines have been added or removed, relative numbering always
647 // requires a redraw.
Lewis Russell16246392022-03-29 11:38:17 +0100648 if (wp->w_p_rnu && xtra != 0)
zeertzjq8c979602022-04-07 15:08:01 +0100649 {
650 wp->w_last_cursor_lnum_rnu = 0;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100651 redraw_win_later(wp, UPD_VALID);
zeertzjq8c979602022-04-07 15:08:01 +0100652 }
Bram Moolenaar11a58af2019-10-24 22:32:31 +0200653#ifdef FEAT_SYN_HL
654 // Cursor line highlighting probably need to be updated with
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100655 // "UPD_VALID" if it's below the change.
Bram Moolenaar11a58af2019-10-24 22:32:31 +0200656 // If the cursor line is inside the change we need to redraw more.
657 if (wp->w_p_cul)
658 {
659 if (xtra == 0)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100660 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar11a58af2019-10-24 22:32:31 +0200661 else if (lnum <= wp->w_last_cursorline)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100662 redraw_win_later(wp, UPD_SOME_VALID);
Bram Moolenaar11a58af2019-10-24 22:32:31 +0200663 }
664#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200665 }
Bram Moolenaar368137a2022-05-31 13:43:12 +0100666#ifdef FEAT_SEARCH_EXTRA
667 if (wp == curwin && xtra != 0 && search_hl_has_cursor_lnum >= lnum)
668 search_hl_has_cursor_lnum += xtra;
669#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200670 }
671
672 // Call update_screen() later, which checks out what needs to be redrawn,
673 // since it notices b_mod_set and then uses b_mod_*.
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100674 if (must_redraw < UPD_VALID)
675 must_redraw = UPD_VALID;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200676
677 // when the cursor line is changed always trigger CursorMoved
678 if (lnum <= curwin->w_cursor.lnum
679 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
680 last_cursormoved.lnum = 0;
681}
682
683 static void
684changedOneline(buf_T *buf, linenr_T lnum)
685{
686 if (buf->b_mod_set)
687 {
688 // find the maximum area that must be redisplayed
689 if (lnum < buf->b_mod_top)
690 buf->b_mod_top = lnum;
691 else if (lnum >= buf->b_mod_bot)
692 buf->b_mod_bot = lnum + 1;
693 }
694 else
695 {
696 // set the area that must be redisplayed to one line
697 buf->b_mod_set = TRUE;
698 buf->b_mod_top = lnum;
699 buf->b_mod_bot = lnum + 1;
700 buf->b_mod_xlines = 0;
701 }
702}
703
704/*
705 * Changed bytes within a single line for the current buffer.
706 * - marks the windows on this buffer to be redisplayed
707 * - marks the buffer changed by calling changed()
708 * - invalidates cached values
709 * Careful: may trigger autocommands that reload the buffer.
710 */
711 void
712changed_bytes(linenr_T lnum, colnr_T col)
713{
714 changedOneline(curbuf, lnum);
715 changed_common(lnum, col, lnum + 1, 0L);
716
717#ifdef FEAT_DIFF
718 // Diff highlighting in other diff windows may need to be updated too.
719 if (curwin->w_p_diff)
720 {
721 win_T *wp;
722 linenr_T wlnum;
723
724 FOR_ALL_WINDOWS(wp)
725 if (wp->w_p_diff && wp != curwin)
726 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100727 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200728 wlnum = diff_lnum_win(lnum, wp);
729 if (wlnum > 0)
730 changedOneline(wp->w_buffer, wlnum);
731 }
732 }
733#endif
734}
735
736/*
737 * Like changed_bytes() but also adjust text properties for "added" bytes.
738 * When "added" is negative text was deleted.
739 */
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200740 void
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200741inserted_bytes(linenr_T lnum, colnr_T col, int added UNUSED)
742{
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100743#ifdef FEAT_PROP_POPUP
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200744 if (curbuf->b_has_textprop && added != 0)
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200745 adjust_prop_columns(lnum, col, added, 0);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200746#endif
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200747
748 changed_bytes(lnum, col);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200749}
750
751/*
752 * Appended "count" lines below line "lnum" in the current buffer.
753 * Must be called AFTER the change and after mark_adjust().
754 * Takes care of marking the buffer to be redrawn and sets the changed flag.
755 */
756 void
757appended_lines(linenr_T lnum, long count)
758{
759 changed_lines(lnum + 1, 0, lnum + 1, count);
760}
761
762/*
763 * Like appended_lines(), but adjust marks first.
764 */
765 void
766appended_lines_mark(linenr_T lnum, long count)
767{
768 // Skip mark_adjust when adding a line after the last one, there can't
769 // be marks there. But it's still needed in diff mode.
770 if (lnum + count < curbuf->b_ml.ml_line_count
771#ifdef FEAT_DIFF
772 || curwin->w_p_diff
773#endif
774 )
775 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
776 changed_lines(lnum + 1, 0, lnum + 1, count);
777}
778
779/*
780 * Deleted "count" lines at line "lnum" in the current buffer.
781 * Must be called AFTER the change and after mark_adjust().
782 * Takes care of marking the buffer to be redrawn and sets the changed flag.
783 */
784 void
785deleted_lines(linenr_T lnum, long count)
786{
787 changed_lines(lnum, 0, lnum + count, -count);
788}
789
790/*
791 * Like deleted_lines(), but adjust marks first.
792 * Make sure the cursor is on a valid line before calling, a GUI callback may
793 * be triggered to display the cursor.
794 */
795 void
796deleted_lines_mark(linenr_T lnum, long count)
797{
798 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
799 changed_lines(lnum, 0, lnum + count, -count);
800}
801
802/*
803 * Marks the area to be redrawn after a change.
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100804 * Consider also calling changed_line_display_buf().
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200805 */
Bram Moolenaar1764faa2021-05-16 20:18:57 +0200806 void
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200807changed_lines_buf(
808 buf_T *buf,
809 linenr_T lnum, // first line with change
810 linenr_T lnume, // line below last changed line
811 long xtra) // number of extra lines (negative when deleting)
812{
813 if (buf->b_mod_set)
814 {
815 // find the maximum area that must be redisplayed
816 if (lnum < buf->b_mod_top)
817 buf->b_mod_top = lnum;
818 if (lnum < buf->b_mod_bot)
819 {
820 // adjust old bot position for xtra lines
821 buf->b_mod_bot += xtra;
822 if (buf->b_mod_bot < lnum)
823 buf->b_mod_bot = lnum;
824 }
825 if (lnume + xtra > buf->b_mod_bot)
826 buf->b_mod_bot = lnume + xtra;
827 buf->b_mod_xlines += xtra;
828 }
829 else
830 {
831 // set the area that must be redisplayed
832 buf->b_mod_set = TRUE;
833 buf->b_mod_top = lnum;
834 buf->b_mod_bot = lnume + xtra;
835 buf->b_mod_xlines = xtra;
836 }
837}
838
839/*
840 * Changed lines for the current buffer.
841 * Must be called AFTER the change and after mark_adjust().
842 * - mark the buffer changed by calling changed()
843 * - mark the windows on this buffer to be redisplayed
844 * - invalidate cached values
845 * "lnum" is the first line that needs displaying, "lnume" the first line
846 * below the changed lines (BEFORE the change).
847 * When only inserting lines, "lnum" and "lnume" are equal.
848 * Takes care of calling changed() and updating b_mod_*.
849 * Careful: may trigger autocommands that reload the buffer.
850 */
851 void
852changed_lines(
853 linenr_T lnum, // first line with change
854 colnr_T col, // column in first line with change
855 linenr_T lnume, // line below last changed line
856 long xtra) // number of extra lines (negative when deleting)
857{
858 changed_lines_buf(curbuf, lnum, lnume, xtra);
859
860#ifdef FEAT_DIFF
861 if (xtra == 0 && curwin->w_p_diff && !diff_internal())
862 {
863 // When the number of lines doesn't change then mark_adjust() isn't
864 // called and other diff buffers still need to be marked for
865 // displaying.
866 win_T *wp;
867 linenr_T wlnum;
868
869 FOR_ALL_WINDOWS(wp)
870 if (wp->w_p_diff && wp != curwin)
871 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100872 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200873 wlnum = diff_lnum_win(lnum, wp);
874 if (wlnum > 0)
875 changed_lines_buf(wp->w_buffer, wlnum,
876 lnume - lnum + wlnum, 0L);
877 }
878 }
879#endif
880
881 changed_common(lnum, col, lnume, xtra);
882}
883
884/*
885 * Called when the changed flag must be reset for buffer "buf".
886 * When "ff" is TRUE also reset 'fileformat'.
Bram Moolenaarc024b462019-06-08 18:07:21 +0200887 * When "always_inc_changedtick" is TRUE b:changedtick is incremented also when
888 * the changed flag was off.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200889 */
890 void
Bram Moolenaarc024b462019-06-08 18:07:21 +0200891unchanged(buf_T *buf, int ff, int always_inc_changedtick)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200892{
893 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
894 {
895 buf->b_changed = 0;
896 ml_setflags(buf);
897 if (ff)
898 save_file_ff(buf);
899 check_status(buf);
900 redraw_tabline = TRUE;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200901 need_maketitle = TRUE; // set window title later
Bram Moolenaarc024b462019-06-08 18:07:21 +0200902 ++CHANGEDTICK(buf);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200903 }
Bram Moolenaarc024b462019-06-08 18:07:21 +0200904 else if (always_inc_changedtick)
905 ++CHANGEDTICK(buf);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200906#ifdef FEAT_NETBEANS_INTG
907 netbeans_unmodified(buf);
908#endif
909}
910
911/*
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100912 * Save the current values of 'fileformat' and 'fileencoding', so that we know
913 * the file must be considered changed when the value is different.
914 */
915 void
916save_file_ff(buf_T *buf)
917{
918 buf->b_start_ffc = *buf->b_p_ff;
919 buf->b_start_eol = buf->b_p_eol;
920 buf->b_start_bomb = buf->b_p_bomb;
921
Bram Moolenaarc667da52019-11-30 20:52:27 +0100922 // Only use free/alloc when necessary, they take time.
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100923 if (buf->b_start_fenc == NULL
924 || STRCMP(buf->b_start_fenc, buf->b_p_fenc) != 0)
925 {
926 vim_free(buf->b_start_fenc);
927 buf->b_start_fenc = vim_strsave(buf->b_p_fenc);
928 }
929}
930
931/*
932 * Return TRUE if 'fileformat' and/or 'fileencoding' has a different value
933 * from when editing started (save_file_ff() called).
934 * Also when 'endofline' was changed and 'binary' is set, or when 'bomb' was
935 * changed and 'binary' is not set.
936 * Also when 'endofline' was changed and 'fixeol' is not set.
937 * When "ignore_empty" is true don't consider a new, empty buffer to be
938 * changed.
939 */
940 int
941file_ff_differs(buf_T *buf, int ignore_empty)
942{
Bram Moolenaarc667da52019-11-30 20:52:27 +0100943 // In a buffer that was never loaded the options are not valid.
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100944 if (buf->b_flags & BF_NEVERLOADED)
945 return FALSE;
946 if (ignore_empty
947 && (buf->b_flags & BF_NEW)
948 && buf->b_ml.ml_line_count == 1
949 && *ml_get_buf(buf, (linenr_T)1, FALSE) == NUL)
950 return FALSE;
951 if (buf->b_start_ffc != *buf->b_p_ff)
952 return TRUE;
953 if ((buf->b_p_bin || !buf->b_p_fixeol) && buf->b_start_eol != buf->b_p_eol)
954 return TRUE;
955 if (!buf->b_p_bin && buf->b_start_bomb != buf->b_p_bomb)
956 return TRUE;
957 if (buf->b_start_fenc == NULL)
958 return (*buf->b_p_fenc != NUL);
959 return (STRCMP(buf->b_start_fenc, buf->b_p_fenc) != 0);
960}
961
962/*
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200963 * Insert string "p" at the cursor position. Stops at a NUL byte.
964 * Handles Replace mode and multi-byte characters.
965 */
966 void
967ins_bytes(char_u *p)
968{
969 ins_bytes_len(p, (int)STRLEN(p));
970}
971
972/*
973 * Insert string "p" with length "len" at the cursor position.
974 * Handles Replace mode and multi-byte characters.
975 */
976 void
977ins_bytes_len(char_u *p, int len)
978{
979 int i;
980 int n;
981
982 if (has_mbyte)
983 for (i = 0; i < len; i += n)
984 {
985 if (enc_utf8)
986 // avoid reading past p[len]
987 n = utfc_ptr2len_len(p + i, len - i);
988 else
989 n = (*mb_ptr2len)(p + i);
990 ins_char_bytes(p + i, n);
991 }
992 else
993 for (i = 0; i < len; ++i)
994 ins_char(p[i]);
995}
996
997/*
998 * Insert or replace a single character at the cursor position.
Bram Moolenaar24959102022-05-07 20:01:16 +0100999 * When in MODE_REPLACE or MODE_VREPLACE state, replace any existing character.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001000 * Caller must have prepared for undo.
1001 * For multi-byte characters we get the whole character, the caller must
1002 * convert bytes to a character.
1003 */
1004 void
1005ins_char(int c)
1006{
1007 char_u buf[MB_MAXBYTES + 1];
1008 int n = (*mb_char2bytes)(c, buf);
1009
1010 // When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
1011 // Happens for CTRL-Vu9900.
1012 if (buf[0] == 0)
1013 buf[0] = '\n';
1014
1015 ins_char_bytes(buf, n);
1016}
1017
1018 void
1019ins_char_bytes(char_u *buf, int charlen)
1020{
1021 int c = buf[0];
1022 int newlen; // nr of bytes inserted
1023 int oldlen; // nr of bytes deleted (0 when not replacing)
1024 char_u *p;
1025 char_u *newp;
1026 char_u *oldp;
1027 int linelen; // length of old line including NUL
1028 colnr_T col;
1029 linenr_T lnum = curwin->w_cursor.lnum;
1030 int i;
1031
1032 // Break tabs if needed.
1033 if (virtual_active() && curwin->w_cursor.coladd > 0)
1034 coladvance_force(getviscol());
1035
1036 col = curwin->w_cursor.col;
1037 oldp = ml_get(lnum);
1038 linelen = (int)STRLEN(oldp) + 1;
1039
1040 // The lengths default to the values for when not replacing.
1041 oldlen = 0;
1042 newlen = charlen;
1043
1044 if (State & REPLACE_FLAG)
1045 {
1046 if (State & VREPLACE_FLAG)
1047 {
1048 colnr_T new_vcol = 0; // init for GCC
1049 colnr_T vcol;
1050 int old_list;
1051
1052 // Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
1053 // Returns the old value of list, so when finished,
1054 // curwin->w_p_list should be set back to this.
1055 old_list = curwin->w_p_list;
1056 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
1057 curwin->w_p_list = FALSE;
1058
1059 // In virtual replace mode each character may replace one or more
1060 // characters (zero if it's a TAB). Count the number of bytes to
1061 // be deleted to make room for the new character, counting screen
1062 // cells. May result in adding spaces to fill a gap.
1063 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
1064 new_vcol = vcol + chartabsize(buf, vcol);
1065 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
1066 {
1067 vcol += chartabsize(oldp + col + oldlen, vcol);
1068 // Don't need to remove a TAB that takes us to the right
1069 // position.
1070 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
1071 break;
1072 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
1073 // Deleted a bit too much, insert spaces.
1074 if (vcol > new_vcol)
1075 newlen += vcol - new_vcol;
1076 }
1077 curwin->w_p_list = old_list;
1078 }
1079 else if (oldp[col] != NUL)
1080 {
1081 // normal replace
1082 oldlen = (*mb_ptr2len)(oldp + col);
1083 }
1084
1085
1086 // Push the replaced bytes onto the replace stack, so that they can be
1087 // put back when BS is used. The bytes of a multi-byte character are
1088 // done the other way around, so that the first byte is popped off
1089 // first (it tells the byte length of the character).
1090 replace_push(NUL);
1091 for (i = 0; i < oldlen; ++i)
1092 {
1093 if (has_mbyte)
1094 i += replace_push_mb(oldp + col + i) - 1;
1095 else
1096 replace_push(oldp[col + i]);
1097 }
1098 }
1099
Bram Moolenaar964b3742019-05-24 18:54:09 +02001100 newp = alloc(linelen + newlen - oldlen);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001101 if (newp == NULL)
1102 return;
1103
1104 // Copy bytes before the cursor.
1105 if (col > 0)
1106 mch_memmove(newp, oldp, (size_t)col);
1107
1108 // Copy bytes after the changed character(s).
1109 p = newp + col;
1110 if (linelen > col + oldlen)
1111 mch_memmove(p + newlen, oldp + col + oldlen,
1112 (size_t)(linelen - col - oldlen));
1113
1114 // Insert or overwrite the new character.
1115 mch_memmove(p, buf, charlen);
1116 i = charlen;
1117
1118 // Fill with spaces when necessary.
1119 while (i < newlen)
1120 p[i++] = ' ';
1121
1122 // Replace the line in the buffer.
1123 ml_replace(lnum, newp, FALSE);
1124
1125 // mark the buffer as changed and prepare for displaying
LemonBoy0d534d92022-05-21 11:20:42 +01001126 changed_bytes(lnum, col);
1127#ifdef FEAT_PROP_POPUP
1128 if (curbuf->b_has_textprop && newlen != oldlen)
1129 adjust_prop_columns(lnum, col, newlen - oldlen,
1130 State & REPLACE_FLAG ? APC_SUBSTITUTE : 0);
1131#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001132
1133 // If we're in Insert or Replace mode and 'showmatch' is set, then briefly
1134 // show the match for right parens and braces.
Bram Moolenaar24959102022-05-07 20:01:16 +01001135 if (p_sm && (State & MODE_INSERT)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001136 && msg_silent == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001137 && !ins_compl_active())
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001138 {
1139 if (has_mbyte)
1140 showmatch(mb_ptr2char(buf));
1141 else
1142 showmatch(c);
1143 }
1144
1145#ifdef FEAT_RIGHTLEFT
1146 if (!p_ri || (State & REPLACE_FLAG))
1147#endif
1148 {
1149 // Normal insert: move cursor right
1150 curwin->w_cursor.col += charlen;
1151 }
1152
1153 // TODO: should try to update w_row here, to avoid recomputing it later.
1154}
1155
1156/*
1157 * Insert a string at the cursor position.
1158 * Note: Does NOT handle Replace mode.
1159 * Caller must have prepared for undo.
1160 */
1161 void
1162ins_str(char_u *s)
1163{
1164 char_u *oldp, *newp;
1165 int newlen = (int)STRLEN(s);
1166 int oldlen;
1167 colnr_T col;
1168 linenr_T lnum = curwin->w_cursor.lnum;
1169
1170 if (virtual_active() && curwin->w_cursor.coladd > 0)
1171 coladvance_force(getviscol());
1172
1173 col = curwin->w_cursor.col;
1174 oldp = ml_get(lnum);
1175 oldlen = (int)STRLEN(oldp);
1176
Bram Moolenaar964b3742019-05-24 18:54:09 +02001177 newp = alloc(oldlen + newlen + 1);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001178 if (newp == NULL)
1179 return;
1180 if (col > 0)
1181 mch_memmove(newp, oldp, (size_t)col);
1182 mch_memmove(newp + col, s, (size_t)newlen);
1183 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
1184 ml_replace(lnum, newp, FALSE);
1185 inserted_bytes(lnum, col, newlen);
1186 curwin->w_cursor.col += newlen;
1187}
1188
1189/*
1190 * Delete one character under the cursor.
1191 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
1192 * Caller must have prepared for undo.
1193 *
1194 * return FAIL for failure, OK otherwise
1195 */
1196 int
1197del_char(int fixpos)
1198{
1199 if (has_mbyte)
1200 {
1201 // Make sure the cursor is at the start of a character.
1202 mb_adjust_cursor();
1203 if (*ml_get_cursor() == NUL)
1204 return FAIL;
1205 return del_chars(1L, fixpos);
1206 }
1207 return del_bytes(1L, fixpos, TRUE);
1208}
1209
1210/*
1211 * Like del_bytes(), but delete characters instead of bytes.
1212 */
1213 int
1214del_chars(long count, int fixpos)
1215{
1216 long bytes = 0;
1217 long i;
1218 char_u *p;
1219 int l;
1220
1221 p = ml_get_cursor();
1222 for (i = 0; i < count && *p != NUL; ++i)
1223 {
1224 l = (*mb_ptr2len)(p);
1225 bytes += l;
1226 p += l;
1227 }
1228 return del_bytes(bytes, fixpos, TRUE);
1229}
1230
1231/*
1232 * Delete "count" bytes under the cursor.
1233 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
1234 * Caller must have prepared for undo.
1235 *
1236 * Return FAIL for failure, OK otherwise.
1237 */
1238 int
1239del_bytes(
1240 long count,
1241 int fixpos_arg,
1242 int use_delcombine UNUSED) // 'delcombine' option applies
1243{
1244 char_u *oldp, *newp;
1245 colnr_T oldlen;
1246 colnr_T newlen;
1247 linenr_T lnum = curwin->w_cursor.lnum;
1248 colnr_T col = curwin->w_cursor.col;
1249 int alloc_newp;
1250 long movelen;
1251 int fixpos = fixpos_arg;
1252
1253 oldp = ml_get(lnum);
1254 oldlen = (int)STRLEN(oldp);
1255
1256 // Can't do anything when the cursor is on the NUL after the line.
1257 if (col >= oldlen)
1258 return FAIL;
1259
1260 // If "count" is zero there is nothing to do.
1261 if (count == 0)
1262 return OK;
1263
1264 // If "count" is negative the caller must be doing something wrong.
1265 if (count < 1)
1266 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001267 siemsg(e_invalid_count_for_del_bytes_nr, count);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001268 return FAIL;
1269 }
1270
1271 // If 'delcombine' is set and deleting (less than) one character, only
1272 // delete the last combining character.
1273 if (p_deco && use_delcombine && enc_utf8
1274 && utfc_ptr2len(oldp + col) >= count)
1275 {
1276 int cc[MAX_MCO];
1277 int n;
1278
1279 (void)utfc_ptr2char(oldp + col, cc);
1280 if (cc[0] != NUL)
1281 {
1282 // Find the last composing char, there can be several.
1283 n = col;
1284 do
1285 {
1286 col = n;
1287 count = utf_ptr2len(oldp + n);
1288 n += count;
1289 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
1290 fixpos = 0;
1291 }
1292 }
1293
1294 // When count is too big, reduce it.
1295 movelen = (long)oldlen - (long)col - count + 1; // includes trailing NUL
1296 if (movelen <= 1)
1297 {
1298 // If we just took off the last character of a non-blank line, and
1299 // fixpos is TRUE, we don't want to end up positioned at the NUL,
1300 // unless "restart_edit" is set or 'virtualedit' contains "onemore".
1301 if (col > 0 && fixpos && restart_edit == 0
Bram Moolenaar113d9de2022-08-08 15:49:18 +01001302 && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001303 {
1304 --curwin->w_cursor.col;
1305 curwin->w_cursor.coladd = 0;
1306 if (has_mbyte)
1307 curwin->w_cursor.col -=
1308 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
1309 }
1310 count = oldlen - col;
1311 movelen = 1;
1312 }
1313 newlen = oldlen - count;
1314
1315 // If the old line has been allocated the deletion can be done in the
1316 // existing line. Otherwise a new line has to be allocated
1317 // Can't do this when using Netbeans, because we would need to invoke
1318 // netbeans_removed(), which deallocates the line. Let ml_replace() take
1319 // care of notifying Netbeans.
1320#ifdef FEAT_NETBEANS_INTG
1321 if (netbeans_active())
1322 alloc_newp = TRUE;
1323 else
1324#endif
1325 alloc_newp = !ml_line_alloced(); // check if oldp was allocated
1326 if (!alloc_newp)
1327 newp = oldp; // use same allocated memory
1328 else
1329 { // need to allocate a new line
Bram Moolenaar964b3742019-05-24 18:54:09 +02001330 newp = alloc(newlen + 1);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001331 if (newp == NULL)
1332 return FAIL;
1333 mch_memmove(newp, oldp, (size_t)col);
1334 }
1335 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
1336 if (alloc_newp)
1337 ml_replace(lnum, newp, FALSE);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001338#ifdef FEAT_PROP_POPUP
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001339 else
1340 {
1341 // Also move any following text properties.
1342 if (oldlen + 1 < curbuf->b_ml.ml_line_len)
1343 mch_memmove(newp + newlen + 1, oldp + oldlen + 1,
1344 (size_t)curbuf->b_ml.ml_line_len - oldlen - 1);
1345 curbuf->b_ml.ml_line_len -= count;
1346 }
1347#endif
1348
1349 // mark the buffer as changed and prepare for displaying
Bram Moolenaar12f20032020-02-26 22:06:00 +01001350 inserted_bytes(lnum, col, -count);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001351
1352 return OK;
1353}
1354
1355/*
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001356 * open_line: Add a new line below or above the current line.
1357 *
Bram Moolenaar24959102022-05-07 20:01:16 +01001358 * For MODE_VREPLACE state, we only add a new line when we get to the end of
1359 * the file, otherwise we just start replacing the next line.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001360 *
Bram Moolenaar24959102022-05-07 20:01:16 +01001361 * Caller must take care of undo. Since MODE_VREPLACE may affect any number of
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001362 * lines however, it may call u_save_cursor() again when starting to change a
1363 * new line.
1364 * "flags": OPENLINE_DELSPACES delete spaces after cursor
1365 * OPENLINE_DO_COM format comments
1366 * OPENLINE_KEEPTRAIL keep trailing spaces
1367 * OPENLINE_MARKFIX adjust mark positions after the line break
1368 * OPENLINE_COM_LIST format comments with list or 2nd line indent
1369 *
1370 * "second_line_indent": indent for after ^^D in Insert mode or if flag
1371 * OPENLINE_COM_LIST
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001372 * "did_do_comment" is set to TRUE when intentionally putting the comment
1373 * leader in fromt of the new line.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001374 *
1375 * Return OK for success, FAIL for failure
1376 */
1377 int
1378open_line(
1379 int dir, // FORWARD or BACKWARD
1380 int flags,
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001381 int second_line_indent,
1382 int *did_do_comment UNUSED)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001383{
1384 char_u *saved_line; // copy of the original line
1385 char_u *next_line = NULL; // copy of the next line
1386 char_u *p_extra = NULL; // what goes to next line
1387 int less_cols = 0; // less columns for mark in new line
LemonBoy788c06a2022-05-14 18:48:05 +01001388 int less_cols_off = 0; // columns to skip for mark and
1389 // textprop adjustment
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001390 pos_T old_cursor; // old cursor position
1391 int newcol = 0; // new cursor column
1392 int newindent = 0; // auto-indent of the new line
1393 int n;
1394 int trunc_line = FALSE; // truncate current line afterwards
1395 int retval = FAIL; // return value
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001396 int extra_len = 0; // length of p_extra string
1397 int lead_len; // length of comment leader
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001398 int comment_start = 0; // start index of the comment leader
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001399 char_u *lead_flags; // position in 'comments' for comment leader
1400 char_u *leader = NULL; // copy of comment leader
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001401 char_u *allocated = NULL; // allocated memory
1402 char_u *p;
1403 int saved_char = NUL; // init for GCC
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001404 pos_T *pos;
Bram Moolenaard2439e02021-12-12 19:10:44 +00001405 int do_cindent;
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001406 int do_si = may_do_si();
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001407 int no_si = FALSE; // reset did_si afterwards
1408 int first_char = NUL; // init for GCC
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001409 int vreplace_mode;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001410 int did_append; // appended a new line
1411 int saved_pi = curbuf->b_p_pi; // copy of preserveindent setting
1412
1413 // make a copy of the current line so we can mess with it
1414 saved_line = vim_strsave(ml_get_curline());
Bram Moolenaarc667da52019-11-30 20:52:27 +01001415 if (saved_line == NULL) // out of memory!
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001416 return FALSE;
1417
1418 if (State & VREPLACE_FLAG)
1419 {
Bram Moolenaar24959102022-05-07 20:01:16 +01001420 // With MODE_VREPLACE we make a copy of the next line, which we will be
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001421 // starting to replace. First make the new line empty and let vim play
1422 // with the indenting and comment leader to its heart's content. Then
1423 // we grab what it ended up putting on the new line, put back the
1424 // original line, and call ins_char() to put each new character onto
1425 // the line, replacing what was there before and pushing the right
1426 // stuff onto the replace stack. -- webb.
1427 if (curwin->w_cursor.lnum < orig_line_count)
1428 next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1));
1429 else
1430 next_line = vim_strsave((char_u *)"");
1431 if (next_line == NULL) // out of memory!
1432 goto theend;
1433
Bram Moolenaar24959102022-05-07 20:01:16 +01001434 // In MODE_VREPLACE state, a NL replaces the rest of the line, and
1435 // starts replacing the next line, so push all of the characters left
1436 // on the line onto the replace stack. We'll push any other characters
1437 // that might be replaced at the start of the next line (due to
1438 // autoindent etc) a bit later.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001439 replace_push(NUL); // Call twice because BS over NL expects it
1440 replace_push(NUL);
1441 p = saved_line + curwin->w_cursor.col;
1442 while (*p != NUL)
1443 {
1444 if (has_mbyte)
1445 p += replace_push_mb(p);
1446 else
1447 replace_push(*p++);
1448 }
1449 saved_line[curwin->w_cursor.col] = NUL;
1450 }
1451
Bram Moolenaar24959102022-05-07 20:01:16 +01001452 if ((State & MODE_INSERT) && (State & VREPLACE_FLAG) == 0)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001453 {
1454 p_extra = saved_line + curwin->w_cursor.col;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001455 if (do_si) // need first char after new line break
1456 {
1457 p = skipwhite(p_extra);
1458 first_char = *p;
1459 }
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001460 extra_len = (int)STRLEN(p_extra);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001461 saved_char = *p_extra;
1462 *p_extra = NUL;
1463 }
1464
1465 u_clearline(); // cannot do "U" command when adding lines
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001466 did_si = FALSE;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001467 ai_col = 0;
1468
1469 // If we just did an auto-indent, then we didn't type anything on
1470 // the prior line, and it should be truncated. Do this even if 'ai' is not
1471 // set because automatically inserting a comment leader also sets did_ai.
1472 if (dir == FORWARD && did_ai)
1473 trunc_line = TRUE;
1474
1475 // If 'autoindent' and/or 'smartindent' is set, try to figure out what
1476 // indent to use for the new line.
Bram Moolenaar8e145b82022-05-21 20:17:31 +01001477 if (curbuf->b_p_ai || do_si)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001478 {
1479 // count white space on current line
1480#ifdef FEAT_VARTABS
1481 newindent = get_indent_str_vtab(saved_line, curbuf->b_p_ts,
1482 curbuf->b_p_vts_array, FALSE);
1483#else
1484 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts, FALSE);
1485#endif
1486 if (newindent == 0 && !(flags & OPENLINE_COM_LIST))
1487 newindent = second_line_indent; // for ^^D command in insert mode
1488
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001489 // Do smart indenting.
1490 // In insert/replace mode (only when dir == FORWARD)
1491 // we may move some text to the next line. If it starts with '{'
1492 // don't add an indent. Fixes inserting a NL before '{' in line
1493 // "if (condition) {"
1494 if (!trunc_line && do_si && *saved_line != NUL
1495 && (p_extra == NULL || first_char != '{'))
1496 {
1497 char_u *ptr;
1498 char_u last_char;
1499
1500 old_cursor = curwin->w_cursor;
1501 ptr = saved_line;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001502 if (flags & OPENLINE_DO_COM)
1503 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
1504 else
1505 lead_len = 0;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001506 if (dir == FORWARD)
1507 {
1508 // Skip preprocessor directives, unless they are
1509 // recognised as comments.
Bram Moolenaar8c96af92019-09-28 19:05:57 +02001510 if ( lead_len == 0 && ptr[0] == '#')
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001511 {
1512 while (ptr[0] == '#' && curwin->w_cursor.lnum > 1)
1513 ptr = ml_get(--curwin->w_cursor.lnum);
1514 newindent = get_indent();
1515 }
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001516 if (flags & OPENLINE_DO_COM)
1517 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
1518 else
1519 lead_len = 0;
1520 if (lead_len > 0)
1521 {
1522 // This case gets the following right:
1523 // /*
1524 // * A comment (read '\' as '/').
1525 // */
1526 // #define IN_THE_WAY
1527 // This should line up here;
1528 p = skipwhite(ptr);
1529 if (p[0] == '/' && p[1] == '*')
1530 p++;
1531 if (p[0] == '*')
1532 {
1533 for (p++; *p; p++)
1534 {
1535 if (p[0] == '/' && p[-1] == '*')
1536 {
1537 // End of C comment, indent should line up
1538 // with the line containing the start of
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001539 // the comment.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001540 curwin->w_cursor.col = (colnr_T)(p - ptr);
1541 if ((pos = findmatch(NULL, NUL)) != NULL)
1542 {
1543 curwin->w_cursor.lnum = pos->lnum;
1544 newindent = get_indent();
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001545 break;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001546 }
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001547 // this may make "ptr" invalid, get it again
1548 ptr = ml_get(curwin->w_cursor.lnum);
1549 p = ptr + curwin->w_cursor.col;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001550 }
1551 }
1552 }
1553 }
1554 else // Not a comment line
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001555 {
1556 // Find last non-blank in line
1557 p = ptr + STRLEN(ptr) - 1;
1558 while (p > ptr && VIM_ISWHITE(*p))
1559 --p;
1560 last_char = *p;
1561
1562 // find the character just before the '{' or ';'
1563 if (last_char == '{' || last_char == ';')
1564 {
1565 if (p > ptr)
1566 --p;
1567 while (p > ptr && VIM_ISWHITE(*p))
1568 --p;
1569 }
1570 // Try to catch lines that are split over multiple
1571 // lines. eg:
1572 // if (condition &&
1573 // condition) {
1574 // Should line up here!
1575 // }
1576 if (*p == ')')
1577 {
1578 curwin->w_cursor.col = (colnr_T)(p - ptr);
1579 if ((pos = findmatch(NULL, '(')) != NULL)
1580 {
1581 curwin->w_cursor.lnum = pos->lnum;
1582 newindent = get_indent();
1583 ptr = ml_get_curline();
1584 }
1585 }
1586 // If last character is '{' do indent, without
1587 // checking for "if" and the like.
1588 if (last_char == '{')
1589 {
1590 did_si = TRUE; // do indent
1591 no_si = TRUE; // don't delete it when '{' typed
1592 }
1593 // Look for "if" and the like, use 'cinwords'.
1594 // Don't do this if the previous line ended in ';' or
1595 // '}'.
1596 else if (last_char != ';' && last_char != '}'
1597 && cin_is_cinword(ptr))
1598 did_si = TRUE;
1599 }
1600 }
1601 else // dir == BACKWARD
1602 {
1603 // Skip preprocessor directives, unless they are
1604 // recognised as comments.
Bram Moolenaar8c96af92019-09-28 19:05:57 +02001605 if (lead_len == 0 && ptr[0] == '#')
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001606 {
1607 int was_backslashed = FALSE;
1608
1609 while ((ptr[0] == '#' || was_backslashed) &&
1610 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
1611 {
1612 if (*ptr && ptr[STRLEN(ptr) - 1] == '\\')
1613 was_backslashed = TRUE;
1614 else
1615 was_backslashed = FALSE;
1616 ptr = ml_get(++curwin->w_cursor.lnum);
1617 }
1618 if (was_backslashed)
1619 newindent = 0; // Got to end of file
1620 else
1621 newindent = get_indent();
1622 }
1623 p = skipwhite(ptr);
1624 if (*p == '}') // if line starts with '}': do indent
1625 did_si = TRUE;
1626 else // can delete indent when '{' typed
1627 can_si_back = TRUE;
1628 }
1629 curwin->w_cursor = old_cursor;
1630 }
1631 if (do_si)
1632 can_si = TRUE;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001633
1634 did_ai = TRUE;
1635 }
1636
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001637 // May do indenting after opening a new line.
1638 do_cindent = !p_paste && (curbuf->b_p_cin
Bram Moolenaar8e145b82022-05-21 20:17:31 +01001639#ifdef FEAT_EVAL
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001640 || *curbuf->b_p_inde != NUL
Bram Moolenaar8e145b82022-05-21 20:17:31 +01001641#endif
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001642 )
1643 && in_cinkeys(dir == FORWARD
1644 ? KEY_OPEN_FORW
1645 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum));
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001646
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001647 // Find out if the current line starts with a comment leader.
1648 // This may then be inserted in front of the new line.
1649 end_comment_pending = NUL;
1650 if (flags & OPENLINE_DO_COM)
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001651 {
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001652 lead_len = get_leader_len(saved_line, &lead_flags,
1653 dir == BACKWARD, TRUE);
Bram Moolenaar2bf875f2022-05-07 14:54:11 +01001654 if (lead_len == 0 && curbuf->b_p_cin && do_cindent && dir == FORWARD
Bram Moolenaar7e667782022-05-23 13:10:48 +01001655 && (!has_format_option(FO_NO_OPEN_COMS)
1656 || (flags & OPENLINE_FORMAT)))
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001657 {
Bram Moolenaar5ea5f372021-12-29 15:15:47 +00001658 // Check for a line comment after code.
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001659 comment_start = check_linecomment(saved_line);
1660 if (comment_start != MAXCOL)
1661 {
1662 lead_len = get_leader_len(saved_line + comment_start,
Bram Moolenaar5ea5f372021-12-29 15:15:47 +00001663 &lead_flags, FALSE, TRUE);
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001664 if (lead_len != 0)
1665 {
1666 lead_len += comment_start;
1667 if (did_do_comment != NULL)
1668 *did_do_comment = TRUE;
1669 }
1670 }
1671 }
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001672 }
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001673 else
1674 lead_len = 0;
1675 if (lead_len > 0)
1676 {
1677 char_u *lead_repl = NULL; // replaces comment leader
1678 int lead_repl_len = 0; // length of *lead_repl
1679 char_u lead_middle[COM_MAX_LEN]; // middle-comment string
1680 char_u lead_end[COM_MAX_LEN]; // end-comment string
1681 char_u *comment_end = NULL; // where lead_end has been found
1682 int extra_space = FALSE; // append extra space
1683 int current_flag;
1684 int require_blank = FALSE; // requires blank after middle
1685 char_u *p2;
1686
1687 // If the comment leader has the start, middle or end flag, it may not
1688 // be used or may be replaced with the middle leader.
1689 for (p = lead_flags; *p && *p != ':'; ++p)
1690 {
1691 if (*p == COM_BLANK)
1692 {
1693 require_blank = TRUE;
1694 continue;
1695 }
1696 if (*p == COM_START || *p == COM_MIDDLE)
1697 {
1698 current_flag = *p;
1699 if (*p == COM_START)
1700 {
1701 // Doing "O" on a start of comment does not insert leader.
1702 if (dir == BACKWARD)
1703 {
1704 lead_len = 0;
1705 break;
1706 }
1707
1708 // find start of middle part
1709 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
1710 require_blank = FALSE;
1711 }
1712
1713 // Isolate the strings of the middle and end leader.
Bram Moolenaarc667da52019-11-30 20:52:27 +01001714 while (*p && p[-1] != ':') // find end of middle flags
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001715 {
1716 if (*p == COM_BLANK)
1717 require_blank = TRUE;
1718 ++p;
1719 }
1720 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
1721
1722 while (*p && p[-1] != ':') // find end of end flags
1723 {
1724 // Check whether we allow automatic ending of comments
1725 if (*p == COM_AUTO_END)
1726 end_comment_pending = -1; // means we want to set it
1727 ++p;
1728 }
1729 n = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
1730
1731 if (end_comment_pending == -1) // we can set it now
1732 end_comment_pending = lead_end[n - 1];
1733
1734 // If the end of the comment is in the same line, don't use
1735 // the comment leader.
1736 if (dir == FORWARD)
1737 {
1738 for (p = saved_line + lead_len; *p; ++p)
1739 if (STRNCMP(p, lead_end, n) == 0)
1740 {
1741 comment_end = p;
1742 lead_len = 0;
1743 break;
1744 }
1745 }
1746
1747 // Doing "o" on a start of comment inserts the middle leader.
1748 if (lead_len > 0)
1749 {
1750 if (current_flag == COM_START)
1751 {
1752 lead_repl = lead_middle;
1753 lead_repl_len = (int)STRLEN(lead_middle);
1754 }
1755
1756 // If we have hit RETURN immediately after the start
1757 // comment leader, then put a space after the middle
1758 // comment leader on the next line.
1759 if (!VIM_ISWHITE(saved_line[lead_len - 1])
1760 && ((p_extra != NULL
1761 && (int)curwin->w_cursor.col == lead_len)
1762 || (p_extra == NULL
1763 && saved_line[lead_len] == NUL)
1764 || require_blank))
1765 extra_space = TRUE;
1766 }
1767 break;
1768 }
1769 if (*p == COM_END)
1770 {
1771 // Doing "o" on the end of a comment does not insert leader.
1772 // Remember where the end is, might want to use it to find the
1773 // start (for C-comments).
1774 if (dir == FORWARD)
1775 {
1776 comment_end = skipwhite(saved_line);
1777 lead_len = 0;
1778 break;
1779 }
1780
1781 // Doing "O" on the end of a comment inserts the middle leader.
1782 // Find the string for the middle leader, searching backwards.
1783 while (p > curbuf->b_p_com && *p != ',')
1784 --p;
1785 for (lead_repl = p; lead_repl > curbuf->b_p_com
1786 && lead_repl[-1] != ':'; --lead_repl)
1787 ;
1788 lead_repl_len = (int)(p - lead_repl);
1789
1790 // We can probably always add an extra space when doing "O" on
1791 // the comment-end
1792 extra_space = TRUE;
1793
1794 // Check whether we allow automatic ending of comments
1795 for (p2 = p; *p2 && *p2 != ':'; p2++)
1796 {
1797 if (*p2 == COM_AUTO_END)
1798 end_comment_pending = -1; // means we want to set it
1799 }
1800 if (end_comment_pending == -1)
1801 {
1802 // Find last character in end-comment string
1803 while (*p2 && *p2 != ',')
1804 p2++;
1805 end_comment_pending = p2[-1];
1806 }
1807 break;
1808 }
1809 if (*p == COM_FIRST)
1810 {
1811 // Comment leader for first line only: Don't repeat leader
1812 // when using "O", blank out leader when using "o".
1813 if (dir == BACKWARD)
1814 lead_len = 0;
1815 else
1816 {
1817 lead_repl = (char_u *)"";
1818 lead_repl_len = 0;
1819 }
1820 break;
1821 }
1822 }
1823 if (lead_len)
1824 {
1825 // allocate buffer (may concatenate p_extra later)
1826 leader = alloc(lead_len + lead_repl_len + extra_space + extra_len
1827 + (second_line_indent > 0 ? second_line_indent : 0) + 1);
1828 allocated = leader; // remember to free it later
1829
1830 if (leader == NULL)
1831 lead_len = 0;
1832 else
1833 {
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001834 int li;
1835
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001836 vim_strncpy(leader, saved_line, lead_len);
1837
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001838 // TODO: handle multi-byte and double width chars
1839 for (li = 0; li < comment_start; ++li)
1840 if (!VIM_ISWHITE(leader[li]))
1841 leader[li] = ' ';
1842
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001843 // Replace leader with lead_repl, right or left adjusted
1844 if (lead_repl != NULL)
1845 {
1846 int c = 0;
1847 int off = 0;
1848
1849 for (p = lead_flags; *p != NUL && *p != ':'; )
1850 {
1851 if (*p == COM_RIGHT || *p == COM_LEFT)
1852 c = *p++;
1853 else if (VIM_ISDIGIT(*p) || *p == '-')
1854 off = getdigits(&p);
1855 else
1856 ++p;
1857 }
1858 if (c == COM_RIGHT) // right adjusted leader
1859 {
1860 // find last non-white in the leader to line up with
1861 for (p = leader + lead_len - 1; p > leader
1862 && VIM_ISWHITE(*p); --p)
1863 ;
1864 ++p;
1865
1866 // Compute the length of the replaced characters in
1867 // screen characters, not bytes.
1868 {
1869 int repl_size = vim_strnsize(lead_repl,
1870 lead_repl_len);
1871 int old_size = 0;
1872 char_u *endp = p;
1873 int l;
1874
1875 while (old_size < repl_size && p > leader)
1876 {
1877 MB_PTR_BACK(leader, p);
1878 old_size += ptr2cells(p);
1879 }
1880 l = lead_repl_len - (int)(endp - p);
1881 if (l != 0)
1882 mch_memmove(endp + l, endp,
1883 (size_t)((leader + lead_len) - endp));
1884 lead_len += l;
1885 }
1886 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1887 if (p + lead_repl_len > leader + lead_len)
1888 p[lead_repl_len] = NUL;
1889
1890 // blank-out any other chars from the old leader.
1891 while (--p >= leader)
1892 {
1893 int l = mb_head_off(leader, p);
1894
1895 if (l > 1)
1896 {
1897 p -= l;
1898 if (ptr2cells(p) > 1)
1899 {
1900 p[1] = ' ';
1901 --l;
1902 }
1903 mch_memmove(p + 1, p + l + 1,
1904 (size_t)((leader + lead_len) - (p + l + 1)));
1905 lead_len -= l;
1906 *p = ' ';
1907 }
1908 else if (!VIM_ISWHITE(*p))
1909 *p = ' ';
1910 }
1911 }
1912 else // left adjusted leader
1913 {
1914 p = skipwhite(leader);
1915
1916 // Compute the length of the replaced characters in
1917 // screen characters, not bytes. Move the part that is
1918 // not to be overwritten.
1919 {
1920 int repl_size = vim_strnsize(lead_repl,
1921 lead_repl_len);
1922 int i;
1923 int l;
1924
1925 for (i = 0; i < lead_len && p[i] != NUL; i += l)
1926 {
1927 l = (*mb_ptr2len)(p + i);
1928 if (vim_strnsize(p, i + l) > repl_size)
1929 break;
1930 }
1931 if (i != lead_repl_len)
1932 {
1933 mch_memmove(p + lead_repl_len, p + i,
1934 (size_t)(lead_len - i - (p - leader)));
1935 lead_len += lead_repl_len - i;
1936 }
1937 }
1938 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1939
1940 // Replace any remaining non-white chars in the old
1941 // leader by spaces. Keep Tabs, the indent must
1942 // remain the same.
1943 for (p += lead_repl_len; p < leader + lead_len; ++p)
1944 if (!VIM_ISWHITE(*p))
1945 {
1946 // Don't put a space before a TAB.
1947 if (p + 1 < leader + lead_len && p[1] == TAB)
1948 {
1949 --lead_len;
1950 mch_memmove(p, p + 1,
1951 (leader + lead_len) - p);
1952 }
1953 else
1954 {
1955 int l = (*mb_ptr2len)(p);
1956
1957 if (l > 1)
1958 {
1959 if (ptr2cells(p) > 1)
1960 {
1961 // Replace a double-wide char with
1962 // two spaces
1963 --l;
1964 *p++ = ' ';
1965 }
1966 mch_memmove(p + 1, p + l,
1967 (leader + lead_len) - p);
1968 lead_len -= l - 1;
1969 }
1970 *p = ' ';
1971 }
1972 }
1973 *p = NUL;
1974 }
1975
1976 // Recompute the indent, it may have changed.
Bram Moolenaar8e145b82022-05-21 20:17:31 +01001977 if (curbuf->b_p_ai || do_si)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001978#ifdef FEAT_VARTABS
1979 newindent = get_indent_str_vtab(leader, curbuf->b_p_ts,
1980 curbuf->b_p_vts_array, FALSE);
1981#else
1982 newindent = get_indent_str(leader,
1983 (int)curbuf->b_p_ts, FALSE);
1984#endif
1985
1986 // Add the indent offset
1987 if (newindent + off < 0)
1988 {
1989 off = -newindent;
1990 newindent = 0;
1991 }
1992 else
1993 newindent += off;
1994
1995 // Correct trailing spaces for the shift, so that
1996 // alignment remains equal.
1997 while (off > 0 && lead_len > 0
1998 && leader[lead_len - 1] == ' ')
1999 {
2000 // Don't do it when there is a tab before the space
2001 if (vim_strchr(skipwhite(leader), '\t') != NULL)
2002 break;
2003 --lead_len;
2004 --off;
2005 }
2006
2007 // If the leader ends in white space, don't add an
2008 // extra space
2009 if (lead_len > 0 && VIM_ISWHITE(leader[lead_len - 1]))
2010 extra_space = FALSE;
2011 leader[lead_len] = NUL;
2012 }
2013
2014 if (extra_space)
2015 {
2016 leader[lead_len++] = ' ';
2017 leader[lead_len] = NUL;
2018 }
2019
2020 newcol = lead_len;
2021
2022 // if a new indent will be set below, remove the indent that
2023 // is in the comment leader
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002024 if (newindent || did_si)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002025 {
2026 while (lead_len && VIM_ISWHITE(*leader))
2027 {
2028 --lead_len;
2029 --newcol;
2030 ++leader;
2031 }
2032 }
2033
2034 }
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002035 did_si = can_si = FALSE;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002036 }
2037 else if (comment_end != NULL)
2038 {
2039 // We have finished a comment, so we don't use the leader.
2040 // If this was a C-comment and 'ai' or 'si' is set do a normal
2041 // indent to align with the line containing the start of the
2042 // comment.
2043 if (comment_end[0] == '*' && comment_end[1] == '/' &&
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002044 (curbuf->b_p_ai || do_si))
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002045 {
2046 old_cursor = curwin->w_cursor;
2047 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line);
2048 if ((pos = findmatch(NULL, NUL)) != NULL)
2049 {
2050 curwin->w_cursor.lnum = pos->lnum;
2051 newindent = get_indent();
2052 }
2053 curwin->w_cursor = old_cursor;
2054 }
2055 }
2056 }
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002057
Bram Moolenaar24959102022-05-07 20:01:16 +01002058 // (State == MODE_INSERT || State == MODE_REPLACE), only when dir == FORWARD
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002059 if (p_extra != NULL)
2060 {
2061 *p_extra = saved_char; // restore char that NUL replaced
2062
2063 // When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first
2064 // non-blank.
2065 //
Bram Moolenaar24959102022-05-07 20:01:16 +01002066 // When in MODE_REPLACE state, put the deleted blanks on the replace
2067 // stack, preceded by a NUL, so they can be put back when a BS is
2068 // entered.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002069 if (REPLACE_NORMAL(State))
Bram Moolenaarc667da52019-11-30 20:52:27 +01002070 replace_push(NUL); // end of extra blanks
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002071 if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES))
2072 {
2073 while ((*p_extra == ' ' || *p_extra == '\t')
2074 && (!enc_utf8
2075 || !utf_iscomposing(utf_ptr2char(p_extra + 1))))
2076 {
2077 if (REPLACE_NORMAL(State))
2078 replace_push(*p_extra);
2079 ++p_extra;
2080 ++less_cols_off;
2081 }
2082 }
2083
2084 // columns for marks adjusted for removed columns
2085 less_cols = (int)(p_extra - saved_line);
2086 }
2087
2088 if (p_extra == NULL)
2089 p_extra = (char_u *)""; // append empty line
2090
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002091 // concatenate leader and p_extra, if there is a leader
2092 if (lead_len)
2093 {
2094 if (flags & OPENLINE_COM_LIST && second_line_indent > 0)
2095 {
2096 int i;
2097 int padding = second_line_indent
2098 - (newindent + (int)STRLEN(leader));
2099
2100 // Here whitespace is inserted after the comment char.
2101 // Below, set_indent(newindent, SIN_INSERT) will insert the
2102 // whitespace needed before the comment char.
2103 for (i = 0; i < padding; i++)
2104 {
2105 STRCAT(leader, " ");
2106 less_cols--;
2107 newcol++;
2108 }
2109 }
2110 STRCAT(leader, p_extra);
2111 p_extra = leader;
2112 did_ai = TRUE; // So truncating blanks works with comments
2113 less_cols -= lead_len;
2114 }
2115 else
2116 end_comment_pending = NUL; // turns out there was no leader
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002117
2118 old_cursor = curwin->w_cursor;
2119 if (dir == BACKWARD)
2120 --curwin->w_cursor.lnum;
2121 if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count)
2122 {
2123 if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE)
2124 == FAIL)
2125 goto theend;
2126 // Postpone calling changed_lines(), because it would mess up folding
2127 // with markers.
2128 // Skip mark_adjust when adding a line after the last one, there can't
2129 // be marks there. But still needed in diff mode.
2130 if (curwin->w_cursor.lnum + 1 < curbuf->b_ml.ml_line_count
2131#ifdef FEAT_DIFF
2132 || curwin->w_p_diff
2133#endif
2134 )
2135 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
2136 did_append = TRUE;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002137#ifdef FEAT_PROP_POPUP
Bram Moolenaar24959102022-05-07 20:01:16 +01002138 if ((State & MODE_INSERT) && (State & VREPLACE_FLAG) == 0)
LemonBoy788c06a2022-05-14 18:48:05 +01002139 // Properties after the split move to the next line.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002140 adjust_props_for_split(curwin->w_cursor.lnum, curwin->w_cursor.lnum,
LemonBoy788c06a2022-05-14 18:48:05 +01002141 curwin->w_cursor.col + 1, 0);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002142#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002143 }
2144 else
2145 {
Bram Moolenaar24959102022-05-07 20:01:16 +01002146 // In MODE_VREPLACE state we are starting to replace the next line.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002147 curwin->w_cursor.lnum++;
2148 if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed)
2149 {
2150 // In case we NL to a new line, BS to the previous one, and NL
2151 // again, we don't want to save the new line for undo twice.
Bram Moolenaarc667da52019-11-30 20:52:27 +01002152 (void)u_save_cursor(); // errors are ignored!
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002153 vr_lines_changed++;
2154 }
2155 ml_replace(curwin->w_cursor.lnum, p_extra, TRUE);
2156 changed_bytes(curwin->w_cursor.lnum, 0);
2157 curwin->w_cursor.lnum--;
2158 did_append = FALSE;
2159 }
2160
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002161 if (newindent || did_si)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002162 {
2163 ++curwin->w_cursor.lnum;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002164 if (did_si)
2165 {
2166 int sw = (int)get_sw_value(curbuf);
2167
2168 if (p_sr)
2169 newindent -= newindent % sw;
2170 newindent += sw;
2171 }
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002172 // Copy the indent
2173 if (curbuf->b_p_ci)
2174 {
2175 (void)copy_indent(newindent, saved_line);
2176
2177 // Set the 'preserveindent' option so that any further screwing
2178 // with the line doesn't entirely destroy our efforts to preserve
2179 // it. It gets restored at the function end.
2180 curbuf->b_p_pi = TRUE;
2181 }
2182 else
2183 (void)set_indent(newindent, SIN_INSERT);
2184 less_cols -= curwin->w_cursor.col;
2185
2186 ai_col = curwin->w_cursor.col;
2187
Bram Moolenaar24959102022-05-07 20:01:16 +01002188 // In MODE_REPLACE state, for each character in the new indent, there
2189 // must be a NUL on the replace stack, for when it is deleted with BS
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002190 if (REPLACE_NORMAL(State))
2191 for (n = 0; n < (int)curwin->w_cursor.col; ++n)
2192 replace_push(NUL);
2193 newcol += curwin->w_cursor.col;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002194 if (no_si)
2195 did_si = FALSE;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002196 }
2197
Bram Moolenaar24959102022-05-07 20:01:16 +01002198 // In MODE_REPLACE state, for each character in the extra leader, there
2199 // must be a NUL on the replace stack, for when it is deleted with BS.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002200 if (REPLACE_NORMAL(State))
2201 while (lead_len-- > 0)
2202 replace_push(NUL);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002203
2204 curwin->w_cursor = old_cursor;
2205
2206 if (dir == FORWARD)
2207 {
Bram Moolenaar24959102022-05-07 20:01:16 +01002208 if (trunc_line || (State & MODE_INSERT))
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002209 {
2210 // truncate current line at cursor
2211 saved_line[curwin->w_cursor.col] = NUL;
2212 // Remove trailing white space, unless OPENLINE_KEEPTRAIL used.
2213 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL))
2214 truncate_spaces(saved_line);
2215 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE);
2216 saved_line = NULL;
2217 if (did_append)
2218 {
2219 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
2220 curwin->w_cursor.lnum + 1, 1L);
2221 did_append = FALSE;
2222
2223 // Move marks after the line break to the new line.
2224 if (flags & OPENLINE_MARKFIX)
2225 mark_col_adjust(curwin->w_cursor.lnum,
2226 curwin->w_cursor.col + less_cols_off,
2227 1L, (long)-less_cols, 0);
LemonBoy788c06a2022-05-14 18:48:05 +01002228#ifdef FEAT_PROP_POPUP
2229 // Keep into account the deleted blanks on the new line.
2230 if (curbuf->b_has_textprop && less_cols_off != 0)
2231 adjust_prop_columns(curwin->w_cursor.lnum + 1, 0,
2232 -less_cols_off, 0);
2233#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002234 }
2235 else
2236 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
2237 }
2238
2239 // Put the cursor on the new line. Careful: the scrollup() above may
2240 // have moved w_cursor, we must use old_cursor.
2241 curwin->w_cursor.lnum = old_cursor.lnum + 1;
2242 }
2243 if (did_append)
2244 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L);
2245
2246 curwin->w_cursor.col = newcol;
2247 curwin->w_cursor.coladd = 0;
2248
Bram Moolenaar24959102022-05-07 20:01:16 +01002249 // In MODE_VREPLACE state, we are handling the replace stack ourselves, so
2250 // stop fixthisline() from doing it (via change_indent()) by telling it
2251 // we're in normal MODE_INSERT state.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002252 if (State & VREPLACE_FLAG)
2253 {
2254 vreplace_mode = State; // So we know to put things right later
Bram Moolenaar24959102022-05-07 20:01:16 +01002255 State = MODE_INSERT;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002256 }
2257 else
2258 vreplace_mode = 0;
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002259
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002260 // May do lisp indenting.
2261 if (!p_paste
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002262 && leader == NULL
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002263 && curbuf->b_p_lisp
2264 && curbuf->b_p_ai)
2265 {
2266 fixthisline(get_lisp_indent);
2267 ai_col = (colnr_T)getwhitecols_curline();
2268 }
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002269
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002270 // May do indenting after opening a new line.
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00002271 if (do_cindent)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002272 {
2273 do_c_expr_indent();
2274 ai_col = (colnr_T)getwhitecols_curline();
2275 }
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002276
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002277 if (vreplace_mode != 0)
2278 State = vreplace_mode;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002279
Bram Moolenaar24959102022-05-07 20:01:16 +01002280 // Finally, MODE_VREPLACE gets the stuff on the new line, then puts back
2281 // the original line, and inserts the new stuff char by char, pushing old
2282 // stuff onto the replace stack (via ins_char()).
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002283 if (State & VREPLACE_FLAG)
2284 {
2285 // Put new line in p_extra
2286 p_extra = vim_strsave(ml_get_curline());
2287 if (p_extra == NULL)
2288 goto theend;
2289
2290 // Put back original line
2291 ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
2292
2293 // Insert new stuff into line again
2294 curwin->w_cursor.col = 0;
2295 curwin->w_cursor.coladd = 0;
2296 ins_bytes(p_extra); // will call changed_bytes()
2297 vim_free(p_extra);
2298 next_line = NULL;
2299 }
2300
2301 retval = OK; // success!
2302theend:
2303 curbuf->b_p_pi = saved_pi;
2304 vim_free(saved_line);
2305 vim_free(next_line);
2306 vim_free(allocated);
2307 return retval;
2308}
2309
2310/*
2311 * Delete from cursor to end of line.
2312 * Caller must have prepared for undo.
2313 * If "fixpos" is TRUE fix the cursor position when done.
2314 *
2315 * Return FAIL for failure, OK otherwise.
2316 */
2317 int
2318truncate_line(int fixpos)
2319{
2320 char_u *newp;
2321 linenr_T lnum = curwin->w_cursor.lnum;
2322 colnr_T col = curwin->w_cursor.col;
LemonBoyd0b1a092022-05-12 18:45:18 +01002323 char_u *old_line;
2324 int deleted;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002325
LemonBoyd0b1a092022-05-12 18:45:18 +01002326 old_line = ml_get(lnum);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002327 if (col == 0)
2328 newp = vim_strsave((char_u *)"");
2329 else
LemonBoyd0b1a092022-05-12 18:45:18 +01002330 newp = vim_strnsave(old_line, col);
2331 deleted = (int)STRLEN(old_line) - col;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002332
2333 if (newp == NULL)
2334 return FAIL;
2335
2336 ml_replace(lnum, newp, FALSE);
2337
2338 // mark the buffer as changed and prepare for displaying
LemonBoyd0b1a092022-05-12 18:45:18 +01002339 inserted_bytes(lnum, curwin->w_cursor.col, -deleted);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002340
2341 // If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2342 if (fixpos && curwin->w_cursor.col > 0)
2343 --curwin->w_cursor.col;
2344
2345 return OK;
2346}
2347
2348/*
2349 * Delete "nlines" lines at the cursor.
2350 * Saves the lines for undo first if "undo" is TRUE.
2351 */
2352 void
2353del_lines(long nlines, int undo)
2354{
2355 long n;
2356 linenr_T first = curwin->w_cursor.lnum;
2357
2358 if (nlines <= 0)
2359 return;
2360
2361 // save the deleted lines for undo
2362 if (undo && u_savedel(first, nlines) == FAIL)
2363 return;
2364
2365 for (n = 0; n < nlines; )
2366 {
2367 if (curbuf->b_ml.ml_flags & ML_EMPTY) // nothing to delete
2368 break;
2369
Bram Moolenaarca70c072020-05-30 20:30:46 +02002370 ml_delete_flags(first, ML_DEL_MESSAGE);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002371 ++n;
2372
2373 // If we delete the last line in the file, stop
2374 if (first > curbuf->b_ml.ml_line_count)
2375 break;
2376 }
2377
2378 // Correct the cursor position before calling deleted_lines_mark(), it may
2379 // trigger a callback to display the cursor.
2380 curwin->w_cursor.col = 0;
2381 check_cursor_lnum();
2382
2383 // adjust marks, mark the buffer as changed and prepare for displaying
2384 deleted_lines_mark(first, n);
2385}