blob: fd1bcc79c4ff1ffd8de6e00ea21f2d1bf43287a4 [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));
Bram Moolenaarca003e12006-03-17 23:19:38 +000090static void u_undoredo __ARGS((int undo));
Bram Moolenaardb552d602006-03-23 22:59:57 +000091static void u_undo_end __ARGS((int did_undo, int absolute));
Bram Moolenaarefd2bf12006-03-16 21:41:35 +000092static void u_add_time __ARGS((char_u *buf, size_t buflen, time_t tt));
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +000093static void u_freeheader __ARGS((buf_T *buf, u_header_T *uhp, u_header_T **uhpp));
Bram Moolenaar1e607892006-03-13 22:15:53 +000094static void u_freebranch __ARGS((buf_T *buf, u_header_T *uhp, u_header_T **uhpp));
95static void u_freeentries __ARGS((buf_T *buf, u_header_T *uhp, u_header_T **uhpp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000096static void u_freeentry __ARGS((u_entry_T *, long));
97
Bram Moolenaar26a60b42005-02-22 08:49:11 +000098#ifdef U_USE_MALLOC
99# define U_FREE_LINE(ptr) vim_free(ptr)
100# define U_ALLOC_LINE(size) lalloc((long_u)((size) + 1), FALSE)
101#else
102static void u_free_line __ARGS((char_u *ptr, int keep));
103static char_u *u_alloc_line __ARGS((unsigned size));
104# define U_FREE_LINE(ptr) u_free_line((ptr), FALSE)
105# define U_ALLOC_LINE(size) u_alloc_line(size)
106#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107static char_u *u_save_line __ARGS((linenr_T));
108
109static long u_newcount, u_oldcount;
110
111/*
112 * When 'u' flag included in 'cpoptions', we behave like vi. Need to remember
113 * the action that "u" should do.
114 */
115static int undo_undoes = FALSE;
116
117/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000118 * Save the current line for both the "u" and "U" command.
119 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120 */
121 int
122u_save_cursor()
123{
124 return (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
125 (linenr_T)(curwin->w_cursor.lnum + 1)));
126}
127
128/*
129 * Save the lines between "top" and "bot" for both the "u" and "U" command.
130 * "top" may be 0 and bot may be curbuf->b_ml.ml_line_count + 1.
131 * Returns FAIL when lines could not be saved, OK otherwise.
132 */
133 int
134u_save(top, bot)
135 linenr_T top, bot;
136{
137 if (undo_off)
138 return OK;
139
140 if (top > curbuf->b_ml.ml_line_count ||
141 top >= bot || bot > curbuf->b_ml.ml_line_count + 1)
142 return FALSE; /* rely on caller to do error messages */
143
144 if (top + 2 == bot)
145 u_saveline((linenr_T)(top + 1));
146
147 return (u_savecommon(top, bot, (linenr_T)0));
148}
149
150/*
151 * save the line "lnum" (used by ":s" and "~" command)
152 * The line is replaced, so the new bottom line is lnum + 1.
153 */
154 int
155u_savesub(lnum)
156 linenr_T lnum;
157{
158 if (undo_off)
159 return OK;
160
161 return (u_savecommon(lnum - 1, lnum + 1, lnum + 1));
162}
163
164/*
165 * a new line is inserted before line "lnum" (used by :s command)
166 * The line is inserted, so the new bottom line is lnum + 1.
167 */
168 int
169u_inssub(lnum)
170 linenr_T lnum;
171{
172 if (undo_off)
173 return OK;
174
175 return (u_savecommon(lnum - 1, lnum, lnum + 1));
176}
177
178/*
179 * save the lines "lnum" - "lnum" + nlines (used by delete command)
180 * The lines are deleted, so the new bottom line is lnum, unless the buffer
181 * becomes empty.
182 */
183 int
184u_savedel(lnum, nlines)
185 linenr_T lnum;
186 long nlines;
187{
188 if (undo_off)
189 return OK;
190
191 return (u_savecommon(lnum - 1, lnum + nlines,
192 nlines == curbuf->b_ml.ml_line_count ? 2 : lnum));
193}
194
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000195/*
196 * Return TRUE when undo is allowed. Otherwise give an error message and
197 * return FALSE.
198 */
199 static int
200undo_allowed()
201{
202 /* Don't allow changes when 'modifiable' is off. */
203 if (!curbuf->b_p_ma)
204 {
205 EMSG(_(e_modifiable));
206 return FALSE;
207 }
208
209#ifdef HAVE_SANDBOX
210 /* In the sandbox it's not allowed to change the text. */
211 if (sandbox != 0)
212 {
213 EMSG(_(e_sandbox));
214 return FALSE;
215 }
216#endif
217
218 /* Don't allow changes in the buffer while editing the cmdline. The
219 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000220 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000221 {
222 EMSG(_(e_secure));
223 return FALSE;
224 }
225
226 return TRUE;
227}
228
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 static int
230u_savecommon(top, bot, newbot)
231 linenr_T top, bot;
232 linenr_T newbot;
233{
Bram Moolenaar1e607892006-03-13 22:15:53 +0000234 linenr_T lnum;
235 long i;
236 u_header_T *uhp;
237 u_header_T *old_curhead;
238 u_entry_T *uep;
239 u_entry_T *prev_uep;
240 long size;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000242 /* When making changes is not allowed return FAIL. It's a crude way to
243 * make all change commands fail. */
244 if (!undo_allowed())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246
247#ifdef FEAT_NETBEANS_INTG
248 /*
249 * Netbeans defines areas that cannot be modified. Bail out here when
250 * trying to change text in a guarded area.
251 */
Bram Moolenaar009b2592004-10-24 19:18:58 +0000252 if (usingNetbeans)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 {
Bram Moolenaar009b2592004-10-24 19:18:58 +0000254 if (netbeans_is_guarded(top, bot))
255 {
256 EMSG(_(e_guarded));
257 return FAIL;
258 }
259 if (curbuf->b_p_ro)
260 {
261 EMSG(_(e_nbreadonly));
262 return FAIL;
263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 }
265#endif
266
267#ifdef FEAT_AUTOCMD
268 /*
269 * Saving text for undo means we are going to make a change. Give a
270 * warning for a read-only file before making the change, so that the
271 * FileChangedRO event can replace the buffer with a read-write version
272 * (e.g., obtained from a source control system).
273 */
274 change_warning(0);
275#endif
276
277 size = bot - top - 1;
278
279 /*
280 * if curbuf->b_u_synced == TRUE make a new header
281 */
282 if (curbuf->b_u_synced)
283 {
284#ifdef FEAT_JUMPLIST
285 /* Need to create new entry in b_changelist. */
286 curbuf->b_new_change = TRUE;
287#endif
288
Bram Moolenaar1e607892006-03-13 22:15:53 +0000289 if (p_ul >= 0)
290 {
291 /*
292 * Make a new header entry. Do this first so that we don't mess
293 * up the undo info when out of memory.
294 */
295 uhp = (u_header_T *)U_ALLOC_LINE((unsigned)sizeof(u_header_T));
296 if (uhp == NULL)
297 goto nomem;
298 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000299 else
300 uhp = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000301
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 /*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000303 * If we undid more than we redid, move the entry lists before and
304 * including curbuf->b_u_curhead to an alternate branch.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 */
Bram Moolenaar1e607892006-03-13 22:15:53 +0000306 old_curhead = curbuf->b_u_curhead;
307 if (old_curhead != NULL)
308 {
309 curbuf->b_u_newhead = old_curhead->uh_next;
310 curbuf->b_u_curhead = NULL;
311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312
313 /*
314 * free headers to keep the size right
315 */
316 while (curbuf->b_u_numhead > p_ul && curbuf->b_u_oldhead != NULL)
Bram Moolenaar1e607892006-03-13 22:15:53 +0000317 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000318 u_header_T *uhfree = curbuf->b_u_oldhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000319
320 /* If there is no branch only free one header. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000321 if (uhfree->uh_alt_next == NULL)
322 u_freeheader(curbuf, uhfree, &old_curhead);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000323 else
324 {
325 /* Free the oldest alternate branch as a whole. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000326 while (uhfree->uh_alt_next != NULL)
327 uhfree = uhfree->uh_alt_next;
328 u_freebranch(curbuf, uhfree, &old_curhead);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000329 }
330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000332 if (uhp == NULL) /* no undo at all */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 {
Bram Moolenaar1e607892006-03-13 22:15:53 +0000334 if (old_curhead != NULL)
335 u_freebranch(curbuf, old_curhead, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 curbuf->b_u_synced = FALSE;
337 return OK;
338 }
339
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 uhp->uh_prev = NULL;
341 uhp->uh_next = curbuf->b_u_newhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000342 uhp->uh_alt_next = old_curhead;
343 if (old_curhead != NULL)
344 {
345 old_curhead->uh_alt_prev = uhp;
346 if (curbuf->b_u_oldhead == old_curhead)
347 curbuf->b_u_oldhead = uhp;
348 }
349 uhp->uh_alt_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 if (curbuf->b_u_newhead != NULL)
351 curbuf->b_u_newhead->uh_prev = uhp;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000352
Bram Moolenaarca003e12006-03-17 23:19:38 +0000353 uhp->uh_seq = ++curbuf->b_u_seq_last;
354 curbuf->b_u_seq_cur = uhp->uh_seq;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000355 uhp->uh_time = time(NULL);
356 curbuf->b_u_seq_time = uhp->uh_time + 1;
357
Bram Moolenaar1e607892006-03-13 22:15:53 +0000358 uhp->uh_walk = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 uhp->uh_entry = NULL;
360 uhp->uh_getbot_entry = NULL;
361 uhp->uh_cursor = curwin->w_cursor; /* save cursor pos. for undo */
362#ifdef FEAT_VIRTUALEDIT
363 if (virtual_active() && curwin->w_cursor.coladd > 0)
364 uhp->uh_cursor_vcol = getviscol();
365 else
366 uhp->uh_cursor_vcol = -1;
367#endif
368
369 /* save changed and buffer empty flag for undo */
370 uhp->uh_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
371 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
372
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000373 /* save named marks and Visual marks for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 mch_memmove(uhp->uh_namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000375#ifdef FEAT_VISUAL
376 uhp->uh_visual = curbuf->b_visual;
377#endif
378
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 curbuf->b_u_newhead = uhp;
380 if (curbuf->b_u_oldhead == NULL)
381 curbuf->b_u_oldhead = uhp;
382 ++curbuf->b_u_numhead;
383 }
384 else
385 {
386 if (p_ul < 0) /* no undo at all */
387 return OK;
388
389 /*
390 * When saving a single line, and it has been saved just before, it
391 * doesn't make sense saving it again. Saves a lot of memory when
392 * making lots of changes inside the same line.
393 * This is only possible if the previous change didn't increase or
394 * decrease the number of lines.
395 * Check the ten last changes. More doesn't make sense and takes too
396 * long.
397 */
398 if (size == 1)
399 {
400 uep = u_get_headentry();
401 prev_uep = NULL;
402 for (i = 0; i < 10; ++i)
403 {
404 if (uep == NULL)
405 break;
406
407 /* If lines have been inserted/deleted we give up.
408 * Also when the line was included in a multi-line save. */
409 if ((curbuf->b_u_newhead->uh_getbot_entry != uep
410 ? (uep->ue_top + uep->ue_size + 1
411 != (uep->ue_bot == 0
412 ? curbuf->b_ml.ml_line_count + 1
413 : uep->ue_bot))
414 : uep->ue_lcount != curbuf->b_ml.ml_line_count)
415 || (uep->ue_size > 1
416 && top >= uep->ue_top
417 && top + 2 <= uep->ue_top + uep->ue_size + 1))
418 break;
419
420 /* If it's the same line we can skip saving it again. */
421 if (uep->ue_size == 1 && uep->ue_top == top)
422 {
423 if (i > 0)
424 {
425 /* It's not the last entry: get ue_bot for the last
426 * entry now. Following deleted/inserted lines go to
427 * the re-used entry. */
428 u_getbot();
429 curbuf->b_u_synced = FALSE;
430
431 /* Move the found entry to become the last entry. The
432 * order of undo/redo doesn't matter for the entries
433 * we move it over, since they don't change the line
434 * count and don't include this line. It does matter
435 * for the found entry if the line count is changed by
436 * the executed command. */
437 prev_uep->ue_next = uep->ue_next;
438 uep->ue_next = curbuf->b_u_newhead->uh_entry;
439 curbuf->b_u_newhead->uh_entry = uep;
440 }
441
442 /* The executed command may change the line count. */
443 if (newbot != 0)
444 uep->ue_bot = newbot;
445 else if (bot > curbuf->b_ml.ml_line_count)
446 uep->ue_bot = 0;
447 else
448 {
449 uep->ue_lcount = curbuf->b_ml.ml_line_count;
450 curbuf->b_u_newhead->uh_getbot_entry = uep;
451 }
452 return OK;
453 }
454 prev_uep = uep;
455 uep = uep->ue_next;
456 }
457 }
458
459 /* find line number for ue_bot for previous u_save() */
460 u_getbot();
461 }
462
463#if !defined(UNIX) && !defined(DJGPP) && !defined(WIN32) && !defined(__EMX__)
464 /*
465 * With Amiga and MSDOS 16 bit we can't handle big undo's, because
466 * then u_alloc_line would have to allocate a block larger than 32K
467 */
468 if (size >= 8000)
469 goto nomem;
470#endif
471
472 /*
473 * add lines in front of entry list
474 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000475 uep = (u_entry_T *)U_ALLOC_LINE((unsigned)sizeof(u_entry_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 if (uep == NULL)
477 goto nomem;
478
479 uep->ue_size = size;
480 uep->ue_top = top;
481 if (newbot != 0)
482 uep->ue_bot = newbot;
483 /*
484 * Use 0 for ue_bot if bot is below last line.
485 * Otherwise we have to compute ue_bot later.
486 */
487 else if (bot > curbuf->b_ml.ml_line_count)
488 uep->ue_bot = 0;
489 else
490 {
491 uep->ue_lcount = curbuf->b_ml.ml_line_count;
492 curbuf->b_u_newhead->uh_getbot_entry = uep;
493 }
494
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000495 if (size > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000497 if ((uep->ue_array = (char_u **)U_ALLOC_LINE(
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 (unsigned)(sizeof(char_u *) * size))) == NULL)
499 {
500 u_freeentry(uep, 0L);
501 goto nomem;
502 }
503 for (i = 0, lnum = top + 1; i < size; ++i)
504 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000505 fast_breakcheck();
506 if (got_int)
507 {
508 u_freeentry(uep, i);
509 return FAIL;
510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 if ((uep->ue_array[i] = u_save_line(lnum++)) == NULL)
512 {
513 u_freeentry(uep, i);
514 goto nomem;
515 }
516 }
517 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000518 else
519 uep->ue_array = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 uep->ue_next = curbuf->b_u_newhead->uh_entry;
521 curbuf->b_u_newhead->uh_entry = uep;
522 curbuf->b_u_synced = FALSE;
523 undo_undoes = FALSE;
524
525 return OK;
526
527nomem:
528 msg_silent = 0; /* must display the prompt */
529 if (ask_yesno((char_u *)_("No undo possible; continue anyway"), TRUE)
530 == 'y')
531 {
532 undo_off = TRUE; /* will be reset when character typed */
533 return OK;
534 }
535 do_outofmem_msg((long_u)0);
536 return FAIL;
537}
538
539/*
540 * If 'cpoptions' contains 'u': Undo the previous undo or redo (vi compatible).
541 * If 'cpoptions' does not contain 'u': Always undo.
542 */
543 void
544u_undo(count)
545 int count;
546{
547 /*
548 * If we get an undo command while executing a macro, we behave like the
549 * original vi. If this happens twice in one macro the result will not
550 * be compatible.
551 */
552 if (curbuf->b_u_synced == FALSE)
553 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000554 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 count = 1;
556 }
557
558 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
559 undo_undoes = TRUE;
560 else
561 undo_undoes = !undo_undoes;
562 u_doit(count);
563}
564
565/*
566 * If 'cpoptions' contains 'u': Repeat the previous undo or redo.
567 * If 'cpoptions' does not contain 'u': Always redo.
568 */
569 void
570u_redo(count)
571 int count;
572{
573 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
574 undo_undoes = FALSE;
575 u_doit(count);
576}
577
578/*
579 * Undo or redo, depending on 'undo_undoes', 'count' times.
580 */
581 static void
Bram Moolenaarca003e12006-03-17 23:19:38 +0000582u_doit(startcount)
583 int startcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584{
Bram Moolenaarca003e12006-03-17 23:19:38 +0000585 int count = startcount;
586
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000587 if (!undo_allowed())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589
590 u_newcount = 0;
591 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000592 if (curbuf->b_ml.ml_flags & ML_EMPTY)
593 u_oldcount = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 while (count--)
595 {
596 if (undo_undoes)
597 {
598 if (curbuf->b_u_curhead == NULL) /* first undo */
599 curbuf->b_u_curhead = curbuf->b_u_newhead;
600 else if (p_ul > 0) /* multi level undo */
601 /* get next undo */
602 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_next;
603 /* nothing to undo */
604 if (curbuf->b_u_numhead == 0 || curbuf->b_u_curhead == NULL)
605 {
606 /* stick curbuf->b_u_curhead at end */
607 curbuf->b_u_curhead = curbuf->b_u_oldhead;
608 beep_flush();
Bram Moolenaarca003e12006-03-17 23:19:38 +0000609 if (count == startcount - 1)
610 {
611 MSG(_("Already at oldest change"));
612 return;
613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 break;
615 }
616
Bram Moolenaarca003e12006-03-17 23:19:38 +0000617 u_undoredo(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 }
619 else
620 {
621 if (curbuf->b_u_curhead == NULL || p_ul <= 0)
622 {
623 beep_flush(); /* nothing to redo */
Bram Moolenaarca003e12006-03-17 23:19:38 +0000624 if (count == startcount - 1)
625 {
626 MSG(_("Already at newest change"));
627 return;
628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 break;
630 }
631
Bram Moolenaarca003e12006-03-17 23:19:38 +0000632 u_undoredo(FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000633
634 /* Advance for next redo. Set "newhead" when at the end of the
635 * redoable changes. */
636 if (curbuf->b_u_curhead->uh_prev == NULL)
637 curbuf->b_u_newhead = curbuf->b_u_curhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_prev;
639 }
640 }
Bram Moolenaardb552d602006-03-23 22:59:57 +0000641 u_undo_end(undo_undoes, FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000642}
643
644static int lastmark = 0;
645
646/*
647 * Undo or redo over the timeline.
648 * When "step" is negative go back in time, otherwise goes forward in time.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000649 * When "sec" is FALSE make "step" steps, when "sec" is TRUE use "step" as
650 * seconds.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000651 * When "absolute" is TRUE use "step" as the sequence number to jump to.
652 * "sec" must be FALSE then.
Bram Moolenaar1e607892006-03-13 22:15:53 +0000653 */
654 void
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000655undo_time(step, sec, absolute)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000656 long step;
657 int sec;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000658 int absolute;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000659{
660 long target;
661 long closest;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000662 long closest_start;
663 long closest_seq = 0;
664 long val;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000665 u_header_T *uhp;
666 u_header_T *last;
667 int mark;
668 int nomark;
669 int round;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000670 int dosec = sec;
671 int above = FALSE;
Bram Moolenaar433f7c82006-03-21 21:29:36 +0000672 int did_undo = TRUE;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000673
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000674 /* First make sure the current undoable change is synced. */
675 if (curbuf->b_u_synced == FALSE)
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000676 u_sync(TRUE);
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000677
Bram Moolenaar1e607892006-03-13 22:15:53 +0000678 u_newcount = 0;
679 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000680 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar1e607892006-03-13 22:15:53 +0000681 u_oldcount = -1;
682
Bram Moolenaarca003e12006-03-17 23:19:38 +0000683 /* "target" is the node below which we want to be.
684 * Init "closest" to a value we can't reach. */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000685 if (absolute)
686 {
687 target = step;
Bram Moolenaarca003e12006-03-17 23:19:38 +0000688 closest = -1;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000689 }
Bram Moolenaarca003e12006-03-17 23:19:38 +0000690 else
Bram Moolenaar1e607892006-03-13 22:15:53 +0000691 {
Bram Moolenaarca003e12006-03-17 23:19:38 +0000692 /* When doing computations with time_t subtract starttime, because
693 * time_t converted to a long may result in a wrong number. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000694 if (sec)
Bram Moolenaarca003e12006-03-17 23:19:38 +0000695 target = (long)(curbuf->b_u_seq_time - starttime) + step;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000696 else
697 target = curbuf->b_u_seq_cur + step;
Bram Moolenaarca003e12006-03-17 23:19:38 +0000698 if (step < 0)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000699 {
Bram Moolenaarca003e12006-03-17 23:19:38 +0000700 if (target < 0)
701 target = 0;
702 closest = -1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000703 }
704 else
705 {
Bram Moolenaarca003e12006-03-17 23:19:38 +0000706 if (sec)
Bram Moolenaardb552d602006-03-23 22:59:57 +0000707 closest = (long)(time(NULL) - starttime + 1);
Bram Moolenaarca003e12006-03-17 23:19:38 +0000708 else
709 closest = curbuf->b_u_seq_last + 2;
710 if (target >= closest)
711 target = closest - 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000712 }
Bram Moolenaar1e607892006-03-13 22:15:53 +0000713 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000714 closest_start = closest;
Bram Moolenaarca003e12006-03-17 23:19:38 +0000715 closest_seq = curbuf->b_u_seq_cur;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000716
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000717 /*
718 * May do this twice:
Bram Moolenaar1e607892006-03-13 22:15:53 +0000719 * 1. Search for "target", update "closest" to the best match found.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000720 * 2. If "target" not found search for "closest".
721 *
722 * When using the closest time we use the sequence number in the second
723 * round, because there may be several entries with the same time.
724 */
Bram Moolenaar1e607892006-03-13 22:15:53 +0000725 for (round = 1; round <= 2; ++round)
726 {
727 /* Find the path from the current state to where we want to go. The
728 * desired state can be anywhere in the undo tree, need to go all over
729 * it. We put "nomark" in uh_walk where we have been without success,
730 * "mark" where it could possibly be. */
731 mark = ++lastmark;
732 nomark = ++lastmark;
733
734 if (curbuf->b_u_curhead == NULL) /* at leaf of the tree */
735 uhp = curbuf->b_u_newhead;
736 else
737 uhp = curbuf->b_u_curhead;
738
739 while (uhp != NULL)
740 {
741 uhp->uh_walk = mark;
Bram Moolenaardb552d602006-03-23 22:59:57 +0000742 val = (long)(dosec ? (uhp->uh_time - starttime) : uhp->uh_seq);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000743
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000744 if (round == 1)
745 {
746 /* Remember the header that is closest to the target.
747 * It must be at least in the right direction (checked with
Bram Moolenaarca003e12006-03-17 23:19:38 +0000748 * "b_u_seq_cur"). When the timestamp is equal find the
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000749 * highest/lowest sequence number. */
Bram Moolenaarca003e12006-03-17 23:19:38 +0000750 if ((step < 0 ? uhp->uh_seq <= curbuf->b_u_seq_cur
751 : uhp->uh_seq > curbuf->b_u_seq_cur)
752 && ((dosec && val == closest)
753 ? (step < 0
754 ? uhp->uh_seq < closest_seq
755 : uhp->uh_seq > closest_seq)
756 : closest == closest_start
757 || (val > target
758 ? (closest > target
759 ? val - target <= closest - target
760 : val - target <= target - closest)
761 : (closest > target
762 ? target - val <= closest - target
763 : target - val <= target - closest))))
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000764 {
765 closest = val;
766 closest_seq = uhp->uh_seq;
767 }
768 }
769
770 /* Quit searching when we found a match. But when searching for a
771 * time we need to continue looking for the best uh_seq. */
772 if (target == val && !dosec)
773 break;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000774
775 /* go down in the tree if we haven't been there */
776 if (uhp->uh_prev != NULL && uhp->uh_prev->uh_walk != nomark
777 && uhp->uh_prev->uh_walk != mark)
778 uhp = uhp->uh_prev;
779
780 /* go to alternate branch if we haven't been there */
781 else if (uhp->uh_alt_next != NULL
782 && uhp->uh_alt_next->uh_walk != nomark
783 && uhp->uh_alt_next->uh_walk != mark)
784 uhp = uhp->uh_alt_next;
785
786 /* go up in the tree if we haven't been there and we are at the
787 * start of alternate branches */
788 else if (uhp->uh_next != NULL && uhp->uh_alt_prev == NULL
789 && uhp->uh_next->uh_walk != nomark
790 && uhp->uh_next->uh_walk != mark)
Bram Moolenaardb552d602006-03-23 22:59:57 +0000791 {
792 /* If still at the start we don't go through this change. */
793 if (uhp == curbuf->b_u_curhead)
794 uhp->uh_walk = nomark;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000795 uhp = uhp->uh_next;
Bram Moolenaardb552d602006-03-23 22:59:57 +0000796 }
Bram Moolenaar1e607892006-03-13 22:15:53 +0000797
798 else
799 {
800 /* need to backtrack; mark this node as useless */
801 uhp->uh_walk = nomark;
802 if (uhp->uh_alt_prev != NULL)
803 uhp = uhp->uh_alt_prev;
804 else
805 uhp = uhp->uh_next;
806 }
807 }
808
809 if (uhp != NULL) /* found it */
810 break;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000811
812 if (absolute)
813 {
814 EMSGN(_("Undo number %ld not found"), step);
815 return;
816 }
817
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000818 if (closest == closest_start)
Bram Moolenaar1e607892006-03-13 22:15:53 +0000819 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000820 if (step < 0)
821 MSG(_("Already at oldest change"));
822 else
823 MSG(_("Already at newest change"));
Bram Moolenaar1e607892006-03-13 22:15:53 +0000824 return;
825 }
826
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000827 target = closest_seq;
828 dosec = FALSE;
829 if (step < 0)
830 above = TRUE; /* stop above the header */
Bram Moolenaar1e607892006-03-13 22:15:53 +0000831 }
832
833 /* If we found it: Follow the path to go to where we want to be. */
834 if (uhp != NULL)
835 {
836 /*
837 * First go up the tree as much as needed.
838 */
839 for (;;)
840 {
841 uhp = curbuf->b_u_curhead;
842 if (uhp == NULL)
843 uhp = curbuf->b_u_newhead;
844 else
Bram Moolenaar1e607892006-03-13 22:15:53 +0000845 uhp = uhp->uh_next;
Bram Moolenaarca003e12006-03-17 23:19:38 +0000846 if (uhp == NULL || uhp->uh_walk != mark
847 || (uhp->uh_seq == target && !above))
Bram Moolenaar1e607892006-03-13 22:15:53 +0000848 break;
849 curbuf->b_u_curhead = uhp;
Bram Moolenaarca003e12006-03-17 23:19:38 +0000850 u_undoredo(TRUE);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000851 uhp->uh_walk = nomark; /* don't go back down here */
Bram Moolenaar1e607892006-03-13 22:15:53 +0000852 }
853
854 /*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000855 * And now go down the tree (redo), branching off where needed.
Bram Moolenaar1e607892006-03-13 22:15:53 +0000856 */
857 uhp = curbuf->b_u_curhead;
Bram Moolenaarca003e12006-03-17 23:19:38 +0000858 while (uhp != NULL)
Bram Moolenaar1e607892006-03-13 22:15:53 +0000859 {
860 /* Find the last branch with a mark, that's the one. */
861 last = uhp;
862 while (last->uh_alt_next != NULL
863 && last->uh_alt_next->uh_walk == mark)
864 last = last->uh_alt_next;
865 if (last != uhp)
866 {
867 /* Make the used branch the first entry in the list of
868 * alternatives to make "u" and CTRL-R take this branch. */
869 if (last->uh_alt_next != NULL)
870 last->uh_alt_next->uh_alt_prev = last->uh_alt_prev;
871 last->uh_alt_prev->uh_alt_next = last->uh_alt_next;
872 last->uh_alt_prev = NULL;
873 last->uh_alt_next = uhp;
874 uhp->uh_alt_prev = last;
875
876 uhp = last;
Bram Moolenaarca003e12006-03-17 23:19:38 +0000877 if (uhp->uh_next != NULL)
878 uhp->uh_next->uh_prev = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000879 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000880 curbuf->b_u_curhead = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000881
882 if (uhp->uh_walk != mark)
883 break; /* must have reached the target */
884
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000885 /* Stop when going backwards in time and didn't find the exact
886 * header we were looking for. */
887 if (uhp->uh_seq == target && above)
Bram Moolenaardb552d602006-03-23 22:59:57 +0000888 {
889 curbuf->b_u_seq_cur = target - 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000890 break;
Bram Moolenaardb552d602006-03-23 22:59:57 +0000891 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000892
Bram Moolenaarca003e12006-03-17 23:19:38 +0000893 u_undoredo(FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000894
895 /* Advance "curhead" to below the header we last used. If it
896 * becomes NULL then we need to set "newhead" to this leaf. */
897 if (uhp->uh_prev == NULL)
898 curbuf->b_u_newhead = uhp;
899 curbuf->b_u_curhead = uhp->uh_prev;
Bram Moolenaar433f7c82006-03-21 21:29:36 +0000900 did_undo = FALSE;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000901
902 if (uhp->uh_seq == target) /* found it! */
903 break;
904
905 uhp = uhp->uh_prev;
906 if (uhp == NULL || uhp->uh_walk != mark)
907 {
Bram Moolenaarca003e12006-03-17 23:19:38 +0000908 /* Need to redo more but can't find it... */
Bram Moolenaar1e607892006-03-13 22:15:53 +0000909 EMSG2(_(e_intern2), "undo_time()");
910 break;
911 }
912 }
913 }
Bram Moolenaardb552d602006-03-23 22:59:57 +0000914 u_undo_end(did_undo, absolute);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915}
916
917/*
918 * u_undoredo: common code for undo and redo
919 *
920 * The lines in the file are replaced by the lines in the entry list at
921 * curbuf->b_u_curhead. The replaced lines in the file are saved in the entry
922 * list for the next undo/redo.
Bram Moolenaarca003e12006-03-17 23:19:38 +0000923 *
924 * When "undo" is TRUE we go up in the tree, when FALSE we go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 */
926 static void
Bram Moolenaarca003e12006-03-17 23:19:38 +0000927u_undoredo(undo)
928 int undo;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929{
930 char_u **newarray = NULL;
931 linenr_T oldsize;
932 linenr_T newsize;
933 linenr_T top, bot;
934 linenr_T lnum;
935 linenr_T newlnum = MAXLNUM;
936 long i;
937 u_entry_T *uep, *nuep;
938 u_entry_T *newlist = NULL;
939 int old_flags;
940 int new_flags;
941 pos_T namedm[NMARKS];
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000942#ifdef FEAT_VISUAL
943 visualinfo_T visualinfo;
944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 int empty_buffer; /* buffer became empty */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000946 u_header_T *curhead = curbuf->b_u_curhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000948 old_flags = curhead->uh_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 new_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
950 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
951 setpcmark();
952
953 /*
954 * save marks before undo/redo
955 */
956 mch_memmove(namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000957#ifdef FEAT_VISUAL
958 visualinfo = curbuf->b_visual;
959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 curbuf->b_op_start.lnum = curbuf->b_ml.ml_line_count;
961 curbuf->b_op_start.col = 0;
962 curbuf->b_op_end.lnum = 0;
963 curbuf->b_op_end.col = 0;
964
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000965 for (uep = curhead->uh_entry; uep != NULL; uep = nuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 {
967 top = uep->ue_top;
968 bot = uep->ue_bot;
969 if (bot == 0)
970 bot = curbuf->b_ml.ml_line_count + 1;
971 if (top > curbuf->b_ml.ml_line_count || top >= bot
972 || bot > curbuf->b_ml.ml_line_count + 1)
973 {
974 EMSG(_("E438: u_undo: line numbers wrong"));
975 changed(); /* don't want UNCHANGED now */
976 return;
977 }
978
979 oldsize = bot - top - 1; /* number of lines before undo */
980 newsize = uep->ue_size; /* number of lines after undo */
981
982 if (top < newlnum)
983 {
984 /* If the saved cursor is somewhere in this undo block, move it to
985 * the remembered position. Makes "gwap" put the cursor back
986 * where it was. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000987 lnum = curhead->uh_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 if (lnum >= top && lnum <= top + newsize + 1)
989 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000990 curwin->w_cursor = curhead->uh_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 newlnum = curwin->w_cursor.lnum - 1;
992 }
993 else
994 {
995 /* Use the first line that actually changed. Avoids that
996 * undoing auto-formatting puts the cursor in the previous
997 * line. */
998 for (i = 0; i < newsize && i < oldsize; ++i)
999 if (STRCMP(uep->ue_array[i], ml_get(top + 1 + i)) != 0)
1000 break;
1001 if (i == newsize && newlnum == MAXLNUM && uep->ue_next == NULL)
1002 {
1003 newlnum = top;
1004 curwin->w_cursor.lnum = newlnum + 1;
1005 }
1006 else if (i < newsize)
1007 {
1008 newlnum = top + i;
1009 curwin->w_cursor.lnum = newlnum + 1;
1010 }
1011 }
1012 }
1013
1014 empty_buffer = FALSE;
1015
1016 /* delete the lines between top and bot and save them in newarray */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001017 if (oldsize > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001019 if ((newarray = (char_u **)U_ALLOC_LINE(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 (unsigned)(sizeof(char_u *) * oldsize))) == NULL)
1021 {
1022 do_outofmem_msg((long_u)(sizeof(char_u *) * oldsize));
1023 /*
1024 * We have messed up the entry list, repair is impossible.
1025 * we have to free the rest of the list.
1026 */
1027 while (uep != NULL)
1028 {
1029 nuep = uep->ue_next;
1030 u_freeentry(uep, uep->ue_size);
1031 uep = nuep;
1032 }
1033 break;
1034 }
1035 /* delete backwards, it goes faster in most cases */
1036 for (lnum = bot - 1, i = oldsize; --i >= 0; --lnum)
1037 {
1038 /* what can we do when we run out of memory? */
1039 if ((newarray[i] = u_save_line(lnum)) == NULL)
1040 do_outofmem_msg((long_u)0);
1041 /* remember we deleted the last line in the buffer, and a
1042 * dummy empty line will be inserted */
1043 if (curbuf->b_ml.ml_line_count == 1)
1044 empty_buffer = TRUE;
1045 ml_delete(lnum, FALSE);
1046 }
1047 }
Bram Moolenaar8d343302005-07-12 22:46:17 +00001048 else
1049 newarray = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050
1051 /* insert the lines in u_array between top and bot */
1052 if (newsize)
1053 {
1054 for (lnum = top, i = 0; i < newsize; ++i, ++lnum)
1055 {
1056 /*
1057 * If the file is empty, there is an empty line 1 that we
1058 * should get rid of, by replacing it with the new line
1059 */
1060 if (empty_buffer && lnum == 0)
1061 ml_replace((linenr_T)1, uep->ue_array[i], TRUE);
1062 else
1063 ml_append(lnum, uep->ue_array[i], (colnr_T)0, FALSE);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001064 U_FREE_LINE(uep->ue_array[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001066 U_FREE_LINE((char_u *)uep->ue_array);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 }
1068
1069 /* adjust marks */
1070 if (oldsize != newsize)
1071 {
1072 mark_adjust(top + 1, top + oldsize, (long)MAXLNUM,
1073 (long)newsize - (long)oldsize);
1074 if (curbuf->b_op_start.lnum > top + oldsize)
1075 curbuf->b_op_start.lnum += newsize - oldsize;
1076 if (curbuf->b_op_end.lnum > top + oldsize)
1077 curbuf->b_op_end.lnum += newsize - oldsize;
1078 }
1079
1080 changed_lines(top + 1, 0, bot, newsize - oldsize);
1081
1082 /* set '[ and '] mark */
1083 if (top + 1 < curbuf->b_op_start.lnum)
1084 curbuf->b_op_start.lnum = top + 1;
1085 if (newsize == 0 && top + 1 > curbuf->b_op_end.lnum)
1086 curbuf->b_op_end.lnum = top + 1;
1087 else if (top + newsize > curbuf->b_op_end.lnum)
1088 curbuf->b_op_end.lnum = top + newsize;
1089
1090 u_newcount += newsize;
1091 u_oldcount += oldsize;
1092 uep->ue_size = oldsize;
1093 uep->ue_array = newarray;
1094 uep->ue_bot = top + newsize + 1;
1095
1096 /*
1097 * insert this entry in front of the new entry list
1098 */
1099 nuep = uep->ue_next;
1100 uep->ue_next = newlist;
1101 newlist = uep;
1102 }
1103
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001104 curhead->uh_entry = newlist;
1105 curhead->uh_flags = new_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 if ((old_flags & UH_EMPTYBUF) && bufempty())
1107 curbuf->b_ml.ml_flags |= ML_EMPTY;
1108 if (old_flags & UH_CHANGED)
1109 changed();
1110 else
Bram Moolenaar009b2592004-10-24 19:18:58 +00001111#ifdef FEAT_NETBEANS_INTG
1112 /* per netbeans undo rules, keep it as modified */
1113 if (!isNetbeansModified(curbuf))
1114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 unchanged(curbuf, FALSE);
1116
1117 /*
1118 * restore marks from before undo/redo
1119 */
1120 for (i = 0; i < NMARKS; ++i)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001121 if (curhead->uh_namedm[i].lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001123 curbuf->b_namedm[i] = curhead->uh_namedm[i];
1124 curhead->uh_namedm[i] = namedm[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001126#ifdef FEAT_VISUAL
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001127 if (curhead->uh_visual.vi_start.lnum != 0)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001128 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001129 curbuf->b_visual = curhead->uh_visual;
1130 curhead->uh_visual = visualinfo;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001131 }
1132#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133
1134 /*
1135 * If the cursor is only off by one line, put it at the same position as
1136 * before starting the change (for the "o" command).
1137 * Otherwise the cursor should go to the first undone line.
1138 */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001139 if (curhead->uh_cursor.lnum + 1 == curwin->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 && curwin->w_cursor.lnum > 1)
1141 --curwin->w_cursor.lnum;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001142 if (curhead->uh_cursor.lnum == curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001144 curwin->w_cursor.col = curhead->uh_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001146 if (virtual_active() && curhead->uh_cursor_vcol >= 0)
1147 coladvance((colnr_T)curhead->uh_cursor_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 else
1149 curwin->w_cursor.coladd = 0;
1150#endif
1151 }
1152 else if (curwin->w_cursor.lnum <= curbuf->b_ml.ml_line_count)
1153 beginline(BL_SOL | BL_FIX);
1154 else
1155 {
1156 /* We get here with the current cursor line being past the end (eg
1157 * after adding lines at the end of the file, and then undoing it).
1158 * check_cursor() will move the cursor to the last line. Move it to
1159 * the first column here. */
1160 curwin->w_cursor.col = 0;
1161#ifdef FEAT_VIRTUALEDIT
1162 curwin->w_cursor.coladd = 0;
1163#endif
1164 }
1165
1166 /* Make sure the cursor is on an existing line and column. */
1167 check_cursor();
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001168
1169 /* Remember where we are for "g-" and ":earlier 10s". */
1170 curbuf->b_u_seq_cur = curhead->uh_seq;
Bram Moolenaarca003e12006-03-17 23:19:38 +00001171 if (undo)
1172 /* We are below the previous undo. However, to make ":earlier 1s"
1173 * work we compute this as being just above the just undone change. */
1174 --curbuf->b_u_seq_cur;
1175
1176 /* The timestamp can be the same for multiple changes, just use the one of
1177 * the undone/redone change. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001178 curbuf->b_u_seq_time = curhead->uh_time;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179}
1180
1181/*
1182 * If we deleted or added lines, report the number of less/more lines.
1183 * Otherwise, report the number of changes (this may be incorrect
1184 * in some cases, but it's better than nothing).
1185 */
1186 static void
Bram Moolenaardb552d602006-03-23 22:59:57 +00001187u_undo_end(did_undo, absolute)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001188 int did_undo; /* just did an undo */
Bram Moolenaardb552d602006-03-23 22:59:57 +00001189 int absolute; /* used ":undo N" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190{
Bram Moolenaar1e607892006-03-13 22:15:53 +00001191 char *msg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001192 u_header_T *uhp;
1193 char_u msgbuf[80];
Bram Moolenaar1e607892006-03-13 22:15:53 +00001194
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195#ifdef FEAT_FOLDING
1196 if ((fdo_flags & FDO_UNDO) && KeyTyped)
1197 foldOpenCursor();
1198#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +00001199
1200 if (global_busy /* no messages now, wait until global is finished */
1201 || !messaging()) /* 'lazyredraw' set, don't do messages now */
1202 return;
1203
1204 if (curbuf->b_ml.ml_flags & ML_EMPTY)
1205 --u_newcount;
1206
1207 u_oldcount -= u_newcount;
1208 if (u_oldcount == -1)
1209 msg = N_("more line");
1210 else if (u_oldcount < 0)
1211 msg = N_("more lines");
1212 else if (u_oldcount == 1)
1213 msg = N_("line less");
1214 else if (u_oldcount > 1)
1215 msg = N_("fewer lines");
1216 else
1217 {
1218 u_oldcount = u_newcount;
1219 if (u_newcount == 1)
1220 msg = N_("change");
1221 else
1222 msg = N_("changes");
1223 }
1224
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001225 if (curbuf->b_u_curhead != NULL)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001226 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00001227 /* For ":undo N" we prefer a "after #N" message. */
1228 if (absolute && curbuf->b_u_curhead->uh_next != NULL)
1229 {
1230 uhp = curbuf->b_u_curhead->uh_next;
1231 did_undo = FALSE;
1232 }
1233 else if (did_undo)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001234 uhp = curbuf->b_u_curhead;
1235 else
1236 uhp = curbuf->b_u_curhead->uh_next;
1237 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00001238 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001239 uhp = curbuf->b_u_newhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001240
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001241 if (uhp == NULL)
1242 *msgbuf = NUL;
1243 else
1244 u_add_time(msgbuf, sizeof(msgbuf), uhp->uh_time);
1245
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001246 smsg((char_u *)_("%ld %s; %s #%ld %s"),
Bram Moolenaarca003e12006-03-17 23:19:38 +00001247 u_oldcount < 0 ? -u_oldcount : u_oldcount,
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001248 _(msg),
1249 did_undo ? _("before") : _("after"),
1250 uhp == NULL ? 0L : uhp->uh_seq,
1251 msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252}
1253
1254/*
1255 * u_sync: stop adding to the current entry list
1256 */
1257 void
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001258u_sync(force)
1259 int force; /* Also sync when no_u_sync is set. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260{
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001261 /* Skip it when already synced or syncing is disabled. */
1262 if (curbuf->b_u_synced || (!force && no_u_sync > 0))
1263 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
1265 if (im_is_preediting())
1266 return; /* XIM is busy, don't break an undo sequence */
1267#endif
1268 if (p_ul < 0)
1269 curbuf->b_u_synced = TRUE; /* no entries, nothing to do */
1270 else
1271 {
1272 u_getbot(); /* compute ue_bot of previous u_save */
1273 curbuf->b_u_curhead = NULL;
1274 }
1275}
1276
1277/*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001278 * ":undolist": List the leafs of the undo tree
1279 */
1280/*ARGSUSED*/
1281 void
1282ex_undolist(eap)
1283 exarg_T *eap;
1284{
1285 garray_T ga;
1286 u_header_T *uhp;
1287 int mark;
1288 int nomark;
1289 int changes = 1;
1290 int i;
1291
1292 /*
1293 * 1: walk the tree to find all leafs, put the info in "ga".
1294 * 2: sort the lines
1295 * 3: display the list
1296 */
1297 mark = ++lastmark;
1298 nomark = ++lastmark;
1299 ga_init2(&ga, (int)sizeof(char *), 20);
1300
1301 uhp = curbuf->b_u_oldhead;
1302 while (uhp != NULL)
1303 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00001304 if (uhp->uh_prev == NULL && uhp->uh_walk != nomark
1305 && uhp->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001306 {
1307 if (ga_grow(&ga, 1) == FAIL)
1308 break;
1309 vim_snprintf((char *)IObuff, IOSIZE, "%6ld %7ld ",
1310 uhp->uh_seq, changes);
1311 u_add_time(IObuff + STRLEN(IObuff), IOSIZE - STRLEN(IObuff),
1312 uhp->uh_time);
1313 ((char_u **)(ga.ga_data))[ga.ga_len++] = vim_strsave(IObuff);
1314 }
1315
1316 uhp->uh_walk = mark;
1317
1318 /* go down in the tree if we haven't been there */
1319 if (uhp->uh_prev != NULL && uhp->uh_prev->uh_walk != nomark
1320 && uhp->uh_prev->uh_walk != mark)
1321 {
1322 uhp = uhp->uh_prev;
1323 ++changes;
1324 }
1325
1326 /* go to alternate branch if we haven't been there */
1327 else if (uhp->uh_alt_next != NULL
1328 && uhp->uh_alt_next->uh_walk != nomark
1329 && uhp->uh_alt_next->uh_walk != mark)
1330 uhp = uhp->uh_alt_next;
1331
1332 /* go up in the tree if we haven't been there and we are at the
1333 * start of alternate branches */
1334 else if (uhp->uh_next != NULL && uhp->uh_alt_prev == NULL
1335 && uhp->uh_next->uh_walk != nomark
1336 && uhp->uh_next->uh_walk != mark)
1337 {
1338 uhp = uhp->uh_next;
1339 --changes;
1340 }
1341
1342 else
1343 {
1344 /* need to backtrack; mark this node as done */
1345 uhp->uh_walk = nomark;
1346 if (uhp->uh_alt_prev != NULL)
1347 uhp = uhp->uh_alt_prev;
1348 else
1349 {
1350 uhp = uhp->uh_next;
1351 --changes;
1352 }
1353 }
1354 }
1355
1356 if (ga.ga_len == 0)
1357 MSG(_("Nothing to undo"));
1358 else
1359 {
1360 sort_strings((char_u **)ga.ga_data, ga.ga_len);
1361
1362 msg_start();
1363 msg_puts_attr((char_u *)_("number changes time"), hl_attr(HLF_T));
1364 for (i = 0; i < ga.ga_len && !got_int; ++i)
1365 {
1366 msg_putchar('\n');
1367 if (got_int)
1368 break;
1369 msg_puts(((char_u **)ga.ga_data)[i]);
1370 }
1371 msg_end();
1372
1373 ga_clear_strings(&ga);
1374 }
1375}
1376
1377/*
1378 * Put the timestamp of an undo header in "buf[buflen]" in a nice format.
1379 */
1380 static void
1381u_add_time(buf, buflen, tt)
1382 char_u *buf;
1383 size_t buflen;
1384 time_t tt;
1385{
1386#ifdef HAVE_STRFTIME
1387 struct tm *curtime;
1388
1389 if (time(NULL) - tt >= 100)
1390 {
1391 curtime = localtime(&tt);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001392 (void)strftime((char *)buf, buflen, "%H:%M:%S", curtime);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001393 }
1394 else
1395#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001396 vim_snprintf((char *)buf, buflen, _("%ld seconds ago"),
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001397 (long)(time(NULL) - tt));
1398}
1399
1400/*
Bram Moolenaare224ffa2006-03-01 00:01:28 +00001401 * ":undojoin": continue adding to the last entry list
1402 */
1403/*ARGSUSED*/
1404 void
1405ex_undojoin(eap)
1406 exarg_T *eap;
1407{
Bram Moolenaare224ffa2006-03-01 00:01:28 +00001408 if (curbuf->b_u_newhead == NULL)
1409 return; /* nothing changed before */
Bram Moolenaar57657d82006-04-21 22:12:41 +00001410 if (curbuf->b_u_curhead != NULL)
1411 {
1412 EMSG(_("E790: undojoin is not allowed after undo"));
1413 return;
1414 }
1415 if (!curbuf->b_u_synced)
1416 return; /* already unsynced */
Bram Moolenaare224ffa2006-03-01 00:01:28 +00001417 if (p_ul < 0)
1418 return; /* no entries, nothing to do */
1419 else
1420 {
1421 /* Go back to the last entry */
1422 curbuf->b_u_curhead = curbuf->b_u_newhead;
1423 curbuf->b_u_synced = FALSE; /* no entries, nothing to do */
1424 }
1425}
1426
1427/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 * Called after writing the file and setting b_changed to FALSE.
1429 * Now an undo means that the buffer is modified.
1430 */
1431 void
1432u_unchanged(buf)
1433 buf_T *buf;
1434{
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001435 u_unch_branch(buf->b_u_oldhead);
1436 buf->b_did_warn = FALSE;
1437}
1438
1439 static void
1440u_unch_branch(uhp)
1441 u_header_T *uhp;
1442{
Bram Moolenaar1e607892006-03-13 22:15:53 +00001443 u_header_T *uh;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001445 for (uh = uhp; uh != NULL; uh = uh->uh_prev)
1446 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 uh->uh_flags |= UH_CHANGED;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001448 if (uh->uh_alt_next != NULL)
1449 u_unch_branch(uh->uh_alt_next); /* recursive */
1450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451}
1452
1453/*
1454 * Get pointer to last added entry.
1455 * If it's not valid, give an error message and return NULL.
1456 */
1457 static u_entry_T *
1458u_get_headentry()
1459{
1460 if (curbuf->b_u_newhead == NULL || curbuf->b_u_newhead->uh_entry == NULL)
1461 {
1462 EMSG(_("E439: undo list corrupt"));
1463 return NULL;
1464 }
1465 return curbuf->b_u_newhead->uh_entry;
1466}
1467
1468/*
1469 * u_getbot(): compute the line number of the previous u_save
1470 * It is called only when b_u_synced is FALSE.
1471 */
1472 static void
1473u_getbot()
1474{
1475 u_entry_T *uep;
1476 linenr_T extra;
1477
1478 uep = u_get_headentry(); /* check for corrupt undo list */
1479 if (uep == NULL)
1480 return;
1481
1482 uep = curbuf->b_u_newhead->uh_getbot_entry;
1483 if (uep != NULL)
1484 {
1485 /*
1486 * the new ue_bot is computed from the number of lines that has been
1487 * inserted (0 - deleted) since calling u_save. This is equal to the
1488 * old line count subtracted from the current line count.
1489 */
1490 extra = curbuf->b_ml.ml_line_count - uep->ue_lcount;
1491 uep->ue_bot = uep->ue_top + uep->ue_size + 1 + extra;
1492 if (uep->ue_bot < 1 || uep->ue_bot > curbuf->b_ml.ml_line_count)
1493 {
1494 EMSG(_("E440: undo line missing"));
1495 uep->ue_bot = uep->ue_top + 1; /* assume all lines deleted, will
1496 * get all the old lines back
1497 * without deleting the current
1498 * ones */
1499 }
1500
1501 curbuf->b_u_newhead->uh_getbot_entry = NULL;
1502 }
1503
1504 curbuf->b_u_synced = TRUE;
1505}
1506
1507/*
Bram Moolenaar1e607892006-03-13 22:15:53 +00001508 * Free one header and its entry list and adjust the pointers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 */
1510 static void
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001511u_freeheader(buf, uhp, uhpp)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001512 buf_T *buf;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001513 u_header_T *uhp;
1514 u_header_T **uhpp; /* if not NULL reset when freeing this header */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515{
Bram Moolenaar1e607892006-03-13 22:15:53 +00001516 /* When there is an alternate redo list free that branch completely,
1517 * because we can never go there. */
1518 if (uhp->uh_alt_next != NULL)
1519 u_freebranch(buf, uhp->uh_alt_next, uhpp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520
Bram Moolenaar1e607892006-03-13 22:15:53 +00001521 if (uhp->uh_alt_prev != NULL)
1522 uhp->uh_alt_prev->uh_alt_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523
Bram Moolenaar1e607892006-03-13 22:15:53 +00001524 /* Update the links in the list to remove the header. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 if (uhp->uh_next == NULL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001526 buf->b_u_oldhead = uhp->uh_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 else
1528 uhp->uh_next->uh_prev = uhp->uh_prev;
1529
1530 if (uhp->uh_prev == NULL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001531 buf->b_u_newhead = uhp->uh_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 else
1533 uhp->uh_prev->uh_next = uhp->uh_next;
1534
Bram Moolenaar1e607892006-03-13 22:15:53 +00001535 u_freeentries(buf, uhp, uhpp);
1536}
1537
1538/*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001539 * Free an alternate branch and any following alternate branches.
Bram Moolenaar1e607892006-03-13 22:15:53 +00001540 */
1541 static void
1542u_freebranch(buf, uhp, uhpp)
1543 buf_T *buf;
1544 u_header_T *uhp;
1545 u_header_T **uhpp; /* if not NULL reset when freeing this header */
1546{
1547 u_header_T *tofree, *next;
1548
1549 if (uhp->uh_alt_prev != NULL)
1550 uhp->uh_alt_prev->uh_alt_next = NULL;
1551
1552 next = uhp;
1553 while (next != NULL)
1554 {
1555 tofree = next;
1556 if (tofree->uh_alt_next != NULL)
1557 u_freebranch(buf, tofree->uh_alt_next, uhpp); /* recursive */
1558 next = tofree->uh_prev;
1559 u_freeentries(buf, tofree, uhpp);
1560 }
1561}
1562
1563/*
1564 * Free all the undo entries for one header and the header itself.
1565 * This means that "uhp" is invalid when returning.
1566 */
1567 static void
1568u_freeentries(buf, uhp, uhpp)
1569 buf_T *buf;
1570 u_header_T *uhp;
1571 u_header_T **uhpp; /* if not NULL reset when freeing this header */
1572{
1573 u_entry_T *uep, *nuep;
1574
1575 /* Check for pointers to the header that become invalid now. */
1576 if (buf->b_u_curhead == uhp)
1577 buf->b_u_curhead = NULL;
1578 if (uhpp != NULL && uhp == *uhpp)
1579 *uhpp = NULL;
1580
1581 for (uep = uhp->uh_entry; uep != NULL; uep = nuep)
1582 {
1583 nuep = uep->ue_next;
1584 u_freeentry(uep, uep->ue_size);
1585 }
1586
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001587 U_FREE_LINE((char_u *)uhp);
1588 --buf->b_u_numhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589}
1590
1591/*
1592 * free entry 'uep' and 'n' lines in uep->ue_array[]
1593 */
1594 static void
1595u_freeentry(uep, n)
1596 u_entry_T *uep;
1597 long n;
1598{
Bram Moolenaar8d343302005-07-12 22:46:17 +00001599 while (n > 0)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001600 U_FREE_LINE(uep->ue_array[--n]);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001601 U_FREE_LINE((char_u *)uep->ue_array);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001602 U_FREE_LINE((char_u *)uep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603}
1604
1605/*
1606 * invalidate the undo buffer; called when storage has already been released
1607 */
1608 void
1609u_clearall(buf)
1610 buf_T *buf;
1611{
1612 buf->b_u_newhead = buf->b_u_oldhead = buf->b_u_curhead = NULL;
1613 buf->b_u_synced = TRUE;
1614 buf->b_u_numhead = 0;
1615 buf->b_u_line_ptr = NULL;
1616 buf->b_u_line_lnum = 0;
1617}
1618
1619/*
1620 * save the line "lnum" for the "U" command
1621 */
1622 void
1623u_saveline(lnum)
1624 linenr_T lnum;
1625{
1626 if (lnum == curbuf->b_u_line_lnum) /* line is already saved */
1627 return;
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001628 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) /* should never happen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 return;
1630 u_clearline();
1631 curbuf->b_u_line_lnum = lnum;
1632 if (curwin->w_cursor.lnum == lnum)
1633 curbuf->b_u_line_colnr = curwin->w_cursor.col;
1634 else
1635 curbuf->b_u_line_colnr = 0;
1636 if ((curbuf->b_u_line_ptr = u_save_line(lnum)) == NULL)
1637 do_outofmem_msg((long_u)0);
1638}
1639
1640/*
1641 * clear the line saved for the "U" command
1642 * (this is used externally for crossing a line while in insert mode)
1643 */
1644 void
1645u_clearline()
1646{
1647 if (curbuf->b_u_line_ptr != NULL)
1648 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001649 U_FREE_LINE(curbuf->b_u_line_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 curbuf->b_u_line_ptr = NULL;
1651 curbuf->b_u_line_lnum = 0;
1652 }
1653}
1654
1655/*
1656 * Implementation of the "U" command.
1657 * Differentiation from vi: "U" can be undone with the next "U".
1658 * We also allow the cursor to be in another line.
1659 */
1660 void
1661u_undoline()
1662{
1663 colnr_T t;
1664 char_u *oldp;
1665
1666 if (undo_off)
1667 return;
1668
1669 if (curbuf->b_u_line_ptr == NULL ||
1670 curbuf->b_u_line_lnum > curbuf->b_ml.ml_line_count)
1671 {
1672 beep_flush();
1673 return;
1674 }
1675 /* first save the line for the 'u' command */
1676 if (u_savecommon(curbuf->b_u_line_lnum - 1,
1677 curbuf->b_u_line_lnum + 1, (linenr_T)0) == FAIL)
1678 return;
1679 oldp = u_save_line(curbuf->b_u_line_lnum);
1680 if (oldp == NULL)
1681 {
1682 do_outofmem_msg((long_u)0);
1683 return;
1684 }
1685 ml_replace(curbuf->b_u_line_lnum, curbuf->b_u_line_ptr, TRUE);
1686 changed_bytes(curbuf->b_u_line_lnum, 0);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001687 U_FREE_LINE(curbuf->b_u_line_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 curbuf->b_u_line_ptr = oldp;
1689
1690 t = curbuf->b_u_line_colnr;
1691 if (curwin->w_cursor.lnum == curbuf->b_u_line_lnum)
1692 curbuf->b_u_line_colnr = curwin->w_cursor.col;
1693 curwin->w_cursor.col = t;
1694 curwin->w_cursor.lnum = curbuf->b_u_line_lnum;
1695}
1696
1697/*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001698 * There are two implementations of the memory management for undo:
1699 * 1. Use the standard malloc()/free() functions.
1700 * This should be fast for allocating memory, but when a buffer is
1701 * abandoned every single allocated chunk must be freed, which may be slow.
1702 * 2. Allocate larger blocks of memory and keep track of chunks ourselves.
1703 * This is fast for abandoning, but the use of linked lists is slow for
1704 * finding a free chunk. Esp. when a lot of lines are changed or deleted.
1705 * A bit of profiling showed that the first method is faster, especially when
1706 * making a large number of changes, under the condition that malloc()/free()
1707 * is implemented efficiently.
1708 */
1709#ifdef U_USE_MALLOC
1710/*
1711 * Version of undo memory allocation using malloc()/free()
1712 *
1713 * U_FREE_LINE() and U_ALLOC_LINE() are macros that invoke vim_free() and
1714 * lalloc() directly.
1715 */
1716
1717/*
1718 * Free all allocated memory blocks for the buffer 'buf'.
1719 */
1720 void
1721u_blockfree(buf)
1722 buf_T *buf;
1723{
Bram Moolenaar1e607892006-03-13 22:15:53 +00001724 while (buf->b_u_oldhead != NULL)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001725 u_freeheader(buf, buf->b_u_oldhead, NULL);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001726 U_FREE_LINE(buf->b_u_line_ptr);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001727}
1728
1729#else
1730/*
1731 * Storage allocation for the undo lines and blocks of the current file.
1732 * Version where Vim keeps track of the available memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 */
1734
1735/*
1736 * Memory is allocated in relatively large blocks. These blocks are linked
1737 * in the allocated block list, headed by curbuf->b_block_head. They are all
1738 * freed when abandoning a file, so we don't have to free every single line.
1739 * The list is kept sorted on memory address.
1740 * block_alloc() allocates a block.
1741 * m_blockfree() frees all blocks.
1742 *
1743 * The available chunks of memory are kept in free chunk lists. There is
1744 * one free list for each block of allocated memory. The list is kept sorted
1745 * on memory address.
1746 * u_alloc_line() gets a chunk from the free lists.
1747 * u_free_line() returns a chunk to the free lists.
1748 * curbuf->b_m_search points to the chunk before the chunk that was
1749 * freed/allocated the last time.
1750 * curbuf->b_mb_current points to the b_head where curbuf->b_m_search
1751 * points into the free list.
1752 *
1753 *
1754 * b_block_head /---> block #1 /---> block #2
1755 * mb_next ---/ mb_next ---/ mb_next ---> NULL
1756 * mb_info mb_info mb_info
1757 * | | |
1758 * V V V
1759 * NULL free chunk #1.1 free chunk #2.1
1760 * | |
1761 * V V
1762 * free chunk #1.2 NULL
1763 * |
1764 * V
1765 * NULL
1766 *
1767 * When a single free chunk list would have been used, it could take a lot
1768 * of time in u_free_line() to find the correct place to insert a chunk in the
1769 * free list. The single free list would become very long when many lines are
1770 * changed (e.g. with :%s/^M$//).
1771 */
1772
1773 /*
1774 * this blocksize is used when allocating new lines
1775 */
1776#define MEMBLOCKSIZE 2044
1777
1778/*
1779 * The size field contains the size of the chunk, including the size field
1780 * itself.
1781 *
1782 * When the chunk is not in-use it is preceded with the m_info structure.
1783 * The m_next field links it in one of the free chunk lists.
1784 *
1785 * On most unix systems structures have to be longword (32 or 64 bit) aligned.
1786 * On most other systems they are short (16 bit) aligned.
1787 */
1788
1789/* the structure definitions are now in structs.h */
1790
1791#ifdef ALIGN_LONG
1792 /* size of m_size */
1793# define M_OFFSET (sizeof(long_u))
1794#else
1795 /* size of m_size */
1796# define M_OFFSET (sizeof(short_u))
1797#endif
1798
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001799static char_u *u_blockalloc __ARGS((long_u));
1800
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801/*
1802 * Allocate a block of memory and link it in the allocated block list.
1803 */
1804 static char_u *
1805u_blockalloc(size)
1806 long_u size;
1807{
1808 mblock_T *p;
1809 mblock_T *mp, *next;
1810
1811 p = (mblock_T *)lalloc(size + sizeof(mblock_T), FALSE);
1812 if (p != NULL)
1813 {
1814 /* Insert the block into the allocated block list, keeping it
1815 sorted on address. */
1816 for (mp = &curbuf->b_block_head;
1817 (next = mp->mb_next) != NULL && next < p;
1818 mp = next)
1819 ;
1820 p->mb_next = next; /* link in block list */
1821 p->mb_size = size;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001822 p->mb_maxsize = 0; /* nothing free yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 mp->mb_next = p;
1824 p->mb_info.m_next = NULL; /* clear free list */
1825 p->mb_info.m_size = 0;
1826 curbuf->b_mb_current = p; /* remember current block */
1827 curbuf->b_m_search = NULL;
1828 p++; /* return usable memory */
1829 }
1830 return (char_u *)p;
1831}
1832
1833/*
1834 * free all allocated memory blocks for the buffer 'buf'
1835 */
1836 void
1837u_blockfree(buf)
1838 buf_T *buf;
1839{
1840 mblock_T *p, *np;
1841
1842 for (p = buf->b_block_head.mb_next; p != NULL; p = np)
1843 {
1844 np = p->mb_next;
1845 vim_free(p);
1846 }
1847 buf->b_block_head.mb_next = NULL;
1848 buf->b_m_search = NULL;
1849 buf->b_mb_current = NULL;
1850}
1851
1852/*
1853 * Free a chunk of memory for the current buffer.
1854 * Insert the chunk into the correct free list, keeping it sorted on address.
1855 */
1856 static void
1857u_free_line(ptr, keep)
1858 char_u *ptr;
1859 int keep; /* don't free the block when it's empty */
1860{
1861 minfo_T *next;
1862 minfo_T *prev, *curr;
1863 minfo_T *mp;
1864 mblock_T *nextb;
1865 mblock_T *prevb;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001866 long_u maxsize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867
1868 if (ptr == NULL || ptr == IObuff)
1869 return; /* illegal address can happen in out-of-memory situations */
1870
1871 mp = (minfo_T *)(ptr - M_OFFSET);
1872
1873 /* find block where chunk could be a part off */
1874 /* if we change curbuf->b_mb_current, curbuf->b_m_search is set to NULL */
1875 if (curbuf->b_mb_current == NULL || mp < (minfo_T *)curbuf->b_mb_current)
1876 {
1877 curbuf->b_mb_current = curbuf->b_block_head.mb_next;
1878 curbuf->b_m_search = NULL;
1879 }
1880 if ((nextb = curbuf->b_mb_current->mb_next) != NULL
1881 && (minfo_T *)nextb < mp)
1882 {
1883 curbuf->b_mb_current = nextb;
1884 curbuf->b_m_search = NULL;
1885 }
1886 while ((nextb = curbuf->b_mb_current->mb_next) != NULL
1887 && (minfo_T *)nextb < mp)
1888 curbuf->b_mb_current = nextb;
1889
1890 curr = NULL;
1891 /*
1892 * If mp is smaller than curbuf->b_m_search->m_next go to the start of
1893 * the free list
1894 */
1895 if (curbuf->b_m_search == NULL || mp < (curbuf->b_m_search->m_next))
1896 next = &(curbuf->b_mb_current->mb_info);
1897 else
1898 next = curbuf->b_m_search;
1899 /*
1900 * The following loop is executed very often.
1901 * Therefore it has been optimized at the cost of readability.
1902 * Keep it fast!
1903 */
1904#ifdef SLOW_BUT_EASY_TO_READ
1905 do
1906 {
1907 prev = curr;
1908 curr = next;
1909 next = next->m_next;
1910 }
1911 while (mp > next && next != NULL);
1912#else
1913 do /* first, middle, last */
1914 {
1915 prev = next->m_next; /* curr, next, prev */
1916 if (prev == NULL || mp <= prev)
1917 {
1918 prev = curr;
1919 curr = next;
1920 next = next->m_next;
1921 break;
1922 }
1923 curr = prev->m_next; /* next, prev, curr */
1924 if (curr == NULL || mp <= curr)
1925 {
1926 prev = next;
1927 curr = prev->m_next;
1928 next = curr->m_next;
1929 break;
1930 }
1931 next = curr->m_next; /* prev, curr, next */
1932 }
1933 while (mp > next && next != NULL);
1934#endif
1935
1936 /* if *mp and *next are concatenated, join them into one chunk */
1937 if ((char_u *)mp + mp->m_size == (char_u *)next)
1938 {
1939 mp->m_size += next->m_size;
1940 mp->m_next = next->m_next;
1941 }
1942 else
1943 mp->m_next = next;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001944 maxsize = mp->m_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945
1946 /* if *curr and *mp are concatenated, join them */
1947 if (prev != NULL && (char_u *)curr + curr->m_size == (char_u *)mp)
1948 {
1949 curr->m_size += mp->m_size;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001950 maxsize = curr->m_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 curr->m_next = mp->m_next;
1952 curbuf->b_m_search = prev;
1953 }
1954 else
1955 {
1956 curr->m_next = mp;
1957 curbuf->b_m_search = curr; /* put curbuf->b_m_search before freed
1958 chunk */
1959 }
1960
1961 /*
1962 * If the block only containes free memory now, release it.
1963 */
1964 if (!keep && curbuf->b_mb_current->mb_size
1965 == curbuf->b_mb_current->mb_info.m_next->m_size)
1966 {
1967 /* Find the block before the current one to be able to unlink it from
1968 * the list of blocks. */
1969 prevb = &curbuf->b_block_head;
1970 for (nextb = prevb->mb_next; nextb != curbuf->b_mb_current;
1971 nextb = nextb->mb_next)
1972 prevb = nextb;
1973 prevb->mb_next = nextb->mb_next;
1974 vim_free(nextb);
1975 curbuf->b_mb_current = NULL;
1976 curbuf->b_m_search = NULL;
1977 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001978 else if (curbuf->b_mb_current->mb_maxsize < maxsize)
1979 curbuf->b_mb_current->mb_maxsize = maxsize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980}
1981
1982/*
1983 * Allocate and initialize a new line structure with room for at least
1984 * 'size' characters plus a terminating NUL.
1985 */
1986 static char_u *
1987u_alloc_line(size)
1988 unsigned size;
1989{
1990 minfo_T *mp, *mprev, *mp2;
1991 mblock_T *mbp;
1992 int size_align;
1993
1994 /*
1995 * Add room for size field and trailing NUL byte.
1996 * Adjust for minimal size (must be able to store minfo_T
1997 * plus a trailing NUL, so the chunk can be released again)
1998 */
1999 size += M_OFFSET + 1;
2000 if (size < sizeof(minfo_T) + 1)
2001 size = sizeof(minfo_T) + 1;
2002
2003 /*
2004 * round size up for alignment
2005 */
2006 size_align = (size + ALIGN_MASK) & ~ALIGN_MASK;
2007
2008 /*
2009 * If curbuf->b_m_search is NULL (uninitialized free list) start at
2010 * curbuf->b_block_head
2011 */
2012 if (curbuf->b_mb_current == NULL || curbuf->b_m_search == NULL)
2013 {
2014 curbuf->b_mb_current = &curbuf->b_block_head;
2015 curbuf->b_m_search = &(curbuf->b_block_head.mb_info);
2016 }
2017
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002018 /* Search for a block with enough space. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 mbp = curbuf->b_mb_current;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002020 while (mbp->mb_maxsize < size_align)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002022 if (mbp->mb_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 mbp = mbp->mb_next;
2024 else
2025 mbp = &curbuf->b_block_head;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002026 if (mbp == curbuf->b_mb_current)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002028 int n = (size_align > (MEMBLOCKSIZE / 4)
2029 ? size_align : MEMBLOCKSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002031 /* Back where we started in block list: need to add a new block
2032 * with enough space. */
2033 mp = (minfo_T *)u_blockalloc((long_u)n);
2034 if (mp == NULL)
2035 return (NULL);
2036 mp->m_size = n;
2037 u_free_line((char_u *)mp + M_OFFSET, TRUE);
2038 mbp = curbuf->b_mb_current;
2039 break;
2040 }
2041 }
2042 if (mbp != curbuf->b_mb_current)
2043 curbuf->b_m_search = &(mbp->mb_info);
2044
2045 /* In this block find a chunk with enough space. */
2046 mprev = curbuf->b_m_search;
2047 mp = curbuf->b_m_search->m_next;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00002048 for (;;)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002049 {
2050 if (mp == NULL) /* at end of the list */
2051 mp = &(mbp->mb_info); /* wrap around to begin */
2052 if (mp->m_size >= size)
2053 break;
2054 if (mp == curbuf->b_m_search)
2055 {
2056 /* back where we started in free chunk list: "cannot happen" */
2057 EMSG2(_(e_intern2), "u_alloc_line()");
2058 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 }
2060 mprev = mp;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002061 mp = mp->m_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 }
2063
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002064 /* when using the largest chunk adjust mb_maxsize */
2065 if (mp->m_size >= mbp->mb_maxsize)
2066 mbp->mb_maxsize = 0;
2067
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 /* if the chunk we found is large enough, split it up in two */
2069 if ((long)mp->m_size - size_align >= (long)(sizeof(minfo_T) + 1))
2070 {
2071 mp2 = (minfo_T *)((char_u *)mp + size_align);
2072 mp2->m_size = mp->m_size - size_align;
2073 mp2->m_next = mp->m_next;
2074 mprev->m_next = mp2;
2075 mp->m_size = size_align;
2076 }
2077 else /* remove *mp from the free list */
2078 {
2079 mprev->m_next = mp->m_next;
2080 }
2081 curbuf->b_m_search = mprev;
2082 curbuf->b_mb_current = mbp;
2083
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002084 /* If using the largest chunk need to find the new largest chunk */
2085 if (mbp->mb_maxsize == 0)
2086 for (mp2 = &(mbp->mb_info); mp2 != NULL; mp2 = mp2->m_next)
2087 if (mbp->mb_maxsize < mp2->m_size)
2088 mbp->mb_maxsize = mp2->m_size;
2089
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 mp = (minfo_T *)((char_u *)mp + M_OFFSET);
2091 *(char_u *)mp = NUL; /* set the first byte to NUL */
2092
2093 return ((char_u *)mp);
2094}
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002095#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096
2097/*
2098 * u_save_line(): allocate memory with u_alloc_line() and copy line 'lnum'
2099 * into it.
2100 */
2101 static char_u *
2102u_save_line(lnum)
2103 linenr_T lnum;
2104{
2105 char_u *src;
2106 char_u *dst;
2107 unsigned len;
2108
2109 src = ml_get(lnum);
2110 len = (unsigned)STRLEN(src);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002111 if ((dst = U_ALLOC_LINE(len)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 mch_memmove(dst, src, (size_t)(len + 1));
2113 return (dst);
2114}
2115
2116/*
2117 * Check if the 'modified' flag is set, or 'ff' has changed (only need to
2118 * check the first character, because it can only be "dos", "unix" or "mac").
2119 * "nofile" and "scratch" type buffers are considered to always be unchanged.
2120 */
2121 int
2122bufIsChanged(buf)
2123 buf_T *buf;
2124{
2125 return
2126#ifdef FEAT_QUICKFIX
2127 !bt_dontwrite(buf) &&
2128#endif
2129 (buf->b_changed || file_ff_differs(buf));
2130}
2131
2132 int
2133curbufIsChanged()
2134{
2135 return
2136#ifdef FEAT_QUICKFIX
2137 !bt_dontwrite(curbuf) &&
2138#endif
2139 (curbuf->b_changed || file_ff_differs(curbuf));
2140}