blob: d0462dbe1cdd27f57028859f409d19ddd8b363a4 [file] [log] [blame]
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * change.c: functions related to changing text
12 */
13
14#include "vim.h"
15
16/*
17 * If the file is readonly, give a warning message with the first change.
18 * Don't do this for autocommands.
19 * Doesn't use emsg(), because it flushes the macro buffer.
20 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
21 * will be TRUE.
22 * "col" is the column for the message; non-zero when in insert mode and
23 * 'showmode' is on.
24 * Careful: may trigger autocommands that reload the buffer.
25 */
26 void
27change_warning(int col)
28{
29 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
30
31 if (curbuf->b_did_warn == FALSE
32 && curbufIsChanged() == 0
33 && !autocmd_busy
34 && curbuf->b_p_ro)
35 {
36 ++curbuf_lock;
37 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
38 --curbuf_lock;
39 if (!curbuf->b_p_ro)
40 return;
41
42 // Do what msg() does, but with a column offset if the warning should
43 // be after the mode message.
44 msg_start();
45 if (msg_row == Rows - 1)
46 msg_col = col;
47 msg_source(HL_ATTR(HLF_W));
48 msg_puts_attr(_(w_readonly), HL_ATTR(HLF_W) | MSG_HIST);
49#ifdef FEAT_EVAL
50 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
51#endif
52 msg_clr_eos();
53 (void)msg_end();
54 if (msg_silent == 0 && !silent_mode
55#ifdef FEAT_EVAL
56 && time_for_testing != 1
57#endif
58 )
59 {
60 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +010061 ui_delay(1002L, TRUE); // give the user time to think about it
Bram Moolenaar3f86ca02019-05-11 18:30:00 +020062 }
63 curbuf->b_did_warn = TRUE;
64 redraw_cmdline = FALSE; // don't redraw and erase the message
65 if (msg_row < Rows - 1)
66 showmode();
67 }
68}
69
70/*
71 * Call this function when something in the current buffer is changed.
72 *
73 * Most often called through changed_bytes() and changed_lines(), which also
74 * mark the area of the display to be redrawn.
75 *
76 * Careful: may trigger autocommands that reload the buffer.
77 */
78 void
79changed(void)
80{
81#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
82 if (p_imst == IM_ON_THE_SPOT)
83 {
84 // The text of the preediting area is inserted, but this doesn't
85 // mean a change of the buffer yet. That is delayed until the
86 // text is committed. (this means preedit becomes empty)
87 if (im_is_preediting() && !xim_changed_while_preediting)
88 return;
89 xim_changed_while_preediting = FALSE;
90 }
91#endif
92
93 if (!curbuf->b_changed)
94 {
95 int save_msg_scroll = msg_scroll;
96
97 // Give a warning about changing a read-only file. This may also
98 // check-out the file, thus change "curbuf"!
99 change_warning(0);
100
101 // Create a swap file if that is wanted.
102 // Don't do this for "nofile" and "nowrite" buffer types.
103 if (curbuf->b_may_swap
104#ifdef FEAT_QUICKFIX
105 && !bt_dontwrite(curbuf)
106#endif
107 )
108 {
109 int save_need_wait_return = need_wait_return;
110
111 need_wait_return = FALSE;
112 ml_open_file(curbuf);
113
114 // The ml_open_file() can cause an ATTENTION message.
115 // Wait two seconds, to make sure the user reads this unexpected
116 // message. Since we could be anywhere, call wait_return() now,
117 // and don't let the emsg() set msg_scroll.
118 if (need_wait_return && emsg_silent == 0)
119 {
120 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +0100121 ui_delay(2002L, TRUE);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200122 wait_return(TRUE);
123 msg_scroll = save_msg_scroll;
124 }
125 else
126 need_wait_return = save_need_wait_return;
127 }
128 changed_internal();
129 }
130 ++CHANGEDTICK(curbuf);
131
132#ifdef FEAT_SEARCH_EXTRA
133 // If a pattern is highlighted, the position may now be invalid.
134 highlight_match = FALSE;
135#endif
136}
137
138/*
139 * Internal part of changed(), no user interaction.
140 * Also used for recovery.
141 */
142 void
143changed_internal(void)
144{
145 curbuf->b_changed = TRUE;
146 ml_setflags(curbuf);
147 check_status(curbuf);
148 redraw_tabline = TRUE;
149#ifdef FEAT_TITLE
150 need_maketitle = TRUE; // set window title later
151#endif
152}
153
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200154#ifdef FEAT_EVAL
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200155static long next_listener_id = 0;
156
157/*
Bram Moolenaar4a0a1612019-07-17 22:00:19 +0200158 * Check if the change at "lnum" is above or overlaps with an existing
159 * change. If above then flush changes and invoke listeners.
Bram Moolenaardda41442019-05-16 22:11:47 +0200160 * Returns TRUE if the change was merged.
161 */
162 static int
163check_recorded_changes(
164 buf_T *buf,
165 linenr_T lnum,
Bram Moolenaardda41442019-05-16 22:11:47 +0200166 linenr_T lnume,
Bram Moolenaar4a0a1612019-07-17 22:00:19 +0200167 long xtra)
Bram Moolenaardda41442019-05-16 22:11:47 +0200168{
169 if (buf->b_recorded_changes != NULL && xtra != 0)
170 {
171 listitem_T *li;
Bram Moolenaar7b31a182019-05-24 21:39:27 +0200172 linenr_T prev_lnum;
173 linenr_T prev_lnume;
Bram Moolenaardda41442019-05-16 22:11:47 +0200174
175 for (li = buf->b_recorded_changes->lv_first; li != NULL;
176 li = li->li_next)
177 {
Bram Moolenaar7b31a182019-05-24 21:39:27 +0200178 prev_lnum = (linenr_T)dict_get_number(
Bram Moolenaardda41442019-05-16 22:11:47 +0200179 li->li_tv.vval.v_dict, (char_u *)"lnum");
Bram Moolenaar7b31a182019-05-24 21:39:27 +0200180 prev_lnume = (linenr_T)dict_get_number(
181 li->li_tv.vval.v_dict, (char_u *)"end");
Bram Moolenaar4a0a1612019-07-17 22:00:19 +0200182 if (prev_lnum >= lnum || prev_lnum > lnume || prev_lnume >= lnum)
Bram Moolenaardda41442019-05-16 22:11:47 +0200183 {
Bram Moolenaar4a0a1612019-07-17 22:00:19 +0200184 // the current change is going to make the line number in
185 // the older change invalid, flush now
186 invoke_listeners(curbuf);
187 break;
Bram Moolenaardda41442019-05-16 22:11:47 +0200188 }
189 }
190 }
191 return FALSE;
192}
193
194/*
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200195 * Record a change for listeners added with listener_add().
Bram Moolenaardda41442019-05-16 22:11:47 +0200196 * Always for the current buffer.
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200197 */
198 static void
199may_record_change(
200 linenr_T lnum,
201 colnr_T col,
202 linenr_T lnume,
203 long xtra)
204{
205 dict_T *dict;
206
207 if (curbuf->b_listener == NULL)
208 return;
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200209
210 // If the new change is going to change the line numbers in already listed
211 // changes, then flush.
Bram Moolenaar4a0a1612019-07-17 22:00:19 +0200212 if (check_recorded_changes(curbuf, lnum, lnume, xtra))
Bram Moolenaardda41442019-05-16 22:11:47 +0200213 return;
214
215 if (curbuf->b_recorded_changes == NULL)
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200216 {
Bram Moolenaardda41442019-05-16 22:11:47 +0200217 curbuf->b_recorded_changes = list_alloc();
218 if (curbuf->b_recorded_changes == NULL) // out of memory
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200219 return;
Bram Moolenaardda41442019-05-16 22:11:47 +0200220 ++curbuf->b_recorded_changes->lv_refcount;
221 curbuf->b_recorded_changes->lv_lock = VAR_FIXED;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200222 }
223
224 dict = dict_alloc();
225 if (dict == NULL)
226 return;
227 dict_add_number(dict, "lnum", (varnumber_T)lnum);
228 dict_add_number(dict, "end", (varnumber_T)lnume);
229 dict_add_number(dict, "added", (varnumber_T)xtra);
Bram Moolenaara3347722019-05-11 21:14:24 +0200230 dict_add_number(dict, "col", (varnumber_T)col + 1);
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200231
Bram Moolenaardda41442019-05-16 22:11:47 +0200232 list_append_dict(curbuf->b_recorded_changes, dict);
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200233}
234
235/*
236 * listener_add() function
237 */
238 void
239f_listener_add(typval_T *argvars, typval_T *rettv)
240{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200241 callback_T callback;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200242 listener_T *lnr;
Bram Moolenaara3347722019-05-11 21:14:24 +0200243 buf_T *buf = curbuf;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200244
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200245 callback = get_callback(&argvars[0]);
246 if (callback.cb_name == NULL)
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200247 return;
248
Bram Moolenaara3347722019-05-11 21:14:24 +0200249 if (argvars[1].v_type != VAR_UNKNOWN)
250 {
251 buf = get_buf_arg(&argvars[1]);
252 if (buf == NULL)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200253 {
254 free_callback(&callback);
Bram Moolenaara3347722019-05-11 21:14:24 +0200255 return;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200256 }
Bram Moolenaara3347722019-05-11 21:14:24 +0200257 }
258
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200259 lnr = ALLOC_CLEAR_ONE(listener_T);
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200260 if (lnr == NULL)
261 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200262 free_callback(&callback);
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200263 return;
264 }
Bram Moolenaara3347722019-05-11 21:14:24 +0200265 lnr->lr_next = buf->b_listener;
266 buf->b_listener = lnr;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200267
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200268 set_callback(&lnr->lr_callback, &callback);
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200269
270 lnr->lr_id = ++next_listener_id;
271 rettv->vval.v_number = lnr->lr_id;
272}
273
274/*
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200275 * listener_flush() function
276 */
277 void
278f_listener_flush(typval_T *argvars, typval_T *rettv UNUSED)
279{
280 buf_T *buf = curbuf;
281
282 if (argvars[0].v_type != VAR_UNKNOWN)
283 {
284 buf = get_buf_arg(&argvars[0]);
285 if (buf == NULL)
286 return;
287 }
288 invoke_listeners(buf);
289}
290
291/*
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200292 * listener_remove() function
293 */
294 void
Bram Moolenaar7b73f912019-07-13 13:03:02 +0200295f_listener_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200296{
297 listener_T *lnr;
298 listener_T *next;
Bram Moolenaar7b73f912019-07-13 13:03:02 +0200299 listener_T *prev;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200300 int id = tv_get_number(argvars);
Bram Moolenaara3347722019-05-11 21:14:24 +0200301 buf_T *buf;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200302
Bram Moolenaar86173482019-10-01 17:02:16 +0200303 FOR_ALL_BUFFERS(buf)
Bram Moolenaar7b73f912019-07-13 13:03:02 +0200304 {
305 prev = NULL;
Bram Moolenaara3347722019-05-11 21:14:24 +0200306 for (lnr = buf->b_listener; lnr != NULL; lnr = next)
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200307 {
Bram Moolenaara3347722019-05-11 21:14:24 +0200308 next = lnr->lr_next;
309 if (lnr->lr_id == id)
310 {
311 if (prev != NULL)
312 prev->lr_next = lnr->lr_next;
313 else
314 buf->b_listener = lnr->lr_next;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200315 free_callback(&lnr->lr_callback);
Bram Moolenaara3347722019-05-11 21:14:24 +0200316 vim_free(lnr);
Bram Moolenaar7b73f912019-07-13 13:03:02 +0200317 rettv->vval.v_number = 1;
318 return;
Bram Moolenaara3347722019-05-11 21:14:24 +0200319 }
320 prev = lnr;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200321 }
Bram Moolenaar7b73f912019-07-13 13:03:02 +0200322 }
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200323}
324
325/*
Bram Moolenaardda41442019-05-16 22:11:47 +0200326 * Called before inserting a line above "lnum"/"lnum3" or deleting line "lnum"
327 * to "lnume".
328 */
329 void
330may_invoke_listeners(buf_T *buf, linenr_T lnum, linenr_T lnume, int added)
331{
Bram Moolenaar4a0a1612019-07-17 22:00:19 +0200332 check_recorded_changes(buf, lnum, lnume, added);
Bram Moolenaardda41442019-05-16 22:11:47 +0200333}
334
335/*
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200336 * Called when a sequence of changes is done: invoke listeners added with
337 * listener_add().
338 */
339 void
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200340invoke_listeners(buf_T *buf)
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200341{
342 listener_T *lnr;
343 typval_T rettv;
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200344 typval_T argv[6];
345 listitem_T *li;
346 linenr_T start = MAXLNUM;
347 linenr_T end = 0;
348 linenr_T added = 0;
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200349 int save_updating_screen = updating_screen;
350 static int recursive = FALSE;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200351
Bram Moolenaardda41442019-05-16 22:11:47 +0200352 if (buf->b_recorded_changes == NULL // nothing changed
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200353 || buf->b_listener == NULL // no listeners
354 || recursive) // already busy
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200355 return;
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200356 recursive = TRUE;
357
358 // Block messages on channels from being handled, so that they don't make
359 // text changes here.
360 ++updating_screen;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200361
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200362 argv[0].v_type = VAR_NUMBER;
363 argv[0].vval.v_number = buf->b_fnum; // a:bufnr
364
365
Bram Moolenaardda41442019-05-16 22:11:47 +0200366 for (li = buf->b_recorded_changes->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200367 {
368 varnumber_T lnum;
369
370 lnum = dict_get_number(li->li_tv.vval.v_dict, (char_u *)"lnum");
371 if (start > lnum)
372 start = lnum;
373 lnum = dict_get_number(li->li_tv.vval.v_dict, (char_u *)"end");
Bram Moolenaar336bf2b2019-10-24 20:07:07 +0200374 if (end < lnum)
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200375 end = lnum;
Bram Moolenaar336bf2b2019-10-24 20:07:07 +0200376 added += dict_get_number(li->li_tv.vval.v_dict, (char_u *)"added");
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200377 }
378 argv[1].v_type = VAR_NUMBER;
379 argv[1].vval.v_number = start;
380 argv[2].v_type = VAR_NUMBER;
381 argv[2].vval.v_number = end;
382 argv[3].v_type = VAR_NUMBER;
383 argv[3].vval.v_number = added;
384
385 argv[4].v_type = VAR_LIST;
Bram Moolenaardda41442019-05-16 22:11:47 +0200386 argv[4].vval.v_list = buf->b_recorded_changes;
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200387 ++textlock;
388
389 for (lnr = buf->b_listener; lnr != NULL; lnr = lnr->lr_next)
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200390 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200391 call_callback(&lnr->lr_callback, -1, &rettv, 5, argv);
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200392 clear_tv(&rettv);
393 }
394
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200395 --textlock;
Bram Moolenaardda41442019-05-16 22:11:47 +0200396 list_unref(buf->b_recorded_changes);
397 buf->b_recorded_changes = NULL;
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200398
399 if (save_updating_screen)
400 updating_screen = TRUE;
401 else
402 after_updating_screen(TRUE);
403 recursive = FALSE;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200404}
Bram Moolenaar86173482019-10-01 17:02:16 +0200405
406/*
407 * Remove all listeners associated with "buf".
408 */
409 void
410remove_listeners(buf_T *buf)
411{
412 listener_T *lnr;
413 listener_T *next;
414
415 for (lnr = buf->b_listener; lnr != NULL; lnr = next)
416 {
417 next = lnr->lr_next;
418 free_callback(&lnr->lr_callback);
419 vim_free(lnr);
420 }
421 buf->b_listener = NULL;
422}
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200423#endif
424
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200425/*
426 * Common code for when a change was made.
427 * See changed_lines() for the arguments.
428 * Careful: may trigger autocommands that reload the buffer.
429 */
430 static void
431changed_common(
432 linenr_T lnum,
433 colnr_T col,
434 linenr_T lnume,
435 long xtra)
436{
437 win_T *wp;
438 tabpage_T *tp;
439 int i;
440#ifdef FEAT_JUMPLIST
441 int cols;
442 pos_T *p;
443 int add;
444#endif
445
446 // mark the buffer as modified
447 changed();
448
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200449#ifdef FEAT_EVAL
450 may_record_change(lnum, col, lnume, xtra);
451#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200452#ifdef FEAT_DIFF
453 if (curwin->w_p_diff && diff_internal())
454 curtab->tp_diff_update = TRUE;
455#endif
456
457 // set the '. mark
458 if (!cmdmod.keepjumps)
459 {
460 curbuf->b_last_change.lnum = lnum;
461 curbuf->b_last_change.col = col;
462
463#ifdef FEAT_JUMPLIST
464 // Create a new entry if a new undo-able change was started or we
465 // don't have an entry yet.
466 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
467 {
468 if (curbuf->b_changelistlen == 0)
469 add = TRUE;
470 else
471 {
472 // Don't create a new entry when the line number is the same
473 // as the last one and the column is not too far away. Avoids
474 // creating many entries for typing "xxxxx".
475 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
476 if (p->lnum != lnum)
477 add = TRUE;
478 else
479 {
480 cols = comp_textwidth(FALSE);
481 if (cols == 0)
482 cols = 79;
483 add = (p->col + cols < col || col + cols < p->col);
484 }
485 }
486 if (add)
487 {
488 // This is the first of a new sequence of undo-able changes
489 // and it's at some distance of the last change. Use a new
490 // position in the changelist.
491 curbuf->b_new_change = FALSE;
492
493 if (curbuf->b_changelistlen == JUMPLISTSIZE)
494 {
495 // changelist is full: remove oldest entry
496 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
497 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
498 sizeof(pos_T) * (JUMPLISTSIZE - 1));
499 FOR_ALL_TAB_WINDOWS(tp, wp)
500 {
501 // Correct position in changelist for other windows on
502 // this buffer.
503 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
504 --wp->w_changelistidx;
505 }
506 }
507 FOR_ALL_TAB_WINDOWS(tp, wp)
508 {
509 // For other windows, if the position in the changelist is
510 // at the end it stays at the end.
511 if (wp->w_buffer == curbuf
512 && wp->w_changelistidx == curbuf->b_changelistlen)
513 ++wp->w_changelistidx;
514 }
515 ++curbuf->b_changelistlen;
516 }
517 }
518 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
519 curbuf->b_last_change;
520 // The current window is always after the last change, so that "g,"
521 // takes you back to it.
522 curwin->w_changelistidx = curbuf->b_changelistlen;
523#endif
524 }
525
526 FOR_ALL_TAB_WINDOWS(tp, wp)
527 {
528 if (wp->w_buffer == curbuf)
529 {
530 // Mark this window to be redrawn later.
531 if (wp->w_redr_type < VALID)
532 wp->w_redr_type = VALID;
533
534 // Check if a change in the buffer has invalidated the cached
535 // values for the cursor.
536#ifdef FEAT_FOLDING
537 // Update the folds for this window. Can't postpone this, because
538 // a following operator might work on the whole fold: ">>dd".
539 foldUpdate(wp, lnum, lnume + xtra - 1);
540
541 // The change may cause lines above or below the change to become
542 // included in a fold. Set lnum/lnume to the first/last line that
543 // might be displayed differently.
544 // Set w_cline_folded here as an efficient way to update it when
545 // inserting lines just above a closed fold.
546 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
547 if (wp->w_cursor.lnum == lnum)
548 wp->w_cline_folded = i;
549 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
550 if (wp->w_cursor.lnum == lnume)
551 wp->w_cline_folded = i;
552
553 // If the changed line is in a range of previously folded lines,
554 // compare with the first line in that range.
555 if (wp->w_cursor.lnum <= lnum)
556 {
557 i = find_wl_entry(wp, lnum);
558 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
559 changed_line_abv_curs_win(wp);
560 }
561#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200562 if (wp->w_cursor.lnum > lnum)
563 changed_line_abv_curs_win(wp);
564 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
565 changed_cline_bef_curs_win(wp);
566 if (wp->w_botline >= lnum)
567 {
568 // Assume that botline doesn't change (inserted lines make
569 // other lines scroll down below botline).
570 approximate_botline_win(wp);
571 }
572
573 // Check if any w_lines[] entries have become invalid.
574 // For entries below the change: Correct the lnums for
575 // inserted/deleted lines. Makes it possible to stop displaying
576 // after the change.
577 for (i = 0; i < wp->w_lines_valid; ++i)
578 if (wp->w_lines[i].wl_valid)
579 {
580 if (wp->w_lines[i].wl_lnum >= lnum)
581 {
582 if (wp->w_lines[i].wl_lnum < lnume)
583 {
584 // line included in change
585 wp->w_lines[i].wl_valid = FALSE;
586 }
587 else if (xtra != 0)
588 {
589 // line below change
590 wp->w_lines[i].wl_lnum += xtra;
591#ifdef FEAT_FOLDING
592 wp->w_lines[i].wl_lastlnum += xtra;
593#endif
594 }
595 }
596#ifdef FEAT_FOLDING
597 else if (wp->w_lines[i].wl_lastlnum >= lnum)
598 {
599 // change somewhere inside this range of folded lines,
600 // may need to be redrawn
601 wp->w_lines[i].wl_valid = FALSE;
602 }
603#endif
604 }
605
606#ifdef FEAT_FOLDING
607 // Take care of side effects for setting w_topline when folds have
608 // changed. Esp. when the buffer was changed in another window.
609 if (hasAnyFolding(wp))
610 set_topline(wp, wp->w_topline);
611#endif
Bram Moolenaar11a58af2019-10-24 22:32:31 +0200612 // Relative numbering may require updating more.
613 if (wp->w_p_rnu)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200614 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar11a58af2019-10-24 22:32:31 +0200615#ifdef FEAT_SYN_HL
616 // Cursor line highlighting probably need to be updated with
617 // "VALID" if it's below the change.
618 // If the cursor line is inside the change we need to redraw more.
619 if (wp->w_p_cul)
620 {
621 if (xtra == 0)
622 redraw_win_later(wp, VALID);
623 else if (lnum <= wp->w_last_cursorline)
624 redraw_win_later(wp, SOME_VALID);
625 }
626#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200627 }
628 }
629
630 // Call update_screen() later, which checks out what needs to be redrawn,
631 // since it notices b_mod_set and then uses b_mod_*.
632 if (must_redraw < VALID)
633 must_redraw = VALID;
634
635 // when the cursor line is changed always trigger CursorMoved
636 if (lnum <= curwin->w_cursor.lnum
637 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
638 last_cursormoved.lnum = 0;
639}
640
641 static void
642changedOneline(buf_T *buf, linenr_T lnum)
643{
644 if (buf->b_mod_set)
645 {
646 // find the maximum area that must be redisplayed
647 if (lnum < buf->b_mod_top)
648 buf->b_mod_top = lnum;
649 else if (lnum >= buf->b_mod_bot)
650 buf->b_mod_bot = lnum + 1;
651 }
652 else
653 {
654 // set the area that must be redisplayed to one line
655 buf->b_mod_set = TRUE;
656 buf->b_mod_top = lnum;
657 buf->b_mod_bot = lnum + 1;
658 buf->b_mod_xlines = 0;
659 }
660}
661
662/*
663 * Changed bytes within a single line for the current buffer.
664 * - marks the windows on this buffer to be redisplayed
665 * - marks the buffer changed by calling changed()
666 * - invalidates cached values
667 * Careful: may trigger autocommands that reload the buffer.
668 */
669 void
670changed_bytes(linenr_T lnum, colnr_T col)
671{
672 changedOneline(curbuf, lnum);
673 changed_common(lnum, col, lnum + 1, 0L);
674
675#ifdef FEAT_DIFF
676 // Diff highlighting in other diff windows may need to be updated too.
677 if (curwin->w_p_diff)
678 {
679 win_T *wp;
680 linenr_T wlnum;
681
682 FOR_ALL_WINDOWS(wp)
683 if (wp->w_p_diff && wp != curwin)
684 {
685 redraw_win_later(wp, VALID);
686 wlnum = diff_lnum_win(lnum, wp);
687 if (wlnum > 0)
688 changedOneline(wp->w_buffer, wlnum);
689 }
690 }
691#endif
692}
693
694/*
695 * Like changed_bytes() but also adjust text properties for "added" bytes.
696 * When "added" is negative text was deleted.
697 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200698 static void
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200699inserted_bytes(linenr_T lnum, colnr_T col, int added UNUSED)
700{
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200701#ifdef FEAT_TEXT_PROP
702 if (curbuf->b_has_textprop && added != 0)
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200703 adjust_prop_columns(lnum, col, added, 0);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200704#endif
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200705
706 changed_bytes(lnum, col);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200707}
708
709/*
710 * Appended "count" lines below line "lnum" in the current buffer.
711 * Must be called AFTER the change and after mark_adjust().
712 * Takes care of marking the buffer to be redrawn and sets the changed flag.
713 */
714 void
715appended_lines(linenr_T lnum, long count)
716{
717 changed_lines(lnum + 1, 0, lnum + 1, count);
718}
719
720/*
721 * Like appended_lines(), but adjust marks first.
722 */
723 void
724appended_lines_mark(linenr_T lnum, long count)
725{
726 // Skip mark_adjust when adding a line after the last one, there can't
727 // be marks there. But it's still needed in diff mode.
728 if (lnum + count < curbuf->b_ml.ml_line_count
729#ifdef FEAT_DIFF
730 || curwin->w_p_diff
731#endif
732 )
733 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
734 changed_lines(lnum + 1, 0, lnum + 1, count);
735}
736
737/*
738 * Deleted "count" lines at line "lnum" in the current buffer.
739 * Must be called AFTER the change and after mark_adjust().
740 * Takes care of marking the buffer to be redrawn and sets the changed flag.
741 */
742 void
743deleted_lines(linenr_T lnum, long count)
744{
745 changed_lines(lnum, 0, lnum + count, -count);
746}
747
748/*
749 * Like deleted_lines(), but adjust marks first.
750 * Make sure the cursor is on a valid line before calling, a GUI callback may
751 * be triggered to display the cursor.
752 */
753 void
754deleted_lines_mark(linenr_T lnum, long count)
755{
756 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
757 changed_lines(lnum, 0, lnum + count, -count);
758}
759
760/*
761 * Marks the area to be redrawn after a change.
762 */
763 static void
764changed_lines_buf(
765 buf_T *buf,
766 linenr_T lnum, // first line with change
767 linenr_T lnume, // line below last changed line
768 long xtra) // number of extra lines (negative when deleting)
769{
770 if (buf->b_mod_set)
771 {
772 // find the maximum area that must be redisplayed
773 if (lnum < buf->b_mod_top)
774 buf->b_mod_top = lnum;
775 if (lnum < buf->b_mod_bot)
776 {
777 // adjust old bot position for xtra lines
778 buf->b_mod_bot += xtra;
779 if (buf->b_mod_bot < lnum)
780 buf->b_mod_bot = lnum;
781 }
782 if (lnume + xtra > buf->b_mod_bot)
783 buf->b_mod_bot = lnume + xtra;
784 buf->b_mod_xlines += xtra;
785 }
786 else
787 {
788 // set the area that must be redisplayed
789 buf->b_mod_set = TRUE;
790 buf->b_mod_top = lnum;
791 buf->b_mod_bot = lnume + xtra;
792 buf->b_mod_xlines = xtra;
793 }
794}
795
796/*
797 * Changed lines for the current buffer.
798 * Must be called AFTER the change and after mark_adjust().
799 * - mark the buffer changed by calling changed()
800 * - mark the windows on this buffer to be redisplayed
801 * - invalidate cached values
802 * "lnum" is the first line that needs displaying, "lnume" the first line
803 * below the changed lines (BEFORE the change).
804 * When only inserting lines, "lnum" and "lnume" are equal.
805 * Takes care of calling changed() and updating b_mod_*.
806 * Careful: may trigger autocommands that reload the buffer.
807 */
808 void
809changed_lines(
810 linenr_T lnum, // first line with change
811 colnr_T col, // column in first line with change
812 linenr_T lnume, // line below last changed line
813 long xtra) // number of extra lines (negative when deleting)
814{
815 changed_lines_buf(curbuf, lnum, lnume, xtra);
816
817#ifdef FEAT_DIFF
818 if (xtra == 0 && curwin->w_p_diff && !diff_internal())
819 {
820 // When the number of lines doesn't change then mark_adjust() isn't
821 // called and other diff buffers still need to be marked for
822 // displaying.
823 win_T *wp;
824 linenr_T wlnum;
825
826 FOR_ALL_WINDOWS(wp)
827 if (wp->w_p_diff && wp != curwin)
828 {
829 redraw_win_later(wp, VALID);
830 wlnum = diff_lnum_win(lnum, wp);
831 if (wlnum > 0)
832 changed_lines_buf(wp->w_buffer, wlnum,
833 lnume - lnum + wlnum, 0L);
834 }
835 }
836#endif
837
838 changed_common(lnum, col, lnume, xtra);
839}
840
841/*
842 * Called when the changed flag must be reset for buffer "buf".
843 * When "ff" is TRUE also reset 'fileformat'.
Bram Moolenaarc024b462019-06-08 18:07:21 +0200844 * When "always_inc_changedtick" is TRUE b:changedtick is incremented also when
845 * the changed flag was off.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200846 */
847 void
Bram Moolenaarc024b462019-06-08 18:07:21 +0200848unchanged(buf_T *buf, int ff, int always_inc_changedtick)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200849{
850 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
851 {
852 buf->b_changed = 0;
853 ml_setflags(buf);
854 if (ff)
855 save_file_ff(buf);
856 check_status(buf);
857 redraw_tabline = TRUE;
858#ifdef FEAT_TITLE
859 need_maketitle = TRUE; // set window title later
860#endif
Bram Moolenaarc024b462019-06-08 18:07:21 +0200861 ++CHANGEDTICK(buf);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200862 }
Bram Moolenaarc024b462019-06-08 18:07:21 +0200863 else if (always_inc_changedtick)
864 ++CHANGEDTICK(buf);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200865#ifdef FEAT_NETBEANS_INTG
866 netbeans_unmodified(buf);
867#endif
868}
869
870/*
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100871 * Save the current values of 'fileformat' and 'fileencoding', so that we know
872 * the file must be considered changed when the value is different.
873 */
874 void
875save_file_ff(buf_T *buf)
876{
877 buf->b_start_ffc = *buf->b_p_ff;
878 buf->b_start_eol = buf->b_p_eol;
879 buf->b_start_bomb = buf->b_p_bomb;
880
Bram Moolenaarc667da52019-11-30 20:52:27 +0100881 // Only use free/alloc when necessary, they take time.
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100882 if (buf->b_start_fenc == NULL
883 || STRCMP(buf->b_start_fenc, buf->b_p_fenc) != 0)
884 {
885 vim_free(buf->b_start_fenc);
886 buf->b_start_fenc = vim_strsave(buf->b_p_fenc);
887 }
888}
889
890/*
891 * Return TRUE if 'fileformat' and/or 'fileencoding' has a different value
892 * from when editing started (save_file_ff() called).
893 * Also when 'endofline' was changed and 'binary' is set, or when 'bomb' was
894 * changed and 'binary' is not set.
895 * Also when 'endofline' was changed and 'fixeol' is not set.
896 * When "ignore_empty" is true don't consider a new, empty buffer to be
897 * changed.
898 */
899 int
900file_ff_differs(buf_T *buf, int ignore_empty)
901{
Bram Moolenaarc667da52019-11-30 20:52:27 +0100902 // In a buffer that was never loaded the options are not valid.
Bram Moolenaar7bae0b12019-11-21 22:14:18 +0100903 if (buf->b_flags & BF_NEVERLOADED)
904 return FALSE;
905 if (ignore_empty
906 && (buf->b_flags & BF_NEW)
907 && buf->b_ml.ml_line_count == 1
908 && *ml_get_buf(buf, (linenr_T)1, FALSE) == NUL)
909 return FALSE;
910 if (buf->b_start_ffc != *buf->b_p_ff)
911 return TRUE;
912 if ((buf->b_p_bin || !buf->b_p_fixeol) && buf->b_start_eol != buf->b_p_eol)
913 return TRUE;
914 if (!buf->b_p_bin && buf->b_start_bomb != buf->b_p_bomb)
915 return TRUE;
916 if (buf->b_start_fenc == NULL)
917 return (*buf->b_p_fenc != NUL);
918 return (STRCMP(buf->b_start_fenc, buf->b_p_fenc) != 0);
919}
920
921/*
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200922 * Insert string "p" at the cursor position. Stops at a NUL byte.
923 * Handles Replace mode and multi-byte characters.
924 */
925 void
926ins_bytes(char_u *p)
927{
928 ins_bytes_len(p, (int)STRLEN(p));
929}
930
931/*
932 * Insert string "p" with length "len" at the cursor position.
933 * Handles Replace mode and multi-byte characters.
934 */
935 void
936ins_bytes_len(char_u *p, int len)
937{
938 int i;
939 int n;
940
941 if (has_mbyte)
942 for (i = 0; i < len; i += n)
943 {
944 if (enc_utf8)
945 // avoid reading past p[len]
946 n = utfc_ptr2len_len(p + i, len - i);
947 else
948 n = (*mb_ptr2len)(p + i);
949 ins_char_bytes(p + i, n);
950 }
951 else
952 for (i = 0; i < len; ++i)
953 ins_char(p[i]);
954}
955
956/*
957 * Insert or replace a single character at the cursor position.
958 * When in REPLACE or VREPLACE mode, replace any existing character.
959 * Caller must have prepared for undo.
960 * For multi-byte characters we get the whole character, the caller must
961 * convert bytes to a character.
962 */
963 void
964ins_char(int c)
965{
966 char_u buf[MB_MAXBYTES + 1];
967 int n = (*mb_char2bytes)(c, buf);
968
969 // When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
970 // Happens for CTRL-Vu9900.
971 if (buf[0] == 0)
972 buf[0] = '\n';
973
974 ins_char_bytes(buf, n);
975}
976
977 void
978ins_char_bytes(char_u *buf, int charlen)
979{
980 int c = buf[0];
981 int newlen; // nr of bytes inserted
982 int oldlen; // nr of bytes deleted (0 when not replacing)
983 char_u *p;
984 char_u *newp;
985 char_u *oldp;
986 int linelen; // length of old line including NUL
987 colnr_T col;
988 linenr_T lnum = curwin->w_cursor.lnum;
989 int i;
990
991 // Break tabs if needed.
992 if (virtual_active() && curwin->w_cursor.coladd > 0)
993 coladvance_force(getviscol());
994
995 col = curwin->w_cursor.col;
996 oldp = ml_get(lnum);
997 linelen = (int)STRLEN(oldp) + 1;
998
999 // The lengths default to the values for when not replacing.
1000 oldlen = 0;
1001 newlen = charlen;
1002
1003 if (State & REPLACE_FLAG)
1004 {
1005 if (State & VREPLACE_FLAG)
1006 {
1007 colnr_T new_vcol = 0; // init for GCC
1008 colnr_T vcol;
1009 int old_list;
1010
1011 // Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
1012 // Returns the old value of list, so when finished,
1013 // curwin->w_p_list should be set back to this.
1014 old_list = curwin->w_p_list;
1015 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
1016 curwin->w_p_list = FALSE;
1017
1018 // In virtual replace mode each character may replace one or more
1019 // characters (zero if it's a TAB). Count the number of bytes to
1020 // be deleted to make room for the new character, counting screen
1021 // cells. May result in adding spaces to fill a gap.
1022 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
1023 new_vcol = vcol + chartabsize(buf, vcol);
1024 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
1025 {
1026 vcol += chartabsize(oldp + col + oldlen, vcol);
1027 // Don't need to remove a TAB that takes us to the right
1028 // position.
1029 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
1030 break;
1031 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
1032 // Deleted a bit too much, insert spaces.
1033 if (vcol > new_vcol)
1034 newlen += vcol - new_vcol;
1035 }
1036 curwin->w_p_list = old_list;
1037 }
1038 else if (oldp[col] != NUL)
1039 {
1040 // normal replace
1041 oldlen = (*mb_ptr2len)(oldp + col);
1042 }
1043
1044
1045 // Push the replaced bytes onto the replace stack, so that they can be
1046 // put back when BS is used. The bytes of a multi-byte character are
1047 // done the other way around, so that the first byte is popped off
1048 // first (it tells the byte length of the character).
1049 replace_push(NUL);
1050 for (i = 0; i < oldlen; ++i)
1051 {
1052 if (has_mbyte)
1053 i += replace_push_mb(oldp + col + i) - 1;
1054 else
1055 replace_push(oldp[col + i]);
1056 }
1057 }
1058
Bram Moolenaar964b3742019-05-24 18:54:09 +02001059 newp = alloc(linelen + newlen - oldlen);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001060 if (newp == NULL)
1061 return;
1062
1063 // Copy bytes before the cursor.
1064 if (col > 0)
1065 mch_memmove(newp, oldp, (size_t)col);
1066
1067 // Copy bytes after the changed character(s).
1068 p = newp + col;
1069 if (linelen > col + oldlen)
1070 mch_memmove(p + newlen, oldp + col + oldlen,
1071 (size_t)(linelen - col - oldlen));
1072
1073 // Insert or overwrite the new character.
1074 mch_memmove(p, buf, charlen);
1075 i = charlen;
1076
1077 // Fill with spaces when necessary.
1078 while (i < newlen)
1079 p[i++] = ' ';
1080
1081 // Replace the line in the buffer.
1082 ml_replace(lnum, newp, FALSE);
1083
1084 // mark the buffer as changed and prepare for displaying
1085 inserted_bytes(lnum, col, newlen - oldlen);
1086
1087 // If we're in Insert or Replace mode and 'showmatch' is set, then briefly
1088 // show the match for right parens and braces.
1089 if (p_sm && (State & INSERT)
1090 && msg_silent == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001091 && !ins_compl_active())
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001092 {
1093 if (has_mbyte)
1094 showmatch(mb_ptr2char(buf));
1095 else
1096 showmatch(c);
1097 }
1098
1099#ifdef FEAT_RIGHTLEFT
1100 if (!p_ri || (State & REPLACE_FLAG))
1101#endif
1102 {
1103 // Normal insert: move cursor right
1104 curwin->w_cursor.col += charlen;
1105 }
1106
1107 // TODO: should try to update w_row here, to avoid recomputing it later.
1108}
1109
1110/*
1111 * Insert a string at the cursor position.
1112 * Note: Does NOT handle Replace mode.
1113 * Caller must have prepared for undo.
1114 */
1115 void
1116ins_str(char_u *s)
1117{
1118 char_u *oldp, *newp;
1119 int newlen = (int)STRLEN(s);
1120 int oldlen;
1121 colnr_T col;
1122 linenr_T lnum = curwin->w_cursor.lnum;
1123
1124 if (virtual_active() && curwin->w_cursor.coladd > 0)
1125 coladvance_force(getviscol());
1126
1127 col = curwin->w_cursor.col;
1128 oldp = ml_get(lnum);
1129 oldlen = (int)STRLEN(oldp);
1130
Bram Moolenaar964b3742019-05-24 18:54:09 +02001131 newp = alloc(oldlen + newlen + 1);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001132 if (newp == NULL)
1133 return;
1134 if (col > 0)
1135 mch_memmove(newp, oldp, (size_t)col);
1136 mch_memmove(newp + col, s, (size_t)newlen);
1137 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
1138 ml_replace(lnum, newp, FALSE);
1139 inserted_bytes(lnum, col, newlen);
1140 curwin->w_cursor.col += newlen;
1141}
1142
1143/*
1144 * Delete one character under the cursor.
1145 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
1146 * Caller must have prepared for undo.
1147 *
1148 * return FAIL for failure, OK otherwise
1149 */
1150 int
1151del_char(int fixpos)
1152{
1153 if (has_mbyte)
1154 {
1155 // Make sure the cursor is at the start of a character.
1156 mb_adjust_cursor();
1157 if (*ml_get_cursor() == NUL)
1158 return FAIL;
1159 return del_chars(1L, fixpos);
1160 }
1161 return del_bytes(1L, fixpos, TRUE);
1162}
1163
1164/*
1165 * Like del_bytes(), but delete characters instead of bytes.
1166 */
1167 int
1168del_chars(long count, int fixpos)
1169{
1170 long bytes = 0;
1171 long i;
1172 char_u *p;
1173 int l;
1174
1175 p = ml_get_cursor();
1176 for (i = 0; i < count && *p != NUL; ++i)
1177 {
1178 l = (*mb_ptr2len)(p);
1179 bytes += l;
1180 p += l;
1181 }
1182 return del_bytes(bytes, fixpos, TRUE);
1183}
1184
1185/*
1186 * Delete "count" bytes under the cursor.
1187 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
1188 * Caller must have prepared for undo.
1189 *
1190 * Return FAIL for failure, OK otherwise.
1191 */
1192 int
1193del_bytes(
1194 long count,
1195 int fixpos_arg,
1196 int use_delcombine UNUSED) // 'delcombine' option applies
1197{
1198 char_u *oldp, *newp;
1199 colnr_T oldlen;
1200 colnr_T newlen;
1201 linenr_T lnum = curwin->w_cursor.lnum;
1202 colnr_T col = curwin->w_cursor.col;
1203 int alloc_newp;
1204 long movelen;
1205 int fixpos = fixpos_arg;
1206
1207 oldp = ml_get(lnum);
1208 oldlen = (int)STRLEN(oldp);
1209
1210 // Can't do anything when the cursor is on the NUL after the line.
1211 if (col >= oldlen)
1212 return FAIL;
1213
1214 // If "count" is zero there is nothing to do.
1215 if (count == 0)
1216 return OK;
1217
1218 // If "count" is negative the caller must be doing something wrong.
1219 if (count < 1)
1220 {
1221 siemsg("E950: Invalid count for del_bytes(): %ld", count);
1222 return FAIL;
1223 }
1224
1225 // If 'delcombine' is set and deleting (less than) one character, only
1226 // delete the last combining character.
1227 if (p_deco && use_delcombine && enc_utf8
1228 && utfc_ptr2len(oldp + col) >= count)
1229 {
1230 int cc[MAX_MCO];
1231 int n;
1232
1233 (void)utfc_ptr2char(oldp + col, cc);
1234 if (cc[0] != NUL)
1235 {
1236 // Find the last composing char, there can be several.
1237 n = col;
1238 do
1239 {
1240 col = n;
1241 count = utf_ptr2len(oldp + n);
1242 n += count;
1243 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
1244 fixpos = 0;
1245 }
1246 }
1247
1248 // When count is too big, reduce it.
1249 movelen = (long)oldlen - (long)col - count + 1; // includes trailing NUL
1250 if (movelen <= 1)
1251 {
1252 // If we just took off the last character of a non-blank line, and
1253 // fixpos is TRUE, we don't want to end up positioned at the NUL,
1254 // unless "restart_edit" is set or 'virtualedit' contains "onemore".
1255 if (col > 0 && fixpos && restart_edit == 0
1256 && (ve_flags & VE_ONEMORE) == 0)
1257 {
1258 --curwin->w_cursor.col;
1259 curwin->w_cursor.coladd = 0;
1260 if (has_mbyte)
1261 curwin->w_cursor.col -=
1262 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
1263 }
1264 count = oldlen - col;
1265 movelen = 1;
1266 }
1267 newlen = oldlen - count;
1268
1269 // If the old line has been allocated the deletion can be done in the
1270 // existing line. Otherwise a new line has to be allocated
1271 // Can't do this when using Netbeans, because we would need to invoke
1272 // netbeans_removed(), which deallocates the line. Let ml_replace() take
1273 // care of notifying Netbeans.
1274#ifdef FEAT_NETBEANS_INTG
1275 if (netbeans_active())
1276 alloc_newp = TRUE;
1277 else
1278#endif
1279 alloc_newp = !ml_line_alloced(); // check if oldp was allocated
1280 if (!alloc_newp)
1281 newp = oldp; // use same allocated memory
1282 else
1283 { // need to allocate a new line
Bram Moolenaar964b3742019-05-24 18:54:09 +02001284 newp = alloc(newlen + 1);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001285 if (newp == NULL)
1286 return FAIL;
1287 mch_memmove(newp, oldp, (size_t)col);
1288 }
1289 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
1290 if (alloc_newp)
1291 ml_replace(lnum, newp, FALSE);
1292#ifdef FEAT_TEXT_PROP
1293 else
1294 {
1295 // Also move any following text properties.
1296 if (oldlen + 1 < curbuf->b_ml.ml_line_len)
1297 mch_memmove(newp + newlen + 1, oldp + oldlen + 1,
1298 (size_t)curbuf->b_ml.ml_line_len - oldlen - 1);
1299 curbuf->b_ml.ml_line_len -= count;
1300 }
1301#endif
1302
1303 // mark the buffer as changed and prepare for displaying
1304 inserted_bytes(lnum, curwin->w_cursor.col, -count);
1305
1306 return OK;
1307}
1308
1309/*
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001310 * open_line: Add a new line below or above the current line.
1311 *
1312 * For VREPLACE mode, we only add a new line when we get to the end of the
1313 * file, otherwise we just start replacing the next line.
1314 *
1315 * Caller must take care of undo. Since VREPLACE may affect any number of
1316 * lines however, it may call u_save_cursor() again when starting to change a
1317 * new line.
1318 * "flags": OPENLINE_DELSPACES delete spaces after cursor
1319 * OPENLINE_DO_COM format comments
1320 * OPENLINE_KEEPTRAIL keep trailing spaces
1321 * OPENLINE_MARKFIX adjust mark positions after the line break
1322 * OPENLINE_COM_LIST format comments with list or 2nd line indent
1323 *
1324 * "second_line_indent": indent for after ^^D in Insert mode or if flag
1325 * OPENLINE_COM_LIST
1326 *
1327 * Return OK for success, FAIL for failure
1328 */
1329 int
1330open_line(
1331 int dir, // FORWARD or BACKWARD
1332 int flags,
1333 int second_line_indent)
1334{
1335 char_u *saved_line; // copy of the original line
1336 char_u *next_line = NULL; // copy of the next line
1337 char_u *p_extra = NULL; // what goes to next line
1338 int less_cols = 0; // less columns for mark in new line
1339 int less_cols_off = 0; // columns to skip for mark adjust
1340 pos_T old_cursor; // old cursor position
1341 int newcol = 0; // new cursor column
1342 int newindent = 0; // auto-indent of the new line
1343 int n;
1344 int trunc_line = FALSE; // truncate current line afterwards
1345 int retval = FAIL; // return value
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001346 int extra_len = 0; // length of p_extra string
1347 int lead_len; // length of comment leader
1348 char_u *lead_flags; // position in 'comments' for comment leader
1349 char_u *leader = NULL; // copy of comment leader
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001350 char_u *allocated = NULL; // allocated memory
1351 char_u *p;
1352 int saved_char = NUL; // init for GCC
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001353 pos_T *pos;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001354#ifdef FEAT_SMARTINDENT
1355 int do_si = (!p_paste && curbuf->b_p_si
1356# ifdef FEAT_CINDENT
1357 && !curbuf->b_p_cin
1358# endif
1359# ifdef FEAT_EVAL
1360 && *curbuf->b_p_inde == NUL
1361# endif
1362 );
1363 int no_si = FALSE; // reset did_si afterwards
1364 int first_char = NUL; // init for GCC
1365#endif
1366#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1367 int vreplace_mode;
1368#endif
1369 int did_append; // appended a new line
1370 int saved_pi = curbuf->b_p_pi; // copy of preserveindent setting
1371
1372 // make a copy of the current line so we can mess with it
1373 saved_line = vim_strsave(ml_get_curline());
Bram Moolenaarc667da52019-11-30 20:52:27 +01001374 if (saved_line == NULL) // out of memory!
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001375 return FALSE;
1376
1377 if (State & VREPLACE_FLAG)
1378 {
1379 // With VREPLACE we make a copy of the next line, which we will be
1380 // starting to replace. First make the new line empty and let vim play
1381 // with the indenting and comment leader to its heart's content. Then
1382 // we grab what it ended up putting on the new line, put back the
1383 // original line, and call ins_char() to put each new character onto
1384 // the line, replacing what was there before and pushing the right
1385 // stuff onto the replace stack. -- webb.
1386 if (curwin->w_cursor.lnum < orig_line_count)
1387 next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1));
1388 else
1389 next_line = vim_strsave((char_u *)"");
1390 if (next_line == NULL) // out of memory!
1391 goto theend;
1392
1393 // In VREPLACE mode, a NL replaces the rest of the line, and starts
1394 // replacing the next line, so push all of the characters left on the
1395 // line onto the replace stack. We'll push any other characters that
1396 // might be replaced at the start of the next line (due to autoindent
1397 // etc) a bit later.
1398 replace_push(NUL); // Call twice because BS over NL expects it
1399 replace_push(NUL);
1400 p = saved_line + curwin->w_cursor.col;
1401 while (*p != NUL)
1402 {
1403 if (has_mbyte)
1404 p += replace_push_mb(p);
1405 else
1406 replace_push(*p++);
1407 }
1408 saved_line[curwin->w_cursor.col] = NUL;
1409 }
1410
1411 if ((State & INSERT) && !(State & VREPLACE_FLAG))
1412 {
1413 p_extra = saved_line + curwin->w_cursor.col;
1414#ifdef FEAT_SMARTINDENT
1415 if (do_si) // need first char after new line break
1416 {
1417 p = skipwhite(p_extra);
1418 first_char = *p;
1419 }
1420#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001421 extra_len = (int)STRLEN(p_extra);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001422 saved_char = *p_extra;
1423 *p_extra = NUL;
1424 }
1425
1426 u_clearline(); // cannot do "U" command when adding lines
1427#ifdef FEAT_SMARTINDENT
1428 did_si = FALSE;
1429#endif
1430 ai_col = 0;
1431
1432 // If we just did an auto-indent, then we didn't type anything on
1433 // the prior line, and it should be truncated. Do this even if 'ai' is not
1434 // set because automatically inserting a comment leader also sets did_ai.
1435 if (dir == FORWARD && did_ai)
1436 trunc_line = TRUE;
1437
1438 // If 'autoindent' and/or 'smartindent' is set, try to figure out what
1439 // indent to use for the new line.
1440 if (curbuf->b_p_ai
1441#ifdef FEAT_SMARTINDENT
1442 || do_si
1443#endif
1444 )
1445 {
1446 // count white space on current line
1447#ifdef FEAT_VARTABS
1448 newindent = get_indent_str_vtab(saved_line, curbuf->b_p_ts,
1449 curbuf->b_p_vts_array, FALSE);
1450#else
1451 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts, FALSE);
1452#endif
1453 if (newindent == 0 && !(flags & OPENLINE_COM_LIST))
1454 newindent = second_line_indent; // for ^^D command in insert mode
1455
1456#ifdef FEAT_SMARTINDENT
1457 // Do smart indenting.
1458 // In insert/replace mode (only when dir == FORWARD)
1459 // we may move some text to the next line. If it starts with '{'
1460 // don't add an indent. Fixes inserting a NL before '{' in line
1461 // "if (condition) {"
1462 if (!trunc_line && do_si && *saved_line != NUL
1463 && (p_extra == NULL || first_char != '{'))
1464 {
1465 char_u *ptr;
1466 char_u last_char;
1467
1468 old_cursor = curwin->w_cursor;
1469 ptr = saved_line;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001470 if (flags & OPENLINE_DO_COM)
1471 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
1472 else
1473 lead_len = 0;
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001474 if (dir == FORWARD)
1475 {
1476 // Skip preprocessor directives, unless they are
1477 // recognised as comments.
Bram Moolenaar8c96af92019-09-28 19:05:57 +02001478 if ( lead_len == 0 && ptr[0] == '#')
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001479 {
1480 while (ptr[0] == '#' && curwin->w_cursor.lnum > 1)
1481 ptr = ml_get(--curwin->w_cursor.lnum);
1482 newindent = get_indent();
1483 }
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001484 if (flags & OPENLINE_DO_COM)
1485 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
1486 else
1487 lead_len = 0;
1488 if (lead_len > 0)
1489 {
1490 // This case gets the following right:
1491 // /*
1492 // * A comment (read '\' as '/').
1493 // */
1494 // #define IN_THE_WAY
1495 // This should line up here;
1496 p = skipwhite(ptr);
1497 if (p[0] == '/' && p[1] == '*')
1498 p++;
1499 if (p[0] == '*')
1500 {
1501 for (p++; *p; p++)
1502 {
1503 if (p[0] == '/' && p[-1] == '*')
1504 {
1505 // End of C comment, indent should line up
1506 // with the line containing the start of
1507 // the comment
1508 curwin->w_cursor.col = (colnr_T)(p - ptr);
1509 if ((pos = findmatch(NULL, NUL)) != NULL)
1510 {
1511 curwin->w_cursor.lnum = pos->lnum;
1512 newindent = get_indent();
1513 }
1514 }
1515 }
1516 }
1517 }
1518 else // Not a comment line
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001519 {
1520 // Find last non-blank in line
1521 p = ptr + STRLEN(ptr) - 1;
1522 while (p > ptr && VIM_ISWHITE(*p))
1523 --p;
1524 last_char = *p;
1525
1526 // find the character just before the '{' or ';'
1527 if (last_char == '{' || last_char == ';')
1528 {
1529 if (p > ptr)
1530 --p;
1531 while (p > ptr && VIM_ISWHITE(*p))
1532 --p;
1533 }
1534 // Try to catch lines that are split over multiple
1535 // lines. eg:
1536 // if (condition &&
1537 // condition) {
1538 // Should line up here!
1539 // }
1540 if (*p == ')')
1541 {
1542 curwin->w_cursor.col = (colnr_T)(p - ptr);
1543 if ((pos = findmatch(NULL, '(')) != NULL)
1544 {
1545 curwin->w_cursor.lnum = pos->lnum;
1546 newindent = get_indent();
1547 ptr = ml_get_curline();
1548 }
1549 }
1550 // If last character is '{' do indent, without
1551 // checking for "if" and the like.
1552 if (last_char == '{')
1553 {
1554 did_si = TRUE; // do indent
1555 no_si = TRUE; // don't delete it when '{' typed
1556 }
1557 // Look for "if" and the like, use 'cinwords'.
1558 // Don't do this if the previous line ended in ';' or
1559 // '}'.
1560 else if (last_char != ';' && last_char != '}'
1561 && cin_is_cinword(ptr))
1562 did_si = TRUE;
1563 }
1564 }
1565 else // dir == BACKWARD
1566 {
1567 // Skip preprocessor directives, unless they are
1568 // recognised as comments.
Bram Moolenaar8c96af92019-09-28 19:05:57 +02001569 if (lead_len == 0 && ptr[0] == '#')
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001570 {
1571 int was_backslashed = FALSE;
1572
1573 while ((ptr[0] == '#' || was_backslashed) &&
1574 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
1575 {
1576 if (*ptr && ptr[STRLEN(ptr) - 1] == '\\')
1577 was_backslashed = TRUE;
1578 else
1579 was_backslashed = FALSE;
1580 ptr = ml_get(++curwin->w_cursor.lnum);
1581 }
1582 if (was_backslashed)
1583 newindent = 0; // Got to end of file
1584 else
1585 newindent = get_indent();
1586 }
1587 p = skipwhite(ptr);
1588 if (*p == '}') // if line starts with '}': do indent
1589 did_si = TRUE;
1590 else // can delete indent when '{' typed
1591 can_si_back = TRUE;
1592 }
1593 curwin->w_cursor = old_cursor;
1594 }
1595 if (do_si)
1596 can_si = TRUE;
1597#endif // FEAT_SMARTINDENT
1598
1599 did_ai = TRUE;
1600 }
1601
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001602 // Find out if the current line starts with a comment leader.
1603 // This may then be inserted in front of the new line.
1604 end_comment_pending = NUL;
1605 if (flags & OPENLINE_DO_COM)
1606 lead_len = get_leader_len(saved_line, &lead_flags,
1607 dir == BACKWARD, TRUE);
1608 else
1609 lead_len = 0;
1610 if (lead_len > 0)
1611 {
1612 char_u *lead_repl = NULL; // replaces comment leader
1613 int lead_repl_len = 0; // length of *lead_repl
1614 char_u lead_middle[COM_MAX_LEN]; // middle-comment string
1615 char_u lead_end[COM_MAX_LEN]; // end-comment string
1616 char_u *comment_end = NULL; // where lead_end has been found
1617 int extra_space = FALSE; // append extra space
1618 int current_flag;
1619 int require_blank = FALSE; // requires blank after middle
1620 char_u *p2;
1621
1622 // If the comment leader has the start, middle or end flag, it may not
1623 // be used or may be replaced with the middle leader.
1624 for (p = lead_flags; *p && *p != ':'; ++p)
1625 {
1626 if (*p == COM_BLANK)
1627 {
1628 require_blank = TRUE;
1629 continue;
1630 }
1631 if (*p == COM_START || *p == COM_MIDDLE)
1632 {
1633 current_flag = *p;
1634 if (*p == COM_START)
1635 {
1636 // Doing "O" on a start of comment does not insert leader.
1637 if (dir == BACKWARD)
1638 {
1639 lead_len = 0;
1640 break;
1641 }
1642
1643 // find start of middle part
1644 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
1645 require_blank = FALSE;
1646 }
1647
1648 // Isolate the strings of the middle and end leader.
Bram Moolenaarc667da52019-11-30 20:52:27 +01001649 while (*p && p[-1] != ':') // find end of middle flags
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001650 {
1651 if (*p == COM_BLANK)
1652 require_blank = TRUE;
1653 ++p;
1654 }
1655 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
1656
1657 while (*p && p[-1] != ':') // find end of end flags
1658 {
1659 // Check whether we allow automatic ending of comments
1660 if (*p == COM_AUTO_END)
1661 end_comment_pending = -1; // means we want to set it
1662 ++p;
1663 }
1664 n = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
1665
1666 if (end_comment_pending == -1) // we can set it now
1667 end_comment_pending = lead_end[n - 1];
1668
1669 // If the end of the comment is in the same line, don't use
1670 // the comment leader.
1671 if (dir == FORWARD)
1672 {
1673 for (p = saved_line + lead_len; *p; ++p)
1674 if (STRNCMP(p, lead_end, n) == 0)
1675 {
1676 comment_end = p;
1677 lead_len = 0;
1678 break;
1679 }
1680 }
1681
1682 // Doing "o" on a start of comment inserts the middle leader.
1683 if (lead_len > 0)
1684 {
1685 if (current_flag == COM_START)
1686 {
1687 lead_repl = lead_middle;
1688 lead_repl_len = (int)STRLEN(lead_middle);
1689 }
1690
1691 // If we have hit RETURN immediately after the start
1692 // comment leader, then put a space after the middle
1693 // comment leader on the next line.
1694 if (!VIM_ISWHITE(saved_line[lead_len - 1])
1695 && ((p_extra != NULL
1696 && (int)curwin->w_cursor.col == lead_len)
1697 || (p_extra == NULL
1698 && saved_line[lead_len] == NUL)
1699 || require_blank))
1700 extra_space = TRUE;
1701 }
1702 break;
1703 }
1704 if (*p == COM_END)
1705 {
1706 // Doing "o" on the end of a comment does not insert leader.
1707 // Remember where the end is, might want to use it to find the
1708 // start (for C-comments).
1709 if (dir == FORWARD)
1710 {
1711 comment_end = skipwhite(saved_line);
1712 lead_len = 0;
1713 break;
1714 }
1715
1716 // Doing "O" on the end of a comment inserts the middle leader.
1717 // Find the string for the middle leader, searching backwards.
1718 while (p > curbuf->b_p_com && *p != ',')
1719 --p;
1720 for (lead_repl = p; lead_repl > curbuf->b_p_com
1721 && lead_repl[-1] != ':'; --lead_repl)
1722 ;
1723 lead_repl_len = (int)(p - lead_repl);
1724
1725 // We can probably always add an extra space when doing "O" on
1726 // the comment-end
1727 extra_space = TRUE;
1728
1729 // Check whether we allow automatic ending of comments
1730 for (p2 = p; *p2 && *p2 != ':'; p2++)
1731 {
1732 if (*p2 == COM_AUTO_END)
1733 end_comment_pending = -1; // means we want to set it
1734 }
1735 if (end_comment_pending == -1)
1736 {
1737 // Find last character in end-comment string
1738 while (*p2 && *p2 != ',')
1739 p2++;
1740 end_comment_pending = p2[-1];
1741 }
1742 break;
1743 }
1744 if (*p == COM_FIRST)
1745 {
1746 // Comment leader for first line only: Don't repeat leader
1747 // when using "O", blank out leader when using "o".
1748 if (dir == BACKWARD)
1749 lead_len = 0;
1750 else
1751 {
1752 lead_repl = (char_u *)"";
1753 lead_repl_len = 0;
1754 }
1755 break;
1756 }
1757 }
1758 if (lead_len)
1759 {
1760 // allocate buffer (may concatenate p_extra later)
1761 leader = alloc(lead_len + lead_repl_len + extra_space + extra_len
1762 + (second_line_indent > 0 ? second_line_indent : 0) + 1);
1763 allocated = leader; // remember to free it later
1764
1765 if (leader == NULL)
1766 lead_len = 0;
1767 else
1768 {
1769 vim_strncpy(leader, saved_line, lead_len);
1770
1771 // Replace leader with lead_repl, right or left adjusted
1772 if (lead_repl != NULL)
1773 {
1774 int c = 0;
1775 int off = 0;
1776
1777 for (p = lead_flags; *p != NUL && *p != ':'; )
1778 {
1779 if (*p == COM_RIGHT || *p == COM_LEFT)
1780 c = *p++;
1781 else if (VIM_ISDIGIT(*p) || *p == '-')
1782 off = getdigits(&p);
1783 else
1784 ++p;
1785 }
1786 if (c == COM_RIGHT) // right adjusted leader
1787 {
1788 // find last non-white in the leader to line up with
1789 for (p = leader + lead_len - 1; p > leader
1790 && VIM_ISWHITE(*p); --p)
1791 ;
1792 ++p;
1793
1794 // Compute the length of the replaced characters in
1795 // screen characters, not bytes.
1796 {
1797 int repl_size = vim_strnsize(lead_repl,
1798 lead_repl_len);
1799 int old_size = 0;
1800 char_u *endp = p;
1801 int l;
1802
1803 while (old_size < repl_size && p > leader)
1804 {
1805 MB_PTR_BACK(leader, p);
1806 old_size += ptr2cells(p);
1807 }
1808 l = lead_repl_len - (int)(endp - p);
1809 if (l != 0)
1810 mch_memmove(endp + l, endp,
1811 (size_t)((leader + lead_len) - endp));
1812 lead_len += l;
1813 }
1814 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1815 if (p + lead_repl_len > leader + lead_len)
1816 p[lead_repl_len] = NUL;
1817
1818 // blank-out any other chars from the old leader.
1819 while (--p >= leader)
1820 {
1821 int l = mb_head_off(leader, p);
1822
1823 if (l > 1)
1824 {
1825 p -= l;
1826 if (ptr2cells(p) > 1)
1827 {
1828 p[1] = ' ';
1829 --l;
1830 }
1831 mch_memmove(p + 1, p + l + 1,
1832 (size_t)((leader + lead_len) - (p + l + 1)));
1833 lead_len -= l;
1834 *p = ' ';
1835 }
1836 else if (!VIM_ISWHITE(*p))
1837 *p = ' ';
1838 }
1839 }
1840 else // left adjusted leader
1841 {
1842 p = skipwhite(leader);
1843
1844 // Compute the length of the replaced characters in
1845 // screen characters, not bytes. Move the part that is
1846 // not to be overwritten.
1847 {
1848 int repl_size = vim_strnsize(lead_repl,
1849 lead_repl_len);
1850 int i;
1851 int l;
1852
1853 for (i = 0; i < lead_len && p[i] != NUL; i += l)
1854 {
1855 l = (*mb_ptr2len)(p + i);
1856 if (vim_strnsize(p, i + l) > repl_size)
1857 break;
1858 }
1859 if (i != lead_repl_len)
1860 {
1861 mch_memmove(p + lead_repl_len, p + i,
1862 (size_t)(lead_len - i - (p - leader)));
1863 lead_len += lead_repl_len - i;
1864 }
1865 }
1866 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1867
1868 // Replace any remaining non-white chars in the old
1869 // leader by spaces. Keep Tabs, the indent must
1870 // remain the same.
1871 for (p += lead_repl_len; p < leader + lead_len; ++p)
1872 if (!VIM_ISWHITE(*p))
1873 {
1874 // Don't put a space before a TAB.
1875 if (p + 1 < leader + lead_len && p[1] == TAB)
1876 {
1877 --lead_len;
1878 mch_memmove(p, p + 1,
1879 (leader + lead_len) - p);
1880 }
1881 else
1882 {
1883 int l = (*mb_ptr2len)(p);
1884
1885 if (l > 1)
1886 {
1887 if (ptr2cells(p) > 1)
1888 {
1889 // Replace a double-wide char with
1890 // two spaces
1891 --l;
1892 *p++ = ' ';
1893 }
1894 mch_memmove(p + 1, p + l,
1895 (leader + lead_len) - p);
1896 lead_len -= l - 1;
1897 }
1898 *p = ' ';
1899 }
1900 }
1901 *p = NUL;
1902 }
1903
1904 // Recompute the indent, it may have changed.
1905 if (curbuf->b_p_ai
1906#ifdef FEAT_SMARTINDENT
1907 || do_si
1908#endif
1909 )
1910#ifdef FEAT_VARTABS
1911 newindent = get_indent_str_vtab(leader, curbuf->b_p_ts,
1912 curbuf->b_p_vts_array, FALSE);
1913#else
1914 newindent = get_indent_str(leader,
1915 (int)curbuf->b_p_ts, FALSE);
1916#endif
1917
1918 // Add the indent offset
1919 if (newindent + off < 0)
1920 {
1921 off = -newindent;
1922 newindent = 0;
1923 }
1924 else
1925 newindent += off;
1926
1927 // Correct trailing spaces for the shift, so that
1928 // alignment remains equal.
1929 while (off > 0 && lead_len > 0
1930 && leader[lead_len - 1] == ' ')
1931 {
1932 // Don't do it when there is a tab before the space
1933 if (vim_strchr(skipwhite(leader), '\t') != NULL)
1934 break;
1935 --lead_len;
1936 --off;
1937 }
1938
1939 // If the leader ends in white space, don't add an
1940 // extra space
1941 if (lead_len > 0 && VIM_ISWHITE(leader[lead_len - 1]))
1942 extra_space = FALSE;
1943 leader[lead_len] = NUL;
1944 }
1945
1946 if (extra_space)
1947 {
1948 leader[lead_len++] = ' ';
1949 leader[lead_len] = NUL;
1950 }
1951
1952 newcol = lead_len;
1953
1954 // if a new indent will be set below, remove the indent that
1955 // is in the comment leader
1956 if (newindent
1957#ifdef FEAT_SMARTINDENT
1958 || did_si
1959#endif
1960 )
1961 {
1962 while (lead_len && VIM_ISWHITE(*leader))
1963 {
1964 --lead_len;
1965 --newcol;
1966 ++leader;
1967 }
1968 }
1969
1970 }
1971#ifdef FEAT_SMARTINDENT
1972 did_si = can_si = FALSE;
1973#endif
1974 }
1975 else if (comment_end != NULL)
1976 {
1977 // We have finished a comment, so we don't use the leader.
1978 // If this was a C-comment and 'ai' or 'si' is set do a normal
1979 // indent to align with the line containing the start of the
1980 // comment.
1981 if (comment_end[0] == '*' && comment_end[1] == '/' &&
1982 (curbuf->b_p_ai
1983#ifdef FEAT_SMARTINDENT
1984 || do_si
1985#endif
1986 ))
1987 {
1988 old_cursor = curwin->w_cursor;
1989 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line);
1990 if ((pos = findmatch(NULL, NUL)) != NULL)
1991 {
1992 curwin->w_cursor.lnum = pos->lnum;
1993 newindent = get_indent();
1994 }
1995 curwin->w_cursor = old_cursor;
1996 }
1997 }
1998 }
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001999
2000 // (State == INSERT || State == REPLACE), only when dir == FORWARD
2001 if (p_extra != NULL)
2002 {
2003 *p_extra = saved_char; // restore char that NUL replaced
2004
2005 // When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first
2006 // non-blank.
2007 //
2008 // When in REPLACE mode, put the deleted blanks on the replace stack,
2009 // preceded by a NUL, so they can be put back when a BS is entered.
2010 if (REPLACE_NORMAL(State))
Bram Moolenaarc667da52019-11-30 20:52:27 +01002011 replace_push(NUL); // end of extra blanks
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002012 if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES))
2013 {
2014 while ((*p_extra == ' ' || *p_extra == '\t')
2015 && (!enc_utf8
2016 || !utf_iscomposing(utf_ptr2char(p_extra + 1))))
2017 {
2018 if (REPLACE_NORMAL(State))
2019 replace_push(*p_extra);
2020 ++p_extra;
2021 ++less_cols_off;
2022 }
2023 }
2024
2025 // columns for marks adjusted for removed columns
2026 less_cols = (int)(p_extra - saved_line);
2027 }
2028
2029 if (p_extra == NULL)
2030 p_extra = (char_u *)""; // append empty line
2031
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002032 // concatenate leader and p_extra, if there is a leader
2033 if (lead_len)
2034 {
2035 if (flags & OPENLINE_COM_LIST && second_line_indent > 0)
2036 {
2037 int i;
2038 int padding = second_line_indent
2039 - (newindent + (int)STRLEN(leader));
2040
2041 // Here whitespace is inserted after the comment char.
2042 // Below, set_indent(newindent, SIN_INSERT) will insert the
2043 // whitespace needed before the comment char.
2044 for (i = 0; i < padding; i++)
2045 {
2046 STRCAT(leader, " ");
2047 less_cols--;
2048 newcol++;
2049 }
2050 }
2051 STRCAT(leader, p_extra);
2052 p_extra = leader;
2053 did_ai = TRUE; // So truncating blanks works with comments
2054 less_cols -= lead_len;
2055 }
2056 else
2057 end_comment_pending = NUL; // turns out there was no leader
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002058
2059 old_cursor = curwin->w_cursor;
2060 if (dir == BACKWARD)
2061 --curwin->w_cursor.lnum;
2062 if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count)
2063 {
2064 if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE)
2065 == FAIL)
2066 goto theend;
2067 // Postpone calling changed_lines(), because it would mess up folding
2068 // with markers.
2069 // Skip mark_adjust when adding a line after the last one, there can't
2070 // be marks there. But still needed in diff mode.
2071 if (curwin->w_cursor.lnum + 1 < curbuf->b_ml.ml_line_count
2072#ifdef FEAT_DIFF
2073 || curwin->w_p_diff
2074#endif
2075 )
2076 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
2077 did_append = TRUE;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002078#ifdef FEAT_TEXT_PROP
2079 if ((State & INSERT) && !(State & VREPLACE_FLAG))
2080 // properties after the split move to the next line
2081 adjust_props_for_split(curwin->w_cursor.lnum, curwin->w_cursor.lnum,
2082 curwin->w_cursor.col + 1, 0);
2083#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002084 }
2085 else
2086 {
2087 // In VREPLACE mode we are starting to replace the next line.
2088 curwin->w_cursor.lnum++;
2089 if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed)
2090 {
2091 // In case we NL to a new line, BS to the previous one, and NL
2092 // again, we don't want to save the new line for undo twice.
Bram Moolenaarc667da52019-11-30 20:52:27 +01002093 (void)u_save_cursor(); // errors are ignored!
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002094 vr_lines_changed++;
2095 }
2096 ml_replace(curwin->w_cursor.lnum, p_extra, TRUE);
2097 changed_bytes(curwin->w_cursor.lnum, 0);
2098 curwin->w_cursor.lnum--;
2099 did_append = FALSE;
2100 }
2101
2102 if (newindent
2103#ifdef FEAT_SMARTINDENT
2104 || did_si
2105#endif
2106 )
2107 {
2108 ++curwin->w_cursor.lnum;
2109#ifdef FEAT_SMARTINDENT
2110 if (did_si)
2111 {
2112 int sw = (int)get_sw_value(curbuf);
2113
2114 if (p_sr)
2115 newindent -= newindent % sw;
2116 newindent += sw;
2117 }
2118#endif
2119 // Copy the indent
2120 if (curbuf->b_p_ci)
2121 {
2122 (void)copy_indent(newindent, saved_line);
2123
2124 // Set the 'preserveindent' option so that any further screwing
2125 // with the line doesn't entirely destroy our efforts to preserve
2126 // it. It gets restored at the function end.
2127 curbuf->b_p_pi = TRUE;
2128 }
2129 else
2130 (void)set_indent(newindent, SIN_INSERT);
2131 less_cols -= curwin->w_cursor.col;
2132
2133 ai_col = curwin->w_cursor.col;
2134
2135 // In REPLACE mode, for each character in the new indent, there must
2136 // be a NUL on the replace stack, for when it is deleted with BS
2137 if (REPLACE_NORMAL(State))
2138 for (n = 0; n < (int)curwin->w_cursor.col; ++n)
2139 replace_push(NUL);
2140 newcol += curwin->w_cursor.col;
2141#ifdef FEAT_SMARTINDENT
2142 if (no_si)
2143 did_si = FALSE;
2144#endif
2145 }
2146
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002147 // In REPLACE mode, for each character in the extra leader, there must be
2148 // a NUL on the replace stack, for when it is deleted with BS.
2149 if (REPLACE_NORMAL(State))
2150 while (lead_len-- > 0)
2151 replace_push(NUL);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002152
2153 curwin->w_cursor = old_cursor;
2154
2155 if (dir == FORWARD)
2156 {
2157 if (trunc_line || (State & INSERT))
2158 {
2159 // truncate current line at cursor
2160 saved_line[curwin->w_cursor.col] = NUL;
2161 // Remove trailing white space, unless OPENLINE_KEEPTRAIL used.
2162 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL))
2163 truncate_spaces(saved_line);
2164 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE);
2165 saved_line = NULL;
2166 if (did_append)
2167 {
2168 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
2169 curwin->w_cursor.lnum + 1, 1L);
2170 did_append = FALSE;
2171
2172 // Move marks after the line break to the new line.
2173 if (flags & OPENLINE_MARKFIX)
2174 mark_col_adjust(curwin->w_cursor.lnum,
2175 curwin->w_cursor.col + less_cols_off,
2176 1L, (long)-less_cols, 0);
2177 }
2178 else
2179 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
2180 }
2181
2182 // Put the cursor on the new line. Careful: the scrollup() above may
2183 // have moved w_cursor, we must use old_cursor.
2184 curwin->w_cursor.lnum = old_cursor.lnum + 1;
2185 }
2186 if (did_append)
2187 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L);
2188
2189 curwin->w_cursor.col = newcol;
2190 curwin->w_cursor.coladd = 0;
2191
2192#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2193 // In VREPLACE mode, we are handling the replace stack ourselves, so stop
2194 // fixthisline() from doing it (via change_indent()) by telling it we're in
2195 // normal INSERT mode.
2196 if (State & VREPLACE_FLAG)
2197 {
2198 vreplace_mode = State; // So we know to put things right later
2199 State = INSERT;
2200 }
2201 else
2202 vreplace_mode = 0;
2203#endif
2204#ifdef FEAT_LISP
2205 // May do lisp indenting.
2206 if (!p_paste
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002207 && leader == NULL
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002208 && curbuf->b_p_lisp
2209 && curbuf->b_p_ai)
2210 {
2211 fixthisline(get_lisp_indent);
2212 ai_col = (colnr_T)getwhitecols_curline();
2213 }
2214#endif
2215#ifdef FEAT_CINDENT
2216 // May do indenting after opening a new line.
2217 if (!p_paste
2218 && (curbuf->b_p_cin
2219# ifdef FEAT_EVAL
2220 || *curbuf->b_p_inde != NUL
2221# endif
2222 )
2223 && in_cinkeys(dir == FORWARD
2224 ? KEY_OPEN_FORW
2225 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum)))
2226 {
2227 do_c_expr_indent();
2228 ai_col = (colnr_T)getwhitecols_curline();
2229 }
2230#endif
2231#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2232 if (vreplace_mode != 0)
2233 State = vreplace_mode;
2234#endif
2235
2236 // Finally, VREPLACE gets the stuff on the new line, then puts back the
2237 // original line, and inserts the new stuff char by char, pushing old stuff
2238 // onto the replace stack (via ins_char()).
2239 if (State & VREPLACE_FLAG)
2240 {
2241 // Put new line in p_extra
2242 p_extra = vim_strsave(ml_get_curline());
2243 if (p_extra == NULL)
2244 goto theend;
2245
2246 // Put back original line
2247 ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
2248
2249 // Insert new stuff into line again
2250 curwin->w_cursor.col = 0;
2251 curwin->w_cursor.coladd = 0;
2252 ins_bytes(p_extra); // will call changed_bytes()
2253 vim_free(p_extra);
2254 next_line = NULL;
2255 }
2256
2257 retval = OK; // success!
2258theend:
2259 curbuf->b_p_pi = saved_pi;
2260 vim_free(saved_line);
2261 vim_free(next_line);
2262 vim_free(allocated);
2263 return retval;
2264}
2265
2266/*
2267 * Delete from cursor to end of line.
2268 * Caller must have prepared for undo.
2269 * If "fixpos" is TRUE fix the cursor position when done.
2270 *
2271 * Return FAIL for failure, OK otherwise.
2272 */
2273 int
2274truncate_line(int fixpos)
2275{
2276 char_u *newp;
2277 linenr_T lnum = curwin->w_cursor.lnum;
2278 colnr_T col = curwin->w_cursor.col;
2279
2280 if (col == 0)
2281 newp = vim_strsave((char_u *)"");
2282 else
2283 newp = vim_strnsave(ml_get(lnum), col);
2284
2285 if (newp == NULL)
2286 return FAIL;
2287
2288 ml_replace(lnum, newp, FALSE);
2289
2290 // mark the buffer as changed and prepare for displaying
2291 changed_bytes(lnum, curwin->w_cursor.col);
2292
2293 // If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2294 if (fixpos && curwin->w_cursor.col > 0)
2295 --curwin->w_cursor.col;
2296
2297 return OK;
2298}
2299
2300/*
2301 * Delete "nlines" lines at the cursor.
2302 * Saves the lines for undo first if "undo" is TRUE.
2303 */
2304 void
2305del_lines(long nlines, int undo)
2306{
2307 long n;
2308 linenr_T first = curwin->w_cursor.lnum;
2309
2310 if (nlines <= 0)
2311 return;
2312
2313 // save the deleted lines for undo
2314 if (undo && u_savedel(first, nlines) == FAIL)
2315 return;
2316
2317 for (n = 0; n < nlines; )
2318 {
2319 if (curbuf->b_ml.ml_flags & ML_EMPTY) // nothing to delete
2320 break;
2321
2322 ml_delete(first, TRUE);
2323 ++n;
2324
2325 // If we delete the last line in the file, stop
2326 if (first > curbuf->b_ml.ml_line_count)
2327 break;
2328 }
2329
2330 // Correct the cursor position before calling deleted_lines_mark(), it may
2331 // trigger a callback to display the cursor.
2332 curwin->w_cursor.col = 0;
2333 check_cursor_lnum();
2334
2335 // adjust marks, mark the buffer as changed and prepare for displaying
2336 deleted_lines_mark(first, n);
2337}