blob: 607f35fdd861e71c222d5aca0c8f0aa0263b54a3 [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 long get_undolevel(void);
104static void u_unch_branch(u_header_T *uhp);
105static u_entry_T *u_get_headentry(void);
106static void u_getbot(void);
107static void u_doit(int count);
108static void u_undoredo(int undo);
109static void u_undo_end(int did_undo, int absolute);
110static void u_add_time(char_u *buf, size_t buflen, time_t tt);
111static void u_freeheader(buf_T *buf, u_header_T *uhp, u_header_T **uhpp);
112static void u_freebranch(buf_T *buf, u_header_T *uhp, u_header_T **uhpp);
113static void u_freeentries(buf_T *buf, u_header_T *uhp, u_header_T **uhpp);
114static void u_freeentry(u_entry_T *, long);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200115#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100116static void corruption_error(char *mesg, char_u *file_name);
117static void u_free_uhp(u_header_T *uhp);
118static int undo_write(bufinfo_T *bi, char_u *ptr, size_t len);
Bram Moolenaar1d6fbe62016-02-19 22:46:34 +0100119# ifdef FEAT_CRYPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100120static int undo_flush(bufinfo_T *bi);
Bram Moolenaar1d6fbe62016-02-19 22:46:34 +0100121# endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100122static int fwrite_crypt(bufinfo_T *bi, char_u *ptr, size_t len);
123static int undo_write_bytes(bufinfo_T *bi, long_u nr, int len);
124static void put_header_ptr(bufinfo_T *bi, u_header_T *uhp);
125static int undo_read_4c(bufinfo_T *bi);
126static int undo_read_2c(bufinfo_T *bi);
127static int undo_read_byte(bufinfo_T *bi);
128static time_t undo_read_time(bufinfo_T *bi);
129static int undo_read(bufinfo_T *bi, char_u *buffer, size_t size);
130static char_u *read_string_decrypt(bufinfo_T *bi, int len);
131static int serialize_header(bufinfo_T *bi, char_u *hash);
132static int serialize_uhp(bufinfo_T *bi, u_header_T *uhp);
133static u_header_T *unserialize_uhp(bufinfo_T *bi, char_u *file_name);
134static int serialize_uep(bufinfo_T *bi, u_entry_T *uep);
135static u_entry_T *unserialize_uep(bufinfo_T *bi, int *error, char_u *file_name);
136static void serialize_pos(bufinfo_T *bi, pos_T pos);
137static void unserialize_pos(bufinfo_T *bi, pos_T *pos);
138static void serialize_visualinfo(bufinfo_T *bi, visualinfo_T *info);
139static void unserialize_visualinfo(bufinfo_T *bi, visualinfo_T *info);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141
Bram Moolenaarf05e3b02010-05-29 15:40:47 +0200142#define U_ALLOC_LINE(size) lalloc((long_u)(size), FALSE)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100143static char_u *u_save_line(linenr_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
Bram Moolenaar730cde92010-06-27 05:18:54 +0200145/* used in undo_end() to report number of added and deleted lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146static long u_newcount, u_oldcount;
147
148/*
149 * When 'u' flag included in 'cpoptions', we behave like vi. Need to remember
150 * the action that "u" should do.
151 */
152static int undo_undoes = FALSE;
153
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200154static int lastmark = 0;
155
Bram Moolenaar9db58062010-05-29 20:33:07 +0200156#if defined(U_DEBUG) || defined(PROTO)
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000157/*
158 * Check the undo structures for being valid. Print a warning when something
159 * looks wrong.
160 */
161static int seen_b_u_curhead;
162static int seen_b_u_newhead;
163static int header_count;
164
165 static void
166u_check_tree(u_header_T *uhp,
167 u_header_T *exp_uh_next,
168 u_header_T *exp_uh_alt_prev)
169{
170 u_entry_T *uep;
171
172 if (uhp == NULL)
173 return;
174 ++header_count;
175 if (uhp == curbuf->b_u_curhead && ++seen_b_u_curhead > 1)
176 {
177 EMSG("b_u_curhead found twice (looping?)");
178 return;
179 }
180 if (uhp == curbuf->b_u_newhead && ++seen_b_u_newhead > 1)
181 {
182 EMSG("b_u_newhead found twice (looping?)");
183 return;
184 }
185
186 if (uhp->uh_magic != UH_MAGIC)
187 EMSG("uh_magic wrong (may be using freed memory)");
188 else
189 {
190 /* Check pointers back are correct. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200191 if (uhp->uh_next.ptr != exp_uh_next)
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000192 {
193 EMSG("uh_next wrong");
194 smsg((char_u *)"expected: 0x%x, actual: 0x%x",
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200195 exp_uh_next, uhp->uh_next.ptr);
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000196 }
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200197 if (uhp->uh_alt_prev.ptr != exp_uh_alt_prev)
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000198 {
199 EMSG("uh_alt_prev wrong");
200 smsg((char_u *)"expected: 0x%x, actual: 0x%x",
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200201 exp_uh_alt_prev, uhp->uh_alt_prev.ptr);
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000202 }
203
204 /* Check the undo tree at this header. */
205 for (uep = uhp->uh_entry; uep != NULL; uep = uep->ue_next)
206 {
207 if (uep->ue_magic != UE_MAGIC)
208 {
209 EMSG("ue_magic wrong (may be using freed memory)");
210 break;
211 }
212 }
213
214 /* Check the next alt tree. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200215 u_check_tree(uhp->uh_alt_next.ptr, uhp->uh_next.ptr, uhp);
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000216
217 /* Check the next header in this branch. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200218 u_check_tree(uhp->uh_prev.ptr, uhp, NULL);
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000219 }
220}
221
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200222 static void
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000223u_check(int newhead_may_be_NULL)
224{
225 seen_b_u_newhead = 0;
226 seen_b_u_curhead = 0;
227 header_count = 0;
228
229 u_check_tree(curbuf->b_u_oldhead, NULL, NULL);
230
231 if (seen_b_u_newhead == 0 && curbuf->b_u_oldhead != NULL
232 && !(newhead_may_be_NULL && curbuf->b_u_newhead == NULL))
233 EMSGN("b_u_newhead invalid: 0x%x", curbuf->b_u_newhead);
234 if (curbuf->b_u_curhead != NULL && seen_b_u_curhead == 0)
235 EMSGN("b_u_curhead invalid: 0x%x", curbuf->b_u_curhead);
236 if (header_count != curbuf->b_u_numhead)
237 {
238 EMSG("b_u_numhead invalid");
239 smsg((char_u *)"expected: %ld, actual: %ld",
240 (long)header_count, (long)curbuf->b_u_numhead);
241 }
242}
243#endif
244
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000246 * Save the current line for both the "u" and "U" command.
Bram Moolenaar687a29c2013-04-15 15:47:12 +0200247 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000248 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 */
250 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100251u_save_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252{
253 return (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
254 (linenr_T)(curwin->w_cursor.lnum + 1)));
255}
256
257/*
258 * Save the lines between "top" and "bot" for both the "u" and "U" command.
259 * "top" may be 0 and bot may be curbuf->b_ml.ml_line_count + 1.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200260 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 * Returns FAIL when lines could not be saved, OK otherwise.
262 */
263 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100264u_save(linenr_T top, linenr_T bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265{
266 if (undo_off)
267 return OK;
268
Bram Moolenaar687a29c2013-04-15 15:47:12 +0200269 if (top > curbuf->b_ml.ml_line_count
270 || top >= bot
271 || bot > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 return FALSE; /* rely on caller to do error messages */
273
274 if (top + 2 == bot)
275 u_saveline((linenr_T)(top + 1));
276
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200277 return (u_savecommon(top, bot, (linenr_T)0, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278}
279
280/*
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200281 * Save the line "lnum" (used by ":s" and "~" command).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282 * The line is replaced, so the new bottom line is lnum + 1.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200283 * Careful: may trigger autocommands that reload the buffer.
284 * Returns FAIL when lines could not be saved, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 */
286 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100287u_savesub(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288{
289 if (undo_off)
290 return OK;
291
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200292 return (u_savecommon(lnum - 1, lnum + 1, lnum + 1, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293}
294
295/*
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200296 * A new line is inserted before line "lnum" (used by :s command).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297 * The line is inserted, so the new bottom line is lnum + 1.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200298 * Careful: may trigger autocommands that reload the buffer.
299 * Returns FAIL when lines could not be saved, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 */
301 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100302u_inssub(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303{
304 if (undo_off)
305 return OK;
306
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200307 return (u_savecommon(lnum - 1, lnum, lnum + 1, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308}
309
310/*
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200311 * Save the lines "lnum" - "lnum" + nlines (used by delete command).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 * The lines are deleted, so the new bottom line is lnum, unless the buffer
313 * becomes empty.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200314 * Careful: may trigger autocommands that reload the buffer.
315 * Returns FAIL when lines could not be saved, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 */
317 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100318u_savedel(linenr_T lnum, long nlines)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319{
320 if (undo_off)
321 return OK;
322
323 return (u_savecommon(lnum - 1, lnum + nlines,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200324 nlines == curbuf->b_ml.ml_line_count ? 2 : lnum, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325}
326
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000327/*
328 * Return TRUE when undo is allowed. Otherwise give an error message and
329 * return FALSE.
330 */
Bram Moolenaarce6ef252006-07-12 19:49:41 +0000331 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100332undo_allowed(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000333{
334 /* Don't allow changes when 'modifiable' is off. */
335 if (!curbuf->b_p_ma)
336 {
337 EMSG(_(e_modifiable));
338 return FALSE;
339 }
340
341#ifdef HAVE_SANDBOX
342 /* In the sandbox it's not allowed to change the text. */
343 if (sandbox != 0)
344 {
345 EMSG(_(e_sandbox));
346 return FALSE;
347 }
348#endif
349
350 /* Don't allow changes in the buffer while editing the cmdline. The
351 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000352 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000353 {
354 EMSG(_(e_secure));
355 return FALSE;
356 }
357
358 return TRUE;
359}
360
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200361/*
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100362 * Get the undolevle value for the current buffer.
363 */
364 static long
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100365get_undolevel(void)
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100366{
367 if (curbuf->b_p_ul == NO_LOCAL_UNDOLEVEL)
368 return p_ul;
369 return curbuf->b_p_ul;
370}
371
372/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200373 * Common code for various ways to save text before a change.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200374 * "top" is the line above the first changed line.
375 * "bot" is the line below the last changed line.
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200376 * "newbot" is the new bottom line. Use zero when not known.
377 * "reload" is TRUE when saving for a buffer reload.
Bram Moolenaard04b7502010-07-08 22:27:55 +0200378 * Careful: may trigger autocommands that reload the buffer.
379 * Returns FAIL when lines could not be saved, OK otherwise.
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200380 */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200381 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100382u_savecommon(
383 linenr_T top,
384 linenr_T bot,
385 linenr_T newbot,
386 int reload)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387{
Bram Moolenaar1e607892006-03-13 22:15:53 +0000388 linenr_T lnum;
389 long i;
390 u_header_T *uhp;
391 u_header_T *old_curhead;
392 u_entry_T *uep;
393 u_entry_T *prev_uep;
394 long size;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200396 if (!reload)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 {
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200398 /* When making changes is not allowed return FAIL. It's a crude way
399 * to make all change commands fail. */
400 if (!undo_allowed())
Bram Moolenaar009b2592004-10-24 19:18:58 +0000401 return FAIL;
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200402
403#ifdef FEAT_NETBEANS_INTG
404 /*
405 * Netbeans defines areas that cannot be modified. Bail out here when
406 * trying to change text in a guarded area.
407 */
408 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +0000409 {
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200410 if (netbeans_is_guarded(top, bot))
411 {
412 EMSG(_(e_guarded));
413 return FAIL;
414 }
415 if (curbuf->b_p_ro)
416 {
417 EMSG(_(e_nbreadonly));
418 return FAIL;
419 }
Bram Moolenaar009b2592004-10-24 19:18:58 +0000420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421#endif
422
423#ifdef FEAT_AUTOCMD
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200424 /*
425 * Saving text for undo means we are going to make a change. Give a
426 * warning for a read-only file before making the change, so that the
427 * FileChangedRO event can replace the buffer with a read-write version
428 * (e.g., obtained from a source control system).
429 */
430 change_warning(0);
431 if (bot > curbuf->b_ml.ml_line_count + 1)
432 {
433 /* This happens when the FileChangedRO autocommand changes the
434 * file in a way it becomes shorter. */
Bram Moolenaarb4d587c2014-01-23 18:12:49 +0100435 EMSG(_("E881: Line count changed unexpectedly"));
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200436 return FAIL;
437 }
438#endif
Bram Moolenaard04b7502010-07-08 22:27:55 +0200439 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200440
441#ifdef U_DEBUG
442 u_check(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443#endif
444
445 size = bot - top - 1;
446
447 /*
Bram Moolenaarb0b50882010-07-07 18:26:28 +0200448 * If curbuf->b_u_synced == TRUE make a new header.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 */
450 if (curbuf->b_u_synced)
451 {
452#ifdef FEAT_JUMPLIST
453 /* Need to create new entry in b_changelist. */
454 curbuf->b_new_change = TRUE;
455#endif
456
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100457 if (get_undolevel() >= 0)
Bram Moolenaar1e607892006-03-13 22:15:53 +0000458 {
459 /*
460 * Make a new header entry. Do this first so that we don't mess
461 * up the undo info when out of memory.
462 */
Bram Moolenaarf05e3b02010-05-29 15:40:47 +0200463 uhp = (u_header_T *)U_ALLOC_LINE(sizeof(u_header_T));
Bram Moolenaar1e607892006-03-13 22:15:53 +0000464 if (uhp == NULL)
465 goto nomem;
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000466#ifdef U_DEBUG
467 uhp->uh_magic = UH_MAGIC;
468#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +0000469 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000470 else
471 uhp = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000472
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473 /*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000474 * If we undid more than we redid, move the entry lists before and
475 * including curbuf->b_u_curhead to an alternate branch.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 */
Bram Moolenaar1e607892006-03-13 22:15:53 +0000477 old_curhead = curbuf->b_u_curhead;
478 if (old_curhead != NULL)
479 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200480 curbuf->b_u_newhead = old_curhead->uh_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000481 curbuf->b_u_curhead = NULL;
482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483
484 /*
485 * free headers to keep the size right
486 */
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100487 while (curbuf->b_u_numhead > get_undolevel()
488 && curbuf->b_u_oldhead != NULL)
Bram Moolenaar1e607892006-03-13 22:15:53 +0000489 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000490 u_header_T *uhfree = curbuf->b_u_oldhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000491
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000492 if (uhfree == old_curhead)
493 /* Can't reconnect the branch, delete all of it. */
494 u_freebranch(curbuf, uhfree, &old_curhead);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200495 else if (uhfree->uh_alt_next.ptr == NULL)
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000496 /* There is no branch, only free one header. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000497 u_freeheader(curbuf, uhfree, &old_curhead);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000498 else
499 {
500 /* Free the oldest alternate branch as a whole. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200501 while (uhfree->uh_alt_next.ptr != NULL)
502 uhfree = uhfree->uh_alt_next.ptr;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000503 u_freebranch(curbuf, uhfree, &old_curhead);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000504 }
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000505#ifdef U_DEBUG
506 u_check(TRUE);
507#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +0000508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000510 if (uhp == NULL) /* no undo at all */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 {
Bram Moolenaar1e607892006-03-13 22:15:53 +0000512 if (old_curhead != NULL)
513 u_freebranch(curbuf, old_curhead, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 curbuf->b_u_synced = FALSE;
515 return OK;
516 }
517
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200518 uhp->uh_prev.ptr = NULL;
519 uhp->uh_next.ptr = curbuf->b_u_newhead;
520 uhp->uh_alt_next.ptr = old_curhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000521 if (old_curhead != NULL)
522 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200523 uhp->uh_alt_prev.ptr = old_curhead->uh_alt_prev.ptr;
524 if (uhp->uh_alt_prev.ptr != NULL)
525 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = uhp;
526 old_curhead->uh_alt_prev.ptr = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000527 if (curbuf->b_u_oldhead == old_curhead)
528 curbuf->b_u_oldhead = uhp;
529 }
Bram Moolenaar89ed3df2007-01-09 19:23:12 +0000530 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200531 uhp->uh_alt_prev.ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 if (curbuf->b_u_newhead != NULL)
Bram Moolenaar83d09bb2010-06-01 19:58:08 +0200533 curbuf->b_u_newhead->uh_prev.ptr = uhp;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000534
Bram Moolenaarca003e12006-03-17 23:19:38 +0000535 uhp->uh_seq = ++curbuf->b_u_seq_last;
536 curbuf->b_u_seq_cur = uhp->uh_seq;
Bram Moolenaar170b10b2016-07-29 16:15:27 +0200537 uhp->uh_time = vim_time();
Bram Moolenaara800b422010-06-27 01:15:55 +0200538 uhp->uh_save_nr = 0;
539 curbuf->b_u_time_cur = uhp->uh_time + 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000540
Bram Moolenaar1e607892006-03-13 22:15:53 +0000541 uhp->uh_walk = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 uhp->uh_entry = NULL;
543 uhp->uh_getbot_entry = NULL;
544 uhp->uh_cursor = curwin->w_cursor; /* save cursor pos. for undo */
545#ifdef FEAT_VIRTUALEDIT
546 if (virtual_active() && curwin->w_cursor.coladd > 0)
547 uhp->uh_cursor_vcol = getviscol();
548 else
549 uhp->uh_cursor_vcol = -1;
550#endif
551
552 /* save changed and buffer empty flag for undo */
553 uhp->uh_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
554 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
555
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000556 /* save named marks and Visual marks for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 mch_memmove(uhp->uh_namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000558 uhp->uh_visual = curbuf->b_visual;
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000559
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 curbuf->b_u_newhead = uhp;
561 if (curbuf->b_u_oldhead == NULL)
562 curbuf->b_u_oldhead = uhp;
563 ++curbuf->b_u_numhead;
564 }
565 else
566 {
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +0100567 if (get_undolevel() < 0) /* no undo at all */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 return OK;
569
570 /*
571 * When saving a single line, and it has been saved just before, it
572 * doesn't make sense saving it again. Saves a lot of memory when
573 * making lots of changes inside the same line.
574 * This is only possible if the previous change didn't increase or
575 * decrease the number of lines.
576 * Check the ten last changes. More doesn't make sense and takes too
577 * long.
578 */
579 if (size == 1)
580 {
581 uep = u_get_headentry();
582 prev_uep = NULL;
583 for (i = 0; i < 10; ++i)
584 {
585 if (uep == NULL)
586 break;
587
588 /* If lines have been inserted/deleted we give up.
589 * Also when the line was included in a multi-line save. */
590 if ((curbuf->b_u_newhead->uh_getbot_entry != uep
591 ? (uep->ue_top + uep->ue_size + 1
592 != (uep->ue_bot == 0
593 ? curbuf->b_ml.ml_line_count + 1
594 : uep->ue_bot))
595 : uep->ue_lcount != curbuf->b_ml.ml_line_count)
596 || (uep->ue_size > 1
597 && top >= uep->ue_top
598 && top + 2 <= uep->ue_top + uep->ue_size + 1))
599 break;
600
601 /* If it's the same line we can skip saving it again. */
602 if (uep->ue_size == 1 && uep->ue_top == top)
603 {
604 if (i > 0)
605 {
606 /* It's not the last entry: get ue_bot for the last
607 * entry now. Following deleted/inserted lines go to
608 * the re-used entry. */
609 u_getbot();
610 curbuf->b_u_synced = FALSE;
611
612 /* Move the found entry to become the last entry. The
613 * order of undo/redo doesn't matter for the entries
614 * we move it over, since they don't change the line
615 * count and don't include this line. It does matter
616 * for the found entry if the line count is changed by
617 * the executed command. */
618 prev_uep->ue_next = uep->ue_next;
619 uep->ue_next = curbuf->b_u_newhead->uh_entry;
620 curbuf->b_u_newhead->uh_entry = uep;
621 }
622
623 /* The executed command may change the line count. */
624 if (newbot != 0)
625 uep->ue_bot = newbot;
626 else if (bot > curbuf->b_ml.ml_line_count)
627 uep->ue_bot = 0;
628 else
629 {
630 uep->ue_lcount = curbuf->b_ml.ml_line_count;
631 curbuf->b_u_newhead->uh_getbot_entry = uep;
632 }
633 return OK;
634 }
635 prev_uep = uep;
636 uep = uep->ue_next;
637 }
638 }
639
640 /* find line number for ue_bot for previous u_save() */
641 u_getbot();
642 }
643
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200644#if !defined(UNIX) && !defined(WIN32)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100646 * With Amiga we can't handle big undo's, because
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 * then u_alloc_line would have to allocate a block larger than 32K
648 */
649 if (size >= 8000)
650 goto nomem;
651#endif
652
653 /*
654 * add lines in front of entry list
655 */
Bram Moolenaarf05e3b02010-05-29 15:40:47 +0200656 uep = (u_entry_T *)U_ALLOC_LINE(sizeof(u_entry_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 if (uep == NULL)
658 goto nomem;
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200659 vim_memset(uep, 0, sizeof(u_entry_T));
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000660#ifdef U_DEBUG
661 uep->ue_magic = UE_MAGIC;
662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663
664 uep->ue_size = size;
665 uep->ue_top = top;
666 if (newbot != 0)
667 uep->ue_bot = newbot;
668 /*
669 * Use 0 for ue_bot if bot is below last line.
670 * Otherwise we have to compute ue_bot later.
671 */
672 else if (bot > curbuf->b_ml.ml_line_count)
673 uep->ue_bot = 0;
674 else
675 {
676 uep->ue_lcount = curbuf->b_ml.ml_line_count;
677 curbuf->b_u_newhead->uh_getbot_entry = uep;
678 }
679
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000680 if (size > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000682 if ((uep->ue_array = (char_u **)U_ALLOC_LINE(
Bram Moolenaarf05e3b02010-05-29 15:40:47 +0200683 sizeof(char_u *) * size)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 {
685 u_freeentry(uep, 0L);
686 goto nomem;
687 }
688 for (i = 0, lnum = top + 1; i < size; ++i)
689 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000690 fast_breakcheck();
691 if (got_int)
692 {
693 u_freeentry(uep, i);
694 return FAIL;
695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 if ((uep->ue_array[i] = u_save_line(lnum++)) == NULL)
697 {
698 u_freeentry(uep, i);
699 goto nomem;
700 }
701 }
702 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000703 else
704 uep->ue_array = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 uep->ue_next = curbuf->b_u_newhead->uh_entry;
706 curbuf->b_u_newhead->uh_entry = uep;
707 curbuf->b_u_synced = FALSE;
708 undo_undoes = FALSE;
709
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000710#ifdef U_DEBUG
711 u_check(FALSE);
712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 return OK;
714
715nomem:
716 msg_silent = 0; /* must display the prompt */
717 if (ask_yesno((char_u *)_("No undo possible; continue anyway"), TRUE)
718 == 'y')
719 {
720 undo_off = TRUE; /* will be reset when character typed */
721 return OK;
722 }
723 do_outofmem_msg((long_u)0);
724 return FAIL;
725}
726
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200727#if defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200728
Bram Moolenaar9db58062010-05-29 20:33:07 +0200729# define UF_START_MAGIC "Vim\237UnDo\345" /* magic at start of undofile */
730# define UF_START_MAGIC_LEN 9
731# define UF_HEADER_MAGIC 0x5fd0 /* magic at start of header */
732# define UF_HEADER_END_MAGIC 0xe7aa /* magic after last header */
733# define UF_ENTRY_MAGIC 0xf518 /* magic at start of entry */
734# define UF_ENTRY_END_MAGIC 0x3581 /* magic after last entry */
Bram Moolenaara800b422010-06-27 01:15:55 +0200735# define UF_VERSION 2 /* 2-byte undofile version number */
Bram Moolenaara800b422010-06-27 01:15:55 +0200736# define UF_VERSION_CRYPT 0x8002 /* idem, encrypted */
737
738/* extra fields for header */
739# define UF_LAST_SAVE_NR 1
740
741/* extra fields for uhp */
742# define UHP_SAVE_NR 1
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200743
Bram Moolenaarcdf04202010-05-29 15:11:47 +0200744static char_u e_not_open[] = N_("E828: Cannot open undo file for writing: %s");
Bram Moolenaarcdf04202010-05-29 15:11:47 +0200745
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200746/*
747 * Compute the hash for the current buffer text into hash[UNDO_HASH_SIZE].
748 */
749 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100750u_compute_hash(char_u *hash)
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200751{
752 context_sha256_T ctx;
753 linenr_T lnum;
754 char_u *p;
755
756 sha256_start(&ctx);
Bram Moolenaarae7ba982011-12-08 15:14:09 +0100757 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200758 {
759 p = ml_get(lnum);
Bram Moolenaar442b4222010-05-24 21:34:22 +0200760 sha256_update(&ctx, p, (UINT32_T)(STRLEN(p) + 1));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200761 }
762 sha256_finish(&ctx, hash);
763}
764
765/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200766 * Return an allocated string of the full path of the target undofile.
767 * When "reading" is TRUE find the file to read, go over all directories in
768 * 'undodir'.
769 * When "reading" is FALSE use the first name where the directory exists.
Bram Moolenaar9db58062010-05-29 20:33:07 +0200770 * Returns NULL when there is no place to write or no file to read.
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200771 */
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200772 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100773u_get_undo_file_name(char_u *buf_ffname, int reading)
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200774{
775 char_u *dirp;
776 char_u dir_name[IOSIZE + 1];
777 char_u *munged_name = NULL;
778 char_u *undo_file_name = NULL;
779 int dir_len;
780 char_u *p;
Bram Moolenaar8767f522016-07-01 17:17:39 +0200781 stat_T st;
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200782 char_u *ffname = buf_ffname;
783#ifdef HAVE_READLINK
784 char_u fname_buf[MAXPATHL];
785#endif
786
787 if (ffname == NULL)
788 return NULL;
789
790#ifdef HAVE_READLINK
791 /* Expand symlink in the file name, so that we put the undo file with the
792 * actual file instead of with the symlink. */
793 if (resolve_symlink(ffname, fname_buf) == OK)
794 ffname = fname_buf;
795#endif
796
797 /* Loop over 'undodir'. When reading find the first file that exists.
798 * When not reading use the first directory that exists or ".". */
799 dirp = p_udir;
800 while (*dirp != NUL)
801 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200802 dir_len = copy_option_part(&dirp, dir_name, IOSIZE, ",");
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200803 if (dir_len == 1 && dir_name[0] == '.')
804 {
805 /* Use same directory as the ffname,
806 * "dir/name" -> "dir/.name.un~" */
Bram Moolenaar442b4222010-05-24 21:34:22 +0200807 undo_file_name = vim_strnsave(ffname, (int)(STRLEN(ffname) + 5));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200808 if (undo_file_name == NULL)
809 break;
810 p = gettail(undo_file_name);
Bram Moolenaar206f0112014-03-12 16:51:55 +0100811#ifdef VMS
812 /* VMS can not handle more than one dot in the filenames
813 * use "dir/name" -> "dir/_un_name" - add _un_
814 * at the beginning to keep the extension */
815 mch_memmove(p + 4, p, STRLEN(p) + 1);
816 mch_memmove(p, "_un_", 4);
817
818#else
819 /* Use same directory as the ffname,
820 * "dir/name" -> "dir/.name.un~" */
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200821 mch_memmove(p + 1, p, STRLEN(p) + 1);
822 *p = '.';
823 STRCAT(p, ".un~");
Bram Moolenaar206f0112014-03-12 16:51:55 +0100824#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200825 }
826 else
827 {
828 dir_name[dir_len] = NUL;
829 if (mch_isdir(dir_name))
830 {
831 if (munged_name == NULL)
832 {
833 munged_name = vim_strsave(ffname);
834 if (munged_name == NULL)
835 return NULL;
836 for (p = munged_name; *p != NUL; mb_ptr_adv(p))
837 if (vim_ispathsep(*p))
838 *p = '%';
839 }
840 undo_file_name = concat_fnames(dir_name, munged_name, TRUE);
841 }
842 }
843
844 /* When reading check if the file exists. */
845 if (undo_file_name != NULL && (!reading
846 || mch_stat((char *)undo_file_name, &st) >= 0))
847 break;
848 vim_free(undo_file_name);
849 undo_file_name = NULL;
850 }
851
852 vim_free(munged_name);
853 return undo_file_name;
854}
855
Bram Moolenaar9db58062010-05-29 20:33:07 +0200856 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100857corruption_error(char *mesg, char_u *file_name)
Bram Moolenaar9db58062010-05-29 20:33:07 +0200858{
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200859 EMSG3(_("E825: Corrupted undo file (%s): %s"), mesg, file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +0200860}
861
862 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100863u_free_uhp(u_header_T *uhp)
Bram Moolenaar9db58062010-05-29 20:33:07 +0200864{
865 u_entry_T *nuep;
866 u_entry_T *uep;
867
868 uep = uhp->uh_entry;
869 while (uep != NULL)
870 {
871 nuep = uep->ue_next;
872 u_freeentry(uep, uep->ue_size);
873 uep = nuep;
874 }
875 vim_free(uhp);
876}
877
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200878/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200879 * Write a sequence of bytes to the undo file.
880 * Buffers and encrypts as needed.
881 * Returns OK or FAIL.
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200882 */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200883 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100884undo_write(bufinfo_T *bi, char_u *ptr, size_t len)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200885{
886#ifdef FEAT_CRYPT
887 if (bi->bi_buffer != NULL)
888 {
889 size_t len_todo = len;
890 char_u *p = ptr;
891
892 while (bi->bi_used + len_todo >= CRYPT_BUF_SIZE)
893 {
894 size_t n = CRYPT_BUF_SIZE - bi->bi_used;
895
896 mch_memmove(bi->bi_buffer + bi->bi_used, p, n);
897 len_todo -= n;
898 p += n;
899 bi->bi_used = CRYPT_BUF_SIZE;
900 if (undo_flush(bi) == FAIL)
901 return FAIL;
902 }
903 if (len_todo > 0)
904 {
905 mch_memmove(bi->bi_buffer + bi->bi_used, p, len_todo);
906 bi->bi_used += len_todo;
907 }
908 return OK;
909 }
910#endif
911 if (fwrite(ptr, len, (size_t)1, bi->bi_fp) != 1)
912 return FAIL;
913 return OK;
914}
915
916#ifdef FEAT_CRYPT
917 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100918undo_flush(bufinfo_T *bi)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200919{
Bram Moolenaar5a669b92014-08-12 20:14:33 +0200920 if (bi->bi_buffer != NULL && bi->bi_used > 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200921 {
922 crypt_encode_inplace(bi->bi_state, bi->bi_buffer, bi->bi_used);
923 if (fwrite(bi->bi_buffer, bi->bi_used, (size_t)1, bi->bi_fp) != 1)
924 return FAIL;
925 bi->bi_used = 0;
926 }
927 return OK;
928}
929#endif
930
931/*
932 * Write "ptr[len]" and crypt the bytes when needed.
933 * Returns OK or FAIL.
934 */
935 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100936fwrite_crypt(bufinfo_T *bi, char_u *ptr, size_t len)
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200937{
938#ifdef FEAT_CRYPT
939 char_u *copy;
940 char_u small_buf[100];
941 size_t i;
942
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200943 if (bi->bi_state != NULL && bi->bi_buffer == NULL)
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200944 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200945 /* crypting every piece of text separately */
946 if (len < 100)
947 copy = small_buf; /* no malloc()/free() for short strings */
948 else
949 {
950 copy = lalloc(len, FALSE);
951 if (copy == NULL)
952 return 0;
953 }
954 crypt_encode(bi->bi_state, ptr, len, copy);
955 i = fwrite(copy, len, (size_t)1, bi->bi_fp);
956 if (copy != small_buf)
957 vim_free(copy);
958 return i == 1 ? OK : FAIL;
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200959 }
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200960#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200961 return undo_write(bi, ptr, len);
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200962}
963
964/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200965 * Write a number, MSB first, in "len" bytes.
966 * Must match with undo_read_?c() functions.
967 * Returns OK or FAIL.
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200968 */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200969 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100970undo_write_bytes(bufinfo_T *bi, long_u nr, int len)
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200971{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200972 char_u buf[8];
973 int i;
974 int bufi = 0;
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200975
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200976 for (i = len - 1; i >= 0; --i)
Bram Moolenaar9b8f0212014-08-13 22:05:53 +0200977 buf[bufi++] = (char_u)(nr >> (i * 8));
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200978 return undo_write(bi, buf, (size_t)len);
979}
980
981/*
982 * Write the pointer to an undo header. Instead of writing the pointer itself
983 * we use the sequence number of the header. This is converted back to
984 * pointers when reading. */
985 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100986put_header_ptr(bufinfo_T *bi, u_header_T *uhp)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200987{
988 undo_write_bytes(bi, (long_u)(uhp != NULL ? uhp->uh_seq : 0), 4);
Bram Moolenaar191e0a22010-06-14 01:39:13 +0200989}
990
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +0200991 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100992undo_read_4c(bufinfo_T *bi)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200993{
994#ifdef FEAT_CRYPT
995 if (bi->bi_buffer != NULL)
996 {
997 char_u buf[4];
998 int n;
999
1000 undo_read(bi, buf, (size_t)4);
Bram Moolenaar35169282014-09-11 22:50:09 +02001001 n = ((unsigned)buf[0] << 24) + (buf[1] << 16) + (buf[2] << 8) + buf[3];
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001002 return n;
1003 }
1004#endif
1005 return get4c(bi->bi_fp);
1006}
1007
1008 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001009undo_read_2c(bufinfo_T *bi)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001010{
1011#ifdef FEAT_CRYPT
1012 if (bi->bi_buffer != NULL)
1013 {
1014 char_u buf[2];
1015 int n;
1016
1017 undo_read(bi, buf, (size_t)2);
1018 n = (buf[0] << 8) + buf[1];
1019 return n;
1020 }
1021#endif
1022 return get2c(bi->bi_fp);
1023}
1024
1025 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001026undo_read_byte(bufinfo_T *bi)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001027{
1028#ifdef FEAT_CRYPT
1029 if (bi->bi_buffer != NULL)
1030 {
1031 char_u buf[1];
1032
1033 undo_read(bi, buf, (size_t)1);
1034 return buf[0];
1035 }
1036#endif
1037 return getc(bi->bi_fp);
1038}
1039
1040 static time_t
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001041undo_read_time(bufinfo_T *bi)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001042{
1043#ifdef FEAT_CRYPT
1044 if (bi->bi_buffer != NULL)
1045 {
1046 char_u buf[8];
1047 time_t n = 0;
1048 int i;
1049
1050 undo_read(bi, buf, (size_t)8);
1051 for (i = 0; i < 8; ++i)
1052 n = (n << 8) + buf[i];
1053 return n;
1054 }
1055#endif
1056 return get8ctime(bi->bi_fp);
1057}
1058
1059/*
1060 * Read "buffer[size]" from the undo file.
1061 * Return OK or FAIL.
1062 */
1063 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001064undo_read(bufinfo_T *bi, char_u *buffer, size_t size)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001065{
1066#ifdef FEAT_CRYPT
1067 if (bi->bi_buffer != NULL)
1068 {
Bram Moolenaar9b8f0212014-08-13 22:05:53 +02001069 int size_todo = (int)size;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001070 char_u *p = buffer;
1071
1072 while (size_todo > 0)
1073 {
1074 size_t n;
1075
1076 if (bi->bi_used >= bi->bi_avail)
1077 {
1078 n = fread(bi->bi_buffer, 1, (size_t)CRYPT_BUF_SIZE, bi->bi_fp);
Bram Moolenaar55952d42016-11-05 14:58:34 +01001079 if (n == 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001080 {
1081 /* Error may be checked for only later. Fill with zeros,
1082 * so that the reader won't use garbage. */
1083 vim_memset(p, 0, size_todo);
1084 return FAIL;
1085 }
1086 bi->bi_avail = n;
1087 bi->bi_used = 0;
1088 crypt_decode_inplace(bi->bi_state, bi->bi_buffer, bi->bi_avail);
1089 }
1090 n = size_todo;
1091 if (n > bi->bi_avail - bi->bi_used)
1092 n = bi->bi_avail - bi->bi_used;
1093 mch_memmove(p, bi->bi_buffer + bi->bi_used, n);
1094 bi->bi_used += n;
Bram Moolenaar9b8f0212014-08-13 22:05:53 +02001095 size_todo -= (int)n;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001096 p += n;
1097 }
1098 return OK;
1099 }
1100#endif
1101 if (fread(buffer, (size_t)size, 1, bi->bi_fp) != 1)
1102 return FAIL;
1103 return OK;
1104}
1105
1106/*
1107 * Read a string of length "len" from "bi->bi_fd".
1108 * "len" can be zero to allocate an empty line.
1109 * Decrypt the bytes if needed.
1110 * Append a NUL.
1111 * Returns a pointer to allocated memory or NULL for failure.
1112 */
1113 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001114read_string_decrypt(bufinfo_T *bi, int len)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001115{
1116 char_u *ptr = alloc((unsigned)len + 1);
1117
1118 if (ptr != NULL)
1119 {
1120 if (len > 0 && undo_read(bi, ptr, len) == FAIL)
1121 {
1122 vim_free(ptr);
1123 return NULL;
1124 }
1125 ptr[len] = NUL;
1126#ifdef FEAT_CRYPT
1127 if (bi->bi_state != NULL && bi->bi_buffer == NULL)
1128 crypt_decode_inplace(bi->bi_state, ptr, len);
1129#endif
1130 }
1131 return ptr;
1132}
1133
1134/*
1135 * Writes the (not encrypted) header and initializes encryption if needed.
1136 */
1137 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001138serialize_header(bufinfo_T *bi, char_u *hash)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001139{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001140 int len;
1141 buf_T *buf = bi->bi_buf;
1142 FILE *fp = bi->bi_fp;
1143 char_u time_buf[8];
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001144
1145 /* Start writing, first the magic marker and undo info version. */
1146 if (fwrite(UF_START_MAGIC, (size_t)UF_START_MAGIC_LEN, (size_t)1, fp) != 1)
1147 return FAIL;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001148
1149 /* If the buffer is encrypted then all text bytes following will be
1150 * encrypted. Numbers and other info is not crypted. */
1151#ifdef FEAT_CRYPT
Bram Moolenaarfa0ff9a2010-07-25 16:05:19 +02001152 if (*buf->b_p_key != NUL)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001153 {
1154 char_u *header;
1155 int header_len;
1156
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001157 undo_write_bytes(bi, (long_u)UF_VERSION_CRYPT, 2);
1158 bi->bi_state = crypt_create_for_writing(crypt_get_method_nr(buf),
1159 buf->b_p_key, &header, &header_len);
1160 if (bi->bi_state == NULL)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001161 return FAIL;
Bram Moolenaarbbd6afe2010-06-02 20:32:23 +02001162 len = (int)fwrite(header, (size_t)header_len, (size_t)1, fp);
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001163 vim_free(header);
1164 if (len != 1)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001165 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001166 crypt_free_state(bi->bi_state);
1167 bi->bi_state = NULL;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001168 return FAIL;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001169 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001170
1171 if (crypt_whole_undofile(crypt_get_method_nr(buf)))
1172 {
1173 bi->bi_buffer = alloc(CRYPT_BUF_SIZE);
1174 if (bi->bi_buffer == NULL)
1175 {
1176 crypt_free_state(bi->bi_state);
1177 bi->bi_state = NULL;
1178 return FAIL;
1179 }
1180 bi->bi_used = 0;
1181 }
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001182 }
1183 else
1184#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001185 undo_write_bytes(bi, (long_u)UF_VERSION, 2);
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001186
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001187
1188 /* Write a hash of the buffer text, so that we can verify it is still the
1189 * same when reading the buffer text. */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001190 if (undo_write(bi, hash, (size_t)UNDO_HASH_SIZE) == FAIL)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001191 return FAIL;
1192
1193 /* buffer-specific data */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001194 undo_write_bytes(bi, (long_u)buf->b_ml.ml_line_count, 4);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001195 len = buf->b_u_line_ptr != NULL ? (int)STRLEN(buf->b_u_line_ptr) : 0;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001196 undo_write_bytes(bi, (long_u)len, 4);
1197 if (len > 0 && fwrite_crypt(bi, buf->b_u_line_ptr, (size_t)len) == FAIL)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001198 return FAIL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001199 undo_write_bytes(bi, (long_u)buf->b_u_line_lnum, 4);
1200 undo_write_bytes(bi, (long_u)buf->b_u_line_colnr, 4);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001201
1202 /* Undo structures header data */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001203 put_header_ptr(bi, buf->b_u_oldhead);
1204 put_header_ptr(bi, buf->b_u_newhead);
1205 put_header_ptr(bi, buf->b_u_curhead);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001206
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001207 undo_write_bytes(bi, (long_u)buf->b_u_numhead, 4);
1208 undo_write_bytes(bi, (long_u)buf->b_u_seq_last, 4);
1209 undo_write_bytes(bi, (long_u)buf->b_u_seq_cur, 4);
1210 time_to_bytes(buf->b_u_time_cur, time_buf);
1211 undo_write(bi, time_buf, 8);
Bram Moolenaara800b422010-06-27 01:15:55 +02001212
1213 /* Optional fields. */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001214 undo_write_bytes(bi, 4, 1);
1215 undo_write_bytes(bi, UF_LAST_SAVE_NR, 1);
1216 undo_write_bytes(bi, (long_u)buf->b_u_save_nr_last, 4);
Bram Moolenaara800b422010-06-27 01:15:55 +02001217
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001218 undo_write_bytes(bi, 0, 1); /* end marker */
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001219
1220 return OK;
1221}
1222
1223 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001224serialize_uhp(bufinfo_T *bi, u_header_T *uhp)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001225{
1226 int i;
1227 u_entry_T *uep;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001228 char_u time_buf[8];
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001229
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001230 if (undo_write_bytes(bi, (long_u)UF_HEADER_MAGIC, 2) == FAIL)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001231 return FAIL;
1232
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001233 put_header_ptr(bi, uhp->uh_next.ptr);
1234 put_header_ptr(bi, uhp->uh_prev.ptr);
1235 put_header_ptr(bi, uhp->uh_alt_next.ptr);
1236 put_header_ptr(bi, uhp->uh_alt_prev.ptr);
1237 undo_write_bytes(bi, uhp->uh_seq, 4);
1238 serialize_pos(bi, uhp->uh_cursor);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001239#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001240 undo_write_bytes(bi, (long_u)uhp->uh_cursor_vcol, 4);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001241#else
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001242 undo_write_bytes(bi, (long_u)0, 4);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001243#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001244 undo_write_bytes(bi, (long_u)uhp->uh_flags, 2);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001245 /* Assume NMARKS will stay the same. */
1246 for (i = 0; i < NMARKS; ++i)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001247 serialize_pos(bi, uhp->uh_namedm[i]);
1248 serialize_visualinfo(bi, &uhp->uh_visual);
1249 time_to_bytes(uhp->uh_time, time_buf);
1250 undo_write(bi, time_buf, 8);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001251
Bram Moolenaara800b422010-06-27 01:15:55 +02001252 /* Optional fields. */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001253 undo_write_bytes(bi, 4, 1);
1254 undo_write_bytes(bi, UHP_SAVE_NR, 1);
1255 undo_write_bytes(bi, (long_u)uhp->uh_save_nr, 4);
Bram Moolenaara800b422010-06-27 01:15:55 +02001256
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001257 undo_write_bytes(bi, 0, 1); /* end marker */
Bram Moolenaara800b422010-06-27 01:15:55 +02001258
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001259 /* Write all the entries. */
1260 for (uep = uhp->uh_entry; uep != NULL; uep = uep->ue_next)
1261 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001262 undo_write_bytes(bi, (long_u)UF_ENTRY_MAGIC, 2);
1263 if (serialize_uep(bi, uep) == FAIL)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001264 return FAIL;
1265 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001266 undo_write_bytes(bi, (long_u)UF_ENTRY_END_MAGIC, 2);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001267 return OK;
1268}
1269
1270 static u_header_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001271unserialize_uhp(bufinfo_T *bi, char_u *file_name)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001272{
1273 u_header_T *uhp;
1274 int i;
1275 u_entry_T *uep, *last_uep;
1276 int c;
1277 int error;
1278
1279 uhp = (u_header_T *)U_ALLOC_LINE(sizeof(u_header_T));
1280 if (uhp == NULL)
1281 return NULL;
1282 vim_memset(uhp, 0, sizeof(u_header_T));
1283#ifdef U_DEBUG
1284 uhp->uh_magic = UH_MAGIC;
1285#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001286 uhp->uh_next.seq = undo_read_4c(bi);
1287 uhp->uh_prev.seq = undo_read_4c(bi);
1288 uhp->uh_alt_next.seq = undo_read_4c(bi);
1289 uhp->uh_alt_prev.seq = undo_read_4c(bi);
1290 uhp->uh_seq = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001291 if (uhp->uh_seq <= 0)
1292 {
1293 corruption_error("uh_seq", file_name);
1294 vim_free(uhp);
1295 return NULL;
1296 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001297 unserialize_pos(bi, &uhp->uh_cursor);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001298#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001299 uhp->uh_cursor_vcol = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001300#else
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001301 (void)undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001302#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001303 uhp->uh_flags = undo_read_2c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001304 for (i = 0; i < NMARKS; ++i)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001305 unserialize_pos(bi, &uhp->uh_namedm[i]);
1306 unserialize_visualinfo(bi, &uhp->uh_visual);
1307 uhp->uh_time = undo_read_time(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001308
Bram Moolenaara800b422010-06-27 01:15:55 +02001309 /* Optional fields. */
Bram Moolenaar69154f22010-07-18 21:42:34 +02001310 for (;;)
1311 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001312 int len = undo_read_byte(bi);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001313 int what;
Bram Moolenaara800b422010-06-27 01:15:55 +02001314
Bram Moolenaar69154f22010-07-18 21:42:34 +02001315 if (len == 0)
1316 break;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001317 what = undo_read_byte(bi);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001318 switch (what)
1319 {
1320 case UHP_SAVE_NR:
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001321 uhp->uh_save_nr = undo_read_4c(bi);
Bram Moolenaara800b422010-06-27 01:15:55 +02001322 break;
Bram Moolenaar69154f22010-07-18 21:42:34 +02001323 default:
1324 /* field not supported, skip */
1325 while (--len >= 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001326 (void)undo_read_byte(bi);
Bram Moolenaara800b422010-06-27 01:15:55 +02001327 }
Bram Moolenaar69154f22010-07-18 21:42:34 +02001328 }
Bram Moolenaara800b422010-06-27 01:15:55 +02001329
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001330 /* Unserialize the uep list. */
1331 last_uep = NULL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001332 while ((c = undo_read_2c(bi)) == UF_ENTRY_MAGIC)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001333 {
1334 error = FALSE;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001335 uep = unserialize_uep(bi, &error, file_name);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001336 if (last_uep == NULL)
1337 uhp->uh_entry = uep;
1338 else
1339 last_uep->ue_next = uep;
1340 last_uep = uep;
1341 if (uep == NULL || error)
1342 {
1343 u_free_uhp(uhp);
1344 return NULL;
1345 }
1346 }
1347 if (c != UF_ENTRY_END_MAGIC)
1348 {
1349 corruption_error("entry end", file_name);
1350 u_free_uhp(uhp);
1351 return NULL;
1352 }
1353
1354 return uhp;
1355}
1356
Bram Moolenaar9db58062010-05-29 20:33:07 +02001357/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001358 * Serialize "uep".
Bram Moolenaar9db58062010-05-29 20:33:07 +02001359 */
1360 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001361serialize_uep(
1362 bufinfo_T *bi,
1363 u_entry_T *uep)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001364{
1365 int i;
1366 size_t len;
1367
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001368 undo_write_bytes(bi, (long_u)uep->ue_top, 4);
1369 undo_write_bytes(bi, (long_u)uep->ue_bot, 4);
1370 undo_write_bytes(bi, (long_u)uep->ue_lcount, 4);
1371 undo_write_bytes(bi, (long_u)uep->ue_size, 4);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001372 for (i = 0; i < uep->ue_size; ++i)
1373 {
1374 len = STRLEN(uep->ue_array[i]);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001375 if (undo_write_bytes(bi, (long_u)len, 4) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001376 return FAIL;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001377 if (len > 0 && fwrite_crypt(bi, uep->ue_array[i], len) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001378 return FAIL;
1379 }
1380 return OK;
1381}
1382
1383 static u_entry_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001384unserialize_uep(bufinfo_T *bi, int *error, char_u *file_name)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001385{
1386 int i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001387 u_entry_T *uep;
1388 char_u **array;
1389 char_u *line;
1390 int line_len;
1391
1392 uep = (u_entry_T *)U_ALLOC_LINE(sizeof(u_entry_T));
1393 if (uep == NULL)
1394 return NULL;
1395 vim_memset(uep, 0, sizeof(u_entry_T));
1396#ifdef U_DEBUG
1397 uep->ue_magic = UE_MAGIC;
1398#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001399 uep->ue_top = undo_read_4c(bi);
1400 uep->ue_bot = undo_read_4c(bi);
1401 uep->ue_lcount = undo_read_4c(bi);
1402 uep->ue_size = undo_read_4c(bi);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001403 if (uep->ue_size > 0)
1404 {
1405 array = (char_u **)U_ALLOC_LINE(sizeof(char_u *) * uep->ue_size);
1406 if (array == NULL)
1407 {
1408 *error = TRUE;
1409 return uep;
1410 }
1411 vim_memset(array, 0, sizeof(char_u *) * uep->ue_size);
1412 }
Bram Moolenaar644fdff2010-05-30 13:26:21 +02001413 else
1414 array = NULL;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001415 uep->ue_array = array;
1416
1417 for (i = 0; i < uep->ue_size; ++i)
1418 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001419 line_len = undo_read_4c(bi);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001420 if (line_len >= 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001421 line = read_string_decrypt(bi, line_len);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001422 else
1423 {
1424 line = NULL;
1425 corruption_error("line length", file_name);
1426 }
1427 if (line == NULL)
1428 {
1429 *error = TRUE;
1430 return uep;
1431 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001432 array[i] = line;
1433 }
1434 return uep;
1435}
1436
1437/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001438 * Serialize "pos".
Bram Moolenaar9db58062010-05-29 20:33:07 +02001439 */
1440 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001441serialize_pos(bufinfo_T *bi, pos_T pos)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001442{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001443 undo_write_bytes(bi, (long_u)pos.lnum, 4);
1444 undo_write_bytes(bi, (long_u)pos.col, 4);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001445#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001446 undo_write_bytes(bi, (long_u)pos.coladd, 4);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001447#else
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001448 undo_write_bytes(bi, (long_u)0, 4);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001449#endif
1450}
1451
1452/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001453 * Unserialize the pos_T at the current position.
Bram Moolenaar9db58062010-05-29 20:33:07 +02001454 */
1455 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001456unserialize_pos(bufinfo_T *bi, pos_T *pos)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001457{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001458 pos->lnum = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001459 if (pos->lnum < 0)
1460 pos->lnum = 0;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001461 pos->col = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001462 if (pos->col < 0)
1463 pos->col = 0;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001464#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001465 pos->coladd = undo_read_4c(bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001466 if (pos->coladd < 0)
1467 pos->coladd = 0;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001468#else
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001469 (void)undo_read_4c(bi);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001470#endif
1471}
1472
1473/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001474 * Serialize "info".
Bram Moolenaar9db58062010-05-29 20:33:07 +02001475 */
1476 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001477serialize_visualinfo(bufinfo_T *bi, visualinfo_T *info)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001478{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001479 serialize_pos(bi, info->vi_start);
1480 serialize_pos(bi, info->vi_end);
1481 undo_write_bytes(bi, (long_u)info->vi_mode, 4);
1482 undo_write_bytes(bi, (long_u)info->vi_curswant, 4);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001483}
1484
1485/*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001486 * Unserialize the visualinfo_T at the current position.
Bram Moolenaar9db58062010-05-29 20:33:07 +02001487 */
1488 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001489unserialize_visualinfo(bufinfo_T *bi, visualinfo_T *info)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001490{
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001491 unserialize_pos(bi, &info->vi_start);
1492 unserialize_pos(bi, &info->vi_end);
1493 info->vi_mode = undo_read_4c(bi);
1494 info->vi_curswant = undo_read_4c(bi);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001495}
1496
1497/*
1498 * Write the undo tree in an undo file.
1499 * When "name" is not NULL, use it as the name of the undo file.
1500 * Otherwise use buf->b_ffname to generate the undo file name.
1501 * "buf" must never be null, buf->b_ffname is used to obtain the original file
1502 * permissions.
1503 * "forceit" is TRUE for ":wundo!", FALSE otherwise.
1504 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text.
1505 */
1506 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001507u_write_undo(
1508 char_u *name,
1509 int forceit,
1510 buf_T *buf,
1511 char_u *hash)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001512{
1513 u_header_T *uhp;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001514 char_u *file_name;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001515 int mark;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001516#ifdef U_DEBUG
1517 int headers_written = 0;
1518#endif
1519 int fd;
1520 FILE *fp = NULL;
1521 int perm;
1522 int write_ok = FALSE;
1523#ifdef UNIX
1524 int st_old_valid = FALSE;
Bram Moolenaar8767f522016-07-01 17:17:39 +02001525 stat_T st_old;
1526 stat_T st_new;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001527#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001528 bufinfo_T bi;
1529
Bram Moolenaar5a669b92014-08-12 20:14:33 +02001530 vim_memset(&bi, 0, sizeof(bi));
Bram Moolenaar9db58062010-05-29 20:33:07 +02001531
1532 if (name == NULL)
1533 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001534 file_name = u_get_undo_file_name(buf->b_ffname, FALSE);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001535 if (file_name == NULL)
1536 {
1537 if (p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001538 {
1539 verbose_enter();
1540 smsg((char_u *)
1541 _("Cannot write undo file in any directory in 'undodir'"));
1542 verbose_leave();
1543 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001544 return;
1545 }
1546 }
1547 else
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001548 file_name = name;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001549
1550 /*
1551 * Decide about the permission to use for the undo file. If the buffer
1552 * has a name use the permission of the original file. Otherwise only
1553 * allow the user to access the undo file.
1554 */
1555 perm = 0600;
1556 if (buf->b_ffname != NULL)
1557 {
1558#ifdef UNIX
1559 if (mch_stat((char *)buf->b_ffname, &st_old) >= 0)
1560 {
1561 perm = st_old.st_mode;
1562 st_old_valid = TRUE;
1563 }
1564#else
1565 perm = mch_getperm(buf->b_ffname);
1566 if (perm < 0)
1567 perm = 0600;
1568#endif
1569 }
1570
Bram Moolenaar3cbac302015-04-21 16:12:06 +02001571 /* strip any s-bit and executable bit */
1572 perm = perm & 0666;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001573
1574 /* If the undo file already exists, verify that it actually is an undo
1575 * file, and delete it. */
1576 if (mch_getperm(file_name) >= 0)
1577 {
1578 if (name == NULL || !forceit)
1579 {
1580 /* Check we can read it and it's an undo file. */
1581 fd = mch_open((char *)file_name, O_RDONLY|O_EXTRA, 0);
1582 if (fd < 0)
1583 {
1584 if (name != NULL || p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001585 {
1586 if (name == NULL)
1587 verbose_enter();
1588 smsg((char_u *)
1589 _("Will not overwrite with undo file, cannot read: %s"),
Bram Moolenaar9db58062010-05-29 20:33:07 +02001590 file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001591 if (name == NULL)
1592 verbose_leave();
1593 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001594 goto theend;
1595 }
1596 else
1597 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02001598 char_u mbuf[UF_START_MAGIC_LEN];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001599 int len;
1600
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01001601 len = read_eintr(fd, mbuf, UF_START_MAGIC_LEN);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001602 close(fd);
1603 if (len < UF_START_MAGIC_LEN
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02001604 || memcmp(mbuf, UF_START_MAGIC, UF_START_MAGIC_LEN) != 0)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001605 {
1606 if (name != NULL || p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001607 {
1608 if (name == NULL)
1609 verbose_enter();
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001610 smsg((char_u *)
1611 _("Will not overwrite, this is not an undo file: %s"),
Bram Moolenaar9db58062010-05-29 20:33:07 +02001612 file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001613 if (name == NULL)
1614 verbose_leave();
1615 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001616 goto theend;
1617 }
1618 }
1619 }
1620 mch_remove(file_name);
1621 }
1622
Bram Moolenaar504a8212010-05-30 17:17:42 +02001623 /* If there is no undo information at all, quit here after deleting any
1624 * existing undo file. */
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001625 if (buf->b_u_numhead == 0 && buf->b_u_line_ptr == NULL)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001626 {
1627 if (p_verbose > 0)
Bram Moolenaar97ea5112010-06-12 06:46:44 +02001628 verb_msg((char_u *)_("Skipping undo file write, nothing to undo"));
Bram Moolenaar504a8212010-05-30 17:17:42 +02001629 goto theend;
1630 }
1631
Bram Moolenaar9db58062010-05-29 20:33:07 +02001632 fd = mch_open((char *)file_name,
1633 O_CREAT|O_EXTRA|O_WRONLY|O_EXCL|O_NOFOLLOW, perm);
1634 if (fd < 0)
1635 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001636 EMSG2(_(e_not_open), file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001637 goto theend;
1638 }
1639 (void)mch_setperm(file_name, perm);
1640 if (p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001641 {
1642 verbose_enter();
Bram Moolenaar9db58062010-05-29 20:33:07 +02001643 smsg((char_u *)_("Writing undo file: %s"), file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001644 verbose_leave();
1645 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001646
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001647#ifdef U_DEBUG
Bram Moolenaar504a8212010-05-30 17:17:42 +02001648 /* Check there is no problem in undo info before writing. */
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001649 u_check(FALSE);
1650#endif
1651
Bram Moolenaar9db58062010-05-29 20:33:07 +02001652#ifdef UNIX
1653 /*
1654 * Try to set the group of the undo file same as the original file. If
1655 * this fails, set the protection bits for the group same as the
1656 * protection bits for others.
1657 */
Bram Moolenaarce69e822010-07-21 20:31:07 +02001658 if (st_old_valid
1659 && mch_stat((char *)file_name, &st_new) >= 0
1660 && st_new.st_gid != st_old.st_gid
Bram Moolenaar9db58062010-05-29 20:33:07 +02001661# ifdef HAVE_FCHOWN /* sequent-ptx lacks fchown() */
Bram Moolenaarce69e822010-07-21 20:31:07 +02001662 && fchown(fd, (uid_t)-1, st_old.st_gid) != 0
Bram Moolenaar9db58062010-05-29 20:33:07 +02001663# endif
1664 )
1665 mch_setperm(file_name, (perm & 0707) | ((perm & 07) << 3));
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02001666# if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001667 if (buf->b_ffname != NULL)
1668 mch_copy_sec(buf->b_ffname, file_name);
1669# endif
1670#endif
1671
1672 fp = fdopen(fd, "w");
1673 if (fp == NULL)
1674 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001675 EMSG2(_(e_not_open), file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001676 close(fd);
1677 mch_remove(file_name);
1678 goto theend;
1679 }
1680
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001681 /* Undo must be synced. */
1682 u_sync(TRUE);
1683
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001684 /*
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001685 * Write the header. Initializes encryption, if enabled.
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001686 */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001687 bi.bi_buf = buf;
1688 bi.bi_fp = fp;
1689 if (serialize_header(&bi, hash) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001690 goto write_error;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001691
1692 /*
1693 * Iteratively serialize UHPs and their UEPs from the top down.
1694 */
1695 mark = ++lastmark;
1696 uhp = buf->b_u_oldhead;
1697 while (uhp != NULL)
1698 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001699 /* Serialize current UHP if we haven't seen it */
1700 if (uhp->uh_walk != mark)
1701 {
1702 uhp->uh_walk = mark;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001703#ifdef U_DEBUG
1704 ++headers_written;
1705#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001706 if (serialize_uhp(&bi, uhp) == FAIL)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001707 goto write_error;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001708 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001709
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001710 /* Now walk through the tree - algorithm from undo_time(). */
1711 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != mark)
1712 uhp = uhp->uh_prev.ptr;
1713 else if (uhp->uh_alt_next.ptr != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001714 && uhp->uh_alt_next.ptr->uh_walk != mark)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001715 uhp = uhp->uh_alt_next.ptr;
1716 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02001717 && uhp->uh_next.ptr->uh_walk != mark)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001718 uhp = uhp->uh_next.ptr;
1719 else if (uhp->uh_alt_prev.ptr != NULL)
1720 uhp = uhp->uh_alt_prev.ptr;
1721 else
1722 uhp = uhp->uh_next.ptr;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001723 }
1724
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001725 if (undo_write_bytes(&bi, (long_u)UF_HEADER_END_MAGIC, 2) == OK)
Bram Moolenaar9db58062010-05-29 20:33:07 +02001726 write_ok = TRUE;
1727#ifdef U_DEBUG
1728 if (headers_written != buf->b_u_numhead)
Bram Moolenaar570064c2013-06-10 20:25:10 +02001729 {
1730 EMSGN("Written %ld headers, ...", headers_written);
1731 EMSGN("... but numhead is %ld", buf->b_u_numhead);
1732 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02001733#endif
1734
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001735#ifdef FEAT_CRYPT
1736 if (bi.bi_state != NULL && undo_flush(&bi) == FAIL)
1737 write_ok = FALSE;
1738#endif
1739
Bram Moolenaar9db58062010-05-29 20:33:07 +02001740write_error:
1741 fclose(fp);
1742 if (!write_ok)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001743 EMSG2(_("E829: write error in undo file: %s"), file_name);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001744
1745#if defined(MACOS_CLASSIC) || defined(WIN3264)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001746 /* Copy file attributes; for systems where this can only be done after
1747 * closing the file. */
Bram Moolenaar9db58062010-05-29 20:33:07 +02001748 if (buf->b_ffname != NULL)
1749 (void)mch_copy_file_attribute(buf->b_ffname, file_name);
1750#endif
1751#ifdef HAVE_ACL
1752 if (buf->b_ffname != NULL)
1753 {
1754 vim_acl_T acl;
1755
1756 /* For systems that support ACL: get the ACL from the original file. */
1757 acl = mch_get_acl(buf->b_ffname);
1758 mch_set_acl(file_name, acl);
Bram Moolenaard2aed442012-06-01 13:46:12 +02001759 mch_free_acl(acl);
Bram Moolenaar9db58062010-05-29 20:33:07 +02001760 }
1761#endif
1762
1763theend:
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001764#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001765 if (bi.bi_state != NULL)
1766 crypt_free_state(bi.bi_state);
1767 vim_free(bi.bi_buffer);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001768#endif
Bram Moolenaar9db58062010-05-29 20:33:07 +02001769 if (file_name != name)
1770 vim_free(file_name);
1771}
1772
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001773/*
1774 * Load the undo tree from an undo file.
1775 * If "name" is not NULL use it as the undo file name. This also means being
1776 * a bit more verbose.
1777 * Otherwise use curbuf->b_ffname to generate the undo file name.
1778 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text.
1779 */
1780 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001781u_read_undo(char_u *name, char_u *hash, char_u *orig_name)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001782{
1783 char_u *file_name;
1784 FILE *fp;
Bram Moolenaar9db58062010-05-29 20:33:07 +02001785 long version, str_len;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001786 char_u *line_ptr = NULL;
1787 linenr_T line_lnum;
1788 colnr_T line_colnr;
1789 linenr_T line_count;
Bram Moolenaar442b4222010-05-24 21:34:22 +02001790 int num_head = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001791 long old_header_seq, new_header_seq, cur_header_seq;
1792 long seq_last, seq_cur;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001793 long last_save_nr = 0;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001794 short old_idx = -1, new_idx = -1, cur_idx = -1;
1795 long num_read_uhps = 0;
1796 time_t seq_time;
1797 int i, j;
1798 int c;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001799 u_header_T *uhp;
1800 u_header_T **uhp_table = NULL;
1801 char_u read_hash[UNDO_HASH_SIZE];
Bram Moolenaar9db58062010-05-29 20:33:07 +02001802 char_u magic_buf[UF_START_MAGIC_LEN];
1803#ifdef U_DEBUG
1804 int *uhp_table_used;
1805#endif
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001806#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001807 stat_T st_orig;
1808 stat_T st_undo;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001809#endif
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001810 bufinfo_T bi;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001811
Bram Moolenaar5a669b92014-08-12 20:14:33 +02001812 vim_memset(&bi, 0, sizeof(bi));
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001813 if (name == NULL)
1814 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001815 file_name = u_get_undo_file_name(curbuf->b_ffname, TRUE);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001816 if (file_name == NULL)
1817 return;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001818
1819#ifdef UNIX
1820 /* For safety we only read an undo file if the owner is equal to the
Bram Moolenaar3b262392013-09-08 15:40:49 +02001821 * owner of the text file or equal to the current user. */
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001822 if (mch_stat((char *)orig_name, &st_orig) >= 0
1823 && mch_stat((char *)file_name, &st_undo) >= 0
Bram Moolenaar3b262392013-09-08 15:40:49 +02001824 && st_orig.st_uid != st_undo.st_uid
1825 && st_undo.st_uid != getuid())
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001826 {
1827 if (p_verbose > 0)
1828 {
1829 verbose_enter();
1830 smsg((char_u *)_("Not reading undo file, owner differs: %s"),
1831 file_name);
1832 verbose_leave();
1833 }
1834 return;
1835 }
1836#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001837 }
1838 else
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001839 file_name = name;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001840
1841 if (p_verbose > 0)
Bram Moolenaar504a8212010-05-30 17:17:42 +02001842 {
1843 verbose_enter();
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001844 smsg((char_u *)_("Reading undo file: %s"), file_name);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001845 verbose_leave();
1846 }
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001847
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001848 fp = mch_fopen((char *)file_name, "r");
1849 if (fp == NULL)
1850 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001851 if (name != NULL || p_verbose > 0)
1852 EMSG2(_("E822: Cannot open undo file for reading: %s"), file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001853 goto error;
1854 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001855 bi.bi_buf = curbuf;
1856 bi.bi_fp = fp;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001857
Bram Moolenaar9db58062010-05-29 20:33:07 +02001858 /*
1859 * Read the undo file header.
1860 */
1861 if (fread(magic_buf, UF_START_MAGIC_LEN, 1, fp) != 1
1862 || memcmp(magic_buf, UF_START_MAGIC, UF_START_MAGIC_LEN) != 0)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001863 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001864 EMSG2(_("E823: Not an undo file: %s"), file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001865 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001866 }
1867 version = get2c(fp);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001868 if (version == UF_VERSION_CRYPT)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001869 {
1870#ifdef FEAT_CRYPT
Bram Moolenaar56be9502010-06-06 14:20:26 +02001871 if (*curbuf->b_p_key == NUL)
1872 {
1873 EMSG2(_("E832: Non-encrypted file has encrypted undo file: %s"),
1874 file_name);
1875 goto error;
1876 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001877 bi.bi_state = crypt_create_from_file(fp, curbuf->b_p_key);
1878 if (bi.bi_state == NULL)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001879 {
1880 EMSG2(_("E826: Undo file decryption failed: %s"), file_name);
1881 goto error;
1882 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001883 if (crypt_whole_undofile(bi.bi_state->method_nr))
1884 {
1885 bi.bi_buffer = alloc(CRYPT_BUF_SIZE);
1886 if (bi.bi_buffer == NULL)
1887 {
1888 crypt_free_state(bi.bi_state);
1889 bi.bi_state = NULL;
1890 goto error;
1891 }
1892 bi.bi_avail = 0;
1893 bi.bi_used = 0;
1894 }
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001895#else
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001896 EMSG2(_("E827: Undo file is encrypted: %s"), file_name);
1897 goto error;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02001898#endif
1899 }
Bram Moolenaar69154f22010-07-18 21:42:34 +02001900 else if (version != UF_VERSION)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001901 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001902 EMSG2(_("E824: Incompatible undo file: %s"), file_name);
1903 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001904 }
1905
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001906 if (undo_read(&bi, read_hash, (size_t)UNDO_HASH_SIZE) == FAIL)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001907 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02001908 corruption_error("hash", file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001909 goto error;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02001910 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001911 line_count = (linenr_T)undo_read_4c(&bi);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001912 if (memcmp(hash, read_hash, UNDO_HASH_SIZE) != 0
1913 || line_count != curbuf->b_ml.ml_line_count)
1914 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001915 if (p_verbose > 0 || name != NULL)
1916 {
Bram Moolenaar504a8212010-05-30 17:17:42 +02001917 if (name == NULL)
1918 verbose_enter();
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001919 give_warning((char_u *)
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001920 _("File contents changed, cannot use undo info"), TRUE);
Bram Moolenaar504a8212010-05-30 17:17:42 +02001921 if (name == NULL)
1922 verbose_leave();
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001923 }
1924 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001925 }
1926
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001927 /* Read undo data for "U" command. */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001928 str_len = undo_read_4c(&bi);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001929 if (str_len < 0)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001930 goto error;
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001931 if (str_len > 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001932 line_ptr = read_string_decrypt(&bi, str_len);
1933 line_lnum = (linenr_T)undo_read_4c(&bi);
1934 line_colnr = (colnr_T)undo_read_4c(&bi);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001935 if (line_lnum < 0 || line_colnr < 0)
1936 {
1937 corruption_error("line lnum/col", file_name);
1938 goto error;
1939 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001940
1941 /* Begin general undo data */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001942 old_header_seq = undo_read_4c(&bi);
1943 new_header_seq = undo_read_4c(&bi);
1944 cur_header_seq = undo_read_4c(&bi);
1945 num_head = undo_read_4c(&bi);
1946 seq_last = undo_read_4c(&bi);
1947 seq_cur = undo_read_4c(&bi);
1948 seq_time = undo_read_time(&bi);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001949
Bram Moolenaar69154f22010-07-18 21:42:34 +02001950 /* Optional header fields. */
1951 for (;;)
1952 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001953 int len = undo_read_byte(&bi);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001954 int what;
Bram Moolenaara800b422010-06-27 01:15:55 +02001955
Bram Moolenaar69154f22010-07-18 21:42:34 +02001956 if (len == 0 || len == EOF)
1957 break;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001958 what = undo_read_byte(&bi);
Bram Moolenaar69154f22010-07-18 21:42:34 +02001959 switch (what)
1960 {
1961 case UF_LAST_SAVE_NR:
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001962 last_save_nr = undo_read_4c(&bi);
Bram Moolenaara800b422010-06-27 01:15:55 +02001963 break;
Bram Moolenaar69154f22010-07-18 21:42:34 +02001964 default:
1965 /* field not supported, skip */
1966 while (--len >= 0)
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001967 (void)undo_read_byte(&bi);
Bram Moolenaara800b422010-06-27 01:15:55 +02001968 }
Bram Moolenaar69154f22010-07-18 21:42:34 +02001969 }
Bram Moolenaara800b422010-06-27 01:15:55 +02001970
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001971 /* uhp_table will store the freshly created undo headers we allocate
1972 * until we insert them into curbuf. The table remains sorted by the
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001973 * sequence numbers of the headers.
1974 * When there are no headers uhp_table is NULL. */
1975 if (num_head > 0)
1976 {
1977 uhp_table = (u_header_T **)U_ALLOC_LINE(
1978 num_head * sizeof(u_header_T *));
1979 if (uhp_table == NULL)
1980 goto error;
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02001981 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001982
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001983 while ((c = undo_read_2c(&bi)) == UF_HEADER_MAGIC)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001984 {
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001985 if (num_read_uhps >= num_head)
1986 {
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001987 corruption_error("num_head too small", file_name);
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02001988 goto error;
1989 }
1990
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001991 uhp = unserialize_uhp(&bi, file_name);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02001992 if (uhp == NULL)
1993 goto error;
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02001994 uhp_table[num_read_uhps++] = uhp;
1995 }
1996
1997 if (num_read_uhps != num_head)
1998 {
1999 corruption_error("num_head", file_name);
2000 goto error;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002001 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02002002 if (c != UF_HEADER_END_MAGIC)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002003 {
Bram Moolenaar9db58062010-05-29 20:33:07 +02002004 corruption_error("end marker", file_name);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002005 goto error;
2006 }
2007
Bram Moolenaar9db58062010-05-29 20:33:07 +02002008#ifdef U_DEBUG
2009 uhp_table_used = (int *)alloc_clear(
2010 (unsigned)(sizeof(int) * num_head + 1));
2011# define SET_FLAG(j) ++uhp_table_used[j]
2012#else
2013# define SET_FLAG(j)
2014#endif
2015
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002016 /* We have put all of the headers into a table. Now we iterate through the
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002017 * table and swizzle each sequence number we have stored in uh_*_seq into
2018 * a pointer corresponding to the header with that sequence number. */
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002019 for (i = 0; i < num_head; i++)
2020 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002021 uhp = uhp_table[i];
2022 if (uhp == NULL)
2023 continue;
2024 for (j = 0; j < num_head; j++)
2025 if (uhp_table[j] != NULL && i != j
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002026 && uhp_table[i]->uh_seq == uhp_table[j]->uh_seq)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002027 {
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02002028 corruption_error("duplicate uh_seq", file_name);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002029 goto error;
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02002030 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002031 for (j = 0; j < num_head; j++)
2032 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002033 && uhp_table[j]->uh_seq == uhp->uh_next.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002034 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002035 uhp->uh_next.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02002036 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002037 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002038 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002039 for (j = 0; j < num_head; j++)
2040 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002041 && uhp_table[j]->uh_seq == uhp->uh_prev.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002042 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002043 uhp->uh_prev.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02002044 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002045 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002046 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002047 for (j = 0; j < num_head; j++)
2048 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002049 && uhp_table[j]->uh_seq == uhp->uh_alt_next.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002050 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002051 uhp->uh_alt_next.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02002052 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002053 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002054 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002055 for (j = 0; j < num_head; j++)
2056 if (uhp_table[j] != NULL
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002057 && uhp_table[j]->uh_seq == uhp->uh_alt_prev.seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002058 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002059 uhp->uh_alt_prev.ptr = uhp_table[j];
Bram Moolenaar9db58062010-05-29 20:33:07 +02002060 SET_FLAG(j);
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002061 break;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002062 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002063 if (old_header_seq > 0 && old_idx < 0 && uhp->uh_seq == old_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002064 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002065 old_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002066 SET_FLAG(i);
2067 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002068 if (new_header_seq > 0 && new_idx < 0 && uhp->uh_seq == new_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002069 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002070 new_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002071 SET_FLAG(i);
2072 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002073 if (cur_header_seq > 0 && cur_idx < 0 && uhp->uh_seq == cur_header_seq)
Bram Moolenaar9db58062010-05-29 20:33:07 +02002074 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002075 cur_idx = i;
Bram Moolenaar9db58062010-05-29 20:33:07 +02002076 SET_FLAG(i);
2077 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002078 }
Bram Moolenaar9db58062010-05-29 20:33:07 +02002079
2080 /* Now that we have read the undo info successfully, free the current undo
2081 * info and use the info from the file. */
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002082 u_blockfree(curbuf);
Bram Moolenaar9db58062010-05-29 20:33:07 +02002083 curbuf->b_u_oldhead = old_idx < 0 ? NULL : uhp_table[old_idx];
2084 curbuf->b_u_newhead = new_idx < 0 ? NULL : uhp_table[new_idx];
2085 curbuf->b_u_curhead = cur_idx < 0 ? NULL : uhp_table[cur_idx];
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002086 curbuf->b_u_line_ptr = line_ptr;
2087 curbuf->b_u_line_lnum = line_lnum;
2088 curbuf->b_u_line_colnr = line_colnr;
2089 curbuf->b_u_numhead = num_head;
2090 curbuf->b_u_seq_last = seq_last;
2091 curbuf->b_u_seq_cur = seq_cur;
Bram Moolenaara800b422010-06-27 01:15:55 +02002092 curbuf->b_u_time_cur = seq_time;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002093 curbuf->b_u_save_nr_last = last_save_nr;
Bram Moolenaardba01a02010-11-03 19:32:42 +01002094 curbuf->b_u_save_nr_cur = last_save_nr;
Bram Moolenaar6773b2b2010-05-30 16:01:37 +02002095
2096 curbuf->b_u_synced = TRUE;
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002097 vim_free(uhp_table);
Bram Moolenaar9db58062010-05-29 20:33:07 +02002098
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002099#ifdef U_DEBUG
Bram Moolenaar9db58062010-05-29 20:33:07 +02002100 for (i = 0; i < num_head; ++i)
2101 if (uhp_table_used[i] == 0)
2102 EMSGN("uhp_table entry %ld not used, leaking memory", i);
2103 vim_free(uhp_table_used);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002104 u_check(TRUE);
2105#endif
Bram Moolenaar9db58062010-05-29 20:33:07 +02002106
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002107 if (name != NULL)
2108 smsg((char_u *)_("Finished reading undo file %s"), file_name);
2109 goto theend;
2110
2111error:
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002112 vim_free(line_ptr);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002113 if (uhp_table != NULL)
2114 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002115 for (i = 0; i < num_read_uhps; i++)
2116 if (uhp_table[i] != NULL)
Bram Moolenaar6a18eb62010-05-26 21:21:00 +02002117 u_free_uhp(uhp_table[i]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002118 vim_free(uhp_table);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002119 }
2120
2121theend:
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002122#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02002123 if (bi.bi_state != NULL)
2124 crypt_free_state(bi.bi_state);
2125 vim_free(bi.bi_buffer);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002126#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002127 if (fp != NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002128 fclose(fp);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002129 if (file_name != name)
2130 vim_free(file_name);
2131 return;
2132}
2133
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002134#endif /* FEAT_PERSISTENT_UNDO */
2135
2136
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137/*
2138 * If 'cpoptions' contains 'u': Undo the previous undo or redo (vi compatible).
2139 * If 'cpoptions' does not contain 'u': Always undo.
2140 */
2141 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002142u_undo(int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143{
2144 /*
2145 * If we get an undo command while executing a macro, we behave like the
2146 * original vi. If this happens twice in one macro the result will not
2147 * be compatible.
2148 */
2149 if (curbuf->b_u_synced == FALSE)
2150 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002151 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 count = 1;
2153 }
2154
2155 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
2156 undo_undoes = TRUE;
2157 else
2158 undo_undoes = !undo_undoes;
2159 u_doit(count);
2160}
2161
2162/*
2163 * If 'cpoptions' contains 'u': Repeat the previous undo or redo.
2164 * If 'cpoptions' does not contain 'u': Always redo.
2165 */
2166 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002167u_redo(int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168{
2169 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
2170 undo_undoes = FALSE;
2171 u_doit(count);
2172}
2173
2174/*
2175 * Undo or redo, depending on 'undo_undoes', 'count' times.
2176 */
2177 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002178u_doit(int startcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179{
Bram Moolenaarca003e12006-03-17 23:19:38 +00002180 int count = startcount;
2181
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002182 if (!undo_allowed())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184
2185 u_newcount = 0;
2186 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002187 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2188 u_oldcount = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 while (count--)
2190 {
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002191 /* Do the change warning now, so that it triggers FileChangedRO when
2192 * needed. This may cause the file to be reloaded, that must happen
2193 * before we do anything, because it may change curbuf->b_u_curhead
2194 * and more. */
2195 change_warning(0);
2196
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 if (undo_undoes)
2198 {
2199 if (curbuf->b_u_curhead == NULL) /* first undo */
2200 curbuf->b_u_curhead = curbuf->b_u_newhead;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002201 else if (get_undolevel() > 0) /* multi level undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 /* get next undo */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002203 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_next.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 /* nothing to undo */
2205 if (curbuf->b_u_numhead == 0 || curbuf->b_u_curhead == NULL)
2206 {
2207 /* stick curbuf->b_u_curhead at end */
2208 curbuf->b_u_curhead = curbuf->b_u_oldhead;
2209 beep_flush();
Bram Moolenaarca003e12006-03-17 23:19:38 +00002210 if (count == startcount - 1)
2211 {
2212 MSG(_("Already at oldest change"));
2213 return;
2214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 break;
2216 }
2217
Bram Moolenaarca003e12006-03-17 23:19:38 +00002218 u_undoredo(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 }
2220 else
2221 {
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002222 if (curbuf->b_u_curhead == NULL || get_undolevel() <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 {
2224 beep_flush(); /* nothing to redo */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002225 if (count == startcount - 1)
2226 {
2227 MSG(_("Already at newest change"));
2228 return;
2229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 break;
2231 }
2232
Bram Moolenaarca003e12006-03-17 23:19:38 +00002233 u_undoredo(FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002234
2235 /* Advance for next redo. Set "newhead" when at the end of the
2236 * redoable changes. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002237 if (curbuf->b_u_curhead->uh_prev.ptr == NULL)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002238 curbuf->b_u_newhead = curbuf->b_u_curhead;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002239 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_prev.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 }
2241 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00002242 u_undo_end(undo_undoes, FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002243}
2244
Bram Moolenaar1e607892006-03-13 22:15:53 +00002245/*
2246 * Undo or redo over the timeline.
2247 * When "step" is negative go back in time, otherwise goes forward in time.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002248 * When "sec" is FALSE make "step" steps, when "sec" is TRUE use "step" as
2249 * seconds.
Bram Moolenaar730cde92010-06-27 05:18:54 +02002250 * When "file" is TRUE use "step" as a number of file writes.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002251 * When "absolute" is TRUE use "step" as the sequence number to jump to.
2252 * "sec" must be FALSE then.
Bram Moolenaar1e607892006-03-13 22:15:53 +00002253 */
2254 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002255undo_time(
2256 long step,
2257 int sec,
2258 int file,
2259 int absolute)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002260{
2261 long target;
2262 long closest;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002263 long closest_start;
2264 long closest_seq = 0;
2265 long val;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002266 u_header_T *uhp;
2267 u_header_T *last;
2268 int mark;
2269 int nomark;
2270 int round;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002271 int dosec = sec;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002272 int dofile = file;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002273 int above = FALSE;
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002274 int did_undo = TRUE;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002275
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002276 /* First make sure the current undoable change is synced. */
2277 if (curbuf->b_u_synced == FALSE)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002278 u_sync(TRUE);
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00002279
Bram Moolenaar1e607892006-03-13 22:15:53 +00002280 u_newcount = 0;
2281 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002282 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002283 u_oldcount = -1;
2284
Bram Moolenaarca003e12006-03-17 23:19:38 +00002285 /* "target" is the node below which we want to be.
2286 * Init "closest" to a value we can't reach. */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002287 if (absolute)
2288 {
Bram Moolenaard22e9462016-03-15 17:43:55 +01002289 if (step == 0)
2290 {
2291 /* target 0 does not exist, got to 1 and above it. */
2292 target = 1;
2293 above = TRUE;
2294 }
2295 else
2296 target = step;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002297 closest = -1;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002298 }
Bram Moolenaarca003e12006-03-17 23:19:38 +00002299 else
Bram Moolenaar1e607892006-03-13 22:15:53 +00002300 {
Bram Moolenaar730cde92010-06-27 05:18:54 +02002301 if (dosec)
Bram Moolenaarcbd4de42017-01-07 16:14:57 +01002302 target = (long)(curbuf->b_u_time_cur) + step;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002303 else if (dofile)
2304 {
2305 if (step < 0)
2306 {
2307 /* Going back to a previous write. If there were changes after
2308 * the last write, count that as moving one file-write, so
2309 * that ":earlier 1f" undoes all changes since the last save. */
2310 uhp = curbuf->b_u_curhead;
2311 if (uhp != NULL)
2312 uhp = uhp->uh_next.ptr;
2313 else
2314 uhp = curbuf->b_u_newhead;
2315 if (uhp != NULL && uhp->uh_save_nr != 0)
2316 /* "uh_save_nr" was set in the last block, that means
2317 * there were no changes since the last write */
2318 target = curbuf->b_u_save_nr_cur + step;
2319 else
2320 /* count the changes since the last write as one step */
2321 target = curbuf->b_u_save_nr_cur + step + 1;
2322 if (target <= 0)
2323 /* Go to before first write: before the oldest change. Use
2324 * the sequence number for that. */
2325 dofile = FALSE;
2326 }
2327 else
2328 {
2329 /* Moving forward to a newer write. */
2330 target = curbuf->b_u_save_nr_cur + step;
2331 if (target > curbuf->b_u_save_nr_last)
2332 {
2333 /* Go to after last write: after the latest change. Use
2334 * the sequence number for that. */
2335 target = curbuf->b_u_seq_last + 1;
2336 dofile = FALSE;
2337 }
2338 }
2339 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002340 else
2341 target = curbuf->b_u_seq_cur + step;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002342 if (step < 0)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002343 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002344 if (target < 0)
2345 target = 0;
2346 closest = -1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002347 }
2348 else
2349 {
Bram Moolenaar730cde92010-06-27 05:18:54 +02002350 if (dosec)
Bram Moolenaarcbd4de42017-01-07 16:14:57 +01002351 closest = (long)(vim_time() + 1);
Bram Moolenaar730cde92010-06-27 05:18:54 +02002352 else if (dofile)
2353 closest = curbuf->b_u_save_nr_last + 2;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002354 else
2355 closest = curbuf->b_u_seq_last + 2;
2356 if (target >= closest)
2357 target = closest - 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002358 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002359 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002360 closest_start = closest;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002361 closest_seq = curbuf->b_u_seq_cur;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002362
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002363 /*
2364 * May do this twice:
Bram Moolenaar1e607892006-03-13 22:15:53 +00002365 * 1. Search for "target", update "closest" to the best match found.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002366 * 2. If "target" not found search for "closest".
2367 *
2368 * When using the closest time we use the sequence number in the second
2369 * round, because there may be several entries with the same time.
2370 */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002371 for (round = 1; round <= 2; ++round)
2372 {
2373 /* Find the path from the current state to where we want to go. The
2374 * desired state can be anywhere in the undo tree, need to go all over
2375 * it. We put "nomark" in uh_walk where we have been without success,
2376 * "mark" where it could possibly be. */
2377 mark = ++lastmark;
2378 nomark = ++lastmark;
2379
2380 if (curbuf->b_u_curhead == NULL) /* at leaf of the tree */
2381 uhp = curbuf->b_u_newhead;
2382 else
2383 uhp = curbuf->b_u_curhead;
2384
2385 while (uhp != NULL)
2386 {
2387 uhp->uh_walk = mark;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002388 if (dosec)
Bram Moolenaarcbd4de42017-01-07 16:14:57 +01002389 val = (long)(uhp->uh_time);
Bram Moolenaar730cde92010-06-27 05:18:54 +02002390 else if (dofile)
2391 val = uhp->uh_save_nr;
2392 else
2393 val = uhp->uh_seq;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002394
Bram Moolenaar730cde92010-06-27 05:18:54 +02002395 if (round == 1 && !(dofile && val == 0))
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002396 {
2397 /* Remember the header that is closest to the target.
2398 * It must be at least in the right direction (checked with
Bram Moolenaarca003e12006-03-17 23:19:38 +00002399 * "b_u_seq_cur"). When the timestamp is equal find the
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002400 * highest/lowest sequence number. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002401 if ((step < 0 ? uhp->uh_seq <= curbuf->b_u_seq_cur
2402 : uhp->uh_seq > curbuf->b_u_seq_cur)
2403 && ((dosec && val == closest)
2404 ? (step < 0
2405 ? uhp->uh_seq < closest_seq
2406 : uhp->uh_seq > closest_seq)
2407 : closest == closest_start
2408 || (val > target
2409 ? (closest > target
2410 ? val - target <= closest - target
2411 : val - target <= target - closest)
2412 : (closest > target
2413 ? target - val <= closest - target
2414 : target - val <= target - closest))))
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002415 {
2416 closest = val;
2417 closest_seq = uhp->uh_seq;
2418 }
2419 }
2420
2421 /* Quit searching when we found a match. But when searching for a
2422 * time we need to continue looking for the best uh_seq. */
2423 if (target == val && !dosec)
Bram Moolenaar730cde92010-06-27 05:18:54 +02002424 {
2425 target = uhp->uh_seq;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002426 break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002427 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002428
2429 /* go down in the tree if we haven't been there */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002430 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != nomark
2431 && uhp->uh_prev.ptr->uh_walk != mark)
2432 uhp = uhp->uh_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002433
2434 /* go to alternate branch if we haven't been there */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002435 else if (uhp->uh_alt_next.ptr != NULL
2436 && uhp->uh_alt_next.ptr->uh_walk != nomark
2437 && uhp->uh_alt_next.ptr->uh_walk != mark)
2438 uhp = uhp->uh_alt_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002439
2440 /* go up in the tree if we haven't been there and we are at the
2441 * start of alternate branches */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002442 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
2443 && uhp->uh_next.ptr->uh_walk != nomark
2444 && uhp->uh_next.ptr->uh_walk != mark)
Bram Moolenaardb552d602006-03-23 22:59:57 +00002445 {
2446 /* If still at the start we don't go through this change. */
2447 if (uhp == curbuf->b_u_curhead)
2448 uhp->uh_walk = nomark;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002449 uhp = uhp->uh_next.ptr;
Bram Moolenaardb552d602006-03-23 22:59:57 +00002450 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002451
2452 else
2453 {
2454 /* need to backtrack; mark this node as useless */
2455 uhp->uh_walk = nomark;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002456 if (uhp->uh_alt_prev.ptr != NULL)
2457 uhp = uhp->uh_alt_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002458 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002459 uhp = uhp->uh_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002460 }
2461 }
2462
2463 if (uhp != NULL) /* found it */
2464 break;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002465
2466 if (absolute)
2467 {
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002468 EMSGN(_("E830: Undo number %ld not found"), step);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002469 return;
2470 }
2471
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002472 if (closest == closest_start)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002473 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002474 if (step < 0)
2475 MSG(_("Already at oldest change"));
2476 else
2477 MSG(_("Already at newest change"));
Bram Moolenaar1e607892006-03-13 22:15:53 +00002478 return;
2479 }
2480
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002481 target = closest_seq;
2482 dosec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02002483 dofile = FALSE;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002484 if (step < 0)
2485 above = TRUE; /* stop above the header */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002486 }
2487
2488 /* If we found it: Follow the path to go to where we want to be. */
2489 if (uhp != NULL)
2490 {
2491 /*
2492 * First go up the tree as much as needed.
2493 */
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002494 while (!got_int)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002495 {
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002496 /* Do the change warning now, for the same reason as above. */
2497 change_warning(0);
2498
Bram Moolenaar1e607892006-03-13 22:15:53 +00002499 uhp = curbuf->b_u_curhead;
2500 if (uhp == NULL)
2501 uhp = curbuf->b_u_newhead;
2502 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002503 uhp = uhp->uh_next.ptr;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002504 if (uhp == NULL || uhp->uh_walk != mark
2505 || (uhp->uh_seq == target && !above))
Bram Moolenaar1e607892006-03-13 22:15:53 +00002506 break;
2507 curbuf->b_u_curhead = uhp;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002508 u_undoredo(TRUE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002509 uhp->uh_walk = nomark; /* don't go back down here */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002510 }
2511
2512 /*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002513 * And now go down the tree (redo), branching off where needed.
Bram Moolenaar1e607892006-03-13 22:15:53 +00002514 */
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002515 while (!got_int)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002516 {
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002517 /* Do the change warning now, for the same reason as above. */
2518 change_warning(0);
2519
2520 uhp = curbuf->b_u_curhead;
2521 if (uhp == NULL)
2522 break;
2523
Bram Moolenaar89ed3df2007-01-09 19:23:12 +00002524 /* Go back to the first branch with a mark. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002525 while (uhp->uh_alt_prev.ptr != NULL
2526 && uhp->uh_alt_prev.ptr->uh_walk == mark)
2527 uhp = uhp->uh_alt_prev.ptr;
Bram Moolenaar89ed3df2007-01-09 19:23:12 +00002528
Bram Moolenaar1e607892006-03-13 22:15:53 +00002529 /* Find the last branch with a mark, that's the one. */
2530 last = uhp;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002531 while (last->uh_alt_next.ptr != NULL
2532 && last->uh_alt_next.ptr->uh_walk == mark)
2533 last = last->uh_alt_next.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002534 if (last != uhp)
2535 {
2536 /* Make the used branch the first entry in the list of
2537 * alternatives to make "u" and CTRL-R take this branch. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002538 while (uhp->uh_alt_prev.ptr != NULL)
2539 uhp = uhp->uh_alt_prev.ptr;
2540 if (last->uh_alt_next.ptr != NULL)
2541 last->uh_alt_next.ptr->uh_alt_prev.ptr =
2542 last->uh_alt_prev.ptr;
2543 last->uh_alt_prev.ptr->uh_alt_next.ptr = last->uh_alt_next.ptr;
2544 last->uh_alt_prev.ptr = NULL;
2545 last->uh_alt_next.ptr = uhp;
2546 uhp->uh_alt_prev.ptr = last;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002547
Bram Moolenaar8f1f6292010-05-30 16:55:22 +02002548 if (curbuf->b_u_oldhead == uhp)
2549 curbuf->b_u_oldhead = last;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002550 uhp = last;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002551 if (uhp->uh_next.ptr != NULL)
2552 uhp->uh_next.ptr->uh_prev.ptr = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002553 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002554 curbuf->b_u_curhead = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002555
2556 if (uhp->uh_walk != mark)
2557 break; /* must have reached the target */
2558
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002559 /* Stop when going backwards in time and didn't find the exact
2560 * header we were looking for. */
2561 if (uhp->uh_seq == target && above)
Bram Moolenaardb552d602006-03-23 22:59:57 +00002562 {
2563 curbuf->b_u_seq_cur = target - 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002564 break;
Bram Moolenaardb552d602006-03-23 22:59:57 +00002565 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002566
Bram Moolenaarca003e12006-03-17 23:19:38 +00002567 u_undoredo(FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00002568
2569 /* Advance "curhead" to below the header we last used. If it
2570 * becomes NULL then we need to set "newhead" to this leaf. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002571 if (uhp->uh_prev.ptr == NULL)
Bram Moolenaar1e607892006-03-13 22:15:53 +00002572 curbuf->b_u_newhead = uhp;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002573 curbuf->b_u_curhead = uhp->uh_prev.ptr;
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002574 did_undo = FALSE;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002575
2576 if (uhp->uh_seq == target) /* found it! */
2577 break;
2578
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002579 uhp = uhp->uh_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002580 if (uhp == NULL || uhp->uh_walk != mark)
2581 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002582 /* Need to redo more but can't find it... */
Bram Moolenaar95f09602016-11-10 20:01:45 +01002583 internal_error("undo_time()");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002584 break;
2585 }
2586 }
2587 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00002588 u_undo_end(did_undo, absolute);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589}
2590
2591/*
2592 * u_undoredo: common code for undo and redo
2593 *
2594 * The lines in the file are replaced by the lines in the entry list at
2595 * curbuf->b_u_curhead. The replaced lines in the file are saved in the entry
2596 * list for the next undo/redo.
Bram Moolenaarca003e12006-03-17 23:19:38 +00002597 *
2598 * When "undo" is TRUE we go up in the tree, when FALSE we go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 */
2600 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002601u_undoredo(int undo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602{
2603 char_u **newarray = NULL;
2604 linenr_T oldsize;
2605 linenr_T newsize;
2606 linenr_T top, bot;
2607 linenr_T lnum;
2608 linenr_T newlnum = MAXLNUM;
2609 long i;
2610 u_entry_T *uep, *nuep;
2611 u_entry_T *newlist = NULL;
2612 int old_flags;
2613 int new_flags;
2614 pos_T namedm[NMARKS];
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002615 visualinfo_T visualinfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 int empty_buffer; /* buffer became empty */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002617 u_header_T *curhead = curbuf->b_u_curhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002619#ifdef FEAT_AUTOCMD
2620 /* Don't want autocommands using the undo structures here, they are
2621 * invalid till the end. */
2622 block_autocmds();
2623#endif
2624
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002625#ifdef U_DEBUG
2626 u_check(FALSE);
2627#endif
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002628 old_flags = curhead->uh_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 new_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
2630 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
2631 setpcmark();
2632
2633 /*
2634 * save marks before undo/redo
2635 */
2636 mch_memmove(namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002637 visualinfo = curbuf->b_visual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 curbuf->b_op_start.lnum = curbuf->b_ml.ml_line_count;
2639 curbuf->b_op_start.col = 0;
2640 curbuf->b_op_end.lnum = 0;
2641 curbuf->b_op_end.col = 0;
2642
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002643 for (uep = curhead->uh_entry; uep != NULL; uep = nuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 {
2645 top = uep->ue_top;
2646 bot = uep->ue_bot;
2647 if (bot == 0)
2648 bot = curbuf->b_ml.ml_line_count + 1;
2649 if (top > curbuf->b_ml.ml_line_count || top >= bot
2650 || bot > curbuf->b_ml.ml_line_count + 1)
2651 {
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002652#ifdef FEAT_AUTOCMD
2653 unblock_autocmds();
2654#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01002655 IEMSG(_("E438: u_undo: line numbers wrong"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 changed(); /* don't want UNCHANGED now */
2657 return;
2658 }
2659
2660 oldsize = bot - top - 1; /* number of lines before undo */
2661 newsize = uep->ue_size; /* number of lines after undo */
2662
2663 if (top < newlnum)
2664 {
2665 /* If the saved cursor is somewhere in this undo block, move it to
2666 * the remembered position. Makes "gwap" put the cursor back
2667 * where it was. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002668 lnum = curhead->uh_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 if (lnum >= top && lnum <= top + newsize + 1)
2670 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002671 curwin->w_cursor = curhead->uh_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 newlnum = curwin->w_cursor.lnum - 1;
2673 }
2674 else
2675 {
2676 /* Use the first line that actually changed. Avoids that
2677 * undoing auto-formatting puts the cursor in the previous
2678 * line. */
2679 for (i = 0; i < newsize && i < oldsize; ++i)
2680 if (STRCMP(uep->ue_array[i], ml_get(top + 1 + i)) != 0)
2681 break;
2682 if (i == newsize && newlnum == MAXLNUM && uep->ue_next == NULL)
2683 {
2684 newlnum = top;
2685 curwin->w_cursor.lnum = newlnum + 1;
2686 }
2687 else if (i < newsize)
2688 {
2689 newlnum = top + i;
2690 curwin->w_cursor.lnum = newlnum + 1;
2691 }
2692 }
2693 }
2694
2695 empty_buffer = FALSE;
2696
2697 /* delete the lines between top and bot and save them in newarray */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002698 if (oldsize > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002700 if ((newarray = (char_u **)U_ALLOC_LINE(
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002701 sizeof(char_u *) * oldsize)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 {
2703 do_outofmem_msg((long_u)(sizeof(char_u *) * oldsize));
2704 /*
2705 * We have messed up the entry list, repair is impossible.
2706 * we have to free the rest of the list.
2707 */
2708 while (uep != NULL)
2709 {
2710 nuep = uep->ue_next;
2711 u_freeentry(uep, uep->ue_size);
2712 uep = nuep;
2713 }
2714 break;
2715 }
2716 /* delete backwards, it goes faster in most cases */
2717 for (lnum = bot - 1, i = oldsize; --i >= 0; --lnum)
2718 {
2719 /* what can we do when we run out of memory? */
2720 if ((newarray[i] = u_save_line(lnum)) == NULL)
2721 do_outofmem_msg((long_u)0);
2722 /* remember we deleted the last line in the buffer, and a
2723 * dummy empty line will be inserted */
2724 if (curbuf->b_ml.ml_line_count == 1)
2725 empty_buffer = TRUE;
2726 ml_delete(lnum, FALSE);
2727 }
2728 }
Bram Moolenaar8d343302005-07-12 22:46:17 +00002729 else
2730 newarray = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731
2732 /* insert the lines in u_array between top and bot */
2733 if (newsize)
2734 {
2735 for (lnum = top, i = 0; i < newsize; ++i, ++lnum)
2736 {
2737 /*
2738 * If the file is empty, there is an empty line 1 that we
2739 * should get rid of, by replacing it with the new line
2740 */
2741 if (empty_buffer && lnum == 0)
2742 ml_replace((linenr_T)1, uep->ue_array[i], TRUE);
2743 else
2744 ml_append(lnum, uep->ue_array[i], (colnr_T)0, FALSE);
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002745 vim_free(uep->ue_array[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 }
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02002747 vim_free((char_u *)uep->ue_array);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 }
2749
2750 /* adjust marks */
2751 if (oldsize != newsize)
2752 {
2753 mark_adjust(top + 1, top + oldsize, (long)MAXLNUM,
2754 (long)newsize - (long)oldsize);
2755 if (curbuf->b_op_start.lnum > top + oldsize)
2756 curbuf->b_op_start.lnum += newsize - oldsize;
2757 if (curbuf->b_op_end.lnum > top + oldsize)
2758 curbuf->b_op_end.lnum += newsize - oldsize;
2759 }
2760
2761 changed_lines(top + 1, 0, bot, newsize - oldsize);
2762
2763 /* set '[ and '] mark */
2764 if (top + 1 < curbuf->b_op_start.lnum)
2765 curbuf->b_op_start.lnum = top + 1;
2766 if (newsize == 0 && top + 1 > curbuf->b_op_end.lnum)
2767 curbuf->b_op_end.lnum = top + 1;
2768 else if (top + newsize > curbuf->b_op_end.lnum)
2769 curbuf->b_op_end.lnum = top + newsize;
2770
2771 u_newcount += newsize;
2772 u_oldcount += oldsize;
2773 uep->ue_size = oldsize;
2774 uep->ue_array = newarray;
2775 uep->ue_bot = top + newsize + 1;
2776
2777 /*
2778 * insert this entry in front of the new entry list
2779 */
2780 nuep = uep->ue_next;
2781 uep->ue_next = newlist;
2782 newlist = uep;
2783 }
2784
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002785 curhead->uh_entry = newlist;
2786 curhead->uh_flags = new_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 if ((old_flags & UH_EMPTYBUF) && bufempty())
2788 curbuf->b_ml.ml_flags |= ML_EMPTY;
2789 if (old_flags & UH_CHANGED)
2790 changed();
2791 else
Bram Moolenaar009b2592004-10-24 19:18:58 +00002792#ifdef FEAT_NETBEANS_INTG
2793 /* per netbeans undo rules, keep it as modified */
2794 if (!isNetbeansModified(curbuf))
2795#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 unchanged(curbuf, FALSE);
2797
2798 /*
2799 * restore marks from before undo/redo
2800 */
2801 for (i = 0; i < NMARKS; ++i)
Bram Moolenaarf65aad52015-02-17 13:43:40 +01002802 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002803 if (curhead->uh_namedm[i].lnum != 0)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002804 curbuf->b_namedm[i] = curhead->uh_namedm[i];
Bram Moolenaarf65aad52015-02-17 13:43:40 +01002805 if (namedm[i].lnum != 0)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002806 curhead->uh_namedm[i] = namedm[i];
Bram Moolenaarf65aad52015-02-17 13:43:40 +01002807 else
2808 curhead->uh_namedm[i].lnum = 0;
2809 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002810 if (curhead->uh_visual.vi_start.lnum != 0)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002811 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002812 curbuf->b_visual = curhead->uh_visual;
2813 curhead->uh_visual = visualinfo;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815
2816 /*
2817 * If the cursor is only off by one line, put it at the same position as
2818 * before starting the change (for the "o" command).
2819 * Otherwise the cursor should go to the first undone line.
2820 */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002821 if (curhead->uh_cursor.lnum + 1 == curwin->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 && curwin->w_cursor.lnum > 1)
2823 --curwin->w_cursor.lnum;
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002824 if (curwin->w_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 {
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002826 if (curhead->uh_cursor.lnum == curwin->w_cursor.lnum)
2827 {
2828 curwin->w_cursor.col = curhead->uh_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002830 if (virtual_active() && curhead->uh_cursor_vcol >= 0)
2831 coladvance((colnr_T)curhead->uh_cursor_vcol);
2832 else
2833 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834#endif
Bram Moolenaar0390ded2010-08-07 12:54:12 +02002835 }
2836 else
2837 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 else
2840 {
2841 /* We get here with the current cursor line being past the end (eg
2842 * after adding lines at the end of the file, and then undoing it).
2843 * check_cursor() will move the cursor to the last line. Move it to
2844 * the first column here. */
2845 curwin->w_cursor.col = 0;
2846#ifdef FEAT_VIRTUALEDIT
2847 curwin->w_cursor.coladd = 0;
2848#endif
2849 }
2850
2851 /* Make sure the cursor is on an existing line and column. */
2852 check_cursor();
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002853
2854 /* Remember where we are for "g-" and ":earlier 10s". */
2855 curbuf->b_u_seq_cur = curhead->uh_seq;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002856 if (undo)
2857 /* We are below the previous undo. However, to make ":earlier 1s"
2858 * work we compute this as being just above the just undone change. */
2859 --curbuf->b_u_seq_cur;
2860
Bram Moolenaar730cde92010-06-27 05:18:54 +02002861 /* Remember where we are for ":earlier 1f" and ":later 1f". */
2862 if (curhead->uh_save_nr != 0)
2863 {
2864 if (undo)
2865 curbuf->b_u_save_nr_cur = curhead->uh_save_nr - 1;
2866 else
2867 curbuf->b_u_save_nr_cur = curhead->uh_save_nr;
2868 }
2869
Bram Moolenaarca003e12006-03-17 23:19:38 +00002870 /* The timestamp can be the same for multiple changes, just use the one of
2871 * the undone/redone change. */
Bram Moolenaara800b422010-06-27 01:15:55 +02002872 curbuf->b_u_time_cur = curhead->uh_time;
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002873
2874#ifdef FEAT_AUTOCMD
2875 unblock_autocmds();
2876#endif
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002877#ifdef U_DEBUG
2878 u_check(FALSE);
2879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880}
2881
2882/*
2883 * If we deleted or added lines, report the number of less/more lines.
2884 * Otherwise, report the number of changes (this may be incorrect
2885 * in some cases, but it's better than nothing).
2886 */
2887 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002888u_undo_end(
2889 int did_undo, /* just did an undo */
2890 int absolute) /* used ":undo N" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891{
Bram Moolenaar89d40322006-08-29 15:30:07 +00002892 char *msgstr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002893 u_header_T *uhp;
2894 char_u msgbuf[80];
Bram Moolenaar1e607892006-03-13 22:15:53 +00002895
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896#ifdef FEAT_FOLDING
2897 if ((fdo_flags & FDO_UNDO) && KeyTyped)
2898 foldOpenCursor();
2899#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +00002900
2901 if (global_busy /* no messages now, wait until global is finished */
2902 || !messaging()) /* 'lazyredraw' set, don't do messages now */
2903 return;
2904
2905 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2906 --u_newcount;
2907
2908 u_oldcount -= u_newcount;
2909 if (u_oldcount == -1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002910 msgstr = N_("more line");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002911 else if (u_oldcount < 0)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002912 msgstr = N_("more lines");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002913 else if (u_oldcount == 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002914 msgstr = N_("line less");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002915 else if (u_oldcount > 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002916 msgstr = N_("fewer lines");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002917 else
2918 {
2919 u_oldcount = u_newcount;
2920 if (u_newcount == 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002921 msgstr = N_("change");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002922 else
Bram Moolenaar89d40322006-08-29 15:30:07 +00002923 msgstr = N_("changes");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002924 }
2925
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002926 if (curbuf->b_u_curhead != NULL)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002927 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00002928 /* For ":undo N" we prefer a "after #N" message. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002929 if (absolute && curbuf->b_u_curhead->uh_next.ptr != NULL)
Bram Moolenaardb552d602006-03-23 22:59:57 +00002930 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002931 uhp = curbuf->b_u_curhead->uh_next.ptr;
Bram Moolenaardb552d602006-03-23 22:59:57 +00002932 did_undo = FALSE;
2933 }
2934 else if (did_undo)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002935 uhp = curbuf->b_u_curhead;
2936 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02002937 uhp = curbuf->b_u_curhead->uh_next.ptr;
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002938 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002939 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002940 uhp = curbuf->b_u_newhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002941
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002942 if (uhp == NULL)
2943 *msgbuf = NUL;
2944 else
2945 u_add_time(msgbuf, sizeof(msgbuf), uhp->uh_time);
2946
Bram Moolenaarb2c03502010-07-02 20:20:09 +02002947#ifdef FEAT_CONCEAL
2948 {
2949 win_T *wp;
2950
2951 FOR_ALL_WINDOWS(wp)
2952 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02002953 if (wp->w_buffer == curbuf && wp->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02002954 redraw_win_later(wp, NOT_VALID);
2955 }
2956 }
2957#endif
2958
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002959 smsg((char_u *)_("%ld %s; %s #%ld %s"),
Bram Moolenaarca003e12006-03-17 23:19:38 +00002960 u_oldcount < 0 ? -u_oldcount : u_oldcount,
Bram Moolenaar89d40322006-08-29 15:30:07 +00002961 _(msgstr),
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002962 did_undo ? _("before") : _("after"),
2963 uhp == NULL ? 0L : uhp->uh_seq,
2964 msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965}
2966
2967/*
2968 * u_sync: stop adding to the current entry list
2969 */
2970 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002971u_sync(
2972 int force) /* Also sync when no_u_sync is set. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973{
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002974 /* Skip it when already synced or syncing is disabled. */
2975 if (curbuf->b_u_synced || (!force && no_u_sync > 0))
2976 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2978 if (im_is_preediting())
2979 return; /* XIM is busy, don't break an undo sequence */
2980#endif
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002981 if (get_undolevel() < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 curbuf->b_u_synced = TRUE; /* no entries, nothing to do */
2983 else
2984 {
2985 u_getbot(); /* compute ue_bot of previous u_save */
2986 curbuf->b_u_curhead = NULL;
2987 }
2988}
2989
2990/*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002991 * ":undolist": List the leafs of the undo tree
2992 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002993 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002994ex_undolist(exarg_T *eap UNUSED)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002995{
2996 garray_T ga;
2997 u_header_T *uhp;
2998 int mark;
2999 int nomark;
3000 int changes = 1;
3001 int i;
3002
3003 /*
3004 * 1: walk the tree to find all leafs, put the info in "ga".
3005 * 2: sort the lines
3006 * 3: display the list
3007 */
3008 mark = ++lastmark;
3009 nomark = ++lastmark;
3010 ga_init2(&ga, (int)sizeof(char *), 20);
3011
3012 uhp = curbuf->b_u_oldhead;
3013 while (uhp != NULL)
3014 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003015 if (uhp->uh_prev.ptr == NULL && uhp->uh_walk != nomark
Bram Moolenaarca003e12006-03-17 23:19:38 +00003016 && uhp->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003017 {
3018 if (ga_grow(&ga, 1) == FAIL)
3019 break;
3020 vim_snprintf((char *)IObuff, IOSIZE, "%6ld %7ld ",
3021 uhp->uh_seq, changes);
3022 u_add_time(IObuff + STRLEN(IObuff), IOSIZE - STRLEN(IObuff),
3023 uhp->uh_time);
Bram Moolenaara800b422010-06-27 01:15:55 +02003024 if (uhp->uh_save_nr > 0)
3025 {
Bram Moolenaardba01a02010-11-03 19:32:42 +01003026 while (STRLEN(IObuff) < 33)
Bram Moolenaara800b422010-06-27 01:15:55 +02003027 STRCAT(IObuff, " ");
3028 vim_snprintf_add((char *)IObuff, IOSIZE,
3029 " %3ld", uhp->uh_save_nr);
3030 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003031 ((char_u **)(ga.ga_data))[ga.ga_len++] = vim_strsave(IObuff);
3032 }
3033
3034 uhp->uh_walk = mark;
3035
3036 /* go down in the tree if we haven't been there */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003037 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != nomark
3038 && uhp->uh_prev.ptr->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003039 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003040 uhp = uhp->uh_prev.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003041 ++changes;
3042 }
3043
3044 /* go to alternate branch if we haven't been there */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003045 else if (uhp->uh_alt_next.ptr != NULL
3046 && uhp->uh_alt_next.ptr->uh_walk != nomark
3047 && uhp->uh_alt_next.ptr->uh_walk != mark)
3048 uhp = uhp->uh_alt_next.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003049
3050 /* go up in the tree if we haven't been there and we are at the
3051 * start of alternate branches */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003052 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
3053 && uhp->uh_next.ptr->uh_walk != nomark
3054 && uhp->uh_next.ptr->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003055 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003056 uhp = uhp->uh_next.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003057 --changes;
3058 }
3059
3060 else
3061 {
3062 /* need to backtrack; mark this node as done */
3063 uhp->uh_walk = nomark;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003064 if (uhp->uh_alt_prev.ptr != NULL)
3065 uhp = uhp->uh_alt_prev.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003066 else
3067 {
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003068 uhp = uhp->uh_next.ptr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003069 --changes;
3070 }
3071 }
3072 }
3073
3074 if (ga.ga_len == 0)
3075 MSG(_("Nothing to undo"));
3076 else
3077 {
3078 sort_strings((char_u **)ga.ga_data, ga.ga_len);
3079
3080 msg_start();
Bram Moolenaardba01a02010-11-03 19:32:42 +01003081 msg_puts_attr((char_u *)_("number changes when saved"),
Bram Moolenaara800b422010-06-27 01:15:55 +02003082 hl_attr(HLF_T));
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003083 for (i = 0; i < ga.ga_len && !got_int; ++i)
3084 {
3085 msg_putchar('\n');
3086 if (got_int)
3087 break;
3088 msg_puts(((char_u **)ga.ga_data)[i]);
3089 }
3090 msg_end();
3091
3092 ga_clear_strings(&ga);
3093 }
3094}
3095
3096/*
3097 * Put the timestamp of an undo header in "buf[buflen]" in a nice format.
3098 */
3099 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003100u_add_time(char_u *buf, size_t buflen, time_t tt)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003101{
3102#ifdef HAVE_STRFTIME
3103 struct tm *curtime;
3104
Bram Moolenaar170b10b2016-07-29 16:15:27 +02003105 if (vim_time() - tt >= 100)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003106 {
3107 curtime = localtime(&tt);
Bram Moolenaar170b10b2016-07-29 16:15:27 +02003108 if (vim_time() - tt < (60L * 60L * 12L))
Bram Moolenaardba01a02010-11-03 19:32:42 +01003109 /* within 12 hours */
3110 (void)strftime((char *)buf, buflen, "%H:%M:%S", curtime);
Bram Moolenaardba01a02010-11-03 19:32:42 +01003111 else
Bram Moolenaarb9ce83e2012-08-23 12:59:02 +02003112 /* longer ago */
Bram Moolenaar5e3d6ca2011-01-22 21:25:11 +01003113 (void)strftime((char *)buf, buflen, "%Y/%m/%d %H:%M:%S", curtime);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003114 }
3115 else
3116#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003117 vim_snprintf((char *)buf, buflen, _("%ld seconds ago"),
Bram Moolenaar170b10b2016-07-29 16:15:27 +02003118 (long)(vim_time() - tt));
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003119}
3120
3121/*
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003122 * ":undojoin": continue adding to the last entry list
3123 */
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003124 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003125ex_undojoin(exarg_T *eap UNUSED)
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003126{
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003127 if (curbuf->b_u_newhead == NULL)
3128 return; /* nothing changed before */
Bram Moolenaar57657d82006-04-21 22:12:41 +00003129 if (curbuf->b_u_curhead != NULL)
3130 {
3131 EMSG(_("E790: undojoin is not allowed after undo"));
3132 return;
3133 }
3134 if (!curbuf->b_u_synced)
3135 return; /* already unsynced */
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01003136 if (get_undolevel() < 0)
Bram Moolenaare224ffa2006-03-01 00:01:28 +00003137 return; /* no entries, nothing to do */
3138 else
3139 {
3140 /* Go back to the last entry */
3141 curbuf->b_u_curhead = curbuf->b_u_newhead;
3142 curbuf->b_u_synced = FALSE; /* no entries, nothing to do */
3143 }
3144}
3145
3146/*
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003147 * Called after writing or reloading the file and setting b_changed to FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 * Now an undo means that the buffer is modified.
3149 */
3150 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003151u_unchanged(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152{
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003153 u_unch_branch(buf->b_u_oldhead);
3154 buf->b_did_warn = FALSE;
3155}
3156
Bram Moolenaar730cde92010-06-27 05:18:54 +02003157/*
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003158 * After reloading a buffer which was saved for 'undoreload': Find the first
3159 * line that was changed and set the cursor there.
3160 */
3161 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003162u_find_first_changed(void)
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02003163{
3164 u_header_T *uhp = curbuf->b_u_newhead;
3165 u_entry_T *uep;
3166 linenr_T lnum;
3167
3168 if (curbuf->b_u_curhead != NULL || uhp == NULL)
3169 return; /* undid something in an autocmd? */
3170
3171 /* Check that the last undo block was for the whole file. */
3172 uep = uhp->uh_entry;
3173 if (uep->ue_top != 0 || uep->ue_bot != 0)
3174 return;
3175
3176 for (lnum = 1; lnum < curbuf->b_ml.ml_line_count
3177 && lnum <= uep->ue_size; ++lnum)
3178 if (STRCMP(ml_get_buf(curbuf, lnum, FALSE),
3179 uep->ue_array[lnum - 1]) != 0)
3180 {
3181 clearpos(&(uhp->uh_cursor));
3182 uhp->uh_cursor.lnum = lnum;
3183 return;
3184 }
3185 if (curbuf->b_ml.ml_line_count != uep->ue_size)
3186 {
3187 /* lines added or deleted at the end, put the cursor there */
3188 clearpos(&(uhp->uh_cursor));
3189 uhp->uh_cursor.lnum = lnum;
3190 }
3191}
3192
3193/*
Bram Moolenaar730cde92010-06-27 05:18:54 +02003194 * Increase the write count, store it in the last undo header, what would be
3195 * used for "u".
3196 */
3197 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003198u_update_save_nr(buf_T *buf)
Bram Moolenaar730cde92010-06-27 05:18:54 +02003199{
3200 u_header_T *uhp;
3201
3202 ++buf->b_u_save_nr_last;
3203 buf->b_u_save_nr_cur = buf->b_u_save_nr_last;
3204 uhp = buf->b_u_curhead;
3205 if (uhp != NULL)
3206 uhp = uhp->uh_next.ptr;
3207 else
3208 uhp = buf->b_u_newhead;
3209 if (uhp != NULL)
3210 uhp->uh_save_nr = buf->b_u_save_nr_last;
3211}
3212
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003213 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003214u_unch_branch(u_header_T *uhp)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003215{
Bram Moolenaar1e607892006-03-13 22:15:53 +00003216 u_header_T *uh;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003218 for (uh = uhp; uh != NULL; uh = uh->uh_prev.ptr)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003219 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 uh->uh_flags |= UH_CHANGED;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003221 if (uh->uh_alt_next.ptr != NULL)
3222 u_unch_branch(uh->uh_alt_next.ptr); /* recursive */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224}
3225
3226/*
3227 * Get pointer to last added entry.
3228 * If it's not valid, give an error message and return NULL.
3229 */
3230 static u_entry_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003231u_get_headentry(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232{
3233 if (curbuf->b_u_newhead == NULL || curbuf->b_u_newhead->uh_entry == NULL)
3234 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003235 IEMSG(_("E439: undo list corrupt"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 return NULL;
3237 }
3238 return curbuf->b_u_newhead->uh_entry;
3239}
3240
3241/*
3242 * u_getbot(): compute the line number of the previous u_save
3243 * It is called only when b_u_synced is FALSE.
3244 */
3245 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003246u_getbot(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247{
3248 u_entry_T *uep;
3249 linenr_T extra;
3250
3251 uep = u_get_headentry(); /* check for corrupt undo list */
3252 if (uep == NULL)
3253 return;
3254
3255 uep = curbuf->b_u_newhead->uh_getbot_entry;
3256 if (uep != NULL)
3257 {
3258 /*
3259 * the new ue_bot is computed from the number of lines that has been
3260 * inserted (0 - deleted) since calling u_save. This is equal to the
3261 * old line count subtracted from the current line count.
3262 */
3263 extra = curbuf->b_ml.ml_line_count - uep->ue_lcount;
3264 uep->ue_bot = uep->ue_top + uep->ue_size + 1 + extra;
3265 if (uep->ue_bot < 1 || uep->ue_bot > curbuf->b_ml.ml_line_count)
3266 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003267 IEMSG(_("E440: undo line missing"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 uep->ue_bot = uep->ue_top + 1; /* assume all lines deleted, will
3269 * get all the old lines back
3270 * without deleting the current
3271 * ones */
3272 }
3273
3274 curbuf->b_u_newhead->uh_getbot_entry = NULL;
3275 }
3276
3277 curbuf->b_u_synced = TRUE;
3278}
3279
3280/*
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003281 * Free one header "uhp" and its entry list and adjust the pointers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 */
3283 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003284u_freeheader(
3285 buf_T *buf,
3286 u_header_T *uhp,
3287 u_header_T **uhpp) /* if not NULL reset when freeing this header */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288{
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003289 u_header_T *uhap;
3290
Bram Moolenaar1e607892006-03-13 22:15:53 +00003291 /* When there is an alternate redo list free that branch completely,
3292 * because we can never go there. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003293 if (uhp->uh_alt_next.ptr != NULL)
3294 u_freebranch(buf, uhp->uh_alt_next.ptr, uhpp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003296 if (uhp->uh_alt_prev.ptr != NULL)
3297 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298
Bram Moolenaar1e607892006-03-13 22:15:53 +00003299 /* Update the links in the list to remove the header. */
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003300 if (uhp->uh_next.ptr == NULL)
3301 buf->b_u_oldhead = uhp->uh_prev.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003303 uhp->uh_next.ptr->uh_prev.ptr = uhp->uh_prev.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003305 if (uhp->uh_prev.ptr == NULL)
3306 buf->b_u_newhead = uhp->uh_next.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 else
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003308 for (uhap = uhp->uh_prev.ptr; uhap != NULL;
3309 uhap = uhap->uh_alt_next.ptr)
3310 uhap->uh_next.ptr = uhp->uh_next.ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311
Bram Moolenaar1e607892006-03-13 22:15:53 +00003312 u_freeentries(buf, uhp, uhpp);
3313}
3314
3315/*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003316 * Free an alternate branch and any following alternate branches.
Bram Moolenaar1e607892006-03-13 22:15:53 +00003317 */
3318 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003319u_freebranch(
3320 buf_T *buf,
3321 u_header_T *uhp,
3322 u_header_T **uhpp) /* if not NULL reset when freeing this header */
Bram Moolenaar1e607892006-03-13 22:15:53 +00003323{
3324 u_header_T *tofree, *next;
3325
Bram Moolenaar07d06772007-11-10 21:51:15 +00003326 /* If this is the top branch we may need to use u_freeheader() to update
3327 * all the pointers. */
3328 if (uhp == buf->b_u_oldhead)
3329 {
Bram Moolenaaraa887322013-11-07 03:04:11 +01003330 while (buf->b_u_oldhead != NULL)
3331 u_freeheader(buf, buf->b_u_oldhead, uhpp);
Bram Moolenaar07d06772007-11-10 21:51:15 +00003332 return;
3333 }
3334
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003335 if (uhp->uh_alt_prev.ptr != NULL)
3336 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +00003337
3338 next = uhp;
3339 while (next != NULL)
3340 {
3341 tofree = next;
Bram Moolenaar83d09bb2010-06-01 19:58:08 +02003342 if (tofree->uh_alt_next.ptr != NULL)
3343 u_freebranch(buf, tofree->uh_alt_next.ptr, uhpp); /* recursive */
3344 next = tofree->uh_prev.ptr;
Bram Moolenaar1e607892006-03-13 22:15:53 +00003345 u_freeentries(buf, tofree, uhpp);
3346 }
3347}
3348
3349/*
3350 * Free all the undo entries for one header and the header itself.
3351 * This means that "uhp" is invalid when returning.
3352 */
3353 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003354u_freeentries(
3355 buf_T *buf,
3356 u_header_T *uhp,
3357 u_header_T **uhpp) /* if not NULL reset when freeing this header */
Bram Moolenaar1e607892006-03-13 22:15:53 +00003358{
3359 u_entry_T *uep, *nuep;
3360
3361 /* Check for pointers to the header that become invalid now. */
3362 if (buf->b_u_curhead == uhp)
3363 buf->b_u_curhead = NULL;
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003364 if (buf->b_u_newhead == uhp)
3365 buf->b_u_newhead = NULL; /* freeing the newest entry */
Bram Moolenaar1e607892006-03-13 22:15:53 +00003366 if (uhpp != NULL && uhp == *uhpp)
3367 *uhpp = NULL;
3368
3369 for (uep = uhp->uh_entry; uep != NULL; uep = nuep)
3370 {
3371 nuep = uep->ue_next;
3372 u_freeentry(uep, uep->ue_size);
3373 }
3374
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003375#ifdef U_DEBUG
3376 uhp->uh_magic = 0;
3377#endif
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003378 vim_free((char_u *)uhp);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003379 --buf->b_u_numhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380}
3381
3382/*
3383 * free entry 'uep' and 'n' lines in uep->ue_array[]
3384 */
3385 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003386u_freeentry(u_entry_T *uep, long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387{
Bram Moolenaar8d343302005-07-12 22:46:17 +00003388 while (n > 0)
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003389 vim_free(uep->ue_array[--n]);
3390 vim_free((char_u *)uep->ue_array);
Bram Moolenaarfecb6602007-10-01 20:54:15 +00003391#ifdef U_DEBUG
3392 uep->ue_magic = 0;
3393#endif
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003394 vim_free((char_u *)uep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395}
3396
3397/*
3398 * invalidate the undo buffer; called when storage has already been released
3399 */
3400 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003401u_clearall(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402{
3403 buf->b_u_newhead = buf->b_u_oldhead = buf->b_u_curhead = NULL;
3404 buf->b_u_synced = TRUE;
3405 buf->b_u_numhead = 0;
3406 buf->b_u_line_ptr = NULL;
3407 buf->b_u_line_lnum = 0;
3408}
3409
3410/*
3411 * save the line "lnum" for the "U" command
3412 */
3413 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003414u_saveline(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415{
3416 if (lnum == curbuf->b_u_line_lnum) /* line is already saved */
3417 return;
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00003418 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) /* should never happen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 return;
3420 u_clearline();
3421 curbuf->b_u_line_lnum = lnum;
3422 if (curwin->w_cursor.lnum == lnum)
3423 curbuf->b_u_line_colnr = curwin->w_cursor.col;
3424 else
3425 curbuf->b_u_line_colnr = 0;
3426 if ((curbuf->b_u_line_ptr = u_save_line(lnum)) == NULL)
3427 do_outofmem_msg((long_u)0);
3428}
3429
3430/*
3431 * clear the line saved for the "U" command
3432 * (this is used externally for crossing a line while in insert mode)
3433 */
3434 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003435u_clearline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436{
3437 if (curbuf->b_u_line_ptr != NULL)
3438 {
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003439 vim_free(curbuf->b_u_line_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 curbuf->b_u_line_ptr = NULL;
3441 curbuf->b_u_line_lnum = 0;
3442 }
3443}
3444
3445/*
3446 * Implementation of the "U" command.
3447 * Differentiation from vi: "U" can be undone with the next "U".
3448 * We also allow the cursor to be in another line.
Bram Moolenaard04b7502010-07-08 22:27:55 +02003449 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 */
3451 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003452u_undoline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453{
3454 colnr_T t;
3455 char_u *oldp;
3456
3457 if (undo_off)
3458 return;
3459
Bram Moolenaare3300c82008-02-13 14:21:38 +00003460 if (curbuf->b_u_line_ptr == NULL
3461 || curbuf->b_u_line_lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 {
3463 beep_flush();
3464 return;
3465 }
Bram Moolenaare3300c82008-02-13 14:21:38 +00003466
3467 /* first save the line for the 'u' command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 if (u_savecommon(curbuf->b_u_line_lnum - 1,
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003469 curbuf->b_u_line_lnum + 1, (linenr_T)0, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 return;
3471 oldp = u_save_line(curbuf->b_u_line_lnum);
3472 if (oldp == NULL)
3473 {
3474 do_outofmem_msg((long_u)0);
3475 return;
3476 }
3477 ml_replace(curbuf->b_u_line_lnum, curbuf->b_u_line_ptr, TRUE);
3478 changed_bytes(curbuf->b_u_line_lnum, 0);
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003479 vim_free(curbuf->b_u_line_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 curbuf->b_u_line_ptr = oldp;
3481
3482 t = curbuf->b_u_line_colnr;
3483 if (curwin->w_cursor.lnum == curbuf->b_u_line_lnum)
3484 curbuf->b_u_line_colnr = curwin->w_cursor.col;
3485 curwin->w_cursor.col = t;
3486 curwin->w_cursor.lnum = curbuf->b_u_line_lnum;
Bram Moolenaare3300c82008-02-13 14:21:38 +00003487 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488}
3489
3490/*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003491 * Free all allocated memory blocks for the buffer 'buf'.
3492 */
3493 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003494u_blockfree(buf_T *buf)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003495{
Bram Moolenaar1e607892006-03-13 22:15:53 +00003496 while (buf->b_u_oldhead != NULL)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00003497 u_freeheader(buf, buf->b_u_oldhead, NULL);
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003498 vim_free(buf->b_u_line_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499}
3500
3501/*
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003502 * u_save_line(): allocate memory and copy line 'lnum' into it.
3503 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 */
3505 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003506u_save_line(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507{
Bram Moolenaarf05e3b02010-05-29 15:40:47 +02003508 return vim_strsave(ml_get(lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509}
3510
3511/*
3512 * Check if the 'modified' flag is set, or 'ff' has changed (only need to
3513 * check the first character, because it can only be "dos", "unix" or "mac").
3514 * "nofile" and "scratch" type buffers are considered to always be unchanged.
3515 */
3516 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003517bufIsChanged(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518{
3519 return
3520#ifdef FEAT_QUICKFIX
3521 !bt_dontwrite(buf) &&
3522#endif
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003523 (buf->b_changed || file_ff_differs(buf, TRUE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524}
3525
3526 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003527curbufIsChanged(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528{
3529 return
3530#ifdef FEAT_QUICKFIX
3531 !bt_dontwrite(curbuf) &&
3532#endif
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003533 (curbuf->b_changed || file_ff_differs(curbuf, TRUE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534}
Bram Moolenaara800b422010-06-27 01:15:55 +02003535
3536#if defined(FEAT_EVAL) || defined(PROTO)
3537/*
3538 * For undotree(): Append the list of undo blocks at "first_uhp" to "list".
3539 * Recursive.
3540 */
3541 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003542u_eval_tree(u_header_T *first_uhp, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02003543{
3544 u_header_T *uhp = first_uhp;
3545 dict_T *dict;
3546
3547 while (uhp != NULL)
3548 {
3549 dict = dict_alloc();
3550 if (dict == NULL)
3551 return;
3552 dict_add_nr_str(dict, "seq", uhp->uh_seq, NULL);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02003553 dict_add_nr_str(dict, "time", (long)uhp->uh_time, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +02003554 if (uhp == curbuf->b_u_newhead)
3555 dict_add_nr_str(dict, "newhead", 1, NULL);
3556 if (uhp == curbuf->b_u_curhead)
3557 dict_add_nr_str(dict, "curhead", 1, NULL);
3558 if (uhp->uh_save_nr > 0)
3559 dict_add_nr_str(dict, "save", uhp->uh_save_nr, NULL);
3560
3561 if (uhp->uh_alt_next.ptr != NULL)
3562 {
3563 list_T *alt_list = list_alloc();
3564
3565 if (alt_list != NULL)
3566 {
3567 /* Recursive call to add alternate undo tree. */
3568 u_eval_tree(uhp->uh_alt_next.ptr, alt_list);
3569 dict_add_list(dict, "alt", alt_list);
3570 }
3571 }
3572
3573 list_append_dict(list, dict);
3574 uhp = uhp->uh_prev.ptr;
3575 }
3576}
3577#endif