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