blob: 033fa6ea0d8fe99d36ff7b43b82197b48af32727 [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 Moolenaare38eab22019-12-05 21:50:01 +010078// 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
Bram Moolenaarfecb6602007-10-01 20:54:15 +000083
Bram Moolenaare38eab22019-12-05 21:50:01 +010084// Size of buffer used for encryption.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020085#define CRYPT_BUF_SIZE 8192
86
Bram Moolenaar071d4272004-06-13 20:20:40 +000087#include "vim.h"
88
Bram Moolenaare38eab22019-12-05 21:50:01 +010089// Structure passed around between functions.
90// Avoids passing cryptstate_T when encryption not available.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020091typedef struct {
92 buf_T *bi_buf;
93 FILE *bi_fp;
94#ifdef FEAT_CRYPT
95 cryptstate_T *bi_state;
Bram Moolenaare38eab22019-12-05 21:50:01 +010096 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
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020099#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);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100109static void u_freeheader(buf_T *buf, u_header_T *uhp, u_header_T **uhpp);
110static void u_freebranch(buf_T *buf, u_header_T *uhp, u_header_T **uhpp);
111static void u_freeentries(buf_T *buf, u_header_T *uhp, u_header_T **uhpp);
112static void u_freeentry(u_entry_T *, long);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200113#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar1d6fbe62016-02-19 22:46:34 +0100114# ifdef FEAT_CRYPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100115static int undo_flush(bufinfo_T *bi);
Bram Moolenaar1d6fbe62016-02-19 22:46:34 +0100116# endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100117static int undo_read(bufinfo_T *bi, char_u *buffer, size_t size);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100118static int serialize_uep(bufinfo_T *bi, u_entry_T *uep);
119static u_entry_T *unserialize_uep(bufinfo_T *bi, int *error, char_u *file_name);
120static void serialize_pos(bufinfo_T *bi, pos_T pos);
121static void unserialize_pos(bufinfo_T *bi, pos_T *pos);
122static void serialize_visualinfo(bufinfo_T *bi, visualinfo_T *info);
123static void unserialize_visualinfo(bufinfo_T *bi, visualinfo_T *info);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200124#endif
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200125static void u_saveline(linenr_T lnum);
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 Moolenaare38eab22019-12-05 21:50:01 +0100129// 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 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100174 // 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
Bram Moolenaare38eab22019-12-05 21:50:01 +0100188 // Check the undo tree at this header.
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000189 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
Bram Moolenaare38eab22019-12-05 21:50:01 +0100198 // 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
Bram Moolenaare38eab22019-12-05 21:50:01 +0100201 // 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{
Bram Moolenaare38eab22019-12-05 21:50:01 +0100316 // Don't allow changes when 'modifiable' is off.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000317 if (!curbuf->b_p_ma)
318 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200319 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000320 return FALSE;
321 }
322
323#ifdef HAVE_SANDBOX
Bram Moolenaare38eab22019-12-05 21:50:01 +0100324 // In the sandbox it's not allowed to change the text.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000325 if (sandbox != 0)
326 {
Bram Moolenaard8e44472021-07-21 22:20:33 +0200327 emsg(_(e_not_allowed_in_sandbox));
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000328 return FALSE;
329 }
330#endif
331
Bram Moolenaare38eab22019-12-05 21:50:01 +0100332 // Don't allow changes in the buffer while editing the cmdline. The
333 // caller of getcmdline() may get confused.
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200334 if (textwinlock != 0 || textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000335 {
Bram Moolenaarff06f282020-04-21 22:01:14 +0200336 emsg(_(e_textlock));
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 Moolenaar32aa1022019-11-02 22:54:41 +0100344 * Get the undolevel value for the current buffer.
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100345 */
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
Bram Moolenaard5c2c772020-05-30 16:17:33 +0200378#ifdef FEAT_PROP_POPUP
Bram Moolenaarccae4672019-01-04 15:09:57 +0100379/*
Bram Moolenaara9d4b842020-05-30 14:46:52 +0200380 * return TRUE if line "lnum" has text property "flags".
381 */
382 static int
383has_prop_w_flags(linenr_T lnum, int flags)
384{
385 char_u *props;
386 int i;
387 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
388
389 for (i = 0; i < proplen; ++i)
390 {
391 textprop_T prop;
392
393 mch_memmove(&prop, props + i * sizeof prop, sizeof prop);
394 if (prop.tp_flags & flags)
395 return TRUE;
396 }
397 return FALSE;
398}
Bram Moolenaard5c2c772020-05-30 16:17:33 +0200399#endif
Bram Moolenaara9d4b842020-05-30 14:46:52 +0200400
401/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200402 * Common code for various ways to save text before a change.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200403 * "top" is the line above the first changed line.
404 * "bot" is the line below the last changed line.
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200405 * "newbot" is the new bottom line. Use zero when not known.
406 * "reload" is TRUE when saving for a buffer reload.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200407 * Careful: may trigger autocommands that reload the buffer.
408 * Returns FAIL when lines could not be saved, OK otherwise.
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200409 */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200410 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100411u_savecommon(
412 linenr_T top,
413 linenr_T bot,
414 linenr_T newbot,
415 int reload)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416{
Bram Moolenaar1e607892006-03-13 22:15:53 +0000417 linenr_T lnum;
418 long i;
419 u_header_T *uhp;
420 u_header_T *old_curhead;
421 u_entry_T *uep;
422 u_entry_T *prev_uep;
423 long size;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200425 if (!reload)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100427 // When making changes is not allowed return FAIL. It's a crude way
428 // to make all change commands fail.
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200429 if (!undo_allowed())
Bram Moolenaar009b2592004-10-24 19:18:58 +0000430 return FAIL;
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200431
432#ifdef FEAT_NETBEANS_INTG
433 /*
434 * Netbeans defines areas that cannot be modified. Bail out here when
435 * trying to change text in a guarded area.
436 */
437 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +0000438 {
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200439 if (netbeans_is_guarded(top, bot))
440 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100441 emsg(_(e_guarded));
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200442 return FAIL;
443 }
444 if (curbuf->b_p_ro)
445 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100446 emsg(_(e_nbreadonly));
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200447 return FAIL;
448 }
Bram Moolenaar009b2592004-10-24 19:18:58 +0000449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200451#ifdef FEAT_TERMINAL
Bram Moolenaare38eab22019-12-05 21:50:01 +0100452 // A change in a terminal buffer removes the highlighting.
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200453 term_change_in_curbuf();
454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200456 /*
457 * Saving text for undo means we are going to make a change. Give a
458 * warning for a read-only file before making the change, so that the
459 * FileChangedRO event can replace the buffer with a read-write version
460 * (e.g., obtained from a source control system).
461 */
462 change_warning(0);
463 if (bot > curbuf->b_ml.ml_line_count + 1)
464 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100465 // This happens when the FileChangedRO autocommand changes the
466 // file in a way it becomes shorter.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100467 emsg(_("E881: Line count changed unexpectedly"));
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200468 return FAIL;
469 }
Bram Moolenaard04b7502010-07-08 22:27:55 +0200470 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200471
472#ifdef U_DEBUG
473 u_check(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474#endif
475
Bram Moolenaara9d4b842020-05-30 14:46:52 +0200476#ifdef FEAT_PROP_POPUP
477 // Include the line above if a text property continues from it.
478 // Include the line below if a text property continues to it.
479 if (bot - top > 1)
480 {
481 if (top > 0 && has_prop_w_flags(top + 1, TP_FLAG_CONT_PREV))
482 --top;
483 if (bot <= curbuf->b_ml.ml_line_count
484 && has_prop_w_flags(bot - 1, TP_FLAG_CONT_NEXT))
485 {
486 ++bot;
487 if (newbot != 0)
488 ++newbot;
489 }
490 }
491#endif
492
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 size = bot - top - 1;
494
495 /*
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200496 * If curbuf->b_u_synced == TRUE make a new header.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 */
498 if (curbuf->b_u_synced)
499 {
500#ifdef FEAT_JUMPLIST
Bram Moolenaare38eab22019-12-05 21:50:01 +0100501 // Need to create new entry in b_changelist.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 curbuf->b_new_change = TRUE;
503#endif
504
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100505 if (get_undolevel() >= 0)
Bram Moolenaar1e607892006-03-13 22:15:53 +0000506 {
507 /*
508 * Make a new header entry. Do this first so that we don't mess
509 * up the undo info when out of memory.
510 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200511 uhp = U_ALLOC_LINE(sizeof(u_header_T));
Bram Moolenaar1e607892006-03-13 22:15:53 +0000512 if (uhp == NULL)
513 goto nomem;
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000514#ifdef U_DEBUG
515 uhp->uh_magic = UH_MAGIC;
516#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +0000517 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000518 else
519 uhp = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000520
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 /*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000522 * If we undid more than we redid, move the entry lists before and
523 * including curbuf->b_u_curhead to an alternate branch.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 */
Bram Moolenaar1e607892006-03-13 22:15:53 +0000525 old_curhead = curbuf->b_u_curhead;
526 if (old_curhead != NULL)
527 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200528 curbuf->b_u_newhead = old_curhead->uh_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000529 curbuf->b_u_curhead = NULL;
530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531
532 /*
533 * free headers to keep the size right
534 */
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100535 while (curbuf->b_u_numhead > get_undolevel()
536 && curbuf->b_u_oldhead != NULL)
Bram Moolenaar1e607892006-03-13 22:15:53 +0000537 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000538 u_header_T *uhfree = curbuf->b_u_oldhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000539
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000540 if (uhfree == old_curhead)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100541 // Can't reconnect the branch, delete all of it.
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000542 u_freebranch(curbuf, uhfree, &old_curhead);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200543 else if (uhfree->uh_alt_next.ptr == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100544 // There is no branch, only free one header.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000545 u_freeheader(curbuf, uhfree, &old_curhead);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000546 else
547 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100548 // Free the oldest alternate branch as a whole.
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200549 while (uhfree->uh_alt_next.ptr != NULL)
550 uhfree = uhfree->uh_alt_next.ptr;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000551 u_freebranch(curbuf, uhfree, &old_curhead);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000552 }
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000553#ifdef U_DEBUG
554 u_check(TRUE);
555#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +0000556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557
Bram Moolenaare38eab22019-12-05 21:50:01 +0100558 if (uhp == NULL) // no undo at all
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 {
Bram Moolenaar1e607892006-03-13 22:15:53 +0000560 if (old_curhead != NULL)
561 u_freebranch(curbuf, old_curhead, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 curbuf->b_u_synced = FALSE;
563 return OK;
564 }
565
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200566 uhp->uh_prev.ptr = NULL;
567 uhp->uh_next.ptr = curbuf->b_u_newhead;
568 uhp->uh_alt_next.ptr = old_curhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000569 if (old_curhead != NULL)
570 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200571 uhp->uh_alt_prev.ptr = old_curhead->uh_alt_prev.ptr;
572 if (uhp->uh_alt_prev.ptr != NULL)
573 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = uhp;
574 old_curhead->uh_alt_prev.ptr = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000575 if (curbuf->b_u_oldhead == old_curhead)
576 curbuf->b_u_oldhead = uhp;
577 }
Bram Moolenaar89ed3df2007-01-09 19:23:12 +0000578 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200579 uhp->uh_alt_prev.ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 if (curbuf->b_u_newhead != NULL)
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200581 curbuf->b_u_newhead->uh_prev.ptr = uhp;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000582
Bram Moolenaarca003e12006-03-17 23:19:38 +0000583 uhp->uh_seq = ++curbuf->b_u_seq_last;
584 curbuf->b_u_seq_cur = uhp->uh_seq;
Bram Moolenaar170b10b2016-07-29 16:15:27 +0200585 uhp->uh_time = vim_time();
Bram Moolenaara800b422010-06-27 01:15:55 +0200586 uhp->uh_save_nr = 0;
587 curbuf->b_u_time_cur = uhp->uh_time + 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000588
Bram Moolenaar1e607892006-03-13 22:15:53 +0000589 uhp->uh_walk = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 uhp->uh_entry = NULL;
591 uhp->uh_getbot_entry = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100592 uhp->uh_cursor = curwin->w_cursor; // save cursor pos. for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 if (virtual_active() && curwin->w_cursor.coladd > 0)
594 uhp->uh_cursor_vcol = getviscol();
595 else
596 uhp->uh_cursor_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597
Bram Moolenaare38eab22019-12-05 21:50:01 +0100598 // save changed and buffer empty flag for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 uhp->uh_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
600 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
601
Bram Moolenaare38eab22019-12-05 21:50:01 +0100602 // save named marks and Visual marks for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 mch_memmove(uhp->uh_namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000604 uhp->uh_visual = curbuf->b_visual;
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000605
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 curbuf->b_u_newhead = uhp;
607 if (curbuf->b_u_oldhead == NULL)
608 curbuf->b_u_oldhead = uhp;
609 ++curbuf->b_u_numhead;
610 }
611 else
612 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100613 if (get_undolevel() < 0) // no undo at all
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 return OK;
615
616 /*
617 * When saving a single line, and it has been saved just before, it
618 * doesn't make sense saving it again. Saves a lot of memory when
619 * making lots of changes inside the same line.
620 * This is only possible if the previous change didn't increase or
621 * decrease the number of lines.
622 * Check the ten last changes. More doesn't make sense and takes too
623 * long.
624 */
625 if (size == 1)
626 {
627 uep = u_get_headentry();
628 prev_uep = NULL;
629 for (i = 0; i < 10; ++i)
630 {
631 if (uep == NULL)
632 break;
633
Bram Moolenaare38eab22019-12-05 21:50:01 +0100634 // If lines have been inserted/deleted we give up.
635 // Also when the line was included in a multi-line save.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 if ((curbuf->b_u_newhead->uh_getbot_entry != uep
637 ? (uep->ue_top + uep->ue_size + 1
638 != (uep->ue_bot == 0
639 ? curbuf->b_ml.ml_line_count + 1
640 : uep->ue_bot))
641 : uep->ue_lcount != curbuf->b_ml.ml_line_count)
642 || (uep->ue_size > 1
643 && top >= uep->ue_top
644 && top + 2 <= uep->ue_top + uep->ue_size + 1))
645 break;
646
Bram Moolenaare38eab22019-12-05 21:50:01 +0100647 // If it's the same line we can skip saving it again.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 if (uep->ue_size == 1 && uep->ue_top == top)
649 {
650 if (i > 0)
651 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100652 // It's not the last entry: get ue_bot for the last
653 // entry now. Following deleted/inserted lines go to
654 // the re-used entry.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 u_getbot();
656 curbuf->b_u_synced = FALSE;
657
Bram Moolenaare38eab22019-12-05 21:50:01 +0100658 // Move the found entry to become the last entry. The
659 // order of undo/redo doesn't matter for the entries
660 // we move it over, since they don't change the line
661 // count and don't include this line. It does matter
662 // for the found entry if the line count is changed by
663 // the executed command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 prev_uep->ue_next = uep->ue_next;
665 uep->ue_next = curbuf->b_u_newhead->uh_entry;
666 curbuf->b_u_newhead->uh_entry = uep;
667 }
668
Bram Moolenaare38eab22019-12-05 21:50:01 +0100669 // The executed command may change the line count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 if (newbot != 0)
671 uep->ue_bot = newbot;
672 else if (bot > curbuf->b_ml.ml_line_count)
673 uep->ue_bot = 0;
674 else
675 {
676 uep->ue_lcount = curbuf->b_ml.ml_line_count;
677 curbuf->b_u_newhead->uh_getbot_entry = uep;
678 }
679 return OK;
680 }
681 prev_uep = uep;
682 uep = uep->ue_next;
683 }
684 }
685
Bram Moolenaare38eab22019-12-05 21:50:01 +0100686 // find line number for ue_bot for previous u_save()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 u_getbot();
688 }
689
Bram Moolenaar4f974752019-02-17 17:44:42 +0100690#if !defined(UNIX) && !defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100692 * With Amiga we can't handle big undo's, because
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 * then u_alloc_line would have to allocate a block larger than 32K
694 */
695 if (size >= 8000)
696 goto nomem;
697#endif
698
699 /*
700 * add lines in front of entry list
701 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200702 uep = U_ALLOC_LINE(sizeof(u_entry_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 if (uep == NULL)
704 goto nomem;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200705 CLEAR_POINTER(uep);
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000706#ifdef U_DEBUG
707 uep->ue_magic = UE_MAGIC;
708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709
710 uep->ue_size = size;
711 uep->ue_top = top;
712 if (newbot != 0)
713 uep->ue_bot = newbot;
714 /*
715 * Use 0 for ue_bot if bot is below last line.
716 * Otherwise we have to compute ue_bot later.
717 */
718 else if (bot > curbuf->b_ml.ml_line_count)
719 uep->ue_bot = 0;
720 else
721 {
722 uep->ue_lcount = curbuf->b_ml.ml_line_count;
723 curbuf->b_u_newhead->uh_getbot_entry = uep;
724 }
725
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000726 if (size > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200728 if ((uep->ue_array = U_ALLOC_LINE(sizeof(undoline_T) * size)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 {
730 u_freeentry(uep, 0L);
731 goto nomem;
732 }
733 for (i = 0, lnum = top + 1; i < size; ++i)
734 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000735 fast_breakcheck();
736 if (got_int)
737 {
738 u_freeentry(uep, i);
739 return FAIL;
740 }
Bram Moolenaarccae4672019-01-04 15:09:57 +0100741 if (u_save_line(&uep->ue_array[i], lnum++) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 {
743 u_freeentry(uep, i);
744 goto nomem;
745 }
746 }
747 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000748 else
749 uep->ue_array = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 uep->ue_next = curbuf->b_u_newhead->uh_entry;
751 curbuf->b_u_newhead->uh_entry = uep;
752 curbuf->b_u_synced = FALSE;
753 undo_undoes = FALSE;
754
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000755#ifdef U_DEBUG
756 u_check(FALSE);
757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 return OK;
759
760nomem:
Bram Moolenaare38eab22019-12-05 21:50:01 +0100761 msg_silent = 0; // must display the prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 if (ask_yesno((char_u *)_("No undo possible; continue anyway"), TRUE)
763 == 'y')
764 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100765 undo_off = TRUE; // will be reset when character typed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 return OK;
767 }
768 do_outofmem_msg((long_u)0);
769 return FAIL;
770}
771
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200772#if defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200773
Bram Moolenaare38eab22019-12-05 21:50:01 +0100774# define UF_START_MAGIC "Vim\237UnDo\345" // magic at start of undofile
Bram Moolenaar9db58062010-05-29 20:33:07 +0200775# define UF_START_MAGIC_LEN 9
Bram Moolenaare38eab22019-12-05 21:50:01 +0100776# define UF_HEADER_MAGIC 0x5fd0 // magic at start of header
777# define UF_HEADER_END_MAGIC 0xe7aa // magic after last header
778# define UF_ENTRY_MAGIC 0xf518 // magic at start of entry
779# define UF_ENTRY_END_MAGIC 0x3581 // magic after last entry
780# define UF_VERSION 2 // 2-byte undofile version number
781# define UF_VERSION_CRYPT 0x8002 // idem, encrypted
Bram Moolenaara800b422010-06-27 01:15:55 +0200782
Bram Moolenaare38eab22019-12-05 21:50:01 +0100783// extra fields for header
Bram Moolenaara800b422010-06-27 01:15:55 +0200784# define UF_LAST_SAVE_NR 1
785
Bram Moolenaare38eab22019-12-05 21:50:01 +0100786// extra fields for uhp
Bram Moolenaara800b422010-06-27 01:15:55 +0200787# define UHP_SAVE_NR 1
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200788
Bram Moolenaarcdf04202010-05-29 15:11:47 +0200789static char_u e_not_open[] = N_("E828: Cannot open undo file for writing: %s");
Bram Moolenaarcdf04202010-05-29 15:11:47 +0200790
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200791/*
792 * Compute the hash for the current buffer text into hash[UNDO_HASH_SIZE].
793 */
794 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100795u_compute_hash(char_u *hash)
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200796{
797 context_sha256_T ctx;
798 linenr_T lnum;
799 char_u *p;
800
801 sha256_start(&ctx);
Bram Moolenaarae7ba982011-12-08 15:14:09 +0100802 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200803 {
804 p = ml_get(lnum);
Bram Moolenaar442b4222010-05-24 21:34:22 +0200805 sha256_update(&ctx, p, (UINT32_T)(STRLEN(p) + 1));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200806 }
807 sha256_finish(&ctx, hash);
808}
809
810/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200811 * Return an allocated string of the full path of the target undofile.
812 * When "reading" is TRUE find the file to read, go over all directories in
813 * 'undodir'.
814 * When "reading" is FALSE use the first name where the directory exists.
Bram Moolenaar9db58062010-05-29 20:33:07 +0200815 * Returns NULL when there is no place to write or no file to read.
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200816 */
Bram Moolenaar840d16f2019-09-10 21:27:18 +0200817 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100818u_get_undo_file_name(char_u *buf_ffname, int reading)
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200819{
820 char_u *dirp;
821 char_u dir_name[IOSIZE + 1];
822 char_u *munged_name = NULL;
823 char_u *undo_file_name = NULL;
824 int dir_len;
825 char_u *p;
Bram Moolenaar8767f522016-07-01 17:17:39 +0200826 stat_T st;
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200827 char_u *ffname = buf_ffname;
828#ifdef HAVE_READLINK
829 char_u fname_buf[MAXPATHL];
830#endif
831
832 if (ffname == NULL)
833 return NULL;
834
835#ifdef HAVE_READLINK
Bram Moolenaare38eab22019-12-05 21:50:01 +0100836 // Expand symlink in the file name, so that we put the undo file with the
837 // actual file instead of with the symlink.
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200838 if (resolve_symlink(ffname, fname_buf) == OK)
839 ffname = fname_buf;
840#endif
841
Bram Moolenaare38eab22019-12-05 21:50:01 +0100842 // Loop over 'undodir'. When reading find the first file that exists.
843 // When not reading use the first directory that exists or ".".
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200844 dirp = p_udir;
845 while (*dirp != NUL)
846 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200847 dir_len = copy_option_part(&dirp, dir_name, IOSIZE, ",");
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200848 if (dir_len == 1 && dir_name[0] == '.')
849 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100850 // Use same directory as the ffname,
851 // "dir/name" -> "dir/.name.un~"
Bram Moolenaar71ccd032020-06-12 22:59:11 +0200852 undo_file_name = vim_strnsave(ffname, STRLEN(ffname) + 5);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200853 if (undo_file_name == NULL)
854 break;
855 p = gettail(undo_file_name);
Bram Moolenaar206f0112014-03-12 16:51:55 +0100856#ifdef VMS
Bram Moolenaare38eab22019-12-05 21:50:01 +0100857 // VMS can not handle more than one dot in the filenames
858 // use "dir/name" -> "dir/_un_name" - add _un_
859 // at the beginning to keep the extension
Bram Moolenaar206f0112014-03-12 16:51:55 +0100860 mch_memmove(p + 4, p, STRLEN(p) + 1);
861 mch_memmove(p, "_un_", 4);
862
863#else
Bram Moolenaare38eab22019-12-05 21:50:01 +0100864 // Use same directory as the ffname,
865 // "dir/name" -> "dir/.name.un~"
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200866 mch_memmove(p + 1, p, STRLEN(p) + 1);
867 *p = '.';
868 STRCAT(p, ".un~");
Bram Moolenaar206f0112014-03-12 16:51:55 +0100869#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200870 }
871 else
872 {
873 dir_name[dir_len] = NUL;
874 if (mch_isdir(dir_name))
875 {
876 if (munged_name == NULL)
877 {
878 munged_name = vim_strsave(ffname);
879 if (munged_name == NULL)
880 return NULL;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100881 for (p = munged_name; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200882 if (vim_ispathsep(*p))
883 *p = '%';
884 }
885 undo_file_name = concat_fnames(dir_name, munged_name, TRUE);
886 }
887 }
888
Bram Moolenaare38eab22019-12-05 21:50:01 +0100889 // When reading check if the file exists.
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200890 if (undo_file_name != NULL && (!reading
891 || mch_stat((char *)undo_file_name, &st) >= 0))
892 break;
Bram Moolenaard23a8232018-02-10 18:45:26 +0100893 VIM_CLEAR(undo_file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200894 }
895
896 vim_free(munged_name);
897 return undo_file_name;
898}
899
Bram Moolenaar9db58062010-05-29 20:33:07 +0200900 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100901corruption_error(char *mesg, char_u *file_name)
Bram Moolenaar9db58062010-05-29 20:33:07 +0200902{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100903 semsg(_("E825: Corrupted undo file (%s): %s"), mesg, file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +0200904}
905
906 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100907u_free_uhp(u_header_T *uhp)
Bram Moolenaar9db58062010-05-29 20:33:07 +0200908{
909 u_entry_T *nuep;
910 u_entry_T *uep;
911
912 uep = uhp->uh_entry;
913 while (uep != NULL)
914 {
915 nuep = uep->ue_next;
916 u_freeentry(uep, uep->ue_size);
917 uep = nuep;
918 }
919 vim_free(uhp);
920}
921
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200922/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200923 * Write a sequence of bytes to the undo file.
924 * Buffers and encrypts as needed.
925 * Returns OK or FAIL.
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200926 */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200927 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100928undo_write(bufinfo_T *bi, char_u *ptr, size_t len)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200929{
930#ifdef FEAT_CRYPT
931 if (bi->bi_buffer != NULL)
932 {
933 size_t len_todo = len;
934 char_u *p = ptr;
935
936 while (bi->bi_used + len_todo >= CRYPT_BUF_SIZE)
937 {
938 size_t n = CRYPT_BUF_SIZE - bi->bi_used;
939
940 mch_memmove(bi->bi_buffer + bi->bi_used, p, n);
941 len_todo -= n;
942 p += n;
943 bi->bi_used = CRYPT_BUF_SIZE;
944 if (undo_flush(bi) == FAIL)
945 return FAIL;
946 }
947 if (len_todo > 0)
948 {
949 mch_memmove(bi->bi_buffer + bi->bi_used, p, len_todo);
950 bi->bi_used += len_todo;
951 }
952 return OK;
953 }
954#endif
955 if (fwrite(ptr, len, (size_t)1, bi->bi_fp) != 1)
956 return FAIL;
957 return OK;
958}
959
960#ifdef FEAT_CRYPT
961 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100962undo_flush(bufinfo_T *bi)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200963{
Bram Moolenaar829aa642017-08-23 22:32:35 +0200964 if (bi->bi_buffer != NULL && bi->bi_state != NULL && bi->bi_used > 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200965 {
Christian Brabandtf573c6e2021-06-20 14:02:16 +0200966 // Last parameter is only used for sodium encryption and that
967 // explicitly disables encryption of undofiles.
968 crypt_encode_inplace(bi->bi_state, bi->bi_buffer, bi->bi_used, FALSE);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200969 if (fwrite(bi->bi_buffer, bi->bi_used, (size_t)1, bi->bi_fp) != 1)
970 return FAIL;
971 bi->bi_used = 0;
972 }
973 return OK;
974}
975#endif
976
977/*
978 * Write "ptr[len]" and crypt the bytes when needed.
979 * Returns OK or FAIL.
980 */
981 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100982fwrite_crypt(bufinfo_T *bi, char_u *ptr, size_t len)
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200983{
984#ifdef FEAT_CRYPT
985 char_u *copy;
986 char_u small_buf[100];
987 size_t i;
988
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200989 if (bi->bi_state != NULL && bi->bi_buffer == NULL)
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200990 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100991 // crypting every piece of text separately
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200992 if (len < 100)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100993 copy = small_buf; // no malloc()/free() for short strings
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200994 else
995 {
996 copy = lalloc(len, FALSE);
997 if (copy == NULL)
998 return 0;
999 }
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001000 // Last parameter is only used for sodium encryption and that
1001 // explicitly disables encryption of undofiles.
1002 crypt_encode(bi->bi_state, ptr, len, copy, TRUE);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001003 i = fwrite(copy, len, (size_t)1, bi->bi_fp);
1004 if (copy != small_buf)
1005 vim_free(copy);
1006 return i == 1 ? OK : FAIL;
Bram Moolenaar191e0a22010-06-14 01:39:13 +02001007 }
Bram Moolenaar191e0a22010-06-14 01:39:13 +02001008#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001009 return undo_write(bi, ptr, len);
Bram Moolenaar191e0a22010-06-14 01:39:13 +02001010}
1011
1012/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001013 * Write a number, MSB first, in "len" bytes.
1014 * Must match with undo_read_?c() functions.
1015 * Returns OK or FAIL.
Bram Moolenaar191e0a22010-06-14 01:39:13 +02001016 */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001017 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001018undo_write_bytes(bufinfo_T *bi, long_u nr, int len)
Bram Moolenaar191e0a22010-06-14 01:39:13 +02001019{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001020 char_u buf[8];
1021 int i;
1022 int bufi = 0;
Bram Moolenaar191e0a22010-06-14 01:39:13 +02001023
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001024 for (i = len - 1; i >= 0; --i)
Bram Moolenaar9b8f0212014-08-13 22:05:53 +02001025 buf[bufi++] = (char_u)(nr >> (i * 8));
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001026 return undo_write(bi, buf, (size_t)len);
1027}
1028
1029/*
1030 * Write the pointer to an undo header. Instead of writing the pointer itself
1031 * we use the sequence number of the header. This is converted back to
1032 * pointers when reading. */
1033 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001034put_header_ptr(bufinfo_T *bi, u_header_T *uhp)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001035{
1036 undo_write_bytes(bi, (long_u)(uhp != NULL ? uhp->uh_seq : 0), 4);
Bram Moolenaar191e0a22010-06-14 01:39:13 +02001037}
1038
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001039 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001040undo_read_4c(bufinfo_T *bi)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001041{
1042#ifdef FEAT_CRYPT
1043 if (bi->bi_buffer != NULL)
1044 {
1045 char_u buf[4];
1046 int n;
1047
1048 undo_read(bi, buf, (size_t)4);
Bram Moolenaar35169282014-09-11 22:50:09 +02001049 n = ((unsigned)buf[0] << 24) + (buf[1] << 16) + (buf[2] << 8) + buf[3];
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001050 return n;
1051 }
1052#endif
1053 return get4c(bi->bi_fp);
1054}
1055
1056 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001057undo_read_2c(bufinfo_T *bi)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001058{
1059#ifdef FEAT_CRYPT
1060 if (bi->bi_buffer != NULL)
1061 {
1062 char_u buf[2];
1063 int n;
1064
1065 undo_read(bi, buf, (size_t)2);
1066 n = (buf[0] << 8) + buf[1];
1067 return n;
1068 }
1069#endif
1070 return get2c(bi->bi_fp);
1071}
1072
1073 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001074undo_read_byte(bufinfo_T *bi)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001075{
1076#ifdef FEAT_CRYPT
1077 if (bi->bi_buffer != NULL)
1078 {
1079 char_u buf[1];
1080
1081 undo_read(bi, buf, (size_t)1);
1082 return buf[0];
1083 }
1084#endif
1085 return getc(bi->bi_fp);
1086}
1087
1088 static time_t
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001089undo_read_time(bufinfo_T *bi)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001090{
1091#ifdef FEAT_CRYPT
1092 if (bi->bi_buffer != NULL)
1093 {
1094 char_u buf[8];
1095 time_t n = 0;
1096 int i;
1097
1098 undo_read(bi, buf, (size_t)8);
1099 for (i = 0; i < 8; ++i)
1100 n = (n << 8) + buf[i];
1101 return n;
1102 }
1103#endif
1104 return get8ctime(bi->bi_fp);
1105}
1106
1107/*
1108 * Read "buffer[size]" from the undo file.
1109 * Return OK or FAIL.
1110 */
1111 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001112undo_read(bufinfo_T *bi, char_u *buffer, size_t size)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001113{
Bram Moolenaar56f2db52017-06-11 23:09:15 +02001114 int retval = OK;
1115
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001116#ifdef FEAT_CRYPT
1117 if (bi->bi_buffer != NULL)
1118 {
Bram Moolenaar9b8f0212014-08-13 22:05:53 +02001119 int size_todo = (int)size;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001120 char_u *p = buffer;
1121
1122 while (size_todo > 0)
1123 {
1124 size_t n;
1125
1126 if (bi->bi_used >= bi->bi_avail)
1127 {
1128 n = fread(bi->bi_buffer, 1, (size_t)CRYPT_BUF_SIZE, bi->bi_fp);
Bram Moolenaar55952d42016-11-05 14:58:34 +01001129 if (n == 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001130 {
Bram Moolenaar56f2db52017-06-11 23:09:15 +02001131 retval = FAIL;
1132 break;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001133 }
1134 bi->bi_avail = n;
1135 bi->bi_used = 0;
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001136 crypt_decode_inplace(bi->bi_state, bi->bi_buffer, bi->bi_avail, FALSE);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001137 }
1138 n = size_todo;
1139 if (n > bi->bi_avail - bi->bi_used)
1140 n = bi->bi_avail - bi->bi_used;
1141 mch_memmove(p, bi->bi_buffer + bi->bi_used, n);
1142 bi->bi_used += n;
Bram Moolenaar9b8f0212014-08-13 22:05:53 +02001143 size_todo -= (int)n;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001144 p += n;
1145 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001146 }
Bram Moolenaar56f2db52017-06-11 23:09:15 +02001147 else
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001148#endif
1149 if (fread(buffer, (size_t)size, 1, bi->bi_fp) != 1)
Bram Moolenaar56f2db52017-06-11 23:09:15 +02001150 retval = FAIL;
1151
1152 if (retval == FAIL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01001153 // Error may be checked for only later. Fill with zeros,
1154 // so that the reader won't use garbage.
Bram Moolenaar56f2db52017-06-11 23:09:15 +02001155 vim_memset(buffer, 0, size);
1156 return retval;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001157}
1158
1159/*
1160 * Read a string of length "len" from "bi->bi_fd".
1161 * "len" can be zero to allocate an empty line.
1162 * Decrypt the bytes if needed.
1163 * Append a NUL.
1164 * Returns a pointer to allocated memory or NULL for failure.
1165 */
1166 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001167read_string_decrypt(bufinfo_T *bi, int len)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001168{
Bram Moolenaar964b3742019-05-24 18:54:09 +02001169 char_u *ptr = alloc(len + 1);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001170
1171 if (ptr != NULL)
1172 {
1173 if (len > 0 && undo_read(bi, ptr, len) == FAIL)
1174 {
1175 vim_free(ptr);
1176 return NULL;
1177 }
Bram Moolenaarccae4672019-01-04 15:09:57 +01001178 // In case there are text properties there already is a NUL, but
1179 // checking for that is more expensive than just adding a dummy byte.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001180 ptr[len] = NUL;
1181#ifdef FEAT_CRYPT
1182 if (bi->bi_state != NULL && bi->bi_buffer == NULL)
Christian Brabandtf573c6e2021-06-20 14:02:16 +02001183 crypt_decode_inplace(bi->bi_state, ptr, len, FALSE);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001184#endif
1185 }
1186 return ptr;
1187}
1188
1189/*
1190 * Writes the (not encrypted) header and initializes encryption if needed.
1191 */
1192 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001193serialize_header(bufinfo_T *bi, char_u *hash)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001194{
Bram Moolenaarccae4672019-01-04 15:09:57 +01001195 long len;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001196 buf_T *buf = bi->bi_buf;
1197 FILE *fp = bi->bi_fp;
1198 char_u time_buf[8];
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001199
Bram Moolenaare38eab22019-12-05 21:50:01 +01001200 // Start writing, first the magic marker and undo info version.
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001201 if (fwrite(UF_START_MAGIC, (size_t)UF_START_MAGIC_LEN, (size_t)1, fp) != 1)
1202 return FAIL;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001203
Bram Moolenaare38eab22019-12-05 21:50:01 +01001204 // If the buffer is encrypted then all text bytes following will be
1205 // encrypted. Numbers and other info is not crypted.
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001206#ifdef FEAT_CRYPT
Bram Moolenaarfa0ff9a2010-07-25 16:05:19 +02001207 if (*buf->b_p_key != NUL)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001208 {
1209 char_u *header;
1210 int header_len;
1211
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001212 undo_write_bytes(bi, (long_u)UF_VERSION_CRYPT, 2);
1213 bi->bi_state = crypt_create_for_writing(crypt_get_method_nr(buf),
1214 buf->b_p_key, &header, &header_len);
1215 if (bi->bi_state == NULL)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001216 return FAIL;
Bram Moolenaarccae4672019-01-04 15:09:57 +01001217 len = (long)fwrite(header, (size_t)header_len, (size_t)1, fp);
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001218 vim_free(header);
1219 if (len != 1)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001220 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001221 crypt_free_state(bi->bi_state);
1222 bi->bi_state = NULL;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001223 return FAIL;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001224 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001225
1226 if (crypt_whole_undofile(crypt_get_method_nr(buf)))
1227 {
1228 bi->bi_buffer = alloc(CRYPT_BUF_SIZE);
1229 if (bi->bi_buffer == NULL)
1230 {
1231 crypt_free_state(bi->bi_state);
1232 bi->bi_state = NULL;
1233 return FAIL;
1234 }
1235 bi->bi_used = 0;
1236 }
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001237 }
1238 else
1239#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001240 undo_write_bytes(bi, (long_u)UF_VERSION, 2);
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001241
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001242
Bram Moolenaare38eab22019-12-05 21:50:01 +01001243 // Write a hash of the buffer text, so that we can verify it is still the
1244 // same when reading the buffer text.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001245 if (undo_write(bi, hash, (size_t)UNDO_HASH_SIZE) == FAIL)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001246 return FAIL;
1247
Bram Moolenaare38eab22019-12-05 21:50:01 +01001248 // buffer-specific data
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001249 undo_write_bytes(bi, (long_u)buf->b_ml.ml_line_count, 4);
Bram Moolenaarccae4672019-01-04 15:09:57 +01001250 len = buf->b_u_line_ptr.ul_line == NULL
Bram Moolenaar8aef43b2019-01-08 20:14:35 +01001251 ? 0L : (long)STRLEN(buf->b_u_line_ptr.ul_line);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001252 undo_write_bytes(bi, (long_u)len, 4);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +01001253 if (len > 0 && fwrite_crypt(bi, buf->b_u_line_ptr.ul_line, (size_t)len)
1254 == FAIL)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001255 return FAIL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001256 undo_write_bytes(bi, (long_u)buf->b_u_line_lnum, 4);
1257 undo_write_bytes(bi, (long_u)buf->b_u_line_colnr, 4);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001258
Bram Moolenaare38eab22019-12-05 21:50:01 +01001259 // Undo structures header data
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001260 put_header_ptr(bi, buf->b_u_oldhead);
1261 put_header_ptr(bi, buf->b_u_newhead);
1262 put_header_ptr(bi, buf->b_u_curhead);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001263
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001264 undo_write_bytes(bi, (long_u)buf->b_u_numhead, 4);
1265 undo_write_bytes(bi, (long_u)buf->b_u_seq_last, 4);
1266 undo_write_bytes(bi, (long_u)buf->b_u_seq_cur, 4);
1267 time_to_bytes(buf->b_u_time_cur, time_buf);
1268 undo_write(bi, time_buf, 8);
Bram Moolenaara800b422010-06-27 01:15:55 +02001269
Bram Moolenaare38eab22019-12-05 21:50:01 +01001270 // Optional fields.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001271 undo_write_bytes(bi, 4, 1);
1272 undo_write_bytes(bi, UF_LAST_SAVE_NR, 1);
1273 undo_write_bytes(bi, (long_u)buf->b_u_save_nr_last, 4);
Bram Moolenaara800b422010-06-27 01:15:55 +02001274
Bram Moolenaare38eab22019-12-05 21:50:01 +01001275 undo_write_bytes(bi, 0, 1); // end marker
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001276
1277 return OK;
1278}
1279
1280 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001281serialize_uhp(bufinfo_T *bi, u_header_T *uhp)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001282{
1283 int i;
1284 u_entry_T *uep;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001285 char_u time_buf[8];
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001286
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001287 if (undo_write_bytes(bi, (long_u)UF_HEADER_MAGIC, 2) == FAIL)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001288 return FAIL;
1289
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001290 put_header_ptr(bi, uhp->uh_next.ptr);
1291 put_header_ptr(bi, uhp->uh_prev.ptr);
1292 put_header_ptr(bi, uhp->uh_alt_next.ptr);
1293 put_header_ptr(bi, uhp->uh_alt_prev.ptr);
1294 undo_write_bytes(bi, uhp->uh_seq, 4);
1295 serialize_pos(bi, uhp->uh_cursor);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001296 undo_write_bytes(bi, (long_u)uhp->uh_cursor_vcol, 4);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001297 undo_write_bytes(bi, (long_u)uhp->uh_flags, 2);
Bram Moolenaare38eab22019-12-05 21:50:01 +01001298 // Assume NMARKS will stay the same.
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001299 for (i = 0; i < NMARKS; ++i)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001300 serialize_pos(bi, uhp->uh_namedm[i]);
1301 serialize_visualinfo(bi, &uhp->uh_visual);
1302 time_to_bytes(uhp->uh_time, time_buf);
1303 undo_write(bi, time_buf, 8);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001304
Bram Moolenaare38eab22019-12-05 21:50:01 +01001305 // Optional fields.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001306 undo_write_bytes(bi, 4, 1);
1307 undo_write_bytes(bi, UHP_SAVE_NR, 1);
1308 undo_write_bytes(bi, (long_u)uhp->uh_save_nr, 4);
Bram Moolenaara800b422010-06-27 01:15:55 +02001309
Bram Moolenaare38eab22019-12-05 21:50:01 +01001310 undo_write_bytes(bi, 0, 1); // end marker
Bram Moolenaara800b422010-06-27 01:15:55 +02001311
Bram Moolenaare38eab22019-12-05 21:50:01 +01001312 // Write all the entries.
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001313 for (uep = uhp->uh_entry; uep != NULL; uep = uep->ue_next)
1314 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001315 undo_write_bytes(bi, (long_u)UF_ENTRY_MAGIC, 2);
1316 if (serialize_uep(bi, uep) == FAIL)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001317 return FAIL;
1318 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001319 undo_write_bytes(bi, (long_u)UF_ENTRY_END_MAGIC, 2);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001320 return OK;
1321}
1322
1323 static u_header_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001324unserialize_uhp(bufinfo_T *bi, char_u *file_name)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001325{
1326 u_header_T *uhp;
1327 int i;
1328 u_entry_T *uep, *last_uep;
1329 int c;
1330 int error;
1331
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001332 uhp = U_ALLOC_LINE(sizeof(u_header_T));
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001333 if (uhp == NULL)
1334 return NULL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001335 CLEAR_POINTER(uhp);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001336#ifdef U_DEBUG
1337 uhp->uh_magic = UH_MAGIC;
1338#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001339 uhp->uh_next.seq = undo_read_4c(bi);
1340 uhp->uh_prev.seq = undo_read_4c(bi);
1341 uhp->uh_alt_next.seq = undo_read_4c(bi);
1342 uhp->uh_alt_prev.seq = undo_read_4c(bi);
1343 uhp->uh_seq = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001344 if (uhp->uh_seq <= 0)
1345 {
1346 corruption_error("uh_seq", file_name);
1347 vim_free(uhp);
1348 return NULL;
1349 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001350 unserialize_pos(bi, &uhp->uh_cursor);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001351 uhp->uh_cursor_vcol = undo_read_4c(bi);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001352 uhp->uh_flags = undo_read_2c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001353 for (i = 0; i < NMARKS; ++i)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001354 unserialize_pos(bi, &uhp->uh_namedm[i]);
1355 unserialize_visualinfo(bi, &uhp->uh_visual);
1356 uhp->uh_time = undo_read_time(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001357
Bram Moolenaare38eab22019-12-05 21:50:01 +01001358 // Optional fields.
Bram Moolenaar69154f22010-07-18 21:42:34 +02001359 for (;;)
1360 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001361 int len = undo_read_byte(bi);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001362 int what;
Bram Moolenaara800b422010-06-27 01:15:55 +02001363
Bram Moolenaarfb06d762019-08-04 18:55:35 +02001364 if (len == EOF)
1365 {
1366 corruption_error("truncated", file_name);
1367 u_free_uhp(uhp);
1368 return NULL;
1369 }
Bram Moolenaar69154f22010-07-18 21:42:34 +02001370 if (len == 0)
1371 break;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001372 what = undo_read_byte(bi);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001373 switch (what)
1374 {
1375 case UHP_SAVE_NR:
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001376 uhp->uh_save_nr = undo_read_4c(bi);
Bram Moolenaara800b422010-06-27 01:15:55 +02001377 break;
Bram Moolenaar69154f22010-07-18 21:42:34 +02001378 default:
Bram Moolenaare38eab22019-12-05 21:50:01 +01001379 // field not supported, skip
Bram Moolenaar69154f22010-07-18 21:42:34 +02001380 while (--len >= 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001381 (void)undo_read_byte(bi);
Bram Moolenaara800b422010-06-27 01:15:55 +02001382 }
Bram Moolenaar69154f22010-07-18 21:42:34 +02001383 }
Bram Moolenaara800b422010-06-27 01:15:55 +02001384
Bram Moolenaare38eab22019-12-05 21:50:01 +01001385 // Unserialize the uep list.
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001386 last_uep = NULL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001387 while ((c = undo_read_2c(bi)) == UF_ENTRY_MAGIC)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001388 {
1389 error = FALSE;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001390 uep = unserialize_uep(bi, &error, file_name);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001391 if (last_uep == NULL)
1392 uhp->uh_entry = uep;
1393 else
1394 last_uep->ue_next = uep;
1395 last_uep = uep;
1396 if (uep == NULL || error)
1397 {
1398 u_free_uhp(uhp);
1399 return NULL;
1400 }
1401 }
1402 if (c != UF_ENTRY_END_MAGIC)
1403 {
1404 corruption_error("entry end", file_name);
1405 u_free_uhp(uhp);
1406 return NULL;
1407 }
1408
1409 return uhp;
1410}
1411
Bram Moolenaar9db58062010-05-29 20:33:07 +02001412/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001413 * Serialize "uep".
Bram Moolenaar9db58062010-05-29 20:33:07 +02001414 */
1415 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001416serialize_uep(
1417 bufinfo_T *bi,
1418 u_entry_T *uep)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001419{
1420 int i;
1421 size_t len;
1422
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001423 undo_write_bytes(bi, (long_u)uep->ue_top, 4);
1424 undo_write_bytes(bi, (long_u)uep->ue_bot, 4);
1425 undo_write_bytes(bi, (long_u)uep->ue_lcount, 4);
1426 undo_write_bytes(bi, (long_u)uep->ue_size, 4);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001427 for (i = 0; i < uep->ue_size; ++i)
1428 {
Bram Moolenaarccae4672019-01-04 15:09:57 +01001429 // Text is written without the text properties, since we cannot restore
1430 // the text property types.
1431 len = STRLEN(uep->ue_array[i].ul_line);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001432 if (undo_write_bytes(bi, (long_u)len, 4) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001433 return FAIL;
Bram Moolenaarccae4672019-01-04 15:09:57 +01001434 if (len > 0 && fwrite_crypt(bi, uep->ue_array[i].ul_line, len) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001435 return FAIL;
1436 }
1437 return OK;
1438}
1439
1440 static u_entry_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001441unserialize_uep(bufinfo_T *bi, int *error, char_u *file_name)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001442{
1443 int i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001444 u_entry_T *uep;
Bram Moolenaarccae4672019-01-04 15:09:57 +01001445 undoline_T *array = NULL;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001446 char_u *line;
1447 int line_len;
1448
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001449 uep = U_ALLOC_LINE(sizeof(u_entry_T));
Bram Moolenaar9db58062010-05-29 20:33:07 +02001450 if (uep == NULL)
1451 return NULL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001452 CLEAR_POINTER(uep);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001453#ifdef U_DEBUG
1454 uep->ue_magic = UE_MAGIC;
1455#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001456 uep->ue_top = undo_read_4c(bi);
1457 uep->ue_bot = undo_read_4c(bi);
1458 uep->ue_lcount = undo_read_4c(bi);
1459 uep->ue_size = undo_read_4c(bi);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001460 if (uep->ue_size > 0)
1461 {
Bram Moolenaar0c8485f2017-02-26 18:17:10 +01001462 if (uep->ue_size < LONG_MAX / (int)sizeof(char_u *))
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001463 array = U_ALLOC_LINE(sizeof(undoline_T) * uep->ue_size);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001464 if (array == NULL)
1465 {
1466 *error = TRUE;
1467 return uep;
1468 }
Bram Moolenaarccae4672019-01-04 15:09:57 +01001469 vim_memset(array, 0, sizeof(undoline_T) * uep->ue_size);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001470 }
1471 uep->ue_array = array;
1472
1473 for (i = 0; i < uep->ue_size; ++i)
1474 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001475 line_len = undo_read_4c(bi);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001476 if (line_len >= 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001477 line = read_string_decrypt(bi, line_len);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001478 else
1479 {
1480 line = NULL;
1481 corruption_error("line length", file_name);
1482 }
1483 if (line == NULL)
1484 {
1485 *error = TRUE;
1486 return uep;
1487 }
Bram Moolenaarccae4672019-01-04 15:09:57 +01001488 array[i].ul_line = line;
1489 array[i].ul_len = line_len + 1;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001490 }
1491 return uep;
1492}
1493
1494/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001495 * Serialize "pos".
Bram Moolenaar9db58062010-05-29 20:33:07 +02001496 */
1497 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001498serialize_pos(bufinfo_T *bi, pos_T pos)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001499{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001500 undo_write_bytes(bi, (long_u)pos.lnum, 4);
1501 undo_write_bytes(bi, (long_u)pos.col, 4);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001502 undo_write_bytes(bi, (long_u)pos.coladd, 4);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001503}
1504
1505/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001506 * Unserialize the pos_T at the current position.
Bram Moolenaar9db58062010-05-29 20:33:07 +02001507 */
1508 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001509unserialize_pos(bufinfo_T *bi, pos_T *pos)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001510{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001511 pos->lnum = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001512 if (pos->lnum < 0)
1513 pos->lnum = 0;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001514 pos->col = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001515 if (pos->col < 0)
1516 pos->col = 0;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001517 pos->coladd = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001518 if (pos->coladd < 0)
1519 pos->coladd = 0;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001520}
1521
1522/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001523 * Serialize "info".
Bram Moolenaar9db58062010-05-29 20:33:07 +02001524 */
1525 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001526serialize_visualinfo(bufinfo_T *bi, visualinfo_T *info)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001527{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001528 serialize_pos(bi, info->vi_start);
1529 serialize_pos(bi, info->vi_end);
1530 undo_write_bytes(bi, (long_u)info->vi_mode, 4);
1531 undo_write_bytes(bi, (long_u)info->vi_curswant, 4);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001532}
1533
1534/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001535 * Unserialize the visualinfo_T at the current position.
Bram Moolenaar9db58062010-05-29 20:33:07 +02001536 */
1537 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001538unserialize_visualinfo(bufinfo_T *bi, visualinfo_T *info)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001539{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001540 unserialize_pos(bi, &info->vi_start);
1541 unserialize_pos(bi, &info->vi_end);
1542 info->vi_mode = undo_read_4c(bi);
1543 info->vi_curswant = undo_read_4c(bi);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001544}
1545
1546/*
1547 * Write the undo tree in an undo file.
1548 * When "name" is not NULL, use it as the name of the undo file.
1549 * Otherwise use buf->b_ffname to generate the undo file name.
1550 * "buf" must never be null, buf->b_ffname is used to obtain the original file
1551 * permissions.
1552 * "forceit" is TRUE for ":wundo!", FALSE otherwise.
1553 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text.
1554 */
1555 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001556u_write_undo(
1557 char_u *name,
1558 int forceit,
1559 buf_T *buf,
1560 char_u *hash)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001561{
1562 u_header_T *uhp;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001563 char_u *file_name;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001564 int mark;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001565#ifdef U_DEBUG
1566 int headers_written = 0;
1567#endif
1568 int fd;
1569 FILE *fp = NULL;
1570 int perm;
1571 int write_ok = FALSE;
1572#ifdef UNIX
1573 int st_old_valid = FALSE;
Bram Moolenaar8767f522016-07-01 17:17:39 +02001574 stat_T st_old;
1575 stat_T st_new;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001576#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001577 bufinfo_T bi;
1578
Bram Moolenaara80faa82020-04-12 19:37:17 +02001579 CLEAR_FIELD(bi);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001580
1581 if (name == NULL)
1582 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001583 file_name = u_get_undo_file_name(buf->b_ffname, FALSE);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001584 if (file_name == NULL)
1585 {
1586 if (p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001587 {
1588 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001589 smsg(
Bram Moolenaar504a8212010-05-30 17:17:42 +02001590 _("Cannot write undo file in any directory in 'undodir'"));
1591 verbose_leave();
1592 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001593 return;
1594 }
1595 }
1596 else
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001597 file_name = name;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001598
1599 /*
1600 * Decide about the permission to use for the undo file. If the buffer
1601 * has a name use the permission of the original file. Otherwise only
1602 * allow the user to access the undo file.
1603 */
1604 perm = 0600;
1605 if (buf->b_ffname != NULL)
1606 {
1607#ifdef UNIX
1608 if (mch_stat((char *)buf->b_ffname, &st_old) >= 0)
1609 {
1610 perm = st_old.st_mode;
1611 st_old_valid = TRUE;
1612 }
1613#else
1614 perm = mch_getperm(buf->b_ffname);
1615 if (perm < 0)
1616 perm = 0600;
1617#endif
1618 }
1619
Bram Moolenaare38eab22019-12-05 21:50:01 +01001620 // strip any s-bit and executable bit
Bram Moolenaar3cbac302015-04-21 16:12:06 +02001621 perm = perm & 0666;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001622
Bram Moolenaare38eab22019-12-05 21:50:01 +01001623 // If the undo file already exists, verify that it actually is an undo
1624 // file, and delete it.
Bram Moolenaar9db58062010-05-29 20:33:07 +02001625 if (mch_getperm(file_name) >= 0)
1626 {
1627 if (name == NULL || !forceit)
1628 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001629 // Check we can read it and it's an undo file.
Bram Moolenaar9db58062010-05-29 20:33:07 +02001630 fd = mch_open((char *)file_name, O_RDONLY|O_EXTRA, 0);
1631 if (fd < 0)
1632 {
1633 if (name != NULL || p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001634 {
1635 if (name == NULL)
1636 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001637 smsg(
Bram Moolenaar504a8212010-05-30 17:17:42 +02001638 _("Will not overwrite with undo file, cannot read: %s"),
Bram Moolenaar9db58062010-05-29 20:33:07 +02001639 file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001640 if (name == NULL)
1641 verbose_leave();
1642 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001643 goto theend;
1644 }
1645 else
1646 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02001647 char_u mbuf[UF_START_MAGIC_LEN];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001648 int len;
1649
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01001650 len = read_eintr(fd, mbuf, UF_START_MAGIC_LEN);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001651 close(fd);
1652 if (len < UF_START_MAGIC_LEN
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02001653 || memcmp(mbuf, UF_START_MAGIC, UF_START_MAGIC_LEN) != 0)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001654 {
1655 if (name != NULL || p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001656 {
1657 if (name == NULL)
1658 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001659 smsg(
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001660 _("Will not overwrite, this is not an undo file: %s"),
Bram Moolenaar9db58062010-05-29 20:33:07 +02001661 file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001662 if (name == NULL)
1663 verbose_leave();
1664 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001665 goto theend;
1666 }
1667 }
1668 }
1669 mch_remove(file_name);
1670 }
1671
Bram Moolenaare38eab22019-12-05 21:50:01 +01001672 // If there is no undo information at all, quit here after deleting any
1673 // existing undo file.
Bram Moolenaarccae4672019-01-04 15:09:57 +01001674 if (buf->b_u_numhead == 0 && buf->b_u_line_ptr.ul_line == NULL)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001675 {
1676 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001677 verb_msg(_("Skipping undo file write, nothing to undo"));
Bram Moolenaar504a8212010-05-30 17:17:42 +02001678 goto theend;
1679 }
1680
Bram Moolenaar9db58062010-05-29 20:33:07 +02001681 fd = mch_open((char *)file_name,
1682 O_CREAT|O_EXTRA|O_WRONLY|O_EXCL|O_NOFOLLOW, perm);
1683 if (fd < 0)
1684 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001685 semsg(_(e_not_open), file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001686 goto theend;
1687 }
1688 (void)mch_setperm(file_name, perm);
1689 if (p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001690 {
1691 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001692 smsg(_("Writing undo file: %s"), file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001693 verbose_leave();
1694 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001695
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001696#ifdef U_DEBUG
Bram Moolenaare38eab22019-12-05 21:50:01 +01001697 // Check there is no problem in undo info before writing.
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001698 u_check(FALSE);
1699#endif
1700
Bram Moolenaar9db58062010-05-29 20:33:07 +02001701#ifdef UNIX
1702 /*
1703 * Try to set the group of the undo file same as the original file. If
1704 * this fails, set the protection bits for the group same as the
1705 * protection bits for others.
1706 */
Bram Moolenaarce69e822010-07-21 20:31:07 +02001707 if (st_old_valid
1708 && mch_stat((char *)file_name, &st_new) >= 0
1709 && st_new.st_gid != st_old.st_gid
Bram Moolenaare38eab22019-12-05 21:50:01 +01001710# ifdef HAVE_FCHOWN // sequent-ptx lacks fchown()
Bram Moolenaarce69e822010-07-21 20:31:07 +02001711 && fchown(fd, (uid_t)-1, st_old.st_gid) != 0
Bram Moolenaar9db58062010-05-29 20:33:07 +02001712# endif
1713 )
1714 mch_setperm(file_name, (perm & 0707) | ((perm & 07) << 3));
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02001715# if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001716 if (buf->b_ffname != NULL)
1717 mch_copy_sec(buf->b_ffname, file_name);
1718# endif
1719#endif
1720
1721 fp = fdopen(fd, "w");
1722 if (fp == NULL)
1723 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001724 semsg(_(e_not_open), file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001725 close(fd);
1726 mch_remove(file_name);
1727 goto theend;
1728 }
1729
Bram Moolenaare38eab22019-12-05 21:50:01 +01001730 // Undo must be synced.
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001731 u_sync(TRUE);
1732
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001733 /*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001734 * Write the header. Initializes encryption, if enabled.
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001735 */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001736 bi.bi_buf = buf;
1737 bi.bi_fp = fp;
1738 if (serialize_header(&bi, hash) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001739 goto write_error;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001740
1741 /*
1742 * Iteratively serialize UHPs and their UEPs from the top down.
1743 */
1744 mark = ++lastmark;
1745 uhp = buf->b_u_oldhead;
1746 while (uhp != NULL)
1747 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001748 // Serialize current UHP if we haven't seen it
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001749 if (uhp->uh_walk != mark)
1750 {
1751 uhp->uh_walk = mark;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001752#ifdef U_DEBUG
1753 ++headers_written;
1754#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001755 if (serialize_uhp(&bi, uhp) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001756 goto write_error;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001757 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001758
Bram Moolenaare38eab22019-12-05 21:50:01 +01001759 // Now walk through the tree - algorithm from undo_time().
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001760 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != mark)
1761 uhp = uhp->uh_prev.ptr;
1762 else if (uhp->uh_alt_next.ptr != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001763 && uhp->uh_alt_next.ptr->uh_walk != mark)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001764 uhp = uhp->uh_alt_next.ptr;
1765 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001766 && uhp->uh_next.ptr->uh_walk != mark)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001767 uhp = uhp->uh_next.ptr;
1768 else if (uhp->uh_alt_prev.ptr != NULL)
1769 uhp = uhp->uh_alt_prev.ptr;
1770 else
1771 uhp = uhp->uh_next.ptr;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001772 }
1773
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001774 if (undo_write_bytes(&bi, (long_u)UF_HEADER_END_MAGIC, 2) == OK)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001775 write_ok = TRUE;
1776#ifdef U_DEBUG
1777 if (headers_written != buf->b_u_numhead)
Bram Moolenaar570064c2013-06-10 20:25:10 +02001778 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001779 semsg("Written %ld headers, ...", headers_written);
1780 semsg("... but numhead is %ld", buf->b_u_numhead);
Bram Moolenaar570064c2013-06-10 20:25:10 +02001781 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001782#endif
1783
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001784#ifdef FEAT_CRYPT
1785 if (bi.bi_state != NULL && undo_flush(&bi) == FAIL)
1786 write_ok = FALSE;
1787#endif
1788
Bram Moolenaar340dd0f2021-10-14 17:52:23 +01001789#if defined(UNIX) && defined(HAVE_FSYNC)
1790 if (p_fs && fflush(fp) == 0 && vim_fsync(fd) != 0)
1791 write_ok = FALSE;
1792#endif
1793
Bram Moolenaar9db58062010-05-29 20:33:07 +02001794write_error:
1795 fclose(fp);
1796 if (!write_ok)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001797 semsg(_("E829: write error in undo file: %s"), file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001798
Bram Moolenaar4f974752019-02-17 17:44:42 +01001799#if defined(MSWIN)
Bram Moolenaare38eab22019-12-05 21:50:01 +01001800 // Copy file attributes; for systems where this can only be done after
1801 // closing the file.
Bram Moolenaar9db58062010-05-29 20:33:07 +02001802 if (buf->b_ffname != NULL)
1803 (void)mch_copy_file_attribute(buf->b_ffname, file_name);
1804#endif
1805#ifdef HAVE_ACL
1806 if (buf->b_ffname != NULL)
1807 {
1808 vim_acl_T acl;
1809
Bram Moolenaare38eab22019-12-05 21:50:01 +01001810 // For systems that support ACL: get the ACL from the original file.
Bram Moolenaar9db58062010-05-29 20:33:07 +02001811 acl = mch_get_acl(buf->b_ffname);
1812 mch_set_acl(file_name, acl);
Bram Moolenaard2aed442012-06-01 13:46:12 +02001813 mch_free_acl(acl);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001814 }
1815#endif
1816
1817theend:
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001818#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001819 if (bi.bi_state != NULL)
1820 crypt_free_state(bi.bi_state);
1821 vim_free(bi.bi_buffer);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001822#endif
Bram Moolenaar9db58062010-05-29 20:33:07 +02001823 if (file_name != name)
1824 vim_free(file_name);
1825}
1826
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001827/*
1828 * Load the undo tree from an undo file.
1829 * If "name" is not NULL use it as the undo file name. This also means being
1830 * a bit more verbose.
1831 * Otherwise use curbuf->b_ffname to generate the undo file name.
1832 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text.
1833 */
1834 void
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02001835u_read_undo(char_u *name, char_u *hash, char_u *orig_name UNUSED)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001836{
1837 char_u *file_name;
1838 FILE *fp;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001839 long version, str_len;
Bram Moolenaarccae4672019-01-04 15:09:57 +01001840 undoline_T line_ptr;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001841 linenr_T line_lnum;
1842 colnr_T line_colnr;
1843 linenr_T line_count;
Bram Moolenaar3eb16372017-02-26 18:11:36 +01001844 long num_head = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001845 long old_header_seq, new_header_seq, cur_header_seq;
1846 long seq_last, seq_cur;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001847 long last_save_nr = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001848 short old_idx = -1, new_idx = -1, cur_idx = -1;
1849 long num_read_uhps = 0;
1850 time_t seq_time;
1851 int i, j;
1852 int c;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001853 u_header_T *uhp;
1854 u_header_T **uhp_table = NULL;
1855 char_u read_hash[UNDO_HASH_SIZE];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001856 char_u magic_buf[UF_START_MAGIC_LEN];
1857#ifdef U_DEBUG
1858 int *uhp_table_used;
1859#endif
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001860#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001861 stat_T st_orig;
1862 stat_T st_undo;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001863#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001864 bufinfo_T bi;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001865
Bram Moolenaara80faa82020-04-12 19:37:17 +02001866 CLEAR_FIELD(bi);
Bram Moolenaarccae4672019-01-04 15:09:57 +01001867 line_ptr.ul_len = 0;
1868 line_ptr.ul_line = NULL;
1869
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001870 if (name == NULL)
1871 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001872 file_name = u_get_undo_file_name(curbuf->b_ffname, TRUE);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001873 if (file_name == NULL)
1874 return;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001875
1876#ifdef UNIX
Bram Moolenaare38eab22019-12-05 21:50:01 +01001877 // For safety we only read an undo file if the owner is equal to the
1878 // owner of the text file or equal to the current user.
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001879 if (mch_stat((char *)orig_name, &st_orig) >= 0
1880 && mch_stat((char *)file_name, &st_undo) >= 0
Bram Moolenaar3b262392013-09-08 15:40:49 +02001881 && st_orig.st_uid != st_undo.st_uid
1882 && st_undo.st_uid != getuid())
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001883 {
1884 if (p_verbose > 0)
1885 {
1886 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001887 smsg(_("Not reading undo file, owner differs: %s"),
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001888 file_name);
1889 verbose_leave();
1890 }
1891 return;
1892 }
1893#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001894 }
1895 else
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001896 file_name = name;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001897
1898 if (p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001899 {
1900 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001901 smsg(_("Reading undo file: %s"), file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001902 verbose_leave();
1903 }
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001904
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001905 fp = mch_fopen((char *)file_name, "r");
1906 if (fp == NULL)
1907 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001908 if (name != NULL || p_verbose > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001909 semsg(_("E822: Cannot open undo file for reading: %s"), file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001910 goto error;
1911 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001912 bi.bi_buf = curbuf;
1913 bi.bi_fp = fp;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001914
Bram Moolenaar9db58062010-05-29 20:33:07 +02001915 /*
1916 * Read the undo file header.
1917 */
1918 if (fread(magic_buf, UF_START_MAGIC_LEN, 1, fp) != 1
1919 || memcmp(magic_buf, UF_START_MAGIC, UF_START_MAGIC_LEN) != 0)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001920 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001921 semsg(_("E823: Not an undo file: %s"), file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001922 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001923 }
1924 version = get2c(fp);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001925 if (version == UF_VERSION_CRYPT)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001926 {
1927#ifdef FEAT_CRYPT
Bram Moolenaar56be9502010-06-06 14:20:26 +02001928 if (*curbuf->b_p_key == NUL)
1929 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001930 semsg(_("E832: Non-encrypted file has encrypted undo file: %s"),
Bram Moolenaar56be9502010-06-06 14:20:26 +02001931 file_name);
1932 goto error;
1933 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001934 bi.bi_state = crypt_create_from_file(fp, curbuf->b_p_key);
1935 if (bi.bi_state == NULL)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001936 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001937 semsg(_("E826: Undo file decryption failed: %s"), file_name);
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001938 goto error;
1939 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001940 if (crypt_whole_undofile(bi.bi_state->method_nr))
1941 {
1942 bi.bi_buffer = alloc(CRYPT_BUF_SIZE);
1943 if (bi.bi_buffer == NULL)
1944 {
1945 crypt_free_state(bi.bi_state);
1946 bi.bi_state = NULL;
1947 goto error;
1948 }
1949 bi.bi_avail = 0;
1950 bi.bi_used = 0;
1951 }
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001952#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001953 semsg(_("E827: Undo file is encrypted: %s"), file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001954 goto error;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001955#endif
1956 }
Bram Moolenaar69154f22010-07-18 21:42:34 +02001957 else if (version != UF_VERSION)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001958 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001959 semsg(_("E824: Incompatible undo file: %s"), file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001960 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001961 }
1962
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001963 if (undo_read(&bi, read_hash, (size_t)UNDO_HASH_SIZE) == FAIL)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001964 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001965 corruption_error("hash", file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001966 goto error;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001967 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001968 line_count = (linenr_T)undo_read_4c(&bi);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001969 if (memcmp(hash, read_hash, UNDO_HASH_SIZE) != 0
1970 || line_count != curbuf->b_ml.ml_line_count)
1971 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001972 if (p_verbose > 0 || name != NULL)
1973 {
Bram Moolenaar504a8212010-05-30 17:17:42 +02001974 if (name == NULL)
1975 verbose_enter();
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001976 give_warning((char_u *)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001977 _("File contents changed, cannot use undo info"), TRUE);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001978 if (name == NULL)
1979 verbose_leave();
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001980 }
1981 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001982 }
1983
Bram Moolenaare38eab22019-12-05 21:50:01 +01001984 // Read undo data for "U" command.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001985 str_len = undo_read_4c(&bi);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001986 if (str_len < 0)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001987 goto error;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001988 if (str_len > 0)
Bram Moolenaarccae4672019-01-04 15:09:57 +01001989 {
1990 line_ptr.ul_line = read_string_decrypt(&bi, str_len);
1991 line_ptr.ul_len = str_len + 1;
1992 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001993 line_lnum = (linenr_T)undo_read_4c(&bi);
1994 line_colnr = (colnr_T)undo_read_4c(&bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001995 if (line_lnum < 0 || line_colnr < 0)
1996 {
1997 corruption_error("line lnum/col", file_name);
1998 goto error;
1999 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002000
Bram Moolenaare38eab22019-12-05 21:50:01 +01002001 // Begin general undo data
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002002 old_header_seq = undo_read_4c(&bi);
2003 new_header_seq = undo_read_4c(&bi);
2004 cur_header_seq = undo_read_4c(&bi);
2005 num_head = undo_read_4c(&bi);
2006 seq_last = undo_read_4c(&bi);
2007 seq_cur = undo_read_4c(&bi);
2008 seq_time = undo_read_time(&bi);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002009
Bram Moolenaare38eab22019-12-05 21:50:01 +01002010 // Optional header fields.
Bram Moolenaar69154f22010-07-18 21:42:34 +02002011 for (;;)
2012 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002013 int len = undo_read_byte(&bi);
Bram Moolenaar69154f22010-07-18 21:42:34 +02002014 int what;
Bram Moolenaara800b422010-06-27 01:15:55 +02002015
Bram Moolenaar69154f22010-07-18 21:42:34 +02002016 if (len == 0 || len == EOF)
2017 break;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002018 what = undo_read_byte(&bi);
Bram Moolenaar69154f22010-07-18 21:42:34 +02002019 switch (what)
2020 {
2021 case UF_LAST_SAVE_NR:
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002022 last_save_nr = undo_read_4c(&bi);
Bram Moolenaara800b422010-06-27 01:15:55 +02002023 break;
Bram Moolenaar69154f22010-07-18 21:42:34 +02002024 default:
Bram Moolenaare38eab22019-12-05 21:50:01 +01002025 // field not supported, skip
Bram Moolenaar69154f22010-07-18 21:42:34 +02002026 while (--len >= 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002027 (void)undo_read_byte(&bi);
Bram Moolenaara800b422010-06-27 01:15:55 +02002028 }
Bram Moolenaar69154f22010-07-18 21:42:34 +02002029 }
Bram Moolenaara800b422010-06-27 01:15:55 +02002030
Bram Moolenaare38eab22019-12-05 21:50:01 +01002031 // uhp_table will store the freshly created undo headers we allocate
2032 // until we insert them into curbuf. The table remains sorted by the
2033 // sequence numbers of the headers.
2034 // When there are no headers uhp_table is NULL.
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002035 if (num_head > 0)
2036 {
Bram Moolenaar3eb16372017-02-26 18:11:36 +01002037 if (num_head < LONG_MAX / (long)sizeof(u_header_T *))
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002038 uhp_table = U_ALLOC_LINE(num_head * sizeof(u_header_T *));
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002039 if (uhp_table == NULL)
2040 goto error;
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002041 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002042
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002043 while ((c = undo_read_2c(&bi)) == UF_HEADER_MAGIC)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002044 {
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02002045 if (num_read_uhps >= num_head)
2046 {
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002047 corruption_error("num_head too small", file_name);
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02002048 goto error;
2049 }
2050
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002051 uhp = unserialize_uhp(&bi, file_name);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002052 if (uhp == NULL)
2053 goto error;
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02002054 uhp_table[num_read_uhps++] = uhp;
2055 }
2056
2057 if (num_read_uhps != num_head)
2058 {
2059 corruption_error("num_head", file_name);
2060 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002061 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02002062 if (c != UF_HEADER_END_MAGIC)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002063 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02002064 corruption_error("end marker", file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002065 goto error;
2066 }
2067
Bram Moolenaar9db58062010-05-29 20:33:07 +02002068#ifdef U_DEBUG
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002069 uhp_table_used = alloc_clear(sizeof(int) * num_head + 1);
Bram Moolenaar9db58062010-05-29 20:33:07 +02002070# define SET_FLAG(j) ++uhp_table_used[j]
2071#else
2072# define SET_FLAG(j)
2073#endif
2074
Bram Moolenaare38eab22019-12-05 21:50:01 +01002075 // We have put all of the headers into a table. Now we iterate through the
2076 // table and swizzle each sequence number we have stored in uh_*_seq into
2077 // a pointer corresponding to the header with that sequence number.
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002078 for (i = 0; i < num_head; i++)
2079 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002080 uhp = uhp_table[i];
2081 if (uhp == NULL)
2082 continue;
2083 for (j = 0; j < num_head; j++)
2084 if (uhp_table[j] != NULL && i != j
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002085 && uhp_table[i]->uh_seq == uhp_table[j]->uh_seq)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002086 {
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02002087 corruption_error("duplicate uh_seq", file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002088 goto error;
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02002089 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002090 for (j = 0; j < num_head; j++)
2091 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002092 && uhp_table[j]->uh_seq == uhp->uh_next.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002093 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002094 uhp->uh_next.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02002095 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002096 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002097 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002098 for (j = 0; j < num_head; j++)
2099 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002100 && uhp_table[j]->uh_seq == uhp->uh_prev.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002101 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002102 uhp->uh_prev.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02002103 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002104 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002105 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002106 for (j = 0; j < num_head; j++)
2107 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002108 && uhp_table[j]->uh_seq == uhp->uh_alt_next.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002109 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002110 uhp->uh_alt_next.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02002111 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002112 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002113 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002114 for (j = 0; j < num_head; j++)
2115 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002116 && uhp_table[j]->uh_seq == uhp->uh_alt_prev.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002117 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002118 uhp->uh_alt_prev.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02002119 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002120 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002121 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002122 if (old_header_seq > 0 && old_idx < 0 && uhp->uh_seq == old_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002123 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002124 old_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002125 SET_FLAG(i);
2126 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002127 if (new_header_seq > 0 && new_idx < 0 && uhp->uh_seq == new_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002128 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002129 new_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002130 SET_FLAG(i);
2131 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002132 if (cur_header_seq > 0 && cur_idx < 0 && uhp->uh_seq == cur_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002133 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002134 cur_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002135 SET_FLAG(i);
2136 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002137 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02002138
Bram Moolenaare38eab22019-12-05 21:50:01 +01002139 // Now that we have read the undo info successfully, free the current undo
2140 // info and use the info from the file.
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002141 u_blockfree(curbuf);
Bram Moolenaar9db58062010-05-29 20:33:07 +02002142 curbuf->b_u_oldhead = old_idx < 0 ? NULL : uhp_table[old_idx];
2143 curbuf->b_u_newhead = new_idx < 0 ? NULL : uhp_table[new_idx];
2144 curbuf->b_u_curhead = cur_idx < 0 ? NULL : uhp_table[cur_idx];
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002145 curbuf->b_u_line_ptr = line_ptr;
2146 curbuf->b_u_line_lnum = line_lnum;
2147 curbuf->b_u_line_colnr = line_colnr;
2148 curbuf->b_u_numhead = num_head;
2149 curbuf->b_u_seq_last = seq_last;
2150 curbuf->b_u_seq_cur = seq_cur;
Bram Moolenaara800b422010-06-27 01:15:55 +02002151 curbuf->b_u_time_cur = seq_time;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002152 curbuf->b_u_save_nr_last = last_save_nr;
Bram Moolenaardba01a02010-11-03 19:32:42 +01002153 curbuf->b_u_save_nr_cur = last_save_nr;
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02002154
2155 curbuf->b_u_synced = TRUE;
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002156 vim_free(uhp_table);
Bram Moolenaar9db58062010-05-29 20:33:07 +02002157
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002158#ifdef U_DEBUG
Bram Moolenaar9db58062010-05-29 20:33:07 +02002159 for (i = 0; i < num_head; ++i)
2160 if (uhp_table_used[i] == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002161 semsg("uhp_table entry %ld not used, leaking memory", i);
Bram Moolenaar9db58062010-05-29 20:33:07 +02002162 vim_free(uhp_table_used);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002163 u_check(TRUE);
2164#endif
Bram Moolenaar9db58062010-05-29 20:33:07 +02002165
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002166 if (name != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002167 smsg(_("Finished reading undo file %s"), file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002168 goto theend;
2169
2170error:
Bram Moolenaarccae4672019-01-04 15:09:57 +01002171 vim_free(line_ptr.ul_line);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002172 if (uhp_table != NULL)
2173 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002174 for (i = 0; i < num_read_uhps; i++)
2175 if (uhp_table[i] != NULL)
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02002176 u_free_uhp(uhp_table[i]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002177 vim_free(uhp_table);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002178 }
2179
2180theend:
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002181#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002182 if (bi.bi_state != NULL)
2183 crypt_free_state(bi.bi_state);
2184 vim_free(bi.bi_buffer);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002185#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002186 if (fp != NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002187 fclose(fp);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002188 if (file_name != name)
2189 vim_free(file_name);
2190 return;
2191}
2192
Bram Moolenaare38eab22019-12-05 21:50:01 +01002193#endif // FEAT_PERSISTENT_UNDO
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002194
2195
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196/*
2197 * If 'cpoptions' contains 'u': Undo the previous undo or redo (vi compatible).
2198 * If 'cpoptions' does not contain 'u': Always undo.
2199 */
2200 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002201u_undo(int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202{
2203 /*
2204 * If we get an undo command while executing a macro, we behave like the
2205 * original vi. If this happens twice in one macro the result will not
2206 * be compatible.
2207 */
2208 if (curbuf->b_u_synced == FALSE)
2209 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002210 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 count = 1;
2212 }
2213
2214 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
2215 undo_undoes = TRUE;
2216 else
2217 undo_undoes = !undo_undoes;
2218 u_doit(count);
2219}
2220
2221/*
2222 * If 'cpoptions' contains 'u': Repeat the previous undo or redo.
2223 * If 'cpoptions' does not contain 'u': Always redo.
2224 */
2225 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002226u_redo(int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227{
2228 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
2229 undo_undoes = FALSE;
2230 u_doit(count);
2231}
2232
2233/*
2234 * Undo or redo, depending on 'undo_undoes', 'count' times.
2235 */
2236 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002237u_doit(int startcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238{
Bram Moolenaarca003e12006-03-17 23:19:38 +00002239 int count = startcount;
2240
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002241 if (!undo_allowed())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243
2244 u_newcount = 0;
2245 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002246 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2247 u_oldcount = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 while (count--)
2249 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002250 // Do the change warning now, so that it triggers FileChangedRO when
2251 // needed. This may cause the file to be reloaded, that must happen
2252 // before we do anything, because it may change curbuf->b_u_curhead
2253 // and more.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002254 change_warning(0);
2255
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 if (undo_undoes)
2257 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002258 if (curbuf->b_u_curhead == NULL) // first undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 curbuf->b_u_curhead = curbuf->b_u_newhead;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002260 else if (get_undolevel() > 0) // multi level undo
2261 // get next undo
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002262 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_next.ptr;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002263 // nothing to undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 if (curbuf->b_u_numhead == 0 || curbuf->b_u_curhead == NULL)
2265 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002266 // stick curbuf->b_u_curhead at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 curbuf->b_u_curhead = curbuf->b_u_oldhead;
2268 beep_flush();
Bram Moolenaarca003e12006-03-17 23:19:38 +00002269 if (count == startcount - 1)
2270 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002271 msg(_("Already at oldest change"));
Bram Moolenaarca003e12006-03-17 23:19:38 +00002272 return;
2273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 break;
2275 }
2276
Bram Moolenaarca003e12006-03-17 23:19:38 +00002277 u_undoredo(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 }
2279 else
2280 {
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002281 if (curbuf->b_u_curhead == NULL || get_undolevel() <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002283 beep_flush(); // nothing to redo
Bram Moolenaarca003e12006-03-17 23:19:38 +00002284 if (count == startcount - 1)
2285 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002286 msg(_("Already at newest change"));
Bram Moolenaarca003e12006-03-17 23:19:38 +00002287 return;
2288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 break;
2290 }
2291
Bram Moolenaarca003e12006-03-17 23:19:38 +00002292 u_undoredo(FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002293
Bram Moolenaare38eab22019-12-05 21:50:01 +01002294 // Advance for next redo. Set "newhead" when at the end of the
2295 // redoable changes.
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002296 if (curbuf->b_u_curhead->uh_prev.ptr == NULL)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002297 curbuf->b_u_newhead = curbuf->b_u_curhead;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002298 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_prev.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 }
2300 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00002301 u_undo_end(undo_undoes, FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002302}
2303
Bram Moolenaar1e607892006-03-13 22:15:53 +00002304/*
2305 * Undo or redo over the timeline.
2306 * When "step" is negative go back in time, otherwise goes forward in time.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002307 * When "sec" is FALSE make "step" steps, when "sec" is TRUE use "step" as
2308 * seconds.
Bram Moolenaar730cde92010-06-27 05:18:54 +02002309 * When "file" is TRUE use "step" as a number of file writes.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002310 * When "absolute" is TRUE use "step" as the sequence number to jump to.
2311 * "sec" must be FALSE then.
Bram Moolenaar1e607892006-03-13 22:15:53 +00002312 */
2313 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002314undo_time(
2315 long step,
2316 int sec,
2317 int file,
2318 int absolute)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002319{
2320 long target;
2321 long closest;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002322 long closest_start;
2323 long closest_seq = 0;
2324 long val;
Bram Moolenaarce46d932018-01-30 22:46:06 +01002325 u_header_T *uhp = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002326 u_header_T *last;
2327 int mark;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02002328 int nomark = 0; // shut up compiler
Bram Moolenaar1e607892006-03-13 22:15:53 +00002329 int round;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002330 int dosec = sec;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002331 int dofile = file;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002332 int above = FALSE;
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002333 int did_undo = TRUE;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002334
Bram Moolenaare38eab22019-12-05 21:50:01 +01002335 // First make sure the current undoable change is synced.
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002336 if (curbuf->b_u_synced == FALSE)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002337 u_sync(TRUE);
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002338
Bram Moolenaar1e607892006-03-13 22:15:53 +00002339 u_newcount = 0;
2340 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002341 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002342 u_oldcount = -1;
2343
Bram Moolenaare38eab22019-12-05 21:50:01 +01002344 // "target" is the node below which we want to be.
2345 // Init "closest" to a value we can't reach.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002346 if (absolute)
2347 {
Bram Moolenaarce46d932018-01-30 22:46:06 +01002348 target = step;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002349 closest = -1;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002350 }
Bram Moolenaarca003e12006-03-17 23:19:38 +00002351 else
Bram Moolenaar1e607892006-03-13 22:15:53 +00002352 {
Bram Moolenaar730cde92010-06-27 05:18:54 +02002353 if (dosec)
Bram Moolenaarcbd4de42017-01-07 16:14:57 +01002354 target = (long)(curbuf->b_u_time_cur) + step;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002355 else if (dofile)
2356 {
2357 if (step < 0)
2358 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002359 // Going back to a previous write. If there were changes after
2360 // the last write, count that as moving one file-write, so
2361 // that ":earlier 1f" undoes all changes since the last save.
Bram Moolenaar730cde92010-06-27 05:18:54 +02002362 uhp = curbuf->b_u_curhead;
2363 if (uhp != NULL)
2364 uhp = uhp->uh_next.ptr;
2365 else
2366 uhp = curbuf->b_u_newhead;
2367 if (uhp != NULL && uhp->uh_save_nr != 0)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002368 // "uh_save_nr" was set in the last block, that means
2369 // there were no changes since the last write
Bram Moolenaar730cde92010-06-27 05:18:54 +02002370 target = curbuf->b_u_save_nr_cur + step;
2371 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01002372 // count the changes since the last write as one step
Bram Moolenaar730cde92010-06-27 05:18:54 +02002373 target = curbuf->b_u_save_nr_cur + step + 1;
2374 if (target <= 0)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002375 // Go to before first write: before the oldest change. Use
2376 // the sequence number for that.
Bram Moolenaar730cde92010-06-27 05:18:54 +02002377 dofile = FALSE;
2378 }
2379 else
2380 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002381 // Moving forward to a newer write.
Bram Moolenaar730cde92010-06-27 05:18:54 +02002382 target = curbuf->b_u_save_nr_cur + step;
2383 if (target > curbuf->b_u_save_nr_last)
2384 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002385 // Go to after last write: after the latest change. Use
2386 // the sequence number for that.
Bram Moolenaar730cde92010-06-27 05:18:54 +02002387 target = curbuf->b_u_seq_last + 1;
2388 dofile = FALSE;
2389 }
2390 }
2391 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002392 else
2393 target = curbuf->b_u_seq_cur + step;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002394 if (step < 0)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002395 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002396 if (target < 0)
2397 target = 0;
2398 closest = -1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002399 }
2400 else
2401 {
Bram Moolenaar730cde92010-06-27 05:18:54 +02002402 if (dosec)
Bram Moolenaarcbd4de42017-01-07 16:14:57 +01002403 closest = (long)(vim_time() + 1);
Bram Moolenaar730cde92010-06-27 05:18:54 +02002404 else if (dofile)
2405 closest = curbuf->b_u_save_nr_last + 2;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002406 else
2407 closest = curbuf->b_u_seq_last + 2;
2408 if (target >= closest)
2409 target = closest - 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002410 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002411 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002412 closest_start = closest;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002413 closest_seq = curbuf->b_u_seq_cur;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002414
Bram Moolenaare38eab22019-12-05 21:50:01 +01002415 // When "target" is 0; Back to origin.
Bram Moolenaarce46d932018-01-30 22:46:06 +01002416 if (target == 0)
Bram Moolenaar059fd012018-01-31 14:25:53 +01002417 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002418 mark = lastmark; // avoid that GCC complains
Bram Moolenaar059fd012018-01-31 14:25:53 +01002419 goto target_zero;
2420 }
Bram Moolenaarce46d932018-01-30 22:46:06 +01002421
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002422 /*
2423 * May do this twice:
Bram Moolenaar1e607892006-03-13 22:15:53 +00002424 * 1. Search for "target", update "closest" to the best match found.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002425 * 2. If "target" not found search for "closest".
2426 *
2427 * When using the closest time we use the sequence number in the second
2428 * round, because there may be several entries with the same time.
2429 */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002430 for (round = 1; round <= 2; ++round)
2431 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002432 // Find the path from the current state to where we want to go. The
2433 // desired state can be anywhere in the undo tree, need to go all over
2434 // it. We put "nomark" in uh_walk where we have been without success,
2435 // "mark" where it could possibly be.
Bram Moolenaar1e607892006-03-13 22:15:53 +00002436 mark = ++lastmark;
2437 nomark = ++lastmark;
2438
Bram Moolenaare38eab22019-12-05 21:50:01 +01002439 if (curbuf->b_u_curhead == NULL) // at leaf of the tree
Bram Moolenaar1e607892006-03-13 22:15:53 +00002440 uhp = curbuf->b_u_newhead;
2441 else
2442 uhp = curbuf->b_u_curhead;
2443
2444 while (uhp != NULL)
2445 {
2446 uhp->uh_walk = mark;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002447 if (dosec)
Bram Moolenaarcbd4de42017-01-07 16:14:57 +01002448 val = (long)(uhp->uh_time);
Bram Moolenaar730cde92010-06-27 05:18:54 +02002449 else if (dofile)
2450 val = uhp->uh_save_nr;
2451 else
2452 val = uhp->uh_seq;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002453
Bram Moolenaar730cde92010-06-27 05:18:54 +02002454 if (round == 1 && !(dofile && val == 0))
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002455 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002456 // Remember the header that is closest to the target.
2457 // It must be at least in the right direction (checked with
2458 // "b_u_seq_cur"). When the timestamp is equal find the
2459 // highest/lowest sequence number.
Bram Moolenaarca003e12006-03-17 23:19:38 +00002460 if ((step < 0 ? uhp->uh_seq <= curbuf->b_u_seq_cur
2461 : uhp->uh_seq > curbuf->b_u_seq_cur)
2462 && ((dosec && val == closest)
2463 ? (step < 0
2464 ? uhp->uh_seq < closest_seq
2465 : uhp->uh_seq > closest_seq)
2466 : closest == closest_start
2467 || (val > target
2468 ? (closest > target
2469 ? val - target <= closest - target
2470 : val - target <= target - closest)
2471 : (closest > target
2472 ? target - val <= closest - target
2473 : target - val <= target - closest))))
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002474 {
2475 closest = val;
2476 closest_seq = uhp->uh_seq;
2477 }
2478 }
2479
Bram Moolenaare38eab22019-12-05 21:50:01 +01002480 // Quit searching when we found a match. But when searching for a
2481 // time we need to continue looking for the best uh_seq.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002482 if (target == val && !dosec)
Bram Moolenaar730cde92010-06-27 05:18:54 +02002483 {
2484 target = uhp->uh_seq;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002485 break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002486 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002487
Bram Moolenaare38eab22019-12-05 21:50:01 +01002488 // go down in the tree if we haven't been there
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002489 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != nomark
2490 && uhp->uh_prev.ptr->uh_walk != mark)
2491 uhp = uhp->uh_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002492
Bram Moolenaare38eab22019-12-05 21:50:01 +01002493 // go to alternate branch if we haven't been there
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002494 else if (uhp->uh_alt_next.ptr != NULL
2495 && uhp->uh_alt_next.ptr->uh_walk != nomark
2496 && uhp->uh_alt_next.ptr->uh_walk != mark)
2497 uhp = uhp->uh_alt_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002498
Bram Moolenaare38eab22019-12-05 21:50:01 +01002499 // go up in the tree if we haven't been there and we are at the
2500 // start of alternate branches
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002501 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
2502 && uhp->uh_next.ptr->uh_walk != nomark
2503 && uhp->uh_next.ptr->uh_walk != mark)
Bram Moolenaardb552d602006-03-23 22:59:57 +00002504 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002505 // If still at the start we don't go through this change.
Bram Moolenaardb552d602006-03-23 22:59:57 +00002506 if (uhp == curbuf->b_u_curhead)
2507 uhp->uh_walk = nomark;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002508 uhp = uhp->uh_next.ptr;
Bram Moolenaardb552d602006-03-23 22:59:57 +00002509 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002510
2511 else
2512 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002513 // need to backtrack; mark this node as useless
Bram Moolenaar1e607892006-03-13 22:15:53 +00002514 uhp->uh_walk = nomark;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002515 if (uhp->uh_alt_prev.ptr != NULL)
2516 uhp = uhp->uh_alt_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002517 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002518 uhp = uhp->uh_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002519 }
2520 }
2521
Bram Moolenaare38eab22019-12-05 21:50:01 +01002522 if (uhp != NULL) // found it
Bram Moolenaar1e607892006-03-13 22:15:53 +00002523 break;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002524
2525 if (absolute)
2526 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002527 semsg(_("E830: Undo number %ld not found"), step);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002528 return;
2529 }
2530
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002531 if (closest == closest_start)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002532 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002533 if (step < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002534 msg(_("Already at oldest change"));
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002535 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002536 msg(_("Already at newest change"));
Bram Moolenaar1e607892006-03-13 22:15:53 +00002537 return;
2538 }
2539
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002540 target = closest_seq;
2541 dosec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002542 dofile = FALSE;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002543 if (step < 0)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002544 above = TRUE; // stop above the header
Bram Moolenaar1e607892006-03-13 22:15:53 +00002545 }
2546
Bram Moolenaar059fd012018-01-31 14:25:53 +01002547target_zero:
Bram Moolenaare38eab22019-12-05 21:50:01 +01002548 // If we found it: Follow the path to go to where we want to be.
Bram Moolenaarce46d932018-01-30 22:46:06 +01002549 if (uhp != NULL || target == 0)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002550 {
2551 /*
2552 * First go up the tree as much as needed.
2553 */
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002554 while (!got_int)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002555 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002556 // Do the change warning now, for the same reason as above.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002557 change_warning(0);
2558
Bram Moolenaar1e607892006-03-13 22:15:53 +00002559 uhp = curbuf->b_u_curhead;
2560 if (uhp == NULL)
2561 uhp = curbuf->b_u_newhead;
2562 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002563 uhp = uhp->uh_next.ptr;
Bram Moolenaarce46d932018-01-30 22:46:06 +01002564 if (uhp == NULL || (target > 0 && uhp->uh_walk != mark)
Bram Moolenaarca003e12006-03-17 23:19:38 +00002565 || (uhp->uh_seq == target && !above))
Bram Moolenaar1e607892006-03-13 22:15:53 +00002566 break;
2567 curbuf->b_u_curhead = uhp;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002568 u_undoredo(TRUE);
Bram Moolenaarce46d932018-01-30 22:46:06 +01002569 if (target > 0)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002570 uhp->uh_walk = nomark; // don't go back down here
Bram Moolenaar1e607892006-03-13 22:15:53 +00002571 }
2572
Bram Moolenaare38eab22019-12-05 21:50:01 +01002573 // When back to origin, redo is not needed.
Bram Moolenaarce46d932018-01-30 22:46:06 +01002574 if (target > 0)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002575 {
Bram Moolenaarce46d932018-01-30 22:46:06 +01002576 /*
2577 * And now go down the tree (redo), branching off where needed.
2578 */
2579 while (!got_int)
2580 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002581 // Do the change warning now, for the same reason as above.
Bram Moolenaarce46d932018-01-30 22:46:06 +01002582 change_warning(0);
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002583
Bram Moolenaarce46d932018-01-30 22:46:06 +01002584 uhp = curbuf->b_u_curhead;
2585 if (uhp == NULL)
2586 break;
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002587
Bram Moolenaare38eab22019-12-05 21:50:01 +01002588 // Go back to the first branch with a mark.
Bram Moolenaarce46d932018-01-30 22:46:06 +01002589 while (uhp->uh_alt_prev.ptr != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002590 && uhp->uh_alt_prev.ptr->uh_walk == mark)
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002591 uhp = uhp->uh_alt_prev.ptr;
Bram Moolenaarce46d932018-01-30 22:46:06 +01002592
Bram Moolenaare38eab22019-12-05 21:50:01 +01002593 // Find the last branch with a mark, that's the one.
Bram Moolenaarce46d932018-01-30 22:46:06 +01002594 last = uhp;
2595 while (last->uh_alt_next.ptr != NULL
2596 && last->uh_alt_next.ptr->uh_walk == mark)
2597 last = last->uh_alt_next.ptr;
2598 if (last != uhp)
2599 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002600 // Make the used branch the first entry in the list of
2601 // alternatives to make "u" and CTRL-R take this branch.
Bram Moolenaarce46d932018-01-30 22:46:06 +01002602 while (uhp->uh_alt_prev.ptr != NULL)
2603 uhp = uhp->uh_alt_prev.ptr;
2604 if (last->uh_alt_next.ptr != NULL)
2605 last->uh_alt_next.ptr->uh_alt_prev.ptr =
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002606 last->uh_alt_prev.ptr;
Bram Moolenaarce46d932018-01-30 22:46:06 +01002607 last->uh_alt_prev.ptr->uh_alt_next.ptr =
2608 last->uh_alt_next.ptr;
2609 last->uh_alt_prev.ptr = NULL;
2610 last->uh_alt_next.ptr = uhp;
2611 uhp->uh_alt_prev.ptr = last;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002612
Bram Moolenaarce46d932018-01-30 22:46:06 +01002613 if (curbuf->b_u_oldhead == uhp)
2614 curbuf->b_u_oldhead = last;
2615 uhp = last;
2616 if (uhp->uh_next.ptr != NULL)
2617 uhp->uh_next.ptr->uh_prev.ptr = uhp;
2618 }
2619 curbuf->b_u_curhead = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002620
Bram Moolenaarce46d932018-01-30 22:46:06 +01002621 if (uhp->uh_walk != mark)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002622 break; // must have reached the target
Bram Moolenaar1e607892006-03-13 22:15:53 +00002623
Bram Moolenaare38eab22019-12-05 21:50:01 +01002624 // Stop when going backwards in time and didn't find the exact
2625 // header we were looking for.
Bram Moolenaarce46d932018-01-30 22:46:06 +01002626 if (uhp->uh_seq == target && above)
2627 {
2628 curbuf->b_u_seq_cur = target - 1;
2629 break;
2630 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002631
Bram Moolenaarce46d932018-01-30 22:46:06 +01002632 u_undoredo(FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002633
Bram Moolenaare38eab22019-12-05 21:50:01 +01002634 // Advance "curhead" to below the header we last used. If it
2635 // becomes NULL then we need to set "newhead" to this leaf.
Bram Moolenaarce46d932018-01-30 22:46:06 +01002636 if (uhp->uh_prev.ptr == NULL)
2637 curbuf->b_u_newhead = uhp;
2638 curbuf->b_u_curhead = uhp->uh_prev.ptr;
2639 did_undo = FALSE;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002640
Bram Moolenaare38eab22019-12-05 21:50:01 +01002641 if (uhp->uh_seq == target) // found it!
Bram Moolenaarce46d932018-01-30 22:46:06 +01002642 break;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002643
Bram Moolenaarce46d932018-01-30 22:46:06 +01002644 uhp = uhp->uh_prev.ptr;
2645 if (uhp == NULL || uhp->uh_walk != mark)
2646 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002647 // Need to redo more but can't find it...
Bram Moolenaarce46d932018-01-30 22:46:06 +01002648 internal_error("undo_time()");
2649 break;
2650 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002651 }
2652 }
2653 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00002654 u_undo_end(did_undo, absolute);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655}
2656
2657/*
2658 * u_undoredo: common code for undo and redo
2659 *
2660 * The lines in the file are replaced by the lines in the entry list at
2661 * curbuf->b_u_curhead. The replaced lines in the file are saved in the entry
2662 * list for the next undo/redo.
Bram Moolenaarca003e12006-03-17 23:19:38 +00002663 *
2664 * When "undo" is TRUE we go up in the tree, when FALSE we go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 */
2666 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002667u_undoredo(int undo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668{
Bram Moolenaarccae4672019-01-04 15:09:57 +01002669 undoline_T *newarray = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 linenr_T oldsize;
2671 linenr_T newsize;
2672 linenr_T top, bot;
2673 linenr_T lnum;
2674 linenr_T newlnum = MAXLNUM;
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002675 pos_T new_curpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 long i;
2677 u_entry_T *uep, *nuep;
2678 u_entry_T *newlist = NULL;
2679 int old_flags;
2680 int new_flags;
2681 pos_T namedm[NMARKS];
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002682 visualinfo_T visualinfo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002683 int empty_buffer; // buffer became empty
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002684 u_header_T *curhead = curbuf->b_u_curhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685
Bram Moolenaare38eab22019-12-05 21:50:01 +01002686 // Don't want autocommands using the undo structures here, they are
2687 // invalid till the end.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002688 block_autocmds();
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002689
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002690#ifdef U_DEBUG
2691 u_check(FALSE);
2692#endif
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002693 old_flags = curhead->uh_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 new_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
2695 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
2696 setpcmark();
2697
2698 /*
2699 * save marks before undo/redo
2700 */
2701 mch_memmove(namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002702 visualinfo = curbuf->b_visual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 curbuf->b_op_start.lnum = curbuf->b_ml.ml_line_count;
2704 curbuf->b_op_start.col = 0;
2705 curbuf->b_op_end.lnum = 0;
2706 curbuf->b_op_end.col = 0;
2707
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002708 for (uep = curhead->uh_entry; uep != NULL; uep = nuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 {
2710 top = uep->ue_top;
2711 bot = uep->ue_bot;
2712 if (bot == 0)
2713 bot = curbuf->b_ml.ml_line_count + 1;
2714 if (top > curbuf->b_ml.ml_line_count || top >= bot
2715 || bot > curbuf->b_ml.ml_line_count + 1)
2716 {
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002717 unblock_autocmds();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002718 iemsg(_("E438: u_undo: line numbers wrong"));
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002719 changed(); // don't want UNCHANGED now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 return;
2721 }
2722
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002723 oldsize = bot - top - 1; // number of lines before undo
2724 newsize = uep->ue_size; // number of lines after undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002726 // Decide about the cursor position, depending on what text changed.
2727 // Don't set it yet, it may be invalid if lines are going to be added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 if (top < newlnum)
2729 {
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002730 // If the saved cursor is somewhere in this undo block, move it to
2731 // the remembered position. Makes "gwap" put the cursor back
2732 // where it was.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002733 lnum = curhead->uh_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 if (lnum >= top && lnum <= top + newsize + 1)
2735 {
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002736 new_curpos = curhead->uh_cursor;
2737 newlnum = new_curpos.lnum - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 }
2739 else
2740 {
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002741 // Use the first line that actually changed. Avoids that
2742 // undoing auto-formatting puts the cursor in the previous
2743 // line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 for (i = 0; i < newsize && i < oldsize; ++i)
Bram Moolenaarccae4672019-01-04 15:09:57 +01002745 {
2746 char_u *p = ml_get(top + 1 + i);
2747
2748 if (curbuf->b_ml.ml_line_len != uep->ue_array[i].ul_len
Bram Moolenaar964b3742019-05-24 18:54:09 +02002749 || memcmp(uep->ue_array[i].ul_line, p,
2750 curbuf->b_ml.ml_line_len) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 break;
Bram Moolenaarccae4672019-01-04 15:09:57 +01002752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 if (i == newsize && newlnum == MAXLNUM && uep->ue_next == NULL)
2754 {
2755 newlnum = top;
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002756 new_curpos.lnum = newlnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 }
2758 else if (i < newsize)
2759 {
2760 newlnum = top + i;
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002761 new_curpos.lnum = newlnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 }
2763 }
2764 }
2765
2766 empty_buffer = FALSE;
2767
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002768 /*
2769 * Delete the lines between top and bot and save them in newarray.
2770 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002771 if (oldsize > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002773 if ((newarray = U_ALLOC_LINE(sizeof(undoline_T) * oldsize)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 {
Bram Moolenaarccae4672019-01-04 15:09:57 +01002775 do_outofmem_msg((long_u)(sizeof(undoline_T) * oldsize));
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002776
2777 // We have messed up the entry list, repair is impossible.
2778 // we have to free the rest of the list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 while (uep != NULL)
2780 {
2781 nuep = uep->ue_next;
2782 u_freeentry(uep, uep->ue_size);
2783 uep = nuep;
2784 }
2785 break;
2786 }
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002787 // delete backwards, it goes faster in most cases
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 for (lnum = bot - 1, i = oldsize; --i >= 0; --lnum)
2789 {
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002790 // what can we do when we run out of memory?
Bram Moolenaarccae4672019-01-04 15:09:57 +01002791 if (u_save_line(&newarray[i], lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 do_outofmem_msg((long_u)0);
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002793 // remember we deleted the last line in the buffer, and a
2794 // dummy empty line will be inserted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 if (curbuf->b_ml.ml_line_count == 1)
2796 empty_buffer = TRUE;
Bram Moolenaara9d4b842020-05-30 14:46:52 +02002797 ml_delete_flags(lnum, ML_DEL_UNDO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 }
2799 }
Bram Moolenaar8d343302005-07-12 22:46:17 +00002800 else
2801 newarray = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002803 // make sure the cursor is on a valid line after the deletions
2804 check_cursor_lnum();
2805
2806 /*
2807 * Insert the lines in u_array between top and bot.
2808 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 if (newsize)
2810 {
2811 for (lnum = top, i = 0; i < newsize; ++i, ++lnum)
2812 {
Bram Moolenaarccae4672019-01-04 15:09:57 +01002813 // If the file is empty, there is an empty line 1 that we
2814 // should get rid of, by replacing it with the new line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 if (empty_buffer && lnum == 0)
Bram Moolenaar964b3742019-05-24 18:54:09 +02002816 ml_replace_len((linenr_T)1, uep->ue_array[i].ul_line,
2817 uep->ue_array[i].ul_len, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 else
Bram Moolenaara9d4b842020-05-30 14:46:52 +02002819 ml_append_flags(lnum, uep->ue_array[i].ul_line,
2820 (colnr_T)uep->ue_array[i].ul_len, ML_APPEND_UNDO);
Bram Moolenaarccae4672019-01-04 15:09:57 +01002821 vim_free(uep->ue_array[i].ul_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 }
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002823 vim_free((char_u *)uep->ue_array);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 }
2825
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002826 // adjust marks
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 if (oldsize != newsize)
2828 {
2829 mark_adjust(top + 1, top + oldsize, (long)MAXLNUM,
2830 (long)newsize - (long)oldsize);
2831 if (curbuf->b_op_start.lnum > top + oldsize)
2832 curbuf->b_op_start.lnum += newsize - oldsize;
2833 if (curbuf->b_op_end.lnum > top + oldsize)
2834 curbuf->b_op_end.lnum += newsize - oldsize;
2835 }
2836
2837 changed_lines(top + 1, 0, bot, newsize - oldsize);
2838
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002839 // set '[ and '] mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 if (top + 1 < curbuf->b_op_start.lnum)
2841 curbuf->b_op_start.lnum = top + 1;
2842 if (newsize == 0 && top + 1 > curbuf->b_op_end.lnum)
2843 curbuf->b_op_end.lnum = top + 1;
2844 else if (top + newsize > curbuf->b_op_end.lnum)
2845 curbuf->b_op_end.lnum = top + newsize;
2846
2847 u_newcount += newsize;
2848 u_oldcount += oldsize;
2849 uep->ue_size = oldsize;
2850 uep->ue_array = newarray;
2851 uep->ue_bot = top + newsize + 1;
2852
2853 /*
2854 * insert this entry in front of the new entry list
2855 */
2856 nuep = uep->ue_next;
2857 uep->ue_next = newlist;
2858 newlist = uep;
2859 }
2860
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002861 // Set the cursor to the desired position. Check that the line is valid.
2862 curwin->w_cursor = new_curpos;
2863 check_cursor_lnum();
2864
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002865 curhead->uh_entry = newlist;
2866 curhead->uh_flags = new_flags;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002867 if ((old_flags & UH_EMPTYBUF) && BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 curbuf->b_ml.ml_flags |= ML_EMPTY;
2869 if (old_flags & UH_CHANGED)
2870 changed();
2871 else
Bram Moolenaar009b2592004-10-24 19:18:58 +00002872#ifdef FEAT_NETBEANS_INTG
Bram Moolenaare38eab22019-12-05 21:50:01 +01002873 // per netbeans undo rules, keep it as modified
Bram Moolenaar009b2592004-10-24 19:18:58 +00002874 if (!isNetbeansModified(curbuf))
2875#endif
Bram Moolenaarc024b462019-06-08 18:07:21 +02002876 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877
2878 /*
2879 * restore marks from before undo/redo
2880 */
2881 for (i = 0; i < NMARKS; ++i)
Bram Moolenaarf65aad52015-02-17 13:43:40 +01002882 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002883 if (curhead->uh_namedm[i].lnum != 0)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002884 curbuf->b_namedm[i] = curhead->uh_namedm[i];
Bram Moolenaarf65aad52015-02-17 13:43:40 +01002885 if (namedm[i].lnum != 0)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002886 curhead->uh_namedm[i] = namedm[i];
Bram Moolenaarf65aad52015-02-17 13:43:40 +01002887 else
2888 curhead->uh_namedm[i].lnum = 0;
2889 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002890 if (curhead->uh_visual.vi_start.lnum != 0)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002891 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002892 curbuf->b_visual = curhead->uh_visual;
2893 curhead->uh_visual = visualinfo;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895
2896 /*
2897 * If the cursor is only off by one line, put it at the same position as
2898 * before starting the change (for the "o" command).
2899 * Otherwise the cursor should go to the first undone line.
2900 */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002901 if (curhead->uh_cursor.lnum + 1 == curwin->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 && curwin->w_cursor.lnum > 1)
2903 --curwin->w_cursor.lnum;
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002904 if (curwin->w_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 {
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002906 if (curhead->uh_cursor.lnum == curwin->w_cursor.lnum)
2907 {
2908 curwin->w_cursor.col = curhead->uh_cursor.col;
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002909 if (virtual_active() && curhead->uh_cursor_vcol >= 0)
2910 coladvance((colnr_T)curhead->uh_cursor_vcol);
2911 else
2912 curwin->w_cursor.coladd = 0;
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002913 }
2914 else
2915 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 else
2918 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002919 // We get here with the current cursor line being past the end (eg
2920 // after adding lines at the end of the file, and then undoing it).
2921 // check_cursor() will move the cursor to the last line. Move it to
2922 // the first column here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 }
2926
Bram Moolenaare38eab22019-12-05 21:50:01 +01002927 // Make sure the cursor is on an existing line and column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 check_cursor();
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002929
Bram Moolenaare38eab22019-12-05 21:50:01 +01002930 // Remember where we are for "g-" and ":earlier 10s".
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002931 curbuf->b_u_seq_cur = curhead->uh_seq;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002932 if (undo)
Bram Moolenaar80eaddd2017-11-11 23:37:08 +01002933 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002934 // We are below the previous undo. However, to make ":earlier 1s"
2935 // work we compute this as being just above the just undone change.
Bram Moolenaar80eaddd2017-11-11 23:37:08 +01002936 if (curhead->uh_next.ptr != NULL)
2937 curbuf->b_u_seq_cur = curhead->uh_next.ptr->uh_seq;
2938 else
2939 curbuf->b_u_seq_cur = 0;
2940 }
Bram Moolenaarca003e12006-03-17 23:19:38 +00002941
Bram Moolenaare38eab22019-12-05 21:50:01 +01002942 // Remember where we are for ":earlier 1f" and ":later 1f".
Bram Moolenaar730cde92010-06-27 05:18:54 +02002943 if (curhead->uh_save_nr != 0)
2944 {
2945 if (undo)
2946 curbuf->b_u_save_nr_cur = curhead->uh_save_nr - 1;
2947 else
2948 curbuf->b_u_save_nr_cur = curhead->uh_save_nr;
2949 }
2950
Bram Moolenaare38eab22019-12-05 21:50:01 +01002951 // The timestamp can be the same for multiple changes, just use the one of
2952 // the undone/redone change.
Bram Moolenaara800b422010-06-27 01:15:55 +02002953 curbuf->b_u_time_cur = curhead->uh_time;
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002954
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002955 unblock_autocmds();
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002956#ifdef U_DEBUG
2957 u_check(FALSE);
2958#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959}
2960
2961/*
2962 * If we deleted or added lines, report the number of less/more lines.
2963 * Otherwise, report the number of changes (this may be incorrect
2964 * in some cases, but it's better than nothing).
2965 */
2966 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002967u_undo_end(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002968 int did_undo, // just did an undo
2969 int absolute) // used ":undo N"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970{
Bram Moolenaar89d40322006-08-29 15:30:07 +00002971 char *msgstr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002972 u_header_T *uhp;
2973 char_u msgbuf[80];
Bram Moolenaar1e607892006-03-13 22:15:53 +00002974
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975#ifdef FEAT_FOLDING
2976 if ((fdo_flags & FDO_UNDO) && KeyTyped)
2977 foldOpenCursor();
2978#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +00002979
Bram Moolenaare38eab22019-12-05 21:50:01 +01002980 if (global_busy // no messages now, wait until global is finished
2981 || !messaging()) // 'lazyredraw' set, don't do messages now
Bram Moolenaar1e607892006-03-13 22:15:53 +00002982 return;
2983
2984 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2985 --u_newcount;
2986
2987 u_oldcount -= u_newcount;
2988 if (u_oldcount == -1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002989 msgstr = N_("more line");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002990 else if (u_oldcount < 0)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002991 msgstr = N_("more lines");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002992 else if (u_oldcount == 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002993 msgstr = N_("line less");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002994 else if (u_oldcount > 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002995 msgstr = N_("fewer lines");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002996 else
2997 {
2998 u_oldcount = u_newcount;
2999 if (u_newcount == 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00003000 msgstr = N_("change");
Bram Moolenaar1e607892006-03-13 22:15:53 +00003001 else
Bram Moolenaar89d40322006-08-29 15:30:07 +00003002 msgstr = N_("changes");
Bram Moolenaar1e607892006-03-13 22:15:53 +00003003 }
3004
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003005 if (curbuf->b_u_curhead != NULL)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00003006 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003007 // For ":undo N" we prefer a "after #N" message.
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003008 if (absolute && curbuf->b_u_curhead->uh_next.ptr != NULL)
Bram Moolenaardb552d602006-03-23 22:59:57 +00003009 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003010 uhp = curbuf->b_u_curhead->uh_next.ptr;
Bram Moolenaardb552d602006-03-23 22:59:57 +00003011 did_undo = FALSE;
3012 }
3013 else if (did_undo)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00003014 uhp = curbuf->b_u_curhead;
3015 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003016 uhp = curbuf->b_u_curhead->uh_next.ptr;
Bram Moolenaar433f7c82006-03-21 21:29:36 +00003017 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00003018 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003019 uhp = curbuf->b_u_newhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +00003020
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003021 if (uhp == NULL)
3022 *msgbuf = NUL;
3023 else
Bram Moolenaar52410572019-10-27 05:12:45 +01003024 add_time(msgbuf, sizeof(msgbuf), uhp->uh_time);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003025
Bram Moolenaarb2c03502010-07-02 20:20:09 +02003026#ifdef FEAT_CONCEAL
3027 {
3028 win_T *wp;
3029
3030 FOR_ALL_WINDOWS(wp)
3031 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003032 if (wp->w_buffer == curbuf && wp->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02003033 redraw_win_later(wp, NOT_VALID);
3034 }
3035 }
3036#endif
3037
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003038 smsg_attr_keep(0, _("%ld %s; %s #%ld %s"),
Bram Moolenaarca003e12006-03-17 23:19:38 +00003039 u_oldcount < 0 ? -u_oldcount : u_oldcount,
Bram Moolenaar89d40322006-08-29 15:30:07 +00003040 _(msgstr),
Bram Moolenaar433f7c82006-03-21 21:29:36 +00003041 did_undo ? _("before") : _("after"),
3042 uhp == NULL ? 0L : uhp->uh_seq,
3043 msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044}
3045
3046/*
3047 * u_sync: stop adding to the current entry list
3048 */
3049 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003050u_sync(
Bram Moolenaare38eab22019-12-05 21:50:01 +01003051 int force) // Also sync when no_u_sync is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052{
Bram Moolenaare38eab22019-12-05 21:50:01 +01003053 // Skip it when already synced or syncing is disabled.
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003054 if (curbuf->b_u_synced || (!force && no_u_sync > 0))
3055 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003057 if (p_imst == IM_ON_THE_SPOT && im_is_preediting())
Bram Moolenaare38eab22019-12-05 21:50:01 +01003058 return; // XIM is busy, don't break an undo sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003060 if (get_undolevel() < 0)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003061 curbuf->b_u_synced = TRUE; // no entries, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 else
3063 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003064 u_getbot(); // compute ue_bot of previous u_save
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 curbuf->b_u_curhead = NULL;
3066 }
3067}
3068
3069/*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003070 * ":undolist": List the leafs of the undo tree
3071 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003072 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003073ex_undolist(exarg_T *eap UNUSED)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003074{
3075 garray_T ga;
3076 u_header_T *uhp;
3077 int mark;
3078 int nomark;
3079 int changes = 1;
3080 int i;
3081
3082 /*
3083 * 1: walk the tree to find all leafs, put the info in "ga".
3084 * 2: sort the lines
3085 * 3: display the list
3086 */
3087 mark = ++lastmark;
3088 nomark = ++lastmark;
3089 ga_init2(&ga, (int)sizeof(char *), 20);
3090
3091 uhp = curbuf->b_u_oldhead;
3092 while (uhp != NULL)
3093 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003094 if (uhp->uh_prev.ptr == NULL && uhp->uh_walk != nomark
Bram Moolenaarca003e12006-03-17 23:19:38 +00003095 && uhp->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003096 {
3097 if (ga_grow(&ga, 1) == FAIL)
3098 break;
Bram Moolenaarea391762018-04-08 13:07:22 +02003099 vim_snprintf((char *)IObuff, IOSIZE, "%6ld %7d ",
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003100 uhp->uh_seq, changes);
Bram Moolenaar52410572019-10-27 05:12:45 +01003101 add_time(IObuff + STRLEN(IObuff), IOSIZE - STRLEN(IObuff),
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003102 uhp->uh_time);
Bram Moolenaara800b422010-06-27 01:15:55 +02003103 if (uhp->uh_save_nr > 0)
3104 {
Bram Moolenaardba01a02010-11-03 19:32:42 +01003105 while (STRLEN(IObuff) < 33)
Bram Moolenaara800b422010-06-27 01:15:55 +02003106 STRCAT(IObuff, " ");
3107 vim_snprintf_add((char *)IObuff, IOSIZE,
3108 " %3ld", uhp->uh_save_nr);
3109 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003110 ((char_u **)(ga.ga_data))[ga.ga_len++] = vim_strsave(IObuff);
3111 }
3112
3113 uhp->uh_walk = mark;
3114
Bram Moolenaare38eab22019-12-05 21:50:01 +01003115 // go down in the tree if we haven't been there
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003116 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != nomark
3117 && uhp->uh_prev.ptr->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003118 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003119 uhp = uhp->uh_prev.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003120 ++changes;
3121 }
3122
Bram Moolenaare38eab22019-12-05 21:50:01 +01003123 // go to alternate branch if we haven't been there
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003124 else if (uhp->uh_alt_next.ptr != NULL
3125 && uhp->uh_alt_next.ptr->uh_walk != nomark
3126 && uhp->uh_alt_next.ptr->uh_walk != mark)
3127 uhp = uhp->uh_alt_next.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003128
Bram Moolenaare38eab22019-12-05 21:50:01 +01003129 // go up in the tree if we haven't been there and we are at the
3130 // start of alternate branches
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003131 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
3132 && uhp->uh_next.ptr->uh_walk != nomark
3133 && uhp->uh_next.ptr->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003134 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003135 uhp = uhp->uh_next.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003136 --changes;
3137 }
3138
3139 else
3140 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003141 // need to backtrack; mark this node as done
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003142 uhp->uh_walk = nomark;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003143 if (uhp->uh_alt_prev.ptr != NULL)
3144 uhp = uhp->uh_alt_prev.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003145 else
3146 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003147 uhp = uhp->uh_next.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003148 --changes;
3149 }
3150 }
3151 }
3152
3153 if (ga.ga_len == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003154 msg(_("Nothing to undo"));
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003155 else
3156 {
3157 sort_strings((char_u **)ga.ga_data, ga.ga_len);
3158
3159 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01003160 msg_puts_attr(_("number changes when saved"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01003161 HL_ATTR(HLF_T));
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003162 for (i = 0; i < ga.ga_len && !got_int; ++i)
3163 {
3164 msg_putchar('\n');
3165 if (got_int)
3166 break;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003167 msg_puts(((char **)ga.ga_data)[i]);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003168 }
3169 msg_end();
3170
3171 ga_clear_strings(&ga);
3172 }
3173}
3174
3175/*
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003176 * ":undojoin": continue adding to the last entry list
3177 */
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003178 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003179ex_undojoin(exarg_T *eap UNUSED)
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003180{
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003181 if (curbuf->b_u_newhead == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003182 return; // nothing changed before
Bram Moolenaar57657d82006-04-21 22:12:41 +00003183 if (curbuf->b_u_curhead != NULL)
3184 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003185 emsg(_("E790: undojoin is not allowed after undo"));
Bram Moolenaar57657d82006-04-21 22:12:41 +00003186 return;
3187 }
3188 if (!curbuf->b_u_synced)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003189 return; // already unsynced
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003190 if (get_undolevel() < 0)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003191 return; // no entries, nothing to do
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003192 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01003193 // Append next change to the last entry
Bram Moolenaar5e4e1b12017-01-17 22:09:45 +01003194 curbuf->b_u_synced = FALSE;
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003195}
3196
3197/*
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003198 * Called after writing or reloading the file and setting b_changed to FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 * Now an undo means that the buffer is modified.
3200 */
3201 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003202u_unchanged(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203{
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003204 u_unch_branch(buf->b_u_oldhead);
3205 buf->b_did_warn = FALSE;
3206}
3207
Bram Moolenaar730cde92010-06-27 05:18:54 +02003208/*
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003209 * After reloading a buffer which was saved for 'undoreload': Find the first
3210 * line that was changed and set the cursor there.
3211 */
3212 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003213u_find_first_changed(void)
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003214{
3215 u_header_T *uhp = curbuf->b_u_newhead;
3216 u_entry_T *uep;
3217 linenr_T lnum;
3218
3219 if (curbuf->b_u_curhead != NULL || uhp == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003220 return; // undid something in an autocmd?
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003221
Bram Moolenaare38eab22019-12-05 21:50:01 +01003222 // Check that the last undo block was for the whole file.
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003223 uep = uhp->uh_entry;
3224 if (uep->ue_top != 0 || uep->ue_bot != 0)
3225 return;
3226
3227 for (lnum = 1; lnum < curbuf->b_ml.ml_line_count
3228 && lnum <= uep->ue_size; ++lnum)
Bram Moolenaarccae4672019-01-04 15:09:57 +01003229 {
3230 char_u *p = ml_get_buf(curbuf, lnum, FALSE);
3231
3232 if (uep->ue_array[lnum - 1].ul_len != curbuf->b_ml.ml_line_len
3233 || memcmp(p, uep->ue_array[lnum - 1].ul_line, uep->ue_array[lnum - 1].ul_len) != 0)
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003234 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003235 CLEAR_POS(&(uhp->uh_cursor));
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003236 uhp->uh_cursor.lnum = lnum;
3237 return;
3238 }
Bram Moolenaarccae4672019-01-04 15:09:57 +01003239 }
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003240 if (curbuf->b_ml.ml_line_count != uep->ue_size)
3241 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003242 // lines added or deleted at the end, put the cursor there
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003243 CLEAR_POS(&(uhp->uh_cursor));
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003244 uhp->uh_cursor.lnum = lnum;
3245 }
3246}
3247
3248/*
Bram Moolenaar730cde92010-06-27 05:18:54 +02003249 * Increase the write count, store it in the last undo header, what would be
3250 * used for "u".
3251 */
3252 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003253u_update_save_nr(buf_T *buf)
Bram Moolenaar730cde92010-06-27 05:18:54 +02003254{
3255 u_header_T *uhp;
3256
3257 ++buf->b_u_save_nr_last;
3258 buf->b_u_save_nr_cur = buf->b_u_save_nr_last;
3259 uhp = buf->b_u_curhead;
3260 if (uhp != NULL)
3261 uhp = uhp->uh_next.ptr;
3262 else
3263 uhp = buf->b_u_newhead;
3264 if (uhp != NULL)
3265 uhp->uh_save_nr = buf->b_u_save_nr_last;
3266}
3267
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003268 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003269u_unch_branch(u_header_T *uhp)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003270{
Bram Moolenaar1e607892006-03-13 22:15:53 +00003271 u_header_T *uh;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003273 for (uh = uhp; uh != NULL; uh = uh->uh_prev.ptr)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003274 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 uh->uh_flags |= UH_CHANGED;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003276 if (uh->uh_alt_next.ptr != NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003277 u_unch_branch(uh->uh_alt_next.ptr); // recursive
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279}
3280
3281/*
3282 * Get pointer to last added entry.
3283 * If it's not valid, give an error message and return NULL.
3284 */
3285 static u_entry_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003286u_get_headentry(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287{
3288 if (curbuf->b_u_newhead == NULL || curbuf->b_u_newhead->uh_entry == NULL)
3289 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003290 iemsg(_("E439: undo list corrupt"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 return NULL;
3292 }
3293 return curbuf->b_u_newhead->uh_entry;
3294}
3295
3296/*
3297 * u_getbot(): compute the line number of the previous u_save
3298 * It is called only when b_u_synced is FALSE.
3299 */
3300 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003301u_getbot(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302{
3303 u_entry_T *uep;
3304 linenr_T extra;
3305
Bram Moolenaare38eab22019-12-05 21:50:01 +01003306 uep = u_get_headentry(); // check for corrupt undo list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 if (uep == NULL)
3308 return;
3309
3310 uep = curbuf->b_u_newhead->uh_getbot_entry;
3311 if (uep != NULL)
3312 {
3313 /*
3314 * the new ue_bot is computed from the number of lines that has been
3315 * inserted (0 - deleted) since calling u_save. This is equal to the
3316 * old line count subtracted from the current line count.
3317 */
3318 extra = curbuf->b_ml.ml_line_count - uep->ue_lcount;
3319 uep->ue_bot = uep->ue_top + uep->ue_size + 1 + extra;
3320 if (uep->ue_bot < 1 || uep->ue_bot > curbuf->b_ml.ml_line_count)
3321 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003322 iemsg(_("E440: undo line missing"));
Bram Moolenaare38eab22019-12-05 21:50:01 +01003323 uep->ue_bot = uep->ue_top + 1; // assume all lines deleted, will
3324 // get all the old lines back
3325 // without deleting the current
3326 // ones
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 }
3328
3329 curbuf->b_u_newhead->uh_getbot_entry = NULL;
3330 }
3331
3332 curbuf->b_u_synced = TRUE;
3333}
3334
3335/*
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003336 * Free one header "uhp" and its entry list and adjust the pointers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 */
3338 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003339u_freeheader(
3340 buf_T *buf,
3341 u_header_T *uhp,
Bram Moolenaare38eab22019-12-05 21:50:01 +01003342 u_header_T **uhpp) // if not NULL reset when freeing this header
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343{
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003344 u_header_T *uhap;
3345
Bram Moolenaare38eab22019-12-05 21:50:01 +01003346 // When there is an alternate redo list free that branch completely,
3347 // because we can never go there.
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003348 if (uhp->uh_alt_next.ptr != NULL)
3349 u_freebranch(buf, uhp->uh_alt_next.ptr, uhpp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003351 if (uhp->uh_alt_prev.ptr != NULL)
3352 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353
Bram Moolenaare38eab22019-12-05 21:50:01 +01003354 // Update the links in the list to remove the header.
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003355 if (uhp->uh_next.ptr == NULL)
3356 buf->b_u_oldhead = uhp->uh_prev.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003358 uhp->uh_next.ptr->uh_prev.ptr = uhp->uh_prev.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003360 if (uhp->uh_prev.ptr == NULL)
3361 buf->b_u_newhead = uhp->uh_next.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003363 for (uhap = uhp->uh_prev.ptr; uhap != NULL;
3364 uhap = uhap->uh_alt_next.ptr)
3365 uhap->uh_next.ptr = uhp->uh_next.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366
Bram Moolenaar1e607892006-03-13 22:15:53 +00003367 u_freeentries(buf, uhp, uhpp);
3368}
3369
3370/*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003371 * Free an alternate branch and any following alternate branches.
Bram Moolenaar1e607892006-03-13 22:15:53 +00003372 */
3373 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003374u_freebranch(
3375 buf_T *buf,
3376 u_header_T *uhp,
Bram Moolenaare38eab22019-12-05 21:50:01 +01003377 u_header_T **uhpp) // if not NULL reset when freeing this header
Bram Moolenaar1e607892006-03-13 22:15:53 +00003378{
3379 u_header_T *tofree, *next;
3380
Bram Moolenaare38eab22019-12-05 21:50:01 +01003381 // If this is the top branch we may need to use u_freeheader() to update
3382 // all the pointers.
Bram Moolenaar07d06772007-11-10 21:51:15 +00003383 if (uhp == buf->b_u_oldhead)
3384 {
Bram Moolenaaraa887322013-11-07 03:04:11 +01003385 while (buf->b_u_oldhead != NULL)
3386 u_freeheader(buf, buf->b_u_oldhead, uhpp);
Bram Moolenaar07d06772007-11-10 21:51:15 +00003387 return;
3388 }
3389
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003390 if (uhp->uh_alt_prev.ptr != NULL)
3391 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +00003392
3393 next = uhp;
3394 while (next != NULL)
3395 {
3396 tofree = next;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003397 if (tofree->uh_alt_next.ptr != NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003398 u_freebranch(buf, tofree->uh_alt_next.ptr, uhpp); // recursive
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003399 next = tofree->uh_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00003400 u_freeentries(buf, tofree, uhpp);
3401 }
3402}
3403
3404/*
3405 * Free all the undo entries for one header and the header itself.
3406 * This means that "uhp" is invalid when returning.
3407 */
3408 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003409u_freeentries(
3410 buf_T *buf,
3411 u_header_T *uhp,
Bram Moolenaare38eab22019-12-05 21:50:01 +01003412 u_header_T **uhpp) // if not NULL reset when freeing this header
Bram Moolenaar1e607892006-03-13 22:15:53 +00003413{
3414 u_entry_T *uep, *nuep;
3415
Bram Moolenaare38eab22019-12-05 21:50:01 +01003416 // Check for pointers to the header that become invalid now.
Bram Moolenaar1e607892006-03-13 22:15:53 +00003417 if (buf->b_u_curhead == uhp)
3418 buf->b_u_curhead = NULL;
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003419 if (buf->b_u_newhead == uhp)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003420 buf->b_u_newhead = NULL; // freeing the newest entry
Bram Moolenaar1e607892006-03-13 22:15:53 +00003421 if (uhpp != NULL && uhp == *uhpp)
3422 *uhpp = NULL;
3423
3424 for (uep = uhp->uh_entry; uep != NULL; uep = nuep)
3425 {
3426 nuep = uep->ue_next;
3427 u_freeentry(uep, uep->ue_size);
3428 }
3429
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003430#ifdef U_DEBUG
3431 uhp->uh_magic = 0;
3432#endif
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003433 vim_free((char_u *)uhp);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003434 --buf->b_u_numhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435}
3436
3437/*
3438 * free entry 'uep' and 'n' lines in uep->ue_array[]
3439 */
3440 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003441u_freeentry(u_entry_T *uep, long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442{
Bram Moolenaar8d343302005-07-12 22:46:17 +00003443 while (n > 0)
Bram Moolenaarccae4672019-01-04 15:09:57 +01003444 vim_free(uep->ue_array[--n].ul_line);
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003445 vim_free((char_u *)uep->ue_array);
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003446#ifdef U_DEBUG
3447 uep->ue_magic = 0;
3448#endif
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003449 vim_free((char_u *)uep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450}
3451
3452/*
3453 * invalidate the undo buffer; called when storage has already been released
3454 */
3455 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003456u_clearall(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457{
3458 buf->b_u_newhead = buf->b_u_oldhead = buf->b_u_curhead = NULL;
3459 buf->b_u_synced = TRUE;
3460 buf->b_u_numhead = 0;
Bram Moolenaarccae4672019-01-04 15:09:57 +01003461 buf->b_u_line_ptr.ul_line = NULL;
3462 buf->b_u_line_ptr.ul_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 buf->b_u_line_lnum = 0;
3464}
3465
3466/*
Bram Moolenaarccae4672019-01-04 15:09:57 +01003467 * Save the line "lnum" for the "U" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003469 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003470u_saveline(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471{
Bram Moolenaare38eab22019-12-05 21:50:01 +01003472 if (lnum == curbuf->b_u_line_lnum) // line is already saved
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 return;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003474 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) // should never happen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 return;
3476 u_clearline();
3477 curbuf->b_u_line_lnum = lnum;
3478 if (curwin->w_cursor.lnum == lnum)
3479 curbuf->b_u_line_colnr = curwin->w_cursor.col;
3480 else
3481 curbuf->b_u_line_colnr = 0;
Bram Moolenaarccae4672019-01-04 15:09:57 +01003482 if (u_save_line(&curbuf->b_u_line_ptr, lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 do_outofmem_msg((long_u)0);
3484}
3485
3486/*
3487 * clear the line saved for the "U" command
3488 * (this is used externally for crossing a line while in insert mode)
3489 */
3490 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003491u_clearline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492{
Bram Moolenaarccae4672019-01-04 15:09:57 +01003493 if (curbuf->b_u_line_ptr.ul_line != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 {
Bram Moolenaarccae4672019-01-04 15:09:57 +01003495 VIM_CLEAR(curbuf->b_u_line_ptr.ul_line);
3496 curbuf->b_u_line_ptr.ul_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 curbuf->b_u_line_lnum = 0;
3498 }
3499}
3500
3501/*
3502 * Implementation of the "U" command.
3503 * Differentiation from vi: "U" can be undone with the next "U".
3504 * We also allow the cursor to be in another line.
Bram Moolenaard04b7502010-07-08 22:27:55 +02003505 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 */
3507 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003508u_undoline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509{
Bram Moolenaarccae4672019-01-04 15:09:57 +01003510 colnr_T t;
3511 undoline_T oldp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512
3513 if (undo_off)
3514 return;
3515
Bram Moolenaarccae4672019-01-04 15:09:57 +01003516 if (curbuf->b_u_line_ptr.ul_line == NULL
Bram Moolenaare3300c82008-02-13 14:21:38 +00003517 || curbuf->b_u_line_lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 {
3519 beep_flush();
3520 return;
3521 }
Bram Moolenaare3300c82008-02-13 14:21:38 +00003522
Bram Moolenaarccae4672019-01-04 15:09:57 +01003523 // first save the line for the 'u' command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 if (u_savecommon(curbuf->b_u_line_lnum - 1,
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003525 curbuf->b_u_line_lnum + 1, (linenr_T)0, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 return;
Bram Moolenaarccae4672019-01-04 15:09:57 +01003527 if (u_save_line(&oldp, curbuf->b_u_line_lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 {
3529 do_outofmem_msg((long_u)0);
3530 return;
3531 }
Bram Moolenaar02c037a2020-08-30 19:26:45 +02003532 ml_replace_len(curbuf->b_u_line_lnum, curbuf->b_u_line_ptr.ul_line,
3533 curbuf->b_u_line_ptr.ul_len, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 changed_bytes(curbuf->b_u_line_lnum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 curbuf->b_u_line_ptr = oldp;
3536
3537 t = curbuf->b_u_line_colnr;
3538 if (curwin->w_cursor.lnum == curbuf->b_u_line_lnum)
3539 curbuf->b_u_line_colnr = curwin->w_cursor.col;
3540 curwin->w_cursor.col = t;
3541 curwin->w_cursor.lnum = curbuf->b_u_line_lnum;
Bram Moolenaare3300c82008-02-13 14:21:38 +00003542 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543}
3544
3545/*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003546 * Free all allocated memory blocks for the buffer 'buf'.
3547 */
3548 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003549u_blockfree(buf_T *buf)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003550{
Bram Moolenaar1e607892006-03-13 22:15:53 +00003551 while (buf->b_u_oldhead != NULL)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003552 u_freeheader(buf, buf->b_u_oldhead, NULL);
Bram Moolenaarccae4672019-01-04 15:09:57 +01003553 vim_free(buf->b_u_line_ptr.ul_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554}
3555
3556/*
3557 * Check if the 'modified' flag is set, or 'ff' has changed (only need to
3558 * check the first character, because it can only be "dos", "unix" or "mac").
3559 * "nofile" and "scratch" type buffers are considered to always be unchanged.
Bram Moolenaarf405c8f2017-12-09 19:51:49 +01003560 * Also considers a buffer changed when a terminal window contains a running
3561 * job.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 */
3563 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003564bufIsChanged(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565{
Bram Moolenaareb44a682017-08-03 22:44:55 +02003566#ifdef FEAT_TERMINAL
3567 if (term_job_running(buf->b_term))
3568 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569#endif
Bram Moolenaarf405c8f2017-12-09 19:51:49 +01003570 return bufIsChangedNotTerm(buf);
3571}
3572
3573/*
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01003574 * Return TRUE if any buffer has changes. Also buffers that are not written.
3575 */
3576 int
3577anyBufIsChanged(void)
3578{
3579 buf_T *buf;
3580
3581 FOR_ALL_BUFFERS(buf)
3582 if (bufIsChanged(buf))
3583 return TRUE;
Bram Moolenaard6c3f1f2019-03-26 00:31:21 +01003584 return FALSE;
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01003585}
3586
3587/*
Bram Moolenaarf405c8f2017-12-09 19:51:49 +01003588 * Like bufIsChanged() but ignoring a terminal window.
3589 */
3590 int
3591bufIsChangedNotTerm(buf_T *buf)
3592{
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003593 // In a "prompt" buffer we do respect 'modified', so that we can control
3594 // closing the window by setting or resetting that option.
3595 return (!bt_dontwrite(buf) || bt_prompt(buf))
Bram Moolenaareb44a682017-08-03 22:44:55 +02003596 && (buf->b_changed || file_ff_differs(buf, TRUE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597}
3598
3599 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003600curbufIsChanged(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601{
Bram Moolenaareb44a682017-08-03 22:44:55 +02003602 return bufIsChanged(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603}
Bram Moolenaara800b422010-06-27 01:15:55 +02003604
3605#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003606
Bram Moolenaara800b422010-06-27 01:15:55 +02003607/*
3608 * For undotree(): Append the list of undo blocks at "first_uhp" to "list".
3609 * Recursive.
3610 */
Bram Moolenaar840d16f2019-09-10 21:27:18 +02003611 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003612u_eval_tree(u_header_T *first_uhp, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02003613{
3614 u_header_T *uhp = first_uhp;
3615 dict_T *dict;
3616
3617 while (uhp != NULL)
3618 {
3619 dict = dict_alloc();
3620 if (dict == NULL)
3621 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003622 dict_add_number(dict, "seq", uhp->uh_seq);
3623 dict_add_number(dict, "time", (long)uhp->uh_time);
Bram Moolenaara800b422010-06-27 01:15:55 +02003624 if (uhp == curbuf->b_u_newhead)
Bram Moolenaare0be1672018-07-08 16:50:37 +02003625 dict_add_number(dict, "newhead", 1);
Bram Moolenaara800b422010-06-27 01:15:55 +02003626 if (uhp == curbuf->b_u_curhead)
Bram Moolenaare0be1672018-07-08 16:50:37 +02003627 dict_add_number(dict, "curhead", 1);
Bram Moolenaara800b422010-06-27 01:15:55 +02003628 if (uhp->uh_save_nr > 0)
Bram Moolenaare0be1672018-07-08 16:50:37 +02003629 dict_add_number(dict, "save", uhp->uh_save_nr);
Bram Moolenaara800b422010-06-27 01:15:55 +02003630
3631 if (uhp->uh_alt_next.ptr != NULL)
3632 {
3633 list_T *alt_list = list_alloc();
3634
3635 if (alt_list != NULL)
3636 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003637 // Recursive call to add alternate undo tree.
Bram Moolenaara800b422010-06-27 01:15:55 +02003638 u_eval_tree(uhp->uh_alt_next.ptr, alt_list);
3639 dict_add_list(dict, "alt", alt_list);
3640 }
3641 }
3642
3643 list_append_dict(list, dict);
3644 uhp = uhp->uh_prev.ptr;
3645 }
3646}
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003647
3648/*
3649 * "undofile(name)" function
3650 */
3651 void
3652f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
3653{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003654 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
3655 return;
3656
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003657 rettv->v_type = VAR_STRING;
3658#ifdef FEAT_PERSISTENT_UNDO
3659 {
3660 char_u *fname = tv_get_string(&argvars[0]);
3661
3662 if (*fname == NUL)
3663 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003664 // If there is no file name there will be no undo file.
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003665 rettv->vval.v_string = NULL;
3666 }
3667 else
3668 {
3669 char_u *ffname = FullName_save(fname, TRUE);
3670
3671 if (ffname != NULL)
3672 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
3673 vim_free(ffname);
3674 }
3675 }
3676#else
3677 rettv->vval.v_string = NULL;
3678#endif
3679}
Christian Brabandt8a4c8122021-07-25 14:36:05 +02003680#ifdef FEAT_PERSISTENT_UNDO
3681/*
3682 * Reset undofile option and delete the undofile
3683 */
3684 void
3685u_undofile_reset_and_delete(buf_T *buf)
3686{
3687 char_u *file_name;
3688
3689 if (!buf->b_p_udf)
3690 return;
3691
3692 file_name = u_get_undo_file_name(buf->b_ffname, TRUE);
3693 if (file_name != NULL)
3694 {
3695 mch_remove(file_name);
3696 vim_free(file_name);
3697 }
3698
3699 set_option_value((char_u *)"undofile", 0L, NULL, OPT_LOCAL);
3700}
3701 #endif
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003702
3703/*
3704 * "undotree()" function
3705 */
3706 void
3707f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
3708{
3709 if (rettv_dict_alloc(rettv) == OK)
3710 {
3711 dict_T *dict = rettv->vval.v_dict;
3712 list_T *list;
3713
3714 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
3715 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
3716 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
3717 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
3718 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
3719 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
3720
3721 list = list_alloc();
3722 if (list != NULL)
3723 {
3724 u_eval_tree(curbuf->b_u_oldhead, list);
3725 dict_add_list(dict, "entries", list);
3726 }
3727 }
3728}
3729
Bram Moolenaara800b422010-06-27 01:15:55 +02003730#endif