blob: 5dafee813b16e30fce5da5930644a996dcf70d7b [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 Moolenaar071d4272004-06-13 20:20:40 +0000126
Bram Moolenaarf05e3b02010-05-29 15:40:47 +0200127#define U_ALLOC_LINE(size) lalloc((long_u)(size), FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128
Bram Moolenaar730cde92010-06-27 05:18:54 +0200129/* used in undo_end() to report number of added and deleted lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130static long u_newcount, u_oldcount;
131
132/*
133 * When 'u' flag included in 'cpoptions', we behave like vi. Need to remember
134 * the action that "u" should do.
135 */
136static int undo_undoes = FALSE;
137
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200138static int lastmark = 0;
139
Bram Moolenaar9db58062010-05-29 20:33:07 +0200140#if defined(U_DEBUG) || defined(PROTO)
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000141/*
142 * Check the undo structures for being valid. Print a warning when something
143 * looks wrong.
144 */
145static int seen_b_u_curhead;
146static int seen_b_u_newhead;
147static int header_count;
148
149 static void
150u_check_tree(u_header_T *uhp,
151 u_header_T *exp_uh_next,
152 u_header_T *exp_uh_alt_prev)
153{
154 u_entry_T *uep;
155
156 if (uhp == NULL)
157 return;
158 ++header_count;
159 if (uhp == curbuf->b_u_curhead && ++seen_b_u_curhead > 1)
160 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100161 emsg("b_u_curhead found twice (looping?)");
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000162 return;
163 }
164 if (uhp == curbuf->b_u_newhead && ++seen_b_u_newhead > 1)
165 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100166 emsg("b_u_newhead found twice (looping?)");
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000167 return;
168 }
169
170 if (uhp->uh_magic != UH_MAGIC)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100171 emsg("uh_magic wrong (may be using freed memory)");
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000172 else
173 {
174 /* Check pointers back are correct. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200175 if (uhp->uh_next.ptr != exp_uh_next)
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000176 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100177 emsg("uh_next wrong");
178 smsg("expected: 0x%x, actual: 0x%x",
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200179 exp_uh_next, uhp->uh_next.ptr);
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000180 }
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200181 if (uhp->uh_alt_prev.ptr != exp_uh_alt_prev)
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000182 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100183 emsg("uh_alt_prev wrong");
184 smsg("expected: 0x%x, actual: 0x%x",
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200185 exp_uh_alt_prev, uhp->uh_alt_prev.ptr);
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000186 }
187
188 /* Check the undo tree at this header. */
189 for (uep = uhp->uh_entry; uep != NULL; uep = uep->ue_next)
190 {
191 if (uep->ue_magic != UE_MAGIC)
192 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100193 emsg("ue_magic wrong (may be using freed memory)");
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000194 break;
195 }
196 }
197
198 /* Check the next alt tree. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200199 u_check_tree(uhp->uh_alt_next.ptr, uhp->uh_next.ptr, uhp);
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000200
201 /* Check the next header in this branch. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200202 u_check_tree(uhp->uh_prev.ptr, uhp, NULL);
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000203 }
204}
205
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200206 static void
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000207u_check(int newhead_may_be_NULL)
208{
209 seen_b_u_newhead = 0;
210 seen_b_u_curhead = 0;
211 header_count = 0;
212
213 u_check_tree(curbuf->b_u_oldhead, NULL, NULL);
214
215 if (seen_b_u_newhead == 0 && curbuf->b_u_oldhead != NULL
216 && !(newhead_may_be_NULL && curbuf->b_u_newhead == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100217 semsg("b_u_newhead invalid: 0x%x", curbuf->b_u_newhead);
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000218 if (curbuf->b_u_curhead != NULL && seen_b_u_curhead == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100219 semsg("b_u_curhead invalid: 0x%x", curbuf->b_u_curhead);
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000220 if (header_count != curbuf->b_u_numhead)
221 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100222 emsg("b_u_numhead invalid");
223 smsg("expected: %ld, actual: %ld",
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000224 (long)header_count, (long)curbuf->b_u_numhead);
225 }
226}
227#endif
228
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000230 * Save the current line for both the "u" and "U" command.
Bram Moolenaar687a29c2013-04-15 15:47:12 +0200231 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000232 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 */
234 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100235u_save_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236{
237 return (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
238 (linenr_T)(curwin->w_cursor.lnum + 1)));
239}
240
241/*
242 * Save the lines between "top" and "bot" for both the "u" and "U" command.
243 * "top" may be 0 and bot may be curbuf->b_ml.ml_line_count + 1.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200244 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 * Returns FAIL when lines could not be saved, OK otherwise.
246 */
247 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100248u_save(linenr_T top, linenr_T bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249{
250 if (undo_off)
251 return OK;
252
Bram Moolenaarefc81332018-07-13 16:31:19 +0200253 if (top >= bot || bot > curbuf->b_ml.ml_line_count + 1)
254 return FAIL; // rely on caller to give an error message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255
256 if (top + 2 == bot)
257 u_saveline((linenr_T)(top + 1));
258
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200259 return (u_savecommon(top, bot, (linenr_T)0, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260}
261
262/*
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200263 * Save the line "lnum" (used by ":s" and "~" command).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 * The line is replaced, so the new bottom line is lnum + 1.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200265 * Careful: may trigger autocommands that reload the buffer.
266 * Returns FAIL when lines could not be saved, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 */
268 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100269u_savesub(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270{
271 if (undo_off)
272 return OK;
273
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200274 return (u_savecommon(lnum - 1, lnum + 1, lnum + 1, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275}
276
277/*
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200278 * A new line is inserted before line "lnum" (used by :s command).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279 * The line is inserted, so the new bottom line is lnum + 1.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200280 * Careful: may trigger autocommands that reload the buffer.
281 * Returns FAIL when lines could not be saved, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282 */
283 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100284u_inssub(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285{
286 if (undo_off)
287 return OK;
288
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200289 return (u_savecommon(lnum - 1, lnum, lnum + 1, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290}
291
292/*
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200293 * Save the lines "lnum" - "lnum" + nlines (used by delete command).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 * The lines are deleted, so the new bottom line is lnum, unless the buffer
295 * becomes empty.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200296 * Careful: may trigger autocommands that reload the buffer.
297 * Returns FAIL when lines could not be saved, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 */
299 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100300u_savedel(linenr_T lnum, long nlines)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301{
302 if (undo_off)
303 return OK;
304
305 return (u_savecommon(lnum - 1, lnum + nlines,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200306 nlines == curbuf->b_ml.ml_line_count ? 2 : lnum, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307}
308
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000309/*
310 * Return TRUE when undo is allowed. Otherwise give an error message and
311 * return FALSE.
312 */
Bram Moolenaarce6ef252006-07-12 19:49:41 +0000313 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100314undo_allowed(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000315{
316 /* Don't allow changes when 'modifiable' is off. */
317 if (!curbuf->b_p_ma)
318 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100319 emsg(_(e_modifiable));
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000320 return FALSE;
321 }
322
323#ifdef HAVE_SANDBOX
324 /* In the sandbox it's not allowed to change the text. */
325 if (sandbox != 0)
326 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100327 emsg(_(e_sandbox));
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000328 return FALSE;
329 }
330#endif
331
332 /* Don't allow changes in the buffer while editing the cmdline. The
333 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000334 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000335 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100336 emsg(_(e_secure));
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000337 return FALSE;
338 }
339
340 return TRUE;
341}
342
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200343/*
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100344 * Get the undolevle value for the current buffer.
345 */
346 static long
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100347get_undolevel(void)
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100348{
349 if (curbuf->b_p_ul == NO_LOCAL_UNDOLEVEL)
350 return p_ul;
351 return curbuf->b_p_ul;
352}
353
354/*
Bram Moolenaarccae4672019-01-04 15:09:57 +0100355 * u_save_line(): save an allocated copy of line "lnum" into "ul".
356 * Returns FAIL when out of memory.
357 */
358 static int
359u_save_line(undoline_T *ul, linenr_T lnum)
360{
361 char_u *line = ml_get(lnum);
362
363 if (curbuf->b_ml.ml_line_len == 0)
364 {
365 ul->ul_len = 1;
366 ul->ul_line = vim_strsave((char_u *)"");
367 }
368 else
369 {
370 ul->ul_len = curbuf->b_ml.ml_line_len;
371 ul->ul_line = vim_memsave(line, ul->ul_len);
372 }
373 return ul->ul_line == NULL ? FAIL : OK;
374}
375
376/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200377 * Common code for various ways to save text before a change.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200378 * "top" is the line above the first changed line.
379 * "bot" is the line below the last changed line.
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200380 * "newbot" is the new bottom line. Use zero when not known.
381 * "reload" is TRUE when saving for a buffer reload.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200382 * Careful: may trigger autocommands that reload the buffer.
383 * Returns FAIL when lines could not be saved, OK otherwise.
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200384 */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200385 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100386u_savecommon(
387 linenr_T top,
388 linenr_T bot,
389 linenr_T newbot,
390 int reload)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391{
Bram Moolenaar1e607892006-03-13 22:15:53 +0000392 linenr_T lnum;
393 long i;
394 u_header_T *uhp;
395 u_header_T *old_curhead;
396 u_entry_T *uep;
397 u_entry_T *prev_uep;
398 long size;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200400 if (!reload)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 {
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200402 /* When making changes is not allowed return FAIL. It's a crude way
403 * to make all change commands fail. */
404 if (!undo_allowed())
Bram Moolenaar009b2592004-10-24 19:18:58 +0000405 return FAIL;
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200406
407#ifdef FEAT_NETBEANS_INTG
408 /*
409 * Netbeans defines areas that cannot be modified. Bail out here when
410 * trying to change text in a guarded area.
411 */
412 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +0000413 {
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200414 if (netbeans_is_guarded(top, bot))
415 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100416 emsg(_(e_guarded));
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200417 return FAIL;
418 }
419 if (curbuf->b_p_ro)
420 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100421 emsg(_(e_nbreadonly));
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200422 return FAIL;
423 }
Bram Moolenaar009b2592004-10-24 19:18:58 +0000424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200426#ifdef FEAT_TERMINAL
427 /* A change in a terminal buffer removes the highlighting. */
428 term_change_in_curbuf();
429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200431 /*
432 * Saving text for undo means we are going to make a change. Give a
433 * warning for a read-only file before making the change, so that the
434 * FileChangedRO event can replace the buffer with a read-write version
435 * (e.g., obtained from a source control system).
436 */
437 change_warning(0);
438 if (bot > curbuf->b_ml.ml_line_count + 1)
439 {
440 /* This happens when the FileChangedRO autocommand changes the
441 * file in a way it becomes shorter. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100442 emsg(_("E881: Line count changed unexpectedly"));
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200443 return FAIL;
444 }
Bram Moolenaard04b7502010-07-08 22:27:55 +0200445 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200446
447#ifdef U_DEBUG
448 u_check(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449#endif
450
451 size = bot - top - 1;
452
453 /*
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200454 * If curbuf->b_u_synced == TRUE make a new header.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 */
456 if (curbuf->b_u_synced)
457 {
458#ifdef FEAT_JUMPLIST
459 /* Need to create new entry in b_changelist. */
460 curbuf->b_new_change = TRUE;
461#endif
462
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100463 if (get_undolevel() >= 0)
Bram Moolenaar1e607892006-03-13 22:15:53 +0000464 {
465 /*
466 * Make a new header entry. Do this first so that we don't mess
467 * up the undo info when out of memory.
468 */
Bram Moolenaarf05e3b02010-05-29 15:40:47 +0200469 uhp = (u_header_T *)U_ALLOC_LINE(sizeof(u_header_T));
Bram Moolenaar1e607892006-03-13 22:15:53 +0000470 if (uhp == NULL)
471 goto nomem;
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000472#ifdef U_DEBUG
473 uhp->uh_magic = UH_MAGIC;
474#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +0000475 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000476 else
477 uhp = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000478
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 /*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000480 * If we undid more than we redid, move the entry lists before and
481 * including curbuf->b_u_curhead to an alternate branch.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 */
Bram Moolenaar1e607892006-03-13 22:15:53 +0000483 old_curhead = curbuf->b_u_curhead;
484 if (old_curhead != NULL)
485 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200486 curbuf->b_u_newhead = old_curhead->uh_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000487 curbuf->b_u_curhead = NULL;
488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489
490 /*
491 * free headers to keep the size right
492 */
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100493 while (curbuf->b_u_numhead > get_undolevel()
494 && curbuf->b_u_oldhead != NULL)
Bram Moolenaar1e607892006-03-13 22:15:53 +0000495 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000496 u_header_T *uhfree = curbuf->b_u_oldhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000497
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000498 if (uhfree == old_curhead)
499 /* Can't reconnect the branch, delete all of it. */
500 u_freebranch(curbuf, uhfree, &old_curhead);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200501 else if (uhfree->uh_alt_next.ptr == NULL)
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000502 /* There is no branch, only free one header. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000503 u_freeheader(curbuf, uhfree, &old_curhead);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000504 else
505 {
506 /* Free the oldest alternate branch as a whole. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200507 while (uhfree->uh_alt_next.ptr != NULL)
508 uhfree = uhfree->uh_alt_next.ptr;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000509 u_freebranch(curbuf, uhfree, &old_curhead);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000510 }
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000511#ifdef U_DEBUG
512 u_check(TRUE);
513#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +0000514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000516 if (uhp == NULL) /* no undo at all */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 {
Bram Moolenaar1e607892006-03-13 22:15:53 +0000518 if (old_curhead != NULL)
519 u_freebranch(curbuf, old_curhead, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 curbuf->b_u_synced = FALSE;
521 return OK;
522 }
523
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200524 uhp->uh_prev.ptr = NULL;
525 uhp->uh_next.ptr = curbuf->b_u_newhead;
526 uhp->uh_alt_next.ptr = old_curhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000527 if (old_curhead != NULL)
528 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200529 uhp->uh_alt_prev.ptr = old_curhead->uh_alt_prev.ptr;
530 if (uhp->uh_alt_prev.ptr != NULL)
531 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = uhp;
532 old_curhead->uh_alt_prev.ptr = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000533 if (curbuf->b_u_oldhead == old_curhead)
534 curbuf->b_u_oldhead = uhp;
535 }
Bram Moolenaar89ed3df2007-01-09 19:23:12 +0000536 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200537 uhp->uh_alt_prev.ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 if (curbuf->b_u_newhead != NULL)
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200539 curbuf->b_u_newhead->uh_prev.ptr = uhp;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000540
Bram Moolenaarca003e12006-03-17 23:19:38 +0000541 uhp->uh_seq = ++curbuf->b_u_seq_last;
542 curbuf->b_u_seq_cur = uhp->uh_seq;
Bram Moolenaar170b10b2016-07-29 16:15:27 +0200543 uhp->uh_time = vim_time();
Bram Moolenaara800b422010-06-27 01:15:55 +0200544 uhp->uh_save_nr = 0;
545 curbuf->b_u_time_cur = uhp->uh_time + 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000546
Bram Moolenaar1e607892006-03-13 22:15:53 +0000547 uhp->uh_walk = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 uhp->uh_entry = NULL;
549 uhp->uh_getbot_entry = NULL;
550 uhp->uh_cursor = curwin->w_cursor; /* save cursor pos. for undo */
551#ifdef FEAT_VIRTUALEDIT
552 if (virtual_active() && curwin->w_cursor.coladd > 0)
553 uhp->uh_cursor_vcol = getviscol();
554 else
555 uhp->uh_cursor_vcol = -1;
556#endif
557
558 /* save changed and buffer empty flag for undo */
559 uhp->uh_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
560 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
561
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000562 /* save named marks and Visual marks for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 mch_memmove(uhp->uh_namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000564 uhp->uh_visual = curbuf->b_visual;
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000565
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 curbuf->b_u_newhead = uhp;
567 if (curbuf->b_u_oldhead == NULL)
568 curbuf->b_u_oldhead = uhp;
569 ++curbuf->b_u_numhead;
570 }
571 else
572 {
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100573 if (get_undolevel() < 0) /* no undo at all */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 return OK;
575
576 /*
577 * When saving a single line, and it has been saved just before, it
578 * doesn't make sense saving it again. Saves a lot of memory when
579 * making lots of changes inside the same line.
580 * This is only possible if the previous change didn't increase or
581 * decrease the number of lines.
582 * Check the ten last changes. More doesn't make sense and takes too
583 * long.
584 */
585 if (size == 1)
586 {
587 uep = u_get_headentry();
588 prev_uep = NULL;
589 for (i = 0; i < 10; ++i)
590 {
591 if (uep == NULL)
592 break;
593
594 /* If lines have been inserted/deleted we give up.
595 * Also when the line was included in a multi-line save. */
596 if ((curbuf->b_u_newhead->uh_getbot_entry != uep
597 ? (uep->ue_top + uep->ue_size + 1
598 != (uep->ue_bot == 0
599 ? curbuf->b_ml.ml_line_count + 1
600 : uep->ue_bot))
601 : uep->ue_lcount != curbuf->b_ml.ml_line_count)
602 || (uep->ue_size > 1
603 && top >= uep->ue_top
604 && top + 2 <= uep->ue_top + uep->ue_size + 1))
605 break;
606
607 /* If it's the same line we can skip saving it again. */
608 if (uep->ue_size == 1 && uep->ue_top == top)
609 {
610 if (i > 0)
611 {
612 /* It's not the last entry: get ue_bot for the last
613 * entry now. Following deleted/inserted lines go to
614 * the re-used entry. */
615 u_getbot();
616 curbuf->b_u_synced = FALSE;
617
618 /* Move the found entry to become the last entry. The
619 * order of undo/redo doesn't matter for the entries
620 * we move it over, since they don't change the line
621 * count and don't include this line. It does matter
622 * for the found entry if the line count is changed by
623 * the executed command. */
624 prev_uep->ue_next = uep->ue_next;
625 uep->ue_next = curbuf->b_u_newhead->uh_entry;
626 curbuf->b_u_newhead->uh_entry = uep;
627 }
628
629 /* The executed command may change the line count. */
630 if (newbot != 0)
631 uep->ue_bot = newbot;
632 else if (bot > curbuf->b_ml.ml_line_count)
633 uep->ue_bot = 0;
634 else
635 {
636 uep->ue_lcount = curbuf->b_ml.ml_line_count;
637 curbuf->b_u_newhead->uh_getbot_entry = uep;
638 }
639 return OK;
640 }
641 prev_uep = uep;
642 uep = uep->ue_next;
643 }
644 }
645
646 /* find line number for ue_bot for previous u_save() */
647 u_getbot();
648 }
649
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200650#if !defined(UNIX) && !defined(WIN32)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100652 * With Amiga we can't handle big undo's, because
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 * then u_alloc_line would have to allocate a block larger than 32K
654 */
655 if (size >= 8000)
656 goto nomem;
657#endif
658
659 /*
660 * add lines in front of entry list
661 */
Bram Moolenaarf05e3b02010-05-29 15:40:47 +0200662 uep = (u_entry_T *)U_ALLOC_LINE(sizeof(u_entry_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 if (uep == NULL)
664 goto nomem;
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200665 vim_memset(uep, 0, sizeof(u_entry_T));
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000666#ifdef U_DEBUG
667 uep->ue_magic = UE_MAGIC;
668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669
670 uep->ue_size = size;
671 uep->ue_top = top;
672 if (newbot != 0)
673 uep->ue_bot = newbot;
674 /*
675 * Use 0 for ue_bot if bot is below last line.
676 * Otherwise we have to compute ue_bot later.
677 */
678 else if (bot > curbuf->b_ml.ml_line_count)
679 uep->ue_bot = 0;
680 else
681 {
682 uep->ue_lcount = curbuf->b_ml.ml_line_count;
683 curbuf->b_u_newhead->uh_getbot_entry = uep;
684 }
685
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000686 if (size > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 {
Bram Moolenaarccae4672019-01-04 15:09:57 +0100688 if ((uep->ue_array = (undoline_T *)U_ALLOC_LINE(
689 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{
1126 char_u *ptr = alloc((unsigned)len + 1);
1127
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 Moolenaar6ed8ed82010-05-30 20:40:11 +02001253#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001254 undo_write_bytes(bi, (long_u)uhp->uh_cursor_vcol, 4);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001255#else
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001256 undo_write_bytes(bi, (long_u)0, 4);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001257#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001258 undo_write_bytes(bi, (long_u)uhp->uh_flags, 2);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001259 /* Assume NMARKS will stay the same. */
1260 for (i = 0; i < NMARKS; ++i)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001261 serialize_pos(bi, uhp->uh_namedm[i]);
1262 serialize_visualinfo(bi, &uhp->uh_visual);
1263 time_to_bytes(uhp->uh_time, time_buf);
1264 undo_write(bi, time_buf, 8);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001265
Bram Moolenaara800b422010-06-27 01:15:55 +02001266 /* Optional fields. */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001267 undo_write_bytes(bi, 4, 1);
1268 undo_write_bytes(bi, UHP_SAVE_NR, 1);
1269 undo_write_bytes(bi, (long_u)uhp->uh_save_nr, 4);
Bram Moolenaara800b422010-06-27 01:15:55 +02001270
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001271 undo_write_bytes(bi, 0, 1); /* end marker */
Bram Moolenaara800b422010-06-27 01:15:55 +02001272
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001273 /* Write all the entries. */
1274 for (uep = uhp->uh_entry; uep != NULL; uep = uep->ue_next)
1275 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001276 undo_write_bytes(bi, (long_u)UF_ENTRY_MAGIC, 2);
1277 if (serialize_uep(bi, uep) == FAIL)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001278 return FAIL;
1279 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001280 undo_write_bytes(bi, (long_u)UF_ENTRY_END_MAGIC, 2);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001281 return OK;
1282}
1283
1284 static u_header_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001285unserialize_uhp(bufinfo_T *bi, char_u *file_name)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001286{
1287 u_header_T *uhp;
1288 int i;
1289 u_entry_T *uep, *last_uep;
1290 int c;
1291 int error;
1292
1293 uhp = (u_header_T *)U_ALLOC_LINE(sizeof(u_header_T));
1294 if (uhp == NULL)
1295 return NULL;
1296 vim_memset(uhp, 0, sizeof(u_header_T));
1297#ifdef U_DEBUG
1298 uhp->uh_magic = UH_MAGIC;
1299#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001300 uhp->uh_next.seq = undo_read_4c(bi);
1301 uhp->uh_prev.seq = undo_read_4c(bi);
1302 uhp->uh_alt_next.seq = undo_read_4c(bi);
1303 uhp->uh_alt_prev.seq = undo_read_4c(bi);
1304 uhp->uh_seq = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001305 if (uhp->uh_seq <= 0)
1306 {
1307 corruption_error("uh_seq", file_name);
1308 vim_free(uhp);
1309 return NULL;
1310 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001311 unserialize_pos(bi, &uhp->uh_cursor);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001312#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001313 uhp->uh_cursor_vcol = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001314#else
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001315 (void)undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001316#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001317 uhp->uh_flags = undo_read_2c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001318 for (i = 0; i < NMARKS; ++i)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001319 unserialize_pos(bi, &uhp->uh_namedm[i]);
1320 unserialize_visualinfo(bi, &uhp->uh_visual);
1321 uhp->uh_time = undo_read_time(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001322
Bram Moolenaara800b422010-06-27 01:15:55 +02001323 /* Optional fields. */
Bram Moolenaar69154f22010-07-18 21:42:34 +02001324 for (;;)
1325 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001326 int len = undo_read_byte(bi);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001327 int what;
Bram Moolenaara800b422010-06-27 01:15:55 +02001328
Bram Moolenaar69154f22010-07-18 21:42:34 +02001329 if (len == 0)
1330 break;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001331 what = undo_read_byte(bi);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001332 switch (what)
1333 {
1334 case UHP_SAVE_NR:
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001335 uhp->uh_save_nr = undo_read_4c(bi);
Bram Moolenaara800b422010-06-27 01:15:55 +02001336 break;
Bram Moolenaar69154f22010-07-18 21:42:34 +02001337 default:
1338 /* field not supported, skip */
1339 while (--len >= 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001340 (void)undo_read_byte(bi);
Bram Moolenaara800b422010-06-27 01:15:55 +02001341 }
Bram Moolenaar69154f22010-07-18 21:42:34 +02001342 }
Bram Moolenaara800b422010-06-27 01:15:55 +02001343
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001344 /* Unserialize the uep list. */
1345 last_uep = NULL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001346 while ((c = undo_read_2c(bi)) == UF_ENTRY_MAGIC)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001347 {
1348 error = FALSE;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001349 uep = unserialize_uep(bi, &error, file_name);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001350 if (last_uep == NULL)
1351 uhp->uh_entry = uep;
1352 else
1353 last_uep->ue_next = uep;
1354 last_uep = uep;
1355 if (uep == NULL || error)
1356 {
1357 u_free_uhp(uhp);
1358 return NULL;
1359 }
1360 }
1361 if (c != UF_ENTRY_END_MAGIC)
1362 {
1363 corruption_error("entry end", file_name);
1364 u_free_uhp(uhp);
1365 return NULL;
1366 }
1367
1368 return uhp;
1369}
1370
Bram Moolenaar9db58062010-05-29 20:33:07 +02001371/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001372 * Serialize "uep".
Bram Moolenaar9db58062010-05-29 20:33:07 +02001373 */
1374 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001375serialize_uep(
1376 bufinfo_T *bi,
1377 u_entry_T *uep)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001378{
1379 int i;
1380 size_t len;
1381
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001382 undo_write_bytes(bi, (long_u)uep->ue_top, 4);
1383 undo_write_bytes(bi, (long_u)uep->ue_bot, 4);
1384 undo_write_bytes(bi, (long_u)uep->ue_lcount, 4);
1385 undo_write_bytes(bi, (long_u)uep->ue_size, 4);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001386 for (i = 0; i < uep->ue_size; ++i)
1387 {
Bram Moolenaarccae4672019-01-04 15:09:57 +01001388 // Text is written without the text properties, since we cannot restore
1389 // the text property types.
1390 len = STRLEN(uep->ue_array[i].ul_line);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001391 if (undo_write_bytes(bi, (long_u)len, 4) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001392 return FAIL;
Bram Moolenaarccae4672019-01-04 15:09:57 +01001393 if (len > 0 && fwrite_crypt(bi, uep->ue_array[i].ul_line, len) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001394 return FAIL;
1395 }
1396 return OK;
1397}
1398
1399 static u_entry_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001400unserialize_uep(bufinfo_T *bi, int *error, char_u *file_name)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001401{
1402 int i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001403 u_entry_T *uep;
Bram Moolenaarccae4672019-01-04 15:09:57 +01001404 undoline_T *array = NULL;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001405 char_u *line;
1406 int line_len;
1407
1408 uep = (u_entry_T *)U_ALLOC_LINE(sizeof(u_entry_T));
1409 if (uep == NULL)
1410 return NULL;
1411 vim_memset(uep, 0, sizeof(u_entry_T));
1412#ifdef U_DEBUG
1413 uep->ue_magic = UE_MAGIC;
1414#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001415 uep->ue_top = undo_read_4c(bi);
1416 uep->ue_bot = undo_read_4c(bi);
1417 uep->ue_lcount = undo_read_4c(bi);
1418 uep->ue_size = undo_read_4c(bi);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001419 if (uep->ue_size > 0)
1420 {
Bram Moolenaar0c8485f2017-02-26 18:17:10 +01001421 if (uep->ue_size < LONG_MAX / (int)sizeof(char_u *))
Bram Moolenaarccae4672019-01-04 15:09:57 +01001422 array = (undoline_T *)U_ALLOC_LINE(sizeof(undoline_T) * uep->ue_size);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001423 if (array == NULL)
1424 {
1425 *error = TRUE;
1426 return uep;
1427 }
Bram Moolenaarccae4672019-01-04 15:09:57 +01001428 vim_memset(array, 0, sizeof(undoline_T) * uep->ue_size);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001429 }
1430 uep->ue_array = array;
1431
1432 for (i = 0; i < uep->ue_size; ++i)
1433 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001434 line_len = undo_read_4c(bi);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001435 if (line_len >= 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001436 line = read_string_decrypt(bi, line_len);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001437 else
1438 {
1439 line = NULL;
1440 corruption_error("line length", file_name);
1441 }
1442 if (line == NULL)
1443 {
1444 *error = TRUE;
1445 return uep;
1446 }
Bram Moolenaarccae4672019-01-04 15:09:57 +01001447 array[i].ul_line = line;
1448 array[i].ul_len = line_len + 1;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001449 }
1450 return uep;
1451}
1452
1453/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001454 * Serialize "pos".
Bram Moolenaar9db58062010-05-29 20:33:07 +02001455 */
1456 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001457serialize_pos(bufinfo_T *bi, pos_T pos)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001458{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001459 undo_write_bytes(bi, (long_u)pos.lnum, 4);
1460 undo_write_bytes(bi, (long_u)pos.col, 4);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001461#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001462 undo_write_bytes(bi, (long_u)pos.coladd, 4);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001463#else
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001464 undo_write_bytes(bi, (long_u)0, 4);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001465#endif
1466}
1467
1468/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001469 * Unserialize the pos_T at the current position.
Bram Moolenaar9db58062010-05-29 20:33:07 +02001470 */
1471 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001472unserialize_pos(bufinfo_T *bi, pos_T *pos)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001473{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001474 pos->lnum = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001475 if (pos->lnum < 0)
1476 pos->lnum = 0;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001477 pos->col = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001478 if (pos->col < 0)
1479 pos->col = 0;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001480#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001481 pos->coladd = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001482 if (pos->coladd < 0)
1483 pos->coladd = 0;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001484#else
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001485 (void)undo_read_4c(bi);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001486#endif
1487}
1488
1489/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001490 * Serialize "info".
Bram Moolenaar9db58062010-05-29 20:33:07 +02001491 */
1492 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001493serialize_visualinfo(bufinfo_T *bi, visualinfo_T *info)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001494{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001495 serialize_pos(bi, info->vi_start);
1496 serialize_pos(bi, info->vi_end);
1497 undo_write_bytes(bi, (long_u)info->vi_mode, 4);
1498 undo_write_bytes(bi, (long_u)info->vi_curswant, 4);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001499}
1500
1501/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001502 * Unserialize the visualinfo_T at the current position.
Bram Moolenaar9db58062010-05-29 20:33:07 +02001503 */
1504 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001505unserialize_visualinfo(bufinfo_T *bi, visualinfo_T *info)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001506{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001507 unserialize_pos(bi, &info->vi_start);
1508 unserialize_pos(bi, &info->vi_end);
1509 info->vi_mode = undo_read_4c(bi);
1510 info->vi_curswant = undo_read_4c(bi);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001511}
1512
1513/*
1514 * Write the undo tree in an undo file.
1515 * When "name" is not NULL, use it as the name of the undo file.
1516 * Otherwise use buf->b_ffname to generate the undo file name.
1517 * "buf" must never be null, buf->b_ffname is used to obtain the original file
1518 * permissions.
1519 * "forceit" is TRUE for ":wundo!", FALSE otherwise.
1520 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text.
1521 */
1522 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001523u_write_undo(
1524 char_u *name,
1525 int forceit,
1526 buf_T *buf,
1527 char_u *hash)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001528{
1529 u_header_T *uhp;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001530 char_u *file_name;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001531 int mark;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001532#ifdef U_DEBUG
1533 int headers_written = 0;
1534#endif
1535 int fd;
1536 FILE *fp = NULL;
1537 int perm;
1538 int write_ok = FALSE;
1539#ifdef UNIX
1540 int st_old_valid = FALSE;
Bram Moolenaar8767f522016-07-01 17:17:39 +02001541 stat_T st_old;
1542 stat_T st_new;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001543#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001544 bufinfo_T bi;
1545
Bram Moolenaar5a669b92014-08-12 20:14:33 +02001546 vim_memset(&bi, 0, sizeof(bi));
Bram Moolenaar9db58062010-05-29 20:33:07 +02001547
1548 if (name == NULL)
1549 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001550 file_name = u_get_undo_file_name(buf->b_ffname, FALSE);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001551 if (file_name == NULL)
1552 {
1553 if (p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001554 {
1555 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001556 smsg(
Bram Moolenaar504a8212010-05-30 17:17:42 +02001557 _("Cannot write undo file in any directory in 'undodir'"));
1558 verbose_leave();
1559 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001560 return;
1561 }
1562 }
1563 else
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001564 file_name = name;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001565
1566 /*
1567 * Decide about the permission to use for the undo file. If the buffer
1568 * has a name use the permission of the original file. Otherwise only
1569 * allow the user to access the undo file.
1570 */
1571 perm = 0600;
1572 if (buf->b_ffname != NULL)
1573 {
1574#ifdef UNIX
1575 if (mch_stat((char *)buf->b_ffname, &st_old) >= 0)
1576 {
1577 perm = st_old.st_mode;
1578 st_old_valid = TRUE;
1579 }
1580#else
1581 perm = mch_getperm(buf->b_ffname);
1582 if (perm < 0)
1583 perm = 0600;
1584#endif
1585 }
1586
Bram Moolenaar3cbac302015-04-21 16:12:06 +02001587 /* strip any s-bit and executable bit */
1588 perm = perm & 0666;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001589
1590 /* If the undo file already exists, verify that it actually is an undo
1591 * file, and delete it. */
1592 if (mch_getperm(file_name) >= 0)
1593 {
1594 if (name == NULL || !forceit)
1595 {
1596 /* Check we can read it and it's an undo file. */
1597 fd = mch_open((char *)file_name, O_RDONLY|O_EXTRA, 0);
1598 if (fd < 0)
1599 {
1600 if (name != NULL || p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001601 {
1602 if (name == NULL)
1603 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001604 smsg(
Bram Moolenaar504a8212010-05-30 17:17:42 +02001605 _("Will not overwrite with undo file, cannot read: %s"),
Bram Moolenaar9db58062010-05-29 20:33:07 +02001606 file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001607 if (name == NULL)
1608 verbose_leave();
1609 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001610 goto theend;
1611 }
1612 else
1613 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02001614 char_u mbuf[UF_START_MAGIC_LEN];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001615 int len;
1616
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01001617 len = read_eintr(fd, mbuf, UF_START_MAGIC_LEN);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001618 close(fd);
1619 if (len < UF_START_MAGIC_LEN
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02001620 || memcmp(mbuf, UF_START_MAGIC, UF_START_MAGIC_LEN) != 0)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001621 {
1622 if (name != NULL || p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001623 {
1624 if (name == NULL)
1625 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001626 smsg(
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001627 _("Will not overwrite, this is not an undo file: %s"),
Bram Moolenaar9db58062010-05-29 20:33:07 +02001628 file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001629 if (name == NULL)
1630 verbose_leave();
1631 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001632 goto theend;
1633 }
1634 }
1635 }
1636 mch_remove(file_name);
1637 }
1638
Bram Moolenaar504a8212010-05-30 17:17:42 +02001639 /* If there is no undo information at all, quit here after deleting any
1640 * existing undo file. */
Bram Moolenaarccae4672019-01-04 15:09:57 +01001641 if (buf->b_u_numhead == 0 && buf->b_u_line_ptr.ul_line == NULL)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001642 {
1643 if (p_verbose > 0)
Bram Moolenaar97ea5112010-06-12 06:46:44 +02001644 verb_msg((char_u *)_("Skipping undo file write, nothing to undo"));
Bram Moolenaar504a8212010-05-30 17:17:42 +02001645 goto theend;
1646 }
1647
Bram Moolenaar9db58062010-05-29 20:33:07 +02001648 fd = mch_open((char *)file_name,
1649 O_CREAT|O_EXTRA|O_WRONLY|O_EXCL|O_NOFOLLOW, perm);
1650 if (fd < 0)
1651 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001652 semsg(_(e_not_open), file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001653 goto theend;
1654 }
1655 (void)mch_setperm(file_name, perm);
1656 if (p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001657 {
1658 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001659 smsg(_("Writing undo file: %s"), file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001660 verbose_leave();
1661 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001662
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001663#ifdef U_DEBUG
Bram Moolenaar504a8212010-05-30 17:17:42 +02001664 /* Check there is no problem in undo info before writing. */
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001665 u_check(FALSE);
1666#endif
1667
Bram Moolenaar9db58062010-05-29 20:33:07 +02001668#ifdef UNIX
1669 /*
1670 * Try to set the group of the undo file same as the original file. If
1671 * this fails, set the protection bits for the group same as the
1672 * protection bits for others.
1673 */
Bram Moolenaarce69e822010-07-21 20:31:07 +02001674 if (st_old_valid
1675 && mch_stat((char *)file_name, &st_new) >= 0
1676 && st_new.st_gid != st_old.st_gid
Bram Moolenaar9db58062010-05-29 20:33:07 +02001677# ifdef HAVE_FCHOWN /* sequent-ptx lacks fchown() */
Bram Moolenaarce69e822010-07-21 20:31:07 +02001678 && fchown(fd, (uid_t)-1, st_old.st_gid) != 0
Bram Moolenaar9db58062010-05-29 20:33:07 +02001679# endif
1680 )
1681 mch_setperm(file_name, (perm & 0707) | ((perm & 07) << 3));
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02001682# if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001683 if (buf->b_ffname != NULL)
1684 mch_copy_sec(buf->b_ffname, file_name);
1685# endif
1686#endif
1687
1688 fp = fdopen(fd, "w");
1689 if (fp == NULL)
1690 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001691 semsg(_(e_not_open), file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001692 close(fd);
1693 mch_remove(file_name);
1694 goto theend;
1695 }
1696
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001697 /* Undo must be synced. */
1698 u_sync(TRUE);
1699
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001700 /*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001701 * Write the header. Initializes encryption, if enabled.
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001702 */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001703 bi.bi_buf = buf;
1704 bi.bi_fp = fp;
1705 if (serialize_header(&bi, hash) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001706 goto write_error;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001707
1708 /*
1709 * Iteratively serialize UHPs and their UEPs from the top down.
1710 */
1711 mark = ++lastmark;
1712 uhp = buf->b_u_oldhead;
1713 while (uhp != NULL)
1714 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001715 /* Serialize current UHP if we haven't seen it */
1716 if (uhp->uh_walk != mark)
1717 {
1718 uhp->uh_walk = mark;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001719#ifdef U_DEBUG
1720 ++headers_written;
1721#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001722 if (serialize_uhp(&bi, uhp) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001723 goto write_error;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001724 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001725
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001726 /* Now walk through the tree - algorithm from undo_time(). */
1727 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != mark)
1728 uhp = uhp->uh_prev.ptr;
1729 else if (uhp->uh_alt_next.ptr != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001730 && uhp->uh_alt_next.ptr->uh_walk != mark)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001731 uhp = uhp->uh_alt_next.ptr;
1732 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001733 && uhp->uh_next.ptr->uh_walk != mark)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001734 uhp = uhp->uh_next.ptr;
1735 else if (uhp->uh_alt_prev.ptr != NULL)
1736 uhp = uhp->uh_alt_prev.ptr;
1737 else
1738 uhp = uhp->uh_next.ptr;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001739 }
1740
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001741 if (undo_write_bytes(&bi, (long_u)UF_HEADER_END_MAGIC, 2) == OK)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001742 write_ok = TRUE;
1743#ifdef U_DEBUG
1744 if (headers_written != buf->b_u_numhead)
Bram Moolenaar570064c2013-06-10 20:25:10 +02001745 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001746 semsg("Written %ld headers, ...", headers_written);
1747 semsg("... but numhead is %ld", buf->b_u_numhead);
Bram Moolenaar570064c2013-06-10 20:25:10 +02001748 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001749#endif
1750
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001751#ifdef FEAT_CRYPT
1752 if (bi.bi_state != NULL && undo_flush(&bi) == FAIL)
1753 write_ok = FALSE;
1754#endif
1755
Bram Moolenaar9db58062010-05-29 20:33:07 +02001756write_error:
1757 fclose(fp);
1758 if (!write_ok)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001759 semsg(_("E829: write error in undo file: %s"), file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001760
Bram Moolenaard0573012017-10-28 21:11:06 +02001761#if defined(WIN3264)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001762 /* Copy file attributes; for systems where this can only be done after
1763 * closing the file. */
Bram Moolenaar9db58062010-05-29 20:33:07 +02001764 if (buf->b_ffname != NULL)
1765 (void)mch_copy_file_attribute(buf->b_ffname, file_name);
1766#endif
1767#ifdef HAVE_ACL
1768 if (buf->b_ffname != NULL)
1769 {
1770 vim_acl_T acl;
1771
1772 /* For systems that support ACL: get the ACL from the original file. */
1773 acl = mch_get_acl(buf->b_ffname);
1774 mch_set_acl(file_name, acl);
Bram Moolenaard2aed442012-06-01 13:46:12 +02001775 mch_free_acl(acl);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001776 }
1777#endif
1778
1779theend:
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001780#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001781 if (bi.bi_state != NULL)
1782 crypt_free_state(bi.bi_state);
1783 vim_free(bi.bi_buffer);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001784#endif
Bram Moolenaar9db58062010-05-29 20:33:07 +02001785 if (file_name != name)
1786 vim_free(file_name);
1787}
1788
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001789/*
1790 * Load the undo tree from an undo file.
1791 * If "name" is not NULL use it as the undo file name. This also means being
1792 * a bit more verbose.
1793 * Otherwise use curbuf->b_ffname to generate the undo file name.
1794 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text.
1795 */
1796 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001797u_read_undo(char_u *name, char_u *hash, char_u *orig_name)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001798{
1799 char_u *file_name;
1800 FILE *fp;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001801 long version, str_len;
Bram Moolenaarccae4672019-01-04 15:09:57 +01001802 undoline_T line_ptr;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001803 linenr_T line_lnum;
1804 colnr_T line_colnr;
1805 linenr_T line_count;
Bram Moolenaar3eb16372017-02-26 18:11:36 +01001806 long num_head = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001807 long old_header_seq, new_header_seq, cur_header_seq;
1808 long seq_last, seq_cur;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001809 long last_save_nr = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001810 short old_idx = -1, new_idx = -1, cur_idx = -1;
1811 long num_read_uhps = 0;
1812 time_t seq_time;
1813 int i, j;
1814 int c;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001815 u_header_T *uhp;
1816 u_header_T **uhp_table = NULL;
1817 char_u read_hash[UNDO_HASH_SIZE];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001818 char_u magic_buf[UF_START_MAGIC_LEN];
1819#ifdef U_DEBUG
1820 int *uhp_table_used;
1821#endif
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001822#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001823 stat_T st_orig;
1824 stat_T st_undo;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001825#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001826 bufinfo_T bi;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001827
Bram Moolenaar5a669b92014-08-12 20:14:33 +02001828 vim_memset(&bi, 0, sizeof(bi));
Bram Moolenaarccae4672019-01-04 15:09:57 +01001829 line_ptr.ul_len = 0;
1830 line_ptr.ul_line = NULL;
1831
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001832 if (name == NULL)
1833 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001834 file_name = u_get_undo_file_name(curbuf->b_ffname, TRUE);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001835 if (file_name == NULL)
1836 return;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001837
1838#ifdef UNIX
1839 /* For safety we only read an undo file if the owner is equal to the
Bram Moolenaar3b262392013-09-08 15:40:49 +02001840 * owner of the text file or equal to the current user. */
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001841 if (mch_stat((char *)orig_name, &st_orig) >= 0
1842 && mch_stat((char *)file_name, &st_undo) >= 0
Bram Moolenaar3b262392013-09-08 15:40:49 +02001843 && st_orig.st_uid != st_undo.st_uid
1844 && st_undo.st_uid != getuid())
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001845 {
1846 if (p_verbose > 0)
1847 {
1848 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001849 smsg(_("Not reading undo file, owner differs: %s"),
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001850 file_name);
1851 verbose_leave();
1852 }
1853 return;
1854 }
1855#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001856 }
1857 else
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001858 file_name = name;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001859
1860 if (p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001861 {
1862 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001863 smsg(_("Reading undo file: %s"), file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001864 verbose_leave();
1865 }
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001866
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001867 fp = mch_fopen((char *)file_name, "r");
1868 if (fp == NULL)
1869 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001870 if (name != NULL || p_verbose > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001871 semsg(_("E822: Cannot open undo file for reading: %s"), file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001872 goto error;
1873 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001874 bi.bi_buf = curbuf;
1875 bi.bi_fp = fp;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001876
Bram Moolenaar9db58062010-05-29 20:33:07 +02001877 /*
1878 * Read the undo file header.
1879 */
1880 if (fread(magic_buf, UF_START_MAGIC_LEN, 1, fp) != 1
1881 || memcmp(magic_buf, UF_START_MAGIC, UF_START_MAGIC_LEN) != 0)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001882 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001883 semsg(_("E823: Not an undo file: %s"), file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001884 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001885 }
1886 version = get2c(fp);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001887 if (version == UF_VERSION_CRYPT)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001888 {
1889#ifdef FEAT_CRYPT
Bram Moolenaar56be9502010-06-06 14:20:26 +02001890 if (*curbuf->b_p_key == NUL)
1891 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001892 semsg(_("E832: Non-encrypted file has encrypted undo file: %s"),
Bram Moolenaar56be9502010-06-06 14:20:26 +02001893 file_name);
1894 goto error;
1895 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001896 bi.bi_state = crypt_create_from_file(fp, curbuf->b_p_key);
1897 if (bi.bi_state == NULL)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001898 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001899 semsg(_("E826: Undo file decryption failed: %s"), file_name);
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001900 goto error;
1901 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001902 if (crypt_whole_undofile(bi.bi_state->method_nr))
1903 {
1904 bi.bi_buffer = alloc(CRYPT_BUF_SIZE);
1905 if (bi.bi_buffer == NULL)
1906 {
1907 crypt_free_state(bi.bi_state);
1908 bi.bi_state = NULL;
1909 goto error;
1910 }
1911 bi.bi_avail = 0;
1912 bi.bi_used = 0;
1913 }
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001914#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001915 semsg(_("E827: Undo file is encrypted: %s"), file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001916 goto error;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001917#endif
1918 }
Bram Moolenaar69154f22010-07-18 21:42:34 +02001919 else if (version != UF_VERSION)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001920 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001921 semsg(_("E824: Incompatible undo file: %s"), file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001922 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001923 }
1924
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001925 if (undo_read(&bi, read_hash, (size_t)UNDO_HASH_SIZE) == FAIL)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001926 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001927 corruption_error("hash", file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001928 goto error;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001929 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001930 line_count = (linenr_T)undo_read_4c(&bi);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001931 if (memcmp(hash, read_hash, UNDO_HASH_SIZE) != 0
1932 || line_count != curbuf->b_ml.ml_line_count)
1933 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001934 if (p_verbose > 0 || name != NULL)
1935 {
Bram Moolenaar504a8212010-05-30 17:17:42 +02001936 if (name == NULL)
1937 verbose_enter();
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001938 give_warning((char_u *)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001939 _("File contents changed, cannot use undo info"), TRUE);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001940 if (name == NULL)
1941 verbose_leave();
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001942 }
1943 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001944 }
1945
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001946 /* Read undo data for "U" command. */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001947 str_len = undo_read_4c(&bi);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001948 if (str_len < 0)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001949 goto error;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001950 if (str_len > 0)
Bram Moolenaarccae4672019-01-04 15:09:57 +01001951 {
1952 line_ptr.ul_line = read_string_decrypt(&bi, str_len);
1953 line_ptr.ul_len = str_len + 1;
1954 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001955 line_lnum = (linenr_T)undo_read_4c(&bi);
1956 line_colnr = (colnr_T)undo_read_4c(&bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001957 if (line_lnum < 0 || line_colnr < 0)
1958 {
1959 corruption_error("line lnum/col", file_name);
1960 goto error;
1961 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001962
1963 /* Begin general undo data */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001964 old_header_seq = undo_read_4c(&bi);
1965 new_header_seq = undo_read_4c(&bi);
1966 cur_header_seq = undo_read_4c(&bi);
1967 num_head = undo_read_4c(&bi);
1968 seq_last = undo_read_4c(&bi);
1969 seq_cur = undo_read_4c(&bi);
1970 seq_time = undo_read_time(&bi);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001971
Bram Moolenaar69154f22010-07-18 21:42:34 +02001972 /* Optional header fields. */
1973 for (;;)
1974 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001975 int len = undo_read_byte(&bi);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001976 int what;
Bram Moolenaara800b422010-06-27 01:15:55 +02001977
Bram Moolenaar69154f22010-07-18 21:42:34 +02001978 if (len == 0 || len == EOF)
1979 break;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001980 what = undo_read_byte(&bi);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001981 switch (what)
1982 {
1983 case UF_LAST_SAVE_NR:
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001984 last_save_nr = undo_read_4c(&bi);
Bram Moolenaara800b422010-06-27 01:15:55 +02001985 break;
Bram Moolenaar69154f22010-07-18 21:42:34 +02001986 default:
1987 /* field not supported, skip */
1988 while (--len >= 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001989 (void)undo_read_byte(&bi);
Bram Moolenaara800b422010-06-27 01:15:55 +02001990 }
Bram Moolenaar69154f22010-07-18 21:42:34 +02001991 }
Bram Moolenaara800b422010-06-27 01:15:55 +02001992
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001993 /* uhp_table will store the freshly created undo headers we allocate
1994 * until we insert them into curbuf. The table remains sorted by the
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001995 * sequence numbers of the headers.
1996 * When there are no headers uhp_table is NULL. */
1997 if (num_head > 0)
1998 {
Bram Moolenaar3eb16372017-02-26 18:11:36 +01001999 if (num_head < LONG_MAX / (long)sizeof(u_header_T *))
2000 uhp_table = (u_header_T **)U_ALLOC_LINE(
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002001 num_head * sizeof(u_header_T *));
2002 if (uhp_table == NULL)
2003 goto error;
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002004 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002005
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002006 while ((c = undo_read_2c(&bi)) == UF_HEADER_MAGIC)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002007 {
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02002008 if (num_read_uhps >= num_head)
2009 {
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002010 corruption_error("num_head too small", file_name);
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02002011 goto error;
2012 }
2013
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002014 uhp = unserialize_uhp(&bi, file_name);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002015 if (uhp == NULL)
2016 goto error;
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02002017 uhp_table[num_read_uhps++] = uhp;
2018 }
2019
2020 if (num_read_uhps != num_head)
2021 {
2022 corruption_error("num_head", file_name);
2023 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002024 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02002025 if (c != UF_HEADER_END_MAGIC)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002026 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02002027 corruption_error("end marker", file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002028 goto error;
2029 }
2030
Bram Moolenaar9db58062010-05-29 20:33:07 +02002031#ifdef U_DEBUG
2032 uhp_table_used = (int *)alloc_clear(
2033 (unsigned)(sizeof(int) * num_head + 1));
2034# define SET_FLAG(j) ++uhp_table_used[j]
2035#else
2036# define SET_FLAG(j)
2037#endif
2038
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002039 /* We have put all of the headers into a table. Now we iterate through the
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002040 * table and swizzle each sequence number we have stored in uh_*_seq into
2041 * a pointer corresponding to the header with that sequence number. */
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002042 for (i = 0; i < num_head; i++)
2043 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002044 uhp = uhp_table[i];
2045 if (uhp == NULL)
2046 continue;
2047 for (j = 0; j < num_head; j++)
2048 if (uhp_table[j] != NULL && i != j
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002049 && uhp_table[i]->uh_seq == uhp_table[j]->uh_seq)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002050 {
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02002051 corruption_error("duplicate uh_seq", file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002052 goto error;
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02002053 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002054 for (j = 0; j < num_head; j++)
2055 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002056 && uhp_table[j]->uh_seq == uhp->uh_next.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002057 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002058 uhp->uh_next.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02002059 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002060 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002061 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002062 for (j = 0; j < num_head; j++)
2063 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002064 && uhp_table[j]->uh_seq == uhp->uh_prev.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002065 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002066 uhp->uh_prev.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02002067 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002068 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002069 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002070 for (j = 0; j < num_head; j++)
2071 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002072 && uhp_table[j]->uh_seq == uhp->uh_alt_next.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002073 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002074 uhp->uh_alt_next.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02002075 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002076 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002077 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002078 for (j = 0; j < num_head; j++)
2079 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002080 && uhp_table[j]->uh_seq == uhp->uh_alt_prev.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002081 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002082 uhp->uh_alt_prev.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02002083 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002084 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002085 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002086 if (old_header_seq > 0 && old_idx < 0 && uhp->uh_seq == old_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002087 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002088 old_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002089 SET_FLAG(i);
2090 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002091 if (new_header_seq > 0 && new_idx < 0 && uhp->uh_seq == new_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002092 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002093 new_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002094 SET_FLAG(i);
2095 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002096 if (cur_header_seq > 0 && cur_idx < 0 && uhp->uh_seq == cur_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002097 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002098 cur_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002099 SET_FLAG(i);
2100 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002101 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02002102
2103 /* Now that we have read the undo info successfully, free the current undo
2104 * info and use the info from the file. */
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002105 u_blockfree(curbuf);
Bram Moolenaar9db58062010-05-29 20:33:07 +02002106 curbuf->b_u_oldhead = old_idx < 0 ? NULL : uhp_table[old_idx];
2107 curbuf->b_u_newhead = new_idx < 0 ? NULL : uhp_table[new_idx];
2108 curbuf->b_u_curhead = cur_idx < 0 ? NULL : uhp_table[cur_idx];
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002109 curbuf->b_u_line_ptr = line_ptr;
2110 curbuf->b_u_line_lnum = line_lnum;
2111 curbuf->b_u_line_colnr = line_colnr;
2112 curbuf->b_u_numhead = num_head;
2113 curbuf->b_u_seq_last = seq_last;
2114 curbuf->b_u_seq_cur = seq_cur;
Bram Moolenaara800b422010-06-27 01:15:55 +02002115 curbuf->b_u_time_cur = seq_time;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002116 curbuf->b_u_save_nr_last = last_save_nr;
Bram Moolenaardba01a02010-11-03 19:32:42 +01002117 curbuf->b_u_save_nr_cur = last_save_nr;
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02002118
2119 curbuf->b_u_synced = TRUE;
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002120 vim_free(uhp_table);
Bram Moolenaar9db58062010-05-29 20:33:07 +02002121
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002122#ifdef U_DEBUG
Bram Moolenaar9db58062010-05-29 20:33:07 +02002123 for (i = 0; i < num_head; ++i)
2124 if (uhp_table_used[i] == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002125 semsg("uhp_table entry %ld not used, leaking memory", i);
Bram Moolenaar9db58062010-05-29 20:33:07 +02002126 vim_free(uhp_table_used);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002127 u_check(TRUE);
2128#endif
Bram Moolenaar9db58062010-05-29 20:33:07 +02002129
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002130 if (name != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002131 smsg(_("Finished reading undo file %s"), file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002132 goto theend;
2133
2134error:
Bram Moolenaarccae4672019-01-04 15:09:57 +01002135 vim_free(line_ptr.ul_line);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002136 if (uhp_table != NULL)
2137 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002138 for (i = 0; i < num_read_uhps; i++)
2139 if (uhp_table[i] != NULL)
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02002140 u_free_uhp(uhp_table[i]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002141 vim_free(uhp_table);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002142 }
2143
2144theend:
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002145#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002146 if (bi.bi_state != NULL)
2147 crypt_free_state(bi.bi_state);
2148 vim_free(bi.bi_buffer);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002149#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002150 if (fp != NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002151 fclose(fp);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002152 if (file_name != name)
2153 vim_free(file_name);
2154 return;
2155}
2156
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002157#endif /* FEAT_PERSISTENT_UNDO */
2158
2159
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160/*
2161 * If 'cpoptions' contains 'u': Undo the previous undo or redo (vi compatible).
2162 * If 'cpoptions' does not contain 'u': Always undo.
2163 */
2164 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002165u_undo(int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166{
2167 /*
2168 * If we get an undo command while executing a macro, we behave like the
2169 * original vi. If this happens twice in one macro the result will not
2170 * be compatible.
2171 */
2172 if (curbuf->b_u_synced == FALSE)
2173 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002174 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 count = 1;
2176 }
2177
2178 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
2179 undo_undoes = TRUE;
2180 else
2181 undo_undoes = !undo_undoes;
2182 u_doit(count);
2183}
2184
2185/*
2186 * If 'cpoptions' contains 'u': Repeat the previous undo or redo.
2187 * If 'cpoptions' does not contain 'u': Always redo.
2188 */
2189 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002190u_redo(int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191{
2192 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
2193 undo_undoes = FALSE;
2194 u_doit(count);
2195}
2196
2197/*
2198 * Undo or redo, depending on 'undo_undoes', 'count' times.
2199 */
2200 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002201u_doit(int startcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202{
Bram Moolenaarca003e12006-03-17 23:19:38 +00002203 int count = startcount;
2204
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002205 if (!undo_allowed())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207
2208 u_newcount = 0;
2209 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002210 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2211 u_oldcount = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 while (count--)
2213 {
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002214 /* Do the change warning now, so that it triggers FileChangedRO when
2215 * needed. This may cause the file to be reloaded, that must happen
2216 * before we do anything, because it may change curbuf->b_u_curhead
2217 * and more. */
2218 change_warning(0);
2219
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 if (undo_undoes)
2221 {
2222 if (curbuf->b_u_curhead == NULL) /* first undo */
2223 curbuf->b_u_curhead = curbuf->b_u_newhead;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002224 else if (get_undolevel() > 0) /* multi level undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 /* get next undo */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002226 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_next.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 /* nothing to undo */
2228 if (curbuf->b_u_numhead == 0 || curbuf->b_u_curhead == NULL)
2229 {
2230 /* stick curbuf->b_u_curhead at end */
2231 curbuf->b_u_curhead = curbuf->b_u_oldhead;
2232 beep_flush();
Bram Moolenaarca003e12006-03-17 23:19:38 +00002233 if (count == startcount - 1)
2234 {
2235 MSG(_("Already at oldest change"));
2236 return;
2237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 break;
2239 }
2240
Bram Moolenaarca003e12006-03-17 23:19:38 +00002241 u_undoredo(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 }
2243 else
2244 {
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002245 if (curbuf->b_u_curhead == NULL || get_undolevel() <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 {
2247 beep_flush(); /* nothing to redo */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002248 if (count == startcount - 1)
2249 {
2250 MSG(_("Already at newest change"));
2251 return;
2252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 break;
2254 }
2255
Bram Moolenaarca003e12006-03-17 23:19:38 +00002256 u_undoredo(FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002257
2258 /* Advance for next redo. Set "newhead" when at the end of the
2259 * redoable changes. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002260 if (curbuf->b_u_curhead->uh_prev.ptr == NULL)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002261 curbuf->b_u_newhead = curbuf->b_u_curhead;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002262 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_prev.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 }
2264 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00002265 u_undo_end(undo_undoes, FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002266}
2267
Bram Moolenaar1e607892006-03-13 22:15:53 +00002268/*
2269 * Undo or redo over the timeline.
2270 * When "step" is negative go back in time, otherwise goes forward in time.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002271 * When "sec" is FALSE make "step" steps, when "sec" is TRUE use "step" as
2272 * seconds.
Bram Moolenaar730cde92010-06-27 05:18:54 +02002273 * When "file" is TRUE use "step" as a number of file writes.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002274 * When "absolute" is TRUE use "step" as the sequence number to jump to.
2275 * "sec" must be FALSE then.
Bram Moolenaar1e607892006-03-13 22:15:53 +00002276 */
2277 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002278undo_time(
2279 long step,
2280 int sec,
2281 int file,
2282 int absolute)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002283{
2284 long target;
2285 long closest;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002286 long closest_start;
2287 long closest_seq = 0;
2288 long val;
Bram Moolenaarce46d932018-01-30 22:46:06 +01002289 u_header_T *uhp = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002290 u_header_T *last;
2291 int mark;
2292 int nomark;
2293 int round;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002294 int dosec = sec;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002295 int dofile = file;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002296 int above = FALSE;
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002297 int did_undo = TRUE;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002298
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002299 /* First make sure the current undoable change is synced. */
2300 if (curbuf->b_u_synced == FALSE)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002301 u_sync(TRUE);
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002302
Bram Moolenaar1e607892006-03-13 22:15:53 +00002303 u_newcount = 0;
2304 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002305 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002306 u_oldcount = -1;
2307
Bram Moolenaarca003e12006-03-17 23:19:38 +00002308 /* "target" is the node below which we want to be.
2309 * Init "closest" to a value we can't reach. */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002310 if (absolute)
2311 {
Bram Moolenaarce46d932018-01-30 22:46:06 +01002312 target = step;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002313 closest = -1;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002314 }
Bram Moolenaarca003e12006-03-17 23:19:38 +00002315 else
Bram Moolenaar1e607892006-03-13 22:15:53 +00002316 {
Bram Moolenaar730cde92010-06-27 05:18:54 +02002317 if (dosec)
Bram Moolenaarcbd4de42017-01-07 16:14:57 +01002318 target = (long)(curbuf->b_u_time_cur) + step;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002319 else if (dofile)
2320 {
2321 if (step < 0)
2322 {
2323 /* Going back to a previous write. If there were changes after
2324 * the last write, count that as moving one file-write, so
2325 * that ":earlier 1f" undoes all changes since the last save. */
2326 uhp = curbuf->b_u_curhead;
2327 if (uhp != NULL)
2328 uhp = uhp->uh_next.ptr;
2329 else
2330 uhp = curbuf->b_u_newhead;
2331 if (uhp != NULL && uhp->uh_save_nr != 0)
2332 /* "uh_save_nr" was set in the last block, that means
2333 * there were no changes since the last write */
2334 target = curbuf->b_u_save_nr_cur + step;
2335 else
2336 /* count the changes since the last write as one step */
2337 target = curbuf->b_u_save_nr_cur + step + 1;
2338 if (target <= 0)
2339 /* Go to before first write: before the oldest change. Use
2340 * the sequence number for that. */
2341 dofile = FALSE;
2342 }
2343 else
2344 {
2345 /* Moving forward to a newer write. */
2346 target = curbuf->b_u_save_nr_cur + step;
2347 if (target > curbuf->b_u_save_nr_last)
2348 {
2349 /* Go to after last write: after the latest change. Use
2350 * the sequence number for that. */
2351 target = curbuf->b_u_seq_last + 1;
2352 dofile = FALSE;
2353 }
2354 }
2355 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002356 else
2357 target = curbuf->b_u_seq_cur + step;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002358 if (step < 0)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002359 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002360 if (target < 0)
2361 target = 0;
2362 closest = -1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002363 }
2364 else
2365 {
Bram Moolenaar730cde92010-06-27 05:18:54 +02002366 if (dosec)
Bram Moolenaarcbd4de42017-01-07 16:14:57 +01002367 closest = (long)(vim_time() + 1);
Bram Moolenaar730cde92010-06-27 05:18:54 +02002368 else if (dofile)
2369 closest = curbuf->b_u_save_nr_last + 2;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002370 else
2371 closest = curbuf->b_u_seq_last + 2;
2372 if (target >= closest)
2373 target = closest - 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002374 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002375 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002376 closest_start = closest;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002377 closest_seq = curbuf->b_u_seq_cur;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002378
Bram Moolenaarce46d932018-01-30 22:46:06 +01002379 /* When "target" is 0; Back to origin. */
2380 if (target == 0)
Bram Moolenaar059fd012018-01-31 14:25:53 +01002381 {
2382 mark = lastmark; /* avoid that GCC complains */
2383 goto target_zero;
2384 }
Bram Moolenaarce46d932018-01-30 22:46:06 +01002385
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002386 /*
2387 * May do this twice:
Bram Moolenaar1e607892006-03-13 22:15:53 +00002388 * 1. Search for "target", update "closest" to the best match found.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002389 * 2. If "target" not found search for "closest".
2390 *
2391 * When using the closest time we use the sequence number in the second
2392 * round, because there may be several entries with the same time.
2393 */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002394 for (round = 1; round <= 2; ++round)
2395 {
2396 /* Find the path from the current state to where we want to go. The
2397 * desired state can be anywhere in the undo tree, need to go all over
2398 * it. We put "nomark" in uh_walk where we have been without success,
2399 * "mark" where it could possibly be. */
2400 mark = ++lastmark;
2401 nomark = ++lastmark;
2402
2403 if (curbuf->b_u_curhead == NULL) /* at leaf of the tree */
2404 uhp = curbuf->b_u_newhead;
2405 else
2406 uhp = curbuf->b_u_curhead;
2407
2408 while (uhp != NULL)
2409 {
2410 uhp->uh_walk = mark;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002411 if (dosec)
Bram Moolenaarcbd4de42017-01-07 16:14:57 +01002412 val = (long)(uhp->uh_time);
Bram Moolenaar730cde92010-06-27 05:18:54 +02002413 else if (dofile)
2414 val = uhp->uh_save_nr;
2415 else
2416 val = uhp->uh_seq;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002417
Bram Moolenaar730cde92010-06-27 05:18:54 +02002418 if (round == 1 && !(dofile && val == 0))
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002419 {
2420 /* Remember the header that is closest to the target.
2421 * It must be at least in the right direction (checked with
Bram Moolenaarca003e12006-03-17 23:19:38 +00002422 * "b_u_seq_cur"). When the timestamp is equal find the
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002423 * highest/lowest sequence number. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002424 if ((step < 0 ? uhp->uh_seq <= curbuf->b_u_seq_cur
2425 : uhp->uh_seq > curbuf->b_u_seq_cur)
2426 && ((dosec && val == closest)
2427 ? (step < 0
2428 ? uhp->uh_seq < closest_seq
2429 : uhp->uh_seq > closest_seq)
2430 : closest == closest_start
2431 || (val > target
2432 ? (closest > target
2433 ? val - target <= closest - target
2434 : val - target <= target - closest)
2435 : (closest > target
2436 ? target - val <= closest - target
2437 : target - val <= target - closest))))
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002438 {
2439 closest = val;
2440 closest_seq = uhp->uh_seq;
2441 }
2442 }
2443
2444 /* Quit searching when we found a match. But when searching for a
2445 * time we need to continue looking for the best uh_seq. */
2446 if (target == val && !dosec)
Bram Moolenaar730cde92010-06-27 05:18:54 +02002447 {
2448 target = uhp->uh_seq;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002449 break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002450 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002451
2452 /* go down in the tree if we haven't been there */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002453 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != nomark
2454 && uhp->uh_prev.ptr->uh_walk != mark)
2455 uhp = uhp->uh_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002456
2457 /* go to alternate branch if we haven't been there */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002458 else if (uhp->uh_alt_next.ptr != NULL
2459 && uhp->uh_alt_next.ptr->uh_walk != nomark
2460 && uhp->uh_alt_next.ptr->uh_walk != mark)
2461 uhp = uhp->uh_alt_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002462
2463 /* go up in the tree if we haven't been there and we are at the
2464 * start of alternate branches */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002465 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
2466 && uhp->uh_next.ptr->uh_walk != nomark
2467 && uhp->uh_next.ptr->uh_walk != mark)
Bram Moolenaardb552d602006-03-23 22:59:57 +00002468 {
2469 /* If still at the start we don't go through this change. */
2470 if (uhp == curbuf->b_u_curhead)
2471 uhp->uh_walk = nomark;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002472 uhp = uhp->uh_next.ptr;
Bram Moolenaardb552d602006-03-23 22:59:57 +00002473 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002474
2475 else
2476 {
2477 /* need to backtrack; mark this node as useless */
2478 uhp->uh_walk = nomark;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002479 if (uhp->uh_alt_prev.ptr != NULL)
2480 uhp = uhp->uh_alt_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002481 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002482 uhp = uhp->uh_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002483 }
2484 }
2485
2486 if (uhp != NULL) /* found it */
2487 break;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002488
2489 if (absolute)
2490 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002491 semsg(_("E830: Undo number %ld not found"), step);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002492 return;
2493 }
2494
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002495 if (closest == closest_start)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002496 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002497 if (step < 0)
2498 MSG(_("Already at oldest change"));
2499 else
2500 MSG(_("Already at newest change"));
Bram Moolenaar1e607892006-03-13 22:15:53 +00002501 return;
2502 }
2503
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002504 target = closest_seq;
2505 dosec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002506 dofile = FALSE;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002507 if (step < 0)
2508 above = TRUE; /* stop above the header */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002509 }
2510
Bram Moolenaar059fd012018-01-31 14:25:53 +01002511target_zero:
Bram Moolenaar1e607892006-03-13 22:15:53 +00002512 /* If we found it: Follow the path to go to where we want to be. */
Bram Moolenaarce46d932018-01-30 22:46:06 +01002513 if (uhp != NULL || target == 0)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002514 {
2515 /*
2516 * First go up the tree as much as needed.
2517 */
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002518 while (!got_int)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002519 {
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002520 /* Do the change warning now, for the same reason as above. */
2521 change_warning(0);
2522
Bram Moolenaar1e607892006-03-13 22:15:53 +00002523 uhp = curbuf->b_u_curhead;
2524 if (uhp == NULL)
2525 uhp = curbuf->b_u_newhead;
2526 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002527 uhp = uhp->uh_next.ptr;
Bram Moolenaarce46d932018-01-30 22:46:06 +01002528 if (uhp == NULL || (target > 0 && uhp->uh_walk != mark)
Bram Moolenaarca003e12006-03-17 23:19:38 +00002529 || (uhp->uh_seq == target && !above))
Bram Moolenaar1e607892006-03-13 22:15:53 +00002530 break;
2531 curbuf->b_u_curhead = uhp;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002532 u_undoredo(TRUE);
Bram Moolenaarce46d932018-01-30 22:46:06 +01002533 if (target > 0)
2534 uhp->uh_walk = nomark; /* don't go back down here */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002535 }
2536
Bram Moolenaarce46d932018-01-30 22:46:06 +01002537 /* When back to origin, redo is not needed. */
2538 if (target > 0)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002539 {
Bram Moolenaarce46d932018-01-30 22:46:06 +01002540 /*
2541 * And now go down the tree (redo), branching off where needed.
2542 */
2543 while (!got_int)
2544 {
2545 /* Do the change warning now, for the same reason as above. */
2546 change_warning(0);
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002547
Bram Moolenaarce46d932018-01-30 22:46:06 +01002548 uhp = curbuf->b_u_curhead;
2549 if (uhp == NULL)
2550 break;
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002551
Bram Moolenaarce46d932018-01-30 22:46:06 +01002552 /* Go back to the first branch with a mark. */
2553 while (uhp->uh_alt_prev.ptr != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002554 && uhp->uh_alt_prev.ptr->uh_walk == mark)
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002555 uhp = uhp->uh_alt_prev.ptr;
Bram Moolenaarce46d932018-01-30 22:46:06 +01002556
2557 /* Find the last branch with a mark, that's the one. */
2558 last = uhp;
2559 while (last->uh_alt_next.ptr != NULL
2560 && last->uh_alt_next.ptr->uh_walk == mark)
2561 last = last->uh_alt_next.ptr;
2562 if (last != uhp)
2563 {
2564 /* Make the used branch the first entry in the list of
2565 * alternatives to make "u" and CTRL-R take this branch. */
2566 while (uhp->uh_alt_prev.ptr != NULL)
2567 uhp = uhp->uh_alt_prev.ptr;
2568 if (last->uh_alt_next.ptr != NULL)
2569 last->uh_alt_next.ptr->uh_alt_prev.ptr =
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002570 last->uh_alt_prev.ptr;
Bram Moolenaarce46d932018-01-30 22:46:06 +01002571 last->uh_alt_prev.ptr->uh_alt_next.ptr =
2572 last->uh_alt_next.ptr;
2573 last->uh_alt_prev.ptr = NULL;
2574 last->uh_alt_next.ptr = uhp;
2575 uhp->uh_alt_prev.ptr = last;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002576
Bram Moolenaarce46d932018-01-30 22:46:06 +01002577 if (curbuf->b_u_oldhead == uhp)
2578 curbuf->b_u_oldhead = last;
2579 uhp = last;
2580 if (uhp->uh_next.ptr != NULL)
2581 uhp->uh_next.ptr->uh_prev.ptr = uhp;
2582 }
2583 curbuf->b_u_curhead = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002584
Bram Moolenaarce46d932018-01-30 22:46:06 +01002585 if (uhp->uh_walk != mark)
2586 break; /* must have reached the target */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002587
Bram Moolenaarce46d932018-01-30 22:46:06 +01002588 /* Stop when going backwards in time and didn't find the exact
2589 * header we were looking for. */
2590 if (uhp->uh_seq == target && above)
2591 {
2592 curbuf->b_u_seq_cur = target - 1;
2593 break;
2594 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002595
Bram Moolenaarce46d932018-01-30 22:46:06 +01002596 u_undoredo(FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002597
Bram Moolenaarce46d932018-01-30 22:46:06 +01002598 /* Advance "curhead" to below the header we last used. If it
2599 * becomes NULL then we need to set "newhead" to this leaf. */
2600 if (uhp->uh_prev.ptr == NULL)
2601 curbuf->b_u_newhead = uhp;
2602 curbuf->b_u_curhead = uhp->uh_prev.ptr;
2603 did_undo = FALSE;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002604
Bram Moolenaarce46d932018-01-30 22:46:06 +01002605 if (uhp->uh_seq == target) /* found it! */
2606 break;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002607
Bram Moolenaarce46d932018-01-30 22:46:06 +01002608 uhp = uhp->uh_prev.ptr;
2609 if (uhp == NULL || uhp->uh_walk != mark)
2610 {
2611 /* Need to redo more but can't find it... */
2612 internal_error("undo_time()");
2613 break;
2614 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002615 }
2616 }
2617 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00002618 u_undo_end(did_undo, absolute);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619}
2620
2621/*
2622 * u_undoredo: common code for undo and redo
2623 *
2624 * The lines in the file are replaced by the lines in the entry list at
2625 * curbuf->b_u_curhead. The replaced lines in the file are saved in the entry
2626 * list for the next undo/redo.
Bram Moolenaarca003e12006-03-17 23:19:38 +00002627 *
2628 * When "undo" is TRUE we go up in the tree, when FALSE we go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 */
2630 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002631u_undoredo(int undo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632{
Bram Moolenaarccae4672019-01-04 15:09:57 +01002633 undoline_T *newarray = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 linenr_T oldsize;
2635 linenr_T newsize;
2636 linenr_T top, bot;
2637 linenr_T lnum;
2638 linenr_T newlnum = MAXLNUM;
2639 long i;
2640 u_entry_T *uep, *nuep;
2641 u_entry_T *newlist = NULL;
2642 int old_flags;
2643 int new_flags;
2644 pos_T namedm[NMARKS];
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002645 visualinfo_T visualinfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 int empty_buffer; /* buffer became empty */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002647 u_header_T *curhead = curbuf->b_u_curhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002649 /* Don't want autocommands using the undo structures here, they are
2650 * invalid till the end. */
2651 block_autocmds();
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002652
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002653#ifdef U_DEBUG
2654 u_check(FALSE);
2655#endif
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002656 old_flags = curhead->uh_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 new_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
2658 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
2659 setpcmark();
2660
2661 /*
2662 * save marks before undo/redo
2663 */
2664 mch_memmove(namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002665 visualinfo = curbuf->b_visual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 curbuf->b_op_start.lnum = curbuf->b_ml.ml_line_count;
2667 curbuf->b_op_start.col = 0;
2668 curbuf->b_op_end.lnum = 0;
2669 curbuf->b_op_end.col = 0;
2670
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002671 for (uep = curhead->uh_entry; uep != NULL; uep = nuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 {
2673 top = uep->ue_top;
2674 bot = uep->ue_bot;
2675 if (bot == 0)
2676 bot = curbuf->b_ml.ml_line_count + 1;
2677 if (top > curbuf->b_ml.ml_line_count || top >= bot
2678 || bot > curbuf->b_ml.ml_line_count + 1)
2679 {
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002680 unblock_autocmds();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002681 iemsg(_("E438: u_undo: line numbers wrong"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 changed(); /* don't want UNCHANGED now */
2683 return;
2684 }
2685
2686 oldsize = bot - top - 1; /* number of lines before undo */
2687 newsize = uep->ue_size; /* number of lines after undo */
2688
2689 if (top < newlnum)
2690 {
2691 /* If the saved cursor is somewhere in this undo block, move it to
2692 * the remembered position. Makes "gwap" put the cursor back
2693 * where it was. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002694 lnum = curhead->uh_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 if (lnum >= top && lnum <= top + newsize + 1)
2696 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002697 curwin->w_cursor = curhead->uh_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 newlnum = curwin->w_cursor.lnum - 1;
2699 }
2700 else
2701 {
2702 /* Use the first line that actually changed. Avoids that
2703 * undoing auto-formatting puts the cursor in the previous
2704 * line. */
2705 for (i = 0; i < newsize && i < oldsize; ++i)
Bram Moolenaarccae4672019-01-04 15:09:57 +01002706 {
2707 char_u *p = ml_get(top + 1 + i);
2708
2709 if (curbuf->b_ml.ml_line_len != uep->ue_array[i].ul_len
2710 || memcmp(uep->ue_array[i].ul_line, p, curbuf->b_ml.ml_line_len) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 break;
Bram Moolenaarccae4672019-01-04 15:09:57 +01002712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 if (i == newsize && newlnum == MAXLNUM && uep->ue_next == NULL)
2714 {
2715 newlnum = top;
2716 curwin->w_cursor.lnum = newlnum + 1;
2717 }
2718 else if (i < newsize)
2719 {
2720 newlnum = top + i;
2721 curwin->w_cursor.lnum = newlnum + 1;
2722 }
2723 }
2724 }
2725
2726 empty_buffer = FALSE;
2727
2728 /* delete the lines between top and bot and save them in newarray */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002729 if (oldsize > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 {
Bram Moolenaarccae4672019-01-04 15:09:57 +01002731 if ((newarray = (undoline_T *)U_ALLOC_LINE(
2732 sizeof(undoline_T) * oldsize)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 {
Bram Moolenaarccae4672019-01-04 15:09:57 +01002734 do_outofmem_msg((long_u)(sizeof(undoline_T) * oldsize));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 /*
2736 * We have messed up the entry list, repair is impossible.
2737 * we have to free the rest of the list.
2738 */
2739 while (uep != NULL)
2740 {
2741 nuep = uep->ue_next;
2742 u_freeentry(uep, uep->ue_size);
2743 uep = nuep;
2744 }
2745 break;
2746 }
2747 /* delete backwards, it goes faster in most cases */
2748 for (lnum = bot - 1, i = oldsize; --i >= 0; --lnum)
2749 {
2750 /* what can we do when we run out of memory? */
Bram Moolenaarccae4672019-01-04 15:09:57 +01002751 if (u_save_line(&newarray[i], lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 do_outofmem_msg((long_u)0);
2753 /* remember we deleted the last line in the buffer, and a
2754 * dummy empty line will be inserted */
2755 if (curbuf->b_ml.ml_line_count == 1)
2756 empty_buffer = TRUE;
2757 ml_delete(lnum, FALSE);
2758 }
2759 }
Bram Moolenaar8d343302005-07-12 22:46:17 +00002760 else
2761 newarray = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762
2763 /* insert the lines in u_array between top and bot */
2764 if (newsize)
2765 {
2766 for (lnum = top, i = 0; i < newsize; ++i, ++lnum)
2767 {
Bram Moolenaarccae4672019-01-04 15:09:57 +01002768 // If the file is empty, there is an empty line 1 that we
2769 // should get rid of, by replacing it with the new line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 if (empty_buffer && lnum == 0)
Bram Moolenaarccae4672019-01-04 15:09:57 +01002771 ml_replace_len((linenr_T)1, uep->ue_array[i].ul_line, uep->ue_array[i].ul_len, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 else
Bram Moolenaarccae4672019-01-04 15:09:57 +01002773 ml_append(lnum, uep->ue_array[i].ul_line, (colnr_T)uep->ue_array[i].ul_len, FALSE);
2774 vim_free(uep->ue_array[i].ul_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 }
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002776 vim_free((char_u *)uep->ue_array);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 }
2778
2779 /* adjust marks */
2780 if (oldsize != newsize)
2781 {
2782 mark_adjust(top + 1, top + oldsize, (long)MAXLNUM,
2783 (long)newsize - (long)oldsize);
2784 if (curbuf->b_op_start.lnum > top + oldsize)
2785 curbuf->b_op_start.lnum += newsize - oldsize;
2786 if (curbuf->b_op_end.lnum > top + oldsize)
2787 curbuf->b_op_end.lnum += newsize - oldsize;
2788 }
2789
2790 changed_lines(top + 1, 0, bot, newsize - oldsize);
2791
2792 /* set '[ and '] mark */
2793 if (top + 1 < curbuf->b_op_start.lnum)
2794 curbuf->b_op_start.lnum = top + 1;
2795 if (newsize == 0 && top + 1 > curbuf->b_op_end.lnum)
2796 curbuf->b_op_end.lnum = top + 1;
2797 else if (top + newsize > curbuf->b_op_end.lnum)
2798 curbuf->b_op_end.lnum = top + newsize;
2799
2800 u_newcount += newsize;
2801 u_oldcount += oldsize;
2802 uep->ue_size = oldsize;
2803 uep->ue_array = newarray;
2804 uep->ue_bot = top + newsize + 1;
2805
2806 /*
2807 * insert this entry in front of the new entry list
2808 */
2809 nuep = uep->ue_next;
2810 uep->ue_next = newlist;
2811 newlist = uep;
2812 }
2813
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002814 curhead->uh_entry = newlist;
2815 curhead->uh_flags = new_flags;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002816 if ((old_flags & UH_EMPTYBUF) && BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 curbuf->b_ml.ml_flags |= ML_EMPTY;
2818 if (old_flags & UH_CHANGED)
2819 changed();
2820 else
Bram Moolenaar009b2592004-10-24 19:18:58 +00002821#ifdef FEAT_NETBEANS_INTG
2822 /* per netbeans undo rules, keep it as modified */
2823 if (!isNetbeansModified(curbuf))
2824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 unchanged(curbuf, FALSE);
2826
2827 /*
2828 * restore marks from before undo/redo
2829 */
2830 for (i = 0; i < NMARKS; ++i)
Bram Moolenaarf65aad52015-02-17 13:43:40 +01002831 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002832 if (curhead->uh_namedm[i].lnum != 0)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002833 curbuf->b_namedm[i] = curhead->uh_namedm[i];
Bram Moolenaarf65aad52015-02-17 13:43:40 +01002834 if (namedm[i].lnum != 0)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002835 curhead->uh_namedm[i] = namedm[i];
Bram Moolenaarf65aad52015-02-17 13:43:40 +01002836 else
2837 curhead->uh_namedm[i].lnum = 0;
2838 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002839 if (curhead->uh_visual.vi_start.lnum != 0)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002840 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002841 curbuf->b_visual = curhead->uh_visual;
2842 curhead->uh_visual = visualinfo;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844
2845 /*
2846 * If the cursor is only off by one line, put it at the same position as
2847 * before starting the change (for the "o" command).
2848 * Otherwise the cursor should go to the first undone line.
2849 */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002850 if (curhead->uh_cursor.lnum + 1 == curwin->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 && curwin->w_cursor.lnum > 1)
2852 --curwin->w_cursor.lnum;
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002853 if (curwin->w_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 {
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002855 if (curhead->uh_cursor.lnum == curwin->w_cursor.lnum)
2856 {
2857 curwin->w_cursor.col = curhead->uh_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002859 if (virtual_active() && curhead->uh_cursor_vcol >= 0)
2860 coladvance((colnr_T)curhead->uh_cursor_vcol);
2861 else
2862 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863#endif
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002864 }
2865 else
2866 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 else
2869 {
2870 /* We get here with the current cursor line being past the end (eg
2871 * after adding lines at the end of the file, and then undoing it).
2872 * check_cursor() will move the cursor to the last line. Move it to
2873 * the first column here. */
2874 curwin->w_cursor.col = 0;
2875#ifdef FEAT_VIRTUALEDIT
2876 curwin->w_cursor.coladd = 0;
2877#endif
2878 }
2879
2880 /* Make sure the cursor is on an existing line and column. */
2881 check_cursor();
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002882
2883 /* Remember where we are for "g-" and ":earlier 10s". */
2884 curbuf->b_u_seq_cur = curhead->uh_seq;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002885 if (undo)
Bram Moolenaar80eaddd2017-11-11 23:37:08 +01002886 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002887 /* We are below the previous undo. However, to make ":earlier 1s"
2888 * work we compute this as being just above the just undone change. */
Bram Moolenaar80eaddd2017-11-11 23:37:08 +01002889 if (curhead->uh_next.ptr != NULL)
2890 curbuf->b_u_seq_cur = curhead->uh_next.ptr->uh_seq;
2891 else
2892 curbuf->b_u_seq_cur = 0;
2893 }
Bram Moolenaarca003e12006-03-17 23:19:38 +00002894
Bram Moolenaar730cde92010-06-27 05:18:54 +02002895 /* Remember where we are for ":earlier 1f" and ":later 1f". */
2896 if (curhead->uh_save_nr != 0)
2897 {
2898 if (undo)
2899 curbuf->b_u_save_nr_cur = curhead->uh_save_nr - 1;
2900 else
2901 curbuf->b_u_save_nr_cur = curhead->uh_save_nr;
2902 }
2903
Bram Moolenaarca003e12006-03-17 23:19:38 +00002904 /* The timestamp can be the same for multiple changes, just use the one of
2905 * the undone/redone change. */
Bram Moolenaara800b422010-06-27 01:15:55 +02002906 curbuf->b_u_time_cur = curhead->uh_time;
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002907
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002908 unblock_autocmds();
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002909#ifdef U_DEBUG
2910 u_check(FALSE);
2911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912}
2913
2914/*
2915 * If we deleted or added lines, report the number of less/more lines.
2916 * Otherwise, report the number of changes (this may be incorrect
2917 * in some cases, but it's better than nothing).
2918 */
2919 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002920u_undo_end(
2921 int did_undo, /* just did an undo */
2922 int absolute) /* used ":undo N" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923{
Bram Moolenaar89d40322006-08-29 15:30:07 +00002924 char *msgstr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002925 u_header_T *uhp;
2926 char_u msgbuf[80];
Bram Moolenaar1e607892006-03-13 22:15:53 +00002927
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928#ifdef FEAT_FOLDING
2929 if ((fdo_flags & FDO_UNDO) && KeyTyped)
2930 foldOpenCursor();
2931#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +00002932
2933 if (global_busy /* no messages now, wait until global is finished */
2934 || !messaging()) /* 'lazyredraw' set, don't do messages now */
2935 return;
2936
2937 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2938 --u_newcount;
2939
2940 u_oldcount -= u_newcount;
2941 if (u_oldcount == -1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002942 msgstr = N_("more line");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002943 else if (u_oldcount < 0)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002944 msgstr = N_("more lines");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002945 else if (u_oldcount == 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002946 msgstr = N_("line less");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002947 else if (u_oldcount > 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002948 msgstr = N_("fewer lines");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002949 else
2950 {
2951 u_oldcount = u_newcount;
2952 if (u_newcount == 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002953 msgstr = N_("change");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002954 else
Bram Moolenaar89d40322006-08-29 15:30:07 +00002955 msgstr = N_("changes");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002956 }
2957
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002958 if (curbuf->b_u_curhead != NULL)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002959 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00002960 /* For ":undo N" we prefer a "after #N" message. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002961 if (absolute && curbuf->b_u_curhead->uh_next.ptr != NULL)
Bram Moolenaardb552d602006-03-23 22:59:57 +00002962 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002963 uhp = curbuf->b_u_curhead->uh_next.ptr;
Bram Moolenaardb552d602006-03-23 22:59:57 +00002964 did_undo = FALSE;
2965 }
2966 else if (did_undo)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002967 uhp = curbuf->b_u_curhead;
2968 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002969 uhp = curbuf->b_u_curhead->uh_next.ptr;
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002970 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002971 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002972 uhp = curbuf->b_u_newhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002973
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002974 if (uhp == NULL)
2975 *msgbuf = NUL;
2976 else
2977 u_add_time(msgbuf, sizeof(msgbuf), uhp->uh_time);
2978
Bram Moolenaarb2c03502010-07-02 20:20:09 +02002979#ifdef FEAT_CONCEAL
2980 {
2981 win_T *wp;
2982
2983 FOR_ALL_WINDOWS(wp)
2984 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02002985 if (wp->w_buffer == curbuf && wp->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02002986 redraw_win_later(wp, NOT_VALID);
2987 }
2988 }
2989#endif
2990
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002991 smsg_attr_keep(0, _("%ld %s; %s #%ld %s"),
Bram Moolenaarca003e12006-03-17 23:19:38 +00002992 u_oldcount < 0 ? -u_oldcount : u_oldcount,
Bram Moolenaar89d40322006-08-29 15:30:07 +00002993 _(msgstr),
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002994 did_undo ? _("before") : _("after"),
2995 uhp == NULL ? 0L : uhp->uh_seq,
2996 msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997}
2998
2999/*
3000 * u_sync: stop adding to the current entry list
3001 */
3002 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003003u_sync(
3004 int force) /* Also sync when no_u_sync is set. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005{
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003006 /* Skip it when already synced or syncing is disabled. */
3007 if (curbuf->b_u_synced || (!force && no_u_sync > 0))
3008 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003010 if (p_imst == IM_ON_THE_SPOT && im_is_preediting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 return; /* XIM is busy, don't break an undo sequence */
3012#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003013 if (get_undolevel() < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 curbuf->b_u_synced = TRUE; /* no entries, nothing to do */
3015 else
3016 {
3017 u_getbot(); /* compute ue_bot of previous u_save */
3018 curbuf->b_u_curhead = NULL;
3019 }
3020}
3021
3022/*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003023 * ":undolist": List the leafs of the undo tree
3024 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003025 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003026ex_undolist(exarg_T *eap UNUSED)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003027{
3028 garray_T ga;
3029 u_header_T *uhp;
3030 int mark;
3031 int nomark;
3032 int changes = 1;
3033 int i;
3034
3035 /*
3036 * 1: walk the tree to find all leafs, put the info in "ga".
3037 * 2: sort the lines
3038 * 3: display the list
3039 */
3040 mark = ++lastmark;
3041 nomark = ++lastmark;
3042 ga_init2(&ga, (int)sizeof(char *), 20);
3043
3044 uhp = curbuf->b_u_oldhead;
3045 while (uhp != NULL)
3046 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003047 if (uhp->uh_prev.ptr == NULL && uhp->uh_walk != nomark
Bram Moolenaarca003e12006-03-17 23:19:38 +00003048 && uhp->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003049 {
3050 if (ga_grow(&ga, 1) == FAIL)
3051 break;
Bram Moolenaarea391762018-04-08 13:07:22 +02003052 vim_snprintf((char *)IObuff, IOSIZE, "%6ld %7d ",
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003053 uhp->uh_seq, changes);
3054 u_add_time(IObuff + STRLEN(IObuff), IOSIZE - STRLEN(IObuff),
3055 uhp->uh_time);
Bram Moolenaara800b422010-06-27 01:15:55 +02003056 if (uhp->uh_save_nr > 0)
3057 {
Bram Moolenaardba01a02010-11-03 19:32:42 +01003058 while (STRLEN(IObuff) < 33)
Bram Moolenaara800b422010-06-27 01:15:55 +02003059 STRCAT(IObuff, " ");
3060 vim_snprintf_add((char *)IObuff, IOSIZE,
3061 " %3ld", uhp->uh_save_nr);
3062 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003063 ((char_u **)(ga.ga_data))[ga.ga_len++] = vim_strsave(IObuff);
3064 }
3065
3066 uhp->uh_walk = mark;
3067
3068 /* go down in the tree if we haven't been there */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003069 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != nomark
3070 && uhp->uh_prev.ptr->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003071 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003072 uhp = uhp->uh_prev.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003073 ++changes;
3074 }
3075
3076 /* go to alternate branch if we haven't been there */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003077 else if (uhp->uh_alt_next.ptr != NULL
3078 && uhp->uh_alt_next.ptr->uh_walk != nomark
3079 && uhp->uh_alt_next.ptr->uh_walk != mark)
3080 uhp = uhp->uh_alt_next.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003081
3082 /* go up in the tree if we haven't been there and we are at the
3083 * start of alternate branches */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003084 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
3085 && uhp->uh_next.ptr->uh_walk != nomark
3086 && uhp->uh_next.ptr->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003087 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003088 uhp = uhp->uh_next.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003089 --changes;
3090 }
3091
3092 else
3093 {
3094 /* need to backtrack; mark this node as done */
3095 uhp->uh_walk = nomark;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003096 if (uhp->uh_alt_prev.ptr != NULL)
3097 uhp = uhp->uh_alt_prev.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003098 else
3099 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003100 uhp = uhp->uh_next.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003101 --changes;
3102 }
3103 }
3104 }
3105
3106 if (ga.ga_len == 0)
3107 MSG(_("Nothing to undo"));
3108 else
3109 {
3110 sort_strings((char_u **)ga.ga_data, ga.ga_len);
3111
3112 msg_start();
Bram Moolenaardba01a02010-11-03 19:32:42 +01003113 msg_puts_attr((char_u *)_("number changes when saved"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01003114 HL_ATTR(HLF_T));
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003115 for (i = 0; i < ga.ga_len && !got_int; ++i)
3116 {
3117 msg_putchar('\n');
3118 if (got_int)
3119 break;
3120 msg_puts(((char_u **)ga.ga_data)[i]);
3121 }
3122 msg_end();
3123
3124 ga_clear_strings(&ga);
3125 }
3126}
3127
3128/*
3129 * Put the timestamp of an undo header in "buf[buflen]" in a nice format.
3130 */
3131 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003132u_add_time(char_u *buf, size_t buflen, time_t tt)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003133{
3134#ifdef HAVE_STRFTIME
3135 struct tm *curtime;
3136
Bram Moolenaar170b10b2016-07-29 16:15:27 +02003137 if (vim_time() - tt >= 100)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003138 {
3139 curtime = localtime(&tt);
Bram Moolenaar170b10b2016-07-29 16:15:27 +02003140 if (vim_time() - tt < (60L * 60L * 12L))
Bram Moolenaardba01a02010-11-03 19:32:42 +01003141 /* within 12 hours */
3142 (void)strftime((char *)buf, buflen, "%H:%M:%S", curtime);
Bram Moolenaardba01a02010-11-03 19:32:42 +01003143 else
Bram Moolenaarb9ce83e2012-08-23 12:59:02 +02003144 /* longer ago */
Bram Moolenaar5e3d6ca2011-01-22 21:25:11 +01003145 (void)strftime((char *)buf, buflen, "%Y/%m/%d %H:%M:%S", curtime);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003146 }
3147 else
3148#endif
Bram Moolenaarfd6100b2018-08-21 17:07:45 +02003149 {
3150 long seconds = (long)(vim_time() - tt);
3151
3152 vim_snprintf((char *)buf, buflen,
3153 NGETTEXT("%ld second ago", "%ld seconds ago", seconds),
3154 seconds);
3155 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003156}
3157
3158/*
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003159 * ":undojoin": continue adding to the last entry list
3160 */
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003161 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003162ex_undojoin(exarg_T *eap UNUSED)
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003163{
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003164 if (curbuf->b_u_newhead == NULL)
3165 return; /* nothing changed before */
Bram Moolenaar57657d82006-04-21 22:12:41 +00003166 if (curbuf->b_u_curhead != NULL)
3167 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003168 emsg(_("E790: undojoin is not allowed after undo"));
Bram Moolenaar57657d82006-04-21 22:12:41 +00003169 return;
3170 }
3171 if (!curbuf->b_u_synced)
3172 return; /* already unsynced */
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003173 if (get_undolevel() < 0)
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003174 return; /* no entries, nothing to do */
3175 else
Bram Moolenaar5e4e1b12017-01-17 22:09:45 +01003176 /* Append next change to the last entry */
3177 curbuf->b_u_synced = FALSE;
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003178}
3179
3180/*
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003181 * Called after writing or reloading the file and setting b_changed to FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 * Now an undo means that the buffer is modified.
3183 */
3184 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003185u_unchanged(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186{
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003187 u_unch_branch(buf->b_u_oldhead);
3188 buf->b_did_warn = FALSE;
3189}
3190
Bram Moolenaar730cde92010-06-27 05:18:54 +02003191/*
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003192 * After reloading a buffer which was saved for 'undoreload': Find the first
3193 * line that was changed and set the cursor there.
3194 */
3195 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003196u_find_first_changed(void)
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003197{
3198 u_header_T *uhp = curbuf->b_u_newhead;
3199 u_entry_T *uep;
3200 linenr_T lnum;
3201
3202 if (curbuf->b_u_curhead != NULL || uhp == NULL)
3203 return; /* undid something in an autocmd? */
3204
3205 /* Check that the last undo block was for the whole file. */
3206 uep = uhp->uh_entry;
3207 if (uep->ue_top != 0 || uep->ue_bot != 0)
3208 return;
3209
3210 for (lnum = 1; lnum < curbuf->b_ml.ml_line_count
3211 && lnum <= uep->ue_size; ++lnum)
Bram Moolenaarccae4672019-01-04 15:09:57 +01003212 {
3213 char_u *p = ml_get_buf(curbuf, lnum, FALSE);
3214
3215 if (uep->ue_array[lnum - 1].ul_len != curbuf->b_ml.ml_line_len
3216 || memcmp(p, uep->ue_array[lnum - 1].ul_line, uep->ue_array[lnum - 1].ul_len) != 0)
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003217 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003218 CLEAR_POS(&(uhp->uh_cursor));
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003219 uhp->uh_cursor.lnum = lnum;
3220 return;
3221 }
Bram Moolenaarccae4672019-01-04 15:09:57 +01003222 }
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003223 if (curbuf->b_ml.ml_line_count != uep->ue_size)
3224 {
3225 /* lines added or deleted at the end, put the cursor there */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003226 CLEAR_POS(&(uhp->uh_cursor));
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003227 uhp->uh_cursor.lnum = lnum;
3228 }
3229}
3230
3231/*
Bram Moolenaar730cde92010-06-27 05:18:54 +02003232 * Increase the write count, store it in the last undo header, what would be
3233 * used for "u".
3234 */
3235 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003236u_update_save_nr(buf_T *buf)
Bram Moolenaar730cde92010-06-27 05:18:54 +02003237{
3238 u_header_T *uhp;
3239
3240 ++buf->b_u_save_nr_last;
3241 buf->b_u_save_nr_cur = buf->b_u_save_nr_last;
3242 uhp = buf->b_u_curhead;
3243 if (uhp != NULL)
3244 uhp = uhp->uh_next.ptr;
3245 else
3246 uhp = buf->b_u_newhead;
3247 if (uhp != NULL)
3248 uhp->uh_save_nr = buf->b_u_save_nr_last;
3249}
3250
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003251 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003252u_unch_branch(u_header_T *uhp)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003253{
Bram Moolenaar1e607892006-03-13 22:15:53 +00003254 u_header_T *uh;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003256 for (uh = uhp; uh != NULL; uh = uh->uh_prev.ptr)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003257 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 uh->uh_flags |= UH_CHANGED;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003259 if (uh->uh_alt_next.ptr != NULL)
3260 u_unch_branch(uh->uh_alt_next.ptr); /* recursive */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262}
3263
3264/*
3265 * Get pointer to last added entry.
3266 * If it's not valid, give an error message and return NULL.
3267 */
3268 static u_entry_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003269u_get_headentry(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270{
3271 if (curbuf->b_u_newhead == NULL || curbuf->b_u_newhead->uh_entry == NULL)
3272 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003273 iemsg(_("E439: undo list corrupt"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 return NULL;
3275 }
3276 return curbuf->b_u_newhead->uh_entry;
3277}
3278
3279/*
3280 * u_getbot(): compute the line number of the previous u_save
3281 * It is called only when b_u_synced is FALSE.
3282 */
3283 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003284u_getbot(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285{
3286 u_entry_T *uep;
3287 linenr_T extra;
3288
3289 uep = u_get_headentry(); /* check for corrupt undo list */
3290 if (uep == NULL)
3291 return;
3292
3293 uep = curbuf->b_u_newhead->uh_getbot_entry;
3294 if (uep != NULL)
3295 {
3296 /*
3297 * the new ue_bot is computed from the number of lines that has been
3298 * inserted (0 - deleted) since calling u_save. This is equal to the
3299 * old line count subtracted from the current line count.
3300 */
3301 extra = curbuf->b_ml.ml_line_count - uep->ue_lcount;
3302 uep->ue_bot = uep->ue_top + uep->ue_size + 1 + extra;
3303 if (uep->ue_bot < 1 || uep->ue_bot > curbuf->b_ml.ml_line_count)
3304 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003305 iemsg(_("E440: undo line missing"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 uep->ue_bot = uep->ue_top + 1; /* assume all lines deleted, will
3307 * get all the old lines back
3308 * without deleting the current
3309 * ones */
3310 }
3311
3312 curbuf->b_u_newhead->uh_getbot_entry = NULL;
3313 }
3314
3315 curbuf->b_u_synced = TRUE;
3316}
3317
3318/*
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003319 * Free one header "uhp" and its entry list and adjust the pointers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 */
3321 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003322u_freeheader(
3323 buf_T *buf,
3324 u_header_T *uhp,
3325 u_header_T **uhpp) /* if not NULL reset when freeing this header */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326{
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003327 u_header_T *uhap;
3328
Bram Moolenaar1e607892006-03-13 22:15:53 +00003329 /* When there is an alternate redo list free that branch completely,
3330 * because we can never go there. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003331 if (uhp->uh_alt_next.ptr != NULL)
3332 u_freebranch(buf, uhp->uh_alt_next.ptr, uhpp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003334 if (uhp->uh_alt_prev.ptr != NULL)
3335 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336
Bram Moolenaar1e607892006-03-13 22:15:53 +00003337 /* Update the links in the list to remove the header. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003338 if (uhp->uh_next.ptr == NULL)
3339 buf->b_u_oldhead = uhp->uh_prev.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003341 uhp->uh_next.ptr->uh_prev.ptr = uhp->uh_prev.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003343 if (uhp->uh_prev.ptr == NULL)
3344 buf->b_u_newhead = uhp->uh_next.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003346 for (uhap = uhp->uh_prev.ptr; uhap != NULL;
3347 uhap = uhap->uh_alt_next.ptr)
3348 uhap->uh_next.ptr = uhp->uh_next.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349
Bram Moolenaar1e607892006-03-13 22:15:53 +00003350 u_freeentries(buf, uhp, uhpp);
3351}
3352
3353/*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003354 * Free an alternate branch and any following alternate branches.
Bram Moolenaar1e607892006-03-13 22:15:53 +00003355 */
3356 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003357u_freebranch(
3358 buf_T *buf,
3359 u_header_T *uhp,
3360 u_header_T **uhpp) /* if not NULL reset when freeing this header */
Bram Moolenaar1e607892006-03-13 22:15:53 +00003361{
3362 u_header_T *tofree, *next;
3363
Bram Moolenaar07d06772007-11-10 21:51:15 +00003364 /* If this is the top branch we may need to use u_freeheader() to update
3365 * all the pointers. */
3366 if (uhp == buf->b_u_oldhead)
3367 {
Bram Moolenaaraa887322013-11-07 03:04:11 +01003368 while (buf->b_u_oldhead != NULL)
3369 u_freeheader(buf, buf->b_u_oldhead, uhpp);
Bram Moolenaar07d06772007-11-10 21:51:15 +00003370 return;
3371 }
3372
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003373 if (uhp->uh_alt_prev.ptr != NULL)
3374 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +00003375
3376 next = uhp;
3377 while (next != NULL)
3378 {
3379 tofree = next;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003380 if (tofree->uh_alt_next.ptr != NULL)
3381 u_freebranch(buf, tofree->uh_alt_next.ptr, uhpp); /* recursive */
3382 next = tofree->uh_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00003383 u_freeentries(buf, tofree, uhpp);
3384 }
3385}
3386
3387/*
3388 * Free all the undo entries for one header and the header itself.
3389 * This means that "uhp" is invalid when returning.
3390 */
3391 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003392u_freeentries(
3393 buf_T *buf,
3394 u_header_T *uhp,
3395 u_header_T **uhpp) /* if not NULL reset when freeing this header */
Bram Moolenaar1e607892006-03-13 22:15:53 +00003396{
3397 u_entry_T *uep, *nuep;
3398
3399 /* Check for pointers to the header that become invalid now. */
3400 if (buf->b_u_curhead == uhp)
3401 buf->b_u_curhead = NULL;
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003402 if (buf->b_u_newhead == uhp)
3403 buf->b_u_newhead = NULL; /* freeing the newest entry */
Bram Moolenaar1e607892006-03-13 22:15:53 +00003404 if (uhpp != NULL && uhp == *uhpp)
3405 *uhpp = NULL;
3406
3407 for (uep = uhp->uh_entry; uep != NULL; uep = nuep)
3408 {
3409 nuep = uep->ue_next;
3410 u_freeentry(uep, uep->ue_size);
3411 }
3412
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003413#ifdef U_DEBUG
3414 uhp->uh_magic = 0;
3415#endif
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003416 vim_free((char_u *)uhp);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003417 --buf->b_u_numhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418}
3419
3420/*
3421 * free entry 'uep' and 'n' lines in uep->ue_array[]
3422 */
3423 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003424u_freeentry(u_entry_T *uep, long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425{
Bram Moolenaar8d343302005-07-12 22:46:17 +00003426 while (n > 0)
Bram Moolenaarccae4672019-01-04 15:09:57 +01003427 vim_free(uep->ue_array[--n].ul_line);
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003428 vim_free((char_u *)uep->ue_array);
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003429#ifdef U_DEBUG
3430 uep->ue_magic = 0;
3431#endif
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003432 vim_free((char_u *)uep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433}
3434
3435/*
3436 * invalidate the undo buffer; called when storage has already been released
3437 */
3438 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003439u_clearall(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440{
3441 buf->b_u_newhead = buf->b_u_oldhead = buf->b_u_curhead = NULL;
3442 buf->b_u_synced = TRUE;
3443 buf->b_u_numhead = 0;
Bram Moolenaarccae4672019-01-04 15:09:57 +01003444 buf->b_u_line_ptr.ul_line = NULL;
3445 buf->b_u_line_ptr.ul_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 buf->b_u_line_lnum = 0;
3447}
3448
3449/*
Bram Moolenaarccae4672019-01-04 15:09:57 +01003450 * Save the line "lnum" for the "U" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 */
3452 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003453u_saveline(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454{
3455 if (lnum == curbuf->b_u_line_lnum) /* line is already saved */
3456 return;
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00003457 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) /* should never happen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 return;
3459 u_clearline();
3460 curbuf->b_u_line_lnum = lnum;
3461 if (curwin->w_cursor.lnum == lnum)
3462 curbuf->b_u_line_colnr = curwin->w_cursor.col;
3463 else
3464 curbuf->b_u_line_colnr = 0;
Bram Moolenaarccae4672019-01-04 15:09:57 +01003465 if (u_save_line(&curbuf->b_u_line_ptr, lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 do_outofmem_msg((long_u)0);
3467}
3468
3469/*
3470 * clear the line saved for the "U" command
3471 * (this is used externally for crossing a line while in insert mode)
3472 */
3473 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003474u_clearline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475{
Bram Moolenaarccae4672019-01-04 15:09:57 +01003476 if (curbuf->b_u_line_ptr.ul_line != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 {
Bram Moolenaarccae4672019-01-04 15:09:57 +01003478 VIM_CLEAR(curbuf->b_u_line_ptr.ul_line);
3479 curbuf->b_u_line_ptr.ul_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 curbuf->b_u_line_lnum = 0;
3481 }
3482}
3483
3484/*
3485 * Implementation of the "U" command.
3486 * Differentiation from vi: "U" can be undone with the next "U".
3487 * We also allow the cursor to be in another line.
Bram Moolenaard04b7502010-07-08 22:27:55 +02003488 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 */
3490 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003491u_undoline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492{
Bram Moolenaarccae4672019-01-04 15:09:57 +01003493 colnr_T t;
3494 undoline_T oldp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495
3496 if (undo_off)
3497 return;
3498
Bram Moolenaarccae4672019-01-04 15:09:57 +01003499 if (curbuf->b_u_line_ptr.ul_line == NULL
Bram Moolenaare3300c82008-02-13 14:21:38 +00003500 || curbuf->b_u_line_lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 {
3502 beep_flush();
3503 return;
3504 }
Bram Moolenaare3300c82008-02-13 14:21:38 +00003505
Bram Moolenaarccae4672019-01-04 15:09:57 +01003506 // first save the line for the 'u' command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 if (u_savecommon(curbuf->b_u_line_lnum - 1,
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003508 curbuf->b_u_line_lnum + 1, (linenr_T)0, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 return;
Bram Moolenaarccae4672019-01-04 15:09:57 +01003510 if (u_save_line(&oldp, curbuf->b_u_line_lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 {
3512 do_outofmem_msg((long_u)0);
3513 return;
3514 }
Bram Moolenaarccae4672019-01-04 15:09:57 +01003515 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 +00003516 changed_bytes(curbuf->b_u_line_lnum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 curbuf->b_u_line_ptr = oldp;
3518
3519 t = curbuf->b_u_line_colnr;
3520 if (curwin->w_cursor.lnum == curbuf->b_u_line_lnum)
3521 curbuf->b_u_line_colnr = curwin->w_cursor.col;
3522 curwin->w_cursor.col = t;
3523 curwin->w_cursor.lnum = curbuf->b_u_line_lnum;
Bram Moolenaare3300c82008-02-13 14:21:38 +00003524 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525}
3526
3527/*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003528 * Free all allocated memory blocks for the buffer 'buf'.
3529 */
3530 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003531u_blockfree(buf_T *buf)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003532{
Bram Moolenaar1e607892006-03-13 22:15:53 +00003533 while (buf->b_u_oldhead != NULL)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003534 u_freeheader(buf, buf->b_u_oldhead, NULL);
Bram Moolenaarccae4672019-01-04 15:09:57 +01003535 vim_free(buf->b_u_line_ptr.ul_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536}
3537
3538/*
3539 * Check if the 'modified' flag is set, or 'ff' has changed (only need to
3540 * check the first character, because it can only be "dos", "unix" or "mac").
3541 * "nofile" and "scratch" type buffers are considered to always be unchanged.
Bram Moolenaarf405c8f2017-12-09 19:51:49 +01003542 * Also considers a buffer changed when a terminal window contains a running
3543 * job.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 */
3545 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003546bufIsChanged(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547{
Bram Moolenaareb44a682017-08-03 22:44:55 +02003548#ifdef FEAT_TERMINAL
3549 if (term_job_running(buf->b_term))
3550 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551#endif
Bram Moolenaarf405c8f2017-12-09 19:51:49 +01003552 return bufIsChangedNotTerm(buf);
3553}
3554
3555/*
3556 * Like bufIsChanged() but ignoring a terminal window.
3557 */
3558 int
3559bufIsChangedNotTerm(buf_T *buf)
3560{
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003561 // In a "prompt" buffer we do respect 'modified', so that we can control
3562 // closing the window by setting or resetting that option.
3563 return (!bt_dontwrite(buf) || bt_prompt(buf))
Bram Moolenaareb44a682017-08-03 22:44:55 +02003564 && (buf->b_changed || file_ff_differs(buf, TRUE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565}
3566
3567 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003568curbufIsChanged(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569{
Bram Moolenaareb44a682017-08-03 22:44:55 +02003570 return bufIsChanged(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571}
Bram Moolenaara800b422010-06-27 01:15:55 +02003572
3573#if defined(FEAT_EVAL) || defined(PROTO)
3574/*
3575 * For undotree(): Append the list of undo blocks at "first_uhp" to "list".
3576 * Recursive.
3577 */
3578 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003579u_eval_tree(u_header_T *first_uhp, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02003580{
3581 u_header_T *uhp = first_uhp;
3582 dict_T *dict;
3583
3584 while (uhp != NULL)
3585 {
3586 dict = dict_alloc();
3587 if (dict == NULL)
3588 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003589 dict_add_number(dict, "seq", uhp->uh_seq);
3590 dict_add_number(dict, "time", (long)uhp->uh_time);
Bram Moolenaara800b422010-06-27 01:15:55 +02003591 if (uhp == curbuf->b_u_newhead)
Bram Moolenaare0be1672018-07-08 16:50:37 +02003592 dict_add_number(dict, "newhead", 1);
Bram Moolenaara800b422010-06-27 01:15:55 +02003593 if (uhp == curbuf->b_u_curhead)
Bram Moolenaare0be1672018-07-08 16:50:37 +02003594 dict_add_number(dict, "curhead", 1);
Bram Moolenaara800b422010-06-27 01:15:55 +02003595 if (uhp->uh_save_nr > 0)
Bram Moolenaare0be1672018-07-08 16:50:37 +02003596 dict_add_number(dict, "save", uhp->uh_save_nr);
Bram Moolenaara800b422010-06-27 01:15:55 +02003597
3598 if (uhp->uh_alt_next.ptr != NULL)
3599 {
3600 list_T *alt_list = list_alloc();
3601
3602 if (alt_list != NULL)
3603 {
3604 /* Recursive call to add alternate undo tree. */
3605 u_eval_tree(uhp->uh_alt_next.ptr, alt_list);
3606 dict_add_list(dict, "alt", alt_list);
3607 }
3608 }
3609
3610 list_append_dict(list, dict);
3611 uhp = uhp->uh_prev.ptr;
3612 }
3613}
3614#endif