blob: 0ad3ae1a1c31db5eda55c568d9a0218e6bfbce31 [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 {
407 if ((uep->ue_array[i] = u_save_line(lnum++)) == NULL)
408 {
409 u_freeentry(uep, i);
410 goto nomem;
411 }
412 }
413 }
414 uep->ue_next = curbuf->b_u_newhead->uh_entry;
415 curbuf->b_u_newhead->uh_entry = uep;
416 curbuf->b_u_synced = FALSE;
417 undo_undoes = FALSE;
418
419 return OK;
420
421nomem:
422 msg_silent = 0; /* must display the prompt */
423 if (ask_yesno((char_u *)_("No undo possible; continue anyway"), TRUE)
424 == 'y')
425 {
426 undo_off = TRUE; /* will be reset when character typed */
427 return OK;
428 }
429 do_outofmem_msg((long_u)0);
430 return FAIL;
431}
432
433/*
434 * If 'cpoptions' contains 'u': Undo the previous undo or redo (vi compatible).
435 * If 'cpoptions' does not contain 'u': Always undo.
436 */
437 void
438u_undo(count)
439 int count;
440{
441 /*
442 * If we get an undo command while executing a macro, we behave like the
443 * original vi. If this happens twice in one macro the result will not
444 * be compatible.
445 */
446 if (curbuf->b_u_synced == FALSE)
447 {
448 u_sync();
449 count = 1;
450 }
451
452 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
453 undo_undoes = TRUE;
454 else
455 undo_undoes = !undo_undoes;
456 u_doit(count);
457}
458
459/*
460 * If 'cpoptions' contains 'u': Repeat the previous undo or redo.
461 * If 'cpoptions' does not contain 'u': Always redo.
462 */
463 void
464u_redo(count)
465 int count;
466{
467 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
468 undo_undoes = FALSE;
469 u_doit(count);
470}
471
472/*
473 * Undo or redo, depending on 'undo_undoes', 'count' times.
474 */
475 static void
476u_doit(count)
477 int count;
478{
479 /* Don't allow changes when 'modifiable' is off. */
480 if (!curbuf->b_p_ma)
481 {
482 EMSG(_(e_modifiable));
483 return;
484 }
485#ifdef HAVE_SANDBOX
486 /* In the sandbox it's not allowed to change the text. */
487 if (sandbox != 0)
488 {
489 EMSG(_(e_sandbox));
490 return;
491 }
492#endif
493
494 u_newcount = 0;
495 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000496 if (curbuf->b_ml.ml_flags & ML_EMPTY)
497 u_oldcount = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 while (count--)
499 {
500 if (undo_undoes)
501 {
502 if (curbuf->b_u_curhead == NULL) /* first undo */
503 curbuf->b_u_curhead = curbuf->b_u_newhead;
504 else if (p_ul > 0) /* multi level undo */
505 /* get next undo */
506 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_next;
507 /* nothing to undo */
508 if (curbuf->b_u_numhead == 0 || curbuf->b_u_curhead == NULL)
509 {
510 /* stick curbuf->b_u_curhead at end */
511 curbuf->b_u_curhead = curbuf->b_u_oldhead;
512 beep_flush();
513 break;
514 }
515
516 u_undoredo();
517 }
518 else
519 {
520 if (curbuf->b_u_curhead == NULL || p_ul <= 0)
521 {
522 beep_flush(); /* nothing to redo */
523 break;
524 }
525
526 u_undoredo();
527 /* advance for next redo */
528 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_prev;
529 }
530 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000531 if (curbuf->b_ml.ml_flags & ML_EMPTY)
532 --u_newcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 u_undo_end();
534}
535
536/*
537 * u_undoredo: common code for undo and redo
538 *
539 * The lines in the file are replaced by the lines in the entry list at
540 * curbuf->b_u_curhead. The replaced lines in the file are saved in the entry
541 * list for the next undo/redo.
542 */
543 static void
544u_undoredo()
545{
546 char_u **newarray = NULL;
547 linenr_T oldsize;
548 linenr_T newsize;
549 linenr_T top, bot;
550 linenr_T lnum;
551 linenr_T newlnum = MAXLNUM;
552 long i;
553 u_entry_T *uep, *nuep;
554 u_entry_T *newlist = NULL;
555 int old_flags;
556 int new_flags;
557 pos_T namedm[NMARKS];
558 int empty_buffer; /* buffer became empty */
559
560 old_flags = curbuf->b_u_curhead->uh_flags;
561 new_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
562 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
563 setpcmark();
564
565 /*
566 * save marks before undo/redo
567 */
568 mch_memmove(namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
569 curbuf->b_op_start.lnum = curbuf->b_ml.ml_line_count;
570 curbuf->b_op_start.col = 0;
571 curbuf->b_op_end.lnum = 0;
572 curbuf->b_op_end.col = 0;
573
574 for (uep = curbuf->b_u_curhead->uh_entry; uep != NULL; uep = nuep)
575 {
576 top = uep->ue_top;
577 bot = uep->ue_bot;
578 if (bot == 0)
579 bot = curbuf->b_ml.ml_line_count + 1;
580 if (top > curbuf->b_ml.ml_line_count || top >= bot
581 || bot > curbuf->b_ml.ml_line_count + 1)
582 {
583 EMSG(_("E438: u_undo: line numbers wrong"));
584 changed(); /* don't want UNCHANGED now */
585 return;
586 }
587
588 oldsize = bot - top - 1; /* number of lines before undo */
589 newsize = uep->ue_size; /* number of lines after undo */
590
591 if (top < newlnum)
592 {
593 /* If the saved cursor is somewhere in this undo block, move it to
594 * the remembered position. Makes "gwap" put the cursor back
595 * where it was. */
596 lnum = curbuf->b_u_curhead->uh_cursor.lnum;
597 if (lnum >= top && lnum <= top + newsize + 1)
598 {
599 curwin->w_cursor = curbuf->b_u_curhead->uh_cursor;
600 newlnum = curwin->w_cursor.lnum - 1;
601 }
602 else
603 {
604 /* Use the first line that actually changed. Avoids that
605 * undoing auto-formatting puts the cursor in the previous
606 * line. */
607 for (i = 0; i < newsize && i < oldsize; ++i)
608 if (STRCMP(uep->ue_array[i], ml_get(top + 1 + i)) != 0)
609 break;
610 if (i == newsize && newlnum == MAXLNUM && uep->ue_next == NULL)
611 {
612 newlnum = top;
613 curwin->w_cursor.lnum = newlnum + 1;
614 }
615 else if (i < newsize)
616 {
617 newlnum = top + i;
618 curwin->w_cursor.lnum = newlnum + 1;
619 }
620 }
621 }
622
623 empty_buffer = FALSE;
624
625 /* delete the lines between top and bot and save them in newarray */
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000626 if (oldsize > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000628 if ((newarray = (char_u **)U_ALLOC_LINE(
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 (unsigned)(sizeof(char_u *) * oldsize))) == NULL)
630 {
631 do_outofmem_msg((long_u)(sizeof(char_u *) * oldsize));
632 /*
633 * We have messed up the entry list, repair is impossible.
634 * we have to free the rest of the list.
635 */
636 while (uep != NULL)
637 {
638 nuep = uep->ue_next;
639 u_freeentry(uep, uep->ue_size);
640 uep = nuep;
641 }
642 break;
643 }
644 /* delete backwards, it goes faster in most cases */
645 for (lnum = bot - 1, i = oldsize; --i >= 0; --lnum)
646 {
647 /* what can we do when we run out of memory? */
648 if ((newarray[i] = u_save_line(lnum)) == NULL)
649 do_outofmem_msg((long_u)0);
650 /* remember we deleted the last line in the buffer, and a
651 * dummy empty line will be inserted */
652 if (curbuf->b_ml.ml_line_count == 1)
653 empty_buffer = TRUE;
654 ml_delete(lnum, FALSE);
655 }
656 }
657
658 /* insert the lines in u_array between top and bot */
659 if (newsize)
660 {
661 for (lnum = top, i = 0; i < newsize; ++i, ++lnum)
662 {
663 /*
664 * If the file is empty, there is an empty line 1 that we
665 * should get rid of, by replacing it with the new line
666 */
667 if (empty_buffer && lnum == 0)
668 ml_replace((linenr_T)1, uep->ue_array[i], TRUE);
669 else
670 ml_append(lnum, uep->ue_array[i], (colnr_T)0, FALSE);
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000671 U_FREE_LINE(uep->ue_array[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000673 U_FREE_LINE((char_u *)uep->ue_array);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 }
675
676 /* adjust marks */
677 if (oldsize != newsize)
678 {
679 mark_adjust(top + 1, top + oldsize, (long)MAXLNUM,
680 (long)newsize - (long)oldsize);
681 if (curbuf->b_op_start.lnum > top + oldsize)
682 curbuf->b_op_start.lnum += newsize - oldsize;
683 if (curbuf->b_op_end.lnum > top + oldsize)
684 curbuf->b_op_end.lnum += newsize - oldsize;
685 }
686
687 changed_lines(top + 1, 0, bot, newsize - oldsize);
688
689 /* set '[ and '] mark */
690 if (top + 1 < curbuf->b_op_start.lnum)
691 curbuf->b_op_start.lnum = top + 1;
692 if (newsize == 0 && top + 1 > curbuf->b_op_end.lnum)
693 curbuf->b_op_end.lnum = top + 1;
694 else if (top + newsize > curbuf->b_op_end.lnum)
695 curbuf->b_op_end.lnum = top + newsize;
696
697 u_newcount += newsize;
698 u_oldcount += oldsize;
699 uep->ue_size = oldsize;
700 uep->ue_array = newarray;
701 uep->ue_bot = top + newsize + 1;
702
703 /*
704 * insert this entry in front of the new entry list
705 */
706 nuep = uep->ue_next;
707 uep->ue_next = newlist;
708 newlist = uep;
709 }
710
711 curbuf->b_u_curhead->uh_entry = newlist;
712 curbuf->b_u_curhead->uh_flags = new_flags;
713 if ((old_flags & UH_EMPTYBUF) && bufempty())
714 curbuf->b_ml.ml_flags |= ML_EMPTY;
715 if (old_flags & UH_CHANGED)
716 changed();
717 else
Bram Moolenaar009b2592004-10-24 19:18:58 +0000718#ifdef FEAT_NETBEANS_INTG
719 /* per netbeans undo rules, keep it as modified */
720 if (!isNetbeansModified(curbuf))
721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 unchanged(curbuf, FALSE);
723
724 /*
725 * restore marks from before undo/redo
726 */
727 for (i = 0; i < NMARKS; ++i)
728 if (curbuf->b_u_curhead->uh_namedm[i].lnum)
729 {
730 curbuf->b_namedm[i] = curbuf->b_u_curhead->uh_namedm[i];
731 curbuf->b_u_curhead->uh_namedm[i] = namedm[i];
732 }
733
734 /*
735 * If the cursor is only off by one line, put it at the same position as
736 * before starting the change (for the "o" command).
737 * Otherwise the cursor should go to the first undone line.
738 */
739 if (curbuf->b_u_curhead->uh_cursor.lnum + 1 == curwin->w_cursor.lnum
740 && curwin->w_cursor.lnum > 1)
741 --curwin->w_cursor.lnum;
742 if (curbuf->b_u_curhead->uh_cursor.lnum == curwin->w_cursor.lnum)
743 {
744 curwin->w_cursor.col = curbuf->b_u_curhead->uh_cursor.col;
745#ifdef FEAT_VIRTUALEDIT
746 if (virtual_active() && curbuf->b_u_curhead->uh_cursor_vcol >= 0)
747 coladvance((colnr_T)curbuf->b_u_curhead->uh_cursor_vcol);
748 else
749 curwin->w_cursor.coladd = 0;
750#endif
751 }
752 else if (curwin->w_cursor.lnum <= curbuf->b_ml.ml_line_count)
753 beginline(BL_SOL | BL_FIX);
754 else
755 {
756 /* We get here with the current cursor line being past the end (eg
757 * after adding lines at the end of the file, and then undoing it).
758 * check_cursor() will move the cursor to the last line. Move it to
759 * the first column here. */
760 curwin->w_cursor.col = 0;
761#ifdef FEAT_VIRTUALEDIT
762 curwin->w_cursor.coladd = 0;
763#endif
764 }
765
766 /* Make sure the cursor is on an existing line and column. */
767 check_cursor();
768}
769
770/*
771 * If we deleted or added lines, report the number of less/more lines.
772 * Otherwise, report the number of changes (this may be incorrect
773 * in some cases, but it's better than nothing).
774 */
775 static void
776u_undo_end()
777{
778 if ((u_oldcount -= u_newcount) != 0)
779 msgmore(-u_oldcount);
780 else if (u_newcount > p_report)
781 {
782 if (u_newcount == 1)
783 MSG(_("1 change"));
784 else
785 smsg((char_u *)_("%ld changes"), u_newcount);
786 }
787#ifdef FEAT_FOLDING
788 if ((fdo_flags & FDO_UNDO) && KeyTyped)
789 foldOpenCursor();
790#endif
791}
792
793/*
794 * u_sync: stop adding to the current entry list
795 */
796 void
797u_sync()
798{
799 if (curbuf->b_u_synced)
800 return; /* already synced */
801#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
802 if (im_is_preediting())
803 return; /* XIM is busy, don't break an undo sequence */
804#endif
805 if (p_ul < 0)
806 curbuf->b_u_synced = TRUE; /* no entries, nothing to do */
807 else
808 {
809 u_getbot(); /* compute ue_bot of previous u_save */
810 curbuf->b_u_curhead = NULL;
811 }
812}
813
814/*
815 * Called after writing the file and setting b_changed to FALSE.
816 * Now an undo means that the buffer is modified.
817 */
818 void
819u_unchanged(buf)
820 buf_T *buf;
821{
822 struct u_header *uh;
823
824 for (uh = buf->b_u_newhead; uh; uh = uh->uh_next)
825 uh->uh_flags |= UH_CHANGED;
826 buf->b_did_warn = FALSE;
827}
828
829/*
830 * Get pointer to last added entry.
831 * If it's not valid, give an error message and return NULL.
832 */
833 static u_entry_T *
834u_get_headentry()
835{
836 if (curbuf->b_u_newhead == NULL || curbuf->b_u_newhead->uh_entry == NULL)
837 {
838 EMSG(_("E439: undo list corrupt"));
839 return NULL;
840 }
841 return curbuf->b_u_newhead->uh_entry;
842}
843
844/*
845 * u_getbot(): compute the line number of the previous u_save
846 * It is called only when b_u_synced is FALSE.
847 */
848 static void
849u_getbot()
850{
851 u_entry_T *uep;
852 linenr_T extra;
853
854 uep = u_get_headentry(); /* check for corrupt undo list */
855 if (uep == NULL)
856 return;
857
858 uep = curbuf->b_u_newhead->uh_getbot_entry;
859 if (uep != NULL)
860 {
861 /*
862 * the new ue_bot is computed from the number of lines that has been
863 * inserted (0 - deleted) since calling u_save. This is equal to the
864 * old line count subtracted from the current line count.
865 */
866 extra = curbuf->b_ml.ml_line_count - uep->ue_lcount;
867 uep->ue_bot = uep->ue_top + uep->ue_size + 1 + extra;
868 if (uep->ue_bot < 1 || uep->ue_bot > curbuf->b_ml.ml_line_count)
869 {
870 EMSG(_("E440: undo line missing"));
871 uep->ue_bot = uep->ue_top + 1; /* assume all lines deleted, will
872 * get all the old lines back
873 * without deleting the current
874 * ones */
875 }
876
877 curbuf->b_u_newhead->uh_getbot_entry = NULL;
878 }
879
880 curbuf->b_u_synced = TRUE;
881}
882
883/*
884 * u_freelist: free one entry list and adjust the pointers
885 */
886 static void
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000887u_freelist(buf, uhp)
888 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 struct u_header *uhp;
890{
891 u_entry_T *uep, *nuep;
892
893 for (uep = uhp->uh_entry; uep != NULL; uep = nuep)
894 {
895 nuep = uep->ue_next;
896 u_freeentry(uep, uep->ue_size);
897 }
898
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000899 if (buf->b_u_curhead == uhp)
900 buf->b_u_curhead = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901
902 if (uhp->uh_next == NULL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000903 buf->b_u_oldhead = uhp->uh_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 else
905 uhp->uh_next->uh_prev = uhp->uh_prev;
906
907 if (uhp->uh_prev == NULL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000908 buf->b_u_newhead = uhp->uh_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 else
910 uhp->uh_prev->uh_next = uhp->uh_next;
911
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000912 U_FREE_LINE((char_u *)uhp);
913 --buf->b_u_numhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914}
915
916/*
917 * free entry 'uep' and 'n' lines in uep->ue_array[]
918 */
919 static void
920u_freeentry(uep, n)
921 u_entry_T *uep;
922 long n;
923{
924 while (n)
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000925 U_FREE_LINE(uep->ue_array[--n]);
926 U_FREE_LINE((char_u *)uep);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927}
928
929/*
930 * invalidate the undo buffer; called when storage has already been released
931 */
932 void
933u_clearall(buf)
934 buf_T *buf;
935{
936 buf->b_u_newhead = buf->b_u_oldhead = buf->b_u_curhead = NULL;
937 buf->b_u_synced = TRUE;
938 buf->b_u_numhead = 0;
939 buf->b_u_line_ptr = NULL;
940 buf->b_u_line_lnum = 0;
941}
942
943/*
944 * save the line "lnum" for the "U" command
945 */
946 void
947u_saveline(lnum)
948 linenr_T lnum;
949{
950 if (lnum == curbuf->b_u_line_lnum) /* line is already saved */
951 return;
952 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) /* should never happen */
953 return;
954 u_clearline();
955 curbuf->b_u_line_lnum = lnum;
956 if (curwin->w_cursor.lnum == lnum)
957 curbuf->b_u_line_colnr = curwin->w_cursor.col;
958 else
959 curbuf->b_u_line_colnr = 0;
960 if ((curbuf->b_u_line_ptr = u_save_line(lnum)) == NULL)
961 do_outofmem_msg((long_u)0);
962}
963
964/*
965 * clear the line saved for the "U" command
966 * (this is used externally for crossing a line while in insert mode)
967 */
968 void
969u_clearline()
970{
971 if (curbuf->b_u_line_ptr != NULL)
972 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000973 U_FREE_LINE(curbuf->b_u_line_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 curbuf->b_u_line_ptr = NULL;
975 curbuf->b_u_line_lnum = 0;
976 }
977}
978
979/*
980 * Implementation of the "U" command.
981 * Differentiation from vi: "U" can be undone with the next "U".
982 * We also allow the cursor to be in another line.
983 */
984 void
985u_undoline()
986{
987 colnr_T t;
988 char_u *oldp;
989
990 if (undo_off)
991 return;
992
993 if (curbuf->b_u_line_ptr == NULL ||
994 curbuf->b_u_line_lnum > curbuf->b_ml.ml_line_count)
995 {
996 beep_flush();
997 return;
998 }
999 /* first save the line for the 'u' command */
1000 if (u_savecommon(curbuf->b_u_line_lnum - 1,
1001 curbuf->b_u_line_lnum + 1, (linenr_T)0) == FAIL)
1002 return;
1003 oldp = u_save_line(curbuf->b_u_line_lnum);
1004 if (oldp == NULL)
1005 {
1006 do_outofmem_msg((long_u)0);
1007 return;
1008 }
1009 ml_replace(curbuf->b_u_line_lnum, curbuf->b_u_line_ptr, TRUE);
1010 changed_bytes(curbuf->b_u_line_lnum, 0);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001011 U_FREE_LINE(curbuf->b_u_line_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 curbuf->b_u_line_ptr = oldp;
1013
1014 t = curbuf->b_u_line_colnr;
1015 if (curwin->w_cursor.lnum == curbuf->b_u_line_lnum)
1016 curbuf->b_u_line_colnr = curwin->w_cursor.col;
1017 curwin->w_cursor.col = t;
1018 curwin->w_cursor.lnum = curbuf->b_u_line_lnum;
1019}
1020
1021/*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001022 * There are two implementations of the memory management for undo:
1023 * 1. Use the standard malloc()/free() functions.
1024 * This should be fast for allocating memory, but when a buffer is
1025 * abandoned every single allocated chunk must be freed, which may be slow.
1026 * 2. Allocate larger blocks of memory and keep track of chunks ourselves.
1027 * This is fast for abandoning, but the use of linked lists is slow for
1028 * finding a free chunk. Esp. when a lot of lines are changed or deleted.
1029 * A bit of profiling showed that the first method is faster, especially when
1030 * making a large number of changes, under the condition that malloc()/free()
1031 * is implemented efficiently.
1032 */
1033#ifdef U_USE_MALLOC
1034/*
1035 * Version of undo memory allocation using malloc()/free()
1036 *
1037 * U_FREE_LINE() and U_ALLOC_LINE() are macros that invoke vim_free() and
1038 * lalloc() directly.
1039 */
1040
1041/*
1042 * Free all allocated memory blocks for the buffer 'buf'.
1043 */
1044 void
1045u_blockfree(buf)
1046 buf_T *buf;
1047{
1048 while (buf->b_u_newhead != NULL)
1049 u_freelist(buf, buf->b_u_newhead);
1050}
1051
1052#else
1053/*
1054 * Storage allocation for the undo lines and blocks of the current file.
1055 * Version where Vim keeps track of the available memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 */
1057
1058/*
1059 * Memory is allocated in relatively large blocks. These blocks are linked
1060 * in the allocated block list, headed by curbuf->b_block_head. They are all
1061 * freed when abandoning a file, so we don't have to free every single line.
1062 * The list is kept sorted on memory address.
1063 * block_alloc() allocates a block.
1064 * m_blockfree() frees all blocks.
1065 *
1066 * The available chunks of memory are kept in free chunk lists. There is
1067 * one free list for each block of allocated memory. The list is kept sorted
1068 * on memory address.
1069 * u_alloc_line() gets a chunk from the free lists.
1070 * u_free_line() returns a chunk to the free lists.
1071 * curbuf->b_m_search points to the chunk before the chunk that was
1072 * freed/allocated the last time.
1073 * curbuf->b_mb_current points to the b_head where curbuf->b_m_search
1074 * points into the free list.
1075 *
1076 *
1077 * b_block_head /---> block #1 /---> block #2
1078 * mb_next ---/ mb_next ---/ mb_next ---> NULL
1079 * mb_info mb_info mb_info
1080 * | | |
1081 * V V V
1082 * NULL free chunk #1.1 free chunk #2.1
1083 * | |
1084 * V V
1085 * free chunk #1.2 NULL
1086 * |
1087 * V
1088 * NULL
1089 *
1090 * When a single free chunk list would have been used, it could take a lot
1091 * of time in u_free_line() to find the correct place to insert a chunk in the
1092 * free list. The single free list would become very long when many lines are
1093 * changed (e.g. with :%s/^M$//).
1094 */
1095
1096 /*
1097 * this blocksize is used when allocating new lines
1098 */
1099#define MEMBLOCKSIZE 2044
1100
1101/*
1102 * The size field contains the size of the chunk, including the size field
1103 * itself.
1104 *
1105 * When the chunk is not in-use it is preceded with the m_info structure.
1106 * The m_next field links it in one of the free chunk lists.
1107 *
1108 * On most unix systems structures have to be longword (32 or 64 bit) aligned.
1109 * On most other systems they are short (16 bit) aligned.
1110 */
1111
1112/* the structure definitions are now in structs.h */
1113
1114#ifdef ALIGN_LONG
1115 /* size of m_size */
1116# define M_OFFSET (sizeof(long_u))
1117#else
1118 /* size of m_size */
1119# define M_OFFSET (sizeof(short_u))
1120#endif
1121
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001122static char_u *u_blockalloc __ARGS((long_u));
1123
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124/*
1125 * Allocate a block of memory and link it in the allocated block list.
1126 */
1127 static char_u *
1128u_blockalloc(size)
1129 long_u size;
1130{
1131 mblock_T *p;
1132 mblock_T *mp, *next;
1133
1134 p = (mblock_T *)lalloc(size + sizeof(mblock_T), FALSE);
1135 if (p != NULL)
1136 {
1137 /* Insert the block into the allocated block list, keeping it
1138 sorted on address. */
1139 for (mp = &curbuf->b_block_head;
1140 (next = mp->mb_next) != NULL && next < p;
1141 mp = next)
1142 ;
1143 p->mb_next = next; /* link in block list */
1144 p->mb_size = size;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001145 p->mb_maxsize = 0; /* nothing free yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 mp->mb_next = p;
1147 p->mb_info.m_next = NULL; /* clear free list */
1148 p->mb_info.m_size = 0;
1149 curbuf->b_mb_current = p; /* remember current block */
1150 curbuf->b_m_search = NULL;
1151 p++; /* return usable memory */
1152 }
1153 return (char_u *)p;
1154}
1155
1156/*
1157 * free all allocated memory blocks for the buffer 'buf'
1158 */
1159 void
1160u_blockfree(buf)
1161 buf_T *buf;
1162{
1163 mblock_T *p, *np;
1164
1165 for (p = buf->b_block_head.mb_next; p != NULL; p = np)
1166 {
1167 np = p->mb_next;
1168 vim_free(p);
1169 }
1170 buf->b_block_head.mb_next = NULL;
1171 buf->b_m_search = NULL;
1172 buf->b_mb_current = NULL;
1173}
1174
1175/*
1176 * Free a chunk of memory for the current buffer.
1177 * Insert the chunk into the correct free list, keeping it sorted on address.
1178 */
1179 static void
1180u_free_line(ptr, keep)
1181 char_u *ptr;
1182 int keep; /* don't free the block when it's empty */
1183{
1184 minfo_T *next;
1185 minfo_T *prev, *curr;
1186 minfo_T *mp;
1187 mblock_T *nextb;
1188 mblock_T *prevb;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001189 long_u maxsize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190
1191 if (ptr == NULL || ptr == IObuff)
1192 return; /* illegal address can happen in out-of-memory situations */
1193
1194 mp = (minfo_T *)(ptr - M_OFFSET);
1195
1196 /* find block where chunk could be a part off */
1197 /* if we change curbuf->b_mb_current, curbuf->b_m_search is set to NULL */
1198 if (curbuf->b_mb_current == NULL || mp < (minfo_T *)curbuf->b_mb_current)
1199 {
1200 curbuf->b_mb_current = curbuf->b_block_head.mb_next;
1201 curbuf->b_m_search = NULL;
1202 }
1203 if ((nextb = curbuf->b_mb_current->mb_next) != NULL
1204 && (minfo_T *)nextb < mp)
1205 {
1206 curbuf->b_mb_current = nextb;
1207 curbuf->b_m_search = NULL;
1208 }
1209 while ((nextb = curbuf->b_mb_current->mb_next) != NULL
1210 && (minfo_T *)nextb < mp)
1211 curbuf->b_mb_current = nextb;
1212
1213 curr = NULL;
1214 /*
1215 * If mp is smaller than curbuf->b_m_search->m_next go to the start of
1216 * the free list
1217 */
1218 if (curbuf->b_m_search == NULL || mp < (curbuf->b_m_search->m_next))
1219 next = &(curbuf->b_mb_current->mb_info);
1220 else
1221 next = curbuf->b_m_search;
1222 /*
1223 * The following loop is executed very often.
1224 * Therefore it has been optimized at the cost of readability.
1225 * Keep it fast!
1226 */
1227#ifdef SLOW_BUT_EASY_TO_READ
1228 do
1229 {
1230 prev = curr;
1231 curr = next;
1232 next = next->m_next;
1233 }
1234 while (mp > next && next != NULL);
1235#else
1236 do /* first, middle, last */
1237 {
1238 prev = next->m_next; /* curr, next, prev */
1239 if (prev == NULL || mp <= prev)
1240 {
1241 prev = curr;
1242 curr = next;
1243 next = next->m_next;
1244 break;
1245 }
1246 curr = prev->m_next; /* next, prev, curr */
1247 if (curr == NULL || mp <= curr)
1248 {
1249 prev = next;
1250 curr = prev->m_next;
1251 next = curr->m_next;
1252 break;
1253 }
1254 next = curr->m_next; /* prev, curr, next */
1255 }
1256 while (mp > next && next != NULL);
1257#endif
1258
1259 /* if *mp and *next are concatenated, join them into one chunk */
1260 if ((char_u *)mp + mp->m_size == (char_u *)next)
1261 {
1262 mp->m_size += next->m_size;
1263 mp->m_next = next->m_next;
1264 }
1265 else
1266 mp->m_next = next;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001267 maxsize = mp->m_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268
1269 /* if *curr and *mp are concatenated, join them */
1270 if (prev != NULL && (char_u *)curr + curr->m_size == (char_u *)mp)
1271 {
1272 curr->m_size += mp->m_size;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001273 maxsize = curr->m_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 curr->m_next = mp->m_next;
1275 curbuf->b_m_search = prev;
1276 }
1277 else
1278 {
1279 curr->m_next = mp;
1280 curbuf->b_m_search = curr; /* put curbuf->b_m_search before freed
1281 chunk */
1282 }
1283
1284 /*
1285 * If the block only containes free memory now, release it.
1286 */
1287 if (!keep && curbuf->b_mb_current->mb_size
1288 == curbuf->b_mb_current->mb_info.m_next->m_size)
1289 {
1290 /* Find the block before the current one to be able to unlink it from
1291 * the list of blocks. */
1292 prevb = &curbuf->b_block_head;
1293 for (nextb = prevb->mb_next; nextb != curbuf->b_mb_current;
1294 nextb = nextb->mb_next)
1295 prevb = nextb;
1296 prevb->mb_next = nextb->mb_next;
1297 vim_free(nextb);
1298 curbuf->b_mb_current = NULL;
1299 curbuf->b_m_search = NULL;
1300 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001301 else if (curbuf->b_mb_current->mb_maxsize < maxsize)
1302 curbuf->b_mb_current->mb_maxsize = maxsize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303}
1304
1305/*
1306 * Allocate and initialize a new line structure with room for at least
1307 * 'size' characters plus a terminating NUL.
1308 */
1309 static char_u *
1310u_alloc_line(size)
1311 unsigned size;
1312{
1313 minfo_T *mp, *mprev, *mp2;
1314 mblock_T *mbp;
1315 int size_align;
1316
1317 /*
1318 * Add room for size field and trailing NUL byte.
1319 * Adjust for minimal size (must be able to store minfo_T
1320 * plus a trailing NUL, so the chunk can be released again)
1321 */
1322 size += M_OFFSET + 1;
1323 if (size < sizeof(minfo_T) + 1)
1324 size = sizeof(minfo_T) + 1;
1325
1326 /*
1327 * round size up for alignment
1328 */
1329 size_align = (size + ALIGN_MASK) & ~ALIGN_MASK;
1330
1331 /*
1332 * If curbuf->b_m_search is NULL (uninitialized free list) start at
1333 * curbuf->b_block_head
1334 */
1335 if (curbuf->b_mb_current == NULL || curbuf->b_m_search == NULL)
1336 {
1337 curbuf->b_mb_current = &curbuf->b_block_head;
1338 curbuf->b_m_search = &(curbuf->b_block_head.mb_info);
1339 }
1340
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001341 /* Search for a block with enough space. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 mbp = curbuf->b_mb_current;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001343 while (mbp->mb_maxsize < size_align)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001345 if (mbp->mb_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 mbp = mbp->mb_next;
1347 else
1348 mbp = &curbuf->b_block_head;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001349 if (mbp == curbuf->b_mb_current)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001351 int n = (size_align > (MEMBLOCKSIZE / 4)
1352 ? size_align : MEMBLOCKSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001354 /* Back where we started in block list: need to add a new block
1355 * with enough space. */
1356 mp = (minfo_T *)u_blockalloc((long_u)n);
1357 if (mp == NULL)
1358 return (NULL);
1359 mp->m_size = n;
1360 u_free_line((char_u *)mp + M_OFFSET, TRUE);
1361 mbp = curbuf->b_mb_current;
1362 break;
1363 }
1364 }
1365 if (mbp != curbuf->b_mb_current)
1366 curbuf->b_m_search = &(mbp->mb_info);
1367
1368 /* In this block find a chunk with enough space. */
1369 mprev = curbuf->b_m_search;
1370 mp = curbuf->b_m_search->m_next;
1371 while (1)
1372 {
1373 if (mp == NULL) /* at end of the list */
1374 mp = &(mbp->mb_info); /* wrap around to begin */
1375 if (mp->m_size >= size)
1376 break;
1377 if (mp == curbuf->b_m_search)
1378 {
1379 /* back where we started in free chunk list: "cannot happen" */
1380 EMSG2(_(e_intern2), "u_alloc_line()");
1381 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 }
1383 mprev = mp;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001384 mp = mp->m_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 }
1386
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001387 /* when using the largest chunk adjust mb_maxsize */
1388 if (mp->m_size >= mbp->mb_maxsize)
1389 mbp->mb_maxsize = 0;
1390
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 /* if the chunk we found is large enough, split it up in two */
1392 if ((long)mp->m_size - size_align >= (long)(sizeof(minfo_T) + 1))
1393 {
1394 mp2 = (minfo_T *)((char_u *)mp + size_align);
1395 mp2->m_size = mp->m_size - size_align;
1396 mp2->m_next = mp->m_next;
1397 mprev->m_next = mp2;
1398 mp->m_size = size_align;
1399 }
1400 else /* remove *mp from the free list */
1401 {
1402 mprev->m_next = mp->m_next;
1403 }
1404 curbuf->b_m_search = mprev;
1405 curbuf->b_mb_current = mbp;
1406
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001407 /* If using the largest chunk need to find the new largest chunk */
1408 if (mbp->mb_maxsize == 0)
1409 for (mp2 = &(mbp->mb_info); mp2 != NULL; mp2 = mp2->m_next)
1410 if (mbp->mb_maxsize < mp2->m_size)
1411 mbp->mb_maxsize = mp2->m_size;
1412
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 mp = (minfo_T *)((char_u *)mp + M_OFFSET);
1414 *(char_u *)mp = NUL; /* set the first byte to NUL */
1415
1416 return ((char_u *)mp);
1417}
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001418#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419
1420/*
1421 * u_save_line(): allocate memory with u_alloc_line() and copy line 'lnum'
1422 * into it.
1423 */
1424 static char_u *
1425u_save_line(lnum)
1426 linenr_T lnum;
1427{
1428 char_u *src;
1429 char_u *dst;
1430 unsigned len;
1431
1432 src = ml_get(lnum);
1433 len = (unsigned)STRLEN(src);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001434 if ((dst = U_ALLOC_LINE(len)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 mch_memmove(dst, src, (size_t)(len + 1));
1436 return (dst);
1437}
1438
1439/*
1440 * Check if the 'modified' flag is set, or 'ff' has changed (only need to
1441 * check the first character, because it can only be "dos", "unix" or "mac").
1442 * "nofile" and "scratch" type buffers are considered to always be unchanged.
1443 */
1444 int
1445bufIsChanged(buf)
1446 buf_T *buf;
1447{
1448 return
1449#ifdef FEAT_QUICKFIX
1450 !bt_dontwrite(buf) &&
1451#endif
1452 (buf->b_changed || file_ff_differs(buf));
1453}
1454
1455 int
1456curbufIsChanged()
1457{
1458 return
1459#ifdef FEAT_QUICKFIX
1460 !bt_dontwrite(curbuf) &&
1461#endif
1462 (curbuf->b_changed || file_ff_differs(curbuf));
1463}