blob: 438bbda18ba9181c55d5e12b8e76bb99db9c3edc [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;
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +0200145#if defined(FEAT_TABPANEL)
146 redraw_tabpanel = TRUE;
147#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200148 need_maketitle = TRUE; // set window title later
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200149}
150
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200151#ifdef FEAT_EVAL
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200152static long next_listener_id = 0;
153
154/*
Bram Moolenaar4a0a1612019-07-17 22:00:19 +0200155 * Check if the change at "lnum" is above or overlaps with an existing
156 * change. If above then flush changes and invoke listeners.
Bram Moolenaardda41442019-05-16 22:11:47 +0200157 */
Bram Moolenaar55737c22022-02-14 14:51:22 +0000158 static void
Bram Moolenaardda41442019-05-16 22:11:47 +0200159check_recorded_changes(
160 buf_T *buf,
161 linenr_T lnum,
Bram Moolenaardda41442019-05-16 22:11:47 +0200162 linenr_T lnume,
Bram Moolenaar4a0a1612019-07-17 22:00:19 +0200163 long xtra)
Bram Moolenaardda41442019-05-16 22:11:47 +0200164{
Yegappan Lakshmanan3f0092c2022-10-16 21:43:07 +0100165 if (buf->b_recorded_changes == NULL || xtra == 0)
166 return;
Bram Moolenaardda41442019-05-16 22:11:47 +0200167
Yegappan Lakshmanan3f0092c2022-10-16 21:43:07 +0100168 listitem_T *li;
169 linenr_T prev_lnum;
170 linenr_T prev_lnume;
171
172 FOR_ALL_LIST_ITEMS(buf->b_recorded_changes, li)
173 {
174 prev_lnum = (linenr_T)dict_get_number(
175 li->li_tv.vval.v_dict, "lnum");
176 prev_lnume = (linenr_T)dict_get_number(
177 li->li_tv.vval.v_dict, "end");
178 if (prev_lnum >= lnum || prev_lnum > lnume || prev_lnume >= lnum)
Bram Moolenaardda41442019-05-16 22:11:47 +0200179 {
Yegappan Lakshmanan3f0092c2022-10-16 21:43:07 +0100180 // the current change is going to make the line number in
181 // the older change invalid, flush now
182 invoke_listeners(curbuf);
183 break;
Bram Moolenaardda41442019-05-16 22:11:47 +0200184 }
185 }
Bram Moolenaardda41442019-05-16 22:11:47 +0200186}
187
188/*
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200189 * Record a change for listeners added with listener_add().
Bram Moolenaardda41442019-05-16 22:11:47 +0200190 * Always for the current buffer.
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200191 */
192 static void
193may_record_change(
194 linenr_T lnum,
195 colnr_T col,
196 linenr_T lnume,
197 long xtra)
198{
199 dict_T *dict;
200
201 if (curbuf->b_listener == NULL)
202 return;
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200203
204 // If the new change is going to change the line numbers in already listed
205 // changes, then flush.
Bram Moolenaar55737c22022-02-14 14:51:22 +0000206 check_recorded_changes(curbuf, lnum, lnume, xtra);
Bram Moolenaardda41442019-05-16 22:11:47 +0200207
208 if (curbuf->b_recorded_changes == NULL)
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200209 {
Bram Moolenaardda41442019-05-16 22:11:47 +0200210 curbuf->b_recorded_changes = list_alloc();
211 if (curbuf->b_recorded_changes == NULL) // out of memory
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200212 return;
Bram Moolenaardda41442019-05-16 22:11:47 +0200213 ++curbuf->b_recorded_changes->lv_refcount;
214 curbuf->b_recorded_changes->lv_lock = VAR_FIXED;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200215 }
216
217 dict = dict_alloc();
218 if (dict == NULL)
219 return;
220 dict_add_number(dict, "lnum", (varnumber_T)lnum);
221 dict_add_number(dict, "end", (varnumber_T)lnume);
222 dict_add_number(dict, "added", (varnumber_T)xtra);
Bram Moolenaara3347722019-05-11 21:14:24 +0200223 dict_add_number(dict, "col", (varnumber_T)col + 1);
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200224
Bram Moolenaardda41442019-05-16 22:11:47 +0200225 list_append_dict(curbuf->b_recorded_changes, dict);
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200226}
227
228/*
229 * listener_add() function
230 */
231 void
232f_listener_add(typval_T *argvars, typval_T *rettv)
233{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200234 callback_T callback;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200235 listener_T *lnr;
Bram Moolenaara3347722019-05-11 21:14:24 +0200236 buf_T *buf = curbuf;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200237
Yegappan Lakshmanan5bca9062021-07-24 21:33:26 +0200238 if (in_vim9script() && check_for_opt_buffer_arg(argvars, 1) == FAIL)
239 return;
240
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200241 callback = get_callback(&argvars[0]);
242 if (callback.cb_name == NULL)
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200243 return;
244
Bram Moolenaara3347722019-05-11 21:14:24 +0200245 if (argvars[1].v_type != VAR_UNKNOWN)
246 {
247 buf = get_buf_arg(&argvars[1]);
248 if (buf == NULL)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200249 {
250 free_callback(&callback);
Bram Moolenaara3347722019-05-11 21:14:24 +0200251 return;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200252 }
Bram Moolenaara3347722019-05-11 21:14:24 +0200253 }
254
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200255 lnr = ALLOC_CLEAR_ONE(listener_T);
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200256 if (lnr == NULL)
257 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200258 free_callback(&callback);
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200259 return;
260 }
Bram Moolenaara3347722019-05-11 21:14:24 +0200261 lnr->lr_next = buf->b_listener;
262 buf->b_listener = lnr;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200263
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200264 set_callback(&lnr->lr_callback, &callback);
Bram Moolenaarc96b7f52022-12-02 15:58:38 +0000265 if (callback.cb_free_name)
266 vim_free(callback.cb_name);
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200267
268 lnr->lr_id = ++next_listener_id;
269 rettv->vval.v_number = lnr->lr_id;
270}
271
272/*
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200273 * listener_flush() function
274 */
275 void
276f_listener_flush(typval_T *argvars, typval_T *rettv UNUSED)
277{
278 buf_T *buf = curbuf;
279
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200280 if (in_vim9script() && check_for_opt_buffer_arg(argvars, 0) == FAIL)
281 return;
282
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200283 if (argvars[0].v_type != VAR_UNKNOWN)
284 {
285 buf = get_buf_arg(&argvars[0]);
286 if (buf == NULL)
287 return;
288 }
289 invoke_listeners(buf);
290}
291
Bram Moolenaar4b4b1b82021-09-11 15:06:44 +0200292
293 static void
294remove_listener(buf_T *buf, listener_T *lnr, listener_T *prev)
295{
296 if (prev != NULL)
297 prev->lr_next = lnr->lr_next;
298 else
299 buf->b_listener = lnr->lr_next;
300 free_callback(&lnr->lr_callback);
301 vim_free(lnr);
302}
303
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200304/*
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200305 * listener_remove() function
306 */
307 void
Bram Moolenaar7b73f912019-07-13 13:03:02 +0200308f_listener_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200309{
310 listener_T *lnr;
311 listener_T *next;
Bram Moolenaar7b73f912019-07-13 13:03:02 +0200312 listener_T *prev;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200313 int id;
Bram Moolenaara3347722019-05-11 21:14:24 +0200314 buf_T *buf;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200315
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200316 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
317 return;
318
319 id = tv_get_number(argvars);
Bram Moolenaar86173482019-10-01 17:02:16 +0200320 FOR_ALL_BUFFERS(buf)
Bram Moolenaar7b73f912019-07-13 13:03:02 +0200321 {
322 prev = NULL;
Bram Moolenaara3347722019-05-11 21:14:24 +0200323 for (lnr = buf->b_listener; lnr != NULL; lnr = next)
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200324 {
Bram Moolenaara3347722019-05-11 21:14:24 +0200325 next = lnr->lr_next;
326 if (lnr->lr_id == id)
327 {
zeertzjqcfe45652022-05-27 17:26:55 +0100328 if (textlock > 0)
Bram Moolenaar4b4b1b82021-09-11 15:06:44 +0200329 {
330 // in invoke_listeners(), clear ID and delete later
331 lnr->lr_id = 0;
332 return;
333 }
334 remove_listener(buf, lnr, prev);
Bram Moolenaar7b73f912019-07-13 13:03:02 +0200335 rettv->vval.v_number = 1;
336 return;
Bram Moolenaara3347722019-05-11 21:14:24 +0200337 }
338 prev = lnr;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200339 }
Bram Moolenaar7b73f912019-07-13 13:03:02 +0200340 }
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200341}
342
343/*
Bram Moolenaardda41442019-05-16 22:11:47 +0200344 * Called before inserting a line above "lnum"/"lnum3" or deleting line "lnum"
345 * to "lnume".
346 */
347 void
348may_invoke_listeners(buf_T *buf, linenr_T lnum, linenr_T lnume, int added)
349{
Bram Moolenaar4a0a1612019-07-17 22:00:19 +0200350 check_recorded_changes(buf, lnum, lnume, added);
Bram Moolenaardda41442019-05-16 22:11:47 +0200351}
352
353/*
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200354 * Called when a sequence of changes is done: invoke listeners added with
355 * listener_add().
356 */
357 void
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200358invoke_listeners(buf_T *buf)
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200359{
360 listener_T *lnr;
361 typval_T rettv;
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200362 typval_T argv[6];
363 listitem_T *li;
364 linenr_T start = MAXLNUM;
365 linenr_T end = 0;
366 linenr_T added = 0;
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200367 int save_updating_screen = updating_screen;
368 static int recursive = FALSE;
Bram Moolenaar4b4b1b82021-09-11 15:06:44 +0200369 listener_T *next;
Yegappan Lakshmanan956be462022-09-02 17:12:07 +0100370 listener_T *prev;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200371
Bram Moolenaardda41442019-05-16 22:11:47 +0200372 if (buf->b_recorded_changes == NULL // nothing changed
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200373 || buf->b_listener == NULL // no listeners
374 || recursive) // already busy
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200375 return;
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200376 recursive = TRUE;
377
378 // Block messages on channels from being handled, so that they don't make
379 // text changes here.
380 ++updating_screen;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200381
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200382 argv[0].v_type = VAR_NUMBER;
383 argv[0].vval.v_number = buf->b_fnum; // a:bufnr
384
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200385 FOR_ALL_LIST_ITEMS(buf->b_recorded_changes, li)
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200386 {
387 varnumber_T lnum;
388
Bram Moolenaard61efa52022-07-23 09:52:04 +0100389 lnum = dict_get_number(li->li_tv.vval.v_dict, "lnum");
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200390 if (start > lnum)
391 start = lnum;
Bram Moolenaard61efa52022-07-23 09:52:04 +0100392 lnum = dict_get_number(li->li_tv.vval.v_dict, "end");
Bram Moolenaar336bf2b2019-10-24 20:07:07 +0200393 if (end < lnum)
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200394 end = lnum;
Bram Moolenaard61efa52022-07-23 09:52:04 +0100395 added += dict_get_number(li->li_tv.vval.v_dict, "added");
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200396 }
397 argv[1].v_type = VAR_NUMBER;
398 argv[1].vval.v_number = start;
399 argv[2].v_type = VAR_NUMBER;
400 argv[2].vval.v_number = end;
401 argv[3].v_type = VAR_NUMBER;
402 argv[3].vval.v_number = added;
403
404 argv[4].v_type = VAR_LIST;
Bram Moolenaardda41442019-05-16 22:11:47 +0200405 argv[4].vval.v_list = buf->b_recorded_changes;
zeertzjqcfe45652022-05-27 17:26:55 +0100406 ++textlock;
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200407
408 for (lnr = buf->b_listener; lnr != NULL; lnr = lnr->lr_next)
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200409 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200410 call_callback(&lnr->lr_callback, -1, &rettv, 5, argv);
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200411 clear_tv(&rettv);
412 }
413
Bram Moolenaar4b4b1b82021-09-11 15:06:44 +0200414 // If f_listener_remove() was called may have to remove a listener now.
Yegappan Lakshmanan956be462022-09-02 17:12:07 +0100415 prev = NULL;
Bram Moolenaar4b4b1b82021-09-11 15:06:44 +0200416 for (lnr = buf->b_listener; lnr != NULL; lnr = next)
417 {
Bram Moolenaar4b4b1b82021-09-11 15:06:44 +0200418 next = lnr->lr_next;
419 if (lnr->lr_id == 0)
420 remove_listener(buf, lnr, prev);
421 else
422 prev = lnr;
423 }
424
zeertzjqcfe45652022-05-27 17:26:55 +0100425 --textlock;
Bram Moolenaardda41442019-05-16 22:11:47 +0200426 list_unref(buf->b_recorded_changes);
427 buf->b_recorded_changes = NULL;
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200428
429 if (save_updating_screen)
430 updating_screen = TRUE;
431 else
432 after_updating_screen(TRUE);
433 recursive = FALSE;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200434}
Bram Moolenaar86173482019-10-01 17:02:16 +0200435
436/*
437 * Remove all listeners associated with "buf".
438 */
439 void
440remove_listeners(buf_T *buf)
441{
442 listener_T *lnr;
443 listener_T *next;
444
445 for (lnr = buf->b_listener; lnr != NULL; lnr = next)
446 {
447 next = lnr->lr_next;
448 free_callback(&lnr->lr_callback);
449 vim_free(lnr);
450 }
451 buf->b_listener = NULL;
452}
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200453#endif
454
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200455/*
456 * Common code for when a change was made.
457 * See changed_lines() for the arguments.
458 * Careful: may trigger autocommands that reload the buffer.
459 */
460 static void
461changed_common(
462 linenr_T lnum,
463 colnr_T col,
464 linenr_T lnume,
465 long xtra)
466{
467 win_T *wp;
468 tabpage_T *tp;
469 int i;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200470 int cols;
471 pos_T *p;
472 int add;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200473
474 // mark the buffer as modified
475 changed();
476
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200477#ifdef FEAT_EVAL
478 may_record_change(lnum, col, lnume, xtra);
479#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200480#ifdef FEAT_DIFF
481 if (curwin->w_p_diff && diff_internal())
Yee Cheng Chin9943d472025-03-26 19:41:02 +0100482 {
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200483 curtab->tp_diff_update = TRUE;
Yee Cheng Chin9943d472025-03-26 19:41:02 +0100484 diff_update_line(lnum);
485 }
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200486#endif
487
488 // set the '. mark
Bram Moolenaare1004402020-10-24 20:49:43 +0200489 if ((cmdmod.cmod_flags & CMOD_KEEPJUMPS) == 0)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200490 {
491 curbuf->b_last_change.lnum = lnum;
492 curbuf->b_last_change.col = col;
493
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200494 // Create a new entry if a new undo-able change was started or we
495 // don't have an entry yet.
496 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
497 {
498 if (curbuf->b_changelistlen == 0)
499 add = TRUE;
500 else
501 {
502 // Don't create a new entry when the line number is the same
503 // as the last one and the column is not too far away. Avoids
504 // creating many entries for typing "xxxxx".
505 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
506 if (p->lnum != lnum)
507 add = TRUE;
508 else
509 {
510 cols = comp_textwidth(FALSE);
511 if (cols == 0)
512 cols = 79;
513 add = (p->col + cols < col || col + cols < p->col);
514 }
515 }
516 if (add)
517 {
518 // This is the first of a new sequence of undo-able changes
519 // and it's at some distance of the last change. Use a new
520 // position in the changelist.
521 curbuf->b_new_change = FALSE;
522
523 if (curbuf->b_changelistlen == JUMPLISTSIZE)
524 {
525 // changelist is full: remove oldest entry
526 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
527 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
528 sizeof(pos_T) * (JUMPLISTSIZE - 1));
529 FOR_ALL_TAB_WINDOWS(tp, wp)
530 {
531 // Correct position in changelist for other windows on
532 // this buffer.
533 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
534 --wp->w_changelistidx;
535 }
536 }
537 FOR_ALL_TAB_WINDOWS(tp, wp)
538 {
539 // For other windows, if the position in the changelist is
540 // at the end it stays at the end.
541 if (wp->w_buffer == curbuf
542 && wp->w_changelistidx == curbuf->b_changelistlen)
543 ++wp->w_changelistidx;
544 }
545 ++curbuf->b_changelistlen;
546 }
547 }
548 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
549 curbuf->b_last_change;
550 // The current window is always after the last change, so that "g,"
551 // takes you back to it.
552 curwin->w_changelistidx = curbuf->b_changelistlen;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200553 }
554
Bram Moolenaar7ce5b2b2022-05-16 19:40:59 +0100555 if (VIsual_active)
556 check_visual_pos();
557
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200558 FOR_ALL_TAB_WINDOWS(tp, wp)
559 {
560 if (wp->w_buffer == curbuf)
561 {
Bram Moolenaar94377372022-02-16 20:30:52 +0000562 linenr_T last = lnume + xtra - 1; // last line after the change
Luuk van Baal5d01f862023-05-11 19:24:20 +0100563
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200564 // Mark this window to be redrawn later.
Bram Moolenaar471c0fa2022-08-22 15:19:16 +0100565 if (!redraw_not_allowed && wp->w_redr_type < UPD_VALID)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100566 wp->w_redr_type = UPD_VALID;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200567
zeertzjqf2d90a32024-02-12 20:28:01 +0100568 // When inserting/deleting lines and the window has specific lines
569 // to be redrawn, w_redraw_top and w_redraw_bot may now be invalid,
570 // so just redraw everything.
571 if (xtra != 0 && wp->w_redraw_top != 0)
572 redraw_win_later(wp, UPD_NOT_VALID);
573
Luuk van Baal5d01f862023-05-11 19:24:20 +0100574 // Reset "w_skipcol" if the topline length has become smaller to
575 // such a degree that nothing will be visible anymore, accounting
576 // for 'smoothscroll' <<< or 'listchars' "precedes" marker.
577 if (wp->w_skipcol > 0
578 && (last < wp->w_topline
579 || (wp->w_topline >= lnum
580 && wp->w_topline < lnume
zeertzjq2c47ab82025-02-13 20:34:34 +0100581 && linetabsize_eol(wp, wp->w_topline)
Luuk van Baalcb204e62024-04-02 20:49:45 +0200582 <= wp->w_skipcol + sms_marker_overlap(wp, -1))))
Luuk van Baal5d01f862023-05-11 19:24:20 +0100583 wp->w_skipcol = 0;
584
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200585 // Check if a change in the buffer has invalidated the cached
586 // values for the cursor.
587#ifdef FEAT_FOLDING
588 // Update the folds for this window. Can't postpone this, because
589 // a following operator might work on the whole fold: ">>dd".
Bram Moolenaar94377372022-02-16 20:30:52 +0000590 foldUpdate(wp, lnum, last);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200591
592 // The change may cause lines above or below the change to become
593 // included in a fold. Set lnum/lnume to the first/last line that
594 // might be displayed differently.
595 // Set w_cline_folded here as an efficient way to update it when
596 // inserting lines just above a closed fold.
597 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
598 if (wp->w_cursor.lnum == lnum)
599 wp->w_cline_folded = i;
Bram Moolenaar94377372022-02-16 20:30:52 +0000600 i = hasFoldingWin(wp, last, NULL, &last, FALSE, NULL);
601 if (wp->w_cursor.lnum == last)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200602 wp->w_cline_folded = i;
603
604 // If the changed line is in a range of previously folded lines,
605 // compare with the first line in that range.
606 if (wp->w_cursor.lnum <= lnum)
607 {
608 i = find_wl_entry(wp, lnum);
609 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
610 changed_line_abv_curs_win(wp);
611 }
612#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200613 if (wp->w_cursor.lnum > lnum)
614 changed_line_abv_curs_win(wp);
615 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
616 changed_cline_bef_curs_win(wp);
617 if (wp->w_botline >= lnum)
618 {
Bram Moolenaar5bea41d2021-07-13 22:21:44 +0200619 if (xtra < 0)
620 invalidate_botline_win(wp);
621 else
622 // Assume that botline doesn't change (inserted lines make
623 // other lines scroll down below botline).
624 approximate_botline_win(wp);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200625 }
626
627 // Check if any w_lines[] entries have become invalid.
628 // For entries below the change: Correct the lnums for
629 // inserted/deleted lines. Makes it possible to stop displaying
630 // after the change.
631 for (i = 0; i < wp->w_lines_valid; ++i)
632 if (wp->w_lines[i].wl_valid)
633 {
634 if (wp->w_lines[i].wl_lnum >= lnum)
635 {
Bram Moolenaar61fdbfa2023-02-04 13:57:55 +0000636 // Do not change wl_lnum at index zero, it is used to
637 // compare with w_topline. Invalidate it instead.
638 if (wp->w_lines[i].wl_lnum < lnume || i == 0)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200639 {
640 // line included in change
641 wp->w_lines[i].wl_valid = FALSE;
642 }
643 else if (xtra != 0)
644 {
645 // line below change
646 wp->w_lines[i].wl_lnum += xtra;
647#ifdef FEAT_FOLDING
648 wp->w_lines[i].wl_lastlnum += xtra;
649#endif
650 }
651 }
652#ifdef FEAT_FOLDING
653 else if (wp->w_lines[i].wl_lastlnum >= lnum)
654 {
655 // change somewhere inside this range of folded lines,
656 // may need to be redrawn
657 wp->w_lines[i].wl_valid = FALSE;
658 }
659#endif
660 }
661
662#ifdef FEAT_FOLDING
663 // Take care of side effects for setting w_topline when folds have
664 // changed. Esp. when the buffer was changed in another window.
665 if (hasAnyFolding(wp))
666 set_topline(wp, wp->w_topline);
667#endif
zeertzjq8c979602022-04-07 15:08:01 +0100668 // If lines have been added or removed, relative numbering always
zeertzjq7ce34c92024-02-08 11:34:55 +0100669 // requires an update even if cursor didn't move.
Lewis Russell16246392022-03-29 11:38:17 +0100670 if (wp->w_p_rnu && xtra != 0)
zeertzjq8c979602022-04-07 15:08:01 +0100671 wp->w_last_cursor_lnum_rnu = 0;
zeertzjq7ce34c92024-02-08 11:34:55 +0100672
Bram Moolenaar11a58af2019-10-24 22:32:31 +0200673#ifdef FEAT_SYN_HL
zeertzjq7ce34c92024-02-08 11:34:55 +0100674 if (wp->w_p_cul && wp->w_last_cursorline >= lnum)
Bram Moolenaar11a58af2019-10-24 22:32:31 +0200675 {
zeertzjq7ce34c92024-02-08 11:34:55 +0100676 if (wp->w_last_cursorline < lnume)
677 // If 'cursorline' was inside the change, it has already
678 // been invalidated in w_lines[] by the loop above.
679 wp->w_last_cursorline = 0;
680 else
681 // If 'cursorline' was below the change, adjust its lnum.
682 wp->w_last_cursorline += xtra;
Bram Moolenaar11a58af2019-10-24 22:32:31 +0200683 }
684#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200685 }
Bram Moolenaar368137a2022-05-31 13:43:12 +0100686#ifdef FEAT_SEARCH_EXTRA
687 if (wp == curwin && xtra != 0 && search_hl_has_cursor_lnum >= lnum)
688 search_hl_has_cursor_lnum += xtra;
689#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200690 }
691
692 // Call update_screen() later, which checks out what needs to be redrawn,
693 // since it notices b_mod_set and then uses b_mod_*.
Bram Moolenaar471c0fa2022-08-22 15:19:16 +0100694 set_must_redraw(UPD_VALID);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200695
696 // when the cursor line is changed always trigger CursorMoved
697 if (lnum <= curwin->w_cursor.lnum
698 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
699 last_cursormoved.lnum = 0;
700}
701
702 static void
703changedOneline(buf_T *buf, linenr_T lnum)
704{
705 if (buf->b_mod_set)
706 {
707 // find the maximum area that must be redisplayed
708 if (lnum < buf->b_mod_top)
709 buf->b_mod_top = lnum;
710 else if (lnum >= buf->b_mod_bot)
711 buf->b_mod_bot = lnum + 1;
712 }
713 else
714 {
715 // set the area that must be redisplayed to one line
716 buf->b_mod_set = TRUE;
717 buf->b_mod_top = lnum;
718 buf->b_mod_bot = lnum + 1;
719 buf->b_mod_xlines = 0;
720 }
721}
722
723/*
724 * Changed bytes within a single line for the current buffer.
725 * - marks the windows on this buffer to be redisplayed
726 * - marks the buffer changed by calling changed()
727 * - invalidates cached values
728 * Careful: may trigger autocommands that reload the buffer.
729 */
730 void
731changed_bytes(linenr_T lnum, colnr_T col)
732{
733 changedOneline(curbuf, lnum);
734 changed_common(lnum, col, lnum + 1, 0L);
735
Bram Moolenaar26f09ea2022-09-27 16:29:38 +0100736#ifdef FEAT_SPELL
737 // When text has been changed at the end of the line, possibly the start of
738 // the next line may have SpellCap that should be removed or it needs to be
739 // displayed. Schedule the next line for redrawing just in case.
Bram Moolenaarf3ef0262022-10-05 13:29:15 +0100740 // Don't do this when displaying '$' at the end of changed text.
741 if (spell_check_window(curwin)
742 && lnum < curbuf->b_ml.ml_line_count
743 && vim_strchr(p_cpo, CPO_DOLLAR) == NULL)
Bram Moolenaar26f09ea2022-09-27 16:29:38 +0100744 redrawWinline(curwin, lnum + 1);
745#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200746#ifdef FEAT_DIFF
747 // Diff highlighting in other diff windows may need to be updated too.
748 if (curwin->w_p_diff)
749 {
750 win_T *wp;
751 linenr_T wlnum;
752
753 FOR_ALL_WINDOWS(wp)
754 if (wp->w_p_diff && wp != curwin)
755 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100756 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200757 wlnum = diff_lnum_win(lnum, wp);
758 if (wlnum > 0)
759 changedOneline(wp->w_buffer, wlnum);
760 }
761 }
762#endif
763}
764
765/*
766 * Like changed_bytes() but also adjust text properties for "added" bytes.
767 * When "added" is negative text was deleted.
768 */
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200769 void
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200770inserted_bytes(linenr_T lnum, colnr_T col, int added UNUSED)
771{
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100772#ifdef FEAT_PROP_POPUP
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200773 if (curbuf->b_has_textprop && added != 0)
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200774 adjust_prop_columns(lnum, col, added, 0);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200775#endif
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200776
777 changed_bytes(lnum, col);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200778}
779
780/*
781 * Appended "count" lines below line "lnum" in the current buffer.
782 * Must be called AFTER the change and after mark_adjust().
783 * Takes care of marking the buffer to be redrawn and sets the changed flag.
784 */
785 void
786appended_lines(linenr_T lnum, long count)
787{
788 changed_lines(lnum + 1, 0, lnum + 1, count);
789}
790
791/*
792 * Like appended_lines(), but adjust marks first.
793 */
794 void
795appended_lines_mark(linenr_T lnum, long count)
796{
Brandon Simmonsda3dd7d2023-01-17 19:48:07 +0000797 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200798 changed_lines(lnum + 1, 0, lnum + 1, count);
799}
800
801/*
802 * Deleted "count" lines at line "lnum" in the current buffer.
803 * Must be called AFTER the change and after mark_adjust().
804 * Takes care of marking the buffer to be redrawn and sets the changed flag.
805 */
806 void
807deleted_lines(linenr_T lnum, long count)
808{
809 changed_lines(lnum, 0, lnum + count, -count);
810}
811
812/*
813 * Like deleted_lines(), but adjust marks first.
814 * Make sure the cursor is on a valid line before calling, a GUI callback may
815 * be triggered to display the cursor.
816 */
817 void
818deleted_lines_mark(linenr_T lnum, long count)
819{
820 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
821 changed_lines(lnum, 0, lnum + count, -count);
822}
823
824/*
825 * Marks the area to be redrawn after a change.
Bram Moolenaar326c5d32022-08-12 13:05:49 +0100826 * Consider also calling changed_line_display_buf().
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200827 */
Bram Moolenaar1764faa2021-05-16 20:18:57 +0200828 void
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200829changed_lines_buf(
830 buf_T *buf,
831 linenr_T lnum, // first line with change
832 linenr_T lnume, // line below last changed line
833 long xtra) // number of extra lines (negative when deleting)
834{
835 if (buf->b_mod_set)
836 {
837 // find the maximum area that must be redisplayed
838 if (lnum < buf->b_mod_top)
839 buf->b_mod_top = lnum;
840 if (lnum < buf->b_mod_bot)
841 {
842 // adjust old bot position for xtra lines
843 buf->b_mod_bot += xtra;
844 if (buf->b_mod_bot < lnum)
845 buf->b_mod_bot = lnum;
846 }
847 if (lnume + xtra > buf->b_mod_bot)
848 buf->b_mod_bot = lnume + xtra;
849 buf->b_mod_xlines += xtra;
850 }
851 else
852 {
853 // set the area that must be redisplayed
854 buf->b_mod_set = TRUE;
855 buf->b_mod_top = lnum;
856 buf->b_mod_bot = lnume + xtra;
857 buf->b_mod_xlines = xtra;
858 }
859}
860
861/*
862 * Changed lines for the current buffer.
863 * Must be called AFTER the change and after mark_adjust().
864 * - mark the buffer changed by calling changed()
865 * - mark the windows on this buffer to be redisplayed
866 * - invalidate cached values
867 * "lnum" is the first line that needs displaying, "lnume" the first line
868 * below the changed lines (BEFORE the change).
869 * When only inserting lines, "lnum" and "lnume" are equal.
870 * Takes care of calling changed() and updating b_mod_*.
871 * Careful: may trigger autocommands that reload the buffer.
872 */
873 void
874changed_lines(
875 linenr_T lnum, // first line with change
876 colnr_T col, // column in first line with change
877 linenr_T lnume, // line below last changed line
878 long xtra) // number of extra lines (negative when deleting)
879{
880 changed_lines_buf(curbuf, lnum, lnume, xtra);
881
882#ifdef FEAT_DIFF
883 if (xtra == 0 && curwin->w_p_diff && !diff_internal())
884 {
885 // When the number of lines doesn't change then mark_adjust() isn't
886 // called and other diff buffers still need to be marked for
887 // displaying.
888 win_T *wp;
889 linenr_T wlnum;
890
891 FOR_ALL_WINDOWS(wp)
892 if (wp->w_p_diff && wp != curwin)
893 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100894 redraw_win_later(wp, UPD_VALID);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200895 wlnum = diff_lnum_win(lnum, wp);
896 if (wlnum > 0)
897 changed_lines_buf(wp->w_buffer, wlnum,
898 lnume - lnum + wlnum, 0L);
899 }
900 }
901#endif
902
903 changed_common(lnum, col, lnume, xtra);
904}
905
906/*
907 * Called when the changed flag must be reset for buffer "buf".
908 * When "ff" is TRUE also reset 'fileformat'.
Bram Moolenaarc024b462019-06-08 18:07:21 +0200909 * When "always_inc_changedtick" is TRUE b:changedtick is incremented also when
910 * the changed flag was off.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200911 */
912 void
Bram Moolenaarc024b462019-06-08 18:07:21 +0200913unchanged(buf_T *buf, int ff, int always_inc_changedtick)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200914{
915 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
916 {
917 buf->b_changed = 0;
918 ml_setflags(buf);
919 if (ff)
920 save_file_ff(buf);
921 check_status(buf);
922 redraw_tabline = TRUE;
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +0200923#if defined(FEAT_TABPANEL)
924 redraw_tabpanel = TRUE;
925#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200926 need_maketitle = TRUE; // set window title later
Bram Moolenaarc024b462019-06-08 18:07:21 +0200927 ++CHANGEDTICK(buf);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200928 }
Bram Moolenaarc024b462019-06-08 18:07:21 +0200929 else if (always_inc_changedtick)
930 ++CHANGEDTICK(buf);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200931#ifdef FEAT_NETBEANS_INTG
932 netbeans_unmodified(buf);
933#endif
934}
935
936/*
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100937 * Save the current values of 'fileformat' and 'fileencoding', so that we know
938 * the file must be considered changed when the value is different.
939 */
940 void
941save_file_ff(buf_T *buf)
942{
943 buf->b_start_ffc = *buf->b_p_ff;
Bram Moolenaar15775372022-10-29 20:01:52 +0100944 buf->b_start_eof = buf->b_p_eof;
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100945 buf->b_start_eol = buf->b_p_eol;
946 buf->b_start_bomb = buf->b_p_bomb;
947
Bram Moolenaarc667da52019-11-30 20:52:27 +0100948 // Only use free/alloc when necessary, they take time.
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100949 if (buf->b_start_fenc == NULL
950 || STRCMP(buf->b_start_fenc, buf->b_p_fenc) != 0)
951 {
952 vim_free(buf->b_start_fenc);
953 buf->b_start_fenc = vim_strsave(buf->b_p_fenc);
954 }
955}
956
957/*
958 * Return TRUE if 'fileformat' and/or 'fileencoding' has a different value
959 * from when editing started (save_file_ff() called).
960 * Also when 'endofline' was changed and 'binary' is set, or when 'bomb' was
961 * changed and 'binary' is not set.
962 * Also when 'endofline' was changed and 'fixeol' is not set.
963 * When "ignore_empty" is true don't consider a new, empty buffer to be
964 * changed.
965 */
966 int
967file_ff_differs(buf_T *buf, int ignore_empty)
968{
Bram Moolenaarc667da52019-11-30 20:52:27 +0100969 // In a buffer that was never loaded the options are not valid.
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100970 if (buf->b_flags & BF_NEVERLOADED)
971 return FALSE;
972 if (ignore_empty
973 && (buf->b_flags & BF_NEW)
974 && buf->b_ml.ml_line_count == 1
975 && *ml_get_buf(buf, (linenr_T)1, FALSE) == NUL)
976 return FALSE;
977 if (buf->b_start_ffc != *buf->b_p_ff)
978 return TRUE;
Bram Moolenaar15775372022-10-29 20:01:52 +0100979 if ((buf->b_p_bin || !buf->b_p_fixeol)
980 && (buf->b_start_eof != buf->b_p_eof
981 || buf->b_start_eol != buf->b_p_eol))
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100982 return TRUE;
983 if (!buf->b_p_bin && buf->b_start_bomb != buf->b_p_bomb)
984 return TRUE;
985 if (buf->b_start_fenc == NULL)
986 return (*buf->b_p_fenc != NUL);
987 return (STRCMP(buf->b_start_fenc, buf->b_p_fenc) != 0);
988}
989
990/*
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200991 * Insert string "p" at the cursor position. Stops at a NUL byte.
992 * Handles Replace mode and multi-byte characters.
993 */
994 void
995ins_bytes(char_u *p)
996{
997 ins_bytes_len(p, (int)STRLEN(p));
998}
999
1000/*
1001 * Insert string "p" with length "len" at the cursor position.
1002 * Handles Replace mode and multi-byte characters.
1003 */
1004 void
1005ins_bytes_len(char_u *p, int len)
1006{
1007 int i;
1008 int n;
1009
1010 if (has_mbyte)
1011 for (i = 0; i < len; i += n)
1012 {
1013 if (enc_utf8)
1014 // avoid reading past p[len]
1015 n = utfc_ptr2len_len(p + i, len - i);
1016 else
1017 n = (*mb_ptr2len)(p + i);
1018 ins_char_bytes(p + i, n);
1019 }
1020 else
1021 for (i = 0; i < len; ++i)
1022 ins_char(p[i]);
1023}
1024
1025/*
1026 * Insert or replace a single character at the cursor position.
Bram Moolenaar24959102022-05-07 20:01:16 +01001027 * When in MODE_REPLACE or MODE_VREPLACE state, replace any existing character.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001028 * Caller must have prepared for undo.
1029 * For multi-byte characters we get the whole character, the caller must
1030 * convert bytes to a character.
1031 */
1032 void
1033ins_char(int c)
1034{
1035 char_u buf[MB_MAXBYTES + 1];
1036 int n = (*mb_char2bytes)(c, buf);
1037
1038 // When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
1039 // Happens for CTRL-Vu9900.
1040 if (buf[0] == 0)
1041 buf[0] = '\n';
1042
1043 ins_char_bytes(buf, n);
1044}
1045
1046 void
1047ins_char_bytes(char_u *buf, int charlen)
1048{
1049 int c = buf[0];
1050 int newlen; // nr of bytes inserted
1051 int oldlen; // nr of bytes deleted (0 when not replacing)
1052 char_u *p;
1053 char_u *newp;
1054 char_u *oldp;
1055 int linelen; // length of old line including NUL
1056 colnr_T col;
1057 linenr_T lnum = curwin->w_cursor.lnum;
1058 int i;
1059
1060 // Break tabs if needed.
1061 if (virtual_active() && curwin->w_cursor.coladd > 0)
1062 coladvance_force(getviscol());
1063
1064 col = curwin->w_cursor.col;
1065 oldp = ml_get(lnum);
John Marriottbfcc8952024-03-11 22:04:45 +01001066 linelen = (int)ml_get_len(lnum) + 1;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001067
1068 // The lengths default to the values for when not replacing.
1069 oldlen = 0;
1070 newlen = charlen;
1071
1072 if (State & REPLACE_FLAG)
1073 {
1074 if (State & VREPLACE_FLAG)
1075 {
1076 colnr_T new_vcol = 0; // init for GCC
1077 colnr_T vcol;
1078 int old_list;
1079
1080 // Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
1081 // Returns the old value of list, so when finished,
1082 // curwin->w_p_list should be set back to this.
1083 old_list = curwin->w_p_list;
1084 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
1085 curwin->w_p_list = FALSE;
1086
1087 // In virtual replace mode each character may replace one or more
1088 // characters (zero if it's a TAB). Count the number of bytes to
1089 // be deleted to make room for the new character, counting screen
1090 // cells. May result in adding spaces to fill a gap.
1091 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
1092 new_vcol = vcol + chartabsize(buf, vcol);
1093 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
1094 {
1095 vcol += chartabsize(oldp + col + oldlen, vcol);
1096 // Don't need to remove a TAB that takes us to the right
1097 // position.
1098 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
1099 break;
1100 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
1101 // Deleted a bit too much, insert spaces.
1102 if (vcol > new_vcol)
1103 newlen += vcol - new_vcol;
1104 }
1105 curwin->w_p_list = old_list;
1106 }
1107 else if (oldp[col] != NUL)
1108 {
1109 // normal replace
1110 oldlen = (*mb_ptr2len)(oldp + col);
1111 }
1112
1113
1114 // Push the replaced bytes onto the replace stack, so that they can be
1115 // put back when BS is used. The bytes of a multi-byte character are
1116 // done the other way around, so that the first byte is popped off
1117 // first (it tells the byte length of the character).
1118 replace_push(NUL);
1119 for (i = 0; i < oldlen; ++i)
1120 {
1121 if (has_mbyte)
1122 i += replace_push_mb(oldp + col + i) - 1;
1123 else
1124 replace_push(oldp[col + i]);
1125 }
1126 }
1127
Bram Moolenaar964b3742019-05-24 18:54:09 +02001128 newp = alloc(linelen + newlen - oldlen);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001129 if (newp == NULL)
1130 return;
1131
1132 // Copy bytes before the cursor.
1133 if (col > 0)
1134 mch_memmove(newp, oldp, (size_t)col);
1135
1136 // Copy bytes after the changed character(s).
1137 p = newp + col;
1138 if (linelen > col + oldlen)
1139 mch_memmove(p + newlen, oldp + col + oldlen,
1140 (size_t)(linelen - col - oldlen));
1141
1142 // Insert or overwrite the new character.
1143 mch_memmove(p, buf, charlen);
1144 i = charlen;
1145
1146 // Fill with spaces when necessary.
1147 while (i < newlen)
1148 p[i++] = ' ';
1149
1150 // Replace the line in the buffer.
1151 ml_replace(lnum, newp, FALSE);
1152
1153 // mark the buffer as changed and prepare for displaying
LemonBoy0d534d92022-05-21 11:20:42 +01001154 changed_bytes(lnum, col);
1155#ifdef FEAT_PROP_POPUP
1156 if (curbuf->b_has_textprop && newlen != oldlen)
1157 adjust_prop_columns(lnum, col, newlen - oldlen,
1158 State & REPLACE_FLAG ? APC_SUBSTITUTE : 0);
1159#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001160
1161 // If we're in Insert or Replace mode and 'showmatch' is set, then briefly
1162 // show the match for right parens and braces.
Bram Moolenaar24959102022-05-07 20:01:16 +01001163 if (p_sm && (State & MODE_INSERT)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001164 && msg_silent == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001165 && !ins_compl_active())
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001166 {
1167 if (has_mbyte)
1168 showmatch(mb_ptr2char(buf));
1169 else
1170 showmatch(c);
1171 }
1172
1173#ifdef FEAT_RIGHTLEFT
1174 if (!p_ri || (State & REPLACE_FLAG))
1175#endif
1176 {
1177 // Normal insert: move cursor right
1178 curwin->w_cursor.col += charlen;
1179 }
1180
1181 // TODO: should try to update w_row here, to avoid recomputing it later.
1182}
1183
1184/*
1185 * Insert a string at the cursor position.
1186 * Note: Does NOT handle Replace mode.
1187 * Caller must have prepared for undo.
1188 */
1189 void
John Marriottf4b36412025-02-23 09:09:59 +01001190ins_str(char_u *s, size_t slen)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001191{
1192 char_u *oldp, *newp;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001193 int oldlen;
1194 colnr_T col;
1195 linenr_T lnum = curwin->w_cursor.lnum;
1196
1197 if (virtual_active() && curwin->w_cursor.coladd > 0)
1198 coladvance_force(getviscol());
1199
1200 col = curwin->w_cursor.col;
1201 oldp = ml_get(lnum);
John Marriottbfcc8952024-03-11 22:04:45 +01001202 oldlen = (int)ml_get_len(lnum);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001203
John Marriottf4b36412025-02-23 09:09:59 +01001204 newp = alloc(oldlen + slen + 1);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001205 if (newp == NULL)
1206 return;
1207 if (col > 0)
1208 mch_memmove(newp, oldp, (size_t)col);
John Marriottf4b36412025-02-23 09:09:59 +01001209 mch_memmove(newp + col, s, slen);
1210 mch_memmove(newp + col + slen, oldp + col, (size_t)(oldlen - col + 1));
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001211 ml_replace(lnum, newp, FALSE);
Yegappan Lakshmanan7b6add02025-04-01 20:38:37 +02001212 inserted_bytes(lnum, col, (int)slen);
1213 curwin->w_cursor.col += (colnr_T)slen;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001214}
1215
1216/*
1217 * Delete one character under the cursor.
1218 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
1219 * Caller must have prepared for undo.
1220 *
1221 * return FAIL for failure, OK otherwise
1222 */
1223 int
1224del_char(int fixpos)
1225{
1226 if (has_mbyte)
1227 {
1228 // Make sure the cursor is at the start of a character.
1229 mb_adjust_cursor();
1230 if (*ml_get_cursor() == NUL)
1231 return FAIL;
1232 return del_chars(1L, fixpos);
1233 }
1234 return del_bytes(1L, fixpos, TRUE);
1235}
1236
1237/*
1238 * Like del_bytes(), but delete characters instead of bytes.
1239 */
1240 int
1241del_chars(long count, int fixpos)
1242{
1243 long bytes = 0;
1244 long i;
1245 char_u *p;
1246 int l;
1247
1248 p = ml_get_cursor();
1249 for (i = 0; i < count && *p != NUL; ++i)
1250 {
1251 l = (*mb_ptr2len)(p);
1252 bytes += l;
1253 p += l;
1254 }
1255 return del_bytes(bytes, fixpos, TRUE);
1256}
1257
1258/*
1259 * Delete "count" bytes under the cursor.
1260 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
1261 * Caller must have prepared for undo.
1262 *
1263 * Return FAIL for failure, OK otherwise.
1264 */
1265 int
1266del_bytes(
1267 long count,
1268 int fixpos_arg,
1269 int use_delcombine UNUSED) // 'delcombine' option applies
1270{
1271 char_u *oldp, *newp;
1272 colnr_T oldlen;
1273 colnr_T newlen;
1274 linenr_T lnum = curwin->w_cursor.lnum;
1275 colnr_T col = curwin->w_cursor.col;
1276 int alloc_newp;
1277 long movelen;
1278 int fixpos = fixpos_arg;
1279
1280 oldp = ml_get(lnum);
John Marriottbfcc8952024-03-11 22:04:45 +01001281 oldlen = (int)ml_get_len(lnum);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001282
1283 // Can't do anything when the cursor is on the NUL after the line.
1284 if (col >= oldlen)
1285 return FAIL;
1286
1287 // If "count" is zero there is nothing to do.
1288 if (count == 0)
1289 return OK;
1290
1291 // If "count" is negative the caller must be doing something wrong.
1292 if (count < 1)
1293 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00001294 siemsg(e_invalid_count_for_del_bytes_nr, count);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001295 return FAIL;
1296 }
1297
1298 // If 'delcombine' is set and deleting (less than) one character, only
1299 // delete the last combining character.
1300 if (p_deco && use_delcombine && enc_utf8
1301 && utfc_ptr2len(oldp + col) >= count)
1302 {
1303 int cc[MAX_MCO];
1304 int n;
1305
1306 (void)utfc_ptr2char(oldp + col, cc);
1307 if (cc[0] != NUL)
1308 {
1309 // Find the last composing char, there can be several.
1310 n = col;
1311 do
1312 {
1313 col = n;
1314 count = utf_ptr2len(oldp + n);
1315 n += count;
1316 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
1317 fixpos = 0;
1318 }
1319 }
1320
1321 // When count is too big, reduce it.
1322 movelen = (long)oldlen - (long)col - count + 1; // includes trailing NUL
1323 if (movelen <= 1)
1324 {
1325 // If we just took off the last character of a non-blank line, and
1326 // fixpos is TRUE, we don't want to end up positioned at the NUL,
1327 // unless "restart_edit" is set or 'virtualedit' contains "onemore".
1328 if (col > 0 && fixpos && restart_edit == 0
Bram Moolenaar113d9de2022-08-08 15:49:18 +01001329 && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001330 {
1331 --curwin->w_cursor.col;
1332 curwin->w_cursor.coladd = 0;
1333 if (has_mbyte)
1334 curwin->w_cursor.col -=
1335 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
1336 }
1337 count = oldlen - col;
1338 movelen = 1;
1339 }
1340 newlen = oldlen - count;
1341
1342 // If the old line has been allocated the deletion can be done in the
1343 // existing line. Otherwise a new line has to be allocated
1344 // Can't do this when using Netbeans, because we would need to invoke
1345 // netbeans_removed(), which deallocates the line. Let ml_replace() take
1346 // care of notifying Netbeans.
1347#ifdef FEAT_NETBEANS_INTG
1348 if (netbeans_active())
1349 alloc_newp = TRUE;
1350 else
1351#endif
1352 alloc_newp = !ml_line_alloced(); // check if oldp was allocated
1353 if (!alloc_newp)
1354 newp = oldp; // use same allocated memory
1355 else
1356 { // need to allocate a new line
Bram Moolenaar964b3742019-05-24 18:54:09 +02001357 newp = alloc(newlen + 1);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001358 if (newp == NULL)
1359 return FAIL;
1360 mch_memmove(newp, oldp, (size_t)col);
1361 }
1362 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
1363 if (alloc_newp)
1364 ml_replace(lnum, newp, FALSE);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001365 else
1366 {
Christian Brabandt03acd472024-07-08 21:12:55 +02001367#ifdef FEAT_PROP_POPUP
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001368 // Also move any following text properties.
1369 if (oldlen + 1 < curbuf->b_ml.ml_line_len)
1370 mch_memmove(newp + newlen + 1, oldp + oldlen + 1,
1371 (size_t)curbuf->b_ml.ml_line_len - oldlen - 1);
Christian Brabandt03acd472024-07-08 21:12:55 +02001372#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001373 curbuf->b_ml.ml_line_len -= count;
zeertzjq82e079d2024-03-10 08:55:42 +01001374 curbuf->b_ml.ml_line_textlen = 0;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001375 }
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001376
1377 // mark the buffer as changed and prepare for displaying
Bram Moolenaar12f20032020-02-26 22:06:00 +01001378 inserted_bytes(lnum, col, -count);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001379
1380 return OK;
1381}
1382
1383/*
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001384 * open_line: Add a new line below or above the current line.
1385 *
Bram Moolenaar24959102022-05-07 20:01:16 +01001386 * For MODE_VREPLACE state, we only add a new line when we get to the end of
1387 * the file, otherwise we just start replacing the next line.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001388 *
Bram Moolenaar24959102022-05-07 20:01:16 +01001389 * Caller must take care of undo. Since MODE_VREPLACE may affect any number of
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001390 * lines however, it may call u_save_cursor() again when starting to change a
1391 * new line.
1392 * "flags": OPENLINE_DELSPACES delete spaces after cursor
1393 * OPENLINE_DO_COM format comments
1394 * OPENLINE_KEEPTRAIL keep trailing spaces
1395 * OPENLINE_MARKFIX adjust mark positions after the line break
1396 * OPENLINE_COM_LIST format comments with list or 2nd line indent
glepnir5090a1f2025-02-24 19:10:37 +01001397 * OPENLINE_FORCE_INDENT set indent from second_line_indent, ignore
1398 * 'autoindent'
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001399 *
1400 * "second_line_indent": indent for after ^^D in Insert mode or if flag
1401 * OPENLINE_COM_LIST
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001402 * "did_do_comment" is set to TRUE when intentionally putting the comment
dundargocc57b5bc2022-11-02 13:30:51 +00001403 * leader in front of the new line.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001404 *
1405 * Return OK for success, FAIL for failure
1406 */
1407 int
1408open_line(
1409 int dir, // FORWARD or BACKWARD
1410 int flags,
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001411 int second_line_indent,
1412 int *did_do_comment UNUSED)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001413{
1414 char_u *saved_line; // copy of the original line
1415 char_u *next_line = NULL; // copy of the next line
1416 char_u *p_extra = NULL; // what goes to next line
1417 int less_cols = 0; // less columns for mark in new line
LemonBoy788c06a2022-05-14 18:48:05 +01001418 int less_cols_off = 0; // columns to skip for mark and
1419 // textprop adjustment
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001420 pos_T old_cursor; // old cursor position
1421 int newcol = 0; // new cursor column
1422 int newindent = 0; // auto-indent of the new line
1423 int n;
1424 int trunc_line = FALSE; // truncate current line afterwards
1425 int retval = FAIL; // return value
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001426 int extra_len = 0; // length of p_extra string
1427 int lead_len; // length of comment leader
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001428 int comment_start = 0; // start index of the comment leader
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001429 char_u *lead_flags; // position in 'comments' for comment leader
1430 char_u *leader = NULL; // copy of comment leader
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001431 char_u *allocated = NULL; // allocated memory
1432 char_u *p;
1433 int saved_char = NUL; // init for GCC
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001434 pos_T *pos;
Bram Moolenaard2439e02021-12-12 19:10:44 +00001435 int do_cindent;
Bram Moolenaarde5cf282022-05-14 11:52:23 +01001436 int do_si = may_do_si();
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001437 int no_si = FALSE; // reset did_si afterwards
1438 int first_char = NUL; // init for GCC
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001439 int vreplace_mode;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001440 int did_append; // appended a new line
1441 int saved_pi = curbuf->b_p_pi; // copy of preserveindent setting
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01001442#ifdef FEAT_PROP_POPUP
1443 int at_eol; // cursor after last character
1444#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001445
1446 // make a copy of the current line so we can mess with it
John Marriottbfcc8952024-03-11 22:04:45 +01001447 saved_line = vim_strnsave(ml_get_curline(), ml_get_curline_len());
Bram Moolenaarc667da52019-11-30 20:52:27 +01001448 if (saved_line == NULL) // out of memory!
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001449 return FALSE;
1450
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01001451#ifdef FEAT_PROP_POPUP
John Marriottbfcc8952024-03-11 22:04:45 +01001452 at_eol = curwin->w_cursor.col >= (int)ml_get_curline_len();
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01001453#endif
1454
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001455 if (State & VREPLACE_FLAG)
1456 {
Bram Moolenaar24959102022-05-07 20:01:16 +01001457 // With MODE_VREPLACE we make a copy of the next line, which we will be
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001458 // starting to replace. First make the new line empty and let vim play
1459 // with the indenting and comment leader to its heart's content. Then
1460 // we grab what it ended up putting on the new line, put back the
1461 // original line, and call ins_char() to put each new character onto
1462 // the line, replacing what was there before and pushing the right
1463 // stuff onto the replace stack. -- webb.
1464 if (curwin->w_cursor.lnum < orig_line_count)
John Marriottbfcc8952024-03-11 22:04:45 +01001465 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 +02001466 else
1467 next_line = vim_strsave((char_u *)"");
1468 if (next_line == NULL) // out of memory!
1469 goto theend;
1470
Bram Moolenaar24959102022-05-07 20:01:16 +01001471 // In MODE_VREPLACE state, a NL replaces the rest of the line, and
1472 // starts replacing the next line, so push all of the characters left
1473 // on the line onto the replace stack. We'll push any other characters
1474 // that might be replaced at the start of the next line (due to
1475 // autoindent etc) a bit later.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001476 replace_push(NUL); // Call twice because BS over NL expects it
1477 replace_push(NUL);
1478 p = saved_line + curwin->w_cursor.col;
1479 while (*p != NUL)
1480 {
1481 if (has_mbyte)
1482 p += replace_push_mb(p);
1483 else
1484 replace_push(*p++);
1485 }
1486 saved_line[curwin->w_cursor.col] = NUL;
1487 }
1488
Bram Moolenaar24959102022-05-07 20:01:16 +01001489 if ((State & MODE_INSERT) && (State & VREPLACE_FLAG) == 0)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001490 {
1491 p_extra = saved_line + curwin->w_cursor.col;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001492 if (do_si) // need first char after new line break
1493 {
1494 p = skipwhite(p_extra);
1495 first_char = *p;
1496 }
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001497 extra_len = (int)STRLEN(p_extra);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001498 saved_char = *p_extra;
1499 *p_extra = NUL;
1500 }
1501
1502 u_clearline(); // cannot do "U" command when adding lines
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001503 did_si = FALSE;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001504 ai_col = 0;
1505
1506 // If we just did an auto-indent, then we didn't type anything on
1507 // the prior line, and it should be truncated. Do this even if 'ai' is not
1508 // set because automatically inserting a comment leader also sets did_ai.
1509 if (dir == FORWARD && did_ai)
1510 trunc_line = TRUE;
1511
glepnir5090a1f2025-02-24 19:10:37 +01001512 if ((flags & OPENLINE_FORCE_INDENT) && second_line_indent >= 0)
1513 newindent = second_line_indent;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001514 // If 'autoindent' and/or 'smartindent' is set, try to figure out what
1515 // indent to use for the new line.
glepnir5090a1f2025-02-24 19:10:37 +01001516 else if (curbuf->b_p_ai || do_si)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001517 {
1518 // count white space on current line
1519#ifdef FEAT_VARTABS
1520 newindent = get_indent_str_vtab(saved_line, curbuf->b_p_ts,
1521 curbuf->b_p_vts_array, FALSE);
1522#else
1523 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts, FALSE);
1524#endif
1525 if (newindent == 0 && !(flags & OPENLINE_COM_LIST))
1526 newindent = second_line_indent; // for ^^D command in insert mode
1527
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001528 // Do smart indenting.
1529 // In insert/replace mode (only when dir == FORWARD)
1530 // we may move some text to the next line. If it starts with '{'
1531 // don't add an indent. Fixes inserting a NL before '{' in line
1532 // "if (condition) {"
1533 if (!trunc_line && do_si && *saved_line != NUL
1534 && (p_extra == NULL || first_char != '{'))
1535 {
1536 char_u *ptr;
1537 char_u last_char;
1538
1539 old_cursor = curwin->w_cursor;
1540 ptr = saved_line;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001541 if (flags & OPENLINE_DO_COM)
1542 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
1543 else
1544 lead_len = 0;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001545 if (dir == FORWARD)
1546 {
1547 // Skip preprocessor directives, unless they are
1548 // recognised as comments.
Bram Moolenaar8c96af92019-09-28 19:05:57 +02001549 if ( lead_len == 0 && ptr[0] == '#')
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001550 {
1551 while (ptr[0] == '#' && curwin->w_cursor.lnum > 1)
1552 ptr = ml_get(--curwin->w_cursor.lnum);
1553 newindent = get_indent();
1554 }
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001555 if (flags & OPENLINE_DO_COM)
1556 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
1557 else
1558 lead_len = 0;
1559 if (lead_len > 0)
1560 {
1561 // This case gets the following right:
1562 // /*
1563 // * A comment (read '\' as '/').
1564 // */
1565 // #define IN_THE_WAY
1566 // This should line up here;
1567 p = skipwhite(ptr);
1568 if (p[0] == '/' && p[1] == '*')
1569 p++;
1570 if (p[0] == '*')
1571 {
1572 for (p++; *p; p++)
1573 {
1574 if (p[0] == '/' && p[-1] == '*')
1575 {
1576 // End of C comment, indent should line up
1577 // with the line containing the start of
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001578 // the comment.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001579 curwin->w_cursor.col = (colnr_T)(p - ptr);
1580 if ((pos = findmatch(NULL, NUL)) != NULL)
1581 {
1582 curwin->w_cursor.lnum = pos->lnum;
1583 newindent = get_indent();
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001584 break;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001585 }
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01001586 // this may make "ptr" invalid, get it again
1587 ptr = ml_get(curwin->w_cursor.lnum);
1588 p = ptr + curwin->w_cursor.col;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001589 }
1590 }
1591 }
1592 }
1593 else // Not a comment line
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001594 {
1595 // Find last non-blank in line
1596 p = ptr + STRLEN(ptr) - 1;
1597 while (p > ptr && VIM_ISWHITE(*p))
1598 --p;
1599 last_char = *p;
1600
1601 // find the character just before the '{' or ';'
1602 if (last_char == '{' || last_char == ';')
1603 {
1604 if (p > ptr)
1605 --p;
1606 while (p > ptr && VIM_ISWHITE(*p))
1607 --p;
1608 }
1609 // Try to catch lines that are split over multiple
1610 // lines. eg:
1611 // if (condition &&
1612 // condition) {
1613 // Should line up here!
1614 // }
1615 if (*p == ')')
1616 {
1617 curwin->w_cursor.col = (colnr_T)(p - ptr);
1618 if ((pos = findmatch(NULL, '(')) != NULL)
1619 {
1620 curwin->w_cursor.lnum = pos->lnum;
1621 newindent = get_indent();
1622 ptr = ml_get_curline();
1623 }
1624 }
1625 // If last character is '{' do indent, without
1626 // checking for "if" and the like.
1627 if (last_char == '{')
1628 {
1629 did_si = TRUE; // do indent
1630 no_si = TRUE; // don't delete it when '{' typed
1631 }
1632 // Look for "if" and the like, use 'cinwords'.
1633 // Don't do this if the previous line ended in ';' or
1634 // '}'.
1635 else if (last_char != ';' && last_char != '}'
1636 && cin_is_cinword(ptr))
1637 did_si = TRUE;
1638 }
1639 }
1640 else // dir == BACKWARD
1641 {
1642 // Skip preprocessor directives, unless they are
1643 // recognised as comments.
Bram Moolenaar8c96af92019-09-28 19:05:57 +02001644 if (lead_len == 0 && ptr[0] == '#')
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001645 {
1646 int was_backslashed = FALSE;
1647
1648 while ((ptr[0] == '#' || was_backslashed) &&
1649 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
1650 {
1651 if (*ptr && ptr[STRLEN(ptr) - 1] == '\\')
1652 was_backslashed = TRUE;
1653 else
1654 was_backslashed = FALSE;
1655 ptr = ml_get(++curwin->w_cursor.lnum);
1656 }
1657 if (was_backslashed)
1658 newindent = 0; // Got to end of file
1659 else
1660 newindent = get_indent();
1661 }
1662 p = skipwhite(ptr);
1663 if (*p == '}') // if line starts with '}': do indent
1664 did_si = TRUE;
1665 else // can delete indent when '{' typed
1666 can_si_back = TRUE;
1667 }
1668 curwin->w_cursor = old_cursor;
1669 }
1670 if (do_si)
1671 can_si = TRUE;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001672
1673 did_ai = TRUE;
1674 }
1675
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001676 // May do indenting after opening a new line.
1677 do_cindent = !p_paste && (curbuf->b_p_cin
Bram Moolenaar8e145b82022-05-21 20:17:31 +01001678#ifdef FEAT_EVAL
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001679 || *curbuf->b_p_inde != NUL
Bram Moolenaar8e145b82022-05-21 20:17:31 +01001680#endif
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001681 )
1682 && in_cinkeys(dir == FORWARD
1683 ? KEY_OPEN_FORW
glepnir34a7d822025-03-05 21:18:20 +01001684 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum))
1685 && !(flags & OPENLINE_FORCE_INDENT);
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001686
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001687 // Find out if the current line starts with a comment leader.
1688 // This may then be inserted in front of the new line.
1689 end_comment_pending = NUL;
1690 if (flags & OPENLINE_DO_COM)
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001691 {
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001692 lead_len = get_leader_len(saved_line, &lead_flags,
1693 dir == BACKWARD, TRUE);
Bram Moolenaar2bf875f2022-05-07 14:54:11 +01001694 if (lead_len == 0 && curbuf->b_p_cin && do_cindent && dir == FORWARD
Bram Moolenaar7e667782022-05-23 13:10:48 +01001695 && (!has_format_option(FO_NO_OPEN_COMS)
1696 || (flags & OPENLINE_FORMAT)))
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001697 {
Bram Moolenaar5ea5f372021-12-29 15:15:47 +00001698 // Check for a line comment after code.
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001699 comment_start = check_linecomment(saved_line);
1700 if (comment_start != MAXCOL)
1701 {
1702 lead_len = get_leader_len(saved_line + comment_start,
Bram Moolenaar5ea5f372021-12-29 15:15:47 +00001703 &lead_flags, FALSE, TRUE);
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001704 if (lead_len != 0)
1705 {
1706 lead_len += comment_start;
1707 if (did_do_comment != NULL)
1708 *did_do_comment = TRUE;
1709 }
1710 }
1711 }
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001712 }
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001713 else
1714 lead_len = 0;
1715 if (lead_len > 0)
1716 {
1717 char_u *lead_repl = NULL; // replaces comment leader
1718 int lead_repl_len = 0; // length of *lead_repl
1719 char_u lead_middle[COM_MAX_LEN]; // middle-comment string
1720 char_u lead_end[COM_MAX_LEN]; // end-comment string
1721 char_u *comment_end = NULL; // where lead_end has been found
1722 int extra_space = FALSE; // append extra space
1723 int current_flag;
1724 int require_blank = FALSE; // requires blank after middle
1725 char_u *p2;
1726
1727 // If the comment leader has the start, middle or end flag, it may not
1728 // be used or may be replaced with the middle leader.
1729 for (p = lead_flags; *p && *p != ':'; ++p)
1730 {
1731 if (*p == COM_BLANK)
1732 {
1733 require_blank = TRUE;
1734 continue;
1735 }
1736 if (*p == COM_START || *p == COM_MIDDLE)
1737 {
1738 current_flag = *p;
1739 if (*p == COM_START)
1740 {
1741 // Doing "O" on a start of comment does not insert leader.
1742 if (dir == BACKWARD)
1743 {
1744 lead_len = 0;
1745 break;
1746 }
1747
1748 // find start of middle part
1749 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
1750 require_blank = FALSE;
1751 }
1752
1753 // Isolate the strings of the middle and end leader.
Bram Moolenaarc667da52019-11-30 20:52:27 +01001754 while (*p && p[-1] != ':') // find end of middle flags
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001755 {
1756 if (*p == COM_BLANK)
1757 require_blank = TRUE;
1758 ++p;
1759 }
1760 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
1761
1762 while (*p && p[-1] != ':') // find end of end flags
1763 {
1764 // Check whether we allow automatic ending of comments
1765 if (*p == COM_AUTO_END)
1766 end_comment_pending = -1; // means we want to set it
1767 ++p;
1768 }
1769 n = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
1770
1771 if (end_comment_pending == -1) // we can set it now
1772 end_comment_pending = lead_end[n - 1];
1773
1774 // If the end of the comment is in the same line, don't use
1775 // the comment leader.
1776 if (dir == FORWARD)
1777 {
1778 for (p = saved_line + lead_len; *p; ++p)
1779 if (STRNCMP(p, lead_end, n) == 0)
1780 {
1781 comment_end = p;
1782 lead_len = 0;
1783 break;
1784 }
1785 }
1786
1787 // Doing "o" on a start of comment inserts the middle leader.
1788 if (lead_len > 0)
1789 {
1790 if (current_flag == COM_START)
1791 {
1792 lead_repl = lead_middle;
1793 lead_repl_len = (int)STRLEN(lead_middle);
1794 }
1795
1796 // If we have hit RETURN immediately after the start
1797 // comment leader, then put a space after the middle
1798 // comment leader on the next line.
1799 if (!VIM_ISWHITE(saved_line[lead_len - 1])
1800 && ((p_extra != NULL
1801 && (int)curwin->w_cursor.col == lead_len)
1802 || (p_extra == NULL
1803 && saved_line[lead_len] == NUL)
1804 || require_blank))
1805 extra_space = TRUE;
1806 }
1807 break;
1808 }
1809 if (*p == COM_END)
1810 {
1811 // Doing "o" on the end of a comment does not insert leader.
1812 // Remember where the end is, might want to use it to find the
1813 // start (for C-comments).
1814 if (dir == FORWARD)
1815 {
1816 comment_end = skipwhite(saved_line);
1817 lead_len = 0;
1818 break;
1819 }
1820
1821 // Doing "O" on the end of a comment inserts the middle leader.
1822 // Find the string for the middle leader, searching backwards.
1823 while (p > curbuf->b_p_com && *p != ',')
1824 --p;
1825 for (lead_repl = p; lead_repl > curbuf->b_p_com
1826 && lead_repl[-1] != ':'; --lead_repl)
1827 ;
1828 lead_repl_len = (int)(p - lead_repl);
1829
1830 // We can probably always add an extra space when doing "O" on
1831 // the comment-end
1832 extra_space = TRUE;
1833
1834 // Check whether we allow automatic ending of comments
1835 for (p2 = p; *p2 && *p2 != ':'; p2++)
1836 {
1837 if (*p2 == COM_AUTO_END)
1838 end_comment_pending = -1; // means we want to set it
1839 }
1840 if (end_comment_pending == -1)
1841 {
1842 // Find last character in end-comment string
1843 while (*p2 && *p2 != ',')
1844 p2++;
1845 end_comment_pending = p2[-1];
1846 }
1847 break;
1848 }
1849 if (*p == COM_FIRST)
1850 {
1851 // Comment leader for first line only: Don't repeat leader
1852 // when using "O", blank out leader when using "o".
1853 if (dir == BACKWARD)
1854 lead_len = 0;
1855 else
1856 {
1857 lead_repl = (char_u *)"";
1858 lead_repl_len = 0;
1859 }
1860 break;
1861 }
1862 }
1863 if (lead_len)
1864 {
1865 // allocate buffer (may concatenate p_extra later)
1866 leader = alloc(lead_len + lead_repl_len + extra_space + extra_len
1867 + (second_line_indent > 0 ? second_line_indent : 0) + 1);
1868 allocated = leader; // remember to free it later
1869
1870 if (leader == NULL)
1871 lead_len = 0;
1872 else
1873 {
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001874 int li;
1875
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001876 vim_strncpy(leader, saved_line, lead_len);
1877
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00001878 // TODO: handle multi-byte and double width chars
1879 for (li = 0; li < comment_start; ++li)
1880 if (!VIM_ISWHITE(leader[li]))
1881 leader[li] = ' ';
1882
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001883 // Replace leader with lead_repl, right or left adjusted
1884 if (lead_repl != NULL)
1885 {
1886 int c = 0;
1887 int off = 0;
1888
1889 for (p = lead_flags; *p != NUL && *p != ':'; )
1890 {
1891 if (*p == COM_RIGHT || *p == COM_LEFT)
1892 c = *p++;
1893 else if (VIM_ISDIGIT(*p) || *p == '-')
1894 off = getdigits(&p);
1895 else
1896 ++p;
1897 }
1898 if (c == COM_RIGHT) // right adjusted leader
1899 {
1900 // find last non-white in the leader to line up with
1901 for (p = leader + lead_len - 1; p > leader
1902 && VIM_ISWHITE(*p); --p)
1903 ;
1904 ++p;
1905
1906 // Compute the length of the replaced characters in
1907 // screen characters, not bytes.
1908 {
1909 int repl_size = vim_strnsize(lead_repl,
1910 lead_repl_len);
1911 int old_size = 0;
1912 char_u *endp = p;
1913 int l;
1914
1915 while (old_size < repl_size && p > leader)
1916 {
1917 MB_PTR_BACK(leader, p);
1918 old_size += ptr2cells(p);
1919 }
1920 l = lead_repl_len - (int)(endp - p);
1921 if (l != 0)
1922 mch_memmove(endp + l, endp,
1923 (size_t)((leader + lead_len) - endp));
1924 lead_len += l;
1925 }
1926 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1927 if (p + lead_repl_len > leader + lead_len)
1928 p[lead_repl_len] = NUL;
1929
1930 // blank-out any other chars from the old leader.
1931 while (--p >= leader)
1932 {
1933 int l = mb_head_off(leader, p);
1934
1935 if (l > 1)
1936 {
1937 p -= l;
1938 if (ptr2cells(p) > 1)
1939 {
1940 p[1] = ' ';
1941 --l;
1942 }
1943 mch_memmove(p + 1, p + l + 1,
1944 (size_t)((leader + lead_len) - (p + l + 1)));
1945 lead_len -= l;
1946 *p = ' ';
1947 }
1948 else if (!VIM_ISWHITE(*p))
1949 *p = ' ';
1950 }
1951 }
1952 else // left adjusted leader
1953 {
1954 p = skipwhite(leader);
1955
1956 // Compute the length of the replaced characters in
1957 // screen characters, not bytes. Move the part that is
1958 // not to be overwritten.
1959 {
1960 int repl_size = vim_strnsize(lead_repl,
1961 lead_repl_len);
1962 int i;
1963 int l;
1964
1965 for (i = 0; i < lead_len && p[i] != NUL; i += l)
1966 {
1967 l = (*mb_ptr2len)(p + i);
1968 if (vim_strnsize(p, i + l) > repl_size)
1969 break;
1970 }
1971 if (i != lead_repl_len)
1972 {
1973 mch_memmove(p + lead_repl_len, p + i,
1974 (size_t)(lead_len - i - (p - leader)));
1975 lead_len += lead_repl_len - i;
1976 }
1977 }
1978 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1979
1980 // Replace any remaining non-white chars in the old
1981 // leader by spaces. Keep Tabs, the indent must
1982 // remain the same.
1983 for (p += lead_repl_len; p < leader + lead_len; ++p)
1984 if (!VIM_ISWHITE(*p))
1985 {
1986 // Don't put a space before a TAB.
1987 if (p + 1 < leader + lead_len && p[1] == TAB)
1988 {
1989 --lead_len;
1990 mch_memmove(p, p + 1,
1991 (leader + lead_len) - p);
1992 }
1993 else
1994 {
1995 int l = (*mb_ptr2len)(p);
1996
1997 if (l > 1)
1998 {
1999 if (ptr2cells(p) > 1)
2000 {
2001 // Replace a double-wide char with
2002 // two spaces
2003 --l;
2004 *p++ = ' ';
2005 }
2006 mch_memmove(p + 1, p + l,
2007 (leader + lead_len) - p);
2008 lead_len -= l - 1;
2009 }
2010 *p = ' ';
2011 }
2012 }
2013 *p = NUL;
2014 }
2015
2016 // Recompute the indent, it may have changed.
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002017 if (curbuf->b_p_ai || do_si)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002018#ifdef FEAT_VARTABS
2019 newindent = get_indent_str_vtab(leader, curbuf->b_p_ts,
2020 curbuf->b_p_vts_array, FALSE);
2021#else
2022 newindent = get_indent_str(leader,
2023 (int)curbuf->b_p_ts, FALSE);
2024#endif
2025
2026 // Add the indent offset
2027 if (newindent + off < 0)
2028 {
2029 off = -newindent;
2030 newindent = 0;
2031 }
2032 else
2033 newindent += off;
2034
2035 // Correct trailing spaces for the shift, so that
2036 // alignment remains equal.
2037 while (off > 0 && lead_len > 0
2038 && leader[lead_len - 1] == ' ')
2039 {
2040 // Don't do it when there is a tab before the space
2041 if (vim_strchr(skipwhite(leader), '\t') != NULL)
2042 break;
2043 --lead_len;
2044 --off;
2045 }
2046
2047 // If the leader ends in white space, don't add an
2048 // extra space
2049 if (lead_len > 0 && VIM_ISWHITE(leader[lead_len - 1]))
2050 extra_space = FALSE;
2051 leader[lead_len] = NUL;
2052 }
2053
2054 if (extra_space)
2055 {
2056 leader[lead_len++] = ' ';
2057 leader[lead_len] = NUL;
2058 }
2059
2060 newcol = lead_len;
2061
2062 // if a new indent will be set below, remove the indent that
2063 // is in the comment leader
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002064 if (newindent || did_si)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002065 {
2066 while (lead_len && VIM_ISWHITE(*leader))
2067 {
2068 --lead_len;
2069 --newcol;
2070 ++leader;
2071 }
2072 }
2073
2074 }
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002075 did_si = can_si = FALSE;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002076 }
2077 else if (comment_end != NULL)
2078 {
2079 // We have finished a comment, so we don't use the leader.
2080 // If this was a C-comment and 'ai' or 'si' is set do a normal
2081 // indent to align with the line containing the start of the
2082 // comment.
2083 if (comment_end[0] == '*' && comment_end[1] == '/' &&
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002084 (curbuf->b_p_ai || do_si))
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002085 {
2086 old_cursor = curwin->w_cursor;
2087 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line);
2088 if ((pos = findmatch(NULL, NUL)) != NULL)
2089 {
2090 curwin->w_cursor.lnum = pos->lnum;
2091 newindent = get_indent();
2092 }
2093 curwin->w_cursor = old_cursor;
2094 }
2095 }
2096 }
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002097
Bram Moolenaar24959102022-05-07 20:01:16 +01002098 // (State == MODE_INSERT || State == MODE_REPLACE), only when dir == FORWARD
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002099 if (p_extra != NULL)
2100 {
2101 *p_extra = saved_char; // restore char that NUL replaced
2102
2103 // When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first
2104 // non-blank.
2105 //
Bram Moolenaar24959102022-05-07 20:01:16 +01002106 // When in MODE_REPLACE state, put the deleted blanks on the replace
2107 // stack, preceded by a NUL, so they can be put back when a BS is
2108 // entered.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002109 if (REPLACE_NORMAL(State))
Bram Moolenaarc667da52019-11-30 20:52:27 +01002110 replace_push(NUL); // end of extra blanks
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002111 if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES))
2112 {
2113 while ((*p_extra == ' ' || *p_extra == '\t')
2114 && (!enc_utf8
2115 || !utf_iscomposing(utf_ptr2char(p_extra + 1))))
2116 {
2117 if (REPLACE_NORMAL(State))
2118 replace_push(*p_extra);
2119 ++p_extra;
2120 ++less_cols_off;
2121 }
2122 }
2123
2124 // columns for marks adjusted for removed columns
2125 less_cols = (int)(p_extra - saved_line);
2126 }
2127
2128 if (p_extra == NULL)
2129 p_extra = (char_u *)""; // append empty line
2130
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002131 // concatenate leader and p_extra, if there is a leader
2132 if (lead_len)
2133 {
2134 if (flags & OPENLINE_COM_LIST && second_line_indent > 0)
2135 {
2136 int i;
2137 int padding = second_line_indent
2138 - (newindent + (int)STRLEN(leader));
2139
2140 // Here whitespace is inserted after the comment char.
2141 // Below, set_indent(newindent, SIN_INSERT) will insert the
2142 // whitespace needed before the comment char.
2143 for (i = 0; i < padding; i++)
2144 {
2145 STRCAT(leader, " ");
2146 less_cols--;
2147 newcol++;
2148 }
2149 }
2150 STRCAT(leader, p_extra);
2151 p_extra = leader;
2152 did_ai = TRUE; // So truncating blanks works with comments
2153 less_cols -= lead_len;
2154 }
2155 else
2156 end_comment_pending = NUL; // turns out there was no leader
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002157
2158 old_cursor = curwin->w_cursor;
2159 if (dir == BACKWARD)
2160 --curwin->w_cursor.lnum;
2161 if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count)
2162 {
2163 if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE)
2164 == FAIL)
2165 goto theend;
2166 // Postpone calling changed_lines(), because it would mess up folding
2167 // with markers.
Brandon Simmonsda3dd7d2023-01-17 19:48:07 +00002168 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002169 did_append = TRUE;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002170#ifdef FEAT_PROP_POPUP
Bram Moolenaar24959102022-05-07 20:01:16 +01002171 if ((State & MODE_INSERT) && (State & VREPLACE_FLAG) == 0)
LemonBoy788c06a2022-05-14 18:48:05 +01002172 // Properties after the split move to the next line.
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002173 adjust_props_for_split(curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaarebd0e8b2022-09-14 22:13:59 +01002174 curwin->w_cursor.col + 1, 0, at_eol);
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002175#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002176 }
2177 else
2178 {
Bram Moolenaar24959102022-05-07 20:01:16 +01002179 // In MODE_VREPLACE state we are starting to replace the next line.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002180 curwin->w_cursor.lnum++;
2181 if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed)
2182 {
2183 // In case we NL to a new line, BS to the previous one, and NL
2184 // again, we don't want to save the new line for undo twice.
Bram Moolenaarc667da52019-11-30 20:52:27 +01002185 (void)u_save_cursor(); // errors are ignored!
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002186 vr_lines_changed++;
2187 }
2188 ml_replace(curwin->w_cursor.lnum, p_extra, TRUE);
2189 changed_bytes(curwin->w_cursor.lnum, 0);
2190 curwin->w_cursor.lnum--;
2191 did_append = FALSE;
2192 }
2193
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002194 if (newindent || did_si)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002195 {
2196 ++curwin->w_cursor.lnum;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002197 if (did_si)
2198 {
2199 int sw = (int)get_sw_value(curbuf);
2200
2201 if (p_sr)
2202 newindent -= newindent % sw;
2203 newindent += sw;
2204 }
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002205 // Copy the indent
2206 if (curbuf->b_p_ci)
2207 {
2208 (void)copy_indent(newindent, saved_line);
2209
2210 // Set the 'preserveindent' option so that any further screwing
2211 // with the line doesn't entirely destroy our efforts to preserve
2212 // it. It gets restored at the function end.
2213 curbuf->b_p_pi = TRUE;
2214 }
2215 else
2216 (void)set_indent(newindent, SIN_INSERT);
2217 less_cols -= curwin->w_cursor.col;
2218
2219 ai_col = curwin->w_cursor.col;
2220
Bram Moolenaar24959102022-05-07 20:01:16 +01002221 // In MODE_REPLACE state, for each character in the new indent, there
2222 // must be a NUL on the replace stack, for when it is deleted with BS
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002223 if (REPLACE_NORMAL(State))
2224 for (n = 0; n < (int)curwin->w_cursor.col; ++n)
2225 replace_push(NUL);
2226 newcol += curwin->w_cursor.col;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002227 if (no_si)
2228 did_si = FALSE;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002229 }
2230
Bram Moolenaar24959102022-05-07 20:01:16 +01002231 // In MODE_REPLACE state, for each character in the extra leader, there
2232 // must be a NUL on the replace stack, for when it is deleted with BS.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002233 if (REPLACE_NORMAL(State))
2234 while (lead_len-- > 0)
2235 replace_push(NUL);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002236
2237 curwin->w_cursor = old_cursor;
2238
2239 if (dir == FORWARD)
2240 {
Bram Moolenaar24959102022-05-07 20:01:16 +01002241 if (trunc_line || (State & MODE_INSERT))
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002242 {
2243 // truncate current line at cursor
2244 saved_line[curwin->w_cursor.col] = NUL;
2245 // Remove trailing white space, unless OPENLINE_KEEPTRAIL used.
2246 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL))
John Marriott34954972025-03-16 20:49:52 +01002247 truncate_spaces(saved_line, curwin->w_cursor.col);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002248 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE);
2249 saved_line = NULL;
2250 if (did_append)
2251 {
2252 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
2253 curwin->w_cursor.lnum + 1, 1L);
2254 did_append = FALSE;
2255
2256 // Move marks after the line break to the new line.
2257 if (flags & OPENLINE_MARKFIX)
2258 mark_col_adjust(curwin->w_cursor.lnum,
2259 curwin->w_cursor.col + less_cols_off,
2260 1L, (long)-less_cols, 0);
LemonBoy788c06a2022-05-14 18:48:05 +01002261#ifdef FEAT_PROP_POPUP
2262 // Keep into account the deleted blanks on the new line.
2263 if (curbuf->b_has_textprop && less_cols_off != 0)
2264 adjust_prop_columns(curwin->w_cursor.lnum + 1, 0,
2265 -less_cols_off, 0);
2266#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002267 }
2268 else
2269 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
2270 }
2271
2272 // Put the cursor on the new line. Careful: the scrollup() above may
2273 // have moved w_cursor, we must use old_cursor.
2274 curwin->w_cursor.lnum = old_cursor.lnum + 1;
2275 }
2276 if (did_append)
2277 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L);
2278
2279 curwin->w_cursor.col = newcol;
2280 curwin->w_cursor.coladd = 0;
2281
Bram Moolenaar24959102022-05-07 20:01:16 +01002282 // In MODE_VREPLACE state, we are handling the replace stack ourselves, so
2283 // stop fixthisline() from doing it (via change_indent()) by telling it
2284 // we're in normal MODE_INSERT state.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002285 if (State & VREPLACE_FLAG)
2286 {
2287 vreplace_mode = State; // So we know to put things right later
Bram Moolenaar24959102022-05-07 20:01:16 +01002288 State = MODE_INSERT;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002289 }
2290 else
2291 vreplace_mode = 0;
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002292
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002293 if (!p_paste)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002294 {
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002295 if (leader == NULL
2296 && !use_indentexpr_for_lisp()
2297 && curbuf->b_p_lisp
2298 && curbuf->b_p_ai)
2299 {
2300 // do lisp indenting
2301 fixthisline(get_lisp_indent);
2302 ai_col = (colnr_T)getwhitecols_curline();
2303 }
2304 else if (do_cindent || (curbuf->b_p_ai && use_indentexpr_for_lisp()))
2305 {
2306 // do 'cindent' or 'indentexpr' indenting
2307 do_c_expr_indent();
2308 ai_col = (colnr_T)getwhitecols_curline();
2309 }
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002310 }
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002311
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002312 if (vreplace_mode != 0)
2313 State = vreplace_mode;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002314
Bram Moolenaar24959102022-05-07 20:01:16 +01002315 // Finally, MODE_VREPLACE gets the stuff on the new line, then puts back
2316 // the original line, and inserts the new stuff char by char, pushing old
2317 // stuff onto the replace stack (via ins_char()).
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002318 if (State & VREPLACE_FLAG)
2319 {
2320 // Put new line in p_extra
John Marriottbfcc8952024-03-11 22:04:45 +01002321 p_extra = vim_strnsave(ml_get_curline(), ml_get_curline_len());
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002322 if (p_extra == NULL)
2323 goto theend;
2324
2325 // Put back original line
2326 ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
2327
2328 // Insert new stuff into line again
2329 curwin->w_cursor.col = 0;
2330 curwin->w_cursor.coladd = 0;
2331 ins_bytes(p_extra); // will call changed_bytes()
2332 vim_free(p_extra);
2333 next_line = NULL;
2334 }
2335
2336 retval = OK; // success!
2337theend:
2338 curbuf->b_p_pi = saved_pi;
2339 vim_free(saved_line);
2340 vim_free(next_line);
2341 vim_free(allocated);
2342 return retval;
2343}
2344
2345/*
2346 * Delete from cursor to end of line.
2347 * Caller must have prepared for undo.
2348 * If "fixpos" is TRUE fix the cursor position when done.
2349 *
2350 * Return FAIL for failure, OK otherwise.
2351 */
2352 int
2353truncate_line(int fixpos)
2354{
2355 char_u *newp;
2356 linenr_T lnum = curwin->w_cursor.lnum;
2357 colnr_T col = curwin->w_cursor.col;
LemonBoyd0b1a092022-05-12 18:45:18 +01002358 char_u *old_line;
2359 int deleted;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002360
LemonBoyd0b1a092022-05-12 18:45:18 +01002361 old_line = ml_get(lnum);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002362 if (col == 0)
2363 newp = vim_strsave((char_u *)"");
2364 else
LemonBoyd0b1a092022-05-12 18:45:18 +01002365 newp = vim_strnsave(old_line, col);
John Marriottbfcc8952024-03-11 22:04:45 +01002366 deleted = (int)ml_get_len(lnum) - col;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002367
2368 if (newp == NULL)
2369 return FAIL;
2370
2371 ml_replace(lnum, newp, FALSE);
2372
2373 // mark the buffer as changed and prepare for displaying
LemonBoyd0b1a092022-05-12 18:45:18 +01002374 inserted_bytes(lnum, curwin->w_cursor.col, -deleted);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002375
2376 // If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2377 if (fixpos && curwin->w_cursor.col > 0)
2378 --curwin->w_cursor.col;
2379
2380 return OK;
2381}
2382
2383/*
2384 * Delete "nlines" lines at the cursor.
2385 * Saves the lines for undo first if "undo" is TRUE.
2386 */
2387 void
2388del_lines(long nlines, int undo)
2389{
2390 long n;
2391 linenr_T first = curwin->w_cursor.lnum;
2392
2393 if (nlines <= 0)
2394 return;
2395
2396 // save the deleted lines for undo
2397 if (undo && u_savedel(first, nlines) == FAIL)
2398 return;
2399
2400 for (n = 0; n < nlines; )
2401 {
2402 if (curbuf->b_ml.ml_flags & ML_EMPTY) // nothing to delete
2403 break;
2404
Bram Moolenaarca70c072020-05-30 20:30:46 +02002405 ml_delete_flags(first, ML_DEL_MESSAGE);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002406 ++n;
2407
2408 // If we delete the last line in the file, stop
2409 if (first > curbuf->b_ml.ml_line_count)
2410 break;
2411 }
2412
2413 // Correct the cursor position before calling deleted_lines_mark(), it may
2414 // trigger a callback to display the cursor.
2415 curwin->w_cursor.col = 0;
2416 check_cursor_lnum();
2417
2418 // adjust marks, mark the buffer as changed and prepare for displaying
2419 deleted_lines_mark(first, n);
2420}