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