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