blob: 4a4a33cc1b3675fc63b205072bfe4ebf431d97d6 [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 Moolenaarc799fe22019-05-28 23:08:19 +0200471 uhp = 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 Moolenaarc799fe22019-05-28 23:08:19 +0200662 uep = 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 Moolenaarc799fe22019-05-28 23:08:19 +0200688 if ((uep->ue_array = U_ALLOC_LINE(sizeof(undoline_T) * size)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 {
690 u_freeentry(uep, 0L);
691 goto nomem;
692 }
693 for (i = 0, lnum = top + 1; i < size; ++i)
694 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000695 fast_breakcheck();
696 if (got_int)
697 {
698 u_freeentry(uep, i);
699 return FAIL;
700 }
Bram Moolenaarccae4672019-01-04 15:09:57 +0100701 if (u_save_line(&uep->ue_array[i], lnum++) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 {
703 u_freeentry(uep, i);
704 goto nomem;
705 }
706 }
707 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000708 else
709 uep->ue_array = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 uep->ue_next = curbuf->b_u_newhead->uh_entry;
711 curbuf->b_u_newhead->uh_entry = uep;
712 curbuf->b_u_synced = FALSE;
713 undo_undoes = FALSE;
714
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000715#ifdef U_DEBUG
716 u_check(FALSE);
717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 return OK;
719
720nomem:
721 msg_silent = 0; /* must display the prompt */
722 if (ask_yesno((char_u *)_("No undo possible; continue anyway"), TRUE)
723 == 'y')
724 {
725 undo_off = TRUE; /* will be reset when character typed */
726 return OK;
727 }
728 do_outofmem_msg((long_u)0);
729 return FAIL;
730}
731
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200732#if defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200733
Bram Moolenaar9db58062010-05-29 20:33:07 +0200734# define UF_START_MAGIC "Vim\237UnDo\345" /* magic at start of undofile */
735# define UF_START_MAGIC_LEN 9
736# define UF_HEADER_MAGIC 0x5fd0 /* magic at start of header */
737# define UF_HEADER_END_MAGIC 0xe7aa /* magic after last header */
738# define UF_ENTRY_MAGIC 0xf518 /* magic at start of entry */
739# define UF_ENTRY_END_MAGIC 0x3581 /* magic after last entry */
Bram Moolenaara800b422010-06-27 01:15:55 +0200740# define UF_VERSION 2 /* 2-byte undofile version number */
Bram Moolenaara800b422010-06-27 01:15:55 +0200741# define UF_VERSION_CRYPT 0x8002 /* idem, encrypted */
742
743/* extra fields for header */
744# define UF_LAST_SAVE_NR 1
745
746/* extra fields for uhp */
747# define UHP_SAVE_NR 1
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200748
Bram Moolenaarcdf04202010-05-29 15:11:47 +0200749static char_u e_not_open[] = N_("E828: Cannot open undo file for writing: %s");
Bram Moolenaarcdf04202010-05-29 15:11:47 +0200750
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200751/*
752 * Compute the hash for the current buffer text into hash[UNDO_HASH_SIZE].
753 */
754 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100755u_compute_hash(char_u *hash)
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200756{
757 context_sha256_T ctx;
758 linenr_T lnum;
759 char_u *p;
760
761 sha256_start(&ctx);
Bram Moolenaarae7ba982011-12-08 15:14:09 +0100762 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200763 {
764 p = ml_get(lnum);
Bram Moolenaar442b4222010-05-24 21:34:22 +0200765 sha256_update(&ctx, p, (UINT32_T)(STRLEN(p) + 1));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200766 }
767 sha256_finish(&ctx, hash);
768}
769
770/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200771 * Return an allocated string of the full path of the target undofile.
772 * When "reading" is TRUE find the file to read, go over all directories in
773 * 'undodir'.
774 * When "reading" is FALSE use the first name where the directory exists.
Bram Moolenaar9db58062010-05-29 20:33:07 +0200775 * Returns NULL when there is no place to write or no file to read.
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200776 */
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200777 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100778u_get_undo_file_name(char_u *buf_ffname, int reading)
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200779{
780 char_u *dirp;
781 char_u dir_name[IOSIZE + 1];
782 char_u *munged_name = NULL;
783 char_u *undo_file_name = NULL;
784 int dir_len;
785 char_u *p;
Bram Moolenaar8767f522016-07-01 17:17:39 +0200786 stat_T st;
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200787 char_u *ffname = buf_ffname;
788#ifdef HAVE_READLINK
789 char_u fname_buf[MAXPATHL];
790#endif
791
792 if (ffname == NULL)
793 return NULL;
794
795#ifdef HAVE_READLINK
796 /* Expand symlink in the file name, so that we put the undo file with the
797 * actual file instead of with the symlink. */
798 if (resolve_symlink(ffname, fname_buf) == OK)
799 ffname = fname_buf;
800#endif
801
802 /* Loop over 'undodir'. When reading find the first file that exists.
803 * When not reading use the first directory that exists or ".". */
804 dirp = p_udir;
805 while (*dirp != NUL)
806 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200807 dir_len = copy_option_part(&dirp, dir_name, IOSIZE, ",");
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200808 if (dir_len == 1 && dir_name[0] == '.')
809 {
810 /* Use same directory as the ffname,
811 * "dir/name" -> "dir/.name.un~" */
Bram Moolenaar442b4222010-05-24 21:34:22 +0200812 undo_file_name = vim_strnsave(ffname, (int)(STRLEN(ffname) + 5));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200813 if (undo_file_name == NULL)
814 break;
815 p = gettail(undo_file_name);
Bram Moolenaar206f0112014-03-12 16:51:55 +0100816#ifdef VMS
817 /* VMS can not handle more than one dot in the filenames
818 * use "dir/name" -> "dir/_un_name" - add _un_
819 * at the beginning to keep the extension */
820 mch_memmove(p + 4, p, STRLEN(p) + 1);
821 mch_memmove(p, "_un_", 4);
822
823#else
824 /* Use same directory as the ffname,
825 * "dir/name" -> "dir/.name.un~" */
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200826 mch_memmove(p + 1, p, STRLEN(p) + 1);
827 *p = '.';
828 STRCAT(p, ".un~");
Bram Moolenaar206f0112014-03-12 16:51:55 +0100829#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200830 }
831 else
832 {
833 dir_name[dir_len] = NUL;
834 if (mch_isdir(dir_name))
835 {
836 if (munged_name == NULL)
837 {
838 munged_name = vim_strsave(ffname);
839 if (munged_name == NULL)
840 return NULL;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100841 for (p = munged_name; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200842 if (vim_ispathsep(*p))
843 *p = '%';
844 }
845 undo_file_name = concat_fnames(dir_name, munged_name, TRUE);
846 }
847 }
848
849 /* When reading check if the file exists. */
850 if (undo_file_name != NULL && (!reading
851 || mch_stat((char *)undo_file_name, &st) >= 0))
852 break;
Bram Moolenaard23a8232018-02-10 18:45:26 +0100853 VIM_CLEAR(undo_file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200854 }
855
856 vim_free(munged_name);
857 return undo_file_name;
858}
859
Bram Moolenaar9db58062010-05-29 20:33:07 +0200860 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100861corruption_error(char *mesg, char_u *file_name)
Bram Moolenaar9db58062010-05-29 20:33:07 +0200862{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100863 semsg(_("E825: Corrupted undo file (%s): %s"), mesg, file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +0200864}
865
866 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100867u_free_uhp(u_header_T *uhp)
Bram Moolenaar9db58062010-05-29 20:33:07 +0200868{
869 u_entry_T *nuep;
870 u_entry_T *uep;
871
872 uep = uhp->uh_entry;
873 while (uep != NULL)
874 {
875 nuep = uep->ue_next;
876 u_freeentry(uep, uep->ue_size);
877 uep = nuep;
878 }
879 vim_free(uhp);
880}
881
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200882/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200883 * Write a sequence of bytes to the undo file.
884 * Buffers and encrypts as needed.
885 * Returns OK or FAIL.
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200886 */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200887 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100888undo_write(bufinfo_T *bi, char_u *ptr, size_t len)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200889{
890#ifdef FEAT_CRYPT
891 if (bi->bi_buffer != NULL)
892 {
893 size_t len_todo = len;
894 char_u *p = ptr;
895
896 while (bi->bi_used + len_todo >= CRYPT_BUF_SIZE)
897 {
898 size_t n = CRYPT_BUF_SIZE - bi->bi_used;
899
900 mch_memmove(bi->bi_buffer + bi->bi_used, p, n);
901 len_todo -= n;
902 p += n;
903 bi->bi_used = CRYPT_BUF_SIZE;
904 if (undo_flush(bi) == FAIL)
905 return FAIL;
906 }
907 if (len_todo > 0)
908 {
909 mch_memmove(bi->bi_buffer + bi->bi_used, p, len_todo);
910 bi->bi_used += len_todo;
911 }
912 return OK;
913 }
914#endif
915 if (fwrite(ptr, len, (size_t)1, bi->bi_fp) != 1)
916 return FAIL;
917 return OK;
918}
919
920#ifdef FEAT_CRYPT
921 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100922undo_flush(bufinfo_T *bi)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200923{
Bram Moolenaar829aa642017-08-23 22:32:35 +0200924 if (bi->bi_buffer != NULL && bi->bi_state != NULL && bi->bi_used > 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200925 {
926 crypt_encode_inplace(bi->bi_state, bi->bi_buffer, bi->bi_used);
927 if (fwrite(bi->bi_buffer, bi->bi_used, (size_t)1, bi->bi_fp) != 1)
928 return FAIL;
929 bi->bi_used = 0;
930 }
931 return OK;
932}
933#endif
934
935/*
936 * Write "ptr[len]" and crypt the bytes when needed.
937 * Returns OK or FAIL.
938 */
939 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100940fwrite_crypt(bufinfo_T *bi, char_u *ptr, size_t len)
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200941{
942#ifdef FEAT_CRYPT
943 char_u *copy;
944 char_u small_buf[100];
945 size_t i;
946
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200947 if (bi->bi_state != NULL && bi->bi_buffer == NULL)
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200948 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200949 /* crypting every piece of text separately */
950 if (len < 100)
951 copy = small_buf; /* no malloc()/free() for short strings */
952 else
953 {
954 copy = lalloc(len, FALSE);
955 if (copy == NULL)
956 return 0;
957 }
958 crypt_encode(bi->bi_state, ptr, len, copy);
959 i = fwrite(copy, len, (size_t)1, bi->bi_fp);
960 if (copy != small_buf)
961 vim_free(copy);
962 return i == 1 ? OK : FAIL;
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200963 }
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200964#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200965 return undo_write(bi, ptr, len);
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200966}
967
968/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200969 * Write a number, MSB first, in "len" bytes.
970 * Must match with undo_read_?c() functions.
971 * Returns OK or FAIL.
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200972 */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200973 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100974undo_write_bytes(bufinfo_T *bi, long_u nr, int len)
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200975{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200976 char_u buf[8];
977 int i;
978 int bufi = 0;
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200979
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200980 for (i = len - 1; i >= 0; --i)
Bram Moolenaar9b8f0212014-08-13 22:05:53 +0200981 buf[bufi++] = (char_u)(nr >> (i * 8));
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200982 return undo_write(bi, buf, (size_t)len);
983}
984
985/*
986 * Write the pointer to an undo header. Instead of writing the pointer itself
987 * we use the sequence number of the header. This is converted back to
988 * pointers when reading. */
989 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100990put_header_ptr(bufinfo_T *bi, u_header_T *uhp)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200991{
992 undo_write_bytes(bi, (long_u)(uhp != NULL ? uhp->uh_seq : 0), 4);
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200993}
994
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +0200995 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100996undo_read_4c(bufinfo_T *bi)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200997{
998#ifdef FEAT_CRYPT
999 if (bi->bi_buffer != NULL)
1000 {
1001 char_u buf[4];
1002 int n;
1003
1004 undo_read(bi, buf, (size_t)4);
Bram Moolenaar35169282014-09-11 22:50:09 +02001005 n = ((unsigned)buf[0] << 24) + (buf[1] << 16) + (buf[2] << 8) + buf[3];
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001006 return n;
1007 }
1008#endif
1009 return get4c(bi->bi_fp);
1010}
1011
1012 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001013undo_read_2c(bufinfo_T *bi)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001014{
1015#ifdef FEAT_CRYPT
1016 if (bi->bi_buffer != NULL)
1017 {
1018 char_u buf[2];
1019 int n;
1020
1021 undo_read(bi, buf, (size_t)2);
1022 n = (buf[0] << 8) + buf[1];
1023 return n;
1024 }
1025#endif
1026 return get2c(bi->bi_fp);
1027}
1028
1029 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001030undo_read_byte(bufinfo_T *bi)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001031{
1032#ifdef FEAT_CRYPT
1033 if (bi->bi_buffer != NULL)
1034 {
1035 char_u buf[1];
1036
1037 undo_read(bi, buf, (size_t)1);
1038 return buf[0];
1039 }
1040#endif
1041 return getc(bi->bi_fp);
1042}
1043
1044 static time_t
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001045undo_read_time(bufinfo_T *bi)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001046{
1047#ifdef FEAT_CRYPT
1048 if (bi->bi_buffer != NULL)
1049 {
1050 char_u buf[8];
1051 time_t n = 0;
1052 int i;
1053
1054 undo_read(bi, buf, (size_t)8);
1055 for (i = 0; i < 8; ++i)
1056 n = (n << 8) + buf[i];
1057 return n;
1058 }
1059#endif
1060 return get8ctime(bi->bi_fp);
1061}
1062
1063/*
1064 * Read "buffer[size]" from the undo file.
1065 * Return OK or FAIL.
1066 */
1067 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001068undo_read(bufinfo_T *bi, char_u *buffer, size_t size)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001069{
Bram Moolenaar56f2db52017-06-11 23:09:15 +02001070 int retval = OK;
1071
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001072#ifdef FEAT_CRYPT
1073 if (bi->bi_buffer != NULL)
1074 {
Bram Moolenaar9b8f0212014-08-13 22:05:53 +02001075 int size_todo = (int)size;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001076 char_u *p = buffer;
1077
1078 while (size_todo > 0)
1079 {
1080 size_t n;
1081
1082 if (bi->bi_used >= bi->bi_avail)
1083 {
1084 n = fread(bi->bi_buffer, 1, (size_t)CRYPT_BUF_SIZE, bi->bi_fp);
Bram Moolenaar55952d42016-11-05 14:58:34 +01001085 if (n == 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001086 {
Bram Moolenaar56f2db52017-06-11 23:09:15 +02001087 retval = FAIL;
1088 break;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001089 }
1090 bi->bi_avail = n;
1091 bi->bi_used = 0;
1092 crypt_decode_inplace(bi->bi_state, bi->bi_buffer, bi->bi_avail);
1093 }
1094 n = size_todo;
1095 if (n > bi->bi_avail - bi->bi_used)
1096 n = bi->bi_avail - bi->bi_used;
1097 mch_memmove(p, bi->bi_buffer + bi->bi_used, n);
1098 bi->bi_used += n;
Bram Moolenaar9b8f0212014-08-13 22:05:53 +02001099 size_todo -= (int)n;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001100 p += n;
1101 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001102 }
Bram Moolenaar56f2db52017-06-11 23:09:15 +02001103 else
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001104#endif
1105 if (fread(buffer, (size_t)size, 1, bi->bi_fp) != 1)
Bram Moolenaar56f2db52017-06-11 23:09:15 +02001106 retval = FAIL;
1107
1108 if (retval == FAIL)
1109 /* Error may be checked for only later. Fill with zeros,
1110 * so that the reader won't use garbage. */
1111 vim_memset(buffer, 0, size);
1112 return retval;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001113}
1114
1115/*
1116 * Read a string of length "len" from "bi->bi_fd".
1117 * "len" can be zero to allocate an empty line.
1118 * Decrypt the bytes if needed.
1119 * Append a NUL.
1120 * Returns a pointer to allocated memory or NULL for failure.
1121 */
1122 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001123read_string_decrypt(bufinfo_T *bi, int len)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001124{
Bram Moolenaar964b3742019-05-24 18:54:09 +02001125 char_u *ptr = alloc(len + 1);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001126
1127 if (ptr != NULL)
1128 {
1129 if (len > 0 && undo_read(bi, ptr, len) == FAIL)
1130 {
1131 vim_free(ptr);
1132 return NULL;
1133 }
Bram Moolenaarccae4672019-01-04 15:09:57 +01001134 // In case there are text properties there already is a NUL, but
1135 // checking for that is more expensive than just adding a dummy byte.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001136 ptr[len] = NUL;
1137#ifdef FEAT_CRYPT
1138 if (bi->bi_state != NULL && bi->bi_buffer == NULL)
1139 crypt_decode_inplace(bi->bi_state, ptr, len);
1140#endif
1141 }
1142 return ptr;
1143}
1144
1145/*
1146 * Writes the (not encrypted) header and initializes encryption if needed.
1147 */
1148 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001149serialize_header(bufinfo_T *bi, char_u *hash)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001150{
Bram Moolenaarccae4672019-01-04 15:09:57 +01001151 long len;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001152 buf_T *buf = bi->bi_buf;
1153 FILE *fp = bi->bi_fp;
1154 char_u time_buf[8];
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001155
1156 /* Start writing, first the magic marker and undo info version. */
1157 if (fwrite(UF_START_MAGIC, (size_t)UF_START_MAGIC_LEN, (size_t)1, fp) != 1)
1158 return FAIL;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001159
1160 /* If the buffer is encrypted then all text bytes following will be
1161 * encrypted. Numbers and other info is not crypted. */
1162#ifdef FEAT_CRYPT
Bram Moolenaarfa0ff9a2010-07-25 16:05:19 +02001163 if (*buf->b_p_key != NUL)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001164 {
1165 char_u *header;
1166 int header_len;
1167
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001168 undo_write_bytes(bi, (long_u)UF_VERSION_CRYPT, 2);
1169 bi->bi_state = crypt_create_for_writing(crypt_get_method_nr(buf),
1170 buf->b_p_key, &header, &header_len);
1171 if (bi->bi_state == NULL)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001172 return FAIL;
Bram Moolenaarccae4672019-01-04 15:09:57 +01001173 len = (long)fwrite(header, (size_t)header_len, (size_t)1, fp);
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001174 vim_free(header);
1175 if (len != 1)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001176 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001177 crypt_free_state(bi->bi_state);
1178 bi->bi_state = NULL;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001179 return FAIL;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001180 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001181
1182 if (crypt_whole_undofile(crypt_get_method_nr(buf)))
1183 {
1184 bi->bi_buffer = alloc(CRYPT_BUF_SIZE);
1185 if (bi->bi_buffer == NULL)
1186 {
1187 crypt_free_state(bi->bi_state);
1188 bi->bi_state = NULL;
1189 return FAIL;
1190 }
1191 bi->bi_used = 0;
1192 }
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001193 }
1194 else
1195#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001196 undo_write_bytes(bi, (long_u)UF_VERSION, 2);
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001197
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001198
1199 /* Write a hash of the buffer text, so that we can verify it is still the
1200 * same when reading the buffer text. */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001201 if (undo_write(bi, hash, (size_t)UNDO_HASH_SIZE) == FAIL)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001202 return FAIL;
1203
1204 /* buffer-specific data */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001205 undo_write_bytes(bi, (long_u)buf->b_ml.ml_line_count, 4);
Bram Moolenaarccae4672019-01-04 15:09:57 +01001206 len = buf->b_u_line_ptr.ul_line == NULL
Bram Moolenaar8aef43b2019-01-08 20:14:35 +01001207 ? 0L : (long)STRLEN(buf->b_u_line_ptr.ul_line);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001208 undo_write_bytes(bi, (long_u)len, 4);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +01001209 if (len > 0 && fwrite_crypt(bi, buf->b_u_line_ptr.ul_line, (size_t)len)
1210 == FAIL)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001211 return FAIL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001212 undo_write_bytes(bi, (long_u)buf->b_u_line_lnum, 4);
1213 undo_write_bytes(bi, (long_u)buf->b_u_line_colnr, 4);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001214
1215 /* Undo structures header data */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001216 put_header_ptr(bi, buf->b_u_oldhead);
1217 put_header_ptr(bi, buf->b_u_newhead);
1218 put_header_ptr(bi, buf->b_u_curhead);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001219
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001220 undo_write_bytes(bi, (long_u)buf->b_u_numhead, 4);
1221 undo_write_bytes(bi, (long_u)buf->b_u_seq_last, 4);
1222 undo_write_bytes(bi, (long_u)buf->b_u_seq_cur, 4);
1223 time_to_bytes(buf->b_u_time_cur, time_buf);
1224 undo_write(bi, time_buf, 8);
Bram Moolenaara800b422010-06-27 01:15:55 +02001225
1226 /* Optional fields. */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001227 undo_write_bytes(bi, 4, 1);
1228 undo_write_bytes(bi, UF_LAST_SAVE_NR, 1);
1229 undo_write_bytes(bi, (long_u)buf->b_u_save_nr_last, 4);
Bram Moolenaara800b422010-06-27 01:15:55 +02001230
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001231 undo_write_bytes(bi, 0, 1); /* end marker */
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001232
1233 return OK;
1234}
1235
1236 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001237serialize_uhp(bufinfo_T *bi, u_header_T *uhp)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001238{
1239 int i;
1240 u_entry_T *uep;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001241 char_u time_buf[8];
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001242
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001243 if (undo_write_bytes(bi, (long_u)UF_HEADER_MAGIC, 2) == FAIL)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001244 return FAIL;
1245
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001246 put_header_ptr(bi, uhp->uh_next.ptr);
1247 put_header_ptr(bi, uhp->uh_prev.ptr);
1248 put_header_ptr(bi, uhp->uh_alt_next.ptr);
1249 put_header_ptr(bi, uhp->uh_alt_prev.ptr);
1250 undo_write_bytes(bi, uhp->uh_seq, 4);
1251 serialize_pos(bi, uhp->uh_cursor);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001252 undo_write_bytes(bi, (long_u)uhp->uh_cursor_vcol, 4);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001253 undo_write_bytes(bi, (long_u)uhp->uh_flags, 2);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001254 /* Assume NMARKS will stay the same. */
1255 for (i = 0; i < NMARKS; ++i)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001256 serialize_pos(bi, uhp->uh_namedm[i]);
1257 serialize_visualinfo(bi, &uhp->uh_visual);
1258 time_to_bytes(uhp->uh_time, time_buf);
1259 undo_write(bi, time_buf, 8);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001260
Bram Moolenaara800b422010-06-27 01:15:55 +02001261 /* Optional fields. */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001262 undo_write_bytes(bi, 4, 1);
1263 undo_write_bytes(bi, UHP_SAVE_NR, 1);
1264 undo_write_bytes(bi, (long_u)uhp->uh_save_nr, 4);
Bram Moolenaara800b422010-06-27 01:15:55 +02001265
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001266 undo_write_bytes(bi, 0, 1); /* end marker */
Bram Moolenaara800b422010-06-27 01:15:55 +02001267
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001268 /* Write all the entries. */
1269 for (uep = uhp->uh_entry; uep != NULL; uep = uep->ue_next)
1270 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001271 undo_write_bytes(bi, (long_u)UF_ENTRY_MAGIC, 2);
1272 if (serialize_uep(bi, uep) == FAIL)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001273 return FAIL;
1274 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001275 undo_write_bytes(bi, (long_u)UF_ENTRY_END_MAGIC, 2);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001276 return OK;
1277}
1278
1279 static u_header_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001280unserialize_uhp(bufinfo_T *bi, char_u *file_name)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001281{
1282 u_header_T *uhp;
1283 int i;
1284 u_entry_T *uep, *last_uep;
1285 int c;
1286 int error;
1287
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001288 uhp = U_ALLOC_LINE(sizeof(u_header_T));
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001289 if (uhp == NULL)
1290 return NULL;
1291 vim_memset(uhp, 0, sizeof(u_header_T));
1292#ifdef U_DEBUG
1293 uhp->uh_magic = UH_MAGIC;
1294#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001295 uhp->uh_next.seq = undo_read_4c(bi);
1296 uhp->uh_prev.seq = undo_read_4c(bi);
1297 uhp->uh_alt_next.seq = undo_read_4c(bi);
1298 uhp->uh_alt_prev.seq = undo_read_4c(bi);
1299 uhp->uh_seq = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001300 if (uhp->uh_seq <= 0)
1301 {
1302 corruption_error("uh_seq", file_name);
1303 vim_free(uhp);
1304 return NULL;
1305 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001306 unserialize_pos(bi, &uhp->uh_cursor);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001307 uhp->uh_cursor_vcol = undo_read_4c(bi);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001308 uhp->uh_flags = undo_read_2c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001309 for (i = 0; i < NMARKS; ++i)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001310 unserialize_pos(bi, &uhp->uh_namedm[i]);
1311 unserialize_visualinfo(bi, &uhp->uh_visual);
1312 uhp->uh_time = undo_read_time(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001313
Bram Moolenaara800b422010-06-27 01:15:55 +02001314 /* Optional fields. */
Bram Moolenaar69154f22010-07-18 21:42:34 +02001315 for (;;)
1316 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001317 int len = undo_read_byte(bi);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001318 int what;
Bram Moolenaara800b422010-06-27 01:15:55 +02001319
Bram Moolenaar69154f22010-07-18 21:42:34 +02001320 if (len == 0)
1321 break;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001322 what = undo_read_byte(bi);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001323 switch (what)
1324 {
1325 case UHP_SAVE_NR:
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001326 uhp->uh_save_nr = undo_read_4c(bi);
Bram Moolenaara800b422010-06-27 01:15:55 +02001327 break;
Bram Moolenaar69154f22010-07-18 21:42:34 +02001328 default:
1329 /* field not supported, skip */
1330 while (--len >= 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001331 (void)undo_read_byte(bi);
Bram Moolenaara800b422010-06-27 01:15:55 +02001332 }
Bram Moolenaar69154f22010-07-18 21:42:34 +02001333 }
Bram Moolenaara800b422010-06-27 01:15:55 +02001334
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001335 /* Unserialize the uep list. */
1336 last_uep = NULL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001337 while ((c = undo_read_2c(bi)) == UF_ENTRY_MAGIC)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001338 {
1339 error = FALSE;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001340 uep = unserialize_uep(bi, &error, file_name);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001341 if (last_uep == NULL)
1342 uhp->uh_entry = uep;
1343 else
1344 last_uep->ue_next = uep;
1345 last_uep = uep;
1346 if (uep == NULL || error)
1347 {
1348 u_free_uhp(uhp);
1349 return NULL;
1350 }
1351 }
1352 if (c != UF_ENTRY_END_MAGIC)
1353 {
1354 corruption_error("entry end", file_name);
1355 u_free_uhp(uhp);
1356 return NULL;
1357 }
1358
1359 return uhp;
1360}
1361
Bram Moolenaar9db58062010-05-29 20:33:07 +02001362/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001363 * Serialize "uep".
Bram Moolenaar9db58062010-05-29 20:33:07 +02001364 */
1365 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001366serialize_uep(
1367 bufinfo_T *bi,
1368 u_entry_T *uep)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001369{
1370 int i;
1371 size_t len;
1372
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001373 undo_write_bytes(bi, (long_u)uep->ue_top, 4);
1374 undo_write_bytes(bi, (long_u)uep->ue_bot, 4);
1375 undo_write_bytes(bi, (long_u)uep->ue_lcount, 4);
1376 undo_write_bytes(bi, (long_u)uep->ue_size, 4);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001377 for (i = 0; i < uep->ue_size; ++i)
1378 {
Bram Moolenaarccae4672019-01-04 15:09:57 +01001379 // Text is written without the text properties, since we cannot restore
1380 // the text property types.
1381 len = STRLEN(uep->ue_array[i].ul_line);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001382 if (undo_write_bytes(bi, (long_u)len, 4) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001383 return FAIL;
Bram Moolenaarccae4672019-01-04 15:09:57 +01001384 if (len > 0 && fwrite_crypt(bi, uep->ue_array[i].ul_line, len) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001385 return FAIL;
1386 }
1387 return OK;
1388}
1389
1390 static u_entry_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001391unserialize_uep(bufinfo_T *bi, int *error, char_u *file_name)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001392{
1393 int i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001394 u_entry_T *uep;
Bram Moolenaarccae4672019-01-04 15:09:57 +01001395 undoline_T *array = NULL;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001396 char_u *line;
1397 int line_len;
1398
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001399 uep = U_ALLOC_LINE(sizeof(u_entry_T));
Bram Moolenaar9db58062010-05-29 20:33:07 +02001400 if (uep == NULL)
1401 return NULL;
1402 vim_memset(uep, 0, sizeof(u_entry_T));
1403#ifdef U_DEBUG
1404 uep->ue_magic = UE_MAGIC;
1405#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001406 uep->ue_top = undo_read_4c(bi);
1407 uep->ue_bot = undo_read_4c(bi);
1408 uep->ue_lcount = undo_read_4c(bi);
1409 uep->ue_size = undo_read_4c(bi);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001410 if (uep->ue_size > 0)
1411 {
Bram Moolenaar0c8485f2017-02-26 18:17:10 +01001412 if (uep->ue_size < LONG_MAX / (int)sizeof(char_u *))
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001413 array = U_ALLOC_LINE(sizeof(undoline_T) * uep->ue_size);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001414 if (array == NULL)
1415 {
1416 *error = TRUE;
1417 return uep;
1418 }
Bram Moolenaarccae4672019-01-04 15:09:57 +01001419 vim_memset(array, 0, sizeof(undoline_T) * uep->ue_size);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001420 }
1421 uep->ue_array = array;
1422
1423 for (i = 0; i < uep->ue_size; ++i)
1424 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001425 line_len = undo_read_4c(bi);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001426 if (line_len >= 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001427 line = read_string_decrypt(bi, line_len);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001428 else
1429 {
1430 line = NULL;
1431 corruption_error("line length", file_name);
1432 }
1433 if (line == NULL)
1434 {
1435 *error = TRUE;
1436 return uep;
1437 }
Bram Moolenaarccae4672019-01-04 15:09:57 +01001438 array[i].ul_line = line;
1439 array[i].ul_len = line_len + 1;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001440 }
1441 return uep;
1442}
1443
1444/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001445 * Serialize "pos".
Bram Moolenaar9db58062010-05-29 20:33:07 +02001446 */
1447 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001448serialize_pos(bufinfo_T *bi, pos_T pos)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001449{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001450 undo_write_bytes(bi, (long_u)pos.lnum, 4);
1451 undo_write_bytes(bi, (long_u)pos.col, 4);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001452 undo_write_bytes(bi, (long_u)pos.coladd, 4);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001453}
1454
1455/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001456 * Unserialize the pos_T at the current position.
Bram Moolenaar9db58062010-05-29 20:33:07 +02001457 */
1458 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001459unserialize_pos(bufinfo_T *bi, pos_T *pos)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001460{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001461 pos->lnum = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001462 if (pos->lnum < 0)
1463 pos->lnum = 0;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001464 pos->col = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001465 if (pos->col < 0)
1466 pos->col = 0;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001467 pos->coladd = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001468 if (pos->coladd < 0)
1469 pos->coladd = 0;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001470}
1471
1472/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001473 * Serialize "info".
Bram Moolenaar9db58062010-05-29 20:33:07 +02001474 */
1475 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001476serialize_visualinfo(bufinfo_T *bi, visualinfo_T *info)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001477{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001478 serialize_pos(bi, info->vi_start);
1479 serialize_pos(bi, info->vi_end);
1480 undo_write_bytes(bi, (long_u)info->vi_mode, 4);
1481 undo_write_bytes(bi, (long_u)info->vi_curswant, 4);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001482}
1483
1484/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001485 * Unserialize the visualinfo_T at the current position.
Bram Moolenaar9db58062010-05-29 20:33:07 +02001486 */
1487 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001488unserialize_visualinfo(bufinfo_T *bi, visualinfo_T *info)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001489{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001490 unserialize_pos(bi, &info->vi_start);
1491 unserialize_pos(bi, &info->vi_end);
1492 info->vi_mode = undo_read_4c(bi);
1493 info->vi_curswant = undo_read_4c(bi);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001494}
1495
1496/*
1497 * Write the undo tree in an undo file.
1498 * When "name" is not NULL, use it as the name of the undo file.
1499 * Otherwise use buf->b_ffname to generate the undo file name.
1500 * "buf" must never be null, buf->b_ffname is used to obtain the original file
1501 * permissions.
1502 * "forceit" is TRUE for ":wundo!", FALSE otherwise.
1503 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text.
1504 */
1505 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001506u_write_undo(
1507 char_u *name,
1508 int forceit,
1509 buf_T *buf,
1510 char_u *hash)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001511{
1512 u_header_T *uhp;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001513 char_u *file_name;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001514 int mark;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001515#ifdef U_DEBUG
1516 int headers_written = 0;
1517#endif
1518 int fd;
1519 FILE *fp = NULL;
1520 int perm;
1521 int write_ok = FALSE;
1522#ifdef UNIX
1523 int st_old_valid = FALSE;
Bram Moolenaar8767f522016-07-01 17:17:39 +02001524 stat_T st_old;
1525 stat_T st_new;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001526#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001527 bufinfo_T bi;
1528
Bram Moolenaar5a669b92014-08-12 20:14:33 +02001529 vim_memset(&bi, 0, sizeof(bi));
Bram Moolenaar9db58062010-05-29 20:33:07 +02001530
1531 if (name == NULL)
1532 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001533 file_name = u_get_undo_file_name(buf->b_ffname, FALSE);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001534 if (file_name == NULL)
1535 {
1536 if (p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001537 {
1538 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001539 smsg(
Bram Moolenaar504a8212010-05-30 17:17:42 +02001540 _("Cannot write undo file in any directory in 'undodir'"));
1541 verbose_leave();
1542 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001543 return;
1544 }
1545 }
1546 else
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001547 file_name = name;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001548
1549 /*
1550 * Decide about the permission to use for the undo file. If the buffer
1551 * has a name use the permission of the original file. Otherwise only
1552 * allow the user to access the undo file.
1553 */
1554 perm = 0600;
1555 if (buf->b_ffname != NULL)
1556 {
1557#ifdef UNIX
1558 if (mch_stat((char *)buf->b_ffname, &st_old) >= 0)
1559 {
1560 perm = st_old.st_mode;
1561 st_old_valid = TRUE;
1562 }
1563#else
1564 perm = mch_getperm(buf->b_ffname);
1565 if (perm < 0)
1566 perm = 0600;
1567#endif
1568 }
1569
Bram Moolenaar3cbac302015-04-21 16:12:06 +02001570 /* strip any s-bit and executable bit */
1571 perm = perm & 0666;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001572
1573 /* If the undo file already exists, verify that it actually is an undo
1574 * file, and delete it. */
1575 if (mch_getperm(file_name) >= 0)
1576 {
1577 if (name == NULL || !forceit)
1578 {
1579 /* Check we can read it and it's an undo file. */
1580 fd = mch_open((char *)file_name, O_RDONLY|O_EXTRA, 0);
1581 if (fd < 0)
1582 {
1583 if (name != NULL || p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001584 {
1585 if (name == NULL)
1586 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001587 smsg(
Bram Moolenaar504a8212010-05-30 17:17:42 +02001588 _("Will not overwrite with undo file, cannot read: %s"),
Bram Moolenaar9db58062010-05-29 20:33:07 +02001589 file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001590 if (name == NULL)
1591 verbose_leave();
1592 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001593 goto theend;
1594 }
1595 else
1596 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02001597 char_u mbuf[UF_START_MAGIC_LEN];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001598 int len;
1599
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01001600 len = read_eintr(fd, mbuf, UF_START_MAGIC_LEN);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001601 close(fd);
1602 if (len < UF_START_MAGIC_LEN
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02001603 || memcmp(mbuf, UF_START_MAGIC, UF_START_MAGIC_LEN) != 0)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001604 {
1605 if (name != NULL || p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001606 {
1607 if (name == NULL)
1608 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001609 smsg(
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001610 _("Will not overwrite, this is not an undo file: %s"),
Bram Moolenaar9db58062010-05-29 20:33:07 +02001611 file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001612 if (name == NULL)
1613 verbose_leave();
1614 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001615 goto theend;
1616 }
1617 }
1618 }
1619 mch_remove(file_name);
1620 }
1621
Bram Moolenaar504a8212010-05-30 17:17:42 +02001622 /* If there is no undo information at all, quit here after deleting any
1623 * existing undo file. */
Bram Moolenaarccae4672019-01-04 15:09:57 +01001624 if (buf->b_u_numhead == 0 && buf->b_u_line_ptr.ul_line == NULL)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001625 {
1626 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001627 verb_msg(_("Skipping undo file write, nothing to undo"));
Bram Moolenaar504a8212010-05-30 17:17:42 +02001628 goto theend;
1629 }
1630
Bram Moolenaar9db58062010-05-29 20:33:07 +02001631 fd = mch_open((char *)file_name,
1632 O_CREAT|O_EXTRA|O_WRONLY|O_EXCL|O_NOFOLLOW, perm);
1633 if (fd < 0)
1634 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001635 semsg(_(e_not_open), file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001636 goto theend;
1637 }
1638 (void)mch_setperm(file_name, perm);
1639 if (p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001640 {
1641 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001642 smsg(_("Writing undo file: %s"), file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001643 verbose_leave();
1644 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001645
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001646#ifdef U_DEBUG
Bram Moolenaar504a8212010-05-30 17:17:42 +02001647 /* Check there is no problem in undo info before writing. */
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001648 u_check(FALSE);
1649#endif
1650
Bram Moolenaar9db58062010-05-29 20:33:07 +02001651#ifdef UNIX
1652 /*
1653 * Try to set the group of the undo file same as the original file. If
1654 * this fails, set the protection bits for the group same as the
1655 * protection bits for others.
1656 */
Bram Moolenaarce69e822010-07-21 20:31:07 +02001657 if (st_old_valid
1658 && mch_stat((char *)file_name, &st_new) >= 0
1659 && st_new.st_gid != st_old.st_gid
Bram Moolenaar9db58062010-05-29 20:33:07 +02001660# ifdef HAVE_FCHOWN /* sequent-ptx lacks fchown() */
Bram Moolenaarce69e822010-07-21 20:31:07 +02001661 && fchown(fd, (uid_t)-1, st_old.st_gid) != 0
Bram Moolenaar9db58062010-05-29 20:33:07 +02001662# endif
1663 )
1664 mch_setperm(file_name, (perm & 0707) | ((perm & 07) << 3));
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02001665# if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001666 if (buf->b_ffname != NULL)
1667 mch_copy_sec(buf->b_ffname, file_name);
1668# endif
1669#endif
1670
1671 fp = fdopen(fd, "w");
1672 if (fp == NULL)
1673 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001674 semsg(_(e_not_open), file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001675 close(fd);
1676 mch_remove(file_name);
1677 goto theend;
1678 }
1679
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001680 /* Undo must be synced. */
1681 u_sync(TRUE);
1682
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001683 /*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001684 * Write the header. Initializes encryption, if enabled.
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001685 */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001686 bi.bi_buf = buf;
1687 bi.bi_fp = fp;
1688 if (serialize_header(&bi, hash) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001689 goto write_error;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001690
1691 /*
1692 * Iteratively serialize UHPs and their UEPs from the top down.
1693 */
1694 mark = ++lastmark;
1695 uhp = buf->b_u_oldhead;
1696 while (uhp != NULL)
1697 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001698 /* Serialize current UHP if we haven't seen it */
1699 if (uhp->uh_walk != mark)
1700 {
1701 uhp->uh_walk = mark;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001702#ifdef U_DEBUG
1703 ++headers_written;
1704#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001705 if (serialize_uhp(&bi, uhp) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001706 goto write_error;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001707 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001708
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001709 /* Now walk through the tree - algorithm from undo_time(). */
1710 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != mark)
1711 uhp = uhp->uh_prev.ptr;
1712 else if (uhp->uh_alt_next.ptr != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001713 && uhp->uh_alt_next.ptr->uh_walk != mark)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001714 uhp = uhp->uh_alt_next.ptr;
1715 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001716 && uhp->uh_next.ptr->uh_walk != mark)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001717 uhp = uhp->uh_next.ptr;
1718 else if (uhp->uh_alt_prev.ptr != NULL)
1719 uhp = uhp->uh_alt_prev.ptr;
1720 else
1721 uhp = uhp->uh_next.ptr;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001722 }
1723
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001724 if (undo_write_bytes(&bi, (long_u)UF_HEADER_END_MAGIC, 2) == OK)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001725 write_ok = TRUE;
1726#ifdef U_DEBUG
1727 if (headers_written != buf->b_u_numhead)
Bram Moolenaar570064c2013-06-10 20:25:10 +02001728 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001729 semsg("Written %ld headers, ...", headers_written);
1730 semsg("... but numhead is %ld", buf->b_u_numhead);
Bram Moolenaar570064c2013-06-10 20:25:10 +02001731 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001732#endif
1733
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001734#ifdef FEAT_CRYPT
1735 if (bi.bi_state != NULL && undo_flush(&bi) == FAIL)
1736 write_ok = FALSE;
1737#endif
1738
Bram Moolenaar9db58062010-05-29 20:33:07 +02001739write_error:
1740 fclose(fp);
1741 if (!write_ok)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001742 semsg(_("E829: write error in undo file: %s"), file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001743
Bram Moolenaar4f974752019-02-17 17:44:42 +01001744#if defined(MSWIN)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001745 /* Copy file attributes; for systems where this can only be done after
1746 * closing the file. */
Bram Moolenaar9db58062010-05-29 20:33:07 +02001747 if (buf->b_ffname != NULL)
1748 (void)mch_copy_file_attribute(buf->b_ffname, file_name);
1749#endif
1750#ifdef HAVE_ACL
1751 if (buf->b_ffname != NULL)
1752 {
1753 vim_acl_T acl;
1754
1755 /* For systems that support ACL: get the ACL from the original file. */
1756 acl = mch_get_acl(buf->b_ffname);
1757 mch_set_acl(file_name, acl);
Bram Moolenaard2aed442012-06-01 13:46:12 +02001758 mch_free_acl(acl);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001759 }
1760#endif
1761
1762theend:
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001763#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001764 if (bi.bi_state != NULL)
1765 crypt_free_state(bi.bi_state);
1766 vim_free(bi.bi_buffer);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001767#endif
Bram Moolenaar9db58062010-05-29 20:33:07 +02001768 if (file_name != name)
1769 vim_free(file_name);
1770}
1771
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001772/*
1773 * Load the undo tree from an undo file.
1774 * If "name" is not NULL use it as the undo file name. This also means being
1775 * a bit more verbose.
1776 * Otherwise use curbuf->b_ffname to generate the undo file name.
1777 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text.
1778 */
1779 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001780u_read_undo(char_u *name, char_u *hash, char_u *orig_name)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001781{
1782 char_u *file_name;
1783 FILE *fp;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001784 long version, str_len;
Bram Moolenaarccae4672019-01-04 15:09:57 +01001785 undoline_T line_ptr;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001786 linenr_T line_lnum;
1787 colnr_T line_colnr;
1788 linenr_T line_count;
Bram Moolenaar3eb16372017-02-26 18:11:36 +01001789 long num_head = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001790 long old_header_seq, new_header_seq, cur_header_seq;
1791 long seq_last, seq_cur;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001792 long last_save_nr = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001793 short old_idx = -1, new_idx = -1, cur_idx = -1;
1794 long num_read_uhps = 0;
1795 time_t seq_time;
1796 int i, j;
1797 int c;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001798 u_header_T *uhp;
1799 u_header_T **uhp_table = NULL;
1800 char_u read_hash[UNDO_HASH_SIZE];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001801 char_u magic_buf[UF_START_MAGIC_LEN];
1802#ifdef U_DEBUG
1803 int *uhp_table_used;
1804#endif
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001805#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001806 stat_T st_orig;
1807 stat_T st_undo;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001808#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001809 bufinfo_T bi;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001810
Bram Moolenaar5a669b92014-08-12 20:14:33 +02001811 vim_memset(&bi, 0, sizeof(bi));
Bram Moolenaarccae4672019-01-04 15:09:57 +01001812 line_ptr.ul_len = 0;
1813 line_ptr.ul_line = NULL;
1814
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001815 if (name == NULL)
1816 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001817 file_name = u_get_undo_file_name(curbuf->b_ffname, TRUE);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001818 if (file_name == NULL)
1819 return;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001820
1821#ifdef UNIX
1822 /* For safety we only read an undo file if the owner is equal to the
Bram Moolenaar3b262392013-09-08 15:40:49 +02001823 * owner of the text file or equal to the current user. */
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001824 if (mch_stat((char *)orig_name, &st_orig) >= 0
1825 && mch_stat((char *)file_name, &st_undo) >= 0
Bram Moolenaar3b262392013-09-08 15:40:49 +02001826 && st_orig.st_uid != st_undo.st_uid
1827 && st_undo.st_uid != getuid())
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001828 {
1829 if (p_verbose > 0)
1830 {
1831 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001832 smsg(_("Not reading undo file, owner differs: %s"),
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001833 file_name);
1834 verbose_leave();
1835 }
1836 return;
1837 }
1838#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001839 }
1840 else
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001841 file_name = name;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001842
1843 if (p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001844 {
1845 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001846 smsg(_("Reading undo file: %s"), file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001847 verbose_leave();
1848 }
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001849
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001850 fp = mch_fopen((char *)file_name, "r");
1851 if (fp == NULL)
1852 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001853 if (name != NULL || p_verbose > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001854 semsg(_("E822: Cannot open undo file for reading: %s"), file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001855 goto error;
1856 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001857 bi.bi_buf = curbuf;
1858 bi.bi_fp = fp;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001859
Bram Moolenaar9db58062010-05-29 20:33:07 +02001860 /*
1861 * Read the undo file header.
1862 */
1863 if (fread(magic_buf, UF_START_MAGIC_LEN, 1, fp) != 1
1864 || memcmp(magic_buf, UF_START_MAGIC, UF_START_MAGIC_LEN) != 0)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001865 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001866 semsg(_("E823: Not an undo file: %s"), file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001867 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001868 }
1869 version = get2c(fp);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001870 if (version == UF_VERSION_CRYPT)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001871 {
1872#ifdef FEAT_CRYPT
Bram Moolenaar56be9502010-06-06 14:20:26 +02001873 if (*curbuf->b_p_key == NUL)
1874 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001875 semsg(_("E832: Non-encrypted file has encrypted undo file: %s"),
Bram Moolenaar56be9502010-06-06 14:20:26 +02001876 file_name);
1877 goto error;
1878 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001879 bi.bi_state = crypt_create_from_file(fp, curbuf->b_p_key);
1880 if (bi.bi_state == NULL)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001881 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001882 semsg(_("E826: Undo file decryption failed: %s"), file_name);
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001883 goto error;
1884 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001885 if (crypt_whole_undofile(bi.bi_state->method_nr))
1886 {
1887 bi.bi_buffer = alloc(CRYPT_BUF_SIZE);
1888 if (bi.bi_buffer == NULL)
1889 {
1890 crypt_free_state(bi.bi_state);
1891 bi.bi_state = NULL;
1892 goto error;
1893 }
1894 bi.bi_avail = 0;
1895 bi.bi_used = 0;
1896 }
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001897#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001898 semsg(_("E827: Undo file is encrypted: %s"), file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001899 goto error;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001900#endif
1901 }
Bram Moolenaar69154f22010-07-18 21:42:34 +02001902 else if (version != UF_VERSION)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001903 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001904 semsg(_("E824: Incompatible undo file: %s"), file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001905 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001906 }
1907
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001908 if (undo_read(&bi, read_hash, (size_t)UNDO_HASH_SIZE) == FAIL)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001909 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001910 corruption_error("hash", file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001911 goto error;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001912 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001913 line_count = (linenr_T)undo_read_4c(&bi);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001914 if (memcmp(hash, read_hash, UNDO_HASH_SIZE) != 0
1915 || line_count != curbuf->b_ml.ml_line_count)
1916 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001917 if (p_verbose > 0 || name != NULL)
1918 {
Bram Moolenaar504a8212010-05-30 17:17:42 +02001919 if (name == NULL)
1920 verbose_enter();
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001921 give_warning((char_u *)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001922 _("File contents changed, cannot use undo info"), TRUE);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001923 if (name == NULL)
1924 verbose_leave();
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001925 }
1926 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001927 }
1928
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001929 /* Read undo data for "U" command. */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001930 str_len = undo_read_4c(&bi);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001931 if (str_len < 0)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001932 goto error;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001933 if (str_len > 0)
Bram Moolenaarccae4672019-01-04 15:09:57 +01001934 {
1935 line_ptr.ul_line = read_string_decrypt(&bi, str_len);
1936 line_ptr.ul_len = str_len + 1;
1937 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001938 line_lnum = (linenr_T)undo_read_4c(&bi);
1939 line_colnr = (colnr_T)undo_read_4c(&bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001940 if (line_lnum < 0 || line_colnr < 0)
1941 {
1942 corruption_error("line lnum/col", file_name);
1943 goto error;
1944 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001945
1946 /* Begin general undo data */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001947 old_header_seq = undo_read_4c(&bi);
1948 new_header_seq = undo_read_4c(&bi);
1949 cur_header_seq = undo_read_4c(&bi);
1950 num_head = undo_read_4c(&bi);
1951 seq_last = undo_read_4c(&bi);
1952 seq_cur = undo_read_4c(&bi);
1953 seq_time = undo_read_time(&bi);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001954
Bram Moolenaar69154f22010-07-18 21:42:34 +02001955 /* Optional header fields. */
1956 for (;;)
1957 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001958 int len = undo_read_byte(&bi);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001959 int what;
Bram Moolenaara800b422010-06-27 01:15:55 +02001960
Bram Moolenaar69154f22010-07-18 21:42:34 +02001961 if (len == 0 || len == EOF)
1962 break;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001963 what = undo_read_byte(&bi);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001964 switch (what)
1965 {
1966 case UF_LAST_SAVE_NR:
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001967 last_save_nr = undo_read_4c(&bi);
Bram Moolenaara800b422010-06-27 01:15:55 +02001968 break;
Bram Moolenaar69154f22010-07-18 21:42:34 +02001969 default:
1970 /* field not supported, skip */
1971 while (--len >= 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001972 (void)undo_read_byte(&bi);
Bram Moolenaara800b422010-06-27 01:15:55 +02001973 }
Bram Moolenaar69154f22010-07-18 21:42:34 +02001974 }
Bram Moolenaara800b422010-06-27 01:15:55 +02001975
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001976 /* uhp_table will store the freshly created undo headers we allocate
1977 * until we insert them into curbuf. The table remains sorted by the
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001978 * sequence numbers of the headers.
1979 * When there are no headers uhp_table is NULL. */
1980 if (num_head > 0)
1981 {
Bram Moolenaar3eb16372017-02-26 18:11:36 +01001982 if (num_head < LONG_MAX / (long)sizeof(u_header_T *))
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001983 uhp_table = U_ALLOC_LINE(num_head * sizeof(u_header_T *));
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001984 if (uhp_table == NULL)
1985 goto error;
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001986 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001987
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001988 while ((c = undo_read_2c(&bi)) == UF_HEADER_MAGIC)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001989 {
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001990 if (num_read_uhps >= num_head)
1991 {
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001992 corruption_error("num_head too small", file_name);
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001993 goto error;
1994 }
1995
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001996 uhp = unserialize_uhp(&bi, file_name);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001997 if (uhp == NULL)
1998 goto error;
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001999 uhp_table[num_read_uhps++] = uhp;
2000 }
2001
2002 if (num_read_uhps != num_head)
2003 {
2004 corruption_error("num_head", file_name);
2005 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002006 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02002007 if (c != UF_HEADER_END_MAGIC)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002008 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02002009 corruption_error("end marker", file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002010 goto error;
2011 }
2012
Bram Moolenaar9db58062010-05-29 20:33:07 +02002013#ifdef U_DEBUG
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002014 uhp_table_used = alloc_clear(sizeof(int) * num_head + 1);
Bram Moolenaar9db58062010-05-29 20:33:07 +02002015# define SET_FLAG(j) ++uhp_table_used[j]
2016#else
2017# define SET_FLAG(j)
2018#endif
2019
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002020 /* We have put all of the headers into a table. Now we iterate through the
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002021 * table and swizzle each sequence number we have stored in uh_*_seq into
2022 * a pointer corresponding to the header with that sequence number. */
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002023 for (i = 0; i < num_head; i++)
2024 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002025 uhp = uhp_table[i];
2026 if (uhp == NULL)
2027 continue;
2028 for (j = 0; j < num_head; j++)
2029 if (uhp_table[j] != NULL && i != j
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002030 && uhp_table[i]->uh_seq == uhp_table[j]->uh_seq)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002031 {
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02002032 corruption_error("duplicate uh_seq", file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002033 goto error;
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02002034 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002035 for (j = 0; j < num_head; j++)
2036 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002037 && uhp_table[j]->uh_seq == uhp->uh_next.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002038 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002039 uhp->uh_next.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02002040 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002041 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002042 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002043 for (j = 0; j < num_head; j++)
2044 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002045 && uhp_table[j]->uh_seq == uhp->uh_prev.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002046 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002047 uhp->uh_prev.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02002048 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002049 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002050 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002051 for (j = 0; j < num_head; j++)
2052 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002053 && uhp_table[j]->uh_seq == uhp->uh_alt_next.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002054 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002055 uhp->uh_alt_next.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02002056 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002057 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002058 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002059 for (j = 0; j < num_head; j++)
2060 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002061 && uhp_table[j]->uh_seq == uhp->uh_alt_prev.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002062 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002063 uhp->uh_alt_prev.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02002064 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002065 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002066 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002067 if (old_header_seq > 0 && old_idx < 0 && uhp->uh_seq == old_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002068 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002069 old_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002070 SET_FLAG(i);
2071 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002072 if (new_header_seq > 0 && new_idx < 0 && uhp->uh_seq == new_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002073 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002074 new_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002075 SET_FLAG(i);
2076 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002077 if (cur_header_seq > 0 && cur_idx < 0 && uhp->uh_seq == cur_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002078 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002079 cur_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002080 SET_FLAG(i);
2081 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002082 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02002083
2084 /* Now that we have read the undo info successfully, free the current undo
2085 * info and use the info from the file. */
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002086 u_blockfree(curbuf);
Bram Moolenaar9db58062010-05-29 20:33:07 +02002087 curbuf->b_u_oldhead = old_idx < 0 ? NULL : uhp_table[old_idx];
2088 curbuf->b_u_newhead = new_idx < 0 ? NULL : uhp_table[new_idx];
2089 curbuf->b_u_curhead = cur_idx < 0 ? NULL : uhp_table[cur_idx];
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002090 curbuf->b_u_line_ptr = line_ptr;
2091 curbuf->b_u_line_lnum = line_lnum;
2092 curbuf->b_u_line_colnr = line_colnr;
2093 curbuf->b_u_numhead = num_head;
2094 curbuf->b_u_seq_last = seq_last;
2095 curbuf->b_u_seq_cur = seq_cur;
Bram Moolenaara800b422010-06-27 01:15:55 +02002096 curbuf->b_u_time_cur = seq_time;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002097 curbuf->b_u_save_nr_last = last_save_nr;
Bram Moolenaardba01a02010-11-03 19:32:42 +01002098 curbuf->b_u_save_nr_cur = last_save_nr;
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02002099
2100 curbuf->b_u_synced = TRUE;
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002101 vim_free(uhp_table);
Bram Moolenaar9db58062010-05-29 20:33:07 +02002102
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002103#ifdef U_DEBUG
Bram Moolenaar9db58062010-05-29 20:33:07 +02002104 for (i = 0; i < num_head; ++i)
2105 if (uhp_table_used[i] == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002106 semsg("uhp_table entry %ld not used, leaking memory", i);
Bram Moolenaar9db58062010-05-29 20:33:07 +02002107 vim_free(uhp_table_used);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002108 u_check(TRUE);
2109#endif
Bram Moolenaar9db58062010-05-29 20:33:07 +02002110
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002111 if (name != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002112 smsg(_("Finished reading undo file %s"), file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002113 goto theend;
2114
2115error:
Bram Moolenaarccae4672019-01-04 15:09:57 +01002116 vim_free(line_ptr.ul_line);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002117 if (uhp_table != NULL)
2118 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002119 for (i = 0; i < num_read_uhps; i++)
2120 if (uhp_table[i] != NULL)
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02002121 u_free_uhp(uhp_table[i]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002122 vim_free(uhp_table);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002123 }
2124
2125theend:
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002126#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002127 if (bi.bi_state != NULL)
2128 crypt_free_state(bi.bi_state);
2129 vim_free(bi.bi_buffer);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002130#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002131 if (fp != NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002132 fclose(fp);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002133 if (file_name != name)
2134 vim_free(file_name);
2135 return;
2136}
2137
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002138#endif /* FEAT_PERSISTENT_UNDO */
2139
2140
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141/*
2142 * If 'cpoptions' contains 'u': Undo the previous undo or redo (vi compatible).
2143 * If 'cpoptions' does not contain 'u': Always undo.
2144 */
2145 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002146u_undo(int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147{
2148 /*
2149 * If we get an undo command while executing a macro, we behave like the
2150 * original vi. If this happens twice in one macro the result will not
2151 * be compatible.
2152 */
2153 if (curbuf->b_u_synced == FALSE)
2154 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002155 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 count = 1;
2157 }
2158
2159 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
2160 undo_undoes = TRUE;
2161 else
2162 undo_undoes = !undo_undoes;
2163 u_doit(count);
2164}
2165
2166/*
2167 * If 'cpoptions' contains 'u': Repeat the previous undo or redo.
2168 * If 'cpoptions' does not contain 'u': Always redo.
2169 */
2170 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002171u_redo(int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172{
2173 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
2174 undo_undoes = FALSE;
2175 u_doit(count);
2176}
2177
2178/*
2179 * Undo or redo, depending on 'undo_undoes', 'count' times.
2180 */
2181 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002182u_doit(int startcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183{
Bram Moolenaarca003e12006-03-17 23:19:38 +00002184 int count = startcount;
2185
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002186 if (!undo_allowed())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188
2189 u_newcount = 0;
2190 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002191 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2192 u_oldcount = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 while (count--)
2194 {
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002195 /* Do the change warning now, so that it triggers FileChangedRO when
2196 * needed. This may cause the file to be reloaded, that must happen
2197 * before we do anything, because it may change curbuf->b_u_curhead
2198 * and more. */
2199 change_warning(0);
2200
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 if (undo_undoes)
2202 {
2203 if (curbuf->b_u_curhead == NULL) /* first undo */
2204 curbuf->b_u_curhead = curbuf->b_u_newhead;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002205 else if (get_undolevel() > 0) /* multi level undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 /* get next undo */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002207 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_next.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 /* nothing to undo */
2209 if (curbuf->b_u_numhead == 0 || curbuf->b_u_curhead == NULL)
2210 {
2211 /* stick curbuf->b_u_curhead at end */
2212 curbuf->b_u_curhead = curbuf->b_u_oldhead;
2213 beep_flush();
Bram Moolenaarca003e12006-03-17 23:19:38 +00002214 if (count == startcount - 1)
2215 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002216 msg(_("Already at oldest change"));
Bram Moolenaarca003e12006-03-17 23:19:38 +00002217 return;
2218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 break;
2220 }
2221
Bram Moolenaarca003e12006-03-17 23:19:38 +00002222 u_undoredo(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 }
2224 else
2225 {
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002226 if (curbuf->b_u_curhead == NULL || get_undolevel() <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 {
2228 beep_flush(); /* nothing to redo */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002229 if (count == startcount - 1)
2230 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002231 msg(_("Already at newest change"));
Bram Moolenaarca003e12006-03-17 23:19:38 +00002232 return;
2233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 break;
2235 }
2236
Bram Moolenaarca003e12006-03-17 23:19:38 +00002237 u_undoredo(FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002238
2239 /* Advance for next redo. Set "newhead" when at the end of the
2240 * redoable changes. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002241 if (curbuf->b_u_curhead->uh_prev.ptr == NULL)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002242 curbuf->b_u_newhead = curbuf->b_u_curhead;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002243 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_prev.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 }
2245 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00002246 u_undo_end(undo_undoes, FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002247}
2248
Bram Moolenaar1e607892006-03-13 22:15:53 +00002249/*
2250 * Undo or redo over the timeline.
2251 * When "step" is negative go back in time, otherwise goes forward in time.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002252 * When "sec" is FALSE make "step" steps, when "sec" is TRUE use "step" as
2253 * seconds.
Bram Moolenaar730cde92010-06-27 05:18:54 +02002254 * When "file" is TRUE use "step" as a number of file writes.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002255 * When "absolute" is TRUE use "step" as the sequence number to jump to.
2256 * "sec" must be FALSE then.
Bram Moolenaar1e607892006-03-13 22:15:53 +00002257 */
2258 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002259undo_time(
2260 long step,
2261 int sec,
2262 int file,
2263 int absolute)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002264{
2265 long target;
2266 long closest;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002267 long closest_start;
2268 long closest_seq = 0;
2269 long val;
Bram Moolenaarce46d932018-01-30 22:46:06 +01002270 u_header_T *uhp = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002271 u_header_T *last;
2272 int mark;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02002273 int nomark = 0; // shut up compiler
Bram Moolenaar1e607892006-03-13 22:15:53 +00002274 int round;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002275 int dosec = sec;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002276 int dofile = file;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002277 int above = FALSE;
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002278 int did_undo = TRUE;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002279
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002280 /* First make sure the current undoable change is synced. */
2281 if (curbuf->b_u_synced == FALSE)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002282 u_sync(TRUE);
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002283
Bram Moolenaar1e607892006-03-13 22:15:53 +00002284 u_newcount = 0;
2285 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002286 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002287 u_oldcount = -1;
2288
Bram Moolenaarca003e12006-03-17 23:19:38 +00002289 /* "target" is the node below which we want to be.
2290 * Init "closest" to a value we can't reach. */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002291 if (absolute)
2292 {
Bram Moolenaarce46d932018-01-30 22:46:06 +01002293 target = step;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002294 closest = -1;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002295 }
Bram Moolenaarca003e12006-03-17 23:19:38 +00002296 else
Bram Moolenaar1e607892006-03-13 22:15:53 +00002297 {
Bram Moolenaar730cde92010-06-27 05:18:54 +02002298 if (dosec)
Bram Moolenaarcbd4de42017-01-07 16:14:57 +01002299 target = (long)(curbuf->b_u_time_cur) + step;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002300 else if (dofile)
2301 {
2302 if (step < 0)
2303 {
2304 /* Going back to a previous write. If there were changes after
2305 * the last write, count that as moving one file-write, so
2306 * that ":earlier 1f" undoes all changes since the last save. */
2307 uhp = curbuf->b_u_curhead;
2308 if (uhp != NULL)
2309 uhp = uhp->uh_next.ptr;
2310 else
2311 uhp = curbuf->b_u_newhead;
2312 if (uhp != NULL && uhp->uh_save_nr != 0)
2313 /* "uh_save_nr" was set in the last block, that means
2314 * there were no changes since the last write */
2315 target = curbuf->b_u_save_nr_cur + step;
2316 else
2317 /* count the changes since the last write as one step */
2318 target = curbuf->b_u_save_nr_cur + step + 1;
2319 if (target <= 0)
2320 /* Go to before first write: before the oldest change. Use
2321 * the sequence number for that. */
2322 dofile = FALSE;
2323 }
2324 else
2325 {
2326 /* Moving forward to a newer write. */
2327 target = curbuf->b_u_save_nr_cur + step;
2328 if (target > curbuf->b_u_save_nr_last)
2329 {
2330 /* Go to after last write: after the latest change. Use
2331 * the sequence number for that. */
2332 target = curbuf->b_u_seq_last + 1;
2333 dofile = FALSE;
2334 }
2335 }
2336 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002337 else
2338 target = curbuf->b_u_seq_cur + step;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002339 if (step < 0)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002340 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002341 if (target < 0)
2342 target = 0;
2343 closest = -1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002344 }
2345 else
2346 {
Bram Moolenaar730cde92010-06-27 05:18:54 +02002347 if (dosec)
Bram Moolenaarcbd4de42017-01-07 16:14:57 +01002348 closest = (long)(vim_time() + 1);
Bram Moolenaar730cde92010-06-27 05:18:54 +02002349 else if (dofile)
2350 closest = curbuf->b_u_save_nr_last + 2;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002351 else
2352 closest = curbuf->b_u_seq_last + 2;
2353 if (target >= closest)
2354 target = closest - 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002355 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002356 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002357 closest_start = closest;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002358 closest_seq = curbuf->b_u_seq_cur;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002359
Bram Moolenaarce46d932018-01-30 22:46:06 +01002360 /* When "target" is 0; Back to origin. */
2361 if (target == 0)
Bram Moolenaar059fd012018-01-31 14:25:53 +01002362 {
2363 mark = lastmark; /* avoid that GCC complains */
2364 goto target_zero;
2365 }
Bram Moolenaarce46d932018-01-30 22:46:06 +01002366
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002367 /*
2368 * May do this twice:
Bram Moolenaar1e607892006-03-13 22:15:53 +00002369 * 1. Search for "target", update "closest" to the best match found.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002370 * 2. If "target" not found search for "closest".
2371 *
2372 * When using the closest time we use the sequence number in the second
2373 * round, because there may be several entries with the same time.
2374 */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002375 for (round = 1; round <= 2; ++round)
2376 {
2377 /* Find the path from the current state to where we want to go. The
2378 * desired state can be anywhere in the undo tree, need to go all over
2379 * it. We put "nomark" in uh_walk where we have been without success,
2380 * "mark" where it could possibly be. */
2381 mark = ++lastmark;
2382 nomark = ++lastmark;
2383
2384 if (curbuf->b_u_curhead == NULL) /* at leaf of the tree */
2385 uhp = curbuf->b_u_newhead;
2386 else
2387 uhp = curbuf->b_u_curhead;
2388
2389 while (uhp != NULL)
2390 {
2391 uhp->uh_walk = mark;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002392 if (dosec)
Bram Moolenaarcbd4de42017-01-07 16:14:57 +01002393 val = (long)(uhp->uh_time);
Bram Moolenaar730cde92010-06-27 05:18:54 +02002394 else if (dofile)
2395 val = uhp->uh_save_nr;
2396 else
2397 val = uhp->uh_seq;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002398
Bram Moolenaar730cde92010-06-27 05:18:54 +02002399 if (round == 1 && !(dofile && val == 0))
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002400 {
2401 /* Remember the header that is closest to the target.
2402 * It must be at least in the right direction (checked with
Bram Moolenaarca003e12006-03-17 23:19:38 +00002403 * "b_u_seq_cur"). When the timestamp is equal find the
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002404 * highest/lowest sequence number. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002405 if ((step < 0 ? uhp->uh_seq <= curbuf->b_u_seq_cur
2406 : uhp->uh_seq > curbuf->b_u_seq_cur)
2407 && ((dosec && val == closest)
2408 ? (step < 0
2409 ? uhp->uh_seq < closest_seq
2410 : uhp->uh_seq > closest_seq)
2411 : closest == closest_start
2412 || (val > target
2413 ? (closest > target
2414 ? val - target <= closest - target
2415 : val - target <= target - closest)
2416 : (closest > target
2417 ? target - val <= closest - target
2418 : target - val <= target - closest))))
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002419 {
2420 closest = val;
2421 closest_seq = uhp->uh_seq;
2422 }
2423 }
2424
2425 /* Quit searching when we found a match. But when searching for a
2426 * time we need to continue looking for the best uh_seq. */
2427 if (target == val && !dosec)
Bram Moolenaar730cde92010-06-27 05:18:54 +02002428 {
2429 target = uhp->uh_seq;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002430 break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002431 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002432
2433 /* go down in the tree if we haven't been there */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002434 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != nomark
2435 && uhp->uh_prev.ptr->uh_walk != mark)
2436 uhp = uhp->uh_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002437
2438 /* go to alternate branch if we haven't been there */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002439 else if (uhp->uh_alt_next.ptr != NULL
2440 && uhp->uh_alt_next.ptr->uh_walk != nomark
2441 && uhp->uh_alt_next.ptr->uh_walk != mark)
2442 uhp = uhp->uh_alt_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002443
2444 /* go up in the tree if we haven't been there and we are at the
2445 * start of alternate branches */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002446 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
2447 && uhp->uh_next.ptr->uh_walk != nomark
2448 && uhp->uh_next.ptr->uh_walk != mark)
Bram Moolenaardb552d602006-03-23 22:59:57 +00002449 {
2450 /* If still at the start we don't go through this change. */
2451 if (uhp == curbuf->b_u_curhead)
2452 uhp->uh_walk = nomark;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002453 uhp = uhp->uh_next.ptr;
Bram Moolenaardb552d602006-03-23 22:59:57 +00002454 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002455
2456 else
2457 {
2458 /* need to backtrack; mark this node as useless */
2459 uhp->uh_walk = nomark;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002460 if (uhp->uh_alt_prev.ptr != NULL)
2461 uhp = uhp->uh_alt_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002462 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002463 uhp = uhp->uh_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002464 }
2465 }
2466
2467 if (uhp != NULL) /* found it */
2468 break;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002469
2470 if (absolute)
2471 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002472 semsg(_("E830: Undo number %ld not found"), step);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002473 return;
2474 }
2475
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002476 if (closest == closest_start)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002477 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002478 if (step < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002479 msg(_("Already at oldest change"));
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002480 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002481 msg(_("Already at newest change"));
Bram Moolenaar1e607892006-03-13 22:15:53 +00002482 return;
2483 }
2484
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002485 target = closest_seq;
2486 dosec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002487 dofile = FALSE;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002488 if (step < 0)
2489 above = TRUE; /* stop above the header */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002490 }
2491
Bram Moolenaar059fd012018-01-31 14:25:53 +01002492target_zero:
Bram Moolenaar1e607892006-03-13 22:15:53 +00002493 /* If we found it: Follow the path to go to where we want to be. */
Bram Moolenaarce46d932018-01-30 22:46:06 +01002494 if (uhp != NULL || target == 0)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002495 {
2496 /*
2497 * First go up the tree as much as needed.
2498 */
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002499 while (!got_int)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002500 {
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002501 /* Do the change warning now, for the same reason as above. */
2502 change_warning(0);
2503
Bram Moolenaar1e607892006-03-13 22:15:53 +00002504 uhp = curbuf->b_u_curhead;
2505 if (uhp == NULL)
2506 uhp = curbuf->b_u_newhead;
2507 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002508 uhp = uhp->uh_next.ptr;
Bram Moolenaarce46d932018-01-30 22:46:06 +01002509 if (uhp == NULL || (target > 0 && uhp->uh_walk != mark)
Bram Moolenaarca003e12006-03-17 23:19:38 +00002510 || (uhp->uh_seq == target && !above))
Bram Moolenaar1e607892006-03-13 22:15:53 +00002511 break;
2512 curbuf->b_u_curhead = uhp;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002513 u_undoredo(TRUE);
Bram Moolenaarce46d932018-01-30 22:46:06 +01002514 if (target > 0)
2515 uhp->uh_walk = nomark; /* don't go back down here */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002516 }
2517
Bram Moolenaarce46d932018-01-30 22:46:06 +01002518 /* When back to origin, redo is not needed. */
2519 if (target > 0)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002520 {
Bram Moolenaarce46d932018-01-30 22:46:06 +01002521 /*
2522 * And now go down the tree (redo), branching off where needed.
2523 */
2524 while (!got_int)
2525 {
2526 /* Do the change warning now, for the same reason as above. */
2527 change_warning(0);
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002528
Bram Moolenaarce46d932018-01-30 22:46:06 +01002529 uhp = curbuf->b_u_curhead;
2530 if (uhp == NULL)
2531 break;
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002532
Bram Moolenaarce46d932018-01-30 22:46:06 +01002533 /* Go back to the first branch with a mark. */
2534 while (uhp->uh_alt_prev.ptr != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002535 && uhp->uh_alt_prev.ptr->uh_walk == mark)
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002536 uhp = uhp->uh_alt_prev.ptr;
Bram Moolenaarce46d932018-01-30 22:46:06 +01002537
2538 /* Find the last branch with a mark, that's the one. */
2539 last = uhp;
2540 while (last->uh_alt_next.ptr != NULL
2541 && last->uh_alt_next.ptr->uh_walk == mark)
2542 last = last->uh_alt_next.ptr;
2543 if (last != uhp)
2544 {
2545 /* Make the used branch the first entry in the list of
2546 * alternatives to make "u" and CTRL-R take this branch. */
2547 while (uhp->uh_alt_prev.ptr != NULL)
2548 uhp = uhp->uh_alt_prev.ptr;
2549 if (last->uh_alt_next.ptr != NULL)
2550 last->uh_alt_next.ptr->uh_alt_prev.ptr =
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002551 last->uh_alt_prev.ptr;
Bram Moolenaarce46d932018-01-30 22:46:06 +01002552 last->uh_alt_prev.ptr->uh_alt_next.ptr =
2553 last->uh_alt_next.ptr;
2554 last->uh_alt_prev.ptr = NULL;
2555 last->uh_alt_next.ptr = uhp;
2556 uhp->uh_alt_prev.ptr = last;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002557
Bram Moolenaarce46d932018-01-30 22:46:06 +01002558 if (curbuf->b_u_oldhead == uhp)
2559 curbuf->b_u_oldhead = last;
2560 uhp = last;
2561 if (uhp->uh_next.ptr != NULL)
2562 uhp->uh_next.ptr->uh_prev.ptr = uhp;
2563 }
2564 curbuf->b_u_curhead = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002565
Bram Moolenaarce46d932018-01-30 22:46:06 +01002566 if (uhp->uh_walk != mark)
2567 break; /* must have reached the target */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002568
Bram Moolenaarce46d932018-01-30 22:46:06 +01002569 /* Stop when going backwards in time and didn't find the exact
2570 * header we were looking for. */
2571 if (uhp->uh_seq == target && above)
2572 {
2573 curbuf->b_u_seq_cur = target - 1;
2574 break;
2575 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002576
Bram Moolenaarce46d932018-01-30 22:46:06 +01002577 u_undoredo(FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002578
Bram Moolenaarce46d932018-01-30 22:46:06 +01002579 /* Advance "curhead" to below the header we last used. If it
2580 * becomes NULL then we need to set "newhead" to this leaf. */
2581 if (uhp->uh_prev.ptr == NULL)
2582 curbuf->b_u_newhead = uhp;
2583 curbuf->b_u_curhead = uhp->uh_prev.ptr;
2584 did_undo = FALSE;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002585
Bram Moolenaarce46d932018-01-30 22:46:06 +01002586 if (uhp->uh_seq == target) /* found it! */
2587 break;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002588
Bram Moolenaarce46d932018-01-30 22:46:06 +01002589 uhp = uhp->uh_prev.ptr;
2590 if (uhp == NULL || uhp->uh_walk != mark)
2591 {
2592 /* Need to redo more but can't find it... */
2593 internal_error("undo_time()");
2594 break;
2595 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002596 }
2597 }
2598 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00002599 u_undo_end(did_undo, absolute);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600}
2601
2602/*
2603 * u_undoredo: common code for undo and redo
2604 *
2605 * The lines in the file are replaced by the lines in the entry list at
2606 * curbuf->b_u_curhead. The replaced lines in the file are saved in the entry
2607 * list for the next undo/redo.
Bram Moolenaarca003e12006-03-17 23:19:38 +00002608 *
2609 * When "undo" is TRUE we go up in the tree, when FALSE we go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 */
2611 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002612u_undoredo(int undo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613{
Bram Moolenaarccae4672019-01-04 15:09:57 +01002614 undoline_T *newarray = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 linenr_T oldsize;
2616 linenr_T newsize;
2617 linenr_T top, bot;
2618 linenr_T lnum;
2619 linenr_T newlnum = MAXLNUM;
2620 long i;
2621 u_entry_T *uep, *nuep;
2622 u_entry_T *newlist = NULL;
2623 int old_flags;
2624 int new_flags;
2625 pos_T namedm[NMARKS];
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002626 visualinfo_T visualinfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 int empty_buffer; /* buffer became empty */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002628 u_header_T *curhead = curbuf->b_u_curhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002630 /* Don't want autocommands using the undo structures here, they are
2631 * invalid till the end. */
2632 block_autocmds();
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002633
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002634#ifdef U_DEBUG
2635 u_check(FALSE);
2636#endif
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002637 old_flags = curhead->uh_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 new_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
2639 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
2640 setpcmark();
2641
2642 /*
2643 * save marks before undo/redo
2644 */
2645 mch_memmove(namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002646 visualinfo = curbuf->b_visual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 curbuf->b_op_start.lnum = curbuf->b_ml.ml_line_count;
2648 curbuf->b_op_start.col = 0;
2649 curbuf->b_op_end.lnum = 0;
2650 curbuf->b_op_end.col = 0;
2651
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002652 for (uep = curhead->uh_entry; uep != NULL; uep = nuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 {
2654 top = uep->ue_top;
2655 bot = uep->ue_bot;
2656 if (bot == 0)
2657 bot = curbuf->b_ml.ml_line_count + 1;
2658 if (top > curbuf->b_ml.ml_line_count || top >= bot
2659 || bot > curbuf->b_ml.ml_line_count + 1)
2660 {
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002661 unblock_autocmds();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002662 iemsg(_("E438: u_undo: line numbers wrong"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 changed(); /* don't want UNCHANGED now */
2664 return;
2665 }
2666
2667 oldsize = bot - top - 1; /* number of lines before undo */
2668 newsize = uep->ue_size; /* number of lines after undo */
2669
2670 if (top < newlnum)
2671 {
2672 /* If the saved cursor is somewhere in this undo block, move it to
2673 * the remembered position. Makes "gwap" put the cursor back
2674 * where it was. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002675 lnum = curhead->uh_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 if (lnum >= top && lnum <= top + newsize + 1)
2677 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002678 curwin->w_cursor = curhead->uh_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 newlnum = curwin->w_cursor.lnum - 1;
2680 }
2681 else
2682 {
2683 /* Use the first line that actually changed. Avoids that
2684 * undoing auto-formatting puts the cursor in the previous
2685 * line. */
2686 for (i = 0; i < newsize && i < oldsize; ++i)
Bram Moolenaarccae4672019-01-04 15:09:57 +01002687 {
2688 char_u *p = ml_get(top + 1 + i);
2689
2690 if (curbuf->b_ml.ml_line_len != uep->ue_array[i].ul_len
Bram Moolenaar964b3742019-05-24 18:54:09 +02002691 || memcmp(uep->ue_array[i].ul_line, p,
2692 curbuf->b_ml.ml_line_len) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 break;
Bram Moolenaarccae4672019-01-04 15:09:57 +01002694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 if (i == newsize && newlnum == MAXLNUM && uep->ue_next == NULL)
2696 {
2697 newlnum = top;
2698 curwin->w_cursor.lnum = newlnum + 1;
2699 }
2700 else if (i < newsize)
2701 {
2702 newlnum = top + i;
2703 curwin->w_cursor.lnum = newlnum + 1;
2704 }
2705 }
2706 }
2707
2708 empty_buffer = FALSE;
2709
2710 /* delete the lines between top and bot and save them in newarray */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002711 if (oldsize > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002713 if ((newarray = U_ALLOC_LINE(sizeof(undoline_T) * oldsize)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 {
Bram Moolenaarccae4672019-01-04 15:09:57 +01002715 do_outofmem_msg((long_u)(sizeof(undoline_T) * oldsize));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 /*
2717 * We have messed up the entry list, repair is impossible.
2718 * we have to free the rest of the list.
2719 */
2720 while (uep != NULL)
2721 {
2722 nuep = uep->ue_next;
2723 u_freeentry(uep, uep->ue_size);
2724 uep = nuep;
2725 }
2726 break;
2727 }
2728 /* delete backwards, it goes faster in most cases */
2729 for (lnum = bot - 1, i = oldsize; --i >= 0; --lnum)
2730 {
2731 /* what can we do when we run out of memory? */
Bram Moolenaarccae4672019-01-04 15:09:57 +01002732 if (u_save_line(&newarray[i], lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 do_outofmem_msg((long_u)0);
2734 /* remember we deleted the last line in the buffer, and a
2735 * dummy empty line will be inserted */
2736 if (curbuf->b_ml.ml_line_count == 1)
2737 empty_buffer = TRUE;
2738 ml_delete(lnum, FALSE);
2739 }
2740 }
Bram Moolenaar8d343302005-07-12 22:46:17 +00002741 else
2742 newarray = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743
2744 /* insert the lines in u_array between top and bot */
2745 if (newsize)
2746 {
2747 for (lnum = top, i = 0; i < newsize; ++i, ++lnum)
2748 {
Bram Moolenaarccae4672019-01-04 15:09:57 +01002749 // If the file is empty, there is an empty line 1 that we
2750 // should get rid of, by replacing it with the new line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 if (empty_buffer && lnum == 0)
Bram Moolenaar964b3742019-05-24 18:54:09 +02002752 ml_replace_len((linenr_T)1, uep->ue_array[i].ul_line,
2753 uep->ue_array[i].ul_len, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 else
Bram Moolenaar964b3742019-05-24 18:54:09 +02002755 ml_append(lnum, uep->ue_array[i].ul_line,
2756 (colnr_T)uep->ue_array[i].ul_len, FALSE);
Bram Moolenaarccae4672019-01-04 15:09:57 +01002757 vim_free(uep->ue_array[i].ul_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 }
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002759 vim_free((char_u *)uep->ue_array);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 }
2761
2762 /* adjust marks */
2763 if (oldsize != newsize)
2764 {
2765 mark_adjust(top + 1, top + oldsize, (long)MAXLNUM,
2766 (long)newsize - (long)oldsize);
2767 if (curbuf->b_op_start.lnum > top + oldsize)
2768 curbuf->b_op_start.lnum += newsize - oldsize;
2769 if (curbuf->b_op_end.lnum > top + oldsize)
2770 curbuf->b_op_end.lnum += newsize - oldsize;
2771 }
2772
2773 changed_lines(top + 1, 0, bot, newsize - oldsize);
2774
2775 /* set '[ and '] mark */
2776 if (top + 1 < curbuf->b_op_start.lnum)
2777 curbuf->b_op_start.lnum = top + 1;
2778 if (newsize == 0 && top + 1 > curbuf->b_op_end.lnum)
2779 curbuf->b_op_end.lnum = top + 1;
2780 else if (top + newsize > curbuf->b_op_end.lnum)
2781 curbuf->b_op_end.lnum = top + newsize;
2782
2783 u_newcount += newsize;
2784 u_oldcount += oldsize;
2785 uep->ue_size = oldsize;
2786 uep->ue_array = newarray;
2787 uep->ue_bot = top + newsize + 1;
2788
2789 /*
2790 * insert this entry in front of the new entry list
2791 */
2792 nuep = uep->ue_next;
2793 uep->ue_next = newlist;
2794 newlist = uep;
2795 }
2796
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002797 curhead->uh_entry = newlist;
2798 curhead->uh_flags = new_flags;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002799 if ((old_flags & UH_EMPTYBUF) && BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 curbuf->b_ml.ml_flags |= ML_EMPTY;
2801 if (old_flags & UH_CHANGED)
2802 changed();
2803 else
Bram Moolenaar009b2592004-10-24 19:18:58 +00002804#ifdef FEAT_NETBEANS_INTG
2805 /* per netbeans undo rules, keep it as modified */
2806 if (!isNetbeansModified(curbuf))
2807#endif
Bram Moolenaarc024b462019-06-08 18:07:21 +02002808 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809
2810 /*
2811 * restore marks from before undo/redo
2812 */
2813 for (i = 0; i < NMARKS; ++i)
Bram Moolenaarf65aad52015-02-17 13:43:40 +01002814 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002815 if (curhead->uh_namedm[i].lnum != 0)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002816 curbuf->b_namedm[i] = curhead->uh_namedm[i];
Bram Moolenaarf65aad52015-02-17 13:43:40 +01002817 if (namedm[i].lnum != 0)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002818 curhead->uh_namedm[i] = namedm[i];
Bram Moolenaarf65aad52015-02-17 13:43:40 +01002819 else
2820 curhead->uh_namedm[i].lnum = 0;
2821 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002822 if (curhead->uh_visual.vi_start.lnum != 0)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002823 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002824 curbuf->b_visual = curhead->uh_visual;
2825 curhead->uh_visual = visualinfo;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827
2828 /*
2829 * If the cursor is only off by one line, put it at the same position as
2830 * before starting the change (for the "o" command).
2831 * Otherwise the cursor should go to the first undone line.
2832 */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002833 if (curhead->uh_cursor.lnum + 1 == curwin->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 && curwin->w_cursor.lnum > 1)
2835 --curwin->w_cursor.lnum;
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002836 if (curwin->w_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 {
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002838 if (curhead->uh_cursor.lnum == curwin->w_cursor.lnum)
2839 {
2840 curwin->w_cursor.col = curhead->uh_cursor.col;
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002841 if (virtual_active() && curhead->uh_cursor_vcol >= 0)
2842 coladvance((colnr_T)curhead->uh_cursor_vcol);
2843 else
2844 curwin->w_cursor.coladd = 0;
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002845 }
2846 else
2847 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 else
2850 {
2851 /* We get here with the current cursor line being past the end (eg
2852 * after adding lines at the end of the file, and then undoing it).
2853 * check_cursor() will move the cursor to the last line. Move it to
2854 * the first column here. */
2855 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 }
2858
2859 /* Make sure the cursor is on an existing line and column. */
2860 check_cursor();
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002861
2862 /* Remember where we are for "g-" and ":earlier 10s". */
2863 curbuf->b_u_seq_cur = curhead->uh_seq;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002864 if (undo)
Bram Moolenaar80eaddd2017-11-11 23:37:08 +01002865 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002866 /* We are below the previous undo. However, to make ":earlier 1s"
2867 * work we compute this as being just above the just undone change. */
Bram Moolenaar80eaddd2017-11-11 23:37:08 +01002868 if (curhead->uh_next.ptr != NULL)
2869 curbuf->b_u_seq_cur = curhead->uh_next.ptr->uh_seq;
2870 else
2871 curbuf->b_u_seq_cur = 0;
2872 }
Bram Moolenaarca003e12006-03-17 23:19:38 +00002873
Bram Moolenaar730cde92010-06-27 05:18:54 +02002874 /* Remember where we are for ":earlier 1f" and ":later 1f". */
2875 if (curhead->uh_save_nr != 0)
2876 {
2877 if (undo)
2878 curbuf->b_u_save_nr_cur = curhead->uh_save_nr - 1;
2879 else
2880 curbuf->b_u_save_nr_cur = curhead->uh_save_nr;
2881 }
2882
Bram Moolenaarca003e12006-03-17 23:19:38 +00002883 /* The timestamp can be the same for multiple changes, just use the one of
2884 * the undone/redone change. */
Bram Moolenaara800b422010-06-27 01:15:55 +02002885 curbuf->b_u_time_cur = curhead->uh_time;
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002886
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002887 unblock_autocmds();
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002888#ifdef U_DEBUG
2889 u_check(FALSE);
2890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891}
2892
2893/*
2894 * If we deleted or added lines, report the number of less/more lines.
2895 * Otherwise, report the number of changes (this may be incorrect
2896 * in some cases, but it's better than nothing).
2897 */
2898 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002899u_undo_end(
2900 int did_undo, /* just did an undo */
2901 int absolute) /* used ":undo N" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902{
Bram Moolenaar89d40322006-08-29 15:30:07 +00002903 char *msgstr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002904 u_header_T *uhp;
2905 char_u msgbuf[80];
Bram Moolenaar1e607892006-03-13 22:15:53 +00002906
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907#ifdef FEAT_FOLDING
2908 if ((fdo_flags & FDO_UNDO) && KeyTyped)
2909 foldOpenCursor();
2910#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +00002911
2912 if (global_busy /* no messages now, wait until global is finished */
2913 || !messaging()) /* 'lazyredraw' set, don't do messages now */
2914 return;
2915
2916 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2917 --u_newcount;
2918
2919 u_oldcount -= u_newcount;
2920 if (u_oldcount == -1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002921 msgstr = N_("more line");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002922 else if (u_oldcount < 0)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002923 msgstr = N_("more lines");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002924 else if (u_oldcount == 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002925 msgstr = N_("line less");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002926 else if (u_oldcount > 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002927 msgstr = N_("fewer lines");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002928 else
2929 {
2930 u_oldcount = u_newcount;
2931 if (u_newcount == 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002932 msgstr = N_("change");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002933 else
Bram Moolenaar89d40322006-08-29 15:30:07 +00002934 msgstr = N_("changes");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002935 }
2936
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002937 if (curbuf->b_u_curhead != NULL)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002938 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00002939 /* For ":undo N" we prefer a "after #N" message. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002940 if (absolute && curbuf->b_u_curhead->uh_next.ptr != NULL)
Bram Moolenaardb552d602006-03-23 22:59:57 +00002941 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002942 uhp = curbuf->b_u_curhead->uh_next.ptr;
Bram Moolenaardb552d602006-03-23 22:59:57 +00002943 did_undo = FALSE;
2944 }
2945 else if (did_undo)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002946 uhp = curbuf->b_u_curhead;
2947 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002948 uhp = curbuf->b_u_curhead->uh_next.ptr;
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002949 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002950 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002951 uhp = curbuf->b_u_newhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002952
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002953 if (uhp == NULL)
2954 *msgbuf = NUL;
2955 else
2956 u_add_time(msgbuf, sizeof(msgbuf), uhp->uh_time);
2957
Bram Moolenaarb2c03502010-07-02 20:20:09 +02002958#ifdef FEAT_CONCEAL
2959 {
2960 win_T *wp;
2961
2962 FOR_ALL_WINDOWS(wp)
2963 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02002964 if (wp->w_buffer == curbuf && wp->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02002965 redraw_win_later(wp, NOT_VALID);
2966 }
2967 }
2968#endif
2969
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002970 smsg_attr_keep(0, _("%ld %s; %s #%ld %s"),
Bram Moolenaarca003e12006-03-17 23:19:38 +00002971 u_oldcount < 0 ? -u_oldcount : u_oldcount,
Bram Moolenaar89d40322006-08-29 15:30:07 +00002972 _(msgstr),
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002973 did_undo ? _("before") : _("after"),
2974 uhp == NULL ? 0L : uhp->uh_seq,
2975 msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976}
2977
2978/*
2979 * u_sync: stop adding to the current entry list
2980 */
2981 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002982u_sync(
2983 int force) /* Also sync when no_u_sync is set. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984{
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002985 /* Skip it when already synced or syncing is disabled. */
2986 if (curbuf->b_u_synced || (!force && no_u_sync > 0))
2987 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02002989 if (p_imst == IM_ON_THE_SPOT && im_is_preediting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 return; /* XIM is busy, don't break an undo sequence */
2991#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002992 if (get_undolevel() < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 curbuf->b_u_synced = TRUE; /* no entries, nothing to do */
2994 else
2995 {
2996 u_getbot(); /* compute ue_bot of previous u_save */
2997 curbuf->b_u_curhead = NULL;
2998 }
2999}
3000
3001/*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003002 * ":undolist": List the leafs of the undo tree
3003 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003004 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003005ex_undolist(exarg_T *eap UNUSED)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003006{
3007 garray_T ga;
3008 u_header_T *uhp;
3009 int mark;
3010 int nomark;
3011 int changes = 1;
3012 int i;
3013
3014 /*
3015 * 1: walk the tree to find all leafs, put the info in "ga".
3016 * 2: sort the lines
3017 * 3: display the list
3018 */
3019 mark = ++lastmark;
3020 nomark = ++lastmark;
3021 ga_init2(&ga, (int)sizeof(char *), 20);
3022
3023 uhp = curbuf->b_u_oldhead;
3024 while (uhp != NULL)
3025 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003026 if (uhp->uh_prev.ptr == NULL && uhp->uh_walk != nomark
Bram Moolenaarca003e12006-03-17 23:19:38 +00003027 && uhp->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003028 {
3029 if (ga_grow(&ga, 1) == FAIL)
3030 break;
Bram Moolenaarea391762018-04-08 13:07:22 +02003031 vim_snprintf((char *)IObuff, IOSIZE, "%6ld %7d ",
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003032 uhp->uh_seq, changes);
3033 u_add_time(IObuff + STRLEN(IObuff), IOSIZE - STRLEN(IObuff),
3034 uhp->uh_time);
Bram Moolenaara800b422010-06-27 01:15:55 +02003035 if (uhp->uh_save_nr > 0)
3036 {
Bram Moolenaardba01a02010-11-03 19:32:42 +01003037 while (STRLEN(IObuff) < 33)
Bram Moolenaara800b422010-06-27 01:15:55 +02003038 STRCAT(IObuff, " ");
3039 vim_snprintf_add((char *)IObuff, IOSIZE,
3040 " %3ld", uhp->uh_save_nr);
3041 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003042 ((char_u **)(ga.ga_data))[ga.ga_len++] = vim_strsave(IObuff);
3043 }
3044
3045 uhp->uh_walk = mark;
3046
3047 /* go down in the tree if we haven't been there */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003048 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != nomark
3049 && uhp->uh_prev.ptr->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003050 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003051 uhp = uhp->uh_prev.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003052 ++changes;
3053 }
3054
3055 /* go to alternate branch if we haven't been there */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003056 else if (uhp->uh_alt_next.ptr != NULL
3057 && uhp->uh_alt_next.ptr->uh_walk != nomark
3058 && uhp->uh_alt_next.ptr->uh_walk != mark)
3059 uhp = uhp->uh_alt_next.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003060
3061 /* go up in the tree if we haven't been there and we are at the
3062 * start of alternate branches */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003063 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
3064 && uhp->uh_next.ptr->uh_walk != nomark
3065 && uhp->uh_next.ptr->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003066 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003067 uhp = uhp->uh_next.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003068 --changes;
3069 }
3070
3071 else
3072 {
3073 /* need to backtrack; mark this node as done */
3074 uhp->uh_walk = nomark;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003075 if (uhp->uh_alt_prev.ptr != NULL)
3076 uhp = uhp->uh_alt_prev.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003077 else
3078 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003079 uhp = uhp->uh_next.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003080 --changes;
3081 }
3082 }
3083 }
3084
3085 if (ga.ga_len == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003086 msg(_("Nothing to undo"));
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003087 else
3088 {
3089 sort_strings((char_u **)ga.ga_data, ga.ga_len);
3090
3091 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01003092 msg_puts_attr(_("number changes when saved"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01003093 HL_ATTR(HLF_T));
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003094 for (i = 0; i < ga.ga_len && !got_int; ++i)
3095 {
3096 msg_putchar('\n');
3097 if (got_int)
3098 break;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003099 msg_puts(((char **)ga.ga_data)[i]);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003100 }
3101 msg_end();
3102
3103 ga_clear_strings(&ga);
3104 }
3105}
3106
3107/*
3108 * Put the timestamp of an undo header in "buf[buflen]" in a nice format.
3109 */
3110 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003111u_add_time(char_u *buf, size_t buflen, time_t tt)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003112{
3113#ifdef HAVE_STRFTIME
Bram Moolenaar63d25552019-05-10 21:28:38 +02003114 struct tm tmval;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003115 struct tm *curtime;
3116
Bram Moolenaar170b10b2016-07-29 16:15:27 +02003117 if (vim_time() - tt >= 100)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003118 {
Bram Moolenaardb517302019-06-18 22:53:24 +02003119 curtime = vim_localtime(&tt, &tmval);
Bram Moolenaar170b10b2016-07-29 16:15:27 +02003120 if (vim_time() - tt < (60L * 60L * 12L))
Bram Moolenaardba01a02010-11-03 19:32:42 +01003121 /* within 12 hours */
3122 (void)strftime((char *)buf, buflen, "%H:%M:%S", curtime);
Bram Moolenaardba01a02010-11-03 19:32:42 +01003123 else
Bram Moolenaarb9ce83e2012-08-23 12:59:02 +02003124 /* longer ago */
Bram Moolenaar5e3d6ca2011-01-22 21:25:11 +01003125 (void)strftime((char *)buf, buflen, "%Y/%m/%d %H:%M:%S", curtime);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003126 }
3127 else
3128#endif
Bram Moolenaarfd6100b2018-08-21 17:07:45 +02003129 {
3130 long seconds = (long)(vim_time() - tt);
3131
3132 vim_snprintf((char *)buf, buflen,
3133 NGETTEXT("%ld second ago", "%ld seconds ago", seconds),
3134 seconds);
3135 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003136}
3137
3138/*
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003139 * ":undojoin": continue adding to the last entry list
3140 */
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003141 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003142ex_undojoin(exarg_T *eap UNUSED)
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003143{
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003144 if (curbuf->b_u_newhead == NULL)
3145 return; /* nothing changed before */
Bram Moolenaar57657d82006-04-21 22:12:41 +00003146 if (curbuf->b_u_curhead != NULL)
3147 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003148 emsg(_("E790: undojoin is not allowed after undo"));
Bram Moolenaar57657d82006-04-21 22:12:41 +00003149 return;
3150 }
3151 if (!curbuf->b_u_synced)
3152 return; /* already unsynced */
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003153 if (get_undolevel() < 0)
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003154 return; /* no entries, nothing to do */
3155 else
Bram Moolenaar5e4e1b12017-01-17 22:09:45 +01003156 /* Append next change to the last entry */
3157 curbuf->b_u_synced = FALSE;
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003158}
3159
3160/*
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003161 * Called after writing or reloading the file and setting b_changed to FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 * Now an undo means that the buffer is modified.
3163 */
3164 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003165u_unchanged(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166{
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003167 u_unch_branch(buf->b_u_oldhead);
3168 buf->b_did_warn = FALSE;
3169}
3170
Bram Moolenaar730cde92010-06-27 05:18:54 +02003171/*
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003172 * After reloading a buffer which was saved for 'undoreload': Find the first
3173 * line that was changed and set the cursor there.
3174 */
3175 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003176u_find_first_changed(void)
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003177{
3178 u_header_T *uhp = curbuf->b_u_newhead;
3179 u_entry_T *uep;
3180 linenr_T lnum;
3181
3182 if (curbuf->b_u_curhead != NULL || uhp == NULL)
3183 return; /* undid something in an autocmd? */
3184
3185 /* Check that the last undo block was for the whole file. */
3186 uep = uhp->uh_entry;
3187 if (uep->ue_top != 0 || uep->ue_bot != 0)
3188 return;
3189
3190 for (lnum = 1; lnum < curbuf->b_ml.ml_line_count
3191 && lnum <= uep->ue_size; ++lnum)
Bram Moolenaarccae4672019-01-04 15:09:57 +01003192 {
3193 char_u *p = ml_get_buf(curbuf, lnum, FALSE);
3194
3195 if (uep->ue_array[lnum - 1].ul_len != curbuf->b_ml.ml_line_len
3196 || memcmp(p, uep->ue_array[lnum - 1].ul_line, uep->ue_array[lnum - 1].ul_len) != 0)
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003197 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003198 CLEAR_POS(&(uhp->uh_cursor));
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003199 uhp->uh_cursor.lnum = lnum;
3200 return;
3201 }
Bram Moolenaarccae4672019-01-04 15:09:57 +01003202 }
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003203 if (curbuf->b_ml.ml_line_count != uep->ue_size)
3204 {
3205 /* lines added or deleted at the end, put the cursor there */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003206 CLEAR_POS(&(uhp->uh_cursor));
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003207 uhp->uh_cursor.lnum = lnum;
3208 }
3209}
3210
3211/*
Bram Moolenaar730cde92010-06-27 05:18:54 +02003212 * Increase the write count, store it in the last undo header, what would be
3213 * used for "u".
3214 */
3215 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003216u_update_save_nr(buf_T *buf)
Bram Moolenaar730cde92010-06-27 05:18:54 +02003217{
3218 u_header_T *uhp;
3219
3220 ++buf->b_u_save_nr_last;
3221 buf->b_u_save_nr_cur = buf->b_u_save_nr_last;
3222 uhp = buf->b_u_curhead;
3223 if (uhp != NULL)
3224 uhp = uhp->uh_next.ptr;
3225 else
3226 uhp = buf->b_u_newhead;
3227 if (uhp != NULL)
3228 uhp->uh_save_nr = buf->b_u_save_nr_last;
3229}
3230
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003231 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003232u_unch_branch(u_header_T *uhp)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003233{
Bram Moolenaar1e607892006-03-13 22:15:53 +00003234 u_header_T *uh;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003236 for (uh = uhp; uh != NULL; uh = uh->uh_prev.ptr)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003237 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 uh->uh_flags |= UH_CHANGED;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003239 if (uh->uh_alt_next.ptr != NULL)
3240 u_unch_branch(uh->uh_alt_next.ptr); /* recursive */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242}
3243
3244/*
3245 * Get pointer to last added entry.
3246 * If it's not valid, give an error message and return NULL.
3247 */
3248 static u_entry_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003249u_get_headentry(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250{
3251 if (curbuf->b_u_newhead == NULL || curbuf->b_u_newhead->uh_entry == NULL)
3252 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003253 iemsg(_("E439: undo list corrupt"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 return NULL;
3255 }
3256 return curbuf->b_u_newhead->uh_entry;
3257}
3258
3259/*
3260 * u_getbot(): compute the line number of the previous u_save
3261 * It is called only when b_u_synced is FALSE.
3262 */
3263 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003264u_getbot(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265{
3266 u_entry_T *uep;
3267 linenr_T extra;
3268
3269 uep = u_get_headentry(); /* check for corrupt undo list */
3270 if (uep == NULL)
3271 return;
3272
3273 uep = curbuf->b_u_newhead->uh_getbot_entry;
3274 if (uep != NULL)
3275 {
3276 /*
3277 * the new ue_bot is computed from the number of lines that has been
3278 * inserted (0 - deleted) since calling u_save. This is equal to the
3279 * old line count subtracted from the current line count.
3280 */
3281 extra = curbuf->b_ml.ml_line_count - uep->ue_lcount;
3282 uep->ue_bot = uep->ue_top + uep->ue_size + 1 + extra;
3283 if (uep->ue_bot < 1 || uep->ue_bot > curbuf->b_ml.ml_line_count)
3284 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003285 iemsg(_("E440: undo line missing"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 uep->ue_bot = uep->ue_top + 1; /* assume all lines deleted, will
3287 * get all the old lines back
3288 * without deleting the current
3289 * ones */
3290 }
3291
3292 curbuf->b_u_newhead->uh_getbot_entry = NULL;
3293 }
3294
3295 curbuf->b_u_synced = TRUE;
3296}
3297
3298/*
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003299 * Free one header "uhp" and its entry list and adjust the pointers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 */
3301 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003302u_freeheader(
3303 buf_T *buf,
3304 u_header_T *uhp,
3305 u_header_T **uhpp) /* if not NULL reset when freeing this header */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306{
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003307 u_header_T *uhap;
3308
Bram Moolenaar1e607892006-03-13 22:15:53 +00003309 /* When there is an alternate redo list free that branch completely,
3310 * because we can never go there. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003311 if (uhp->uh_alt_next.ptr != NULL)
3312 u_freebranch(buf, uhp->uh_alt_next.ptr, uhpp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003314 if (uhp->uh_alt_prev.ptr != NULL)
3315 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316
Bram Moolenaar1e607892006-03-13 22:15:53 +00003317 /* Update the links in the list to remove the header. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003318 if (uhp->uh_next.ptr == NULL)
3319 buf->b_u_oldhead = uhp->uh_prev.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003321 uhp->uh_next.ptr->uh_prev.ptr = uhp->uh_prev.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003323 if (uhp->uh_prev.ptr == NULL)
3324 buf->b_u_newhead = uhp->uh_next.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003326 for (uhap = uhp->uh_prev.ptr; uhap != NULL;
3327 uhap = uhap->uh_alt_next.ptr)
3328 uhap->uh_next.ptr = uhp->uh_next.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329
Bram Moolenaar1e607892006-03-13 22:15:53 +00003330 u_freeentries(buf, uhp, uhpp);
3331}
3332
3333/*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003334 * Free an alternate branch and any following alternate branches.
Bram Moolenaar1e607892006-03-13 22:15:53 +00003335 */
3336 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003337u_freebranch(
3338 buf_T *buf,
3339 u_header_T *uhp,
3340 u_header_T **uhpp) /* if not NULL reset when freeing this header */
Bram Moolenaar1e607892006-03-13 22:15:53 +00003341{
3342 u_header_T *tofree, *next;
3343
Bram Moolenaar07d06772007-11-10 21:51:15 +00003344 /* If this is the top branch we may need to use u_freeheader() to update
3345 * all the pointers. */
3346 if (uhp == buf->b_u_oldhead)
3347 {
Bram Moolenaaraa887322013-11-07 03:04:11 +01003348 while (buf->b_u_oldhead != NULL)
3349 u_freeheader(buf, buf->b_u_oldhead, uhpp);
Bram Moolenaar07d06772007-11-10 21:51:15 +00003350 return;
3351 }
3352
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003353 if (uhp->uh_alt_prev.ptr != NULL)
3354 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +00003355
3356 next = uhp;
3357 while (next != NULL)
3358 {
3359 tofree = next;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003360 if (tofree->uh_alt_next.ptr != NULL)
3361 u_freebranch(buf, tofree->uh_alt_next.ptr, uhpp); /* recursive */
3362 next = tofree->uh_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00003363 u_freeentries(buf, tofree, uhpp);
3364 }
3365}
3366
3367/*
3368 * Free all the undo entries for one header and the header itself.
3369 * This means that "uhp" is invalid when returning.
3370 */
3371 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003372u_freeentries(
3373 buf_T *buf,
3374 u_header_T *uhp,
3375 u_header_T **uhpp) /* if not NULL reset when freeing this header */
Bram Moolenaar1e607892006-03-13 22:15:53 +00003376{
3377 u_entry_T *uep, *nuep;
3378
3379 /* Check for pointers to the header that become invalid now. */
3380 if (buf->b_u_curhead == uhp)
3381 buf->b_u_curhead = NULL;
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003382 if (buf->b_u_newhead == uhp)
3383 buf->b_u_newhead = NULL; /* freeing the newest entry */
Bram Moolenaar1e607892006-03-13 22:15:53 +00003384 if (uhpp != NULL && uhp == *uhpp)
3385 *uhpp = NULL;
3386
3387 for (uep = uhp->uh_entry; uep != NULL; uep = nuep)
3388 {
3389 nuep = uep->ue_next;
3390 u_freeentry(uep, uep->ue_size);
3391 }
3392
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003393#ifdef U_DEBUG
3394 uhp->uh_magic = 0;
3395#endif
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003396 vim_free((char_u *)uhp);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003397 --buf->b_u_numhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398}
3399
3400/*
3401 * free entry 'uep' and 'n' lines in uep->ue_array[]
3402 */
3403 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003404u_freeentry(u_entry_T *uep, long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405{
Bram Moolenaar8d343302005-07-12 22:46:17 +00003406 while (n > 0)
Bram Moolenaarccae4672019-01-04 15:09:57 +01003407 vim_free(uep->ue_array[--n].ul_line);
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003408 vim_free((char_u *)uep->ue_array);
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003409#ifdef U_DEBUG
3410 uep->ue_magic = 0;
3411#endif
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003412 vim_free((char_u *)uep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413}
3414
3415/*
3416 * invalidate the undo buffer; called when storage has already been released
3417 */
3418 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003419u_clearall(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420{
3421 buf->b_u_newhead = buf->b_u_oldhead = buf->b_u_curhead = NULL;
3422 buf->b_u_synced = TRUE;
3423 buf->b_u_numhead = 0;
Bram Moolenaarccae4672019-01-04 15:09:57 +01003424 buf->b_u_line_ptr.ul_line = NULL;
3425 buf->b_u_line_ptr.ul_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 buf->b_u_line_lnum = 0;
3427}
3428
3429/*
Bram Moolenaarccae4672019-01-04 15:09:57 +01003430 * Save the line "lnum" for the "U" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 */
3432 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003433u_saveline(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434{
3435 if (lnum == curbuf->b_u_line_lnum) /* line is already saved */
3436 return;
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00003437 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) /* should never happen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 return;
3439 u_clearline();
3440 curbuf->b_u_line_lnum = lnum;
3441 if (curwin->w_cursor.lnum == lnum)
3442 curbuf->b_u_line_colnr = curwin->w_cursor.col;
3443 else
3444 curbuf->b_u_line_colnr = 0;
Bram Moolenaarccae4672019-01-04 15:09:57 +01003445 if (u_save_line(&curbuf->b_u_line_ptr, lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 do_outofmem_msg((long_u)0);
3447}
3448
3449/*
3450 * clear the line saved for the "U" command
3451 * (this is used externally for crossing a line while in insert mode)
3452 */
3453 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003454u_clearline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455{
Bram Moolenaarccae4672019-01-04 15:09:57 +01003456 if (curbuf->b_u_line_ptr.ul_line != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 {
Bram Moolenaarccae4672019-01-04 15:09:57 +01003458 VIM_CLEAR(curbuf->b_u_line_ptr.ul_line);
3459 curbuf->b_u_line_ptr.ul_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 curbuf->b_u_line_lnum = 0;
3461 }
3462}
3463
3464/*
3465 * Implementation of the "U" command.
3466 * Differentiation from vi: "U" can be undone with the next "U".
3467 * We also allow the cursor to be in another line.
Bram Moolenaard04b7502010-07-08 22:27:55 +02003468 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 */
3470 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003471u_undoline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472{
Bram Moolenaarccae4672019-01-04 15:09:57 +01003473 colnr_T t;
3474 undoline_T oldp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475
3476 if (undo_off)
3477 return;
3478
Bram Moolenaarccae4672019-01-04 15:09:57 +01003479 if (curbuf->b_u_line_ptr.ul_line == NULL
Bram Moolenaare3300c82008-02-13 14:21:38 +00003480 || curbuf->b_u_line_lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 {
3482 beep_flush();
3483 return;
3484 }
Bram Moolenaare3300c82008-02-13 14:21:38 +00003485
Bram Moolenaarccae4672019-01-04 15:09:57 +01003486 // first save the line for the 'u' command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 if (u_savecommon(curbuf->b_u_line_lnum - 1,
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003488 curbuf->b_u_line_lnum + 1, (linenr_T)0, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 return;
Bram Moolenaarccae4672019-01-04 15:09:57 +01003490 if (u_save_line(&oldp, curbuf->b_u_line_lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 {
3492 do_outofmem_msg((long_u)0);
3493 return;
3494 }
Bram Moolenaarccae4672019-01-04 15:09:57 +01003495 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 +00003496 changed_bytes(curbuf->b_u_line_lnum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 curbuf->b_u_line_ptr = oldp;
3498
3499 t = curbuf->b_u_line_colnr;
3500 if (curwin->w_cursor.lnum == curbuf->b_u_line_lnum)
3501 curbuf->b_u_line_colnr = curwin->w_cursor.col;
3502 curwin->w_cursor.col = t;
3503 curwin->w_cursor.lnum = curbuf->b_u_line_lnum;
Bram Moolenaare3300c82008-02-13 14:21:38 +00003504 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505}
3506
3507/*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003508 * Free all allocated memory blocks for the buffer 'buf'.
3509 */
3510 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003511u_blockfree(buf_T *buf)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003512{
Bram Moolenaar1e607892006-03-13 22:15:53 +00003513 while (buf->b_u_oldhead != NULL)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003514 u_freeheader(buf, buf->b_u_oldhead, NULL);
Bram Moolenaarccae4672019-01-04 15:09:57 +01003515 vim_free(buf->b_u_line_ptr.ul_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516}
3517
3518/*
3519 * Check if the 'modified' flag is set, or 'ff' has changed (only need to
3520 * check the first character, because it can only be "dos", "unix" or "mac").
3521 * "nofile" and "scratch" type buffers are considered to always be unchanged.
Bram Moolenaarf405c8f2017-12-09 19:51:49 +01003522 * Also considers a buffer changed when a terminal window contains a running
3523 * job.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 */
3525 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003526bufIsChanged(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527{
Bram Moolenaareb44a682017-08-03 22:44:55 +02003528#ifdef FEAT_TERMINAL
3529 if (term_job_running(buf->b_term))
3530 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531#endif
Bram Moolenaarf405c8f2017-12-09 19:51:49 +01003532 return bufIsChangedNotTerm(buf);
3533}
3534
3535/*
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01003536 * Return TRUE if any buffer has changes. Also buffers that are not written.
3537 */
3538 int
3539anyBufIsChanged(void)
3540{
3541 buf_T *buf;
3542
3543 FOR_ALL_BUFFERS(buf)
3544 if (bufIsChanged(buf))
3545 return TRUE;
Bram Moolenaard6c3f1f2019-03-26 00:31:21 +01003546 return FALSE;
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01003547}
3548
3549/*
Bram Moolenaarf405c8f2017-12-09 19:51:49 +01003550 * Like bufIsChanged() but ignoring a terminal window.
3551 */
3552 int
3553bufIsChangedNotTerm(buf_T *buf)
3554{
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003555 // In a "prompt" buffer we do respect 'modified', so that we can control
3556 // closing the window by setting or resetting that option.
3557 return (!bt_dontwrite(buf) || bt_prompt(buf))
Bram Moolenaareb44a682017-08-03 22:44:55 +02003558 && (buf->b_changed || file_ff_differs(buf, TRUE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559}
3560
3561 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003562curbufIsChanged(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563{
Bram Moolenaareb44a682017-08-03 22:44:55 +02003564 return bufIsChanged(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565}
Bram Moolenaara800b422010-06-27 01:15:55 +02003566
3567#if defined(FEAT_EVAL) || defined(PROTO)
3568/*
3569 * For undotree(): Append the list of undo blocks at "first_uhp" to "list".
3570 * Recursive.
3571 */
3572 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003573u_eval_tree(u_header_T *first_uhp, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02003574{
3575 u_header_T *uhp = first_uhp;
3576 dict_T *dict;
3577
3578 while (uhp != NULL)
3579 {
3580 dict = dict_alloc();
3581 if (dict == NULL)
3582 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003583 dict_add_number(dict, "seq", uhp->uh_seq);
3584 dict_add_number(dict, "time", (long)uhp->uh_time);
Bram Moolenaara800b422010-06-27 01:15:55 +02003585 if (uhp == curbuf->b_u_newhead)
Bram Moolenaare0be1672018-07-08 16:50:37 +02003586 dict_add_number(dict, "newhead", 1);
Bram Moolenaara800b422010-06-27 01:15:55 +02003587 if (uhp == curbuf->b_u_curhead)
Bram Moolenaare0be1672018-07-08 16:50:37 +02003588 dict_add_number(dict, "curhead", 1);
Bram Moolenaara800b422010-06-27 01:15:55 +02003589 if (uhp->uh_save_nr > 0)
Bram Moolenaare0be1672018-07-08 16:50:37 +02003590 dict_add_number(dict, "save", uhp->uh_save_nr);
Bram Moolenaara800b422010-06-27 01:15:55 +02003591
3592 if (uhp->uh_alt_next.ptr != NULL)
3593 {
3594 list_T *alt_list = list_alloc();
3595
3596 if (alt_list != NULL)
3597 {
3598 /* Recursive call to add alternate undo tree. */
3599 u_eval_tree(uhp->uh_alt_next.ptr, alt_list);
3600 dict_add_list(dict, "alt", alt_list);
3601 }
3602 }
3603
3604 list_append_dict(list, dict);
3605 uhp = uhp->uh_prev.ptr;
3606 }
3607}
3608#endif