blob: 94918e0f691fe23bdbb6b7d2ed3904aa71c76417 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * undo.c: multi level undo facility
12 *
13 * The saved lines are stored in a list of lists (one for each buffer):
14 *
15 * b_u_oldhead------------------------------------------------+
16 * |
17 * V
18 * +--------------+ +--------------+ +--------------+
19 * b_u_newhead--->| u_header | | u_header | | u_header |
20 * | uh_next------>| uh_next------>| uh_next---->NULL
21 * NULL<--------uh_prev |<---------uh_prev |<---------uh_prev |
22 * | uh_entry | | uh_entry | | uh_entry |
23 * +--------|-----+ +--------|-----+ +--------|-----+
24 * | | |
25 * V V V
26 * +--------------+ +--------------+ +--------------+
27 * | u_entry | | u_entry | | u_entry |
28 * | ue_next | | ue_next | | ue_next |
29 * +--------|-----+ +--------|-----+ +--------|-----+
30 * | | |
31 * V V V
32 * +--------------+ NULL NULL
33 * | u_entry |
34 * | ue_next |
35 * +--------|-----+
36 * |
37 * V
38 * etc.
39 *
40 * Each u_entry list contains the information for one undo or redo.
41 * curbuf->b_u_curhead points to the header of the last undo (the next redo),
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +000042 * or is NULL if nothing has been undone (end of the branch).
Bram Moolenaar071d4272004-06-13 20:20:40 +000043 *
Bram Moolenaar1e607892006-03-13 22:15:53 +000044 * For keeping alternate undo/redo branches the uh_alt field is used. Thus at
45 * each point in the list a branch may appear for an alternate to redo. The
46 * uh_seq field is numbered sequentially to be able to find a newer or older
47 * branch.
48 *
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +000049 * +---------------+ +---------------+
50 * b_u_oldhead --->| u_header | | u_header |
51 * | uh_alt_next ---->| uh_alt_next ----> NULL
52 * NULL <----- uh_alt_prev |<------ uh_alt_prev |
53 * | uh_prev | | uh_prev |
54 * +-----|---------+ +-----|---------+
55 * | |
56 * V V
57 * +---------------+ +---------------+
58 * | u_header | | u_header |
59 * | uh_alt_next | | uh_alt_next |
60 * b_u_newhead --->| uh_alt_prev | | uh_alt_prev |
61 * | uh_prev | | uh_prev |
62 * +-----|---------+ +-----|---------+
63 * | |
64 * V V
65 * NULL +---------------+ +---------------+
66 * | u_header | | u_header |
67 * | uh_alt_next ---->| uh_alt_next |
68 * | uh_alt_prev |<------ uh_alt_prev |
69 * | uh_prev | | uh_prev |
70 * +-----|---------+ +-----|---------+
71 * | |
72 * etc. etc.
73 *
74 *
Bram Moolenaarf05e3b02010-05-29 15:40:47 +020075 * All data is allocated and will all be freed when the buffer is unloaded.
Bram Moolenaar071d4272004-06-13 20:20:40 +000076 */
77
Bram Moolenaarfecb6602007-10-01 20:54:15 +000078/* Uncomment the next line for including the u_check() function. This warns
79 * for errors in the debug information. */
80/* #define U_DEBUG 1 */
81#define UH_MAGIC 0x18dade /* value for uh_magic when in use */
82#define UE_MAGIC 0xabc123 /* value for ue_magic when in use */
83
Bram Moolenaar071d4272004-06-13 20:20:40 +000084#include "vim.h"
85
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +010086static long get_undolevel __ARGS((void));
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +000087static void u_unch_branch __ARGS((u_header_T *uhp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000088static u_entry_T *u_get_headentry __ARGS((void));
89static void u_getbot __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000090static void u_doit __ARGS((int count));
Bram Moolenaarca003e12006-03-17 23:19:38 +000091static void u_undoredo __ARGS((int undo));
Bram Moolenaardb552d602006-03-23 22:59:57 +000092static void u_undo_end __ARGS((int did_undo, int absolute));
Bram Moolenaarefd2bf12006-03-16 21:41:35 +000093static void u_add_time __ARGS((char_u *buf, size_t buflen, time_t tt));
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +000094static void u_freeheader __ARGS((buf_T *buf, u_header_T *uhp, u_header_T **uhpp));
Bram Moolenaar1e607892006-03-13 22:15:53 +000095static void u_freebranch __ARGS((buf_T *buf, u_header_T *uhp, u_header_T **uhpp));
96static void u_freeentries __ARGS((buf_T *buf, u_header_T *uhp, u_header_T **uhpp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000097static void u_freeentry __ARGS((u_entry_T *, long));
Bram Moolenaar55debbe2010-05-23 23:34:36 +020098#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020099static void corruption_error __ARGS((char *mesg, char_u *file_name));
Bram Moolenaar6a18eb62010-05-26 21:21:00 +0200100static void u_free_uhp __ARGS((u_header_T *uhp));
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200101static size_t fwrite_crypt __ARGS((buf_T *buf UNUSED, char_u *ptr, size_t len, FILE *fp));
102static char_u *read_string_decrypt __ARGS((buf_T *buf UNUSED, FILE *fd, int len));
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +0200103static int serialize_header __ARGS((FILE *fp, buf_T *buf, char_u *hash));
Bram Moolenaara3ff49f2010-05-30 22:48:02 +0200104static int serialize_uhp __ARGS((FILE *fp, buf_T *buf, u_header_T *uhp));
Bram Moolenaar69154f22010-07-18 21:42:34 +0200105static u_header_T *unserialize_uhp __ARGS((FILE *fp, char_u *file_name));
Bram Moolenaara3ff49f2010-05-30 22:48:02 +0200106static int serialize_uep __ARGS((FILE *fp, buf_T *buf, u_entry_T *uep));
Bram Moolenaar9db58062010-05-29 20:33:07 +0200107static u_entry_T *unserialize_uep __ARGS((FILE *fp, int *error, char_u *file_name));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200108static void serialize_pos __ARGS((pos_T pos, FILE *fp));
Bram Moolenaar9db58062010-05-29 20:33:07 +0200109static void unserialize_pos __ARGS((pos_T *pos, FILE *fp));
Bram Moolenaarcdf04202010-05-29 15:11:47 +0200110static void serialize_visualinfo __ARGS((visualinfo_T *info, FILE *fp));
Bram Moolenaar9db58062010-05-29 20:33:07 +0200111static void unserialize_visualinfo __ARGS((visualinfo_T *info, FILE *fp));
112static void put_header_ptr __ARGS((FILE *fp, u_header_T *uhp));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114
Bram Moolenaarf05e3b02010-05-29 15:40:47 +0200115#define U_ALLOC_LINE(size) lalloc((long_u)(size), FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116static char_u *u_save_line __ARGS((linenr_T));
117
Bram Moolenaar730cde92010-06-27 05:18:54 +0200118/* used in undo_end() to report number of added and deleted lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119static long u_newcount, u_oldcount;
120
121/*
122 * When 'u' flag included in 'cpoptions', we behave like vi. Need to remember
123 * the action that "u" should do.
124 */
125static int undo_undoes = FALSE;
126
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200127static int lastmark = 0;
128
Bram Moolenaar9db58062010-05-29 20:33:07 +0200129#if defined(U_DEBUG) || defined(PROTO)
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000130/*
131 * Check the undo structures for being valid. Print a warning when something
132 * looks wrong.
133 */
134static int seen_b_u_curhead;
135static int seen_b_u_newhead;
136static int header_count;
137
138 static void
139u_check_tree(u_header_T *uhp,
140 u_header_T *exp_uh_next,
141 u_header_T *exp_uh_alt_prev)
142{
143 u_entry_T *uep;
144
145 if (uhp == NULL)
146 return;
147 ++header_count;
148 if (uhp == curbuf->b_u_curhead && ++seen_b_u_curhead > 1)
149 {
150 EMSG("b_u_curhead found twice (looping?)");
151 return;
152 }
153 if (uhp == curbuf->b_u_newhead && ++seen_b_u_newhead > 1)
154 {
155 EMSG("b_u_newhead found twice (looping?)");
156 return;
157 }
158
159 if (uhp->uh_magic != UH_MAGIC)
160 EMSG("uh_magic wrong (may be using freed memory)");
161 else
162 {
163 /* Check pointers back are correct. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200164 if (uhp->uh_next.ptr != exp_uh_next)
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000165 {
166 EMSG("uh_next wrong");
167 smsg((char_u *)"expected: 0x%x, actual: 0x%x",
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200168 exp_uh_next, uhp->uh_next.ptr);
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000169 }
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200170 if (uhp->uh_alt_prev.ptr != exp_uh_alt_prev)
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000171 {
172 EMSG("uh_alt_prev wrong");
173 smsg((char_u *)"expected: 0x%x, actual: 0x%x",
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200174 exp_uh_alt_prev, uhp->uh_alt_prev.ptr);
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000175 }
176
177 /* Check the undo tree at this header. */
178 for (uep = uhp->uh_entry; uep != NULL; uep = uep->ue_next)
179 {
180 if (uep->ue_magic != UE_MAGIC)
181 {
182 EMSG("ue_magic wrong (may be using freed memory)");
183 break;
184 }
185 }
186
187 /* Check the next alt tree. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200188 u_check_tree(uhp->uh_alt_next.ptr, uhp->uh_next.ptr, uhp);
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000189
190 /* Check the next header in this branch. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200191 u_check_tree(uhp->uh_prev.ptr, uhp, NULL);
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000192 }
193}
194
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200195 static void
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000196u_check(int newhead_may_be_NULL)
197{
198 seen_b_u_newhead = 0;
199 seen_b_u_curhead = 0;
200 header_count = 0;
201
202 u_check_tree(curbuf->b_u_oldhead, NULL, NULL);
203
204 if (seen_b_u_newhead == 0 && curbuf->b_u_oldhead != NULL
205 && !(newhead_may_be_NULL && curbuf->b_u_newhead == NULL))
206 EMSGN("b_u_newhead invalid: 0x%x", curbuf->b_u_newhead);
207 if (curbuf->b_u_curhead != NULL && seen_b_u_curhead == 0)
208 EMSGN("b_u_curhead invalid: 0x%x", curbuf->b_u_curhead);
209 if (header_count != curbuf->b_u_numhead)
210 {
211 EMSG("b_u_numhead invalid");
212 smsg((char_u *)"expected: %ld, actual: %ld",
213 (long)header_count, (long)curbuf->b_u_numhead);
214 }
215}
216#endif
217
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000219 * Save the current line for both the "u" and "U" command.
Bram Moolenaar687a29c2013-04-15 15:47:12 +0200220 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000221 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222 */
223 int
224u_save_cursor()
225{
226 return (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
227 (linenr_T)(curwin->w_cursor.lnum + 1)));
228}
229
230/*
231 * Save the lines between "top" and "bot" for both the "u" and "U" command.
232 * "top" may be 0 and bot may be curbuf->b_ml.ml_line_count + 1.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200233 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 * Returns FAIL when lines could not be saved, OK otherwise.
235 */
236 int
237u_save(top, bot)
238 linenr_T top, bot;
239{
240 if (undo_off)
241 return OK;
242
Bram Moolenaar687a29c2013-04-15 15:47:12 +0200243 if (top > curbuf->b_ml.ml_line_count
244 || top >= bot
245 || bot > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 return FALSE; /* rely on caller to do error messages */
247
248 if (top + 2 == bot)
249 u_saveline((linenr_T)(top + 1));
250
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200251 return (u_savecommon(top, bot, (linenr_T)0, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252}
253
254/*
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200255 * Save the line "lnum" (used by ":s" and "~" command).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256 * The line is replaced, so the new bottom line is lnum + 1.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200257 * Careful: may trigger autocommands that reload the buffer.
258 * Returns FAIL when lines could not be saved, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 */
260 int
261u_savesub(lnum)
262 linenr_T lnum;
263{
264 if (undo_off)
265 return OK;
266
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200267 return (u_savecommon(lnum - 1, lnum + 1, lnum + 1, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268}
269
270/*
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200271 * A new line is inserted before line "lnum" (used by :s command).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 * The line is inserted, so the new bottom line is lnum + 1.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200273 * Careful: may trigger autocommands that reload the buffer.
274 * Returns FAIL when lines could not be saved, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 */
276 int
277u_inssub(lnum)
278 linenr_T lnum;
279{
280 if (undo_off)
281 return OK;
282
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200283 return (u_savecommon(lnum - 1, lnum, lnum + 1, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284}
285
286/*
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200287 * Save the lines "lnum" - "lnum" + nlines (used by delete command).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 * The lines are deleted, so the new bottom line is lnum, unless the buffer
289 * becomes empty.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200290 * Careful: may trigger autocommands that reload the buffer.
291 * Returns FAIL when lines could not be saved, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 */
293 int
294u_savedel(lnum, nlines)
295 linenr_T lnum;
296 long nlines;
297{
298 if (undo_off)
299 return OK;
300
301 return (u_savecommon(lnum - 1, lnum + nlines,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200302 nlines == curbuf->b_ml.ml_line_count ? 2 : lnum, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303}
304
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000305/*
306 * Return TRUE when undo is allowed. Otherwise give an error message and
307 * return FALSE.
308 */
Bram Moolenaarce6ef252006-07-12 19:49:41 +0000309 int
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000310undo_allowed()
311{
312 /* Don't allow changes when 'modifiable' is off. */
313 if (!curbuf->b_p_ma)
314 {
315 EMSG(_(e_modifiable));
316 return FALSE;
317 }
318
319#ifdef HAVE_SANDBOX
320 /* In the sandbox it's not allowed to change the text. */
321 if (sandbox != 0)
322 {
323 EMSG(_(e_sandbox));
324 return FALSE;
325 }
326#endif
327
328 /* Don't allow changes in the buffer while editing the cmdline. The
329 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000330 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000331 {
332 EMSG(_(e_secure));
333 return FALSE;
334 }
335
336 return TRUE;
337}
338
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200339/*
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100340 * Get the undolevle value for the current buffer.
341 */
342 static long
343get_undolevel()
344{
345 if (curbuf->b_p_ul == NO_LOCAL_UNDOLEVEL)
346 return p_ul;
347 return curbuf->b_p_ul;
348}
349
350/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200351 * Common code for various ways to save text before a change.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200352 * "top" is the line above the first changed line.
353 * "bot" is the line below the last changed line.
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200354 * "newbot" is the new bottom line. Use zero when not known.
355 * "reload" is TRUE when saving for a buffer reload.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200356 * Careful: may trigger autocommands that reload the buffer.
357 * Returns FAIL when lines could not be saved, OK otherwise.
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200358 */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200359 int
360u_savecommon(top, bot, newbot, reload)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 linenr_T top, bot;
362 linenr_T newbot;
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200363 int reload;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364{
Bram Moolenaar1e607892006-03-13 22:15:53 +0000365 linenr_T lnum;
366 long i;
367 u_header_T *uhp;
368 u_header_T *old_curhead;
369 u_entry_T *uep;
370 u_entry_T *prev_uep;
371 long size;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200373 if (!reload)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 {
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200375 /* When making changes is not allowed return FAIL. It's a crude way
376 * to make all change commands fail. */
377 if (!undo_allowed())
Bram Moolenaar009b2592004-10-24 19:18:58 +0000378 return FAIL;
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200379
380#ifdef FEAT_NETBEANS_INTG
381 /*
382 * Netbeans defines areas that cannot be modified. Bail out here when
383 * trying to change text in a guarded area.
384 */
385 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +0000386 {
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200387 if (netbeans_is_guarded(top, bot))
388 {
389 EMSG(_(e_guarded));
390 return FAIL;
391 }
392 if (curbuf->b_p_ro)
393 {
394 EMSG(_(e_nbreadonly));
395 return FAIL;
396 }
Bram Moolenaar009b2592004-10-24 19:18:58 +0000397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398#endif
399
400#ifdef FEAT_AUTOCMD
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200401 /*
402 * Saving text for undo means we are going to make a change. Give a
403 * warning for a read-only file before making the change, so that the
404 * FileChangedRO event can replace the buffer with a read-write version
405 * (e.g., obtained from a source control system).
406 */
407 change_warning(0);
408 if (bot > curbuf->b_ml.ml_line_count + 1)
409 {
410 /* This happens when the FileChangedRO autocommand changes the
411 * file in a way it becomes shorter. */
Bram Moolenaarb4d587c2014-01-23 18:12:49 +0100412 EMSG(_("E881: Line count changed unexpectedly"));
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200413 return FAIL;
414 }
415#endif
Bram Moolenaard04b7502010-07-08 22:27:55 +0200416 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200417
418#ifdef U_DEBUG
419 u_check(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420#endif
421
422 size = bot - top - 1;
423
424 /*
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200425 * If curbuf->b_u_synced == TRUE make a new header.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 */
427 if (curbuf->b_u_synced)
428 {
429#ifdef FEAT_JUMPLIST
430 /* Need to create new entry in b_changelist. */
431 curbuf->b_new_change = TRUE;
432#endif
433
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100434 if (get_undolevel() >= 0)
Bram Moolenaar1e607892006-03-13 22:15:53 +0000435 {
436 /*
437 * Make a new header entry. Do this first so that we don't mess
438 * up the undo info when out of memory.
439 */
Bram Moolenaarf05e3b02010-05-29 15:40:47 +0200440 uhp = (u_header_T *)U_ALLOC_LINE(sizeof(u_header_T));
Bram Moolenaar1e607892006-03-13 22:15:53 +0000441 if (uhp == NULL)
442 goto nomem;
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000443#ifdef U_DEBUG
444 uhp->uh_magic = UH_MAGIC;
445#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +0000446 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000447 else
448 uhp = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000449
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 /*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000451 * If we undid more than we redid, move the entry lists before and
452 * including curbuf->b_u_curhead to an alternate branch.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 */
Bram Moolenaar1e607892006-03-13 22:15:53 +0000454 old_curhead = curbuf->b_u_curhead;
455 if (old_curhead != NULL)
456 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200457 curbuf->b_u_newhead = old_curhead->uh_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000458 curbuf->b_u_curhead = NULL;
459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460
461 /*
462 * free headers to keep the size right
463 */
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100464 while (curbuf->b_u_numhead > get_undolevel()
465 && curbuf->b_u_oldhead != NULL)
Bram Moolenaar1e607892006-03-13 22:15:53 +0000466 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000467 u_header_T *uhfree = curbuf->b_u_oldhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000468
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000469 if (uhfree == old_curhead)
470 /* Can't reconnect the branch, delete all of it. */
471 u_freebranch(curbuf, uhfree, &old_curhead);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200472 else if (uhfree->uh_alt_next.ptr == NULL)
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000473 /* There is no branch, only free one header. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000474 u_freeheader(curbuf, uhfree, &old_curhead);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000475 else
476 {
477 /* Free the oldest alternate branch as a whole. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200478 while (uhfree->uh_alt_next.ptr != NULL)
479 uhfree = uhfree->uh_alt_next.ptr;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000480 u_freebranch(curbuf, uhfree, &old_curhead);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000481 }
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000482#ifdef U_DEBUG
483 u_check(TRUE);
484#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +0000485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000487 if (uhp == NULL) /* no undo at all */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 {
Bram Moolenaar1e607892006-03-13 22:15:53 +0000489 if (old_curhead != NULL)
490 u_freebranch(curbuf, old_curhead, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 curbuf->b_u_synced = FALSE;
492 return OK;
493 }
494
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200495 uhp->uh_prev.ptr = NULL;
496 uhp->uh_next.ptr = curbuf->b_u_newhead;
497 uhp->uh_alt_next.ptr = old_curhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000498 if (old_curhead != NULL)
499 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200500 uhp->uh_alt_prev.ptr = old_curhead->uh_alt_prev.ptr;
501 if (uhp->uh_alt_prev.ptr != NULL)
502 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = uhp;
503 old_curhead->uh_alt_prev.ptr = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000504 if (curbuf->b_u_oldhead == old_curhead)
505 curbuf->b_u_oldhead = uhp;
506 }
Bram Moolenaar89ed3df2007-01-09 19:23:12 +0000507 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200508 uhp->uh_alt_prev.ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 if (curbuf->b_u_newhead != NULL)
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200510 curbuf->b_u_newhead->uh_prev.ptr = uhp;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000511
Bram Moolenaarca003e12006-03-17 23:19:38 +0000512 uhp->uh_seq = ++curbuf->b_u_seq_last;
513 curbuf->b_u_seq_cur = uhp->uh_seq;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000514 uhp->uh_time = time(NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +0200515 uhp->uh_save_nr = 0;
516 curbuf->b_u_time_cur = uhp->uh_time + 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000517
Bram Moolenaar1e607892006-03-13 22:15:53 +0000518 uhp->uh_walk = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 uhp->uh_entry = NULL;
520 uhp->uh_getbot_entry = NULL;
521 uhp->uh_cursor = curwin->w_cursor; /* save cursor pos. for undo */
522#ifdef FEAT_VIRTUALEDIT
523 if (virtual_active() && curwin->w_cursor.coladd > 0)
524 uhp->uh_cursor_vcol = getviscol();
525 else
526 uhp->uh_cursor_vcol = -1;
527#endif
528
529 /* save changed and buffer empty flag for undo */
530 uhp->uh_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
531 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
532
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000533 /* save named marks and Visual marks for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 mch_memmove(uhp->uh_namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000535#ifdef FEAT_VISUAL
536 uhp->uh_visual = curbuf->b_visual;
537#endif
538
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 curbuf->b_u_newhead = uhp;
540 if (curbuf->b_u_oldhead == NULL)
541 curbuf->b_u_oldhead = uhp;
542 ++curbuf->b_u_numhead;
543 }
544 else
545 {
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100546 if (get_undolevel() < 0) /* no undo at all */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 return OK;
548
549 /*
550 * When saving a single line, and it has been saved just before, it
551 * doesn't make sense saving it again. Saves a lot of memory when
552 * making lots of changes inside the same line.
553 * This is only possible if the previous change didn't increase or
554 * decrease the number of lines.
555 * Check the ten last changes. More doesn't make sense and takes too
556 * long.
557 */
558 if (size == 1)
559 {
560 uep = u_get_headentry();
561 prev_uep = NULL;
562 for (i = 0; i < 10; ++i)
563 {
564 if (uep == NULL)
565 break;
566
567 /* If lines have been inserted/deleted we give up.
568 * Also when the line was included in a multi-line save. */
569 if ((curbuf->b_u_newhead->uh_getbot_entry != uep
570 ? (uep->ue_top + uep->ue_size + 1
571 != (uep->ue_bot == 0
572 ? curbuf->b_ml.ml_line_count + 1
573 : uep->ue_bot))
574 : uep->ue_lcount != curbuf->b_ml.ml_line_count)
575 || (uep->ue_size > 1
576 && top >= uep->ue_top
577 && top + 2 <= uep->ue_top + uep->ue_size + 1))
578 break;
579
580 /* If it's the same line we can skip saving it again. */
581 if (uep->ue_size == 1 && uep->ue_top == top)
582 {
583 if (i > 0)
584 {
585 /* It's not the last entry: get ue_bot for the last
586 * entry now. Following deleted/inserted lines go to
587 * the re-used entry. */
588 u_getbot();
589 curbuf->b_u_synced = FALSE;
590
591 /* Move the found entry to become the last entry. The
592 * order of undo/redo doesn't matter for the entries
593 * we move it over, since they don't change the line
594 * count and don't include this line. It does matter
595 * for the found entry if the line count is changed by
596 * the executed command. */
597 prev_uep->ue_next = uep->ue_next;
598 uep->ue_next = curbuf->b_u_newhead->uh_entry;
599 curbuf->b_u_newhead->uh_entry = uep;
600 }
601
602 /* The executed command may change the line count. */
603 if (newbot != 0)
604 uep->ue_bot = newbot;
605 else if (bot > curbuf->b_ml.ml_line_count)
606 uep->ue_bot = 0;
607 else
608 {
609 uep->ue_lcount = curbuf->b_ml.ml_line_count;
610 curbuf->b_u_newhead->uh_getbot_entry = uep;
611 }
612 return OK;
613 }
614 prev_uep = uep;
615 uep = uep->ue_next;
616 }
617 }
618
619 /* find line number for ue_bot for previous u_save() */
620 u_getbot();
621 }
622
623#if !defined(UNIX) && !defined(DJGPP) && !defined(WIN32) && !defined(__EMX__)
624 /*
625 * With Amiga and MSDOS 16 bit we can't handle big undo's, because
626 * then u_alloc_line would have to allocate a block larger than 32K
627 */
628 if (size >= 8000)
629 goto nomem;
630#endif
631
632 /*
633 * add lines in front of entry list
634 */
Bram Moolenaarf05e3b02010-05-29 15:40:47 +0200635 uep = (u_entry_T *)U_ALLOC_LINE(sizeof(u_entry_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 if (uep == NULL)
637 goto nomem;
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200638 vim_memset(uep, 0, sizeof(u_entry_T));
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000639#ifdef U_DEBUG
640 uep->ue_magic = UE_MAGIC;
641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642
643 uep->ue_size = size;
644 uep->ue_top = top;
645 if (newbot != 0)
646 uep->ue_bot = newbot;
647 /*
648 * Use 0 for ue_bot if bot is below last line.
649 * Otherwise we have to compute ue_bot later.
650 */
651 else if (bot > curbuf->b_ml.ml_line_count)
652 uep->ue_bot = 0;
653 else
654 {
655 uep->ue_lcount = curbuf->b_ml.ml_line_count;
656 curbuf->b_u_newhead->uh_getbot_entry = uep;
657 }
658
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000659 if (size > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000661 if ((uep->ue_array = (char_u **)U_ALLOC_LINE(
Bram Moolenaarf05e3b02010-05-29 15:40:47 +0200662 sizeof(char_u *) * size)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 {
664 u_freeentry(uep, 0L);
665 goto nomem;
666 }
667 for (i = 0, lnum = top + 1; i < size; ++i)
668 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000669 fast_breakcheck();
670 if (got_int)
671 {
672 u_freeentry(uep, i);
673 return FAIL;
674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 if ((uep->ue_array[i] = u_save_line(lnum++)) == NULL)
676 {
677 u_freeentry(uep, i);
678 goto nomem;
679 }
680 }
681 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000682 else
683 uep->ue_array = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 uep->ue_next = curbuf->b_u_newhead->uh_entry;
685 curbuf->b_u_newhead->uh_entry = uep;
686 curbuf->b_u_synced = FALSE;
687 undo_undoes = FALSE;
688
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000689#ifdef U_DEBUG
690 u_check(FALSE);
691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 return OK;
693
694nomem:
695 msg_silent = 0; /* must display the prompt */
696 if (ask_yesno((char_u *)_("No undo possible; continue anyway"), TRUE)
697 == 'y')
698 {
699 undo_off = TRUE; /* will be reset when character typed */
700 return OK;
701 }
702 do_outofmem_msg((long_u)0);
703 return FAIL;
704}
705
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200706#if defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200707
Bram Moolenaar9db58062010-05-29 20:33:07 +0200708# define UF_START_MAGIC "Vim\237UnDo\345" /* magic at start of undofile */
709# define UF_START_MAGIC_LEN 9
710# define UF_HEADER_MAGIC 0x5fd0 /* magic at start of header */
711# define UF_HEADER_END_MAGIC 0xe7aa /* magic after last header */
712# define UF_ENTRY_MAGIC 0xf518 /* magic at start of entry */
713# define UF_ENTRY_END_MAGIC 0x3581 /* magic after last entry */
Bram Moolenaara800b422010-06-27 01:15:55 +0200714# define UF_VERSION 2 /* 2-byte undofile version number */
Bram Moolenaara800b422010-06-27 01:15:55 +0200715# define UF_VERSION_CRYPT 0x8002 /* idem, encrypted */
716
717/* extra fields for header */
718# define UF_LAST_SAVE_NR 1
719
720/* extra fields for uhp */
721# define UHP_SAVE_NR 1
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200722
Bram Moolenaarcdf04202010-05-29 15:11:47 +0200723static char_u e_not_open[] = N_("E828: Cannot open undo file for writing: %s");
Bram Moolenaarcdf04202010-05-29 15:11:47 +0200724
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200725/*
726 * Compute the hash for the current buffer text into hash[UNDO_HASH_SIZE].
727 */
728 void
729u_compute_hash(hash)
730 char_u *hash;
731{
732 context_sha256_T ctx;
733 linenr_T lnum;
734 char_u *p;
735
736 sha256_start(&ctx);
Bram Moolenaarae7ba982011-12-08 15:14:09 +0100737 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200738 {
739 p = ml_get(lnum);
Bram Moolenaar442b4222010-05-24 21:34:22 +0200740 sha256_update(&ctx, p, (UINT32_T)(STRLEN(p) + 1));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200741 }
742 sha256_finish(&ctx, hash);
743}
744
745/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200746 * Return an allocated string of the full path of the target undofile.
747 * When "reading" is TRUE find the file to read, go over all directories in
748 * 'undodir'.
749 * When "reading" is FALSE use the first name where the directory exists.
Bram Moolenaar9db58062010-05-29 20:33:07 +0200750 * Returns NULL when there is no place to write or no file to read.
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200751 */
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200752 char_u *
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200753u_get_undo_file_name(buf_ffname, reading)
754 char_u *buf_ffname;
755 int reading;
756{
757 char_u *dirp;
758 char_u dir_name[IOSIZE + 1];
759 char_u *munged_name = NULL;
760 char_u *undo_file_name = NULL;
761 int dir_len;
762 char_u *p;
763 struct stat st;
764 char_u *ffname = buf_ffname;
765#ifdef HAVE_READLINK
766 char_u fname_buf[MAXPATHL];
767#endif
768
769 if (ffname == NULL)
770 return NULL;
771
772#ifdef HAVE_READLINK
773 /* Expand symlink in the file name, so that we put the undo file with the
774 * actual file instead of with the symlink. */
775 if (resolve_symlink(ffname, fname_buf) == OK)
776 ffname = fname_buf;
777#endif
778
779 /* Loop over 'undodir'. When reading find the first file that exists.
780 * When not reading use the first directory that exists or ".". */
781 dirp = p_udir;
782 while (*dirp != NUL)
783 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200784 dir_len = copy_option_part(&dirp, dir_name, IOSIZE, ",");
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200785 if (dir_len == 1 && dir_name[0] == '.')
786 {
787 /* Use same directory as the ffname,
788 * "dir/name" -> "dir/.name.un~" */
Bram Moolenaar442b4222010-05-24 21:34:22 +0200789 undo_file_name = vim_strnsave(ffname, (int)(STRLEN(ffname) + 5));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200790 if (undo_file_name == NULL)
791 break;
792 p = gettail(undo_file_name);
Bram Moolenaar206f0112014-03-12 16:51:55 +0100793#ifdef VMS
794 /* VMS can not handle more than one dot in the filenames
795 * use "dir/name" -> "dir/_un_name" - add _un_
796 * at the beginning to keep the extension */
797 mch_memmove(p + 4, p, STRLEN(p) + 1);
798 mch_memmove(p, "_un_", 4);
799
800#else
801 /* Use same directory as the ffname,
802 * "dir/name" -> "dir/.name.un~" */
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200803 mch_memmove(p + 1, p, STRLEN(p) + 1);
804 *p = '.';
805 STRCAT(p, ".un~");
Bram Moolenaar206f0112014-03-12 16:51:55 +0100806#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200807 }
808 else
809 {
810 dir_name[dir_len] = NUL;
811 if (mch_isdir(dir_name))
812 {
813 if (munged_name == NULL)
814 {
815 munged_name = vim_strsave(ffname);
816 if (munged_name == NULL)
817 return NULL;
818 for (p = munged_name; *p != NUL; mb_ptr_adv(p))
819 if (vim_ispathsep(*p))
820 *p = '%';
821 }
822 undo_file_name = concat_fnames(dir_name, munged_name, TRUE);
823 }
824 }
825
826 /* When reading check if the file exists. */
827 if (undo_file_name != NULL && (!reading
828 || mch_stat((char *)undo_file_name, &st) >= 0))
829 break;
830 vim_free(undo_file_name);
831 undo_file_name = NULL;
832 }
833
834 vim_free(munged_name);
835 return undo_file_name;
836}
837
Bram Moolenaar9db58062010-05-29 20:33:07 +0200838 static void
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200839corruption_error(mesg, file_name)
840 char *mesg;
Bram Moolenaar9db58062010-05-29 20:33:07 +0200841 char_u *file_name;
842{
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200843 EMSG3(_("E825: Corrupted undo file (%s): %s"), mesg, file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +0200844}
845
846 static void
847u_free_uhp(uhp)
848 u_header_T *uhp;
849{
850 u_entry_T *nuep;
851 u_entry_T *uep;
852
853 uep = uhp->uh_entry;
854 while (uep != NULL)
855 {
856 nuep = uep->ue_next;
857 u_freeentry(uep, uep->ue_size);
858 uep = nuep;
859 }
860 vim_free(uhp);
861}
862
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200863/*
864 * Like fwrite() but crypt the bytes when 'key' is set.
865 * Returns 1 if successful.
866 */
867 static size_t
868fwrite_crypt(buf, ptr, len, fp)
869 buf_T *buf UNUSED;
870 char_u *ptr;
871 size_t len;
872 FILE *fp;
873{
874#ifdef FEAT_CRYPT
875 char_u *copy;
876 char_u small_buf[100];
877 size_t i;
878
879 if (*buf->b_p_key == NUL)
880 return fwrite(ptr, len, (size_t)1, fp);
881 if (len < 100)
882 copy = small_buf; /* no malloc()/free() for short strings */
883 else
884 {
885 copy = lalloc(len, FALSE);
886 if (copy == NULL)
887 return 0;
888 }
889 crypt_encode(ptr, len, copy);
890 i = fwrite(copy, len, (size_t)1, fp);
891 if (copy != small_buf)
892 vim_free(copy);
893 return i;
894#else
895 return fwrite(ptr, len, (size_t)1, fp);
896#endif
897}
898
899/*
900 * Read a string of length "len" from "fd".
901 * When 'key' is set decrypt the bytes.
902 */
903 static char_u *
904read_string_decrypt(buf, fd, len)
905 buf_T *buf UNUSED;
906 FILE *fd;
907 int len;
908{
909 char_u *ptr;
910
911 ptr = read_string(fd, len);
912#ifdef FEAT_CRYPT
Bram Moolenaarfa0ff9a2010-07-25 16:05:19 +0200913 if (ptr != NULL && *buf->b_p_key != NUL)
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200914 crypt_decode(ptr, len);
915#endif
916 return ptr;
917}
918
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +0200919 static int
920serialize_header(fp, buf, hash)
921 FILE *fp;
922 buf_T *buf;
923 char_u *hash;
924{
925 int len;
926
927 /* Start writing, first the magic marker and undo info version. */
928 if (fwrite(UF_START_MAGIC, (size_t)UF_START_MAGIC_LEN, (size_t)1, fp) != 1)
929 return FAIL;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +0200930
931 /* If the buffer is encrypted then all text bytes following will be
932 * encrypted. Numbers and other info is not crypted. */
933#ifdef FEAT_CRYPT
Bram Moolenaarfa0ff9a2010-07-25 16:05:19 +0200934 if (*buf->b_p_key != NUL)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +0200935 {
936 char_u *header;
937 int header_len;
938
939 put_bytes(fp, (long_u)UF_VERSION_CRYPT, 2);
940 header = prepare_crypt_write(buf, &header_len);
941 if (header == NULL)
942 return FAIL;
Bram Moolenaarbbd6afe2010-06-02 20:32:23 +0200943 len = (int)fwrite(header, (size_t)header_len, (size_t)1, fp);
Bram Moolenaara3ff49f2010-05-30 22:48:02 +0200944 vim_free(header);
945 if (len != 1)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200946 {
947 crypt_pop_state();
Bram Moolenaara3ff49f2010-05-30 22:48:02 +0200948 return FAIL;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200949 }
Bram Moolenaara3ff49f2010-05-30 22:48:02 +0200950 }
951 else
952#endif
953 put_bytes(fp, (long_u)UF_VERSION, 2);
954
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +0200955
956 /* Write a hash of the buffer text, so that we can verify it is still the
957 * same when reading the buffer text. */
958 if (fwrite(hash, (size_t)UNDO_HASH_SIZE, (size_t)1, fp) != 1)
959 return FAIL;
960
961 /* buffer-specific data */
962 put_bytes(fp, (long_u)buf->b_ml.ml_line_count, 4);
963 len = buf->b_u_line_ptr != NULL ? (int)STRLEN(buf->b_u_line_ptr) : 0;
964 put_bytes(fp, (long_u)len, 4);
Bram Moolenaara3ff49f2010-05-30 22:48:02 +0200965 if (len > 0 && fwrite_crypt(buf, buf->b_u_line_ptr, (size_t)len, fp) != 1)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +0200966 return FAIL;
967 put_bytes(fp, (long_u)buf->b_u_line_lnum, 4);
968 put_bytes(fp, (long_u)buf->b_u_line_colnr, 4);
969
970 /* Undo structures header data */
971 put_header_ptr(fp, buf->b_u_oldhead);
972 put_header_ptr(fp, buf->b_u_newhead);
973 put_header_ptr(fp, buf->b_u_curhead);
974
975 put_bytes(fp, (long_u)buf->b_u_numhead, 4);
976 put_bytes(fp, (long_u)buf->b_u_seq_last, 4);
977 put_bytes(fp, (long_u)buf->b_u_seq_cur, 4);
Bram Moolenaara800b422010-06-27 01:15:55 +0200978 put_time(fp, buf->b_u_time_cur);
979
980 /* Optional fields. */
981 putc(4, fp);
982 putc(UF_LAST_SAVE_NR, fp);
Bram Moolenaar730cde92010-06-27 05:18:54 +0200983 put_bytes(fp, (long_u)buf->b_u_save_nr_last, 4);
Bram Moolenaara800b422010-06-27 01:15:55 +0200984
985 putc(0, fp); /* end marker */
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +0200986
987 return OK;
988}
989
990 static int
Bram Moolenaara3ff49f2010-05-30 22:48:02 +0200991serialize_uhp(fp, buf, uhp)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +0200992 FILE *fp;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +0200993 buf_T *buf;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +0200994 u_header_T *uhp;
995{
996 int i;
997 u_entry_T *uep;
998
999 if (put_bytes(fp, (long_u)UF_HEADER_MAGIC, 2) == FAIL)
1000 return FAIL;
1001
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001002 put_header_ptr(fp, uhp->uh_next.ptr);
1003 put_header_ptr(fp, uhp->uh_prev.ptr);
1004 put_header_ptr(fp, uhp->uh_alt_next.ptr);
1005 put_header_ptr(fp, uhp->uh_alt_prev.ptr);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001006 put_bytes(fp, uhp->uh_seq, 4);
1007 serialize_pos(uhp->uh_cursor, fp);
1008#ifdef FEAT_VIRTUALEDIT
1009 put_bytes(fp, (long_u)uhp->uh_cursor_vcol, 4);
1010#else
1011 put_bytes(fp, (long_u)0, 4);
1012#endif
1013 put_bytes(fp, (long_u)uhp->uh_flags, 2);
1014 /* Assume NMARKS will stay the same. */
1015 for (i = 0; i < NMARKS; ++i)
1016 serialize_pos(uhp->uh_namedm[i], fp);
1017#ifdef FEAT_VISUAL
1018 serialize_visualinfo(&uhp->uh_visual, fp);
1019#else
1020 {
1021 visualinfo_T info;
1022
1023 memset(&info, 0, sizeof(visualinfo_T));
1024 serialize_visualinfo(&info, fp);
1025 }
1026#endif
1027 put_time(fp, uhp->uh_time);
1028
Bram Moolenaara800b422010-06-27 01:15:55 +02001029 /* Optional fields. */
1030 putc(4, fp);
1031 putc(UHP_SAVE_NR, fp);
1032 put_bytes(fp, (long_u)uhp->uh_save_nr, 4);
1033
1034 putc(0, fp); /* end marker */
1035
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001036 /* Write all the entries. */
1037 for (uep = uhp->uh_entry; uep != NULL; uep = uep->ue_next)
1038 {
1039 put_bytes(fp, (long_u)UF_ENTRY_MAGIC, 2);
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001040 if (serialize_uep(fp, buf, uep) == FAIL)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001041 return FAIL;
1042 }
1043 put_bytes(fp, (long_u)UF_ENTRY_END_MAGIC, 2);
1044 return OK;
1045}
1046
1047 static u_header_T *
Bram Moolenaar69154f22010-07-18 21:42:34 +02001048unserialize_uhp(fp, file_name)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001049 FILE *fp;
1050 char_u *file_name;
1051{
1052 u_header_T *uhp;
1053 int i;
1054 u_entry_T *uep, *last_uep;
1055 int c;
1056 int error;
1057
1058 uhp = (u_header_T *)U_ALLOC_LINE(sizeof(u_header_T));
1059 if (uhp == NULL)
1060 return NULL;
1061 vim_memset(uhp, 0, sizeof(u_header_T));
1062#ifdef U_DEBUG
1063 uhp->uh_magic = UH_MAGIC;
1064#endif
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001065 uhp->uh_next.seq = get4c(fp);
1066 uhp->uh_prev.seq = get4c(fp);
1067 uhp->uh_alt_next.seq = get4c(fp);
1068 uhp->uh_alt_prev.seq = get4c(fp);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001069 uhp->uh_seq = get4c(fp);
1070 if (uhp->uh_seq <= 0)
1071 {
1072 corruption_error("uh_seq", file_name);
1073 vim_free(uhp);
1074 return NULL;
1075 }
1076 unserialize_pos(&uhp->uh_cursor, fp);
1077#ifdef FEAT_VIRTUALEDIT
1078 uhp->uh_cursor_vcol = get4c(fp);
1079#else
1080 (void)get4c(fp);
1081#endif
1082 uhp->uh_flags = get2c(fp);
1083 for (i = 0; i < NMARKS; ++i)
1084 unserialize_pos(&uhp->uh_namedm[i], fp);
1085#ifdef FEAT_VISUAL
1086 unserialize_visualinfo(&uhp->uh_visual, fp);
1087#else
1088 {
1089 visualinfo_T info;
1090 unserialize_visualinfo(&info, fp);
1091 }
1092#endif
1093 uhp->uh_time = get8ctime(fp);
1094
Bram Moolenaara800b422010-06-27 01:15:55 +02001095 /* Optional fields. */
Bram Moolenaar69154f22010-07-18 21:42:34 +02001096 for (;;)
1097 {
1098 int len = getc(fp);
1099 int what;
Bram Moolenaara800b422010-06-27 01:15:55 +02001100
Bram Moolenaar69154f22010-07-18 21:42:34 +02001101 if (len == 0)
1102 break;
1103 what = getc(fp);
1104 switch (what)
1105 {
1106 case UHP_SAVE_NR:
1107 uhp->uh_save_nr = get4c(fp);
Bram Moolenaara800b422010-06-27 01:15:55 +02001108 break;
Bram Moolenaar69154f22010-07-18 21:42:34 +02001109 default:
1110 /* field not supported, skip */
1111 while (--len >= 0)
1112 (void)getc(fp);
Bram Moolenaara800b422010-06-27 01:15:55 +02001113 }
Bram Moolenaar69154f22010-07-18 21:42:34 +02001114 }
Bram Moolenaara800b422010-06-27 01:15:55 +02001115
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001116 /* Unserialize the uep list. */
1117 last_uep = NULL;
1118 while ((c = get2c(fp)) == UF_ENTRY_MAGIC)
1119 {
1120 error = FALSE;
1121 uep = unserialize_uep(fp, &error, file_name);
1122 if (last_uep == NULL)
1123 uhp->uh_entry = uep;
1124 else
1125 last_uep->ue_next = uep;
1126 last_uep = uep;
1127 if (uep == NULL || error)
1128 {
1129 u_free_uhp(uhp);
1130 return NULL;
1131 }
1132 }
1133 if (c != UF_ENTRY_END_MAGIC)
1134 {
1135 corruption_error("entry end", file_name);
1136 u_free_uhp(uhp);
1137 return NULL;
1138 }
1139
1140 return uhp;
1141}
1142
Bram Moolenaar9db58062010-05-29 20:33:07 +02001143/*
1144 * Serialize "uep" to "fp".
1145 */
1146 static int
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001147serialize_uep(fp, buf, uep)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001148 FILE *fp;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001149 buf_T *buf;
1150 u_entry_T *uep;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001151{
1152 int i;
1153 size_t len;
1154
1155 put_bytes(fp, (long_u)uep->ue_top, 4);
1156 put_bytes(fp, (long_u)uep->ue_bot, 4);
1157 put_bytes(fp, (long_u)uep->ue_lcount, 4);
1158 put_bytes(fp, (long_u)uep->ue_size, 4);
1159 for (i = 0; i < uep->ue_size; ++i)
1160 {
1161 len = STRLEN(uep->ue_array[i]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001162 if (put_bytes(fp, (long_u)len, 4) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001163 return FAIL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001164 if (len > 0 && fwrite_crypt(buf, uep->ue_array[i], len, fp) != 1)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001165 return FAIL;
1166 }
1167 return OK;
1168}
1169
1170 static u_entry_T *
1171unserialize_uep(fp, error, file_name)
1172 FILE *fp;
1173 int *error;
1174 char_u *file_name;
1175{
1176 int i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001177 u_entry_T *uep;
1178 char_u **array;
1179 char_u *line;
1180 int line_len;
1181
1182 uep = (u_entry_T *)U_ALLOC_LINE(sizeof(u_entry_T));
1183 if (uep == NULL)
1184 return NULL;
1185 vim_memset(uep, 0, sizeof(u_entry_T));
1186#ifdef U_DEBUG
1187 uep->ue_magic = UE_MAGIC;
1188#endif
1189 uep->ue_top = get4c(fp);
1190 uep->ue_bot = get4c(fp);
1191 uep->ue_lcount = get4c(fp);
1192 uep->ue_size = get4c(fp);
1193 if (uep->ue_size > 0)
1194 {
1195 array = (char_u **)U_ALLOC_LINE(sizeof(char_u *) * uep->ue_size);
1196 if (array == NULL)
1197 {
1198 *error = TRUE;
1199 return uep;
1200 }
1201 vim_memset(array, 0, sizeof(char_u *) * uep->ue_size);
1202 }
Bram Moolenaar644fdff2010-05-30 13:26:21 +02001203 else
1204 array = NULL;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001205 uep->ue_array = array;
1206
1207 for (i = 0; i < uep->ue_size; ++i)
1208 {
1209 line_len = get4c(fp);
1210 if (line_len >= 0)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001211 line = read_string_decrypt(curbuf, fp, line_len);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001212 else
1213 {
1214 line = NULL;
1215 corruption_error("line length", file_name);
1216 }
1217 if (line == NULL)
1218 {
1219 *error = TRUE;
1220 return uep;
1221 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001222 array[i] = line;
1223 }
1224 return uep;
1225}
1226
1227/*
1228 * Serialize "pos" to "fp".
1229 */
1230 static void
1231serialize_pos(pos, fp)
1232 pos_T pos;
1233 FILE *fp;
1234{
1235 put_bytes(fp, (long_u)pos.lnum, 4);
1236 put_bytes(fp, (long_u)pos.col, 4);
1237#ifdef FEAT_VIRTUALEDIT
1238 put_bytes(fp, (long_u)pos.coladd, 4);
1239#else
1240 put_bytes(fp, (long_u)0, 4);
1241#endif
1242}
1243
1244/*
1245 * Unserialize the pos_T at the current position in fp.
1246 */
1247 static void
1248unserialize_pos(pos, fp)
1249 pos_T *pos;
1250 FILE *fp;
1251{
1252 pos->lnum = get4c(fp);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001253 if (pos->lnum < 0)
1254 pos->lnum = 0;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001255 pos->col = get4c(fp);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001256 if (pos->col < 0)
1257 pos->col = 0;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001258#ifdef FEAT_VIRTUALEDIT
1259 pos->coladd = get4c(fp);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001260 if (pos->coladd < 0)
1261 pos->coladd = 0;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001262#else
1263 (void)get4c(fp);
1264#endif
1265}
1266
1267/*
1268 * Serialize "info" to "fp".
1269 */
1270 static void
1271serialize_visualinfo(info, fp)
1272 visualinfo_T *info;
1273 FILE *fp;
1274{
1275 serialize_pos(info->vi_start, fp);
1276 serialize_pos(info->vi_end, fp);
1277 put_bytes(fp, (long_u)info->vi_mode, 4);
1278 put_bytes(fp, (long_u)info->vi_curswant, 4);
1279}
1280
1281/*
1282 * Unserialize the visualinfo_T at the current position in fp.
1283 */
1284 static void
1285unserialize_visualinfo(info, fp)
1286 visualinfo_T *info;
1287 FILE *fp;
1288{
1289 unserialize_pos(&info->vi_start, fp);
1290 unserialize_pos(&info->vi_end, fp);
1291 info->vi_mode = get4c(fp);
1292 info->vi_curswant = get4c(fp);
1293}
1294
1295/*
1296 * Write the pointer to an undo header. Instead of writing the pointer itself
1297 * we use the sequence number of the header. This is converted back to
1298 * pointers when reading. */
1299 static void
1300put_header_ptr(fp, uhp)
1301 FILE *fp;
1302 u_header_T *uhp;
1303{
1304 put_bytes(fp, (long_u)(uhp != NULL ? uhp->uh_seq : 0), 4);
1305}
1306
1307/*
1308 * Write the undo tree in an undo file.
1309 * When "name" is not NULL, use it as the name of the undo file.
1310 * Otherwise use buf->b_ffname to generate the undo file name.
1311 * "buf" must never be null, buf->b_ffname is used to obtain the original file
1312 * permissions.
1313 * "forceit" is TRUE for ":wundo!", FALSE otherwise.
1314 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text.
1315 */
1316 void
1317u_write_undo(name, forceit, buf, hash)
1318 char_u *name;
1319 int forceit;
1320 buf_T *buf;
1321 char_u *hash;
1322{
1323 u_header_T *uhp;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001324 char_u *file_name;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001325 int mark;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001326#ifdef U_DEBUG
1327 int headers_written = 0;
1328#endif
1329 int fd;
1330 FILE *fp = NULL;
1331 int perm;
1332 int write_ok = FALSE;
1333#ifdef UNIX
1334 int st_old_valid = FALSE;
1335 struct stat st_old;
1336 struct stat st_new;
1337#endif
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001338#ifdef FEAT_CRYPT
1339 int do_crypt = FALSE;
1340#endif
Bram Moolenaar9db58062010-05-29 20:33:07 +02001341
1342 if (name == NULL)
1343 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001344 file_name = u_get_undo_file_name(buf->b_ffname, FALSE);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001345 if (file_name == NULL)
1346 {
1347 if (p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001348 {
1349 verbose_enter();
1350 smsg((char_u *)
1351 _("Cannot write undo file in any directory in 'undodir'"));
1352 verbose_leave();
1353 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001354 return;
1355 }
1356 }
1357 else
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001358 file_name = name;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001359
1360 /*
1361 * Decide about the permission to use for the undo file. If the buffer
1362 * has a name use the permission of the original file. Otherwise only
1363 * allow the user to access the undo file.
1364 */
1365 perm = 0600;
1366 if (buf->b_ffname != NULL)
1367 {
1368#ifdef UNIX
1369 if (mch_stat((char *)buf->b_ffname, &st_old) >= 0)
1370 {
1371 perm = st_old.st_mode;
1372 st_old_valid = TRUE;
1373 }
1374#else
1375 perm = mch_getperm(buf->b_ffname);
1376 if (perm < 0)
1377 perm = 0600;
1378#endif
1379 }
1380
1381 /* strip any s-bit */
1382 perm = perm & 0777;
1383
1384 /* If the undo file already exists, verify that it actually is an undo
1385 * file, and delete it. */
1386 if (mch_getperm(file_name) >= 0)
1387 {
1388 if (name == NULL || !forceit)
1389 {
1390 /* Check we can read it and it's an undo file. */
1391 fd = mch_open((char *)file_name, O_RDONLY|O_EXTRA, 0);
1392 if (fd < 0)
1393 {
1394 if (name != NULL || p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001395 {
1396 if (name == NULL)
1397 verbose_enter();
1398 smsg((char_u *)
1399 _("Will not overwrite with undo file, cannot read: %s"),
Bram Moolenaar9db58062010-05-29 20:33:07 +02001400 file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001401 if (name == NULL)
1402 verbose_leave();
1403 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001404 goto theend;
1405 }
1406 else
1407 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02001408 char_u mbuf[UF_START_MAGIC_LEN];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001409 int len;
1410
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01001411 len = read_eintr(fd, mbuf, UF_START_MAGIC_LEN);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001412 close(fd);
1413 if (len < UF_START_MAGIC_LEN
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02001414 || memcmp(mbuf, UF_START_MAGIC, UF_START_MAGIC_LEN) != 0)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001415 {
1416 if (name != NULL || p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001417 {
1418 if (name == NULL)
1419 verbose_enter();
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001420 smsg((char_u *)
1421 _("Will not overwrite, this is not an undo file: %s"),
Bram Moolenaar9db58062010-05-29 20:33:07 +02001422 file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001423 if (name == NULL)
1424 verbose_leave();
1425 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001426 goto theend;
1427 }
1428 }
1429 }
1430 mch_remove(file_name);
1431 }
1432
Bram Moolenaar504a8212010-05-30 17:17:42 +02001433 /* If there is no undo information at all, quit here after deleting any
1434 * existing undo file. */
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001435 if (buf->b_u_numhead == 0 && buf->b_u_line_ptr == NULL)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001436 {
1437 if (p_verbose > 0)
Bram Moolenaar97ea5112010-06-12 06:46:44 +02001438 verb_msg((char_u *)_("Skipping undo file write, nothing to undo"));
Bram Moolenaar504a8212010-05-30 17:17:42 +02001439 goto theend;
1440 }
1441
Bram Moolenaar9db58062010-05-29 20:33:07 +02001442 fd = mch_open((char *)file_name,
1443 O_CREAT|O_EXTRA|O_WRONLY|O_EXCL|O_NOFOLLOW, perm);
1444 if (fd < 0)
1445 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001446 EMSG2(_(e_not_open), file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001447 goto theend;
1448 }
1449 (void)mch_setperm(file_name, perm);
1450 if (p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001451 {
1452 verbose_enter();
Bram Moolenaar9db58062010-05-29 20:33:07 +02001453 smsg((char_u *)_("Writing undo file: %s"), file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001454 verbose_leave();
1455 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001456
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001457#ifdef U_DEBUG
Bram Moolenaar504a8212010-05-30 17:17:42 +02001458 /* Check there is no problem in undo info before writing. */
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001459 u_check(FALSE);
1460#endif
1461
Bram Moolenaar9db58062010-05-29 20:33:07 +02001462#ifdef UNIX
1463 /*
1464 * Try to set the group of the undo file same as the original file. If
1465 * this fails, set the protection bits for the group same as the
1466 * protection bits for others.
1467 */
Bram Moolenaarce69e822010-07-21 20:31:07 +02001468 if (st_old_valid
1469 && mch_stat((char *)file_name, &st_new) >= 0
1470 && st_new.st_gid != st_old.st_gid
Bram Moolenaar9db58062010-05-29 20:33:07 +02001471# ifdef HAVE_FCHOWN /* sequent-ptx lacks fchown() */
Bram Moolenaarce69e822010-07-21 20:31:07 +02001472 && fchown(fd, (uid_t)-1, st_old.st_gid) != 0
Bram Moolenaar9db58062010-05-29 20:33:07 +02001473# endif
1474 )
1475 mch_setperm(file_name, (perm & 0707) | ((perm & 07) << 3));
1476# ifdef HAVE_SELINUX
1477 if (buf->b_ffname != NULL)
1478 mch_copy_sec(buf->b_ffname, file_name);
1479# endif
1480#endif
1481
1482 fp = fdopen(fd, "w");
1483 if (fp == NULL)
1484 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001485 EMSG2(_(e_not_open), file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001486 close(fd);
1487 mch_remove(file_name);
1488 goto theend;
1489 }
1490
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001491 /* Undo must be synced. */
1492 u_sync(TRUE);
1493
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001494 /*
1495 * Write the header.
1496 */
1497 if (serialize_header(fp, buf, hash) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001498 goto write_error;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001499#ifdef FEAT_CRYPT
Bram Moolenaarfa0ff9a2010-07-25 16:05:19 +02001500 if (*buf->b_p_key != NUL)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001501 do_crypt = TRUE;
1502#endif
Bram Moolenaar9db58062010-05-29 20:33:07 +02001503
1504 /*
1505 * Iteratively serialize UHPs and their UEPs from the top down.
1506 */
1507 mark = ++lastmark;
1508 uhp = buf->b_u_oldhead;
1509 while (uhp != NULL)
1510 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001511 /* Serialize current UHP if we haven't seen it */
1512 if (uhp->uh_walk != mark)
1513 {
1514 uhp->uh_walk = mark;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001515#ifdef U_DEBUG
1516 ++headers_written;
1517#endif
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001518 if (serialize_uhp(fp, buf, uhp) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001519 goto write_error;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001520 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001521
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001522 /* Now walk through the tree - algorithm from undo_time(). */
1523 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != mark)
1524 uhp = uhp->uh_prev.ptr;
1525 else if (uhp->uh_alt_next.ptr != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001526 && uhp->uh_alt_next.ptr->uh_walk != mark)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001527 uhp = uhp->uh_alt_next.ptr;
1528 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001529 && uhp->uh_next.ptr->uh_walk != mark)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001530 uhp = uhp->uh_next.ptr;
1531 else if (uhp->uh_alt_prev.ptr != NULL)
1532 uhp = uhp->uh_alt_prev.ptr;
1533 else
1534 uhp = uhp->uh_next.ptr;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001535 }
1536
1537 if (put_bytes(fp, (long_u)UF_HEADER_END_MAGIC, 2) == OK)
1538 write_ok = TRUE;
1539#ifdef U_DEBUG
1540 if (headers_written != buf->b_u_numhead)
Bram Moolenaar570064c2013-06-10 20:25:10 +02001541 {
1542 EMSGN("Written %ld headers, ...", headers_written);
1543 EMSGN("... but numhead is %ld", buf->b_u_numhead);
1544 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001545#endif
1546
1547write_error:
1548 fclose(fp);
1549 if (!write_ok)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001550 EMSG2(_("E829: write error in undo file: %s"), file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001551
1552#if defined(MACOS_CLASSIC) || defined(WIN3264)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001553 /* Copy file attributes; for systems where this can only be done after
1554 * closing the file. */
Bram Moolenaar9db58062010-05-29 20:33:07 +02001555 if (buf->b_ffname != NULL)
1556 (void)mch_copy_file_attribute(buf->b_ffname, file_name);
1557#endif
1558#ifdef HAVE_ACL
1559 if (buf->b_ffname != NULL)
1560 {
1561 vim_acl_T acl;
1562
1563 /* For systems that support ACL: get the ACL from the original file. */
1564 acl = mch_get_acl(buf->b_ffname);
1565 mch_set_acl(file_name, acl);
Bram Moolenaard2aed442012-06-01 13:46:12 +02001566 mch_free_acl(acl);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001567 }
1568#endif
1569
1570theend:
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001571#ifdef FEAT_CRYPT
1572 if (do_crypt)
1573 crypt_pop_state();
1574#endif
Bram Moolenaar9db58062010-05-29 20:33:07 +02001575 if (file_name != name)
1576 vim_free(file_name);
1577}
1578
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001579/*
1580 * Load the undo tree from an undo file.
1581 * If "name" is not NULL use it as the undo file name. This also means being
1582 * a bit more verbose.
1583 * Otherwise use curbuf->b_ffname to generate the undo file name.
1584 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text.
1585 */
1586 void
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001587u_read_undo(name, hash, orig_name)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001588 char_u *name;
1589 char_u *hash;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001590 char_u *orig_name;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001591{
1592 char_u *file_name;
1593 FILE *fp;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001594 long version, str_len;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001595 char_u *line_ptr = NULL;
1596 linenr_T line_lnum;
1597 colnr_T line_colnr;
1598 linenr_T line_count;
Bram Moolenaar442b4222010-05-24 21:34:22 +02001599 int num_head = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001600 long old_header_seq, new_header_seq, cur_header_seq;
1601 long seq_last, seq_cur;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001602 long last_save_nr = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001603 short old_idx = -1, new_idx = -1, cur_idx = -1;
1604 long num_read_uhps = 0;
1605 time_t seq_time;
1606 int i, j;
1607 int c;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001608 u_header_T *uhp;
1609 u_header_T **uhp_table = NULL;
1610 char_u read_hash[UNDO_HASH_SIZE];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001611 char_u magic_buf[UF_START_MAGIC_LEN];
1612#ifdef U_DEBUG
1613 int *uhp_table_used;
1614#endif
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001615#ifdef UNIX
1616 struct stat st_orig;
1617 struct stat st_undo;
1618#endif
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001619#ifdef FEAT_CRYPT
1620 int do_decrypt = FALSE;
1621#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001622
1623 if (name == NULL)
1624 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001625 file_name = u_get_undo_file_name(curbuf->b_ffname, TRUE);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001626 if (file_name == NULL)
1627 return;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001628
1629#ifdef UNIX
1630 /* For safety we only read an undo file if the owner is equal to the
Bram Moolenaar3b262392013-09-08 15:40:49 +02001631 * owner of the text file or equal to the current user. */
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001632 if (mch_stat((char *)orig_name, &st_orig) >= 0
1633 && mch_stat((char *)file_name, &st_undo) >= 0
Bram Moolenaar3b262392013-09-08 15:40:49 +02001634 && st_orig.st_uid != st_undo.st_uid
1635 && st_undo.st_uid != getuid())
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001636 {
1637 if (p_verbose > 0)
1638 {
1639 verbose_enter();
1640 smsg((char_u *)_("Not reading undo file, owner differs: %s"),
1641 file_name);
1642 verbose_leave();
1643 }
1644 return;
1645 }
1646#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001647 }
1648 else
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001649 file_name = name;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001650
1651 if (p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001652 {
1653 verbose_enter();
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001654 smsg((char_u *)_("Reading undo file: %s"), file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001655 verbose_leave();
1656 }
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001657
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001658 fp = mch_fopen((char *)file_name, "r");
1659 if (fp == NULL)
1660 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001661 if (name != NULL || p_verbose > 0)
1662 EMSG2(_("E822: Cannot open undo file for reading: %s"), file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001663 goto error;
1664 }
1665
Bram Moolenaar9db58062010-05-29 20:33:07 +02001666 /*
1667 * Read the undo file header.
1668 */
1669 if (fread(magic_buf, UF_START_MAGIC_LEN, 1, fp) != 1
1670 || memcmp(magic_buf, UF_START_MAGIC, UF_START_MAGIC_LEN) != 0)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001671 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001672 EMSG2(_("E823: Not an undo file: %s"), file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001673 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001674 }
1675 version = get2c(fp);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001676 if (version == UF_VERSION_CRYPT)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001677 {
1678#ifdef FEAT_CRYPT
Bram Moolenaar56be9502010-06-06 14:20:26 +02001679 if (*curbuf->b_p_key == NUL)
1680 {
1681 EMSG2(_("E832: Non-encrypted file has encrypted undo file: %s"),
1682 file_name);
1683 goto error;
1684 }
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001685 if (prepare_crypt_read(fp) == FAIL)
1686 {
1687 EMSG2(_("E826: Undo file decryption failed: %s"), file_name);
1688 goto error;
1689 }
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001690 do_decrypt = TRUE;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001691#else
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001692 EMSG2(_("E827: Undo file is encrypted: %s"), file_name);
1693 goto error;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001694#endif
1695 }
Bram Moolenaar69154f22010-07-18 21:42:34 +02001696 else if (version != UF_VERSION)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001697 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001698 EMSG2(_("E824: Incompatible undo file: %s"), file_name);
1699 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001700 }
1701
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001702 if (fread(read_hash, UNDO_HASH_SIZE, 1, fp) != 1)
1703 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001704 corruption_error("hash", file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001705 goto error;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001706 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001707 line_count = (linenr_T)get4c(fp);
1708 if (memcmp(hash, read_hash, UNDO_HASH_SIZE) != 0
1709 || line_count != curbuf->b_ml.ml_line_count)
1710 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001711 if (p_verbose > 0 || name != NULL)
1712 {
Bram Moolenaar504a8212010-05-30 17:17:42 +02001713 if (name == NULL)
1714 verbose_enter();
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001715 give_warning((char_u *)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001716 _("File contents changed, cannot use undo info"), TRUE);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001717 if (name == NULL)
1718 verbose_leave();
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001719 }
1720 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001721 }
1722
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001723 /* Read undo data for "U" command. */
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001724 str_len = get4c(fp);
1725 if (str_len < 0)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001726 goto error;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001727 if (str_len > 0)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001728 line_ptr = read_string_decrypt(curbuf, fp, str_len);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001729 line_lnum = (linenr_T)get4c(fp);
1730 line_colnr = (colnr_T)get4c(fp);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001731 if (line_lnum < 0 || line_colnr < 0)
1732 {
1733 corruption_error("line lnum/col", file_name);
1734 goto error;
1735 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001736
1737 /* Begin general undo data */
1738 old_header_seq = get4c(fp);
1739 new_header_seq = get4c(fp);
1740 cur_header_seq = get4c(fp);
1741 num_head = get4c(fp);
1742 seq_last = get4c(fp);
1743 seq_cur = get4c(fp);
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001744 seq_time = get8ctime(fp);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001745
Bram Moolenaar69154f22010-07-18 21:42:34 +02001746 /* Optional header fields. */
1747 for (;;)
1748 {
1749 int len = getc(fp);
1750 int what;
Bram Moolenaara800b422010-06-27 01:15:55 +02001751
Bram Moolenaar69154f22010-07-18 21:42:34 +02001752 if (len == 0 || len == EOF)
1753 break;
1754 what = getc(fp);
1755 switch (what)
1756 {
1757 case UF_LAST_SAVE_NR:
1758 last_save_nr = get4c(fp);
Bram Moolenaara800b422010-06-27 01:15:55 +02001759 break;
Bram Moolenaar69154f22010-07-18 21:42:34 +02001760 default:
1761 /* field not supported, skip */
1762 while (--len >= 0)
1763 (void)getc(fp);
Bram Moolenaara800b422010-06-27 01:15:55 +02001764 }
Bram Moolenaar69154f22010-07-18 21:42:34 +02001765 }
Bram Moolenaara800b422010-06-27 01:15:55 +02001766
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001767 /* uhp_table will store the freshly created undo headers we allocate
1768 * until we insert them into curbuf. The table remains sorted by the
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001769 * sequence numbers of the headers.
1770 * When there are no headers uhp_table is NULL. */
1771 if (num_head > 0)
1772 {
1773 uhp_table = (u_header_T **)U_ALLOC_LINE(
1774 num_head * sizeof(u_header_T *));
1775 if (uhp_table == NULL)
1776 goto error;
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001777 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001778
Bram Moolenaar9db58062010-05-29 20:33:07 +02001779 while ((c = get2c(fp)) == UF_HEADER_MAGIC)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001780 {
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001781 if (num_read_uhps >= num_head)
1782 {
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001783 corruption_error("num_head too small", file_name);
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001784 goto error;
1785 }
1786
Bram Moolenaar69154f22010-07-18 21:42:34 +02001787 uhp = unserialize_uhp(fp, file_name);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001788 if (uhp == NULL)
1789 goto error;
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001790 uhp_table[num_read_uhps++] = uhp;
1791 }
1792
1793 if (num_read_uhps != num_head)
1794 {
1795 corruption_error("num_head", file_name);
1796 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001797 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001798 if (c != UF_HEADER_END_MAGIC)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001799 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001800 corruption_error("end marker", file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001801 goto error;
1802 }
1803
Bram Moolenaar9db58062010-05-29 20:33:07 +02001804#ifdef U_DEBUG
1805 uhp_table_used = (int *)alloc_clear(
1806 (unsigned)(sizeof(int) * num_head + 1));
1807# define SET_FLAG(j) ++uhp_table_used[j]
1808#else
1809# define SET_FLAG(j)
1810#endif
1811
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001812 /* We have put all of the headers into a table. Now we iterate through the
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001813 * table and swizzle each sequence number we have stored in uh_*_seq into
1814 * a pointer corresponding to the header with that sequence number. */
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001815 for (i = 0; i < num_head; i++)
1816 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001817 uhp = uhp_table[i];
1818 if (uhp == NULL)
1819 continue;
1820 for (j = 0; j < num_head; j++)
1821 if (uhp_table[j] != NULL && i != j
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001822 && uhp_table[i]->uh_seq == uhp_table[j]->uh_seq)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001823 {
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001824 corruption_error("duplicate uh_seq", file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001825 goto error;
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001826 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001827 for (j = 0; j < num_head; j++)
1828 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001829 && uhp_table[j]->uh_seq == uhp->uh_next.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001830 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001831 uhp->uh_next.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001832 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001833 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001834 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001835 for (j = 0; j < num_head; j++)
1836 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001837 && uhp_table[j]->uh_seq == uhp->uh_prev.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001838 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001839 uhp->uh_prev.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001840 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001841 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001842 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001843 for (j = 0; j < num_head; j++)
1844 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001845 && uhp_table[j]->uh_seq == uhp->uh_alt_next.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001846 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001847 uhp->uh_alt_next.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001848 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001849 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001850 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001851 for (j = 0; j < num_head; j++)
1852 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001853 && uhp_table[j]->uh_seq == uhp->uh_alt_prev.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001854 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001855 uhp->uh_alt_prev.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001856 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001857 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001858 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001859 if (old_header_seq > 0 && old_idx < 0 && uhp->uh_seq == old_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001860 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001861 old_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001862 SET_FLAG(i);
1863 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001864 if (new_header_seq > 0 && new_idx < 0 && uhp->uh_seq == new_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001865 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001866 new_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001867 SET_FLAG(i);
1868 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001869 if (cur_header_seq > 0 && cur_idx < 0 && uhp->uh_seq == cur_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001870 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001871 cur_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001872 SET_FLAG(i);
1873 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001874 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001875
1876 /* Now that we have read the undo info successfully, free the current undo
1877 * info and use the info from the file. */
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001878 u_blockfree(curbuf);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001879 curbuf->b_u_oldhead = old_idx < 0 ? NULL : uhp_table[old_idx];
1880 curbuf->b_u_newhead = new_idx < 0 ? NULL : uhp_table[new_idx];
1881 curbuf->b_u_curhead = cur_idx < 0 ? NULL : uhp_table[cur_idx];
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001882 curbuf->b_u_line_ptr = line_ptr;
1883 curbuf->b_u_line_lnum = line_lnum;
1884 curbuf->b_u_line_colnr = line_colnr;
1885 curbuf->b_u_numhead = num_head;
1886 curbuf->b_u_seq_last = seq_last;
1887 curbuf->b_u_seq_cur = seq_cur;
Bram Moolenaara800b422010-06-27 01:15:55 +02001888 curbuf->b_u_time_cur = seq_time;
Bram Moolenaar730cde92010-06-27 05:18:54 +02001889 curbuf->b_u_save_nr_last = last_save_nr;
Bram Moolenaardba01a02010-11-03 19:32:42 +01001890 curbuf->b_u_save_nr_cur = last_save_nr;
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001891
1892 curbuf->b_u_synced = TRUE;
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001893 vim_free(uhp_table);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001894
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001895#ifdef U_DEBUG
Bram Moolenaar9db58062010-05-29 20:33:07 +02001896 for (i = 0; i < num_head; ++i)
1897 if (uhp_table_used[i] == 0)
1898 EMSGN("uhp_table entry %ld not used, leaking memory", i);
1899 vim_free(uhp_table_used);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001900 u_check(TRUE);
1901#endif
Bram Moolenaar9db58062010-05-29 20:33:07 +02001902
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001903 if (name != NULL)
1904 smsg((char_u *)_("Finished reading undo file %s"), file_name);
1905 goto theend;
1906
1907error:
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001908 vim_free(line_ptr);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001909 if (uhp_table != NULL)
1910 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001911 for (i = 0; i < num_read_uhps; i++)
1912 if (uhp_table[i] != NULL)
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001913 u_free_uhp(uhp_table[i]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001914 vim_free(uhp_table);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001915 }
1916
1917theend:
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001918#ifdef FEAT_CRYPT
1919 if (do_decrypt)
1920 crypt_pop_state();
1921#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001922 if (fp != NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001923 fclose(fp);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001924 if (file_name != name)
1925 vim_free(file_name);
1926 return;
1927}
1928
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001929#endif /* FEAT_PERSISTENT_UNDO */
1930
1931
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932/*
1933 * If 'cpoptions' contains 'u': Undo the previous undo or redo (vi compatible).
1934 * If 'cpoptions' does not contain 'u': Always undo.
1935 */
1936 void
1937u_undo(count)
1938 int count;
1939{
1940 /*
1941 * If we get an undo command while executing a macro, we behave like the
1942 * original vi. If this happens twice in one macro the result will not
1943 * be compatible.
1944 */
1945 if (curbuf->b_u_synced == FALSE)
1946 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001947 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 count = 1;
1949 }
1950
1951 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
1952 undo_undoes = TRUE;
1953 else
1954 undo_undoes = !undo_undoes;
1955 u_doit(count);
1956}
1957
1958/*
1959 * If 'cpoptions' contains 'u': Repeat the previous undo or redo.
1960 * If 'cpoptions' does not contain 'u': Always redo.
1961 */
1962 void
1963u_redo(count)
1964 int count;
1965{
1966 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
1967 undo_undoes = FALSE;
1968 u_doit(count);
1969}
1970
1971/*
1972 * Undo or redo, depending on 'undo_undoes', 'count' times.
1973 */
1974 static void
Bram Moolenaarca003e12006-03-17 23:19:38 +00001975u_doit(startcount)
1976 int startcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977{
Bram Moolenaarca003e12006-03-17 23:19:38 +00001978 int count = startcount;
1979
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001980 if (!undo_allowed())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982
1983 u_newcount = 0;
1984 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001985 if (curbuf->b_ml.ml_flags & ML_EMPTY)
1986 u_oldcount = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 while (count--)
1988 {
Bram Moolenaarb0b50882010-07-07 18:26:28 +02001989 /* Do the change warning now, so that it triggers FileChangedRO when
1990 * needed. This may cause the file to be reloaded, that must happen
1991 * before we do anything, because it may change curbuf->b_u_curhead
1992 * and more. */
1993 change_warning(0);
1994
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 if (undo_undoes)
1996 {
1997 if (curbuf->b_u_curhead == NULL) /* first undo */
1998 curbuf->b_u_curhead = curbuf->b_u_newhead;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001999 else if (get_undolevel() > 0) /* multi level undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 /* get next undo */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002001 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_next.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 /* nothing to undo */
2003 if (curbuf->b_u_numhead == 0 || curbuf->b_u_curhead == NULL)
2004 {
2005 /* stick curbuf->b_u_curhead at end */
2006 curbuf->b_u_curhead = curbuf->b_u_oldhead;
2007 beep_flush();
Bram Moolenaarca003e12006-03-17 23:19:38 +00002008 if (count == startcount - 1)
2009 {
2010 MSG(_("Already at oldest change"));
2011 return;
2012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 break;
2014 }
2015
Bram Moolenaarca003e12006-03-17 23:19:38 +00002016 u_undoredo(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 }
2018 else
2019 {
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002020 if (curbuf->b_u_curhead == NULL || get_undolevel() <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 {
2022 beep_flush(); /* nothing to redo */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002023 if (count == startcount - 1)
2024 {
2025 MSG(_("Already at newest change"));
2026 return;
2027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 break;
2029 }
2030
Bram Moolenaarca003e12006-03-17 23:19:38 +00002031 u_undoredo(FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002032
2033 /* Advance for next redo. Set "newhead" when at the end of the
2034 * redoable changes. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002035 if (curbuf->b_u_curhead->uh_prev.ptr == NULL)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002036 curbuf->b_u_newhead = curbuf->b_u_curhead;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002037 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_prev.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 }
2039 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00002040 u_undo_end(undo_undoes, FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002041}
2042
Bram Moolenaar1e607892006-03-13 22:15:53 +00002043/*
2044 * Undo or redo over the timeline.
2045 * When "step" is negative go back in time, otherwise goes forward in time.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002046 * When "sec" is FALSE make "step" steps, when "sec" is TRUE use "step" as
2047 * seconds.
Bram Moolenaar730cde92010-06-27 05:18:54 +02002048 * When "file" is TRUE use "step" as a number of file writes.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002049 * When "absolute" is TRUE use "step" as the sequence number to jump to.
2050 * "sec" must be FALSE then.
Bram Moolenaar1e607892006-03-13 22:15:53 +00002051 */
2052 void
Bram Moolenaar730cde92010-06-27 05:18:54 +02002053undo_time(step, sec, file, absolute)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002054 long step;
2055 int sec;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002056 int file;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002057 int absolute;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002058{
2059 long target;
2060 long closest;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002061 long closest_start;
2062 long closest_seq = 0;
2063 long val;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002064 u_header_T *uhp;
2065 u_header_T *last;
2066 int mark;
2067 int nomark;
2068 int round;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002069 int dosec = sec;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002070 int dofile = file;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002071 int above = FALSE;
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002072 int did_undo = TRUE;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002073
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002074 /* First make sure the current undoable change is synced. */
2075 if (curbuf->b_u_synced == FALSE)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002076 u_sync(TRUE);
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002077
Bram Moolenaar1e607892006-03-13 22:15:53 +00002078 u_newcount = 0;
2079 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002080 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002081 u_oldcount = -1;
2082
Bram Moolenaarca003e12006-03-17 23:19:38 +00002083 /* "target" is the node below which we want to be.
2084 * Init "closest" to a value we can't reach. */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002085 if (absolute)
2086 {
2087 target = step;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002088 closest = -1;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002089 }
Bram Moolenaarca003e12006-03-17 23:19:38 +00002090 else
Bram Moolenaar1e607892006-03-13 22:15:53 +00002091 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002092 /* When doing computations with time_t subtract starttime, because
2093 * time_t converted to a long may result in a wrong number. */
Bram Moolenaar730cde92010-06-27 05:18:54 +02002094 if (dosec)
Bram Moolenaara800b422010-06-27 01:15:55 +02002095 target = (long)(curbuf->b_u_time_cur - starttime) + step;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002096 else if (dofile)
2097 {
2098 if (step < 0)
2099 {
2100 /* Going back to a previous write. If there were changes after
2101 * the last write, count that as moving one file-write, so
2102 * that ":earlier 1f" undoes all changes since the last save. */
2103 uhp = curbuf->b_u_curhead;
2104 if (uhp != NULL)
2105 uhp = uhp->uh_next.ptr;
2106 else
2107 uhp = curbuf->b_u_newhead;
2108 if (uhp != NULL && uhp->uh_save_nr != 0)
2109 /* "uh_save_nr" was set in the last block, that means
2110 * there were no changes since the last write */
2111 target = curbuf->b_u_save_nr_cur + step;
2112 else
2113 /* count the changes since the last write as one step */
2114 target = curbuf->b_u_save_nr_cur + step + 1;
2115 if (target <= 0)
2116 /* Go to before first write: before the oldest change. Use
2117 * the sequence number for that. */
2118 dofile = FALSE;
2119 }
2120 else
2121 {
2122 /* Moving forward to a newer write. */
2123 target = curbuf->b_u_save_nr_cur + step;
2124 if (target > curbuf->b_u_save_nr_last)
2125 {
2126 /* Go to after last write: after the latest change. Use
2127 * the sequence number for that. */
2128 target = curbuf->b_u_seq_last + 1;
2129 dofile = FALSE;
2130 }
2131 }
2132 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002133 else
2134 target = curbuf->b_u_seq_cur + step;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002135 if (step < 0)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002136 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002137 if (target < 0)
2138 target = 0;
2139 closest = -1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002140 }
2141 else
2142 {
Bram Moolenaar730cde92010-06-27 05:18:54 +02002143 if (dosec)
Bram Moolenaardb552d602006-03-23 22:59:57 +00002144 closest = (long)(time(NULL) - starttime + 1);
Bram Moolenaar730cde92010-06-27 05:18:54 +02002145 else if (dofile)
2146 closest = curbuf->b_u_save_nr_last + 2;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002147 else
2148 closest = curbuf->b_u_seq_last + 2;
2149 if (target >= closest)
2150 target = closest - 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002151 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002152 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002153 closest_start = closest;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002154 closest_seq = curbuf->b_u_seq_cur;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002155
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002156 /*
2157 * May do this twice:
Bram Moolenaar1e607892006-03-13 22:15:53 +00002158 * 1. Search for "target", update "closest" to the best match found.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002159 * 2. If "target" not found search for "closest".
2160 *
2161 * When using the closest time we use the sequence number in the second
2162 * round, because there may be several entries with the same time.
2163 */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002164 for (round = 1; round <= 2; ++round)
2165 {
2166 /* Find the path from the current state to where we want to go. The
2167 * desired state can be anywhere in the undo tree, need to go all over
2168 * it. We put "nomark" in uh_walk where we have been without success,
2169 * "mark" where it could possibly be. */
2170 mark = ++lastmark;
2171 nomark = ++lastmark;
2172
2173 if (curbuf->b_u_curhead == NULL) /* at leaf of the tree */
2174 uhp = curbuf->b_u_newhead;
2175 else
2176 uhp = curbuf->b_u_curhead;
2177
2178 while (uhp != NULL)
2179 {
2180 uhp->uh_walk = mark;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002181 if (dosec)
2182 val = (long)(uhp->uh_time - starttime);
2183 else if (dofile)
2184 val = uhp->uh_save_nr;
2185 else
2186 val = uhp->uh_seq;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002187
Bram Moolenaar730cde92010-06-27 05:18:54 +02002188 if (round == 1 && !(dofile && val == 0))
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002189 {
2190 /* Remember the header that is closest to the target.
2191 * It must be at least in the right direction (checked with
Bram Moolenaarca003e12006-03-17 23:19:38 +00002192 * "b_u_seq_cur"). When the timestamp is equal find the
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002193 * highest/lowest sequence number. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002194 if ((step < 0 ? uhp->uh_seq <= curbuf->b_u_seq_cur
2195 : uhp->uh_seq > curbuf->b_u_seq_cur)
2196 && ((dosec && val == closest)
2197 ? (step < 0
2198 ? uhp->uh_seq < closest_seq
2199 : uhp->uh_seq > closest_seq)
2200 : closest == closest_start
2201 || (val > target
2202 ? (closest > target
2203 ? val - target <= closest - target
2204 : val - target <= target - closest)
2205 : (closest > target
2206 ? target - val <= closest - target
2207 : target - val <= target - closest))))
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002208 {
2209 closest = val;
2210 closest_seq = uhp->uh_seq;
2211 }
2212 }
2213
2214 /* Quit searching when we found a match. But when searching for a
2215 * time we need to continue looking for the best uh_seq. */
2216 if (target == val && !dosec)
Bram Moolenaar730cde92010-06-27 05:18:54 +02002217 {
2218 target = uhp->uh_seq;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002219 break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002220 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002221
2222 /* go down in the tree if we haven't been there */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002223 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != nomark
2224 && uhp->uh_prev.ptr->uh_walk != mark)
2225 uhp = uhp->uh_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002226
2227 /* go to alternate branch if we haven't been there */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002228 else if (uhp->uh_alt_next.ptr != NULL
2229 && uhp->uh_alt_next.ptr->uh_walk != nomark
2230 && uhp->uh_alt_next.ptr->uh_walk != mark)
2231 uhp = uhp->uh_alt_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002232
2233 /* go up in the tree if we haven't been there and we are at the
2234 * start of alternate branches */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002235 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
2236 && uhp->uh_next.ptr->uh_walk != nomark
2237 && uhp->uh_next.ptr->uh_walk != mark)
Bram Moolenaardb552d602006-03-23 22:59:57 +00002238 {
2239 /* If still at the start we don't go through this change. */
2240 if (uhp == curbuf->b_u_curhead)
2241 uhp->uh_walk = nomark;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002242 uhp = uhp->uh_next.ptr;
Bram Moolenaardb552d602006-03-23 22:59:57 +00002243 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002244
2245 else
2246 {
2247 /* need to backtrack; mark this node as useless */
2248 uhp->uh_walk = nomark;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002249 if (uhp->uh_alt_prev.ptr != NULL)
2250 uhp = uhp->uh_alt_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002251 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002252 uhp = uhp->uh_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002253 }
2254 }
2255
2256 if (uhp != NULL) /* found it */
2257 break;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002258
2259 if (absolute)
2260 {
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002261 EMSGN(_("E830: Undo number %ld not found"), step);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002262 return;
2263 }
2264
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002265 if (closest == closest_start)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002266 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002267 if (step < 0)
2268 MSG(_("Already at oldest change"));
2269 else
2270 MSG(_("Already at newest change"));
Bram Moolenaar1e607892006-03-13 22:15:53 +00002271 return;
2272 }
2273
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002274 target = closest_seq;
2275 dosec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002276 dofile = FALSE;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002277 if (step < 0)
2278 above = TRUE; /* stop above the header */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002279 }
2280
2281 /* If we found it: Follow the path to go to where we want to be. */
2282 if (uhp != NULL)
2283 {
2284 /*
2285 * First go up the tree as much as needed.
2286 */
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002287 while (!got_int)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002288 {
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002289 /* Do the change warning now, for the same reason as above. */
2290 change_warning(0);
2291
Bram Moolenaar1e607892006-03-13 22:15:53 +00002292 uhp = curbuf->b_u_curhead;
2293 if (uhp == NULL)
2294 uhp = curbuf->b_u_newhead;
2295 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002296 uhp = uhp->uh_next.ptr;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002297 if (uhp == NULL || uhp->uh_walk != mark
2298 || (uhp->uh_seq == target && !above))
Bram Moolenaar1e607892006-03-13 22:15:53 +00002299 break;
2300 curbuf->b_u_curhead = uhp;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002301 u_undoredo(TRUE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002302 uhp->uh_walk = nomark; /* don't go back down here */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002303 }
2304
2305 /*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002306 * And now go down the tree (redo), branching off where needed.
Bram Moolenaar1e607892006-03-13 22:15:53 +00002307 */
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002308 while (!got_int)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002309 {
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002310 /* Do the change warning now, for the same reason as above. */
2311 change_warning(0);
2312
2313 uhp = curbuf->b_u_curhead;
2314 if (uhp == NULL)
2315 break;
2316
Bram Moolenaar89ed3df2007-01-09 19:23:12 +00002317 /* Go back to the first branch with a mark. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002318 while (uhp->uh_alt_prev.ptr != NULL
2319 && uhp->uh_alt_prev.ptr->uh_walk == mark)
2320 uhp = uhp->uh_alt_prev.ptr;
Bram Moolenaar89ed3df2007-01-09 19:23:12 +00002321
Bram Moolenaar1e607892006-03-13 22:15:53 +00002322 /* Find the last branch with a mark, that's the one. */
2323 last = uhp;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002324 while (last->uh_alt_next.ptr != NULL
2325 && last->uh_alt_next.ptr->uh_walk == mark)
2326 last = last->uh_alt_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002327 if (last != uhp)
2328 {
2329 /* Make the used branch the first entry in the list of
2330 * alternatives to make "u" and CTRL-R take this branch. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002331 while (uhp->uh_alt_prev.ptr != NULL)
2332 uhp = uhp->uh_alt_prev.ptr;
2333 if (last->uh_alt_next.ptr != NULL)
2334 last->uh_alt_next.ptr->uh_alt_prev.ptr =
2335 last->uh_alt_prev.ptr;
2336 last->uh_alt_prev.ptr->uh_alt_next.ptr = last->uh_alt_next.ptr;
2337 last->uh_alt_prev.ptr = NULL;
2338 last->uh_alt_next.ptr = uhp;
2339 uhp->uh_alt_prev.ptr = last;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002340
Bram Moolenaar8f1f6292010-05-30 16:55:22 +02002341 if (curbuf->b_u_oldhead == uhp)
2342 curbuf->b_u_oldhead = last;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002343 uhp = last;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002344 if (uhp->uh_next.ptr != NULL)
2345 uhp->uh_next.ptr->uh_prev.ptr = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002346 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002347 curbuf->b_u_curhead = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002348
2349 if (uhp->uh_walk != mark)
2350 break; /* must have reached the target */
2351
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002352 /* Stop when going backwards in time and didn't find the exact
2353 * header we were looking for. */
2354 if (uhp->uh_seq == target && above)
Bram Moolenaardb552d602006-03-23 22:59:57 +00002355 {
2356 curbuf->b_u_seq_cur = target - 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002357 break;
Bram Moolenaardb552d602006-03-23 22:59:57 +00002358 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002359
Bram Moolenaarca003e12006-03-17 23:19:38 +00002360 u_undoredo(FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002361
2362 /* Advance "curhead" to below the header we last used. If it
2363 * becomes NULL then we need to set "newhead" to this leaf. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002364 if (uhp->uh_prev.ptr == NULL)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002365 curbuf->b_u_newhead = uhp;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002366 curbuf->b_u_curhead = uhp->uh_prev.ptr;
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002367 did_undo = FALSE;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002368
2369 if (uhp->uh_seq == target) /* found it! */
2370 break;
2371
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002372 uhp = uhp->uh_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002373 if (uhp == NULL || uhp->uh_walk != mark)
2374 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002375 /* Need to redo more but can't find it... */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002376 EMSG2(_(e_intern2), "undo_time()");
2377 break;
2378 }
2379 }
2380 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00002381 u_undo_end(did_undo, absolute);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382}
2383
2384/*
2385 * u_undoredo: common code for undo and redo
2386 *
2387 * The lines in the file are replaced by the lines in the entry list at
2388 * curbuf->b_u_curhead. The replaced lines in the file are saved in the entry
2389 * list for the next undo/redo.
Bram Moolenaarca003e12006-03-17 23:19:38 +00002390 *
2391 * When "undo" is TRUE we go up in the tree, when FALSE we go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 */
2393 static void
Bram Moolenaarca003e12006-03-17 23:19:38 +00002394u_undoredo(undo)
2395 int undo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396{
2397 char_u **newarray = NULL;
2398 linenr_T oldsize;
2399 linenr_T newsize;
2400 linenr_T top, bot;
2401 linenr_T lnum;
2402 linenr_T newlnum = MAXLNUM;
2403 long i;
2404 u_entry_T *uep, *nuep;
2405 u_entry_T *newlist = NULL;
2406 int old_flags;
2407 int new_flags;
2408 pos_T namedm[NMARKS];
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002409#ifdef FEAT_VISUAL
2410 visualinfo_T visualinfo;
2411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 int empty_buffer; /* buffer became empty */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002413 u_header_T *curhead = curbuf->b_u_curhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002415#ifdef FEAT_AUTOCMD
2416 /* Don't want autocommands using the undo structures here, they are
2417 * invalid till the end. */
2418 block_autocmds();
2419#endif
2420
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002421#ifdef U_DEBUG
2422 u_check(FALSE);
2423#endif
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002424 old_flags = curhead->uh_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 new_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
2426 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
2427 setpcmark();
2428
2429 /*
2430 * save marks before undo/redo
2431 */
2432 mch_memmove(namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002433#ifdef FEAT_VISUAL
2434 visualinfo = curbuf->b_visual;
2435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 curbuf->b_op_start.lnum = curbuf->b_ml.ml_line_count;
2437 curbuf->b_op_start.col = 0;
2438 curbuf->b_op_end.lnum = 0;
2439 curbuf->b_op_end.col = 0;
2440
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002441 for (uep = curhead->uh_entry; uep != NULL; uep = nuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 {
2443 top = uep->ue_top;
2444 bot = uep->ue_bot;
2445 if (bot == 0)
2446 bot = curbuf->b_ml.ml_line_count + 1;
2447 if (top > curbuf->b_ml.ml_line_count || top >= bot
2448 || bot > curbuf->b_ml.ml_line_count + 1)
2449 {
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002450#ifdef FEAT_AUTOCMD
2451 unblock_autocmds();
2452#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 EMSG(_("E438: u_undo: line numbers wrong"));
2454 changed(); /* don't want UNCHANGED now */
2455 return;
2456 }
2457
2458 oldsize = bot - top - 1; /* number of lines before undo */
2459 newsize = uep->ue_size; /* number of lines after undo */
2460
2461 if (top < newlnum)
2462 {
2463 /* If the saved cursor is somewhere in this undo block, move it to
2464 * the remembered position. Makes "gwap" put the cursor back
2465 * where it was. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002466 lnum = curhead->uh_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 if (lnum >= top && lnum <= top + newsize + 1)
2468 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002469 curwin->w_cursor = curhead->uh_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 newlnum = curwin->w_cursor.lnum - 1;
2471 }
2472 else
2473 {
2474 /* Use the first line that actually changed. Avoids that
2475 * undoing auto-formatting puts the cursor in the previous
2476 * line. */
2477 for (i = 0; i < newsize && i < oldsize; ++i)
2478 if (STRCMP(uep->ue_array[i], ml_get(top + 1 + i)) != 0)
2479 break;
2480 if (i == newsize && newlnum == MAXLNUM && uep->ue_next == NULL)
2481 {
2482 newlnum = top;
2483 curwin->w_cursor.lnum = newlnum + 1;
2484 }
2485 else if (i < newsize)
2486 {
2487 newlnum = top + i;
2488 curwin->w_cursor.lnum = newlnum + 1;
2489 }
2490 }
2491 }
2492
2493 empty_buffer = FALSE;
2494
2495 /* delete the lines between top and bot and save them in newarray */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002496 if (oldsize > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002498 if ((newarray = (char_u **)U_ALLOC_LINE(
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002499 sizeof(char_u *) * oldsize)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 {
2501 do_outofmem_msg((long_u)(sizeof(char_u *) * oldsize));
2502 /*
2503 * We have messed up the entry list, repair is impossible.
2504 * we have to free the rest of the list.
2505 */
2506 while (uep != NULL)
2507 {
2508 nuep = uep->ue_next;
2509 u_freeentry(uep, uep->ue_size);
2510 uep = nuep;
2511 }
2512 break;
2513 }
2514 /* delete backwards, it goes faster in most cases */
2515 for (lnum = bot - 1, i = oldsize; --i >= 0; --lnum)
2516 {
2517 /* what can we do when we run out of memory? */
2518 if ((newarray[i] = u_save_line(lnum)) == NULL)
2519 do_outofmem_msg((long_u)0);
2520 /* remember we deleted the last line in the buffer, and a
2521 * dummy empty line will be inserted */
2522 if (curbuf->b_ml.ml_line_count == 1)
2523 empty_buffer = TRUE;
2524 ml_delete(lnum, FALSE);
2525 }
2526 }
Bram Moolenaar8d343302005-07-12 22:46:17 +00002527 else
2528 newarray = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529
2530 /* insert the lines in u_array between top and bot */
2531 if (newsize)
2532 {
2533 for (lnum = top, i = 0; i < newsize; ++i, ++lnum)
2534 {
2535 /*
2536 * If the file is empty, there is an empty line 1 that we
2537 * should get rid of, by replacing it with the new line
2538 */
2539 if (empty_buffer && lnum == 0)
2540 ml_replace((linenr_T)1, uep->ue_array[i], TRUE);
2541 else
2542 ml_append(lnum, uep->ue_array[i], (colnr_T)0, FALSE);
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002543 vim_free(uep->ue_array[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 }
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002545 vim_free((char_u *)uep->ue_array);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 }
2547
2548 /* adjust marks */
2549 if (oldsize != newsize)
2550 {
2551 mark_adjust(top + 1, top + oldsize, (long)MAXLNUM,
2552 (long)newsize - (long)oldsize);
2553 if (curbuf->b_op_start.lnum > top + oldsize)
2554 curbuf->b_op_start.lnum += newsize - oldsize;
2555 if (curbuf->b_op_end.lnum > top + oldsize)
2556 curbuf->b_op_end.lnum += newsize - oldsize;
2557 }
2558
2559 changed_lines(top + 1, 0, bot, newsize - oldsize);
2560
2561 /* set '[ and '] mark */
2562 if (top + 1 < curbuf->b_op_start.lnum)
2563 curbuf->b_op_start.lnum = top + 1;
2564 if (newsize == 0 && top + 1 > curbuf->b_op_end.lnum)
2565 curbuf->b_op_end.lnum = top + 1;
2566 else if (top + newsize > curbuf->b_op_end.lnum)
2567 curbuf->b_op_end.lnum = top + newsize;
2568
2569 u_newcount += newsize;
2570 u_oldcount += oldsize;
2571 uep->ue_size = oldsize;
2572 uep->ue_array = newarray;
2573 uep->ue_bot = top + newsize + 1;
2574
2575 /*
2576 * insert this entry in front of the new entry list
2577 */
2578 nuep = uep->ue_next;
2579 uep->ue_next = newlist;
2580 newlist = uep;
2581 }
2582
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002583 curhead->uh_entry = newlist;
2584 curhead->uh_flags = new_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 if ((old_flags & UH_EMPTYBUF) && bufempty())
2586 curbuf->b_ml.ml_flags |= ML_EMPTY;
2587 if (old_flags & UH_CHANGED)
2588 changed();
2589 else
Bram Moolenaar009b2592004-10-24 19:18:58 +00002590#ifdef FEAT_NETBEANS_INTG
2591 /* per netbeans undo rules, keep it as modified */
2592 if (!isNetbeansModified(curbuf))
2593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 unchanged(curbuf, FALSE);
2595
2596 /*
2597 * restore marks from before undo/redo
2598 */
2599 for (i = 0; i < NMARKS; ++i)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002600 if (curhead->uh_namedm[i].lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002602 curbuf->b_namedm[i] = curhead->uh_namedm[i];
2603 curhead->uh_namedm[i] = namedm[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002605#ifdef FEAT_VISUAL
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002606 if (curhead->uh_visual.vi_start.lnum != 0)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002607 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002608 curbuf->b_visual = curhead->uh_visual;
2609 curhead->uh_visual = visualinfo;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002610 }
2611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612
2613 /*
2614 * If the cursor is only off by one line, put it at the same position as
2615 * before starting the change (for the "o" command).
2616 * Otherwise the cursor should go to the first undone line.
2617 */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002618 if (curhead->uh_cursor.lnum + 1 == curwin->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 && curwin->w_cursor.lnum > 1)
2620 --curwin->w_cursor.lnum;
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002621 if (curwin->w_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 {
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002623 if (curhead->uh_cursor.lnum == curwin->w_cursor.lnum)
2624 {
2625 curwin->w_cursor.col = curhead->uh_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002627 if (virtual_active() && curhead->uh_cursor_vcol >= 0)
2628 coladvance((colnr_T)curhead->uh_cursor_vcol);
2629 else
2630 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631#endif
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002632 }
2633 else
2634 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 else
2637 {
2638 /* We get here with the current cursor line being past the end (eg
2639 * after adding lines at the end of the file, and then undoing it).
2640 * check_cursor() will move the cursor to the last line. Move it to
2641 * the first column here. */
2642 curwin->w_cursor.col = 0;
2643#ifdef FEAT_VIRTUALEDIT
2644 curwin->w_cursor.coladd = 0;
2645#endif
2646 }
2647
2648 /* Make sure the cursor is on an existing line and column. */
2649 check_cursor();
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002650
2651 /* Remember where we are for "g-" and ":earlier 10s". */
2652 curbuf->b_u_seq_cur = curhead->uh_seq;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002653 if (undo)
2654 /* We are below the previous undo. However, to make ":earlier 1s"
2655 * work we compute this as being just above the just undone change. */
2656 --curbuf->b_u_seq_cur;
2657
Bram Moolenaar730cde92010-06-27 05:18:54 +02002658 /* Remember where we are for ":earlier 1f" and ":later 1f". */
2659 if (curhead->uh_save_nr != 0)
2660 {
2661 if (undo)
2662 curbuf->b_u_save_nr_cur = curhead->uh_save_nr - 1;
2663 else
2664 curbuf->b_u_save_nr_cur = curhead->uh_save_nr;
2665 }
2666
Bram Moolenaarca003e12006-03-17 23:19:38 +00002667 /* The timestamp can be the same for multiple changes, just use the one of
2668 * the undone/redone change. */
Bram Moolenaara800b422010-06-27 01:15:55 +02002669 curbuf->b_u_time_cur = curhead->uh_time;
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002670
2671#ifdef FEAT_AUTOCMD
2672 unblock_autocmds();
2673#endif
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002674#ifdef U_DEBUG
2675 u_check(FALSE);
2676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677}
2678
2679/*
2680 * If we deleted or added lines, report the number of less/more lines.
2681 * Otherwise, report the number of changes (this may be incorrect
2682 * in some cases, but it's better than nothing).
2683 */
2684 static void
Bram Moolenaardb552d602006-03-23 22:59:57 +00002685u_undo_end(did_undo, absolute)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002686 int did_undo; /* just did an undo */
Bram Moolenaardb552d602006-03-23 22:59:57 +00002687 int absolute; /* used ":undo N" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688{
Bram Moolenaar89d40322006-08-29 15:30:07 +00002689 char *msgstr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002690 u_header_T *uhp;
2691 char_u msgbuf[80];
Bram Moolenaar1e607892006-03-13 22:15:53 +00002692
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693#ifdef FEAT_FOLDING
2694 if ((fdo_flags & FDO_UNDO) && KeyTyped)
2695 foldOpenCursor();
2696#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +00002697
2698 if (global_busy /* no messages now, wait until global is finished */
2699 || !messaging()) /* 'lazyredraw' set, don't do messages now */
2700 return;
2701
2702 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2703 --u_newcount;
2704
2705 u_oldcount -= u_newcount;
2706 if (u_oldcount == -1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002707 msgstr = N_("more line");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002708 else if (u_oldcount < 0)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002709 msgstr = N_("more lines");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002710 else if (u_oldcount == 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002711 msgstr = N_("line less");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002712 else if (u_oldcount > 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002713 msgstr = N_("fewer lines");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002714 else
2715 {
2716 u_oldcount = u_newcount;
2717 if (u_newcount == 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002718 msgstr = N_("change");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002719 else
Bram Moolenaar89d40322006-08-29 15:30:07 +00002720 msgstr = N_("changes");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002721 }
2722
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002723 if (curbuf->b_u_curhead != NULL)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002724 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00002725 /* For ":undo N" we prefer a "after #N" message. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002726 if (absolute && curbuf->b_u_curhead->uh_next.ptr != NULL)
Bram Moolenaardb552d602006-03-23 22:59:57 +00002727 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002728 uhp = curbuf->b_u_curhead->uh_next.ptr;
Bram Moolenaardb552d602006-03-23 22:59:57 +00002729 did_undo = FALSE;
2730 }
2731 else if (did_undo)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002732 uhp = curbuf->b_u_curhead;
2733 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002734 uhp = curbuf->b_u_curhead->uh_next.ptr;
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002735 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002736 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002737 uhp = curbuf->b_u_newhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002738
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002739 if (uhp == NULL)
2740 *msgbuf = NUL;
2741 else
2742 u_add_time(msgbuf, sizeof(msgbuf), uhp->uh_time);
2743
Bram Moolenaarb2c03502010-07-02 20:20:09 +02002744#ifdef FEAT_CONCEAL
2745 {
2746 win_T *wp;
2747
2748 FOR_ALL_WINDOWS(wp)
2749 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02002750 if (wp->w_buffer == curbuf && wp->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02002751 redraw_win_later(wp, NOT_VALID);
2752 }
2753 }
2754#endif
2755
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002756 smsg((char_u *)_("%ld %s; %s #%ld %s"),
Bram Moolenaarca003e12006-03-17 23:19:38 +00002757 u_oldcount < 0 ? -u_oldcount : u_oldcount,
Bram Moolenaar89d40322006-08-29 15:30:07 +00002758 _(msgstr),
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002759 did_undo ? _("before") : _("after"),
2760 uhp == NULL ? 0L : uhp->uh_seq,
2761 msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762}
2763
2764/*
2765 * u_sync: stop adding to the current entry list
2766 */
2767 void
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002768u_sync(force)
2769 int force; /* Also sync when no_u_sync is set. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770{
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002771 /* Skip it when already synced or syncing is disabled. */
2772 if (curbuf->b_u_synced || (!force && no_u_sync > 0))
2773 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2775 if (im_is_preediting())
2776 return; /* XIM is busy, don't break an undo sequence */
2777#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002778 if (get_undolevel() < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 curbuf->b_u_synced = TRUE; /* no entries, nothing to do */
2780 else
2781 {
2782 u_getbot(); /* compute ue_bot of previous u_save */
2783 curbuf->b_u_curhead = NULL;
2784 }
2785}
2786
2787/*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002788 * ":undolist": List the leafs of the undo tree
2789 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002790 void
2791ex_undolist(eap)
Bram Moolenaarfff2bee2010-05-15 13:56:02 +02002792 exarg_T *eap UNUSED;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002793{
2794 garray_T ga;
2795 u_header_T *uhp;
2796 int mark;
2797 int nomark;
2798 int changes = 1;
2799 int i;
2800
2801 /*
2802 * 1: walk the tree to find all leafs, put the info in "ga".
2803 * 2: sort the lines
2804 * 3: display the list
2805 */
2806 mark = ++lastmark;
2807 nomark = ++lastmark;
2808 ga_init2(&ga, (int)sizeof(char *), 20);
2809
2810 uhp = curbuf->b_u_oldhead;
2811 while (uhp != NULL)
2812 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002813 if (uhp->uh_prev.ptr == NULL && uhp->uh_walk != nomark
Bram Moolenaarca003e12006-03-17 23:19:38 +00002814 && uhp->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002815 {
2816 if (ga_grow(&ga, 1) == FAIL)
2817 break;
2818 vim_snprintf((char *)IObuff, IOSIZE, "%6ld %7ld ",
2819 uhp->uh_seq, changes);
2820 u_add_time(IObuff + STRLEN(IObuff), IOSIZE - STRLEN(IObuff),
2821 uhp->uh_time);
Bram Moolenaara800b422010-06-27 01:15:55 +02002822 if (uhp->uh_save_nr > 0)
2823 {
Bram Moolenaardba01a02010-11-03 19:32:42 +01002824 while (STRLEN(IObuff) < 33)
Bram Moolenaara800b422010-06-27 01:15:55 +02002825 STRCAT(IObuff, " ");
2826 vim_snprintf_add((char *)IObuff, IOSIZE,
2827 " %3ld", uhp->uh_save_nr);
2828 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002829 ((char_u **)(ga.ga_data))[ga.ga_len++] = vim_strsave(IObuff);
2830 }
2831
2832 uhp->uh_walk = mark;
2833
2834 /* go down in the tree if we haven't been there */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002835 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != nomark
2836 && uhp->uh_prev.ptr->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002837 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002838 uhp = uhp->uh_prev.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002839 ++changes;
2840 }
2841
2842 /* go to alternate branch if we haven't been there */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002843 else if (uhp->uh_alt_next.ptr != NULL
2844 && uhp->uh_alt_next.ptr->uh_walk != nomark
2845 && uhp->uh_alt_next.ptr->uh_walk != mark)
2846 uhp = uhp->uh_alt_next.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002847
2848 /* go up in the tree if we haven't been there and we are at the
2849 * start of alternate branches */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002850 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
2851 && uhp->uh_next.ptr->uh_walk != nomark
2852 && uhp->uh_next.ptr->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002853 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002854 uhp = uhp->uh_next.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002855 --changes;
2856 }
2857
2858 else
2859 {
2860 /* need to backtrack; mark this node as done */
2861 uhp->uh_walk = nomark;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002862 if (uhp->uh_alt_prev.ptr != NULL)
2863 uhp = uhp->uh_alt_prev.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002864 else
2865 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002866 uhp = uhp->uh_next.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002867 --changes;
2868 }
2869 }
2870 }
2871
2872 if (ga.ga_len == 0)
2873 MSG(_("Nothing to undo"));
2874 else
2875 {
2876 sort_strings((char_u **)ga.ga_data, ga.ga_len);
2877
2878 msg_start();
Bram Moolenaardba01a02010-11-03 19:32:42 +01002879 msg_puts_attr((char_u *)_("number changes when saved"),
Bram Moolenaara800b422010-06-27 01:15:55 +02002880 hl_attr(HLF_T));
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002881 for (i = 0; i < ga.ga_len && !got_int; ++i)
2882 {
2883 msg_putchar('\n');
2884 if (got_int)
2885 break;
2886 msg_puts(((char_u **)ga.ga_data)[i]);
2887 }
2888 msg_end();
2889
2890 ga_clear_strings(&ga);
2891 }
2892}
2893
2894/*
2895 * Put the timestamp of an undo header in "buf[buflen]" in a nice format.
2896 */
2897 static void
2898u_add_time(buf, buflen, tt)
2899 char_u *buf;
2900 size_t buflen;
2901 time_t tt;
2902{
2903#ifdef HAVE_STRFTIME
2904 struct tm *curtime;
2905
2906 if (time(NULL) - tt >= 100)
2907 {
2908 curtime = localtime(&tt);
Bram Moolenaardba01a02010-11-03 19:32:42 +01002909 if (time(NULL) - tt < (60L * 60L * 12L))
2910 /* within 12 hours */
2911 (void)strftime((char *)buf, buflen, "%H:%M:%S", curtime);
Bram Moolenaardba01a02010-11-03 19:32:42 +01002912 else
Bram Moolenaarb9ce83e2012-08-23 12:59:02 +02002913 /* longer ago */
Bram Moolenaar5e3d6ca2011-01-22 21:25:11 +01002914 (void)strftime((char *)buf, buflen, "%Y/%m/%d %H:%M:%S", curtime);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002915 }
2916 else
2917#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002918 vim_snprintf((char *)buf, buflen, _("%ld seconds ago"),
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002919 (long)(time(NULL) - tt));
2920}
2921
2922/*
Bram Moolenaare224ffa2006-03-01 00:01:28 +00002923 * ":undojoin": continue adding to the last entry list
2924 */
Bram Moolenaare224ffa2006-03-01 00:01:28 +00002925 void
2926ex_undojoin(eap)
Bram Moolenaarfff2bee2010-05-15 13:56:02 +02002927 exarg_T *eap UNUSED;
Bram Moolenaare224ffa2006-03-01 00:01:28 +00002928{
Bram Moolenaare224ffa2006-03-01 00:01:28 +00002929 if (curbuf->b_u_newhead == NULL)
2930 return; /* nothing changed before */
Bram Moolenaar57657d82006-04-21 22:12:41 +00002931 if (curbuf->b_u_curhead != NULL)
2932 {
2933 EMSG(_("E790: undojoin is not allowed after undo"));
2934 return;
2935 }
2936 if (!curbuf->b_u_synced)
2937 return; /* already unsynced */
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002938 if (get_undolevel() < 0)
Bram Moolenaare224ffa2006-03-01 00:01:28 +00002939 return; /* no entries, nothing to do */
2940 else
2941 {
2942 /* Go back to the last entry */
2943 curbuf->b_u_curhead = curbuf->b_u_newhead;
2944 curbuf->b_u_synced = FALSE; /* no entries, nothing to do */
2945 }
2946}
2947
2948/*
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002949 * Called after writing or reloading the file and setting b_changed to FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 * Now an undo means that the buffer is modified.
2951 */
2952 void
2953u_unchanged(buf)
2954 buf_T *buf;
2955{
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002956 u_unch_branch(buf->b_u_oldhead);
2957 buf->b_did_warn = FALSE;
2958}
2959
Bram Moolenaar730cde92010-06-27 05:18:54 +02002960/*
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02002961 * After reloading a buffer which was saved for 'undoreload': Find the first
2962 * line that was changed and set the cursor there.
2963 */
2964 void
2965u_find_first_changed()
2966{
2967 u_header_T *uhp = curbuf->b_u_newhead;
2968 u_entry_T *uep;
2969 linenr_T lnum;
2970
2971 if (curbuf->b_u_curhead != NULL || uhp == NULL)
2972 return; /* undid something in an autocmd? */
2973
2974 /* Check that the last undo block was for the whole file. */
2975 uep = uhp->uh_entry;
2976 if (uep->ue_top != 0 || uep->ue_bot != 0)
2977 return;
2978
2979 for (lnum = 1; lnum < curbuf->b_ml.ml_line_count
2980 && lnum <= uep->ue_size; ++lnum)
2981 if (STRCMP(ml_get_buf(curbuf, lnum, FALSE),
2982 uep->ue_array[lnum - 1]) != 0)
2983 {
2984 clearpos(&(uhp->uh_cursor));
2985 uhp->uh_cursor.lnum = lnum;
2986 return;
2987 }
2988 if (curbuf->b_ml.ml_line_count != uep->ue_size)
2989 {
2990 /* lines added or deleted at the end, put the cursor there */
2991 clearpos(&(uhp->uh_cursor));
2992 uhp->uh_cursor.lnum = lnum;
2993 }
2994}
2995
2996/*
Bram Moolenaar730cde92010-06-27 05:18:54 +02002997 * Increase the write count, store it in the last undo header, what would be
2998 * used for "u".
2999 */
3000 void
3001u_update_save_nr(buf)
3002 buf_T *buf;
3003{
3004 u_header_T *uhp;
3005
3006 ++buf->b_u_save_nr_last;
3007 buf->b_u_save_nr_cur = buf->b_u_save_nr_last;
3008 uhp = buf->b_u_curhead;
3009 if (uhp != NULL)
3010 uhp = uhp->uh_next.ptr;
3011 else
3012 uhp = buf->b_u_newhead;
3013 if (uhp != NULL)
3014 uhp->uh_save_nr = buf->b_u_save_nr_last;
3015}
3016
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003017 static void
3018u_unch_branch(uhp)
3019 u_header_T *uhp;
3020{
Bram Moolenaar1e607892006-03-13 22:15:53 +00003021 u_header_T *uh;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003023 for (uh = uhp; uh != NULL; uh = uh->uh_prev.ptr)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003024 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 uh->uh_flags |= UH_CHANGED;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003026 if (uh->uh_alt_next.ptr != NULL)
3027 u_unch_branch(uh->uh_alt_next.ptr); /* recursive */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029}
3030
3031/*
3032 * Get pointer to last added entry.
3033 * If it's not valid, give an error message and return NULL.
3034 */
3035 static u_entry_T *
3036u_get_headentry()
3037{
3038 if (curbuf->b_u_newhead == NULL || curbuf->b_u_newhead->uh_entry == NULL)
3039 {
3040 EMSG(_("E439: undo list corrupt"));
3041 return NULL;
3042 }
3043 return curbuf->b_u_newhead->uh_entry;
3044}
3045
3046/*
3047 * u_getbot(): compute the line number of the previous u_save
3048 * It is called only when b_u_synced is FALSE.
3049 */
3050 static void
3051u_getbot()
3052{
3053 u_entry_T *uep;
3054 linenr_T extra;
3055
3056 uep = u_get_headentry(); /* check for corrupt undo list */
3057 if (uep == NULL)
3058 return;
3059
3060 uep = curbuf->b_u_newhead->uh_getbot_entry;
3061 if (uep != NULL)
3062 {
3063 /*
3064 * the new ue_bot is computed from the number of lines that has been
3065 * inserted (0 - deleted) since calling u_save. This is equal to the
3066 * old line count subtracted from the current line count.
3067 */
3068 extra = curbuf->b_ml.ml_line_count - uep->ue_lcount;
3069 uep->ue_bot = uep->ue_top + uep->ue_size + 1 + extra;
3070 if (uep->ue_bot < 1 || uep->ue_bot > curbuf->b_ml.ml_line_count)
3071 {
3072 EMSG(_("E440: undo line missing"));
3073 uep->ue_bot = uep->ue_top + 1; /* assume all lines deleted, will
3074 * get all the old lines back
3075 * without deleting the current
3076 * ones */
3077 }
3078
3079 curbuf->b_u_newhead->uh_getbot_entry = NULL;
3080 }
3081
3082 curbuf->b_u_synced = TRUE;
3083}
3084
3085/*
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003086 * Free one header "uhp" and its entry list and adjust the pointers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 */
3088 static void
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003089u_freeheader(buf, uhp, uhpp)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003090 buf_T *buf;
Bram Moolenaar1e607892006-03-13 22:15:53 +00003091 u_header_T *uhp;
3092 u_header_T **uhpp; /* if not NULL reset when freeing this header */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093{
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003094 u_header_T *uhap;
3095
Bram Moolenaar1e607892006-03-13 22:15:53 +00003096 /* When there is an alternate redo list free that branch completely,
3097 * because we can never go there. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003098 if (uhp->uh_alt_next.ptr != NULL)
3099 u_freebranch(buf, uhp->uh_alt_next.ptr, uhpp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003101 if (uhp->uh_alt_prev.ptr != NULL)
3102 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103
Bram Moolenaar1e607892006-03-13 22:15:53 +00003104 /* Update the links in the list to remove the header. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003105 if (uhp->uh_next.ptr == NULL)
3106 buf->b_u_oldhead = uhp->uh_prev.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003108 uhp->uh_next.ptr->uh_prev.ptr = uhp->uh_prev.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003110 if (uhp->uh_prev.ptr == NULL)
3111 buf->b_u_newhead = uhp->uh_next.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003113 for (uhap = uhp->uh_prev.ptr; uhap != NULL;
3114 uhap = uhap->uh_alt_next.ptr)
3115 uhap->uh_next.ptr = uhp->uh_next.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116
Bram Moolenaar1e607892006-03-13 22:15:53 +00003117 u_freeentries(buf, uhp, uhpp);
3118}
3119
3120/*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003121 * Free an alternate branch and any following alternate branches.
Bram Moolenaar1e607892006-03-13 22:15:53 +00003122 */
3123 static void
3124u_freebranch(buf, uhp, uhpp)
3125 buf_T *buf;
3126 u_header_T *uhp;
3127 u_header_T **uhpp; /* if not NULL reset when freeing this header */
3128{
3129 u_header_T *tofree, *next;
3130
Bram Moolenaar07d06772007-11-10 21:51:15 +00003131 /* If this is the top branch we may need to use u_freeheader() to update
3132 * all the pointers. */
3133 if (uhp == buf->b_u_oldhead)
3134 {
Bram Moolenaaraa887322013-11-07 03:04:11 +01003135 while (buf->b_u_oldhead != NULL)
3136 u_freeheader(buf, buf->b_u_oldhead, uhpp);
Bram Moolenaar07d06772007-11-10 21:51:15 +00003137 return;
3138 }
3139
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003140 if (uhp->uh_alt_prev.ptr != NULL)
3141 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +00003142
3143 next = uhp;
3144 while (next != NULL)
3145 {
3146 tofree = next;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003147 if (tofree->uh_alt_next.ptr != NULL)
3148 u_freebranch(buf, tofree->uh_alt_next.ptr, uhpp); /* recursive */
3149 next = tofree->uh_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00003150 u_freeentries(buf, tofree, uhpp);
3151 }
3152}
3153
3154/*
3155 * Free all the undo entries for one header and the header itself.
3156 * This means that "uhp" is invalid when returning.
3157 */
3158 static void
3159u_freeentries(buf, uhp, uhpp)
3160 buf_T *buf;
3161 u_header_T *uhp;
3162 u_header_T **uhpp; /* if not NULL reset when freeing this header */
3163{
3164 u_entry_T *uep, *nuep;
3165
3166 /* Check for pointers to the header that become invalid now. */
3167 if (buf->b_u_curhead == uhp)
3168 buf->b_u_curhead = NULL;
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003169 if (buf->b_u_newhead == uhp)
3170 buf->b_u_newhead = NULL; /* freeing the newest entry */
Bram Moolenaar1e607892006-03-13 22:15:53 +00003171 if (uhpp != NULL && uhp == *uhpp)
3172 *uhpp = NULL;
3173
3174 for (uep = uhp->uh_entry; uep != NULL; uep = nuep)
3175 {
3176 nuep = uep->ue_next;
3177 u_freeentry(uep, uep->ue_size);
3178 }
3179
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003180#ifdef U_DEBUG
3181 uhp->uh_magic = 0;
3182#endif
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003183 vim_free((char_u *)uhp);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003184 --buf->b_u_numhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185}
3186
3187/*
3188 * free entry 'uep' and 'n' lines in uep->ue_array[]
3189 */
3190 static void
3191u_freeentry(uep, n)
3192 u_entry_T *uep;
3193 long n;
3194{
Bram Moolenaar8d343302005-07-12 22:46:17 +00003195 while (n > 0)
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003196 vim_free(uep->ue_array[--n]);
3197 vim_free((char_u *)uep->ue_array);
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003198#ifdef U_DEBUG
3199 uep->ue_magic = 0;
3200#endif
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003201 vim_free((char_u *)uep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202}
3203
3204/*
3205 * invalidate the undo buffer; called when storage has already been released
3206 */
3207 void
3208u_clearall(buf)
3209 buf_T *buf;
3210{
3211 buf->b_u_newhead = buf->b_u_oldhead = buf->b_u_curhead = NULL;
3212 buf->b_u_synced = TRUE;
3213 buf->b_u_numhead = 0;
3214 buf->b_u_line_ptr = NULL;
3215 buf->b_u_line_lnum = 0;
3216}
3217
3218/*
3219 * save the line "lnum" for the "U" command
3220 */
3221 void
3222u_saveline(lnum)
3223 linenr_T lnum;
3224{
3225 if (lnum == curbuf->b_u_line_lnum) /* line is already saved */
3226 return;
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00003227 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) /* should never happen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 return;
3229 u_clearline();
3230 curbuf->b_u_line_lnum = lnum;
3231 if (curwin->w_cursor.lnum == lnum)
3232 curbuf->b_u_line_colnr = curwin->w_cursor.col;
3233 else
3234 curbuf->b_u_line_colnr = 0;
3235 if ((curbuf->b_u_line_ptr = u_save_line(lnum)) == NULL)
3236 do_outofmem_msg((long_u)0);
3237}
3238
3239/*
3240 * clear the line saved for the "U" command
3241 * (this is used externally for crossing a line while in insert mode)
3242 */
3243 void
3244u_clearline()
3245{
3246 if (curbuf->b_u_line_ptr != NULL)
3247 {
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003248 vim_free(curbuf->b_u_line_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 curbuf->b_u_line_ptr = NULL;
3250 curbuf->b_u_line_lnum = 0;
3251 }
3252}
3253
3254/*
3255 * Implementation of the "U" command.
3256 * Differentiation from vi: "U" can be undone with the next "U".
3257 * We also allow the cursor to be in another line.
Bram Moolenaard04b7502010-07-08 22:27:55 +02003258 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 */
3260 void
3261u_undoline()
3262{
3263 colnr_T t;
3264 char_u *oldp;
3265
3266 if (undo_off)
3267 return;
3268
Bram Moolenaare3300c82008-02-13 14:21:38 +00003269 if (curbuf->b_u_line_ptr == NULL
3270 || curbuf->b_u_line_lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 {
3272 beep_flush();
3273 return;
3274 }
Bram Moolenaare3300c82008-02-13 14:21:38 +00003275
3276 /* first save the line for the 'u' command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 if (u_savecommon(curbuf->b_u_line_lnum - 1,
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003278 curbuf->b_u_line_lnum + 1, (linenr_T)0, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 return;
3280 oldp = u_save_line(curbuf->b_u_line_lnum);
3281 if (oldp == NULL)
3282 {
3283 do_outofmem_msg((long_u)0);
3284 return;
3285 }
3286 ml_replace(curbuf->b_u_line_lnum, curbuf->b_u_line_ptr, TRUE);
3287 changed_bytes(curbuf->b_u_line_lnum, 0);
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003288 vim_free(curbuf->b_u_line_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 curbuf->b_u_line_ptr = oldp;
3290
3291 t = curbuf->b_u_line_colnr;
3292 if (curwin->w_cursor.lnum == curbuf->b_u_line_lnum)
3293 curbuf->b_u_line_colnr = curwin->w_cursor.col;
3294 curwin->w_cursor.col = t;
3295 curwin->w_cursor.lnum = curbuf->b_u_line_lnum;
Bram Moolenaare3300c82008-02-13 14:21:38 +00003296 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297}
3298
3299/*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003300 * Free all allocated memory blocks for the buffer 'buf'.
3301 */
3302 void
3303u_blockfree(buf)
3304 buf_T *buf;
3305{
Bram Moolenaar1e607892006-03-13 22:15:53 +00003306 while (buf->b_u_oldhead != NULL)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003307 u_freeheader(buf, buf->b_u_oldhead, NULL);
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003308 vim_free(buf->b_u_line_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309}
3310
3311/*
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003312 * u_save_line(): allocate memory and copy line 'lnum' into it.
3313 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 */
3315 static char_u *
3316u_save_line(lnum)
3317 linenr_T lnum;
3318{
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003319 return vim_strsave(ml_get(lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320}
3321
3322/*
3323 * Check if the 'modified' flag is set, or 'ff' has changed (only need to
3324 * check the first character, because it can only be "dos", "unix" or "mac").
3325 * "nofile" and "scratch" type buffers are considered to always be unchanged.
3326 */
3327 int
3328bufIsChanged(buf)
3329 buf_T *buf;
3330{
3331 return
3332#ifdef FEAT_QUICKFIX
3333 !bt_dontwrite(buf) &&
3334#endif
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003335 (buf->b_changed || file_ff_differs(buf, TRUE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336}
3337
3338 int
3339curbufIsChanged()
3340{
3341 return
3342#ifdef FEAT_QUICKFIX
3343 !bt_dontwrite(curbuf) &&
3344#endif
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003345 (curbuf->b_changed || file_ff_differs(curbuf, TRUE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346}
Bram Moolenaara800b422010-06-27 01:15:55 +02003347
3348#if defined(FEAT_EVAL) || defined(PROTO)
3349/*
3350 * For undotree(): Append the list of undo blocks at "first_uhp" to "list".
3351 * Recursive.
3352 */
3353 void
3354u_eval_tree(first_uhp, list)
3355 u_header_T *first_uhp;
3356 list_T *list;
3357{
3358 u_header_T *uhp = first_uhp;
3359 dict_T *dict;
3360
3361 while (uhp != NULL)
3362 {
3363 dict = dict_alloc();
3364 if (dict == NULL)
3365 return;
3366 dict_add_nr_str(dict, "seq", uhp->uh_seq, NULL);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02003367 dict_add_nr_str(dict, "time", (long)uhp->uh_time, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +02003368 if (uhp == curbuf->b_u_newhead)
3369 dict_add_nr_str(dict, "newhead", 1, NULL);
3370 if (uhp == curbuf->b_u_curhead)
3371 dict_add_nr_str(dict, "curhead", 1, NULL);
3372 if (uhp->uh_save_nr > 0)
3373 dict_add_nr_str(dict, "save", uhp->uh_save_nr, NULL);
3374
3375 if (uhp->uh_alt_next.ptr != NULL)
3376 {
3377 list_T *alt_list = list_alloc();
3378
3379 if (alt_list != NULL)
3380 {
3381 /* Recursive call to add alternate undo tree. */
3382 u_eval_tree(uhp->uh_alt_next.ptr, alt_list);
3383 dict_add_list(dict, "alt", alt_list);
3384 }
3385 }
3386
3387 list_append_dict(list, dict);
3388 uhp = uhp->uh_prev.ptr;
3389 }
3390}
3391#endif