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