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