blob: c2fe819460ba7d3863501a703e504693e07c7a24 [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 Moolenaarf9e3e092019-01-13 23:38:42 +0100319 emsg(_(e_modifiable));
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000320 return FALSE;
321 }
322
323#ifdef HAVE_SANDBOX
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 Moolenaarf9e3e092019-01-13 23:38:42 +0100327 emsg(_(e_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 Moolenaar442b4222010-05-24 21:34:22 +0200852 undo_file_name = vim_strnsave(ffname, (int)(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 {
966 crypt_encode_inplace(bi->bi_state, bi->bi_buffer, bi->bi_used);
967 if (fwrite(bi->bi_buffer, bi->bi_used, (size_t)1, bi->bi_fp) != 1)
968 return FAIL;
969 bi->bi_used = 0;
970 }
971 return OK;
972}
973#endif
974
975/*
976 * Write "ptr[len]" and crypt the bytes when needed.
977 * Returns OK or FAIL.
978 */
979 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100980fwrite_crypt(bufinfo_T *bi, char_u *ptr, size_t len)
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200981{
982#ifdef FEAT_CRYPT
983 char_u *copy;
984 char_u small_buf[100];
985 size_t i;
986
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200987 if (bi->bi_state != NULL && bi->bi_buffer == NULL)
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200988 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100989 // crypting every piece of text separately
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200990 if (len < 100)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100991 copy = small_buf; // no malloc()/free() for short strings
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200992 else
993 {
994 copy = lalloc(len, FALSE);
995 if (copy == NULL)
996 return 0;
997 }
998 crypt_encode(bi->bi_state, ptr, len, copy);
999 i = fwrite(copy, len, (size_t)1, bi->bi_fp);
1000 if (copy != small_buf)
1001 vim_free(copy);
1002 return i == 1 ? OK : FAIL;
Bram Moolenaar191e0a22010-06-14 01:39:13 +02001003 }
Bram Moolenaar191e0a22010-06-14 01:39:13 +02001004#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001005 return undo_write(bi, ptr, len);
Bram Moolenaar191e0a22010-06-14 01:39:13 +02001006}
1007
1008/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001009 * Write a number, MSB first, in "len" bytes.
1010 * Must match with undo_read_?c() functions.
1011 * Returns OK or FAIL.
Bram Moolenaar191e0a22010-06-14 01:39:13 +02001012 */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001013 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001014undo_write_bytes(bufinfo_T *bi, long_u nr, int len)
Bram Moolenaar191e0a22010-06-14 01:39:13 +02001015{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001016 char_u buf[8];
1017 int i;
1018 int bufi = 0;
Bram Moolenaar191e0a22010-06-14 01:39:13 +02001019
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001020 for (i = len - 1; i >= 0; --i)
Bram Moolenaar9b8f0212014-08-13 22:05:53 +02001021 buf[bufi++] = (char_u)(nr >> (i * 8));
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001022 return undo_write(bi, buf, (size_t)len);
1023}
1024
1025/*
1026 * Write the pointer to an undo header. Instead of writing the pointer itself
1027 * we use the sequence number of the header. This is converted back to
1028 * pointers when reading. */
1029 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001030put_header_ptr(bufinfo_T *bi, u_header_T *uhp)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001031{
1032 undo_write_bytes(bi, (long_u)(uhp != NULL ? uhp->uh_seq : 0), 4);
Bram Moolenaar191e0a22010-06-14 01:39:13 +02001033}
1034
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001035 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001036undo_read_4c(bufinfo_T *bi)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001037{
1038#ifdef FEAT_CRYPT
1039 if (bi->bi_buffer != NULL)
1040 {
1041 char_u buf[4];
1042 int n;
1043
1044 undo_read(bi, buf, (size_t)4);
Bram Moolenaar35169282014-09-11 22:50:09 +02001045 n = ((unsigned)buf[0] << 24) + (buf[1] << 16) + (buf[2] << 8) + buf[3];
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001046 return n;
1047 }
1048#endif
1049 return get4c(bi->bi_fp);
1050}
1051
1052 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001053undo_read_2c(bufinfo_T *bi)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001054{
1055#ifdef FEAT_CRYPT
1056 if (bi->bi_buffer != NULL)
1057 {
1058 char_u buf[2];
1059 int n;
1060
1061 undo_read(bi, buf, (size_t)2);
1062 n = (buf[0] << 8) + buf[1];
1063 return n;
1064 }
1065#endif
1066 return get2c(bi->bi_fp);
1067}
1068
1069 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001070undo_read_byte(bufinfo_T *bi)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001071{
1072#ifdef FEAT_CRYPT
1073 if (bi->bi_buffer != NULL)
1074 {
1075 char_u buf[1];
1076
1077 undo_read(bi, buf, (size_t)1);
1078 return buf[0];
1079 }
1080#endif
1081 return getc(bi->bi_fp);
1082}
1083
1084 static time_t
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001085undo_read_time(bufinfo_T *bi)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001086{
1087#ifdef FEAT_CRYPT
1088 if (bi->bi_buffer != NULL)
1089 {
1090 char_u buf[8];
1091 time_t n = 0;
1092 int i;
1093
1094 undo_read(bi, buf, (size_t)8);
1095 for (i = 0; i < 8; ++i)
1096 n = (n << 8) + buf[i];
1097 return n;
1098 }
1099#endif
1100 return get8ctime(bi->bi_fp);
1101}
1102
1103/*
1104 * Read "buffer[size]" from the undo file.
1105 * Return OK or FAIL.
1106 */
1107 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001108undo_read(bufinfo_T *bi, char_u *buffer, size_t size)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001109{
Bram Moolenaar56f2db52017-06-11 23:09:15 +02001110 int retval = OK;
1111
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001112#ifdef FEAT_CRYPT
1113 if (bi->bi_buffer != NULL)
1114 {
Bram Moolenaar9b8f0212014-08-13 22:05:53 +02001115 int size_todo = (int)size;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001116 char_u *p = buffer;
1117
1118 while (size_todo > 0)
1119 {
1120 size_t n;
1121
1122 if (bi->bi_used >= bi->bi_avail)
1123 {
1124 n = fread(bi->bi_buffer, 1, (size_t)CRYPT_BUF_SIZE, bi->bi_fp);
Bram Moolenaar55952d42016-11-05 14:58:34 +01001125 if (n == 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001126 {
Bram Moolenaar56f2db52017-06-11 23:09:15 +02001127 retval = FAIL;
1128 break;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001129 }
1130 bi->bi_avail = n;
1131 bi->bi_used = 0;
1132 crypt_decode_inplace(bi->bi_state, bi->bi_buffer, bi->bi_avail);
1133 }
1134 n = size_todo;
1135 if (n > bi->bi_avail - bi->bi_used)
1136 n = bi->bi_avail - bi->bi_used;
1137 mch_memmove(p, bi->bi_buffer + bi->bi_used, n);
1138 bi->bi_used += n;
Bram Moolenaar9b8f0212014-08-13 22:05:53 +02001139 size_todo -= (int)n;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001140 p += n;
1141 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001142 }
Bram Moolenaar56f2db52017-06-11 23:09:15 +02001143 else
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001144#endif
1145 if (fread(buffer, (size_t)size, 1, bi->bi_fp) != 1)
Bram Moolenaar56f2db52017-06-11 23:09:15 +02001146 retval = FAIL;
1147
1148 if (retval == FAIL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01001149 // Error may be checked for only later. Fill with zeros,
1150 // so that the reader won't use garbage.
Bram Moolenaar56f2db52017-06-11 23:09:15 +02001151 vim_memset(buffer, 0, size);
1152 return retval;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001153}
1154
1155/*
1156 * Read a string of length "len" from "bi->bi_fd".
1157 * "len" can be zero to allocate an empty line.
1158 * Decrypt the bytes if needed.
1159 * Append a NUL.
1160 * Returns a pointer to allocated memory or NULL for failure.
1161 */
1162 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001163read_string_decrypt(bufinfo_T *bi, int len)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001164{
Bram Moolenaar964b3742019-05-24 18:54:09 +02001165 char_u *ptr = alloc(len + 1);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001166
1167 if (ptr != NULL)
1168 {
1169 if (len > 0 && undo_read(bi, ptr, len) == FAIL)
1170 {
1171 vim_free(ptr);
1172 return NULL;
1173 }
Bram Moolenaarccae4672019-01-04 15:09:57 +01001174 // In case there are text properties there already is a NUL, but
1175 // checking for that is more expensive than just adding a dummy byte.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001176 ptr[len] = NUL;
1177#ifdef FEAT_CRYPT
1178 if (bi->bi_state != NULL && bi->bi_buffer == NULL)
1179 crypt_decode_inplace(bi->bi_state, ptr, len);
1180#endif
1181 }
1182 return ptr;
1183}
1184
1185/*
1186 * Writes the (not encrypted) header and initializes encryption if needed.
1187 */
1188 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001189serialize_header(bufinfo_T *bi, char_u *hash)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001190{
Bram Moolenaarccae4672019-01-04 15:09:57 +01001191 long len;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001192 buf_T *buf = bi->bi_buf;
1193 FILE *fp = bi->bi_fp;
1194 char_u time_buf[8];
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001195
Bram Moolenaare38eab22019-12-05 21:50:01 +01001196 // Start writing, first the magic marker and undo info version.
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001197 if (fwrite(UF_START_MAGIC, (size_t)UF_START_MAGIC_LEN, (size_t)1, fp) != 1)
1198 return FAIL;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001199
Bram Moolenaare38eab22019-12-05 21:50:01 +01001200 // If the buffer is encrypted then all text bytes following will be
1201 // encrypted. Numbers and other info is not crypted.
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001202#ifdef FEAT_CRYPT
Bram Moolenaarfa0ff9a2010-07-25 16:05:19 +02001203 if (*buf->b_p_key != NUL)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001204 {
1205 char_u *header;
1206 int header_len;
1207
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001208 undo_write_bytes(bi, (long_u)UF_VERSION_CRYPT, 2);
1209 bi->bi_state = crypt_create_for_writing(crypt_get_method_nr(buf),
1210 buf->b_p_key, &header, &header_len);
1211 if (bi->bi_state == NULL)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001212 return FAIL;
Bram Moolenaarccae4672019-01-04 15:09:57 +01001213 len = (long)fwrite(header, (size_t)header_len, (size_t)1, fp);
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001214 vim_free(header);
1215 if (len != 1)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001216 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001217 crypt_free_state(bi->bi_state);
1218 bi->bi_state = NULL;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001219 return FAIL;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001220 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001221
1222 if (crypt_whole_undofile(crypt_get_method_nr(buf)))
1223 {
1224 bi->bi_buffer = alloc(CRYPT_BUF_SIZE);
1225 if (bi->bi_buffer == NULL)
1226 {
1227 crypt_free_state(bi->bi_state);
1228 bi->bi_state = NULL;
1229 return FAIL;
1230 }
1231 bi->bi_used = 0;
1232 }
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001233 }
1234 else
1235#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001236 undo_write_bytes(bi, (long_u)UF_VERSION, 2);
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001237
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001238
Bram Moolenaare38eab22019-12-05 21:50:01 +01001239 // Write a hash of the buffer text, so that we can verify it is still the
1240 // same when reading the buffer text.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001241 if (undo_write(bi, hash, (size_t)UNDO_HASH_SIZE) == FAIL)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001242 return FAIL;
1243
Bram Moolenaare38eab22019-12-05 21:50:01 +01001244 // buffer-specific data
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001245 undo_write_bytes(bi, (long_u)buf->b_ml.ml_line_count, 4);
Bram Moolenaarccae4672019-01-04 15:09:57 +01001246 len = buf->b_u_line_ptr.ul_line == NULL
Bram Moolenaar8aef43b2019-01-08 20:14:35 +01001247 ? 0L : (long)STRLEN(buf->b_u_line_ptr.ul_line);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001248 undo_write_bytes(bi, (long_u)len, 4);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +01001249 if (len > 0 && fwrite_crypt(bi, buf->b_u_line_ptr.ul_line, (size_t)len)
1250 == FAIL)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001251 return FAIL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001252 undo_write_bytes(bi, (long_u)buf->b_u_line_lnum, 4);
1253 undo_write_bytes(bi, (long_u)buf->b_u_line_colnr, 4);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001254
Bram Moolenaare38eab22019-12-05 21:50:01 +01001255 // Undo structures header data
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001256 put_header_ptr(bi, buf->b_u_oldhead);
1257 put_header_ptr(bi, buf->b_u_newhead);
1258 put_header_ptr(bi, buf->b_u_curhead);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001259
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001260 undo_write_bytes(bi, (long_u)buf->b_u_numhead, 4);
1261 undo_write_bytes(bi, (long_u)buf->b_u_seq_last, 4);
1262 undo_write_bytes(bi, (long_u)buf->b_u_seq_cur, 4);
1263 time_to_bytes(buf->b_u_time_cur, time_buf);
1264 undo_write(bi, time_buf, 8);
Bram Moolenaara800b422010-06-27 01:15:55 +02001265
Bram Moolenaare38eab22019-12-05 21:50:01 +01001266 // Optional fields.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001267 undo_write_bytes(bi, 4, 1);
1268 undo_write_bytes(bi, UF_LAST_SAVE_NR, 1);
1269 undo_write_bytes(bi, (long_u)buf->b_u_save_nr_last, 4);
Bram Moolenaara800b422010-06-27 01:15:55 +02001270
Bram Moolenaare38eab22019-12-05 21:50:01 +01001271 undo_write_bytes(bi, 0, 1); // end marker
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001272
1273 return OK;
1274}
1275
1276 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001277serialize_uhp(bufinfo_T *bi, u_header_T *uhp)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001278{
1279 int i;
1280 u_entry_T *uep;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001281 char_u time_buf[8];
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001282
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001283 if (undo_write_bytes(bi, (long_u)UF_HEADER_MAGIC, 2) == FAIL)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001284 return FAIL;
1285
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001286 put_header_ptr(bi, uhp->uh_next.ptr);
1287 put_header_ptr(bi, uhp->uh_prev.ptr);
1288 put_header_ptr(bi, uhp->uh_alt_next.ptr);
1289 put_header_ptr(bi, uhp->uh_alt_prev.ptr);
1290 undo_write_bytes(bi, uhp->uh_seq, 4);
1291 serialize_pos(bi, uhp->uh_cursor);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001292 undo_write_bytes(bi, (long_u)uhp->uh_cursor_vcol, 4);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001293 undo_write_bytes(bi, (long_u)uhp->uh_flags, 2);
Bram Moolenaare38eab22019-12-05 21:50:01 +01001294 // Assume NMARKS will stay the same.
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001295 for (i = 0; i < NMARKS; ++i)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001296 serialize_pos(bi, uhp->uh_namedm[i]);
1297 serialize_visualinfo(bi, &uhp->uh_visual);
1298 time_to_bytes(uhp->uh_time, time_buf);
1299 undo_write(bi, time_buf, 8);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001300
Bram Moolenaare38eab22019-12-05 21:50:01 +01001301 // Optional fields.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001302 undo_write_bytes(bi, 4, 1);
1303 undo_write_bytes(bi, UHP_SAVE_NR, 1);
1304 undo_write_bytes(bi, (long_u)uhp->uh_save_nr, 4);
Bram Moolenaara800b422010-06-27 01:15:55 +02001305
Bram Moolenaare38eab22019-12-05 21:50:01 +01001306 undo_write_bytes(bi, 0, 1); // end marker
Bram Moolenaara800b422010-06-27 01:15:55 +02001307
Bram Moolenaare38eab22019-12-05 21:50:01 +01001308 // Write all the entries.
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001309 for (uep = uhp->uh_entry; uep != NULL; uep = uep->ue_next)
1310 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001311 undo_write_bytes(bi, (long_u)UF_ENTRY_MAGIC, 2);
1312 if (serialize_uep(bi, uep) == FAIL)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001313 return FAIL;
1314 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001315 undo_write_bytes(bi, (long_u)UF_ENTRY_END_MAGIC, 2);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001316 return OK;
1317}
1318
1319 static u_header_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001320unserialize_uhp(bufinfo_T *bi, char_u *file_name)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001321{
1322 u_header_T *uhp;
1323 int i;
1324 u_entry_T *uep, *last_uep;
1325 int c;
1326 int error;
1327
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001328 uhp = U_ALLOC_LINE(sizeof(u_header_T));
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001329 if (uhp == NULL)
1330 return NULL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001331 CLEAR_POINTER(uhp);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001332#ifdef U_DEBUG
1333 uhp->uh_magic = UH_MAGIC;
1334#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001335 uhp->uh_next.seq = undo_read_4c(bi);
1336 uhp->uh_prev.seq = undo_read_4c(bi);
1337 uhp->uh_alt_next.seq = undo_read_4c(bi);
1338 uhp->uh_alt_prev.seq = undo_read_4c(bi);
1339 uhp->uh_seq = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001340 if (uhp->uh_seq <= 0)
1341 {
1342 corruption_error("uh_seq", file_name);
1343 vim_free(uhp);
1344 return NULL;
1345 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001346 unserialize_pos(bi, &uhp->uh_cursor);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001347 uhp->uh_cursor_vcol = undo_read_4c(bi);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001348 uhp->uh_flags = undo_read_2c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001349 for (i = 0; i < NMARKS; ++i)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001350 unserialize_pos(bi, &uhp->uh_namedm[i]);
1351 unserialize_visualinfo(bi, &uhp->uh_visual);
1352 uhp->uh_time = undo_read_time(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001353
Bram Moolenaare38eab22019-12-05 21:50:01 +01001354 // Optional fields.
Bram Moolenaar69154f22010-07-18 21:42:34 +02001355 for (;;)
1356 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001357 int len = undo_read_byte(bi);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001358 int what;
Bram Moolenaara800b422010-06-27 01:15:55 +02001359
Bram Moolenaarfb06d762019-08-04 18:55:35 +02001360 if (len == EOF)
1361 {
1362 corruption_error("truncated", file_name);
1363 u_free_uhp(uhp);
1364 return NULL;
1365 }
Bram Moolenaar69154f22010-07-18 21:42:34 +02001366 if (len == 0)
1367 break;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001368 what = undo_read_byte(bi);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001369 switch (what)
1370 {
1371 case UHP_SAVE_NR:
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001372 uhp->uh_save_nr = undo_read_4c(bi);
Bram Moolenaara800b422010-06-27 01:15:55 +02001373 break;
Bram Moolenaar69154f22010-07-18 21:42:34 +02001374 default:
Bram Moolenaare38eab22019-12-05 21:50:01 +01001375 // field not supported, skip
Bram Moolenaar69154f22010-07-18 21:42:34 +02001376 while (--len >= 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001377 (void)undo_read_byte(bi);
Bram Moolenaara800b422010-06-27 01:15:55 +02001378 }
Bram Moolenaar69154f22010-07-18 21:42:34 +02001379 }
Bram Moolenaara800b422010-06-27 01:15:55 +02001380
Bram Moolenaare38eab22019-12-05 21:50:01 +01001381 // Unserialize the uep list.
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001382 last_uep = NULL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001383 while ((c = undo_read_2c(bi)) == UF_ENTRY_MAGIC)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001384 {
1385 error = FALSE;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001386 uep = unserialize_uep(bi, &error, file_name);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001387 if (last_uep == NULL)
1388 uhp->uh_entry = uep;
1389 else
1390 last_uep->ue_next = uep;
1391 last_uep = uep;
1392 if (uep == NULL || error)
1393 {
1394 u_free_uhp(uhp);
1395 return NULL;
1396 }
1397 }
1398 if (c != UF_ENTRY_END_MAGIC)
1399 {
1400 corruption_error("entry end", file_name);
1401 u_free_uhp(uhp);
1402 return NULL;
1403 }
1404
1405 return uhp;
1406}
1407
Bram Moolenaar9db58062010-05-29 20:33:07 +02001408/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001409 * Serialize "uep".
Bram Moolenaar9db58062010-05-29 20:33:07 +02001410 */
1411 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001412serialize_uep(
1413 bufinfo_T *bi,
1414 u_entry_T *uep)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001415{
1416 int i;
1417 size_t len;
1418
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001419 undo_write_bytes(bi, (long_u)uep->ue_top, 4);
1420 undo_write_bytes(bi, (long_u)uep->ue_bot, 4);
1421 undo_write_bytes(bi, (long_u)uep->ue_lcount, 4);
1422 undo_write_bytes(bi, (long_u)uep->ue_size, 4);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001423 for (i = 0; i < uep->ue_size; ++i)
1424 {
Bram Moolenaarccae4672019-01-04 15:09:57 +01001425 // Text is written without the text properties, since we cannot restore
1426 // the text property types.
1427 len = STRLEN(uep->ue_array[i].ul_line);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001428 if (undo_write_bytes(bi, (long_u)len, 4) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001429 return FAIL;
Bram Moolenaarccae4672019-01-04 15:09:57 +01001430 if (len > 0 && fwrite_crypt(bi, uep->ue_array[i].ul_line, len) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001431 return FAIL;
1432 }
1433 return OK;
1434}
1435
1436 static u_entry_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001437unserialize_uep(bufinfo_T *bi, int *error, char_u *file_name)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001438{
1439 int i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001440 u_entry_T *uep;
Bram Moolenaarccae4672019-01-04 15:09:57 +01001441 undoline_T *array = NULL;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001442 char_u *line;
1443 int line_len;
1444
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001445 uep = U_ALLOC_LINE(sizeof(u_entry_T));
Bram Moolenaar9db58062010-05-29 20:33:07 +02001446 if (uep == NULL)
1447 return NULL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001448 CLEAR_POINTER(uep);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001449#ifdef U_DEBUG
1450 uep->ue_magic = UE_MAGIC;
1451#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001452 uep->ue_top = undo_read_4c(bi);
1453 uep->ue_bot = undo_read_4c(bi);
1454 uep->ue_lcount = undo_read_4c(bi);
1455 uep->ue_size = undo_read_4c(bi);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001456 if (uep->ue_size > 0)
1457 {
Bram Moolenaar0c8485f2017-02-26 18:17:10 +01001458 if (uep->ue_size < LONG_MAX / (int)sizeof(char_u *))
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001459 array = U_ALLOC_LINE(sizeof(undoline_T) * uep->ue_size);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001460 if (array == NULL)
1461 {
1462 *error = TRUE;
1463 return uep;
1464 }
Bram Moolenaarccae4672019-01-04 15:09:57 +01001465 vim_memset(array, 0, sizeof(undoline_T) * uep->ue_size);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001466 }
1467 uep->ue_array = array;
1468
1469 for (i = 0; i < uep->ue_size; ++i)
1470 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001471 line_len = undo_read_4c(bi);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001472 if (line_len >= 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001473 line = read_string_decrypt(bi, line_len);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001474 else
1475 {
1476 line = NULL;
1477 corruption_error("line length", file_name);
1478 }
1479 if (line == NULL)
1480 {
1481 *error = TRUE;
1482 return uep;
1483 }
Bram Moolenaarccae4672019-01-04 15:09:57 +01001484 array[i].ul_line = line;
1485 array[i].ul_len = line_len + 1;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001486 }
1487 return uep;
1488}
1489
1490/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001491 * Serialize "pos".
Bram Moolenaar9db58062010-05-29 20:33:07 +02001492 */
1493 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001494serialize_pos(bufinfo_T *bi, pos_T pos)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001495{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001496 undo_write_bytes(bi, (long_u)pos.lnum, 4);
1497 undo_write_bytes(bi, (long_u)pos.col, 4);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001498 undo_write_bytes(bi, (long_u)pos.coladd, 4);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001499}
1500
1501/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001502 * Unserialize the pos_T at the current position.
Bram Moolenaar9db58062010-05-29 20:33:07 +02001503 */
1504 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001505unserialize_pos(bufinfo_T *bi, pos_T *pos)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001506{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001507 pos->lnum = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001508 if (pos->lnum < 0)
1509 pos->lnum = 0;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001510 pos->col = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001511 if (pos->col < 0)
1512 pos->col = 0;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001513 pos->coladd = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001514 if (pos->coladd < 0)
1515 pos->coladd = 0;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001516}
1517
1518/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001519 * Serialize "info".
Bram Moolenaar9db58062010-05-29 20:33:07 +02001520 */
1521 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001522serialize_visualinfo(bufinfo_T *bi, visualinfo_T *info)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001523{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001524 serialize_pos(bi, info->vi_start);
1525 serialize_pos(bi, info->vi_end);
1526 undo_write_bytes(bi, (long_u)info->vi_mode, 4);
1527 undo_write_bytes(bi, (long_u)info->vi_curswant, 4);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001528}
1529
1530/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001531 * Unserialize the visualinfo_T at the current position.
Bram Moolenaar9db58062010-05-29 20:33:07 +02001532 */
1533 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001534unserialize_visualinfo(bufinfo_T *bi, visualinfo_T *info)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001535{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001536 unserialize_pos(bi, &info->vi_start);
1537 unserialize_pos(bi, &info->vi_end);
1538 info->vi_mode = undo_read_4c(bi);
1539 info->vi_curswant = undo_read_4c(bi);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001540}
1541
1542/*
1543 * Write the undo tree in an undo file.
1544 * When "name" is not NULL, use it as the name of the undo file.
1545 * Otherwise use buf->b_ffname to generate the undo file name.
1546 * "buf" must never be null, buf->b_ffname is used to obtain the original file
1547 * permissions.
1548 * "forceit" is TRUE for ":wundo!", FALSE otherwise.
1549 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text.
1550 */
1551 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001552u_write_undo(
1553 char_u *name,
1554 int forceit,
1555 buf_T *buf,
1556 char_u *hash)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001557{
1558 u_header_T *uhp;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001559 char_u *file_name;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001560 int mark;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001561#ifdef U_DEBUG
1562 int headers_written = 0;
1563#endif
1564 int fd;
1565 FILE *fp = NULL;
1566 int perm;
1567 int write_ok = FALSE;
1568#ifdef UNIX
1569 int st_old_valid = FALSE;
Bram Moolenaar8767f522016-07-01 17:17:39 +02001570 stat_T st_old;
1571 stat_T st_new;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001572#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001573 bufinfo_T bi;
1574
Bram Moolenaara80faa82020-04-12 19:37:17 +02001575 CLEAR_FIELD(bi);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001576
1577 if (name == NULL)
1578 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001579 file_name = u_get_undo_file_name(buf->b_ffname, FALSE);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001580 if (file_name == NULL)
1581 {
1582 if (p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001583 {
1584 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001585 smsg(
Bram Moolenaar504a8212010-05-30 17:17:42 +02001586 _("Cannot write undo file in any directory in 'undodir'"));
1587 verbose_leave();
1588 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001589 return;
1590 }
1591 }
1592 else
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001593 file_name = name;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001594
1595 /*
1596 * Decide about the permission to use for the undo file. If the buffer
1597 * has a name use the permission of the original file. Otherwise only
1598 * allow the user to access the undo file.
1599 */
1600 perm = 0600;
1601 if (buf->b_ffname != NULL)
1602 {
1603#ifdef UNIX
1604 if (mch_stat((char *)buf->b_ffname, &st_old) >= 0)
1605 {
1606 perm = st_old.st_mode;
1607 st_old_valid = TRUE;
1608 }
1609#else
1610 perm = mch_getperm(buf->b_ffname);
1611 if (perm < 0)
1612 perm = 0600;
1613#endif
1614 }
1615
Bram Moolenaare38eab22019-12-05 21:50:01 +01001616 // strip any s-bit and executable bit
Bram Moolenaar3cbac302015-04-21 16:12:06 +02001617 perm = perm & 0666;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001618
Bram Moolenaare38eab22019-12-05 21:50:01 +01001619 // If the undo file already exists, verify that it actually is an undo
1620 // file, and delete it.
Bram Moolenaar9db58062010-05-29 20:33:07 +02001621 if (mch_getperm(file_name) >= 0)
1622 {
1623 if (name == NULL || !forceit)
1624 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001625 // Check we can read it and it's an undo file.
Bram Moolenaar9db58062010-05-29 20:33:07 +02001626 fd = mch_open((char *)file_name, O_RDONLY|O_EXTRA, 0);
1627 if (fd < 0)
1628 {
1629 if (name != NULL || p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001630 {
1631 if (name == NULL)
1632 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001633 smsg(
Bram Moolenaar504a8212010-05-30 17:17:42 +02001634 _("Will not overwrite with undo file, cannot read: %s"),
Bram Moolenaar9db58062010-05-29 20:33:07 +02001635 file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001636 if (name == NULL)
1637 verbose_leave();
1638 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001639 goto theend;
1640 }
1641 else
1642 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02001643 char_u mbuf[UF_START_MAGIC_LEN];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001644 int len;
1645
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01001646 len = read_eintr(fd, mbuf, UF_START_MAGIC_LEN);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001647 close(fd);
1648 if (len < UF_START_MAGIC_LEN
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02001649 || memcmp(mbuf, UF_START_MAGIC, UF_START_MAGIC_LEN) != 0)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001650 {
1651 if (name != NULL || p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001652 {
1653 if (name == NULL)
1654 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001655 smsg(
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001656 _("Will not overwrite, this is not an undo file: %s"),
Bram Moolenaar9db58062010-05-29 20:33:07 +02001657 file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001658 if (name == NULL)
1659 verbose_leave();
1660 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001661 goto theend;
1662 }
1663 }
1664 }
1665 mch_remove(file_name);
1666 }
1667
Bram Moolenaare38eab22019-12-05 21:50:01 +01001668 // If there is no undo information at all, quit here after deleting any
1669 // existing undo file.
Bram Moolenaarccae4672019-01-04 15:09:57 +01001670 if (buf->b_u_numhead == 0 && buf->b_u_line_ptr.ul_line == NULL)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001671 {
1672 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001673 verb_msg(_("Skipping undo file write, nothing to undo"));
Bram Moolenaar504a8212010-05-30 17:17:42 +02001674 goto theend;
1675 }
1676
Bram Moolenaar9db58062010-05-29 20:33:07 +02001677 fd = mch_open((char *)file_name,
1678 O_CREAT|O_EXTRA|O_WRONLY|O_EXCL|O_NOFOLLOW, perm);
1679 if (fd < 0)
1680 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001681 semsg(_(e_not_open), file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001682 goto theend;
1683 }
1684 (void)mch_setperm(file_name, perm);
1685 if (p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001686 {
1687 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001688 smsg(_("Writing undo file: %s"), file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001689 verbose_leave();
1690 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001691
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001692#ifdef U_DEBUG
Bram Moolenaare38eab22019-12-05 21:50:01 +01001693 // Check there is no problem in undo info before writing.
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001694 u_check(FALSE);
1695#endif
1696
Bram Moolenaar9db58062010-05-29 20:33:07 +02001697#ifdef UNIX
1698 /*
1699 * Try to set the group of the undo file same as the original file. If
1700 * this fails, set the protection bits for the group same as the
1701 * protection bits for others.
1702 */
Bram Moolenaarce69e822010-07-21 20:31:07 +02001703 if (st_old_valid
1704 && mch_stat((char *)file_name, &st_new) >= 0
1705 && st_new.st_gid != st_old.st_gid
Bram Moolenaare38eab22019-12-05 21:50:01 +01001706# ifdef HAVE_FCHOWN // sequent-ptx lacks fchown()
Bram Moolenaarce69e822010-07-21 20:31:07 +02001707 && fchown(fd, (uid_t)-1, st_old.st_gid) != 0
Bram Moolenaar9db58062010-05-29 20:33:07 +02001708# endif
1709 )
1710 mch_setperm(file_name, (perm & 0707) | ((perm & 07) << 3));
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02001711# if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001712 if (buf->b_ffname != NULL)
1713 mch_copy_sec(buf->b_ffname, file_name);
1714# endif
1715#endif
1716
1717 fp = fdopen(fd, "w");
1718 if (fp == NULL)
1719 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001720 semsg(_(e_not_open), file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001721 close(fd);
1722 mch_remove(file_name);
1723 goto theend;
1724 }
1725
Bram Moolenaare38eab22019-12-05 21:50:01 +01001726 // Undo must be synced.
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001727 u_sync(TRUE);
1728
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001729 /*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001730 * Write the header. Initializes encryption, if enabled.
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001731 */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001732 bi.bi_buf = buf;
1733 bi.bi_fp = fp;
1734 if (serialize_header(&bi, hash) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001735 goto write_error;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001736
1737 /*
1738 * Iteratively serialize UHPs and their UEPs from the top down.
1739 */
1740 mark = ++lastmark;
1741 uhp = buf->b_u_oldhead;
1742 while (uhp != NULL)
1743 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001744 // Serialize current UHP if we haven't seen it
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001745 if (uhp->uh_walk != mark)
1746 {
1747 uhp->uh_walk = mark;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001748#ifdef U_DEBUG
1749 ++headers_written;
1750#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001751 if (serialize_uhp(&bi, uhp) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001752 goto write_error;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001753 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001754
Bram Moolenaare38eab22019-12-05 21:50:01 +01001755 // Now walk through the tree - algorithm from undo_time().
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001756 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != mark)
1757 uhp = uhp->uh_prev.ptr;
1758 else if (uhp->uh_alt_next.ptr != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001759 && uhp->uh_alt_next.ptr->uh_walk != mark)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001760 uhp = uhp->uh_alt_next.ptr;
1761 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001762 && uhp->uh_next.ptr->uh_walk != mark)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001763 uhp = uhp->uh_next.ptr;
1764 else if (uhp->uh_alt_prev.ptr != NULL)
1765 uhp = uhp->uh_alt_prev.ptr;
1766 else
1767 uhp = uhp->uh_next.ptr;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001768 }
1769
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001770 if (undo_write_bytes(&bi, (long_u)UF_HEADER_END_MAGIC, 2) == OK)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001771 write_ok = TRUE;
1772#ifdef U_DEBUG
1773 if (headers_written != buf->b_u_numhead)
Bram Moolenaar570064c2013-06-10 20:25:10 +02001774 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001775 semsg("Written %ld headers, ...", headers_written);
1776 semsg("... but numhead is %ld", buf->b_u_numhead);
Bram Moolenaar570064c2013-06-10 20:25:10 +02001777 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001778#endif
1779
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001780#ifdef FEAT_CRYPT
1781 if (bi.bi_state != NULL && undo_flush(&bi) == FAIL)
1782 write_ok = FALSE;
1783#endif
1784
Bram Moolenaar9db58062010-05-29 20:33:07 +02001785write_error:
1786 fclose(fp);
1787 if (!write_ok)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001788 semsg(_("E829: write error in undo file: %s"), file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001789
Bram Moolenaar4f974752019-02-17 17:44:42 +01001790#if defined(MSWIN)
Bram Moolenaare38eab22019-12-05 21:50:01 +01001791 // Copy file attributes; for systems where this can only be done after
1792 // closing the file.
Bram Moolenaar9db58062010-05-29 20:33:07 +02001793 if (buf->b_ffname != NULL)
1794 (void)mch_copy_file_attribute(buf->b_ffname, file_name);
1795#endif
1796#ifdef HAVE_ACL
1797 if (buf->b_ffname != NULL)
1798 {
1799 vim_acl_T acl;
1800
Bram Moolenaare38eab22019-12-05 21:50:01 +01001801 // For systems that support ACL: get the ACL from the original file.
Bram Moolenaar9db58062010-05-29 20:33:07 +02001802 acl = mch_get_acl(buf->b_ffname);
1803 mch_set_acl(file_name, acl);
Bram Moolenaard2aed442012-06-01 13:46:12 +02001804 mch_free_acl(acl);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001805 }
1806#endif
1807
1808theend:
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001809#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001810 if (bi.bi_state != NULL)
1811 crypt_free_state(bi.bi_state);
1812 vim_free(bi.bi_buffer);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001813#endif
Bram Moolenaar9db58062010-05-29 20:33:07 +02001814 if (file_name != name)
1815 vim_free(file_name);
1816}
1817
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001818/*
1819 * Load the undo tree from an undo file.
1820 * If "name" is not NULL use it as the undo file name. This also means being
1821 * a bit more verbose.
1822 * Otherwise use curbuf->b_ffname to generate the undo file name.
1823 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text.
1824 */
1825 void
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02001826u_read_undo(char_u *name, char_u *hash, char_u *orig_name UNUSED)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001827{
1828 char_u *file_name;
1829 FILE *fp;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001830 long version, str_len;
Bram Moolenaarccae4672019-01-04 15:09:57 +01001831 undoline_T line_ptr;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001832 linenr_T line_lnum;
1833 colnr_T line_colnr;
1834 linenr_T line_count;
Bram Moolenaar3eb16372017-02-26 18:11:36 +01001835 long num_head = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001836 long old_header_seq, new_header_seq, cur_header_seq;
1837 long seq_last, seq_cur;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001838 long last_save_nr = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001839 short old_idx = -1, new_idx = -1, cur_idx = -1;
1840 long num_read_uhps = 0;
1841 time_t seq_time;
1842 int i, j;
1843 int c;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001844 u_header_T *uhp;
1845 u_header_T **uhp_table = NULL;
1846 char_u read_hash[UNDO_HASH_SIZE];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001847 char_u magic_buf[UF_START_MAGIC_LEN];
1848#ifdef U_DEBUG
1849 int *uhp_table_used;
1850#endif
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001851#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001852 stat_T st_orig;
1853 stat_T st_undo;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001854#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001855 bufinfo_T bi;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001856
Bram Moolenaara80faa82020-04-12 19:37:17 +02001857 CLEAR_FIELD(bi);
Bram Moolenaarccae4672019-01-04 15:09:57 +01001858 line_ptr.ul_len = 0;
1859 line_ptr.ul_line = NULL;
1860
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001861 if (name == NULL)
1862 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001863 file_name = u_get_undo_file_name(curbuf->b_ffname, TRUE);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001864 if (file_name == NULL)
1865 return;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001866
1867#ifdef UNIX
Bram Moolenaare38eab22019-12-05 21:50:01 +01001868 // For safety we only read an undo file if the owner is equal to the
1869 // owner of the text file or equal to the current user.
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001870 if (mch_stat((char *)orig_name, &st_orig) >= 0
1871 && mch_stat((char *)file_name, &st_undo) >= 0
Bram Moolenaar3b262392013-09-08 15:40:49 +02001872 && st_orig.st_uid != st_undo.st_uid
1873 && st_undo.st_uid != getuid())
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001874 {
1875 if (p_verbose > 0)
1876 {
1877 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001878 smsg(_("Not reading undo file, owner differs: %s"),
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001879 file_name);
1880 verbose_leave();
1881 }
1882 return;
1883 }
1884#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001885 }
1886 else
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001887 file_name = name;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001888
1889 if (p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001890 {
1891 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001892 smsg(_("Reading undo file: %s"), file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001893 verbose_leave();
1894 }
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001895
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001896 fp = mch_fopen((char *)file_name, "r");
1897 if (fp == NULL)
1898 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001899 if (name != NULL || p_verbose > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001900 semsg(_("E822: Cannot open undo file for reading: %s"), file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001901 goto error;
1902 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001903 bi.bi_buf = curbuf;
1904 bi.bi_fp = fp;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001905
Bram Moolenaar9db58062010-05-29 20:33:07 +02001906 /*
1907 * Read the undo file header.
1908 */
1909 if (fread(magic_buf, UF_START_MAGIC_LEN, 1, fp) != 1
1910 || memcmp(magic_buf, UF_START_MAGIC, UF_START_MAGIC_LEN) != 0)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001911 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001912 semsg(_("E823: Not an undo file: %s"), file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001913 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001914 }
1915 version = get2c(fp);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001916 if (version == UF_VERSION_CRYPT)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001917 {
1918#ifdef FEAT_CRYPT
Bram Moolenaar56be9502010-06-06 14:20:26 +02001919 if (*curbuf->b_p_key == NUL)
1920 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001921 semsg(_("E832: Non-encrypted file has encrypted undo file: %s"),
Bram Moolenaar56be9502010-06-06 14:20:26 +02001922 file_name);
1923 goto error;
1924 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001925 bi.bi_state = crypt_create_from_file(fp, curbuf->b_p_key);
1926 if (bi.bi_state == NULL)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001927 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001928 semsg(_("E826: Undo file decryption failed: %s"), file_name);
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001929 goto error;
1930 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001931 if (crypt_whole_undofile(bi.bi_state->method_nr))
1932 {
1933 bi.bi_buffer = alloc(CRYPT_BUF_SIZE);
1934 if (bi.bi_buffer == NULL)
1935 {
1936 crypt_free_state(bi.bi_state);
1937 bi.bi_state = NULL;
1938 goto error;
1939 }
1940 bi.bi_avail = 0;
1941 bi.bi_used = 0;
1942 }
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001943#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001944 semsg(_("E827: Undo file is encrypted: %s"), file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001945 goto error;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001946#endif
1947 }
Bram Moolenaar69154f22010-07-18 21:42:34 +02001948 else if (version != UF_VERSION)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001949 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001950 semsg(_("E824: Incompatible undo file: %s"), file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001951 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001952 }
1953
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001954 if (undo_read(&bi, read_hash, (size_t)UNDO_HASH_SIZE) == FAIL)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001955 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001956 corruption_error("hash", file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001957 goto error;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001958 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001959 line_count = (linenr_T)undo_read_4c(&bi);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001960 if (memcmp(hash, read_hash, UNDO_HASH_SIZE) != 0
1961 || line_count != curbuf->b_ml.ml_line_count)
1962 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001963 if (p_verbose > 0 || name != NULL)
1964 {
Bram Moolenaar504a8212010-05-30 17:17:42 +02001965 if (name == NULL)
1966 verbose_enter();
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001967 give_warning((char_u *)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001968 _("File contents changed, cannot use undo info"), TRUE);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001969 if (name == NULL)
1970 verbose_leave();
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001971 }
1972 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001973 }
1974
Bram Moolenaare38eab22019-12-05 21:50:01 +01001975 // Read undo data for "U" command.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001976 str_len = undo_read_4c(&bi);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001977 if (str_len < 0)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001978 goto error;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001979 if (str_len > 0)
Bram Moolenaarccae4672019-01-04 15:09:57 +01001980 {
1981 line_ptr.ul_line = read_string_decrypt(&bi, str_len);
1982 line_ptr.ul_len = str_len + 1;
1983 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001984 line_lnum = (linenr_T)undo_read_4c(&bi);
1985 line_colnr = (colnr_T)undo_read_4c(&bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001986 if (line_lnum < 0 || line_colnr < 0)
1987 {
1988 corruption_error("line lnum/col", file_name);
1989 goto error;
1990 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001991
Bram Moolenaare38eab22019-12-05 21:50:01 +01001992 // Begin general undo data
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001993 old_header_seq = undo_read_4c(&bi);
1994 new_header_seq = undo_read_4c(&bi);
1995 cur_header_seq = undo_read_4c(&bi);
1996 num_head = undo_read_4c(&bi);
1997 seq_last = undo_read_4c(&bi);
1998 seq_cur = undo_read_4c(&bi);
1999 seq_time = undo_read_time(&bi);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002000
Bram Moolenaare38eab22019-12-05 21:50:01 +01002001 // Optional header fields.
Bram Moolenaar69154f22010-07-18 21:42:34 +02002002 for (;;)
2003 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002004 int len = undo_read_byte(&bi);
Bram Moolenaar69154f22010-07-18 21:42:34 +02002005 int what;
Bram Moolenaara800b422010-06-27 01:15:55 +02002006
Bram Moolenaar69154f22010-07-18 21:42:34 +02002007 if (len == 0 || len == EOF)
2008 break;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002009 what = undo_read_byte(&bi);
Bram Moolenaar69154f22010-07-18 21:42:34 +02002010 switch (what)
2011 {
2012 case UF_LAST_SAVE_NR:
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002013 last_save_nr = undo_read_4c(&bi);
Bram Moolenaara800b422010-06-27 01:15:55 +02002014 break;
Bram Moolenaar69154f22010-07-18 21:42:34 +02002015 default:
Bram Moolenaare38eab22019-12-05 21:50:01 +01002016 // field not supported, skip
Bram Moolenaar69154f22010-07-18 21:42:34 +02002017 while (--len >= 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002018 (void)undo_read_byte(&bi);
Bram Moolenaara800b422010-06-27 01:15:55 +02002019 }
Bram Moolenaar69154f22010-07-18 21:42:34 +02002020 }
Bram Moolenaara800b422010-06-27 01:15:55 +02002021
Bram Moolenaare38eab22019-12-05 21:50:01 +01002022 // uhp_table will store the freshly created undo headers we allocate
2023 // until we insert them into curbuf. The table remains sorted by the
2024 // sequence numbers of the headers.
2025 // When there are no headers uhp_table is NULL.
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002026 if (num_head > 0)
2027 {
Bram Moolenaar3eb16372017-02-26 18:11:36 +01002028 if (num_head < LONG_MAX / (long)sizeof(u_header_T *))
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002029 uhp_table = U_ALLOC_LINE(num_head * sizeof(u_header_T *));
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002030 if (uhp_table == NULL)
2031 goto error;
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002032 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002033
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002034 while ((c = undo_read_2c(&bi)) == UF_HEADER_MAGIC)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002035 {
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02002036 if (num_read_uhps >= num_head)
2037 {
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002038 corruption_error("num_head too small", file_name);
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02002039 goto error;
2040 }
2041
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002042 uhp = unserialize_uhp(&bi, file_name);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002043 if (uhp == NULL)
2044 goto error;
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02002045 uhp_table[num_read_uhps++] = uhp;
2046 }
2047
2048 if (num_read_uhps != num_head)
2049 {
2050 corruption_error("num_head", file_name);
2051 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002052 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02002053 if (c != UF_HEADER_END_MAGIC)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002054 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02002055 corruption_error("end marker", file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002056 goto error;
2057 }
2058
Bram Moolenaar9db58062010-05-29 20:33:07 +02002059#ifdef U_DEBUG
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002060 uhp_table_used = alloc_clear(sizeof(int) * num_head + 1);
Bram Moolenaar9db58062010-05-29 20:33:07 +02002061# define SET_FLAG(j) ++uhp_table_used[j]
2062#else
2063# define SET_FLAG(j)
2064#endif
2065
Bram Moolenaare38eab22019-12-05 21:50:01 +01002066 // We have put all of the headers into a table. Now we iterate through the
2067 // table and swizzle each sequence number we have stored in uh_*_seq into
2068 // a pointer corresponding to the header with that sequence number.
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002069 for (i = 0; i < num_head; i++)
2070 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002071 uhp = uhp_table[i];
2072 if (uhp == NULL)
2073 continue;
2074 for (j = 0; j < num_head; j++)
2075 if (uhp_table[j] != NULL && i != j
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002076 && uhp_table[i]->uh_seq == uhp_table[j]->uh_seq)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002077 {
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02002078 corruption_error("duplicate uh_seq", file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002079 goto error;
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02002080 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002081 for (j = 0; j < num_head; j++)
2082 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002083 && uhp_table[j]->uh_seq == uhp->uh_next.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002084 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002085 uhp->uh_next.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02002086 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002087 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002088 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002089 for (j = 0; j < num_head; j++)
2090 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002091 && uhp_table[j]->uh_seq == uhp->uh_prev.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002092 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002093 uhp->uh_prev.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02002094 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002095 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002096 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002097 for (j = 0; j < num_head; j++)
2098 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002099 && uhp_table[j]->uh_seq == uhp->uh_alt_next.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002100 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002101 uhp->uh_alt_next.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02002102 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002103 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002104 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002105 for (j = 0; j < num_head; j++)
2106 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002107 && uhp_table[j]->uh_seq == uhp->uh_alt_prev.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002108 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002109 uhp->uh_alt_prev.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02002110 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002111 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002112 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002113 if (old_header_seq > 0 && old_idx < 0 && uhp->uh_seq == old_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002114 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002115 old_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002116 SET_FLAG(i);
2117 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002118 if (new_header_seq > 0 && new_idx < 0 && uhp->uh_seq == new_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002119 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002120 new_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002121 SET_FLAG(i);
2122 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002123 if (cur_header_seq > 0 && cur_idx < 0 && uhp->uh_seq == cur_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002124 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002125 cur_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002126 SET_FLAG(i);
2127 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002128 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02002129
Bram Moolenaare38eab22019-12-05 21:50:01 +01002130 // Now that we have read the undo info successfully, free the current undo
2131 // info and use the info from the file.
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002132 u_blockfree(curbuf);
Bram Moolenaar9db58062010-05-29 20:33:07 +02002133 curbuf->b_u_oldhead = old_idx < 0 ? NULL : uhp_table[old_idx];
2134 curbuf->b_u_newhead = new_idx < 0 ? NULL : uhp_table[new_idx];
2135 curbuf->b_u_curhead = cur_idx < 0 ? NULL : uhp_table[cur_idx];
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002136 curbuf->b_u_line_ptr = line_ptr;
2137 curbuf->b_u_line_lnum = line_lnum;
2138 curbuf->b_u_line_colnr = line_colnr;
2139 curbuf->b_u_numhead = num_head;
2140 curbuf->b_u_seq_last = seq_last;
2141 curbuf->b_u_seq_cur = seq_cur;
Bram Moolenaara800b422010-06-27 01:15:55 +02002142 curbuf->b_u_time_cur = seq_time;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002143 curbuf->b_u_save_nr_last = last_save_nr;
Bram Moolenaardba01a02010-11-03 19:32:42 +01002144 curbuf->b_u_save_nr_cur = last_save_nr;
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02002145
2146 curbuf->b_u_synced = TRUE;
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002147 vim_free(uhp_table);
Bram Moolenaar9db58062010-05-29 20:33:07 +02002148
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002149#ifdef U_DEBUG
Bram Moolenaar9db58062010-05-29 20:33:07 +02002150 for (i = 0; i < num_head; ++i)
2151 if (uhp_table_used[i] == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002152 semsg("uhp_table entry %ld not used, leaking memory", i);
Bram Moolenaar9db58062010-05-29 20:33:07 +02002153 vim_free(uhp_table_used);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002154 u_check(TRUE);
2155#endif
Bram Moolenaar9db58062010-05-29 20:33:07 +02002156
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002157 if (name != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002158 smsg(_("Finished reading undo file %s"), file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002159 goto theend;
2160
2161error:
Bram Moolenaarccae4672019-01-04 15:09:57 +01002162 vim_free(line_ptr.ul_line);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002163 if (uhp_table != NULL)
2164 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002165 for (i = 0; i < num_read_uhps; i++)
2166 if (uhp_table[i] != NULL)
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02002167 u_free_uhp(uhp_table[i]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002168 vim_free(uhp_table);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002169 }
2170
2171theend:
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002172#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002173 if (bi.bi_state != NULL)
2174 crypt_free_state(bi.bi_state);
2175 vim_free(bi.bi_buffer);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002176#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002177 if (fp != NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002178 fclose(fp);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002179 if (file_name != name)
2180 vim_free(file_name);
2181 return;
2182}
2183
Bram Moolenaare38eab22019-12-05 21:50:01 +01002184#endif // FEAT_PERSISTENT_UNDO
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002185
2186
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187/*
2188 * If 'cpoptions' contains 'u': Undo the previous undo or redo (vi compatible).
2189 * If 'cpoptions' does not contain 'u': Always undo.
2190 */
2191 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002192u_undo(int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193{
2194 /*
2195 * If we get an undo command while executing a macro, we behave like the
2196 * original vi. If this happens twice in one macro the result will not
2197 * be compatible.
2198 */
2199 if (curbuf->b_u_synced == FALSE)
2200 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002201 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 count = 1;
2203 }
2204
2205 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
2206 undo_undoes = TRUE;
2207 else
2208 undo_undoes = !undo_undoes;
2209 u_doit(count);
2210}
2211
2212/*
2213 * If 'cpoptions' contains 'u': Repeat the previous undo or redo.
2214 * If 'cpoptions' does not contain 'u': Always redo.
2215 */
2216 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002217u_redo(int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218{
2219 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
2220 undo_undoes = FALSE;
2221 u_doit(count);
2222}
2223
2224/*
2225 * Undo or redo, depending on 'undo_undoes', 'count' times.
2226 */
2227 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002228u_doit(int startcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229{
Bram Moolenaarca003e12006-03-17 23:19:38 +00002230 int count = startcount;
2231
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002232 if (!undo_allowed())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234
2235 u_newcount = 0;
2236 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002237 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2238 u_oldcount = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 while (count--)
2240 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002241 // Do the change warning now, so that it triggers FileChangedRO when
2242 // needed. This may cause the file to be reloaded, that must happen
2243 // before we do anything, because it may change curbuf->b_u_curhead
2244 // and more.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002245 change_warning(0);
2246
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 if (undo_undoes)
2248 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002249 if (curbuf->b_u_curhead == NULL) // first undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 curbuf->b_u_curhead = curbuf->b_u_newhead;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002251 else if (get_undolevel() > 0) // multi level undo
2252 // get next undo
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002253 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_next.ptr;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002254 // nothing to undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 if (curbuf->b_u_numhead == 0 || curbuf->b_u_curhead == NULL)
2256 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002257 // stick curbuf->b_u_curhead at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 curbuf->b_u_curhead = curbuf->b_u_oldhead;
2259 beep_flush();
Bram Moolenaarca003e12006-03-17 23:19:38 +00002260 if (count == startcount - 1)
2261 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002262 msg(_("Already at oldest change"));
Bram Moolenaarca003e12006-03-17 23:19:38 +00002263 return;
2264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 break;
2266 }
2267
Bram Moolenaarca003e12006-03-17 23:19:38 +00002268 u_undoredo(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 }
2270 else
2271 {
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002272 if (curbuf->b_u_curhead == NULL || get_undolevel() <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002274 beep_flush(); // nothing to redo
Bram Moolenaarca003e12006-03-17 23:19:38 +00002275 if (count == startcount - 1)
2276 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002277 msg(_("Already at newest change"));
Bram Moolenaarca003e12006-03-17 23:19:38 +00002278 return;
2279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 break;
2281 }
2282
Bram Moolenaarca003e12006-03-17 23:19:38 +00002283 u_undoredo(FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002284
Bram Moolenaare38eab22019-12-05 21:50:01 +01002285 // Advance for next redo. Set "newhead" when at the end of the
2286 // redoable changes.
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002287 if (curbuf->b_u_curhead->uh_prev.ptr == NULL)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002288 curbuf->b_u_newhead = curbuf->b_u_curhead;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002289 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_prev.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 }
2291 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00002292 u_undo_end(undo_undoes, FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002293}
2294
Bram Moolenaar1e607892006-03-13 22:15:53 +00002295/*
2296 * Undo or redo over the timeline.
2297 * When "step" is negative go back in time, otherwise goes forward in time.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002298 * When "sec" is FALSE make "step" steps, when "sec" is TRUE use "step" as
2299 * seconds.
Bram Moolenaar730cde92010-06-27 05:18:54 +02002300 * When "file" is TRUE use "step" as a number of file writes.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002301 * When "absolute" is TRUE use "step" as the sequence number to jump to.
2302 * "sec" must be FALSE then.
Bram Moolenaar1e607892006-03-13 22:15:53 +00002303 */
2304 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002305undo_time(
2306 long step,
2307 int sec,
2308 int file,
2309 int absolute)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002310{
2311 long target;
2312 long closest;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002313 long closest_start;
2314 long closest_seq = 0;
2315 long val;
Bram Moolenaarce46d932018-01-30 22:46:06 +01002316 u_header_T *uhp = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002317 u_header_T *last;
2318 int mark;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02002319 int nomark = 0; // shut up compiler
Bram Moolenaar1e607892006-03-13 22:15:53 +00002320 int round;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002321 int dosec = sec;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002322 int dofile = file;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002323 int above = FALSE;
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002324 int did_undo = TRUE;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002325
Bram Moolenaare38eab22019-12-05 21:50:01 +01002326 // First make sure the current undoable change is synced.
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002327 if (curbuf->b_u_synced == FALSE)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002328 u_sync(TRUE);
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002329
Bram Moolenaar1e607892006-03-13 22:15:53 +00002330 u_newcount = 0;
2331 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002332 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002333 u_oldcount = -1;
2334
Bram Moolenaare38eab22019-12-05 21:50:01 +01002335 // "target" is the node below which we want to be.
2336 // Init "closest" to a value we can't reach.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002337 if (absolute)
2338 {
Bram Moolenaarce46d932018-01-30 22:46:06 +01002339 target = step;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002340 closest = -1;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002341 }
Bram Moolenaarca003e12006-03-17 23:19:38 +00002342 else
Bram Moolenaar1e607892006-03-13 22:15:53 +00002343 {
Bram Moolenaar730cde92010-06-27 05:18:54 +02002344 if (dosec)
Bram Moolenaarcbd4de42017-01-07 16:14:57 +01002345 target = (long)(curbuf->b_u_time_cur) + step;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002346 else if (dofile)
2347 {
2348 if (step < 0)
2349 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002350 // Going back to a previous write. If there were changes after
2351 // the last write, count that as moving one file-write, so
2352 // that ":earlier 1f" undoes all changes since the last save.
Bram Moolenaar730cde92010-06-27 05:18:54 +02002353 uhp = curbuf->b_u_curhead;
2354 if (uhp != NULL)
2355 uhp = uhp->uh_next.ptr;
2356 else
2357 uhp = curbuf->b_u_newhead;
2358 if (uhp != NULL && uhp->uh_save_nr != 0)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002359 // "uh_save_nr" was set in the last block, that means
2360 // there were no changes since the last write
Bram Moolenaar730cde92010-06-27 05:18:54 +02002361 target = curbuf->b_u_save_nr_cur + step;
2362 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01002363 // count the changes since the last write as one step
Bram Moolenaar730cde92010-06-27 05:18:54 +02002364 target = curbuf->b_u_save_nr_cur + step + 1;
2365 if (target <= 0)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002366 // Go to before first write: before the oldest change. Use
2367 // the sequence number for that.
Bram Moolenaar730cde92010-06-27 05:18:54 +02002368 dofile = FALSE;
2369 }
2370 else
2371 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002372 // Moving forward to a newer write.
Bram Moolenaar730cde92010-06-27 05:18:54 +02002373 target = curbuf->b_u_save_nr_cur + step;
2374 if (target > curbuf->b_u_save_nr_last)
2375 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002376 // Go to after last write: after the latest change. Use
2377 // the sequence number for that.
Bram Moolenaar730cde92010-06-27 05:18:54 +02002378 target = curbuf->b_u_seq_last + 1;
2379 dofile = FALSE;
2380 }
2381 }
2382 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002383 else
2384 target = curbuf->b_u_seq_cur + step;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002385 if (step < 0)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002386 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002387 if (target < 0)
2388 target = 0;
2389 closest = -1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002390 }
2391 else
2392 {
Bram Moolenaar730cde92010-06-27 05:18:54 +02002393 if (dosec)
Bram Moolenaarcbd4de42017-01-07 16:14:57 +01002394 closest = (long)(vim_time() + 1);
Bram Moolenaar730cde92010-06-27 05:18:54 +02002395 else if (dofile)
2396 closest = curbuf->b_u_save_nr_last + 2;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002397 else
2398 closest = curbuf->b_u_seq_last + 2;
2399 if (target >= closest)
2400 target = closest - 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002401 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002402 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002403 closest_start = closest;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002404 closest_seq = curbuf->b_u_seq_cur;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002405
Bram Moolenaare38eab22019-12-05 21:50:01 +01002406 // When "target" is 0; Back to origin.
Bram Moolenaarce46d932018-01-30 22:46:06 +01002407 if (target == 0)
Bram Moolenaar059fd012018-01-31 14:25:53 +01002408 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002409 mark = lastmark; // avoid that GCC complains
Bram Moolenaar059fd012018-01-31 14:25:53 +01002410 goto target_zero;
2411 }
Bram Moolenaarce46d932018-01-30 22:46:06 +01002412
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002413 /*
2414 * May do this twice:
Bram Moolenaar1e607892006-03-13 22:15:53 +00002415 * 1. Search for "target", update "closest" to the best match found.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002416 * 2. If "target" not found search for "closest".
2417 *
2418 * When using the closest time we use the sequence number in the second
2419 * round, because there may be several entries with the same time.
2420 */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002421 for (round = 1; round <= 2; ++round)
2422 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002423 // Find the path from the current state to where we want to go. The
2424 // desired state can be anywhere in the undo tree, need to go all over
2425 // it. We put "nomark" in uh_walk where we have been without success,
2426 // "mark" where it could possibly be.
Bram Moolenaar1e607892006-03-13 22:15:53 +00002427 mark = ++lastmark;
2428 nomark = ++lastmark;
2429
Bram Moolenaare38eab22019-12-05 21:50:01 +01002430 if (curbuf->b_u_curhead == NULL) // at leaf of the tree
Bram Moolenaar1e607892006-03-13 22:15:53 +00002431 uhp = curbuf->b_u_newhead;
2432 else
2433 uhp = curbuf->b_u_curhead;
2434
2435 while (uhp != NULL)
2436 {
2437 uhp->uh_walk = mark;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002438 if (dosec)
Bram Moolenaarcbd4de42017-01-07 16:14:57 +01002439 val = (long)(uhp->uh_time);
Bram Moolenaar730cde92010-06-27 05:18:54 +02002440 else if (dofile)
2441 val = uhp->uh_save_nr;
2442 else
2443 val = uhp->uh_seq;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002444
Bram Moolenaar730cde92010-06-27 05:18:54 +02002445 if (round == 1 && !(dofile && val == 0))
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002446 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002447 // Remember the header that is closest to the target.
2448 // It must be at least in the right direction (checked with
2449 // "b_u_seq_cur"). When the timestamp is equal find the
2450 // highest/lowest sequence number.
Bram Moolenaarca003e12006-03-17 23:19:38 +00002451 if ((step < 0 ? uhp->uh_seq <= curbuf->b_u_seq_cur
2452 : uhp->uh_seq > curbuf->b_u_seq_cur)
2453 && ((dosec && val == closest)
2454 ? (step < 0
2455 ? uhp->uh_seq < closest_seq
2456 : uhp->uh_seq > closest_seq)
2457 : closest == closest_start
2458 || (val > target
2459 ? (closest > target
2460 ? val - target <= closest - target
2461 : val - target <= target - closest)
2462 : (closest > target
2463 ? target - val <= closest - target
2464 : target - val <= target - closest))))
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002465 {
2466 closest = val;
2467 closest_seq = uhp->uh_seq;
2468 }
2469 }
2470
Bram Moolenaare38eab22019-12-05 21:50:01 +01002471 // Quit searching when we found a match. But when searching for a
2472 // time we need to continue looking for the best uh_seq.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002473 if (target == val && !dosec)
Bram Moolenaar730cde92010-06-27 05:18:54 +02002474 {
2475 target = uhp->uh_seq;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002476 break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002477 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002478
Bram Moolenaare38eab22019-12-05 21:50:01 +01002479 // go down in the tree if we haven't been there
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002480 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != nomark
2481 && uhp->uh_prev.ptr->uh_walk != mark)
2482 uhp = uhp->uh_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002483
Bram Moolenaare38eab22019-12-05 21:50:01 +01002484 // go to alternate branch if we haven't been there
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002485 else if (uhp->uh_alt_next.ptr != NULL
2486 && uhp->uh_alt_next.ptr->uh_walk != nomark
2487 && uhp->uh_alt_next.ptr->uh_walk != mark)
2488 uhp = uhp->uh_alt_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002489
Bram Moolenaare38eab22019-12-05 21:50:01 +01002490 // go up in the tree if we haven't been there and we are at the
2491 // start of alternate branches
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002492 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
2493 && uhp->uh_next.ptr->uh_walk != nomark
2494 && uhp->uh_next.ptr->uh_walk != mark)
Bram Moolenaardb552d602006-03-23 22:59:57 +00002495 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002496 // If still at the start we don't go through this change.
Bram Moolenaardb552d602006-03-23 22:59:57 +00002497 if (uhp == curbuf->b_u_curhead)
2498 uhp->uh_walk = nomark;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002499 uhp = uhp->uh_next.ptr;
Bram Moolenaardb552d602006-03-23 22:59:57 +00002500 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002501
2502 else
2503 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002504 // need to backtrack; mark this node as useless
Bram Moolenaar1e607892006-03-13 22:15:53 +00002505 uhp->uh_walk = nomark;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002506 if (uhp->uh_alt_prev.ptr != NULL)
2507 uhp = uhp->uh_alt_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002508 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002509 uhp = uhp->uh_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002510 }
2511 }
2512
Bram Moolenaare38eab22019-12-05 21:50:01 +01002513 if (uhp != NULL) // found it
Bram Moolenaar1e607892006-03-13 22:15:53 +00002514 break;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002515
2516 if (absolute)
2517 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002518 semsg(_("E830: Undo number %ld not found"), step);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002519 return;
2520 }
2521
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002522 if (closest == closest_start)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002523 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002524 if (step < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002525 msg(_("Already at oldest change"));
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002526 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002527 msg(_("Already at newest change"));
Bram Moolenaar1e607892006-03-13 22:15:53 +00002528 return;
2529 }
2530
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002531 target = closest_seq;
2532 dosec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002533 dofile = FALSE;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002534 if (step < 0)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002535 above = TRUE; // stop above the header
Bram Moolenaar1e607892006-03-13 22:15:53 +00002536 }
2537
Bram Moolenaar059fd012018-01-31 14:25:53 +01002538target_zero:
Bram Moolenaare38eab22019-12-05 21:50:01 +01002539 // If we found it: Follow the path to go to where we want to be.
Bram Moolenaarce46d932018-01-30 22:46:06 +01002540 if (uhp != NULL || target == 0)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002541 {
2542 /*
2543 * First go up the tree as much as needed.
2544 */
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002545 while (!got_int)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002546 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002547 // Do the change warning now, for the same reason as above.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002548 change_warning(0);
2549
Bram Moolenaar1e607892006-03-13 22:15:53 +00002550 uhp = curbuf->b_u_curhead;
2551 if (uhp == NULL)
2552 uhp = curbuf->b_u_newhead;
2553 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002554 uhp = uhp->uh_next.ptr;
Bram Moolenaarce46d932018-01-30 22:46:06 +01002555 if (uhp == NULL || (target > 0 && uhp->uh_walk != mark)
Bram Moolenaarca003e12006-03-17 23:19:38 +00002556 || (uhp->uh_seq == target && !above))
Bram Moolenaar1e607892006-03-13 22:15:53 +00002557 break;
2558 curbuf->b_u_curhead = uhp;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002559 u_undoredo(TRUE);
Bram Moolenaarce46d932018-01-30 22:46:06 +01002560 if (target > 0)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002561 uhp->uh_walk = nomark; // don't go back down here
Bram Moolenaar1e607892006-03-13 22:15:53 +00002562 }
2563
Bram Moolenaare38eab22019-12-05 21:50:01 +01002564 // When back to origin, redo is not needed.
Bram Moolenaarce46d932018-01-30 22:46:06 +01002565 if (target > 0)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002566 {
Bram Moolenaarce46d932018-01-30 22:46:06 +01002567 /*
2568 * And now go down the tree (redo), branching off where needed.
2569 */
2570 while (!got_int)
2571 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002572 // Do the change warning now, for the same reason as above.
Bram Moolenaarce46d932018-01-30 22:46:06 +01002573 change_warning(0);
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002574
Bram Moolenaarce46d932018-01-30 22:46:06 +01002575 uhp = curbuf->b_u_curhead;
2576 if (uhp == NULL)
2577 break;
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002578
Bram Moolenaare38eab22019-12-05 21:50:01 +01002579 // Go back to the first branch with a mark.
Bram Moolenaarce46d932018-01-30 22:46:06 +01002580 while (uhp->uh_alt_prev.ptr != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002581 && uhp->uh_alt_prev.ptr->uh_walk == mark)
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002582 uhp = uhp->uh_alt_prev.ptr;
Bram Moolenaarce46d932018-01-30 22:46:06 +01002583
Bram Moolenaare38eab22019-12-05 21:50:01 +01002584 // Find the last branch with a mark, that's the one.
Bram Moolenaarce46d932018-01-30 22:46:06 +01002585 last = uhp;
2586 while (last->uh_alt_next.ptr != NULL
2587 && last->uh_alt_next.ptr->uh_walk == mark)
2588 last = last->uh_alt_next.ptr;
2589 if (last != uhp)
2590 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002591 // Make the used branch the first entry in the list of
2592 // alternatives to make "u" and CTRL-R take this branch.
Bram Moolenaarce46d932018-01-30 22:46:06 +01002593 while (uhp->uh_alt_prev.ptr != NULL)
2594 uhp = uhp->uh_alt_prev.ptr;
2595 if (last->uh_alt_next.ptr != NULL)
2596 last->uh_alt_next.ptr->uh_alt_prev.ptr =
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002597 last->uh_alt_prev.ptr;
Bram Moolenaarce46d932018-01-30 22:46:06 +01002598 last->uh_alt_prev.ptr->uh_alt_next.ptr =
2599 last->uh_alt_next.ptr;
2600 last->uh_alt_prev.ptr = NULL;
2601 last->uh_alt_next.ptr = uhp;
2602 uhp->uh_alt_prev.ptr = last;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002603
Bram Moolenaarce46d932018-01-30 22:46:06 +01002604 if (curbuf->b_u_oldhead == uhp)
2605 curbuf->b_u_oldhead = last;
2606 uhp = last;
2607 if (uhp->uh_next.ptr != NULL)
2608 uhp->uh_next.ptr->uh_prev.ptr = uhp;
2609 }
2610 curbuf->b_u_curhead = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002611
Bram Moolenaarce46d932018-01-30 22:46:06 +01002612 if (uhp->uh_walk != mark)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002613 break; // must have reached the target
Bram Moolenaar1e607892006-03-13 22:15:53 +00002614
Bram Moolenaare38eab22019-12-05 21:50:01 +01002615 // Stop when going backwards in time and didn't find the exact
2616 // header we were looking for.
Bram Moolenaarce46d932018-01-30 22:46:06 +01002617 if (uhp->uh_seq == target && above)
2618 {
2619 curbuf->b_u_seq_cur = target - 1;
2620 break;
2621 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002622
Bram Moolenaarce46d932018-01-30 22:46:06 +01002623 u_undoredo(FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002624
Bram Moolenaare38eab22019-12-05 21:50:01 +01002625 // Advance "curhead" to below the header we last used. If it
2626 // becomes NULL then we need to set "newhead" to this leaf.
Bram Moolenaarce46d932018-01-30 22:46:06 +01002627 if (uhp->uh_prev.ptr == NULL)
2628 curbuf->b_u_newhead = uhp;
2629 curbuf->b_u_curhead = uhp->uh_prev.ptr;
2630 did_undo = FALSE;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002631
Bram Moolenaare38eab22019-12-05 21:50:01 +01002632 if (uhp->uh_seq == target) // found it!
Bram Moolenaarce46d932018-01-30 22:46:06 +01002633 break;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002634
Bram Moolenaarce46d932018-01-30 22:46:06 +01002635 uhp = uhp->uh_prev.ptr;
2636 if (uhp == NULL || uhp->uh_walk != mark)
2637 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002638 // Need to redo more but can't find it...
Bram Moolenaarce46d932018-01-30 22:46:06 +01002639 internal_error("undo_time()");
2640 break;
2641 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002642 }
2643 }
2644 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00002645 u_undo_end(did_undo, absolute);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646}
2647
2648/*
2649 * u_undoredo: common code for undo and redo
2650 *
2651 * The lines in the file are replaced by the lines in the entry list at
2652 * curbuf->b_u_curhead. The replaced lines in the file are saved in the entry
2653 * list for the next undo/redo.
Bram Moolenaarca003e12006-03-17 23:19:38 +00002654 *
2655 * When "undo" is TRUE we go up in the tree, when FALSE we go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 */
2657 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002658u_undoredo(int undo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659{
Bram Moolenaarccae4672019-01-04 15:09:57 +01002660 undoline_T *newarray = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 linenr_T oldsize;
2662 linenr_T newsize;
2663 linenr_T top, bot;
2664 linenr_T lnum;
2665 linenr_T newlnum = MAXLNUM;
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002666 pos_T new_curpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 long i;
2668 u_entry_T *uep, *nuep;
2669 u_entry_T *newlist = NULL;
2670 int old_flags;
2671 int new_flags;
2672 pos_T namedm[NMARKS];
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002673 visualinfo_T visualinfo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002674 int empty_buffer; // buffer became empty
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002675 u_header_T *curhead = curbuf->b_u_curhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676
Bram Moolenaare38eab22019-12-05 21:50:01 +01002677 // Don't want autocommands using the undo structures here, they are
2678 // invalid till the end.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002679 block_autocmds();
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002680
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002681#ifdef U_DEBUG
2682 u_check(FALSE);
2683#endif
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002684 old_flags = curhead->uh_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 new_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
2686 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
2687 setpcmark();
2688
2689 /*
2690 * save marks before undo/redo
2691 */
2692 mch_memmove(namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002693 visualinfo = curbuf->b_visual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 curbuf->b_op_start.lnum = curbuf->b_ml.ml_line_count;
2695 curbuf->b_op_start.col = 0;
2696 curbuf->b_op_end.lnum = 0;
2697 curbuf->b_op_end.col = 0;
2698
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002699 for (uep = curhead->uh_entry; uep != NULL; uep = nuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 {
2701 top = uep->ue_top;
2702 bot = uep->ue_bot;
2703 if (bot == 0)
2704 bot = curbuf->b_ml.ml_line_count + 1;
2705 if (top > curbuf->b_ml.ml_line_count || top >= bot
2706 || bot > curbuf->b_ml.ml_line_count + 1)
2707 {
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002708 unblock_autocmds();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002709 iemsg(_("E438: u_undo: line numbers wrong"));
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002710 changed(); // don't want UNCHANGED now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 return;
2712 }
2713
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002714 oldsize = bot - top - 1; // number of lines before undo
2715 newsize = uep->ue_size; // number of lines after undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002717 // Decide about the cursor position, depending on what text changed.
2718 // Don't set it yet, it may be invalid if lines are going to be added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 if (top < newlnum)
2720 {
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002721 // If the saved cursor is somewhere in this undo block, move it to
2722 // the remembered position. Makes "gwap" put the cursor back
2723 // where it was.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002724 lnum = curhead->uh_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 if (lnum >= top && lnum <= top + newsize + 1)
2726 {
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002727 new_curpos = curhead->uh_cursor;
2728 newlnum = new_curpos.lnum - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 }
2730 else
2731 {
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002732 // Use the first line that actually changed. Avoids that
2733 // undoing auto-formatting puts the cursor in the previous
2734 // line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 for (i = 0; i < newsize && i < oldsize; ++i)
Bram Moolenaarccae4672019-01-04 15:09:57 +01002736 {
2737 char_u *p = ml_get(top + 1 + i);
2738
2739 if (curbuf->b_ml.ml_line_len != uep->ue_array[i].ul_len
Bram Moolenaar964b3742019-05-24 18:54:09 +02002740 || memcmp(uep->ue_array[i].ul_line, p,
2741 curbuf->b_ml.ml_line_len) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 break;
Bram Moolenaarccae4672019-01-04 15:09:57 +01002743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 if (i == newsize && newlnum == MAXLNUM && uep->ue_next == NULL)
2745 {
2746 newlnum = top;
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002747 new_curpos.lnum = newlnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 }
2749 else if (i < newsize)
2750 {
2751 newlnum = top + i;
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002752 new_curpos.lnum = newlnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 }
2754 }
2755 }
2756
2757 empty_buffer = FALSE;
2758
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002759 /*
2760 * Delete the lines between top and bot and save them in newarray.
2761 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002762 if (oldsize > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002764 if ((newarray = U_ALLOC_LINE(sizeof(undoline_T) * oldsize)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 {
Bram Moolenaarccae4672019-01-04 15:09:57 +01002766 do_outofmem_msg((long_u)(sizeof(undoline_T) * oldsize));
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002767
2768 // We have messed up the entry list, repair is impossible.
2769 // we have to free the rest of the list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 while (uep != NULL)
2771 {
2772 nuep = uep->ue_next;
2773 u_freeentry(uep, uep->ue_size);
2774 uep = nuep;
2775 }
2776 break;
2777 }
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002778 // delete backwards, it goes faster in most cases
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 for (lnum = bot - 1, i = oldsize; --i >= 0; --lnum)
2780 {
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002781 // what can we do when we run out of memory?
Bram Moolenaarccae4672019-01-04 15:09:57 +01002782 if (u_save_line(&newarray[i], lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 do_outofmem_msg((long_u)0);
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002784 // remember we deleted the last line in the buffer, and a
2785 // dummy empty line will be inserted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 if (curbuf->b_ml.ml_line_count == 1)
2787 empty_buffer = TRUE;
Bram Moolenaara9d4b842020-05-30 14:46:52 +02002788 ml_delete_flags(lnum, ML_DEL_UNDO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 }
2790 }
Bram Moolenaar8d343302005-07-12 22:46:17 +00002791 else
2792 newarray = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002794 // make sure the cursor is on a valid line after the deletions
2795 check_cursor_lnum();
2796
2797 /*
2798 * Insert the lines in u_array between top and bot.
2799 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 if (newsize)
2801 {
2802 for (lnum = top, i = 0; i < newsize; ++i, ++lnum)
2803 {
Bram Moolenaarccae4672019-01-04 15:09:57 +01002804 // If the file is empty, there is an empty line 1 that we
2805 // should get rid of, by replacing it with the new line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 if (empty_buffer && lnum == 0)
Bram Moolenaar964b3742019-05-24 18:54:09 +02002807 ml_replace_len((linenr_T)1, uep->ue_array[i].ul_line,
2808 uep->ue_array[i].ul_len, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 else
Bram Moolenaara9d4b842020-05-30 14:46:52 +02002810 ml_append_flags(lnum, uep->ue_array[i].ul_line,
2811 (colnr_T)uep->ue_array[i].ul_len, ML_APPEND_UNDO);
Bram Moolenaarccae4672019-01-04 15:09:57 +01002812 vim_free(uep->ue_array[i].ul_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 }
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002814 vim_free((char_u *)uep->ue_array);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 }
2816
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002817 // adjust marks
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 if (oldsize != newsize)
2819 {
2820 mark_adjust(top + 1, top + oldsize, (long)MAXLNUM,
2821 (long)newsize - (long)oldsize);
2822 if (curbuf->b_op_start.lnum > top + oldsize)
2823 curbuf->b_op_start.lnum += newsize - oldsize;
2824 if (curbuf->b_op_end.lnum > top + oldsize)
2825 curbuf->b_op_end.lnum += newsize - oldsize;
2826 }
2827
2828 changed_lines(top + 1, 0, bot, newsize - oldsize);
2829
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002830 // set '[ and '] mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 if (top + 1 < curbuf->b_op_start.lnum)
2832 curbuf->b_op_start.lnum = top + 1;
2833 if (newsize == 0 && top + 1 > curbuf->b_op_end.lnum)
2834 curbuf->b_op_end.lnum = top + 1;
2835 else if (top + newsize > curbuf->b_op_end.lnum)
2836 curbuf->b_op_end.lnum = top + newsize;
2837
2838 u_newcount += newsize;
2839 u_oldcount += oldsize;
2840 uep->ue_size = oldsize;
2841 uep->ue_array = newarray;
2842 uep->ue_bot = top + newsize + 1;
2843
2844 /*
2845 * insert this entry in front of the new entry list
2846 */
2847 nuep = uep->ue_next;
2848 uep->ue_next = newlist;
2849 newlist = uep;
2850 }
2851
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002852 // Set the cursor to the desired position. Check that the line is valid.
2853 curwin->w_cursor = new_curpos;
2854 check_cursor_lnum();
2855
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002856 curhead->uh_entry = newlist;
2857 curhead->uh_flags = new_flags;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002858 if ((old_flags & UH_EMPTYBUF) && BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 curbuf->b_ml.ml_flags |= ML_EMPTY;
2860 if (old_flags & UH_CHANGED)
2861 changed();
2862 else
Bram Moolenaar009b2592004-10-24 19:18:58 +00002863#ifdef FEAT_NETBEANS_INTG
Bram Moolenaare38eab22019-12-05 21:50:01 +01002864 // per netbeans undo rules, keep it as modified
Bram Moolenaar009b2592004-10-24 19:18:58 +00002865 if (!isNetbeansModified(curbuf))
2866#endif
Bram Moolenaarc024b462019-06-08 18:07:21 +02002867 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868
2869 /*
2870 * restore marks from before undo/redo
2871 */
2872 for (i = 0; i < NMARKS; ++i)
Bram Moolenaarf65aad52015-02-17 13:43:40 +01002873 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002874 if (curhead->uh_namedm[i].lnum != 0)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002875 curbuf->b_namedm[i] = curhead->uh_namedm[i];
Bram Moolenaarf65aad52015-02-17 13:43:40 +01002876 if (namedm[i].lnum != 0)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002877 curhead->uh_namedm[i] = namedm[i];
Bram Moolenaarf65aad52015-02-17 13:43:40 +01002878 else
2879 curhead->uh_namedm[i].lnum = 0;
2880 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002881 if (curhead->uh_visual.vi_start.lnum != 0)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002882 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002883 curbuf->b_visual = curhead->uh_visual;
2884 curhead->uh_visual = visualinfo;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886
2887 /*
2888 * If the cursor is only off by one line, put it at the same position as
2889 * before starting the change (for the "o" command).
2890 * Otherwise the cursor should go to the first undone line.
2891 */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002892 if (curhead->uh_cursor.lnum + 1 == curwin->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 && curwin->w_cursor.lnum > 1)
2894 --curwin->w_cursor.lnum;
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002895 if (curwin->w_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 {
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002897 if (curhead->uh_cursor.lnum == curwin->w_cursor.lnum)
2898 {
2899 curwin->w_cursor.col = curhead->uh_cursor.col;
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002900 if (virtual_active() && curhead->uh_cursor_vcol >= 0)
2901 coladvance((colnr_T)curhead->uh_cursor_vcol);
2902 else
2903 curwin->w_cursor.coladd = 0;
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002904 }
2905 else
2906 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 else
2909 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002910 // We get here with the current cursor line being past the end (eg
2911 // after adding lines at the end of the file, and then undoing it).
2912 // check_cursor() will move the cursor to the last line. Move it to
2913 // the first column here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 }
2917
Bram Moolenaare38eab22019-12-05 21:50:01 +01002918 // Make sure the cursor is on an existing line and column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 check_cursor();
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002920
Bram Moolenaare38eab22019-12-05 21:50:01 +01002921 // Remember where we are for "g-" and ":earlier 10s".
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002922 curbuf->b_u_seq_cur = curhead->uh_seq;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002923 if (undo)
Bram Moolenaar80eaddd2017-11-11 23:37:08 +01002924 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002925 // We are below the previous undo. However, to make ":earlier 1s"
2926 // work we compute this as being just above the just undone change.
Bram Moolenaar80eaddd2017-11-11 23:37:08 +01002927 if (curhead->uh_next.ptr != NULL)
2928 curbuf->b_u_seq_cur = curhead->uh_next.ptr->uh_seq;
2929 else
2930 curbuf->b_u_seq_cur = 0;
2931 }
Bram Moolenaarca003e12006-03-17 23:19:38 +00002932
Bram Moolenaare38eab22019-12-05 21:50:01 +01002933 // Remember where we are for ":earlier 1f" and ":later 1f".
Bram Moolenaar730cde92010-06-27 05:18:54 +02002934 if (curhead->uh_save_nr != 0)
2935 {
2936 if (undo)
2937 curbuf->b_u_save_nr_cur = curhead->uh_save_nr - 1;
2938 else
2939 curbuf->b_u_save_nr_cur = curhead->uh_save_nr;
2940 }
2941
Bram Moolenaare38eab22019-12-05 21:50:01 +01002942 // The timestamp can be the same for multiple changes, just use the one of
2943 // the undone/redone change.
Bram Moolenaara800b422010-06-27 01:15:55 +02002944 curbuf->b_u_time_cur = curhead->uh_time;
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002945
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002946 unblock_autocmds();
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002947#ifdef U_DEBUG
2948 u_check(FALSE);
2949#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950}
2951
2952/*
2953 * If we deleted or added lines, report the number of less/more lines.
2954 * Otherwise, report the number of changes (this may be incorrect
2955 * in some cases, but it's better than nothing).
2956 */
2957 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002958u_undo_end(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002959 int did_undo, // just did an undo
2960 int absolute) // used ":undo N"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961{
Bram Moolenaar89d40322006-08-29 15:30:07 +00002962 char *msgstr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002963 u_header_T *uhp;
2964 char_u msgbuf[80];
Bram Moolenaar1e607892006-03-13 22:15:53 +00002965
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966#ifdef FEAT_FOLDING
2967 if ((fdo_flags & FDO_UNDO) && KeyTyped)
2968 foldOpenCursor();
2969#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +00002970
Bram Moolenaare38eab22019-12-05 21:50:01 +01002971 if (global_busy // no messages now, wait until global is finished
2972 || !messaging()) // 'lazyredraw' set, don't do messages now
Bram Moolenaar1e607892006-03-13 22:15:53 +00002973 return;
2974
2975 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2976 --u_newcount;
2977
2978 u_oldcount -= u_newcount;
2979 if (u_oldcount == -1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002980 msgstr = N_("more line");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002981 else if (u_oldcount < 0)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002982 msgstr = N_("more lines");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002983 else if (u_oldcount == 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002984 msgstr = N_("line less");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002985 else if (u_oldcount > 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002986 msgstr = N_("fewer lines");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002987 else
2988 {
2989 u_oldcount = u_newcount;
2990 if (u_newcount == 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002991 msgstr = N_("change");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002992 else
Bram Moolenaar89d40322006-08-29 15:30:07 +00002993 msgstr = N_("changes");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002994 }
2995
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002996 if (curbuf->b_u_curhead != NULL)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002997 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002998 // For ":undo N" we prefer a "after #N" message.
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002999 if (absolute && curbuf->b_u_curhead->uh_next.ptr != NULL)
Bram Moolenaardb552d602006-03-23 22:59:57 +00003000 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003001 uhp = curbuf->b_u_curhead->uh_next.ptr;
Bram Moolenaardb552d602006-03-23 22:59:57 +00003002 did_undo = FALSE;
3003 }
3004 else if (did_undo)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00003005 uhp = curbuf->b_u_curhead;
3006 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003007 uhp = curbuf->b_u_curhead->uh_next.ptr;
Bram Moolenaar433f7c82006-03-21 21:29:36 +00003008 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00003009 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003010 uhp = curbuf->b_u_newhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +00003011
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003012 if (uhp == NULL)
3013 *msgbuf = NUL;
3014 else
Bram Moolenaar52410572019-10-27 05:12:45 +01003015 add_time(msgbuf, sizeof(msgbuf), uhp->uh_time);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003016
Bram Moolenaarb2c03502010-07-02 20:20:09 +02003017#ifdef FEAT_CONCEAL
3018 {
3019 win_T *wp;
3020
3021 FOR_ALL_WINDOWS(wp)
3022 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003023 if (wp->w_buffer == curbuf && wp->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02003024 redraw_win_later(wp, NOT_VALID);
3025 }
3026 }
3027#endif
3028
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003029 smsg_attr_keep(0, _("%ld %s; %s #%ld %s"),
Bram Moolenaarca003e12006-03-17 23:19:38 +00003030 u_oldcount < 0 ? -u_oldcount : u_oldcount,
Bram Moolenaar89d40322006-08-29 15:30:07 +00003031 _(msgstr),
Bram Moolenaar433f7c82006-03-21 21:29:36 +00003032 did_undo ? _("before") : _("after"),
3033 uhp == NULL ? 0L : uhp->uh_seq,
3034 msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035}
3036
3037/*
3038 * u_sync: stop adding to the current entry list
3039 */
3040 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003041u_sync(
Bram Moolenaare38eab22019-12-05 21:50:01 +01003042 int force) // Also sync when no_u_sync is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043{
Bram Moolenaare38eab22019-12-05 21:50:01 +01003044 // Skip it when already synced or syncing is disabled.
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003045 if (curbuf->b_u_synced || (!force && no_u_sync > 0))
3046 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003048 if (p_imst == IM_ON_THE_SPOT && im_is_preediting())
Bram Moolenaare38eab22019-12-05 21:50:01 +01003049 return; // XIM is busy, don't break an undo sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003051 if (get_undolevel() < 0)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003052 curbuf->b_u_synced = TRUE; // no entries, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 else
3054 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003055 u_getbot(); // compute ue_bot of previous u_save
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 curbuf->b_u_curhead = NULL;
3057 }
3058}
3059
3060/*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003061 * ":undolist": List the leafs of the undo tree
3062 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003063 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003064ex_undolist(exarg_T *eap UNUSED)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003065{
3066 garray_T ga;
3067 u_header_T *uhp;
3068 int mark;
3069 int nomark;
3070 int changes = 1;
3071 int i;
3072
3073 /*
3074 * 1: walk the tree to find all leafs, put the info in "ga".
3075 * 2: sort the lines
3076 * 3: display the list
3077 */
3078 mark = ++lastmark;
3079 nomark = ++lastmark;
3080 ga_init2(&ga, (int)sizeof(char *), 20);
3081
3082 uhp = curbuf->b_u_oldhead;
3083 while (uhp != NULL)
3084 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003085 if (uhp->uh_prev.ptr == NULL && uhp->uh_walk != nomark
Bram Moolenaarca003e12006-03-17 23:19:38 +00003086 && uhp->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003087 {
3088 if (ga_grow(&ga, 1) == FAIL)
3089 break;
Bram Moolenaarea391762018-04-08 13:07:22 +02003090 vim_snprintf((char *)IObuff, IOSIZE, "%6ld %7d ",
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003091 uhp->uh_seq, changes);
Bram Moolenaar52410572019-10-27 05:12:45 +01003092 add_time(IObuff + STRLEN(IObuff), IOSIZE - STRLEN(IObuff),
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003093 uhp->uh_time);
Bram Moolenaara800b422010-06-27 01:15:55 +02003094 if (uhp->uh_save_nr > 0)
3095 {
Bram Moolenaardba01a02010-11-03 19:32:42 +01003096 while (STRLEN(IObuff) < 33)
Bram Moolenaara800b422010-06-27 01:15:55 +02003097 STRCAT(IObuff, " ");
3098 vim_snprintf_add((char *)IObuff, IOSIZE,
3099 " %3ld", uhp->uh_save_nr);
3100 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003101 ((char_u **)(ga.ga_data))[ga.ga_len++] = vim_strsave(IObuff);
3102 }
3103
3104 uhp->uh_walk = mark;
3105
Bram Moolenaare38eab22019-12-05 21:50:01 +01003106 // go down in the tree if we haven't been there
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003107 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != nomark
3108 && uhp->uh_prev.ptr->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003109 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003110 uhp = uhp->uh_prev.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003111 ++changes;
3112 }
3113
Bram Moolenaare38eab22019-12-05 21:50:01 +01003114 // go to alternate branch if we haven't been there
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003115 else if (uhp->uh_alt_next.ptr != NULL
3116 && uhp->uh_alt_next.ptr->uh_walk != nomark
3117 && uhp->uh_alt_next.ptr->uh_walk != mark)
3118 uhp = uhp->uh_alt_next.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003119
Bram Moolenaare38eab22019-12-05 21:50:01 +01003120 // go up in the tree if we haven't been there and we are at the
3121 // start of alternate branches
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003122 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
3123 && uhp->uh_next.ptr->uh_walk != nomark
3124 && uhp->uh_next.ptr->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003125 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003126 uhp = uhp->uh_next.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003127 --changes;
3128 }
3129
3130 else
3131 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003132 // need to backtrack; mark this node as done
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003133 uhp->uh_walk = nomark;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003134 if (uhp->uh_alt_prev.ptr != NULL)
3135 uhp = uhp->uh_alt_prev.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003136 else
3137 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003138 uhp = uhp->uh_next.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003139 --changes;
3140 }
3141 }
3142 }
3143
3144 if (ga.ga_len == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003145 msg(_("Nothing to undo"));
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003146 else
3147 {
3148 sort_strings((char_u **)ga.ga_data, ga.ga_len);
3149
3150 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01003151 msg_puts_attr(_("number changes when saved"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01003152 HL_ATTR(HLF_T));
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003153 for (i = 0; i < ga.ga_len && !got_int; ++i)
3154 {
3155 msg_putchar('\n');
3156 if (got_int)
3157 break;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003158 msg_puts(((char **)ga.ga_data)[i]);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003159 }
3160 msg_end();
3161
3162 ga_clear_strings(&ga);
3163 }
3164}
3165
3166/*
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003167 * ":undojoin": continue adding to the last entry list
3168 */
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003169 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003170ex_undojoin(exarg_T *eap UNUSED)
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003171{
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003172 if (curbuf->b_u_newhead == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003173 return; // nothing changed before
Bram Moolenaar57657d82006-04-21 22:12:41 +00003174 if (curbuf->b_u_curhead != NULL)
3175 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003176 emsg(_("E790: undojoin is not allowed after undo"));
Bram Moolenaar57657d82006-04-21 22:12:41 +00003177 return;
3178 }
3179 if (!curbuf->b_u_synced)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003180 return; // already unsynced
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003181 if (get_undolevel() < 0)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003182 return; // no entries, nothing to do
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003183 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01003184 // Append next change to the last entry
Bram Moolenaar5e4e1b12017-01-17 22:09:45 +01003185 curbuf->b_u_synced = FALSE;
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003186}
3187
3188/*
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003189 * Called after writing or reloading the file and setting b_changed to FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 * Now an undo means that the buffer is modified.
3191 */
3192 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003193u_unchanged(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194{
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003195 u_unch_branch(buf->b_u_oldhead);
3196 buf->b_did_warn = FALSE;
3197}
3198
Bram Moolenaar730cde92010-06-27 05:18:54 +02003199/*
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003200 * After reloading a buffer which was saved for 'undoreload': Find the first
3201 * line that was changed and set the cursor there.
3202 */
3203 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003204u_find_first_changed(void)
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003205{
3206 u_header_T *uhp = curbuf->b_u_newhead;
3207 u_entry_T *uep;
3208 linenr_T lnum;
3209
3210 if (curbuf->b_u_curhead != NULL || uhp == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003211 return; // undid something in an autocmd?
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003212
Bram Moolenaare38eab22019-12-05 21:50:01 +01003213 // Check that the last undo block was for the whole file.
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003214 uep = uhp->uh_entry;
3215 if (uep->ue_top != 0 || uep->ue_bot != 0)
3216 return;
3217
3218 for (lnum = 1; lnum < curbuf->b_ml.ml_line_count
3219 && lnum <= uep->ue_size; ++lnum)
Bram Moolenaarccae4672019-01-04 15:09:57 +01003220 {
3221 char_u *p = ml_get_buf(curbuf, lnum, FALSE);
3222
3223 if (uep->ue_array[lnum - 1].ul_len != curbuf->b_ml.ml_line_len
3224 || memcmp(p, uep->ue_array[lnum - 1].ul_line, uep->ue_array[lnum - 1].ul_len) != 0)
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003225 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003226 CLEAR_POS(&(uhp->uh_cursor));
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003227 uhp->uh_cursor.lnum = lnum;
3228 return;
3229 }
Bram Moolenaarccae4672019-01-04 15:09:57 +01003230 }
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003231 if (curbuf->b_ml.ml_line_count != uep->ue_size)
3232 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003233 // lines added or deleted at the end, put the cursor there
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003234 CLEAR_POS(&(uhp->uh_cursor));
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003235 uhp->uh_cursor.lnum = lnum;
3236 }
3237}
3238
3239/*
Bram Moolenaar730cde92010-06-27 05:18:54 +02003240 * Increase the write count, store it in the last undo header, what would be
3241 * used for "u".
3242 */
3243 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003244u_update_save_nr(buf_T *buf)
Bram Moolenaar730cde92010-06-27 05:18:54 +02003245{
3246 u_header_T *uhp;
3247
3248 ++buf->b_u_save_nr_last;
3249 buf->b_u_save_nr_cur = buf->b_u_save_nr_last;
3250 uhp = buf->b_u_curhead;
3251 if (uhp != NULL)
3252 uhp = uhp->uh_next.ptr;
3253 else
3254 uhp = buf->b_u_newhead;
3255 if (uhp != NULL)
3256 uhp->uh_save_nr = buf->b_u_save_nr_last;
3257}
3258
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003259 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003260u_unch_branch(u_header_T *uhp)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003261{
Bram Moolenaar1e607892006-03-13 22:15:53 +00003262 u_header_T *uh;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003264 for (uh = uhp; uh != NULL; uh = uh->uh_prev.ptr)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003265 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 uh->uh_flags |= UH_CHANGED;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003267 if (uh->uh_alt_next.ptr != NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003268 u_unch_branch(uh->uh_alt_next.ptr); // recursive
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270}
3271
3272/*
3273 * Get pointer to last added entry.
3274 * If it's not valid, give an error message and return NULL.
3275 */
3276 static u_entry_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003277u_get_headentry(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278{
3279 if (curbuf->b_u_newhead == NULL || curbuf->b_u_newhead->uh_entry == NULL)
3280 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003281 iemsg(_("E439: undo list corrupt"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 return NULL;
3283 }
3284 return curbuf->b_u_newhead->uh_entry;
3285}
3286
3287/*
3288 * u_getbot(): compute the line number of the previous u_save
3289 * It is called only when b_u_synced is FALSE.
3290 */
3291 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003292u_getbot(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293{
3294 u_entry_T *uep;
3295 linenr_T extra;
3296
Bram Moolenaare38eab22019-12-05 21:50:01 +01003297 uep = u_get_headentry(); // check for corrupt undo list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 if (uep == NULL)
3299 return;
3300
3301 uep = curbuf->b_u_newhead->uh_getbot_entry;
3302 if (uep != NULL)
3303 {
3304 /*
3305 * the new ue_bot is computed from the number of lines that has been
3306 * inserted (0 - deleted) since calling u_save. This is equal to the
3307 * old line count subtracted from the current line count.
3308 */
3309 extra = curbuf->b_ml.ml_line_count - uep->ue_lcount;
3310 uep->ue_bot = uep->ue_top + uep->ue_size + 1 + extra;
3311 if (uep->ue_bot < 1 || uep->ue_bot > curbuf->b_ml.ml_line_count)
3312 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003313 iemsg(_("E440: undo line missing"));
Bram Moolenaare38eab22019-12-05 21:50:01 +01003314 uep->ue_bot = uep->ue_top + 1; // assume all lines deleted, will
3315 // get all the old lines back
3316 // without deleting the current
3317 // ones
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 }
3319
3320 curbuf->b_u_newhead->uh_getbot_entry = NULL;
3321 }
3322
3323 curbuf->b_u_synced = TRUE;
3324}
3325
3326/*
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003327 * Free one header "uhp" and its entry list and adjust the pointers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 */
3329 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003330u_freeheader(
3331 buf_T *buf,
3332 u_header_T *uhp,
Bram Moolenaare38eab22019-12-05 21:50:01 +01003333 u_header_T **uhpp) // if not NULL reset when freeing this header
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334{
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003335 u_header_T *uhap;
3336
Bram Moolenaare38eab22019-12-05 21:50:01 +01003337 // When there is an alternate redo list free that branch completely,
3338 // because we can never go there.
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003339 if (uhp->uh_alt_next.ptr != NULL)
3340 u_freebranch(buf, uhp->uh_alt_next.ptr, uhpp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003342 if (uhp->uh_alt_prev.ptr != NULL)
3343 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344
Bram Moolenaare38eab22019-12-05 21:50:01 +01003345 // Update the links in the list to remove the header.
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003346 if (uhp->uh_next.ptr == NULL)
3347 buf->b_u_oldhead = uhp->uh_prev.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003349 uhp->uh_next.ptr->uh_prev.ptr = uhp->uh_prev.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003351 if (uhp->uh_prev.ptr == NULL)
3352 buf->b_u_newhead = uhp->uh_next.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003354 for (uhap = uhp->uh_prev.ptr; uhap != NULL;
3355 uhap = uhap->uh_alt_next.ptr)
3356 uhap->uh_next.ptr = uhp->uh_next.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357
Bram Moolenaar1e607892006-03-13 22:15:53 +00003358 u_freeentries(buf, uhp, uhpp);
3359}
3360
3361/*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003362 * Free an alternate branch and any following alternate branches.
Bram Moolenaar1e607892006-03-13 22:15:53 +00003363 */
3364 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003365u_freebranch(
3366 buf_T *buf,
3367 u_header_T *uhp,
Bram Moolenaare38eab22019-12-05 21:50:01 +01003368 u_header_T **uhpp) // if not NULL reset when freeing this header
Bram Moolenaar1e607892006-03-13 22:15:53 +00003369{
3370 u_header_T *tofree, *next;
3371
Bram Moolenaare38eab22019-12-05 21:50:01 +01003372 // If this is the top branch we may need to use u_freeheader() to update
3373 // all the pointers.
Bram Moolenaar07d06772007-11-10 21:51:15 +00003374 if (uhp == buf->b_u_oldhead)
3375 {
Bram Moolenaaraa887322013-11-07 03:04:11 +01003376 while (buf->b_u_oldhead != NULL)
3377 u_freeheader(buf, buf->b_u_oldhead, uhpp);
Bram Moolenaar07d06772007-11-10 21:51:15 +00003378 return;
3379 }
3380
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003381 if (uhp->uh_alt_prev.ptr != NULL)
3382 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +00003383
3384 next = uhp;
3385 while (next != NULL)
3386 {
3387 tofree = next;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003388 if (tofree->uh_alt_next.ptr != NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003389 u_freebranch(buf, tofree->uh_alt_next.ptr, uhpp); // recursive
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003390 next = tofree->uh_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00003391 u_freeentries(buf, tofree, uhpp);
3392 }
3393}
3394
3395/*
3396 * Free all the undo entries for one header and the header itself.
3397 * This means that "uhp" is invalid when returning.
3398 */
3399 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003400u_freeentries(
3401 buf_T *buf,
3402 u_header_T *uhp,
Bram Moolenaare38eab22019-12-05 21:50:01 +01003403 u_header_T **uhpp) // if not NULL reset when freeing this header
Bram Moolenaar1e607892006-03-13 22:15:53 +00003404{
3405 u_entry_T *uep, *nuep;
3406
Bram Moolenaare38eab22019-12-05 21:50:01 +01003407 // Check for pointers to the header that become invalid now.
Bram Moolenaar1e607892006-03-13 22:15:53 +00003408 if (buf->b_u_curhead == uhp)
3409 buf->b_u_curhead = NULL;
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003410 if (buf->b_u_newhead == uhp)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003411 buf->b_u_newhead = NULL; // freeing the newest entry
Bram Moolenaar1e607892006-03-13 22:15:53 +00003412 if (uhpp != NULL && uhp == *uhpp)
3413 *uhpp = NULL;
3414
3415 for (uep = uhp->uh_entry; uep != NULL; uep = nuep)
3416 {
3417 nuep = uep->ue_next;
3418 u_freeentry(uep, uep->ue_size);
3419 }
3420
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003421#ifdef U_DEBUG
3422 uhp->uh_magic = 0;
3423#endif
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003424 vim_free((char_u *)uhp);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003425 --buf->b_u_numhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426}
3427
3428/*
3429 * free entry 'uep' and 'n' lines in uep->ue_array[]
3430 */
3431 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003432u_freeentry(u_entry_T *uep, long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433{
Bram Moolenaar8d343302005-07-12 22:46:17 +00003434 while (n > 0)
Bram Moolenaarccae4672019-01-04 15:09:57 +01003435 vim_free(uep->ue_array[--n].ul_line);
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003436 vim_free((char_u *)uep->ue_array);
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003437#ifdef U_DEBUG
3438 uep->ue_magic = 0;
3439#endif
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003440 vim_free((char_u *)uep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441}
3442
3443/*
3444 * invalidate the undo buffer; called when storage has already been released
3445 */
3446 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003447u_clearall(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448{
3449 buf->b_u_newhead = buf->b_u_oldhead = buf->b_u_curhead = NULL;
3450 buf->b_u_synced = TRUE;
3451 buf->b_u_numhead = 0;
Bram Moolenaarccae4672019-01-04 15:09:57 +01003452 buf->b_u_line_ptr.ul_line = NULL;
3453 buf->b_u_line_ptr.ul_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 buf->b_u_line_lnum = 0;
3455}
3456
3457/*
Bram Moolenaarccae4672019-01-04 15:09:57 +01003458 * Save the line "lnum" for the "U" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003460 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003461u_saveline(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462{
Bram Moolenaare38eab22019-12-05 21:50:01 +01003463 if (lnum == curbuf->b_u_line_lnum) // line is already saved
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 return;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003465 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) // should never happen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 return;
3467 u_clearline();
3468 curbuf->b_u_line_lnum = lnum;
3469 if (curwin->w_cursor.lnum == lnum)
3470 curbuf->b_u_line_colnr = curwin->w_cursor.col;
3471 else
3472 curbuf->b_u_line_colnr = 0;
Bram Moolenaarccae4672019-01-04 15:09:57 +01003473 if (u_save_line(&curbuf->b_u_line_ptr, lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 do_outofmem_msg((long_u)0);
3475}
3476
3477/*
3478 * clear the line saved for the "U" command
3479 * (this is used externally for crossing a line while in insert mode)
3480 */
3481 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003482u_clearline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483{
Bram Moolenaarccae4672019-01-04 15:09:57 +01003484 if (curbuf->b_u_line_ptr.ul_line != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 {
Bram Moolenaarccae4672019-01-04 15:09:57 +01003486 VIM_CLEAR(curbuf->b_u_line_ptr.ul_line);
3487 curbuf->b_u_line_ptr.ul_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 curbuf->b_u_line_lnum = 0;
3489 }
3490}
3491
3492/*
3493 * Implementation of the "U" command.
3494 * Differentiation from vi: "U" can be undone with the next "U".
3495 * We also allow the cursor to be in another line.
Bram Moolenaard04b7502010-07-08 22:27:55 +02003496 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 */
3498 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003499u_undoline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500{
Bram Moolenaarccae4672019-01-04 15:09:57 +01003501 colnr_T t;
3502 undoline_T oldp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503
3504 if (undo_off)
3505 return;
3506
Bram Moolenaarccae4672019-01-04 15:09:57 +01003507 if (curbuf->b_u_line_ptr.ul_line == NULL
Bram Moolenaare3300c82008-02-13 14:21:38 +00003508 || curbuf->b_u_line_lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 {
3510 beep_flush();
3511 return;
3512 }
Bram Moolenaare3300c82008-02-13 14:21:38 +00003513
Bram Moolenaarccae4672019-01-04 15:09:57 +01003514 // first save the line for the 'u' command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 if (u_savecommon(curbuf->b_u_line_lnum - 1,
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003516 curbuf->b_u_line_lnum + 1, (linenr_T)0, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 return;
Bram Moolenaarccae4672019-01-04 15:09:57 +01003518 if (u_save_line(&oldp, curbuf->b_u_line_lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 {
3520 do_outofmem_msg((long_u)0);
3521 return;
3522 }
Bram Moolenaarccae4672019-01-04 15:09:57 +01003523 ml_replace_len(curbuf->b_u_line_lnum, curbuf->b_u_line_ptr.ul_line, curbuf->b_u_line_ptr.ul_len, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 changed_bytes(curbuf->b_u_line_lnum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 curbuf->b_u_line_ptr = oldp;
3526
3527 t = curbuf->b_u_line_colnr;
3528 if (curwin->w_cursor.lnum == curbuf->b_u_line_lnum)
3529 curbuf->b_u_line_colnr = curwin->w_cursor.col;
3530 curwin->w_cursor.col = t;
3531 curwin->w_cursor.lnum = curbuf->b_u_line_lnum;
Bram Moolenaare3300c82008-02-13 14:21:38 +00003532 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533}
3534
3535/*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003536 * Free all allocated memory blocks for the buffer 'buf'.
3537 */
3538 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003539u_blockfree(buf_T *buf)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003540{
Bram Moolenaar1e607892006-03-13 22:15:53 +00003541 while (buf->b_u_oldhead != NULL)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003542 u_freeheader(buf, buf->b_u_oldhead, NULL);
Bram Moolenaarccae4672019-01-04 15:09:57 +01003543 vim_free(buf->b_u_line_ptr.ul_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544}
3545
3546/*
3547 * Check if the 'modified' flag is set, or 'ff' has changed (only need to
3548 * check the first character, because it can only be "dos", "unix" or "mac").
3549 * "nofile" and "scratch" type buffers are considered to always be unchanged.
Bram Moolenaarf405c8f2017-12-09 19:51:49 +01003550 * Also considers a buffer changed when a terminal window contains a running
3551 * job.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 */
3553 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003554bufIsChanged(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555{
Bram Moolenaareb44a682017-08-03 22:44:55 +02003556#ifdef FEAT_TERMINAL
3557 if (term_job_running(buf->b_term))
3558 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559#endif
Bram Moolenaarf405c8f2017-12-09 19:51:49 +01003560 return bufIsChangedNotTerm(buf);
3561}
3562
3563/*
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01003564 * Return TRUE if any buffer has changes. Also buffers that are not written.
3565 */
3566 int
3567anyBufIsChanged(void)
3568{
3569 buf_T *buf;
3570
3571 FOR_ALL_BUFFERS(buf)
3572 if (bufIsChanged(buf))
3573 return TRUE;
Bram Moolenaard6c3f1f2019-03-26 00:31:21 +01003574 return FALSE;
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01003575}
3576
3577/*
Bram Moolenaarf405c8f2017-12-09 19:51:49 +01003578 * Like bufIsChanged() but ignoring a terminal window.
3579 */
3580 int
3581bufIsChangedNotTerm(buf_T *buf)
3582{
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003583 // In a "prompt" buffer we do respect 'modified', so that we can control
3584 // closing the window by setting or resetting that option.
3585 return (!bt_dontwrite(buf) || bt_prompt(buf))
Bram Moolenaareb44a682017-08-03 22:44:55 +02003586 && (buf->b_changed || file_ff_differs(buf, TRUE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587}
3588
3589 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003590curbufIsChanged(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591{
Bram Moolenaareb44a682017-08-03 22:44:55 +02003592 return bufIsChanged(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593}
Bram Moolenaara800b422010-06-27 01:15:55 +02003594
3595#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003596
Bram Moolenaara800b422010-06-27 01:15:55 +02003597/*
3598 * For undotree(): Append the list of undo blocks at "first_uhp" to "list".
3599 * Recursive.
3600 */
Bram Moolenaar840d16f2019-09-10 21:27:18 +02003601 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003602u_eval_tree(u_header_T *first_uhp, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02003603{
3604 u_header_T *uhp = first_uhp;
3605 dict_T *dict;
3606
3607 while (uhp != NULL)
3608 {
3609 dict = dict_alloc();
3610 if (dict == NULL)
3611 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003612 dict_add_number(dict, "seq", uhp->uh_seq);
3613 dict_add_number(dict, "time", (long)uhp->uh_time);
Bram Moolenaara800b422010-06-27 01:15:55 +02003614 if (uhp == curbuf->b_u_newhead)
Bram Moolenaare0be1672018-07-08 16:50:37 +02003615 dict_add_number(dict, "newhead", 1);
Bram Moolenaara800b422010-06-27 01:15:55 +02003616 if (uhp == curbuf->b_u_curhead)
Bram Moolenaare0be1672018-07-08 16:50:37 +02003617 dict_add_number(dict, "curhead", 1);
Bram Moolenaara800b422010-06-27 01:15:55 +02003618 if (uhp->uh_save_nr > 0)
Bram Moolenaare0be1672018-07-08 16:50:37 +02003619 dict_add_number(dict, "save", uhp->uh_save_nr);
Bram Moolenaara800b422010-06-27 01:15:55 +02003620
3621 if (uhp->uh_alt_next.ptr != NULL)
3622 {
3623 list_T *alt_list = list_alloc();
3624
3625 if (alt_list != NULL)
3626 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003627 // Recursive call to add alternate undo tree.
Bram Moolenaara800b422010-06-27 01:15:55 +02003628 u_eval_tree(uhp->uh_alt_next.ptr, alt_list);
3629 dict_add_list(dict, "alt", alt_list);
3630 }
3631 }
3632
3633 list_append_dict(list, dict);
3634 uhp = uhp->uh_prev.ptr;
3635 }
3636}
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003637
3638/*
3639 * "undofile(name)" function
3640 */
3641 void
3642f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
3643{
3644 rettv->v_type = VAR_STRING;
3645#ifdef FEAT_PERSISTENT_UNDO
3646 {
3647 char_u *fname = tv_get_string(&argvars[0]);
3648
3649 if (*fname == NUL)
3650 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003651 // If there is no file name there will be no undo file.
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003652 rettv->vval.v_string = NULL;
3653 }
3654 else
3655 {
3656 char_u *ffname = FullName_save(fname, TRUE);
3657
3658 if (ffname != NULL)
3659 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
3660 vim_free(ffname);
3661 }
3662 }
3663#else
3664 rettv->vval.v_string = NULL;
3665#endif
3666}
3667
3668/*
3669 * "undotree()" function
3670 */
3671 void
3672f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
3673{
3674 if (rettv_dict_alloc(rettv) == OK)
3675 {
3676 dict_T *dict = rettv->vval.v_dict;
3677 list_T *list;
3678
3679 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
3680 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
3681 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
3682 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
3683 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
3684 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
3685
3686 list = list_alloc();
3687 if (list != NULL)
3688 {
3689 u_eval_tree(curbuf->b_u_oldhead, list);
3690 dict_add_list(dict, "entries", list);
3691 }
3692 }
3693}
3694
Bram Moolenaara800b422010-06-27 01:15:55 +02003695#endif