blob: 435decc3ae000dfa9acb7a74d3944be3eb35530b [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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 Moolenaar8f4ac012014-08-10 13:38:34 +020084/* Size of buffer used for encryption. */
85#define CRYPT_BUF_SIZE 8192
86
Bram Moolenaar071d4272004-06-13 20:20:40 +000087#include "vim.h"
88
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020089/* Structure passed around between functions.
90 * Avoids passing cryptstate_T when encryption not available. */
91typedef struct {
92 buf_T *bi_buf;
93 FILE *bi_fp;
94#ifdef FEAT_CRYPT
95 cryptstate_T *bi_state;
96 char_u *bi_buffer; /* CRYPT_BUF_SIZE, NULL when not buffering */
97 size_t bi_used; /* bytes written to/read from bi_buffer */
98 size_t bi_avail; /* bytes available in bi_buffer */
99#endif
100} bufinfo_T;
101
102
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100103static long get_undolevel(void);
104static void u_unch_branch(u_header_T *uhp);
105static u_entry_T *u_get_headentry(void);
106static void u_getbot(void);
107static void u_doit(int count);
108static void u_undoredo(int undo);
109static void u_undo_end(int did_undo, int absolute);
110static void u_add_time(char_u *buf, size_t buflen, time_t tt);
111static void u_freeheader(buf_T *buf, u_header_T *uhp, u_header_T **uhpp);
112static void u_freebranch(buf_T *buf, u_header_T *uhp, u_header_T **uhpp);
113static void u_freeentries(buf_T *buf, u_header_T *uhp, u_header_T **uhpp);
114static void u_freeentry(u_entry_T *, long);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200115#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100116static void corruption_error(char *mesg, char_u *file_name);
117static void u_free_uhp(u_header_T *uhp);
118static int undo_write(bufinfo_T *bi, char_u *ptr, size_t len);
Bram Moolenaar1d6fbe62016-02-19 22:46:34 +0100119# ifdef FEAT_CRYPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100120static int undo_flush(bufinfo_T *bi);
Bram Moolenaar1d6fbe62016-02-19 22:46:34 +0100121# endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100122static int fwrite_crypt(bufinfo_T *bi, char_u *ptr, size_t len);
123static int undo_write_bytes(bufinfo_T *bi, long_u nr, int len);
124static void put_header_ptr(bufinfo_T *bi, u_header_T *uhp);
125static int undo_read_4c(bufinfo_T *bi);
126static int undo_read_2c(bufinfo_T *bi);
127static int undo_read_byte(bufinfo_T *bi);
128static time_t undo_read_time(bufinfo_T *bi);
129static int undo_read(bufinfo_T *bi, char_u *buffer, size_t size);
130static char_u *read_string_decrypt(bufinfo_T *bi, int len);
131static int serialize_header(bufinfo_T *bi, char_u *hash);
132static int serialize_uhp(bufinfo_T *bi, u_header_T *uhp);
133static u_header_T *unserialize_uhp(bufinfo_T *bi, char_u *file_name);
134static int serialize_uep(bufinfo_T *bi, u_entry_T *uep);
135static u_entry_T *unserialize_uep(bufinfo_T *bi, int *error, char_u *file_name);
136static void serialize_pos(bufinfo_T *bi, pos_T pos);
137static void unserialize_pos(bufinfo_T *bi, pos_T *pos);
138static void serialize_visualinfo(bufinfo_T *bi, visualinfo_T *info);
139static void unserialize_visualinfo(bufinfo_T *bi, visualinfo_T *info);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141
Bram Moolenaarf05e3b02010-05-29 15:40:47 +0200142#define U_ALLOC_LINE(size) lalloc((long_u)(size), FALSE)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100143static char_u *u_save_line(linenr_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
Bram Moolenaar730cde92010-06-27 05:18:54 +0200145/* used in undo_end() to report number of added and deleted lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146static long u_newcount, u_oldcount;
147
148/*
149 * When 'u' flag included in 'cpoptions', we behave like vi. Need to remember
150 * the action that "u" should do.
151 */
152static int undo_undoes = FALSE;
153
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200154static int lastmark = 0;
155
Bram Moolenaar9db58062010-05-29 20:33:07 +0200156#if defined(U_DEBUG) || defined(PROTO)
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000157/*
158 * Check the undo structures for being valid. Print a warning when something
159 * looks wrong.
160 */
161static int seen_b_u_curhead;
162static int seen_b_u_newhead;
163static int header_count;
164
165 static void
166u_check_tree(u_header_T *uhp,
167 u_header_T *exp_uh_next,
168 u_header_T *exp_uh_alt_prev)
169{
170 u_entry_T *uep;
171
172 if (uhp == NULL)
173 return;
174 ++header_count;
175 if (uhp == curbuf->b_u_curhead && ++seen_b_u_curhead > 1)
176 {
177 EMSG("b_u_curhead found twice (looping?)");
178 return;
179 }
180 if (uhp == curbuf->b_u_newhead && ++seen_b_u_newhead > 1)
181 {
182 EMSG("b_u_newhead found twice (looping?)");
183 return;
184 }
185
186 if (uhp->uh_magic != UH_MAGIC)
187 EMSG("uh_magic wrong (may be using freed memory)");
188 else
189 {
190 /* Check pointers back are correct. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200191 if (uhp->uh_next.ptr != exp_uh_next)
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000192 {
193 EMSG("uh_next wrong");
194 smsg((char_u *)"expected: 0x%x, actual: 0x%x",
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200195 exp_uh_next, uhp->uh_next.ptr);
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000196 }
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200197 if (uhp->uh_alt_prev.ptr != exp_uh_alt_prev)
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000198 {
199 EMSG("uh_alt_prev wrong");
200 smsg((char_u *)"expected: 0x%x, actual: 0x%x",
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200201 exp_uh_alt_prev, uhp->uh_alt_prev.ptr);
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000202 }
203
204 /* Check the undo tree at this header. */
205 for (uep = uhp->uh_entry; uep != NULL; uep = uep->ue_next)
206 {
207 if (uep->ue_magic != UE_MAGIC)
208 {
209 EMSG("ue_magic wrong (may be using freed memory)");
210 break;
211 }
212 }
213
214 /* Check the next alt tree. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200215 u_check_tree(uhp->uh_alt_next.ptr, uhp->uh_next.ptr, uhp);
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000216
217 /* Check the next header in this branch. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200218 u_check_tree(uhp->uh_prev.ptr, uhp, NULL);
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000219 }
220}
221
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200222 static void
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000223u_check(int newhead_may_be_NULL)
224{
225 seen_b_u_newhead = 0;
226 seen_b_u_curhead = 0;
227 header_count = 0;
228
229 u_check_tree(curbuf->b_u_oldhead, NULL, NULL);
230
231 if (seen_b_u_newhead == 0 && curbuf->b_u_oldhead != NULL
232 && !(newhead_may_be_NULL && curbuf->b_u_newhead == NULL))
233 EMSGN("b_u_newhead invalid: 0x%x", curbuf->b_u_newhead);
234 if (curbuf->b_u_curhead != NULL && seen_b_u_curhead == 0)
235 EMSGN("b_u_curhead invalid: 0x%x", curbuf->b_u_curhead);
236 if (header_count != curbuf->b_u_numhead)
237 {
238 EMSG("b_u_numhead invalid");
239 smsg((char_u *)"expected: %ld, actual: %ld",
240 (long)header_count, (long)curbuf->b_u_numhead);
241 }
242}
243#endif
244
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000246 * Save the current line for both the "u" and "U" command.
Bram Moolenaar687a29c2013-04-15 15:47:12 +0200247 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000248 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 */
250 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100251u_save_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252{
253 return (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
254 (linenr_T)(curwin->w_cursor.lnum + 1)));
255}
256
257/*
258 * Save the lines between "top" and "bot" for both the "u" and "U" command.
259 * "top" may be 0 and bot may be curbuf->b_ml.ml_line_count + 1.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200260 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 * Returns FAIL when lines could not be saved, OK otherwise.
262 */
263 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100264u_save(linenr_T top, linenr_T bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265{
266 if (undo_off)
267 return OK;
268
Bram Moolenaar687a29c2013-04-15 15:47:12 +0200269 if (top > curbuf->b_ml.ml_line_count
270 || top >= bot
271 || bot > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 return FALSE; /* rely on caller to do error messages */
273
274 if (top + 2 == bot)
275 u_saveline((linenr_T)(top + 1));
276
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200277 return (u_savecommon(top, bot, (linenr_T)0, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278}
279
280/*
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200281 * Save the line "lnum" (used by ":s" and "~" command).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282 * The line is replaced, so the new bottom line is lnum + 1.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200283 * Careful: may trigger autocommands that reload the buffer.
284 * Returns FAIL when lines could not be saved, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 */
286 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100287u_savesub(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288{
289 if (undo_off)
290 return OK;
291
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200292 return (u_savecommon(lnum - 1, lnum + 1, lnum + 1, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293}
294
295/*
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200296 * A new line is inserted before line "lnum" (used by :s command).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297 * The line is inserted, so the new bottom line is lnum + 1.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200298 * Careful: may trigger autocommands that reload the buffer.
299 * Returns FAIL when lines could not be saved, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 */
301 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100302u_inssub(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303{
304 if (undo_off)
305 return OK;
306
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200307 return (u_savecommon(lnum - 1, lnum, lnum + 1, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308}
309
310/*
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200311 * Save the lines "lnum" - "lnum" + nlines (used by delete command).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 * The lines are deleted, so the new bottom line is lnum, unless the buffer
313 * becomes empty.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200314 * Careful: may trigger autocommands that reload the buffer.
315 * Returns FAIL when lines could not be saved, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 */
317 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100318u_savedel(linenr_T lnum, long nlines)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319{
320 if (undo_off)
321 return OK;
322
323 return (u_savecommon(lnum - 1, lnum + nlines,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200324 nlines == curbuf->b_ml.ml_line_count ? 2 : lnum, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325}
326
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000327/*
328 * Return TRUE when undo is allowed. Otherwise give an error message and
329 * return FALSE.
330 */
Bram Moolenaarce6ef252006-07-12 19:49:41 +0000331 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100332undo_allowed(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000333{
334 /* Don't allow changes when 'modifiable' is off. */
335 if (!curbuf->b_p_ma)
336 {
337 EMSG(_(e_modifiable));
338 return FALSE;
339 }
340
341#ifdef HAVE_SANDBOX
342 /* In the sandbox it's not allowed to change the text. */
343 if (sandbox != 0)
344 {
345 EMSG(_(e_sandbox));
346 return FALSE;
347 }
348#endif
349
350 /* Don't allow changes in the buffer while editing the cmdline. The
351 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000352 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000353 {
354 EMSG(_(e_secure));
355 return FALSE;
356 }
357
358 return TRUE;
359}
360
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200361/*
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100362 * Get the undolevle value for the current buffer.
363 */
364 static long
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100365get_undolevel(void)
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100366{
367 if (curbuf->b_p_ul == NO_LOCAL_UNDOLEVEL)
368 return p_ul;
369 return curbuf->b_p_ul;
370}
371
372/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200373 * Common code for various ways to save text before a change.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200374 * "top" is the line above the first changed line.
375 * "bot" is the line below the last changed line.
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200376 * "newbot" is the new bottom line. Use zero when not known.
377 * "reload" is TRUE when saving for a buffer reload.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200378 * Careful: may trigger autocommands that reload the buffer.
379 * Returns FAIL when lines could not be saved, OK otherwise.
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200380 */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200381 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100382u_savecommon(
383 linenr_T top,
384 linenr_T bot,
385 linenr_T newbot,
386 int reload)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387{
Bram Moolenaar1e607892006-03-13 22:15:53 +0000388 linenr_T lnum;
389 long i;
390 u_header_T *uhp;
391 u_header_T *old_curhead;
392 u_entry_T *uep;
393 u_entry_T *prev_uep;
394 long size;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200396 if (!reload)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 {
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200398 /* When making changes is not allowed return FAIL. It's a crude way
399 * to make all change commands fail. */
400 if (!undo_allowed())
Bram Moolenaar009b2592004-10-24 19:18:58 +0000401 return FAIL;
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200402
403#ifdef FEAT_NETBEANS_INTG
404 /*
405 * Netbeans defines areas that cannot be modified. Bail out here when
406 * trying to change text in a guarded area.
407 */
408 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +0000409 {
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200410 if (netbeans_is_guarded(top, bot))
411 {
412 EMSG(_(e_guarded));
413 return FAIL;
414 }
415 if (curbuf->b_p_ro)
416 {
417 EMSG(_(e_nbreadonly));
418 return FAIL;
419 }
Bram Moolenaar009b2592004-10-24 19:18:58 +0000420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200422#ifdef FEAT_TERMINAL
423 /* A change in a terminal buffer removes the highlighting. */
424 term_change_in_curbuf();
425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426
427#ifdef FEAT_AUTOCMD
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200428 /*
429 * Saving text for undo means we are going to make a change. Give a
430 * warning for a read-only file before making the change, so that the
431 * FileChangedRO event can replace the buffer with a read-write version
432 * (e.g., obtained from a source control system).
433 */
434 change_warning(0);
435 if (bot > curbuf->b_ml.ml_line_count + 1)
436 {
437 /* This happens when the FileChangedRO autocommand changes the
438 * file in a way it becomes shorter. */
Bram Moolenaarb4d587c2014-01-23 18:12:49 +0100439 EMSG(_("E881: Line count changed unexpectedly"));
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200440 return FAIL;
441 }
442#endif
Bram Moolenaard04b7502010-07-08 22:27:55 +0200443 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200444
445#ifdef U_DEBUG
446 u_check(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447#endif
448
449 size = bot - top - 1;
450
451 /*
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200452 * If curbuf->b_u_synced == TRUE make a new header.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 */
454 if (curbuf->b_u_synced)
455 {
456#ifdef FEAT_JUMPLIST
457 /* Need to create new entry in b_changelist. */
458 curbuf->b_new_change = TRUE;
459#endif
460
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100461 if (get_undolevel() >= 0)
Bram Moolenaar1e607892006-03-13 22:15:53 +0000462 {
463 /*
464 * Make a new header entry. Do this first so that we don't mess
465 * up the undo info when out of memory.
466 */
Bram Moolenaarf05e3b02010-05-29 15:40:47 +0200467 uhp = (u_header_T *)U_ALLOC_LINE(sizeof(u_header_T));
Bram Moolenaar1e607892006-03-13 22:15:53 +0000468 if (uhp == NULL)
469 goto nomem;
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000470#ifdef U_DEBUG
471 uhp->uh_magic = UH_MAGIC;
472#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +0000473 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000474 else
475 uhp = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000476
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 /*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000478 * If we undid more than we redid, move the entry lists before and
479 * including curbuf->b_u_curhead to an alternate branch.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 */
Bram Moolenaar1e607892006-03-13 22:15:53 +0000481 old_curhead = curbuf->b_u_curhead;
482 if (old_curhead != NULL)
483 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200484 curbuf->b_u_newhead = old_curhead->uh_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000485 curbuf->b_u_curhead = NULL;
486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487
488 /*
489 * free headers to keep the size right
490 */
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100491 while (curbuf->b_u_numhead > get_undolevel()
492 && curbuf->b_u_oldhead != NULL)
Bram Moolenaar1e607892006-03-13 22:15:53 +0000493 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000494 u_header_T *uhfree = curbuf->b_u_oldhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000495
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000496 if (uhfree == old_curhead)
497 /* Can't reconnect the branch, delete all of it. */
498 u_freebranch(curbuf, uhfree, &old_curhead);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200499 else if (uhfree->uh_alt_next.ptr == NULL)
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000500 /* There is no branch, only free one header. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000501 u_freeheader(curbuf, uhfree, &old_curhead);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000502 else
503 {
504 /* Free the oldest alternate branch as a whole. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200505 while (uhfree->uh_alt_next.ptr != NULL)
506 uhfree = uhfree->uh_alt_next.ptr;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000507 u_freebranch(curbuf, uhfree, &old_curhead);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000508 }
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000509#ifdef U_DEBUG
510 u_check(TRUE);
511#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +0000512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000514 if (uhp == NULL) /* no undo at all */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 {
Bram Moolenaar1e607892006-03-13 22:15:53 +0000516 if (old_curhead != NULL)
517 u_freebranch(curbuf, old_curhead, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 curbuf->b_u_synced = FALSE;
519 return OK;
520 }
521
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200522 uhp->uh_prev.ptr = NULL;
523 uhp->uh_next.ptr = curbuf->b_u_newhead;
524 uhp->uh_alt_next.ptr = old_curhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000525 if (old_curhead != NULL)
526 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200527 uhp->uh_alt_prev.ptr = old_curhead->uh_alt_prev.ptr;
528 if (uhp->uh_alt_prev.ptr != NULL)
529 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = uhp;
530 old_curhead->uh_alt_prev.ptr = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000531 if (curbuf->b_u_oldhead == old_curhead)
532 curbuf->b_u_oldhead = uhp;
533 }
Bram Moolenaar89ed3df2007-01-09 19:23:12 +0000534 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200535 uhp->uh_alt_prev.ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 if (curbuf->b_u_newhead != NULL)
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200537 curbuf->b_u_newhead->uh_prev.ptr = uhp;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000538
Bram Moolenaarca003e12006-03-17 23:19:38 +0000539 uhp->uh_seq = ++curbuf->b_u_seq_last;
540 curbuf->b_u_seq_cur = uhp->uh_seq;
Bram Moolenaar170b10b2016-07-29 16:15:27 +0200541 uhp->uh_time = vim_time();
Bram Moolenaara800b422010-06-27 01:15:55 +0200542 uhp->uh_save_nr = 0;
543 curbuf->b_u_time_cur = uhp->uh_time + 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000544
Bram Moolenaar1e607892006-03-13 22:15:53 +0000545 uhp->uh_walk = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 uhp->uh_entry = NULL;
547 uhp->uh_getbot_entry = NULL;
548 uhp->uh_cursor = curwin->w_cursor; /* save cursor pos. for undo */
549#ifdef FEAT_VIRTUALEDIT
550 if (virtual_active() && curwin->w_cursor.coladd > 0)
551 uhp->uh_cursor_vcol = getviscol();
552 else
553 uhp->uh_cursor_vcol = -1;
554#endif
555
556 /* save changed and buffer empty flag for undo */
557 uhp->uh_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
558 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
559
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000560 /* save named marks and Visual marks for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 mch_memmove(uhp->uh_namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000562 uhp->uh_visual = curbuf->b_visual;
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000563
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 curbuf->b_u_newhead = uhp;
565 if (curbuf->b_u_oldhead == NULL)
566 curbuf->b_u_oldhead = uhp;
567 ++curbuf->b_u_numhead;
568 }
569 else
570 {
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100571 if (get_undolevel() < 0) /* no undo at all */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 return OK;
573
574 /*
575 * When saving a single line, and it has been saved just before, it
576 * doesn't make sense saving it again. Saves a lot of memory when
577 * making lots of changes inside the same line.
578 * This is only possible if the previous change didn't increase or
579 * decrease the number of lines.
580 * Check the ten last changes. More doesn't make sense and takes too
581 * long.
582 */
583 if (size == 1)
584 {
585 uep = u_get_headentry();
586 prev_uep = NULL;
587 for (i = 0; i < 10; ++i)
588 {
589 if (uep == NULL)
590 break;
591
592 /* If lines have been inserted/deleted we give up.
593 * Also when the line was included in a multi-line save. */
594 if ((curbuf->b_u_newhead->uh_getbot_entry != uep
595 ? (uep->ue_top + uep->ue_size + 1
596 != (uep->ue_bot == 0
597 ? curbuf->b_ml.ml_line_count + 1
598 : uep->ue_bot))
599 : uep->ue_lcount != curbuf->b_ml.ml_line_count)
600 || (uep->ue_size > 1
601 && top >= uep->ue_top
602 && top + 2 <= uep->ue_top + uep->ue_size + 1))
603 break;
604
605 /* If it's the same line we can skip saving it again. */
606 if (uep->ue_size == 1 && uep->ue_top == top)
607 {
608 if (i > 0)
609 {
610 /* It's not the last entry: get ue_bot for the last
611 * entry now. Following deleted/inserted lines go to
612 * the re-used entry. */
613 u_getbot();
614 curbuf->b_u_synced = FALSE;
615
616 /* Move the found entry to become the last entry. The
617 * order of undo/redo doesn't matter for the entries
618 * we move it over, since they don't change the line
619 * count and don't include this line. It does matter
620 * for the found entry if the line count is changed by
621 * the executed command. */
622 prev_uep->ue_next = uep->ue_next;
623 uep->ue_next = curbuf->b_u_newhead->uh_entry;
624 curbuf->b_u_newhead->uh_entry = uep;
625 }
626
627 /* The executed command may change the line count. */
628 if (newbot != 0)
629 uep->ue_bot = newbot;
630 else if (bot > curbuf->b_ml.ml_line_count)
631 uep->ue_bot = 0;
632 else
633 {
634 uep->ue_lcount = curbuf->b_ml.ml_line_count;
635 curbuf->b_u_newhead->uh_getbot_entry = uep;
636 }
637 return OK;
638 }
639 prev_uep = uep;
640 uep = uep->ue_next;
641 }
642 }
643
644 /* find line number for ue_bot for previous u_save() */
645 u_getbot();
646 }
647
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200648#if !defined(UNIX) && !defined(WIN32)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100650 * With Amiga we can't handle big undo's, because
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 * then u_alloc_line would have to allocate a block larger than 32K
652 */
653 if (size >= 8000)
654 goto nomem;
655#endif
656
657 /*
658 * add lines in front of entry list
659 */
Bram Moolenaarf05e3b02010-05-29 15:40:47 +0200660 uep = (u_entry_T *)U_ALLOC_LINE(sizeof(u_entry_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 if (uep == NULL)
662 goto nomem;
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200663 vim_memset(uep, 0, sizeof(u_entry_T));
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000664#ifdef U_DEBUG
665 uep->ue_magic = UE_MAGIC;
666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667
668 uep->ue_size = size;
669 uep->ue_top = top;
670 if (newbot != 0)
671 uep->ue_bot = newbot;
672 /*
673 * Use 0 for ue_bot if bot is below last line.
674 * Otherwise we have to compute ue_bot later.
675 */
676 else if (bot > curbuf->b_ml.ml_line_count)
677 uep->ue_bot = 0;
678 else
679 {
680 uep->ue_lcount = curbuf->b_ml.ml_line_count;
681 curbuf->b_u_newhead->uh_getbot_entry = uep;
682 }
683
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000684 if (size > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000686 if ((uep->ue_array = (char_u **)U_ALLOC_LINE(
Bram Moolenaarf05e3b02010-05-29 15:40:47 +0200687 sizeof(char_u *) * size)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 {
689 u_freeentry(uep, 0L);
690 goto nomem;
691 }
692 for (i = 0, lnum = top + 1; i < size; ++i)
693 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000694 fast_breakcheck();
695 if (got_int)
696 {
697 u_freeentry(uep, i);
698 return FAIL;
699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 if ((uep->ue_array[i] = u_save_line(lnum++)) == NULL)
701 {
702 u_freeentry(uep, i);
703 goto nomem;
704 }
705 }
706 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000707 else
708 uep->ue_array = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 uep->ue_next = curbuf->b_u_newhead->uh_entry;
710 curbuf->b_u_newhead->uh_entry = uep;
711 curbuf->b_u_synced = FALSE;
712 undo_undoes = FALSE;
713
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000714#ifdef U_DEBUG
715 u_check(FALSE);
716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 return OK;
718
719nomem:
720 msg_silent = 0; /* must display the prompt */
721 if (ask_yesno((char_u *)_("No undo possible; continue anyway"), TRUE)
722 == 'y')
723 {
724 undo_off = TRUE; /* will be reset when character typed */
725 return OK;
726 }
727 do_outofmem_msg((long_u)0);
728 return FAIL;
729}
730
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200731#if defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200732
Bram Moolenaar9db58062010-05-29 20:33:07 +0200733# define UF_START_MAGIC "Vim\237UnDo\345" /* magic at start of undofile */
734# define UF_START_MAGIC_LEN 9
735# define UF_HEADER_MAGIC 0x5fd0 /* magic at start of header */
736# define UF_HEADER_END_MAGIC 0xe7aa /* magic after last header */
737# define UF_ENTRY_MAGIC 0xf518 /* magic at start of entry */
738# define UF_ENTRY_END_MAGIC 0x3581 /* magic after last entry */
Bram Moolenaara800b422010-06-27 01:15:55 +0200739# define UF_VERSION 2 /* 2-byte undofile version number */
Bram Moolenaara800b422010-06-27 01:15:55 +0200740# define UF_VERSION_CRYPT 0x8002 /* idem, encrypted */
741
742/* extra fields for header */
743# define UF_LAST_SAVE_NR 1
744
745/* extra fields for uhp */
746# define UHP_SAVE_NR 1
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200747
Bram Moolenaarcdf04202010-05-29 15:11:47 +0200748static char_u e_not_open[] = N_("E828: Cannot open undo file for writing: %s");
Bram Moolenaarcdf04202010-05-29 15:11:47 +0200749
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200750/*
751 * Compute the hash for the current buffer text into hash[UNDO_HASH_SIZE].
752 */
753 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100754u_compute_hash(char_u *hash)
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200755{
756 context_sha256_T ctx;
757 linenr_T lnum;
758 char_u *p;
759
760 sha256_start(&ctx);
Bram Moolenaarae7ba982011-12-08 15:14:09 +0100761 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200762 {
763 p = ml_get(lnum);
Bram Moolenaar442b4222010-05-24 21:34:22 +0200764 sha256_update(&ctx, p, (UINT32_T)(STRLEN(p) + 1));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200765 }
766 sha256_finish(&ctx, hash);
767}
768
769/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200770 * Return an allocated string of the full path of the target undofile.
771 * When "reading" is TRUE find the file to read, go over all directories in
772 * 'undodir'.
773 * When "reading" is FALSE use the first name where the directory exists.
Bram Moolenaar9db58062010-05-29 20:33:07 +0200774 * Returns NULL when there is no place to write or no file to read.
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200775 */
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200776 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100777u_get_undo_file_name(char_u *buf_ffname, int reading)
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200778{
779 char_u *dirp;
780 char_u dir_name[IOSIZE + 1];
781 char_u *munged_name = NULL;
782 char_u *undo_file_name = NULL;
783 int dir_len;
784 char_u *p;
Bram Moolenaar8767f522016-07-01 17:17:39 +0200785 stat_T st;
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200786 char_u *ffname = buf_ffname;
787#ifdef HAVE_READLINK
788 char_u fname_buf[MAXPATHL];
789#endif
790
791 if (ffname == NULL)
792 return NULL;
793
794#ifdef HAVE_READLINK
795 /* Expand symlink in the file name, so that we put the undo file with the
796 * actual file instead of with the symlink. */
797 if (resolve_symlink(ffname, fname_buf) == OK)
798 ffname = fname_buf;
799#endif
800
801 /* Loop over 'undodir'. When reading find the first file that exists.
802 * When not reading use the first directory that exists or ".". */
803 dirp = p_udir;
804 while (*dirp != NUL)
805 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200806 dir_len = copy_option_part(&dirp, dir_name, IOSIZE, ",");
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200807 if (dir_len == 1 && dir_name[0] == '.')
808 {
809 /* Use same directory as the ffname,
810 * "dir/name" -> "dir/.name.un~" */
Bram Moolenaar442b4222010-05-24 21:34:22 +0200811 undo_file_name = vim_strnsave(ffname, (int)(STRLEN(ffname) + 5));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200812 if (undo_file_name == NULL)
813 break;
814 p = gettail(undo_file_name);
Bram Moolenaar206f0112014-03-12 16:51:55 +0100815#ifdef VMS
816 /* VMS can not handle more than one dot in the filenames
817 * use "dir/name" -> "dir/_un_name" - add _un_
818 * at the beginning to keep the extension */
819 mch_memmove(p + 4, p, STRLEN(p) + 1);
820 mch_memmove(p, "_un_", 4);
821
822#else
823 /* Use same directory as the ffname,
824 * "dir/name" -> "dir/.name.un~" */
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200825 mch_memmove(p + 1, p, STRLEN(p) + 1);
826 *p = '.';
827 STRCAT(p, ".un~");
Bram Moolenaar206f0112014-03-12 16:51:55 +0100828#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200829 }
830 else
831 {
832 dir_name[dir_len] = NUL;
833 if (mch_isdir(dir_name))
834 {
835 if (munged_name == NULL)
836 {
837 munged_name = vim_strsave(ffname);
838 if (munged_name == NULL)
839 return NULL;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100840 for (p = munged_name; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200841 if (vim_ispathsep(*p))
842 *p = '%';
843 }
844 undo_file_name = concat_fnames(dir_name, munged_name, TRUE);
845 }
846 }
847
848 /* When reading check if the file exists. */
849 if (undo_file_name != NULL && (!reading
850 || mch_stat((char *)undo_file_name, &st) >= 0))
851 break;
852 vim_free(undo_file_name);
853 undo_file_name = NULL;
854 }
855
856 vim_free(munged_name);
857 return undo_file_name;
858}
859
Bram Moolenaar9db58062010-05-29 20:33:07 +0200860 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100861corruption_error(char *mesg, char_u *file_name)
Bram Moolenaar9db58062010-05-29 20:33:07 +0200862{
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200863 EMSG3(_("E825: Corrupted undo file (%s): %s"), mesg, file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +0200864}
865
866 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100867u_free_uhp(u_header_T *uhp)
Bram Moolenaar9db58062010-05-29 20:33:07 +0200868{
869 u_entry_T *nuep;
870 u_entry_T *uep;
871
872 uep = uhp->uh_entry;
873 while (uep != NULL)
874 {
875 nuep = uep->ue_next;
876 u_freeentry(uep, uep->ue_size);
877 uep = nuep;
878 }
879 vim_free(uhp);
880}
881
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200882/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200883 * Write a sequence of bytes to the undo file.
884 * Buffers and encrypts as needed.
885 * Returns OK or FAIL.
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200886 */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200887 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100888undo_write(bufinfo_T *bi, char_u *ptr, size_t len)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200889{
890#ifdef FEAT_CRYPT
891 if (bi->bi_buffer != NULL)
892 {
893 size_t len_todo = len;
894 char_u *p = ptr;
895
896 while (bi->bi_used + len_todo >= CRYPT_BUF_SIZE)
897 {
898 size_t n = CRYPT_BUF_SIZE - bi->bi_used;
899
900 mch_memmove(bi->bi_buffer + bi->bi_used, p, n);
901 len_todo -= n;
902 p += n;
903 bi->bi_used = CRYPT_BUF_SIZE;
904 if (undo_flush(bi) == FAIL)
905 return FAIL;
906 }
907 if (len_todo > 0)
908 {
909 mch_memmove(bi->bi_buffer + bi->bi_used, p, len_todo);
910 bi->bi_used += len_todo;
911 }
912 return OK;
913 }
914#endif
915 if (fwrite(ptr, len, (size_t)1, bi->bi_fp) != 1)
916 return FAIL;
917 return OK;
918}
919
920#ifdef FEAT_CRYPT
921 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100922undo_flush(bufinfo_T *bi)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200923{
Bram Moolenaar829aa642017-08-23 22:32:35 +0200924 if (bi->bi_buffer != NULL && bi->bi_state != NULL && bi->bi_used > 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200925 {
926 crypt_encode_inplace(bi->bi_state, bi->bi_buffer, bi->bi_used);
927 if (fwrite(bi->bi_buffer, bi->bi_used, (size_t)1, bi->bi_fp) != 1)
928 return FAIL;
929 bi->bi_used = 0;
930 }
931 return OK;
932}
933#endif
934
935/*
936 * Write "ptr[len]" and crypt the bytes when needed.
937 * Returns OK or FAIL.
938 */
939 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100940fwrite_crypt(bufinfo_T *bi, char_u *ptr, size_t len)
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200941{
942#ifdef FEAT_CRYPT
943 char_u *copy;
944 char_u small_buf[100];
945 size_t i;
946
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200947 if (bi->bi_state != NULL && bi->bi_buffer == NULL)
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200948 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200949 /* crypting every piece of text separately */
950 if (len < 100)
951 copy = small_buf; /* no malloc()/free() for short strings */
952 else
953 {
954 copy = lalloc(len, FALSE);
955 if (copy == NULL)
956 return 0;
957 }
958 crypt_encode(bi->bi_state, ptr, len, copy);
959 i = fwrite(copy, len, (size_t)1, bi->bi_fp);
960 if (copy != small_buf)
961 vim_free(copy);
962 return i == 1 ? OK : FAIL;
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200963 }
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200964#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200965 return undo_write(bi, ptr, len);
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200966}
967
968/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200969 * Write a number, MSB first, in "len" bytes.
970 * Must match with undo_read_?c() functions.
971 * Returns OK or FAIL.
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200972 */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200973 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100974undo_write_bytes(bufinfo_T *bi, long_u nr, int len)
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200975{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200976 char_u buf[8];
977 int i;
978 int bufi = 0;
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200979
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200980 for (i = len - 1; i >= 0; --i)
Bram Moolenaar9b8f0212014-08-13 22:05:53 +0200981 buf[bufi++] = (char_u)(nr >> (i * 8));
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200982 return undo_write(bi, buf, (size_t)len);
983}
984
985/*
986 * Write the pointer to an undo header. Instead of writing the pointer itself
987 * we use the sequence number of the header. This is converted back to
988 * pointers when reading. */
989 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100990put_header_ptr(bufinfo_T *bi, u_header_T *uhp)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200991{
992 undo_write_bytes(bi, (long_u)(uhp != NULL ? uhp->uh_seq : 0), 4);
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200993}
994
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +0200995 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100996undo_read_4c(bufinfo_T *bi)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200997{
998#ifdef FEAT_CRYPT
999 if (bi->bi_buffer != NULL)
1000 {
1001 char_u buf[4];
1002 int n;
1003
1004 undo_read(bi, buf, (size_t)4);
Bram Moolenaar35169282014-09-11 22:50:09 +02001005 n = ((unsigned)buf[0] << 24) + (buf[1] << 16) + (buf[2] << 8) + buf[3];
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001006 return n;
1007 }
1008#endif
1009 return get4c(bi->bi_fp);
1010}
1011
1012 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001013undo_read_2c(bufinfo_T *bi)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001014{
1015#ifdef FEAT_CRYPT
1016 if (bi->bi_buffer != NULL)
1017 {
1018 char_u buf[2];
1019 int n;
1020
1021 undo_read(bi, buf, (size_t)2);
1022 n = (buf[0] << 8) + buf[1];
1023 return n;
1024 }
1025#endif
1026 return get2c(bi->bi_fp);
1027}
1028
1029 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001030undo_read_byte(bufinfo_T *bi)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001031{
1032#ifdef FEAT_CRYPT
1033 if (bi->bi_buffer != NULL)
1034 {
1035 char_u buf[1];
1036
1037 undo_read(bi, buf, (size_t)1);
1038 return buf[0];
1039 }
1040#endif
1041 return getc(bi->bi_fp);
1042}
1043
1044 static time_t
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001045undo_read_time(bufinfo_T *bi)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001046{
1047#ifdef FEAT_CRYPT
1048 if (bi->bi_buffer != NULL)
1049 {
1050 char_u buf[8];
1051 time_t n = 0;
1052 int i;
1053
1054 undo_read(bi, buf, (size_t)8);
1055 for (i = 0; i < 8; ++i)
1056 n = (n << 8) + buf[i];
1057 return n;
1058 }
1059#endif
1060 return get8ctime(bi->bi_fp);
1061}
1062
1063/*
1064 * Read "buffer[size]" from the undo file.
1065 * Return OK or FAIL.
1066 */
1067 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001068undo_read(bufinfo_T *bi, char_u *buffer, size_t size)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001069{
Bram Moolenaar56f2db52017-06-11 23:09:15 +02001070 int retval = OK;
1071
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001072#ifdef FEAT_CRYPT
1073 if (bi->bi_buffer != NULL)
1074 {
Bram Moolenaar9b8f0212014-08-13 22:05:53 +02001075 int size_todo = (int)size;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001076 char_u *p = buffer;
1077
1078 while (size_todo > 0)
1079 {
1080 size_t n;
1081
1082 if (bi->bi_used >= bi->bi_avail)
1083 {
1084 n = fread(bi->bi_buffer, 1, (size_t)CRYPT_BUF_SIZE, bi->bi_fp);
Bram Moolenaar55952d42016-11-05 14:58:34 +01001085 if (n == 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001086 {
Bram Moolenaar56f2db52017-06-11 23:09:15 +02001087 retval = FAIL;
1088 break;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001089 }
1090 bi->bi_avail = n;
1091 bi->bi_used = 0;
1092 crypt_decode_inplace(bi->bi_state, bi->bi_buffer, bi->bi_avail);
1093 }
1094 n = size_todo;
1095 if (n > bi->bi_avail - bi->bi_used)
1096 n = bi->bi_avail - bi->bi_used;
1097 mch_memmove(p, bi->bi_buffer + bi->bi_used, n);
1098 bi->bi_used += n;
Bram Moolenaar9b8f0212014-08-13 22:05:53 +02001099 size_todo -= (int)n;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001100 p += n;
1101 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001102 }
Bram Moolenaar56f2db52017-06-11 23:09:15 +02001103 else
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001104#endif
1105 if (fread(buffer, (size_t)size, 1, bi->bi_fp) != 1)
Bram Moolenaar56f2db52017-06-11 23:09:15 +02001106 retval = FAIL;
1107
1108 if (retval == FAIL)
1109 /* Error may be checked for only later. Fill with zeros,
1110 * so that the reader won't use garbage. */
1111 vim_memset(buffer, 0, size);
1112 return retval;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001113}
1114
1115/*
1116 * Read a string of length "len" from "bi->bi_fd".
1117 * "len" can be zero to allocate an empty line.
1118 * Decrypt the bytes if needed.
1119 * Append a NUL.
1120 * Returns a pointer to allocated memory or NULL for failure.
1121 */
1122 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001123read_string_decrypt(bufinfo_T *bi, int len)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001124{
1125 char_u *ptr = alloc((unsigned)len + 1);
1126
1127 if (ptr != NULL)
1128 {
1129 if (len > 0 && undo_read(bi, ptr, len) == FAIL)
1130 {
1131 vim_free(ptr);
1132 return NULL;
1133 }
1134 ptr[len] = NUL;
1135#ifdef FEAT_CRYPT
1136 if (bi->bi_state != NULL && bi->bi_buffer == NULL)
1137 crypt_decode_inplace(bi->bi_state, ptr, len);
1138#endif
1139 }
1140 return ptr;
1141}
1142
1143/*
1144 * Writes the (not encrypted) header and initializes encryption if needed.
1145 */
1146 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001147serialize_header(bufinfo_T *bi, char_u *hash)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001148{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001149 int len;
1150 buf_T *buf = bi->bi_buf;
1151 FILE *fp = bi->bi_fp;
1152 char_u time_buf[8];
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001153
1154 /* Start writing, first the magic marker and undo info version. */
1155 if (fwrite(UF_START_MAGIC, (size_t)UF_START_MAGIC_LEN, (size_t)1, fp) != 1)
1156 return FAIL;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001157
1158 /* If the buffer is encrypted then all text bytes following will be
1159 * encrypted. Numbers and other info is not crypted. */
1160#ifdef FEAT_CRYPT
Bram Moolenaarfa0ff9a2010-07-25 16:05:19 +02001161 if (*buf->b_p_key != NUL)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001162 {
1163 char_u *header;
1164 int header_len;
1165
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001166 undo_write_bytes(bi, (long_u)UF_VERSION_CRYPT, 2);
1167 bi->bi_state = crypt_create_for_writing(crypt_get_method_nr(buf),
1168 buf->b_p_key, &header, &header_len);
1169 if (bi->bi_state == NULL)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001170 return FAIL;
Bram Moolenaarbbd6afe2010-06-02 20:32:23 +02001171 len = (int)fwrite(header, (size_t)header_len, (size_t)1, fp);
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001172 vim_free(header);
1173 if (len != 1)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001174 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001175 crypt_free_state(bi->bi_state);
1176 bi->bi_state = NULL;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001177 return FAIL;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001178 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001179
1180 if (crypt_whole_undofile(crypt_get_method_nr(buf)))
1181 {
1182 bi->bi_buffer = alloc(CRYPT_BUF_SIZE);
1183 if (bi->bi_buffer == NULL)
1184 {
1185 crypt_free_state(bi->bi_state);
1186 bi->bi_state = NULL;
1187 return FAIL;
1188 }
1189 bi->bi_used = 0;
1190 }
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001191 }
1192 else
1193#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001194 undo_write_bytes(bi, (long_u)UF_VERSION, 2);
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001195
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001196
1197 /* Write a hash of the buffer text, so that we can verify it is still the
1198 * same when reading the buffer text. */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001199 if (undo_write(bi, hash, (size_t)UNDO_HASH_SIZE) == FAIL)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001200 return FAIL;
1201
1202 /* buffer-specific data */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001203 undo_write_bytes(bi, (long_u)buf->b_ml.ml_line_count, 4);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001204 len = buf->b_u_line_ptr != NULL ? (int)STRLEN(buf->b_u_line_ptr) : 0;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001205 undo_write_bytes(bi, (long_u)len, 4);
1206 if (len > 0 && fwrite_crypt(bi, buf->b_u_line_ptr, (size_t)len) == FAIL)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001207 return FAIL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001208 undo_write_bytes(bi, (long_u)buf->b_u_line_lnum, 4);
1209 undo_write_bytes(bi, (long_u)buf->b_u_line_colnr, 4);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001210
1211 /* Undo structures header data */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001212 put_header_ptr(bi, buf->b_u_oldhead);
1213 put_header_ptr(bi, buf->b_u_newhead);
1214 put_header_ptr(bi, buf->b_u_curhead);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001215
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001216 undo_write_bytes(bi, (long_u)buf->b_u_numhead, 4);
1217 undo_write_bytes(bi, (long_u)buf->b_u_seq_last, 4);
1218 undo_write_bytes(bi, (long_u)buf->b_u_seq_cur, 4);
1219 time_to_bytes(buf->b_u_time_cur, time_buf);
1220 undo_write(bi, time_buf, 8);
Bram Moolenaara800b422010-06-27 01:15:55 +02001221
1222 /* Optional fields. */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001223 undo_write_bytes(bi, 4, 1);
1224 undo_write_bytes(bi, UF_LAST_SAVE_NR, 1);
1225 undo_write_bytes(bi, (long_u)buf->b_u_save_nr_last, 4);
Bram Moolenaara800b422010-06-27 01:15:55 +02001226
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001227 undo_write_bytes(bi, 0, 1); /* end marker */
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001228
1229 return OK;
1230}
1231
1232 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001233serialize_uhp(bufinfo_T *bi, u_header_T *uhp)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001234{
1235 int i;
1236 u_entry_T *uep;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001237 char_u time_buf[8];
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001238
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001239 if (undo_write_bytes(bi, (long_u)UF_HEADER_MAGIC, 2) == FAIL)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001240 return FAIL;
1241
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001242 put_header_ptr(bi, uhp->uh_next.ptr);
1243 put_header_ptr(bi, uhp->uh_prev.ptr);
1244 put_header_ptr(bi, uhp->uh_alt_next.ptr);
1245 put_header_ptr(bi, uhp->uh_alt_prev.ptr);
1246 undo_write_bytes(bi, uhp->uh_seq, 4);
1247 serialize_pos(bi, uhp->uh_cursor);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001248#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001249 undo_write_bytes(bi, (long_u)uhp->uh_cursor_vcol, 4);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001250#else
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001251 undo_write_bytes(bi, (long_u)0, 4);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001252#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001253 undo_write_bytes(bi, (long_u)uhp->uh_flags, 2);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001254 /* Assume NMARKS will stay the same. */
1255 for (i = 0; i < NMARKS; ++i)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001256 serialize_pos(bi, uhp->uh_namedm[i]);
1257 serialize_visualinfo(bi, &uhp->uh_visual);
1258 time_to_bytes(uhp->uh_time, time_buf);
1259 undo_write(bi, time_buf, 8);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001260
Bram Moolenaara800b422010-06-27 01:15:55 +02001261 /* Optional fields. */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001262 undo_write_bytes(bi, 4, 1);
1263 undo_write_bytes(bi, UHP_SAVE_NR, 1);
1264 undo_write_bytes(bi, (long_u)uhp->uh_save_nr, 4);
Bram Moolenaara800b422010-06-27 01:15:55 +02001265
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001266 undo_write_bytes(bi, 0, 1); /* end marker */
Bram Moolenaara800b422010-06-27 01:15:55 +02001267
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001268 /* Write all the entries. */
1269 for (uep = uhp->uh_entry; uep != NULL; uep = uep->ue_next)
1270 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001271 undo_write_bytes(bi, (long_u)UF_ENTRY_MAGIC, 2);
1272 if (serialize_uep(bi, uep) == FAIL)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001273 return FAIL;
1274 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001275 undo_write_bytes(bi, (long_u)UF_ENTRY_END_MAGIC, 2);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001276 return OK;
1277}
1278
1279 static u_header_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001280unserialize_uhp(bufinfo_T *bi, char_u *file_name)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001281{
1282 u_header_T *uhp;
1283 int i;
1284 u_entry_T *uep, *last_uep;
1285 int c;
1286 int error;
1287
1288 uhp = (u_header_T *)U_ALLOC_LINE(sizeof(u_header_T));
1289 if (uhp == NULL)
1290 return NULL;
1291 vim_memset(uhp, 0, sizeof(u_header_T));
1292#ifdef U_DEBUG
1293 uhp->uh_magic = UH_MAGIC;
1294#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001295 uhp->uh_next.seq = undo_read_4c(bi);
1296 uhp->uh_prev.seq = undo_read_4c(bi);
1297 uhp->uh_alt_next.seq = undo_read_4c(bi);
1298 uhp->uh_alt_prev.seq = undo_read_4c(bi);
1299 uhp->uh_seq = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001300 if (uhp->uh_seq <= 0)
1301 {
1302 corruption_error("uh_seq", file_name);
1303 vim_free(uhp);
1304 return NULL;
1305 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001306 unserialize_pos(bi, &uhp->uh_cursor);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001307#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001308 uhp->uh_cursor_vcol = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001309#else
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001310 (void)undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001311#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001312 uhp->uh_flags = undo_read_2c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001313 for (i = 0; i < NMARKS; ++i)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001314 unserialize_pos(bi, &uhp->uh_namedm[i]);
1315 unserialize_visualinfo(bi, &uhp->uh_visual);
1316 uhp->uh_time = undo_read_time(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001317
Bram Moolenaara800b422010-06-27 01:15:55 +02001318 /* Optional fields. */
Bram Moolenaar69154f22010-07-18 21:42:34 +02001319 for (;;)
1320 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001321 int len = undo_read_byte(bi);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001322 int what;
Bram Moolenaara800b422010-06-27 01:15:55 +02001323
Bram Moolenaar69154f22010-07-18 21:42:34 +02001324 if (len == 0)
1325 break;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001326 what = undo_read_byte(bi);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001327 switch (what)
1328 {
1329 case UHP_SAVE_NR:
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001330 uhp->uh_save_nr = undo_read_4c(bi);
Bram Moolenaara800b422010-06-27 01:15:55 +02001331 break;
Bram Moolenaar69154f22010-07-18 21:42:34 +02001332 default:
1333 /* field not supported, skip */
1334 while (--len >= 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001335 (void)undo_read_byte(bi);
Bram Moolenaara800b422010-06-27 01:15:55 +02001336 }
Bram Moolenaar69154f22010-07-18 21:42:34 +02001337 }
Bram Moolenaara800b422010-06-27 01:15:55 +02001338
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001339 /* Unserialize the uep list. */
1340 last_uep = NULL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001341 while ((c = undo_read_2c(bi)) == UF_ENTRY_MAGIC)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001342 {
1343 error = FALSE;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001344 uep = unserialize_uep(bi, &error, file_name);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001345 if (last_uep == NULL)
1346 uhp->uh_entry = uep;
1347 else
1348 last_uep->ue_next = uep;
1349 last_uep = uep;
1350 if (uep == NULL || error)
1351 {
1352 u_free_uhp(uhp);
1353 return NULL;
1354 }
1355 }
1356 if (c != UF_ENTRY_END_MAGIC)
1357 {
1358 corruption_error("entry end", file_name);
1359 u_free_uhp(uhp);
1360 return NULL;
1361 }
1362
1363 return uhp;
1364}
1365
Bram Moolenaar9db58062010-05-29 20:33:07 +02001366/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001367 * Serialize "uep".
Bram Moolenaar9db58062010-05-29 20:33:07 +02001368 */
1369 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001370serialize_uep(
1371 bufinfo_T *bi,
1372 u_entry_T *uep)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001373{
1374 int i;
1375 size_t len;
1376
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001377 undo_write_bytes(bi, (long_u)uep->ue_top, 4);
1378 undo_write_bytes(bi, (long_u)uep->ue_bot, 4);
1379 undo_write_bytes(bi, (long_u)uep->ue_lcount, 4);
1380 undo_write_bytes(bi, (long_u)uep->ue_size, 4);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001381 for (i = 0; i < uep->ue_size; ++i)
1382 {
1383 len = STRLEN(uep->ue_array[i]);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001384 if (undo_write_bytes(bi, (long_u)len, 4) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001385 return FAIL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001386 if (len > 0 && fwrite_crypt(bi, uep->ue_array[i], len) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001387 return FAIL;
1388 }
1389 return OK;
1390}
1391
1392 static u_entry_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001393unserialize_uep(bufinfo_T *bi, int *error, char_u *file_name)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001394{
1395 int i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001396 u_entry_T *uep;
Bram Moolenaar0c8485f2017-02-26 18:17:10 +01001397 char_u **array = NULL;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001398 char_u *line;
1399 int line_len;
1400
1401 uep = (u_entry_T *)U_ALLOC_LINE(sizeof(u_entry_T));
1402 if (uep == NULL)
1403 return NULL;
1404 vim_memset(uep, 0, sizeof(u_entry_T));
1405#ifdef U_DEBUG
1406 uep->ue_magic = UE_MAGIC;
1407#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001408 uep->ue_top = undo_read_4c(bi);
1409 uep->ue_bot = undo_read_4c(bi);
1410 uep->ue_lcount = undo_read_4c(bi);
1411 uep->ue_size = undo_read_4c(bi);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001412 if (uep->ue_size > 0)
1413 {
Bram Moolenaar0c8485f2017-02-26 18:17:10 +01001414 if (uep->ue_size < LONG_MAX / (int)sizeof(char_u *))
1415 array = (char_u **)U_ALLOC_LINE(sizeof(char_u *) * uep->ue_size);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001416 if (array == NULL)
1417 {
1418 *error = TRUE;
1419 return uep;
1420 }
1421 vim_memset(array, 0, sizeof(char_u *) * uep->ue_size);
1422 }
1423 uep->ue_array = array;
1424
1425 for (i = 0; i < uep->ue_size; ++i)
1426 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001427 line_len = undo_read_4c(bi);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001428 if (line_len >= 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001429 line = read_string_decrypt(bi, line_len);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001430 else
1431 {
1432 line = NULL;
1433 corruption_error("line length", file_name);
1434 }
1435 if (line == NULL)
1436 {
1437 *error = TRUE;
1438 return uep;
1439 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001440 array[i] = line;
1441 }
1442 return uep;
1443}
1444
1445/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001446 * Serialize "pos".
Bram Moolenaar9db58062010-05-29 20:33:07 +02001447 */
1448 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001449serialize_pos(bufinfo_T *bi, pos_T pos)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001450{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001451 undo_write_bytes(bi, (long_u)pos.lnum, 4);
1452 undo_write_bytes(bi, (long_u)pos.col, 4);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001453#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001454 undo_write_bytes(bi, (long_u)pos.coladd, 4);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001455#else
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001456 undo_write_bytes(bi, (long_u)0, 4);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001457#endif
1458}
1459
1460/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001461 * Unserialize the pos_T at the current position.
Bram Moolenaar9db58062010-05-29 20:33:07 +02001462 */
1463 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001464unserialize_pos(bufinfo_T *bi, pos_T *pos)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001465{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001466 pos->lnum = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001467 if (pos->lnum < 0)
1468 pos->lnum = 0;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001469 pos->col = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001470 if (pos->col < 0)
1471 pos->col = 0;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001472#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001473 pos->coladd = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001474 if (pos->coladd < 0)
1475 pos->coladd = 0;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001476#else
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001477 (void)undo_read_4c(bi);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001478#endif
1479}
1480
1481/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001482 * Serialize "info".
Bram Moolenaar9db58062010-05-29 20:33:07 +02001483 */
1484 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001485serialize_visualinfo(bufinfo_T *bi, visualinfo_T *info)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001486{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001487 serialize_pos(bi, info->vi_start);
1488 serialize_pos(bi, info->vi_end);
1489 undo_write_bytes(bi, (long_u)info->vi_mode, 4);
1490 undo_write_bytes(bi, (long_u)info->vi_curswant, 4);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001491}
1492
1493/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001494 * Unserialize the visualinfo_T at the current position.
Bram Moolenaar9db58062010-05-29 20:33:07 +02001495 */
1496 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001497unserialize_visualinfo(bufinfo_T *bi, visualinfo_T *info)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001498{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001499 unserialize_pos(bi, &info->vi_start);
1500 unserialize_pos(bi, &info->vi_end);
1501 info->vi_mode = undo_read_4c(bi);
1502 info->vi_curswant = undo_read_4c(bi);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001503}
1504
1505/*
1506 * Write the undo tree in an undo file.
1507 * When "name" is not NULL, use it as the name of the undo file.
1508 * Otherwise use buf->b_ffname to generate the undo file name.
1509 * "buf" must never be null, buf->b_ffname is used to obtain the original file
1510 * permissions.
1511 * "forceit" is TRUE for ":wundo!", FALSE otherwise.
1512 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text.
1513 */
1514 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001515u_write_undo(
1516 char_u *name,
1517 int forceit,
1518 buf_T *buf,
1519 char_u *hash)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001520{
1521 u_header_T *uhp;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001522 char_u *file_name;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001523 int mark;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001524#ifdef U_DEBUG
1525 int headers_written = 0;
1526#endif
1527 int fd;
1528 FILE *fp = NULL;
1529 int perm;
1530 int write_ok = FALSE;
1531#ifdef UNIX
1532 int st_old_valid = FALSE;
Bram Moolenaar8767f522016-07-01 17:17:39 +02001533 stat_T st_old;
1534 stat_T st_new;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001535#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001536 bufinfo_T bi;
1537
Bram Moolenaar5a669b92014-08-12 20:14:33 +02001538 vim_memset(&bi, 0, sizeof(bi));
Bram Moolenaar9db58062010-05-29 20:33:07 +02001539
1540 if (name == NULL)
1541 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001542 file_name = u_get_undo_file_name(buf->b_ffname, FALSE);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001543 if (file_name == NULL)
1544 {
1545 if (p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001546 {
1547 verbose_enter();
1548 smsg((char_u *)
1549 _("Cannot write undo file in any directory in 'undodir'"));
1550 verbose_leave();
1551 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001552 return;
1553 }
1554 }
1555 else
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001556 file_name = name;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001557
1558 /*
1559 * Decide about the permission to use for the undo file. If the buffer
1560 * has a name use the permission of the original file. Otherwise only
1561 * allow the user to access the undo file.
1562 */
1563 perm = 0600;
1564 if (buf->b_ffname != NULL)
1565 {
1566#ifdef UNIX
1567 if (mch_stat((char *)buf->b_ffname, &st_old) >= 0)
1568 {
1569 perm = st_old.st_mode;
1570 st_old_valid = TRUE;
1571 }
1572#else
1573 perm = mch_getperm(buf->b_ffname);
1574 if (perm < 0)
1575 perm = 0600;
1576#endif
1577 }
1578
Bram Moolenaar3cbac302015-04-21 16:12:06 +02001579 /* strip any s-bit and executable bit */
1580 perm = perm & 0666;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001581
1582 /* If the undo file already exists, verify that it actually is an undo
1583 * file, and delete it. */
1584 if (mch_getperm(file_name) >= 0)
1585 {
1586 if (name == NULL || !forceit)
1587 {
1588 /* Check we can read it and it's an undo file. */
1589 fd = mch_open((char *)file_name, O_RDONLY|O_EXTRA, 0);
1590 if (fd < 0)
1591 {
1592 if (name != NULL || p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001593 {
1594 if (name == NULL)
1595 verbose_enter();
1596 smsg((char_u *)
1597 _("Will not overwrite with undo file, cannot read: %s"),
Bram Moolenaar9db58062010-05-29 20:33:07 +02001598 file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001599 if (name == NULL)
1600 verbose_leave();
1601 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001602 goto theend;
1603 }
1604 else
1605 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02001606 char_u mbuf[UF_START_MAGIC_LEN];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001607 int len;
1608
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01001609 len = read_eintr(fd, mbuf, UF_START_MAGIC_LEN);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001610 close(fd);
1611 if (len < UF_START_MAGIC_LEN
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02001612 || memcmp(mbuf, UF_START_MAGIC, UF_START_MAGIC_LEN) != 0)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001613 {
1614 if (name != NULL || p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001615 {
1616 if (name == NULL)
1617 verbose_enter();
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001618 smsg((char_u *)
1619 _("Will not overwrite, this is not an undo file: %s"),
Bram Moolenaar9db58062010-05-29 20:33:07 +02001620 file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001621 if (name == NULL)
1622 verbose_leave();
1623 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001624 goto theend;
1625 }
1626 }
1627 }
1628 mch_remove(file_name);
1629 }
1630
Bram Moolenaar504a8212010-05-30 17:17:42 +02001631 /* If there is no undo information at all, quit here after deleting any
1632 * existing undo file. */
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001633 if (buf->b_u_numhead == 0 && buf->b_u_line_ptr == NULL)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001634 {
1635 if (p_verbose > 0)
Bram Moolenaar97ea5112010-06-12 06:46:44 +02001636 verb_msg((char_u *)_("Skipping undo file write, nothing to undo"));
Bram Moolenaar504a8212010-05-30 17:17:42 +02001637 goto theend;
1638 }
1639
Bram Moolenaar9db58062010-05-29 20:33:07 +02001640 fd = mch_open((char *)file_name,
1641 O_CREAT|O_EXTRA|O_WRONLY|O_EXCL|O_NOFOLLOW, perm);
1642 if (fd < 0)
1643 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001644 EMSG2(_(e_not_open), file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001645 goto theend;
1646 }
1647 (void)mch_setperm(file_name, perm);
1648 if (p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001649 {
1650 verbose_enter();
Bram Moolenaar9db58062010-05-29 20:33:07 +02001651 smsg((char_u *)_("Writing undo file: %s"), file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001652 verbose_leave();
1653 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001654
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001655#ifdef U_DEBUG
Bram Moolenaar504a8212010-05-30 17:17:42 +02001656 /* Check there is no problem in undo info before writing. */
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001657 u_check(FALSE);
1658#endif
1659
Bram Moolenaar9db58062010-05-29 20:33:07 +02001660#ifdef UNIX
1661 /*
1662 * Try to set the group of the undo file same as the original file. If
1663 * this fails, set the protection bits for the group same as the
1664 * protection bits for others.
1665 */
Bram Moolenaarce69e822010-07-21 20:31:07 +02001666 if (st_old_valid
1667 && mch_stat((char *)file_name, &st_new) >= 0
1668 && st_new.st_gid != st_old.st_gid
Bram Moolenaar9db58062010-05-29 20:33:07 +02001669# ifdef HAVE_FCHOWN /* sequent-ptx lacks fchown() */
Bram Moolenaarce69e822010-07-21 20:31:07 +02001670 && fchown(fd, (uid_t)-1, st_old.st_gid) != 0
Bram Moolenaar9db58062010-05-29 20:33:07 +02001671# endif
1672 )
1673 mch_setperm(file_name, (perm & 0707) | ((perm & 07) << 3));
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02001674# if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001675 if (buf->b_ffname != NULL)
1676 mch_copy_sec(buf->b_ffname, file_name);
1677# endif
1678#endif
1679
1680 fp = fdopen(fd, "w");
1681 if (fp == NULL)
1682 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001683 EMSG2(_(e_not_open), file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001684 close(fd);
1685 mch_remove(file_name);
1686 goto theend;
1687 }
1688
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001689 /* Undo must be synced. */
1690 u_sync(TRUE);
1691
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001692 /*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001693 * Write the header. Initializes encryption, if enabled.
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001694 */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001695 bi.bi_buf = buf;
1696 bi.bi_fp = fp;
1697 if (serialize_header(&bi, hash) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001698 goto write_error;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001699
1700 /*
1701 * Iteratively serialize UHPs and their UEPs from the top down.
1702 */
1703 mark = ++lastmark;
1704 uhp = buf->b_u_oldhead;
1705 while (uhp != NULL)
1706 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001707 /* Serialize current UHP if we haven't seen it */
1708 if (uhp->uh_walk != mark)
1709 {
1710 uhp->uh_walk = mark;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001711#ifdef U_DEBUG
1712 ++headers_written;
1713#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001714 if (serialize_uhp(&bi, uhp) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001715 goto write_error;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001716 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001717
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001718 /* Now walk through the tree - algorithm from undo_time(). */
1719 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != mark)
1720 uhp = uhp->uh_prev.ptr;
1721 else if (uhp->uh_alt_next.ptr != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001722 && uhp->uh_alt_next.ptr->uh_walk != mark)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001723 uhp = uhp->uh_alt_next.ptr;
1724 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001725 && uhp->uh_next.ptr->uh_walk != mark)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001726 uhp = uhp->uh_next.ptr;
1727 else if (uhp->uh_alt_prev.ptr != NULL)
1728 uhp = uhp->uh_alt_prev.ptr;
1729 else
1730 uhp = uhp->uh_next.ptr;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001731 }
1732
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001733 if (undo_write_bytes(&bi, (long_u)UF_HEADER_END_MAGIC, 2) == OK)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001734 write_ok = TRUE;
1735#ifdef U_DEBUG
1736 if (headers_written != buf->b_u_numhead)
Bram Moolenaar570064c2013-06-10 20:25:10 +02001737 {
1738 EMSGN("Written %ld headers, ...", headers_written);
1739 EMSGN("... but numhead is %ld", buf->b_u_numhead);
1740 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001741#endif
1742
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001743#ifdef FEAT_CRYPT
1744 if (bi.bi_state != NULL && undo_flush(&bi) == FAIL)
1745 write_ok = FALSE;
1746#endif
1747
Bram Moolenaar9db58062010-05-29 20:33:07 +02001748write_error:
1749 fclose(fp);
1750 if (!write_ok)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001751 EMSG2(_("E829: write error in undo file: %s"), file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001752
Bram Moolenaard0573012017-10-28 21:11:06 +02001753#if defined(WIN3264)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001754 /* Copy file attributes; for systems where this can only be done after
1755 * closing the file. */
Bram Moolenaar9db58062010-05-29 20:33:07 +02001756 if (buf->b_ffname != NULL)
1757 (void)mch_copy_file_attribute(buf->b_ffname, file_name);
1758#endif
1759#ifdef HAVE_ACL
1760 if (buf->b_ffname != NULL)
1761 {
1762 vim_acl_T acl;
1763
1764 /* For systems that support ACL: get the ACL from the original file. */
1765 acl = mch_get_acl(buf->b_ffname);
1766 mch_set_acl(file_name, acl);
Bram Moolenaard2aed442012-06-01 13:46:12 +02001767 mch_free_acl(acl);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001768 }
1769#endif
1770
1771theend:
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001772#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001773 if (bi.bi_state != NULL)
1774 crypt_free_state(bi.bi_state);
1775 vim_free(bi.bi_buffer);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001776#endif
Bram Moolenaar9db58062010-05-29 20:33:07 +02001777 if (file_name != name)
1778 vim_free(file_name);
1779}
1780
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001781/*
1782 * Load the undo tree from an undo file.
1783 * If "name" is not NULL use it as the undo file name. This also means being
1784 * a bit more verbose.
1785 * Otherwise use curbuf->b_ffname to generate the undo file name.
1786 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text.
1787 */
1788 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001789u_read_undo(char_u *name, char_u *hash, char_u *orig_name)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001790{
1791 char_u *file_name;
1792 FILE *fp;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001793 long version, str_len;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001794 char_u *line_ptr = NULL;
1795 linenr_T line_lnum;
1796 colnr_T line_colnr;
1797 linenr_T line_count;
Bram Moolenaar3eb16372017-02-26 18:11:36 +01001798 long num_head = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001799 long old_header_seq, new_header_seq, cur_header_seq;
1800 long seq_last, seq_cur;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001801 long last_save_nr = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001802 short old_idx = -1, new_idx = -1, cur_idx = -1;
1803 long num_read_uhps = 0;
1804 time_t seq_time;
1805 int i, j;
1806 int c;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001807 u_header_T *uhp;
1808 u_header_T **uhp_table = NULL;
1809 char_u read_hash[UNDO_HASH_SIZE];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001810 char_u magic_buf[UF_START_MAGIC_LEN];
1811#ifdef U_DEBUG
1812 int *uhp_table_used;
1813#endif
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001814#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001815 stat_T st_orig;
1816 stat_T st_undo;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001817#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001818 bufinfo_T bi;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001819
Bram Moolenaar5a669b92014-08-12 20:14:33 +02001820 vim_memset(&bi, 0, sizeof(bi));
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001821 if (name == NULL)
1822 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001823 file_name = u_get_undo_file_name(curbuf->b_ffname, TRUE);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001824 if (file_name == NULL)
1825 return;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001826
1827#ifdef UNIX
1828 /* For safety we only read an undo file if the owner is equal to the
Bram Moolenaar3b262392013-09-08 15:40:49 +02001829 * owner of the text file or equal to the current user. */
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001830 if (mch_stat((char *)orig_name, &st_orig) >= 0
1831 && mch_stat((char *)file_name, &st_undo) >= 0
Bram Moolenaar3b262392013-09-08 15:40:49 +02001832 && st_orig.st_uid != st_undo.st_uid
1833 && st_undo.st_uid != getuid())
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001834 {
1835 if (p_verbose > 0)
1836 {
1837 verbose_enter();
1838 smsg((char_u *)_("Not reading undo file, owner differs: %s"),
1839 file_name);
1840 verbose_leave();
1841 }
1842 return;
1843 }
1844#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001845 }
1846 else
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001847 file_name = name;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001848
1849 if (p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001850 {
1851 verbose_enter();
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001852 smsg((char_u *)_("Reading undo file: %s"), file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001853 verbose_leave();
1854 }
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001855
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001856 fp = mch_fopen((char *)file_name, "r");
1857 if (fp == NULL)
1858 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001859 if (name != NULL || p_verbose > 0)
1860 EMSG2(_("E822: Cannot open undo file for reading: %s"), file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001861 goto error;
1862 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001863 bi.bi_buf = curbuf;
1864 bi.bi_fp = fp;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001865
Bram Moolenaar9db58062010-05-29 20:33:07 +02001866 /*
1867 * Read the undo file header.
1868 */
1869 if (fread(magic_buf, UF_START_MAGIC_LEN, 1, fp) != 1
1870 || memcmp(magic_buf, UF_START_MAGIC, UF_START_MAGIC_LEN) != 0)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001871 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001872 EMSG2(_("E823: Not an undo file: %s"), file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001873 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001874 }
1875 version = get2c(fp);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001876 if (version == UF_VERSION_CRYPT)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001877 {
1878#ifdef FEAT_CRYPT
Bram Moolenaar56be9502010-06-06 14:20:26 +02001879 if (*curbuf->b_p_key == NUL)
1880 {
1881 EMSG2(_("E832: Non-encrypted file has encrypted undo file: %s"),
1882 file_name);
1883 goto error;
1884 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001885 bi.bi_state = crypt_create_from_file(fp, curbuf->b_p_key);
1886 if (bi.bi_state == NULL)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001887 {
1888 EMSG2(_("E826: Undo file decryption failed: %s"), file_name);
1889 goto error;
1890 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001891 if (crypt_whole_undofile(bi.bi_state->method_nr))
1892 {
1893 bi.bi_buffer = alloc(CRYPT_BUF_SIZE);
1894 if (bi.bi_buffer == NULL)
1895 {
1896 crypt_free_state(bi.bi_state);
1897 bi.bi_state = NULL;
1898 goto error;
1899 }
1900 bi.bi_avail = 0;
1901 bi.bi_used = 0;
1902 }
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001903#else
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001904 EMSG2(_("E827: Undo file is encrypted: %s"), file_name);
1905 goto error;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001906#endif
1907 }
Bram Moolenaar69154f22010-07-18 21:42:34 +02001908 else if (version != UF_VERSION)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001909 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001910 EMSG2(_("E824: Incompatible undo file: %s"), file_name);
1911 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001912 }
1913
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001914 if (undo_read(&bi, read_hash, (size_t)UNDO_HASH_SIZE) == FAIL)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001915 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001916 corruption_error("hash", file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001917 goto error;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001918 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001919 line_count = (linenr_T)undo_read_4c(&bi);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001920 if (memcmp(hash, read_hash, UNDO_HASH_SIZE) != 0
1921 || line_count != curbuf->b_ml.ml_line_count)
1922 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001923 if (p_verbose > 0 || name != NULL)
1924 {
Bram Moolenaar504a8212010-05-30 17:17:42 +02001925 if (name == NULL)
1926 verbose_enter();
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001927 give_warning((char_u *)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001928 _("File contents changed, cannot use undo info"), TRUE);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001929 if (name == NULL)
1930 verbose_leave();
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001931 }
1932 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001933 }
1934
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001935 /* Read undo data for "U" command. */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001936 str_len = undo_read_4c(&bi);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001937 if (str_len < 0)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001938 goto error;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001939 if (str_len > 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001940 line_ptr = read_string_decrypt(&bi, str_len);
1941 line_lnum = (linenr_T)undo_read_4c(&bi);
1942 line_colnr = (colnr_T)undo_read_4c(&bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001943 if (line_lnum < 0 || line_colnr < 0)
1944 {
1945 corruption_error("line lnum/col", file_name);
1946 goto error;
1947 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001948
1949 /* Begin general undo data */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001950 old_header_seq = undo_read_4c(&bi);
1951 new_header_seq = undo_read_4c(&bi);
1952 cur_header_seq = undo_read_4c(&bi);
1953 num_head = undo_read_4c(&bi);
1954 seq_last = undo_read_4c(&bi);
1955 seq_cur = undo_read_4c(&bi);
1956 seq_time = undo_read_time(&bi);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001957
Bram Moolenaar69154f22010-07-18 21:42:34 +02001958 /* Optional header fields. */
1959 for (;;)
1960 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001961 int len = undo_read_byte(&bi);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001962 int what;
Bram Moolenaara800b422010-06-27 01:15:55 +02001963
Bram Moolenaar69154f22010-07-18 21:42:34 +02001964 if (len == 0 || len == EOF)
1965 break;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001966 what = undo_read_byte(&bi);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001967 switch (what)
1968 {
1969 case UF_LAST_SAVE_NR:
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001970 last_save_nr = undo_read_4c(&bi);
Bram Moolenaara800b422010-06-27 01:15:55 +02001971 break;
Bram Moolenaar69154f22010-07-18 21:42:34 +02001972 default:
1973 /* field not supported, skip */
1974 while (--len >= 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001975 (void)undo_read_byte(&bi);
Bram Moolenaara800b422010-06-27 01:15:55 +02001976 }
Bram Moolenaar69154f22010-07-18 21:42:34 +02001977 }
Bram Moolenaara800b422010-06-27 01:15:55 +02001978
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001979 /* uhp_table will store the freshly created undo headers we allocate
1980 * until we insert them into curbuf. The table remains sorted by the
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001981 * sequence numbers of the headers.
1982 * When there are no headers uhp_table is NULL. */
1983 if (num_head > 0)
1984 {
Bram Moolenaar3eb16372017-02-26 18:11:36 +01001985 if (num_head < LONG_MAX / (long)sizeof(u_header_T *))
1986 uhp_table = (u_header_T **)U_ALLOC_LINE(
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001987 num_head * sizeof(u_header_T *));
1988 if (uhp_table == NULL)
1989 goto error;
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001990 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001991
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001992 while ((c = undo_read_2c(&bi)) == UF_HEADER_MAGIC)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001993 {
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001994 if (num_read_uhps >= num_head)
1995 {
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001996 corruption_error("num_head too small", file_name);
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001997 goto error;
1998 }
1999
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002000 uhp = unserialize_uhp(&bi, file_name);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002001 if (uhp == NULL)
2002 goto error;
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02002003 uhp_table[num_read_uhps++] = uhp;
2004 }
2005
2006 if (num_read_uhps != num_head)
2007 {
2008 corruption_error("num_head", file_name);
2009 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002010 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02002011 if (c != UF_HEADER_END_MAGIC)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002012 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02002013 corruption_error("end marker", file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002014 goto error;
2015 }
2016
Bram Moolenaar9db58062010-05-29 20:33:07 +02002017#ifdef U_DEBUG
2018 uhp_table_used = (int *)alloc_clear(
2019 (unsigned)(sizeof(int) * num_head + 1));
2020# define SET_FLAG(j) ++uhp_table_used[j]
2021#else
2022# define SET_FLAG(j)
2023#endif
2024
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002025 /* We have put all of the headers into a table. Now we iterate through the
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002026 * table and swizzle each sequence number we have stored in uh_*_seq into
2027 * a pointer corresponding to the header with that sequence number. */
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002028 for (i = 0; i < num_head; i++)
2029 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002030 uhp = uhp_table[i];
2031 if (uhp == NULL)
2032 continue;
2033 for (j = 0; j < num_head; j++)
2034 if (uhp_table[j] != NULL && i != j
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002035 && uhp_table[i]->uh_seq == uhp_table[j]->uh_seq)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002036 {
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02002037 corruption_error("duplicate uh_seq", file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002038 goto error;
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02002039 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002040 for (j = 0; j < num_head; j++)
2041 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002042 && uhp_table[j]->uh_seq == uhp->uh_next.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002043 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002044 uhp->uh_next.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02002045 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002046 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002047 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002048 for (j = 0; j < num_head; j++)
2049 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002050 && uhp_table[j]->uh_seq == uhp->uh_prev.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002051 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002052 uhp->uh_prev.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02002053 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002054 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002055 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002056 for (j = 0; j < num_head; j++)
2057 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002058 && uhp_table[j]->uh_seq == uhp->uh_alt_next.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002059 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002060 uhp->uh_alt_next.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02002061 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002062 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002063 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002064 for (j = 0; j < num_head; j++)
2065 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002066 && uhp_table[j]->uh_seq == uhp->uh_alt_prev.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002067 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002068 uhp->uh_alt_prev.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02002069 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002070 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002071 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002072 if (old_header_seq > 0 && old_idx < 0 && uhp->uh_seq == old_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002073 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002074 old_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002075 SET_FLAG(i);
2076 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002077 if (new_header_seq > 0 && new_idx < 0 && uhp->uh_seq == new_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002078 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002079 new_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002080 SET_FLAG(i);
2081 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002082 if (cur_header_seq > 0 && cur_idx < 0 && uhp->uh_seq == cur_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002083 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002084 cur_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002085 SET_FLAG(i);
2086 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002087 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02002088
2089 /* Now that we have read the undo info successfully, free the current undo
2090 * info and use the info from the file. */
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002091 u_blockfree(curbuf);
Bram Moolenaar9db58062010-05-29 20:33:07 +02002092 curbuf->b_u_oldhead = old_idx < 0 ? NULL : uhp_table[old_idx];
2093 curbuf->b_u_newhead = new_idx < 0 ? NULL : uhp_table[new_idx];
2094 curbuf->b_u_curhead = cur_idx < 0 ? NULL : uhp_table[cur_idx];
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002095 curbuf->b_u_line_ptr = line_ptr;
2096 curbuf->b_u_line_lnum = line_lnum;
2097 curbuf->b_u_line_colnr = line_colnr;
2098 curbuf->b_u_numhead = num_head;
2099 curbuf->b_u_seq_last = seq_last;
2100 curbuf->b_u_seq_cur = seq_cur;
Bram Moolenaara800b422010-06-27 01:15:55 +02002101 curbuf->b_u_time_cur = seq_time;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002102 curbuf->b_u_save_nr_last = last_save_nr;
Bram Moolenaardba01a02010-11-03 19:32:42 +01002103 curbuf->b_u_save_nr_cur = last_save_nr;
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02002104
2105 curbuf->b_u_synced = TRUE;
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002106 vim_free(uhp_table);
Bram Moolenaar9db58062010-05-29 20:33:07 +02002107
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002108#ifdef U_DEBUG
Bram Moolenaar9db58062010-05-29 20:33:07 +02002109 for (i = 0; i < num_head; ++i)
2110 if (uhp_table_used[i] == 0)
2111 EMSGN("uhp_table entry %ld not used, leaking memory", i);
2112 vim_free(uhp_table_used);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002113 u_check(TRUE);
2114#endif
Bram Moolenaar9db58062010-05-29 20:33:07 +02002115
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002116 if (name != NULL)
2117 smsg((char_u *)_("Finished reading undo file %s"), file_name);
2118 goto theend;
2119
2120error:
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002121 vim_free(line_ptr);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002122 if (uhp_table != NULL)
2123 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002124 for (i = 0; i < num_read_uhps; i++)
2125 if (uhp_table[i] != NULL)
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02002126 u_free_uhp(uhp_table[i]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002127 vim_free(uhp_table);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002128 }
2129
2130theend:
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002131#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002132 if (bi.bi_state != NULL)
2133 crypt_free_state(bi.bi_state);
2134 vim_free(bi.bi_buffer);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002135#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002136 if (fp != NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002137 fclose(fp);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002138 if (file_name != name)
2139 vim_free(file_name);
2140 return;
2141}
2142
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002143#endif /* FEAT_PERSISTENT_UNDO */
2144
2145
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146/*
2147 * If 'cpoptions' contains 'u': Undo the previous undo or redo (vi compatible).
2148 * If 'cpoptions' does not contain 'u': Always undo.
2149 */
2150 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002151u_undo(int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152{
2153 /*
2154 * If we get an undo command while executing a macro, we behave like the
2155 * original vi. If this happens twice in one macro the result will not
2156 * be compatible.
2157 */
2158 if (curbuf->b_u_synced == FALSE)
2159 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002160 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 count = 1;
2162 }
2163
2164 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
2165 undo_undoes = TRUE;
2166 else
2167 undo_undoes = !undo_undoes;
2168 u_doit(count);
2169}
2170
2171/*
2172 * If 'cpoptions' contains 'u': Repeat the previous undo or redo.
2173 * If 'cpoptions' does not contain 'u': Always redo.
2174 */
2175 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002176u_redo(int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177{
2178 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
2179 undo_undoes = FALSE;
2180 u_doit(count);
2181}
2182
2183/*
2184 * Undo or redo, depending on 'undo_undoes', 'count' times.
2185 */
2186 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002187u_doit(int startcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188{
Bram Moolenaarca003e12006-03-17 23:19:38 +00002189 int count = startcount;
2190
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002191 if (!undo_allowed())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193
2194 u_newcount = 0;
2195 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002196 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2197 u_oldcount = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 while (count--)
2199 {
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002200 /* Do the change warning now, so that it triggers FileChangedRO when
2201 * needed. This may cause the file to be reloaded, that must happen
2202 * before we do anything, because it may change curbuf->b_u_curhead
2203 * and more. */
2204 change_warning(0);
2205
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 if (undo_undoes)
2207 {
2208 if (curbuf->b_u_curhead == NULL) /* first undo */
2209 curbuf->b_u_curhead = curbuf->b_u_newhead;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002210 else if (get_undolevel() > 0) /* multi level undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 /* get next undo */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002212 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_next.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 /* nothing to undo */
2214 if (curbuf->b_u_numhead == 0 || curbuf->b_u_curhead == NULL)
2215 {
2216 /* stick curbuf->b_u_curhead at end */
2217 curbuf->b_u_curhead = curbuf->b_u_oldhead;
2218 beep_flush();
Bram Moolenaarca003e12006-03-17 23:19:38 +00002219 if (count == startcount - 1)
2220 {
2221 MSG(_("Already at oldest change"));
2222 return;
2223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 break;
2225 }
2226
Bram Moolenaarca003e12006-03-17 23:19:38 +00002227 u_undoredo(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 }
2229 else
2230 {
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002231 if (curbuf->b_u_curhead == NULL || get_undolevel() <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 {
2233 beep_flush(); /* nothing to redo */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002234 if (count == startcount - 1)
2235 {
2236 MSG(_("Already at newest change"));
2237 return;
2238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 break;
2240 }
2241
Bram Moolenaarca003e12006-03-17 23:19:38 +00002242 u_undoredo(FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002243
2244 /* Advance for next redo. Set "newhead" when at the end of the
2245 * redoable changes. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002246 if (curbuf->b_u_curhead->uh_prev.ptr == NULL)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002247 curbuf->b_u_newhead = curbuf->b_u_curhead;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002248 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_prev.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 }
2250 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00002251 u_undo_end(undo_undoes, FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002252}
2253
Bram Moolenaar1e607892006-03-13 22:15:53 +00002254/*
2255 * Undo or redo over the timeline.
2256 * When "step" is negative go back in time, otherwise goes forward in time.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002257 * When "sec" is FALSE make "step" steps, when "sec" is TRUE use "step" as
2258 * seconds.
Bram Moolenaar730cde92010-06-27 05:18:54 +02002259 * When "file" is TRUE use "step" as a number of file writes.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002260 * When "absolute" is TRUE use "step" as the sequence number to jump to.
2261 * "sec" must be FALSE then.
Bram Moolenaar1e607892006-03-13 22:15:53 +00002262 */
2263 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002264undo_time(
2265 long step,
2266 int sec,
2267 int file,
2268 int absolute)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002269{
2270 long target;
2271 long closest;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002272 long closest_start;
2273 long closest_seq = 0;
2274 long val;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002275 u_header_T *uhp;
2276 u_header_T *last;
2277 int mark;
2278 int nomark;
2279 int round;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002280 int dosec = sec;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002281 int dofile = file;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002282 int above = FALSE;
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002283 int did_undo = TRUE;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002284
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002285 /* First make sure the current undoable change is synced. */
2286 if (curbuf->b_u_synced == FALSE)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002287 u_sync(TRUE);
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002288
Bram Moolenaar1e607892006-03-13 22:15:53 +00002289 u_newcount = 0;
2290 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002291 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002292 u_oldcount = -1;
2293
Bram Moolenaarca003e12006-03-17 23:19:38 +00002294 /* "target" is the node below which we want to be.
2295 * Init "closest" to a value we can't reach. */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002296 if (absolute)
2297 {
Bram Moolenaard22e9462016-03-15 17:43:55 +01002298 if (step == 0)
2299 {
2300 /* target 0 does not exist, got to 1 and above it. */
2301 target = 1;
2302 above = TRUE;
2303 }
2304 else
2305 target = step;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002306 closest = -1;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002307 }
Bram Moolenaarca003e12006-03-17 23:19:38 +00002308 else
Bram Moolenaar1e607892006-03-13 22:15:53 +00002309 {
Bram Moolenaar730cde92010-06-27 05:18:54 +02002310 if (dosec)
Bram Moolenaarcbd4de42017-01-07 16:14:57 +01002311 target = (long)(curbuf->b_u_time_cur) + step;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002312 else if (dofile)
2313 {
2314 if (step < 0)
2315 {
2316 /* Going back to a previous write. If there were changes after
2317 * the last write, count that as moving one file-write, so
2318 * that ":earlier 1f" undoes all changes since the last save. */
2319 uhp = curbuf->b_u_curhead;
2320 if (uhp != NULL)
2321 uhp = uhp->uh_next.ptr;
2322 else
2323 uhp = curbuf->b_u_newhead;
2324 if (uhp != NULL && uhp->uh_save_nr != 0)
2325 /* "uh_save_nr" was set in the last block, that means
2326 * there were no changes since the last write */
2327 target = curbuf->b_u_save_nr_cur + step;
2328 else
2329 /* count the changes since the last write as one step */
2330 target = curbuf->b_u_save_nr_cur + step + 1;
2331 if (target <= 0)
2332 /* Go to before first write: before the oldest change. Use
2333 * the sequence number for that. */
2334 dofile = FALSE;
2335 }
2336 else
2337 {
2338 /* Moving forward to a newer write. */
2339 target = curbuf->b_u_save_nr_cur + step;
2340 if (target > curbuf->b_u_save_nr_last)
2341 {
2342 /* Go to after last write: after the latest change. Use
2343 * the sequence number for that. */
2344 target = curbuf->b_u_seq_last + 1;
2345 dofile = FALSE;
2346 }
2347 }
2348 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002349 else
2350 target = curbuf->b_u_seq_cur + step;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002351 if (step < 0)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002352 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002353 if (target < 0)
2354 target = 0;
2355 closest = -1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002356 }
2357 else
2358 {
Bram Moolenaar730cde92010-06-27 05:18:54 +02002359 if (dosec)
Bram Moolenaarcbd4de42017-01-07 16:14:57 +01002360 closest = (long)(vim_time() + 1);
Bram Moolenaar730cde92010-06-27 05:18:54 +02002361 else if (dofile)
2362 closest = curbuf->b_u_save_nr_last + 2;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002363 else
2364 closest = curbuf->b_u_seq_last + 2;
2365 if (target >= closest)
2366 target = closest - 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002367 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002368 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002369 closest_start = closest;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002370 closest_seq = curbuf->b_u_seq_cur;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002371
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002372 /*
2373 * May do this twice:
Bram Moolenaar1e607892006-03-13 22:15:53 +00002374 * 1. Search for "target", update "closest" to the best match found.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002375 * 2. If "target" not found search for "closest".
2376 *
2377 * When using the closest time we use the sequence number in the second
2378 * round, because there may be several entries with the same time.
2379 */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002380 for (round = 1; round <= 2; ++round)
2381 {
2382 /* Find the path from the current state to where we want to go. The
2383 * desired state can be anywhere in the undo tree, need to go all over
2384 * it. We put "nomark" in uh_walk where we have been without success,
2385 * "mark" where it could possibly be. */
2386 mark = ++lastmark;
2387 nomark = ++lastmark;
2388
2389 if (curbuf->b_u_curhead == NULL) /* at leaf of the tree */
2390 uhp = curbuf->b_u_newhead;
2391 else
2392 uhp = curbuf->b_u_curhead;
2393
2394 while (uhp != NULL)
2395 {
2396 uhp->uh_walk = mark;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002397 if (dosec)
Bram Moolenaarcbd4de42017-01-07 16:14:57 +01002398 val = (long)(uhp->uh_time);
Bram Moolenaar730cde92010-06-27 05:18:54 +02002399 else if (dofile)
2400 val = uhp->uh_save_nr;
2401 else
2402 val = uhp->uh_seq;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002403
Bram Moolenaar730cde92010-06-27 05:18:54 +02002404 if (round == 1 && !(dofile && val == 0))
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002405 {
2406 /* Remember the header that is closest to the target.
2407 * It must be at least in the right direction (checked with
Bram Moolenaarca003e12006-03-17 23:19:38 +00002408 * "b_u_seq_cur"). When the timestamp is equal find the
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002409 * highest/lowest sequence number. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002410 if ((step < 0 ? uhp->uh_seq <= curbuf->b_u_seq_cur
2411 : uhp->uh_seq > curbuf->b_u_seq_cur)
2412 && ((dosec && val == closest)
2413 ? (step < 0
2414 ? uhp->uh_seq < closest_seq
2415 : uhp->uh_seq > closest_seq)
2416 : closest == closest_start
2417 || (val > target
2418 ? (closest > target
2419 ? val - target <= closest - target
2420 : val - target <= target - closest)
2421 : (closest > target
2422 ? target - val <= closest - target
2423 : target - val <= target - closest))))
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002424 {
2425 closest = val;
2426 closest_seq = uhp->uh_seq;
2427 }
2428 }
2429
2430 /* Quit searching when we found a match. But when searching for a
2431 * time we need to continue looking for the best uh_seq. */
2432 if (target == val && !dosec)
Bram Moolenaar730cde92010-06-27 05:18:54 +02002433 {
2434 target = uhp->uh_seq;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002435 break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002436 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002437
2438 /* go down in the tree if we haven't been there */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002439 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != nomark
2440 && uhp->uh_prev.ptr->uh_walk != mark)
2441 uhp = uhp->uh_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002442
2443 /* go to alternate branch if we haven't been there */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002444 else if (uhp->uh_alt_next.ptr != NULL
2445 && uhp->uh_alt_next.ptr->uh_walk != nomark
2446 && uhp->uh_alt_next.ptr->uh_walk != mark)
2447 uhp = uhp->uh_alt_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002448
2449 /* go up in the tree if we haven't been there and we are at the
2450 * start of alternate branches */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002451 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
2452 && uhp->uh_next.ptr->uh_walk != nomark
2453 && uhp->uh_next.ptr->uh_walk != mark)
Bram Moolenaardb552d602006-03-23 22:59:57 +00002454 {
2455 /* If still at the start we don't go through this change. */
2456 if (uhp == curbuf->b_u_curhead)
2457 uhp->uh_walk = nomark;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002458 uhp = uhp->uh_next.ptr;
Bram Moolenaardb552d602006-03-23 22:59:57 +00002459 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002460
2461 else
2462 {
2463 /* need to backtrack; mark this node as useless */
2464 uhp->uh_walk = nomark;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002465 if (uhp->uh_alt_prev.ptr != NULL)
2466 uhp = uhp->uh_alt_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002467 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002468 uhp = uhp->uh_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002469 }
2470 }
2471
2472 if (uhp != NULL) /* found it */
2473 break;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002474
2475 if (absolute)
2476 {
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002477 EMSGN(_("E830: Undo number %ld not found"), step);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002478 return;
2479 }
2480
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002481 if (closest == closest_start)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002482 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002483 if (step < 0)
2484 MSG(_("Already at oldest change"));
2485 else
2486 MSG(_("Already at newest change"));
Bram Moolenaar1e607892006-03-13 22:15:53 +00002487 return;
2488 }
2489
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002490 target = closest_seq;
2491 dosec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002492 dofile = FALSE;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002493 if (step < 0)
2494 above = TRUE; /* stop above the header */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002495 }
2496
2497 /* If we found it: Follow the path to go to where we want to be. */
2498 if (uhp != NULL)
2499 {
2500 /*
2501 * First go up the tree as much as needed.
2502 */
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002503 while (!got_int)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002504 {
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002505 /* Do the change warning now, for the same reason as above. */
2506 change_warning(0);
2507
Bram Moolenaar1e607892006-03-13 22:15:53 +00002508 uhp = curbuf->b_u_curhead;
2509 if (uhp == NULL)
2510 uhp = curbuf->b_u_newhead;
2511 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002512 uhp = uhp->uh_next.ptr;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002513 if (uhp == NULL || uhp->uh_walk != mark
2514 || (uhp->uh_seq == target && !above))
Bram Moolenaar1e607892006-03-13 22:15:53 +00002515 break;
2516 curbuf->b_u_curhead = uhp;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002517 u_undoredo(TRUE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002518 uhp->uh_walk = nomark; /* don't go back down here */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002519 }
2520
2521 /*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002522 * And now go down the tree (redo), branching off where needed.
Bram Moolenaar1e607892006-03-13 22:15:53 +00002523 */
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002524 while (!got_int)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002525 {
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002526 /* Do the change warning now, for the same reason as above. */
2527 change_warning(0);
2528
2529 uhp = curbuf->b_u_curhead;
2530 if (uhp == NULL)
2531 break;
2532
Bram Moolenaar89ed3df2007-01-09 19:23:12 +00002533 /* Go back to the first branch with a mark. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002534 while (uhp->uh_alt_prev.ptr != NULL
2535 && uhp->uh_alt_prev.ptr->uh_walk == mark)
2536 uhp = uhp->uh_alt_prev.ptr;
Bram Moolenaar89ed3df2007-01-09 19:23:12 +00002537
Bram Moolenaar1e607892006-03-13 22:15:53 +00002538 /* Find the last branch with a mark, that's the one. */
2539 last = uhp;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002540 while (last->uh_alt_next.ptr != NULL
2541 && last->uh_alt_next.ptr->uh_walk == mark)
2542 last = last->uh_alt_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002543 if (last != uhp)
2544 {
2545 /* Make the used branch the first entry in the list of
2546 * alternatives to make "u" and CTRL-R take this branch. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002547 while (uhp->uh_alt_prev.ptr != NULL)
2548 uhp = uhp->uh_alt_prev.ptr;
2549 if (last->uh_alt_next.ptr != NULL)
2550 last->uh_alt_next.ptr->uh_alt_prev.ptr =
2551 last->uh_alt_prev.ptr;
2552 last->uh_alt_prev.ptr->uh_alt_next.ptr = last->uh_alt_next.ptr;
2553 last->uh_alt_prev.ptr = NULL;
2554 last->uh_alt_next.ptr = uhp;
2555 uhp->uh_alt_prev.ptr = last;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002556
Bram Moolenaar8f1f6292010-05-30 16:55:22 +02002557 if (curbuf->b_u_oldhead == uhp)
2558 curbuf->b_u_oldhead = last;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002559 uhp = last;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002560 if (uhp->uh_next.ptr != NULL)
2561 uhp->uh_next.ptr->uh_prev.ptr = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002562 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002563 curbuf->b_u_curhead = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002564
2565 if (uhp->uh_walk != mark)
2566 break; /* must have reached the target */
2567
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002568 /* Stop when going backwards in time and didn't find the exact
2569 * header we were looking for. */
2570 if (uhp->uh_seq == target && above)
Bram Moolenaardb552d602006-03-23 22:59:57 +00002571 {
2572 curbuf->b_u_seq_cur = target - 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002573 break;
Bram Moolenaardb552d602006-03-23 22:59:57 +00002574 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002575
Bram Moolenaarca003e12006-03-17 23:19:38 +00002576 u_undoredo(FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002577
2578 /* Advance "curhead" to below the header we last used. If it
2579 * becomes NULL then we need to set "newhead" to this leaf. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002580 if (uhp->uh_prev.ptr == NULL)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002581 curbuf->b_u_newhead = uhp;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002582 curbuf->b_u_curhead = uhp->uh_prev.ptr;
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002583 did_undo = FALSE;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002584
2585 if (uhp->uh_seq == target) /* found it! */
2586 break;
2587
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002588 uhp = uhp->uh_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002589 if (uhp == NULL || uhp->uh_walk != mark)
2590 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002591 /* Need to redo more but can't find it... */
Bram Moolenaar95f09602016-11-10 20:01:45 +01002592 internal_error("undo_time()");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002593 break;
2594 }
2595 }
2596 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00002597 u_undo_end(did_undo, absolute);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598}
2599
2600/*
2601 * u_undoredo: common code for undo and redo
2602 *
2603 * The lines in the file are replaced by the lines in the entry list at
2604 * curbuf->b_u_curhead. The replaced lines in the file are saved in the entry
2605 * list for the next undo/redo.
Bram Moolenaarca003e12006-03-17 23:19:38 +00002606 *
2607 * When "undo" is TRUE we go up in the tree, when FALSE we go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 */
2609 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002610u_undoredo(int undo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611{
2612 char_u **newarray = NULL;
2613 linenr_T oldsize;
2614 linenr_T newsize;
2615 linenr_T top, bot;
2616 linenr_T lnum;
2617 linenr_T newlnum = MAXLNUM;
2618 long i;
2619 u_entry_T *uep, *nuep;
2620 u_entry_T *newlist = NULL;
2621 int old_flags;
2622 int new_flags;
2623 pos_T namedm[NMARKS];
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002624 visualinfo_T visualinfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 int empty_buffer; /* buffer became empty */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002626 u_header_T *curhead = curbuf->b_u_curhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002628#ifdef FEAT_AUTOCMD
2629 /* Don't want autocommands using the undo structures here, they are
2630 * invalid till the end. */
2631 block_autocmds();
2632#endif
2633
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002634#ifdef U_DEBUG
2635 u_check(FALSE);
2636#endif
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002637 old_flags = curhead->uh_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 new_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
2639 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
2640 setpcmark();
2641
2642 /*
2643 * save marks before undo/redo
2644 */
2645 mch_memmove(namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002646 visualinfo = curbuf->b_visual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 curbuf->b_op_start.lnum = curbuf->b_ml.ml_line_count;
2648 curbuf->b_op_start.col = 0;
2649 curbuf->b_op_end.lnum = 0;
2650 curbuf->b_op_end.col = 0;
2651
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002652 for (uep = curhead->uh_entry; uep != NULL; uep = nuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 {
2654 top = uep->ue_top;
2655 bot = uep->ue_bot;
2656 if (bot == 0)
2657 bot = curbuf->b_ml.ml_line_count + 1;
2658 if (top > curbuf->b_ml.ml_line_count || top >= bot
2659 || bot > curbuf->b_ml.ml_line_count + 1)
2660 {
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002661#ifdef FEAT_AUTOCMD
2662 unblock_autocmds();
2663#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01002664 IEMSG(_("E438: u_undo: line numbers wrong"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 changed(); /* don't want UNCHANGED now */
2666 return;
2667 }
2668
2669 oldsize = bot - top - 1; /* number of lines before undo */
2670 newsize = uep->ue_size; /* number of lines after undo */
2671
2672 if (top < newlnum)
2673 {
2674 /* If the saved cursor is somewhere in this undo block, move it to
2675 * the remembered position. Makes "gwap" put the cursor back
2676 * where it was. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002677 lnum = curhead->uh_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 if (lnum >= top && lnum <= top + newsize + 1)
2679 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002680 curwin->w_cursor = curhead->uh_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 newlnum = curwin->w_cursor.lnum - 1;
2682 }
2683 else
2684 {
2685 /* Use the first line that actually changed. Avoids that
2686 * undoing auto-formatting puts the cursor in the previous
2687 * line. */
2688 for (i = 0; i < newsize && i < oldsize; ++i)
2689 if (STRCMP(uep->ue_array[i], ml_get(top + 1 + i)) != 0)
2690 break;
2691 if (i == newsize && newlnum == MAXLNUM && uep->ue_next == NULL)
2692 {
2693 newlnum = top;
2694 curwin->w_cursor.lnum = newlnum + 1;
2695 }
2696 else if (i < newsize)
2697 {
2698 newlnum = top + i;
2699 curwin->w_cursor.lnum = newlnum + 1;
2700 }
2701 }
2702 }
2703
2704 empty_buffer = FALSE;
2705
2706 /* delete the lines between top and bot and save them in newarray */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002707 if (oldsize > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002709 if ((newarray = (char_u **)U_ALLOC_LINE(
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002710 sizeof(char_u *) * oldsize)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 {
2712 do_outofmem_msg((long_u)(sizeof(char_u *) * oldsize));
2713 /*
2714 * We have messed up the entry list, repair is impossible.
2715 * we have to free the rest of the list.
2716 */
2717 while (uep != NULL)
2718 {
2719 nuep = uep->ue_next;
2720 u_freeentry(uep, uep->ue_size);
2721 uep = nuep;
2722 }
2723 break;
2724 }
2725 /* delete backwards, it goes faster in most cases */
2726 for (lnum = bot - 1, i = oldsize; --i >= 0; --lnum)
2727 {
2728 /* what can we do when we run out of memory? */
2729 if ((newarray[i] = u_save_line(lnum)) == NULL)
2730 do_outofmem_msg((long_u)0);
2731 /* remember we deleted the last line in the buffer, and a
2732 * dummy empty line will be inserted */
2733 if (curbuf->b_ml.ml_line_count == 1)
2734 empty_buffer = TRUE;
2735 ml_delete(lnum, FALSE);
2736 }
2737 }
Bram Moolenaar8d343302005-07-12 22:46:17 +00002738 else
2739 newarray = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740
2741 /* insert the lines in u_array between top and bot */
2742 if (newsize)
2743 {
2744 for (lnum = top, i = 0; i < newsize; ++i, ++lnum)
2745 {
2746 /*
2747 * If the file is empty, there is an empty line 1 that we
2748 * should get rid of, by replacing it with the new line
2749 */
2750 if (empty_buffer && lnum == 0)
2751 ml_replace((linenr_T)1, uep->ue_array[i], TRUE);
2752 else
2753 ml_append(lnum, uep->ue_array[i], (colnr_T)0, FALSE);
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002754 vim_free(uep->ue_array[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 }
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002756 vim_free((char_u *)uep->ue_array);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 }
2758
2759 /* adjust marks */
2760 if (oldsize != newsize)
2761 {
2762 mark_adjust(top + 1, top + oldsize, (long)MAXLNUM,
2763 (long)newsize - (long)oldsize);
2764 if (curbuf->b_op_start.lnum > top + oldsize)
2765 curbuf->b_op_start.lnum += newsize - oldsize;
2766 if (curbuf->b_op_end.lnum > top + oldsize)
2767 curbuf->b_op_end.lnum += newsize - oldsize;
2768 }
2769
2770 changed_lines(top + 1, 0, bot, newsize - oldsize);
2771
2772 /* set '[ and '] mark */
2773 if (top + 1 < curbuf->b_op_start.lnum)
2774 curbuf->b_op_start.lnum = top + 1;
2775 if (newsize == 0 && top + 1 > curbuf->b_op_end.lnum)
2776 curbuf->b_op_end.lnum = top + 1;
2777 else if (top + newsize > curbuf->b_op_end.lnum)
2778 curbuf->b_op_end.lnum = top + newsize;
2779
2780 u_newcount += newsize;
2781 u_oldcount += oldsize;
2782 uep->ue_size = oldsize;
2783 uep->ue_array = newarray;
2784 uep->ue_bot = top + newsize + 1;
2785
2786 /*
2787 * insert this entry in front of the new entry list
2788 */
2789 nuep = uep->ue_next;
2790 uep->ue_next = newlist;
2791 newlist = uep;
2792 }
2793
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002794 curhead->uh_entry = newlist;
2795 curhead->uh_flags = new_flags;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002796 if ((old_flags & UH_EMPTYBUF) && BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 curbuf->b_ml.ml_flags |= ML_EMPTY;
2798 if (old_flags & UH_CHANGED)
2799 changed();
2800 else
Bram Moolenaar009b2592004-10-24 19:18:58 +00002801#ifdef FEAT_NETBEANS_INTG
2802 /* per netbeans undo rules, keep it as modified */
2803 if (!isNetbeansModified(curbuf))
2804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 unchanged(curbuf, FALSE);
2806
2807 /*
2808 * restore marks from before undo/redo
2809 */
2810 for (i = 0; i < NMARKS; ++i)
Bram Moolenaarf65aad52015-02-17 13:43:40 +01002811 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002812 if (curhead->uh_namedm[i].lnum != 0)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002813 curbuf->b_namedm[i] = curhead->uh_namedm[i];
Bram Moolenaarf65aad52015-02-17 13:43:40 +01002814 if (namedm[i].lnum != 0)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002815 curhead->uh_namedm[i] = namedm[i];
Bram Moolenaarf65aad52015-02-17 13:43:40 +01002816 else
2817 curhead->uh_namedm[i].lnum = 0;
2818 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002819 if (curhead->uh_visual.vi_start.lnum != 0)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002820 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002821 curbuf->b_visual = curhead->uh_visual;
2822 curhead->uh_visual = visualinfo;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824
2825 /*
2826 * If the cursor is only off by one line, put it at the same position as
2827 * before starting the change (for the "o" command).
2828 * Otherwise the cursor should go to the first undone line.
2829 */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002830 if (curhead->uh_cursor.lnum + 1 == curwin->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 && curwin->w_cursor.lnum > 1)
2832 --curwin->w_cursor.lnum;
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002833 if (curwin->w_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 {
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002835 if (curhead->uh_cursor.lnum == curwin->w_cursor.lnum)
2836 {
2837 curwin->w_cursor.col = curhead->uh_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002839 if (virtual_active() && curhead->uh_cursor_vcol >= 0)
2840 coladvance((colnr_T)curhead->uh_cursor_vcol);
2841 else
2842 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843#endif
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002844 }
2845 else
2846 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 else
2849 {
2850 /* We get here with the current cursor line being past the end (eg
2851 * after adding lines at the end of the file, and then undoing it).
2852 * check_cursor() will move the cursor to the last line. Move it to
2853 * the first column here. */
2854 curwin->w_cursor.col = 0;
2855#ifdef FEAT_VIRTUALEDIT
2856 curwin->w_cursor.coladd = 0;
2857#endif
2858 }
2859
2860 /* Make sure the cursor is on an existing line and column. */
2861 check_cursor();
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002862
2863 /* Remember where we are for "g-" and ":earlier 10s". */
2864 curbuf->b_u_seq_cur = curhead->uh_seq;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002865 if (undo)
Bram Moolenaar80eaddd2017-11-11 23:37:08 +01002866 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002867 /* We are below the previous undo. However, to make ":earlier 1s"
2868 * work we compute this as being just above the just undone change. */
Bram Moolenaar80eaddd2017-11-11 23:37:08 +01002869 if (curhead->uh_next.ptr != NULL)
2870 curbuf->b_u_seq_cur = curhead->uh_next.ptr->uh_seq;
2871 else
2872 curbuf->b_u_seq_cur = 0;
2873 }
Bram Moolenaarca003e12006-03-17 23:19:38 +00002874
Bram Moolenaar730cde92010-06-27 05:18:54 +02002875 /* Remember where we are for ":earlier 1f" and ":later 1f". */
2876 if (curhead->uh_save_nr != 0)
2877 {
2878 if (undo)
2879 curbuf->b_u_save_nr_cur = curhead->uh_save_nr - 1;
2880 else
2881 curbuf->b_u_save_nr_cur = curhead->uh_save_nr;
2882 }
2883
Bram Moolenaarca003e12006-03-17 23:19:38 +00002884 /* The timestamp can be the same for multiple changes, just use the one of
2885 * the undone/redone change. */
Bram Moolenaara800b422010-06-27 01:15:55 +02002886 curbuf->b_u_time_cur = curhead->uh_time;
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002887
2888#ifdef FEAT_AUTOCMD
2889 unblock_autocmds();
2890#endif
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002891#ifdef U_DEBUG
2892 u_check(FALSE);
2893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894}
2895
2896/*
2897 * If we deleted or added lines, report the number of less/more lines.
2898 * Otherwise, report the number of changes (this may be incorrect
2899 * in some cases, but it's better than nothing).
2900 */
2901 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002902u_undo_end(
2903 int did_undo, /* just did an undo */
2904 int absolute) /* used ":undo N" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905{
Bram Moolenaar89d40322006-08-29 15:30:07 +00002906 char *msgstr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002907 u_header_T *uhp;
2908 char_u msgbuf[80];
Bram Moolenaar1e607892006-03-13 22:15:53 +00002909
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910#ifdef FEAT_FOLDING
2911 if ((fdo_flags & FDO_UNDO) && KeyTyped)
2912 foldOpenCursor();
2913#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +00002914
2915 if (global_busy /* no messages now, wait until global is finished */
2916 || !messaging()) /* 'lazyredraw' set, don't do messages now */
2917 return;
2918
2919 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2920 --u_newcount;
2921
2922 u_oldcount -= u_newcount;
2923 if (u_oldcount == -1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002924 msgstr = N_("more line");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002925 else if (u_oldcount < 0)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002926 msgstr = N_("more lines");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002927 else if (u_oldcount == 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002928 msgstr = N_("line less");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002929 else if (u_oldcount > 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002930 msgstr = N_("fewer lines");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002931 else
2932 {
2933 u_oldcount = u_newcount;
2934 if (u_newcount == 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002935 msgstr = N_("change");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002936 else
Bram Moolenaar89d40322006-08-29 15:30:07 +00002937 msgstr = N_("changes");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002938 }
2939
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002940 if (curbuf->b_u_curhead != NULL)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002941 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00002942 /* For ":undo N" we prefer a "after #N" message. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002943 if (absolute && curbuf->b_u_curhead->uh_next.ptr != NULL)
Bram Moolenaardb552d602006-03-23 22:59:57 +00002944 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002945 uhp = curbuf->b_u_curhead->uh_next.ptr;
Bram Moolenaardb552d602006-03-23 22:59:57 +00002946 did_undo = FALSE;
2947 }
2948 else if (did_undo)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002949 uhp = curbuf->b_u_curhead;
2950 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002951 uhp = curbuf->b_u_curhead->uh_next.ptr;
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002952 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002953 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002954 uhp = curbuf->b_u_newhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002955
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002956 if (uhp == NULL)
2957 *msgbuf = NUL;
2958 else
2959 u_add_time(msgbuf, sizeof(msgbuf), uhp->uh_time);
2960
Bram Moolenaarb2c03502010-07-02 20:20:09 +02002961#ifdef FEAT_CONCEAL
2962 {
2963 win_T *wp;
2964
2965 FOR_ALL_WINDOWS(wp)
2966 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02002967 if (wp->w_buffer == curbuf && wp->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02002968 redraw_win_later(wp, NOT_VALID);
2969 }
2970 }
2971#endif
2972
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002973 smsg((char_u *)_("%ld %s; %s #%ld %s"),
Bram Moolenaarca003e12006-03-17 23:19:38 +00002974 u_oldcount < 0 ? -u_oldcount : u_oldcount,
Bram Moolenaar89d40322006-08-29 15:30:07 +00002975 _(msgstr),
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002976 did_undo ? _("before") : _("after"),
2977 uhp == NULL ? 0L : uhp->uh_seq,
2978 msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979}
2980
2981/*
2982 * u_sync: stop adding to the current entry list
2983 */
2984 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002985u_sync(
2986 int force) /* Also sync when no_u_sync is set. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987{
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002988 /* Skip it when already synced or syncing is disabled. */
2989 if (curbuf->b_u_synced || (!force && no_u_sync > 0))
2990 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02002992 if (p_imst == IM_ON_THE_SPOT && im_is_preediting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 return; /* XIM is busy, don't break an undo sequence */
2994#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002995 if (get_undolevel() < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 curbuf->b_u_synced = TRUE; /* no entries, nothing to do */
2997 else
2998 {
2999 u_getbot(); /* compute ue_bot of previous u_save */
3000 curbuf->b_u_curhead = NULL;
3001 }
3002}
3003
3004/*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003005 * ":undolist": List the leafs of the undo tree
3006 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003007 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003008ex_undolist(exarg_T *eap UNUSED)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003009{
3010 garray_T ga;
3011 u_header_T *uhp;
3012 int mark;
3013 int nomark;
3014 int changes = 1;
3015 int i;
3016
3017 /*
3018 * 1: walk the tree to find all leafs, put the info in "ga".
3019 * 2: sort the lines
3020 * 3: display the list
3021 */
3022 mark = ++lastmark;
3023 nomark = ++lastmark;
3024 ga_init2(&ga, (int)sizeof(char *), 20);
3025
3026 uhp = curbuf->b_u_oldhead;
3027 while (uhp != NULL)
3028 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003029 if (uhp->uh_prev.ptr == NULL && uhp->uh_walk != nomark
Bram Moolenaarca003e12006-03-17 23:19:38 +00003030 && uhp->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003031 {
3032 if (ga_grow(&ga, 1) == FAIL)
3033 break;
3034 vim_snprintf((char *)IObuff, IOSIZE, "%6ld %7ld ",
3035 uhp->uh_seq, changes);
3036 u_add_time(IObuff + STRLEN(IObuff), IOSIZE - STRLEN(IObuff),
3037 uhp->uh_time);
Bram Moolenaara800b422010-06-27 01:15:55 +02003038 if (uhp->uh_save_nr > 0)
3039 {
Bram Moolenaardba01a02010-11-03 19:32:42 +01003040 while (STRLEN(IObuff) < 33)
Bram Moolenaara800b422010-06-27 01:15:55 +02003041 STRCAT(IObuff, " ");
3042 vim_snprintf_add((char *)IObuff, IOSIZE,
3043 " %3ld", uhp->uh_save_nr);
3044 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003045 ((char_u **)(ga.ga_data))[ga.ga_len++] = vim_strsave(IObuff);
3046 }
3047
3048 uhp->uh_walk = mark;
3049
3050 /* go down in the tree if we haven't been there */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003051 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != nomark
3052 && uhp->uh_prev.ptr->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003053 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003054 uhp = uhp->uh_prev.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003055 ++changes;
3056 }
3057
3058 /* go to alternate branch if we haven't been there */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003059 else if (uhp->uh_alt_next.ptr != NULL
3060 && uhp->uh_alt_next.ptr->uh_walk != nomark
3061 && uhp->uh_alt_next.ptr->uh_walk != mark)
3062 uhp = uhp->uh_alt_next.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003063
3064 /* go up in the tree if we haven't been there and we are at the
3065 * start of alternate branches */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003066 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
3067 && uhp->uh_next.ptr->uh_walk != nomark
3068 && uhp->uh_next.ptr->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003069 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003070 uhp = uhp->uh_next.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003071 --changes;
3072 }
3073
3074 else
3075 {
3076 /* need to backtrack; mark this node as done */
3077 uhp->uh_walk = nomark;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003078 if (uhp->uh_alt_prev.ptr != NULL)
3079 uhp = uhp->uh_alt_prev.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003080 else
3081 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003082 uhp = uhp->uh_next.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003083 --changes;
3084 }
3085 }
3086 }
3087
3088 if (ga.ga_len == 0)
3089 MSG(_("Nothing to undo"));
3090 else
3091 {
3092 sort_strings((char_u **)ga.ga_data, ga.ga_len);
3093
3094 msg_start();
Bram Moolenaardba01a02010-11-03 19:32:42 +01003095 msg_puts_attr((char_u *)_("number changes when saved"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01003096 HL_ATTR(HLF_T));
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003097 for (i = 0; i < ga.ga_len && !got_int; ++i)
3098 {
3099 msg_putchar('\n');
3100 if (got_int)
3101 break;
3102 msg_puts(((char_u **)ga.ga_data)[i]);
3103 }
3104 msg_end();
3105
3106 ga_clear_strings(&ga);
3107 }
3108}
3109
3110/*
3111 * Put the timestamp of an undo header in "buf[buflen]" in a nice format.
3112 */
3113 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003114u_add_time(char_u *buf, size_t buflen, time_t tt)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003115{
3116#ifdef HAVE_STRFTIME
3117 struct tm *curtime;
3118
Bram Moolenaar170b10b2016-07-29 16:15:27 +02003119 if (vim_time() - tt >= 100)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003120 {
3121 curtime = localtime(&tt);
Bram Moolenaar170b10b2016-07-29 16:15:27 +02003122 if (vim_time() - tt < (60L * 60L * 12L))
Bram Moolenaardba01a02010-11-03 19:32:42 +01003123 /* within 12 hours */
3124 (void)strftime((char *)buf, buflen, "%H:%M:%S", curtime);
Bram Moolenaardba01a02010-11-03 19:32:42 +01003125 else
Bram Moolenaarb9ce83e2012-08-23 12:59:02 +02003126 /* longer ago */
Bram Moolenaar5e3d6ca2011-01-22 21:25:11 +01003127 (void)strftime((char *)buf, buflen, "%Y/%m/%d %H:%M:%S", curtime);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003128 }
3129 else
3130#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003131 vim_snprintf((char *)buf, buflen, _("%ld seconds ago"),
Bram Moolenaar170b10b2016-07-29 16:15:27 +02003132 (long)(vim_time() - tt));
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003133}
3134
3135/*
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003136 * ":undojoin": continue adding to the last entry list
3137 */
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003138 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003139ex_undojoin(exarg_T *eap UNUSED)
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003140{
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003141 if (curbuf->b_u_newhead == NULL)
3142 return; /* nothing changed before */
Bram Moolenaar57657d82006-04-21 22:12:41 +00003143 if (curbuf->b_u_curhead != NULL)
3144 {
3145 EMSG(_("E790: undojoin is not allowed after undo"));
3146 return;
3147 }
3148 if (!curbuf->b_u_synced)
3149 return; /* already unsynced */
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003150 if (get_undolevel() < 0)
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003151 return; /* no entries, nothing to do */
3152 else
Bram Moolenaar5e4e1b12017-01-17 22:09:45 +01003153 /* Append next change to the last entry */
3154 curbuf->b_u_synced = FALSE;
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003155}
3156
3157/*
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003158 * Called after writing or reloading the file and setting b_changed to FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 * Now an undo means that the buffer is modified.
3160 */
3161 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003162u_unchanged(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163{
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003164 u_unch_branch(buf->b_u_oldhead);
3165 buf->b_did_warn = FALSE;
3166}
3167
Bram Moolenaar730cde92010-06-27 05:18:54 +02003168/*
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003169 * After reloading a buffer which was saved for 'undoreload': Find the first
3170 * line that was changed and set the cursor there.
3171 */
3172 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003173u_find_first_changed(void)
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003174{
3175 u_header_T *uhp = curbuf->b_u_newhead;
3176 u_entry_T *uep;
3177 linenr_T lnum;
3178
3179 if (curbuf->b_u_curhead != NULL || uhp == NULL)
3180 return; /* undid something in an autocmd? */
3181
3182 /* Check that the last undo block was for the whole file. */
3183 uep = uhp->uh_entry;
3184 if (uep->ue_top != 0 || uep->ue_bot != 0)
3185 return;
3186
3187 for (lnum = 1; lnum < curbuf->b_ml.ml_line_count
3188 && lnum <= uep->ue_size; ++lnum)
3189 if (STRCMP(ml_get_buf(curbuf, lnum, FALSE),
3190 uep->ue_array[lnum - 1]) != 0)
3191 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003192 CLEAR_POS(&(uhp->uh_cursor));
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003193 uhp->uh_cursor.lnum = lnum;
3194 return;
3195 }
3196 if (curbuf->b_ml.ml_line_count != uep->ue_size)
3197 {
3198 /* lines added or deleted at the end, put the cursor there */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003199 CLEAR_POS(&(uhp->uh_cursor));
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003200 uhp->uh_cursor.lnum = lnum;
3201 }
3202}
3203
3204/*
Bram Moolenaar730cde92010-06-27 05:18:54 +02003205 * Increase the write count, store it in the last undo header, what would be
3206 * used for "u".
3207 */
3208 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003209u_update_save_nr(buf_T *buf)
Bram Moolenaar730cde92010-06-27 05:18:54 +02003210{
3211 u_header_T *uhp;
3212
3213 ++buf->b_u_save_nr_last;
3214 buf->b_u_save_nr_cur = buf->b_u_save_nr_last;
3215 uhp = buf->b_u_curhead;
3216 if (uhp != NULL)
3217 uhp = uhp->uh_next.ptr;
3218 else
3219 uhp = buf->b_u_newhead;
3220 if (uhp != NULL)
3221 uhp->uh_save_nr = buf->b_u_save_nr_last;
3222}
3223
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003224 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003225u_unch_branch(u_header_T *uhp)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003226{
Bram Moolenaar1e607892006-03-13 22:15:53 +00003227 u_header_T *uh;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003229 for (uh = uhp; uh != NULL; uh = uh->uh_prev.ptr)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003230 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 uh->uh_flags |= UH_CHANGED;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003232 if (uh->uh_alt_next.ptr != NULL)
3233 u_unch_branch(uh->uh_alt_next.ptr); /* recursive */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235}
3236
3237/*
3238 * Get pointer to last added entry.
3239 * If it's not valid, give an error message and return NULL.
3240 */
3241 static u_entry_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003242u_get_headentry(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243{
3244 if (curbuf->b_u_newhead == NULL || curbuf->b_u_newhead->uh_entry == NULL)
3245 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003246 IEMSG(_("E439: undo list corrupt"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 return NULL;
3248 }
3249 return curbuf->b_u_newhead->uh_entry;
3250}
3251
3252/*
3253 * u_getbot(): compute the line number of the previous u_save
3254 * It is called only when b_u_synced is FALSE.
3255 */
3256 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003257u_getbot(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258{
3259 u_entry_T *uep;
3260 linenr_T extra;
3261
3262 uep = u_get_headentry(); /* check for corrupt undo list */
3263 if (uep == NULL)
3264 return;
3265
3266 uep = curbuf->b_u_newhead->uh_getbot_entry;
3267 if (uep != NULL)
3268 {
3269 /*
3270 * the new ue_bot is computed from the number of lines that has been
3271 * inserted (0 - deleted) since calling u_save. This is equal to the
3272 * old line count subtracted from the current line count.
3273 */
3274 extra = curbuf->b_ml.ml_line_count - uep->ue_lcount;
3275 uep->ue_bot = uep->ue_top + uep->ue_size + 1 + extra;
3276 if (uep->ue_bot < 1 || uep->ue_bot > curbuf->b_ml.ml_line_count)
3277 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003278 IEMSG(_("E440: undo line missing"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 uep->ue_bot = uep->ue_top + 1; /* assume all lines deleted, will
3280 * get all the old lines back
3281 * without deleting the current
3282 * ones */
3283 }
3284
3285 curbuf->b_u_newhead->uh_getbot_entry = NULL;
3286 }
3287
3288 curbuf->b_u_synced = TRUE;
3289}
3290
3291/*
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003292 * Free one header "uhp" and its entry list and adjust the pointers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 */
3294 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003295u_freeheader(
3296 buf_T *buf,
3297 u_header_T *uhp,
3298 u_header_T **uhpp) /* if not NULL reset when freeing this header */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299{
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003300 u_header_T *uhap;
3301
Bram Moolenaar1e607892006-03-13 22:15:53 +00003302 /* When there is an alternate redo list free that branch completely,
3303 * because we can never go there. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003304 if (uhp->uh_alt_next.ptr != NULL)
3305 u_freebranch(buf, uhp->uh_alt_next.ptr, uhpp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003307 if (uhp->uh_alt_prev.ptr != NULL)
3308 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309
Bram Moolenaar1e607892006-03-13 22:15:53 +00003310 /* Update the links in the list to remove the header. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003311 if (uhp->uh_next.ptr == NULL)
3312 buf->b_u_oldhead = uhp->uh_prev.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003314 uhp->uh_next.ptr->uh_prev.ptr = uhp->uh_prev.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003316 if (uhp->uh_prev.ptr == NULL)
3317 buf->b_u_newhead = uhp->uh_next.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003319 for (uhap = uhp->uh_prev.ptr; uhap != NULL;
3320 uhap = uhap->uh_alt_next.ptr)
3321 uhap->uh_next.ptr = uhp->uh_next.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322
Bram Moolenaar1e607892006-03-13 22:15:53 +00003323 u_freeentries(buf, uhp, uhpp);
3324}
3325
3326/*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003327 * Free an alternate branch and any following alternate branches.
Bram Moolenaar1e607892006-03-13 22:15:53 +00003328 */
3329 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003330u_freebranch(
3331 buf_T *buf,
3332 u_header_T *uhp,
3333 u_header_T **uhpp) /* if not NULL reset when freeing this header */
Bram Moolenaar1e607892006-03-13 22:15:53 +00003334{
3335 u_header_T *tofree, *next;
3336
Bram Moolenaar07d06772007-11-10 21:51:15 +00003337 /* If this is the top branch we may need to use u_freeheader() to update
3338 * all the pointers. */
3339 if (uhp == buf->b_u_oldhead)
3340 {
Bram Moolenaaraa887322013-11-07 03:04:11 +01003341 while (buf->b_u_oldhead != NULL)
3342 u_freeheader(buf, buf->b_u_oldhead, uhpp);
Bram Moolenaar07d06772007-11-10 21:51:15 +00003343 return;
3344 }
3345
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003346 if (uhp->uh_alt_prev.ptr != NULL)
3347 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +00003348
3349 next = uhp;
3350 while (next != NULL)
3351 {
3352 tofree = next;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003353 if (tofree->uh_alt_next.ptr != NULL)
3354 u_freebranch(buf, tofree->uh_alt_next.ptr, uhpp); /* recursive */
3355 next = tofree->uh_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00003356 u_freeentries(buf, tofree, uhpp);
3357 }
3358}
3359
3360/*
3361 * Free all the undo entries for one header and the header itself.
3362 * This means that "uhp" is invalid when returning.
3363 */
3364 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003365u_freeentries(
3366 buf_T *buf,
3367 u_header_T *uhp,
3368 u_header_T **uhpp) /* if not NULL reset when freeing this header */
Bram Moolenaar1e607892006-03-13 22:15:53 +00003369{
3370 u_entry_T *uep, *nuep;
3371
3372 /* Check for pointers to the header that become invalid now. */
3373 if (buf->b_u_curhead == uhp)
3374 buf->b_u_curhead = NULL;
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003375 if (buf->b_u_newhead == uhp)
3376 buf->b_u_newhead = NULL; /* freeing the newest entry */
Bram Moolenaar1e607892006-03-13 22:15:53 +00003377 if (uhpp != NULL && uhp == *uhpp)
3378 *uhpp = NULL;
3379
3380 for (uep = uhp->uh_entry; uep != NULL; uep = nuep)
3381 {
3382 nuep = uep->ue_next;
3383 u_freeentry(uep, uep->ue_size);
3384 }
3385
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003386#ifdef U_DEBUG
3387 uhp->uh_magic = 0;
3388#endif
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003389 vim_free((char_u *)uhp);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003390 --buf->b_u_numhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391}
3392
3393/*
3394 * free entry 'uep' and 'n' lines in uep->ue_array[]
3395 */
3396 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003397u_freeentry(u_entry_T *uep, long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398{
Bram Moolenaar8d343302005-07-12 22:46:17 +00003399 while (n > 0)
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003400 vim_free(uep->ue_array[--n]);
3401 vim_free((char_u *)uep->ue_array);
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003402#ifdef U_DEBUG
3403 uep->ue_magic = 0;
3404#endif
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003405 vim_free((char_u *)uep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406}
3407
3408/*
3409 * invalidate the undo buffer; called when storage has already been released
3410 */
3411 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003412u_clearall(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413{
3414 buf->b_u_newhead = buf->b_u_oldhead = buf->b_u_curhead = NULL;
3415 buf->b_u_synced = TRUE;
3416 buf->b_u_numhead = 0;
3417 buf->b_u_line_ptr = NULL;
3418 buf->b_u_line_lnum = 0;
3419}
3420
3421/*
3422 * save the line "lnum" for the "U" command
3423 */
3424 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003425u_saveline(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426{
3427 if (lnum == curbuf->b_u_line_lnum) /* line is already saved */
3428 return;
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00003429 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) /* should never happen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 return;
3431 u_clearline();
3432 curbuf->b_u_line_lnum = lnum;
3433 if (curwin->w_cursor.lnum == lnum)
3434 curbuf->b_u_line_colnr = curwin->w_cursor.col;
3435 else
3436 curbuf->b_u_line_colnr = 0;
3437 if ((curbuf->b_u_line_ptr = u_save_line(lnum)) == NULL)
3438 do_outofmem_msg((long_u)0);
3439}
3440
3441/*
3442 * clear the line saved for the "U" command
3443 * (this is used externally for crossing a line while in insert mode)
3444 */
3445 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003446u_clearline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447{
3448 if (curbuf->b_u_line_ptr != NULL)
3449 {
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003450 vim_free(curbuf->b_u_line_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 curbuf->b_u_line_ptr = NULL;
3452 curbuf->b_u_line_lnum = 0;
3453 }
3454}
3455
3456/*
3457 * Implementation of the "U" command.
3458 * Differentiation from vi: "U" can be undone with the next "U".
3459 * We also allow the cursor to be in another line.
Bram Moolenaard04b7502010-07-08 22:27:55 +02003460 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 */
3462 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003463u_undoline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464{
3465 colnr_T t;
3466 char_u *oldp;
3467
3468 if (undo_off)
3469 return;
3470
Bram Moolenaare3300c82008-02-13 14:21:38 +00003471 if (curbuf->b_u_line_ptr == NULL
3472 || curbuf->b_u_line_lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 {
3474 beep_flush();
3475 return;
3476 }
Bram Moolenaare3300c82008-02-13 14:21:38 +00003477
3478 /* first save the line for the 'u' command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 if (u_savecommon(curbuf->b_u_line_lnum - 1,
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003480 curbuf->b_u_line_lnum + 1, (linenr_T)0, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 return;
3482 oldp = u_save_line(curbuf->b_u_line_lnum);
3483 if (oldp == NULL)
3484 {
3485 do_outofmem_msg((long_u)0);
3486 return;
3487 }
3488 ml_replace(curbuf->b_u_line_lnum, curbuf->b_u_line_ptr, TRUE);
3489 changed_bytes(curbuf->b_u_line_lnum, 0);
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003490 vim_free(curbuf->b_u_line_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 curbuf->b_u_line_ptr = oldp;
3492
3493 t = curbuf->b_u_line_colnr;
3494 if (curwin->w_cursor.lnum == curbuf->b_u_line_lnum)
3495 curbuf->b_u_line_colnr = curwin->w_cursor.col;
3496 curwin->w_cursor.col = t;
3497 curwin->w_cursor.lnum = curbuf->b_u_line_lnum;
Bram Moolenaare3300c82008-02-13 14:21:38 +00003498 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499}
3500
3501/*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003502 * Free all allocated memory blocks for the buffer 'buf'.
3503 */
3504 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003505u_blockfree(buf_T *buf)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003506{
Bram Moolenaar1e607892006-03-13 22:15:53 +00003507 while (buf->b_u_oldhead != NULL)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003508 u_freeheader(buf, buf->b_u_oldhead, NULL);
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003509 vim_free(buf->b_u_line_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510}
3511
3512/*
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003513 * u_save_line(): allocate memory and copy line 'lnum' into it.
3514 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 */
3516 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003517u_save_line(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518{
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003519 return vim_strsave(ml_get(lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520}
3521
3522/*
3523 * Check if the 'modified' flag is set, or 'ff' has changed (only need to
3524 * check the first character, because it can only be "dos", "unix" or "mac").
3525 * "nofile" and "scratch" type buffers are considered to always be unchanged.
3526 */
3527 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003528bufIsChanged(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529{
Bram Moolenaareb44a682017-08-03 22:44:55 +02003530#ifdef FEAT_TERMINAL
3531 if (term_job_running(buf->b_term))
3532 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02003534 return !bt_dontwrite(buf)
3535 && (buf->b_changed || file_ff_differs(buf, TRUE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536}
3537
3538 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003539curbufIsChanged(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540{
Bram Moolenaareb44a682017-08-03 22:44:55 +02003541 return bufIsChanged(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542}
Bram Moolenaara800b422010-06-27 01:15:55 +02003543
3544#if defined(FEAT_EVAL) || defined(PROTO)
3545/*
3546 * For undotree(): Append the list of undo blocks at "first_uhp" to "list".
3547 * Recursive.
3548 */
3549 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003550u_eval_tree(u_header_T *first_uhp, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02003551{
3552 u_header_T *uhp = first_uhp;
3553 dict_T *dict;
3554
3555 while (uhp != NULL)
3556 {
3557 dict = dict_alloc();
3558 if (dict == NULL)
3559 return;
3560 dict_add_nr_str(dict, "seq", uhp->uh_seq, NULL);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02003561 dict_add_nr_str(dict, "time", (long)uhp->uh_time, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +02003562 if (uhp == curbuf->b_u_newhead)
3563 dict_add_nr_str(dict, "newhead", 1, NULL);
3564 if (uhp == curbuf->b_u_curhead)
3565 dict_add_nr_str(dict, "curhead", 1, NULL);
3566 if (uhp->uh_save_nr > 0)
3567 dict_add_nr_str(dict, "save", uhp->uh_save_nr, NULL);
3568
3569 if (uhp->uh_alt_next.ptr != NULL)
3570 {
3571 list_T *alt_list = list_alloc();
3572
3573 if (alt_list != NULL)
3574 {
3575 /* Recursive call to add alternate undo tree. */
3576 u_eval_tree(uhp->uh_alt_next.ptr, alt_list);
3577 dict_add_list(dict, "alt", alt_list);
3578 }
3579 }
3580
3581 list_append_dict(list, dict);
3582 uhp = uhp->uh_prev.ptr;
3583 }
3584}
3585#endif