blob: 1ead1561f7c5fa08d61402baa1e0ecb2d33664bd [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * undo.c: multi level undo facility
12 *
13 * The saved lines are stored in a list of lists (one for each buffer):
14 *
15 * b_u_oldhead------------------------------------------------+
16 * |
17 * V
18 * +--------------+ +--------------+ +--------------+
19 * b_u_newhead--->| u_header | | u_header | | u_header |
20 * | uh_next------>| uh_next------>| uh_next---->NULL
21 * NULL<--------uh_prev |<---------uh_prev |<---------uh_prev |
22 * | uh_entry | | uh_entry | | uh_entry |
23 * +--------|-----+ +--------|-----+ +--------|-----+
24 * | | |
25 * V V V
26 * +--------------+ +--------------+ +--------------+
27 * | u_entry | | u_entry | | u_entry |
28 * | ue_next | | ue_next | | ue_next |
29 * +--------|-----+ +--------|-----+ +--------|-----+
30 * | | |
31 * V V V
32 * +--------------+ NULL NULL
33 * | u_entry |
34 * | ue_next |
35 * +--------|-----+
36 * |
37 * V
38 * etc.
39 *
40 * Each u_entry list contains the information for one undo or redo.
41 * curbuf->b_u_curhead points to the header of the last undo (the next redo),
42 * or is NULL if nothing has been undone.
43 *
Bram Moolenaar26a60b42005-02-22 08:49:11 +000044 * All data is allocated with U_ALLOC_LINE(), it will be freed as soon as the
45 * buffer is unloaded.
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 */
47
48#include "vim.h"
49
Bram Moolenaar26a60b42005-02-22 08:49:11 +000050/* See below: use malloc()/free() for memory management. */
51#define U_USE_MALLOC 1
52
Bram Moolenaar071d4272004-06-13 20:20:40 +000053static u_entry_T *u_get_headentry __ARGS((void));
54static void u_getbot __ARGS((void));
Bram Moolenaar8ada17c2006-01-19 22:16:24 +000055static int undo_allowed __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000056static int u_savecommon __ARGS((linenr_T, linenr_T, linenr_T));
57static void u_doit __ARGS((int count));
58static void u_undoredo __ARGS((void));
59static void u_undo_end __ARGS((void));
Bram Moolenaar26a60b42005-02-22 08:49:11 +000060static void u_freelist __ARGS((buf_T *buf, struct u_header *));
Bram Moolenaar071d4272004-06-13 20:20:40 +000061static void u_freeentry __ARGS((u_entry_T *, long));
62
Bram Moolenaar26a60b42005-02-22 08:49:11 +000063#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
67static void u_free_line __ARGS((char_u *ptr, int keep));
68static 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
Bram Moolenaar071d4272004-06-13 20:20:40 +000072static char_u *u_save_line __ARGS((linenr_T));
73
74static 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 */
80static int undo_undoes = FALSE;
81
82/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000083 * Save the current line for both the "u" and "U" command.
84 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000085 */
86 int
87u_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
99u_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
120u_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
134u_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
149u_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
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000160/*
161 * Return TRUE when undo is allowed. Otherwise give an error message and
162 * return FALSE.
163 */
164 static int
165undo_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. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000185 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000186 {
187 EMSG(_(e_secure));
188 return FALSE;
189 }
190
191 return TRUE;
192}
193
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 static int
195u_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
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000206 /* When making changes is not allowed return FAIL. It's a crude way to
207 * make all change commands fail. */
208 if (!undo_allowed())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210
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 */
Bram Moolenaar009b2592004-10-24 19:18:58 +0000216 if (usingNetbeans)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217 {
Bram Moolenaar009b2592004-10-24 19:18:58 +0000218 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 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 }
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)
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000258 u_freelist(curbuf, curbuf->b_u_newhead);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259
260 /*
261 * free headers to keep the size right
262 */
263 while (curbuf->b_u_numhead > p_ul && curbuf->b_u_oldhead != NULL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000264 u_freelist(curbuf, curbuf->b_u_oldhead);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265
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 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000275 uhp = (struct u_header *)U_ALLOC_LINE((unsigned)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276 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
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000297 /* save named marks and Visual marks for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 mch_memmove(uhp->uh_namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000299#ifdef FEAT_VISUAL
300 uhp->uh_visual = curbuf->b_visual;
301#endif
302
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303 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 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000399 uep = (u_entry_T *)U_ALLOC_LINE((unsigned)sizeof(u_entry_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 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
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000419 if (size > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000421 if ((uep->ue_array = (char_u **)U_ALLOC_LINE(
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 (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 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000429 fast_breakcheck();
430 if (got_int)
431 {
432 u_freeentry(uep, i);
433 return FAIL;
434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 if ((uep->ue_array[i] = u_save_line(lnum++)) == NULL)
436 {
437 u_freeentry(uep, i);
438 goto nomem;
439 }
440 }
441 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000442 else
443 uep->ue_array = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 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
451nomem:
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
468u_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
494u_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
506u_doit(count)
507 int count;
508{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000509 if (!undo_allowed())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511
512 u_newcount = 0;
513 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000514 if (curbuf->b_ml.ml_flags & ML_EMPTY)
515 u_oldcount = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 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 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000549 if (curbuf->b_ml.ml_flags & ML_EMPTY)
550 --u_newcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 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
562u_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];
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000576#ifdef FEAT_VISUAL
577 visualinfo_T visualinfo;
578#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 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);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000590#ifdef FEAT_VISUAL
591 visualinfo = curbuf->b_visual;
592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 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 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000650 if (oldsize > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000652 if ((newarray = (char_u **)U_ALLOC_LINE(
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 (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 }
Bram Moolenaar8d343302005-07-12 22:46:17 +0000681 else
682 newarray = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683
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);
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000697 U_FREE_LINE(uep->ue_array[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000699 U_FREE_LINE((char_u *)uep->ue_array);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 }
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
Bram Moolenaar009b2592004-10-24 19:18:58 +0000744#ifdef FEAT_NETBEANS_INTG
745 /* per netbeans undo rules, keep it as modified */
746 if (!isNetbeansModified(curbuf))
747#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 unchanged(curbuf, FALSE);
749
750 /*
751 * restore marks from before undo/redo
752 */
753 for (i = 0; i < NMARKS; ++i)
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000754 if (curbuf->b_u_curhead->uh_namedm[i].lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 {
756 curbuf->b_namedm[i] = curbuf->b_u_curhead->uh_namedm[i];
757 curbuf->b_u_curhead->uh_namedm[i] = namedm[i];
758 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000759#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
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766
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
809u_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
830u_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 * Called after writing the file and setting b_changed to FALSE.
849 * Now an undo means that the buffer is modified.
850 */
851 void
852u_unchanged(buf)
853 buf_T *buf;
854{
855 struct u_header *uh;
856
857 for (uh = buf->b_u_newhead; uh; uh = uh->uh_next)
858 uh->uh_flags |= UH_CHANGED;
859 buf->b_did_warn = FALSE;
860}
861
862/*
863 * Get pointer to last added entry.
864 * If it's not valid, give an error message and return NULL.
865 */
866 static u_entry_T *
867u_get_headentry()
868{
869 if (curbuf->b_u_newhead == NULL || curbuf->b_u_newhead->uh_entry == NULL)
870 {
871 EMSG(_("E439: undo list corrupt"));
872 return NULL;
873 }
874 return curbuf->b_u_newhead->uh_entry;
875}
876
877/*
878 * u_getbot(): compute the line number of the previous u_save
879 * It is called only when b_u_synced is FALSE.
880 */
881 static void
882u_getbot()
883{
884 u_entry_T *uep;
885 linenr_T extra;
886
887 uep = u_get_headentry(); /* check for corrupt undo list */
888 if (uep == NULL)
889 return;
890
891 uep = curbuf->b_u_newhead->uh_getbot_entry;
892 if (uep != NULL)
893 {
894 /*
895 * the new ue_bot is computed from the number of lines that has been
896 * inserted (0 - deleted) since calling u_save. This is equal to the
897 * old line count subtracted from the current line count.
898 */
899 extra = curbuf->b_ml.ml_line_count - uep->ue_lcount;
900 uep->ue_bot = uep->ue_top + uep->ue_size + 1 + extra;
901 if (uep->ue_bot < 1 || uep->ue_bot > curbuf->b_ml.ml_line_count)
902 {
903 EMSG(_("E440: undo line missing"));
904 uep->ue_bot = uep->ue_top + 1; /* assume all lines deleted, will
905 * get all the old lines back
906 * without deleting the current
907 * ones */
908 }
909
910 curbuf->b_u_newhead->uh_getbot_entry = NULL;
911 }
912
913 curbuf->b_u_synced = TRUE;
914}
915
916/*
917 * u_freelist: free one entry list and adjust the pointers
918 */
919 static void
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000920u_freelist(buf, uhp)
921 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 struct u_header *uhp;
923{
924 u_entry_T *uep, *nuep;
925
926 for (uep = uhp->uh_entry; uep != NULL; uep = nuep)
927 {
928 nuep = uep->ue_next;
929 u_freeentry(uep, uep->ue_size);
930 }
931
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000932 if (buf->b_u_curhead == uhp)
933 buf->b_u_curhead = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934
935 if (uhp->uh_next == NULL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000936 buf->b_u_oldhead = uhp->uh_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 else
938 uhp->uh_next->uh_prev = uhp->uh_prev;
939
940 if (uhp->uh_prev == NULL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000941 buf->b_u_newhead = uhp->uh_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 else
943 uhp->uh_prev->uh_next = uhp->uh_next;
944
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000945 U_FREE_LINE((char_u *)uhp);
946 --buf->b_u_numhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947}
948
949/*
950 * free entry 'uep' and 'n' lines in uep->ue_array[]
951 */
952 static void
953u_freeentry(uep, n)
954 u_entry_T *uep;
955 long n;
956{
Bram Moolenaar8d343302005-07-12 22:46:17 +0000957 while (n > 0)
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000958 U_FREE_LINE(uep->ue_array[--n]);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000959 U_FREE_LINE((char_u *)uep->ue_array);
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000960 U_FREE_LINE((char_u *)uep);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961}
962
963/*
964 * invalidate the undo buffer; called when storage has already been released
965 */
966 void
967u_clearall(buf)
968 buf_T *buf;
969{
970 buf->b_u_newhead = buf->b_u_oldhead = buf->b_u_curhead = NULL;
971 buf->b_u_synced = TRUE;
972 buf->b_u_numhead = 0;
973 buf->b_u_line_ptr = NULL;
974 buf->b_u_line_lnum = 0;
975}
976
977/*
978 * save the line "lnum" for the "U" command
979 */
980 void
981u_saveline(lnum)
982 linenr_T lnum;
983{
984 if (lnum == curbuf->b_u_line_lnum) /* line is already saved */
985 return;
Bram Moolenaar1ec484f2005-06-24 23:07:47 +0000986 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) /* should never happen */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 return;
988 u_clearline();
989 curbuf->b_u_line_lnum = lnum;
990 if (curwin->w_cursor.lnum == lnum)
991 curbuf->b_u_line_colnr = curwin->w_cursor.col;
992 else
993 curbuf->b_u_line_colnr = 0;
994 if ((curbuf->b_u_line_ptr = u_save_line(lnum)) == NULL)
995 do_outofmem_msg((long_u)0);
996}
997
998/*
999 * clear the line saved for the "U" command
1000 * (this is used externally for crossing a line while in insert mode)
1001 */
1002 void
1003u_clearline()
1004{
1005 if (curbuf->b_u_line_ptr != NULL)
1006 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001007 U_FREE_LINE(curbuf->b_u_line_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 curbuf->b_u_line_ptr = NULL;
1009 curbuf->b_u_line_lnum = 0;
1010 }
1011}
1012
1013/*
1014 * Implementation of the "U" command.
1015 * Differentiation from vi: "U" can be undone with the next "U".
1016 * We also allow the cursor to be in another line.
1017 */
1018 void
1019u_undoline()
1020{
1021 colnr_T t;
1022 char_u *oldp;
1023
1024 if (undo_off)
1025 return;
1026
1027 if (curbuf->b_u_line_ptr == NULL ||
1028 curbuf->b_u_line_lnum > curbuf->b_ml.ml_line_count)
1029 {
1030 beep_flush();
1031 return;
1032 }
1033 /* first save the line for the 'u' command */
1034 if (u_savecommon(curbuf->b_u_line_lnum - 1,
1035 curbuf->b_u_line_lnum + 1, (linenr_T)0) == FAIL)
1036 return;
1037 oldp = u_save_line(curbuf->b_u_line_lnum);
1038 if (oldp == NULL)
1039 {
1040 do_outofmem_msg((long_u)0);
1041 return;
1042 }
1043 ml_replace(curbuf->b_u_line_lnum, curbuf->b_u_line_ptr, TRUE);
1044 changed_bytes(curbuf->b_u_line_lnum, 0);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001045 U_FREE_LINE(curbuf->b_u_line_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 curbuf->b_u_line_ptr = oldp;
1047
1048 t = curbuf->b_u_line_colnr;
1049 if (curwin->w_cursor.lnum == curbuf->b_u_line_lnum)
1050 curbuf->b_u_line_colnr = curwin->w_cursor.col;
1051 curwin->w_cursor.col = t;
1052 curwin->w_cursor.lnum = curbuf->b_u_line_lnum;
1053}
1054
1055/*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001056 * There are two implementations of the memory management for undo:
1057 * 1. Use the standard malloc()/free() functions.
1058 * This should be fast for allocating memory, but when a buffer is
1059 * abandoned every single allocated chunk must be freed, which may be slow.
1060 * 2. Allocate larger blocks of memory and keep track of chunks ourselves.
1061 * This is fast for abandoning, but the use of linked lists is slow for
1062 * finding a free chunk. Esp. when a lot of lines are changed or deleted.
1063 * A bit of profiling showed that the first method is faster, especially when
1064 * making a large number of changes, under the condition that malloc()/free()
1065 * is implemented efficiently.
1066 */
1067#ifdef U_USE_MALLOC
1068/*
1069 * Version of undo memory allocation using malloc()/free()
1070 *
1071 * U_FREE_LINE() and U_ALLOC_LINE() are macros that invoke vim_free() and
1072 * lalloc() directly.
1073 */
1074
1075/*
1076 * Free all allocated memory blocks for the buffer 'buf'.
1077 */
1078 void
1079u_blockfree(buf)
1080 buf_T *buf;
1081{
1082 while (buf->b_u_newhead != NULL)
1083 u_freelist(buf, buf->b_u_newhead);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001084 U_FREE_LINE(buf->b_u_line_ptr);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001085}
1086
1087#else
1088/*
1089 * Storage allocation for the undo lines and blocks of the current file.
1090 * Version where Vim keeps track of the available memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 */
1092
1093/*
1094 * Memory is allocated in relatively large blocks. These blocks are linked
1095 * in the allocated block list, headed by curbuf->b_block_head. They are all
1096 * freed when abandoning a file, so we don't have to free every single line.
1097 * The list is kept sorted on memory address.
1098 * block_alloc() allocates a block.
1099 * m_blockfree() frees all blocks.
1100 *
1101 * The available chunks of memory are kept in free chunk lists. There is
1102 * one free list for each block of allocated memory. The list is kept sorted
1103 * on memory address.
1104 * u_alloc_line() gets a chunk from the free lists.
1105 * u_free_line() returns a chunk to the free lists.
1106 * curbuf->b_m_search points to the chunk before the chunk that was
1107 * freed/allocated the last time.
1108 * curbuf->b_mb_current points to the b_head where curbuf->b_m_search
1109 * points into the free list.
1110 *
1111 *
1112 * b_block_head /---> block #1 /---> block #2
1113 * mb_next ---/ mb_next ---/ mb_next ---> NULL
1114 * mb_info mb_info mb_info
1115 * | | |
1116 * V V V
1117 * NULL free chunk #1.1 free chunk #2.1
1118 * | |
1119 * V V
1120 * free chunk #1.2 NULL
1121 * |
1122 * V
1123 * NULL
1124 *
1125 * When a single free chunk list would have been used, it could take a lot
1126 * of time in u_free_line() to find the correct place to insert a chunk in the
1127 * free list. The single free list would become very long when many lines are
1128 * changed (e.g. with :%s/^M$//).
1129 */
1130
1131 /*
1132 * this blocksize is used when allocating new lines
1133 */
1134#define MEMBLOCKSIZE 2044
1135
1136/*
1137 * The size field contains the size of the chunk, including the size field
1138 * itself.
1139 *
1140 * When the chunk is not in-use it is preceded with the m_info structure.
1141 * The m_next field links it in one of the free chunk lists.
1142 *
1143 * On most unix systems structures have to be longword (32 or 64 bit) aligned.
1144 * On most other systems they are short (16 bit) aligned.
1145 */
1146
1147/* the structure definitions are now in structs.h */
1148
1149#ifdef ALIGN_LONG
1150 /* size of m_size */
1151# define M_OFFSET (sizeof(long_u))
1152#else
1153 /* size of m_size */
1154# define M_OFFSET (sizeof(short_u))
1155#endif
1156
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001157static char_u *u_blockalloc __ARGS((long_u));
1158
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159/*
1160 * Allocate a block of memory and link it in the allocated block list.
1161 */
1162 static char_u *
1163u_blockalloc(size)
1164 long_u size;
1165{
1166 mblock_T *p;
1167 mblock_T *mp, *next;
1168
1169 p = (mblock_T *)lalloc(size + sizeof(mblock_T), FALSE);
1170 if (p != NULL)
1171 {
1172 /* Insert the block into the allocated block list, keeping it
1173 sorted on address. */
1174 for (mp = &curbuf->b_block_head;
1175 (next = mp->mb_next) != NULL && next < p;
1176 mp = next)
1177 ;
1178 p->mb_next = next; /* link in block list */
1179 p->mb_size = size;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001180 p->mb_maxsize = 0; /* nothing free yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 mp->mb_next = p;
1182 p->mb_info.m_next = NULL; /* clear free list */
1183 p->mb_info.m_size = 0;
1184 curbuf->b_mb_current = p; /* remember current block */
1185 curbuf->b_m_search = NULL;
1186 p++; /* return usable memory */
1187 }
1188 return (char_u *)p;
1189}
1190
1191/*
1192 * free all allocated memory blocks for the buffer 'buf'
1193 */
1194 void
1195u_blockfree(buf)
1196 buf_T *buf;
1197{
1198 mblock_T *p, *np;
1199
1200 for (p = buf->b_block_head.mb_next; p != NULL; p = np)
1201 {
1202 np = p->mb_next;
1203 vim_free(p);
1204 }
1205 buf->b_block_head.mb_next = NULL;
1206 buf->b_m_search = NULL;
1207 buf->b_mb_current = NULL;
1208}
1209
1210/*
1211 * Free a chunk of memory for the current buffer.
1212 * Insert the chunk into the correct free list, keeping it sorted on address.
1213 */
1214 static void
1215u_free_line(ptr, keep)
1216 char_u *ptr;
1217 int keep; /* don't free the block when it's empty */
1218{
1219 minfo_T *next;
1220 minfo_T *prev, *curr;
1221 minfo_T *mp;
1222 mblock_T *nextb;
1223 mblock_T *prevb;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001224 long_u maxsize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225
1226 if (ptr == NULL || ptr == IObuff)
1227 return; /* illegal address can happen in out-of-memory situations */
1228
1229 mp = (minfo_T *)(ptr - M_OFFSET);
1230
1231 /* find block where chunk could be a part off */
1232 /* if we change curbuf->b_mb_current, curbuf->b_m_search is set to NULL */
1233 if (curbuf->b_mb_current == NULL || mp < (minfo_T *)curbuf->b_mb_current)
1234 {
1235 curbuf->b_mb_current = curbuf->b_block_head.mb_next;
1236 curbuf->b_m_search = NULL;
1237 }
1238 if ((nextb = curbuf->b_mb_current->mb_next) != NULL
1239 && (minfo_T *)nextb < mp)
1240 {
1241 curbuf->b_mb_current = nextb;
1242 curbuf->b_m_search = NULL;
1243 }
1244 while ((nextb = curbuf->b_mb_current->mb_next) != NULL
1245 && (minfo_T *)nextb < mp)
1246 curbuf->b_mb_current = nextb;
1247
1248 curr = NULL;
1249 /*
1250 * If mp is smaller than curbuf->b_m_search->m_next go to the start of
1251 * the free list
1252 */
1253 if (curbuf->b_m_search == NULL || mp < (curbuf->b_m_search->m_next))
1254 next = &(curbuf->b_mb_current->mb_info);
1255 else
1256 next = curbuf->b_m_search;
1257 /*
1258 * The following loop is executed very often.
1259 * Therefore it has been optimized at the cost of readability.
1260 * Keep it fast!
1261 */
1262#ifdef SLOW_BUT_EASY_TO_READ
1263 do
1264 {
1265 prev = curr;
1266 curr = next;
1267 next = next->m_next;
1268 }
1269 while (mp > next && next != NULL);
1270#else
1271 do /* first, middle, last */
1272 {
1273 prev = next->m_next; /* curr, next, prev */
1274 if (prev == NULL || mp <= prev)
1275 {
1276 prev = curr;
1277 curr = next;
1278 next = next->m_next;
1279 break;
1280 }
1281 curr = prev->m_next; /* next, prev, curr */
1282 if (curr == NULL || mp <= curr)
1283 {
1284 prev = next;
1285 curr = prev->m_next;
1286 next = curr->m_next;
1287 break;
1288 }
1289 next = curr->m_next; /* prev, curr, next */
1290 }
1291 while (mp > next && next != NULL);
1292#endif
1293
1294 /* if *mp and *next are concatenated, join them into one chunk */
1295 if ((char_u *)mp + mp->m_size == (char_u *)next)
1296 {
1297 mp->m_size += next->m_size;
1298 mp->m_next = next->m_next;
1299 }
1300 else
1301 mp->m_next = next;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001302 maxsize = mp->m_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303
1304 /* if *curr and *mp are concatenated, join them */
1305 if (prev != NULL && (char_u *)curr + curr->m_size == (char_u *)mp)
1306 {
1307 curr->m_size += mp->m_size;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001308 maxsize = curr->m_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 curr->m_next = mp->m_next;
1310 curbuf->b_m_search = prev;
1311 }
1312 else
1313 {
1314 curr->m_next = mp;
1315 curbuf->b_m_search = curr; /* put curbuf->b_m_search before freed
1316 chunk */
1317 }
1318
1319 /*
1320 * If the block only containes free memory now, release it.
1321 */
1322 if (!keep && curbuf->b_mb_current->mb_size
1323 == curbuf->b_mb_current->mb_info.m_next->m_size)
1324 {
1325 /* Find the block before the current one to be able to unlink it from
1326 * the list of blocks. */
1327 prevb = &curbuf->b_block_head;
1328 for (nextb = prevb->mb_next; nextb != curbuf->b_mb_current;
1329 nextb = nextb->mb_next)
1330 prevb = nextb;
1331 prevb->mb_next = nextb->mb_next;
1332 vim_free(nextb);
1333 curbuf->b_mb_current = NULL;
1334 curbuf->b_m_search = NULL;
1335 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001336 else if (curbuf->b_mb_current->mb_maxsize < maxsize)
1337 curbuf->b_mb_current->mb_maxsize = maxsize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338}
1339
1340/*
1341 * Allocate and initialize a new line structure with room for at least
1342 * 'size' characters plus a terminating NUL.
1343 */
1344 static char_u *
1345u_alloc_line(size)
1346 unsigned size;
1347{
1348 minfo_T *mp, *mprev, *mp2;
1349 mblock_T *mbp;
1350 int size_align;
1351
1352 /*
1353 * Add room for size field and trailing NUL byte.
1354 * Adjust for minimal size (must be able to store minfo_T
1355 * plus a trailing NUL, so the chunk can be released again)
1356 */
1357 size += M_OFFSET + 1;
1358 if (size < sizeof(minfo_T) + 1)
1359 size = sizeof(minfo_T) + 1;
1360
1361 /*
1362 * round size up for alignment
1363 */
1364 size_align = (size + ALIGN_MASK) & ~ALIGN_MASK;
1365
1366 /*
1367 * If curbuf->b_m_search is NULL (uninitialized free list) start at
1368 * curbuf->b_block_head
1369 */
1370 if (curbuf->b_mb_current == NULL || curbuf->b_m_search == NULL)
1371 {
1372 curbuf->b_mb_current = &curbuf->b_block_head;
1373 curbuf->b_m_search = &(curbuf->b_block_head.mb_info);
1374 }
1375
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001376 /* Search for a block with enough space. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 mbp = curbuf->b_mb_current;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001378 while (mbp->mb_maxsize < size_align)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001380 if (mbp->mb_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 mbp = mbp->mb_next;
1382 else
1383 mbp = &curbuf->b_block_head;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001384 if (mbp == curbuf->b_mb_current)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001386 int n = (size_align > (MEMBLOCKSIZE / 4)
1387 ? size_align : MEMBLOCKSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001389 /* Back where we started in block list: need to add a new block
1390 * with enough space. */
1391 mp = (minfo_T *)u_blockalloc((long_u)n);
1392 if (mp == NULL)
1393 return (NULL);
1394 mp->m_size = n;
1395 u_free_line((char_u *)mp + M_OFFSET, TRUE);
1396 mbp = curbuf->b_mb_current;
1397 break;
1398 }
1399 }
1400 if (mbp != curbuf->b_mb_current)
1401 curbuf->b_m_search = &(mbp->mb_info);
1402
1403 /* In this block find a chunk with enough space. */
1404 mprev = curbuf->b_m_search;
1405 mp = curbuf->b_m_search->m_next;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00001406 for (;;)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001407 {
1408 if (mp == NULL) /* at end of the list */
1409 mp = &(mbp->mb_info); /* wrap around to begin */
1410 if (mp->m_size >= size)
1411 break;
1412 if (mp == curbuf->b_m_search)
1413 {
1414 /* back where we started in free chunk list: "cannot happen" */
1415 EMSG2(_(e_intern2), "u_alloc_line()");
1416 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 }
1418 mprev = mp;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001419 mp = mp->m_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 }
1421
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001422 /* when using the largest chunk adjust mb_maxsize */
1423 if (mp->m_size >= mbp->mb_maxsize)
1424 mbp->mb_maxsize = 0;
1425
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 /* if the chunk we found is large enough, split it up in two */
1427 if ((long)mp->m_size - size_align >= (long)(sizeof(minfo_T) + 1))
1428 {
1429 mp2 = (minfo_T *)((char_u *)mp + size_align);
1430 mp2->m_size = mp->m_size - size_align;
1431 mp2->m_next = mp->m_next;
1432 mprev->m_next = mp2;
1433 mp->m_size = size_align;
1434 }
1435 else /* remove *mp from the free list */
1436 {
1437 mprev->m_next = mp->m_next;
1438 }
1439 curbuf->b_m_search = mprev;
1440 curbuf->b_mb_current = mbp;
1441
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001442 /* If using the largest chunk need to find the new largest chunk */
1443 if (mbp->mb_maxsize == 0)
1444 for (mp2 = &(mbp->mb_info); mp2 != NULL; mp2 = mp2->m_next)
1445 if (mbp->mb_maxsize < mp2->m_size)
1446 mbp->mb_maxsize = mp2->m_size;
1447
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 mp = (minfo_T *)((char_u *)mp + M_OFFSET);
1449 *(char_u *)mp = NUL; /* set the first byte to NUL */
1450
1451 return ((char_u *)mp);
1452}
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454
1455/*
1456 * u_save_line(): allocate memory with u_alloc_line() and copy line 'lnum'
1457 * into it.
1458 */
1459 static char_u *
1460u_save_line(lnum)
1461 linenr_T lnum;
1462{
1463 char_u *src;
1464 char_u *dst;
1465 unsigned len;
1466
1467 src = ml_get(lnum);
1468 len = (unsigned)STRLEN(src);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001469 if ((dst = U_ALLOC_LINE(len)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 mch_memmove(dst, src, (size_t)(len + 1));
1471 return (dst);
1472}
1473
1474/*
1475 * Check if the 'modified' flag is set, or 'ff' has changed (only need to
1476 * check the first character, because it can only be "dos", "unix" or "mac").
1477 * "nofile" and "scratch" type buffers are considered to always be unchanged.
1478 */
1479 int
1480bufIsChanged(buf)
1481 buf_T *buf;
1482{
1483 return
1484#ifdef FEAT_QUICKFIX
1485 !bt_dontwrite(buf) &&
1486#endif
1487 (buf->b_changed || file_ff_differs(buf));
1488}
1489
1490 int
1491curbufIsChanged()
1492{
1493 return
1494#ifdef FEAT_QUICKFIX
1495 !bt_dontwrite(curbuf) &&
1496#endif
1497 (curbuf->b_changed || file_ff_differs(curbuf));
1498}