blob: 5b049862504d62952383763f273a2842c390db87 [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
1078#ifdef UNIX
1079 /*
1080 * Try to set the group of the undo file same as the original file. If
1081 * this fails, set the protection bits for the group same as the
1082 * protection bits for others.
1083 */
1084 if (st_old_valid && (mch_stat((char *)file_name, &st_new) >= 0
1085 && st_new.st_gid != st_old.st_gid
1086# ifdef HAVE_FCHOWN /* sequent-ptx lacks fchown() */
1087 && fchown(fd, (uid_t)-1, st_old.st_gid) != 0)
1088# endif
1089 )
1090 mch_setperm(file_name, (perm & 0707) | ((perm & 07) << 3));
1091# ifdef HAVE_SELINUX
1092 if (buf->b_ffname != NULL)
1093 mch_copy_sec(buf->b_ffname, file_name);
1094# endif
1095#endif
1096
1097 fp = fdopen(fd, "w");
1098 if (fp == NULL)
1099 {
1100 EMSG2(_(e_not_open), file_name);
1101 close(fd);
1102 mch_remove(file_name);
1103 goto theend;
1104 }
1105
1106 /* Start writing, first the undo file header. */
1107 if (fwrite(UF_START_MAGIC, (size_t)UF_START_MAGIC_LEN, (size_t)1, fp) != 1)
1108 goto write_error;
1109 put_bytes(fp, (long_u)UF_VERSION, 2);
1110
1111 /* Write a hash of the buffer text, so that we can verify it is still the
1112 * same when reading the buffer text. */
1113 if (fwrite(hash, (size_t)UNDO_HASH_SIZE, (size_t)1, fp) != 1)
1114 goto write_error;
1115 put_bytes(fp, (long_u)buf->b_ml.ml_line_count, 4);
1116
1117 /* Begin undo data for U */
1118 str_len = buf->b_u_line_ptr != NULL ? (int)STRLEN(buf->b_u_line_ptr) : 0;
1119 put_bytes(fp, (long_u)str_len, 4);
1120 if (str_len > 0 && fwrite(buf->b_u_line_ptr, (size_t)str_len,
1121 (size_t)1, fp) != 1)
1122 goto write_error;
1123
1124 put_bytes(fp, (long_u)buf->b_u_line_lnum, 4);
1125 put_bytes(fp, (long_u)buf->b_u_line_colnr, 4);
1126
1127 /* Begin general undo data */
1128 put_header_ptr(fp, buf->b_u_oldhead);
1129 put_header_ptr(fp, buf->b_u_newhead);
1130 put_header_ptr(fp, buf->b_u_curhead);
1131
1132 put_bytes(fp, (long_u)buf->b_u_numhead, 4);
1133 put_bytes(fp, (long_u)buf->b_u_seq_last, 4);
1134 put_bytes(fp, (long_u)buf->b_u_seq_cur, 4);
1135 put_time(fp, buf->b_u_seq_time);
1136
1137 /*
1138 * Iteratively serialize UHPs and their UEPs from the top down.
1139 */
1140 mark = ++lastmark;
1141 uhp = buf->b_u_oldhead;
1142 while (uhp != NULL)
1143 {
1144 /* Serialize current UHP if we haven't seen it */
1145 if (uhp->uh_walk != mark)
1146 {
1147 uhp->uh_walk = mark;
1148#ifdef U_DEBUG
1149 ++headers_written;
1150#endif
1151
1152 if (put_bytes(fp, (long_u)UF_HEADER_MAGIC, 2) == FAIL)
1153 goto write_error;
1154
1155 put_header_ptr(fp, uhp->uh_next);
1156 put_header_ptr(fp, uhp->uh_prev);
1157 put_header_ptr(fp, uhp->uh_alt_next);
1158 put_header_ptr(fp, uhp->uh_alt_prev);
1159 put_bytes(fp, uhp->uh_seq, 4);
1160 serialize_pos(uhp->uh_cursor, fp);
1161#ifdef FEAT_VIRTUALEDIT
1162 put_bytes(fp, (long_u)uhp->uh_cursor_vcol, 4);
1163#else
1164 put_bytes(fp, (long_u)0, 4);
1165#endif
1166 put_bytes(fp, (long_u)uhp->uh_flags, 2);
1167 /* Assume NMARKS will stay the same. */
1168 for (i = 0; i < NMARKS; ++i)
1169 serialize_pos(uhp->uh_namedm[i], fp);
1170#ifdef FEAT_VISUAL
1171 serialize_visualinfo(&uhp->uh_visual, fp);
1172#else
1173 {
1174 visualinfo_T info;
1175
1176 memset(&info, 0, sizeof(visualinfo_T));
1177 serialize_visualinfo(&info, fp);
1178 }
1179#endif
1180 put_time(fp, uhp->uh_time);
1181
1182 /* Write all the entries. */
1183 for (uep = uhp->uh_entry; uep != NULL; uep = uep->ue_next)
1184 {
1185 put_bytes(fp, (long_u)UF_ENTRY_MAGIC, 2);
1186 if (serialize_uep(uep, fp) == FAIL)
1187 goto write_error;
1188 }
1189 put_bytes(fp, (long_u)UF_ENTRY_END_MAGIC, 2);
1190 }
1191
1192 /* Now walk through the tree - algorithm from undo_time */
1193 if (uhp->uh_prev != NULL && uhp->uh_prev->uh_walk != mark)
1194 uhp = uhp->uh_prev;
1195 else if (uhp->uh_alt_next != NULL && uhp->uh_alt_next->uh_walk != mark)
1196 uhp = uhp->uh_alt_next;
1197 else if (uhp->uh_next != NULL && uhp->uh_alt_prev == NULL
1198 && uhp->uh_next->uh_walk != mark)
1199 uhp = uhp->uh_next;
1200 else if (uhp->uh_alt_prev != NULL)
1201 uhp = uhp->uh_alt_prev;
1202 else
1203 uhp = uhp->uh_next;
1204 }
1205
1206 if (put_bytes(fp, (long_u)UF_HEADER_END_MAGIC, 2) == OK)
1207 write_ok = TRUE;
1208#ifdef U_DEBUG
1209 if (headers_written != buf->b_u_numhead)
1210 EMSG3("Written %ld headers, but numhead is %ld",
1211 headers_written, buf->b_u_numhead);
1212#endif
1213
1214write_error:
1215 fclose(fp);
1216 if (!write_ok)
1217 EMSG2(_("E829: write error in undo file: %s"), file_name);
1218
1219#if defined(MACOS_CLASSIC) || defined(WIN3264)
1220 if (buf->b_ffname != NULL)
1221 (void)mch_copy_file_attribute(buf->b_ffname, file_name);
1222#endif
1223#ifdef HAVE_ACL
1224 if (buf->b_ffname != NULL)
1225 {
1226 vim_acl_T acl;
1227
1228 /* For systems that support ACL: get the ACL from the original file. */
1229 acl = mch_get_acl(buf->b_ffname);
1230 mch_set_acl(file_name, acl);
1231 }
1232#endif
1233
1234theend:
1235 if (file_name != name)
1236 vim_free(file_name);
1237}
1238
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001239/*
1240 * Load the undo tree from an undo file.
1241 * If "name" is not NULL use it as the undo file name. This also means being
1242 * a bit more verbose.
1243 * Otherwise use curbuf->b_ffname to generate the undo file name.
1244 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text.
1245 */
1246 void
1247u_read_undo(name, hash)
1248 char_u *name;
1249 char_u *hash;
1250{
1251 char_u *file_name;
1252 FILE *fp;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001253 long version, str_len;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001254 char_u *line_ptr = NULL;
1255 linenr_T line_lnum;
1256 colnr_T line_colnr;
1257 linenr_T line_count;
Bram Moolenaar442b4222010-05-24 21:34:22 +02001258 int num_head = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001259 long old_header_seq, new_header_seq, cur_header_seq;
1260 long seq_last, seq_cur;
1261 short old_idx = -1, new_idx = -1, cur_idx = -1;
1262 long num_read_uhps = 0;
1263 time_t seq_time;
1264 int i, j;
1265 int c;
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001266 u_entry_T *uep, *last_uep;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001267 u_header_T *uhp;
1268 u_header_T **uhp_table = NULL;
1269 char_u read_hash[UNDO_HASH_SIZE];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001270 char_u magic_buf[UF_START_MAGIC_LEN];
1271#ifdef U_DEBUG
1272 int *uhp_table_used;
1273#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001274
1275 if (name == NULL)
1276 {
1277 file_name = u_get_undo_file_name(curbuf->b_ffname, TRUE);
1278 if (file_name == NULL)
1279 return;
1280 }
1281 else
1282 file_name = name;
1283
1284 if (p_verbose > 0)
1285 smsg((char_u *)_("Reading undo file: %s"), file_name);
1286 fp = mch_fopen((char *)file_name, "r");
1287 if (fp == NULL)
1288 {
1289 if (name != NULL || p_verbose > 0)
1290 EMSG2(_("E822: Cannot open undo file for reading: %s"), file_name);
1291 goto error;
1292 }
1293
Bram Moolenaar9db58062010-05-29 20:33:07 +02001294 /*
1295 * Read the undo file header.
1296 */
1297 if (fread(magic_buf, UF_START_MAGIC_LEN, 1, fp) != 1
1298 || memcmp(magic_buf, UF_START_MAGIC, UF_START_MAGIC_LEN) != 0)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001299 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001300 EMSG2(_("E823: Not an undo file: %s"), file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001301 goto error;
1302 }
1303 version = get2c(fp);
1304 if (version != UF_VERSION)
1305 {
1306 EMSG2(_("E824: Incompatible undo file: %s"), file_name);
1307 goto error;
1308 }
1309
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001310 if (fread(read_hash, UNDO_HASH_SIZE, 1, fp) != 1)
1311 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001312 corruption_error("hash", file_name);
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001313 goto error;
1314 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001315 line_count = (linenr_T)get4c(fp);
1316 if (memcmp(hash, read_hash, UNDO_HASH_SIZE) != 0
1317 || line_count != curbuf->b_ml.ml_line_count)
1318 {
1319 if (p_verbose > 0 || name != NULL)
1320 {
1321 verbose_enter();
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001322 give_warning((char_u *)_("File contents changed, cannot use undo info"), TRUE);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001323 verbose_leave();
1324 }
1325 goto error;
1326 }
1327
1328 /* Begin undo data for U */
1329 str_len = get4c(fp);
1330 if (str_len < 0)
1331 goto error;
1332 else if (str_len > 0)
1333 {
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001334 if ((line_ptr = U_ALLOC_LINE(str_len + 1)) == NULL)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001335 goto error;
1336 for (i = 0; i < str_len; i++)
1337 line_ptr[i] = (char_u)getc(fp);
1338 line_ptr[i] = NUL;
1339 }
1340 line_lnum = (linenr_T)get4c(fp);
1341 line_colnr = (colnr_T)get4c(fp);
1342
1343 /* Begin general undo data */
1344 old_header_seq = get4c(fp);
1345 new_header_seq = get4c(fp);
1346 cur_header_seq = get4c(fp);
1347 num_head = get4c(fp);
1348 seq_last = get4c(fp);
1349 seq_cur = get4c(fp);
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001350 seq_time = get8ctime(fp);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001351
1352 /* uhp_table will store the freshly created undo headers we allocate
1353 * until we insert them into curbuf. The table remains sorted by the
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001354 * sequence numbers of the headers.
1355 * When there are no headers uhp_table is NULL. */
1356 if (num_head > 0)
1357 {
1358 uhp_table = (u_header_T **)U_ALLOC_LINE(
1359 num_head * sizeof(u_header_T *));
1360 if (uhp_table == NULL)
1361 goto error;
1362 vim_memset(uhp_table, 0, num_head * sizeof(u_header_T *));
1363 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001364
Bram Moolenaar9db58062010-05-29 20:33:07 +02001365 while ((c = get2c(fp)) == UF_HEADER_MAGIC)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001366 {
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001367 if (num_read_uhps >= num_head)
1368 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001369 corruption_error("num_head", file_name);
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001370 goto error;
1371 }
1372
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001373 uhp = (u_header_T *)U_ALLOC_LINE(sizeof(u_header_T));
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001374 if (uhp == NULL)
1375 goto error;
1376 vim_memset(uhp, 0, sizeof(u_header_T));
Bram Moolenaar9db58062010-05-29 20:33:07 +02001377#ifdef U_DEBUG
1378 uhp->uh_magic = UH_MAGIC;
1379#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001380 /* We're not actually trying to store pointers here. We're just storing
1381 * IDs so we can swizzle them into pointers later - hence the type
1382 * cast. */
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001383 uhp->uh_next = (u_header_T *)(long_u)get4c(fp);
1384 uhp->uh_prev = (u_header_T *)(long_u)get4c(fp);
1385 uhp->uh_alt_next = (u_header_T *)(long_u)get4c(fp);
1386 uhp->uh_alt_prev = (u_header_T *)(long_u)get4c(fp);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001387 uhp->uh_seq = get4c(fp);
1388 if (uhp->uh_seq <= 0)
1389 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001390 corruption_error("uh_seq", file_name);
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001391 vim_free(uhp);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001392 goto error;
1393 }
1394 uhp->uh_walk = 0;
1395 unserialize_pos(&uhp->uh_cursor, fp);
1396#ifdef FEAT_VIRTUALEDIT
1397 uhp->uh_cursor_vcol = get4c(fp);
1398#else
1399 (void)get4c(fp);
1400#endif
1401 uhp->uh_flags = get2c(fp);
1402 for (i = 0; i < NMARKS; ++i)
1403 unserialize_pos(&uhp->uh_namedm[i], fp);
1404#ifdef FEAT_VISUAL
1405 unserialize_visualinfo(&uhp->uh_visual, fp);
1406#else
1407 {
1408 visualinfo_T info;
1409 unserialize_visualinfo(&info, fp);
1410 }
1411#endif
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001412 uhp->uh_time = get8ctime(fp);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001413
Bram Moolenaar9db58062010-05-29 20:33:07 +02001414 /* Unserialize the uep list. */
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001415 last_uep = NULL;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001416 while ((c = get2c(fp)) == UF_ENTRY_MAGIC)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001417 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001418 int error = FALSE;
1419
1420 uep = unserialize_uep(fp, &error, file_name);
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001421 if (last_uep == NULL)
1422 uhp->uh_entry = uep;
1423 else
1424 last_uep->ue_next = uep;
1425 last_uep = uep;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001426 if (uep == NULL || error)
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001427 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001428 u_free_uhp(uhp);
1429 goto error;
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001430 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001431 }
1432 if (c != UF_ENTRY_END_MAGIC)
1433 {
1434 corruption_error("entry end", file_name);
1435 u_free_uhp(uhp);
1436 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001437 }
1438
1439 /* Insertion sort the uhp into the table by its uh_seq. This is
1440 * required because, while the number of uhps is limited to
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001441 * num_head, and the uh_seq order is monotonic with respect to
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001442 * creation time, the starting uh_seq can be > 0 if any undolevel
1443 * culling was done at undofile write time, and there can be uh_seq
1444 * gaps in the uhps.
1445 */
Bram Moolenaar644fdff2010-05-30 13:26:21 +02001446 for (i = num_read_uhps - 1; ; --i)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001447 {
1448 /* if i == -1, we've hit the leftmost side of the table, so insert
1449 * at uhp_table[0]. */
1450 if (i == -1 || uhp->uh_seq > uhp_table[i]->uh_seq)
1451 {
1452 /* If we've had to move from the rightmost side of the table,
1453 * we have to shift everything to the right by one spot. */
Bram Moolenaar9d728072010-05-24 22:06:04 +02001454 if (num_read_uhps - i - 1 > 0)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001455 {
1456 memmove(uhp_table + i + 2, uhp_table + i + 1,
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001457 (num_read_uhps - i - 1) * sizeof(u_header_T *));
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001458 }
1459 uhp_table[i + 1] = uhp;
1460 break;
1461 }
1462 else if (uhp->uh_seq == uhp_table[i]->uh_seq)
1463 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001464 corruption_error("duplicate uh_seq", file_name);
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001465 u_free_uhp(uhp);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001466 goto error;
1467 }
1468 }
1469 num_read_uhps++;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001470 }
1471
Bram Moolenaar9db58062010-05-29 20:33:07 +02001472 if (c != UF_HEADER_END_MAGIC)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001473 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001474 corruption_error("end marker", file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001475 goto error;
1476 }
1477
Bram Moolenaar9db58062010-05-29 20:33:07 +02001478#ifdef U_DEBUG
1479 uhp_table_used = (int *)alloc_clear(
1480 (unsigned)(sizeof(int) * num_head + 1));
1481# define SET_FLAG(j) ++uhp_table_used[j]
1482#else
1483# define SET_FLAG(j)
1484#endif
1485
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001486 /* We've organized all of the uhps into a table sorted by uh_seq. Now we
1487 * iterate through the table and swizzle each sequence number we've
Bram Moolenaar9db58062010-05-29 20:33:07 +02001488 * stored in uh_* into a pointer corresponding to the header with that
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001489 * sequence number. Then free curbuf's old undo structure, give curbuf
1490 * the updated {old,new,cur}head pointers, and then free the table. */
1491 for (i = 0; i < num_head; i++)
1492 {
1493 uhp = uhp_table[i];
1494 if (uhp == NULL)
1495 continue;
1496 for (j = 0; j < num_head; j++)
1497 {
1498 if (uhp_table[j] == NULL)
1499 continue;
1500 if (uhp_table[j]->uh_seq == (long)uhp->uh_next)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001501 {
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001502 uhp->uh_next = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001503 SET_FLAG(j);
1504 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001505 if (uhp_table[j]->uh_seq == (long)uhp->uh_prev)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001506 {
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001507 uhp->uh_prev = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001508 SET_FLAG(j);
1509 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001510 if (uhp_table[j]->uh_seq == (long)uhp->uh_alt_next)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001511 {
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001512 uhp->uh_alt_next = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001513 SET_FLAG(j);
1514 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001515 if (uhp_table[j]->uh_seq == (long)uhp->uh_alt_prev)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001516 {
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001517 uhp->uh_alt_prev = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001518 SET_FLAG(j);
1519 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001520 }
1521 if (old_header_seq > 0 && old_idx < 0 && uhp->uh_seq == old_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001522 {
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001523 old_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001524 SET_FLAG(i);
1525 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001526 if (new_header_seq > 0 && new_idx < 0 && uhp->uh_seq == new_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001527 {
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001528 new_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001529 SET_FLAG(i);
1530 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001531 if (cur_header_seq > 0 && cur_idx < 0 && uhp->uh_seq == cur_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001532 {
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001533 cur_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001534 SET_FLAG(i);
1535 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001536 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001537
1538 /* Now that we have read the undo info successfully, free the current undo
1539 * info and use the info from the file. */
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001540 u_blockfree(curbuf);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001541 curbuf->b_u_oldhead = old_idx < 0 ? NULL : uhp_table[old_idx];
1542 curbuf->b_u_newhead = new_idx < 0 ? NULL : uhp_table[new_idx];
1543 curbuf->b_u_curhead = cur_idx < 0 ? NULL : uhp_table[cur_idx];
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001544 curbuf->b_u_line_ptr = line_ptr;
1545 curbuf->b_u_line_lnum = line_lnum;
1546 curbuf->b_u_line_colnr = line_colnr;
1547 curbuf->b_u_numhead = num_head;
1548 curbuf->b_u_seq_last = seq_last;
1549 curbuf->b_u_seq_cur = seq_cur;
1550 curbuf->b_u_seq_time = seq_time;
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001551 vim_free(uhp_table);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001552
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001553#ifdef U_DEBUG
Bram Moolenaar9db58062010-05-29 20:33:07 +02001554 for (i = 0; i < num_head; ++i)
1555 if (uhp_table_used[i] == 0)
1556 EMSGN("uhp_table entry %ld not used, leaking memory", i);
1557 vim_free(uhp_table_used);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001558 u_check(TRUE);
1559#endif
Bram Moolenaar9db58062010-05-29 20:33:07 +02001560
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001561 if (name != NULL)
1562 smsg((char_u *)_("Finished reading undo file %s"), file_name);
1563 goto theend;
1564
1565error:
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001566 vim_free(line_ptr);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001567 if (uhp_table != NULL)
1568 {
1569 for (i = 0; i < num_head; i++)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001570 if (uhp_table[i] != NULL)
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001571 u_free_uhp(uhp_table[i]);
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001572 vim_free(uhp_table);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001573 }
1574
1575theend:
1576 if (fp != NULL)
1577 fclose(fp);
1578 if (file_name != name)
1579 vim_free(file_name);
1580 return;
1581}
1582
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001583#endif /* FEAT_PERSISTENT_UNDO */
1584
1585
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586/*
1587 * If 'cpoptions' contains 'u': Undo the previous undo or redo (vi compatible).
1588 * If 'cpoptions' does not contain 'u': Always undo.
1589 */
1590 void
1591u_undo(count)
1592 int count;
1593{
1594 /*
1595 * If we get an undo command while executing a macro, we behave like the
1596 * original vi. If this happens twice in one macro the result will not
1597 * be compatible.
1598 */
1599 if (curbuf->b_u_synced == FALSE)
1600 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001601 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 count = 1;
1603 }
1604
1605 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
1606 undo_undoes = TRUE;
1607 else
1608 undo_undoes = !undo_undoes;
1609 u_doit(count);
1610}
1611
1612/*
1613 * If 'cpoptions' contains 'u': Repeat the previous undo or redo.
1614 * If 'cpoptions' does not contain 'u': Always redo.
1615 */
1616 void
1617u_redo(count)
1618 int count;
1619{
1620 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
1621 undo_undoes = FALSE;
1622 u_doit(count);
1623}
1624
1625/*
1626 * Undo or redo, depending on 'undo_undoes', 'count' times.
1627 */
1628 static void
Bram Moolenaarca003e12006-03-17 23:19:38 +00001629u_doit(startcount)
1630 int startcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631{
Bram Moolenaarca003e12006-03-17 23:19:38 +00001632 int count = startcount;
1633
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001634 if (!undo_allowed())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636
1637 u_newcount = 0;
1638 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001639 if (curbuf->b_ml.ml_flags & ML_EMPTY)
1640 u_oldcount = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 while (count--)
1642 {
1643 if (undo_undoes)
1644 {
1645 if (curbuf->b_u_curhead == NULL) /* first undo */
1646 curbuf->b_u_curhead = curbuf->b_u_newhead;
1647 else if (p_ul > 0) /* multi level undo */
1648 /* get next undo */
1649 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_next;
1650 /* nothing to undo */
1651 if (curbuf->b_u_numhead == 0 || curbuf->b_u_curhead == NULL)
1652 {
1653 /* stick curbuf->b_u_curhead at end */
1654 curbuf->b_u_curhead = curbuf->b_u_oldhead;
1655 beep_flush();
Bram Moolenaarca003e12006-03-17 23:19:38 +00001656 if (count == startcount - 1)
1657 {
1658 MSG(_("Already at oldest change"));
1659 return;
1660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 break;
1662 }
1663
Bram Moolenaarca003e12006-03-17 23:19:38 +00001664 u_undoredo(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 }
1666 else
1667 {
1668 if (curbuf->b_u_curhead == NULL || p_ul <= 0)
1669 {
1670 beep_flush(); /* nothing to redo */
Bram Moolenaarca003e12006-03-17 23:19:38 +00001671 if (count == startcount - 1)
1672 {
1673 MSG(_("Already at newest change"));
1674 return;
1675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 break;
1677 }
1678
Bram Moolenaarca003e12006-03-17 23:19:38 +00001679 u_undoredo(FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00001680
1681 /* Advance for next redo. Set "newhead" when at the end of the
1682 * redoable changes. */
1683 if (curbuf->b_u_curhead->uh_prev == NULL)
1684 curbuf->b_u_newhead = curbuf->b_u_curhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_prev;
1686 }
1687 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001688 u_undo_end(undo_undoes, FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00001689}
1690
Bram Moolenaar1e607892006-03-13 22:15:53 +00001691/*
1692 * Undo or redo over the timeline.
1693 * When "step" is negative go back in time, otherwise goes forward in time.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001694 * When "sec" is FALSE make "step" steps, when "sec" is TRUE use "step" as
1695 * seconds.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001696 * When "absolute" is TRUE use "step" as the sequence number to jump to.
1697 * "sec" must be FALSE then.
Bram Moolenaar1e607892006-03-13 22:15:53 +00001698 */
1699 void
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001700undo_time(step, sec, absolute)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001701 long step;
1702 int sec;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001703 int absolute;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001704{
1705 long target;
1706 long closest;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001707 long closest_start;
1708 long closest_seq = 0;
1709 long val;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001710 u_header_T *uhp;
1711 u_header_T *last;
1712 int mark;
1713 int nomark;
1714 int round;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001715 int dosec = sec;
1716 int above = FALSE;
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001717 int did_undo = TRUE;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001718
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001719 /* First make sure the current undoable change is synced. */
1720 if (curbuf->b_u_synced == FALSE)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001721 u_sync(TRUE);
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001722
Bram Moolenaar1e607892006-03-13 22:15:53 +00001723 u_newcount = 0;
1724 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001725 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar1e607892006-03-13 22:15:53 +00001726 u_oldcount = -1;
1727
Bram Moolenaarca003e12006-03-17 23:19:38 +00001728 /* "target" is the node below which we want to be.
1729 * Init "closest" to a value we can't reach. */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001730 if (absolute)
1731 {
1732 target = step;
Bram Moolenaarca003e12006-03-17 23:19:38 +00001733 closest = -1;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001734 }
Bram Moolenaarca003e12006-03-17 23:19:38 +00001735 else
Bram Moolenaar1e607892006-03-13 22:15:53 +00001736 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00001737 /* When doing computations with time_t subtract starttime, because
1738 * time_t converted to a long may result in a wrong number. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001739 if (sec)
Bram Moolenaarca003e12006-03-17 23:19:38 +00001740 target = (long)(curbuf->b_u_seq_time - starttime) + step;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001741 else
1742 target = curbuf->b_u_seq_cur + step;
Bram Moolenaarca003e12006-03-17 23:19:38 +00001743 if (step < 0)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001744 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00001745 if (target < 0)
1746 target = 0;
1747 closest = -1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001748 }
1749 else
1750 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00001751 if (sec)
Bram Moolenaardb552d602006-03-23 22:59:57 +00001752 closest = (long)(time(NULL) - starttime + 1);
Bram Moolenaarca003e12006-03-17 23:19:38 +00001753 else
1754 closest = curbuf->b_u_seq_last + 2;
1755 if (target >= closest)
1756 target = closest - 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001757 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00001758 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001759 closest_start = closest;
Bram Moolenaarca003e12006-03-17 23:19:38 +00001760 closest_seq = curbuf->b_u_seq_cur;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001761
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001762 /*
1763 * May do this twice:
Bram Moolenaar1e607892006-03-13 22:15:53 +00001764 * 1. Search for "target", update "closest" to the best match found.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001765 * 2. If "target" not found search for "closest".
1766 *
1767 * When using the closest time we use the sequence number in the second
1768 * round, because there may be several entries with the same time.
1769 */
Bram Moolenaar1e607892006-03-13 22:15:53 +00001770 for (round = 1; round <= 2; ++round)
1771 {
1772 /* Find the path from the current state to where we want to go. The
1773 * desired state can be anywhere in the undo tree, need to go all over
1774 * it. We put "nomark" in uh_walk where we have been without success,
1775 * "mark" where it could possibly be. */
1776 mark = ++lastmark;
1777 nomark = ++lastmark;
1778
1779 if (curbuf->b_u_curhead == NULL) /* at leaf of the tree */
1780 uhp = curbuf->b_u_newhead;
1781 else
1782 uhp = curbuf->b_u_curhead;
1783
1784 while (uhp != NULL)
1785 {
1786 uhp->uh_walk = mark;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001787 val = (long)(dosec ? (uhp->uh_time - starttime) : uhp->uh_seq);
Bram Moolenaar1e607892006-03-13 22:15:53 +00001788
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001789 if (round == 1)
1790 {
1791 /* Remember the header that is closest to the target.
1792 * It must be at least in the right direction (checked with
Bram Moolenaarca003e12006-03-17 23:19:38 +00001793 * "b_u_seq_cur"). When the timestamp is equal find the
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001794 * highest/lowest sequence number. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00001795 if ((step < 0 ? uhp->uh_seq <= curbuf->b_u_seq_cur
1796 : uhp->uh_seq > curbuf->b_u_seq_cur)
1797 && ((dosec && val == closest)
1798 ? (step < 0
1799 ? uhp->uh_seq < closest_seq
1800 : uhp->uh_seq > closest_seq)
1801 : closest == closest_start
1802 || (val > target
1803 ? (closest > target
1804 ? val - target <= closest - target
1805 : val - target <= target - closest)
1806 : (closest > target
1807 ? target - val <= closest - target
1808 : target - val <= target - closest))))
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001809 {
1810 closest = val;
1811 closest_seq = uhp->uh_seq;
1812 }
1813 }
1814
1815 /* Quit searching when we found a match. But when searching for a
1816 * time we need to continue looking for the best uh_seq. */
1817 if (target == val && !dosec)
1818 break;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001819
1820 /* go down in the tree if we haven't been there */
1821 if (uhp->uh_prev != NULL && uhp->uh_prev->uh_walk != nomark
1822 && uhp->uh_prev->uh_walk != mark)
1823 uhp = uhp->uh_prev;
1824
1825 /* go to alternate branch if we haven't been there */
1826 else if (uhp->uh_alt_next != NULL
1827 && uhp->uh_alt_next->uh_walk != nomark
1828 && uhp->uh_alt_next->uh_walk != mark)
1829 uhp = uhp->uh_alt_next;
1830
1831 /* go up in the tree if we haven't been there and we are at the
1832 * start of alternate branches */
1833 else if (uhp->uh_next != NULL && uhp->uh_alt_prev == NULL
1834 && uhp->uh_next->uh_walk != nomark
1835 && uhp->uh_next->uh_walk != mark)
Bram Moolenaardb552d602006-03-23 22:59:57 +00001836 {
1837 /* If still at the start we don't go through this change. */
1838 if (uhp == curbuf->b_u_curhead)
1839 uhp->uh_walk = nomark;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001840 uhp = uhp->uh_next;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001841 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00001842
1843 else
1844 {
1845 /* need to backtrack; mark this node as useless */
1846 uhp->uh_walk = nomark;
1847 if (uhp->uh_alt_prev != NULL)
1848 uhp = uhp->uh_alt_prev;
1849 else
1850 uhp = uhp->uh_next;
1851 }
1852 }
1853
1854 if (uhp != NULL) /* found it */
1855 break;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001856
1857 if (absolute)
1858 {
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001859 EMSGN(_("E830: Undo number %ld not found"), step);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001860 return;
1861 }
1862
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001863 if (closest == closest_start)
Bram Moolenaar1e607892006-03-13 22:15:53 +00001864 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001865 if (step < 0)
1866 MSG(_("Already at oldest change"));
1867 else
1868 MSG(_("Already at newest change"));
Bram Moolenaar1e607892006-03-13 22:15:53 +00001869 return;
1870 }
1871
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001872 target = closest_seq;
1873 dosec = FALSE;
1874 if (step < 0)
1875 above = TRUE; /* stop above the header */
Bram Moolenaar1e607892006-03-13 22:15:53 +00001876 }
1877
1878 /* If we found it: Follow the path to go to where we want to be. */
1879 if (uhp != NULL)
1880 {
1881 /*
1882 * First go up the tree as much as needed.
1883 */
1884 for (;;)
1885 {
1886 uhp = curbuf->b_u_curhead;
1887 if (uhp == NULL)
1888 uhp = curbuf->b_u_newhead;
1889 else
Bram Moolenaar1e607892006-03-13 22:15:53 +00001890 uhp = uhp->uh_next;
Bram Moolenaarca003e12006-03-17 23:19:38 +00001891 if (uhp == NULL || uhp->uh_walk != mark
1892 || (uhp->uh_seq == target && !above))
Bram Moolenaar1e607892006-03-13 22:15:53 +00001893 break;
1894 curbuf->b_u_curhead = uhp;
Bram Moolenaarca003e12006-03-17 23:19:38 +00001895 u_undoredo(TRUE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00001896 uhp->uh_walk = nomark; /* don't go back down here */
Bram Moolenaar1e607892006-03-13 22:15:53 +00001897 }
1898
1899 /*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001900 * And now go down the tree (redo), branching off where needed.
Bram Moolenaar1e607892006-03-13 22:15:53 +00001901 */
1902 uhp = curbuf->b_u_curhead;
Bram Moolenaarca003e12006-03-17 23:19:38 +00001903 while (uhp != NULL)
Bram Moolenaar1e607892006-03-13 22:15:53 +00001904 {
Bram Moolenaar89ed3df2007-01-09 19:23:12 +00001905 /* Go back to the first branch with a mark. */
1906 while (uhp->uh_alt_prev != NULL
1907 && uhp->uh_alt_prev->uh_walk == mark)
1908 uhp = uhp->uh_alt_prev;
1909
Bram Moolenaar1e607892006-03-13 22:15:53 +00001910 /* Find the last branch with a mark, that's the one. */
1911 last = uhp;
1912 while (last->uh_alt_next != NULL
1913 && last->uh_alt_next->uh_walk == mark)
1914 last = last->uh_alt_next;
1915 if (last != uhp)
1916 {
1917 /* Make the used branch the first entry in the list of
1918 * alternatives to make "u" and CTRL-R take this branch. */
Bram Moolenaar89ed3df2007-01-09 19:23:12 +00001919 while (uhp->uh_alt_prev != NULL)
1920 uhp = uhp->uh_alt_prev;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001921 if (last->uh_alt_next != NULL)
1922 last->uh_alt_next->uh_alt_prev = last->uh_alt_prev;
1923 last->uh_alt_prev->uh_alt_next = last->uh_alt_next;
1924 last->uh_alt_prev = NULL;
1925 last->uh_alt_next = uhp;
1926 uhp->uh_alt_prev = last;
1927
1928 uhp = last;
Bram Moolenaarca003e12006-03-17 23:19:38 +00001929 if (uhp->uh_next != NULL)
1930 uhp->uh_next->uh_prev = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001931 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001932 curbuf->b_u_curhead = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001933
1934 if (uhp->uh_walk != mark)
1935 break; /* must have reached the target */
1936
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001937 /* Stop when going backwards in time and didn't find the exact
1938 * header we were looking for. */
1939 if (uhp->uh_seq == target && above)
Bram Moolenaardb552d602006-03-23 22:59:57 +00001940 {
1941 curbuf->b_u_seq_cur = target - 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001942 break;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001943 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001944
Bram Moolenaarca003e12006-03-17 23:19:38 +00001945 u_undoredo(FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00001946
1947 /* Advance "curhead" to below the header we last used. If it
1948 * becomes NULL then we need to set "newhead" to this leaf. */
1949 if (uhp->uh_prev == NULL)
1950 curbuf->b_u_newhead = uhp;
1951 curbuf->b_u_curhead = uhp->uh_prev;
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001952 did_undo = FALSE;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001953
1954 if (uhp->uh_seq == target) /* found it! */
1955 break;
1956
1957 uhp = uhp->uh_prev;
1958 if (uhp == NULL || uhp->uh_walk != mark)
1959 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00001960 /* Need to redo more but can't find it... */
Bram Moolenaar1e607892006-03-13 22:15:53 +00001961 EMSG2(_(e_intern2), "undo_time()");
1962 break;
1963 }
1964 }
1965 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001966 u_undo_end(did_undo, absolute);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967}
1968
1969/*
1970 * u_undoredo: common code for undo and redo
1971 *
1972 * The lines in the file are replaced by the lines in the entry list at
1973 * curbuf->b_u_curhead. The replaced lines in the file are saved in the entry
1974 * list for the next undo/redo.
Bram Moolenaarca003e12006-03-17 23:19:38 +00001975 *
1976 * When "undo" is TRUE we go up in the tree, when FALSE we go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 */
1978 static void
Bram Moolenaarca003e12006-03-17 23:19:38 +00001979u_undoredo(undo)
1980 int undo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981{
1982 char_u **newarray = NULL;
1983 linenr_T oldsize;
1984 linenr_T newsize;
1985 linenr_T top, bot;
1986 linenr_T lnum;
1987 linenr_T newlnum = MAXLNUM;
1988 long i;
1989 u_entry_T *uep, *nuep;
1990 u_entry_T *newlist = NULL;
1991 int old_flags;
1992 int new_flags;
1993 pos_T namedm[NMARKS];
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001994#ifdef FEAT_VISUAL
1995 visualinfo_T visualinfo;
1996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 int empty_buffer; /* buffer became empty */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001998 u_header_T *curhead = curbuf->b_u_curhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002000#ifdef U_DEBUG
2001 u_check(FALSE);
2002#endif
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002003 old_flags = curhead->uh_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 new_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
2005 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
2006 setpcmark();
2007
2008 /*
2009 * save marks before undo/redo
2010 */
2011 mch_memmove(namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002012#ifdef FEAT_VISUAL
2013 visualinfo = curbuf->b_visual;
2014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 curbuf->b_op_start.lnum = curbuf->b_ml.ml_line_count;
2016 curbuf->b_op_start.col = 0;
2017 curbuf->b_op_end.lnum = 0;
2018 curbuf->b_op_end.col = 0;
2019
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002020 for (uep = curhead->uh_entry; uep != NULL; uep = nuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 {
2022 top = uep->ue_top;
2023 bot = uep->ue_bot;
2024 if (bot == 0)
2025 bot = curbuf->b_ml.ml_line_count + 1;
2026 if (top > curbuf->b_ml.ml_line_count || top >= bot
2027 || bot > curbuf->b_ml.ml_line_count + 1)
2028 {
2029 EMSG(_("E438: u_undo: line numbers wrong"));
2030 changed(); /* don't want UNCHANGED now */
2031 return;
2032 }
2033
2034 oldsize = bot - top - 1; /* number of lines before undo */
2035 newsize = uep->ue_size; /* number of lines after undo */
2036
2037 if (top < newlnum)
2038 {
2039 /* If the saved cursor is somewhere in this undo block, move it to
2040 * the remembered position. Makes "gwap" put the cursor back
2041 * where it was. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002042 lnum = curhead->uh_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 if (lnum >= top && lnum <= top + newsize + 1)
2044 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002045 curwin->w_cursor = curhead->uh_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 newlnum = curwin->w_cursor.lnum - 1;
2047 }
2048 else
2049 {
2050 /* Use the first line that actually changed. Avoids that
2051 * undoing auto-formatting puts the cursor in the previous
2052 * line. */
2053 for (i = 0; i < newsize && i < oldsize; ++i)
2054 if (STRCMP(uep->ue_array[i], ml_get(top + 1 + i)) != 0)
2055 break;
2056 if (i == newsize && newlnum == MAXLNUM && uep->ue_next == NULL)
2057 {
2058 newlnum = top;
2059 curwin->w_cursor.lnum = newlnum + 1;
2060 }
2061 else if (i < newsize)
2062 {
2063 newlnum = top + i;
2064 curwin->w_cursor.lnum = newlnum + 1;
2065 }
2066 }
2067 }
2068
2069 empty_buffer = FALSE;
2070
2071 /* delete the lines between top and bot and save them in newarray */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002072 if (oldsize > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002074 if ((newarray = (char_u **)U_ALLOC_LINE(
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002075 sizeof(char_u *) * oldsize)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 {
2077 do_outofmem_msg((long_u)(sizeof(char_u *) * oldsize));
2078 /*
2079 * We have messed up the entry list, repair is impossible.
2080 * we have to free the rest of the list.
2081 */
2082 while (uep != NULL)
2083 {
2084 nuep = uep->ue_next;
2085 u_freeentry(uep, uep->ue_size);
2086 uep = nuep;
2087 }
2088 break;
2089 }
2090 /* delete backwards, it goes faster in most cases */
2091 for (lnum = bot - 1, i = oldsize; --i >= 0; --lnum)
2092 {
2093 /* what can we do when we run out of memory? */
2094 if ((newarray[i] = u_save_line(lnum)) == NULL)
2095 do_outofmem_msg((long_u)0);
2096 /* remember we deleted the last line in the buffer, and a
2097 * dummy empty line will be inserted */
2098 if (curbuf->b_ml.ml_line_count == 1)
2099 empty_buffer = TRUE;
2100 ml_delete(lnum, FALSE);
2101 }
2102 }
Bram Moolenaar8d343302005-07-12 22:46:17 +00002103 else
2104 newarray = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105
2106 /* insert the lines in u_array between top and bot */
2107 if (newsize)
2108 {
2109 for (lnum = top, i = 0; i < newsize; ++i, ++lnum)
2110 {
2111 /*
2112 * If the file is empty, there is an empty line 1 that we
2113 * should get rid of, by replacing it with the new line
2114 */
2115 if (empty_buffer && lnum == 0)
2116 ml_replace((linenr_T)1, uep->ue_array[i], TRUE);
2117 else
2118 ml_append(lnum, uep->ue_array[i], (colnr_T)0, FALSE);
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002119 vim_free(uep->ue_array[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 }
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002121 vim_free((char_u *)uep->ue_array);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 }
2123
2124 /* adjust marks */
2125 if (oldsize != newsize)
2126 {
2127 mark_adjust(top + 1, top + oldsize, (long)MAXLNUM,
2128 (long)newsize - (long)oldsize);
2129 if (curbuf->b_op_start.lnum > top + oldsize)
2130 curbuf->b_op_start.lnum += newsize - oldsize;
2131 if (curbuf->b_op_end.lnum > top + oldsize)
2132 curbuf->b_op_end.lnum += newsize - oldsize;
2133 }
2134
2135 changed_lines(top + 1, 0, bot, newsize - oldsize);
2136
2137 /* set '[ and '] mark */
2138 if (top + 1 < curbuf->b_op_start.lnum)
2139 curbuf->b_op_start.lnum = top + 1;
2140 if (newsize == 0 && top + 1 > curbuf->b_op_end.lnum)
2141 curbuf->b_op_end.lnum = top + 1;
2142 else if (top + newsize > curbuf->b_op_end.lnum)
2143 curbuf->b_op_end.lnum = top + newsize;
2144
2145 u_newcount += newsize;
2146 u_oldcount += oldsize;
2147 uep->ue_size = oldsize;
2148 uep->ue_array = newarray;
2149 uep->ue_bot = top + newsize + 1;
2150
2151 /*
2152 * insert this entry in front of the new entry list
2153 */
2154 nuep = uep->ue_next;
2155 uep->ue_next = newlist;
2156 newlist = uep;
2157 }
2158
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002159 curhead->uh_entry = newlist;
2160 curhead->uh_flags = new_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 if ((old_flags & UH_EMPTYBUF) && bufempty())
2162 curbuf->b_ml.ml_flags |= ML_EMPTY;
2163 if (old_flags & UH_CHANGED)
2164 changed();
2165 else
Bram Moolenaar009b2592004-10-24 19:18:58 +00002166#ifdef FEAT_NETBEANS_INTG
2167 /* per netbeans undo rules, keep it as modified */
2168 if (!isNetbeansModified(curbuf))
2169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 unchanged(curbuf, FALSE);
2171
2172 /*
2173 * restore marks from before undo/redo
2174 */
2175 for (i = 0; i < NMARKS; ++i)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002176 if (curhead->uh_namedm[i].lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002178 curbuf->b_namedm[i] = curhead->uh_namedm[i];
2179 curhead->uh_namedm[i] = namedm[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002181#ifdef FEAT_VISUAL
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002182 if (curhead->uh_visual.vi_start.lnum != 0)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002183 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002184 curbuf->b_visual = curhead->uh_visual;
2185 curhead->uh_visual = visualinfo;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002186 }
2187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188
2189 /*
2190 * If the cursor is only off by one line, put it at the same position as
2191 * before starting the change (for the "o" command).
2192 * Otherwise the cursor should go to the first undone line.
2193 */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002194 if (curhead->uh_cursor.lnum + 1 == curwin->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 && curwin->w_cursor.lnum > 1)
2196 --curwin->w_cursor.lnum;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002197 if (curhead->uh_cursor.lnum == curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002199 curwin->w_cursor.col = curhead->uh_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002201 if (virtual_active() && curhead->uh_cursor_vcol >= 0)
2202 coladvance((colnr_T)curhead->uh_cursor_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 else
2204 curwin->w_cursor.coladd = 0;
2205#endif
2206 }
2207 else if (curwin->w_cursor.lnum <= curbuf->b_ml.ml_line_count)
2208 beginline(BL_SOL | BL_FIX);
2209 else
2210 {
2211 /* We get here with the current cursor line being past the end (eg
2212 * after adding lines at the end of the file, and then undoing it).
2213 * check_cursor() will move the cursor to the last line. Move it to
2214 * the first column here. */
2215 curwin->w_cursor.col = 0;
2216#ifdef FEAT_VIRTUALEDIT
2217 curwin->w_cursor.coladd = 0;
2218#endif
2219 }
2220
2221 /* Make sure the cursor is on an existing line and column. */
2222 check_cursor();
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002223
2224 /* Remember where we are for "g-" and ":earlier 10s". */
2225 curbuf->b_u_seq_cur = curhead->uh_seq;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002226 if (undo)
2227 /* We are below the previous undo. However, to make ":earlier 1s"
2228 * work we compute this as being just above the just undone change. */
2229 --curbuf->b_u_seq_cur;
2230
2231 /* The timestamp can be the same for multiple changes, just use the one of
2232 * the undone/redone change. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002233 curbuf->b_u_seq_time = curhead->uh_time;
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002234#ifdef U_DEBUG
2235 u_check(FALSE);
2236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237}
2238
2239/*
2240 * If we deleted or added lines, report the number of less/more lines.
2241 * Otherwise, report the number of changes (this may be incorrect
2242 * in some cases, but it's better than nothing).
2243 */
2244 static void
Bram Moolenaardb552d602006-03-23 22:59:57 +00002245u_undo_end(did_undo, absolute)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002246 int did_undo; /* just did an undo */
Bram Moolenaardb552d602006-03-23 22:59:57 +00002247 int absolute; /* used ":undo N" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248{
Bram Moolenaar89d40322006-08-29 15:30:07 +00002249 char *msgstr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002250 u_header_T *uhp;
2251 char_u msgbuf[80];
Bram Moolenaar1e607892006-03-13 22:15:53 +00002252
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253#ifdef FEAT_FOLDING
2254 if ((fdo_flags & FDO_UNDO) && KeyTyped)
2255 foldOpenCursor();
2256#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +00002257
2258 if (global_busy /* no messages now, wait until global is finished */
2259 || !messaging()) /* 'lazyredraw' set, don't do messages now */
2260 return;
2261
2262 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2263 --u_newcount;
2264
2265 u_oldcount -= u_newcount;
2266 if (u_oldcount == -1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002267 msgstr = N_("more line");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002268 else if (u_oldcount < 0)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002269 msgstr = N_("more lines");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002270 else if (u_oldcount == 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002271 msgstr = N_("line less");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002272 else if (u_oldcount > 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002273 msgstr = N_("fewer lines");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002274 else
2275 {
2276 u_oldcount = u_newcount;
2277 if (u_newcount == 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002278 msgstr = N_("change");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002279 else
Bram Moolenaar89d40322006-08-29 15:30:07 +00002280 msgstr = N_("changes");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002281 }
2282
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002283 if (curbuf->b_u_curhead != NULL)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002284 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00002285 /* For ":undo N" we prefer a "after #N" message. */
2286 if (absolute && curbuf->b_u_curhead->uh_next != NULL)
2287 {
2288 uhp = curbuf->b_u_curhead->uh_next;
2289 did_undo = FALSE;
2290 }
2291 else if (did_undo)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002292 uhp = curbuf->b_u_curhead;
2293 else
2294 uhp = curbuf->b_u_curhead->uh_next;
2295 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002296 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002297 uhp = curbuf->b_u_newhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002298
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002299 if (uhp == NULL)
2300 *msgbuf = NUL;
2301 else
2302 u_add_time(msgbuf, sizeof(msgbuf), uhp->uh_time);
2303
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002304 smsg((char_u *)_("%ld %s; %s #%ld %s"),
Bram Moolenaarca003e12006-03-17 23:19:38 +00002305 u_oldcount < 0 ? -u_oldcount : u_oldcount,
Bram Moolenaar89d40322006-08-29 15:30:07 +00002306 _(msgstr),
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002307 did_undo ? _("before") : _("after"),
2308 uhp == NULL ? 0L : uhp->uh_seq,
2309 msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310}
2311
2312/*
2313 * u_sync: stop adding to the current entry list
2314 */
2315 void
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002316u_sync(force)
2317 int force; /* Also sync when no_u_sync is set. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318{
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002319 /* Skip it when already synced or syncing is disabled. */
2320 if (curbuf->b_u_synced || (!force && no_u_sync > 0))
2321 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2323 if (im_is_preediting())
2324 return; /* XIM is busy, don't break an undo sequence */
2325#endif
2326 if (p_ul < 0)
2327 curbuf->b_u_synced = TRUE; /* no entries, nothing to do */
2328 else
2329 {
2330 u_getbot(); /* compute ue_bot of previous u_save */
2331 curbuf->b_u_curhead = NULL;
2332 }
2333}
2334
2335/*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002336 * ":undolist": List the leafs of the undo tree
2337 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002338 void
2339ex_undolist(eap)
Bram Moolenaarfff2bee2010-05-15 13:56:02 +02002340 exarg_T *eap UNUSED;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002341{
2342 garray_T ga;
2343 u_header_T *uhp;
2344 int mark;
2345 int nomark;
2346 int changes = 1;
2347 int i;
2348
2349 /*
2350 * 1: walk the tree to find all leafs, put the info in "ga".
2351 * 2: sort the lines
2352 * 3: display the list
2353 */
2354 mark = ++lastmark;
2355 nomark = ++lastmark;
2356 ga_init2(&ga, (int)sizeof(char *), 20);
2357
2358 uhp = curbuf->b_u_oldhead;
2359 while (uhp != NULL)
2360 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002361 if (uhp->uh_prev == NULL && uhp->uh_walk != nomark
2362 && uhp->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002363 {
2364 if (ga_grow(&ga, 1) == FAIL)
2365 break;
2366 vim_snprintf((char *)IObuff, IOSIZE, "%6ld %7ld ",
2367 uhp->uh_seq, changes);
2368 u_add_time(IObuff + STRLEN(IObuff), IOSIZE - STRLEN(IObuff),
2369 uhp->uh_time);
2370 ((char_u **)(ga.ga_data))[ga.ga_len++] = vim_strsave(IObuff);
2371 }
2372
2373 uhp->uh_walk = mark;
2374
2375 /* go down in the tree if we haven't been there */
2376 if (uhp->uh_prev != NULL && uhp->uh_prev->uh_walk != nomark
2377 && uhp->uh_prev->uh_walk != mark)
2378 {
2379 uhp = uhp->uh_prev;
2380 ++changes;
2381 }
2382
2383 /* go to alternate branch if we haven't been there */
2384 else if (uhp->uh_alt_next != NULL
2385 && uhp->uh_alt_next->uh_walk != nomark
2386 && uhp->uh_alt_next->uh_walk != mark)
2387 uhp = uhp->uh_alt_next;
2388
2389 /* go up in the tree if we haven't been there and we are at the
2390 * start of alternate branches */
2391 else if (uhp->uh_next != NULL && uhp->uh_alt_prev == NULL
2392 && uhp->uh_next->uh_walk != nomark
2393 && uhp->uh_next->uh_walk != mark)
2394 {
2395 uhp = uhp->uh_next;
2396 --changes;
2397 }
2398
2399 else
2400 {
2401 /* need to backtrack; mark this node as done */
2402 uhp->uh_walk = nomark;
2403 if (uhp->uh_alt_prev != NULL)
2404 uhp = uhp->uh_alt_prev;
2405 else
2406 {
2407 uhp = uhp->uh_next;
2408 --changes;
2409 }
2410 }
2411 }
2412
2413 if (ga.ga_len == 0)
2414 MSG(_("Nothing to undo"));
2415 else
2416 {
2417 sort_strings((char_u **)ga.ga_data, ga.ga_len);
2418
2419 msg_start();
2420 msg_puts_attr((char_u *)_("number changes time"), hl_attr(HLF_T));
2421 for (i = 0; i < ga.ga_len && !got_int; ++i)
2422 {
2423 msg_putchar('\n');
2424 if (got_int)
2425 break;
2426 msg_puts(((char_u **)ga.ga_data)[i]);
2427 }
2428 msg_end();
2429
2430 ga_clear_strings(&ga);
2431 }
2432}
2433
2434/*
2435 * Put the timestamp of an undo header in "buf[buflen]" in a nice format.
2436 */
2437 static void
2438u_add_time(buf, buflen, tt)
2439 char_u *buf;
2440 size_t buflen;
2441 time_t tt;
2442{
2443#ifdef HAVE_STRFTIME
2444 struct tm *curtime;
2445
2446 if (time(NULL) - tt >= 100)
2447 {
2448 curtime = localtime(&tt);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00002449 (void)strftime((char *)buf, buflen, "%H:%M:%S", curtime);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002450 }
2451 else
2452#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002453 vim_snprintf((char *)buf, buflen, _("%ld seconds ago"),
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002454 (long)(time(NULL) - tt));
2455}
2456
2457/*
Bram Moolenaare224ffa2006-03-01 00:01:28 +00002458 * ":undojoin": continue adding to the last entry list
2459 */
Bram Moolenaare224ffa2006-03-01 00:01:28 +00002460 void
2461ex_undojoin(eap)
Bram Moolenaarfff2bee2010-05-15 13:56:02 +02002462 exarg_T *eap UNUSED;
Bram Moolenaare224ffa2006-03-01 00:01:28 +00002463{
Bram Moolenaare224ffa2006-03-01 00:01:28 +00002464 if (curbuf->b_u_newhead == NULL)
2465 return; /* nothing changed before */
Bram Moolenaar57657d82006-04-21 22:12:41 +00002466 if (curbuf->b_u_curhead != NULL)
2467 {
2468 EMSG(_("E790: undojoin is not allowed after undo"));
2469 return;
2470 }
2471 if (!curbuf->b_u_synced)
2472 return; /* already unsynced */
Bram Moolenaare224ffa2006-03-01 00:01:28 +00002473 if (p_ul < 0)
2474 return; /* no entries, nothing to do */
2475 else
2476 {
2477 /* Go back to the last entry */
2478 curbuf->b_u_curhead = curbuf->b_u_newhead;
2479 curbuf->b_u_synced = FALSE; /* no entries, nothing to do */
2480 }
2481}
2482
2483/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 * Called after writing the file and setting b_changed to FALSE.
2485 * Now an undo means that the buffer is modified.
2486 */
2487 void
2488u_unchanged(buf)
2489 buf_T *buf;
2490{
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002491 u_unch_branch(buf->b_u_oldhead);
2492 buf->b_did_warn = FALSE;
2493}
2494
2495 static void
2496u_unch_branch(uhp)
2497 u_header_T *uhp;
2498{
Bram Moolenaar1e607892006-03-13 22:15:53 +00002499 u_header_T *uh;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002501 for (uh = uhp; uh != NULL; uh = uh->uh_prev)
2502 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 uh->uh_flags |= UH_CHANGED;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002504 if (uh->uh_alt_next != NULL)
2505 u_unch_branch(uh->uh_alt_next); /* recursive */
2506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507}
2508
2509/*
2510 * Get pointer to last added entry.
2511 * If it's not valid, give an error message and return NULL.
2512 */
2513 static u_entry_T *
2514u_get_headentry()
2515{
2516 if (curbuf->b_u_newhead == NULL || curbuf->b_u_newhead->uh_entry == NULL)
2517 {
2518 EMSG(_("E439: undo list corrupt"));
2519 return NULL;
2520 }
2521 return curbuf->b_u_newhead->uh_entry;
2522}
2523
2524/*
2525 * u_getbot(): compute the line number of the previous u_save
2526 * It is called only when b_u_synced is FALSE.
2527 */
2528 static void
2529u_getbot()
2530{
2531 u_entry_T *uep;
2532 linenr_T extra;
2533
2534 uep = u_get_headentry(); /* check for corrupt undo list */
2535 if (uep == NULL)
2536 return;
2537
2538 uep = curbuf->b_u_newhead->uh_getbot_entry;
2539 if (uep != NULL)
2540 {
2541 /*
2542 * the new ue_bot is computed from the number of lines that has been
2543 * inserted (0 - deleted) since calling u_save. This is equal to the
2544 * old line count subtracted from the current line count.
2545 */
2546 extra = curbuf->b_ml.ml_line_count - uep->ue_lcount;
2547 uep->ue_bot = uep->ue_top + uep->ue_size + 1 + extra;
2548 if (uep->ue_bot < 1 || uep->ue_bot > curbuf->b_ml.ml_line_count)
2549 {
2550 EMSG(_("E440: undo line missing"));
2551 uep->ue_bot = uep->ue_top + 1; /* assume all lines deleted, will
2552 * get all the old lines back
2553 * without deleting the current
2554 * ones */
2555 }
2556
2557 curbuf->b_u_newhead->uh_getbot_entry = NULL;
2558 }
2559
2560 curbuf->b_u_synced = TRUE;
2561}
2562
2563/*
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002564 * Free one header "uhp" and its entry list and adjust the pointers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 */
2566 static void
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002567u_freeheader(buf, uhp, uhpp)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002568 buf_T *buf;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002569 u_header_T *uhp;
2570 u_header_T **uhpp; /* if not NULL reset when freeing this header */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571{
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002572 u_header_T *uhap;
2573
Bram Moolenaar1e607892006-03-13 22:15:53 +00002574 /* When there is an alternate redo list free that branch completely,
2575 * because we can never go there. */
2576 if (uhp->uh_alt_next != NULL)
2577 u_freebranch(buf, uhp->uh_alt_next, uhpp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578
Bram Moolenaar1e607892006-03-13 22:15:53 +00002579 if (uhp->uh_alt_prev != NULL)
2580 uhp->uh_alt_prev->uh_alt_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581
Bram Moolenaar1e607892006-03-13 22:15:53 +00002582 /* Update the links in the list to remove the header. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 if (uhp->uh_next == NULL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002584 buf->b_u_oldhead = uhp->uh_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 else
2586 uhp->uh_next->uh_prev = uhp->uh_prev;
2587
2588 if (uhp->uh_prev == NULL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002589 buf->b_u_newhead = uhp->uh_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 else
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002591 for (uhap = uhp->uh_prev; uhap != NULL; uhap = uhap->uh_alt_next)
2592 uhap->uh_next = uhp->uh_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593
Bram Moolenaar1e607892006-03-13 22:15:53 +00002594 u_freeentries(buf, uhp, uhpp);
2595}
2596
2597/*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002598 * Free an alternate branch and any following alternate branches.
Bram Moolenaar1e607892006-03-13 22:15:53 +00002599 */
2600 static void
2601u_freebranch(buf, uhp, uhpp)
2602 buf_T *buf;
2603 u_header_T *uhp;
2604 u_header_T **uhpp; /* if not NULL reset when freeing this header */
2605{
2606 u_header_T *tofree, *next;
2607
Bram Moolenaar07d06772007-11-10 21:51:15 +00002608 /* If this is the top branch we may need to use u_freeheader() to update
2609 * all the pointers. */
2610 if (uhp == buf->b_u_oldhead)
2611 {
2612 u_freeheader(buf, uhp, uhpp);
2613 return;
2614 }
2615
Bram Moolenaar1e607892006-03-13 22:15:53 +00002616 if (uhp->uh_alt_prev != NULL)
2617 uhp->uh_alt_prev->uh_alt_next = NULL;
2618
2619 next = uhp;
2620 while (next != NULL)
2621 {
2622 tofree = next;
2623 if (tofree->uh_alt_next != NULL)
2624 u_freebranch(buf, tofree->uh_alt_next, uhpp); /* recursive */
2625 next = tofree->uh_prev;
2626 u_freeentries(buf, tofree, uhpp);
2627 }
2628}
2629
2630/*
2631 * Free all the undo entries for one header and the header itself.
2632 * This means that "uhp" is invalid when returning.
2633 */
2634 static void
2635u_freeentries(buf, uhp, uhpp)
2636 buf_T *buf;
2637 u_header_T *uhp;
2638 u_header_T **uhpp; /* if not NULL reset when freeing this header */
2639{
2640 u_entry_T *uep, *nuep;
2641
2642 /* Check for pointers to the header that become invalid now. */
2643 if (buf->b_u_curhead == uhp)
2644 buf->b_u_curhead = NULL;
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002645 if (buf->b_u_newhead == uhp)
2646 buf->b_u_newhead = NULL; /* freeing the newest entry */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002647 if (uhpp != NULL && uhp == *uhpp)
2648 *uhpp = NULL;
2649
2650 for (uep = uhp->uh_entry; uep != NULL; uep = nuep)
2651 {
2652 nuep = uep->ue_next;
2653 u_freeentry(uep, uep->ue_size);
2654 }
2655
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002656#ifdef U_DEBUG
2657 uhp->uh_magic = 0;
2658#endif
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002659 vim_free((char_u *)uhp);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002660 --buf->b_u_numhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661}
2662
2663/*
2664 * free entry 'uep' and 'n' lines in uep->ue_array[]
2665 */
2666 static void
2667u_freeentry(uep, n)
2668 u_entry_T *uep;
2669 long n;
2670{
Bram Moolenaar8d343302005-07-12 22:46:17 +00002671 while (n > 0)
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002672 vim_free(uep->ue_array[--n]);
2673 vim_free((char_u *)uep->ue_array);
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002674#ifdef U_DEBUG
2675 uep->ue_magic = 0;
2676#endif
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002677 vim_free((char_u *)uep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678}
2679
2680/*
2681 * invalidate the undo buffer; called when storage has already been released
2682 */
2683 void
2684u_clearall(buf)
2685 buf_T *buf;
2686{
2687 buf->b_u_newhead = buf->b_u_oldhead = buf->b_u_curhead = NULL;
2688 buf->b_u_synced = TRUE;
2689 buf->b_u_numhead = 0;
2690 buf->b_u_line_ptr = NULL;
2691 buf->b_u_line_lnum = 0;
2692}
2693
2694/*
2695 * save the line "lnum" for the "U" command
2696 */
2697 void
2698u_saveline(lnum)
2699 linenr_T lnum;
2700{
2701 if (lnum == curbuf->b_u_line_lnum) /* line is already saved */
2702 return;
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002703 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) /* should never happen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 return;
2705 u_clearline();
2706 curbuf->b_u_line_lnum = lnum;
2707 if (curwin->w_cursor.lnum == lnum)
2708 curbuf->b_u_line_colnr = curwin->w_cursor.col;
2709 else
2710 curbuf->b_u_line_colnr = 0;
2711 if ((curbuf->b_u_line_ptr = u_save_line(lnum)) == NULL)
2712 do_outofmem_msg((long_u)0);
2713}
2714
2715/*
2716 * clear the line saved for the "U" command
2717 * (this is used externally for crossing a line while in insert mode)
2718 */
2719 void
2720u_clearline()
2721{
2722 if (curbuf->b_u_line_ptr != NULL)
2723 {
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002724 vim_free(curbuf->b_u_line_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 curbuf->b_u_line_ptr = NULL;
2726 curbuf->b_u_line_lnum = 0;
2727 }
2728}
2729
2730/*
2731 * Implementation of the "U" command.
2732 * Differentiation from vi: "U" can be undone with the next "U".
2733 * We also allow the cursor to be in another line.
2734 */
2735 void
2736u_undoline()
2737{
2738 colnr_T t;
2739 char_u *oldp;
2740
2741 if (undo_off)
2742 return;
2743
Bram Moolenaare3300c82008-02-13 14:21:38 +00002744 if (curbuf->b_u_line_ptr == NULL
2745 || curbuf->b_u_line_lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 {
2747 beep_flush();
2748 return;
2749 }
Bram Moolenaare3300c82008-02-13 14:21:38 +00002750
2751 /* first save the line for the 'u' command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 if (u_savecommon(curbuf->b_u_line_lnum - 1,
2753 curbuf->b_u_line_lnum + 1, (linenr_T)0) == FAIL)
2754 return;
2755 oldp = u_save_line(curbuf->b_u_line_lnum);
2756 if (oldp == NULL)
2757 {
2758 do_outofmem_msg((long_u)0);
2759 return;
2760 }
2761 ml_replace(curbuf->b_u_line_lnum, curbuf->b_u_line_ptr, TRUE);
2762 changed_bytes(curbuf->b_u_line_lnum, 0);
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002763 vim_free(curbuf->b_u_line_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 curbuf->b_u_line_ptr = oldp;
2765
2766 t = curbuf->b_u_line_colnr;
2767 if (curwin->w_cursor.lnum == curbuf->b_u_line_lnum)
2768 curbuf->b_u_line_colnr = curwin->w_cursor.col;
2769 curwin->w_cursor.col = t;
2770 curwin->w_cursor.lnum = curbuf->b_u_line_lnum;
Bram Moolenaare3300c82008-02-13 14:21:38 +00002771 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772}
2773
2774/*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002775 * Free all allocated memory blocks for the buffer 'buf'.
2776 */
2777 void
2778u_blockfree(buf)
2779 buf_T *buf;
2780{
Bram Moolenaar1e607892006-03-13 22:15:53 +00002781 while (buf->b_u_oldhead != NULL)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002782 u_freeheader(buf, buf->b_u_oldhead, NULL);
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002783 vim_free(buf->b_u_line_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784}
2785
2786/*
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002787 * u_save_line(): allocate memory and copy line 'lnum' into it.
2788 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 */
2790 static char_u *
2791u_save_line(lnum)
2792 linenr_T lnum;
2793{
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002794 return vim_strsave(ml_get(lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795}
2796
2797/*
2798 * Check if the 'modified' flag is set, or 'ff' has changed (only need to
2799 * check the first character, because it can only be "dos", "unix" or "mac").
2800 * "nofile" and "scratch" type buffers are considered to always be unchanged.
2801 */
2802 int
2803bufIsChanged(buf)
2804 buf_T *buf;
2805{
2806 return
2807#ifdef FEAT_QUICKFIX
2808 !bt_dontwrite(buf) &&
2809#endif
2810 (buf->b_changed || file_ff_differs(buf));
2811}
2812
2813 int
2814curbufIsChanged()
2815{
2816 return
2817#ifdef FEAT_QUICKFIX
2818 !bt_dontwrite(curbuf) &&
2819#endif
2820 (curbuf->b_changed || file_ff_differs(curbuf));
2821}