blob: 35a907fa8e23898cd7f2a5050ffdd78ce28b2628 [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
Bram Moolenaarfecb6602007-10-01 20:54:15 +000079/* Uncomment the next line for including the u_check() function. This warns
80 * for errors in the debug information. */
81/* #define U_DEBUG 1 */
82#define UH_MAGIC 0x18dade /* value for uh_magic when in use */
83#define UE_MAGIC 0xabc123 /* value for ue_magic when in use */
84
Bram Moolenaar442b4222010-05-24 21:34:22 +020085#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
86# include "vimio.h" /* for vim_read(), must be before vim.h */
87#endif
88
Bram Moolenaar071d4272004-06-13 20:20:40 +000089#include "vim.h"
90
Bram Moolenaar26a60b42005-02-22 08:49:11 +000091/* See below: use malloc()/free() for memory management. */
92#define U_USE_MALLOC 1
93
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +000094static void u_unch_branch __ARGS((u_header_T *uhp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000095static u_entry_T *u_get_headentry __ARGS((void));
96static void u_getbot __ARGS((void));
97static int u_savecommon __ARGS((linenr_T, linenr_T, linenr_T));
98static void u_doit __ARGS((int count));
Bram Moolenaarca003e12006-03-17 23:19:38 +000099static void u_undoredo __ARGS((int undo));
Bram Moolenaardb552d602006-03-23 22:59:57 +0000100static void u_undo_end __ARGS((int did_undo, int absolute));
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000101static void u_add_time __ARGS((char_u *buf, size_t buflen, time_t tt));
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000102static void u_freeheader __ARGS((buf_T *buf, u_header_T *uhp, u_header_T **uhpp));
Bram Moolenaar1e607892006-03-13 22:15:53 +0000103static void u_freebranch __ARGS((buf_T *buf, u_header_T *uhp, u_header_T **uhpp));
104static void u_freeentries __ARGS((buf_T *buf, u_header_T *uhp, u_header_T **uhpp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105static void u_freeentry __ARGS((u_entry_T *, long));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200106#ifdef FEAT_PERSISTENT_UNDO
107static void unserialize_pos __ARGS((pos_T *pos, FILE *fp));
108static void unserialize_visualinfo __ARGS((visualinfo_T *info, FILE *fp));
109static char_u *u_get_undo_file_name __ARGS((char_u *, int reading));
110static int serialize_uep __ARGS((u_entry_T *uep, FILE *fp));
111static void serialize_pos __ARGS((pos_T pos, FILE *fp));
112static void serialize_visualinfo __ARGS((visualinfo_T info, FILE *fp));
113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000115#ifdef U_USE_MALLOC
116# define U_FREE_LINE(ptr) vim_free(ptr)
117# define U_ALLOC_LINE(size) lalloc((long_u)((size) + 1), FALSE)
118#else
119static void u_free_line __ARGS((char_u *ptr, int keep));
120static char_u *u_alloc_line __ARGS((unsigned size));
121# define U_FREE_LINE(ptr) u_free_line((ptr), FALSE)
122# define U_ALLOC_LINE(size) u_alloc_line(size)
123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124static char_u *u_save_line __ARGS((linenr_T));
125
126static long u_newcount, u_oldcount;
127
128/*
129 * When 'u' flag included in 'cpoptions', we behave like vi. Need to remember
130 * the action that "u" should do.
131 */
132static int undo_undoes = FALSE;
133
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200134static int lastmark = 0;
135
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000136#ifdef U_DEBUG
137/*
138 * Check the undo structures for being valid. Print a warning when something
139 * looks wrong.
140 */
141static int seen_b_u_curhead;
142static int seen_b_u_newhead;
143static int header_count;
144
145 static void
146u_check_tree(u_header_T *uhp,
147 u_header_T *exp_uh_next,
148 u_header_T *exp_uh_alt_prev)
149{
150 u_entry_T *uep;
151
152 if (uhp == NULL)
153 return;
154 ++header_count;
155 if (uhp == curbuf->b_u_curhead && ++seen_b_u_curhead > 1)
156 {
157 EMSG("b_u_curhead found twice (looping?)");
158 return;
159 }
160 if (uhp == curbuf->b_u_newhead && ++seen_b_u_newhead > 1)
161 {
162 EMSG("b_u_newhead found twice (looping?)");
163 return;
164 }
165
166 if (uhp->uh_magic != UH_MAGIC)
167 EMSG("uh_magic wrong (may be using freed memory)");
168 else
169 {
170 /* Check pointers back are correct. */
171 if (uhp->uh_next != exp_uh_next)
172 {
173 EMSG("uh_next wrong");
174 smsg((char_u *)"expected: 0x%x, actual: 0x%x",
175 exp_uh_next, uhp->uh_next);
176 }
177 if (uhp->uh_alt_prev != exp_uh_alt_prev)
178 {
179 EMSG("uh_alt_prev wrong");
180 smsg((char_u *)"expected: 0x%x, actual: 0x%x",
181 exp_uh_alt_prev, uhp->uh_alt_prev);
182 }
183
184 /* Check the undo tree at this header. */
185 for (uep = uhp->uh_entry; uep != NULL; uep = uep->ue_next)
186 {
187 if (uep->ue_magic != UE_MAGIC)
188 {
189 EMSG("ue_magic wrong (may be using freed memory)");
190 break;
191 }
192 }
193
194 /* Check the next alt tree. */
195 u_check_tree(uhp->uh_alt_next, uhp->uh_next, uhp);
196
197 /* Check the next header in this branch. */
198 u_check_tree(uhp->uh_prev, uhp, NULL);
199 }
200}
201
202 void
203u_check(int newhead_may_be_NULL)
204{
205 seen_b_u_newhead = 0;
206 seen_b_u_curhead = 0;
207 header_count = 0;
208
209 u_check_tree(curbuf->b_u_oldhead, NULL, NULL);
210
211 if (seen_b_u_newhead == 0 && curbuf->b_u_oldhead != NULL
212 && !(newhead_may_be_NULL && curbuf->b_u_newhead == NULL))
213 EMSGN("b_u_newhead invalid: 0x%x", curbuf->b_u_newhead);
214 if (curbuf->b_u_curhead != NULL && seen_b_u_curhead == 0)
215 EMSGN("b_u_curhead invalid: 0x%x", curbuf->b_u_curhead);
216 if (header_count != curbuf->b_u_numhead)
217 {
218 EMSG("b_u_numhead invalid");
219 smsg((char_u *)"expected: %ld, actual: %ld",
220 (long)header_count, (long)curbuf->b_u_numhead);
221 }
222}
223#endif
224
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000226 * Save the current line for both the "u" and "U" command.
227 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 */
229 int
230u_save_cursor()
231{
232 return (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
233 (linenr_T)(curwin->w_cursor.lnum + 1)));
234}
235
236/*
237 * Save the lines between "top" and "bot" for both the "u" and "U" command.
238 * "top" may be 0 and bot may be curbuf->b_ml.ml_line_count + 1.
239 * Returns FAIL when lines could not be saved, OK otherwise.
240 */
241 int
242u_save(top, bot)
243 linenr_T top, bot;
244{
245 if (undo_off)
246 return OK;
247
248 if (top > curbuf->b_ml.ml_line_count ||
249 top >= bot || bot > curbuf->b_ml.ml_line_count + 1)
250 return FALSE; /* rely on caller to do error messages */
251
252 if (top + 2 == bot)
253 u_saveline((linenr_T)(top + 1));
254
255 return (u_savecommon(top, bot, (linenr_T)0));
256}
257
258/*
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200259 * Save the line "lnum" (used by ":s" and "~" command).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 * The line is replaced, so the new bottom line is lnum + 1.
261 */
262 int
263u_savesub(lnum)
264 linenr_T lnum;
265{
266 if (undo_off)
267 return OK;
268
269 return (u_savecommon(lnum - 1, lnum + 1, lnum + 1));
270}
271
272/*
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200273 * A new line is inserted before line "lnum" (used by :s command).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 * The line is inserted, so the new bottom line is lnum + 1.
275 */
276 int
277u_inssub(lnum)
278 linenr_T lnum;
279{
280 if (undo_off)
281 return OK;
282
283 return (u_savecommon(lnum - 1, lnum, lnum + 1));
284}
285
286/*
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200287 * Save the lines "lnum" - "lnum" + nlines (used by delete command).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 * The lines are deleted, so the new bottom line is lnum, unless the buffer
289 * becomes empty.
290 */
291 int
292u_savedel(lnum, nlines)
293 linenr_T lnum;
294 long nlines;
295{
296 if (undo_off)
297 return OK;
298
299 return (u_savecommon(lnum - 1, lnum + nlines,
300 nlines == curbuf->b_ml.ml_line_count ? 2 : lnum));
301}
302
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000303/*
304 * Return TRUE when undo is allowed. Otherwise give an error message and
305 * return FALSE.
306 */
Bram Moolenaarce6ef252006-07-12 19:49:41 +0000307 int
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000308undo_allowed()
309{
310 /* Don't allow changes when 'modifiable' is off. */
311 if (!curbuf->b_p_ma)
312 {
313 EMSG(_(e_modifiable));
314 return FALSE;
315 }
316
317#ifdef HAVE_SANDBOX
318 /* In the sandbox it's not allowed to change the text. */
319 if (sandbox != 0)
320 {
321 EMSG(_(e_sandbox));
322 return FALSE;
323 }
324#endif
325
326 /* Don't allow changes in the buffer while editing the cmdline. The
327 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000328 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000329 {
330 EMSG(_(e_secure));
331 return FALSE;
332 }
333
334 return TRUE;
335}
336
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 static int
338u_savecommon(top, bot, newbot)
339 linenr_T top, bot;
340 linenr_T newbot;
341{
Bram Moolenaar1e607892006-03-13 22:15:53 +0000342 linenr_T lnum;
343 long i;
344 u_header_T *uhp;
345 u_header_T *old_curhead;
346 u_entry_T *uep;
347 u_entry_T *prev_uep;
348 long size;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000350 /* When making changes is not allowed return FAIL. It's a crude way to
351 * make all change commands fail. */
352 if (!undo_allowed())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000355#ifdef U_DEBUG
356 u_check(FALSE);
357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358#ifdef FEAT_NETBEANS_INTG
359 /*
360 * Netbeans defines areas that cannot be modified. Bail out here when
361 * trying to change text in a guarded area.
362 */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200363 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 {
Bram Moolenaar009b2592004-10-24 19:18:58 +0000365 if (netbeans_is_guarded(top, bot))
366 {
367 EMSG(_(e_guarded));
368 return FAIL;
369 }
370 if (curbuf->b_p_ro)
371 {
372 EMSG(_(e_nbreadonly));
373 return FAIL;
374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 }
376#endif
377
378#ifdef FEAT_AUTOCMD
379 /*
380 * Saving text for undo means we are going to make a change. Give a
381 * warning for a read-only file before making the change, so that the
382 * FileChangedRO event can replace the buffer with a read-write version
383 * (e.g., obtained from a source control system).
384 */
385 change_warning(0);
386#endif
387
388 size = bot - top - 1;
389
390 /*
391 * if curbuf->b_u_synced == TRUE make a new header
392 */
393 if (curbuf->b_u_synced)
394 {
395#ifdef FEAT_JUMPLIST
396 /* Need to create new entry in b_changelist. */
397 curbuf->b_new_change = TRUE;
398#endif
399
Bram Moolenaar1e607892006-03-13 22:15:53 +0000400 if (p_ul >= 0)
401 {
402 /*
403 * Make a new header entry. Do this first so that we don't mess
404 * up the undo info when out of memory.
405 */
406 uhp = (u_header_T *)U_ALLOC_LINE((unsigned)sizeof(u_header_T));
407 if (uhp == NULL)
408 goto nomem;
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000409#ifdef U_DEBUG
410 uhp->uh_magic = UH_MAGIC;
411#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +0000412 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000413 else
414 uhp = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000415
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 /*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000417 * If we undid more than we redid, move the entry lists before and
418 * including curbuf->b_u_curhead to an alternate branch.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 */
Bram Moolenaar1e607892006-03-13 22:15:53 +0000420 old_curhead = curbuf->b_u_curhead;
421 if (old_curhead != NULL)
422 {
423 curbuf->b_u_newhead = old_curhead->uh_next;
424 curbuf->b_u_curhead = NULL;
425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426
427 /*
428 * free headers to keep the size right
429 */
430 while (curbuf->b_u_numhead > p_ul && curbuf->b_u_oldhead != NULL)
Bram Moolenaar1e607892006-03-13 22:15:53 +0000431 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000432 u_header_T *uhfree = curbuf->b_u_oldhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000433
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000434 if (uhfree == old_curhead)
435 /* Can't reconnect the branch, delete all of it. */
436 u_freebranch(curbuf, uhfree, &old_curhead);
437 else if (uhfree->uh_alt_next == NULL)
438 /* There is no branch, only free one header. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000439 u_freeheader(curbuf, uhfree, &old_curhead);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000440 else
441 {
442 /* Free the oldest alternate branch as a whole. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000443 while (uhfree->uh_alt_next != NULL)
444 uhfree = uhfree->uh_alt_next;
445 u_freebranch(curbuf, uhfree, &old_curhead);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000446 }
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000447#ifdef U_DEBUG
448 u_check(TRUE);
449#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +0000450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000452 if (uhp == NULL) /* no undo at all */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 {
Bram Moolenaar1e607892006-03-13 22:15:53 +0000454 if (old_curhead != NULL)
455 u_freebranch(curbuf, old_curhead, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 curbuf->b_u_synced = FALSE;
457 return OK;
458 }
459
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 uhp->uh_prev = NULL;
461 uhp->uh_next = curbuf->b_u_newhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000462 uhp->uh_alt_next = old_curhead;
463 if (old_curhead != NULL)
464 {
Bram Moolenaar89ed3df2007-01-09 19:23:12 +0000465 uhp->uh_alt_prev = old_curhead->uh_alt_prev;
466 if (uhp->uh_alt_prev != NULL)
467 uhp->uh_alt_prev->uh_alt_next = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000468 old_curhead->uh_alt_prev = uhp;
469 if (curbuf->b_u_oldhead == old_curhead)
470 curbuf->b_u_oldhead = uhp;
471 }
Bram Moolenaar89ed3df2007-01-09 19:23:12 +0000472 else
473 uhp->uh_alt_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 if (curbuf->b_u_newhead != NULL)
475 curbuf->b_u_newhead->uh_prev = uhp;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000476
Bram Moolenaarca003e12006-03-17 23:19:38 +0000477 uhp->uh_seq = ++curbuf->b_u_seq_last;
478 curbuf->b_u_seq_cur = uhp->uh_seq;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000479 uhp->uh_time = time(NULL);
480 curbuf->b_u_seq_time = uhp->uh_time + 1;
481
Bram Moolenaar1e607892006-03-13 22:15:53 +0000482 uhp->uh_walk = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 uhp->uh_entry = NULL;
484 uhp->uh_getbot_entry = NULL;
485 uhp->uh_cursor = curwin->w_cursor; /* save cursor pos. for undo */
486#ifdef FEAT_VIRTUALEDIT
487 if (virtual_active() && curwin->w_cursor.coladd > 0)
488 uhp->uh_cursor_vcol = getviscol();
489 else
490 uhp->uh_cursor_vcol = -1;
491#endif
492
493 /* save changed and buffer empty flag for undo */
494 uhp->uh_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
495 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
496
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000497 /* save named marks and Visual marks for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 mch_memmove(uhp->uh_namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000499#ifdef FEAT_VISUAL
500 uhp->uh_visual = curbuf->b_visual;
501#endif
502
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 curbuf->b_u_newhead = uhp;
504 if (curbuf->b_u_oldhead == NULL)
505 curbuf->b_u_oldhead = uhp;
506 ++curbuf->b_u_numhead;
507 }
508 else
509 {
510 if (p_ul < 0) /* no undo at all */
511 return OK;
512
513 /*
514 * When saving a single line, and it has been saved just before, it
515 * doesn't make sense saving it again. Saves a lot of memory when
516 * making lots of changes inside the same line.
517 * This is only possible if the previous change didn't increase or
518 * decrease the number of lines.
519 * Check the ten last changes. More doesn't make sense and takes too
520 * long.
521 */
522 if (size == 1)
523 {
524 uep = u_get_headentry();
525 prev_uep = NULL;
526 for (i = 0; i < 10; ++i)
527 {
528 if (uep == NULL)
529 break;
530
531 /* If lines have been inserted/deleted we give up.
532 * Also when the line was included in a multi-line save. */
533 if ((curbuf->b_u_newhead->uh_getbot_entry != uep
534 ? (uep->ue_top + uep->ue_size + 1
535 != (uep->ue_bot == 0
536 ? curbuf->b_ml.ml_line_count + 1
537 : uep->ue_bot))
538 : uep->ue_lcount != curbuf->b_ml.ml_line_count)
539 || (uep->ue_size > 1
540 && top >= uep->ue_top
541 && top + 2 <= uep->ue_top + uep->ue_size + 1))
542 break;
543
544 /* If it's the same line we can skip saving it again. */
545 if (uep->ue_size == 1 && uep->ue_top == top)
546 {
547 if (i > 0)
548 {
549 /* It's not the last entry: get ue_bot for the last
550 * entry now. Following deleted/inserted lines go to
551 * the re-used entry. */
552 u_getbot();
553 curbuf->b_u_synced = FALSE;
554
555 /* Move the found entry to become the last entry. The
556 * order of undo/redo doesn't matter for the entries
557 * we move it over, since they don't change the line
558 * count and don't include this line. It does matter
559 * for the found entry if the line count is changed by
560 * the executed command. */
561 prev_uep->ue_next = uep->ue_next;
562 uep->ue_next = curbuf->b_u_newhead->uh_entry;
563 curbuf->b_u_newhead->uh_entry = uep;
564 }
565
566 /* The executed command may change the line count. */
567 if (newbot != 0)
568 uep->ue_bot = newbot;
569 else if (bot > curbuf->b_ml.ml_line_count)
570 uep->ue_bot = 0;
571 else
572 {
573 uep->ue_lcount = curbuf->b_ml.ml_line_count;
574 curbuf->b_u_newhead->uh_getbot_entry = uep;
575 }
576 return OK;
577 }
578 prev_uep = uep;
579 uep = uep->ue_next;
580 }
581 }
582
583 /* find line number for ue_bot for previous u_save() */
584 u_getbot();
585 }
586
587#if !defined(UNIX) && !defined(DJGPP) && !defined(WIN32) && !defined(__EMX__)
588 /*
589 * With Amiga and MSDOS 16 bit we can't handle big undo's, because
590 * then u_alloc_line would have to allocate a block larger than 32K
591 */
592 if (size >= 8000)
593 goto nomem;
594#endif
595
596 /*
597 * add lines in front of entry list
598 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000599 uep = (u_entry_T *)U_ALLOC_LINE((unsigned)sizeof(u_entry_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 if (uep == NULL)
601 goto nomem;
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200602 vim_memset(uep, 0, sizeof(u_entry_T));
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000603#ifdef U_DEBUG
604 uep->ue_magic = UE_MAGIC;
605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606
607 uep->ue_size = size;
608 uep->ue_top = top;
609 if (newbot != 0)
610 uep->ue_bot = newbot;
611 /*
612 * Use 0 for ue_bot if bot is below last line.
613 * Otherwise we have to compute ue_bot later.
614 */
615 else if (bot > curbuf->b_ml.ml_line_count)
616 uep->ue_bot = 0;
617 else
618 {
619 uep->ue_lcount = curbuf->b_ml.ml_line_count;
620 curbuf->b_u_newhead->uh_getbot_entry = uep;
621 }
622
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000623 if (size > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000625 if ((uep->ue_array = (char_u **)U_ALLOC_LINE(
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 (unsigned)(sizeof(char_u *) * size))) == NULL)
627 {
628 u_freeentry(uep, 0L);
629 goto nomem;
630 }
631 for (i = 0, lnum = top + 1; i < size; ++i)
632 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000633 fast_breakcheck();
634 if (got_int)
635 {
636 u_freeentry(uep, i);
637 return FAIL;
638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 if ((uep->ue_array[i] = u_save_line(lnum++)) == NULL)
640 {
641 u_freeentry(uep, i);
642 goto nomem;
643 }
644 }
645 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000646 else
647 uep->ue_array = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 uep->ue_next = curbuf->b_u_newhead->uh_entry;
649 curbuf->b_u_newhead->uh_entry = uep;
650 curbuf->b_u_synced = FALSE;
651 undo_undoes = FALSE;
652
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000653#ifdef U_DEBUG
654 u_check(FALSE);
655#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 return OK;
657
658nomem:
659 msg_silent = 0; /* must display the prompt */
660 if (ask_yesno((char_u *)_("No undo possible; continue anyway"), TRUE)
661 == 'y')
662 {
663 undo_off = TRUE; /* will be reset when character typed */
664 return OK;
665 }
666 do_outofmem_msg((long_u)0);
667 return FAIL;
668}
669
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200670#ifdef FEAT_PERSISTENT_UNDO
671
672# define UF_START_MAGIC 0xfeac /* magic at start of undofile */
673# define UF_HEADER_MAGIC 0x5fd0 /* magic at start of header */
674# define UF_END_MAGIC 0xe7aa /* magic after last header */
675# define UF_VERSION 1 /* 2-byte undofile version number */
676
677/*
678 * Compute the hash for the current buffer text into hash[UNDO_HASH_SIZE].
679 */
680 void
681u_compute_hash(hash)
682 char_u *hash;
683{
684 context_sha256_T ctx;
685 linenr_T lnum;
686 char_u *p;
687
688 sha256_start(&ctx);
689 for (lnum = 1; lnum < curbuf->b_ml.ml_line_count; ++lnum)
690 {
691 p = ml_get(lnum);
Bram Moolenaar442b4222010-05-24 21:34:22 +0200692 sha256_update(&ctx, p, (UINT32_T)(STRLEN(p) + 1));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200693 }
694 sha256_finish(&ctx, hash);
695}
696
697/*
698 * Unserialize the pos_T at the current position in fp.
699 */
700 static void
701unserialize_pos(pos, fp)
702 pos_T *pos;
703 FILE *fp;
704{
705 pos->lnum = get4c(fp);
706 pos->col = get4c(fp);
707#ifdef FEAT_VIRTUALEDIT
708 pos->coladd = get4c(fp);
709#else
710 (void)get4c(fp);
711#endif
712}
713
714/*
715 * Unserialize the visualinfo_T at the current position in fp.
716 */
717 static void
718unserialize_visualinfo(info, fp)
719 visualinfo_T *info;
720 FILE *fp;
721{
722 unserialize_pos(&info->vi_start, fp);
723 unserialize_pos(&info->vi_end, fp);
724 info->vi_mode = get4c(fp);
725 info->vi_curswant = get4c(fp);
726}
727
728/*
729 * Return an allocated string of the full path of the target undofile.
730 * When "reading" is TRUE find the file to read, go over all directories in
731 * 'undodir'.
732 * When "reading" is FALSE use the first name where the directory exists.
733 */
734 static char_u *
735u_get_undo_file_name(buf_ffname, reading)
736 char_u *buf_ffname;
737 int reading;
738{
739 char_u *dirp;
740 char_u dir_name[IOSIZE + 1];
741 char_u *munged_name = NULL;
742 char_u *undo_file_name = NULL;
743 int dir_len;
744 char_u *p;
745 struct stat st;
746 char_u *ffname = buf_ffname;
747#ifdef HAVE_READLINK
748 char_u fname_buf[MAXPATHL];
749#endif
750
751 if (ffname == NULL)
752 return NULL;
753
754#ifdef HAVE_READLINK
755 /* Expand symlink in the file name, so that we put the undo file with the
756 * actual file instead of with the symlink. */
757 if (resolve_symlink(ffname, fname_buf) == OK)
758 ffname = fname_buf;
759#endif
760
761 /* Loop over 'undodir'. When reading find the first file that exists.
762 * When not reading use the first directory that exists or ".". */
763 dirp = p_udir;
764 while (*dirp != NUL)
765 {
766 dir_len = copy_option_part(&dirp, dir_name, IOSIZE, ",");
767 if (dir_len == 1 && dir_name[0] == '.')
768 {
769 /* Use same directory as the ffname,
770 * "dir/name" -> "dir/.name.un~" */
Bram Moolenaar442b4222010-05-24 21:34:22 +0200771 undo_file_name = vim_strnsave(ffname, (int)(STRLEN(ffname) + 5));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200772 if (undo_file_name == NULL)
773 break;
774 p = gettail(undo_file_name);
775 mch_memmove(p + 1, p, STRLEN(p) + 1);
776 *p = '.';
777 STRCAT(p, ".un~");
778 }
779 else
780 {
781 dir_name[dir_len] = NUL;
782 if (mch_isdir(dir_name))
783 {
784 if (munged_name == NULL)
785 {
786 munged_name = vim_strsave(ffname);
787 if (munged_name == NULL)
788 return NULL;
789 for (p = munged_name; *p != NUL; mb_ptr_adv(p))
790 if (vim_ispathsep(*p))
791 *p = '%';
792 }
793 undo_file_name = concat_fnames(dir_name, munged_name, TRUE);
794 }
795 }
796
797 /* When reading check if the file exists. */
798 if (undo_file_name != NULL && (!reading
799 || mch_stat((char *)undo_file_name, &st) >= 0))
800 break;
801 vim_free(undo_file_name);
802 undo_file_name = NULL;
803 }
804
805 vim_free(munged_name);
806 return undo_file_name;
807}
808
809/*
810 * Load the undo tree from an undo file.
811 * If "name" is not NULL use it as the undo file name. This also means being
812 * a bit more verbose.
813 * Otherwise use curbuf->b_ffname to generate the undo file name.
814 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text.
815 */
816 void
817u_read_undo(name, hash)
818 char_u *name;
819 char_u *hash;
820{
821 char_u *file_name;
822 FILE *fp;
823 long magic, version, str_len;
824 char_u *line_ptr = NULL;
825 linenr_T line_lnum;
826 colnr_T line_colnr;
827 linenr_T line_count;
828 int uep_len;
829 int line_len;
Bram Moolenaar442b4222010-05-24 21:34:22 +0200830 int num_head = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200831 long old_header_seq, new_header_seq, cur_header_seq;
832 long seq_last, seq_cur;
833 short old_idx = -1, new_idx = -1, cur_idx = -1;
834 long num_read_uhps = 0;
835 time_t seq_time;
836 int i, j;
837 int c;
838 short found_first_uep = 0;
839 char_u **array;
840 char_u *line;
841 u_entry_T *uep, *last_uep, *nuep;
842 u_header_T *uhp;
843 u_header_T **uhp_table = NULL;
844 char_u read_hash[UNDO_HASH_SIZE];
845
846 if (name == NULL)
847 {
848 file_name = u_get_undo_file_name(curbuf->b_ffname, TRUE);
849 if (file_name == NULL)
850 return;
851 }
852 else
853 file_name = name;
854
855 if (p_verbose > 0)
856 smsg((char_u *)_("Reading undo file: %s"), file_name);
857 fp = mch_fopen((char *)file_name, "r");
858 if (fp == NULL)
859 {
860 if (name != NULL || p_verbose > 0)
861 EMSG2(_("E822: Cannot open undo file for reading: %s"), file_name);
862 goto error;
863 }
864
865 /* Begin overall file information */
866 magic = get2c(fp);
867 if (magic != UF_START_MAGIC)
868 {
869 EMSG2(_("E823: Corrupted undo file: %s"), file_name);
870 goto error;
871 }
872 version = get2c(fp);
873 if (version != UF_VERSION)
874 {
875 EMSG2(_("E824: Incompatible undo file: %s"), file_name);
876 goto error;
877 }
878
879 fread(read_hash, UNDO_HASH_SIZE, 1, fp);
880 line_count = (linenr_T)get4c(fp);
881 if (memcmp(hash, read_hash, UNDO_HASH_SIZE) != 0
882 || line_count != curbuf->b_ml.ml_line_count)
883 {
884 if (p_verbose > 0 || name != NULL)
885 {
886 verbose_enter();
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200887 give_warning((char_u *)_("File contents changed, cannot use undo info"), TRUE);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200888 verbose_leave();
889 }
890 goto error;
891 }
892
893 /* Begin undo data for U */
894 str_len = get4c(fp);
895 if (str_len < 0)
896 goto error;
897 else if (str_len > 0)
898 {
899 if ((line_ptr = U_ALLOC_LINE(str_len)) == NULL)
900 goto error;
901 for (i = 0; i < str_len; i++)
902 line_ptr[i] = (char_u)getc(fp);
903 line_ptr[i] = NUL;
904 }
905 line_lnum = (linenr_T)get4c(fp);
906 line_colnr = (colnr_T)get4c(fp);
907
908 /* Begin general undo data */
909 old_header_seq = get4c(fp);
910 new_header_seq = get4c(fp);
911 cur_header_seq = get4c(fp);
912 num_head = get4c(fp);
913 seq_last = get4c(fp);
914 seq_cur = get4c(fp);
915 seq_time = get4c(fp);
916
917 /* uhp_table will store the freshly created undo headers we allocate
918 * until we insert them into curbuf. The table remains sorted by the
919 * sequence numbers of the headers. */
920 uhp_table = (u_header_T **)U_ALLOC_LINE(num_head * sizeof(u_header_T *));
921 if (uhp_table == NULL)
922 goto error;
923 vim_memset(uhp_table, 0, num_head * sizeof(u_header_T *));
924
925 c = get2c(fp);
926 while (c == UF_HEADER_MAGIC)
927 {
928 found_first_uep = 0;
929 uhp = (u_header_T *)U_ALLOC_LINE((unsigned)sizeof(u_header_T));
930 if (uhp == NULL)
931 goto error;
932 vim_memset(uhp, 0, sizeof(u_header_T));
933 /* We're not actually trying to store pointers here. We're just storing
934 * IDs so we can swizzle them into pointers later - hence the type
935 * cast. */
Bram Moolenaar442b4222010-05-24 21:34:22 +0200936 uhp->uh_next = (u_header_T *)get4c(fp);
937 uhp->uh_prev = (u_header_T *)get4c(fp);
938 uhp->uh_alt_next = (u_header_T *)get4c(fp);
939 uhp->uh_alt_prev = (u_header_T *)get4c(fp);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200940 uhp->uh_seq = get4c(fp);
941 if (uhp->uh_seq <= 0)
942 {
943 EMSG2(_("E825: Undo file corruption: invalid uh_seq.: %s"),
944 file_name);
945 U_FREE_LINE(uhp);
946 goto error;
947 }
948 uhp->uh_walk = 0;
949 unserialize_pos(&uhp->uh_cursor, fp);
950#ifdef FEAT_VIRTUALEDIT
951 uhp->uh_cursor_vcol = get4c(fp);
952#else
953 (void)get4c(fp);
954#endif
955 uhp->uh_flags = get2c(fp);
956 for (i = 0; i < NMARKS; ++i)
957 unserialize_pos(&uhp->uh_namedm[i], fp);
958#ifdef FEAT_VISUAL
959 unserialize_visualinfo(&uhp->uh_visual, fp);
960#else
961 {
962 visualinfo_T info;
963 unserialize_visualinfo(&info, fp);
964 }
965#endif
966 uhp->uh_time = get4c(fp);
967
968 /* Unserialize uep list. The first 4 bytes is the length of the
969 * entire uep in bytes minus the length of the strings within.
970 * -1 is a sentinel value meaning no more ueps.*/
971 last_uep = NULL;
972 while ((uep_len = get4c(fp)) != -1)
973 {
974 uep = (u_entry_T *)U_ALLOC_LINE((unsigned)sizeof(u_entry_T));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200975 if (uep == NULL)
976 goto error;
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200977 vim_memset(uep, 0, sizeof(u_entry_T));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200978 uep->ue_top = get4c(fp);
979 uep->ue_bot = get4c(fp);
980 uep->ue_lcount = get4c(fp);
981 uep->ue_size = get4c(fp);
982 uep->ue_next = NULL;
983 array = (char_u **)U_ALLOC_LINE(
984 (unsigned)(sizeof(char_u *) * uep->ue_size));
985 for (i = 0; i < uep->ue_size; i++)
986 {
987 line_len = get4c(fp);
988 /* U_ALLOC_LINE provides an extra byte for the NUL terminator.*/
989 line = (char_u *)U_ALLOC_LINE(
990 (unsigned) (sizeof(char_u) * line_len));
991 if (line == NULL)
992 goto error;
993 for (j = 0; j < line_len; j++)
994 {
995 line[j] = getc(fp);
996 }
997 line[j] = '\0';
998 array[i] = line;
999 }
1000 uep->ue_array = array;
1001 if (found_first_uep == 0)
1002 {
1003 uhp->uh_entry = uep;
1004 found_first_uep = 1;
1005 }
1006 else
1007 {
1008 last_uep->ue_next = uep;
1009 }
1010 last_uep = uep;
1011 }
1012
1013 /* Insertion sort the uhp into the table by its uh_seq. This is
1014 * required because, while the number of uhps is limited to
1015 * num_heads, and the uh_seq order is monotonic with respect to
1016 * creation time, the starting uh_seq can be > 0 if any undolevel
1017 * culling was done at undofile write time, and there can be uh_seq
1018 * gaps in the uhps.
1019 */
1020 for (i = num_read_uhps - 1; i >= -1; i--)
1021 {
1022 /* if i == -1, we've hit the leftmost side of the table, so insert
1023 * at uhp_table[0]. */
1024 if (i == -1 || uhp->uh_seq > uhp_table[i]->uh_seq)
1025 {
1026 /* If we've had to move from the rightmost side of the table,
1027 * we have to shift everything to the right by one spot. */
1028 if (i < num_read_uhps - 1)
1029 {
1030 memmove(uhp_table + i + 2, uhp_table + i + 1,
1031 (num_read_uhps - i) * sizeof(u_header_T *));
1032 }
1033 uhp_table[i + 1] = uhp;
1034 break;
1035 }
1036 else if (uhp->uh_seq == uhp_table[i]->uh_seq)
1037 {
1038 EMSG2(_("E826 Undo file corruption: duplicate uh_seq: %s"),
1039 file_name);
1040 goto error;
1041 }
1042 }
1043 num_read_uhps++;
1044 c = get2c(fp);
1045 }
1046
1047 if (c != UF_END_MAGIC)
1048 {
1049 EMSG2(_("E827: Undo file corruption; no end marker: %s"), file_name);
1050 goto error;
1051 }
1052
1053 /* We've organized all of the uhps into a table sorted by uh_seq. Now we
1054 * iterate through the table and swizzle each sequence number we've
1055 * stored in uh_foo into a pointer corresponding to the header with that
1056 * sequence number. Then free curbuf's old undo structure, give curbuf
1057 * the updated {old,new,cur}head pointers, and then free the table. */
1058 for (i = 0; i < num_head; i++)
1059 {
1060 uhp = uhp_table[i];
1061 if (uhp == NULL)
1062 continue;
1063 for (j = 0; j < num_head; j++)
1064 {
1065 if (uhp_table[j] == NULL)
1066 continue;
1067 if (uhp_table[j]->uh_seq == (long)uhp->uh_next)
1068 uhp->uh_next = uhp_table[j];
1069 if (uhp_table[j]->uh_seq == (long)uhp->uh_prev)
1070 uhp->uh_prev = uhp_table[j];
1071 if (uhp_table[j]->uh_seq == (long)uhp->uh_alt_next)
1072 uhp->uh_alt_next = uhp_table[j];
1073 if (uhp_table[j]->uh_seq == (long)uhp->uh_alt_prev)
1074 uhp->uh_alt_prev = uhp_table[j];
1075 }
1076 if (old_header_seq > 0 && old_idx < 0 && uhp->uh_seq == old_header_seq)
1077 old_idx = i;
1078 if (new_header_seq > 0 && new_idx < 0 && uhp->uh_seq == new_header_seq)
1079 new_idx = i;
1080 if (cur_header_seq > 0 && cur_idx < 0 && uhp->uh_seq == cur_header_seq)
1081 cur_idx = i;
1082 }
1083 u_blockfree(curbuf);
1084 curbuf->b_u_oldhead = old_idx < 0 ? 0 : uhp_table[old_idx];
1085 curbuf->b_u_newhead = new_idx < 0 ? 0 : uhp_table[new_idx];
1086 curbuf->b_u_curhead = cur_idx < 0 ? 0 : uhp_table[cur_idx];
1087 curbuf->b_u_line_ptr = line_ptr;
1088 curbuf->b_u_line_lnum = line_lnum;
1089 curbuf->b_u_line_colnr = line_colnr;
1090 curbuf->b_u_numhead = num_head;
1091 curbuf->b_u_seq_last = seq_last;
1092 curbuf->b_u_seq_cur = seq_cur;
1093 curbuf->b_u_seq_time = seq_time;
1094 U_FREE_LINE(uhp_table);
1095#ifdef U_DEBUG
1096 u_check(TRUE);
1097#endif
1098 if (name != NULL)
1099 smsg((char_u *)_("Finished reading undo file %s"), file_name);
1100 goto theend;
1101
1102error:
1103 if (line_ptr != NULL)
1104 U_FREE_LINE(line_ptr);
1105 if (uhp_table != NULL)
1106 {
1107 for (i = 0; i < num_head; i++)
1108 {
1109 if (uhp_table[i] != NULL)
1110 {
1111 uep = uhp_table[i]->uh_entry;
1112 while (uep != NULL)
1113 {
1114 nuep = uep->ue_next;
1115 u_freeentry(uep, uep->ue_size);
1116 uep = nuep;
1117 }
1118 U_FREE_LINE(uhp_table[i]);
1119 }
1120 }
1121 U_FREE_LINE(uhp_table);
1122 }
1123
1124theend:
1125 if (fp != NULL)
1126 fclose(fp);
1127 if (file_name != name)
1128 vim_free(file_name);
1129 return;
1130}
1131
1132/*
1133 * Serialize "uep" to "fp".
1134 */
1135 static int
1136serialize_uep(uep, fp)
1137 u_entry_T *uep;
1138 FILE *fp;
1139{
1140 int i;
1141 int uep_len;
1142 int *entry_lens;
1143
1144 if (uep->ue_size > 0)
1145 entry_lens = (int *)alloc(uep->ue_size * sizeof(int));
Bram Moolenaar442b4222010-05-24 21:34:22 +02001146 else
1147 entry_lens = NULL;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001148
1149 /* Define uep_len to be the size of the entire uep minus the size of its
1150 * component strings, in bytes. The sizes of the component strings
1151 * are written before each individual string.
1152 * We have 4 entries each of 4 bytes, plus ue_size * 4 bytes
1153 * of string size information. */
1154
1155 uep_len = uep->ue_size * 4;
1156 /* Collect sizing information for later serialization. */
1157 for (i = 0; i < uep->ue_size; i++)
1158 {
1159 entry_lens[i] = (int)STRLEN(uep->ue_array[i]);
1160 uep_len += entry_lens[i];
1161 }
1162 put_bytes(fp, (long_u)uep_len, 4);
1163 put_bytes(fp, (long_u)uep->ue_top, 4);
1164 put_bytes(fp, (long_u)uep->ue_bot, 4);
1165 put_bytes(fp, (long_u)uep->ue_lcount, 4);
1166 put_bytes(fp, (long_u)uep->ue_size, 4);
1167 for (i = 0; i < uep->ue_size; i++)
1168 {
1169 if (put_bytes(fp, (long_u)entry_lens[i], 4) == FAIL)
1170 return FAIL;
1171 fprintf(fp, "%s", uep->ue_array[i]);
1172 }
1173 if (uep->ue_size > 0)
1174 vim_free(entry_lens);
1175 return OK;
1176}
1177
1178/*
1179 * Serialize "pos" to "fp".
1180 */
1181 static void
1182serialize_pos(pos, fp)
1183 pos_T pos;
1184 FILE *fp;
1185{
1186 put_bytes(fp, (long_u)pos.lnum, 4);
1187 put_bytes(fp, (long_u)pos.col, 4);
1188#ifdef FEAT_VIRTUALEDIT
1189 put_bytes(fp, (long_u)pos.coladd, 4);
1190#else
1191 put_bytes(fp, (long_u)0, 4);
1192#endif
1193}
1194
1195/*
1196 * Serialize "info" to "fp".
1197 */
1198 static void
1199serialize_visualinfo(info, fp)
1200 visualinfo_T info;
1201 FILE *fp;
1202{
1203 serialize_pos(info.vi_start, fp);
1204 serialize_pos(info.vi_end, fp);
1205 put_bytes(fp, (long_u)info.vi_mode, 4);
1206 put_bytes(fp, (long_u)info.vi_curswant, 4);
1207}
1208
1209static char_u e_not_open[] = N_("E828: Cannot open undo file for writing: %s");
1210
1211/*
1212 * Write the undo tree in an undo file.
1213 * When "name" is not NULL, use it as the name of the undo file.
1214 * Otherwise use buf->b_ffname to generate the undo file name.
1215 * "buf" must never be null, buf->b_ffname is used to obtain the original file
1216 * permissions.
1217 * "forceit" is TRUE for ":wundo!", FALSE otherwise.
1218 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text.
1219 */
1220 void
1221u_write_undo(name, forceit, buf, hash)
1222 char_u *name;
1223 int forceit;
1224 buf_T *buf;
1225 char_u *hash;
1226{
1227 u_header_T *uhp;
1228 u_entry_T *uep;
1229 char_u *file_name;
1230 int str_len, i, uep_len, mark;
1231 int fd;
1232 FILE *fp = NULL;
1233 int perm;
1234 int write_ok = FALSE;
1235#ifdef UNIX
Bram Moolenaar6a244fe2010-05-24 22:02:24 +02001236 int st_old_valid = FALSE;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001237 struct stat st_old;
1238 struct stat st_new;
1239#endif
1240
1241 if (name == NULL)
1242 {
1243 file_name = u_get_undo_file_name(buf->b_ffname, FALSE);
1244 if (file_name == NULL)
1245 return;
1246 }
1247 else
1248 file_name = name;
1249
Bram Moolenaar6a244fe2010-05-24 22:02:24 +02001250 if (buf->b_ffname == NULL)
1251 perm = 0600;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001252 else
Bram Moolenaar6a244fe2010-05-24 22:02:24 +02001253 {
1254#ifdef UNIX
1255 if (mch_stat((char *)buf->b_ffname, &st_old) >= 0)
1256 {
1257 perm = st_old.st_mode;
1258 st_old_valid = TRUE;
1259 }
1260 else
1261 perm = 0600;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001262#else
Bram Moolenaar6a244fe2010-05-24 22:02:24 +02001263 perm = mch_getperm(buf->b_ffname);
1264 if (perm < 0)
1265 perm = 0600;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001266#endif
Bram Moolenaar6a244fe2010-05-24 22:02:24 +02001267 }
1268
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001269 /* set file protection same as original file, but strip s-bit */
1270 perm = perm & 0777;
1271
1272 /* If the undo file exists, verify that it actually is an undo file, and
1273 * delete it. */
1274 if (mch_getperm(file_name) >= 0)
1275 {
1276 if (name == NULL || !forceit)
1277 {
1278 /* Check we can read it and it's an undo file. */
1279 fd = mch_open((char *)file_name, O_RDONLY|O_EXTRA, 0);
1280 if (fd < 0)
1281 {
1282 if (name != NULL || p_verbose > 0)
1283 smsg((char_u *)_("Will not overwrite with undo file, cannot read: %s"),
1284 file_name);
1285 goto theend;
1286 }
1287 else
1288 {
1289 char_u buf[2];
1290
1291 vim_read(fd, buf, 2);
1292 close(fd);
1293 if ((buf[0] << 8) + buf[1] != UF_START_MAGIC)
1294 {
1295 if (name != NULL || p_verbose > 0)
1296 smsg((char_u *)_("Will not overwrite, this is not an undo file: %s"),
1297 file_name);
1298 goto theend;
1299 }
1300 }
1301 }
1302 mch_remove(file_name);
1303 }
1304
1305 fd = mch_open((char *)file_name,
1306 O_CREAT|O_EXTRA|O_WRONLY|O_EXCL|O_NOFOLLOW, perm);
1307 (void)mch_setperm(file_name, perm);
1308 if (fd < 0)
1309 {
1310 EMSG2(_(e_not_open), file_name);
1311 goto theend;
1312 }
1313 if (p_verbose > 0)
1314 smsg((char_u *)_("Writing undo file: %s"), file_name);
1315
1316#ifdef UNIX
1317 /*
1318 * Try to set the group of the undo file same as the original file. If
1319 * this fails, set the protection bits for the group same as the
1320 * protection bits for others.
1321 */
Bram Moolenaar6a244fe2010-05-24 22:02:24 +02001322 if (st_old_valid && (mch_stat((char *)file_name, &st_new) >= 0
1323 && st_new.st_gid != st_old.st_gid
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001324# ifdef HAVE_FCHOWN /* sequent-ptx lacks fchown() */
Bram Moolenaar6a244fe2010-05-24 22:02:24 +02001325 && fchown(fd, (uid_t)-1, st_old.st_gid) != 0)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001326# endif
1327 )
1328 mch_setperm(file_name, (perm & 0707) | ((perm & 07) << 3));
1329# ifdef HAVE_SELINUX
Bram Moolenaar6a244fe2010-05-24 22:02:24 +02001330 if (buf->b_ffname != NULL)
1331 mch_copy_sec(buf->b_ffname, file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001332# endif
1333#endif
1334
1335 fp = fdopen(fd, "w");
1336 if (fp == NULL)
1337 {
1338 EMSG2(_(e_not_open), file_name);
1339 close(fd);
1340 mch_remove(file_name);
1341 goto theend;
1342 }
1343
1344 /* Start writing, first overall file information */
1345 put_bytes(fp, (long_u)UF_START_MAGIC, 2);
1346 put_bytes(fp, (long_u)UF_VERSION, 2);
1347
1348 /* Write a hash of the buffer text, so that we can verify it is still the
1349 * same when reading the buffer text. */
1350 if (fwrite(hash, (size_t)UNDO_HASH_SIZE, (size_t)1, fp) != 1)
1351 goto write_error;
1352 put_bytes(fp, (long_u)buf->b_ml.ml_line_count, 4);
1353
1354 /* Begin undo data for U */
Bram Moolenaar442b4222010-05-24 21:34:22 +02001355 str_len = buf->b_u_line_ptr != NULL ? (int)STRLEN(buf->b_u_line_ptr) : 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001356 put_bytes(fp, (long_u)str_len, 4);
1357 if (str_len > 0 && fwrite(buf->b_u_line_ptr, (size_t)str_len,
1358 (size_t)1, fp) != 1)
1359 goto write_error;
1360
1361 put_bytes(fp, (long_u)buf->b_u_line_lnum, 4);
1362 put_bytes(fp, (long_u)buf->b_u_line_colnr, 4);
1363
1364 /* Begin general undo data */
1365 uhp = buf->b_u_oldhead;
1366 put_bytes(fp, (long_u)(uhp != NULL ? uhp->uh_seq : 0), 4);
1367
1368 uhp = buf->b_u_newhead;
1369 put_bytes(fp, (long_u)(uhp != NULL ? uhp->uh_seq : 0), 4);
1370
1371 uhp = buf->b_u_curhead;
1372 put_bytes(fp, (long_u)(uhp != NULL ? uhp->uh_seq : 0), 4);
1373
1374 put_bytes(fp, (long_u)buf->b_u_numhead, 4);
1375 put_bytes(fp, (long_u)buf->b_u_seq_last, 4);
1376 put_bytes(fp, (long_u)buf->b_u_seq_cur, 4);
1377 put_bytes(fp, (long_u)buf->b_u_seq_time, 4);
1378
1379 /* Iteratively serialize UHPs and their UEPs from the top down. */
1380 mark = ++lastmark;
1381 uhp = buf->b_u_oldhead;
1382 while (uhp != NULL)
1383 {
1384 /* Serialize current UHP if we haven't seen it */
1385 if (uhp->uh_walk != mark)
1386 {
1387 if (put_bytes(fp, (long_u)UF_HEADER_MAGIC, 2) == FAIL)
1388 goto write_error;
1389
1390 put_bytes(fp, (long_u)((uhp->uh_next != NULL)
1391 ? uhp->uh_next->uh_seq : 0), 4);
1392 put_bytes(fp, (long_u)((uhp->uh_prev != NULL)
1393 ? uhp->uh_prev->uh_seq : 0), 4);
1394 put_bytes(fp, (long_u)((uhp->uh_alt_next != NULL)
1395 ? uhp->uh_alt_next->uh_seq : 0), 4);
1396 put_bytes(fp, (long_u)((uhp->uh_alt_prev != NULL)
1397 ? uhp->uh_alt_prev->uh_seq : 0), 4);
1398 put_bytes(fp, uhp->uh_seq, 4);
1399 serialize_pos(uhp->uh_cursor, fp);
1400#ifdef FEAT_VIRTUALEDIT
1401 put_bytes(fp, (long_u)uhp->uh_cursor_vcol, 4);
1402#else
1403 put_bytes(fp, (long_u)0, 4);
1404#endif
1405 put_bytes(fp, (long_u)uhp->uh_flags, 2);
1406 /* Assume NMARKS will stay the same. */
1407 for (i = 0; i < NMARKS; ++i)
1408 {
1409 serialize_pos(uhp->uh_namedm[i], fp);
1410 }
1411#ifdef FEAT_VISUAL
1412 serialize_visualinfo(uhp->uh_visual, fp);
1413#endif
1414 put_bytes(fp, (long_u)uhp->uh_time, 4);
1415
1416 uep = uhp->uh_entry;
1417 while (uep != NULL)
1418 {
1419 if (serialize_uep(uep, fp) == FAIL)
1420 goto write_error;
1421 uep = uep->ue_next;
1422 }
1423 /* Sentinel value: no more ueps */
1424 uep_len = -1;
1425 put_bytes(fp, (long_u)uep_len, 4);
1426 uhp->uh_walk = mark;
1427 }
1428
1429 /* Now walk through the tree - algorithm from undo_time */
1430 if (uhp->uh_prev != NULL && uhp->uh_prev->uh_walk != mark)
1431 uhp = uhp->uh_prev;
1432 else if (uhp->uh_alt_next != NULL && uhp->uh_alt_next->uh_walk != mark)
1433 uhp = uhp->uh_alt_next;
1434 else if (uhp->uh_next != NULL && uhp->uh_alt_prev == NULL
1435 && uhp->uh_next->uh_walk != mark)
1436 uhp = uhp->uh_next;
1437 else if (uhp->uh_alt_prev != NULL)
1438 uhp = uhp->uh_alt_prev;
1439 else
1440 uhp = uhp->uh_next;
1441 }
1442
1443 if (put_bytes(fp, (long_u)UF_END_MAGIC, 2) == OK)
1444 write_ok = TRUE;
1445
1446write_error:
1447 fclose(fp);
1448 if (!write_ok)
1449 EMSG2(_("E829: write error in undo file: %s"), file_name);
1450
1451#if defined(MACOS_CLASSIC) || defined(WIN3264)
Bram Moolenaar6a244fe2010-05-24 22:02:24 +02001452 if (buf->b_ffname != NULL)
1453 (void)mch_copy_file_attribute(buf->b_ffname, file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001454#endif
1455#ifdef HAVE_ACL
Bram Moolenaar6a244fe2010-05-24 22:02:24 +02001456 if (buf->b_ffname != NULL)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001457 {
1458 vim_acl_T acl;
1459
1460 /* For systems that support ACL: get the ACL from the original file. */
1461 acl = mch_get_acl(buf->b_ffname);
1462 mch_set_acl(file_name, acl);
1463 }
1464#endif
1465
1466theend:
1467 if (file_name != name)
1468 vim_free(file_name);
1469}
1470
1471#endif /* FEAT_PERSISTENT_UNDO */
1472
1473
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474/*
1475 * If 'cpoptions' contains 'u': Undo the previous undo or redo (vi compatible).
1476 * If 'cpoptions' does not contain 'u': Always undo.
1477 */
1478 void
1479u_undo(count)
1480 int count;
1481{
1482 /*
1483 * If we get an undo command while executing a macro, we behave like the
1484 * original vi. If this happens twice in one macro the result will not
1485 * be compatible.
1486 */
1487 if (curbuf->b_u_synced == FALSE)
1488 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001489 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 count = 1;
1491 }
1492
1493 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
1494 undo_undoes = TRUE;
1495 else
1496 undo_undoes = !undo_undoes;
1497 u_doit(count);
1498}
1499
1500/*
1501 * If 'cpoptions' contains 'u': Repeat the previous undo or redo.
1502 * If 'cpoptions' does not contain 'u': Always redo.
1503 */
1504 void
1505u_redo(count)
1506 int count;
1507{
1508 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
1509 undo_undoes = FALSE;
1510 u_doit(count);
1511}
1512
1513/*
1514 * Undo or redo, depending on 'undo_undoes', 'count' times.
1515 */
1516 static void
Bram Moolenaarca003e12006-03-17 23:19:38 +00001517u_doit(startcount)
1518 int startcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519{
Bram Moolenaarca003e12006-03-17 23:19:38 +00001520 int count = startcount;
1521
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001522 if (!undo_allowed())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524
1525 u_newcount = 0;
1526 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001527 if (curbuf->b_ml.ml_flags & ML_EMPTY)
1528 u_oldcount = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 while (count--)
1530 {
1531 if (undo_undoes)
1532 {
1533 if (curbuf->b_u_curhead == NULL) /* first undo */
1534 curbuf->b_u_curhead = curbuf->b_u_newhead;
1535 else if (p_ul > 0) /* multi level undo */
1536 /* get next undo */
1537 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_next;
1538 /* nothing to undo */
1539 if (curbuf->b_u_numhead == 0 || curbuf->b_u_curhead == NULL)
1540 {
1541 /* stick curbuf->b_u_curhead at end */
1542 curbuf->b_u_curhead = curbuf->b_u_oldhead;
1543 beep_flush();
Bram Moolenaarca003e12006-03-17 23:19:38 +00001544 if (count == startcount - 1)
1545 {
1546 MSG(_("Already at oldest change"));
1547 return;
1548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 break;
1550 }
1551
Bram Moolenaarca003e12006-03-17 23:19:38 +00001552 u_undoredo(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 }
1554 else
1555 {
1556 if (curbuf->b_u_curhead == NULL || p_ul <= 0)
1557 {
1558 beep_flush(); /* nothing to redo */
Bram Moolenaarca003e12006-03-17 23:19:38 +00001559 if (count == startcount - 1)
1560 {
1561 MSG(_("Already at newest change"));
1562 return;
1563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 break;
1565 }
1566
Bram Moolenaarca003e12006-03-17 23:19:38 +00001567 u_undoredo(FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00001568
1569 /* Advance for next redo. Set "newhead" when at the end of the
1570 * redoable changes. */
1571 if (curbuf->b_u_curhead->uh_prev == NULL)
1572 curbuf->b_u_newhead = curbuf->b_u_curhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_prev;
1574 }
1575 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001576 u_undo_end(undo_undoes, FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00001577}
1578
Bram Moolenaar1e607892006-03-13 22:15:53 +00001579/*
1580 * Undo or redo over the timeline.
1581 * When "step" is negative go back in time, otherwise goes forward in time.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001582 * When "sec" is FALSE make "step" steps, when "sec" is TRUE use "step" as
1583 * seconds.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001584 * When "absolute" is TRUE use "step" as the sequence number to jump to.
1585 * "sec" must be FALSE then.
Bram Moolenaar1e607892006-03-13 22:15:53 +00001586 */
1587 void
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001588undo_time(step, sec, absolute)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001589 long step;
1590 int sec;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001591 int absolute;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001592{
1593 long target;
1594 long closest;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001595 long closest_start;
1596 long closest_seq = 0;
1597 long val;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001598 u_header_T *uhp;
1599 u_header_T *last;
1600 int mark;
1601 int nomark;
1602 int round;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001603 int dosec = sec;
1604 int above = FALSE;
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001605 int did_undo = TRUE;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001606
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001607 /* First make sure the current undoable change is synced. */
1608 if (curbuf->b_u_synced == FALSE)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001609 u_sync(TRUE);
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001610
Bram Moolenaar1e607892006-03-13 22:15:53 +00001611 u_newcount = 0;
1612 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001613 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar1e607892006-03-13 22:15:53 +00001614 u_oldcount = -1;
1615
Bram Moolenaarca003e12006-03-17 23:19:38 +00001616 /* "target" is the node below which we want to be.
1617 * Init "closest" to a value we can't reach. */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001618 if (absolute)
1619 {
1620 target = step;
Bram Moolenaarca003e12006-03-17 23:19:38 +00001621 closest = -1;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001622 }
Bram Moolenaarca003e12006-03-17 23:19:38 +00001623 else
Bram Moolenaar1e607892006-03-13 22:15:53 +00001624 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00001625 /* When doing computations with time_t subtract starttime, because
1626 * time_t converted to a long may result in a wrong number. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001627 if (sec)
Bram Moolenaarca003e12006-03-17 23:19:38 +00001628 target = (long)(curbuf->b_u_seq_time - starttime) + step;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001629 else
1630 target = curbuf->b_u_seq_cur + step;
Bram Moolenaarca003e12006-03-17 23:19:38 +00001631 if (step < 0)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001632 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00001633 if (target < 0)
1634 target = 0;
1635 closest = -1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001636 }
1637 else
1638 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00001639 if (sec)
Bram Moolenaardb552d602006-03-23 22:59:57 +00001640 closest = (long)(time(NULL) - starttime + 1);
Bram Moolenaarca003e12006-03-17 23:19:38 +00001641 else
1642 closest = curbuf->b_u_seq_last + 2;
1643 if (target >= closest)
1644 target = closest - 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001645 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00001646 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001647 closest_start = closest;
Bram Moolenaarca003e12006-03-17 23:19:38 +00001648 closest_seq = curbuf->b_u_seq_cur;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001649
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001650 /*
1651 * May do this twice:
Bram Moolenaar1e607892006-03-13 22:15:53 +00001652 * 1. Search for "target", update "closest" to the best match found.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001653 * 2. If "target" not found search for "closest".
1654 *
1655 * When using the closest time we use the sequence number in the second
1656 * round, because there may be several entries with the same time.
1657 */
Bram Moolenaar1e607892006-03-13 22:15:53 +00001658 for (round = 1; round <= 2; ++round)
1659 {
1660 /* Find the path from the current state to where we want to go. The
1661 * desired state can be anywhere in the undo tree, need to go all over
1662 * it. We put "nomark" in uh_walk where we have been without success,
1663 * "mark" where it could possibly be. */
1664 mark = ++lastmark;
1665 nomark = ++lastmark;
1666
1667 if (curbuf->b_u_curhead == NULL) /* at leaf of the tree */
1668 uhp = curbuf->b_u_newhead;
1669 else
1670 uhp = curbuf->b_u_curhead;
1671
1672 while (uhp != NULL)
1673 {
1674 uhp->uh_walk = mark;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001675 val = (long)(dosec ? (uhp->uh_time - starttime) : uhp->uh_seq);
Bram Moolenaar1e607892006-03-13 22:15:53 +00001676
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001677 if (round == 1)
1678 {
1679 /* Remember the header that is closest to the target.
1680 * It must be at least in the right direction (checked with
Bram Moolenaarca003e12006-03-17 23:19:38 +00001681 * "b_u_seq_cur"). When the timestamp is equal find the
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001682 * highest/lowest sequence number. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00001683 if ((step < 0 ? uhp->uh_seq <= curbuf->b_u_seq_cur
1684 : uhp->uh_seq > curbuf->b_u_seq_cur)
1685 && ((dosec && val == closest)
1686 ? (step < 0
1687 ? uhp->uh_seq < closest_seq
1688 : uhp->uh_seq > closest_seq)
1689 : closest == closest_start
1690 || (val > target
1691 ? (closest > target
1692 ? val - target <= closest - target
1693 : val - target <= target - closest)
1694 : (closest > target
1695 ? target - val <= closest - target
1696 : target - val <= target - closest))))
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001697 {
1698 closest = val;
1699 closest_seq = uhp->uh_seq;
1700 }
1701 }
1702
1703 /* Quit searching when we found a match. But when searching for a
1704 * time we need to continue looking for the best uh_seq. */
1705 if (target == val && !dosec)
1706 break;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001707
1708 /* go down in the tree if we haven't been there */
1709 if (uhp->uh_prev != NULL && uhp->uh_prev->uh_walk != nomark
1710 && uhp->uh_prev->uh_walk != mark)
1711 uhp = uhp->uh_prev;
1712
1713 /* go to alternate branch if we haven't been there */
1714 else if (uhp->uh_alt_next != NULL
1715 && uhp->uh_alt_next->uh_walk != nomark
1716 && uhp->uh_alt_next->uh_walk != mark)
1717 uhp = uhp->uh_alt_next;
1718
1719 /* go up in the tree if we haven't been there and we are at the
1720 * start of alternate branches */
1721 else if (uhp->uh_next != NULL && uhp->uh_alt_prev == NULL
1722 && uhp->uh_next->uh_walk != nomark
1723 && uhp->uh_next->uh_walk != mark)
Bram Moolenaardb552d602006-03-23 22:59:57 +00001724 {
1725 /* If still at the start we don't go through this change. */
1726 if (uhp == curbuf->b_u_curhead)
1727 uhp->uh_walk = nomark;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001728 uhp = uhp->uh_next;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001729 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00001730
1731 else
1732 {
1733 /* need to backtrack; mark this node as useless */
1734 uhp->uh_walk = nomark;
1735 if (uhp->uh_alt_prev != NULL)
1736 uhp = uhp->uh_alt_prev;
1737 else
1738 uhp = uhp->uh_next;
1739 }
1740 }
1741
1742 if (uhp != NULL) /* found it */
1743 break;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001744
1745 if (absolute)
1746 {
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001747 EMSGN(_("E830: Undo number %ld not found"), step);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001748 return;
1749 }
1750
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001751 if (closest == closest_start)
Bram Moolenaar1e607892006-03-13 22:15:53 +00001752 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001753 if (step < 0)
1754 MSG(_("Already at oldest change"));
1755 else
1756 MSG(_("Already at newest change"));
Bram Moolenaar1e607892006-03-13 22:15:53 +00001757 return;
1758 }
1759
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001760 target = closest_seq;
1761 dosec = FALSE;
1762 if (step < 0)
1763 above = TRUE; /* stop above the header */
Bram Moolenaar1e607892006-03-13 22:15:53 +00001764 }
1765
1766 /* If we found it: Follow the path to go to where we want to be. */
1767 if (uhp != NULL)
1768 {
1769 /*
1770 * First go up the tree as much as needed.
1771 */
1772 for (;;)
1773 {
1774 uhp = curbuf->b_u_curhead;
1775 if (uhp == NULL)
1776 uhp = curbuf->b_u_newhead;
1777 else
Bram Moolenaar1e607892006-03-13 22:15:53 +00001778 uhp = uhp->uh_next;
Bram Moolenaarca003e12006-03-17 23:19:38 +00001779 if (uhp == NULL || uhp->uh_walk != mark
1780 || (uhp->uh_seq == target && !above))
Bram Moolenaar1e607892006-03-13 22:15:53 +00001781 break;
1782 curbuf->b_u_curhead = uhp;
Bram Moolenaarca003e12006-03-17 23:19:38 +00001783 u_undoredo(TRUE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00001784 uhp->uh_walk = nomark; /* don't go back down here */
Bram Moolenaar1e607892006-03-13 22:15:53 +00001785 }
1786
1787 /*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001788 * And now go down the tree (redo), branching off where needed.
Bram Moolenaar1e607892006-03-13 22:15:53 +00001789 */
1790 uhp = curbuf->b_u_curhead;
Bram Moolenaarca003e12006-03-17 23:19:38 +00001791 while (uhp != NULL)
Bram Moolenaar1e607892006-03-13 22:15:53 +00001792 {
Bram Moolenaar89ed3df2007-01-09 19:23:12 +00001793 /* Go back to the first branch with a mark. */
1794 while (uhp->uh_alt_prev != NULL
1795 && uhp->uh_alt_prev->uh_walk == mark)
1796 uhp = uhp->uh_alt_prev;
1797
Bram Moolenaar1e607892006-03-13 22:15:53 +00001798 /* Find the last branch with a mark, that's the one. */
1799 last = uhp;
1800 while (last->uh_alt_next != NULL
1801 && last->uh_alt_next->uh_walk == mark)
1802 last = last->uh_alt_next;
1803 if (last != uhp)
1804 {
1805 /* Make the used branch the first entry in the list of
1806 * alternatives to make "u" and CTRL-R take this branch. */
Bram Moolenaar89ed3df2007-01-09 19:23:12 +00001807 while (uhp->uh_alt_prev != NULL)
1808 uhp = uhp->uh_alt_prev;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001809 if (last->uh_alt_next != NULL)
1810 last->uh_alt_next->uh_alt_prev = last->uh_alt_prev;
1811 last->uh_alt_prev->uh_alt_next = last->uh_alt_next;
1812 last->uh_alt_prev = NULL;
1813 last->uh_alt_next = uhp;
1814 uhp->uh_alt_prev = last;
1815
1816 uhp = last;
Bram Moolenaarca003e12006-03-17 23:19:38 +00001817 if (uhp->uh_next != NULL)
1818 uhp->uh_next->uh_prev = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001819 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001820 curbuf->b_u_curhead = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001821
1822 if (uhp->uh_walk != mark)
1823 break; /* must have reached the target */
1824
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001825 /* Stop when going backwards in time and didn't find the exact
1826 * header we were looking for. */
1827 if (uhp->uh_seq == target && above)
Bram Moolenaardb552d602006-03-23 22:59:57 +00001828 {
1829 curbuf->b_u_seq_cur = target - 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001830 break;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001831 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001832
Bram Moolenaarca003e12006-03-17 23:19:38 +00001833 u_undoredo(FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00001834
1835 /* Advance "curhead" to below the header we last used. If it
1836 * becomes NULL then we need to set "newhead" to this leaf. */
1837 if (uhp->uh_prev == NULL)
1838 curbuf->b_u_newhead = uhp;
1839 curbuf->b_u_curhead = uhp->uh_prev;
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001840 did_undo = FALSE;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001841
1842 if (uhp->uh_seq == target) /* found it! */
1843 break;
1844
1845 uhp = uhp->uh_prev;
1846 if (uhp == NULL || uhp->uh_walk != mark)
1847 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00001848 /* Need to redo more but can't find it... */
Bram Moolenaar1e607892006-03-13 22:15:53 +00001849 EMSG2(_(e_intern2), "undo_time()");
1850 break;
1851 }
1852 }
1853 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001854 u_undo_end(did_undo, absolute);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855}
1856
1857/*
1858 * u_undoredo: common code for undo and redo
1859 *
1860 * The lines in the file are replaced by the lines in the entry list at
1861 * curbuf->b_u_curhead. The replaced lines in the file are saved in the entry
1862 * list for the next undo/redo.
Bram Moolenaarca003e12006-03-17 23:19:38 +00001863 *
1864 * When "undo" is TRUE we go up in the tree, when FALSE we go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 */
1866 static void
Bram Moolenaarca003e12006-03-17 23:19:38 +00001867u_undoredo(undo)
1868 int undo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869{
1870 char_u **newarray = NULL;
1871 linenr_T oldsize;
1872 linenr_T newsize;
1873 linenr_T top, bot;
1874 linenr_T lnum;
1875 linenr_T newlnum = MAXLNUM;
1876 long i;
1877 u_entry_T *uep, *nuep;
1878 u_entry_T *newlist = NULL;
1879 int old_flags;
1880 int new_flags;
1881 pos_T namedm[NMARKS];
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001882#ifdef FEAT_VISUAL
1883 visualinfo_T visualinfo;
1884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 int empty_buffer; /* buffer became empty */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001886 u_header_T *curhead = curbuf->b_u_curhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887
Bram Moolenaarfecb6602007-10-01 20:54:15 +00001888#ifdef U_DEBUG
1889 u_check(FALSE);
1890#endif
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001891 old_flags = curhead->uh_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 new_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
1893 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
1894 setpcmark();
1895
1896 /*
1897 * save marks before undo/redo
1898 */
1899 mch_memmove(namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001900#ifdef FEAT_VISUAL
1901 visualinfo = curbuf->b_visual;
1902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 curbuf->b_op_start.lnum = curbuf->b_ml.ml_line_count;
1904 curbuf->b_op_start.col = 0;
1905 curbuf->b_op_end.lnum = 0;
1906 curbuf->b_op_end.col = 0;
1907
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001908 for (uep = curhead->uh_entry; uep != NULL; uep = nuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 {
1910 top = uep->ue_top;
1911 bot = uep->ue_bot;
1912 if (bot == 0)
1913 bot = curbuf->b_ml.ml_line_count + 1;
1914 if (top > curbuf->b_ml.ml_line_count || top >= bot
1915 || bot > curbuf->b_ml.ml_line_count + 1)
1916 {
1917 EMSG(_("E438: u_undo: line numbers wrong"));
1918 changed(); /* don't want UNCHANGED now */
1919 return;
1920 }
1921
1922 oldsize = bot - top - 1; /* number of lines before undo */
1923 newsize = uep->ue_size; /* number of lines after undo */
1924
1925 if (top < newlnum)
1926 {
1927 /* If the saved cursor is somewhere in this undo block, move it to
1928 * the remembered position. Makes "gwap" put the cursor back
1929 * where it was. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001930 lnum = curhead->uh_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 if (lnum >= top && lnum <= top + newsize + 1)
1932 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001933 curwin->w_cursor = curhead->uh_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 newlnum = curwin->w_cursor.lnum - 1;
1935 }
1936 else
1937 {
1938 /* Use the first line that actually changed. Avoids that
1939 * undoing auto-formatting puts the cursor in the previous
1940 * line. */
1941 for (i = 0; i < newsize && i < oldsize; ++i)
1942 if (STRCMP(uep->ue_array[i], ml_get(top + 1 + i)) != 0)
1943 break;
1944 if (i == newsize && newlnum == MAXLNUM && uep->ue_next == NULL)
1945 {
1946 newlnum = top;
1947 curwin->w_cursor.lnum = newlnum + 1;
1948 }
1949 else if (i < newsize)
1950 {
1951 newlnum = top + i;
1952 curwin->w_cursor.lnum = newlnum + 1;
1953 }
1954 }
1955 }
1956
1957 empty_buffer = FALSE;
1958
1959 /* delete the lines between top and bot and save them in newarray */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001960 if (oldsize > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001962 if ((newarray = (char_u **)U_ALLOC_LINE(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 (unsigned)(sizeof(char_u *) * oldsize))) == NULL)
1964 {
1965 do_outofmem_msg((long_u)(sizeof(char_u *) * oldsize));
1966 /*
1967 * We have messed up the entry list, repair is impossible.
1968 * we have to free the rest of the list.
1969 */
1970 while (uep != NULL)
1971 {
1972 nuep = uep->ue_next;
1973 u_freeentry(uep, uep->ue_size);
1974 uep = nuep;
1975 }
1976 break;
1977 }
1978 /* delete backwards, it goes faster in most cases */
1979 for (lnum = bot - 1, i = oldsize; --i >= 0; --lnum)
1980 {
1981 /* what can we do when we run out of memory? */
1982 if ((newarray[i] = u_save_line(lnum)) == NULL)
1983 do_outofmem_msg((long_u)0);
1984 /* remember we deleted the last line in the buffer, and a
1985 * dummy empty line will be inserted */
1986 if (curbuf->b_ml.ml_line_count == 1)
1987 empty_buffer = TRUE;
1988 ml_delete(lnum, FALSE);
1989 }
1990 }
Bram Moolenaar8d343302005-07-12 22:46:17 +00001991 else
1992 newarray = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993
1994 /* insert the lines in u_array between top and bot */
1995 if (newsize)
1996 {
1997 for (lnum = top, i = 0; i < newsize; ++i, ++lnum)
1998 {
1999 /*
2000 * If the file is empty, there is an empty line 1 that we
2001 * should get rid of, by replacing it with the new line
2002 */
2003 if (empty_buffer && lnum == 0)
2004 ml_replace((linenr_T)1, uep->ue_array[i], TRUE);
2005 else
2006 ml_append(lnum, uep->ue_array[i], (colnr_T)0, FALSE);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002007 U_FREE_LINE(uep->ue_array[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002009 U_FREE_LINE((char_u *)uep->ue_array);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 }
2011
2012 /* adjust marks */
2013 if (oldsize != newsize)
2014 {
2015 mark_adjust(top + 1, top + oldsize, (long)MAXLNUM,
2016 (long)newsize - (long)oldsize);
2017 if (curbuf->b_op_start.lnum > top + oldsize)
2018 curbuf->b_op_start.lnum += newsize - oldsize;
2019 if (curbuf->b_op_end.lnum > top + oldsize)
2020 curbuf->b_op_end.lnum += newsize - oldsize;
2021 }
2022
2023 changed_lines(top + 1, 0, bot, newsize - oldsize);
2024
2025 /* set '[ and '] mark */
2026 if (top + 1 < curbuf->b_op_start.lnum)
2027 curbuf->b_op_start.lnum = top + 1;
2028 if (newsize == 0 && top + 1 > curbuf->b_op_end.lnum)
2029 curbuf->b_op_end.lnum = top + 1;
2030 else if (top + newsize > curbuf->b_op_end.lnum)
2031 curbuf->b_op_end.lnum = top + newsize;
2032
2033 u_newcount += newsize;
2034 u_oldcount += oldsize;
2035 uep->ue_size = oldsize;
2036 uep->ue_array = newarray;
2037 uep->ue_bot = top + newsize + 1;
2038
2039 /*
2040 * insert this entry in front of the new entry list
2041 */
2042 nuep = uep->ue_next;
2043 uep->ue_next = newlist;
2044 newlist = uep;
2045 }
2046
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002047 curhead->uh_entry = newlist;
2048 curhead->uh_flags = new_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 if ((old_flags & UH_EMPTYBUF) && bufempty())
2050 curbuf->b_ml.ml_flags |= ML_EMPTY;
2051 if (old_flags & UH_CHANGED)
2052 changed();
2053 else
Bram Moolenaar009b2592004-10-24 19:18:58 +00002054#ifdef FEAT_NETBEANS_INTG
2055 /* per netbeans undo rules, keep it as modified */
2056 if (!isNetbeansModified(curbuf))
2057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 unchanged(curbuf, FALSE);
2059
2060 /*
2061 * restore marks from before undo/redo
2062 */
2063 for (i = 0; i < NMARKS; ++i)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002064 if (curhead->uh_namedm[i].lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002066 curbuf->b_namedm[i] = curhead->uh_namedm[i];
2067 curhead->uh_namedm[i] = namedm[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002069#ifdef FEAT_VISUAL
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002070 if (curhead->uh_visual.vi_start.lnum != 0)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002071 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002072 curbuf->b_visual = curhead->uh_visual;
2073 curhead->uh_visual = visualinfo;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002074 }
2075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076
2077 /*
2078 * If the cursor is only off by one line, put it at the same position as
2079 * before starting the change (for the "o" command).
2080 * Otherwise the cursor should go to the first undone line.
2081 */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002082 if (curhead->uh_cursor.lnum + 1 == curwin->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 && curwin->w_cursor.lnum > 1)
2084 --curwin->w_cursor.lnum;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002085 if (curhead->uh_cursor.lnum == curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002087 curwin->w_cursor.col = curhead->uh_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002089 if (virtual_active() && curhead->uh_cursor_vcol >= 0)
2090 coladvance((colnr_T)curhead->uh_cursor_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 else
2092 curwin->w_cursor.coladd = 0;
2093#endif
2094 }
2095 else if (curwin->w_cursor.lnum <= curbuf->b_ml.ml_line_count)
2096 beginline(BL_SOL | BL_FIX);
2097 else
2098 {
2099 /* We get here with the current cursor line being past the end (eg
2100 * after adding lines at the end of the file, and then undoing it).
2101 * check_cursor() will move the cursor to the last line. Move it to
2102 * the first column here. */
2103 curwin->w_cursor.col = 0;
2104#ifdef FEAT_VIRTUALEDIT
2105 curwin->w_cursor.coladd = 0;
2106#endif
2107 }
2108
2109 /* Make sure the cursor is on an existing line and column. */
2110 check_cursor();
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002111
2112 /* Remember where we are for "g-" and ":earlier 10s". */
2113 curbuf->b_u_seq_cur = curhead->uh_seq;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002114 if (undo)
2115 /* We are below the previous undo. However, to make ":earlier 1s"
2116 * work we compute this as being just above the just undone change. */
2117 --curbuf->b_u_seq_cur;
2118
2119 /* The timestamp can be the same for multiple changes, just use the one of
2120 * the undone/redone change. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002121 curbuf->b_u_seq_time = curhead->uh_time;
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002122#ifdef U_DEBUG
2123 u_check(FALSE);
2124#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125}
2126
2127/*
2128 * If we deleted or added lines, report the number of less/more lines.
2129 * Otherwise, report the number of changes (this may be incorrect
2130 * in some cases, but it's better than nothing).
2131 */
2132 static void
Bram Moolenaardb552d602006-03-23 22:59:57 +00002133u_undo_end(did_undo, absolute)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002134 int did_undo; /* just did an undo */
Bram Moolenaardb552d602006-03-23 22:59:57 +00002135 int absolute; /* used ":undo N" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136{
Bram Moolenaar89d40322006-08-29 15:30:07 +00002137 char *msgstr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002138 u_header_T *uhp;
2139 char_u msgbuf[80];
Bram Moolenaar1e607892006-03-13 22:15:53 +00002140
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141#ifdef FEAT_FOLDING
2142 if ((fdo_flags & FDO_UNDO) && KeyTyped)
2143 foldOpenCursor();
2144#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +00002145
2146 if (global_busy /* no messages now, wait until global is finished */
2147 || !messaging()) /* 'lazyredraw' set, don't do messages now */
2148 return;
2149
2150 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2151 --u_newcount;
2152
2153 u_oldcount -= u_newcount;
2154 if (u_oldcount == -1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002155 msgstr = N_("more line");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002156 else if (u_oldcount < 0)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002157 msgstr = N_("more lines");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002158 else if (u_oldcount == 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002159 msgstr = N_("line less");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002160 else if (u_oldcount > 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002161 msgstr = N_("fewer lines");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002162 else
2163 {
2164 u_oldcount = u_newcount;
2165 if (u_newcount == 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002166 msgstr = N_("change");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002167 else
Bram Moolenaar89d40322006-08-29 15:30:07 +00002168 msgstr = N_("changes");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002169 }
2170
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002171 if (curbuf->b_u_curhead != NULL)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002172 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00002173 /* For ":undo N" we prefer a "after #N" message. */
2174 if (absolute && curbuf->b_u_curhead->uh_next != NULL)
2175 {
2176 uhp = curbuf->b_u_curhead->uh_next;
2177 did_undo = FALSE;
2178 }
2179 else if (did_undo)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002180 uhp = curbuf->b_u_curhead;
2181 else
2182 uhp = curbuf->b_u_curhead->uh_next;
2183 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002184 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002185 uhp = curbuf->b_u_newhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002186
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002187 if (uhp == NULL)
2188 *msgbuf = NUL;
2189 else
2190 u_add_time(msgbuf, sizeof(msgbuf), uhp->uh_time);
2191
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002192 smsg((char_u *)_("%ld %s; %s #%ld %s"),
Bram Moolenaarca003e12006-03-17 23:19:38 +00002193 u_oldcount < 0 ? -u_oldcount : u_oldcount,
Bram Moolenaar89d40322006-08-29 15:30:07 +00002194 _(msgstr),
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002195 did_undo ? _("before") : _("after"),
2196 uhp == NULL ? 0L : uhp->uh_seq,
2197 msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198}
2199
2200/*
2201 * u_sync: stop adding to the current entry list
2202 */
2203 void
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002204u_sync(force)
2205 int force; /* Also sync when no_u_sync is set. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206{
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002207 /* Skip it when already synced or syncing is disabled. */
2208 if (curbuf->b_u_synced || (!force && no_u_sync > 0))
2209 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2211 if (im_is_preediting())
2212 return; /* XIM is busy, don't break an undo sequence */
2213#endif
2214 if (p_ul < 0)
2215 curbuf->b_u_synced = TRUE; /* no entries, nothing to do */
2216 else
2217 {
2218 u_getbot(); /* compute ue_bot of previous u_save */
2219 curbuf->b_u_curhead = NULL;
2220 }
2221}
2222
2223/*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002224 * ":undolist": List the leafs of the undo tree
2225 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002226 void
2227ex_undolist(eap)
Bram Moolenaarfff2bee2010-05-15 13:56:02 +02002228 exarg_T *eap UNUSED;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002229{
2230 garray_T ga;
2231 u_header_T *uhp;
2232 int mark;
2233 int nomark;
2234 int changes = 1;
2235 int i;
2236
2237 /*
2238 * 1: walk the tree to find all leafs, put the info in "ga".
2239 * 2: sort the lines
2240 * 3: display the list
2241 */
2242 mark = ++lastmark;
2243 nomark = ++lastmark;
2244 ga_init2(&ga, (int)sizeof(char *), 20);
2245
2246 uhp = curbuf->b_u_oldhead;
2247 while (uhp != NULL)
2248 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002249 if (uhp->uh_prev == NULL && uhp->uh_walk != nomark
2250 && uhp->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002251 {
2252 if (ga_grow(&ga, 1) == FAIL)
2253 break;
2254 vim_snprintf((char *)IObuff, IOSIZE, "%6ld %7ld ",
2255 uhp->uh_seq, changes);
2256 u_add_time(IObuff + STRLEN(IObuff), IOSIZE - STRLEN(IObuff),
2257 uhp->uh_time);
2258 ((char_u **)(ga.ga_data))[ga.ga_len++] = vim_strsave(IObuff);
2259 }
2260
2261 uhp->uh_walk = mark;
2262
2263 /* go down in the tree if we haven't been there */
2264 if (uhp->uh_prev != NULL && uhp->uh_prev->uh_walk != nomark
2265 && uhp->uh_prev->uh_walk != mark)
2266 {
2267 uhp = uhp->uh_prev;
2268 ++changes;
2269 }
2270
2271 /* go to alternate branch if we haven't been there */
2272 else if (uhp->uh_alt_next != NULL
2273 && uhp->uh_alt_next->uh_walk != nomark
2274 && uhp->uh_alt_next->uh_walk != mark)
2275 uhp = uhp->uh_alt_next;
2276
2277 /* go up in the tree if we haven't been there and we are at the
2278 * start of alternate branches */
2279 else if (uhp->uh_next != NULL && uhp->uh_alt_prev == NULL
2280 && uhp->uh_next->uh_walk != nomark
2281 && uhp->uh_next->uh_walk != mark)
2282 {
2283 uhp = uhp->uh_next;
2284 --changes;
2285 }
2286
2287 else
2288 {
2289 /* need to backtrack; mark this node as done */
2290 uhp->uh_walk = nomark;
2291 if (uhp->uh_alt_prev != NULL)
2292 uhp = uhp->uh_alt_prev;
2293 else
2294 {
2295 uhp = uhp->uh_next;
2296 --changes;
2297 }
2298 }
2299 }
2300
2301 if (ga.ga_len == 0)
2302 MSG(_("Nothing to undo"));
2303 else
2304 {
2305 sort_strings((char_u **)ga.ga_data, ga.ga_len);
2306
2307 msg_start();
2308 msg_puts_attr((char_u *)_("number changes time"), hl_attr(HLF_T));
2309 for (i = 0; i < ga.ga_len && !got_int; ++i)
2310 {
2311 msg_putchar('\n');
2312 if (got_int)
2313 break;
2314 msg_puts(((char_u **)ga.ga_data)[i]);
2315 }
2316 msg_end();
2317
2318 ga_clear_strings(&ga);
2319 }
2320}
2321
2322/*
2323 * Put the timestamp of an undo header in "buf[buflen]" in a nice format.
2324 */
2325 static void
2326u_add_time(buf, buflen, tt)
2327 char_u *buf;
2328 size_t buflen;
2329 time_t tt;
2330{
2331#ifdef HAVE_STRFTIME
2332 struct tm *curtime;
2333
2334 if (time(NULL) - tt >= 100)
2335 {
2336 curtime = localtime(&tt);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00002337 (void)strftime((char *)buf, buflen, "%H:%M:%S", curtime);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002338 }
2339 else
2340#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002341 vim_snprintf((char *)buf, buflen, _("%ld seconds ago"),
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002342 (long)(time(NULL) - tt));
2343}
2344
2345/*
Bram Moolenaare224ffa2006-03-01 00:01:28 +00002346 * ":undojoin": continue adding to the last entry list
2347 */
Bram Moolenaare224ffa2006-03-01 00:01:28 +00002348 void
2349ex_undojoin(eap)
Bram Moolenaarfff2bee2010-05-15 13:56:02 +02002350 exarg_T *eap UNUSED;
Bram Moolenaare224ffa2006-03-01 00:01:28 +00002351{
Bram Moolenaare224ffa2006-03-01 00:01:28 +00002352 if (curbuf->b_u_newhead == NULL)
2353 return; /* nothing changed before */
Bram Moolenaar57657d82006-04-21 22:12:41 +00002354 if (curbuf->b_u_curhead != NULL)
2355 {
2356 EMSG(_("E790: undojoin is not allowed after undo"));
2357 return;
2358 }
2359 if (!curbuf->b_u_synced)
2360 return; /* already unsynced */
Bram Moolenaare224ffa2006-03-01 00:01:28 +00002361 if (p_ul < 0)
2362 return; /* no entries, nothing to do */
2363 else
2364 {
2365 /* Go back to the last entry */
2366 curbuf->b_u_curhead = curbuf->b_u_newhead;
2367 curbuf->b_u_synced = FALSE; /* no entries, nothing to do */
2368 }
2369}
2370
2371/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 * Called after writing the file and setting b_changed to FALSE.
2373 * Now an undo means that the buffer is modified.
2374 */
2375 void
2376u_unchanged(buf)
2377 buf_T *buf;
2378{
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002379 u_unch_branch(buf->b_u_oldhead);
2380 buf->b_did_warn = FALSE;
2381}
2382
2383 static void
2384u_unch_branch(uhp)
2385 u_header_T *uhp;
2386{
Bram Moolenaar1e607892006-03-13 22:15:53 +00002387 u_header_T *uh;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002389 for (uh = uhp; uh != NULL; uh = uh->uh_prev)
2390 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 uh->uh_flags |= UH_CHANGED;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002392 if (uh->uh_alt_next != NULL)
2393 u_unch_branch(uh->uh_alt_next); /* recursive */
2394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395}
2396
2397/*
2398 * Get pointer to last added entry.
2399 * If it's not valid, give an error message and return NULL.
2400 */
2401 static u_entry_T *
2402u_get_headentry()
2403{
2404 if (curbuf->b_u_newhead == NULL || curbuf->b_u_newhead->uh_entry == NULL)
2405 {
2406 EMSG(_("E439: undo list corrupt"));
2407 return NULL;
2408 }
2409 return curbuf->b_u_newhead->uh_entry;
2410}
2411
2412/*
2413 * u_getbot(): compute the line number of the previous u_save
2414 * It is called only when b_u_synced is FALSE.
2415 */
2416 static void
2417u_getbot()
2418{
2419 u_entry_T *uep;
2420 linenr_T extra;
2421
2422 uep = u_get_headentry(); /* check for corrupt undo list */
2423 if (uep == NULL)
2424 return;
2425
2426 uep = curbuf->b_u_newhead->uh_getbot_entry;
2427 if (uep != NULL)
2428 {
2429 /*
2430 * the new ue_bot is computed from the number of lines that has been
2431 * inserted (0 - deleted) since calling u_save. This is equal to the
2432 * old line count subtracted from the current line count.
2433 */
2434 extra = curbuf->b_ml.ml_line_count - uep->ue_lcount;
2435 uep->ue_bot = uep->ue_top + uep->ue_size + 1 + extra;
2436 if (uep->ue_bot < 1 || uep->ue_bot > curbuf->b_ml.ml_line_count)
2437 {
2438 EMSG(_("E440: undo line missing"));
2439 uep->ue_bot = uep->ue_top + 1; /* assume all lines deleted, will
2440 * get all the old lines back
2441 * without deleting the current
2442 * ones */
2443 }
2444
2445 curbuf->b_u_newhead->uh_getbot_entry = NULL;
2446 }
2447
2448 curbuf->b_u_synced = TRUE;
2449}
2450
2451/*
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002452 * Free one header "uhp" and its entry list and adjust the pointers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 */
2454 static void
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002455u_freeheader(buf, uhp, uhpp)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002456 buf_T *buf;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002457 u_header_T *uhp;
2458 u_header_T **uhpp; /* if not NULL reset when freeing this header */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459{
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002460 u_header_T *uhap;
2461
Bram Moolenaar1e607892006-03-13 22:15:53 +00002462 /* When there is an alternate redo list free that branch completely,
2463 * because we can never go there. */
2464 if (uhp->uh_alt_next != NULL)
2465 u_freebranch(buf, uhp->uh_alt_next, uhpp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466
Bram Moolenaar1e607892006-03-13 22:15:53 +00002467 if (uhp->uh_alt_prev != NULL)
2468 uhp->uh_alt_prev->uh_alt_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469
Bram Moolenaar1e607892006-03-13 22:15:53 +00002470 /* Update the links in the list to remove the header. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 if (uhp->uh_next == NULL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002472 buf->b_u_oldhead = uhp->uh_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 else
2474 uhp->uh_next->uh_prev = uhp->uh_prev;
2475
2476 if (uhp->uh_prev == NULL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002477 buf->b_u_newhead = uhp->uh_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 else
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002479 for (uhap = uhp->uh_prev; uhap != NULL; uhap = uhap->uh_alt_next)
2480 uhap->uh_next = uhp->uh_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481
Bram Moolenaar1e607892006-03-13 22:15:53 +00002482 u_freeentries(buf, uhp, uhpp);
2483}
2484
2485/*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002486 * Free an alternate branch and any following alternate branches.
Bram Moolenaar1e607892006-03-13 22:15:53 +00002487 */
2488 static void
2489u_freebranch(buf, uhp, uhpp)
2490 buf_T *buf;
2491 u_header_T *uhp;
2492 u_header_T **uhpp; /* if not NULL reset when freeing this header */
2493{
2494 u_header_T *tofree, *next;
2495
Bram Moolenaar07d06772007-11-10 21:51:15 +00002496 /* If this is the top branch we may need to use u_freeheader() to update
2497 * all the pointers. */
2498 if (uhp == buf->b_u_oldhead)
2499 {
2500 u_freeheader(buf, uhp, uhpp);
2501 return;
2502 }
2503
Bram Moolenaar1e607892006-03-13 22:15:53 +00002504 if (uhp->uh_alt_prev != NULL)
2505 uhp->uh_alt_prev->uh_alt_next = NULL;
2506
2507 next = uhp;
2508 while (next != NULL)
2509 {
2510 tofree = next;
2511 if (tofree->uh_alt_next != NULL)
2512 u_freebranch(buf, tofree->uh_alt_next, uhpp); /* recursive */
2513 next = tofree->uh_prev;
2514 u_freeentries(buf, tofree, uhpp);
2515 }
2516}
2517
2518/*
2519 * Free all the undo entries for one header and the header itself.
2520 * This means that "uhp" is invalid when returning.
2521 */
2522 static void
2523u_freeentries(buf, uhp, uhpp)
2524 buf_T *buf;
2525 u_header_T *uhp;
2526 u_header_T **uhpp; /* if not NULL reset when freeing this header */
2527{
2528 u_entry_T *uep, *nuep;
2529
2530 /* Check for pointers to the header that become invalid now. */
2531 if (buf->b_u_curhead == uhp)
2532 buf->b_u_curhead = NULL;
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002533 if (buf->b_u_newhead == uhp)
2534 buf->b_u_newhead = NULL; /* freeing the newest entry */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002535 if (uhpp != NULL && uhp == *uhpp)
2536 *uhpp = NULL;
2537
2538 for (uep = uhp->uh_entry; uep != NULL; uep = nuep)
2539 {
2540 nuep = uep->ue_next;
2541 u_freeentry(uep, uep->ue_size);
2542 }
2543
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002544#ifdef U_DEBUG
2545 uhp->uh_magic = 0;
2546#endif
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002547 U_FREE_LINE((char_u *)uhp);
2548 --buf->b_u_numhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549}
2550
2551/*
2552 * free entry 'uep' and 'n' lines in uep->ue_array[]
2553 */
2554 static void
2555u_freeentry(uep, n)
2556 u_entry_T *uep;
2557 long n;
2558{
Bram Moolenaar8d343302005-07-12 22:46:17 +00002559 while (n > 0)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002560 U_FREE_LINE(uep->ue_array[--n]);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002561 U_FREE_LINE((char_u *)uep->ue_array);
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002562#ifdef U_DEBUG
2563 uep->ue_magic = 0;
2564#endif
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002565 U_FREE_LINE((char_u *)uep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566}
2567
2568/*
2569 * invalidate the undo buffer; called when storage has already been released
2570 */
2571 void
2572u_clearall(buf)
2573 buf_T *buf;
2574{
2575 buf->b_u_newhead = buf->b_u_oldhead = buf->b_u_curhead = NULL;
2576 buf->b_u_synced = TRUE;
2577 buf->b_u_numhead = 0;
2578 buf->b_u_line_ptr = NULL;
2579 buf->b_u_line_lnum = 0;
2580}
2581
2582/*
2583 * save the line "lnum" for the "U" command
2584 */
2585 void
2586u_saveline(lnum)
2587 linenr_T lnum;
2588{
2589 if (lnum == curbuf->b_u_line_lnum) /* line is already saved */
2590 return;
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002591 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) /* should never happen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 return;
2593 u_clearline();
2594 curbuf->b_u_line_lnum = lnum;
2595 if (curwin->w_cursor.lnum == lnum)
2596 curbuf->b_u_line_colnr = curwin->w_cursor.col;
2597 else
2598 curbuf->b_u_line_colnr = 0;
2599 if ((curbuf->b_u_line_ptr = u_save_line(lnum)) == NULL)
2600 do_outofmem_msg((long_u)0);
2601}
2602
2603/*
2604 * clear the line saved for the "U" command
2605 * (this is used externally for crossing a line while in insert mode)
2606 */
2607 void
2608u_clearline()
2609{
2610 if (curbuf->b_u_line_ptr != NULL)
2611 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002612 U_FREE_LINE(curbuf->b_u_line_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 curbuf->b_u_line_ptr = NULL;
2614 curbuf->b_u_line_lnum = 0;
2615 }
2616}
2617
2618/*
2619 * Implementation of the "U" command.
2620 * Differentiation from vi: "U" can be undone with the next "U".
2621 * We also allow the cursor to be in another line.
2622 */
2623 void
2624u_undoline()
2625{
2626 colnr_T t;
2627 char_u *oldp;
2628
2629 if (undo_off)
2630 return;
2631
Bram Moolenaare3300c82008-02-13 14:21:38 +00002632 if (curbuf->b_u_line_ptr == NULL
2633 || curbuf->b_u_line_lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 {
2635 beep_flush();
2636 return;
2637 }
Bram Moolenaare3300c82008-02-13 14:21:38 +00002638
2639 /* first save the line for the 'u' command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 if (u_savecommon(curbuf->b_u_line_lnum - 1,
2641 curbuf->b_u_line_lnum + 1, (linenr_T)0) == FAIL)
2642 return;
2643 oldp = u_save_line(curbuf->b_u_line_lnum);
2644 if (oldp == NULL)
2645 {
2646 do_outofmem_msg((long_u)0);
2647 return;
2648 }
2649 ml_replace(curbuf->b_u_line_lnum, curbuf->b_u_line_ptr, TRUE);
2650 changed_bytes(curbuf->b_u_line_lnum, 0);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002651 U_FREE_LINE(curbuf->b_u_line_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 curbuf->b_u_line_ptr = oldp;
2653
2654 t = curbuf->b_u_line_colnr;
2655 if (curwin->w_cursor.lnum == curbuf->b_u_line_lnum)
2656 curbuf->b_u_line_colnr = curwin->w_cursor.col;
2657 curwin->w_cursor.col = t;
2658 curwin->w_cursor.lnum = curbuf->b_u_line_lnum;
Bram Moolenaare3300c82008-02-13 14:21:38 +00002659 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660}
2661
2662/*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002663 * There are two implementations of the memory management for undo:
2664 * 1. Use the standard malloc()/free() functions.
2665 * This should be fast for allocating memory, but when a buffer is
2666 * abandoned every single allocated chunk must be freed, which may be slow.
2667 * 2. Allocate larger blocks of memory and keep track of chunks ourselves.
2668 * This is fast for abandoning, but the use of linked lists is slow for
2669 * finding a free chunk. Esp. when a lot of lines are changed or deleted.
2670 * A bit of profiling showed that the first method is faster, especially when
2671 * making a large number of changes, under the condition that malloc()/free()
2672 * is implemented efficiently.
2673 */
2674#ifdef U_USE_MALLOC
2675/*
2676 * Version of undo memory allocation using malloc()/free()
2677 *
2678 * U_FREE_LINE() and U_ALLOC_LINE() are macros that invoke vim_free() and
2679 * lalloc() directly.
2680 */
2681
2682/*
2683 * Free all allocated memory blocks for the buffer 'buf'.
2684 */
2685 void
2686u_blockfree(buf)
2687 buf_T *buf;
2688{
Bram Moolenaar1e607892006-03-13 22:15:53 +00002689 while (buf->b_u_oldhead != NULL)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002690 u_freeheader(buf, buf->b_u_oldhead, NULL);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002691 U_FREE_LINE(buf->b_u_line_ptr);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002692}
2693
2694#else
2695/*
2696 * Storage allocation for the undo lines and blocks of the current file.
2697 * Version where Vim keeps track of the available memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 */
2699
2700/*
2701 * Memory is allocated in relatively large blocks. These blocks are linked
2702 * in the allocated block list, headed by curbuf->b_block_head. They are all
2703 * freed when abandoning a file, so we don't have to free every single line.
2704 * The list is kept sorted on memory address.
2705 * block_alloc() allocates a block.
2706 * m_blockfree() frees all blocks.
2707 *
2708 * The available chunks of memory are kept in free chunk lists. There is
2709 * one free list for each block of allocated memory. The list is kept sorted
2710 * on memory address.
2711 * u_alloc_line() gets a chunk from the free lists.
2712 * u_free_line() returns a chunk to the free lists.
2713 * curbuf->b_m_search points to the chunk before the chunk that was
2714 * freed/allocated the last time.
2715 * curbuf->b_mb_current points to the b_head where curbuf->b_m_search
2716 * points into the free list.
2717 *
2718 *
2719 * b_block_head /---> block #1 /---> block #2
2720 * mb_next ---/ mb_next ---/ mb_next ---> NULL
2721 * mb_info mb_info mb_info
2722 * | | |
2723 * V V V
2724 * NULL free chunk #1.1 free chunk #2.1
2725 * | |
2726 * V V
2727 * free chunk #1.2 NULL
2728 * |
2729 * V
2730 * NULL
2731 *
2732 * When a single free chunk list would have been used, it could take a lot
2733 * of time in u_free_line() to find the correct place to insert a chunk in the
2734 * free list. The single free list would become very long when many lines are
2735 * changed (e.g. with :%s/^M$//).
2736 */
2737
2738 /*
2739 * this blocksize is used when allocating new lines
2740 */
2741#define MEMBLOCKSIZE 2044
2742
2743/*
2744 * The size field contains the size of the chunk, including the size field
2745 * itself.
2746 *
2747 * When the chunk is not in-use it is preceded with the m_info structure.
2748 * The m_next field links it in one of the free chunk lists.
2749 *
2750 * On most unix systems structures have to be longword (32 or 64 bit) aligned.
2751 * On most other systems they are short (16 bit) aligned.
2752 */
2753
2754/* the structure definitions are now in structs.h */
2755
2756#ifdef ALIGN_LONG
2757 /* size of m_size */
2758# define M_OFFSET (sizeof(long_u))
2759#else
2760 /* size of m_size */
2761# define M_OFFSET (sizeof(short_u))
2762#endif
2763
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002764static char_u *u_blockalloc __ARGS((long_u));
2765
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766/*
2767 * Allocate a block of memory and link it in the allocated block list.
2768 */
2769 static char_u *
2770u_blockalloc(size)
2771 long_u size;
2772{
2773 mblock_T *p;
2774 mblock_T *mp, *next;
2775
2776 p = (mblock_T *)lalloc(size + sizeof(mblock_T), FALSE);
2777 if (p != NULL)
2778 {
2779 /* Insert the block into the allocated block list, keeping it
2780 sorted on address. */
2781 for (mp = &curbuf->b_block_head;
2782 (next = mp->mb_next) != NULL && next < p;
2783 mp = next)
2784 ;
2785 p->mb_next = next; /* link in block list */
2786 p->mb_size = size;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002787 p->mb_maxsize = 0; /* nothing free yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 mp->mb_next = p;
2789 p->mb_info.m_next = NULL; /* clear free list */
2790 p->mb_info.m_size = 0;
2791 curbuf->b_mb_current = p; /* remember current block */
2792 curbuf->b_m_search = NULL;
2793 p++; /* return usable memory */
2794 }
2795 return (char_u *)p;
2796}
2797
2798/*
2799 * free all allocated memory blocks for the buffer 'buf'
2800 */
2801 void
2802u_blockfree(buf)
2803 buf_T *buf;
2804{
2805 mblock_T *p, *np;
2806
2807 for (p = buf->b_block_head.mb_next; p != NULL; p = np)
2808 {
2809 np = p->mb_next;
2810 vim_free(p);
2811 }
2812 buf->b_block_head.mb_next = NULL;
2813 buf->b_m_search = NULL;
2814 buf->b_mb_current = NULL;
2815}
2816
2817/*
2818 * Free a chunk of memory for the current buffer.
2819 * Insert the chunk into the correct free list, keeping it sorted on address.
2820 */
2821 static void
2822u_free_line(ptr, keep)
2823 char_u *ptr;
2824 int keep; /* don't free the block when it's empty */
2825{
2826 minfo_T *next;
2827 minfo_T *prev, *curr;
2828 minfo_T *mp;
2829 mblock_T *nextb;
2830 mblock_T *prevb;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002831 long_u maxsize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832
2833 if (ptr == NULL || ptr == IObuff)
2834 return; /* illegal address can happen in out-of-memory situations */
2835
2836 mp = (minfo_T *)(ptr - M_OFFSET);
2837
2838 /* find block where chunk could be a part off */
2839 /* if we change curbuf->b_mb_current, curbuf->b_m_search is set to NULL */
2840 if (curbuf->b_mb_current == NULL || mp < (minfo_T *)curbuf->b_mb_current)
2841 {
2842 curbuf->b_mb_current = curbuf->b_block_head.mb_next;
2843 curbuf->b_m_search = NULL;
2844 }
2845 if ((nextb = curbuf->b_mb_current->mb_next) != NULL
2846 && (minfo_T *)nextb < mp)
2847 {
2848 curbuf->b_mb_current = nextb;
2849 curbuf->b_m_search = NULL;
2850 }
2851 while ((nextb = curbuf->b_mb_current->mb_next) != NULL
2852 && (minfo_T *)nextb < mp)
2853 curbuf->b_mb_current = nextb;
2854
2855 curr = NULL;
2856 /*
2857 * If mp is smaller than curbuf->b_m_search->m_next go to the start of
2858 * the free list
2859 */
2860 if (curbuf->b_m_search == NULL || mp < (curbuf->b_m_search->m_next))
2861 next = &(curbuf->b_mb_current->mb_info);
2862 else
2863 next = curbuf->b_m_search;
2864 /*
2865 * The following loop is executed very often.
2866 * Therefore it has been optimized at the cost of readability.
2867 * Keep it fast!
2868 */
2869#ifdef SLOW_BUT_EASY_TO_READ
2870 do
2871 {
2872 prev = curr;
2873 curr = next;
2874 next = next->m_next;
2875 }
2876 while (mp > next && next != NULL);
2877#else
2878 do /* first, middle, last */
2879 {
2880 prev = next->m_next; /* curr, next, prev */
2881 if (prev == NULL || mp <= prev)
2882 {
2883 prev = curr;
2884 curr = next;
2885 next = next->m_next;
2886 break;
2887 }
2888 curr = prev->m_next; /* next, prev, curr */
2889 if (curr == NULL || mp <= curr)
2890 {
2891 prev = next;
2892 curr = prev->m_next;
2893 next = curr->m_next;
2894 break;
2895 }
2896 next = curr->m_next; /* prev, curr, next */
2897 }
2898 while (mp > next && next != NULL);
2899#endif
2900
2901 /* if *mp and *next are concatenated, join them into one chunk */
2902 if ((char_u *)mp + mp->m_size == (char_u *)next)
2903 {
2904 mp->m_size += next->m_size;
2905 mp->m_next = next->m_next;
2906 }
2907 else
2908 mp->m_next = next;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002909 maxsize = mp->m_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910
2911 /* if *curr and *mp are concatenated, join them */
2912 if (prev != NULL && (char_u *)curr + curr->m_size == (char_u *)mp)
2913 {
2914 curr->m_size += mp->m_size;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002915 maxsize = curr->m_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 curr->m_next = mp->m_next;
2917 curbuf->b_m_search = prev;
2918 }
2919 else
2920 {
2921 curr->m_next = mp;
2922 curbuf->b_m_search = curr; /* put curbuf->b_m_search before freed
2923 chunk */
2924 }
2925
2926 /*
Bram Moolenaar035db9f2007-05-10 18:02:27 +00002927 * If the block only contains free memory now, release it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 */
2929 if (!keep && curbuf->b_mb_current->mb_size
2930 == curbuf->b_mb_current->mb_info.m_next->m_size)
2931 {
2932 /* Find the block before the current one to be able to unlink it from
2933 * the list of blocks. */
2934 prevb = &curbuf->b_block_head;
2935 for (nextb = prevb->mb_next; nextb != curbuf->b_mb_current;
2936 nextb = nextb->mb_next)
2937 prevb = nextb;
2938 prevb->mb_next = nextb->mb_next;
2939 vim_free(nextb);
2940 curbuf->b_mb_current = NULL;
2941 curbuf->b_m_search = NULL;
2942 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002943 else if (curbuf->b_mb_current->mb_maxsize < maxsize)
2944 curbuf->b_mb_current->mb_maxsize = maxsize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945}
2946
2947/*
2948 * Allocate and initialize a new line structure with room for at least
2949 * 'size' characters plus a terminating NUL.
2950 */
2951 static char_u *
2952u_alloc_line(size)
2953 unsigned size;
2954{
2955 minfo_T *mp, *mprev, *mp2;
2956 mblock_T *mbp;
2957 int size_align;
2958
2959 /*
2960 * Add room for size field and trailing NUL byte.
2961 * Adjust for minimal size (must be able to store minfo_T
2962 * plus a trailing NUL, so the chunk can be released again)
2963 */
2964 size += M_OFFSET + 1;
2965 if (size < sizeof(minfo_T) + 1)
2966 size = sizeof(minfo_T) + 1;
2967
2968 /*
2969 * round size up for alignment
2970 */
2971 size_align = (size + ALIGN_MASK) & ~ALIGN_MASK;
2972
2973 /*
2974 * If curbuf->b_m_search is NULL (uninitialized free list) start at
2975 * curbuf->b_block_head
2976 */
2977 if (curbuf->b_mb_current == NULL || curbuf->b_m_search == NULL)
2978 {
2979 curbuf->b_mb_current = &curbuf->b_block_head;
2980 curbuf->b_m_search = &(curbuf->b_block_head.mb_info);
2981 }
2982
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002983 /* Search for a block with enough space. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 mbp = curbuf->b_mb_current;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002985 while (mbp->mb_maxsize < size_align)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002987 if (mbp->mb_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 mbp = mbp->mb_next;
2989 else
2990 mbp = &curbuf->b_block_head;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002991 if (mbp == curbuf->b_mb_current)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002993 int n = (size_align > (MEMBLOCKSIZE / 4)
2994 ? size_align : MEMBLOCKSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002996 /* Back where we started in block list: need to add a new block
2997 * with enough space. */
2998 mp = (minfo_T *)u_blockalloc((long_u)n);
2999 if (mp == NULL)
3000 return (NULL);
3001 mp->m_size = n;
3002 u_free_line((char_u *)mp + M_OFFSET, TRUE);
3003 mbp = curbuf->b_mb_current;
3004 break;
3005 }
3006 }
3007 if (mbp != curbuf->b_mb_current)
3008 curbuf->b_m_search = &(mbp->mb_info);
3009
3010 /* In this block find a chunk with enough space. */
3011 mprev = curbuf->b_m_search;
3012 mp = curbuf->b_m_search->m_next;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00003013 for (;;)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003014 {
3015 if (mp == NULL) /* at end of the list */
3016 mp = &(mbp->mb_info); /* wrap around to begin */
3017 if (mp->m_size >= size)
3018 break;
3019 if (mp == curbuf->b_m_search)
3020 {
3021 /* back where we started in free chunk list: "cannot happen" */
3022 EMSG2(_(e_intern2), "u_alloc_line()");
3023 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 }
3025 mprev = mp;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003026 mp = mp->m_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 }
3028
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003029 /* when using the largest chunk adjust mb_maxsize */
3030 if (mp->m_size >= mbp->mb_maxsize)
3031 mbp->mb_maxsize = 0;
3032
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 /* if the chunk we found is large enough, split it up in two */
3034 if ((long)mp->m_size - size_align >= (long)(sizeof(minfo_T) + 1))
3035 {
3036 mp2 = (minfo_T *)((char_u *)mp + size_align);
3037 mp2->m_size = mp->m_size - size_align;
3038 mp2->m_next = mp->m_next;
3039 mprev->m_next = mp2;
3040 mp->m_size = size_align;
3041 }
3042 else /* remove *mp from the free list */
3043 {
3044 mprev->m_next = mp->m_next;
3045 }
3046 curbuf->b_m_search = mprev;
3047 curbuf->b_mb_current = mbp;
3048
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003049 /* If using the largest chunk need to find the new largest chunk */
3050 if (mbp->mb_maxsize == 0)
3051 for (mp2 = &(mbp->mb_info); mp2 != NULL; mp2 = mp2->m_next)
3052 if (mbp->mb_maxsize < mp2->m_size)
3053 mbp->mb_maxsize = mp2->m_size;
3054
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 mp = (minfo_T *)((char_u *)mp + M_OFFSET);
3056 *(char_u *)mp = NUL; /* set the first byte to NUL */
3057
3058 return ((char_u *)mp);
3059}
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061
3062/*
3063 * u_save_line(): allocate memory with u_alloc_line() and copy line 'lnum'
3064 * into it.
3065 */
3066 static char_u *
3067u_save_line(lnum)
3068 linenr_T lnum;
3069{
3070 char_u *src;
3071 char_u *dst;
3072 unsigned len;
3073
3074 src = ml_get(lnum);
3075 len = (unsigned)STRLEN(src);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003076 if ((dst = U_ALLOC_LINE(len)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 mch_memmove(dst, src, (size_t)(len + 1));
3078 return (dst);
3079}
3080
3081/*
3082 * Check if the 'modified' flag is set, or 'ff' has changed (only need to
3083 * check the first character, because it can only be "dos", "unix" or "mac").
3084 * "nofile" and "scratch" type buffers are considered to always be unchanged.
3085 */
3086 int
3087bufIsChanged(buf)
3088 buf_T *buf;
3089{
3090 return
3091#ifdef FEAT_QUICKFIX
3092 !bt_dontwrite(buf) &&
3093#endif
3094 (buf->b_changed || file_ff_differs(buf));
3095}
3096
3097 int
3098curbufIsChanged()
3099{
3100 return
3101#ifdef FEAT_QUICKFIX
3102 !bt_dontwrite(curbuf) &&
3103#endif
3104 (curbuf->b_changed || file_ff_differs(curbuf));
3105}