blob: d56302d626ac2c845406fd0aa718de7b52b67302 [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));
87static int u_savecommon __ARGS((linenr_T, linenr_T, linenr_T));
88static void u_doit __ARGS((int count));
Bram Moolenaarca003e12006-03-17 23:19:38 +000089static void u_undoredo __ARGS((int undo));
Bram Moolenaardb552d602006-03-23 22:59:57 +000090static void u_undo_end __ARGS((int did_undo, int absolute));
Bram Moolenaarefd2bf12006-03-16 21:41:35 +000091static void u_add_time __ARGS((char_u *buf, size_t buflen, time_t tt));
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 */
Bram Moolenaarce6ef252006-07-12 19:49:41 +0000198 int
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000199undo_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
Bram Moolenaarca003e12006-03-17 23:19:38 +0000352 uhp->uh_seq = ++curbuf->b_u_seq_last;
353 curbuf->b_u_seq_cur = uhp->uh_seq;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000354 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 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000553 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 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
Bram Moolenaarca003e12006-03-17 23:19:38 +0000581u_doit(startcount)
582 int startcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583{
Bram Moolenaarca003e12006-03-17 23:19:38 +0000584 int count = startcount;
585
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000586 if (!undo_allowed())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588
589 u_newcount = 0;
590 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000591 if (curbuf->b_ml.ml_flags & ML_EMPTY)
592 u_oldcount = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 while (count--)
594 {
595 if (undo_undoes)
596 {
597 if (curbuf->b_u_curhead == NULL) /* first undo */
598 curbuf->b_u_curhead = curbuf->b_u_newhead;
599 else if (p_ul > 0) /* multi level undo */
600 /* get next undo */
601 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_next;
602 /* nothing to undo */
603 if (curbuf->b_u_numhead == 0 || curbuf->b_u_curhead == NULL)
604 {
605 /* stick curbuf->b_u_curhead at end */
606 curbuf->b_u_curhead = curbuf->b_u_oldhead;
607 beep_flush();
Bram Moolenaarca003e12006-03-17 23:19:38 +0000608 if (count == startcount - 1)
609 {
610 MSG(_("Already at oldest change"));
611 return;
612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 break;
614 }
615
Bram Moolenaarca003e12006-03-17 23:19:38 +0000616 u_undoredo(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 }
618 else
619 {
620 if (curbuf->b_u_curhead == NULL || p_ul <= 0)
621 {
622 beep_flush(); /* nothing to redo */
Bram Moolenaarca003e12006-03-17 23:19:38 +0000623 if (count == startcount - 1)
624 {
625 MSG(_("Already at newest change"));
626 return;
627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 break;
629 }
630
Bram Moolenaarca003e12006-03-17 23:19:38 +0000631 u_undoredo(FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000632
633 /* Advance for next redo. Set "newhead" when at the end of the
634 * redoable changes. */
635 if (curbuf->b_u_curhead->uh_prev == NULL)
636 curbuf->b_u_newhead = curbuf->b_u_curhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_prev;
638 }
639 }
Bram Moolenaardb552d602006-03-23 22:59:57 +0000640 u_undo_end(undo_undoes, FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000641}
642
643static int lastmark = 0;
644
645/*
646 * Undo or redo over the timeline.
647 * When "step" is negative go back in time, otherwise goes forward in time.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000648 * When "sec" is FALSE make "step" steps, when "sec" is TRUE use "step" as
649 * seconds.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000650 * When "absolute" is TRUE use "step" as the sequence number to jump to.
651 * "sec" must be FALSE then.
Bram Moolenaar1e607892006-03-13 22:15:53 +0000652 */
653 void
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000654undo_time(step, sec, absolute)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000655 long step;
656 int sec;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000657 int absolute;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000658{
659 long target;
660 long closest;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000661 long closest_start;
662 long closest_seq = 0;
663 long val;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000664 u_header_T *uhp;
665 u_header_T *last;
666 int mark;
667 int nomark;
668 int round;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000669 int dosec = sec;
670 int above = FALSE;
Bram Moolenaar433f7c82006-03-21 21:29:36 +0000671 int did_undo = TRUE;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000672
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000673 /* First make sure the current undoable change is synced. */
674 if (curbuf->b_u_synced == FALSE)
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000675 u_sync(TRUE);
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000676
Bram Moolenaar1e607892006-03-13 22:15:53 +0000677 u_newcount = 0;
678 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000679 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar1e607892006-03-13 22:15:53 +0000680 u_oldcount = -1;
681
Bram Moolenaarca003e12006-03-17 23:19:38 +0000682 /* "target" is the node below which we want to be.
683 * Init "closest" to a value we can't reach. */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000684 if (absolute)
685 {
686 target = step;
Bram Moolenaarca003e12006-03-17 23:19:38 +0000687 closest = -1;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000688 }
Bram Moolenaarca003e12006-03-17 23:19:38 +0000689 else
Bram Moolenaar1e607892006-03-13 22:15:53 +0000690 {
Bram Moolenaarca003e12006-03-17 23:19:38 +0000691 /* When doing computations with time_t subtract starttime, because
692 * time_t converted to a long may result in a wrong number. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000693 if (sec)
Bram Moolenaarca003e12006-03-17 23:19:38 +0000694 target = (long)(curbuf->b_u_seq_time - starttime) + step;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000695 else
696 target = curbuf->b_u_seq_cur + step;
Bram Moolenaarca003e12006-03-17 23:19:38 +0000697 if (step < 0)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000698 {
Bram Moolenaarca003e12006-03-17 23:19:38 +0000699 if (target < 0)
700 target = 0;
701 closest = -1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000702 }
703 else
704 {
Bram Moolenaarca003e12006-03-17 23:19:38 +0000705 if (sec)
Bram Moolenaardb552d602006-03-23 22:59:57 +0000706 closest = (long)(time(NULL) - starttime + 1);
Bram Moolenaarca003e12006-03-17 23:19:38 +0000707 else
708 closest = curbuf->b_u_seq_last + 2;
709 if (target >= closest)
710 target = closest - 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000711 }
Bram Moolenaar1e607892006-03-13 22:15:53 +0000712 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000713 closest_start = closest;
Bram Moolenaarca003e12006-03-17 23:19:38 +0000714 closest_seq = curbuf->b_u_seq_cur;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000715
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000716 /*
717 * May do this twice:
Bram Moolenaar1e607892006-03-13 22:15:53 +0000718 * 1. Search for "target", update "closest" to the best match found.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000719 * 2. If "target" not found search for "closest".
720 *
721 * When using the closest time we use the sequence number in the second
722 * round, because there may be several entries with the same time.
723 */
Bram Moolenaar1e607892006-03-13 22:15:53 +0000724 for (round = 1; round <= 2; ++round)
725 {
726 /* Find the path from the current state to where we want to go. The
727 * desired state can be anywhere in the undo tree, need to go all over
728 * it. We put "nomark" in uh_walk where we have been without success,
729 * "mark" where it could possibly be. */
730 mark = ++lastmark;
731 nomark = ++lastmark;
732
733 if (curbuf->b_u_curhead == NULL) /* at leaf of the tree */
734 uhp = curbuf->b_u_newhead;
735 else
736 uhp = curbuf->b_u_curhead;
737
738 while (uhp != NULL)
739 {
740 uhp->uh_walk = mark;
Bram Moolenaardb552d602006-03-23 22:59:57 +0000741 val = (long)(dosec ? (uhp->uh_time - starttime) : uhp->uh_seq);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000742
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000743 if (round == 1)
744 {
745 /* Remember the header that is closest to the target.
746 * It must be at least in the right direction (checked with
Bram Moolenaarca003e12006-03-17 23:19:38 +0000747 * "b_u_seq_cur"). When the timestamp is equal find the
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000748 * highest/lowest sequence number. */
Bram Moolenaarca003e12006-03-17 23:19:38 +0000749 if ((step < 0 ? uhp->uh_seq <= curbuf->b_u_seq_cur
750 : uhp->uh_seq > curbuf->b_u_seq_cur)
751 && ((dosec && val == closest)
752 ? (step < 0
753 ? uhp->uh_seq < closest_seq
754 : uhp->uh_seq > closest_seq)
755 : closest == closest_start
756 || (val > target
757 ? (closest > target
758 ? val - target <= closest - target
759 : val - target <= target - closest)
760 : (closest > target
761 ? target - val <= closest - target
762 : target - val <= target - closest))))
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000763 {
764 closest = val;
765 closest_seq = uhp->uh_seq;
766 }
767 }
768
769 /* Quit searching when we found a match. But when searching for a
770 * time we need to continue looking for the best uh_seq. */
771 if (target == val && !dosec)
772 break;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000773
774 /* go down in the tree if we haven't been there */
775 if (uhp->uh_prev != NULL && uhp->uh_prev->uh_walk != nomark
776 && uhp->uh_prev->uh_walk != mark)
777 uhp = uhp->uh_prev;
778
779 /* go to alternate branch if we haven't been there */
780 else if (uhp->uh_alt_next != NULL
781 && uhp->uh_alt_next->uh_walk != nomark
782 && uhp->uh_alt_next->uh_walk != mark)
783 uhp = uhp->uh_alt_next;
784
785 /* go up in the tree if we haven't been there and we are at the
786 * start of alternate branches */
787 else if (uhp->uh_next != NULL && uhp->uh_alt_prev == NULL
788 && uhp->uh_next->uh_walk != nomark
789 && uhp->uh_next->uh_walk != mark)
Bram Moolenaardb552d602006-03-23 22:59:57 +0000790 {
791 /* If still at the start we don't go through this change. */
792 if (uhp == curbuf->b_u_curhead)
793 uhp->uh_walk = nomark;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000794 uhp = uhp->uh_next;
Bram Moolenaardb552d602006-03-23 22:59:57 +0000795 }
Bram Moolenaar1e607892006-03-13 22:15:53 +0000796
797 else
798 {
799 /* need to backtrack; mark this node as useless */
800 uhp->uh_walk = nomark;
801 if (uhp->uh_alt_prev != NULL)
802 uhp = uhp->uh_alt_prev;
803 else
804 uhp = uhp->uh_next;
805 }
806 }
807
808 if (uhp != NULL) /* found it */
809 break;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000810
811 if (absolute)
812 {
813 EMSGN(_("Undo number %ld not found"), step);
814 return;
815 }
816
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000817 if (closest == closest_start)
Bram Moolenaar1e607892006-03-13 22:15:53 +0000818 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000819 if (step < 0)
820 MSG(_("Already at oldest change"));
821 else
822 MSG(_("Already at newest change"));
Bram Moolenaar1e607892006-03-13 22:15:53 +0000823 return;
824 }
825
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000826 target = closest_seq;
827 dosec = FALSE;
828 if (step < 0)
829 above = TRUE; /* stop above the header */
Bram Moolenaar1e607892006-03-13 22:15:53 +0000830 }
831
832 /* If we found it: Follow the path to go to where we want to be. */
833 if (uhp != NULL)
834 {
835 /*
836 * First go up the tree as much as needed.
837 */
838 for (;;)
839 {
840 uhp = curbuf->b_u_curhead;
841 if (uhp == NULL)
842 uhp = curbuf->b_u_newhead;
843 else
Bram Moolenaar1e607892006-03-13 22:15:53 +0000844 uhp = uhp->uh_next;
Bram Moolenaarca003e12006-03-17 23:19:38 +0000845 if (uhp == NULL || uhp->uh_walk != mark
846 || (uhp->uh_seq == target && !above))
Bram Moolenaar1e607892006-03-13 22:15:53 +0000847 break;
848 curbuf->b_u_curhead = uhp;
Bram Moolenaarca003e12006-03-17 23:19:38 +0000849 u_undoredo(TRUE);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000850 uhp->uh_walk = nomark; /* don't go back down here */
Bram Moolenaar1e607892006-03-13 22:15:53 +0000851 }
852
853 /*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000854 * And now go down the tree (redo), branching off where needed.
Bram Moolenaar1e607892006-03-13 22:15:53 +0000855 */
856 uhp = curbuf->b_u_curhead;
Bram Moolenaarca003e12006-03-17 23:19:38 +0000857 while (uhp != NULL)
Bram Moolenaar1e607892006-03-13 22:15:53 +0000858 {
859 /* Find the last branch with a mark, that's the one. */
860 last = uhp;
861 while (last->uh_alt_next != NULL
862 && last->uh_alt_next->uh_walk == mark)
863 last = last->uh_alt_next;
864 if (last != uhp)
865 {
866 /* Make the used branch the first entry in the list of
867 * alternatives to make "u" and CTRL-R take this branch. */
868 if (last->uh_alt_next != NULL)
869 last->uh_alt_next->uh_alt_prev = last->uh_alt_prev;
870 last->uh_alt_prev->uh_alt_next = last->uh_alt_next;
871 last->uh_alt_prev = NULL;
872 last->uh_alt_next = uhp;
873 uhp->uh_alt_prev = last;
874
875 uhp = last;
Bram Moolenaarca003e12006-03-17 23:19:38 +0000876 if (uhp->uh_next != NULL)
877 uhp->uh_next->uh_prev = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000878 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000879 curbuf->b_u_curhead = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000880
881 if (uhp->uh_walk != mark)
882 break; /* must have reached the target */
883
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000884 /* Stop when going backwards in time and didn't find the exact
885 * header we were looking for. */
886 if (uhp->uh_seq == target && above)
Bram Moolenaardb552d602006-03-23 22:59:57 +0000887 {
888 curbuf->b_u_seq_cur = target - 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000889 break;
Bram Moolenaardb552d602006-03-23 22:59:57 +0000890 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000891
Bram Moolenaarca003e12006-03-17 23:19:38 +0000892 u_undoredo(FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000893
894 /* Advance "curhead" to below the header we last used. If it
895 * becomes NULL then we need to set "newhead" to this leaf. */
896 if (uhp->uh_prev == NULL)
897 curbuf->b_u_newhead = uhp;
898 curbuf->b_u_curhead = uhp->uh_prev;
Bram Moolenaar433f7c82006-03-21 21:29:36 +0000899 did_undo = FALSE;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000900
901 if (uhp->uh_seq == target) /* found it! */
902 break;
903
904 uhp = uhp->uh_prev;
905 if (uhp == NULL || uhp->uh_walk != mark)
906 {
Bram Moolenaarca003e12006-03-17 23:19:38 +0000907 /* Need to redo more but can't find it... */
Bram Moolenaar1e607892006-03-13 22:15:53 +0000908 EMSG2(_(e_intern2), "undo_time()");
909 break;
910 }
911 }
912 }
Bram Moolenaardb552d602006-03-23 22:59:57 +0000913 u_undo_end(did_undo, absolute);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914}
915
916/*
917 * u_undoredo: common code for undo and redo
918 *
919 * The lines in the file are replaced by the lines in the entry list at
920 * curbuf->b_u_curhead. The replaced lines in the file are saved in the entry
921 * list for the next undo/redo.
Bram Moolenaarca003e12006-03-17 23:19:38 +0000922 *
923 * When "undo" is TRUE we go up in the tree, when FALSE we go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 */
925 static void
Bram Moolenaarca003e12006-03-17 23:19:38 +0000926u_undoredo(undo)
927 int undo;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928{
929 char_u **newarray = NULL;
930 linenr_T oldsize;
931 linenr_T newsize;
932 linenr_T top, bot;
933 linenr_T lnum;
934 linenr_T newlnum = MAXLNUM;
935 long i;
936 u_entry_T *uep, *nuep;
937 u_entry_T *newlist = NULL;
938 int old_flags;
939 int new_flags;
940 pos_T namedm[NMARKS];
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000941#ifdef FEAT_VISUAL
942 visualinfo_T visualinfo;
943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 int empty_buffer; /* buffer became empty */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000945 u_header_T *curhead = curbuf->b_u_curhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000947 old_flags = curhead->uh_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 new_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
949 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
950 setpcmark();
951
952 /*
953 * save marks before undo/redo
954 */
955 mch_memmove(namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000956#ifdef FEAT_VISUAL
957 visualinfo = curbuf->b_visual;
958#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 curbuf->b_op_start.lnum = curbuf->b_ml.ml_line_count;
960 curbuf->b_op_start.col = 0;
961 curbuf->b_op_end.lnum = 0;
962 curbuf->b_op_end.col = 0;
963
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000964 for (uep = curhead->uh_entry; uep != NULL; uep = nuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 {
966 top = uep->ue_top;
967 bot = uep->ue_bot;
968 if (bot == 0)
969 bot = curbuf->b_ml.ml_line_count + 1;
970 if (top > curbuf->b_ml.ml_line_count || top >= bot
971 || bot > curbuf->b_ml.ml_line_count + 1)
972 {
973 EMSG(_("E438: u_undo: line numbers wrong"));
974 changed(); /* don't want UNCHANGED now */
975 return;
976 }
977
978 oldsize = bot - top - 1; /* number of lines before undo */
979 newsize = uep->ue_size; /* number of lines after undo */
980
981 if (top < newlnum)
982 {
983 /* If the saved cursor is somewhere in this undo block, move it to
984 * the remembered position. Makes "gwap" put the cursor back
985 * where it was. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000986 lnum = curhead->uh_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 if (lnum >= top && lnum <= top + newsize + 1)
988 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000989 curwin->w_cursor = curhead->uh_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 newlnum = curwin->w_cursor.lnum - 1;
991 }
992 else
993 {
994 /* Use the first line that actually changed. Avoids that
995 * undoing auto-formatting puts the cursor in the previous
996 * line. */
997 for (i = 0; i < newsize && i < oldsize; ++i)
998 if (STRCMP(uep->ue_array[i], ml_get(top + 1 + i)) != 0)
999 break;
1000 if (i == newsize && newlnum == MAXLNUM && uep->ue_next == NULL)
1001 {
1002 newlnum = top;
1003 curwin->w_cursor.lnum = newlnum + 1;
1004 }
1005 else if (i < newsize)
1006 {
1007 newlnum = top + i;
1008 curwin->w_cursor.lnum = newlnum + 1;
1009 }
1010 }
1011 }
1012
1013 empty_buffer = FALSE;
1014
1015 /* delete the lines between top and bot and save them in newarray */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001016 if (oldsize > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001018 if ((newarray = (char_u **)U_ALLOC_LINE(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 (unsigned)(sizeof(char_u *) * oldsize))) == NULL)
1020 {
1021 do_outofmem_msg((long_u)(sizeof(char_u *) * oldsize));
1022 /*
1023 * We have messed up the entry list, repair is impossible.
1024 * we have to free the rest of the list.
1025 */
1026 while (uep != NULL)
1027 {
1028 nuep = uep->ue_next;
1029 u_freeentry(uep, uep->ue_size);
1030 uep = nuep;
1031 }
1032 break;
1033 }
1034 /* delete backwards, it goes faster in most cases */
1035 for (lnum = bot - 1, i = oldsize; --i >= 0; --lnum)
1036 {
1037 /* what can we do when we run out of memory? */
1038 if ((newarray[i] = u_save_line(lnum)) == NULL)
1039 do_outofmem_msg((long_u)0);
1040 /* remember we deleted the last line in the buffer, and a
1041 * dummy empty line will be inserted */
1042 if (curbuf->b_ml.ml_line_count == 1)
1043 empty_buffer = TRUE;
1044 ml_delete(lnum, FALSE);
1045 }
1046 }
Bram Moolenaar8d343302005-07-12 22:46:17 +00001047 else
1048 newarray = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049
1050 /* insert the lines in u_array between top and bot */
1051 if (newsize)
1052 {
1053 for (lnum = top, i = 0; i < newsize; ++i, ++lnum)
1054 {
1055 /*
1056 * If the file is empty, there is an empty line 1 that we
1057 * should get rid of, by replacing it with the new line
1058 */
1059 if (empty_buffer && lnum == 0)
1060 ml_replace((linenr_T)1, uep->ue_array[i], TRUE);
1061 else
1062 ml_append(lnum, uep->ue_array[i], (colnr_T)0, FALSE);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001063 U_FREE_LINE(uep->ue_array[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001065 U_FREE_LINE((char_u *)uep->ue_array);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 }
1067
1068 /* adjust marks */
1069 if (oldsize != newsize)
1070 {
1071 mark_adjust(top + 1, top + oldsize, (long)MAXLNUM,
1072 (long)newsize - (long)oldsize);
1073 if (curbuf->b_op_start.lnum > top + oldsize)
1074 curbuf->b_op_start.lnum += newsize - oldsize;
1075 if (curbuf->b_op_end.lnum > top + oldsize)
1076 curbuf->b_op_end.lnum += newsize - oldsize;
1077 }
1078
1079 changed_lines(top + 1, 0, bot, newsize - oldsize);
1080
1081 /* set '[ and '] mark */
1082 if (top + 1 < curbuf->b_op_start.lnum)
1083 curbuf->b_op_start.lnum = top + 1;
1084 if (newsize == 0 && top + 1 > curbuf->b_op_end.lnum)
1085 curbuf->b_op_end.lnum = top + 1;
1086 else if (top + newsize > curbuf->b_op_end.lnum)
1087 curbuf->b_op_end.lnum = top + newsize;
1088
1089 u_newcount += newsize;
1090 u_oldcount += oldsize;
1091 uep->ue_size = oldsize;
1092 uep->ue_array = newarray;
1093 uep->ue_bot = top + newsize + 1;
1094
1095 /*
1096 * insert this entry in front of the new entry list
1097 */
1098 nuep = uep->ue_next;
1099 uep->ue_next = newlist;
1100 newlist = uep;
1101 }
1102
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001103 curhead->uh_entry = newlist;
1104 curhead->uh_flags = new_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 if ((old_flags & UH_EMPTYBUF) && bufempty())
1106 curbuf->b_ml.ml_flags |= ML_EMPTY;
1107 if (old_flags & UH_CHANGED)
1108 changed();
1109 else
Bram Moolenaar009b2592004-10-24 19:18:58 +00001110#ifdef FEAT_NETBEANS_INTG
1111 /* per netbeans undo rules, keep it as modified */
1112 if (!isNetbeansModified(curbuf))
1113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 unchanged(curbuf, FALSE);
1115
1116 /*
1117 * restore marks from before undo/redo
1118 */
1119 for (i = 0; i < NMARKS; ++i)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001120 if (curhead->uh_namedm[i].lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001122 curbuf->b_namedm[i] = curhead->uh_namedm[i];
1123 curhead->uh_namedm[i] = namedm[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001125#ifdef FEAT_VISUAL
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001126 if (curhead->uh_visual.vi_start.lnum != 0)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001127 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001128 curbuf->b_visual = curhead->uh_visual;
1129 curhead->uh_visual = visualinfo;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001130 }
1131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132
1133 /*
1134 * If the cursor is only off by one line, put it at the same position as
1135 * before starting the change (for the "o" command).
1136 * Otherwise the cursor should go to the first undone line.
1137 */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001138 if (curhead->uh_cursor.lnum + 1 == curwin->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 && curwin->w_cursor.lnum > 1)
1140 --curwin->w_cursor.lnum;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001141 if (curhead->uh_cursor.lnum == curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001143 curwin->w_cursor.col = curhead->uh_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001145 if (virtual_active() && curhead->uh_cursor_vcol >= 0)
1146 coladvance((colnr_T)curhead->uh_cursor_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 else
1148 curwin->w_cursor.coladd = 0;
1149#endif
1150 }
1151 else if (curwin->w_cursor.lnum <= curbuf->b_ml.ml_line_count)
1152 beginline(BL_SOL | BL_FIX);
1153 else
1154 {
1155 /* We get here with the current cursor line being past the end (eg
1156 * after adding lines at the end of the file, and then undoing it).
1157 * check_cursor() will move the cursor to the last line. Move it to
1158 * the first column here. */
1159 curwin->w_cursor.col = 0;
1160#ifdef FEAT_VIRTUALEDIT
1161 curwin->w_cursor.coladd = 0;
1162#endif
1163 }
1164
1165 /* Make sure the cursor is on an existing line and column. */
1166 check_cursor();
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001167
1168 /* Remember where we are for "g-" and ":earlier 10s". */
1169 curbuf->b_u_seq_cur = curhead->uh_seq;
Bram Moolenaarca003e12006-03-17 23:19:38 +00001170 if (undo)
1171 /* We are below the previous undo. However, to make ":earlier 1s"
1172 * work we compute this as being just above the just undone change. */
1173 --curbuf->b_u_seq_cur;
1174
1175 /* The timestamp can be the same for multiple changes, just use the one of
1176 * the undone/redone change. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001177 curbuf->b_u_seq_time = curhead->uh_time;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178}
1179
1180/*
1181 * If we deleted or added lines, report the number of less/more lines.
1182 * Otherwise, report the number of changes (this may be incorrect
1183 * in some cases, but it's better than nothing).
1184 */
1185 static void
Bram Moolenaardb552d602006-03-23 22:59:57 +00001186u_undo_end(did_undo, absolute)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001187 int did_undo; /* just did an undo */
Bram Moolenaardb552d602006-03-23 22:59:57 +00001188 int absolute; /* used ":undo N" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189{
Bram Moolenaar89d40322006-08-29 15:30:07 +00001190 char *msgstr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001191 u_header_T *uhp;
1192 char_u msgbuf[80];
Bram Moolenaar1e607892006-03-13 22:15:53 +00001193
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194#ifdef FEAT_FOLDING
1195 if ((fdo_flags & FDO_UNDO) && KeyTyped)
1196 foldOpenCursor();
1197#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +00001198
1199 if (global_busy /* no messages now, wait until global is finished */
1200 || !messaging()) /* 'lazyredraw' set, don't do messages now */
1201 return;
1202
1203 if (curbuf->b_ml.ml_flags & ML_EMPTY)
1204 --u_newcount;
1205
1206 u_oldcount -= u_newcount;
1207 if (u_oldcount == -1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00001208 msgstr = N_("more line");
Bram Moolenaar1e607892006-03-13 22:15:53 +00001209 else if (u_oldcount < 0)
Bram Moolenaar89d40322006-08-29 15:30:07 +00001210 msgstr = N_("more lines");
Bram Moolenaar1e607892006-03-13 22:15:53 +00001211 else if (u_oldcount == 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00001212 msgstr = N_("line less");
Bram Moolenaar1e607892006-03-13 22:15:53 +00001213 else if (u_oldcount > 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00001214 msgstr = N_("fewer lines");
Bram Moolenaar1e607892006-03-13 22:15:53 +00001215 else
1216 {
1217 u_oldcount = u_newcount;
1218 if (u_newcount == 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00001219 msgstr = N_("change");
Bram Moolenaar1e607892006-03-13 22:15:53 +00001220 else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001221 msgstr = N_("changes");
Bram Moolenaar1e607892006-03-13 22:15:53 +00001222 }
1223
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001224 if (curbuf->b_u_curhead != NULL)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001225 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00001226 /* For ":undo N" we prefer a "after #N" message. */
1227 if (absolute && curbuf->b_u_curhead->uh_next != NULL)
1228 {
1229 uhp = curbuf->b_u_curhead->uh_next;
1230 did_undo = FALSE;
1231 }
1232 else if (did_undo)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001233 uhp = curbuf->b_u_curhead;
1234 else
1235 uhp = curbuf->b_u_curhead->uh_next;
1236 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00001237 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001238 uhp = curbuf->b_u_newhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001239
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001240 if (uhp == NULL)
1241 *msgbuf = NUL;
1242 else
1243 u_add_time(msgbuf, sizeof(msgbuf), uhp->uh_time);
1244
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001245 smsg((char_u *)_("%ld %s; %s #%ld %s"),
Bram Moolenaarca003e12006-03-17 23:19:38 +00001246 u_oldcount < 0 ? -u_oldcount : u_oldcount,
Bram Moolenaar89d40322006-08-29 15:30:07 +00001247 _(msgstr),
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001248 did_undo ? _("before") : _("after"),
1249 uhp == NULL ? 0L : uhp->uh_seq,
1250 msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251}
1252
1253/*
1254 * u_sync: stop adding to the current entry list
1255 */
1256 void
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001257u_sync(force)
1258 int force; /* Also sync when no_u_sync is set. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259{
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001260 /* Skip it when already synced or syncing is disabled. */
1261 if (curbuf->b_u_synced || (!force && no_u_sync > 0))
1262 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
1264 if (im_is_preediting())
1265 return; /* XIM is busy, don't break an undo sequence */
1266#endif
1267 if (p_ul < 0)
1268 curbuf->b_u_synced = TRUE; /* no entries, nothing to do */
1269 else
1270 {
1271 u_getbot(); /* compute ue_bot of previous u_save */
1272 curbuf->b_u_curhead = NULL;
1273 }
1274}
1275
1276/*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001277 * ":undolist": List the leafs of the undo tree
1278 */
1279/*ARGSUSED*/
1280 void
1281ex_undolist(eap)
1282 exarg_T *eap;
1283{
1284 garray_T ga;
1285 u_header_T *uhp;
1286 int mark;
1287 int nomark;
1288 int changes = 1;
1289 int i;
1290
1291 /*
1292 * 1: walk the tree to find all leafs, put the info in "ga".
1293 * 2: sort the lines
1294 * 3: display the list
1295 */
1296 mark = ++lastmark;
1297 nomark = ++lastmark;
1298 ga_init2(&ga, (int)sizeof(char *), 20);
1299
1300 uhp = curbuf->b_u_oldhead;
1301 while (uhp != NULL)
1302 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00001303 if (uhp->uh_prev == NULL && uhp->uh_walk != nomark
1304 && uhp->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001305 {
1306 if (ga_grow(&ga, 1) == FAIL)
1307 break;
1308 vim_snprintf((char *)IObuff, IOSIZE, "%6ld %7ld ",
1309 uhp->uh_seq, changes);
1310 u_add_time(IObuff + STRLEN(IObuff), IOSIZE - STRLEN(IObuff),
1311 uhp->uh_time);
1312 ((char_u **)(ga.ga_data))[ga.ga_len++] = vim_strsave(IObuff);
1313 }
1314
1315 uhp->uh_walk = mark;
1316
1317 /* go down in the tree if we haven't been there */
1318 if (uhp->uh_prev != NULL && uhp->uh_prev->uh_walk != nomark
1319 && uhp->uh_prev->uh_walk != mark)
1320 {
1321 uhp = uhp->uh_prev;
1322 ++changes;
1323 }
1324
1325 /* go to alternate branch if we haven't been there */
1326 else if (uhp->uh_alt_next != NULL
1327 && uhp->uh_alt_next->uh_walk != nomark
1328 && uhp->uh_alt_next->uh_walk != mark)
1329 uhp = uhp->uh_alt_next;
1330
1331 /* go up in the tree if we haven't been there and we are at the
1332 * start of alternate branches */
1333 else if (uhp->uh_next != NULL && uhp->uh_alt_prev == NULL
1334 && uhp->uh_next->uh_walk != nomark
1335 && uhp->uh_next->uh_walk != mark)
1336 {
1337 uhp = uhp->uh_next;
1338 --changes;
1339 }
1340
1341 else
1342 {
1343 /* need to backtrack; mark this node as done */
1344 uhp->uh_walk = nomark;
1345 if (uhp->uh_alt_prev != NULL)
1346 uhp = uhp->uh_alt_prev;
1347 else
1348 {
1349 uhp = uhp->uh_next;
1350 --changes;
1351 }
1352 }
1353 }
1354
1355 if (ga.ga_len == 0)
1356 MSG(_("Nothing to undo"));
1357 else
1358 {
1359 sort_strings((char_u **)ga.ga_data, ga.ga_len);
1360
1361 msg_start();
1362 msg_puts_attr((char_u *)_("number changes time"), hl_attr(HLF_T));
1363 for (i = 0; i < ga.ga_len && !got_int; ++i)
1364 {
1365 msg_putchar('\n');
1366 if (got_int)
1367 break;
1368 msg_puts(((char_u **)ga.ga_data)[i]);
1369 }
1370 msg_end();
1371
1372 ga_clear_strings(&ga);
1373 }
1374}
1375
1376/*
1377 * Put the timestamp of an undo header in "buf[buflen]" in a nice format.
1378 */
1379 static void
1380u_add_time(buf, buflen, tt)
1381 char_u *buf;
1382 size_t buflen;
1383 time_t tt;
1384{
1385#ifdef HAVE_STRFTIME
1386 struct tm *curtime;
1387
1388 if (time(NULL) - tt >= 100)
1389 {
1390 curtime = localtime(&tt);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001391 (void)strftime((char *)buf, buflen, "%H:%M:%S", curtime);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001392 }
1393 else
1394#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001395 vim_snprintf((char *)buf, buflen, _("%ld seconds ago"),
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001396 (long)(time(NULL) - tt));
1397}
1398
1399/*
Bram Moolenaare224ffa2006-03-01 00:01:28 +00001400 * ":undojoin": continue adding to the last entry list
1401 */
1402/*ARGSUSED*/
1403 void
1404ex_undojoin(eap)
1405 exarg_T *eap;
1406{
Bram Moolenaare224ffa2006-03-01 00:01:28 +00001407 if (curbuf->b_u_newhead == NULL)
1408 return; /* nothing changed before */
Bram Moolenaar57657d82006-04-21 22:12:41 +00001409 if (curbuf->b_u_curhead != NULL)
1410 {
1411 EMSG(_("E790: undojoin is not allowed after undo"));
1412 return;
1413 }
1414 if (!curbuf->b_u_synced)
1415 return; /* already unsynced */
Bram Moolenaare224ffa2006-03-01 00:01:28 +00001416 if (p_ul < 0)
1417 return; /* no entries, nothing to do */
1418 else
1419 {
1420 /* Go back to the last entry */
1421 curbuf->b_u_curhead = curbuf->b_u_newhead;
1422 curbuf->b_u_synced = FALSE; /* no entries, nothing to do */
1423 }
1424}
1425
1426/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 * Called after writing the file and setting b_changed to FALSE.
1428 * Now an undo means that the buffer is modified.
1429 */
1430 void
1431u_unchanged(buf)
1432 buf_T *buf;
1433{
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001434 u_unch_branch(buf->b_u_oldhead);
1435 buf->b_did_warn = FALSE;
1436}
1437
1438 static void
1439u_unch_branch(uhp)
1440 u_header_T *uhp;
1441{
Bram Moolenaar1e607892006-03-13 22:15:53 +00001442 u_header_T *uh;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001444 for (uh = uhp; uh != NULL; uh = uh->uh_prev)
1445 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 uh->uh_flags |= UH_CHANGED;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001447 if (uh->uh_alt_next != NULL)
1448 u_unch_branch(uh->uh_alt_next); /* recursive */
1449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450}
1451
1452/*
1453 * Get pointer to last added entry.
1454 * If it's not valid, give an error message and return NULL.
1455 */
1456 static u_entry_T *
1457u_get_headentry()
1458{
1459 if (curbuf->b_u_newhead == NULL || curbuf->b_u_newhead->uh_entry == NULL)
1460 {
1461 EMSG(_("E439: undo list corrupt"));
1462 return NULL;
1463 }
1464 return curbuf->b_u_newhead->uh_entry;
1465}
1466
1467/*
1468 * u_getbot(): compute the line number of the previous u_save
1469 * It is called only when b_u_synced is FALSE.
1470 */
1471 static void
1472u_getbot()
1473{
1474 u_entry_T *uep;
1475 linenr_T extra;
1476
1477 uep = u_get_headentry(); /* check for corrupt undo list */
1478 if (uep == NULL)
1479 return;
1480
1481 uep = curbuf->b_u_newhead->uh_getbot_entry;
1482 if (uep != NULL)
1483 {
1484 /*
1485 * the new ue_bot is computed from the number of lines that has been
1486 * inserted (0 - deleted) since calling u_save. This is equal to the
1487 * old line count subtracted from the current line count.
1488 */
1489 extra = curbuf->b_ml.ml_line_count - uep->ue_lcount;
1490 uep->ue_bot = uep->ue_top + uep->ue_size + 1 + extra;
1491 if (uep->ue_bot < 1 || uep->ue_bot > curbuf->b_ml.ml_line_count)
1492 {
1493 EMSG(_("E440: undo line missing"));
1494 uep->ue_bot = uep->ue_top + 1; /* assume all lines deleted, will
1495 * get all the old lines back
1496 * without deleting the current
1497 * ones */
1498 }
1499
1500 curbuf->b_u_newhead->uh_getbot_entry = NULL;
1501 }
1502
1503 curbuf->b_u_synced = TRUE;
1504}
1505
1506/*
Bram Moolenaar1e607892006-03-13 22:15:53 +00001507 * Free one header and its entry list and adjust the pointers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 */
1509 static void
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001510u_freeheader(buf, uhp, uhpp)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001511 buf_T *buf;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001512 u_header_T *uhp;
1513 u_header_T **uhpp; /* if not NULL reset when freeing this header */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514{
Bram Moolenaar1e607892006-03-13 22:15:53 +00001515 /* When there is an alternate redo list free that branch completely,
1516 * because we can never go there. */
1517 if (uhp->uh_alt_next != NULL)
1518 u_freebranch(buf, uhp->uh_alt_next, uhpp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519
Bram Moolenaar1e607892006-03-13 22:15:53 +00001520 if (uhp->uh_alt_prev != NULL)
1521 uhp->uh_alt_prev->uh_alt_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522
Bram Moolenaar1e607892006-03-13 22:15:53 +00001523 /* Update the links in the list to remove the header. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 if (uhp->uh_next == NULL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001525 buf->b_u_oldhead = uhp->uh_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 else
1527 uhp->uh_next->uh_prev = uhp->uh_prev;
1528
1529 if (uhp->uh_prev == NULL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001530 buf->b_u_newhead = uhp->uh_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 else
1532 uhp->uh_prev->uh_next = uhp->uh_next;
1533
Bram Moolenaar1e607892006-03-13 22:15:53 +00001534 u_freeentries(buf, uhp, uhpp);
1535}
1536
1537/*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001538 * Free an alternate branch and any following alternate branches.
Bram Moolenaar1e607892006-03-13 22:15:53 +00001539 */
1540 static void
1541u_freebranch(buf, uhp, uhpp)
1542 buf_T *buf;
1543 u_header_T *uhp;
1544 u_header_T **uhpp; /* if not NULL reset when freeing this header */
1545{
1546 u_header_T *tofree, *next;
1547
1548 if (uhp->uh_alt_prev != NULL)
1549 uhp->uh_alt_prev->uh_alt_next = NULL;
1550
1551 next = uhp;
1552 while (next != NULL)
1553 {
1554 tofree = next;
1555 if (tofree->uh_alt_next != NULL)
1556 u_freebranch(buf, tofree->uh_alt_next, uhpp); /* recursive */
1557 next = tofree->uh_prev;
1558 u_freeentries(buf, tofree, uhpp);
1559 }
1560}
1561
1562/*
1563 * Free all the undo entries for one header and the header itself.
1564 * This means that "uhp" is invalid when returning.
1565 */
1566 static void
1567u_freeentries(buf, uhp, uhpp)
1568 buf_T *buf;
1569 u_header_T *uhp;
1570 u_header_T **uhpp; /* if not NULL reset when freeing this header */
1571{
1572 u_entry_T *uep, *nuep;
1573
1574 /* Check for pointers to the header that become invalid now. */
1575 if (buf->b_u_curhead == uhp)
1576 buf->b_u_curhead = NULL;
1577 if (uhpp != NULL && uhp == *uhpp)
1578 *uhpp = NULL;
1579
1580 for (uep = uhp->uh_entry; uep != NULL; uep = nuep)
1581 {
1582 nuep = uep->ue_next;
1583 u_freeentry(uep, uep->ue_size);
1584 }
1585
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001586 U_FREE_LINE((char_u *)uhp);
1587 --buf->b_u_numhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588}
1589
1590/*
1591 * free entry 'uep' and 'n' lines in uep->ue_array[]
1592 */
1593 static void
1594u_freeentry(uep, n)
1595 u_entry_T *uep;
1596 long n;
1597{
Bram Moolenaar8d343302005-07-12 22:46:17 +00001598 while (n > 0)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001599 U_FREE_LINE(uep->ue_array[--n]);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001600 U_FREE_LINE((char_u *)uep->ue_array);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001601 U_FREE_LINE((char_u *)uep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602}
1603
1604/*
1605 * invalidate the undo buffer; called when storage has already been released
1606 */
1607 void
1608u_clearall(buf)
1609 buf_T *buf;
1610{
1611 buf->b_u_newhead = buf->b_u_oldhead = buf->b_u_curhead = NULL;
1612 buf->b_u_synced = TRUE;
1613 buf->b_u_numhead = 0;
1614 buf->b_u_line_ptr = NULL;
1615 buf->b_u_line_lnum = 0;
1616}
1617
1618/*
1619 * save the line "lnum" for the "U" command
1620 */
1621 void
1622u_saveline(lnum)
1623 linenr_T lnum;
1624{
1625 if (lnum == curbuf->b_u_line_lnum) /* line is already saved */
1626 return;
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001627 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) /* should never happen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 return;
1629 u_clearline();
1630 curbuf->b_u_line_lnum = lnum;
1631 if (curwin->w_cursor.lnum == lnum)
1632 curbuf->b_u_line_colnr = curwin->w_cursor.col;
1633 else
1634 curbuf->b_u_line_colnr = 0;
1635 if ((curbuf->b_u_line_ptr = u_save_line(lnum)) == NULL)
1636 do_outofmem_msg((long_u)0);
1637}
1638
1639/*
1640 * clear the line saved for the "U" command
1641 * (this is used externally for crossing a line while in insert mode)
1642 */
1643 void
1644u_clearline()
1645{
1646 if (curbuf->b_u_line_ptr != NULL)
1647 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001648 U_FREE_LINE(curbuf->b_u_line_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 curbuf->b_u_line_ptr = NULL;
1650 curbuf->b_u_line_lnum = 0;
1651 }
1652}
1653
1654/*
1655 * Implementation of the "U" command.
1656 * Differentiation from vi: "U" can be undone with the next "U".
1657 * We also allow the cursor to be in another line.
1658 */
1659 void
1660u_undoline()
1661{
1662 colnr_T t;
1663 char_u *oldp;
1664
1665 if (undo_off)
1666 return;
1667
1668 if (curbuf->b_u_line_ptr == NULL ||
1669 curbuf->b_u_line_lnum > curbuf->b_ml.ml_line_count)
1670 {
1671 beep_flush();
1672 return;
1673 }
1674 /* first save the line for the 'u' command */
1675 if (u_savecommon(curbuf->b_u_line_lnum - 1,
1676 curbuf->b_u_line_lnum + 1, (linenr_T)0) == FAIL)
1677 return;
1678 oldp = u_save_line(curbuf->b_u_line_lnum);
1679 if (oldp == NULL)
1680 {
1681 do_outofmem_msg((long_u)0);
1682 return;
1683 }
1684 ml_replace(curbuf->b_u_line_lnum, curbuf->b_u_line_ptr, TRUE);
1685 changed_bytes(curbuf->b_u_line_lnum, 0);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001686 U_FREE_LINE(curbuf->b_u_line_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 curbuf->b_u_line_ptr = oldp;
1688
1689 t = curbuf->b_u_line_colnr;
1690 if (curwin->w_cursor.lnum == curbuf->b_u_line_lnum)
1691 curbuf->b_u_line_colnr = curwin->w_cursor.col;
1692 curwin->w_cursor.col = t;
1693 curwin->w_cursor.lnum = curbuf->b_u_line_lnum;
1694}
1695
1696/*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001697 * There are two implementations of the memory management for undo:
1698 * 1. Use the standard malloc()/free() functions.
1699 * This should be fast for allocating memory, but when a buffer is
1700 * abandoned every single allocated chunk must be freed, which may be slow.
1701 * 2. Allocate larger blocks of memory and keep track of chunks ourselves.
1702 * This is fast for abandoning, but the use of linked lists is slow for
1703 * finding a free chunk. Esp. when a lot of lines are changed or deleted.
1704 * A bit of profiling showed that the first method is faster, especially when
1705 * making a large number of changes, under the condition that malloc()/free()
1706 * is implemented efficiently.
1707 */
1708#ifdef U_USE_MALLOC
1709/*
1710 * Version of undo memory allocation using malloc()/free()
1711 *
1712 * U_FREE_LINE() and U_ALLOC_LINE() are macros that invoke vim_free() and
1713 * lalloc() directly.
1714 */
1715
1716/*
1717 * Free all allocated memory blocks for the buffer 'buf'.
1718 */
1719 void
1720u_blockfree(buf)
1721 buf_T *buf;
1722{
Bram Moolenaar1e607892006-03-13 22:15:53 +00001723 while (buf->b_u_oldhead != NULL)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001724 u_freeheader(buf, buf->b_u_oldhead, NULL);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001725 U_FREE_LINE(buf->b_u_line_ptr);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001726}
1727
1728#else
1729/*
1730 * Storage allocation for the undo lines and blocks of the current file.
1731 * Version where Vim keeps track of the available memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 */
1733
1734/*
1735 * Memory is allocated in relatively large blocks. These blocks are linked
1736 * in the allocated block list, headed by curbuf->b_block_head. They are all
1737 * freed when abandoning a file, so we don't have to free every single line.
1738 * The list is kept sorted on memory address.
1739 * block_alloc() allocates a block.
1740 * m_blockfree() frees all blocks.
1741 *
1742 * The available chunks of memory are kept in free chunk lists. There is
1743 * one free list for each block of allocated memory. The list is kept sorted
1744 * on memory address.
1745 * u_alloc_line() gets a chunk from the free lists.
1746 * u_free_line() returns a chunk to the free lists.
1747 * curbuf->b_m_search points to the chunk before the chunk that was
1748 * freed/allocated the last time.
1749 * curbuf->b_mb_current points to the b_head where curbuf->b_m_search
1750 * points into the free list.
1751 *
1752 *
1753 * b_block_head /---> block #1 /---> block #2
1754 * mb_next ---/ mb_next ---/ mb_next ---> NULL
1755 * mb_info mb_info mb_info
1756 * | | |
1757 * V V V
1758 * NULL free chunk #1.1 free chunk #2.1
1759 * | |
1760 * V V
1761 * free chunk #1.2 NULL
1762 * |
1763 * V
1764 * NULL
1765 *
1766 * When a single free chunk list would have been used, it could take a lot
1767 * of time in u_free_line() to find the correct place to insert a chunk in the
1768 * free list. The single free list would become very long when many lines are
1769 * changed (e.g. with :%s/^M$//).
1770 */
1771
1772 /*
1773 * this blocksize is used when allocating new lines
1774 */
1775#define MEMBLOCKSIZE 2044
1776
1777/*
1778 * The size field contains the size of the chunk, including the size field
1779 * itself.
1780 *
1781 * When the chunk is not in-use it is preceded with the m_info structure.
1782 * The m_next field links it in one of the free chunk lists.
1783 *
1784 * On most unix systems structures have to be longword (32 or 64 bit) aligned.
1785 * On most other systems they are short (16 bit) aligned.
1786 */
1787
1788/* the structure definitions are now in structs.h */
1789
1790#ifdef ALIGN_LONG
1791 /* size of m_size */
1792# define M_OFFSET (sizeof(long_u))
1793#else
1794 /* size of m_size */
1795# define M_OFFSET (sizeof(short_u))
1796#endif
1797
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001798static char_u *u_blockalloc __ARGS((long_u));
1799
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800/*
1801 * Allocate a block of memory and link it in the allocated block list.
1802 */
1803 static char_u *
1804u_blockalloc(size)
1805 long_u size;
1806{
1807 mblock_T *p;
1808 mblock_T *mp, *next;
1809
1810 p = (mblock_T *)lalloc(size + sizeof(mblock_T), FALSE);
1811 if (p != NULL)
1812 {
1813 /* Insert the block into the allocated block list, keeping it
1814 sorted on address. */
1815 for (mp = &curbuf->b_block_head;
1816 (next = mp->mb_next) != NULL && next < p;
1817 mp = next)
1818 ;
1819 p->mb_next = next; /* link in block list */
1820 p->mb_size = size;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001821 p->mb_maxsize = 0; /* nothing free yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 mp->mb_next = p;
1823 p->mb_info.m_next = NULL; /* clear free list */
1824 p->mb_info.m_size = 0;
1825 curbuf->b_mb_current = p; /* remember current block */
1826 curbuf->b_m_search = NULL;
1827 p++; /* return usable memory */
1828 }
1829 return (char_u *)p;
1830}
1831
1832/*
1833 * free all allocated memory blocks for the buffer 'buf'
1834 */
1835 void
1836u_blockfree(buf)
1837 buf_T *buf;
1838{
1839 mblock_T *p, *np;
1840
1841 for (p = buf->b_block_head.mb_next; p != NULL; p = np)
1842 {
1843 np = p->mb_next;
1844 vim_free(p);
1845 }
1846 buf->b_block_head.mb_next = NULL;
1847 buf->b_m_search = NULL;
1848 buf->b_mb_current = NULL;
1849}
1850
1851/*
1852 * Free a chunk of memory for the current buffer.
1853 * Insert the chunk into the correct free list, keeping it sorted on address.
1854 */
1855 static void
1856u_free_line(ptr, keep)
1857 char_u *ptr;
1858 int keep; /* don't free the block when it's empty */
1859{
1860 minfo_T *next;
1861 minfo_T *prev, *curr;
1862 minfo_T *mp;
1863 mblock_T *nextb;
1864 mblock_T *prevb;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001865 long_u maxsize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866
1867 if (ptr == NULL || ptr == IObuff)
1868 return; /* illegal address can happen in out-of-memory situations */
1869
1870 mp = (minfo_T *)(ptr - M_OFFSET);
1871
1872 /* find block where chunk could be a part off */
1873 /* if we change curbuf->b_mb_current, curbuf->b_m_search is set to NULL */
1874 if (curbuf->b_mb_current == NULL || mp < (minfo_T *)curbuf->b_mb_current)
1875 {
1876 curbuf->b_mb_current = curbuf->b_block_head.mb_next;
1877 curbuf->b_m_search = NULL;
1878 }
1879 if ((nextb = curbuf->b_mb_current->mb_next) != NULL
1880 && (minfo_T *)nextb < mp)
1881 {
1882 curbuf->b_mb_current = nextb;
1883 curbuf->b_m_search = NULL;
1884 }
1885 while ((nextb = curbuf->b_mb_current->mb_next) != NULL
1886 && (minfo_T *)nextb < mp)
1887 curbuf->b_mb_current = nextb;
1888
1889 curr = NULL;
1890 /*
1891 * If mp is smaller than curbuf->b_m_search->m_next go to the start of
1892 * the free list
1893 */
1894 if (curbuf->b_m_search == NULL || mp < (curbuf->b_m_search->m_next))
1895 next = &(curbuf->b_mb_current->mb_info);
1896 else
1897 next = curbuf->b_m_search;
1898 /*
1899 * The following loop is executed very often.
1900 * Therefore it has been optimized at the cost of readability.
1901 * Keep it fast!
1902 */
1903#ifdef SLOW_BUT_EASY_TO_READ
1904 do
1905 {
1906 prev = curr;
1907 curr = next;
1908 next = next->m_next;
1909 }
1910 while (mp > next && next != NULL);
1911#else
1912 do /* first, middle, last */
1913 {
1914 prev = next->m_next; /* curr, next, prev */
1915 if (prev == NULL || mp <= prev)
1916 {
1917 prev = curr;
1918 curr = next;
1919 next = next->m_next;
1920 break;
1921 }
1922 curr = prev->m_next; /* next, prev, curr */
1923 if (curr == NULL || mp <= curr)
1924 {
1925 prev = next;
1926 curr = prev->m_next;
1927 next = curr->m_next;
1928 break;
1929 }
1930 next = curr->m_next; /* prev, curr, next */
1931 }
1932 while (mp > next && next != NULL);
1933#endif
1934
1935 /* if *mp and *next are concatenated, join them into one chunk */
1936 if ((char_u *)mp + mp->m_size == (char_u *)next)
1937 {
1938 mp->m_size += next->m_size;
1939 mp->m_next = next->m_next;
1940 }
1941 else
1942 mp->m_next = next;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001943 maxsize = mp->m_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944
1945 /* if *curr and *mp are concatenated, join them */
1946 if (prev != NULL && (char_u *)curr + curr->m_size == (char_u *)mp)
1947 {
1948 curr->m_size += mp->m_size;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001949 maxsize = curr->m_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 curr->m_next = mp->m_next;
1951 curbuf->b_m_search = prev;
1952 }
1953 else
1954 {
1955 curr->m_next = mp;
1956 curbuf->b_m_search = curr; /* put curbuf->b_m_search before freed
1957 chunk */
1958 }
1959
1960 /*
1961 * If the block only containes free memory now, release it.
1962 */
1963 if (!keep && curbuf->b_mb_current->mb_size
1964 == curbuf->b_mb_current->mb_info.m_next->m_size)
1965 {
1966 /* Find the block before the current one to be able to unlink it from
1967 * the list of blocks. */
1968 prevb = &curbuf->b_block_head;
1969 for (nextb = prevb->mb_next; nextb != curbuf->b_mb_current;
1970 nextb = nextb->mb_next)
1971 prevb = nextb;
1972 prevb->mb_next = nextb->mb_next;
1973 vim_free(nextb);
1974 curbuf->b_mb_current = NULL;
1975 curbuf->b_m_search = NULL;
1976 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001977 else if (curbuf->b_mb_current->mb_maxsize < maxsize)
1978 curbuf->b_mb_current->mb_maxsize = maxsize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979}
1980
1981/*
1982 * Allocate and initialize a new line structure with room for at least
1983 * 'size' characters plus a terminating NUL.
1984 */
1985 static char_u *
1986u_alloc_line(size)
1987 unsigned size;
1988{
1989 minfo_T *mp, *mprev, *mp2;
1990 mblock_T *mbp;
1991 int size_align;
1992
1993 /*
1994 * Add room for size field and trailing NUL byte.
1995 * Adjust for minimal size (must be able to store minfo_T
1996 * plus a trailing NUL, so the chunk can be released again)
1997 */
1998 size += M_OFFSET + 1;
1999 if (size < sizeof(minfo_T) + 1)
2000 size = sizeof(minfo_T) + 1;
2001
2002 /*
2003 * round size up for alignment
2004 */
2005 size_align = (size + ALIGN_MASK) & ~ALIGN_MASK;
2006
2007 /*
2008 * If curbuf->b_m_search is NULL (uninitialized free list) start at
2009 * curbuf->b_block_head
2010 */
2011 if (curbuf->b_mb_current == NULL || curbuf->b_m_search == NULL)
2012 {
2013 curbuf->b_mb_current = &curbuf->b_block_head;
2014 curbuf->b_m_search = &(curbuf->b_block_head.mb_info);
2015 }
2016
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002017 /* Search for a block with enough space. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 mbp = curbuf->b_mb_current;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002019 while (mbp->mb_maxsize < size_align)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002021 if (mbp->mb_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 mbp = mbp->mb_next;
2023 else
2024 mbp = &curbuf->b_block_head;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002025 if (mbp == curbuf->b_mb_current)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002027 int n = (size_align > (MEMBLOCKSIZE / 4)
2028 ? size_align : MEMBLOCKSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002030 /* Back where we started in block list: need to add a new block
2031 * with enough space. */
2032 mp = (minfo_T *)u_blockalloc((long_u)n);
2033 if (mp == NULL)
2034 return (NULL);
2035 mp->m_size = n;
2036 u_free_line((char_u *)mp + M_OFFSET, TRUE);
2037 mbp = curbuf->b_mb_current;
2038 break;
2039 }
2040 }
2041 if (mbp != curbuf->b_mb_current)
2042 curbuf->b_m_search = &(mbp->mb_info);
2043
2044 /* In this block find a chunk with enough space. */
2045 mprev = curbuf->b_m_search;
2046 mp = curbuf->b_m_search->m_next;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00002047 for (;;)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002048 {
2049 if (mp == NULL) /* at end of the list */
2050 mp = &(mbp->mb_info); /* wrap around to begin */
2051 if (mp->m_size >= size)
2052 break;
2053 if (mp == curbuf->b_m_search)
2054 {
2055 /* back where we started in free chunk list: "cannot happen" */
2056 EMSG2(_(e_intern2), "u_alloc_line()");
2057 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 }
2059 mprev = mp;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002060 mp = mp->m_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 }
2062
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002063 /* when using the largest chunk adjust mb_maxsize */
2064 if (mp->m_size >= mbp->mb_maxsize)
2065 mbp->mb_maxsize = 0;
2066
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 /* if the chunk we found is large enough, split it up in two */
2068 if ((long)mp->m_size - size_align >= (long)(sizeof(minfo_T) + 1))
2069 {
2070 mp2 = (minfo_T *)((char_u *)mp + size_align);
2071 mp2->m_size = mp->m_size - size_align;
2072 mp2->m_next = mp->m_next;
2073 mprev->m_next = mp2;
2074 mp->m_size = size_align;
2075 }
2076 else /* remove *mp from the free list */
2077 {
2078 mprev->m_next = mp->m_next;
2079 }
2080 curbuf->b_m_search = mprev;
2081 curbuf->b_mb_current = mbp;
2082
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002083 /* If using the largest chunk need to find the new largest chunk */
2084 if (mbp->mb_maxsize == 0)
2085 for (mp2 = &(mbp->mb_info); mp2 != NULL; mp2 = mp2->m_next)
2086 if (mbp->mb_maxsize < mp2->m_size)
2087 mbp->mb_maxsize = mp2->m_size;
2088
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 mp = (minfo_T *)((char_u *)mp + M_OFFSET);
2090 *(char_u *)mp = NUL; /* set the first byte to NUL */
2091
2092 return ((char_u *)mp);
2093}
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002094#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095
2096/*
2097 * u_save_line(): allocate memory with u_alloc_line() and copy line 'lnum'
2098 * into it.
2099 */
2100 static char_u *
2101u_save_line(lnum)
2102 linenr_T lnum;
2103{
2104 char_u *src;
2105 char_u *dst;
2106 unsigned len;
2107
2108 src = ml_get(lnum);
2109 len = (unsigned)STRLEN(src);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002110 if ((dst = U_ALLOC_LINE(len)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 mch_memmove(dst, src, (size_t)(len + 1));
2112 return (dst);
2113}
2114
2115/*
2116 * Check if the 'modified' flag is set, or 'ff' has changed (only need to
2117 * check the first character, because it can only be "dos", "unix" or "mac").
2118 * "nofile" and "scratch" type buffers are considered to always be unchanged.
2119 */
2120 int
2121bufIsChanged(buf)
2122 buf_T *buf;
2123{
2124 return
2125#ifdef FEAT_QUICKFIX
2126 !bt_dontwrite(buf) &&
2127#endif
2128 (buf->b_changed || file_ff_differs(buf));
2129}
2130
2131 int
2132curbufIsChanged()
2133{
2134 return
2135#ifdef FEAT_QUICKFIX
2136 !bt_dontwrite(curbuf) &&
2137#endif
2138 (curbuf->b_changed || file_ff_differs(curbuf));
2139}