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