blob: 0f6164b04b69168dd8185211ea9db529e9d5d419 [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();
61 ui_delay(1000L, TRUE); // give the user time to think about it
62 }
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();
121 ui_delay(2000L, TRUE);
122 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 Moolenaara3347722019-05-11 21:14:24 +0200303 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
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;
344 int dummy;
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200345 typval_T argv[6];
346 listitem_T *li;
347 linenr_T start = MAXLNUM;
348 linenr_T end = 0;
349 linenr_T added = 0;
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200350 int save_updating_screen = updating_screen;
351 static int recursive = FALSE;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200352
Bram Moolenaardda41442019-05-16 22:11:47 +0200353 if (buf->b_recorded_changes == NULL // nothing changed
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200354 || buf->b_listener == NULL // no listeners
355 || recursive) // already busy
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200356 return;
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200357 recursive = TRUE;
358
359 // Block messages on channels from being handled, so that they don't make
360 // text changes here.
361 ++updating_screen;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200362
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200363 argv[0].v_type = VAR_NUMBER;
364 argv[0].vval.v_number = buf->b_fnum; // a:bufnr
365
366
Bram Moolenaardda41442019-05-16 22:11:47 +0200367 for (li = buf->b_recorded_changes->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200368 {
369 varnumber_T lnum;
370
371 lnum = dict_get_number(li->li_tv.vval.v_dict, (char_u *)"lnum");
372 if (start > lnum)
373 start = lnum;
374 lnum = dict_get_number(li->li_tv.vval.v_dict, (char_u *)"end");
375 if (lnum > end)
376 end = lnum;
377 added = dict_get_number(li->li_tv.vval.v_dict, (char_u *)"added");
378 }
379 argv[1].v_type = VAR_NUMBER;
380 argv[1].vval.v_number = start;
381 argv[2].v_type = VAR_NUMBER;
382 argv[2].vval.v_number = end;
383 argv[3].v_type = VAR_NUMBER;
384 argv[3].vval.v_number = added;
385
386 argv[4].v_type = VAR_LIST;
Bram Moolenaardda41442019-05-16 22:11:47 +0200387 argv[4].vval.v_list = buf->b_recorded_changes;
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200388 ++textlock;
389
390 for (lnr = buf->b_listener; lnr != NULL; lnr = lnr->lr_next)
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200391 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200392 call_callback(&lnr->lr_callback, -1, &rettv,
393 5, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200394 clear_tv(&rettv);
395 }
396
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200397 --textlock;
Bram Moolenaardda41442019-05-16 22:11:47 +0200398 list_unref(buf->b_recorded_changes);
399 buf->b_recorded_changes = NULL;
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200400
401 if (save_updating_screen)
402 updating_screen = TRUE;
403 else
404 after_updating_screen(TRUE);
405 recursive = FALSE;
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200406}
407#endif
408
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200409/*
410 * Common code for when a change was made.
411 * See changed_lines() for the arguments.
412 * Careful: may trigger autocommands that reload the buffer.
413 */
414 static void
415changed_common(
416 linenr_T lnum,
417 colnr_T col,
418 linenr_T lnume,
419 long xtra)
420{
421 win_T *wp;
422 tabpage_T *tp;
423 int i;
424#ifdef FEAT_JUMPLIST
425 int cols;
426 pos_T *p;
427 int add;
428#endif
429
430 // mark the buffer as modified
431 changed();
432
Bram Moolenaar6d2399b2019-05-11 19:14:16 +0200433#ifdef FEAT_EVAL
434 may_record_change(lnum, col, lnume, xtra);
435#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200436#ifdef FEAT_DIFF
437 if (curwin->w_p_diff && diff_internal())
438 curtab->tp_diff_update = TRUE;
439#endif
440
441 // set the '. mark
442 if (!cmdmod.keepjumps)
443 {
444 curbuf->b_last_change.lnum = lnum;
445 curbuf->b_last_change.col = col;
446
447#ifdef FEAT_JUMPLIST
448 // Create a new entry if a new undo-able change was started or we
449 // don't have an entry yet.
450 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
451 {
452 if (curbuf->b_changelistlen == 0)
453 add = TRUE;
454 else
455 {
456 // Don't create a new entry when the line number is the same
457 // as the last one and the column is not too far away. Avoids
458 // creating many entries for typing "xxxxx".
459 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
460 if (p->lnum != lnum)
461 add = TRUE;
462 else
463 {
464 cols = comp_textwidth(FALSE);
465 if (cols == 0)
466 cols = 79;
467 add = (p->col + cols < col || col + cols < p->col);
468 }
469 }
470 if (add)
471 {
472 // This is the first of a new sequence of undo-able changes
473 // and it's at some distance of the last change. Use a new
474 // position in the changelist.
475 curbuf->b_new_change = FALSE;
476
477 if (curbuf->b_changelistlen == JUMPLISTSIZE)
478 {
479 // changelist is full: remove oldest entry
480 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
481 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
482 sizeof(pos_T) * (JUMPLISTSIZE - 1));
483 FOR_ALL_TAB_WINDOWS(tp, wp)
484 {
485 // Correct position in changelist for other windows on
486 // this buffer.
487 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
488 --wp->w_changelistidx;
489 }
490 }
491 FOR_ALL_TAB_WINDOWS(tp, wp)
492 {
493 // For other windows, if the position in the changelist is
494 // at the end it stays at the end.
495 if (wp->w_buffer == curbuf
496 && wp->w_changelistidx == curbuf->b_changelistlen)
497 ++wp->w_changelistidx;
498 }
499 ++curbuf->b_changelistlen;
500 }
501 }
502 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
503 curbuf->b_last_change;
504 // The current window is always after the last change, so that "g,"
505 // takes you back to it.
506 curwin->w_changelistidx = curbuf->b_changelistlen;
507#endif
508 }
509
510 FOR_ALL_TAB_WINDOWS(tp, wp)
511 {
512 if (wp->w_buffer == curbuf)
513 {
514 // Mark this window to be redrawn later.
515 if (wp->w_redr_type < VALID)
516 wp->w_redr_type = VALID;
517
518 // Check if a change in the buffer has invalidated the cached
519 // values for the cursor.
520#ifdef FEAT_FOLDING
521 // Update the folds for this window. Can't postpone this, because
522 // a following operator might work on the whole fold: ">>dd".
523 foldUpdate(wp, lnum, lnume + xtra - 1);
524
525 // The change may cause lines above or below the change to become
526 // included in a fold. Set lnum/lnume to the first/last line that
527 // might be displayed differently.
528 // Set w_cline_folded here as an efficient way to update it when
529 // inserting lines just above a closed fold.
530 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
531 if (wp->w_cursor.lnum == lnum)
532 wp->w_cline_folded = i;
533 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
534 if (wp->w_cursor.lnum == lnume)
535 wp->w_cline_folded = i;
536
537 // If the changed line is in a range of previously folded lines,
538 // compare with the first line in that range.
539 if (wp->w_cursor.lnum <= lnum)
540 {
541 i = find_wl_entry(wp, lnum);
542 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
543 changed_line_abv_curs_win(wp);
544 }
545#endif
546
547 if (wp->w_cursor.lnum > lnum)
548 changed_line_abv_curs_win(wp);
549 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
550 changed_cline_bef_curs_win(wp);
551 if (wp->w_botline >= lnum)
552 {
553 // Assume that botline doesn't change (inserted lines make
554 // other lines scroll down below botline).
555 approximate_botline_win(wp);
556 }
557
558 // Check if any w_lines[] entries have become invalid.
559 // For entries below the change: Correct the lnums for
560 // inserted/deleted lines. Makes it possible to stop displaying
561 // after the change.
562 for (i = 0; i < wp->w_lines_valid; ++i)
563 if (wp->w_lines[i].wl_valid)
564 {
565 if (wp->w_lines[i].wl_lnum >= lnum)
566 {
567 if (wp->w_lines[i].wl_lnum < lnume)
568 {
569 // line included in change
570 wp->w_lines[i].wl_valid = FALSE;
571 }
572 else if (xtra != 0)
573 {
574 // line below change
575 wp->w_lines[i].wl_lnum += xtra;
576#ifdef FEAT_FOLDING
577 wp->w_lines[i].wl_lastlnum += xtra;
578#endif
579 }
580 }
581#ifdef FEAT_FOLDING
582 else if (wp->w_lines[i].wl_lastlnum >= lnum)
583 {
584 // change somewhere inside this range of folded lines,
585 // may need to be redrawn
586 wp->w_lines[i].wl_valid = FALSE;
587 }
588#endif
589 }
590
591#ifdef FEAT_FOLDING
592 // Take care of side effects for setting w_topline when folds have
593 // changed. Esp. when the buffer was changed in another window.
594 if (hasAnyFolding(wp))
595 set_topline(wp, wp->w_topline);
596#endif
597 // relative numbering may require updating more
598 if (wp->w_p_rnu)
599 redraw_win_later(wp, SOME_VALID);
600 }
601 }
602
603 // Call update_screen() later, which checks out what needs to be redrawn,
604 // since it notices b_mod_set and then uses b_mod_*.
605 if (must_redraw < VALID)
606 must_redraw = VALID;
607
608 // when the cursor line is changed always trigger CursorMoved
609 if (lnum <= curwin->w_cursor.lnum
610 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
611 last_cursormoved.lnum = 0;
612}
613
614 static void
615changedOneline(buf_T *buf, linenr_T lnum)
616{
617 if (buf->b_mod_set)
618 {
619 // find the maximum area that must be redisplayed
620 if (lnum < buf->b_mod_top)
621 buf->b_mod_top = lnum;
622 else if (lnum >= buf->b_mod_bot)
623 buf->b_mod_bot = lnum + 1;
624 }
625 else
626 {
627 // set the area that must be redisplayed to one line
628 buf->b_mod_set = TRUE;
629 buf->b_mod_top = lnum;
630 buf->b_mod_bot = lnum + 1;
631 buf->b_mod_xlines = 0;
632 }
633}
634
635/*
636 * Changed bytes within a single line for the current buffer.
637 * - marks the windows on this buffer to be redisplayed
638 * - marks the buffer changed by calling changed()
639 * - invalidates cached values
640 * Careful: may trigger autocommands that reload the buffer.
641 */
642 void
643changed_bytes(linenr_T lnum, colnr_T col)
644{
645 changedOneline(curbuf, lnum);
646 changed_common(lnum, col, lnum + 1, 0L);
647
648#ifdef FEAT_DIFF
649 // Diff highlighting in other diff windows may need to be updated too.
650 if (curwin->w_p_diff)
651 {
652 win_T *wp;
653 linenr_T wlnum;
654
655 FOR_ALL_WINDOWS(wp)
656 if (wp->w_p_diff && wp != curwin)
657 {
658 redraw_win_later(wp, VALID);
659 wlnum = diff_lnum_win(lnum, wp);
660 if (wlnum > 0)
661 changedOneline(wp->w_buffer, wlnum);
662 }
663 }
664#endif
665}
666
667/*
668 * Like changed_bytes() but also adjust text properties for "added" bytes.
669 * When "added" is negative text was deleted.
670 */
671 void
672inserted_bytes(linenr_T lnum, colnr_T col, int added UNUSED)
673{
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200674#ifdef FEAT_TEXT_PROP
675 if (curbuf->b_has_textprop && added != 0)
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200676 adjust_prop_columns(lnum, col, added, 0);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200677#endif
Bram Moolenaar45dd07f2019-05-15 22:45:37 +0200678
679 changed_bytes(lnum, col);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200680}
681
682/*
683 * Appended "count" lines below line "lnum" in the current buffer.
684 * Must be called AFTER the change and after mark_adjust().
685 * Takes care of marking the buffer to be redrawn and sets the changed flag.
686 */
687 void
688appended_lines(linenr_T lnum, long count)
689{
690 changed_lines(lnum + 1, 0, lnum + 1, count);
691}
692
693/*
694 * Like appended_lines(), but adjust marks first.
695 */
696 void
697appended_lines_mark(linenr_T lnum, long count)
698{
699 // Skip mark_adjust when adding a line after the last one, there can't
700 // be marks there. But it's still needed in diff mode.
701 if (lnum + count < curbuf->b_ml.ml_line_count
702#ifdef FEAT_DIFF
703 || curwin->w_p_diff
704#endif
705 )
706 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
707 changed_lines(lnum + 1, 0, lnum + 1, count);
708}
709
710/*
711 * Deleted "count" lines at line "lnum" in the current buffer.
712 * Must be called AFTER the change and after mark_adjust().
713 * Takes care of marking the buffer to be redrawn and sets the changed flag.
714 */
715 void
716deleted_lines(linenr_T lnum, long count)
717{
718 changed_lines(lnum, 0, lnum + count, -count);
719}
720
721/*
722 * Like deleted_lines(), but adjust marks first.
723 * Make sure the cursor is on a valid line before calling, a GUI callback may
724 * be triggered to display the cursor.
725 */
726 void
727deleted_lines_mark(linenr_T lnum, long count)
728{
729 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
730 changed_lines(lnum, 0, lnum + count, -count);
731}
732
733/*
734 * Marks the area to be redrawn after a change.
735 */
736 static void
737changed_lines_buf(
738 buf_T *buf,
739 linenr_T lnum, // first line with change
740 linenr_T lnume, // line below last changed line
741 long xtra) // number of extra lines (negative when deleting)
742{
743 if (buf->b_mod_set)
744 {
745 // find the maximum area that must be redisplayed
746 if (lnum < buf->b_mod_top)
747 buf->b_mod_top = lnum;
748 if (lnum < buf->b_mod_bot)
749 {
750 // adjust old bot position for xtra lines
751 buf->b_mod_bot += xtra;
752 if (buf->b_mod_bot < lnum)
753 buf->b_mod_bot = lnum;
754 }
755 if (lnume + xtra > buf->b_mod_bot)
756 buf->b_mod_bot = lnume + xtra;
757 buf->b_mod_xlines += xtra;
758 }
759 else
760 {
761 // set the area that must be redisplayed
762 buf->b_mod_set = TRUE;
763 buf->b_mod_top = lnum;
764 buf->b_mod_bot = lnume + xtra;
765 buf->b_mod_xlines = xtra;
766 }
767}
768
769/*
770 * Changed lines for the current buffer.
771 * Must be called AFTER the change and after mark_adjust().
772 * - mark the buffer changed by calling changed()
773 * - mark the windows on this buffer to be redisplayed
774 * - invalidate cached values
775 * "lnum" is the first line that needs displaying, "lnume" the first line
776 * below the changed lines (BEFORE the change).
777 * When only inserting lines, "lnum" and "lnume" are equal.
778 * Takes care of calling changed() and updating b_mod_*.
779 * Careful: may trigger autocommands that reload the buffer.
780 */
781 void
782changed_lines(
783 linenr_T lnum, // first line with change
784 colnr_T col, // column in first line with change
785 linenr_T lnume, // line below last changed line
786 long xtra) // number of extra lines (negative when deleting)
787{
788 changed_lines_buf(curbuf, lnum, lnume, xtra);
789
790#ifdef FEAT_DIFF
791 if (xtra == 0 && curwin->w_p_diff && !diff_internal())
792 {
793 // When the number of lines doesn't change then mark_adjust() isn't
794 // called and other diff buffers still need to be marked for
795 // displaying.
796 win_T *wp;
797 linenr_T wlnum;
798
799 FOR_ALL_WINDOWS(wp)
800 if (wp->w_p_diff && wp != curwin)
801 {
802 redraw_win_later(wp, VALID);
803 wlnum = diff_lnum_win(lnum, wp);
804 if (wlnum > 0)
805 changed_lines_buf(wp->w_buffer, wlnum,
806 lnume - lnum + wlnum, 0L);
807 }
808 }
809#endif
810
811 changed_common(lnum, col, lnume, xtra);
812}
813
814/*
815 * Called when the changed flag must be reset for buffer "buf".
816 * When "ff" is TRUE also reset 'fileformat'.
Bram Moolenaarc024b462019-06-08 18:07:21 +0200817 * When "always_inc_changedtick" is TRUE b:changedtick is incremented also when
818 * the changed flag was off.
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200819 */
820 void
Bram Moolenaarc024b462019-06-08 18:07:21 +0200821unchanged(buf_T *buf, int ff, int always_inc_changedtick)
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200822{
823 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
824 {
825 buf->b_changed = 0;
826 ml_setflags(buf);
827 if (ff)
828 save_file_ff(buf);
829 check_status(buf);
830 redraw_tabline = TRUE;
831#ifdef FEAT_TITLE
832 need_maketitle = TRUE; // set window title later
833#endif
Bram Moolenaarc024b462019-06-08 18:07:21 +0200834 ++CHANGEDTICK(buf);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200835 }
Bram Moolenaarc024b462019-06-08 18:07:21 +0200836 else if (always_inc_changedtick)
837 ++CHANGEDTICK(buf);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200838#ifdef FEAT_NETBEANS_INTG
839 netbeans_unmodified(buf);
840#endif
841}
842
843/*
844 * Insert string "p" at the cursor position. Stops at a NUL byte.
845 * Handles Replace mode and multi-byte characters.
846 */
847 void
848ins_bytes(char_u *p)
849{
850 ins_bytes_len(p, (int)STRLEN(p));
851}
852
853/*
854 * Insert string "p" with length "len" at the cursor position.
855 * Handles Replace mode and multi-byte characters.
856 */
857 void
858ins_bytes_len(char_u *p, int len)
859{
860 int i;
861 int n;
862
863 if (has_mbyte)
864 for (i = 0; i < len; i += n)
865 {
866 if (enc_utf8)
867 // avoid reading past p[len]
868 n = utfc_ptr2len_len(p + i, len - i);
869 else
870 n = (*mb_ptr2len)(p + i);
871 ins_char_bytes(p + i, n);
872 }
873 else
874 for (i = 0; i < len; ++i)
875 ins_char(p[i]);
876}
877
878/*
879 * Insert or replace a single character at the cursor position.
880 * When in REPLACE or VREPLACE mode, replace any existing character.
881 * Caller must have prepared for undo.
882 * For multi-byte characters we get the whole character, the caller must
883 * convert bytes to a character.
884 */
885 void
886ins_char(int c)
887{
888 char_u buf[MB_MAXBYTES + 1];
889 int n = (*mb_char2bytes)(c, buf);
890
891 // When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
892 // Happens for CTRL-Vu9900.
893 if (buf[0] == 0)
894 buf[0] = '\n';
895
896 ins_char_bytes(buf, n);
897}
898
899 void
900ins_char_bytes(char_u *buf, int charlen)
901{
902 int c = buf[0];
903 int newlen; // nr of bytes inserted
904 int oldlen; // nr of bytes deleted (0 when not replacing)
905 char_u *p;
906 char_u *newp;
907 char_u *oldp;
908 int linelen; // length of old line including NUL
909 colnr_T col;
910 linenr_T lnum = curwin->w_cursor.lnum;
911 int i;
912
913 // Break tabs if needed.
914 if (virtual_active() && curwin->w_cursor.coladd > 0)
915 coladvance_force(getviscol());
916
917 col = curwin->w_cursor.col;
918 oldp = ml_get(lnum);
919 linelen = (int)STRLEN(oldp) + 1;
920
921 // The lengths default to the values for when not replacing.
922 oldlen = 0;
923 newlen = charlen;
924
925 if (State & REPLACE_FLAG)
926 {
927 if (State & VREPLACE_FLAG)
928 {
929 colnr_T new_vcol = 0; // init for GCC
930 colnr_T vcol;
931 int old_list;
932
933 // Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
934 // Returns the old value of list, so when finished,
935 // curwin->w_p_list should be set back to this.
936 old_list = curwin->w_p_list;
937 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
938 curwin->w_p_list = FALSE;
939
940 // In virtual replace mode each character may replace one or more
941 // characters (zero if it's a TAB). Count the number of bytes to
942 // be deleted to make room for the new character, counting screen
943 // cells. May result in adding spaces to fill a gap.
944 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
945 new_vcol = vcol + chartabsize(buf, vcol);
946 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
947 {
948 vcol += chartabsize(oldp + col + oldlen, vcol);
949 // Don't need to remove a TAB that takes us to the right
950 // position.
951 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
952 break;
953 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
954 // Deleted a bit too much, insert spaces.
955 if (vcol > new_vcol)
956 newlen += vcol - new_vcol;
957 }
958 curwin->w_p_list = old_list;
959 }
960 else if (oldp[col] != NUL)
961 {
962 // normal replace
963 oldlen = (*mb_ptr2len)(oldp + col);
964 }
965
966
967 // Push the replaced bytes onto the replace stack, so that they can be
968 // put back when BS is used. The bytes of a multi-byte character are
969 // done the other way around, so that the first byte is popped off
970 // first (it tells the byte length of the character).
971 replace_push(NUL);
972 for (i = 0; i < oldlen; ++i)
973 {
974 if (has_mbyte)
975 i += replace_push_mb(oldp + col + i) - 1;
976 else
977 replace_push(oldp[col + i]);
978 }
979 }
980
Bram Moolenaar964b3742019-05-24 18:54:09 +0200981 newp = alloc(linelen + newlen - oldlen);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +0200982 if (newp == NULL)
983 return;
984
985 // Copy bytes before the cursor.
986 if (col > 0)
987 mch_memmove(newp, oldp, (size_t)col);
988
989 // Copy bytes after the changed character(s).
990 p = newp + col;
991 if (linelen > col + oldlen)
992 mch_memmove(p + newlen, oldp + col + oldlen,
993 (size_t)(linelen - col - oldlen));
994
995 // Insert or overwrite the new character.
996 mch_memmove(p, buf, charlen);
997 i = charlen;
998
999 // Fill with spaces when necessary.
1000 while (i < newlen)
1001 p[i++] = ' ';
1002
1003 // Replace the line in the buffer.
1004 ml_replace(lnum, newp, FALSE);
1005
1006 // mark the buffer as changed and prepare for displaying
1007 inserted_bytes(lnum, col, newlen - oldlen);
1008
1009 // If we're in Insert or Replace mode and 'showmatch' is set, then briefly
1010 // show the match for right parens and braces.
1011 if (p_sm && (State & INSERT)
1012 && msg_silent == 0
1013#ifdef FEAT_INS_EXPAND
1014 && !ins_compl_active()
1015#endif
1016 )
1017 {
1018 if (has_mbyte)
1019 showmatch(mb_ptr2char(buf));
1020 else
1021 showmatch(c);
1022 }
1023
1024#ifdef FEAT_RIGHTLEFT
1025 if (!p_ri || (State & REPLACE_FLAG))
1026#endif
1027 {
1028 // Normal insert: move cursor right
1029 curwin->w_cursor.col += charlen;
1030 }
1031
1032 // TODO: should try to update w_row here, to avoid recomputing it later.
1033}
1034
1035/*
1036 * Insert a string at the cursor position.
1037 * Note: Does NOT handle Replace mode.
1038 * Caller must have prepared for undo.
1039 */
1040 void
1041ins_str(char_u *s)
1042{
1043 char_u *oldp, *newp;
1044 int newlen = (int)STRLEN(s);
1045 int oldlen;
1046 colnr_T col;
1047 linenr_T lnum = curwin->w_cursor.lnum;
1048
1049 if (virtual_active() && curwin->w_cursor.coladd > 0)
1050 coladvance_force(getviscol());
1051
1052 col = curwin->w_cursor.col;
1053 oldp = ml_get(lnum);
1054 oldlen = (int)STRLEN(oldp);
1055
Bram Moolenaar964b3742019-05-24 18:54:09 +02001056 newp = alloc(oldlen + newlen + 1);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001057 if (newp == NULL)
1058 return;
1059 if (col > 0)
1060 mch_memmove(newp, oldp, (size_t)col);
1061 mch_memmove(newp + col, s, (size_t)newlen);
1062 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
1063 ml_replace(lnum, newp, FALSE);
1064 inserted_bytes(lnum, col, newlen);
1065 curwin->w_cursor.col += newlen;
1066}
1067
1068/*
1069 * Delete one character under the cursor.
1070 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
1071 * Caller must have prepared for undo.
1072 *
1073 * return FAIL for failure, OK otherwise
1074 */
1075 int
1076del_char(int fixpos)
1077{
1078 if (has_mbyte)
1079 {
1080 // Make sure the cursor is at the start of a character.
1081 mb_adjust_cursor();
1082 if (*ml_get_cursor() == NUL)
1083 return FAIL;
1084 return del_chars(1L, fixpos);
1085 }
1086 return del_bytes(1L, fixpos, TRUE);
1087}
1088
1089/*
1090 * Like del_bytes(), but delete characters instead of bytes.
1091 */
1092 int
1093del_chars(long count, int fixpos)
1094{
1095 long bytes = 0;
1096 long i;
1097 char_u *p;
1098 int l;
1099
1100 p = ml_get_cursor();
1101 for (i = 0; i < count && *p != NUL; ++i)
1102 {
1103 l = (*mb_ptr2len)(p);
1104 bytes += l;
1105 p += l;
1106 }
1107 return del_bytes(bytes, fixpos, TRUE);
1108}
1109
1110/*
1111 * Delete "count" bytes under the cursor.
1112 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
1113 * Caller must have prepared for undo.
1114 *
1115 * Return FAIL for failure, OK otherwise.
1116 */
1117 int
1118del_bytes(
1119 long count,
1120 int fixpos_arg,
1121 int use_delcombine UNUSED) // 'delcombine' option applies
1122{
1123 char_u *oldp, *newp;
1124 colnr_T oldlen;
1125 colnr_T newlen;
1126 linenr_T lnum = curwin->w_cursor.lnum;
1127 colnr_T col = curwin->w_cursor.col;
1128 int alloc_newp;
1129 long movelen;
1130 int fixpos = fixpos_arg;
1131
1132 oldp = ml_get(lnum);
1133 oldlen = (int)STRLEN(oldp);
1134
1135 // Can't do anything when the cursor is on the NUL after the line.
1136 if (col >= oldlen)
1137 return FAIL;
1138
1139 // If "count" is zero there is nothing to do.
1140 if (count == 0)
1141 return OK;
1142
1143 // If "count" is negative the caller must be doing something wrong.
1144 if (count < 1)
1145 {
1146 siemsg("E950: Invalid count for del_bytes(): %ld", count);
1147 return FAIL;
1148 }
1149
1150 // If 'delcombine' is set and deleting (less than) one character, only
1151 // delete the last combining character.
1152 if (p_deco && use_delcombine && enc_utf8
1153 && utfc_ptr2len(oldp + col) >= count)
1154 {
1155 int cc[MAX_MCO];
1156 int n;
1157
1158 (void)utfc_ptr2char(oldp + col, cc);
1159 if (cc[0] != NUL)
1160 {
1161 // Find the last composing char, there can be several.
1162 n = col;
1163 do
1164 {
1165 col = n;
1166 count = utf_ptr2len(oldp + n);
1167 n += count;
1168 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
1169 fixpos = 0;
1170 }
1171 }
1172
1173 // When count is too big, reduce it.
1174 movelen = (long)oldlen - (long)col - count + 1; // includes trailing NUL
1175 if (movelen <= 1)
1176 {
1177 // If we just took off the last character of a non-blank line, and
1178 // fixpos is TRUE, we don't want to end up positioned at the NUL,
1179 // unless "restart_edit" is set or 'virtualedit' contains "onemore".
1180 if (col > 0 && fixpos && restart_edit == 0
1181 && (ve_flags & VE_ONEMORE) == 0)
1182 {
1183 --curwin->w_cursor.col;
1184 curwin->w_cursor.coladd = 0;
1185 if (has_mbyte)
1186 curwin->w_cursor.col -=
1187 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
1188 }
1189 count = oldlen - col;
1190 movelen = 1;
1191 }
1192 newlen = oldlen - count;
1193
1194 // If the old line has been allocated the deletion can be done in the
1195 // existing line. Otherwise a new line has to be allocated
1196 // Can't do this when using Netbeans, because we would need to invoke
1197 // netbeans_removed(), which deallocates the line. Let ml_replace() take
1198 // care of notifying Netbeans.
1199#ifdef FEAT_NETBEANS_INTG
1200 if (netbeans_active())
1201 alloc_newp = TRUE;
1202 else
1203#endif
1204 alloc_newp = !ml_line_alloced(); // check if oldp was allocated
1205 if (!alloc_newp)
1206 newp = oldp; // use same allocated memory
1207 else
1208 { // need to allocate a new line
Bram Moolenaar964b3742019-05-24 18:54:09 +02001209 newp = alloc(newlen + 1);
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02001210 if (newp == NULL)
1211 return FAIL;
1212 mch_memmove(newp, oldp, (size_t)col);
1213 }
1214 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
1215 if (alloc_newp)
1216 ml_replace(lnum, newp, FALSE);
1217#ifdef FEAT_TEXT_PROP
1218 else
1219 {
1220 // Also move any following text properties.
1221 if (oldlen + 1 < curbuf->b_ml.ml_line_len)
1222 mch_memmove(newp + newlen + 1, oldp + oldlen + 1,
1223 (size_t)curbuf->b_ml.ml_line_len - oldlen - 1);
1224 curbuf->b_ml.ml_line_len -= count;
1225 }
1226#endif
1227
1228 // mark the buffer as changed and prepare for displaying
1229 inserted_bytes(lnum, curwin->w_cursor.col, -count);
1230
1231 return OK;
1232}
1233
1234/*
1235 * Copy the indent from ptr to the current line (and fill to size)
1236 * Leaves the cursor on the first non-blank in the line.
1237 * Returns TRUE if the line was changed.
1238 */
1239 static int
1240copy_indent(int size, char_u *src)
1241{
1242 char_u *p = NULL;
1243 char_u *line = NULL;
1244 char_u *s;
1245 int todo;
1246 int ind_len;
1247 int line_len = 0;
1248 int tab_pad;
1249 int ind_done;
1250 int round;
1251#ifdef FEAT_VARTABS
1252 int ind_col;
1253#endif
1254
1255 // Round 1: compute the number of characters needed for the indent
1256 // Round 2: copy the characters.
1257 for (round = 1; round <= 2; ++round)
1258 {
1259 todo = size;
1260 ind_len = 0;
1261 ind_done = 0;
1262#ifdef FEAT_VARTABS
1263 ind_col = 0;
1264#endif
1265 s = src;
1266
1267 // Count/copy the usable portion of the source line
1268 while (todo > 0 && VIM_ISWHITE(*s))
1269 {
1270 if (*s == TAB)
1271 {
1272#ifdef FEAT_VARTABS
1273 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
1274 curbuf->b_p_vts_array);
1275#else
1276 tab_pad = (int)curbuf->b_p_ts
1277 - (ind_done % (int)curbuf->b_p_ts);
1278#endif
1279 // Stop if this tab will overshoot the target
1280 if (todo < tab_pad)
1281 break;
1282 todo -= tab_pad;
1283 ind_done += tab_pad;
1284#ifdef FEAT_VARTABS
1285 ind_col += tab_pad;
1286#endif
1287 }
1288 else
1289 {
1290 --todo;
1291 ++ind_done;
1292#ifdef FEAT_VARTABS
1293 ++ind_col;
1294#endif
1295 }
1296 ++ind_len;
1297 if (p != NULL)
1298 *p++ = *s;
1299 ++s;
1300 }
1301
1302 // Fill to next tabstop with a tab, if possible
1303#ifdef FEAT_VARTABS
1304 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
1305 curbuf->b_p_vts_array);
1306#else
1307 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
1308#endif
1309 if (todo >= tab_pad && !curbuf->b_p_et)
1310 {
1311 todo -= tab_pad;
1312 ++ind_len;
1313#ifdef FEAT_VARTABS
1314 ind_col += tab_pad;
1315#endif
1316 if (p != NULL)
1317 *p++ = TAB;
1318 }
1319
1320 // Add tabs required for indent
1321 if (!curbuf->b_p_et)
1322 {
1323#ifdef FEAT_VARTABS
1324 for (;;)
1325 {
1326 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
1327 curbuf->b_p_vts_array);
1328 if (todo < tab_pad)
1329 break;
1330 todo -= tab_pad;
1331 ++ind_len;
1332 ind_col += tab_pad;
1333 if (p != NULL)
1334 *p++ = TAB;
1335 }
1336#else
1337 while (todo >= (int)curbuf->b_p_ts)
1338 {
1339 todo -= (int)curbuf->b_p_ts;
1340 ++ind_len;
1341 if (p != NULL)
1342 *p++ = TAB;
1343 }
1344#endif
1345 }
1346
1347 // Count/add spaces required for indent
1348 while (todo > 0)
1349 {
1350 --todo;
1351 ++ind_len;
1352 if (p != NULL)
1353 *p++ = ' ';
1354 }
1355
1356 if (p == NULL)
1357 {
1358 // Allocate memory for the result: the copied indent, new indent
1359 // and the rest of the line.
1360 line_len = (int)STRLEN(ml_get_curline()) + 1;
1361 line = alloc(ind_len + line_len);
1362 if (line == NULL)
1363 return FALSE;
1364 p = line;
1365 }
1366 }
1367
1368 // Append the original line
1369 mch_memmove(p, ml_get_curline(), (size_t)line_len);
1370
1371 // Replace the line
1372 ml_replace(curwin->w_cursor.lnum, line, FALSE);
1373
1374 // Put the cursor after the indent.
1375 curwin->w_cursor.col = ind_len;
1376 return TRUE;
1377}
1378
1379/*
1380 * open_line: Add a new line below or above the current line.
1381 *
1382 * For VREPLACE mode, we only add a new line when we get to the end of the
1383 * file, otherwise we just start replacing the next line.
1384 *
1385 * Caller must take care of undo. Since VREPLACE may affect any number of
1386 * lines however, it may call u_save_cursor() again when starting to change a
1387 * new line.
1388 * "flags": OPENLINE_DELSPACES delete spaces after cursor
1389 * OPENLINE_DO_COM format comments
1390 * OPENLINE_KEEPTRAIL keep trailing spaces
1391 * OPENLINE_MARKFIX adjust mark positions after the line break
1392 * OPENLINE_COM_LIST format comments with list or 2nd line indent
1393 *
1394 * "second_line_indent": indent for after ^^D in Insert mode or if flag
1395 * OPENLINE_COM_LIST
1396 *
1397 * Return OK for success, FAIL for failure
1398 */
1399 int
1400open_line(
1401 int dir, // FORWARD or BACKWARD
1402 int flags,
1403 int second_line_indent)
1404{
1405 char_u *saved_line; // copy of the original line
1406 char_u *next_line = NULL; // copy of the next line
1407 char_u *p_extra = NULL; // what goes to next line
1408 int less_cols = 0; // less columns for mark in new line
1409 int less_cols_off = 0; // columns to skip for mark adjust
1410 pos_T old_cursor; // old cursor position
1411 int newcol = 0; // new cursor column
1412 int newindent = 0; // auto-indent of the new line
1413 int n;
1414 int trunc_line = FALSE; // truncate current line afterwards
1415 int retval = FAIL; // return value
1416#ifdef FEAT_COMMENTS
1417 int extra_len = 0; // length of p_extra string
1418 int lead_len; // length of comment leader
1419 char_u *lead_flags; // position in 'comments' for comment leader
1420 char_u *leader = NULL; // copy of comment leader
1421#endif
1422 char_u *allocated = NULL; // allocated memory
1423 char_u *p;
1424 int saved_char = NUL; // init for GCC
1425#if defined(FEAT_SMARTINDENT) || defined(FEAT_COMMENTS)
1426 pos_T *pos;
1427#endif
1428#ifdef FEAT_SMARTINDENT
1429 int do_si = (!p_paste && curbuf->b_p_si
1430# ifdef FEAT_CINDENT
1431 && !curbuf->b_p_cin
1432# endif
1433# ifdef FEAT_EVAL
1434 && *curbuf->b_p_inde == NUL
1435# endif
1436 );
1437 int no_si = FALSE; // reset did_si afterwards
1438 int first_char = NUL; // init for GCC
1439#endif
1440#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1441 int vreplace_mode;
1442#endif
1443 int did_append; // appended a new line
1444 int saved_pi = curbuf->b_p_pi; // copy of preserveindent setting
1445
1446 // make a copy of the current line so we can mess with it
1447 saved_line = vim_strsave(ml_get_curline());
1448 if (saved_line == NULL) /* out of memory! */
1449 return FALSE;
1450
1451 if (State & VREPLACE_FLAG)
1452 {
1453 // With VREPLACE we make a copy of the next line, which we will be
1454 // starting to replace. First make the new line empty and let vim play
1455 // with the indenting and comment leader to its heart's content. Then
1456 // we grab what it ended up putting on the new line, put back the
1457 // original line, and call ins_char() to put each new character onto
1458 // the line, replacing what was there before and pushing the right
1459 // stuff onto the replace stack. -- webb.
1460 if (curwin->w_cursor.lnum < orig_line_count)
1461 next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1));
1462 else
1463 next_line = vim_strsave((char_u *)"");
1464 if (next_line == NULL) // out of memory!
1465 goto theend;
1466
1467 // In VREPLACE mode, a NL replaces the rest of the line, and starts
1468 // replacing the next line, so push all of the characters left on the
1469 // line onto the replace stack. We'll push any other characters that
1470 // might be replaced at the start of the next line (due to autoindent
1471 // etc) a bit later.
1472 replace_push(NUL); // Call twice because BS over NL expects it
1473 replace_push(NUL);
1474 p = saved_line + curwin->w_cursor.col;
1475 while (*p != NUL)
1476 {
1477 if (has_mbyte)
1478 p += replace_push_mb(p);
1479 else
1480 replace_push(*p++);
1481 }
1482 saved_line[curwin->w_cursor.col] = NUL;
1483 }
1484
1485 if ((State & INSERT) && !(State & VREPLACE_FLAG))
1486 {
1487 p_extra = saved_line + curwin->w_cursor.col;
1488#ifdef FEAT_SMARTINDENT
1489 if (do_si) // need first char after new line break
1490 {
1491 p = skipwhite(p_extra);
1492 first_char = *p;
1493 }
1494#endif
1495#ifdef FEAT_COMMENTS
1496 extra_len = (int)STRLEN(p_extra);
1497#endif
1498 saved_char = *p_extra;
1499 *p_extra = NUL;
1500 }
1501
1502 u_clearline(); // cannot do "U" command when adding lines
1503#ifdef FEAT_SMARTINDENT
1504 did_si = FALSE;
1505#endif
1506 ai_col = 0;
1507
1508 // If we just did an auto-indent, then we didn't type anything on
1509 // the prior line, and it should be truncated. Do this even if 'ai' is not
1510 // set because automatically inserting a comment leader also sets did_ai.
1511 if (dir == FORWARD && did_ai)
1512 trunc_line = TRUE;
1513
1514 // If 'autoindent' and/or 'smartindent' is set, try to figure out what
1515 // indent to use for the new line.
1516 if (curbuf->b_p_ai
1517#ifdef FEAT_SMARTINDENT
1518 || do_si
1519#endif
1520 )
1521 {
1522 // count white space on current line
1523#ifdef FEAT_VARTABS
1524 newindent = get_indent_str_vtab(saved_line, curbuf->b_p_ts,
1525 curbuf->b_p_vts_array, FALSE);
1526#else
1527 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts, FALSE);
1528#endif
1529 if (newindent == 0 && !(flags & OPENLINE_COM_LIST))
1530 newindent = second_line_indent; // for ^^D command in insert mode
1531
1532#ifdef FEAT_SMARTINDENT
1533 // Do smart indenting.
1534 // In insert/replace mode (only when dir == FORWARD)
1535 // we may move some text to the next line. If it starts with '{'
1536 // don't add an indent. Fixes inserting a NL before '{' in line
1537 // "if (condition) {"
1538 if (!trunc_line && do_si && *saved_line != NUL
1539 && (p_extra == NULL || first_char != '{'))
1540 {
1541 char_u *ptr;
1542 char_u last_char;
1543
1544 old_cursor = curwin->w_cursor;
1545 ptr = saved_line;
1546# ifdef FEAT_COMMENTS
1547 if (flags & OPENLINE_DO_COM)
1548 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
1549 else
1550 lead_len = 0;
1551# endif
1552 if (dir == FORWARD)
1553 {
1554 // Skip preprocessor directives, unless they are
1555 // recognised as comments.
1556 if (
1557# ifdef FEAT_COMMENTS
1558 lead_len == 0 &&
1559# endif
1560 ptr[0] == '#')
1561 {
1562 while (ptr[0] == '#' && curwin->w_cursor.lnum > 1)
1563 ptr = ml_get(--curwin->w_cursor.lnum);
1564 newindent = get_indent();
1565 }
1566# ifdef FEAT_COMMENTS
1567 if (flags & OPENLINE_DO_COM)
1568 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
1569 else
1570 lead_len = 0;
1571 if (lead_len > 0)
1572 {
1573 // This case gets the following right:
1574 // /*
1575 // * A comment (read '\' as '/').
1576 // */
1577 // #define IN_THE_WAY
1578 // This should line up here;
1579 p = skipwhite(ptr);
1580 if (p[0] == '/' && p[1] == '*')
1581 p++;
1582 if (p[0] == '*')
1583 {
1584 for (p++; *p; p++)
1585 {
1586 if (p[0] == '/' && p[-1] == '*')
1587 {
1588 // End of C comment, indent should line up
1589 // with the line containing the start of
1590 // the comment
1591 curwin->w_cursor.col = (colnr_T)(p - ptr);
1592 if ((pos = findmatch(NULL, NUL)) != NULL)
1593 {
1594 curwin->w_cursor.lnum = pos->lnum;
1595 newindent = get_indent();
1596 }
1597 }
1598 }
1599 }
1600 }
1601 else // Not a comment line
1602# endif
1603 {
1604 // Find last non-blank in line
1605 p = ptr + STRLEN(ptr) - 1;
1606 while (p > ptr && VIM_ISWHITE(*p))
1607 --p;
1608 last_char = *p;
1609
1610 // find the character just before the '{' or ';'
1611 if (last_char == '{' || last_char == ';')
1612 {
1613 if (p > ptr)
1614 --p;
1615 while (p > ptr && VIM_ISWHITE(*p))
1616 --p;
1617 }
1618 // Try to catch lines that are split over multiple
1619 // lines. eg:
1620 // if (condition &&
1621 // condition) {
1622 // Should line up here!
1623 // }
1624 if (*p == ')')
1625 {
1626 curwin->w_cursor.col = (colnr_T)(p - ptr);
1627 if ((pos = findmatch(NULL, '(')) != NULL)
1628 {
1629 curwin->w_cursor.lnum = pos->lnum;
1630 newindent = get_indent();
1631 ptr = ml_get_curline();
1632 }
1633 }
1634 // If last character is '{' do indent, without
1635 // checking for "if" and the like.
1636 if (last_char == '{')
1637 {
1638 did_si = TRUE; // do indent
1639 no_si = TRUE; // don't delete it when '{' typed
1640 }
1641 // Look for "if" and the like, use 'cinwords'.
1642 // Don't do this if the previous line ended in ';' or
1643 // '}'.
1644 else if (last_char != ';' && last_char != '}'
1645 && cin_is_cinword(ptr))
1646 did_si = TRUE;
1647 }
1648 }
1649 else // dir == BACKWARD
1650 {
1651 // Skip preprocessor directives, unless they are
1652 // recognised as comments.
1653 if (
1654# ifdef FEAT_COMMENTS
1655 lead_len == 0 &&
1656# endif
1657 ptr[0] == '#')
1658 {
1659 int was_backslashed = FALSE;
1660
1661 while ((ptr[0] == '#' || was_backslashed) &&
1662 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
1663 {
1664 if (*ptr && ptr[STRLEN(ptr) - 1] == '\\')
1665 was_backslashed = TRUE;
1666 else
1667 was_backslashed = FALSE;
1668 ptr = ml_get(++curwin->w_cursor.lnum);
1669 }
1670 if (was_backslashed)
1671 newindent = 0; // Got to end of file
1672 else
1673 newindent = get_indent();
1674 }
1675 p = skipwhite(ptr);
1676 if (*p == '}') // if line starts with '}': do indent
1677 did_si = TRUE;
1678 else // can delete indent when '{' typed
1679 can_si_back = TRUE;
1680 }
1681 curwin->w_cursor = old_cursor;
1682 }
1683 if (do_si)
1684 can_si = TRUE;
1685#endif // FEAT_SMARTINDENT
1686
1687 did_ai = TRUE;
1688 }
1689
1690#ifdef FEAT_COMMENTS
1691 // Find out if the current line starts with a comment leader.
1692 // This may then be inserted in front of the new line.
1693 end_comment_pending = NUL;
1694 if (flags & OPENLINE_DO_COM)
1695 lead_len = get_leader_len(saved_line, &lead_flags,
1696 dir == BACKWARD, TRUE);
1697 else
1698 lead_len = 0;
1699 if (lead_len > 0)
1700 {
1701 char_u *lead_repl = NULL; // replaces comment leader
1702 int lead_repl_len = 0; // length of *lead_repl
1703 char_u lead_middle[COM_MAX_LEN]; // middle-comment string
1704 char_u lead_end[COM_MAX_LEN]; // end-comment string
1705 char_u *comment_end = NULL; // where lead_end has been found
1706 int extra_space = FALSE; // append extra space
1707 int current_flag;
1708 int require_blank = FALSE; // requires blank after middle
1709 char_u *p2;
1710
1711 // If the comment leader has the start, middle or end flag, it may not
1712 // be used or may be replaced with the middle leader.
1713 for (p = lead_flags; *p && *p != ':'; ++p)
1714 {
1715 if (*p == COM_BLANK)
1716 {
1717 require_blank = TRUE;
1718 continue;
1719 }
1720 if (*p == COM_START || *p == COM_MIDDLE)
1721 {
1722 current_flag = *p;
1723 if (*p == COM_START)
1724 {
1725 // Doing "O" on a start of comment does not insert leader.
1726 if (dir == BACKWARD)
1727 {
1728 lead_len = 0;
1729 break;
1730 }
1731
1732 // find start of middle part
1733 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
1734 require_blank = FALSE;
1735 }
1736
1737 // Isolate the strings of the middle and end leader.
1738 while (*p && p[-1] != ':') /* find end of middle flags */
1739 {
1740 if (*p == COM_BLANK)
1741 require_blank = TRUE;
1742 ++p;
1743 }
1744 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
1745
1746 while (*p && p[-1] != ':') // find end of end flags
1747 {
1748 // Check whether we allow automatic ending of comments
1749 if (*p == COM_AUTO_END)
1750 end_comment_pending = -1; // means we want to set it
1751 ++p;
1752 }
1753 n = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
1754
1755 if (end_comment_pending == -1) // we can set it now
1756 end_comment_pending = lead_end[n - 1];
1757
1758 // If the end of the comment is in the same line, don't use
1759 // the comment leader.
1760 if (dir == FORWARD)
1761 {
1762 for (p = saved_line + lead_len; *p; ++p)
1763 if (STRNCMP(p, lead_end, n) == 0)
1764 {
1765 comment_end = p;
1766 lead_len = 0;
1767 break;
1768 }
1769 }
1770
1771 // Doing "o" on a start of comment inserts the middle leader.
1772 if (lead_len > 0)
1773 {
1774 if (current_flag == COM_START)
1775 {
1776 lead_repl = lead_middle;
1777 lead_repl_len = (int)STRLEN(lead_middle);
1778 }
1779
1780 // If we have hit RETURN immediately after the start
1781 // comment leader, then put a space after the middle
1782 // comment leader on the next line.
1783 if (!VIM_ISWHITE(saved_line[lead_len - 1])
1784 && ((p_extra != NULL
1785 && (int)curwin->w_cursor.col == lead_len)
1786 || (p_extra == NULL
1787 && saved_line[lead_len] == NUL)
1788 || require_blank))
1789 extra_space = TRUE;
1790 }
1791 break;
1792 }
1793 if (*p == COM_END)
1794 {
1795 // Doing "o" on the end of a comment does not insert leader.
1796 // Remember where the end is, might want to use it to find the
1797 // start (for C-comments).
1798 if (dir == FORWARD)
1799 {
1800 comment_end = skipwhite(saved_line);
1801 lead_len = 0;
1802 break;
1803 }
1804
1805 // Doing "O" on the end of a comment inserts the middle leader.
1806 // Find the string for the middle leader, searching backwards.
1807 while (p > curbuf->b_p_com && *p != ',')
1808 --p;
1809 for (lead_repl = p; lead_repl > curbuf->b_p_com
1810 && lead_repl[-1] != ':'; --lead_repl)
1811 ;
1812 lead_repl_len = (int)(p - lead_repl);
1813
1814 // We can probably always add an extra space when doing "O" on
1815 // the comment-end
1816 extra_space = TRUE;
1817
1818 // Check whether we allow automatic ending of comments
1819 for (p2 = p; *p2 && *p2 != ':'; p2++)
1820 {
1821 if (*p2 == COM_AUTO_END)
1822 end_comment_pending = -1; // means we want to set it
1823 }
1824 if (end_comment_pending == -1)
1825 {
1826 // Find last character in end-comment string
1827 while (*p2 && *p2 != ',')
1828 p2++;
1829 end_comment_pending = p2[-1];
1830 }
1831 break;
1832 }
1833 if (*p == COM_FIRST)
1834 {
1835 // Comment leader for first line only: Don't repeat leader
1836 // when using "O", blank out leader when using "o".
1837 if (dir == BACKWARD)
1838 lead_len = 0;
1839 else
1840 {
1841 lead_repl = (char_u *)"";
1842 lead_repl_len = 0;
1843 }
1844 break;
1845 }
1846 }
1847 if (lead_len)
1848 {
1849 // allocate buffer (may concatenate p_extra later)
1850 leader = alloc(lead_len + lead_repl_len + extra_space + extra_len
1851 + (second_line_indent > 0 ? second_line_indent : 0) + 1);
1852 allocated = leader; // remember to free it later
1853
1854 if (leader == NULL)
1855 lead_len = 0;
1856 else
1857 {
1858 vim_strncpy(leader, saved_line, lead_len);
1859
1860 // Replace leader with lead_repl, right or left adjusted
1861 if (lead_repl != NULL)
1862 {
1863 int c = 0;
1864 int off = 0;
1865
1866 for (p = lead_flags; *p != NUL && *p != ':'; )
1867 {
1868 if (*p == COM_RIGHT || *p == COM_LEFT)
1869 c = *p++;
1870 else if (VIM_ISDIGIT(*p) || *p == '-')
1871 off = getdigits(&p);
1872 else
1873 ++p;
1874 }
1875 if (c == COM_RIGHT) // right adjusted leader
1876 {
1877 // find last non-white in the leader to line up with
1878 for (p = leader + lead_len - 1; p > leader
1879 && VIM_ISWHITE(*p); --p)
1880 ;
1881 ++p;
1882
1883 // Compute the length of the replaced characters in
1884 // screen characters, not bytes.
1885 {
1886 int repl_size = vim_strnsize(lead_repl,
1887 lead_repl_len);
1888 int old_size = 0;
1889 char_u *endp = p;
1890 int l;
1891
1892 while (old_size < repl_size && p > leader)
1893 {
1894 MB_PTR_BACK(leader, p);
1895 old_size += ptr2cells(p);
1896 }
1897 l = lead_repl_len - (int)(endp - p);
1898 if (l != 0)
1899 mch_memmove(endp + l, endp,
1900 (size_t)((leader + lead_len) - endp));
1901 lead_len += l;
1902 }
1903 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1904 if (p + lead_repl_len > leader + lead_len)
1905 p[lead_repl_len] = NUL;
1906
1907 // blank-out any other chars from the old leader.
1908 while (--p >= leader)
1909 {
1910 int l = mb_head_off(leader, p);
1911
1912 if (l > 1)
1913 {
1914 p -= l;
1915 if (ptr2cells(p) > 1)
1916 {
1917 p[1] = ' ';
1918 --l;
1919 }
1920 mch_memmove(p + 1, p + l + 1,
1921 (size_t)((leader + lead_len) - (p + l + 1)));
1922 lead_len -= l;
1923 *p = ' ';
1924 }
1925 else if (!VIM_ISWHITE(*p))
1926 *p = ' ';
1927 }
1928 }
1929 else // left adjusted leader
1930 {
1931 p = skipwhite(leader);
1932
1933 // Compute the length of the replaced characters in
1934 // screen characters, not bytes. Move the part that is
1935 // not to be overwritten.
1936 {
1937 int repl_size = vim_strnsize(lead_repl,
1938 lead_repl_len);
1939 int i;
1940 int l;
1941
1942 for (i = 0; i < lead_len && p[i] != NUL; i += l)
1943 {
1944 l = (*mb_ptr2len)(p + i);
1945 if (vim_strnsize(p, i + l) > repl_size)
1946 break;
1947 }
1948 if (i != lead_repl_len)
1949 {
1950 mch_memmove(p + lead_repl_len, p + i,
1951 (size_t)(lead_len - i - (p - leader)));
1952 lead_len += lead_repl_len - i;
1953 }
1954 }
1955 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1956
1957 // Replace any remaining non-white chars in the old
1958 // leader by spaces. Keep Tabs, the indent must
1959 // remain the same.
1960 for (p += lead_repl_len; p < leader + lead_len; ++p)
1961 if (!VIM_ISWHITE(*p))
1962 {
1963 // Don't put a space before a TAB.
1964 if (p + 1 < leader + lead_len && p[1] == TAB)
1965 {
1966 --lead_len;
1967 mch_memmove(p, p + 1,
1968 (leader + lead_len) - p);
1969 }
1970 else
1971 {
1972 int l = (*mb_ptr2len)(p);
1973
1974 if (l > 1)
1975 {
1976 if (ptr2cells(p) > 1)
1977 {
1978 // Replace a double-wide char with
1979 // two spaces
1980 --l;
1981 *p++ = ' ';
1982 }
1983 mch_memmove(p + 1, p + l,
1984 (leader + lead_len) - p);
1985 lead_len -= l - 1;
1986 }
1987 *p = ' ';
1988 }
1989 }
1990 *p = NUL;
1991 }
1992
1993 // Recompute the indent, it may have changed.
1994 if (curbuf->b_p_ai
1995#ifdef FEAT_SMARTINDENT
1996 || do_si
1997#endif
1998 )
1999#ifdef FEAT_VARTABS
2000 newindent = get_indent_str_vtab(leader, curbuf->b_p_ts,
2001 curbuf->b_p_vts_array, FALSE);
2002#else
2003 newindent = get_indent_str(leader,
2004 (int)curbuf->b_p_ts, FALSE);
2005#endif
2006
2007 // Add the indent offset
2008 if (newindent + off < 0)
2009 {
2010 off = -newindent;
2011 newindent = 0;
2012 }
2013 else
2014 newindent += off;
2015
2016 // Correct trailing spaces for the shift, so that
2017 // alignment remains equal.
2018 while (off > 0 && lead_len > 0
2019 && leader[lead_len - 1] == ' ')
2020 {
2021 // Don't do it when there is a tab before the space
2022 if (vim_strchr(skipwhite(leader), '\t') != NULL)
2023 break;
2024 --lead_len;
2025 --off;
2026 }
2027
2028 // If the leader ends in white space, don't add an
2029 // extra space
2030 if (lead_len > 0 && VIM_ISWHITE(leader[lead_len - 1]))
2031 extra_space = FALSE;
2032 leader[lead_len] = NUL;
2033 }
2034
2035 if (extra_space)
2036 {
2037 leader[lead_len++] = ' ';
2038 leader[lead_len] = NUL;
2039 }
2040
2041 newcol = lead_len;
2042
2043 // if a new indent will be set below, remove the indent that
2044 // is in the comment leader
2045 if (newindent
2046#ifdef FEAT_SMARTINDENT
2047 || did_si
2048#endif
2049 )
2050 {
2051 while (lead_len && VIM_ISWHITE(*leader))
2052 {
2053 --lead_len;
2054 --newcol;
2055 ++leader;
2056 }
2057 }
2058
2059 }
2060#ifdef FEAT_SMARTINDENT
2061 did_si = can_si = FALSE;
2062#endif
2063 }
2064 else if (comment_end != NULL)
2065 {
2066 // We have finished a comment, so we don't use the leader.
2067 // If this was a C-comment and 'ai' or 'si' is set do a normal
2068 // indent to align with the line containing the start of the
2069 // comment.
2070 if (comment_end[0] == '*' && comment_end[1] == '/' &&
2071 (curbuf->b_p_ai
2072#ifdef FEAT_SMARTINDENT
2073 || do_si
2074#endif
2075 ))
2076 {
2077 old_cursor = curwin->w_cursor;
2078 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line);
2079 if ((pos = findmatch(NULL, NUL)) != NULL)
2080 {
2081 curwin->w_cursor.lnum = pos->lnum;
2082 newindent = get_indent();
2083 }
2084 curwin->w_cursor = old_cursor;
2085 }
2086 }
2087 }
2088#endif
2089
2090 // (State == INSERT || State == REPLACE), only when dir == FORWARD
2091 if (p_extra != NULL)
2092 {
2093 *p_extra = saved_char; // restore char that NUL replaced
2094
2095 // When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first
2096 // non-blank.
2097 //
2098 // When in REPLACE mode, put the deleted blanks on the replace stack,
2099 // preceded by a NUL, so they can be put back when a BS is entered.
2100 if (REPLACE_NORMAL(State))
2101 replace_push(NUL); /* end of extra blanks */
2102 if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES))
2103 {
2104 while ((*p_extra == ' ' || *p_extra == '\t')
2105 && (!enc_utf8
2106 || !utf_iscomposing(utf_ptr2char(p_extra + 1))))
2107 {
2108 if (REPLACE_NORMAL(State))
2109 replace_push(*p_extra);
2110 ++p_extra;
2111 ++less_cols_off;
2112 }
2113 }
2114
2115 // columns for marks adjusted for removed columns
2116 less_cols = (int)(p_extra - saved_line);
2117 }
2118
2119 if (p_extra == NULL)
2120 p_extra = (char_u *)""; // append empty line
2121
2122#ifdef FEAT_COMMENTS
2123 // concatenate leader and p_extra, if there is a leader
2124 if (lead_len)
2125 {
2126 if (flags & OPENLINE_COM_LIST && second_line_indent > 0)
2127 {
2128 int i;
2129 int padding = second_line_indent
2130 - (newindent + (int)STRLEN(leader));
2131
2132 // Here whitespace is inserted after the comment char.
2133 // Below, set_indent(newindent, SIN_INSERT) will insert the
2134 // whitespace needed before the comment char.
2135 for (i = 0; i < padding; i++)
2136 {
2137 STRCAT(leader, " ");
2138 less_cols--;
2139 newcol++;
2140 }
2141 }
2142 STRCAT(leader, p_extra);
2143 p_extra = leader;
2144 did_ai = TRUE; // So truncating blanks works with comments
2145 less_cols -= lead_len;
2146 }
2147 else
2148 end_comment_pending = NUL; // turns out there was no leader
2149#endif
2150
2151 old_cursor = curwin->w_cursor;
2152 if (dir == BACKWARD)
2153 --curwin->w_cursor.lnum;
2154 if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count)
2155 {
2156 if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE)
2157 == FAIL)
2158 goto theend;
2159 // Postpone calling changed_lines(), because it would mess up folding
2160 // with markers.
2161 // Skip mark_adjust when adding a line after the last one, there can't
2162 // be marks there. But still needed in diff mode.
2163 if (curwin->w_cursor.lnum + 1 < curbuf->b_ml.ml_line_count
2164#ifdef FEAT_DIFF
2165 || curwin->w_p_diff
2166#endif
2167 )
2168 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
2169 did_append = TRUE;
Bram Moolenaar45dd07f2019-05-15 22:45:37 +02002170#ifdef FEAT_TEXT_PROP
2171 if ((State & INSERT) && !(State & VREPLACE_FLAG))
2172 // properties after the split move to the next line
2173 adjust_props_for_split(curwin->w_cursor.lnum, curwin->w_cursor.lnum,
2174 curwin->w_cursor.col + 1, 0);
2175#endif
Bram Moolenaar3f86ca02019-05-11 18:30:00 +02002176 }
2177 else
2178 {
2179 // In VREPLACE mode we are starting to replace the next line.
2180 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.
2185 (void)u_save_cursor(); /* errors are ignored! */
2186 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
2194 if (newindent
2195#ifdef FEAT_SMARTINDENT
2196 || did_si
2197#endif
2198 )
2199 {
2200 ++curwin->w_cursor.lnum;
2201#ifdef FEAT_SMARTINDENT
2202 if (did_si)
2203 {
2204 int sw = (int)get_sw_value(curbuf);
2205
2206 if (p_sr)
2207 newindent -= newindent % sw;
2208 newindent += sw;
2209 }
2210#endif
2211 // Copy the indent
2212 if (curbuf->b_p_ci)
2213 {
2214 (void)copy_indent(newindent, saved_line);
2215
2216 // Set the 'preserveindent' option so that any further screwing
2217 // with the line doesn't entirely destroy our efforts to preserve
2218 // it. It gets restored at the function end.
2219 curbuf->b_p_pi = TRUE;
2220 }
2221 else
2222 (void)set_indent(newindent, SIN_INSERT);
2223 less_cols -= curwin->w_cursor.col;
2224
2225 ai_col = curwin->w_cursor.col;
2226
2227 // In REPLACE mode, for each character in the new indent, there must
2228 // be a NUL on the replace stack, for when it is deleted with BS
2229 if (REPLACE_NORMAL(State))
2230 for (n = 0; n < (int)curwin->w_cursor.col; ++n)
2231 replace_push(NUL);
2232 newcol += curwin->w_cursor.col;
2233#ifdef FEAT_SMARTINDENT
2234 if (no_si)
2235 did_si = FALSE;
2236#endif
2237 }
2238
2239#ifdef FEAT_COMMENTS
2240 // In REPLACE mode, for each character in the extra leader, there must be
2241 // a NUL on the replace stack, for when it is deleted with BS.
2242 if (REPLACE_NORMAL(State))
2243 while (lead_len-- > 0)
2244 replace_push(NUL);
2245#endif
2246
2247 curwin->w_cursor = old_cursor;
2248
2249 if (dir == FORWARD)
2250 {
2251 if (trunc_line || (State & INSERT))
2252 {
2253 // truncate current line at cursor
2254 saved_line[curwin->w_cursor.col] = NUL;
2255 // Remove trailing white space, unless OPENLINE_KEEPTRAIL used.
2256 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL))
2257 truncate_spaces(saved_line);
2258 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE);
2259 saved_line = NULL;
2260 if (did_append)
2261 {
2262 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
2263 curwin->w_cursor.lnum + 1, 1L);
2264 did_append = FALSE;
2265
2266 // Move marks after the line break to the new line.
2267 if (flags & OPENLINE_MARKFIX)
2268 mark_col_adjust(curwin->w_cursor.lnum,
2269 curwin->w_cursor.col + less_cols_off,
2270 1L, (long)-less_cols, 0);
2271 }
2272 else
2273 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
2274 }
2275
2276 // Put the cursor on the new line. Careful: the scrollup() above may
2277 // have moved w_cursor, we must use old_cursor.
2278 curwin->w_cursor.lnum = old_cursor.lnum + 1;
2279 }
2280 if (did_append)
2281 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L);
2282
2283 curwin->w_cursor.col = newcol;
2284 curwin->w_cursor.coladd = 0;
2285
2286#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2287 // In VREPLACE mode, we are handling the replace stack ourselves, so stop
2288 // fixthisline() from doing it (via change_indent()) by telling it we're in
2289 // normal INSERT mode.
2290 if (State & VREPLACE_FLAG)
2291 {
2292 vreplace_mode = State; // So we know to put things right later
2293 State = INSERT;
2294 }
2295 else
2296 vreplace_mode = 0;
2297#endif
2298#ifdef FEAT_LISP
2299 // May do lisp indenting.
2300 if (!p_paste
2301# ifdef FEAT_COMMENTS
2302 && leader == NULL
2303# endif
2304 && curbuf->b_p_lisp
2305 && curbuf->b_p_ai)
2306 {
2307 fixthisline(get_lisp_indent);
2308 ai_col = (colnr_T)getwhitecols_curline();
2309 }
2310#endif
2311#ifdef FEAT_CINDENT
2312 // May do indenting after opening a new line.
2313 if (!p_paste
2314 && (curbuf->b_p_cin
2315# ifdef FEAT_EVAL
2316 || *curbuf->b_p_inde != NUL
2317# endif
2318 )
2319 && in_cinkeys(dir == FORWARD
2320 ? KEY_OPEN_FORW
2321 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum)))
2322 {
2323 do_c_expr_indent();
2324 ai_col = (colnr_T)getwhitecols_curline();
2325 }
2326#endif
2327#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2328 if (vreplace_mode != 0)
2329 State = vreplace_mode;
2330#endif
2331
2332 // Finally, VREPLACE gets the stuff on the new line, then puts back the
2333 // original line, and inserts the new stuff char by char, pushing old stuff
2334 // onto the replace stack (via ins_char()).
2335 if (State & VREPLACE_FLAG)
2336 {
2337 // Put new line in p_extra
2338 p_extra = vim_strsave(ml_get_curline());
2339 if (p_extra == NULL)
2340 goto theend;
2341
2342 // Put back original line
2343 ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
2344
2345 // Insert new stuff into line again
2346 curwin->w_cursor.col = 0;
2347 curwin->w_cursor.coladd = 0;
2348 ins_bytes(p_extra); // will call changed_bytes()
2349 vim_free(p_extra);
2350 next_line = NULL;
2351 }
2352
2353 retval = OK; // success!
2354theend:
2355 curbuf->b_p_pi = saved_pi;
2356 vim_free(saved_line);
2357 vim_free(next_line);
2358 vim_free(allocated);
2359 return retval;
2360}
2361
2362/*
2363 * Delete from cursor to end of line.
2364 * Caller must have prepared for undo.
2365 * If "fixpos" is TRUE fix the cursor position when done.
2366 *
2367 * Return FAIL for failure, OK otherwise.
2368 */
2369 int
2370truncate_line(int fixpos)
2371{
2372 char_u *newp;
2373 linenr_T lnum = curwin->w_cursor.lnum;
2374 colnr_T col = curwin->w_cursor.col;
2375
2376 if (col == 0)
2377 newp = vim_strsave((char_u *)"");
2378 else
2379 newp = vim_strnsave(ml_get(lnum), col);
2380
2381 if (newp == NULL)
2382 return FAIL;
2383
2384 ml_replace(lnum, newp, FALSE);
2385
2386 // mark the buffer as changed and prepare for displaying
2387 changed_bytes(lnum, curwin->w_cursor.col);
2388
2389 // If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2390 if (fixpos && curwin->w_cursor.col > 0)
2391 --curwin->w_cursor.col;
2392
2393 return OK;
2394}
2395
2396/*
2397 * Delete "nlines" lines at the cursor.
2398 * Saves the lines for undo first if "undo" is TRUE.
2399 */
2400 void
2401del_lines(long nlines, int undo)
2402{
2403 long n;
2404 linenr_T first = curwin->w_cursor.lnum;
2405
2406 if (nlines <= 0)
2407 return;
2408
2409 // save the deleted lines for undo
2410 if (undo && u_savedel(first, nlines) == FAIL)
2411 return;
2412
2413 for (n = 0; n < nlines; )
2414 {
2415 if (curbuf->b_ml.ml_flags & ML_EMPTY) // nothing to delete
2416 break;
2417
2418 ml_delete(first, TRUE);
2419 ++n;
2420
2421 // If we delete the last line in the file, stop
2422 if (first > curbuf->b_ml.ml_line_count)
2423 break;
2424 }
2425
2426 // Correct the cursor position before calling deleted_lines_mark(), it may
2427 // trigger a callback to display the cursor.
2428 curwin->w_cursor.col = 0;
2429 check_cursor_lnum();
2430
2431 // adjust marks, mark the buffer as changed and prepare for displaying
2432 deleted_lines_mark(first, n);
2433}