blob: c16f2afa7559a9548c0e2b902839ef5d1ee449a3 [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 Moolenaar071d4272004-06-13 20:20:40 +000084#include "vim.h"
85
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +010086static long get_undolevel __ARGS((void));
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +000087static void u_unch_branch __ARGS((u_header_T *uhp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000088static u_entry_T *u_get_headentry __ARGS((void));
89static void u_getbot __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000090static void u_doit __ARGS((int count));
Bram Moolenaarca003e12006-03-17 23:19:38 +000091static void u_undoredo __ARGS((int undo));
Bram Moolenaardb552d602006-03-23 22:59:57 +000092static void u_undo_end __ARGS((int did_undo, int absolute));
Bram Moolenaarefd2bf12006-03-16 21:41:35 +000093static void u_add_time __ARGS((char_u *buf, size_t buflen, time_t tt));
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +000094static void u_freeheader __ARGS((buf_T *buf, u_header_T *uhp, u_header_T **uhpp));
Bram Moolenaar1e607892006-03-13 22:15:53 +000095static void u_freebranch __ARGS((buf_T *buf, u_header_T *uhp, u_header_T **uhpp));
96static void u_freeentries __ARGS((buf_T *buf, u_header_T *uhp, u_header_T **uhpp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000097static void u_freeentry __ARGS((u_entry_T *, long));
Bram Moolenaar55debbe2010-05-23 23:34:36 +020098#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020099static void corruption_error __ARGS((char *mesg, char_u *file_name));
Bram Moolenaar6a18eb62010-05-26 21:21:00 +0200100static void u_free_uhp __ARGS((u_header_T *uhp));
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200101static size_t fwrite_crypt __ARGS((buf_T *buf UNUSED, char_u *ptr, size_t len, FILE *fp));
102static char_u *read_string_decrypt __ARGS((buf_T *buf UNUSED, FILE *fd, int len));
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +0200103static int serialize_header __ARGS((FILE *fp, buf_T *buf, char_u *hash));
Bram Moolenaara3ff49f2010-05-30 22:48:02 +0200104static int serialize_uhp __ARGS((FILE *fp, buf_T *buf, u_header_T *uhp));
Bram Moolenaar69154f22010-07-18 21:42:34 +0200105static u_header_T *unserialize_uhp __ARGS((FILE *fp, char_u *file_name));
Bram Moolenaara3ff49f2010-05-30 22:48:02 +0200106static int serialize_uep __ARGS((FILE *fp, buf_T *buf, u_entry_T *uep));
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
Bram Moolenaar730cde92010-06-27 05:18:54 +0200118/* used in undo_end() to report number of added and deleted lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119static long u_newcount, u_oldcount;
120
121/*
122 * When 'u' flag included in 'cpoptions', we behave like vi. Need to remember
123 * the action that "u" should do.
124 */
125static int undo_undoes = FALSE;
126
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200127static int lastmark = 0;
128
Bram Moolenaar9db58062010-05-29 20:33:07 +0200129#if defined(U_DEBUG) || defined(PROTO)
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000130/*
131 * Check the undo structures for being valid. Print a warning when something
132 * looks wrong.
133 */
134static int seen_b_u_curhead;
135static int seen_b_u_newhead;
136static int header_count;
137
138 static void
139u_check_tree(u_header_T *uhp,
140 u_header_T *exp_uh_next,
141 u_header_T *exp_uh_alt_prev)
142{
143 u_entry_T *uep;
144
145 if (uhp == NULL)
146 return;
147 ++header_count;
148 if (uhp == curbuf->b_u_curhead && ++seen_b_u_curhead > 1)
149 {
150 EMSG("b_u_curhead found twice (looping?)");
151 return;
152 }
153 if (uhp == curbuf->b_u_newhead && ++seen_b_u_newhead > 1)
154 {
155 EMSG("b_u_newhead found twice (looping?)");
156 return;
157 }
158
159 if (uhp->uh_magic != UH_MAGIC)
160 EMSG("uh_magic wrong (may be using freed memory)");
161 else
162 {
163 /* Check pointers back are correct. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200164 if (uhp->uh_next.ptr != exp_uh_next)
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000165 {
166 EMSG("uh_next wrong");
167 smsg((char_u *)"expected: 0x%x, actual: 0x%x",
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200168 exp_uh_next, uhp->uh_next.ptr);
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000169 }
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200170 if (uhp->uh_alt_prev.ptr != exp_uh_alt_prev)
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000171 {
172 EMSG("uh_alt_prev wrong");
173 smsg((char_u *)"expected: 0x%x, actual: 0x%x",
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200174 exp_uh_alt_prev, uhp->uh_alt_prev.ptr);
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000175 }
176
177 /* Check the undo tree at this header. */
178 for (uep = uhp->uh_entry; uep != NULL; uep = uep->ue_next)
179 {
180 if (uep->ue_magic != UE_MAGIC)
181 {
182 EMSG("ue_magic wrong (may be using freed memory)");
183 break;
184 }
185 }
186
187 /* Check the next alt tree. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200188 u_check_tree(uhp->uh_alt_next.ptr, uhp->uh_next.ptr, uhp);
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000189
190 /* Check the next header in this branch. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200191 u_check_tree(uhp->uh_prev.ptr, uhp, NULL);
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000192 }
193}
194
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200195 static void
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000196u_check(int newhead_may_be_NULL)
197{
198 seen_b_u_newhead = 0;
199 seen_b_u_curhead = 0;
200 header_count = 0;
201
202 u_check_tree(curbuf->b_u_oldhead, NULL, NULL);
203
204 if (seen_b_u_newhead == 0 && curbuf->b_u_oldhead != NULL
205 && !(newhead_may_be_NULL && curbuf->b_u_newhead == NULL))
206 EMSGN("b_u_newhead invalid: 0x%x", curbuf->b_u_newhead);
207 if (curbuf->b_u_curhead != NULL && seen_b_u_curhead == 0)
208 EMSGN("b_u_curhead invalid: 0x%x", curbuf->b_u_curhead);
209 if (header_count != curbuf->b_u_numhead)
210 {
211 EMSG("b_u_numhead invalid");
212 smsg((char_u *)"expected: %ld, actual: %ld",
213 (long)header_count, (long)curbuf->b_u_numhead);
214 }
215}
216#endif
217
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000219 * Save the current line for both the "u" and "U" command.
Bram Moolenaar687a29c2013-04-15 15:47:12 +0200220 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000221 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222 */
223 int
224u_save_cursor()
225{
226 return (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
227 (linenr_T)(curwin->w_cursor.lnum + 1)));
228}
229
230/*
231 * Save the lines between "top" and "bot" for both the "u" and "U" command.
232 * "top" may be 0 and bot may be curbuf->b_ml.ml_line_count + 1.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200233 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 * Returns FAIL when lines could not be saved, OK otherwise.
235 */
236 int
237u_save(top, bot)
238 linenr_T top, bot;
239{
240 if (undo_off)
241 return OK;
242
Bram Moolenaar687a29c2013-04-15 15:47:12 +0200243 if (top > curbuf->b_ml.ml_line_count
244 || top >= bot
245 || bot > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 return FALSE; /* rely on caller to do error messages */
247
248 if (top + 2 == bot)
249 u_saveline((linenr_T)(top + 1));
250
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200251 return (u_savecommon(top, bot, (linenr_T)0, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252}
253
254/*
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200255 * Save the line "lnum" (used by ":s" and "~" command).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256 * The line is replaced, so the new bottom line is lnum + 1.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200257 * Careful: may trigger autocommands that reload the buffer.
258 * Returns FAIL when lines could not be saved, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 */
260 int
261u_savesub(lnum)
262 linenr_T lnum;
263{
264 if (undo_off)
265 return OK;
266
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200267 return (u_savecommon(lnum - 1, lnum + 1, lnum + 1, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268}
269
270/*
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200271 * A new line is inserted before line "lnum" (used by :s command).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 * The line is inserted, so the new bottom line is lnum + 1.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200273 * Careful: may trigger autocommands that reload the buffer.
274 * Returns FAIL when lines could not be saved, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 */
276 int
277u_inssub(lnum)
278 linenr_T lnum;
279{
280 if (undo_off)
281 return OK;
282
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200283 return (u_savecommon(lnum - 1, lnum, lnum + 1, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284}
285
286/*
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200287 * Save the lines "lnum" - "lnum" + nlines (used by delete command).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 * The lines are deleted, so the new bottom line is lnum, unless the buffer
289 * becomes empty.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200290 * Careful: may trigger autocommands that reload the buffer.
291 * Returns FAIL when lines could not be saved, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 */
293 int
294u_savedel(lnum, nlines)
295 linenr_T lnum;
296 long nlines;
297{
298 if (undo_off)
299 return OK;
300
301 return (u_savecommon(lnum - 1, lnum + nlines,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200302 nlines == curbuf->b_ml.ml_line_count ? 2 : lnum, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303}
304
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000305/*
306 * Return TRUE when undo is allowed. Otherwise give an error message and
307 * return FALSE.
308 */
Bram Moolenaarce6ef252006-07-12 19:49:41 +0000309 int
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000310undo_allowed()
311{
312 /* Don't allow changes when 'modifiable' is off. */
313 if (!curbuf->b_p_ma)
314 {
315 EMSG(_(e_modifiable));
316 return FALSE;
317 }
318
319#ifdef HAVE_SANDBOX
320 /* In the sandbox it's not allowed to change the text. */
321 if (sandbox != 0)
322 {
323 EMSG(_(e_sandbox));
324 return FALSE;
325 }
326#endif
327
328 /* Don't allow changes in the buffer while editing the cmdline. The
329 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000330 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000331 {
332 EMSG(_(e_secure));
333 return FALSE;
334 }
335
336 return TRUE;
337}
338
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200339/*
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100340 * Get the undolevle value for the current buffer.
341 */
342 static long
343get_undolevel()
344{
345 if (curbuf->b_p_ul == NO_LOCAL_UNDOLEVEL)
346 return p_ul;
347 return curbuf->b_p_ul;
348}
349
350/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200351 * Common code for various ways to save text before a change.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200352 * "top" is the line above the first changed line.
353 * "bot" is the line below the last changed line.
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200354 * "newbot" is the new bottom line. Use zero when not known.
355 * "reload" is TRUE when saving for a buffer reload.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200356 * Careful: may trigger autocommands that reload the buffer.
357 * Returns FAIL when lines could not be saved, OK otherwise.
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200358 */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200359 int
360u_savecommon(top, bot, newbot, reload)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 linenr_T top, bot;
362 linenr_T newbot;
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200363 int reload;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364{
Bram Moolenaar1e607892006-03-13 22:15:53 +0000365 linenr_T lnum;
366 long i;
367 u_header_T *uhp;
368 u_header_T *old_curhead;
369 u_entry_T *uep;
370 u_entry_T *prev_uep;
371 long size;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200373 if (!reload)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 {
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200375 /* When making changes is not allowed return FAIL. It's a crude way
376 * to make all change commands fail. */
377 if (!undo_allowed())
Bram Moolenaar009b2592004-10-24 19:18:58 +0000378 return FAIL;
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200379
380#ifdef FEAT_NETBEANS_INTG
381 /*
382 * Netbeans defines areas that cannot be modified. Bail out here when
383 * trying to change text in a guarded area.
384 */
385 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +0000386 {
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200387 if (netbeans_is_guarded(top, bot))
388 {
389 EMSG(_(e_guarded));
390 return FAIL;
391 }
392 if (curbuf->b_p_ro)
393 {
394 EMSG(_(e_nbreadonly));
395 return FAIL;
396 }
Bram Moolenaar009b2592004-10-24 19:18:58 +0000397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398#endif
399
400#ifdef FEAT_AUTOCMD
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200401 /*
402 * Saving text for undo means we are going to make a change. Give a
403 * warning for a read-only file before making the change, so that the
404 * FileChangedRO event can replace the buffer with a read-write version
405 * (e.g., obtained from a source control system).
406 */
407 change_warning(0);
408 if (bot > curbuf->b_ml.ml_line_count + 1)
409 {
410 /* This happens when the FileChangedRO autocommand changes the
411 * file in a way it becomes shorter. */
412 EMSG(_("E834: Line count changed unexpectedly"));
413 return FAIL;
414 }
415#endif
Bram Moolenaard04b7502010-07-08 22:27:55 +0200416 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200417
418#ifdef U_DEBUG
419 u_check(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420#endif
421
422 size = bot - top - 1;
423
424 /*
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200425 * If curbuf->b_u_synced == TRUE make a new header.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 */
427 if (curbuf->b_u_synced)
428 {
429#ifdef FEAT_JUMPLIST
430 /* Need to create new entry in b_changelist. */
431 curbuf->b_new_change = TRUE;
432#endif
433
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100434 if (get_undolevel() >= 0)
Bram Moolenaar1e607892006-03-13 22:15:53 +0000435 {
436 /*
437 * Make a new header entry. Do this first so that we don't mess
438 * up the undo info when out of memory.
439 */
Bram Moolenaarf05e3b02010-05-29 15:40:47 +0200440 uhp = (u_header_T *)U_ALLOC_LINE(sizeof(u_header_T));
Bram Moolenaar1e607892006-03-13 22:15:53 +0000441 if (uhp == NULL)
442 goto nomem;
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000443#ifdef U_DEBUG
444 uhp->uh_magic = UH_MAGIC;
445#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +0000446 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000447 else
448 uhp = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000449
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 /*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000451 * If we undid more than we redid, move the entry lists before and
452 * including curbuf->b_u_curhead to an alternate branch.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 */
Bram Moolenaar1e607892006-03-13 22:15:53 +0000454 old_curhead = curbuf->b_u_curhead;
455 if (old_curhead != NULL)
456 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200457 curbuf->b_u_newhead = old_curhead->uh_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000458 curbuf->b_u_curhead = NULL;
459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460
461 /*
462 * free headers to keep the size right
463 */
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100464 while (curbuf->b_u_numhead > get_undolevel()
465 && curbuf->b_u_oldhead != NULL)
Bram Moolenaar1e607892006-03-13 22:15:53 +0000466 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000467 u_header_T *uhfree = curbuf->b_u_oldhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000468
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000469 if (uhfree == old_curhead)
470 /* Can't reconnect the branch, delete all of it. */
471 u_freebranch(curbuf, uhfree, &old_curhead);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200472 else if (uhfree->uh_alt_next.ptr == NULL)
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000473 /* There is no branch, only free one header. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000474 u_freeheader(curbuf, uhfree, &old_curhead);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000475 else
476 {
477 /* Free the oldest alternate branch as a whole. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200478 while (uhfree->uh_alt_next.ptr != NULL)
479 uhfree = uhfree->uh_alt_next.ptr;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000480 u_freebranch(curbuf, uhfree, &old_curhead);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000481 }
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000482#ifdef U_DEBUG
483 u_check(TRUE);
484#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +0000485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000487 if (uhp == NULL) /* no undo at all */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 {
Bram Moolenaar1e607892006-03-13 22:15:53 +0000489 if (old_curhead != NULL)
490 u_freebranch(curbuf, old_curhead, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 curbuf->b_u_synced = FALSE;
492 return OK;
493 }
494
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200495 uhp->uh_prev.ptr = NULL;
496 uhp->uh_next.ptr = curbuf->b_u_newhead;
497 uhp->uh_alt_next.ptr = old_curhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000498 if (old_curhead != NULL)
499 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200500 uhp->uh_alt_prev.ptr = old_curhead->uh_alt_prev.ptr;
501 if (uhp->uh_alt_prev.ptr != NULL)
502 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = uhp;
503 old_curhead->uh_alt_prev.ptr = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000504 if (curbuf->b_u_oldhead == old_curhead)
505 curbuf->b_u_oldhead = uhp;
506 }
Bram Moolenaar89ed3df2007-01-09 19:23:12 +0000507 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200508 uhp->uh_alt_prev.ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 if (curbuf->b_u_newhead != NULL)
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200510 curbuf->b_u_newhead->uh_prev.ptr = uhp;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000511
Bram Moolenaarca003e12006-03-17 23:19:38 +0000512 uhp->uh_seq = ++curbuf->b_u_seq_last;
513 curbuf->b_u_seq_cur = uhp->uh_seq;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000514 uhp->uh_time = time(NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +0200515 uhp->uh_save_nr = 0;
516 curbuf->b_u_time_cur = uhp->uh_time + 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000517
Bram Moolenaar1e607892006-03-13 22:15:53 +0000518 uhp->uh_walk = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 uhp->uh_entry = NULL;
520 uhp->uh_getbot_entry = NULL;
521 uhp->uh_cursor = curwin->w_cursor; /* save cursor pos. for undo */
522#ifdef FEAT_VIRTUALEDIT
523 if (virtual_active() && curwin->w_cursor.coladd > 0)
524 uhp->uh_cursor_vcol = getviscol();
525 else
526 uhp->uh_cursor_vcol = -1;
527#endif
528
529 /* save changed and buffer empty flag for undo */
530 uhp->uh_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
531 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
532
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000533 /* save named marks and Visual marks for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 mch_memmove(uhp->uh_namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000535#ifdef FEAT_VISUAL
536 uhp->uh_visual = curbuf->b_visual;
537#endif
538
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 curbuf->b_u_newhead = uhp;
540 if (curbuf->b_u_oldhead == NULL)
541 curbuf->b_u_oldhead = uhp;
542 ++curbuf->b_u_numhead;
543 }
544 else
545 {
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100546 if (get_undolevel() < 0) /* no undo at all */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 return OK;
548
549 /*
550 * When saving a single line, and it has been saved just before, it
551 * doesn't make sense saving it again. Saves a lot of memory when
552 * making lots of changes inside the same line.
553 * This is only possible if the previous change didn't increase or
554 * decrease the number of lines.
555 * Check the ten last changes. More doesn't make sense and takes too
556 * long.
557 */
558 if (size == 1)
559 {
560 uep = u_get_headentry();
561 prev_uep = NULL;
562 for (i = 0; i < 10; ++i)
563 {
564 if (uep == NULL)
565 break;
566
567 /* If lines have been inserted/deleted we give up.
568 * Also when the line was included in a multi-line save. */
569 if ((curbuf->b_u_newhead->uh_getbot_entry != uep
570 ? (uep->ue_top + uep->ue_size + 1
571 != (uep->ue_bot == 0
572 ? curbuf->b_ml.ml_line_count + 1
573 : uep->ue_bot))
574 : uep->ue_lcount != curbuf->b_ml.ml_line_count)
575 || (uep->ue_size > 1
576 && top >= uep->ue_top
577 && top + 2 <= uep->ue_top + uep->ue_size + 1))
578 break;
579
580 /* If it's the same line we can skip saving it again. */
581 if (uep->ue_size == 1 && uep->ue_top == top)
582 {
583 if (i > 0)
584 {
585 /* It's not the last entry: get ue_bot for the last
586 * entry now. Following deleted/inserted lines go to
587 * the re-used entry. */
588 u_getbot();
589 curbuf->b_u_synced = FALSE;
590
591 /* Move the found entry to become the last entry. The
592 * order of undo/redo doesn't matter for the entries
593 * we move it over, since they don't change the line
594 * count and don't include this line. It does matter
595 * for the found entry if the line count is changed by
596 * the executed command. */
597 prev_uep->ue_next = uep->ue_next;
598 uep->ue_next = curbuf->b_u_newhead->uh_entry;
599 curbuf->b_u_newhead->uh_entry = uep;
600 }
601
602 /* The executed command may change the line count. */
603 if (newbot != 0)
604 uep->ue_bot = newbot;
605 else if (bot > curbuf->b_ml.ml_line_count)
606 uep->ue_bot = 0;
607 else
608 {
609 uep->ue_lcount = curbuf->b_ml.ml_line_count;
610 curbuf->b_u_newhead->uh_getbot_entry = uep;
611 }
612 return OK;
613 }
614 prev_uep = uep;
615 uep = uep->ue_next;
616 }
617 }
618
619 /* find line number for ue_bot for previous u_save() */
620 u_getbot();
621 }
622
623#if !defined(UNIX) && !defined(DJGPP) && !defined(WIN32) && !defined(__EMX__)
624 /*
625 * With Amiga and MSDOS 16 bit we can't handle big undo's, because
626 * then u_alloc_line would have to allocate a block larger than 32K
627 */
628 if (size >= 8000)
629 goto nomem;
630#endif
631
632 /*
633 * add lines in front of entry list
634 */
Bram Moolenaarf05e3b02010-05-29 15:40:47 +0200635 uep = (u_entry_T *)U_ALLOC_LINE(sizeof(u_entry_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 if (uep == NULL)
637 goto nomem;
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200638 vim_memset(uep, 0, sizeof(u_entry_T));
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000639#ifdef U_DEBUG
640 uep->ue_magic = UE_MAGIC;
641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642
643 uep->ue_size = size;
644 uep->ue_top = top;
645 if (newbot != 0)
646 uep->ue_bot = newbot;
647 /*
648 * Use 0 for ue_bot if bot is below last line.
649 * Otherwise we have to compute ue_bot later.
650 */
651 else if (bot > curbuf->b_ml.ml_line_count)
652 uep->ue_bot = 0;
653 else
654 {
655 uep->ue_lcount = curbuf->b_ml.ml_line_count;
656 curbuf->b_u_newhead->uh_getbot_entry = uep;
657 }
658
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000659 if (size > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000661 if ((uep->ue_array = (char_u **)U_ALLOC_LINE(
Bram Moolenaarf05e3b02010-05-29 15:40:47 +0200662 sizeof(char_u *) * size)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 {
664 u_freeentry(uep, 0L);
665 goto nomem;
666 }
667 for (i = 0, lnum = top + 1; i < size; ++i)
668 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000669 fast_breakcheck();
670 if (got_int)
671 {
672 u_freeentry(uep, i);
673 return FAIL;
674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 if ((uep->ue_array[i] = u_save_line(lnum++)) == NULL)
676 {
677 u_freeentry(uep, i);
678 goto nomem;
679 }
680 }
681 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000682 else
683 uep->ue_array = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 uep->ue_next = curbuf->b_u_newhead->uh_entry;
685 curbuf->b_u_newhead->uh_entry = uep;
686 curbuf->b_u_synced = FALSE;
687 undo_undoes = FALSE;
688
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000689#ifdef U_DEBUG
690 u_check(FALSE);
691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 return OK;
693
694nomem:
695 msg_silent = 0; /* must display the prompt */
696 if (ask_yesno((char_u *)_("No undo possible; continue anyway"), TRUE)
697 == 'y')
698 {
699 undo_off = TRUE; /* will be reset when character typed */
700 return OK;
701 }
702 do_outofmem_msg((long_u)0);
703 return FAIL;
704}
705
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200706#if defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200707
Bram Moolenaar9db58062010-05-29 20:33:07 +0200708# define UF_START_MAGIC "Vim\237UnDo\345" /* magic at start of undofile */
709# define UF_START_MAGIC_LEN 9
710# define UF_HEADER_MAGIC 0x5fd0 /* magic at start of header */
711# define UF_HEADER_END_MAGIC 0xe7aa /* magic after last header */
712# define UF_ENTRY_MAGIC 0xf518 /* magic at start of entry */
713# define UF_ENTRY_END_MAGIC 0x3581 /* magic after last entry */
Bram Moolenaara800b422010-06-27 01:15:55 +0200714# define UF_VERSION 2 /* 2-byte undofile version number */
Bram Moolenaara800b422010-06-27 01:15:55 +0200715# define UF_VERSION_CRYPT 0x8002 /* idem, encrypted */
716
717/* extra fields for header */
718# define UF_LAST_SAVE_NR 1
719
720/* extra fields for uhp */
721# define UHP_SAVE_NR 1
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200722
Bram Moolenaarcdf04202010-05-29 15:11:47 +0200723static char_u e_not_open[] = N_("E828: Cannot open undo file for writing: %s");
Bram Moolenaarcdf04202010-05-29 15:11:47 +0200724
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200725/*
726 * Compute the hash for the current buffer text into hash[UNDO_HASH_SIZE].
727 */
728 void
729u_compute_hash(hash)
730 char_u *hash;
731{
732 context_sha256_T ctx;
733 linenr_T lnum;
734 char_u *p;
735
736 sha256_start(&ctx);
Bram Moolenaarae7ba982011-12-08 15:14:09 +0100737 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200738 {
739 p = ml_get(lnum);
Bram Moolenaar442b4222010-05-24 21:34:22 +0200740 sha256_update(&ctx, p, (UINT32_T)(STRLEN(p) + 1));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200741 }
742 sha256_finish(&ctx, hash);
743}
744
745/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200746 * Return an allocated string of the full path of the target undofile.
747 * When "reading" is TRUE find the file to read, go over all directories in
748 * 'undodir'.
749 * When "reading" is FALSE use the first name where the directory exists.
Bram Moolenaar9db58062010-05-29 20:33:07 +0200750 * Returns NULL when there is no place to write or no file to read.
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200751 */
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200752 char_u *
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200753u_get_undo_file_name(buf_ffname, reading)
754 char_u *buf_ffname;
755 int reading;
756{
757 char_u *dirp;
758 char_u dir_name[IOSIZE + 1];
759 char_u *munged_name = NULL;
760 char_u *undo_file_name = NULL;
761 int dir_len;
762 char_u *p;
763 struct stat st;
764 char_u *ffname = buf_ffname;
765#ifdef HAVE_READLINK
766 char_u fname_buf[MAXPATHL];
767#endif
768
769 if (ffname == NULL)
770 return NULL;
771
772#ifdef HAVE_READLINK
773 /* Expand symlink in the file name, so that we put the undo file with the
774 * actual file instead of with the symlink. */
775 if (resolve_symlink(ffname, fname_buf) == OK)
776 ffname = fname_buf;
777#endif
778
779 /* Loop over 'undodir'. When reading find the first file that exists.
780 * When not reading use the first directory that exists or ".". */
781 dirp = p_udir;
782 while (*dirp != NUL)
783 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200784 dir_len = copy_option_part(&dirp, dir_name, IOSIZE, ",");
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200785 if (dir_len == 1 && dir_name[0] == '.')
786 {
787 /* Use same directory as the ffname,
788 * "dir/name" -> "dir/.name.un~" */
Bram Moolenaar442b4222010-05-24 21:34:22 +0200789 undo_file_name = vim_strnsave(ffname, (int)(STRLEN(ffname) + 5));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200790 if (undo_file_name == NULL)
791 break;
792 p = gettail(undo_file_name);
793 mch_memmove(p + 1, p, STRLEN(p) + 1);
794 *p = '.';
795 STRCAT(p, ".un~");
796 }
797 else
798 {
799 dir_name[dir_len] = NUL;
800 if (mch_isdir(dir_name))
801 {
802 if (munged_name == NULL)
803 {
804 munged_name = vim_strsave(ffname);
805 if (munged_name == NULL)
806 return NULL;
807 for (p = munged_name; *p != NUL; mb_ptr_adv(p))
808 if (vim_ispathsep(*p))
809 *p = '%';
810 }
811 undo_file_name = concat_fnames(dir_name, munged_name, TRUE);
812 }
813 }
814
815 /* When reading check if the file exists. */
816 if (undo_file_name != NULL && (!reading
817 || mch_stat((char *)undo_file_name, &st) >= 0))
818 break;
819 vim_free(undo_file_name);
820 undo_file_name = NULL;
821 }
822
823 vim_free(munged_name);
824 return undo_file_name;
825}
826
Bram Moolenaar9db58062010-05-29 20:33:07 +0200827 static void
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200828corruption_error(mesg, file_name)
829 char *mesg;
Bram Moolenaar9db58062010-05-29 20:33:07 +0200830 char_u *file_name;
831{
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200832 EMSG3(_("E825: Corrupted undo file (%s): %s"), mesg, file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +0200833}
834
835 static void
836u_free_uhp(uhp)
837 u_header_T *uhp;
838{
839 u_entry_T *nuep;
840 u_entry_T *uep;
841
842 uep = uhp->uh_entry;
843 while (uep != NULL)
844 {
845 nuep = uep->ue_next;
846 u_freeentry(uep, uep->ue_size);
847 uep = nuep;
848 }
849 vim_free(uhp);
850}
851
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200852/*
853 * Like fwrite() but crypt the bytes when 'key' is set.
854 * Returns 1 if successful.
855 */
856 static size_t
857fwrite_crypt(buf, ptr, len, fp)
858 buf_T *buf UNUSED;
859 char_u *ptr;
860 size_t len;
861 FILE *fp;
862{
863#ifdef FEAT_CRYPT
864 char_u *copy;
865 char_u small_buf[100];
866 size_t i;
867
868 if (*buf->b_p_key == NUL)
869 return fwrite(ptr, len, (size_t)1, fp);
870 if (len < 100)
871 copy = small_buf; /* no malloc()/free() for short strings */
872 else
873 {
874 copy = lalloc(len, FALSE);
875 if (copy == NULL)
876 return 0;
877 }
878 crypt_encode(ptr, len, copy);
879 i = fwrite(copy, len, (size_t)1, fp);
880 if (copy != small_buf)
881 vim_free(copy);
882 return i;
883#else
884 return fwrite(ptr, len, (size_t)1, fp);
885#endif
886}
887
888/*
889 * Read a string of length "len" from "fd".
890 * When 'key' is set decrypt the bytes.
891 */
892 static char_u *
893read_string_decrypt(buf, fd, len)
894 buf_T *buf UNUSED;
895 FILE *fd;
896 int len;
897{
898 char_u *ptr;
899
900 ptr = read_string(fd, len);
901#ifdef FEAT_CRYPT
Bram Moolenaarfa0ff9a2010-07-25 16:05:19 +0200902 if (ptr != NULL && *buf->b_p_key != NUL)
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200903 crypt_decode(ptr, len);
904#endif
905 return ptr;
906}
907
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +0200908 static int
909serialize_header(fp, buf, hash)
910 FILE *fp;
911 buf_T *buf;
912 char_u *hash;
913{
914 int len;
915
916 /* Start writing, first the magic marker and undo info version. */
917 if (fwrite(UF_START_MAGIC, (size_t)UF_START_MAGIC_LEN, (size_t)1, fp) != 1)
918 return FAIL;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +0200919
920 /* If the buffer is encrypted then all text bytes following will be
921 * encrypted. Numbers and other info is not crypted. */
922#ifdef FEAT_CRYPT
Bram Moolenaarfa0ff9a2010-07-25 16:05:19 +0200923 if (*buf->b_p_key != NUL)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +0200924 {
925 char_u *header;
926 int header_len;
927
928 put_bytes(fp, (long_u)UF_VERSION_CRYPT, 2);
929 header = prepare_crypt_write(buf, &header_len);
930 if (header == NULL)
931 return FAIL;
Bram Moolenaarbbd6afe2010-06-02 20:32:23 +0200932 len = (int)fwrite(header, (size_t)header_len, (size_t)1, fp);
Bram Moolenaara3ff49f2010-05-30 22:48:02 +0200933 vim_free(header);
934 if (len != 1)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200935 {
936 crypt_pop_state();
Bram Moolenaara3ff49f2010-05-30 22:48:02 +0200937 return FAIL;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200938 }
Bram Moolenaara3ff49f2010-05-30 22:48:02 +0200939 }
940 else
941#endif
942 put_bytes(fp, (long_u)UF_VERSION, 2);
943
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +0200944
945 /* Write a hash of the buffer text, so that we can verify it is still the
946 * same when reading the buffer text. */
947 if (fwrite(hash, (size_t)UNDO_HASH_SIZE, (size_t)1, fp) != 1)
948 return FAIL;
949
950 /* buffer-specific data */
951 put_bytes(fp, (long_u)buf->b_ml.ml_line_count, 4);
952 len = buf->b_u_line_ptr != NULL ? (int)STRLEN(buf->b_u_line_ptr) : 0;
953 put_bytes(fp, (long_u)len, 4);
Bram Moolenaara3ff49f2010-05-30 22:48:02 +0200954 if (len > 0 && fwrite_crypt(buf, buf->b_u_line_ptr, (size_t)len, fp) != 1)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +0200955 return FAIL;
956 put_bytes(fp, (long_u)buf->b_u_line_lnum, 4);
957 put_bytes(fp, (long_u)buf->b_u_line_colnr, 4);
958
959 /* Undo structures header data */
960 put_header_ptr(fp, buf->b_u_oldhead);
961 put_header_ptr(fp, buf->b_u_newhead);
962 put_header_ptr(fp, buf->b_u_curhead);
963
964 put_bytes(fp, (long_u)buf->b_u_numhead, 4);
965 put_bytes(fp, (long_u)buf->b_u_seq_last, 4);
966 put_bytes(fp, (long_u)buf->b_u_seq_cur, 4);
Bram Moolenaara800b422010-06-27 01:15:55 +0200967 put_time(fp, buf->b_u_time_cur);
968
969 /* Optional fields. */
970 putc(4, fp);
971 putc(UF_LAST_SAVE_NR, fp);
Bram Moolenaar730cde92010-06-27 05:18:54 +0200972 put_bytes(fp, (long_u)buf->b_u_save_nr_last, 4);
Bram Moolenaara800b422010-06-27 01:15:55 +0200973
974 putc(0, fp); /* end marker */
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +0200975
976 return OK;
977}
978
979 static int
Bram Moolenaara3ff49f2010-05-30 22:48:02 +0200980serialize_uhp(fp, buf, uhp)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +0200981 FILE *fp;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +0200982 buf_T *buf;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +0200983 u_header_T *uhp;
984{
985 int i;
986 u_entry_T *uep;
987
988 if (put_bytes(fp, (long_u)UF_HEADER_MAGIC, 2) == FAIL)
989 return FAIL;
990
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200991 put_header_ptr(fp, uhp->uh_next.ptr);
992 put_header_ptr(fp, uhp->uh_prev.ptr);
993 put_header_ptr(fp, uhp->uh_alt_next.ptr);
994 put_header_ptr(fp, uhp->uh_alt_prev.ptr);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +0200995 put_bytes(fp, uhp->uh_seq, 4);
996 serialize_pos(uhp->uh_cursor, fp);
997#ifdef FEAT_VIRTUALEDIT
998 put_bytes(fp, (long_u)uhp->uh_cursor_vcol, 4);
999#else
1000 put_bytes(fp, (long_u)0, 4);
1001#endif
1002 put_bytes(fp, (long_u)uhp->uh_flags, 2);
1003 /* Assume NMARKS will stay the same. */
1004 for (i = 0; i < NMARKS; ++i)
1005 serialize_pos(uhp->uh_namedm[i], fp);
1006#ifdef FEAT_VISUAL
1007 serialize_visualinfo(&uhp->uh_visual, fp);
1008#else
1009 {
1010 visualinfo_T info;
1011
1012 memset(&info, 0, sizeof(visualinfo_T));
1013 serialize_visualinfo(&info, fp);
1014 }
1015#endif
1016 put_time(fp, uhp->uh_time);
1017
Bram Moolenaara800b422010-06-27 01:15:55 +02001018 /* Optional fields. */
1019 putc(4, fp);
1020 putc(UHP_SAVE_NR, fp);
1021 put_bytes(fp, (long_u)uhp->uh_save_nr, 4);
1022
1023 putc(0, fp); /* end marker */
1024
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001025 /* Write all the entries. */
1026 for (uep = uhp->uh_entry; uep != NULL; uep = uep->ue_next)
1027 {
1028 put_bytes(fp, (long_u)UF_ENTRY_MAGIC, 2);
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001029 if (serialize_uep(fp, buf, uep) == FAIL)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001030 return FAIL;
1031 }
1032 put_bytes(fp, (long_u)UF_ENTRY_END_MAGIC, 2);
1033 return OK;
1034}
1035
1036 static u_header_T *
Bram Moolenaar69154f22010-07-18 21:42:34 +02001037unserialize_uhp(fp, file_name)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001038 FILE *fp;
1039 char_u *file_name;
1040{
1041 u_header_T *uhp;
1042 int i;
1043 u_entry_T *uep, *last_uep;
1044 int c;
1045 int error;
1046
1047 uhp = (u_header_T *)U_ALLOC_LINE(sizeof(u_header_T));
1048 if (uhp == NULL)
1049 return NULL;
1050 vim_memset(uhp, 0, sizeof(u_header_T));
1051#ifdef U_DEBUG
1052 uhp->uh_magic = UH_MAGIC;
1053#endif
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001054 uhp->uh_next.seq = get4c(fp);
1055 uhp->uh_prev.seq = get4c(fp);
1056 uhp->uh_alt_next.seq = get4c(fp);
1057 uhp->uh_alt_prev.seq = get4c(fp);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001058 uhp->uh_seq = get4c(fp);
1059 if (uhp->uh_seq <= 0)
1060 {
1061 corruption_error("uh_seq", file_name);
1062 vim_free(uhp);
1063 return NULL;
1064 }
1065 unserialize_pos(&uhp->uh_cursor, fp);
1066#ifdef FEAT_VIRTUALEDIT
1067 uhp->uh_cursor_vcol = get4c(fp);
1068#else
1069 (void)get4c(fp);
1070#endif
1071 uhp->uh_flags = get2c(fp);
1072 for (i = 0; i < NMARKS; ++i)
1073 unserialize_pos(&uhp->uh_namedm[i], fp);
1074#ifdef FEAT_VISUAL
1075 unserialize_visualinfo(&uhp->uh_visual, fp);
1076#else
1077 {
1078 visualinfo_T info;
1079 unserialize_visualinfo(&info, fp);
1080 }
1081#endif
1082 uhp->uh_time = get8ctime(fp);
1083
Bram Moolenaara800b422010-06-27 01:15:55 +02001084 /* Optional fields. */
Bram Moolenaar69154f22010-07-18 21:42:34 +02001085 for (;;)
1086 {
1087 int len = getc(fp);
1088 int what;
Bram Moolenaara800b422010-06-27 01:15:55 +02001089
Bram Moolenaar69154f22010-07-18 21:42:34 +02001090 if (len == 0)
1091 break;
1092 what = getc(fp);
1093 switch (what)
1094 {
1095 case UHP_SAVE_NR:
1096 uhp->uh_save_nr = get4c(fp);
Bram Moolenaara800b422010-06-27 01:15:55 +02001097 break;
Bram Moolenaar69154f22010-07-18 21:42:34 +02001098 default:
1099 /* field not supported, skip */
1100 while (--len >= 0)
1101 (void)getc(fp);
Bram Moolenaara800b422010-06-27 01:15:55 +02001102 }
Bram Moolenaar69154f22010-07-18 21:42:34 +02001103 }
Bram Moolenaara800b422010-06-27 01:15:55 +02001104
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001105 /* Unserialize the uep list. */
1106 last_uep = NULL;
1107 while ((c = get2c(fp)) == UF_ENTRY_MAGIC)
1108 {
1109 error = FALSE;
1110 uep = unserialize_uep(fp, &error, file_name);
1111 if (last_uep == NULL)
1112 uhp->uh_entry = uep;
1113 else
1114 last_uep->ue_next = uep;
1115 last_uep = uep;
1116 if (uep == NULL || error)
1117 {
1118 u_free_uhp(uhp);
1119 return NULL;
1120 }
1121 }
1122 if (c != UF_ENTRY_END_MAGIC)
1123 {
1124 corruption_error("entry end", file_name);
1125 u_free_uhp(uhp);
1126 return NULL;
1127 }
1128
1129 return uhp;
1130}
1131
Bram Moolenaar9db58062010-05-29 20:33:07 +02001132/*
1133 * Serialize "uep" to "fp".
1134 */
1135 static int
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001136serialize_uep(fp, buf, uep)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001137 FILE *fp;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001138 buf_T *buf;
1139 u_entry_T *uep;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001140{
1141 int i;
1142 size_t len;
1143
1144 put_bytes(fp, (long_u)uep->ue_top, 4);
1145 put_bytes(fp, (long_u)uep->ue_bot, 4);
1146 put_bytes(fp, (long_u)uep->ue_lcount, 4);
1147 put_bytes(fp, (long_u)uep->ue_size, 4);
1148 for (i = 0; i < uep->ue_size; ++i)
1149 {
1150 len = STRLEN(uep->ue_array[i]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001151 if (put_bytes(fp, (long_u)len, 4) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001152 return FAIL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001153 if (len > 0 && fwrite_crypt(buf, uep->ue_array[i], len, fp) != 1)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001154 return FAIL;
1155 }
1156 return OK;
1157}
1158
1159 static u_entry_T *
1160unserialize_uep(fp, error, file_name)
1161 FILE *fp;
1162 int *error;
1163 char_u *file_name;
1164{
1165 int i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001166 u_entry_T *uep;
1167 char_u **array;
1168 char_u *line;
1169 int line_len;
1170
1171 uep = (u_entry_T *)U_ALLOC_LINE(sizeof(u_entry_T));
1172 if (uep == NULL)
1173 return NULL;
1174 vim_memset(uep, 0, sizeof(u_entry_T));
1175#ifdef U_DEBUG
1176 uep->ue_magic = UE_MAGIC;
1177#endif
1178 uep->ue_top = get4c(fp);
1179 uep->ue_bot = get4c(fp);
1180 uep->ue_lcount = get4c(fp);
1181 uep->ue_size = get4c(fp);
1182 if (uep->ue_size > 0)
1183 {
1184 array = (char_u **)U_ALLOC_LINE(sizeof(char_u *) * uep->ue_size);
1185 if (array == NULL)
1186 {
1187 *error = TRUE;
1188 return uep;
1189 }
1190 vim_memset(array, 0, sizeof(char_u *) * uep->ue_size);
1191 }
Bram Moolenaar644fdff2010-05-30 13:26:21 +02001192 else
1193 array = NULL;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001194 uep->ue_array = array;
1195
1196 for (i = 0; i < uep->ue_size; ++i)
1197 {
1198 line_len = get4c(fp);
1199 if (line_len >= 0)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001200 line = read_string_decrypt(curbuf, fp, line_len);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001201 else
1202 {
1203 line = NULL;
1204 corruption_error("line length", file_name);
1205 }
1206 if (line == NULL)
1207 {
1208 *error = TRUE;
1209 return uep;
1210 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001211 array[i] = line;
1212 }
1213 return uep;
1214}
1215
1216/*
1217 * Serialize "pos" to "fp".
1218 */
1219 static void
1220serialize_pos(pos, fp)
1221 pos_T pos;
1222 FILE *fp;
1223{
1224 put_bytes(fp, (long_u)pos.lnum, 4);
1225 put_bytes(fp, (long_u)pos.col, 4);
1226#ifdef FEAT_VIRTUALEDIT
1227 put_bytes(fp, (long_u)pos.coladd, 4);
1228#else
1229 put_bytes(fp, (long_u)0, 4);
1230#endif
1231}
1232
1233/*
1234 * Unserialize the pos_T at the current position in fp.
1235 */
1236 static void
1237unserialize_pos(pos, fp)
1238 pos_T *pos;
1239 FILE *fp;
1240{
1241 pos->lnum = get4c(fp);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001242 if (pos->lnum < 0)
1243 pos->lnum = 0;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001244 pos->col = get4c(fp);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001245 if (pos->col < 0)
1246 pos->col = 0;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001247#ifdef FEAT_VIRTUALEDIT
1248 pos->coladd = get4c(fp);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001249 if (pos->coladd < 0)
1250 pos->coladd = 0;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001251#else
1252 (void)get4c(fp);
1253#endif
1254}
1255
1256/*
1257 * Serialize "info" to "fp".
1258 */
1259 static void
1260serialize_visualinfo(info, fp)
1261 visualinfo_T *info;
1262 FILE *fp;
1263{
1264 serialize_pos(info->vi_start, fp);
1265 serialize_pos(info->vi_end, fp);
1266 put_bytes(fp, (long_u)info->vi_mode, 4);
1267 put_bytes(fp, (long_u)info->vi_curswant, 4);
1268}
1269
1270/*
1271 * Unserialize the visualinfo_T at the current position in fp.
1272 */
1273 static void
1274unserialize_visualinfo(info, fp)
1275 visualinfo_T *info;
1276 FILE *fp;
1277{
1278 unserialize_pos(&info->vi_start, fp);
1279 unserialize_pos(&info->vi_end, fp);
1280 info->vi_mode = get4c(fp);
1281 info->vi_curswant = get4c(fp);
1282}
1283
1284/*
1285 * Write the pointer to an undo header. Instead of writing the pointer itself
1286 * we use the sequence number of the header. This is converted back to
1287 * pointers when reading. */
1288 static void
1289put_header_ptr(fp, uhp)
1290 FILE *fp;
1291 u_header_T *uhp;
1292{
1293 put_bytes(fp, (long_u)(uhp != NULL ? uhp->uh_seq : 0), 4);
1294}
1295
1296/*
1297 * Write the undo tree in an undo file.
1298 * When "name" is not NULL, use it as the name of the undo file.
1299 * Otherwise use buf->b_ffname to generate the undo file name.
1300 * "buf" must never be null, buf->b_ffname is used to obtain the original file
1301 * permissions.
1302 * "forceit" is TRUE for ":wundo!", FALSE otherwise.
1303 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text.
1304 */
1305 void
1306u_write_undo(name, forceit, buf, hash)
1307 char_u *name;
1308 int forceit;
1309 buf_T *buf;
1310 char_u *hash;
1311{
1312 u_header_T *uhp;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001313 char_u *file_name;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001314 int mark;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001315#ifdef U_DEBUG
1316 int headers_written = 0;
1317#endif
1318 int fd;
1319 FILE *fp = NULL;
1320 int perm;
1321 int write_ok = FALSE;
1322#ifdef UNIX
1323 int st_old_valid = FALSE;
1324 struct stat st_old;
1325 struct stat st_new;
1326#endif
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001327#ifdef FEAT_CRYPT
1328 int do_crypt = FALSE;
1329#endif
Bram Moolenaar9db58062010-05-29 20:33:07 +02001330
1331 if (name == NULL)
1332 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001333 file_name = u_get_undo_file_name(buf->b_ffname, FALSE);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001334 if (file_name == NULL)
1335 {
1336 if (p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001337 {
1338 verbose_enter();
1339 smsg((char_u *)
1340 _("Cannot write undo file in any directory in 'undodir'"));
1341 verbose_leave();
1342 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001343 return;
1344 }
1345 }
1346 else
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001347 file_name = name;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001348
1349 /*
1350 * Decide about the permission to use for the undo file. If the buffer
1351 * has a name use the permission of the original file. Otherwise only
1352 * allow the user to access the undo file.
1353 */
1354 perm = 0600;
1355 if (buf->b_ffname != NULL)
1356 {
1357#ifdef UNIX
1358 if (mch_stat((char *)buf->b_ffname, &st_old) >= 0)
1359 {
1360 perm = st_old.st_mode;
1361 st_old_valid = TRUE;
1362 }
1363#else
1364 perm = mch_getperm(buf->b_ffname);
1365 if (perm < 0)
1366 perm = 0600;
1367#endif
1368 }
1369
1370 /* strip any s-bit */
1371 perm = perm & 0777;
1372
1373 /* If the undo file already exists, verify that it actually is an undo
1374 * file, and delete it. */
1375 if (mch_getperm(file_name) >= 0)
1376 {
1377 if (name == NULL || !forceit)
1378 {
1379 /* Check we can read it and it's an undo file. */
1380 fd = mch_open((char *)file_name, O_RDONLY|O_EXTRA, 0);
1381 if (fd < 0)
1382 {
1383 if (name != NULL || p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001384 {
1385 if (name == NULL)
1386 verbose_enter();
1387 smsg((char_u *)
1388 _("Will not overwrite with undo file, cannot read: %s"),
Bram Moolenaar9db58062010-05-29 20:33:07 +02001389 file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001390 if (name == NULL)
1391 verbose_leave();
1392 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001393 goto theend;
1394 }
1395 else
1396 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02001397 char_u mbuf[UF_START_MAGIC_LEN];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001398 int len;
1399
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01001400 len = read_eintr(fd, mbuf, UF_START_MAGIC_LEN);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001401 close(fd);
1402 if (len < UF_START_MAGIC_LEN
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02001403 || memcmp(mbuf, UF_START_MAGIC, UF_START_MAGIC_LEN) != 0)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001404 {
1405 if (name != NULL || p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001406 {
1407 if (name == NULL)
1408 verbose_enter();
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001409 smsg((char_u *)
1410 _("Will not overwrite, this is not an undo file: %s"),
Bram Moolenaar9db58062010-05-29 20:33:07 +02001411 file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001412 if (name == NULL)
1413 verbose_leave();
1414 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001415 goto theend;
1416 }
1417 }
1418 }
1419 mch_remove(file_name);
1420 }
1421
Bram Moolenaar504a8212010-05-30 17:17:42 +02001422 /* If there is no undo information at all, quit here after deleting any
1423 * existing undo file. */
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001424 if (buf->b_u_numhead == 0 && buf->b_u_line_ptr == NULL)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001425 {
1426 if (p_verbose > 0)
Bram Moolenaar97ea5112010-06-12 06:46:44 +02001427 verb_msg((char_u *)_("Skipping undo file write, nothing to undo"));
Bram Moolenaar504a8212010-05-30 17:17:42 +02001428 goto theend;
1429 }
1430
Bram Moolenaar9db58062010-05-29 20:33:07 +02001431 fd = mch_open((char *)file_name,
1432 O_CREAT|O_EXTRA|O_WRONLY|O_EXCL|O_NOFOLLOW, perm);
1433 if (fd < 0)
1434 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001435 EMSG2(_(e_not_open), file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001436 goto theend;
1437 }
1438 (void)mch_setperm(file_name, perm);
1439 if (p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001440 {
1441 verbose_enter();
Bram Moolenaar9db58062010-05-29 20:33:07 +02001442 smsg((char_u *)_("Writing undo file: %s"), file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001443 verbose_leave();
1444 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001445
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001446#ifdef U_DEBUG
Bram Moolenaar504a8212010-05-30 17:17:42 +02001447 /* Check there is no problem in undo info before writing. */
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001448 u_check(FALSE);
1449#endif
1450
Bram Moolenaar9db58062010-05-29 20:33:07 +02001451#ifdef UNIX
1452 /*
1453 * Try to set the group of the undo file same as the original file. If
1454 * this fails, set the protection bits for the group same as the
1455 * protection bits for others.
1456 */
Bram Moolenaarce69e822010-07-21 20:31:07 +02001457 if (st_old_valid
1458 && mch_stat((char *)file_name, &st_new) >= 0
1459 && st_new.st_gid != st_old.st_gid
Bram Moolenaar9db58062010-05-29 20:33:07 +02001460# ifdef HAVE_FCHOWN /* sequent-ptx lacks fchown() */
Bram Moolenaarce69e822010-07-21 20:31:07 +02001461 && fchown(fd, (uid_t)-1, st_old.st_gid) != 0
Bram Moolenaar9db58062010-05-29 20:33:07 +02001462# endif
1463 )
1464 mch_setperm(file_name, (perm & 0707) | ((perm & 07) << 3));
1465# ifdef HAVE_SELINUX
1466 if (buf->b_ffname != NULL)
1467 mch_copy_sec(buf->b_ffname, file_name);
1468# endif
1469#endif
1470
1471 fp = fdopen(fd, "w");
1472 if (fp == NULL)
1473 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001474 EMSG2(_(e_not_open), file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001475 close(fd);
1476 mch_remove(file_name);
1477 goto theend;
1478 }
1479
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001480 /* Undo must be synced. */
1481 u_sync(TRUE);
1482
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001483 /*
1484 * Write the header.
1485 */
1486 if (serialize_header(fp, buf, hash) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001487 goto write_error;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001488#ifdef FEAT_CRYPT
Bram Moolenaarfa0ff9a2010-07-25 16:05:19 +02001489 if (*buf->b_p_key != NUL)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001490 do_crypt = TRUE;
1491#endif
Bram Moolenaar9db58062010-05-29 20:33:07 +02001492
1493 /*
1494 * Iteratively serialize UHPs and their UEPs from the top down.
1495 */
1496 mark = ++lastmark;
1497 uhp = buf->b_u_oldhead;
1498 while (uhp != NULL)
1499 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001500 /* Serialize current UHP if we haven't seen it */
1501 if (uhp->uh_walk != mark)
1502 {
1503 uhp->uh_walk = mark;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001504#ifdef U_DEBUG
1505 ++headers_written;
1506#endif
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001507 if (serialize_uhp(fp, buf, uhp) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001508 goto write_error;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001509 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001510
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001511 /* Now walk through the tree - algorithm from undo_time(). */
1512 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != mark)
1513 uhp = uhp->uh_prev.ptr;
1514 else if (uhp->uh_alt_next.ptr != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001515 && uhp->uh_alt_next.ptr->uh_walk != mark)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001516 uhp = uhp->uh_alt_next.ptr;
1517 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001518 && uhp->uh_next.ptr->uh_walk != mark)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001519 uhp = uhp->uh_next.ptr;
1520 else if (uhp->uh_alt_prev.ptr != NULL)
1521 uhp = uhp->uh_alt_prev.ptr;
1522 else
1523 uhp = uhp->uh_next.ptr;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001524 }
1525
1526 if (put_bytes(fp, (long_u)UF_HEADER_END_MAGIC, 2) == OK)
1527 write_ok = TRUE;
1528#ifdef U_DEBUG
1529 if (headers_written != buf->b_u_numhead)
Bram Moolenaar570064c2013-06-10 20:25:10 +02001530 {
1531 EMSGN("Written %ld headers, ...", headers_written);
1532 EMSGN("... but numhead is %ld", buf->b_u_numhead);
1533 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001534#endif
1535
1536write_error:
1537 fclose(fp);
1538 if (!write_ok)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001539 EMSG2(_("E829: write error in undo file: %s"), file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001540
1541#if defined(MACOS_CLASSIC) || defined(WIN3264)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001542 /* Copy file attributes; for systems where this can only be done after
1543 * closing the file. */
Bram Moolenaar9db58062010-05-29 20:33:07 +02001544 if (buf->b_ffname != NULL)
1545 (void)mch_copy_file_attribute(buf->b_ffname, file_name);
1546#endif
1547#ifdef HAVE_ACL
1548 if (buf->b_ffname != NULL)
1549 {
1550 vim_acl_T acl;
1551
1552 /* For systems that support ACL: get the ACL from the original file. */
1553 acl = mch_get_acl(buf->b_ffname);
1554 mch_set_acl(file_name, acl);
Bram Moolenaard2aed442012-06-01 13:46:12 +02001555 mch_free_acl(acl);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001556 }
1557#endif
1558
1559theend:
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001560#ifdef FEAT_CRYPT
1561 if (do_crypt)
1562 crypt_pop_state();
1563#endif
Bram Moolenaar9db58062010-05-29 20:33:07 +02001564 if (file_name != name)
1565 vim_free(file_name);
1566}
1567
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001568/*
1569 * Load the undo tree from an undo file.
1570 * If "name" is not NULL use it as the undo file name. This also means being
1571 * a bit more verbose.
1572 * Otherwise use curbuf->b_ffname to generate the undo file name.
1573 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text.
1574 */
1575 void
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001576u_read_undo(name, hash, orig_name)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001577 char_u *name;
1578 char_u *hash;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001579 char_u *orig_name;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001580{
1581 char_u *file_name;
1582 FILE *fp;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001583 long version, str_len;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001584 char_u *line_ptr = NULL;
1585 linenr_T line_lnum;
1586 colnr_T line_colnr;
1587 linenr_T line_count;
Bram Moolenaar442b4222010-05-24 21:34:22 +02001588 int num_head = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001589 long old_header_seq, new_header_seq, cur_header_seq;
1590 long seq_last, seq_cur;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001591 long last_save_nr = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001592 short old_idx = -1, new_idx = -1, cur_idx = -1;
1593 long num_read_uhps = 0;
1594 time_t seq_time;
1595 int i, j;
1596 int c;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001597 u_header_T *uhp;
1598 u_header_T **uhp_table = NULL;
1599 char_u read_hash[UNDO_HASH_SIZE];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001600 char_u magic_buf[UF_START_MAGIC_LEN];
1601#ifdef U_DEBUG
1602 int *uhp_table_used;
1603#endif
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001604#ifdef UNIX
1605 struct stat st_orig;
1606 struct stat st_undo;
1607#endif
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001608#ifdef FEAT_CRYPT
1609 int do_decrypt = FALSE;
1610#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001611
1612 if (name == NULL)
1613 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001614 file_name = u_get_undo_file_name(curbuf->b_ffname, TRUE);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001615 if (file_name == NULL)
1616 return;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001617
1618#ifdef UNIX
1619 /* For safety we only read an undo file if the owner is equal to the
Bram Moolenaar3b262392013-09-08 15:40:49 +02001620 * owner of the text file or equal to the current user. */
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001621 if (mch_stat((char *)orig_name, &st_orig) >= 0
1622 && mch_stat((char *)file_name, &st_undo) >= 0
Bram Moolenaar3b262392013-09-08 15:40:49 +02001623 && st_orig.st_uid != st_undo.st_uid
1624 && st_undo.st_uid != getuid())
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001625 {
1626 if (p_verbose > 0)
1627 {
1628 verbose_enter();
1629 smsg((char_u *)_("Not reading undo file, owner differs: %s"),
1630 file_name);
1631 verbose_leave();
1632 }
1633 return;
1634 }
1635#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001636 }
1637 else
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001638 file_name = name;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001639
1640 if (p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001641 {
1642 verbose_enter();
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001643 smsg((char_u *)_("Reading undo file: %s"), file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001644 verbose_leave();
1645 }
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001646
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001647 fp = mch_fopen((char *)file_name, "r");
1648 if (fp == NULL)
1649 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001650 if (name != NULL || p_verbose > 0)
1651 EMSG2(_("E822: Cannot open undo file for reading: %s"), file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001652 goto error;
1653 }
1654
Bram Moolenaar9db58062010-05-29 20:33:07 +02001655 /*
1656 * Read the undo file header.
1657 */
1658 if (fread(magic_buf, UF_START_MAGIC_LEN, 1, fp) != 1
1659 || memcmp(magic_buf, UF_START_MAGIC, UF_START_MAGIC_LEN) != 0)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001660 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001661 EMSG2(_("E823: Not an undo file: %s"), file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001662 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001663 }
1664 version = get2c(fp);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001665 if (version == UF_VERSION_CRYPT)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001666 {
1667#ifdef FEAT_CRYPT
Bram Moolenaar56be9502010-06-06 14:20:26 +02001668 if (*curbuf->b_p_key == NUL)
1669 {
1670 EMSG2(_("E832: Non-encrypted file has encrypted undo file: %s"),
1671 file_name);
1672 goto error;
1673 }
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001674 if (prepare_crypt_read(fp) == FAIL)
1675 {
1676 EMSG2(_("E826: Undo file decryption failed: %s"), file_name);
1677 goto error;
1678 }
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001679 do_decrypt = TRUE;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001680#else
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001681 EMSG2(_("E827: Undo file is encrypted: %s"), file_name);
1682 goto error;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001683#endif
1684 }
Bram Moolenaar69154f22010-07-18 21:42:34 +02001685 else if (version != UF_VERSION)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001686 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001687 EMSG2(_("E824: Incompatible undo file: %s"), file_name);
1688 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001689 }
1690
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001691 if (fread(read_hash, UNDO_HASH_SIZE, 1, fp) != 1)
1692 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001693 corruption_error("hash", file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001694 goto error;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001695 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001696 line_count = (linenr_T)get4c(fp);
1697 if (memcmp(hash, read_hash, UNDO_HASH_SIZE) != 0
1698 || line_count != curbuf->b_ml.ml_line_count)
1699 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001700 if (p_verbose > 0 || name != NULL)
1701 {
Bram Moolenaar504a8212010-05-30 17:17:42 +02001702 if (name == NULL)
1703 verbose_enter();
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001704 give_warning((char_u *)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001705 _("File contents changed, cannot use undo info"), TRUE);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001706 if (name == NULL)
1707 verbose_leave();
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001708 }
1709 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001710 }
1711
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001712 /* Read undo data for "U" command. */
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001713 str_len = get4c(fp);
1714 if (str_len < 0)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001715 goto error;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001716 if (str_len > 0)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001717 line_ptr = read_string_decrypt(curbuf, fp, str_len);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001718 line_lnum = (linenr_T)get4c(fp);
1719 line_colnr = (colnr_T)get4c(fp);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001720 if (line_lnum < 0 || line_colnr < 0)
1721 {
1722 corruption_error("line lnum/col", file_name);
1723 goto error;
1724 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001725
1726 /* Begin general undo data */
1727 old_header_seq = get4c(fp);
1728 new_header_seq = get4c(fp);
1729 cur_header_seq = get4c(fp);
1730 num_head = get4c(fp);
1731 seq_last = get4c(fp);
1732 seq_cur = get4c(fp);
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001733 seq_time = get8ctime(fp);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001734
Bram Moolenaar69154f22010-07-18 21:42:34 +02001735 /* Optional header fields. */
1736 for (;;)
1737 {
1738 int len = getc(fp);
1739 int what;
Bram Moolenaara800b422010-06-27 01:15:55 +02001740
Bram Moolenaar69154f22010-07-18 21:42:34 +02001741 if (len == 0 || len == EOF)
1742 break;
1743 what = getc(fp);
1744 switch (what)
1745 {
1746 case UF_LAST_SAVE_NR:
1747 last_save_nr = get4c(fp);
Bram Moolenaara800b422010-06-27 01:15:55 +02001748 break;
Bram Moolenaar69154f22010-07-18 21:42:34 +02001749 default:
1750 /* field not supported, skip */
1751 while (--len >= 0)
1752 (void)getc(fp);
Bram Moolenaara800b422010-06-27 01:15:55 +02001753 }
Bram Moolenaar69154f22010-07-18 21:42:34 +02001754 }
Bram Moolenaara800b422010-06-27 01:15:55 +02001755
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001756 /* uhp_table will store the freshly created undo headers we allocate
1757 * until we insert them into curbuf. The table remains sorted by the
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001758 * sequence numbers of the headers.
1759 * When there are no headers uhp_table is NULL. */
1760 if (num_head > 0)
1761 {
1762 uhp_table = (u_header_T **)U_ALLOC_LINE(
1763 num_head * sizeof(u_header_T *));
1764 if (uhp_table == NULL)
1765 goto error;
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001766 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001767
Bram Moolenaar9db58062010-05-29 20:33:07 +02001768 while ((c = get2c(fp)) == UF_HEADER_MAGIC)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001769 {
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001770 if (num_read_uhps >= num_head)
1771 {
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001772 corruption_error("num_head too small", file_name);
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001773 goto error;
1774 }
1775
Bram Moolenaar69154f22010-07-18 21:42:34 +02001776 uhp = unserialize_uhp(fp, file_name);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001777 if (uhp == NULL)
1778 goto error;
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001779 uhp_table[num_read_uhps++] = uhp;
1780 }
1781
1782 if (num_read_uhps != num_head)
1783 {
1784 corruption_error("num_head", file_name);
1785 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001786 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001787 if (c != UF_HEADER_END_MAGIC)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001788 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001789 corruption_error("end marker", file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001790 goto error;
1791 }
1792
Bram Moolenaar9db58062010-05-29 20:33:07 +02001793#ifdef U_DEBUG
1794 uhp_table_used = (int *)alloc_clear(
1795 (unsigned)(sizeof(int) * num_head + 1));
1796# define SET_FLAG(j) ++uhp_table_used[j]
1797#else
1798# define SET_FLAG(j)
1799#endif
1800
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001801 /* We have put all of the headers into a table. Now we iterate through the
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001802 * table and swizzle each sequence number we have stored in uh_*_seq into
1803 * a pointer corresponding to the header with that sequence number. */
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001804 for (i = 0; i < num_head; i++)
1805 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001806 uhp = uhp_table[i];
1807 if (uhp == NULL)
1808 continue;
1809 for (j = 0; j < num_head; j++)
1810 if (uhp_table[j] != NULL && i != j
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001811 && uhp_table[i]->uh_seq == uhp_table[j]->uh_seq)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001812 {
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001813 corruption_error("duplicate uh_seq", file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001814 goto error;
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001815 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001816 for (j = 0; j < num_head; j++)
1817 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001818 && uhp_table[j]->uh_seq == uhp->uh_next.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001819 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001820 uhp->uh_next.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001821 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001822 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001823 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001824 for (j = 0; j < num_head; j++)
1825 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001826 && uhp_table[j]->uh_seq == uhp->uh_prev.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001827 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001828 uhp->uh_prev.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001829 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001830 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001831 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001832 for (j = 0; j < num_head; j++)
1833 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001834 && uhp_table[j]->uh_seq == uhp->uh_alt_next.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001835 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001836 uhp->uh_alt_next.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001837 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001838 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001839 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001840 for (j = 0; j < num_head; j++)
1841 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001842 && uhp_table[j]->uh_seq == uhp->uh_alt_prev.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001843 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001844 uhp->uh_alt_prev.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001845 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001846 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001847 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001848 if (old_header_seq > 0 && old_idx < 0 && uhp->uh_seq == old_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001849 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001850 old_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001851 SET_FLAG(i);
1852 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001853 if (new_header_seq > 0 && new_idx < 0 && uhp->uh_seq == new_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001854 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001855 new_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001856 SET_FLAG(i);
1857 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001858 if (cur_header_seq > 0 && cur_idx < 0 && uhp->uh_seq == cur_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001859 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001860 cur_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001861 SET_FLAG(i);
1862 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001863 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001864
1865 /* Now that we have read the undo info successfully, free the current undo
1866 * info and use the info from the file. */
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001867 u_blockfree(curbuf);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001868 curbuf->b_u_oldhead = old_idx < 0 ? NULL : uhp_table[old_idx];
1869 curbuf->b_u_newhead = new_idx < 0 ? NULL : uhp_table[new_idx];
1870 curbuf->b_u_curhead = cur_idx < 0 ? NULL : uhp_table[cur_idx];
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001871 curbuf->b_u_line_ptr = line_ptr;
1872 curbuf->b_u_line_lnum = line_lnum;
1873 curbuf->b_u_line_colnr = line_colnr;
1874 curbuf->b_u_numhead = num_head;
1875 curbuf->b_u_seq_last = seq_last;
1876 curbuf->b_u_seq_cur = seq_cur;
Bram Moolenaara800b422010-06-27 01:15:55 +02001877 curbuf->b_u_time_cur = seq_time;
Bram Moolenaar730cde92010-06-27 05:18:54 +02001878 curbuf->b_u_save_nr_last = last_save_nr;
Bram Moolenaardba01a02010-11-03 19:32:42 +01001879 curbuf->b_u_save_nr_cur = last_save_nr;
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001880
1881 curbuf->b_u_synced = TRUE;
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001882 vim_free(uhp_table);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001883
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001884#ifdef U_DEBUG
Bram Moolenaar9db58062010-05-29 20:33:07 +02001885 for (i = 0; i < num_head; ++i)
1886 if (uhp_table_used[i] == 0)
1887 EMSGN("uhp_table entry %ld not used, leaking memory", i);
1888 vim_free(uhp_table_used);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001889 u_check(TRUE);
1890#endif
Bram Moolenaar9db58062010-05-29 20:33:07 +02001891
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001892 if (name != NULL)
1893 smsg((char_u *)_("Finished reading undo file %s"), file_name);
1894 goto theend;
1895
1896error:
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001897 vim_free(line_ptr);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001898 if (uhp_table != NULL)
1899 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001900 for (i = 0; i < num_read_uhps; i++)
1901 if (uhp_table[i] != NULL)
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001902 u_free_uhp(uhp_table[i]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001903 vim_free(uhp_table);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001904 }
1905
1906theend:
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001907#ifdef FEAT_CRYPT
1908 if (do_decrypt)
1909 crypt_pop_state();
1910#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001911 if (fp != NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001912 fclose(fp);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001913 if (file_name != name)
1914 vim_free(file_name);
1915 return;
1916}
1917
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001918#endif /* FEAT_PERSISTENT_UNDO */
1919
1920
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921/*
1922 * If 'cpoptions' contains 'u': Undo the previous undo or redo (vi compatible).
1923 * If 'cpoptions' does not contain 'u': Always undo.
1924 */
1925 void
1926u_undo(count)
1927 int count;
1928{
1929 /*
1930 * If we get an undo command while executing a macro, we behave like the
1931 * original vi. If this happens twice in one macro the result will not
1932 * be compatible.
1933 */
1934 if (curbuf->b_u_synced == FALSE)
1935 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001936 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 count = 1;
1938 }
1939
1940 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
1941 undo_undoes = TRUE;
1942 else
1943 undo_undoes = !undo_undoes;
1944 u_doit(count);
1945}
1946
1947/*
1948 * If 'cpoptions' contains 'u': Repeat the previous undo or redo.
1949 * If 'cpoptions' does not contain 'u': Always redo.
1950 */
1951 void
1952u_redo(count)
1953 int count;
1954{
1955 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
1956 undo_undoes = FALSE;
1957 u_doit(count);
1958}
1959
1960/*
1961 * Undo or redo, depending on 'undo_undoes', 'count' times.
1962 */
1963 static void
Bram Moolenaarca003e12006-03-17 23:19:38 +00001964u_doit(startcount)
1965 int startcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966{
Bram Moolenaarca003e12006-03-17 23:19:38 +00001967 int count = startcount;
1968
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001969 if (!undo_allowed())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971
1972 u_newcount = 0;
1973 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001974 if (curbuf->b_ml.ml_flags & ML_EMPTY)
1975 u_oldcount = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 while (count--)
1977 {
Bram Moolenaarb0b50882010-07-07 18:26:28 +02001978 /* Do the change warning now, so that it triggers FileChangedRO when
1979 * needed. This may cause the file to be reloaded, that must happen
1980 * before we do anything, because it may change curbuf->b_u_curhead
1981 * and more. */
1982 change_warning(0);
1983
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 if (undo_undoes)
1985 {
1986 if (curbuf->b_u_curhead == NULL) /* first undo */
1987 curbuf->b_u_curhead = curbuf->b_u_newhead;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001988 else if (get_undolevel() > 0) /* multi level undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 /* get next undo */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001990 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_next.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 /* nothing to undo */
1992 if (curbuf->b_u_numhead == 0 || curbuf->b_u_curhead == NULL)
1993 {
1994 /* stick curbuf->b_u_curhead at end */
1995 curbuf->b_u_curhead = curbuf->b_u_oldhead;
1996 beep_flush();
Bram Moolenaarca003e12006-03-17 23:19:38 +00001997 if (count == startcount - 1)
1998 {
1999 MSG(_("Already at oldest change"));
2000 return;
2001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 break;
2003 }
2004
Bram Moolenaarca003e12006-03-17 23:19:38 +00002005 u_undoredo(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 }
2007 else
2008 {
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002009 if (curbuf->b_u_curhead == NULL || get_undolevel() <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 {
2011 beep_flush(); /* nothing to redo */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002012 if (count == startcount - 1)
2013 {
2014 MSG(_("Already at newest change"));
2015 return;
2016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 break;
2018 }
2019
Bram Moolenaarca003e12006-03-17 23:19:38 +00002020 u_undoredo(FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002021
2022 /* Advance for next redo. Set "newhead" when at the end of the
2023 * redoable changes. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002024 if (curbuf->b_u_curhead->uh_prev.ptr == NULL)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002025 curbuf->b_u_newhead = curbuf->b_u_curhead;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002026 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_prev.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 }
2028 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00002029 u_undo_end(undo_undoes, FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002030}
2031
Bram Moolenaar1e607892006-03-13 22:15:53 +00002032/*
2033 * Undo or redo over the timeline.
2034 * When "step" is negative go back in time, otherwise goes forward in time.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002035 * When "sec" is FALSE make "step" steps, when "sec" is TRUE use "step" as
2036 * seconds.
Bram Moolenaar730cde92010-06-27 05:18:54 +02002037 * When "file" is TRUE use "step" as a number of file writes.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002038 * When "absolute" is TRUE use "step" as the sequence number to jump to.
2039 * "sec" must be FALSE then.
Bram Moolenaar1e607892006-03-13 22:15:53 +00002040 */
2041 void
Bram Moolenaar730cde92010-06-27 05:18:54 +02002042undo_time(step, sec, file, absolute)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002043 long step;
2044 int sec;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002045 int file;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002046 int absolute;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002047{
2048 long target;
2049 long closest;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002050 long closest_start;
2051 long closest_seq = 0;
2052 long val;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002053 u_header_T *uhp;
2054 u_header_T *last;
2055 int mark;
2056 int nomark;
2057 int round;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002058 int dosec = sec;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002059 int dofile = file;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002060 int above = FALSE;
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002061 int did_undo = TRUE;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002062
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002063 /* First make sure the current undoable change is synced. */
2064 if (curbuf->b_u_synced == FALSE)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002065 u_sync(TRUE);
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002066
Bram Moolenaar1e607892006-03-13 22:15:53 +00002067 u_newcount = 0;
2068 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002069 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002070 u_oldcount = -1;
2071
Bram Moolenaarca003e12006-03-17 23:19:38 +00002072 /* "target" is the node below which we want to be.
2073 * Init "closest" to a value we can't reach. */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002074 if (absolute)
2075 {
2076 target = step;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002077 closest = -1;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002078 }
Bram Moolenaarca003e12006-03-17 23:19:38 +00002079 else
Bram Moolenaar1e607892006-03-13 22:15:53 +00002080 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002081 /* When doing computations with time_t subtract starttime, because
2082 * time_t converted to a long may result in a wrong number. */
Bram Moolenaar730cde92010-06-27 05:18:54 +02002083 if (dosec)
Bram Moolenaara800b422010-06-27 01:15:55 +02002084 target = (long)(curbuf->b_u_time_cur - starttime) + step;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002085 else if (dofile)
2086 {
2087 if (step < 0)
2088 {
2089 /* Going back to a previous write. If there were changes after
2090 * the last write, count that as moving one file-write, so
2091 * that ":earlier 1f" undoes all changes since the last save. */
2092 uhp = curbuf->b_u_curhead;
2093 if (uhp != NULL)
2094 uhp = uhp->uh_next.ptr;
2095 else
2096 uhp = curbuf->b_u_newhead;
2097 if (uhp != NULL && uhp->uh_save_nr != 0)
2098 /* "uh_save_nr" was set in the last block, that means
2099 * there were no changes since the last write */
2100 target = curbuf->b_u_save_nr_cur + step;
2101 else
2102 /* count the changes since the last write as one step */
2103 target = curbuf->b_u_save_nr_cur + step + 1;
2104 if (target <= 0)
2105 /* Go to before first write: before the oldest change. Use
2106 * the sequence number for that. */
2107 dofile = FALSE;
2108 }
2109 else
2110 {
2111 /* Moving forward to a newer write. */
2112 target = curbuf->b_u_save_nr_cur + step;
2113 if (target > curbuf->b_u_save_nr_last)
2114 {
2115 /* Go to after last write: after the latest change. Use
2116 * the sequence number for that. */
2117 target = curbuf->b_u_seq_last + 1;
2118 dofile = FALSE;
2119 }
2120 }
2121 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002122 else
2123 target = curbuf->b_u_seq_cur + step;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002124 if (step < 0)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002125 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002126 if (target < 0)
2127 target = 0;
2128 closest = -1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002129 }
2130 else
2131 {
Bram Moolenaar730cde92010-06-27 05:18:54 +02002132 if (dosec)
Bram Moolenaardb552d602006-03-23 22:59:57 +00002133 closest = (long)(time(NULL) - starttime + 1);
Bram Moolenaar730cde92010-06-27 05:18:54 +02002134 else if (dofile)
2135 closest = curbuf->b_u_save_nr_last + 2;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002136 else
2137 closest = curbuf->b_u_seq_last + 2;
2138 if (target >= closest)
2139 target = closest - 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002140 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002141 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002142 closest_start = closest;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002143 closest_seq = curbuf->b_u_seq_cur;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002144
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002145 /*
2146 * May do this twice:
Bram Moolenaar1e607892006-03-13 22:15:53 +00002147 * 1. Search for "target", update "closest" to the best match found.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002148 * 2. If "target" not found search for "closest".
2149 *
2150 * When using the closest time we use the sequence number in the second
2151 * round, because there may be several entries with the same time.
2152 */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002153 for (round = 1; round <= 2; ++round)
2154 {
2155 /* Find the path from the current state to where we want to go. The
2156 * desired state can be anywhere in the undo tree, need to go all over
2157 * it. We put "nomark" in uh_walk where we have been without success,
2158 * "mark" where it could possibly be. */
2159 mark = ++lastmark;
2160 nomark = ++lastmark;
2161
2162 if (curbuf->b_u_curhead == NULL) /* at leaf of the tree */
2163 uhp = curbuf->b_u_newhead;
2164 else
2165 uhp = curbuf->b_u_curhead;
2166
2167 while (uhp != NULL)
2168 {
2169 uhp->uh_walk = mark;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002170 if (dosec)
2171 val = (long)(uhp->uh_time - starttime);
2172 else if (dofile)
2173 val = uhp->uh_save_nr;
2174 else
2175 val = uhp->uh_seq;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002176
Bram Moolenaar730cde92010-06-27 05:18:54 +02002177 if (round == 1 && !(dofile && val == 0))
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002178 {
2179 /* Remember the header that is closest to the target.
2180 * It must be at least in the right direction (checked with
Bram Moolenaarca003e12006-03-17 23:19:38 +00002181 * "b_u_seq_cur"). When the timestamp is equal find the
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002182 * highest/lowest sequence number. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002183 if ((step < 0 ? uhp->uh_seq <= curbuf->b_u_seq_cur
2184 : uhp->uh_seq > curbuf->b_u_seq_cur)
2185 && ((dosec && val == closest)
2186 ? (step < 0
2187 ? uhp->uh_seq < closest_seq
2188 : uhp->uh_seq > closest_seq)
2189 : closest == closest_start
2190 || (val > target
2191 ? (closest > target
2192 ? val - target <= closest - target
2193 : val - target <= target - closest)
2194 : (closest > target
2195 ? target - val <= closest - target
2196 : target - val <= target - closest))))
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002197 {
2198 closest = val;
2199 closest_seq = uhp->uh_seq;
2200 }
2201 }
2202
2203 /* Quit searching when we found a match. But when searching for a
2204 * time we need to continue looking for the best uh_seq. */
2205 if (target == val && !dosec)
Bram Moolenaar730cde92010-06-27 05:18:54 +02002206 {
2207 target = uhp->uh_seq;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002208 break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002209 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002210
2211 /* go down in the tree if we haven't been there */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002212 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != nomark
2213 && uhp->uh_prev.ptr->uh_walk != mark)
2214 uhp = uhp->uh_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002215
2216 /* go to alternate branch if we haven't been there */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002217 else if (uhp->uh_alt_next.ptr != NULL
2218 && uhp->uh_alt_next.ptr->uh_walk != nomark
2219 && uhp->uh_alt_next.ptr->uh_walk != mark)
2220 uhp = uhp->uh_alt_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002221
2222 /* go up in the tree if we haven't been there and we are at the
2223 * start of alternate branches */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002224 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
2225 && uhp->uh_next.ptr->uh_walk != nomark
2226 && uhp->uh_next.ptr->uh_walk != mark)
Bram Moolenaardb552d602006-03-23 22:59:57 +00002227 {
2228 /* If still at the start we don't go through this change. */
2229 if (uhp == curbuf->b_u_curhead)
2230 uhp->uh_walk = nomark;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002231 uhp = uhp->uh_next.ptr;
Bram Moolenaardb552d602006-03-23 22:59:57 +00002232 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002233
2234 else
2235 {
2236 /* need to backtrack; mark this node as useless */
2237 uhp->uh_walk = nomark;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002238 if (uhp->uh_alt_prev.ptr != NULL)
2239 uhp = uhp->uh_alt_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002240 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002241 uhp = uhp->uh_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002242 }
2243 }
2244
2245 if (uhp != NULL) /* found it */
2246 break;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002247
2248 if (absolute)
2249 {
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002250 EMSGN(_("E830: Undo number %ld not found"), step);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002251 return;
2252 }
2253
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002254 if (closest == closest_start)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002255 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002256 if (step < 0)
2257 MSG(_("Already at oldest change"));
2258 else
2259 MSG(_("Already at newest change"));
Bram Moolenaar1e607892006-03-13 22:15:53 +00002260 return;
2261 }
2262
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002263 target = closest_seq;
2264 dosec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002265 dofile = FALSE;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002266 if (step < 0)
2267 above = TRUE; /* stop above the header */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002268 }
2269
2270 /* If we found it: Follow the path to go to where we want to be. */
2271 if (uhp != NULL)
2272 {
2273 /*
2274 * First go up the tree as much as needed.
2275 */
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002276 while (!got_int)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002277 {
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002278 /* Do the change warning now, for the same reason as above. */
2279 change_warning(0);
2280
Bram Moolenaar1e607892006-03-13 22:15:53 +00002281 uhp = curbuf->b_u_curhead;
2282 if (uhp == NULL)
2283 uhp = curbuf->b_u_newhead;
2284 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002285 uhp = uhp->uh_next.ptr;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002286 if (uhp == NULL || uhp->uh_walk != mark
2287 || (uhp->uh_seq == target && !above))
Bram Moolenaar1e607892006-03-13 22:15:53 +00002288 break;
2289 curbuf->b_u_curhead = uhp;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002290 u_undoredo(TRUE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002291 uhp->uh_walk = nomark; /* don't go back down here */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002292 }
2293
2294 /*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002295 * And now go down the tree (redo), branching off where needed.
Bram Moolenaar1e607892006-03-13 22:15:53 +00002296 */
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002297 while (!got_int)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002298 {
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002299 /* Do the change warning now, for the same reason as above. */
2300 change_warning(0);
2301
2302 uhp = curbuf->b_u_curhead;
2303 if (uhp == NULL)
2304 break;
2305
Bram Moolenaar89ed3df2007-01-09 19:23:12 +00002306 /* Go back to the first branch with a mark. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002307 while (uhp->uh_alt_prev.ptr != NULL
2308 && uhp->uh_alt_prev.ptr->uh_walk == mark)
2309 uhp = uhp->uh_alt_prev.ptr;
Bram Moolenaar89ed3df2007-01-09 19:23:12 +00002310
Bram Moolenaar1e607892006-03-13 22:15:53 +00002311 /* Find the last branch with a mark, that's the one. */
2312 last = uhp;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002313 while (last->uh_alt_next.ptr != NULL
2314 && last->uh_alt_next.ptr->uh_walk == mark)
2315 last = last->uh_alt_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002316 if (last != uhp)
2317 {
2318 /* Make the used branch the first entry in the list of
2319 * alternatives to make "u" and CTRL-R take this branch. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002320 while (uhp->uh_alt_prev.ptr != NULL)
2321 uhp = uhp->uh_alt_prev.ptr;
2322 if (last->uh_alt_next.ptr != NULL)
2323 last->uh_alt_next.ptr->uh_alt_prev.ptr =
2324 last->uh_alt_prev.ptr;
2325 last->uh_alt_prev.ptr->uh_alt_next.ptr = last->uh_alt_next.ptr;
2326 last->uh_alt_prev.ptr = NULL;
2327 last->uh_alt_next.ptr = uhp;
2328 uhp->uh_alt_prev.ptr = last;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002329
Bram Moolenaar8f1f6292010-05-30 16:55:22 +02002330 if (curbuf->b_u_oldhead == uhp)
2331 curbuf->b_u_oldhead = last;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002332 uhp = last;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002333 if (uhp->uh_next.ptr != NULL)
2334 uhp->uh_next.ptr->uh_prev.ptr = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002335 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002336 curbuf->b_u_curhead = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002337
2338 if (uhp->uh_walk != mark)
2339 break; /* must have reached the target */
2340
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002341 /* Stop when going backwards in time and didn't find the exact
2342 * header we were looking for. */
2343 if (uhp->uh_seq == target && above)
Bram Moolenaardb552d602006-03-23 22:59:57 +00002344 {
2345 curbuf->b_u_seq_cur = target - 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002346 break;
Bram Moolenaardb552d602006-03-23 22:59:57 +00002347 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002348
Bram Moolenaarca003e12006-03-17 23:19:38 +00002349 u_undoredo(FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002350
2351 /* Advance "curhead" to below the header we last used. If it
2352 * becomes NULL then we need to set "newhead" to this leaf. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002353 if (uhp->uh_prev.ptr == NULL)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002354 curbuf->b_u_newhead = uhp;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002355 curbuf->b_u_curhead = uhp->uh_prev.ptr;
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002356 did_undo = FALSE;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002357
2358 if (uhp->uh_seq == target) /* found it! */
2359 break;
2360
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002361 uhp = uhp->uh_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002362 if (uhp == NULL || uhp->uh_walk != mark)
2363 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002364 /* Need to redo more but can't find it... */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002365 EMSG2(_(e_intern2), "undo_time()");
2366 break;
2367 }
2368 }
2369 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00002370 u_undo_end(did_undo, absolute);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371}
2372
2373/*
2374 * u_undoredo: common code for undo and redo
2375 *
2376 * The lines in the file are replaced by the lines in the entry list at
2377 * curbuf->b_u_curhead. The replaced lines in the file are saved in the entry
2378 * list for the next undo/redo.
Bram Moolenaarca003e12006-03-17 23:19:38 +00002379 *
2380 * When "undo" is TRUE we go up in the tree, when FALSE we go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 */
2382 static void
Bram Moolenaarca003e12006-03-17 23:19:38 +00002383u_undoredo(undo)
2384 int undo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385{
2386 char_u **newarray = NULL;
2387 linenr_T oldsize;
2388 linenr_T newsize;
2389 linenr_T top, bot;
2390 linenr_T lnum;
2391 linenr_T newlnum = MAXLNUM;
2392 long i;
2393 u_entry_T *uep, *nuep;
2394 u_entry_T *newlist = NULL;
2395 int old_flags;
2396 int new_flags;
2397 pos_T namedm[NMARKS];
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002398#ifdef FEAT_VISUAL
2399 visualinfo_T visualinfo;
2400#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 int empty_buffer; /* buffer became empty */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002402 u_header_T *curhead = curbuf->b_u_curhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002404#ifdef FEAT_AUTOCMD
2405 /* Don't want autocommands using the undo structures here, they are
2406 * invalid till the end. */
2407 block_autocmds();
2408#endif
2409
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002410#ifdef U_DEBUG
2411 u_check(FALSE);
2412#endif
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002413 old_flags = curhead->uh_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 new_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
2415 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
2416 setpcmark();
2417
2418 /*
2419 * save marks before undo/redo
2420 */
2421 mch_memmove(namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002422#ifdef FEAT_VISUAL
2423 visualinfo = curbuf->b_visual;
2424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 curbuf->b_op_start.lnum = curbuf->b_ml.ml_line_count;
2426 curbuf->b_op_start.col = 0;
2427 curbuf->b_op_end.lnum = 0;
2428 curbuf->b_op_end.col = 0;
2429
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002430 for (uep = curhead->uh_entry; uep != NULL; uep = nuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 {
2432 top = uep->ue_top;
2433 bot = uep->ue_bot;
2434 if (bot == 0)
2435 bot = curbuf->b_ml.ml_line_count + 1;
2436 if (top > curbuf->b_ml.ml_line_count || top >= bot
2437 || bot > curbuf->b_ml.ml_line_count + 1)
2438 {
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002439#ifdef FEAT_AUTOCMD
2440 unblock_autocmds();
2441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 EMSG(_("E438: u_undo: line numbers wrong"));
2443 changed(); /* don't want UNCHANGED now */
2444 return;
2445 }
2446
2447 oldsize = bot - top - 1; /* number of lines before undo */
2448 newsize = uep->ue_size; /* number of lines after undo */
2449
2450 if (top < newlnum)
2451 {
2452 /* If the saved cursor is somewhere in this undo block, move it to
2453 * the remembered position. Makes "gwap" put the cursor back
2454 * where it was. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002455 lnum = curhead->uh_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 if (lnum >= top && lnum <= top + newsize + 1)
2457 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002458 curwin->w_cursor = curhead->uh_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 newlnum = curwin->w_cursor.lnum - 1;
2460 }
2461 else
2462 {
2463 /* Use the first line that actually changed. Avoids that
2464 * undoing auto-formatting puts the cursor in the previous
2465 * line. */
2466 for (i = 0; i < newsize && i < oldsize; ++i)
2467 if (STRCMP(uep->ue_array[i], ml_get(top + 1 + i)) != 0)
2468 break;
2469 if (i == newsize && newlnum == MAXLNUM && uep->ue_next == NULL)
2470 {
2471 newlnum = top;
2472 curwin->w_cursor.lnum = newlnum + 1;
2473 }
2474 else if (i < newsize)
2475 {
2476 newlnum = top + i;
2477 curwin->w_cursor.lnum = newlnum + 1;
2478 }
2479 }
2480 }
2481
2482 empty_buffer = FALSE;
2483
2484 /* delete the lines between top and bot and save them in newarray */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002485 if (oldsize > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002487 if ((newarray = (char_u **)U_ALLOC_LINE(
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002488 sizeof(char_u *) * oldsize)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 {
2490 do_outofmem_msg((long_u)(sizeof(char_u *) * oldsize));
2491 /*
2492 * We have messed up the entry list, repair is impossible.
2493 * we have to free the rest of the list.
2494 */
2495 while (uep != NULL)
2496 {
2497 nuep = uep->ue_next;
2498 u_freeentry(uep, uep->ue_size);
2499 uep = nuep;
2500 }
2501 break;
2502 }
2503 /* delete backwards, it goes faster in most cases */
2504 for (lnum = bot - 1, i = oldsize; --i >= 0; --lnum)
2505 {
2506 /* what can we do when we run out of memory? */
2507 if ((newarray[i] = u_save_line(lnum)) == NULL)
2508 do_outofmem_msg((long_u)0);
2509 /* remember we deleted the last line in the buffer, and a
2510 * dummy empty line will be inserted */
2511 if (curbuf->b_ml.ml_line_count == 1)
2512 empty_buffer = TRUE;
2513 ml_delete(lnum, FALSE);
2514 }
2515 }
Bram Moolenaar8d343302005-07-12 22:46:17 +00002516 else
2517 newarray = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518
2519 /* insert the lines in u_array between top and bot */
2520 if (newsize)
2521 {
2522 for (lnum = top, i = 0; i < newsize; ++i, ++lnum)
2523 {
2524 /*
2525 * If the file is empty, there is an empty line 1 that we
2526 * should get rid of, by replacing it with the new line
2527 */
2528 if (empty_buffer && lnum == 0)
2529 ml_replace((linenr_T)1, uep->ue_array[i], TRUE);
2530 else
2531 ml_append(lnum, uep->ue_array[i], (colnr_T)0, FALSE);
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002532 vim_free(uep->ue_array[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 }
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002534 vim_free((char_u *)uep->ue_array);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 }
2536
2537 /* adjust marks */
2538 if (oldsize != newsize)
2539 {
2540 mark_adjust(top + 1, top + oldsize, (long)MAXLNUM,
2541 (long)newsize - (long)oldsize);
2542 if (curbuf->b_op_start.lnum > top + oldsize)
2543 curbuf->b_op_start.lnum += newsize - oldsize;
2544 if (curbuf->b_op_end.lnum > top + oldsize)
2545 curbuf->b_op_end.lnum += newsize - oldsize;
2546 }
2547
2548 changed_lines(top + 1, 0, bot, newsize - oldsize);
2549
2550 /* set '[ and '] mark */
2551 if (top + 1 < curbuf->b_op_start.lnum)
2552 curbuf->b_op_start.lnum = top + 1;
2553 if (newsize == 0 && top + 1 > curbuf->b_op_end.lnum)
2554 curbuf->b_op_end.lnum = top + 1;
2555 else if (top + newsize > curbuf->b_op_end.lnum)
2556 curbuf->b_op_end.lnum = top + newsize;
2557
2558 u_newcount += newsize;
2559 u_oldcount += oldsize;
2560 uep->ue_size = oldsize;
2561 uep->ue_array = newarray;
2562 uep->ue_bot = top + newsize + 1;
2563
2564 /*
2565 * insert this entry in front of the new entry list
2566 */
2567 nuep = uep->ue_next;
2568 uep->ue_next = newlist;
2569 newlist = uep;
2570 }
2571
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002572 curhead->uh_entry = newlist;
2573 curhead->uh_flags = new_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 if ((old_flags & UH_EMPTYBUF) && bufempty())
2575 curbuf->b_ml.ml_flags |= ML_EMPTY;
2576 if (old_flags & UH_CHANGED)
2577 changed();
2578 else
Bram Moolenaar009b2592004-10-24 19:18:58 +00002579#ifdef FEAT_NETBEANS_INTG
2580 /* per netbeans undo rules, keep it as modified */
2581 if (!isNetbeansModified(curbuf))
2582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 unchanged(curbuf, FALSE);
2584
2585 /*
2586 * restore marks from before undo/redo
2587 */
2588 for (i = 0; i < NMARKS; ++i)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002589 if (curhead->uh_namedm[i].lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002591 curbuf->b_namedm[i] = curhead->uh_namedm[i];
2592 curhead->uh_namedm[i] = namedm[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002594#ifdef FEAT_VISUAL
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002595 if (curhead->uh_visual.vi_start.lnum != 0)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002596 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002597 curbuf->b_visual = curhead->uh_visual;
2598 curhead->uh_visual = visualinfo;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002599 }
2600#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601
2602 /*
2603 * If the cursor is only off by one line, put it at the same position as
2604 * before starting the change (for the "o" command).
2605 * Otherwise the cursor should go to the first undone line.
2606 */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002607 if (curhead->uh_cursor.lnum + 1 == curwin->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 && curwin->w_cursor.lnum > 1)
2609 --curwin->w_cursor.lnum;
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002610 if (curwin->w_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 {
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002612 if (curhead->uh_cursor.lnum == curwin->w_cursor.lnum)
2613 {
2614 curwin->w_cursor.col = curhead->uh_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002616 if (virtual_active() && curhead->uh_cursor_vcol >= 0)
2617 coladvance((colnr_T)curhead->uh_cursor_vcol);
2618 else
2619 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620#endif
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002621 }
2622 else
2623 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 else
2626 {
2627 /* We get here with the current cursor line being past the end (eg
2628 * after adding lines at the end of the file, and then undoing it).
2629 * check_cursor() will move the cursor to the last line. Move it to
2630 * the first column here. */
2631 curwin->w_cursor.col = 0;
2632#ifdef FEAT_VIRTUALEDIT
2633 curwin->w_cursor.coladd = 0;
2634#endif
2635 }
2636
2637 /* Make sure the cursor is on an existing line and column. */
2638 check_cursor();
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002639
2640 /* Remember where we are for "g-" and ":earlier 10s". */
2641 curbuf->b_u_seq_cur = curhead->uh_seq;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002642 if (undo)
2643 /* We are below the previous undo. However, to make ":earlier 1s"
2644 * work we compute this as being just above the just undone change. */
2645 --curbuf->b_u_seq_cur;
2646
Bram Moolenaar730cde92010-06-27 05:18:54 +02002647 /* Remember where we are for ":earlier 1f" and ":later 1f". */
2648 if (curhead->uh_save_nr != 0)
2649 {
2650 if (undo)
2651 curbuf->b_u_save_nr_cur = curhead->uh_save_nr - 1;
2652 else
2653 curbuf->b_u_save_nr_cur = curhead->uh_save_nr;
2654 }
2655
Bram Moolenaarca003e12006-03-17 23:19:38 +00002656 /* The timestamp can be the same for multiple changes, just use the one of
2657 * the undone/redone change. */
Bram Moolenaara800b422010-06-27 01:15:55 +02002658 curbuf->b_u_time_cur = curhead->uh_time;
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002659
2660#ifdef FEAT_AUTOCMD
2661 unblock_autocmds();
2662#endif
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002663#ifdef U_DEBUG
2664 u_check(FALSE);
2665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666}
2667
2668/*
2669 * If we deleted or added lines, report the number of less/more lines.
2670 * Otherwise, report the number of changes (this may be incorrect
2671 * in some cases, but it's better than nothing).
2672 */
2673 static void
Bram Moolenaardb552d602006-03-23 22:59:57 +00002674u_undo_end(did_undo, absolute)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002675 int did_undo; /* just did an undo */
Bram Moolenaardb552d602006-03-23 22:59:57 +00002676 int absolute; /* used ":undo N" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677{
Bram Moolenaar89d40322006-08-29 15:30:07 +00002678 char *msgstr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002679 u_header_T *uhp;
2680 char_u msgbuf[80];
Bram Moolenaar1e607892006-03-13 22:15:53 +00002681
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682#ifdef FEAT_FOLDING
2683 if ((fdo_flags & FDO_UNDO) && KeyTyped)
2684 foldOpenCursor();
2685#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +00002686
2687 if (global_busy /* no messages now, wait until global is finished */
2688 || !messaging()) /* 'lazyredraw' set, don't do messages now */
2689 return;
2690
2691 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2692 --u_newcount;
2693
2694 u_oldcount -= u_newcount;
2695 if (u_oldcount == -1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002696 msgstr = N_("more line");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002697 else if (u_oldcount < 0)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002698 msgstr = N_("more lines");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002699 else if (u_oldcount == 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002700 msgstr = N_("line less");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002701 else if (u_oldcount > 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002702 msgstr = N_("fewer lines");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002703 else
2704 {
2705 u_oldcount = u_newcount;
2706 if (u_newcount == 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002707 msgstr = N_("change");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002708 else
Bram Moolenaar89d40322006-08-29 15:30:07 +00002709 msgstr = N_("changes");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002710 }
2711
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002712 if (curbuf->b_u_curhead != NULL)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002713 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00002714 /* For ":undo N" we prefer a "after #N" message. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002715 if (absolute && curbuf->b_u_curhead->uh_next.ptr != NULL)
Bram Moolenaardb552d602006-03-23 22:59:57 +00002716 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002717 uhp = curbuf->b_u_curhead->uh_next.ptr;
Bram Moolenaardb552d602006-03-23 22:59:57 +00002718 did_undo = FALSE;
2719 }
2720 else if (did_undo)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002721 uhp = curbuf->b_u_curhead;
2722 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002723 uhp = curbuf->b_u_curhead->uh_next.ptr;
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002724 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002725 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002726 uhp = curbuf->b_u_newhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002727
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002728 if (uhp == NULL)
2729 *msgbuf = NUL;
2730 else
2731 u_add_time(msgbuf, sizeof(msgbuf), uhp->uh_time);
2732
Bram Moolenaarb2c03502010-07-02 20:20:09 +02002733#ifdef FEAT_CONCEAL
2734 {
2735 win_T *wp;
2736
2737 FOR_ALL_WINDOWS(wp)
2738 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02002739 if (wp->w_buffer == curbuf && wp->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02002740 redraw_win_later(wp, NOT_VALID);
2741 }
2742 }
2743#endif
2744
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002745 smsg((char_u *)_("%ld %s; %s #%ld %s"),
Bram Moolenaarca003e12006-03-17 23:19:38 +00002746 u_oldcount < 0 ? -u_oldcount : u_oldcount,
Bram Moolenaar89d40322006-08-29 15:30:07 +00002747 _(msgstr),
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002748 did_undo ? _("before") : _("after"),
2749 uhp == NULL ? 0L : uhp->uh_seq,
2750 msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751}
2752
2753/*
2754 * u_sync: stop adding to the current entry list
2755 */
2756 void
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002757u_sync(force)
2758 int force; /* Also sync when no_u_sync is set. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759{
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002760 /* Skip it when already synced or syncing is disabled. */
2761 if (curbuf->b_u_synced || (!force && no_u_sync > 0))
2762 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2764 if (im_is_preediting())
2765 return; /* XIM is busy, don't break an undo sequence */
2766#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002767 if (get_undolevel() < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 curbuf->b_u_synced = TRUE; /* no entries, nothing to do */
2769 else
2770 {
2771 u_getbot(); /* compute ue_bot of previous u_save */
2772 curbuf->b_u_curhead = NULL;
2773 }
2774}
2775
2776/*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002777 * ":undolist": List the leafs of the undo tree
2778 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002779 void
2780ex_undolist(eap)
Bram Moolenaarfff2bee2010-05-15 13:56:02 +02002781 exarg_T *eap UNUSED;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002782{
2783 garray_T ga;
2784 u_header_T *uhp;
2785 int mark;
2786 int nomark;
2787 int changes = 1;
2788 int i;
2789
2790 /*
2791 * 1: walk the tree to find all leafs, put the info in "ga".
2792 * 2: sort the lines
2793 * 3: display the list
2794 */
2795 mark = ++lastmark;
2796 nomark = ++lastmark;
2797 ga_init2(&ga, (int)sizeof(char *), 20);
2798
2799 uhp = curbuf->b_u_oldhead;
2800 while (uhp != NULL)
2801 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002802 if (uhp->uh_prev.ptr == NULL && uhp->uh_walk != nomark
Bram Moolenaarca003e12006-03-17 23:19:38 +00002803 && uhp->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002804 {
2805 if (ga_grow(&ga, 1) == FAIL)
2806 break;
2807 vim_snprintf((char *)IObuff, IOSIZE, "%6ld %7ld ",
2808 uhp->uh_seq, changes);
2809 u_add_time(IObuff + STRLEN(IObuff), IOSIZE - STRLEN(IObuff),
2810 uhp->uh_time);
Bram Moolenaara800b422010-06-27 01:15:55 +02002811 if (uhp->uh_save_nr > 0)
2812 {
Bram Moolenaardba01a02010-11-03 19:32:42 +01002813 while (STRLEN(IObuff) < 33)
Bram Moolenaara800b422010-06-27 01:15:55 +02002814 STRCAT(IObuff, " ");
2815 vim_snprintf_add((char *)IObuff, IOSIZE,
2816 " %3ld", uhp->uh_save_nr);
2817 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002818 ((char_u **)(ga.ga_data))[ga.ga_len++] = vim_strsave(IObuff);
2819 }
2820
2821 uhp->uh_walk = mark;
2822
2823 /* go down in the tree if we haven't been there */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002824 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != nomark
2825 && uhp->uh_prev.ptr->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002826 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002827 uhp = uhp->uh_prev.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002828 ++changes;
2829 }
2830
2831 /* go to alternate branch if we haven't been there */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002832 else if (uhp->uh_alt_next.ptr != NULL
2833 && uhp->uh_alt_next.ptr->uh_walk != nomark
2834 && uhp->uh_alt_next.ptr->uh_walk != mark)
2835 uhp = uhp->uh_alt_next.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002836
2837 /* go up in the tree if we haven't been there and we are at the
2838 * start of alternate branches */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002839 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
2840 && uhp->uh_next.ptr->uh_walk != nomark
2841 && uhp->uh_next.ptr->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002842 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002843 uhp = uhp->uh_next.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002844 --changes;
2845 }
2846
2847 else
2848 {
2849 /* need to backtrack; mark this node as done */
2850 uhp->uh_walk = nomark;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002851 if (uhp->uh_alt_prev.ptr != NULL)
2852 uhp = uhp->uh_alt_prev.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002853 else
2854 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002855 uhp = uhp->uh_next.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002856 --changes;
2857 }
2858 }
2859 }
2860
2861 if (ga.ga_len == 0)
2862 MSG(_("Nothing to undo"));
2863 else
2864 {
2865 sort_strings((char_u **)ga.ga_data, ga.ga_len);
2866
2867 msg_start();
Bram Moolenaardba01a02010-11-03 19:32:42 +01002868 msg_puts_attr((char_u *)_("number changes when saved"),
Bram Moolenaara800b422010-06-27 01:15:55 +02002869 hl_attr(HLF_T));
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002870 for (i = 0; i < ga.ga_len && !got_int; ++i)
2871 {
2872 msg_putchar('\n');
2873 if (got_int)
2874 break;
2875 msg_puts(((char_u **)ga.ga_data)[i]);
2876 }
2877 msg_end();
2878
2879 ga_clear_strings(&ga);
2880 }
2881}
2882
2883/*
2884 * Put the timestamp of an undo header in "buf[buflen]" in a nice format.
2885 */
2886 static void
2887u_add_time(buf, buflen, tt)
2888 char_u *buf;
2889 size_t buflen;
2890 time_t tt;
2891{
2892#ifdef HAVE_STRFTIME
2893 struct tm *curtime;
2894
2895 if (time(NULL) - tt >= 100)
2896 {
2897 curtime = localtime(&tt);
Bram Moolenaardba01a02010-11-03 19:32:42 +01002898 if (time(NULL) - tt < (60L * 60L * 12L))
2899 /* within 12 hours */
2900 (void)strftime((char *)buf, buflen, "%H:%M:%S", curtime);
Bram Moolenaardba01a02010-11-03 19:32:42 +01002901 else
Bram Moolenaarb9ce83e2012-08-23 12:59:02 +02002902 /* longer ago */
Bram Moolenaar5e3d6ca2011-01-22 21:25:11 +01002903 (void)strftime((char *)buf, buflen, "%Y/%m/%d %H:%M:%S", curtime);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002904 }
2905 else
2906#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002907 vim_snprintf((char *)buf, buflen, _("%ld seconds ago"),
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002908 (long)(time(NULL) - tt));
2909}
2910
2911/*
Bram Moolenaare224ffa2006-03-01 00:01:28 +00002912 * ":undojoin": continue adding to the last entry list
2913 */
Bram Moolenaare224ffa2006-03-01 00:01:28 +00002914 void
2915ex_undojoin(eap)
Bram Moolenaarfff2bee2010-05-15 13:56:02 +02002916 exarg_T *eap UNUSED;
Bram Moolenaare224ffa2006-03-01 00:01:28 +00002917{
Bram Moolenaare224ffa2006-03-01 00:01:28 +00002918 if (curbuf->b_u_newhead == NULL)
2919 return; /* nothing changed before */
Bram Moolenaar57657d82006-04-21 22:12:41 +00002920 if (curbuf->b_u_curhead != NULL)
2921 {
2922 EMSG(_("E790: undojoin is not allowed after undo"));
2923 return;
2924 }
2925 if (!curbuf->b_u_synced)
2926 return; /* already unsynced */
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002927 if (get_undolevel() < 0)
Bram Moolenaare224ffa2006-03-01 00:01:28 +00002928 return; /* no entries, nothing to do */
2929 else
2930 {
2931 /* Go back to the last entry */
2932 curbuf->b_u_curhead = curbuf->b_u_newhead;
2933 curbuf->b_u_synced = FALSE; /* no entries, nothing to do */
2934 }
2935}
2936
2937/*
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002938 * Called after writing or reloading the file and setting b_changed to FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 * Now an undo means that the buffer is modified.
2940 */
2941 void
2942u_unchanged(buf)
2943 buf_T *buf;
2944{
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002945 u_unch_branch(buf->b_u_oldhead);
2946 buf->b_did_warn = FALSE;
2947}
2948
Bram Moolenaar730cde92010-06-27 05:18:54 +02002949/*
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02002950 * After reloading a buffer which was saved for 'undoreload': Find the first
2951 * line that was changed and set the cursor there.
2952 */
2953 void
2954u_find_first_changed()
2955{
2956 u_header_T *uhp = curbuf->b_u_newhead;
2957 u_entry_T *uep;
2958 linenr_T lnum;
2959
2960 if (curbuf->b_u_curhead != NULL || uhp == NULL)
2961 return; /* undid something in an autocmd? */
2962
2963 /* Check that the last undo block was for the whole file. */
2964 uep = uhp->uh_entry;
2965 if (uep->ue_top != 0 || uep->ue_bot != 0)
2966 return;
2967
2968 for (lnum = 1; lnum < curbuf->b_ml.ml_line_count
2969 && lnum <= uep->ue_size; ++lnum)
2970 if (STRCMP(ml_get_buf(curbuf, lnum, FALSE),
2971 uep->ue_array[lnum - 1]) != 0)
2972 {
2973 clearpos(&(uhp->uh_cursor));
2974 uhp->uh_cursor.lnum = lnum;
2975 return;
2976 }
2977 if (curbuf->b_ml.ml_line_count != uep->ue_size)
2978 {
2979 /* lines added or deleted at the end, put the cursor there */
2980 clearpos(&(uhp->uh_cursor));
2981 uhp->uh_cursor.lnum = lnum;
2982 }
2983}
2984
2985/*
Bram Moolenaar730cde92010-06-27 05:18:54 +02002986 * Increase the write count, store it in the last undo header, what would be
2987 * used for "u".
2988 */
2989 void
2990u_update_save_nr(buf)
2991 buf_T *buf;
2992{
2993 u_header_T *uhp;
2994
2995 ++buf->b_u_save_nr_last;
2996 buf->b_u_save_nr_cur = buf->b_u_save_nr_last;
2997 uhp = buf->b_u_curhead;
2998 if (uhp != NULL)
2999 uhp = uhp->uh_next.ptr;
3000 else
3001 uhp = buf->b_u_newhead;
3002 if (uhp != NULL)
3003 uhp->uh_save_nr = buf->b_u_save_nr_last;
3004}
3005
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003006 static void
3007u_unch_branch(uhp)
3008 u_header_T *uhp;
3009{
Bram Moolenaar1e607892006-03-13 22:15:53 +00003010 u_header_T *uh;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003012 for (uh = uhp; uh != NULL; uh = uh->uh_prev.ptr)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003013 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 uh->uh_flags |= UH_CHANGED;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003015 if (uh->uh_alt_next.ptr != NULL)
3016 u_unch_branch(uh->uh_alt_next.ptr); /* recursive */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018}
3019
3020/*
3021 * Get pointer to last added entry.
3022 * If it's not valid, give an error message and return NULL.
3023 */
3024 static u_entry_T *
3025u_get_headentry()
3026{
3027 if (curbuf->b_u_newhead == NULL || curbuf->b_u_newhead->uh_entry == NULL)
3028 {
3029 EMSG(_("E439: undo list corrupt"));
3030 return NULL;
3031 }
3032 return curbuf->b_u_newhead->uh_entry;
3033}
3034
3035/*
3036 * u_getbot(): compute the line number of the previous u_save
3037 * It is called only when b_u_synced is FALSE.
3038 */
3039 static void
3040u_getbot()
3041{
3042 u_entry_T *uep;
3043 linenr_T extra;
3044
3045 uep = u_get_headentry(); /* check for corrupt undo list */
3046 if (uep == NULL)
3047 return;
3048
3049 uep = curbuf->b_u_newhead->uh_getbot_entry;
3050 if (uep != NULL)
3051 {
3052 /*
3053 * the new ue_bot is computed from the number of lines that has been
3054 * inserted (0 - deleted) since calling u_save. This is equal to the
3055 * old line count subtracted from the current line count.
3056 */
3057 extra = curbuf->b_ml.ml_line_count - uep->ue_lcount;
3058 uep->ue_bot = uep->ue_top + uep->ue_size + 1 + extra;
3059 if (uep->ue_bot < 1 || uep->ue_bot > curbuf->b_ml.ml_line_count)
3060 {
3061 EMSG(_("E440: undo line missing"));
3062 uep->ue_bot = uep->ue_top + 1; /* assume all lines deleted, will
3063 * get all the old lines back
3064 * without deleting the current
3065 * ones */
3066 }
3067
3068 curbuf->b_u_newhead->uh_getbot_entry = NULL;
3069 }
3070
3071 curbuf->b_u_synced = TRUE;
3072}
3073
3074/*
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003075 * Free one header "uhp" and its entry list and adjust the pointers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 */
3077 static void
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003078u_freeheader(buf, uhp, uhpp)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003079 buf_T *buf;
Bram Moolenaar1e607892006-03-13 22:15:53 +00003080 u_header_T *uhp;
3081 u_header_T **uhpp; /* if not NULL reset when freeing this header */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082{
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003083 u_header_T *uhap;
3084
Bram Moolenaar1e607892006-03-13 22:15:53 +00003085 /* When there is an alternate redo list free that branch completely,
3086 * because we can never go there. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003087 if (uhp->uh_alt_next.ptr != NULL)
3088 u_freebranch(buf, uhp->uh_alt_next.ptr, uhpp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003090 if (uhp->uh_alt_prev.ptr != NULL)
3091 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092
Bram Moolenaar1e607892006-03-13 22:15:53 +00003093 /* Update the links in the list to remove the header. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003094 if (uhp->uh_next.ptr == NULL)
3095 buf->b_u_oldhead = uhp->uh_prev.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003097 uhp->uh_next.ptr->uh_prev.ptr = uhp->uh_prev.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003099 if (uhp->uh_prev.ptr == NULL)
3100 buf->b_u_newhead = uhp->uh_next.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003102 for (uhap = uhp->uh_prev.ptr; uhap != NULL;
3103 uhap = uhap->uh_alt_next.ptr)
3104 uhap->uh_next.ptr = uhp->uh_next.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105
Bram Moolenaar1e607892006-03-13 22:15:53 +00003106 u_freeentries(buf, uhp, uhpp);
3107}
3108
3109/*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003110 * Free an alternate branch and any following alternate branches.
Bram Moolenaar1e607892006-03-13 22:15:53 +00003111 */
3112 static void
3113u_freebranch(buf, uhp, uhpp)
3114 buf_T *buf;
3115 u_header_T *uhp;
3116 u_header_T **uhpp; /* if not NULL reset when freeing this header */
3117{
3118 u_header_T *tofree, *next;
3119
Bram Moolenaar07d06772007-11-10 21:51:15 +00003120 /* If this is the top branch we may need to use u_freeheader() to update
3121 * all the pointers. */
3122 if (uhp == buf->b_u_oldhead)
3123 {
Bram Moolenaaraa887322013-11-07 03:04:11 +01003124 while (buf->b_u_oldhead != NULL)
3125 u_freeheader(buf, buf->b_u_oldhead, uhpp);
Bram Moolenaar07d06772007-11-10 21:51:15 +00003126 return;
3127 }
3128
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003129 if (uhp->uh_alt_prev.ptr != NULL)
3130 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +00003131
3132 next = uhp;
3133 while (next != NULL)
3134 {
3135 tofree = next;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003136 if (tofree->uh_alt_next.ptr != NULL)
3137 u_freebranch(buf, tofree->uh_alt_next.ptr, uhpp); /* recursive */
3138 next = tofree->uh_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00003139 u_freeentries(buf, tofree, uhpp);
3140 }
3141}
3142
3143/*
3144 * Free all the undo entries for one header and the header itself.
3145 * This means that "uhp" is invalid when returning.
3146 */
3147 static void
3148u_freeentries(buf, uhp, uhpp)
3149 buf_T *buf;
3150 u_header_T *uhp;
3151 u_header_T **uhpp; /* if not NULL reset when freeing this header */
3152{
3153 u_entry_T *uep, *nuep;
3154
3155 /* Check for pointers to the header that become invalid now. */
3156 if (buf->b_u_curhead == uhp)
3157 buf->b_u_curhead = NULL;
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003158 if (buf->b_u_newhead == uhp)
3159 buf->b_u_newhead = NULL; /* freeing the newest entry */
Bram Moolenaar1e607892006-03-13 22:15:53 +00003160 if (uhpp != NULL && uhp == *uhpp)
3161 *uhpp = NULL;
3162
3163 for (uep = uhp->uh_entry; uep != NULL; uep = nuep)
3164 {
3165 nuep = uep->ue_next;
3166 u_freeentry(uep, uep->ue_size);
3167 }
3168
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003169#ifdef U_DEBUG
3170 uhp->uh_magic = 0;
3171#endif
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003172 vim_free((char_u *)uhp);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003173 --buf->b_u_numhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174}
3175
3176/*
3177 * free entry 'uep' and 'n' lines in uep->ue_array[]
3178 */
3179 static void
3180u_freeentry(uep, n)
3181 u_entry_T *uep;
3182 long n;
3183{
Bram Moolenaar8d343302005-07-12 22:46:17 +00003184 while (n > 0)
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003185 vim_free(uep->ue_array[--n]);
3186 vim_free((char_u *)uep->ue_array);
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003187#ifdef U_DEBUG
3188 uep->ue_magic = 0;
3189#endif
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003190 vim_free((char_u *)uep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191}
3192
3193/*
3194 * invalidate the undo buffer; called when storage has already been released
3195 */
3196 void
3197u_clearall(buf)
3198 buf_T *buf;
3199{
3200 buf->b_u_newhead = buf->b_u_oldhead = buf->b_u_curhead = NULL;
3201 buf->b_u_synced = TRUE;
3202 buf->b_u_numhead = 0;
3203 buf->b_u_line_ptr = NULL;
3204 buf->b_u_line_lnum = 0;
3205}
3206
3207/*
3208 * save the line "lnum" for the "U" command
3209 */
3210 void
3211u_saveline(lnum)
3212 linenr_T lnum;
3213{
3214 if (lnum == curbuf->b_u_line_lnum) /* line is already saved */
3215 return;
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00003216 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) /* should never happen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 return;
3218 u_clearline();
3219 curbuf->b_u_line_lnum = lnum;
3220 if (curwin->w_cursor.lnum == lnum)
3221 curbuf->b_u_line_colnr = curwin->w_cursor.col;
3222 else
3223 curbuf->b_u_line_colnr = 0;
3224 if ((curbuf->b_u_line_ptr = u_save_line(lnum)) == NULL)
3225 do_outofmem_msg((long_u)0);
3226}
3227
3228/*
3229 * clear the line saved for the "U" command
3230 * (this is used externally for crossing a line while in insert mode)
3231 */
3232 void
3233u_clearline()
3234{
3235 if (curbuf->b_u_line_ptr != NULL)
3236 {
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003237 vim_free(curbuf->b_u_line_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 curbuf->b_u_line_ptr = NULL;
3239 curbuf->b_u_line_lnum = 0;
3240 }
3241}
3242
3243/*
3244 * Implementation of the "U" command.
3245 * Differentiation from vi: "U" can be undone with the next "U".
3246 * We also allow the cursor to be in another line.
Bram Moolenaard04b7502010-07-08 22:27:55 +02003247 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 */
3249 void
3250u_undoline()
3251{
3252 colnr_T t;
3253 char_u *oldp;
3254
3255 if (undo_off)
3256 return;
3257
Bram Moolenaare3300c82008-02-13 14:21:38 +00003258 if (curbuf->b_u_line_ptr == NULL
3259 || curbuf->b_u_line_lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 {
3261 beep_flush();
3262 return;
3263 }
Bram Moolenaare3300c82008-02-13 14:21:38 +00003264
3265 /* first save the line for the 'u' command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 if (u_savecommon(curbuf->b_u_line_lnum - 1,
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003267 curbuf->b_u_line_lnum + 1, (linenr_T)0, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 return;
3269 oldp = u_save_line(curbuf->b_u_line_lnum);
3270 if (oldp == NULL)
3271 {
3272 do_outofmem_msg((long_u)0);
3273 return;
3274 }
3275 ml_replace(curbuf->b_u_line_lnum, curbuf->b_u_line_ptr, TRUE);
3276 changed_bytes(curbuf->b_u_line_lnum, 0);
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003277 vim_free(curbuf->b_u_line_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 curbuf->b_u_line_ptr = oldp;
3279
3280 t = curbuf->b_u_line_colnr;
3281 if (curwin->w_cursor.lnum == curbuf->b_u_line_lnum)
3282 curbuf->b_u_line_colnr = curwin->w_cursor.col;
3283 curwin->w_cursor.col = t;
3284 curwin->w_cursor.lnum = curbuf->b_u_line_lnum;
Bram Moolenaare3300c82008-02-13 14:21:38 +00003285 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286}
3287
3288/*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003289 * Free all allocated memory blocks for the buffer 'buf'.
3290 */
3291 void
3292u_blockfree(buf)
3293 buf_T *buf;
3294{
Bram Moolenaar1e607892006-03-13 22:15:53 +00003295 while (buf->b_u_oldhead != NULL)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003296 u_freeheader(buf, buf->b_u_oldhead, NULL);
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003297 vim_free(buf->b_u_line_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298}
3299
3300/*
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003301 * u_save_line(): allocate memory and copy line 'lnum' into it.
3302 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 */
3304 static char_u *
3305u_save_line(lnum)
3306 linenr_T lnum;
3307{
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003308 return vim_strsave(ml_get(lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309}
3310
3311/*
3312 * Check if the 'modified' flag is set, or 'ff' has changed (only need to
3313 * check the first character, because it can only be "dos", "unix" or "mac").
3314 * "nofile" and "scratch" type buffers are considered to always be unchanged.
3315 */
3316 int
3317bufIsChanged(buf)
3318 buf_T *buf;
3319{
3320 return
3321#ifdef FEAT_QUICKFIX
3322 !bt_dontwrite(buf) &&
3323#endif
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003324 (buf->b_changed || file_ff_differs(buf, TRUE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325}
3326
3327 int
3328curbufIsChanged()
3329{
3330 return
3331#ifdef FEAT_QUICKFIX
3332 !bt_dontwrite(curbuf) &&
3333#endif
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003334 (curbuf->b_changed || file_ff_differs(curbuf, TRUE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335}
Bram Moolenaara800b422010-06-27 01:15:55 +02003336
3337#if defined(FEAT_EVAL) || defined(PROTO)
3338/*
3339 * For undotree(): Append the list of undo blocks at "first_uhp" to "list".
3340 * Recursive.
3341 */
3342 void
3343u_eval_tree(first_uhp, list)
3344 u_header_T *first_uhp;
3345 list_T *list;
3346{
3347 u_header_T *uhp = first_uhp;
3348 dict_T *dict;
3349
3350 while (uhp != NULL)
3351 {
3352 dict = dict_alloc();
3353 if (dict == NULL)
3354 return;
3355 dict_add_nr_str(dict, "seq", uhp->uh_seq, NULL);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02003356 dict_add_nr_str(dict, "time", (long)uhp->uh_time, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +02003357 if (uhp == curbuf->b_u_newhead)
3358 dict_add_nr_str(dict, "newhead", 1, NULL);
3359 if (uhp == curbuf->b_u_curhead)
3360 dict_add_nr_str(dict, "curhead", 1, NULL);
3361 if (uhp->uh_save_nr > 0)
3362 dict_add_nr_str(dict, "save", uhp->uh_save_nr, NULL);
3363
3364 if (uhp->uh_alt_next.ptr != NULL)
3365 {
3366 list_T *alt_list = list_alloc();
3367
3368 if (alt_list != NULL)
3369 {
3370 /* Recursive call to add alternate undo tree. */
3371 u_eval_tree(uhp->uh_alt_next.ptr, alt_list);
3372 dict_add_list(dict, "alt", alt_list);
3373 }
3374 }
3375
3376 list_append_dict(list, dict);
3377 uhp = uhp->uh_prev.ptr;
3378 }
3379}
3380#endif