blob: b9c203ce29c271b241df658b7623903fd13527b6 [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 void u_unch_branch(u_header_T *uhp);
104static u_entry_T *u_get_headentry(void);
105static void u_getbot(void);
106static void u_doit(int count);
107static void u_undoredo(int undo);
108static void u_undo_end(int did_undo, int absolute);
109static void u_add_time(char_u *buf, size_t buflen, time_t tt);
110static void u_freeheader(buf_T *buf, u_header_T *uhp, u_header_T **uhpp);
111static void u_freebranch(buf_T *buf, u_header_T *uhp, u_header_T **uhpp);
112static void u_freeentries(buf_T *buf, u_header_T *uhp, u_header_T **uhpp);
113static void u_freeentry(u_entry_T *, long);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200114#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar1d6fbe62016-02-19 22:46:34 +0100115# ifdef FEAT_CRYPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100116static int undo_flush(bufinfo_T *bi);
Bram Moolenaar1d6fbe62016-02-19 22:46:34 +0100117# endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100118static int undo_read(bufinfo_T *bi, char_u *buffer, size_t size);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100119static int serialize_uep(bufinfo_T *bi, u_entry_T *uep);
120static u_entry_T *unserialize_uep(bufinfo_T *bi, int *error, char_u *file_name);
121static void serialize_pos(bufinfo_T *bi, pos_T pos);
122static void unserialize_pos(bufinfo_T *bi, pos_T *pos);
123static void serialize_visualinfo(bufinfo_T *bi, visualinfo_T *info);
124static void unserialize_visualinfo(bufinfo_T *bi, visualinfo_T *info);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200125#endif
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200126static void u_saveline(linenr_T lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127
Bram Moolenaar18a4ba22019-05-24 19:39:03 +0200128#define U_ALLOC_LINE(size) lalloc(size, FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129
Bram Moolenaar730cde92010-06-27 05:18:54 +0200130/* used in undo_end() to report number of added and deleted lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131static long u_newcount, u_oldcount;
132
133/*
134 * When 'u' flag included in 'cpoptions', we behave like vi. Need to remember
135 * the action that "u" should do.
136 */
137static int undo_undoes = FALSE;
138
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200139static int lastmark = 0;
140
Bram Moolenaar9db58062010-05-29 20:33:07 +0200141#if defined(U_DEBUG) || defined(PROTO)
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000142/*
143 * Check the undo structures for being valid. Print a warning when something
144 * looks wrong.
145 */
146static int seen_b_u_curhead;
147static int seen_b_u_newhead;
148static int header_count;
149
150 static void
151u_check_tree(u_header_T *uhp,
152 u_header_T *exp_uh_next,
153 u_header_T *exp_uh_alt_prev)
154{
155 u_entry_T *uep;
156
157 if (uhp == NULL)
158 return;
159 ++header_count;
160 if (uhp == curbuf->b_u_curhead && ++seen_b_u_curhead > 1)
161 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100162 emsg("b_u_curhead found twice (looping?)");
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000163 return;
164 }
165 if (uhp == curbuf->b_u_newhead && ++seen_b_u_newhead > 1)
166 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100167 emsg("b_u_newhead found twice (looping?)");
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000168 return;
169 }
170
171 if (uhp->uh_magic != UH_MAGIC)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100172 emsg("uh_magic wrong (may be using freed memory)");
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000173 else
174 {
175 /* Check pointers back are correct. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200176 if (uhp->uh_next.ptr != exp_uh_next)
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000177 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100178 emsg("uh_next wrong");
179 smsg("expected: 0x%x, actual: 0x%x",
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200180 exp_uh_next, uhp->uh_next.ptr);
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000181 }
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200182 if (uhp->uh_alt_prev.ptr != exp_uh_alt_prev)
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000183 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100184 emsg("uh_alt_prev wrong");
185 smsg("expected: 0x%x, actual: 0x%x",
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200186 exp_uh_alt_prev, uhp->uh_alt_prev.ptr);
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000187 }
188
189 /* Check the undo tree at this header. */
190 for (uep = uhp->uh_entry; uep != NULL; uep = uep->ue_next)
191 {
192 if (uep->ue_magic != UE_MAGIC)
193 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100194 emsg("ue_magic wrong (may be using freed memory)");
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000195 break;
196 }
197 }
198
199 /* Check the next alt tree. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200200 u_check_tree(uhp->uh_alt_next.ptr, uhp->uh_next.ptr, uhp);
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000201
202 /* Check the next header in this branch. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200203 u_check_tree(uhp->uh_prev.ptr, uhp, NULL);
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000204 }
205}
206
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200207 static void
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000208u_check(int newhead_may_be_NULL)
209{
210 seen_b_u_newhead = 0;
211 seen_b_u_curhead = 0;
212 header_count = 0;
213
214 u_check_tree(curbuf->b_u_oldhead, NULL, NULL);
215
216 if (seen_b_u_newhead == 0 && curbuf->b_u_oldhead != NULL
217 && !(newhead_may_be_NULL && curbuf->b_u_newhead == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100218 semsg("b_u_newhead invalid: 0x%x", curbuf->b_u_newhead);
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000219 if (curbuf->b_u_curhead != NULL && seen_b_u_curhead == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100220 semsg("b_u_curhead invalid: 0x%x", curbuf->b_u_curhead);
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000221 if (header_count != curbuf->b_u_numhead)
222 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100223 emsg("b_u_numhead invalid");
224 smsg("expected: %ld, actual: %ld",
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000225 (long)header_count, (long)curbuf->b_u_numhead);
226 }
227}
228#endif
229
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000231 * Save the current line for both the "u" and "U" command.
Bram Moolenaar687a29c2013-04-15 15:47:12 +0200232 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000233 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 */
235 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100236u_save_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237{
238 return (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
239 (linenr_T)(curwin->w_cursor.lnum + 1)));
240}
241
242/*
243 * Save the lines between "top" and "bot" for both the "u" and "U" command.
244 * "top" may be 0 and bot may be curbuf->b_ml.ml_line_count + 1.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200245 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 * Returns FAIL when lines could not be saved, OK otherwise.
247 */
248 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100249u_save(linenr_T top, linenr_T bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250{
251 if (undo_off)
252 return OK;
253
Bram Moolenaarefc81332018-07-13 16:31:19 +0200254 if (top >= bot || bot > curbuf->b_ml.ml_line_count + 1)
255 return FAIL; // rely on caller to give an error message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256
257 if (top + 2 == bot)
258 u_saveline((linenr_T)(top + 1));
259
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200260 return (u_savecommon(top, bot, (linenr_T)0, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261}
262
263/*
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200264 * Save the line "lnum" (used by ":s" and "~" command).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 * The line is replaced, so the new bottom line is lnum + 1.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200266 * Careful: may trigger autocommands that reload the buffer.
267 * Returns FAIL when lines could not be saved, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268 */
269 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100270u_savesub(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271{
272 if (undo_off)
273 return OK;
274
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200275 return (u_savecommon(lnum - 1, lnum + 1, lnum + 1, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276}
277
278/*
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200279 * A new line is inserted before line "lnum" (used by :s command).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 * The line is inserted, so the new bottom line is lnum + 1.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200281 * Careful: may trigger autocommands that reload the buffer.
282 * Returns FAIL when lines could not be saved, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 */
284 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100285u_inssub(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286{
287 if (undo_off)
288 return OK;
289
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200290 return (u_savecommon(lnum - 1, lnum, lnum + 1, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291}
292
293/*
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200294 * Save the lines "lnum" - "lnum" + nlines (used by delete command).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295 * The lines are deleted, so the new bottom line is lnum, unless the buffer
296 * becomes empty.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200297 * Careful: may trigger autocommands that reload the buffer.
298 * Returns FAIL when lines could not be saved, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299 */
300 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100301u_savedel(linenr_T lnum, long nlines)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302{
303 if (undo_off)
304 return OK;
305
306 return (u_savecommon(lnum - 1, lnum + nlines,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200307 nlines == curbuf->b_ml.ml_line_count ? 2 : lnum, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308}
309
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000310/*
311 * Return TRUE when undo is allowed. Otherwise give an error message and
312 * return FALSE.
313 */
Bram Moolenaarce6ef252006-07-12 19:49:41 +0000314 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100315undo_allowed(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000316{
317 /* Don't allow changes when 'modifiable' is off. */
318 if (!curbuf->b_p_ma)
319 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100320 emsg(_(e_modifiable));
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000321 return FALSE;
322 }
323
324#ifdef HAVE_SANDBOX
325 /* In the sandbox it's not allowed to change the text. */
326 if (sandbox != 0)
327 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100328 emsg(_(e_sandbox));
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000329 return FALSE;
330 }
331#endif
332
333 /* Don't allow changes in the buffer while editing the cmdline. The
334 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000335 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000336 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100337 emsg(_(e_secure));
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000338 return FALSE;
339 }
340
341 return TRUE;
342}
343
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200344/*
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100345 * Get the undolevle value for the current buffer.
346 */
347 static long
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100348get_undolevel(void)
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100349{
350 if (curbuf->b_p_ul == NO_LOCAL_UNDOLEVEL)
351 return p_ul;
352 return curbuf->b_p_ul;
353}
354
355/*
Bram Moolenaarccae4672019-01-04 15:09:57 +0100356 * u_save_line(): save an allocated copy of line "lnum" into "ul".
357 * Returns FAIL when out of memory.
358 */
359 static int
360u_save_line(undoline_T *ul, linenr_T lnum)
361{
362 char_u *line = ml_get(lnum);
363
364 if (curbuf->b_ml.ml_line_len == 0)
365 {
366 ul->ul_len = 1;
367 ul->ul_line = vim_strsave((char_u *)"");
368 }
369 else
370 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200371 // This uses the length in the memline, thus text properties are
372 // included.
Bram Moolenaarccae4672019-01-04 15:09:57 +0100373 ul->ul_len = curbuf->b_ml.ml_line_len;
374 ul->ul_line = vim_memsave(line, ul->ul_len);
375 }
376 return ul->ul_line == NULL ? FAIL : OK;
377}
378
379/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200380 * Common code for various ways to save text before a change.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200381 * "top" is the line above the first changed line.
382 * "bot" is the line below the last changed line.
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200383 * "newbot" is the new bottom line. Use zero when not known.
384 * "reload" is TRUE when saving for a buffer reload.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200385 * Careful: may trigger autocommands that reload the buffer.
386 * Returns FAIL when lines could not be saved, OK otherwise.
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200387 */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200388 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100389u_savecommon(
390 linenr_T top,
391 linenr_T bot,
392 linenr_T newbot,
393 int reload)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394{
Bram Moolenaar1e607892006-03-13 22:15:53 +0000395 linenr_T lnum;
396 long i;
397 u_header_T *uhp;
398 u_header_T *old_curhead;
399 u_entry_T *uep;
400 u_entry_T *prev_uep;
401 long size;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200403 if (!reload)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 {
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200405 /* When making changes is not allowed return FAIL. It's a crude way
406 * to make all change commands fail. */
407 if (!undo_allowed())
Bram Moolenaar009b2592004-10-24 19:18:58 +0000408 return FAIL;
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200409
410#ifdef FEAT_NETBEANS_INTG
411 /*
412 * Netbeans defines areas that cannot be modified. Bail out here when
413 * trying to change text in a guarded area.
414 */
415 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +0000416 {
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200417 if (netbeans_is_guarded(top, bot))
418 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100419 emsg(_(e_guarded));
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200420 return FAIL;
421 }
422 if (curbuf->b_p_ro)
423 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100424 emsg(_(e_nbreadonly));
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200425 return FAIL;
426 }
Bram Moolenaar009b2592004-10-24 19:18:58 +0000427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200429#ifdef FEAT_TERMINAL
430 /* A change in a terminal buffer removes the highlighting. */
431 term_change_in_curbuf();
432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200434 /*
435 * Saving text for undo means we are going to make a change. Give a
436 * warning for a read-only file before making the change, so that the
437 * FileChangedRO event can replace the buffer with a read-write version
438 * (e.g., obtained from a source control system).
439 */
440 change_warning(0);
441 if (bot > curbuf->b_ml.ml_line_count + 1)
442 {
443 /* This happens when the FileChangedRO autocommand changes the
444 * file in a way it becomes shorter. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100445 emsg(_("E881: Line count changed unexpectedly"));
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200446 return FAIL;
447 }
Bram Moolenaard04b7502010-07-08 22:27:55 +0200448 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200449
450#ifdef U_DEBUG
451 u_check(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452#endif
453
454 size = bot - top - 1;
455
456 /*
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200457 * If curbuf->b_u_synced == TRUE make a new header.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 */
459 if (curbuf->b_u_synced)
460 {
461#ifdef FEAT_JUMPLIST
462 /* Need to create new entry in b_changelist. */
463 curbuf->b_new_change = TRUE;
464#endif
465
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100466 if (get_undolevel() >= 0)
Bram Moolenaar1e607892006-03-13 22:15:53 +0000467 {
468 /*
469 * Make a new header entry. Do this first so that we don't mess
470 * up the undo info when out of memory.
471 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200472 uhp = U_ALLOC_LINE(sizeof(u_header_T));
Bram Moolenaar1e607892006-03-13 22:15:53 +0000473 if (uhp == NULL)
474 goto nomem;
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000475#ifdef U_DEBUG
476 uhp->uh_magic = UH_MAGIC;
477#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +0000478 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000479 else
480 uhp = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000481
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 /*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000483 * If we undid more than we redid, move the entry lists before and
484 * including curbuf->b_u_curhead to an alternate branch.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 */
Bram Moolenaar1e607892006-03-13 22:15:53 +0000486 old_curhead = curbuf->b_u_curhead;
487 if (old_curhead != NULL)
488 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200489 curbuf->b_u_newhead = old_curhead->uh_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000490 curbuf->b_u_curhead = NULL;
491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492
493 /*
494 * free headers to keep the size right
495 */
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100496 while (curbuf->b_u_numhead > get_undolevel()
497 && curbuf->b_u_oldhead != NULL)
Bram Moolenaar1e607892006-03-13 22:15:53 +0000498 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000499 u_header_T *uhfree = curbuf->b_u_oldhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000500
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000501 if (uhfree == old_curhead)
502 /* Can't reconnect the branch, delete all of it. */
503 u_freebranch(curbuf, uhfree, &old_curhead);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200504 else if (uhfree->uh_alt_next.ptr == NULL)
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000505 /* There is no branch, only free one header. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000506 u_freeheader(curbuf, uhfree, &old_curhead);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000507 else
508 {
509 /* Free the oldest alternate branch as a whole. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200510 while (uhfree->uh_alt_next.ptr != NULL)
511 uhfree = uhfree->uh_alt_next.ptr;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000512 u_freebranch(curbuf, uhfree, &old_curhead);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000513 }
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000514#ifdef U_DEBUG
515 u_check(TRUE);
516#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +0000517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000519 if (uhp == NULL) /* no undo at all */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 {
Bram Moolenaar1e607892006-03-13 22:15:53 +0000521 if (old_curhead != NULL)
522 u_freebranch(curbuf, old_curhead, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 curbuf->b_u_synced = FALSE;
524 return OK;
525 }
526
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200527 uhp->uh_prev.ptr = NULL;
528 uhp->uh_next.ptr = curbuf->b_u_newhead;
529 uhp->uh_alt_next.ptr = old_curhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000530 if (old_curhead != NULL)
531 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200532 uhp->uh_alt_prev.ptr = old_curhead->uh_alt_prev.ptr;
533 if (uhp->uh_alt_prev.ptr != NULL)
534 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = uhp;
535 old_curhead->uh_alt_prev.ptr = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000536 if (curbuf->b_u_oldhead == old_curhead)
537 curbuf->b_u_oldhead = uhp;
538 }
Bram Moolenaar89ed3df2007-01-09 19:23:12 +0000539 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200540 uhp->uh_alt_prev.ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 if (curbuf->b_u_newhead != NULL)
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200542 curbuf->b_u_newhead->uh_prev.ptr = uhp;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000543
Bram Moolenaarca003e12006-03-17 23:19:38 +0000544 uhp->uh_seq = ++curbuf->b_u_seq_last;
545 curbuf->b_u_seq_cur = uhp->uh_seq;
Bram Moolenaar170b10b2016-07-29 16:15:27 +0200546 uhp->uh_time = vim_time();
Bram Moolenaara800b422010-06-27 01:15:55 +0200547 uhp->uh_save_nr = 0;
548 curbuf->b_u_time_cur = uhp->uh_time + 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000549
Bram Moolenaar1e607892006-03-13 22:15:53 +0000550 uhp->uh_walk = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 uhp->uh_entry = NULL;
552 uhp->uh_getbot_entry = NULL;
553 uhp->uh_cursor = curwin->w_cursor; /* save cursor pos. for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 if (virtual_active() && curwin->w_cursor.coladd > 0)
555 uhp->uh_cursor_vcol = getviscol();
556 else
557 uhp->uh_cursor_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558
559 /* save changed and buffer empty flag for undo */
560 uhp->uh_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
561 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
562
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000563 /* save named marks and Visual marks for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 mch_memmove(uhp->uh_namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000565 uhp->uh_visual = curbuf->b_visual;
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000566
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 curbuf->b_u_newhead = uhp;
568 if (curbuf->b_u_oldhead == NULL)
569 curbuf->b_u_oldhead = uhp;
570 ++curbuf->b_u_numhead;
571 }
572 else
573 {
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100574 if (get_undolevel() < 0) /* no undo at all */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 return OK;
576
577 /*
578 * When saving a single line, and it has been saved just before, it
579 * doesn't make sense saving it again. Saves a lot of memory when
580 * making lots of changes inside the same line.
581 * This is only possible if the previous change didn't increase or
582 * decrease the number of lines.
583 * Check the ten last changes. More doesn't make sense and takes too
584 * long.
585 */
586 if (size == 1)
587 {
588 uep = u_get_headentry();
589 prev_uep = NULL;
590 for (i = 0; i < 10; ++i)
591 {
592 if (uep == NULL)
593 break;
594
595 /* If lines have been inserted/deleted we give up.
596 * Also when the line was included in a multi-line save. */
597 if ((curbuf->b_u_newhead->uh_getbot_entry != uep
598 ? (uep->ue_top + uep->ue_size + 1
599 != (uep->ue_bot == 0
600 ? curbuf->b_ml.ml_line_count + 1
601 : uep->ue_bot))
602 : uep->ue_lcount != curbuf->b_ml.ml_line_count)
603 || (uep->ue_size > 1
604 && top >= uep->ue_top
605 && top + 2 <= uep->ue_top + uep->ue_size + 1))
606 break;
607
608 /* If it's the same line we can skip saving it again. */
609 if (uep->ue_size == 1 && uep->ue_top == top)
610 {
611 if (i > 0)
612 {
613 /* It's not the last entry: get ue_bot for the last
614 * entry now. Following deleted/inserted lines go to
615 * the re-used entry. */
616 u_getbot();
617 curbuf->b_u_synced = FALSE;
618
619 /* Move the found entry to become the last entry. The
620 * order of undo/redo doesn't matter for the entries
621 * we move it over, since they don't change the line
622 * count and don't include this line. It does matter
623 * for the found entry if the line count is changed by
624 * the executed command. */
625 prev_uep->ue_next = uep->ue_next;
626 uep->ue_next = curbuf->b_u_newhead->uh_entry;
627 curbuf->b_u_newhead->uh_entry = uep;
628 }
629
630 /* The executed command may change the line count. */
631 if (newbot != 0)
632 uep->ue_bot = newbot;
633 else if (bot > curbuf->b_ml.ml_line_count)
634 uep->ue_bot = 0;
635 else
636 {
637 uep->ue_lcount = curbuf->b_ml.ml_line_count;
638 curbuf->b_u_newhead->uh_getbot_entry = uep;
639 }
640 return OK;
641 }
642 prev_uep = uep;
643 uep = uep->ue_next;
644 }
645 }
646
647 /* find line number for ue_bot for previous u_save() */
648 u_getbot();
649 }
650
Bram Moolenaar4f974752019-02-17 17:44:42 +0100651#if !defined(UNIX) && !defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100653 * With Amiga we can't handle big undo's, because
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 * then u_alloc_line would have to allocate a block larger than 32K
655 */
656 if (size >= 8000)
657 goto nomem;
658#endif
659
660 /*
661 * add lines in front of entry list
662 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200663 uep = U_ALLOC_LINE(sizeof(u_entry_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 if (uep == NULL)
665 goto nomem;
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200666 vim_memset(uep, 0, sizeof(u_entry_T));
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000667#ifdef U_DEBUG
668 uep->ue_magic = UE_MAGIC;
669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670
671 uep->ue_size = size;
672 uep->ue_top = top;
673 if (newbot != 0)
674 uep->ue_bot = newbot;
675 /*
676 * Use 0 for ue_bot if bot is below last line.
677 * Otherwise we have to compute ue_bot later.
678 */
679 else if (bot > curbuf->b_ml.ml_line_count)
680 uep->ue_bot = 0;
681 else
682 {
683 uep->ue_lcount = curbuf->b_ml.ml_line_count;
684 curbuf->b_u_newhead->uh_getbot_entry = uep;
685 }
686
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000687 if (size > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200689 if ((uep->ue_array = U_ALLOC_LINE(sizeof(undoline_T) * size)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 {
691 u_freeentry(uep, 0L);
692 goto nomem;
693 }
694 for (i = 0, lnum = top + 1; i < size; ++i)
695 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000696 fast_breakcheck();
697 if (got_int)
698 {
699 u_freeentry(uep, i);
700 return FAIL;
701 }
Bram Moolenaarccae4672019-01-04 15:09:57 +0100702 if (u_save_line(&uep->ue_array[i], lnum++) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 {
704 u_freeentry(uep, i);
705 goto nomem;
706 }
707 }
708 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000709 else
710 uep->ue_array = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 uep->ue_next = curbuf->b_u_newhead->uh_entry;
712 curbuf->b_u_newhead->uh_entry = uep;
713 curbuf->b_u_synced = FALSE;
714 undo_undoes = FALSE;
715
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000716#ifdef U_DEBUG
717 u_check(FALSE);
718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 return OK;
720
721nomem:
722 msg_silent = 0; /* must display the prompt */
723 if (ask_yesno((char_u *)_("No undo possible; continue anyway"), TRUE)
724 == 'y')
725 {
726 undo_off = TRUE; /* will be reset when character typed */
727 return OK;
728 }
729 do_outofmem_msg((long_u)0);
730 return FAIL;
731}
732
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200733#if defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200734
Bram Moolenaar9db58062010-05-29 20:33:07 +0200735# define UF_START_MAGIC "Vim\237UnDo\345" /* magic at start of undofile */
736# define UF_START_MAGIC_LEN 9
737# define UF_HEADER_MAGIC 0x5fd0 /* magic at start of header */
738# define UF_HEADER_END_MAGIC 0xe7aa /* magic after last header */
739# define UF_ENTRY_MAGIC 0xf518 /* magic at start of entry */
740# define UF_ENTRY_END_MAGIC 0x3581 /* magic after last entry */
Bram Moolenaara800b422010-06-27 01:15:55 +0200741# define UF_VERSION 2 /* 2-byte undofile version number */
Bram Moolenaara800b422010-06-27 01:15:55 +0200742# define UF_VERSION_CRYPT 0x8002 /* idem, encrypted */
743
744/* extra fields for header */
745# define UF_LAST_SAVE_NR 1
746
747/* extra fields for uhp */
748# define UHP_SAVE_NR 1
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200749
Bram Moolenaarcdf04202010-05-29 15:11:47 +0200750static char_u e_not_open[] = N_("E828: Cannot open undo file for writing: %s");
Bram Moolenaarcdf04202010-05-29 15:11:47 +0200751
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200752/*
753 * Compute the hash for the current buffer text into hash[UNDO_HASH_SIZE].
754 */
755 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100756u_compute_hash(char_u *hash)
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200757{
758 context_sha256_T ctx;
759 linenr_T lnum;
760 char_u *p;
761
762 sha256_start(&ctx);
Bram Moolenaarae7ba982011-12-08 15:14:09 +0100763 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200764 {
765 p = ml_get(lnum);
Bram Moolenaar442b4222010-05-24 21:34:22 +0200766 sha256_update(&ctx, p, (UINT32_T)(STRLEN(p) + 1));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200767 }
768 sha256_finish(&ctx, hash);
769}
770
771/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200772 * Return an allocated string of the full path of the target undofile.
773 * When "reading" is TRUE find the file to read, go over all directories in
774 * 'undodir'.
775 * When "reading" is FALSE use the first name where the directory exists.
Bram Moolenaar9db58062010-05-29 20:33:07 +0200776 * Returns NULL when there is no place to write or no file to read.
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200777 */
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200778 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100779u_get_undo_file_name(char_u *buf_ffname, int reading)
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200780{
781 char_u *dirp;
782 char_u dir_name[IOSIZE + 1];
783 char_u *munged_name = NULL;
784 char_u *undo_file_name = NULL;
785 int dir_len;
786 char_u *p;
Bram Moolenaar8767f522016-07-01 17:17:39 +0200787 stat_T st;
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200788 char_u *ffname = buf_ffname;
789#ifdef HAVE_READLINK
790 char_u fname_buf[MAXPATHL];
791#endif
792
793 if (ffname == NULL)
794 return NULL;
795
796#ifdef HAVE_READLINK
797 /* Expand symlink in the file name, so that we put the undo file with the
798 * actual file instead of with the symlink. */
799 if (resolve_symlink(ffname, fname_buf) == OK)
800 ffname = fname_buf;
801#endif
802
803 /* Loop over 'undodir'. When reading find the first file that exists.
804 * When not reading use the first directory that exists or ".". */
805 dirp = p_udir;
806 while (*dirp != NUL)
807 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200808 dir_len = copy_option_part(&dirp, dir_name, IOSIZE, ",");
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200809 if (dir_len == 1 && dir_name[0] == '.')
810 {
811 /* Use same directory as the ffname,
812 * "dir/name" -> "dir/.name.un~" */
Bram Moolenaar442b4222010-05-24 21:34:22 +0200813 undo_file_name = vim_strnsave(ffname, (int)(STRLEN(ffname) + 5));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200814 if (undo_file_name == NULL)
815 break;
816 p = gettail(undo_file_name);
Bram Moolenaar206f0112014-03-12 16:51:55 +0100817#ifdef VMS
818 /* VMS can not handle more than one dot in the filenames
819 * use "dir/name" -> "dir/_un_name" - add _un_
820 * at the beginning to keep the extension */
821 mch_memmove(p + 4, p, STRLEN(p) + 1);
822 mch_memmove(p, "_un_", 4);
823
824#else
825 /* Use same directory as the ffname,
826 * "dir/name" -> "dir/.name.un~" */
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200827 mch_memmove(p + 1, p, STRLEN(p) + 1);
828 *p = '.';
829 STRCAT(p, ".un~");
Bram Moolenaar206f0112014-03-12 16:51:55 +0100830#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200831 }
832 else
833 {
834 dir_name[dir_len] = NUL;
835 if (mch_isdir(dir_name))
836 {
837 if (munged_name == NULL)
838 {
839 munged_name = vim_strsave(ffname);
840 if (munged_name == NULL)
841 return NULL;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100842 for (p = munged_name; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200843 if (vim_ispathsep(*p))
844 *p = '%';
845 }
846 undo_file_name = concat_fnames(dir_name, munged_name, TRUE);
847 }
848 }
849
850 /* When reading check if the file exists. */
851 if (undo_file_name != NULL && (!reading
852 || mch_stat((char *)undo_file_name, &st) >= 0))
853 break;
Bram Moolenaard23a8232018-02-10 18:45:26 +0100854 VIM_CLEAR(undo_file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200855 }
856
857 vim_free(munged_name);
858 return undo_file_name;
859}
860
Bram Moolenaar9db58062010-05-29 20:33:07 +0200861 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100862corruption_error(char *mesg, char_u *file_name)
Bram Moolenaar9db58062010-05-29 20:33:07 +0200863{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100864 semsg(_("E825: Corrupted undo file (%s): %s"), mesg, file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +0200865}
866
867 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100868u_free_uhp(u_header_T *uhp)
Bram Moolenaar9db58062010-05-29 20:33:07 +0200869{
870 u_entry_T *nuep;
871 u_entry_T *uep;
872
873 uep = uhp->uh_entry;
874 while (uep != NULL)
875 {
876 nuep = uep->ue_next;
877 u_freeentry(uep, uep->ue_size);
878 uep = nuep;
879 }
880 vim_free(uhp);
881}
882
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200883/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200884 * Write a sequence of bytes to the undo file.
885 * Buffers and encrypts as needed.
886 * Returns OK or FAIL.
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200887 */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200888 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100889undo_write(bufinfo_T *bi, char_u *ptr, size_t len)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200890{
891#ifdef FEAT_CRYPT
892 if (bi->bi_buffer != NULL)
893 {
894 size_t len_todo = len;
895 char_u *p = ptr;
896
897 while (bi->bi_used + len_todo >= CRYPT_BUF_SIZE)
898 {
899 size_t n = CRYPT_BUF_SIZE - bi->bi_used;
900
901 mch_memmove(bi->bi_buffer + bi->bi_used, p, n);
902 len_todo -= n;
903 p += n;
904 bi->bi_used = CRYPT_BUF_SIZE;
905 if (undo_flush(bi) == FAIL)
906 return FAIL;
907 }
908 if (len_todo > 0)
909 {
910 mch_memmove(bi->bi_buffer + bi->bi_used, p, len_todo);
911 bi->bi_used += len_todo;
912 }
913 return OK;
914 }
915#endif
916 if (fwrite(ptr, len, (size_t)1, bi->bi_fp) != 1)
917 return FAIL;
918 return OK;
919}
920
921#ifdef FEAT_CRYPT
922 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100923undo_flush(bufinfo_T *bi)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200924{
Bram Moolenaar829aa642017-08-23 22:32:35 +0200925 if (bi->bi_buffer != NULL && bi->bi_state != NULL && bi->bi_used > 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200926 {
927 crypt_encode_inplace(bi->bi_state, bi->bi_buffer, bi->bi_used);
928 if (fwrite(bi->bi_buffer, bi->bi_used, (size_t)1, bi->bi_fp) != 1)
929 return FAIL;
930 bi->bi_used = 0;
931 }
932 return OK;
933}
934#endif
935
936/*
937 * Write "ptr[len]" and crypt the bytes when needed.
938 * Returns OK or FAIL.
939 */
940 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100941fwrite_crypt(bufinfo_T *bi, char_u *ptr, size_t len)
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200942{
943#ifdef FEAT_CRYPT
944 char_u *copy;
945 char_u small_buf[100];
946 size_t i;
947
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200948 if (bi->bi_state != NULL && bi->bi_buffer == NULL)
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200949 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200950 /* crypting every piece of text separately */
951 if (len < 100)
952 copy = small_buf; /* no malloc()/free() for short strings */
953 else
954 {
955 copy = lalloc(len, FALSE);
956 if (copy == NULL)
957 return 0;
958 }
959 crypt_encode(bi->bi_state, ptr, len, copy);
960 i = fwrite(copy, len, (size_t)1, bi->bi_fp);
961 if (copy != small_buf)
962 vim_free(copy);
963 return i == 1 ? OK : FAIL;
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200964 }
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200965#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200966 return undo_write(bi, ptr, len);
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200967}
968
969/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200970 * Write a number, MSB first, in "len" bytes.
971 * Must match with undo_read_?c() functions.
972 * Returns OK or FAIL.
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200973 */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200974 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100975undo_write_bytes(bufinfo_T *bi, long_u nr, int len)
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200976{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200977 char_u buf[8];
978 int i;
979 int bufi = 0;
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200980
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200981 for (i = len - 1; i >= 0; --i)
Bram Moolenaar9b8f0212014-08-13 22:05:53 +0200982 buf[bufi++] = (char_u)(nr >> (i * 8));
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200983 return undo_write(bi, buf, (size_t)len);
984}
985
986/*
987 * Write the pointer to an undo header. Instead of writing the pointer itself
988 * we use the sequence number of the header. This is converted back to
989 * pointers when reading. */
990 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100991put_header_ptr(bufinfo_T *bi, u_header_T *uhp)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200992{
993 undo_write_bytes(bi, (long_u)(uhp != NULL ? uhp->uh_seq : 0), 4);
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200994}
995
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +0200996 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100997undo_read_4c(bufinfo_T *bi)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200998{
999#ifdef FEAT_CRYPT
1000 if (bi->bi_buffer != NULL)
1001 {
1002 char_u buf[4];
1003 int n;
1004
1005 undo_read(bi, buf, (size_t)4);
Bram Moolenaar35169282014-09-11 22:50:09 +02001006 n = ((unsigned)buf[0] << 24) + (buf[1] << 16) + (buf[2] << 8) + buf[3];
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001007 return n;
1008 }
1009#endif
1010 return get4c(bi->bi_fp);
1011}
1012
1013 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001014undo_read_2c(bufinfo_T *bi)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001015{
1016#ifdef FEAT_CRYPT
1017 if (bi->bi_buffer != NULL)
1018 {
1019 char_u buf[2];
1020 int n;
1021
1022 undo_read(bi, buf, (size_t)2);
1023 n = (buf[0] << 8) + buf[1];
1024 return n;
1025 }
1026#endif
1027 return get2c(bi->bi_fp);
1028}
1029
1030 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001031undo_read_byte(bufinfo_T *bi)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001032{
1033#ifdef FEAT_CRYPT
1034 if (bi->bi_buffer != NULL)
1035 {
1036 char_u buf[1];
1037
1038 undo_read(bi, buf, (size_t)1);
1039 return buf[0];
1040 }
1041#endif
1042 return getc(bi->bi_fp);
1043}
1044
1045 static time_t
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001046undo_read_time(bufinfo_T *bi)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001047{
1048#ifdef FEAT_CRYPT
1049 if (bi->bi_buffer != NULL)
1050 {
1051 char_u buf[8];
1052 time_t n = 0;
1053 int i;
1054
1055 undo_read(bi, buf, (size_t)8);
1056 for (i = 0; i < 8; ++i)
1057 n = (n << 8) + buf[i];
1058 return n;
1059 }
1060#endif
1061 return get8ctime(bi->bi_fp);
1062}
1063
1064/*
1065 * Read "buffer[size]" from the undo file.
1066 * Return OK or FAIL.
1067 */
1068 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001069undo_read(bufinfo_T *bi, char_u *buffer, size_t size)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001070{
Bram Moolenaar56f2db52017-06-11 23:09:15 +02001071 int retval = OK;
1072
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001073#ifdef FEAT_CRYPT
1074 if (bi->bi_buffer != NULL)
1075 {
Bram Moolenaar9b8f0212014-08-13 22:05:53 +02001076 int size_todo = (int)size;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001077 char_u *p = buffer;
1078
1079 while (size_todo > 0)
1080 {
1081 size_t n;
1082
1083 if (bi->bi_used >= bi->bi_avail)
1084 {
1085 n = fread(bi->bi_buffer, 1, (size_t)CRYPT_BUF_SIZE, bi->bi_fp);
Bram Moolenaar55952d42016-11-05 14:58:34 +01001086 if (n == 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001087 {
Bram Moolenaar56f2db52017-06-11 23:09:15 +02001088 retval = FAIL;
1089 break;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001090 }
1091 bi->bi_avail = n;
1092 bi->bi_used = 0;
1093 crypt_decode_inplace(bi->bi_state, bi->bi_buffer, bi->bi_avail);
1094 }
1095 n = size_todo;
1096 if (n > bi->bi_avail - bi->bi_used)
1097 n = bi->bi_avail - bi->bi_used;
1098 mch_memmove(p, bi->bi_buffer + bi->bi_used, n);
1099 bi->bi_used += n;
Bram Moolenaar9b8f0212014-08-13 22:05:53 +02001100 size_todo -= (int)n;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001101 p += n;
1102 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001103 }
Bram Moolenaar56f2db52017-06-11 23:09:15 +02001104 else
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001105#endif
1106 if (fread(buffer, (size_t)size, 1, bi->bi_fp) != 1)
Bram Moolenaar56f2db52017-06-11 23:09:15 +02001107 retval = FAIL;
1108
1109 if (retval == FAIL)
1110 /* Error may be checked for only later. Fill with zeros,
1111 * so that the reader won't use garbage. */
1112 vim_memset(buffer, 0, size);
1113 return retval;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001114}
1115
1116/*
1117 * Read a string of length "len" from "bi->bi_fd".
1118 * "len" can be zero to allocate an empty line.
1119 * Decrypt the bytes if needed.
1120 * Append a NUL.
1121 * Returns a pointer to allocated memory or NULL for failure.
1122 */
1123 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001124read_string_decrypt(bufinfo_T *bi, int len)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001125{
Bram Moolenaar964b3742019-05-24 18:54:09 +02001126 char_u *ptr = alloc(len + 1);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001127
1128 if (ptr != NULL)
1129 {
1130 if (len > 0 && undo_read(bi, ptr, len) == FAIL)
1131 {
1132 vim_free(ptr);
1133 return NULL;
1134 }
Bram Moolenaarccae4672019-01-04 15:09:57 +01001135 // In case there are text properties there already is a NUL, but
1136 // checking for that is more expensive than just adding a dummy byte.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001137 ptr[len] = NUL;
1138#ifdef FEAT_CRYPT
1139 if (bi->bi_state != NULL && bi->bi_buffer == NULL)
1140 crypt_decode_inplace(bi->bi_state, ptr, len);
1141#endif
1142 }
1143 return ptr;
1144}
1145
1146/*
1147 * Writes the (not encrypted) header and initializes encryption if needed.
1148 */
1149 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001150serialize_header(bufinfo_T *bi, char_u *hash)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001151{
Bram Moolenaarccae4672019-01-04 15:09:57 +01001152 long len;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001153 buf_T *buf = bi->bi_buf;
1154 FILE *fp = bi->bi_fp;
1155 char_u time_buf[8];
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001156
1157 /* Start writing, first the magic marker and undo info version. */
1158 if (fwrite(UF_START_MAGIC, (size_t)UF_START_MAGIC_LEN, (size_t)1, fp) != 1)
1159 return FAIL;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001160
1161 /* If the buffer is encrypted then all text bytes following will be
1162 * encrypted. Numbers and other info is not crypted. */
1163#ifdef FEAT_CRYPT
Bram Moolenaarfa0ff9a2010-07-25 16:05:19 +02001164 if (*buf->b_p_key != NUL)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001165 {
1166 char_u *header;
1167 int header_len;
1168
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001169 undo_write_bytes(bi, (long_u)UF_VERSION_CRYPT, 2);
1170 bi->bi_state = crypt_create_for_writing(crypt_get_method_nr(buf),
1171 buf->b_p_key, &header, &header_len);
1172 if (bi->bi_state == NULL)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001173 return FAIL;
Bram Moolenaarccae4672019-01-04 15:09:57 +01001174 len = (long)fwrite(header, (size_t)header_len, (size_t)1, fp);
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001175 vim_free(header);
1176 if (len != 1)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001177 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001178 crypt_free_state(bi->bi_state);
1179 bi->bi_state = NULL;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001180 return FAIL;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001181 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001182
1183 if (crypt_whole_undofile(crypt_get_method_nr(buf)))
1184 {
1185 bi->bi_buffer = alloc(CRYPT_BUF_SIZE);
1186 if (bi->bi_buffer == NULL)
1187 {
1188 crypt_free_state(bi->bi_state);
1189 bi->bi_state = NULL;
1190 return FAIL;
1191 }
1192 bi->bi_used = 0;
1193 }
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001194 }
1195 else
1196#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001197 undo_write_bytes(bi, (long_u)UF_VERSION, 2);
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001198
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001199
1200 /* Write a hash of the buffer text, so that we can verify it is still the
1201 * same when reading the buffer text. */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001202 if (undo_write(bi, hash, (size_t)UNDO_HASH_SIZE) == FAIL)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001203 return FAIL;
1204
1205 /* buffer-specific data */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001206 undo_write_bytes(bi, (long_u)buf->b_ml.ml_line_count, 4);
Bram Moolenaarccae4672019-01-04 15:09:57 +01001207 len = buf->b_u_line_ptr.ul_line == NULL
Bram Moolenaar8aef43b2019-01-08 20:14:35 +01001208 ? 0L : (long)STRLEN(buf->b_u_line_ptr.ul_line);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001209 undo_write_bytes(bi, (long_u)len, 4);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +01001210 if (len > 0 && fwrite_crypt(bi, buf->b_u_line_ptr.ul_line, (size_t)len)
1211 == FAIL)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001212 return FAIL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001213 undo_write_bytes(bi, (long_u)buf->b_u_line_lnum, 4);
1214 undo_write_bytes(bi, (long_u)buf->b_u_line_colnr, 4);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001215
1216 /* Undo structures header data */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001217 put_header_ptr(bi, buf->b_u_oldhead);
1218 put_header_ptr(bi, buf->b_u_newhead);
1219 put_header_ptr(bi, buf->b_u_curhead);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001220
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001221 undo_write_bytes(bi, (long_u)buf->b_u_numhead, 4);
1222 undo_write_bytes(bi, (long_u)buf->b_u_seq_last, 4);
1223 undo_write_bytes(bi, (long_u)buf->b_u_seq_cur, 4);
1224 time_to_bytes(buf->b_u_time_cur, time_buf);
1225 undo_write(bi, time_buf, 8);
Bram Moolenaara800b422010-06-27 01:15:55 +02001226
1227 /* Optional fields. */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001228 undo_write_bytes(bi, 4, 1);
1229 undo_write_bytes(bi, UF_LAST_SAVE_NR, 1);
1230 undo_write_bytes(bi, (long_u)buf->b_u_save_nr_last, 4);
Bram Moolenaara800b422010-06-27 01:15:55 +02001231
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001232 undo_write_bytes(bi, 0, 1); /* end marker */
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001233
1234 return OK;
1235}
1236
1237 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001238serialize_uhp(bufinfo_T *bi, u_header_T *uhp)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001239{
1240 int i;
1241 u_entry_T *uep;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001242 char_u time_buf[8];
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001243
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001244 if (undo_write_bytes(bi, (long_u)UF_HEADER_MAGIC, 2) == FAIL)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001245 return FAIL;
1246
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001247 put_header_ptr(bi, uhp->uh_next.ptr);
1248 put_header_ptr(bi, uhp->uh_prev.ptr);
1249 put_header_ptr(bi, uhp->uh_alt_next.ptr);
1250 put_header_ptr(bi, uhp->uh_alt_prev.ptr);
1251 undo_write_bytes(bi, uhp->uh_seq, 4);
1252 serialize_pos(bi, uhp->uh_cursor);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001253 undo_write_bytes(bi, (long_u)uhp->uh_cursor_vcol, 4);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001254 undo_write_bytes(bi, (long_u)uhp->uh_flags, 2);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001255 /* Assume NMARKS will stay the same. */
1256 for (i = 0; i < NMARKS; ++i)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001257 serialize_pos(bi, uhp->uh_namedm[i]);
1258 serialize_visualinfo(bi, &uhp->uh_visual);
1259 time_to_bytes(uhp->uh_time, time_buf);
1260 undo_write(bi, time_buf, 8);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001261
Bram Moolenaara800b422010-06-27 01:15:55 +02001262 /* Optional fields. */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001263 undo_write_bytes(bi, 4, 1);
1264 undo_write_bytes(bi, UHP_SAVE_NR, 1);
1265 undo_write_bytes(bi, (long_u)uhp->uh_save_nr, 4);
Bram Moolenaara800b422010-06-27 01:15:55 +02001266
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001267 undo_write_bytes(bi, 0, 1); /* end marker */
Bram Moolenaara800b422010-06-27 01:15:55 +02001268
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001269 /* Write all the entries. */
1270 for (uep = uhp->uh_entry; uep != NULL; uep = uep->ue_next)
1271 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001272 undo_write_bytes(bi, (long_u)UF_ENTRY_MAGIC, 2);
1273 if (serialize_uep(bi, uep) == FAIL)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001274 return FAIL;
1275 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001276 undo_write_bytes(bi, (long_u)UF_ENTRY_END_MAGIC, 2);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001277 return OK;
1278}
1279
1280 static u_header_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001281unserialize_uhp(bufinfo_T *bi, char_u *file_name)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001282{
1283 u_header_T *uhp;
1284 int i;
1285 u_entry_T *uep, *last_uep;
1286 int c;
1287 int error;
1288
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001289 uhp = U_ALLOC_LINE(sizeof(u_header_T));
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001290 if (uhp == NULL)
1291 return NULL;
1292 vim_memset(uhp, 0, sizeof(u_header_T));
1293#ifdef U_DEBUG
1294 uhp->uh_magic = UH_MAGIC;
1295#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001296 uhp->uh_next.seq = undo_read_4c(bi);
1297 uhp->uh_prev.seq = undo_read_4c(bi);
1298 uhp->uh_alt_next.seq = undo_read_4c(bi);
1299 uhp->uh_alt_prev.seq = undo_read_4c(bi);
1300 uhp->uh_seq = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001301 if (uhp->uh_seq <= 0)
1302 {
1303 corruption_error("uh_seq", file_name);
1304 vim_free(uhp);
1305 return NULL;
1306 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001307 unserialize_pos(bi, &uhp->uh_cursor);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001308 uhp->uh_cursor_vcol = undo_read_4c(bi);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001309 uhp->uh_flags = undo_read_2c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001310 for (i = 0; i < NMARKS; ++i)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001311 unserialize_pos(bi, &uhp->uh_namedm[i]);
1312 unserialize_visualinfo(bi, &uhp->uh_visual);
1313 uhp->uh_time = undo_read_time(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001314
Bram Moolenaara800b422010-06-27 01:15:55 +02001315 /* Optional fields. */
Bram Moolenaar69154f22010-07-18 21:42:34 +02001316 for (;;)
1317 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001318 int len = undo_read_byte(bi);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001319 int what;
Bram Moolenaara800b422010-06-27 01:15:55 +02001320
Bram Moolenaarfb06d762019-08-04 18:55:35 +02001321 if (len == EOF)
1322 {
1323 corruption_error("truncated", file_name);
1324 u_free_uhp(uhp);
1325 return NULL;
1326 }
Bram Moolenaar69154f22010-07-18 21:42:34 +02001327 if (len == 0)
1328 break;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001329 what = undo_read_byte(bi);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001330 switch (what)
1331 {
1332 case UHP_SAVE_NR:
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001333 uhp->uh_save_nr = undo_read_4c(bi);
Bram Moolenaara800b422010-06-27 01:15:55 +02001334 break;
Bram Moolenaar69154f22010-07-18 21:42:34 +02001335 default:
1336 /* field not supported, skip */
1337 while (--len >= 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001338 (void)undo_read_byte(bi);
Bram Moolenaara800b422010-06-27 01:15:55 +02001339 }
Bram Moolenaar69154f22010-07-18 21:42:34 +02001340 }
Bram Moolenaara800b422010-06-27 01:15:55 +02001341
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001342 /* Unserialize the uep list. */
1343 last_uep = NULL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001344 while ((c = undo_read_2c(bi)) == UF_ENTRY_MAGIC)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001345 {
1346 error = FALSE;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001347 uep = unserialize_uep(bi, &error, file_name);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001348 if (last_uep == NULL)
1349 uhp->uh_entry = uep;
1350 else
1351 last_uep->ue_next = uep;
1352 last_uep = uep;
1353 if (uep == NULL || error)
1354 {
1355 u_free_uhp(uhp);
1356 return NULL;
1357 }
1358 }
1359 if (c != UF_ENTRY_END_MAGIC)
1360 {
1361 corruption_error("entry end", file_name);
1362 u_free_uhp(uhp);
1363 return NULL;
1364 }
1365
1366 return uhp;
1367}
1368
Bram Moolenaar9db58062010-05-29 20:33:07 +02001369/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001370 * Serialize "uep".
Bram Moolenaar9db58062010-05-29 20:33:07 +02001371 */
1372 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001373serialize_uep(
1374 bufinfo_T *bi,
1375 u_entry_T *uep)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001376{
1377 int i;
1378 size_t len;
1379
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001380 undo_write_bytes(bi, (long_u)uep->ue_top, 4);
1381 undo_write_bytes(bi, (long_u)uep->ue_bot, 4);
1382 undo_write_bytes(bi, (long_u)uep->ue_lcount, 4);
1383 undo_write_bytes(bi, (long_u)uep->ue_size, 4);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001384 for (i = 0; i < uep->ue_size; ++i)
1385 {
Bram Moolenaarccae4672019-01-04 15:09:57 +01001386 // Text is written without the text properties, since we cannot restore
1387 // the text property types.
1388 len = STRLEN(uep->ue_array[i].ul_line);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001389 if (undo_write_bytes(bi, (long_u)len, 4) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001390 return FAIL;
Bram Moolenaarccae4672019-01-04 15:09:57 +01001391 if (len > 0 && fwrite_crypt(bi, uep->ue_array[i].ul_line, len) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001392 return FAIL;
1393 }
1394 return OK;
1395}
1396
1397 static u_entry_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001398unserialize_uep(bufinfo_T *bi, int *error, char_u *file_name)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001399{
1400 int i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001401 u_entry_T *uep;
Bram Moolenaarccae4672019-01-04 15:09:57 +01001402 undoline_T *array = NULL;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001403 char_u *line;
1404 int line_len;
1405
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001406 uep = U_ALLOC_LINE(sizeof(u_entry_T));
Bram Moolenaar9db58062010-05-29 20:33:07 +02001407 if (uep == NULL)
1408 return NULL;
1409 vim_memset(uep, 0, sizeof(u_entry_T));
1410#ifdef U_DEBUG
1411 uep->ue_magic = UE_MAGIC;
1412#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001413 uep->ue_top = undo_read_4c(bi);
1414 uep->ue_bot = undo_read_4c(bi);
1415 uep->ue_lcount = undo_read_4c(bi);
1416 uep->ue_size = undo_read_4c(bi);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001417 if (uep->ue_size > 0)
1418 {
Bram Moolenaar0c8485f2017-02-26 18:17:10 +01001419 if (uep->ue_size < LONG_MAX / (int)sizeof(char_u *))
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001420 array = U_ALLOC_LINE(sizeof(undoline_T) * uep->ue_size);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001421 if (array == NULL)
1422 {
1423 *error = TRUE;
1424 return uep;
1425 }
Bram Moolenaarccae4672019-01-04 15:09:57 +01001426 vim_memset(array, 0, sizeof(undoline_T) * uep->ue_size);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001427 }
1428 uep->ue_array = array;
1429
1430 for (i = 0; i < uep->ue_size; ++i)
1431 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001432 line_len = undo_read_4c(bi);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001433 if (line_len >= 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001434 line = read_string_decrypt(bi, line_len);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001435 else
1436 {
1437 line = NULL;
1438 corruption_error("line length", file_name);
1439 }
1440 if (line == NULL)
1441 {
1442 *error = TRUE;
1443 return uep;
1444 }
Bram Moolenaarccae4672019-01-04 15:09:57 +01001445 array[i].ul_line = line;
1446 array[i].ul_len = line_len + 1;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001447 }
1448 return uep;
1449}
1450
1451/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001452 * Serialize "pos".
Bram Moolenaar9db58062010-05-29 20:33:07 +02001453 */
1454 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001455serialize_pos(bufinfo_T *bi, pos_T pos)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001456{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001457 undo_write_bytes(bi, (long_u)pos.lnum, 4);
1458 undo_write_bytes(bi, (long_u)pos.col, 4);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001459 undo_write_bytes(bi, (long_u)pos.coladd, 4);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001460}
1461
1462/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001463 * Unserialize the pos_T at the current position.
Bram Moolenaar9db58062010-05-29 20:33:07 +02001464 */
1465 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001466unserialize_pos(bufinfo_T *bi, pos_T *pos)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001467{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001468 pos->lnum = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001469 if (pos->lnum < 0)
1470 pos->lnum = 0;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001471 pos->col = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001472 if (pos->col < 0)
1473 pos->col = 0;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001474 pos->coladd = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001475 if (pos->coladd < 0)
1476 pos->coladd = 0;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001477}
1478
1479/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001480 * Serialize "info".
Bram Moolenaar9db58062010-05-29 20:33:07 +02001481 */
1482 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001483serialize_visualinfo(bufinfo_T *bi, visualinfo_T *info)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001484{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001485 serialize_pos(bi, info->vi_start);
1486 serialize_pos(bi, info->vi_end);
1487 undo_write_bytes(bi, (long_u)info->vi_mode, 4);
1488 undo_write_bytes(bi, (long_u)info->vi_curswant, 4);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001489}
1490
1491/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001492 * Unserialize the visualinfo_T at the current position.
Bram Moolenaar9db58062010-05-29 20:33:07 +02001493 */
1494 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001495unserialize_visualinfo(bufinfo_T *bi, visualinfo_T *info)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001496{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001497 unserialize_pos(bi, &info->vi_start);
1498 unserialize_pos(bi, &info->vi_end);
1499 info->vi_mode = undo_read_4c(bi);
1500 info->vi_curswant = undo_read_4c(bi);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001501}
1502
1503/*
1504 * Write the undo tree in an undo file.
1505 * When "name" is not NULL, use it as the name of the undo file.
1506 * Otherwise use buf->b_ffname to generate the undo file name.
1507 * "buf" must never be null, buf->b_ffname is used to obtain the original file
1508 * permissions.
1509 * "forceit" is TRUE for ":wundo!", FALSE otherwise.
1510 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text.
1511 */
1512 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001513u_write_undo(
1514 char_u *name,
1515 int forceit,
1516 buf_T *buf,
1517 char_u *hash)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001518{
1519 u_header_T *uhp;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001520 char_u *file_name;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001521 int mark;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001522#ifdef U_DEBUG
1523 int headers_written = 0;
1524#endif
1525 int fd;
1526 FILE *fp = NULL;
1527 int perm;
1528 int write_ok = FALSE;
1529#ifdef UNIX
1530 int st_old_valid = FALSE;
Bram Moolenaar8767f522016-07-01 17:17:39 +02001531 stat_T st_old;
1532 stat_T st_new;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001533#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001534 bufinfo_T bi;
1535
Bram Moolenaar5a669b92014-08-12 20:14:33 +02001536 vim_memset(&bi, 0, sizeof(bi));
Bram Moolenaar9db58062010-05-29 20:33:07 +02001537
1538 if (name == NULL)
1539 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001540 file_name = u_get_undo_file_name(buf->b_ffname, FALSE);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001541 if (file_name == NULL)
1542 {
1543 if (p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001544 {
1545 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001546 smsg(
Bram Moolenaar504a8212010-05-30 17:17:42 +02001547 _("Cannot write undo file in any directory in 'undodir'"));
1548 verbose_leave();
1549 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001550 return;
1551 }
1552 }
1553 else
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001554 file_name = name;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001555
1556 /*
1557 * Decide about the permission to use for the undo file. If the buffer
1558 * has a name use the permission of the original file. Otherwise only
1559 * allow the user to access the undo file.
1560 */
1561 perm = 0600;
1562 if (buf->b_ffname != NULL)
1563 {
1564#ifdef UNIX
1565 if (mch_stat((char *)buf->b_ffname, &st_old) >= 0)
1566 {
1567 perm = st_old.st_mode;
1568 st_old_valid = TRUE;
1569 }
1570#else
1571 perm = mch_getperm(buf->b_ffname);
1572 if (perm < 0)
1573 perm = 0600;
1574#endif
1575 }
1576
Bram Moolenaar3cbac302015-04-21 16:12:06 +02001577 /* strip any s-bit and executable bit */
1578 perm = perm & 0666;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001579
1580 /* If the undo file already exists, verify that it actually is an undo
1581 * file, and delete it. */
1582 if (mch_getperm(file_name) >= 0)
1583 {
1584 if (name == NULL || !forceit)
1585 {
1586 /* Check we can read it and it's an undo file. */
1587 fd = mch_open((char *)file_name, O_RDONLY|O_EXTRA, 0);
1588 if (fd < 0)
1589 {
1590 if (name != NULL || p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001591 {
1592 if (name == NULL)
1593 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001594 smsg(
Bram Moolenaar504a8212010-05-30 17:17:42 +02001595 _("Will not overwrite with undo file, cannot read: %s"),
Bram Moolenaar9db58062010-05-29 20:33:07 +02001596 file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001597 if (name == NULL)
1598 verbose_leave();
1599 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001600 goto theend;
1601 }
1602 else
1603 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02001604 char_u mbuf[UF_START_MAGIC_LEN];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001605 int len;
1606
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01001607 len = read_eintr(fd, mbuf, UF_START_MAGIC_LEN);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001608 close(fd);
1609 if (len < UF_START_MAGIC_LEN
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02001610 || memcmp(mbuf, UF_START_MAGIC, UF_START_MAGIC_LEN) != 0)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001611 {
1612 if (name != NULL || p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001613 {
1614 if (name == NULL)
1615 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001616 smsg(
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001617 _("Will not overwrite, this is not an undo file: %s"),
Bram Moolenaar9db58062010-05-29 20:33:07 +02001618 file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001619 if (name == NULL)
1620 verbose_leave();
1621 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001622 goto theend;
1623 }
1624 }
1625 }
1626 mch_remove(file_name);
1627 }
1628
Bram Moolenaar504a8212010-05-30 17:17:42 +02001629 /* If there is no undo information at all, quit here after deleting any
1630 * existing undo file. */
Bram Moolenaarccae4672019-01-04 15:09:57 +01001631 if (buf->b_u_numhead == 0 && buf->b_u_line_ptr.ul_line == NULL)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001632 {
1633 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001634 verb_msg(_("Skipping undo file write, nothing to undo"));
Bram Moolenaar504a8212010-05-30 17:17:42 +02001635 goto theend;
1636 }
1637
Bram Moolenaar9db58062010-05-29 20:33:07 +02001638 fd = mch_open((char *)file_name,
1639 O_CREAT|O_EXTRA|O_WRONLY|O_EXCL|O_NOFOLLOW, perm);
1640 if (fd < 0)
1641 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001642 semsg(_(e_not_open), file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001643 goto theend;
1644 }
1645 (void)mch_setperm(file_name, perm);
1646 if (p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001647 {
1648 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001649 smsg(_("Writing undo file: %s"), file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001650 verbose_leave();
1651 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001652
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001653#ifdef U_DEBUG
Bram Moolenaar504a8212010-05-30 17:17:42 +02001654 /* Check there is no problem in undo info before writing. */
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001655 u_check(FALSE);
1656#endif
1657
Bram Moolenaar9db58062010-05-29 20:33:07 +02001658#ifdef UNIX
1659 /*
1660 * Try to set the group of the undo file same as the original file. If
1661 * this fails, set the protection bits for the group same as the
1662 * protection bits for others.
1663 */
Bram Moolenaarce69e822010-07-21 20:31:07 +02001664 if (st_old_valid
1665 && mch_stat((char *)file_name, &st_new) >= 0
1666 && st_new.st_gid != st_old.st_gid
Bram Moolenaar9db58062010-05-29 20:33:07 +02001667# ifdef HAVE_FCHOWN /* sequent-ptx lacks fchown() */
Bram Moolenaarce69e822010-07-21 20:31:07 +02001668 && fchown(fd, (uid_t)-1, st_old.st_gid) != 0
Bram Moolenaar9db58062010-05-29 20:33:07 +02001669# endif
1670 )
1671 mch_setperm(file_name, (perm & 0707) | ((perm & 07) << 3));
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02001672# if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001673 if (buf->b_ffname != NULL)
1674 mch_copy_sec(buf->b_ffname, file_name);
1675# endif
1676#endif
1677
1678 fp = fdopen(fd, "w");
1679 if (fp == NULL)
1680 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001681 semsg(_(e_not_open), file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001682 close(fd);
1683 mch_remove(file_name);
1684 goto theend;
1685 }
1686
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001687 /* Undo must be synced. */
1688 u_sync(TRUE);
1689
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001690 /*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001691 * Write the header. Initializes encryption, if enabled.
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001692 */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001693 bi.bi_buf = buf;
1694 bi.bi_fp = fp;
1695 if (serialize_header(&bi, hash) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001696 goto write_error;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001697
1698 /*
1699 * Iteratively serialize UHPs and their UEPs from the top down.
1700 */
1701 mark = ++lastmark;
1702 uhp = buf->b_u_oldhead;
1703 while (uhp != NULL)
1704 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001705 /* Serialize current UHP if we haven't seen it */
1706 if (uhp->uh_walk != mark)
1707 {
1708 uhp->uh_walk = mark;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001709#ifdef U_DEBUG
1710 ++headers_written;
1711#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001712 if (serialize_uhp(&bi, uhp) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001713 goto write_error;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001714 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001715
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001716 /* Now walk through the tree - algorithm from undo_time(). */
1717 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != mark)
1718 uhp = uhp->uh_prev.ptr;
1719 else if (uhp->uh_alt_next.ptr != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001720 && uhp->uh_alt_next.ptr->uh_walk != mark)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001721 uhp = uhp->uh_alt_next.ptr;
1722 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001723 && uhp->uh_next.ptr->uh_walk != mark)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001724 uhp = uhp->uh_next.ptr;
1725 else if (uhp->uh_alt_prev.ptr != NULL)
1726 uhp = uhp->uh_alt_prev.ptr;
1727 else
1728 uhp = uhp->uh_next.ptr;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001729 }
1730
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001731 if (undo_write_bytes(&bi, (long_u)UF_HEADER_END_MAGIC, 2) == OK)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001732 write_ok = TRUE;
1733#ifdef U_DEBUG
1734 if (headers_written != buf->b_u_numhead)
Bram Moolenaar570064c2013-06-10 20:25:10 +02001735 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001736 semsg("Written %ld headers, ...", headers_written);
1737 semsg("... but numhead is %ld", buf->b_u_numhead);
Bram Moolenaar570064c2013-06-10 20:25:10 +02001738 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001739#endif
1740
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001741#ifdef FEAT_CRYPT
1742 if (bi.bi_state != NULL && undo_flush(&bi) == FAIL)
1743 write_ok = FALSE;
1744#endif
1745
Bram Moolenaar9db58062010-05-29 20:33:07 +02001746write_error:
1747 fclose(fp);
1748 if (!write_ok)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001749 semsg(_("E829: write error in undo file: %s"), file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001750
Bram Moolenaar4f974752019-02-17 17:44:42 +01001751#if defined(MSWIN)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001752 /* Copy file attributes; for systems where this can only be done after
1753 * closing the file. */
Bram Moolenaar9db58062010-05-29 20:33:07 +02001754 if (buf->b_ffname != NULL)
1755 (void)mch_copy_file_attribute(buf->b_ffname, file_name);
1756#endif
1757#ifdef HAVE_ACL
1758 if (buf->b_ffname != NULL)
1759 {
1760 vim_acl_T acl;
1761
1762 /* For systems that support ACL: get the ACL from the original file. */
1763 acl = mch_get_acl(buf->b_ffname);
1764 mch_set_acl(file_name, acl);
Bram Moolenaard2aed442012-06-01 13:46:12 +02001765 mch_free_acl(acl);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001766 }
1767#endif
1768
1769theend:
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001770#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001771 if (bi.bi_state != NULL)
1772 crypt_free_state(bi.bi_state);
1773 vim_free(bi.bi_buffer);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001774#endif
Bram Moolenaar9db58062010-05-29 20:33:07 +02001775 if (file_name != name)
1776 vim_free(file_name);
1777}
1778
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001779/*
1780 * Load the undo tree from an undo file.
1781 * If "name" is not NULL use it as the undo file name. This also means being
1782 * a bit more verbose.
1783 * Otherwise use curbuf->b_ffname to generate the undo file name.
1784 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text.
1785 */
1786 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001787u_read_undo(char_u *name, char_u *hash, char_u *orig_name)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001788{
1789 char_u *file_name;
1790 FILE *fp;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001791 long version, str_len;
Bram Moolenaarccae4672019-01-04 15:09:57 +01001792 undoline_T line_ptr;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001793 linenr_T line_lnum;
1794 colnr_T line_colnr;
1795 linenr_T line_count;
Bram Moolenaar3eb16372017-02-26 18:11:36 +01001796 long num_head = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001797 long old_header_seq, new_header_seq, cur_header_seq;
1798 long seq_last, seq_cur;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001799 long last_save_nr = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001800 short old_idx = -1, new_idx = -1, cur_idx = -1;
1801 long num_read_uhps = 0;
1802 time_t seq_time;
1803 int i, j;
1804 int c;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001805 u_header_T *uhp;
1806 u_header_T **uhp_table = NULL;
1807 char_u read_hash[UNDO_HASH_SIZE];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001808 char_u magic_buf[UF_START_MAGIC_LEN];
1809#ifdef U_DEBUG
1810 int *uhp_table_used;
1811#endif
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001812#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001813 stat_T st_orig;
1814 stat_T st_undo;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001815#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001816 bufinfo_T bi;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001817
Bram Moolenaar5a669b92014-08-12 20:14:33 +02001818 vim_memset(&bi, 0, sizeof(bi));
Bram Moolenaarccae4672019-01-04 15:09:57 +01001819 line_ptr.ul_len = 0;
1820 line_ptr.ul_line = NULL;
1821
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001822 if (name == NULL)
1823 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001824 file_name = u_get_undo_file_name(curbuf->b_ffname, TRUE);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001825 if (file_name == NULL)
1826 return;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001827
1828#ifdef UNIX
1829 /* For safety we only read an undo file if the owner is equal to the
Bram Moolenaar3b262392013-09-08 15:40:49 +02001830 * owner of the text file or equal to the current user. */
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001831 if (mch_stat((char *)orig_name, &st_orig) >= 0
1832 && mch_stat((char *)file_name, &st_undo) >= 0
Bram Moolenaar3b262392013-09-08 15:40:49 +02001833 && st_orig.st_uid != st_undo.st_uid
1834 && st_undo.st_uid != getuid())
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001835 {
1836 if (p_verbose > 0)
1837 {
1838 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001839 smsg(_("Not reading undo file, owner differs: %s"),
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001840 file_name);
1841 verbose_leave();
1842 }
1843 return;
1844 }
1845#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001846 }
1847 else
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001848 file_name = name;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001849
1850 if (p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001851 {
1852 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001853 smsg(_("Reading undo file: %s"), file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001854 verbose_leave();
1855 }
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001856
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001857 fp = mch_fopen((char *)file_name, "r");
1858 if (fp == NULL)
1859 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001860 if (name != NULL || p_verbose > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001861 semsg(_("E822: Cannot open undo file for reading: %s"), file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001862 goto error;
1863 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001864 bi.bi_buf = curbuf;
1865 bi.bi_fp = fp;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001866
Bram Moolenaar9db58062010-05-29 20:33:07 +02001867 /*
1868 * Read the undo file header.
1869 */
1870 if (fread(magic_buf, UF_START_MAGIC_LEN, 1, fp) != 1
1871 || memcmp(magic_buf, UF_START_MAGIC, UF_START_MAGIC_LEN) != 0)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001872 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001873 semsg(_("E823: Not an undo file: %s"), file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001874 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001875 }
1876 version = get2c(fp);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001877 if (version == UF_VERSION_CRYPT)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001878 {
1879#ifdef FEAT_CRYPT
Bram Moolenaar56be9502010-06-06 14:20:26 +02001880 if (*curbuf->b_p_key == NUL)
1881 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001882 semsg(_("E832: Non-encrypted file has encrypted undo file: %s"),
Bram Moolenaar56be9502010-06-06 14:20:26 +02001883 file_name);
1884 goto error;
1885 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001886 bi.bi_state = crypt_create_from_file(fp, curbuf->b_p_key);
1887 if (bi.bi_state == NULL)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001888 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001889 semsg(_("E826: Undo file decryption failed: %s"), file_name);
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001890 goto error;
1891 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001892 if (crypt_whole_undofile(bi.bi_state->method_nr))
1893 {
1894 bi.bi_buffer = alloc(CRYPT_BUF_SIZE);
1895 if (bi.bi_buffer == NULL)
1896 {
1897 crypt_free_state(bi.bi_state);
1898 bi.bi_state = NULL;
1899 goto error;
1900 }
1901 bi.bi_avail = 0;
1902 bi.bi_used = 0;
1903 }
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001904#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001905 semsg(_("E827: Undo file is encrypted: %s"), file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001906 goto error;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001907#endif
1908 }
Bram Moolenaar69154f22010-07-18 21:42:34 +02001909 else if (version != UF_VERSION)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001910 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001911 semsg(_("E824: Incompatible undo file: %s"), file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001912 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001913 }
1914
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001915 if (undo_read(&bi, read_hash, (size_t)UNDO_HASH_SIZE) == FAIL)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001916 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001917 corruption_error("hash", file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001918 goto error;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001919 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001920 line_count = (linenr_T)undo_read_4c(&bi);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001921 if (memcmp(hash, read_hash, UNDO_HASH_SIZE) != 0
1922 || line_count != curbuf->b_ml.ml_line_count)
1923 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001924 if (p_verbose > 0 || name != NULL)
1925 {
Bram Moolenaar504a8212010-05-30 17:17:42 +02001926 if (name == NULL)
1927 verbose_enter();
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001928 give_warning((char_u *)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001929 _("File contents changed, cannot use undo info"), TRUE);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001930 if (name == NULL)
1931 verbose_leave();
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001932 }
1933 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001934 }
1935
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001936 /* Read undo data for "U" command. */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001937 str_len = undo_read_4c(&bi);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001938 if (str_len < 0)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001939 goto error;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001940 if (str_len > 0)
Bram Moolenaarccae4672019-01-04 15:09:57 +01001941 {
1942 line_ptr.ul_line = read_string_decrypt(&bi, str_len);
1943 line_ptr.ul_len = str_len + 1;
1944 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001945 line_lnum = (linenr_T)undo_read_4c(&bi);
1946 line_colnr = (colnr_T)undo_read_4c(&bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001947 if (line_lnum < 0 || line_colnr < 0)
1948 {
1949 corruption_error("line lnum/col", file_name);
1950 goto error;
1951 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001952
1953 /* Begin general undo data */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001954 old_header_seq = undo_read_4c(&bi);
1955 new_header_seq = undo_read_4c(&bi);
1956 cur_header_seq = undo_read_4c(&bi);
1957 num_head = undo_read_4c(&bi);
1958 seq_last = undo_read_4c(&bi);
1959 seq_cur = undo_read_4c(&bi);
1960 seq_time = undo_read_time(&bi);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001961
Bram Moolenaar69154f22010-07-18 21:42:34 +02001962 /* Optional header fields. */
1963 for (;;)
1964 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001965 int len = undo_read_byte(&bi);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001966 int what;
Bram Moolenaara800b422010-06-27 01:15:55 +02001967
Bram Moolenaar69154f22010-07-18 21:42:34 +02001968 if (len == 0 || len == EOF)
1969 break;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001970 what = undo_read_byte(&bi);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001971 switch (what)
1972 {
1973 case UF_LAST_SAVE_NR:
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001974 last_save_nr = undo_read_4c(&bi);
Bram Moolenaara800b422010-06-27 01:15:55 +02001975 break;
Bram Moolenaar69154f22010-07-18 21:42:34 +02001976 default:
1977 /* field not supported, skip */
1978 while (--len >= 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001979 (void)undo_read_byte(&bi);
Bram Moolenaara800b422010-06-27 01:15:55 +02001980 }
Bram Moolenaar69154f22010-07-18 21:42:34 +02001981 }
Bram Moolenaara800b422010-06-27 01:15:55 +02001982
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001983 /* uhp_table will store the freshly created undo headers we allocate
1984 * until we insert them into curbuf. The table remains sorted by the
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001985 * sequence numbers of the headers.
1986 * When there are no headers uhp_table is NULL. */
1987 if (num_head > 0)
1988 {
Bram Moolenaar3eb16372017-02-26 18:11:36 +01001989 if (num_head < LONG_MAX / (long)sizeof(u_header_T *))
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001990 uhp_table = U_ALLOC_LINE(num_head * sizeof(u_header_T *));
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001991 if (uhp_table == NULL)
1992 goto error;
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001993 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001994
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001995 while ((c = undo_read_2c(&bi)) == UF_HEADER_MAGIC)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001996 {
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001997 if (num_read_uhps >= num_head)
1998 {
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001999 corruption_error("num_head too small", file_name);
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02002000 goto error;
2001 }
2002
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002003 uhp = unserialize_uhp(&bi, file_name);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002004 if (uhp == NULL)
2005 goto error;
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02002006 uhp_table[num_read_uhps++] = uhp;
2007 }
2008
2009 if (num_read_uhps != num_head)
2010 {
2011 corruption_error("num_head", file_name);
2012 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002013 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02002014 if (c != UF_HEADER_END_MAGIC)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002015 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02002016 corruption_error("end marker", file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002017 goto error;
2018 }
2019
Bram Moolenaar9db58062010-05-29 20:33:07 +02002020#ifdef U_DEBUG
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002021 uhp_table_used = alloc_clear(sizeof(int) * num_head + 1);
Bram Moolenaar9db58062010-05-29 20:33:07 +02002022# define SET_FLAG(j) ++uhp_table_used[j]
2023#else
2024# define SET_FLAG(j)
2025#endif
2026
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002027 /* We have put all of the headers into a table. Now we iterate through the
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002028 * table and swizzle each sequence number we have stored in uh_*_seq into
2029 * a pointer corresponding to the header with that sequence number. */
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002030 for (i = 0; i < num_head; i++)
2031 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002032 uhp = uhp_table[i];
2033 if (uhp == NULL)
2034 continue;
2035 for (j = 0; j < num_head; j++)
2036 if (uhp_table[j] != NULL && i != j
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002037 && uhp_table[i]->uh_seq == uhp_table[j]->uh_seq)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002038 {
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02002039 corruption_error("duplicate uh_seq", file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002040 goto error;
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02002041 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002042 for (j = 0; j < num_head; j++)
2043 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002044 && uhp_table[j]->uh_seq == uhp->uh_next.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002045 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002046 uhp->uh_next.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02002047 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002048 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002049 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002050 for (j = 0; j < num_head; j++)
2051 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002052 && uhp_table[j]->uh_seq == uhp->uh_prev.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002053 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002054 uhp->uh_prev.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02002055 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002056 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002057 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002058 for (j = 0; j < num_head; j++)
2059 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002060 && uhp_table[j]->uh_seq == uhp->uh_alt_next.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002061 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002062 uhp->uh_alt_next.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02002063 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002064 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002065 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002066 for (j = 0; j < num_head; j++)
2067 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002068 && uhp_table[j]->uh_seq == uhp->uh_alt_prev.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002069 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002070 uhp->uh_alt_prev.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02002071 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002072 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002073 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002074 if (old_header_seq > 0 && old_idx < 0 && uhp->uh_seq == old_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002075 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002076 old_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002077 SET_FLAG(i);
2078 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002079 if (new_header_seq > 0 && new_idx < 0 && uhp->uh_seq == new_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002080 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002081 new_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002082 SET_FLAG(i);
2083 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002084 if (cur_header_seq > 0 && cur_idx < 0 && uhp->uh_seq == cur_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002085 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002086 cur_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002087 SET_FLAG(i);
2088 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002089 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02002090
2091 /* Now that we have read the undo info successfully, free the current undo
2092 * info and use the info from the file. */
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002093 u_blockfree(curbuf);
Bram Moolenaar9db58062010-05-29 20:33:07 +02002094 curbuf->b_u_oldhead = old_idx < 0 ? NULL : uhp_table[old_idx];
2095 curbuf->b_u_newhead = new_idx < 0 ? NULL : uhp_table[new_idx];
2096 curbuf->b_u_curhead = cur_idx < 0 ? NULL : uhp_table[cur_idx];
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002097 curbuf->b_u_line_ptr = line_ptr;
2098 curbuf->b_u_line_lnum = line_lnum;
2099 curbuf->b_u_line_colnr = line_colnr;
2100 curbuf->b_u_numhead = num_head;
2101 curbuf->b_u_seq_last = seq_last;
2102 curbuf->b_u_seq_cur = seq_cur;
Bram Moolenaara800b422010-06-27 01:15:55 +02002103 curbuf->b_u_time_cur = seq_time;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002104 curbuf->b_u_save_nr_last = last_save_nr;
Bram Moolenaardba01a02010-11-03 19:32:42 +01002105 curbuf->b_u_save_nr_cur = last_save_nr;
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02002106
2107 curbuf->b_u_synced = TRUE;
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002108 vim_free(uhp_table);
Bram Moolenaar9db58062010-05-29 20:33:07 +02002109
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002110#ifdef U_DEBUG
Bram Moolenaar9db58062010-05-29 20:33:07 +02002111 for (i = 0; i < num_head; ++i)
2112 if (uhp_table_used[i] == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002113 semsg("uhp_table entry %ld not used, leaking memory", i);
Bram Moolenaar9db58062010-05-29 20:33:07 +02002114 vim_free(uhp_table_used);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002115 u_check(TRUE);
2116#endif
Bram Moolenaar9db58062010-05-29 20:33:07 +02002117
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002118 if (name != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002119 smsg(_("Finished reading undo file %s"), file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002120 goto theend;
2121
2122error:
Bram Moolenaarccae4672019-01-04 15:09:57 +01002123 vim_free(line_ptr.ul_line);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002124 if (uhp_table != NULL)
2125 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002126 for (i = 0; i < num_read_uhps; i++)
2127 if (uhp_table[i] != NULL)
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02002128 u_free_uhp(uhp_table[i]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002129 vim_free(uhp_table);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002130 }
2131
2132theend:
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002133#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002134 if (bi.bi_state != NULL)
2135 crypt_free_state(bi.bi_state);
2136 vim_free(bi.bi_buffer);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002137#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002138 if (fp != NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002139 fclose(fp);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002140 if (file_name != name)
2141 vim_free(file_name);
2142 return;
2143}
2144
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002145#endif /* FEAT_PERSISTENT_UNDO */
2146
2147
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148/*
2149 * If 'cpoptions' contains 'u': Undo the previous undo or redo (vi compatible).
2150 * If 'cpoptions' does not contain 'u': Always undo.
2151 */
2152 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002153u_undo(int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154{
2155 /*
2156 * If we get an undo command while executing a macro, we behave like the
2157 * original vi. If this happens twice in one macro the result will not
2158 * be compatible.
2159 */
2160 if (curbuf->b_u_synced == FALSE)
2161 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002162 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 count = 1;
2164 }
2165
2166 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
2167 undo_undoes = TRUE;
2168 else
2169 undo_undoes = !undo_undoes;
2170 u_doit(count);
2171}
2172
2173/*
2174 * If 'cpoptions' contains 'u': Repeat the previous undo or redo.
2175 * If 'cpoptions' does not contain 'u': Always redo.
2176 */
2177 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002178u_redo(int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179{
2180 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
2181 undo_undoes = FALSE;
2182 u_doit(count);
2183}
2184
2185/*
2186 * Undo or redo, depending on 'undo_undoes', 'count' times.
2187 */
2188 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002189u_doit(int startcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190{
Bram Moolenaarca003e12006-03-17 23:19:38 +00002191 int count = startcount;
2192
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002193 if (!undo_allowed())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195
2196 u_newcount = 0;
2197 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002198 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2199 u_oldcount = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 while (count--)
2201 {
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002202 /* Do the change warning now, so that it triggers FileChangedRO when
2203 * needed. This may cause the file to be reloaded, that must happen
2204 * before we do anything, because it may change curbuf->b_u_curhead
2205 * and more. */
2206 change_warning(0);
2207
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 if (undo_undoes)
2209 {
2210 if (curbuf->b_u_curhead == NULL) /* first undo */
2211 curbuf->b_u_curhead = curbuf->b_u_newhead;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002212 else if (get_undolevel() > 0) /* multi level undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 /* get next undo */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002214 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_next.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 /* nothing to undo */
2216 if (curbuf->b_u_numhead == 0 || curbuf->b_u_curhead == NULL)
2217 {
2218 /* stick curbuf->b_u_curhead at end */
2219 curbuf->b_u_curhead = curbuf->b_u_oldhead;
2220 beep_flush();
Bram Moolenaarca003e12006-03-17 23:19:38 +00002221 if (count == startcount - 1)
2222 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002223 msg(_("Already at oldest change"));
Bram Moolenaarca003e12006-03-17 23:19:38 +00002224 return;
2225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 break;
2227 }
2228
Bram Moolenaarca003e12006-03-17 23:19:38 +00002229 u_undoredo(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 }
2231 else
2232 {
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002233 if (curbuf->b_u_curhead == NULL || get_undolevel() <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 {
2235 beep_flush(); /* nothing to redo */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002236 if (count == startcount - 1)
2237 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002238 msg(_("Already at newest change"));
Bram Moolenaarca003e12006-03-17 23:19:38 +00002239 return;
2240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 break;
2242 }
2243
Bram Moolenaarca003e12006-03-17 23:19:38 +00002244 u_undoredo(FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002245
2246 /* Advance for next redo. Set "newhead" when at the end of the
2247 * redoable changes. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002248 if (curbuf->b_u_curhead->uh_prev.ptr == NULL)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002249 curbuf->b_u_newhead = curbuf->b_u_curhead;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002250 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_prev.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 }
2252 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00002253 u_undo_end(undo_undoes, FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002254}
2255
Bram Moolenaar1e607892006-03-13 22:15:53 +00002256/*
2257 * Undo or redo over the timeline.
2258 * When "step" is negative go back in time, otherwise goes forward in time.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002259 * When "sec" is FALSE make "step" steps, when "sec" is TRUE use "step" as
2260 * seconds.
Bram Moolenaar730cde92010-06-27 05:18:54 +02002261 * When "file" is TRUE use "step" as a number of file writes.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002262 * When "absolute" is TRUE use "step" as the sequence number to jump to.
2263 * "sec" must be FALSE then.
Bram Moolenaar1e607892006-03-13 22:15:53 +00002264 */
2265 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002266undo_time(
2267 long step,
2268 int sec,
2269 int file,
2270 int absolute)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002271{
2272 long target;
2273 long closest;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002274 long closest_start;
2275 long closest_seq = 0;
2276 long val;
Bram Moolenaarce46d932018-01-30 22:46:06 +01002277 u_header_T *uhp = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002278 u_header_T *last;
2279 int mark;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02002280 int nomark = 0; // shut up compiler
Bram Moolenaar1e607892006-03-13 22:15:53 +00002281 int round;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002282 int dosec = sec;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002283 int dofile = file;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002284 int above = FALSE;
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002285 int did_undo = TRUE;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002286
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002287 /* First make sure the current undoable change is synced. */
2288 if (curbuf->b_u_synced == FALSE)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002289 u_sync(TRUE);
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002290
Bram Moolenaar1e607892006-03-13 22:15:53 +00002291 u_newcount = 0;
2292 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002293 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002294 u_oldcount = -1;
2295
Bram Moolenaarca003e12006-03-17 23:19:38 +00002296 /* "target" is the node below which we want to be.
2297 * Init "closest" to a value we can't reach. */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002298 if (absolute)
2299 {
Bram Moolenaarce46d932018-01-30 22:46:06 +01002300 target = step;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002301 closest = -1;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002302 }
Bram Moolenaarca003e12006-03-17 23:19:38 +00002303 else
Bram Moolenaar1e607892006-03-13 22:15:53 +00002304 {
Bram Moolenaar730cde92010-06-27 05:18:54 +02002305 if (dosec)
Bram Moolenaarcbd4de42017-01-07 16:14:57 +01002306 target = (long)(curbuf->b_u_time_cur) + step;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002307 else if (dofile)
2308 {
2309 if (step < 0)
2310 {
2311 /* Going back to a previous write. If there were changes after
2312 * the last write, count that as moving one file-write, so
2313 * that ":earlier 1f" undoes all changes since the last save. */
2314 uhp = curbuf->b_u_curhead;
2315 if (uhp != NULL)
2316 uhp = uhp->uh_next.ptr;
2317 else
2318 uhp = curbuf->b_u_newhead;
2319 if (uhp != NULL && uhp->uh_save_nr != 0)
2320 /* "uh_save_nr" was set in the last block, that means
2321 * there were no changes since the last write */
2322 target = curbuf->b_u_save_nr_cur + step;
2323 else
2324 /* count the changes since the last write as one step */
2325 target = curbuf->b_u_save_nr_cur + step + 1;
2326 if (target <= 0)
2327 /* Go to before first write: before the oldest change. Use
2328 * the sequence number for that. */
2329 dofile = FALSE;
2330 }
2331 else
2332 {
2333 /* Moving forward to a newer write. */
2334 target = curbuf->b_u_save_nr_cur + step;
2335 if (target > curbuf->b_u_save_nr_last)
2336 {
2337 /* Go to after last write: after the latest change. Use
2338 * the sequence number for that. */
2339 target = curbuf->b_u_seq_last + 1;
2340 dofile = FALSE;
2341 }
2342 }
2343 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002344 else
2345 target = curbuf->b_u_seq_cur + step;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002346 if (step < 0)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002347 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002348 if (target < 0)
2349 target = 0;
2350 closest = -1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002351 }
2352 else
2353 {
Bram Moolenaar730cde92010-06-27 05:18:54 +02002354 if (dosec)
Bram Moolenaarcbd4de42017-01-07 16:14:57 +01002355 closest = (long)(vim_time() + 1);
Bram Moolenaar730cde92010-06-27 05:18:54 +02002356 else if (dofile)
2357 closest = curbuf->b_u_save_nr_last + 2;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002358 else
2359 closest = curbuf->b_u_seq_last + 2;
2360 if (target >= closest)
2361 target = closest - 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002362 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002363 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002364 closest_start = closest;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002365 closest_seq = curbuf->b_u_seq_cur;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002366
Bram Moolenaarce46d932018-01-30 22:46:06 +01002367 /* When "target" is 0; Back to origin. */
2368 if (target == 0)
Bram Moolenaar059fd012018-01-31 14:25:53 +01002369 {
2370 mark = lastmark; /* avoid that GCC complains */
2371 goto target_zero;
2372 }
Bram Moolenaarce46d932018-01-30 22:46:06 +01002373
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002374 /*
2375 * May do this twice:
Bram Moolenaar1e607892006-03-13 22:15:53 +00002376 * 1. Search for "target", update "closest" to the best match found.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002377 * 2. If "target" not found search for "closest".
2378 *
2379 * When using the closest time we use the sequence number in the second
2380 * round, because there may be several entries with the same time.
2381 */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002382 for (round = 1; round <= 2; ++round)
2383 {
2384 /* Find the path from the current state to where we want to go. The
2385 * desired state can be anywhere in the undo tree, need to go all over
2386 * it. We put "nomark" in uh_walk where we have been without success,
2387 * "mark" where it could possibly be. */
2388 mark = ++lastmark;
2389 nomark = ++lastmark;
2390
2391 if (curbuf->b_u_curhead == NULL) /* at leaf of the tree */
2392 uhp = curbuf->b_u_newhead;
2393 else
2394 uhp = curbuf->b_u_curhead;
2395
2396 while (uhp != NULL)
2397 {
2398 uhp->uh_walk = mark;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002399 if (dosec)
Bram Moolenaarcbd4de42017-01-07 16:14:57 +01002400 val = (long)(uhp->uh_time);
Bram Moolenaar730cde92010-06-27 05:18:54 +02002401 else if (dofile)
2402 val = uhp->uh_save_nr;
2403 else
2404 val = uhp->uh_seq;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002405
Bram Moolenaar730cde92010-06-27 05:18:54 +02002406 if (round == 1 && !(dofile && val == 0))
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002407 {
2408 /* Remember the header that is closest to the target.
2409 * It must be at least in the right direction (checked with
Bram Moolenaarca003e12006-03-17 23:19:38 +00002410 * "b_u_seq_cur"). When the timestamp is equal find the
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002411 * highest/lowest sequence number. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002412 if ((step < 0 ? uhp->uh_seq <= curbuf->b_u_seq_cur
2413 : uhp->uh_seq > curbuf->b_u_seq_cur)
2414 && ((dosec && val == closest)
2415 ? (step < 0
2416 ? uhp->uh_seq < closest_seq
2417 : uhp->uh_seq > closest_seq)
2418 : closest == closest_start
2419 || (val > target
2420 ? (closest > target
2421 ? val - target <= closest - target
2422 : val - target <= target - closest)
2423 : (closest > target
2424 ? target - val <= closest - target
2425 : target - val <= target - closest))))
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002426 {
2427 closest = val;
2428 closest_seq = uhp->uh_seq;
2429 }
2430 }
2431
2432 /* Quit searching when we found a match. But when searching for a
2433 * time we need to continue looking for the best uh_seq. */
2434 if (target == val && !dosec)
Bram Moolenaar730cde92010-06-27 05:18:54 +02002435 {
2436 target = uhp->uh_seq;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002437 break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002438 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002439
2440 /* go down in the tree if we haven't been there */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002441 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != nomark
2442 && uhp->uh_prev.ptr->uh_walk != mark)
2443 uhp = uhp->uh_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002444
2445 /* go to alternate branch if we haven't been there */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002446 else if (uhp->uh_alt_next.ptr != NULL
2447 && uhp->uh_alt_next.ptr->uh_walk != nomark
2448 && uhp->uh_alt_next.ptr->uh_walk != mark)
2449 uhp = uhp->uh_alt_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002450
2451 /* go up in the tree if we haven't been there and we are at the
2452 * start of alternate branches */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002453 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
2454 && uhp->uh_next.ptr->uh_walk != nomark
2455 && uhp->uh_next.ptr->uh_walk != mark)
Bram Moolenaardb552d602006-03-23 22:59:57 +00002456 {
2457 /* If still at the start we don't go through this change. */
2458 if (uhp == curbuf->b_u_curhead)
2459 uhp->uh_walk = nomark;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002460 uhp = uhp->uh_next.ptr;
Bram Moolenaardb552d602006-03-23 22:59:57 +00002461 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002462
2463 else
2464 {
2465 /* need to backtrack; mark this node as useless */
2466 uhp->uh_walk = nomark;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002467 if (uhp->uh_alt_prev.ptr != NULL)
2468 uhp = uhp->uh_alt_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002469 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002470 uhp = uhp->uh_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002471 }
2472 }
2473
2474 if (uhp != NULL) /* found it */
2475 break;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002476
2477 if (absolute)
2478 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002479 semsg(_("E830: Undo number %ld not found"), step);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002480 return;
2481 }
2482
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002483 if (closest == closest_start)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002484 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002485 if (step < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002486 msg(_("Already at oldest change"));
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002487 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002488 msg(_("Already at newest change"));
Bram Moolenaar1e607892006-03-13 22:15:53 +00002489 return;
2490 }
2491
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002492 target = closest_seq;
2493 dosec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002494 dofile = FALSE;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002495 if (step < 0)
2496 above = TRUE; /* stop above the header */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002497 }
2498
Bram Moolenaar059fd012018-01-31 14:25:53 +01002499target_zero:
Bram Moolenaar1e607892006-03-13 22:15:53 +00002500 /* If we found it: Follow the path to go to where we want to be. */
Bram Moolenaarce46d932018-01-30 22:46:06 +01002501 if (uhp != NULL || target == 0)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002502 {
2503 /*
2504 * First go up the tree as much as needed.
2505 */
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002506 while (!got_int)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002507 {
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002508 /* Do the change warning now, for the same reason as above. */
2509 change_warning(0);
2510
Bram Moolenaar1e607892006-03-13 22:15:53 +00002511 uhp = curbuf->b_u_curhead;
2512 if (uhp == NULL)
2513 uhp = curbuf->b_u_newhead;
2514 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002515 uhp = uhp->uh_next.ptr;
Bram Moolenaarce46d932018-01-30 22:46:06 +01002516 if (uhp == NULL || (target > 0 && uhp->uh_walk != mark)
Bram Moolenaarca003e12006-03-17 23:19:38 +00002517 || (uhp->uh_seq == target && !above))
Bram Moolenaar1e607892006-03-13 22:15:53 +00002518 break;
2519 curbuf->b_u_curhead = uhp;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002520 u_undoredo(TRUE);
Bram Moolenaarce46d932018-01-30 22:46:06 +01002521 if (target > 0)
2522 uhp->uh_walk = nomark; /* don't go back down here */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002523 }
2524
Bram Moolenaarce46d932018-01-30 22:46:06 +01002525 /* When back to origin, redo is not needed. */
2526 if (target > 0)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002527 {
Bram Moolenaarce46d932018-01-30 22:46:06 +01002528 /*
2529 * And now go down the tree (redo), branching off where needed.
2530 */
2531 while (!got_int)
2532 {
2533 /* Do the change warning now, for the same reason as above. */
2534 change_warning(0);
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002535
Bram Moolenaarce46d932018-01-30 22:46:06 +01002536 uhp = curbuf->b_u_curhead;
2537 if (uhp == NULL)
2538 break;
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002539
Bram Moolenaarce46d932018-01-30 22:46:06 +01002540 /* Go back to the first branch with a mark. */
2541 while (uhp->uh_alt_prev.ptr != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002542 && uhp->uh_alt_prev.ptr->uh_walk == mark)
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002543 uhp = uhp->uh_alt_prev.ptr;
Bram Moolenaarce46d932018-01-30 22:46:06 +01002544
2545 /* Find the last branch with a mark, that's the one. */
2546 last = uhp;
2547 while (last->uh_alt_next.ptr != NULL
2548 && last->uh_alt_next.ptr->uh_walk == mark)
2549 last = last->uh_alt_next.ptr;
2550 if (last != uhp)
2551 {
2552 /* Make the used branch the first entry in the list of
2553 * alternatives to make "u" and CTRL-R take this branch. */
2554 while (uhp->uh_alt_prev.ptr != NULL)
2555 uhp = uhp->uh_alt_prev.ptr;
2556 if (last->uh_alt_next.ptr != NULL)
2557 last->uh_alt_next.ptr->uh_alt_prev.ptr =
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002558 last->uh_alt_prev.ptr;
Bram Moolenaarce46d932018-01-30 22:46:06 +01002559 last->uh_alt_prev.ptr->uh_alt_next.ptr =
2560 last->uh_alt_next.ptr;
2561 last->uh_alt_prev.ptr = NULL;
2562 last->uh_alt_next.ptr = uhp;
2563 uhp->uh_alt_prev.ptr = last;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002564
Bram Moolenaarce46d932018-01-30 22:46:06 +01002565 if (curbuf->b_u_oldhead == uhp)
2566 curbuf->b_u_oldhead = last;
2567 uhp = last;
2568 if (uhp->uh_next.ptr != NULL)
2569 uhp->uh_next.ptr->uh_prev.ptr = uhp;
2570 }
2571 curbuf->b_u_curhead = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002572
Bram Moolenaarce46d932018-01-30 22:46:06 +01002573 if (uhp->uh_walk != mark)
2574 break; /* must have reached the target */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002575
Bram Moolenaarce46d932018-01-30 22:46:06 +01002576 /* Stop when going backwards in time and didn't find the exact
2577 * header we were looking for. */
2578 if (uhp->uh_seq == target && above)
2579 {
2580 curbuf->b_u_seq_cur = target - 1;
2581 break;
2582 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002583
Bram Moolenaarce46d932018-01-30 22:46:06 +01002584 u_undoredo(FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002585
Bram Moolenaarce46d932018-01-30 22:46:06 +01002586 /* Advance "curhead" to below the header we last used. If it
2587 * becomes NULL then we need to set "newhead" to this leaf. */
2588 if (uhp->uh_prev.ptr == NULL)
2589 curbuf->b_u_newhead = uhp;
2590 curbuf->b_u_curhead = uhp->uh_prev.ptr;
2591 did_undo = FALSE;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002592
Bram Moolenaarce46d932018-01-30 22:46:06 +01002593 if (uhp->uh_seq == target) /* found it! */
2594 break;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002595
Bram Moolenaarce46d932018-01-30 22:46:06 +01002596 uhp = uhp->uh_prev.ptr;
2597 if (uhp == NULL || uhp->uh_walk != mark)
2598 {
2599 /* Need to redo more but can't find it... */
2600 internal_error("undo_time()");
2601 break;
2602 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002603 }
2604 }
2605 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00002606 u_undo_end(did_undo, absolute);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607}
2608
2609/*
2610 * u_undoredo: common code for undo and redo
2611 *
2612 * The lines in the file are replaced by the lines in the entry list at
2613 * curbuf->b_u_curhead. The replaced lines in the file are saved in the entry
2614 * list for the next undo/redo.
Bram Moolenaarca003e12006-03-17 23:19:38 +00002615 *
2616 * When "undo" is TRUE we go up in the tree, when FALSE we go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 */
2618 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002619u_undoredo(int undo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620{
Bram Moolenaarccae4672019-01-04 15:09:57 +01002621 undoline_T *newarray = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 linenr_T oldsize;
2623 linenr_T newsize;
2624 linenr_T top, bot;
2625 linenr_T lnum;
2626 linenr_T newlnum = MAXLNUM;
2627 long i;
2628 u_entry_T *uep, *nuep;
2629 u_entry_T *newlist = NULL;
2630 int old_flags;
2631 int new_flags;
2632 pos_T namedm[NMARKS];
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002633 visualinfo_T visualinfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 int empty_buffer; /* buffer became empty */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002635 u_header_T *curhead = curbuf->b_u_curhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002637 /* Don't want autocommands using the undo structures here, they are
2638 * invalid till the end. */
2639 block_autocmds();
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002640
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002641#ifdef U_DEBUG
2642 u_check(FALSE);
2643#endif
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002644 old_flags = curhead->uh_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 new_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
2646 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
2647 setpcmark();
2648
2649 /*
2650 * save marks before undo/redo
2651 */
2652 mch_memmove(namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002653 visualinfo = curbuf->b_visual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 curbuf->b_op_start.lnum = curbuf->b_ml.ml_line_count;
2655 curbuf->b_op_start.col = 0;
2656 curbuf->b_op_end.lnum = 0;
2657 curbuf->b_op_end.col = 0;
2658
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002659 for (uep = curhead->uh_entry; uep != NULL; uep = nuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 {
2661 top = uep->ue_top;
2662 bot = uep->ue_bot;
2663 if (bot == 0)
2664 bot = curbuf->b_ml.ml_line_count + 1;
2665 if (top > curbuf->b_ml.ml_line_count || top >= bot
2666 || bot > curbuf->b_ml.ml_line_count + 1)
2667 {
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002668 unblock_autocmds();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002669 iemsg(_("E438: u_undo: line numbers wrong"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 changed(); /* don't want UNCHANGED now */
2671 return;
2672 }
2673
2674 oldsize = bot - top - 1; /* number of lines before undo */
2675 newsize = uep->ue_size; /* number of lines after undo */
2676
2677 if (top < newlnum)
2678 {
2679 /* If the saved cursor is somewhere in this undo block, move it to
2680 * the remembered position. Makes "gwap" put the cursor back
2681 * where it was. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002682 lnum = curhead->uh_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 if (lnum >= top && lnum <= top + newsize + 1)
2684 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002685 curwin->w_cursor = curhead->uh_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 newlnum = curwin->w_cursor.lnum - 1;
2687 }
2688 else
2689 {
2690 /* Use the first line that actually changed. Avoids that
2691 * undoing auto-formatting puts the cursor in the previous
2692 * line. */
2693 for (i = 0; i < newsize && i < oldsize; ++i)
Bram Moolenaarccae4672019-01-04 15:09:57 +01002694 {
2695 char_u *p = ml_get(top + 1 + i);
2696
2697 if (curbuf->b_ml.ml_line_len != uep->ue_array[i].ul_len
Bram Moolenaar964b3742019-05-24 18:54:09 +02002698 || memcmp(uep->ue_array[i].ul_line, p,
2699 curbuf->b_ml.ml_line_len) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 break;
Bram Moolenaarccae4672019-01-04 15:09:57 +01002701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 if (i == newsize && newlnum == MAXLNUM && uep->ue_next == NULL)
2703 {
2704 newlnum = top;
2705 curwin->w_cursor.lnum = newlnum + 1;
2706 }
2707 else if (i < newsize)
2708 {
2709 newlnum = top + i;
2710 curwin->w_cursor.lnum = newlnum + 1;
2711 }
2712 }
2713 }
2714
2715 empty_buffer = FALSE;
2716
2717 /* delete the lines between top and bot and save them in newarray */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002718 if (oldsize > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002720 if ((newarray = U_ALLOC_LINE(sizeof(undoline_T) * oldsize)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 {
Bram Moolenaarccae4672019-01-04 15:09:57 +01002722 do_outofmem_msg((long_u)(sizeof(undoline_T) * oldsize));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 /*
2724 * We have messed up the entry list, repair is impossible.
2725 * we have to free the rest of the list.
2726 */
2727 while (uep != NULL)
2728 {
2729 nuep = uep->ue_next;
2730 u_freeentry(uep, uep->ue_size);
2731 uep = nuep;
2732 }
2733 break;
2734 }
2735 /* delete backwards, it goes faster in most cases */
2736 for (lnum = bot - 1, i = oldsize; --i >= 0; --lnum)
2737 {
2738 /* what can we do when we run out of memory? */
Bram Moolenaarccae4672019-01-04 15:09:57 +01002739 if (u_save_line(&newarray[i], lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 do_outofmem_msg((long_u)0);
2741 /* remember we deleted the last line in the buffer, and a
2742 * dummy empty line will be inserted */
2743 if (curbuf->b_ml.ml_line_count == 1)
2744 empty_buffer = TRUE;
2745 ml_delete(lnum, FALSE);
2746 }
2747 }
Bram Moolenaar8d343302005-07-12 22:46:17 +00002748 else
2749 newarray = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750
2751 /* insert the lines in u_array between top and bot */
2752 if (newsize)
2753 {
2754 for (lnum = top, i = 0; i < newsize; ++i, ++lnum)
2755 {
Bram Moolenaarccae4672019-01-04 15:09:57 +01002756 // If the file is empty, there is an empty line 1 that we
2757 // should get rid of, by replacing it with the new line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 if (empty_buffer && lnum == 0)
Bram Moolenaar964b3742019-05-24 18:54:09 +02002759 ml_replace_len((linenr_T)1, uep->ue_array[i].ul_line,
2760 uep->ue_array[i].ul_len, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 else
Bram Moolenaar964b3742019-05-24 18:54:09 +02002762 ml_append(lnum, uep->ue_array[i].ul_line,
2763 (colnr_T)uep->ue_array[i].ul_len, FALSE);
Bram Moolenaarccae4672019-01-04 15:09:57 +01002764 vim_free(uep->ue_array[i].ul_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 }
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002766 vim_free((char_u *)uep->ue_array);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 }
2768
2769 /* adjust marks */
2770 if (oldsize != newsize)
2771 {
2772 mark_adjust(top + 1, top + oldsize, (long)MAXLNUM,
2773 (long)newsize - (long)oldsize);
2774 if (curbuf->b_op_start.lnum > top + oldsize)
2775 curbuf->b_op_start.lnum += newsize - oldsize;
2776 if (curbuf->b_op_end.lnum > top + oldsize)
2777 curbuf->b_op_end.lnum += newsize - oldsize;
2778 }
2779
2780 changed_lines(top + 1, 0, bot, newsize - oldsize);
2781
2782 /* set '[ and '] mark */
2783 if (top + 1 < curbuf->b_op_start.lnum)
2784 curbuf->b_op_start.lnum = top + 1;
2785 if (newsize == 0 && top + 1 > curbuf->b_op_end.lnum)
2786 curbuf->b_op_end.lnum = top + 1;
2787 else if (top + newsize > curbuf->b_op_end.lnum)
2788 curbuf->b_op_end.lnum = top + newsize;
2789
2790 u_newcount += newsize;
2791 u_oldcount += oldsize;
2792 uep->ue_size = oldsize;
2793 uep->ue_array = newarray;
2794 uep->ue_bot = top + newsize + 1;
2795
2796 /*
2797 * insert this entry in front of the new entry list
2798 */
2799 nuep = uep->ue_next;
2800 uep->ue_next = newlist;
2801 newlist = uep;
2802 }
2803
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002804 curhead->uh_entry = newlist;
2805 curhead->uh_flags = new_flags;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002806 if ((old_flags & UH_EMPTYBUF) && BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 curbuf->b_ml.ml_flags |= ML_EMPTY;
2808 if (old_flags & UH_CHANGED)
2809 changed();
2810 else
Bram Moolenaar009b2592004-10-24 19:18:58 +00002811#ifdef FEAT_NETBEANS_INTG
2812 /* per netbeans undo rules, keep it as modified */
2813 if (!isNetbeansModified(curbuf))
2814#endif
Bram Moolenaarc024b462019-06-08 18:07:21 +02002815 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816
2817 /*
2818 * restore marks from before undo/redo
2819 */
2820 for (i = 0; i < NMARKS; ++i)
Bram Moolenaarf65aad52015-02-17 13:43:40 +01002821 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002822 if (curhead->uh_namedm[i].lnum != 0)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002823 curbuf->b_namedm[i] = curhead->uh_namedm[i];
Bram Moolenaarf65aad52015-02-17 13:43:40 +01002824 if (namedm[i].lnum != 0)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002825 curhead->uh_namedm[i] = namedm[i];
Bram Moolenaarf65aad52015-02-17 13:43:40 +01002826 else
2827 curhead->uh_namedm[i].lnum = 0;
2828 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002829 if (curhead->uh_visual.vi_start.lnum != 0)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002830 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002831 curbuf->b_visual = curhead->uh_visual;
2832 curhead->uh_visual = visualinfo;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834
2835 /*
2836 * If the cursor is only off by one line, put it at the same position as
2837 * before starting the change (for the "o" command).
2838 * Otherwise the cursor should go to the first undone line.
2839 */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002840 if (curhead->uh_cursor.lnum + 1 == curwin->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 && curwin->w_cursor.lnum > 1)
2842 --curwin->w_cursor.lnum;
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002843 if (curwin->w_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 {
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002845 if (curhead->uh_cursor.lnum == curwin->w_cursor.lnum)
2846 {
2847 curwin->w_cursor.col = curhead->uh_cursor.col;
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002848 if (virtual_active() && curhead->uh_cursor_vcol >= 0)
2849 coladvance((colnr_T)curhead->uh_cursor_vcol);
2850 else
2851 curwin->w_cursor.coladd = 0;
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002852 }
2853 else
2854 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 else
2857 {
2858 /* We get here with the current cursor line being past the end (eg
2859 * after adding lines at the end of the file, and then undoing it).
2860 * check_cursor() will move the cursor to the last line. Move it to
2861 * the first column here. */
2862 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 }
2865
2866 /* Make sure the cursor is on an existing line and column. */
2867 check_cursor();
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002868
2869 /* Remember where we are for "g-" and ":earlier 10s". */
2870 curbuf->b_u_seq_cur = curhead->uh_seq;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002871 if (undo)
Bram Moolenaar80eaddd2017-11-11 23:37:08 +01002872 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002873 /* We are below the previous undo. However, to make ":earlier 1s"
2874 * work we compute this as being just above the just undone change. */
Bram Moolenaar80eaddd2017-11-11 23:37:08 +01002875 if (curhead->uh_next.ptr != NULL)
2876 curbuf->b_u_seq_cur = curhead->uh_next.ptr->uh_seq;
2877 else
2878 curbuf->b_u_seq_cur = 0;
2879 }
Bram Moolenaarca003e12006-03-17 23:19:38 +00002880
Bram Moolenaar730cde92010-06-27 05:18:54 +02002881 /* Remember where we are for ":earlier 1f" and ":later 1f". */
2882 if (curhead->uh_save_nr != 0)
2883 {
2884 if (undo)
2885 curbuf->b_u_save_nr_cur = curhead->uh_save_nr - 1;
2886 else
2887 curbuf->b_u_save_nr_cur = curhead->uh_save_nr;
2888 }
2889
Bram Moolenaarca003e12006-03-17 23:19:38 +00002890 /* The timestamp can be the same for multiple changes, just use the one of
2891 * the undone/redone change. */
Bram Moolenaara800b422010-06-27 01:15:55 +02002892 curbuf->b_u_time_cur = curhead->uh_time;
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002893
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002894 unblock_autocmds();
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002895#ifdef U_DEBUG
2896 u_check(FALSE);
2897#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898}
2899
2900/*
2901 * If we deleted or added lines, report the number of less/more lines.
2902 * Otherwise, report the number of changes (this may be incorrect
2903 * in some cases, but it's better than nothing).
2904 */
2905 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002906u_undo_end(
2907 int did_undo, /* just did an undo */
2908 int absolute) /* used ":undo N" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909{
Bram Moolenaar89d40322006-08-29 15:30:07 +00002910 char *msgstr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002911 u_header_T *uhp;
2912 char_u msgbuf[80];
Bram Moolenaar1e607892006-03-13 22:15:53 +00002913
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914#ifdef FEAT_FOLDING
2915 if ((fdo_flags & FDO_UNDO) && KeyTyped)
2916 foldOpenCursor();
2917#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +00002918
2919 if (global_busy /* no messages now, wait until global is finished */
2920 || !messaging()) /* 'lazyredraw' set, don't do messages now */
2921 return;
2922
2923 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2924 --u_newcount;
2925
2926 u_oldcount -= u_newcount;
2927 if (u_oldcount == -1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002928 msgstr = N_("more line");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002929 else if (u_oldcount < 0)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002930 msgstr = N_("more lines");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002931 else if (u_oldcount == 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002932 msgstr = N_("line less");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002933 else if (u_oldcount > 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002934 msgstr = N_("fewer lines");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002935 else
2936 {
2937 u_oldcount = u_newcount;
2938 if (u_newcount == 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002939 msgstr = N_("change");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002940 else
Bram Moolenaar89d40322006-08-29 15:30:07 +00002941 msgstr = N_("changes");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002942 }
2943
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002944 if (curbuf->b_u_curhead != NULL)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002945 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00002946 /* For ":undo N" we prefer a "after #N" message. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002947 if (absolute && curbuf->b_u_curhead->uh_next.ptr != NULL)
Bram Moolenaardb552d602006-03-23 22:59:57 +00002948 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002949 uhp = curbuf->b_u_curhead->uh_next.ptr;
Bram Moolenaardb552d602006-03-23 22:59:57 +00002950 did_undo = FALSE;
2951 }
2952 else if (did_undo)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002953 uhp = curbuf->b_u_curhead;
2954 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002955 uhp = curbuf->b_u_curhead->uh_next.ptr;
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002956 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002957 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002958 uhp = curbuf->b_u_newhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002959
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002960 if (uhp == NULL)
2961 *msgbuf = NUL;
2962 else
2963 u_add_time(msgbuf, sizeof(msgbuf), uhp->uh_time);
2964
Bram Moolenaarb2c03502010-07-02 20:20:09 +02002965#ifdef FEAT_CONCEAL
2966 {
2967 win_T *wp;
2968
2969 FOR_ALL_WINDOWS(wp)
2970 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02002971 if (wp->w_buffer == curbuf && wp->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02002972 redraw_win_later(wp, NOT_VALID);
2973 }
2974 }
2975#endif
2976
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002977 smsg_attr_keep(0, _("%ld %s; %s #%ld %s"),
Bram Moolenaarca003e12006-03-17 23:19:38 +00002978 u_oldcount < 0 ? -u_oldcount : u_oldcount,
Bram Moolenaar89d40322006-08-29 15:30:07 +00002979 _(msgstr),
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002980 did_undo ? _("before") : _("after"),
2981 uhp == NULL ? 0L : uhp->uh_seq,
2982 msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983}
2984
2985/*
2986 * u_sync: stop adding to the current entry list
2987 */
2988 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002989u_sync(
2990 int force) /* Also sync when no_u_sync is set. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991{
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002992 /* Skip it when already synced or syncing is disabled. */
2993 if (curbuf->b_u_synced || (!force && no_u_sync > 0))
2994 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02002996 if (p_imst == IM_ON_THE_SPOT && im_is_preediting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 return; /* XIM is busy, don't break an undo sequence */
2998#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002999 if (get_undolevel() < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 curbuf->b_u_synced = TRUE; /* no entries, nothing to do */
3001 else
3002 {
3003 u_getbot(); /* compute ue_bot of previous u_save */
3004 curbuf->b_u_curhead = NULL;
3005 }
3006}
3007
3008/*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003009 * ":undolist": List the leafs of the undo tree
3010 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003011 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003012ex_undolist(exarg_T *eap UNUSED)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003013{
3014 garray_T ga;
3015 u_header_T *uhp;
3016 int mark;
3017 int nomark;
3018 int changes = 1;
3019 int i;
3020
3021 /*
3022 * 1: walk the tree to find all leafs, put the info in "ga".
3023 * 2: sort the lines
3024 * 3: display the list
3025 */
3026 mark = ++lastmark;
3027 nomark = ++lastmark;
3028 ga_init2(&ga, (int)sizeof(char *), 20);
3029
3030 uhp = curbuf->b_u_oldhead;
3031 while (uhp != NULL)
3032 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003033 if (uhp->uh_prev.ptr == NULL && uhp->uh_walk != nomark
Bram Moolenaarca003e12006-03-17 23:19:38 +00003034 && uhp->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003035 {
3036 if (ga_grow(&ga, 1) == FAIL)
3037 break;
Bram Moolenaarea391762018-04-08 13:07:22 +02003038 vim_snprintf((char *)IObuff, IOSIZE, "%6ld %7d ",
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003039 uhp->uh_seq, changes);
3040 u_add_time(IObuff + STRLEN(IObuff), IOSIZE - STRLEN(IObuff),
3041 uhp->uh_time);
Bram Moolenaara800b422010-06-27 01:15:55 +02003042 if (uhp->uh_save_nr > 0)
3043 {
Bram Moolenaardba01a02010-11-03 19:32:42 +01003044 while (STRLEN(IObuff) < 33)
Bram Moolenaara800b422010-06-27 01:15:55 +02003045 STRCAT(IObuff, " ");
3046 vim_snprintf_add((char *)IObuff, IOSIZE,
3047 " %3ld", uhp->uh_save_nr);
3048 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003049 ((char_u **)(ga.ga_data))[ga.ga_len++] = vim_strsave(IObuff);
3050 }
3051
3052 uhp->uh_walk = mark;
3053
3054 /* go down in the tree if we haven't been there */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003055 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != nomark
3056 && uhp->uh_prev.ptr->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003057 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003058 uhp = uhp->uh_prev.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003059 ++changes;
3060 }
3061
3062 /* go to alternate branch if we haven't been there */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003063 else if (uhp->uh_alt_next.ptr != NULL
3064 && uhp->uh_alt_next.ptr->uh_walk != nomark
3065 && uhp->uh_alt_next.ptr->uh_walk != mark)
3066 uhp = uhp->uh_alt_next.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003067
3068 /* go up in the tree if we haven't been there and we are at the
3069 * start of alternate branches */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003070 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
3071 && uhp->uh_next.ptr->uh_walk != nomark
3072 && uhp->uh_next.ptr->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003073 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003074 uhp = uhp->uh_next.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003075 --changes;
3076 }
3077
3078 else
3079 {
3080 /* need to backtrack; mark this node as done */
3081 uhp->uh_walk = nomark;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003082 if (uhp->uh_alt_prev.ptr != NULL)
3083 uhp = uhp->uh_alt_prev.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003084 else
3085 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003086 uhp = uhp->uh_next.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003087 --changes;
3088 }
3089 }
3090 }
3091
3092 if (ga.ga_len == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003093 msg(_("Nothing to undo"));
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003094 else
3095 {
3096 sort_strings((char_u **)ga.ga_data, ga.ga_len);
3097
3098 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01003099 msg_puts_attr(_("number changes when saved"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01003100 HL_ATTR(HLF_T));
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003101 for (i = 0; i < ga.ga_len && !got_int; ++i)
3102 {
3103 msg_putchar('\n');
3104 if (got_int)
3105 break;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003106 msg_puts(((char **)ga.ga_data)[i]);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003107 }
3108 msg_end();
3109
3110 ga_clear_strings(&ga);
3111 }
3112}
3113
3114/*
3115 * Put the timestamp of an undo header in "buf[buflen]" in a nice format.
3116 */
3117 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003118u_add_time(char_u *buf, size_t buflen, time_t tt)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003119{
3120#ifdef HAVE_STRFTIME
Bram Moolenaar63d25552019-05-10 21:28:38 +02003121 struct tm tmval;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003122 struct tm *curtime;
3123
Bram Moolenaar170b10b2016-07-29 16:15:27 +02003124 if (vim_time() - tt >= 100)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003125 {
Bram Moolenaardb517302019-06-18 22:53:24 +02003126 curtime = vim_localtime(&tt, &tmval);
Bram Moolenaar170b10b2016-07-29 16:15:27 +02003127 if (vim_time() - tt < (60L * 60L * 12L))
Bram Moolenaardba01a02010-11-03 19:32:42 +01003128 /* within 12 hours */
3129 (void)strftime((char *)buf, buflen, "%H:%M:%S", curtime);
Bram Moolenaardba01a02010-11-03 19:32:42 +01003130 else
Bram Moolenaarb9ce83e2012-08-23 12:59:02 +02003131 /* longer ago */
Bram Moolenaar5e3d6ca2011-01-22 21:25:11 +01003132 (void)strftime((char *)buf, buflen, "%Y/%m/%d %H:%M:%S", curtime);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003133 }
3134 else
3135#endif
Bram Moolenaarfd6100b2018-08-21 17:07:45 +02003136 {
3137 long seconds = (long)(vim_time() - tt);
3138
3139 vim_snprintf((char *)buf, buflen,
3140 NGETTEXT("%ld second ago", "%ld seconds ago", seconds),
3141 seconds);
3142 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003143}
3144
3145/*
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003146 * ":undojoin": continue adding to the last entry list
3147 */
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003148 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003149ex_undojoin(exarg_T *eap UNUSED)
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003150{
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003151 if (curbuf->b_u_newhead == NULL)
3152 return; /* nothing changed before */
Bram Moolenaar57657d82006-04-21 22:12:41 +00003153 if (curbuf->b_u_curhead != NULL)
3154 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003155 emsg(_("E790: undojoin is not allowed after undo"));
Bram Moolenaar57657d82006-04-21 22:12:41 +00003156 return;
3157 }
3158 if (!curbuf->b_u_synced)
3159 return; /* already unsynced */
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003160 if (get_undolevel() < 0)
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003161 return; /* no entries, nothing to do */
3162 else
Bram Moolenaar5e4e1b12017-01-17 22:09:45 +01003163 /* Append next change to the last entry */
3164 curbuf->b_u_synced = FALSE;
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003165}
3166
3167/*
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003168 * Called after writing or reloading the file and setting b_changed to FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 * Now an undo means that the buffer is modified.
3170 */
3171 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003172u_unchanged(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173{
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003174 u_unch_branch(buf->b_u_oldhead);
3175 buf->b_did_warn = FALSE;
3176}
3177
Bram Moolenaar730cde92010-06-27 05:18:54 +02003178/*
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003179 * After reloading a buffer which was saved for 'undoreload': Find the first
3180 * line that was changed and set the cursor there.
3181 */
3182 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003183u_find_first_changed(void)
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003184{
3185 u_header_T *uhp = curbuf->b_u_newhead;
3186 u_entry_T *uep;
3187 linenr_T lnum;
3188
3189 if (curbuf->b_u_curhead != NULL || uhp == NULL)
3190 return; /* undid something in an autocmd? */
3191
3192 /* Check that the last undo block was for the whole file. */
3193 uep = uhp->uh_entry;
3194 if (uep->ue_top != 0 || uep->ue_bot != 0)
3195 return;
3196
3197 for (lnum = 1; lnum < curbuf->b_ml.ml_line_count
3198 && lnum <= uep->ue_size; ++lnum)
Bram Moolenaarccae4672019-01-04 15:09:57 +01003199 {
3200 char_u *p = ml_get_buf(curbuf, lnum, FALSE);
3201
3202 if (uep->ue_array[lnum - 1].ul_len != curbuf->b_ml.ml_line_len
3203 || memcmp(p, uep->ue_array[lnum - 1].ul_line, uep->ue_array[lnum - 1].ul_len) != 0)
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003204 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003205 CLEAR_POS(&(uhp->uh_cursor));
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003206 uhp->uh_cursor.lnum = lnum;
3207 return;
3208 }
Bram Moolenaarccae4672019-01-04 15:09:57 +01003209 }
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003210 if (curbuf->b_ml.ml_line_count != uep->ue_size)
3211 {
3212 /* lines added or deleted at the end, put the cursor there */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003213 CLEAR_POS(&(uhp->uh_cursor));
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003214 uhp->uh_cursor.lnum = lnum;
3215 }
3216}
3217
3218/*
Bram Moolenaar730cde92010-06-27 05:18:54 +02003219 * Increase the write count, store it in the last undo header, what would be
3220 * used for "u".
3221 */
3222 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003223u_update_save_nr(buf_T *buf)
Bram Moolenaar730cde92010-06-27 05:18:54 +02003224{
3225 u_header_T *uhp;
3226
3227 ++buf->b_u_save_nr_last;
3228 buf->b_u_save_nr_cur = buf->b_u_save_nr_last;
3229 uhp = buf->b_u_curhead;
3230 if (uhp != NULL)
3231 uhp = uhp->uh_next.ptr;
3232 else
3233 uhp = buf->b_u_newhead;
3234 if (uhp != NULL)
3235 uhp->uh_save_nr = buf->b_u_save_nr_last;
3236}
3237
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003238 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003239u_unch_branch(u_header_T *uhp)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003240{
Bram Moolenaar1e607892006-03-13 22:15:53 +00003241 u_header_T *uh;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003243 for (uh = uhp; uh != NULL; uh = uh->uh_prev.ptr)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003244 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 uh->uh_flags |= UH_CHANGED;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003246 if (uh->uh_alt_next.ptr != NULL)
3247 u_unch_branch(uh->uh_alt_next.ptr); /* recursive */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249}
3250
3251/*
3252 * Get pointer to last added entry.
3253 * If it's not valid, give an error message and return NULL.
3254 */
3255 static u_entry_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003256u_get_headentry(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257{
3258 if (curbuf->b_u_newhead == NULL || curbuf->b_u_newhead->uh_entry == NULL)
3259 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003260 iemsg(_("E439: undo list corrupt"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 return NULL;
3262 }
3263 return curbuf->b_u_newhead->uh_entry;
3264}
3265
3266/*
3267 * u_getbot(): compute the line number of the previous u_save
3268 * It is called only when b_u_synced is FALSE.
3269 */
3270 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003271u_getbot(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272{
3273 u_entry_T *uep;
3274 linenr_T extra;
3275
3276 uep = u_get_headentry(); /* check for corrupt undo list */
3277 if (uep == NULL)
3278 return;
3279
3280 uep = curbuf->b_u_newhead->uh_getbot_entry;
3281 if (uep != NULL)
3282 {
3283 /*
3284 * the new ue_bot is computed from the number of lines that has been
3285 * inserted (0 - deleted) since calling u_save. This is equal to the
3286 * old line count subtracted from the current line count.
3287 */
3288 extra = curbuf->b_ml.ml_line_count - uep->ue_lcount;
3289 uep->ue_bot = uep->ue_top + uep->ue_size + 1 + extra;
3290 if (uep->ue_bot < 1 || uep->ue_bot > curbuf->b_ml.ml_line_count)
3291 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003292 iemsg(_("E440: undo line missing"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 uep->ue_bot = uep->ue_top + 1; /* assume all lines deleted, will
3294 * get all the old lines back
3295 * without deleting the current
3296 * ones */
3297 }
3298
3299 curbuf->b_u_newhead->uh_getbot_entry = NULL;
3300 }
3301
3302 curbuf->b_u_synced = TRUE;
3303}
3304
3305/*
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003306 * Free one header "uhp" and its entry list and adjust the pointers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 */
3308 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003309u_freeheader(
3310 buf_T *buf,
3311 u_header_T *uhp,
3312 u_header_T **uhpp) /* if not NULL reset when freeing this header */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313{
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003314 u_header_T *uhap;
3315
Bram Moolenaar1e607892006-03-13 22:15:53 +00003316 /* When there is an alternate redo list free that branch completely,
3317 * because we can never go there. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003318 if (uhp->uh_alt_next.ptr != NULL)
3319 u_freebranch(buf, uhp->uh_alt_next.ptr, uhpp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003321 if (uhp->uh_alt_prev.ptr != NULL)
3322 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323
Bram Moolenaar1e607892006-03-13 22:15:53 +00003324 /* Update the links in the list to remove the header. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003325 if (uhp->uh_next.ptr == NULL)
3326 buf->b_u_oldhead = uhp->uh_prev.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003328 uhp->uh_next.ptr->uh_prev.ptr = uhp->uh_prev.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003330 if (uhp->uh_prev.ptr == NULL)
3331 buf->b_u_newhead = uhp->uh_next.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003333 for (uhap = uhp->uh_prev.ptr; uhap != NULL;
3334 uhap = uhap->uh_alt_next.ptr)
3335 uhap->uh_next.ptr = uhp->uh_next.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336
Bram Moolenaar1e607892006-03-13 22:15:53 +00003337 u_freeentries(buf, uhp, uhpp);
3338}
3339
3340/*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003341 * Free an alternate branch and any following alternate branches.
Bram Moolenaar1e607892006-03-13 22:15:53 +00003342 */
3343 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003344u_freebranch(
3345 buf_T *buf,
3346 u_header_T *uhp,
3347 u_header_T **uhpp) /* if not NULL reset when freeing this header */
Bram Moolenaar1e607892006-03-13 22:15:53 +00003348{
3349 u_header_T *tofree, *next;
3350
Bram Moolenaar07d06772007-11-10 21:51:15 +00003351 /* If this is the top branch we may need to use u_freeheader() to update
3352 * all the pointers. */
3353 if (uhp == buf->b_u_oldhead)
3354 {
Bram Moolenaaraa887322013-11-07 03:04:11 +01003355 while (buf->b_u_oldhead != NULL)
3356 u_freeheader(buf, buf->b_u_oldhead, uhpp);
Bram Moolenaar07d06772007-11-10 21:51:15 +00003357 return;
3358 }
3359
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003360 if (uhp->uh_alt_prev.ptr != NULL)
3361 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +00003362
3363 next = uhp;
3364 while (next != NULL)
3365 {
3366 tofree = next;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003367 if (tofree->uh_alt_next.ptr != NULL)
3368 u_freebranch(buf, tofree->uh_alt_next.ptr, uhpp); /* recursive */
3369 next = tofree->uh_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00003370 u_freeentries(buf, tofree, uhpp);
3371 }
3372}
3373
3374/*
3375 * Free all the undo entries for one header and the header itself.
3376 * This means that "uhp" is invalid when returning.
3377 */
3378 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003379u_freeentries(
3380 buf_T *buf,
3381 u_header_T *uhp,
3382 u_header_T **uhpp) /* if not NULL reset when freeing this header */
Bram Moolenaar1e607892006-03-13 22:15:53 +00003383{
3384 u_entry_T *uep, *nuep;
3385
3386 /* Check for pointers to the header that become invalid now. */
3387 if (buf->b_u_curhead == uhp)
3388 buf->b_u_curhead = NULL;
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003389 if (buf->b_u_newhead == uhp)
3390 buf->b_u_newhead = NULL; /* freeing the newest entry */
Bram Moolenaar1e607892006-03-13 22:15:53 +00003391 if (uhpp != NULL && uhp == *uhpp)
3392 *uhpp = NULL;
3393
3394 for (uep = uhp->uh_entry; uep != NULL; uep = nuep)
3395 {
3396 nuep = uep->ue_next;
3397 u_freeentry(uep, uep->ue_size);
3398 }
3399
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003400#ifdef U_DEBUG
3401 uhp->uh_magic = 0;
3402#endif
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003403 vim_free((char_u *)uhp);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003404 --buf->b_u_numhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405}
3406
3407/*
3408 * free entry 'uep' and 'n' lines in uep->ue_array[]
3409 */
3410 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003411u_freeentry(u_entry_T *uep, long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412{
Bram Moolenaar8d343302005-07-12 22:46:17 +00003413 while (n > 0)
Bram Moolenaarccae4672019-01-04 15:09:57 +01003414 vim_free(uep->ue_array[--n].ul_line);
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003415 vim_free((char_u *)uep->ue_array);
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003416#ifdef U_DEBUG
3417 uep->ue_magic = 0;
3418#endif
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003419 vim_free((char_u *)uep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420}
3421
3422/*
3423 * invalidate the undo buffer; called when storage has already been released
3424 */
3425 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003426u_clearall(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427{
3428 buf->b_u_newhead = buf->b_u_oldhead = buf->b_u_curhead = NULL;
3429 buf->b_u_synced = TRUE;
3430 buf->b_u_numhead = 0;
Bram Moolenaarccae4672019-01-04 15:09:57 +01003431 buf->b_u_line_ptr.ul_line = NULL;
3432 buf->b_u_line_ptr.ul_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 buf->b_u_line_lnum = 0;
3434}
3435
3436/*
Bram Moolenaarccae4672019-01-04 15:09:57 +01003437 * Save the line "lnum" for the "U" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003439 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003440u_saveline(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441{
3442 if (lnum == curbuf->b_u_line_lnum) /* line is already saved */
3443 return;
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00003444 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) /* should never happen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 return;
3446 u_clearline();
3447 curbuf->b_u_line_lnum = lnum;
3448 if (curwin->w_cursor.lnum == lnum)
3449 curbuf->b_u_line_colnr = curwin->w_cursor.col;
3450 else
3451 curbuf->b_u_line_colnr = 0;
Bram Moolenaarccae4672019-01-04 15:09:57 +01003452 if (u_save_line(&curbuf->b_u_line_ptr, lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 do_outofmem_msg((long_u)0);
3454}
3455
3456/*
3457 * clear the line saved for the "U" command
3458 * (this is used externally for crossing a line while in insert mode)
3459 */
3460 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003461u_clearline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462{
Bram Moolenaarccae4672019-01-04 15:09:57 +01003463 if (curbuf->b_u_line_ptr.ul_line != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 {
Bram Moolenaarccae4672019-01-04 15:09:57 +01003465 VIM_CLEAR(curbuf->b_u_line_ptr.ul_line);
3466 curbuf->b_u_line_ptr.ul_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 curbuf->b_u_line_lnum = 0;
3468 }
3469}
3470
3471/*
3472 * Implementation of the "U" command.
3473 * Differentiation from vi: "U" can be undone with the next "U".
3474 * We also allow the cursor to be in another line.
Bram Moolenaard04b7502010-07-08 22:27:55 +02003475 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 */
3477 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003478u_undoline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479{
Bram Moolenaarccae4672019-01-04 15:09:57 +01003480 colnr_T t;
3481 undoline_T oldp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482
3483 if (undo_off)
3484 return;
3485
Bram Moolenaarccae4672019-01-04 15:09:57 +01003486 if (curbuf->b_u_line_ptr.ul_line == NULL
Bram Moolenaare3300c82008-02-13 14:21:38 +00003487 || curbuf->b_u_line_lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 {
3489 beep_flush();
3490 return;
3491 }
Bram Moolenaare3300c82008-02-13 14:21:38 +00003492
Bram Moolenaarccae4672019-01-04 15:09:57 +01003493 // first save the line for the 'u' command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 if (u_savecommon(curbuf->b_u_line_lnum - 1,
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003495 curbuf->b_u_line_lnum + 1, (linenr_T)0, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 return;
Bram Moolenaarccae4672019-01-04 15:09:57 +01003497 if (u_save_line(&oldp, curbuf->b_u_line_lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 {
3499 do_outofmem_msg((long_u)0);
3500 return;
3501 }
Bram Moolenaarccae4672019-01-04 15:09:57 +01003502 ml_replace_len(curbuf->b_u_line_lnum, curbuf->b_u_line_ptr.ul_line, curbuf->b_u_line_ptr.ul_len, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 changed_bytes(curbuf->b_u_line_lnum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 curbuf->b_u_line_ptr = oldp;
3505
3506 t = curbuf->b_u_line_colnr;
3507 if (curwin->w_cursor.lnum == curbuf->b_u_line_lnum)
3508 curbuf->b_u_line_colnr = curwin->w_cursor.col;
3509 curwin->w_cursor.col = t;
3510 curwin->w_cursor.lnum = curbuf->b_u_line_lnum;
Bram Moolenaare3300c82008-02-13 14:21:38 +00003511 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512}
3513
3514/*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003515 * Free all allocated memory blocks for the buffer 'buf'.
3516 */
3517 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003518u_blockfree(buf_T *buf)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003519{
Bram Moolenaar1e607892006-03-13 22:15:53 +00003520 while (buf->b_u_oldhead != NULL)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003521 u_freeheader(buf, buf->b_u_oldhead, NULL);
Bram Moolenaarccae4672019-01-04 15:09:57 +01003522 vim_free(buf->b_u_line_ptr.ul_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523}
3524
3525/*
3526 * Check if the 'modified' flag is set, or 'ff' has changed (only need to
3527 * check the first character, because it can only be "dos", "unix" or "mac").
3528 * "nofile" and "scratch" type buffers are considered to always be unchanged.
Bram Moolenaarf405c8f2017-12-09 19:51:49 +01003529 * Also considers a buffer changed when a terminal window contains a running
3530 * job.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 */
3532 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003533bufIsChanged(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534{
Bram Moolenaareb44a682017-08-03 22:44:55 +02003535#ifdef FEAT_TERMINAL
3536 if (term_job_running(buf->b_term))
3537 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538#endif
Bram Moolenaarf405c8f2017-12-09 19:51:49 +01003539 return bufIsChangedNotTerm(buf);
3540}
3541
3542/*
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01003543 * Return TRUE if any buffer has changes. Also buffers that are not written.
3544 */
3545 int
3546anyBufIsChanged(void)
3547{
3548 buf_T *buf;
3549
3550 FOR_ALL_BUFFERS(buf)
3551 if (bufIsChanged(buf))
3552 return TRUE;
Bram Moolenaard6c3f1f2019-03-26 00:31:21 +01003553 return FALSE;
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01003554}
3555
3556/*
Bram Moolenaarf405c8f2017-12-09 19:51:49 +01003557 * Like bufIsChanged() but ignoring a terminal window.
3558 */
3559 int
3560bufIsChangedNotTerm(buf_T *buf)
3561{
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003562 // In a "prompt" buffer we do respect 'modified', so that we can control
3563 // closing the window by setting or resetting that option.
3564 return (!bt_dontwrite(buf) || bt_prompt(buf))
Bram Moolenaareb44a682017-08-03 22:44:55 +02003565 && (buf->b_changed || file_ff_differs(buf, TRUE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566}
3567
3568 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003569curbufIsChanged(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570{
Bram Moolenaareb44a682017-08-03 22:44:55 +02003571 return bufIsChanged(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572}
Bram Moolenaara800b422010-06-27 01:15:55 +02003573
3574#if defined(FEAT_EVAL) || defined(PROTO)
3575/*
3576 * For undotree(): Append the list of undo blocks at "first_uhp" to "list".
3577 * Recursive.
3578 */
3579 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003580u_eval_tree(u_header_T *first_uhp, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02003581{
3582 u_header_T *uhp = first_uhp;
3583 dict_T *dict;
3584
3585 while (uhp != NULL)
3586 {
3587 dict = dict_alloc();
3588 if (dict == NULL)
3589 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003590 dict_add_number(dict, "seq", uhp->uh_seq);
3591 dict_add_number(dict, "time", (long)uhp->uh_time);
Bram Moolenaara800b422010-06-27 01:15:55 +02003592 if (uhp == curbuf->b_u_newhead)
Bram Moolenaare0be1672018-07-08 16:50:37 +02003593 dict_add_number(dict, "newhead", 1);
Bram Moolenaara800b422010-06-27 01:15:55 +02003594 if (uhp == curbuf->b_u_curhead)
Bram Moolenaare0be1672018-07-08 16:50:37 +02003595 dict_add_number(dict, "curhead", 1);
Bram Moolenaara800b422010-06-27 01:15:55 +02003596 if (uhp->uh_save_nr > 0)
Bram Moolenaare0be1672018-07-08 16:50:37 +02003597 dict_add_number(dict, "save", uhp->uh_save_nr);
Bram Moolenaara800b422010-06-27 01:15:55 +02003598
3599 if (uhp->uh_alt_next.ptr != NULL)
3600 {
3601 list_T *alt_list = list_alloc();
3602
3603 if (alt_list != NULL)
3604 {
3605 /* Recursive call to add alternate undo tree. */
3606 u_eval_tree(uhp->uh_alt_next.ptr, alt_list);
3607 dict_add_list(dict, "alt", alt_list);
3608 }
3609 }
3610
3611 list_append_dict(list, dict);
3612 uhp = uhp->uh_prev.ptr;
3613 }
3614}
3615#endif