blob: 6547ee6420c921e476a12bce6d61b7f1b9009f69 [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 Moolenaar18a4ba22019-05-24 19:39:03 +0200127#define U_ALLOC_LINE(size) lalloc(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 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200370 // This uses the length in the memline, thus text properties are
371 // included.
Bram Moolenaarccae4672019-01-04 15:09:57 +0100372 ul->ul_len = curbuf->b_ml.ml_line_len;
373 ul->ul_line = vim_memsave(line, ul->ul_len);
374 }
375 return ul->ul_line == NULL ? FAIL : OK;
376}
377
378/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200379 * Common code for various ways to save text before a change.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200380 * "top" is the line above the first changed line.
381 * "bot" is the line below the last changed line.
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200382 * "newbot" is the new bottom line. Use zero when not known.
383 * "reload" is TRUE when saving for a buffer reload.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200384 * Careful: may trigger autocommands that reload the buffer.
385 * Returns FAIL when lines could not be saved, OK otherwise.
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200386 */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200387 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100388u_savecommon(
389 linenr_T top,
390 linenr_T bot,
391 linenr_T newbot,
392 int reload)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393{
Bram Moolenaar1e607892006-03-13 22:15:53 +0000394 linenr_T lnum;
395 long i;
396 u_header_T *uhp;
397 u_header_T *old_curhead;
398 u_entry_T *uep;
399 u_entry_T *prev_uep;
400 long size;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200402 if (!reload)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 {
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200404 /* When making changes is not allowed return FAIL. It's a crude way
405 * to make all change commands fail. */
406 if (!undo_allowed())
Bram Moolenaar009b2592004-10-24 19:18:58 +0000407 return FAIL;
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200408
409#ifdef FEAT_NETBEANS_INTG
410 /*
411 * Netbeans defines areas that cannot be modified. Bail out here when
412 * trying to change text in a guarded area.
413 */
414 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +0000415 {
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200416 if (netbeans_is_guarded(top, bot))
417 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100418 emsg(_(e_guarded));
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200419 return FAIL;
420 }
421 if (curbuf->b_p_ro)
422 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100423 emsg(_(e_nbreadonly));
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200424 return FAIL;
425 }
Bram Moolenaar009b2592004-10-24 19:18:58 +0000426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200428#ifdef FEAT_TERMINAL
429 /* A change in a terminal buffer removes the highlighting. */
430 term_change_in_curbuf();
431#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200433 /*
434 * Saving text for undo means we are going to make a change. Give a
435 * warning for a read-only file before making the change, so that the
436 * FileChangedRO event can replace the buffer with a read-write version
437 * (e.g., obtained from a source control system).
438 */
439 change_warning(0);
440 if (bot > curbuf->b_ml.ml_line_count + 1)
441 {
442 /* This happens when the FileChangedRO autocommand changes the
443 * file in a way it becomes shorter. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100444 emsg(_("E881: Line count changed unexpectedly"));
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200445 return FAIL;
446 }
Bram Moolenaard04b7502010-07-08 22:27:55 +0200447 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200448
449#ifdef U_DEBUG
450 u_check(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451#endif
452
453 size = bot - top - 1;
454
455 /*
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200456 * If curbuf->b_u_synced == TRUE make a new header.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 */
458 if (curbuf->b_u_synced)
459 {
460#ifdef FEAT_JUMPLIST
461 /* Need to create new entry in b_changelist. */
462 curbuf->b_new_change = TRUE;
463#endif
464
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100465 if (get_undolevel() >= 0)
Bram Moolenaar1e607892006-03-13 22:15:53 +0000466 {
467 /*
468 * Make a new header entry. Do this first so that we don't mess
469 * up the undo info when out of memory.
470 */
Bram Moolenaarf05e3b02010-05-29 15:40:47 +0200471 uhp = (u_header_T *)U_ALLOC_LINE(sizeof(u_header_T));
Bram Moolenaar1e607892006-03-13 22:15:53 +0000472 if (uhp == NULL)
473 goto nomem;
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000474#ifdef U_DEBUG
475 uhp->uh_magic = UH_MAGIC;
476#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +0000477 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000478 else
479 uhp = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000480
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 /*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000482 * If we undid more than we redid, move the entry lists before and
483 * including curbuf->b_u_curhead to an alternate branch.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 */
Bram Moolenaar1e607892006-03-13 22:15:53 +0000485 old_curhead = curbuf->b_u_curhead;
486 if (old_curhead != NULL)
487 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200488 curbuf->b_u_newhead = old_curhead->uh_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000489 curbuf->b_u_curhead = NULL;
490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491
492 /*
493 * free headers to keep the size right
494 */
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100495 while (curbuf->b_u_numhead > get_undolevel()
496 && curbuf->b_u_oldhead != NULL)
Bram Moolenaar1e607892006-03-13 22:15:53 +0000497 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000498 u_header_T *uhfree = curbuf->b_u_oldhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000499
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000500 if (uhfree == old_curhead)
501 /* Can't reconnect the branch, delete all of it. */
502 u_freebranch(curbuf, uhfree, &old_curhead);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200503 else if (uhfree->uh_alt_next.ptr == NULL)
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000504 /* There is no branch, only free one header. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000505 u_freeheader(curbuf, uhfree, &old_curhead);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000506 else
507 {
508 /* Free the oldest alternate branch as a whole. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200509 while (uhfree->uh_alt_next.ptr != NULL)
510 uhfree = uhfree->uh_alt_next.ptr;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000511 u_freebranch(curbuf, uhfree, &old_curhead);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000512 }
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000513#ifdef U_DEBUG
514 u_check(TRUE);
515#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +0000516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000518 if (uhp == NULL) /* no undo at all */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 {
Bram Moolenaar1e607892006-03-13 22:15:53 +0000520 if (old_curhead != NULL)
521 u_freebranch(curbuf, old_curhead, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 curbuf->b_u_synced = FALSE;
523 return OK;
524 }
525
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200526 uhp->uh_prev.ptr = NULL;
527 uhp->uh_next.ptr = curbuf->b_u_newhead;
528 uhp->uh_alt_next.ptr = old_curhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000529 if (old_curhead != NULL)
530 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200531 uhp->uh_alt_prev.ptr = old_curhead->uh_alt_prev.ptr;
532 if (uhp->uh_alt_prev.ptr != NULL)
533 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = uhp;
534 old_curhead->uh_alt_prev.ptr = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000535 if (curbuf->b_u_oldhead == old_curhead)
536 curbuf->b_u_oldhead = uhp;
537 }
Bram Moolenaar89ed3df2007-01-09 19:23:12 +0000538 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200539 uhp->uh_alt_prev.ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 if (curbuf->b_u_newhead != NULL)
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200541 curbuf->b_u_newhead->uh_prev.ptr = uhp;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000542
Bram Moolenaarca003e12006-03-17 23:19:38 +0000543 uhp->uh_seq = ++curbuf->b_u_seq_last;
544 curbuf->b_u_seq_cur = uhp->uh_seq;
Bram Moolenaar170b10b2016-07-29 16:15:27 +0200545 uhp->uh_time = vim_time();
Bram Moolenaara800b422010-06-27 01:15:55 +0200546 uhp->uh_save_nr = 0;
547 curbuf->b_u_time_cur = uhp->uh_time + 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000548
Bram Moolenaar1e607892006-03-13 22:15:53 +0000549 uhp->uh_walk = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 uhp->uh_entry = NULL;
551 uhp->uh_getbot_entry = NULL;
552 uhp->uh_cursor = curwin->w_cursor; /* save cursor pos. for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 if (virtual_active() && curwin->w_cursor.coladd > 0)
554 uhp->uh_cursor_vcol = getviscol();
555 else
556 uhp->uh_cursor_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557
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 Moolenaar4f974752019-02-17 17:44:42 +0100650#if !defined(UNIX) && !defined(MSWIN)
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{
Bram Moolenaar964b3742019-05-24 18:54:09 +02001126 char_u *ptr = alloc(len + 1);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001127
1128 if (ptr != NULL)
1129 {
1130 if (len > 0 && undo_read(bi, ptr, len) == FAIL)
1131 {
1132 vim_free(ptr);
1133 return NULL;
1134 }
Bram Moolenaarccae4672019-01-04 15:09:57 +01001135 // In case there are text properties there already is a NUL, but
1136 // checking for that is more expensive than just adding a dummy byte.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001137 ptr[len] = NUL;
1138#ifdef FEAT_CRYPT
1139 if (bi->bi_state != NULL && bi->bi_buffer == NULL)
1140 crypt_decode_inplace(bi->bi_state, ptr, len);
1141#endif
1142 }
1143 return ptr;
1144}
1145
1146/*
1147 * Writes the (not encrypted) header and initializes encryption if needed.
1148 */
1149 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001150serialize_header(bufinfo_T *bi, char_u *hash)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001151{
Bram Moolenaarccae4672019-01-04 15:09:57 +01001152 long len;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001153 buf_T *buf = bi->bi_buf;
1154 FILE *fp = bi->bi_fp;
1155 char_u time_buf[8];
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001156
1157 /* Start writing, first the magic marker and undo info version. */
1158 if (fwrite(UF_START_MAGIC, (size_t)UF_START_MAGIC_LEN, (size_t)1, fp) != 1)
1159 return FAIL;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001160
1161 /* If the buffer is encrypted then all text bytes following will be
1162 * encrypted. Numbers and other info is not crypted. */
1163#ifdef FEAT_CRYPT
Bram Moolenaarfa0ff9a2010-07-25 16:05:19 +02001164 if (*buf->b_p_key != NUL)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001165 {
1166 char_u *header;
1167 int header_len;
1168
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001169 undo_write_bytes(bi, (long_u)UF_VERSION_CRYPT, 2);
1170 bi->bi_state = crypt_create_for_writing(crypt_get_method_nr(buf),
1171 buf->b_p_key, &header, &header_len);
1172 if (bi->bi_state == NULL)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001173 return FAIL;
Bram Moolenaarccae4672019-01-04 15:09:57 +01001174 len = (long)fwrite(header, (size_t)header_len, (size_t)1, fp);
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001175 vim_free(header);
1176 if (len != 1)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001177 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001178 crypt_free_state(bi->bi_state);
1179 bi->bi_state = NULL;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001180 return FAIL;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001181 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001182
1183 if (crypt_whole_undofile(crypt_get_method_nr(buf)))
1184 {
1185 bi->bi_buffer = alloc(CRYPT_BUF_SIZE);
1186 if (bi->bi_buffer == NULL)
1187 {
1188 crypt_free_state(bi->bi_state);
1189 bi->bi_state = NULL;
1190 return FAIL;
1191 }
1192 bi->bi_used = 0;
1193 }
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001194 }
1195 else
1196#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001197 undo_write_bytes(bi, (long_u)UF_VERSION, 2);
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001198
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001199
1200 /* Write a hash of the buffer text, so that we can verify it is still the
1201 * same when reading the buffer text. */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001202 if (undo_write(bi, hash, (size_t)UNDO_HASH_SIZE) == FAIL)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001203 return FAIL;
1204
1205 /* buffer-specific data */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001206 undo_write_bytes(bi, (long_u)buf->b_ml.ml_line_count, 4);
Bram Moolenaarccae4672019-01-04 15:09:57 +01001207 len = buf->b_u_line_ptr.ul_line == NULL
Bram Moolenaar8aef43b2019-01-08 20:14:35 +01001208 ? 0L : (long)STRLEN(buf->b_u_line_ptr.ul_line);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001209 undo_write_bytes(bi, (long_u)len, 4);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +01001210 if (len > 0 && fwrite_crypt(bi, buf->b_u_line_ptr.ul_line, (size_t)len)
1211 == FAIL)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001212 return FAIL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001213 undo_write_bytes(bi, (long_u)buf->b_u_line_lnum, 4);
1214 undo_write_bytes(bi, (long_u)buf->b_u_line_colnr, 4);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001215
1216 /* Undo structures header data */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001217 put_header_ptr(bi, buf->b_u_oldhead);
1218 put_header_ptr(bi, buf->b_u_newhead);
1219 put_header_ptr(bi, buf->b_u_curhead);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001220
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001221 undo_write_bytes(bi, (long_u)buf->b_u_numhead, 4);
1222 undo_write_bytes(bi, (long_u)buf->b_u_seq_last, 4);
1223 undo_write_bytes(bi, (long_u)buf->b_u_seq_cur, 4);
1224 time_to_bytes(buf->b_u_time_cur, time_buf);
1225 undo_write(bi, time_buf, 8);
Bram Moolenaara800b422010-06-27 01:15:55 +02001226
1227 /* Optional fields. */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001228 undo_write_bytes(bi, 4, 1);
1229 undo_write_bytes(bi, UF_LAST_SAVE_NR, 1);
1230 undo_write_bytes(bi, (long_u)buf->b_u_save_nr_last, 4);
Bram Moolenaara800b422010-06-27 01:15:55 +02001231
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001232 undo_write_bytes(bi, 0, 1); /* end marker */
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001233
1234 return OK;
1235}
1236
1237 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001238serialize_uhp(bufinfo_T *bi, u_header_T *uhp)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001239{
1240 int i;
1241 u_entry_T *uep;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001242 char_u time_buf[8];
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001243
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001244 if (undo_write_bytes(bi, (long_u)UF_HEADER_MAGIC, 2) == FAIL)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001245 return FAIL;
1246
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001247 put_header_ptr(bi, uhp->uh_next.ptr);
1248 put_header_ptr(bi, uhp->uh_prev.ptr);
1249 put_header_ptr(bi, uhp->uh_alt_next.ptr);
1250 put_header_ptr(bi, uhp->uh_alt_prev.ptr);
1251 undo_write_bytes(bi, uhp->uh_seq, 4);
1252 serialize_pos(bi, uhp->uh_cursor);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001253 undo_write_bytes(bi, (long_u)uhp->uh_cursor_vcol, 4);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001254 undo_write_bytes(bi, (long_u)uhp->uh_flags, 2);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001255 /* Assume NMARKS will stay the same. */
1256 for (i = 0; i < NMARKS; ++i)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001257 serialize_pos(bi, uhp->uh_namedm[i]);
1258 serialize_visualinfo(bi, &uhp->uh_visual);
1259 time_to_bytes(uhp->uh_time, time_buf);
1260 undo_write(bi, time_buf, 8);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001261
Bram Moolenaara800b422010-06-27 01:15:55 +02001262 /* Optional fields. */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001263 undo_write_bytes(bi, 4, 1);
1264 undo_write_bytes(bi, UHP_SAVE_NR, 1);
1265 undo_write_bytes(bi, (long_u)uhp->uh_save_nr, 4);
Bram Moolenaara800b422010-06-27 01:15:55 +02001266
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001267 undo_write_bytes(bi, 0, 1); /* end marker */
Bram Moolenaara800b422010-06-27 01:15:55 +02001268
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001269 /* Write all the entries. */
1270 for (uep = uhp->uh_entry; uep != NULL; uep = uep->ue_next)
1271 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001272 undo_write_bytes(bi, (long_u)UF_ENTRY_MAGIC, 2);
1273 if (serialize_uep(bi, uep) == FAIL)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001274 return FAIL;
1275 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001276 undo_write_bytes(bi, (long_u)UF_ENTRY_END_MAGIC, 2);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001277 return OK;
1278}
1279
1280 static u_header_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001281unserialize_uhp(bufinfo_T *bi, char_u *file_name)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001282{
1283 u_header_T *uhp;
1284 int i;
1285 u_entry_T *uep, *last_uep;
1286 int c;
1287 int error;
1288
1289 uhp = (u_header_T *)U_ALLOC_LINE(sizeof(u_header_T));
1290 if (uhp == NULL)
1291 return NULL;
1292 vim_memset(uhp, 0, sizeof(u_header_T));
1293#ifdef U_DEBUG
1294 uhp->uh_magic = UH_MAGIC;
1295#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001296 uhp->uh_next.seq = undo_read_4c(bi);
1297 uhp->uh_prev.seq = undo_read_4c(bi);
1298 uhp->uh_alt_next.seq = undo_read_4c(bi);
1299 uhp->uh_alt_prev.seq = undo_read_4c(bi);
1300 uhp->uh_seq = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001301 if (uhp->uh_seq <= 0)
1302 {
1303 corruption_error("uh_seq", file_name);
1304 vim_free(uhp);
1305 return NULL;
1306 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001307 unserialize_pos(bi, &uhp->uh_cursor);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001308 uhp->uh_cursor_vcol = undo_read_4c(bi);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001309 uhp->uh_flags = undo_read_2c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001310 for (i = 0; i < NMARKS; ++i)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001311 unserialize_pos(bi, &uhp->uh_namedm[i]);
1312 unserialize_visualinfo(bi, &uhp->uh_visual);
1313 uhp->uh_time = undo_read_time(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001314
Bram Moolenaara800b422010-06-27 01:15:55 +02001315 /* Optional fields. */
Bram Moolenaar69154f22010-07-18 21:42:34 +02001316 for (;;)
1317 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001318 int len = undo_read_byte(bi);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001319 int what;
Bram Moolenaara800b422010-06-27 01:15:55 +02001320
Bram Moolenaar69154f22010-07-18 21:42:34 +02001321 if (len == 0)
1322 break;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001323 what = undo_read_byte(bi);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001324 switch (what)
1325 {
1326 case UHP_SAVE_NR:
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001327 uhp->uh_save_nr = undo_read_4c(bi);
Bram Moolenaara800b422010-06-27 01:15:55 +02001328 break;
Bram Moolenaar69154f22010-07-18 21:42:34 +02001329 default:
1330 /* field not supported, skip */
1331 while (--len >= 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001332 (void)undo_read_byte(bi);
Bram Moolenaara800b422010-06-27 01:15:55 +02001333 }
Bram Moolenaar69154f22010-07-18 21:42:34 +02001334 }
Bram Moolenaara800b422010-06-27 01:15:55 +02001335
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001336 /* Unserialize the uep list. */
1337 last_uep = NULL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001338 while ((c = undo_read_2c(bi)) == UF_ENTRY_MAGIC)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001339 {
1340 error = FALSE;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001341 uep = unserialize_uep(bi, &error, file_name);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001342 if (last_uep == NULL)
1343 uhp->uh_entry = uep;
1344 else
1345 last_uep->ue_next = uep;
1346 last_uep = uep;
1347 if (uep == NULL || error)
1348 {
1349 u_free_uhp(uhp);
1350 return NULL;
1351 }
1352 }
1353 if (c != UF_ENTRY_END_MAGIC)
1354 {
1355 corruption_error("entry end", file_name);
1356 u_free_uhp(uhp);
1357 return NULL;
1358 }
1359
1360 return uhp;
1361}
1362
Bram Moolenaar9db58062010-05-29 20:33:07 +02001363/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001364 * Serialize "uep".
Bram Moolenaar9db58062010-05-29 20:33:07 +02001365 */
1366 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001367serialize_uep(
1368 bufinfo_T *bi,
1369 u_entry_T *uep)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001370{
1371 int i;
1372 size_t len;
1373
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001374 undo_write_bytes(bi, (long_u)uep->ue_top, 4);
1375 undo_write_bytes(bi, (long_u)uep->ue_bot, 4);
1376 undo_write_bytes(bi, (long_u)uep->ue_lcount, 4);
1377 undo_write_bytes(bi, (long_u)uep->ue_size, 4);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001378 for (i = 0; i < uep->ue_size; ++i)
1379 {
Bram Moolenaarccae4672019-01-04 15:09:57 +01001380 // Text is written without the text properties, since we cannot restore
1381 // the text property types.
1382 len = STRLEN(uep->ue_array[i].ul_line);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001383 if (undo_write_bytes(bi, (long_u)len, 4) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001384 return FAIL;
Bram Moolenaarccae4672019-01-04 15:09:57 +01001385 if (len > 0 && fwrite_crypt(bi, uep->ue_array[i].ul_line, len) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001386 return FAIL;
1387 }
1388 return OK;
1389}
1390
1391 static u_entry_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001392unserialize_uep(bufinfo_T *bi, int *error, char_u *file_name)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001393{
1394 int i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001395 u_entry_T *uep;
Bram Moolenaarccae4672019-01-04 15:09:57 +01001396 undoline_T *array = NULL;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001397 char_u *line;
1398 int line_len;
1399
1400 uep = (u_entry_T *)U_ALLOC_LINE(sizeof(u_entry_T));
1401 if (uep == NULL)
1402 return NULL;
1403 vim_memset(uep, 0, sizeof(u_entry_T));
1404#ifdef U_DEBUG
1405 uep->ue_magic = UE_MAGIC;
1406#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001407 uep->ue_top = undo_read_4c(bi);
1408 uep->ue_bot = undo_read_4c(bi);
1409 uep->ue_lcount = undo_read_4c(bi);
1410 uep->ue_size = undo_read_4c(bi);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001411 if (uep->ue_size > 0)
1412 {
Bram Moolenaar0c8485f2017-02-26 18:17:10 +01001413 if (uep->ue_size < LONG_MAX / (int)sizeof(char_u *))
Bram Moolenaarccae4672019-01-04 15:09:57 +01001414 array = (undoline_T *)U_ALLOC_LINE(sizeof(undoline_T) * uep->ue_size);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001415 if (array == NULL)
1416 {
1417 *error = TRUE;
1418 return uep;
1419 }
Bram Moolenaarccae4672019-01-04 15:09:57 +01001420 vim_memset(array, 0, sizeof(undoline_T) * uep->ue_size);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001421 }
1422 uep->ue_array = array;
1423
1424 for (i = 0; i < uep->ue_size; ++i)
1425 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001426 line_len = undo_read_4c(bi);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001427 if (line_len >= 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001428 line = read_string_decrypt(bi, line_len);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001429 else
1430 {
1431 line = NULL;
1432 corruption_error("line length", file_name);
1433 }
1434 if (line == NULL)
1435 {
1436 *error = TRUE;
1437 return uep;
1438 }
Bram Moolenaarccae4672019-01-04 15:09:57 +01001439 array[i].ul_line = line;
1440 array[i].ul_len = line_len + 1;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001441 }
1442 return uep;
1443}
1444
1445/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001446 * Serialize "pos".
Bram Moolenaar9db58062010-05-29 20:33:07 +02001447 */
1448 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001449serialize_pos(bufinfo_T *bi, pos_T pos)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001450{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001451 undo_write_bytes(bi, (long_u)pos.lnum, 4);
1452 undo_write_bytes(bi, (long_u)pos.col, 4);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001453 undo_write_bytes(bi, (long_u)pos.coladd, 4);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001454}
1455
1456/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001457 * Unserialize the pos_T at the current position.
Bram Moolenaar9db58062010-05-29 20:33:07 +02001458 */
1459 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001460unserialize_pos(bufinfo_T *bi, pos_T *pos)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001461{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001462 pos->lnum = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001463 if (pos->lnum < 0)
1464 pos->lnum = 0;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001465 pos->col = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001466 if (pos->col < 0)
1467 pos->col = 0;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001468 pos->coladd = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001469 if (pos->coladd < 0)
1470 pos->coladd = 0;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001471}
1472
1473/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001474 * Serialize "info".
Bram Moolenaar9db58062010-05-29 20:33:07 +02001475 */
1476 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001477serialize_visualinfo(bufinfo_T *bi, visualinfo_T *info)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001478{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001479 serialize_pos(bi, info->vi_start);
1480 serialize_pos(bi, info->vi_end);
1481 undo_write_bytes(bi, (long_u)info->vi_mode, 4);
1482 undo_write_bytes(bi, (long_u)info->vi_curswant, 4);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001483}
1484
1485/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001486 * Unserialize the visualinfo_T at the current position.
Bram Moolenaar9db58062010-05-29 20:33:07 +02001487 */
1488 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001489unserialize_visualinfo(bufinfo_T *bi, visualinfo_T *info)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001490{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001491 unserialize_pos(bi, &info->vi_start);
1492 unserialize_pos(bi, &info->vi_end);
1493 info->vi_mode = undo_read_4c(bi);
1494 info->vi_curswant = undo_read_4c(bi);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001495}
1496
1497/*
1498 * Write the undo tree in an undo file.
1499 * When "name" is not NULL, use it as the name of the undo file.
1500 * Otherwise use buf->b_ffname to generate the undo file name.
1501 * "buf" must never be null, buf->b_ffname is used to obtain the original file
1502 * permissions.
1503 * "forceit" is TRUE for ":wundo!", FALSE otherwise.
1504 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text.
1505 */
1506 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001507u_write_undo(
1508 char_u *name,
1509 int forceit,
1510 buf_T *buf,
1511 char_u *hash)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001512{
1513 u_header_T *uhp;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001514 char_u *file_name;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001515 int mark;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001516#ifdef U_DEBUG
1517 int headers_written = 0;
1518#endif
1519 int fd;
1520 FILE *fp = NULL;
1521 int perm;
1522 int write_ok = FALSE;
1523#ifdef UNIX
1524 int st_old_valid = FALSE;
Bram Moolenaar8767f522016-07-01 17:17:39 +02001525 stat_T st_old;
1526 stat_T st_new;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001527#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001528 bufinfo_T bi;
1529
Bram Moolenaar5a669b92014-08-12 20:14:33 +02001530 vim_memset(&bi, 0, sizeof(bi));
Bram Moolenaar9db58062010-05-29 20:33:07 +02001531
1532 if (name == NULL)
1533 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001534 file_name = u_get_undo_file_name(buf->b_ffname, FALSE);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001535 if (file_name == NULL)
1536 {
1537 if (p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001538 {
1539 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001540 smsg(
Bram Moolenaar504a8212010-05-30 17:17:42 +02001541 _("Cannot write undo file in any directory in 'undodir'"));
1542 verbose_leave();
1543 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001544 return;
1545 }
1546 }
1547 else
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001548 file_name = name;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001549
1550 /*
1551 * Decide about the permission to use for the undo file. If the buffer
1552 * has a name use the permission of the original file. Otherwise only
1553 * allow the user to access the undo file.
1554 */
1555 perm = 0600;
1556 if (buf->b_ffname != NULL)
1557 {
1558#ifdef UNIX
1559 if (mch_stat((char *)buf->b_ffname, &st_old) >= 0)
1560 {
1561 perm = st_old.st_mode;
1562 st_old_valid = TRUE;
1563 }
1564#else
1565 perm = mch_getperm(buf->b_ffname);
1566 if (perm < 0)
1567 perm = 0600;
1568#endif
1569 }
1570
Bram Moolenaar3cbac302015-04-21 16:12:06 +02001571 /* strip any s-bit and executable bit */
1572 perm = perm & 0666;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001573
1574 /* If the undo file already exists, verify that it actually is an undo
1575 * file, and delete it. */
1576 if (mch_getperm(file_name) >= 0)
1577 {
1578 if (name == NULL || !forceit)
1579 {
1580 /* Check we can read it and it's an undo file. */
1581 fd = mch_open((char *)file_name, O_RDONLY|O_EXTRA, 0);
1582 if (fd < 0)
1583 {
1584 if (name != NULL || p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001585 {
1586 if (name == NULL)
1587 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001588 smsg(
Bram Moolenaar504a8212010-05-30 17:17:42 +02001589 _("Will not overwrite with undo file, cannot read: %s"),
Bram Moolenaar9db58062010-05-29 20:33:07 +02001590 file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001591 if (name == NULL)
1592 verbose_leave();
1593 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001594 goto theend;
1595 }
1596 else
1597 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02001598 char_u mbuf[UF_START_MAGIC_LEN];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001599 int len;
1600
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01001601 len = read_eintr(fd, mbuf, UF_START_MAGIC_LEN);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001602 close(fd);
1603 if (len < UF_START_MAGIC_LEN
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02001604 || memcmp(mbuf, UF_START_MAGIC, UF_START_MAGIC_LEN) != 0)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001605 {
1606 if (name != NULL || p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001607 {
1608 if (name == NULL)
1609 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001610 smsg(
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001611 _("Will not overwrite, this is not an undo file: %s"),
Bram Moolenaar9db58062010-05-29 20:33:07 +02001612 file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001613 if (name == NULL)
1614 verbose_leave();
1615 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001616 goto theend;
1617 }
1618 }
1619 }
1620 mch_remove(file_name);
1621 }
1622
Bram Moolenaar504a8212010-05-30 17:17:42 +02001623 /* If there is no undo information at all, quit here after deleting any
1624 * existing undo file. */
Bram Moolenaarccae4672019-01-04 15:09:57 +01001625 if (buf->b_u_numhead == 0 && buf->b_u_line_ptr.ul_line == NULL)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001626 {
1627 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001628 verb_msg(_("Skipping undo file write, nothing to undo"));
Bram Moolenaar504a8212010-05-30 17:17:42 +02001629 goto theend;
1630 }
1631
Bram Moolenaar9db58062010-05-29 20:33:07 +02001632 fd = mch_open((char *)file_name,
1633 O_CREAT|O_EXTRA|O_WRONLY|O_EXCL|O_NOFOLLOW, perm);
1634 if (fd < 0)
1635 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001636 semsg(_(e_not_open), file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001637 goto theend;
1638 }
1639 (void)mch_setperm(file_name, perm);
1640 if (p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001641 {
1642 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001643 smsg(_("Writing undo file: %s"), file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001644 verbose_leave();
1645 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001646
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001647#ifdef U_DEBUG
Bram Moolenaar504a8212010-05-30 17:17:42 +02001648 /* Check there is no problem in undo info before writing. */
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001649 u_check(FALSE);
1650#endif
1651
Bram Moolenaar9db58062010-05-29 20:33:07 +02001652#ifdef UNIX
1653 /*
1654 * Try to set the group of the undo file same as the original file. If
1655 * this fails, set the protection bits for the group same as the
1656 * protection bits for others.
1657 */
Bram Moolenaarce69e822010-07-21 20:31:07 +02001658 if (st_old_valid
1659 && mch_stat((char *)file_name, &st_new) >= 0
1660 && st_new.st_gid != st_old.st_gid
Bram Moolenaar9db58062010-05-29 20:33:07 +02001661# ifdef HAVE_FCHOWN /* sequent-ptx lacks fchown() */
Bram Moolenaarce69e822010-07-21 20:31:07 +02001662 && fchown(fd, (uid_t)-1, st_old.st_gid) != 0
Bram Moolenaar9db58062010-05-29 20:33:07 +02001663# endif
1664 )
1665 mch_setperm(file_name, (perm & 0707) | ((perm & 07) << 3));
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02001666# if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001667 if (buf->b_ffname != NULL)
1668 mch_copy_sec(buf->b_ffname, file_name);
1669# endif
1670#endif
1671
1672 fp = fdopen(fd, "w");
1673 if (fp == NULL)
1674 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001675 semsg(_(e_not_open), file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001676 close(fd);
1677 mch_remove(file_name);
1678 goto theend;
1679 }
1680
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001681 /* Undo must be synced. */
1682 u_sync(TRUE);
1683
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001684 /*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001685 * Write the header. Initializes encryption, if enabled.
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001686 */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001687 bi.bi_buf = buf;
1688 bi.bi_fp = fp;
1689 if (serialize_header(&bi, hash) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001690 goto write_error;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001691
1692 /*
1693 * Iteratively serialize UHPs and their UEPs from the top down.
1694 */
1695 mark = ++lastmark;
1696 uhp = buf->b_u_oldhead;
1697 while (uhp != NULL)
1698 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001699 /* Serialize current UHP if we haven't seen it */
1700 if (uhp->uh_walk != mark)
1701 {
1702 uhp->uh_walk = mark;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001703#ifdef U_DEBUG
1704 ++headers_written;
1705#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001706 if (serialize_uhp(&bi, uhp) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001707 goto write_error;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001708 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001709
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001710 /* Now walk through the tree - algorithm from undo_time(). */
1711 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != mark)
1712 uhp = uhp->uh_prev.ptr;
1713 else if (uhp->uh_alt_next.ptr != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001714 && uhp->uh_alt_next.ptr->uh_walk != mark)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001715 uhp = uhp->uh_alt_next.ptr;
1716 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001717 && uhp->uh_next.ptr->uh_walk != mark)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001718 uhp = uhp->uh_next.ptr;
1719 else if (uhp->uh_alt_prev.ptr != NULL)
1720 uhp = uhp->uh_alt_prev.ptr;
1721 else
1722 uhp = uhp->uh_next.ptr;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001723 }
1724
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001725 if (undo_write_bytes(&bi, (long_u)UF_HEADER_END_MAGIC, 2) == OK)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001726 write_ok = TRUE;
1727#ifdef U_DEBUG
1728 if (headers_written != buf->b_u_numhead)
Bram Moolenaar570064c2013-06-10 20:25:10 +02001729 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001730 semsg("Written %ld headers, ...", headers_written);
1731 semsg("... but numhead is %ld", buf->b_u_numhead);
Bram Moolenaar570064c2013-06-10 20:25:10 +02001732 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001733#endif
1734
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001735#ifdef FEAT_CRYPT
1736 if (bi.bi_state != NULL && undo_flush(&bi) == FAIL)
1737 write_ok = FALSE;
1738#endif
1739
Bram Moolenaar9db58062010-05-29 20:33:07 +02001740write_error:
1741 fclose(fp);
1742 if (!write_ok)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001743 semsg(_("E829: write error in undo file: %s"), file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001744
Bram Moolenaar4f974752019-02-17 17:44:42 +01001745#if defined(MSWIN)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001746 /* Copy file attributes; for systems where this can only be done after
1747 * closing the file. */
Bram Moolenaar9db58062010-05-29 20:33:07 +02001748 if (buf->b_ffname != NULL)
1749 (void)mch_copy_file_attribute(buf->b_ffname, file_name);
1750#endif
1751#ifdef HAVE_ACL
1752 if (buf->b_ffname != NULL)
1753 {
1754 vim_acl_T acl;
1755
1756 /* For systems that support ACL: get the ACL from the original file. */
1757 acl = mch_get_acl(buf->b_ffname);
1758 mch_set_acl(file_name, acl);
Bram Moolenaard2aed442012-06-01 13:46:12 +02001759 mch_free_acl(acl);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001760 }
1761#endif
1762
1763theend:
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001764#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001765 if (bi.bi_state != NULL)
1766 crypt_free_state(bi.bi_state);
1767 vim_free(bi.bi_buffer);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001768#endif
Bram Moolenaar9db58062010-05-29 20:33:07 +02001769 if (file_name != name)
1770 vim_free(file_name);
1771}
1772
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001773/*
1774 * Load the undo tree from an undo file.
1775 * If "name" is not NULL use it as the undo file name. This also means being
1776 * a bit more verbose.
1777 * Otherwise use curbuf->b_ffname to generate the undo file name.
1778 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text.
1779 */
1780 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001781u_read_undo(char_u *name, char_u *hash, char_u *orig_name)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001782{
1783 char_u *file_name;
1784 FILE *fp;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001785 long version, str_len;
Bram Moolenaarccae4672019-01-04 15:09:57 +01001786 undoline_T line_ptr;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001787 linenr_T line_lnum;
1788 colnr_T line_colnr;
1789 linenr_T line_count;
Bram Moolenaar3eb16372017-02-26 18:11:36 +01001790 long num_head = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001791 long old_header_seq, new_header_seq, cur_header_seq;
1792 long seq_last, seq_cur;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001793 long last_save_nr = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001794 short old_idx = -1, new_idx = -1, cur_idx = -1;
1795 long num_read_uhps = 0;
1796 time_t seq_time;
1797 int i, j;
1798 int c;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001799 u_header_T *uhp;
1800 u_header_T **uhp_table = NULL;
1801 char_u read_hash[UNDO_HASH_SIZE];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001802 char_u magic_buf[UF_START_MAGIC_LEN];
1803#ifdef U_DEBUG
1804 int *uhp_table_used;
1805#endif
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001806#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001807 stat_T st_orig;
1808 stat_T st_undo;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001809#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001810 bufinfo_T bi;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001811
Bram Moolenaar5a669b92014-08-12 20:14:33 +02001812 vim_memset(&bi, 0, sizeof(bi));
Bram Moolenaarccae4672019-01-04 15:09:57 +01001813 line_ptr.ul_len = 0;
1814 line_ptr.ul_line = NULL;
1815
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001816 if (name == NULL)
1817 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001818 file_name = u_get_undo_file_name(curbuf->b_ffname, TRUE);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001819 if (file_name == NULL)
1820 return;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001821
1822#ifdef UNIX
1823 /* For safety we only read an undo file if the owner is equal to the
Bram Moolenaar3b262392013-09-08 15:40:49 +02001824 * owner of the text file or equal to the current user. */
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001825 if (mch_stat((char *)orig_name, &st_orig) >= 0
1826 && mch_stat((char *)file_name, &st_undo) >= 0
Bram Moolenaar3b262392013-09-08 15:40:49 +02001827 && st_orig.st_uid != st_undo.st_uid
1828 && st_undo.st_uid != getuid())
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001829 {
1830 if (p_verbose > 0)
1831 {
1832 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001833 smsg(_("Not reading undo file, owner differs: %s"),
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001834 file_name);
1835 verbose_leave();
1836 }
1837 return;
1838 }
1839#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001840 }
1841 else
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001842 file_name = name;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001843
1844 if (p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001845 {
1846 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001847 smsg(_("Reading undo file: %s"), file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001848 verbose_leave();
1849 }
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001850
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001851 fp = mch_fopen((char *)file_name, "r");
1852 if (fp == NULL)
1853 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001854 if (name != NULL || p_verbose > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001855 semsg(_("E822: Cannot open undo file for reading: %s"), file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001856 goto error;
1857 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001858 bi.bi_buf = curbuf;
1859 bi.bi_fp = fp;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001860
Bram Moolenaar9db58062010-05-29 20:33:07 +02001861 /*
1862 * Read the undo file header.
1863 */
1864 if (fread(magic_buf, UF_START_MAGIC_LEN, 1, fp) != 1
1865 || memcmp(magic_buf, UF_START_MAGIC, UF_START_MAGIC_LEN) != 0)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001866 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001867 semsg(_("E823: Not an undo file: %s"), file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001868 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001869 }
1870 version = get2c(fp);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001871 if (version == UF_VERSION_CRYPT)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001872 {
1873#ifdef FEAT_CRYPT
Bram Moolenaar56be9502010-06-06 14:20:26 +02001874 if (*curbuf->b_p_key == NUL)
1875 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001876 semsg(_("E832: Non-encrypted file has encrypted undo file: %s"),
Bram Moolenaar56be9502010-06-06 14:20:26 +02001877 file_name);
1878 goto error;
1879 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001880 bi.bi_state = crypt_create_from_file(fp, curbuf->b_p_key);
1881 if (bi.bi_state == NULL)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001882 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001883 semsg(_("E826: Undo file decryption failed: %s"), file_name);
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001884 goto error;
1885 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001886 if (crypt_whole_undofile(bi.bi_state->method_nr))
1887 {
1888 bi.bi_buffer = alloc(CRYPT_BUF_SIZE);
1889 if (bi.bi_buffer == NULL)
1890 {
1891 crypt_free_state(bi.bi_state);
1892 bi.bi_state = NULL;
1893 goto error;
1894 }
1895 bi.bi_avail = 0;
1896 bi.bi_used = 0;
1897 }
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001898#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001899 semsg(_("E827: Undo file is encrypted: %s"), file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001900 goto error;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001901#endif
1902 }
Bram Moolenaar69154f22010-07-18 21:42:34 +02001903 else if (version != UF_VERSION)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001904 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001905 semsg(_("E824: Incompatible undo file: %s"), file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001906 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001907 }
1908
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001909 if (undo_read(&bi, read_hash, (size_t)UNDO_HASH_SIZE) == FAIL)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001910 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001911 corruption_error("hash", file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001912 goto error;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001913 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001914 line_count = (linenr_T)undo_read_4c(&bi);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001915 if (memcmp(hash, read_hash, UNDO_HASH_SIZE) != 0
1916 || line_count != curbuf->b_ml.ml_line_count)
1917 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001918 if (p_verbose > 0 || name != NULL)
1919 {
Bram Moolenaar504a8212010-05-30 17:17:42 +02001920 if (name == NULL)
1921 verbose_enter();
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001922 give_warning((char_u *)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001923 _("File contents changed, cannot use undo info"), TRUE);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001924 if (name == NULL)
1925 verbose_leave();
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001926 }
1927 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001928 }
1929
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001930 /* Read undo data for "U" command. */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001931 str_len = undo_read_4c(&bi);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001932 if (str_len < 0)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001933 goto error;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001934 if (str_len > 0)
Bram Moolenaarccae4672019-01-04 15:09:57 +01001935 {
1936 line_ptr.ul_line = read_string_decrypt(&bi, str_len);
1937 line_ptr.ul_len = str_len + 1;
1938 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001939 line_lnum = (linenr_T)undo_read_4c(&bi);
1940 line_colnr = (colnr_T)undo_read_4c(&bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001941 if (line_lnum < 0 || line_colnr < 0)
1942 {
1943 corruption_error("line lnum/col", file_name);
1944 goto error;
1945 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001946
1947 /* Begin general undo data */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001948 old_header_seq = undo_read_4c(&bi);
1949 new_header_seq = undo_read_4c(&bi);
1950 cur_header_seq = undo_read_4c(&bi);
1951 num_head = undo_read_4c(&bi);
1952 seq_last = undo_read_4c(&bi);
1953 seq_cur = undo_read_4c(&bi);
1954 seq_time = undo_read_time(&bi);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001955
Bram Moolenaar69154f22010-07-18 21:42:34 +02001956 /* Optional header fields. */
1957 for (;;)
1958 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001959 int len = undo_read_byte(&bi);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001960 int what;
Bram Moolenaara800b422010-06-27 01:15:55 +02001961
Bram Moolenaar69154f22010-07-18 21:42:34 +02001962 if (len == 0 || len == EOF)
1963 break;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001964 what = undo_read_byte(&bi);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001965 switch (what)
1966 {
1967 case UF_LAST_SAVE_NR:
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001968 last_save_nr = undo_read_4c(&bi);
Bram Moolenaara800b422010-06-27 01:15:55 +02001969 break;
Bram Moolenaar69154f22010-07-18 21:42:34 +02001970 default:
1971 /* field not supported, skip */
1972 while (--len >= 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001973 (void)undo_read_byte(&bi);
Bram Moolenaara800b422010-06-27 01:15:55 +02001974 }
Bram Moolenaar69154f22010-07-18 21:42:34 +02001975 }
Bram Moolenaara800b422010-06-27 01:15:55 +02001976
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001977 /* uhp_table will store the freshly created undo headers we allocate
1978 * until we insert them into curbuf. The table remains sorted by the
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001979 * sequence numbers of the headers.
1980 * When there are no headers uhp_table is NULL. */
1981 if (num_head > 0)
1982 {
Bram Moolenaar3eb16372017-02-26 18:11:36 +01001983 if (num_head < LONG_MAX / (long)sizeof(u_header_T *))
1984 uhp_table = (u_header_T **)U_ALLOC_LINE(
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001985 num_head * sizeof(u_header_T *));
1986 if (uhp_table == NULL)
1987 goto error;
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001988 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001989
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001990 while ((c = undo_read_2c(&bi)) == UF_HEADER_MAGIC)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001991 {
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001992 if (num_read_uhps >= num_head)
1993 {
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001994 corruption_error("num_head too small", file_name);
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001995 goto error;
1996 }
1997
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001998 uhp = unserialize_uhp(&bi, file_name);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001999 if (uhp == NULL)
2000 goto error;
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02002001 uhp_table[num_read_uhps++] = uhp;
2002 }
2003
2004 if (num_read_uhps != num_head)
2005 {
2006 corruption_error("num_head", file_name);
2007 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002008 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02002009 if (c != UF_HEADER_END_MAGIC)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002010 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02002011 corruption_error("end marker", file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002012 goto error;
2013 }
2014
Bram Moolenaar9db58062010-05-29 20:33:07 +02002015#ifdef U_DEBUG
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02002016 uhp_table_used = (int *)alloc_clear(sizeof(int) * num_head + 1);
Bram Moolenaar9db58062010-05-29 20:33:07 +02002017# define SET_FLAG(j) ++uhp_table_used[j]
2018#else
2019# define SET_FLAG(j)
2020#endif
2021
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002022 /* We have put all of the headers into a table. Now we iterate through the
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002023 * table and swizzle each sequence number we have stored in uh_*_seq into
2024 * a pointer corresponding to the header with that sequence number. */
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002025 for (i = 0; i < num_head; i++)
2026 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002027 uhp = uhp_table[i];
2028 if (uhp == NULL)
2029 continue;
2030 for (j = 0; j < num_head; j++)
2031 if (uhp_table[j] != NULL && i != j
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002032 && uhp_table[i]->uh_seq == uhp_table[j]->uh_seq)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002033 {
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02002034 corruption_error("duplicate uh_seq", file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002035 goto error;
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02002036 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002037 for (j = 0; j < num_head; j++)
2038 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002039 && uhp_table[j]->uh_seq == uhp->uh_next.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002040 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002041 uhp->uh_next.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02002042 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002043 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002044 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002045 for (j = 0; j < num_head; j++)
2046 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002047 && uhp_table[j]->uh_seq == uhp->uh_prev.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002048 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002049 uhp->uh_prev.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02002050 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002051 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002052 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002053 for (j = 0; j < num_head; j++)
2054 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002055 && uhp_table[j]->uh_seq == uhp->uh_alt_next.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002056 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002057 uhp->uh_alt_next.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02002058 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002059 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002060 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002061 for (j = 0; j < num_head; j++)
2062 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002063 && uhp_table[j]->uh_seq == uhp->uh_alt_prev.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002064 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002065 uhp->uh_alt_prev.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02002066 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002067 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002068 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002069 if (old_header_seq > 0 && old_idx < 0 && uhp->uh_seq == old_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002070 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002071 old_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002072 SET_FLAG(i);
2073 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002074 if (new_header_seq > 0 && new_idx < 0 && uhp->uh_seq == new_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002075 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002076 new_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002077 SET_FLAG(i);
2078 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002079 if (cur_header_seq > 0 && cur_idx < 0 && uhp->uh_seq == cur_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002080 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002081 cur_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002082 SET_FLAG(i);
2083 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002084 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02002085
2086 /* Now that we have read the undo info successfully, free the current undo
2087 * info and use the info from the file. */
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002088 u_blockfree(curbuf);
Bram Moolenaar9db58062010-05-29 20:33:07 +02002089 curbuf->b_u_oldhead = old_idx < 0 ? NULL : uhp_table[old_idx];
2090 curbuf->b_u_newhead = new_idx < 0 ? NULL : uhp_table[new_idx];
2091 curbuf->b_u_curhead = cur_idx < 0 ? NULL : uhp_table[cur_idx];
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002092 curbuf->b_u_line_ptr = line_ptr;
2093 curbuf->b_u_line_lnum = line_lnum;
2094 curbuf->b_u_line_colnr = line_colnr;
2095 curbuf->b_u_numhead = num_head;
2096 curbuf->b_u_seq_last = seq_last;
2097 curbuf->b_u_seq_cur = seq_cur;
Bram Moolenaara800b422010-06-27 01:15:55 +02002098 curbuf->b_u_time_cur = seq_time;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002099 curbuf->b_u_save_nr_last = last_save_nr;
Bram Moolenaardba01a02010-11-03 19:32:42 +01002100 curbuf->b_u_save_nr_cur = last_save_nr;
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02002101
2102 curbuf->b_u_synced = TRUE;
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002103 vim_free(uhp_table);
Bram Moolenaar9db58062010-05-29 20:33:07 +02002104
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002105#ifdef U_DEBUG
Bram Moolenaar9db58062010-05-29 20:33:07 +02002106 for (i = 0; i < num_head; ++i)
2107 if (uhp_table_used[i] == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002108 semsg("uhp_table entry %ld not used, leaking memory", i);
Bram Moolenaar9db58062010-05-29 20:33:07 +02002109 vim_free(uhp_table_used);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002110 u_check(TRUE);
2111#endif
Bram Moolenaar9db58062010-05-29 20:33:07 +02002112
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002113 if (name != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002114 smsg(_("Finished reading undo file %s"), file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002115 goto theend;
2116
2117error:
Bram Moolenaarccae4672019-01-04 15:09:57 +01002118 vim_free(line_ptr.ul_line);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002119 if (uhp_table != NULL)
2120 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002121 for (i = 0; i < num_read_uhps; i++)
2122 if (uhp_table[i] != NULL)
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02002123 u_free_uhp(uhp_table[i]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002124 vim_free(uhp_table);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002125 }
2126
2127theend:
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002128#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002129 if (bi.bi_state != NULL)
2130 crypt_free_state(bi.bi_state);
2131 vim_free(bi.bi_buffer);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002132#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002133 if (fp != NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002134 fclose(fp);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002135 if (file_name != name)
2136 vim_free(file_name);
2137 return;
2138}
2139
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002140#endif /* FEAT_PERSISTENT_UNDO */
2141
2142
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143/*
2144 * If 'cpoptions' contains 'u': Undo the previous undo or redo (vi compatible).
2145 * If 'cpoptions' does not contain 'u': Always undo.
2146 */
2147 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002148u_undo(int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149{
2150 /*
2151 * If we get an undo command while executing a macro, we behave like the
2152 * original vi. If this happens twice in one macro the result will not
2153 * be compatible.
2154 */
2155 if (curbuf->b_u_synced == FALSE)
2156 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002157 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 count = 1;
2159 }
2160
2161 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
2162 undo_undoes = TRUE;
2163 else
2164 undo_undoes = !undo_undoes;
2165 u_doit(count);
2166}
2167
2168/*
2169 * If 'cpoptions' contains 'u': Repeat the previous undo or redo.
2170 * If 'cpoptions' does not contain 'u': Always redo.
2171 */
2172 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002173u_redo(int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174{
2175 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
2176 undo_undoes = FALSE;
2177 u_doit(count);
2178}
2179
2180/*
2181 * Undo or redo, depending on 'undo_undoes', 'count' times.
2182 */
2183 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002184u_doit(int startcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185{
Bram Moolenaarca003e12006-03-17 23:19:38 +00002186 int count = startcount;
2187
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002188 if (!undo_allowed())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190
2191 u_newcount = 0;
2192 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002193 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2194 u_oldcount = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 while (count--)
2196 {
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002197 /* Do the change warning now, so that it triggers FileChangedRO when
2198 * needed. This may cause the file to be reloaded, that must happen
2199 * before we do anything, because it may change curbuf->b_u_curhead
2200 * and more. */
2201 change_warning(0);
2202
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 if (undo_undoes)
2204 {
2205 if (curbuf->b_u_curhead == NULL) /* first undo */
2206 curbuf->b_u_curhead = curbuf->b_u_newhead;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002207 else if (get_undolevel() > 0) /* multi level undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 /* get next undo */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002209 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_next.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 /* nothing to undo */
2211 if (curbuf->b_u_numhead == 0 || curbuf->b_u_curhead == NULL)
2212 {
2213 /* stick curbuf->b_u_curhead at end */
2214 curbuf->b_u_curhead = curbuf->b_u_oldhead;
2215 beep_flush();
Bram Moolenaarca003e12006-03-17 23:19:38 +00002216 if (count == startcount - 1)
2217 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002218 msg(_("Already at oldest change"));
Bram Moolenaarca003e12006-03-17 23:19:38 +00002219 return;
2220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 break;
2222 }
2223
Bram Moolenaarca003e12006-03-17 23:19:38 +00002224 u_undoredo(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 }
2226 else
2227 {
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002228 if (curbuf->b_u_curhead == NULL || get_undolevel() <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 {
2230 beep_flush(); /* nothing to redo */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002231 if (count == startcount - 1)
2232 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002233 msg(_("Already at newest change"));
Bram Moolenaarca003e12006-03-17 23:19:38 +00002234 return;
2235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 break;
2237 }
2238
Bram Moolenaarca003e12006-03-17 23:19:38 +00002239 u_undoredo(FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002240
2241 /* Advance for next redo. Set "newhead" when at the end of the
2242 * redoable changes. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002243 if (curbuf->b_u_curhead->uh_prev.ptr == NULL)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002244 curbuf->b_u_newhead = curbuf->b_u_curhead;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002245 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_prev.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 }
2247 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00002248 u_undo_end(undo_undoes, FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002249}
2250
Bram Moolenaar1e607892006-03-13 22:15:53 +00002251/*
2252 * Undo or redo over the timeline.
2253 * When "step" is negative go back in time, otherwise goes forward in time.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002254 * When "sec" is FALSE make "step" steps, when "sec" is TRUE use "step" as
2255 * seconds.
Bram Moolenaar730cde92010-06-27 05:18:54 +02002256 * When "file" is TRUE use "step" as a number of file writes.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002257 * When "absolute" is TRUE use "step" as the sequence number to jump to.
2258 * "sec" must be FALSE then.
Bram Moolenaar1e607892006-03-13 22:15:53 +00002259 */
2260 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002261undo_time(
2262 long step,
2263 int sec,
2264 int file,
2265 int absolute)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002266{
2267 long target;
2268 long closest;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002269 long closest_start;
2270 long closest_seq = 0;
2271 long val;
Bram Moolenaarce46d932018-01-30 22:46:06 +01002272 u_header_T *uhp = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002273 u_header_T *last;
2274 int mark;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02002275 int nomark = 0; // shut up compiler
Bram Moolenaar1e607892006-03-13 22:15:53 +00002276 int round;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002277 int dosec = sec;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002278 int dofile = file;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002279 int above = FALSE;
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002280 int did_undo = TRUE;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002281
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002282 /* First make sure the current undoable change is synced. */
2283 if (curbuf->b_u_synced == FALSE)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002284 u_sync(TRUE);
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002285
Bram Moolenaar1e607892006-03-13 22:15:53 +00002286 u_newcount = 0;
2287 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002288 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002289 u_oldcount = -1;
2290
Bram Moolenaarca003e12006-03-17 23:19:38 +00002291 /* "target" is the node below which we want to be.
2292 * Init "closest" to a value we can't reach. */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002293 if (absolute)
2294 {
Bram Moolenaarce46d932018-01-30 22:46:06 +01002295 target = step;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002296 closest = -1;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002297 }
Bram Moolenaarca003e12006-03-17 23:19:38 +00002298 else
Bram Moolenaar1e607892006-03-13 22:15:53 +00002299 {
Bram Moolenaar730cde92010-06-27 05:18:54 +02002300 if (dosec)
Bram Moolenaarcbd4de42017-01-07 16:14:57 +01002301 target = (long)(curbuf->b_u_time_cur) + step;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002302 else if (dofile)
2303 {
2304 if (step < 0)
2305 {
2306 /* Going back to a previous write. If there were changes after
2307 * the last write, count that as moving one file-write, so
2308 * that ":earlier 1f" undoes all changes since the last save. */
2309 uhp = curbuf->b_u_curhead;
2310 if (uhp != NULL)
2311 uhp = uhp->uh_next.ptr;
2312 else
2313 uhp = curbuf->b_u_newhead;
2314 if (uhp != NULL && uhp->uh_save_nr != 0)
2315 /* "uh_save_nr" was set in the last block, that means
2316 * there were no changes since the last write */
2317 target = curbuf->b_u_save_nr_cur + step;
2318 else
2319 /* count the changes since the last write as one step */
2320 target = curbuf->b_u_save_nr_cur + step + 1;
2321 if (target <= 0)
2322 /* Go to before first write: before the oldest change. Use
2323 * the sequence number for that. */
2324 dofile = FALSE;
2325 }
2326 else
2327 {
2328 /* Moving forward to a newer write. */
2329 target = curbuf->b_u_save_nr_cur + step;
2330 if (target > curbuf->b_u_save_nr_last)
2331 {
2332 /* Go to after last write: after the latest change. Use
2333 * the sequence number for that. */
2334 target = curbuf->b_u_seq_last + 1;
2335 dofile = FALSE;
2336 }
2337 }
2338 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002339 else
2340 target = curbuf->b_u_seq_cur + step;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002341 if (step < 0)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002342 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002343 if (target < 0)
2344 target = 0;
2345 closest = -1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002346 }
2347 else
2348 {
Bram Moolenaar730cde92010-06-27 05:18:54 +02002349 if (dosec)
Bram Moolenaarcbd4de42017-01-07 16:14:57 +01002350 closest = (long)(vim_time() + 1);
Bram Moolenaar730cde92010-06-27 05:18:54 +02002351 else if (dofile)
2352 closest = curbuf->b_u_save_nr_last + 2;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002353 else
2354 closest = curbuf->b_u_seq_last + 2;
2355 if (target >= closest)
2356 target = closest - 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002357 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002358 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002359 closest_start = closest;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002360 closest_seq = curbuf->b_u_seq_cur;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002361
Bram Moolenaarce46d932018-01-30 22:46:06 +01002362 /* When "target" is 0; Back to origin. */
2363 if (target == 0)
Bram Moolenaar059fd012018-01-31 14:25:53 +01002364 {
2365 mark = lastmark; /* avoid that GCC complains */
2366 goto target_zero;
2367 }
Bram Moolenaarce46d932018-01-30 22:46:06 +01002368
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002369 /*
2370 * May do this twice:
Bram Moolenaar1e607892006-03-13 22:15:53 +00002371 * 1. Search for "target", update "closest" to the best match found.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002372 * 2. If "target" not found search for "closest".
2373 *
2374 * When using the closest time we use the sequence number in the second
2375 * round, because there may be several entries with the same time.
2376 */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002377 for (round = 1; round <= 2; ++round)
2378 {
2379 /* Find the path from the current state to where we want to go. The
2380 * desired state can be anywhere in the undo tree, need to go all over
2381 * it. We put "nomark" in uh_walk where we have been without success,
2382 * "mark" where it could possibly be. */
2383 mark = ++lastmark;
2384 nomark = ++lastmark;
2385
2386 if (curbuf->b_u_curhead == NULL) /* at leaf of the tree */
2387 uhp = curbuf->b_u_newhead;
2388 else
2389 uhp = curbuf->b_u_curhead;
2390
2391 while (uhp != NULL)
2392 {
2393 uhp->uh_walk = mark;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002394 if (dosec)
Bram Moolenaarcbd4de42017-01-07 16:14:57 +01002395 val = (long)(uhp->uh_time);
Bram Moolenaar730cde92010-06-27 05:18:54 +02002396 else if (dofile)
2397 val = uhp->uh_save_nr;
2398 else
2399 val = uhp->uh_seq;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002400
Bram Moolenaar730cde92010-06-27 05:18:54 +02002401 if (round == 1 && !(dofile && val == 0))
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002402 {
2403 /* Remember the header that is closest to the target.
2404 * It must be at least in the right direction (checked with
Bram Moolenaarca003e12006-03-17 23:19:38 +00002405 * "b_u_seq_cur"). When the timestamp is equal find the
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002406 * highest/lowest sequence number. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002407 if ((step < 0 ? uhp->uh_seq <= curbuf->b_u_seq_cur
2408 : uhp->uh_seq > curbuf->b_u_seq_cur)
2409 && ((dosec && val == closest)
2410 ? (step < 0
2411 ? uhp->uh_seq < closest_seq
2412 : uhp->uh_seq > closest_seq)
2413 : closest == closest_start
2414 || (val > target
2415 ? (closest > target
2416 ? val - target <= closest - target
2417 : val - target <= target - closest)
2418 : (closest > target
2419 ? target - val <= closest - target
2420 : target - val <= target - closest))))
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002421 {
2422 closest = val;
2423 closest_seq = uhp->uh_seq;
2424 }
2425 }
2426
2427 /* Quit searching when we found a match. But when searching for a
2428 * time we need to continue looking for the best uh_seq. */
2429 if (target == val && !dosec)
Bram Moolenaar730cde92010-06-27 05:18:54 +02002430 {
2431 target = uhp->uh_seq;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002432 break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002433 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002434
2435 /* go down in the tree if we haven't been there */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002436 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != nomark
2437 && uhp->uh_prev.ptr->uh_walk != mark)
2438 uhp = uhp->uh_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002439
2440 /* go to alternate branch if we haven't been there */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002441 else if (uhp->uh_alt_next.ptr != NULL
2442 && uhp->uh_alt_next.ptr->uh_walk != nomark
2443 && uhp->uh_alt_next.ptr->uh_walk != mark)
2444 uhp = uhp->uh_alt_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002445
2446 /* go up in the tree if we haven't been there and we are at the
2447 * start of alternate branches */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002448 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
2449 && uhp->uh_next.ptr->uh_walk != nomark
2450 && uhp->uh_next.ptr->uh_walk != mark)
Bram Moolenaardb552d602006-03-23 22:59:57 +00002451 {
2452 /* If still at the start we don't go through this change. */
2453 if (uhp == curbuf->b_u_curhead)
2454 uhp->uh_walk = nomark;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002455 uhp = uhp->uh_next.ptr;
Bram Moolenaardb552d602006-03-23 22:59:57 +00002456 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002457
2458 else
2459 {
2460 /* need to backtrack; mark this node as useless */
2461 uhp->uh_walk = nomark;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002462 if (uhp->uh_alt_prev.ptr != NULL)
2463 uhp = uhp->uh_alt_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002464 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002465 uhp = uhp->uh_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002466 }
2467 }
2468
2469 if (uhp != NULL) /* found it */
2470 break;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002471
2472 if (absolute)
2473 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002474 semsg(_("E830: Undo number %ld not found"), step);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002475 return;
2476 }
2477
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002478 if (closest == closest_start)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002479 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002480 if (step < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002481 msg(_("Already at oldest change"));
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002482 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002483 msg(_("Already at newest change"));
Bram Moolenaar1e607892006-03-13 22:15:53 +00002484 return;
2485 }
2486
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002487 target = closest_seq;
2488 dosec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002489 dofile = FALSE;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002490 if (step < 0)
2491 above = TRUE; /* stop above the header */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002492 }
2493
Bram Moolenaar059fd012018-01-31 14:25:53 +01002494target_zero:
Bram Moolenaar1e607892006-03-13 22:15:53 +00002495 /* If we found it: Follow the path to go to where we want to be. */
Bram Moolenaarce46d932018-01-30 22:46:06 +01002496 if (uhp != NULL || target == 0)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002497 {
2498 /*
2499 * First go up the tree as much as needed.
2500 */
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002501 while (!got_int)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002502 {
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002503 /* Do the change warning now, for the same reason as above. */
2504 change_warning(0);
2505
Bram Moolenaar1e607892006-03-13 22:15:53 +00002506 uhp = curbuf->b_u_curhead;
2507 if (uhp == NULL)
2508 uhp = curbuf->b_u_newhead;
2509 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002510 uhp = uhp->uh_next.ptr;
Bram Moolenaarce46d932018-01-30 22:46:06 +01002511 if (uhp == NULL || (target > 0 && uhp->uh_walk != mark)
Bram Moolenaarca003e12006-03-17 23:19:38 +00002512 || (uhp->uh_seq == target && !above))
Bram Moolenaar1e607892006-03-13 22:15:53 +00002513 break;
2514 curbuf->b_u_curhead = uhp;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002515 u_undoredo(TRUE);
Bram Moolenaarce46d932018-01-30 22:46:06 +01002516 if (target > 0)
2517 uhp->uh_walk = nomark; /* don't go back down here */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002518 }
2519
Bram Moolenaarce46d932018-01-30 22:46:06 +01002520 /* When back to origin, redo is not needed. */
2521 if (target > 0)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002522 {
Bram Moolenaarce46d932018-01-30 22:46:06 +01002523 /*
2524 * And now go down the tree (redo), branching off where needed.
2525 */
2526 while (!got_int)
2527 {
2528 /* Do the change warning now, for the same reason as above. */
2529 change_warning(0);
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002530
Bram Moolenaarce46d932018-01-30 22:46:06 +01002531 uhp = curbuf->b_u_curhead;
2532 if (uhp == NULL)
2533 break;
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002534
Bram Moolenaarce46d932018-01-30 22:46:06 +01002535 /* Go back to the first branch with a mark. */
2536 while (uhp->uh_alt_prev.ptr != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002537 && uhp->uh_alt_prev.ptr->uh_walk == mark)
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002538 uhp = uhp->uh_alt_prev.ptr;
Bram Moolenaarce46d932018-01-30 22:46:06 +01002539
2540 /* Find the last branch with a mark, that's the one. */
2541 last = uhp;
2542 while (last->uh_alt_next.ptr != NULL
2543 && last->uh_alt_next.ptr->uh_walk == mark)
2544 last = last->uh_alt_next.ptr;
2545 if (last != uhp)
2546 {
2547 /* Make the used branch the first entry in the list of
2548 * alternatives to make "u" and CTRL-R take this branch. */
2549 while (uhp->uh_alt_prev.ptr != NULL)
2550 uhp = uhp->uh_alt_prev.ptr;
2551 if (last->uh_alt_next.ptr != NULL)
2552 last->uh_alt_next.ptr->uh_alt_prev.ptr =
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002553 last->uh_alt_prev.ptr;
Bram Moolenaarce46d932018-01-30 22:46:06 +01002554 last->uh_alt_prev.ptr->uh_alt_next.ptr =
2555 last->uh_alt_next.ptr;
2556 last->uh_alt_prev.ptr = NULL;
2557 last->uh_alt_next.ptr = uhp;
2558 uhp->uh_alt_prev.ptr = last;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002559
Bram Moolenaarce46d932018-01-30 22:46:06 +01002560 if (curbuf->b_u_oldhead == uhp)
2561 curbuf->b_u_oldhead = last;
2562 uhp = last;
2563 if (uhp->uh_next.ptr != NULL)
2564 uhp->uh_next.ptr->uh_prev.ptr = uhp;
2565 }
2566 curbuf->b_u_curhead = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002567
Bram Moolenaarce46d932018-01-30 22:46:06 +01002568 if (uhp->uh_walk != mark)
2569 break; /* must have reached the target */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002570
Bram Moolenaarce46d932018-01-30 22:46:06 +01002571 /* Stop when going backwards in time and didn't find the exact
2572 * header we were looking for. */
2573 if (uhp->uh_seq == target && above)
2574 {
2575 curbuf->b_u_seq_cur = target - 1;
2576 break;
2577 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002578
Bram Moolenaarce46d932018-01-30 22:46:06 +01002579 u_undoredo(FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002580
Bram Moolenaarce46d932018-01-30 22:46:06 +01002581 /* Advance "curhead" to below the header we last used. If it
2582 * becomes NULL then we need to set "newhead" to this leaf. */
2583 if (uhp->uh_prev.ptr == NULL)
2584 curbuf->b_u_newhead = uhp;
2585 curbuf->b_u_curhead = uhp->uh_prev.ptr;
2586 did_undo = FALSE;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002587
Bram Moolenaarce46d932018-01-30 22:46:06 +01002588 if (uhp->uh_seq == target) /* found it! */
2589 break;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002590
Bram Moolenaarce46d932018-01-30 22:46:06 +01002591 uhp = uhp->uh_prev.ptr;
2592 if (uhp == NULL || uhp->uh_walk != mark)
2593 {
2594 /* Need to redo more but can't find it... */
2595 internal_error("undo_time()");
2596 break;
2597 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002598 }
2599 }
2600 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00002601 u_undo_end(did_undo, absolute);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602}
2603
2604/*
2605 * u_undoredo: common code for undo and redo
2606 *
2607 * The lines in the file are replaced by the lines in the entry list at
2608 * curbuf->b_u_curhead. The replaced lines in the file are saved in the entry
2609 * list for the next undo/redo.
Bram Moolenaarca003e12006-03-17 23:19:38 +00002610 *
2611 * When "undo" is TRUE we go up in the tree, when FALSE we go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 */
2613 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002614u_undoredo(int undo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615{
Bram Moolenaarccae4672019-01-04 15:09:57 +01002616 undoline_T *newarray = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 linenr_T oldsize;
2618 linenr_T newsize;
2619 linenr_T top, bot;
2620 linenr_T lnum;
2621 linenr_T newlnum = MAXLNUM;
2622 long i;
2623 u_entry_T *uep, *nuep;
2624 u_entry_T *newlist = NULL;
2625 int old_flags;
2626 int new_flags;
2627 pos_T namedm[NMARKS];
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002628 visualinfo_T visualinfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 int empty_buffer; /* buffer became empty */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002630 u_header_T *curhead = curbuf->b_u_curhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002632 /* Don't want autocommands using the undo structures here, they are
2633 * invalid till the end. */
2634 block_autocmds();
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002635
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002636#ifdef U_DEBUG
2637 u_check(FALSE);
2638#endif
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002639 old_flags = curhead->uh_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 new_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
2641 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
2642 setpcmark();
2643
2644 /*
2645 * save marks before undo/redo
2646 */
2647 mch_memmove(namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002648 visualinfo = curbuf->b_visual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 curbuf->b_op_start.lnum = curbuf->b_ml.ml_line_count;
2650 curbuf->b_op_start.col = 0;
2651 curbuf->b_op_end.lnum = 0;
2652 curbuf->b_op_end.col = 0;
2653
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002654 for (uep = curhead->uh_entry; uep != NULL; uep = nuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 {
2656 top = uep->ue_top;
2657 bot = uep->ue_bot;
2658 if (bot == 0)
2659 bot = curbuf->b_ml.ml_line_count + 1;
2660 if (top > curbuf->b_ml.ml_line_count || top >= bot
2661 || bot > curbuf->b_ml.ml_line_count + 1)
2662 {
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002663 unblock_autocmds();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002664 iemsg(_("E438: u_undo: line numbers wrong"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 changed(); /* don't want UNCHANGED now */
2666 return;
2667 }
2668
2669 oldsize = bot - top - 1; /* number of lines before undo */
2670 newsize = uep->ue_size; /* number of lines after undo */
2671
2672 if (top < newlnum)
2673 {
2674 /* If the saved cursor is somewhere in this undo block, move it to
2675 * the remembered position. Makes "gwap" put the cursor back
2676 * where it was. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002677 lnum = curhead->uh_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 if (lnum >= top && lnum <= top + newsize + 1)
2679 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002680 curwin->w_cursor = curhead->uh_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 newlnum = curwin->w_cursor.lnum - 1;
2682 }
2683 else
2684 {
2685 /* Use the first line that actually changed. Avoids that
2686 * undoing auto-formatting puts the cursor in the previous
2687 * line. */
2688 for (i = 0; i < newsize && i < oldsize; ++i)
Bram Moolenaarccae4672019-01-04 15:09:57 +01002689 {
2690 char_u *p = ml_get(top + 1 + i);
2691
2692 if (curbuf->b_ml.ml_line_len != uep->ue_array[i].ul_len
Bram Moolenaar964b3742019-05-24 18:54:09 +02002693 || memcmp(uep->ue_array[i].ul_line, p,
2694 curbuf->b_ml.ml_line_len) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 break;
Bram Moolenaarccae4672019-01-04 15:09:57 +01002696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 if (i == newsize && newlnum == MAXLNUM && uep->ue_next == NULL)
2698 {
2699 newlnum = top;
2700 curwin->w_cursor.lnum = newlnum + 1;
2701 }
2702 else if (i < newsize)
2703 {
2704 newlnum = top + i;
2705 curwin->w_cursor.lnum = newlnum + 1;
2706 }
2707 }
2708 }
2709
2710 empty_buffer = FALSE;
2711
2712 /* delete the lines between top and bot and save them in newarray */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002713 if (oldsize > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 {
Bram Moolenaarccae4672019-01-04 15:09:57 +01002715 if ((newarray = (undoline_T *)U_ALLOC_LINE(
2716 sizeof(undoline_T) * oldsize)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 {
Bram Moolenaarccae4672019-01-04 15:09:57 +01002718 do_outofmem_msg((long_u)(sizeof(undoline_T) * oldsize));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 /*
2720 * We have messed up the entry list, repair is impossible.
2721 * we have to free the rest of the list.
2722 */
2723 while (uep != NULL)
2724 {
2725 nuep = uep->ue_next;
2726 u_freeentry(uep, uep->ue_size);
2727 uep = nuep;
2728 }
2729 break;
2730 }
2731 /* delete backwards, it goes faster in most cases */
2732 for (lnum = bot - 1, i = oldsize; --i >= 0; --lnum)
2733 {
2734 /* what can we do when we run out of memory? */
Bram Moolenaarccae4672019-01-04 15:09:57 +01002735 if (u_save_line(&newarray[i], lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 do_outofmem_msg((long_u)0);
2737 /* remember we deleted the last line in the buffer, and a
2738 * dummy empty line will be inserted */
2739 if (curbuf->b_ml.ml_line_count == 1)
2740 empty_buffer = TRUE;
2741 ml_delete(lnum, FALSE);
2742 }
2743 }
Bram Moolenaar8d343302005-07-12 22:46:17 +00002744 else
2745 newarray = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746
2747 /* insert the lines in u_array between top and bot */
2748 if (newsize)
2749 {
2750 for (lnum = top, i = 0; i < newsize; ++i, ++lnum)
2751 {
Bram Moolenaarccae4672019-01-04 15:09:57 +01002752 // If the file is empty, there is an empty line 1 that we
2753 // should get rid of, by replacing it with the new line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 if (empty_buffer && lnum == 0)
Bram Moolenaar964b3742019-05-24 18:54:09 +02002755 ml_replace_len((linenr_T)1, uep->ue_array[i].ul_line,
2756 uep->ue_array[i].ul_len, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 else
Bram Moolenaar964b3742019-05-24 18:54:09 +02002758 ml_append(lnum, uep->ue_array[i].ul_line,
2759 (colnr_T)uep->ue_array[i].ul_len, FALSE);
Bram Moolenaarccae4672019-01-04 15:09:57 +01002760 vim_free(uep->ue_array[i].ul_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 }
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002762 vim_free((char_u *)uep->ue_array);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 }
2764
2765 /* adjust marks */
2766 if (oldsize != newsize)
2767 {
2768 mark_adjust(top + 1, top + oldsize, (long)MAXLNUM,
2769 (long)newsize - (long)oldsize);
2770 if (curbuf->b_op_start.lnum > top + oldsize)
2771 curbuf->b_op_start.lnum += newsize - oldsize;
2772 if (curbuf->b_op_end.lnum > top + oldsize)
2773 curbuf->b_op_end.lnum += newsize - oldsize;
2774 }
2775
2776 changed_lines(top + 1, 0, bot, newsize - oldsize);
2777
2778 /* set '[ and '] mark */
2779 if (top + 1 < curbuf->b_op_start.lnum)
2780 curbuf->b_op_start.lnum = top + 1;
2781 if (newsize == 0 && top + 1 > curbuf->b_op_end.lnum)
2782 curbuf->b_op_end.lnum = top + 1;
2783 else if (top + newsize > curbuf->b_op_end.lnum)
2784 curbuf->b_op_end.lnum = top + newsize;
2785
2786 u_newcount += newsize;
2787 u_oldcount += oldsize;
2788 uep->ue_size = oldsize;
2789 uep->ue_array = newarray;
2790 uep->ue_bot = top + newsize + 1;
2791
2792 /*
2793 * insert this entry in front of the new entry list
2794 */
2795 nuep = uep->ue_next;
2796 uep->ue_next = newlist;
2797 newlist = uep;
2798 }
2799
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002800 curhead->uh_entry = newlist;
2801 curhead->uh_flags = new_flags;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002802 if ((old_flags & UH_EMPTYBUF) && BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 curbuf->b_ml.ml_flags |= ML_EMPTY;
2804 if (old_flags & UH_CHANGED)
2805 changed();
2806 else
Bram Moolenaar009b2592004-10-24 19:18:58 +00002807#ifdef FEAT_NETBEANS_INTG
2808 /* per netbeans undo rules, keep it as modified */
2809 if (!isNetbeansModified(curbuf))
2810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 unchanged(curbuf, FALSE);
2812
2813 /*
2814 * restore marks from before undo/redo
2815 */
2816 for (i = 0; i < NMARKS; ++i)
Bram Moolenaarf65aad52015-02-17 13:43:40 +01002817 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002818 if (curhead->uh_namedm[i].lnum != 0)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002819 curbuf->b_namedm[i] = curhead->uh_namedm[i];
Bram Moolenaarf65aad52015-02-17 13:43:40 +01002820 if (namedm[i].lnum != 0)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002821 curhead->uh_namedm[i] = namedm[i];
Bram Moolenaarf65aad52015-02-17 13:43:40 +01002822 else
2823 curhead->uh_namedm[i].lnum = 0;
2824 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002825 if (curhead->uh_visual.vi_start.lnum != 0)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002826 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002827 curbuf->b_visual = curhead->uh_visual;
2828 curhead->uh_visual = visualinfo;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830
2831 /*
2832 * If the cursor is only off by one line, put it at the same position as
2833 * before starting the change (for the "o" command).
2834 * Otherwise the cursor should go to the first undone line.
2835 */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002836 if (curhead->uh_cursor.lnum + 1 == curwin->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 && curwin->w_cursor.lnum > 1)
2838 --curwin->w_cursor.lnum;
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002839 if (curwin->w_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 {
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002841 if (curhead->uh_cursor.lnum == curwin->w_cursor.lnum)
2842 {
2843 curwin->w_cursor.col = curhead->uh_cursor.col;
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002844 if (virtual_active() && curhead->uh_cursor_vcol >= 0)
2845 coladvance((colnr_T)curhead->uh_cursor_vcol);
2846 else
2847 curwin->w_cursor.coladd = 0;
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002848 }
2849 else
2850 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 else
2853 {
2854 /* We get here with the current cursor line being past the end (eg
2855 * after adding lines at the end of the file, and then undoing it).
2856 * check_cursor() will move the cursor to the last line. Move it to
2857 * the first column here. */
2858 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 }
2861
2862 /* Make sure the cursor is on an existing line and column. */
2863 check_cursor();
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002864
2865 /* Remember where we are for "g-" and ":earlier 10s". */
2866 curbuf->b_u_seq_cur = curhead->uh_seq;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002867 if (undo)
Bram Moolenaar80eaddd2017-11-11 23:37:08 +01002868 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002869 /* We are below the previous undo. However, to make ":earlier 1s"
2870 * work we compute this as being just above the just undone change. */
Bram Moolenaar80eaddd2017-11-11 23:37:08 +01002871 if (curhead->uh_next.ptr != NULL)
2872 curbuf->b_u_seq_cur = curhead->uh_next.ptr->uh_seq;
2873 else
2874 curbuf->b_u_seq_cur = 0;
2875 }
Bram Moolenaarca003e12006-03-17 23:19:38 +00002876
Bram Moolenaar730cde92010-06-27 05:18:54 +02002877 /* Remember where we are for ":earlier 1f" and ":later 1f". */
2878 if (curhead->uh_save_nr != 0)
2879 {
2880 if (undo)
2881 curbuf->b_u_save_nr_cur = curhead->uh_save_nr - 1;
2882 else
2883 curbuf->b_u_save_nr_cur = curhead->uh_save_nr;
2884 }
2885
Bram Moolenaarca003e12006-03-17 23:19:38 +00002886 /* The timestamp can be the same for multiple changes, just use the one of
2887 * the undone/redone change. */
Bram Moolenaara800b422010-06-27 01:15:55 +02002888 curbuf->b_u_time_cur = curhead->uh_time;
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002889
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002890 unblock_autocmds();
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002891#ifdef U_DEBUG
2892 u_check(FALSE);
2893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894}
2895
2896/*
2897 * If we deleted or added lines, report the number of less/more lines.
2898 * Otherwise, report the number of changes (this may be incorrect
2899 * in some cases, but it's better than nothing).
2900 */
2901 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002902u_undo_end(
2903 int did_undo, /* just did an undo */
2904 int absolute) /* used ":undo N" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905{
Bram Moolenaar89d40322006-08-29 15:30:07 +00002906 char *msgstr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002907 u_header_T *uhp;
2908 char_u msgbuf[80];
Bram Moolenaar1e607892006-03-13 22:15:53 +00002909
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910#ifdef FEAT_FOLDING
2911 if ((fdo_flags & FDO_UNDO) && KeyTyped)
2912 foldOpenCursor();
2913#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +00002914
2915 if (global_busy /* no messages now, wait until global is finished */
2916 || !messaging()) /* 'lazyredraw' set, don't do messages now */
2917 return;
2918
2919 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2920 --u_newcount;
2921
2922 u_oldcount -= u_newcount;
2923 if (u_oldcount == -1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002924 msgstr = N_("more line");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002925 else if (u_oldcount < 0)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002926 msgstr = N_("more lines");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002927 else if (u_oldcount == 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002928 msgstr = N_("line less");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002929 else if (u_oldcount > 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002930 msgstr = N_("fewer lines");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002931 else
2932 {
2933 u_oldcount = u_newcount;
2934 if (u_newcount == 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002935 msgstr = N_("change");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002936 else
Bram Moolenaar89d40322006-08-29 15:30:07 +00002937 msgstr = N_("changes");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002938 }
2939
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002940 if (curbuf->b_u_curhead != NULL)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002941 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00002942 /* For ":undo N" we prefer a "after #N" message. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002943 if (absolute && curbuf->b_u_curhead->uh_next.ptr != NULL)
Bram Moolenaardb552d602006-03-23 22:59:57 +00002944 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002945 uhp = curbuf->b_u_curhead->uh_next.ptr;
Bram Moolenaardb552d602006-03-23 22:59:57 +00002946 did_undo = FALSE;
2947 }
2948 else if (did_undo)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002949 uhp = curbuf->b_u_curhead;
2950 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002951 uhp = curbuf->b_u_curhead->uh_next.ptr;
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002952 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002953 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002954 uhp = curbuf->b_u_newhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002955
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002956 if (uhp == NULL)
2957 *msgbuf = NUL;
2958 else
2959 u_add_time(msgbuf, sizeof(msgbuf), uhp->uh_time);
2960
Bram Moolenaarb2c03502010-07-02 20:20:09 +02002961#ifdef FEAT_CONCEAL
2962 {
2963 win_T *wp;
2964
2965 FOR_ALL_WINDOWS(wp)
2966 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02002967 if (wp->w_buffer == curbuf && wp->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02002968 redraw_win_later(wp, NOT_VALID);
2969 }
2970 }
2971#endif
2972
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002973 smsg_attr_keep(0, _("%ld %s; %s #%ld %s"),
Bram Moolenaarca003e12006-03-17 23:19:38 +00002974 u_oldcount < 0 ? -u_oldcount : u_oldcount,
Bram Moolenaar89d40322006-08-29 15:30:07 +00002975 _(msgstr),
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002976 did_undo ? _("before") : _("after"),
2977 uhp == NULL ? 0L : uhp->uh_seq,
2978 msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979}
2980
2981/*
2982 * u_sync: stop adding to the current entry list
2983 */
2984 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002985u_sync(
2986 int force) /* Also sync when no_u_sync is set. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987{
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002988 /* Skip it when already synced or syncing is disabled. */
2989 if (curbuf->b_u_synced || (!force && no_u_sync > 0))
2990 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02002992 if (p_imst == IM_ON_THE_SPOT && im_is_preediting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 return; /* XIM is busy, don't break an undo sequence */
2994#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002995 if (get_undolevel() < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 curbuf->b_u_synced = TRUE; /* no entries, nothing to do */
2997 else
2998 {
2999 u_getbot(); /* compute ue_bot of previous u_save */
3000 curbuf->b_u_curhead = NULL;
3001 }
3002}
3003
3004/*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003005 * ":undolist": List the leafs of the undo tree
3006 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003007 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003008ex_undolist(exarg_T *eap UNUSED)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003009{
3010 garray_T ga;
3011 u_header_T *uhp;
3012 int mark;
3013 int nomark;
3014 int changes = 1;
3015 int i;
3016
3017 /*
3018 * 1: walk the tree to find all leafs, put the info in "ga".
3019 * 2: sort the lines
3020 * 3: display the list
3021 */
3022 mark = ++lastmark;
3023 nomark = ++lastmark;
3024 ga_init2(&ga, (int)sizeof(char *), 20);
3025
3026 uhp = curbuf->b_u_oldhead;
3027 while (uhp != NULL)
3028 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003029 if (uhp->uh_prev.ptr == NULL && uhp->uh_walk != nomark
Bram Moolenaarca003e12006-03-17 23:19:38 +00003030 && uhp->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003031 {
3032 if (ga_grow(&ga, 1) == FAIL)
3033 break;
Bram Moolenaarea391762018-04-08 13:07:22 +02003034 vim_snprintf((char *)IObuff, IOSIZE, "%6ld %7d ",
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003035 uhp->uh_seq, changes);
3036 u_add_time(IObuff + STRLEN(IObuff), IOSIZE - STRLEN(IObuff),
3037 uhp->uh_time);
Bram Moolenaara800b422010-06-27 01:15:55 +02003038 if (uhp->uh_save_nr > 0)
3039 {
Bram Moolenaardba01a02010-11-03 19:32:42 +01003040 while (STRLEN(IObuff) < 33)
Bram Moolenaara800b422010-06-27 01:15:55 +02003041 STRCAT(IObuff, " ");
3042 vim_snprintf_add((char *)IObuff, IOSIZE,
3043 " %3ld", uhp->uh_save_nr);
3044 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003045 ((char_u **)(ga.ga_data))[ga.ga_len++] = vim_strsave(IObuff);
3046 }
3047
3048 uhp->uh_walk = mark;
3049
3050 /* go down in the tree if we haven't been there */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003051 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != nomark
3052 && uhp->uh_prev.ptr->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003053 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003054 uhp = uhp->uh_prev.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003055 ++changes;
3056 }
3057
3058 /* go to alternate branch if we haven't been there */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003059 else if (uhp->uh_alt_next.ptr != NULL
3060 && uhp->uh_alt_next.ptr->uh_walk != nomark
3061 && uhp->uh_alt_next.ptr->uh_walk != mark)
3062 uhp = uhp->uh_alt_next.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003063
3064 /* go up in the tree if we haven't been there and we are at the
3065 * start of alternate branches */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003066 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
3067 && uhp->uh_next.ptr->uh_walk != nomark
3068 && uhp->uh_next.ptr->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003069 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003070 uhp = uhp->uh_next.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003071 --changes;
3072 }
3073
3074 else
3075 {
3076 /* need to backtrack; mark this node as done */
3077 uhp->uh_walk = nomark;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003078 if (uhp->uh_alt_prev.ptr != NULL)
3079 uhp = uhp->uh_alt_prev.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003080 else
3081 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003082 uhp = uhp->uh_next.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003083 --changes;
3084 }
3085 }
3086 }
3087
3088 if (ga.ga_len == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003089 msg(_("Nothing to undo"));
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003090 else
3091 {
3092 sort_strings((char_u **)ga.ga_data, ga.ga_len);
3093
3094 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01003095 msg_puts_attr(_("number changes when saved"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01003096 HL_ATTR(HLF_T));
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003097 for (i = 0; i < ga.ga_len && !got_int; ++i)
3098 {
3099 msg_putchar('\n');
3100 if (got_int)
3101 break;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003102 msg_puts(((char **)ga.ga_data)[i]);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003103 }
3104 msg_end();
3105
3106 ga_clear_strings(&ga);
3107 }
3108}
3109
3110/*
3111 * Put the timestamp of an undo header in "buf[buflen]" in a nice format.
3112 */
3113 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003114u_add_time(char_u *buf, size_t buflen, time_t tt)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003115{
3116#ifdef HAVE_STRFTIME
Bram Moolenaar63d25552019-05-10 21:28:38 +02003117# ifdef HAVE_LOCALTIME_R
3118 struct tm tmval;
3119# endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003120 struct tm *curtime;
3121
Bram Moolenaar170b10b2016-07-29 16:15:27 +02003122 if (vim_time() - tt >= 100)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003123 {
Bram Moolenaar63d25552019-05-10 21:28:38 +02003124# ifdef HAVE_LOCALTIME_R
3125 curtime = localtime_r(&tt, &tmval);
3126# else
3127 curtime = localtime(&tt);
3128# endif
Bram Moolenaar170b10b2016-07-29 16:15:27 +02003129 if (vim_time() - tt < (60L * 60L * 12L))
Bram Moolenaardba01a02010-11-03 19:32:42 +01003130 /* within 12 hours */
3131 (void)strftime((char *)buf, buflen, "%H:%M:%S", curtime);
Bram Moolenaardba01a02010-11-03 19:32:42 +01003132 else
Bram Moolenaarb9ce83e2012-08-23 12:59:02 +02003133 /* longer ago */
Bram Moolenaar5e3d6ca2011-01-22 21:25:11 +01003134 (void)strftime((char *)buf, buflen, "%Y/%m/%d %H:%M:%S", curtime);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003135 }
3136 else
3137#endif
Bram Moolenaarfd6100b2018-08-21 17:07:45 +02003138 {
3139 long seconds = (long)(vim_time() - tt);
3140
3141 vim_snprintf((char *)buf, buflen,
3142 NGETTEXT("%ld second ago", "%ld seconds ago", seconds),
3143 seconds);
3144 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003145}
3146
3147/*
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003148 * ":undojoin": continue adding to the last entry list
3149 */
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003150 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003151ex_undojoin(exarg_T *eap UNUSED)
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003152{
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003153 if (curbuf->b_u_newhead == NULL)
3154 return; /* nothing changed before */
Bram Moolenaar57657d82006-04-21 22:12:41 +00003155 if (curbuf->b_u_curhead != NULL)
3156 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003157 emsg(_("E790: undojoin is not allowed after undo"));
Bram Moolenaar57657d82006-04-21 22:12:41 +00003158 return;
3159 }
3160 if (!curbuf->b_u_synced)
3161 return; /* already unsynced */
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003162 if (get_undolevel() < 0)
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003163 return; /* no entries, nothing to do */
3164 else
Bram Moolenaar5e4e1b12017-01-17 22:09:45 +01003165 /* Append next change to the last entry */
3166 curbuf->b_u_synced = FALSE;
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003167}
3168
3169/*
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003170 * Called after writing or reloading the file and setting b_changed to FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 * Now an undo means that the buffer is modified.
3172 */
3173 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003174u_unchanged(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175{
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003176 u_unch_branch(buf->b_u_oldhead);
3177 buf->b_did_warn = FALSE;
3178}
3179
Bram Moolenaar730cde92010-06-27 05:18:54 +02003180/*
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003181 * After reloading a buffer which was saved for 'undoreload': Find the first
3182 * line that was changed and set the cursor there.
3183 */
3184 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003185u_find_first_changed(void)
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003186{
3187 u_header_T *uhp = curbuf->b_u_newhead;
3188 u_entry_T *uep;
3189 linenr_T lnum;
3190
3191 if (curbuf->b_u_curhead != NULL || uhp == NULL)
3192 return; /* undid something in an autocmd? */
3193
3194 /* Check that the last undo block was for the whole file. */
3195 uep = uhp->uh_entry;
3196 if (uep->ue_top != 0 || uep->ue_bot != 0)
3197 return;
3198
3199 for (lnum = 1; lnum < curbuf->b_ml.ml_line_count
3200 && lnum <= uep->ue_size; ++lnum)
Bram Moolenaarccae4672019-01-04 15:09:57 +01003201 {
3202 char_u *p = ml_get_buf(curbuf, lnum, FALSE);
3203
3204 if (uep->ue_array[lnum - 1].ul_len != curbuf->b_ml.ml_line_len
3205 || memcmp(p, uep->ue_array[lnum - 1].ul_line, uep->ue_array[lnum - 1].ul_len) != 0)
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003206 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003207 CLEAR_POS(&(uhp->uh_cursor));
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003208 uhp->uh_cursor.lnum = lnum;
3209 return;
3210 }
Bram Moolenaarccae4672019-01-04 15:09:57 +01003211 }
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003212 if (curbuf->b_ml.ml_line_count != uep->ue_size)
3213 {
3214 /* lines added or deleted at the end, put the cursor there */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003215 CLEAR_POS(&(uhp->uh_cursor));
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003216 uhp->uh_cursor.lnum = lnum;
3217 }
3218}
3219
3220/*
Bram Moolenaar730cde92010-06-27 05:18:54 +02003221 * Increase the write count, store it in the last undo header, what would be
3222 * used for "u".
3223 */
3224 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003225u_update_save_nr(buf_T *buf)
Bram Moolenaar730cde92010-06-27 05:18:54 +02003226{
3227 u_header_T *uhp;
3228
3229 ++buf->b_u_save_nr_last;
3230 buf->b_u_save_nr_cur = buf->b_u_save_nr_last;
3231 uhp = buf->b_u_curhead;
3232 if (uhp != NULL)
3233 uhp = uhp->uh_next.ptr;
3234 else
3235 uhp = buf->b_u_newhead;
3236 if (uhp != NULL)
3237 uhp->uh_save_nr = buf->b_u_save_nr_last;
3238}
3239
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003240 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003241u_unch_branch(u_header_T *uhp)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003242{
Bram Moolenaar1e607892006-03-13 22:15:53 +00003243 u_header_T *uh;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003245 for (uh = uhp; uh != NULL; uh = uh->uh_prev.ptr)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003246 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 uh->uh_flags |= UH_CHANGED;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003248 if (uh->uh_alt_next.ptr != NULL)
3249 u_unch_branch(uh->uh_alt_next.ptr); /* recursive */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251}
3252
3253/*
3254 * Get pointer to last added entry.
3255 * If it's not valid, give an error message and return NULL.
3256 */
3257 static u_entry_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003258u_get_headentry(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259{
3260 if (curbuf->b_u_newhead == NULL || curbuf->b_u_newhead->uh_entry == NULL)
3261 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003262 iemsg(_("E439: undo list corrupt"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 return NULL;
3264 }
3265 return curbuf->b_u_newhead->uh_entry;
3266}
3267
3268/*
3269 * u_getbot(): compute the line number of the previous u_save
3270 * It is called only when b_u_synced is FALSE.
3271 */
3272 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003273u_getbot(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274{
3275 u_entry_T *uep;
3276 linenr_T extra;
3277
3278 uep = u_get_headentry(); /* check for corrupt undo list */
3279 if (uep == NULL)
3280 return;
3281
3282 uep = curbuf->b_u_newhead->uh_getbot_entry;
3283 if (uep != NULL)
3284 {
3285 /*
3286 * the new ue_bot is computed from the number of lines that has been
3287 * inserted (0 - deleted) since calling u_save. This is equal to the
3288 * old line count subtracted from the current line count.
3289 */
3290 extra = curbuf->b_ml.ml_line_count - uep->ue_lcount;
3291 uep->ue_bot = uep->ue_top + uep->ue_size + 1 + extra;
3292 if (uep->ue_bot < 1 || uep->ue_bot > curbuf->b_ml.ml_line_count)
3293 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003294 iemsg(_("E440: undo line missing"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 uep->ue_bot = uep->ue_top + 1; /* assume all lines deleted, will
3296 * get all the old lines back
3297 * without deleting the current
3298 * ones */
3299 }
3300
3301 curbuf->b_u_newhead->uh_getbot_entry = NULL;
3302 }
3303
3304 curbuf->b_u_synced = TRUE;
3305}
3306
3307/*
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003308 * Free one header "uhp" and its entry list and adjust the pointers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 */
3310 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003311u_freeheader(
3312 buf_T *buf,
3313 u_header_T *uhp,
3314 u_header_T **uhpp) /* if not NULL reset when freeing this header */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315{
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003316 u_header_T *uhap;
3317
Bram Moolenaar1e607892006-03-13 22:15:53 +00003318 /* When there is an alternate redo list free that branch completely,
3319 * because we can never go there. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003320 if (uhp->uh_alt_next.ptr != NULL)
3321 u_freebranch(buf, uhp->uh_alt_next.ptr, uhpp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003323 if (uhp->uh_alt_prev.ptr != NULL)
3324 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325
Bram Moolenaar1e607892006-03-13 22:15:53 +00003326 /* Update the links in the list to remove the header. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003327 if (uhp->uh_next.ptr == NULL)
3328 buf->b_u_oldhead = uhp->uh_prev.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003330 uhp->uh_next.ptr->uh_prev.ptr = uhp->uh_prev.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003332 if (uhp->uh_prev.ptr == NULL)
3333 buf->b_u_newhead = uhp->uh_next.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003335 for (uhap = uhp->uh_prev.ptr; uhap != NULL;
3336 uhap = uhap->uh_alt_next.ptr)
3337 uhap->uh_next.ptr = uhp->uh_next.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338
Bram Moolenaar1e607892006-03-13 22:15:53 +00003339 u_freeentries(buf, uhp, uhpp);
3340}
3341
3342/*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003343 * Free an alternate branch and any following alternate branches.
Bram Moolenaar1e607892006-03-13 22:15:53 +00003344 */
3345 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003346u_freebranch(
3347 buf_T *buf,
3348 u_header_T *uhp,
3349 u_header_T **uhpp) /* if not NULL reset when freeing this header */
Bram Moolenaar1e607892006-03-13 22:15:53 +00003350{
3351 u_header_T *tofree, *next;
3352
Bram Moolenaar07d06772007-11-10 21:51:15 +00003353 /* If this is the top branch we may need to use u_freeheader() to update
3354 * all the pointers. */
3355 if (uhp == buf->b_u_oldhead)
3356 {
Bram Moolenaaraa887322013-11-07 03:04:11 +01003357 while (buf->b_u_oldhead != NULL)
3358 u_freeheader(buf, buf->b_u_oldhead, uhpp);
Bram Moolenaar07d06772007-11-10 21:51:15 +00003359 return;
3360 }
3361
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003362 if (uhp->uh_alt_prev.ptr != NULL)
3363 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +00003364
3365 next = uhp;
3366 while (next != NULL)
3367 {
3368 tofree = next;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003369 if (tofree->uh_alt_next.ptr != NULL)
3370 u_freebranch(buf, tofree->uh_alt_next.ptr, uhpp); /* recursive */
3371 next = tofree->uh_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00003372 u_freeentries(buf, tofree, uhpp);
3373 }
3374}
3375
3376/*
3377 * Free all the undo entries for one header and the header itself.
3378 * This means that "uhp" is invalid when returning.
3379 */
3380 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003381u_freeentries(
3382 buf_T *buf,
3383 u_header_T *uhp,
3384 u_header_T **uhpp) /* if not NULL reset when freeing this header */
Bram Moolenaar1e607892006-03-13 22:15:53 +00003385{
3386 u_entry_T *uep, *nuep;
3387
3388 /* Check for pointers to the header that become invalid now. */
3389 if (buf->b_u_curhead == uhp)
3390 buf->b_u_curhead = NULL;
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003391 if (buf->b_u_newhead == uhp)
3392 buf->b_u_newhead = NULL; /* freeing the newest entry */
Bram Moolenaar1e607892006-03-13 22:15:53 +00003393 if (uhpp != NULL && uhp == *uhpp)
3394 *uhpp = NULL;
3395
3396 for (uep = uhp->uh_entry; uep != NULL; uep = nuep)
3397 {
3398 nuep = uep->ue_next;
3399 u_freeentry(uep, uep->ue_size);
3400 }
3401
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003402#ifdef U_DEBUG
3403 uhp->uh_magic = 0;
3404#endif
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003405 vim_free((char_u *)uhp);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003406 --buf->b_u_numhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407}
3408
3409/*
3410 * free entry 'uep' and 'n' lines in uep->ue_array[]
3411 */
3412 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003413u_freeentry(u_entry_T *uep, long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414{
Bram Moolenaar8d343302005-07-12 22:46:17 +00003415 while (n > 0)
Bram Moolenaarccae4672019-01-04 15:09:57 +01003416 vim_free(uep->ue_array[--n].ul_line);
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003417 vim_free((char_u *)uep->ue_array);
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003418#ifdef U_DEBUG
3419 uep->ue_magic = 0;
3420#endif
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003421 vim_free((char_u *)uep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422}
3423
3424/*
3425 * invalidate the undo buffer; called when storage has already been released
3426 */
3427 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003428u_clearall(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429{
3430 buf->b_u_newhead = buf->b_u_oldhead = buf->b_u_curhead = NULL;
3431 buf->b_u_synced = TRUE;
3432 buf->b_u_numhead = 0;
Bram Moolenaarccae4672019-01-04 15:09:57 +01003433 buf->b_u_line_ptr.ul_line = NULL;
3434 buf->b_u_line_ptr.ul_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 buf->b_u_line_lnum = 0;
3436}
3437
3438/*
Bram Moolenaarccae4672019-01-04 15:09:57 +01003439 * Save the line "lnum" for the "U" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 */
3441 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003442u_saveline(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443{
3444 if (lnum == curbuf->b_u_line_lnum) /* line is already saved */
3445 return;
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00003446 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) /* should never happen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 return;
3448 u_clearline();
3449 curbuf->b_u_line_lnum = lnum;
3450 if (curwin->w_cursor.lnum == lnum)
3451 curbuf->b_u_line_colnr = curwin->w_cursor.col;
3452 else
3453 curbuf->b_u_line_colnr = 0;
Bram Moolenaarccae4672019-01-04 15:09:57 +01003454 if (u_save_line(&curbuf->b_u_line_ptr, lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 do_outofmem_msg((long_u)0);
3456}
3457
3458/*
3459 * clear the line saved for the "U" command
3460 * (this is used externally for crossing a line while in insert mode)
3461 */
3462 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003463u_clearline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464{
Bram Moolenaarccae4672019-01-04 15:09:57 +01003465 if (curbuf->b_u_line_ptr.ul_line != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 {
Bram Moolenaarccae4672019-01-04 15:09:57 +01003467 VIM_CLEAR(curbuf->b_u_line_ptr.ul_line);
3468 curbuf->b_u_line_ptr.ul_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 curbuf->b_u_line_lnum = 0;
3470 }
3471}
3472
3473/*
3474 * Implementation of the "U" command.
3475 * Differentiation from vi: "U" can be undone with the next "U".
3476 * We also allow the cursor to be in another line.
Bram Moolenaard04b7502010-07-08 22:27:55 +02003477 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 */
3479 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003480u_undoline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481{
Bram Moolenaarccae4672019-01-04 15:09:57 +01003482 colnr_T t;
3483 undoline_T oldp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484
3485 if (undo_off)
3486 return;
3487
Bram Moolenaarccae4672019-01-04 15:09:57 +01003488 if (curbuf->b_u_line_ptr.ul_line == NULL
Bram Moolenaare3300c82008-02-13 14:21:38 +00003489 || curbuf->b_u_line_lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 {
3491 beep_flush();
3492 return;
3493 }
Bram Moolenaare3300c82008-02-13 14:21:38 +00003494
Bram Moolenaarccae4672019-01-04 15:09:57 +01003495 // first save the line for the 'u' command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 if (u_savecommon(curbuf->b_u_line_lnum - 1,
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003497 curbuf->b_u_line_lnum + 1, (linenr_T)0, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 return;
Bram Moolenaarccae4672019-01-04 15:09:57 +01003499 if (u_save_line(&oldp, curbuf->b_u_line_lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 {
3501 do_outofmem_msg((long_u)0);
3502 return;
3503 }
Bram Moolenaarccae4672019-01-04 15:09:57 +01003504 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 +00003505 changed_bytes(curbuf->b_u_line_lnum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 curbuf->b_u_line_ptr = oldp;
3507
3508 t = curbuf->b_u_line_colnr;
3509 if (curwin->w_cursor.lnum == curbuf->b_u_line_lnum)
3510 curbuf->b_u_line_colnr = curwin->w_cursor.col;
3511 curwin->w_cursor.col = t;
3512 curwin->w_cursor.lnum = curbuf->b_u_line_lnum;
Bram Moolenaare3300c82008-02-13 14:21:38 +00003513 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514}
3515
3516/*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003517 * Free all allocated memory blocks for the buffer 'buf'.
3518 */
3519 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003520u_blockfree(buf_T *buf)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003521{
Bram Moolenaar1e607892006-03-13 22:15:53 +00003522 while (buf->b_u_oldhead != NULL)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003523 u_freeheader(buf, buf->b_u_oldhead, NULL);
Bram Moolenaarccae4672019-01-04 15:09:57 +01003524 vim_free(buf->b_u_line_ptr.ul_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525}
3526
3527/*
3528 * Check if the 'modified' flag is set, or 'ff' has changed (only need to
3529 * check the first character, because it can only be "dos", "unix" or "mac").
3530 * "nofile" and "scratch" type buffers are considered to always be unchanged.
Bram Moolenaarf405c8f2017-12-09 19:51:49 +01003531 * Also considers a buffer changed when a terminal window contains a running
3532 * job.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 */
3534 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003535bufIsChanged(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536{
Bram Moolenaareb44a682017-08-03 22:44:55 +02003537#ifdef FEAT_TERMINAL
3538 if (term_job_running(buf->b_term))
3539 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540#endif
Bram Moolenaarf405c8f2017-12-09 19:51:49 +01003541 return bufIsChangedNotTerm(buf);
3542}
3543
3544/*
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01003545 * Return TRUE if any buffer has changes. Also buffers that are not written.
3546 */
3547 int
3548anyBufIsChanged(void)
3549{
3550 buf_T *buf;
3551
3552 FOR_ALL_BUFFERS(buf)
3553 if (bufIsChanged(buf))
3554 return TRUE;
Bram Moolenaard6c3f1f2019-03-26 00:31:21 +01003555 return FALSE;
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01003556}
3557
3558/*
Bram Moolenaarf405c8f2017-12-09 19:51:49 +01003559 * Like bufIsChanged() but ignoring a terminal window.
3560 */
3561 int
3562bufIsChangedNotTerm(buf_T *buf)
3563{
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003564 // In a "prompt" buffer we do respect 'modified', so that we can control
3565 // closing the window by setting or resetting that option.
3566 return (!bt_dontwrite(buf) || bt_prompt(buf))
Bram Moolenaareb44a682017-08-03 22:44:55 +02003567 && (buf->b_changed || file_ff_differs(buf, TRUE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568}
3569
3570 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003571curbufIsChanged(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572{
Bram Moolenaareb44a682017-08-03 22:44:55 +02003573 return bufIsChanged(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574}
Bram Moolenaara800b422010-06-27 01:15:55 +02003575
3576#if defined(FEAT_EVAL) || defined(PROTO)
3577/*
3578 * For undotree(): Append the list of undo blocks at "first_uhp" to "list".
3579 * Recursive.
3580 */
3581 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003582u_eval_tree(u_header_T *first_uhp, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02003583{
3584 u_header_T *uhp = first_uhp;
3585 dict_T *dict;
3586
3587 while (uhp != NULL)
3588 {
3589 dict = dict_alloc();
3590 if (dict == NULL)
3591 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003592 dict_add_number(dict, "seq", uhp->uh_seq);
3593 dict_add_number(dict, "time", (long)uhp->uh_time);
Bram Moolenaara800b422010-06-27 01:15:55 +02003594 if (uhp == curbuf->b_u_newhead)
Bram Moolenaare0be1672018-07-08 16:50:37 +02003595 dict_add_number(dict, "newhead", 1);
Bram Moolenaara800b422010-06-27 01:15:55 +02003596 if (uhp == curbuf->b_u_curhead)
Bram Moolenaare0be1672018-07-08 16:50:37 +02003597 dict_add_number(dict, "curhead", 1);
Bram Moolenaara800b422010-06-27 01:15:55 +02003598 if (uhp->uh_save_nr > 0)
Bram Moolenaare0be1672018-07-08 16:50:37 +02003599 dict_add_number(dict, "save", uhp->uh_save_nr);
Bram Moolenaara800b422010-06-27 01:15:55 +02003600
3601 if (uhp->uh_alt_next.ptr != NULL)
3602 {
3603 list_T *alt_list = list_alloc();
3604
3605 if (alt_list != NULL)
3606 {
3607 /* Recursive call to add alternate undo tree. */
3608 u_eval_tree(uhp->uh_alt_next.ptr, alt_list);
3609 dict_add_list(dict, "alt", alt_list);
3610 }
3611 }
3612
3613 list_append_dict(list, dict);
3614 uhp = uhp->uh_prev.ptr;
3615 }
3616}
3617#endif