blob: b4945d12e122c01f448d9cf17509c5413eeca774 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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 Moolenaar26a60b42005-02-22 08:49:11 +000075 * All data is allocated with U_ALLOC_LINE(), it will be freed as soon as the
76 * buffer is unloaded.
Bram Moolenaar071d4272004-06-13 20:20:40 +000077 */
78
Bram Moolenaarfecb6602007-10-01 20:54:15 +000079/* Uncomment the next line for including the u_check() function. This warns
80 * for errors in the debug information. */
81/* #define U_DEBUG 1 */
82#define UH_MAGIC 0x18dade /* value for uh_magic when in use */
83#define UE_MAGIC 0xabc123 /* value for ue_magic when in use */
84
Bram Moolenaar071d4272004-06-13 20:20:40 +000085#include "vim.h"
86
Bram Moolenaar26a60b42005-02-22 08:49:11 +000087/* See below: use malloc()/free() for memory management. */
88#define U_USE_MALLOC 1
89
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +000090static void u_unch_branch __ARGS((u_header_T *uhp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000091static u_entry_T *u_get_headentry __ARGS((void));
92static void u_getbot __ARGS((void));
93static int u_savecommon __ARGS((linenr_T, linenr_T, linenr_T));
94static void u_doit __ARGS((int count));
Bram Moolenaarca003e12006-03-17 23:19:38 +000095static void u_undoredo __ARGS((int undo));
Bram Moolenaardb552d602006-03-23 22:59:57 +000096static void u_undo_end __ARGS((int did_undo, int absolute));
Bram Moolenaarefd2bf12006-03-16 21:41:35 +000097static void u_add_time __ARGS((char_u *buf, size_t buflen, time_t tt));
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +000098static void u_freeheader __ARGS((buf_T *buf, u_header_T *uhp, u_header_T **uhpp));
Bram Moolenaar1e607892006-03-13 22:15:53 +000099static void u_freebranch __ARGS((buf_T *buf, u_header_T *uhp, u_header_T **uhpp));
100static void u_freeentries __ARGS((buf_T *buf, u_header_T *uhp, u_header_T **uhpp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101static void u_freeentry __ARGS((u_entry_T *, long));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200102#ifdef FEAT_PERSISTENT_UNDO
103static void unserialize_pos __ARGS((pos_T *pos, FILE *fp));
104static void unserialize_visualinfo __ARGS((visualinfo_T *info, FILE *fp));
105static char_u *u_get_undo_file_name __ARGS((char_u *, int reading));
106static int serialize_uep __ARGS((u_entry_T *uep, FILE *fp));
107static void serialize_pos __ARGS((pos_T pos, FILE *fp));
108static void serialize_visualinfo __ARGS((visualinfo_T info, FILE *fp));
109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000111#ifdef U_USE_MALLOC
112# define U_FREE_LINE(ptr) vim_free(ptr)
113# define U_ALLOC_LINE(size) lalloc((long_u)((size) + 1), FALSE)
114#else
115static void u_free_line __ARGS((char_u *ptr, int keep));
116static char_u *u_alloc_line __ARGS((unsigned size));
117# define U_FREE_LINE(ptr) u_free_line((ptr), FALSE)
118# define U_ALLOC_LINE(size) u_alloc_line(size)
119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120static char_u *u_save_line __ARGS((linenr_T));
121
122static long u_newcount, u_oldcount;
123
124/*
125 * When 'u' flag included in 'cpoptions', we behave like vi. Need to remember
126 * the action that "u" should do.
127 */
128static int undo_undoes = FALSE;
129
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200130static int lastmark = 0;
131
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000132#ifdef U_DEBUG
133/*
134 * Check the undo structures for being valid. Print a warning when something
135 * looks wrong.
136 */
137static int seen_b_u_curhead;
138static int seen_b_u_newhead;
139static int header_count;
140
141 static void
142u_check_tree(u_header_T *uhp,
143 u_header_T *exp_uh_next,
144 u_header_T *exp_uh_alt_prev)
145{
146 u_entry_T *uep;
147
148 if (uhp == NULL)
149 return;
150 ++header_count;
151 if (uhp == curbuf->b_u_curhead && ++seen_b_u_curhead > 1)
152 {
153 EMSG("b_u_curhead found twice (looping?)");
154 return;
155 }
156 if (uhp == curbuf->b_u_newhead && ++seen_b_u_newhead > 1)
157 {
158 EMSG("b_u_newhead found twice (looping?)");
159 return;
160 }
161
162 if (uhp->uh_magic != UH_MAGIC)
163 EMSG("uh_magic wrong (may be using freed memory)");
164 else
165 {
166 /* Check pointers back are correct. */
167 if (uhp->uh_next != exp_uh_next)
168 {
169 EMSG("uh_next wrong");
170 smsg((char_u *)"expected: 0x%x, actual: 0x%x",
171 exp_uh_next, uhp->uh_next);
172 }
173 if (uhp->uh_alt_prev != exp_uh_alt_prev)
174 {
175 EMSG("uh_alt_prev wrong");
176 smsg((char_u *)"expected: 0x%x, actual: 0x%x",
177 exp_uh_alt_prev, uhp->uh_alt_prev);
178 }
179
180 /* Check the undo tree at this header. */
181 for (uep = uhp->uh_entry; uep != NULL; uep = uep->ue_next)
182 {
183 if (uep->ue_magic != UE_MAGIC)
184 {
185 EMSG("ue_magic wrong (may be using freed memory)");
186 break;
187 }
188 }
189
190 /* Check the next alt tree. */
191 u_check_tree(uhp->uh_alt_next, uhp->uh_next, uhp);
192
193 /* Check the next header in this branch. */
194 u_check_tree(uhp->uh_prev, uhp, NULL);
195 }
196}
197
198 void
199u_check(int newhead_may_be_NULL)
200{
201 seen_b_u_newhead = 0;
202 seen_b_u_curhead = 0;
203 header_count = 0;
204
205 u_check_tree(curbuf->b_u_oldhead, NULL, NULL);
206
207 if (seen_b_u_newhead == 0 && curbuf->b_u_oldhead != NULL
208 && !(newhead_may_be_NULL && curbuf->b_u_newhead == NULL))
209 EMSGN("b_u_newhead invalid: 0x%x", curbuf->b_u_newhead);
210 if (curbuf->b_u_curhead != NULL && seen_b_u_curhead == 0)
211 EMSGN("b_u_curhead invalid: 0x%x", curbuf->b_u_curhead);
212 if (header_count != curbuf->b_u_numhead)
213 {
214 EMSG("b_u_numhead invalid");
215 smsg((char_u *)"expected: %ld, actual: %ld",
216 (long)header_count, (long)curbuf->b_u_numhead);
217 }
218}
219#endif
220
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000222 * Save the current line for both the "u" and "U" command.
223 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 */
225 int
226u_save_cursor()
227{
228 return (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
229 (linenr_T)(curwin->w_cursor.lnum + 1)));
230}
231
232/*
233 * Save the lines between "top" and "bot" for both the "u" and "U" command.
234 * "top" may be 0 and bot may be curbuf->b_ml.ml_line_count + 1.
235 * Returns FAIL when lines could not be saved, OK otherwise.
236 */
237 int
238u_save(top, bot)
239 linenr_T top, bot;
240{
241 if (undo_off)
242 return OK;
243
244 if (top > curbuf->b_ml.ml_line_count ||
245 top >= bot || bot > curbuf->b_ml.ml_line_count + 1)
246 return FALSE; /* rely on caller to do error messages */
247
248 if (top + 2 == bot)
249 u_saveline((linenr_T)(top + 1));
250
251 return (u_savecommon(top, bot, (linenr_T)0));
252}
253
254/*
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200255 * Save the line "lnum" (used by ":s" and "~" command).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256 * The line is replaced, so the new bottom line is lnum + 1.
257 */
258 int
259u_savesub(lnum)
260 linenr_T lnum;
261{
262 if (undo_off)
263 return OK;
264
265 return (u_savecommon(lnum - 1, lnum + 1, lnum + 1));
266}
267
268/*
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200269 * A new line is inserted before line "lnum" (used by :s command).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 * The line is inserted, so the new bottom line is lnum + 1.
271 */
272 int
273u_inssub(lnum)
274 linenr_T lnum;
275{
276 if (undo_off)
277 return OK;
278
279 return (u_savecommon(lnum - 1, lnum, lnum + 1));
280}
281
282/*
Bram Moolenaarfff2bee2010-05-15 13:56:02 +0200283 * Save the lines "lnum" - "lnum" + nlines (used by delete command).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284 * The lines are deleted, so the new bottom line is lnum, unless the buffer
285 * becomes empty.
286 */
287 int
288u_savedel(lnum, nlines)
289 linenr_T lnum;
290 long nlines;
291{
292 if (undo_off)
293 return OK;
294
295 return (u_savecommon(lnum - 1, lnum + nlines,
296 nlines == curbuf->b_ml.ml_line_count ? 2 : lnum));
297}
298
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000299/*
300 * Return TRUE when undo is allowed. Otherwise give an error message and
301 * return FALSE.
302 */
Bram Moolenaarce6ef252006-07-12 19:49:41 +0000303 int
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000304undo_allowed()
305{
306 /* Don't allow changes when 'modifiable' is off. */
307 if (!curbuf->b_p_ma)
308 {
309 EMSG(_(e_modifiable));
310 return FALSE;
311 }
312
313#ifdef HAVE_SANDBOX
314 /* In the sandbox it's not allowed to change the text. */
315 if (sandbox != 0)
316 {
317 EMSG(_(e_sandbox));
318 return FALSE;
319 }
320#endif
321
322 /* Don't allow changes in the buffer while editing the cmdline. The
323 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000324 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000325 {
326 EMSG(_(e_secure));
327 return FALSE;
328 }
329
330 return TRUE;
331}
332
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 static int
334u_savecommon(top, bot, newbot)
335 linenr_T top, bot;
336 linenr_T newbot;
337{
Bram Moolenaar1e607892006-03-13 22:15:53 +0000338 linenr_T lnum;
339 long i;
340 u_header_T *uhp;
341 u_header_T *old_curhead;
342 u_entry_T *uep;
343 u_entry_T *prev_uep;
344 long size;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000346 /* When making changes is not allowed return FAIL. It's a crude way to
347 * make all change commands fail. */
348 if (!undo_allowed())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000351#ifdef U_DEBUG
352 u_check(FALSE);
353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354#ifdef FEAT_NETBEANS_INTG
355 /*
356 * Netbeans defines areas that cannot be modified. Bail out here when
357 * trying to change text in a guarded area.
358 */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200359 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 {
Bram Moolenaar009b2592004-10-24 19:18:58 +0000361 if (netbeans_is_guarded(top, bot))
362 {
363 EMSG(_(e_guarded));
364 return FAIL;
365 }
366 if (curbuf->b_p_ro)
367 {
368 EMSG(_(e_nbreadonly));
369 return FAIL;
370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 }
372#endif
373
374#ifdef FEAT_AUTOCMD
375 /*
376 * Saving text for undo means we are going to make a change. Give a
377 * warning for a read-only file before making the change, so that the
378 * FileChangedRO event can replace the buffer with a read-write version
379 * (e.g., obtained from a source control system).
380 */
381 change_warning(0);
382#endif
383
384 size = bot - top - 1;
385
386 /*
387 * if curbuf->b_u_synced == TRUE make a new header
388 */
389 if (curbuf->b_u_synced)
390 {
391#ifdef FEAT_JUMPLIST
392 /* Need to create new entry in b_changelist. */
393 curbuf->b_new_change = TRUE;
394#endif
395
Bram Moolenaar1e607892006-03-13 22:15:53 +0000396 if (p_ul >= 0)
397 {
398 /*
399 * Make a new header entry. Do this first so that we don't mess
400 * up the undo info when out of memory.
401 */
402 uhp = (u_header_T *)U_ALLOC_LINE((unsigned)sizeof(u_header_T));
403 if (uhp == NULL)
404 goto nomem;
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000405#ifdef U_DEBUG
406 uhp->uh_magic = UH_MAGIC;
407#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +0000408 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000409 else
410 uhp = NULL;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000411
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 /*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000413 * If we undid more than we redid, move the entry lists before and
414 * including curbuf->b_u_curhead to an alternate branch.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415 */
Bram Moolenaar1e607892006-03-13 22:15:53 +0000416 old_curhead = curbuf->b_u_curhead;
417 if (old_curhead != NULL)
418 {
419 curbuf->b_u_newhead = old_curhead->uh_next;
420 curbuf->b_u_curhead = NULL;
421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422
423 /*
424 * free headers to keep the size right
425 */
426 while (curbuf->b_u_numhead > p_ul && curbuf->b_u_oldhead != NULL)
Bram Moolenaar1e607892006-03-13 22:15:53 +0000427 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000428 u_header_T *uhfree = curbuf->b_u_oldhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000429
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000430 if (uhfree == old_curhead)
431 /* Can't reconnect the branch, delete all of it. */
432 u_freebranch(curbuf, uhfree, &old_curhead);
433 else if (uhfree->uh_alt_next == NULL)
434 /* There is no branch, only free one header. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000435 u_freeheader(curbuf, uhfree, &old_curhead);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000436 else
437 {
438 /* Free the oldest alternate branch as a whole. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000439 while (uhfree->uh_alt_next != NULL)
440 uhfree = uhfree->uh_alt_next;
441 u_freebranch(curbuf, uhfree, &old_curhead);
Bram Moolenaar1e607892006-03-13 22:15:53 +0000442 }
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000443#ifdef U_DEBUG
444 u_check(TRUE);
445#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +0000446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +0000448 if (uhp == NULL) /* no undo at all */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 {
Bram Moolenaar1e607892006-03-13 22:15:53 +0000450 if (old_curhead != NULL)
451 u_freebranch(curbuf, old_curhead, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 curbuf->b_u_synced = FALSE;
453 return OK;
454 }
455
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 uhp->uh_prev = NULL;
457 uhp->uh_next = curbuf->b_u_newhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000458 uhp->uh_alt_next = old_curhead;
459 if (old_curhead != NULL)
460 {
Bram Moolenaar89ed3df2007-01-09 19:23:12 +0000461 uhp->uh_alt_prev = old_curhead->uh_alt_prev;
462 if (uhp->uh_alt_prev != NULL)
463 uhp->uh_alt_prev->uh_alt_next = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +0000464 old_curhead->uh_alt_prev = uhp;
465 if (curbuf->b_u_oldhead == old_curhead)
466 curbuf->b_u_oldhead = uhp;
467 }
Bram Moolenaar89ed3df2007-01-09 19:23:12 +0000468 else
469 uhp->uh_alt_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 if (curbuf->b_u_newhead != NULL)
471 curbuf->b_u_newhead->uh_prev = uhp;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000472
Bram Moolenaarca003e12006-03-17 23:19:38 +0000473 uhp->uh_seq = ++curbuf->b_u_seq_last;
474 curbuf->b_u_seq_cur = uhp->uh_seq;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000475 uhp->uh_time = time(NULL);
476 curbuf->b_u_seq_time = uhp->uh_time + 1;
477
Bram Moolenaar1e607892006-03-13 22:15:53 +0000478 uhp->uh_walk = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 uhp->uh_entry = NULL;
480 uhp->uh_getbot_entry = NULL;
481 uhp->uh_cursor = curwin->w_cursor; /* save cursor pos. for undo */
482#ifdef FEAT_VIRTUALEDIT
483 if (virtual_active() && curwin->w_cursor.coladd > 0)
484 uhp->uh_cursor_vcol = getviscol();
485 else
486 uhp->uh_cursor_vcol = -1;
487#endif
488
489 /* save changed and buffer empty flag for undo */
490 uhp->uh_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
491 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
492
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000493 /* save named marks and Visual marks for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 mch_memmove(uhp->uh_namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000495#ifdef FEAT_VISUAL
496 uhp->uh_visual = curbuf->b_visual;
497#endif
498
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 curbuf->b_u_newhead = uhp;
500 if (curbuf->b_u_oldhead == NULL)
501 curbuf->b_u_oldhead = uhp;
502 ++curbuf->b_u_numhead;
503 }
504 else
505 {
506 if (p_ul < 0) /* no undo at all */
507 return OK;
508
509 /*
510 * When saving a single line, and it has been saved just before, it
511 * doesn't make sense saving it again. Saves a lot of memory when
512 * making lots of changes inside the same line.
513 * This is only possible if the previous change didn't increase or
514 * decrease the number of lines.
515 * Check the ten last changes. More doesn't make sense and takes too
516 * long.
517 */
518 if (size == 1)
519 {
520 uep = u_get_headentry();
521 prev_uep = NULL;
522 for (i = 0; i < 10; ++i)
523 {
524 if (uep == NULL)
525 break;
526
527 /* If lines have been inserted/deleted we give up.
528 * Also when the line was included in a multi-line save. */
529 if ((curbuf->b_u_newhead->uh_getbot_entry != uep
530 ? (uep->ue_top + uep->ue_size + 1
531 != (uep->ue_bot == 0
532 ? curbuf->b_ml.ml_line_count + 1
533 : uep->ue_bot))
534 : uep->ue_lcount != curbuf->b_ml.ml_line_count)
535 || (uep->ue_size > 1
536 && top >= uep->ue_top
537 && top + 2 <= uep->ue_top + uep->ue_size + 1))
538 break;
539
540 /* If it's the same line we can skip saving it again. */
541 if (uep->ue_size == 1 && uep->ue_top == top)
542 {
543 if (i > 0)
544 {
545 /* It's not the last entry: get ue_bot for the last
546 * entry now. Following deleted/inserted lines go to
547 * the re-used entry. */
548 u_getbot();
549 curbuf->b_u_synced = FALSE;
550
551 /* Move the found entry to become the last entry. The
552 * order of undo/redo doesn't matter for the entries
553 * we move it over, since they don't change the line
554 * count and don't include this line. It does matter
555 * for the found entry if the line count is changed by
556 * the executed command. */
557 prev_uep->ue_next = uep->ue_next;
558 uep->ue_next = curbuf->b_u_newhead->uh_entry;
559 curbuf->b_u_newhead->uh_entry = uep;
560 }
561
562 /* The executed command may change the line count. */
563 if (newbot != 0)
564 uep->ue_bot = newbot;
565 else if (bot > curbuf->b_ml.ml_line_count)
566 uep->ue_bot = 0;
567 else
568 {
569 uep->ue_lcount = curbuf->b_ml.ml_line_count;
570 curbuf->b_u_newhead->uh_getbot_entry = uep;
571 }
572 return OK;
573 }
574 prev_uep = uep;
575 uep = uep->ue_next;
576 }
577 }
578
579 /* find line number for ue_bot for previous u_save() */
580 u_getbot();
581 }
582
583#if !defined(UNIX) && !defined(DJGPP) && !defined(WIN32) && !defined(__EMX__)
584 /*
585 * With Amiga and MSDOS 16 bit we can't handle big undo's, because
586 * then u_alloc_line would have to allocate a block larger than 32K
587 */
588 if (size >= 8000)
589 goto nomem;
590#endif
591
592 /*
593 * add lines in front of entry list
594 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000595 uep = (u_entry_T *)U_ALLOC_LINE((unsigned)sizeof(u_entry_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 if (uep == NULL)
597 goto nomem;
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000598#ifdef U_DEBUG
599 uep->ue_magic = UE_MAGIC;
600#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601
602 uep->ue_size = size;
603 uep->ue_top = top;
604 if (newbot != 0)
605 uep->ue_bot = newbot;
606 /*
607 * Use 0 for ue_bot if bot is below last line.
608 * Otherwise we have to compute ue_bot later.
609 */
610 else if (bot > curbuf->b_ml.ml_line_count)
611 uep->ue_bot = 0;
612 else
613 {
614 uep->ue_lcount = curbuf->b_ml.ml_line_count;
615 curbuf->b_u_newhead->uh_getbot_entry = uep;
616 }
617
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000618 if (size > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000620 if ((uep->ue_array = (char_u **)U_ALLOC_LINE(
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 (unsigned)(sizeof(char_u *) * size))) == NULL)
622 {
623 u_freeentry(uep, 0L);
624 goto nomem;
625 }
626 for (i = 0, lnum = top + 1; i < size; ++i)
627 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000628 fast_breakcheck();
629 if (got_int)
630 {
631 u_freeentry(uep, i);
632 return FAIL;
633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 if ((uep->ue_array[i] = u_save_line(lnum++)) == NULL)
635 {
636 u_freeentry(uep, i);
637 goto nomem;
638 }
639 }
640 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000641 else
642 uep->ue_array = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 uep->ue_next = curbuf->b_u_newhead->uh_entry;
644 curbuf->b_u_newhead->uh_entry = uep;
645 curbuf->b_u_synced = FALSE;
646 undo_undoes = FALSE;
647
Bram Moolenaarfecb6602007-10-01 20:54:15 +0000648#ifdef U_DEBUG
649 u_check(FALSE);
650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 return OK;
652
653nomem:
654 msg_silent = 0; /* must display the prompt */
655 if (ask_yesno((char_u *)_("No undo possible; continue anyway"), TRUE)
656 == 'y')
657 {
658 undo_off = TRUE; /* will be reset when character typed */
659 return OK;
660 }
661 do_outofmem_msg((long_u)0);
662 return FAIL;
663}
664
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200665#ifdef FEAT_PERSISTENT_UNDO
666
667# define UF_START_MAGIC 0xfeac /* magic at start of undofile */
668# define UF_HEADER_MAGIC 0x5fd0 /* magic at start of header */
669# define UF_END_MAGIC 0xe7aa /* magic after last header */
670# define UF_VERSION 1 /* 2-byte undofile version number */
671
672/*
673 * Compute the hash for the current buffer text into hash[UNDO_HASH_SIZE].
674 */
675 void
676u_compute_hash(hash)
677 char_u *hash;
678{
679 context_sha256_T ctx;
680 linenr_T lnum;
681 char_u *p;
682
683 sha256_start(&ctx);
684 for (lnum = 1; lnum < curbuf->b_ml.ml_line_count; ++lnum)
685 {
686 p = ml_get(lnum);
687 sha256_update(&ctx, p, STRLEN(p) + 1);
688 }
689 sha256_finish(&ctx, hash);
690}
691
692/*
693 * Unserialize the pos_T at the current position in fp.
694 */
695 static void
696unserialize_pos(pos, fp)
697 pos_T *pos;
698 FILE *fp;
699{
700 pos->lnum = get4c(fp);
701 pos->col = get4c(fp);
702#ifdef FEAT_VIRTUALEDIT
703 pos->coladd = get4c(fp);
704#else
705 (void)get4c(fp);
706#endif
707}
708
709/*
710 * Unserialize the visualinfo_T at the current position in fp.
711 */
712 static void
713unserialize_visualinfo(info, fp)
714 visualinfo_T *info;
715 FILE *fp;
716{
717 unserialize_pos(&info->vi_start, fp);
718 unserialize_pos(&info->vi_end, fp);
719 info->vi_mode = get4c(fp);
720 info->vi_curswant = get4c(fp);
721}
722
723/*
724 * Return an allocated string of the full path of the target undofile.
725 * When "reading" is TRUE find the file to read, go over all directories in
726 * 'undodir'.
727 * When "reading" is FALSE use the first name where the directory exists.
728 */
729 static char_u *
730u_get_undo_file_name(buf_ffname, reading)
731 char_u *buf_ffname;
732 int reading;
733{
734 char_u *dirp;
735 char_u dir_name[IOSIZE + 1];
736 char_u *munged_name = NULL;
737 char_u *undo_file_name = NULL;
738 int dir_len;
739 char_u *p;
740 struct stat st;
741 char_u *ffname = buf_ffname;
742#ifdef HAVE_READLINK
743 char_u fname_buf[MAXPATHL];
744#endif
745
746 if (ffname == NULL)
747 return NULL;
748
749#ifdef HAVE_READLINK
750 /* Expand symlink in the file name, so that we put the undo file with the
751 * actual file instead of with the symlink. */
752 if (resolve_symlink(ffname, fname_buf) == OK)
753 ffname = fname_buf;
754#endif
755
756 /* Loop over 'undodir'. When reading find the first file that exists.
757 * When not reading use the first directory that exists or ".". */
758 dirp = p_udir;
759 while (*dirp != NUL)
760 {
761 dir_len = copy_option_part(&dirp, dir_name, IOSIZE, ",");
762 if (dir_len == 1 && dir_name[0] == '.')
763 {
764 /* Use same directory as the ffname,
765 * "dir/name" -> "dir/.name.un~" */
766 undo_file_name = vim_strnsave(ffname, STRLEN(ffname) + 5);
767 if (undo_file_name == NULL)
768 break;
769 p = gettail(undo_file_name);
770 mch_memmove(p + 1, p, STRLEN(p) + 1);
771 *p = '.';
772 STRCAT(p, ".un~");
773 }
774 else
775 {
776 dir_name[dir_len] = NUL;
777 if (mch_isdir(dir_name))
778 {
779 if (munged_name == NULL)
780 {
781 munged_name = vim_strsave(ffname);
782 if (munged_name == NULL)
783 return NULL;
784 for (p = munged_name; *p != NUL; mb_ptr_adv(p))
785 if (vim_ispathsep(*p))
786 *p = '%';
787 }
788 undo_file_name = concat_fnames(dir_name, munged_name, TRUE);
789 }
790 }
791
792 /* When reading check if the file exists. */
793 if (undo_file_name != NULL && (!reading
794 || mch_stat((char *)undo_file_name, &st) >= 0))
795 break;
796 vim_free(undo_file_name);
797 undo_file_name = NULL;
798 }
799
800 vim_free(munged_name);
801 return undo_file_name;
802}
803
804/*
805 * Load the undo tree from an undo file.
806 * If "name" is not NULL use it as the undo file name. This also means being
807 * a bit more verbose.
808 * Otherwise use curbuf->b_ffname to generate the undo file name.
809 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text.
810 */
811 void
812u_read_undo(name, hash)
813 char_u *name;
814 char_u *hash;
815{
816 char_u *file_name;
817 FILE *fp;
818 long magic, version, str_len;
819 char_u *line_ptr = NULL;
820 linenr_T line_lnum;
821 colnr_T line_colnr;
822 linenr_T line_count;
823 int uep_len;
824 int line_len;
825 int num_head;
826 long old_header_seq, new_header_seq, cur_header_seq;
827 long seq_last, seq_cur;
828 short old_idx = -1, new_idx = -1, cur_idx = -1;
829 long num_read_uhps = 0;
830 time_t seq_time;
831 int i, j;
832 int c;
833 short found_first_uep = 0;
834 char_u **array;
835 char_u *line;
836 u_entry_T *uep, *last_uep, *nuep;
837 u_header_T *uhp;
838 u_header_T **uhp_table = NULL;
839 char_u read_hash[UNDO_HASH_SIZE];
840
841 if (name == NULL)
842 {
843 file_name = u_get_undo_file_name(curbuf->b_ffname, TRUE);
844 if (file_name == NULL)
845 return;
846 }
847 else
848 file_name = name;
849
850 if (p_verbose > 0)
851 smsg((char_u *)_("Reading undo file: %s"), file_name);
852 fp = mch_fopen((char *)file_name, "r");
853 if (fp == NULL)
854 {
855 if (name != NULL || p_verbose > 0)
856 EMSG2(_("E822: Cannot open undo file for reading: %s"), file_name);
857 goto error;
858 }
859
860 /* Begin overall file information */
861 magic = get2c(fp);
862 if (magic != UF_START_MAGIC)
863 {
864 EMSG2(_("E823: Corrupted undo file: %s"), file_name);
865 goto error;
866 }
867 version = get2c(fp);
868 if (version != UF_VERSION)
869 {
870 EMSG2(_("E824: Incompatible undo file: %s"), file_name);
871 goto error;
872 }
873
874 fread(read_hash, UNDO_HASH_SIZE, 1, fp);
875 line_count = (linenr_T)get4c(fp);
876 if (memcmp(hash, read_hash, UNDO_HASH_SIZE) != 0
877 || line_count != curbuf->b_ml.ml_line_count)
878 {
879 if (p_verbose > 0 || name != NULL)
880 {
881 verbose_enter();
882 give_warning((char_u *)_("Undo file contents changed"), TRUE);
883 verbose_leave();
884 }
885 goto error;
886 }
887
888 /* Begin undo data for U */
889 str_len = get4c(fp);
890 if (str_len < 0)
891 goto error;
892 else if (str_len > 0)
893 {
894 if ((line_ptr = U_ALLOC_LINE(str_len)) == NULL)
895 goto error;
896 for (i = 0; i < str_len; i++)
897 line_ptr[i] = (char_u)getc(fp);
898 line_ptr[i] = NUL;
899 }
900 line_lnum = (linenr_T)get4c(fp);
901 line_colnr = (colnr_T)get4c(fp);
902
903 /* Begin general undo data */
904 old_header_seq = get4c(fp);
905 new_header_seq = get4c(fp);
906 cur_header_seq = get4c(fp);
907 num_head = get4c(fp);
908 seq_last = get4c(fp);
909 seq_cur = get4c(fp);
910 seq_time = get4c(fp);
911
912 /* uhp_table will store the freshly created undo headers we allocate
913 * until we insert them into curbuf. The table remains sorted by the
914 * sequence numbers of the headers. */
915 uhp_table = (u_header_T **)U_ALLOC_LINE(num_head * sizeof(u_header_T *));
916 if (uhp_table == NULL)
917 goto error;
918 vim_memset(uhp_table, 0, num_head * sizeof(u_header_T *));
919
920 c = get2c(fp);
921 while (c == UF_HEADER_MAGIC)
922 {
923 found_first_uep = 0;
924 uhp = (u_header_T *)U_ALLOC_LINE((unsigned)sizeof(u_header_T));
925 if (uhp == NULL)
926 goto error;
927 vim_memset(uhp, 0, sizeof(u_header_T));
928 /* We're not actually trying to store pointers here. We're just storing
929 * IDs so we can swizzle them into pointers later - hence the type
930 * cast. */
931 uhp->uh_next = (u_header_T *)(long)get4c(fp);
932 uhp->uh_prev = (u_header_T *)(long)get4c(fp);
933 uhp->uh_alt_next = (u_header_T *)(long)get4c(fp);
934 uhp->uh_alt_prev = (u_header_T *)(long)get4c(fp);
935 uhp->uh_seq = get4c(fp);
936 if (uhp->uh_seq <= 0)
937 {
938 EMSG2(_("E825: Undo file corruption: invalid uh_seq.: %s"),
939 file_name);
940 U_FREE_LINE(uhp);
941 goto error;
942 }
943 uhp->uh_walk = 0;
944 unserialize_pos(&uhp->uh_cursor, fp);
945#ifdef FEAT_VIRTUALEDIT
946 uhp->uh_cursor_vcol = get4c(fp);
947#else
948 (void)get4c(fp);
949#endif
950 uhp->uh_flags = get2c(fp);
951 for (i = 0; i < NMARKS; ++i)
952 unserialize_pos(&uhp->uh_namedm[i], fp);
953#ifdef FEAT_VISUAL
954 unserialize_visualinfo(&uhp->uh_visual, fp);
955#else
956 {
957 visualinfo_T info;
958 unserialize_visualinfo(&info, fp);
959 }
960#endif
961 uhp->uh_time = get4c(fp);
962
963 /* Unserialize uep list. The first 4 bytes is the length of the
964 * entire uep in bytes minus the length of the strings within.
965 * -1 is a sentinel value meaning no more ueps.*/
966 last_uep = NULL;
967 while ((uep_len = get4c(fp)) != -1)
968 {
969 uep = (u_entry_T *)U_ALLOC_LINE((unsigned)sizeof(u_entry_T));
970 vim_memset(uep, 0, sizeof(u_entry_T));
971 if (uep == NULL)
972 goto error;
973 uep->ue_top = get4c(fp);
974 uep->ue_bot = get4c(fp);
975 uep->ue_lcount = get4c(fp);
976 uep->ue_size = get4c(fp);
977 uep->ue_next = NULL;
978 array = (char_u **)U_ALLOC_LINE(
979 (unsigned)(sizeof(char_u *) * uep->ue_size));
980 for (i = 0; i < uep->ue_size; i++)
981 {
982 line_len = get4c(fp);
983 /* U_ALLOC_LINE provides an extra byte for the NUL terminator.*/
984 line = (char_u *)U_ALLOC_LINE(
985 (unsigned) (sizeof(char_u) * line_len));
986 if (line == NULL)
987 goto error;
988 for (j = 0; j < line_len; j++)
989 {
990 line[j] = getc(fp);
991 }
992 line[j] = '\0';
993 array[i] = line;
994 }
995 uep->ue_array = array;
996 if (found_first_uep == 0)
997 {
998 uhp->uh_entry = uep;
999 found_first_uep = 1;
1000 }
1001 else
1002 {
1003 last_uep->ue_next = uep;
1004 }
1005 last_uep = uep;
1006 }
1007
1008 /* Insertion sort the uhp into the table by its uh_seq. This is
1009 * required because, while the number of uhps is limited to
1010 * num_heads, and the uh_seq order is monotonic with respect to
1011 * creation time, the starting uh_seq can be > 0 if any undolevel
1012 * culling was done at undofile write time, and there can be uh_seq
1013 * gaps in the uhps.
1014 */
1015 for (i = num_read_uhps - 1; i >= -1; i--)
1016 {
1017 /* if i == -1, we've hit the leftmost side of the table, so insert
1018 * at uhp_table[0]. */
1019 if (i == -1 || uhp->uh_seq > uhp_table[i]->uh_seq)
1020 {
1021 /* If we've had to move from the rightmost side of the table,
1022 * we have to shift everything to the right by one spot. */
1023 if (i < num_read_uhps - 1)
1024 {
1025 memmove(uhp_table + i + 2, uhp_table + i + 1,
1026 (num_read_uhps - i) * sizeof(u_header_T *));
1027 }
1028 uhp_table[i + 1] = uhp;
1029 break;
1030 }
1031 else if (uhp->uh_seq == uhp_table[i]->uh_seq)
1032 {
1033 EMSG2(_("E826 Undo file corruption: duplicate uh_seq: %s"),
1034 file_name);
1035 goto error;
1036 }
1037 }
1038 num_read_uhps++;
1039 c = get2c(fp);
1040 }
1041
1042 if (c != UF_END_MAGIC)
1043 {
1044 EMSG2(_("E827: Undo file corruption; no end marker: %s"), file_name);
1045 goto error;
1046 }
1047
1048 /* We've organized all of the uhps into a table sorted by uh_seq. Now we
1049 * iterate through the table and swizzle each sequence number we've
1050 * stored in uh_foo into a pointer corresponding to the header with that
1051 * sequence number. Then free curbuf's old undo structure, give curbuf
1052 * the updated {old,new,cur}head pointers, and then free the table. */
1053 for (i = 0; i < num_head; i++)
1054 {
1055 uhp = uhp_table[i];
1056 if (uhp == NULL)
1057 continue;
1058 for (j = 0; j < num_head; j++)
1059 {
1060 if (uhp_table[j] == NULL)
1061 continue;
1062 if (uhp_table[j]->uh_seq == (long)uhp->uh_next)
1063 uhp->uh_next = uhp_table[j];
1064 if (uhp_table[j]->uh_seq == (long)uhp->uh_prev)
1065 uhp->uh_prev = uhp_table[j];
1066 if (uhp_table[j]->uh_seq == (long)uhp->uh_alt_next)
1067 uhp->uh_alt_next = uhp_table[j];
1068 if (uhp_table[j]->uh_seq == (long)uhp->uh_alt_prev)
1069 uhp->uh_alt_prev = uhp_table[j];
1070 }
1071 if (old_header_seq > 0 && old_idx < 0 && uhp->uh_seq == old_header_seq)
1072 old_idx = i;
1073 if (new_header_seq > 0 && new_idx < 0 && uhp->uh_seq == new_header_seq)
1074 new_idx = i;
1075 if (cur_header_seq > 0 && cur_idx < 0 && uhp->uh_seq == cur_header_seq)
1076 cur_idx = i;
1077 }
1078 u_blockfree(curbuf);
1079 curbuf->b_u_oldhead = old_idx < 0 ? 0 : uhp_table[old_idx];
1080 curbuf->b_u_newhead = new_idx < 0 ? 0 : uhp_table[new_idx];
1081 curbuf->b_u_curhead = cur_idx < 0 ? 0 : uhp_table[cur_idx];
1082 curbuf->b_u_line_ptr = line_ptr;
1083 curbuf->b_u_line_lnum = line_lnum;
1084 curbuf->b_u_line_colnr = line_colnr;
1085 curbuf->b_u_numhead = num_head;
1086 curbuf->b_u_seq_last = seq_last;
1087 curbuf->b_u_seq_cur = seq_cur;
1088 curbuf->b_u_seq_time = seq_time;
1089 U_FREE_LINE(uhp_table);
1090#ifdef U_DEBUG
1091 u_check(TRUE);
1092#endif
1093 if (name != NULL)
1094 smsg((char_u *)_("Finished reading undo file %s"), file_name);
1095 goto theend;
1096
1097error:
1098 if (line_ptr != NULL)
1099 U_FREE_LINE(line_ptr);
1100 if (uhp_table != NULL)
1101 {
1102 for (i = 0; i < num_head; i++)
1103 {
1104 if (uhp_table[i] != NULL)
1105 {
1106 uep = uhp_table[i]->uh_entry;
1107 while (uep != NULL)
1108 {
1109 nuep = uep->ue_next;
1110 u_freeentry(uep, uep->ue_size);
1111 uep = nuep;
1112 }
1113 U_FREE_LINE(uhp_table[i]);
1114 }
1115 }
1116 U_FREE_LINE(uhp_table);
1117 }
1118
1119theend:
1120 if (fp != NULL)
1121 fclose(fp);
1122 if (file_name != name)
1123 vim_free(file_name);
1124 return;
1125}
1126
1127/*
1128 * Serialize "uep" to "fp".
1129 */
1130 static int
1131serialize_uep(uep, fp)
1132 u_entry_T *uep;
1133 FILE *fp;
1134{
1135 int i;
1136 int uep_len;
1137 int *entry_lens;
1138
1139 if (uep->ue_size > 0)
1140 entry_lens = (int *)alloc(uep->ue_size * sizeof(int));
1141
1142 /* Define uep_len to be the size of the entire uep minus the size of its
1143 * component strings, in bytes. The sizes of the component strings
1144 * are written before each individual string.
1145 * We have 4 entries each of 4 bytes, plus ue_size * 4 bytes
1146 * of string size information. */
1147
1148 uep_len = uep->ue_size * 4;
1149 /* Collect sizing information for later serialization. */
1150 for (i = 0; i < uep->ue_size; i++)
1151 {
1152 entry_lens[i] = (int)STRLEN(uep->ue_array[i]);
1153 uep_len += entry_lens[i];
1154 }
1155 put_bytes(fp, (long_u)uep_len, 4);
1156 put_bytes(fp, (long_u)uep->ue_top, 4);
1157 put_bytes(fp, (long_u)uep->ue_bot, 4);
1158 put_bytes(fp, (long_u)uep->ue_lcount, 4);
1159 put_bytes(fp, (long_u)uep->ue_size, 4);
1160 for (i = 0; i < uep->ue_size; i++)
1161 {
1162 if (put_bytes(fp, (long_u)entry_lens[i], 4) == FAIL)
1163 return FAIL;
1164 fprintf(fp, "%s", uep->ue_array[i]);
1165 }
1166 if (uep->ue_size > 0)
1167 vim_free(entry_lens);
1168 return OK;
1169}
1170
1171/*
1172 * Serialize "pos" to "fp".
1173 */
1174 static void
1175serialize_pos(pos, fp)
1176 pos_T pos;
1177 FILE *fp;
1178{
1179 put_bytes(fp, (long_u)pos.lnum, 4);
1180 put_bytes(fp, (long_u)pos.col, 4);
1181#ifdef FEAT_VIRTUALEDIT
1182 put_bytes(fp, (long_u)pos.coladd, 4);
1183#else
1184 put_bytes(fp, (long_u)0, 4);
1185#endif
1186}
1187
1188/*
1189 * Serialize "info" to "fp".
1190 */
1191 static void
1192serialize_visualinfo(info, fp)
1193 visualinfo_T info;
1194 FILE *fp;
1195{
1196 serialize_pos(info.vi_start, fp);
1197 serialize_pos(info.vi_end, fp);
1198 put_bytes(fp, (long_u)info.vi_mode, 4);
1199 put_bytes(fp, (long_u)info.vi_curswant, 4);
1200}
1201
1202static char_u e_not_open[] = N_("E828: Cannot open undo file for writing: %s");
1203
1204/*
1205 * Write the undo tree in an undo file.
1206 * When "name" is not NULL, use it as the name of the undo file.
1207 * Otherwise use buf->b_ffname to generate the undo file name.
1208 * "buf" must never be null, buf->b_ffname is used to obtain the original file
1209 * permissions.
1210 * "forceit" is TRUE for ":wundo!", FALSE otherwise.
1211 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text.
1212 */
1213 void
1214u_write_undo(name, forceit, buf, hash)
1215 char_u *name;
1216 int forceit;
1217 buf_T *buf;
1218 char_u *hash;
1219{
1220 u_header_T *uhp;
1221 u_entry_T *uep;
1222 char_u *file_name;
1223 int str_len, i, uep_len, mark;
1224 int fd;
1225 FILE *fp = NULL;
1226 int perm;
1227 int write_ok = FALSE;
1228#ifdef UNIX
1229 struct stat st_old;
1230 struct stat st_new;
1231#endif
1232
1233 if (name == NULL)
1234 {
1235 file_name = u_get_undo_file_name(buf->b_ffname, FALSE);
1236 if (file_name == NULL)
1237 return;
1238 }
1239 else
1240 file_name = name;
1241
1242#ifdef UNIX
1243 if (mch_stat((char *)buf->b_ffname, &st_old) >= 0)
1244 perm = st_old.st_mode;
1245 else
1246 perm = 0600;
1247#else
1248 perm = mch_getperm(buf->b_ffname);
1249 if (perm < 0)
1250 perm = 0600;
1251#endif
1252 /* set file protection same as original file, but strip s-bit */
1253 perm = perm & 0777;
1254
1255 /* If the undo file exists, verify that it actually is an undo file, and
1256 * delete it. */
1257 if (mch_getperm(file_name) >= 0)
1258 {
1259 if (name == NULL || !forceit)
1260 {
1261 /* Check we can read it and it's an undo file. */
1262 fd = mch_open((char *)file_name, O_RDONLY|O_EXTRA, 0);
1263 if (fd < 0)
1264 {
1265 if (name != NULL || p_verbose > 0)
1266 smsg((char_u *)_("Will not overwrite with undo file, cannot read: %s"),
1267 file_name);
1268 goto theend;
1269 }
1270 else
1271 {
1272 char_u buf[2];
1273
1274 vim_read(fd, buf, 2);
1275 close(fd);
1276 if ((buf[0] << 8) + buf[1] != UF_START_MAGIC)
1277 {
1278 if (name != NULL || p_verbose > 0)
1279 smsg((char_u *)_("Will not overwrite, this is not an undo file: %s"),
1280 file_name);
1281 goto theend;
1282 }
1283 }
1284 }
1285 mch_remove(file_name);
1286 }
1287
1288 fd = mch_open((char *)file_name,
1289 O_CREAT|O_EXTRA|O_WRONLY|O_EXCL|O_NOFOLLOW, perm);
1290 (void)mch_setperm(file_name, perm);
1291 if (fd < 0)
1292 {
1293 EMSG2(_(e_not_open), file_name);
1294 goto theend;
1295 }
1296 if (p_verbose > 0)
1297 smsg((char_u *)_("Writing undo file: %s"), file_name);
1298
1299#ifdef UNIX
1300 /*
1301 * Try to set the group of the undo file same as the original file. If
1302 * this fails, set the protection bits for the group same as the
1303 * protection bits for others.
1304 */
1305 if (mch_stat((char *)file_name, &st_new) >= 0
1306 && st_new.st_gid != st_old.st_gid
1307# ifdef HAVE_FCHOWN /* sequent-ptx lacks fchown() */
1308 && fchown(fd, (uid_t)-1, st_old.st_gid) != 0
1309# endif
1310 )
1311 mch_setperm(file_name, (perm & 0707) | ((perm & 07) << 3));
1312# ifdef HAVE_SELINUX
1313 mch_copy_sec(buf->b_ffname, file_name);
1314# endif
1315#endif
1316
1317 fp = fdopen(fd, "w");
1318 if (fp == NULL)
1319 {
1320 EMSG2(_(e_not_open), file_name);
1321 close(fd);
1322 mch_remove(file_name);
1323 goto theend;
1324 }
1325
1326 /* Start writing, first overall file information */
1327 put_bytes(fp, (long_u)UF_START_MAGIC, 2);
1328 put_bytes(fp, (long_u)UF_VERSION, 2);
1329
1330 /* Write a hash of the buffer text, so that we can verify it is still the
1331 * same when reading the buffer text. */
1332 if (fwrite(hash, (size_t)UNDO_HASH_SIZE, (size_t)1, fp) != 1)
1333 goto write_error;
1334 put_bytes(fp, (long_u)buf->b_ml.ml_line_count, 4);
1335
1336 /* Begin undo data for U */
1337 str_len = buf->b_u_line_ptr != NULL ? STRLEN(buf->b_u_line_ptr) : 0;
1338 put_bytes(fp, (long_u)str_len, 4);
1339 if (str_len > 0 && fwrite(buf->b_u_line_ptr, (size_t)str_len,
1340 (size_t)1, fp) != 1)
1341 goto write_error;
1342
1343 put_bytes(fp, (long_u)buf->b_u_line_lnum, 4);
1344 put_bytes(fp, (long_u)buf->b_u_line_colnr, 4);
1345
1346 /* Begin general undo data */
1347 uhp = buf->b_u_oldhead;
1348 put_bytes(fp, (long_u)(uhp != NULL ? uhp->uh_seq : 0), 4);
1349
1350 uhp = buf->b_u_newhead;
1351 put_bytes(fp, (long_u)(uhp != NULL ? uhp->uh_seq : 0), 4);
1352
1353 uhp = buf->b_u_curhead;
1354 put_bytes(fp, (long_u)(uhp != NULL ? uhp->uh_seq : 0), 4);
1355
1356 put_bytes(fp, (long_u)buf->b_u_numhead, 4);
1357 put_bytes(fp, (long_u)buf->b_u_seq_last, 4);
1358 put_bytes(fp, (long_u)buf->b_u_seq_cur, 4);
1359 put_bytes(fp, (long_u)buf->b_u_seq_time, 4);
1360
1361 /* Iteratively serialize UHPs and their UEPs from the top down. */
1362 mark = ++lastmark;
1363 uhp = buf->b_u_oldhead;
1364 while (uhp != NULL)
1365 {
1366 /* Serialize current UHP if we haven't seen it */
1367 if (uhp->uh_walk != mark)
1368 {
1369 if (put_bytes(fp, (long_u)UF_HEADER_MAGIC, 2) == FAIL)
1370 goto write_error;
1371
1372 put_bytes(fp, (long_u)((uhp->uh_next != NULL)
1373 ? uhp->uh_next->uh_seq : 0), 4);
1374 put_bytes(fp, (long_u)((uhp->uh_prev != NULL)
1375 ? uhp->uh_prev->uh_seq : 0), 4);
1376 put_bytes(fp, (long_u)((uhp->uh_alt_next != NULL)
1377 ? uhp->uh_alt_next->uh_seq : 0), 4);
1378 put_bytes(fp, (long_u)((uhp->uh_alt_prev != NULL)
1379 ? uhp->uh_alt_prev->uh_seq : 0), 4);
1380 put_bytes(fp, uhp->uh_seq, 4);
1381 serialize_pos(uhp->uh_cursor, fp);
1382#ifdef FEAT_VIRTUALEDIT
1383 put_bytes(fp, (long_u)uhp->uh_cursor_vcol, 4);
1384#else
1385 put_bytes(fp, (long_u)0, 4);
1386#endif
1387 put_bytes(fp, (long_u)uhp->uh_flags, 2);
1388 /* Assume NMARKS will stay the same. */
1389 for (i = 0; i < NMARKS; ++i)
1390 {
1391 serialize_pos(uhp->uh_namedm[i], fp);
1392 }
1393#ifdef FEAT_VISUAL
1394 serialize_visualinfo(uhp->uh_visual, fp);
1395#endif
1396 put_bytes(fp, (long_u)uhp->uh_time, 4);
1397
1398 uep = uhp->uh_entry;
1399 while (uep != NULL)
1400 {
1401 if (serialize_uep(uep, fp) == FAIL)
1402 goto write_error;
1403 uep = uep->ue_next;
1404 }
1405 /* Sentinel value: no more ueps */
1406 uep_len = -1;
1407 put_bytes(fp, (long_u)uep_len, 4);
1408 uhp->uh_walk = mark;
1409 }
1410
1411 /* Now walk through the tree - algorithm from undo_time */
1412 if (uhp->uh_prev != NULL && uhp->uh_prev->uh_walk != mark)
1413 uhp = uhp->uh_prev;
1414 else if (uhp->uh_alt_next != NULL && uhp->uh_alt_next->uh_walk != mark)
1415 uhp = uhp->uh_alt_next;
1416 else if (uhp->uh_next != NULL && uhp->uh_alt_prev == NULL
1417 && uhp->uh_next->uh_walk != mark)
1418 uhp = uhp->uh_next;
1419 else if (uhp->uh_alt_prev != NULL)
1420 uhp = uhp->uh_alt_prev;
1421 else
1422 uhp = uhp->uh_next;
1423 }
1424
1425 if (put_bytes(fp, (long_u)UF_END_MAGIC, 2) == OK)
1426 write_ok = TRUE;
1427
1428write_error:
1429 fclose(fp);
1430 if (!write_ok)
1431 EMSG2(_("E829: write error in undo file: %s"), file_name);
1432
1433#if defined(MACOS_CLASSIC) || defined(WIN3264)
1434 (void)mch_copy_file_attribute(buf->b_ffname, file_name);
1435#endif
1436#ifdef HAVE_ACL
1437 {
1438 vim_acl_T acl;
1439
1440 /* For systems that support ACL: get the ACL from the original file. */
1441 acl = mch_get_acl(buf->b_ffname);
1442 mch_set_acl(file_name, acl);
1443 }
1444#endif
1445
1446theend:
1447 if (file_name != name)
1448 vim_free(file_name);
1449}
1450
1451#endif /* FEAT_PERSISTENT_UNDO */
1452
1453
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454/*
1455 * If 'cpoptions' contains 'u': Undo the previous undo or redo (vi compatible).
1456 * If 'cpoptions' does not contain 'u': Always undo.
1457 */
1458 void
1459u_undo(count)
1460 int count;
1461{
1462 /*
1463 * If we get an undo command while executing a macro, we behave like the
1464 * original vi. If this happens twice in one macro the result will not
1465 * be compatible.
1466 */
1467 if (curbuf->b_u_synced == FALSE)
1468 {
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001469 u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 count = 1;
1471 }
1472
1473 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
1474 undo_undoes = TRUE;
1475 else
1476 undo_undoes = !undo_undoes;
1477 u_doit(count);
1478}
1479
1480/*
1481 * If 'cpoptions' contains 'u': Repeat the previous undo or redo.
1482 * If 'cpoptions' does not contain 'u': Always redo.
1483 */
1484 void
1485u_redo(count)
1486 int count;
1487{
1488 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
1489 undo_undoes = FALSE;
1490 u_doit(count);
1491}
1492
1493/*
1494 * Undo or redo, depending on 'undo_undoes', 'count' times.
1495 */
1496 static void
Bram Moolenaarca003e12006-03-17 23:19:38 +00001497u_doit(startcount)
1498 int startcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499{
Bram Moolenaarca003e12006-03-17 23:19:38 +00001500 int count = startcount;
1501
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001502 if (!undo_allowed())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504
1505 u_newcount = 0;
1506 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001507 if (curbuf->b_ml.ml_flags & ML_EMPTY)
1508 u_oldcount = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 while (count--)
1510 {
1511 if (undo_undoes)
1512 {
1513 if (curbuf->b_u_curhead == NULL) /* first undo */
1514 curbuf->b_u_curhead = curbuf->b_u_newhead;
1515 else if (p_ul > 0) /* multi level undo */
1516 /* get next undo */
1517 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_next;
1518 /* nothing to undo */
1519 if (curbuf->b_u_numhead == 0 || curbuf->b_u_curhead == NULL)
1520 {
1521 /* stick curbuf->b_u_curhead at end */
1522 curbuf->b_u_curhead = curbuf->b_u_oldhead;
1523 beep_flush();
Bram Moolenaarca003e12006-03-17 23:19:38 +00001524 if (count == startcount - 1)
1525 {
1526 MSG(_("Already at oldest change"));
1527 return;
1528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 break;
1530 }
1531
Bram Moolenaarca003e12006-03-17 23:19:38 +00001532 u_undoredo(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 }
1534 else
1535 {
1536 if (curbuf->b_u_curhead == NULL || p_ul <= 0)
1537 {
1538 beep_flush(); /* nothing to redo */
Bram Moolenaarca003e12006-03-17 23:19:38 +00001539 if (count == startcount - 1)
1540 {
1541 MSG(_("Already at newest change"));
1542 return;
1543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 break;
1545 }
1546
Bram Moolenaarca003e12006-03-17 23:19:38 +00001547 u_undoredo(FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00001548
1549 /* Advance for next redo. Set "newhead" when at the end of the
1550 * redoable changes. */
1551 if (curbuf->b_u_curhead->uh_prev == NULL)
1552 curbuf->b_u_newhead = curbuf->b_u_curhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_prev;
1554 }
1555 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001556 u_undo_end(undo_undoes, FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00001557}
1558
Bram Moolenaar1e607892006-03-13 22:15:53 +00001559/*
1560 * Undo or redo over the timeline.
1561 * When "step" is negative go back in time, otherwise goes forward in time.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001562 * When "sec" is FALSE make "step" steps, when "sec" is TRUE use "step" as
1563 * seconds.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001564 * When "absolute" is TRUE use "step" as the sequence number to jump to.
1565 * "sec" must be FALSE then.
Bram Moolenaar1e607892006-03-13 22:15:53 +00001566 */
1567 void
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001568undo_time(step, sec, absolute)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001569 long step;
1570 int sec;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001571 int absolute;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001572{
1573 long target;
1574 long closest;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001575 long closest_start;
1576 long closest_seq = 0;
1577 long val;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001578 u_header_T *uhp;
1579 u_header_T *last;
1580 int mark;
1581 int nomark;
1582 int round;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001583 int dosec = sec;
1584 int above = FALSE;
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001585 int did_undo = TRUE;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001586
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001587 /* First make sure the current undoable change is synced. */
1588 if (curbuf->b_u_synced == FALSE)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001589 u_sync(TRUE);
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001590
Bram Moolenaar1e607892006-03-13 22:15:53 +00001591 u_newcount = 0;
1592 u_oldcount = 0;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001593 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar1e607892006-03-13 22:15:53 +00001594 u_oldcount = -1;
1595
Bram Moolenaarca003e12006-03-17 23:19:38 +00001596 /* "target" is the node below which we want to be.
1597 * Init "closest" to a value we can't reach. */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001598 if (absolute)
1599 {
1600 target = step;
Bram Moolenaarca003e12006-03-17 23:19:38 +00001601 closest = -1;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001602 }
Bram Moolenaarca003e12006-03-17 23:19:38 +00001603 else
Bram Moolenaar1e607892006-03-13 22:15:53 +00001604 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00001605 /* When doing computations with time_t subtract starttime, because
1606 * time_t converted to a long may result in a wrong number. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001607 if (sec)
Bram Moolenaarca003e12006-03-17 23:19:38 +00001608 target = (long)(curbuf->b_u_seq_time - starttime) + step;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001609 else
1610 target = curbuf->b_u_seq_cur + step;
Bram Moolenaarca003e12006-03-17 23:19:38 +00001611 if (step < 0)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001612 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00001613 if (target < 0)
1614 target = 0;
1615 closest = -1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001616 }
1617 else
1618 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00001619 if (sec)
Bram Moolenaardb552d602006-03-23 22:59:57 +00001620 closest = (long)(time(NULL) - starttime + 1);
Bram Moolenaarca003e12006-03-17 23:19:38 +00001621 else
1622 closest = curbuf->b_u_seq_last + 2;
1623 if (target >= closest)
1624 target = closest - 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001625 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00001626 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001627 closest_start = closest;
Bram Moolenaarca003e12006-03-17 23:19:38 +00001628 closest_seq = curbuf->b_u_seq_cur;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001629
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001630 /*
1631 * May do this twice:
Bram Moolenaar1e607892006-03-13 22:15:53 +00001632 * 1. Search for "target", update "closest" to the best match found.
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001633 * 2. If "target" not found search for "closest".
1634 *
1635 * When using the closest time we use the sequence number in the second
1636 * round, because there may be several entries with the same time.
1637 */
Bram Moolenaar1e607892006-03-13 22:15:53 +00001638 for (round = 1; round <= 2; ++round)
1639 {
1640 /* Find the path from the current state to where we want to go. The
1641 * desired state can be anywhere in the undo tree, need to go all over
1642 * it. We put "nomark" in uh_walk where we have been without success,
1643 * "mark" where it could possibly be. */
1644 mark = ++lastmark;
1645 nomark = ++lastmark;
1646
1647 if (curbuf->b_u_curhead == NULL) /* at leaf of the tree */
1648 uhp = curbuf->b_u_newhead;
1649 else
1650 uhp = curbuf->b_u_curhead;
1651
1652 while (uhp != NULL)
1653 {
1654 uhp->uh_walk = mark;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001655 val = (long)(dosec ? (uhp->uh_time - starttime) : uhp->uh_seq);
Bram Moolenaar1e607892006-03-13 22:15:53 +00001656
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001657 if (round == 1)
1658 {
1659 /* Remember the header that is closest to the target.
1660 * It must be at least in the right direction (checked with
Bram Moolenaarca003e12006-03-17 23:19:38 +00001661 * "b_u_seq_cur"). When the timestamp is equal find the
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001662 * highest/lowest sequence number. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00001663 if ((step < 0 ? uhp->uh_seq <= curbuf->b_u_seq_cur
1664 : uhp->uh_seq > curbuf->b_u_seq_cur)
1665 && ((dosec && val == closest)
1666 ? (step < 0
1667 ? uhp->uh_seq < closest_seq
1668 : uhp->uh_seq > closest_seq)
1669 : closest == closest_start
1670 || (val > target
1671 ? (closest > target
1672 ? val - target <= closest - target
1673 : val - target <= target - closest)
1674 : (closest > target
1675 ? target - val <= closest - target
1676 : target - val <= target - closest))))
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001677 {
1678 closest = val;
1679 closest_seq = uhp->uh_seq;
1680 }
1681 }
1682
1683 /* Quit searching when we found a match. But when searching for a
1684 * time we need to continue looking for the best uh_seq. */
1685 if (target == val && !dosec)
1686 break;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001687
1688 /* go down in the tree if we haven't been there */
1689 if (uhp->uh_prev != NULL && uhp->uh_prev->uh_walk != nomark
1690 && uhp->uh_prev->uh_walk != mark)
1691 uhp = uhp->uh_prev;
1692
1693 /* go to alternate branch if we haven't been there */
1694 else if (uhp->uh_alt_next != NULL
1695 && uhp->uh_alt_next->uh_walk != nomark
1696 && uhp->uh_alt_next->uh_walk != mark)
1697 uhp = uhp->uh_alt_next;
1698
1699 /* go up in the tree if we haven't been there and we are at the
1700 * start of alternate branches */
1701 else if (uhp->uh_next != NULL && uhp->uh_alt_prev == NULL
1702 && uhp->uh_next->uh_walk != nomark
1703 && uhp->uh_next->uh_walk != mark)
Bram Moolenaardb552d602006-03-23 22:59:57 +00001704 {
1705 /* If still at the start we don't go through this change. */
1706 if (uhp == curbuf->b_u_curhead)
1707 uhp->uh_walk = nomark;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001708 uhp = uhp->uh_next;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001709 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00001710
1711 else
1712 {
1713 /* need to backtrack; mark this node as useless */
1714 uhp->uh_walk = nomark;
1715 if (uhp->uh_alt_prev != NULL)
1716 uhp = uhp->uh_alt_prev;
1717 else
1718 uhp = uhp->uh_next;
1719 }
1720 }
1721
1722 if (uhp != NULL) /* found it */
1723 break;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001724
1725 if (absolute)
1726 {
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001727 EMSGN(_("E830: Undo number %ld not found"), step);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001728 return;
1729 }
1730
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001731 if (closest == closest_start)
Bram Moolenaar1e607892006-03-13 22:15:53 +00001732 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001733 if (step < 0)
1734 MSG(_("Already at oldest change"));
1735 else
1736 MSG(_("Already at newest change"));
Bram Moolenaar1e607892006-03-13 22:15:53 +00001737 return;
1738 }
1739
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001740 target = closest_seq;
1741 dosec = FALSE;
1742 if (step < 0)
1743 above = TRUE; /* stop above the header */
Bram Moolenaar1e607892006-03-13 22:15:53 +00001744 }
1745
1746 /* If we found it: Follow the path to go to where we want to be. */
1747 if (uhp != NULL)
1748 {
1749 /*
1750 * First go up the tree as much as needed.
1751 */
1752 for (;;)
1753 {
1754 uhp = curbuf->b_u_curhead;
1755 if (uhp == NULL)
1756 uhp = curbuf->b_u_newhead;
1757 else
Bram Moolenaar1e607892006-03-13 22:15:53 +00001758 uhp = uhp->uh_next;
Bram Moolenaarca003e12006-03-17 23:19:38 +00001759 if (uhp == NULL || uhp->uh_walk != mark
1760 || (uhp->uh_seq == target && !above))
Bram Moolenaar1e607892006-03-13 22:15:53 +00001761 break;
1762 curbuf->b_u_curhead = uhp;
Bram Moolenaarca003e12006-03-17 23:19:38 +00001763 u_undoredo(TRUE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00001764 uhp->uh_walk = nomark; /* don't go back down here */
Bram Moolenaar1e607892006-03-13 22:15:53 +00001765 }
1766
1767 /*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001768 * And now go down the tree (redo), branching off where needed.
Bram Moolenaar1e607892006-03-13 22:15:53 +00001769 */
1770 uhp = curbuf->b_u_curhead;
Bram Moolenaarca003e12006-03-17 23:19:38 +00001771 while (uhp != NULL)
Bram Moolenaar1e607892006-03-13 22:15:53 +00001772 {
Bram Moolenaar89ed3df2007-01-09 19:23:12 +00001773 /* Go back to the first branch with a mark. */
1774 while (uhp->uh_alt_prev != NULL
1775 && uhp->uh_alt_prev->uh_walk == mark)
1776 uhp = uhp->uh_alt_prev;
1777
Bram Moolenaar1e607892006-03-13 22:15:53 +00001778 /* Find the last branch with a mark, that's the one. */
1779 last = uhp;
1780 while (last->uh_alt_next != NULL
1781 && last->uh_alt_next->uh_walk == mark)
1782 last = last->uh_alt_next;
1783 if (last != uhp)
1784 {
1785 /* Make the used branch the first entry in the list of
1786 * alternatives to make "u" and CTRL-R take this branch. */
Bram Moolenaar89ed3df2007-01-09 19:23:12 +00001787 while (uhp->uh_alt_prev != NULL)
1788 uhp = uhp->uh_alt_prev;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001789 if (last->uh_alt_next != NULL)
1790 last->uh_alt_next->uh_alt_prev = last->uh_alt_prev;
1791 last->uh_alt_prev->uh_alt_next = last->uh_alt_next;
1792 last->uh_alt_prev = NULL;
1793 last->uh_alt_next = uhp;
1794 uhp->uh_alt_prev = last;
1795
1796 uhp = last;
Bram Moolenaarca003e12006-03-17 23:19:38 +00001797 if (uhp->uh_next != NULL)
1798 uhp->uh_next->uh_prev = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001799 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001800 curbuf->b_u_curhead = uhp;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001801
1802 if (uhp->uh_walk != mark)
1803 break; /* must have reached the target */
1804
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001805 /* Stop when going backwards in time and didn't find the exact
1806 * header we were looking for. */
1807 if (uhp->uh_seq == target && above)
Bram Moolenaardb552d602006-03-23 22:59:57 +00001808 {
1809 curbuf->b_u_seq_cur = target - 1;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001810 break;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001811 }
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001812
Bram Moolenaarca003e12006-03-17 23:19:38 +00001813 u_undoredo(FALSE);
Bram Moolenaar1e607892006-03-13 22:15:53 +00001814
1815 /* Advance "curhead" to below the header we last used. If it
1816 * becomes NULL then we need to set "newhead" to this leaf. */
1817 if (uhp->uh_prev == NULL)
1818 curbuf->b_u_newhead = uhp;
1819 curbuf->b_u_curhead = uhp->uh_prev;
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001820 did_undo = FALSE;
Bram Moolenaar1e607892006-03-13 22:15:53 +00001821
1822 if (uhp->uh_seq == target) /* found it! */
1823 break;
1824
1825 uhp = uhp->uh_prev;
1826 if (uhp == NULL || uhp->uh_walk != mark)
1827 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00001828 /* Need to redo more but can't find it... */
Bram Moolenaar1e607892006-03-13 22:15:53 +00001829 EMSG2(_(e_intern2), "undo_time()");
1830 break;
1831 }
1832 }
1833 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001834 u_undo_end(did_undo, absolute);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835}
1836
1837/*
1838 * u_undoredo: common code for undo and redo
1839 *
1840 * The lines in the file are replaced by the lines in the entry list at
1841 * curbuf->b_u_curhead. The replaced lines in the file are saved in the entry
1842 * list for the next undo/redo.
Bram Moolenaarca003e12006-03-17 23:19:38 +00001843 *
1844 * When "undo" is TRUE we go up in the tree, when FALSE we go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 */
1846 static void
Bram Moolenaarca003e12006-03-17 23:19:38 +00001847u_undoredo(undo)
1848 int undo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849{
1850 char_u **newarray = NULL;
1851 linenr_T oldsize;
1852 linenr_T newsize;
1853 linenr_T top, bot;
1854 linenr_T lnum;
1855 linenr_T newlnum = MAXLNUM;
1856 long i;
1857 u_entry_T *uep, *nuep;
1858 u_entry_T *newlist = NULL;
1859 int old_flags;
1860 int new_flags;
1861 pos_T namedm[NMARKS];
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001862#ifdef FEAT_VISUAL
1863 visualinfo_T visualinfo;
1864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 int empty_buffer; /* buffer became empty */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001866 u_header_T *curhead = curbuf->b_u_curhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867
Bram Moolenaarfecb6602007-10-01 20:54:15 +00001868#ifdef U_DEBUG
1869 u_check(FALSE);
1870#endif
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001871 old_flags = curhead->uh_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 new_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
1873 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
1874 setpcmark();
1875
1876 /*
1877 * save marks before undo/redo
1878 */
1879 mch_memmove(namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001880#ifdef FEAT_VISUAL
1881 visualinfo = curbuf->b_visual;
1882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 curbuf->b_op_start.lnum = curbuf->b_ml.ml_line_count;
1884 curbuf->b_op_start.col = 0;
1885 curbuf->b_op_end.lnum = 0;
1886 curbuf->b_op_end.col = 0;
1887
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001888 for (uep = curhead->uh_entry; uep != NULL; uep = nuep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 {
1890 top = uep->ue_top;
1891 bot = uep->ue_bot;
1892 if (bot == 0)
1893 bot = curbuf->b_ml.ml_line_count + 1;
1894 if (top > curbuf->b_ml.ml_line_count || top >= bot
1895 || bot > curbuf->b_ml.ml_line_count + 1)
1896 {
1897 EMSG(_("E438: u_undo: line numbers wrong"));
1898 changed(); /* don't want UNCHANGED now */
1899 return;
1900 }
1901
1902 oldsize = bot - top - 1; /* number of lines before undo */
1903 newsize = uep->ue_size; /* number of lines after undo */
1904
1905 if (top < newlnum)
1906 {
1907 /* If the saved cursor is somewhere in this undo block, move it to
1908 * the remembered position. Makes "gwap" put the cursor back
1909 * where it was. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001910 lnum = curhead->uh_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 if (lnum >= top && lnum <= top + newsize + 1)
1912 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001913 curwin->w_cursor = curhead->uh_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 newlnum = curwin->w_cursor.lnum - 1;
1915 }
1916 else
1917 {
1918 /* Use the first line that actually changed. Avoids that
1919 * undoing auto-formatting puts the cursor in the previous
1920 * line. */
1921 for (i = 0; i < newsize && i < oldsize; ++i)
1922 if (STRCMP(uep->ue_array[i], ml_get(top + 1 + i)) != 0)
1923 break;
1924 if (i == newsize && newlnum == MAXLNUM && uep->ue_next == NULL)
1925 {
1926 newlnum = top;
1927 curwin->w_cursor.lnum = newlnum + 1;
1928 }
1929 else if (i < newsize)
1930 {
1931 newlnum = top + i;
1932 curwin->w_cursor.lnum = newlnum + 1;
1933 }
1934 }
1935 }
1936
1937 empty_buffer = FALSE;
1938
1939 /* delete the lines between top and bot and save them in newarray */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001940 if (oldsize > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001942 if ((newarray = (char_u **)U_ALLOC_LINE(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 (unsigned)(sizeof(char_u *) * oldsize))) == NULL)
1944 {
1945 do_outofmem_msg((long_u)(sizeof(char_u *) * oldsize));
1946 /*
1947 * We have messed up the entry list, repair is impossible.
1948 * we have to free the rest of the list.
1949 */
1950 while (uep != NULL)
1951 {
1952 nuep = uep->ue_next;
1953 u_freeentry(uep, uep->ue_size);
1954 uep = nuep;
1955 }
1956 break;
1957 }
1958 /* delete backwards, it goes faster in most cases */
1959 for (lnum = bot - 1, i = oldsize; --i >= 0; --lnum)
1960 {
1961 /* what can we do when we run out of memory? */
1962 if ((newarray[i] = u_save_line(lnum)) == NULL)
1963 do_outofmem_msg((long_u)0);
1964 /* remember we deleted the last line in the buffer, and a
1965 * dummy empty line will be inserted */
1966 if (curbuf->b_ml.ml_line_count == 1)
1967 empty_buffer = TRUE;
1968 ml_delete(lnum, FALSE);
1969 }
1970 }
Bram Moolenaar8d343302005-07-12 22:46:17 +00001971 else
1972 newarray = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973
1974 /* insert the lines in u_array between top and bot */
1975 if (newsize)
1976 {
1977 for (lnum = top, i = 0; i < newsize; ++i, ++lnum)
1978 {
1979 /*
1980 * If the file is empty, there is an empty line 1 that we
1981 * should get rid of, by replacing it with the new line
1982 */
1983 if (empty_buffer && lnum == 0)
1984 ml_replace((linenr_T)1, uep->ue_array[i], TRUE);
1985 else
1986 ml_append(lnum, uep->ue_array[i], (colnr_T)0, FALSE);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001987 U_FREE_LINE(uep->ue_array[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001989 U_FREE_LINE((char_u *)uep->ue_array);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 }
1991
1992 /* adjust marks */
1993 if (oldsize != newsize)
1994 {
1995 mark_adjust(top + 1, top + oldsize, (long)MAXLNUM,
1996 (long)newsize - (long)oldsize);
1997 if (curbuf->b_op_start.lnum > top + oldsize)
1998 curbuf->b_op_start.lnum += newsize - oldsize;
1999 if (curbuf->b_op_end.lnum > top + oldsize)
2000 curbuf->b_op_end.lnum += newsize - oldsize;
2001 }
2002
2003 changed_lines(top + 1, 0, bot, newsize - oldsize);
2004
2005 /* set '[ and '] mark */
2006 if (top + 1 < curbuf->b_op_start.lnum)
2007 curbuf->b_op_start.lnum = top + 1;
2008 if (newsize == 0 && top + 1 > curbuf->b_op_end.lnum)
2009 curbuf->b_op_end.lnum = top + 1;
2010 else if (top + newsize > curbuf->b_op_end.lnum)
2011 curbuf->b_op_end.lnum = top + newsize;
2012
2013 u_newcount += newsize;
2014 u_oldcount += oldsize;
2015 uep->ue_size = oldsize;
2016 uep->ue_array = newarray;
2017 uep->ue_bot = top + newsize + 1;
2018
2019 /*
2020 * insert this entry in front of the new entry list
2021 */
2022 nuep = uep->ue_next;
2023 uep->ue_next = newlist;
2024 newlist = uep;
2025 }
2026
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002027 curhead->uh_entry = newlist;
2028 curhead->uh_flags = new_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 if ((old_flags & UH_EMPTYBUF) && bufempty())
2030 curbuf->b_ml.ml_flags |= ML_EMPTY;
2031 if (old_flags & UH_CHANGED)
2032 changed();
2033 else
Bram Moolenaar009b2592004-10-24 19:18:58 +00002034#ifdef FEAT_NETBEANS_INTG
2035 /* per netbeans undo rules, keep it as modified */
2036 if (!isNetbeansModified(curbuf))
2037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 unchanged(curbuf, FALSE);
2039
2040 /*
2041 * restore marks from before undo/redo
2042 */
2043 for (i = 0; i < NMARKS; ++i)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002044 if (curhead->uh_namedm[i].lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002046 curbuf->b_namedm[i] = curhead->uh_namedm[i];
2047 curhead->uh_namedm[i] = namedm[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002049#ifdef FEAT_VISUAL
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002050 if (curhead->uh_visual.vi_start.lnum != 0)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002051 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002052 curbuf->b_visual = curhead->uh_visual;
2053 curhead->uh_visual = visualinfo;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002054 }
2055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056
2057 /*
2058 * If the cursor is only off by one line, put it at the same position as
2059 * before starting the change (for the "o" command).
2060 * Otherwise the cursor should go to the first undone line.
2061 */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002062 if (curhead->uh_cursor.lnum + 1 == curwin->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 && curwin->w_cursor.lnum > 1)
2064 --curwin->w_cursor.lnum;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002065 if (curhead->uh_cursor.lnum == curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 {
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002067 curwin->w_cursor.col = curhead->uh_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002069 if (virtual_active() && curhead->uh_cursor_vcol >= 0)
2070 coladvance((colnr_T)curhead->uh_cursor_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 else
2072 curwin->w_cursor.coladd = 0;
2073#endif
2074 }
2075 else if (curwin->w_cursor.lnum <= curbuf->b_ml.ml_line_count)
2076 beginline(BL_SOL | BL_FIX);
2077 else
2078 {
2079 /* We get here with the current cursor line being past the end (eg
2080 * after adding lines at the end of the file, and then undoing it).
2081 * check_cursor() will move the cursor to the last line. Move it to
2082 * the first column here. */
2083 curwin->w_cursor.col = 0;
2084#ifdef FEAT_VIRTUALEDIT
2085 curwin->w_cursor.coladd = 0;
2086#endif
2087 }
2088
2089 /* Make sure the cursor is on an existing line and column. */
2090 check_cursor();
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002091
2092 /* Remember where we are for "g-" and ":earlier 10s". */
2093 curbuf->b_u_seq_cur = curhead->uh_seq;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002094 if (undo)
2095 /* We are below the previous undo. However, to make ":earlier 1s"
2096 * work we compute this as being just above the just undone change. */
2097 --curbuf->b_u_seq_cur;
2098
2099 /* The timestamp can be the same for multiple changes, just use the one of
2100 * the undone/redone change. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002101 curbuf->b_u_seq_time = curhead->uh_time;
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002102#ifdef U_DEBUG
2103 u_check(FALSE);
2104#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105}
2106
2107/*
2108 * If we deleted or added lines, report the number of less/more lines.
2109 * Otherwise, report the number of changes (this may be incorrect
2110 * in some cases, but it's better than nothing).
2111 */
2112 static void
Bram Moolenaardb552d602006-03-23 22:59:57 +00002113u_undo_end(did_undo, absolute)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002114 int did_undo; /* just did an undo */
Bram Moolenaardb552d602006-03-23 22:59:57 +00002115 int absolute; /* used ":undo N" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116{
Bram Moolenaar89d40322006-08-29 15:30:07 +00002117 char *msgstr;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002118 u_header_T *uhp;
2119 char_u msgbuf[80];
Bram Moolenaar1e607892006-03-13 22:15:53 +00002120
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121#ifdef FEAT_FOLDING
2122 if ((fdo_flags & FDO_UNDO) && KeyTyped)
2123 foldOpenCursor();
2124#endif
Bram Moolenaar1e607892006-03-13 22:15:53 +00002125
2126 if (global_busy /* no messages now, wait until global is finished */
2127 || !messaging()) /* 'lazyredraw' set, don't do messages now */
2128 return;
2129
2130 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2131 --u_newcount;
2132
2133 u_oldcount -= u_newcount;
2134 if (u_oldcount == -1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002135 msgstr = N_("more line");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002136 else if (u_oldcount < 0)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002137 msgstr = N_("more lines");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002138 else if (u_oldcount == 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002139 msgstr = N_("line less");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002140 else if (u_oldcount > 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002141 msgstr = N_("fewer lines");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002142 else
2143 {
2144 u_oldcount = u_newcount;
2145 if (u_newcount == 1)
Bram Moolenaar89d40322006-08-29 15:30:07 +00002146 msgstr = N_("change");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002147 else
Bram Moolenaar89d40322006-08-29 15:30:07 +00002148 msgstr = N_("changes");
Bram Moolenaar1e607892006-03-13 22:15:53 +00002149 }
2150
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002151 if (curbuf->b_u_curhead != NULL)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002152 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00002153 /* For ":undo N" we prefer a "after #N" message. */
2154 if (absolute && curbuf->b_u_curhead->uh_next != NULL)
2155 {
2156 uhp = curbuf->b_u_curhead->uh_next;
2157 did_undo = FALSE;
2158 }
2159 else if (did_undo)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002160 uhp = curbuf->b_u_curhead;
2161 else
2162 uhp = curbuf->b_u_curhead->uh_next;
2163 }
Bram Moolenaar1e607892006-03-13 22:15:53 +00002164 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002165 uhp = curbuf->b_u_newhead;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002166
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002167 if (uhp == NULL)
2168 *msgbuf = NUL;
2169 else
2170 u_add_time(msgbuf, sizeof(msgbuf), uhp->uh_time);
2171
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002172 smsg((char_u *)_("%ld %s; %s #%ld %s"),
Bram Moolenaarca003e12006-03-17 23:19:38 +00002173 u_oldcount < 0 ? -u_oldcount : u_oldcount,
Bram Moolenaar89d40322006-08-29 15:30:07 +00002174 _(msgstr),
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002175 did_undo ? _("before") : _("after"),
2176 uhp == NULL ? 0L : uhp->uh_seq,
2177 msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178}
2179
2180/*
2181 * u_sync: stop adding to the current entry list
2182 */
2183 void
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002184u_sync(force)
2185 int force; /* Also sync when no_u_sync is set. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186{
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002187 /* Skip it when already synced or syncing is disabled. */
2188 if (curbuf->b_u_synced || (!force && no_u_sync > 0))
2189 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2191 if (im_is_preediting())
2192 return; /* XIM is busy, don't break an undo sequence */
2193#endif
2194 if (p_ul < 0)
2195 curbuf->b_u_synced = TRUE; /* no entries, nothing to do */
2196 else
2197 {
2198 u_getbot(); /* compute ue_bot of previous u_save */
2199 curbuf->b_u_curhead = NULL;
2200 }
2201}
2202
2203/*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002204 * ":undolist": List the leafs of the undo tree
2205 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002206 void
2207ex_undolist(eap)
Bram Moolenaarfff2bee2010-05-15 13:56:02 +02002208 exarg_T *eap UNUSED;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002209{
2210 garray_T ga;
2211 u_header_T *uhp;
2212 int mark;
2213 int nomark;
2214 int changes = 1;
2215 int i;
2216
2217 /*
2218 * 1: walk the tree to find all leafs, put the info in "ga".
2219 * 2: sort the lines
2220 * 3: display the list
2221 */
2222 mark = ++lastmark;
2223 nomark = ++lastmark;
2224 ga_init2(&ga, (int)sizeof(char *), 20);
2225
2226 uhp = curbuf->b_u_oldhead;
2227 while (uhp != NULL)
2228 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00002229 if (uhp->uh_prev == NULL && uhp->uh_walk != nomark
2230 && uhp->uh_walk != mark)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002231 {
2232 if (ga_grow(&ga, 1) == FAIL)
2233 break;
2234 vim_snprintf((char *)IObuff, IOSIZE, "%6ld %7ld ",
2235 uhp->uh_seq, changes);
2236 u_add_time(IObuff + STRLEN(IObuff), IOSIZE - STRLEN(IObuff),
2237 uhp->uh_time);
2238 ((char_u **)(ga.ga_data))[ga.ga_len++] = vim_strsave(IObuff);
2239 }
2240
2241 uhp->uh_walk = mark;
2242
2243 /* go down in the tree if we haven't been there */
2244 if (uhp->uh_prev != NULL && uhp->uh_prev->uh_walk != nomark
2245 && uhp->uh_prev->uh_walk != mark)
2246 {
2247 uhp = uhp->uh_prev;
2248 ++changes;
2249 }
2250
2251 /* go to alternate branch if we haven't been there */
2252 else if (uhp->uh_alt_next != NULL
2253 && uhp->uh_alt_next->uh_walk != nomark
2254 && uhp->uh_alt_next->uh_walk != mark)
2255 uhp = uhp->uh_alt_next;
2256
2257 /* go up in the tree if we haven't been there and we are at the
2258 * start of alternate branches */
2259 else if (uhp->uh_next != NULL && uhp->uh_alt_prev == NULL
2260 && uhp->uh_next->uh_walk != nomark
2261 && uhp->uh_next->uh_walk != mark)
2262 {
2263 uhp = uhp->uh_next;
2264 --changes;
2265 }
2266
2267 else
2268 {
2269 /* need to backtrack; mark this node as done */
2270 uhp->uh_walk = nomark;
2271 if (uhp->uh_alt_prev != NULL)
2272 uhp = uhp->uh_alt_prev;
2273 else
2274 {
2275 uhp = uhp->uh_next;
2276 --changes;
2277 }
2278 }
2279 }
2280
2281 if (ga.ga_len == 0)
2282 MSG(_("Nothing to undo"));
2283 else
2284 {
2285 sort_strings((char_u **)ga.ga_data, ga.ga_len);
2286
2287 msg_start();
2288 msg_puts_attr((char_u *)_("number changes time"), hl_attr(HLF_T));
2289 for (i = 0; i < ga.ga_len && !got_int; ++i)
2290 {
2291 msg_putchar('\n');
2292 if (got_int)
2293 break;
2294 msg_puts(((char_u **)ga.ga_data)[i]);
2295 }
2296 msg_end();
2297
2298 ga_clear_strings(&ga);
2299 }
2300}
2301
2302/*
2303 * Put the timestamp of an undo header in "buf[buflen]" in a nice format.
2304 */
2305 static void
2306u_add_time(buf, buflen, tt)
2307 char_u *buf;
2308 size_t buflen;
2309 time_t tt;
2310{
2311#ifdef HAVE_STRFTIME
2312 struct tm *curtime;
2313
2314 if (time(NULL) - tt >= 100)
2315 {
2316 curtime = localtime(&tt);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00002317 (void)strftime((char *)buf, buflen, "%H:%M:%S", curtime);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002318 }
2319 else
2320#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002321 vim_snprintf((char *)buf, buflen, _("%ld seconds ago"),
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002322 (long)(time(NULL) - tt));
2323}
2324
2325/*
Bram Moolenaare224ffa2006-03-01 00:01:28 +00002326 * ":undojoin": continue adding to the last entry list
2327 */
Bram Moolenaare224ffa2006-03-01 00:01:28 +00002328 void
2329ex_undojoin(eap)
Bram Moolenaarfff2bee2010-05-15 13:56:02 +02002330 exarg_T *eap UNUSED;
Bram Moolenaare224ffa2006-03-01 00:01:28 +00002331{
Bram Moolenaare224ffa2006-03-01 00:01:28 +00002332 if (curbuf->b_u_newhead == NULL)
2333 return; /* nothing changed before */
Bram Moolenaar57657d82006-04-21 22:12:41 +00002334 if (curbuf->b_u_curhead != NULL)
2335 {
2336 EMSG(_("E790: undojoin is not allowed after undo"));
2337 return;
2338 }
2339 if (!curbuf->b_u_synced)
2340 return; /* already unsynced */
Bram Moolenaare224ffa2006-03-01 00:01:28 +00002341 if (p_ul < 0)
2342 return; /* no entries, nothing to do */
2343 else
2344 {
2345 /* Go back to the last entry */
2346 curbuf->b_u_curhead = curbuf->b_u_newhead;
2347 curbuf->b_u_synced = FALSE; /* no entries, nothing to do */
2348 }
2349}
2350
2351/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 * Called after writing the file and setting b_changed to FALSE.
2353 * Now an undo means that the buffer is modified.
2354 */
2355 void
2356u_unchanged(buf)
2357 buf_T *buf;
2358{
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002359 u_unch_branch(buf->b_u_oldhead);
2360 buf->b_did_warn = FALSE;
2361}
2362
2363 static void
2364u_unch_branch(uhp)
2365 u_header_T *uhp;
2366{
Bram Moolenaar1e607892006-03-13 22:15:53 +00002367 u_header_T *uh;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002369 for (uh = uhp; uh != NULL; uh = uh->uh_prev)
2370 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 uh->uh_flags |= UH_CHANGED;
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002372 if (uh->uh_alt_next != NULL)
2373 u_unch_branch(uh->uh_alt_next); /* recursive */
2374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375}
2376
2377/*
2378 * Get pointer to last added entry.
2379 * If it's not valid, give an error message and return NULL.
2380 */
2381 static u_entry_T *
2382u_get_headentry()
2383{
2384 if (curbuf->b_u_newhead == NULL || curbuf->b_u_newhead->uh_entry == NULL)
2385 {
2386 EMSG(_("E439: undo list corrupt"));
2387 return NULL;
2388 }
2389 return curbuf->b_u_newhead->uh_entry;
2390}
2391
2392/*
2393 * u_getbot(): compute the line number of the previous u_save
2394 * It is called only when b_u_synced is FALSE.
2395 */
2396 static void
2397u_getbot()
2398{
2399 u_entry_T *uep;
2400 linenr_T extra;
2401
2402 uep = u_get_headentry(); /* check for corrupt undo list */
2403 if (uep == NULL)
2404 return;
2405
2406 uep = curbuf->b_u_newhead->uh_getbot_entry;
2407 if (uep != NULL)
2408 {
2409 /*
2410 * the new ue_bot is computed from the number of lines that has been
2411 * inserted (0 - deleted) since calling u_save. This is equal to the
2412 * old line count subtracted from the current line count.
2413 */
2414 extra = curbuf->b_ml.ml_line_count - uep->ue_lcount;
2415 uep->ue_bot = uep->ue_top + uep->ue_size + 1 + extra;
2416 if (uep->ue_bot < 1 || uep->ue_bot > curbuf->b_ml.ml_line_count)
2417 {
2418 EMSG(_("E440: undo line missing"));
2419 uep->ue_bot = uep->ue_top + 1; /* assume all lines deleted, will
2420 * get all the old lines back
2421 * without deleting the current
2422 * ones */
2423 }
2424
2425 curbuf->b_u_newhead->uh_getbot_entry = NULL;
2426 }
2427
2428 curbuf->b_u_synced = TRUE;
2429}
2430
2431/*
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002432 * Free one header "uhp" and its entry list and adjust the pointers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 */
2434 static void
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002435u_freeheader(buf, uhp, uhpp)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002436 buf_T *buf;
Bram Moolenaar1e607892006-03-13 22:15:53 +00002437 u_header_T *uhp;
2438 u_header_T **uhpp; /* if not NULL reset when freeing this header */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439{
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002440 u_header_T *uhap;
2441
Bram Moolenaar1e607892006-03-13 22:15:53 +00002442 /* When there is an alternate redo list free that branch completely,
2443 * because we can never go there. */
2444 if (uhp->uh_alt_next != NULL)
2445 u_freebranch(buf, uhp->uh_alt_next, uhpp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446
Bram Moolenaar1e607892006-03-13 22:15:53 +00002447 if (uhp->uh_alt_prev != NULL)
2448 uhp->uh_alt_prev->uh_alt_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449
Bram Moolenaar1e607892006-03-13 22:15:53 +00002450 /* Update the links in the list to remove the header. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 if (uhp->uh_next == NULL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002452 buf->b_u_oldhead = uhp->uh_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 else
2454 uhp->uh_next->uh_prev = uhp->uh_prev;
2455
2456 if (uhp->uh_prev == NULL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002457 buf->b_u_newhead = uhp->uh_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 else
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002459 for (uhap = uhp->uh_prev; uhap != NULL; uhap = uhap->uh_alt_next)
2460 uhap->uh_next = uhp->uh_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461
Bram Moolenaar1e607892006-03-13 22:15:53 +00002462 u_freeentries(buf, uhp, uhpp);
2463}
2464
2465/*
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002466 * Free an alternate branch and any following alternate branches.
Bram Moolenaar1e607892006-03-13 22:15:53 +00002467 */
2468 static void
2469u_freebranch(buf, uhp, uhpp)
2470 buf_T *buf;
2471 u_header_T *uhp;
2472 u_header_T **uhpp; /* if not NULL reset when freeing this header */
2473{
2474 u_header_T *tofree, *next;
2475
Bram Moolenaar07d06772007-11-10 21:51:15 +00002476 /* If this is the top branch we may need to use u_freeheader() to update
2477 * all the pointers. */
2478 if (uhp == buf->b_u_oldhead)
2479 {
2480 u_freeheader(buf, uhp, uhpp);
2481 return;
2482 }
2483
Bram Moolenaar1e607892006-03-13 22:15:53 +00002484 if (uhp->uh_alt_prev != NULL)
2485 uhp->uh_alt_prev->uh_alt_next = NULL;
2486
2487 next = uhp;
2488 while (next != NULL)
2489 {
2490 tofree = next;
2491 if (tofree->uh_alt_next != NULL)
2492 u_freebranch(buf, tofree->uh_alt_next, uhpp); /* recursive */
2493 next = tofree->uh_prev;
2494 u_freeentries(buf, tofree, uhpp);
2495 }
2496}
2497
2498/*
2499 * Free all the undo entries for one header and the header itself.
2500 * This means that "uhp" is invalid when returning.
2501 */
2502 static void
2503u_freeentries(buf, uhp, uhpp)
2504 buf_T *buf;
2505 u_header_T *uhp;
2506 u_header_T **uhpp; /* if not NULL reset when freeing this header */
2507{
2508 u_entry_T *uep, *nuep;
2509
2510 /* Check for pointers to the header that become invalid now. */
2511 if (buf->b_u_curhead == uhp)
2512 buf->b_u_curhead = NULL;
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002513 if (buf->b_u_newhead == uhp)
2514 buf->b_u_newhead = NULL; /* freeing the newest entry */
Bram Moolenaar1e607892006-03-13 22:15:53 +00002515 if (uhpp != NULL && uhp == *uhpp)
2516 *uhpp = NULL;
2517
2518 for (uep = uhp->uh_entry; uep != NULL; uep = nuep)
2519 {
2520 nuep = uep->ue_next;
2521 u_freeentry(uep, uep->ue_size);
2522 }
2523
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002524#ifdef U_DEBUG
2525 uhp->uh_magic = 0;
2526#endif
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002527 U_FREE_LINE((char_u *)uhp);
2528 --buf->b_u_numhead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529}
2530
2531/*
2532 * free entry 'uep' and 'n' lines in uep->ue_array[]
2533 */
2534 static void
2535u_freeentry(uep, n)
2536 u_entry_T *uep;
2537 long n;
2538{
Bram Moolenaar8d343302005-07-12 22:46:17 +00002539 while (n > 0)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002540 U_FREE_LINE(uep->ue_array[--n]);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002541 U_FREE_LINE((char_u *)uep->ue_array);
Bram Moolenaarfecb6602007-10-01 20:54:15 +00002542#ifdef U_DEBUG
2543 uep->ue_magic = 0;
2544#endif
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002545 U_FREE_LINE((char_u *)uep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546}
2547
2548/*
2549 * invalidate the undo buffer; called when storage has already been released
2550 */
2551 void
2552u_clearall(buf)
2553 buf_T *buf;
2554{
2555 buf->b_u_newhead = buf->b_u_oldhead = buf->b_u_curhead = NULL;
2556 buf->b_u_synced = TRUE;
2557 buf->b_u_numhead = 0;
2558 buf->b_u_line_ptr = NULL;
2559 buf->b_u_line_lnum = 0;
2560}
2561
2562/*
2563 * save the line "lnum" for the "U" command
2564 */
2565 void
2566u_saveline(lnum)
2567 linenr_T lnum;
2568{
2569 if (lnum == curbuf->b_u_line_lnum) /* line is already saved */
2570 return;
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002571 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) /* should never happen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 return;
2573 u_clearline();
2574 curbuf->b_u_line_lnum = lnum;
2575 if (curwin->w_cursor.lnum == lnum)
2576 curbuf->b_u_line_colnr = curwin->w_cursor.col;
2577 else
2578 curbuf->b_u_line_colnr = 0;
2579 if ((curbuf->b_u_line_ptr = u_save_line(lnum)) == NULL)
2580 do_outofmem_msg((long_u)0);
2581}
2582
2583/*
2584 * clear the line saved for the "U" command
2585 * (this is used externally for crossing a line while in insert mode)
2586 */
2587 void
2588u_clearline()
2589{
2590 if (curbuf->b_u_line_ptr != NULL)
2591 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002592 U_FREE_LINE(curbuf->b_u_line_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 curbuf->b_u_line_ptr = NULL;
2594 curbuf->b_u_line_lnum = 0;
2595 }
2596}
2597
2598/*
2599 * Implementation of the "U" command.
2600 * Differentiation from vi: "U" can be undone with the next "U".
2601 * We also allow the cursor to be in another line.
2602 */
2603 void
2604u_undoline()
2605{
2606 colnr_T t;
2607 char_u *oldp;
2608
2609 if (undo_off)
2610 return;
2611
Bram Moolenaare3300c82008-02-13 14:21:38 +00002612 if (curbuf->b_u_line_ptr == NULL
2613 || curbuf->b_u_line_lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 {
2615 beep_flush();
2616 return;
2617 }
Bram Moolenaare3300c82008-02-13 14:21:38 +00002618
2619 /* first save the line for the 'u' command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 if (u_savecommon(curbuf->b_u_line_lnum - 1,
2621 curbuf->b_u_line_lnum + 1, (linenr_T)0) == FAIL)
2622 return;
2623 oldp = u_save_line(curbuf->b_u_line_lnum);
2624 if (oldp == NULL)
2625 {
2626 do_outofmem_msg((long_u)0);
2627 return;
2628 }
2629 ml_replace(curbuf->b_u_line_lnum, curbuf->b_u_line_ptr, TRUE);
2630 changed_bytes(curbuf->b_u_line_lnum, 0);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002631 U_FREE_LINE(curbuf->b_u_line_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 curbuf->b_u_line_ptr = oldp;
2633
2634 t = curbuf->b_u_line_colnr;
2635 if (curwin->w_cursor.lnum == curbuf->b_u_line_lnum)
2636 curbuf->b_u_line_colnr = curwin->w_cursor.col;
2637 curwin->w_cursor.col = t;
2638 curwin->w_cursor.lnum = curbuf->b_u_line_lnum;
Bram Moolenaare3300c82008-02-13 14:21:38 +00002639 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640}
2641
2642/*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002643 * There are two implementations of the memory management for undo:
2644 * 1. Use the standard malloc()/free() functions.
2645 * This should be fast for allocating memory, but when a buffer is
2646 * abandoned every single allocated chunk must be freed, which may be slow.
2647 * 2. Allocate larger blocks of memory and keep track of chunks ourselves.
2648 * This is fast for abandoning, but the use of linked lists is slow for
2649 * finding a free chunk. Esp. when a lot of lines are changed or deleted.
2650 * A bit of profiling showed that the first method is faster, especially when
2651 * making a large number of changes, under the condition that malloc()/free()
2652 * is implemented efficiently.
2653 */
2654#ifdef U_USE_MALLOC
2655/*
2656 * Version of undo memory allocation using malloc()/free()
2657 *
2658 * U_FREE_LINE() and U_ALLOC_LINE() are macros that invoke vim_free() and
2659 * lalloc() directly.
2660 */
2661
2662/*
2663 * Free all allocated memory blocks for the buffer 'buf'.
2664 */
2665 void
2666u_blockfree(buf)
2667 buf_T *buf;
2668{
Bram Moolenaar1e607892006-03-13 22:15:53 +00002669 while (buf->b_u_oldhead != NULL)
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00002670 u_freeheader(buf, buf->b_u_oldhead, NULL);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002671 U_FREE_LINE(buf->b_u_line_ptr);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002672}
2673
2674#else
2675/*
2676 * Storage allocation for the undo lines and blocks of the current file.
2677 * Version where Vim keeps track of the available memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 */
2679
2680/*
2681 * Memory is allocated in relatively large blocks. These blocks are linked
2682 * in the allocated block list, headed by curbuf->b_block_head. They are all
2683 * freed when abandoning a file, so we don't have to free every single line.
2684 * The list is kept sorted on memory address.
2685 * block_alloc() allocates a block.
2686 * m_blockfree() frees all blocks.
2687 *
2688 * The available chunks of memory are kept in free chunk lists. There is
2689 * one free list for each block of allocated memory. The list is kept sorted
2690 * on memory address.
2691 * u_alloc_line() gets a chunk from the free lists.
2692 * u_free_line() returns a chunk to the free lists.
2693 * curbuf->b_m_search points to the chunk before the chunk that was
2694 * freed/allocated the last time.
2695 * curbuf->b_mb_current points to the b_head where curbuf->b_m_search
2696 * points into the free list.
2697 *
2698 *
2699 * b_block_head /---> block #1 /---> block #2
2700 * mb_next ---/ mb_next ---/ mb_next ---> NULL
2701 * mb_info mb_info mb_info
2702 * | | |
2703 * V V V
2704 * NULL free chunk #1.1 free chunk #2.1
2705 * | |
2706 * V V
2707 * free chunk #1.2 NULL
2708 * |
2709 * V
2710 * NULL
2711 *
2712 * When a single free chunk list would have been used, it could take a lot
2713 * of time in u_free_line() to find the correct place to insert a chunk in the
2714 * free list. The single free list would become very long when many lines are
2715 * changed (e.g. with :%s/^M$//).
2716 */
2717
2718 /*
2719 * this blocksize is used when allocating new lines
2720 */
2721#define MEMBLOCKSIZE 2044
2722
2723/*
2724 * The size field contains the size of the chunk, including the size field
2725 * itself.
2726 *
2727 * When the chunk is not in-use it is preceded with the m_info structure.
2728 * The m_next field links it in one of the free chunk lists.
2729 *
2730 * On most unix systems structures have to be longword (32 or 64 bit) aligned.
2731 * On most other systems they are short (16 bit) aligned.
2732 */
2733
2734/* the structure definitions are now in structs.h */
2735
2736#ifdef ALIGN_LONG
2737 /* size of m_size */
2738# define M_OFFSET (sizeof(long_u))
2739#else
2740 /* size of m_size */
2741# define M_OFFSET (sizeof(short_u))
2742#endif
2743
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002744static char_u *u_blockalloc __ARGS((long_u));
2745
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746/*
2747 * Allocate a block of memory and link it in the allocated block list.
2748 */
2749 static char_u *
2750u_blockalloc(size)
2751 long_u size;
2752{
2753 mblock_T *p;
2754 mblock_T *mp, *next;
2755
2756 p = (mblock_T *)lalloc(size + sizeof(mblock_T), FALSE);
2757 if (p != NULL)
2758 {
2759 /* Insert the block into the allocated block list, keeping it
2760 sorted on address. */
2761 for (mp = &curbuf->b_block_head;
2762 (next = mp->mb_next) != NULL && next < p;
2763 mp = next)
2764 ;
2765 p->mb_next = next; /* link in block list */
2766 p->mb_size = size;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002767 p->mb_maxsize = 0; /* nothing free yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 mp->mb_next = p;
2769 p->mb_info.m_next = NULL; /* clear free list */
2770 p->mb_info.m_size = 0;
2771 curbuf->b_mb_current = p; /* remember current block */
2772 curbuf->b_m_search = NULL;
2773 p++; /* return usable memory */
2774 }
2775 return (char_u *)p;
2776}
2777
2778/*
2779 * free all allocated memory blocks for the buffer 'buf'
2780 */
2781 void
2782u_blockfree(buf)
2783 buf_T *buf;
2784{
2785 mblock_T *p, *np;
2786
2787 for (p = buf->b_block_head.mb_next; p != NULL; p = np)
2788 {
2789 np = p->mb_next;
2790 vim_free(p);
2791 }
2792 buf->b_block_head.mb_next = NULL;
2793 buf->b_m_search = NULL;
2794 buf->b_mb_current = NULL;
2795}
2796
2797/*
2798 * Free a chunk of memory for the current buffer.
2799 * Insert the chunk into the correct free list, keeping it sorted on address.
2800 */
2801 static void
2802u_free_line(ptr, keep)
2803 char_u *ptr;
2804 int keep; /* don't free the block when it's empty */
2805{
2806 minfo_T *next;
2807 minfo_T *prev, *curr;
2808 minfo_T *mp;
2809 mblock_T *nextb;
2810 mblock_T *prevb;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002811 long_u maxsize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812
2813 if (ptr == NULL || ptr == IObuff)
2814 return; /* illegal address can happen in out-of-memory situations */
2815
2816 mp = (minfo_T *)(ptr - M_OFFSET);
2817
2818 /* find block where chunk could be a part off */
2819 /* if we change curbuf->b_mb_current, curbuf->b_m_search is set to NULL */
2820 if (curbuf->b_mb_current == NULL || mp < (minfo_T *)curbuf->b_mb_current)
2821 {
2822 curbuf->b_mb_current = curbuf->b_block_head.mb_next;
2823 curbuf->b_m_search = NULL;
2824 }
2825 if ((nextb = curbuf->b_mb_current->mb_next) != NULL
2826 && (minfo_T *)nextb < mp)
2827 {
2828 curbuf->b_mb_current = nextb;
2829 curbuf->b_m_search = NULL;
2830 }
2831 while ((nextb = curbuf->b_mb_current->mb_next) != NULL
2832 && (minfo_T *)nextb < mp)
2833 curbuf->b_mb_current = nextb;
2834
2835 curr = NULL;
2836 /*
2837 * If mp is smaller than curbuf->b_m_search->m_next go to the start of
2838 * the free list
2839 */
2840 if (curbuf->b_m_search == NULL || mp < (curbuf->b_m_search->m_next))
2841 next = &(curbuf->b_mb_current->mb_info);
2842 else
2843 next = curbuf->b_m_search;
2844 /*
2845 * The following loop is executed very often.
2846 * Therefore it has been optimized at the cost of readability.
2847 * Keep it fast!
2848 */
2849#ifdef SLOW_BUT_EASY_TO_READ
2850 do
2851 {
2852 prev = curr;
2853 curr = next;
2854 next = next->m_next;
2855 }
2856 while (mp > next && next != NULL);
2857#else
2858 do /* first, middle, last */
2859 {
2860 prev = next->m_next; /* curr, next, prev */
2861 if (prev == NULL || mp <= prev)
2862 {
2863 prev = curr;
2864 curr = next;
2865 next = next->m_next;
2866 break;
2867 }
2868 curr = prev->m_next; /* next, prev, curr */
2869 if (curr == NULL || mp <= curr)
2870 {
2871 prev = next;
2872 curr = prev->m_next;
2873 next = curr->m_next;
2874 break;
2875 }
2876 next = curr->m_next; /* prev, curr, next */
2877 }
2878 while (mp > next && next != NULL);
2879#endif
2880
2881 /* if *mp and *next are concatenated, join them into one chunk */
2882 if ((char_u *)mp + mp->m_size == (char_u *)next)
2883 {
2884 mp->m_size += next->m_size;
2885 mp->m_next = next->m_next;
2886 }
2887 else
2888 mp->m_next = next;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002889 maxsize = mp->m_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890
2891 /* if *curr and *mp are concatenated, join them */
2892 if (prev != NULL && (char_u *)curr + curr->m_size == (char_u *)mp)
2893 {
2894 curr->m_size += mp->m_size;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002895 maxsize = curr->m_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 curr->m_next = mp->m_next;
2897 curbuf->b_m_search = prev;
2898 }
2899 else
2900 {
2901 curr->m_next = mp;
2902 curbuf->b_m_search = curr; /* put curbuf->b_m_search before freed
2903 chunk */
2904 }
2905
2906 /*
Bram Moolenaar035db9f2007-05-10 18:02:27 +00002907 * If the block only contains free memory now, release it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 */
2909 if (!keep && curbuf->b_mb_current->mb_size
2910 == curbuf->b_mb_current->mb_info.m_next->m_size)
2911 {
2912 /* Find the block before the current one to be able to unlink it from
2913 * the list of blocks. */
2914 prevb = &curbuf->b_block_head;
2915 for (nextb = prevb->mb_next; nextb != curbuf->b_mb_current;
2916 nextb = nextb->mb_next)
2917 prevb = nextb;
2918 prevb->mb_next = nextb->mb_next;
2919 vim_free(nextb);
2920 curbuf->b_mb_current = NULL;
2921 curbuf->b_m_search = NULL;
2922 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002923 else if (curbuf->b_mb_current->mb_maxsize < maxsize)
2924 curbuf->b_mb_current->mb_maxsize = maxsize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925}
2926
2927/*
2928 * Allocate and initialize a new line structure with room for at least
2929 * 'size' characters plus a terminating NUL.
2930 */
2931 static char_u *
2932u_alloc_line(size)
2933 unsigned size;
2934{
2935 minfo_T *mp, *mprev, *mp2;
2936 mblock_T *mbp;
2937 int size_align;
2938
2939 /*
2940 * Add room for size field and trailing NUL byte.
2941 * Adjust for minimal size (must be able to store minfo_T
2942 * plus a trailing NUL, so the chunk can be released again)
2943 */
2944 size += M_OFFSET + 1;
2945 if (size < sizeof(minfo_T) + 1)
2946 size = sizeof(minfo_T) + 1;
2947
2948 /*
2949 * round size up for alignment
2950 */
2951 size_align = (size + ALIGN_MASK) & ~ALIGN_MASK;
2952
2953 /*
2954 * If curbuf->b_m_search is NULL (uninitialized free list) start at
2955 * curbuf->b_block_head
2956 */
2957 if (curbuf->b_mb_current == NULL || curbuf->b_m_search == NULL)
2958 {
2959 curbuf->b_mb_current = &curbuf->b_block_head;
2960 curbuf->b_m_search = &(curbuf->b_block_head.mb_info);
2961 }
2962
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002963 /* Search for a block with enough space. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 mbp = curbuf->b_mb_current;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002965 while (mbp->mb_maxsize < size_align)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002967 if (mbp->mb_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 mbp = mbp->mb_next;
2969 else
2970 mbp = &curbuf->b_block_head;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002971 if (mbp == curbuf->b_mb_current)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002973 int n = (size_align > (MEMBLOCKSIZE / 4)
2974 ? size_align : MEMBLOCKSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002976 /* Back where we started in block list: need to add a new block
2977 * with enough space. */
2978 mp = (minfo_T *)u_blockalloc((long_u)n);
2979 if (mp == NULL)
2980 return (NULL);
2981 mp->m_size = n;
2982 u_free_line((char_u *)mp + M_OFFSET, TRUE);
2983 mbp = curbuf->b_mb_current;
2984 break;
2985 }
2986 }
2987 if (mbp != curbuf->b_mb_current)
2988 curbuf->b_m_search = &(mbp->mb_info);
2989
2990 /* In this block find a chunk with enough space. */
2991 mprev = curbuf->b_m_search;
2992 mp = curbuf->b_m_search->m_next;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00002993 for (;;)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002994 {
2995 if (mp == NULL) /* at end of the list */
2996 mp = &(mbp->mb_info); /* wrap around to begin */
2997 if (mp->m_size >= size)
2998 break;
2999 if (mp == curbuf->b_m_search)
3000 {
3001 /* back where we started in free chunk list: "cannot happen" */
3002 EMSG2(_(e_intern2), "u_alloc_line()");
3003 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 }
3005 mprev = mp;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003006 mp = mp->m_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 }
3008
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003009 /* when using the largest chunk adjust mb_maxsize */
3010 if (mp->m_size >= mbp->mb_maxsize)
3011 mbp->mb_maxsize = 0;
3012
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 /* if the chunk we found is large enough, split it up in two */
3014 if ((long)mp->m_size - size_align >= (long)(sizeof(minfo_T) + 1))
3015 {
3016 mp2 = (minfo_T *)((char_u *)mp + size_align);
3017 mp2->m_size = mp->m_size - size_align;
3018 mp2->m_next = mp->m_next;
3019 mprev->m_next = mp2;
3020 mp->m_size = size_align;
3021 }
3022 else /* remove *mp from the free list */
3023 {
3024 mprev->m_next = mp->m_next;
3025 }
3026 curbuf->b_m_search = mprev;
3027 curbuf->b_mb_current = mbp;
3028
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003029 /* If using the largest chunk need to find the new largest chunk */
3030 if (mbp->mb_maxsize == 0)
3031 for (mp2 = &(mbp->mb_info); mp2 != NULL; mp2 = mp2->m_next)
3032 if (mbp->mb_maxsize < mp2->m_size)
3033 mbp->mb_maxsize = mp2->m_size;
3034
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 mp = (minfo_T *)((char_u *)mp + M_OFFSET);
3036 *(char_u *)mp = NUL; /* set the first byte to NUL */
3037
3038 return ((char_u *)mp);
3039}
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041
3042/*
3043 * u_save_line(): allocate memory with u_alloc_line() and copy line 'lnum'
3044 * into it.
3045 */
3046 static char_u *
3047u_save_line(lnum)
3048 linenr_T lnum;
3049{
3050 char_u *src;
3051 char_u *dst;
3052 unsigned len;
3053
3054 src = ml_get(lnum);
3055 len = (unsigned)STRLEN(src);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003056 if ((dst = U_ALLOC_LINE(len)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 mch_memmove(dst, src, (size_t)(len + 1));
3058 return (dst);
3059}
3060
3061/*
3062 * Check if the 'modified' flag is set, or 'ff' has changed (only need to
3063 * check the first character, because it can only be "dos", "unix" or "mac").
3064 * "nofile" and "scratch" type buffers are considered to always be unchanged.
3065 */
3066 int
3067bufIsChanged(buf)
3068 buf_T *buf;
3069{
3070 return
3071#ifdef FEAT_QUICKFIX
3072 !bt_dontwrite(buf) &&
3073#endif
3074 (buf->b_changed || file_ff_differs(buf));
3075}
3076
3077 int
3078curbufIsChanged()
3079{
3080 return
3081#ifdef FEAT_QUICKFIX
3082 !bt_dontwrite(curbuf) &&
3083#endif
3084 (curbuf->b_changed || file_ff_differs(curbuf));
3085}