blob: f036a3767ce501d5c04085cf364efc85325ef110 [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.
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{
162 if (buf->b_recorded_changes != NULL && xtra != 0)
163 {
164 listitem_T *li;
Bram Moolenaar7b31a182019-05-24 21:39:27 +0200165 linenr_T prev_lnum;
166 linenr_T prev_lnume;
Bram Moolenaardda41442019-05-16 22:11:47 +0200167
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200168 FOR_ALL_LIST_ITEMS(buf->b_recorded_changes, li)
Bram Moolenaardda41442019-05-16 22:11:47 +0200169 {
Bram Moolenaar7b31a182019-05-24 21:39:27 +0200170 prev_lnum = (linenr_T)dict_get_number(
Bram Moolenaard61efa52022-07-23 09:52:04 +0100171 li->li_tv.vval.v_dict, "lnum");
Bram Moolenaar7b31a182019-05-24 21:39:27 +0200172 prev_lnume = (linenr_T)dict_get_number(
Bram Moolenaard61efa52022-07-23 09:52:04 +0100173 li->li_tv.vval.v_dict, "end");
Bram Moolenaar4a0a1612019-07-17 22:00:19 +0200174 if (prev_lnum >= lnum || prev_lnum > lnume || prev_lnume >= lnum)
Bram Moolenaardda41442019-05-16 22:11:47 +0200175 {
Bram Moolenaar4a0a1612019-07-17 22:00:19 +0200176 // the current change is going to make the line number in
177 // the older change invalid, flush now
178 invoke_listeners(curbuf);
179 break;
Bram Moolenaardda41442019-05-16 22:11:47 +0200180 }
181 }
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 Moolenaar6d2399b2019-05-11 19:14:16 +0200262
263 lnr->lr_id = ++next_listener_id;
264 rettv->vval.v_number = lnr->lr_id;
265}
266
267/*
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200268 * listener_flush() function
269 */
270 void
271f_listener_flush(typval_T *argvars, typval_T *rettv UNUSED)
272{
273 buf_T *buf = curbuf;
274
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200275 if (in_vim9script() && check_for_opt_buffer_arg(argvars, 0) == FAIL)
276 return;
277
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200278 if (argvars[0].v_type != VAR_UNKNOWN)
279 {
280 buf = get_buf_arg(&argvars[0]);
281 if (buf == NULL)
282 return;
283 }
284 invoke_listeners(buf);
285}
286
Bram Moolenaar4b4b1b82021-09-11 15:06:44 +0200287
288 static void
289remove_listener(buf_T *buf, listener_T *lnr, listener_T *prev)
290{
291 if (prev != NULL)
292 prev->lr_next = lnr->lr_next;
293 else
294 buf->b_listener = lnr->lr_next;
295 free_callback(&lnr->lr_callback);
296 vim_free(lnr);
297}
298
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200299/*
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200300 * listener_remove() function
301 */
302 void
Bram Moolenaar7b73f912019-07-13 13:03:02 +0200303f_listener_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200304{
305 listener_T *lnr;
306 listener_T *next;
Bram Moolenaar7b73f912019-07-13 13:03:02 +0200307 listener_T *prev;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200308 int id;
Bram Moolenaara3347722019-05-11 21:14:24 +0200309 buf_T *buf;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200310
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200311 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
312 return;
313
314 id = tv_get_number(argvars);
Bram Moolenaar86173482019-10-01 17:02:16 +0200315 FOR_ALL_BUFFERS(buf)
Bram Moolenaar7b73f912019-07-13 13:03:02 +0200316 {
317 prev = NULL;
Bram Moolenaara3347722019-05-11 21:14:24 +0200318 for (lnr = buf->b_listener; lnr != NULL; lnr = next)
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200319 {
Bram Moolenaara3347722019-05-11 21:14:24 +0200320 next = lnr->lr_next;
321 if (lnr->lr_id == id)
322 {
zeertzjqcfe45652022-05-27 17:26:55 +0100323 if (textlock > 0)
Bram Moolenaar4b4b1b82021-09-11 15:06:44 +0200324 {
325 // in invoke_listeners(), clear ID and delete later
326 lnr->lr_id = 0;
327 return;
328 }
329 remove_listener(buf, lnr, prev);
Bram Moolenaar7b73f912019-07-13 13:03:02 +0200330 rettv->vval.v_number = 1;
331 return;
Bram Moolenaara3347722019-05-11 21:14:24 +0200332 }
333 prev = lnr;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200334 }
Bram Moolenaar7b73f912019-07-13 13:03:02 +0200335 }
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200336}
337
338/*
Bram Moolenaardda41442019-05-16 22:11:47 +0200339 * Called before inserting a line above "lnum"/"lnum3" or deleting line "lnum"
340 * to "lnume".
341 */
342 void
343may_invoke_listeners(buf_T *buf, linenr_T lnum, linenr_T lnume, int added)
344{
Bram Moolenaar4a0a1612019-07-17 22:00:19 +0200345 check_recorded_changes(buf, lnum, lnume, added);
Bram Moolenaardda41442019-05-16 22:11:47 +0200346}
347
348/*
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200349 * Called when a sequence of changes is done: invoke listeners added with
350 * listener_add().
351 */
352 void
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200353invoke_listeners(buf_T *buf)
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200354{
355 listener_T *lnr;
356 typval_T rettv;
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200357 typval_T argv[6];
358 listitem_T *li;
359 linenr_T start = MAXLNUM;
360 linenr_T end = 0;
361 linenr_T added = 0;
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200362 int save_updating_screen = updating_screen;
363 static int recursive = FALSE;
Bram Moolenaar4b4b1b82021-09-11 15:06:44 +0200364 listener_T *next;
Yegappan Lakshmanan956be462022-09-02 17:12:07 +0100365 listener_T *prev;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200366
Bram Moolenaardda41442019-05-16 22:11:47 +0200367 if (buf->b_recorded_changes == NULL // nothing changed
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200368 || buf->b_listener == NULL // no listeners
369 || recursive) // already busy
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200370 return;
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200371 recursive = TRUE;
372
373 // Block messages on channels from being handled, so that they don't make
374 // text changes here.
375 ++updating_screen;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200376
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200377 argv[0].v_type = VAR_NUMBER;
378 argv[0].vval.v_number = buf->b_fnum; // a:bufnr
379
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200380 FOR_ALL_LIST_ITEMS(buf->b_recorded_changes, li)
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200381 {
382 varnumber_T lnum;
383
Bram Moolenaard61efa52022-07-23 09:52:04 +0100384 lnum = dict_get_number(li->li_tv.vval.v_dict, "lnum");
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200385 if (start > lnum)
386 start = lnum;
Bram Moolenaard61efa52022-07-23 09:52:04 +0100387 lnum = dict_get_number(li->li_tv.vval.v_dict, "end");
Bram Moolenaar336bf2b2019-10-24 20:07:07 +0200388 if (end < lnum)
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200389 end = lnum;
Bram Moolenaard61efa52022-07-23 09:52:04 +0100390 added += dict_get_number(li->li_tv.vval.v_dict, "added");
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200391 }
392 argv[1].v_type = VAR_NUMBER;
393 argv[1].vval.v_number = start;
394 argv[2].v_type = VAR_NUMBER;
395 argv[2].vval.v_number = end;
396 argv[3].v_type = VAR_NUMBER;
397 argv[3].vval.v_number = added;
398
399 argv[4].v_type = VAR_LIST;
Bram Moolenaardda41442019-05-16 22:11:47 +0200400 argv[4].vval.v_list = buf->b_recorded_changes;
zeertzjqcfe45652022-05-27 17:26:55 +0100401 ++textlock;
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200402
403 for (lnr = buf->b_listener; lnr != NULL; lnr = lnr->lr_next)
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200404 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200405 call_callback(&lnr->lr_callback, -1, &rettv, 5, argv);
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200406 clear_tv(&rettv);
407 }
408
Bram Moolenaar4b4b1b82021-09-11 15:06:44 +0200409 // If f_listener_remove() was called may have to remove a listener now.
Yegappan Lakshmanan956be462022-09-02 17:12:07 +0100410 prev = NULL;
Bram Moolenaar4b4b1b82021-09-11 15:06:44 +0200411 for (lnr = buf->b_listener; lnr != NULL; lnr = next)
412 {
Bram Moolenaar4b4b1b82021-09-11 15:06:44 +0200413 next = lnr->lr_next;
414 if (lnr->lr_id == 0)
415 remove_listener(buf, lnr, prev);
416 else
417 prev = lnr;
418 }
419
zeertzjqcfe45652022-05-27 17:26:55 +0100420 --textlock;
Bram Moolenaardda41442019-05-16 22:11:47 +0200421 list_unref(buf->b_recorded_changes);
422 buf->b_recorded_changes = NULL;
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200423
424 if (save_updating_screen)
425 updating_screen = TRUE;
426 else
427 after_updating_screen(TRUE);
428 recursive = FALSE;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200429}
Bram Moolenaar86173482019-10-01 17:02:16 +0200430
431/*
432 * Remove all listeners associated with "buf".
433 */
434 void
435remove_listeners(buf_T *buf)
436{
437 listener_T *lnr;
438 listener_T *next;
439
440 for (lnr = buf->b_listener; lnr != NULL; lnr = next)
441 {
442 next = lnr->lr_next;
443 free_callback(&lnr->lr_callback);
444 vim_free(lnr);
445 }
446 buf->b_listener = NULL;
447}
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200448#endif
449
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200450/*
451 * Common code for when a change was made.
452 * See changed_lines() for the arguments.
453 * Careful: may trigger autocommands that reload the buffer.
454 */
455 static void
456changed_common(
457 linenr_T lnum,
458 colnr_T col,
459 linenr_T lnume,
460 long xtra)
461{
462 win_T *wp;
463 tabpage_T *tp;
464 int i;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200465 int cols;
466 pos_T *p;
467 int add;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200468
469 // mark the buffer as modified
470 changed();
471
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200472#ifdef FEAT_EVAL
473 may_record_change(lnum, col, lnume, xtra);
474#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200475#ifdef FEAT_DIFF
476 if (curwin->w_p_diff && diff_internal())
477 curtab->tp_diff_update = TRUE;
478#endif
479
480 // set the '. mark
Bram Moolenaare1004402020-10-24 20:49:43 +0200481 if ((cmdmod.cmod_flags & CMOD_KEEPJUMPS) == 0)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200482 {
483 curbuf->b_last_change.lnum = lnum;
484 curbuf->b_last_change.col = col;
485
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200486 // Create a new entry if a new undo-able change was started or we
487 // don't have an entry yet.
488 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
489 {
490 if (curbuf->b_changelistlen == 0)
491 add = TRUE;
492 else
493 {
494 // Don't create a new entry when the line number is the same
495 // as the last one and the column is not too far away. Avoids
496 // creating many entries for typing "xxxxx".
497 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
498 if (p->lnum != lnum)
499 add = TRUE;
500 else
501 {
502 cols = comp_textwidth(FALSE);
503 if (cols == 0)
504 cols = 79;
505 add = (p->col + cols < col || col + cols < p->col);
506 }
507 }
508 if (add)
509 {
510 // This is the first of a new sequence of undo-able changes
511 // and it's at some distance of the last change. Use a new
512 // position in the changelist.
513 curbuf->b_new_change = FALSE;
514
515 if (curbuf->b_changelistlen == JUMPLISTSIZE)
516 {
517 // changelist is full: remove oldest entry
518 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
519 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
520 sizeof(pos_T) * (JUMPLISTSIZE - 1));
521 FOR_ALL_TAB_WINDOWS(tp, wp)
522 {
523 // Correct position in changelist for other windows on
524 // this buffer.
525 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
526 --wp->w_changelistidx;
527 }
528 }
529 FOR_ALL_TAB_WINDOWS(tp, wp)
530 {
531 // For other windows, if the position in the changelist is
532 // at the end it stays at the end.
533 if (wp->w_buffer == curbuf
534 && wp->w_changelistidx == curbuf->b_changelistlen)
535 ++wp->w_changelistidx;
536 }
537 ++curbuf->b_changelistlen;
538 }
539 }
540 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
541 curbuf->b_last_change;
542 // The current window is always after the last change, so that "g,"
543 // takes you back to it.
544 curwin->w_changelistidx = curbuf->b_changelistlen;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200545 }
546
Bram Moolenaar7ce5b2b2022-05-16 19:40:59 +0100547 if (VIsual_active)
548 check_visual_pos();
549
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200550 FOR_ALL_TAB_WINDOWS(tp, wp)
551 {
552 if (wp->w_buffer == curbuf)
553 {
Bram Moolenaar8329ab72022-02-16 21:51:00 +0000554#ifdef FEAT_FOLDING
Bram Moolenaar94377372022-02-16 20:30:52 +0000555 linenr_T last = lnume + xtra - 1; // last line after the change
Bram Moolenaar8329ab72022-02-16 21:51:00 +0000556#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200557 // Mark this window to be redrawn later.
Bram Moolenaar471c0fa2022-08-22 15:19:16 +0100558 if (!redraw_not_allowed && wp->w_redr_type < UPD_VALID)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100559 wp->w_redr_type = UPD_VALID;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200560
561 // Check if a change in the buffer has invalidated the cached
562 // values for the cursor.
563#ifdef FEAT_FOLDING
564 // Update the folds for this window. Can't postpone this, because
565 // a following operator might work on the whole fold: ">>dd".
Bram Moolenaar94377372022-02-16 20:30:52 +0000566 foldUpdate(wp, lnum, last);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200567
568 // The change may cause lines above or below the change to become
569 // included in a fold. Set lnum/lnume to the first/last line that
570 // might be displayed differently.
571 // Set w_cline_folded here as an efficient way to update it when
572 // inserting lines just above a closed fold.
573 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
574 if (wp->w_cursor.lnum == lnum)
575 wp->w_cline_folded = i;
Bram Moolenaar94377372022-02-16 20:30:52 +0000576 i = hasFoldingWin(wp, last, NULL, &last, FALSE, NULL);
577 if (wp->w_cursor.lnum == last)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200578 wp->w_cline_folded = i;
579
580 // If the changed line is in a range of previously folded lines,
581 // compare with the first line in that range.
582 if (wp->w_cursor.lnum <= lnum)
583 {
584 i = find_wl_entry(wp, lnum);
585 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
586 changed_line_abv_curs_win(wp);
587 }
588#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200589 if (wp->w_cursor.lnum > lnum)
590 changed_line_abv_curs_win(wp);
591 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
592 changed_cline_bef_curs_win(wp);
593 if (wp->w_botline >= lnum)
594 {
Bram Moolenaar5bea41d2021-07-13 22:21:44 +0200595 if (xtra < 0)
596 invalidate_botline_win(wp);
597 else
598 // Assume that botline doesn't change (inserted lines make
599 // other lines scroll down below botline).
600 approximate_botline_win(wp);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200601 }
602
603 // Check if any w_lines[] entries have become invalid.
604 // For entries below the change: Correct the lnums for
605 // inserted/deleted lines. Makes it possible to stop displaying
606 // after the change.
607 for (i = 0; i < wp->w_lines_valid; ++i)
608 if (wp->w_lines[i].wl_valid)
609 {
610 if (wp->w_lines[i].wl_lnum >= lnum)
611 {
612 if (wp->w_lines[i].wl_lnum < lnume)
613 {
614 // line included in change
615 wp->w_lines[i].wl_valid = FALSE;
616 }
617 else if (xtra != 0)
618 {
619 // line below change
620 wp->w_lines[i].wl_lnum += xtra;
621#ifdef FEAT_FOLDING
622 wp->w_lines[i].wl_lastlnum += xtra;
623#endif
624 }
625 }
626#ifdef FEAT_FOLDING
627 else if (wp->w_lines[i].wl_lastlnum >= lnum)
628 {
629 // change somewhere inside this range of folded lines,
630 // may need to be redrawn
631 wp->w_lines[i].wl_valid = FALSE;
632 }
633#endif
634 }
635
636#ifdef FEAT_FOLDING
637 // Take care of side effects for setting w_topline when folds have
638 // changed. Esp. when the buffer was changed in another window.
639 if (hasAnyFolding(wp))
640 set_topline(wp, wp->w_topline);
641#endif
zeertzjq8c979602022-04-07 15:08:01 +0100642 // If lines have been added or removed, relative numbering always
643 // requires a redraw.
Lewis Russell16246392022-03-29 11:38:17 +0100644 if (wp->w_p_rnu && xtra != 0)
zeertzjq8c979602022-04-07 15:08:01 +0100645 {
646 wp->w_last_cursor_lnum_rnu = 0;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100647 redraw_win_later(wp, UPD_VALID);
zeertzjq8c979602022-04-07 15:08:01 +0100648 }
Bram Moolenaar11a58af2019-10-24 22:32:31 +0200649#ifdef FEAT_SYN_HL
650 // Cursor line highlighting probably need to be updated with
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100651 // "UPD_VALID" if it's below the change.
Bram Moolenaar11a58af2019-10-24 22:32:31 +0200652 // If the cursor line is inside the change we need to redraw more.
653 if (wp->w_p_cul)
654 {
655 if (xtra == 0)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100656 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar11a58af2019-10-24 22:32:31 +0200657 else if (lnum <= wp->w_last_cursorline)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100658 redraw_win_later(wp, UPD_SOME_VALID);
Bram Moolenaar11a58af2019-10-24 22:32:31 +0200659 }
660#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200661 }
Bram Moolenaar368137a2022-05-31 13:43:12 +0100662#ifdef FEAT_SEARCH_EXTRA
663 if (wp == curwin && xtra != 0 && search_hl_has_cursor_lnum >= lnum)
664 search_hl_has_cursor_lnum += xtra;
665#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200666 }
667
668 // Call update_screen() later, which checks out what needs to be redrawn,
669 // since it notices b_mod_set and then uses b_mod_*.
Bram Moolenaar471c0fa2022-08-22 15:19:16 +0100670 set_must_redraw(UPD_VALID);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200671
672 // when the cursor line is changed always trigger CursorMoved
673 if (lnum <= curwin->w_cursor.lnum
674 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
675 last_cursormoved.lnum = 0;
676}
677
678 static void
679changedOneline(buf_T *buf, linenr_T lnum)
680{
681 if (buf->b_mod_set)
682 {
683 // find the maximum area that must be redisplayed
684 if (lnum < buf->b_mod_top)
685 buf->b_mod_top = lnum;
686 else if (lnum >= buf->b_mod_bot)
687 buf->b_mod_bot = lnum + 1;
688 }
689 else
690 {
691 // set the area that must be redisplayed to one line
692 buf->b_mod_set = TRUE;
693 buf->b_mod_top = lnum;
694 buf->b_mod_bot = lnum + 1;
695 buf->b_mod_xlines = 0;
696 }
697}
698
699/*
700 * Changed bytes within a single line for the current buffer.
701 * - marks the windows on this buffer to be redisplayed
702 * - marks the buffer changed by calling changed()
703 * - invalidates cached values
704 * Careful: may trigger autocommands that reload the buffer.
705 */
706 void
707changed_bytes(linenr_T lnum, colnr_T col)
708{
709 changedOneline(curbuf, lnum);
710 changed_common(lnum, col, lnum + 1, 0L);
711
Bram Moolenaar26f09ea2022-09-27 16:29:38 +0100712#ifdef FEAT_SPELL
713 // When text has been changed at the end of the line, possibly the start of
714 // the next line may have SpellCap that should be removed or it needs to be
715 // displayed. Schedule the next line for redrawing just in case.
Bram Moolenaarf3ef0262022-10-05 13:29:15 +0100716 // Don't do this when displaying '$' at the end of changed text.
717 if (spell_check_window(curwin)
718 && lnum < curbuf->b_ml.ml_line_count
719 && vim_strchr(p_cpo, CPO_DOLLAR) == NULL)
Bram Moolenaar26f09ea2022-09-27 16:29:38 +0100720 redrawWinline(curwin, lnum + 1);
721#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200722#ifdef FEAT_DIFF
723 // Diff highlighting in other diff windows may need to be updated too.
724 if (curwin->w_p_diff)
725 {
726 win_T *wp;
727 linenr_T wlnum;
728
729 FOR_ALL_WINDOWS(wp)
730 if (wp->w_p_diff && wp != curwin)
731 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100732 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200733 wlnum = diff_lnum_win(lnum, wp);
734 if (wlnum > 0)
735 changedOneline(wp->w_buffer, wlnum);
736 }
737 }
738#endif
739}
740
741/*
742 * Like changed_bytes() but also adjust text properties for "added" bytes.
743 * When "added" is negative text was deleted.
744 */
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200745 void
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200746inserted_bytes(linenr_T lnum, colnr_T col, int added UNUSED)
747{
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100748#ifdef FEAT_PROP_POPUP
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200749 if (curbuf->b_has_textprop && added != 0)
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200750 adjust_prop_columns(lnum, col, added, 0);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200751#endif
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200752
753 changed_bytes(lnum, col);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200754}
755
756/*
757 * Appended "count" lines below line "lnum" in the current buffer.
758 * Must be called AFTER the change and after mark_adjust().
759 * Takes care of marking the buffer to be redrawn and sets the changed flag.
760 */
761 void
762appended_lines(linenr_T lnum, long count)
763{
764 changed_lines(lnum + 1, 0, lnum + 1, count);
765}
766
767/*
768 * Like appended_lines(), but adjust marks first.
769 */
770 void
771appended_lines_mark(linenr_T lnum, long count)
772{
773 // Skip mark_adjust when adding a line after the last one, there can't
774 // be marks there. But it's still needed in diff mode.
775 if (lnum + count < curbuf->b_ml.ml_line_count
776#ifdef FEAT_DIFF
777 || curwin->w_p_diff
778#endif
779 )
780 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
781 changed_lines(lnum + 1, 0, lnum + 1, count);
782}
783
784/*
785 * Deleted "count" lines at line "lnum" in the current buffer.
786 * Must be called AFTER the change and after mark_adjust().
787 * Takes care of marking the buffer to be redrawn and sets the changed flag.
788 */
789 void
790deleted_lines(linenr_T lnum, long count)
791{
792 changed_lines(lnum, 0, lnum + count, -count);
793}
794
795/*
796 * Like deleted_lines(), but adjust marks first.
797 * Make sure the cursor is on a valid line before calling, a GUI callback may
798 * be triggered to display the cursor.
799 */
800 void
801deleted_lines_mark(linenr_T lnum, long count)
802{
803 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
804 changed_lines(lnum, 0, lnum + count, -count);
805}
806
807/*
808 * Marks the area to be redrawn after a change.
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100809 * Consider also calling changed_line_display_buf().
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200810 */
Bram Moolenaar1764faa2021-05-16 20:18:57 +0200811 void
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200812changed_lines_buf(
813 buf_T *buf,
814 linenr_T lnum, // first line with change
815 linenr_T lnume, // line below last changed line
816 long xtra) // number of extra lines (negative when deleting)
817{
818 if (buf->b_mod_set)
819 {
820 // find the maximum area that must be redisplayed
821 if (lnum < buf->b_mod_top)
822 buf->b_mod_top = lnum;
823 if (lnum < buf->b_mod_bot)
824 {
825 // adjust old bot position for xtra lines
826 buf->b_mod_bot += xtra;
827 if (buf->b_mod_bot < lnum)
828 buf->b_mod_bot = lnum;
829 }
830 if (lnume + xtra > buf->b_mod_bot)
831 buf->b_mod_bot = lnume + xtra;
832 buf->b_mod_xlines += xtra;
833 }
834 else
835 {
836 // set the area that must be redisplayed
837 buf->b_mod_set = TRUE;
838 buf->b_mod_top = lnum;
839 buf->b_mod_bot = lnume + xtra;
840 buf->b_mod_xlines = xtra;
841 }
842}
843
844/*
845 * Changed lines for the current buffer.
846 * Must be called AFTER the change and after mark_adjust().
847 * - mark the buffer changed by calling changed()
848 * - mark the windows on this buffer to be redisplayed
849 * - invalidate cached values
850 * "lnum" is the first line that needs displaying, "lnume" the first line
851 * below the changed lines (BEFORE the change).
852 * When only inserting lines, "lnum" and "lnume" are equal.
853 * Takes care of calling changed() and updating b_mod_*.
854 * Careful: may trigger autocommands that reload the buffer.
855 */
856 void
857changed_lines(
858 linenr_T lnum, // first line with change
859 colnr_T col, // column in first line with change
860 linenr_T lnume, // line below last changed line
861 long xtra) // number of extra lines (negative when deleting)
862{
863 changed_lines_buf(curbuf, lnum, lnume, xtra);
864
865#ifdef FEAT_DIFF
866 if (xtra == 0 && curwin->w_p_diff && !diff_internal())
867 {
868 // When the number of lines doesn't change then mark_adjust() isn't
869 // called and other diff buffers still need to be marked for
870 // displaying.
871 win_T *wp;
872 linenr_T wlnum;
873
874 FOR_ALL_WINDOWS(wp)
875 if (wp->w_p_diff && wp != curwin)
876 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100877 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200878 wlnum = diff_lnum_win(lnum, wp);
879 if (wlnum > 0)
880 changed_lines_buf(wp->w_buffer, wlnum,
881 lnume - lnum + wlnum, 0L);
882 }
883 }
884#endif
885
886 changed_common(lnum, col, lnume, xtra);
887}
888
889/*
890 * Called when the changed flag must be reset for buffer "buf".
891 * When "ff" is TRUE also reset 'fileformat'.
Bram Moolenaarc024b462019-06-08 18:07:21 +0200892 * When "always_inc_changedtick" is TRUE b:changedtick is incremented also when
893 * the changed flag was off.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200894 */
895 void
Bram Moolenaarc024b462019-06-08 18:07:21 +0200896unchanged(buf_T *buf, int ff, int always_inc_changedtick)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200897{
898 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
899 {
900 buf->b_changed = 0;
901 ml_setflags(buf);
902 if (ff)
903 save_file_ff(buf);
904 check_status(buf);
905 redraw_tabline = TRUE;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200906 need_maketitle = TRUE; // set window title later
Bram Moolenaarc024b462019-06-08 18:07:21 +0200907 ++CHANGEDTICK(buf);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200908 }
Bram Moolenaarc024b462019-06-08 18:07:21 +0200909 else if (always_inc_changedtick)
910 ++CHANGEDTICK(buf);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200911#ifdef FEAT_NETBEANS_INTG
912 netbeans_unmodified(buf);
913#endif
914}
915
916/*
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100917 * Save the current values of 'fileformat' and 'fileencoding', so that we know
918 * the file must be considered changed when the value is different.
919 */
920 void
921save_file_ff(buf_T *buf)
922{
923 buf->b_start_ffc = *buf->b_p_ff;
924 buf->b_start_eol = buf->b_p_eol;
925 buf->b_start_bomb = buf->b_p_bomb;
926
Bram Moolenaarc667da52019-11-30 20:52:27 +0100927 // Only use free/alloc when necessary, they take time.
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100928 if (buf->b_start_fenc == NULL
929 || STRCMP(buf->b_start_fenc, buf->b_p_fenc) != 0)
930 {
931 vim_free(buf->b_start_fenc);
932 buf->b_start_fenc = vim_strsave(buf->b_p_fenc);
933 }
934}
935
936/*
937 * Return TRUE if 'fileformat' and/or 'fileencoding' has a different value
938 * from when editing started (save_file_ff() called).
939 * Also when 'endofline' was changed and 'binary' is set, or when 'bomb' was
940 * changed and 'binary' is not set.
941 * Also when 'endofline' was changed and 'fixeol' is not set.
942 * When "ignore_empty" is true don't consider a new, empty buffer to be
943 * changed.
944 */
945 int
946file_ff_differs(buf_T *buf, int ignore_empty)
947{
Bram Moolenaarc667da52019-11-30 20:52:27 +0100948 // In a buffer that was never loaded the options are not valid.
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100949 if (buf->b_flags & BF_NEVERLOADED)
950 return FALSE;
951 if (ignore_empty
952 && (buf->b_flags & BF_NEW)
953 && buf->b_ml.ml_line_count == 1
954 && *ml_get_buf(buf, (linenr_T)1, FALSE) == NUL)
955 return FALSE;
956 if (buf->b_start_ffc != *buf->b_p_ff)
957 return TRUE;
958 if ((buf->b_p_bin || !buf->b_p_fixeol) && buf->b_start_eol != buf->b_p_eol)
959 return TRUE;
960 if (!buf->b_p_bin && buf->b_start_bomb != buf->b_p_bomb)
961 return TRUE;
962 if (buf->b_start_fenc == NULL)
963 return (*buf->b_p_fenc != NUL);
964 return (STRCMP(buf->b_start_fenc, buf->b_p_fenc) != 0);
965}
966
967/*
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200968 * Insert string "p" at the cursor position. Stops at a NUL byte.
969 * Handles Replace mode and multi-byte characters.
970 */
971 void
972ins_bytes(char_u *p)
973{
974 ins_bytes_len(p, (int)STRLEN(p));
975}
976
977/*
978 * Insert string "p" with length "len" at the cursor position.
979 * Handles Replace mode and multi-byte characters.
980 */
981 void
982ins_bytes_len(char_u *p, int len)
983{
984 int i;
985 int n;
986
987 if (has_mbyte)
988 for (i = 0; i < len; i += n)
989 {
990 if (enc_utf8)
991 // avoid reading past p[len]
992 n = utfc_ptr2len_len(p + i, len - i);
993 else
994 n = (*mb_ptr2len)(p + i);
995 ins_char_bytes(p + i, n);
996 }
997 else
998 for (i = 0; i < len; ++i)
999 ins_char(p[i]);
1000}
1001
1002/*
1003 * Insert or replace a single character at the cursor position.
Bram Moolenaar24959102022-05-07 20:01:16 +01001004 * When in MODE_REPLACE or MODE_VREPLACE state, replace any existing character.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001005 * Caller must have prepared for undo.
1006 * For multi-byte characters we get the whole character, the caller must
1007 * convert bytes to a character.
1008 */
1009 void
1010ins_char(int c)
1011{
1012 char_u buf[MB_MAXBYTES + 1];
1013 int n = (*mb_char2bytes)(c, buf);
1014
1015 // When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
1016 // Happens for CTRL-Vu9900.
1017 if (buf[0] == 0)
1018 buf[0] = '\n';
1019
1020 ins_char_bytes(buf, n);
1021}
1022
1023 void
1024ins_char_bytes(char_u *buf, int charlen)
1025{
1026 int c = buf[0];
1027 int newlen; // nr of bytes inserted
1028 int oldlen; // nr of bytes deleted (0 when not replacing)
1029 char_u *p;
1030 char_u *newp;
1031 char_u *oldp;
1032 int linelen; // length of old line including NUL
1033 colnr_T col;
1034 linenr_T lnum = curwin->w_cursor.lnum;
1035 int i;
1036
1037 // Break tabs if needed.
1038 if (virtual_active() && curwin->w_cursor.coladd > 0)
1039 coladvance_force(getviscol());
1040
1041 col = curwin->w_cursor.col;
1042 oldp = ml_get(lnum);
1043 linelen = (int)STRLEN(oldp) + 1;
1044
1045 // The lengths default to the values for when not replacing.
1046 oldlen = 0;
1047 newlen = charlen;
1048
1049 if (State & REPLACE_FLAG)
1050 {
1051 if (State & VREPLACE_FLAG)
1052 {
1053 colnr_T new_vcol = 0; // init for GCC
1054 colnr_T vcol;
1055 int old_list;
1056
1057 // Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
1058 // Returns the old value of list, so when finished,
1059 // curwin->w_p_list should be set back to this.
1060 old_list = curwin->w_p_list;
1061 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
1062 curwin->w_p_list = FALSE;
1063
1064 // In virtual replace mode each character may replace one or more
1065 // characters (zero if it's a TAB). Count the number of bytes to
1066 // be deleted to make room for the new character, counting screen
1067 // cells. May result in adding spaces to fill a gap.
1068 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
1069 new_vcol = vcol + chartabsize(buf, vcol);
1070 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
1071 {
1072 vcol += chartabsize(oldp + col + oldlen, vcol);
1073 // Don't need to remove a TAB that takes us to the right
1074 // position.
1075 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
1076 break;
1077 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
1078 // Deleted a bit too much, insert spaces.
1079 if (vcol > new_vcol)
1080 newlen += vcol - new_vcol;
1081 }
1082 curwin->w_p_list = old_list;
1083 }
1084 else if (oldp[col] != NUL)
1085 {
1086 // normal replace
1087 oldlen = (*mb_ptr2len)(oldp + col);
1088 }
1089
1090
1091 // Push the replaced bytes onto the replace stack, so that they can be
1092 // put back when BS is used. The bytes of a multi-byte character are
1093 // done the other way around, so that the first byte is popped off
1094 // first (it tells the byte length of the character).
1095 replace_push(NUL);
1096 for (i = 0; i < oldlen; ++i)
1097 {
1098 if (has_mbyte)
1099 i += replace_push_mb(oldp + col + i) - 1;
1100 else
1101 replace_push(oldp[col + i]);
1102 }
1103 }
1104
Bram Moolenaar964b3742019-05-24 18:54:09 +02001105 newp = alloc(linelen + newlen - oldlen);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001106 if (newp == NULL)
1107 return;
1108
1109 // Copy bytes before the cursor.
1110 if (col > 0)
1111 mch_memmove(newp, oldp, (size_t)col);
1112
1113 // Copy bytes after the changed character(s).
1114 p = newp + col;
1115 if (linelen > col + oldlen)
1116 mch_memmove(p + newlen, oldp + col + oldlen,
1117 (size_t)(linelen - col - oldlen));
1118
1119 // Insert or overwrite the new character.
1120 mch_memmove(p, buf, charlen);
1121 i = charlen;
1122
1123 // Fill with spaces when necessary.
1124 while (i < newlen)
1125 p[i++] = ' ';
1126
1127 // Replace the line in the buffer.
1128 ml_replace(lnum, newp, FALSE);
1129
1130 // mark the buffer as changed and prepare for displaying
LemonBoy0d534d92022-05-21 11:20:42 +01001131 changed_bytes(lnum, col);
1132#ifdef FEAT_PROP_POPUP
1133 if (curbuf->b_has_textprop && newlen != oldlen)
1134 adjust_prop_columns(lnum, col, newlen - oldlen,
1135 State & REPLACE_FLAG ? APC_SUBSTITUTE : 0);
1136#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001137
1138 // If we're in Insert or Replace mode and 'showmatch' is set, then briefly
1139 // show the match for right parens and braces.
Bram Moolenaar24959102022-05-07 20:01:16 +01001140 if (p_sm && (State & MODE_INSERT)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001141 && msg_silent == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001142 && !ins_compl_active())
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001143 {
1144 if (has_mbyte)
1145 showmatch(mb_ptr2char(buf));
1146 else
1147 showmatch(c);
1148 }
1149
1150#ifdef FEAT_RIGHTLEFT
1151 if (!p_ri || (State & REPLACE_FLAG))
1152#endif
1153 {
1154 // Normal insert: move cursor right
1155 curwin->w_cursor.col += charlen;
1156 }
1157
1158 // TODO: should try to update w_row here, to avoid recomputing it later.
1159}
1160
1161/*
1162 * Insert a string at the cursor position.
1163 * Note: Does NOT handle Replace mode.
1164 * Caller must have prepared for undo.
1165 */
1166 void
1167ins_str(char_u *s)
1168{
1169 char_u *oldp, *newp;
1170 int newlen = (int)STRLEN(s);
1171 int oldlen;
1172 colnr_T col;
1173 linenr_T lnum = curwin->w_cursor.lnum;
1174
1175 if (virtual_active() && curwin->w_cursor.coladd > 0)
1176 coladvance_force(getviscol());
1177
1178 col = curwin->w_cursor.col;
1179 oldp = ml_get(lnum);
1180 oldlen = (int)STRLEN(oldp);
1181
Bram Moolenaar964b3742019-05-24 18:54:09 +02001182 newp = alloc(oldlen + newlen + 1);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001183 if (newp == NULL)
1184 return;
1185 if (col > 0)
1186 mch_memmove(newp, oldp, (size_t)col);
1187 mch_memmove(newp + col, s, (size_t)newlen);
1188 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
1189 ml_replace(lnum, newp, FALSE);
1190 inserted_bytes(lnum, col, newlen);
1191 curwin->w_cursor.col += newlen;
1192}
1193
1194/*
1195 * Delete one character under the cursor.
1196 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
1197 * Caller must have prepared for undo.
1198 *
1199 * return FAIL for failure, OK otherwise
1200 */
1201 int
1202del_char(int fixpos)
1203{
1204 if (has_mbyte)
1205 {
1206 // Make sure the cursor is at the start of a character.
1207 mb_adjust_cursor();
1208 if (*ml_get_cursor() == NUL)
1209 return FAIL;
1210 return del_chars(1L, fixpos);
1211 }
1212 return del_bytes(1L, fixpos, TRUE);
1213}
1214
1215/*
1216 * Like del_bytes(), but delete characters instead of bytes.
1217 */
1218 int
1219del_chars(long count, int fixpos)
1220{
1221 long bytes = 0;
1222 long i;
1223 char_u *p;
1224 int l;
1225
1226 p = ml_get_cursor();
1227 for (i = 0; i < count && *p != NUL; ++i)
1228 {
1229 l = (*mb_ptr2len)(p);
1230 bytes += l;
1231 p += l;
1232 }
1233 return del_bytes(bytes, fixpos, TRUE);
1234}
1235
1236/*
1237 * Delete "count" bytes under the cursor.
1238 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
1239 * Caller must have prepared for undo.
1240 *
1241 * Return FAIL for failure, OK otherwise.
1242 */
1243 int
1244del_bytes(
1245 long count,
1246 int fixpos_arg,
1247 int use_delcombine UNUSED) // 'delcombine' option applies
1248{
1249 char_u *oldp, *newp;
1250 colnr_T oldlen;
1251 colnr_T newlen;
1252 linenr_T lnum = curwin->w_cursor.lnum;
1253 colnr_T col = curwin->w_cursor.col;
1254 int alloc_newp;
1255 long movelen;
1256 int fixpos = fixpos_arg;
1257
1258 oldp = ml_get(lnum);
1259 oldlen = (int)STRLEN(oldp);
1260
1261 // Can't do anything when the cursor is on the NUL after the line.
1262 if (col >= oldlen)
1263 return FAIL;
1264
1265 // If "count" is zero there is nothing to do.
1266 if (count == 0)
1267 return OK;
1268
1269 // If "count" is negative the caller must be doing something wrong.
1270 if (count < 1)
1271 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001272 siemsg(e_invalid_count_for_del_bytes_nr, count);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001273 return FAIL;
1274 }
1275
1276 // If 'delcombine' is set and deleting (less than) one character, only
1277 // delete the last combining character.
1278 if (p_deco && use_delcombine && enc_utf8
1279 && utfc_ptr2len(oldp + col) >= count)
1280 {
1281 int cc[MAX_MCO];
1282 int n;
1283
1284 (void)utfc_ptr2char(oldp + col, cc);
1285 if (cc[0] != NUL)
1286 {
1287 // Find the last composing char, there can be several.
1288 n = col;
1289 do
1290 {
1291 col = n;
1292 count = utf_ptr2len(oldp + n);
1293 n += count;
1294 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
1295 fixpos = 0;
1296 }
1297 }
1298
1299 // When count is too big, reduce it.
1300 movelen = (long)oldlen - (long)col - count + 1; // includes trailing NUL
1301 if (movelen <= 1)
1302 {
1303 // If we just took off the last character of a non-blank line, and
1304 // fixpos is TRUE, we don't want to end up positioned at the NUL,
1305 // unless "restart_edit" is set or 'virtualedit' contains "onemore".
1306 if (col > 0 && fixpos && restart_edit == 0
Bram Moolenaar113d9de2022-08-08 15:49:18 +01001307 && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001308 {
1309 --curwin->w_cursor.col;
1310 curwin->w_cursor.coladd = 0;
1311 if (has_mbyte)
1312 curwin->w_cursor.col -=
1313 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
1314 }
1315 count = oldlen - col;
1316 movelen = 1;
1317 }
1318 newlen = oldlen - count;
1319
1320 // If the old line has been allocated the deletion can be done in the
1321 // existing line. Otherwise a new line has to be allocated
1322 // Can't do this when using Netbeans, because we would need to invoke
1323 // netbeans_removed(), which deallocates the line. Let ml_replace() take
1324 // care of notifying Netbeans.
1325#ifdef FEAT_NETBEANS_INTG
1326 if (netbeans_active())
1327 alloc_newp = TRUE;
1328 else
1329#endif
1330 alloc_newp = !ml_line_alloced(); // check if oldp was allocated
1331 if (!alloc_newp)
1332 newp = oldp; // use same allocated memory
1333 else
1334 { // need to allocate a new line
Bram Moolenaar964b3742019-05-24 18:54:09 +02001335 newp = alloc(newlen + 1);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001336 if (newp == NULL)
1337 return FAIL;
1338 mch_memmove(newp, oldp, (size_t)col);
1339 }
1340 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
1341 if (alloc_newp)
1342 ml_replace(lnum, newp, FALSE);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001343#ifdef FEAT_PROP_POPUP
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001344 else
1345 {
1346 // Also move any following text properties.
1347 if (oldlen + 1 < curbuf->b_ml.ml_line_len)
1348 mch_memmove(newp + newlen + 1, oldp + oldlen + 1,
1349 (size_t)curbuf->b_ml.ml_line_len - oldlen - 1);
1350 curbuf->b_ml.ml_line_len -= count;
1351 }
1352#endif
1353
1354 // mark the buffer as changed and prepare for displaying
Bram Moolenaar12f20032020-02-26 22:06:00 +01001355 inserted_bytes(lnum, col, -count);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001356
1357 return OK;
1358}
1359
1360/*
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001361 * open_line: Add a new line below or above the current line.
1362 *
Bram Moolenaar24959102022-05-07 20:01:16 +01001363 * For MODE_VREPLACE state, we only add a new line when we get to the end of
1364 * the file, otherwise we just start replacing the next line.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001365 *
Bram Moolenaar24959102022-05-07 20:01:16 +01001366 * Caller must take care of undo. Since MODE_VREPLACE may affect any number of
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001367 * lines however, it may call u_save_cursor() again when starting to change a
1368 * new line.
1369 * "flags": OPENLINE_DELSPACES delete spaces after cursor
1370 * OPENLINE_DO_COM format comments
1371 * OPENLINE_KEEPTRAIL keep trailing spaces
1372 * OPENLINE_MARKFIX adjust mark positions after the line break
1373 * OPENLINE_COM_LIST format comments with list or 2nd line indent
1374 *
1375 * "second_line_indent": indent for after ^^D in Insert mode or if flag
1376 * OPENLINE_COM_LIST
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001377 * "did_do_comment" is set to TRUE when intentionally putting the comment
1378 * leader in fromt of the new line.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001379 *
1380 * Return OK for success, FAIL for failure
1381 */
1382 int
1383open_line(
1384 int dir, // FORWARD or BACKWARD
1385 int flags,
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001386 int second_line_indent,
1387 int *did_do_comment UNUSED)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001388{
1389 char_u *saved_line; // copy of the original line
1390 char_u *next_line = NULL; // copy of the next line
1391 char_u *p_extra = NULL; // what goes to next line
1392 int less_cols = 0; // less columns for mark in new line
LemonBoy788c06a2022-05-14 18:48:05 +01001393 int less_cols_off = 0; // columns to skip for mark and
1394 // textprop adjustment
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001395 pos_T old_cursor; // old cursor position
1396 int newcol = 0; // new cursor column
1397 int newindent = 0; // auto-indent of the new line
1398 int n;
1399 int trunc_line = FALSE; // truncate current line afterwards
1400 int retval = FAIL; // return value
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001401 int extra_len = 0; // length of p_extra string
1402 int lead_len; // length of comment leader
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001403 int comment_start = 0; // start index of the comment leader
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001404 char_u *lead_flags; // position in 'comments' for comment leader
1405 char_u *leader = NULL; // copy of comment leader
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001406 char_u *allocated = NULL; // allocated memory
1407 char_u *p;
1408 int saved_char = NUL; // init for GCC
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001409 pos_T *pos;
Bram Moolenaard2439e02021-12-12 19:10:44 +00001410 int do_cindent;
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001411 int do_si = may_do_si();
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001412 int no_si = FALSE; // reset did_si afterwards
1413 int first_char = NUL; // init for GCC
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001414 int vreplace_mode;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001415 int did_append; // appended a new line
1416 int saved_pi = curbuf->b_p_pi; // copy of preserveindent setting
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01001417#ifdef FEAT_PROP_POPUP
1418 int at_eol; // cursor after last character
1419#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001420
1421 // make a copy of the current line so we can mess with it
1422 saved_line = vim_strsave(ml_get_curline());
Bram Moolenaarc667da52019-11-30 20:52:27 +01001423 if (saved_line == NULL) // out of memory!
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001424 return FALSE;
1425
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01001426#ifdef FEAT_PROP_POPUP
1427 at_eol = curwin->w_cursor.col >= (int)STRLEN(saved_line);
1428#endif
1429
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001430 if (State & VREPLACE_FLAG)
1431 {
Bram Moolenaar24959102022-05-07 20:01:16 +01001432 // With MODE_VREPLACE we make a copy of the next line, which we will be
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001433 // starting to replace. First make the new line empty and let vim play
1434 // with the indenting and comment leader to its heart's content. Then
1435 // we grab what it ended up putting on the new line, put back the
1436 // original line, and call ins_char() to put each new character onto
1437 // the line, replacing what was there before and pushing the right
1438 // stuff onto the replace stack. -- webb.
1439 if (curwin->w_cursor.lnum < orig_line_count)
1440 next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1));
1441 else
1442 next_line = vim_strsave((char_u *)"");
1443 if (next_line == NULL) // out of memory!
1444 goto theend;
1445
Bram Moolenaar24959102022-05-07 20:01:16 +01001446 // In MODE_VREPLACE state, a NL replaces the rest of the line, and
1447 // starts replacing the next line, so push all of the characters left
1448 // on the line onto the replace stack. We'll push any other characters
1449 // that might be replaced at the start of the next line (due to
1450 // autoindent etc) a bit later.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001451 replace_push(NUL); // Call twice because BS over NL expects it
1452 replace_push(NUL);
1453 p = saved_line + curwin->w_cursor.col;
1454 while (*p != NUL)
1455 {
1456 if (has_mbyte)
1457 p += replace_push_mb(p);
1458 else
1459 replace_push(*p++);
1460 }
1461 saved_line[curwin->w_cursor.col] = NUL;
1462 }
1463
Bram Moolenaar24959102022-05-07 20:01:16 +01001464 if ((State & MODE_INSERT) && (State & VREPLACE_FLAG) == 0)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001465 {
1466 p_extra = saved_line + curwin->w_cursor.col;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001467 if (do_si) // need first char after new line break
1468 {
1469 p = skipwhite(p_extra);
1470 first_char = *p;
1471 }
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001472 extra_len = (int)STRLEN(p_extra);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001473 saved_char = *p_extra;
1474 *p_extra = NUL;
1475 }
1476
1477 u_clearline(); // cannot do "U" command when adding lines
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001478 did_si = FALSE;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001479 ai_col = 0;
1480
1481 // If we just did an auto-indent, then we didn't type anything on
1482 // the prior line, and it should be truncated. Do this even if 'ai' is not
1483 // set because automatically inserting a comment leader also sets did_ai.
1484 if (dir == FORWARD && did_ai)
1485 trunc_line = TRUE;
1486
1487 // If 'autoindent' and/or 'smartindent' is set, try to figure out what
1488 // indent to use for the new line.
Bram Moolenaar8e145b82022-05-21 20:17:31 +01001489 if (curbuf->b_p_ai || do_si)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001490 {
1491 // count white space on current line
1492#ifdef FEAT_VARTABS
1493 newindent = get_indent_str_vtab(saved_line, curbuf->b_p_ts,
1494 curbuf->b_p_vts_array, FALSE);
1495#else
1496 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts, FALSE);
1497#endif
1498 if (newindent == 0 && !(flags & OPENLINE_COM_LIST))
1499 newindent = second_line_indent; // for ^^D command in insert mode
1500
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001501 // Do smart indenting.
1502 // In insert/replace mode (only when dir == FORWARD)
1503 // we may move some text to the next line. If it starts with '{'
1504 // don't add an indent. Fixes inserting a NL before '{' in line
1505 // "if (condition) {"
1506 if (!trunc_line && do_si && *saved_line != NUL
1507 && (p_extra == NULL || first_char != '{'))
1508 {
1509 char_u *ptr;
1510 char_u last_char;
1511
1512 old_cursor = curwin->w_cursor;
1513 ptr = saved_line;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001514 if (flags & OPENLINE_DO_COM)
1515 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
1516 else
1517 lead_len = 0;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001518 if (dir == FORWARD)
1519 {
1520 // Skip preprocessor directives, unless they are
1521 // recognised as comments.
Bram Moolenaar8c96af92019-09-28 19:05:57 +02001522 if ( lead_len == 0 && ptr[0] == '#')
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001523 {
1524 while (ptr[0] == '#' && curwin->w_cursor.lnum > 1)
1525 ptr = ml_get(--curwin->w_cursor.lnum);
1526 newindent = get_indent();
1527 }
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001528 if (flags & OPENLINE_DO_COM)
1529 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
1530 else
1531 lead_len = 0;
1532 if (lead_len > 0)
1533 {
1534 // This case gets the following right:
1535 // /*
1536 // * A comment (read '\' as '/').
1537 // */
1538 // #define IN_THE_WAY
1539 // This should line up here;
1540 p = skipwhite(ptr);
1541 if (p[0] == '/' && p[1] == '*')
1542 p++;
1543 if (p[0] == '*')
1544 {
1545 for (p++; *p; p++)
1546 {
1547 if (p[0] == '/' && p[-1] == '*')
1548 {
1549 // End of C comment, indent should line up
1550 // with the line containing the start of
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001551 // the comment.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001552 curwin->w_cursor.col = (colnr_T)(p - ptr);
1553 if ((pos = findmatch(NULL, NUL)) != NULL)
1554 {
1555 curwin->w_cursor.lnum = pos->lnum;
1556 newindent = get_indent();
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001557 break;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001558 }
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001559 // this may make "ptr" invalid, get it again
1560 ptr = ml_get(curwin->w_cursor.lnum);
1561 p = ptr + curwin->w_cursor.col;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001562 }
1563 }
1564 }
1565 }
1566 else // Not a comment line
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001567 {
1568 // Find last non-blank in line
1569 p = ptr + STRLEN(ptr) - 1;
1570 while (p > ptr && VIM_ISWHITE(*p))
1571 --p;
1572 last_char = *p;
1573
1574 // find the character just before the '{' or ';'
1575 if (last_char == '{' || last_char == ';')
1576 {
1577 if (p > ptr)
1578 --p;
1579 while (p > ptr && VIM_ISWHITE(*p))
1580 --p;
1581 }
1582 // Try to catch lines that are split over multiple
1583 // lines. eg:
1584 // if (condition &&
1585 // condition) {
1586 // Should line up here!
1587 // }
1588 if (*p == ')')
1589 {
1590 curwin->w_cursor.col = (colnr_T)(p - ptr);
1591 if ((pos = findmatch(NULL, '(')) != NULL)
1592 {
1593 curwin->w_cursor.lnum = pos->lnum;
1594 newindent = get_indent();
1595 ptr = ml_get_curline();
1596 }
1597 }
1598 // If last character is '{' do indent, without
1599 // checking for "if" and the like.
1600 if (last_char == '{')
1601 {
1602 did_si = TRUE; // do indent
1603 no_si = TRUE; // don't delete it when '{' typed
1604 }
1605 // Look for "if" and the like, use 'cinwords'.
1606 // Don't do this if the previous line ended in ';' or
1607 // '}'.
1608 else if (last_char != ';' && last_char != '}'
1609 && cin_is_cinword(ptr))
1610 did_si = TRUE;
1611 }
1612 }
1613 else // dir == BACKWARD
1614 {
1615 // Skip preprocessor directives, unless they are
1616 // recognised as comments.
Bram Moolenaar8c96af92019-09-28 19:05:57 +02001617 if (lead_len == 0 && ptr[0] == '#')
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001618 {
1619 int was_backslashed = FALSE;
1620
1621 while ((ptr[0] == '#' || was_backslashed) &&
1622 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
1623 {
1624 if (*ptr && ptr[STRLEN(ptr) - 1] == '\\')
1625 was_backslashed = TRUE;
1626 else
1627 was_backslashed = FALSE;
1628 ptr = ml_get(++curwin->w_cursor.lnum);
1629 }
1630 if (was_backslashed)
1631 newindent = 0; // Got to end of file
1632 else
1633 newindent = get_indent();
1634 }
1635 p = skipwhite(ptr);
1636 if (*p == '}') // if line starts with '}': do indent
1637 did_si = TRUE;
1638 else // can delete indent when '{' typed
1639 can_si_back = TRUE;
1640 }
1641 curwin->w_cursor = old_cursor;
1642 }
1643 if (do_si)
1644 can_si = TRUE;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001645
1646 did_ai = TRUE;
1647 }
1648
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001649 // May do indenting after opening a new line.
1650 do_cindent = !p_paste && (curbuf->b_p_cin
Bram Moolenaar8e145b82022-05-21 20:17:31 +01001651#ifdef FEAT_EVAL
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001652 || *curbuf->b_p_inde != NUL
Bram Moolenaar8e145b82022-05-21 20:17:31 +01001653#endif
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001654 )
1655 && in_cinkeys(dir == FORWARD
1656 ? KEY_OPEN_FORW
1657 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum));
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001658
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001659 // Find out if the current line starts with a comment leader.
1660 // This may then be inserted in front of the new line.
1661 end_comment_pending = NUL;
1662 if (flags & OPENLINE_DO_COM)
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001663 {
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001664 lead_len = get_leader_len(saved_line, &lead_flags,
1665 dir == BACKWARD, TRUE);
Bram Moolenaar2bf875f2022-05-07 14:54:11 +01001666 if (lead_len == 0 && curbuf->b_p_cin && do_cindent && dir == FORWARD
Bram Moolenaar7e667782022-05-23 13:10:48 +01001667 && (!has_format_option(FO_NO_OPEN_COMS)
1668 || (flags & OPENLINE_FORMAT)))
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001669 {
Bram Moolenaar5ea5f372021-12-29 15:15:47 +00001670 // Check for a line comment after code.
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001671 comment_start = check_linecomment(saved_line);
1672 if (comment_start != MAXCOL)
1673 {
1674 lead_len = get_leader_len(saved_line + comment_start,
Bram Moolenaar5ea5f372021-12-29 15:15:47 +00001675 &lead_flags, FALSE, TRUE);
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001676 if (lead_len != 0)
1677 {
1678 lead_len += comment_start;
1679 if (did_do_comment != NULL)
1680 *did_do_comment = TRUE;
1681 }
1682 }
1683 }
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001684 }
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001685 else
1686 lead_len = 0;
1687 if (lead_len > 0)
1688 {
1689 char_u *lead_repl = NULL; // replaces comment leader
1690 int lead_repl_len = 0; // length of *lead_repl
1691 char_u lead_middle[COM_MAX_LEN]; // middle-comment string
1692 char_u lead_end[COM_MAX_LEN]; // end-comment string
1693 char_u *comment_end = NULL; // where lead_end has been found
1694 int extra_space = FALSE; // append extra space
1695 int current_flag;
1696 int require_blank = FALSE; // requires blank after middle
1697 char_u *p2;
1698
1699 // If the comment leader has the start, middle or end flag, it may not
1700 // be used or may be replaced with the middle leader.
1701 for (p = lead_flags; *p && *p != ':'; ++p)
1702 {
1703 if (*p == COM_BLANK)
1704 {
1705 require_blank = TRUE;
1706 continue;
1707 }
1708 if (*p == COM_START || *p == COM_MIDDLE)
1709 {
1710 current_flag = *p;
1711 if (*p == COM_START)
1712 {
1713 // Doing "O" on a start of comment does not insert leader.
1714 if (dir == BACKWARD)
1715 {
1716 lead_len = 0;
1717 break;
1718 }
1719
1720 // find start of middle part
1721 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
1722 require_blank = FALSE;
1723 }
1724
1725 // Isolate the strings of the middle and end leader.
Bram Moolenaarc667da52019-11-30 20:52:27 +01001726 while (*p && p[-1] != ':') // find end of middle flags
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001727 {
1728 if (*p == COM_BLANK)
1729 require_blank = TRUE;
1730 ++p;
1731 }
1732 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
1733
1734 while (*p && p[-1] != ':') // find end of end flags
1735 {
1736 // Check whether we allow automatic ending of comments
1737 if (*p == COM_AUTO_END)
1738 end_comment_pending = -1; // means we want to set it
1739 ++p;
1740 }
1741 n = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
1742
1743 if (end_comment_pending == -1) // we can set it now
1744 end_comment_pending = lead_end[n - 1];
1745
1746 // If the end of the comment is in the same line, don't use
1747 // the comment leader.
1748 if (dir == FORWARD)
1749 {
1750 for (p = saved_line + lead_len; *p; ++p)
1751 if (STRNCMP(p, lead_end, n) == 0)
1752 {
1753 comment_end = p;
1754 lead_len = 0;
1755 break;
1756 }
1757 }
1758
1759 // Doing "o" on a start of comment inserts the middle leader.
1760 if (lead_len > 0)
1761 {
1762 if (current_flag == COM_START)
1763 {
1764 lead_repl = lead_middle;
1765 lead_repl_len = (int)STRLEN(lead_middle);
1766 }
1767
1768 // If we have hit RETURN immediately after the start
1769 // comment leader, then put a space after the middle
1770 // comment leader on the next line.
1771 if (!VIM_ISWHITE(saved_line[lead_len - 1])
1772 && ((p_extra != NULL
1773 && (int)curwin->w_cursor.col == lead_len)
1774 || (p_extra == NULL
1775 && saved_line[lead_len] == NUL)
1776 || require_blank))
1777 extra_space = TRUE;
1778 }
1779 break;
1780 }
1781 if (*p == COM_END)
1782 {
1783 // Doing "o" on the end of a comment does not insert leader.
1784 // Remember where the end is, might want to use it to find the
1785 // start (for C-comments).
1786 if (dir == FORWARD)
1787 {
1788 comment_end = skipwhite(saved_line);
1789 lead_len = 0;
1790 break;
1791 }
1792
1793 // Doing "O" on the end of a comment inserts the middle leader.
1794 // Find the string for the middle leader, searching backwards.
1795 while (p > curbuf->b_p_com && *p != ',')
1796 --p;
1797 for (lead_repl = p; lead_repl > curbuf->b_p_com
1798 && lead_repl[-1] != ':'; --lead_repl)
1799 ;
1800 lead_repl_len = (int)(p - lead_repl);
1801
1802 // We can probably always add an extra space when doing "O" on
1803 // the comment-end
1804 extra_space = TRUE;
1805
1806 // Check whether we allow automatic ending of comments
1807 for (p2 = p; *p2 && *p2 != ':'; p2++)
1808 {
1809 if (*p2 == COM_AUTO_END)
1810 end_comment_pending = -1; // means we want to set it
1811 }
1812 if (end_comment_pending == -1)
1813 {
1814 // Find last character in end-comment string
1815 while (*p2 && *p2 != ',')
1816 p2++;
1817 end_comment_pending = p2[-1];
1818 }
1819 break;
1820 }
1821 if (*p == COM_FIRST)
1822 {
1823 // Comment leader for first line only: Don't repeat leader
1824 // when using "O", blank out leader when using "o".
1825 if (dir == BACKWARD)
1826 lead_len = 0;
1827 else
1828 {
1829 lead_repl = (char_u *)"";
1830 lead_repl_len = 0;
1831 }
1832 break;
1833 }
1834 }
1835 if (lead_len)
1836 {
1837 // allocate buffer (may concatenate p_extra later)
1838 leader = alloc(lead_len + lead_repl_len + extra_space + extra_len
1839 + (second_line_indent > 0 ? second_line_indent : 0) + 1);
1840 allocated = leader; // remember to free it later
1841
1842 if (leader == NULL)
1843 lead_len = 0;
1844 else
1845 {
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001846 int li;
1847
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001848 vim_strncpy(leader, saved_line, lead_len);
1849
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001850 // TODO: handle multi-byte and double width chars
1851 for (li = 0; li < comment_start; ++li)
1852 if (!VIM_ISWHITE(leader[li]))
1853 leader[li] = ' ';
1854
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001855 // Replace leader with lead_repl, right or left adjusted
1856 if (lead_repl != NULL)
1857 {
1858 int c = 0;
1859 int off = 0;
1860
1861 for (p = lead_flags; *p != NUL && *p != ':'; )
1862 {
1863 if (*p == COM_RIGHT || *p == COM_LEFT)
1864 c = *p++;
1865 else if (VIM_ISDIGIT(*p) || *p == '-')
1866 off = getdigits(&p);
1867 else
1868 ++p;
1869 }
1870 if (c == COM_RIGHT) // right adjusted leader
1871 {
1872 // find last non-white in the leader to line up with
1873 for (p = leader + lead_len - 1; p > leader
1874 && VIM_ISWHITE(*p); --p)
1875 ;
1876 ++p;
1877
1878 // Compute the length of the replaced characters in
1879 // screen characters, not bytes.
1880 {
1881 int repl_size = vim_strnsize(lead_repl,
1882 lead_repl_len);
1883 int old_size = 0;
1884 char_u *endp = p;
1885 int l;
1886
1887 while (old_size < repl_size && p > leader)
1888 {
1889 MB_PTR_BACK(leader, p);
1890 old_size += ptr2cells(p);
1891 }
1892 l = lead_repl_len - (int)(endp - p);
1893 if (l != 0)
1894 mch_memmove(endp + l, endp,
1895 (size_t)((leader + lead_len) - endp));
1896 lead_len += l;
1897 }
1898 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1899 if (p + lead_repl_len > leader + lead_len)
1900 p[lead_repl_len] = NUL;
1901
1902 // blank-out any other chars from the old leader.
1903 while (--p >= leader)
1904 {
1905 int l = mb_head_off(leader, p);
1906
1907 if (l > 1)
1908 {
1909 p -= l;
1910 if (ptr2cells(p) > 1)
1911 {
1912 p[1] = ' ';
1913 --l;
1914 }
1915 mch_memmove(p + 1, p + l + 1,
1916 (size_t)((leader + lead_len) - (p + l + 1)));
1917 lead_len -= l;
1918 *p = ' ';
1919 }
1920 else if (!VIM_ISWHITE(*p))
1921 *p = ' ';
1922 }
1923 }
1924 else // left adjusted leader
1925 {
1926 p = skipwhite(leader);
1927
1928 // Compute the length of the replaced characters in
1929 // screen characters, not bytes. Move the part that is
1930 // not to be overwritten.
1931 {
1932 int repl_size = vim_strnsize(lead_repl,
1933 lead_repl_len);
1934 int i;
1935 int l;
1936
1937 for (i = 0; i < lead_len && p[i] != NUL; i += l)
1938 {
1939 l = (*mb_ptr2len)(p + i);
1940 if (vim_strnsize(p, i + l) > repl_size)
1941 break;
1942 }
1943 if (i != lead_repl_len)
1944 {
1945 mch_memmove(p + lead_repl_len, p + i,
1946 (size_t)(lead_len - i - (p - leader)));
1947 lead_len += lead_repl_len - i;
1948 }
1949 }
1950 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1951
1952 // Replace any remaining non-white chars in the old
1953 // leader by spaces. Keep Tabs, the indent must
1954 // remain the same.
1955 for (p += lead_repl_len; p < leader + lead_len; ++p)
1956 if (!VIM_ISWHITE(*p))
1957 {
1958 // Don't put a space before a TAB.
1959 if (p + 1 < leader + lead_len && p[1] == TAB)
1960 {
1961 --lead_len;
1962 mch_memmove(p, p + 1,
1963 (leader + lead_len) - p);
1964 }
1965 else
1966 {
1967 int l = (*mb_ptr2len)(p);
1968
1969 if (l > 1)
1970 {
1971 if (ptr2cells(p) > 1)
1972 {
1973 // Replace a double-wide char with
1974 // two spaces
1975 --l;
1976 *p++ = ' ';
1977 }
1978 mch_memmove(p + 1, p + l,
1979 (leader + lead_len) - p);
1980 lead_len -= l - 1;
1981 }
1982 *p = ' ';
1983 }
1984 }
1985 *p = NUL;
1986 }
1987
1988 // Recompute the indent, it may have changed.
Bram Moolenaar8e145b82022-05-21 20:17:31 +01001989 if (curbuf->b_p_ai || do_si)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001990#ifdef FEAT_VARTABS
1991 newindent = get_indent_str_vtab(leader, curbuf->b_p_ts,
1992 curbuf->b_p_vts_array, FALSE);
1993#else
1994 newindent = get_indent_str(leader,
1995 (int)curbuf->b_p_ts, FALSE);
1996#endif
1997
1998 // Add the indent offset
1999 if (newindent + off < 0)
2000 {
2001 off = -newindent;
2002 newindent = 0;
2003 }
2004 else
2005 newindent += off;
2006
2007 // Correct trailing spaces for the shift, so that
2008 // alignment remains equal.
2009 while (off > 0 && lead_len > 0
2010 && leader[lead_len - 1] == ' ')
2011 {
2012 // Don't do it when there is a tab before the space
2013 if (vim_strchr(skipwhite(leader), '\t') != NULL)
2014 break;
2015 --lead_len;
2016 --off;
2017 }
2018
2019 // If the leader ends in white space, don't add an
2020 // extra space
2021 if (lead_len > 0 && VIM_ISWHITE(leader[lead_len - 1]))
2022 extra_space = FALSE;
2023 leader[lead_len] = NUL;
2024 }
2025
2026 if (extra_space)
2027 {
2028 leader[lead_len++] = ' ';
2029 leader[lead_len] = NUL;
2030 }
2031
2032 newcol = lead_len;
2033
2034 // if a new indent will be set below, remove the indent that
2035 // is in the comment leader
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002036 if (newindent || did_si)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002037 {
2038 while (lead_len && VIM_ISWHITE(*leader))
2039 {
2040 --lead_len;
2041 --newcol;
2042 ++leader;
2043 }
2044 }
2045
2046 }
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002047 did_si = can_si = FALSE;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002048 }
2049 else if (comment_end != NULL)
2050 {
2051 // We have finished a comment, so we don't use the leader.
2052 // If this was a C-comment and 'ai' or 'si' is set do a normal
2053 // indent to align with the line containing the start of the
2054 // comment.
2055 if (comment_end[0] == '*' && comment_end[1] == '/' &&
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002056 (curbuf->b_p_ai || do_si))
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002057 {
2058 old_cursor = curwin->w_cursor;
2059 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line);
2060 if ((pos = findmatch(NULL, NUL)) != NULL)
2061 {
2062 curwin->w_cursor.lnum = pos->lnum;
2063 newindent = get_indent();
2064 }
2065 curwin->w_cursor = old_cursor;
2066 }
2067 }
2068 }
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002069
Bram Moolenaar24959102022-05-07 20:01:16 +01002070 // (State == MODE_INSERT || State == MODE_REPLACE), only when dir == FORWARD
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002071 if (p_extra != NULL)
2072 {
2073 *p_extra = saved_char; // restore char that NUL replaced
2074
2075 // When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first
2076 // non-blank.
2077 //
Bram Moolenaar24959102022-05-07 20:01:16 +01002078 // When in MODE_REPLACE state, put the deleted blanks on the replace
2079 // stack, preceded by a NUL, so they can be put back when a BS is
2080 // entered.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002081 if (REPLACE_NORMAL(State))
Bram Moolenaarc667da52019-11-30 20:52:27 +01002082 replace_push(NUL); // end of extra blanks
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002083 if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES))
2084 {
2085 while ((*p_extra == ' ' || *p_extra == '\t')
2086 && (!enc_utf8
2087 || !utf_iscomposing(utf_ptr2char(p_extra + 1))))
2088 {
2089 if (REPLACE_NORMAL(State))
2090 replace_push(*p_extra);
2091 ++p_extra;
2092 ++less_cols_off;
2093 }
2094 }
2095
2096 // columns for marks adjusted for removed columns
2097 less_cols = (int)(p_extra - saved_line);
2098 }
2099
2100 if (p_extra == NULL)
2101 p_extra = (char_u *)""; // append empty line
2102
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002103 // concatenate leader and p_extra, if there is a leader
2104 if (lead_len)
2105 {
2106 if (flags & OPENLINE_COM_LIST && second_line_indent > 0)
2107 {
2108 int i;
2109 int padding = second_line_indent
2110 - (newindent + (int)STRLEN(leader));
2111
2112 // Here whitespace is inserted after the comment char.
2113 // Below, set_indent(newindent, SIN_INSERT) will insert the
2114 // whitespace needed before the comment char.
2115 for (i = 0; i < padding; i++)
2116 {
2117 STRCAT(leader, " ");
2118 less_cols--;
2119 newcol++;
2120 }
2121 }
2122 STRCAT(leader, p_extra);
2123 p_extra = leader;
2124 did_ai = TRUE; // So truncating blanks works with comments
2125 less_cols -= lead_len;
2126 }
2127 else
2128 end_comment_pending = NUL; // turns out there was no leader
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002129
2130 old_cursor = curwin->w_cursor;
2131 if (dir == BACKWARD)
2132 --curwin->w_cursor.lnum;
2133 if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count)
2134 {
2135 if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE)
2136 == FAIL)
2137 goto theend;
2138 // Postpone calling changed_lines(), because it would mess up folding
2139 // with markers.
2140 // Skip mark_adjust when adding a line after the last one, there can't
2141 // be marks there. But still needed in diff mode.
2142 if (curwin->w_cursor.lnum + 1 < curbuf->b_ml.ml_line_count
2143#ifdef FEAT_DIFF
2144 || curwin->w_p_diff
2145#endif
2146 )
2147 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
2148 did_append = TRUE;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002149#ifdef FEAT_PROP_POPUP
Bram Moolenaar24959102022-05-07 20:01:16 +01002150 if ((State & MODE_INSERT) && (State & VREPLACE_FLAG) == 0)
LemonBoy788c06a2022-05-14 18:48:05 +01002151 // Properties after the split move to the next line.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002152 adjust_props_for_split(curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002153 curwin->w_cursor.col + 1, 0, at_eol);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002154#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002155 }
2156 else
2157 {
Bram Moolenaar24959102022-05-07 20:01:16 +01002158 // In MODE_VREPLACE state we are starting to replace the next line.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002159 curwin->w_cursor.lnum++;
2160 if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed)
2161 {
2162 // In case we NL to a new line, BS to the previous one, and NL
2163 // again, we don't want to save the new line for undo twice.
Bram Moolenaarc667da52019-11-30 20:52:27 +01002164 (void)u_save_cursor(); // errors are ignored!
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002165 vr_lines_changed++;
2166 }
2167 ml_replace(curwin->w_cursor.lnum, p_extra, TRUE);
2168 changed_bytes(curwin->w_cursor.lnum, 0);
2169 curwin->w_cursor.lnum--;
2170 did_append = FALSE;
2171 }
2172
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002173 if (newindent || did_si)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002174 {
2175 ++curwin->w_cursor.lnum;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002176 if (did_si)
2177 {
2178 int sw = (int)get_sw_value(curbuf);
2179
2180 if (p_sr)
2181 newindent -= newindent % sw;
2182 newindent += sw;
2183 }
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002184 // Copy the indent
2185 if (curbuf->b_p_ci)
2186 {
2187 (void)copy_indent(newindent, saved_line);
2188
2189 // Set the 'preserveindent' option so that any further screwing
2190 // with the line doesn't entirely destroy our efforts to preserve
2191 // it. It gets restored at the function end.
2192 curbuf->b_p_pi = TRUE;
2193 }
2194 else
2195 (void)set_indent(newindent, SIN_INSERT);
2196 less_cols -= curwin->w_cursor.col;
2197
2198 ai_col = curwin->w_cursor.col;
2199
Bram Moolenaar24959102022-05-07 20:01:16 +01002200 // In MODE_REPLACE state, for each character in the new indent, there
2201 // must be a NUL on the replace stack, for when it is deleted with BS
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002202 if (REPLACE_NORMAL(State))
2203 for (n = 0; n < (int)curwin->w_cursor.col; ++n)
2204 replace_push(NUL);
2205 newcol += curwin->w_cursor.col;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002206 if (no_si)
2207 did_si = FALSE;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002208 }
2209
Bram Moolenaar24959102022-05-07 20:01:16 +01002210 // In MODE_REPLACE state, for each character in the extra leader, there
2211 // must be a NUL on the replace stack, for when it is deleted with BS.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002212 if (REPLACE_NORMAL(State))
2213 while (lead_len-- > 0)
2214 replace_push(NUL);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002215
2216 curwin->w_cursor = old_cursor;
2217
2218 if (dir == FORWARD)
2219 {
Bram Moolenaar24959102022-05-07 20:01:16 +01002220 if (trunc_line || (State & MODE_INSERT))
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002221 {
2222 // truncate current line at cursor
2223 saved_line[curwin->w_cursor.col] = NUL;
2224 // Remove trailing white space, unless OPENLINE_KEEPTRAIL used.
2225 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL))
2226 truncate_spaces(saved_line);
2227 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE);
2228 saved_line = NULL;
2229 if (did_append)
2230 {
2231 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
2232 curwin->w_cursor.lnum + 1, 1L);
2233 did_append = FALSE;
2234
2235 // Move marks after the line break to the new line.
2236 if (flags & OPENLINE_MARKFIX)
2237 mark_col_adjust(curwin->w_cursor.lnum,
2238 curwin->w_cursor.col + less_cols_off,
2239 1L, (long)-less_cols, 0);
LemonBoy788c06a2022-05-14 18:48:05 +01002240#ifdef FEAT_PROP_POPUP
2241 // Keep into account the deleted blanks on the new line.
2242 if (curbuf->b_has_textprop && less_cols_off != 0)
2243 adjust_prop_columns(curwin->w_cursor.lnum + 1, 0,
2244 -less_cols_off, 0);
2245#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002246 }
2247 else
2248 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
2249 }
2250
2251 // Put the cursor on the new line. Careful: the scrollup() above may
2252 // have moved w_cursor, we must use old_cursor.
2253 curwin->w_cursor.lnum = old_cursor.lnum + 1;
2254 }
2255 if (did_append)
2256 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L);
2257
2258 curwin->w_cursor.col = newcol;
2259 curwin->w_cursor.coladd = 0;
2260
Bram Moolenaar24959102022-05-07 20:01:16 +01002261 // In MODE_VREPLACE state, we are handling the replace stack ourselves, so
2262 // stop fixthisline() from doing it (via change_indent()) by telling it
2263 // we're in normal MODE_INSERT state.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002264 if (State & VREPLACE_FLAG)
2265 {
2266 vreplace_mode = State; // So we know to put things right later
Bram Moolenaar24959102022-05-07 20:01:16 +01002267 State = MODE_INSERT;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002268 }
2269 else
2270 vreplace_mode = 0;
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002271
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002272 if (!p_paste)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002273 {
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002274 if (leader == NULL
2275 && !use_indentexpr_for_lisp()
2276 && curbuf->b_p_lisp
2277 && curbuf->b_p_ai)
2278 {
2279 // do lisp indenting
2280 fixthisline(get_lisp_indent);
2281 ai_col = (colnr_T)getwhitecols_curline();
2282 }
2283 else if (do_cindent || (curbuf->b_p_ai && use_indentexpr_for_lisp()))
2284 {
2285 // do 'cindent' or 'indentexpr' indenting
2286 do_c_expr_indent();
2287 ai_col = (colnr_T)getwhitecols_curline();
2288 }
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002289 }
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002290
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002291 if (vreplace_mode != 0)
2292 State = vreplace_mode;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002293
Bram Moolenaar24959102022-05-07 20:01:16 +01002294 // Finally, MODE_VREPLACE gets the stuff on the new line, then puts back
2295 // the original line, and inserts the new stuff char by char, pushing old
2296 // stuff onto the replace stack (via ins_char()).
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002297 if (State & VREPLACE_FLAG)
2298 {
2299 // Put new line in p_extra
2300 p_extra = vim_strsave(ml_get_curline());
2301 if (p_extra == NULL)
2302 goto theend;
2303
2304 // Put back original line
2305 ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
2306
2307 // Insert new stuff into line again
2308 curwin->w_cursor.col = 0;
2309 curwin->w_cursor.coladd = 0;
2310 ins_bytes(p_extra); // will call changed_bytes()
2311 vim_free(p_extra);
2312 next_line = NULL;
2313 }
2314
2315 retval = OK; // success!
2316theend:
2317 curbuf->b_p_pi = saved_pi;
2318 vim_free(saved_line);
2319 vim_free(next_line);
2320 vim_free(allocated);
2321 return retval;
2322}
2323
2324/*
2325 * Delete from cursor to end of line.
2326 * Caller must have prepared for undo.
2327 * If "fixpos" is TRUE fix the cursor position when done.
2328 *
2329 * Return FAIL for failure, OK otherwise.
2330 */
2331 int
2332truncate_line(int fixpos)
2333{
2334 char_u *newp;
2335 linenr_T lnum = curwin->w_cursor.lnum;
2336 colnr_T col = curwin->w_cursor.col;
LemonBoyd0b1a092022-05-12 18:45:18 +01002337 char_u *old_line;
2338 int deleted;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002339
LemonBoyd0b1a092022-05-12 18:45:18 +01002340 old_line = ml_get(lnum);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002341 if (col == 0)
2342 newp = vim_strsave((char_u *)"");
2343 else
LemonBoyd0b1a092022-05-12 18:45:18 +01002344 newp = vim_strnsave(old_line, col);
2345 deleted = (int)STRLEN(old_line) - col;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002346
2347 if (newp == NULL)
2348 return FAIL;
2349
2350 ml_replace(lnum, newp, FALSE);
2351
2352 // mark the buffer as changed and prepare for displaying
LemonBoyd0b1a092022-05-12 18:45:18 +01002353 inserted_bytes(lnum, curwin->w_cursor.col, -deleted);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002354
2355 // If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2356 if (fixpos && curwin->w_cursor.col > 0)
2357 --curwin->w_cursor.col;
2358
2359 return OK;
2360}
2361
2362/*
2363 * Delete "nlines" lines at the cursor.
2364 * Saves the lines for undo first if "undo" is TRUE.
2365 */
2366 void
2367del_lines(long nlines, int undo)
2368{
2369 long n;
2370 linenr_T first = curwin->w_cursor.lnum;
2371
2372 if (nlines <= 0)
2373 return;
2374
2375 // save the deleted lines for undo
2376 if (undo && u_savedel(first, nlines) == FAIL)
2377 return;
2378
2379 for (n = 0; n < nlines; )
2380 {
2381 if (curbuf->b_ml.ml_flags & ML_EMPTY) // nothing to delete
2382 break;
2383
Bram Moolenaarca70c072020-05-30 20:30:46 +02002384 ml_delete_flags(first, ML_DEL_MESSAGE);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002385 ++n;
2386
2387 // If we delete the last line in the file, stop
2388 if (first > curbuf->b_ml.ml_line_count)
2389 break;
2390 }
2391
2392 // Correct the cursor position before calling deleted_lines_mark(), it may
2393 // trigger a callback to display the cursor.
2394 curwin->w_cursor.col = 0;
2395 check_cursor_lnum();
2396
2397 // adjust marks, mark the buffer as changed and prepare for displaying
2398 deleted_lines_mark(first, n);
2399}