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