blob: 0a015e65b0123f6211b929c61d7ba4b4f584b65b [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * undo.c: multi level undo facility
12 *
13 * The saved lines are stored in a list of lists (one for each buffer):
14 *
15 * b_u_oldhead------------------------------------------------+
16 * |
17 * V
18 * +--------------+ +--------------+ +--------------+
19 * b_u_newhead--->| u_header | | u_header | | u_header |
20 * | uh_next------>| uh_next------>| uh_next---->NULL
21 * NULL<--------uh_prev |<---------uh_prev |<---------uh_prev |
22 * | uh_entry | | uh_entry | | uh_entry |
23 * +--------|-----+ +--------|-----+ +--------|-----+
24 * | | |
25 * V V V
26 * +--------------+ +--------------+ +--------------+
27 * | u_entry | | u_entry | | u_entry |
28 * | ue_next | | ue_next | | ue_next |
29 * +--------|-----+ +--------|-----+ +--------|-----+
30 * | | |
31 * V V V
32 * +--------------+ NULL NULL
33 * | u_entry |
34 * | ue_next |
35 * +--------|-----+
36 * |
37 * V
38 * etc.
39 *
40 * Each u_entry list contains the information for one undo or redo.
41 * curbuf->b_u_curhead points to the header of the last undo (the next redo),
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +000042 * or is NULL if nothing has been undone (end of the branch).
Bram Moolenaar071d4272004-06-13 20:20:40 +000043 *
Bram Moolenaar1e607892006-03-13 22:15:53 +000044 * For keeping alternate undo/redo branches the uh_alt field is used. Thus at
45 * each point in the list a branch may appear for an alternate to redo. The
46 * uh_seq field is numbered sequentially to be able to find a newer or older
47 * branch.
48 *
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +000049 * +---------------+ +---------------+
50 * b_u_oldhead --->| u_header | | u_header |
51 * | uh_alt_next ---->| uh_alt_next ----> NULL
52 * NULL <----- uh_alt_prev |<------ uh_alt_prev |
53 * | uh_prev | | uh_prev |
54 * +-----|---------+ +-----|---------+
55 * | |
56 * V V
57 * +---------------+ +---------------+
58 * | u_header | | u_header |
59 * | uh_alt_next | | uh_alt_next |
60 * b_u_newhead --->| uh_alt_prev | | uh_alt_prev |
61 * | uh_prev | | uh_prev |
62 * +-----|---------+ +-----|---------+
63 * | |
64 * V V
65 * NULL +---------------+ +---------------+
66 * | u_header | | u_header |
67 * | uh_alt_next ---->| uh_alt_next |
68 * | uh_alt_prev |<------ uh_alt_prev |
69 * | uh_prev | | uh_prev |
70 * +-----|---------+ +-----|---------+
71 * | |
72 * etc. etc.
73 *
74 *
Bram Moolenaarf05e3b02010-05-29 15:40:47 +020075 * All data is allocated and will all be freed when the buffer is unloaded.
Bram Moolenaar071d4272004-06-13 20:20:40 +000076 */
77
Bram Moolenaarfecb6602007-10-01 20:54:15 +000078/* Uncomment the next line for including the u_check() function. This warns
79 * for errors in the debug information. */
80/* #define U_DEBUG 1 */
81#define UH_MAGIC 0x18dade /* value for uh_magic when in use */
82#define UE_MAGIC 0xabc123 /* value for ue_magic when in use */
83
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020084/* Size of buffer used for encryption. */
85#define CRYPT_BUF_SIZE 8192
86
Bram Moolenaar071d4272004-06-13 20:20:40 +000087#include "vim.h"
88
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020089/* Structure passed around between functions.
90 * Avoids passing cryptstate_T when encryption not available. */
91typedef struct {
92 buf_T *bi_buf;
93 FILE *bi_fp;
94#ifdef FEAT_CRYPT
95 cryptstate_T *bi_state;
96 char_u *bi_buffer; /* CRYPT_BUF_SIZE, NULL when not buffering */
97 size_t bi_used; /* bytes written to/read from bi_buffer */
98 size_t bi_avail; /* bytes available in bi_buffer */
99#endif
100} bufinfo_T;
101
102
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100103static void u_unch_branch(u_header_T *uhp);
104static u_entry_T *u_get_headentry(void);
105static void u_getbot(void);
106static void u_doit(int count);
107static void u_undoredo(int undo);
108static void u_undo_end(int did_undo, int absolute);
109static void u_add_time(char_u *buf, size_t buflen, time_t tt);
110static void u_freeheader(buf_T *buf, u_header_T *uhp, u_header_T **uhpp);
111static void u_freebranch(buf_T *buf, u_header_T *uhp, u_header_T **uhpp);
112static void u_freeentries(buf_T *buf, u_header_T *uhp, u_header_T **uhpp);
113static void u_freeentry(u_entry_T *, long);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200114#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar1d6fbe62016-02-19 22:46:34 +0100115# ifdef FEAT_CRYPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100116static int undo_flush(bufinfo_T *bi);
Bram Moolenaar1d6fbe62016-02-19 22:46:34 +0100117# endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100118static int undo_read(bufinfo_T *bi, char_u *buffer, size_t size);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100119static int serialize_uep(bufinfo_T *bi, u_entry_T *uep);
120static u_entry_T *unserialize_uep(bufinfo_T *bi, int *error, char_u *file_name);
121static void serialize_pos(bufinfo_T *bi, pos_T pos);
122static void unserialize_pos(bufinfo_T *bi, pos_T *pos);
123static void serialize_visualinfo(bufinfo_T *bi, visualinfo_T *info);
124static void unserialize_visualinfo(bufinfo_T *bi, visualinfo_T *info);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200125#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126
Bram Moolenaarf05e3b02010-05-29 15:40:47 +0200127#define U_ALLOC_LINE(size) lalloc((long_u)(size), FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128
Bram Moolenaar730cde92010-06-27 05:18:54 +0200129/* used in undo_end() to report number of added and deleted lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130static long u_newcount, u_oldcount;
131
132/*
133 * When 'u' flag included in 'cpoptions', we behave like vi. Need to remember
134 * the action that "u" should do.
135 */
136static int undo_undoes = FALSE;
137
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200138static int lastmark = 0;
139
Bram Moolenaar9db58062010-05-29 20:33:07 +0200140#if defined(U_DEBUG) || defined(PROTO)
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000141/*
142 * Check the undo structures for being valid. Print a warning when something
143 * looks wrong.
144 */
145static int seen_b_u_curhead;
146static int seen_b_u_newhead;
147static int header_count;
148
149 static void
150u_check_tree(u_header_T *uhp,
151 u_header_T *exp_uh_next,
152 u_header_T *exp_uh_alt_prev)
153{
154 u_entry_T *uep;
155
156 if (uhp == NULL)
157 return;
158 ++header_count;
159 if (uhp == curbuf->b_u_curhead && ++seen_b_u_curhead > 1)
160 {
161 EMSG("b_u_curhead found twice (looping?)");
162 return;
163 }
164 if (uhp == curbuf->b_u_newhead && ++seen_b_u_newhead > 1)
165 {
166 EMSG("b_u_newhead found twice (looping?)");
167 return;
168 }
169
170 if (uhp->uh_magic != UH_MAGIC)
171 EMSG("uh_magic wrong (may be using freed memory)");
172 else
173 {
174 /* Check pointers back are correct. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200175 if (uhp->uh_next.ptr != exp_uh_next)
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000176 {
177 EMSG("uh_next wrong");
178 smsg((char_u *)"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 {
183 EMSG("uh_alt_prev wrong");
184 smsg((char_u *)"expected: 0x%x, actual: 0x%x",
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200185 exp_uh_alt_prev, uhp->uh_alt_prev.ptr);
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000186 }
187
188 /* Check the undo tree at this header. */
189 for (uep = uhp->uh_entry; uep != NULL; uep = uep->ue_next)
190 {
191 if (uep->ue_magic != UE_MAGIC)
192 {
193 EMSG("ue_magic wrong (may be using freed memory)");
194 break;
195 }
196 }
197
198 /* Check the next alt tree. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200199 u_check_tree(uhp->uh_alt_next.ptr, uhp->uh_next.ptr, uhp);
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000200
201 /* Check the next header in this branch. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200202 u_check_tree(uhp->uh_prev.ptr, uhp, NULL);
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000203 }
204}
205
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200206 static void
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000207u_check(int newhead_may_be_NULL)
208{
209 seen_b_u_newhead = 0;
210 seen_b_u_curhead = 0;
211 header_count = 0;
212
213 u_check_tree(curbuf->b_u_oldhead, NULL, NULL);
214
215 if (seen_b_u_newhead == 0 && curbuf->b_u_oldhead != NULL
216 && !(newhead_may_be_NULL && curbuf->b_u_newhead == NULL))
217 EMSGN("b_u_newhead invalid: 0x%x", curbuf->b_u_newhead);
218 if (curbuf->b_u_curhead != NULL && seen_b_u_curhead == 0)
219 EMSGN("b_u_curhead invalid: 0x%x", curbuf->b_u_curhead);
220 if (header_count != curbuf->b_u_numhead)
221 {
222 EMSG("b_u_numhead invalid");
223 smsg((char_u *)"expected: %ld, actual: %ld",
224 (long)header_count, (long)curbuf->b_u_numhead);
225 }
226}
227#endif
228
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000230 * Save the current line for both the "u" and "U" command.
Bram Moolenaar687a29c2013-04-15 15:47:12 +0200231 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000232 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 */
234 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100235u_save_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236{
237 return (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
238 (linenr_T)(curwin->w_cursor.lnum + 1)));
239}
240
241/*
242 * Save the lines between "top" and "bot" for both the "u" and "U" command.
243 * "top" may be 0 and bot may be curbuf->b_ml.ml_line_count + 1.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200244 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 * Returns FAIL when lines could not be saved, OK otherwise.
246 */
247 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100248u_save(linenr_T top, linenr_T bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249{
250 if (undo_off)
251 return OK;
252
Bram Moolenaarefc81332018-07-13 16:31:19 +0200253 if (top >= bot || bot > curbuf->b_ml.ml_line_count + 1)
254 return FAIL; // rely on caller to give an error message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255
256 if (top + 2 == bot)
257 u_saveline((linenr_T)(top + 1));
258
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200259 return (u_savecommon(top, bot, (linenr_T)0, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260}
261
262/*
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200263 * Save the line "lnum" (used by ":s" and "~" command).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 * The line is replaced, so the new bottom line is lnum + 1.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200265 * Careful: may trigger autocommands that reload the buffer.
266 * Returns FAIL when lines could not be saved, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 */
268 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100269u_savesub(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270{
271 if (undo_off)
272 return OK;
273
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200274 return (u_savecommon(lnum - 1, lnum + 1, lnum + 1, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275}
276
277/*
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200278 * A new line is inserted before line "lnum" (used by :s command).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279 * The line is inserted, so the new bottom line is lnum + 1.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200280 * Careful: may trigger autocommands that reload the buffer.
281 * Returns FAIL when lines could not be saved, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282 */
283 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100284u_inssub(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285{
286 if (undo_off)
287 return OK;
288
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200289 return (u_savecommon(lnum - 1, lnum, lnum + 1, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290}
291
292/*
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200293 * Save the lines "lnum" - "lnum" + nlines (used by delete command).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 * The lines are deleted, so the new bottom line is lnum, unless the buffer
295 * becomes empty.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200296 * Careful: may trigger autocommands that reload the buffer.
297 * Returns FAIL when lines could not be saved, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 */
299 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100300u_savedel(linenr_T lnum, long nlines)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301{
302 if (undo_off)
303 return OK;
304
305 return (u_savecommon(lnum - 1, lnum + nlines,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200306 nlines == curbuf->b_ml.ml_line_count ? 2 : lnum, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307}
308
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000309/*
310 * Return TRUE when undo is allowed. Otherwise give an error message and
311 * return FALSE.
312 */
Bram Moolenaarce6ef252006-07-12 19:49:41 +0000313 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100314undo_allowed(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000315{
316 /* Don't allow changes when 'modifiable' is off. */
317 if (!curbuf->b_p_ma)
318 {
319 EMSG(_(e_modifiable));
320 return FALSE;
321 }
322
323#ifdef HAVE_SANDBOX
324 /* In the sandbox it's not allowed to change the text. */
325 if (sandbox != 0)
326 {
327 EMSG(_(e_sandbox));
328 return FALSE;
329 }
330#endif
331
332 /* Don't allow changes in the buffer while editing the cmdline. The
333 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000334 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000335 {
336 EMSG(_(e_secure));
337 return FALSE;
338 }
339
340 return TRUE;
341}
342
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200343/*
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100344 * Get the undolevle value for the current buffer.
345 */
346 static long
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100347get_undolevel(void)
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100348{
349 if (curbuf->b_p_ul == NO_LOCAL_UNDOLEVEL)
350 return p_ul;
351 return curbuf->b_p_ul;
352}
353
354/*
Bram Moolenaarccae4672019-01-04 15:09:57 +0100355 * u_save_line(): save an allocated copy of line "lnum" into "ul".
356 * Returns FAIL when out of memory.
357 */
358 static int
359u_save_line(undoline_T *ul, linenr_T lnum)
360{
361 char_u *line = ml_get(lnum);
362
363 if (curbuf->b_ml.ml_line_len == 0)
364 {
365 ul->ul_len = 1;
366 ul->ul_line = vim_strsave((char_u *)"");
367 }
368 else
369 {
370 ul->ul_len = curbuf->b_ml.ml_line_len;
371 ul->ul_line = vim_memsave(line, ul->ul_len);
372 }
373 return ul->ul_line == NULL ? FAIL : OK;
374}
375
376/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200377 * Common code for various ways to save text before a change.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200378 * "top" is the line above the first changed line.
379 * "bot" is the line below the last changed line.
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200380 * "newbot" is the new bottom line. Use zero when not known.
381 * "reload" is TRUE when saving for a buffer reload.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200382 * Careful: may trigger autocommands that reload the buffer.
383 * Returns FAIL when lines could not be saved, OK otherwise.
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200384 */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200385 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100386u_savecommon(
387 linenr_T top,
388 linenr_T bot,
389 linenr_T newbot,
390 int reload)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391{
Bram Moolenaar1e607892006-03-13 22:15:53 +0000392 linenr_T lnum;
393 long i;
394 u_header_T *uhp;
395 u_header_T *old_curhead;
396 u_entry_T *uep;
397 u_entry_T *prev_uep;
398 long size;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200400 if (!reload)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 {
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200402 /* When making changes is not allowed return FAIL. It's a crude way
403 * to make all change commands fail. */
404 if (!undo_allowed())
Bram Moolenaar009b2592004-10-24 19:18:58 +0000405 return FAIL;
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200406
407#ifdef FEAT_NETBEANS_INTG
408 /*
409 * Netbeans defines areas that cannot be modified. Bail out here when
410 * trying to change text in a guarded area.
411 */
412 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +0000413 {
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200414 if (netbeans_is_guarded(top, bot))
415 {
416 EMSG(_(e_guarded));
417 return FAIL;
418 }
419 if (curbuf->b_p_ro)
420 {
421 EMSG(_(e_nbreadonly));
422 return FAIL;
423 }
Bram Moolenaar009b2592004-10-24 19:18:58 +0000424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200426#ifdef FEAT_TERMINAL
427 /* A change in a terminal buffer removes the highlighting. */
428 term_change_in_curbuf();
429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200431 /*
432 * Saving text for undo means we are going to make a change. Give a
433 * warning for a read-only file before making the change, so that the
434 * FileChangedRO event can replace the buffer with a read-write version
435 * (e.g., obtained from a source control system).
436 */
437 change_warning(0);
438 if (bot > curbuf->b_ml.ml_line_count + 1)
439 {
440 /* This happens when the FileChangedRO autocommand changes the
441 * file in a way it becomes shorter. */
Bram Moolenaarb4d587c2014-01-23 18:12:49 +0100442 EMSG(_("E881: Line count changed unexpectedly"));
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200443 return FAIL;
444 }
Bram Moolenaard04b7502010-07-08 22:27:55 +0200445 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200446
447#ifdef U_DEBUG
448 u_check(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449#endif
450
451 size = bot - top - 1;
452
453 /*
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200454 * If curbuf->b_u_synced == TRUE make a new header.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 */
456 if (curbuf->b_u_synced)
457 {
458#ifdef FEAT_JUMPLIST
459 /* Need to create new entry in b_changelist. */
460 curbuf->b_new_change = TRUE;
461#endif
462
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100463 if (get_undolevel() >= 0)
Bram Moolenaar1e607892006-03-13 22:15:53 +0000464 {
465 /*
466 * Make a new header entry. Do this first so that we don't mess
467 * up the undo info when out of memory.
468 */
Bram Moolenaarf05e3b02010-05-29 15:40:47 +0200469 uhp = (u_header_T *)U_ALLOC_LINE(sizeof(u_header_T));
Bram Moolenaar1e607892006-03-13 22:15:53 +0000470 if (uhp == NULL)
471 goto nomem;
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000472#ifdef U_DEBUG
473 uhp->uh_magic = UH_MAGIC;
474#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +0000475 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000476 else
477 uhp = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000478
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 /*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000480 * If we undid more than we redid, move the entry lists before and
481 * including curbuf->b_u_curhead to an alternate branch.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 */
Bram Moolenaar1e607892006-03-13 22:15:53 +0000483 old_curhead = curbuf->b_u_curhead;
484 if (old_curhead != NULL)
485 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200486 curbuf->b_u_newhead = old_curhead->uh_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000487 curbuf->b_u_curhead = NULL;
488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489
490 /*
491 * free headers to keep the size right
492 */
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100493 while (curbuf->b_u_numhead > get_undolevel()
494 && curbuf->b_u_oldhead != NULL)
Bram Moolenaar1e607892006-03-13 22:15:53 +0000495 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000496 u_header_T *uhfree = curbuf->b_u_oldhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000497
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000498 if (uhfree == old_curhead)
499 /* Can't reconnect the branch, delete all of it. */
500 u_freebranch(curbuf, uhfree, &old_curhead);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200501 else if (uhfree->uh_alt_next.ptr == NULL)
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000502 /* There is no branch, only free one header. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000503 u_freeheader(curbuf, uhfree, &old_curhead);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000504 else
505 {
506 /* Free the oldest alternate branch as a whole. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200507 while (uhfree->uh_alt_next.ptr != NULL)
508 uhfree = uhfree->uh_alt_next.ptr;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000509 u_freebranch(curbuf, uhfree, &old_curhead);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000510 }
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000511#ifdef U_DEBUG
512 u_check(TRUE);
513#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +0000514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000516 if (uhp == NULL) /* no undo at all */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 {
Bram Moolenaar1e607892006-03-13 22:15:53 +0000518 if (old_curhead != NULL)
519 u_freebranch(curbuf, old_curhead, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 curbuf->b_u_synced = FALSE;
521 return OK;
522 }
523
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200524 uhp->uh_prev.ptr = NULL;
525 uhp->uh_next.ptr = curbuf->b_u_newhead;
526 uhp->uh_alt_next.ptr = old_curhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000527 if (old_curhead != NULL)
528 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200529 uhp->uh_alt_prev.ptr = old_curhead->uh_alt_prev.ptr;
530 if (uhp->uh_alt_prev.ptr != NULL)
531 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = uhp;
532 old_curhead->uh_alt_prev.ptr = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000533 if (curbuf->b_u_oldhead == old_curhead)
534 curbuf->b_u_oldhead = uhp;
535 }
Bram Moolenaar89ed3df2007-01-09 19:23:12 +0000536 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200537 uhp->uh_alt_prev.ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 if (curbuf->b_u_newhead != NULL)
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200539 curbuf->b_u_newhead->uh_prev.ptr = uhp;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000540
Bram Moolenaarca003e12006-03-17 23:19:38 +0000541 uhp->uh_seq = ++curbuf->b_u_seq_last;
542 curbuf->b_u_seq_cur = uhp->uh_seq;
Bram Moolenaar170b10b2016-07-29 16:15:27 +0200543 uhp->uh_time = vim_time();
Bram Moolenaara800b422010-06-27 01:15:55 +0200544 uhp->uh_save_nr = 0;
545 curbuf->b_u_time_cur = uhp->uh_time + 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000546
Bram Moolenaar1e607892006-03-13 22:15:53 +0000547 uhp->uh_walk = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 uhp->uh_entry = NULL;
549 uhp->uh_getbot_entry = NULL;
550 uhp->uh_cursor = curwin->w_cursor; /* save cursor pos. for undo */
551#ifdef FEAT_VIRTUALEDIT
552 if (virtual_active() && curwin->w_cursor.coladd > 0)
553 uhp->uh_cursor_vcol = getviscol();
554 else
555 uhp->uh_cursor_vcol = -1;
556#endif
557
558 /* save changed and buffer empty flag for undo */
559 uhp->uh_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
560 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
561
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000562 /* save named marks and Visual marks for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 mch_memmove(uhp->uh_namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000564 uhp->uh_visual = curbuf->b_visual;
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000565
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 curbuf->b_u_newhead = uhp;
567 if (curbuf->b_u_oldhead == NULL)
568 curbuf->b_u_oldhead = uhp;
569 ++curbuf->b_u_numhead;
570 }
571 else
572 {
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100573 if (get_undolevel() < 0) /* no undo at all */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 return OK;
575
576 /*
577 * When saving a single line, and it has been saved just before, it
578 * doesn't make sense saving it again. Saves a lot of memory when
579 * making lots of changes inside the same line.
580 * This is only possible if the previous change didn't increase or
581 * decrease the number of lines.
582 * Check the ten last changes. More doesn't make sense and takes too
583 * long.
584 */
585 if (size == 1)
586 {
587 uep = u_get_headentry();
588 prev_uep = NULL;
589 for (i = 0; i < 10; ++i)
590 {
591 if (uep == NULL)
592 break;
593
594 /* If lines have been inserted/deleted we give up.
595 * Also when the line was included in a multi-line save. */
596 if ((curbuf->b_u_newhead->uh_getbot_entry != uep
597 ? (uep->ue_top + uep->ue_size + 1
598 != (uep->ue_bot == 0
599 ? curbuf->b_ml.ml_line_count + 1
600 : uep->ue_bot))
601 : uep->ue_lcount != curbuf->b_ml.ml_line_count)
602 || (uep->ue_size > 1
603 && top >= uep->ue_top
604 && top + 2 <= uep->ue_top + uep->ue_size + 1))
605 break;
606
607 /* If it's the same line we can skip saving it again. */
608 if (uep->ue_size == 1 && uep->ue_top == top)
609 {
610 if (i > 0)
611 {
612 /* It's not the last entry: get ue_bot for the last
613 * entry now. Following deleted/inserted lines go to
614 * the re-used entry. */
615 u_getbot();
616 curbuf->b_u_synced = FALSE;
617
618 /* Move the found entry to become the last entry. The
619 * order of undo/redo doesn't matter for the entries
620 * we move it over, since they don't change the line
621 * count and don't include this line. It does matter
622 * for the found entry if the line count is changed by
623 * the executed command. */
624 prev_uep->ue_next = uep->ue_next;
625 uep->ue_next = curbuf->b_u_newhead->uh_entry;
626 curbuf->b_u_newhead->uh_entry = uep;
627 }
628
629 /* The executed command may change the line count. */
630 if (newbot != 0)
631 uep->ue_bot = newbot;
632 else if (bot > curbuf->b_ml.ml_line_count)
633 uep->ue_bot = 0;
634 else
635 {
636 uep->ue_lcount = curbuf->b_ml.ml_line_count;
637 curbuf->b_u_newhead->uh_getbot_entry = uep;
638 }
639 return OK;
640 }
641 prev_uep = uep;
642 uep = uep->ue_next;
643 }
644 }
645
646 /* find line number for ue_bot for previous u_save() */
647 u_getbot();
648 }
649
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200650#if !defined(UNIX) && !defined(WIN32)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100652 * With Amiga we can't handle big undo's, because
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 * then u_alloc_line would have to allocate a block larger than 32K
654 */
655 if (size >= 8000)
656 goto nomem;
657#endif
658
659 /*
660 * add lines in front of entry list
661 */
Bram Moolenaarf05e3b02010-05-29 15:40:47 +0200662 uep = (u_entry_T *)U_ALLOC_LINE(sizeof(u_entry_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 if (uep == NULL)
664 goto nomem;
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200665 vim_memset(uep, 0, sizeof(u_entry_T));
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000666#ifdef U_DEBUG
667 uep->ue_magic = UE_MAGIC;
668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669
670 uep->ue_size = size;
671 uep->ue_top = top;
672 if (newbot != 0)
673 uep->ue_bot = newbot;
674 /*
675 * Use 0 for ue_bot if bot is below last line.
676 * Otherwise we have to compute ue_bot later.
677 */
678 else if (bot > curbuf->b_ml.ml_line_count)
679 uep->ue_bot = 0;
680 else
681 {
682 uep->ue_lcount = curbuf->b_ml.ml_line_count;
683 curbuf->b_u_newhead->uh_getbot_entry = uep;
684 }
685
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000686 if (size > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 {
Bram Moolenaarccae4672019-01-04 15:09:57 +0100688 if ((uep->ue_array = (undoline_T *)U_ALLOC_LINE(
689 sizeof(undoline_T) * size)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 {
691 u_freeentry(uep, 0L);
692 goto nomem;
693 }
694 for (i = 0, lnum = top + 1; i < size; ++i)
695 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000696 fast_breakcheck();
697 if (got_int)
698 {
699 u_freeentry(uep, i);
700 return FAIL;
701 }
Bram Moolenaarccae4672019-01-04 15:09:57 +0100702 if (u_save_line(&uep->ue_array[i], lnum++) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 {
704 u_freeentry(uep, i);
705 goto nomem;
706 }
707 }
708 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000709 else
710 uep->ue_array = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 uep->ue_next = curbuf->b_u_newhead->uh_entry;
712 curbuf->b_u_newhead->uh_entry = uep;
713 curbuf->b_u_synced = FALSE;
714 undo_undoes = FALSE;
715
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000716#ifdef U_DEBUG
717 u_check(FALSE);
718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 return OK;
720
721nomem:
722 msg_silent = 0; /* must display the prompt */
723 if (ask_yesno((char_u *)_("No undo possible; continue anyway"), TRUE)
724 == 'y')
725 {
726 undo_off = TRUE; /* will be reset when character typed */
727 return OK;
728 }
729 do_outofmem_msg((long_u)0);
730 return FAIL;
731}
732
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200733#if defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200734
Bram Moolenaar9db58062010-05-29 20:33:07 +0200735# define UF_START_MAGIC "Vim\237UnDo\345" /* magic at start of undofile */
736# define UF_START_MAGIC_LEN 9
737# define UF_HEADER_MAGIC 0x5fd0 /* magic at start of header */
738# define UF_HEADER_END_MAGIC 0xe7aa /* magic after last header */
739# define UF_ENTRY_MAGIC 0xf518 /* magic at start of entry */
740# define UF_ENTRY_END_MAGIC 0x3581 /* magic after last entry */
Bram Moolenaara800b422010-06-27 01:15:55 +0200741# define UF_VERSION 2 /* 2-byte undofile version number */
Bram Moolenaara800b422010-06-27 01:15:55 +0200742# define UF_VERSION_CRYPT 0x8002 /* idem, encrypted */
743
744/* extra fields for header */
745# define UF_LAST_SAVE_NR 1
746
747/* extra fields for uhp */
748# define UHP_SAVE_NR 1
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200749
Bram Moolenaarcdf04202010-05-29 15:11:47 +0200750static char_u e_not_open[] = N_("E828: Cannot open undo file for writing: %s");
Bram Moolenaarcdf04202010-05-29 15:11:47 +0200751
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200752/*
753 * Compute the hash for the current buffer text into hash[UNDO_HASH_SIZE].
754 */
755 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100756u_compute_hash(char_u *hash)
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200757{
758 context_sha256_T ctx;
759 linenr_T lnum;
760 char_u *p;
761
762 sha256_start(&ctx);
Bram Moolenaarae7ba982011-12-08 15:14:09 +0100763 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200764 {
765 p = ml_get(lnum);
Bram Moolenaar442b4222010-05-24 21:34:22 +0200766 sha256_update(&ctx, p, (UINT32_T)(STRLEN(p) + 1));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200767 }
768 sha256_finish(&ctx, hash);
769}
770
771/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200772 * Return an allocated string of the full path of the target undofile.
773 * When "reading" is TRUE find the file to read, go over all directories in
774 * 'undodir'.
775 * When "reading" is FALSE use the first name where the directory exists.
Bram Moolenaar9db58062010-05-29 20:33:07 +0200776 * Returns NULL when there is no place to write or no file to read.
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200777 */
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200778 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100779u_get_undo_file_name(char_u *buf_ffname, int reading)
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200780{
781 char_u *dirp;
782 char_u dir_name[IOSIZE + 1];
783 char_u *munged_name = NULL;
784 char_u *undo_file_name = NULL;
785 int dir_len;
786 char_u *p;
Bram Moolenaar8767f522016-07-01 17:17:39 +0200787 stat_T st;
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200788 char_u *ffname = buf_ffname;
789#ifdef HAVE_READLINK
790 char_u fname_buf[MAXPATHL];
791#endif
792
793 if (ffname == NULL)
794 return NULL;
795
796#ifdef HAVE_READLINK
797 /* Expand symlink in the file name, so that we put the undo file with the
798 * actual file instead of with the symlink. */
799 if (resolve_symlink(ffname, fname_buf) == OK)
800 ffname = fname_buf;
801#endif
802
803 /* Loop over 'undodir'. When reading find the first file that exists.
804 * When not reading use the first directory that exists or ".". */
805 dirp = p_udir;
806 while (*dirp != NUL)
807 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200808 dir_len = copy_option_part(&dirp, dir_name, IOSIZE, ",");
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200809 if (dir_len == 1 && dir_name[0] == '.')
810 {
811 /* Use same directory as the ffname,
812 * "dir/name" -> "dir/.name.un~" */
Bram Moolenaar442b4222010-05-24 21:34:22 +0200813 undo_file_name = vim_strnsave(ffname, (int)(STRLEN(ffname) + 5));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200814 if (undo_file_name == NULL)
815 break;
816 p = gettail(undo_file_name);
Bram Moolenaar206f0112014-03-12 16:51:55 +0100817#ifdef VMS
818 /* VMS can not handle more than one dot in the filenames
819 * use "dir/name" -> "dir/_un_name" - add _un_
820 * at the beginning to keep the extension */
821 mch_memmove(p + 4, p, STRLEN(p) + 1);
822 mch_memmove(p, "_un_", 4);
823
824#else
825 /* Use same directory as the ffname,
826 * "dir/name" -> "dir/.name.un~" */
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200827 mch_memmove(p + 1, p, STRLEN(p) + 1);
828 *p = '.';
829 STRCAT(p, ".un~");
Bram Moolenaar206f0112014-03-12 16:51:55 +0100830#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200831 }
832 else
833 {
834 dir_name[dir_len] = NUL;
835 if (mch_isdir(dir_name))
836 {
837 if (munged_name == NULL)
838 {
839 munged_name = vim_strsave(ffname);
840 if (munged_name == NULL)
841 return NULL;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100842 for (p = munged_name; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200843 if (vim_ispathsep(*p))
844 *p = '%';
845 }
846 undo_file_name = concat_fnames(dir_name, munged_name, TRUE);
847 }
848 }
849
850 /* When reading check if the file exists. */
851 if (undo_file_name != NULL && (!reading
852 || mch_stat((char *)undo_file_name, &st) >= 0))
853 break;
Bram Moolenaard23a8232018-02-10 18:45:26 +0100854 VIM_CLEAR(undo_file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200855 }
856
857 vim_free(munged_name);
858 return undo_file_name;
859}
860
Bram Moolenaar9db58062010-05-29 20:33:07 +0200861 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100862corruption_error(char *mesg, char_u *file_name)
Bram Moolenaar9db58062010-05-29 20:33:07 +0200863{
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200864 EMSG3(_("E825: Corrupted undo file (%s): %s"), mesg, file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +0200865}
866
867 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100868u_free_uhp(u_header_T *uhp)
Bram Moolenaar9db58062010-05-29 20:33:07 +0200869{
870 u_entry_T *nuep;
871 u_entry_T *uep;
872
873 uep = uhp->uh_entry;
874 while (uep != NULL)
875 {
876 nuep = uep->ue_next;
877 u_freeentry(uep, uep->ue_size);
878 uep = nuep;
879 }
880 vim_free(uhp);
881}
882
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200883/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200884 * Write a sequence of bytes to the undo file.
885 * Buffers and encrypts as needed.
886 * Returns OK or FAIL.
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200887 */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200888 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100889undo_write(bufinfo_T *bi, char_u *ptr, size_t len)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200890{
891#ifdef FEAT_CRYPT
892 if (bi->bi_buffer != NULL)
893 {
894 size_t len_todo = len;
895 char_u *p = ptr;
896
897 while (bi->bi_used + len_todo >= CRYPT_BUF_SIZE)
898 {
899 size_t n = CRYPT_BUF_SIZE - bi->bi_used;
900
901 mch_memmove(bi->bi_buffer + bi->bi_used, p, n);
902 len_todo -= n;
903 p += n;
904 bi->bi_used = CRYPT_BUF_SIZE;
905 if (undo_flush(bi) == FAIL)
906 return FAIL;
907 }
908 if (len_todo > 0)
909 {
910 mch_memmove(bi->bi_buffer + bi->bi_used, p, len_todo);
911 bi->bi_used += len_todo;
912 }
913 return OK;
914 }
915#endif
916 if (fwrite(ptr, len, (size_t)1, bi->bi_fp) != 1)
917 return FAIL;
918 return OK;
919}
920
921#ifdef FEAT_CRYPT
922 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100923undo_flush(bufinfo_T *bi)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200924{
Bram Moolenaar829aa642017-08-23 22:32:35 +0200925 if (bi->bi_buffer != NULL && bi->bi_state != NULL && bi->bi_used > 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200926 {
927 crypt_encode_inplace(bi->bi_state, bi->bi_buffer, bi->bi_used);
928 if (fwrite(bi->bi_buffer, bi->bi_used, (size_t)1, bi->bi_fp) != 1)
929 return FAIL;
930 bi->bi_used = 0;
931 }
932 return OK;
933}
934#endif
935
936/*
937 * Write "ptr[len]" and crypt the bytes when needed.
938 * Returns OK or FAIL.
939 */
940 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100941fwrite_crypt(bufinfo_T *bi, char_u *ptr, size_t len)
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200942{
943#ifdef FEAT_CRYPT
944 char_u *copy;
945 char_u small_buf[100];
946 size_t i;
947
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200948 if (bi->bi_state != NULL && bi->bi_buffer == NULL)
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200949 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200950 /* crypting every piece of text separately */
951 if (len < 100)
952 copy = small_buf; /* no malloc()/free() for short strings */
953 else
954 {
955 copy = lalloc(len, FALSE);
956 if (copy == NULL)
957 return 0;
958 }
959 crypt_encode(bi->bi_state, ptr, len, copy);
960 i = fwrite(copy, len, (size_t)1, bi->bi_fp);
961 if (copy != small_buf)
962 vim_free(copy);
963 return i == 1 ? OK : FAIL;
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200964 }
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200965#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200966 return undo_write(bi, ptr, len);
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200967}
968
969/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200970 * Write a number, MSB first, in "len" bytes.
971 * Must match with undo_read_?c() functions.
972 * Returns OK or FAIL.
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200973 */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200974 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100975undo_write_bytes(bufinfo_T *bi, long_u nr, int len)
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200976{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200977 char_u buf[8];
978 int i;
979 int bufi = 0;
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200980
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200981 for (i = len - 1; i >= 0; --i)
Bram Moolenaar9b8f0212014-08-13 22:05:53 +0200982 buf[bufi++] = (char_u)(nr >> (i * 8));
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200983 return undo_write(bi, buf, (size_t)len);
984}
985
986/*
987 * Write the pointer to an undo header. Instead of writing the pointer itself
988 * we use the sequence number of the header. This is converted back to
989 * pointers when reading. */
990 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100991put_header_ptr(bufinfo_T *bi, u_header_T *uhp)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200992{
993 undo_write_bytes(bi, (long_u)(uhp != NULL ? uhp->uh_seq : 0), 4);
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200994}
995
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +0200996 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100997undo_read_4c(bufinfo_T *bi)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200998{
999#ifdef FEAT_CRYPT
1000 if (bi->bi_buffer != NULL)
1001 {
1002 char_u buf[4];
1003 int n;
1004
1005 undo_read(bi, buf, (size_t)4);
Bram Moolenaar35169282014-09-11 22:50:09 +02001006 n = ((unsigned)buf[0] << 24) + (buf[1] << 16) + (buf[2] << 8) + buf[3];
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001007 return n;
1008 }
1009#endif
1010 return get4c(bi->bi_fp);
1011}
1012
1013 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001014undo_read_2c(bufinfo_T *bi)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001015{
1016#ifdef FEAT_CRYPT
1017 if (bi->bi_buffer != NULL)
1018 {
1019 char_u buf[2];
1020 int n;
1021
1022 undo_read(bi, buf, (size_t)2);
1023 n = (buf[0] << 8) + buf[1];
1024 return n;
1025 }
1026#endif
1027 return get2c(bi->bi_fp);
1028}
1029
1030 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001031undo_read_byte(bufinfo_T *bi)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001032{
1033#ifdef FEAT_CRYPT
1034 if (bi->bi_buffer != NULL)
1035 {
1036 char_u buf[1];
1037
1038 undo_read(bi, buf, (size_t)1);
1039 return buf[0];
1040 }
1041#endif
1042 return getc(bi->bi_fp);
1043}
1044
1045 static time_t
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001046undo_read_time(bufinfo_T *bi)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001047{
1048#ifdef FEAT_CRYPT
1049 if (bi->bi_buffer != NULL)
1050 {
1051 char_u buf[8];
1052 time_t n = 0;
1053 int i;
1054
1055 undo_read(bi, buf, (size_t)8);
1056 for (i = 0; i < 8; ++i)
1057 n = (n << 8) + buf[i];
1058 return n;
1059 }
1060#endif
1061 return get8ctime(bi->bi_fp);
1062}
1063
1064/*
1065 * Read "buffer[size]" from the undo file.
1066 * Return OK or FAIL.
1067 */
1068 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001069undo_read(bufinfo_T *bi, char_u *buffer, size_t size)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001070{
Bram Moolenaar56f2db52017-06-11 23:09:15 +02001071 int retval = OK;
1072
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001073#ifdef FEAT_CRYPT
1074 if (bi->bi_buffer != NULL)
1075 {
Bram Moolenaar9b8f0212014-08-13 22:05:53 +02001076 int size_todo = (int)size;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001077 char_u *p = buffer;
1078
1079 while (size_todo > 0)
1080 {
1081 size_t n;
1082
1083 if (bi->bi_used >= bi->bi_avail)
1084 {
1085 n = fread(bi->bi_buffer, 1, (size_t)CRYPT_BUF_SIZE, bi->bi_fp);
Bram Moolenaar55952d42016-11-05 14:58:34 +01001086 if (n == 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001087 {
Bram Moolenaar56f2db52017-06-11 23:09:15 +02001088 retval = FAIL;
1089 break;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001090 }
1091 bi->bi_avail = n;
1092 bi->bi_used = 0;
1093 crypt_decode_inplace(bi->bi_state, bi->bi_buffer, bi->bi_avail);
1094 }
1095 n = size_todo;
1096 if (n > bi->bi_avail - bi->bi_used)
1097 n = bi->bi_avail - bi->bi_used;
1098 mch_memmove(p, bi->bi_buffer + bi->bi_used, n);
1099 bi->bi_used += n;
Bram Moolenaar9b8f0212014-08-13 22:05:53 +02001100 size_todo -= (int)n;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001101 p += n;
1102 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001103 }
Bram Moolenaar56f2db52017-06-11 23:09:15 +02001104 else
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001105#endif
1106 if (fread(buffer, (size_t)size, 1, bi->bi_fp) != 1)
Bram Moolenaar56f2db52017-06-11 23:09:15 +02001107 retval = FAIL;
1108
1109 if (retval == FAIL)
1110 /* Error may be checked for only later. Fill with zeros,
1111 * so that the reader won't use garbage. */
1112 vim_memset(buffer, 0, size);
1113 return retval;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001114}
1115
1116/*
1117 * Read a string of length "len" from "bi->bi_fd".
1118 * "len" can be zero to allocate an empty line.
1119 * Decrypt the bytes if needed.
1120 * Append a NUL.
1121 * Returns a pointer to allocated memory or NULL for failure.
1122 */
1123 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001124read_string_decrypt(bufinfo_T *bi, int len)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001125{
1126 char_u *ptr = alloc((unsigned)len + 1);
1127
1128 if (ptr != NULL)
1129 {
1130 if (len > 0 && undo_read(bi, ptr, len) == FAIL)
1131 {
1132 vim_free(ptr);
1133 return NULL;
1134 }
Bram Moolenaarccae4672019-01-04 15:09:57 +01001135 // In case there are text properties there already is a NUL, but
1136 // checking for that is more expensive than just adding a dummy byte.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001137 ptr[len] = NUL;
1138#ifdef FEAT_CRYPT
1139 if (bi->bi_state != NULL && bi->bi_buffer == NULL)
1140 crypt_decode_inplace(bi->bi_state, ptr, len);
1141#endif
1142 }
1143 return ptr;
1144}
1145
1146/*
1147 * Writes the (not encrypted) header and initializes encryption if needed.
1148 */
1149 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001150serialize_header(bufinfo_T *bi, char_u *hash)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001151{
Bram Moolenaarccae4672019-01-04 15:09:57 +01001152 long len;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001153 buf_T *buf = bi->bi_buf;
1154 FILE *fp = bi->bi_fp;
1155 char_u time_buf[8];
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001156
1157 /* Start writing, first the magic marker and undo info version. */
1158 if (fwrite(UF_START_MAGIC, (size_t)UF_START_MAGIC_LEN, (size_t)1, fp) != 1)
1159 return FAIL;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001160
1161 /* If the buffer is encrypted then all text bytes following will be
1162 * encrypted. Numbers and other info is not crypted. */
1163#ifdef FEAT_CRYPT
Bram Moolenaarfa0ff9a2010-07-25 16:05:19 +02001164 if (*buf->b_p_key != NUL)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001165 {
1166 char_u *header;
1167 int header_len;
1168
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001169 undo_write_bytes(bi, (long_u)UF_VERSION_CRYPT, 2);
1170 bi->bi_state = crypt_create_for_writing(crypt_get_method_nr(buf),
1171 buf->b_p_key, &header, &header_len);
1172 if (bi->bi_state == NULL)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001173 return FAIL;
Bram Moolenaarccae4672019-01-04 15:09:57 +01001174 len = (long)fwrite(header, (size_t)header_len, (size_t)1, fp);
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001175 vim_free(header);
1176 if (len != 1)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001177 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001178 crypt_free_state(bi->bi_state);
1179 bi->bi_state = NULL;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001180 return FAIL;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001181 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001182
1183 if (crypt_whole_undofile(crypt_get_method_nr(buf)))
1184 {
1185 bi->bi_buffer = alloc(CRYPT_BUF_SIZE);
1186 if (bi->bi_buffer == NULL)
1187 {
1188 crypt_free_state(bi->bi_state);
1189 bi->bi_state = NULL;
1190 return FAIL;
1191 }
1192 bi->bi_used = 0;
1193 }
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001194 }
1195 else
1196#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001197 undo_write_bytes(bi, (long_u)UF_VERSION, 2);
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001198
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001199
1200 /* Write a hash of the buffer text, so that we can verify it is still the
1201 * same when reading the buffer text. */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001202 if (undo_write(bi, hash, (size_t)UNDO_HASH_SIZE) == FAIL)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001203 return FAIL;
1204
1205 /* buffer-specific data */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001206 undo_write_bytes(bi, (long_u)buf->b_ml.ml_line_count, 4);
Bram Moolenaarccae4672019-01-04 15:09:57 +01001207 len = buf->b_u_line_ptr.ul_line == NULL
1208 ? 0 : STRLEN(buf->b_u_line_ptr.ul_line);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001209 undo_write_bytes(bi, (long_u)len, 4);
Bram Moolenaarccae4672019-01-04 15:09:57 +01001210 if (len > 0 && fwrite_crypt(bi, buf->b_u_line_ptr.ul_line, (size_t)len) == FAIL)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001211 return FAIL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001212 undo_write_bytes(bi, (long_u)buf->b_u_line_lnum, 4);
1213 undo_write_bytes(bi, (long_u)buf->b_u_line_colnr, 4);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001214
1215 /* Undo structures header data */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001216 put_header_ptr(bi, buf->b_u_oldhead);
1217 put_header_ptr(bi, buf->b_u_newhead);
1218 put_header_ptr(bi, buf->b_u_curhead);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001219
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001220 undo_write_bytes(bi, (long_u)buf->b_u_numhead, 4);
1221 undo_write_bytes(bi, (long_u)buf->b_u_seq_last, 4);
1222 undo_write_bytes(bi, (long_u)buf->b_u_seq_cur, 4);
1223 time_to_bytes(buf->b_u_time_cur, time_buf);
1224 undo_write(bi, time_buf, 8);
Bram Moolenaara800b422010-06-27 01:15:55 +02001225
1226 /* Optional fields. */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001227 undo_write_bytes(bi, 4, 1);
1228 undo_write_bytes(bi, UF_LAST_SAVE_NR, 1);
1229 undo_write_bytes(bi, (long_u)buf->b_u_save_nr_last, 4);
Bram Moolenaara800b422010-06-27 01:15:55 +02001230
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001231 undo_write_bytes(bi, 0, 1); /* end marker */
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001232
1233 return OK;
1234}
1235
1236 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001237serialize_uhp(bufinfo_T *bi, u_header_T *uhp)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001238{
1239 int i;
1240 u_entry_T *uep;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001241 char_u time_buf[8];
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001242
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001243 if (undo_write_bytes(bi, (long_u)UF_HEADER_MAGIC, 2) == FAIL)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001244 return FAIL;
1245
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001246 put_header_ptr(bi, uhp->uh_next.ptr);
1247 put_header_ptr(bi, uhp->uh_prev.ptr);
1248 put_header_ptr(bi, uhp->uh_alt_next.ptr);
1249 put_header_ptr(bi, uhp->uh_alt_prev.ptr);
1250 undo_write_bytes(bi, uhp->uh_seq, 4);
1251 serialize_pos(bi, uhp->uh_cursor);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001252#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001253 undo_write_bytes(bi, (long_u)uhp->uh_cursor_vcol, 4);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001254#else
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001255 undo_write_bytes(bi, (long_u)0, 4);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001256#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001257 undo_write_bytes(bi, (long_u)uhp->uh_flags, 2);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001258 /* Assume NMARKS will stay the same. */
1259 for (i = 0; i < NMARKS; ++i)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001260 serialize_pos(bi, uhp->uh_namedm[i]);
1261 serialize_visualinfo(bi, &uhp->uh_visual);
1262 time_to_bytes(uhp->uh_time, time_buf);
1263 undo_write(bi, time_buf, 8);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001264
Bram Moolenaara800b422010-06-27 01:15:55 +02001265 /* Optional fields. */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001266 undo_write_bytes(bi, 4, 1);
1267 undo_write_bytes(bi, UHP_SAVE_NR, 1);
1268 undo_write_bytes(bi, (long_u)uhp->uh_save_nr, 4);
Bram Moolenaara800b422010-06-27 01:15:55 +02001269
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001270 undo_write_bytes(bi, 0, 1); /* end marker */
Bram Moolenaara800b422010-06-27 01:15:55 +02001271
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001272 /* Write all the entries. */
1273 for (uep = uhp->uh_entry; uep != NULL; uep = uep->ue_next)
1274 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001275 undo_write_bytes(bi, (long_u)UF_ENTRY_MAGIC, 2);
1276 if (serialize_uep(bi, uep) == FAIL)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001277 return FAIL;
1278 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001279 undo_write_bytes(bi, (long_u)UF_ENTRY_END_MAGIC, 2);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001280 return OK;
1281}
1282
1283 static u_header_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001284unserialize_uhp(bufinfo_T *bi, char_u *file_name)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001285{
1286 u_header_T *uhp;
1287 int i;
1288 u_entry_T *uep, *last_uep;
1289 int c;
1290 int error;
1291
1292 uhp = (u_header_T *)U_ALLOC_LINE(sizeof(u_header_T));
1293 if (uhp == NULL)
1294 return NULL;
1295 vim_memset(uhp, 0, sizeof(u_header_T));
1296#ifdef U_DEBUG
1297 uhp->uh_magic = UH_MAGIC;
1298#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001299 uhp->uh_next.seq = undo_read_4c(bi);
1300 uhp->uh_prev.seq = undo_read_4c(bi);
1301 uhp->uh_alt_next.seq = undo_read_4c(bi);
1302 uhp->uh_alt_prev.seq = undo_read_4c(bi);
1303 uhp->uh_seq = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001304 if (uhp->uh_seq <= 0)
1305 {
1306 corruption_error("uh_seq", file_name);
1307 vim_free(uhp);
1308 return NULL;
1309 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001310 unserialize_pos(bi, &uhp->uh_cursor);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001311#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001312 uhp->uh_cursor_vcol = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001313#else
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001314 (void)undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001315#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001316 uhp->uh_flags = undo_read_2c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001317 for (i = 0; i < NMARKS; ++i)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001318 unserialize_pos(bi, &uhp->uh_namedm[i]);
1319 unserialize_visualinfo(bi, &uhp->uh_visual);
1320 uhp->uh_time = undo_read_time(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001321
Bram Moolenaara800b422010-06-27 01:15:55 +02001322 /* Optional fields. */
Bram Moolenaar69154f22010-07-18 21:42:34 +02001323 for (;;)
1324 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001325 int len = undo_read_byte(bi);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001326 int what;
Bram Moolenaara800b422010-06-27 01:15:55 +02001327
Bram Moolenaar69154f22010-07-18 21:42:34 +02001328 if (len == 0)
1329 break;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001330 what = undo_read_byte(bi);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001331 switch (what)
1332 {
1333 case UHP_SAVE_NR:
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001334 uhp->uh_save_nr = undo_read_4c(bi);
Bram Moolenaara800b422010-06-27 01:15:55 +02001335 break;
Bram Moolenaar69154f22010-07-18 21:42:34 +02001336 default:
1337 /* field not supported, skip */
1338 while (--len >= 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001339 (void)undo_read_byte(bi);
Bram Moolenaara800b422010-06-27 01:15:55 +02001340 }
Bram Moolenaar69154f22010-07-18 21:42:34 +02001341 }
Bram Moolenaara800b422010-06-27 01:15:55 +02001342
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001343 /* Unserialize the uep list. */
1344 last_uep = NULL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001345 while ((c = undo_read_2c(bi)) == UF_ENTRY_MAGIC)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001346 {
1347 error = FALSE;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001348 uep = unserialize_uep(bi, &error, file_name);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001349 if (last_uep == NULL)
1350 uhp->uh_entry = uep;
1351 else
1352 last_uep->ue_next = uep;
1353 last_uep = uep;
1354 if (uep == NULL || error)
1355 {
1356 u_free_uhp(uhp);
1357 return NULL;
1358 }
1359 }
1360 if (c != UF_ENTRY_END_MAGIC)
1361 {
1362 corruption_error("entry end", file_name);
1363 u_free_uhp(uhp);
1364 return NULL;
1365 }
1366
1367 return uhp;
1368}
1369
Bram Moolenaar9db58062010-05-29 20:33:07 +02001370/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001371 * Serialize "uep".
Bram Moolenaar9db58062010-05-29 20:33:07 +02001372 */
1373 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001374serialize_uep(
1375 bufinfo_T *bi,
1376 u_entry_T *uep)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001377{
1378 int i;
1379 size_t len;
1380
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001381 undo_write_bytes(bi, (long_u)uep->ue_top, 4);
1382 undo_write_bytes(bi, (long_u)uep->ue_bot, 4);
1383 undo_write_bytes(bi, (long_u)uep->ue_lcount, 4);
1384 undo_write_bytes(bi, (long_u)uep->ue_size, 4);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001385 for (i = 0; i < uep->ue_size; ++i)
1386 {
Bram Moolenaarccae4672019-01-04 15:09:57 +01001387 // Text is written without the text properties, since we cannot restore
1388 // the text property types.
1389 len = STRLEN(uep->ue_array[i].ul_line);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001390 if (undo_write_bytes(bi, (long_u)len, 4) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001391 return FAIL;
Bram Moolenaarccae4672019-01-04 15:09:57 +01001392 if (len > 0 && fwrite_crypt(bi, uep->ue_array[i].ul_line, len) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001393 return FAIL;
1394 }
1395 return OK;
1396}
1397
1398 static u_entry_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001399unserialize_uep(bufinfo_T *bi, int *error, char_u *file_name)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001400{
1401 int i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001402 u_entry_T *uep;
Bram Moolenaarccae4672019-01-04 15:09:57 +01001403 undoline_T *array = NULL;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001404 char_u *line;
1405 int line_len;
1406
1407 uep = (u_entry_T *)U_ALLOC_LINE(sizeof(u_entry_T));
1408 if (uep == NULL)
1409 return NULL;
1410 vim_memset(uep, 0, sizeof(u_entry_T));
1411#ifdef U_DEBUG
1412 uep->ue_magic = UE_MAGIC;
1413#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001414 uep->ue_top = undo_read_4c(bi);
1415 uep->ue_bot = undo_read_4c(bi);
1416 uep->ue_lcount = undo_read_4c(bi);
1417 uep->ue_size = undo_read_4c(bi);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001418 if (uep->ue_size > 0)
1419 {
Bram Moolenaar0c8485f2017-02-26 18:17:10 +01001420 if (uep->ue_size < LONG_MAX / (int)sizeof(char_u *))
Bram Moolenaarccae4672019-01-04 15:09:57 +01001421 array = (undoline_T *)U_ALLOC_LINE(sizeof(undoline_T) * uep->ue_size);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001422 if (array == NULL)
1423 {
1424 *error = TRUE;
1425 return uep;
1426 }
Bram Moolenaarccae4672019-01-04 15:09:57 +01001427 vim_memset(array, 0, sizeof(undoline_T) * uep->ue_size);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001428 }
1429 uep->ue_array = array;
1430
1431 for (i = 0; i < uep->ue_size; ++i)
1432 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001433 line_len = undo_read_4c(bi);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001434 if (line_len >= 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001435 line = read_string_decrypt(bi, line_len);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001436 else
1437 {
1438 line = NULL;
1439 corruption_error("line length", file_name);
1440 }
1441 if (line == NULL)
1442 {
1443 *error = TRUE;
1444 return uep;
1445 }
Bram Moolenaarccae4672019-01-04 15:09:57 +01001446 array[i].ul_line = line;
1447 array[i].ul_len = line_len + 1;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001448 }
1449 return uep;
1450}
1451
1452/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001453 * Serialize "pos".
Bram Moolenaar9db58062010-05-29 20:33:07 +02001454 */
1455 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001456serialize_pos(bufinfo_T *bi, pos_T pos)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001457{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001458 undo_write_bytes(bi, (long_u)pos.lnum, 4);
1459 undo_write_bytes(bi, (long_u)pos.col, 4);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001460#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001461 undo_write_bytes(bi, (long_u)pos.coladd, 4);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001462#else
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001463 undo_write_bytes(bi, (long_u)0, 4);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001464#endif
1465}
1466
1467/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001468 * Unserialize the pos_T at the current position.
Bram Moolenaar9db58062010-05-29 20:33:07 +02001469 */
1470 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001471unserialize_pos(bufinfo_T *bi, pos_T *pos)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001472{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001473 pos->lnum = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001474 if (pos->lnum < 0)
1475 pos->lnum = 0;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001476 pos->col = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001477 if (pos->col < 0)
1478 pos->col = 0;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001479#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001480 pos->coladd = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001481 if (pos->coladd < 0)
1482 pos->coladd = 0;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001483#else
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001484 (void)undo_read_4c(bi);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001485#endif
1486}
1487
1488/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001489 * Serialize "info".
Bram Moolenaar9db58062010-05-29 20:33:07 +02001490 */
1491 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001492serialize_visualinfo(bufinfo_T *bi, visualinfo_T *info)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001493{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001494 serialize_pos(bi, info->vi_start);
1495 serialize_pos(bi, info->vi_end);
1496 undo_write_bytes(bi, (long_u)info->vi_mode, 4);
1497 undo_write_bytes(bi, (long_u)info->vi_curswant, 4);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001498}
1499
1500/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001501 * Unserialize the visualinfo_T at the current position.
Bram Moolenaar9db58062010-05-29 20:33:07 +02001502 */
1503 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001504unserialize_visualinfo(bufinfo_T *bi, visualinfo_T *info)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001505{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001506 unserialize_pos(bi, &info->vi_start);
1507 unserialize_pos(bi, &info->vi_end);
1508 info->vi_mode = undo_read_4c(bi);
1509 info->vi_curswant = undo_read_4c(bi);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001510}
1511
1512/*
1513 * Write the undo tree in an undo file.
1514 * When "name" is not NULL, use it as the name of the undo file.
1515 * Otherwise use buf->b_ffname to generate the undo file name.
1516 * "buf" must never be null, buf->b_ffname is used to obtain the original file
1517 * permissions.
1518 * "forceit" is TRUE for ":wundo!", FALSE otherwise.
1519 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text.
1520 */
1521 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001522u_write_undo(
1523 char_u *name,
1524 int forceit,
1525 buf_T *buf,
1526 char_u *hash)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001527{
1528 u_header_T *uhp;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001529 char_u *file_name;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001530 int mark;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001531#ifdef U_DEBUG
1532 int headers_written = 0;
1533#endif
1534 int fd;
1535 FILE *fp = NULL;
1536 int perm;
1537 int write_ok = FALSE;
1538#ifdef UNIX
1539 int st_old_valid = FALSE;
Bram Moolenaar8767f522016-07-01 17:17:39 +02001540 stat_T st_old;
1541 stat_T st_new;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001542#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001543 bufinfo_T bi;
1544
Bram Moolenaar5a669b92014-08-12 20:14:33 +02001545 vim_memset(&bi, 0, sizeof(bi));
Bram Moolenaar9db58062010-05-29 20:33:07 +02001546
1547 if (name == NULL)
1548 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001549 file_name = u_get_undo_file_name(buf->b_ffname, FALSE);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001550 if (file_name == NULL)
1551 {
1552 if (p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001553 {
1554 verbose_enter();
1555 smsg((char_u *)
1556 _("Cannot write undo file in any directory in 'undodir'"));
1557 verbose_leave();
1558 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001559 return;
1560 }
1561 }
1562 else
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001563 file_name = name;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001564
1565 /*
1566 * Decide about the permission to use for the undo file. If the buffer
1567 * has a name use the permission of the original file. Otherwise only
1568 * allow the user to access the undo file.
1569 */
1570 perm = 0600;
1571 if (buf->b_ffname != NULL)
1572 {
1573#ifdef UNIX
1574 if (mch_stat((char *)buf->b_ffname, &st_old) >= 0)
1575 {
1576 perm = st_old.st_mode;
1577 st_old_valid = TRUE;
1578 }
1579#else
1580 perm = mch_getperm(buf->b_ffname);
1581 if (perm < 0)
1582 perm = 0600;
1583#endif
1584 }
1585
Bram Moolenaar3cbac302015-04-21 16:12:06 +02001586 /* strip any s-bit and executable bit */
1587 perm = perm & 0666;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001588
1589 /* If the undo file already exists, verify that it actually is an undo
1590 * file, and delete it. */
1591 if (mch_getperm(file_name) >= 0)
1592 {
1593 if (name == NULL || !forceit)
1594 {
1595 /* Check we can read it and it's an undo file. */
1596 fd = mch_open((char *)file_name, O_RDONLY|O_EXTRA, 0);
1597 if (fd < 0)
1598 {
1599 if (name != NULL || p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001600 {
1601 if (name == NULL)
1602 verbose_enter();
1603 smsg((char_u *)
1604 _("Will not overwrite with undo file, cannot read: %s"),
Bram Moolenaar9db58062010-05-29 20:33:07 +02001605 file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001606 if (name == NULL)
1607 verbose_leave();
1608 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001609 goto theend;
1610 }
1611 else
1612 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02001613 char_u mbuf[UF_START_MAGIC_LEN];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001614 int len;
1615
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01001616 len = read_eintr(fd, mbuf, UF_START_MAGIC_LEN);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001617 close(fd);
1618 if (len < UF_START_MAGIC_LEN
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02001619 || memcmp(mbuf, UF_START_MAGIC, UF_START_MAGIC_LEN) != 0)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001620 {
1621 if (name != NULL || p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001622 {
1623 if (name == NULL)
1624 verbose_enter();
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001625 smsg((char_u *)
1626 _("Will not overwrite, this is not an undo file: %s"),
Bram Moolenaar9db58062010-05-29 20:33:07 +02001627 file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001628 if (name == NULL)
1629 verbose_leave();
1630 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001631 goto theend;
1632 }
1633 }
1634 }
1635 mch_remove(file_name);
1636 }
1637
Bram Moolenaar504a8212010-05-30 17:17:42 +02001638 /* If there is no undo information at all, quit here after deleting any
1639 * existing undo file. */
Bram Moolenaarccae4672019-01-04 15:09:57 +01001640 if (buf->b_u_numhead == 0 && buf->b_u_line_ptr.ul_line == NULL)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001641 {
1642 if (p_verbose > 0)
Bram Moolenaar97ea5112010-06-12 06:46:44 +02001643 verb_msg((char_u *)_("Skipping undo file write, nothing to undo"));
Bram Moolenaar504a8212010-05-30 17:17:42 +02001644 goto theend;
1645 }
1646
Bram Moolenaar9db58062010-05-29 20:33:07 +02001647 fd = mch_open((char *)file_name,
1648 O_CREAT|O_EXTRA|O_WRONLY|O_EXCL|O_NOFOLLOW, perm);
1649 if (fd < 0)
1650 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001651 EMSG2(_(e_not_open), file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001652 goto theend;
1653 }
1654 (void)mch_setperm(file_name, perm);
1655 if (p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001656 {
1657 verbose_enter();
Bram Moolenaar9db58062010-05-29 20:33:07 +02001658 smsg((char_u *)_("Writing undo file: %s"), file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001659 verbose_leave();
1660 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001661
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001662#ifdef U_DEBUG
Bram Moolenaar504a8212010-05-30 17:17:42 +02001663 /* Check there is no problem in undo info before writing. */
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001664 u_check(FALSE);
1665#endif
1666
Bram Moolenaar9db58062010-05-29 20:33:07 +02001667#ifdef UNIX
1668 /*
1669 * Try to set the group of the undo file same as the original file. If
1670 * this fails, set the protection bits for the group same as the
1671 * protection bits for others.
1672 */
Bram Moolenaarce69e822010-07-21 20:31:07 +02001673 if (st_old_valid
1674 && mch_stat((char *)file_name, &st_new) >= 0
1675 && st_new.st_gid != st_old.st_gid
Bram Moolenaar9db58062010-05-29 20:33:07 +02001676# ifdef HAVE_FCHOWN /* sequent-ptx lacks fchown() */
Bram Moolenaarce69e822010-07-21 20:31:07 +02001677 && fchown(fd, (uid_t)-1, st_old.st_gid) != 0
Bram Moolenaar9db58062010-05-29 20:33:07 +02001678# endif
1679 )
1680 mch_setperm(file_name, (perm & 0707) | ((perm & 07) << 3));
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02001681# if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001682 if (buf->b_ffname != NULL)
1683 mch_copy_sec(buf->b_ffname, file_name);
1684# endif
1685#endif
1686
1687 fp = fdopen(fd, "w");
1688 if (fp == NULL)
1689 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001690 EMSG2(_(e_not_open), file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001691 close(fd);
1692 mch_remove(file_name);
1693 goto theend;
1694 }
1695
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001696 /* Undo must be synced. */
1697 u_sync(TRUE);
1698
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001699 /*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001700 * Write the header. Initializes encryption, if enabled.
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001701 */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001702 bi.bi_buf = buf;
1703 bi.bi_fp = fp;
1704 if (serialize_header(&bi, hash) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001705 goto write_error;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001706
1707 /*
1708 * Iteratively serialize UHPs and their UEPs from the top down.
1709 */
1710 mark = ++lastmark;
1711 uhp = buf->b_u_oldhead;
1712 while (uhp != NULL)
1713 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001714 /* Serialize current UHP if we haven't seen it */
1715 if (uhp->uh_walk != mark)
1716 {
1717 uhp->uh_walk = mark;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001718#ifdef U_DEBUG
1719 ++headers_written;
1720#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001721 if (serialize_uhp(&bi, uhp) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001722 goto write_error;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001723 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001724
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001725 /* Now walk through the tree - algorithm from undo_time(). */
1726 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != mark)
1727 uhp = uhp->uh_prev.ptr;
1728 else if (uhp->uh_alt_next.ptr != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001729 && uhp->uh_alt_next.ptr->uh_walk != mark)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001730 uhp = uhp->uh_alt_next.ptr;
1731 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001732 && uhp->uh_next.ptr->uh_walk != mark)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001733 uhp = uhp->uh_next.ptr;
1734 else if (uhp->uh_alt_prev.ptr != NULL)
1735 uhp = uhp->uh_alt_prev.ptr;
1736 else
1737 uhp = uhp->uh_next.ptr;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001738 }
1739
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001740 if (undo_write_bytes(&bi, (long_u)UF_HEADER_END_MAGIC, 2) == OK)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001741 write_ok = TRUE;
1742#ifdef U_DEBUG
1743 if (headers_written != buf->b_u_numhead)
Bram Moolenaar570064c2013-06-10 20:25:10 +02001744 {
1745 EMSGN("Written %ld headers, ...", headers_written);
1746 EMSGN("... but numhead is %ld", buf->b_u_numhead);
1747 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001748#endif
1749
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001750#ifdef FEAT_CRYPT
1751 if (bi.bi_state != NULL && undo_flush(&bi) == FAIL)
1752 write_ok = FALSE;
1753#endif
1754
Bram Moolenaar9db58062010-05-29 20:33:07 +02001755write_error:
1756 fclose(fp);
1757 if (!write_ok)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001758 EMSG2(_("E829: write error in undo file: %s"), file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001759
Bram Moolenaard0573012017-10-28 21:11:06 +02001760#if defined(WIN3264)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001761 /* Copy file attributes; for systems where this can only be done after
1762 * closing the file. */
Bram Moolenaar9db58062010-05-29 20:33:07 +02001763 if (buf->b_ffname != NULL)
1764 (void)mch_copy_file_attribute(buf->b_ffname, file_name);
1765#endif
1766#ifdef HAVE_ACL
1767 if (buf->b_ffname != NULL)
1768 {
1769 vim_acl_T acl;
1770
1771 /* For systems that support ACL: get the ACL from the original file. */
1772 acl = mch_get_acl(buf->b_ffname);
1773 mch_set_acl(file_name, acl);
Bram Moolenaard2aed442012-06-01 13:46:12 +02001774 mch_free_acl(acl);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001775 }
1776#endif
1777
1778theend:
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001779#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001780 if (bi.bi_state != NULL)
1781 crypt_free_state(bi.bi_state);
1782 vim_free(bi.bi_buffer);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001783#endif
Bram Moolenaar9db58062010-05-29 20:33:07 +02001784 if (file_name != name)
1785 vim_free(file_name);
1786}
1787
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001788/*
1789 * Load the undo tree from an undo file.
1790 * If "name" is not NULL use it as the undo file name. This also means being
1791 * a bit more verbose.
1792 * Otherwise use curbuf->b_ffname to generate the undo file name.
1793 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text.
1794 */
1795 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001796u_read_undo(char_u *name, char_u *hash, char_u *orig_name)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001797{
1798 char_u *file_name;
1799 FILE *fp;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001800 long version, str_len;
Bram Moolenaarccae4672019-01-04 15:09:57 +01001801 undoline_T line_ptr;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001802 linenr_T line_lnum;
1803 colnr_T line_colnr;
1804 linenr_T line_count;
Bram Moolenaar3eb16372017-02-26 18:11:36 +01001805 long num_head = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001806 long old_header_seq, new_header_seq, cur_header_seq;
1807 long seq_last, seq_cur;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001808 long last_save_nr = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001809 short old_idx = -1, new_idx = -1, cur_idx = -1;
1810 long num_read_uhps = 0;
1811 time_t seq_time;
1812 int i, j;
1813 int c;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001814 u_header_T *uhp;
1815 u_header_T **uhp_table = NULL;
1816 char_u read_hash[UNDO_HASH_SIZE];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001817 char_u magic_buf[UF_START_MAGIC_LEN];
1818#ifdef U_DEBUG
1819 int *uhp_table_used;
1820#endif
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001821#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001822 stat_T st_orig;
1823 stat_T st_undo;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001824#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001825 bufinfo_T bi;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001826
Bram Moolenaar5a669b92014-08-12 20:14:33 +02001827 vim_memset(&bi, 0, sizeof(bi));
Bram Moolenaarccae4672019-01-04 15:09:57 +01001828 line_ptr.ul_len = 0;
1829 line_ptr.ul_line = NULL;
1830
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001831 if (name == NULL)
1832 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001833 file_name = u_get_undo_file_name(curbuf->b_ffname, TRUE);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001834 if (file_name == NULL)
1835 return;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001836
1837#ifdef UNIX
1838 /* For safety we only read an undo file if the owner is equal to the
Bram Moolenaar3b262392013-09-08 15:40:49 +02001839 * owner of the text file or equal to the current user. */
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001840 if (mch_stat((char *)orig_name, &st_orig) >= 0
1841 && mch_stat((char *)file_name, &st_undo) >= 0
Bram Moolenaar3b262392013-09-08 15:40:49 +02001842 && st_orig.st_uid != st_undo.st_uid
1843 && st_undo.st_uid != getuid())
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001844 {
1845 if (p_verbose > 0)
1846 {
1847 verbose_enter();
1848 smsg((char_u *)_("Not reading undo file, owner differs: %s"),
1849 file_name);
1850 verbose_leave();
1851 }
1852 return;
1853 }
1854#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001855 }
1856 else
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001857 file_name = name;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001858
1859 if (p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001860 {
1861 verbose_enter();
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001862 smsg((char_u *)_("Reading undo file: %s"), file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001863 verbose_leave();
1864 }
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001865
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001866 fp = mch_fopen((char *)file_name, "r");
1867 if (fp == NULL)
1868 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001869 if (name != NULL || p_verbose > 0)
1870 EMSG2(_("E822: Cannot open undo file for reading: %s"), file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001871 goto error;
1872 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001873 bi.bi_buf = curbuf;
1874 bi.bi_fp = fp;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001875
Bram Moolenaar9db58062010-05-29 20:33:07 +02001876 /*
1877 * Read the undo file header.
1878 */
1879 if (fread(magic_buf, UF_START_MAGIC_LEN, 1, fp) != 1
1880 || memcmp(magic_buf, UF_START_MAGIC, UF_START_MAGIC_LEN) != 0)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001881 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001882 EMSG2(_("E823: Not an undo file: %s"), file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001883 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001884 }
1885 version = get2c(fp);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001886 if (version == UF_VERSION_CRYPT)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001887 {
1888#ifdef FEAT_CRYPT
Bram Moolenaar56be9502010-06-06 14:20:26 +02001889 if (*curbuf->b_p_key == NUL)
1890 {
1891 EMSG2(_("E832: Non-encrypted file has encrypted undo file: %s"),
1892 file_name);
1893 goto error;
1894 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001895 bi.bi_state = crypt_create_from_file(fp, curbuf->b_p_key);
1896 if (bi.bi_state == NULL)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001897 {
1898 EMSG2(_("E826: Undo file decryption failed: %s"), file_name);
1899 goto error;
1900 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001901 if (crypt_whole_undofile(bi.bi_state->method_nr))
1902 {
1903 bi.bi_buffer = alloc(CRYPT_BUF_SIZE);
1904 if (bi.bi_buffer == NULL)
1905 {
1906 crypt_free_state(bi.bi_state);
1907 bi.bi_state = NULL;
1908 goto error;
1909 }
1910 bi.bi_avail = 0;
1911 bi.bi_used = 0;
1912 }
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001913#else
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001914 EMSG2(_("E827: Undo file is encrypted: %s"), file_name);
1915 goto error;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001916#endif
1917 }
Bram Moolenaar69154f22010-07-18 21:42:34 +02001918 else if (version != UF_VERSION)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001919 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001920 EMSG2(_("E824: Incompatible undo file: %s"), file_name);
1921 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001922 }
1923
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001924 if (undo_read(&bi, read_hash, (size_t)UNDO_HASH_SIZE) == FAIL)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001925 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001926 corruption_error("hash", file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001927 goto error;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001928 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001929 line_count = (linenr_T)undo_read_4c(&bi);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001930 if (memcmp(hash, read_hash, UNDO_HASH_SIZE) != 0
1931 || line_count != curbuf->b_ml.ml_line_count)
1932 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001933 if (p_verbose > 0 || name != NULL)
1934 {
Bram Moolenaar504a8212010-05-30 17:17:42 +02001935 if (name == NULL)
1936 verbose_enter();
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001937 give_warning((char_u *)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001938 _("File contents changed, cannot use undo info"), TRUE);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001939 if (name == NULL)
1940 verbose_leave();
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001941 }
1942 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001943 }
1944
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001945 /* Read undo data for "U" command. */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001946 str_len = undo_read_4c(&bi);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001947 if (str_len < 0)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001948 goto error;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001949 if (str_len > 0)
Bram Moolenaarccae4672019-01-04 15:09:57 +01001950 {
1951 line_ptr.ul_line = read_string_decrypt(&bi, str_len);
1952 line_ptr.ul_len = str_len + 1;
1953 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001954 line_lnum = (linenr_T)undo_read_4c(&bi);
1955 line_colnr = (colnr_T)undo_read_4c(&bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001956 if (line_lnum < 0 || line_colnr < 0)
1957 {
1958 corruption_error("line lnum/col", file_name);
1959 goto error;
1960 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001961
1962 /* Begin general undo data */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001963 old_header_seq = undo_read_4c(&bi);
1964 new_header_seq = undo_read_4c(&bi);
1965 cur_header_seq = undo_read_4c(&bi);
1966 num_head = undo_read_4c(&bi);
1967 seq_last = undo_read_4c(&bi);
1968 seq_cur = undo_read_4c(&bi);
1969 seq_time = undo_read_time(&bi);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001970
Bram Moolenaar69154f22010-07-18 21:42:34 +02001971 /* Optional header fields. */
1972 for (;;)
1973 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001974 int len = undo_read_byte(&bi);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001975 int what;
Bram Moolenaara800b422010-06-27 01:15:55 +02001976
Bram Moolenaar69154f22010-07-18 21:42:34 +02001977 if (len == 0 || len == EOF)
1978 break;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001979 what = undo_read_byte(&bi);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001980 switch (what)
1981 {
1982 case UF_LAST_SAVE_NR:
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001983 last_save_nr = undo_read_4c(&bi);
Bram Moolenaara800b422010-06-27 01:15:55 +02001984 break;
Bram Moolenaar69154f22010-07-18 21:42:34 +02001985 default:
1986 /* field not supported, skip */
1987 while (--len >= 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001988 (void)undo_read_byte(&bi);
Bram Moolenaara800b422010-06-27 01:15:55 +02001989 }
Bram Moolenaar69154f22010-07-18 21:42:34 +02001990 }
Bram Moolenaara800b422010-06-27 01:15:55 +02001991
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001992 /* uhp_table will store the freshly created undo headers we allocate
1993 * until we insert them into curbuf. The table remains sorted by the
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001994 * sequence numbers of the headers.
1995 * When there are no headers uhp_table is NULL. */
1996 if (num_head > 0)
1997 {
Bram Moolenaar3eb16372017-02-26 18:11:36 +01001998 if (num_head < LONG_MAX / (long)sizeof(u_header_T *))
1999 uhp_table = (u_header_T **)U_ALLOC_LINE(
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002000 num_head * sizeof(u_header_T *));
2001 if (uhp_table == NULL)
2002 goto error;
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002003 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002004
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002005 while ((c = undo_read_2c(&bi)) == UF_HEADER_MAGIC)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002006 {
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02002007 if (num_read_uhps >= num_head)
2008 {
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002009 corruption_error("num_head too small", file_name);
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02002010 goto error;
2011 }
2012
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002013 uhp = unserialize_uhp(&bi, file_name);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002014 if (uhp == NULL)
2015 goto error;
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02002016 uhp_table[num_read_uhps++] = uhp;
2017 }
2018
2019 if (num_read_uhps != num_head)
2020 {
2021 corruption_error("num_head", file_name);
2022 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002023 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02002024 if (c != UF_HEADER_END_MAGIC)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002025 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02002026 corruption_error("end marker", file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002027 goto error;
2028 }
2029
Bram Moolenaar9db58062010-05-29 20:33:07 +02002030#ifdef U_DEBUG
2031 uhp_table_used = (int *)alloc_clear(
2032 (unsigned)(sizeof(int) * num_head + 1));
2033# define SET_FLAG(j) ++uhp_table_used[j]
2034#else
2035# define SET_FLAG(j)
2036#endif
2037
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002038 /* We have put all of the headers into a table. Now we iterate through the
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002039 * table and swizzle each sequence number we have stored in uh_*_seq into
2040 * a pointer corresponding to the header with that sequence number. */
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002041 for (i = 0; i < num_head; i++)
2042 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002043 uhp = uhp_table[i];
2044 if (uhp == NULL)
2045 continue;
2046 for (j = 0; j < num_head; j++)
2047 if (uhp_table[j] != NULL && i != j
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002048 && uhp_table[i]->uh_seq == uhp_table[j]->uh_seq)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002049 {
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02002050 corruption_error("duplicate uh_seq", file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002051 goto error;
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02002052 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002053 for (j = 0; j < num_head; j++)
2054 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002055 && uhp_table[j]->uh_seq == uhp->uh_next.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002056 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002057 uhp->uh_next.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02002058 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002059 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002060 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002061 for (j = 0; j < num_head; j++)
2062 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002063 && uhp_table[j]->uh_seq == uhp->uh_prev.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002064 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002065 uhp->uh_prev.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02002066 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002067 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002068 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002069 for (j = 0; j < num_head; j++)
2070 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002071 && uhp_table[j]->uh_seq == uhp->uh_alt_next.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002072 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002073 uhp->uh_alt_next.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02002074 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002075 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002076 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002077 for (j = 0; j < num_head; j++)
2078 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002079 && uhp_table[j]->uh_seq == uhp->uh_alt_prev.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002080 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002081 uhp->uh_alt_prev.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02002082 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002083 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002084 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002085 if (old_header_seq > 0 && old_idx < 0 && uhp->uh_seq == old_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002086 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002087 old_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002088 SET_FLAG(i);
2089 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002090 if (new_header_seq > 0 && new_idx < 0 && uhp->uh_seq == new_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002091 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002092 new_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002093 SET_FLAG(i);
2094 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002095 if (cur_header_seq > 0 && cur_idx < 0 && uhp->uh_seq == cur_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002096 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002097 cur_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002098 SET_FLAG(i);
2099 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002100 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02002101
2102 /* Now that we have read the undo info successfully, free the current undo
2103 * info and use the info from the file. */
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002104 u_blockfree(curbuf);
Bram Moolenaar9db58062010-05-29 20:33:07 +02002105 curbuf->b_u_oldhead = old_idx < 0 ? NULL : uhp_table[old_idx];
2106 curbuf->b_u_newhead = new_idx < 0 ? NULL : uhp_table[new_idx];
2107 curbuf->b_u_curhead = cur_idx < 0 ? NULL : uhp_table[cur_idx];
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002108 curbuf->b_u_line_ptr = line_ptr;
2109 curbuf->b_u_line_lnum = line_lnum;
2110 curbuf->b_u_line_colnr = line_colnr;
2111 curbuf->b_u_numhead = num_head;
2112 curbuf->b_u_seq_last = seq_last;
2113 curbuf->b_u_seq_cur = seq_cur;
Bram Moolenaara800b422010-06-27 01:15:55 +02002114 curbuf->b_u_time_cur = seq_time;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002115 curbuf->b_u_save_nr_last = last_save_nr;
Bram Moolenaardba01a02010-11-03 19:32:42 +01002116 curbuf->b_u_save_nr_cur = last_save_nr;
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02002117
2118 curbuf->b_u_synced = TRUE;
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002119 vim_free(uhp_table);
Bram Moolenaar9db58062010-05-29 20:33:07 +02002120
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002121#ifdef U_DEBUG
Bram Moolenaar9db58062010-05-29 20:33:07 +02002122 for (i = 0; i < num_head; ++i)
2123 if (uhp_table_used[i] == 0)
2124 EMSGN("uhp_table entry %ld not used, leaking memory", i);
2125 vim_free(uhp_table_used);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002126 u_check(TRUE);
2127#endif
Bram Moolenaar9db58062010-05-29 20:33:07 +02002128
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002129 if (name != NULL)
2130 smsg((char_u *)_("Finished reading undo file %s"), file_name);
2131 goto theend;
2132
2133error:
Bram Moolenaarccae4672019-01-04 15:09:57 +01002134 vim_free(line_ptr.ul_line);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002135 if (uhp_table != NULL)
2136 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002137 for (i = 0; i < num_read_uhps; i++)
2138 if (uhp_table[i] != NULL)
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02002139 u_free_uhp(uhp_table[i]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002140 vim_free(uhp_table);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002141 }
2142
2143theend:
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002144#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002145 if (bi.bi_state != NULL)
2146 crypt_free_state(bi.bi_state);
2147 vim_free(bi.bi_buffer);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002148#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002149 if (fp != NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002150 fclose(fp);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002151 if (file_name != name)
2152 vim_free(file_name);
2153 return;
2154}
2155
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002156#endif /* FEAT_PERSISTENT_UNDO */
2157
2158
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159/*
2160 * If 'cpoptions' contains 'u': Undo the previous undo or redo (vi compatible).
2161 * If 'cpoptions' does not contain 'u': Always undo.
2162 */
2163 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002164u_undo(int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165{
2166 /*
2167 * If we get an undo command while executing a macro, we behave like the
2168 * original vi. If this happens twice in one macro the result will not
2169 * be compatible.
2170 */
2171 if (curbuf->b_u_synced == FALSE)
2172 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002173 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 count = 1;
2175 }
2176
2177 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
2178 undo_undoes = TRUE;
2179 else
2180 undo_undoes = !undo_undoes;
2181 u_doit(count);
2182}
2183
2184/*
2185 * If 'cpoptions' contains 'u': Repeat the previous undo or redo.
2186 * If 'cpoptions' does not contain 'u': Always redo.
2187 */
2188 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002189u_redo(int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190{
2191 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
2192 undo_undoes = FALSE;
2193 u_doit(count);
2194}
2195
2196/*
2197 * Undo or redo, depending on 'undo_undoes', 'count' times.
2198 */
2199 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002200u_doit(int startcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201{
Bram Moolenaarca003e12006-03-17 23:19:38 +00002202 int count = startcount;
2203
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002204 if (!undo_allowed())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206
2207 u_newcount = 0;
2208 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002209 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2210 u_oldcount = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 while (count--)
2212 {
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002213 /* Do the change warning now, so that it triggers FileChangedRO when
2214 * needed. This may cause the file to be reloaded, that must happen
2215 * before we do anything, because it may change curbuf->b_u_curhead
2216 * and more. */
2217 change_warning(0);
2218
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 if (undo_undoes)
2220 {
2221 if (curbuf->b_u_curhead == NULL) /* first undo */
2222 curbuf->b_u_curhead = curbuf->b_u_newhead;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002223 else if (get_undolevel() > 0) /* multi level undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 /* get next undo */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002225 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_next.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 /* nothing to undo */
2227 if (curbuf->b_u_numhead == 0 || curbuf->b_u_curhead == NULL)
2228 {
2229 /* stick curbuf->b_u_curhead at end */
2230 curbuf->b_u_curhead = curbuf->b_u_oldhead;
2231 beep_flush();
Bram Moolenaarca003e12006-03-17 23:19:38 +00002232 if (count == startcount - 1)
2233 {
2234 MSG(_("Already at oldest change"));
2235 return;
2236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 break;
2238 }
2239
Bram Moolenaarca003e12006-03-17 23:19:38 +00002240 u_undoredo(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 }
2242 else
2243 {
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002244 if (curbuf->b_u_curhead == NULL || get_undolevel() <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 {
2246 beep_flush(); /* nothing to redo */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002247 if (count == startcount - 1)
2248 {
2249 MSG(_("Already at newest change"));
2250 return;
2251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 break;
2253 }
2254
Bram Moolenaarca003e12006-03-17 23:19:38 +00002255 u_undoredo(FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002256
2257 /* Advance for next redo. Set "newhead" when at the end of the
2258 * redoable changes. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002259 if (curbuf->b_u_curhead->uh_prev.ptr == NULL)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002260 curbuf->b_u_newhead = curbuf->b_u_curhead;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002261 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_prev.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 }
2263 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00002264 u_undo_end(undo_undoes, FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002265}
2266
Bram Moolenaar1e607892006-03-13 22:15:53 +00002267/*
2268 * Undo or redo over the timeline.
2269 * When "step" is negative go back in time, otherwise goes forward in time.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002270 * When "sec" is FALSE make "step" steps, when "sec" is TRUE use "step" as
2271 * seconds.
Bram Moolenaar730cde92010-06-27 05:18:54 +02002272 * When "file" is TRUE use "step" as a number of file writes.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002273 * When "absolute" is TRUE use "step" as the sequence number to jump to.
2274 * "sec" must be FALSE then.
Bram Moolenaar1e607892006-03-13 22:15:53 +00002275 */
2276 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002277undo_time(
2278 long step,
2279 int sec,
2280 int file,
2281 int absolute)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002282{
2283 long target;
2284 long closest;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002285 long closest_start;
2286 long closest_seq = 0;
2287 long val;
Bram Moolenaarce46d932018-01-30 22:46:06 +01002288 u_header_T *uhp = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002289 u_header_T *last;
2290 int mark;
2291 int nomark;
2292 int round;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002293 int dosec = sec;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002294 int dofile = file;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002295 int above = FALSE;
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002296 int did_undo = TRUE;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002297
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002298 /* First make sure the current undoable change is synced. */
2299 if (curbuf->b_u_synced == FALSE)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002300 u_sync(TRUE);
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002301
Bram Moolenaar1e607892006-03-13 22:15:53 +00002302 u_newcount = 0;
2303 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002304 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002305 u_oldcount = -1;
2306
Bram Moolenaarca003e12006-03-17 23:19:38 +00002307 /* "target" is the node below which we want to be.
2308 * Init "closest" to a value we can't reach. */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002309 if (absolute)
2310 {
Bram Moolenaarce46d932018-01-30 22:46:06 +01002311 target = step;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002312 closest = -1;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002313 }
Bram Moolenaarca003e12006-03-17 23:19:38 +00002314 else
Bram Moolenaar1e607892006-03-13 22:15:53 +00002315 {
Bram Moolenaar730cde92010-06-27 05:18:54 +02002316 if (dosec)
Bram Moolenaarcbd4de42017-01-07 16:14:57 +01002317 target = (long)(curbuf->b_u_time_cur) + step;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002318 else if (dofile)
2319 {
2320 if (step < 0)
2321 {
2322 /* Going back to a previous write. If there were changes after
2323 * the last write, count that as moving one file-write, so
2324 * that ":earlier 1f" undoes all changes since the last save. */
2325 uhp = curbuf->b_u_curhead;
2326 if (uhp != NULL)
2327 uhp = uhp->uh_next.ptr;
2328 else
2329 uhp = curbuf->b_u_newhead;
2330 if (uhp != NULL && uhp->uh_save_nr != 0)
2331 /* "uh_save_nr" was set in the last block, that means
2332 * there were no changes since the last write */
2333 target = curbuf->b_u_save_nr_cur + step;
2334 else
2335 /* count the changes since the last write as one step */
2336 target = curbuf->b_u_save_nr_cur + step + 1;
2337 if (target <= 0)
2338 /* Go to before first write: before the oldest change. Use
2339 * the sequence number for that. */
2340 dofile = FALSE;
2341 }
2342 else
2343 {
2344 /* Moving forward to a newer write. */
2345 target = curbuf->b_u_save_nr_cur + step;
2346 if (target > curbuf->b_u_save_nr_last)
2347 {
2348 /* Go to after last write: after the latest change. Use
2349 * the sequence number for that. */
2350 target = curbuf->b_u_seq_last + 1;
2351 dofile = FALSE;
2352 }
2353 }
2354 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002355 else
2356 target = curbuf->b_u_seq_cur + step;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002357 if (step < 0)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002358 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002359 if (target < 0)
2360 target = 0;
2361 closest = -1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002362 }
2363 else
2364 {
Bram Moolenaar730cde92010-06-27 05:18:54 +02002365 if (dosec)
Bram Moolenaarcbd4de42017-01-07 16:14:57 +01002366 closest = (long)(vim_time() + 1);
Bram Moolenaar730cde92010-06-27 05:18:54 +02002367 else if (dofile)
2368 closest = curbuf->b_u_save_nr_last + 2;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002369 else
2370 closest = curbuf->b_u_seq_last + 2;
2371 if (target >= closest)
2372 target = closest - 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002373 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002374 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002375 closest_start = closest;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002376 closest_seq = curbuf->b_u_seq_cur;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002377
Bram Moolenaarce46d932018-01-30 22:46:06 +01002378 /* When "target" is 0; Back to origin. */
2379 if (target == 0)
Bram Moolenaar059fd012018-01-31 14:25:53 +01002380 {
2381 mark = lastmark; /* avoid that GCC complains */
2382 goto target_zero;
2383 }
Bram Moolenaarce46d932018-01-30 22:46:06 +01002384
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002385 /*
2386 * May do this twice:
Bram Moolenaar1e607892006-03-13 22:15:53 +00002387 * 1. Search for "target", update "closest" to the best match found.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002388 * 2. If "target" not found search for "closest".
2389 *
2390 * When using the closest time we use the sequence number in the second
2391 * round, because there may be several entries with the same time.
2392 */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002393 for (round = 1; round <= 2; ++round)
2394 {
2395 /* Find the path from the current state to where we want to go. The
2396 * desired state can be anywhere in the undo tree, need to go all over
2397 * it. We put "nomark" in uh_walk where we have been without success,
2398 * "mark" where it could possibly be. */
2399 mark = ++lastmark;
2400 nomark = ++lastmark;
2401
2402 if (curbuf->b_u_curhead == NULL) /* at leaf of the tree */
2403 uhp = curbuf->b_u_newhead;
2404 else
2405 uhp = curbuf->b_u_curhead;
2406
2407 while (uhp != NULL)
2408 {
2409 uhp->uh_walk = mark;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002410 if (dosec)
Bram Moolenaarcbd4de42017-01-07 16:14:57 +01002411 val = (long)(uhp->uh_time);
Bram Moolenaar730cde92010-06-27 05:18:54 +02002412 else if (dofile)
2413 val = uhp->uh_save_nr;
2414 else
2415 val = uhp->uh_seq;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002416
Bram Moolenaar730cde92010-06-27 05:18:54 +02002417 if (round == 1 && !(dofile && val == 0))
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002418 {
2419 /* Remember the header that is closest to the target.
2420 * It must be at least in the right direction (checked with
Bram Moolenaarca003e12006-03-17 23:19:38 +00002421 * "b_u_seq_cur"). When the timestamp is equal find the
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002422 * highest/lowest sequence number. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002423 if ((step < 0 ? uhp->uh_seq <= curbuf->b_u_seq_cur
2424 : uhp->uh_seq > curbuf->b_u_seq_cur)
2425 && ((dosec && val == closest)
2426 ? (step < 0
2427 ? uhp->uh_seq < closest_seq
2428 : uhp->uh_seq > closest_seq)
2429 : closest == closest_start
2430 || (val > target
2431 ? (closest > target
2432 ? val - target <= closest - target
2433 : val - target <= target - closest)
2434 : (closest > target
2435 ? target - val <= closest - target
2436 : target - val <= target - closest))))
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002437 {
2438 closest = val;
2439 closest_seq = uhp->uh_seq;
2440 }
2441 }
2442
2443 /* Quit searching when we found a match. But when searching for a
2444 * time we need to continue looking for the best uh_seq. */
2445 if (target == val && !dosec)
Bram Moolenaar730cde92010-06-27 05:18:54 +02002446 {
2447 target = uhp->uh_seq;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002448 break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002449 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002450
2451 /* go down in the tree if we haven't been there */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002452 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != nomark
2453 && uhp->uh_prev.ptr->uh_walk != mark)
2454 uhp = uhp->uh_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002455
2456 /* go to alternate branch if we haven't been there */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002457 else if (uhp->uh_alt_next.ptr != NULL
2458 && uhp->uh_alt_next.ptr->uh_walk != nomark
2459 && uhp->uh_alt_next.ptr->uh_walk != mark)
2460 uhp = uhp->uh_alt_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002461
2462 /* go up in the tree if we haven't been there and we are at the
2463 * start of alternate branches */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002464 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
2465 && uhp->uh_next.ptr->uh_walk != nomark
2466 && uhp->uh_next.ptr->uh_walk != mark)
Bram Moolenaardb552d602006-03-23 22:59:57 +00002467 {
2468 /* If still at the start we don't go through this change. */
2469 if (uhp == curbuf->b_u_curhead)
2470 uhp->uh_walk = nomark;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002471 uhp = uhp->uh_next.ptr;
Bram Moolenaardb552d602006-03-23 22:59:57 +00002472 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002473
2474 else
2475 {
2476 /* need to backtrack; mark this node as useless */
2477 uhp->uh_walk = nomark;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002478 if (uhp->uh_alt_prev.ptr != NULL)
2479 uhp = uhp->uh_alt_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002480 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002481 uhp = uhp->uh_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002482 }
2483 }
2484
2485 if (uhp != NULL) /* found it */
2486 break;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002487
2488 if (absolute)
2489 {
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002490 EMSGN(_("E830: Undo number %ld not found"), step);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002491 return;
2492 }
2493
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002494 if (closest == closest_start)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002495 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002496 if (step < 0)
2497 MSG(_("Already at oldest change"));
2498 else
2499 MSG(_("Already at newest change"));
Bram Moolenaar1e607892006-03-13 22:15:53 +00002500 return;
2501 }
2502
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002503 target = closest_seq;
2504 dosec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002505 dofile = FALSE;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002506 if (step < 0)
2507 above = TRUE; /* stop above the header */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002508 }
2509
Bram Moolenaar059fd012018-01-31 14:25:53 +01002510target_zero:
Bram Moolenaar1e607892006-03-13 22:15:53 +00002511 /* If we found it: Follow the path to go to where we want to be. */
Bram Moolenaarce46d932018-01-30 22:46:06 +01002512 if (uhp != NULL || target == 0)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002513 {
2514 /*
2515 * First go up the tree as much as needed.
2516 */
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002517 while (!got_int)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002518 {
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002519 /* Do the change warning now, for the same reason as above. */
2520 change_warning(0);
2521
Bram Moolenaar1e607892006-03-13 22:15:53 +00002522 uhp = curbuf->b_u_curhead;
2523 if (uhp == NULL)
2524 uhp = curbuf->b_u_newhead;
2525 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002526 uhp = uhp->uh_next.ptr;
Bram Moolenaarce46d932018-01-30 22:46:06 +01002527 if (uhp == NULL || (target > 0 && uhp->uh_walk != mark)
Bram Moolenaarca003e12006-03-17 23:19:38 +00002528 || (uhp->uh_seq == target && !above))
Bram Moolenaar1e607892006-03-13 22:15:53 +00002529 break;
2530 curbuf->b_u_curhead = uhp;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002531 u_undoredo(TRUE);
Bram Moolenaarce46d932018-01-30 22:46:06 +01002532 if (target > 0)
2533 uhp->uh_walk = nomark; /* don't go back down here */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002534 }
2535
Bram Moolenaarce46d932018-01-30 22:46:06 +01002536 /* When back to origin, redo is not needed. */
2537 if (target > 0)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002538 {
Bram Moolenaarce46d932018-01-30 22:46:06 +01002539 /*
2540 * And now go down the tree (redo), branching off where needed.
2541 */
2542 while (!got_int)
2543 {
2544 /* Do the change warning now, for the same reason as above. */
2545 change_warning(0);
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002546
Bram Moolenaarce46d932018-01-30 22:46:06 +01002547 uhp = curbuf->b_u_curhead;
2548 if (uhp == NULL)
2549 break;
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002550
Bram Moolenaarce46d932018-01-30 22:46:06 +01002551 /* Go back to the first branch with a mark. */
2552 while (uhp->uh_alt_prev.ptr != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002553 && uhp->uh_alt_prev.ptr->uh_walk == mark)
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002554 uhp = uhp->uh_alt_prev.ptr;
Bram Moolenaarce46d932018-01-30 22:46:06 +01002555
2556 /* Find the last branch with a mark, that's the one. */
2557 last = uhp;
2558 while (last->uh_alt_next.ptr != NULL
2559 && last->uh_alt_next.ptr->uh_walk == mark)
2560 last = last->uh_alt_next.ptr;
2561 if (last != uhp)
2562 {
2563 /* Make the used branch the first entry in the list of
2564 * alternatives to make "u" and CTRL-R take this branch. */
2565 while (uhp->uh_alt_prev.ptr != NULL)
2566 uhp = uhp->uh_alt_prev.ptr;
2567 if (last->uh_alt_next.ptr != NULL)
2568 last->uh_alt_next.ptr->uh_alt_prev.ptr =
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002569 last->uh_alt_prev.ptr;
Bram Moolenaarce46d932018-01-30 22:46:06 +01002570 last->uh_alt_prev.ptr->uh_alt_next.ptr =
2571 last->uh_alt_next.ptr;
2572 last->uh_alt_prev.ptr = NULL;
2573 last->uh_alt_next.ptr = uhp;
2574 uhp->uh_alt_prev.ptr = last;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002575
Bram Moolenaarce46d932018-01-30 22:46:06 +01002576 if (curbuf->b_u_oldhead == uhp)
2577 curbuf->b_u_oldhead = last;
2578 uhp = last;
2579 if (uhp->uh_next.ptr != NULL)
2580 uhp->uh_next.ptr->uh_prev.ptr = uhp;
2581 }
2582 curbuf->b_u_curhead = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002583
Bram Moolenaarce46d932018-01-30 22:46:06 +01002584 if (uhp->uh_walk != mark)
2585 break; /* must have reached the target */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002586
Bram Moolenaarce46d932018-01-30 22:46:06 +01002587 /* Stop when going backwards in time and didn't find the exact
2588 * header we were looking for. */
2589 if (uhp->uh_seq == target && above)
2590 {
2591 curbuf->b_u_seq_cur = target - 1;
2592 break;
2593 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002594
Bram Moolenaarce46d932018-01-30 22:46:06 +01002595 u_undoredo(FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002596
Bram Moolenaarce46d932018-01-30 22:46:06 +01002597 /* Advance "curhead" to below the header we last used. If it
2598 * becomes NULL then we need to set "newhead" to this leaf. */
2599 if (uhp->uh_prev.ptr == NULL)
2600 curbuf->b_u_newhead = uhp;
2601 curbuf->b_u_curhead = uhp->uh_prev.ptr;
2602 did_undo = FALSE;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002603
Bram Moolenaarce46d932018-01-30 22:46:06 +01002604 if (uhp->uh_seq == target) /* found it! */
2605 break;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002606
Bram Moolenaarce46d932018-01-30 22:46:06 +01002607 uhp = uhp->uh_prev.ptr;
2608 if (uhp == NULL || uhp->uh_walk != mark)
2609 {
2610 /* Need to redo more but can't find it... */
2611 internal_error("undo_time()");
2612 break;
2613 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002614 }
2615 }
2616 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00002617 u_undo_end(did_undo, absolute);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618}
2619
2620/*
2621 * u_undoredo: common code for undo and redo
2622 *
2623 * The lines in the file are replaced by the lines in the entry list at
2624 * curbuf->b_u_curhead. The replaced lines in the file are saved in the entry
2625 * list for the next undo/redo.
Bram Moolenaarca003e12006-03-17 23:19:38 +00002626 *
2627 * When "undo" is TRUE we go up in the tree, when FALSE we go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 */
2629 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002630u_undoredo(int undo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631{
Bram Moolenaarccae4672019-01-04 15:09:57 +01002632 undoline_T *newarray = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 linenr_T oldsize;
2634 linenr_T newsize;
2635 linenr_T top, bot;
2636 linenr_T lnum;
2637 linenr_T newlnum = MAXLNUM;
2638 long i;
2639 u_entry_T *uep, *nuep;
2640 u_entry_T *newlist = NULL;
2641 int old_flags;
2642 int new_flags;
2643 pos_T namedm[NMARKS];
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002644 visualinfo_T visualinfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 int empty_buffer; /* buffer became empty */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002646 u_header_T *curhead = curbuf->b_u_curhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002648 /* Don't want autocommands using the undo structures here, they are
2649 * invalid till the end. */
2650 block_autocmds();
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002651
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002652#ifdef U_DEBUG
2653 u_check(FALSE);
2654#endif
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002655 old_flags = curhead->uh_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 new_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
2657 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
2658 setpcmark();
2659
2660 /*
2661 * save marks before undo/redo
2662 */
2663 mch_memmove(namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002664 visualinfo = curbuf->b_visual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 curbuf->b_op_start.lnum = curbuf->b_ml.ml_line_count;
2666 curbuf->b_op_start.col = 0;
2667 curbuf->b_op_end.lnum = 0;
2668 curbuf->b_op_end.col = 0;
2669
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002670 for (uep = curhead->uh_entry; uep != NULL; uep = nuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 {
2672 top = uep->ue_top;
2673 bot = uep->ue_bot;
2674 if (bot == 0)
2675 bot = curbuf->b_ml.ml_line_count + 1;
2676 if (top > curbuf->b_ml.ml_line_count || top >= bot
2677 || bot > curbuf->b_ml.ml_line_count + 1)
2678 {
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002679 unblock_autocmds();
Bram Moolenaar95f09602016-11-10 20:01:45 +01002680 IEMSG(_("E438: u_undo: line numbers wrong"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 changed(); /* don't want UNCHANGED now */
2682 return;
2683 }
2684
2685 oldsize = bot - top - 1; /* number of lines before undo */
2686 newsize = uep->ue_size; /* number of lines after undo */
2687
2688 if (top < newlnum)
2689 {
2690 /* If the saved cursor is somewhere in this undo block, move it to
2691 * the remembered position. Makes "gwap" put the cursor back
2692 * where it was. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002693 lnum = curhead->uh_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 if (lnum >= top && lnum <= top + newsize + 1)
2695 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002696 curwin->w_cursor = curhead->uh_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 newlnum = curwin->w_cursor.lnum - 1;
2698 }
2699 else
2700 {
2701 /* Use the first line that actually changed. Avoids that
2702 * undoing auto-formatting puts the cursor in the previous
2703 * line. */
2704 for (i = 0; i < newsize && i < oldsize; ++i)
Bram Moolenaarccae4672019-01-04 15:09:57 +01002705 {
2706 char_u *p = ml_get(top + 1 + i);
2707
2708 if (curbuf->b_ml.ml_line_len != uep->ue_array[i].ul_len
2709 || memcmp(uep->ue_array[i].ul_line, p, curbuf->b_ml.ml_line_len) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 break;
Bram Moolenaarccae4672019-01-04 15:09:57 +01002711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 if (i == newsize && newlnum == MAXLNUM && uep->ue_next == NULL)
2713 {
2714 newlnum = top;
2715 curwin->w_cursor.lnum = newlnum + 1;
2716 }
2717 else if (i < newsize)
2718 {
2719 newlnum = top + i;
2720 curwin->w_cursor.lnum = newlnum + 1;
2721 }
2722 }
2723 }
2724
2725 empty_buffer = FALSE;
2726
2727 /* delete the lines between top and bot and save them in newarray */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002728 if (oldsize > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 {
Bram Moolenaarccae4672019-01-04 15:09:57 +01002730 if ((newarray = (undoline_T *)U_ALLOC_LINE(
2731 sizeof(undoline_T) * oldsize)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 {
Bram Moolenaarccae4672019-01-04 15:09:57 +01002733 do_outofmem_msg((long_u)(sizeof(undoline_T) * oldsize));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 /*
2735 * We have messed up the entry list, repair is impossible.
2736 * we have to free the rest of the list.
2737 */
2738 while (uep != NULL)
2739 {
2740 nuep = uep->ue_next;
2741 u_freeentry(uep, uep->ue_size);
2742 uep = nuep;
2743 }
2744 break;
2745 }
2746 /* delete backwards, it goes faster in most cases */
2747 for (lnum = bot - 1, i = oldsize; --i >= 0; --lnum)
2748 {
2749 /* what can we do when we run out of memory? */
Bram Moolenaarccae4672019-01-04 15:09:57 +01002750 if (u_save_line(&newarray[i], lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 do_outofmem_msg((long_u)0);
2752 /* remember we deleted the last line in the buffer, and a
2753 * dummy empty line will be inserted */
2754 if (curbuf->b_ml.ml_line_count == 1)
2755 empty_buffer = TRUE;
2756 ml_delete(lnum, FALSE);
2757 }
2758 }
Bram Moolenaar8d343302005-07-12 22:46:17 +00002759 else
2760 newarray = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761
2762 /* insert the lines in u_array between top and bot */
2763 if (newsize)
2764 {
2765 for (lnum = top, i = 0; i < newsize; ++i, ++lnum)
2766 {
Bram Moolenaarccae4672019-01-04 15:09:57 +01002767 // If the file is empty, there is an empty line 1 that we
2768 // should get rid of, by replacing it with the new line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 if (empty_buffer && lnum == 0)
Bram Moolenaarccae4672019-01-04 15:09:57 +01002770 ml_replace_len((linenr_T)1, uep->ue_array[i].ul_line, uep->ue_array[i].ul_len, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 else
Bram Moolenaarccae4672019-01-04 15:09:57 +01002772 ml_append(lnum, uep->ue_array[i].ul_line, (colnr_T)uep->ue_array[i].ul_len, FALSE);
2773 vim_free(uep->ue_array[i].ul_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 }
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002775 vim_free((char_u *)uep->ue_array);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 }
2777
2778 /* adjust marks */
2779 if (oldsize != newsize)
2780 {
2781 mark_adjust(top + 1, top + oldsize, (long)MAXLNUM,
2782 (long)newsize - (long)oldsize);
2783 if (curbuf->b_op_start.lnum > top + oldsize)
2784 curbuf->b_op_start.lnum += newsize - oldsize;
2785 if (curbuf->b_op_end.lnum > top + oldsize)
2786 curbuf->b_op_end.lnum += newsize - oldsize;
2787 }
2788
2789 changed_lines(top + 1, 0, bot, newsize - oldsize);
2790
2791 /* set '[ and '] mark */
2792 if (top + 1 < curbuf->b_op_start.lnum)
2793 curbuf->b_op_start.lnum = top + 1;
2794 if (newsize == 0 && top + 1 > curbuf->b_op_end.lnum)
2795 curbuf->b_op_end.lnum = top + 1;
2796 else if (top + newsize > curbuf->b_op_end.lnum)
2797 curbuf->b_op_end.lnum = top + newsize;
2798
2799 u_newcount += newsize;
2800 u_oldcount += oldsize;
2801 uep->ue_size = oldsize;
2802 uep->ue_array = newarray;
2803 uep->ue_bot = top + newsize + 1;
2804
2805 /*
2806 * insert this entry in front of the new entry list
2807 */
2808 nuep = uep->ue_next;
2809 uep->ue_next = newlist;
2810 newlist = uep;
2811 }
2812
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002813 curhead->uh_entry = newlist;
2814 curhead->uh_flags = new_flags;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002815 if ((old_flags & UH_EMPTYBUF) && BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 curbuf->b_ml.ml_flags |= ML_EMPTY;
2817 if (old_flags & UH_CHANGED)
2818 changed();
2819 else
Bram Moolenaar009b2592004-10-24 19:18:58 +00002820#ifdef FEAT_NETBEANS_INTG
2821 /* per netbeans undo rules, keep it as modified */
2822 if (!isNetbeansModified(curbuf))
2823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 unchanged(curbuf, FALSE);
2825
2826 /*
2827 * restore marks from before undo/redo
2828 */
2829 for (i = 0; i < NMARKS; ++i)
Bram Moolenaarf65aad52015-02-17 13:43:40 +01002830 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002831 if (curhead->uh_namedm[i].lnum != 0)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002832 curbuf->b_namedm[i] = curhead->uh_namedm[i];
Bram Moolenaarf65aad52015-02-17 13:43:40 +01002833 if (namedm[i].lnum != 0)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002834 curhead->uh_namedm[i] = namedm[i];
Bram Moolenaarf65aad52015-02-17 13:43:40 +01002835 else
2836 curhead->uh_namedm[i].lnum = 0;
2837 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002838 if (curhead->uh_visual.vi_start.lnum != 0)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002839 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002840 curbuf->b_visual = curhead->uh_visual;
2841 curhead->uh_visual = visualinfo;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843
2844 /*
2845 * If the cursor is only off by one line, put it at the same position as
2846 * before starting the change (for the "o" command).
2847 * Otherwise the cursor should go to the first undone line.
2848 */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002849 if (curhead->uh_cursor.lnum + 1 == curwin->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 && curwin->w_cursor.lnum > 1)
2851 --curwin->w_cursor.lnum;
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002852 if (curwin->w_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 {
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002854 if (curhead->uh_cursor.lnum == curwin->w_cursor.lnum)
2855 {
2856 curwin->w_cursor.col = curhead->uh_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002858 if (virtual_active() && curhead->uh_cursor_vcol >= 0)
2859 coladvance((colnr_T)curhead->uh_cursor_vcol);
2860 else
2861 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862#endif
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002863 }
2864 else
2865 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 else
2868 {
2869 /* We get here with the current cursor line being past the end (eg
2870 * after adding lines at the end of the file, and then undoing it).
2871 * check_cursor() will move the cursor to the last line. Move it to
2872 * the first column here. */
2873 curwin->w_cursor.col = 0;
2874#ifdef FEAT_VIRTUALEDIT
2875 curwin->w_cursor.coladd = 0;
2876#endif
2877 }
2878
2879 /* Make sure the cursor is on an existing line and column. */
2880 check_cursor();
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002881
2882 /* Remember where we are for "g-" and ":earlier 10s". */
2883 curbuf->b_u_seq_cur = curhead->uh_seq;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002884 if (undo)
Bram Moolenaar80eaddd2017-11-11 23:37:08 +01002885 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002886 /* We are below the previous undo. However, to make ":earlier 1s"
2887 * work we compute this as being just above the just undone change. */
Bram Moolenaar80eaddd2017-11-11 23:37:08 +01002888 if (curhead->uh_next.ptr != NULL)
2889 curbuf->b_u_seq_cur = curhead->uh_next.ptr->uh_seq;
2890 else
2891 curbuf->b_u_seq_cur = 0;
2892 }
Bram Moolenaarca003e12006-03-17 23:19:38 +00002893
Bram Moolenaar730cde92010-06-27 05:18:54 +02002894 /* Remember where we are for ":earlier 1f" and ":later 1f". */
2895 if (curhead->uh_save_nr != 0)
2896 {
2897 if (undo)
2898 curbuf->b_u_save_nr_cur = curhead->uh_save_nr - 1;
2899 else
2900 curbuf->b_u_save_nr_cur = curhead->uh_save_nr;
2901 }
2902
Bram Moolenaarca003e12006-03-17 23:19:38 +00002903 /* The timestamp can be the same for multiple changes, just use the one of
2904 * the undone/redone change. */
Bram Moolenaara800b422010-06-27 01:15:55 +02002905 curbuf->b_u_time_cur = curhead->uh_time;
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002906
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002907 unblock_autocmds();
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002908#ifdef U_DEBUG
2909 u_check(FALSE);
2910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911}
2912
2913/*
2914 * If we deleted or added lines, report the number of less/more lines.
2915 * Otherwise, report the number of changes (this may be incorrect
2916 * in some cases, but it's better than nothing).
2917 */
2918 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002919u_undo_end(
2920 int did_undo, /* just did an undo */
2921 int absolute) /* used ":undo N" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922{
Bram Moolenaar89d40322006-08-29 15:30:07 +00002923 char *msgstr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002924 u_header_T *uhp;
2925 char_u msgbuf[80];
Bram Moolenaar1e607892006-03-13 22:15:53 +00002926
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927#ifdef FEAT_FOLDING
2928 if ((fdo_flags & FDO_UNDO) && KeyTyped)
2929 foldOpenCursor();
2930#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +00002931
2932 if (global_busy /* no messages now, wait until global is finished */
2933 || !messaging()) /* 'lazyredraw' set, don't do messages now */
2934 return;
2935
2936 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2937 --u_newcount;
2938
2939 u_oldcount -= u_newcount;
2940 if (u_oldcount == -1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002941 msgstr = N_("more line");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002942 else if (u_oldcount < 0)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002943 msgstr = N_("more lines");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002944 else if (u_oldcount == 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002945 msgstr = N_("line less");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002946 else if (u_oldcount > 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002947 msgstr = N_("fewer lines");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002948 else
2949 {
2950 u_oldcount = u_newcount;
2951 if (u_newcount == 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002952 msgstr = N_("change");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002953 else
Bram Moolenaar89d40322006-08-29 15:30:07 +00002954 msgstr = N_("changes");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002955 }
2956
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002957 if (curbuf->b_u_curhead != NULL)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002958 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00002959 /* For ":undo N" we prefer a "after #N" message. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002960 if (absolute && curbuf->b_u_curhead->uh_next.ptr != NULL)
Bram Moolenaardb552d602006-03-23 22:59:57 +00002961 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002962 uhp = curbuf->b_u_curhead->uh_next.ptr;
Bram Moolenaardb552d602006-03-23 22:59:57 +00002963 did_undo = FALSE;
2964 }
2965 else if (did_undo)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002966 uhp = curbuf->b_u_curhead;
2967 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002968 uhp = curbuf->b_u_curhead->uh_next.ptr;
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002969 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002970 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002971 uhp = curbuf->b_u_newhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002972
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002973 if (uhp == NULL)
2974 *msgbuf = NUL;
2975 else
2976 u_add_time(msgbuf, sizeof(msgbuf), uhp->uh_time);
2977
Bram Moolenaarb2c03502010-07-02 20:20:09 +02002978#ifdef FEAT_CONCEAL
2979 {
2980 win_T *wp;
2981
2982 FOR_ALL_WINDOWS(wp)
2983 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02002984 if (wp->w_buffer == curbuf && wp->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02002985 redraw_win_later(wp, NOT_VALID);
2986 }
2987 }
2988#endif
2989
Bram Moolenaare0429682018-07-01 16:44:03 +02002990 smsg_attr_keep(0, (char_u *)_("%ld %s; %s #%ld %s"),
Bram Moolenaarca003e12006-03-17 23:19:38 +00002991 u_oldcount < 0 ? -u_oldcount : u_oldcount,
Bram Moolenaar89d40322006-08-29 15:30:07 +00002992 _(msgstr),
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002993 did_undo ? _("before") : _("after"),
2994 uhp == NULL ? 0L : uhp->uh_seq,
2995 msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996}
2997
2998/*
2999 * u_sync: stop adding to the current entry list
3000 */
3001 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003002u_sync(
3003 int force) /* Also sync when no_u_sync is set. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004{
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003005 /* Skip it when already synced or syncing is disabled. */
3006 if (curbuf->b_u_synced || (!force && no_u_sync > 0))
3007 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003009 if (p_imst == IM_ON_THE_SPOT && im_is_preediting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 return; /* XIM is busy, don't break an undo sequence */
3011#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003012 if (get_undolevel() < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 curbuf->b_u_synced = TRUE; /* no entries, nothing to do */
3014 else
3015 {
3016 u_getbot(); /* compute ue_bot of previous u_save */
3017 curbuf->b_u_curhead = NULL;
3018 }
3019}
3020
3021/*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003022 * ":undolist": List the leafs of the undo tree
3023 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003024 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003025ex_undolist(exarg_T *eap UNUSED)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003026{
3027 garray_T ga;
3028 u_header_T *uhp;
3029 int mark;
3030 int nomark;
3031 int changes = 1;
3032 int i;
3033
3034 /*
3035 * 1: walk the tree to find all leafs, put the info in "ga".
3036 * 2: sort the lines
3037 * 3: display the list
3038 */
3039 mark = ++lastmark;
3040 nomark = ++lastmark;
3041 ga_init2(&ga, (int)sizeof(char *), 20);
3042
3043 uhp = curbuf->b_u_oldhead;
3044 while (uhp != NULL)
3045 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003046 if (uhp->uh_prev.ptr == NULL && uhp->uh_walk != nomark
Bram Moolenaarca003e12006-03-17 23:19:38 +00003047 && uhp->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003048 {
3049 if (ga_grow(&ga, 1) == FAIL)
3050 break;
Bram Moolenaarea391762018-04-08 13:07:22 +02003051 vim_snprintf((char *)IObuff, IOSIZE, "%6ld %7d ",
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003052 uhp->uh_seq, changes);
3053 u_add_time(IObuff + STRLEN(IObuff), IOSIZE - STRLEN(IObuff),
3054 uhp->uh_time);
Bram Moolenaara800b422010-06-27 01:15:55 +02003055 if (uhp->uh_save_nr > 0)
3056 {
Bram Moolenaardba01a02010-11-03 19:32:42 +01003057 while (STRLEN(IObuff) < 33)
Bram Moolenaara800b422010-06-27 01:15:55 +02003058 STRCAT(IObuff, " ");
3059 vim_snprintf_add((char *)IObuff, IOSIZE,
3060 " %3ld", uhp->uh_save_nr);
3061 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003062 ((char_u **)(ga.ga_data))[ga.ga_len++] = vim_strsave(IObuff);
3063 }
3064
3065 uhp->uh_walk = mark;
3066
3067 /* go down in the tree if we haven't been there */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003068 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != nomark
3069 && uhp->uh_prev.ptr->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003070 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003071 uhp = uhp->uh_prev.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003072 ++changes;
3073 }
3074
3075 /* go to alternate branch if we haven't been there */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003076 else if (uhp->uh_alt_next.ptr != NULL
3077 && uhp->uh_alt_next.ptr->uh_walk != nomark
3078 && uhp->uh_alt_next.ptr->uh_walk != mark)
3079 uhp = uhp->uh_alt_next.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003080
3081 /* go up in the tree if we haven't been there and we are at the
3082 * start of alternate branches */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003083 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
3084 && uhp->uh_next.ptr->uh_walk != nomark
3085 && uhp->uh_next.ptr->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003086 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003087 uhp = uhp->uh_next.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003088 --changes;
3089 }
3090
3091 else
3092 {
3093 /* need to backtrack; mark this node as done */
3094 uhp->uh_walk = nomark;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003095 if (uhp->uh_alt_prev.ptr != NULL)
3096 uhp = uhp->uh_alt_prev.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003097 else
3098 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003099 uhp = uhp->uh_next.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003100 --changes;
3101 }
3102 }
3103 }
3104
3105 if (ga.ga_len == 0)
3106 MSG(_("Nothing to undo"));
3107 else
3108 {
3109 sort_strings((char_u **)ga.ga_data, ga.ga_len);
3110
3111 msg_start();
Bram Moolenaardba01a02010-11-03 19:32:42 +01003112 msg_puts_attr((char_u *)_("number changes when saved"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01003113 HL_ATTR(HLF_T));
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003114 for (i = 0; i < ga.ga_len && !got_int; ++i)
3115 {
3116 msg_putchar('\n');
3117 if (got_int)
3118 break;
3119 msg_puts(((char_u **)ga.ga_data)[i]);
3120 }
3121 msg_end();
3122
3123 ga_clear_strings(&ga);
3124 }
3125}
3126
3127/*
3128 * Put the timestamp of an undo header in "buf[buflen]" in a nice format.
3129 */
3130 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003131u_add_time(char_u *buf, size_t buflen, time_t tt)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003132{
3133#ifdef HAVE_STRFTIME
3134 struct tm *curtime;
3135
Bram Moolenaar170b10b2016-07-29 16:15:27 +02003136 if (vim_time() - tt >= 100)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003137 {
3138 curtime = localtime(&tt);
Bram Moolenaar170b10b2016-07-29 16:15:27 +02003139 if (vim_time() - tt < (60L * 60L * 12L))
Bram Moolenaardba01a02010-11-03 19:32:42 +01003140 /* within 12 hours */
3141 (void)strftime((char *)buf, buflen, "%H:%M:%S", curtime);
Bram Moolenaardba01a02010-11-03 19:32:42 +01003142 else
Bram Moolenaarb9ce83e2012-08-23 12:59:02 +02003143 /* longer ago */
Bram Moolenaar5e3d6ca2011-01-22 21:25:11 +01003144 (void)strftime((char *)buf, buflen, "%Y/%m/%d %H:%M:%S", curtime);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003145 }
3146 else
3147#endif
Bram Moolenaarfd6100b2018-08-21 17:07:45 +02003148 {
3149 long seconds = (long)(vim_time() - tt);
3150
3151 vim_snprintf((char *)buf, buflen,
3152 NGETTEXT("%ld second ago", "%ld seconds ago", seconds),
3153 seconds);
3154 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003155}
3156
3157/*
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003158 * ":undojoin": continue adding to the last entry list
3159 */
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003160 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003161ex_undojoin(exarg_T *eap UNUSED)
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003162{
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003163 if (curbuf->b_u_newhead == NULL)
3164 return; /* nothing changed before */
Bram Moolenaar57657d82006-04-21 22:12:41 +00003165 if (curbuf->b_u_curhead != NULL)
3166 {
3167 EMSG(_("E790: undojoin is not allowed after undo"));
3168 return;
3169 }
3170 if (!curbuf->b_u_synced)
3171 return; /* already unsynced */
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003172 if (get_undolevel() < 0)
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003173 return; /* no entries, nothing to do */
3174 else
Bram Moolenaar5e4e1b12017-01-17 22:09:45 +01003175 /* Append next change to the last entry */
3176 curbuf->b_u_synced = FALSE;
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003177}
3178
3179/*
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003180 * Called after writing or reloading the file and setting b_changed to FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 * Now an undo means that the buffer is modified.
3182 */
3183 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003184u_unchanged(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185{
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003186 u_unch_branch(buf->b_u_oldhead);
3187 buf->b_did_warn = FALSE;
3188}
3189
Bram Moolenaar730cde92010-06-27 05:18:54 +02003190/*
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003191 * After reloading a buffer which was saved for 'undoreload': Find the first
3192 * line that was changed and set the cursor there.
3193 */
3194 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003195u_find_first_changed(void)
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003196{
3197 u_header_T *uhp = curbuf->b_u_newhead;
3198 u_entry_T *uep;
3199 linenr_T lnum;
3200
3201 if (curbuf->b_u_curhead != NULL || uhp == NULL)
3202 return; /* undid something in an autocmd? */
3203
3204 /* Check that the last undo block was for the whole file. */
3205 uep = uhp->uh_entry;
3206 if (uep->ue_top != 0 || uep->ue_bot != 0)
3207 return;
3208
3209 for (lnum = 1; lnum < curbuf->b_ml.ml_line_count
3210 && lnum <= uep->ue_size; ++lnum)
Bram Moolenaarccae4672019-01-04 15:09:57 +01003211 {
3212 char_u *p = ml_get_buf(curbuf, lnum, FALSE);
3213
3214 if (uep->ue_array[lnum - 1].ul_len != curbuf->b_ml.ml_line_len
3215 || memcmp(p, uep->ue_array[lnum - 1].ul_line, uep->ue_array[lnum - 1].ul_len) != 0)
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003216 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003217 CLEAR_POS(&(uhp->uh_cursor));
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003218 uhp->uh_cursor.lnum = lnum;
3219 return;
3220 }
Bram Moolenaarccae4672019-01-04 15:09:57 +01003221 }
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003222 if (curbuf->b_ml.ml_line_count != uep->ue_size)
3223 {
3224 /* lines added or deleted at the end, put the cursor there */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003225 CLEAR_POS(&(uhp->uh_cursor));
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003226 uhp->uh_cursor.lnum = lnum;
3227 }
3228}
3229
3230/*
Bram Moolenaar730cde92010-06-27 05:18:54 +02003231 * Increase the write count, store it in the last undo header, what would be
3232 * used for "u".
3233 */
3234 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003235u_update_save_nr(buf_T *buf)
Bram Moolenaar730cde92010-06-27 05:18:54 +02003236{
3237 u_header_T *uhp;
3238
3239 ++buf->b_u_save_nr_last;
3240 buf->b_u_save_nr_cur = buf->b_u_save_nr_last;
3241 uhp = buf->b_u_curhead;
3242 if (uhp != NULL)
3243 uhp = uhp->uh_next.ptr;
3244 else
3245 uhp = buf->b_u_newhead;
3246 if (uhp != NULL)
3247 uhp->uh_save_nr = buf->b_u_save_nr_last;
3248}
3249
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003250 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003251u_unch_branch(u_header_T *uhp)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003252{
Bram Moolenaar1e607892006-03-13 22:15:53 +00003253 u_header_T *uh;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003255 for (uh = uhp; uh != NULL; uh = uh->uh_prev.ptr)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003256 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 uh->uh_flags |= UH_CHANGED;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003258 if (uh->uh_alt_next.ptr != NULL)
3259 u_unch_branch(uh->uh_alt_next.ptr); /* recursive */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261}
3262
3263/*
3264 * Get pointer to last added entry.
3265 * If it's not valid, give an error message and return NULL.
3266 */
3267 static u_entry_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003268u_get_headentry(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269{
3270 if (curbuf->b_u_newhead == NULL || curbuf->b_u_newhead->uh_entry == NULL)
3271 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003272 IEMSG(_("E439: undo list corrupt"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 return NULL;
3274 }
3275 return curbuf->b_u_newhead->uh_entry;
3276}
3277
3278/*
3279 * u_getbot(): compute the line number of the previous u_save
3280 * It is called only when b_u_synced is FALSE.
3281 */
3282 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003283u_getbot(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284{
3285 u_entry_T *uep;
3286 linenr_T extra;
3287
3288 uep = u_get_headentry(); /* check for corrupt undo list */
3289 if (uep == NULL)
3290 return;
3291
3292 uep = curbuf->b_u_newhead->uh_getbot_entry;
3293 if (uep != NULL)
3294 {
3295 /*
3296 * the new ue_bot is computed from the number of lines that has been
3297 * inserted (0 - deleted) since calling u_save. This is equal to the
3298 * old line count subtracted from the current line count.
3299 */
3300 extra = curbuf->b_ml.ml_line_count - uep->ue_lcount;
3301 uep->ue_bot = uep->ue_top + uep->ue_size + 1 + extra;
3302 if (uep->ue_bot < 1 || uep->ue_bot > curbuf->b_ml.ml_line_count)
3303 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003304 IEMSG(_("E440: undo line missing"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 uep->ue_bot = uep->ue_top + 1; /* assume all lines deleted, will
3306 * get all the old lines back
3307 * without deleting the current
3308 * ones */
3309 }
3310
3311 curbuf->b_u_newhead->uh_getbot_entry = NULL;
3312 }
3313
3314 curbuf->b_u_synced = TRUE;
3315}
3316
3317/*
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003318 * Free one header "uhp" and its entry list and adjust the pointers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 */
3320 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003321u_freeheader(
3322 buf_T *buf,
3323 u_header_T *uhp,
3324 u_header_T **uhpp) /* if not NULL reset when freeing this header */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325{
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003326 u_header_T *uhap;
3327
Bram Moolenaar1e607892006-03-13 22:15:53 +00003328 /* When there is an alternate redo list free that branch completely,
3329 * because we can never go there. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003330 if (uhp->uh_alt_next.ptr != NULL)
3331 u_freebranch(buf, uhp->uh_alt_next.ptr, uhpp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003333 if (uhp->uh_alt_prev.ptr != NULL)
3334 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335
Bram Moolenaar1e607892006-03-13 22:15:53 +00003336 /* Update the links in the list to remove the header. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003337 if (uhp->uh_next.ptr == NULL)
3338 buf->b_u_oldhead = uhp->uh_prev.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003340 uhp->uh_next.ptr->uh_prev.ptr = uhp->uh_prev.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003342 if (uhp->uh_prev.ptr == NULL)
3343 buf->b_u_newhead = uhp->uh_next.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003345 for (uhap = uhp->uh_prev.ptr; uhap != NULL;
3346 uhap = uhap->uh_alt_next.ptr)
3347 uhap->uh_next.ptr = uhp->uh_next.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348
Bram Moolenaar1e607892006-03-13 22:15:53 +00003349 u_freeentries(buf, uhp, uhpp);
3350}
3351
3352/*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003353 * Free an alternate branch and any following alternate branches.
Bram Moolenaar1e607892006-03-13 22:15:53 +00003354 */
3355 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003356u_freebranch(
3357 buf_T *buf,
3358 u_header_T *uhp,
3359 u_header_T **uhpp) /* if not NULL reset when freeing this header */
Bram Moolenaar1e607892006-03-13 22:15:53 +00003360{
3361 u_header_T *tofree, *next;
3362
Bram Moolenaar07d06772007-11-10 21:51:15 +00003363 /* If this is the top branch we may need to use u_freeheader() to update
3364 * all the pointers. */
3365 if (uhp == buf->b_u_oldhead)
3366 {
Bram Moolenaaraa887322013-11-07 03:04:11 +01003367 while (buf->b_u_oldhead != NULL)
3368 u_freeheader(buf, buf->b_u_oldhead, uhpp);
Bram Moolenaar07d06772007-11-10 21:51:15 +00003369 return;
3370 }
3371
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003372 if (uhp->uh_alt_prev.ptr != NULL)
3373 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +00003374
3375 next = uhp;
3376 while (next != NULL)
3377 {
3378 tofree = next;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003379 if (tofree->uh_alt_next.ptr != NULL)
3380 u_freebranch(buf, tofree->uh_alt_next.ptr, uhpp); /* recursive */
3381 next = tofree->uh_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00003382 u_freeentries(buf, tofree, uhpp);
3383 }
3384}
3385
3386/*
3387 * Free all the undo entries for one header and the header itself.
3388 * This means that "uhp" is invalid when returning.
3389 */
3390 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003391u_freeentries(
3392 buf_T *buf,
3393 u_header_T *uhp,
3394 u_header_T **uhpp) /* if not NULL reset when freeing this header */
Bram Moolenaar1e607892006-03-13 22:15:53 +00003395{
3396 u_entry_T *uep, *nuep;
3397
3398 /* Check for pointers to the header that become invalid now. */
3399 if (buf->b_u_curhead == uhp)
3400 buf->b_u_curhead = NULL;
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003401 if (buf->b_u_newhead == uhp)
3402 buf->b_u_newhead = NULL; /* freeing the newest entry */
Bram Moolenaar1e607892006-03-13 22:15:53 +00003403 if (uhpp != NULL && uhp == *uhpp)
3404 *uhpp = NULL;
3405
3406 for (uep = uhp->uh_entry; uep != NULL; uep = nuep)
3407 {
3408 nuep = uep->ue_next;
3409 u_freeentry(uep, uep->ue_size);
3410 }
3411
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003412#ifdef U_DEBUG
3413 uhp->uh_magic = 0;
3414#endif
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003415 vim_free((char_u *)uhp);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003416 --buf->b_u_numhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417}
3418
3419/*
3420 * free entry 'uep' and 'n' lines in uep->ue_array[]
3421 */
3422 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003423u_freeentry(u_entry_T *uep, long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424{
Bram Moolenaar8d343302005-07-12 22:46:17 +00003425 while (n > 0)
Bram Moolenaarccae4672019-01-04 15:09:57 +01003426 vim_free(uep->ue_array[--n].ul_line);
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003427 vim_free((char_u *)uep->ue_array);
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003428#ifdef U_DEBUG
3429 uep->ue_magic = 0;
3430#endif
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003431 vim_free((char_u *)uep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432}
3433
3434/*
3435 * invalidate the undo buffer; called when storage has already been released
3436 */
3437 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003438u_clearall(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439{
3440 buf->b_u_newhead = buf->b_u_oldhead = buf->b_u_curhead = NULL;
3441 buf->b_u_synced = TRUE;
3442 buf->b_u_numhead = 0;
Bram Moolenaarccae4672019-01-04 15:09:57 +01003443 buf->b_u_line_ptr.ul_line = NULL;
3444 buf->b_u_line_ptr.ul_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 buf->b_u_line_lnum = 0;
3446}
3447
3448/*
Bram Moolenaarccae4672019-01-04 15:09:57 +01003449 * Save the line "lnum" for the "U" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 */
3451 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003452u_saveline(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453{
3454 if (lnum == curbuf->b_u_line_lnum) /* line is already saved */
3455 return;
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00003456 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) /* should never happen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 return;
3458 u_clearline();
3459 curbuf->b_u_line_lnum = lnum;
3460 if (curwin->w_cursor.lnum == lnum)
3461 curbuf->b_u_line_colnr = curwin->w_cursor.col;
3462 else
3463 curbuf->b_u_line_colnr = 0;
Bram Moolenaarccae4672019-01-04 15:09:57 +01003464 if (u_save_line(&curbuf->b_u_line_ptr, lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 do_outofmem_msg((long_u)0);
3466}
3467
3468/*
3469 * clear the line saved for the "U" command
3470 * (this is used externally for crossing a line while in insert mode)
3471 */
3472 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003473u_clearline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474{
Bram Moolenaarccae4672019-01-04 15:09:57 +01003475 if (curbuf->b_u_line_ptr.ul_line != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 {
Bram Moolenaarccae4672019-01-04 15:09:57 +01003477 VIM_CLEAR(curbuf->b_u_line_ptr.ul_line);
3478 curbuf->b_u_line_ptr.ul_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 curbuf->b_u_line_lnum = 0;
3480 }
3481}
3482
3483/*
3484 * Implementation of the "U" command.
3485 * Differentiation from vi: "U" can be undone with the next "U".
3486 * We also allow the cursor to be in another line.
Bram Moolenaard04b7502010-07-08 22:27:55 +02003487 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 */
3489 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003490u_undoline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491{
Bram Moolenaarccae4672019-01-04 15:09:57 +01003492 colnr_T t;
3493 undoline_T oldp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494
3495 if (undo_off)
3496 return;
3497
Bram Moolenaarccae4672019-01-04 15:09:57 +01003498 if (curbuf->b_u_line_ptr.ul_line == NULL
Bram Moolenaare3300c82008-02-13 14:21:38 +00003499 || curbuf->b_u_line_lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 {
3501 beep_flush();
3502 return;
3503 }
Bram Moolenaare3300c82008-02-13 14:21:38 +00003504
Bram Moolenaarccae4672019-01-04 15:09:57 +01003505 // first save the line for the 'u' command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 if (u_savecommon(curbuf->b_u_line_lnum - 1,
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003507 curbuf->b_u_line_lnum + 1, (linenr_T)0, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 return;
Bram Moolenaarccae4672019-01-04 15:09:57 +01003509 if (u_save_line(&oldp, curbuf->b_u_line_lnum) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 {
3511 do_outofmem_msg((long_u)0);
3512 return;
3513 }
Bram Moolenaarccae4672019-01-04 15:09:57 +01003514 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 +00003515 changed_bytes(curbuf->b_u_line_lnum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 curbuf->b_u_line_ptr = oldp;
3517
3518 t = curbuf->b_u_line_colnr;
3519 if (curwin->w_cursor.lnum == curbuf->b_u_line_lnum)
3520 curbuf->b_u_line_colnr = curwin->w_cursor.col;
3521 curwin->w_cursor.col = t;
3522 curwin->w_cursor.lnum = curbuf->b_u_line_lnum;
Bram Moolenaare3300c82008-02-13 14:21:38 +00003523 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524}
3525
3526/*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003527 * Free all allocated memory blocks for the buffer 'buf'.
3528 */
3529 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003530u_blockfree(buf_T *buf)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003531{
Bram Moolenaar1e607892006-03-13 22:15:53 +00003532 while (buf->b_u_oldhead != NULL)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003533 u_freeheader(buf, buf->b_u_oldhead, NULL);
Bram Moolenaarccae4672019-01-04 15:09:57 +01003534 vim_free(buf->b_u_line_ptr.ul_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535}
3536
3537/*
3538 * Check if the 'modified' flag is set, or 'ff' has changed (only need to
3539 * check the first character, because it can only be "dos", "unix" or "mac").
3540 * "nofile" and "scratch" type buffers are considered to always be unchanged.
Bram Moolenaarf405c8f2017-12-09 19:51:49 +01003541 * Also considers a buffer changed when a terminal window contains a running
3542 * job.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 */
3544 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003545bufIsChanged(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546{
Bram Moolenaareb44a682017-08-03 22:44:55 +02003547#ifdef FEAT_TERMINAL
3548 if (term_job_running(buf->b_term))
3549 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550#endif
Bram Moolenaarf405c8f2017-12-09 19:51:49 +01003551 return bufIsChangedNotTerm(buf);
3552}
3553
3554/*
3555 * Like bufIsChanged() but ignoring a terminal window.
3556 */
3557 int
3558bufIsChangedNotTerm(buf_T *buf)
3559{
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003560 // In a "prompt" buffer we do respect 'modified', so that we can control
3561 // closing the window by setting or resetting that option.
3562 return (!bt_dontwrite(buf) || bt_prompt(buf))
Bram Moolenaareb44a682017-08-03 22:44:55 +02003563 && (buf->b_changed || file_ff_differs(buf, TRUE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564}
3565
3566 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003567curbufIsChanged(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568{
Bram Moolenaareb44a682017-08-03 22:44:55 +02003569 return bufIsChanged(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570}
Bram Moolenaara800b422010-06-27 01:15:55 +02003571
3572#if defined(FEAT_EVAL) || defined(PROTO)
3573/*
3574 * For undotree(): Append the list of undo blocks at "first_uhp" to "list".
3575 * Recursive.
3576 */
3577 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003578u_eval_tree(u_header_T *first_uhp, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02003579{
3580 u_header_T *uhp = first_uhp;
3581 dict_T *dict;
3582
3583 while (uhp != NULL)
3584 {
3585 dict = dict_alloc();
3586 if (dict == NULL)
3587 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003588 dict_add_number(dict, "seq", uhp->uh_seq);
3589 dict_add_number(dict, "time", (long)uhp->uh_time);
Bram Moolenaara800b422010-06-27 01:15:55 +02003590 if (uhp == curbuf->b_u_newhead)
Bram Moolenaare0be1672018-07-08 16:50:37 +02003591 dict_add_number(dict, "newhead", 1);
Bram Moolenaara800b422010-06-27 01:15:55 +02003592 if (uhp == curbuf->b_u_curhead)
Bram Moolenaare0be1672018-07-08 16:50:37 +02003593 dict_add_number(dict, "curhead", 1);
Bram Moolenaara800b422010-06-27 01:15:55 +02003594 if (uhp->uh_save_nr > 0)
Bram Moolenaare0be1672018-07-08 16:50:37 +02003595 dict_add_number(dict, "save", uhp->uh_save_nr);
Bram Moolenaara800b422010-06-27 01:15:55 +02003596
3597 if (uhp->uh_alt_next.ptr != NULL)
3598 {
3599 list_T *alt_list = list_alloc();
3600
3601 if (alt_list != NULL)
3602 {
3603 /* Recursive call to add alternate undo tree. */
3604 u_eval_tree(uhp->uh_alt_next.ptr, alt_list);
3605 dict_add_list(dict, "alt", alt_list);
3606 }
3607 }
3608
3609 list_append_dict(list, dict);
3610 uhp = uhp->uh_prev.ptr;
3611 }
3612}
3613#endif