xref: /vim-8.2.3635/src/undo.c (revision 35fdbb54)
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 
660 	/* insert the lines in u_array between top and bot */
661 	if (newsize)
662 	{
663 	    for (lnum = top, i = 0; i < newsize; ++i, ++lnum)
664 	    {
665 		/*
666 		 * If the file is empty, there is an empty line 1 that we
667 		 * should get rid of, by replacing it with the new line
668 		 */
669 		if (empty_buffer && lnum == 0)
670 		    ml_replace((linenr_T)1, uep->ue_array[i], TRUE);
671 		else
672 		    ml_append(lnum, uep->ue_array[i], (colnr_T)0, FALSE);
673 		U_FREE_LINE(uep->ue_array[i]);
674 	    }
675 	    U_FREE_LINE((char_u *)uep->ue_array);
676 	}
677 
678 	/* adjust marks */
679 	if (oldsize != newsize)
680 	{
681 	    mark_adjust(top + 1, top + oldsize, (long)MAXLNUM,
682 					       (long)newsize - (long)oldsize);
683 	    if (curbuf->b_op_start.lnum > top + oldsize)
684 		curbuf->b_op_start.lnum += newsize - oldsize;
685 	    if (curbuf->b_op_end.lnum > top + oldsize)
686 		curbuf->b_op_end.lnum += newsize - oldsize;
687 	}
688 
689 	changed_lines(top + 1, 0, bot, newsize - oldsize);
690 
691 	/* set '[ and '] mark */
692 	if (top + 1 < curbuf->b_op_start.lnum)
693 	    curbuf->b_op_start.lnum = top + 1;
694 	if (newsize == 0 && top + 1 > curbuf->b_op_end.lnum)
695 	    curbuf->b_op_end.lnum = top + 1;
696 	else if (top + newsize > curbuf->b_op_end.lnum)
697 	    curbuf->b_op_end.lnum = top + newsize;
698 
699 	u_newcount += newsize;
700 	u_oldcount += oldsize;
701 	uep->ue_size = oldsize;
702 	uep->ue_array = newarray;
703 	uep->ue_bot = top + newsize + 1;
704 
705 	/*
706 	 * insert this entry in front of the new entry list
707 	 */
708 	nuep = uep->ue_next;
709 	uep->ue_next = newlist;
710 	newlist = uep;
711     }
712 
713     curbuf->b_u_curhead->uh_entry = newlist;
714     curbuf->b_u_curhead->uh_flags = new_flags;
715     if ((old_flags & UH_EMPTYBUF) && bufempty())
716 	curbuf->b_ml.ml_flags |= ML_EMPTY;
717     if (old_flags & UH_CHANGED)
718 	changed();
719     else
720 #ifdef FEAT_NETBEANS_INTG
721 	/* per netbeans undo rules, keep it as modified */
722 	if (!isNetbeansModified(curbuf))
723 #endif
724 	unchanged(curbuf, FALSE);
725 
726     /*
727      * restore marks from before undo/redo
728      */
729     for (i = 0; i < NMARKS; ++i)
730 	if (curbuf->b_u_curhead->uh_namedm[i].lnum)
731 	{
732 	    curbuf->b_namedm[i] = curbuf->b_u_curhead->uh_namedm[i];
733 	    curbuf->b_u_curhead->uh_namedm[i] = namedm[i];
734 	}
735 
736     /*
737      * If the cursor is only off by one line, put it at the same position as
738      * before starting the change (for the "o" command).
739      * Otherwise the cursor should go to the first undone line.
740      */
741     if (curbuf->b_u_curhead->uh_cursor.lnum + 1 == curwin->w_cursor.lnum
742 						 && curwin->w_cursor.lnum > 1)
743 	--curwin->w_cursor.lnum;
744     if (curbuf->b_u_curhead->uh_cursor.lnum == curwin->w_cursor.lnum)
745     {
746 	curwin->w_cursor.col = curbuf->b_u_curhead->uh_cursor.col;
747 #ifdef FEAT_VIRTUALEDIT
748 	if (virtual_active() && curbuf->b_u_curhead->uh_cursor_vcol >= 0)
749 	    coladvance((colnr_T)curbuf->b_u_curhead->uh_cursor_vcol);
750 	else
751 	    curwin->w_cursor.coladd = 0;
752 #endif
753     }
754     else if (curwin->w_cursor.lnum <= curbuf->b_ml.ml_line_count)
755 	beginline(BL_SOL | BL_FIX);
756     else
757     {
758 	/* We get here with the current cursor line being past the end (eg
759 	 * after adding lines at the end of the file, and then undoing it).
760 	 * check_cursor() will move the cursor to the last line.  Move it to
761 	 * the first column here. */
762 	curwin->w_cursor.col = 0;
763 #ifdef FEAT_VIRTUALEDIT
764 	curwin->w_cursor.coladd = 0;
765 #endif
766     }
767 
768     /* Make sure the cursor is on an existing line and column. */
769     check_cursor();
770 }
771 
772 /*
773  * If we deleted or added lines, report the number of less/more lines.
774  * Otherwise, report the number of changes (this may be incorrect
775  * in some cases, but it's better than nothing).
776  */
777     static void
778 u_undo_end()
779 {
780     if ((u_oldcount -= u_newcount) != 0)
781 	msgmore(-u_oldcount);
782     else if (u_newcount > p_report)
783     {
784 	if (u_newcount == 1)
785 	    MSG(_("1 change"));
786 	else
787 	    smsg((char_u *)_("%ld changes"), u_newcount);
788     }
789 #ifdef FEAT_FOLDING
790     if ((fdo_flags & FDO_UNDO) && KeyTyped)
791 	foldOpenCursor();
792 #endif
793 }
794 
795 /*
796  * u_sync: stop adding to the current entry list
797  */
798     void
799 u_sync()
800 {
801     if (curbuf->b_u_synced)
802 	return;		    /* already synced */
803 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
804     if (im_is_preediting())
805 	return;		    /* XIM is busy, don't break an undo sequence */
806 #endif
807     if (p_ul < 0)
808 	curbuf->b_u_synced = TRUE;  /* no entries, nothing to do */
809     else
810     {
811 	u_getbot();		    /* compute ue_bot of previous u_save */
812 	curbuf->b_u_curhead = NULL;
813     }
814 }
815 
816 /*
817  * Called after writing the file and setting b_changed to FALSE.
818  * Now an undo means that the buffer is modified.
819  */
820     void
821 u_unchanged(buf)
822     buf_T	*buf;
823 {
824     struct u_header	*uh;
825 
826     for (uh = buf->b_u_newhead; uh; uh = uh->uh_next)
827 	uh->uh_flags |= UH_CHANGED;
828     buf->b_did_warn = FALSE;
829 }
830 
831 /*
832  * Get pointer to last added entry.
833  * If it's not valid, give an error message and return NULL.
834  */
835     static u_entry_T *
836 u_get_headentry()
837 {
838     if (curbuf->b_u_newhead == NULL || curbuf->b_u_newhead->uh_entry == NULL)
839     {
840 	EMSG(_("E439: undo list corrupt"));
841 	return NULL;
842     }
843     return curbuf->b_u_newhead->uh_entry;
844 }
845 
846 /*
847  * u_getbot(): compute the line number of the previous u_save
848  *		It is called only when b_u_synced is FALSE.
849  */
850     static void
851 u_getbot()
852 {
853     u_entry_T	*uep;
854     linenr_T	extra;
855 
856     uep = u_get_headentry();	/* check for corrupt undo list */
857     if (uep == NULL)
858 	return;
859 
860     uep = curbuf->b_u_newhead->uh_getbot_entry;
861     if (uep != NULL)
862     {
863 	/*
864 	 * the new ue_bot is computed from the number of lines that has been
865 	 * inserted (0 - deleted) since calling u_save. This is equal to the
866 	 * old line count subtracted from the current line count.
867 	 */
868 	extra = curbuf->b_ml.ml_line_count - uep->ue_lcount;
869 	uep->ue_bot = uep->ue_top + uep->ue_size + 1 + extra;
870 	if (uep->ue_bot < 1 || uep->ue_bot > curbuf->b_ml.ml_line_count)
871 	{
872 	    EMSG(_("E440: undo line missing"));
873 	    uep->ue_bot = uep->ue_top + 1;  /* assume all lines deleted, will
874 					     * get all the old lines back
875 					     * without deleting the current
876 					     * ones */
877 	}
878 
879 	curbuf->b_u_newhead->uh_getbot_entry = NULL;
880     }
881 
882     curbuf->b_u_synced = TRUE;
883 }
884 
885 /*
886  * u_freelist: free one entry list and adjust the pointers
887  */
888     static void
889 u_freelist(buf, uhp)
890     buf_T	    *buf;
891     struct u_header *uhp;
892 {
893     u_entry_T	*uep, *nuep;
894 
895     for (uep = uhp->uh_entry; uep != NULL; uep = nuep)
896     {
897 	nuep = uep->ue_next;
898 	u_freeentry(uep, uep->ue_size);
899     }
900 
901     if (buf->b_u_curhead == uhp)
902 	buf->b_u_curhead = NULL;
903 
904     if (uhp->uh_next == NULL)
905 	buf->b_u_oldhead = uhp->uh_prev;
906     else
907 	uhp->uh_next->uh_prev = uhp->uh_prev;
908 
909     if (uhp->uh_prev == NULL)
910 	buf->b_u_newhead = uhp->uh_next;
911     else
912 	uhp->uh_prev->uh_next = uhp->uh_next;
913 
914     U_FREE_LINE((char_u *)uhp);
915     --buf->b_u_numhead;
916 }
917 
918 /*
919  * free entry 'uep' and 'n' lines in uep->ue_array[]
920  */
921     static void
922 u_freeentry(uep, n)
923     u_entry_T	*uep;
924     long	    n;
925 {
926     while (n)
927 	U_FREE_LINE(uep->ue_array[--n]);
928     U_FREE_LINE((char_u *)uep->ue_array);
929     U_FREE_LINE((char_u *)uep);
930 }
931 
932 /*
933  * invalidate the undo buffer; called when storage has already been released
934  */
935     void
936 u_clearall(buf)
937     buf_T	*buf;
938 {
939     buf->b_u_newhead = buf->b_u_oldhead = buf->b_u_curhead = NULL;
940     buf->b_u_synced = TRUE;
941     buf->b_u_numhead = 0;
942     buf->b_u_line_ptr = NULL;
943     buf->b_u_line_lnum = 0;
944 }
945 
946 /*
947  * save the line "lnum" for the "U" command
948  */
949     void
950 u_saveline(lnum)
951     linenr_T lnum;
952 {
953     if (lnum == curbuf->b_u_line_lnum)	    /* line is already saved */
954 	return;
955     if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) /* should never happen */
956 	return;
957     u_clearline();
958     curbuf->b_u_line_lnum = lnum;
959     if (curwin->w_cursor.lnum == lnum)
960 	curbuf->b_u_line_colnr = curwin->w_cursor.col;
961     else
962 	curbuf->b_u_line_colnr = 0;
963     if ((curbuf->b_u_line_ptr = u_save_line(lnum)) == NULL)
964 	do_outofmem_msg((long_u)0);
965 }
966 
967 /*
968  * clear the line saved for the "U" command
969  * (this is used externally for crossing a line while in insert mode)
970  */
971     void
972 u_clearline()
973 {
974     if (curbuf->b_u_line_ptr != NULL)
975     {
976 	U_FREE_LINE(curbuf->b_u_line_ptr);
977 	curbuf->b_u_line_ptr = NULL;
978 	curbuf->b_u_line_lnum = 0;
979     }
980 }
981 
982 /*
983  * Implementation of the "U" command.
984  * Differentiation from vi: "U" can be undone with the next "U".
985  * We also allow the cursor to be in another line.
986  */
987     void
988 u_undoline()
989 {
990     colnr_T t;
991     char_u  *oldp;
992 
993     if (undo_off)
994 	return;
995 
996     if (curbuf->b_u_line_ptr == NULL ||
997 			curbuf->b_u_line_lnum > curbuf->b_ml.ml_line_count)
998     {
999 	beep_flush();
1000 	return;
1001     }
1002 	/* first save the line for the 'u' command */
1003     if (u_savecommon(curbuf->b_u_line_lnum - 1,
1004 				curbuf->b_u_line_lnum + 1, (linenr_T)0) == FAIL)
1005 	return;
1006     oldp = u_save_line(curbuf->b_u_line_lnum);
1007     if (oldp == NULL)
1008     {
1009 	do_outofmem_msg((long_u)0);
1010 	return;
1011     }
1012     ml_replace(curbuf->b_u_line_lnum, curbuf->b_u_line_ptr, TRUE);
1013     changed_bytes(curbuf->b_u_line_lnum, 0);
1014     U_FREE_LINE(curbuf->b_u_line_ptr);
1015     curbuf->b_u_line_ptr = oldp;
1016 
1017     t = curbuf->b_u_line_colnr;
1018     if (curwin->w_cursor.lnum == curbuf->b_u_line_lnum)
1019 	curbuf->b_u_line_colnr = curwin->w_cursor.col;
1020     curwin->w_cursor.col = t;
1021     curwin->w_cursor.lnum = curbuf->b_u_line_lnum;
1022 }
1023 
1024 /*
1025  * There are two implementations of the memory management for undo:
1026  * 1. Use the standard malloc()/free() functions.
1027  *    This should be fast for allocating memory, but when a buffer is
1028  *    abandoned every single allocated chunk must be freed, which may be slow.
1029  * 2. Allocate larger blocks of memory and keep track of chunks ourselves.
1030  *    This is fast for abandoning, but the use of linked lists is slow for
1031  *    finding a free chunk.  Esp. when a lot of lines are changed or deleted.
1032  * A bit of profiling showed that the first method is faster, especially when
1033  * making a large number of changes, under the condition that malloc()/free()
1034  * is implemented efficiently.
1035  */
1036 #ifdef U_USE_MALLOC
1037 /*
1038  * Version of undo memory allocation using malloc()/free()
1039  *
1040  * U_FREE_LINE() and U_ALLOC_LINE() are macros that invoke vim_free() and
1041  * lalloc() directly.
1042  */
1043 
1044 /*
1045  * Free all allocated memory blocks for the buffer 'buf'.
1046  */
1047     void
1048 u_blockfree(buf)
1049     buf_T	*buf;
1050 {
1051     while (buf->b_u_newhead != NULL)
1052 	u_freelist(buf, buf->b_u_newhead);
1053     U_FREE_LINE(buf->b_u_line_ptr);
1054 }
1055 
1056 #else
1057 /*
1058  * Storage allocation for the undo lines and blocks of the current file.
1059  * Version where Vim keeps track of the available memory.
1060  */
1061 
1062 /*
1063  * Memory is allocated in relatively large blocks. These blocks are linked
1064  * in the allocated block list, headed by curbuf->b_block_head. They are all
1065  * freed when abandoning a file, so we don't have to free every single line.
1066  * The list is kept sorted on memory address.
1067  * block_alloc() allocates a block.
1068  * m_blockfree() frees all blocks.
1069  *
1070  * The available chunks of memory are kept in free chunk lists. There is
1071  * one free list for each block of allocated memory. The list is kept sorted
1072  * on memory address.
1073  * u_alloc_line() gets a chunk from the free lists.
1074  * u_free_line() returns a chunk to the free lists.
1075  * curbuf->b_m_search points to the chunk before the chunk that was
1076  * freed/allocated the last time.
1077  * curbuf->b_mb_current points to the b_head where curbuf->b_m_search
1078  * points into the free list.
1079  *
1080  *
1081  *  b_block_head     /---> block #1	/---> block #2
1082  *	 mb_next ---/	    mb_next ---/       mb_next ---> NULL
1083  *	 mb_info	    mb_info	       mb_info
1084  *	    |		       |		  |
1085  *	    V		       V		  V
1086  *	  NULL		free chunk #1.1      free chunk #2.1
1087  *			       |		  |
1088  *			       V		  V
1089  *			free chunk #1.2		 NULL
1090  *			       |
1091  *			       V
1092  *			      NULL
1093  *
1094  * When a single free chunk list would have been used, it could take a lot
1095  * of time in u_free_line() to find the correct place to insert a chunk in the
1096  * free list. The single free list would become very long when many lines are
1097  * changed (e.g. with :%s/^M$//).
1098  */
1099 
1100  /*
1101   * this blocksize is used when allocating new lines
1102   */
1103 #define MEMBLOCKSIZE 2044
1104 
1105 /*
1106  * The size field contains the size of the chunk, including the size field
1107  * itself.
1108  *
1109  * When the chunk is not in-use it is preceded with the m_info structure.
1110  * The m_next field links it in one of the free chunk lists.
1111  *
1112  * On most unix systems structures have to be longword (32 or 64 bit) aligned.
1113  * On most other systems they are short (16 bit) aligned.
1114  */
1115 
1116 /* the structure definitions are now in structs.h */
1117 
1118 #ifdef ALIGN_LONG
1119     /* size of m_size */
1120 # define M_OFFSET (sizeof(long_u))
1121 #else
1122     /* size of m_size */
1123 # define M_OFFSET (sizeof(short_u))
1124 #endif
1125 
1126 static char_u *u_blockalloc __ARGS((long_u));
1127 
1128 /*
1129  * Allocate a block of memory and link it in the allocated block list.
1130  */
1131     static char_u *
1132 u_blockalloc(size)
1133     long_u	size;
1134 {
1135     mblock_T	*p;
1136     mblock_T	*mp, *next;
1137 
1138     p = (mblock_T *)lalloc(size + sizeof(mblock_T), FALSE);
1139     if (p != NULL)
1140     {
1141 	 /* Insert the block into the allocated block list, keeping it
1142 		    sorted on address. */
1143 	for (mp = &curbuf->b_block_head;
1144 		(next = mp->mb_next) != NULL && next < p;
1145 			mp = next)
1146 	    ;
1147 	p->mb_next = next;		/* link in block list */
1148 	p->mb_size = size;
1149 	p->mb_maxsize = 0;		/* nothing free yet */
1150 	mp->mb_next = p;
1151 	p->mb_info.m_next = NULL;	/* clear free list */
1152 	p->mb_info.m_size = 0;
1153 	curbuf->b_mb_current = p;	/* remember current block */
1154 	curbuf->b_m_search = NULL;
1155 	p++;				/* return usable memory */
1156     }
1157     return (char_u *)p;
1158 }
1159 
1160 /*
1161  * free all allocated memory blocks for the buffer 'buf'
1162  */
1163     void
1164 u_blockfree(buf)
1165     buf_T	*buf;
1166 {
1167     mblock_T	*p, *np;
1168 
1169     for (p = buf->b_block_head.mb_next; p != NULL; p = np)
1170     {
1171 	np = p->mb_next;
1172 	vim_free(p);
1173     }
1174     buf->b_block_head.mb_next = NULL;
1175     buf->b_m_search = NULL;
1176     buf->b_mb_current = NULL;
1177 }
1178 
1179 /*
1180  * Free a chunk of memory for the current buffer.
1181  * Insert the chunk into the correct free list, keeping it sorted on address.
1182  */
1183     static void
1184 u_free_line(ptr, keep)
1185     char_u	*ptr;
1186     int		keep;	/* don't free the block when it's empty */
1187 {
1188     minfo_T	*next;
1189     minfo_T	*prev, *curr;
1190     minfo_T	*mp;
1191     mblock_T	*nextb;
1192     mblock_T	*prevb;
1193     long_u	maxsize;
1194 
1195     if (ptr == NULL || ptr == IObuff)
1196 	return;	/* illegal address can happen in out-of-memory situations */
1197 
1198     mp = (minfo_T *)(ptr - M_OFFSET);
1199 
1200     /* find block where chunk could be a part off */
1201     /* if we change curbuf->b_mb_current, curbuf->b_m_search is set to NULL */
1202     if (curbuf->b_mb_current == NULL || mp < (minfo_T *)curbuf->b_mb_current)
1203     {
1204 	curbuf->b_mb_current = curbuf->b_block_head.mb_next;
1205 	curbuf->b_m_search = NULL;
1206     }
1207     if ((nextb = curbuf->b_mb_current->mb_next) != NULL
1208 						     && (minfo_T *)nextb < mp)
1209     {
1210 	curbuf->b_mb_current = nextb;
1211 	curbuf->b_m_search = NULL;
1212     }
1213     while ((nextb = curbuf->b_mb_current->mb_next) != NULL
1214 						     && (minfo_T *)nextb < mp)
1215 	curbuf->b_mb_current = nextb;
1216 
1217     curr = NULL;
1218     /*
1219      * If mp is smaller than curbuf->b_m_search->m_next go to the start of
1220      * the free list
1221      */
1222     if (curbuf->b_m_search == NULL || mp < (curbuf->b_m_search->m_next))
1223 	next = &(curbuf->b_mb_current->mb_info);
1224     else
1225 	next = curbuf->b_m_search;
1226     /*
1227      * The following loop is executed very often.
1228      * Therefore it has been optimized at the cost of readability.
1229      * Keep it fast!
1230      */
1231 #ifdef SLOW_BUT_EASY_TO_READ
1232     do
1233     {
1234 	prev = curr;
1235 	curr = next;
1236 	next = next->m_next;
1237     }
1238     while (mp > next && next != NULL);
1239 #else
1240     do					    /* first, middle, last */
1241     {
1242 	prev = next->m_next;		    /* curr, next, prev */
1243 	if (prev == NULL || mp <= prev)
1244 	{
1245 	    prev = curr;
1246 	    curr = next;
1247 	    next = next->m_next;
1248 	    break;
1249 	}
1250 	curr = prev->m_next;		    /* next, prev, curr */
1251 	if (curr == NULL || mp <= curr)
1252 	{
1253 	    prev = next;
1254 	    curr = prev->m_next;
1255 	    next = curr->m_next;
1256 	    break;
1257 	}
1258 	next = curr->m_next;		    /* prev, curr, next */
1259     }
1260     while (mp > next && next != NULL);
1261 #endif
1262 
1263     /* if *mp and *next are concatenated, join them into one chunk */
1264     if ((char_u *)mp + mp->m_size == (char_u *)next)
1265     {
1266 	mp->m_size += next->m_size;
1267 	mp->m_next = next->m_next;
1268     }
1269     else
1270 	mp->m_next = next;
1271     maxsize = mp->m_size;
1272 
1273     /* if *curr and *mp are concatenated, join them */
1274     if (prev != NULL && (char_u *)curr + curr->m_size == (char_u *)mp)
1275     {
1276 	curr->m_size += mp->m_size;
1277 	maxsize = curr->m_size;
1278 	curr->m_next = mp->m_next;
1279 	curbuf->b_m_search = prev;
1280     }
1281     else
1282     {
1283 	curr->m_next = mp;
1284 	curbuf->b_m_search = curr;  /* put curbuf->b_m_search before freed
1285 				       chunk */
1286     }
1287 
1288     /*
1289      * If the block only containes free memory now, release it.
1290      */
1291     if (!keep && curbuf->b_mb_current->mb_size
1292 			      == curbuf->b_mb_current->mb_info.m_next->m_size)
1293     {
1294 	/* Find the block before the current one to be able to unlink it from
1295 	 * the list of blocks. */
1296 	prevb = &curbuf->b_block_head;
1297 	for (nextb = prevb->mb_next; nextb != curbuf->b_mb_current;
1298 						       nextb = nextb->mb_next)
1299 	    prevb = nextb;
1300 	prevb->mb_next = nextb->mb_next;
1301 	vim_free(nextb);
1302 	curbuf->b_mb_current = NULL;
1303 	curbuf->b_m_search = NULL;
1304     }
1305     else if (curbuf->b_mb_current->mb_maxsize < maxsize)
1306 	curbuf->b_mb_current->mb_maxsize = maxsize;
1307 }
1308 
1309 /*
1310  * Allocate and initialize a new line structure with room for at least
1311  * 'size' characters plus a terminating NUL.
1312  */
1313     static char_u *
1314 u_alloc_line(size)
1315     unsigned	size;
1316 {
1317     minfo_T	*mp, *mprev, *mp2;
1318     mblock_T	*mbp;
1319     int		size_align;
1320 
1321     /*
1322      * Add room for size field and trailing NUL byte.
1323      * Adjust for minimal size (must be able to store minfo_T
1324      * plus a trailing NUL, so the chunk can be released again)
1325      */
1326     size += M_OFFSET + 1;
1327     if (size < sizeof(minfo_T) + 1)
1328 	size = sizeof(minfo_T) + 1;
1329 
1330     /*
1331      * round size up for alignment
1332      */
1333     size_align = (size + ALIGN_MASK) & ~ALIGN_MASK;
1334 
1335     /*
1336      * If curbuf->b_m_search is NULL (uninitialized free list) start at
1337      * curbuf->b_block_head
1338      */
1339     if (curbuf->b_mb_current == NULL || curbuf->b_m_search == NULL)
1340     {
1341 	curbuf->b_mb_current = &curbuf->b_block_head;
1342 	curbuf->b_m_search = &(curbuf->b_block_head.mb_info);
1343     }
1344 
1345     /* Search for a block with enough space. */
1346     mbp = curbuf->b_mb_current;
1347     while (mbp->mb_maxsize < size_align)
1348     {
1349 	if (mbp->mb_next != NULL)
1350 	    mbp = mbp->mb_next;
1351 	else
1352 	    mbp = &curbuf->b_block_head;
1353 	if (mbp == curbuf->b_mb_current)
1354 	{
1355 	    int	n = (size_align > (MEMBLOCKSIZE / 4)
1356 					     ? size_align : MEMBLOCKSIZE);
1357 
1358 	    /* Back where we started in block list: need to add a new block
1359 	     * with enough space. */
1360 	    mp = (minfo_T *)u_blockalloc((long_u)n);
1361 	    if (mp == NULL)
1362 		return (NULL);
1363 	    mp->m_size = n;
1364 	    u_free_line((char_u *)mp + M_OFFSET, TRUE);
1365 	    mbp = curbuf->b_mb_current;
1366 	    break;
1367 	}
1368     }
1369     if (mbp != curbuf->b_mb_current)
1370 	curbuf->b_m_search = &(mbp->mb_info);
1371 
1372     /* In this block find a chunk with enough space. */
1373     mprev = curbuf->b_m_search;
1374     mp = curbuf->b_m_search->m_next;
1375     for (;;)
1376     {
1377 	if (mp == NULL)			    /* at end of the list */
1378 	    mp = &(mbp->mb_info);	    /* wrap around to begin */
1379 	if (mp->m_size >= size)
1380 	    break;
1381 	if (mp == curbuf->b_m_search)
1382 	{
1383 	    /* back where we started in free chunk list: "cannot happen" */
1384 	    EMSG2(_(e_intern2), "u_alloc_line()");
1385 	    return NULL;
1386 	}
1387 	mprev = mp;
1388 	mp = mp->m_next;
1389     }
1390 
1391     /* when using the largest chunk adjust mb_maxsize */
1392     if (mp->m_size >= mbp->mb_maxsize)
1393 	mbp->mb_maxsize = 0;
1394 
1395     /* if the chunk we found is large enough, split it up in two */
1396     if ((long)mp->m_size - size_align >= (long)(sizeof(minfo_T) + 1))
1397     {
1398 	mp2 = (minfo_T *)((char_u *)mp + size_align);
1399 	mp2->m_size = mp->m_size - size_align;
1400 	mp2->m_next = mp->m_next;
1401 	mprev->m_next = mp2;
1402 	mp->m_size = size_align;
1403     }
1404     else		    /* remove *mp from the free list */
1405     {
1406 	mprev->m_next = mp->m_next;
1407     }
1408     curbuf->b_m_search = mprev;
1409     curbuf->b_mb_current = mbp;
1410 
1411     /* If using the largest chunk need to find the new largest chunk */
1412     if (mbp->mb_maxsize == 0)
1413 	for (mp2 = &(mbp->mb_info); mp2 != NULL; mp2 = mp2->m_next)
1414 	    if (mbp->mb_maxsize < mp2->m_size)
1415 		mbp->mb_maxsize = mp2->m_size;
1416 
1417     mp = (minfo_T *)((char_u *)mp + M_OFFSET);
1418     *(char_u *)mp = NUL;		    /* set the first byte to NUL */
1419 
1420     return ((char_u *)mp);
1421 }
1422 #endif
1423 
1424 /*
1425  * u_save_line(): allocate memory with u_alloc_line() and copy line 'lnum'
1426  * into it.
1427  */
1428     static char_u *
1429 u_save_line(lnum)
1430     linenr_T	lnum;
1431 {
1432     char_u	*src;
1433     char_u	*dst;
1434     unsigned	len;
1435 
1436     src = ml_get(lnum);
1437     len = (unsigned)STRLEN(src);
1438     if ((dst = U_ALLOC_LINE(len)) != NULL)
1439 	mch_memmove(dst, src, (size_t)(len + 1));
1440     return (dst);
1441 }
1442 
1443 /*
1444  * Check if the 'modified' flag is set, or 'ff' has changed (only need to
1445  * check the first character, because it can only be "dos", "unix" or "mac").
1446  * "nofile" and "scratch" type buffers are considered to always be unchanged.
1447  */
1448     int
1449 bufIsChanged(buf)
1450     buf_T	*buf;
1451 {
1452     return
1453 #ifdef FEAT_QUICKFIX
1454 	    !bt_dontwrite(buf) &&
1455 #endif
1456 	    (buf->b_changed || file_ff_differs(buf));
1457 }
1458 
1459     int
1460 curbufIsChanged()
1461 {
1462     return
1463 #ifdef FEAT_QUICKFIX
1464 	!bt_dontwrite(curbuf) &&
1465 #endif
1466 	(curbuf->b_changed || file_ff_differs(curbuf));
1467 }
1468