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