blob: 27f04b92e55494e6cfb1c8d6663aefbb8ee42b25 [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),
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +000042 * or is NULL if nothing has been undone (end of the branch).
Bram Moolenaar071d4272004-06-13 20:20:40 +000043 *
Bram Moolenaar1e607892006-03-13 22:15:53 +000044 * For keeping alternate undo/redo branches the uh_alt field is used. Thus at
45 * each point in the list a branch may appear for an alternate to redo. The
46 * uh_seq field is numbered sequentially to be able to find a newer or older
47 * branch.
48 *
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +000049 * +---------------+ +---------------+
50 * b_u_oldhead --->| u_header | | u_header |
51 * | uh_alt_next ---->| uh_alt_next ----> NULL
52 * NULL <----- uh_alt_prev |<------ uh_alt_prev |
53 * | uh_prev | | uh_prev |
54 * +-----|---------+ +-----|---------+
55 * | |
56 * V V
57 * +---------------+ +---------------+
58 * | u_header | | u_header |
59 * | uh_alt_next | | uh_alt_next |
60 * b_u_newhead --->| uh_alt_prev | | uh_alt_prev |
61 * | uh_prev | | uh_prev |
62 * +-----|---------+ +-----|---------+
63 * | |
64 * V V
65 * NULL +---------------+ +---------------+
66 * | u_header | | u_header |
67 * | uh_alt_next ---->| uh_alt_next |
68 * | uh_alt_prev |<------ uh_alt_prev |
69 * | uh_prev | | uh_prev |
70 * +-----|---------+ +-----|---------+
71 * | |
72 * etc. etc.
73 *
74 *
Bram Moolenaar26a60b42005-02-22 08:49:11 +000075 * All data is allocated with U_ALLOC_LINE(), it will be freed as soon as the
76 * buffer is unloaded.
Bram Moolenaar071d4272004-06-13 20:20:40 +000077 */
78
79#include "vim.h"
80
Bram Moolenaar26a60b42005-02-22 08:49:11 +000081/* See below: use malloc()/free() for memory management. */
82#define U_USE_MALLOC 1
83
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +000084static void u_unch_branch __ARGS((u_header_T *uhp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000085static u_entry_T *u_get_headentry __ARGS((void));
86static void u_getbot __ARGS((void));
Bram Moolenaar8ada17c2006-01-19 22:16:24 +000087static int undo_allowed __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000088static int u_savecommon __ARGS((linenr_T, linenr_T, linenr_T));
89static void u_doit __ARGS((int count));
90static void u_undoredo __ARGS((void));
91static void u_undo_end __ARGS((void));
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +000092static void u_freeheader __ARGS((buf_T *buf, u_header_T *uhp, u_header_T **uhpp));
Bram Moolenaar1e607892006-03-13 22:15:53 +000093static void u_freebranch __ARGS((buf_T *buf, u_header_T *uhp, u_header_T **uhpp));
94static void u_freeentries __ARGS((buf_T *buf, u_header_T *uhp, u_header_T **uhpp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000095static void u_freeentry __ARGS((u_entry_T *, long));
96
Bram Moolenaar26a60b42005-02-22 08:49:11 +000097#ifdef U_USE_MALLOC
98# define U_FREE_LINE(ptr) vim_free(ptr)
99# define U_ALLOC_LINE(size) lalloc((long_u)((size) + 1), FALSE)
100#else
101static void u_free_line __ARGS((char_u *ptr, int keep));
102static char_u *u_alloc_line __ARGS((unsigned size));
103# define U_FREE_LINE(ptr) u_free_line((ptr), FALSE)
104# define U_ALLOC_LINE(size) u_alloc_line(size)
105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106static char_u *u_save_line __ARGS((linenr_T));
107
108static long u_newcount, u_oldcount;
109
110/*
111 * When 'u' flag included in 'cpoptions', we behave like vi. Need to remember
112 * the action that "u" should do.
113 */
114static int undo_undoes = FALSE;
115
116/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000117 * Save the current line for both the "u" and "U" command.
118 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119 */
120 int
121u_save_cursor()
122{
123 return (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
124 (linenr_T)(curwin->w_cursor.lnum + 1)));
125}
126
127/*
128 * Save the lines between "top" and "bot" for both the "u" and "U" command.
129 * "top" may be 0 and bot may be curbuf->b_ml.ml_line_count + 1.
130 * Returns FAIL when lines could not be saved, OK otherwise.
131 */
132 int
133u_save(top, bot)
134 linenr_T top, bot;
135{
136 if (undo_off)
137 return OK;
138
139 if (top > curbuf->b_ml.ml_line_count ||
140 top >= bot || bot > curbuf->b_ml.ml_line_count + 1)
141 return FALSE; /* rely on caller to do error messages */
142
143 if (top + 2 == bot)
144 u_saveline((linenr_T)(top + 1));
145
146 return (u_savecommon(top, bot, (linenr_T)0));
147}
148
149/*
150 * save the line "lnum" (used by ":s" and "~" command)
151 * The line is replaced, so the new bottom line is lnum + 1.
152 */
153 int
154u_savesub(lnum)
155 linenr_T lnum;
156{
157 if (undo_off)
158 return OK;
159
160 return (u_savecommon(lnum - 1, lnum + 1, lnum + 1));
161}
162
163/*
164 * a new line is inserted before line "lnum" (used by :s command)
165 * The line is inserted, so the new bottom line is lnum + 1.
166 */
167 int
168u_inssub(lnum)
169 linenr_T lnum;
170{
171 if (undo_off)
172 return OK;
173
174 return (u_savecommon(lnum - 1, lnum, lnum + 1));
175}
176
177/*
178 * save the lines "lnum" - "lnum" + nlines (used by delete command)
179 * The lines are deleted, so the new bottom line is lnum, unless the buffer
180 * becomes empty.
181 */
182 int
183u_savedel(lnum, nlines)
184 linenr_T lnum;
185 long nlines;
186{
187 if (undo_off)
188 return OK;
189
190 return (u_savecommon(lnum - 1, lnum + nlines,
191 nlines == curbuf->b_ml.ml_line_count ? 2 : lnum));
192}
193
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000194/*
195 * Return TRUE when undo is allowed. Otherwise give an error message and
196 * return FALSE.
197 */
198 static int
199undo_allowed()
200{
201 /* Don't allow changes when 'modifiable' is off. */
202 if (!curbuf->b_p_ma)
203 {
204 EMSG(_(e_modifiable));
205 return FALSE;
206 }
207
208#ifdef HAVE_SANDBOX
209 /* In the sandbox it's not allowed to change the text. */
210 if (sandbox != 0)
211 {
212 EMSG(_(e_sandbox));
213 return FALSE;
214 }
215#endif
216
217 /* Don't allow changes in the buffer while editing the cmdline. The
218 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000219 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000220 {
221 EMSG(_(e_secure));
222 return FALSE;
223 }
224
225 return TRUE;
226}
227
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 static int
229u_savecommon(top, bot, newbot)
230 linenr_T top, bot;
231 linenr_T newbot;
232{
Bram Moolenaar1e607892006-03-13 22:15:53 +0000233 linenr_T lnum;
234 long i;
235 u_header_T *uhp;
236 u_header_T *old_curhead;
237 u_entry_T *uep;
238 u_entry_T *prev_uep;
239 long size;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000241 /* When making changes is not allowed return FAIL. It's a crude way to
242 * make all change commands fail. */
243 if (!undo_allowed())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245
246#ifdef FEAT_NETBEANS_INTG
247 /*
248 * Netbeans defines areas that cannot be modified. Bail out here when
249 * trying to change text in a guarded area.
250 */
Bram Moolenaar009b2592004-10-24 19:18:58 +0000251 if (usingNetbeans)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 {
Bram Moolenaar009b2592004-10-24 19:18:58 +0000253 if (netbeans_is_guarded(top, bot))
254 {
255 EMSG(_(e_guarded));
256 return FAIL;
257 }
258 if (curbuf->b_p_ro)
259 {
260 EMSG(_(e_nbreadonly));
261 return FAIL;
262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 }
264#endif
265
266#ifdef FEAT_AUTOCMD
267 /*
268 * Saving text for undo means we are going to make a change. Give a
269 * warning for a read-only file before making the change, so that the
270 * FileChangedRO event can replace the buffer with a read-write version
271 * (e.g., obtained from a source control system).
272 */
273 change_warning(0);
274#endif
275
276 size = bot - top - 1;
277
278 /*
279 * if curbuf->b_u_synced == TRUE make a new header
280 */
281 if (curbuf->b_u_synced)
282 {
283#ifdef FEAT_JUMPLIST
284 /* Need to create new entry in b_changelist. */
285 curbuf->b_new_change = TRUE;
286#endif
287
Bram Moolenaar1e607892006-03-13 22:15:53 +0000288 if (p_ul >= 0)
289 {
290 /*
291 * Make a new header entry. Do this first so that we don't mess
292 * up the undo info when out of memory.
293 */
294 uhp = (u_header_T *)U_ALLOC_LINE((unsigned)sizeof(u_header_T));
295 if (uhp == NULL)
296 goto nomem;
297 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000298 else
299 uhp = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000300
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 /*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000302 * If we undid more than we redid, move the entry lists before and
303 * including curbuf->b_u_curhead to an alternate branch.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 */
Bram Moolenaar1e607892006-03-13 22:15:53 +0000305 old_curhead = curbuf->b_u_curhead;
306 if (old_curhead != NULL)
307 {
308 curbuf->b_u_newhead = old_curhead->uh_next;
309 curbuf->b_u_curhead = NULL;
310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311
312 /*
313 * free headers to keep the size right
314 */
315 while (curbuf->b_u_numhead > p_ul && curbuf->b_u_oldhead != NULL)
Bram Moolenaar1e607892006-03-13 22:15:53 +0000316 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000317 u_header_T *uhfree = curbuf->b_u_oldhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000318
319 /* If there is no branch only free one header. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000320 if (uhfree->uh_alt_next == NULL)
321 u_freeheader(curbuf, uhfree, &old_curhead);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000322 else
323 {
324 /* Free the oldest alternate branch as a whole. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000325 while (uhfree->uh_alt_next != NULL)
326 uhfree = uhfree->uh_alt_next;
327 u_freebranch(curbuf, uhfree, &old_curhead);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000328 }
329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000331 if (uhp == NULL) /* no undo at all */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332 {
Bram Moolenaar1e607892006-03-13 22:15:53 +0000333 if (old_curhead != NULL)
334 u_freebranch(curbuf, old_curhead, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 curbuf->b_u_synced = FALSE;
336 return OK;
337 }
338
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 uhp->uh_prev = NULL;
340 uhp->uh_next = curbuf->b_u_newhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000341 uhp->uh_alt_next = old_curhead;
342 if (old_curhead != NULL)
343 {
344 old_curhead->uh_alt_prev = uhp;
345 if (curbuf->b_u_oldhead == old_curhead)
346 curbuf->b_u_oldhead = uhp;
347 }
348 uhp->uh_alt_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 if (curbuf->b_u_newhead != NULL)
350 curbuf->b_u_newhead->uh_prev = uhp;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000351
352 uhp->uh_seq = curbuf->b_u_seq_last++;
353 curbuf->b_u_seq_cur = curbuf->b_u_seq_last;
354 uhp->uh_time = time(NULL);
355 curbuf->b_u_seq_time = uhp->uh_time + 1;
356
Bram Moolenaar1e607892006-03-13 22:15:53 +0000357 uhp->uh_walk = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 uhp->uh_entry = NULL;
359 uhp->uh_getbot_entry = NULL;
360 uhp->uh_cursor = curwin->w_cursor; /* save cursor pos. for undo */
361#ifdef FEAT_VIRTUALEDIT
362 if (virtual_active() && curwin->w_cursor.coladd > 0)
363 uhp->uh_cursor_vcol = getviscol();
364 else
365 uhp->uh_cursor_vcol = -1;
366#endif
367
368 /* save changed and buffer empty flag for undo */
369 uhp->uh_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
370 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
371
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000372 /* save named marks and Visual marks for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373 mch_memmove(uhp->uh_namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000374#ifdef FEAT_VISUAL
375 uhp->uh_visual = curbuf->b_visual;
376#endif
377
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 curbuf->b_u_newhead = uhp;
379 if (curbuf->b_u_oldhead == NULL)
380 curbuf->b_u_oldhead = uhp;
381 ++curbuf->b_u_numhead;
382 }
383 else
384 {
385 if (p_ul < 0) /* no undo at all */
386 return OK;
387
388 /*
389 * When saving a single line, and it has been saved just before, it
390 * doesn't make sense saving it again. Saves a lot of memory when
391 * making lots of changes inside the same line.
392 * This is only possible if the previous change didn't increase or
393 * decrease the number of lines.
394 * Check the ten last changes. More doesn't make sense and takes too
395 * long.
396 */
397 if (size == 1)
398 {
399 uep = u_get_headentry();
400 prev_uep = NULL;
401 for (i = 0; i < 10; ++i)
402 {
403 if (uep == NULL)
404 break;
405
406 /* If lines have been inserted/deleted we give up.
407 * Also when the line was included in a multi-line save. */
408 if ((curbuf->b_u_newhead->uh_getbot_entry != uep
409 ? (uep->ue_top + uep->ue_size + 1
410 != (uep->ue_bot == 0
411 ? curbuf->b_ml.ml_line_count + 1
412 : uep->ue_bot))
413 : uep->ue_lcount != curbuf->b_ml.ml_line_count)
414 || (uep->ue_size > 1
415 && top >= uep->ue_top
416 && top + 2 <= uep->ue_top + uep->ue_size + 1))
417 break;
418
419 /* If it's the same line we can skip saving it again. */
420 if (uep->ue_size == 1 && uep->ue_top == top)
421 {
422 if (i > 0)
423 {
424 /* It's not the last entry: get ue_bot for the last
425 * entry now. Following deleted/inserted lines go to
426 * the re-used entry. */
427 u_getbot();
428 curbuf->b_u_synced = FALSE;
429
430 /* Move the found entry to become the last entry. The
431 * order of undo/redo doesn't matter for the entries
432 * we move it over, since they don't change the line
433 * count and don't include this line. It does matter
434 * for the found entry if the line count is changed by
435 * the executed command. */
436 prev_uep->ue_next = uep->ue_next;
437 uep->ue_next = curbuf->b_u_newhead->uh_entry;
438 curbuf->b_u_newhead->uh_entry = uep;
439 }
440
441 /* The executed command may change the line count. */
442 if (newbot != 0)
443 uep->ue_bot = newbot;
444 else if (bot > curbuf->b_ml.ml_line_count)
445 uep->ue_bot = 0;
446 else
447 {
448 uep->ue_lcount = curbuf->b_ml.ml_line_count;
449 curbuf->b_u_newhead->uh_getbot_entry = uep;
450 }
451 return OK;
452 }
453 prev_uep = uep;
454 uep = uep->ue_next;
455 }
456 }
457
458 /* find line number for ue_bot for previous u_save() */
459 u_getbot();
460 }
461
462#if !defined(UNIX) && !defined(DJGPP) && !defined(WIN32) && !defined(__EMX__)
463 /*
464 * With Amiga and MSDOS 16 bit we can't handle big undo's, because
465 * then u_alloc_line would have to allocate a block larger than 32K
466 */
467 if (size >= 8000)
468 goto nomem;
469#endif
470
471 /*
472 * add lines in front of entry list
473 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000474 uep = (u_entry_T *)U_ALLOC_LINE((unsigned)sizeof(u_entry_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 if (uep == NULL)
476 goto nomem;
477
478 uep->ue_size = size;
479 uep->ue_top = top;
480 if (newbot != 0)
481 uep->ue_bot = newbot;
482 /*
483 * Use 0 for ue_bot if bot is below last line.
484 * Otherwise we have to compute ue_bot later.
485 */
486 else if (bot > curbuf->b_ml.ml_line_count)
487 uep->ue_bot = 0;
488 else
489 {
490 uep->ue_lcount = curbuf->b_ml.ml_line_count;
491 curbuf->b_u_newhead->uh_getbot_entry = uep;
492 }
493
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000494 if (size > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000496 if ((uep->ue_array = (char_u **)U_ALLOC_LINE(
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 (unsigned)(sizeof(char_u *) * size))) == NULL)
498 {
499 u_freeentry(uep, 0L);
500 goto nomem;
501 }
502 for (i = 0, lnum = top + 1; i < size; ++i)
503 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000504 fast_breakcheck();
505 if (got_int)
506 {
507 u_freeentry(uep, i);
508 return FAIL;
509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 if ((uep->ue_array[i] = u_save_line(lnum++)) == NULL)
511 {
512 u_freeentry(uep, i);
513 goto nomem;
514 }
515 }
516 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000517 else
518 uep->ue_array = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 uep->ue_next = curbuf->b_u_newhead->uh_entry;
520 curbuf->b_u_newhead->uh_entry = uep;
521 curbuf->b_u_synced = FALSE;
522 undo_undoes = FALSE;
523
524 return OK;
525
526nomem:
527 msg_silent = 0; /* must display the prompt */
528 if (ask_yesno((char_u *)_("No undo possible; continue anyway"), TRUE)
529 == 'y')
530 {
531 undo_off = TRUE; /* will be reset when character typed */
532 return OK;
533 }
534 do_outofmem_msg((long_u)0);
535 return FAIL;
536}
537
538/*
539 * If 'cpoptions' contains 'u': Undo the previous undo or redo (vi compatible).
540 * If 'cpoptions' does not contain 'u': Always undo.
541 */
542 void
543u_undo(count)
544 int count;
545{
546 /*
547 * If we get an undo command while executing a macro, we behave like the
548 * original vi. If this happens twice in one macro the result will not
549 * be compatible.
550 */
551 if (curbuf->b_u_synced == FALSE)
552 {
553 u_sync();
554 count = 1;
555 }
556
557 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
558 undo_undoes = TRUE;
559 else
560 undo_undoes = !undo_undoes;
561 u_doit(count);
562}
563
564/*
565 * If 'cpoptions' contains 'u': Repeat the previous undo or redo.
566 * If 'cpoptions' does not contain 'u': Always redo.
567 */
568 void
569u_redo(count)
570 int count;
571{
572 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
573 undo_undoes = FALSE;
574 u_doit(count);
575}
576
577/*
578 * Undo or redo, depending on 'undo_undoes', 'count' times.
579 */
580 static void
581u_doit(count)
582 int count;
583{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000584 if (!undo_allowed())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586
587 u_newcount = 0;
588 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000589 if (curbuf->b_ml.ml_flags & ML_EMPTY)
590 u_oldcount = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 while (count--)
592 {
593 if (undo_undoes)
594 {
595 if (curbuf->b_u_curhead == NULL) /* first undo */
596 curbuf->b_u_curhead = curbuf->b_u_newhead;
597 else if (p_ul > 0) /* multi level undo */
598 /* get next undo */
599 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_next;
600 /* nothing to undo */
601 if (curbuf->b_u_numhead == 0 || curbuf->b_u_curhead == NULL)
602 {
603 /* stick curbuf->b_u_curhead at end */
604 curbuf->b_u_curhead = curbuf->b_u_oldhead;
605 beep_flush();
606 break;
607 }
608
609 u_undoredo();
610 }
611 else
612 {
613 if (curbuf->b_u_curhead == NULL || p_ul <= 0)
614 {
615 beep_flush(); /* nothing to redo */
616 break;
617 }
618
619 u_undoredo();
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000620 ++curbuf->b_u_seq_cur;
621 ++curbuf->b_u_seq_time;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000622
623 /* Advance for next redo. Set "newhead" when at the end of the
624 * redoable changes. */
625 if (curbuf->b_u_curhead->uh_prev == NULL)
626 curbuf->b_u_newhead = curbuf->b_u_curhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_prev;
628 }
629 }
Bram Moolenaar1e607892006-03-13 22:15:53 +0000630 u_undo_end();
631}
632
633static int lastmark = 0;
634
635/*
636 * Undo or redo over the timeline.
637 * When "step" is negative go back in time, otherwise goes forward in time.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000638 * When "sec" is FALSE make "step" steps, when "sec" is TRUE use "step" as
639 * seconds.
Bram Moolenaar1e607892006-03-13 22:15:53 +0000640 */
641 void
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000642undo_time(step, sec)
643 long step;
644 int sec;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000645{
646 long target;
647 long closest;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000648 long closest_start;
649 long closest_seq = 0;
650 long val;
651 long limit;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000652 u_header_T *uhp;
653 u_header_T *last;
654 int mark;
655 int nomark;
656 int round;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000657 int dosec = sec;
658 int above = FALSE;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000659
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000660 /* First make sure the current undoable change is synced. */
661 if (curbuf->b_u_synced == FALSE)
662 u_sync();
663
Bram Moolenaar1e607892006-03-13 22:15:53 +0000664 u_newcount = 0;
665 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000666 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar1e607892006-03-13 22:15:53 +0000667 u_oldcount = -1;
668
669 /* "target" is the node below which we want to be. When going forward
670 * the current one also counts, thus do one less. */
671 if (step < 0)
672 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000673 if (sec)
674 target = (long)curbuf->b_u_seq_time + step;
675 else
676 target = curbuf->b_u_seq_cur + step;
677 if (target < 0)
678 target = -1;
679 closest = -2;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000680 }
681 else
682 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000683 if (sec)
684 {
685 target = curbuf->b_u_seq_time + step - 1;
686 closest = time(NULL) + 1;
687 }
688 else
689 {
690 target = curbuf->b_u_seq_cur + step - 1;
691 closest = curbuf->b_u_seq_last + 1;
692 }
693 if (target >= closest)
694 target = closest - 1;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000695 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000696 closest_start = closest;
697 if (sec)
698 limit = curbuf->b_u_seq_time + (step > 0 ? -1 : 1);
699 else
700 limit = curbuf->b_u_seq_cur;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000701
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000702 /*
703 * May do this twice:
Bram Moolenaar1e607892006-03-13 22:15:53 +0000704 * 1. Search for "target", update "closest" to the best match found.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000705 * 2. If "target" not found search for "closest".
706 *
707 * When using the closest time we use the sequence number in the second
708 * round, because there may be several entries with the same time.
709 */
Bram Moolenaar1e607892006-03-13 22:15:53 +0000710 for (round = 1; round <= 2; ++round)
711 {
712 /* Find the path from the current state to where we want to go. The
713 * desired state can be anywhere in the undo tree, need to go all over
714 * it. We put "nomark" in uh_walk where we have been without success,
715 * "mark" where it could possibly be. */
716 mark = ++lastmark;
717 nomark = ++lastmark;
718
719 if (curbuf->b_u_curhead == NULL) /* at leaf of the tree */
720 uhp = curbuf->b_u_newhead;
721 else
722 uhp = curbuf->b_u_curhead;
723
724 while (uhp != NULL)
725 {
726 uhp->uh_walk = mark;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000727 val = (dosec ? uhp->uh_time : uhp->uh_seq);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000728
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000729 if (round == 1)
730 {
731 /* Remember the header that is closest to the target.
732 * It must be at least in the right direction (checked with
733 * "limit"). When the timestamp is equal find the
734 * highest/lowest sequence number. */
735 if ((dosec && val == closest)
736 ? (step < 0
737 ? uhp->uh_seq < closest_seq
738 : uhp->uh_seq > closest_seq)
739 : (step < 0
740 ? (val < limit
741 && (closest > target
742 ? (val <= closest)
743 : (val >= closest)))
744 : (val > limit
745 && (closest < target
746 ? val >= closest
747 : val <= closest))))
748 {
749 closest = val;
750 closest_seq = uhp->uh_seq;
751 }
752 }
753
754 /* Quit searching when we found a match. But when searching for a
755 * time we need to continue looking for the best uh_seq. */
756 if (target == val && !dosec)
757 break;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000758
759 /* go down in the tree if we haven't been there */
760 if (uhp->uh_prev != NULL && uhp->uh_prev->uh_walk != nomark
761 && uhp->uh_prev->uh_walk != mark)
762 uhp = uhp->uh_prev;
763
764 /* go to alternate branch if we haven't been there */
765 else if (uhp->uh_alt_next != NULL
766 && uhp->uh_alt_next->uh_walk != nomark
767 && uhp->uh_alt_next->uh_walk != mark)
768 uhp = uhp->uh_alt_next;
769
770 /* go up in the tree if we haven't been there and we are at the
771 * start of alternate branches */
772 else if (uhp->uh_next != NULL && uhp->uh_alt_prev == NULL
773 && uhp->uh_next->uh_walk != nomark
774 && uhp->uh_next->uh_walk != mark)
775 uhp = uhp->uh_next;
776
777 else
778 {
779 /* need to backtrack; mark this node as useless */
780 uhp->uh_walk = nomark;
781 if (uhp->uh_alt_prev != NULL)
782 uhp = uhp->uh_alt_prev;
783 else
784 uhp = uhp->uh_next;
785 }
786 }
787
788 if (uhp != NULL) /* found it */
789 break;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000790 if (closest == closest_start)
Bram Moolenaar1e607892006-03-13 22:15:53 +0000791 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000792 if (step < 0)
793 MSG(_("Already at oldest change"));
794 else
795 MSG(_("Already at newest change"));
Bram Moolenaar1e607892006-03-13 22:15:53 +0000796 return;
797 }
798
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000799 target = closest_seq;
800 dosec = FALSE;
801 if (step < 0)
802 above = TRUE; /* stop above the header */
Bram Moolenaar1e607892006-03-13 22:15:53 +0000803 }
804
805 /* If we found it: Follow the path to go to where we want to be. */
806 if (uhp != NULL)
807 {
808 /*
809 * First go up the tree as much as needed.
810 */
811 for (;;)
812 {
813 uhp = curbuf->b_u_curhead;
814 if (uhp == NULL)
815 uhp = curbuf->b_u_newhead;
816 else
817 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000818 while (uhp->uh_alt_prev != NULL
819 && uhp->uh_alt_prev->uh_walk == mark)
Bram Moolenaar1e607892006-03-13 22:15:53 +0000820 {
821 uhp->uh_walk = nomark;
822 uhp = uhp->uh_alt_prev;
823 }
824 uhp = uhp->uh_next;
825 }
826 if (uhp == NULL || uhp->uh_walk != mark)
827 break;
828 curbuf->b_u_curhead = uhp;
829 u_undoredo();
830 uhp->uh_walk = nomark; /* don't go back down here */
Bram Moolenaar1e607892006-03-13 22:15:53 +0000831 }
832
833 /*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000834 * And now go down the tree (redo), branching off where needed.
Bram Moolenaar1e607892006-03-13 22:15:53 +0000835 */
836 uhp = curbuf->b_u_curhead;
837 for (;;)
838 {
839 /* Find the last branch with a mark, that's the one. */
840 last = uhp;
841 while (last->uh_alt_next != NULL
842 && last->uh_alt_next->uh_walk == mark)
843 last = last->uh_alt_next;
844 if (last != uhp)
845 {
846 /* Make the used branch the first entry in the list of
847 * alternatives to make "u" and CTRL-R take this branch. */
848 if (last->uh_alt_next != NULL)
849 last->uh_alt_next->uh_alt_prev = last->uh_alt_prev;
850 last->uh_alt_prev->uh_alt_next = last->uh_alt_next;
851 last->uh_alt_prev = NULL;
852 last->uh_alt_next = uhp;
853 uhp->uh_alt_prev = last;
854
855 uhp = last;
856 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000857 curbuf->b_u_curhead = uhp;
858 curbuf->b_u_seq_cur = uhp->uh_seq;
859 curbuf->b_u_seq_time = uhp->uh_time;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000860
861 if (uhp->uh_walk != mark)
862 break; /* must have reached the target */
863
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000864 /* Stop when going backwards in time and didn't find the exact
865 * header we were looking for. */
866 if (uhp->uh_seq == target && above)
867 break;
868
Bram Moolenaar1e607892006-03-13 22:15:53 +0000869 u_undoredo();
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000870 ++curbuf->b_u_seq_cur;
871 ++curbuf->b_u_seq_time;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000872
873 /* Advance "curhead" to below the header we last used. If it
874 * becomes NULL then we need to set "newhead" to this leaf. */
875 if (uhp->uh_prev == NULL)
876 curbuf->b_u_newhead = uhp;
877 curbuf->b_u_curhead = uhp->uh_prev;
878
879 if (uhp->uh_seq == target) /* found it! */
880 break;
881
882 uhp = uhp->uh_prev;
883 if (uhp == NULL || uhp->uh_walk != mark)
884 {
885 EMSG2(_(e_intern2), "undo_time()");
886 break;
887 }
888 }
889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 u_undo_end();
891}
892
893/*
894 * u_undoredo: common code for undo and redo
895 *
896 * The lines in the file are replaced by the lines in the entry list at
897 * curbuf->b_u_curhead. The replaced lines in the file are saved in the entry
898 * list for the next undo/redo.
899 */
900 static void
901u_undoredo()
902{
903 char_u **newarray = NULL;
904 linenr_T oldsize;
905 linenr_T newsize;
906 linenr_T top, bot;
907 linenr_T lnum;
908 linenr_T newlnum = MAXLNUM;
909 long i;
910 u_entry_T *uep, *nuep;
911 u_entry_T *newlist = NULL;
912 int old_flags;
913 int new_flags;
914 pos_T namedm[NMARKS];
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000915#ifdef FEAT_VISUAL
916 visualinfo_T visualinfo;
917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 int empty_buffer; /* buffer became empty */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000919 u_header_T *curhead = curbuf->b_u_curhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000921 old_flags = curhead->uh_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 new_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
923 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
924 setpcmark();
925
926 /*
927 * save marks before undo/redo
928 */
929 mch_memmove(namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000930#ifdef FEAT_VISUAL
931 visualinfo = curbuf->b_visual;
932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 curbuf->b_op_start.lnum = curbuf->b_ml.ml_line_count;
934 curbuf->b_op_start.col = 0;
935 curbuf->b_op_end.lnum = 0;
936 curbuf->b_op_end.col = 0;
937
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000938 for (uep = curhead->uh_entry; uep != NULL; uep = nuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 {
940 top = uep->ue_top;
941 bot = uep->ue_bot;
942 if (bot == 0)
943 bot = curbuf->b_ml.ml_line_count + 1;
944 if (top > curbuf->b_ml.ml_line_count || top >= bot
945 || bot > curbuf->b_ml.ml_line_count + 1)
946 {
947 EMSG(_("E438: u_undo: line numbers wrong"));
948 changed(); /* don't want UNCHANGED now */
949 return;
950 }
951
952 oldsize = bot - top - 1; /* number of lines before undo */
953 newsize = uep->ue_size; /* number of lines after undo */
954
955 if (top < newlnum)
956 {
957 /* If the saved cursor is somewhere in this undo block, move it to
958 * the remembered position. Makes "gwap" put the cursor back
959 * where it was. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000960 lnum = curhead->uh_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 if (lnum >= top && lnum <= top + newsize + 1)
962 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000963 curwin->w_cursor = curhead->uh_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 newlnum = curwin->w_cursor.lnum - 1;
965 }
966 else
967 {
968 /* Use the first line that actually changed. Avoids that
969 * undoing auto-formatting puts the cursor in the previous
970 * line. */
971 for (i = 0; i < newsize && i < oldsize; ++i)
972 if (STRCMP(uep->ue_array[i], ml_get(top + 1 + i)) != 0)
973 break;
974 if (i == newsize && newlnum == MAXLNUM && uep->ue_next == NULL)
975 {
976 newlnum = top;
977 curwin->w_cursor.lnum = newlnum + 1;
978 }
979 else if (i < newsize)
980 {
981 newlnum = top + i;
982 curwin->w_cursor.lnum = newlnum + 1;
983 }
984 }
985 }
986
987 empty_buffer = FALSE;
988
989 /* delete the lines between top and bot and save them in newarray */
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000990 if (oldsize > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000992 if ((newarray = (char_u **)U_ALLOC_LINE(
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 (unsigned)(sizeof(char_u *) * oldsize))) == NULL)
994 {
995 do_outofmem_msg((long_u)(sizeof(char_u *) * oldsize));
996 /*
997 * We have messed up the entry list, repair is impossible.
998 * we have to free the rest of the list.
999 */
1000 while (uep != NULL)
1001 {
1002 nuep = uep->ue_next;
1003 u_freeentry(uep, uep->ue_size);
1004 uep = nuep;
1005 }
1006 break;
1007 }
1008 /* delete backwards, it goes faster in most cases */
1009 for (lnum = bot - 1, i = oldsize; --i >= 0; --lnum)
1010 {
1011 /* what can we do when we run out of memory? */
1012 if ((newarray[i] = u_save_line(lnum)) == NULL)
1013 do_outofmem_msg((long_u)0);
1014 /* remember we deleted the last line in the buffer, and a
1015 * dummy empty line will be inserted */
1016 if (curbuf->b_ml.ml_line_count == 1)
1017 empty_buffer = TRUE;
1018 ml_delete(lnum, FALSE);
1019 }
1020 }
Bram Moolenaar8d343302005-07-12 22:46:17 +00001021 else
1022 newarray = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023
1024 /* insert the lines in u_array between top and bot */
1025 if (newsize)
1026 {
1027 for (lnum = top, i = 0; i < newsize; ++i, ++lnum)
1028 {
1029 /*
1030 * If the file is empty, there is an empty line 1 that we
1031 * should get rid of, by replacing it with the new line
1032 */
1033 if (empty_buffer && lnum == 0)
1034 ml_replace((linenr_T)1, uep->ue_array[i], TRUE);
1035 else
1036 ml_append(lnum, uep->ue_array[i], (colnr_T)0, FALSE);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001037 U_FREE_LINE(uep->ue_array[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001039 U_FREE_LINE((char_u *)uep->ue_array);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 }
1041
1042 /* adjust marks */
1043 if (oldsize != newsize)
1044 {
1045 mark_adjust(top + 1, top + oldsize, (long)MAXLNUM,
1046 (long)newsize - (long)oldsize);
1047 if (curbuf->b_op_start.lnum > top + oldsize)
1048 curbuf->b_op_start.lnum += newsize - oldsize;
1049 if (curbuf->b_op_end.lnum > top + oldsize)
1050 curbuf->b_op_end.lnum += newsize - oldsize;
1051 }
1052
1053 changed_lines(top + 1, 0, bot, newsize - oldsize);
1054
1055 /* set '[ and '] mark */
1056 if (top + 1 < curbuf->b_op_start.lnum)
1057 curbuf->b_op_start.lnum = top + 1;
1058 if (newsize == 0 && top + 1 > curbuf->b_op_end.lnum)
1059 curbuf->b_op_end.lnum = top + 1;
1060 else if (top + newsize > curbuf->b_op_end.lnum)
1061 curbuf->b_op_end.lnum = top + newsize;
1062
1063 u_newcount += newsize;
1064 u_oldcount += oldsize;
1065 uep->ue_size = oldsize;
1066 uep->ue_array = newarray;
1067 uep->ue_bot = top + newsize + 1;
1068
1069 /*
1070 * insert this entry in front of the new entry list
1071 */
1072 nuep = uep->ue_next;
1073 uep->ue_next = newlist;
1074 newlist = uep;
1075 }
1076
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001077 curhead->uh_entry = newlist;
1078 curhead->uh_flags = new_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 if ((old_flags & UH_EMPTYBUF) && bufempty())
1080 curbuf->b_ml.ml_flags |= ML_EMPTY;
1081 if (old_flags & UH_CHANGED)
1082 changed();
1083 else
Bram Moolenaar009b2592004-10-24 19:18:58 +00001084#ifdef FEAT_NETBEANS_INTG
1085 /* per netbeans undo rules, keep it as modified */
1086 if (!isNetbeansModified(curbuf))
1087#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 unchanged(curbuf, FALSE);
1089
1090 /*
1091 * restore marks from before undo/redo
1092 */
1093 for (i = 0; i < NMARKS; ++i)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001094 if (curhead->uh_namedm[i].lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001096 curbuf->b_namedm[i] = curhead->uh_namedm[i];
1097 curhead->uh_namedm[i] = namedm[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001099#ifdef FEAT_VISUAL
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001100 if (curhead->uh_visual.vi_start.lnum != 0)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001101 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001102 curbuf->b_visual = curhead->uh_visual;
1103 curhead->uh_visual = visualinfo;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001104 }
1105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106
1107 /*
1108 * If the cursor is only off by one line, put it at the same position as
1109 * before starting the change (for the "o" command).
1110 * Otherwise the cursor should go to the first undone line.
1111 */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001112 if (curhead->uh_cursor.lnum + 1 == curwin->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 && curwin->w_cursor.lnum > 1)
1114 --curwin->w_cursor.lnum;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001115 if (curhead->uh_cursor.lnum == curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001117 curwin->w_cursor.col = curhead->uh_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001119 if (virtual_active() && curhead->uh_cursor_vcol >= 0)
1120 coladvance((colnr_T)curhead->uh_cursor_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 else
1122 curwin->w_cursor.coladd = 0;
1123#endif
1124 }
1125 else if (curwin->w_cursor.lnum <= curbuf->b_ml.ml_line_count)
1126 beginline(BL_SOL | BL_FIX);
1127 else
1128 {
1129 /* We get here with the current cursor line being past the end (eg
1130 * after adding lines at the end of the file, and then undoing it).
1131 * check_cursor() will move the cursor to the last line. Move it to
1132 * the first column here. */
1133 curwin->w_cursor.col = 0;
1134#ifdef FEAT_VIRTUALEDIT
1135 curwin->w_cursor.coladd = 0;
1136#endif
1137 }
1138
1139 /* Make sure the cursor is on an existing line and column. */
1140 check_cursor();
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001141
1142 /* Remember where we are for "g-" and ":earlier 10s". */
1143 curbuf->b_u_seq_cur = curhead->uh_seq;
1144 curbuf->b_u_seq_time = curhead->uh_time;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145}
1146
1147/*
1148 * If we deleted or added lines, report the number of less/more lines.
1149 * Otherwise, report the number of changes (this may be incorrect
1150 * in some cases, but it's better than nothing).
1151 */
1152 static void
1153u_undo_end()
1154{
Bram Moolenaar1e607892006-03-13 22:15:53 +00001155 long sec;
1156 char *msg;
1157
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158#ifdef FEAT_FOLDING
1159 if ((fdo_flags & FDO_UNDO) && KeyTyped)
1160 foldOpenCursor();
1161#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +00001162
1163 if (global_busy /* no messages now, wait until global is finished */
1164 || !messaging()) /* 'lazyredraw' set, don't do messages now */
1165 return;
1166
1167 if (curbuf->b_ml.ml_flags & ML_EMPTY)
1168 --u_newcount;
1169
1170 u_oldcount -= u_newcount;
1171 if (u_oldcount == -1)
1172 msg = N_("more line");
1173 else if (u_oldcount < 0)
1174 msg = N_("more lines");
1175 else if (u_oldcount == 1)
1176 msg = N_("line less");
1177 else if (u_oldcount > 1)
1178 msg = N_("fewer lines");
1179 else
1180 {
1181 u_oldcount = u_newcount;
1182 if (u_newcount == 1)
1183 msg = N_("change");
1184 else
1185 msg = N_("changes");
1186 }
1187
1188 if (curbuf->b_u_curhead == 0)
1189 sec = 0;
1190 else
1191 sec = time(NULL) - curbuf->b_u_curhead->uh_time;
1192
1193 smsg((char_u *)_("%ld %s; %ld seconds ago"), u_oldcount, _(msg), sec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194}
1195
1196/*
1197 * u_sync: stop adding to the current entry list
1198 */
1199 void
1200u_sync()
1201{
1202 if (curbuf->b_u_synced)
1203 return; /* already synced */
1204#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
1205 if (im_is_preediting())
1206 return; /* XIM is busy, don't break an undo sequence */
1207#endif
1208 if (p_ul < 0)
1209 curbuf->b_u_synced = TRUE; /* no entries, nothing to do */
1210 else
1211 {
1212 u_getbot(); /* compute ue_bot of previous u_save */
1213 curbuf->b_u_curhead = NULL;
1214 }
1215}
1216
1217/*
Bram Moolenaare224ffa2006-03-01 00:01:28 +00001218 * ":undojoin": continue adding to the last entry list
1219 */
1220/*ARGSUSED*/
1221 void
1222ex_undojoin(eap)
1223 exarg_T *eap;
1224{
1225 if (!curbuf->b_u_synced)
1226 return; /* already unsynced */
1227 if (curbuf->b_u_newhead == NULL)
1228 return; /* nothing changed before */
1229 if (p_ul < 0)
1230 return; /* no entries, nothing to do */
1231 else
1232 {
1233 /* Go back to the last entry */
1234 curbuf->b_u_curhead = curbuf->b_u_newhead;
1235 curbuf->b_u_synced = FALSE; /* no entries, nothing to do */
1236 }
1237}
1238
1239/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 * Called after writing the file and setting b_changed to FALSE.
1241 * Now an undo means that the buffer is modified.
1242 */
1243 void
1244u_unchanged(buf)
1245 buf_T *buf;
1246{
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001247 u_unch_branch(buf->b_u_oldhead);
1248 buf->b_did_warn = FALSE;
1249}
1250
1251 static void
1252u_unch_branch(uhp)
1253 u_header_T *uhp;
1254{
Bram Moolenaar1e607892006-03-13 22:15:53 +00001255 u_header_T *uh;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001257 for (uh = uhp; uh != NULL; uh = uh->uh_prev)
1258 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 uh->uh_flags |= UH_CHANGED;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001260 if (uh->uh_alt_next != NULL)
1261 u_unch_branch(uh->uh_alt_next); /* recursive */
1262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263}
1264
1265/*
1266 * Get pointer to last added entry.
1267 * If it's not valid, give an error message and return NULL.
1268 */
1269 static u_entry_T *
1270u_get_headentry()
1271{
1272 if (curbuf->b_u_newhead == NULL || curbuf->b_u_newhead->uh_entry == NULL)
1273 {
1274 EMSG(_("E439: undo list corrupt"));
1275 return NULL;
1276 }
1277 return curbuf->b_u_newhead->uh_entry;
1278}
1279
1280/*
1281 * u_getbot(): compute the line number of the previous u_save
1282 * It is called only when b_u_synced is FALSE.
1283 */
1284 static void
1285u_getbot()
1286{
1287 u_entry_T *uep;
1288 linenr_T extra;
1289
1290 uep = u_get_headentry(); /* check for corrupt undo list */
1291 if (uep == NULL)
1292 return;
1293
1294 uep = curbuf->b_u_newhead->uh_getbot_entry;
1295 if (uep != NULL)
1296 {
1297 /*
1298 * the new ue_bot is computed from the number of lines that has been
1299 * inserted (0 - deleted) since calling u_save. This is equal to the
1300 * old line count subtracted from the current line count.
1301 */
1302 extra = curbuf->b_ml.ml_line_count - uep->ue_lcount;
1303 uep->ue_bot = uep->ue_top + uep->ue_size + 1 + extra;
1304 if (uep->ue_bot < 1 || uep->ue_bot > curbuf->b_ml.ml_line_count)
1305 {
1306 EMSG(_("E440: undo line missing"));
1307 uep->ue_bot = uep->ue_top + 1; /* assume all lines deleted, will
1308 * get all the old lines back
1309 * without deleting the current
1310 * ones */
1311 }
1312
1313 curbuf->b_u_newhead->uh_getbot_entry = NULL;
1314 }
1315
1316 curbuf->b_u_synced = TRUE;
1317}
1318
1319/*
Bram Moolenaar1e607892006-03-13 22:15:53 +00001320 * Free one header and its entry list and adjust the pointers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 */
1322 static void
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001323u_freeheader(buf, uhp, uhpp)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001324 buf_T *buf;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001325 u_header_T *uhp;
1326 u_header_T **uhpp; /* if not NULL reset when freeing this header */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327{
Bram Moolenaar1e607892006-03-13 22:15:53 +00001328 /* When there is an alternate redo list free that branch completely,
1329 * because we can never go there. */
1330 if (uhp->uh_alt_next != NULL)
1331 u_freebranch(buf, uhp->uh_alt_next, uhpp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332
Bram Moolenaar1e607892006-03-13 22:15:53 +00001333 if (uhp->uh_alt_prev != NULL)
1334 uhp->uh_alt_prev->uh_alt_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335
Bram Moolenaar1e607892006-03-13 22:15:53 +00001336 /* Update the links in the list to remove the header. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 if (uhp->uh_next == NULL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001338 buf->b_u_oldhead = uhp->uh_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 else
1340 uhp->uh_next->uh_prev = uhp->uh_prev;
1341
1342 if (uhp->uh_prev == NULL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001343 buf->b_u_newhead = uhp->uh_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 else
1345 uhp->uh_prev->uh_next = uhp->uh_next;
1346
Bram Moolenaar1e607892006-03-13 22:15:53 +00001347 u_freeentries(buf, uhp, uhpp);
1348}
1349
1350/*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001351 * Free an alternate branch and any following alternate branches.
Bram Moolenaar1e607892006-03-13 22:15:53 +00001352 */
1353 static void
1354u_freebranch(buf, uhp, uhpp)
1355 buf_T *buf;
1356 u_header_T *uhp;
1357 u_header_T **uhpp; /* if not NULL reset when freeing this header */
1358{
1359 u_header_T *tofree, *next;
1360
1361 if (uhp->uh_alt_prev != NULL)
1362 uhp->uh_alt_prev->uh_alt_next = NULL;
1363
1364 next = uhp;
1365 while (next != NULL)
1366 {
1367 tofree = next;
1368 if (tofree->uh_alt_next != NULL)
1369 u_freebranch(buf, tofree->uh_alt_next, uhpp); /* recursive */
1370 next = tofree->uh_prev;
1371 u_freeentries(buf, tofree, uhpp);
1372 }
1373}
1374
1375/*
1376 * Free all the undo entries for one header and the header itself.
1377 * This means that "uhp" is invalid when returning.
1378 */
1379 static void
1380u_freeentries(buf, uhp, uhpp)
1381 buf_T *buf;
1382 u_header_T *uhp;
1383 u_header_T **uhpp; /* if not NULL reset when freeing this header */
1384{
1385 u_entry_T *uep, *nuep;
1386
1387 /* Check for pointers to the header that become invalid now. */
1388 if (buf->b_u_curhead == uhp)
1389 buf->b_u_curhead = NULL;
1390 if (uhpp != NULL && uhp == *uhpp)
1391 *uhpp = NULL;
1392
1393 for (uep = uhp->uh_entry; uep != NULL; uep = nuep)
1394 {
1395 nuep = uep->ue_next;
1396 u_freeentry(uep, uep->ue_size);
1397 }
1398
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001399 U_FREE_LINE((char_u *)uhp);
1400 --buf->b_u_numhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401}
1402
1403/*
1404 * free entry 'uep' and 'n' lines in uep->ue_array[]
1405 */
1406 static void
1407u_freeentry(uep, n)
1408 u_entry_T *uep;
1409 long n;
1410{
Bram Moolenaar8d343302005-07-12 22:46:17 +00001411 while (n > 0)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001412 U_FREE_LINE(uep->ue_array[--n]);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001413 U_FREE_LINE((char_u *)uep->ue_array);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001414 U_FREE_LINE((char_u *)uep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415}
1416
1417/*
1418 * invalidate the undo buffer; called when storage has already been released
1419 */
1420 void
1421u_clearall(buf)
1422 buf_T *buf;
1423{
1424 buf->b_u_newhead = buf->b_u_oldhead = buf->b_u_curhead = NULL;
1425 buf->b_u_synced = TRUE;
1426 buf->b_u_numhead = 0;
1427 buf->b_u_line_ptr = NULL;
1428 buf->b_u_line_lnum = 0;
1429}
1430
1431/*
1432 * save the line "lnum" for the "U" command
1433 */
1434 void
1435u_saveline(lnum)
1436 linenr_T lnum;
1437{
1438 if (lnum == curbuf->b_u_line_lnum) /* line is already saved */
1439 return;
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001440 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) /* should never happen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 return;
1442 u_clearline();
1443 curbuf->b_u_line_lnum = lnum;
1444 if (curwin->w_cursor.lnum == lnum)
1445 curbuf->b_u_line_colnr = curwin->w_cursor.col;
1446 else
1447 curbuf->b_u_line_colnr = 0;
1448 if ((curbuf->b_u_line_ptr = u_save_line(lnum)) == NULL)
1449 do_outofmem_msg((long_u)0);
1450}
1451
1452/*
1453 * clear the line saved for the "U" command
1454 * (this is used externally for crossing a line while in insert mode)
1455 */
1456 void
1457u_clearline()
1458{
1459 if (curbuf->b_u_line_ptr != NULL)
1460 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001461 U_FREE_LINE(curbuf->b_u_line_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 curbuf->b_u_line_ptr = NULL;
1463 curbuf->b_u_line_lnum = 0;
1464 }
1465}
1466
1467/*
1468 * Implementation of the "U" command.
1469 * Differentiation from vi: "U" can be undone with the next "U".
1470 * We also allow the cursor to be in another line.
1471 */
1472 void
1473u_undoline()
1474{
1475 colnr_T t;
1476 char_u *oldp;
1477
1478 if (undo_off)
1479 return;
1480
1481 if (curbuf->b_u_line_ptr == NULL ||
1482 curbuf->b_u_line_lnum > curbuf->b_ml.ml_line_count)
1483 {
1484 beep_flush();
1485 return;
1486 }
1487 /* first save the line for the 'u' command */
1488 if (u_savecommon(curbuf->b_u_line_lnum - 1,
1489 curbuf->b_u_line_lnum + 1, (linenr_T)0) == FAIL)
1490 return;
1491 oldp = u_save_line(curbuf->b_u_line_lnum);
1492 if (oldp == NULL)
1493 {
1494 do_outofmem_msg((long_u)0);
1495 return;
1496 }
1497 ml_replace(curbuf->b_u_line_lnum, curbuf->b_u_line_ptr, TRUE);
1498 changed_bytes(curbuf->b_u_line_lnum, 0);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001499 U_FREE_LINE(curbuf->b_u_line_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 curbuf->b_u_line_ptr = oldp;
1501
1502 t = curbuf->b_u_line_colnr;
1503 if (curwin->w_cursor.lnum == curbuf->b_u_line_lnum)
1504 curbuf->b_u_line_colnr = curwin->w_cursor.col;
1505 curwin->w_cursor.col = t;
1506 curwin->w_cursor.lnum = curbuf->b_u_line_lnum;
1507}
1508
1509/*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001510 * There are two implementations of the memory management for undo:
1511 * 1. Use the standard malloc()/free() functions.
1512 * This should be fast for allocating memory, but when a buffer is
1513 * abandoned every single allocated chunk must be freed, which may be slow.
1514 * 2. Allocate larger blocks of memory and keep track of chunks ourselves.
1515 * This is fast for abandoning, but the use of linked lists is slow for
1516 * finding a free chunk. Esp. when a lot of lines are changed or deleted.
1517 * A bit of profiling showed that the first method is faster, especially when
1518 * making a large number of changes, under the condition that malloc()/free()
1519 * is implemented efficiently.
1520 */
1521#ifdef U_USE_MALLOC
1522/*
1523 * Version of undo memory allocation using malloc()/free()
1524 *
1525 * U_FREE_LINE() and U_ALLOC_LINE() are macros that invoke vim_free() and
1526 * lalloc() directly.
1527 */
1528
1529/*
1530 * Free all allocated memory blocks for the buffer 'buf'.
1531 */
1532 void
1533u_blockfree(buf)
1534 buf_T *buf;
1535{
Bram Moolenaar1e607892006-03-13 22:15:53 +00001536 while (buf->b_u_oldhead != NULL)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001537 u_freeheader(buf, buf->b_u_oldhead, NULL);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001538 U_FREE_LINE(buf->b_u_line_ptr);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001539}
1540
1541#else
1542/*
1543 * Storage allocation for the undo lines and blocks of the current file.
1544 * Version where Vim keeps track of the available memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 */
1546
1547/*
1548 * Memory is allocated in relatively large blocks. These blocks are linked
1549 * in the allocated block list, headed by curbuf->b_block_head. They are all
1550 * freed when abandoning a file, so we don't have to free every single line.
1551 * The list is kept sorted on memory address.
1552 * block_alloc() allocates a block.
1553 * m_blockfree() frees all blocks.
1554 *
1555 * The available chunks of memory are kept in free chunk lists. There is
1556 * one free list for each block of allocated memory. The list is kept sorted
1557 * on memory address.
1558 * u_alloc_line() gets a chunk from the free lists.
1559 * u_free_line() returns a chunk to the free lists.
1560 * curbuf->b_m_search points to the chunk before the chunk that was
1561 * freed/allocated the last time.
1562 * curbuf->b_mb_current points to the b_head where curbuf->b_m_search
1563 * points into the free list.
1564 *
1565 *
1566 * b_block_head /---> block #1 /---> block #2
1567 * mb_next ---/ mb_next ---/ mb_next ---> NULL
1568 * mb_info mb_info mb_info
1569 * | | |
1570 * V V V
1571 * NULL free chunk #1.1 free chunk #2.1
1572 * | |
1573 * V V
1574 * free chunk #1.2 NULL
1575 * |
1576 * V
1577 * NULL
1578 *
1579 * When a single free chunk list would have been used, it could take a lot
1580 * of time in u_free_line() to find the correct place to insert a chunk in the
1581 * free list. The single free list would become very long when many lines are
1582 * changed (e.g. with :%s/^M$//).
1583 */
1584
1585 /*
1586 * this blocksize is used when allocating new lines
1587 */
1588#define MEMBLOCKSIZE 2044
1589
1590/*
1591 * The size field contains the size of the chunk, including the size field
1592 * itself.
1593 *
1594 * When the chunk is not in-use it is preceded with the m_info structure.
1595 * The m_next field links it in one of the free chunk lists.
1596 *
1597 * On most unix systems structures have to be longword (32 or 64 bit) aligned.
1598 * On most other systems they are short (16 bit) aligned.
1599 */
1600
1601/* the structure definitions are now in structs.h */
1602
1603#ifdef ALIGN_LONG
1604 /* size of m_size */
1605# define M_OFFSET (sizeof(long_u))
1606#else
1607 /* size of m_size */
1608# define M_OFFSET (sizeof(short_u))
1609#endif
1610
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001611static char_u *u_blockalloc __ARGS((long_u));
1612
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613/*
1614 * Allocate a block of memory and link it in the allocated block list.
1615 */
1616 static char_u *
1617u_blockalloc(size)
1618 long_u size;
1619{
1620 mblock_T *p;
1621 mblock_T *mp, *next;
1622
1623 p = (mblock_T *)lalloc(size + sizeof(mblock_T), FALSE);
1624 if (p != NULL)
1625 {
1626 /* Insert the block into the allocated block list, keeping it
1627 sorted on address. */
1628 for (mp = &curbuf->b_block_head;
1629 (next = mp->mb_next) != NULL && next < p;
1630 mp = next)
1631 ;
1632 p->mb_next = next; /* link in block list */
1633 p->mb_size = size;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001634 p->mb_maxsize = 0; /* nothing free yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 mp->mb_next = p;
1636 p->mb_info.m_next = NULL; /* clear free list */
1637 p->mb_info.m_size = 0;
1638 curbuf->b_mb_current = p; /* remember current block */
1639 curbuf->b_m_search = NULL;
1640 p++; /* return usable memory */
1641 }
1642 return (char_u *)p;
1643}
1644
1645/*
1646 * free all allocated memory blocks for the buffer 'buf'
1647 */
1648 void
1649u_blockfree(buf)
1650 buf_T *buf;
1651{
1652 mblock_T *p, *np;
1653
1654 for (p = buf->b_block_head.mb_next; p != NULL; p = np)
1655 {
1656 np = p->mb_next;
1657 vim_free(p);
1658 }
1659 buf->b_block_head.mb_next = NULL;
1660 buf->b_m_search = NULL;
1661 buf->b_mb_current = NULL;
1662}
1663
1664/*
1665 * Free a chunk of memory for the current buffer.
1666 * Insert the chunk into the correct free list, keeping it sorted on address.
1667 */
1668 static void
1669u_free_line(ptr, keep)
1670 char_u *ptr;
1671 int keep; /* don't free the block when it's empty */
1672{
1673 minfo_T *next;
1674 minfo_T *prev, *curr;
1675 minfo_T *mp;
1676 mblock_T *nextb;
1677 mblock_T *prevb;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001678 long_u maxsize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679
1680 if (ptr == NULL || ptr == IObuff)
1681 return; /* illegal address can happen in out-of-memory situations */
1682
1683 mp = (minfo_T *)(ptr - M_OFFSET);
1684
1685 /* find block where chunk could be a part off */
1686 /* if we change curbuf->b_mb_current, curbuf->b_m_search is set to NULL */
1687 if (curbuf->b_mb_current == NULL || mp < (minfo_T *)curbuf->b_mb_current)
1688 {
1689 curbuf->b_mb_current = curbuf->b_block_head.mb_next;
1690 curbuf->b_m_search = NULL;
1691 }
1692 if ((nextb = curbuf->b_mb_current->mb_next) != NULL
1693 && (minfo_T *)nextb < mp)
1694 {
1695 curbuf->b_mb_current = nextb;
1696 curbuf->b_m_search = NULL;
1697 }
1698 while ((nextb = curbuf->b_mb_current->mb_next) != NULL
1699 && (minfo_T *)nextb < mp)
1700 curbuf->b_mb_current = nextb;
1701
1702 curr = NULL;
1703 /*
1704 * If mp is smaller than curbuf->b_m_search->m_next go to the start of
1705 * the free list
1706 */
1707 if (curbuf->b_m_search == NULL || mp < (curbuf->b_m_search->m_next))
1708 next = &(curbuf->b_mb_current->mb_info);
1709 else
1710 next = curbuf->b_m_search;
1711 /*
1712 * The following loop is executed very often.
1713 * Therefore it has been optimized at the cost of readability.
1714 * Keep it fast!
1715 */
1716#ifdef SLOW_BUT_EASY_TO_READ
1717 do
1718 {
1719 prev = curr;
1720 curr = next;
1721 next = next->m_next;
1722 }
1723 while (mp > next && next != NULL);
1724#else
1725 do /* first, middle, last */
1726 {
1727 prev = next->m_next; /* curr, next, prev */
1728 if (prev == NULL || mp <= prev)
1729 {
1730 prev = curr;
1731 curr = next;
1732 next = next->m_next;
1733 break;
1734 }
1735 curr = prev->m_next; /* next, prev, curr */
1736 if (curr == NULL || mp <= curr)
1737 {
1738 prev = next;
1739 curr = prev->m_next;
1740 next = curr->m_next;
1741 break;
1742 }
1743 next = curr->m_next; /* prev, curr, next */
1744 }
1745 while (mp > next && next != NULL);
1746#endif
1747
1748 /* if *mp and *next are concatenated, join them into one chunk */
1749 if ((char_u *)mp + mp->m_size == (char_u *)next)
1750 {
1751 mp->m_size += next->m_size;
1752 mp->m_next = next->m_next;
1753 }
1754 else
1755 mp->m_next = next;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001756 maxsize = mp->m_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757
1758 /* if *curr and *mp are concatenated, join them */
1759 if (prev != NULL && (char_u *)curr + curr->m_size == (char_u *)mp)
1760 {
1761 curr->m_size += mp->m_size;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001762 maxsize = curr->m_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 curr->m_next = mp->m_next;
1764 curbuf->b_m_search = prev;
1765 }
1766 else
1767 {
1768 curr->m_next = mp;
1769 curbuf->b_m_search = curr; /* put curbuf->b_m_search before freed
1770 chunk */
1771 }
1772
1773 /*
1774 * If the block only containes free memory now, release it.
1775 */
1776 if (!keep && curbuf->b_mb_current->mb_size
1777 == curbuf->b_mb_current->mb_info.m_next->m_size)
1778 {
1779 /* Find the block before the current one to be able to unlink it from
1780 * the list of blocks. */
1781 prevb = &curbuf->b_block_head;
1782 for (nextb = prevb->mb_next; nextb != curbuf->b_mb_current;
1783 nextb = nextb->mb_next)
1784 prevb = nextb;
1785 prevb->mb_next = nextb->mb_next;
1786 vim_free(nextb);
1787 curbuf->b_mb_current = NULL;
1788 curbuf->b_m_search = NULL;
1789 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001790 else if (curbuf->b_mb_current->mb_maxsize < maxsize)
1791 curbuf->b_mb_current->mb_maxsize = maxsize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792}
1793
1794/*
1795 * Allocate and initialize a new line structure with room for at least
1796 * 'size' characters plus a terminating NUL.
1797 */
1798 static char_u *
1799u_alloc_line(size)
1800 unsigned size;
1801{
1802 minfo_T *mp, *mprev, *mp2;
1803 mblock_T *mbp;
1804 int size_align;
1805
1806 /*
1807 * Add room for size field and trailing NUL byte.
1808 * Adjust for minimal size (must be able to store minfo_T
1809 * plus a trailing NUL, so the chunk can be released again)
1810 */
1811 size += M_OFFSET + 1;
1812 if (size < sizeof(minfo_T) + 1)
1813 size = sizeof(minfo_T) + 1;
1814
1815 /*
1816 * round size up for alignment
1817 */
1818 size_align = (size + ALIGN_MASK) & ~ALIGN_MASK;
1819
1820 /*
1821 * If curbuf->b_m_search is NULL (uninitialized free list) start at
1822 * curbuf->b_block_head
1823 */
1824 if (curbuf->b_mb_current == NULL || curbuf->b_m_search == NULL)
1825 {
1826 curbuf->b_mb_current = &curbuf->b_block_head;
1827 curbuf->b_m_search = &(curbuf->b_block_head.mb_info);
1828 }
1829
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001830 /* Search for a block with enough space. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 mbp = curbuf->b_mb_current;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001832 while (mbp->mb_maxsize < size_align)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001834 if (mbp->mb_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 mbp = mbp->mb_next;
1836 else
1837 mbp = &curbuf->b_block_head;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001838 if (mbp == curbuf->b_mb_current)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001840 int n = (size_align > (MEMBLOCKSIZE / 4)
1841 ? size_align : MEMBLOCKSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001843 /* Back where we started in block list: need to add a new block
1844 * with enough space. */
1845 mp = (minfo_T *)u_blockalloc((long_u)n);
1846 if (mp == NULL)
1847 return (NULL);
1848 mp->m_size = n;
1849 u_free_line((char_u *)mp + M_OFFSET, TRUE);
1850 mbp = curbuf->b_mb_current;
1851 break;
1852 }
1853 }
1854 if (mbp != curbuf->b_mb_current)
1855 curbuf->b_m_search = &(mbp->mb_info);
1856
1857 /* In this block find a chunk with enough space. */
1858 mprev = curbuf->b_m_search;
1859 mp = curbuf->b_m_search->m_next;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00001860 for (;;)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001861 {
1862 if (mp == NULL) /* at end of the list */
1863 mp = &(mbp->mb_info); /* wrap around to begin */
1864 if (mp->m_size >= size)
1865 break;
1866 if (mp == curbuf->b_m_search)
1867 {
1868 /* back where we started in free chunk list: "cannot happen" */
1869 EMSG2(_(e_intern2), "u_alloc_line()");
1870 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 }
1872 mprev = mp;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001873 mp = mp->m_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 }
1875
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001876 /* when using the largest chunk adjust mb_maxsize */
1877 if (mp->m_size >= mbp->mb_maxsize)
1878 mbp->mb_maxsize = 0;
1879
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 /* if the chunk we found is large enough, split it up in two */
1881 if ((long)mp->m_size - size_align >= (long)(sizeof(minfo_T) + 1))
1882 {
1883 mp2 = (minfo_T *)((char_u *)mp + size_align);
1884 mp2->m_size = mp->m_size - size_align;
1885 mp2->m_next = mp->m_next;
1886 mprev->m_next = mp2;
1887 mp->m_size = size_align;
1888 }
1889 else /* remove *mp from the free list */
1890 {
1891 mprev->m_next = mp->m_next;
1892 }
1893 curbuf->b_m_search = mprev;
1894 curbuf->b_mb_current = mbp;
1895
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001896 /* If using the largest chunk need to find the new largest chunk */
1897 if (mbp->mb_maxsize == 0)
1898 for (mp2 = &(mbp->mb_info); mp2 != NULL; mp2 = mp2->m_next)
1899 if (mbp->mb_maxsize < mp2->m_size)
1900 mbp->mb_maxsize = mp2->m_size;
1901
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 mp = (minfo_T *)((char_u *)mp + M_OFFSET);
1903 *(char_u *)mp = NUL; /* set the first byte to NUL */
1904
1905 return ((char_u *)mp);
1906}
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908
1909/*
1910 * u_save_line(): allocate memory with u_alloc_line() and copy line 'lnum'
1911 * into it.
1912 */
1913 static char_u *
1914u_save_line(lnum)
1915 linenr_T lnum;
1916{
1917 char_u *src;
1918 char_u *dst;
1919 unsigned len;
1920
1921 src = ml_get(lnum);
1922 len = (unsigned)STRLEN(src);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001923 if ((dst = U_ALLOC_LINE(len)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 mch_memmove(dst, src, (size_t)(len + 1));
1925 return (dst);
1926}
1927
1928/*
1929 * Check if the 'modified' flag is set, or 'ff' has changed (only need to
1930 * check the first character, because it can only be "dos", "unix" or "mac").
1931 * "nofile" and "scratch" type buffers are considered to always be unchanged.
1932 */
1933 int
1934bufIsChanged(buf)
1935 buf_T *buf;
1936{
1937 return
1938#ifdef FEAT_QUICKFIX
1939 !bt_dontwrite(buf) &&
1940#endif
1941 (buf->b_changed || file_ff_differs(buf));
1942}
1943
1944 int
1945curbufIsChanged()
1946{
1947 return
1948#ifdef FEAT_QUICKFIX
1949 !bt_dontwrite(curbuf) &&
1950#endif
1951 (curbuf->b_changed || file_ff_differs(curbuf));
1952}