blob: 958c6d2dea33dad533f1872d2d2cf9fc9441a5a0 [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 Moolenaarf05e3b02010-05-29 15:40:47 +020075 * All data is allocated and will all be freed when the buffer is unloaded.
Bram Moolenaar071d4272004-06-13 20:20:40 +000076 */
77
Bram Moolenaarfecb6602007-10-01 20:54:15 +000078/* Uncomment the next line for including the u_check() function. This warns
79 * for errors in the debug information. */
80/* #define U_DEBUG 1 */
81#define UH_MAGIC 0x18dade /* value for uh_magic when in use */
82#define UE_MAGIC 0xabc123 /* value for ue_magic when in use */
83
Bram Moolenaar442b4222010-05-24 21:34:22 +020084#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
85# include "vimio.h" /* for vim_read(), must be before vim.h */
86#endif
87
Bram Moolenaar071d4272004-06-13 20:20:40 +000088#include "vim.h"
89
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +000090static void u_unch_branch __ARGS((u_header_T *uhp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000091static u_entry_T *u_get_headentry __ARGS((void));
92static void u_getbot __ARGS((void));
93static int u_savecommon __ARGS((linenr_T, linenr_T, linenr_T));
94static void u_doit __ARGS((int count));
Bram Moolenaarca003e12006-03-17 23:19:38 +000095static void u_undoredo __ARGS((int undo));
Bram Moolenaardb552d602006-03-23 22:59:57 +000096static void u_undo_end __ARGS((int did_undo, int absolute));
Bram Moolenaarefd2bf12006-03-16 21:41:35 +000097static void u_add_time __ARGS((char_u *buf, size_t buflen, time_t tt));
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +000098static void u_freeheader __ARGS((buf_T *buf, u_header_T *uhp, u_header_T **uhpp));
Bram Moolenaar1e607892006-03-13 22:15:53 +000099static void u_freebranch __ARGS((buf_T *buf, u_header_T *uhp, u_header_T **uhpp));
100static void u_freeentries __ARGS((buf_T *buf, u_header_T *uhp, u_header_T **uhpp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101static void u_freeentry __ARGS((u_entry_T *, long));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200102#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200103static char_u *u_get_undo_file_name __ARGS((char_u *, int reading));
Bram Moolenaar9db58062010-05-29 20:33:07 +0200104static void corruption_error __ARGS((char *msg, char_u *file_name));
Bram Moolenaar6a18eb62010-05-26 21:21:00 +0200105static void u_free_uhp __ARGS((u_header_T *uhp));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200106static int serialize_uep __ARGS((u_entry_T *uep, FILE *fp));
Bram Moolenaar9db58062010-05-29 20:33:07 +0200107static u_entry_T *unserialize_uep __ARGS((FILE *fp, int *error, char_u *file_name));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200108static void serialize_pos __ARGS((pos_T pos, FILE *fp));
Bram Moolenaar9db58062010-05-29 20:33:07 +0200109static void unserialize_pos __ARGS((pos_T *pos, FILE *fp));
Bram Moolenaarcdf04202010-05-29 15:11:47 +0200110static void serialize_visualinfo __ARGS((visualinfo_T *info, FILE *fp));
Bram Moolenaar9db58062010-05-29 20:33:07 +0200111static void unserialize_visualinfo __ARGS((visualinfo_T *info, FILE *fp));
112static void put_header_ptr __ARGS((FILE *fp, u_header_T *uhp));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114
Bram Moolenaarf05e3b02010-05-29 15:40:47 +0200115#define U_ALLOC_LINE(size) lalloc((long_u)(size), FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116static char_u *u_save_line __ARGS((linenr_T));
117
118static long u_newcount, u_oldcount;
119
120/*
121 * When 'u' flag included in 'cpoptions', we behave like vi. Need to remember
122 * the action that "u" should do.
123 */
124static int undo_undoes = FALSE;
125
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200126static int lastmark = 0;
127
Bram Moolenaar9db58062010-05-29 20:33:07 +0200128#if defined(U_DEBUG) || defined(PROTO)
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000129/*
130 * Check the undo structures for being valid. Print a warning when something
131 * looks wrong.
132 */
133static int seen_b_u_curhead;
134static int seen_b_u_newhead;
135static int header_count;
136
137 static void
138u_check_tree(u_header_T *uhp,
139 u_header_T *exp_uh_next,
140 u_header_T *exp_uh_alt_prev)
141{
142 u_entry_T *uep;
143
144 if (uhp == NULL)
145 return;
146 ++header_count;
147 if (uhp == curbuf->b_u_curhead && ++seen_b_u_curhead > 1)
148 {
149 EMSG("b_u_curhead found twice (looping?)");
150 return;
151 }
152 if (uhp == curbuf->b_u_newhead && ++seen_b_u_newhead > 1)
153 {
154 EMSG("b_u_newhead found twice (looping?)");
155 return;
156 }
157
158 if (uhp->uh_magic != UH_MAGIC)
159 EMSG("uh_magic wrong (may be using freed memory)");
160 else
161 {
162 /* Check pointers back are correct. */
163 if (uhp->uh_next != exp_uh_next)
164 {
165 EMSG("uh_next wrong");
166 smsg((char_u *)"expected: 0x%x, actual: 0x%x",
167 exp_uh_next, uhp->uh_next);
168 }
169 if (uhp->uh_alt_prev != exp_uh_alt_prev)
170 {
171 EMSG("uh_alt_prev wrong");
172 smsg((char_u *)"expected: 0x%x, actual: 0x%x",
173 exp_uh_alt_prev, uhp->uh_alt_prev);
174 }
175
176 /* Check the undo tree at this header. */
177 for (uep = uhp->uh_entry; uep != NULL; uep = uep->ue_next)
178 {
179 if (uep->ue_magic != UE_MAGIC)
180 {
181 EMSG("ue_magic wrong (may be using freed memory)");
182 break;
183 }
184 }
185
186 /* Check the next alt tree. */
187 u_check_tree(uhp->uh_alt_next, uhp->uh_next, uhp);
188
189 /* Check the next header in this branch. */
190 u_check_tree(uhp->uh_prev, uhp, NULL);
191 }
192}
193
194 void
195u_check(int newhead_may_be_NULL)
196{
197 seen_b_u_newhead = 0;
198 seen_b_u_curhead = 0;
199 header_count = 0;
200
201 u_check_tree(curbuf->b_u_oldhead, NULL, NULL);
202
203 if (seen_b_u_newhead == 0 && curbuf->b_u_oldhead != NULL
204 && !(newhead_may_be_NULL && curbuf->b_u_newhead == NULL))
205 EMSGN("b_u_newhead invalid: 0x%x", curbuf->b_u_newhead);
206 if (curbuf->b_u_curhead != NULL && seen_b_u_curhead == 0)
207 EMSGN("b_u_curhead invalid: 0x%x", curbuf->b_u_curhead);
208 if (header_count != curbuf->b_u_numhead)
209 {
210 EMSG("b_u_numhead invalid");
211 smsg((char_u *)"expected: %ld, actual: %ld",
212 (long)header_count, (long)curbuf->b_u_numhead);
213 }
214}
215#endif
216
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000218 * Save the current line for both the "u" and "U" command.
219 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 */
221 int
222u_save_cursor()
223{
224 return (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
225 (linenr_T)(curwin->w_cursor.lnum + 1)));
226}
227
228/*
229 * Save the lines between "top" and "bot" for both the "u" and "U" command.
230 * "top" may be 0 and bot may be curbuf->b_ml.ml_line_count + 1.
231 * Returns FAIL when lines could not be saved, OK otherwise.
232 */
233 int
234u_save(top, bot)
235 linenr_T top, bot;
236{
237 if (undo_off)
238 return OK;
239
240 if (top > curbuf->b_ml.ml_line_count ||
241 top >= bot || bot > curbuf->b_ml.ml_line_count + 1)
242 return FALSE; /* rely on caller to do error messages */
243
244 if (top + 2 == bot)
245 u_saveline((linenr_T)(top + 1));
246
247 return (u_savecommon(top, bot, (linenr_T)0));
248}
249
250/*
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200251 * Save the line "lnum" (used by ":s" and "~" command).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 * The line is replaced, so the new bottom line is lnum + 1.
253 */
254 int
255u_savesub(lnum)
256 linenr_T lnum;
257{
258 if (undo_off)
259 return OK;
260
261 return (u_savecommon(lnum - 1, lnum + 1, lnum + 1));
262}
263
264/*
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200265 * A new line is inserted before line "lnum" (used by :s command).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 * The line is inserted, so the new bottom line is lnum + 1.
267 */
268 int
269u_inssub(lnum)
270 linenr_T lnum;
271{
272 if (undo_off)
273 return OK;
274
275 return (u_savecommon(lnum - 1, lnum, lnum + 1));
276}
277
278/*
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200279 * Save the lines "lnum" - "lnum" + nlines (used by delete command).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 * The lines are deleted, so the new bottom line is lnum, unless the buffer
281 * becomes empty.
282 */
283 int
284u_savedel(lnum, nlines)
285 linenr_T lnum;
286 long nlines;
287{
288 if (undo_off)
289 return OK;
290
291 return (u_savecommon(lnum - 1, lnum + nlines,
292 nlines == curbuf->b_ml.ml_line_count ? 2 : lnum));
293}
294
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000295/*
296 * Return TRUE when undo is allowed. Otherwise give an error message and
297 * return FALSE.
298 */
Bram Moolenaarce6ef252006-07-12 19:49:41 +0000299 int
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000300undo_allowed()
301{
302 /* Don't allow changes when 'modifiable' is off. */
303 if (!curbuf->b_p_ma)
304 {
305 EMSG(_(e_modifiable));
306 return FALSE;
307 }
308
309#ifdef HAVE_SANDBOX
310 /* In the sandbox it's not allowed to change the text. */
311 if (sandbox != 0)
312 {
313 EMSG(_(e_sandbox));
314 return FALSE;
315 }
316#endif
317
318 /* Don't allow changes in the buffer while editing the cmdline. The
319 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000320 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000321 {
322 EMSG(_(e_secure));
323 return FALSE;
324 }
325
326 return TRUE;
327}
328
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 static int
330u_savecommon(top, bot, newbot)
331 linenr_T top, bot;
332 linenr_T newbot;
333{
Bram Moolenaar1e607892006-03-13 22:15:53 +0000334 linenr_T lnum;
335 long i;
336 u_header_T *uhp;
337 u_header_T *old_curhead;
338 u_entry_T *uep;
339 u_entry_T *prev_uep;
340 long size;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000342 /* When making changes is not allowed return FAIL. It's a crude way to
343 * make all change commands fail. */
344 if (!undo_allowed())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000347#ifdef U_DEBUG
348 u_check(FALSE);
349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350#ifdef FEAT_NETBEANS_INTG
351 /*
352 * Netbeans defines areas that cannot be modified. Bail out here when
353 * trying to change text in a guarded area.
354 */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200355 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 {
Bram Moolenaar009b2592004-10-24 19:18:58 +0000357 if (netbeans_is_guarded(top, bot))
358 {
359 EMSG(_(e_guarded));
360 return FAIL;
361 }
362 if (curbuf->b_p_ro)
363 {
364 EMSG(_(e_nbreadonly));
365 return FAIL;
366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 }
368#endif
369
370#ifdef FEAT_AUTOCMD
371 /*
372 * Saving text for undo means we are going to make a change. Give a
373 * warning for a read-only file before making the change, so that the
374 * FileChangedRO event can replace the buffer with a read-write version
375 * (e.g., obtained from a source control system).
376 */
377 change_warning(0);
378#endif
379
380 size = bot - top - 1;
381
382 /*
383 * if curbuf->b_u_synced == TRUE make a new header
384 */
385 if (curbuf->b_u_synced)
386 {
387#ifdef FEAT_JUMPLIST
388 /* Need to create new entry in b_changelist. */
389 curbuf->b_new_change = TRUE;
390#endif
391
Bram Moolenaar1e607892006-03-13 22:15:53 +0000392 if (p_ul >= 0)
393 {
394 /*
395 * Make a new header entry. Do this first so that we don't mess
396 * up the undo info when out of memory.
397 */
Bram Moolenaarf05e3b02010-05-29 15:40:47 +0200398 uhp = (u_header_T *)U_ALLOC_LINE(sizeof(u_header_T));
Bram Moolenaar1e607892006-03-13 22:15:53 +0000399 if (uhp == NULL)
400 goto nomem;
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000401#ifdef U_DEBUG
402 uhp->uh_magic = UH_MAGIC;
403#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +0000404 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000405 else
406 uhp = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000407
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 /*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000409 * If we undid more than we redid, move the entry lists before and
410 * including curbuf->b_u_curhead to an alternate branch.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 */
Bram Moolenaar1e607892006-03-13 22:15:53 +0000412 old_curhead = curbuf->b_u_curhead;
413 if (old_curhead != NULL)
414 {
415 curbuf->b_u_newhead = old_curhead->uh_next;
416 curbuf->b_u_curhead = NULL;
417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418
419 /*
420 * free headers to keep the size right
421 */
422 while (curbuf->b_u_numhead > p_ul && curbuf->b_u_oldhead != NULL)
Bram Moolenaar1e607892006-03-13 22:15:53 +0000423 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000424 u_header_T *uhfree = curbuf->b_u_oldhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000425
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000426 if (uhfree == old_curhead)
427 /* Can't reconnect the branch, delete all of it. */
428 u_freebranch(curbuf, uhfree, &old_curhead);
429 else if (uhfree->uh_alt_next == NULL)
430 /* There is no branch, only free one header. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000431 u_freeheader(curbuf, uhfree, &old_curhead);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000432 else
433 {
434 /* Free the oldest alternate branch as a whole. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000435 while (uhfree->uh_alt_next != NULL)
436 uhfree = uhfree->uh_alt_next;
437 u_freebranch(curbuf, uhfree, &old_curhead);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000438 }
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000439#ifdef U_DEBUG
440 u_check(TRUE);
441#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +0000442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000444 if (uhp == NULL) /* no undo at all */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 {
Bram Moolenaar1e607892006-03-13 22:15:53 +0000446 if (old_curhead != NULL)
447 u_freebranch(curbuf, old_curhead, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 curbuf->b_u_synced = FALSE;
449 return OK;
450 }
451
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 uhp->uh_prev = NULL;
453 uhp->uh_next = curbuf->b_u_newhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000454 uhp->uh_alt_next = old_curhead;
455 if (old_curhead != NULL)
456 {
Bram Moolenaar89ed3df2007-01-09 19:23:12 +0000457 uhp->uh_alt_prev = old_curhead->uh_alt_prev;
458 if (uhp->uh_alt_prev != NULL)
459 uhp->uh_alt_prev->uh_alt_next = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000460 old_curhead->uh_alt_prev = uhp;
461 if (curbuf->b_u_oldhead == old_curhead)
462 curbuf->b_u_oldhead = uhp;
463 }
Bram Moolenaar89ed3df2007-01-09 19:23:12 +0000464 else
465 uhp->uh_alt_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 if (curbuf->b_u_newhead != NULL)
467 curbuf->b_u_newhead->uh_prev = uhp;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000468
Bram Moolenaarca003e12006-03-17 23:19:38 +0000469 uhp->uh_seq = ++curbuf->b_u_seq_last;
470 curbuf->b_u_seq_cur = uhp->uh_seq;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000471 uhp->uh_time = time(NULL);
472 curbuf->b_u_seq_time = uhp->uh_time + 1;
473
Bram Moolenaar1e607892006-03-13 22:15:53 +0000474 uhp->uh_walk = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 uhp->uh_entry = NULL;
476 uhp->uh_getbot_entry = NULL;
477 uhp->uh_cursor = curwin->w_cursor; /* save cursor pos. for undo */
478#ifdef FEAT_VIRTUALEDIT
479 if (virtual_active() && curwin->w_cursor.coladd > 0)
480 uhp->uh_cursor_vcol = getviscol();
481 else
482 uhp->uh_cursor_vcol = -1;
483#endif
484
485 /* save changed and buffer empty flag for undo */
486 uhp->uh_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
487 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
488
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000489 /* save named marks and Visual marks for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 mch_memmove(uhp->uh_namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000491#ifdef FEAT_VISUAL
492 uhp->uh_visual = curbuf->b_visual;
493#endif
494
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 curbuf->b_u_newhead = uhp;
496 if (curbuf->b_u_oldhead == NULL)
497 curbuf->b_u_oldhead = uhp;
498 ++curbuf->b_u_numhead;
499 }
500 else
501 {
502 if (p_ul < 0) /* no undo at all */
503 return OK;
504
505 /*
506 * When saving a single line, and it has been saved just before, it
507 * doesn't make sense saving it again. Saves a lot of memory when
508 * making lots of changes inside the same line.
509 * This is only possible if the previous change didn't increase or
510 * decrease the number of lines.
511 * Check the ten last changes. More doesn't make sense and takes too
512 * long.
513 */
514 if (size == 1)
515 {
516 uep = u_get_headentry();
517 prev_uep = NULL;
518 for (i = 0; i < 10; ++i)
519 {
520 if (uep == NULL)
521 break;
522
523 /* If lines have been inserted/deleted we give up.
524 * Also when the line was included in a multi-line save. */
525 if ((curbuf->b_u_newhead->uh_getbot_entry != uep
526 ? (uep->ue_top + uep->ue_size + 1
527 != (uep->ue_bot == 0
528 ? curbuf->b_ml.ml_line_count + 1
529 : uep->ue_bot))
530 : uep->ue_lcount != curbuf->b_ml.ml_line_count)
531 || (uep->ue_size > 1
532 && top >= uep->ue_top
533 && top + 2 <= uep->ue_top + uep->ue_size + 1))
534 break;
535
536 /* If it's the same line we can skip saving it again. */
537 if (uep->ue_size == 1 && uep->ue_top == top)
538 {
539 if (i > 0)
540 {
541 /* It's not the last entry: get ue_bot for the last
542 * entry now. Following deleted/inserted lines go to
543 * the re-used entry. */
544 u_getbot();
545 curbuf->b_u_synced = FALSE;
546
547 /* Move the found entry to become the last entry. The
548 * order of undo/redo doesn't matter for the entries
549 * we move it over, since they don't change the line
550 * count and don't include this line. It does matter
551 * for the found entry if the line count is changed by
552 * the executed command. */
553 prev_uep->ue_next = uep->ue_next;
554 uep->ue_next = curbuf->b_u_newhead->uh_entry;
555 curbuf->b_u_newhead->uh_entry = uep;
556 }
557
558 /* The executed command may change the line count. */
559 if (newbot != 0)
560 uep->ue_bot = newbot;
561 else if (bot > curbuf->b_ml.ml_line_count)
562 uep->ue_bot = 0;
563 else
564 {
565 uep->ue_lcount = curbuf->b_ml.ml_line_count;
566 curbuf->b_u_newhead->uh_getbot_entry = uep;
567 }
568 return OK;
569 }
570 prev_uep = uep;
571 uep = uep->ue_next;
572 }
573 }
574
575 /* find line number for ue_bot for previous u_save() */
576 u_getbot();
577 }
578
579#if !defined(UNIX) && !defined(DJGPP) && !defined(WIN32) && !defined(__EMX__)
580 /*
581 * With Amiga and MSDOS 16 bit we can't handle big undo's, because
582 * then u_alloc_line would have to allocate a block larger than 32K
583 */
584 if (size >= 8000)
585 goto nomem;
586#endif
587
588 /*
589 * add lines in front of entry list
590 */
Bram Moolenaarf05e3b02010-05-29 15:40:47 +0200591 uep = (u_entry_T *)U_ALLOC_LINE(sizeof(u_entry_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 if (uep == NULL)
593 goto nomem;
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200594 vim_memset(uep, 0, sizeof(u_entry_T));
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000595#ifdef U_DEBUG
596 uep->ue_magic = UE_MAGIC;
597#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598
599 uep->ue_size = size;
600 uep->ue_top = top;
601 if (newbot != 0)
602 uep->ue_bot = newbot;
603 /*
604 * Use 0 for ue_bot if bot is below last line.
605 * Otherwise we have to compute ue_bot later.
606 */
607 else if (bot > curbuf->b_ml.ml_line_count)
608 uep->ue_bot = 0;
609 else
610 {
611 uep->ue_lcount = curbuf->b_ml.ml_line_count;
612 curbuf->b_u_newhead->uh_getbot_entry = uep;
613 }
614
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000615 if (size > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000617 if ((uep->ue_array = (char_u **)U_ALLOC_LINE(
Bram Moolenaarf05e3b02010-05-29 15:40:47 +0200618 sizeof(char_u *) * size)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 {
620 u_freeentry(uep, 0L);
621 goto nomem;
622 }
623 for (i = 0, lnum = top + 1; i < size; ++i)
624 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000625 fast_breakcheck();
626 if (got_int)
627 {
628 u_freeentry(uep, i);
629 return FAIL;
630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 if ((uep->ue_array[i] = u_save_line(lnum++)) == NULL)
632 {
633 u_freeentry(uep, i);
634 goto nomem;
635 }
636 }
637 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000638 else
639 uep->ue_array = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 uep->ue_next = curbuf->b_u_newhead->uh_entry;
641 curbuf->b_u_newhead->uh_entry = uep;
642 curbuf->b_u_synced = FALSE;
643 undo_undoes = FALSE;
644
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000645#ifdef U_DEBUG
646 u_check(FALSE);
647#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 return OK;
649
650nomem:
651 msg_silent = 0; /* must display the prompt */
652 if (ask_yesno((char_u *)_("No undo possible; continue anyway"), TRUE)
653 == 'y')
654 {
655 undo_off = TRUE; /* will be reset when character typed */
656 return OK;
657 }
658 do_outofmem_msg((long_u)0);
659 return FAIL;
660}
661
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200662#ifdef FEAT_PERSISTENT_UNDO
663
Bram Moolenaar9db58062010-05-29 20:33:07 +0200664# define UF_START_MAGIC "Vim\237UnDo\345" /* magic at start of undofile */
665# define UF_START_MAGIC_LEN 9
666# define UF_HEADER_MAGIC 0x5fd0 /* magic at start of header */
667# define UF_HEADER_END_MAGIC 0xe7aa /* magic after last header */
668# define UF_ENTRY_MAGIC 0xf518 /* magic at start of entry */
669# define UF_ENTRY_END_MAGIC 0x3581 /* magic after last entry */
670# define UF_VERSION 1 /* 2-byte undofile version number */
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200671
Bram Moolenaarcdf04202010-05-29 15:11:47 +0200672static char_u e_not_open[] = N_("E828: Cannot open undo file for writing: %s");
Bram Moolenaarcdf04202010-05-29 15:11:47 +0200673
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200674/*
675 * Compute the hash for the current buffer text into hash[UNDO_HASH_SIZE].
676 */
677 void
678u_compute_hash(hash)
679 char_u *hash;
680{
681 context_sha256_T ctx;
682 linenr_T lnum;
683 char_u *p;
684
685 sha256_start(&ctx);
686 for (lnum = 1; lnum < curbuf->b_ml.ml_line_count; ++lnum)
687 {
688 p = ml_get(lnum);
Bram Moolenaar442b4222010-05-24 21:34:22 +0200689 sha256_update(&ctx, p, (UINT32_T)(STRLEN(p) + 1));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200690 }
691 sha256_finish(&ctx, hash);
692}
693
694/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200695 * Return an allocated string of the full path of the target undofile.
696 * When "reading" is TRUE find the file to read, go over all directories in
697 * 'undodir'.
698 * When "reading" is FALSE use the first name where the directory exists.
Bram Moolenaar9db58062010-05-29 20:33:07 +0200699 * Returns NULL when there is no place to write or no file to read.
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200700 */
701 static char_u *
702u_get_undo_file_name(buf_ffname, reading)
703 char_u *buf_ffname;
704 int reading;
705{
706 char_u *dirp;
707 char_u dir_name[IOSIZE + 1];
708 char_u *munged_name = NULL;
709 char_u *undo_file_name = NULL;
710 int dir_len;
711 char_u *p;
712 struct stat st;
713 char_u *ffname = buf_ffname;
714#ifdef HAVE_READLINK
715 char_u fname_buf[MAXPATHL];
716#endif
717
718 if (ffname == NULL)
719 return NULL;
720
721#ifdef HAVE_READLINK
722 /* Expand symlink in the file name, so that we put the undo file with the
723 * actual file instead of with the symlink. */
724 if (resolve_symlink(ffname, fname_buf) == OK)
725 ffname = fname_buf;
726#endif
727
728 /* Loop over 'undodir'. When reading find the first file that exists.
729 * When not reading use the first directory that exists or ".". */
730 dirp = p_udir;
731 while (*dirp != NUL)
732 {
733 dir_len = copy_option_part(&dirp, dir_name, IOSIZE, ",");
734 if (dir_len == 1 && dir_name[0] == '.')
735 {
736 /* Use same directory as the ffname,
737 * "dir/name" -> "dir/.name.un~" */
Bram Moolenaar442b4222010-05-24 21:34:22 +0200738 undo_file_name = vim_strnsave(ffname, (int)(STRLEN(ffname) + 5));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200739 if (undo_file_name == NULL)
740 break;
741 p = gettail(undo_file_name);
742 mch_memmove(p + 1, p, STRLEN(p) + 1);
743 *p = '.';
744 STRCAT(p, ".un~");
745 }
746 else
747 {
748 dir_name[dir_len] = NUL;
749 if (mch_isdir(dir_name))
750 {
751 if (munged_name == NULL)
752 {
753 munged_name = vim_strsave(ffname);
754 if (munged_name == NULL)
755 return NULL;
756 for (p = munged_name; *p != NUL; mb_ptr_adv(p))
757 if (vim_ispathsep(*p))
758 *p = '%';
759 }
760 undo_file_name = concat_fnames(dir_name, munged_name, TRUE);
761 }
762 }
763
764 /* When reading check if the file exists. */
765 if (undo_file_name != NULL && (!reading
766 || mch_stat((char *)undo_file_name, &st) >= 0))
767 break;
768 vim_free(undo_file_name);
769 undo_file_name = NULL;
770 }
771
772 vim_free(munged_name);
773 return undo_file_name;
774}
775
Bram Moolenaar9db58062010-05-29 20:33:07 +0200776 static void
777corruption_error(msg, file_name)
778 char *msg;
779 char_u *file_name;
780{
781 EMSG3(_("E825: Corrupted undo file (%s): %s"), msg, file_name);
782}
783
784 static void
785u_free_uhp(uhp)
786 u_header_T *uhp;
787{
788 u_entry_T *nuep;
789 u_entry_T *uep;
790
791 uep = uhp->uh_entry;
792 while (uep != NULL)
793 {
794 nuep = uep->ue_next;
795 u_freeentry(uep, uep->ue_size);
796 uep = nuep;
797 }
798 vim_free(uhp);
799}
800
801/*
802 * Serialize "uep" to "fp".
803 */
804 static int
805serialize_uep(uep, fp)
806 u_entry_T *uep;
807 FILE *fp;
808{
809 int i;
810 size_t len;
811
812 put_bytes(fp, (long_u)uep->ue_top, 4);
813 put_bytes(fp, (long_u)uep->ue_bot, 4);
814 put_bytes(fp, (long_u)uep->ue_lcount, 4);
815 put_bytes(fp, (long_u)uep->ue_size, 4);
816 for (i = 0; i < uep->ue_size; ++i)
817 {
818 len = STRLEN(uep->ue_array[i]);
819 if (put_bytes(fp, (long_u)len, 4) == FAIL)
820 return FAIL;
821 if (len > 0 && fwrite(uep->ue_array[i], len, (size_t)1, fp) != 1)
822 return FAIL;
823 }
824 return OK;
825}
826
827 static u_entry_T *
828unserialize_uep(fp, error, file_name)
829 FILE *fp;
830 int *error;
831 char_u *file_name;
832{
833 int i;
834 int j;
835 u_entry_T *uep;
836 char_u **array;
837 char_u *line;
838 int line_len;
839
840 uep = (u_entry_T *)U_ALLOC_LINE(sizeof(u_entry_T));
841 if (uep == NULL)
842 return NULL;
843 vim_memset(uep, 0, sizeof(u_entry_T));
844#ifdef U_DEBUG
845 uep->ue_magic = UE_MAGIC;
846#endif
847 uep->ue_top = get4c(fp);
848 uep->ue_bot = get4c(fp);
849 uep->ue_lcount = get4c(fp);
850 uep->ue_size = get4c(fp);
851 if (uep->ue_size > 0)
852 {
853 array = (char_u **)U_ALLOC_LINE(sizeof(char_u *) * uep->ue_size);
854 if (array == NULL)
855 {
856 *error = TRUE;
857 return uep;
858 }
859 vim_memset(array, 0, sizeof(char_u *) * uep->ue_size);
860 }
861 uep->ue_array = array;
862
863 for (i = 0; i < uep->ue_size; ++i)
864 {
865 line_len = get4c(fp);
866 if (line_len >= 0)
867 line = (char_u *)U_ALLOC_LINE(line_len + 1);
868 else
869 {
870 line = NULL;
871 corruption_error("line length", file_name);
872 }
873 if (line == NULL)
874 {
875 *error = TRUE;
876 return uep;
877 }
878 for (j = 0; j < line_len; j++)
879 line[j] = getc(fp);
880 line[j] = NUL;
881 array[i] = line;
882 }
883 return uep;
884}
885
886/*
887 * Serialize "pos" to "fp".
888 */
889 static void
890serialize_pos(pos, fp)
891 pos_T pos;
892 FILE *fp;
893{
894 put_bytes(fp, (long_u)pos.lnum, 4);
895 put_bytes(fp, (long_u)pos.col, 4);
896#ifdef FEAT_VIRTUALEDIT
897 put_bytes(fp, (long_u)pos.coladd, 4);
898#else
899 put_bytes(fp, (long_u)0, 4);
900#endif
901}
902
903/*
904 * Unserialize the pos_T at the current position in fp.
905 */
906 static void
907unserialize_pos(pos, fp)
908 pos_T *pos;
909 FILE *fp;
910{
911 pos->lnum = get4c(fp);
912 pos->col = get4c(fp);
913#ifdef FEAT_VIRTUALEDIT
914 pos->coladd = get4c(fp);
915#else
916 (void)get4c(fp);
917#endif
918}
919
920/*
921 * Serialize "info" to "fp".
922 */
923 static void
924serialize_visualinfo(info, fp)
925 visualinfo_T *info;
926 FILE *fp;
927{
928 serialize_pos(info->vi_start, fp);
929 serialize_pos(info->vi_end, fp);
930 put_bytes(fp, (long_u)info->vi_mode, 4);
931 put_bytes(fp, (long_u)info->vi_curswant, 4);
932}
933
934/*
935 * Unserialize the visualinfo_T at the current position in fp.
936 */
937 static void
938unserialize_visualinfo(info, fp)
939 visualinfo_T *info;
940 FILE *fp;
941{
942 unserialize_pos(&info->vi_start, fp);
943 unserialize_pos(&info->vi_end, fp);
944 info->vi_mode = get4c(fp);
945 info->vi_curswant = get4c(fp);
946}
947
948/*
949 * Write the pointer to an undo header. Instead of writing the pointer itself
950 * we use the sequence number of the header. This is converted back to
951 * pointers when reading. */
952 static void
953put_header_ptr(fp, uhp)
954 FILE *fp;
955 u_header_T *uhp;
956{
957 put_bytes(fp, (long_u)(uhp != NULL ? uhp->uh_seq : 0), 4);
958}
959
960/*
961 * Write the undo tree in an undo file.
962 * When "name" is not NULL, use it as the name of the undo file.
963 * Otherwise use buf->b_ffname to generate the undo file name.
964 * "buf" must never be null, buf->b_ffname is used to obtain the original file
965 * permissions.
966 * "forceit" is TRUE for ":wundo!", FALSE otherwise.
967 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text.
968 */
969 void
970u_write_undo(name, forceit, buf, hash)
971 char_u *name;
972 int forceit;
973 buf_T *buf;
974 char_u *hash;
975{
976 u_header_T *uhp;
977 u_entry_T *uep;
978 char_u *file_name;
979 int str_len, i, mark;
980#ifdef U_DEBUG
981 int headers_written = 0;
982#endif
983 int fd;
984 FILE *fp = NULL;
985 int perm;
986 int write_ok = FALSE;
987#ifdef UNIX
988 int st_old_valid = FALSE;
989 struct stat st_old;
990 struct stat st_new;
991#endif
992
993 if (name == NULL)
994 {
995 file_name = u_get_undo_file_name(buf->b_ffname, FALSE);
996 if (file_name == NULL)
997 {
998 if (p_verbose > 0)
999 smsg((char_u *)_("Cannot write undo file in any directory in 'undodir'"));
1000 return;
1001 }
1002 }
1003 else
1004 file_name = name;
1005
1006 /*
1007 * Decide about the permission to use for the undo file. If the buffer
1008 * has a name use the permission of the original file. Otherwise only
1009 * allow the user to access the undo file.
1010 */
1011 perm = 0600;
1012 if (buf->b_ffname != NULL)
1013 {
1014#ifdef UNIX
1015 if (mch_stat((char *)buf->b_ffname, &st_old) >= 0)
1016 {
1017 perm = st_old.st_mode;
1018 st_old_valid = TRUE;
1019 }
1020#else
1021 perm = mch_getperm(buf->b_ffname);
1022 if (perm < 0)
1023 perm = 0600;
1024#endif
1025 }
1026
1027 /* strip any s-bit */
1028 perm = perm & 0777;
1029
1030 /* If the undo file already exists, verify that it actually is an undo
1031 * file, and delete it. */
1032 if (mch_getperm(file_name) >= 0)
1033 {
1034 if (name == NULL || !forceit)
1035 {
1036 /* Check we can read it and it's an undo file. */
1037 fd = mch_open((char *)file_name, O_RDONLY|O_EXTRA, 0);
1038 if (fd < 0)
1039 {
1040 if (name != NULL || p_verbose > 0)
1041 smsg((char_u *)_("Will not overwrite with undo file, cannot read: %s"),
1042 file_name);
1043 goto theend;
1044 }
1045 else
1046 {
1047 char_u buf[UF_START_MAGIC_LEN];
1048 int len;
1049
1050 len = vim_read(fd, buf, UF_START_MAGIC_LEN);
1051 close(fd);
1052 if (len < UF_START_MAGIC_LEN
1053 || memcmp(buf, UF_START_MAGIC, UF_START_MAGIC_LEN) != 0)
1054 {
1055 if (name != NULL || p_verbose > 0)
1056 smsg((char_u *)_("Will not overwrite, this is not an undo file: %s"),
1057 file_name);
1058 goto theend;
1059 }
1060 }
1061 }
1062 mch_remove(file_name);
1063 }
1064
1065 fd = mch_open((char *)file_name,
1066 O_CREAT|O_EXTRA|O_WRONLY|O_EXCL|O_NOFOLLOW, perm);
1067 if (fd < 0)
1068 {
1069 EMSG2(_(e_not_open), file_name);
1070 goto theend;
1071 }
1072 (void)mch_setperm(file_name, perm);
1073 if (p_verbose > 0)
1074 smsg((char_u *)_("Writing undo file: %s"), file_name);
1075
1076#ifdef UNIX
1077 /*
1078 * Try to set the group of the undo file same as the original file. If
1079 * this fails, set the protection bits for the group same as the
1080 * protection bits for others.
1081 */
1082 if (st_old_valid && (mch_stat((char *)file_name, &st_new) >= 0
1083 && st_new.st_gid != st_old.st_gid
1084# ifdef HAVE_FCHOWN /* sequent-ptx lacks fchown() */
1085 && fchown(fd, (uid_t)-1, st_old.st_gid) != 0)
1086# endif
1087 )
1088 mch_setperm(file_name, (perm & 0707) | ((perm & 07) << 3));
1089# ifdef HAVE_SELINUX
1090 if (buf->b_ffname != NULL)
1091 mch_copy_sec(buf->b_ffname, file_name);
1092# endif
1093#endif
1094
1095 fp = fdopen(fd, "w");
1096 if (fp == NULL)
1097 {
1098 EMSG2(_(e_not_open), file_name);
1099 close(fd);
1100 mch_remove(file_name);
1101 goto theend;
1102 }
1103
1104 /* Start writing, first the undo file header. */
1105 if (fwrite(UF_START_MAGIC, (size_t)UF_START_MAGIC_LEN, (size_t)1, fp) != 1)
1106 goto write_error;
1107 put_bytes(fp, (long_u)UF_VERSION, 2);
1108
1109 /* Write a hash of the buffer text, so that we can verify it is still the
1110 * same when reading the buffer text. */
1111 if (fwrite(hash, (size_t)UNDO_HASH_SIZE, (size_t)1, fp) != 1)
1112 goto write_error;
1113 put_bytes(fp, (long_u)buf->b_ml.ml_line_count, 4);
1114
1115 /* Begin undo data for U */
1116 str_len = buf->b_u_line_ptr != NULL ? (int)STRLEN(buf->b_u_line_ptr) : 0;
1117 put_bytes(fp, (long_u)str_len, 4);
1118 if (str_len > 0 && fwrite(buf->b_u_line_ptr, (size_t)str_len,
1119 (size_t)1, fp) != 1)
1120 goto write_error;
1121
1122 put_bytes(fp, (long_u)buf->b_u_line_lnum, 4);
1123 put_bytes(fp, (long_u)buf->b_u_line_colnr, 4);
1124
1125 /* Begin general undo data */
1126 put_header_ptr(fp, buf->b_u_oldhead);
1127 put_header_ptr(fp, buf->b_u_newhead);
1128 put_header_ptr(fp, buf->b_u_curhead);
1129
1130 put_bytes(fp, (long_u)buf->b_u_numhead, 4);
1131 put_bytes(fp, (long_u)buf->b_u_seq_last, 4);
1132 put_bytes(fp, (long_u)buf->b_u_seq_cur, 4);
1133 put_time(fp, buf->b_u_seq_time);
1134
1135 /*
1136 * Iteratively serialize UHPs and their UEPs from the top down.
1137 */
1138 mark = ++lastmark;
1139 uhp = buf->b_u_oldhead;
1140 while (uhp != NULL)
1141 {
1142 /* Serialize current UHP if we haven't seen it */
1143 if (uhp->uh_walk != mark)
1144 {
1145 uhp->uh_walk = mark;
1146#ifdef U_DEBUG
1147 ++headers_written;
1148#endif
1149
1150 if (put_bytes(fp, (long_u)UF_HEADER_MAGIC, 2) == FAIL)
1151 goto write_error;
1152
1153 put_header_ptr(fp, uhp->uh_next);
1154 put_header_ptr(fp, uhp->uh_prev);
1155 put_header_ptr(fp, uhp->uh_alt_next);
1156 put_header_ptr(fp, uhp->uh_alt_prev);
1157 put_bytes(fp, uhp->uh_seq, 4);
1158 serialize_pos(uhp->uh_cursor, fp);
1159#ifdef FEAT_VIRTUALEDIT
1160 put_bytes(fp, (long_u)uhp->uh_cursor_vcol, 4);
1161#else
1162 put_bytes(fp, (long_u)0, 4);
1163#endif
1164 put_bytes(fp, (long_u)uhp->uh_flags, 2);
1165 /* Assume NMARKS will stay the same. */
1166 for (i = 0; i < NMARKS; ++i)
1167 serialize_pos(uhp->uh_namedm[i], fp);
1168#ifdef FEAT_VISUAL
1169 serialize_visualinfo(&uhp->uh_visual, fp);
1170#else
1171 {
1172 visualinfo_T info;
1173
1174 memset(&info, 0, sizeof(visualinfo_T));
1175 serialize_visualinfo(&info, fp);
1176 }
1177#endif
1178 put_time(fp, uhp->uh_time);
1179
1180 /* Write all the entries. */
1181 for (uep = uhp->uh_entry; uep != NULL; uep = uep->ue_next)
1182 {
1183 put_bytes(fp, (long_u)UF_ENTRY_MAGIC, 2);
1184 if (serialize_uep(uep, fp) == FAIL)
1185 goto write_error;
1186 }
1187 put_bytes(fp, (long_u)UF_ENTRY_END_MAGIC, 2);
1188 }
1189
1190 /* Now walk through the tree - algorithm from undo_time */
1191 if (uhp->uh_prev != NULL && uhp->uh_prev->uh_walk != mark)
1192 uhp = uhp->uh_prev;
1193 else if (uhp->uh_alt_next != NULL && uhp->uh_alt_next->uh_walk != mark)
1194 uhp = uhp->uh_alt_next;
1195 else if (uhp->uh_next != NULL && uhp->uh_alt_prev == NULL
1196 && uhp->uh_next->uh_walk != mark)
1197 uhp = uhp->uh_next;
1198 else if (uhp->uh_alt_prev != NULL)
1199 uhp = uhp->uh_alt_prev;
1200 else
1201 uhp = uhp->uh_next;
1202 }
1203
1204 if (put_bytes(fp, (long_u)UF_HEADER_END_MAGIC, 2) == OK)
1205 write_ok = TRUE;
1206#ifdef U_DEBUG
1207 if (headers_written != buf->b_u_numhead)
1208 EMSG3("Written %ld headers, but numhead is %ld",
1209 headers_written, buf->b_u_numhead);
1210#endif
1211
1212write_error:
1213 fclose(fp);
1214 if (!write_ok)
1215 EMSG2(_("E829: write error in undo file: %s"), file_name);
1216
1217#if defined(MACOS_CLASSIC) || defined(WIN3264)
1218 if (buf->b_ffname != NULL)
1219 (void)mch_copy_file_attribute(buf->b_ffname, file_name);
1220#endif
1221#ifdef HAVE_ACL
1222 if (buf->b_ffname != NULL)
1223 {
1224 vim_acl_T acl;
1225
1226 /* For systems that support ACL: get the ACL from the original file. */
1227 acl = mch_get_acl(buf->b_ffname);
1228 mch_set_acl(file_name, acl);
1229 }
1230#endif
1231
1232theend:
1233 if (file_name != name)
1234 vim_free(file_name);
1235}
1236
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001237/*
1238 * Load the undo tree from an undo file.
1239 * If "name" is not NULL use it as the undo file name. This also means being
1240 * a bit more verbose.
1241 * Otherwise use curbuf->b_ffname to generate the undo file name.
1242 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text.
1243 */
1244 void
1245u_read_undo(name, hash)
1246 char_u *name;
1247 char_u *hash;
1248{
1249 char_u *file_name;
1250 FILE *fp;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001251 long version, str_len;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001252 char_u *line_ptr = NULL;
1253 linenr_T line_lnum;
1254 colnr_T line_colnr;
1255 linenr_T line_count;
Bram Moolenaar442b4222010-05-24 21:34:22 +02001256 int num_head = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001257 long old_header_seq, new_header_seq, cur_header_seq;
1258 long seq_last, seq_cur;
1259 short old_idx = -1, new_idx = -1, cur_idx = -1;
1260 long num_read_uhps = 0;
1261 time_t seq_time;
1262 int i, j;
1263 int c;
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001264 u_entry_T *uep, *last_uep;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001265 u_header_T *uhp;
1266 u_header_T **uhp_table = NULL;
1267 char_u read_hash[UNDO_HASH_SIZE];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001268 char_u magic_buf[UF_START_MAGIC_LEN];
1269#ifdef U_DEBUG
1270 int *uhp_table_used;
1271#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001272
1273 if (name == NULL)
1274 {
1275 file_name = u_get_undo_file_name(curbuf->b_ffname, TRUE);
1276 if (file_name == NULL)
1277 return;
1278 }
1279 else
1280 file_name = name;
1281
1282 if (p_verbose > 0)
1283 smsg((char_u *)_("Reading undo file: %s"), file_name);
1284 fp = mch_fopen((char *)file_name, "r");
1285 if (fp == NULL)
1286 {
1287 if (name != NULL || p_verbose > 0)
1288 EMSG2(_("E822: Cannot open undo file for reading: %s"), file_name);
1289 goto error;
1290 }
1291
Bram Moolenaar9db58062010-05-29 20:33:07 +02001292 /*
1293 * Read the undo file header.
1294 */
1295 if (fread(magic_buf, UF_START_MAGIC_LEN, 1, fp) != 1
1296 || memcmp(magic_buf, UF_START_MAGIC, UF_START_MAGIC_LEN) != 0)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001297 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001298 EMSG2(_("E823: Not an undo file: %s"), file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001299 goto error;
1300 }
1301 version = get2c(fp);
1302 if (version != UF_VERSION)
1303 {
1304 EMSG2(_("E824: Incompatible undo file: %s"), file_name);
1305 goto error;
1306 }
1307
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001308 if (fread(read_hash, UNDO_HASH_SIZE, 1, fp) != 1)
1309 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001310 corruption_error("hash", file_name);
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001311 goto error;
1312 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001313 line_count = (linenr_T)get4c(fp);
1314 if (memcmp(hash, read_hash, UNDO_HASH_SIZE) != 0
1315 || line_count != curbuf->b_ml.ml_line_count)
1316 {
1317 if (p_verbose > 0 || name != NULL)
1318 {
1319 verbose_enter();
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001320 give_warning((char_u *)_("File contents changed, cannot use undo info"), TRUE);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001321 verbose_leave();
1322 }
1323 goto error;
1324 }
1325
1326 /* Begin undo data for U */
1327 str_len = get4c(fp);
1328 if (str_len < 0)
1329 goto error;
1330 else if (str_len > 0)
1331 {
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001332 if ((line_ptr = U_ALLOC_LINE(str_len + 1)) == NULL)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001333 goto error;
1334 for (i = 0; i < str_len; i++)
1335 line_ptr[i] = (char_u)getc(fp);
1336 line_ptr[i] = NUL;
1337 }
1338 line_lnum = (linenr_T)get4c(fp);
1339 line_colnr = (colnr_T)get4c(fp);
1340
1341 /* Begin general undo data */
1342 old_header_seq = get4c(fp);
1343 new_header_seq = get4c(fp);
1344 cur_header_seq = get4c(fp);
1345 num_head = get4c(fp);
1346 seq_last = get4c(fp);
1347 seq_cur = get4c(fp);
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001348 seq_time = get8ctime(fp);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001349
1350 /* uhp_table will store the freshly created undo headers we allocate
1351 * until we insert them into curbuf. The table remains sorted by the
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001352 * sequence numbers of the headers.
1353 * When there are no headers uhp_table is NULL. */
1354 if (num_head > 0)
1355 {
1356 uhp_table = (u_header_T **)U_ALLOC_LINE(
1357 num_head * sizeof(u_header_T *));
1358 if (uhp_table == NULL)
1359 goto error;
1360 vim_memset(uhp_table, 0, num_head * sizeof(u_header_T *));
1361 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001362
Bram Moolenaar9db58062010-05-29 20:33:07 +02001363 while ((c = get2c(fp)) == UF_HEADER_MAGIC)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001364 {
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001365 if (num_read_uhps >= num_head)
1366 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001367 corruption_error("num_head", file_name);
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001368 u_free_uhp(uhp);
1369 goto error;
1370 }
1371
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001372 uhp = (u_header_T *)U_ALLOC_LINE(sizeof(u_header_T));
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001373 if (uhp == NULL)
1374 goto error;
1375 vim_memset(uhp, 0, sizeof(u_header_T));
Bram Moolenaar9db58062010-05-29 20:33:07 +02001376#ifdef U_DEBUG
1377 uhp->uh_magic = UH_MAGIC;
1378#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001379 /* We're not actually trying to store pointers here. We're just storing
1380 * IDs so we can swizzle them into pointers later - hence the type
1381 * cast. */
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001382 uhp->uh_next = (u_header_T *)(long_u)get4c(fp);
1383 uhp->uh_prev = (u_header_T *)(long_u)get4c(fp);
1384 uhp->uh_alt_next = (u_header_T *)(long_u)get4c(fp);
1385 uhp->uh_alt_prev = (u_header_T *)(long_u)get4c(fp);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001386 uhp->uh_seq = get4c(fp);
1387 if (uhp->uh_seq <= 0)
1388 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001389 corruption_error("uh_seq", file_name);
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001390 vim_free(uhp);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001391 goto error;
1392 }
1393 uhp->uh_walk = 0;
1394 unserialize_pos(&uhp->uh_cursor, fp);
1395#ifdef FEAT_VIRTUALEDIT
1396 uhp->uh_cursor_vcol = get4c(fp);
1397#else
1398 (void)get4c(fp);
1399#endif
1400 uhp->uh_flags = get2c(fp);
1401 for (i = 0; i < NMARKS; ++i)
1402 unserialize_pos(&uhp->uh_namedm[i], fp);
1403#ifdef FEAT_VISUAL
1404 unserialize_visualinfo(&uhp->uh_visual, fp);
1405#else
1406 {
1407 visualinfo_T info;
1408 unserialize_visualinfo(&info, fp);
1409 }
1410#endif
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001411 uhp->uh_time = get8ctime(fp);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001412
Bram Moolenaar9db58062010-05-29 20:33:07 +02001413 /* Unserialize the uep list. */
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001414 last_uep = NULL;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001415 while ((c = get2c(fp)) == UF_ENTRY_MAGIC)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001416 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001417 int error = FALSE;
1418
1419 uep = unserialize_uep(fp, &error, file_name);
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001420 if (last_uep == NULL)
1421 uhp->uh_entry = uep;
1422 else
1423 last_uep->ue_next = uep;
1424 last_uep = uep;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001425 if (uep == NULL || error)
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001426 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001427 u_free_uhp(uhp);
1428 goto error;
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001429 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001430 }
1431 if (c != UF_ENTRY_END_MAGIC)
1432 {
1433 corruption_error("entry end", file_name);
1434 u_free_uhp(uhp);
1435 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001436 }
1437
1438 /* Insertion sort the uhp into the table by its uh_seq. This is
1439 * required because, while the number of uhps is limited to
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001440 * num_head, and the uh_seq order is monotonic with respect to
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001441 * creation time, the starting uh_seq can be > 0 if any undolevel
1442 * culling was done at undofile write time, and there can be uh_seq
1443 * gaps in the uhps.
1444 */
1445 for (i = num_read_uhps - 1; i >= -1; i--)
1446 {
1447 /* if i == -1, we've hit the leftmost side of the table, so insert
1448 * at uhp_table[0]. */
1449 if (i == -1 || uhp->uh_seq > uhp_table[i]->uh_seq)
1450 {
1451 /* If we've had to move from the rightmost side of the table,
1452 * we have to shift everything to the right by one spot. */
Bram Moolenaar9d728072010-05-24 22:06:04 +02001453 if (num_read_uhps - i - 1 > 0)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001454 {
1455 memmove(uhp_table + i + 2, uhp_table + i + 1,
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001456 (num_read_uhps - i - 1) * sizeof(u_header_T *));
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001457 }
1458 uhp_table[i + 1] = uhp;
1459 break;
1460 }
1461 else if (uhp->uh_seq == uhp_table[i]->uh_seq)
1462 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001463 corruption_error("duplicate uh_seq", file_name);
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001464 u_free_uhp(uhp);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001465 goto error;
1466 }
1467 }
1468 num_read_uhps++;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001469 }
1470
Bram Moolenaar9db58062010-05-29 20:33:07 +02001471 if (c != UF_HEADER_END_MAGIC)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001472 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001473 corruption_error("end marker", file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001474 goto error;
1475 }
1476
Bram Moolenaar9db58062010-05-29 20:33:07 +02001477#ifdef U_DEBUG
1478 uhp_table_used = (int *)alloc_clear(
1479 (unsigned)(sizeof(int) * num_head + 1));
1480# define SET_FLAG(j) ++uhp_table_used[j]
1481#else
1482# define SET_FLAG(j)
1483#endif
1484
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001485 /* We've organized all of the uhps into a table sorted by uh_seq. Now we
1486 * iterate through the table and swizzle each sequence number we've
Bram Moolenaar9db58062010-05-29 20:33:07 +02001487 * stored in uh_* into a pointer corresponding to the header with that
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001488 * sequence number. Then free curbuf's old undo structure, give curbuf
1489 * the updated {old,new,cur}head pointers, and then free the table. */
1490 for (i = 0; i < num_head; i++)
1491 {
1492 uhp = uhp_table[i];
1493 if (uhp == NULL)
1494 continue;
1495 for (j = 0; j < num_head; j++)
1496 {
1497 if (uhp_table[j] == NULL)
1498 continue;
1499 if (uhp_table[j]->uh_seq == (long)uhp->uh_next)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001500 {
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001501 uhp->uh_next = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001502 SET_FLAG(j);
1503 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001504 if (uhp_table[j]->uh_seq == (long)uhp->uh_prev)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001505 {
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001506 uhp->uh_prev = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001507 SET_FLAG(j);
1508 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001509 if (uhp_table[j]->uh_seq == (long)uhp->uh_alt_next)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001510 {
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001511 uhp->uh_alt_next = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001512 SET_FLAG(j);
1513 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001514 if (uhp_table[j]->uh_seq == (long)uhp->uh_alt_prev)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001515 {
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001516 uhp->uh_alt_prev = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001517 SET_FLAG(j);
1518 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001519 }
1520 if (old_header_seq > 0 && old_idx < 0 && uhp->uh_seq == old_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001521 {
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001522 old_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001523 SET_FLAG(i);
1524 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001525 if (new_header_seq > 0 && new_idx < 0 && uhp->uh_seq == new_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001526 {
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001527 new_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001528 SET_FLAG(i);
1529 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001530 if (cur_header_seq > 0 && cur_idx < 0 && uhp->uh_seq == cur_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001531 {
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001532 cur_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001533 SET_FLAG(i);
1534 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001535 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001536
1537 /* Now that we have read the undo info successfully, free the current undo
1538 * info and use the info from the file. */
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001539 u_blockfree(curbuf);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001540 curbuf->b_u_oldhead = old_idx < 0 ? NULL : uhp_table[old_idx];
1541 curbuf->b_u_newhead = new_idx < 0 ? NULL : uhp_table[new_idx];
1542 curbuf->b_u_curhead = cur_idx < 0 ? NULL : uhp_table[cur_idx];
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001543 curbuf->b_u_line_ptr = line_ptr;
1544 curbuf->b_u_line_lnum = line_lnum;
1545 curbuf->b_u_line_colnr = line_colnr;
1546 curbuf->b_u_numhead = num_head;
1547 curbuf->b_u_seq_last = seq_last;
1548 curbuf->b_u_seq_cur = seq_cur;
1549 curbuf->b_u_seq_time = seq_time;
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001550 vim_free(uhp_table);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001551
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001552#ifdef U_DEBUG
Bram Moolenaar9db58062010-05-29 20:33:07 +02001553 for (i = 0; i < num_head; ++i)
1554 if (uhp_table_used[i] == 0)
1555 EMSGN("uhp_table entry %ld not used, leaking memory", i);
1556 vim_free(uhp_table_used);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001557 u_check(TRUE);
1558#endif
Bram Moolenaar9db58062010-05-29 20:33:07 +02001559
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001560 if (name != NULL)
1561 smsg((char_u *)_("Finished reading undo file %s"), file_name);
1562 goto theend;
1563
1564error:
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001565 vim_free(line_ptr);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001566 if (uhp_table != NULL)
1567 {
1568 for (i = 0; i < num_head; i++)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001569 if (uhp_table[i] != NULL)
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001570 u_free_uhp(uhp_table[i]);
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001571 vim_free(uhp_table);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001572 }
1573
1574theend:
1575 if (fp != NULL)
1576 fclose(fp);
1577 if (file_name != name)
1578 vim_free(file_name);
1579 return;
1580}
1581
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001582#endif /* FEAT_PERSISTENT_UNDO */
1583
1584
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585/*
1586 * If 'cpoptions' contains 'u': Undo the previous undo or redo (vi compatible).
1587 * If 'cpoptions' does not contain 'u': Always undo.
1588 */
1589 void
1590u_undo(count)
1591 int count;
1592{
1593 /*
1594 * If we get an undo command while executing a macro, we behave like the
1595 * original vi. If this happens twice in one macro the result will not
1596 * be compatible.
1597 */
1598 if (curbuf->b_u_synced == FALSE)
1599 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001600 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 count = 1;
1602 }
1603
1604 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
1605 undo_undoes = TRUE;
1606 else
1607 undo_undoes = !undo_undoes;
1608 u_doit(count);
1609}
1610
1611/*
1612 * If 'cpoptions' contains 'u': Repeat the previous undo or redo.
1613 * If 'cpoptions' does not contain 'u': Always redo.
1614 */
1615 void
1616u_redo(count)
1617 int count;
1618{
1619 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
1620 undo_undoes = FALSE;
1621 u_doit(count);
1622}
1623
1624/*
1625 * Undo or redo, depending on 'undo_undoes', 'count' times.
1626 */
1627 static void
Bram Moolenaarca003e12006-03-17 23:19:38 +00001628u_doit(startcount)
1629 int startcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630{
Bram Moolenaarca003e12006-03-17 23:19:38 +00001631 int count = startcount;
1632
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001633 if (!undo_allowed())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635
1636 u_newcount = 0;
1637 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001638 if (curbuf->b_ml.ml_flags & ML_EMPTY)
1639 u_oldcount = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 while (count--)
1641 {
1642 if (undo_undoes)
1643 {
1644 if (curbuf->b_u_curhead == NULL) /* first undo */
1645 curbuf->b_u_curhead = curbuf->b_u_newhead;
1646 else if (p_ul > 0) /* multi level undo */
1647 /* get next undo */
1648 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_next;
1649 /* nothing to undo */
1650 if (curbuf->b_u_numhead == 0 || curbuf->b_u_curhead == NULL)
1651 {
1652 /* stick curbuf->b_u_curhead at end */
1653 curbuf->b_u_curhead = curbuf->b_u_oldhead;
1654 beep_flush();
Bram Moolenaarca003e12006-03-17 23:19:38 +00001655 if (count == startcount - 1)
1656 {
1657 MSG(_("Already at oldest change"));
1658 return;
1659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 break;
1661 }
1662
Bram Moolenaarca003e12006-03-17 23:19:38 +00001663 u_undoredo(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 }
1665 else
1666 {
1667 if (curbuf->b_u_curhead == NULL || p_ul <= 0)
1668 {
1669 beep_flush(); /* nothing to redo */
Bram Moolenaarca003e12006-03-17 23:19:38 +00001670 if (count == startcount - 1)
1671 {
1672 MSG(_("Already at newest change"));
1673 return;
1674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 break;
1676 }
1677
Bram Moolenaarca003e12006-03-17 23:19:38 +00001678 u_undoredo(FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00001679
1680 /* Advance for next redo. Set "newhead" when at the end of the
1681 * redoable changes. */
1682 if (curbuf->b_u_curhead->uh_prev == NULL)
1683 curbuf->b_u_newhead = curbuf->b_u_curhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_prev;
1685 }
1686 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001687 u_undo_end(undo_undoes, FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00001688}
1689
Bram Moolenaar1e607892006-03-13 22:15:53 +00001690/*
1691 * Undo or redo over the timeline.
1692 * When "step" is negative go back in time, otherwise goes forward in time.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001693 * When "sec" is FALSE make "step" steps, when "sec" is TRUE use "step" as
1694 * seconds.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001695 * When "absolute" is TRUE use "step" as the sequence number to jump to.
1696 * "sec" must be FALSE then.
Bram Moolenaar1e607892006-03-13 22:15:53 +00001697 */
1698 void
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001699undo_time(step, sec, absolute)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001700 long step;
1701 int sec;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001702 int absolute;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001703{
1704 long target;
1705 long closest;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001706 long closest_start;
1707 long closest_seq = 0;
1708 long val;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001709 u_header_T *uhp;
1710 u_header_T *last;
1711 int mark;
1712 int nomark;
1713 int round;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001714 int dosec = sec;
1715 int above = FALSE;
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001716 int did_undo = TRUE;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001717
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001718 /* First make sure the current undoable change is synced. */
1719 if (curbuf->b_u_synced == FALSE)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001720 u_sync(TRUE);
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001721
Bram Moolenaar1e607892006-03-13 22:15:53 +00001722 u_newcount = 0;
1723 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001724 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar1e607892006-03-13 22:15:53 +00001725 u_oldcount = -1;
1726
Bram Moolenaarca003e12006-03-17 23:19:38 +00001727 /* "target" is the node below which we want to be.
1728 * Init "closest" to a value we can't reach. */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001729 if (absolute)
1730 {
1731 target = step;
Bram Moolenaarca003e12006-03-17 23:19:38 +00001732 closest = -1;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001733 }
Bram Moolenaarca003e12006-03-17 23:19:38 +00001734 else
Bram Moolenaar1e607892006-03-13 22:15:53 +00001735 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00001736 /* When doing computations with time_t subtract starttime, because
1737 * time_t converted to a long may result in a wrong number. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001738 if (sec)
Bram Moolenaarca003e12006-03-17 23:19:38 +00001739 target = (long)(curbuf->b_u_seq_time - starttime) + step;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001740 else
1741 target = curbuf->b_u_seq_cur + step;
Bram Moolenaarca003e12006-03-17 23:19:38 +00001742 if (step < 0)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001743 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00001744 if (target < 0)
1745 target = 0;
1746 closest = -1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001747 }
1748 else
1749 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00001750 if (sec)
Bram Moolenaardb552d602006-03-23 22:59:57 +00001751 closest = (long)(time(NULL) - starttime + 1);
Bram Moolenaarca003e12006-03-17 23:19:38 +00001752 else
1753 closest = curbuf->b_u_seq_last + 2;
1754 if (target >= closest)
1755 target = closest - 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001756 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00001757 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001758 closest_start = closest;
Bram Moolenaarca003e12006-03-17 23:19:38 +00001759 closest_seq = curbuf->b_u_seq_cur;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001760
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001761 /*
1762 * May do this twice:
Bram Moolenaar1e607892006-03-13 22:15:53 +00001763 * 1. Search for "target", update "closest" to the best match found.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001764 * 2. If "target" not found search for "closest".
1765 *
1766 * When using the closest time we use the sequence number in the second
1767 * round, because there may be several entries with the same time.
1768 */
Bram Moolenaar1e607892006-03-13 22:15:53 +00001769 for (round = 1; round <= 2; ++round)
1770 {
1771 /* Find the path from the current state to where we want to go. The
1772 * desired state can be anywhere in the undo tree, need to go all over
1773 * it. We put "nomark" in uh_walk where we have been without success,
1774 * "mark" where it could possibly be. */
1775 mark = ++lastmark;
1776 nomark = ++lastmark;
1777
1778 if (curbuf->b_u_curhead == NULL) /* at leaf of the tree */
1779 uhp = curbuf->b_u_newhead;
1780 else
1781 uhp = curbuf->b_u_curhead;
1782
1783 while (uhp != NULL)
1784 {
1785 uhp->uh_walk = mark;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001786 val = (long)(dosec ? (uhp->uh_time - starttime) : uhp->uh_seq);
Bram Moolenaar1e607892006-03-13 22:15:53 +00001787
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001788 if (round == 1)
1789 {
1790 /* Remember the header that is closest to the target.
1791 * It must be at least in the right direction (checked with
Bram Moolenaarca003e12006-03-17 23:19:38 +00001792 * "b_u_seq_cur"). When the timestamp is equal find the
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001793 * highest/lowest sequence number. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00001794 if ((step < 0 ? uhp->uh_seq <= curbuf->b_u_seq_cur
1795 : uhp->uh_seq > curbuf->b_u_seq_cur)
1796 && ((dosec && val == closest)
1797 ? (step < 0
1798 ? uhp->uh_seq < closest_seq
1799 : uhp->uh_seq > closest_seq)
1800 : closest == closest_start
1801 || (val > target
1802 ? (closest > target
1803 ? val - target <= closest - target
1804 : val - target <= target - closest)
1805 : (closest > target
1806 ? target - val <= closest - target
1807 : target - val <= target - closest))))
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001808 {
1809 closest = val;
1810 closest_seq = uhp->uh_seq;
1811 }
1812 }
1813
1814 /* Quit searching when we found a match. But when searching for a
1815 * time we need to continue looking for the best uh_seq. */
1816 if (target == val && !dosec)
1817 break;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001818
1819 /* go down in the tree if we haven't been there */
1820 if (uhp->uh_prev != NULL && uhp->uh_prev->uh_walk != nomark
1821 && uhp->uh_prev->uh_walk != mark)
1822 uhp = uhp->uh_prev;
1823
1824 /* go to alternate branch if we haven't been there */
1825 else if (uhp->uh_alt_next != NULL
1826 && uhp->uh_alt_next->uh_walk != nomark
1827 && uhp->uh_alt_next->uh_walk != mark)
1828 uhp = uhp->uh_alt_next;
1829
1830 /* go up in the tree if we haven't been there and we are at the
1831 * start of alternate branches */
1832 else if (uhp->uh_next != NULL && uhp->uh_alt_prev == NULL
1833 && uhp->uh_next->uh_walk != nomark
1834 && uhp->uh_next->uh_walk != mark)
Bram Moolenaardb552d602006-03-23 22:59:57 +00001835 {
1836 /* If still at the start we don't go through this change. */
1837 if (uhp == curbuf->b_u_curhead)
1838 uhp->uh_walk = nomark;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001839 uhp = uhp->uh_next;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001840 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00001841
1842 else
1843 {
1844 /* need to backtrack; mark this node as useless */
1845 uhp->uh_walk = nomark;
1846 if (uhp->uh_alt_prev != NULL)
1847 uhp = uhp->uh_alt_prev;
1848 else
1849 uhp = uhp->uh_next;
1850 }
1851 }
1852
1853 if (uhp != NULL) /* found it */
1854 break;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001855
1856 if (absolute)
1857 {
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001858 EMSGN(_("E830: Undo number %ld not found"), step);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001859 return;
1860 }
1861
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001862 if (closest == closest_start)
Bram Moolenaar1e607892006-03-13 22:15:53 +00001863 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001864 if (step < 0)
1865 MSG(_("Already at oldest change"));
1866 else
1867 MSG(_("Already at newest change"));
Bram Moolenaar1e607892006-03-13 22:15:53 +00001868 return;
1869 }
1870
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001871 target = closest_seq;
1872 dosec = FALSE;
1873 if (step < 0)
1874 above = TRUE; /* stop above the header */
Bram Moolenaar1e607892006-03-13 22:15:53 +00001875 }
1876
1877 /* If we found it: Follow the path to go to where we want to be. */
1878 if (uhp != NULL)
1879 {
1880 /*
1881 * First go up the tree as much as needed.
1882 */
1883 for (;;)
1884 {
1885 uhp = curbuf->b_u_curhead;
1886 if (uhp == NULL)
1887 uhp = curbuf->b_u_newhead;
1888 else
Bram Moolenaar1e607892006-03-13 22:15:53 +00001889 uhp = uhp->uh_next;
Bram Moolenaarca003e12006-03-17 23:19:38 +00001890 if (uhp == NULL || uhp->uh_walk != mark
1891 || (uhp->uh_seq == target && !above))
Bram Moolenaar1e607892006-03-13 22:15:53 +00001892 break;
1893 curbuf->b_u_curhead = uhp;
Bram Moolenaarca003e12006-03-17 23:19:38 +00001894 u_undoredo(TRUE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00001895 uhp->uh_walk = nomark; /* don't go back down here */
Bram Moolenaar1e607892006-03-13 22:15:53 +00001896 }
1897
1898 /*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001899 * And now go down the tree (redo), branching off where needed.
Bram Moolenaar1e607892006-03-13 22:15:53 +00001900 */
1901 uhp = curbuf->b_u_curhead;
Bram Moolenaarca003e12006-03-17 23:19:38 +00001902 while (uhp != NULL)
Bram Moolenaar1e607892006-03-13 22:15:53 +00001903 {
Bram Moolenaar89ed3df2007-01-09 19:23:12 +00001904 /* Go back to the first branch with a mark. */
1905 while (uhp->uh_alt_prev != NULL
1906 && uhp->uh_alt_prev->uh_walk == mark)
1907 uhp = uhp->uh_alt_prev;
1908
Bram Moolenaar1e607892006-03-13 22:15:53 +00001909 /* Find the last branch with a mark, that's the one. */
1910 last = uhp;
1911 while (last->uh_alt_next != NULL
1912 && last->uh_alt_next->uh_walk == mark)
1913 last = last->uh_alt_next;
1914 if (last != uhp)
1915 {
1916 /* Make the used branch the first entry in the list of
1917 * alternatives to make "u" and CTRL-R take this branch. */
Bram Moolenaar89ed3df2007-01-09 19:23:12 +00001918 while (uhp->uh_alt_prev != NULL)
1919 uhp = uhp->uh_alt_prev;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001920 if (last->uh_alt_next != NULL)
1921 last->uh_alt_next->uh_alt_prev = last->uh_alt_prev;
1922 last->uh_alt_prev->uh_alt_next = last->uh_alt_next;
1923 last->uh_alt_prev = NULL;
1924 last->uh_alt_next = uhp;
1925 uhp->uh_alt_prev = last;
1926
1927 uhp = last;
Bram Moolenaarca003e12006-03-17 23:19:38 +00001928 if (uhp->uh_next != NULL)
1929 uhp->uh_next->uh_prev = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001930 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001931 curbuf->b_u_curhead = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001932
1933 if (uhp->uh_walk != mark)
1934 break; /* must have reached the target */
1935
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001936 /* Stop when going backwards in time and didn't find the exact
1937 * header we were looking for. */
1938 if (uhp->uh_seq == target && above)
Bram Moolenaardb552d602006-03-23 22:59:57 +00001939 {
1940 curbuf->b_u_seq_cur = target - 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001941 break;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001942 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001943
Bram Moolenaarca003e12006-03-17 23:19:38 +00001944 u_undoredo(FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00001945
1946 /* Advance "curhead" to below the header we last used. If it
1947 * becomes NULL then we need to set "newhead" to this leaf. */
1948 if (uhp->uh_prev == NULL)
1949 curbuf->b_u_newhead = uhp;
1950 curbuf->b_u_curhead = uhp->uh_prev;
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001951 did_undo = FALSE;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001952
1953 if (uhp->uh_seq == target) /* found it! */
1954 break;
1955
1956 uhp = uhp->uh_prev;
1957 if (uhp == NULL || uhp->uh_walk != mark)
1958 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00001959 /* Need to redo more but can't find it... */
Bram Moolenaar1e607892006-03-13 22:15:53 +00001960 EMSG2(_(e_intern2), "undo_time()");
1961 break;
1962 }
1963 }
1964 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001965 u_undo_end(did_undo, absolute);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966}
1967
1968/*
1969 * u_undoredo: common code for undo and redo
1970 *
1971 * The lines in the file are replaced by the lines in the entry list at
1972 * curbuf->b_u_curhead. The replaced lines in the file are saved in the entry
1973 * list for the next undo/redo.
Bram Moolenaarca003e12006-03-17 23:19:38 +00001974 *
1975 * When "undo" is TRUE we go up in the tree, when FALSE we go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 */
1977 static void
Bram Moolenaarca003e12006-03-17 23:19:38 +00001978u_undoredo(undo)
1979 int undo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980{
1981 char_u **newarray = NULL;
1982 linenr_T oldsize;
1983 linenr_T newsize;
1984 linenr_T top, bot;
1985 linenr_T lnum;
1986 linenr_T newlnum = MAXLNUM;
1987 long i;
1988 u_entry_T *uep, *nuep;
1989 u_entry_T *newlist = NULL;
1990 int old_flags;
1991 int new_flags;
1992 pos_T namedm[NMARKS];
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001993#ifdef FEAT_VISUAL
1994 visualinfo_T visualinfo;
1995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 int empty_buffer; /* buffer became empty */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001997 u_header_T *curhead = curbuf->b_u_curhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998
Bram Moolenaarfecb6602007-10-01 20:54:15 +00001999#ifdef U_DEBUG
2000 u_check(FALSE);
2001#endif
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002002 old_flags = curhead->uh_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 new_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
2004 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
2005 setpcmark();
2006
2007 /*
2008 * save marks before undo/redo
2009 */
2010 mch_memmove(namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002011#ifdef FEAT_VISUAL
2012 visualinfo = curbuf->b_visual;
2013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 curbuf->b_op_start.lnum = curbuf->b_ml.ml_line_count;
2015 curbuf->b_op_start.col = 0;
2016 curbuf->b_op_end.lnum = 0;
2017 curbuf->b_op_end.col = 0;
2018
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002019 for (uep = curhead->uh_entry; uep != NULL; uep = nuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 {
2021 top = uep->ue_top;
2022 bot = uep->ue_bot;
2023 if (bot == 0)
2024 bot = curbuf->b_ml.ml_line_count + 1;
2025 if (top > curbuf->b_ml.ml_line_count || top >= bot
2026 || bot > curbuf->b_ml.ml_line_count + 1)
2027 {
2028 EMSG(_("E438: u_undo: line numbers wrong"));
2029 changed(); /* don't want UNCHANGED now */
2030 return;
2031 }
2032
2033 oldsize = bot - top - 1; /* number of lines before undo */
2034 newsize = uep->ue_size; /* number of lines after undo */
2035
2036 if (top < newlnum)
2037 {
2038 /* If the saved cursor is somewhere in this undo block, move it to
2039 * the remembered position. Makes "gwap" put the cursor back
2040 * where it was. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002041 lnum = curhead->uh_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 if (lnum >= top && lnum <= top + newsize + 1)
2043 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002044 curwin->w_cursor = curhead->uh_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 newlnum = curwin->w_cursor.lnum - 1;
2046 }
2047 else
2048 {
2049 /* Use the first line that actually changed. Avoids that
2050 * undoing auto-formatting puts the cursor in the previous
2051 * line. */
2052 for (i = 0; i < newsize && i < oldsize; ++i)
2053 if (STRCMP(uep->ue_array[i], ml_get(top + 1 + i)) != 0)
2054 break;
2055 if (i == newsize && newlnum == MAXLNUM && uep->ue_next == NULL)
2056 {
2057 newlnum = top;
2058 curwin->w_cursor.lnum = newlnum + 1;
2059 }
2060 else if (i < newsize)
2061 {
2062 newlnum = top + i;
2063 curwin->w_cursor.lnum = newlnum + 1;
2064 }
2065 }
2066 }
2067
2068 empty_buffer = FALSE;
2069
2070 /* delete the lines between top and bot and save them in newarray */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002071 if (oldsize > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002073 if ((newarray = (char_u **)U_ALLOC_LINE(
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002074 sizeof(char_u *) * oldsize)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 {
2076 do_outofmem_msg((long_u)(sizeof(char_u *) * oldsize));
2077 /*
2078 * We have messed up the entry list, repair is impossible.
2079 * we have to free the rest of the list.
2080 */
2081 while (uep != NULL)
2082 {
2083 nuep = uep->ue_next;
2084 u_freeentry(uep, uep->ue_size);
2085 uep = nuep;
2086 }
2087 break;
2088 }
2089 /* delete backwards, it goes faster in most cases */
2090 for (lnum = bot - 1, i = oldsize; --i >= 0; --lnum)
2091 {
2092 /* what can we do when we run out of memory? */
2093 if ((newarray[i] = u_save_line(lnum)) == NULL)
2094 do_outofmem_msg((long_u)0);
2095 /* remember we deleted the last line in the buffer, and a
2096 * dummy empty line will be inserted */
2097 if (curbuf->b_ml.ml_line_count == 1)
2098 empty_buffer = TRUE;
2099 ml_delete(lnum, FALSE);
2100 }
2101 }
Bram Moolenaar8d343302005-07-12 22:46:17 +00002102 else
2103 newarray = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104
2105 /* insert the lines in u_array between top and bot */
2106 if (newsize)
2107 {
2108 for (lnum = top, i = 0; i < newsize; ++i, ++lnum)
2109 {
2110 /*
2111 * If the file is empty, there is an empty line 1 that we
2112 * should get rid of, by replacing it with the new line
2113 */
2114 if (empty_buffer && lnum == 0)
2115 ml_replace((linenr_T)1, uep->ue_array[i], TRUE);
2116 else
2117 ml_append(lnum, uep->ue_array[i], (colnr_T)0, FALSE);
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002118 vim_free(uep->ue_array[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 }
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002120 vim_free((char_u *)uep->ue_array);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 }
2122
2123 /* adjust marks */
2124 if (oldsize != newsize)
2125 {
2126 mark_adjust(top + 1, top + oldsize, (long)MAXLNUM,
2127 (long)newsize - (long)oldsize);
2128 if (curbuf->b_op_start.lnum > top + oldsize)
2129 curbuf->b_op_start.lnum += newsize - oldsize;
2130 if (curbuf->b_op_end.lnum > top + oldsize)
2131 curbuf->b_op_end.lnum += newsize - oldsize;
2132 }
2133
2134 changed_lines(top + 1, 0, bot, newsize - oldsize);
2135
2136 /* set '[ and '] mark */
2137 if (top + 1 < curbuf->b_op_start.lnum)
2138 curbuf->b_op_start.lnum = top + 1;
2139 if (newsize == 0 && top + 1 > curbuf->b_op_end.lnum)
2140 curbuf->b_op_end.lnum = top + 1;
2141 else if (top + newsize > curbuf->b_op_end.lnum)
2142 curbuf->b_op_end.lnum = top + newsize;
2143
2144 u_newcount += newsize;
2145 u_oldcount += oldsize;
2146 uep->ue_size = oldsize;
2147 uep->ue_array = newarray;
2148 uep->ue_bot = top + newsize + 1;
2149
2150 /*
2151 * insert this entry in front of the new entry list
2152 */
2153 nuep = uep->ue_next;
2154 uep->ue_next = newlist;
2155 newlist = uep;
2156 }
2157
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002158 curhead->uh_entry = newlist;
2159 curhead->uh_flags = new_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 if ((old_flags & UH_EMPTYBUF) && bufempty())
2161 curbuf->b_ml.ml_flags |= ML_EMPTY;
2162 if (old_flags & UH_CHANGED)
2163 changed();
2164 else
Bram Moolenaar009b2592004-10-24 19:18:58 +00002165#ifdef FEAT_NETBEANS_INTG
2166 /* per netbeans undo rules, keep it as modified */
2167 if (!isNetbeansModified(curbuf))
2168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 unchanged(curbuf, FALSE);
2170
2171 /*
2172 * restore marks from before undo/redo
2173 */
2174 for (i = 0; i < NMARKS; ++i)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002175 if (curhead->uh_namedm[i].lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002177 curbuf->b_namedm[i] = curhead->uh_namedm[i];
2178 curhead->uh_namedm[i] = namedm[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002180#ifdef FEAT_VISUAL
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002181 if (curhead->uh_visual.vi_start.lnum != 0)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002182 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002183 curbuf->b_visual = curhead->uh_visual;
2184 curhead->uh_visual = visualinfo;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002185 }
2186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187
2188 /*
2189 * If the cursor is only off by one line, put it at the same position as
2190 * before starting the change (for the "o" command).
2191 * Otherwise the cursor should go to the first undone line.
2192 */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002193 if (curhead->uh_cursor.lnum + 1 == curwin->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 && curwin->w_cursor.lnum > 1)
2195 --curwin->w_cursor.lnum;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002196 if (curhead->uh_cursor.lnum == curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002198 curwin->w_cursor.col = curhead->uh_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002200 if (virtual_active() && curhead->uh_cursor_vcol >= 0)
2201 coladvance((colnr_T)curhead->uh_cursor_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 else
2203 curwin->w_cursor.coladd = 0;
2204#endif
2205 }
2206 else if (curwin->w_cursor.lnum <= curbuf->b_ml.ml_line_count)
2207 beginline(BL_SOL | BL_FIX);
2208 else
2209 {
2210 /* We get here with the current cursor line being past the end (eg
2211 * after adding lines at the end of the file, and then undoing it).
2212 * check_cursor() will move the cursor to the last line. Move it to
2213 * the first column here. */
2214 curwin->w_cursor.col = 0;
2215#ifdef FEAT_VIRTUALEDIT
2216 curwin->w_cursor.coladd = 0;
2217#endif
2218 }
2219
2220 /* Make sure the cursor is on an existing line and column. */
2221 check_cursor();
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002222
2223 /* Remember where we are for "g-" and ":earlier 10s". */
2224 curbuf->b_u_seq_cur = curhead->uh_seq;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002225 if (undo)
2226 /* We are below the previous undo. However, to make ":earlier 1s"
2227 * work we compute this as being just above the just undone change. */
2228 --curbuf->b_u_seq_cur;
2229
2230 /* The timestamp can be the same for multiple changes, just use the one of
2231 * the undone/redone change. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002232 curbuf->b_u_seq_time = curhead->uh_time;
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002233#ifdef U_DEBUG
2234 u_check(FALSE);
2235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236}
2237
2238/*
2239 * If we deleted or added lines, report the number of less/more lines.
2240 * Otherwise, report the number of changes (this may be incorrect
2241 * in some cases, but it's better than nothing).
2242 */
2243 static void
Bram Moolenaardb552d602006-03-23 22:59:57 +00002244u_undo_end(did_undo, absolute)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002245 int did_undo; /* just did an undo */
Bram Moolenaardb552d602006-03-23 22:59:57 +00002246 int absolute; /* used ":undo N" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247{
Bram Moolenaar89d40322006-08-29 15:30:07 +00002248 char *msgstr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002249 u_header_T *uhp;
2250 char_u msgbuf[80];
Bram Moolenaar1e607892006-03-13 22:15:53 +00002251
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252#ifdef FEAT_FOLDING
2253 if ((fdo_flags & FDO_UNDO) && KeyTyped)
2254 foldOpenCursor();
2255#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +00002256
2257 if (global_busy /* no messages now, wait until global is finished */
2258 || !messaging()) /* 'lazyredraw' set, don't do messages now */
2259 return;
2260
2261 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2262 --u_newcount;
2263
2264 u_oldcount -= u_newcount;
2265 if (u_oldcount == -1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002266 msgstr = N_("more line");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002267 else if (u_oldcount < 0)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002268 msgstr = N_("more lines");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002269 else if (u_oldcount == 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002270 msgstr = N_("line less");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002271 else if (u_oldcount > 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002272 msgstr = N_("fewer lines");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002273 else
2274 {
2275 u_oldcount = u_newcount;
2276 if (u_newcount == 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002277 msgstr = N_("change");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002278 else
Bram Moolenaar89d40322006-08-29 15:30:07 +00002279 msgstr = N_("changes");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002280 }
2281
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002282 if (curbuf->b_u_curhead != NULL)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002283 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00002284 /* For ":undo N" we prefer a "after #N" message. */
2285 if (absolute && curbuf->b_u_curhead->uh_next != NULL)
2286 {
2287 uhp = curbuf->b_u_curhead->uh_next;
2288 did_undo = FALSE;
2289 }
2290 else if (did_undo)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002291 uhp = curbuf->b_u_curhead;
2292 else
2293 uhp = curbuf->b_u_curhead->uh_next;
2294 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002295 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002296 uhp = curbuf->b_u_newhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002297
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002298 if (uhp == NULL)
2299 *msgbuf = NUL;
2300 else
2301 u_add_time(msgbuf, sizeof(msgbuf), uhp->uh_time);
2302
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002303 smsg((char_u *)_("%ld %s; %s #%ld %s"),
Bram Moolenaarca003e12006-03-17 23:19:38 +00002304 u_oldcount < 0 ? -u_oldcount : u_oldcount,
Bram Moolenaar89d40322006-08-29 15:30:07 +00002305 _(msgstr),
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002306 did_undo ? _("before") : _("after"),
2307 uhp == NULL ? 0L : uhp->uh_seq,
2308 msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309}
2310
2311/*
2312 * u_sync: stop adding to the current entry list
2313 */
2314 void
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002315u_sync(force)
2316 int force; /* Also sync when no_u_sync is set. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317{
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002318 /* Skip it when already synced or syncing is disabled. */
2319 if (curbuf->b_u_synced || (!force && no_u_sync > 0))
2320 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2322 if (im_is_preediting())
2323 return; /* XIM is busy, don't break an undo sequence */
2324#endif
2325 if (p_ul < 0)
2326 curbuf->b_u_synced = TRUE; /* no entries, nothing to do */
2327 else
2328 {
2329 u_getbot(); /* compute ue_bot of previous u_save */
2330 curbuf->b_u_curhead = NULL;
2331 }
2332}
2333
2334/*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002335 * ":undolist": List the leafs of the undo tree
2336 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002337 void
2338ex_undolist(eap)
Bram Moolenaarfff2bee2010-05-15 13:56:02 +02002339 exarg_T *eap UNUSED;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002340{
2341 garray_T ga;
2342 u_header_T *uhp;
2343 int mark;
2344 int nomark;
2345 int changes = 1;
2346 int i;
2347
2348 /*
2349 * 1: walk the tree to find all leafs, put the info in "ga".
2350 * 2: sort the lines
2351 * 3: display the list
2352 */
2353 mark = ++lastmark;
2354 nomark = ++lastmark;
2355 ga_init2(&ga, (int)sizeof(char *), 20);
2356
2357 uhp = curbuf->b_u_oldhead;
2358 while (uhp != NULL)
2359 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002360 if (uhp->uh_prev == NULL && uhp->uh_walk != nomark
2361 && uhp->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002362 {
2363 if (ga_grow(&ga, 1) == FAIL)
2364 break;
2365 vim_snprintf((char *)IObuff, IOSIZE, "%6ld %7ld ",
2366 uhp->uh_seq, changes);
2367 u_add_time(IObuff + STRLEN(IObuff), IOSIZE - STRLEN(IObuff),
2368 uhp->uh_time);
2369 ((char_u **)(ga.ga_data))[ga.ga_len++] = vim_strsave(IObuff);
2370 }
2371
2372 uhp->uh_walk = mark;
2373
2374 /* go down in the tree if we haven't been there */
2375 if (uhp->uh_prev != NULL && uhp->uh_prev->uh_walk != nomark
2376 && uhp->uh_prev->uh_walk != mark)
2377 {
2378 uhp = uhp->uh_prev;
2379 ++changes;
2380 }
2381
2382 /* go to alternate branch if we haven't been there */
2383 else if (uhp->uh_alt_next != NULL
2384 && uhp->uh_alt_next->uh_walk != nomark
2385 && uhp->uh_alt_next->uh_walk != mark)
2386 uhp = uhp->uh_alt_next;
2387
2388 /* go up in the tree if we haven't been there and we are at the
2389 * start of alternate branches */
2390 else if (uhp->uh_next != NULL && uhp->uh_alt_prev == NULL
2391 && uhp->uh_next->uh_walk != nomark
2392 && uhp->uh_next->uh_walk != mark)
2393 {
2394 uhp = uhp->uh_next;
2395 --changes;
2396 }
2397
2398 else
2399 {
2400 /* need to backtrack; mark this node as done */
2401 uhp->uh_walk = nomark;
2402 if (uhp->uh_alt_prev != NULL)
2403 uhp = uhp->uh_alt_prev;
2404 else
2405 {
2406 uhp = uhp->uh_next;
2407 --changes;
2408 }
2409 }
2410 }
2411
2412 if (ga.ga_len == 0)
2413 MSG(_("Nothing to undo"));
2414 else
2415 {
2416 sort_strings((char_u **)ga.ga_data, ga.ga_len);
2417
2418 msg_start();
2419 msg_puts_attr((char_u *)_("number changes time"), hl_attr(HLF_T));
2420 for (i = 0; i < ga.ga_len && !got_int; ++i)
2421 {
2422 msg_putchar('\n');
2423 if (got_int)
2424 break;
2425 msg_puts(((char_u **)ga.ga_data)[i]);
2426 }
2427 msg_end();
2428
2429 ga_clear_strings(&ga);
2430 }
2431}
2432
2433/*
2434 * Put the timestamp of an undo header in "buf[buflen]" in a nice format.
2435 */
2436 static void
2437u_add_time(buf, buflen, tt)
2438 char_u *buf;
2439 size_t buflen;
2440 time_t tt;
2441{
2442#ifdef HAVE_STRFTIME
2443 struct tm *curtime;
2444
2445 if (time(NULL) - tt >= 100)
2446 {
2447 curtime = localtime(&tt);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00002448 (void)strftime((char *)buf, buflen, "%H:%M:%S", curtime);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002449 }
2450 else
2451#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002452 vim_snprintf((char *)buf, buflen, _("%ld seconds ago"),
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002453 (long)(time(NULL) - tt));
2454}
2455
2456/*
Bram Moolenaare224ffa2006-03-01 00:01:28 +00002457 * ":undojoin": continue adding to the last entry list
2458 */
Bram Moolenaare224ffa2006-03-01 00:01:28 +00002459 void
2460ex_undojoin(eap)
Bram Moolenaarfff2bee2010-05-15 13:56:02 +02002461 exarg_T *eap UNUSED;
Bram Moolenaare224ffa2006-03-01 00:01:28 +00002462{
Bram Moolenaare224ffa2006-03-01 00:01:28 +00002463 if (curbuf->b_u_newhead == NULL)
2464 return; /* nothing changed before */
Bram Moolenaar57657d82006-04-21 22:12:41 +00002465 if (curbuf->b_u_curhead != NULL)
2466 {
2467 EMSG(_("E790: undojoin is not allowed after undo"));
2468 return;
2469 }
2470 if (!curbuf->b_u_synced)
2471 return; /* already unsynced */
Bram Moolenaare224ffa2006-03-01 00:01:28 +00002472 if (p_ul < 0)
2473 return; /* no entries, nothing to do */
2474 else
2475 {
2476 /* Go back to the last entry */
2477 curbuf->b_u_curhead = curbuf->b_u_newhead;
2478 curbuf->b_u_synced = FALSE; /* no entries, nothing to do */
2479 }
2480}
2481
2482/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 * Called after writing the file and setting b_changed to FALSE.
2484 * Now an undo means that the buffer is modified.
2485 */
2486 void
2487u_unchanged(buf)
2488 buf_T *buf;
2489{
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002490 u_unch_branch(buf->b_u_oldhead);
2491 buf->b_did_warn = FALSE;
2492}
2493
2494 static void
2495u_unch_branch(uhp)
2496 u_header_T *uhp;
2497{
Bram Moolenaar1e607892006-03-13 22:15:53 +00002498 u_header_T *uh;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002500 for (uh = uhp; uh != NULL; uh = uh->uh_prev)
2501 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 uh->uh_flags |= UH_CHANGED;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002503 if (uh->uh_alt_next != NULL)
2504 u_unch_branch(uh->uh_alt_next); /* recursive */
2505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506}
2507
2508/*
2509 * Get pointer to last added entry.
2510 * If it's not valid, give an error message and return NULL.
2511 */
2512 static u_entry_T *
2513u_get_headentry()
2514{
2515 if (curbuf->b_u_newhead == NULL || curbuf->b_u_newhead->uh_entry == NULL)
2516 {
2517 EMSG(_("E439: undo list corrupt"));
2518 return NULL;
2519 }
2520 return curbuf->b_u_newhead->uh_entry;
2521}
2522
2523/*
2524 * u_getbot(): compute the line number of the previous u_save
2525 * It is called only when b_u_synced is FALSE.
2526 */
2527 static void
2528u_getbot()
2529{
2530 u_entry_T *uep;
2531 linenr_T extra;
2532
2533 uep = u_get_headentry(); /* check for corrupt undo list */
2534 if (uep == NULL)
2535 return;
2536
2537 uep = curbuf->b_u_newhead->uh_getbot_entry;
2538 if (uep != NULL)
2539 {
2540 /*
2541 * the new ue_bot is computed from the number of lines that has been
2542 * inserted (0 - deleted) since calling u_save. This is equal to the
2543 * old line count subtracted from the current line count.
2544 */
2545 extra = curbuf->b_ml.ml_line_count - uep->ue_lcount;
2546 uep->ue_bot = uep->ue_top + uep->ue_size + 1 + extra;
2547 if (uep->ue_bot < 1 || uep->ue_bot > curbuf->b_ml.ml_line_count)
2548 {
2549 EMSG(_("E440: undo line missing"));
2550 uep->ue_bot = uep->ue_top + 1; /* assume all lines deleted, will
2551 * get all the old lines back
2552 * without deleting the current
2553 * ones */
2554 }
2555
2556 curbuf->b_u_newhead->uh_getbot_entry = NULL;
2557 }
2558
2559 curbuf->b_u_synced = TRUE;
2560}
2561
2562/*
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002563 * Free one header "uhp" and its entry list and adjust the pointers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 */
2565 static void
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002566u_freeheader(buf, uhp, uhpp)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002567 buf_T *buf;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002568 u_header_T *uhp;
2569 u_header_T **uhpp; /* if not NULL reset when freeing this header */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570{
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002571 u_header_T *uhap;
2572
Bram Moolenaar1e607892006-03-13 22:15:53 +00002573 /* When there is an alternate redo list free that branch completely,
2574 * because we can never go there. */
2575 if (uhp->uh_alt_next != NULL)
2576 u_freebranch(buf, uhp->uh_alt_next, uhpp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577
Bram Moolenaar1e607892006-03-13 22:15:53 +00002578 if (uhp->uh_alt_prev != NULL)
2579 uhp->uh_alt_prev->uh_alt_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580
Bram Moolenaar1e607892006-03-13 22:15:53 +00002581 /* Update the links in the list to remove the header. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 if (uhp->uh_next == NULL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002583 buf->b_u_oldhead = uhp->uh_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 else
2585 uhp->uh_next->uh_prev = uhp->uh_prev;
2586
2587 if (uhp->uh_prev == NULL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002588 buf->b_u_newhead = uhp->uh_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 else
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002590 for (uhap = uhp->uh_prev; uhap != NULL; uhap = uhap->uh_alt_next)
2591 uhap->uh_next = uhp->uh_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592
Bram Moolenaar1e607892006-03-13 22:15:53 +00002593 u_freeentries(buf, uhp, uhpp);
2594}
2595
2596/*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002597 * Free an alternate branch and any following alternate branches.
Bram Moolenaar1e607892006-03-13 22:15:53 +00002598 */
2599 static void
2600u_freebranch(buf, uhp, uhpp)
2601 buf_T *buf;
2602 u_header_T *uhp;
2603 u_header_T **uhpp; /* if not NULL reset when freeing this header */
2604{
2605 u_header_T *tofree, *next;
2606
Bram Moolenaar07d06772007-11-10 21:51:15 +00002607 /* If this is the top branch we may need to use u_freeheader() to update
2608 * all the pointers. */
2609 if (uhp == buf->b_u_oldhead)
2610 {
2611 u_freeheader(buf, uhp, uhpp);
2612 return;
2613 }
2614
Bram Moolenaar1e607892006-03-13 22:15:53 +00002615 if (uhp->uh_alt_prev != NULL)
2616 uhp->uh_alt_prev->uh_alt_next = NULL;
2617
2618 next = uhp;
2619 while (next != NULL)
2620 {
2621 tofree = next;
2622 if (tofree->uh_alt_next != NULL)
2623 u_freebranch(buf, tofree->uh_alt_next, uhpp); /* recursive */
2624 next = tofree->uh_prev;
2625 u_freeentries(buf, tofree, uhpp);
2626 }
2627}
2628
2629/*
2630 * Free all the undo entries for one header and the header itself.
2631 * This means that "uhp" is invalid when returning.
2632 */
2633 static void
2634u_freeentries(buf, uhp, uhpp)
2635 buf_T *buf;
2636 u_header_T *uhp;
2637 u_header_T **uhpp; /* if not NULL reset when freeing this header */
2638{
2639 u_entry_T *uep, *nuep;
2640
2641 /* Check for pointers to the header that become invalid now. */
2642 if (buf->b_u_curhead == uhp)
2643 buf->b_u_curhead = NULL;
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002644 if (buf->b_u_newhead == uhp)
2645 buf->b_u_newhead = NULL; /* freeing the newest entry */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002646 if (uhpp != NULL && uhp == *uhpp)
2647 *uhpp = NULL;
2648
2649 for (uep = uhp->uh_entry; uep != NULL; uep = nuep)
2650 {
2651 nuep = uep->ue_next;
2652 u_freeentry(uep, uep->ue_size);
2653 }
2654
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002655#ifdef U_DEBUG
2656 uhp->uh_magic = 0;
2657#endif
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002658 vim_free((char_u *)uhp);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002659 --buf->b_u_numhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660}
2661
2662/*
2663 * free entry 'uep' and 'n' lines in uep->ue_array[]
2664 */
2665 static void
2666u_freeentry(uep, n)
2667 u_entry_T *uep;
2668 long n;
2669{
Bram Moolenaar8d343302005-07-12 22:46:17 +00002670 while (n > 0)
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002671 vim_free(uep->ue_array[--n]);
2672 vim_free((char_u *)uep->ue_array);
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002673#ifdef U_DEBUG
2674 uep->ue_magic = 0;
2675#endif
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002676 vim_free((char_u *)uep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677}
2678
2679/*
2680 * invalidate the undo buffer; called when storage has already been released
2681 */
2682 void
2683u_clearall(buf)
2684 buf_T *buf;
2685{
2686 buf->b_u_newhead = buf->b_u_oldhead = buf->b_u_curhead = NULL;
2687 buf->b_u_synced = TRUE;
2688 buf->b_u_numhead = 0;
2689 buf->b_u_line_ptr = NULL;
2690 buf->b_u_line_lnum = 0;
2691}
2692
2693/*
2694 * save the line "lnum" for the "U" command
2695 */
2696 void
2697u_saveline(lnum)
2698 linenr_T lnum;
2699{
2700 if (lnum == curbuf->b_u_line_lnum) /* line is already saved */
2701 return;
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002702 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) /* should never happen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 return;
2704 u_clearline();
2705 curbuf->b_u_line_lnum = lnum;
2706 if (curwin->w_cursor.lnum == lnum)
2707 curbuf->b_u_line_colnr = curwin->w_cursor.col;
2708 else
2709 curbuf->b_u_line_colnr = 0;
2710 if ((curbuf->b_u_line_ptr = u_save_line(lnum)) == NULL)
2711 do_outofmem_msg((long_u)0);
2712}
2713
2714/*
2715 * clear the line saved for the "U" command
2716 * (this is used externally for crossing a line while in insert mode)
2717 */
2718 void
2719u_clearline()
2720{
2721 if (curbuf->b_u_line_ptr != NULL)
2722 {
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002723 vim_free(curbuf->b_u_line_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 curbuf->b_u_line_ptr = NULL;
2725 curbuf->b_u_line_lnum = 0;
2726 }
2727}
2728
2729/*
2730 * Implementation of the "U" command.
2731 * Differentiation from vi: "U" can be undone with the next "U".
2732 * We also allow the cursor to be in another line.
2733 */
2734 void
2735u_undoline()
2736{
2737 colnr_T t;
2738 char_u *oldp;
2739
2740 if (undo_off)
2741 return;
2742
Bram Moolenaare3300c82008-02-13 14:21:38 +00002743 if (curbuf->b_u_line_ptr == NULL
2744 || curbuf->b_u_line_lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 {
2746 beep_flush();
2747 return;
2748 }
Bram Moolenaare3300c82008-02-13 14:21:38 +00002749
2750 /* first save the line for the 'u' command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 if (u_savecommon(curbuf->b_u_line_lnum - 1,
2752 curbuf->b_u_line_lnum + 1, (linenr_T)0) == FAIL)
2753 return;
2754 oldp = u_save_line(curbuf->b_u_line_lnum);
2755 if (oldp == NULL)
2756 {
2757 do_outofmem_msg((long_u)0);
2758 return;
2759 }
2760 ml_replace(curbuf->b_u_line_lnum, curbuf->b_u_line_ptr, TRUE);
2761 changed_bytes(curbuf->b_u_line_lnum, 0);
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002762 vim_free(curbuf->b_u_line_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 curbuf->b_u_line_ptr = oldp;
2764
2765 t = curbuf->b_u_line_colnr;
2766 if (curwin->w_cursor.lnum == curbuf->b_u_line_lnum)
2767 curbuf->b_u_line_colnr = curwin->w_cursor.col;
2768 curwin->w_cursor.col = t;
2769 curwin->w_cursor.lnum = curbuf->b_u_line_lnum;
Bram Moolenaare3300c82008-02-13 14:21:38 +00002770 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771}
2772
2773/*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002774 * Free all allocated memory blocks for the buffer 'buf'.
2775 */
2776 void
2777u_blockfree(buf)
2778 buf_T *buf;
2779{
Bram Moolenaar1e607892006-03-13 22:15:53 +00002780 while (buf->b_u_oldhead != NULL)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002781 u_freeheader(buf, buf->b_u_oldhead, NULL);
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002782 vim_free(buf->b_u_line_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783}
2784
2785/*
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002786 * u_save_line(): allocate memory and copy line 'lnum' into it.
2787 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 */
2789 static char_u *
2790u_save_line(lnum)
2791 linenr_T lnum;
2792{
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002793 return vim_strsave(ml_get(lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794}
2795
2796/*
2797 * Check if the 'modified' flag is set, or 'ff' has changed (only need to
2798 * check the first character, because it can only be "dos", "unix" or "mac").
2799 * "nofile" and "scratch" type buffers are considered to always be unchanged.
2800 */
2801 int
2802bufIsChanged(buf)
2803 buf_T *buf;
2804{
2805 return
2806#ifdef FEAT_QUICKFIX
2807 !bt_dontwrite(buf) &&
2808#endif
2809 (buf->b_changed || file_ff_differs(buf));
2810}
2811
2812 int
2813curbufIsChanged()
2814{
2815 return
2816#ifdef FEAT_QUICKFIX
2817 !bt_dontwrite(curbuf) &&
2818#endif
2819 (curbuf->b_changed || file_ff_differs(curbuf));
2820}