blob: 5cddf9b340435a2bbb9ddf87ceb3312e3225d676 [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
Yegappan Lakshmanan3f0092c2022-10-16 21:43:07 +010031 if (curbuf->b_did_warn
32 || curbufIsChanged()
33 || autocmd_busy
34 || !curbuf->b_p_ro)
35 return;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +020036
Yegappan Lakshmanan3f0092c2022-10-16 21:43:07 +010037 ++curbuf_lock;
38 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
39 --curbuf_lock;
40 if (!curbuf->b_p_ro)
41 return;
42
43 // Do what msg() does, but with a column offset if the warning should
44 // be after the mode message.
45 msg_start();
46 if (msg_row == Rows - 1)
47 msg_col = col;
48 msg_source(HL_ATTR(HLF_W));
49 msg_puts_attr(_(w_readonly), HL_ATTR(HLF_W) | MSG_HIST);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +020050#ifdef FEAT_EVAL
Yegappan Lakshmanan3f0092c2022-10-16 21:43:07 +010051 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +020052#endif
Yegappan Lakshmanan3f0092c2022-10-16 21:43:07 +010053 msg_clr_eos();
54 (void)msg_end();
55 if (msg_silent == 0 && !silent_mode
Bram Moolenaar3f86ca02019-05-11 18:30:00 +020056#ifdef FEAT_EVAL
Yegappan Lakshmanan3f0092c2022-10-16 21:43:07 +010057 && time_for_testing != 1
Bram Moolenaar3f86ca02019-05-11 18:30:00 +020058#endif
Yegappan Lakshmanan3f0092c2022-10-16 21:43:07 +010059 )
60 {
61 out_flush();
62 ui_delay(1002L, TRUE); // give the user time to think about it
Bram Moolenaar3f86ca02019-05-11 18:30:00 +020063 }
Yegappan Lakshmanan3f0092c2022-10-16 21:43:07 +010064 curbuf->b_did_warn = TRUE;
65 redraw_cmdline = FALSE; // don't redraw and erase the message
66 if (msg_row < Rows - 1)
67 showmode();
Bram Moolenaar3f86ca02019-05-11 18:30:00 +020068}
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.
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +0100103 if (curbuf->b_may_swap && !bt_dontwrite(curbuf))
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200104 {
105 int save_need_wait_return = need_wait_return;
106
107 need_wait_return = FALSE;
108 ml_open_file(curbuf);
109
110 // The ml_open_file() can cause an ATTENTION message.
111 // Wait two seconds, to make sure the user reads this unexpected
112 // message. Since we could be anywhere, call wait_return() now,
113 // and don't let the emsg() set msg_scroll.
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100114 if (need_wait_return && emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200115 {
116 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +0100117 ui_delay(2002L, TRUE);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200118 wait_return(TRUE);
119 msg_scroll = save_msg_scroll;
120 }
121 else
122 need_wait_return = save_need_wait_return;
123 }
124 changed_internal();
125 }
126 ++CHANGEDTICK(curbuf);
127
128#ifdef FEAT_SEARCH_EXTRA
129 // If a pattern is highlighted, the position may now be invalid.
130 highlight_match = FALSE;
131#endif
132}
133
134/*
135 * Internal part of changed(), no user interaction.
136 * Also used for recovery.
137 */
138 void
139changed_internal(void)
140{
141 curbuf->b_changed = TRUE;
142 ml_setflags(curbuf);
143 check_status(curbuf);
144 redraw_tabline = TRUE;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200145 need_maketitle = TRUE; // set window title later
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200146}
147
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200148#ifdef FEAT_EVAL
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200149static long next_listener_id = 0;
150
151/*
Bram Moolenaar4a0a1612019-07-17 22:00:19 +0200152 * Check if the change at "lnum" is above or overlaps with an existing
153 * change. If above then flush changes and invoke listeners.
Bram Moolenaardda41442019-05-16 22:11:47 +0200154 */
Bram Moolenaar55737c22022-02-14 14:51:22 +0000155 static void
Bram Moolenaardda41442019-05-16 22:11:47 +0200156check_recorded_changes(
157 buf_T *buf,
158 linenr_T lnum,
Bram Moolenaardda41442019-05-16 22:11:47 +0200159 linenr_T lnume,
Bram Moolenaar4a0a1612019-07-17 22:00:19 +0200160 long xtra)
Bram Moolenaardda41442019-05-16 22:11:47 +0200161{
Yegappan Lakshmanan3f0092c2022-10-16 21:43:07 +0100162 if (buf->b_recorded_changes == NULL || xtra == 0)
163 return;
Bram Moolenaardda41442019-05-16 22:11:47 +0200164
Yegappan Lakshmanan3f0092c2022-10-16 21:43:07 +0100165 listitem_T *li;
166 linenr_T prev_lnum;
167 linenr_T prev_lnume;
168
169 FOR_ALL_LIST_ITEMS(buf->b_recorded_changes, li)
170 {
171 prev_lnum = (linenr_T)dict_get_number(
172 li->li_tv.vval.v_dict, "lnum");
173 prev_lnume = (linenr_T)dict_get_number(
174 li->li_tv.vval.v_dict, "end");
175 if (prev_lnum >= lnum || prev_lnum > lnume || prev_lnume >= lnum)
Bram Moolenaardda41442019-05-16 22:11:47 +0200176 {
Yegappan Lakshmanan3f0092c2022-10-16 21:43:07 +0100177 // the current change is going to make the line number in
178 // the older change invalid, flush now
179 invoke_listeners(curbuf);
180 break;
Bram Moolenaardda41442019-05-16 22:11:47 +0200181 }
182 }
Bram Moolenaardda41442019-05-16 22:11:47 +0200183}
184
185/*
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200186 * Record a change for listeners added with listener_add().
Bram Moolenaardda41442019-05-16 22:11:47 +0200187 * Always for the current buffer.
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200188 */
189 static void
190may_record_change(
191 linenr_T lnum,
192 colnr_T col,
193 linenr_T lnume,
194 long xtra)
195{
196 dict_T *dict;
197
198 if (curbuf->b_listener == NULL)
199 return;
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200200
201 // If the new change is going to change the line numbers in already listed
202 // changes, then flush.
Bram Moolenaar55737c22022-02-14 14:51:22 +0000203 check_recorded_changes(curbuf, lnum, lnume, xtra);
Bram Moolenaardda41442019-05-16 22:11:47 +0200204
205 if (curbuf->b_recorded_changes == NULL)
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200206 {
Bram Moolenaardda41442019-05-16 22:11:47 +0200207 curbuf->b_recorded_changes = list_alloc();
208 if (curbuf->b_recorded_changes == NULL) // out of memory
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200209 return;
Bram Moolenaardda41442019-05-16 22:11:47 +0200210 ++curbuf->b_recorded_changes->lv_refcount;
211 curbuf->b_recorded_changes->lv_lock = VAR_FIXED;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200212 }
213
214 dict = dict_alloc();
215 if (dict == NULL)
216 return;
217 dict_add_number(dict, "lnum", (varnumber_T)lnum);
218 dict_add_number(dict, "end", (varnumber_T)lnume);
219 dict_add_number(dict, "added", (varnumber_T)xtra);
Bram Moolenaara3347722019-05-11 21:14:24 +0200220 dict_add_number(dict, "col", (varnumber_T)col + 1);
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200221
Bram Moolenaardda41442019-05-16 22:11:47 +0200222 list_append_dict(curbuf->b_recorded_changes, dict);
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200223}
224
225/*
226 * listener_add() function
227 */
228 void
229f_listener_add(typval_T *argvars, typval_T *rettv)
230{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200231 callback_T callback;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200232 listener_T *lnr;
Bram Moolenaara3347722019-05-11 21:14:24 +0200233 buf_T *buf = curbuf;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200234
Yegappan Lakshmanan5bca9062021-07-24 21:33:26 +0200235 if (in_vim9script() && check_for_opt_buffer_arg(argvars, 1) == FAIL)
236 return;
237
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200238 callback = get_callback(&argvars[0]);
239 if (callback.cb_name == NULL)
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200240 return;
241
Bram Moolenaara3347722019-05-11 21:14:24 +0200242 if (argvars[1].v_type != VAR_UNKNOWN)
243 {
244 buf = get_buf_arg(&argvars[1]);
245 if (buf == NULL)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200246 {
247 free_callback(&callback);
Bram Moolenaara3347722019-05-11 21:14:24 +0200248 return;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200249 }
Bram Moolenaara3347722019-05-11 21:14:24 +0200250 }
251
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200252 lnr = ALLOC_CLEAR_ONE(listener_T);
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200253 if (lnr == NULL)
254 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200255 free_callback(&callback);
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200256 return;
257 }
Bram Moolenaara3347722019-05-11 21:14:24 +0200258 lnr->lr_next = buf->b_listener;
259 buf->b_listener = lnr;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200260
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200261 set_callback(&lnr->lr_callback, &callback);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +0000262 if (callback.cb_free_name)
263 vim_free(callback.cb_name);
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200264
265 lnr->lr_id = ++next_listener_id;
266 rettv->vval.v_number = lnr->lr_id;
267}
268
269/*
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200270 * listener_flush() function
271 */
272 void
273f_listener_flush(typval_T *argvars, typval_T *rettv UNUSED)
274{
275 buf_T *buf = curbuf;
276
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200277 if (in_vim9script() && check_for_opt_buffer_arg(argvars, 0) == FAIL)
278 return;
279
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200280 if (argvars[0].v_type != VAR_UNKNOWN)
281 {
282 buf = get_buf_arg(&argvars[0]);
283 if (buf == NULL)
284 return;
285 }
286 invoke_listeners(buf);
287}
288
Bram Moolenaar4b4b1b82021-09-11 15:06:44 +0200289
290 static void
291remove_listener(buf_T *buf, listener_T *lnr, listener_T *prev)
292{
293 if (prev != NULL)
294 prev->lr_next = lnr->lr_next;
295 else
296 buf->b_listener = lnr->lr_next;
297 free_callback(&lnr->lr_callback);
298 vim_free(lnr);
299}
300
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200301/*
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200302 * listener_remove() function
303 */
304 void
Bram Moolenaar7b73f912019-07-13 13:03:02 +0200305f_listener_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200306{
307 listener_T *lnr;
308 listener_T *next;
Bram Moolenaar7b73f912019-07-13 13:03:02 +0200309 listener_T *prev;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200310 int id;
Bram Moolenaara3347722019-05-11 21:14:24 +0200311 buf_T *buf;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200312
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200313 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
314 return;
315
316 id = tv_get_number(argvars);
Bram Moolenaar86173482019-10-01 17:02:16 +0200317 FOR_ALL_BUFFERS(buf)
Bram Moolenaar7b73f912019-07-13 13:03:02 +0200318 {
319 prev = NULL;
Bram Moolenaara3347722019-05-11 21:14:24 +0200320 for (lnr = buf->b_listener; lnr != NULL; lnr = next)
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200321 {
Bram Moolenaara3347722019-05-11 21:14:24 +0200322 next = lnr->lr_next;
323 if (lnr->lr_id == id)
324 {
zeertzjqcfe45652022-05-27 17:26:55 +0100325 if (textlock > 0)
Bram Moolenaar4b4b1b82021-09-11 15:06:44 +0200326 {
327 // in invoke_listeners(), clear ID and delete later
328 lnr->lr_id = 0;
329 return;
330 }
331 remove_listener(buf, lnr, prev);
Bram Moolenaar7b73f912019-07-13 13:03:02 +0200332 rettv->vval.v_number = 1;
333 return;
Bram Moolenaara3347722019-05-11 21:14:24 +0200334 }
335 prev = lnr;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200336 }
Bram Moolenaar7b73f912019-07-13 13:03:02 +0200337 }
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200338}
339
340/*
Bram Moolenaardda41442019-05-16 22:11:47 +0200341 * Called before inserting a line above "lnum"/"lnum3" or deleting line "lnum"
342 * to "lnume".
343 */
344 void
345may_invoke_listeners(buf_T *buf, linenr_T lnum, linenr_T lnume, int added)
346{
Bram Moolenaar4a0a1612019-07-17 22:00:19 +0200347 check_recorded_changes(buf, lnum, lnume, added);
Bram Moolenaardda41442019-05-16 22:11:47 +0200348}
349
350/*
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200351 * Called when a sequence of changes is done: invoke listeners added with
352 * listener_add().
353 */
354 void
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200355invoke_listeners(buf_T *buf)
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200356{
357 listener_T *lnr;
358 typval_T rettv;
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200359 typval_T argv[6];
360 listitem_T *li;
361 linenr_T start = MAXLNUM;
362 linenr_T end = 0;
363 linenr_T added = 0;
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200364 int save_updating_screen = updating_screen;
365 static int recursive = FALSE;
Bram Moolenaar4b4b1b82021-09-11 15:06:44 +0200366 listener_T *next;
Yegappan Lakshmanan956be462022-09-02 17:12:07 +0100367 listener_T *prev;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200368
Bram Moolenaardda41442019-05-16 22:11:47 +0200369 if (buf->b_recorded_changes == NULL // nothing changed
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200370 || buf->b_listener == NULL // no listeners
371 || recursive) // already busy
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200372 return;
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200373 recursive = TRUE;
374
375 // Block messages on channels from being handled, so that they don't make
376 // text changes here.
377 ++updating_screen;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200378
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200379 argv[0].v_type = VAR_NUMBER;
380 argv[0].vval.v_number = buf->b_fnum; // a:bufnr
381
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200382 FOR_ALL_LIST_ITEMS(buf->b_recorded_changes, li)
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200383 {
384 varnumber_T lnum;
385
Bram Moolenaard61efa52022-07-23 09:52:04 +0100386 lnum = dict_get_number(li->li_tv.vval.v_dict, "lnum");
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200387 if (start > lnum)
388 start = lnum;
Bram Moolenaard61efa52022-07-23 09:52:04 +0100389 lnum = dict_get_number(li->li_tv.vval.v_dict, "end");
Bram Moolenaar336bf2b2019-10-24 20:07:07 +0200390 if (end < lnum)
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200391 end = lnum;
Bram Moolenaard61efa52022-07-23 09:52:04 +0100392 added += dict_get_number(li->li_tv.vval.v_dict, "added");
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200393 }
394 argv[1].v_type = VAR_NUMBER;
395 argv[1].vval.v_number = start;
396 argv[2].v_type = VAR_NUMBER;
397 argv[2].vval.v_number = end;
398 argv[3].v_type = VAR_NUMBER;
399 argv[3].vval.v_number = added;
400
401 argv[4].v_type = VAR_LIST;
Bram Moolenaardda41442019-05-16 22:11:47 +0200402 argv[4].vval.v_list = buf->b_recorded_changes;
zeertzjqcfe45652022-05-27 17:26:55 +0100403 ++textlock;
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200404
405 for (lnr = buf->b_listener; lnr != NULL; lnr = lnr->lr_next)
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200406 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200407 call_callback(&lnr->lr_callback, -1, &rettv, 5, argv);
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200408 clear_tv(&rettv);
409 }
410
Bram Moolenaar4b4b1b82021-09-11 15:06:44 +0200411 // If f_listener_remove() was called may have to remove a listener now.
Yegappan Lakshmanan956be462022-09-02 17:12:07 +0100412 prev = NULL;
Bram Moolenaar4b4b1b82021-09-11 15:06:44 +0200413 for (lnr = buf->b_listener; lnr != NULL; lnr = next)
414 {
Bram Moolenaar4b4b1b82021-09-11 15:06:44 +0200415 next = lnr->lr_next;
416 if (lnr->lr_id == 0)
417 remove_listener(buf, lnr, prev);
418 else
419 prev = lnr;
420 }
421
zeertzjqcfe45652022-05-27 17:26:55 +0100422 --textlock;
Bram Moolenaardda41442019-05-16 22:11:47 +0200423 list_unref(buf->b_recorded_changes);
424 buf->b_recorded_changes = NULL;
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200425
426 if (save_updating_screen)
427 updating_screen = TRUE;
428 else
429 after_updating_screen(TRUE);
430 recursive = FALSE;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200431}
Bram Moolenaar86173482019-10-01 17:02:16 +0200432
433/*
434 * Remove all listeners associated with "buf".
435 */
436 void
437remove_listeners(buf_T *buf)
438{
439 listener_T *lnr;
440 listener_T *next;
441
442 for (lnr = buf->b_listener; lnr != NULL; lnr = next)
443 {
444 next = lnr->lr_next;
445 free_callback(&lnr->lr_callback);
446 vim_free(lnr);
447 }
448 buf->b_listener = NULL;
449}
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200450#endif
451
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200452/*
453 * Common code for when a change was made.
454 * See changed_lines() for the arguments.
455 * Careful: may trigger autocommands that reload the buffer.
456 */
457 static void
458changed_common(
459 linenr_T lnum,
460 colnr_T col,
461 linenr_T lnume,
462 long xtra)
463{
464 win_T *wp;
465 tabpage_T *tp;
466 int i;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200467 int cols;
468 pos_T *p;
469 int add;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200470
471 // mark the buffer as modified
472 changed();
473
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200474#ifdef FEAT_EVAL
475 may_record_change(lnum, col, lnume, xtra);
476#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200477#ifdef FEAT_DIFF
478 if (curwin->w_p_diff && diff_internal())
Yee Cheng Chin9943d472025-03-26 19:41:02 +0100479 {
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200480 curtab->tp_diff_update = TRUE;
Yee Cheng Chin9943d472025-03-26 19:41:02 +0100481 diff_update_line(lnum);
482 }
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200483#endif
484
485 // set the '. mark
Bram Moolenaare1004402020-10-24 20:49:43 +0200486 if ((cmdmod.cmod_flags & CMOD_KEEPJUMPS) == 0)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200487 {
488 curbuf->b_last_change.lnum = lnum;
489 curbuf->b_last_change.col = col;
490
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200491 // Create a new entry if a new undo-able change was started or we
492 // don't have an entry yet.
493 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
494 {
495 if (curbuf->b_changelistlen == 0)
496 add = TRUE;
497 else
498 {
499 // Don't create a new entry when the line number is the same
500 // as the last one and the column is not too far away. Avoids
501 // creating many entries for typing "xxxxx".
502 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
503 if (p->lnum != lnum)
504 add = TRUE;
505 else
506 {
507 cols = comp_textwidth(FALSE);
508 if (cols == 0)
509 cols = 79;
510 add = (p->col + cols < col || col + cols < p->col);
511 }
512 }
513 if (add)
514 {
515 // This is the first of a new sequence of undo-able changes
516 // and it's at some distance of the last change. Use a new
517 // position in the changelist.
518 curbuf->b_new_change = FALSE;
519
520 if (curbuf->b_changelistlen == JUMPLISTSIZE)
521 {
522 // changelist is full: remove oldest entry
523 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
524 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
525 sizeof(pos_T) * (JUMPLISTSIZE - 1));
526 FOR_ALL_TAB_WINDOWS(tp, wp)
527 {
528 // Correct position in changelist for other windows on
529 // this buffer.
530 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
531 --wp->w_changelistidx;
532 }
533 }
534 FOR_ALL_TAB_WINDOWS(tp, wp)
535 {
536 // For other windows, if the position in the changelist is
537 // at the end it stays at the end.
538 if (wp->w_buffer == curbuf
539 && wp->w_changelistidx == curbuf->b_changelistlen)
540 ++wp->w_changelistidx;
541 }
542 ++curbuf->b_changelistlen;
543 }
544 }
545 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
546 curbuf->b_last_change;
547 // The current window is always after the last change, so that "g,"
548 // takes you back to it.
549 curwin->w_changelistidx = curbuf->b_changelistlen;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200550 }
551
Bram Moolenaar7ce5b2b2022-05-16 19:40:59 +0100552 if (VIsual_active)
553 check_visual_pos();
554
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200555 FOR_ALL_TAB_WINDOWS(tp, wp)
556 {
557 if (wp->w_buffer == curbuf)
558 {
Bram Moolenaar94377372022-02-16 20:30:52 +0000559 linenr_T last = lnume + xtra - 1; // last line after the change
Luuk van Baal5d01f862023-05-11 19:24:20 +0100560
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200561 // Mark this window to be redrawn later.
Bram Moolenaar471c0fa2022-08-22 15:19:16 +0100562 if (!redraw_not_allowed && wp->w_redr_type < UPD_VALID)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100563 wp->w_redr_type = UPD_VALID;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200564
zeertzjqf2d90a32024-02-12 20:28:01 +0100565 // When inserting/deleting lines and the window has specific lines
566 // to be redrawn, w_redraw_top and w_redraw_bot may now be invalid,
567 // so just redraw everything.
568 if (xtra != 0 && wp->w_redraw_top != 0)
569 redraw_win_later(wp, UPD_NOT_VALID);
570
Luuk van Baal5d01f862023-05-11 19:24:20 +0100571 // Reset "w_skipcol" if the topline length has become smaller to
572 // such a degree that nothing will be visible anymore, accounting
573 // for 'smoothscroll' <<< or 'listchars' "precedes" marker.
574 if (wp->w_skipcol > 0
575 && (last < wp->w_topline
576 || (wp->w_topline >= lnum
577 && wp->w_topline < lnume
zeertzjq2c47ab82025-02-13 20:34:34 +0100578 && linetabsize_eol(wp, wp->w_topline)
Luuk van Baalcb204e62024-04-02 20:49:45 +0200579 <= wp->w_skipcol + sms_marker_overlap(wp, -1))))
Luuk van Baal5d01f862023-05-11 19:24:20 +0100580 wp->w_skipcol = 0;
581
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200582 // Check if a change in the buffer has invalidated the cached
583 // values for the cursor.
584#ifdef FEAT_FOLDING
585 // Update the folds for this window. Can't postpone this, because
586 // a following operator might work on the whole fold: ">>dd".
Bram Moolenaar94377372022-02-16 20:30:52 +0000587 foldUpdate(wp, lnum, last);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200588
589 // The change may cause lines above or below the change to become
590 // included in a fold. Set lnum/lnume to the first/last line that
591 // might be displayed differently.
592 // Set w_cline_folded here as an efficient way to update it when
593 // inserting lines just above a closed fold.
594 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
595 if (wp->w_cursor.lnum == lnum)
596 wp->w_cline_folded = i;
Bram Moolenaar94377372022-02-16 20:30:52 +0000597 i = hasFoldingWin(wp, last, NULL, &last, FALSE, NULL);
598 if (wp->w_cursor.lnum == last)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200599 wp->w_cline_folded = i;
600
601 // If the changed line is in a range of previously folded lines,
602 // compare with the first line in that range.
603 if (wp->w_cursor.lnum <= lnum)
604 {
605 i = find_wl_entry(wp, lnum);
606 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
607 changed_line_abv_curs_win(wp);
608 }
609#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200610 if (wp->w_cursor.lnum > lnum)
611 changed_line_abv_curs_win(wp);
612 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
613 changed_cline_bef_curs_win(wp);
614 if (wp->w_botline >= lnum)
615 {
Bram Moolenaar5bea41d2021-07-13 22:21:44 +0200616 if (xtra < 0)
617 invalidate_botline_win(wp);
618 else
619 // Assume that botline doesn't change (inserted lines make
620 // other lines scroll down below botline).
621 approximate_botline_win(wp);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200622 }
623
624 // Check if any w_lines[] entries have become invalid.
625 // For entries below the change: Correct the lnums for
626 // inserted/deleted lines. Makes it possible to stop displaying
627 // after the change.
628 for (i = 0; i < wp->w_lines_valid; ++i)
629 if (wp->w_lines[i].wl_valid)
630 {
631 if (wp->w_lines[i].wl_lnum >= lnum)
632 {
Bram Moolenaar61fdbfa2023-02-04 13:57:55 +0000633 // Do not change wl_lnum at index zero, it is used to
634 // compare with w_topline. Invalidate it instead.
635 if (wp->w_lines[i].wl_lnum < lnume || i == 0)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200636 {
637 // line included in change
638 wp->w_lines[i].wl_valid = FALSE;
639 }
640 else if (xtra != 0)
641 {
642 // line below change
643 wp->w_lines[i].wl_lnum += xtra;
644#ifdef FEAT_FOLDING
645 wp->w_lines[i].wl_lastlnum += xtra;
646#endif
647 }
648 }
649#ifdef FEAT_FOLDING
650 else if (wp->w_lines[i].wl_lastlnum >= lnum)
651 {
652 // change somewhere inside this range of folded lines,
653 // may need to be redrawn
654 wp->w_lines[i].wl_valid = FALSE;
655 }
656#endif
657 }
658
659#ifdef FEAT_FOLDING
660 // Take care of side effects for setting w_topline when folds have
661 // changed. Esp. when the buffer was changed in another window.
662 if (hasAnyFolding(wp))
663 set_topline(wp, wp->w_topline);
664#endif
zeertzjq8c979602022-04-07 15:08:01 +0100665 // If lines have been added or removed, relative numbering always
zeertzjq7ce34c92024-02-08 11:34:55 +0100666 // requires an update even if cursor didn't move.
Lewis Russell16246392022-03-29 11:38:17 +0100667 if (wp->w_p_rnu && xtra != 0)
zeertzjq8c979602022-04-07 15:08:01 +0100668 wp->w_last_cursor_lnum_rnu = 0;
zeertzjq7ce34c92024-02-08 11:34:55 +0100669
Bram Moolenaar11a58af2019-10-24 22:32:31 +0200670#ifdef FEAT_SYN_HL
zeertzjq7ce34c92024-02-08 11:34:55 +0100671 if (wp->w_p_cul && wp->w_last_cursorline >= lnum)
Bram Moolenaar11a58af2019-10-24 22:32:31 +0200672 {
zeertzjq7ce34c92024-02-08 11:34:55 +0100673 if (wp->w_last_cursorline < lnume)
674 // If 'cursorline' was inside the change, it has already
675 // been invalidated in w_lines[] by the loop above.
676 wp->w_last_cursorline = 0;
677 else
678 // If 'cursorline' was below the change, adjust its lnum.
679 wp->w_last_cursorline += xtra;
Bram Moolenaar11a58af2019-10-24 22:32:31 +0200680 }
681#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200682 }
Bram Moolenaar368137a2022-05-31 13:43:12 +0100683#ifdef FEAT_SEARCH_EXTRA
684 if (wp == curwin && xtra != 0 && search_hl_has_cursor_lnum >= lnum)
685 search_hl_has_cursor_lnum += xtra;
686#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200687 }
688
689 // Call update_screen() later, which checks out what needs to be redrawn,
690 // since it notices b_mod_set and then uses b_mod_*.
Bram Moolenaar471c0fa2022-08-22 15:19:16 +0100691 set_must_redraw(UPD_VALID);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200692
693 // when the cursor line is changed always trigger CursorMoved
694 if (lnum <= curwin->w_cursor.lnum
695 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
696 last_cursormoved.lnum = 0;
697}
698
699 static void
700changedOneline(buf_T *buf, linenr_T lnum)
701{
702 if (buf->b_mod_set)
703 {
704 // find the maximum area that must be redisplayed
705 if (lnum < buf->b_mod_top)
706 buf->b_mod_top = lnum;
707 else if (lnum >= buf->b_mod_bot)
708 buf->b_mod_bot = lnum + 1;
709 }
710 else
711 {
712 // set the area that must be redisplayed to one line
713 buf->b_mod_set = TRUE;
714 buf->b_mod_top = lnum;
715 buf->b_mod_bot = lnum + 1;
716 buf->b_mod_xlines = 0;
717 }
718}
719
720/*
721 * Changed bytes within a single line for the current buffer.
722 * - marks the windows on this buffer to be redisplayed
723 * - marks the buffer changed by calling changed()
724 * - invalidates cached values
725 * Careful: may trigger autocommands that reload the buffer.
726 */
727 void
728changed_bytes(linenr_T lnum, colnr_T col)
729{
730 changedOneline(curbuf, lnum);
731 changed_common(lnum, col, lnum + 1, 0L);
732
Bram Moolenaar26f09ea2022-09-27 16:29:38 +0100733#ifdef FEAT_SPELL
734 // When text has been changed at the end of the line, possibly the start of
735 // the next line may have SpellCap that should be removed or it needs to be
736 // displayed. Schedule the next line for redrawing just in case.
Bram Moolenaarf3ef0262022-10-05 13:29:15 +0100737 // Don't do this when displaying '$' at the end of changed text.
738 if (spell_check_window(curwin)
739 && lnum < curbuf->b_ml.ml_line_count
740 && vim_strchr(p_cpo, CPO_DOLLAR) == NULL)
Bram Moolenaar26f09ea2022-09-27 16:29:38 +0100741 redrawWinline(curwin, lnum + 1);
742#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200743#ifdef FEAT_DIFF
744 // Diff highlighting in other diff windows may need to be updated too.
745 if (curwin->w_p_diff)
746 {
747 win_T *wp;
748 linenr_T wlnum;
749
750 FOR_ALL_WINDOWS(wp)
751 if (wp->w_p_diff && wp != curwin)
752 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100753 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200754 wlnum = diff_lnum_win(lnum, wp);
755 if (wlnum > 0)
756 changedOneline(wp->w_buffer, wlnum);
757 }
758 }
759#endif
760}
761
762/*
763 * Like changed_bytes() but also adjust text properties for "added" bytes.
764 * When "added" is negative text was deleted.
765 */
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200766 void
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200767inserted_bytes(linenr_T lnum, colnr_T col, int added UNUSED)
768{
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100769#ifdef FEAT_PROP_POPUP
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200770 if (curbuf->b_has_textprop && added != 0)
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200771 adjust_prop_columns(lnum, col, added, 0);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200772#endif
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200773
774 changed_bytes(lnum, col);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200775}
776
777/*
778 * Appended "count" lines below line "lnum" in the current buffer.
779 * Must be called AFTER the change and after mark_adjust().
780 * Takes care of marking the buffer to be redrawn and sets the changed flag.
781 */
782 void
783appended_lines(linenr_T lnum, long count)
784{
785 changed_lines(lnum + 1, 0, lnum + 1, count);
786}
787
788/*
789 * Like appended_lines(), but adjust marks first.
790 */
791 void
792appended_lines_mark(linenr_T lnum, long count)
793{
Brandon Simmonsda3dd7d2023-01-17 19:48:07 +0000794 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200795 changed_lines(lnum + 1, 0, lnum + 1, count);
796}
797
798/*
799 * Deleted "count" lines at line "lnum" in the current buffer.
800 * Must be called AFTER the change and after mark_adjust().
801 * Takes care of marking the buffer to be redrawn and sets the changed flag.
802 */
803 void
804deleted_lines(linenr_T lnum, long count)
805{
806 changed_lines(lnum, 0, lnum + count, -count);
807}
808
809/*
810 * Like deleted_lines(), but adjust marks first.
811 * Make sure the cursor is on a valid line before calling, a GUI callback may
812 * be triggered to display the cursor.
813 */
814 void
815deleted_lines_mark(linenr_T lnum, long count)
816{
817 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
818 changed_lines(lnum, 0, lnum + count, -count);
819}
820
821/*
822 * Marks the area to be redrawn after a change.
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100823 * Consider also calling changed_line_display_buf().
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200824 */
Bram Moolenaar1764faa2021-05-16 20:18:57 +0200825 void
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200826changed_lines_buf(
827 buf_T *buf,
828 linenr_T lnum, // first line with change
829 linenr_T lnume, // line below last changed line
830 long xtra) // number of extra lines (negative when deleting)
831{
832 if (buf->b_mod_set)
833 {
834 // find the maximum area that must be redisplayed
835 if (lnum < buf->b_mod_top)
836 buf->b_mod_top = lnum;
837 if (lnum < buf->b_mod_bot)
838 {
839 // adjust old bot position for xtra lines
840 buf->b_mod_bot += xtra;
841 if (buf->b_mod_bot < lnum)
842 buf->b_mod_bot = lnum;
843 }
844 if (lnume + xtra > buf->b_mod_bot)
845 buf->b_mod_bot = lnume + xtra;
846 buf->b_mod_xlines += xtra;
847 }
848 else
849 {
850 // set the area that must be redisplayed
851 buf->b_mod_set = TRUE;
852 buf->b_mod_top = lnum;
853 buf->b_mod_bot = lnume + xtra;
854 buf->b_mod_xlines = xtra;
855 }
856}
857
858/*
859 * Changed lines for the current buffer.
860 * Must be called AFTER the change and after mark_adjust().
861 * - mark the buffer changed by calling changed()
862 * - mark the windows on this buffer to be redisplayed
863 * - invalidate cached values
864 * "lnum" is the first line that needs displaying, "lnume" the first line
865 * below the changed lines (BEFORE the change).
866 * When only inserting lines, "lnum" and "lnume" are equal.
867 * Takes care of calling changed() and updating b_mod_*.
868 * Careful: may trigger autocommands that reload the buffer.
869 */
870 void
871changed_lines(
872 linenr_T lnum, // first line with change
873 colnr_T col, // column in first line with change
874 linenr_T lnume, // line below last changed line
875 long xtra) // number of extra lines (negative when deleting)
876{
877 changed_lines_buf(curbuf, lnum, lnume, xtra);
878
879#ifdef FEAT_DIFF
880 if (xtra == 0 && curwin->w_p_diff && !diff_internal())
881 {
882 // When the number of lines doesn't change then mark_adjust() isn't
883 // called and other diff buffers still need to be marked for
884 // displaying.
885 win_T *wp;
886 linenr_T wlnum;
887
888 FOR_ALL_WINDOWS(wp)
889 if (wp->w_p_diff && wp != curwin)
890 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100891 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200892 wlnum = diff_lnum_win(lnum, wp);
893 if (wlnum > 0)
894 changed_lines_buf(wp->w_buffer, wlnum,
895 lnume - lnum + wlnum, 0L);
896 }
897 }
898#endif
899
900 changed_common(lnum, col, lnume, xtra);
901}
902
903/*
904 * Called when the changed flag must be reset for buffer "buf".
905 * When "ff" is TRUE also reset 'fileformat'.
Bram Moolenaarc024b462019-06-08 18:07:21 +0200906 * When "always_inc_changedtick" is TRUE b:changedtick is incremented also when
907 * the changed flag was off.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200908 */
909 void
Bram Moolenaarc024b462019-06-08 18:07:21 +0200910unchanged(buf_T *buf, int ff, int always_inc_changedtick)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200911{
912 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
913 {
914 buf->b_changed = 0;
915 ml_setflags(buf);
916 if (ff)
917 save_file_ff(buf);
918 check_status(buf);
919 redraw_tabline = TRUE;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200920 need_maketitle = TRUE; // set window title later
Bram Moolenaarc024b462019-06-08 18:07:21 +0200921 ++CHANGEDTICK(buf);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200922 }
Bram Moolenaarc024b462019-06-08 18:07:21 +0200923 else if (always_inc_changedtick)
924 ++CHANGEDTICK(buf);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200925#ifdef FEAT_NETBEANS_INTG
926 netbeans_unmodified(buf);
927#endif
928}
929
930/*
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100931 * Save the current values of 'fileformat' and 'fileencoding', so that we know
932 * the file must be considered changed when the value is different.
933 */
934 void
935save_file_ff(buf_T *buf)
936{
937 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar15775372022-10-29 20:01:52 +0100938 buf->b_start_eof = buf->b_p_eof;
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100939 buf->b_start_eol = buf->b_p_eol;
940 buf->b_start_bomb = buf->b_p_bomb;
941
Bram Moolenaarc667da52019-11-30 20:52:27 +0100942 // Only use free/alloc when necessary, they take time.
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100943 if (buf->b_start_fenc == NULL
944 || STRCMP(buf->b_start_fenc, buf->b_p_fenc) != 0)
945 {
946 vim_free(buf->b_start_fenc);
947 buf->b_start_fenc = vim_strsave(buf->b_p_fenc);
948 }
949}
950
951/*
952 * Return TRUE if 'fileformat' and/or 'fileencoding' has a different value
953 * from when editing started (save_file_ff() called).
954 * Also when 'endofline' was changed and 'binary' is set, or when 'bomb' was
955 * changed and 'binary' is not set.
956 * Also when 'endofline' was changed and 'fixeol' is not set.
957 * When "ignore_empty" is true don't consider a new, empty buffer to be
958 * changed.
959 */
960 int
961file_ff_differs(buf_T *buf, int ignore_empty)
962{
Bram Moolenaarc667da52019-11-30 20:52:27 +0100963 // In a buffer that was never loaded the options are not valid.
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100964 if (buf->b_flags & BF_NEVERLOADED)
965 return FALSE;
966 if (ignore_empty
967 && (buf->b_flags & BF_NEW)
968 && buf->b_ml.ml_line_count == 1
969 && *ml_get_buf(buf, (linenr_T)1, FALSE) == NUL)
970 return FALSE;
971 if (buf->b_start_ffc != *buf->b_p_ff)
972 return TRUE;
Bram Moolenaar15775372022-10-29 20:01:52 +0100973 if ((buf->b_p_bin || !buf->b_p_fixeol)
974 && (buf->b_start_eof != buf->b_p_eof
975 || buf->b_start_eol != buf->b_p_eol))
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100976 return TRUE;
977 if (!buf->b_p_bin && buf->b_start_bomb != buf->b_p_bomb)
978 return TRUE;
979 if (buf->b_start_fenc == NULL)
980 return (*buf->b_p_fenc != NUL);
981 return (STRCMP(buf->b_start_fenc, buf->b_p_fenc) != 0);
982}
983
984/*
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200985 * Insert string "p" at the cursor position. Stops at a NUL byte.
986 * Handles Replace mode and multi-byte characters.
987 */
988 void
989ins_bytes(char_u *p)
990{
991 ins_bytes_len(p, (int)STRLEN(p));
992}
993
994/*
995 * Insert string "p" with length "len" at the cursor position.
996 * Handles Replace mode and multi-byte characters.
997 */
998 void
999ins_bytes_len(char_u *p, int len)
1000{
1001 int i;
1002 int n;
1003
1004 if (has_mbyte)
1005 for (i = 0; i < len; i += n)
1006 {
1007 if (enc_utf8)
1008 // avoid reading past p[len]
1009 n = utfc_ptr2len_len(p + i, len - i);
1010 else
1011 n = (*mb_ptr2len)(p + i);
1012 ins_char_bytes(p + i, n);
1013 }
1014 else
1015 for (i = 0; i < len; ++i)
1016 ins_char(p[i]);
1017}
1018
1019/*
1020 * Insert or replace a single character at the cursor position.
Bram Moolenaar24959102022-05-07 20:01:16 +01001021 * When in MODE_REPLACE or MODE_VREPLACE state, replace any existing character.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001022 * Caller must have prepared for undo.
1023 * For multi-byte characters we get the whole character, the caller must
1024 * convert bytes to a character.
1025 */
1026 void
1027ins_char(int c)
1028{
1029 char_u buf[MB_MAXBYTES + 1];
1030 int n = (*mb_char2bytes)(c, buf);
1031
1032 // When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
1033 // Happens for CTRL-Vu9900.
1034 if (buf[0] == 0)
1035 buf[0] = '\n';
1036
1037 ins_char_bytes(buf, n);
1038}
1039
1040 void
1041ins_char_bytes(char_u *buf, int charlen)
1042{
1043 int c = buf[0];
1044 int newlen; // nr of bytes inserted
1045 int oldlen; // nr of bytes deleted (0 when not replacing)
1046 char_u *p;
1047 char_u *newp;
1048 char_u *oldp;
1049 int linelen; // length of old line including NUL
1050 colnr_T col;
1051 linenr_T lnum = curwin->w_cursor.lnum;
1052 int i;
1053
1054 // Break tabs if needed.
1055 if (virtual_active() && curwin->w_cursor.coladd > 0)
1056 coladvance_force(getviscol());
1057
1058 col = curwin->w_cursor.col;
1059 oldp = ml_get(lnum);
John Marriottbfcc8952024-03-11 22:04:45 +01001060 linelen = (int)ml_get_len(lnum) + 1;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001061
1062 // The lengths default to the values for when not replacing.
1063 oldlen = 0;
1064 newlen = charlen;
1065
1066 if (State & REPLACE_FLAG)
1067 {
1068 if (State & VREPLACE_FLAG)
1069 {
1070 colnr_T new_vcol = 0; // init for GCC
1071 colnr_T vcol;
1072 int old_list;
1073
1074 // Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
1075 // Returns the old value of list, so when finished,
1076 // curwin->w_p_list should be set back to this.
1077 old_list = curwin->w_p_list;
1078 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
1079 curwin->w_p_list = FALSE;
1080
1081 // In virtual replace mode each character may replace one or more
1082 // characters (zero if it's a TAB). Count the number of bytes to
1083 // be deleted to make room for the new character, counting screen
1084 // cells. May result in adding spaces to fill a gap.
1085 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
1086 new_vcol = vcol + chartabsize(buf, vcol);
1087 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
1088 {
1089 vcol += chartabsize(oldp + col + oldlen, vcol);
1090 // Don't need to remove a TAB that takes us to the right
1091 // position.
1092 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
1093 break;
1094 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
1095 // Deleted a bit too much, insert spaces.
1096 if (vcol > new_vcol)
1097 newlen += vcol - new_vcol;
1098 }
1099 curwin->w_p_list = old_list;
1100 }
1101 else if (oldp[col] != NUL)
1102 {
1103 // normal replace
1104 oldlen = (*mb_ptr2len)(oldp + col);
1105 }
1106
1107
1108 // Push the replaced bytes onto the replace stack, so that they can be
1109 // put back when BS is used. The bytes of a multi-byte character are
1110 // done the other way around, so that the first byte is popped off
1111 // first (it tells the byte length of the character).
1112 replace_push(NUL);
1113 for (i = 0; i < oldlen; ++i)
1114 {
1115 if (has_mbyte)
1116 i += replace_push_mb(oldp + col + i) - 1;
1117 else
1118 replace_push(oldp[col + i]);
1119 }
1120 }
1121
Bram Moolenaar964b3742019-05-24 18:54:09 +02001122 newp = alloc(linelen + newlen - oldlen);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001123 if (newp == NULL)
1124 return;
1125
1126 // Copy bytes before the cursor.
1127 if (col > 0)
1128 mch_memmove(newp, oldp, (size_t)col);
1129
1130 // Copy bytes after the changed character(s).
1131 p = newp + col;
1132 if (linelen > col + oldlen)
1133 mch_memmove(p + newlen, oldp + col + oldlen,
1134 (size_t)(linelen - col - oldlen));
1135
1136 // Insert or overwrite the new character.
1137 mch_memmove(p, buf, charlen);
1138 i = charlen;
1139
1140 // Fill with spaces when necessary.
1141 while (i < newlen)
1142 p[i++] = ' ';
1143
1144 // Replace the line in the buffer.
1145 ml_replace(lnum, newp, FALSE);
1146
1147 // mark the buffer as changed and prepare for displaying
LemonBoy0d534d92022-05-21 11:20:42 +01001148 changed_bytes(lnum, col);
1149#ifdef FEAT_PROP_POPUP
1150 if (curbuf->b_has_textprop && newlen != oldlen)
1151 adjust_prop_columns(lnum, col, newlen - oldlen,
1152 State & REPLACE_FLAG ? APC_SUBSTITUTE : 0);
1153#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001154
1155 // If we're in Insert or Replace mode and 'showmatch' is set, then briefly
1156 // show the match for right parens and braces.
Bram Moolenaar24959102022-05-07 20:01:16 +01001157 if (p_sm && (State & MODE_INSERT)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001158 && msg_silent == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001159 && !ins_compl_active())
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001160 {
1161 if (has_mbyte)
1162 showmatch(mb_ptr2char(buf));
1163 else
1164 showmatch(c);
1165 }
1166
1167#ifdef FEAT_RIGHTLEFT
1168 if (!p_ri || (State & REPLACE_FLAG))
1169#endif
1170 {
1171 // Normal insert: move cursor right
1172 curwin->w_cursor.col += charlen;
1173 }
1174
1175 // TODO: should try to update w_row here, to avoid recomputing it later.
1176}
1177
1178/*
1179 * Insert a string at the cursor position.
1180 * Note: Does NOT handle Replace mode.
1181 * Caller must have prepared for undo.
1182 */
1183 void
John Marriottf4b36412025-02-23 09:09:59 +01001184ins_str(char_u *s, size_t slen)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001185{
1186 char_u *oldp, *newp;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001187 int oldlen;
1188 colnr_T col;
1189 linenr_T lnum = curwin->w_cursor.lnum;
1190
1191 if (virtual_active() && curwin->w_cursor.coladd > 0)
1192 coladvance_force(getviscol());
1193
1194 col = curwin->w_cursor.col;
1195 oldp = ml_get(lnum);
John Marriottbfcc8952024-03-11 22:04:45 +01001196 oldlen = (int)ml_get_len(lnum);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001197
John Marriottf4b36412025-02-23 09:09:59 +01001198 newp = alloc(oldlen + slen + 1);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001199 if (newp == NULL)
1200 return;
1201 if (col > 0)
1202 mch_memmove(newp, oldp, (size_t)col);
John Marriottf4b36412025-02-23 09:09:59 +01001203 mch_memmove(newp + col, s, slen);
1204 mch_memmove(newp + col + slen, oldp + col, (size_t)(oldlen - col + 1));
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001205 ml_replace(lnum, newp, FALSE);
John Marriottf4b36412025-02-23 09:09:59 +01001206 inserted_bytes(lnum, col, slen);
1207 curwin->w_cursor.col += slen;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001208}
1209
1210/*
1211 * Delete one character under the cursor.
1212 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
1213 * Caller must have prepared for undo.
1214 *
1215 * return FAIL for failure, OK otherwise
1216 */
1217 int
1218del_char(int fixpos)
1219{
1220 if (has_mbyte)
1221 {
1222 // Make sure the cursor is at the start of a character.
1223 mb_adjust_cursor();
1224 if (*ml_get_cursor() == NUL)
1225 return FAIL;
1226 return del_chars(1L, fixpos);
1227 }
1228 return del_bytes(1L, fixpos, TRUE);
1229}
1230
1231/*
1232 * Like del_bytes(), but delete characters instead of bytes.
1233 */
1234 int
1235del_chars(long count, int fixpos)
1236{
1237 long bytes = 0;
1238 long i;
1239 char_u *p;
1240 int l;
1241
1242 p = ml_get_cursor();
1243 for (i = 0; i < count && *p != NUL; ++i)
1244 {
1245 l = (*mb_ptr2len)(p);
1246 bytes += l;
1247 p += l;
1248 }
1249 return del_bytes(bytes, fixpos, TRUE);
1250}
1251
1252/*
1253 * Delete "count" bytes under the cursor.
1254 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
1255 * Caller must have prepared for undo.
1256 *
1257 * Return FAIL for failure, OK otherwise.
1258 */
1259 int
1260del_bytes(
1261 long count,
1262 int fixpos_arg,
1263 int use_delcombine UNUSED) // 'delcombine' option applies
1264{
1265 char_u *oldp, *newp;
1266 colnr_T oldlen;
1267 colnr_T newlen;
1268 linenr_T lnum = curwin->w_cursor.lnum;
1269 colnr_T col = curwin->w_cursor.col;
1270 int alloc_newp;
1271 long movelen;
1272 int fixpos = fixpos_arg;
1273
1274 oldp = ml_get(lnum);
John Marriottbfcc8952024-03-11 22:04:45 +01001275 oldlen = (int)ml_get_len(lnum);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001276
1277 // Can't do anything when the cursor is on the NUL after the line.
1278 if (col >= oldlen)
1279 return FAIL;
1280
1281 // If "count" is zero there is nothing to do.
1282 if (count == 0)
1283 return OK;
1284
1285 // If "count" is negative the caller must be doing something wrong.
1286 if (count < 1)
1287 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001288 siemsg(e_invalid_count_for_del_bytes_nr, count);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001289 return FAIL;
1290 }
1291
1292 // If 'delcombine' is set and deleting (less than) one character, only
1293 // delete the last combining character.
1294 if (p_deco && use_delcombine && enc_utf8
1295 && utfc_ptr2len(oldp + col) >= count)
1296 {
1297 int cc[MAX_MCO];
1298 int n;
1299
1300 (void)utfc_ptr2char(oldp + col, cc);
1301 if (cc[0] != NUL)
1302 {
1303 // Find the last composing char, there can be several.
1304 n = col;
1305 do
1306 {
1307 col = n;
1308 count = utf_ptr2len(oldp + n);
1309 n += count;
1310 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
1311 fixpos = 0;
1312 }
1313 }
1314
1315 // When count is too big, reduce it.
1316 movelen = (long)oldlen - (long)col - count + 1; // includes trailing NUL
1317 if (movelen <= 1)
1318 {
1319 // If we just took off the last character of a non-blank line, and
1320 // fixpos is TRUE, we don't want to end up positioned at the NUL,
1321 // unless "restart_edit" is set or 'virtualedit' contains "onemore".
1322 if (col > 0 && fixpos && restart_edit == 0
Bram Moolenaar113d9de2022-08-08 15:49:18 +01001323 && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001324 {
1325 --curwin->w_cursor.col;
1326 curwin->w_cursor.coladd = 0;
1327 if (has_mbyte)
1328 curwin->w_cursor.col -=
1329 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
1330 }
1331 count = oldlen - col;
1332 movelen = 1;
1333 }
1334 newlen = oldlen - count;
1335
1336 // If the old line has been allocated the deletion can be done in the
1337 // existing line. Otherwise a new line has to be allocated
1338 // Can't do this when using Netbeans, because we would need to invoke
1339 // netbeans_removed(), which deallocates the line. Let ml_replace() take
1340 // care of notifying Netbeans.
1341#ifdef FEAT_NETBEANS_INTG
1342 if (netbeans_active())
1343 alloc_newp = TRUE;
1344 else
1345#endif
1346 alloc_newp = !ml_line_alloced(); // check if oldp was allocated
1347 if (!alloc_newp)
1348 newp = oldp; // use same allocated memory
1349 else
1350 { // need to allocate a new line
Bram Moolenaar964b3742019-05-24 18:54:09 +02001351 newp = alloc(newlen + 1);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001352 if (newp == NULL)
1353 return FAIL;
1354 mch_memmove(newp, oldp, (size_t)col);
1355 }
1356 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
1357 if (alloc_newp)
1358 ml_replace(lnum, newp, FALSE);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001359 else
1360 {
Christian Brabandt03acd472024-07-08 21:12:55 +02001361#ifdef FEAT_PROP_POPUP
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001362 // Also move any following text properties.
1363 if (oldlen + 1 < curbuf->b_ml.ml_line_len)
1364 mch_memmove(newp + newlen + 1, oldp + oldlen + 1,
1365 (size_t)curbuf->b_ml.ml_line_len - oldlen - 1);
Christian Brabandt03acd472024-07-08 21:12:55 +02001366#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001367 curbuf->b_ml.ml_line_len -= count;
zeertzjq82e079d2024-03-10 08:55:42 +01001368 curbuf->b_ml.ml_line_textlen = 0;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001369 }
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001370
1371 // mark the buffer as changed and prepare for displaying
Bram Moolenaar12f20032020-02-26 22:06:00 +01001372 inserted_bytes(lnum, col, -count);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001373
1374 return OK;
1375}
1376
1377/*
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001378 * open_line: Add a new line below or above the current line.
1379 *
Bram Moolenaar24959102022-05-07 20:01:16 +01001380 * For MODE_VREPLACE state, we only add a new line when we get to the end of
1381 * the file, otherwise we just start replacing the next line.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001382 *
Bram Moolenaar24959102022-05-07 20:01:16 +01001383 * Caller must take care of undo. Since MODE_VREPLACE may affect any number of
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001384 * lines however, it may call u_save_cursor() again when starting to change a
1385 * new line.
1386 * "flags": OPENLINE_DELSPACES delete spaces after cursor
1387 * OPENLINE_DO_COM format comments
1388 * OPENLINE_KEEPTRAIL keep trailing spaces
1389 * OPENLINE_MARKFIX adjust mark positions after the line break
1390 * OPENLINE_COM_LIST format comments with list or 2nd line indent
glepnir5090a1f2025-02-24 19:10:37 +01001391 * OPENLINE_FORCE_INDENT set indent from second_line_indent, ignore
1392 * 'autoindent'
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001393 *
1394 * "second_line_indent": indent for after ^^D in Insert mode or if flag
1395 * OPENLINE_COM_LIST
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001396 * "did_do_comment" is set to TRUE when intentionally putting the comment
dundargocc57b5bc2022-11-02 13:30:51 +00001397 * leader in front of the new line.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001398 *
1399 * Return OK for success, FAIL for failure
1400 */
1401 int
1402open_line(
1403 int dir, // FORWARD or BACKWARD
1404 int flags,
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001405 int second_line_indent,
1406 int *did_do_comment UNUSED)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001407{
1408 char_u *saved_line; // copy of the original line
1409 char_u *next_line = NULL; // copy of the next line
1410 char_u *p_extra = NULL; // what goes to next line
1411 int less_cols = 0; // less columns for mark in new line
LemonBoy788c06a2022-05-14 18:48:05 +01001412 int less_cols_off = 0; // columns to skip for mark and
1413 // textprop adjustment
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001414 pos_T old_cursor; // old cursor position
1415 int newcol = 0; // new cursor column
1416 int newindent = 0; // auto-indent of the new line
1417 int n;
1418 int trunc_line = FALSE; // truncate current line afterwards
1419 int retval = FAIL; // return value
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001420 int extra_len = 0; // length of p_extra string
1421 int lead_len; // length of comment leader
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001422 int comment_start = 0; // start index of the comment leader
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001423 char_u *lead_flags; // position in 'comments' for comment leader
1424 char_u *leader = NULL; // copy of comment leader
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001425 char_u *allocated = NULL; // allocated memory
1426 char_u *p;
1427 int saved_char = NUL; // init for GCC
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001428 pos_T *pos;
Bram Moolenaard2439e02021-12-12 19:10:44 +00001429 int do_cindent;
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001430 int do_si = may_do_si();
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001431 int no_si = FALSE; // reset did_si afterwards
1432 int first_char = NUL; // init for GCC
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001433 int vreplace_mode;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001434 int did_append; // appended a new line
1435 int saved_pi = curbuf->b_p_pi; // copy of preserveindent setting
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01001436#ifdef FEAT_PROP_POPUP
1437 int at_eol; // cursor after last character
1438#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001439
1440 // make a copy of the current line so we can mess with it
John Marriottbfcc8952024-03-11 22:04:45 +01001441 saved_line = vim_strnsave(ml_get_curline(), ml_get_curline_len());
Bram Moolenaarc667da52019-11-30 20:52:27 +01001442 if (saved_line == NULL) // out of memory!
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001443 return FALSE;
1444
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01001445#ifdef FEAT_PROP_POPUP
John Marriottbfcc8952024-03-11 22:04:45 +01001446 at_eol = curwin->w_cursor.col >= (int)ml_get_curline_len();
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01001447#endif
1448
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001449 if (State & VREPLACE_FLAG)
1450 {
Bram Moolenaar24959102022-05-07 20:01:16 +01001451 // With MODE_VREPLACE we make a copy of the next line, which we will be
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001452 // starting to replace. First make the new line empty and let vim play
1453 // with the indenting and comment leader to its heart's content. Then
1454 // we grab what it ended up putting on the new line, put back the
1455 // original line, and call ins_char() to put each new character onto
1456 // the line, replacing what was there before and pushing the right
1457 // stuff onto the replace stack. -- webb.
1458 if (curwin->w_cursor.lnum < orig_line_count)
John Marriottbfcc8952024-03-11 22:04:45 +01001459 next_line = vim_strnsave(ml_get(curwin->w_cursor.lnum + 1), ml_get_len(curwin->w_cursor.lnum + 1));
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001460 else
1461 next_line = vim_strsave((char_u *)"");
1462 if (next_line == NULL) // out of memory!
1463 goto theend;
1464
Bram Moolenaar24959102022-05-07 20:01:16 +01001465 // In MODE_VREPLACE state, a NL replaces the rest of the line, and
1466 // starts replacing the next line, so push all of the characters left
1467 // on the line onto the replace stack. We'll push any other characters
1468 // that might be replaced at the start of the next line (due to
1469 // autoindent etc) a bit later.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001470 replace_push(NUL); // Call twice because BS over NL expects it
1471 replace_push(NUL);
1472 p = saved_line + curwin->w_cursor.col;
1473 while (*p != NUL)
1474 {
1475 if (has_mbyte)
1476 p += replace_push_mb(p);
1477 else
1478 replace_push(*p++);
1479 }
1480 saved_line[curwin->w_cursor.col] = NUL;
1481 }
1482
Bram Moolenaar24959102022-05-07 20:01:16 +01001483 if ((State & MODE_INSERT) && (State & VREPLACE_FLAG) == 0)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001484 {
1485 p_extra = saved_line + curwin->w_cursor.col;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001486 if (do_si) // need first char after new line break
1487 {
1488 p = skipwhite(p_extra);
1489 first_char = *p;
1490 }
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001491 extra_len = (int)STRLEN(p_extra);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001492 saved_char = *p_extra;
1493 *p_extra = NUL;
1494 }
1495
1496 u_clearline(); // cannot do "U" command when adding lines
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001497 did_si = FALSE;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001498 ai_col = 0;
1499
1500 // If we just did an auto-indent, then we didn't type anything on
1501 // the prior line, and it should be truncated. Do this even if 'ai' is not
1502 // set because automatically inserting a comment leader also sets did_ai.
1503 if (dir == FORWARD && did_ai)
1504 trunc_line = TRUE;
1505
glepnir5090a1f2025-02-24 19:10:37 +01001506 if ((flags & OPENLINE_FORCE_INDENT) && second_line_indent >= 0)
1507 newindent = second_line_indent;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001508 // If 'autoindent' and/or 'smartindent' is set, try to figure out what
1509 // indent to use for the new line.
glepnir5090a1f2025-02-24 19:10:37 +01001510 else if (curbuf->b_p_ai || do_si)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001511 {
1512 // count white space on current line
1513#ifdef FEAT_VARTABS
1514 newindent = get_indent_str_vtab(saved_line, curbuf->b_p_ts,
1515 curbuf->b_p_vts_array, FALSE);
1516#else
1517 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts, FALSE);
1518#endif
1519 if (newindent == 0 && !(flags & OPENLINE_COM_LIST))
1520 newindent = second_line_indent; // for ^^D command in insert mode
1521
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001522 // Do smart indenting.
1523 // In insert/replace mode (only when dir == FORWARD)
1524 // we may move some text to the next line. If it starts with '{'
1525 // don't add an indent. Fixes inserting a NL before '{' in line
1526 // "if (condition) {"
1527 if (!trunc_line && do_si && *saved_line != NUL
1528 && (p_extra == NULL || first_char != '{'))
1529 {
1530 char_u *ptr;
1531 char_u last_char;
1532
1533 old_cursor = curwin->w_cursor;
1534 ptr = saved_line;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001535 if (flags & OPENLINE_DO_COM)
1536 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
1537 else
1538 lead_len = 0;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001539 if (dir == FORWARD)
1540 {
1541 // Skip preprocessor directives, unless they are
1542 // recognised as comments.
Bram Moolenaar8c96af92019-09-28 19:05:57 +02001543 if ( lead_len == 0 && ptr[0] == '#')
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001544 {
1545 while (ptr[0] == '#' && curwin->w_cursor.lnum > 1)
1546 ptr = ml_get(--curwin->w_cursor.lnum);
1547 newindent = get_indent();
1548 }
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001549 if (flags & OPENLINE_DO_COM)
1550 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
1551 else
1552 lead_len = 0;
1553 if (lead_len > 0)
1554 {
1555 // This case gets the following right:
1556 // /*
1557 // * A comment (read '\' as '/').
1558 // */
1559 // #define IN_THE_WAY
1560 // This should line up here;
1561 p = skipwhite(ptr);
1562 if (p[0] == '/' && p[1] == '*')
1563 p++;
1564 if (p[0] == '*')
1565 {
1566 for (p++; *p; p++)
1567 {
1568 if (p[0] == '/' && p[-1] == '*')
1569 {
1570 // End of C comment, indent should line up
1571 // with the line containing the start of
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001572 // the comment.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001573 curwin->w_cursor.col = (colnr_T)(p - ptr);
1574 if ((pos = findmatch(NULL, NUL)) != NULL)
1575 {
1576 curwin->w_cursor.lnum = pos->lnum;
1577 newindent = get_indent();
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001578 break;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001579 }
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001580 // this may make "ptr" invalid, get it again
1581 ptr = ml_get(curwin->w_cursor.lnum);
1582 p = ptr + curwin->w_cursor.col;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001583 }
1584 }
1585 }
1586 }
1587 else // Not a comment line
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001588 {
1589 // Find last non-blank in line
1590 p = ptr + STRLEN(ptr) - 1;
1591 while (p > ptr && VIM_ISWHITE(*p))
1592 --p;
1593 last_char = *p;
1594
1595 // find the character just before the '{' or ';'
1596 if (last_char == '{' || last_char == ';')
1597 {
1598 if (p > ptr)
1599 --p;
1600 while (p > ptr && VIM_ISWHITE(*p))
1601 --p;
1602 }
1603 // Try to catch lines that are split over multiple
1604 // lines. eg:
1605 // if (condition &&
1606 // condition) {
1607 // Should line up here!
1608 // }
1609 if (*p == ')')
1610 {
1611 curwin->w_cursor.col = (colnr_T)(p - ptr);
1612 if ((pos = findmatch(NULL, '(')) != NULL)
1613 {
1614 curwin->w_cursor.lnum = pos->lnum;
1615 newindent = get_indent();
1616 ptr = ml_get_curline();
1617 }
1618 }
1619 // If last character is '{' do indent, without
1620 // checking for "if" and the like.
1621 if (last_char == '{')
1622 {
1623 did_si = TRUE; // do indent
1624 no_si = TRUE; // don't delete it when '{' typed
1625 }
1626 // Look for "if" and the like, use 'cinwords'.
1627 // Don't do this if the previous line ended in ';' or
1628 // '}'.
1629 else if (last_char != ';' && last_char != '}'
1630 && cin_is_cinword(ptr))
1631 did_si = TRUE;
1632 }
1633 }
1634 else // dir == BACKWARD
1635 {
1636 // Skip preprocessor directives, unless they are
1637 // recognised as comments.
Bram Moolenaar8c96af92019-09-28 19:05:57 +02001638 if (lead_len == 0 && ptr[0] == '#')
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001639 {
1640 int was_backslashed = FALSE;
1641
1642 while ((ptr[0] == '#' || was_backslashed) &&
1643 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
1644 {
1645 if (*ptr && ptr[STRLEN(ptr) - 1] == '\\')
1646 was_backslashed = TRUE;
1647 else
1648 was_backslashed = FALSE;
1649 ptr = ml_get(++curwin->w_cursor.lnum);
1650 }
1651 if (was_backslashed)
1652 newindent = 0; // Got to end of file
1653 else
1654 newindent = get_indent();
1655 }
1656 p = skipwhite(ptr);
1657 if (*p == '}') // if line starts with '}': do indent
1658 did_si = TRUE;
1659 else // can delete indent when '{' typed
1660 can_si_back = TRUE;
1661 }
1662 curwin->w_cursor = old_cursor;
1663 }
1664 if (do_si)
1665 can_si = TRUE;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001666
1667 did_ai = TRUE;
1668 }
1669
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001670 // May do indenting after opening a new line.
1671 do_cindent = !p_paste && (curbuf->b_p_cin
Bram Moolenaar8e145b82022-05-21 20:17:31 +01001672#ifdef FEAT_EVAL
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001673 || *curbuf->b_p_inde != NUL
Bram Moolenaar8e145b82022-05-21 20:17:31 +01001674#endif
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001675 )
1676 && in_cinkeys(dir == FORWARD
1677 ? KEY_OPEN_FORW
glepnir34a7d822025-03-05 21:18:20 +01001678 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum))
1679 && !(flags & OPENLINE_FORCE_INDENT);
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001680
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001681 // Find out if the current line starts with a comment leader.
1682 // This may then be inserted in front of the new line.
1683 end_comment_pending = NUL;
1684 if (flags & OPENLINE_DO_COM)
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001685 {
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001686 lead_len = get_leader_len(saved_line, &lead_flags,
1687 dir == BACKWARD, TRUE);
Bram Moolenaar2bf875f2022-05-07 14:54:11 +01001688 if (lead_len == 0 && curbuf->b_p_cin && do_cindent && dir == FORWARD
Bram Moolenaar7e667782022-05-23 13:10:48 +01001689 && (!has_format_option(FO_NO_OPEN_COMS)
1690 || (flags & OPENLINE_FORMAT)))
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001691 {
Bram Moolenaar5ea5f372021-12-29 15:15:47 +00001692 // Check for a line comment after code.
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001693 comment_start = check_linecomment(saved_line);
1694 if (comment_start != MAXCOL)
1695 {
1696 lead_len = get_leader_len(saved_line + comment_start,
Bram Moolenaar5ea5f372021-12-29 15:15:47 +00001697 &lead_flags, FALSE, TRUE);
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001698 if (lead_len != 0)
1699 {
1700 lead_len += comment_start;
1701 if (did_do_comment != NULL)
1702 *did_do_comment = TRUE;
1703 }
1704 }
1705 }
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001706 }
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001707 else
1708 lead_len = 0;
1709 if (lead_len > 0)
1710 {
1711 char_u *lead_repl = NULL; // replaces comment leader
1712 int lead_repl_len = 0; // length of *lead_repl
1713 char_u lead_middle[COM_MAX_LEN]; // middle-comment string
1714 char_u lead_end[COM_MAX_LEN]; // end-comment string
1715 char_u *comment_end = NULL; // where lead_end has been found
1716 int extra_space = FALSE; // append extra space
1717 int current_flag;
1718 int require_blank = FALSE; // requires blank after middle
1719 char_u *p2;
1720
1721 // If the comment leader has the start, middle or end flag, it may not
1722 // be used or may be replaced with the middle leader.
1723 for (p = lead_flags; *p && *p != ':'; ++p)
1724 {
1725 if (*p == COM_BLANK)
1726 {
1727 require_blank = TRUE;
1728 continue;
1729 }
1730 if (*p == COM_START || *p == COM_MIDDLE)
1731 {
1732 current_flag = *p;
1733 if (*p == COM_START)
1734 {
1735 // Doing "O" on a start of comment does not insert leader.
1736 if (dir == BACKWARD)
1737 {
1738 lead_len = 0;
1739 break;
1740 }
1741
1742 // find start of middle part
1743 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
1744 require_blank = FALSE;
1745 }
1746
1747 // Isolate the strings of the middle and end leader.
Bram Moolenaarc667da52019-11-30 20:52:27 +01001748 while (*p && p[-1] != ':') // find end of middle flags
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001749 {
1750 if (*p == COM_BLANK)
1751 require_blank = TRUE;
1752 ++p;
1753 }
1754 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
1755
1756 while (*p && p[-1] != ':') // find end of end flags
1757 {
1758 // Check whether we allow automatic ending of comments
1759 if (*p == COM_AUTO_END)
1760 end_comment_pending = -1; // means we want to set it
1761 ++p;
1762 }
1763 n = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
1764
1765 if (end_comment_pending == -1) // we can set it now
1766 end_comment_pending = lead_end[n - 1];
1767
1768 // If the end of the comment is in the same line, don't use
1769 // the comment leader.
1770 if (dir == FORWARD)
1771 {
1772 for (p = saved_line + lead_len; *p; ++p)
1773 if (STRNCMP(p, lead_end, n) == 0)
1774 {
1775 comment_end = p;
1776 lead_len = 0;
1777 break;
1778 }
1779 }
1780
1781 // Doing "o" on a start of comment inserts the middle leader.
1782 if (lead_len > 0)
1783 {
1784 if (current_flag == COM_START)
1785 {
1786 lead_repl = lead_middle;
1787 lead_repl_len = (int)STRLEN(lead_middle);
1788 }
1789
1790 // If we have hit RETURN immediately after the start
1791 // comment leader, then put a space after the middle
1792 // comment leader on the next line.
1793 if (!VIM_ISWHITE(saved_line[lead_len - 1])
1794 && ((p_extra != NULL
1795 && (int)curwin->w_cursor.col == lead_len)
1796 || (p_extra == NULL
1797 && saved_line[lead_len] == NUL)
1798 || require_blank))
1799 extra_space = TRUE;
1800 }
1801 break;
1802 }
1803 if (*p == COM_END)
1804 {
1805 // Doing "o" on the end of a comment does not insert leader.
1806 // Remember where the end is, might want to use it to find the
1807 // start (for C-comments).
1808 if (dir == FORWARD)
1809 {
1810 comment_end = skipwhite(saved_line);
1811 lead_len = 0;
1812 break;
1813 }
1814
1815 // Doing "O" on the end of a comment inserts the middle leader.
1816 // Find the string for the middle leader, searching backwards.
1817 while (p > curbuf->b_p_com && *p != ',')
1818 --p;
1819 for (lead_repl = p; lead_repl > curbuf->b_p_com
1820 && lead_repl[-1] != ':'; --lead_repl)
1821 ;
1822 lead_repl_len = (int)(p - lead_repl);
1823
1824 // We can probably always add an extra space when doing "O" on
1825 // the comment-end
1826 extra_space = TRUE;
1827
1828 // Check whether we allow automatic ending of comments
1829 for (p2 = p; *p2 && *p2 != ':'; p2++)
1830 {
1831 if (*p2 == COM_AUTO_END)
1832 end_comment_pending = -1; // means we want to set it
1833 }
1834 if (end_comment_pending == -1)
1835 {
1836 // Find last character in end-comment string
1837 while (*p2 && *p2 != ',')
1838 p2++;
1839 end_comment_pending = p2[-1];
1840 }
1841 break;
1842 }
1843 if (*p == COM_FIRST)
1844 {
1845 // Comment leader for first line only: Don't repeat leader
1846 // when using "O", blank out leader when using "o".
1847 if (dir == BACKWARD)
1848 lead_len = 0;
1849 else
1850 {
1851 lead_repl = (char_u *)"";
1852 lead_repl_len = 0;
1853 }
1854 break;
1855 }
1856 }
1857 if (lead_len)
1858 {
1859 // allocate buffer (may concatenate p_extra later)
1860 leader = alloc(lead_len + lead_repl_len + extra_space + extra_len
1861 + (second_line_indent > 0 ? second_line_indent : 0) + 1);
1862 allocated = leader; // remember to free it later
1863
1864 if (leader == NULL)
1865 lead_len = 0;
1866 else
1867 {
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001868 int li;
1869
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001870 vim_strncpy(leader, saved_line, lead_len);
1871
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001872 // TODO: handle multi-byte and double width chars
1873 for (li = 0; li < comment_start; ++li)
1874 if (!VIM_ISWHITE(leader[li]))
1875 leader[li] = ' ';
1876
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001877 // Replace leader with lead_repl, right or left adjusted
1878 if (lead_repl != NULL)
1879 {
1880 int c = 0;
1881 int off = 0;
1882
1883 for (p = lead_flags; *p != NUL && *p != ':'; )
1884 {
1885 if (*p == COM_RIGHT || *p == COM_LEFT)
1886 c = *p++;
1887 else if (VIM_ISDIGIT(*p) || *p == '-')
1888 off = getdigits(&p);
1889 else
1890 ++p;
1891 }
1892 if (c == COM_RIGHT) // right adjusted leader
1893 {
1894 // find last non-white in the leader to line up with
1895 for (p = leader + lead_len - 1; p > leader
1896 && VIM_ISWHITE(*p); --p)
1897 ;
1898 ++p;
1899
1900 // Compute the length of the replaced characters in
1901 // screen characters, not bytes.
1902 {
1903 int repl_size = vim_strnsize(lead_repl,
1904 lead_repl_len);
1905 int old_size = 0;
1906 char_u *endp = p;
1907 int l;
1908
1909 while (old_size < repl_size && p > leader)
1910 {
1911 MB_PTR_BACK(leader, p);
1912 old_size += ptr2cells(p);
1913 }
1914 l = lead_repl_len - (int)(endp - p);
1915 if (l != 0)
1916 mch_memmove(endp + l, endp,
1917 (size_t)((leader + lead_len) - endp));
1918 lead_len += l;
1919 }
1920 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1921 if (p + lead_repl_len > leader + lead_len)
1922 p[lead_repl_len] = NUL;
1923
1924 // blank-out any other chars from the old leader.
1925 while (--p >= leader)
1926 {
1927 int l = mb_head_off(leader, p);
1928
1929 if (l > 1)
1930 {
1931 p -= l;
1932 if (ptr2cells(p) > 1)
1933 {
1934 p[1] = ' ';
1935 --l;
1936 }
1937 mch_memmove(p + 1, p + l + 1,
1938 (size_t)((leader + lead_len) - (p + l + 1)));
1939 lead_len -= l;
1940 *p = ' ';
1941 }
1942 else if (!VIM_ISWHITE(*p))
1943 *p = ' ';
1944 }
1945 }
1946 else // left adjusted leader
1947 {
1948 p = skipwhite(leader);
1949
1950 // Compute the length of the replaced characters in
1951 // screen characters, not bytes. Move the part that is
1952 // not to be overwritten.
1953 {
1954 int repl_size = vim_strnsize(lead_repl,
1955 lead_repl_len);
1956 int i;
1957 int l;
1958
1959 for (i = 0; i < lead_len && p[i] != NUL; i += l)
1960 {
1961 l = (*mb_ptr2len)(p + i);
1962 if (vim_strnsize(p, i + l) > repl_size)
1963 break;
1964 }
1965 if (i != lead_repl_len)
1966 {
1967 mch_memmove(p + lead_repl_len, p + i,
1968 (size_t)(lead_len - i - (p - leader)));
1969 lead_len += lead_repl_len - i;
1970 }
1971 }
1972 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1973
1974 // Replace any remaining non-white chars in the old
1975 // leader by spaces. Keep Tabs, the indent must
1976 // remain the same.
1977 for (p += lead_repl_len; p < leader + lead_len; ++p)
1978 if (!VIM_ISWHITE(*p))
1979 {
1980 // Don't put a space before a TAB.
1981 if (p + 1 < leader + lead_len && p[1] == TAB)
1982 {
1983 --lead_len;
1984 mch_memmove(p, p + 1,
1985 (leader + lead_len) - p);
1986 }
1987 else
1988 {
1989 int l = (*mb_ptr2len)(p);
1990
1991 if (l > 1)
1992 {
1993 if (ptr2cells(p) > 1)
1994 {
1995 // Replace a double-wide char with
1996 // two spaces
1997 --l;
1998 *p++ = ' ';
1999 }
2000 mch_memmove(p + 1, p + l,
2001 (leader + lead_len) - p);
2002 lead_len -= l - 1;
2003 }
2004 *p = ' ';
2005 }
2006 }
2007 *p = NUL;
2008 }
2009
2010 // Recompute the indent, it may have changed.
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002011 if (curbuf->b_p_ai || do_si)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002012#ifdef FEAT_VARTABS
2013 newindent = get_indent_str_vtab(leader, curbuf->b_p_ts,
2014 curbuf->b_p_vts_array, FALSE);
2015#else
2016 newindent = get_indent_str(leader,
2017 (int)curbuf->b_p_ts, FALSE);
2018#endif
2019
2020 // Add the indent offset
2021 if (newindent + off < 0)
2022 {
2023 off = -newindent;
2024 newindent = 0;
2025 }
2026 else
2027 newindent += off;
2028
2029 // Correct trailing spaces for the shift, so that
2030 // alignment remains equal.
2031 while (off > 0 && lead_len > 0
2032 && leader[lead_len - 1] == ' ')
2033 {
2034 // Don't do it when there is a tab before the space
2035 if (vim_strchr(skipwhite(leader), '\t') != NULL)
2036 break;
2037 --lead_len;
2038 --off;
2039 }
2040
2041 // If the leader ends in white space, don't add an
2042 // extra space
2043 if (lead_len > 0 && VIM_ISWHITE(leader[lead_len - 1]))
2044 extra_space = FALSE;
2045 leader[lead_len] = NUL;
2046 }
2047
2048 if (extra_space)
2049 {
2050 leader[lead_len++] = ' ';
2051 leader[lead_len] = NUL;
2052 }
2053
2054 newcol = lead_len;
2055
2056 // if a new indent will be set below, remove the indent that
2057 // is in the comment leader
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002058 if (newindent || did_si)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002059 {
2060 while (lead_len && VIM_ISWHITE(*leader))
2061 {
2062 --lead_len;
2063 --newcol;
2064 ++leader;
2065 }
2066 }
2067
2068 }
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002069 did_si = can_si = FALSE;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002070 }
2071 else if (comment_end != NULL)
2072 {
2073 // We have finished a comment, so we don't use the leader.
2074 // If this was a C-comment and 'ai' or 'si' is set do a normal
2075 // indent to align with the line containing the start of the
2076 // comment.
2077 if (comment_end[0] == '*' && comment_end[1] == '/' &&
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002078 (curbuf->b_p_ai || do_si))
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002079 {
2080 old_cursor = curwin->w_cursor;
2081 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line);
2082 if ((pos = findmatch(NULL, NUL)) != NULL)
2083 {
2084 curwin->w_cursor.lnum = pos->lnum;
2085 newindent = get_indent();
2086 }
2087 curwin->w_cursor = old_cursor;
2088 }
2089 }
2090 }
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002091
Bram Moolenaar24959102022-05-07 20:01:16 +01002092 // (State == MODE_INSERT || State == MODE_REPLACE), only when dir == FORWARD
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002093 if (p_extra != NULL)
2094 {
2095 *p_extra = saved_char; // restore char that NUL replaced
2096
2097 // When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first
2098 // non-blank.
2099 //
Bram Moolenaar24959102022-05-07 20:01:16 +01002100 // When in MODE_REPLACE state, put the deleted blanks on the replace
2101 // stack, preceded by a NUL, so they can be put back when a BS is
2102 // entered.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002103 if (REPLACE_NORMAL(State))
Bram Moolenaarc667da52019-11-30 20:52:27 +01002104 replace_push(NUL); // end of extra blanks
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002105 if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES))
2106 {
2107 while ((*p_extra == ' ' || *p_extra == '\t')
2108 && (!enc_utf8
2109 || !utf_iscomposing(utf_ptr2char(p_extra + 1))))
2110 {
2111 if (REPLACE_NORMAL(State))
2112 replace_push(*p_extra);
2113 ++p_extra;
2114 ++less_cols_off;
2115 }
2116 }
2117
2118 // columns for marks adjusted for removed columns
2119 less_cols = (int)(p_extra - saved_line);
2120 }
2121
2122 if (p_extra == NULL)
2123 p_extra = (char_u *)""; // append empty line
2124
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002125 // concatenate leader and p_extra, if there is a leader
2126 if (lead_len)
2127 {
2128 if (flags & OPENLINE_COM_LIST && second_line_indent > 0)
2129 {
2130 int i;
2131 int padding = second_line_indent
2132 - (newindent + (int)STRLEN(leader));
2133
2134 // Here whitespace is inserted after the comment char.
2135 // Below, set_indent(newindent, SIN_INSERT) will insert the
2136 // whitespace needed before the comment char.
2137 for (i = 0; i < padding; i++)
2138 {
2139 STRCAT(leader, " ");
2140 less_cols--;
2141 newcol++;
2142 }
2143 }
2144 STRCAT(leader, p_extra);
2145 p_extra = leader;
2146 did_ai = TRUE; // So truncating blanks works with comments
2147 less_cols -= lead_len;
2148 }
2149 else
2150 end_comment_pending = NUL; // turns out there was no leader
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002151
2152 old_cursor = curwin->w_cursor;
2153 if (dir == BACKWARD)
2154 --curwin->w_cursor.lnum;
2155 if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count)
2156 {
2157 if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE)
2158 == FAIL)
2159 goto theend;
2160 // Postpone calling changed_lines(), because it would mess up folding
2161 // with markers.
Brandon Simmonsda3dd7d2023-01-17 19:48:07 +00002162 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002163 did_append = TRUE;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002164#ifdef FEAT_PROP_POPUP
Bram Moolenaar24959102022-05-07 20:01:16 +01002165 if ((State & MODE_INSERT) && (State & VREPLACE_FLAG) == 0)
LemonBoy788c06a2022-05-14 18:48:05 +01002166 // Properties after the split move to the next line.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002167 adjust_props_for_split(curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002168 curwin->w_cursor.col + 1, 0, at_eol);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002169#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002170 }
2171 else
2172 {
Bram Moolenaar24959102022-05-07 20:01:16 +01002173 // In MODE_VREPLACE state we are starting to replace the next line.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002174 curwin->w_cursor.lnum++;
2175 if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed)
2176 {
2177 // In case we NL to a new line, BS to the previous one, and NL
2178 // again, we don't want to save the new line for undo twice.
Bram Moolenaarc667da52019-11-30 20:52:27 +01002179 (void)u_save_cursor(); // errors are ignored!
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002180 vr_lines_changed++;
2181 }
2182 ml_replace(curwin->w_cursor.lnum, p_extra, TRUE);
2183 changed_bytes(curwin->w_cursor.lnum, 0);
2184 curwin->w_cursor.lnum--;
2185 did_append = FALSE;
2186 }
2187
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002188 if (newindent || did_si)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002189 {
2190 ++curwin->w_cursor.lnum;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002191 if (did_si)
2192 {
2193 int sw = (int)get_sw_value(curbuf);
2194
2195 if (p_sr)
2196 newindent -= newindent % sw;
2197 newindent += sw;
2198 }
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002199 // Copy the indent
2200 if (curbuf->b_p_ci)
2201 {
2202 (void)copy_indent(newindent, saved_line);
2203
2204 // Set the 'preserveindent' option so that any further screwing
2205 // with the line doesn't entirely destroy our efforts to preserve
2206 // it. It gets restored at the function end.
2207 curbuf->b_p_pi = TRUE;
2208 }
2209 else
2210 (void)set_indent(newindent, SIN_INSERT);
2211 less_cols -= curwin->w_cursor.col;
2212
2213 ai_col = curwin->w_cursor.col;
2214
Bram Moolenaar24959102022-05-07 20:01:16 +01002215 // In MODE_REPLACE state, for each character in the new indent, there
2216 // must be a NUL on the replace stack, for when it is deleted with BS
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002217 if (REPLACE_NORMAL(State))
2218 for (n = 0; n < (int)curwin->w_cursor.col; ++n)
2219 replace_push(NUL);
2220 newcol += curwin->w_cursor.col;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002221 if (no_si)
2222 did_si = FALSE;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002223 }
2224
Bram Moolenaar24959102022-05-07 20:01:16 +01002225 // In MODE_REPLACE state, for each character in the extra leader, there
2226 // must be a NUL on the replace stack, for when it is deleted with BS.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002227 if (REPLACE_NORMAL(State))
2228 while (lead_len-- > 0)
2229 replace_push(NUL);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002230
2231 curwin->w_cursor = old_cursor;
2232
2233 if (dir == FORWARD)
2234 {
Bram Moolenaar24959102022-05-07 20:01:16 +01002235 if (trunc_line || (State & MODE_INSERT))
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002236 {
2237 // truncate current line at cursor
2238 saved_line[curwin->w_cursor.col] = NUL;
2239 // Remove trailing white space, unless OPENLINE_KEEPTRAIL used.
2240 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL))
John Marriott34954972025-03-16 20:49:52 +01002241 truncate_spaces(saved_line, curwin->w_cursor.col);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002242 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE);
2243 saved_line = NULL;
2244 if (did_append)
2245 {
2246 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
2247 curwin->w_cursor.lnum + 1, 1L);
2248 did_append = FALSE;
2249
2250 // Move marks after the line break to the new line.
2251 if (flags & OPENLINE_MARKFIX)
2252 mark_col_adjust(curwin->w_cursor.lnum,
2253 curwin->w_cursor.col + less_cols_off,
2254 1L, (long)-less_cols, 0);
LemonBoy788c06a2022-05-14 18:48:05 +01002255#ifdef FEAT_PROP_POPUP
2256 // Keep into account the deleted blanks on the new line.
2257 if (curbuf->b_has_textprop && less_cols_off != 0)
2258 adjust_prop_columns(curwin->w_cursor.lnum + 1, 0,
2259 -less_cols_off, 0);
2260#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002261 }
2262 else
2263 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
2264 }
2265
2266 // Put the cursor on the new line. Careful: the scrollup() above may
2267 // have moved w_cursor, we must use old_cursor.
2268 curwin->w_cursor.lnum = old_cursor.lnum + 1;
2269 }
2270 if (did_append)
2271 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L);
2272
2273 curwin->w_cursor.col = newcol;
2274 curwin->w_cursor.coladd = 0;
2275
Bram Moolenaar24959102022-05-07 20:01:16 +01002276 // In MODE_VREPLACE state, we are handling the replace stack ourselves, so
2277 // stop fixthisline() from doing it (via change_indent()) by telling it
2278 // we're in normal MODE_INSERT state.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002279 if (State & VREPLACE_FLAG)
2280 {
2281 vreplace_mode = State; // So we know to put things right later
Bram Moolenaar24959102022-05-07 20:01:16 +01002282 State = MODE_INSERT;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002283 }
2284 else
2285 vreplace_mode = 0;
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002286
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002287 if (!p_paste)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002288 {
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002289 if (leader == NULL
2290 && !use_indentexpr_for_lisp()
2291 && curbuf->b_p_lisp
2292 && curbuf->b_p_ai)
2293 {
2294 // do lisp indenting
2295 fixthisline(get_lisp_indent);
2296 ai_col = (colnr_T)getwhitecols_curline();
2297 }
2298 else if (do_cindent || (curbuf->b_p_ai && use_indentexpr_for_lisp()))
2299 {
2300 // do 'cindent' or 'indentexpr' indenting
2301 do_c_expr_indent();
2302 ai_col = (colnr_T)getwhitecols_curline();
2303 }
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002304 }
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002305
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002306 if (vreplace_mode != 0)
2307 State = vreplace_mode;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002308
Bram Moolenaar24959102022-05-07 20:01:16 +01002309 // Finally, MODE_VREPLACE gets the stuff on the new line, then puts back
2310 // the original line, and inserts the new stuff char by char, pushing old
2311 // stuff onto the replace stack (via ins_char()).
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002312 if (State & VREPLACE_FLAG)
2313 {
2314 // Put new line in p_extra
John Marriottbfcc8952024-03-11 22:04:45 +01002315 p_extra = vim_strnsave(ml_get_curline(), ml_get_curline_len());
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002316 if (p_extra == NULL)
2317 goto theend;
2318
2319 // Put back original line
2320 ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
2321
2322 // Insert new stuff into line again
2323 curwin->w_cursor.col = 0;
2324 curwin->w_cursor.coladd = 0;
2325 ins_bytes(p_extra); // will call changed_bytes()
2326 vim_free(p_extra);
2327 next_line = NULL;
2328 }
2329
2330 retval = OK; // success!
2331theend:
2332 curbuf->b_p_pi = saved_pi;
2333 vim_free(saved_line);
2334 vim_free(next_line);
2335 vim_free(allocated);
2336 return retval;
2337}
2338
2339/*
2340 * Delete from cursor to end of line.
2341 * Caller must have prepared for undo.
2342 * If "fixpos" is TRUE fix the cursor position when done.
2343 *
2344 * Return FAIL for failure, OK otherwise.
2345 */
2346 int
2347truncate_line(int fixpos)
2348{
2349 char_u *newp;
2350 linenr_T lnum = curwin->w_cursor.lnum;
2351 colnr_T col = curwin->w_cursor.col;
LemonBoyd0b1a092022-05-12 18:45:18 +01002352 char_u *old_line;
2353 int deleted;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002354
LemonBoyd0b1a092022-05-12 18:45:18 +01002355 old_line = ml_get(lnum);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002356 if (col == 0)
2357 newp = vim_strsave((char_u *)"");
2358 else
LemonBoyd0b1a092022-05-12 18:45:18 +01002359 newp = vim_strnsave(old_line, col);
John Marriottbfcc8952024-03-11 22:04:45 +01002360 deleted = (int)ml_get_len(lnum) - col;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002361
2362 if (newp == NULL)
2363 return FAIL;
2364
2365 ml_replace(lnum, newp, FALSE);
2366
2367 // mark the buffer as changed and prepare for displaying
LemonBoyd0b1a092022-05-12 18:45:18 +01002368 inserted_bytes(lnum, curwin->w_cursor.col, -deleted);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002369
2370 // If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2371 if (fixpos && curwin->w_cursor.col > 0)
2372 --curwin->w_cursor.col;
2373
2374 return OK;
2375}
2376
2377/*
2378 * Delete "nlines" lines at the cursor.
2379 * Saves the lines for undo first if "undo" is TRUE.
2380 */
2381 void
2382del_lines(long nlines, int undo)
2383{
2384 long n;
2385 linenr_T first = curwin->w_cursor.lnum;
2386
2387 if (nlines <= 0)
2388 return;
2389
2390 // save the deleted lines for undo
2391 if (undo && u_savedel(first, nlines) == FAIL)
2392 return;
2393
2394 for (n = 0; n < nlines; )
2395 {
2396 if (curbuf->b_ml.ml_flags & ML_EMPTY) // nothing to delete
2397 break;
2398
Bram Moolenaarca70c072020-05-30 20:30:46 +02002399 ml_delete_flags(first, ML_DEL_MESSAGE);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002400 ++n;
2401
2402 // If we delete the last line in the file, stop
2403 if (first > curbuf->b_ml.ml_line_count)
2404 break;
2405 }
2406
2407 // Correct the cursor position before calling deleted_lines_mark(), it may
2408 // trigger a callback to display the cursor.
2409 curwin->w_cursor.col = 0;
2410 check_cursor_lnum();
2411
2412 // adjust marks, mark the buffer as changed and prepare for displaying
2413 deleted_lines_mark(first, n);
2414}