blob: bdba903d8893bc4c844939e8bc396188f6d39eeb [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
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 * undo.c: multi level undo facility
12 *
13 * The saved lines are stored in a list of lists (one for each buffer):
14 *
15 * b_u_oldhead------------------------------------------------+
16 * |
17 * V
18 * +--------------+ +--------------+ +--------------+
19 * b_u_newhead--->| u_header | | u_header | | u_header |
20 * | uh_next------>| uh_next------>| uh_next---->NULL
21 * NULL<--------uh_prev |<---------uh_prev |<---------uh_prev |
22 * | uh_entry | | uh_entry | | uh_entry |
23 * +--------|-----+ +--------|-----+ +--------|-----+
24 * | | |
25 * V V V
26 * +--------------+ +--------------+ +--------------+
27 * | u_entry | | u_entry | | u_entry |
28 * | ue_next | | ue_next | | ue_next |
29 * +--------|-----+ +--------|-----+ +--------|-----+
30 * | | |
31 * V V V
32 * +--------------+ NULL NULL
33 * | u_entry |
34 * | ue_next |
35 * +--------|-----+
36 * |
37 * V
38 * etc.
39 *
40 * Each u_entry list contains the information for one undo or redo.
41 * curbuf->b_u_curhead points to the header of the last undo (the next redo),
42 * or is NULL if nothing has been undone.
43 *
44 * All data is allocated with u_alloc_line(), thus it will be freed as soon as
45 * we switch files!
46 */
47
48#include "vim.h"
49
50static u_entry_T *u_get_headentry __ARGS((void));
51static void u_getbot __ARGS((void));
52static int u_savecommon __ARGS((linenr_T, linenr_T, linenr_T));
53static void u_doit __ARGS((int count));
54static void u_undoredo __ARGS((void));
55static void u_undo_end __ARGS((void));
56static void u_freelist __ARGS((struct u_header *));
57static void u_freeentry __ARGS((u_entry_T *, long));
58
59static char_u *u_blockalloc __ARGS((long_u));
60static void u_free_line __ARGS((char_u *, int keep));
61static char_u *u_alloc_line __ARGS((unsigned));
62static char_u *u_save_line __ARGS((linenr_T));
63
64static long u_newcount, u_oldcount;
65
66/*
67 * When 'u' flag included in 'cpoptions', we behave like vi. Need to remember
68 * the action that "u" should do.
69 */
70static int undo_undoes = FALSE;
71
72/*
73 * save the current line for both the "u" and "U" command
74 */
75 int
76u_save_cursor()
77{
78 return (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
79 (linenr_T)(curwin->w_cursor.lnum + 1)));
80}
81
82/*
83 * Save the lines between "top" and "bot" for both the "u" and "U" command.
84 * "top" may be 0 and bot may be curbuf->b_ml.ml_line_count + 1.
85 * Returns FAIL when lines could not be saved, OK otherwise.
86 */
87 int
88u_save(top, bot)
89 linenr_T top, bot;
90{
91 if (undo_off)
92 return OK;
93
94 if (top > curbuf->b_ml.ml_line_count ||
95 top >= bot || bot > curbuf->b_ml.ml_line_count + 1)
96 return FALSE; /* rely on caller to do error messages */
97
98 if (top + 2 == bot)
99 u_saveline((linenr_T)(top + 1));
100
101 return (u_savecommon(top, bot, (linenr_T)0));
102}
103
104/*
105 * save the line "lnum" (used by ":s" and "~" command)
106 * The line is replaced, so the new bottom line is lnum + 1.
107 */
108 int
109u_savesub(lnum)
110 linenr_T lnum;
111{
112 if (undo_off)
113 return OK;
114
115 return (u_savecommon(lnum - 1, lnum + 1, lnum + 1));
116}
117
118/*
119 * a new line is inserted before line "lnum" (used by :s command)
120 * The line is inserted, so the new bottom line is lnum + 1.
121 */
122 int
123u_inssub(lnum)
124 linenr_T lnum;
125{
126 if (undo_off)
127 return OK;
128
129 return (u_savecommon(lnum - 1, lnum, lnum + 1));
130}
131
132/*
133 * save the lines "lnum" - "lnum" + nlines (used by delete command)
134 * The lines are deleted, so the new bottom line is lnum, unless the buffer
135 * becomes empty.
136 */
137 int
138u_savedel(lnum, nlines)
139 linenr_T lnum;
140 long nlines;
141{
142 if (undo_off)
143 return OK;
144
145 return (u_savecommon(lnum - 1, lnum + nlines,
146 nlines == curbuf->b_ml.ml_line_count ? 2 : lnum));
147}
148
149 static int
150u_savecommon(top, bot, newbot)
151 linenr_T top, bot;
152 linenr_T newbot;
153{
154 linenr_T lnum;
155 long i;
156 struct u_header *uhp;
157 u_entry_T *uep;
158 u_entry_T *prev_uep;
159 long size;
160
161 /*
162 * Don't allow changes when 'modifiable' is off. Letting the
163 * undo fail is a crude way to make all change commands fail.
164 */
165 if (!curbuf->b_p_ma)
166 {
167 EMSG(_(e_modifiable));
168 return FAIL;
169 }
170
171#ifdef HAVE_SANDBOX
172 /*
173 * In the sandbox it's not allowed to change the text. Letting the
174 * undo fail is a crude way to make all change commands fail.
175 */
176 if (sandbox != 0)
177 {
178 EMSG(_(e_sandbox));
179 return FAIL;
180 }
181#endif
182
183#ifdef FEAT_NETBEANS_INTG
184 /*
185 * Netbeans defines areas that cannot be modified. Bail out here when
186 * trying to change text in a guarded area.
187 */
Bram Moolenaar009b2592004-10-24 19:18:58 +0000188 if (usingNetbeans)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 {
Bram Moolenaar009b2592004-10-24 19:18:58 +0000190 if (netbeans_is_guarded(top, bot))
191 {
192 EMSG(_(e_guarded));
193 return FAIL;
194 }
195 if (curbuf->b_p_ro)
196 {
197 EMSG(_(e_nbreadonly));
198 return FAIL;
199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200 }
201#endif
202
203#ifdef FEAT_AUTOCMD
204 /*
205 * Saving text for undo means we are going to make a change. Give a
206 * warning for a read-only file before making the change, so that the
207 * FileChangedRO event can replace the buffer with a read-write version
208 * (e.g., obtained from a source control system).
209 */
210 change_warning(0);
211#endif
212
213 size = bot - top - 1;
214
215 /*
216 * if curbuf->b_u_synced == TRUE make a new header
217 */
218 if (curbuf->b_u_synced)
219 {
220#ifdef FEAT_JUMPLIST
221 /* Need to create new entry in b_changelist. */
222 curbuf->b_new_change = TRUE;
223#endif
224
225 /*
226 * if we undid more than we redid, free the entry lists before and
227 * including curbuf->b_u_curhead
228 */
229 while (curbuf->b_u_curhead != NULL)
230 u_freelist(curbuf->b_u_newhead);
231
232 /*
233 * free headers to keep the size right
234 */
235 while (curbuf->b_u_numhead > p_ul && curbuf->b_u_oldhead != NULL)
236 u_freelist(curbuf->b_u_oldhead);
237
238 if (p_ul < 0) /* no undo at all */
239 {
240 curbuf->b_u_synced = FALSE;
241 return OK;
242 }
243
244 /*
245 * make a new header entry
246 */
247 uhp = (struct u_header *)u_alloc_line((unsigned)
248 sizeof(struct u_header));
249 if (uhp == NULL)
250 goto nomem;
251 uhp->uh_prev = NULL;
252 uhp->uh_next = curbuf->b_u_newhead;
253 if (curbuf->b_u_newhead != NULL)
254 curbuf->b_u_newhead->uh_prev = uhp;
255 uhp->uh_entry = NULL;
256 uhp->uh_getbot_entry = NULL;
257 uhp->uh_cursor = curwin->w_cursor; /* save cursor pos. for undo */
258#ifdef FEAT_VIRTUALEDIT
259 if (virtual_active() && curwin->w_cursor.coladd > 0)
260 uhp->uh_cursor_vcol = getviscol();
261 else
262 uhp->uh_cursor_vcol = -1;
263#endif
264
265 /* save changed and buffer empty flag for undo */
266 uhp->uh_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
267 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
268
269 /* save named marks for undo */
270 mch_memmove(uhp->uh_namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
271 curbuf->b_u_newhead = uhp;
272 if (curbuf->b_u_oldhead == NULL)
273 curbuf->b_u_oldhead = uhp;
274 ++curbuf->b_u_numhead;
275 }
276 else
277 {
278 if (p_ul < 0) /* no undo at all */
279 return OK;
280
281 /*
282 * When saving a single line, and it has been saved just before, it
283 * doesn't make sense saving it again. Saves a lot of memory when
284 * making lots of changes inside the same line.
285 * This is only possible if the previous change didn't increase or
286 * decrease the number of lines.
287 * Check the ten last changes. More doesn't make sense and takes too
288 * long.
289 */
290 if (size == 1)
291 {
292 uep = u_get_headentry();
293 prev_uep = NULL;
294 for (i = 0; i < 10; ++i)
295 {
296 if (uep == NULL)
297 break;
298
299 /* If lines have been inserted/deleted we give up.
300 * Also when the line was included in a multi-line save. */
301 if ((curbuf->b_u_newhead->uh_getbot_entry != uep
302 ? (uep->ue_top + uep->ue_size + 1
303 != (uep->ue_bot == 0
304 ? curbuf->b_ml.ml_line_count + 1
305 : uep->ue_bot))
306 : uep->ue_lcount != curbuf->b_ml.ml_line_count)
307 || (uep->ue_size > 1
308 && top >= uep->ue_top
309 && top + 2 <= uep->ue_top + uep->ue_size + 1))
310 break;
311
312 /* If it's the same line we can skip saving it again. */
313 if (uep->ue_size == 1 && uep->ue_top == top)
314 {
315 if (i > 0)
316 {
317 /* It's not the last entry: get ue_bot for the last
318 * entry now. Following deleted/inserted lines go to
319 * the re-used entry. */
320 u_getbot();
321 curbuf->b_u_synced = FALSE;
322
323 /* Move the found entry to become the last entry. The
324 * order of undo/redo doesn't matter for the entries
325 * we move it over, since they don't change the line
326 * count and don't include this line. It does matter
327 * for the found entry if the line count is changed by
328 * the executed command. */
329 prev_uep->ue_next = uep->ue_next;
330 uep->ue_next = curbuf->b_u_newhead->uh_entry;
331 curbuf->b_u_newhead->uh_entry = uep;
332 }
333
334 /* The executed command may change the line count. */
335 if (newbot != 0)
336 uep->ue_bot = newbot;
337 else if (bot > curbuf->b_ml.ml_line_count)
338 uep->ue_bot = 0;
339 else
340 {
341 uep->ue_lcount = curbuf->b_ml.ml_line_count;
342 curbuf->b_u_newhead->uh_getbot_entry = uep;
343 }
344 return OK;
345 }
346 prev_uep = uep;
347 uep = uep->ue_next;
348 }
349 }
350
351 /* find line number for ue_bot for previous u_save() */
352 u_getbot();
353 }
354
355#if !defined(UNIX) && !defined(DJGPP) && !defined(WIN32) && !defined(__EMX__)
356 /*
357 * With Amiga and MSDOS 16 bit we can't handle big undo's, because
358 * then u_alloc_line would have to allocate a block larger than 32K
359 */
360 if (size >= 8000)
361 goto nomem;
362#endif
363
364 /*
365 * add lines in front of entry list
366 */
367 uep = (u_entry_T *)u_alloc_line((unsigned)sizeof(u_entry_T));
368 if (uep == NULL)
369 goto nomem;
370
371 uep->ue_size = size;
372 uep->ue_top = top;
373 if (newbot != 0)
374 uep->ue_bot = newbot;
375 /*
376 * Use 0 for ue_bot if bot is below last line.
377 * Otherwise we have to compute ue_bot later.
378 */
379 else if (bot > curbuf->b_ml.ml_line_count)
380 uep->ue_bot = 0;
381 else
382 {
383 uep->ue_lcount = curbuf->b_ml.ml_line_count;
384 curbuf->b_u_newhead->uh_getbot_entry = uep;
385 }
386
387 if (size)
388 {
389 if ((uep->ue_array = (char_u **)u_alloc_line(
390 (unsigned)(sizeof(char_u *) * size))) == NULL)
391 {
392 u_freeentry(uep, 0L);
393 goto nomem;
394 }
395 for (i = 0, lnum = top + 1; i < size; ++i)
396 {
397 if ((uep->ue_array[i] = u_save_line(lnum++)) == NULL)
398 {
399 u_freeentry(uep, i);
400 goto nomem;
401 }
402 }
403 }
404 uep->ue_next = curbuf->b_u_newhead->uh_entry;
405 curbuf->b_u_newhead->uh_entry = uep;
406 curbuf->b_u_synced = FALSE;
407 undo_undoes = FALSE;
408
409 return OK;
410
411nomem:
412 msg_silent = 0; /* must display the prompt */
413 if (ask_yesno((char_u *)_("No undo possible; continue anyway"), TRUE)
414 == 'y')
415 {
416 undo_off = TRUE; /* will be reset when character typed */
417 return OK;
418 }
419 do_outofmem_msg((long_u)0);
420 return FAIL;
421}
422
423/*
424 * If 'cpoptions' contains 'u': Undo the previous undo or redo (vi compatible).
425 * If 'cpoptions' does not contain 'u': Always undo.
426 */
427 void
428u_undo(count)
429 int count;
430{
431 /*
432 * If we get an undo command while executing a macro, we behave like the
433 * original vi. If this happens twice in one macro the result will not
434 * be compatible.
435 */
436 if (curbuf->b_u_synced == FALSE)
437 {
438 u_sync();
439 count = 1;
440 }
441
442 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
443 undo_undoes = TRUE;
444 else
445 undo_undoes = !undo_undoes;
446 u_doit(count);
447}
448
449/*
450 * If 'cpoptions' contains 'u': Repeat the previous undo or redo.
451 * If 'cpoptions' does not contain 'u': Always redo.
452 */
453 void
454u_redo(count)
455 int count;
456{
457 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
458 undo_undoes = FALSE;
459 u_doit(count);
460}
461
462/*
463 * Undo or redo, depending on 'undo_undoes', 'count' times.
464 */
465 static void
466u_doit(count)
467 int count;
468{
469 /* Don't allow changes when 'modifiable' is off. */
470 if (!curbuf->b_p_ma)
471 {
472 EMSG(_(e_modifiable));
473 return;
474 }
475#ifdef HAVE_SANDBOX
476 /* In the sandbox it's not allowed to change the text. */
477 if (sandbox != 0)
478 {
479 EMSG(_(e_sandbox));
480 return;
481 }
482#endif
483
484 u_newcount = 0;
485 u_oldcount = 0;
486 while (count--)
487 {
488 if (undo_undoes)
489 {
490 if (curbuf->b_u_curhead == NULL) /* first undo */
491 curbuf->b_u_curhead = curbuf->b_u_newhead;
492 else if (p_ul > 0) /* multi level undo */
493 /* get next undo */
494 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_next;
495 /* nothing to undo */
496 if (curbuf->b_u_numhead == 0 || curbuf->b_u_curhead == NULL)
497 {
498 /* stick curbuf->b_u_curhead at end */
499 curbuf->b_u_curhead = curbuf->b_u_oldhead;
500 beep_flush();
501 break;
502 }
503
504 u_undoredo();
505 }
506 else
507 {
508 if (curbuf->b_u_curhead == NULL || p_ul <= 0)
509 {
510 beep_flush(); /* nothing to redo */
511 break;
512 }
513
514 u_undoredo();
515 /* advance for next redo */
516 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_prev;
517 }
518 }
519 u_undo_end();
520}
521
522/*
523 * u_undoredo: common code for undo and redo
524 *
525 * The lines in the file are replaced by the lines in the entry list at
526 * curbuf->b_u_curhead. The replaced lines in the file are saved in the entry
527 * list for the next undo/redo.
528 */
529 static void
530u_undoredo()
531{
532 char_u **newarray = NULL;
533 linenr_T oldsize;
534 linenr_T newsize;
535 linenr_T top, bot;
536 linenr_T lnum;
537 linenr_T newlnum = MAXLNUM;
538 long i;
539 u_entry_T *uep, *nuep;
540 u_entry_T *newlist = NULL;
541 int old_flags;
542 int new_flags;
543 pos_T namedm[NMARKS];
544 int empty_buffer; /* buffer became empty */
545
546 old_flags = curbuf->b_u_curhead->uh_flags;
547 new_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
548 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
549 setpcmark();
550
551 /*
552 * save marks before undo/redo
553 */
554 mch_memmove(namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
555 curbuf->b_op_start.lnum = curbuf->b_ml.ml_line_count;
556 curbuf->b_op_start.col = 0;
557 curbuf->b_op_end.lnum = 0;
558 curbuf->b_op_end.col = 0;
559
560 for (uep = curbuf->b_u_curhead->uh_entry; uep != NULL; uep = nuep)
561 {
562 top = uep->ue_top;
563 bot = uep->ue_bot;
564 if (bot == 0)
565 bot = curbuf->b_ml.ml_line_count + 1;
566 if (top > curbuf->b_ml.ml_line_count || top >= bot
567 || bot > curbuf->b_ml.ml_line_count + 1)
568 {
569 EMSG(_("E438: u_undo: line numbers wrong"));
570 changed(); /* don't want UNCHANGED now */
571 return;
572 }
573
574 oldsize = bot - top - 1; /* number of lines before undo */
575 newsize = uep->ue_size; /* number of lines after undo */
576
577 if (top < newlnum)
578 {
579 /* If the saved cursor is somewhere in this undo block, move it to
580 * the remembered position. Makes "gwap" put the cursor back
581 * where it was. */
582 lnum = curbuf->b_u_curhead->uh_cursor.lnum;
583 if (lnum >= top && lnum <= top + newsize + 1)
584 {
585 curwin->w_cursor = curbuf->b_u_curhead->uh_cursor;
586 newlnum = curwin->w_cursor.lnum - 1;
587 }
588 else
589 {
590 /* Use the first line that actually changed. Avoids that
591 * undoing auto-formatting puts the cursor in the previous
592 * line. */
593 for (i = 0; i < newsize && i < oldsize; ++i)
594 if (STRCMP(uep->ue_array[i], ml_get(top + 1 + i)) != 0)
595 break;
596 if (i == newsize && newlnum == MAXLNUM && uep->ue_next == NULL)
597 {
598 newlnum = top;
599 curwin->w_cursor.lnum = newlnum + 1;
600 }
601 else if (i < newsize)
602 {
603 newlnum = top + i;
604 curwin->w_cursor.lnum = newlnum + 1;
605 }
606 }
607 }
608
609 empty_buffer = FALSE;
610
611 /* delete the lines between top and bot and save them in newarray */
612 if (oldsize)
613 {
614 if ((newarray = (char_u **)u_alloc_line(
615 (unsigned)(sizeof(char_u *) * oldsize))) == NULL)
616 {
617 do_outofmem_msg((long_u)(sizeof(char_u *) * oldsize));
618 /*
619 * We have messed up the entry list, repair is impossible.
620 * we have to free the rest of the list.
621 */
622 while (uep != NULL)
623 {
624 nuep = uep->ue_next;
625 u_freeentry(uep, uep->ue_size);
626 uep = nuep;
627 }
628 break;
629 }
630 /* delete backwards, it goes faster in most cases */
631 for (lnum = bot - 1, i = oldsize; --i >= 0; --lnum)
632 {
633 /* what can we do when we run out of memory? */
634 if ((newarray[i] = u_save_line(lnum)) == NULL)
635 do_outofmem_msg((long_u)0);
636 /* remember we deleted the last line in the buffer, and a
637 * dummy empty line will be inserted */
638 if (curbuf->b_ml.ml_line_count == 1)
639 empty_buffer = TRUE;
640 ml_delete(lnum, FALSE);
641 }
642 }
643
644 /* insert the lines in u_array between top and bot */
645 if (newsize)
646 {
647 for (lnum = top, i = 0; i < newsize; ++i, ++lnum)
648 {
649 /*
650 * If the file is empty, there is an empty line 1 that we
651 * should get rid of, by replacing it with the new line
652 */
653 if (empty_buffer && lnum == 0)
654 ml_replace((linenr_T)1, uep->ue_array[i], TRUE);
655 else
656 ml_append(lnum, uep->ue_array[i], (colnr_T)0, FALSE);
657 u_free_line(uep->ue_array[i], FALSE);
658 }
659 u_free_line((char_u *)uep->ue_array, FALSE);
660 }
661
662 /* adjust marks */
663 if (oldsize != newsize)
664 {
665 mark_adjust(top + 1, top + oldsize, (long)MAXLNUM,
666 (long)newsize - (long)oldsize);
667 if (curbuf->b_op_start.lnum > top + oldsize)
668 curbuf->b_op_start.lnum += newsize - oldsize;
669 if (curbuf->b_op_end.lnum > top + oldsize)
670 curbuf->b_op_end.lnum += newsize - oldsize;
671 }
672
673 changed_lines(top + 1, 0, bot, newsize - oldsize);
674
675 /* set '[ and '] mark */
676 if (top + 1 < curbuf->b_op_start.lnum)
677 curbuf->b_op_start.lnum = top + 1;
678 if (newsize == 0 && top + 1 > curbuf->b_op_end.lnum)
679 curbuf->b_op_end.lnum = top + 1;
680 else if (top + newsize > curbuf->b_op_end.lnum)
681 curbuf->b_op_end.lnum = top + newsize;
682
683 u_newcount += newsize;
684 u_oldcount += oldsize;
685 uep->ue_size = oldsize;
686 uep->ue_array = newarray;
687 uep->ue_bot = top + newsize + 1;
688
689 /*
690 * insert this entry in front of the new entry list
691 */
692 nuep = uep->ue_next;
693 uep->ue_next = newlist;
694 newlist = uep;
695 }
696
697 curbuf->b_u_curhead->uh_entry = newlist;
698 curbuf->b_u_curhead->uh_flags = new_flags;
699 if ((old_flags & UH_EMPTYBUF) && bufempty())
700 curbuf->b_ml.ml_flags |= ML_EMPTY;
701 if (old_flags & UH_CHANGED)
702 changed();
703 else
Bram Moolenaar009b2592004-10-24 19:18:58 +0000704#ifdef FEAT_NETBEANS_INTG
705 /* per netbeans undo rules, keep it as modified */
706 if (!isNetbeansModified(curbuf))
707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 unchanged(curbuf, FALSE);
709
710 /*
711 * restore marks from before undo/redo
712 */
713 for (i = 0; i < NMARKS; ++i)
714 if (curbuf->b_u_curhead->uh_namedm[i].lnum)
715 {
716 curbuf->b_namedm[i] = curbuf->b_u_curhead->uh_namedm[i];
717 curbuf->b_u_curhead->uh_namedm[i] = namedm[i];
718 }
719
720 /*
721 * If the cursor is only off by one line, put it at the same position as
722 * before starting the change (for the "o" command).
723 * Otherwise the cursor should go to the first undone line.
724 */
725 if (curbuf->b_u_curhead->uh_cursor.lnum + 1 == curwin->w_cursor.lnum
726 && curwin->w_cursor.lnum > 1)
727 --curwin->w_cursor.lnum;
728 if (curbuf->b_u_curhead->uh_cursor.lnum == curwin->w_cursor.lnum)
729 {
730 curwin->w_cursor.col = curbuf->b_u_curhead->uh_cursor.col;
731#ifdef FEAT_VIRTUALEDIT
732 if (virtual_active() && curbuf->b_u_curhead->uh_cursor_vcol >= 0)
733 coladvance((colnr_T)curbuf->b_u_curhead->uh_cursor_vcol);
734 else
735 curwin->w_cursor.coladd = 0;
736#endif
737 }
738 else if (curwin->w_cursor.lnum <= curbuf->b_ml.ml_line_count)
739 beginline(BL_SOL | BL_FIX);
740 else
741 {
742 /* We get here with the current cursor line being past the end (eg
743 * after adding lines at the end of the file, and then undoing it).
744 * check_cursor() will move the cursor to the last line. Move it to
745 * the first column here. */
746 curwin->w_cursor.col = 0;
747#ifdef FEAT_VIRTUALEDIT
748 curwin->w_cursor.coladd = 0;
749#endif
750 }
751
752 /* Make sure the cursor is on an existing line and column. */
753 check_cursor();
754}
755
756/*
757 * If we deleted or added lines, report the number of less/more lines.
758 * Otherwise, report the number of changes (this may be incorrect
759 * in some cases, but it's better than nothing).
760 */
761 static void
762u_undo_end()
763{
764 if ((u_oldcount -= u_newcount) != 0)
765 msgmore(-u_oldcount);
766 else if (u_newcount > p_report)
767 {
768 if (u_newcount == 1)
769 MSG(_("1 change"));
770 else
771 smsg((char_u *)_("%ld changes"), u_newcount);
772 }
773#ifdef FEAT_FOLDING
774 if ((fdo_flags & FDO_UNDO) && KeyTyped)
775 foldOpenCursor();
776#endif
777}
778
779/*
780 * u_sync: stop adding to the current entry list
781 */
782 void
783u_sync()
784{
785 if (curbuf->b_u_synced)
786 return; /* already synced */
787#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
788 if (im_is_preediting())
789 return; /* XIM is busy, don't break an undo sequence */
790#endif
791 if (p_ul < 0)
792 curbuf->b_u_synced = TRUE; /* no entries, nothing to do */
793 else
794 {
795 u_getbot(); /* compute ue_bot of previous u_save */
796 curbuf->b_u_curhead = NULL;
797 }
798}
799
800/*
801 * Called after writing the file and setting b_changed to FALSE.
802 * Now an undo means that the buffer is modified.
803 */
804 void
805u_unchanged(buf)
806 buf_T *buf;
807{
808 struct u_header *uh;
809
810 for (uh = buf->b_u_newhead; uh; uh = uh->uh_next)
811 uh->uh_flags |= UH_CHANGED;
812 buf->b_did_warn = FALSE;
813}
814
815/*
816 * Get pointer to last added entry.
817 * If it's not valid, give an error message and return NULL.
818 */
819 static u_entry_T *
820u_get_headentry()
821{
822 if (curbuf->b_u_newhead == NULL || curbuf->b_u_newhead->uh_entry == NULL)
823 {
824 EMSG(_("E439: undo list corrupt"));
825 return NULL;
826 }
827 return curbuf->b_u_newhead->uh_entry;
828}
829
830/*
831 * u_getbot(): compute the line number of the previous u_save
832 * It is called only when b_u_synced is FALSE.
833 */
834 static void
835u_getbot()
836{
837 u_entry_T *uep;
838 linenr_T extra;
839
840 uep = u_get_headentry(); /* check for corrupt undo list */
841 if (uep == NULL)
842 return;
843
844 uep = curbuf->b_u_newhead->uh_getbot_entry;
845 if (uep != NULL)
846 {
847 /*
848 * the new ue_bot is computed from the number of lines that has been
849 * inserted (0 - deleted) since calling u_save. This is equal to the
850 * old line count subtracted from the current line count.
851 */
852 extra = curbuf->b_ml.ml_line_count - uep->ue_lcount;
853 uep->ue_bot = uep->ue_top + uep->ue_size + 1 + extra;
854 if (uep->ue_bot < 1 || uep->ue_bot > curbuf->b_ml.ml_line_count)
855 {
856 EMSG(_("E440: undo line missing"));
857 uep->ue_bot = uep->ue_top + 1; /* assume all lines deleted, will
858 * get all the old lines back
859 * without deleting the current
860 * ones */
861 }
862
863 curbuf->b_u_newhead->uh_getbot_entry = NULL;
864 }
865
866 curbuf->b_u_synced = TRUE;
867}
868
869/*
870 * u_freelist: free one entry list and adjust the pointers
871 */
872 static void
873u_freelist(uhp)
874 struct u_header *uhp;
875{
876 u_entry_T *uep, *nuep;
877
878 for (uep = uhp->uh_entry; uep != NULL; uep = nuep)
879 {
880 nuep = uep->ue_next;
881 u_freeentry(uep, uep->ue_size);
882 }
883
884 if (curbuf->b_u_curhead == uhp)
885 curbuf->b_u_curhead = NULL;
886
887 if (uhp->uh_next == NULL)
888 curbuf->b_u_oldhead = uhp->uh_prev;
889 else
890 uhp->uh_next->uh_prev = uhp->uh_prev;
891
892 if (uhp->uh_prev == NULL)
893 curbuf->b_u_newhead = uhp->uh_next;
894 else
895 uhp->uh_prev->uh_next = uhp->uh_next;
896
897 u_free_line((char_u *)uhp, FALSE);
898 --curbuf->b_u_numhead;
899}
900
901/*
902 * free entry 'uep' and 'n' lines in uep->ue_array[]
903 */
904 static void
905u_freeentry(uep, n)
906 u_entry_T *uep;
907 long n;
908{
909 while (n)
910 u_free_line(uep->ue_array[--n], FALSE);
911 u_free_line((char_u *)uep, FALSE);
912}
913
914/*
915 * invalidate the undo buffer; called when storage has already been released
916 */
917 void
918u_clearall(buf)
919 buf_T *buf;
920{
921 buf->b_u_newhead = buf->b_u_oldhead = buf->b_u_curhead = NULL;
922 buf->b_u_synced = TRUE;
923 buf->b_u_numhead = 0;
924 buf->b_u_line_ptr = NULL;
925 buf->b_u_line_lnum = 0;
926}
927
928/*
929 * save the line "lnum" for the "U" command
930 */
931 void
932u_saveline(lnum)
933 linenr_T lnum;
934{
935 if (lnum == curbuf->b_u_line_lnum) /* line is already saved */
936 return;
937 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) /* should never happen */
938 return;
939 u_clearline();
940 curbuf->b_u_line_lnum = lnum;
941 if (curwin->w_cursor.lnum == lnum)
942 curbuf->b_u_line_colnr = curwin->w_cursor.col;
943 else
944 curbuf->b_u_line_colnr = 0;
945 if ((curbuf->b_u_line_ptr = u_save_line(lnum)) == NULL)
946 do_outofmem_msg((long_u)0);
947}
948
949/*
950 * clear the line saved for the "U" command
951 * (this is used externally for crossing a line while in insert mode)
952 */
953 void
954u_clearline()
955{
956 if (curbuf->b_u_line_ptr != NULL)
957 {
958 u_free_line(curbuf->b_u_line_ptr, FALSE);
959 curbuf->b_u_line_ptr = NULL;
960 curbuf->b_u_line_lnum = 0;
961 }
962}
963
964/*
965 * Implementation of the "U" command.
966 * Differentiation from vi: "U" can be undone with the next "U".
967 * We also allow the cursor to be in another line.
968 */
969 void
970u_undoline()
971{
972 colnr_T t;
973 char_u *oldp;
974
975 if (undo_off)
976 return;
977
978 if (curbuf->b_u_line_ptr == NULL ||
979 curbuf->b_u_line_lnum > curbuf->b_ml.ml_line_count)
980 {
981 beep_flush();
982 return;
983 }
984 /* first save the line for the 'u' command */
985 if (u_savecommon(curbuf->b_u_line_lnum - 1,
986 curbuf->b_u_line_lnum + 1, (linenr_T)0) == FAIL)
987 return;
988 oldp = u_save_line(curbuf->b_u_line_lnum);
989 if (oldp == NULL)
990 {
991 do_outofmem_msg((long_u)0);
992 return;
993 }
994 ml_replace(curbuf->b_u_line_lnum, curbuf->b_u_line_ptr, TRUE);
995 changed_bytes(curbuf->b_u_line_lnum, 0);
996 u_free_line(curbuf->b_u_line_ptr, FALSE);
997 curbuf->b_u_line_ptr = oldp;
998
999 t = curbuf->b_u_line_colnr;
1000 if (curwin->w_cursor.lnum == curbuf->b_u_line_lnum)
1001 curbuf->b_u_line_colnr = curwin->w_cursor.col;
1002 curwin->w_cursor.col = t;
1003 curwin->w_cursor.lnum = curbuf->b_u_line_lnum;
1004}
1005
1006/*
1007 * storage allocation for the undo lines and blocks of the current file
1008 */
1009
1010/*
1011 * Memory is allocated in relatively large blocks. These blocks are linked
1012 * in the allocated block list, headed by curbuf->b_block_head. They are all
1013 * freed when abandoning a file, so we don't have to free every single line.
1014 * The list is kept sorted on memory address.
1015 * block_alloc() allocates a block.
1016 * m_blockfree() frees all blocks.
1017 *
1018 * The available chunks of memory are kept in free chunk lists. There is
1019 * one free list for each block of allocated memory. The list is kept sorted
1020 * on memory address.
1021 * u_alloc_line() gets a chunk from the free lists.
1022 * u_free_line() returns a chunk to the free lists.
1023 * curbuf->b_m_search points to the chunk before the chunk that was
1024 * freed/allocated the last time.
1025 * curbuf->b_mb_current points to the b_head where curbuf->b_m_search
1026 * points into the free list.
1027 *
1028 *
1029 * b_block_head /---> block #1 /---> block #2
1030 * mb_next ---/ mb_next ---/ mb_next ---> NULL
1031 * mb_info mb_info mb_info
1032 * | | |
1033 * V V V
1034 * NULL free chunk #1.1 free chunk #2.1
1035 * | |
1036 * V V
1037 * free chunk #1.2 NULL
1038 * |
1039 * V
1040 * NULL
1041 *
1042 * When a single free chunk list would have been used, it could take a lot
1043 * of time in u_free_line() to find the correct place to insert a chunk in the
1044 * free list. The single free list would become very long when many lines are
1045 * changed (e.g. with :%s/^M$//).
1046 */
1047
1048 /*
1049 * this blocksize is used when allocating new lines
1050 */
1051#define MEMBLOCKSIZE 2044
1052
1053/*
1054 * The size field contains the size of the chunk, including the size field
1055 * itself.
1056 *
1057 * When the chunk is not in-use it is preceded with the m_info structure.
1058 * The m_next field links it in one of the free chunk lists.
1059 *
1060 * On most unix systems structures have to be longword (32 or 64 bit) aligned.
1061 * On most other systems they are short (16 bit) aligned.
1062 */
1063
1064/* the structure definitions are now in structs.h */
1065
1066#ifdef ALIGN_LONG
1067 /* size of m_size */
1068# define M_OFFSET (sizeof(long_u))
1069#else
1070 /* size of m_size */
1071# define M_OFFSET (sizeof(short_u))
1072#endif
1073
1074/*
1075 * Allocate a block of memory and link it in the allocated block list.
1076 */
1077 static char_u *
1078u_blockalloc(size)
1079 long_u size;
1080{
1081 mblock_T *p;
1082 mblock_T *mp, *next;
1083
1084 p = (mblock_T *)lalloc(size + sizeof(mblock_T), FALSE);
1085 if (p != NULL)
1086 {
1087 /* Insert the block into the allocated block list, keeping it
1088 sorted on address. */
1089 for (mp = &curbuf->b_block_head;
1090 (next = mp->mb_next) != NULL && next < p;
1091 mp = next)
1092 ;
1093 p->mb_next = next; /* link in block list */
1094 p->mb_size = size;
1095 mp->mb_next = p;
1096 p->mb_info.m_next = NULL; /* clear free list */
1097 p->mb_info.m_size = 0;
1098 curbuf->b_mb_current = p; /* remember current block */
1099 curbuf->b_m_search = NULL;
1100 p++; /* return usable memory */
1101 }
1102 return (char_u *)p;
1103}
1104
1105/*
1106 * free all allocated memory blocks for the buffer 'buf'
1107 */
1108 void
1109u_blockfree(buf)
1110 buf_T *buf;
1111{
1112 mblock_T *p, *np;
1113
1114 for (p = buf->b_block_head.mb_next; p != NULL; p = np)
1115 {
1116 np = p->mb_next;
1117 vim_free(p);
1118 }
1119 buf->b_block_head.mb_next = NULL;
1120 buf->b_m_search = NULL;
1121 buf->b_mb_current = NULL;
1122}
1123
1124/*
1125 * Free a chunk of memory for the current buffer.
1126 * Insert the chunk into the correct free list, keeping it sorted on address.
1127 */
1128 static void
1129u_free_line(ptr, keep)
1130 char_u *ptr;
1131 int keep; /* don't free the block when it's empty */
1132{
1133 minfo_T *next;
1134 minfo_T *prev, *curr;
1135 minfo_T *mp;
1136 mblock_T *nextb;
1137 mblock_T *prevb;
1138
1139 if (ptr == NULL || ptr == IObuff)
1140 return; /* illegal address can happen in out-of-memory situations */
1141
1142 mp = (minfo_T *)(ptr - M_OFFSET);
1143
1144 /* find block where chunk could be a part off */
1145 /* if we change curbuf->b_mb_current, curbuf->b_m_search is set to NULL */
1146 if (curbuf->b_mb_current == NULL || mp < (minfo_T *)curbuf->b_mb_current)
1147 {
1148 curbuf->b_mb_current = curbuf->b_block_head.mb_next;
1149 curbuf->b_m_search = NULL;
1150 }
1151 if ((nextb = curbuf->b_mb_current->mb_next) != NULL
1152 && (minfo_T *)nextb < mp)
1153 {
1154 curbuf->b_mb_current = nextb;
1155 curbuf->b_m_search = NULL;
1156 }
1157 while ((nextb = curbuf->b_mb_current->mb_next) != NULL
1158 && (minfo_T *)nextb < mp)
1159 curbuf->b_mb_current = nextb;
1160
1161 curr = NULL;
1162 /*
1163 * If mp is smaller than curbuf->b_m_search->m_next go to the start of
1164 * the free list
1165 */
1166 if (curbuf->b_m_search == NULL || mp < (curbuf->b_m_search->m_next))
1167 next = &(curbuf->b_mb_current->mb_info);
1168 else
1169 next = curbuf->b_m_search;
1170 /*
1171 * The following loop is executed very often.
1172 * Therefore it has been optimized at the cost of readability.
1173 * Keep it fast!
1174 */
1175#ifdef SLOW_BUT_EASY_TO_READ
1176 do
1177 {
1178 prev = curr;
1179 curr = next;
1180 next = next->m_next;
1181 }
1182 while (mp > next && next != NULL);
1183#else
1184 do /* first, middle, last */
1185 {
1186 prev = next->m_next; /* curr, next, prev */
1187 if (prev == NULL || mp <= prev)
1188 {
1189 prev = curr;
1190 curr = next;
1191 next = next->m_next;
1192 break;
1193 }
1194 curr = prev->m_next; /* next, prev, curr */
1195 if (curr == NULL || mp <= curr)
1196 {
1197 prev = next;
1198 curr = prev->m_next;
1199 next = curr->m_next;
1200 break;
1201 }
1202 next = curr->m_next; /* prev, curr, next */
1203 }
1204 while (mp > next && next != NULL);
1205#endif
1206
1207 /* if *mp and *next are concatenated, join them into one chunk */
1208 if ((char_u *)mp + mp->m_size == (char_u *)next)
1209 {
1210 mp->m_size += next->m_size;
1211 mp->m_next = next->m_next;
1212 }
1213 else
1214 mp->m_next = next;
1215
1216 /* if *curr and *mp are concatenated, join them */
1217 if (prev != NULL && (char_u *)curr + curr->m_size == (char_u *)mp)
1218 {
1219 curr->m_size += mp->m_size;
1220 curr->m_next = mp->m_next;
1221 curbuf->b_m_search = prev;
1222 }
1223 else
1224 {
1225 curr->m_next = mp;
1226 curbuf->b_m_search = curr; /* put curbuf->b_m_search before freed
1227 chunk */
1228 }
1229
1230 /*
1231 * If the block only containes free memory now, release it.
1232 */
1233 if (!keep && curbuf->b_mb_current->mb_size
1234 == curbuf->b_mb_current->mb_info.m_next->m_size)
1235 {
1236 /* Find the block before the current one to be able to unlink it from
1237 * the list of blocks. */
1238 prevb = &curbuf->b_block_head;
1239 for (nextb = prevb->mb_next; nextb != curbuf->b_mb_current;
1240 nextb = nextb->mb_next)
1241 prevb = nextb;
1242 prevb->mb_next = nextb->mb_next;
1243 vim_free(nextb);
1244 curbuf->b_mb_current = NULL;
1245 curbuf->b_m_search = NULL;
1246 }
1247}
1248
1249/*
1250 * Allocate and initialize a new line structure with room for at least
1251 * 'size' characters plus a terminating NUL.
1252 */
1253 static char_u *
1254u_alloc_line(size)
1255 unsigned size;
1256{
1257 minfo_T *mp, *mprev, *mp2;
1258 mblock_T *mbp;
1259 int size_align;
1260
1261 /*
1262 * Add room for size field and trailing NUL byte.
1263 * Adjust for minimal size (must be able to store minfo_T
1264 * plus a trailing NUL, so the chunk can be released again)
1265 */
1266 size += M_OFFSET + 1;
1267 if (size < sizeof(minfo_T) + 1)
1268 size = sizeof(minfo_T) + 1;
1269
1270 /*
1271 * round size up for alignment
1272 */
1273 size_align = (size + ALIGN_MASK) & ~ALIGN_MASK;
1274
1275 /*
1276 * If curbuf->b_m_search is NULL (uninitialized free list) start at
1277 * curbuf->b_block_head
1278 */
1279 if (curbuf->b_mb_current == NULL || curbuf->b_m_search == NULL)
1280 {
1281 curbuf->b_mb_current = &curbuf->b_block_head;
1282 curbuf->b_m_search = &(curbuf->b_block_head.mb_info);
1283 }
1284
1285 /* search for space in free list */
1286 mprev = curbuf->b_m_search;
1287 mbp = curbuf->b_mb_current;
1288 mp = curbuf->b_m_search->m_next;
1289 if (mp == NULL)
1290 {
1291 if (mbp->mb_next)
1292 mbp = mbp->mb_next;
1293 else
1294 mbp = &curbuf->b_block_head;
1295 mp = curbuf->b_m_search = &(mbp->mb_info);
1296 }
1297 while (mp->m_size < size)
1298 {
1299 if (mp == curbuf->b_m_search) /* back where we started in free
1300 chunk list */
1301 {
1302 if (mbp->mb_next)
1303 mbp = mbp->mb_next;
1304 else
1305 mbp = &curbuf->b_block_head;
1306 mp = curbuf->b_m_search = &(mbp->mb_info);
1307 if (mbp == curbuf->b_mb_current) /* back where we started in
1308 block list */
1309 {
1310 int n = (size_align > (MEMBLOCKSIZE / 4)
1311 ? size_align : MEMBLOCKSIZE);
1312
1313 mp = (minfo_T *)u_blockalloc((long_u)n);
1314 if (mp == NULL)
1315 return (NULL);
1316 mp->m_size = n;
1317 u_free_line((char_u *)mp + M_OFFSET, TRUE);
1318 mp = curbuf->b_m_search;
1319 mbp = curbuf->b_mb_current;
1320 }
1321 }
1322 mprev = mp;
1323 if ((mp = mp->m_next) == NULL) /* at end of the list */
1324 mp = &(mbp->mb_info); /* wrap around to begin */
1325 }
1326
1327 /* if the chunk we found is large enough, split it up in two */
1328 if ((long)mp->m_size - size_align >= (long)(sizeof(minfo_T) + 1))
1329 {
1330 mp2 = (minfo_T *)((char_u *)mp + size_align);
1331 mp2->m_size = mp->m_size - size_align;
1332 mp2->m_next = mp->m_next;
1333 mprev->m_next = mp2;
1334 mp->m_size = size_align;
1335 }
1336 else /* remove *mp from the free list */
1337 {
1338 mprev->m_next = mp->m_next;
1339 }
1340 curbuf->b_m_search = mprev;
1341 curbuf->b_mb_current = mbp;
1342
1343 mp = (minfo_T *)((char_u *)mp + M_OFFSET);
1344 *(char_u *)mp = NUL; /* set the first byte to NUL */
1345
1346 return ((char_u *)mp);
1347}
1348
1349/*
1350 * u_save_line(): allocate memory with u_alloc_line() and copy line 'lnum'
1351 * into it.
1352 */
1353 static char_u *
1354u_save_line(lnum)
1355 linenr_T lnum;
1356{
1357 char_u *src;
1358 char_u *dst;
1359 unsigned len;
1360
1361 src = ml_get(lnum);
1362 len = (unsigned)STRLEN(src);
1363 if ((dst = u_alloc_line(len)) != NULL)
1364 mch_memmove(dst, src, (size_t)(len + 1));
1365 return (dst);
1366}
1367
1368/*
1369 * Check if the 'modified' flag is set, or 'ff' has changed (only need to
1370 * check the first character, because it can only be "dos", "unix" or "mac").
1371 * "nofile" and "scratch" type buffers are considered to always be unchanged.
1372 */
1373 int
1374bufIsChanged(buf)
1375 buf_T *buf;
1376{
1377 return
1378#ifdef FEAT_QUICKFIX
1379 !bt_dontwrite(buf) &&
1380#endif
1381 (buf->b_changed || file_ff_differs(buf));
1382}
1383
1384 int
1385curbufIsChanged()
1386{
1387 return
1388#ifdef FEAT_QUICKFIX
1389 !bt_dontwrite(curbuf) &&
1390#endif
1391 (curbuf->b_changed || file_ff_differs(curbuf));
1392}