blob: 7f1aa8ad87517ea0c744f9116399e4565714cba5 [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 }
Bram Moolenaar644fdff2010-05-30 13:26:21 +0200861 else
862 array = NULL;
Bram Moolenaar9db58062010-05-29 20:33:07 +0200863 uep->ue_array = array;
864
865 for (i = 0; i < uep->ue_size; ++i)
866 {
867 line_len = get4c(fp);
868 if (line_len >= 0)
869 line = (char_u *)U_ALLOC_LINE(line_len + 1);
870 else
871 {
872 line = NULL;
873 corruption_error("line length", file_name);
874 }
875 if (line == NULL)
876 {
877 *error = TRUE;
878 return uep;
879 }
880 for (j = 0; j < line_len; j++)
881 line[j] = getc(fp);
882 line[j] = NUL;
883 array[i] = line;
884 }
885 return uep;
886}
887
888/*
889 * Serialize "pos" to "fp".
890 */
891 static void
892serialize_pos(pos, fp)
893 pos_T pos;
894 FILE *fp;
895{
896 put_bytes(fp, (long_u)pos.lnum, 4);
897 put_bytes(fp, (long_u)pos.col, 4);
898#ifdef FEAT_VIRTUALEDIT
899 put_bytes(fp, (long_u)pos.coladd, 4);
900#else
901 put_bytes(fp, (long_u)0, 4);
902#endif
903}
904
905/*
906 * Unserialize the pos_T at the current position in fp.
907 */
908 static void
909unserialize_pos(pos, fp)
910 pos_T *pos;
911 FILE *fp;
912{
913 pos->lnum = get4c(fp);
914 pos->col = get4c(fp);
915#ifdef FEAT_VIRTUALEDIT
916 pos->coladd = get4c(fp);
917#else
918 (void)get4c(fp);
919#endif
920}
921
922/*
923 * Serialize "info" to "fp".
924 */
925 static void
926serialize_visualinfo(info, fp)
927 visualinfo_T *info;
928 FILE *fp;
929{
930 serialize_pos(info->vi_start, fp);
931 serialize_pos(info->vi_end, fp);
932 put_bytes(fp, (long_u)info->vi_mode, 4);
933 put_bytes(fp, (long_u)info->vi_curswant, 4);
934}
935
936/*
937 * Unserialize the visualinfo_T at the current position in fp.
938 */
939 static void
940unserialize_visualinfo(info, fp)
941 visualinfo_T *info;
942 FILE *fp;
943{
944 unserialize_pos(&info->vi_start, fp);
945 unserialize_pos(&info->vi_end, fp);
946 info->vi_mode = get4c(fp);
947 info->vi_curswant = get4c(fp);
948}
949
950/*
951 * Write the pointer to an undo header. Instead of writing the pointer itself
952 * we use the sequence number of the header. This is converted back to
953 * pointers when reading. */
954 static void
955put_header_ptr(fp, uhp)
956 FILE *fp;
957 u_header_T *uhp;
958{
959 put_bytes(fp, (long_u)(uhp != NULL ? uhp->uh_seq : 0), 4);
960}
961
962/*
963 * Write the undo tree in an undo file.
964 * When "name" is not NULL, use it as the name of the undo file.
965 * Otherwise use buf->b_ffname to generate the undo file name.
966 * "buf" must never be null, buf->b_ffname is used to obtain the original file
967 * permissions.
968 * "forceit" is TRUE for ":wundo!", FALSE otherwise.
969 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text.
970 */
971 void
972u_write_undo(name, forceit, buf, hash)
973 char_u *name;
974 int forceit;
975 buf_T *buf;
976 char_u *hash;
977{
978 u_header_T *uhp;
979 u_entry_T *uep;
980 char_u *file_name;
981 int str_len, i, mark;
982#ifdef U_DEBUG
983 int headers_written = 0;
984#endif
985 int fd;
986 FILE *fp = NULL;
987 int perm;
988 int write_ok = FALSE;
989#ifdef UNIX
990 int st_old_valid = FALSE;
991 struct stat st_old;
992 struct stat st_new;
993#endif
994
995 if (name == NULL)
996 {
997 file_name = u_get_undo_file_name(buf->b_ffname, FALSE);
998 if (file_name == NULL)
999 {
1000 if (p_verbose > 0)
1001 smsg((char_u *)_("Cannot write undo file in any directory in 'undodir'"));
1002 return;
1003 }
1004 }
1005 else
1006 file_name = name;
1007
1008 /*
1009 * Decide about the permission to use for the undo file. If the buffer
1010 * has a name use the permission of the original file. Otherwise only
1011 * allow the user to access the undo file.
1012 */
1013 perm = 0600;
1014 if (buf->b_ffname != NULL)
1015 {
1016#ifdef UNIX
1017 if (mch_stat((char *)buf->b_ffname, &st_old) >= 0)
1018 {
1019 perm = st_old.st_mode;
1020 st_old_valid = TRUE;
1021 }
1022#else
1023 perm = mch_getperm(buf->b_ffname);
1024 if (perm < 0)
1025 perm = 0600;
1026#endif
1027 }
1028
1029 /* strip any s-bit */
1030 perm = perm & 0777;
1031
1032 /* If the undo file already exists, verify that it actually is an undo
1033 * file, and delete it. */
1034 if (mch_getperm(file_name) >= 0)
1035 {
1036 if (name == NULL || !forceit)
1037 {
1038 /* Check we can read it and it's an undo file. */
1039 fd = mch_open((char *)file_name, O_RDONLY|O_EXTRA, 0);
1040 if (fd < 0)
1041 {
1042 if (name != NULL || p_verbose > 0)
1043 smsg((char_u *)_("Will not overwrite with undo file, cannot read: %s"),
1044 file_name);
1045 goto theend;
1046 }
1047 else
1048 {
1049 char_u buf[UF_START_MAGIC_LEN];
1050 int len;
1051
1052 len = vim_read(fd, buf, UF_START_MAGIC_LEN);
1053 close(fd);
1054 if (len < UF_START_MAGIC_LEN
1055 || memcmp(buf, UF_START_MAGIC, UF_START_MAGIC_LEN) != 0)
1056 {
1057 if (name != NULL || p_verbose > 0)
1058 smsg((char_u *)_("Will not overwrite, this is not an undo file: %s"),
1059 file_name);
1060 goto theend;
1061 }
1062 }
1063 }
1064 mch_remove(file_name);
1065 }
1066
1067 fd = mch_open((char *)file_name,
1068 O_CREAT|O_EXTRA|O_WRONLY|O_EXCL|O_NOFOLLOW, perm);
1069 if (fd < 0)
1070 {
1071 EMSG2(_(e_not_open), file_name);
1072 goto theend;
1073 }
1074 (void)mch_setperm(file_name, perm);
1075 if (p_verbose > 0)
1076 smsg((char_u *)_("Writing undo file: %s"), file_name);
1077
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001078#ifdef U_DEBUG
1079 /* Check if there already is a problem before writing. */
1080 u_check(FALSE);
1081#endif
1082
Bram Moolenaar9db58062010-05-29 20:33:07 +02001083#ifdef UNIX
1084 /*
1085 * Try to set the group of the undo file same as the original file. If
1086 * this fails, set the protection bits for the group same as the
1087 * protection bits for others.
1088 */
1089 if (st_old_valid && (mch_stat((char *)file_name, &st_new) >= 0
1090 && st_new.st_gid != st_old.st_gid
1091# ifdef HAVE_FCHOWN /* sequent-ptx lacks fchown() */
1092 && fchown(fd, (uid_t)-1, st_old.st_gid) != 0)
1093# endif
1094 )
1095 mch_setperm(file_name, (perm & 0707) | ((perm & 07) << 3));
1096# ifdef HAVE_SELINUX
1097 if (buf->b_ffname != NULL)
1098 mch_copy_sec(buf->b_ffname, file_name);
1099# endif
1100#endif
1101
1102 fp = fdopen(fd, "w");
1103 if (fp == NULL)
1104 {
1105 EMSG2(_(e_not_open), file_name);
1106 close(fd);
1107 mch_remove(file_name);
1108 goto theend;
1109 }
1110
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001111 /* Undo must be synced. */
1112 u_sync(TRUE);
1113
Bram Moolenaar9db58062010-05-29 20:33:07 +02001114 /* Start writing, first the undo file header. */
1115 if (fwrite(UF_START_MAGIC, (size_t)UF_START_MAGIC_LEN, (size_t)1, fp) != 1)
1116 goto write_error;
1117 put_bytes(fp, (long_u)UF_VERSION, 2);
1118
1119 /* Write a hash of the buffer text, so that we can verify it is still the
1120 * same when reading the buffer text. */
1121 if (fwrite(hash, (size_t)UNDO_HASH_SIZE, (size_t)1, fp) != 1)
1122 goto write_error;
1123 put_bytes(fp, (long_u)buf->b_ml.ml_line_count, 4);
1124
1125 /* Begin undo data for U */
1126 str_len = buf->b_u_line_ptr != NULL ? (int)STRLEN(buf->b_u_line_ptr) : 0;
1127 put_bytes(fp, (long_u)str_len, 4);
1128 if (str_len > 0 && fwrite(buf->b_u_line_ptr, (size_t)str_len,
1129 (size_t)1, fp) != 1)
1130 goto write_error;
1131
1132 put_bytes(fp, (long_u)buf->b_u_line_lnum, 4);
1133 put_bytes(fp, (long_u)buf->b_u_line_colnr, 4);
1134
1135 /* Begin general undo data */
1136 put_header_ptr(fp, buf->b_u_oldhead);
1137 put_header_ptr(fp, buf->b_u_newhead);
1138 put_header_ptr(fp, buf->b_u_curhead);
1139
1140 put_bytes(fp, (long_u)buf->b_u_numhead, 4);
1141 put_bytes(fp, (long_u)buf->b_u_seq_last, 4);
1142 put_bytes(fp, (long_u)buf->b_u_seq_cur, 4);
1143 put_time(fp, buf->b_u_seq_time);
1144
1145 /*
1146 * Iteratively serialize UHPs and their UEPs from the top down.
1147 */
1148 mark = ++lastmark;
1149 uhp = buf->b_u_oldhead;
1150 while (uhp != NULL)
1151 {
1152 /* Serialize current UHP if we haven't seen it */
1153 if (uhp->uh_walk != mark)
1154 {
1155 uhp->uh_walk = mark;
1156#ifdef U_DEBUG
1157 ++headers_written;
1158#endif
1159
1160 if (put_bytes(fp, (long_u)UF_HEADER_MAGIC, 2) == FAIL)
1161 goto write_error;
1162
1163 put_header_ptr(fp, uhp->uh_next);
1164 put_header_ptr(fp, uhp->uh_prev);
1165 put_header_ptr(fp, uhp->uh_alt_next);
1166 put_header_ptr(fp, uhp->uh_alt_prev);
1167 put_bytes(fp, uhp->uh_seq, 4);
1168 serialize_pos(uhp->uh_cursor, fp);
1169#ifdef FEAT_VIRTUALEDIT
1170 put_bytes(fp, (long_u)uhp->uh_cursor_vcol, 4);
1171#else
1172 put_bytes(fp, (long_u)0, 4);
1173#endif
1174 put_bytes(fp, (long_u)uhp->uh_flags, 2);
1175 /* Assume NMARKS will stay the same. */
1176 for (i = 0; i < NMARKS; ++i)
1177 serialize_pos(uhp->uh_namedm[i], fp);
1178#ifdef FEAT_VISUAL
1179 serialize_visualinfo(&uhp->uh_visual, fp);
1180#else
1181 {
1182 visualinfo_T info;
1183
1184 memset(&info, 0, sizeof(visualinfo_T));
1185 serialize_visualinfo(&info, fp);
1186 }
1187#endif
1188 put_time(fp, uhp->uh_time);
1189
1190 /* Write all the entries. */
1191 for (uep = uhp->uh_entry; uep != NULL; uep = uep->ue_next)
1192 {
1193 put_bytes(fp, (long_u)UF_ENTRY_MAGIC, 2);
1194 if (serialize_uep(uep, fp) == FAIL)
1195 goto write_error;
1196 }
1197 put_bytes(fp, (long_u)UF_ENTRY_END_MAGIC, 2);
1198 }
1199
1200 /* Now walk through the tree - algorithm from undo_time */
1201 if (uhp->uh_prev != NULL && uhp->uh_prev->uh_walk != mark)
1202 uhp = uhp->uh_prev;
1203 else if (uhp->uh_alt_next != NULL && uhp->uh_alt_next->uh_walk != mark)
1204 uhp = uhp->uh_alt_next;
1205 else if (uhp->uh_next != NULL && uhp->uh_alt_prev == NULL
1206 && uhp->uh_next->uh_walk != mark)
1207 uhp = uhp->uh_next;
1208 else if (uhp->uh_alt_prev != NULL)
1209 uhp = uhp->uh_alt_prev;
1210 else
1211 uhp = uhp->uh_next;
1212 }
1213
1214 if (put_bytes(fp, (long_u)UF_HEADER_END_MAGIC, 2) == OK)
1215 write_ok = TRUE;
1216#ifdef U_DEBUG
1217 if (headers_written != buf->b_u_numhead)
1218 EMSG3("Written %ld headers, but numhead is %ld",
1219 headers_written, buf->b_u_numhead);
1220#endif
1221
1222write_error:
1223 fclose(fp);
1224 if (!write_ok)
1225 EMSG2(_("E829: write error in undo file: %s"), file_name);
1226
1227#if defined(MACOS_CLASSIC) || defined(WIN3264)
1228 if (buf->b_ffname != NULL)
1229 (void)mch_copy_file_attribute(buf->b_ffname, file_name);
1230#endif
1231#ifdef HAVE_ACL
1232 if (buf->b_ffname != NULL)
1233 {
1234 vim_acl_T acl;
1235
1236 /* For systems that support ACL: get the ACL from the original file. */
1237 acl = mch_get_acl(buf->b_ffname);
1238 mch_set_acl(file_name, acl);
1239 }
1240#endif
1241
1242theend:
1243 if (file_name != name)
1244 vim_free(file_name);
1245}
1246
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001247/*
1248 * Load the undo tree from an undo file.
1249 * If "name" is not NULL use it as the undo file name. This also means being
1250 * a bit more verbose.
1251 * Otherwise use curbuf->b_ffname to generate the undo file name.
1252 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text.
1253 */
1254 void
1255u_read_undo(name, hash)
1256 char_u *name;
1257 char_u *hash;
1258{
1259 char_u *file_name;
1260 FILE *fp;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001261 long version, str_len;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001262 char_u *line_ptr = NULL;
1263 linenr_T line_lnum;
1264 colnr_T line_colnr;
1265 linenr_T line_count;
Bram Moolenaar442b4222010-05-24 21:34:22 +02001266 int num_head = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001267 long old_header_seq, new_header_seq, cur_header_seq;
1268 long seq_last, seq_cur;
1269 short old_idx = -1, new_idx = -1, cur_idx = -1;
1270 long num_read_uhps = 0;
1271 time_t seq_time;
1272 int i, j;
1273 int c;
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001274 u_entry_T *uep, *last_uep;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001275 u_header_T *uhp;
1276 u_header_T **uhp_table = NULL;
1277 char_u read_hash[UNDO_HASH_SIZE];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001278 char_u magic_buf[UF_START_MAGIC_LEN];
1279#ifdef U_DEBUG
1280 int *uhp_table_used;
1281#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001282
1283 if (name == NULL)
1284 {
1285 file_name = u_get_undo_file_name(curbuf->b_ffname, TRUE);
1286 if (file_name == NULL)
1287 return;
1288 }
1289 else
1290 file_name = name;
1291
1292 if (p_verbose > 0)
1293 smsg((char_u *)_("Reading undo file: %s"), file_name);
1294 fp = mch_fopen((char *)file_name, "r");
1295 if (fp == NULL)
1296 {
1297 if (name != NULL || p_verbose > 0)
1298 EMSG2(_("E822: Cannot open undo file for reading: %s"), file_name);
1299 goto error;
1300 }
1301
Bram Moolenaar9db58062010-05-29 20:33:07 +02001302 /*
1303 * Read the undo file header.
1304 */
1305 if (fread(magic_buf, UF_START_MAGIC_LEN, 1, fp) != 1
1306 || memcmp(magic_buf, UF_START_MAGIC, UF_START_MAGIC_LEN) != 0)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001307 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001308 EMSG2(_("E823: Not an undo file: %s"), file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001309 goto error;
1310 }
1311 version = get2c(fp);
1312 if (version != UF_VERSION)
1313 {
1314 EMSG2(_("E824: Incompatible undo file: %s"), file_name);
1315 goto error;
1316 }
1317
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001318 if (fread(read_hash, UNDO_HASH_SIZE, 1, fp) != 1)
1319 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001320 corruption_error("hash", file_name);
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001321 goto error;
1322 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001323 line_count = (linenr_T)get4c(fp);
1324 if (memcmp(hash, read_hash, UNDO_HASH_SIZE) != 0
1325 || line_count != curbuf->b_ml.ml_line_count)
1326 {
1327 if (p_verbose > 0 || name != NULL)
1328 {
1329 verbose_enter();
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001330 give_warning((char_u *)_("File contents changed, cannot use undo info"), TRUE);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001331 verbose_leave();
1332 }
1333 goto error;
1334 }
1335
1336 /* Begin undo data for U */
1337 str_len = get4c(fp);
1338 if (str_len < 0)
1339 goto error;
1340 else if (str_len > 0)
1341 {
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001342 if ((line_ptr = U_ALLOC_LINE(str_len + 1)) == NULL)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001343 goto error;
1344 for (i = 0; i < str_len; i++)
1345 line_ptr[i] = (char_u)getc(fp);
1346 line_ptr[i] = NUL;
1347 }
1348 line_lnum = (linenr_T)get4c(fp);
1349 line_colnr = (colnr_T)get4c(fp);
1350
1351 /* Begin general undo data */
1352 old_header_seq = get4c(fp);
1353 new_header_seq = get4c(fp);
1354 cur_header_seq = get4c(fp);
1355 num_head = get4c(fp);
1356 seq_last = get4c(fp);
1357 seq_cur = get4c(fp);
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001358 seq_time = get8ctime(fp);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001359
1360 /* uhp_table will store the freshly created undo headers we allocate
1361 * until we insert them into curbuf. The table remains sorted by the
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001362 * sequence numbers of the headers.
1363 * When there are no headers uhp_table is NULL. */
1364 if (num_head > 0)
1365 {
1366 uhp_table = (u_header_T **)U_ALLOC_LINE(
1367 num_head * sizeof(u_header_T *));
1368 if (uhp_table == NULL)
1369 goto error;
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001370 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001371
Bram Moolenaar9db58062010-05-29 20:33:07 +02001372 while ((c = get2c(fp)) == UF_HEADER_MAGIC)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001373 {
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001374 if (num_read_uhps >= num_head)
1375 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001376 corruption_error("num_head", file_name);
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001377 goto error;
1378 }
1379
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001380 uhp = (u_header_T *)U_ALLOC_LINE(sizeof(u_header_T));
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001381 if (uhp == NULL)
1382 goto error;
1383 vim_memset(uhp, 0, sizeof(u_header_T));
Bram Moolenaar9db58062010-05-29 20:33:07 +02001384#ifdef U_DEBUG
1385 uhp->uh_magic = UH_MAGIC;
1386#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001387 /* We're not actually trying to store pointers here. We're just storing
1388 * IDs so we can swizzle them into pointers later - hence the type
1389 * cast. */
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001390 uhp->uh_next = (u_header_T *)(long_u)get4c(fp);
1391 uhp->uh_prev = (u_header_T *)(long_u)get4c(fp);
1392 uhp->uh_alt_next = (u_header_T *)(long_u)get4c(fp);
1393 uhp->uh_alt_prev = (u_header_T *)(long_u)get4c(fp);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001394 uhp->uh_seq = get4c(fp);
1395 if (uhp->uh_seq <= 0)
1396 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001397 corruption_error("uh_seq", file_name);
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001398 vim_free(uhp);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001399 goto error;
1400 }
1401 uhp->uh_walk = 0;
1402 unserialize_pos(&uhp->uh_cursor, fp);
1403#ifdef FEAT_VIRTUALEDIT
1404 uhp->uh_cursor_vcol = get4c(fp);
1405#else
1406 (void)get4c(fp);
1407#endif
1408 uhp->uh_flags = get2c(fp);
1409 for (i = 0; i < NMARKS; ++i)
1410 unserialize_pos(&uhp->uh_namedm[i], fp);
1411#ifdef FEAT_VISUAL
1412 unserialize_visualinfo(&uhp->uh_visual, fp);
1413#else
1414 {
1415 visualinfo_T info;
1416 unserialize_visualinfo(&info, fp);
1417 }
1418#endif
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001419 uhp->uh_time = get8ctime(fp);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001420
Bram Moolenaar9db58062010-05-29 20:33:07 +02001421 /* Unserialize the uep list. */
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001422 last_uep = NULL;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001423 while ((c = get2c(fp)) == UF_ENTRY_MAGIC)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001424 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001425 int error = FALSE;
1426
1427 uep = unserialize_uep(fp, &error, file_name);
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001428 if (last_uep == NULL)
1429 uhp->uh_entry = uep;
1430 else
1431 last_uep->ue_next = uep;
1432 last_uep = uep;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001433 if (uep == NULL || error)
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001434 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001435 u_free_uhp(uhp);
1436 goto error;
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001437 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001438 }
1439 if (c != UF_ENTRY_END_MAGIC)
1440 {
1441 corruption_error("entry end", file_name);
1442 u_free_uhp(uhp);
1443 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001444 }
1445
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001446 uhp_table[num_read_uhps++] = uhp;
1447 }
1448
1449 if (num_read_uhps != num_head)
1450 {
1451 corruption_error("num_head", file_name);
1452 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001453 }
1454
Bram Moolenaar9db58062010-05-29 20:33:07 +02001455 if (c != UF_HEADER_END_MAGIC)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001456 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001457 corruption_error("end marker", file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001458 goto error;
1459 }
1460
Bram Moolenaar9db58062010-05-29 20:33:07 +02001461#ifdef U_DEBUG
1462 uhp_table_used = (int *)alloc_clear(
1463 (unsigned)(sizeof(int) * num_head + 1));
1464# define SET_FLAG(j) ++uhp_table_used[j]
1465#else
1466# define SET_FLAG(j)
1467#endif
1468
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001469 /* We have put all of the uhps into a table. Now we iterate through the
1470 * table and swizzle each sequence number we've stored in uh_* into a
1471 * pointer corresponding to the header with that sequence number. */
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001472 for (i = 0; i < num_head; i++)
1473 {
1474 uhp = uhp_table[i];
1475 if (uhp == NULL)
1476 continue;
1477 for (j = 0; j < num_head; j++)
1478 {
1479 if (uhp_table[j] == NULL)
1480 continue;
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001481 if (i != j && uhp_table[i]->uh_seq == uhp_table[j]->uh_seq)
1482 {
1483 corruption_error("duplicate uh_seq", file_name);
1484 goto error;
1485 }
1486
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001487 if (uhp_table[j]->uh_seq == (long)uhp->uh_next)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001488 {
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001489 uhp->uh_next = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001490 SET_FLAG(j);
1491 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001492 if (uhp_table[j]->uh_seq == (long)uhp->uh_prev)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001493 {
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001494 uhp->uh_prev = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001495 SET_FLAG(j);
1496 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001497 if (uhp_table[j]->uh_seq == (long)uhp->uh_alt_next)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001498 {
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001499 uhp->uh_alt_next = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001500 SET_FLAG(j);
1501 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001502 if (uhp_table[j]->uh_seq == (long)uhp->uh_alt_prev)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001503 {
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001504 uhp->uh_alt_prev = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001505 SET_FLAG(j);
1506 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001507 }
1508 if (old_header_seq > 0 && old_idx < 0 && uhp->uh_seq == old_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001509 {
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001510 old_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001511 SET_FLAG(i);
1512 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001513 if (new_header_seq > 0 && new_idx < 0 && uhp->uh_seq == new_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001514 {
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001515 new_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001516 SET_FLAG(i);
1517 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001518 if (cur_header_seq > 0 && cur_idx < 0 && uhp->uh_seq == cur_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001519 {
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001520 cur_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001521 SET_FLAG(i);
1522 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001523 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001524
1525 /* Now that we have read the undo info successfully, free the current undo
1526 * info and use the info from the file. */
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001527 u_blockfree(curbuf);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001528 curbuf->b_u_oldhead = old_idx < 0 ? NULL : uhp_table[old_idx];
1529 curbuf->b_u_newhead = new_idx < 0 ? NULL : uhp_table[new_idx];
1530 curbuf->b_u_curhead = cur_idx < 0 ? NULL : uhp_table[cur_idx];
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001531#ifdef U_DEBUG
1532 if (curbuf->b_u_curhead != NULL)
1533 corruption_error("curhead not NULL", file_name);
1534#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001535 curbuf->b_u_line_ptr = line_ptr;
1536 curbuf->b_u_line_lnum = line_lnum;
1537 curbuf->b_u_line_colnr = line_colnr;
1538 curbuf->b_u_numhead = num_head;
1539 curbuf->b_u_seq_last = seq_last;
1540 curbuf->b_u_seq_cur = seq_cur;
1541 curbuf->b_u_seq_time = seq_time;
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001542
1543 curbuf->b_u_synced = TRUE;
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001544 vim_free(uhp_table);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001545
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001546#ifdef U_DEBUG
Bram Moolenaar9db58062010-05-29 20:33:07 +02001547 for (i = 0; i < num_head; ++i)
1548 if (uhp_table_used[i] == 0)
1549 EMSGN("uhp_table entry %ld not used, leaking memory", i);
1550 vim_free(uhp_table_used);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001551 u_check(TRUE);
1552#endif
Bram Moolenaar9db58062010-05-29 20:33:07 +02001553
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001554 if (name != NULL)
1555 smsg((char_u *)_("Finished reading undo file %s"), file_name);
1556 goto theend;
1557
1558error:
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001559 vim_free(line_ptr);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001560 if (uhp_table != NULL)
1561 {
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001562 for (i = 0; i < num_read_uhps; i++)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001563 if (uhp_table[i] != NULL)
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001564 u_free_uhp(uhp_table[i]);
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001565 vim_free(uhp_table);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001566 }
1567
1568theend:
1569 if (fp != NULL)
1570 fclose(fp);
1571 if (file_name != name)
1572 vim_free(file_name);
1573 return;
1574}
1575
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001576#endif /* FEAT_PERSISTENT_UNDO */
1577
1578
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579/*
1580 * If 'cpoptions' contains 'u': Undo the previous undo or redo (vi compatible).
1581 * If 'cpoptions' does not contain 'u': Always undo.
1582 */
1583 void
1584u_undo(count)
1585 int count;
1586{
1587 /*
1588 * If we get an undo command while executing a macro, we behave like the
1589 * original vi. If this happens twice in one macro the result will not
1590 * be compatible.
1591 */
1592 if (curbuf->b_u_synced == FALSE)
1593 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001594 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 count = 1;
1596 }
1597
1598 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
1599 undo_undoes = TRUE;
1600 else
1601 undo_undoes = !undo_undoes;
1602 u_doit(count);
1603}
1604
1605/*
1606 * If 'cpoptions' contains 'u': Repeat the previous undo or redo.
1607 * If 'cpoptions' does not contain 'u': Always redo.
1608 */
1609 void
1610u_redo(count)
1611 int count;
1612{
1613 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
1614 undo_undoes = FALSE;
1615 u_doit(count);
1616}
1617
1618/*
1619 * Undo or redo, depending on 'undo_undoes', 'count' times.
1620 */
1621 static void
Bram Moolenaarca003e12006-03-17 23:19:38 +00001622u_doit(startcount)
1623 int startcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624{
Bram Moolenaarca003e12006-03-17 23:19:38 +00001625 int count = startcount;
1626
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001627 if (!undo_allowed())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629
1630 u_newcount = 0;
1631 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001632 if (curbuf->b_ml.ml_flags & ML_EMPTY)
1633 u_oldcount = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 while (count--)
1635 {
1636 if (undo_undoes)
1637 {
1638 if (curbuf->b_u_curhead == NULL) /* first undo */
1639 curbuf->b_u_curhead = curbuf->b_u_newhead;
1640 else if (p_ul > 0) /* multi level undo */
1641 /* get next undo */
1642 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_next;
1643 /* nothing to undo */
1644 if (curbuf->b_u_numhead == 0 || curbuf->b_u_curhead == NULL)
1645 {
1646 /* stick curbuf->b_u_curhead at end */
1647 curbuf->b_u_curhead = curbuf->b_u_oldhead;
1648 beep_flush();
Bram Moolenaarca003e12006-03-17 23:19:38 +00001649 if (count == startcount - 1)
1650 {
1651 MSG(_("Already at oldest change"));
1652 return;
1653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 break;
1655 }
1656
Bram Moolenaarca003e12006-03-17 23:19:38 +00001657 u_undoredo(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 }
1659 else
1660 {
1661 if (curbuf->b_u_curhead == NULL || p_ul <= 0)
1662 {
1663 beep_flush(); /* nothing to redo */
Bram Moolenaarca003e12006-03-17 23:19:38 +00001664 if (count == startcount - 1)
1665 {
1666 MSG(_("Already at newest change"));
1667 return;
1668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 break;
1670 }
1671
Bram Moolenaarca003e12006-03-17 23:19:38 +00001672 u_undoredo(FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00001673
1674 /* Advance for next redo. Set "newhead" when at the end of the
1675 * redoable changes. */
1676 if (curbuf->b_u_curhead->uh_prev == NULL)
1677 curbuf->b_u_newhead = curbuf->b_u_curhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_prev;
1679 }
1680 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001681 u_undo_end(undo_undoes, FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00001682}
1683
Bram Moolenaar1e607892006-03-13 22:15:53 +00001684/*
1685 * Undo or redo over the timeline.
1686 * When "step" is negative go back in time, otherwise goes forward in time.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001687 * When "sec" is FALSE make "step" steps, when "sec" is TRUE use "step" as
1688 * seconds.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001689 * When "absolute" is TRUE use "step" as the sequence number to jump to.
1690 * "sec" must be FALSE then.
Bram Moolenaar1e607892006-03-13 22:15:53 +00001691 */
1692 void
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001693undo_time(step, sec, absolute)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001694 long step;
1695 int sec;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001696 int absolute;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001697{
1698 long target;
1699 long closest;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001700 long closest_start;
1701 long closest_seq = 0;
1702 long val;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001703 u_header_T *uhp;
1704 u_header_T *last;
1705 int mark;
1706 int nomark;
1707 int round;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001708 int dosec = sec;
1709 int above = FALSE;
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001710 int did_undo = TRUE;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001711
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001712 /* First make sure the current undoable change is synced. */
1713 if (curbuf->b_u_synced == FALSE)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001714 u_sync(TRUE);
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001715
Bram Moolenaar1e607892006-03-13 22:15:53 +00001716 u_newcount = 0;
1717 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001718 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar1e607892006-03-13 22:15:53 +00001719 u_oldcount = -1;
1720
Bram Moolenaarca003e12006-03-17 23:19:38 +00001721 /* "target" is the node below which we want to be.
1722 * Init "closest" to a value we can't reach. */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001723 if (absolute)
1724 {
1725 target = step;
Bram Moolenaarca003e12006-03-17 23:19:38 +00001726 closest = -1;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001727 }
Bram Moolenaarca003e12006-03-17 23:19:38 +00001728 else
Bram Moolenaar1e607892006-03-13 22:15:53 +00001729 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00001730 /* When doing computations with time_t subtract starttime, because
1731 * time_t converted to a long may result in a wrong number. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001732 if (sec)
Bram Moolenaarca003e12006-03-17 23:19:38 +00001733 target = (long)(curbuf->b_u_seq_time - starttime) + step;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001734 else
1735 target = curbuf->b_u_seq_cur + step;
Bram Moolenaarca003e12006-03-17 23:19:38 +00001736 if (step < 0)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001737 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00001738 if (target < 0)
1739 target = 0;
1740 closest = -1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001741 }
1742 else
1743 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00001744 if (sec)
Bram Moolenaardb552d602006-03-23 22:59:57 +00001745 closest = (long)(time(NULL) - starttime + 1);
Bram Moolenaarca003e12006-03-17 23:19:38 +00001746 else
1747 closest = curbuf->b_u_seq_last + 2;
1748 if (target >= closest)
1749 target = closest - 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001750 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00001751 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001752 closest_start = closest;
Bram Moolenaarca003e12006-03-17 23:19:38 +00001753 closest_seq = curbuf->b_u_seq_cur;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001754
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001755 /*
1756 * May do this twice:
Bram Moolenaar1e607892006-03-13 22:15:53 +00001757 * 1. Search for "target", update "closest" to the best match found.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001758 * 2. If "target" not found search for "closest".
1759 *
1760 * When using the closest time we use the sequence number in the second
1761 * round, because there may be several entries with the same time.
1762 */
Bram Moolenaar1e607892006-03-13 22:15:53 +00001763 for (round = 1; round <= 2; ++round)
1764 {
1765 /* Find the path from the current state to where we want to go. The
1766 * desired state can be anywhere in the undo tree, need to go all over
1767 * it. We put "nomark" in uh_walk where we have been without success,
1768 * "mark" where it could possibly be. */
1769 mark = ++lastmark;
1770 nomark = ++lastmark;
1771
1772 if (curbuf->b_u_curhead == NULL) /* at leaf of the tree */
1773 uhp = curbuf->b_u_newhead;
1774 else
1775 uhp = curbuf->b_u_curhead;
1776
1777 while (uhp != NULL)
1778 {
1779 uhp->uh_walk = mark;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001780 val = (long)(dosec ? (uhp->uh_time - starttime) : uhp->uh_seq);
Bram Moolenaar1e607892006-03-13 22:15:53 +00001781
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001782 if (round == 1)
1783 {
1784 /* Remember the header that is closest to the target.
1785 * It must be at least in the right direction (checked with
Bram Moolenaarca003e12006-03-17 23:19:38 +00001786 * "b_u_seq_cur"). When the timestamp is equal find the
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001787 * highest/lowest sequence number. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00001788 if ((step < 0 ? uhp->uh_seq <= curbuf->b_u_seq_cur
1789 : uhp->uh_seq > curbuf->b_u_seq_cur)
1790 && ((dosec && val == closest)
1791 ? (step < 0
1792 ? uhp->uh_seq < closest_seq
1793 : uhp->uh_seq > closest_seq)
1794 : closest == closest_start
1795 || (val > target
1796 ? (closest > target
1797 ? val - target <= closest - target
1798 : val - target <= target - closest)
1799 : (closest > target
1800 ? target - val <= closest - target
1801 : target - val <= target - closest))))
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001802 {
1803 closest = val;
1804 closest_seq = uhp->uh_seq;
1805 }
1806 }
1807
1808 /* Quit searching when we found a match. But when searching for a
1809 * time we need to continue looking for the best uh_seq. */
1810 if (target == val && !dosec)
1811 break;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001812
1813 /* go down in the tree if we haven't been there */
1814 if (uhp->uh_prev != NULL && uhp->uh_prev->uh_walk != nomark
1815 && uhp->uh_prev->uh_walk != mark)
1816 uhp = uhp->uh_prev;
1817
1818 /* go to alternate branch if we haven't been there */
1819 else if (uhp->uh_alt_next != NULL
1820 && uhp->uh_alt_next->uh_walk != nomark
1821 && uhp->uh_alt_next->uh_walk != mark)
1822 uhp = uhp->uh_alt_next;
1823
1824 /* go up in the tree if we haven't been there and we are at the
1825 * start of alternate branches */
1826 else if (uhp->uh_next != NULL && uhp->uh_alt_prev == NULL
1827 && uhp->uh_next->uh_walk != nomark
1828 && uhp->uh_next->uh_walk != mark)
Bram Moolenaardb552d602006-03-23 22:59:57 +00001829 {
1830 /* If still at the start we don't go through this change. */
1831 if (uhp == curbuf->b_u_curhead)
1832 uhp->uh_walk = nomark;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001833 uhp = uhp->uh_next;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001834 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00001835
1836 else
1837 {
1838 /* need to backtrack; mark this node as useless */
1839 uhp->uh_walk = nomark;
1840 if (uhp->uh_alt_prev != NULL)
1841 uhp = uhp->uh_alt_prev;
1842 else
1843 uhp = uhp->uh_next;
1844 }
1845 }
1846
1847 if (uhp != NULL) /* found it */
1848 break;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001849
1850 if (absolute)
1851 {
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001852 EMSGN(_("E830: Undo number %ld not found"), step);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001853 return;
1854 }
1855
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001856 if (closest == closest_start)
Bram Moolenaar1e607892006-03-13 22:15:53 +00001857 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001858 if (step < 0)
1859 MSG(_("Already at oldest change"));
1860 else
1861 MSG(_("Already at newest change"));
Bram Moolenaar1e607892006-03-13 22:15:53 +00001862 return;
1863 }
1864
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001865 target = closest_seq;
1866 dosec = FALSE;
1867 if (step < 0)
1868 above = TRUE; /* stop above the header */
Bram Moolenaar1e607892006-03-13 22:15:53 +00001869 }
1870
1871 /* If we found it: Follow the path to go to where we want to be. */
1872 if (uhp != NULL)
1873 {
1874 /*
1875 * First go up the tree as much as needed.
1876 */
1877 for (;;)
1878 {
1879 uhp = curbuf->b_u_curhead;
1880 if (uhp == NULL)
1881 uhp = curbuf->b_u_newhead;
1882 else
Bram Moolenaar1e607892006-03-13 22:15:53 +00001883 uhp = uhp->uh_next;
Bram Moolenaarca003e12006-03-17 23:19:38 +00001884 if (uhp == NULL || uhp->uh_walk != mark
1885 || (uhp->uh_seq == target && !above))
Bram Moolenaar1e607892006-03-13 22:15:53 +00001886 break;
1887 curbuf->b_u_curhead = uhp;
Bram Moolenaarca003e12006-03-17 23:19:38 +00001888 u_undoredo(TRUE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00001889 uhp->uh_walk = nomark; /* don't go back down here */
Bram Moolenaar1e607892006-03-13 22:15:53 +00001890 }
1891
1892 /*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001893 * And now go down the tree (redo), branching off where needed.
Bram Moolenaar1e607892006-03-13 22:15:53 +00001894 */
1895 uhp = curbuf->b_u_curhead;
Bram Moolenaarca003e12006-03-17 23:19:38 +00001896 while (uhp != NULL)
Bram Moolenaar1e607892006-03-13 22:15:53 +00001897 {
Bram Moolenaar89ed3df2007-01-09 19:23:12 +00001898 /* Go back to the first branch with a mark. */
1899 while (uhp->uh_alt_prev != NULL
1900 && uhp->uh_alt_prev->uh_walk == mark)
1901 uhp = uhp->uh_alt_prev;
1902
Bram Moolenaar1e607892006-03-13 22:15:53 +00001903 /* Find the last branch with a mark, that's the one. */
1904 last = uhp;
1905 while (last->uh_alt_next != NULL
1906 && last->uh_alt_next->uh_walk == mark)
1907 last = last->uh_alt_next;
1908 if (last != uhp)
1909 {
1910 /* Make the used branch the first entry in the list of
1911 * alternatives to make "u" and CTRL-R take this branch. */
Bram Moolenaar89ed3df2007-01-09 19:23:12 +00001912 while (uhp->uh_alt_prev != NULL)
1913 uhp = uhp->uh_alt_prev;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001914 if (last->uh_alt_next != NULL)
1915 last->uh_alt_next->uh_alt_prev = last->uh_alt_prev;
1916 last->uh_alt_prev->uh_alt_next = last->uh_alt_next;
1917 last->uh_alt_prev = NULL;
1918 last->uh_alt_next = uhp;
1919 uhp->uh_alt_prev = last;
1920
1921 uhp = last;
Bram Moolenaarca003e12006-03-17 23:19:38 +00001922 if (uhp->uh_next != NULL)
1923 uhp->uh_next->uh_prev = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001924 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001925 curbuf->b_u_curhead = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001926
1927 if (uhp->uh_walk != mark)
1928 break; /* must have reached the target */
1929
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001930 /* Stop when going backwards in time and didn't find the exact
1931 * header we were looking for. */
1932 if (uhp->uh_seq == target && above)
Bram Moolenaardb552d602006-03-23 22:59:57 +00001933 {
1934 curbuf->b_u_seq_cur = target - 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001935 break;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001936 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001937
Bram Moolenaarca003e12006-03-17 23:19:38 +00001938 u_undoredo(FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00001939
1940 /* Advance "curhead" to below the header we last used. If it
1941 * becomes NULL then we need to set "newhead" to this leaf. */
1942 if (uhp->uh_prev == NULL)
1943 curbuf->b_u_newhead = uhp;
1944 curbuf->b_u_curhead = uhp->uh_prev;
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001945 did_undo = FALSE;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001946
1947 if (uhp->uh_seq == target) /* found it! */
1948 break;
1949
1950 uhp = uhp->uh_prev;
1951 if (uhp == NULL || uhp->uh_walk != mark)
1952 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00001953 /* Need to redo more but can't find it... */
Bram Moolenaar1e607892006-03-13 22:15:53 +00001954 EMSG2(_(e_intern2), "undo_time()");
1955 break;
1956 }
1957 }
1958 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001959 u_undo_end(did_undo, absolute);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960}
1961
1962/*
1963 * u_undoredo: common code for undo and redo
1964 *
1965 * The lines in the file are replaced by the lines in the entry list at
1966 * curbuf->b_u_curhead. The replaced lines in the file are saved in the entry
1967 * list for the next undo/redo.
Bram Moolenaarca003e12006-03-17 23:19:38 +00001968 *
1969 * When "undo" is TRUE we go up in the tree, when FALSE we go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 */
1971 static void
Bram Moolenaarca003e12006-03-17 23:19:38 +00001972u_undoredo(undo)
1973 int undo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974{
1975 char_u **newarray = NULL;
1976 linenr_T oldsize;
1977 linenr_T newsize;
1978 linenr_T top, bot;
1979 linenr_T lnum;
1980 linenr_T newlnum = MAXLNUM;
1981 long i;
1982 u_entry_T *uep, *nuep;
1983 u_entry_T *newlist = NULL;
1984 int old_flags;
1985 int new_flags;
1986 pos_T namedm[NMARKS];
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001987#ifdef FEAT_VISUAL
1988 visualinfo_T visualinfo;
1989#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 int empty_buffer; /* buffer became empty */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001991 u_header_T *curhead = curbuf->b_u_curhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992
Bram Moolenaarfecb6602007-10-01 20:54:15 +00001993#ifdef U_DEBUG
1994 u_check(FALSE);
1995#endif
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001996 old_flags = curhead->uh_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 new_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
1998 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
1999 setpcmark();
2000
2001 /*
2002 * save marks before undo/redo
2003 */
2004 mch_memmove(namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002005#ifdef FEAT_VISUAL
2006 visualinfo = curbuf->b_visual;
2007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 curbuf->b_op_start.lnum = curbuf->b_ml.ml_line_count;
2009 curbuf->b_op_start.col = 0;
2010 curbuf->b_op_end.lnum = 0;
2011 curbuf->b_op_end.col = 0;
2012
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002013 for (uep = curhead->uh_entry; uep != NULL; uep = nuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 {
2015 top = uep->ue_top;
2016 bot = uep->ue_bot;
2017 if (bot == 0)
2018 bot = curbuf->b_ml.ml_line_count + 1;
2019 if (top > curbuf->b_ml.ml_line_count || top >= bot
2020 || bot > curbuf->b_ml.ml_line_count + 1)
2021 {
2022 EMSG(_("E438: u_undo: line numbers wrong"));
2023 changed(); /* don't want UNCHANGED now */
2024 return;
2025 }
2026
2027 oldsize = bot - top - 1; /* number of lines before undo */
2028 newsize = uep->ue_size; /* number of lines after undo */
2029
2030 if (top < newlnum)
2031 {
2032 /* If the saved cursor is somewhere in this undo block, move it to
2033 * the remembered position. Makes "gwap" put the cursor back
2034 * where it was. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002035 lnum = curhead->uh_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 if (lnum >= top && lnum <= top + newsize + 1)
2037 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002038 curwin->w_cursor = curhead->uh_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 newlnum = curwin->w_cursor.lnum - 1;
2040 }
2041 else
2042 {
2043 /* Use the first line that actually changed. Avoids that
2044 * undoing auto-formatting puts the cursor in the previous
2045 * line. */
2046 for (i = 0; i < newsize && i < oldsize; ++i)
2047 if (STRCMP(uep->ue_array[i], ml_get(top + 1 + i)) != 0)
2048 break;
2049 if (i == newsize && newlnum == MAXLNUM && uep->ue_next == NULL)
2050 {
2051 newlnum = top;
2052 curwin->w_cursor.lnum = newlnum + 1;
2053 }
2054 else if (i < newsize)
2055 {
2056 newlnum = top + i;
2057 curwin->w_cursor.lnum = newlnum + 1;
2058 }
2059 }
2060 }
2061
2062 empty_buffer = FALSE;
2063
2064 /* delete the lines between top and bot and save them in newarray */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002065 if (oldsize > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002067 if ((newarray = (char_u **)U_ALLOC_LINE(
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002068 sizeof(char_u *) * oldsize)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 {
2070 do_outofmem_msg((long_u)(sizeof(char_u *) * oldsize));
2071 /*
2072 * We have messed up the entry list, repair is impossible.
2073 * we have to free the rest of the list.
2074 */
2075 while (uep != NULL)
2076 {
2077 nuep = uep->ue_next;
2078 u_freeentry(uep, uep->ue_size);
2079 uep = nuep;
2080 }
2081 break;
2082 }
2083 /* delete backwards, it goes faster in most cases */
2084 for (lnum = bot - 1, i = oldsize; --i >= 0; --lnum)
2085 {
2086 /* what can we do when we run out of memory? */
2087 if ((newarray[i] = u_save_line(lnum)) == NULL)
2088 do_outofmem_msg((long_u)0);
2089 /* remember we deleted the last line in the buffer, and a
2090 * dummy empty line will be inserted */
2091 if (curbuf->b_ml.ml_line_count == 1)
2092 empty_buffer = TRUE;
2093 ml_delete(lnum, FALSE);
2094 }
2095 }
Bram Moolenaar8d343302005-07-12 22:46:17 +00002096 else
2097 newarray = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098
2099 /* insert the lines in u_array between top and bot */
2100 if (newsize)
2101 {
2102 for (lnum = top, i = 0; i < newsize; ++i, ++lnum)
2103 {
2104 /*
2105 * If the file is empty, there is an empty line 1 that we
2106 * should get rid of, by replacing it with the new line
2107 */
2108 if (empty_buffer && lnum == 0)
2109 ml_replace((linenr_T)1, uep->ue_array[i], TRUE);
2110 else
2111 ml_append(lnum, uep->ue_array[i], (colnr_T)0, FALSE);
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002112 vim_free(uep->ue_array[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 }
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002114 vim_free((char_u *)uep->ue_array);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 }
2116
2117 /* adjust marks */
2118 if (oldsize != newsize)
2119 {
2120 mark_adjust(top + 1, top + oldsize, (long)MAXLNUM,
2121 (long)newsize - (long)oldsize);
2122 if (curbuf->b_op_start.lnum > top + oldsize)
2123 curbuf->b_op_start.lnum += newsize - oldsize;
2124 if (curbuf->b_op_end.lnum > top + oldsize)
2125 curbuf->b_op_end.lnum += newsize - oldsize;
2126 }
2127
2128 changed_lines(top + 1, 0, bot, newsize - oldsize);
2129
2130 /* set '[ and '] mark */
2131 if (top + 1 < curbuf->b_op_start.lnum)
2132 curbuf->b_op_start.lnum = top + 1;
2133 if (newsize == 0 && top + 1 > curbuf->b_op_end.lnum)
2134 curbuf->b_op_end.lnum = top + 1;
2135 else if (top + newsize > curbuf->b_op_end.lnum)
2136 curbuf->b_op_end.lnum = top + newsize;
2137
2138 u_newcount += newsize;
2139 u_oldcount += oldsize;
2140 uep->ue_size = oldsize;
2141 uep->ue_array = newarray;
2142 uep->ue_bot = top + newsize + 1;
2143
2144 /*
2145 * insert this entry in front of the new entry list
2146 */
2147 nuep = uep->ue_next;
2148 uep->ue_next = newlist;
2149 newlist = uep;
2150 }
2151
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002152 curhead->uh_entry = newlist;
2153 curhead->uh_flags = new_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 if ((old_flags & UH_EMPTYBUF) && bufempty())
2155 curbuf->b_ml.ml_flags |= ML_EMPTY;
2156 if (old_flags & UH_CHANGED)
2157 changed();
2158 else
Bram Moolenaar009b2592004-10-24 19:18:58 +00002159#ifdef FEAT_NETBEANS_INTG
2160 /* per netbeans undo rules, keep it as modified */
2161 if (!isNetbeansModified(curbuf))
2162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 unchanged(curbuf, FALSE);
2164
2165 /*
2166 * restore marks from before undo/redo
2167 */
2168 for (i = 0; i < NMARKS; ++i)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002169 if (curhead->uh_namedm[i].lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002171 curbuf->b_namedm[i] = curhead->uh_namedm[i];
2172 curhead->uh_namedm[i] = namedm[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002174#ifdef FEAT_VISUAL
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002175 if (curhead->uh_visual.vi_start.lnum != 0)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002176 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002177 curbuf->b_visual = curhead->uh_visual;
2178 curhead->uh_visual = visualinfo;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002179 }
2180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181
2182 /*
2183 * If the cursor is only off by one line, put it at the same position as
2184 * before starting the change (for the "o" command).
2185 * Otherwise the cursor should go to the first undone line.
2186 */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002187 if (curhead->uh_cursor.lnum + 1 == curwin->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 && curwin->w_cursor.lnum > 1)
2189 --curwin->w_cursor.lnum;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002190 if (curhead->uh_cursor.lnum == curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002192 curwin->w_cursor.col = curhead->uh_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002194 if (virtual_active() && curhead->uh_cursor_vcol >= 0)
2195 coladvance((colnr_T)curhead->uh_cursor_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 else
2197 curwin->w_cursor.coladd = 0;
2198#endif
2199 }
2200 else if (curwin->w_cursor.lnum <= curbuf->b_ml.ml_line_count)
2201 beginline(BL_SOL | BL_FIX);
2202 else
2203 {
2204 /* We get here with the current cursor line being past the end (eg
2205 * after adding lines at the end of the file, and then undoing it).
2206 * check_cursor() will move the cursor to the last line. Move it to
2207 * the first column here. */
2208 curwin->w_cursor.col = 0;
2209#ifdef FEAT_VIRTUALEDIT
2210 curwin->w_cursor.coladd = 0;
2211#endif
2212 }
2213
2214 /* Make sure the cursor is on an existing line and column. */
2215 check_cursor();
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002216
2217 /* Remember where we are for "g-" and ":earlier 10s". */
2218 curbuf->b_u_seq_cur = curhead->uh_seq;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002219 if (undo)
2220 /* We are below the previous undo. However, to make ":earlier 1s"
2221 * work we compute this as being just above the just undone change. */
2222 --curbuf->b_u_seq_cur;
2223
2224 /* The timestamp can be the same for multiple changes, just use the one of
2225 * the undone/redone change. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002226 curbuf->b_u_seq_time = curhead->uh_time;
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002227#ifdef U_DEBUG
2228 u_check(FALSE);
2229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230}
2231
2232/*
2233 * If we deleted or added lines, report the number of less/more lines.
2234 * Otherwise, report the number of changes (this may be incorrect
2235 * in some cases, but it's better than nothing).
2236 */
2237 static void
Bram Moolenaardb552d602006-03-23 22:59:57 +00002238u_undo_end(did_undo, absolute)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002239 int did_undo; /* just did an undo */
Bram Moolenaardb552d602006-03-23 22:59:57 +00002240 int absolute; /* used ":undo N" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241{
Bram Moolenaar89d40322006-08-29 15:30:07 +00002242 char *msgstr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002243 u_header_T *uhp;
2244 char_u msgbuf[80];
Bram Moolenaar1e607892006-03-13 22:15:53 +00002245
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246#ifdef FEAT_FOLDING
2247 if ((fdo_flags & FDO_UNDO) && KeyTyped)
2248 foldOpenCursor();
2249#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +00002250
2251 if (global_busy /* no messages now, wait until global is finished */
2252 || !messaging()) /* 'lazyredraw' set, don't do messages now */
2253 return;
2254
2255 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2256 --u_newcount;
2257
2258 u_oldcount -= u_newcount;
2259 if (u_oldcount == -1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002260 msgstr = N_("more line");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002261 else if (u_oldcount < 0)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002262 msgstr = N_("more lines");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002263 else if (u_oldcount == 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002264 msgstr = N_("line less");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002265 else if (u_oldcount > 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002266 msgstr = N_("fewer lines");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002267 else
2268 {
2269 u_oldcount = u_newcount;
2270 if (u_newcount == 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002271 msgstr = N_("change");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002272 else
Bram Moolenaar89d40322006-08-29 15:30:07 +00002273 msgstr = N_("changes");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002274 }
2275
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002276 if (curbuf->b_u_curhead != NULL)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002277 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00002278 /* For ":undo N" we prefer a "after #N" message. */
2279 if (absolute && curbuf->b_u_curhead->uh_next != NULL)
2280 {
2281 uhp = curbuf->b_u_curhead->uh_next;
2282 did_undo = FALSE;
2283 }
2284 else if (did_undo)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002285 uhp = curbuf->b_u_curhead;
2286 else
2287 uhp = curbuf->b_u_curhead->uh_next;
2288 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002289 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002290 uhp = curbuf->b_u_newhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002291
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002292 if (uhp == NULL)
2293 *msgbuf = NUL;
2294 else
2295 u_add_time(msgbuf, sizeof(msgbuf), uhp->uh_time);
2296
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002297 smsg((char_u *)_("%ld %s; %s #%ld %s"),
Bram Moolenaarca003e12006-03-17 23:19:38 +00002298 u_oldcount < 0 ? -u_oldcount : u_oldcount,
Bram Moolenaar89d40322006-08-29 15:30:07 +00002299 _(msgstr),
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002300 did_undo ? _("before") : _("after"),
2301 uhp == NULL ? 0L : uhp->uh_seq,
2302 msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303}
2304
2305/*
2306 * u_sync: stop adding to the current entry list
2307 */
2308 void
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002309u_sync(force)
2310 int force; /* Also sync when no_u_sync is set. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311{
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002312 /* Skip it when already synced or syncing is disabled. */
2313 if (curbuf->b_u_synced || (!force && no_u_sync > 0))
2314 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2316 if (im_is_preediting())
2317 return; /* XIM is busy, don't break an undo sequence */
2318#endif
2319 if (p_ul < 0)
2320 curbuf->b_u_synced = TRUE; /* no entries, nothing to do */
2321 else
2322 {
2323 u_getbot(); /* compute ue_bot of previous u_save */
2324 curbuf->b_u_curhead = NULL;
2325 }
2326}
2327
2328/*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002329 * ":undolist": List the leafs of the undo tree
2330 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002331 void
2332ex_undolist(eap)
Bram Moolenaarfff2bee2010-05-15 13:56:02 +02002333 exarg_T *eap UNUSED;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002334{
2335 garray_T ga;
2336 u_header_T *uhp;
2337 int mark;
2338 int nomark;
2339 int changes = 1;
2340 int i;
2341
2342 /*
2343 * 1: walk the tree to find all leafs, put the info in "ga".
2344 * 2: sort the lines
2345 * 3: display the list
2346 */
2347 mark = ++lastmark;
2348 nomark = ++lastmark;
2349 ga_init2(&ga, (int)sizeof(char *), 20);
2350
2351 uhp = curbuf->b_u_oldhead;
2352 while (uhp != NULL)
2353 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002354 if (uhp->uh_prev == NULL && uhp->uh_walk != nomark
2355 && uhp->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002356 {
2357 if (ga_grow(&ga, 1) == FAIL)
2358 break;
2359 vim_snprintf((char *)IObuff, IOSIZE, "%6ld %7ld ",
2360 uhp->uh_seq, changes);
2361 u_add_time(IObuff + STRLEN(IObuff), IOSIZE - STRLEN(IObuff),
2362 uhp->uh_time);
2363 ((char_u **)(ga.ga_data))[ga.ga_len++] = vim_strsave(IObuff);
2364 }
2365
2366 uhp->uh_walk = mark;
2367
2368 /* go down in the tree if we haven't been there */
2369 if (uhp->uh_prev != NULL && uhp->uh_prev->uh_walk != nomark
2370 && uhp->uh_prev->uh_walk != mark)
2371 {
2372 uhp = uhp->uh_prev;
2373 ++changes;
2374 }
2375
2376 /* go to alternate branch if we haven't been there */
2377 else if (uhp->uh_alt_next != NULL
2378 && uhp->uh_alt_next->uh_walk != nomark
2379 && uhp->uh_alt_next->uh_walk != mark)
2380 uhp = uhp->uh_alt_next;
2381
2382 /* go up in the tree if we haven't been there and we are at the
2383 * start of alternate branches */
2384 else if (uhp->uh_next != NULL && uhp->uh_alt_prev == NULL
2385 && uhp->uh_next->uh_walk != nomark
2386 && uhp->uh_next->uh_walk != mark)
2387 {
2388 uhp = uhp->uh_next;
2389 --changes;
2390 }
2391
2392 else
2393 {
2394 /* need to backtrack; mark this node as done */
2395 uhp->uh_walk = nomark;
2396 if (uhp->uh_alt_prev != NULL)
2397 uhp = uhp->uh_alt_prev;
2398 else
2399 {
2400 uhp = uhp->uh_next;
2401 --changes;
2402 }
2403 }
2404 }
2405
2406 if (ga.ga_len == 0)
2407 MSG(_("Nothing to undo"));
2408 else
2409 {
2410 sort_strings((char_u **)ga.ga_data, ga.ga_len);
2411
2412 msg_start();
2413 msg_puts_attr((char_u *)_("number changes time"), hl_attr(HLF_T));
2414 for (i = 0; i < ga.ga_len && !got_int; ++i)
2415 {
2416 msg_putchar('\n');
2417 if (got_int)
2418 break;
2419 msg_puts(((char_u **)ga.ga_data)[i]);
2420 }
2421 msg_end();
2422
2423 ga_clear_strings(&ga);
2424 }
2425}
2426
2427/*
2428 * Put the timestamp of an undo header in "buf[buflen]" in a nice format.
2429 */
2430 static void
2431u_add_time(buf, buflen, tt)
2432 char_u *buf;
2433 size_t buflen;
2434 time_t tt;
2435{
2436#ifdef HAVE_STRFTIME
2437 struct tm *curtime;
2438
2439 if (time(NULL) - tt >= 100)
2440 {
2441 curtime = localtime(&tt);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00002442 (void)strftime((char *)buf, buflen, "%H:%M:%S", curtime);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002443 }
2444 else
2445#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002446 vim_snprintf((char *)buf, buflen, _("%ld seconds ago"),
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002447 (long)(time(NULL) - tt));
2448}
2449
2450/*
Bram Moolenaare224ffa2006-03-01 00:01:28 +00002451 * ":undojoin": continue adding to the last entry list
2452 */
Bram Moolenaare224ffa2006-03-01 00:01:28 +00002453 void
2454ex_undojoin(eap)
Bram Moolenaarfff2bee2010-05-15 13:56:02 +02002455 exarg_T *eap UNUSED;
Bram Moolenaare224ffa2006-03-01 00:01:28 +00002456{
Bram Moolenaare224ffa2006-03-01 00:01:28 +00002457 if (curbuf->b_u_newhead == NULL)
2458 return; /* nothing changed before */
Bram Moolenaar57657d82006-04-21 22:12:41 +00002459 if (curbuf->b_u_curhead != NULL)
2460 {
2461 EMSG(_("E790: undojoin is not allowed after undo"));
2462 return;
2463 }
2464 if (!curbuf->b_u_synced)
2465 return; /* already unsynced */
Bram Moolenaare224ffa2006-03-01 00:01:28 +00002466 if (p_ul < 0)
2467 return; /* no entries, nothing to do */
2468 else
2469 {
2470 /* Go back to the last entry */
2471 curbuf->b_u_curhead = curbuf->b_u_newhead;
2472 curbuf->b_u_synced = FALSE; /* no entries, nothing to do */
2473 }
2474}
2475
2476/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 * Called after writing the file and setting b_changed to FALSE.
2478 * Now an undo means that the buffer is modified.
2479 */
2480 void
2481u_unchanged(buf)
2482 buf_T *buf;
2483{
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002484 u_unch_branch(buf->b_u_oldhead);
2485 buf->b_did_warn = FALSE;
2486}
2487
2488 static void
2489u_unch_branch(uhp)
2490 u_header_T *uhp;
2491{
Bram Moolenaar1e607892006-03-13 22:15:53 +00002492 u_header_T *uh;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002494 for (uh = uhp; uh != NULL; uh = uh->uh_prev)
2495 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 uh->uh_flags |= UH_CHANGED;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002497 if (uh->uh_alt_next != NULL)
2498 u_unch_branch(uh->uh_alt_next); /* recursive */
2499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500}
2501
2502/*
2503 * Get pointer to last added entry.
2504 * If it's not valid, give an error message and return NULL.
2505 */
2506 static u_entry_T *
2507u_get_headentry()
2508{
2509 if (curbuf->b_u_newhead == NULL || curbuf->b_u_newhead->uh_entry == NULL)
2510 {
2511 EMSG(_("E439: undo list corrupt"));
2512 return NULL;
2513 }
2514 return curbuf->b_u_newhead->uh_entry;
2515}
2516
2517/*
2518 * u_getbot(): compute the line number of the previous u_save
2519 * It is called only when b_u_synced is FALSE.
2520 */
2521 static void
2522u_getbot()
2523{
2524 u_entry_T *uep;
2525 linenr_T extra;
2526
2527 uep = u_get_headentry(); /* check for corrupt undo list */
2528 if (uep == NULL)
2529 return;
2530
2531 uep = curbuf->b_u_newhead->uh_getbot_entry;
2532 if (uep != NULL)
2533 {
2534 /*
2535 * the new ue_bot is computed from the number of lines that has been
2536 * inserted (0 - deleted) since calling u_save. This is equal to the
2537 * old line count subtracted from the current line count.
2538 */
2539 extra = curbuf->b_ml.ml_line_count - uep->ue_lcount;
2540 uep->ue_bot = uep->ue_top + uep->ue_size + 1 + extra;
2541 if (uep->ue_bot < 1 || uep->ue_bot > curbuf->b_ml.ml_line_count)
2542 {
2543 EMSG(_("E440: undo line missing"));
2544 uep->ue_bot = uep->ue_top + 1; /* assume all lines deleted, will
2545 * get all the old lines back
2546 * without deleting the current
2547 * ones */
2548 }
2549
2550 curbuf->b_u_newhead->uh_getbot_entry = NULL;
2551 }
2552
2553 curbuf->b_u_synced = TRUE;
2554}
2555
2556/*
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002557 * Free one header "uhp" and its entry list and adjust the pointers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 */
2559 static void
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002560u_freeheader(buf, uhp, uhpp)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002561 buf_T *buf;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002562 u_header_T *uhp;
2563 u_header_T **uhpp; /* if not NULL reset when freeing this header */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564{
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002565 u_header_T *uhap;
2566
Bram Moolenaar1e607892006-03-13 22:15:53 +00002567 /* When there is an alternate redo list free that branch completely,
2568 * because we can never go there. */
2569 if (uhp->uh_alt_next != NULL)
2570 u_freebranch(buf, uhp->uh_alt_next, uhpp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571
Bram Moolenaar1e607892006-03-13 22:15:53 +00002572 if (uhp->uh_alt_prev != NULL)
2573 uhp->uh_alt_prev->uh_alt_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574
Bram Moolenaar1e607892006-03-13 22:15:53 +00002575 /* Update the links in the list to remove the header. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 if (uhp->uh_next == NULL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002577 buf->b_u_oldhead = uhp->uh_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 else
2579 uhp->uh_next->uh_prev = uhp->uh_prev;
2580
2581 if (uhp->uh_prev == NULL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002582 buf->b_u_newhead = uhp->uh_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 else
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002584 for (uhap = uhp->uh_prev; uhap != NULL; uhap = uhap->uh_alt_next)
2585 uhap->uh_next = uhp->uh_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586
Bram Moolenaar1e607892006-03-13 22:15:53 +00002587 u_freeentries(buf, uhp, uhpp);
2588}
2589
2590/*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002591 * Free an alternate branch and any following alternate branches.
Bram Moolenaar1e607892006-03-13 22:15:53 +00002592 */
2593 static void
2594u_freebranch(buf, uhp, uhpp)
2595 buf_T *buf;
2596 u_header_T *uhp;
2597 u_header_T **uhpp; /* if not NULL reset when freeing this header */
2598{
2599 u_header_T *tofree, *next;
2600
Bram Moolenaar07d06772007-11-10 21:51:15 +00002601 /* If this is the top branch we may need to use u_freeheader() to update
2602 * all the pointers. */
2603 if (uhp == buf->b_u_oldhead)
2604 {
2605 u_freeheader(buf, uhp, uhpp);
2606 return;
2607 }
2608
Bram Moolenaar1e607892006-03-13 22:15:53 +00002609 if (uhp->uh_alt_prev != NULL)
2610 uhp->uh_alt_prev->uh_alt_next = NULL;
2611
2612 next = uhp;
2613 while (next != NULL)
2614 {
2615 tofree = next;
2616 if (tofree->uh_alt_next != NULL)
2617 u_freebranch(buf, tofree->uh_alt_next, uhpp); /* recursive */
2618 next = tofree->uh_prev;
2619 u_freeentries(buf, tofree, uhpp);
2620 }
2621}
2622
2623/*
2624 * Free all the undo entries for one header and the header itself.
2625 * This means that "uhp" is invalid when returning.
2626 */
2627 static void
2628u_freeentries(buf, uhp, uhpp)
2629 buf_T *buf;
2630 u_header_T *uhp;
2631 u_header_T **uhpp; /* if not NULL reset when freeing this header */
2632{
2633 u_entry_T *uep, *nuep;
2634
2635 /* Check for pointers to the header that become invalid now. */
2636 if (buf->b_u_curhead == uhp)
2637 buf->b_u_curhead = NULL;
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002638 if (buf->b_u_newhead == uhp)
2639 buf->b_u_newhead = NULL; /* freeing the newest entry */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002640 if (uhpp != NULL && uhp == *uhpp)
2641 *uhpp = NULL;
2642
2643 for (uep = uhp->uh_entry; uep != NULL; uep = nuep)
2644 {
2645 nuep = uep->ue_next;
2646 u_freeentry(uep, uep->ue_size);
2647 }
2648
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002649#ifdef U_DEBUG
2650 uhp->uh_magic = 0;
2651#endif
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002652 vim_free((char_u *)uhp);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002653 --buf->b_u_numhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654}
2655
2656/*
2657 * free entry 'uep' and 'n' lines in uep->ue_array[]
2658 */
2659 static void
2660u_freeentry(uep, n)
2661 u_entry_T *uep;
2662 long n;
2663{
Bram Moolenaar8d343302005-07-12 22:46:17 +00002664 while (n > 0)
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002665 vim_free(uep->ue_array[--n]);
2666 vim_free((char_u *)uep->ue_array);
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002667#ifdef U_DEBUG
2668 uep->ue_magic = 0;
2669#endif
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002670 vim_free((char_u *)uep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671}
2672
2673/*
2674 * invalidate the undo buffer; called when storage has already been released
2675 */
2676 void
2677u_clearall(buf)
2678 buf_T *buf;
2679{
2680 buf->b_u_newhead = buf->b_u_oldhead = buf->b_u_curhead = NULL;
2681 buf->b_u_synced = TRUE;
2682 buf->b_u_numhead = 0;
2683 buf->b_u_line_ptr = NULL;
2684 buf->b_u_line_lnum = 0;
2685}
2686
2687/*
2688 * save the line "lnum" for the "U" command
2689 */
2690 void
2691u_saveline(lnum)
2692 linenr_T lnum;
2693{
2694 if (lnum == curbuf->b_u_line_lnum) /* line is already saved */
2695 return;
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002696 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) /* should never happen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 return;
2698 u_clearline();
2699 curbuf->b_u_line_lnum = lnum;
2700 if (curwin->w_cursor.lnum == lnum)
2701 curbuf->b_u_line_colnr = curwin->w_cursor.col;
2702 else
2703 curbuf->b_u_line_colnr = 0;
2704 if ((curbuf->b_u_line_ptr = u_save_line(lnum)) == NULL)
2705 do_outofmem_msg((long_u)0);
2706}
2707
2708/*
2709 * clear the line saved for the "U" command
2710 * (this is used externally for crossing a line while in insert mode)
2711 */
2712 void
2713u_clearline()
2714{
2715 if (curbuf->b_u_line_ptr != NULL)
2716 {
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002717 vim_free(curbuf->b_u_line_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 curbuf->b_u_line_ptr = NULL;
2719 curbuf->b_u_line_lnum = 0;
2720 }
2721}
2722
2723/*
2724 * Implementation of the "U" command.
2725 * Differentiation from vi: "U" can be undone with the next "U".
2726 * We also allow the cursor to be in another line.
2727 */
2728 void
2729u_undoline()
2730{
2731 colnr_T t;
2732 char_u *oldp;
2733
2734 if (undo_off)
2735 return;
2736
Bram Moolenaare3300c82008-02-13 14:21:38 +00002737 if (curbuf->b_u_line_ptr == NULL
2738 || curbuf->b_u_line_lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 {
2740 beep_flush();
2741 return;
2742 }
Bram Moolenaare3300c82008-02-13 14:21:38 +00002743
2744 /* first save the line for the 'u' command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 if (u_savecommon(curbuf->b_u_line_lnum - 1,
2746 curbuf->b_u_line_lnum + 1, (linenr_T)0) == FAIL)
2747 return;
2748 oldp = u_save_line(curbuf->b_u_line_lnum);
2749 if (oldp == NULL)
2750 {
2751 do_outofmem_msg((long_u)0);
2752 return;
2753 }
2754 ml_replace(curbuf->b_u_line_lnum, curbuf->b_u_line_ptr, TRUE);
2755 changed_bytes(curbuf->b_u_line_lnum, 0);
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002756 vim_free(curbuf->b_u_line_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 curbuf->b_u_line_ptr = oldp;
2758
2759 t = curbuf->b_u_line_colnr;
2760 if (curwin->w_cursor.lnum == curbuf->b_u_line_lnum)
2761 curbuf->b_u_line_colnr = curwin->w_cursor.col;
2762 curwin->w_cursor.col = t;
2763 curwin->w_cursor.lnum = curbuf->b_u_line_lnum;
Bram Moolenaare3300c82008-02-13 14:21:38 +00002764 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765}
2766
2767/*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002768 * Free all allocated memory blocks for the buffer 'buf'.
2769 */
2770 void
2771u_blockfree(buf)
2772 buf_T *buf;
2773{
Bram Moolenaar1e607892006-03-13 22:15:53 +00002774 while (buf->b_u_oldhead != NULL)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002775 u_freeheader(buf, buf->b_u_oldhead, NULL);
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002776 vim_free(buf->b_u_line_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777}
2778
2779/*
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002780 * u_save_line(): allocate memory and copy line 'lnum' into it.
2781 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 */
2783 static char_u *
2784u_save_line(lnum)
2785 linenr_T lnum;
2786{
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002787 return vim_strsave(ml_get(lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788}
2789
2790/*
2791 * Check if the 'modified' flag is set, or 'ff' has changed (only need to
2792 * check the first character, because it can only be "dos", "unix" or "mac").
2793 * "nofile" and "scratch" type buffers are considered to always be unchanged.
2794 */
2795 int
2796bufIsChanged(buf)
2797 buf_T *buf;
2798{
2799 return
2800#ifdef FEAT_QUICKFIX
2801 !bt_dontwrite(buf) &&
2802#endif
2803 (buf->b_changed || file_ff_differs(buf));
2804}
2805
2806 int
2807curbufIsChanged()
2808{
2809 return
2810#ifdef FEAT_QUICKFIX
2811 !bt_dontwrite(curbuf) &&
2812#endif
2813 (curbuf->b_changed || file_ff_differs(curbuf));
2814}