blob: 54a6e1c5bdbd585db9cdee66baf3aa5192b079d8 [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 Moolenaarb71eaae2006-01-20 23:10:18 +0000334 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000335 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100336 emsg(_(e_secure));
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000337 return FALSE;
338 }
339
340 return TRUE;
341}
342
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200343/*
Bram 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
378/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200379 * Common code for various ways to save text before a change.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200380 * "top" is the line above the first changed line.
381 * "bot" is the line below the last changed line.
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200382 * "newbot" is the new bottom line. Use zero when not known.
383 * "reload" is TRUE when saving for a buffer reload.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200384 * Careful: may trigger autocommands that reload the buffer.
385 * Returns FAIL when lines could not be saved, OK otherwise.
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200386 */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200387 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100388u_savecommon(
389 linenr_T top,
390 linenr_T bot,
391 linenr_T newbot,
392 int reload)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393{
Bram Moolenaar1e607892006-03-13 22:15:53 +0000394 linenr_T lnum;
395 long i;
396 u_header_T *uhp;
397 u_header_T *old_curhead;
398 u_entry_T *uep;
399 u_entry_T *prev_uep;
400 long size;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200402 if (!reload)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100404 // When making changes is not allowed return FAIL. It's a crude way
405 // to make all change commands fail.
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200406 if (!undo_allowed())
Bram Moolenaar009b2592004-10-24 19:18:58 +0000407 return FAIL;
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200408
409#ifdef FEAT_NETBEANS_INTG
410 /*
411 * Netbeans defines areas that cannot be modified. Bail out here when
412 * trying to change text in a guarded area.
413 */
414 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +0000415 {
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200416 if (netbeans_is_guarded(top, bot))
417 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100418 emsg(_(e_guarded));
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200419 return FAIL;
420 }
421 if (curbuf->b_p_ro)
422 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100423 emsg(_(e_nbreadonly));
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200424 return FAIL;
425 }
Bram Moolenaar009b2592004-10-24 19:18:58 +0000426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200428#ifdef FEAT_TERMINAL
Bram Moolenaare38eab22019-12-05 21:50:01 +0100429 // A change in a terminal buffer removes the highlighting.
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200430 term_change_in_curbuf();
431#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200433 /*
434 * Saving text for undo means we are going to make a change. Give a
435 * warning for a read-only file before making the change, so that the
436 * FileChangedRO event can replace the buffer with a read-write version
437 * (e.g., obtained from a source control system).
438 */
439 change_warning(0);
440 if (bot > curbuf->b_ml.ml_line_count + 1)
441 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100442 // This happens when the FileChangedRO autocommand changes the
443 // file in a way it becomes shorter.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100444 emsg(_("E881: Line count changed unexpectedly"));
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200445 return FAIL;
446 }
Bram Moolenaard04b7502010-07-08 22:27:55 +0200447 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200448
449#ifdef U_DEBUG
450 u_check(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451#endif
452
453 size = bot - top - 1;
454
455 /*
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200456 * If curbuf->b_u_synced == TRUE make a new header.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 */
458 if (curbuf->b_u_synced)
459 {
460#ifdef FEAT_JUMPLIST
Bram Moolenaare38eab22019-12-05 21:50:01 +0100461 // Need to create new entry in b_changelist.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 curbuf->b_new_change = TRUE;
463#endif
464
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100465 if (get_undolevel() >= 0)
Bram Moolenaar1e607892006-03-13 22:15:53 +0000466 {
467 /*
468 * Make a new header entry. Do this first so that we don't mess
469 * up the undo info when out of memory.
470 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200471 uhp = U_ALLOC_LINE(sizeof(u_header_T));
Bram Moolenaar1e607892006-03-13 22:15:53 +0000472 if (uhp == NULL)
473 goto nomem;
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000474#ifdef U_DEBUG
475 uhp->uh_magic = UH_MAGIC;
476#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +0000477 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000478 else
479 uhp = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000480
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 /*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000482 * If we undid more than we redid, move the entry lists before and
483 * including curbuf->b_u_curhead to an alternate branch.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 */
Bram Moolenaar1e607892006-03-13 22:15:53 +0000485 old_curhead = curbuf->b_u_curhead;
486 if (old_curhead != NULL)
487 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200488 curbuf->b_u_newhead = old_curhead->uh_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000489 curbuf->b_u_curhead = NULL;
490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491
492 /*
493 * free headers to keep the size right
494 */
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100495 while (curbuf->b_u_numhead > get_undolevel()
496 && curbuf->b_u_oldhead != NULL)
Bram Moolenaar1e607892006-03-13 22:15:53 +0000497 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000498 u_header_T *uhfree = curbuf->b_u_oldhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000499
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000500 if (uhfree == old_curhead)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100501 // Can't reconnect the branch, delete all of it.
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000502 u_freebranch(curbuf, uhfree, &old_curhead);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200503 else if (uhfree->uh_alt_next.ptr == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100504 // There is no branch, only free one header.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000505 u_freeheader(curbuf, uhfree, &old_curhead);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000506 else
507 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100508 // Free the oldest alternate branch as a whole.
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200509 while (uhfree->uh_alt_next.ptr != NULL)
510 uhfree = uhfree->uh_alt_next.ptr;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000511 u_freebranch(curbuf, uhfree, &old_curhead);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000512 }
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000513#ifdef U_DEBUG
514 u_check(TRUE);
515#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +0000516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517
Bram Moolenaare38eab22019-12-05 21:50:01 +0100518 if (uhp == NULL) // no undo at all
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 {
Bram Moolenaar1e607892006-03-13 22:15:53 +0000520 if (old_curhead != NULL)
521 u_freebranch(curbuf, old_curhead, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 curbuf->b_u_synced = FALSE;
523 return OK;
524 }
525
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200526 uhp->uh_prev.ptr = NULL;
527 uhp->uh_next.ptr = curbuf->b_u_newhead;
528 uhp->uh_alt_next.ptr = old_curhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000529 if (old_curhead != NULL)
530 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200531 uhp->uh_alt_prev.ptr = old_curhead->uh_alt_prev.ptr;
532 if (uhp->uh_alt_prev.ptr != NULL)
533 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = uhp;
534 old_curhead->uh_alt_prev.ptr = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000535 if (curbuf->b_u_oldhead == old_curhead)
536 curbuf->b_u_oldhead = uhp;
537 }
Bram Moolenaar89ed3df2007-01-09 19:23:12 +0000538 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200539 uhp->uh_alt_prev.ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 if (curbuf->b_u_newhead != NULL)
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200541 curbuf->b_u_newhead->uh_prev.ptr = uhp;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000542
Bram Moolenaarca003e12006-03-17 23:19:38 +0000543 uhp->uh_seq = ++curbuf->b_u_seq_last;
544 curbuf->b_u_seq_cur = uhp->uh_seq;
Bram Moolenaar170b10b2016-07-29 16:15:27 +0200545 uhp->uh_time = vim_time();
Bram Moolenaara800b422010-06-27 01:15:55 +0200546 uhp->uh_save_nr = 0;
547 curbuf->b_u_time_cur = uhp->uh_time + 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000548
Bram Moolenaar1e607892006-03-13 22:15:53 +0000549 uhp->uh_walk = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 uhp->uh_entry = NULL;
551 uhp->uh_getbot_entry = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100552 uhp->uh_cursor = curwin->w_cursor; // save cursor pos. for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 if (virtual_active() && curwin->w_cursor.coladd > 0)
554 uhp->uh_cursor_vcol = getviscol();
555 else
556 uhp->uh_cursor_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557
Bram Moolenaare38eab22019-12-05 21:50:01 +0100558 // save changed and buffer empty flag for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 uhp->uh_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
560 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
561
Bram Moolenaare38eab22019-12-05 21:50:01 +0100562 // save named marks and Visual marks for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 mch_memmove(uhp->uh_namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000564 uhp->uh_visual = curbuf->b_visual;
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000565
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 curbuf->b_u_newhead = uhp;
567 if (curbuf->b_u_oldhead == NULL)
568 curbuf->b_u_oldhead = uhp;
569 ++curbuf->b_u_numhead;
570 }
571 else
572 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100573 if (get_undolevel() < 0) // no undo at all
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 return OK;
575
576 /*
577 * When saving a single line, and it has been saved just before, it
578 * doesn't make sense saving it again. Saves a lot of memory when
579 * making lots of changes inside the same line.
580 * This is only possible if the previous change didn't increase or
581 * decrease the number of lines.
582 * Check the ten last changes. More doesn't make sense and takes too
583 * long.
584 */
585 if (size == 1)
586 {
587 uep = u_get_headentry();
588 prev_uep = NULL;
589 for (i = 0; i < 10; ++i)
590 {
591 if (uep == NULL)
592 break;
593
Bram Moolenaare38eab22019-12-05 21:50:01 +0100594 // If lines have been inserted/deleted we give up.
595 // Also when the line was included in a multi-line save.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 if ((curbuf->b_u_newhead->uh_getbot_entry != uep
597 ? (uep->ue_top + uep->ue_size + 1
598 != (uep->ue_bot == 0
599 ? curbuf->b_ml.ml_line_count + 1
600 : uep->ue_bot))
601 : uep->ue_lcount != curbuf->b_ml.ml_line_count)
602 || (uep->ue_size > 1
603 && top >= uep->ue_top
604 && top + 2 <= uep->ue_top + uep->ue_size + 1))
605 break;
606
Bram Moolenaare38eab22019-12-05 21:50:01 +0100607 // If it's the same line we can skip saving it again.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 if (uep->ue_size == 1 && uep->ue_top == top)
609 {
610 if (i > 0)
611 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100612 // It's not the last entry: get ue_bot for the last
613 // entry now. Following deleted/inserted lines go to
614 // the re-used entry.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 u_getbot();
616 curbuf->b_u_synced = FALSE;
617
Bram Moolenaare38eab22019-12-05 21:50:01 +0100618 // Move the found entry to become the last entry. The
619 // order of undo/redo doesn't matter for the entries
620 // we move it over, since they don't change the line
621 // count and don't include this line. It does matter
622 // for the found entry if the line count is changed by
623 // the executed command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 prev_uep->ue_next = uep->ue_next;
625 uep->ue_next = curbuf->b_u_newhead->uh_entry;
626 curbuf->b_u_newhead->uh_entry = uep;
627 }
628
Bram Moolenaare38eab22019-12-05 21:50:01 +0100629 // The executed command may change the line count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 if (newbot != 0)
631 uep->ue_bot = newbot;
632 else if (bot > curbuf->b_ml.ml_line_count)
633 uep->ue_bot = 0;
634 else
635 {
636 uep->ue_lcount = curbuf->b_ml.ml_line_count;
637 curbuf->b_u_newhead->uh_getbot_entry = uep;
638 }
639 return OK;
640 }
641 prev_uep = uep;
642 uep = uep->ue_next;
643 }
644 }
645
Bram Moolenaare38eab22019-12-05 21:50:01 +0100646 // find line number for ue_bot for previous u_save()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 u_getbot();
648 }
649
Bram Moolenaar4f974752019-02-17 17:44:42 +0100650#if !defined(UNIX) && !defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100652 * With Amiga we can't handle big undo's, because
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 * then u_alloc_line would have to allocate a block larger than 32K
654 */
655 if (size >= 8000)
656 goto nomem;
657#endif
658
659 /*
660 * add lines in front of entry list
661 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200662 uep = U_ALLOC_LINE(sizeof(u_entry_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 if (uep == NULL)
664 goto nomem;
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200665 vim_memset(uep, 0, sizeof(u_entry_T));
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000666#ifdef U_DEBUG
667 uep->ue_magic = UE_MAGIC;
668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669
670 uep->ue_size = size;
671 uep->ue_top = top;
672 if (newbot != 0)
673 uep->ue_bot = newbot;
674 /*
675 * Use 0 for ue_bot if bot is below last line.
676 * Otherwise we have to compute ue_bot later.
677 */
678 else if (bot > curbuf->b_ml.ml_line_count)
679 uep->ue_bot = 0;
680 else
681 {
682 uep->ue_lcount = curbuf->b_ml.ml_line_count;
683 curbuf->b_u_newhead->uh_getbot_entry = uep;
684 }
685
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000686 if (size > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200688 if ((uep->ue_array = U_ALLOC_LINE(sizeof(undoline_T) * size)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 {
690 u_freeentry(uep, 0L);
691 goto nomem;
692 }
693 for (i = 0, lnum = top + 1; i < size; ++i)
694 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000695 fast_breakcheck();
696 if (got_int)
697 {
698 u_freeentry(uep, i);
699 return FAIL;
700 }
Bram Moolenaarccae4672019-01-04 15:09:57 +0100701 if (u_save_line(&uep->ue_array[i], lnum++) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 {
703 u_freeentry(uep, i);
704 goto nomem;
705 }
706 }
707 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000708 else
709 uep->ue_array = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 uep->ue_next = curbuf->b_u_newhead->uh_entry;
711 curbuf->b_u_newhead->uh_entry = uep;
712 curbuf->b_u_synced = FALSE;
713 undo_undoes = FALSE;
714
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000715#ifdef U_DEBUG
716 u_check(FALSE);
717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 return OK;
719
720nomem:
Bram Moolenaare38eab22019-12-05 21:50:01 +0100721 msg_silent = 0; // must display the prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 if (ask_yesno((char_u *)_("No undo possible; continue anyway"), TRUE)
723 == 'y')
724 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100725 undo_off = TRUE; // will be reset when character typed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 return OK;
727 }
728 do_outofmem_msg((long_u)0);
729 return FAIL;
730}
731
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200732#if defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200733
Bram Moolenaare38eab22019-12-05 21:50:01 +0100734# define UF_START_MAGIC "Vim\237UnDo\345" // magic at start of undofile
Bram Moolenaar9db58062010-05-29 20:33:07 +0200735# define UF_START_MAGIC_LEN 9
Bram Moolenaare38eab22019-12-05 21:50:01 +0100736# define UF_HEADER_MAGIC 0x5fd0 // magic at start of header
737# define UF_HEADER_END_MAGIC 0xe7aa // magic after last header
738# define UF_ENTRY_MAGIC 0xf518 // magic at start of entry
739# define UF_ENTRY_END_MAGIC 0x3581 // magic after last entry
740# define UF_VERSION 2 // 2-byte undofile version number
741# define UF_VERSION_CRYPT 0x8002 // idem, encrypted
Bram Moolenaara800b422010-06-27 01:15:55 +0200742
Bram Moolenaare38eab22019-12-05 21:50:01 +0100743// extra fields for header
Bram Moolenaara800b422010-06-27 01:15:55 +0200744# define UF_LAST_SAVE_NR 1
745
Bram Moolenaare38eab22019-12-05 21:50:01 +0100746// extra fields for uhp
Bram Moolenaara800b422010-06-27 01:15:55 +0200747# define UHP_SAVE_NR 1
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200748
Bram Moolenaarcdf04202010-05-29 15:11:47 +0200749static char_u e_not_open[] = N_("E828: Cannot open undo file for writing: %s");
Bram Moolenaarcdf04202010-05-29 15:11:47 +0200750
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200751/*
752 * Compute the hash for the current buffer text into hash[UNDO_HASH_SIZE].
753 */
754 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100755u_compute_hash(char_u *hash)
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200756{
757 context_sha256_T ctx;
758 linenr_T lnum;
759 char_u *p;
760
761 sha256_start(&ctx);
Bram Moolenaarae7ba982011-12-08 15:14:09 +0100762 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200763 {
764 p = ml_get(lnum);
Bram Moolenaar442b4222010-05-24 21:34:22 +0200765 sha256_update(&ctx, p, (UINT32_T)(STRLEN(p) + 1));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200766 }
767 sha256_finish(&ctx, hash);
768}
769
770/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200771 * Return an allocated string of the full path of the target undofile.
772 * When "reading" is TRUE find the file to read, go over all directories in
773 * 'undodir'.
774 * When "reading" is FALSE use the first name where the directory exists.
Bram Moolenaar9db58062010-05-29 20:33:07 +0200775 * Returns NULL when there is no place to write or no file to read.
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200776 */
Bram Moolenaar840d16f2019-09-10 21:27:18 +0200777 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100778u_get_undo_file_name(char_u *buf_ffname, int reading)
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200779{
780 char_u *dirp;
781 char_u dir_name[IOSIZE + 1];
782 char_u *munged_name = NULL;
783 char_u *undo_file_name = NULL;
784 int dir_len;
785 char_u *p;
Bram Moolenaar8767f522016-07-01 17:17:39 +0200786 stat_T st;
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200787 char_u *ffname = buf_ffname;
788#ifdef HAVE_READLINK
789 char_u fname_buf[MAXPATHL];
790#endif
791
792 if (ffname == NULL)
793 return NULL;
794
795#ifdef HAVE_READLINK
Bram Moolenaare38eab22019-12-05 21:50:01 +0100796 // Expand symlink in the file name, so that we put the undo file with the
797 // actual file instead of with the symlink.
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200798 if (resolve_symlink(ffname, fname_buf) == OK)
799 ffname = fname_buf;
800#endif
801
Bram Moolenaare38eab22019-12-05 21:50:01 +0100802 // Loop over 'undodir'. When reading find the first file that exists.
803 // When not reading use the first directory that exists or ".".
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200804 dirp = p_udir;
805 while (*dirp != NUL)
806 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200807 dir_len = copy_option_part(&dirp, dir_name, IOSIZE, ",");
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200808 if (dir_len == 1 && dir_name[0] == '.')
809 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100810 // Use same directory as the ffname,
811 // "dir/name" -> "dir/.name.un~"
Bram Moolenaar442b4222010-05-24 21:34:22 +0200812 undo_file_name = vim_strnsave(ffname, (int)(STRLEN(ffname) + 5));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200813 if (undo_file_name == NULL)
814 break;
815 p = gettail(undo_file_name);
Bram Moolenaar206f0112014-03-12 16:51:55 +0100816#ifdef VMS
Bram Moolenaare38eab22019-12-05 21:50:01 +0100817 // VMS can not handle more than one dot in the filenames
818 // use "dir/name" -> "dir/_un_name" - add _un_
819 // at the beginning to keep the extension
Bram Moolenaar206f0112014-03-12 16:51:55 +0100820 mch_memmove(p + 4, p, STRLEN(p) + 1);
821 mch_memmove(p, "_un_", 4);
822
823#else
Bram Moolenaare38eab22019-12-05 21:50:01 +0100824 // Use same directory as the ffname,
825 // "dir/name" -> "dir/.name.un~"
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200826 mch_memmove(p + 1, p, STRLEN(p) + 1);
827 *p = '.';
828 STRCAT(p, ".un~");
Bram Moolenaar206f0112014-03-12 16:51:55 +0100829#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200830 }
831 else
832 {
833 dir_name[dir_len] = NUL;
834 if (mch_isdir(dir_name))
835 {
836 if (munged_name == NULL)
837 {
838 munged_name = vim_strsave(ffname);
839 if (munged_name == NULL)
840 return NULL;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100841 for (p = munged_name; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200842 if (vim_ispathsep(*p))
843 *p = '%';
844 }
845 undo_file_name = concat_fnames(dir_name, munged_name, TRUE);
846 }
847 }
848
Bram Moolenaare38eab22019-12-05 21:50:01 +0100849 // When reading check if the file exists.
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200850 if (undo_file_name != NULL && (!reading
851 || mch_stat((char *)undo_file_name, &st) >= 0))
852 break;
Bram Moolenaard23a8232018-02-10 18:45:26 +0100853 VIM_CLEAR(undo_file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200854 }
855
856 vim_free(munged_name);
857 return undo_file_name;
858}
859
Bram Moolenaar9db58062010-05-29 20:33:07 +0200860 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100861corruption_error(char *mesg, char_u *file_name)
Bram Moolenaar9db58062010-05-29 20:33:07 +0200862{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100863 semsg(_("E825: Corrupted undo file (%s): %s"), mesg, file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +0200864}
865
866 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100867u_free_uhp(u_header_T *uhp)
Bram Moolenaar9db58062010-05-29 20:33:07 +0200868{
869 u_entry_T *nuep;
870 u_entry_T *uep;
871
872 uep = uhp->uh_entry;
873 while (uep != NULL)
874 {
875 nuep = uep->ue_next;
876 u_freeentry(uep, uep->ue_size);
877 uep = nuep;
878 }
879 vim_free(uhp);
880}
881
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200882/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200883 * Write a sequence of bytes to the undo file.
884 * Buffers and encrypts as needed.
885 * Returns OK or FAIL.
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200886 */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200887 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100888undo_write(bufinfo_T *bi, char_u *ptr, size_t len)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200889{
890#ifdef FEAT_CRYPT
891 if (bi->bi_buffer != NULL)
892 {
893 size_t len_todo = len;
894 char_u *p = ptr;
895
896 while (bi->bi_used + len_todo >= CRYPT_BUF_SIZE)
897 {
898 size_t n = CRYPT_BUF_SIZE - bi->bi_used;
899
900 mch_memmove(bi->bi_buffer + bi->bi_used, p, n);
901 len_todo -= n;
902 p += n;
903 bi->bi_used = CRYPT_BUF_SIZE;
904 if (undo_flush(bi) == FAIL)
905 return FAIL;
906 }
907 if (len_todo > 0)
908 {
909 mch_memmove(bi->bi_buffer + bi->bi_used, p, len_todo);
910 bi->bi_used += len_todo;
911 }
912 return OK;
913 }
914#endif
915 if (fwrite(ptr, len, (size_t)1, bi->bi_fp) != 1)
916 return FAIL;
917 return OK;
918}
919
920#ifdef FEAT_CRYPT
921 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100922undo_flush(bufinfo_T *bi)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200923{
Bram Moolenaar829aa642017-08-23 22:32:35 +0200924 if (bi->bi_buffer != NULL && bi->bi_state != NULL && bi->bi_used > 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200925 {
926 crypt_encode_inplace(bi->bi_state, bi->bi_buffer, bi->bi_used);
927 if (fwrite(bi->bi_buffer, bi->bi_used, (size_t)1, bi->bi_fp) != 1)
928 return FAIL;
929 bi->bi_used = 0;
930 }
931 return OK;
932}
933#endif
934
935/*
936 * Write "ptr[len]" and crypt the bytes when needed.
937 * Returns OK or FAIL.
938 */
939 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100940fwrite_crypt(bufinfo_T *bi, char_u *ptr, size_t len)
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200941{
942#ifdef FEAT_CRYPT
943 char_u *copy;
944 char_u small_buf[100];
945 size_t i;
946
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200947 if (bi->bi_state != NULL && bi->bi_buffer == NULL)
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200948 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100949 // crypting every piece of text separately
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200950 if (len < 100)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100951 copy = small_buf; // no malloc()/free() for short strings
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200952 else
953 {
954 copy = lalloc(len, FALSE);
955 if (copy == NULL)
956 return 0;
957 }
958 crypt_encode(bi->bi_state, ptr, len, copy);
959 i = fwrite(copy, len, (size_t)1, bi->bi_fp);
960 if (copy != small_buf)
961 vim_free(copy);
962 return i == 1 ? OK : FAIL;
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200963 }
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200964#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200965 return undo_write(bi, ptr, len);
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200966}
967
968/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200969 * Write a number, MSB first, in "len" bytes.
970 * Must match with undo_read_?c() functions.
971 * Returns OK or FAIL.
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200972 */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200973 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100974undo_write_bytes(bufinfo_T *bi, long_u nr, int len)
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200975{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200976 char_u buf[8];
977 int i;
978 int bufi = 0;
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200979
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200980 for (i = len - 1; i >= 0; --i)
Bram Moolenaar9b8f0212014-08-13 22:05:53 +0200981 buf[bufi++] = (char_u)(nr >> (i * 8));
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200982 return undo_write(bi, buf, (size_t)len);
983}
984
985/*
986 * Write the pointer to an undo header. Instead of writing the pointer itself
987 * we use the sequence number of the header. This is converted back to
988 * pointers when reading. */
989 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100990put_header_ptr(bufinfo_T *bi, u_header_T *uhp)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200991{
992 undo_write_bytes(bi, (long_u)(uhp != NULL ? uhp->uh_seq : 0), 4);
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200993}
994
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +0200995 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100996undo_read_4c(bufinfo_T *bi)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200997{
998#ifdef FEAT_CRYPT
999 if (bi->bi_buffer != NULL)
1000 {
1001 char_u buf[4];
1002 int n;
1003
1004 undo_read(bi, buf, (size_t)4);
Bram Moolenaar35169282014-09-11 22:50:09 +02001005 n = ((unsigned)buf[0] << 24) + (buf[1] << 16) + (buf[2] << 8) + buf[3];
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001006 return n;
1007 }
1008#endif
1009 return get4c(bi->bi_fp);
1010}
1011
1012 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001013undo_read_2c(bufinfo_T *bi)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001014{
1015#ifdef FEAT_CRYPT
1016 if (bi->bi_buffer != NULL)
1017 {
1018 char_u buf[2];
1019 int n;
1020
1021 undo_read(bi, buf, (size_t)2);
1022 n = (buf[0] << 8) + buf[1];
1023 return n;
1024 }
1025#endif
1026 return get2c(bi->bi_fp);
1027}
1028
1029 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001030undo_read_byte(bufinfo_T *bi)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001031{
1032#ifdef FEAT_CRYPT
1033 if (bi->bi_buffer != NULL)
1034 {
1035 char_u buf[1];
1036
1037 undo_read(bi, buf, (size_t)1);
1038 return buf[0];
1039 }
1040#endif
1041 return getc(bi->bi_fp);
1042}
1043
1044 static time_t
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001045undo_read_time(bufinfo_T *bi)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001046{
1047#ifdef FEAT_CRYPT
1048 if (bi->bi_buffer != NULL)
1049 {
1050 char_u buf[8];
1051 time_t n = 0;
1052 int i;
1053
1054 undo_read(bi, buf, (size_t)8);
1055 for (i = 0; i < 8; ++i)
1056 n = (n << 8) + buf[i];
1057 return n;
1058 }
1059#endif
1060 return get8ctime(bi->bi_fp);
1061}
1062
1063/*
1064 * Read "buffer[size]" from the undo file.
1065 * Return OK or FAIL.
1066 */
1067 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001068undo_read(bufinfo_T *bi, char_u *buffer, size_t size)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001069{
Bram Moolenaar56f2db52017-06-11 23:09:15 +02001070 int retval = OK;
1071
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001072#ifdef FEAT_CRYPT
1073 if (bi->bi_buffer != NULL)
1074 {
Bram Moolenaar9b8f0212014-08-13 22:05:53 +02001075 int size_todo = (int)size;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001076 char_u *p = buffer;
1077
1078 while (size_todo > 0)
1079 {
1080 size_t n;
1081
1082 if (bi->bi_used >= bi->bi_avail)
1083 {
1084 n = fread(bi->bi_buffer, 1, (size_t)CRYPT_BUF_SIZE, bi->bi_fp);
Bram Moolenaar55952d42016-11-05 14:58:34 +01001085 if (n == 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001086 {
Bram Moolenaar56f2db52017-06-11 23:09:15 +02001087 retval = FAIL;
1088 break;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001089 }
1090 bi->bi_avail = n;
1091 bi->bi_used = 0;
1092 crypt_decode_inplace(bi->bi_state, bi->bi_buffer, bi->bi_avail);
1093 }
1094 n = size_todo;
1095 if (n > bi->bi_avail - bi->bi_used)
1096 n = bi->bi_avail - bi->bi_used;
1097 mch_memmove(p, bi->bi_buffer + bi->bi_used, n);
1098 bi->bi_used += n;
Bram Moolenaar9b8f0212014-08-13 22:05:53 +02001099 size_todo -= (int)n;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001100 p += n;
1101 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001102 }
Bram Moolenaar56f2db52017-06-11 23:09:15 +02001103 else
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001104#endif
1105 if (fread(buffer, (size_t)size, 1, bi->bi_fp) != 1)
Bram Moolenaar56f2db52017-06-11 23:09:15 +02001106 retval = FAIL;
1107
1108 if (retval == FAIL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01001109 // Error may be checked for only later. Fill with zeros,
1110 // so that the reader won't use garbage.
Bram Moolenaar56f2db52017-06-11 23:09:15 +02001111 vim_memset(buffer, 0, size);
1112 return retval;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001113}
1114
1115/*
1116 * Read a string of length "len" from "bi->bi_fd".
1117 * "len" can be zero to allocate an empty line.
1118 * Decrypt the bytes if needed.
1119 * Append a NUL.
1120 * Returns a pointer to allocated memory or NULL for failure.
1121 */
1122 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001123read_string_decrypt(bufinfo_T *bi, int len)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001124{
Bram Moolenaar964b3742019-05-24 18:54:09 +02001125 char_u *ptr = alloc(len + 1);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001126
1127 if (ptr != NULL)
1128 {
1129 if (len > 0 && undo_read(bi, ptr, len) == FAIL)
1130 {
1131 vim_free(ptr);
1132 return NULL;
1133 }
Bram Moolenaarccae4672019-01-04 15:09:57 +01001134 // In case there are text properties there already is a NUL, but
1135 // checking for that is more expensive than just adding a dummy byte.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001136 ptr[len] = NUL;
1137#ifdef FEAT_CRYPT
1138 if (bi->bi_state != NULL && bi->bi_buffer == NULL)
1139 crypt_decode_inplace(bi->bi_state, ptr, len);
1140#endif
1141 }
1142 return ptr;
1143}
1144
1145/*
1146 * Writes the (not encrypted) header and initializes encryption if needed.
1147 */
1148 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001149serialize_header(bufinfo_T *bi, char_u *hash)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001150{
Bram Moolenaarccae4672019-01-04 15:09:57 +01001151 long len;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001152 buf_T *buf = bi->bi_buf;
1153 FILE *fp = bi->bi_fp;
1154 char_u time_buf[8];
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001155
Bram Moolenaare38eab22019-12-05 21:50:01 +01001156 // Start writing, first the magic marker and undo info version.
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001157 if (fwrite(UF_START_MAGIC, (size_t)UF_START_MAGIC_LEN, (size_t)1, fp) != 1)
1158 return FAIL;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001159
Bram Moolenaare38eab22019-12-05 21:50:01 +01001160 // If the buffer is encrypted then all text bytes following will be
1161 // encrypted. Numbers and other info is not crypted.
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001162#ifdef FEAT_CRYPT
Bram Moolenaarfa0ff9a2010-07-25 16:05:19 +02001163 if (*buf->b_p_key != NUL)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001164 {
1165 char_u *header;
1166 int header_len;
1167
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001168 undo_write_bytes(bi, (long_u)UF_VERSION_CRYPT, 2);
1169 bi->bi_state = crypt_create_for_writing(crypt_get_method_nr(buf),
1170 buf->b_p_key, &header, &header_len);
1171 if (bi->bi_state == NULL)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001172 return FAIL;
Bram Moolenaarccae4672019-01-04 15:09:57 +01001173 len = (long)fwrite(header, (size_t)header_len, (size_t)1, fp);
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001174 vim_free(header);
1175 if (len != 1)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001176 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001177 crypt_free_state(bi->bi_state);
1178 bi->bi_state = NULL;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001179 return FAIL;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001180 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001181
1182 if (crypt_whole_undofile(crypt_get_method_nr(buf)))
1183 {
1184 bi->bi_buffer = alloc(CRYPT_BUF_SIZE);
1185 if (bi->bi_buffer == NULL)
1186 {
1187 crypt_free_state(bi->bi_state);
1188 bi->bi_state = NULL;
1189 return FAIL;
1190 }
1191 bi->bi_used = 0;
1192 }
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001193 }
1194 else
1195#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001196 undo_write_bytes(bi, (long_u)UF_VERSION, 2);
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001197
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001198
Bram Moolenaare38eab22019-12-05 21:50:01 +01001199 // Write a hash of the buffer text, so that we can verify it is still the
1200 // same when reading the buffer text.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001201 if (undo_write(bi, hash, (size_t)UNDO_HASH_SIZE) == FAIL)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001202 return FAIL;
1203
Bram Moolenaare38eab22019-12-05 21:50:01 +01001204 // buffer-specific data
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001205 undo_write_bytes(bi, (long_u)buf->b_ml.ml_line_count, 4);
Bram Moolenaarccae4672019-01-04 15:09:57 +01001206 len = buf->b_u_line_ptr.ul_line == NULL
Bram Moolenaar8aef43b2019-01-08 20:14:35 +01001207 ? 0L : (long)STRLEN(buf->b_u_line_ptr.ul_line);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001208 undo_write_bytes(bi, (long_u)len, 4);
Bram Moolenaar8aef43b2019-01-08 20:14:35 +01001209 if (len > 0 && fwrite_crypt(bi, buf->b_u_line_ptr.ul_line, (size_t)len)
1210 == FAIL)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001211 return FAIL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001212 undo_write_bytes(bi, (long_u)buf->b_u_line_lnum, 4);
1213 undo_write_bytes(bi, (long_u)buf->b_u_line_colnr, 4);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001214
Bram Moolenaare38eab22019-12-05 21:50:01 +01001215 // Undo structures header data
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001216 put_header_ptr(bi, buf->b_u_oldhead);
1217 put_header_ptr(bi, buf->b_u_newhead);
1218 put_header_ptr(bi, buf->b_u_curhead);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001219
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001220 undo_write_bytes(bi, (long_u)buf->b_u_numhead, 4);
1221 undo_write_bytes(bi, (long_u)buf->b_u_seq_last, 4);
1222 undo_write_bytes(bi, (long_u)buf->b_u_seq_cur, 4);
1223 time_to_bytes(buf->b_u_time_cur, time_buf);
1224 undo_write(bi, time_buf, 8);
Bram Moolenaara800b422010-06-27 01:15:55 +02001225
Bram Moolenaare38eab22019-12-05 21:50:01 +01001226 // Optional fields.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001227 undo_write_bytes(bi, 4, 1);
1228 undo_write_bytes(bi, UF_LAST_SAVE_NR, 1);
1229 undo_write_bytes(bi, (long_u)buf->b_u_save_nr_last, 4);
Bram Moolenaara800b422010-06-27 01:15:55 +02001230
Bram Moolenaare38eab22019-12-05 21:50:01 +01001231 undo_write_bytes(bi, 0, 1); // end marker
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001232
1233 return OK;
1234}
1235
1236 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001237serialize_uhp(bufinfo_T *bi, u_header_T *uhp)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001238{
1239 int i;
1240 u_entry_T *uep;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001241 char_u time_buf[8];
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001242
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001243 if (undo_write_bytes(bi, (long_u)UF_HEADER_MAGIC, 2) == FAIL)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001244 return FAIL;
1245
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001246 put_header_ptr(bi, uhp->uh_next.ptr);
1247 put_header_ptr(bi, uhp->uh_prev.ptr);
1248 put_header_ptr(bi, uhp->uh_alt_next.ptr);
1249 put_header_ptr(bi, uhp->uh_alt_prev.ptr);
1250 undo_write_bytes(bi, uhp->uh_seq, 4);
1251 serialize_pos(bi, uhp->uh_cursor);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001252 undo_write_bytes(bi, (long_u)uhp->uh_cursor_vcol, 4);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001253 undo_write_bytes(bi, (long_u)uhp->uh_flags, 2);
Bram Moolenaare38eab22019-12-05 21:50:01 +01001254 // Assume NMARKS will stay the same.
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001255 for (i = 0; i < NMARKS; ++i)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001256 serialize_pos(bi, uhp->uh_namedm[i]);
1257 serialize_visualinfo(bi, &uhp->uh_visual);
1258 time_to_bytes(uhp->uh_time, time_buf);
1259 undo_write(bi, time_buf, 8);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001260
Bram Moolenaare38eab22019-12-05 21:50:01 +01001261 // Optional fields.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001262 undo_write_bytes(bi, 4, 1);
1263 undo_write_bytes(bi, UHP_SAVE_NR, 1);
1264 undo_write_bytes(bi, (long_u)uhp->uh_save_nr, 4);
Bram Moolenaara800b422010-06-27 01:15:55 +02001265
Bram Moolenaare38eab22019-12-05 21:50:01 +01001266 undo_write_bytes(bi, 0, 1); // end marker
Bram Moolenaara800b422010-06-27 01:15:55 +02001267
Bram Moolenaare38eab22019-12-05 21:50:01 +01001268 // Write all the entries.
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001269 for (uep = uhp->uh_entry; uep != NULL; uep = uep->ue_next)
1270 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001271 undo_write_bytes(bi, (long_u)UF_ENTRY_MAGIC, 2);
1272 if (serialize_uep(bi, uep) == FAIL)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001273 return FAIL;
1274 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001275 undo_write_bytes(bi, (long_u)UF_ENTRY_END_MAGIC, 2);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001276 return OK;
1277}
1278
1279 static u_header_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001280unserialize_uhp(bufinfo_T *bi, char_u *file_name)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001281{
1282 u_header_T *uhp;
1283 int i;
1284 u_entry_T *uep, *last_uep;
1285 int c;
1286 int error;
1287
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001288 uhp = U_ALLOC_LINE(sizeof(u_header_T));
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001289 if (uhp == NULL)
1290 return NULL;
1291 vim_memset(uhp, 0, sizeof(u_header_T));
1292#ifdef U_DEBUG
1293 uhp->uh_magic = UH_MAGIC;
1294#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001295 uhp->uh_next.seq = undo_read_4c(bi);
1296 uhp->uh_prev.seq = undo_read_4c(bi);
1297 uhp->uh_alt_next.seq = undo_read_4c(bi);
1298 uhp->uh_alt_prev.seq = undo_read_4c(bi);
1299 uhp->uh_seq = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001300 if (uhp->uh_seq <= 0)
1301 {
1302 corruption_error("uh_seq", file_name);
1303 vim_free(uhp);
1304 return NULL;
1305 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001306 unserialize_pos(bi, &uhp->uh_cursor);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001307 uhp->uh_cursor_vcol = undo_read_4c(bi);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001308 uhp->uh_flags = undo_read_2c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001309 for (i = 0; i < NMARKS; ++i)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001310 unserialize_pos(bi, &uhp->uh_namedm[i]);
1311 unserialize_visualinfo(bi, &uhp->uh_visual);
1312 uhp->uh_time = undo_read_time(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001313
Bram Moolenaare38eab22019-12-05 21:50:01 +01001314 // Optional fields.
Bram Moolenaar69154f22010-07-18 21:42:34 +02001315 for (;;)
1316 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001317 int len = undo_read_byte(bi);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001318 int what;
Bram Moolenaara800b422010-06-27 01:15:55 +02001319
Bram Moolenaarfb06d762019-08-04 18:55:35 +02001320 if (len == EOF)
1321 {
1322 corruption_error("truncated", file_name);
1323 u_free_uhp(uhp);
1324 return NULL;
1325 }
Bram Moolenaar69154f22010-07-18 21:42:34 +02001326 if (len == 0)
1327 break;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001328 what = undo_read_byte(bi);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001329 switch (what)
1330 {
1331 case UHP_SAVE_NR:
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001332 uhp->uh_save_nr = undo_read_4c(bi);
Bram Moolenaara800b422010-06-27 01:15:55 +02001333 break;
Bram Moolenaar69154f22010-07-18 21:42:34 +02001334 default:
Bram Moolenaare38eab22019-12-05 21:50:01 +01001335 // field not supported, skip
Bram Moolenaar69154f22010-07-18 21:42:34 +02001336 while (--len >= 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001337 (void)undo_read_byte(bi);
Bram Moolenaara800b422010-06-27 01:15:55 +02001338 }
Bram Moolenaar69154f22010-07-18 21:42:34 +02001339 }
Bram Moolenaara800b422010-06-27 01:15:55 +02001340
Bram Moolenaare38eab22019-12-05 21:50:01 +01001341 // Unserialize the uep list.
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001342 last_uep = NULL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001343 while ((c = undo_read_2c(bi)) == UF_ENTRY_MAGIC)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001344 {
1345 error = FALSE;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001346 uep = unserialize_uep(bi, &error, file_name);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001347 if (last_uep == NULL)
1348 uhp->uh_entry = uep;
1349 else
1350 last_uep->ue_next = uep;
1351 last_uep = uep;
1352 if (uep == NULL || error)
1353 {
1354 u_free_uhp(uhp);
1355 return NULL;
1356 }
1357 }
1358 if (c != UF_ENTRY_END_MAGIC)
1359 {
1360 corruption_error("entry end", file_name);
1361 u_free_uhp(uhp);
1362 return NULL;
1363 }
1364
1365 return uhp;
1366}
1367
Bram Moolenaar9db58062010-05-29 20:33:07 +02001368/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001369 * Serialize "uep".
Bram Moolenaar9db58062010-05-29 20:33:07 +02001370 */
1371 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001372serialize_uep(
1373 bufinfo_T *bi,
1374 u_entry_T *uep)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001375{
1376 int i;
1377 size_t len;
1378
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001379 undo_write_bytes(bi, (long_u)uep->ue_top, 4);
1380 undo_write_bytes(bi, (long_u)uep->ue_bot, 4);
1381 undo_write_bytes(bi, (long_u)uep->ue_lcount, 4);
1382 undo_write_bytes(bi, (long_u)uep->ue_size, 4);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001383 for (i = 0; i < uep->ue_size; ++i)
1384 {
Bram Moolenaarccae4672019-01-04 15:09:57 +01001385 // Text is written without the text properties, since we cannot restore
1386 // the text property types.
1387 len = STRLEN(uep->ue_array[i].ul_line);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001388 if (undo_write_bytes(bi, (long_u)len, 4) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001389 return FAIL;
Bram Moolenaarccae4672019-01-04 15:09:57 +01001390 if (len > 0 && fwrite_crypt(bi, uep->ue_array[i].ul_line, len) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001391 return FAIL;
1392 }
1393 return OK;
1394}
1395
1396 static u_entry_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001397unserialize_uep(bufinfo_T *bi, int *error, char_u *file_name)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001398{
1399 int i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001400 u_entry_T *uep;
Bram Moolenaarccae4672019-01-04 15:09:57 +01001401 undoline_T *array = NULL;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001402 char_u *line;
1403 int line_len;
1404
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001405 uep = U_ALLOC_LINE(sizeof(u_entry_T));
Bram Moolenaar9db58062010-05-29 20:33:07 +02001406 if (uep == NULL)
1407 return NULL;
1408 vim_memset(uep, 0, sizeof(u_entry_T));
1409#ifdef U_DEBUG
1410 uep->ue_magic = UE_MAGIC;
1411#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001412 uep->ue_top = undo_read_4c(bi);
1413 uep->ue_bot = undo_read_4c(bi);
1414 uep->ue_lcount = undo_read_4c(bi);
1415 uep->ue_size = undo_read_4c(bi);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001416 if (uep->ue_size > 0)
1417 {
Bram Moolenaar0c8485f2017-02-26 18:17:10 +01001418 if (uep->ue_size < LONG_MAX / (int)sizeof(char_u *))
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001419 array = U_ALLOC_LINE(sizeof(undoline_T) * uep->ue_size);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001420 if (array == NULL)
1421 {
1422 *error = TRUE;
1423 return uep;
1424 }
Bram Moolenaarccae4672019-01-04 15:09:57 +01001425 vim_memset(array, 0, sizeof(undoline_T) * uep->ue_size);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001426 }
1427 uep->ue_array = array;
1428
1429 for (i = 0; i < uep->ue_size; ++i)
1430 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001431 line_len = undo_read_4c(bi);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001432 if (line_len >= 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001433 line = read_string_decrypt(bi, line_len);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001434 else
1435 {
1436 line = NULL;
1437 corruption_error("line length", file_name);
1438 }
1439 if (line == NULL)
1440 {
1441 *error = TRUE;
1442 return uep;
1443 }
Bram Moolenaarccae4672019-01-04 15:09:57 +01001444 array[i].ul_line = line;
1445 array[i].ul_len = line_len + 1;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001446 }
1447 return uep;
1448}
1449
1450/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001451 * Serialize "pos".
Bram Moolenaar9db58062010-05-29 20:33:07 +02001452 */
1453 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001454serialize_pos(bufinfo_T *bi, pos_T pos)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001455{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001456 undo_write_bytes(bi, (long_u)pos.lnum, 4);
1457 undo_write_bytes(bi, (long_u)pos.col, 4);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001458 undo_write_bytes(bi, (long_u)pos.coladd, 4);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001459}
1460
1461/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001462 * Unserialize the pos_T at the current position.
Bram Moolenaar9db58062010-05-29 20:33:07 +02001463 */
1464 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001465unserialize_pos(bufinfo_T *bi, pos_T *pos)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001466{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001467 pos->lnum = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001468 if (pos->lnum < 0)
1469 pos->lnum = 0;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001470 pos->col = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001471 if (pos->col < 0)
1472 pos->col = 0;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001473 pos->coladd = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001474 if (pos->coladd < 0)
1475 pos->coladd = 0;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001476}
1477
1478/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001479 * Serialize "info".
Bram Moolenaar9db58062010-05-29 20:33:07 +02001480 */
1481 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001482serialize_visualinfo(bufinfo_T *bi, visualinfo_T *info)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001483{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001484 serialize_pos(bi, info->vi_start);
1485 serialize_pos(bi, info->vi_end);
1486 undo_write_bytes(bi, (long_u)info->vi_mode, 4);
1487 undo_write_bytes(bi, (long_u)info->vi_curswant, 4);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001488}
1489
1490/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001491 * Unserialize the visualinfo_T at the current position.
Bram Moolenaar9db58062010-05-29 20:33:07 +02001492 */
1493 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001494unserialize_visualinfo(bufinfo_T *bi, visualinfo_T *info)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001495{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001496 unserialize_pos(bi, &info->vi_start);
1497 unserialize_pos(bi, &info->vi_end);
1498 info->vi_mode = undo_read_4c(bi);
1499 info->vi_curswant = undo_read_4c(bi);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001500}
1501
1502/*
1503 * Write the undo tree in an undo file.
1504 * When "name" is not NULL, use it as the name of the undo file.
1505 * Otherwise use buf->b_ffname to generate the undo file name.
1506 * "buf" must never be null, buf->b_ffname is used to obtain the original file
1507 * permissions.
1508 * "forceit" is TRUE for ":wundo!", FALSE otherwise.
1509 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text.
1510 */
1511 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001512u_write_undo(
1513 char_u *name,
1514 int forceit,
1515 buf_T *buf,
1516 char_u *hash)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001517{
1518 u_header_T *uhp;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001519 char_u *file_name;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001520 int mark;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001521#ifdef U_DEBUG
1522 int headers_written = 0;
1523#endif
1524 int fd;
1525 FILE *fp = NULL;
1526 int perm;
1527 int write_ok = FALSE;
1528#ifdef UNIX
1529 int st_old_valid = FALSE;
Bram Moolenaar8767f522016-07-01 17:17:39 +02001530 stat_T st_old;
1531 stat_T st_new;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001532#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001533 bufinfo_T bi;
1534
Bram Moolenaar5a669b92014-08-12 20:14:33 +02001535 vim_memset(&bi, 0, sizeof(bi));
Bram Moolenaar9db58062010-05-29 20:33:07 +02001536
1537 if (name == NULL)
1538 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001539 file_name = u_get_undo_file_name(buf->b_ffname, FALSE);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001540 if (file_name == NULL)
1541 {
1542 if (p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001543 {
1544 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001545 smsg(
Bram Moolenaar504a8212010-05-30 17:17:42 +02001546 _("Cannot write undo file in any directory in 'undodir'"));
1547 verbose_leave();
1548 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001549 return;
1550 }
1551 }
1552 else
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001553 file_name = name;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001554
1555 /*
1556 * Decide about the permission to use for the undo file. If the buffer
1557 * has a name use the permission of the original file. Otherwise only
1558 * allow the user to access the undo file.
1559 */
1560 perm = 0600;
1561 if (buf->b_ffname != NULL)
1562 {
1563#ifdef UNIX
1564 if (mch_stat((char *)buf->b_ffname, &st_old) >= 0)
1565 {
1566 perm = st_old.st_mode;
1567 st_old_valid = TRUE;
1568 }
1569#else
1570 perm = mch_getperm(buf->b_ffname);
1571 if (perm < 0)
1572 perm = 0600;
1573#endif
1574 }
1575
Bram Moolenaare38eab22019-12-05 21:50:01 +01001576 // strip any s-bit and executable bit
Bram Moolenaar3cbac302015-04-21 16:12:06 +02001577 perm = perm & 0666;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001578
Bram Moolenaare38eab22019-12-05 21:50:01 +01001579 // If the undo file already exists, verify that it actually is an undo
1580 // file, and delete it.
Bram Moolenaar9db58062010-05-29 20:33:07 +02001581 if (mch_getperm(file_name) >= 0)
1582 {
1583 if (name == NULL || !forceit)
1584 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001585 // Check we can read it and it's an undo file.
Bram Moolenaar9db58062010-05-29 20:33:07 +02001586 fd = mch_open((char *)file_name, O_RDONLY|O_EXTRA, 0);
1587 if (fd < 0)
1588 {
1589 if (name != NULL || p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001590 {
1591 if (name == NULL)
1592 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001593 smsg(
Bram Moolenaar504a8212010-05-30 17:17:42 +02001594 _("Will not overwrite with undo file, cannot read: %s"),
Bram Moolenaar9db58062010-05-29 20:33:07 +02001595 file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001596 if (name == NULL)
1597 verbose_leave();
1598 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001599 goto theend;
1600 }
1601 else
1602 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02001603 char_u mbuf[UF_START_MAGIC_LEN];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001604 int len;
1605
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01001606 len = read_eintr(fd, mbuf, UF_START_MAGIC_LEN);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001607 close(fd);
1608 if (len < UF_START_MAGIC_LEN
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02001609 || memcmp(mbuf, UF_START_MAGIC, UF_START_MAGIC_LEN) != 0)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001610 {
1611 if (name != NULL || p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001612 {
1613 if (name == NULL)
1614 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001615 smsg(
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001616 _("Will not overwrite, this is not an undo file: %s"),
Bram Moolenaar9db58062010-05-29 20:33:07 +02001617 file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001618 if (name == NULL)
1619 verbose_leave();
1620 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001621 goto theend;
1622 }
1623 }
1624 }
1625 mch_remove(file_name);
1626 }
1627
Bram Moolenaare38eab22019-12-05 21:50:01 +01001628 // If there is no undo information at all, quit here after deleting any
1629 // existing undo file.
Bram Moolenaarccae4672019-01-04 15:09:57 +01001630 if (buf->b_u_numhead == 0 && buf->b_u_line_ptr.ul_line == NULL)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001631 {
1632 if (p_verbose > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001633 verb_msg(_("Skipping undo file write, nothing to undo"));
Bram Moolenaar504a8212010-05-30 17:17:42 +02001634 goto theend;
1635 }
1636
Bram Moolenaar9db58062010-05-29 20:33:07 +02001637 fd = mch_open((char *)file_name,
1638 O_CREAT|O_EXTRA|O_WRONLY|O_EXCL|O_NOFOLLOW, perm);
1639 if (fd < 0)
1640 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001641 semsg(_(e_not_open), file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001642 goto theend;
1643 }
1644 (void)mch_setperm(file_name, perm);
1645 if (p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001646 {
1647 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001648 smsg(_("Writing undo file: %s"), file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001649 verbose_leave();
1650 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001651
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001652#ifdef U_DEBUG
Bram Moolenaare38eab22019-12-05 21:50:01 +01001653 // Check there is no problem in undo info before writing.
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001654 u_check(FALSE);
1655#endif
1656
Bram Moolenaar9db58062010-05-29 20:33:07 +02001657#ifdef UNIX
1658 /*
1659 * Try to set the group of the undo file same as the original file. If
1660 * this fails, set the protection bits for the group same as the
1661 * protection bits for others.
1662 */
Bram Moolenaarce69e822010-07-21 20:31:07 +02001663 if (st_old_valid
1664 && mch_stat((char *)file_name, &st_new) >= 0
1665 && st_new.st_gid != st_old.st_gid
Bram Moolenaare38eab22019-12-05 21:50:01 +01001666# ifdef HAVE_FCHOWN // sequent-ptx lacks fchown()
Bram Moolenaarce69e822010-07-21 20:31:07 +02001667 && fchown(fd, (uid_t)-1, st_old.st_gid) != 0
Bram Moolenaar9db58062010-05-29 20:33:07 +02001668# endif
1669 )
1670 mch_setperm(file_name, (perm & 0707) | ((perm & 07) << 3));
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02001671# if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001672 if (buf->b_ffname != NULL)
1673 mch_copy_sec(buf->b_ffname, file_name);
1674# endif
1675#endif
1676
1677 fp = fdopen(fd, "w");
1678 if (fp == NULL)
1679 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001680 semsg(_(e_not_open), file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001681 close(fd);
1682 mch_remove(file_name);
1683 goto theend;
1684 }
1685
Bram Moolenaare38eab22019-12-05 21:50:01 +01001686 // Undo must be synced.
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001687 u_sync(TRUE);
1688
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001689 /*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001690 * Write the header. Initializes encryption, if enabled.
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001691 */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001692 bi.bi_buf = buf;
1693 bi.bi_fp = fp;
1694 if (serialize_header(&bi, hash) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001695 goto write_error;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001696
1697 /*
1698 * Iteratively serialize UHPs and their UEPs from the top down.
1699 */
1700 mark = ++lastmark;
1701 uhp = buf->b_u_oldhead;
1702 while (uhp != NULL)
1703 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001704 // Serialize current UHP if we haven't seen it
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001705 if (uhp->uh_walk != mark)
1706 {
1707 uhp->uh_walk = mark;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001708#ifdef U_DEBUG
1709 ++headers_written;
1710#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001711 if (serialize_uhp(&bi, uhp) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001712 goto write_error;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001713 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001714
Bram Moolenaare38eab22019-12-05 21:50:01 +01001715 // Now walk through the tree - algorithm from undo_time().
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001716 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != mark)
1717 uhp = uhp->uh_prev.ptr;
1718 else if (uhp->uh_alt_next.ptr != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001719 && uhp->uh_alt_next.ptr->uh_walk != mark)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001720 uhp = uhp->uh_alt_next.ptr;
1721 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001722 && uhp->uh_next.ptr->uh_walk != mark)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001723 uhp = uhp->uh_next.ptr;
1724 else if (uhp->uh_alt_prev.ptr != NULL)
1725 uhp = uhp->uh_alt_prev.ptr;
1726 else
1727 uhp = uhp->uh_next.ptr;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001728 }
1729
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001730 if (undo_write_bytes(&bi, (long_u)UF_HEADER_END_MAGIC, 2) == OK)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001731 write_ok = TRUE;
1732#ifdef U_DEBUG
1733 if (headers_written != buf->b_u_numhead)
Bram Moolenaar570064c2013-06-10 20:25:10 +02001734 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001735 semsg("Written %ld headers, ...", headers_written);
1736 semsg("... but numhead is %ld", buf->b_u_numhead);
Bram Moolenaar570064c2013-06-10 20:25:10 +02001737 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001738#endif
1739
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001740#ifdef FEAT_CRYPT
1741 if (bi.bi_state != NULL && undo_flush(&bi) == FAIL)
1742 write_ok = FALSE;
1743#endif
1744
Bram Moolenaar9db58062010-05-29 20:33:07 +02001745write_error:
1746 fclose(fp);
1747 if (!write_ok)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001748 semsg(_("E829: write error in undo file: %s"), file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001749
Bram Moolenaar4f974752019-02-17 17:44:42 +01001750#if defined(MSWIN)
Bram Moolenaare38eab22019-12-05 21:50:01 +01001751 // Copy file attributes; for systems where this can only be done after
1752 // closing the file.
Bram Moolenaar9db58062010-05-29 20:33:07 +02001753 if (buf->b_ffname != NULL)
1754 (void)mch_copy_file_attribute(buf->b_ffname, file_name);
1755#endif
1756#ifdef HAVE_ACL
1757 if (buf->b_ffname != NULL)
1758 {
1759 vim_acl_T acl;
1760
Bram Moolenaare38eab22019-12-05 21:50:01 +01001761 // For systems that support ACL: get the ACL from the original file.
Bram Moolenaar9db58062010-05-29 20:33:07 +02001762 acl = mch_get_acl(buf->b_ffname);
1763 mch_set_acl(file_name, acl);
Bram Moolenaard2aed442012-06-01 13:46:12 +02001764 mch_free_acl(acl);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001765 }
1766#endif
1767
1768theend:
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001769#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001770 if (bi.bi_state != NULL)
1771 crypt_free_state(bi.bi_state);
1772 vim_free(bi.bi_buffer);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001773#endif
Bram Moolenaar9db58062010-05-29 20:33:07 +02001774 if (file_name != name)
1775 vim_free(file_name);
1776}
1777
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001778/*
1779 * Load the undo tree from an undo file.
1780 * If "name" is not NULL use it as the undo file name. This also means being
1781 * a bit more verbose.
1782 * Otherwise use curbuf->b_ffname to generate the undo file name.
1783 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text.
1784 */
1785 void
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02001786u_read_undo(char_u *name, char_u *hash, char_u *orig_name UNUSED)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001787{
1788 char_u *file_name;
1789 FILE *fp;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001790 long version, str_len;
Bram Moolenaarccae4672019-01-04 15:09:57 +01001791 undoline_T line_ptr;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001792 linenr_T line_lnum;
1793 colnr_T line_colnr;
1794 linenr_T line_count;
Bram Moolenaar3eb16372017-02-26 18:11:36 +01001795 long num_head = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001796 long old_header_seq, new_header_seq, cur_header_seq;
1797 long seq_last, seq_cur;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001798 long last_save_nr = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001799 short old_idx = -1, new_idx = -1, cur_idx = -1;
1800 long num_read_uhps = 0;
1801 time_t seq_time;
1802 int i, j;
1803 int c;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001804 u_header_T *uhp;
1805 u_header_T **uhp_table = NULL;
1806 char_u read_hash[UNDO_HASH_SIZE];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001807 char_u magic_buf[UF_START_MAGIC_LEN];
1808#ifdef U_DEBUG
1809 int *uhp_table_used;
1810#endif
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001811#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001812 stat_T st_orig;
1813 stat_T st_undo;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001814#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001815 bufinfo_T bi;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001816
Bram Moolenaar5a669b92014-08-12 20:14:33 +02001817 vim_memset(&bi, 0, sizeof(bi));
Bram Moolenaarccae4672019-01-04 15:09:57 +01001818 line_ptr.ul_len = 0;
1819 line_ptr.ul_line = NULL;
1820
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001821 if (name == NULL)
1822 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001823 file_name = u_get_undo_file_name(curbuf->b_ffname, TRUE);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001824 if (file_name == NULL)
1825 return;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001826
1827#ifdef UNIX
Bram Moolenaare38eab22019-12-05 21:50:01 +01001828 // For safety we only read an undo file if the owner is equal to the
1829 // owner of the text file or equal to the current user.
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001830 if (mch_stat((char *)orig_name, &st_orig) >= 0
1831 && mch_stat((char *)file_name, &st_undo) >= 0
Bram Moolenaar3b262392013-09-08 15:40:49 +02001832 && st_orig.st_uid != st_undo.st_uid
1833 && st_undo.st_uid != getuid())
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001834 {
1835 if (p_verbose > 0)
1836 {
1837 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001838 smsg(_("Not reading undo file, owner differs: %s"),
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001839 file_name);
1840 verbose_leave();
1841 }
1842 return;
1843 }
1844#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001845 }
1846 else
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001847 file_name = name;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001848
1849 if (p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001850 {
1851 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001852 smsg(_("Reading undo file: %s"), file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001853 verbose_leave();
1854 }
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001855
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001856 fp = mch_fopen((char *)file_name, "r");
1857 if (fp == NULL)
1858 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001859 if (name != NULL || p_verbose > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001860 semsg(_("E822: Cannot open undo file for reading: %s"), file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001861 goto error;
1862 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001863 bi.bi_buf = curbuf;
1864 bi.bi_fp = fp;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001865
Bram Moolenaar9db58062010-05-29 20:33:07 +02001866 /*
1867 * Read the undo file header.
1868 */
1869 if (fread(magic_buf, UF_START_MAGIC_LEN, 1, fp) != 1
1870 || memcmp(magic_buf, UF_START_MAGIC, UF_START_MAGIC_LEN) != 0)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001871 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001872 semsg(_("E823: Not an undo file: %s"), file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001873 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001874 }
1875 version = get2c(fp);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001876 if (version == UF_VERSION_CRYPT)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001877 {
1878#ifdef FEAT_CRYPT
Bram Moolenaar56be9502010-06-06 14:20:26 +02001879 if (*curbuf->b_p_key == NUL)
1880 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001881 semsg(_("E832: Non-encrypted file has encrypted undo file: %s"),
Bram Moolenaar56be9502010-06-06 14:20:26 +02001882 file_name);
1883 goto error;
1884 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001885 bi.bi_state = crypt_create_from_file(fp, curbuf->b_p_key);
1886 if (bi.bi_state == NULL)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001887 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001888 semsg(_("E826: Undo file decryption failed: %s"), file_name);
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001889 goto error;
1890 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001891 if (crypt_whole_undofile(bi.bi_state->method_nr))
1892 {
1893 bi.bi_buffer = alloc(CRYPT_BUF_SIZE);
1894 if (bi.bi_buffer == NULL)
1895 {
1896 crypt_free_state(bi.bi_state);
1897 bi.bi_state = NULL;
1898 goto error;
1899 }
1900 bi.bi_avail = 0;
1901 bi.bi_used = 0;
1902 }
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001903#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001904 semsg(_("E827: Undo file is encrypted: %s"), file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001905 goto error;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001906#endif
1907 }
Bram Moolenaar69154f22010-07-18 21:42:34 +02001908 else if (version != UF_VERSION)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001909 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001910 semsg(_("E824: Incompatible undo file: %s"), file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001911 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001912 }
1913
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001914 if (undo_read(&bi, read_hash, (size_t)UNDO_HASH_SIZE) == FAIL)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001915 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001916 corruption_error("hash", file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001917 goto error;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001918 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001919 line_count = (linenr_T)undo_read_4c(&bi);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001920 if (memcmp(hash, read_hash, UNDO_HASH_SIZE) != 0
1921 || line_count != curbuf->b_ml.ml_line_count)
1922 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001923 if (p_verbose > 0 || name != NULL)
1924 {
Bram Moolenaar504a8212010-05-30 17:17:42 +02001925 if (name == NULL)
1926 verbose_enter();
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001927 give_warning((char_u *)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001928 _("File contents changed, cannot use undo info"), TRUE);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001929 if (name == NULL)
1930 verbose_leave();
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001931 }
1932 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001933 }
1934
Bram Moolenaare38eab22019-12-05 21:50:01 +01001935 // Read undo data for "U" command.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001936 str_len = undo_read_4c(&bi);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001937 if (str_len < 0)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001938 goto error;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001939 if (str_len > 0)
Bram Moolenaarccae4672019-01-04 15:09:57 +01001940 {
1941 line_ptr.ul_line = read_string_decrypt(&bi, str_len);
1942 line_ptr.ul_len = str_len + 1;
1943 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001944 line_lnum = (linenr_T)undo_read_4c(&bi);
1945 line_colnr = (colnr_T)undo_read_4c(&bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001946 if (line_lnum < 0 || line_colnr < 0)
1947 {
1948 corruption_error("line lnum/col", file_name);
1949 goto error;
1950 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001951
Bram Moolenaare38eab22019-12-05 21:50:01 +01001952 // Begin general undo data
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001953 old_header_seq = undo_read_4c(&bi);
1954 new_header_seq = undo_read_4c(&bi);
1955 cur_header_seq = undo_read_4c(&bi);
1956 num_head = undo_read_4c(&bi);
1957 seq_last = undo_read_4c(&bi);
1958 seq_cur = undo_read_4c(&bi);
1959 seq_time = undo_read_time(&bi);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001960
Bram Moolenaare38eab22019-12-05 21:50:01 +01001961 // Optional header fields.
Bram Moolenaar69154f22010-07-18 21:42:34 +02001962 for (;;)
1963 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001964 int len = undo_read_byte(&bi);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001965 int what;
Bram Moolenaara800b422010-06-27 01:15:55 +02001966
Bram Moolenaar69154f22010-07-18 21:42:34 +02001967 if (len == 0 || len == EOF)
1968 break;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001969 what = undo_read_byte(&bi);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001970 switch (what)
1971 {
1972 case UF_LAST_SAVE_NR:
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001973 last_save_nr = undo_read_4c(&bi);
Bram Moolenaara800b422010-06-27 01:15:55 +02001974 break;
Bram Moolenaar69154f22010-07-18 21:42:34 +02001975 default:
Bram Moolenaare38eab22019-12-05 21:50:01 +01001976 // field not supported, skip
Bram Moolenaar69154f22010-07-18 21:42:34 +02001977 while (--len >= 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001978 (void)undo_read_byte(&bi);
Bram Moolenaara800b422010-06-27 01:15:55 +02001979 }
Bram Moolenaar69154f22010-07-18 21:42:34 +02001980 }
Bram Moolenaara800b422010-06-27 01:15:55 +02001981
Bram Moolenaare38eab22019-12-05 21:50:01 +01001982 // uhp_table will store the freshly created undo headers we allocate
1983 // until we insert them into curbuf. The table remains sorted by the
1984 // sequence numbers of the headers.
1985 // When there are no headers uhp_table is NULL.
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001986 if (num_head > 0)
1987 {
Bram Moolenaar3eb16372017-02-26 18:11:36 +01001988 if (num_head < LONG_MAX / (long)sizeof(u_header_T *))
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001989 uhp_table = U_ALLOC_LINE(num_head * sizeof(u_header_T *));
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001990 if (uhp_table == NULL)
1991 goto error;
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001992 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001993
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001994 while ((c = undo_read_2c(&bi)) == UF_HEADER_MAGIC)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001995 {
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001996 if (num_read_uhps >= num_head)
1997 {
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001998 corruption_error("num_head too small", file_name);
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001999 goto error;
2000 }
2001
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002002 uhp = unserialize_uhp(&bi, file_name);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002003 if (uhp == NULL)
2004 goto error;
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02002005 uhp_table[num_read_uhps++] = uhp;
2006 }
2007
2008 if (num_read_uhps != num_head)
2009 {
2010 corruption_error("num_head", file_name);
2011 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002012 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02002013 if (c != UF_HEADER_END_MAGIC)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002014 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02002015 corruption_error("end marker", file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002016 goto error;
2017 }
2018
Bram Moolenaar9db58062010-05-29 20:33:07 +02002019#ifdef U_DEBUG
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002020 uhp_table_used = alloc_clear(sizeof(int) * num_head + 1);
Bram Moolenaar9db58062010-05-29 20:33:07 +02002021# define SET_FLAG(j) ++uhp_table_used[j]
2022#else
2023# define SET_FLAG(j)
2024#endif
2025
Bram Moolenaare38eab22019-12-05 21:50:01 +01002026 // We have put all of the headers into a table. Now we iterate through the
2027 // table and swizzle each sequence number we have stored in uh_*_seq into
2028 // a pointer corresponding to the header with that sequence number.
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002029 for (i = 0; i < num_head; i++)
2030 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002031 uhp = uhp_table[i];
2032 if (uhp == NULL)
2033 continue;
2034 for (j = 0; j < num_head; j++)
2035 if (uhp_table[j] != NULL && i != j
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002036 && uhp_table[i]->uh_seq == uhp_table[j]->uh_seq)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002037 {
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02002038 corruption_error("duplicate uh_seq", file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002039 goto error;
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02002040 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002041 for (j = 0; j < num_head; j++)
2042 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002043 && uhp_table[j]->uh_seq == uhp->uh_next.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002044 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002045 uhp->uh_next.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02002046 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002047 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002048 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002049 for (j = 0; j < num_head; j++)
2050 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002051 && uhp_table[j]->uh_seq == uhp->uh_prev.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002052 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002053 uhp->uh_prev.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02002054 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002055 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002056 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002057 for (j = 0; j < num_head; j++)
2058 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002059 && uhp_table[j]->uh_seq == uhp->uh_alt_next.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002060 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002061 uhp->uh_alt_next.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02002062 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002063 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002064 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002065 for (j = 0; j < num_head; j++)
2066 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002067 && uhp_table[j]->uh_seq == uhp->uh_alt_prev.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002068 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002069 uhp->uh_alt_prev.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02002070 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002071 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002072 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002073 if (old_header_seq > 0 && old_idx < 0 && uhp->uh_seq == old_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002074 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002075 old_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002076 SET_FLAG(i);
2077 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002078 if (new_header_seq > 0 && new_idx < 0 && uhp->uh_seq == new_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002079 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002080 new_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002081 SET_FLAG(i);
2082 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002083 if (cur_header_seq > 0 && cur_idx < 0 && uhp->uh_seq == cur_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002084 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002085 cur_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002086 SET_FLAG(i);
2087 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002088 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02002089
Bram Moolenaare38eab22019-12-05 21:50:01 +01002090 // Now that we have read the undo info successfully, free the current undo
2091 // info and use the info from the file.
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002092 u_blockfree(curbuf);
Bram Moolenaar9db58062010-05-29 20:33:07 +02002093 curbuf->b_u_oldhead = old_idx < 0 ? NULL : uhp_table[old_idx];
2094 curbuf->b_u_newhead = new_idx < 0 ? NULL : uhp_table[new_idx];
2095 curbuf->b_u_curhead = cur_idx < 0 ? NULL : uhp_table[cur_idx];
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002096 curbuf->b_u_line_ptr = line_ptr;
2097 curbuf->b_u_line_lnum = line_lnum;
2098 curbuf->b_u_line_colnr = line_colnr;
2099 curbuf->b_u_numhead = num_head;
2100 curbuf->b_u_seq_last = seq_last;
2101 curbuf->b_u_seq_cur = seq_cur;
Bram Moolenaara800b422010-06-27 01:15:55 +02002102 curbuf->b_u_time_cur = seq_time;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002103 curbuf->b_u_save_nr_last = last_save_nr;
Bram Moolenaardba01a02010-11-03 19:32:42 +01002104 curbuf->b_u_save_nr_cur = last_save_nr;
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02002105
2106 curbuf->b_u_synced = TRUE;
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002107 vim_free(uhp_table);
Bram Moolenaar9db58062010-05-29 20:33:07 +02002108
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002109#ifdef U_DEBUG
Bram Moolenaar9db58062010-05-29 20:33:07 +02002110 for (i = 0; i < num_head; ++i)
2111 if (uhp_table_used[i] == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002112 semsg("uhp_table entry %ld not used, leaking memory", i);
Bram Moolenaar9db58062010-05-29 20:33:07 +02002113 vim_free(uhp_table_used);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002114 u_check(TRUE);
2115#endif
Bram Moolenaar9db58062010-05-29 20:33:07 +02002116
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002117 if (name != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002118 smsg(_("Finished reading undo file %s"), file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002119 goto theend;
2120
2121error:
Bram Moolenaarccae4672019-01-04 15:09:57 +01002122 vim_free(line_ptr.ul_line);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002123 if (uhp_table != NULL)
2124 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002125 for (i = 0; i < num_read_uhps; i++)
2126 if (uhp_table[i] != NULL)
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02002127 u_free_uhp(uhp_table[i]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002128 vim_free(uhp_table);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002129 }
2130
2131theend:
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002132#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002133 if (bi.bi_state != NULL)
2134 crypt_free_state(bi.bi_state);
2135 vim_free(bi.bi_buffer);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002136#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002137 if (fp != NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002138 fclose(fp);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002139 if (file_name != name)
2140 vim_free(file_name);
2141 return;
2142}
2143
Bram Moolenaare38eab22019-12-05 21:50:01 +01002144#endif // FEAT_PERSISTENT_UNDO
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002145
2146
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147/*
2148 * If 'cpoptions' contains 'u': Undo the previous undo or redo (vi compatible).
2149 * If 'cpoptions' does not contain 'u': Always undo.
2150 */
2151 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002152u_undo(int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153{
2154 /*
2155 * If we get an undo command while executing a macro, we behave like the
2156 * original vi. If this happens twice in one macro the result will not
2157 * be compatible.
2158 */
2159 if (curbuf->b_u_synced == FALSE)
2160 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002161 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 count = 1;
2163 }
2164
2165 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
2166 undo_undoes = TRUE;
2167 else
2168 undo_undoes = !undo_undoes;
2169 u_doit(count);
2170}
2171
2172/*
2173 * If 'cpoptions' contains 'u': Repeat the previous undo or redo.
2174 * If 'cpoptions' does not contain 'u': Always redo.
2175 */
2176 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002177u_redo(int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178{
2179 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
2180 undo_undoes = FALSE;
2181 u_doit(count);
2182}
2183
2184/*
2185 * Undo or redo, depending on 'undo_undoes', 'count' times.
2186 */
2187 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002188u_doit(int startcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189{
Bram Moolenaarca003e12006-03-17 23:19:38 +00002190 int count = startcount;
2191
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002192 if (!undo_allowed())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194
2195 u_newcount = 0;
2196 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002197 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2198 u_oldcount = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 while (count--)
2200 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002201 // Do the change warning now, so that it triggers FileChangedRO when
2202 // needed. This may cause the file to be reloaded, that must happen
2203 // before we do anything, because it may change curbuf->b_u_curhead
2204 // and more.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002205 change_warning(0);
2206
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 if (undo_undoes)
2208 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002209 if (curbuf->b_u_curhead == NULL) // first undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 curbuf->b_u_curhead = curbuf->b_u_newhead;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002211 else if (get_undolevel() > 0) // multi level undo
2212 // get next undo
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002213 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_next.ptr;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002214 // nothing to undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 if (curbuf->b_u_numhead == 0 || curbuf->b_u_curhead == NULL)
2216 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002217 // stick curbuf->b_u_curhead at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 curbuf->b_u_curhead = curbuf->b_u_oldhead;
2219 beep_flush();
Bram Moolenaarca003e12006-03-17 23:19:38 +00002220 if (count == startcount - 1)
2221 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002222 msg(_("Already at oldest change"));
Bram Moolenaarca003e12006-03-17 23:19:38 +00002223 return;
2224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 break;
2226 }
2227
Bram Moolenaarca003e12006-03-17 23:19:38 +00002228 u_undoredo(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 }
2230 else
2231 {
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002232 if (curbuf->b_u_curhead == NULL || get_undolevel() <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002234 beep_flush(); // nothing to redo
Bram Moolenaarca003e12006-03-17 23:19:38 +00002235 if (count == startcount - 1)
2236 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002237 msg(_("Already at newest change"));
Bram Moolenaarca003e12006-03-17 23:19:38 +00002238 return;
2239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 break;
2241 }
2242
Bram Moolenaarca003e12006-03-17 23:19:38 +00002243 u_undoredo(FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002244
Bram Moolenaare38eab22019-12-05 21:50:01 +01002245 // Advance for next redo. Set "newhead" when at the end of the
2246 // redoable changes.
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002247 if (curbuf->b_u_curhead->uh_prev.ptr == NULL)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002248 curbuf->b_u_newhead = curbuf->b_u_curhead;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002249 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_prev.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 }
2251 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00002252 u_undo_end(undo_undoes, FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002253}
2254
Bram Moolenaar1e607892006-03-13 22:15:53 +00002255/*
2256 * Undo or redo over the timeline.
2257 * When "step" is negative go back in time, otherwise goes forward in time.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002258 * When "sec" is FALSE make "step" steps, when "sec" is TRUE use "step" as
2259 * seconds.
Bram Moolenaar730cde92010-06-27 05:18:54 +02002260 * When "file" is TRUE use "step" as a number of file writes.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002261 * When "absolute" is TRUE use "step" as the sequence number to jump to.
2262 * "sec" must be FALSE then.
Bram Moolenaar1e607892006-03-13 22:15:53 +00002263 */
2264 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002265undo_time(
2266 long step,
2267 int sec,
2268 int file,
2269 int absolute)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002270{
2271 long target;
2272 long closest;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002273 long closest_start;
2274 long closest_seq = 0;
2275 long val;
Bram Moolenaarce46d932018-01-30 22:46:06 +01002276 u_header_T *uhp = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002277 u_header_T *last;
2278 int mark;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02002279 int nomark = 0; // shut up compiler
Bram Moolenaar1e607892006-03-13 22:15:53 +00002280 int round;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002281 int dosec = sec;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002282 int dofile = file;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002283 int above = FALSE;
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002284 int did_undo = TRUE;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002285
Bram Moolenaare38eab22019-12-05 21:50:01 +01002286 // First make sure the current undoable change is synced.
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002287 if (curbuf->b_u_synced == FALSE)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002288 u_sync(TRUE);
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002289
Bram Moolenaar1e607892006-03-13 22:15:53 +00002290 u_newcount = 0;
2291 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002292 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002293 u_oldcount = -1;
2294
Bram Moolenaare38eab22019-12-05 21:50:01 +01002295 // "target" is the node below which we want to be.
2296 // Init "closest" to a value we can't reach.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002297 if (absolute)
2298 {
Bram Moolenaarce46d932018-01-30 22:46:06 +01002299 target = step;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002300 closest = -1;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002301 }
Bram Moolenaarca003e12006-03-17 23:19:38 +00002302 else
Bram Moolenaar1e607892006-03-13 22:15:53 +00002303 {
Bram Moolenaar730cde92010-06-27 05:18:54 +02002304 if (dosec)
Bram Moolenaarcbd4de42017-01-07 16:14:57 +01002305 target = (long)(curbuf->b_u_time_cur) + step;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002306 else if (dofile)
2307 {
2308 if (step < 0)
2309 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002310 // Going back to a previous write. If there were changes after
2311 // the last write, count that as moving one file-write, so
2312 // that ":earlier 1f" undoes all changes since the last save.
Bram Moolenaar730cde92010-06-27 05:18:54 +02002313 uhp = curbuf->b_u_curhead;
2314 if (uhp != NULL)
2315 uhp = uhp->uh_next.ptr;
2316 else
2317 uhp = curbuf->b_u_newhead;
2318 if (uhp != NULL && uhp->uh_save_nr != 0)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002319 // "uh_save_nr" was set in the last block, that means
2320 // there were no changes since the last write
Bram Moolenaar730cde92010-06-27 05:18:54 +02002321 target = curbuf->b_u_save_nr_cur + step;
2322 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01002323 // count the changes since the last write as one step
Bram Moolenaar730cde92010-06-27 05:18:54 +02002324 target = curbuf->b_u_save_nr_cur + step + 1;
2325 if (target <= 0)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002326 // Go to before first write: before the oldest change. Use
2327 // the sequence number for that.
Bram Moolenaar730cde92010-06-27 05:18:54 +02002328 dofile = FALSE;
2329 }
2330 else
2331 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002332 // Moving forward to a newer write.
Bram Moolenaar730cde92010-06-27 05:18:54 +02002333 target = curbuf->b_u_save_nr_cur + step;
2334 if (target > curbuf->b_u_save_nr_last)
2335 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002336 // Go to after last write: after the latest change. Use
2337 // the sequence number for that.
Bram Moolenaar730cde92010-06-27 05:18:54 +02002338 target = curbuf->b_u_seq_last + 1;
2339 dofile = FALSE;
2340 }
2341 }
2342 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002343 else
2344 target = curbuf->b_u_seq_cur + step;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002345 if (step < 0)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002346 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002347 if (target < 0)
2348 target = 0;
2349 closest = -1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002350 }
2351 else
2352 {
Bram Moolenaar730cde92010-06-27 05:18:54 +02002353 if (dosec)
Bram Moolenaarcbd4de42017-01-07 16:14:57 +01002354 closest = (long)(vim_time() + 1);
Bram Moolenaar730cde92010-06-27 05:18:54 +02002355 else if (dofile)
2356 closest = curbuf->b_u_save_nr_last + 2;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002357 else
2358 closest = curbuf->b_u_seq_last + 2;
2359 if (target >= closest)
2360 target = closest - 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002361 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002362 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002363 closest_start = closest;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002364 closest_seq = curbuf->b_u_seq_cur;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002365
Bram Moolenaare38eab22019-12-05 21:50:01 +01002366 // When "target" is 0; Back to origin.
Bram Moolenaarce46d932018-01-30 22:46:06 +01002367 if (target == 0)
Bram Moolenaar059fd012018-01-31 14:25:53 +01002368 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002369 mark = lastmark; // avoid that GCC complains
Bram Moolenaar059fd012018-01-31 14:25:53 +01002370 goto target_zero;
2371 }
Bram Moolenaarce46d932018-01-30 22:46:06 +01002372
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002373 /*
2374 * May do this twice:
Bram Moolenaar1e607892006-03-13 22:15:53 +00002375 * 1. Search for "target", update "closest" to the best match found.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002376 * 2. If "target" not found search for "closest".
2377 *
2378 * When using the closest time we use the sequence number in the second
2379 * round, because there may be several entries with the same time.
2380 */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002381 for (round = 1; round <= 2; ++round)
2382 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002383 // Find the path from the current state to where we want to go. The
2384 // desired state can be anywhere in the undo tree, need to go all over
2385 // it. We put "nomark" in uh_walk where we have been without success,
2386 // "mark" where it could possibly be.
Bram Moolenaar1e607892006-03-13 22:15:53 +00002387 mark = ++lastmark;
2388 nomark = ++lastmark;
2389
Bram Moolenaare38eab22019-12-05 21:50:01 +01002390 if (curbuf->b_u_curhead == NULL) // at leaf of the tree
Bram Moolenaar1e607892006-03-13 22:15:53 +00002391 uhp = curbuf->b_u_newhead;
2392 else
2393 uhp = curbuf->b_u_curhead;
2394
2395 while (uhp != NULL)
2396 {
2397 uhp->uh_walk = mark;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002398 if (dosec)
Bram Moolenaarcbd4de42017-01-07 16:14:57 +01002399 val = (long)(uhp->uh_time);
Bram Moolenaar730cde92010-06-27 05:18:54 +02002400 else if (dofile)
2401 val = uhp->uh_save_nr;
2402 else
2403 val = uhp->uh_seq;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002404
Bram Moolenaar730cde92010-06-27 05:18:54 +02002405 if (round == 1 && !(dofile && val == 0))
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002406 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002407 // Remember the header that is closest to the target.
2408 // It must be at least in the right direction (checked with
2409 // "b_u_seq_cur"). When the timestamp is equal find the
2410 // highest/lowest sequence number.
Bram Moolenaarca003e12006-03-17 23:19:38 +00002411 if ((step < 0 ? uhp->uh_seq <= curbuf->b_u_seq_cur
2412 : uhp->uh_seq > curbuf->b_u_seq_cur)
2413 && ((dosec && val == closest)
2414 ? (step < 0
2415 ? uhp->uh_seq < closest_seq
2416 : uhp->uh_seq > closest_seq)
2417 : closest == closest_start
2418 || (val > target
2419 ? (closest > target
2420 ? val - target <= closest - target
2421 : val - target <= target - closest)
2422 : (closest > target
2423 ? target - val <= closest - target
2424 : target - val <= target - closest))))
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002425 {
2426 closest = val;
2427 closest_seq = uhp->uh_seq;
2428 }
2429 }
2430
Bram Moolenaare38eab22019-12-05 21:50:01 +01002431 // Quit searching when we found a match. But when searching for a
2432 // time we need to continue looking for the best uh_seq.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002433 if (target == val && !dosec)
Bram Moolenaar730cde92010-06-27 05:18:54 +02002434 {
2435 target = uhp->uh_seq;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002436 break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002437 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002438
Bram Moolenaare38eab22019-12-05 21:50:01 +01002439 // go down in the tree if we haven't been there
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002440 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != nomark
2441 && uhp->uh_prev.ptr->uh_walk != mark)
2442 uhp = uhp->uh_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002443
Bram Moolenaare38eab22019-12-05 21:50:01 +01002444 // go to alternate branch if we haven't been there
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002445 else if (uhp->uh_alt_next.ptr != NULL
2446 && uhp->uh_alt_next.ptr->uh_walk != nomark
2447 && uhp->uh_alt_next.ptr->uh_walk != mark)
2448 uhp = uhp->uh_alt_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002449
Bram Moolenaare38eab22019-12-05 21:50:01 +01002450 // go up in the tree if we haven't been there and we are at the
2451 // start of alternate branches
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002452 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
2453 && uhp->uh_next.ptr->uh_walk != nomark
2454 && uhp->uh_next.ptr->uh_walk != mark)
Bram Moolenaardb552d602006-03-23 22:59:57 +00002455 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002456 // If still at the start we don't go through this change.
Bram Moolenaardb552d602006-03-23 22:59:57 +00002457 if (uhp == curbuf->b_u_curhead)
2458 uhp->uh_walk = nomark;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002459 uhp = uhp->uh_next.ptr;
Bram Moolenaardb552d602006-03-23 22:59:57 +00002460 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002461
2462 else
2463 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002464 // need to backtrack; mark this node as useless
Bram Moolenaar1e607892006-03-13 22:15:53 +00002465 uhp->uh_walk = nomark;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002466 if (uhp->uh_alt_prev.ptr != NULL)
2467 uhp = uhp->uh_alt_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002468 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002469 uhp = uhp->uh_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002470 }
2471 }
2472
Bram Moolenaare38eab22019-12-05 21:50:01 +01002473 if (uhp != NULL) // found it
Bram Moolenaar1e607892006-03-13 22:15:53 +00002474 break;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002475
2476 if (absolute)
2477 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002478 semsg(_("E830: Undo number %ld not found"), step);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002479 return;
2480 }
2481
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002482 if (closest == closest_start)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002483 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002484 if (step < 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002485 msg(_("Already at oldest change"));
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002486 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002487 msg(_("Already at newest change"));
Bram Moolenaar1e607892006-03-13 22:15:53 +00002488 return;
2489 }
2490
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002491 target = closest_seq;
2492 dosec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002493 dofile = FALSE;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002494 if (step < 0)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002495 above = TRUE; // stop above the header
Bram Moolenaar1e607892006-03-13 22:15:53 +00002496 }
2497
Bram Moolenaar059fd012018-01-31 14:25:53 +01002498target_zero:
Bram Moolenaare38eab22019-12-05 21:50:01 +01002499 // If we found it: Follow the path to go to where we want to be.
Bram Moolenaarce46d932018-01-30 22:46:06 +01002500 if (uhp != NULL || target == 0)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002501 {
2502 /*
2503 * First go up the tree as much as needed.
2504 */
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002505 while (!got_int)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002506 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002507 // Do the change warning now, for the same reason as above.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002508 change_warning(0);
2509
Bram Moolenaar1e607892006-03-13 22:15:53 +00002510 uhp = curbuf->b_u_curhead;
2511 if (uhp == NULL)
2512 uhp = curbuf->b_u_newhead;
2513 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002514 uhp = uhp->uh_next.ptr;
Bram Moolenaarce46d932018-01-30 22:46:06 +01002515 if (uhp == NULL || (target > 0 && uhp->uh_walk != mark)
Bram Moolenaarca003e12006-03-17 23:19:38 +00002516 || (uhp->uh_seq == target && !above))
Bram Moolenaar1e607892006-03-13 22:15:53 +00002517 break;
2518 curbuf->b_u_curhead = uhp;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002519 u_undoredo(TRUE);
Bram Moolenaarce46d932018-01-30 22:46:06 +01002520 if (target > 0)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002521 uhp->uh_walk = nomark; // don't go back down here
Bram Moolenaar1e607892006-03-13 22:15:53 +00002522 }
2523
Bram Moolenaare38eab22019-12-05 21:50:01 +01002524 // When back to origin, redo is not needed.
Bram Moolenaarce46d932018-01-30 22:46:06 +01002525 if (target > 0)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002526 {
Bram Moolenaarce46d932018-01-30 22:46:06 +01002527 /*
2528 * And now go down the tree (redo), branching off where needed.
2529 */
2530 while (!got_int)
2531 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002532 // Do the change warning now, for the same reason as above.
Bram Moolenaarce46d932018-01-30 22:46:06 +01002533 change_warning(0);
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002534
Bram Moolenaarce46d932018-01-30 22:46:06 +01002535 uhp = curbuf->b_u_curhead;
2536 if (uhp == NULL)
2537 break;
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002538
Bram Moolenaare38eab22019-12-05 21:50:01 +01002539 // Go back to the first branch with a mark.
Bram Moolenaarce46d932018-01-30 22:46:06 +01002540 while (uhp->uh_alt_prev.ptr != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002541 && uhp->uh_alt_prev.ptr->uh_walk == mark)
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002542 uhp = uhp->uh_alt_prev.ptr;
Bram Moolenaarce46d932018-01-30 22:46:06 +01002543
Bram Moolenaare38eab22019-12-05 21:50:01 +01002544 // Find the last branch with a mark, that's the one.
Bram Moolenaarce46d932018-01-30 22:46:06 +01002545 last = uhp;
2546 while (last->uh_alt_next.ptr != NULL
2547 && last->uh_alt_next.ptr->uh_walk == mark)
2548 last = last->uh_alt_next.ptr;
2549 if (last != uhp)
2550 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002551 // Make the used branch the first entry in the list of
2552 // alternatives to make "u" and CTRL-R take this branch.
Bram Moolenaarce46d932018-01-30 22:46:06 +01002553 while (uhp->uh_alt_prev.ptr != NULL)
2554 uhp = uhp->uh_alt_prev.ptr;
2555 if (last->uh_alt_next.ptr != NULL)
2556 last->uh_alt_next.ptr->uh_alt_prev.ptr =
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002557 last->uh_alt_prev.ptr;
Bram Moolenaarce46d932018-01-30 22:46:06 +01002558 last->uh_alt_prev.ptr->uh_alt_next.ptr =
2559 last->uh_alt_next.ptr;
2560 last->uh_alt_prev.ptr = NULL;
2561 last->uh_alt_next.ptr = uhp;
2562 uhp->uh_alt_prev.ptr = last;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002563
Bram Moolenaarce46d932018-01-30 22:46:06 +01002564 if (curbuf->b_u_oldhead == uhp)
2565 curbuf->b_u_oldhead = last;
2566 uhp = last;
2567 if (uhp->uh_next.ptr != NULL)
2568 uhp->uh_next.ptr->uh_prev.ptr = uhp;
2569 }
2570 curbuf->b_u_curhead = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002571
Bram Moolenaarce46d932018-01-30 22:46:06 +01002572 if (uhp->uh_walk != mark)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002573 break; // must have reached the target
Bram Moolenaar1e607892006-03-13 22:15:53 +00002574
Bram Moolenaare38eab22019-12-05 21:50:01 +01002575 // Stop when going backwards in time and didn't find the exact
2576 // header we were looking for.
Bram Moolenaarce46d932018-01-30 22:46:06 +01002577 if (uhp->uh_seq == target && above)
2578 {
2579 curbuf->b_u_seq_cur = target - 1;
2580 break;
2581 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002582
Bram Moolenaarce46d932018-01-30 22:46:06 +01002583 u_undoredo(FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002584
Bram Moolenaare38eab22019-12-05 21:50:01 +01002585 // Advance "curhead" to below the header we last used. If it
2586 // becomes NULL then we need to set "newhead" to this leaf.
Bram Moolenaarce46d932018-01-30 22:46:06 +01002587 if (uhp->uh_prev.ptr == NULL)
2588 curbuf->b_u_newhead = uhp;
2589 curbuf->b_u_curhead = uhp->uh_prev.ptr;
2590 did_undo = FALSE;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002591
Bram Moolenaare38eab22019-12-05 21:50:01 +01002592 if (uhp->uh_seq == target) // found it!
Bram Moolenaarce46d932018-01-30 22:46:06 +01002593 break;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002594
Bram Moolenaarce46d932018-01-30 22:46:06 +01002595 uhp = uhp->uh_prev.ptr;
2596 if (uhp == NULL || uhp->uh_walk != mark)
2597 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002598 // Need to redo more but can't find it...
Bram Moolenaarce46d932018-01-30 22:46:06 +01002599 internal_error("undo_time()");
2600 break;
2601 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002602 }
2603 }
2604 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00002605 u_undo_end(did_undo, absolute);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606}
2607
2608/*
2609 * u_undoredo: common code for undo and redo
2610 *
2611 * The lines in the file are replaced by the lines in the entry list at
2612 * curbuf->b_u_curhead. The replaced lines in the file are saved in the entry
2613 * list for the next undo/redo.
Bram Moolenaarca003e12006-03-17 23:19:38 +00002614 *
2615 * When "undo" is TRUE we go up in the tree, when FALSE we go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 */
2617 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002618u_undoredo(int undo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619{
Bram Moolenaarccae4672019-01-04 15:09:57 +01002620 undoline_T *newarray = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 linenr_T oldsize;
2622 linenr_T newsize;
2623 linenr_T top, bot;
2624 linenr_T lnum;
2625 linenr_T newlnum = MAXLNUM;
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002626 pos_T new_curpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 long i;
2628 u_entry_T *uep, *nuep;
2629 u_entry_T *newlist = NULL;
2630 int old_flags;
2631 int new_flags;
2632 pos_T namedm[NMARKS];
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002633 visualinfo_T visualinfo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002634 int empty_buffer; // buffer became empty
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002635 u_header_T *curhead = curbuf->b_u_curhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636
Bram Moolenaare38eab22019-12-05 21:50:01 +01002637 // Don't want autocommands using the undo structures here, they are
2638 // invalid till the end.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002639 block_autocmds();
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002640
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002641#ifdef U_DEBUG
2642 u_check(FALSE);
2643#endif
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002644 old_flags = curhead->uh_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 new_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
2646 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
2647 setpcmark();
2648
2649 /*
2650 * save marks before undo/redo
2651 */
2652 mch_memmove(namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002653 visualinfo = curbuf->b_visual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 curbuf->b_op_start.lnum = curbuf->b_ml.ml_line_count;
2655 curbuf->b_op_start.col = 0;
2656 curbuf->b_op_end.lnum = 0;
2657 curbuf->b_op_end.col = 0;
2658
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002659 for (uep = curhead->uh_entry; uep != NULL; uep = nuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 {
2661 top = uep->ue_top;
2662 bot = uep->ue_bot;
2663 if (bot == 0)
2664 bot = curbuf->b_ml.ml_line_count + 1;
2665 if (top > curbuf->b_ml.ml_line_count || top >= bot
2666 || bot > curbuf->b_ml.ml_line_count + 1)
2667 {
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002668 unblock_autocmds();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002669 iemsg(_("E438: u_undo: line numbers wrong"));
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002670 changed(); // don't want UNCHANGED now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 return;
2672 }
2673
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002674 oldsize = bot - top - 1; // number of lines before undo
2675 newsize = uep->ue_size; // number of lines after undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002677 // Decide about the cursor position, depending on what text changed.
2678 // Don't set it yet, it may be invalid if lines are going to be added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 if (top < newlnum)
2680 {
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002681 // If the saved cursor is somewhere in this undo block, move it to
2682 // the remembered position. Makes "gwap" put the cursor back
2683 // where it was.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002684 lnum = curhead->uh_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 if (lnum >= top && lnum <= top + newsize + 1)
2686 {
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002687 new_curpos = curhead->uh_cursor;
2688 newlnum = new_curpos.lnum - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 }
2690 else
2691 {
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002692 // Use the first line that actually changed. Avoids that
2693 // undoing auto-formatting puts the cursor in the previous
2694 // line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 for (i = 0; i < newsize && i < oldsize; ++i)
Bram Moolenaarccae4672019-01-04 15:09:57 +01002696 {
2697 char_u *p = ml_get(top + 1 + i);
2698
2699 if (curbuf->b_ml.ml_line_len != uep->ue_array[i].ul_len
Bram Moolenaar964b3742019-05-24 18:54:09 +02002700 || memcmp(uep->ue_array[i].ul_line, p,
2701 curbuf->b_ml.ml_line_len) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 break;
Bram Moolenaarccae4672019-01-04 15:09:57 +01002703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 if (i == newsize && newlnum == MAXLNUM && uep->ue_next == NULL)
2705 {
2706 newlnum = top;
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002707 new_curpos.lnum = newlnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 }
2709 else if (i < newsize)
2710 {
2711 newlnum = top + i;
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002712 new_curpos.lnum = newlnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 }
2714 }
2715 }
2716
2717 empty_buffer = FALSE;
2718
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002719 /*
2720 * Delete the lines between top and bot and save them in newarray.
2721 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002722 if (oldsize > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002724 if ((newarray = U_ALLOC_LINE(sizeof(undoline_T) * oldsize)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 {
Bram Moolenaarccae4672019-01-04 15:09:57 +01002726 do_outofmem_msg((long_u)(sizeof(undoline_T) * oldsize));
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002727
2728 // We have messed up the entry list, repair is impossible.
2729 // we have to free the rest of the list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 while (uep != NULL)
2731 {
2732 nuep = uep->ue_next;
2733 u_freeentry(uep, uep->ue_size);
2734 uep = nuep;
2735 }
2736 break;
2737 }
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002738 // delete backwards, it goes faster in most cases
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 for (lnum = bot - 1, i = oldsize; --i >= 0; --lnum)
2740 {
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002741 // what can we do when we run out of memory?
Bram Moolenaarccae4672019-01-04 15:09:57 +01002742 if (u_save_line(&newarray[i], lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 do_outofmem_msg((long_u)0);
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002744 // remember we deleted the last line in the buffer, and a
2745 // dummy empty line will be inserted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 if (curbuf->b_ml.ml_line_count == 1)
2747 empty_buffer = TRUE;
2748 ml_delete(lnum, FALSE);
2749 }
2750 }
Bram Moolenaar8d343302005-07-12 22:46:17 +00002751 else
2752 newarray = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002754 // make sure the cursor is on a valid line after the deletions
2755 check_cursor_lnum();
2756
2757 /*
2758 * Insert the lines in u_array between top and bot.
2759 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 if (newsize)
2761 {
2762 for (lnum = top, i = 0; i < newsize; ++i, ++lnum)
2763 {
Bram Moolenaarccae4672019-01-04 15:09:57 +01002764 // If the file is empty, there is an empty line 1 that we
2765 // should get rid of, by replacing it with the new line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 if (empty_buffer && lnum == 0)
Bram Moolenaar964b3742019-05-24 18:54:09 +02002767 ml_replace_len((linenr_T)1, uep->ue_array[i].ul_line,
2768 uep->ue_array[i].ul_len, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 else
Bram Moolenaar964b3742019-05-24 18:54:09 +02002770 ml_append(lnum, uep->ue_array[i].ul_line,
2771 (colnr_T)uep->ue_array[i].ul_len, FALSE);
Bram Moolenaarccae4672019-01-04 15:09:57 +01002772 vim_free(uep->ue_array[i].ul_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 }
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002774 vim_free((char_u *)uep->ue_array);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 }
2776
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002777 // adjust marks
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 if (oldsize != newsize)
2779 {
2780 mark_adjust(top + 1, top + oldsize, (long)MAXLNUM,
2781 (long)newsize - (long)oldsize);
2782 if (curbuf->b_op_start.lnum > top + oldsize)
2783 curbuf->b_op_start.lnum += newsize - oldsize;
2784 if (curbuf->b_op_end.lnum > top + oldsize)
2785 curbuf->b_op_end.lnum += newsize - oldsize;
2786 }
2787
2788 changed_lines(top + 1, 0, bot, newsize - oldsize);
2789
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002790 // set '[ and '] mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 if (top + 1 < curbuf->b_op_start.lnum)
2792 curbuf->b_op_start.lnum = top + 1;
2793 if (newsize == 0 && top + 1 > curbuf->b_op_end.lnum)
2794 curbuf->b_op_end.lnum = top + 1;
2795 else if (top + newsize > curbuf->b_op_end.lnum)
2796 curbuf->b_op_end.lnum = top + newsize;
2797
2798 u_newcount += newsize;
2799 u_oldcount += oldsize;
2800 uep->ue_size = oldsize;
2801 uep->ue_array = newarray;
2802 uep->ue_bot = top + newsize + 1;
2803
2804 /*
2805 * insert this entry in front of the new entry list
2806 */
2807 nuep = uep->ue_next;
2808 uep->ue_next = newlist;
2809 newlist = uep;
2810 }
2811
Bram Moolenaar4544bd22019-09-08 15:27:21 +02002812 // Set the cursor to the desired position. Check that the line is valid.
2813 curwin->w_cursor = new_curpos;
2814 check_cursor_lnum();
2815
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002816 curhead->uh_entry = newlist;
2817 curhead->uh_flags = new_flags;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002818 if ((old_flags & UH_EMPTYBUF) && BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 curbuf->b_ml.ml_flags |= ML_EMPTY;
2820 if (old_flags & UH_CHANGED)
2821 changed();
2822 else
Bram Moolenaar009b2592004-10-24 19:18:58 +00002823#ifdef FEAT_NETBEANS_INTG
Bram Moolenaare38eab22019-12-05 21:50:01 +01002824 // per netbeans undo rules, keep it as modified
Bram Moolenaar009b2592004-10-24 19:18:58 +00002825 if (!isNetbeansModified(curbuf))
2826#endif
Bram Moolenaarc024b462019-06-08 18:07:21 +02002827 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828
2829 /*
2830 * restore marks from before undo/redo
2831 */
2832 for (i = 0; i < NMARKS; ++i)
Bram Moolenaarf65aad52015-02-17 13:43:40 +01002833 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002834 if (curhead->uh_namedm[i].lnum != 0)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002835 curbuf->b_namedm[i] = curhead->uh_namedm[i];
Bram Moolenaarf65aad52015-02-17 13:43:40 +01002836 if (namedm[i].lnum != 0)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002837 curhead->uh_namedm[i] = namedm[i];
Bram Moolenaarf65aad52015-02-17 13:43:40 +01002838 else
2839 curhead->uh_namedm[i].lnum = 0;
2840 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002841 if (curhead->uh_visual.vi_start.lnum != 0)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002842 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002843 curbuf->b_visual = curhead->uh_visual;
2844 curhead->uh_visual = visualinfo;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846
2847 /*
2848 * If the cursor is only off by one line, put it at the same position as
2849 * before starting the change (for the "o" command).
2850 * Otherwise the cursor should go to the first undone line.
2851 */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002852 if (curhead->uh_cursor.lnum + 1 == curwin->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 && curwin->w_cursor.lnum > 1)
2854 --curwin->w_cursor.lnum;
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002855 if (curwin->w_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 {
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002857 if (curhead->uh_cursor.lnum == curwin->w_cursor.lnum)
2858 {
2859 curwin->w_cursor.col = curhead->uh_cursor.col;
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002860 if (virtual_active() && curhead->uh_cursor_vcol >= 0)
2861 coladvance((colnr_T)curhead->uh_cursor_vcol);
2862 else
2863 curwin->w_cursor.coladd = 0;
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002864 }
2865 else
2866 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 else
2869 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002870 // We get here with the current cursor line being past the end (eg
2871 // after adding lines at the end of the file, and then undoing it).
2872 // check_cursor() will move the cursor to the last line. Move it to
2873 // the first column here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 }
2877
Bram Moolenaare38eab22019-12-05 21:50:01 +01002878 // Make sure the cursor is on an existing line and column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 check_cursor();
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002880
Bram Moolenaare38eab22019-12-05 21:50:01 +01002881 // Remember where we are for "g-" and ":earlier 10s".
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002882 curbuf->b_u_seq_cur = curhead->uh_seq;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002883 if (undo)
Bram Moolenaar80eaddd2017-11-11 23:37:08 +01002884 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002885 // We are below the previous undo. However, to make ":earlier 1s"
2886 // work we compute this as being just above the just undone change.
Bram Moolenaar80eaddd2017-11-11 23:37:08 +01002887 if (curhead->uh_next.ptr != NULL)
2888 curbuf->b_u_seq_cur = curhead->uh_next.ptr->uh_seq;
2889 else
2890 curbuf->b_u_seq_cur = 0;
2891 }
Bram Moolenaarca003e12006-03-17 23:19:38 +00002892
Bram Moolenaare38eab22019-12-05 21:50:01 +01002893 // Remember where we are for ":earlier 1f" and ":later 1f".
Bram Moolenaar730cde92010-06-27 05:18:54 +02002894 if (curhead->uh_save_nr != 0)
2895 {
2896 if (undo)
2897 curbuf->b_u_save_nr_cur = curhead->uh_save_nr - 1;
2898 else
2899 curbuf->b_u_save_nr_cur = curhead->uh_save_nr;
2900 }
2901
Bram Moolenaare38eab22019-12-05 21:50:01 +01002902 // The timestamp can be the same for multiple changes, just use the one of
2903 // the undone/redone change.
Bram Moolenaara800b422010-06-27 01:15:55 +02002904 curbuf->b_u_time_cur = curhead->uh_time;
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002905
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002906 unblock_autocmds();
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002907#ifdef U_DEBUG
2908 u_check(FALSE);
2909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910}
2911
2912/*
2913 * If we deleted or added lines, report the number of less/more lines.
2914 * Otherwise, report the number of changes (this may be incorrect
2915 * in some cases, but it's better than nothing).
2916 */
2917 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002918u_undo_end(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002919 int did_undo, // just did an undo
2920 int absolute) // used ":undo N"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921{
Bram Moolenaar89d40322006-08-29 15:30:07 +00002922 char *msgstr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002923 u_header_T *uhp;
2924 char_u msgbuf[80];
Bram Moolenaar1e607892006-03-13 22:15:53 +00002925
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926#ifdef FEAT_FOLDING
2927 if ((fdo_flags & FDO_UNDO) && KeyTyped)
2928 foldOpenCursor();
2929#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +00002930
Bram Moolenaare38eab22019-12-05 21:50:01 +01002931 if (global_busy // no messages now, wait until global is finished
2932 || !messaging()) // 'lazyredraw' set, don't do messages now
Bram Moolenaar1e607892006-03-13 22:15:53 +00002933 return;
2934
2935 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2936 --u_newcount;
2937
2938 u_oldcount -= u_newcount;
2939 if (u_oldcount == -1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002940 msgstr = N_("more line");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002941 else if (u_oldcount < 0)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002942 msgstr = N_("more lines");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002943 else if (u_oldcount == 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002944 msgstr = N_("line less");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002945 else if (u_oldcount > 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002946 msgstr = N_("fewer lines");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002947 else
2948 {
2949 u_oldcount = u_newcount;
2950 if (u_newcount == 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002951 msgstr = N_("change");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002952 else
Bram Moolenaar89d40322006-08-29 15:30:07 +00002953 msgstr = N_("changes");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002954 }
2955
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002956 if (curbuf->b_u_curhead != NULL)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002957 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002958 // For ":undo N" we prefer a "after #N" message.
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002959 if (absolute && curbuf->b_u_curhead->uh_next.ptr != NULL)
Bram Moolenaardb552d602006-03-23 22:59:57 +00002960 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002961 uhp = curbuf->b_u_curhead->uh_next.ptr;
Bram Moolenaardb552d602006-03-23 22:59:57 +00002962 did_undo = FALSE;
2963 }
2964 else if (did_undo)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002965 uhp = curbuf->b_u_curhead;
2966 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002967 uhp = curbuf->b_u_curhead->uh_next.ptr;
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002968 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002969 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002970 uhp = curbuf->b_u_newhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002971
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002972 if (uhp == NULL)
2973 *msgbuf = NUL;
2974 else
Bram Moolenaar52410572019-10-27 05:12:45 +01002975 add_time(msgbuf, sizeof(msgbuf), uhp->uh_time);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002976
Bram Moolenaarb2c03502010-07-02 20:20:09 +02002977#ifdef FEAT_CONCEAL
2978 {
2979 win_T *wp;
2980
2981 FOR_ALL_WINDOWS(wp)
2982 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02002983 if (wp->w_buffer == curbuf && wp->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02002984 redraw_win_later(wp, NOT_VALID);
2985 }
2986 }
2987#endif
2988
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002989 smsg_attr_keep(0, _("%ld %s; %s #%ld %s"),
Bram Moolenaarca003e12006-03-17 23:19:38 +00002990 u_oldcount < 0 ? -u_oldcount : u_oldcount,
Bram Moolenaar89d40322006-08-29 15:30:07 +00002991 _(msgstr),
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002992 did_undo ? _("before") : _("after"),
2993 uhp == NULL ? 0L : uhp->uh_seq,
2994 msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995}
2996
2997/*
2998 * u_sync: stop adding to the current entry list
2999 */
3000 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003001u_sync(
Bram Moolenaare38eab22019-12-05 21:50:01 +01003002 int force) // Also sync when no_u_sync is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003{
Bram Moolenaare38eab22019-12-05 21:50:01 +01003004 // Skip it when already synced or syncing is disabled.
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003005 if (curbuf->b_u_synced || (!force && no_u_sync > 0))
3006 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003008 if (p_imst == IM_ON_THE_SPOT && im_is_preediting())
Bram Moolenaare38eab22019-12-05 21:50:01 +01003009 return; // XIM is busy, don't break an undo sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003011 if (get_undolevel() < 0)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003012 curbuf->b_u_synced = TRUE; // no entries, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 else
3014 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003015 u_getbot(); // compute ue_bot of previous u_save
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 curbuf->b_u_curhead = NULL;
3017 }
3018}
3019
3020/*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003021 * ":undolist": List the leafs of the undo tree
3022 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003023 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003024ex_undolist(exarg_T *eap UNUSED)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003025{
3026 garray_T ga;
3027 u_header_T *uhp;
3028 int mark;
3029 int nomark;
3030 int changes = 1;
3031 int i;
3032
3033 /*
3034 * 1: walk the tree to find all leafs, put the info in "ga".
3035 * 2: sort the lines
3036 * 3: display the list
3037 */
3038 mark = ++lastmark;
3039 nomark = ++lastmark;
3040 ga_init2(&ga, (int)sizeof(char *), 20);
3041
3042 uhp = curbuf->b_u_oldhead;
3043 while (uhp != NULL)
3044 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003045 if (uhp->uh_prev.ptr == NULL && uhp->uh_walk != nomark
Bram Moolenaarca003e12006-03-17 23:19:38 +00003046 && uhp->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003047 {
3048 if (ga_grow(&ga, 1) == FAIL)
3049 break;
Bram Moolenaarea391762018-04-08 13:07:22 +02003050 vim_snprintf((char *)IObuff, IOSIZE, "%6ld %7d ",
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003051 uhp->uh_seq, changes);
Bram Moolenaar52410572019-10-27 05:12:45 +01003052 add_time(IObuff + STRLEN(IObuff), IOSIZE - STRLEN(IObuff),
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003053 uhp->uh_time);
Bram Moolenaara800b422010-06-27 01:15:55 +02003054 if (uhp->uh_save_nr > 0)
3055 {
Bram Moolenaardba01a02010-11-03 19:32:42 +01003056 while (STRLEN(IObuff) < 33)
Bram Moolenaara800b422010-06-27 01:15:55 +02003057 STRCAT(IObuff, " ");
3058 vim_snprintf_add((char *)IObuff, IOSIZE,
3059 " %3ld", uhp->uh_save_nr);
3060 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003061 ((char_u **)(ga.ga_data))[ga.ga_len++] = vim_strsave(IObuff);
3062 }
3063
3064 uhp->uh_walk = mark;
3065
Bram Moolenaare38eab22019-12-05 21:50:01 +01003066 // go down in the tree if we haven't been there
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003067 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != nomark
3068 && uhp->uh_prev.ptr->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003069 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003070 uhp = uhp->uh_prev.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003071 ++changes;
3072 }
3073
Bram Moolenaare38eab22019-12-05 21:50:01 +01003074 // go to alternate branch if we haven't been there
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003075 else if (uhp->uh_alt_next.ptr != NULL
3076 && uhp->uh_alt_next.ptr->uh_walk != nomark
3077 && uhp->uh_alt_next.ptr->uh_walk != mark)
3078 uhp = uhp->uh_alt_next.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003079
Bram Moolenaare38eab22019-12-05 21:50:01 +01003080 // go up in the tree if we haven't been there and we are at the
3081 // start of alternate branches
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003082 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
3083 && uhp->uh_next.ptr->uh_walk != nomark
3084 && uhp->uh_next.ptr->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003085 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003086 uhp = uhp->uh_next.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003087 --changes;
3088 }
3089
3090 else
3091 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003092 // need to backtrack; mark this node as done
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003093 uhp->uh_walk = nomark;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003094 if (uhp->uh_alt_prev.ptr != NULL)
3095 uhp = uhp->uh_alt_prev.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003096 else
3097 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003098 uhp = uhp->uh_next.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003099 --changes;
3100 }
3101 }
3102 }
3103
3104 if (ga.ga_len == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003105 msg(_("Nothing to undo"));
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003106 else
3107 {
3108 sort_strings((char_u **)ga.ga_data, ga.ga_len);
3109
3110 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01003111 msg_puts_attr(_("number changes when saved"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01003112 HL_ATTR(HLF_T));
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003113 for (i = 0; i < ga.ga_len && !got_int; ++i)
3114 {
3115 msg_putchar('\n');
3116 if (got_int)
3117 break;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003118 msg_puts(((char **)ga.ga_data)[i]);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003119 }
3120 msg_end();
3121
3122 ga_clear_strings(&ga);
3123 }
3124}
3125
3126/*
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003127 * ":undojoin": continue adding to the last entry list
3128 */
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003129 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003130ex_undojoin(exarg_T *eap UNUSED)
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003131{
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003132 if (curbuf->b_u_newhead == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003133 return; // nothing changed before
Bram Moolenaar57657d82006-04-21 22:12:41 +00003134 if (curbuf->b_u_curhead != NULL)
3135 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003136 emsg(_("E790: undojoin is not allowed after undo"));
Bram Moolenaar57657d82006-04-21 22:12:41 +00003137 return;
3138 }
3139 if (!curbuf->b_u_synced)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003140 return; // already unsynced
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003141 if (get_undolevel() < 0)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003142 return; // no entries, nothing to do
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003143 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01003144 // Append next change to the last entry
Bram Moolenaar5e4e1b12017-01-17 22:09:45 +01003145 curbuf->b_u_synced = FALSE;
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003146}
3147
3148/*
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003149 * Called after writing or reloading the file and setting b_changed to FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 * Now an undo means that the buffer is modified.
3151 */
3152 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003153u_unchanged(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154{
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003155 u_unch_branch(buf->b_u_oldhead);
3156 buf->b_did_warn = FALSE;
3157}
3158
Bram Moolenaar730cde92010-06-27 05:18:54 +02003159/*
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003160 * After reloading a buffer which was saved for 'undoreload': Find the first
3161 * line that was changed and set the cursor there.
3162 */
3163 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003164u_find_first_changed(void)
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003165{
3166 u_header_T *uhp = curbuf->b_u_newhead;
3167 u_entry_T *uep;
3168 linenr_T lnum;
3169
3170 if (curbuf->b_u_curhead != NULL || uhp == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003171 return; // undid something in an autocmd?
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003172
Bram Moolenaare38eab22019-12-05 21:50:01 +01003173 // Check that the last undo block was for the whole file.
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003174 uep = uhp->uh_entry;
3175 if (uep->ue_top != 0 || uep->ue_bot != 0)
3176 return;
3177
3178 for (lnum = 1; lnum < curbuf->b_ml.ml_line_count
3179 && lnum <= uep->ue_size; ++lnum)
Bram Moolenaarccae4672019-01-04 15:09:57 +01003180 {
3181 char_u *p = ml_get_buf(curbuf, lnum, FALSE);
3182
3183 if (uep->ue_array[lnum - 1].ul_len != curbuf->b_ml.ml_line_len
3184 || memcmp(p, uep->ue_array[lnum - 1].ul_line, uep->ue_array[lnum - 1].ul_len) != 0)
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003185 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003186 CLEAR_POS(&(uhp->uh_cursor));
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003187 uhp->uh_cursor.lnum = lnum;
3188 return;
3189 }
Bram Moolenaarccae4672019-01-04 15:09:57 +01003190 }
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003191 if (curbuf->b_ml.ml_line_count != uep->ue_size)
3192 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003193 // lines added or deleted at the end, put the cursor there
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003194 CLEAR_POS(&(uhp->uh_cursor));
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003195 uhp->uh_cursor.lnum = lnum;
3196 }
3197}
3198
3199/*
Bram Moolenaar730cde92010-06-27 05:18:54 +02003200 * Increase the write count, store it in the last undo header, what would be
3201 * used for "u".
3202 */
3203 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003204u_update_save_nr(buf_T *buf)
Bram Moolenaar730cde92010-06-27 05:18:54 +02003205{
3206 u_header_T *uhp;
3207
3208 ++buf->b_u_save_nr_last;
3209 buf->b_u_save_nr_cur = buf->b_u_save_nr_last;
3210 uhp = buf->b_u_curhead;
3211 if (uhp != NULL)
3212 uhp = uhp->uh_next.ptr;
3213 else
3214 uhp = buf->b_u_newhead;
3215 if (uhp != NULL)
3216 uhp->uh_save_nr = buf->b_u_save_nr_last;
3217}
3218
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003219 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003220u_unch_branch(u_header_T *uhp)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003221{
Bram Moolenaar1e607892006-03-13 22:15:53 +00003222 u_header_T *uh;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003224 for (uh = uhp; uh != NULL; uh = uh->uh_prev.ptr)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003225 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 uh->uh_flags |= UH_CHANGED;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003227 if (uh->uh_alt_next.ptr != NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003228 u_unch_branch(uh->uh_alt_next.ptr); // recursive
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230}
3231
3232/*
3233 * Get pointer to last added entry.
3234 * If it's not valid, give an error message and return NULL.
3235 */
3236 static u_entry_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003237u_get_headentry(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238{
3239 if (curbuf->b_u_newhead == NULL || curbuf->b_u_newhead->uh_entry == NULL)
3240 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003241 iemsg(_("E439: undo list corrupt"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 return NULL;
3243 }
3244 return curbuf->b_u_newhead->uh_entry;
3245}
3246
3247/*
3248 * u_getbot(): compute the line number of the previous u_save
3249 * It is called only when b_u_synced is FALSE.
3250 */
3251 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003252u_getbot(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253{
3254 u_entry_T *uep;
3255 linenr_T extra;
3256
Bram Moolenaare38eab22019-12-05 21:50:01 +01003257 uep = u_get_headentry(); // check for corrupt undo list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 if (uep == NULL)
3259 return;
3260
3261 uep = curbuf->b_u_newhead->uh_getbot_entry;
3262 if (uep != NULL)
3263 {
3264 /*
3265 * the new ue_bot is computed from the number of lines that has been
3266 * inserted (0 - deleted) since calling u_save. This is equal to the
3267 * old line count subtracted from the current line count.
3268 */
3269 extra = curbuf->b_ml.ml_line_count - uep->ue_lcount;
3270 uep->ue_bot = uep->ue_top + uep->ue_size + 1 + extra;
3271 if (uep->ue_bot < 1 || uep->ue_bot > curbuf->b_ml.ml_line_count)
3272 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003273 iemsg(_("E440: undo line missing"));
Bram Moolenaare38eab22019-12-05 21:50:01 +01003274 uep->ue_bot = uep->ue_top + 1; // assume all lines deleted, will
3275 // get all the old lines back
3276 // without deleting the current
3277 // ones
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 }
3279
3280 curbuf->b_u_newhead->uh_getbot_entry = NULL;
3281 }
3282
3283 curbuf->b_u_synced = TRUE;
3284}
3285
3286/*
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003287 * Free one header "uhp" and its entry list and adjust the pointers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 */
3289 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003290u_freeheader(
3291 buf_T *buf,
3292 u_header_T *uhp,
Bram Moolenaare38eab22019-12-05 21:50:01 +01003293 u_header_T **uhpp) // if not NULL reset when freeing this header
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294{
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003295 u_header_T *uhap;
3296
Bram Moolenaare38eab22019-12-05 21:50:01 +01003297 // When there is an alternate redo list free that branch completely,
3298 // because we can never go there.
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003299 if (uhp->uh_alt_next.ptr != NULL)
3300 u_freebranch(buf, uhp->uh_alt_next.ptr, uhpp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003302 if (uhp->uh_alt_prev.ptr != NULL)
3303 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304
Bram Moolenaare38eab22019-12-05 21:50:01 +01003305 // Update the links in the list to remove the header.
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003306 if (uhp->uh_next.ptr == NULL)
3307 buf->b_u_oldhead = uhp->uh_prev.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003309 uhp->uh_next.ptr->uh_prev.ptr = uhp->uh_prev.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003311 if (uhp->uh_prev.ptr == NULL)
3312 buf->b_u_newhead = uhp->uh_next.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003314 for (uhap = uhp->uh_prev.ptr; uhap != NULL;
3315 uhap = uhap->uh_alt_next.ptr)
3316 uhap->uh_next.ptr = uhp->uh_next.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317
Bram Moolenaar1e607892006-03-13 22:15:53 +00003318 u_freeentries(buf, uhp, uhpp);
3319}
3320
3321/*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003322 * Free an alternate branch and any following alternate branches.
Bram Moolenaar1e607892006-03-13 22:15:53 +00003323 */
3324 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003325u_freebranch(
3326 buf_T *buf,
3327 u_header_T *uhp,
Bram Moolenaare38eab22019-12-05 21:50:01 +01003328 u_header_T **uhpp) // if not NULL reset when freeing this header
Bram Moolenaar1e607892006-03-13 22:15:53 +00003329{
3330 u_header_T *tofree, *next;
3331
Bram Moolenaare38eab22019-12-05 21:50:01 +01003332 // If this is the top branch we may need to use u_freeheader() to update
3333 // all the pointers.
Bram Moolenaar07d06772007-11-10 21:51:15 +00003334 if (uhp == buf->b_u_oldhead)
3335 {
Bram Moolenaaraa887322013-11-07 03:04:11 +01003336 while (buf->b_u_oldhead != NULL)
3337 u_freeheader(buf, buf->b_u_oldhead, uhpp);
Bram Moolenaar07d06772007-11-10 21:51:15 +00003338 return;
3339 }
3340
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003341 if (uhp->uh_alt_prev.ptr != NULL)
3342 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +00003343
3344 next = uhp;
3345 while (next != NULL)
3346 {
3347 tofree = next;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003348 if (tofree->uh_alt_next.ptr != NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003349 u_freebranch(buf, tofree->uh_alt_next.ptr, uhpp); // recursive
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003350 next = tofree->uh_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00003351 u_freeentries(buf, tofree, uhpp);
3352 }
3353}
3354
3355/*
3356 * Free all the undo entries for one header and the header itself.
3357 * This means that "uhp" is invalid when returning.
3358 */
3359 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003360u_freeentries(
3361 buf_T *buf,
3362 u_header_T *uhp,
Bram Moolenaare38eab22019-12-05 21:50:01 +01003363 u_header_T **uhpp) // if not NULL reset when freeing this header
Bram Moolenaar1e607892006-03-13 22:15:53 +00003364{
3365 u_entry_T *uep, *nuep;
3366
Bram Moolenaare38eab22019-12-05 21:50:01 +01003367 // Check for pointers to the header that become invalid now.
Bram Moolenaar1e607892006-03-13 22:15:53 +00003368 if (buf->b_u_curhead == uhp)
3369 buf->b_u_curhead = NULL;
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003370 if (buf->b_u_newhead == uhp)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003371 buf->b_u_newhead = NULL; // freeing the newest entry
Bram Moolenaar1e607892006-03-13 22:15:53 +00003372 if (uhpp != NULL && uhp == *uhpp)
3373 *uhpp = NULL;
3374
3375 for (uep = uhp->uh_entry; uep != NULL; uep = nuep)
3376 {
3377 nuep = uep->ue_next;
3378 u_freeentry(uep, uep->ue_size);
3379 }
3380
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003381#ifdef U_DEBUG
3382 uhp->uh_magic = 0;
3383#endif
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003384 vim_free((char_u *)uhp);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003385 --buf->b_u_numhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386}
3387
3388/*
3389 * free entry 'uep' and 'n' lines in uep->ue_array[]
3390 */
3391 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003392u_freeentry(u_entry_T *uep, long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393{
Bram Moolenaar8d343302005-07-12 22:46:17 +00003394 while (n > 0)
Bram Moolenaarccae4672019-01-04 15:09:57 +01003395 vim_free(uep->ue_array[--n].ul_line);
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003396 vim_free((char_u *)uep->ue_array);
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003397#ifdef U_DEBUG
3398 uep->ue_magic = 0;
3399#endif
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003400 vim_free((char_u *)uep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401}
3402
3403/*
3404 * invalidate the undo buffer; called when storage has already been released
3405 */
3406 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003407u_clearall(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408{
3409 buf->b_u_newhead = buf->b_u_oldhead = buf->b_u_curhead = NULL;
3410 buf->b_u_synced = TRUE;
3411 buf->b_u_numhead = 0;
Bram Moolenaarccae4672019-01-04 15:09:57 +01003412 buf->b_u_line_ptr.ul_line = NULL;
3413 buf->b_u_line_ptr.ul_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 buf->b_u_line_lnum = 0;
3415}
3416
3417/*
Bram Moolenaarccae4672019-01-04 15:09:57 +01003418 * Save the line "lnum" for the "U" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003420 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003421u_saveline(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422{
Bram Moolenaare38eab22019-12-05 21:50:01 +01003423 if (lnum == curbuf->b_u_line_lnum) // line is already saved
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 return;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003425 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) // should never happen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 return;
3427 u_clearline();
3428 curbuf->b_u_line_lnum = lnum;
3429 if (curwin->w_cursor.lnum == lnum)
3430 curbuf->b_u_line_colnr = curwin->w_cursor.col;
3431 else
3432 curbuf->b_u_line_colnr = 0;
Bram Moolenaarccae4672019-01-04 15:09:57 +01003433 if (u_save_line(&curbuf->b_u_line_ptr, lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 do_outofmem_msg((long_u)0);
3435}
3436
3437/*
3438 * clear the line saved for the "U" command
3439 * (this is used externally for crossing a line while in insert mode)
3440 */
3441 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003442u_clearline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443{
Bram Moolenaarccae4672019-01-04 15:09:57 +01003444 if (curbuf->b_u_line_ptr.ul_line != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 {
Bram Moolenaarccae4672019-01-04 15:09:57 +01003446 VIM_CLEAR(curbuf->b_u_line_ptr.ul_line);
3447 curbuf->b_u_line_ptr.ul_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 curbuf->b_u_line_lnum = 0;
3449 }
3450}
3451
3452/*
3453 * Implementation of the "U" command.
3454 * Differentiation from vi: "U" can be undone with the next "U".
3455 * We also allow the cursor to be in another line.
Bram Moolenaard04b7502010-07-08 22:27:55 +02003456 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 */
3458 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003459u_undoline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460{
Bram Moolenaarccae4672019-01-04 15:09:57 +01003461 colnr_T t;
3462 undoline_T oldp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463
3464 if (undo_off)
3465 return;
3466
Bram Moolenaarccae4672019-01-04 15:09:57 +01003467 if (curbuf->b_u_line_ptr.ul_line == NULL
Bram Moolenaare3300c82008-02-13 14:21:38 +00003468 || curbuf->b_u_line_lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 {
3470 beep_flush();
3471 return;
3472 }
Bram Moolenaare3300c82008-02-13 14:21:38 +00003473
Bram Moolenaarccae4672019-01-04 15:09:57 +01003474 // first save the line for the 'u' command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 if (u_savecommon(curbuf->b_u_line_lnum - 1,
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003476 curbuf->b_u_line_lnum + 1, (linenr_T)0, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 return;
Bram Moolenaarccae4672019-01-04 15:09:57 +01003478 if (u_save_line(&oldp, curbuf->b_u_line_lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 {
3480 do_outofmem_msg((long_u)0);
3481 return;
3482 }
Bram Moolenaarccae4672019-01-04 15:09:57 +01003483 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 +00003484 changed_bytes(curbuf->b_u_line_lnum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 curbuf->b_u_line_ptr = oldp;
3486
3487 t = curbuf->b_u_line_colnr;
3488 if (curwin->w_cursor.lnum == curbuf->b_u_line_lnum)
3489 curbuf->b_u_line_colnr = curwin->w_cursor.col;
3490 curwin->w_cursor.col = t;
3491 curwin->w_cursor.lnum = curbuf->b_u_line_lnum;
Bram Moolenaare3300c82008-02-13 14:21:38 +00003492 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493}
3494
3495/*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003496 * Free all allocated memory blocks for the buffer 'buf'.
3497 */
3498 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003499u_blockfree(buf_T *buf)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003500{
Bram Moolenaar1e607892006-03-13 22:15:53 +00003501 while (buf->b_u_oldhead != NULL)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003502 u_freeheader(buf, buf->b_u_oldhead, NULL);
Bram Moolenaarccae4672019-01-04 15:09:57 +01003503 vim_free(buf->b_u_line_ptr.ul_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504}
3505
3506/*
3507 * Check if the 'modified' flag is set, or 'ff' has changed (only need to
3508 * check the first character, because it can only be "dos", "unix" or "mac").
3509 * "nofile" and "scratch" type buffers are considered to always be unchanged.
Bram Moolenaarf405c8f2017-12-09 19:51:49 +01003510 * Also considers a buffer changed when a terminal window contains a running
3511 * job.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 */
3513 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003514bufIsChanged(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515{
Bram Moolenaareb44a682017-08-03 22:44:55 +02003516#ifdef FEAT_TERMINAL
3517 if (term_job_running(buf->b_term))
3518 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519#endif
Bram Moolenaarf405c8f2017-12-09 19:51:49 +01003520 return bufIsChangedNotTerm(buf);
3521}
3522
3523/*
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01003524 * Return TRUE if any buffer has changes. Also buffers that are not written.
3525 */
3526 int
3527anyBufIsChanged(void)
3528{
3529 buf_T *buf;
3530
3531 FOR_ALL_BUFFERS(buf)
3532 if (bufIsChanged(buf))
3533 return TRUE;
Bram Moolenaard6c3f1f2019-03-26 00:31:21 +01003534 return FALSE;
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01003535}
3536
3537/*
Bram Moolenaarf405c8f2017-12-09 19:51:49 +01003538 * Like bufIsChanged() but ignoring a terminal window.
3539 */
3540 int
3541bufIsChangedNotTerm(buf_T *buf)
3542{
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003543 // In a "prompt" buffer we do respect 'modified', so that we can control
3544 // closing the window by setting or resetting that option.
3545 return (!bt_dontwrite(buf) || bt_prompt(buf))
Bram Moolenaareb44a682017-08-03 22:44:55 +02003546 && (buf->b_changed || file_ff_differs(buf, TRUE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547}
3548
3549 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003550curbufIsChanged(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551{
Bram Moolenaareb44a682017-08-03 22:44:55 +02003552 return bufIsChanged(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553}
Bram Moolenaara800b422010-06-27 01:15:55 +02003554
3555#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003556
Bram Moolenaara800b422010-06-27 01:15:55 +02003557/*
3558 * For undotree(): Append the list of undo blocks at "first_uhp" to "list".
3559 * Recursive.
3560 */
Bram Moolenaar840d16f2019-09-10 21:27:18 +02003561 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003562u_eval_tree(u_header_T *first_uhp, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02003563{
3564 u_header_T *uhp = first_uhp;
3565 dict_T *dict;
3566
3567 while (uhp != NULL)
3568 {
3569 dict = dict_alloc();
3570 if (dict == NULL)
3571 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003572 dict_add_number(dict, "seq", uhp->uh_seq);
3573 dict_add_number(dict, "time", (long)uhp->uh_time);
Bram Moolenaara800b422010-06-27 01:15:55 +02003574 if (uhp == curbuf->b_u_newhead)
Bram Moolenaare0be1672018-07-08 16:50:37 +02003575 dict_add_number(dict, "newhead", 1);
Bram Moolenaara800b422010-06-27 01:15:55 +02003576 if (uhp == curbuf->b_u_curhead)
Bram Moolenaare0be1672018-07-08 16:50:37 +02003577 dict_add_number(dict, "curhead", 1);
Bram Moolenaara800b422010-06-27 01:15:55 +02003578 if (uhp->uh_save_nr > 0)
Bram Moolenaare0be1672018-07-08 16:50:37 +02003579 dict_add_number(dict, "save", uhp->uh_save_nr);
Bram Moolenaara800b422010-06-27 01:15:55 +02003580
3581 if (uhp->uh_alt_next.ptr != NULL)
3582 {
3583 list_T *alt_list = list_alloc();
3584
3585 if (alt_list != NULL)
3586 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003587 // Recursive call to add alternate undo tree.
Bram Moolenaara800b422010-06-27 01:15:55 +02003588 u_eval_tree(uhp->uh_alt_next.ptr, alt_list);
3589 dict_add_list(dict, "alt", alt_list);
3590 }
3591 }
3592
3593 list_append_dict(list, dict);
3594 uhp = uhp->uh_prev.ptr;
3595 }
3596}
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003597
3598/*
3599 * "undofile(name)" function
3600 */
3601 void
3602f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
3603{
3604 rettv->v_type = VAR_STRING;
3605#ifdef FEAT_PERSISTENT_UNDO
3606 {
3607 char_u *fname = tv_get_string(&argvars[0]);
3608
3609 if (*fname == NUL)
3610 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003611 // If there is no file name there will be no undo file.
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003612 rettv->vval.v_string = NULL;
3613 }
3614 else
3615 {
3616 char_u *ffname = FullName_save(fname, TRUE);
3617
3618 if (ffname != NULL)
3619 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
3620 vim_free(ffname);
3621 }
3622 }
3623#else
3624 rettv->vval.v_string = NULL;
3625#endif
3626}
3627
3628/*
3629 * "undotree()" function
3630 */
3631 void
3632f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
3633{
3634 if (rettv_dict_alloc(rettv) == OK)
3635 {
3636 dict_T *dict = rettv->vval.v_dict;
3637 list_T *list;
3638
3639 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
3640 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
3641 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
3642 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
3643 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
3644 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
3645
3646 list = list_alloc();
3647 if (list != NULL)
3648 {
3649 u_eval_tree(curbuf->b_u_oldhead, list);
3650 dict_add_list(dict, "entries", list);
3651 }
3652 }
3653}
3654
Bram Moolenaara800b422010-06-27 01:15:55 +02003655#endif