blob: 86b4d81d409092da3e52b41456ee7c0b757888ab [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * undo.c: multi level undo facility
12 *
13 * The saved lines are stored in a list of lists (one for each buffer):
14 *
15 * b_u_oldhead------------------------------------------------+
16 * |
17 * V
18 * +--------------+ +--------------+ +--------------+
19 * b_u_newhead--->| u_header | | u_header | | u_header |
20 * | uh_next------>| uh_next------>| uh_next---->NULL
21 * NULL<--------uh_prev |<---------uh_prev |<---------uh_prev |
22 * | uh_entry | | uh_entry | | uh_entry |
23 * +--------|-----+ +--------|-----+ +--------|-----+
24 * | | |
25 * V V V
26 * +--------------+ +--------------+ +--------------+
27 * | u_entry | | u_entry | | u_entry |
28 * | ue_next | | ue_next | | ue_next |
29 * +--------|-----+ +--------|-----+ +--------|-----+
30 * | | |
31 * V V V
32 * +--------------+ NULL NULL
33 * | u_entry |
34 * | ue_next |
35 * +--------|-----+
36 * |
37 * V
38 * etc.
39 *
40 * Each u_entry list contains the information for one undo or redo.
41 * curbuf->b_u_curhead points to the header of the last undo (the next redo),
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +000042 * or is NULL if nothing has been undone (end of the branch).
Bram Moolenaar071d4272004-06-13 20:20:40 +000043 *
Bram Moolenaar1e607892006-03-13 22:15:53 +000044 * For keeping alternate undo/redo branches the uh_alt field is used. Thus at
45 * each point in the list a branch may appear for an alternate to redo. The
46 * uh_seq field is numbered sequentially to be able to find a newer or older
47 * branch.
48 *
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +000049 * +---------------+ +---------------+
50 * b_u_oldhead --->| u_header | | u_header |
51 * | uh_alt_next ---->| uh_alt_next ----> NULL
52 * NULL <----- uh_alt_prev |<------ uh_alt_prev |
53 * | uh_prev | | uh_prev |
54 * +-----|---------+ +-----|---------+
55 * | |
56 * V V
57 * +---------------+ +---------------+
58 * | u_header | | u_header |
59 * | uh_alt_next | | uh_alt_next |
60 * b_u_newhead --->| uh_alt_prev | | uh_alt_prev |
61 * | uh_prev | | uh_prev |
62 * +-----|---------+ +-----|---------+
63 * | |
64 * V V
65 * NULL +---------------+ +---------------+
66 * | u_header | | u_header |
67 * | uh_alt_next ---->| uh_alt_next |
68 * | uh_alt_prev |<------ uh_alt_prev |
69 * | uh_prev | | uh_prev |
70 * +-----|---------+ +-----|---------+
71 * | |
72 * etc. etc.
73 *
74 *
Bram Moolenaarf05e3b02010-05-29 15:40:47 +020075 * All data is allocated and will all be freed when the buffer is unloaded.
Bram Moolenaar071d4272004-06-13 20:20:40 +000076 */
77
Bram Moolenaarfecb6602007-10-01 20:54:15 +000078/* Uncomment the next line for including the u_check() function. This warns
79 * for errors in the debug information. */
80/* #define U_DEBUG 1 */
81#define UH_MAGIC 0x18dade /* value for uh_magic when in use */
82#define UE_MAGIC 0xabc123 /* value for ue_magic when in use */
83
Bram Moolenaar442b4222010-05-24 21:34:22 +020084#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
85# include "vimio.h" /* for vim_read(), must be before vim.h */
86#endif
87
Bram Moolenaar071d4272004-06-13 20:20:40 +000088#include "vim.h"
89
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +000090static void u_unch_branch __ARGS((u_header_T *uhp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000091static u_entry_T *u_get_headentry __ARGS((void));
92static void u_getbot __ARGS((void));
93static int u_savecommon __ARGS((linenr_T, linenr_T, linenr_T));
94static void u_doit __ARGS((int count));
Bram Moolenaarca003e12006-03-17 23:19:38 +000095static void u_undoredo __ARGS((int undo));
Bram Moolenaardb552d602006-03-23 22:59:57 +000096static void u_undo_end __ARGS((int did_undo, int absolute));
Bram Moolenaarefd2bf12006-03-16 21:41:35 +000097static void u_add_time __ARGS((char_u *buf, size_t buflen, time_t tt));
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +000098static void u_freeheader __ARGS((buf_T *buf, u_header_T *uhp, u_header_T **uhpp));
Bram Moolenaar1e607892006-03-13 22:15:53 +000099static void u_freebranch __ARGS((buf_T *buf, u_header_T *uhp, u_header_T **uhpp));
100static void u_freeentries __ARGS((buf_T *buf, u_header_T *uhp, u_header_T **uhpp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101static void u_freeentry __ARGS((u_entry_T *, long));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200102#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200103static void corruption_error __ARGS((char *mesg, char_u *file_name));
Bram Moolenaar6a18eb62010-05-26 21:21:00 +0200104static void u_free_uhp __ARGS((u_header_T *uhp));
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200105static size_t fwrite_crypt __ARGS((buf_T *buf UNUSED, char_u *ptr, size_t len, FILE *fp));
106static char_u *read_string_decrypt __ARGS((buf_T *buf UNUSED, FILE *fd, int len));
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +0200107static int serialize_header __ARGS((FILE *fp, buf_T *buf, char_u *hash));
Bram Moolenaara3ff49f2010-05-30 22:48:02 +0200108static int serialize_uhp __ARGS((FILE *fp, buf_T *buf, u_header_T *uhp));
Bram Moolenaara800b422010-06-27 01:15:55 +0200109static u_header_T *unserialize_uhp __ARGS((FILE *fp, char_u *file_name, int new_version));
Bram Moolenaara3ff49f2010-05-30 22:48:02 +0200110static int serialize_uep __ARGS((FILE *fp, buf_T *buf, u_entry_T *uep));
Bram Moolenaar9db58062010-05-29 20:33:07 +0200111static u_entry_T *unserialize_uep __ARGS((FILE *fp, int *error, char_u *file_name));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200112static void serialize_pos __ARGS((pos_T pos, FILE *fp));
Bram Moolenaar9db58062010-05-29 20:33:07 +0200113static void unserialize_pos __ARGS((pos_T *pos, FILE *fp));
Bram Moolenaarcdf04202010-05-29 15:11:47 +0200114static void serialize_visualinfo __ARGS((visualinfo_T *info, FILE *fp));
Bram Moolenaar9db58062010-05-29 20:33:07 +0200115static void unserialize_visualinfo __ARGS((visualinfo_T *info, FILE *fp));
116static void put_header_ptr __ARGS((FILE *fp, u_header_T *uhp));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
Bram Moolenaarf05e3b02010-05-29 15:40:47 +0200119#define U_ALLOC_LINE(size) lalloc((long_u)(size), FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120static char_u *u_save_line __ARGS((linenr_T));
121
Bram Moolenaar730cde92010-06-27 05:18:54 +0200122/* used in undo_end() to report number of added and deleted lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123static long u_newcount, u_oldcount;
124
125/*
126 * When 'u' flag included in 'cpoptions', we behave like vi. Need to remember
127 * the action that "u" should do.
128 */
129static int undo_undoes = FALSE;
130
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200131static int lastmark = 0;
132
Bram Moolenaar9db58062010-05-29 20:33:07 +0200133#if defined(U_DEBUG) || defined(PROTO)
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000134/*
135 * Check the undo structures for being valid. Print a warning when something
136 * looks wrong.
137 */
138static int seen_b_u_curhead;
139static int seen_b_u_newhead;
140static int header_count;
141
142 static void
143u_check_tree(u_header_T *uhp,
144 u_header_T *exp_uh_next,
145 u_header_T *exp_uh_alt_prev)
146{
147 u_entry_T *uep;
148
149 if (uhp == NULL)
150 return;
151 ++header_count;
152 if (uhp == curbuf->b_u_curhead && ++seen_b_u_curhead > 1)
153 {
154 EMSG("b_u_curhead found twice (looping?)");
155 return;
156 }
157 if (uhp == curbuf->b_u_newhead && ++seen_b_u_newhead > 1)
158 {
159 EMSG("b_u_newhead found twice (looping?)");
160 return;
161 }
162
163 if (uhp->uh_magic != UH_MAGIC)
164 EMSG("uh_magic wrong (may be using freed memory)");
165 else
166 {
167 /* Check pointers back are correct. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200168 if (uhp->uh_next.ptr != exp_uh_next)
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000169 {
170 EMSG("uh_next wrong");
171 smsg((char_u *)"expected: 0x%x, actual: 0x%x",
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200172 exp_uh_next, uhp->uh_next.ptr);
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000173 }
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200174 if (uhp->uh_alt_prev.ptr != exp_uh_alt_prev)
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000175 {
176 EMSG("uh_alt_prev wrong");
177 smsg((char_u *)"expected: 0x%x, actual: 0x%x",
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200178 exp_uh_alt_prev, uhp->uh_alt_prev.ptr);
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000179 }
180
181 /* Check the undo tree at this header. */
182 for (uep = uhp->uh_entry; uep != NULL; uep = uep->ue_next)
183 {
184 if (uep->ue_magic != UE_MAGIC)
185 {
186 EMSG("ue_magic wrong (may be using freed memory)");
187 break;
188 }
189 }
190
191 /* Check the next alt tree. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200192 u_check_tree(uhp->uh_alt_next.ptr, uhp->uh_next.ptr, uhp);
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000193
194 /* Check the next header in this branch. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200195 u_check_tree(uhp->uh_prev.ptr, uhp, NULL);
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000196 }
197}
198
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200199 static void
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000200u_check(int newhead_may_be_NULL)
201{
202 seen_b_u_newhead = 0;
203 seen_b_u_curhead = 0;
204 header_count = 0;
205
206 u_check_tree(curbuf->b_u_oldhead, NULL, NULL);
207
208 if (seen_b_u_newhead == 0 && curbuf->b_u_oldhead != NULL
209 && !(newhead_may_be_NULL && curbuf->b_u_newhead == NULL))
210 EMSGN("b_u_newhead invalid: 0x%x", curbuf->b_u_newhead);
211 if (curbuf->b_u_curhead != NULL && seen_b_u_curhead == 0)
212 EMSGN("b_u_curhead invalid: 0x%x", curbuf->b_u_curhead);
213 if (header_count != curbuf->b_u_numhead)
214 {
215 EMSG("b_u_numhead invalid");
216 smsg((char_u *)"expected: %ld, actual: %ld",
217 (long)header_count, (long)curbuf->b_u_numhead);
218 }
219}
220#endif
221
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000223 * Save the current line for both the "u" and "U" command.
224 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 */
226 int
227u_save_cursor()
228{
229 return (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
230 (linenr_T)(curwin->w_cursor.lnum + 1)));
231}
232
233/*
234 * Save the lines between "top" and "bot" for both the "u" and "U" command.
235 * "top" may be 0 and bot may be curbuf->b_ml.ml_line_count + 1.
236 * Returns FAIL when lines could not be saved, OK otherwise.
237 */
238 int
239u_save(top, bot)
240 linenr_T top, bot;
241{
242 if (undo_off)
243 return OK;
244
245 if (top > curbuf->b_ml.ml_line_count ||
246 top >= bot || bot > curbuf->b_ml.ml_line_count + 1)
247 return FALSE; /* rely on caller to do error messages */
248
249 if (top + 2 == bot)
250 u_saveline((linenr_T)(top + 1));
251
252 return (u_savecommon(top, bot, (linenr_T)0));
253}
254
255/*
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200256 * Save the line "lnum" (used by ":s" and "~" command).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 * The line is replaced, so the new bottom line is lnum + 1.
258 */
259 int
260u_savesub(lnum)
261 linenr_T lnum;
262{
263 if (undo_off)
264 return OK;
265
266 return (u_savecommon(lnum - 1, lnum + 1, lnum + 1));
267}
268
269/*
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200270 * A new line is inserted before line "lnum" (used by :s command).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271 * The line is inserted, so the new bottom line is lnum + 1.
272 */
273 int
274u_inssub(lnum)
275 linenr_T lnum;
276{
277 if (undo_off)
278 return OK;
279
280 return (u_savecommon(lnum - 1, lnum, lnum + 1));
281}
282
283/*
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200284 * Save the lines "lnum" - "lnum" + nlines (used by delete command).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 * The lines are deleted, so the new bottom line is lnum, unless the buffer
286 * becomes empty.
287 */
288 int
289u_savedel(lnum, nlines)
290 linenr_T lnum;
291 long nlines;
292{
293 if (undo_off)
294 return OK;
295
296 return (u_savecommon(lnum - 1, lnum + nlines,
297 nlines == curbuf->b_ml.ml_line_count ? 2 : lnum));
298}
299
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000300/*
301 * Return TRUE when undo is allowed. Otherwise give an error message and
302 * return FALSE.
303 */
Bram Moolenaarce6ef252006-07-12 19:49:41 +0000304 int
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000305undo_allowed()
306{
307 /* Don't allow changes when 'modifiable' is off. */
308 if (!curbuf->b_p_ma)
309 {
310 EMSG(_(e_modifiable));
311 return FALSE;
312 }
313
314#ifdef HAVE_SANDBOX
315 /* In the sandbox it's not allowed to change the text. */
316 if (sandbox != 0)
317 {
318 EMSG(_(e_sandbox));
319 return FALSE;
320 }
321#endif
322
323 /* Don't allow changes in the buffer while editing the cmdline. The
324 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000325 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000326 {
327 EMSG(_(e_secure));
328 return FALSE;
329 }
330
331 return TRUE;
332}
333
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200334/*
335 * Common code for various ways to save text before a change.
336 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 static int
338u_savecommon(top, bot, newbot)
339 linenr_T top, bot;
340 linenr_T newbot;
341{
Bram Moolenaar1e607892006-03-13 22:15:53 +0000342 linenr_T lnum;
343 long i;
344 u_header_T *uhp;
345 u_header_T *old_curhead;
346 u_entry_T *uep;
347 u_entry_T *prev_uep;
348 long size;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000350 /* When making changes is not allowed return FAIL. It's a crude way to
351 * make all change commands fail. */
352 if (!undo_allowed())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000355#ifdef U_DEBUG
356 u_check(FALSE);
357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358#ifdef FEAT_NETBEANS_INTG
359 /*
360 * Netbeans defines areas that cannot be modified. Bail out here when
361 * trying to change text in a guarded area.
362 */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200363 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 {
Bram Moolenaar009b2592004-10-24 19:18:58 +0000365 if (netbeans_is_guarded(top, bot))
366 {
367 EMSG(_(e_guarded));
368 return FAIL;
369 }
370 if (curbuf->b_p_ro)
371 {
372 EMSG(_(e_nbreadonly));
373 return FAIL;
374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 }
376#endif
377
378#ifdef FEAT_AUTOCMD
379 /*
380 * Saving text for undo means we are going to make a change. Give a
381 * warning for a read-only file before making the change, so that the
382 * FileChangedRO event can replace the buffer with a read-write version
383 * (e.g., obtained from a source control system).
384 */
385 change_warning(0);
386#endif
387
388 size = bot - top - 1;
389
390 /*
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200391 * If curbuf->b_u_synced == TRUE make a new header.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392 */
393 if (curbuf->b_u_synced)
394 {
395#ifdef FEAT_JUMPLIST
396 /* Need to create new entry in b_changelist. */
397 curbuf->b_new_change = TRUE;
398#endif
399
Bram Moolenaar1e607892006-03-13 22:15:53 +0000400 if (p_ul >= 0)
401 {
402 /*
403 * Make a new header entry. Do this first so that we don't mess
404 * up the undo info when out of memory.
405 */
Bram Moolenaarf05e3b02010-05-29 15:40:47 +0200406 uhp = (u_header_T *)U_ALLOC_LINE(sizeof(u_header_T));
Bram Moolenaar1e607892006-03-13 22:15:53 +0000407 if (uhp == NULL)
408 goto nomem;
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000409#ifdef U_DEBUG
410 uhp->uh_magic = UH_MAGIC;
411#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +0000412 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000413 else
414 uhp = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000415
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 /*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000417 * If we undid more than we redid, move the entry lists before and
418 * including curbuf->b_u_curhead to an alternate branch.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 */
Bram Moolenaar1e607892006-03-13 22:15:53 +0000420 old_curhead = curbuf->b_u_curhead;
421 if (old_curhead != NULL)
422 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200423 curbuf->b_u_newhead = old_curhead->uh_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000424 curbuf->b_u_curhead = NULL;
425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426
427 /*
428 * free headers to keep the size right
429 */
430 while (curbuf->b_u_numhead > p_ul && curbuf->b_u_oldhead != NULL)
Bram Moolenaar1e607892006-03-13 22:15:53 +0000431 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000432 u_header_T *uhfree = curbuf->b_u_oldhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000433
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000434 if (uhfree == old_curhead)
435 /* Can't reconnect the branch, delete all of it. */
436 u_freebranch(curbuf, uhfree, &old_curhead);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200437 else if (uhfree->uh_alt_next.ptr == NULL)
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000438 /* There is no branch, only free one header. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000439 u_freeheader(curbuf, uhfree, &old_curhead);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000440 else
441 {
442 /* Free the oldest alternate branch as a whole. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200443 while (uhfree->uh_alt_next.ptr != NULL)
444 uhfree = uhfree->uh_alt_next.ptr;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000445 u_freebranch(curbuf, uhfree, &old_curhead);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000446 }
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000447#ifdef U_DEBUG
448 u_check(TRUE);
449#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +0000450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000452 if (uhp == NULL) /* no undo at all */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 {
Bram Moolenaar1e607892006-03-13 22:15:53 +0000454 if (old_curhead != NULL)
455 u_freebranch(curbuf, old_curhead, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 curbuf->b_u_synced = FALSE;
457 return OK;
458 }
459
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200460 uhp->uh_prev.ptr = NULL;
461 uhp->uh_next.ptr = curbuf->b_u_newhead;
462 uhp->uh_alt_next.ptr = old_curhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000463 if (old_curhead != NULL)
464 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200465 uhp->uh_alt_prev.ptr = old_curhead->uh_alt_prev.ptr;
466 if (uhp->uh_alt_prev.ptr != NULL)
467 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = uhp;
468 old_curhead->uh_alt_prev.ptr = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000469 if (curbuf->b_u_oldhead == old_curhead)
470 curbuf->b_u_oldhead = uhp;
471 }
Bram Moolenaar89ed3df2007-01-09 19:23:12 +0000472 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200473 uhp->uh_alt_prev.ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 if (curbuf->b_u_newhead != NULL)
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200475 curbuf->b_u_newhead->uh_prev.ptr = uhp;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000476
Bram Moolenaarca003e12006-03-17 23:19:38 +0000477 uhp->uh_seq = ++curbuf->b_u_seq_last;
478 curbuf->b_u_seq_cur = uhp->uh_seq;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000479 uhp->uh_time = time(NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +0200480 uhp->uh_save_nr = 0;
481 curbuf->b_u_time_cur = uhp->uh_time + 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000482
Bram Moolenaar1e607892006-03-13 22:15:53 +0000483 uhp->uh_walk = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 uhp->uh_entry = NULL;
485 uhp->uh_getbot_entry = NULL;
486 uhp->uh_cursor = curwin->w_cursor; /* save cursor pos. for undo */
487#ifdef FEAT_VIRTUALEDIT
488 if (virtual_active() && curwin->w_cursor.coladd > 0)
489 uhp->uh_cursor_vcol = getviscol();
490 else
491 uhp->uh_cursor_vcol = -1;
492#endif
493
494 /* save changed and buffer empty flag for undo */
495 uhp->uh_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
496 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
497
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000498 /* save named marks and Visual marks for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 mch_memmove(uhp->uh_namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000500#ifdef FEAT_VISUAL
501 uhp->uh_visual = curbuf->b_visual;
502#endif
503
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 curbuf->b_u_newhead = uhp;
505 if (curbuf->b_u_oldhead == NULL)
506 curbuf->b_u_oldhead = uhp;
507 ++curbuf->b_u_numhead;
508 }
509 else
510 {
511 if (p_ul < 0) /* no undo at all */
512 return OK;
513
514 /*
515 * When saving a single line, and it has been saved just before, it
516 * doesn't make sense saving it again. Saves a lot of memory when
517 * making lots of changes inside the same line.
518 * This is only possible if the previous change didn't increase or
519 * decrease the number of lines.
520 * Check the ten last changes. More doesn't make sense and takes too
521 * long.
522 */
523 if (size == 1)
524 {
525 uep = u_get_headentry();
526 prev_uep = NULL;
527 for (i = 0; i < 10; ++i)
528 {
529 if (uep == NULL)
530 break;
531
532 /* If lines have been inserted/deleted we give up.
533 * Also when the line was included in a multi-line save. */
534 if ((curbuf->b_u_newhead->uh_getbot_entry != uep
535 ? (uep->ue_top + uep->ue_size + 1
536 != (uep->ue_bot == 0
537 ? curbuf->b_ml.ml_line_count + 1
538 : uep->ue_bot))
539 : uep->ue_lcount != curbuf->b_ml.ml_line_count)
540 || (uep->ue_size > 1
541 && top >= uep->ue_top
542 && top + 2 <= uep->ue_top + uep->ue_size + 1))
543 break;
544
545 /* If it's the same line we can skip saving it again. */
546 if (uep->ue_size == 1 && uep->ue_top == top)
547 {
548 if (i > 0)
549 {
550 /* It's not the last entry: get ue_bot for the last
551 * entry now. Following deleted/inserted lines go to
552 * the re-used entry. */
553 u_getbot();
554 curbuf->b_u_synced = FALSE;
555
556 /* Move the found entry to become the last entry. The
557 * order of undo/redo doesn't matter for the entries
558 * we move it over, since they don't change the line
559 * count and don't include this line. It does matter
560 * for the found entry if the line count is changed by
561 * the executed command. */
562 prev_uep->ue_next = uep->ue_next;
563 uep->ue_next = curbuf->b_u_newhead->uh_entry;
564 curbuf->b_u_newhead->uh_entry = uep;
565 }
566
567 /* The executed command may change the line count. */
568 if (newbot != 0)
569 uep->ue_bot = newbot;
570 else if (bot > curbuf->b_ml.ml_line_count)
571 uep->ue_bot = 0;
572 else
573 {
574 uep->ue_lcount = curbuf->b_ml.ml_line_count;
575 curbuf->b_u_newhead->uh_getbot_entry = uep;
576 }
577 return OK;
578 }
579 prev_uep = uep;
580 uep = uep->ue_next;
581 }
582 }
583
584 /* find line number for ue_bot for previous u_save() */
585 u_getbot();
586 }
587
588#if !defined(UNIX) && !defined(DJGPP) && !defined(WIN32) && !defined(__EMX__)
589 /*
590 * With Amiga and MSDOS 16 bit we can't handle big undo's, because
591 * then u_alloc_line would have to allocate a block larger than 32K
592 */
593 if (size >= 8000)
594 goto nomem;
595#endif
596
597 /*
598 * add lines in front of entry list
599 */
Bram Moolenaarf05e3b02010-05-29 15:40:47 +0200600 uep = (u_entry_T *)U_ALLOC_LINE(sizeof(u_entry_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 if (uep == NULL)
602 goto nomem;
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200603 vim_memset(uep, 0, sizeof(u_entry_T));
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000604#ifdef U_DEBUG
605 uep->ue_magic = UE_MAGIC;
606#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607
608 uep->ue_size = size;
609 uep->ue_top = top;
610 if (newbot != 0)
611 uep->ue_bot = newbot;
612 /*
613 * Use 0 for ue_bot if bot is below last line.
614 * Otherwise we have to compute ue_bot later.
615 */
616 else if (bot > curbuf->b_ml.ml_line_count)
617 uep->ue_bot = 0;
618 else
619 {
620 uep->ue_lcount = curbuf->b_ml.ml_line_count;
621 curbuf->b_u_newhead->uh_getbot_entry = uep;
622 }
623
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000624 if (size > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000626 if ((uep->ue_array = (char_u **)U_ALLOC_LINE(
Bram Moolenaarf05e3b02010-05-29 15:40:47 +0200627 sizeof(char_u *) * size)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 {
629 u_freeentry(uep, 0L);
630 goto nomem;
631 }
632 for (i = 0, lnum = top + 1; i < size; ++i)
633 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000634 fast_breakcheck();
635 if (got_int)
636 {
637 u_freeentry(uep, i);
638 return FAIL;
639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 if ((uep->ue_array[i] = u_save_line(lnum++)) == NULL)
641 {
642 u_freeentry(uep, i);
643 goto nomem;
644 }
645 }
646 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000647 else
648 uep->ue_array = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 uep->ue_next = curbuf->b_u_newhead->uh_entry;
650 curbuf->b_u_newhead->uh_entry = uep;
651 curbuf->b_u_synced = FALSE;
652 undo_undoes = FALSE;
653
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000654#ifdef U_DEBUG
655 u_check(FALSE);
656#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 return OK;
658
659nomem:
660 msg_silent = 0; /* must display the prompt */
661 if (ask_yesno((char_u *)_("No undo possible; continue anyway"), TRUE)
662 == 'y')
663 {
664 undo_off = TRUE; /* will be reset when character typed */
665 return OK;
666 }
667 do_outofmem_msg((long_u)0);
668 return FAIL;
669}
670
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200671#if defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200672
Bram Moolenaar9db58062010-05-29 20:33:07 +0200673# define UF_START_MAGIC "Vim\237UnDo\345" /* magic at start of undofile */
674# define UF_START_MAGIC_LEN 9
675# define UF_HEADER_MAGIC 0x5fd0 /* magic at start of header */
676# define UF_HEADER_END_MAGIC 0xe7aa /* magic after last header */
677# define UF_ENTRY_MAGIC 0xf518 /* magic at start of entry */
678# define UF_ENTRY_END_MAGIC 0x3581 /* magic after last entry */
Bram Moolenaara800b422010-06-27 01:15:55 +0200679# define UF_VERSION_PREV 1 /* 2-byte undofile version number */
680# define UF_VERSION 2 /* 2-byte undofile version number */
681# define UF_VERSION_CRYPT_PREV 0x8001 /* idem, encrypted */
682# define UF_VERSION_CRYPT 0x8002 /* idem, encrypted */
683
684/* extra fields for header */
685# define UF_LAST_SAVE_NR 1
686
687/* extra fields for uhp */
688# define UHP_SAVE_NR 1
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200689
Bram Moolenaarcdf04202010-05-29 15:11:47 +0200690static char_u e_not_open[] = N_("E828: Cannot open undo file for writing: %s");
Bram Moolenaarcdf04202010-05-29 15:11:47 +0200691
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200692/*
693 * Compute the hash for the current buffer text into hash[UNDO_HASH_SIZE].
694 */
695 void
696u_compute_hash(hash)
697 char_u *hash;
698{
699 context_sha256_T ctx;
700 linenr_T lnum;
701 char_u *p;
702
703 sha256_start(&ctx);
704 for (lnum = 1; lnum < curbuf->b_ml.ml_line_count; ++lnum)
705 {
706 p = ml_get(lnum);
Bram Moolenaar442b4222010-05-24 21:34:22 +0200707 sha256_update(&ctx, p, (UINT32_T)(STRLEN(p) + 1));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200708 }
709 sha256_finish(&ctx, hash);
710}
711
712/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200713 * Return an allocated string of the full path of the target undofile.
714 * When "reading" is TRUE find the file to read, go over all directories in
715 * 'undodir'.
716 * When "reading" is FALSE use the first name where the directory exists.
Bram Moolenaar9db58062010-05-29 20:33:07 +0200717 * Returns NULL when there is no place to write or no file to read.
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200718 */
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200719 char_u *
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200720u_get_undo_file_name(buf_ffname, reading)
721 char_u *buf_ffname;
722 int reading;
723{
724 char_u *dirp;
725 char_u dir_name[IOSIZE + 1];
726 char_u *munged_name = NULL;
727 char_u *undo_file_name = NULL;
728 int dir_len;
729 char_u *p;
730 struct stat st;
731 char_u *ffname = buf_ffname;
732#ifdef HAVE_READLINK
733 char_u fname_buf[MAXPATHL];
734#endif
735
736 if (ffname == NULL)
737 return NULL;
738
739#ifdef HAVE_READLINK
740 /* Expand symlink in the file name, so that we put the undo file with the
741 * actual file instead of with the symlink. */
742 if (resolve_symlink(ffname, fname_buf) == OK)
743 ffname = fname_buf;
744#endif
745
746 /* Loop over 'undodir'. When reading find the first file that exists.
747 * When not reading use the first directory that exists or ".". */
748 dirp = p_udir;
749 while (*dirp != NUL)
750 {
751 dir_len = copy_option_part(&dirp, dir_name, IOSIZE, ",");
752 if (dir_len == 1 && dir_name[0] == '.')
753 {
754 /* Use same directory as the ffname,
755 * "dir/name" -> "dir/.name.un~" */
Bram Moolenaar442b4222010-05-24 21:34:22 +0200756 undo_file_name = vim_strnsave(ffname, (int)(STRLEN(ffname) + 5));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200757 if (undo_file_name == NULL)
758 break;
759 p = gettail(undo_file_name);
760 mch_memmove(p + 1, p, STRLEN(p) + 1);
761 *p = '.';
762 STRCAT(p, ".un~");
763 }
764 else
765 {
766 dir_name[dir_len] = NUL;
767 if (mch_isdir(dir_name))
768 {
769 if (munged_name == NULL)
770 {
771 munged_name = vim_strsave(ffname);
772 if (munged_name == NULL)
773 return NULL;
774 for (p = munged_name; *p != NUL; mb_ptr_adv(p))
775 if (vim_ispathsep(*p))
776 *p = '%';
777 }
778 undo_file_name = concat_fnames(dir_name, munged_name, TRUE);
779 }
780 }
781
782 /* When reading check if the file exists. */
783 if (undo_file_name != NULL && (!reading
784 || mch_stat((char *)undo_file_name, &st) >= 0))
785 break;
786 vim_free(undo_file_name);
787 undo_file_name = NULL;
788 }
789
790 vim_free(munged_name);
791 return undo_file_name;
792}
793
Bram Moolenaar9db58062010-05-29 20:33:07 +0200794 static void
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200795corruption_error(mesg, file_name)
796 char *mesg;
Bram Moolenaar9db58062010-05-29 20:33:07 +0200797 char_u *file_name;
798{
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200799 EMSG3(_("E825: Corrupted undo file (%s): %s"), mesg, file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +0200800}
801
802 static void
803u_free_uhp(uhp)
804 u_header_T *uhp;
805{
806 u_entry_T *nuep;
807 u_entry_T *uep;
808
809 uep = uhp->uh_entry;
810 while (uep != NULL)
811 {
812 nuep = uep->ue_next;
813 u_freeentry(uep, uep->ue_size);
814 uep = nuep;
815 }
816 vim_free(uhp);
817}
818
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200819/*
820 * Like fwrite() but crypt the bytes when 'key' is set.
821 * Returns 1 if successful.
822 */
823 static size_t
824fwrite_crypt(buf, ptr, len, fp)
825 buf_T *buf UNUSED;
826 char_u *ptr;
827 size_t len;
828 FILE *fp;
829{
830#ifdef FEAT_CRYPT
831 char_u *copy;
832 char_u small_buf[100];
833 size_t i;
834
835 if (*buf->b_p_key == NUL)
836 return fwrite(ptr, len, (size_t)1, fp);
837 if (len < 100)
838 copy = small_buf; /* no malloc()/free() for short strings */
839 else
840 {
841 copy = lalloc(len, FALSE);
842 if (copy == NULL)
843 return 0;
844 }
845 crypt_encode(ptr, len, copy);
846 i = fwrite(copy, len, (size_t)1, fp);
847 if (copy != small_buf)
848 vim_free(copy);
849 return i;
850#else
851 return fwrite(ptr, len, (size_t)1, fp);
852#endif
853}
854
855/*
856 * Read a string of length "len" from "fd".
857 * When 'key' is set decrypt the bytes.
858 */
859 static char_u *
860read_string_decrypt(buf, fd, len)
861 buf_T *buf UNUSED;
862 FILE *fd;
863 int len;
864{
865 char_u *ptr;
866
867 ptr = read_string(fd, len);
868#ifdef FEAT_CRYPT
869 if (ptr != NULL || *buf->b_p_key != NUL)
870 crypt_decode(ptr, len);
871#endif
872 return ptr;
873}
874
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +0200875 static int
876serialize_header(fp, buf, hash)
877 FILE *fp;
878 buf_T *buf;
879 char_u *hash;
880{
881 int len;
882
883 /* Start writing, first the magic marker and undo info version. */
884 if (fwrite(UF_START_MAGIC, (size_t)UF_START_MAGIC_LEN, (size_t)1, fp) != 1)
885 return FAIL;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +0200886
887 /* If the buffer is encrypted then all text bytes following will be
888 * encrypted. Numbers and other info is not crypted. */
889#ifdef FEAT_CRYPT
890 if (*buf->b_p_key)
891 {
892 char_u *header;
893 int header_len;
894
895 put_bytes(fp, (long_u)UF_VERSION_CRYPT, 2);
896 header = prepare_crypt_write(buf, &header_len);
897 if (header == NULL)
898 return FAIL;
Bram Moolenaarbbd6afe2010-06-02 20:32:23 +0200899 len = (int)fwrite(header, (size_t)header_len, (size_t)1, fp);
Bram Moolenaara3ff49f2010-05-30 22:48:02 +0200900 vim_free(header);
901 if (len != 1)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200902 {
903 crypt_pop_state();
Bram Moolenaara3ff49f2010-05-30 22:48:02 +0200904 return FAIL;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200905 }
Bram Moolenaara3ff49f2010-05-30 22:48:02 +0200906 }
907 else
908#endif
909 put_bytes(fp, (long_u)UF_VERSION, 2);
910
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +0200911
912 /* Write a hash of the buffer text, so that we can verify it is still the
913 * same when reading the buffer text. */
914 if (fwrite(hash, (size_t)UNDO_HASH_SIZE, (size_t)1, fp) != 1)
915 return FAIL;
916
917 /* buffer-specific data */
918 put_bytes(fp, (long_u)buf->b_ml.ml_line_count, 4);
919 len = buf->b_u_line_ptr != NULL ? (int)STRLEN(buf->b_u_line_ptr) : 0;
920 put_bytes(fp, (long_u)len, 4);
Bram Moolenaara3ff49f2010-05-30 22:48:02 +0200921 if (len > 0 && fwrite_crypt(buf, buf->b_u_line_ptr, (size_t)len, fp) != 1)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +0200922 return FAIL;
923 put_bytes(fp, (long_u)buf->b_u_line_lnum, 4);
924 put_bytes(fp, (long_u)buf->b_u_line_colnr, 4);
925
926 /* Undo structures header data */
927 put_header_ptr(fp, buf->b_u_oldhead);
928 put_header_ptr(fp, buf->b_u_newhead);
929 put_header_ptr(fp, buf->b_u_curhead);
930
931 put_bytes(fp, (long_u)buf->b_u_numhead, 4);
932 put_bytes(fp, (long_u)buf->b_u_seq_last, 4);
933 put_bytes(fp, (long_u)buf->b_u_seq_cur, 4);
Bram Moolenaara800b422010-06-27 01:15:55 +0200934 put_time(fp, buf->b_u_time_cur);
935
936 /* Optional fields. */
937 putc(4, fp);
938 putc(UF_LAST_SAVE_NR, fp);
Bram Moolenaar730cde92010-06-27 05:18:54 +0200939 put_bytes(fp, (long_u)buf->b_u_save_nr_last, 4);
Bram Moolenaara800b422010-06-27 01:15:55 +0200940
941 putc(0, fp); /* end marker */
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +0200942
943 return OK;
944}
945
946 static int
Bram Moolenaara3ff49f2010-05-30 22:48:02 +0200947serialize_uhp(fp, buf, uhp)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +0200948 FILE *fp;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +0200949 buf_T *buf;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +0200950 u_header_T *uhp;
951{
952 int i;
953 u_entry_T *uep;
954
955 if (put_bytes(fp, (long_u)UF_HEADER_MAGIC, 2) == FAIL)
956 return FAIL;
957
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200958 put_header_ptr(fp, uhp->uh_next.ptr);
959 put_header_ptr(fp, uhp->uh_prev.ptr);
960 put_header_ptr(fp, uhp->uh_alt_next.ptr);
961 put_header_ptr(fp, uhp->uh_alt_prev.ptr);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +0200962 put_bytes(fp, uhp->uh_seq, 4);
963 serialize_pos(uhp->uh_cursor, fp);
964#ifdef FEAT_VIRTUALEDIT
965 put_bytes(fp, (long_u)uhp->uh_cursor_vcol, 4);
966#else
967 put_bytes(fp, (long_u)0, 4);
968#endif
969 put_bytes(fp, (long_u)uhp->uh_flags, 2);
970 /* Assume NMARKS will stay the same. */
971 for (i = 0; i < NMARKS; ++i)
972 serialize_pos(uhp->uh_namedm[i], fp);
973#ifdef FEAT_VISUAL
974 serialize_visualinfo(&uhp->uh_visual, fp);
975#else
976 {
977 visualinfo_T info;
978
979 memset(&info, 0, sizeof(visualinfo_T));
980 serialize_visualinfo(&info, fp);
981 }
982#endif
983 put_time(fp, uhp->uh_time);
984
Bram Moolenaara800b422010-06-27 01:15:55 +0200985 /* Optional fields. */
986 putc(4, fp);
987 putc(UHP_SAVE_NR, fp);
988 put_bytes(fp, (long_u)uhp->uh_save_nr, 4);
989
990 putc(0, fp); /* end marker */
991
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +0200992 /* Write all the entries. */
993 for (uep = uhp->uh_entry; uep != NULL; uep = uep->ue_next)
994 {
995 put_bytes(fp, (long_u)UF_ENTRY_MAGIC, 2);
Bram Moolenaara3ff49f2010-05-30 22:48:02 +0200996 if (serialize_uep(fp, buf, uep) == FAIL)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +0200997 return FAIL;
998 }
999 put_bytes(fp, (long_u)UF_ENTRY_END_MAGIC, 2);
1000 return OK;
1001}
1002
1003 static u_header_T *
Bram Moolenaara800b422010-06-27 01:15:55 +02001004unserialize_uhp(fp, file_name, new_version)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001005 FILE *fp;
1006 char_u *file_name;
Bram Moolenaara800b422010-06-27 01:15:55 +02001007 int new_version;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001008{
1009 u_header_T *uhp;
1010 int i;
1011 u_entry_T *uep, *last_uep;
1012 int c;
1013 int error;
1014
1015 uhp = (u_header_T *)U_ALLOC_LINE(sizeof(u_header_T));
1016 if (uhp == NULL)
1017 return NULL;
1018 vim_memset(uhp, 0, sizeof(u_header_T));
1019#ifdef U_DEBUG
1020 uhp->uh_magic = UH_MAGIC;
1021#endif
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001022 uhp->uh_next.seq = get4c(fp);
1023 uhp->uh_prev.seq = get4c(fp);
1024 uhp->uh_alt_next.seq = get4c(fp);
1025 uhp->uh_alt_prev.seq = get4c(fp);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001026 uhp->uh_seq = get4c(fp);
1027 if (uhp->uh_seq <= 0)
1028 {
1029 corruption_error("uh_seq", file_name);
1030 vim_free(uhp);
1031 return NULL;
1032 }
1033 unserialize_pos(&uhp->uh_cursor, fp);
1034#ifdef FEAT_VIRTUALEDIT
1035 uhp->uh_cursor_vcol = get4c(fp);
1036#else
1037 (void)get4c(fp);
1038#endif
1039 uhp->uh_flags = get2c(fp);
1040 for (i = 0; i < NMARKS; ++i)
1041 unserialize_pos(&uhp->uh_namedm[i], fp);
1042#ifdef FEAT_VISUAL
1043 unserialize_visualinfo(&uhp->uh_visual, fp);
1044#else
1045 {
1046 visualinfo_T info;
1047 unserialize_visualinfo(&info, fp);
1048 }
1049#endif
1050 uhp->uh_time = get8ctime(fp);
1051
Bram Moolenaara800b422010-06-27 01:15:55 +02001052 /* Optional fields. */
1053 if (new_version)
1054 for (;;)
1055 {
1056 int len = getc(fp);
1057 int what;
1058
1059 if (len == 0)
1060 break;
1061 what = getc(fp);
1062 switch (what)
1063 {
1064 case UHP_SAVE_NR:
1065 uhp->uh_save_nr = get4c(fp);
1066 break;
1067 default:
1068 /* field not supported, skip */
1069 while (--len >= 0)
1070 (void)getc(fp);
1071 }
1072 }
1073
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001074 /* Unserialize the uep list. */
1075 last_uep = NULL;
1076 while ((c = get2c(fp)) == UF_ENTRY_MAGIC)
1077 {
1078 error = FALSE;
1079 uep = unserialize_uep(fp, &error, file_name);
1080 if (last_uep == NULL)
1081 uhp->uh_entry = uep;
1082 else
1083 last_uep->ue_next = uep;
1084 last_uep = uep;
1085 if (uep == NULL || error)
1086 {
1087 u_free_uhp(uhp);
1088 return NULL;
1089 }
1090 }
1091 if (c != UF_ENTRY_END_MAGIC)
1092 {
1093 corruption_error("entry end", file_name);
1094 u_free_uhp(uhp);
1095 return NULL;
1096 }
1097
1098 return uhp;
1099}
1100
Bram Moolenaar9db58062010-05-29 20:33:07 +02001101/*
1102 * Serialize "uep" to "fp".
1103 */
1104 static int
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001105serialize_uep(fp, buf, uep)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001106 FILE *fp;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001107 buf_T *buf;
1108 u_entry_T *uep;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001109{
1110 int i;
1111 size_t len;
1112
1113 put_bytes(fp, (long_u)uep->ue_top, 4);
1114 put_bytes(fp, (long_u)uep->ue_bot, 4);
1115 put_bytes(fp, (long_u)uep->ue_lcount, 4);
1116 put_bytes(fp, (long_u)uep->ue_size, 4);
1117 for (i = 0; i < uep->ue_size; ++i)
1118 {
1119 len = STRLEN(uep->ue_array[i]);
1120 if (put_bytes(fp, (long_u)len, 4) == FAIL)
1121 return FAIL;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001122 if (len > 0 && fwrite_crypt(buf, uep->ue_array[i], len, fp) != 1)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001123 return FAIL;
1124 }
1125 return OK;
1126}
1127
1128 static u_entry_T *
1129unserialize_uep(fp, error, file_name)
1130 FILE *fp;
1131 int *error;
1132 char_u *file_name;
1133{
1134 int i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001135 u_entry_T *uep;
1136 char_u **array;
1137 char_u *line;
1138 int line_len;
1139
1140 uep = (u_entry_T *)U_ALLOC_LINE(sizeof(u_entry_T));
1141 if (uep == NULL)
1142 return NULL;
1143 vim_memset(uep, 0, sizeof(u_entry_T));
1144#ifdef U_DEBUG
1145 uep->ue_magic = UE_MAGIC;
1146#endif
1147 uep->ue_top = get4c(fp);
1148 uep->ue_bot = get4c(fp);
1149 uep->ue_lcount = get4c(fp);
1150 uep->ue_size = get4c(fp);
1151 if (uep->ue_size > 0)
1152 {
1153 array = (char_u **)U_ALLOC_LINE(sizeof(char_u *) * uep->ue_size);
1154 if (array == NULL)
1155 {
1156 *error = TRUE;
1157 return uep;
1158 }
1159 vim_memset(array, 0, sizeof(char_u *) * uep->ue_size);
1160 }
Bram Moolenaar644fdff2010-05-30 13:26:21 +02001161 else
1162 array = NULL;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001163 uep->ue_array = array;
1164
1165 for (i = 0; i < uep->ue_size; ++i)
1166 {
1167 line_len = get4c(fp);
1168 if (line_len >= 0)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001169 line = read_string_decrypt(curbuf, fp, line_len);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001170 else
1171 {
1172 line = NULL;
1173 corruption_error("line length", file_name);
1174 }
1175 if (line == NULL)
1176 {
1177 *error = TRUE;
1178 return uep;
1179 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001180 array[i] = line;
1181 }
1182 return uep;
1183}
1184
1185/*
1186 * Serialize "pos" to "fp".
1187 */
1188 static void
1189serialize_pos(pos, fp)
1190 pos_T pos;
1191 FILE *fp;
1192{
1193 put_bytes(fp, (long_u)pos.lnum, 4);
1194 put_bytes(fp, (long_u)pos.col, 4);
1195#ifdef FEAT_VIRTUALEDIT
1196 put_bytes(fp, (long_u)pos.coladd, 4);
1197#else
1198 put_bytes(fp, (long_u)0, 4);
1199#endif
1200}
1201
1202/*
1203 * Unserialize the pos_T at the current position in fp.
1204 */
1205 static void
1206unserialize_pos(pos, fp)
1207 pos_T *pos;
1208 FILE *fp;
1209{
1210 pos->lnum = get4c(fp);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001211 if (pos->lnum < 0)
1212 pos->lnum = 0;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001213 pos->col = get4c(fp);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001214 if (pos->col < 0)
1215 pos->col = 0;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001216#ifdef FEAT_VIRTUALEDIT
1217 pos->coladd = get4c(fp);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001218 if (pos->coladd < 0)
1219 pos->coladd = 0;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001220#else
1221 (void)get4c(fp);
1222#endif
1223}
1224
1225/*
1226 * Serialize "info" to "fp".
1227 */
1228 static void
1229serialize_visualinfo(info, fp)
1230 visualinfo_T *info;
1231 FILE *fp;
1232{
1233 serialize_pos(info->vi_start, fp);
1234 serialize_pos(info->vi_end, fp);
1235 put_bytes(fp, (long_u)info->vi_mode, 4);
1236 put_bytes(fp, (long_u)info->vi_curswant, 4);
1237}
1238
1239/*
1240 * Unserialize the visualinfo_T at the current position in fp.
1241 */
1242 static void
1243unserialize_visualinfo(info, fp)
1244 visualinfo_T *info;
1245 FILE *fp;
1246{
1247 unserialize_pos(&info->vi_start, fp);
1248 unserialize_pos(&info->vi_end, fp);
1249 info->vi_mode = get4c(fp);
1250 info->vi_curswant = get4c(fp);
1251}
1252
1253/*
1254 * Write the pointer to an undo header. Instead of writing the pointer itself
1255 * we use the sequence number of the header. This is converted back to
1256 * pointers when reading. */
1257 static void
1258put_header_ptr(fp, uhp)
1259 FILE *fp;
1260 u_header_T *uhp;
1261{
1262 put_bytes(fp, (long_u)(uhp != NULL ? uhp->uh_seq : 0), 4);
1263}
1264
1265/*
1266 * Write the undo tree in an undo file.
1267 * When "name" is not NULL, use it as the name of the undo file.
1268 * Otherwise use buf->b_ffname to generate the undo file name.
1269 * "buf" must never be null, buf->b_ffname is used to obtain the original file
1270 * permissions.
1271 * "forceit" is TRUE for ":wundo!", FALSE otherwise.
1272 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text.
1273 */
1274 void
1275u_write_undo(name, forceit, buf, hash)
1276 char_u *name;
1277 int forceit;
1278 buf_T *buf;
1279 char_u *hash;
1280{
1281 u_header_T *uhp;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001282 char_u *file_name;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001283 int mark;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001284#ifdef U_DEBUG
1285 int headers_written = 0;
1286#endif
1287 int fd;
1288 FILE *fp = NULL;
1289 int perm;
1290 int write_ok = FALSE;
1291#ifdef UNIX
1292 int st_old_valid = FALSE;
1293 struct stat st_old;
1294 struct stat st_new;
1295#endif
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001296#ifdef FEAT_CRYPT
1297 int do_crypt = FALSE;
1298#endif
Bram Moolenaar9db58062010-05-29 20:33:07 +02001299
1300 if (name == NULL)
1301 {
1302 file_name = u_get_undo_file_name(buf->b_ffname, FALSE);
1303 if (file_name == NULL)
1304 {
1305 if (p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001306 {
1307 verbose_enter();
1308 smsg((char_u *)
1309 _("Cannot write undo file in any directory in 'undodir'"));
1310 verbose_leave();
1311 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001312 return;
1313 }
1314 }
1315 else
1316 file_name = name;
1317
1318 /*
1319 * Decide about the permission to use for the undo file. If the buffer
1320 * has a name use the permission of the original file. Otherwise only
1321 * allow the user to access the undo file.
1322 */
1323 perm = 0600;
1324 if (buf->b_ffname != NULL)
1325 {
1326#ifdef UNIX
1327 if (mch_stat((char *)buf->b_ffname, &st_old) >= 0)
1328 {
1329 perm = st_old.st_mode;
1330 st_old_valid = TRUE;
1331 }
1332#else
1333 perm = mch_getperm(buf->b_ffname);
1334 if (perm < 0)
1335 perm = 0600;
1336#endif
1337 }
1338
1339 /* strip any s-bit */
1340 perm = perm & 0777;
1341
1342 /* If the undo file already exists, verify that it actually is an undo
1343 * file, and delete it. */
1344 if (mch_getperm(file_name) >= 0)
1345 {
1346 if (name == NULL || !forceit)
1347 {
1348 /* Check we can read it and it's an undo file. */
1349 fd = mch_open((char *)file_name, O_RDONLY|O_EXTRA, 0);
1350 if (fd < 0)
1351 {
1352 if (name != NULL || p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001353 {
1354 if (name == NULL)
1355 verbose_enter();
1356 smsg((char_u *)
1357 _("Will not overwrite with undo file, cannot read: %s"),
Bram Moolenaar9db58062010-05-29 20:33:07 +02001358 file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001359 if (name == NULL)
1360 verbose_leave();
1361 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001362 goto theend;
1363 }
1364 else
1365 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02001366 char_u mbuf[UF_START_MAGIC_LEN];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001367 int len;
1368
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02001369 len = vim_read(fd, mbuf, UF_START_MAGIC_LEN);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001370 close(fd);
1371 if (len < UF_START_MAGIC_LEN
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02001372 || memcmp(mbuf, UF_START_MAGIC, UF_START_MAGIC_LEN) != 0)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001373 {
1374 if (name != NULL || p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001375 {
1376 if (name == NULL)
1377 verbose_enter();
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001378 smsg((char_u *)
1379 _("Will not overwrite, this is not an undo file: %s"),
Bram Moolenaar9db58062010-05-29 20:33:07 +02001380 file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001381 if (name == NULL)
1382 verbose_leave();
1383 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001384 goto theend;
1385 }
1386 }
1387 }
1388 mch_remove(file_name);
1389 }
1390
Bram Moolenaar504a8212010-05-30 17:17:42 +02001391 /* If there is no undo information at all, quit here after deleting any
1392 * existing undo file. */
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001393 if (buf->b_u_numhead == 0 && buf->b_u_line_ptr == NULL)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001394 {
1395 if (p_verbose > 0)
Bram Moolenaar97ea5112010-06-12 06:46:44 +02001396 verb_msg((char_u *)_("Skipping undo file write, nothing to undo"));
Bram Moolenaar504a8212010-05-30 17:17:42 +02001397 goto theend;
1398 }
1399
Bram Moolenaar9db58062010-05-29 20:33:07 +02001400 fd = mch_open((char *)file_name,
1401 O_CREAT|O_EXTRA|O_WRONLY|O_EXCL|O_NOFOLLOW, perm);
1402 if (fd < 0)
1403 {
1404 EMSG2(_(e_not_open), file_name);
1405 goto theend;
1406 }
1407 (void)mch_setperm(file_name, perm);
1408 if (p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001409 {
1410 verbose_enter();
Bram Moolenaar9db58062010-05-29 20:33:07 +02001411 smsg((char_u *)_("Writing undo file: %s"), file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001412 verbose_leave();
1413 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001414
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001415#ifdef U_DEBUG
Bram Moolenaar504a8212010-05-30 17:17:42 +02001416 /* Check there is no problem in undo info before writing. */
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001417 u_check(FALSE);
1418#endif
1419
Bram Moolenaar9db58062010-05-29 20:33:07 +02001420#ifdef UNIX
1421 /*
1422 * Try to set the group of the undo file same as the original file. If
1423 * this fails, set the protection bits for the group same as the
1424 * protection bits for others.
1425 */
1426 if (st_old_valid && (mch_stat((char *)file_name, &st_new) >= 0
1427 && st_new.st_gid != st_old.st_gid
1428# ifdef HAVE_FCHOWN /* sequent-ptx lacks fchown() */
1429 && fchown(fd, (uid_t)-1, st_old.st_gid) != 0)
1430# endif
1431 )
1432 mch_setperm(file_name, (perm & 0707) | ((perm & 07) << 3));
1433# ifdef HAVE_SELINUX
1434 if (buf->b_ffname != NULL)
1435 mch_copy_sec(buf->b_ffname, file_name);
1436# endif
1437#endif
1438
1439 fp = fdopen(fd, "w");
1440 if (fp == NULL)
1441 {
1442 EMSG2(_(e_not_open), file_name);
1443 close(fd);
1444 mch_remove(file_name);
1445 goto theend;
1446 }
1447
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001448 /* Undo must be synced. */
1449 u_sync(TRUE);
1450
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001451 /*
1452 * Write the header.
1453 */
1454 if (serialize_header(fp, buf, hash) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001455 goto write_error;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001456#ifdef FEAT_CRYPT
1457 if (*buf->b_p_key)
1458 do_crypt = TRUE;
1459#endif
Bram Moolenaar9db58062010-05-29 20:33:07 +02001460
1461 /*
1462 * Iteratively serialize UHPs and their UEPs from the top down.
1463 */
1464 mark = ++lastmark;
1465 uhp = buf->b_u_oldhead;
1466 while (uhp != NULL)
1467 {
1468 /* Serialize current UHP if we haven't seen it */
1469 if (uhp->uh_walk != mark)
1470 {
1471 uhp->uh_walk = mark;
1472#ifdef U_DEBUG
1473 ++headers_written;
1474#endif
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001475 if (serialize_uhp(fp, buf, uhp) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001476 goto write_error;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001477 }
1478
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001479 /* Now walk through the tree - algorithm from undo_time(). */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001480 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != mark)
1481 uhp = uhp->uh_prev.ptr;
1482 else if (uhp->uh_alt_next.ptr != NULL
1483 && uhp->uh_alt_next.ptr->uh_walk != mark)
1484 uhp = uhp->uh_alt_next.ptr;
1485 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
1486 && uhp->uh_next.ptr->uh_walk != mark)
1487 uhp = uhp->uh_next.ptr;
1488 else if (uhp->uh_alt_prev.ptr != NULL)
1489 uhp = uhp->uh_alt_prev.ptr;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001490 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001491 uhp = uhp->uh_next.ptr;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001492 }
1493
1494 if (put_bytes(fp, (long_u)UF_HEADER_END_MAGIC, 2) == OK)
1495 write_ok = TRUE;
1496#ifdef U_DEBUG
1497 if (headers_written != buf->b_u_numhead)
1498 EMSG3("Written %ld headers, but numhead is %ld",
1499 headers_written, buf->b_u_numhead);
1500#endif
1501
1502write_error:
1503 fclose(fp);
1504 if (!write_ok)
1505 EMSG2(_("E829: write error in undo file: %s"), file_name);
1506
1507#if defined(MACOS_CLASSIC) || defined(WIN3264)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001508 /* Copy file attributes; for systems where this can only be done after
1509 * closing the file. */
Bram Moolenaar9db58062010-05-29 20:33:07 +02001510 if (buf->b_ffname != NULL)
1511 (void)mch_copy_file_attribute(buf->b_ffname, file_name);
1512#endif
1513#ifdef HAVE_ACL
1514 if (buf->b_ffname != NULL)
1515 {
1516 vim_acl_T acl;
1517
1518 /* For systems that support ACL: get the ACL from the original file. */
1519 acl = mch_get_acl(buf->b_ffname);
1520 mch_set_acl(file_name, acl);
1521 }
1522#endif
1523
1524theend:
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001525#ifdef FEAT_CRYPT
1526 if (do_crypt)
1527 crypt_pop_state();
1528#endif
Bram Moolenaar9db58062010-05-29 20:33:07 +02001529 if (file_name != name)
1530 vim_free(file_name);
1531}
1532
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001533/*
1534 * Load the undo tree from an undo file.
1535 * If "name" is not NULL use it as the undo file name. This also means being
1536 * a bit more verbose.
1537 * Otherwise use curbuf->b_ffname to generate the undo file name.
1538 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text.
1539 */
1540 void
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001541u_read_undo(name, hash, orig_name)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001542 char_u *name;
1543 char_u *hash;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001544 char_u *orig_name;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001545{
1546 char_u *file_name;
1547 FILE *fp;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001548 long version, str_len;
Bram Moolenaara800b422010-06-27 01:15:55 +02001549 int new_version;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001550 char_u *line_ptr = NULL;
1551 linenr_T line_lnum;
1552 colnr_T line_colnr;
1553 linenr_T line_count;
Bram Moolenaar442b4222010-05-24 21:34:22 +02001554 int num_head = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001555 long old_header_seq, new_header_seq, cur_header_seq;
1556 long seq_last, seq_cur;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001557 long last_save_nr = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001558 short old_idx = -1, new_idx = -1, cur_idx = -1;
1559 long num_read_uhps = 0;
1560 time_t seq_time;
1561 int i, j;
1562 int c;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001563 u_header_T *uhp;
1564 u_header_T **uhp_table = NULL;
1565 char_u read_hash[UNDO_HASH_SIZE];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001566 char_u magic_buf[UF_START_MAGIC_LEN];
1567#ifdef U_DEBUG
1568 int *uhp_table_used;
1569#endif
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001570#ifdef UNIX
1571 struct stat st_orig;
1572 struct stat st_undo;
1573#endif
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001574#ifdef FEAT_CRYPT
1575 int do_decrypt = FALSE;
1576#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001577
1578 if (name == NULL)
1579 {
1580 file_name = u_get_undo_file_name(curbuf->b_ffname, TRUE);
1581 if (file_name == NULL)
1582 return;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001583
1584#ifdef UNIX
1585 /* For safety we only read an undo file if the owner is equal to the
1586 * owner of the text file. */
1587 if (mch_stat((char *)orig_name, &st_orig) >= 0
1588 && mch_stat((char *)file_name, &st_undo) >= 0
1589 && st_orig.st_uid != st_undo.st_uid)
1590 {
1591 if (p_verbose > 0)
1592 {
1593 verbose_enter();
1594 smsg((char_u *)_("Not reading undo file, owner differs: %s"),
1595 file_name);
1596 verbose_leave();
1597 }
1598 return;
1599 }
1600#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001601 }
1602 else
1603 file_name = name;
1604
1605 if (p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001606 {
1607 verbose_enter();
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001608 smsg((char_u *)_("Reading undo file: %s"), file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001609 verbose_leave();
1610 }
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001611
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001612 fp = mch_fopen((char *)file_name, "r");
1613 if (fp == NULL)
1614 {
1615 if (name != NULL || p_verbose > 0)
1616 EMSG2(_("E822: Cannot open undo file for reading: %s"), file_name);
1617 goto error;
1618 }
1619
Bram Moolenaar9db58062010-05-29 20:33:07 +02001620 /*
1621 * Read the undo file header.
1622 */
1623 if (fread(magic_buf, UF_START_MAGIC_LEN, 1, fp) != 1
1624 || memcmp(magic_buf, UF_START_MAGIC, UF_START_MAGIC_LEN) != 0)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001625 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001626 EMSG2(_("E823: Not an undo file: %s"), file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001627 goto error;
1628 }
1629 version = get2c(fp);
Bram Moolenaara800b422010-06-27 01:15:55 +02001630 if (version == UF_VERSION_CRYPT || version == UF_VERSION_CRYPT_PREV)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001631 {
1632#ifdef FEAT_CRYPT
Bram Moolenaar56be9502010-06-06 14:20:26 +02001633 if (*curbuf->b_p_key == NUL)
1634 {
1635 EMSG2(_("E832: Non-encrypted file has encrypted undo file: %s"),
1636 file_name);
1637 goto error;
1638 }
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001639 if (prepare_crypt_read(fp) == FAIL)
1640 {
1641 EMSG2(_("E826: Undo file decryption failed: %s"), file_name);
1642 goto error;
1643 }
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001644 do_decrypt = TRUE;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001645#else
Bram Moolenaar56be9502010-06-06 14:20:26 +02001646 EMSG2(_("E827: Undo file is encrypted: %s"), file_name);
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001647 goto error;
1648#endif
1649 }
Bram Moolenaara800b422010-06-27 01:15:55 +02001650 else if (version != UF_VERSION && version != UF_VERSION_PREV)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001651 {
1652 EMSG2(_("E824: Incompatible undo file: %s"), file_name);
1653 goto error;
1654 }
Bram Moolenaara800b422010-06-27 01:15:55 +02001655 new_version = (version == UF_VERSION || version == UF_VERSION_CRYPT);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001656
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001657 if (fread(read_hash, UNDO_HASH_SIZE, 1, fp) != 1)
1658 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001659 corruption_error("hash", file_name);
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001660 goto error;
1661 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001662 line_count = (linenr_T)get4c(fp);
1663 if (memcmp(hash, read_hash, UNDO_HASH_SIZE) != 0
1664 || line_count != curbuf->b_ml.ml_line_count)
1665 {
1666 if (p_verbose > 0 || name != NULL)
1667 {
Bram Moolenaar504a8212010-05-30 17:17:42 +02001668 if (name == NULL)
1669 verbose_enter();
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001670 give_warning((char_u *)
1671 _("File contents changed, cannot use undo info"), TRUE);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001672 if (name == NULL)
1673 verbose_leave();
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001674 }
1675 goto error;
1676 }
1677
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001678 /* Read undo data for "U" command. */
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001679 str_len = get4c(fp);
1680 if (str_len < 0)
1681 goto error;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001682 if (str_len > 0)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001683 line_ptr = read_string_decrypt(curbuf, fp, str_len);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001684 line_lnum = (linenr_T)get4c(fp);
1685 line_colnr = (colnr_T)get4c(fp);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001686 if (line_lnum < 0 || line_colnr < 0)
1687 {
1688 corruption_error("line lnum/col", file_name);
1689 goto error;
1690 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001691
1692 /* Begin general undo data */
1693 old_header_seq = get4c(fp);
1694 new_header_seq = get4c(fp);
1695 cur_header_seq = get4c(fp);
1696 num_head = get4c(fp);
1697 seq_last = get4c(fp);
1698 seq_cur = get4c(fp);
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001699 seq_time = get8ctime(fp);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001700
Bram Moolenaara800b422010-06-27 01:15:55 +02001701 /* Optional header fields, not in previous version. */
1702 if (new_version)
1703 for (;;)
1704 {
1705 int len = getc(fp);
1706 int what;
1707
1708 if (len == 0 || len == EOF)
1709 break;
1710 what = getc(fp);
1711 switch (what)
1712 {
1713 case UF_LAST_SAVE_NR:
1714 last_save_nr = get4c(fp);
1715 break;
1716 default:
1717 /* field not supported, skip */
1718 while (--len >= 0)
1719 (void)getc(fp);
1720 }
1721 }
1722
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001723 /* uhp_table will store the freshly created undo headers we allocate
1724 * until we insert them into curbuf. The table remains sorted by the
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001725 * sequence numbers of the headers.
1726 * When there are no headers uhp_table is NULL. */
1727 if (num_head > 0)
1728 {
1729 uhp_table = (u_header_T **)U_ALLOC_LINE(
1730 num_head * sizeof(u_header_T *));
1731 if (uhp_table == NULL)
1732 goto error;
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001733 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001734
Bram Moolenaar9db58062010-05-29 20:33:07 +02001735 while ((c = get2c(fp)) == UF_HEADER_MAGIC)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001736 {
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001737 if (num_read_uhps >= num_head)
1738 {
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001739 corruption_error("num_head too small", file_name);
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001740 goto error;
1741 }
1742
Bram Moolenaara800b422010-06-27 01:15:55 +02001743 uhp = unserialize_uhp(fp, file_name, new_version);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001744 if (uhp == NULL)
1745 goto error;
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001746 uhp_table[num_read_uhps++] = uhp;
1747 }
1748
1749 if (num_read_uhps != num_head)
1750 {
1751 corruption_error("num_head", file_name);
1752 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001753 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001754 if (c != UF_HEADER_END_MAGIC)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001755 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001756 corruption_error("end marker", file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001757 goto error;
1758 }
1759
Bram Moolenaar9db58062010-05-29 20:33:07 +02001760#ifdef U_DEBUG
1761 uhp_table_used = (int *)alloc_clear(
1762 (unsigned)(sizeof(int) * num_head + 1));
1763# define SET_FLAG(j) ++uhp_table_used[j]
1764#else
1765# define SET_FLAG(j)
1766#endif
1767
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001768 /* We have put all of the headers into a table. Now we iterate through the
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001769 * table and swizzle each sequence number we have stored in uh_*_seq into
1770 * a pointer corresponding to the header with that sequence number. */
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001771 for (i = 0; i < num_head; i++)
1772 {
1773 uhp = uhp_table[i];
1774 if (uhp == NULL)
1775 continue;
1776 for (j = 0; j < num_head; j++)
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001777 if (uhp_table[j] != NULL && i != j
1778 && uhp_table[i]->uh_seq == uhp_table[j]->uh_seq)
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001779 {
1780 corruption_error("duplicate uh_seq", file_name);
1781 goto error;
1782 }
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001783 for (j = 0; j < num_head; j++)
1784 if (uhp_table[j] != NULL
1785 && uhp_table[j]->uh_seq == uhp->uh_next.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001786 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001787 uhp->uh_next.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001788 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001789 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001790 }
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001791 for (j = 0; j < num_head; j++)
1792 if (uhp_table[j] != NULL
1793 && uhp_table[j]->uh_seq == uhp->uh_prev.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001794 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001795 uhp->uh_prev.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001796 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001797 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001798 }
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001799 for (j = 0; j < num_head; j++)
1800 if (uhp_table[j] != NULL
1801 && uhp_table[j]->uh_seq == uhp->uh_alt_next.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001802 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001803 uhp->uh_alt_next.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001804 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001805 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001806 }
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001807 for (j = 0; j < num_head; j++)
1808 if (uhp_table[j] != NULL
1809 && uhp_table[j]->uh_seq == uhp->uh_alt_prev.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001810 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001811 uhp->uh_alt_prev.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001812 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001813 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001814 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001815 if (old_header_seq > 0 && old_idx < 0 && uhp->uh_seq == old_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001816 {
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001817 old_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001818 SET_FLAG(i);
1819 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001820 if (new_header_seq > 0 && new_idx < 0 && uhp->uh_seq == new_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001821 {
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001822 new_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001823 SET_FLAG(i);
1824 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001825 if (cur_header_seq > 0 && cur_idx < 0 && uhp->uh_seq == cur_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001826 {
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001827 cur_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001828 SET_FLAG(i);
1829 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001830 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001831
1832 /* Now that we have read the undo info successfully, free the current undo
1833 * info and use the info from the file. */
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001834 u_blockfree(curbuf);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001835 curbuf->b_u_oldhead = old_idx < 0 ? NULL : uhp_table[old_idx];
1836 curbuf->b_u_newhead = new_idx < 0 ? NULL : uhp_table[new_idx];
1837 curbuf->b_u_curhead = cur_idx < 0 ? NULL : uhp_table[cur_idx];
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001838 curbuf->b_u_line_ptr = line_ptr;
1839 curbuf->b_u_line_lnum = line_lnum;
1840 curbuf->b_u_line_colnr = line_colnr;
1841 curbuf->b_u_numhead = num_head;
1842 curbuf->b_u_seq_last = seq_last;
1843 curbuf->b_u_seq_cur = seq_cur;
Bram Moolenaara800b422010-06-27 01:15:55 +02001844 curbuf->b_u_time_cur = seq_time;
Bram Moolenaar730cde92010-06-27 05:18:54 +02001845 curbuf->b_u_save_nr_last = last_save_nr;
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001846
1847 curbuf->b_u_synced = TRUE;
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001848 vim_free(uhp_table);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001849
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001850#ifdef U_DEBUG
Bram Moolenaar9db58062010-05-29 20:33:07 +02001851 for (i = 0; i < num_head; ++i)
1852 if (uhp_table_used[i] == 0)
1853 EMSGN("uhp_table entry %ld not used, leaking memory", i);
1854 vim_free(uhp_table_used);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001855 u_check(TRUE);
1856#endif
Bram Moolenaar9db58062010-05-29 20:33:07 +02001857
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001858 if (name != NULL)
1859 smsg((char_u *)_("Finished reading undo file %s"), file_name);
1860 goto theend;
1861
1862error:
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001863 vim_free(line_ptr);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001864 if (uhp_table != NULL)
1865 {
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001866 for (i = 0; i < num_read_uhps; i++)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001867 if (uhp_table[i] != NULL)
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001868 u_free_uhp(uhp_table[i]);
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001869 vim_free(uhp_table);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001870 }
1871
1872theend:
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001873#ifdef FEAT_CRYPT
1874 if (do_decrypt)
1875 crypt_pop_state();
1876#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001877 if (fp != NULL)
1878 fclose(fp);
1879 if (file_name != name)
1880 vim_free(file_name);
1881 return;
1882}
1883
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001884#endif /* FEAT_PERSISTENT_UNDO */
1885
1886
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887/*
1888 * If 'cpoptions' contains 'u': Undo the previous undo or redo (vi compatible).
1889 * If 'cpoptions' does not contain 'u': Always undo.
1890 */
1891 void
1892u_undo(count)
1893 int count;
1894{
1895 /*
1896 * If we get an undo command while executing a macro, we behave like the
1897 * original vi. If this happens twice in one macro the result will not
1898 * be compatible.
1899 */
1900 if (curbuf->b_u_synced == FALSE)
1901 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001902 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 count = 1;
1904 }
1905
1906 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
1907 undo_undoes = TRUE;
1908 else
1909 undo_undoes = !undo_undoes;
1910 u_doit(count);
1911}
1912
1913/*
1914 * If 'cpoptions' contains 'u': Repeat the previous undo or redo.
1915 * If 'cpoptions' does not contain 'u': Always redo.
1916 */
1917 void
1918u_redo(count)
1919 int count;
1920{
1921 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
1922 undo_undoes = FALSE;
1923 u_doit(count);
1924}
1925
1926/*
1927 * Undo or redo, depending on 'undo_undoes', 'count' times.
1928 */
1929 static void
Bram Moolenaarca003e12006-03-17 23:19:38 +00001930u_doit(startcount)
1931 int startcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932{
Bram Moolenaarca003e12006-03-17 23:19:38 +00001933 int count = startcount;
1934
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001935 if (!undo_allowed())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937
1938 u_newcount = 0;
1939 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001940 if (curbuf->b_ml.ml_flags & ML_EMPTY)
1941 u_oldcount = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 while (count--)
1943 {
Bram Moolenaarb0b50882010-07-07 18:26:28 +02001944 /* Do the change warning now, so that it triggers FileChangedRO when
1945 * needed. This may cause the file to be reloaded, that must happen
1946 * before we do anything, because it may change curbuf->b_u_curhead
1947 * and more. */
1948 change_warning(0);
1949
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 if (undo_undoes)
1951 {
1952 if (curbuf->b_u_curhead == NULL) /* first undo */
1953 curbuf->b_u_curhead = curbuf->b_u_newhead;
1954 else if (p_ul > 0) /* multi level undo */
1955 /* get next undo */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001956 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_next.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 /* nothing to undo */
1958 if (curbuf->b_u_numhead == 0 || curbuf->b_u_curhead == NULL)
1959 {
1960 /* stick curbuf->b_u_curhead at end */
1961 curbuf->b_u_curhead = curbuf->b_u_oldhead;
1962 beep_flush();
Bram Moolenaarca003e12006-03-17 23:19:38 +00001963 if (count == startcount - 1)
1964 {
1965 MSG(_("Already at oldest change"));
1966 return;
1967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 break;
1969 }
1970
Bram Moolenaarca003e12006-03-17 23:19:38 +00001971 u_undoredo(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 }
1973 else
1974 {
1975 if (curbuf->b_u_curhead == NULL || p_ul <= 0)
1976 {
1977 beep_flush(); /* nothing to redo */
Bram Moolenaarca003e12006-03-17 23:19:38 +00001978 if (count == startcount - 1)
1979 {
1980 MSG(_("Already at newest change"));
1981 return;
1982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 break;
1984 }
1985
Bram Moolenaarca003e12006-03-17 23:19:38 +00001986 u_undoredo(FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00001987
1988 /* Advance for next redo. Set "newhead" when at the end of the
1989 * redoable changes. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001990 if (curbuf->b_u_curhead->uh_prev.ptr == NULL)
Bram Moolenaar1e607892006-03-13 22:15:53 +00001991 curbuf->b_u_newhead = curbuf->b_u_curhead;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001992 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_prev.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 }
1994 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001995 u_undo_end(undo_undoes, FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00001996}
1997
Bram Moolenaar1e607892006-03-13 22:15:53 +00001998/*
1999 * Undo or redo over the timeline.
2000 * When "step" is negative go back in time, otherwise goes forward in time.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002001 * When "sec" is FALSE make "step" steps, when "sec" is TRUE use "step" as
2002 * seconds.
Bram Moolenaar730cde92010-06-27 05:18:54 +02002003 * When "file" is TRUE use "step" as a number of file writes.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002004 * When "absolute" is TRUE use "step" as the sequence number to jump to.
2005 * "sec" must be FALSE then.
Bram Moolenaar1e607892006-03-13 22:15:53 +00002006 */
2007 void
Bram Moolenaar730cde92010-06-27 05:18:54 +02002008undo_time(step, sec, file, absolute)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002009 long step;
2010 int sec;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002011 int file;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002012 int absolute;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002013{
2014 long target;
2015 long closest;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002016 long closest_start;
2017 long closest_seq = 0;
2018 long val;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002019 u_header_T *uhp;
2020 u_header_T *last;
2021 int mark;
2022 int nomark;
2023 int round;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002024 int dosec = sec;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002025 int dofile = file;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002026 int above = FALSE;
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002027 int did_undo = TRUE;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002028
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002029 /* First make sure the current undoable change is synced. */
2030 if (curbuf->b_u_synced == FALSE)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002031 u_sync(TRUE);
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002032
Bram Moolenaar1e607892006-03-13 22:15:53 +00002033 u_newcount = 0;
2034 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002035 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002036 u_oldcount = -1;
2037
Bram Moolenaarca003e12006-03-17 23:19:38 +00002038 /* "target" is the node below which we want to be.
2039 * Init "closest" to a value we can't reach. */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002040 if (absolute)
2041 {
2042 target = step;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002043 closest = -1;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002044 }
Bram Moolenaarca003e12006-03-17 23:19:38 +00002045 else
Bram Moolenaar1e607892006-03-13 22:15:53 +00002046 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002047 /* When doing computations with time_t subtract starttime, because
2048 * time_t converted to a long may result in a wrong number. */
Bram Moolenaar730cde92010-06-27 05:18:54 +02002049 if (dosec)
Bram Moolenaara800b422010-06-27 01:15:55 +02002050 target = (long)(curbuf->b_u_time_cur - starttime) + step;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002051 else if (dofile)
2052 {
2053 if (step < 0)
2054 {
2055 /* Going back to a previous write. If there were changes after
2056 * the last write, count that as moving one file-write, so
2057 * that ":earlier 1f" undoes all changes since the last save. */
2058 uhp = curbuf->b_u_curhead;
2059 if (uhp != NULL)
2060 uhp = uhp->uh_next.ptr;
2061 else
2062 uhp = curbuf->b_u_newhead;
2063 if (uhp != NULL && uhp->uh_save_nr != 0)
2064 /* "uh_save_nr" was set in the last block, that means
2065 * there were no changes since the last write */
2066 target = curbuf->b_u_save_nr_cur + step;
2067 else
2068 /* count the changes since the last write as one step */
2069 target = curbuf->b_u_save_nr_cur + step + 1;
2070 if (target <= 0)
2071 /* Go to before first write: before the oldest change. Use
2072 * the sequence number for that. */
2073 dofile = FALSE;
2074 }
2075 else
2076 {
2077 /* Moving forward to a newer write. */
2078 target = curbuf->b_u_save_nr_cur + step;
2079 if (target > curbuf->b_u_save_nr_last)
2080 {
2081 /* Go to after last write: after the latest change. Use
2082 * the sequence number for that. */
2083 target = curbuf->b_u_seq_last + 1;
2084 dofile = FALSE;
2085 }
2086 }
2087 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002088 else
2089 target = curbuf->b_u_seq_cur + step;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002090 if (step < 0)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002091 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002092 if (target < 0)
2093 target = 0;
2094 closest = -1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002095 }
2096 else
2097 {
Bram Moolenaar730cde92010-06-27 05:18:54 +02002098 if (dosec)
Bram Moolenaardb552d602006-03-23 22:59:57 +00002099 closest = (long)(time(NULL) - starttime + 1);
Bram Moolenaar730cde92010-06-27 05:18:54 +02002100 else if (dofile)
2101 closest = curbuf->b_u_save_nr_last + 2;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002102 else
2103 closest = curbuf->b_u_seq_last + 2;
2104 if (target >= closest)
2105 target = closest - 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002106 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002107 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002108 closest_start = closest;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002109 closest_seq = curbuf->b_u_seq_cur;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002110
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002111 /*
2112 * May do this twice:
Bram Moolenaar1e607892006-03-13 22:15:53 +00002113 * 1. Search for "target", update "closest" to the best match found.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002114 * 2. If "target" not found search for "closest".
2115 *
2116 * When using the closest time we use the sequence number in the second
2117 * round, because there may be several entries with the same time.
2118 */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002119 for (round = 1; round <= 2; ++round)
2120 {
2121 /* Find the path from the current state to where we want to go. The
2122 * desired state can be anywhere in the undo tree, need to go all over
2123 * it. We put "nomark" in uh_walk where we have been without success,
2124 * "mark" where it could possibly be. */
2125 mark = ++lastmark;
2126 nomark = ++lastmark;
2127
2128 if (curbuf->b_u_curhead == NULL) /* at leaf of the tree */
2129 uhp = curbuf->b_u_newhead;
2130 else
2131 uhp = curbuf->b_u_curhead;
2132
2133 while (uhp != NULL)
2134 {
2135 uhp->uh_walk = mark;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002136 if (dosec)
2137 val = (long)(uhp->uh_time - starttime);
2138 else if (dofile)
2139 val = uhp->uh_save_nr;
2140 else
2141 val = uhp->uh_seq;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002142
Bram Moolenaar730cde92010-06-27 05:18:54 +02002143 if (round == 1 && !(dofile && val == 0))
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002144 {
2145 /* Remember the header that is closest to the target.
2146 * It must be at least in the right direction (checked with
Bram Moolenaarca003e12006-03-17 23:19:38 +00002147 * "b_u_seq_cur"). When the timestamp is equal find the
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002148 * highest/lowest sequence number. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002149 if ((step < 0 ? uhp->uh_seq <= curbuf->b_u_seq_cur
2150 : uhp->uh_seq > curbuf->b_u_seq_cur)
2151 && ((dosec && val == closest)
2152 ? (step < 0
2153 ? uhp->uh_seq < closest_seq
2154 : uhp->uh_seq > closest_seq)
2155 : closest == closest_start
2156 || (val > target
2157 ? (closest > target
2158 ? val - target <= closest - target
2159 : val - target <= target - closest)
2160 : (closest > target
2161 ? target - val <= closest - target
2162 : target - val <= target - closest))))
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002163 {
2164 closest = val;
2165 closest_seq = uhp->uh_seq;
2166 }
2167 }
2168
2169 /* Quit searching when we found a match. But when searching for a
2170 * time we need to continue looking for the best uh_seq. */
2171 if (target == val && !dosec)
Bram Moolenaar730cde92010-06-27 05:18:54 +02002172 {
2173 target = uhp->uh_seq;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002174 break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002175 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002176
2177 /* go down in the tree if we haven't been there */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002178 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != nomark
2179 && uhp->uh_prev.ptr->uh_walk != mark)
2180 uhp = uhp->uh_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002181
2182 /* go to alternate branch if we haven't been there */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002183 else if (uhp->uh_alt_next.ptr != NULL
2184 && uhp->uh_alt_next.ptr->uh_walk != nomark
2185 && uhp->uh_alt_next.ptr->uh_walk != mark)
2186 uhp = uhp->uh_alt_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002187
2188 /* go up in the tree if we haven't been there and we are at the
2189 * start of alternate branches */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002190 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
2191 && uhp->uh_next.ptr->uh_walk != nomark
2192 && uhp->uh_next.ptr->uh_walk != mark)
Bram Moolenaardb552d602006-03-23 22:59:57 +00002193 {
2194 /* If still at the start we don't go through this change. */
2195 if (uhp == curbuf->b_u_curhead)
2196 uhp->uh_walk = nomark;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002197 uhp = uhp->uh_next.ptr;
Bram Moolenaardb552d602006-03-23 22:59:57 +00002198 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002199
2200 else
2201 {
2202 /* need to backtrack; mark this node as useless */
2203 uhp->uh_walk = nomark;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002204 if (uhp->uh_alt_prev.ptr != NULL)
2205 uhp = uhp->uh_alt_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002206 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002207 uhp = uhp->uh_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002208 }
2209 }
2210
2211 if (uhp != NULL) /* found it */
2212 break;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002213
2214 if (absolute)
2215 {
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002216 EMSGN(_("E830: Undo number %ld not found"), step);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002217 return;
2218 }
2219
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002220 if (closest == closest_start)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002221 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002222 if (step < 0)
2223 MSG(_("Already at oldest change"));
2224 else
2225 MSG(_("Already at newest change"));
Bram Moolenaar1e607892006-03-13 22:15:53 +00002226 return;
2227 }
2228
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002229 target = closest_seq;
2230 dosec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002231 dofile = FALSE;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002232 if (step < 0)
2233 above = TRUE; /* stop above the header */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002234 }
2235
2236 /* If we found it: Follow the path to go to where we want to be. */
2237 if (uhp != NULL)
2238 {
2239 /*
2240 * First go up the tree as much as needed.
2241 */
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002242 while (!got_int)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002243 {
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002244 /* Do the change warning now, for the same reason as above. */
2245 change_warning(0);
2246
Bram Moolenaar1e607892006-03-13 22:15:53 +00002247 uhp = curbuf->b_u_curhead;
2248 if (uhp == NULL)
2249 uhp = curbuf->b_u_newhead;
2250 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002251 uhp = uhp->uh_next.ptr;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002252 if (uhp == NULL || uhp->uh_walk != mark
2253 || (uhp->uh_seq == target && !above))
Bram Moolenaar1e607892006-03-13 22:15:53 +00002254 break;
2255 curbuf->b_u_curhead = uhp;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002256 u_undoredo(TRUE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002257 uhp->uh_walk = nomark; /* don't go back down here */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002258 }
2259
2260 /*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002261 * And now go down the tree (redo), branching off where needed.
Bram Moolenaar1e607892006-03-13 22:15:53 +00002262 */
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002263 while (!got_int)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002264 {
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002265 /* Do the change warning now, for the same reason as above. */
2266 change_warning(0);
2267
2268 uhp = curbuf->b_u_curhead;
2269 if (uhp == NULL)
2270 break;
2271
Bram Moolenaar89ed3df2007-01-09 19:23:12 +00002272 /* Go back to the first branch with a mark. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002273 while (uhp->uh_alt_prev.ptr != NULL
2274 && uhp->uh_alt_prev.ptr->uh_walk == mark)
2275 uhp = uhp->uh_alt_prev.ptr;
Bram Moolenaar89ed3df2007-01-09 19:23:12 +00002276
Bram Moolenaar1e607892006-03-13 22:15:53 +00002277 /* Find the last branch with a mark, that's the one. */
2278 last = uhp;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002279 while (last->uh_alt_next.ptr != NULL
2280 && last->uh_alt_next.ptr->uh_walk == mark)
2281 last = last->uh_alt_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002282 if (last != uhp)
2283 {
2284 /* Make the used branch the first entry in the list of
2285 * alternatives to make "u" and CTRL-R take this branch. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002286 while (uhp->uh_alt_prev.ptr != NULL)
2287 uhp = uhp->uh_alt_prev.ptr;
2288 if (last->uh_alt_next.ptr != NULL)
2289 last->uh_alt_next.ptr->uh_alt_prev.ptr =
2290 last->uh_alt_prev.ptr;
2291 last->uh_alt_prev.ptr->uh_alt_next.ptr = last->uh_alt_next.ptr;
2292 last->uh_alt_prev.ptr = NULL;
2293 last->uh_alt_next.ptr = uhp;
2294 uhp->uh_alt_prev.ptr = last;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002295
Bram Moolenaar8f1f6292010-05-30 16:55:22 +02002296 if (curbuf->b_u_oldhead == uhp)
2297 curbuf->b_u_oldhead = last;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002298 uhp = last;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002299 if (uhp->uh_next.ptr != NULL)
2300 uhp->uh_next.ptr->uh_prev.ptr = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002301 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002302 curbuf->b_u_curhead = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002303
2304 if (uhp->uh_walk != mark)
2305 break; /* must have reached the target */
2306
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002307 /* Stop when going backwards in time and didn't find the exact
2308 * header we were looking for. */
2309 if (uhp->uh_seq == target && above)
Bram Moolenaardb552d602006-03-23 22:59:57 +00002310 {
2311 curbuf->b_u_seq_cur = target - 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002312 break;
Bram Moolenaardb552d602006-03-23 22:59:57 +00002313 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002314
Bram Moolenaarca003e12006-03-17 23:19:38 +00002315 u_undoredo(FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002316
2317 /* Advance "curhead" to below the header we last used. If it
2318 * becomes NULL then we need to set "newhead" to this leaf. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002319 if (uhp->uh_prev.ptr == NULL)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002320 curbuf->b_u_newhead = uhp;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002321 curbuf->b_u_curhead = uhp->uh_prev.ptr;
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002322 did_undo = FALSE;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002323
2324 if (uhp->uh_seq == target) /* found it! */
2325 break;
2326
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002327 uhp = uhp->uh_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002328 if (uhp == NULL || uhp->uh_walk != mark)
2329 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002330 /* Need to redo more but can't find it... */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002331 EMSG2(_(e_intern2), "undo_time()");
2332 break;
2333 }
2334 }
2335 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00002336 u_undo_end(did_undo, absolute);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337}
2338
2339/*
2340 * u_undoredo: common code for undo and redo
2341 *
2342 * The lines in the file are replaced by the lines in the entry list at
2343 * curbuf->b_u_curhead. The replaced lines in the file are saved in the entry
2344 * list for the next undo/redo.
Bram Moolenaarca003e12006-03-17 23:19:38 +00002345 *
2346 * When "undo" is TRUE we go up in the tree, when FALSE we go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 */
2348 static void
Bram Moolenaarca003e12006-03-17 23:19:38 +00002349u_undoredo(undo)
2350 int undo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351{
2352 char_u **newarray = NULL;
2353 linenr_T oldsize;
2354 linenr_T newsize;
2355 linenr_T top, bot;
2356 linenr_T lnum;
2357 linenr_T newlnum = MAXLNUM;
2358 long i;
2359 u_entry_T *uep, *nuep;
2360 u_entry_T *newlist = NULL;
2361 int old_flags;
2362 int new_flags;
2363 pos_T namedm[NMARKS];
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002364#ifdef FEAT_VISUAL
2365 visualinfo_T visualinfo;
2366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 int empty_buffer; /* buffer became empty */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002368 u_header_T *curhead = curbuf->b_u_curhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002370#ifdef FEAT_AUTOCMD
2371 /* Don't want autocommands using the undo structures here, they are
2372 * invalid till the end. */
2373 block_autocmds();
2374#endif
2375
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002376#ifdef U_DEBUG
2377 u_check(FALSE);
2378#endif
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002379 old_flags = curhead->uh_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 new_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
2381 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
2382 setpcmark();
2383
2384 /*
2385 * save marks before undo/redo
2386 */
2387 mch_memmove(namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002388#ifdef FEAT_VISUAL
2389 visualinfo = curbuf->b_visual;
2390#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 curbuf->b_op_start.lnum = curbuf->b_ml.ml_line_count;
2392 curbuf->b_op_start.col = 0;
2393 curbuf->b_op_end.lnum = 0;
2394 curbuf->b_op_end.col = 0;
2395
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002396 for (uep = curhead->uh_entry; uep != NULL; uep = nuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 {
2398 top = uep->ue_top;
2399 bot = uep->ue_bot;
2400 if (bot == 0)
2401 bot = curbuf->b_ml.ml_line_count + 1;
2402 if (top > curbuf->b_ml.ml_line_count || top >= bot
2403 || bot > curbuf->b_ml.ml_line_count + 1)
2404 {
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002405#ifdef FEAT_AUTOCMD
2406 unblock_autocmds();
2407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 EMSG(_("E438: u_undo: line numbers wrong"));
2409 changed(); /* don't want UNCHANGED now */
2410 return;
2411 }
2412
2413 oldsize = bot - top - 1; /* number of lines before undo */
2414 newsize = uep->ue_size; /* number of lines after undo */
2415
2416 if (top < newlnum)
2417 {
2418 /* If the saved cursor is somewhere in this undo block, move it to
2419 * the remembered position. Makes "gwap" put the cursor back
2420 * where it was. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002421 lnum = curhead->uh_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 if (lnum >= top && lnum <= top + newsize + 1)
2423 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002424 curwin->w_cursor = curhead->uh_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 newlnum = curwin->w_cursor.lnum - 1;
2426 }
2427 else
2428 {
2429 /* Use the first line that actually changed. Avoids that
2430 * undoing auto-formatting puts the cursor in the previous
2431 * line. */
2432 for (i = 0; i < newsize && i < oldsize; ++i)
2433 if (STRCMP(uep->ue_array[i], ml_get(top + 1 + i)) != 0)
2434 break;
2435 if (i == newsize && newlnum == MAXLNUM && uep->ue_next == NULL)
2436 {
2437 newlnum = top;
2438 curwin->w_cursor.lnum = newlnum + 1;
2439 }
2440 else if (i < newsize)
2441 {
2442 newlnum = top + i;
2443 curwin->w_cursor.lnum = newlnum + 1;
2444 }
2445 }
2446 }
2447
2448 empty_buffer = FALSE;
2449
2450 /* delete the lines between top and bot and save them in newarray */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002451 if (oldsize > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002453 if ((newarray = (char_u **)U_ALLOC_LINE(
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002454 sizeof(char_u *) * oldsize)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 {
2456 do_outofmem_msg((long_u)(sizeof(char_u *) * oldsize));
2457 /*
2458 * We have messed up the entry list, repair is impossible.
2459 * we have to free the rest of the list.
2460 */
2461 while (uep != NULL)
2462 {
2463 nuep = uep->ue_next;
2464 u_freeentry(uep, uep->ue_size);
2465 uep = nuep;
2466 }
2467 break;
2468 }
2469 /* delete backwards, it goes faster in most cases */
2470 for (lnum = bot - 1, i = oldsize; --i >= 0; --lnum)
2471 {
2472 /* what can we do when we run out of memory? */
2473 if ((newarray[i] = u_save_line(lnum)) == NULL)
2474 do_outofmem_msg((long_u)0);
2475 /* remember we deleted the last line in the buffer, and a
2476 * dummy empty line will be inserted */
2477 if (curbuf->b_ml.ml_line_count == 1)
2478 empty_buffer = TRUE;
2479 ml_delete(lnum, FALSE);
2480 }
2481 }
Bram Moolenaar8d343302005-07-12 22:46:17 +00002482 else
2483 newarray = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484
2485 /* insert the lines in u_array between top and bot */
2486 if (newsize)
2487 {
2488 for (lnum = top, i = 0; i < newsize; ++i, ++lnum)
2489 {
2490 /*
2491 * If the file is empty, there is an empty line 1 that we
2492 * should get rid of, by replacing it with the new line
2493 */
2494 if (empty_buffer && lnum == 0)
2495 ml_replace((linenr_T)1, uep->ue_array[i], TRUE);
2496 else
2497 ml_append(lnum, uep->ue_array[i], (colnr_T)0, FALSE);
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002498 vim_free(uep->ue_array[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 }
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002500 vim_free((char_u *)uep->ue_array);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 }
2502
2503 /* adjust marks */
2504 if (oldsize != newsize)
2505 {
2506 mark_adjust(top + 1, top + oldsize, (long)MAXLNUM,
2507 (long)newsize - (long)oldsize);
2508 if (curbuf->b_op_start.lnum > top + oldsize)
2509 curbuf->b_op_start.lnum += newsize - oldsize;
2510 if (curbuf->b_op_end.lnum > top + oldsize)
2511 curbuf->b_op_end.lnum += newsize - oldsize;
2512 }
2513
2514 changed_lines(top + 1, 0, bot, newsize - oldsize);
2515
2516 /* set '[ and '] mark */
2517 if (top + 1 < curbuf->b_op_start.lnum)
2518 curbuf->b_op_start.lnum = top + 1;
2519 if (newsize == 0 && top + 1 > curbuf->b_op_end.lnum)
2520 curbuf->b_op_end.lnum = top + 1;
2521 else if (top + newsize > curbuf->b_op_end.lnum)
2522 curbuf->b_op_end.lnum = top + newsize;
2523
2524 u_newcount += newsize;
2525 u_oldcount += oldsize;
2526 uep->ue_size = oldsize;
2527 uep->ue_array = newarray;
2528 uep->ue_bot = top + newsize + 1;
2529
2530 /*
2531 * insert this entry in front of the new entry list
2532 */
2533 nuep = uep->ue_next;
2534 uep->ue_next = newlist;
2535 newlist = uep;
2536 }
2537
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002538 curhead->uh_entry = newlist;
2539 curhead->uh_flags = new_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 if ((old_flags & UH_EMPTYBUF) && bufempty())
2541 curbuf->b_ml.ml_flags |= ML_EMPTY;
2542 if (old_flags & UH_CHANGED)
2543 changed();
2544 else
Bram Moolenaar009b2592004-10-24 19:18:58 +00002545#ifdef FEAT_NETBEANS_INTG
2546 /* per netbeans undo rules, keep it as modified */
2547 if (!isNetbeansModified(curbuf))
2548#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 unchanged(curbuf, FALSE);
2550
2551 /*
2552 * restore marks from before undo/redo
2553 */
2554 for (i = 0; i < NMARKS; ++i)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002555 if (curhead->uh_namedm[i].lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002557 curbuf->b_namedm[i] = curhead->uh_namedm[i];
2558 curhead->uh_namedm[i] = namedm[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002560#ifdef FEAT_VISUAL
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002561 if (curhead->uh_visual.vi_start.lnum != 0)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002562 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002563 curbuf->b_visual = curhead->uh_visual;
2564 curhead->uh_visual = visualinfo;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002565 }
2566#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567
2568 /*
2569 * If the cursor is only off by one line, put it at the same position as
2570 * before starting the change (for the "o" command).
2571 * Otherwise the cursor should go to the first undone line.
2572 */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002573 if (curhead->uh_cursor.lnum + 1 == curwin->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 && curwin->w_cursor.lnum > 1)
2575 --curwin->w_cursor.lnum;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002576 if (curhead->uh_cursor.lnum == curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002578 curwin->w_cursor.col = curhead->uh_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002580 if (virtual_active() && curhead->uh_cursor_vcol >= 0)
2581 coladvance((colnr_T)curhead->uh_cursor_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 else
2583 curwin->w_cursor.coladd = 0;
2584#endif
2585 }
2586 else if (curwin->w_cursor.lnum <= curbuf->b_ml.ml_line_count)
2587 beginline(BL_SOL | BL_FIX);
2588 else
2589 {
2590 /* We get here with the current cursor line being past the end (eg
2591 * after adding lines at the end of the file, and then undoing it).
2592 * check_cursor() will move the cursor to the last line. Move it to
2593 * the first column here. */
2594 curwin->w_cursor.col = 0;
2595#ifdef FEAT_VIRTUALEDIT
2596 curwin->w_cursor.coladd = 0;
2597#endif
2598 }
2599
2600 /* Make sure the cursor is on an existing line and column. */
2601 check_cursor();
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002602
2603 /* Remember where we are for "g-" and ":earlier 10s". */
2604 curbuf->b_u_seq_cur = curhead->uh_seq;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002605 if (undo)
2606 /* We are below the previous undo. However, to make ":earlier 1s"
2607 * work we compute this as being just above the just undone change. */
2608 --curbuf->b_u_seq_cur;
2609
Bram Moolenaar730cde92010-06-27 05:18:54 +02002610 /* Remember where we are for ":earlier 1f" and ":later 1f". */
2611 if (curhead->uh_save_nr != 0)
2612 {
2613 if (undo)
2614 curbuf->b_u_save_nr_cur = curhead->uh_save_nr - 1;
2615 else
2616 curbuf->b_u_save_nr_cur = curhead->uh_save_nr;
2617 }
2618
Bram Moolenaarca003e12006-03-17 23:19:38 +00002619 /* The timestamp can be the same for multiple changes, just use the one of
2620 * the undone/redone change. */
Bram Moolenaara800b422010-06-27 01:15:55 +02002621 curbuf->b_u_time_cur = curhead->uh_time;
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002622
2623#ifdef FEAT_AUTOCMD
2624 unblock_autocmds();
2625#endif
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002626#ifdef U_DEBUG
2627 u_check(FALSE);
2628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629}
2630
2631/*
2632 * If we deleted or added lines, report the number of less/more lines.
2633 * Otherwise, report the number of changes (this may be incorrect
2634 * in some cases, but it's better than nothing).
2635 */
2636 static void
Bram Moolenaardb552d602006-03-23 22:59:57 +00002637u_undo_end(did_undo, absolute)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002638 int did_undo; /* just did an undo */
Bram Moolenaardb552d602006-03-23 22:59:57 +00002639 int absolute; /* used ":undo N" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640{
Bram Moolenaar89d40322006-08-29 15:30:07 +00002641 char *msgstr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002642 u_header_T *uhp;
2643 char_u msgbuf[80];
Bram Moolenaar1e607892006-03-13 22:15:53 +00002644
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645#ifdef FEAT_FOLDING
2646 if ((fdo_flags & FDO_UNDO) && KeyTyped)
2647 foldOpenCursor();
2648#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +00002649
2650 if (global_busy /* no messages now, wait until global is finished */
2651 || !messaging()) /* 'lazyredraw' set, don't do messages now */
2652 return;
2653
2654 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2655 --u_newcount;
2656
2657 u_oldcount -= u_newcount;
2658 if (u_oldcount == -1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002659 msgstr = N_("more line");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002660 else if (u_oldcount < 0)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002661 msgstr = N_("more lines");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002662 else if (u_oldcount == 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002663 msgstr = N_("line less");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002664 else if (u_oldcount > 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002665 msgstr = N_("fewer lines");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002666 else
2667 {
2668 u_oldcount = u_newcount;
2669 if (u_newcount == 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002670 msgstr = N_("change");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002671 else
Bram Moolenaar89d40322006-08-29 15:30:07 +00002672 msgstr = N_("changes");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002673 }
2674
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002675 if (curbuf->b_u_curhead != NULL)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002676 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00002677 /* For ":undo N" we prefer a "after #N" message. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002678 if (absolute && curbuf->b_u_curhead->uh_next.ptr != NULL)
Bram Moolenaardb552d602006-03-23 22:59:57 +00002679 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002680 uhp = curbuf->b_u_curhead->uh_next.ptr;
Bram Moolenaardb552d602006-03-23 22:59:57 +00002681 did_undo = FALSE;
2682 }
2683 else if (did_undo)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002684 uhp = curbuf->b_u_curhead;
2685 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002686 uhp = curbuf->b_u_curhead->uh_next.ptr;
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002687 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002688 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002689 uhp = curbuf->b_u_newhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002690
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002691 if (uhp == NULL)
2692 *msgbuf = NUL;
2693 else
2694 u_add_time(msgbuf, sizeof(msgbuf), uhp->uh_time);
2695
Bram Moolenaarb2c03502010-07-02 20:20:09 +02002696#ifdef FEAT_CONCEAL
2697 {
2698 win_T *wp;
2699
2700 FOR_ALL_WINDOWS(wp)
2701 {
2702 if (wp->w_buffer == curbuf && wp->w_p_conceal)
2703 redraw_win_later(wp, NOT_VALID);
2704 }
2705 }
2706#endif
2707
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002708 smsg((char_u *)_("%ld %s; %s #%ld %s"),
Bram Moolenaarca003e12006-03-17 23:19:38 +00002709 u_oldcount < 0 ? -u_oldcount : u_oldcount,
Bram Moolenaar89d40322006-08-29 15:30:07 +00002710 _(msgstr),
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002711 did_undo ? _("before") : _("after"),
2712 uhp == NULL ? 0L : uhp->uh_seq,
2713 msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714}
2715
2716/*
2717 * u_sync: stop adding to the current entry list
2718 */
2719 void
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002720u_sync(force)
2721 int force; /* Also sync when no_u_sync is set. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722{
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002723 /* Skip it when already synced or syncing is disabled. */
2724 if (curbuf->b_u_synced || (!force && no_u_sync > 0))
2725 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2727 if (im_is_preediting())
2728 return; /* XIM is busy, don't break an undo sequence */
2729#endif
2730 if (p_ul < 0)
2731 curbuf->b_u_synced = TRUE; /* no entries, nothing to do */
2732 else
2733 {
2734 u_getbot(); /* compute ue_bot of previous u_save */
2735 curbuf->b_u_curhead = NULL;
2736 }
2737}
2738
2739/*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002740 * ":undolist": List the leafs of the undo tree
2741 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002742 void
2743ex_undolist(eap)
Bram Moolenaarfff2bee2010-05-15 13:56:02 +02002744 exarg_T *eap UNUSED;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002745{
2746 garray_T ga;
2747 u_header_T *uhp;
2748 int mark;
2749 int nomark;
2750 int changes = 1;
2751 int i;
2752
2753 /*
2754 * 1: walk the tree to find all leafs, put the info in "ga".
2755 * 2: sort the lines
2756 * 3: display the list
2757 */
2758 mark = ++lastmark;
2759 nomark = ++lastmark;
2760 ga_init2(&ga, (int)sizeof(char *), 20);
2761
2762 uhp = curbuf->b_u_oldhead;
2763 while (uhp != NULL)
2764 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002765 if (uhp->uh_prev.ptr == NULL && uhp->uh_walk != nomark
Bram Moolenaarca003e12006-03-17 23:19:38 +00002766 && uhp->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002767 {
2768 if (ga_grow(&ga, 1) == FAIL)
2769 break;
2770 vim_snprintf((char *)IObuff, IOSIZE, "%6ld %7ld ",
2771 uhp->uh_seq, changes);
2772 u_add_time(IObuff + STRLEN(IObuff), IOSIZE - STRLEN(IObuff),
2773 uhp->uh_time);
Bram Moolenaara800b422010-06-27 01:15:55 +02002774 if (uhp->uh_save_nr > 0)
2775 {
2776 while (STRLEN(IObuff) < 32)
2777 STRCAT(IObuff, " ");
2778 vim_snprintf_add((char *)IObuff, IOSIZE,
2779 " %3ld", uhp->uh_save_nr);
2780 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002781 ((char_u **)(ga.ga_data))[ga.ga_len++] = vim_strsave(IObuff);
2782 }
2783
2784 uhp->uh_walk = mark;
2785
2786 /* go down in the tree if we haven't been there */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002787 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != nomark
2788 && uhp->uh_prev.ptr->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002789 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002790 uhp = uhp->uh_prev.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002791 ++changes;
2792 }
2793
2794 /* go to alternate branch if we haven't been there */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002795 else if (uhp->uh_alt_next.ptr != NULL
2796 && uhp->uh_alt_next.ptr->uh_walk != nomark
2797 && uhp->uh_alt_next.ptr->uh_walk != mark)
2798 uhp = uhp->uh_alt_next.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002799
2800 /* go up in the tree if we haven't been there and we are at the
2801 * start of alternate branches */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002802 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
2803 && uhp->uh_next.ptr->uh_walk != nomark
2804 && uhp->uh_next.ptr->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002805 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002806 uhp = uhp->uh_next.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002807 --changes;
2808 }
2809
2810 else
2811 {
2812 /* need to backtrack; mark this node as done */
2813 uhp->uh_walk = nomark;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002814 if (uhp->uh_alt_prev.ptr != NULL)
2815 uhp = uhp->uh_alt_prev.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002816 else
2817 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002818 uhp = uhp->uh_next.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002819 --changes;
2820 }
2821 }
2822 }
2823
2824 if (ga.ga_len == 0)
2825 MSG(_("Nothing to undo"));
2826 else
2827 {
2828 sort_strings((char_u **)ga.ga_data, ga.ga_len);
2829
2830 msg_start();
Bram Moolenaara800b422010-06-27 01:15:55 +02002831 msg_puts_attr((char_u *)_("number changes time saved"),
2832 hl_attr(HLF_T));
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002833 for (i = 0; i < ga.ga_len && !got_int; ++i)
2834 {
2835 msg_putchar('\n');
2836 if (got_int)
2837 break;
2838 msg_puts(((char_u **)ga.ga_data)[i]);
2839 }
2840 msg_end();
2841
2842 ga_clear_strings(&ga);
2843 }
2844}
2845
2846/*
2847 * Put the timestamp of an undo header in "buf[buflen]" in a nice format.
2848 */
2849 static void
2850u_add_time(buf, buflen, tt)
2851 char_u *buf;
2852 size_t buflen;
2853 time_t tt;
2854{
2855#ifdef HAVE_STRFTIME
2856 struct tm *curtime;
2857
2858 if (time(NULL) - tt >= 100)
2859 {
2860 curtime = localtime(&tt);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00002861 (void)strftime((char *)buf, buflen, "%H:%M:%S", curtime);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002862 }
2863 else
2864#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002865 vim_snprintf((char *)buf, buflen, _("%ld seconds ago"),
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002866 (long)(time(NULL) - tt));
2867}
2868
2869/*
Bram Moolenaare224ffa2006-03-01 00:01:28 +00002870 * ":undojoin": continue adding to the last entry list
2871 */
Bram Moolenaare224ffa2006-03-01 00:01:28 +00002872 void
2873ex_undojoin(eap)
Bram Moolenaarfff2bee2010-05-15 13:56:02 +02002874 exarg_T *eap UNUSED;
Bram Moolenaare224ffa2006-03-01 00:01:28 +00002875{
Bram Moolenaare224ffa2006-03-01 00:01:28 +00002876 if (curbuf->b_u_newhead == NULL)
2877 return; /* nothing changed before */
Bram Moolenaar57657d82006-04-21 22:12:41 +00002878 if (curbuf->b_u_curhead != NULL)
2879 {
2880 EMSG(_("E790: undojoin is not allowed after undo"));
2881 return;
2882 }
2883 if (!curbuf->b_u_synced)
2884 return; /* already unsynced */
Bram Moolenaare224ffa2006-03-01 00:01:28 +00002885 if (p_ul < 0)
2886 return; /* no entries, nothing to do */
2887 else
2888 {
2889 /* Go back to the last entry */
2890 curbuf->b_u_curhead = curbuf->b_u_newhead;
2891 curbuf->b_u_synced = FALSE; /* no entries, nothing to do */
2892 }
2893}
2894
2895/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 * Called after writing the file and setting b_changed to FALSE.
2897 * Now an undo means that the buffer is modified.
2898 */
2899 void
2900u_unchanged(buf)
2901 buf_T *buf;
2902{
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002903 u_unch_branch(buf->b_u_oldhead);
2904 buf->b_did_warn = FALSE;
2905}
2906
Bram Moolenaar730cde92010-06-27 05:18:54 +02002907/*
2908 * Increase the write count, store it in the last undo header, what would be
2909 * used for "u".
2910 */
2911 void
2912u_update_save_nr(buf)
2913 buf_T *buf;
2914{
2915 u_header_T *uhp;
2916
2917 ++buf->b_u_save_nr_last;
2918 buf->b_u_save_nr_cur = buf->b_u_save_nr_last;
2919 uhp = buf->b_u_curhead;
2920 if (uhp != NULL)
2921 uhp = uhp->uh_next.ptr;
2922 else
2923 uhp = buf->b_u_newhead;
2924 if (uhp != NULL)
2925 uhp->uh_save_nr = buf->b_u_save_nr_last;
2926}
2927
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002928 static void
2929u_unch_branch(uhp)
2930 u_header_T *uhp;
2931{
Bram Moolenaar1e607892006-03-13 22:15:53 +00002932 u_header_T *uh;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002934 for (uh = uhp; uh != NULL; uh = uh->uh_prev.ptr)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002935 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 uh->uh_flags |= UH_CHANGED;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002937 if (uh->uh_alt_next.ptr != NULL)
2938 u_unch_branch(uh->uh_alt_next.ptr); /* recursive */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940}
2941
2942/*
2943 * Get pointer to last added entry.
2944 * If it's not valid, give an error message and return NULL.
2945 */
2946 static u_entry_T *
2947u_get_headentry()
2948{
2949 if (curbuf->b_u_newhead == NULL || curbuf->b_u_newhead->uh_entry == NULL)
2950 {
2951 EMSG(_("E439: undo list corrupt"));
2952 return NULL;
2953 }
2954 return curbuf->b_u_newhead->uh_entry;
2955}
2956
2957/*
2958 * u_getbot(): compute the line number of the previous u_save
2959 * It is called only when b_u_synced is FALSE.
2960 */
2961 static void
2962u_getbot()
2963{
2964 u_entry_T *uep;
2965 linenr_T extra;
2966
2967 uep = u_get_headentry(); /* check for corrupt undo list */
2968 if (uep == NULL)
2969 return;
2970
2971 uep = curbuf->b_u_newhead->uh_getbot_entry;
2972 if (uep != NULL)
2973 {
2974 /*
2975 * the new ue_bot is computed from the number of lines that has been
2976 * inserted (0 - deleted) since calling u_save. This is equal to the
2977 * old line count subtracted from the current line count.
2978 */
2979 extra = curbuf->b_ml.ml_line_count - uep->ue_lcount;
2980 uep->ue_bot = uep->ue_top + uep->ue_size + 1 + extra;
2981 if (uep->ue_bot < 1 || uep->ue_bot > curbuf->b_ml.ml_line_count)
2982 {
2983 EMSG(_("E440: undo line missing"));
2984 uep->ue_bot = uep->ue_top + 1; /* assume all lines deleted, will
2985 * get all the old lines back
2986 * without deleting the current
2987 * ones */
2988 }
2989
2990 curbuf->b_u_newhead->uh_getbot_entry = NULL;
2991 }
2992
2993 curbuf->b_u_synced = TRUE;
2994}
2995
2996/*
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002997 * Free one header "uhp" and its entry list and adjust the pointers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 */
2999 static void
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003000u_freeheader(buf, uhp, uhpp)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003001 buf_T *buf;
Bram Moolenaar1e607892006-03-13 22:15:53 +00003002 u_header_T *uhp;
3003 u_header_T **uhpp; /* if not NULL reset when freeing this header */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004{
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003005 u_header_T *uhap;
3006
Bram Moolenaar1e607892006-03-13 22:15:53 +00003007 /* When there is an alternate redo list free that branch completely,
3008 * because we can never go there. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003009 if (uhp->uh_alt_next.ptr != NULL)
3010 u_freebranch(buf, uhp->uh_alt_next.ptr, uhpp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003012 if (uhp->uh_alt_prev.ptr != NULL)
3013 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014
Bram Moolenaar1e607892006-03-13 22:15:53 +00003015 /* Update the links in the list to remove the header. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003016 if (uhp->uh_next.ptr == NULL)
3017 buf->b_u_oldhead = uhp->uh_prev.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003019 uhp->uh_next.ptr->uh_prev.ptr = uhp->uh_prev.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003021 if (uhp->uh_prev.ptr == NULL)
3022 buf->b_u_newhead = uhp->uh_next.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003024 for (uhap = uhp->uh_prev.ptr; uhap != NULL;
3025 uhap = uhap->uh_alt_next.ptr)
3026 uhap->uh_next.ptr = uhp->uh_next.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027
Bram Moolenaar1e607892006-03-13 22:15:53 +00003028 u_freeentries(buf, uhp, uhpp);
3029}
3030
3031/*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003032 * Free an alternate branch and any following alternate branches.
Bram Moolenaar1e607892006-03-13 22:15:53 +00003033 */
3034 static void
3035u_freebranch(buf, uhp, uhpp)
3036 buf_T *buf;
3037 u_header_T *uhp;
3038 u_header_T **uhpp; /* if not NULL reset when freeing this header */
3039{
3040 u_header_T *tofree, *next;
3041
Bram Moolenaar07d06772007-11-10 21:51:15 +00003042 /* If this is the top branch we may need to use u_freeheader() to update
3043 * all the pointers. */
3044 if (uhp == buf->b_u_oldhead)
3045 {
3046 u_freeheader(buf, uhp, uhpp);
3047 return;
3048 }
3049
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003050 if (uhp->uh_alt_prev.ptr != NULL)
3051 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +00003052
3053 next = uhp;
3054 while (next != NULL)
3055 {
3056 tofree = next;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003057 if (tofree->uh_alt_next.ptr != NULL)
3058 u_freebranch(buf, tofree->uh_alt_next.ptr, uhpp); /* recursive */
3059 next = tofree->uh_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00003060 u_freeentries(buf, tofree, uhpp);
3061 }
3062}
3063
3064/*
3065 * Free all the undo entries for one header and the header itself.
3066 * This means that "uhp" is invalid when returning.
3067 */
3068 static void
3069u_freeentries(buf, uhp, uhpp)
3070 buf_T *buf;
3071 u_header_T *uhp;
3072 u_header_T **uhpp; /* if not NULL reset when freeing this header */
3073{
3074 u_entry_T *uep, *nuep;
3075
3076 /* Check for pointers to the header that become invalid now. */
3077 if (buf->b_u_curhead == uhp)
3078 buf->b_u_curhead = NULL;
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003079 if (buf->b_u_newhead == uhp)
3080 buf->b_u_newhead = NULL; /* freeing the newest entry */
Bram Moolenaar1e607892006-03-13 22:15:53 +00003081 if (uhpp != NULL && uhp == *uhpp)
3082 *uhpp = NULL;
3083
3084 for (uep = uhp->uh_entry; uep != NULL; uep = nuep)
3085 {
3086 nuep = uep->ue_next;
3087 u_freeentry(uep, uep->ue_size);
3088 }
3089
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003090#ifdef U_DEBUG
3091 uhp->uh_magic = 0;
3092#endif
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003093 vim_free((char_u *)uhp);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003094 --buf->b_u_numhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095}
3096
3097/*
3098 * free entry 'uep' and 'n' lines in uep->ue_array[]
3099 */
3100 static void
3101u_freeentry(uep, n)
3102 u_entry_T *uep;
3103 long n;
3104{
Bram Moolenaar8d343302005-07-12 22:46:17 +00003105 while (n > 0)
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003106 vim_free(uep->ue_array[--n]);
3107 vim_free((char_u *)uep->ue_array);
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003108#ifdef U_DEBUG
3109 uep->ue_magic = 0;
3110#endif
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003111 vim_free((char_u *)uep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112}
3113
3114/*
3115 * invalidate the undo buffer; called when storage has already been released
3116 */
3117 void
3118u_clearall(buf)
3119 buf_T *buf;
3120{
3121 buf->b_u_newhead = buf->b_u_oldhead = buf->b_u_curhead = NULL;
3122 buf->b_u_synced = TRUE;
3123 buf->b_u_numhead = 0;
3124 buf->b_u_line_ptr = NULL;
3125 buf->b_u_line_lnum = 0;
3126}
3127
3128/*
3129 * save the line "lnum" for the "U" command
3130 */
3131 void
3132u_saveline(lnum)
3133 linenr_T lnum;
3134{
3135 if (lnum == curbuf->b_u_line_lnum) /* line is already saved */
3136 return;
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00003137 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) /* should never happen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 return;
3139 u_clearline();
3140 curbuf->b_u_line_lnum = lnum;
3141 if (curwin->w_cursor.lnum == lnum)
3142 curbuf->b_u_line_colnr = curwin->w_cursor.col;
3143 else
3144 curbuf->b_u_line_colnr = 0;
3145 if ((curbuf->b_u_line_ptr = u_save_line(lnum)) == NULL)
3146 do_outofmem_msg((long_u)0);
3147}
3148
3149/*
3150 * clear the line saved for the "U" command
3151 * (this is used externally for crossing a line while in insert mode)
3152 */
3153 void
3154u_clearline()
3155{
3156 if (curbuf->b_u_line_ptr != NULL)
3157 {
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003158 vim_free(curbuf->b_u_line_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 curbuf->b_u_line_ptr = NULL;
3160 curbuf->b_u_line_lnum = 0;
3161 }
3162}
3163
3164/*
3165 * Implementation of the "U" command.
3166 * Differentiation from vi: "U" can be undone with the next "U".
3167 * We also allow the cursor to be in another line.
3168 */
3169 void
3170u_undoline()
3171{
3172 colnr_T t;
3173 char_u *oldp;
3174
3175 if (undo_off)
3176 return;
3177
Bram Moolenaare3300c82008-02-13 14:21:38 +00003178 if (curbuf->b_u_line_ptr == NULL
3179 || curbuf->b_u_line_lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 {
3181 beep_flush();
3182 return;
3183 }
Bram Moolenaare3300c82008-02-13 14:21:38 +00003184
3185 /* first save the line for the 'u' command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 if (u_savecommon(curbuf->b_u_line_lnum - 1,
3187 curbuf->b_u_line_lnum + 1, (linenr_T)0) == FAIL)
3188 return;
3189 oldp = u_save_line(curbuf->b_u_line_lnum);
3190 if (oldp == NULL)
3191 {
3192 do_outofmem_msg((long_u)0);
3193 return;
3194 }
3195 ml_replace(curbuf->b_u_line_lnum, curbuf->b_u_line_ptr, TRUE);
3196 changed_bytes(curbuf->b_u_line_lnum, 0);
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003197 vim_free(curbuf->b_u_line_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 curbuf->b_u_line_ptr = oldp;
3199
3200 t = curbuf->b_u_line_colnr;
3201 if (curwin->w_cursor.lnum == curbuf->b_u_line_lnum)
3202 curbuf->b_u_line_colnr = curwin->w_cursor.col;
3203 curwin->w_cursor.col = t;
3204 curwin->w_cursor.lnum = curbuf->b_u_line_lnum;
Bram Moolenaare3300c82008-02-13 14:21:38 +00003205 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206}
3207
3208/*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003209 * Free all allocated memory blocks for the buffer 'buf'.
3210 */
3211 void
3212u_blockfree(buf)
3213 buf_T *buf;
3214{
Bram Moolenaar1e607892006-03-13 22:15:53 +00003215 while (buf->b_u_oldhead != NULL)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003216 u_freeheader(buf, buf->b_u_oldhead, NULL);
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003217 vim_free(buf->b_u_line_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218}
3219
3220/*
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003221 * u_save_line(): allocate memory and copy line 'lnum' into it.
3222 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 */
3224 static char_u *
3225u_save_line(lnum)
3226 linenr_T lnum;
3227{
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003228 return vim_strsave(ml_get(lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229}
3230
3231/*
3232 * Check if the 'modified' flag is set, or 'ff' has changed (only need to
3233 * check the first character, because it can only be "dos", "unix" or "mac").
3234 * "nofile" and "scratch" type buffers are considered to always be unchanged.
3235 */
3236 int
3237bufIsChanged(buf)
3238 buf_T *buf;
3239{
3240 return
3241#ifdef FEAT_QUICKFIX
3242 !bt_dontwrite(buf) &&
3243#endif
3244 (buf->b_changed || file_ff_differs(buf));
3245}
3246
3247 int
3248curbufIsChanged()
3249{
3250 return
3251#ifdef FEAT_QUICKFIX
3252 !bt_dontwrite(curbuf) &&
3253#endif
3254 (curbuf->b_changed || file_ff_differs(curbuf));
3255}
Bram Moolenaara800b422010-06-27 01:15:55 +02003256
3257#if defined(FEAT_EVAL) || defined(PROTO)
3258/*
3259 * For undotree(): Append the list of undo blocks at "first_uhp" to "list".
3260 * Recursive.
3261 */
3262 void
3263u_eval_tree(first_uhp, list)
3264 u_header_T *first_uhp;
3265 list_T *list;
3266{
3267 u_header_T *uhp = first_uhp;
3268 dict_T *dict;
3269
3270 while (uhp != NULL)
3271 {
3272 dict = dict_alloc();
3273 if (dict == NULL)
3274 return;
3275 dict_add_nr_str(dict, "seq", uhp->uh_seq, NULL);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02003276 dict_add_nr_str(dict, "time", (long)uhp->uh_time, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +02003277 if (uhp == curbuf->b_u_newhead)
3278 dict_add_nr_str(dict, "newhead", 1, NULL);
3279 if (uhp == curbuf->b_u_curhead)
3280 dict_add_nr_str(dict, "curhead", 1, NULL);
3281 if (uhp->uh_save_nr > 0)
3282 dict_add_nr_str(dict, "save", uhp->uh_save_nr, NULL);
3283
3284 if (uhp->uh_alt_next.ptr != NULL)
3285 {
3286 list_T *alt_list = list_alloc();
3287
3288 if (alt_list != NULL)
3289 {
3290 /* Recursive call to add alternate undo tree. */
3291 u_eval_tree(uhp->uh_alt_next.ptr, alt_list);
3292 dict_add_list(dict, "alt", alt_list);
3293 }
3294 }
3295
3296 list_append_dict(list, dict);
3297 uhp = uhp->uh_prev.ptr;
3298 }
3299}
3300#endif