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