Mercurial > vim
annotate src/undo.c @ 2215:cccb71c2c5c1 vim73
Fix uninit memory read in undo code. Fix uint32_t in proto file.
A few minor changes.
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Mon, 24 May 2010 11:59:29 +0200 |
parents | f8222d1f9a73 |
children | 120502692d82 |
rev | line source |
---|---|
7 | 1 /* 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), | |
758 | 42 * or is NULL if nothing has been undone (end of the branch). |
7 | 43 * |
753 | 44 * 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 * | |
758 | 49 * +---------------+ +---------------+ |
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 * | |
168 | 75 * All data is allocated with U_ALLOC_LINE(), it will be freed as soon as the |
76 * buffer is unloaded. | |
7 | 77 */ |
78 | |
1415 | 79 /* 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 | |
7 | 85 #include "vim.h" |
86 | |
168 | 87 /* See below: use malloc()/free() for memory management. */ |
88 #define U_USE_MALLOC 1 | |
89 | |
758 | 90 static void u_unch_branch __ARGS((u_header_T *uhp)); |
7 | 91 static u_entry_T *u_get_headentry __ARGS((void)); |
92 static void u_getbot __ARGS((void)); | |
93 static int u_savecommon __ARGS((linenr_T, linenr_T, linenr_T)); | |
94 static void u_doit __ARGS((int count)); | |
777 | 95 static void u_undoredo __ARGS((int undo)); |
798 | 96 static void u_undo_end __ARGS((int did_undo, int absolute)); |
772 | 97 static void u_add_time __ARGS((char_u *buf, size_t buflen, time_t tt)); |
758 | 98 static void u_freeheader __ARGS((buf_T *buf, u_header_T *uhp, u_header_T **uhpp)); |
753 | 99 static void u_freebranch __ARGS((buf_T *buf, u_header_T *uhp, u_header_T **uhpp)); |
100 static void u_freeentries __ARGS((buf_T *buf, u_header_T *uhp, u_header_T **uhpp)); | |
7 | 101 static void u_freeentry __ARGS((u_entry_T *, long)); |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
102 #ifdef FEAT_PERSISTENT_UNDO |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
103 static void unserialize_pos __ARGS((pos_T *pos, FILE *fp)); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
104 static void unserialize_visualinfo __ARGS((visualinfo_T *info, FILE *fp)); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
105 static char_u *u_get_undo_file_name __ARGS((char_u *, int reading)); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
106 static int serialize_uep __ARGS((u_entry_T *uep, FILE *fp)); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
107 static void serialize_pos __ARGS((pos_T pos, FILE *fp)); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
108 static void serialize_visualinfo __ARGS((visualinfo_T info, FILE *fp)); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
109 #endif |
7 | 110 |
168 | 111 #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 | |
115 static void u_free_line __ARGS((char_u *ptr, int keep)); | |
116 static 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 | |
7 | 120 static char_u *u_save_line __ARGS((linenr_T)); |
121 | |
122 static 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 */ | |
128 static int undo_undoes = FALSE; | |
129 | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
130 static int lastmark = 0; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
131 |
1415 | 132 #ifdef U_DEBUG |
133 /* | |
134 * Check the undo structures for being valid. Print a warning when something | |
135 * looks wrong. | |
136 */ | |
137 static int seen_b_u_curhead; | |
138 static int seen_b_u_newhead; | |
139 static int header_count; | |
140 | |
141 static void | |
142 u_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 | |
199 u_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 | |
7 | 221 /* |
344 | 222 * Save the current line for both the "u" and "U" command. |
223 * Returns OK or FAIL. | |
7 | 224 */ |
225 int | |
226 u_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 | |
238 u_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 /* | |
2154
7c8c7c95a865
First step in the Vim 7.3 branch. Changed version numbers.
Bram Moolenaar <bram@zimbu.org>
parents:
1534
diff
changeset
|
255 * Save the line "lnum" (used by ":s" and "~" command). |
7 | 256 * The line is replaced, so the new bottom line is lnum + 1. |
257 */ | |
258 int | |
259 u_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 /* | |
2154
7c8c7c95a865
First step in the Vim 7.3 branch. Changed version numbers.
Bram Moolenaar <bram@zimbu.org>
parents:
1534
diff
changeset
|
269 * A new line is inserted before line "lnum" (used by :s command). |
7 | 270 * The line is inserted, so the new bottom line is lnum + 1. |
271 */ | |
272 int | |
273 u_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 /* | |
2154
7c8c7c95a865
First step in the Vim 7.3 branch. Changed version numbers.
Bram Moolenaar <bram@zimbu.org>
parents:
1534
diff
changeset
|
283 * Save the lines "lnum" - "lnum" + nlines (used by delete command). |
7 | 284 * The lines are deleted, so the new bottom line is lnum, unless the buffer |
285 * becomes empty. | |
286 */ | |
287 int | |
288 u_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 | |
632 | 299 /* |
300 * Return TRUE when undo is allowed. Otherwise give an error message and | |
301 * return FALSE. | |
302 */ | |
912 | 303 int |
632 | 304 undo_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. */ | |
634 | 324 if (textlock != 0) |
632 | 325 { |
326 EMSG(_(e_secure)); | |
327 return FALSE; | |
328 } | |
329 | |
330 return TRUE; | |
331 } | |
332 | |
7 | 333 static int |
334 u_savecommon(top, bot, newbot) | |
335 linenr_T top, bot; | |
336 linenr_T newbot; | |
337 { | |
753 | 338 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; | |
7 | 345 |
632 | 346 /* When making changes is not allowed return FAIL. It's a crude way to |
347 * make all change commands fail. */ | |
348 if (!undo_allowed()) | |
7 | 349 return FAIL; |
350 | |
1415 | 351 #ifdef U_DEBUG |
352 u_check(FALSE); | |
353 #endif | |
7 | 354 #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 */ | |
2210 | 359 if (netbeans_active()) |
7 | 360 { |
33 | 361 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 } | |
7 | 371 } |
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 | |
753 | 396 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; | |
1415 | 405 #ifdef U_DEBUG |
406 uhp->uh_magic = UH_MAGIC; | |
407 #endif | |
753 | 408 } |
766 | 409 else |
410 uhp = NULL; | |
753 | 411 |
7 | 412 /* |
758 | 413 * If we undid more than we redid, move the entry lists before and |
414 * including curbuf->b_u_curhead to an alternate branch. | |
7 | 415 */ |
753 | 416 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 } | |
7 | 422 |
423 /* | |
424 * free headers to keep the size right | |
425 */ | |
426 while (curbuf->b_u_numhead > p_ul && curbuf->b_u_oldhead != NULL) | |
753 | 427 { |
758 | 428 u_header_T *uhfree = curbuf->b_u_oldhead; |
753 | 429 |
1415 | 430 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. */ | |
758 | 435 u_freeheader(curbuf, uhfree, &old_curhead); |
753 | 436 else |
437 { | |
438 /* Free the oldest alternate branch as a whole. */ | |
758 | 439 while (uhfree->uh_alt_next != NULL) |
440 uhfree = uhfree->uh_alt_next; | |
441 u_freebranch(curbuf, uhfree, &old_curhead); | |
753 | 442 } |
1415 | 443 #ifdef U_DEBUG |
444 u_check(TRUE); | |
445 #endif | |
753 | 446 } |
7 | 447 |
766 | 448 if (uhp == NULL) /* no undo at all */ |
7 | 449 { |
753 | 450 if (old_curhead != NULL) |
451 u_freebranch(curbuf, old_curhead, NULL); | |
7 | 452 curbuf->b_u_synced = FALSE; |
453 return OK; | |
454 } | |
455 | |
456 uhp->uh_prev = NULL; | |
457 uhp->uh_next = curbuf->b_u_newhead; | |
753 | 458 uhp->uh_alt_next = old_curhead; |
459 if (old_curhead != NULL) | |
460 { | |
1056 | 461 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; | |
753 | 464 old_curhead->uh_alt_prev = uhp; |
465 if (curbuf->b_u_oldhead == old_curhead) | |
466 curbuf->b_u_oldhead = uhp; | |
467 } | |
1056 | 468 else |
469 uhp->uh_alt_prev = NULL; | |
758 | 470 if (curbuf->b_u_newhead != NULL) |
471 curbuf->b_u_newhead->uh_prev = uhp; | |
472 | |
777 | 473 uhp->uh_seq = ++curbuf->b_u_seq_last; |
474 curbuf->b_u_seq_cur = uhp->uh_seq; | |
758 | 475 uhp->uh_time = time(NULL); |
476 curbuf->b_u_seq_time = uhp->uh_time + 1; | |
477 | |
753 | 478 uhp->uh_walk = 0; |
7 | 479 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 | |
692 | 493 /* save named marks and Visual marks for undo */ |
7 | 494 mch_memmove(uhp->uh_namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS); |
692 | 495 #ifdef FEAT_VISUAL |
496 uhp->uh_visual = curbuf->b_visual; | |
497 #endif | |
498 | |
7 | 499 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 */ | |
168 | 595 uep = (u_entry_T *)U_ALLOC_LINE((unsigned)sizeof(u_entry_T)); |
7 | 596 if (uep == NULL) |
597 goto nomem; | |
2215
cccb71c2c5c1
Fix uninit memory read in undo code. Fix uint32_t in proto file.
Bram Moolenaar <bram@vim.org>
parents:
2214
diff
changeset
|
598 vim_memset(uep, 0, sizeof(u_entry_T)); |
1415 | 599 #ifdef U_DEBUG |
600 uep->ue_magic = UE_MAGIC; | |
601 #endif | |
7 | 602 |
603 uep->ue_size = size; | |
604 uep->ue_top = top; | |
605 if (newbot != 0) | |
606 uep->ue_bot = newbot; | |
607 /* | |
608 * Use 0 for ue_bot if bot is below last line. | |
609 * Otherwise we have to compute ue_bot later. | |
610 */ | |
611 else if (bot > curbuf->b_ml.ml_line_count) | |
612 uep->ue_bot = 0; | |
613 else | |
614 { | |
615 uep->ue_lcount = curbuf->b_ml.ml_line_count; | |
616 curbuf->b_u_newhead->uh_getbot_entry = uep; | |
617 } | |
618 | |
168 | 619 if (size > 0) |
7 | 620 { |
168 | 621 if ((uep->ue_array = (char_u **)U_ALLOC_LINE( |
7 | 622 (unsigned)(sizeof(char_u *) * size))) == NULL) |
623 { | |
624 u_freeentry(uep, 0L); | |
625 goto nomem; | |
626 } | |
627 for (i = 0, lnum = top + 1; i < size; ++i) | |
628 { | |
481 | 629 fast_breakcheck(); |
630 if (got_int) | |
631 { | |
632 u_freeentry(uep, i); | |
633 return FAIL; | |
634 } | |
7 | 635 if ((uep->ue_array[i] = u_save_line(lnum++)) == NULL) |
636 { | |
637 u_freeentry(uep, i); | |
638 goto nomem; | |
639 } | |
640 } | |
641 } | |
359 | 642 else |
643 uep->ue_array = NULL; | |
7 | 644 uep->ue_next = curbuf->b_u_newhead->uh_entry; |
645 curbuf->b_u_newhead->uh_entry = uep; | |
646 curbuf->b_u_synced = FALSE; | |
647 undo_undoes = FALSE; | |
648 | |
1415 | 649 #ifdef U_DEBUG |
650 u_check(FALSE); | |
651 #endif | |
7 | 652 return OK; |
653 | |
654 nomem: | |
655 msg_silent = 0; /* must display the prompt */ | |
656 if (ask_yesno((char_u *)_("No undo possible; continue anyway"), TRUE) | |
657 == 'y') | |
658 { | |
659 undo_off = TRUE; /* will be reset when character typed */ | |
660 return OK; | |
661 } | |
662 do_outofmem_msg((long_u)0); | |
663 return FAIL; | |
664 } | |
665 | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
666 #ifdef FEAT_PERSISTENT_UNDO |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
667 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
668 # define UF_START_MAGIC 0xfeac /* magic at start of undofile */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
669 # define UF_HEADER_MAGIC 0x5fd0 /* magic at start of header */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
670 # define UF_END_MAGIC 0xe7aa /* magic after last header */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
671 # define UF_VERSION 1 /* 2-byte undofile version number */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
672 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
673 /* |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
674 * Compute the hash for the current buffer text into hash[UNDO_HASH_SIZE]. |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
675 */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
676 void |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
677 u_compute_hash(hash) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
678 char_u *hash; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
679 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
680 context_sha256_T ctx; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
681 linenr_T lnum; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
682 char_u *p; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
683 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
684 sha256_start(&ctx); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
685 for (lnum = 1; lnum < curbuf->b_ml.ml_line_count; ++lnum) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
686 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
687 p = ml_get(lnum); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
688 sha256_update(&ctx, p, STRLEN(p) + 1); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
689 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
690 sha256_finish(&ctx, hash); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
691 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
692 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
693 /* |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
694 * Unserialize the pos_T at the current position in fp. |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
695 */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
696 static void |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
697 unserialize_pos(pos, fp) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
698 pos_T *pos; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
699 FILE *fp; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
700 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
701 pos->lnum = get4c(fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
702 pos->col = get4c(fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
703 #ifdef FEAT_VIRTUALEDIT |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
704 pos->coladd = get4c(fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
705 #else |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
706 (void)get4c(fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
707 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
708 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
709 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
710 /* |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
711 * Unserialize the visualinfo_T at the current position in fp. |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
712 */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
713 static void |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
714 unserialize_visualinfo(info, fp) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
715 visualinfo_T *info; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
716 FILE *fp; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
717 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
718 unserialize_pos(&info->vi_start, fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
719 unserialize_pos(&info->vi_end, fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
720 info->vi_mode = get4c(fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
721 info->vi_curswant = get4c(fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
722 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
723 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
724 /* |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
725 * Return an allocated string of the full path of the target undofile. |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
726 * When "reading" is TRUE find the file to read, go over all directories in |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
727 * 'undodir'. |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
728 * When "reading" is FALSE use the first name where the directory exists. |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
729 */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
730 static char_u * |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
731 u_get_undo_file_name(buf_ffname, reading) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
732 char_u *buf_ffname; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
733 int reading; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
734 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
735 char_u *dirp; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
736 char_u dir_name[IOSIZE + 1]; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
737 char_u *munged_name = NULL; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
738 char_u *undo_file_name = NULL; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
739 int dir_len; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
740 char_u *p; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
741 struct stat st; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
742 char_u *ffname = buf_ffname; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
743 #ifdef HAVE_READLINK |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
744 char_u fname_buf[MAXPATHL]; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
745 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
746 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
747 if (ffname == NULL) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
748 return NULL; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
749 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
750 #ifdef HAVE_READLINK |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
751 /* Expand symlink in the file name, so that we put the undo file with the |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
752 * actual file instead of with the symlink. */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
753 if (resolve_symlink(ffname, fname_buf) == OK) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
754 ffname = fname_buf; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
755 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
756 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
757 /* Loop over 'undodir'. When reading find the first file that exists. |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
758 * When not reading use the first directory that exists or ".". */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
759 dirp = p_udir; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
760 while (*dirp != NUL) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
761 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
762 dir_len = copy_option_part(&dirp, dir_name, IOSIZE, ","); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
763 if (dir_len == 1 && dir_name[0] == '.') |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
764 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
765 /* Use same directory as the ffname, |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
766 * "dir/name" -> "dir/.name.un~" */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
767 undo_file_name = vim_strnsave(ffname, STRLEN(ffname) + 5); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
768 if (undo_file_name == NULL) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
769 break; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
770 p = gettail(undo_file_name); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
771 mch_memmove(p + 1, p, STRLEN(p) + 1); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
772 *p = '.'; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
773 STRCAT(p, ".un~"); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
774 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
775 else |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
776 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
777 dir_name[dir_len] = NUL; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
778 if (mch_isdir(dir_name)) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
779 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
780 if (munged_name == NULL) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
781 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
782 munged_name = vim_strsave(ffname); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
783 if (munged_name == NULL) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
784 return NULL; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
785 for (p = munged_name; *p != NUL; mb_ptr_adv(p)) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
786 if (vim_ispathsep(*p)) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
787 *p = '%'; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
788 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
789 undo_file_name = concat_fnames(dir_name, munged_name, TRUE); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
790 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
791 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
792 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
793 /* When reading check if the file exists. */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
794 if (undo_file_name != NULL && (!reading |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
795 || mch_stat((char *)undo_file_name, &st) >= 0)) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
796 break; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
797 vim_free(undo_file_name); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
798 undo_file_name = NULL; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
799 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
800 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
801 vim_free(munged_name); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
802 return undo_file_name; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
803 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
804 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
805 /* |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
806 * Load the undo tree from an undo file. |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
807 * If "name" is not NULL use it as the undo file name. This also means being |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
808 * a bit more verbose. |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
809 * Otherwise use curbuf->b_ffname to generate the undo file name. |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
810 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text. |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
811 */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
812 void |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
813 u_read_undo(name, hash) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
814 char_u *name; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
815 char_u *hash; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
816 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
817 char_u *file_name; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
818 FILE *fp; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
819 long magic, version, str_len; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
820 char_u *line_ptr = NULL; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
821 linenr_T line_lnum; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
822 colnr_T line_colnr; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
823 linenr_T line_count; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
824 int uep_len; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
825 int line_len; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
826 int num_head; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
827 long old_header_seq, new_header_seq, cur_header_seq; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
828 long seq_last, seq_cur; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
829 short old_idx = -1, new_idx = -1, cur_idx = -1; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
830 long num_read_uhps = 0; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
831 time_t seq_time; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
832 int i, j; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
833 int c; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
834 short found_first_uep = 0; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
835 char_u **array; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
836 char_u *line; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
837 u_entry_T *uep, *last_uep, *nuep; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
838 u_header_T *uhp; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
839 u_header_T **uhp_table = NULL; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
840 char_u read_hash[UNDO_HASH_SIZE]; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
841 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
842 if (name == NULL) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
843 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
844 file_name = u_get_undo_file_name(curbuf->b_ffname, TRUE); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
845 if (file_name == NULL) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
846 return; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
847 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
848 else |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
849 file_name = name; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
850 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
851 if (p_verbose > 0) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
852 smsg((char_u *)_("Reading undo file: %s"), file_name); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
853 fp = mch_fopen((char *)file_name, "r"); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
854 if (fp == NULL) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
855 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
856 if (name != NULL || p_verbose > 0) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
857 EMSG2(_("E822: Cannot open undo file for reading: %s"), file_name); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
858 goto error; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
859 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
860 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
861 /* Begin overall file information */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
862 magic = get2c(fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
863 if (magic != UF_START_MAGIC) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
864 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
865 EMSG2(_("E823: Corrupted undo file: %s"), file_name); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
866 goto error; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
867 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
868 version = get2c(fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
869 if (version != UF_VERSION) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
870 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
871 EMSG2(_("E824: Incompatible undo file: %s"), file_name); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
872 goto error; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
873 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
874 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
875 fread(read_hash, UNDO_HASH_SIZE, 1, fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
876 line_count = (linenr_T)get4c(fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
877 if (memcmp(hash, read_hash, UNDO_HASH_SIZE) != 0 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
878 || line_count != curbuf->b_ml.ml_line_count) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
879 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
880 if (p_verbose > 0 || name != NULL) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
881 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
882 verbose_enter(); |
2215
cccb71c2c5c1
Fix uninit memory read in undo code. Fix uint32_t in proto file.
Bram Moolenaar <bram@vim.org>
parents:
2214
diff
changeset
|
883 give_warning((char_u *)_("File contents changed, cannot use undo info"), TRUE); |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
884 verbose_leave(); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
885 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
886 goto error; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
887 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
888 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
889 /* Begin undo data for U */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
890 str_len = get4c(fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
891 if (str_len < 0) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
892 goto error; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
893 else if (str_len > 0) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
894 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
895 if ((line_ptr = U_ALLOC_LINE(str_len)) == NULL) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
896 goto error; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
897 for (i = 0; i < str_len; i++) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
898 line_ptr[i] = (char_u)getc(fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
899 line_ptr[i] = NUL; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
900 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
901 line_lnum = (linenr_T)get4c(fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
902 line_colnr = (colnr_T)get4c(fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
903 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
904 /* Begin general undo data */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
905 old_header_seq = get4c(fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
906 new_header_seq = get4c(fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
907 cur_header_seq = get4c(fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
908 num_head = get4c(fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
909 seq_last = get4c(fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
910 seq_cur = get4c(fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
911 seq_time = get4c(fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
912 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
913 /* uhp_table will store the freshly created undo headers we allocate |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
914 * until we insert them into curbuf. The table remains sorted by the |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
915 * sequence numbers of the headers. */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
916 uhp_table = (u_header_T **)U_ALLOC_LINE(num_head * sizeof(u_header_T *)); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
917 if (uhp_table == NULL) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
918 goto error; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
919 vim_memset(uhp_table, 0, num_head * sizeof(u_header_T *)); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
920 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
921 c = get2c(fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
922 while (c == UF_HEADER_MAGIC) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
923 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
924 found_first_uep = 0; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
925 uhp = (u_header_T *)U_ALLOC_LINE((unsigned)sizeof(u_header_T)); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
926 if (uhp == NULL) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
927 goto error; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
928 vim_memset(uhp, 0, sizeof(u_header_T)); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
929 /* We're not actually trying to store pointers here. We're just storing |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
930 * IDs so we can swizzle them into pointers later - hence the type |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
931 * cast. */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
932 uhp->uh_next = (u_header_T *)(long)get4c(fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
933 uhp->uh_prev = (u_header_T *)(long)get4c(fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
934 uhp->uh_alt_next = (u_header_T *)(long)get4c(fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
935 uhp->uh_alt_prev = (u_header_T *)(long)get4c(fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
936 uhp->uh_seq = get4c(fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
937 if (uhp->uh_seq <= 0) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
938 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
939 EMSG2(_("E825: Undo file corruption: invalid uh_seq.: %s"), |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
940 file_name); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
941 U_FREE_LINE(uhp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
942 goto error; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
943 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
944 uhp->uh_walk = 0; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
945 unserialize_pos(&uhp->uh_cursor, fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
946 #ifdef FEAT_VIRTUALEDIT |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
947 uhp->uh_cursor_vcol = get4c(fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
948 #else |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
949 (void)get4c(fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
950 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
951 uhp->uh_flags = get2c(fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
952 for (i = 0; i < NMARKS; ++i) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
953 unserialize_pos(&uhp->uh_namedm[i], fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
954 #ifdef FEAT_VISUAL |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
955 unserialize_visualinfo(&uhp->uh_visual, fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
956 #else |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
957 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
958 visualinfo_T info; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
959 unserialize_visualinfo(&info, fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
960 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
961 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
962 uhp->uh_time = get4c(fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
963 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
964 /* Unserialize uep list. The first 4 bytes is the length of the |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
965 * entire uep in bytes minus the length of the strings within. |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
966 * -1 is a sentinel value meaning no more ueps.*/ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
967 last_uep = NULL; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
968 while ((uep_len = get4c(fp)) != -1) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
969 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
970 uep = (u_entry_T *)U_ALLOC_LINE((unsigned)sizeof(u_entry_T)); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
971 if (uep == NULL) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
972 goto error; |
2215
cccb71c2c5c1
Fix uninit memory read in undo code. Fix uint32_t in proto file.
Bram Moolenaar <bram@vim.org>
parents:
2214
diff
changeset
|
973 vim_memset(uep, 0, sizeof(u_entry_T)); |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
974 uep->ue_top = get4c(fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
975 uep->ue_bot = get4c(fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
976 uep->ue_lcount = get4c(fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
977 uep->ue_size = get4c(fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
978 uep->ue_next = NULL; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
979 array = (char_u **)U_ALLOC_LINE( |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
980 (unsigned)(sizeof(char_u *) * uep->ue_size)); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
981 for (i = 0; i < uep->ue_size; i++) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
982 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
983 line_len = get4c(fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
984 /* U_ALLOC_LINE provides an extra byte for the NUL terminator.*/ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
985 line = (char_u *)U_ALLOC_LINE( |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
986 (unsigned) (sizeof(char_u) * line_len)); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
987 if (line == NULL) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
988 goto error; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
989 for (j = 0; j < line_len; j++) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
990 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
991 line[j] = getc(fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
992 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
993 line[j] = '\0'; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
994 array[i] = line; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
995 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
996 uep->ue_array = array; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
997 if (found_first_uep == 0) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
998 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
999 uhp->uh_entry = uep; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1000 found_first_uep = 1; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1001 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1002 else |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1003 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1004 last_uep->ue_next = uep; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1005 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1006 last_uep = uep; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1007 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1008 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1009 /* Insertion sort the uhp into the table by its uh_seq. This is |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1010 * required because, while the number of uhps is limited to |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1011 * num_heads, and the uh_seq order is monotonic with respect to |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1012 * creation time, the starting uh_seq can be > 0 if any undolevel |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1013 * culling was done at undofile write time, and there can be uh_seq |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1014 * gaps in the uhps. |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1015 */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1016 for (i = num_read_uhps - 1; i >= -1; i--) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1017 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1018 /* if i == -1, we've hit the leftmost side of the table, so insert |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1019 * at uhp_table[0]. */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1020 if (i == -1 || uhp->uh_seq > uhp_table[i]->uh_seq) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1021 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1022 /* If we've had to move from the rightmost side of the table, |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1023 * we have to shift everything to the right by one spot. */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1024 if (i < num_read_uhps - 1) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1025 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1026 memmove(uhp_table + i + 2, uhp_table + i + 1, |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1027 (num_read_uhps - i) * sizeof(u_header_T *)); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1028 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1029 uhp_table[i + 1] = uhp; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1030 break; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1031 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1032 else if (uhp->uh_seq == uhp_table[i]->uh_seq) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1033 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1034 EMSG2(_("E826 Undo file corruption: duplicate uh_seq: %s"), |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1035 file_name); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1036 goto error; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1037 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1038 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1039 num_read_uhps++; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1040 c = get2c(fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1041 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1042 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1043 if (c != UF_END_MAGIC) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1044 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1045 EMSG2(_("E827: Undo file corruption; no end marker: %s"), file_name); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1046 goto error; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1047 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1048 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1049 /* We've organized all of the uhps into a table sorted by uh_seq. Now we |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1050 * iterate through the table and swizzle each sequence number we've |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1051 * stored in uh_foo into a pointer corresponding to the header with that |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1052 * sequence number. Then free curbuf's old undo structure, give curbuf |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1053 * the updated {old,new,cur}head pointers, and then free the table. */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1054 for (i = 0; i < num_head; i++) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1055 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1056 uhp = uhp_table[i]; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1057 if (uhp == NULL) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1058 continue; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1059 for (j = 0; j < num_head; j++) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1060 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1061 if (uhp_table[j] == NULL) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1062 continue; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1063 if (uhp_table[j]->uh_seq == (long)uhp->uh_next) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1064 uhp->uh_next = uhp_table[j]; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1065 if (uhp_table[j]->uh_seq == (long)uhp->uh_prev) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1066 uhp->uh_prev = uhp_table[j]; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1067 if (uhp_table[j]->uh_seq == (long)uhp->uh_alt_next) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1068 uhp->uh_alt_next = uhp_table[j]; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1069 if (uhp_table[j]->uh_seq == (long)uhp->uh_alt_prev) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1070 uhp->uh_alt_prev = uhp_table[j]; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1071 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1072 if (old_header_seq > 0 && old_idx < 0 && uhp->uh_seq == old_header_seq) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1073 old_idx = i; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1074 if (new_header_seq > 0 && new_idx < 0 && uhp->uh_seq == new_header_seq) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1075 new_idx = i; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1076 if (cur_header_seq > 0 && cur_idx < 0 && uhp->uh_seq == cur_header_seq) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1077 cur_idx = i; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1078 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1079 u_blockfree(curbuf); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1080 curbuf->b_u_oldhead = old_idx < 0 ? 0 : uhp_table[old_idx]; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1081 curbuf->b_u_newhead = new_idx < 0 ? 0 : uhp_table[new_idx]; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1082 curbuf->b_u_curhead = cur_idx < 0 ? 0 : uhp_table[cur_idx]; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1083 curbuf->b_u_line_ptr = line_ptr; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1084 curbuf->b_u_line_lnum = line_lnum; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1085 curbuf->b_u_line_colnr = line_colnr; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1086 curbuf->b_u_numhead = num_head; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1087 curbuf->b_u_seq_last = seq_last; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1088 curbuf->b_u_seq_cur = seq_cur; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1089 curbuf->b_u_seq_time = seq_time; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1090 U_FREE_LINE(uhp_table); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1091 #ifdef U_DEBUG |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1092 u_check(TRUE); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1093 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1094 if (name != NULL) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1095 smsg((char_u *)_("Finished reading undo file %s"), file_name); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1096 goto theend; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1097 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1098 error: |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1099 if (line_ptr != NULL) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1100 U_FREE_LINE(line_ptr); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1101 if (uhp_table != NULL) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1102 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1103 for (i = 0; i < num_head; i++) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1104 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1105 if (uhp_table[i] != NULL) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1106 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1107 uep = uhp_table[i]->uh_entry; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1108 while (uep != NULL) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1109 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1110 nuep = uep->ue_next; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1111 u_freeentry(uep, uep->ue_size); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1112 uep = nuep; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1113 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1114 U_FREE_LINE(uhp_table[i]); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1115 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1116 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1117 U_FREE_LINE(uhp_table); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1118 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1119 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1120 theend: |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1121 if (fp != NULL) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1122 fclose(fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1123 if (file_name != name) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1124 vim_free(file_name); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1125 return; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1126 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1127 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1128 /* |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1129 * Serialize "uep" to "fp". |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1130 */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1131 static int |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1132 serialize_uep(uep, fp) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1133 u_entry_T *uep; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1134 FILE *fp; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1135 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1136 int i; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1137 int uep_len; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1138 int *entry_lens; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1139 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1140 if (uep->ue_size > 0) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1141 entry_lens = (int *)alloc(uep->ue_size * sizeof(int)); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1142 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1143 /* Define uep_len to be the size of the entire uep minus the size of its |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1144 * component strings, in bytes. The sizes of the component strings |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1145 * are written before each individual string. |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1146 * We have 4 entries each of 4 bytes, plus ue_size * 4 bytes |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1147 * of string size information. */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1148 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1149 uep_len = uep->ue_size * 4; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1150 /* Collect sizing information for later serialization. */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1151 for (i = 0; i < uep->ue_size; i++) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1152 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1153 entry_lens[i] = (int)STRLEN(uep->ue_array[i]); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1154 uep_len += entry_lens[i]; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1155 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1156 put_bytes(fp, (long_u)uep_len, 4); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1157 put_bytes(fp, (long_u)uep->ue_top, 4); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1158 put_bytes(fp, (long_u)uep->ue_bot, 4); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1159 put_bytes(fp, (long_u)uep->ue_lcount, 4); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1160 put_bytes(fp, (long_u)uep->ue_size, 4); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1161 for (i = 0; i < uep->ue_size; i++) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1162 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1163 if (put_bytes(fp, (long_u)entry_lens[i], 4) == FAIL) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1164 return FAIL; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1165 fprintf(fp, "%s", uep->ue_array[i]); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1166 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1167 if (uep->ue_size > 0) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1168 vim_free(entry_lens); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1169 return OK; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1170 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1171 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1172 /* |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1173 * Serialize "pos" to "fp". |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1174 */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1175 static void |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1176 serialize_pos(pos, fp) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1177 pos_T pos; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1178 FILE *fp; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1179 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1180 put_bytes(fp, (long_u)pos.lnum, 4); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1181 put_bytes(fp, (long_u)pos.col, 4); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1182 #ifdef FEAT_VIRTUALEDIT |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1183 put_bytes(fp, (long_u)pos.coladd, 4); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1184 #else |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1185 put_bytes(fp, (long_u)0, 4); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1186 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1187 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1188 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1189 /* |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1190 * Serialize "info" to "fp". |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1191 */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1192 static void |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1193 serialize_visualinfo(info, fp) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1194 visualinfo_T info; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1195 FILE *fp; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1196 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1197 serialize_pos(info.vi_start, fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1198 serialize_pos(info.vi_end, fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1199 put_bytes(fp, (long_u)info.vi_mode, 4); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1200 put_bytes(fp, (long_u)info.vi_curswant, 4); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1201 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1202 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1203 static char_u e_not_open[] = N_("E828: Cannot open undo file for writing: %s"); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1204 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1205 /* |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1206 * Write the undo tree in an undo file. |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1207 * When "name" is not NULL, use it as the name of the undo file. |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1208 * Otherwise use buf->b_ffname to generate the undo file name. |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1209 * "buf" must never be null, buf->b_ffname is used to obtain the original file |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1210 * permissions. |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1211 * "forceit" is TRUE for ":wundo!", FALSE otherwise. |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1212 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text. |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1213 */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1214 void |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1215 u_write_undo(name, forceit, buf, hash) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1216 char_u *name; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1217 int forceit; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1218 buf_T *buf; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1219 char_u *hash; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1220 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1221 u_header_T *uhp; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1222 u_entry_T *uep; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1223 char_u *file_name; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1224 int str_len, i, uep_len, mark; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1225 int fd; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1226 FILE *fp = NULL; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1227 int perm; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1228 int write_ok = FALSE; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1229 #ifdef UNIX |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1230 struct stat st_old; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1231 struct stat st_new; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1232 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1233 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1234 if (name == NULL) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1235 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1236 file_name = u_get_undo_file_name(buf->b_ffname, FALSE); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1237 if (file_name == NULL) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1238 return; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1239 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1240 else |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1241 file_name = name; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1242 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1243 #ifdef UNIX |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1244 if (mch_stat((char *)buf->b_ffname, &st_old) >= 0) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1245 perm = st_old.st_mode; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1246 else |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1247 perm = 0600; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1248 #else |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1249 perm = mch_getperm(buf->b_ffname); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1250 if (perm < 0) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1251 perm = 0600; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1252 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1253 /* set file protection same as original file, but strip s-bit */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1254 perm = perm & 0777; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1255 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1256 /* If the undo file exists, verify that it actually is an undo file, and |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1257 * delete it. */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1258 if (mch_getperm(file_name) >= 0) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1259 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1260 if (name == NULL || !forceit) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1261 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1262 /* Check we can read it and it's an undo file. */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1263 fd = mch_open((char *)file_name, O_RDONLY|O_EXTRA, 0); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1264 if (fd < 0) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1265 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1266 if (name != NULL || p_verbose > 0) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1267 smsg((char_u *)_("Will not overwrite with undo file, cannot read: %s"), |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1268 file_name); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1269 goto theend; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1270 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1271 else |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1272 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1273 char_u buf[2]; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1274 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1275 vim_read(fd, buf, 2); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1276 close(fd); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1277 if ((buf[0] << 8) + buf[1] != UF_START_MAGIC) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1278 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1279 if (name != NULL || p_verbose > 0) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1280 smsg((char_u *)_("Will not overwrite, this is not an undo file: %s"), |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1281 file_name); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1282 goto theend; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1283 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1284 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1285 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1286 mch_remove(file_name); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1287 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1288 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1289 fd = mch_open((char *)file_name, |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1290 O_CREAT|O_EXTRA|O_WRONLY|O_EXCL|O_NOFOLLOW, perm); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1291 (void)mch_setperm(file_name, perm); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1292 if (fd < 0) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1293 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1294 EMSG2(_(e_not_open), file_name); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1295 goto theend; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1296 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1297 if (p_verbose > 0) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1298 smsg((char_u *)_("Writing undo file: %s"), file_name); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1299 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1300 #ifdef UNIX |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1301 /* |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1302 * Try to set the group of the undo file same as the original file. If |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1303 * this fails, set the protection bits for the group same as the |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1304 * protection bits for others. |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1305 */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1306 if (mch_stat((char *)file_name, &st_new) >= 0 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1307 && st_new.st_gid != st_old.st_gid |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1308 # ifdef HAVE_FCHOWN /* sequent-ptx lacks fchown() */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1309 && fchown(fd, (uid_t)-1, st_old.st_gid) != 0 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1310 # endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1311 ) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1312 mch_setperm(file_name, (perm & 0707) | ((perm & 07) << 3)); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1313 # ifdef HAVE_SELINUX |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1314 mch_copy_sec(buf->b_ffname, file_name); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1315 # endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1316 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1317 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1318 fp = fdopen(fd, "w"); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1319 if (fp == NULL) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1320 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1321 EMSG2(_(e_not_open), file_name); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1322 close(fd); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1323 mch_remove(file_name); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1324 goto theend; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1325 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1326 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1327 /* Start writing, first overall file information */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1328 put_bytes(fp, (long_u)UF_START_MAGIC, 2); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1329 put_bytes(fp, (long_u)UF_VERSION, 2); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1330 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1331 /* Write a hash of the buffer text, so that we can verify it is still the |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1332 * same when reading the buffer text. */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1333 if (fwrite(hash, (size_t)UNDO_HASH_SIZE, (size_t)1, fp) != 1) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1334 goto write_error; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1335 put_bytes(fp, (long_u)buf->b_ml.ml_line_count, 4); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1336 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1337 /* Begin undo data for U */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1338 str_len = buf->b_u_line_ptr != NULL ? STRLEN(buf->b_u_line_ptr) : 0; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1339 put_bytes(fp, (long_u)str_len, 4); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1340 if (str_len > 0 && fwrite(buf->b_u_line_ptr, (size_t)str_len, |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1341 (size_t)1, fp) != 1) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1342 goto write_error; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1343 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1344 put_bytes(fp, (long_u)buf->b_u_line_lnum, 4); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1345 put_bytes(fp, (long_u)buf->b_u_line_colnr, 4); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1346 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1347 /* Begin general undo data */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1348 uhp = buf->b_u_oldhead; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1349 put_bytes(fp, (long_u)(uhp != NULL ? uhp->uh_seq : 0), 4); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1350 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1351 uhp = buf->b_u_newhead; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1352 put_bytes(fp, (long_u)(uhp != NULL ? uhp->uh_seq : 0), 4); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1353 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1354 uhp = buf->b_u_curhead; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1355 put_bytes(fp, (long_u)(uhp != NULL ? uhp->uh_seq : 0), 4); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1356 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1357 put_bytes(fp, (long_u)buf->b_u_numhead, 4); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1358 put_bytes(fp, (long_u)buf->b_u_seq_last, 4); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1359 put_bytes(fp, (long_u)buf->b_u_seq_cur, 4); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1360 put_bytes(fp, (long_u)buf->b_u_seq_time, 4); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1361 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1362 /* Iteratively serialize UHPs and their UEPs from the top down. */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1363 mark = ++lastmark; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1364 uhp = buf->b_u_oldhead; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1365 while (uhp != NULL) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1366 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1367 /* Serialize current UHP if we haven't seen it */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1368 if (uhp->uh_walk != mark) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1369 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1370 if (put_bytes(fp, (long_u)UF_HEADER_MAGIC, 2) == FAIL) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1371 goto write_error; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1372 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1373 put_bytes(fp, (long_u)((uhp->uh_next != NULL) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1374 ? uhp->uh_next->uh_seq : 0), 4); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1375 put_bytes(fp, (long_u)((uhp->uh_prev != NULL) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1376 ? uhp->uh_prev->uh_seq : 0), 4); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1377 put_bytes(fp, (long_u)((uhp->uh_alt_next != NULL) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1378 ? uhp->uh_alt_next->uh_seq : 0), 4); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1379 put_bytes(fp, (long_u)((uhp->uh_alt_prev != NULL) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1380 ? uhp->uh_alt_prev->uh_seq : 0), 4); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1381 put_bytes(fp, uhp->uh_seq, 4); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1382 serialize_pos(uhp->uh_cursor, fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1383 #ifdef FEAT_VIRTUALEDIT |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1384 put_bytes(fp, (long_u)uhp->uh_cursor_vcol, 4); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1385 #else |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1386 put_bytes(fp, (long_u)0, 4); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1387 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1388 put_bytes(fp, (long_u)uhp->uh_flags, 2); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1389 /* Assume NMARKS will stay the same. */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1390 for (i = 0; i < NMARKS; ++i) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1391 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1392 serialize_pos(uhp->uh_namedm[i], fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1393 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1394 #ifdef FEAT_VISUAL |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1395 serialize_visualinfo(uhp->uh_visual, fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1396 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1397 put_bytes(fp, (long_u)uhp->uh_time, 4); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1398 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1399 uep = uhp->uh_entry; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1400 while (uep != NULL) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1401 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1402 if (serialize_uep(uep, fp) == FAIL) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1403 goto write_error; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1404 uep = uep->ue_next; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1405 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1406 /* Sentinel value: no more ueps */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1407 uep_len = -1; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1408 put_bytes(fp, (long_u)uep_len, 4); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1409 uhp->uh_walk = mark; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1410 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1411 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1412 /* Now walk through the tree - algorithm from undo_time */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1413 if (uhp->uh_prev != NULL && uhp->uh_prev->uh_walk != mark) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1414 uhp = uhp->uh_prev; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1415 else if (uhp->uh_alt_next != NULL && uhp->uh_alt_next->uh_walk != mark) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1416 uhp = uhp->uh_alt_next; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1417 else if (uhp->uh_next != NULL && uhp->uh_alt_prev == NULL |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1418 && uhp->uh_next->uh_walk != mark) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1419 uhp = uhp->uh_next; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1420 else if (uhp->uh_alt_prev != NULL) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1421 uhp = uhp->uh_alt_prev; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1422 else |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1423 uhp = uhp->uh_next; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1424 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1425 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1426 if (put_bytes(fp, (long_u)UF_END_MAGIC, 2) == OK) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1427 write_ok = TRUE; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1428 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1429 write_error: |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1430 fclose(fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1431 if (!write_ok) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1432 EMSG2(_("E829: write error in undo file: %s"), file_name); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1433 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1434 #if defined(MACOS_CLASSIC) || defined(WIN3264) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1435 (void)mch_copy_file_attribute(buf->b_ffname, file_name); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1436 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1437 #ifdef HAVE_ACL |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1438 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1439 vim_acl_T acl; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1440 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1441 /* For systems that support ACL: get the ACL from the original file. */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1442 acl = mch_get_acl(buf->b_ffname); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1443 mch_set_acl(file_name, acl); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1444 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1445 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1446 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1447 theend: |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1448 if (file_name != name) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1449 vim_free(file_name); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1450 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1451 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1452 #endif /* FEAT_PERSISTENT_UNDO */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1453 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1454 |
7 | 1455 /* |
1456 * If 'cpoptions' contains 'u': Undo the previous undo or redo (vi compatible). | |
1457 * If 'cpoptions' does not contain 'u': Always undo. | |
1458 */ | |
1459 void | |
1460 u_undo(count) | |
1461 int count; | |
1462 { | |
1463 /* | |
1464 * If we get an undo command while executing a macro, we behave like the | |
1465 * original vi. If this happens twice in one macro the result will not | |
1466 * be compatible. | |
1467 */ | |
1468 if (curbuf->b_u_synced == FALSE) | |
1469 { | |
825 | 1470 u_sync(TRUE); |
7 | 1471 count = 1; |
1472 } | |
1473 | |
1474 if (vim_strchr(p_cpo, CPO_UNDO) == NULL) | |
1475 undo_undoes = TRUE; | |
1476 else | |
1477 undo_undoes = !undo_undoes; | |
1478 u_doit(count); | |
1479 } | |
1480 | |
1481 /* | |
1482 * If 'cpoptions' contains 'u': Repeat the previous undo or redo. | |
1483 * If 'cpoptions' does not contain 'u': Always redo. | |
1484 */ | |
1485 void | |
1486 u_redo(count) | |
1487 int count; | |
1488 { | |
1489 if (vim_strchr(p_cpo, CPO_UNDO) == NULL) | |
1490 undo_undoes = FALSE; | |
1491 u_doit(count); | |
1492 } | |
1493 | |
1494 /* | |
1495 * Undo or redo, depending on 'undo_undoes', 'count' times. | |
1496 */ | |
1497 static void | |
777 | 1498 u_doit(startcount) |
1499 int startcount; | |
7 | 1500 { |
777 | 1501 int count = startcount; |
1502 | |
632 | 1503 if (!undo_allowed()) |
7 | 1504 return; |
1505 | |
1506 u_newcount = 0; | |
1507 u_oldcount = 0; | |
179 | 1508 if (curbuf->b_ml.ml_flags & ML_EMPTY) |
1509 u_oldcount = -1; | |
7 | 1510 while (count--) |
1511 { | |
1512 if (undo_undoes) | |
1513 { | |
1514 if (curbuf->b_u_curhead == NULL) /* first undo */ | |
1515 curbuf->b_u_curhead = curbuf->b_u_newhead; | |
1516 else if (p_ul > 0) /* multi level undo */ | |
1517 /* get next undo */ | |
1518 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_next; | |
1519 /* nothing to undo */ | |
1520 if (curbuf->b_u_numhead == 0 || curbuf->b_u_curhead == NULL) | |
1521 { | |
1522 /* stick curbuf->b_u_curhead at end */ | |
1523 curbuf->b_u_curhead = curbuf->b_u_oldhead; | |
1524 beep_flush(); | |
777 | 1525 if (count == startcount - 1) |
1526 { | |
1527 MSG(_("Already at oldest change")); | |
1528 return; | |
1529 } | |
7 | 1530 break; |
1531 } | |
1532 | |
777 | 1533 u_undoredo(TRUE); |
7 | 1534 } |
1535 else | |
1536 { | |
1537 if (curbuf->b_u_curhead == NULL || p_ul <= 0) | |
1538 { | |
1539 beep_flush(); /* nothing to redo */ | |
777 | 1540 if (count == startcount - 1) |
1541 { | |
1542 MSG(_("Already at newest change")); | |
1543 return; | |
1544 } | |
7 | 1545 break; |
1546 } | |
1547 | |
777 | 1548 u_undoredo(FALSE); |
753 | 1549 |
1550 /* Advance for next redo. Set "newhead" when at the end of the | |
1551 * redoable changes. */ | |
1552 if (curbuf->b_u_curhead->uh_prev == NULL) | |
1553 curbuf->b_u_newhead = curbuf->b_u_curhead; | |
7 | 1554 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_prev; |
1555 } | |
1556 } | |
798 | 1557 u_undo_end(undo_undoes, FALSE); |
753 | 1558 } |
1559 | |
1560 /* | |
1561 * Undo or redo over the timeline. | |
1562 * When "step" is negative go back in time, otherwise goes forward in time. | |
758 | 1563 * When "sec" is FALSE make "step" steps, when "sec" is TRUE use "step" as |
1564 * seconds. | |
772 | 1565 * When "absolute" is TRUE use "step" as the sequence number to jump to. |
1566 * "sec" must be FALSE then. | |
753 | 1567 */ |
1568 void | |
772 | 1569 undo_time(step, sec, absolute) |
758 | 1570 long step; |
1571 int sec; | |
772 | 1572 int absolute; |
753 | 1573 { |
1574 long target; | |
1575 long closest; | |
758 | 1576 long closest_start; |
1577 long closest_seq = 0; | |
1578 long val; | |
753 | 1579 u_header_T *uhp; |
1580 u_header_T *last; | |
1581 int mark; | |
1582 int nomark; | |
1583 int round; | |
758 | 1584 int dosec = sec; |
1585 int above = FALSE; | |
794 | 1586 int did_undo = TRUE; |
753 | 1587 |
766 | 1588 /* First make sure the current undoable change is synced. */ |
1589 if (curbuf->b_u_synced == FALSE) | |
825 | 1590 u_sync(TRUE); |
766 | 1591 |
753 | 1592 u_newcount = 0; |
1593 u_oldcount = 0; | |
179 | 1594 if (curbuf->b_ml.ml_flags & ML_EMPTY) |
753 | 1595 u_oldcount = -1; |
1596 | |
777 | 1597 /* "target" is the node below which we want to be. |
1598 * Init "closest" to a value we can't reach. */ | |
772 | 1599 if (absolute) |
1600 { | |
1601 target = step; | |
777 | 1602 closest = -1; |
753 | 1603 } |
1604 else | |
1605 { | |
777 | 1606 /* When doing computations with time_t subtract starttime, because |
1607 * time_t converted to a long may result in a wrong number. */ | |
758 | 1608 if (sec) |
777 | 1609 target = (long)(curbuf->b_u_seq_time - starttime) + step; |
1610 else | |
1611 target = curbuf->b_u_seq_cur + step; | |
1612 if (step < 0) | |
758 | 1613 { |
777 | 1614 if (target < 0) |
1615 target = 0; | |
1616 closest = -1; | |
758 | 1617 } |
1618 else | |
1619 { | |
777 | 1620 if (sec) |
798 | 1621 closest = (long)(time(NULL) - starttime + 1); |
777 | 1622 else |
1623 closest = curbuf->b_u_seq_last + 2; | |
1624 if (target >= closest) | |
1625 target = closest - 1; | |
758 | 1626 } |
753 | 1627 } |
758 | 1628 closest_start = closest; |
777 | 1629 closest_seq = curbuf->b_u_seq_cur; |
753 | 1630 |
758 | 1631 /* |
1632 * May do this twice: | |
753 | 1633 * 1. Search for "target", update "closest" to the best match found. |
758 | 1634 * 2. If "target" not found search for "closest". |
1635 * | |
1636 * When using the closest time we use the sequence number in the second | |
1637 * round, because there may be several entries with the same time. | |
1638 */ | |
753 | 1639 for (round = 1; round <= 2; ++round) |
1640 { | |
1641 /* Find the path from the current state to where we want to go. The | |
1642 * desired state can be anywhere in the undo tree, need to go all over | |
1643 * it. We put "nomark" in uh_walk where we have been without success, | |
1644 * "mark" where it could possibly be. */ | |
1645 mark = ++lastmark; | |
1646 nomark = ++lastmark; | |
1647 | |
1648 if (curbuf->b_u_curhead == NULL) /* at leaf of the tree */ | |
1649 uhp = curbuf->b_u_newhead; | |
1650 else | |
1651 uhp = curbuf->b_u_curhead; | |
1652 | |
1653 while (uhp != NULL) | |
1654 { | |
1655 uhp->uh_walk = mark; | |
798 | 1656 val = (long)(dosec ? (uhp->uh_time - starttime) : uhp->uh_seq); |
753 | 1657 |
758 | 1658 if (round == 1) |
1659 { | |
1660 /* Remember the header that is closest to the target. | |
1661 * It must be at least in the right direction (checked with | |
777 | 1662 * "b_u_seq_cur"). When the timestamp is equal find the |
758 | 1663 * highest/lowest sequence number. */ |
777 | 1664 if ((step < 0 ? uhp->uh_seq <= curbuf->b_u_seq_cur |
1665 : uhp->uh_seq > curbuf->b_u_seq_cur) | |
1666 && ((dosec && val == closest) | |
1667 ? (step < 0 | |
1668 ? uhp->uh_seq < closest_seq | |
1669 : uhp->uh_seq > closest_seq) | |
1670 : closest == closest_start | |
1671 || (val > target | |
1672 ? (closest > target | |
1673 ? val - target <= closest - target | |
1674 : val - target <= target - closest) | |
1675 : (closest > target | |
1676 ? target - val <= closest - target | |
1677 : target - val <= target - closest)))) | |
758 | 1678 { |
1679 closest = val; | |
1680 closest_seq = uhp->uh_seq; | |
1681 } | |
1682 } | |
1683 | |
1684 /* Quit searching when we found a match. But when searching for a | |
1685 * time we need to continue looking for the best uh_seq. */ | |
1686 if (target == val && !dosec) | |
1687 break; | |
753 | 1688 |
1689 /* go down in the tree if we haven't been there */ | |
1690 if (uhp->uh_prev != NULL && uhp->uh_prev->uh_walk != nomark | |
1691 && uhp->uh_prev->uh_walk != mark) | |
1692 uhp = uhp->uh_prev; | |
1693 | |
1694 /* go to alternate branch if we haven't been there */ | |
1695 else if (uhp->uh_alt_next != NULL | |
1696 && uhp->uh_alt_next->uh_walk != nomark | |
1697 && uhp->uh_alt_next->uh_walk != mark) | |
1698 uhp = uhp->uh_alt_next; | |
1699 | |
1700 /* go up in the tree if we haven't been there and we are at the | |
1701 * start of alternate branches */ | |
1702 else if (uhp->uh_next != NULL && uhp->uh_alt_prev == NULL | |
1703 && uhp->uh_next->uh_walk != nomark | |
1704 && uhp->uh_next->uh_walk != mark) | |
798 | 1705 { |
1706 /* If still at the start we don't go through this change. */ | |
1707 if (uhp == curbuf->b_u_curhead) | |
1708 uhp->uh_walk = nomark; | |
753 | 1709 uhp = uhp->uh_next; |
798 | 1710 } |
753 | 1711 |
1712 else | |
1713 { | |
1714 /* need to backtrack; mark this node as useless */ | |
1715 uhp->uh_walk = nomark; | |
1716 if (uhp->uh_alt_prev != NULL) | |
1717 uhp = uhp->uh_alt_prev; | |
1718 else | |
1719 uhp = uhp->uh_next; | |
1720 } | |
1721 } | |
1722 | |
1723 if (uhp != NULL) /* found it */ | |
1724 break; | |
772 | 1725 |
1726 if (absolute) | |
1727 { | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1728 EMSGN(_("E830: Undo number %ld not found"), step); |
772 | 1729 return; |
1730 } | |
1731 | |
758 | 1732 if (closest == closest_start) |
753 | 1733 { |
758 | 1734 if (step < 0) |
1735 MSG(_("Already at oldest change")); | |
1736 else | |
1737 MSG(_("Already at newest change")); | |
753 | 1738 return; |
1739 } | |
1740 | |
758 | 1741 target = closest_seq; |
1742 dosec = FALSE; | |
1743 if (step < 0) | |
1744 above = TRUE; /* stop above the header */ | |
753 | 1745 } |
1746 | |
1747 /* If we found it: Follow the path to go to where we want to be. */ | |
1748 if (uhp != NULL) | |
1749 { | |
1750 /* | |
1751 * First go up the tree as much as needed. | |
1752 */ | |
1753 for (;;) | |
1754 { | |
1755 uhp = curbuf->b_u_curhead; | |
1756 if (uhp == NULL) | |
1757 uhp = curbuf->b_u_newhead; | |
1758 else | |
1759 uhp = uhp->uh_next; | |
777 | 1760 if (uhp == NULL || uhp->uh_walk != mark |
1761 || (uhp->uh_seq == target && !above)) | |
753 | 1762 break; |
1763 curbuf->b_u_curhead = uhp; | |
777 | 1764 u_undoredo(TRUE); |
753 | 1765 uhp->uh_walk = nomark; /* don't go back down here */ |
1766 } | |
1767 | |
1768 /* | |
758 | 1769 * And now go down the tree (redo), branching off where needed. |
753 | 1770 */ |
1771 uhp = curbuf->b_u_curhead; | |
777 | 1772 while (uhp != NULL) |
753 | 1773 { |
1056 | 1774 /* Go back to the first branch with a mark. */ |
1775 while (uhp->uh_alt_prev != NULL | |
1776 && uhp->uh_alt_prev->uh_walk == mark) | |
1777 uhp = uhp->uh_alt_prev; | |
1778 | |
753 | 1779 /* Find the last branch with a mark, that's the one. */ |
1780 last = uhp; | |
1781 while (last->uh_alt_next != NULL | |
1782 && last->uh_alt_next->uh_walk == mark) | |
1783 last = last->uh_alt_next; | |
1784 if (last != uhp) | |
1785 { | |
1786 /* Make the used branch the first entry in the list of | |
1787 * alternatives to make "u" and CTRL-R take this branch. */ | |
1056 | 1788 while (uhp->uh_alt_prev != NULL) |
1789 uhp = uhp->uh_alt_prev; | |
753 | 1790 if (last->uh_alt_next != NULL) |
1791 last->uh_alt_next->uh_alt_prev = last->uh_alt_prev; | |
1792 last->uh_alt_prev->uh_alt_next = last->uh_alt_next; | |
1793 last->uh_alt_prev = NULL; | |
1794 last->uh_alt_next = uhp; | |
1795 uhp->uh_alt_prev = last; | |
1796 | |
1797 uhp = last; | |
777 | 1798 if (uhp->uh_next != NULL) |
1799 uhp->uh_next->uh_prev = uhp; | |
753 | 1800 } |
758 | 1801 curbuf->b_u_curhead = uhp; |
753 | 1802 |
1803 if (uhp->uh_walk != mark) | |
1804 break; /* must have reached the target */ | |
1805 | |
758 | 1806 /* Stop when going backwards in time and didn't find the exact |
1807 * header we were looking for. */ | |
1808 if (uhp->uh_seq == target && above) | |
798 | 1809 { |
1810 curbuf->b_u_seq_cur = target - 1; | |
758 | 1811 break; |
798 | 1812 } |
758 | 1813 |
777 | 1814 u_undoredo(FALSE); |
753 | 1815 |
1816 /* Advance "curhead" to below the header we last used. If it | |
1817 * becomes NULL then we need to set "newhead" to this leaf. */ | |
1818 if (uhp->uh_prev == NULL) | |
1819 curbuf->b_u_newhead = uhp; | |
1820 curbuf->b_u_curhead = uhp->uh_prev; | |
794 | 1821 did_undo = FALSE; |
753 | 1822 |
1823 if (uhp->uh_seq == target) /* found it! */ | |
1824 break; | |
1825 | |
1826 uhp = uhp->uh_prev; | |
1827 if (uhp == NULL || uhp->uh_walk != mark) | |
1828 { | |
777 | 1829 /* Need to redo more but can't find it... */ |
753 | 1830 EMSG2(_(e_intern2), "undo_time()"); |
1831 break; | |
1832 } | |
1833 } | |
1834 } | |
798 | 1835 u_undo_end(did_undo, absolute); |
7 | 1836 } |
1837 | |
1838 /* | |
1839 * u_undoredo: common code for undo and redo | |
1840 * | |
1841 * The lines in the file are replaced by the lines in the entry list at | |
1842 * curbuf->b_u_curhead. The replaced lines in the file are saved in the entry | |
1843 * list for the next undo/redo. | |
777 | 1844 * |
1845 * When "undo" is TRUE we go up in the tree, when FALSE we go down. | |
7 | 1846 */ |
1847 static void | |
777 | 1848 u_undoredo(undo) |
1849 int undo; | |
7 | 1850 { |
1851 char_u **newarray = NULL; | |
1852 linenr_T oldsize; | |
1853 linenr_T newsize; | |
1854 linenr_T top, bot; | |
1855 linenr_T lnum; | |
1856 linenr_T newlnum = MAXLNUM; | |
1857 long i; | |
1858 u_entry_T *uep, *nuep; | |
1859 u_entry_T *newlist = NULL; | |
1860 int old_flags; | |
1861 int new_flags; | |
1862 pos_T namedm[NMARKS]; | |
692 | 1863 #ifdef FEAT_VISUAL |
1864 visualinfo_T visualinfo; | |
1865 #endif | |
7 | 1866 int empty_buffer; /* buffer became empty */ |
758 | 1867 u_header_T *curhead = curbuf->b_u_curhead; |
7 | 1868 |
1415 | 1869 #ifdef U_DEBUG |
1870 u_check(FALSE); | |
1871 #endif | |
758 | 1872 old_flags = curhead->uh_flags; |
7 | 1873 new_flags = (curbuf->b_changed ? UH_CHANGED : 0) + |
1874 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0); | |
1875 setpcmark(); | |
1876 | |
1877 /* | |
1878 * save marks before undo/redo | |
1879 */ | |
1880 mch_memmove(namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS); | |
692 | 1881 #ifdef FEAT_VISUAL |
1882 visualinfo = curbuf->b_visual; | |
1883 #endif | |
7 | 1884 curbuf->b_op_start.lnum = curbuf->b_ml.ml_line_count; |
1885 curbuf->b_op_start.col = 0; | |
1886 curbuf->b_op_end.lnum = 0; | |
1887 curbuf->b_op_end.col = 0; | |
1888 | |
758 | 1889 for (uep = curhead->uh_entry; uep != NULL; uep = nuep) |
7 | 1890 { |
1891 top = uep->ue_top; | |
1892 bot = uep->ue_bot; | |
1893 if (bot == 0) | |
1894 bot = curbuf->b_ml.ml_line_count + 1; | |
1895 if (top > curbuf->b_ml.ml_line_count || top >= bot | |
1896 || bot > curbuf->b_ml.ml_line_count + 1) | |
1897 { | |
1898 EMSG(_("E438: u_undo: line numbers wrong")); | |
1899 changed(); /* don't want UNCHANGED now */ | |
1900 return; | |
1901 } | |
1902 | |
1903 oldsize = bot - top - 1; /* number of lines before undo */ | |
1904 newsize = uep->ue_size; /* number of lines after undo */ | |
1905 | |
1906 if (top < newlnum) | |
1907 { | |
1908 /* If the saved cursor is somewhere in this undo block, move it to | |
1909 * the remembered position. Makes "gwap" put the cursor back | |
1910 * where it was. */ | |
758 | 1911 lnum = curhead->uh_cursor.lnum; |
7 | 1912 if (lnum >= top && lnum <= top + newsize + 1) |
1913 { | |
758 | 1914 curwin->w_cursor = curhead->uh_cursor; |
7 | 1915 newlnum = curwin->w_cursor.lnum - 1; |
1916 } | |
1917 else | |
1918 { | |
1919 /* Use the first line that actually changed. Avoids that | |
1920 * undoing auto-formatting puts the cursor in the previous | |
1921 * line. */ | |
1922 for (i = 0; i < newsize && i < oldsize; ++i) | |
1923 if (STRCMP(uep->ue_array[i], ml_get(top + 1 + i)) != 0) | |
1924 break; | |
1925 if (i == newsize && newlnum == MAXLNUM && uep->ue_next == NULL) | |
1926 { | |
1927 newlnum = top; | |
1928 curwin->w_cursor.lnum = newlnum + 1; | |
1929 } | |
1930 else if (i < newsize) | |
1931 { | |
1932 newlnum = top + i; | |
1933 curwin->w_cursor.lnum = newlnum + 1; | |
1934 } | |
1935 } | |
1936 } | |
1937 | |
1938 empty_buffer = FALSE; | |
1939 | |
1940 /* delete the lines between top and bot and save them in newarray */ | |
168 | 1941 if (oldsize > 0) |
7 | 1942 { |
168 | 1943 if ((newarray = (char_u **)U_ALLOC_LINE( |
7 | 1944 (unsigned)(sizeof(char_u *) * oldsize))) == NULL) |
1945 { | |
1946 do_outofmem_msg((long_u)(sizeof(char_u *) * oldsize)); | |
1947 /* | |
1948 * We have messed up the entry list, repair is impossible. | |
1949 * we have to free the rest of the list. | |
1950 */ | |
1951 while (uep != NULL) | |
1952 { | |
1953 nuep = uep->ue_next; | |
1954 u_freeentry(uep, uep->ue_size); | |
1955 uep = nuep; | |
1956 } | |
1957 break; | |
1958 } | |
1959 /* delete backwards, it goes faster in most cases */ | |
1960 for (lnum = bot - 1, i = oldsize; --i >= 0; --lnum) | |
1961 { | |
1962 /* what can we do when we run out of memory? */ | |
1963 if ((newarray[i] = u_save_line(lnum)) == NULL) | |
1964 do_outofmem_msg((long_u)0); | |
1965 /* remember we deleted the last line in the buffer, and a | |
1966 * dummy empty line will be inserted */ | |
1967 if (curbuf->b_ml.ml_line_count == 1) | |
1968 empty_buffer = TRUE; | |
1969 ml_delete(lnum, FALSE); | |
1970 } | |
1971 } | |
414 | 1972 else |
1973 newarray = NULL; | |
7 | 1974 |
1975 /* insert the lines in u_array between top and bot */ | |
1976 if (newsize) | |
1977 { | |
1978 for (lnum = top, i = 0; i < newsize; ++i, ++lnum) | |
1979 { | |
1980 /* | |
1981 * If the file is empty, there is an empty line 1 that we | |
1982 * should get rid of, by replacing it with the new line | |
1983 */ | |
1984 if (empty_buffer && lnum == 0) | |
1985 ml_replace((linenr_T)1, uep->ue_array[i], TRUE); | |
1986 else | |
1987 ml_append(lnum, uep->ue_array[i], (colnr_T)0, FALSE); | |
168 | 1988 U_FREE_LINE(uep->ue_array[i]); |
7 | 1989 } |
168 | 1990 U_FREE_LINE((char_u *)uep->ue_array); |
7 | 1991 } |
1992 | |
1993 /* adjust marks */ | |
1994 if (oldsize != newsize) | |
1995 { | |
1996 mark_adjust(top + 1, top + oldsize, (long)MAXLNUM, | |
1997 (long)newsize - (long)oldsize); | |
1998 if (curbuf->b_op_start.lnum > top + oldsize) | |
1999 curbuf->b_op_start.lnum += newsize - oldsize; | |
2000 if (curbuf->b_op_end.lnum > top + oldsize) | |
2001 curbuf->b_op_end.lnum += newsize - oldsize; | |
2002 } | |
2003 | |
2004 changed_lines(top + 1, 0, bot, newsize - oldsize); | |
2005 | |
2006 /* set '[ and '] mark */ | |
2007 if (top + 1 < curbuf->b_op_start.lnum) | |
2008 curbuf->b_op_start.lnum = top + 1; | |
2009 if (newsize == 0 && top + 1 > curbuf->b_op_end.lnum) | |
2010 curbuf->b_op_end.lnum = top + 1; | |
2011 else if (top + newsize > curbuf->b_op_end.lnum) | |
2012 curbuf->b_op_end.lnum = top + newsize; | |
2013 | |
2014 u_newcount += newsize; | |
2015 u_oldcount += oldsize; | |
2016 uep->ue_size = oldsize; | |
2017 uep->ue_array = newarray; | |
2018 uep->ue_bot = top + newsize + 1; | |
2019 | |
2020 /* | |
2021 * insert this entry in front of the new entry list | |
2022 */ | |
2023 nuep = uep->ue_next; | |
2024 uep->ue_next = newlist; | |
2025 newlist = uep; | |
2026 } | |
2027 | |
758 | 2028 curhead->uh_entry = newlist; |
2029 curhead->uh_flags = new_flags; | |
7 | 2030 if ((old_flags & UH_EMPTYBUF) && bufempty()) |
2031 curbuf->b_ml.ml_flags |= ML_EMPTY; | |
2032 if (old_flags & UH_CHANGED) | |
2033 changed(); | |
2034 else | |
33 | 2035 #ifdef FEAT_NETBEANS_INTG |
2036 /* per netbeans undo rules, keep it as modified */ | |
2037 if (!isNetbeansModified(curbuf)) | |
2038 #endif | |
7 | 2039 unchanged(curbuf, FALSE); |
2040 | |
2041 /* | |
2042 * restore marks from before undo/redo | |
2043 */ | |
2044 for (i = 0; i < NMARKS; ++i) | |
758 | 2045 if (curhead->uh_namedm[i].lnum != 0) |
7 | 2046 { |
758 | 2047 curbuf->b_namedm[i] = curhead->uh_namedm[i]; |
2048 curhead->uh_namedm[i] = namedm[i]; | |
7 | 2049 } |
692 | 2050 #ifdef FEAT_VISUAL |
758 | 2051 if (curhead->uh_visual.vi_start.lnum != 0) |
692 | 2052 { |
758 | 2053 curbuf->b_visual = curhead->uh_visual; |
2054 curhead->uh_visual = visualinfo; | |
692 | 2055 } |
2056 #endif | |
7 | 2057 |
2058 /* | |
2059 * If the cursor is only off by one line, put it at the same position as | |
2060 * before starting the change (for the "o" command). | |
2061 * Otherwise the cursor should go to the first undone line. | |
2062 */ | |
758 | 2063 if (curhead->uh_cursor.lnum + 1 == curwin->w_cursor.lnum |
7 | 2064 && curwin->w_cursor.lnum > 1) |
2065 --curwin->w_cursor.lnum; | |
758 | 2066 if (curhead->uh_cursor.lnum == curwin->w_cursor.lnum) |
7 | 2067 { |
758 | 2068 curwin->w_cursor.col = curhead->uh_cursor.col; |
7 | 2069 #ifdef FEAT_VIRTUALEDIT |
758 | 2070 if (virtual_active() && curhead->uh_cursor_vcol >= 0) |
2071 coladvance((colnr_T)curhead->uh_cursor_vcol); | |
7 | 2072 else |
2073 curwin->w_cursor.coladd = 0; | |
2074 #endif | |
2075 } | |
2076 else if (curwin->w_cursor.lnum <= curbuf->b_ml.ml_line_count) | |
2077 beginline(BL_SOL | BL_FIX); | |
2078 else | |
2079 { | |
2080 /* We get here with the current cursor line being past the end (eg | |
2081 * after adding lines at the end of the file, and then undoing it). | |
2082 * check_cursor() will move the cursor to the last line. Move it to | |
2083 * the first column here. */ | |
2084 curwin->w_cursor.col = 0; | |
2085 #ifdef FEAT_VIRTUALEDIT | |
2086 curwin->w_cursor.coladd = 0; | |
2087 #endif | |
2088 } | |
2089 | |
2090 /* Make sure the cursor is on an existing line and column. */ | |
2091 check_cursor(); | |
758 | 2092 |
2093 /* Remember where we are for "g-" and ":earlier 10s". */ | |
2094 curbuf->b_u_seq_cur = curhead->uh_seq; | |
777 | 2095 if (undo) |
2096 /* We are below the previous undo. However, to make ":earlier 1s" | |
2097 * work we compute this as being just above the just undone change. */ | |
2098 --curbuf->b_u_seq_cur; | |
2099 | |
2100 /* The timestamp can be the same for multiple changes, just use the one of | |
2101 * the undone/redone change. */ | |
758 | 2102 curbuf->b_u_seq_time = curhead->uh_time; |
1415 | 2103 #ifdef U_DEBUG |
2104 u_check(FALSE); | |
2105 #endif | |
7 | 2106 } |
2107 | |
2108 /* | |
2109 * If we deleted or added lines, report the number of less/more lines. | |
2110 * Otherwise, report the number of changes (this may be incorrect | |
2111 * in some cases, but it's better than nothing). | |
2112 */ | |
2113 static void | |
798 | 2114 u_undo_end(did_undo, absolute) |
794 | 2115 int did_undo; /* just did an undo */ |
798 | 2116 int absolute; /* used ":undo N" */ |
7 | 2117 { |
944 | 2118 char *msgstr; |
772 | 2119 u_header_T *uhp; |
2120 char_u msgbuf[80]; | |
753 | 2121 |
7 | 2122 #ifdef FEAT_FOLDING |
2123 if ((fdo_flags & FDO_UNDO) && KeyTyped) | |
2124 foldOpenCursor(); | |
2125 #endif | |
753 | 2126 |
2127 if (global_busy /* no messages now, wait until global is finished */ | |
2128 || !messaging()) /* 'lazyredraw' set, don't do messages now */ | |
2129 return; | |
2130 | |
2131 if (curbuf->b_ml.ml_flags & ML_EMPTY) | |
2132 --u_newcount; | |
2133 | |
2134 u_oldcount -= u_newcount; | |
2135 if (u_oldcount == -1) | |
944 | 2136 msgstr = N_("more line"); |
753 | 2137 else if (u_oldcount < 0) |
944 | 2138 msgstr = N_("more lines"); |
753 | 2139 else if (u_oldcount == 1) |
944 | 2140 msgstr = N_("line less"); |
753 | 2141 else if (u_oldcount > 1) |
944 | 2142 msgstr = N_("fewer lines"); |
753 | 2143 else |
2144 { | |
2145 u_oldcount = u_newcount; | |
2146 if (u_newcount == 1) | |
944 | 2147 msgstr = N_("change"); |
753 | 2148 else |
944 | 2149 msgstr = N_("changes"); |
753 | 2150 } |
2151 | |
772 | 2152 if (curbuf->b_u_curhead != NULL) |
794 | 2153 { |
798 | 2154 /* For ":undo N" we prefer a "after #N" message. */ |
2155 if (absolute && curbuf->b_u_curhead->uh_next != NULL) | |
2156 { | |
2157 uhp = curbuf->b_u_curhead->uh_next; | |
2158 did_undo = FALSE; | |
2159 } | |
2160 else if (did_undo) | |
794 | 2161 uhp = curbuf->b_u_curhead; |
2162 else | |
2163 uhp = curbuf->b_u_curhead->uh_next; | |
2164 } | |
753 | 2165 else |
772 | 2166 uhp = curbuf->b_u_newhead; |
753 | 2167 |
772 | 2168 if (uhp == NULL) |
2169 *msgbuf = NUL; | |
2170 else | |
2171 u_add_time(msgbuf, sizeof(msgbuf), uhp->uh_time); | |
2172 | |
794 | 2173 smsg((char_u *)_("%ld %s; %s #%ld %s"), |
777 | 2174 u_oldcount < 0 ? -u_oldcount : u_oldcount, |
944 | 2175 _(msgstr), |
794 | 2176 did_undo ? _("before") : _("after"), |
2177 uhp == NULL ? 0L : uhp->uh_seq, | |
2178 msgbuf); | |
7 | 2179 } |
2180 | |
2181 /* | |
2182 * u_sync: stop adding to the current entry list | |
2183 */ | |
2184 void | |
825 | 2185 u_sync(force) |
2186 int force; /* Also sync when no_u_sync is set. */ | |
7 | 2187 { |
825 | 2188 /* Skip it when already synced or syncing is disabled. */ |
2189 if (curbuf->b_u_synced || (!force && no_u_sync > 0)) | |
2190 return; | |
7 | 2191 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
2192 if (im_is_preediting()) | |
2193 return; /* XIM is busy, don't break an undo sequence */ | |
2194 #endif | |
2195 if (p_ul < 0) | |
2196 curbuf->b_u_synced = TRUE; /* no entries, nothing to do */ | |
2197 else | |
2198 { | |
2199 u_getbot(); /* compute ue_bot of previous u_save */ | |
2200 curbuf->b_u_curhead = NULL; | |
2201 } | |
2202 } | |
2203 | |
2204 /* | |
772 | 2205 * ":undolist": List the leafs of the undo tree |
2206 */ | |
2207 void | |
2208 ex_undolist(eap) | |
2154
7c8c7c95a865
First step in the Vim 7.3 branch. Changed version numbers.
Bram Moolenaar <bram@zimbu.org>
parents:
1534
diff
changeset
|
2209 exarg_T *eap UNUSED; |
772 | 2210 { |
2211 garray_T ga; | |
2212 u_header_T *uhp; | |
2213 int mark; | |
2214 int nomark; | |
2215 int changes = 1; | |
2216 int i; | |
2217 | |
2218 /* | |
2219 * 1: walk the tree to find all leafs, put the info in "ga". | |
2220 * 2: sort the lines | |
2221 * 3: display the list | |
2222 */ | |
2223 mark = ++lastmark; | |
2224 nomark = ++lastmark; | |
2225 ga_init2(&ga, (int)sizeof(char *), 20); | |
2226 | |
2227 uhp = curbuf->b_u_oldhead; | |
2228 while (uhp != NULL) | |
2229 { | |
777 | 2230 if (uhp->uh_prev == NULL && uhp->uh_walk != nomark |
2231 && uhp->uh_walk != mark) | |
772 | 2232 { |
2233 if (ga_grow(&ga, 1) == FAIL) | |
2234 break; | |
2235 vim_snprintf((char *)IObuff, IOSIZE, "%6ld %7ld ", | |
2236 uhp->uh_seq, changes); | |
2237 u_add_time(IObuff + STRLEN(IObuff), IOSIZE - STRLEN(IObuff), | |
2238 uhp->uh_time); | |
2239 ((char_u **)(ga.ga_data))[ga.ga_len++] = vim_strsave(IObuff); | |
2240 } | |
2241 | |
2242 uhp->uh_walk = mark; | |
2243 | |
2244 /* go down in the tree if we haven't been there */ | |
2245 if (uhp->uh_prev != NULL && uhp->uh_prev->uh_walk != nomark | |
2246 && uhp->uh_prev->uh_walk != mark) | |
2247 { | |
2248 uhp = uhp->uh_prev; | |
2249 ++changes; | |
2250 } | |
2251 | |
2252 /* go to alternate branch if we haven't been there */ | |
2253 else if (uhp->uh_alt_next != NULL | |
2254 && uhp->uh_alt_next->uh_walk != nomark | |
2255 && uhp->uh_alt_next->uh_walk != mark) | |
2256 uhp = uhp->uh_alt_next; | |
2257 | |
2258 /* go up in the tree if we haven't been there and we are at the | |
2259 * start of alternate branches */ | |
2260 else if (uhp->uh_next != NULL && uhp->uh_alt_prev == NULL | |
2261 && uhp->uh_next->uh_walk != nomark | |
2262 && uhp->uh_next->uh_walk != mark) | |
2263 { | |
2264 uhp = uhp->uh_next; | |
2265 --changes; | |
2266 } | |
2267 | |
2268 else | |
2269 { | |
2270 /* need to backtrack; mark this node as done */ | |
2271 uhp->uh_walk = nomark; | |
2272 if (uhp->uh_alt_prev != NULL) | |
2273 uhp = uhp->uh_alt_prev; | |
2274 else | |
2275 { | |
2276 uhp = uhp->uh_next; | |
2277 --changes; | |
2278 } | |
2279 } | |
2280 } | |
2281 | |
2282 if (ga.ga_len == 0) | |
2283 MSG(_("Nothing to undo")); | |
2284 else | |
2285 { | |
2286 sort_strings((char_u **)ga.ga_data, ga.ga_len); | |
2287 | |
2288 msg_start(); | |
2289 msg_puts_attr((char_u *)_("number changes time"), hl_attr(HLF_T)); | |
2290 for (i = 0; i < ga.ga_len && !got_int; ++i) | |
2291 { | |
2292 msg_putchar('\n'); | |
2293 if (got_int) | |
2294 break; | |
2295 msg_puts(((char_u **)ga.ga_data)[i]); | |
2296 } | |
2297 msg_end(); | |
2298 | |
2299 ga_clear_strings(&ga); | |
2300 } | |
2301 } | |
2302 | |
2303 /* | |
2304 * Put the timestamp of an undo header in "buf[buflen]" in a nice format. | |
2305 */ | |
2306 static void | |
2307 u_add_time(buf, buflen, tt) | |
2308 char_u *buf; | |
2309 size_t buflen; | |
2310 time_t tt; | |
2311 { | |
2312 #ifdef HAVE_STRFTIME | |
2313 struct tm *curtime; | |
2314 | |
2315 if (time(NULL) - tt >= 100) | |
2316 { | |
2317 curtime = localtime(&tt); | |
810 | 2318 (void)strftime((char *)buf, buflen, "%H:%M:%S", curtime); |
772 | 2319 } |
2320 else | |
2321 #endif | |
835 | 2322 vim_snprintf((char *)buf, buflen, _("%ld seconds ago"), |
772 | 2323 (long)(time(NULL) - tt)); |
2324 } | |
2325 | |
2326 /* | |
697 | 2327 * ":undojoin": continue adding to the last entry list |
2328 */ | |
2329 void | |
2330 ex_undojoin(eap) | |
2154
7c8c7c95a865
First step in the Vim 7.3 branch. Changed version numbers.
Bram Moolenaar <bram@zimbu.org>
parents:
1534
diff
changeset
|
2331 exarg_T *eap UNUSED; |
697 | 2332 { |
839 | 2333 if (curbuf->b_u_newhead == NULL) |
2334 return; /* nothing changed before */ | |
2335 if (curbuf->b_u_curhead != NULL) | |
2336 { | |
2337 EMSG(_("E790: undojoin is not allowed after undo")); | |
2338 return; | |
2339 } | |
697 | 2340 if (!curbuf->b_u_synced) |
2341 return; /* already unsynced */ | |
2342 if (p_ul < 0) | |
2343 return; /* no entries, nothing to do */ | |
2344 else | |
2345 { | |
2346 /* Go back to the last entry */ | |
2347 curbuf->b_u_curhead = curbuf->b_u_newhead; | |
2348 curbuf->b_u_synced = FALSE; /* no entries, nothing to do */ | |
2349 } | |
2350 } | |
2351 | |
2352 /* | |
7 | 2353 * Called after writing the file and setting b_changed to FALSE. |
2354 * Now an undo means that the buffer is modified. | |
2355 */ | |
2356 void | |
2357 u_unchanged(buf) | |
2358 buf_T *buf; | |
2359 { | |
758 | 2360 u_unch_branch(buf->b_u_oldhead); |
2361 buf->b_did_warn = FALSE; | |
2362 } | |
2363 | |
2364 static void | |
2365 u_unch_branch(uhp) | |
2366 u_header_T *uhp; | |
2367 { | |
753 | 2368 u_header_T *uh; |
7 | 2369 |
758 | 2370 for (uh = uhp; uh != NULL; uh = uh->uh_prev) |
2371 { | |
7 | 2372 uh->uh_flags |= UH_CHANGED; |
758 | 2373 if (uh->uh_alt_next != NULL) |
2374 u_unch_branch(uh->uh_alt_next); /* recursive */ | |
2375 } | |
7 | 2376 } |
2377 | |
2378 /* | |
2379 * Get pointer to last added entry. | |
2380 * If it's not valid, give an error message and return NULL. | |
2381 */ | |
2382 static u_entry_T * | |
2383 u_get_headentry() | |
2384 { | |
2385 if (curbuf->b_u_newhead == NULL || curbuf->b_u_newhead->uh_entry == NULL) | |
2386 { | |
2387 EMSG(_("E439: undo list corrupt")); | |
2388 return NULL; | |
2389 } | |
2390 return curbuf->b_u_newhead->uh_entry; | |
2391 } | |
2392 | |
2393 /* | |
2394 * u_getbot(): compute the line number of the previous u_save | |
2395 * It is called only when b_u_synced is FALSE. | |
2396 */ | |
2397 static void | |
2398 u_getbot() | |
2399 { | |
2400 u_entry_T *uep; | |
2401 linenr_T extra; | |
2402 | |
2403 uep = u_get_headentry(); /* check for corrupt undo list */ | |
2404 if (uep == NULL) | |
2405 return; | |
2406 | |
2407 uep = curbuf->b_u_newhead->uh_getbot_entry; | |
2408 if (uep != NULL) | |
2409 { | |
2410 /* | |
2411 * the new ue_bot is computed from the number of lines that has been | |
2412 * inserted (0 - deleted) since calling u_save. This is equal to the | |
2413 * old line count subtracted from the current line count. | |
2414 */ | |
2415 extra = curbuf->b_ml.ml_line_count - uep->ue_lcount; | |
2416 uep->ue_bot = uep->ue_top + uep->ue_size + 1 + extra; | |
2417 if (uep->ue_bot < 1 || uep->ue_bot > curbuf->b_ml.ml_line_count) | |
2418 { | |
2419 EMSG(_("E440: undo line missing")); | |
2420 uep->ue_bot = uep->ue_top + 1; /* assume all lines deleted, will | |
2421 * get all the old lines back | |
2422 * without deleting the current | |
2423 * ones */ | |
2424 } | |
2425 | |
2426 curbuf->b_u_newhead->uh_getbot_entry = NULL; | |
2427 } | |
2428 | |
2429 curbuf->b_u_synced = TRUE; | |
2430 } | |
2431 | |
2432 /* | |
1415 | 2433 * Free one header "uhp" and its entry list and adjust the pointers. |
7 | 2434 */ |
2435 static void | |
758 | 2436 u_freeheader(buf, uhp, uhpp) |
168 | 2437 buf_T *buf; |
753 | 2438 u_header_T *uhp; |
2439 u_header_T **uhpp; /* if not NULL reset when freeing this header */ | |
7 | 2440 { |
1415 | 2441 u_header_T *uhap; |
2442 | |
753 | 2443 /* When there is an alternate redo list free that branch completely, |
2444 * because we can never go there. */ | |
2445 if (uhp->uh_alt_next != NULL) | |
2446 u_freebranch(buf, uhp->uh_alt_next, uhpp); | |
7 | 2447 |
753 | 2448 if (uhp->uh_alt_prev != NULL) |
2449 uhp->uh_alt_prev->uh_alt_next = NULL; | |
7 | 2450 |
753 | 2451 /* Update the links in the list to remove the header. */ |
7 | 2452 if (uhp->uh_next == NULL) |
168 | 2453 buf->b_u_oldhead = uhp->uh_prev; |
7 | 2454 else |
2455 uhp->uh_next->uh_prev = uhp->uh_prev; | |
2456 | |
2457 if (uhp->uh_prev == NULL) | |
168 | 2458 buf->b_u_newhead = uhp->uh_next; |
7 | 2459 else |
1415 | 2460 for (uhap = uhp->uh_prev; uhap != NULL; uhap = uhap->uh_alt_next) |
2461 uhap->uh_next = uhp->uh_next; | |
7 | 2462 |
753 | 2463 u_freeentries(buf, uhp, uhpp); |
2464 } | |
2465 | |
2466 /* | |
758 | 2467 * Free an alternate branch and any following alternate branches. |
753 | 2468 */ |
2469 static void | |
2470 u_freebranch(buf, uhp, uhpp) | |
2471 buf_T *buf; | |
2472 u_header_T *uhp; | |
2473 u_header_T **uhpp; /* if not NULL reset when freeing this header */ | |
2474 { | |
2475 u_header_T *tofree, *next; | |
2476 | |
1440 | 2477 /* If this is the top branch we may need to use u_freeheader() to update |
2478 * all the pointers. */ | |
2479 if (uhp == buf->b_u_oldhead) | |
2480 { | |
2481 u_freeheader(buf, uhp, uhpp); | |
2482 return; | |
2483 } | |
2484 | |
753 | 2485 if (uhp->uh_alt_prev != NULL) |
2486 uhp->uh_alt_prev->uh_alt_next = NULL; | |
2487 | |
2488 next = uhp; | |
2489 while (next != NULL) | |
2490 { | |
2491 tofree = next; | |
2492 if (tofree->uh_alt_next != NULL) | |
2493 u_freebranch(buf, tofree->uh_alt_next, uhpp); /* recursive */ | |
2494 next = tofree->uh_prev; | |
2495 u_freeentries(buf, tofree, uhpp); | |
2496 } | |
2497 } | |
2498 | |
2499 /* | |
2500 * Free all the undo entries for one header and the header itself. | |
2501 * This means that "uhp" is invalid when returning. | |
2502 */ | |
2503 static void | |
2504 u_freeentries(buf, uhp, uhpp) | |
2505 buf_T *buf; | |
2506 u_header_T *uhp; | |
2507 u_header_T **uhpp; /* if not NULL reset when freeing this header */ | |
2508 { | |
2509 u_entry_T *uep, *nuep; | |
2510 | |
2511 /* Check for pointers to the header that become invalid now. */ | |
2512 if (buf->b_u_curhead == uhp) | |
2513 buf->b_u_curhead = NULL; | |
1415 | 2514 if (buf->b_u_newhead == uhp) |
2515 buf->b_u_newhead = NULL; /* freeing the newest entry */ | |
753 | 2516 if (uhpp != NULL && uhp == *uhpp) |
2517 *uhpp = NULL; | |
2518 | |
2519 for (uep = uhp->uh_entry; uep != NULL; uep = nuep) | |
2520 { | |
2521 nuep = uep->ue_next; | |
2522 u_freeentry(uep, uep->ue_size); | |
2523 } | |
2524 | |
1415 | 2525 #ifdef U_DEBUG |
2526 uhp->uh_magic = 0; | |
2527 #endif | |
168 | 2528 U_FREE_LINE((char_u *)uhp); |
2529 --buf->b_u_numhead; | |
7 | 2530 } |
2531 | |
2532 /* | |
2533 * free entry 'uep' and 'n' lines in uep->ue_array[] | |
2534 */ | |
2535 static void | |
2536 u_freeentry(uep, n) | |
2537 u_entry_T *uep; | |
2538 long n; | |
2539 { | |
414 | 2540 while (n > 0) |
168 | 2541 U_FREE_LINE(uep->ue_array[--n]); |
359 | 2542 U_FREE_LINE((char_u *)uep->ue_array); |
1415 | 2543 #ifdef U_DEBUG |
2544 uep->ue_magic = 0; | |
2545 #endif | |
168 | 2546 U_FREE_LINE((char_u *)uep); |
7 | 2547 } |
2548 | |
2549 /* | |
2550 * invalidate the undo buffer; called when storage has already been released | |
2551 */ | |
2552 void | |
2553 u_clearall(buf) | |
2554 buf_T *buf; | |
2555 { | |
2556 buf->b_u_newhead = buf->b_u_oldhead = buf->b_u_curhead = NULL; | |
2557 buf->b_u_synced = TRUE; | |
2558 buf->b_u_numhead = 0; | |
2559 buf->b_u_line_ptr = NULL; | |
2560 buf->b_u_line_lnum = 0; | |
2561 } | |
2562 | |
2563 /* | |
2564 * save the line "lnum" for the "U" command | |
2565 */ | |
2566 void | |
2567 u_saveline(lnum) | |
2568 linenr_T lnum; | |
2569 { | |
2570 if (lnum == curbuf->b_u_line_lnum) /* line is already saved */ | |
2571 return; | |
356 | 2572 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) /* should never happen */ |
7 | 2573 return; |
2574 u_clearline(); | |
2575 curbuf->b_u_line_lnum = lnum; | |
2576 if (curwin->w_cursor.lnum == lnum) | |
2577 curbuf->b_u_line_colnr = curwin->w_cursor.col; | |
2578 else | |
2579 curbuf->b_u_line_colnr = 0; | |
2580 if ((curbuf->b_u_line_ptr = u_save_line(lnum)) == NULL) | |
2581 do_outofmem_msg((long_u)0); | |
2582 } | |
2583 | |
2584 /* | |
2585 * clear the line saved for the "U" command | |
2586 * (this is used externally for crossing a line while in insert mode) | |
2587 */ | |
2588 void | |
2589 u_clearline() | |
2590 { | |
2591 if (curbuf->b_u_line_ptr != NULL) | |
2592 { | |
168 | 2593 U_FREE_LINE(curbuf->b_u_line_ptr); |
7 | 2594 curbuf->b_u_line_ptr = NULL; |
2595 curbuf->b_u_line_lnum = 0; | |
2596 } | |
2597 } | |
2598 | |
2599 /* | |
2600 * Implementation of the "U" command. | |
2601 * Differentiation from vi: "U" can be undone with the next "U". | |
2602 * We also allow the cursor to be in another line. | |
2603 */ | |
2604 void | |
2605 u_undoline() | |
2606 { | |
2607 colnr_T t; | |
2608 char_u *oldp; | |
2609 | |
2610 if (undo_off) | |
2611 return; | |
2612 | |
1534 | 2613 if (curbuf->b_u_line_ptr == NULL |
2614 || curbuf->b_u_line_lnum > curbuf->b_ml.ml_line_count) | |
7 | 2615 { |
2616 beep_flush(); | |
2617 return; | |
2618 } | |
1534 | 2619 |
2620 /* first save the line for the 'u' command */ | |
7 | 2621 if (u_savecommon(curbuf->b_u_line_lnum - 1, |
2622 curbuf->b_u_line_lnum + 1, (linenr_T)0) == FAIL) | |
2623 return; | |
2624 oldp = u_save_line(curbuf->b_u_line_lnum); | |
2625 if (oldp == NULL) | |
2626 { | |
2627 do_outofmem_msg((long_u)0); | |
2628 return; | |
2629 } | |
2630 ml_replace(curbuf->b_u_line_lnum, curbuf->b_u_line_ptr, TRUE); | |
2631 changed_bytes(curbuf->b_u_line_lnum, 0); | |
168 | 2632 U_FREE_LINE(curbuf->b_u_line_ptr); |
7 | 2633 curbuf->b_u_line_ptr = oldp; |
2634 | |
2635 t = curbuf->b_u_line_colnr; | |
2636 if (curwin->w_cursor.lnum == curbuf->b_u_line_lnum) | |
2637 curbuf->b_u_line_colnr = curwin->w_cursor.col; | |
2638 curwin->w_cursor.col = t; | |
2639 curwin->w_cursor.lnum = curbuf->b_u_line_lnum; | |
1534 | 2640 check_cursor_col(); |
7 | 2641 } |
2642 | |
2643 /* | |
168 | 2644 * There are two implementations of the memory management for undo: |
2645 * 1. Use the standard malloc()/free() functions. | |
2646 * This should be fast for allocating memory, but when a buffer is | |
2647 * abandoned every single allocated chunk must be freed, which may be slow. | |
2648 * 2. Allocate larger blocks of memory and keep track of chunks ourselves. | |
2649 * This is fast for abandoning, but the use of linked lists is slow for | |
2650 * finding a free chunk. Esp. when a lot of lines are changed or deleted. | |
2651 * A bit of profiling showed that the first method is faster, especially when | |
2652 * making a large number of changes, under the condition that malloc()/free() | |
2653 * is implemented efficiently. | |
2654 */ | |
2655 #ifdef U_USE_MALLOC | |
2656 /* | |
2657 * Version of undo memory allocation using malloc()/free() | |
2658 * | |
2659 * U_FREE_LINE() and U_ALLOC_LINE() are macros that invoke vim_free() and | |
2660 * lalloc() directly. | |
2661 */ | |
2662 | |
2663 /* | |
2664 * Free all allocated memory blocks for the buffer 'buf'. | |
2665 */ | |
2666 void | |
2667 u_blockfree(buf) | |
2668 buf_T *buf; | |
2669 { | |
753 | 2670 while (buf->b_u_oldhead != NULL) |
758 | 2671 u_freeheader(buf, buf->b_u_oldhead, NULL); |
359 | 2672 U_FREE_LINE(buf->b_u_line_ptr); |
168 | 2673 } |
2674 | |
2675 #else | |
2676 /* | |
2677 * Storage allocation for the undo lines and blocks of the current file. | |
2678 * Version where Vim keeps track of the available memory. | |
7 | 2679 */ |
2680 | |
2681 /* | |
2682 * Memory is allocated in relatively large blocks. These blocks are linked | |
2683 * in the allocated block list, headed by curbuf->b_block_head. They are all | |
2684 * freed when abandoning a file, so we don't have to free every single line. | |
2685 * The list is kept sorted on memory address. | |
2686 * block_alloc() allocates a block. | |
2687 * m_blockfree() frees all blocks. | |
2688 * | |
2689 * The available chunks of memory are kept in free chunk lists. There is | |
2690 * one free list for each block of allocated memory. The list is kept sorted | |
2691 * on memory address. | |
2692 * u_alloc_line() gets a chunk from the free lists. | |
2693 * u_free_line() returns a chunk to the free lists. | |
2694 * curbuf->b_m_search points to the chunk before the chunk that was | |
2695 * freed/allocated the last time. | |
2696 * curbuf->b_mb_current points to the b_head where curbuf->b_m_search | |
2697 * points into the free list. | |
2698 * | |
2699 * | |
2700 * b_block_head /---> block #1 /---> block #2 | |
2701 * mb_next ---/ mb_next ---/ mb_next ---> NULL | |
2702 * mb_info mb_info mb_info | |
2703 * | | | | |
2704 * V V V | |
2705 * NULL free chunk #1.1 free chunk #2.1 | |
2706 * | | | |
2707 * V V | |
2708 * free chunk #1.2 NULL | |
2709 * | | |
2710 * V | |
2711 * NULL | |
2712 * | |
2713 * When a single free chunk list would have been used, it could take a lot | |
2714 * of time in u_free_line() to find the correct place to insert a chunk in the | |
2715 * free list. The single free list would become very long when many lines are | |
2716 * changed (e.g. with :%s/^M$//). | |
2717 */ | |
2718 | |
2719 /* | |
2720 * this blocksize is used when allocating new lines | |
2721 */ | |
2722 #define MEMBLOCKSIZE 2044 | |
2723 | |
2724 /* | |
2725 * The size field contains the size of the chunk, including the size field | |
2726 * itself. | |
2727 * | |
2728 * When the chunk is not in-use it is preceded with the m_info structure. | |
2729 * The m_next field links it in one of the free chunk lists. | |
2730 * | |
2731 * On most unix systems structures have to be longword (32 or 64 bit) aligned. | |
2732 * On most other systems they are short (16 bit) aligned. | |
2733 */ | |
2734 | |
2735 /* the structure definitions are now in structs.h */ | |
2736 | |
2737 #ifdef ALIGN_LONG | |
2738 /* size of m_size */ | |
2739 # define M_OFFSET (sizeof(long_u)) | |
2740 #else | |
2741 /* size of m_size */ | |
2742 # define M_OFFSET (sizeof(short_u)) | |
2743 #endif | |
2744 | |
168 | 2745 static char_u *u_blockalloc __ARGS((long_u)); |
2746 | |
7 | 2747 /* |
2748 * Allocate a block of memory and link it in the allocated block list. | |
2749 */ | |
2750 static char_u * | |
2751 u_blockalloc(size) | |
2752 long_u size; | |
2753 { | |
2754 mblock_T *p; | |
2755 mblock_T *mp, *next; | |
2756 | |
2757 p = (mblock_T *)lalloc(size + sizeof(mblock_T), FALSE); | |
2758 if (p != NULL) | |
2759 { | |
2760 /* Insert the block into the allocated block list, keeping it | |
2761 sorted on address. */ | |
2762 for (mp = &curbuf->b_block_head; | |
2763 (next = mp->mb_next) != NULL && next < p; | |
2764 mp = next) | |
2765 ; | |
2766 p->mb_next = next; /* link in block list */ | |
2767 p->mb_size = size; | |
168 | 2768 p->mb_maxsize = 0; /* nothing free yet */ |
7 | 2769 mp->mb_next = p; |
2770 p->mb_info.m_next = NULL; /* clear free list */ | |
2771 p->mb_info.m_size = 0; | |
2772 curbuf->b_mb_current = p; /* remember current block */ | |
2773 curbuf->b_m_search = NULL; | |
2774 p++; /* return usable memory */ | |
2775 } | |
2776 return (char_u *)p; | |
2777 } | |
2778 | |
2779 /* | |
2780 * free all allocated memory blocks for the buffer 'buf' | |
2781 */ | |
2782 void | |
2783 u_blockfree(buf) | |
2784 buf_T *buf; | |
2785 { | |
2786 mblock_T *p, *np; | |
2787 | |
2788 for (p = buf->b_block_head.mb_next; p != NULL; p = np) | |
2789 { | |
2790 np = p->mb_next; | |
2791 vim_free(p); | |
2792 } | |
2793 buf->b_block_head.mb_next = NULL; | |
2794 buf->b_m_search = NULL; | |
2795 buf->b_mb_current = NULL; | |
2796 } | |
2797 | |
2798 /* | |
2799 * Free a chunk of memory for the current buffer. | |
2800 * Insert the chunk into the correct free list, keeping it sorted on address. | |
2801 */ | |
2802 static void | |
2803 u_free_line(ptr, keep) | |
2804 char_u *ptr; | |
2805 int keep; /* don't free the block when it's empty */ | |
2806 { | |
2807 minfo_T *next; | |
2808 minfo_T *prev, *curr; | |
2809 minfo_T *mp; | |
2810 mblock_T *nextb; | |
2811 mblock_T *prevb; | |
168 | 2812 long_u maxsize; |
7 | 2813 |
2814 if (ptr == NULL || ptr == IObuff) | |
2815 return; /* illegal address can happen in out-of-memory situations */ | |
2816 | |
2817 mp = (minfo_T *)(ptr - M_OFFSET); | |
2818 | |
2819 /* find block where chunk could be a part off */ | |
2820 /* if we change curbuf->b_mb_current, curbuf->b_m_search is set to NULL */ | |
2821 if (curbuf->b_mb_current == NULL || mp < (minfo_T *)curbuf->b_mb_current) | |
2822 { | |
2823 curbuf->b_mb_current = curbuf->b_block_head.mb_next; | |
2824 curbuf->b_m_search = NULL; | |
2825 } | |
2826 if ((nextb = curbuf->b_mb_current->mb_next) != NULL | |
2827 && (minfo_T *)nextb < mp) | |
2828 { | |
2829 curbuf->b_mb_current = nextb; | |
2830 curbuf->b_m_search = NULL; | |
2831 } | |
2832 while ((nextb = curbuf->b_mb_current->mb_next) != NULL | |
2833 && (minfo_T *)nextb < mp) | |
2834 curbuf->b_mb_current = nextb; | |
2835 | |
2836 curr = NULL; | |
2837 /* | |
2838 * If mp is smaller than curbuf->b_m_search->m_next go to the start of | |
2839 * the free list | |
2840 */ | |
2841 if (curbuf->b_m_search == NULL || mp < (curbuf->b_m_search->m_next)) | |
2842 next = &(curbuf->b_mb_current->mb_info); | |
2843 else | |
2844 next = curbuf->b_m_search; | |
2845 /* | |
2846 * The following loop is executed very often. | |
2847 * Therefore it has been optimized at the cost of readability. | |
2848 * Keep it fast! | |
2849 */ | |
2850 #ifdef SLOW_BUT_EASY_TO_READ | |
2851 do | |
2852 { | |
2853 prev = curr; | |
2854 curr = next; | |
2855 next = next->m_next; | |
2856 } | |
2857 while (mp > next && next != NULL); | |
2858 #else | |
2859 do /* first, middle, last */ | |
2860 { | |
2861 prev = next->m_next; /* curr, next, prev */ | |
2862 if (prev == NULL || mp <= prev) | |
2863 { | |
2864 prev = curr; | |
2865 curr = next; | |
2866 next = next->m_next; | |
2867 break; | |
2868 } | |
2869 curr = prev->m_next; /* next, prev, curr */ | |
2870 if (curr == NULL || mp <= curr) | |
2871 { | |
2872 prev = next; | |
2873 curr = prev->m_next; | |
2874 next = curr->m_next; | |
2875 break; | |
2876 } | |
2877 next = curr->m_next; /* prev, curr, next */ | |
2878 } | |
2879 while (mp > next && next != NULL); | |
2880 #endif | |
2881 | |
2882 /* if *mp and *next are concatenated, join them into one chunk */ | |
2883 if ((char_u *)mp + mp->m_size == (char_u *)next) | |
2884 { | |
2885 mp->m_size += next->m_size; | |
2886 mp->m_next = next->m_next; | |
2887 } | |
2888 else | |
2889 mp->m_next = next; | |
168 | 2890 maxsize = mp->m_size; |
7 | 2891 |
2892 /* if *curr and *mp are concatenated, join them */ | |
2893 if (prev != NULL && (char_u *)curr + curr->m_size == (char_u *)mp) | |
2894 { | |
2895 curr->m_size += mp->m_size; | |
168 | 2896 maxsize = curr->m_size; |
7 | 2897 curr->m_next = mp->m_next; |
2898 curbuf->b_m_search = prev; | |
2899 } | |
2900 else | |
2901 { | |
2902 curr->m_next = mp; | |
2903 curbuf->b_m_search = curr; /* put curbuf->b_m_search before freed | |
2904 chunk */ | |
2905 } | |
2906 | |
2907 /* | |
1210 | 2908 * If the block only contains free memory now, release it. |
7 | 2909 */ |
2910 if (!keep && curbuf->b_mb_current->mb_size | |
2911 == curbuf->b_mb_current->mb_info.m_next->m_size) | |
2912 { | |
2913 /* Find the block before the current one to be able to unlink it from | |
2914 * the list of blocks. */ | |
2915 prevb = &curbuf->b_block_head; | |
2916 for (nextb = prevb->mb_next; nextb != curbuf->b_mb_current; | |
2917 nextb = nextb->mb_next) | |
2918 prevb = nextb; | |
2919 prevb->mb_next = nextb->mb_next; | |
2920 vim_free(nextb); | |
2921 curbuf->b_mb_current = NULL; | |
2922 curbuf->b_m_search = NULL; | |
2923 } | |
168 | 2924 else if (curbuf->b_mb_current->mb_maxsize < maxsize) |
2925 curbuf->b_mb_current->mb_maxsize = maxsize; | |
7 | 2926 } |
2927 | |
2928 /* | |
2929 * Allocate and initialize a new line structure with room for at least | |
2930 * 'size' characters plus a terminating NUL. | |
2931 */ | |
2932 static char_u * | |
2933 u_alloc_line(size) | |
2934 unsigned size; | |
2935 { | |
2936 minfo_T *mp, *mprev, *mp2; | |
2937 mblock_T *mbp; | |
2938 int size_align; | |
2939 | |
2940 /* | |
2941 * Add room for size field and trailing NUL byte. | |
2942 * Adjust for minimal size (must be able to store minfo_T | |
2943 * plus a trailing NUL, so the chunk can be released again) | |
2944 */ | |
2945 size += M_OFFSET + 1; | |
2946 if (size < sizeof(minfo_T) + 1) | |
2947 size = sizeof(minfo_T) + 1; | |
2948 | |
2949 /* | |
2950 * round size up for alignment | |
2951 */ | |
2952 size_align = (size + ALIGN_MASK) & ~ALIGN_MASK; | |
2953 | |
2954 /* | |
2955 * If curbuf->b_m_search is NULL (uninitialized free list) start at | |
2956 * curbuf->b_block_head | |
2957 */ | |
2958 if (curbuf->b_mb_current == NULL || curbuf->b_m_search == NULL) | |
2959 { | |
2960 curbuf->b_mb_current = &curbuf->b_block_head; | |
2961 curbuf->b_m_search = &(curbuf->b_block_head.mb_info); | |
2962 } | |
2963 | |
168 | 2964 /* Search for a block with enough space. */ |
7 | 2965 mbp = curbuf->b_mb_current; |
168 | 2966 while (mbp->mb_maxsize < size_align) |
7 | 2967 { |
168 | 2968 if (mbp->mb_next != NULL) |
7 | 2969 mbp = mbp->mb_next; |
2970 else | |
2971 mbp = &curbuf->b_block_head; | |
168 | 2972 if (mbp == curbuf->b_mb_current) |
7 | 2973 { |
168 | 2974 int n = (size_align > (MEMBLOCKSIZE / 4) |
2975 ? size_align : MEMBLOCKSIZE); | |
7 | 2976 |
168 | 2977 /* Back where we started in block list: need to add a new block |
2978 * with enough space. */ | |
2979 mp = (minfo_T *)u_blockalloc((long_u)n); | |
2980 if (mp == NULL) | |
2981 return (NULL); | |
2982 mp->m_size = n; | |
2983 u_free_line((char_u *)mp + M_OFFSET, TRUE); | |
2984 mbp = curbuf->b_mb_current; | |
2985 break; | |
2986 } | |
2987 } | |
2988 if (mbp != curbuf->b_mb_current) | |
2989 curbuf->b_m_search = &(mbp->mb_info); | |
2990 | |
2991 /* In this block find a chunk with enough space. */ | |
2992 mprev = curbuf->b_m_search; | |
2993 mp = curbuf->b_m_search->m_next; | |
407 | 2994 for (;;) |
168 | 2995 { |
2996 if (mp == NULL) /* at end of the list */ | |
2997 mp = &(mbp->mb_info); /* wrap around to begin */ | |
2998 if (mp->m_size >= size) | |
2999 break; | |
3000 if (mp == curbuf->b_m_search) | |
3001 { | |
3002 /* back where we started in free chunk list: "cannot happen" */ | |
3003 EMSG2(_(e_intern2), "u_alloc_line()"); | |
3004 return NULL; | |
7 | 3005 } |
3006 mprev = mp; | |
168 | 3007 mp = mp->m_next; |
7 | 3008 } |
3009 | |
168 | 3010 /* when using the largest chunk adjust mb_maxsize */ |
3011 if (mp->m_size >= mbp->mb_maxsize) | |
3012 mbp->mb_maxsize = 0; | |
3013 | |
7 | 3014 /* if the chunk we found is large enough, split it up in two */ |
3015 if ((long)mp->m_size - size_align >= (long)(sizeof(minfo_T) + 1)) | |
3016 { | |
3017 mp2 = (minfo_T *)((char_u *)mp + size_align); | |
3018 mp2->m_size = mp->m_size - size_align; | |
3019 mp2->m_next = mp->m_next; | |
3020 mprev->m_next = mp2; | |
3021 mp->m_size = size_align; | |
3022 } | |
3023 else /* remove *mp from the free list */ | |
3024 { | |
3025 mprev->m_next = mp->m_next; | |
3026 } | |
3027 curbuf->b_m_search = mprev; | |
3028 curbuf->b_mb_current = mbp; | |
3029 | |
168 | 3030 /* If using the largest chunk need to find the new largest chunk */ |
3031 if (mbp->mb_maxsize == 0) | |
3032 for (mp2 = &(mbp->mb_info); mp2 != NULL; mp2 = mp2->m_next) | |
3033 if (mbp->mb_maxsize < mp2->m_size) | |
3034 mbp->mb_maxsize = mp2->m_size; | |
3035 | |
7 | 3036 mp = (minfo_T *)((char_u *)mp + M_OFFSET); |
3037 *(char_u *)mp = NUL; /* set the first byte to NUL */ | |
3038 | |
3039 return ((char_u *)mp); | |
3040 } | |
168 | 3041 #endif |
7 | 3042 |
3043 /* | |
3044 * u_save_line(): allocate memory with u_alloc_line() and copy line 'lnum' | |
3045 * into it. | |
3046 */ | |
3047 static char_u * | |
3048 u_save_line(lnum) | |
3049 linenr_T lnum; | |
3050 { | |
3051 char_u *src; | |
3052 char_u *dst; | |
3053 unsigned len; | |
3054 | |
3055 src = ml_get(lnum); | |
3056 len = (unsigned)STRLEN(src); | |
168 | 3057 if ((dst = U_ALLOC_LINE(len)) != NULL) |
7 | 3058 mch_memmove(dst, src, (size_t)(len + 1)); |
3059 return (dst); | |
3060 } | |
3061 | |
3062 /* | |
3063 * Check if the 'modified' flag is set, or 'ff' has changed (only need to | |
3064 * check the first character, because it can only be "dos", "unix" or "mac"). | |
3065 * "nofile" and "scratch" type buffers are considered to always be unchanged. | |
3066 */ | |
3067 int | |
3068 bufIsChanged(buf) | |
3069 buf_T *buf; | |
3070 { | |
3071 return | |
3072 #ifdef FEAT_QUICKFIX | |
3073 !bt_dontwrite(buf) && | |
3074 #endif | |
3075 (buf->b_changed || file_ff_differs(buf)); | |
3076 } | |
3077 | |
3078 int | |
3079 curbufIsChanged() | |
3080 { | |
3081 return | |
3082 #ifdef FEAT_QUICKFIX | |
3083 !bt_dontwrite(curbuf) && | |
3084 #endif | |
3085 (curbuf->b_changed || file_ff_differs(curbuf)); | |
3086 } |