Mercurial > vim
annotate src/undo.c @ 2222:271a5907f944 vim73
Fix for using ":wundo foo" when foo is an empty file.
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Tue, 25 May 2010 22:09:21 +0200 |
parents | c81f0a037059 |
children | 81b83a19e127 |
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 { |
2222
271a5907f944
Fix for using ":wundo foo" when foo is an empty file.
Bram Moolenaar <bram@vim.org>
parents:
2219
diff
changeset
|
1289 char_u buf[2]; |
271a5907f944
Fix for using ":wundo foo" when foo is an empty file.
Bram Moolenaar <bram@vim.org>
parents:
2219
diff
changeset
|
1290 int len; |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1291 |
2222
271a5907f944
Fix for using ":wundo foo" when foo is an empty file.
Bram Moolenaar <bram@vim.org>
parents:
2219
diff
changeset
|
1292 len = vim_read(fd, buf, 2); |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1293 close(fd); |
2222
271a5907f944
Fix for using ":wundo foo" when foo is an empty file.
Bram Moolenaar <bram@vim.org>
parents:
2219
diff
changeset
|
1294 if (len < 2 || (buf[0] << 8) + buf[1] != UF_START_MAGIC) |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1295 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1296 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
|
1297 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
|
1298 file_name); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1299 goto theend; |
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 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1303 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
|
1304 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1305 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1306 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
|
1307 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
|
1308 (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
|
1309 if (fd < 0) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1310 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1311 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
|
1312 goto theend; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1313 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1314 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
|
1315 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
|
1316 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1317 #ifdef UNIX |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1318 /* |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1319 * 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
|
1320 * 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
|
1321 * 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
|
1322 */ |
2218
695ceebf17ca
Fix: :wundo didn't work in a buffer without a name.
Bram Moolenaar <bram@vim.org>
parents:
2217
diff
changeset
|
1323 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
|
1324 && 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
|
1325 # 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
|
1326 && 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
|
1327 # endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1328 ) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1329 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
|
1330 # 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
|
1331 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
|
1332 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
|
1333 # endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1334 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1335 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1336 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
|
1337 if (fp == NULL) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1338 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1339 EMSG2(_(e_not_open), file_name); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1340 close(fd); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1341 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
|
1342 goto theend; |
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 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1345 /* 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
|
1346 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
|
1347 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
|
1348 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1349 /* 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
|
1350 * 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
|
1351 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
|
1352 goto write_error; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1353 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
|
1354 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1355 /* Begin undo data for U */ |
2217
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
1356 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
|
1357 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
|
1358 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
|
1359 (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
|
1360 goto write_error; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1361 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1362 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
|
1363 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
|
1364 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1365 /* 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
|
1366 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
|
1367 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
|
1368 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1369 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
|
1370 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
|
1371 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1372 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
|
1373 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
|
1374 |
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_numhead, 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_last, 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_cur, 4); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1378 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
|
1379 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1380 /* 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
|
1381 mark = ++lastmark; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1382 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
|
1383 while (uhp != NULL) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1384 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1385 /* 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
|
1386 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
|
1387 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1388 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
|
1389 goto write_error; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1390 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1391 put_bytes(fp, (long_u)((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
|
1392 ? 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
|
1393 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
|
1394 ? 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
|
1395 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
|
1396 ? 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
|
1397 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
|
1398 ? 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
|
1399 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
|
1400 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
|
1401 #ifdef FEAT_VIRTUALEDIT |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1402 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
|
1403 #else |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1404 put_bytes(fp, (long_u)0, 4); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1405 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1406 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
|
1407 /* 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
|
1408 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
|
1409 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1410 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
|
1411 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1412 #ifdef FEAT_VISUAL |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1413 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
|
1414 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1415 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
|
1416 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1417 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
|
1418 while (uep != NULL) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1419 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1420 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
|
1421 goto write_error; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1422 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
|
1423 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1424 /* 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
|
1425 uep_len = -1; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1426 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
|
1427 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
|
1428 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1429 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1430 /* 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
|
1431 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
|
1432 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
|
1433 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
|
1434 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
|
1435 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
|
1436 && 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
|
1437 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
|
1438 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
|
1439 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
|
1440 else |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1441 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
|
1442 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1443 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1444 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
|
1445 write_ok = TRUE; |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1446 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1447 write_error: |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1448 fclose(fp); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1449 if (!write_ok) |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1450 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
|
1451 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1452 #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
|
1453 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
|
1454 (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
|
1455 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1456 #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
|
1457 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
|
1458 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1459 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
|
1460 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1461 /* 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
|
1462 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
|
1463 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
|
1464 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1465 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1466 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1467 theend: |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1468 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
|
1469 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
|
1470 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1471 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1472 #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
|
1473 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1474 |
7 | 1475 /* |
1476 * If 'cpoptions' contains 'u': Undo the previous undo or redo (vi compatible). | |
1477 * If 'cpoptions' does not contain 'u': Always undo. | |
1478 */ | |
1479 void | |
1480 u_undo(count) | |
1481 int count; | |
1482 { | |
1483 /* | |
1484 * If we get an undo command while executing a macro, we behave like the | |
1485 * original vi. If this happens twice in one macro the result will not | |
1486 * be compatible. | |
1487 */ | |
1488 if (curbuf->b_u_synced == FALSE) | |
1489 { | |
825 | 1490 u_sync(TRUE); |
7 | 1491 count = 1; |
1492 } | |
1493 | |
1494 if (vim_strchr(p_cpo, CPO_UNDO) == NULL) | |
1495 undo_undoes = TRUE; | |
1496 else | |
1497 undo_undoes = !undo_undoes; | |
1498 u_doit(count); | |
1499 } | |
1500 | |
1501 /* | |
1502 * If 'cpoptions' contains 'u': Repeat the previous undo or redo. | |
1503 * If 'cpoptions' does not contain 'u': Always redo. | |
1504 */ | |
1505 void | |
1506 u_redo(count) | |
1507 int count; | |
1508 { | |
1509 if (vim_strchr(p_cpo, CPO_UNDO) == NULL) | |
1510 undo_undoes = FALSE; | |
1511 u_doit(count); | |
1512 } | |
1513 | |
1514 /* | |
1515 * Undo or redo, depending on 'undo_undoes', 'count' times. | |
1516 */ | |
1517 static void | |
777 | 1518 u_doit(startcount) |
1519 int startcount; | |
7 | 1520 { |
777 | 1521 int count = startcount; |
1522 | |
632 | 1523 if (!undo_allowed()) |
7 | 1524 return; |
1525 | |
1526 u_newcount = 0; | |
1527 u_oldcount = 0; | |
179 | 1528 if (curbuf->b_ml.ml_flags & ML_EMPTY) |
1529 u_oldcount = -1; | |
7 | 1530 while (count--) |
1531 { | |
1532 if (undo_undoes) | |
1533 { | |
1534 if (curbuf->b_u_curhead == NULL) /* first undo */ | |
1535 curbuf->b_u_curhead = curbuf->b_u_newhead; | |
1536 else if (p_ul > 0) /* multi level undo */ | |
1537 /* get next undo */ | |
1538 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_next; | |
1539 /* nothing to undo */ | |
1540 if (curbuf->b_u_numhead == 0 || curbuf->b_u_curhead == NULL) | |
1541 { | |
1542 /* stick curbuf->b_u_curhead at end */ | |
1543 curbuf->b_u_curhead = curbuf->b_u_oldhead; | |
1544 beep_flush(); | |
777 | 1545 if (count == startcount - 1) |
1546 { | |
1547 MSG(_("Already at oldest change")); | |
1548 return; | |
1549 } | |
7 | 1550 break; |
1551 } | |
1552 | |
777 | 1553 u_undoredo(TRUE); |
7 | 1554 } |
1555 else | |
1556 { | |
1557 if (curbuf->b_u_curhead == NULL || p_ul <= 0) | |
1558 { | |
1559 beep_flush(); /* nothing to redo */ | |
777 | 1560 if (count == startcount - 1) |
1561 { | |
1562 MSG(_("Already at newest change")); | |
1563 return; | |
1564 } | |
7 | 1565 break; |
1566 } | |
1567 | |
777 | 1568 u_undoredo(FALSE); |
753 | 1569 |
1570 /* Advance for next redo. Set "newhead" when at the end of the | |
1571 * redoable changes. */ | |
1572 if (curbuf->b_u_curhead->uh_prev == NULL) | |
1573 curbuf->b_u_newhead = curbuf->b_u_curhead; | |
7 | 1574 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_prev; |
1575 } | |
1576 } | |
798 | 1577 u_undo_end(undo_undoes, FALSE); |
753 | 1578 } |
1579 | |
1580 /* | |
1581 * Undo or redo over the timeline. | |
1582 * When "step" is negative go back in time, otherwise goes forward in time. | |
758 | 1583 * When "sec" is FALSE make "step" steps, when "sec" is TRUE use "step" as |
1584 * seconds. | |
772 | 1585 * When "absolute" is TRUE use "step" as the sequence number to jump to. |
1586 * "sec" must be FALSE then. | |
753 | 1587 */ |
1588 void | |
772 | 1589 undo_time(step, sec, absolute) |
758 | 1590 long step; |
1591 int sec; | |
772 | 1592 int absolute; |
753 | 1593 { |
1594 long target; | |
1595 long closest; | |
758 | 1596 long closest_start; |
1597 long closest_seq = 0; | |
1598 long val; | |
753 | 1599 u_header_T *uhp; |
1600 u_header_T *last; | |
1601 int mark; | |
1602 int nomark; | |
1603 int round; | |
758 | 1604 int dosec = sec; |
1605 int above = FALSE; | |
794 | 1606 int did_undo = TRUE; |
753 | 1607 |
766 | 1608 /* First make sure the current undoable change is synced. */ |
1609 if (curbuf->b_u_synced == FALSE) | |
825 | 1610 u_sync(TRUE); |
766 | 1611 |
753 | 1612 u_newcount = 0; |
1613 u_oldcount = 0; | |
179 | 1614 if (curbuf->b_ml.ml_flags & ML_EMPTY) |
753 | 1615 u_oldcount = -1; |
1616 | |
777 | 1617 /* "target" is the node below which we want to be. |
1618 * Init "closest" to a value we can't reach. */ | |
772 | 1619 if (absolute) |
1620 { | |
1621 target = step; | |
777 | 1622 closest = -1; |
753 | 1623 } |
1624 else | |
1625 { | |
777 | 1626 /* When doing computations with time_t subtract starttime, because |
1627 * time_t converted to a long may result in a wrong number. */ | |
758 | 1628 if (sec) |
777 | 1629 target = (long)(curbuf->b_u_seq_time - starttime) + step; |
1630 else | |
1631 target = curbuf->b_u_seq_cur + step; | |
1632 if (step < 0) | |
758 | 1633 { |
777 | 1634 if (target < 0) |
1635 target = 0; | |
1636 closest = -1; | |
758 | 1637 } |
1638 else | |
1639 { | |
777 | 1640 if (sec) |
798 | 1641 closest = (long)(time(NULL) - starttime + 1); |
777 | 1642 else |
1643 closest = curbuf->b_u_seq_last + 2; | |
1644 if (target >= closest) | |
1645 target = closest - 1; | |
758 | 1646 } |
753 | 1647 } |
758 | 1648 closest_start = closest; |
777 | 1649 closest_seq = curbuf->b_u_seq_cur; |
753 | 1650 |
758 | 1651 /* |
1652 * May do this twice: | |
753 | 1653 * 1. Search for "target", update "closest" to the best match found. |
758 | 1654 * 2. If "target" not found search for "closest". |
1655 * | |
1656 * When using the closest time we use the sequence number in the second | |
1657 * round, because there may be several entries with the same time. | |
1658 */ | |
753 | 1659 for (round = 1; round <= 2; ++round) |
1660 { | |
1661 /* Find the path from the current state to where we want to go. The | |
1662 * desired state can be anywhere in the undo tree, need to go all over | |
1663 * it. We put "nomark" in uh_walk where we have been without success, | |
1664 * "mark" where it could possibly be. */ | |
1665 mark = ++lastmark; | |
1666 nomark = ++lastmark; | |
1667 | |
1668 if (curbuf->b_u_curhead == NULL) /* at leaf of the tree */ | |
1669 uhp = curbuf->b_u_newhead; | |
1670 else | |
1671 uhp = curbuf->b_u_curhead; | |
1672 | |
1673 while (uhp != NULL) | |
1674 { | |
1675 uhp->uh_walk = mark; | |
798 | 1676 val = (long)(dosec ? (uhp->uh_time - starttime) : uhp->uh_seq); |
753 | 1677 |
758 | 1678 if (round == 1) |
1679 { | |
1680 /* Remember the header that is closest to the target. | |
1681 * It must be at least in the right direction (checked with | |
777 | 1682 * "b_u_seq_cur"). When the timestamp is equal find the |
758 | 1683 * highest/lowest sequence number. */ |
777 | 1684 if ((step < 0 ? uhp->uh_seq <= curbuf->b_u_seq_cur |
1685 : uhp->uh_seq > curbuf->b_u_seq_cur) | |
1686 && ((dosec && val == closest) | |
1687 ? (step < 0 | |
1688 ? uhp->uh_seq < closest_seq | |
1689 : uhp->uh_seq > closest_seq) | |
1690 : closest == closest_start | |
1691 || (val > target | |
1692 ? (closest > target | |
1693 ? val - target <= closest - target | |
1694 : val - target <= target - closest) | |
1695 : (closest > target | |
1696 ? target - val <= closest - target | |
1697 : target - val <= target - closest)))) | |
758 | 1698 { |
1699 closest = val; | |
1700 closest_seq = uhp->uh_seq; | |
1701 } | |
1702 } | |
1703 | |
1704 /* Quit searching when we found a match. But when searching for a | |
1705 * time we need to continue looking for the best uh_seq. */ | |
1706 if (target == val && !dosec) | |
1707 break; | |
753 | 1708 |
1709 /* go down in the tree if we haven't been there */ | |
1710 if (uhp->uh_prev != NULL && uhp->uh_prev->uh_walk != nomark | |
1711 && uhp->uh_prev->uh_walk != mark) | |
1712 uhp = uhp->uh_prev; | |
1713 | |
1714 /* go to alternate branch if we haven't been there */ | |
1715 else if (uhp->uh_alt_next != NULL | |
1716 && uhp->uh_alt_next->uh_walk != nomark | |
1717 && uhp->uh_alt_next->uh_walk != mark) | |
1718 uhp = uhp->uh_alt_next; | |
1719 | |
1720 /* go up in the tree if we haven't been there and we are at the | |
1721 * start of alternate branches */ | |
1722 else if (uhp->uh_next != NULL && uhp->uh_alt_prev == NULL | |
1723 && uhp->uh_next->uh_walk != nomark | |
1724 && uhp->uh_next->uh_walk != mark) | |
798 | 1725 { |
1726 /* If still at the start we don't go through this change. */ | |
1727 if (uhp == curbuf->b_u_curhead) | |
1728 uhp->uh_walk = nomark; | |
753 | 1729 uhp = uhp->uh_next; |
798 | 1730 } |
753 | 1731 |
1732 else | |
1733 { | |
1734 /* need to backtrack; mark this node as useless */ | |
1735 uhp->uh_walk = nomark; | |
1736 if (uhp->uh_alt_prev != NULL) | |
1737 uhp = uhp->uh_alt_prev; | |
1738 else | |
1739 uhp = uhp->uh_next; | |
1740 } | |
1741 } | |
1742 | |
1743 if (uhp != NULL) /* found it */ | |
1744 break; | |
772 | 1745 |
1746 if (absolute) | |
1747 { | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1748 EMSGN(_("E830: Undo number %ld not found"), step); |
772 | 1749 return; |
1750 } | |
1751 | |
758 | 1752 if (closest == closest_start) |
753 | 1753 { |
758 | 1754 if (step < 0) |
1755 MSG(_("Already at oldest change")); | |
1756 else | |
1757 MSG(_("Already at newest change")); | |
753 | 1758 return; |
1759 } | |
1760 | |
758 | 1761 target = closest_seq; |
1762 dosec = FALSE; | |
1763 if (step < 0) | |
1764 above = TRUE; /* stop above the header */ | |
753 | 1765 } |
1766 | |
1767 /* If we found it: Follow the path to go to where we want to be. */ | |
1768 if (uhp != NULL) | |
1769 { | |
1770 /* | |
1771 * First go up the tree as much as needed. | |
1772 */ | |
1773 for (;;) | |
1774 { | |
1775 uhp = curbuf->b_u_curhead; | |
1776 if (uhp == NULL) | |
1777 uhp = curbuf->b_u_newhead; | |
1778 else | |
1779 uhp = uhp->uh_next; | |
777 | 1780 if (uhp == NULL || uhp->uh_walk != mark |
1781 || (uhp->uh_seq == target && !above)) | |
753 | 1782 break; |
1783 curbuf->b_u_curhead = uhp; | |
777 | 1784 u_undoredo(TRUE); |
753 | 1785 uhp->uh_walk = nomark; /* don't go back down here */ |
1786 } | |
1787 | |
1788 /* | |
758 | 1789 * And now go down the tree (redo), branching off where needed. |
753 | 1790 */ |
1791 uhp = curbuf->b_u_curhead; | |
777 | 1792 while (uhp != NULL) |
753 | 1793 { |
1056 | 1794 /* Go back to the first branch with a mark. */ |
1795 while (uhp->uh_alt_prev != NULL | |
1796 && uhp->uh_alt_prev->uh_walk == mark) | |
1797 uhp = uhp->uh_alt_prev; | |
1798 | |
753 | 1799 /* Find the last branch with a mark, that's the one. */ |
1800 last = uhp; | |
1801 while (last->uh_alt_next != NULL | |
1802 && last->uh_alt_next->uh_walk == mark) | |
1803 last = last->uh_alt_next; | |
1804 if (last != uhp) | |
1805 { | |
1806 /* Make the used branch the first entry in the list of | |
1807 * alternatives to make "u" and CTRL-R take this branch. */ | |
1056 | 1808 while (uhp->uh_alt_prev != NULL) |
1809 uhp = uhp->uh_alt_prev; | |
753 | 1810 if (last->uh_alt_next != NULL) |
1811 last->uh_alt_next->uh_alt_prev = last->uh_alt_prev; | |
1812 last->uh_alt_prev->uh_alt_next = last->uh_alt_next; | |
1813 last->uh_alt_prev = NULL; | |
1814 last->uh_alt_next = uhp; | |
1815 uhp->uh_alt_prev = last; | |
1816 | |
1817 uhp = last; | |
777 | 1818 if (uhp->uh_next != NULL) |
1819 uhp->uh_next->uh_prev = uhp; | |
753 | 1820 } |
758 | 1821 curbuf->b_u_curhead = uhp; |
753 | 1822 |
1823 if (uhp->uh_walk != mark) | |
1824 break; /* must have reached the target */ | |
1825 | |
758 | 1826 /* Stop when going backwards in time and didn't find the exact |
1827 * header we were looking for. */ | |
1828 if (uhp->uh_seq == target && above) | |
798 | 1829 { |
1830 curbuf->b_u_seq_cur = target - 1; | |
758 | 1831 break; |
798 | 1832 } |
758 | 1833 |
777 | 1834 u_undoredo(FALSE); |
753 | 1835 |
1836 /* Advance "curhead" to below the header we last used. If it | |
1837 * becomes NULL then we need to set "newhead" to this leaf. */ | |
1838 if (uhp->uh_prev == NULL) | |
1839 curbuf->b_u_newhead = uhp; | |
1840 curbuf->b_u_curhead = uhp->uh_prev; | |
794 | 1841 did_undo = FALSE; |
753 | 1842 |
1843 if (uhp->uh_seq == target) /* found it! */ | |
1844 break; | |
1845 | |
1846 uhp = uhp->uh_prev; | |
1847 if (uhp == NULL || uhp->uh_walk != mark) | |
1848 { | |
777 | 1849 /* Need to redo more but can't find it... */ |
753 | 1850 EMSG2(_(e_intern2), "undo_time()"); |
1851 break; | |
1852 } | |
1853 } | |
1854 } | |
798 | 1855 u_undo_end(did_undo, absolute); |
7 | 1856 } |
1857 | |
1858 /* | |
1859 * u_undoredo: common code for undo and redo | |
1860 * | |
1861 * The lines in the file are replaced by the lines in the entry list at | |
1862 * curbuf->b_u_curhead. The replaced lines in the file are saved in the entry | |
1863 * list for the next undo/redo. | |
777 | 1864 * |
1865 * When "undo" is TRUE we go up in the tree, when FALSE we go down. | |
7 | 1866 */ |
1867 static void | |
777 | 1868 u_undoredo(undo) |
1869 int undo; | |
7 | 1870 { |
1871 char_u **newarray = NULL; | |
1872 linenr_T oldsize; | |
1873 linenr_T newsize; | |
1874 linenr_T top, bot; | |
1875 linenr_T lnum; | |
1876 linenr_T newlnum = MAXLNUM; | |
1877 long i; | |
1878 u_entry_T *uep, *nuep; | |
1879 u_entry_T *newlist = NULL; | |
1880 int old_flags; | |
1881 int new_flags; | |
1882 pos_T namedm[NMARKS]; | |
692 | 1883 #ifdef FEAT_VISUAL |
1884 visualinfo_T visualinfo; | |
1885 #endif | |
7 | 1886 int empty_buffer; /* buffer became empty */ |
758 | 1887 u_header_T *curhead = curbuf->b_u_curhead; |
7 | 1888 |
1415 | 1889 #ifdef U_DEBUG |
1890 u_check(FALSE); | |
1891 #endif | |
758 | 1892 old_flags = curhead->uh_flags; |
7 | 1893 new_flags = (curbuf->b_changed ? UH_CHANGED : 0) + |
1894 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0); | |
1895 setpcmark(); | |
1896 | |
1897 /* | |
1898 * save marks before undo/redo | |
1899 */ | |
1900 mch_memmove(namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS); | |
692 | 1901 #ifdef FEAT_VISUAL |
1902 visualinfo = curbuf->b_visual; | |
1903 #endif | |
7 | 1904 curbuf->b_op_start.lnum = curbuf->b_ml.ml_line_count; |
1905 curbuf->b_op_start.col = 0; | |
1906 curbuf->b_op_end.lnum = 0; | |
1907 curbuf->b_op_end.col = 0; | |
1908 | |
758 | 1909 for (uep = curhead->uh_entry; uep != NULL; uep = nuep) |
7 | 1910 { |
1911 top = uep->ue_top; | |
1912 bot = uep->ue_bot; | |
1913 if (bot == 0) | |
1914 bot = curbuf->b_ml.ml_line_count + 1; | |
1915 if (top > curbuf->b_ml.ml_line_count || top >= bot | |
1916 || bot > curbuf->b_ml.ml_line_count + 1) | |
1917 { | |
1918 EMSG(_("E438: u_undo: line numbers wrong")); | |
1919 changed(); /* don't want UNCHANGED now */ | |
1920 return; | |
1921 } | |
1922 | |
1923 oldsize = bot - top - 1; /* number of lines before undo */ | |
1924 newsize = uep->ue_size; /* number of lines after undo */ | |
1925 | |
1926 if (top < newlnum) | |
1927 { | |
1928 /* If the saved cursor is somewhere in this undo block, move it to | |
1929 * the remembered position. Makes "gwap" put the cursor back | |
1930 * where it was. */ | |
758 | 1931 lnum = curhead->uh_cursor.lnum; |
7 | 1932 if (lnum >= top && lnum <= top + newsize + 1) |
1933 { | |
758 | 1934 curwin->w_cursor = curhead->uh_cursor; |
7 | 1935 newlnum = curwin->w_cursor.lnum - 1; |
1936 } | |
1937 else | |
1938 { | |
1939 /* Use the first line that actually changed. Avoids that | |
1940 * undoing auto-formatting puts the cursor in the previous | |
1941 * line. */ | |
1942 for (i = 0; i < newsize && i < oldsize; ++i) | |
1943 if (STRCMP(uep->ue_array[i], ml_get(top + 1 + i)) != 0) | |
1944 break; | |
1945 if (i == newsize && newlnum == MAXLNUM && uep->ue_next == NULL) | |
1946 { | |
1947 newlnum = top; | |
1948 curwin->w_cursor.lnum = newlnum + 1; | |
1949 } | |
1950 else if (i < newsize) | |
1951 { | |
1952 newlnum = top + i; | |
1953 curwin->w_cursor.lnum = newlnum + 1; | |
1954 } | |
1955 } | |
1956 } | |
1957 | |
1958 empty_buffer = FALSE; | |
1959 | |
1960 /* delete the lines between top and bot and save them in newarray */ | |
168 | 1961 if (oldsize > 0) |
7 | 1962 { |
168 | 1963 if ((newarray = (char_u **)U_ALLOC_LINE( |
7 | 1964 (unsigned)(sizeof(char_u *) * oldsize))) == NULL) |
1965 { | |
1966 do_outofmem_msg((long_u)(sizeof(char_u *) * oldsize)); | |
1967 /* | |
1968 * We have messed up the entry list, repair is impossible. | |
1969 * we have to free the rest of the list. | |
1970 */ | |
1971 while (uep != NULL) | |
1972 { | |
1973 nuep = uep->ue_next; | |
1974 u_freeentry(uep, uep->ue_size); | |
1975 uep = nuep; | |
1976 } | |
1977 break; | |
1978 } | |
1979 /* delete backwards, it goes faster in most cases */ | |
1980 for (lnum = bot - 1, i = oldsize; --i >= 0; --lnum) | |
1981 { | |
1982 /* what can we do when we run out of memory? */ | |
1983 if ((newarray[i] = u_save_line(lnum)) == NULL) | |
1984 do_outofmem_msg((long_u)0); | |
1985 /* remember we deleted the last line in the buffer, and a | |
1986 * dummy empty line will be inserted */ | |
1987 if (curbuf->b_ml.ml_line_count == 1) | |
1988 empty_buffer = TRUE; | |
1989 ml_delete(lnum, FALSE); | |
1990 } | |
1991 } | |
414 | 1992 else |
1993 newarray = NULL; | |
7 | 1994 |
1995 /* insert the lines in u_array between top and bot */ | |
1996 if (newsize) | |
1997 { | |
1998 for (lnum = top, i = 0; i < newsize; ++i, ++lnum) | |
1999 { | |
2000 /* | |
2001 * If the file is empty, there is an empty line 1 that we | |
2002 * should get rid of, by replacing it with the new line | |
2003 */ | |
2004 if (empty_buffer && lnum == 0) | |
2005 ml_replace((linenr_T)1, uep->ue_array[i], TRUE); | |
2006 else | |
2007 ml_append(lnum, uep->ue_array[i], (colnr_T)0, FALSE); | |
168 | 2008 U_FREE_LINE(uep->ue_array[i]); |
7 | 2009 } |
168 | 2010 U_FREE_LINE((char_u *)uep->ue_array); |
7 | 2011 } |
2012 | |
2013 /* adjust marks */ | |
2014 if (oldsize != newsize) | |
2015 { | |
2016 mark_adjust(top + 1, top + oldsize, (long)MAXLNUM, | |
2017 (long)newsize - (long)oldsize); | |
2018 if (curbuf->b_op_start.lnum > top + oldsize) | |
2019 curbuf->b_op_start.lnum += newsize - oldsize; | |
2020 if (curbuf->b_op_end.lnum > top + oldsize) | |
2021 curbuf->b_op_end.lnum += newsize - oldsize; | |
2022 } | |
2023 | |
2024 changed_lines(top + 1, 0, bot, newsize - oldsize); | |
2025 | |
2026 /* set '[ and '] mark */ | |
2027 if (top + 1 < curbuf->b_op_start.lnum) | |
2028 curbuf->b_op_start.lnum = top + 1; | |
2029 if (newsize == 0 && top + 1 > curbuf->b_op_end.lnum) | |
2030 curbuf->b_op_end.lnum = top + 1; | |
2031 else if (top + newsize > curbuf->b_op_end.lnum) | |
2032 curbuf->b_op_end.lnum = top + newsize; | |
2033 | |
2034 u_newcount += newsize; | |
2035 u_oldcount += oldsize; | |
2036 uep->ue_size = oldsize; | |
2037 uep->ue_array = newarray; | |
2038 uep->ue_bot = top + newsize + 1; | |
2039 | |
2040 /* | |
2041 * insert this entry in front of the new entry list | |
2042 */ | |
2043 nuep = uep->ue_next; | |
2044 uep->ue_next = newlist; | |
2045 newlist = uep; | |
2046 } | |
2047 | |
758 | 2048 curhead->uh_entry = newlist; |
2049 curhead->uh_flags = new_flags; | |
7 | 2050 if ((old_flags & UH_EMPTYBUF) && bufempty()) |
2051 curbuf->b_ml.ml_flags |= ML_EMPTY; | |
2052 if (old_flags & UH_CHANGED) | |
2053 changed(); | |
2054 else | |
33 | 2055 #ifdef FEAT_NETBEANS_INTG |
2056 /* per netbeans undo rules, keep it as modified */ | |
2057 if (!isNetbeansModified(curbuf)) | |
2058 #endif | |
7 | 2059 unchanged(curbuf, FALSE); |
2060 | |
2061 /* | |
2062 * restore marks from before undo/redo | |
2063 */ | |
2064 for (i = 0; i < NMARKS; ++i) | |
758 | 2065 if (curhead->uh_namedm[i].lnum != 0) |
7 | 2066 { |
758 | 2067 curbuf->b_namedm[i] = curhead->uh_namedm[i]; |
2068 curhead->uh_namedm[i] = namedm[i]; | |
7 | 2069 } |
692 | 2070 #ifdef FEAT_VISUAL |
758 | 2071 if (curhead->uh_visual.vi_start.lnum != 0) |
692 | 2072 { |
758 | 2073 curbuf->b_visual = curhead->uh_visual; |
2074 curhead->uh_visual = visualinfo; | |
692 | 2075 } |
2076 #endif | |
7 | 2077 |
2078 /* | |
2079 * If the cursor is only off by one line, put it at the same position as | |
2080 * before starting the change (for the "o" command). | |
2081 * Otherwise the cursor should go to the first undone line. | |
2082 */ | |
758 | 2083 if (curhead->uh_cursor.lnum + 1 == curwin->w_cursor.lnum |
7 | 2084 && curwin->w_cursor.lnum > 1) |
2085 --curwin->w_cursor.lnum; | |
758 | 2086 if (curhead->uh_cursor.lnum == curwin->w_cursor.lnum) |
7 | 2087 { |
758 | 2088 curwin->w_cursor.col = curhead->uh_cursor.col; |
7 | 2089 #ifdef FEAT_VIRTUALEDIT |
758 | 2090 if (virtual_active() && curhead->uh_cursor_vcol >= 0) |
2091 coladvance((colnr_T)curhead->uh_cursor_vcol); | |
7 | 2092 else |
2093 curwin->w_cursor.coladd = 0; | |
2094 #endif | |
2095 } | |
2096 else if (curwin->w_cursor.lnum <= curbuf->b_ml.ml_line_count) | |
2097 beginline(BL_SOL | BL_FIX); | |
2098 else | |
2099 { | |
2100 /* We get here with the current cursor line being past the end (eg | |
2101 * after adding lines at the end of the file, and then undoing it). | |
2102 * check_cursor() will move the cursor to the last line. Move it to | |
2103 * the first column here. */ | |
2104 curwin->w_cursor.col = 0; | |
2105 #ifdef FEAT_VIRTUALEDIT | |
2106 curwin->w_cursor.coladd = 0; | |
2107 #endif | |
2108 } | |
2109 | |
2110 /* Make sure the cursor is on an existing line and column. */ | |
2111 check_cursor(); | |
758 | 2112 |
2113 /* Remember where we are for "g-" and ":earlier 10s". */ | |
2114 curbuf->b_u_seq_cur = curhead->uh_seq; | |
777 | 2115 if (undo) |
2116 /* We are below the previous undo. However, to make ":earlier 1s" | |
2117 * work we compute this as being just above the just undone change. */ | |
2118 --curbuf->b_u_seq_cur; | |
2119 | |
2120 /* The timestamp can be the same for multiple changes, just use the one of | |
2121 * the undone/redone change. */ | |
758 | 2122 curbuf->b_u_seq_time = curhead->uh_time; |
1415 | 2123 #ifdef U_DEBUG |
2124 u_check(FALSE); | |
2125 #endif | |
7 | 2126 } |
2127 | |
2128 /* | |
2129 * If we deleted or added lines, report the number of less/more lines. | |
2130 * Otherwise, report the number of changes (this may be incorrect | |
2131 * in some cases, but it's better than nothing). | |
2132 */ | |
2133 static void | |
798 | 2134 u_undo_end(did_undo, absolute) |
794 | 2135 int did_undo; /* just did an undo */ |
798 | 2136 int absolute; /* used ":undo N" */ |
7 | 2137 { |
944 | 2138 char *msgstr; |
772 | 2139 u_header_T *uhp; |
2140 char_u msgbuf[80]; | |
753 | 2141 |
7 | 2142 #ifdef FEAT_FOLDING |
2143 if ((fdo_flags & FDO_UNDO) && KeyTyped) | |
2144 foldOpenCursor(); | |
2145 #endif | |
753 | 2146 |
2147 if (global_busy /* no messages now, wait until global is finished */ | |
2148 || !messaging()) /* 'lazyredraw' set, don't do messages now */ | |
2149 return; | |
2150 | |
2151 if (curbuf->b_ml.ml_flags & ML_EMPTY) | |
2152 --u_newcount; | |
2153 | |
2154 u_oldcount -= u_newcount; | |
2155 if (u_oldcount == -1) | |
944 | 2156 msgstr = N_("more line"); |
753 | 2157 else if (u_oldcount < 0) |
944 | 2158 msgstr = N_("more lines"); |
753 | 2159 else if (u_oldcount == 1) |
944 | 2160 msgstr = N_("line less"); |
753 | 2161 else if (u_oldcount > 1) |
944 | 2162 msgstr = N_("fewer lines"); |
753 | 2163 else |
2164 { | |
2165 u_oldcount = u_newcount; | |
2166 if (u_newcount == 1) | |
944 | 2167 msgstr = N_("change"); |
753 | 2168 else |
944 | 2169 msgstr = N_("changes"); |
753 | 2170 } |
2171 | |
772 | 2172 if (curbuf->b_u_curhead != NULL) |
794 | 2173 { |
798 | 2174 /* For ":undo N" we prefer a "after #N" message. */ |
2175 if (absolute && curbuf->b_u_curhead->uh_next != NULL) | |
2176 { | |
2177 uhp = curbuf->b_u_curhead->uh_next; | |
2178 did_undo = FALSE; | |
2179 } | |
2180 else if (did_undo) | |
794 | 2181 uhp = curbuf->b_u_curhead; |
2182 else | |
2183 uhp = curbuf->b_u_curhead->uh_next; | |
2184 } | |
753 | 2185 else |
772 | 2186 uhp = curbuf->b_u_newhead; |
753 | 2187 |
772 | 2188 if (uhp == NULL) |
2189 *msgbuf = NUL; | |
2190 else | |
2191 u_add_time(msgbuf, sizeof(msgbuf), uhp->uh_time); | |
2192 | |
794 | 2193 smsg((char_u *)_("%ld %s; %s #%ld %s"), |
777 | 2194 u_oldcount < 0 ? -u_oldcount : u_oldcount, |
944 | 2195 _(msgstr), |
794 | 2196 did_undo ? _("before") : _("after"), |
2197 uhp == NULL ? 0L : uhp->uh_seq, | |
2198 msgbuf); | |
7 | 2199 } |
2200 | |
2201 /* | |
2202 * u_sync: stop adding to the current entry list | |
2203 */ | |
2204 void | |
825 | 2205 u_sync(force) |
2206 int force; /* Also sync when no_u_sync is set. */ | |
7 | 2207 { |
825 | 2208 /* Skip it when already synced or syncing is disabled. */ |
2209 if (curbuf->b_u_synced || (!force && no_u_sync > 0)) | |
2210 return; | |
7 | 2211 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
2212 if (im_is_preediting()) | |
2213 return; /* XIM is busy, don't break an undo sequence */ | |
2214 #endif | |
2215 if (p_ul < 0) | |
2216 curbuf->b_u_synced = TRUE; /* no entries, nothing to do */ | |
2217 else | |
2218 { | |
2219 u_getbot(); /* compute ue_bot of previous u_save */ | |
2220 curbuf->b_u_curhead = NULL; | |
2221 } | |
2222 } | |
2223 | |
2224 /* | |
772 | 2225 * ":undolist": List the leafs of the undo tree |
2226 */ | |
2227 void | |
2228 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
|
2229 exarg_T *eap UNUSED; |
772 | 2230 { |
2231 garray_T ga; | |
2232 u_header_T *uhp; | |
2233 int mark; | |
2234 int nomark; | |
2235 int changes = 1; | |
2236 int i; | |
2237 | |
2238 /* | |
2239 * 1: walk the tree to find all leafs, put the info in "ga". | |
2240 * 2: sort the lines | |
2241 * 3: display the list | |
2242 */ | |
2243 mark = ++lastmark; | |
2244 nomark = ++lastmark; | |
2245 ga_init2(&ga, (int)sizeof(char *), 20); | |
2246 | |
2247 uhp = curbuf->b_u_oldhead; | |
2248 while (uhp != NULL) | |
2249 { | |
777 | 2250 if (uhp->uh_prev == NULL && uhp->uh_walk != nomark |
2251 && uhp->uh_walk != mark) | |
772 | 2252 { |
2253 if (ga_grow(&ga, 1) == FAIL) | |
2254 break; | |
2255 vim_snprintf((char *)IObuff, IOSIZE, "%6ld %7ld ", | |
2256 uhp->uh_seq, changes); | |
2257 u_add_time(IObuff + STRLEN(IObuff), IOSIZE - STRLEN(IObuff), | |
2258 uhp->uh_time); | |
2259 ((char_u **)(ga.ga_data))[ga.ga_len++] = vim_strsave(IObuff); | |
2260 } | |
2261 | |
2262 uhp->uh_walk = mark; | |
2263 | |
2264 /* go down in the tree if we haven't been there */ | |
2265 if (uhp->uh_prev != NULL && uhp->uh_prev->uh_walk != nomark | |
2266 && uhp->uh_prev->uh_walk != mark) | |
2267 { | |
2268 uhp = uhp->uh_prev; | |
2269 ++changes; | |
2270 } | |
2271 | |
2272 /* go to alternate branch if we haven't been there */ | |
2273 else if (uhp->uh_alt_next != NULL | |
2274 && uhp->uh_alt_next->uh_walk != nomark | |
2275 && uhp->uh_alt_next->uh_walk != mark) | |
2276 uhp = uhp->uh_alt_next; | |
2277 | |
2278 /* go up in the tree if we haven't been there and we are at the | |
2279 * start of alternate branches */ | |
2280 else if (uhp->uh_next != NULL && uhp->uh_alt_prev == NULL | |
2281 && uhp->uh_next->uh_walk != nomark | |
2282 && uhp->uh_next->uh_walk != mark) | |
2283 { | |
2284 uhp = uhp->uh_next; | |
2285 --changes; | |
2286 } | |
2287 | |
2288 else | |
2289 { | |
2290 /* need to backtrack; mark this node as done */ | |
2291 uhp->uh_walk = nomark; | |
2292 if (uhp->uh_alt_prev != NULL) | |
2293 uhp = uhp->uh_alt_prev; | |
2294 else | |
2295 { | |
2296 uhp = uhp->uh_next; | |
2297 --changes; | |
2298 } | |
2299 } | |
2300 } | |
2301 | |
2302 if (ga.ga_len == 0) | |
2303 MSG(_("Nothing to undo")); | |
2304 else | |
2305 { | |
2306 sort_strings((char_u **)ga.ga_data, ga.ga_len); | |
2307 | |
2308 msg_start(); | |
2309 msg_puts_attr((char_u *)_("number changes time"), hl_attr(HLF_T)); | |
2310 for (i = 0; i < ga.ga_len && !got_int; ++i) | |
2311 { | |
2312 msg_putchar('\n'); | |
2313 if (got_int) | |
2314 break; | |
2315 msg_puts(((char_u **)ga.ga_data)[i]); | |
2316 } | |
2317 msg_end(); | |
2318 | |
2319 ga_clear_strings(&ga); | |
2320 } | |
2321 } | |
2322 | |
2323 /* | |
2324 * Put the timestamp of an undo header in "buf[buflen]" in a nice format. | |
2325 */ | |
2326 static void | |
2327 u_add_time(buf, buflen, tt) | |
2328 char_u *buf; | |
2329 size_t buflen; | |
2330 time_t tt; | |
2331 { | |
2332 #ifdef HAVE_STRFTIME | |
2333 struct tm *curtime; | |
2334 | |
2335 if (time(NULL) - tt >= 100) | |
2336 { | |
2337 curtime = localtime(&tt); | |
810 | 2338 (void)strftime((char *)buf, buflen, "%H:%M:%S", curtime); |
772 | 2339 } |
2340 else | |
2341 #endif | |
835 | 2342 vim_snprintf((char *)buf, buflen, _("%ld seconds ago"), |
772 | 2343 (long)(time(NULL) - tt)); |
2344 } | |
2345 | |
2346 /* | |
697 | 2347 * ":undojoin": continue adding to the last entry list |
2348 */ | |
2349 void | |
2350 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
|
2351 exarg_T *eap UNUSED; |
697 | 2352 { |
839 | 2353 if (curbuf->b_u_newhead == NULL) |
2354 return; /* nothing changed before */ | |
2355 if (curbuf->b_u_curhead != NULL) | |
2356 { | |
2357 EMSG(_("E790: undojoin is not allowed after undo")); | |
2358 return; | |
2359 } | |
697 | 2360 if (!curbuf->b_u_synced) |
2361 return; /* already unsynced */ | |
2362 if (p_ul < 0) | |
2363 return; /* no entries, nothing to do */ | |
2364 else | |
2365 { | |
2366 /* Go back to the last entry */ | |
2367 curbuf->b_u_curhead = curbuf->b_u_newhead; | |
2368 curbuf->b_u_synced = FALSE; /* no entries, nothing to do */ | |
2369 } | |
2370 } | |
2371 | |
2372 /* | |
7 | 2373 * Called after writing the file and setting b_changed to FALSE. |
2374 * Now an undo means that the buffer is modified. | |
2375 */ | |
2376 void | |
2377 u_unchanged(buf) | |
2378 buf_T *buf; | |
2379 { | |
758 | 2380 u_unch_branch(buf->b_u_oldhead); |
2381 buf->b_did_warn = FALSE; | |
2382 } | |
2383 | |
2384 static void | |
2385 u_unch_branch(uhp) | |
2386 u_header_T *uhp; | |
2387 { | |
753 | 2388 u_header_T *uh; |
7 | 2389 |
758 | 2390 for (uh = uhp; uh != NULL; uh = uh->uh_prev) |
2391 { | |
7 | 2392 uh->uh_flags |= UH_CHANGED; |
758 | 2393 if (uh->uh_alt_next != NULL) |
2394 u_unch_branch(uh->uh_alt_next); /* recursive */ | |
2395 } | |
7 | 2396 } |
2397 | |
2398 /* | |
2399 * Get pointer to last added entry. | |
2400 * If it's not valid, give an error message and return NULL. | |
2401 */ | |
2402 static u_entry_T * | |
2403 u_get_headentry() | |
2404 { | |
2405 if (curbuf->b_u_newhead == NULL || curbuf->b_u_newhead->uh_entry == NULL) | |
2406 { | |
2407 EMSG(_("E439: undo list corrupt")); | |
2408 return NULL; | |
2409 } | |
2410 return curbuf->b_u_newhead->uh_entry; | |
2411 } | |
2412 | |
2413 /* | |
2414 * u_getbot(): compute the line number of the previous u_save | |
2415 * It is called only when b_u_synced is FALSE. | |
2416 */ | |
2417 static void | |
2418 u_getbot() | |
2419 { | |
2420 u_entry_T *uep; | |
2421 linenr_T extra; | |
2422 | |
2423 uep = u_get_headentry(); /* check for corrupt undo list */ | |
2424 if (uep == NULL) | |
2425 return; | |
2426 | |
2427 uep = curbuf->b_u_newhead->uh_getbot_entry; | |
2428 if (uep != NULL) | |
2429 { | |
2430 /* | |
2431 * the new ue_bot is computed from the number of lines that has been | |
2432 * inserted (0 - deleted) since calling u_save. This is equal to the | |
2433 * old line count subtracted from the current line count. | |
2434 */ | |
2435 extra = curbuf->b_ml.ml_line_count - uep->ue_lcount; | |
2436 uep->ue_bot = uep->ue_top + uep->ue_size + 1 + extra; | |
2437 if (uep->ue_bot < 1 || uep->ue_bot > curbuf->b_ml.ml_line_count) | |
2438 { | |
2439 EMSG(_("E440: undo line missing")); | |
2440 uep->ue_bot = uep->ue_top + 1; /* assume all lines deleted, will | |
2441 * get all the old lines back | |
2442 * without deleting the current | |
2443 * ones */ | |
2444 } | |
2445 | |
2446 curbuf->b_u_newhead->uh_getbot_entry = NULL; | |
2447 } | |
2448 | |
2449 curbuf->b_u_synced = TRUE; | |
2450 } | |
2451 | |
2452 /* | |
1415 | 2453 * Free one header "uhp" and its entry list and adjust the pointers. |
7 | 2454 */ |
2455 static void | |
758 | 2456 u_freeheader(buf, uhp, uhpp) |
168 | 2457 buf_T *buf; |
753 | 2458 u_header_T *uhp; |
2459 u_header_T **uhpp; /* if not NULL reset when freeing this header */ | |
7 | 2460 { |
1415 | 2461 u_header_T *uhap; |
2462 | |
753 | 2463 /* When there is an alternate redo list free that branch completely, |
2464 * because we can never go there. */ | |
2465 if (uhp->uh_alt_next != NULL) | |
2466 u_freebranch(buf, uhp->uh_alt_next, uhpp); | |
7 | 2467 |
753 | 2468 if (uhp->uh_alt_prev != NULL) |
2469 uhp->uh_alt_prev->uh_alt_next = NULL; | |
7 | 2470 |
753 | 2471 /* Update the links in the list to remove the header. */ |
7 | 2472 if (uhp->uh_next == NULL) |
168 | 2473 buf->b_u_oldhead = uhp->uh_prev; |
7 | 2474 else |
2475 uhp->uh_next->uh_prev = uhp->uh_prev; | |
2476 | |
2477 if (uhp->uh_prev == NULL) | |
168 | 2478 buf->b_u_newhead = uhp->uh_next; |
7 | 2479 else |
1415 | 2480 for (uhap = uhp->uh_prev; uhap != NULL; uhap = uhap->uh_alt_next) |
2481 uhap->uh_next = uhp->uh_next; | |
7 | 2482 |
753 | 2483 u_freeentries(buf, uhp, uhpp); |
2484 } | |
2485 | |
2486 /* | |
758 | 2487 * Free an alternate branch and any following alternate branches. |
753 | 2488 */ |
2489 static void | |
2490 u_freebranch(buf, uhp, uhpp) | |
2491 buf_T *buf; | |
2492 u_header_T *uhp; | |
2493 u_header_T **uhpp; /* if not NULL reset when freeing this header */ | |
2494 { | |
2495 u_header_T *tofree, *next; | |
2496 | |
1440 | 2497 /* If this is the top branch we may need to use u_freeheader() to update |
2498 * all the pointers. */ | |
2499 if (uhp == buf->b_u_oldhead) | |
2500 { | |
2501 u_freeheader(buf, uhp, uhpp); | |
2502 return; | |
2503 } | |
2504 | |
753 | 2505 if (uhp->uh_alt_prev != NULL) |
2506 uhp->uh_alt_prev->uh_alt_next = NULL; | |
2507 | |
2508 next = uhp; | |
2509 while (next != NULL) | |
2510 { | |
2511 tofree = next; | |
2512 if (tofree->uh_alt_next != NULL) | |
2513 u_freebranch(buf, tofree->uh_alt_next, uhpp); /* recursive */ | |
2514 next = tofree->uh_prev; | |
2515 u_freeentries(buf, tofree, uhpp); | |
2516 } | |
2517 } | |
2518 | |
2519 /* | |
2520 * Free all the undo entries for one header and the header itself. | |
2521 * This means that "uhp" is invalid when returning. | |
2522 */ | |
2523 static void | |
2524 u_freeentries(buf, uhp, uhpp) | |
2525 buf_T *buf; | |
2526 u_header_T *uhp; | |
2527 u_header_T **uhpp; /* if not NULL reset when freeing this header */ | |
2528 { | |
2529 u_entry_T *uep, *nuep; | |
2530 | |
2531 /* Check for pointers to the header that become invalid now. */ | |
2532 if (buf->b_u_curhead == uhp) | |
2533 buf->b_u_curhead = NULL; | |
1415 | 2534 if (buf->b_u_newhead == uhp) |
2535 buf->b_u_newhead = NULL; /* freeing the newest entry */ | |
753 | 2536 if (uhpp != NULL && uhp == *uhpp) |
2537 *uhpp = NULL; | |
2538 | |
2539 for (uep = uhp->uh_entry; uep != NULL; uep = nuep) | |
2540 { | |
2541 nuep = uep->ue_next; | |
2542 u_freeentry(uep, uep->ue_size); | |
2543 } | |
2544 | |
1415 | 2545 #ifdef U_DEBUG |
2546 uhp->uh_magic = 0; | |
2547 #endif | |
168 | 2548 U_FREE_LINE((char_u *)uhp); |
2549 --buf->b_u_numhead; | |
7 | 2550 } |
2551 | |
2552 /* | |
2553 * free entry 'uep' and 'n' lines in uep->ue_array[] | |
2554 */ | |
2555 static void | |
2556 u_freeentry(uep, n) | |
2557 u_entry_T *uep; | |
2558 long n; | |
2559 { | |
414 | 2560 while (n > 0) |
168 | 2561 U_FREE_LINE(uep->ue_array[--n]); |
359 | 2562 U_FREE_LINE((char_u *)uep->ue_array); |
1415 | 2563 #ifdef U_DEBUG |
2564 uep->ue_magic = 0; | |
2565 #endif | |
168 | 2566 U_FREE_LINE((char_u *)uep); |
7 | 2567 } |
2568 | |
2569 /* | |
2570 * invalidate the undo buffer; called when storage has already been released | |
2571 */ | |
2572 void | |
2573 u_clearall(buf) | |
2574 buf_T *buf; | |
2575 { | |
2576 buf->b_u_newhead = buf->b_u_oldhead = buf->b_u_curhead = NULL; | |
2577 buf->b_u_synced = TRUE; | |
2578 buf->b_u_numhead = 0; | |
2579 buf->b_u_line_ptr = NULL; | |
2580 buf->b_u_line_lnum = 0; | |
2581 } | |
2582 | |
2583 /* | |
2584 * save the line "lnum" for the "U" command | |
2585 */ | |
2586 void | |
2587 u_saveline(lnum) | |
2588 linenr_T lnum; | |
2589 { | |
2590 if (lnum == curbuf->b_u_line_lnum) /* line is already saved */ | |
2591 return; | |
356 | 2592 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) /* should never happen */ |
7 | 2593 return; |
2594 u_clearline(); | |
2595 curbuf->b_u_line_lnum = lnum; | |
2596 if (curwin->w_cursor.lnum == lnum) | |
2597 curbuf->b_u_line_colnr = curwin->w_cursor.col; | |
2598 else | |
2599 curbuf->b_u_line_colnr = 0; | |
2600 if ((curbuf->b_u_line_ptr = u_save_line(lnum)) == NULL) | |
2601 do_outofmem_msg((long_u)0); | |
2602 } | |
2603 | |
2604 /* | |
2605 * clear the line saved for the "U" command | |
2606 * (this is used externally for crossing a line while in insert mode) | |
2607 */ | |
2608 void | |
2609 u_clearline() | |
2610 { | |
2611 if (curbuf->b_u_line_ptr != NULL) | |
2612 { | |
168 | 2613 U_FREE_LINE(curbuf->b_u_line_ptr); |
7 | 2614 curbuf->b_u_line_ptr = NULL; |
2615 curbuf->b_u_line_lnum = 0; | |
2616 } | |
2617 } | |
2618 | |
2619 /* | |
2620 * Implementation of the "U" command. | |
2621 * Differentiation from vi: "U" can be undone with the next "U". | |
2622 * We also allow the cursor to be in another line. | |
2623 */ | |
2624 void | |
2625 u_undoline() | |
2626 { | |
2627 colnr_T t; | |
2628 char_u *oldp; | |
2629 | |
2630 if (undo_off) | |
2631 return; | |
2632 | |
1534 | 2633 if (curbuf->b_u_line_ptr == NULL |
2634 || curbuf->b_u_line_lnum > curbuf->b_ml.ml_line_count) | |
7 | 2635 { |
2636 beep_flush(); | |
2637 return; | |
2638 } | |
1534 | 2639 |
2640 /* first save the line for the 'u' command */ | |
7 | 2641 if (u_savecommon(curbuf->b_u_line_lnum - 1, |
2642 curbuf->b_u_line_lnum + 1, (linenr_T)0) == FAIL) | |
2643 return; | |
2644 oldp = u_save_line(curbuf->b_u_line_lnum); | |
2645 if (oldp == NULL) | |
2646 { | |
2647 do_outofmem_msg((long_u)0); | |
2648 return; | |
2649 } | |
2650 ml_replace(curbuf->b_u_line_lnum, curbuf->b_u_line_ptr, TRUE); | |
2651 changed_bytes(curbuf->b_u_line_lnum, 0); | |
168 | 2652 U_FREE_LINE(curbuf->b_u_line_ptr); |
7 | 2653 curbuf->b_u_line_ptr = oldp; |
2654 | |
2655 t = curbuf->b_u_line_colnr; | |
2656 if (curwin->w_cursor.lnum == curbuf->b_u_line_lnum) | |
2657 curbuf->b_u_line_colnr = curwin->w_cursor.col; | |
2658 curwin->w_cursor.col = t; | |
2659 curwin->w_cursor.lnum = curbuf->b_u_line_lnum; | |
1534 | 2660 check_cursor_col(); |
7 | 2661 } |
2662 | |
2663 /* | |
168 | 2664 * There are two implementations of the memory management for undo: |
2665 * 1. Use the standard malloc()/free() functions. | |
2666 * This should be fast for allocating memory, but when a buffer is | |
2667 * abandoned every single allocated chunk must be freed, which may be slow. | |
2668 * 2. Allocate larger blocks of memory and keep track of chunks ourselves. | |
2669 * This is fast for abandoning, but the use of linked lists is slow for | |
2670 * finding a free chunk. Esp. when a lot of lines are changed or deleted. | |
2671 * A bit of profiling showed that the first method is faster, especially when | |
2672 * making a large number of changes, under the condition that malloc()/free() | |
2673 * is implemented efficiently. | |
2674 */ | |
2675 #ifdef U_USE_MALLOC | |
2676 /* | |
2677 * Version of undo memory allocation using malloc()/free() | |
2678 * | |
2679 * U_FREE_LINE() and U_ALLOC_LINE() are macros that invoke vim_free() and | |
2680 * lalloc() directly. | |
2681 */ | |
2682 | |
2683 /* | |
2684 * Free all allocated memory blocks for the buffer 'buf'. | |
2685 */ | |
2686 void | |
2687 u_blockfree(buf) | |
2688 buf_T *buf; | |
2689 { | |
753 | 2690 while (buf->b_u_oldhead != NULL) |
758 | 2691 u_freeheader(buf, buf->b_u_oldhead, NULL); |
359 | 2692 U_FREE_LINE(buf->b_u_line_ptr); |
168 | 2693 } |
2694 | |
2695 #else | |
2696 /* | |
2697 * Storage allocation for the undo lines and blocks of the current file. | |
2698 * Version where Vim keeps track of the available memory. | |
7 | 2699 */ |
2700 | |
2701 /* | |
2702 * Memory is allocated in relatively large blocks. These blocks are linked | |
2703 * in the allocated block list, headed by curbuf->b_block_head. They are all | |
2704 * freed when abandoning a file, so we don't have to free every single line. | |
2705 * The list is kept sorted on memory address. | |
2706 * block_alloc() allocates a block. | |
2707 * m_blockfree() frees all blocks. | |
2708 * | |
2709 * The available chunks of memory are kept in free chunk lists. There is | |
2710 * one free list for each block of allocated memory. The list is kept sorted | |
2711 * on memory address. | |
2712 * u_alloc_line() gets a chunk from the free lists. | |
2713 * u_free_line() returns a chunk to the free lists. | |
2714 * curbuf->b_m_search points to the chunk before the chunk that was | |
2715 * freed/allocated the last time. | |
2716 * curbuf->b_mb_current points to the b_head where curbuf->b_m_search | |
2717 * points into the free list. | |
2718 * | |
2719 * | |
2720 * b_block_head /---> block #1 /---> block #2 | |
2721 * mb_next ---/ mb_next ---/ mb_next ---> NULL | |
2722 * mb_info mb_info mb_info | |
2723 * | | | | |
2724 * V V V | |
2725 * NULL free chunk #1.1 free chunk #2.1 | |
2726 * | | | |
2727 * V V | |
2728 * free chunk #1.2 NULL | |
2729 * | | |
2730 * V | |
2731 * NULL | |
2732 * | |
2733 * When a single free chunk list would have been used, it could take a lot | |
2734 * of time in u_free_line() to find the correct place to insert a chunk in the | |
2735 * free list. The single free list would become very long when many lines are | |
2736 * changed (e.g. with :%s/^M$//). | |
2737 */ | |
2738 | |
2739 /* | |
2740 * this blocksize is used when allocating new lines | |
2741 */ | |
2742 #define MEMBLOCKSIZE 2044 | |
2743 | |
2744 /* | |
2745 * The size field contains the size of the chunk, including the size field | |
2746 * itself. | |
2747 * | |
2748 * When the chunk is not in-use it is preceded with the m_info structure. | |
2749 * The m_next field links it in one of the free chunk lists. | |
2750 * | |
2751 * On most unix systems structures have to be longword (32 or 64 bit) aligned. | |
2752 * On most other systems they are short (16 bit) aligned. | |
2753 */ | |
2754 | |
2755 /* the structure definitions are now in structs.h */ | |
2756 | |
2757 #ifdef ALIGN_LONG | |
2758 /* size of m_size */ | |
2759 # define M_OFFSET (sizeof(long_u)) | |
2760 #else | |
2761 /* size of m_size */ | |
2762 # define M_OFFSET (sizeof(short_u)) | |
2763 #endif | |
2764 | |
168 | 2765 static char_u *u_blockalloc __ARGS((long_u)); |
2766 | |
7 | 2767 /* |
2768 * Allocate a block of memory and link it in the allocated block list. | |
2769 */ | |
2770 static char_u * | |
2771 u_blockalloc(size) | |
2772 long_u size; | |
2773 { | |
2774 mblock_T *p; | |
2775 mblock_T *mp, *next; | |
2776 | |
2777 p = (mblock_T *)lalloc(size + sizeof(mblock_T), FALSE); | |
2778 if (p != NULL) | |
2779 { | |
2780 /* Insert the block into the allocated block list, keeping it | |
2781 sorted on address. */ | |
2782 for (mp = &curbuf->b_block_head; | |
2783 (next = mp->mb_next) != NULL && next < p; | |
2784 mp = next) | |
2785 ; | |
2786 p->mb_next = next; /* link in block list */ | |
2787 p->mb_size = size; | |
168 | 2788 p->mb_maxsize = 0; /* nothing free yet */ |
7 | 2789 mp->mb_next = p; |
2790 p->mb_info.m_next = NULL; /* clear free list */ | |
2791 p->mb_info.m_size = 0; | |
2792 curbuf->b_mb_current = p; /* remember current block */ | |
2793 curbuf->b_m_search = NULL; | |
2794 p++; /* return usable memory */ | |
2795 } | |
2796 return (char_u *)p; | |
2797 } | |
2798 | |
2799 /* | |
2800 * free all allocated memory blocks for the buffer 'buf' | |
2801 */ | |
2802 void | |
2803 u_blockfree(buf) | |
2804 buf_T *buf; | |
2805 { | |
2806 mblock_T *p, *np; | |
2807 | |
2808 for (p = buf->b_block_head.mb_next; p != NULL; p = np) | |
2809 { | |
2810 np = p->mb_next; | |
2811 vim_free(p); | |
2812 } | |
2813 buf->b_block_head.mb_next = NULL; | |
2814 buf->b_m_search = NULL; | |
2815 buf->b_mb_current = NULL; | |
2816 } | |
2817 | |
2818 /* | |
2819 * Free a chunk of memory for the current buffer. | |
2820 * Insert the chunk into the correct free list, keeping it sorted on address. | |
2821 */ | |
2822 static void | |
2823 u_free_line(ptr, keep) | |
2824 char_u *ptr; | |
2825 int keep; /* don't free the block when it's empty */ | |
2826 { | |
2827 minfo_T *next; | |
2828 minfo_T *prev, *curr; | |
2829 minfo_T *mp; | |
2830 mblock_T *nextb; | |
2831 mblock_T *prevb; | |
168 | 2832 long_u maxsize; |
7 | 2833 |
2834 if (ptr == NULL || ptr == IObuff) | |
2835 return; /* illegal address can happen in out-of-memory situations */ | |
2836 | |
2837 mp = (minfo_T *)(ptr - M_OFFSET); | |
2838 | |
2839 /* find block where chunk could be a part off */ | |
2840 /* if we change curbuf->b_mb_current, curbuf->b_m_search is set to NULL */ | |
2841 if (curbuf->b_mb_current == NULL || mp < (minfo_T *)curbuf->b_mb_current) | |
2842 { | |
2843 curbuf->b_mb_current = curbuf->b_block_head.mb_next; | |
2844 curbuf->b_m_search = NULL; | |
2845 } | |
2846 if ((nextb = curbuf->b_mb_current->mb_next) != NULL | |
2847 && (minfo_T *)nextb < mp) | |
2848 { | |
2849 curbuf->b_mb_current = nextb; | |
2850 curbuf->b_m_search = NULL; | |
2851 } | |
2852 while ((nextb = curbuf->b_mb_current->mb_next) != NULL | |
2853 && (minfo_T *)nextb < mp) | |
2854 curbuf->b_mb_current = nextb; | |
2855 | |
2856 curr = NULL; | |
2857 /* | |
2858 * If mp is smaller than curbuf->b_m_search->m_next go to the start of | |
2859 * the free list | |
2860 */ | |
2861 if (curbuf->b_m_search == NULL || mp < (curbuf->b_m_search->m_next)) | |
2862 next = &(curbuf->b_mb_current->mb_info); | |
2863 else | |
2864 next = curbuf->b_m_search; | |
2865 /* | |
2866 * The following loop is executed very often. | |
2867 * Therefore it has been optimized at the cost of readability. | |
2868 * Keep it fast! | |
2869 */ | |
2870 #ifdef SLOW_BUT_EASY_TO_READ | |
2871 do | |
2872 { | |
2873 prev = curr; | |
2874 curr = next; | |
2875 next = next->m_next; | |
2876 } | |
2877 while (mp > next && next != NULL); | |
2878 #else | |
2879 do /* first, middle, last */ | |
2880 { | |
2881 prev = next->m_next; /* curr, next, prev */ | |
2882 if (prev == NULL || mp <= prev) | |
2883 { | |
2884 prev = curr; | |
2885 curr = next; | |
2886 next = next->m_next; | |
2887 break; | |
2888 } | |
2889 curr = prev->m_next; /* next, prev, curr */ | |
2890 if (curr == NULL || mp <= curr) | |
2891 { | |
2892 prev = next; | |
2893 curr = prev->m_next; | |
2894 next = curr->m_next; | |
2895 break; | |
2896 } | |
2897 next = curr->m_next; /* prev, curr, next */ | |
2898 } | |
2899 while (mp > next && next != NULL); | |
2900 #endif | |
2901 | |
2902 /* if *mp and *next are concatenated, join them into one chunk */ | |
2903 if ((char_u *)mp + mp->m_size == (char_u *)next) | |
2904 { | |
2905 mp->m_size += next->m_size; | |
2906 mp->m_next = next->m_next; | |
2907 } | |
2908 else | |
2909 mp->m_next = next; | |
168 | 2910 maxsize = mp->m_size; |
7 | 2911 |
2912 /* if *curr and *mp are concatenated, join them */ | |
2913 if (prev != NULL && (char_u *)curr + curr->m_size == (char_u *)mp) | |
2914 { | |
2915 curr->m_size += mp->m_size; | |
168 | 2916 maxsize = curr->m_size; |
7 | 2917 curr->m_next = mp->m_next; |
2918 curbuf->b_m_search = prev; | |
2919 } | |
2920 else | |
2921 { | |
2922 curr->m_next = mp; | |
2923 curbuf->b_m_search = curr; /* put curbuf->b_m_search before freed | |
2924 chunk */ | |
2925 } | |
2926 | |
2927 /* | |
1210 | 2928 * If the block only contains free memory now, release it. |
7 | 2929 */ |
2930 if (!keep && curbuf->b_mb_current->mb_size | |
2931 == curbuf->b_mb_current->mb_info.m_next->m_size) | |
2932 { | |
2933 /* Find the block before the current one to be able to unlink it from | |
2934 * the list of blocks. */ | |
2935 prevb = &curbuf->b_block_head; | |
2936 for (nextb = prevb->mb_next; nextb != curbuf->b_mb_current; | |
2937 nextb = nextb->mb_next) | |
2938 prevb = nextb; | |
2939 prevb->mb_next = nextb->mb_next; | |
2940 vim_free(nextb); | |
2941 curbuf->b_mb_current = NULL; | |
2942 curbuf->b_m_search = NULL; | |
2943 } | |
168 | 2944 else if (curbuf->b_mb_current->mb_maxsize < maxsize) |
2945 curbuf->b_mb_current->mb_maxsize = maxsize; | |
7 | 2946 } |
2947 | |
2948 /* | |
2949 * Allocate and initialize a new line structure with room for at least | |
2950 * 'size' characters plus a terminating NUL. | |
2951 */ | |
2952 static char_u * | |
2953 u_alloc_line(size) | |
2954 unsigned size; | |
2955 { | |
2956 minfo_T *mp, *mprev, *mp2; | |
2957 mblock_T *mbp; | |
2958 int size_align; | |
2959 | |
2960 /* | |
2961 * Add room for size field and trailing NUL byte. | |
2962 * Adjust for minimal size (must be able to store minfo_T | |
2963 * plus a trailing NUL, so the chunk can be released again) | |
2964 */ | |
2965 size += M_OFFSET + 1; | |
2966 if (size < sizeof(minfo_T) + 1) | |
2967 size = sizeof(minfo_T) + 1; | |
2968 | |
2969 /* | |
2970 * round size up for alignment | |
2971 */ | |
2972 size_align = (size + ALIGN_MASK) & ~ALIGN_MASK; | |
2973 | |
2974 /* | |
2975 * If curbuf->b_m_search is NULL (uninitialized free list) start at | |
2976 * curbuf->b_block_head | |
2977 */ | |
2978 if (curbuf->b_mb_current == NULL || curbuf->b_m_search == NULL) | |
2979 { | |
2980 curbuf->b_mb_current = &curbuf->b_block_head; | |
2981 curbuf->b_m_search = &(curbuf->b_block_head.mb_info); | |
2982 } | |
2983 | |
168 | 2984 /* Search for a block with enough space. */ |
7 | 2985 mbp = curbuf->b_mb_current; |
168 | 2986 while (mbp->mb_maxsize < size_align) |
7 | 2987 { |
168 | 2988 if (mbp->mb_next != NULL) |
7 | 2989 mbp = mbp->mb_next; |
2990 else | |
2991 mbp = &curbuf->b_block_head; | |
168 | 2992 if (mbp == curbuf->b_mb_current) |
7 | 2993 { |
168 | 2994 int n = (size_align > (MEMBLOCKSIZE / 4) |
2995 ? size_align : MEMBLOCKSIZE); | |
7 | 2996 |
168 | 2997 /* Back where we started in block list: need to add a new block |
2998 * with enough space. */ | |
2999 mp = (minfo_T *)u_blockalloc((long_u)n); | |
3000 if (mp == NULL) | |
3001 return (NULL); | |
3002 mp->m_size = n; | |
3003 u_free_line((char_u *)mp + M_OFFSET, TRUE); | |
3004 mbp = curbuf->b_mb_current; | |
3005 break; | |
3006 } | |
3007 } | |
3008 if (mbp != curbuf->b_mb_current) | |
3009 curbuf->b_m_search = &(mbp->mb_info); | |
3010 | |
3011 /* In this block find a chunk with enough space. */ | |
3012 mprev = curbuf->b_m_search; | |
3013 mp = curbuf->b_m_search->m_next; | |
407 | 3014 for (;;) |
168 | 3015 { |
3016 if (mp == NULL) /* at end of the list */ | |
3017 mp = &(mbp->mb_info); /* wrap around to begin */ | |
3018 if (mp->m_size >= size) | |
3019 break; | |
3020 if (mp == curbuf->b_m_search) | |
3021 { | |
3022 /* back where we started in free chunk list: "cannot happen" */ | |
3023 EMSG2(_(e_intern2), "u_alloc_line()"); | |
3024 return NULL; | |
7 | 3025 } |
3026 mprev = mp; | |
168 | 3027 mp = mp->m_next; |
7 | 3028 } |
3029 | |
168 | 3030 /* when using the largest chunk adjust mb_maxsize */ |
3031 if (mp->m_size >= mbp->mb_maxsize) | |
3032 mbp->mb_maxsize = 0; | |
3033 | |
7 | 3034 /* if the chunk we found is large enough, split it up in two */ |
3035 if ((long)mp->m_size - size_align >= (long)(sizeof(minfo_T) + 1)) | |
3036 { | |
3037 mp2 = (minfo_T *)((char_u *)mp + size_align); | |
3038 mp2->m_size = mp->m_size - size_align; | |
3039 mp2->m_next = mp->m_next; | |
3040 mprev->m_next = mp2; | |
3041 mp->m_size = size_align; | |
3042 } | |
3043 else /* remove *mp from the free list */ | |
3044 { | |
3045 mprev->m_next = mp->m_next; | |
3046 } | |
3047 curbuf->b_m_search = mprev; | |
3048 curbuf->b_mb_current = mbp; | |
3049 | |
168 | 3050 /* If using the largest chunk need to find the new largest chunk */ |
3051 if (mbp->mb_maxsize == 0) | |
3052 for (mp2 = &(mbp->mb_info); mp2 != NULL; mp2 = mp2->m_next) | |
3053 if (mbp->mb_maxsize < mp2->m_size) | |
3054 mbp->mb_maxsize = mp2->m_size; | |
3055 | |
7 | 3056 mp = (minfo_T *)((char_u *)mp + M_OFFSET); |
3057 *(char_u *)mp = NUL; /* set the first byte to NUL */ | |
3058 | |
3059 return ((char_u *)mp); | |
3060 } | |
168 | 3061 #endif |
7 | 3062 |
3063 /* | |
3064 * u_save_line(): allocate memory with u_alloc_line() and copy line 'lnum' | |
3065 * into it. | |
3066 */ | |
3067 static char_u * | |
3068 u_save_line(lnum) | |
3069 linenr_T lnum; | |
3070 { | |
3071 char_u *src; | |
3072 char_u *dst; | |
3073 unsigned len; | |
3074 | |
3075 src = ml_get(lnum); | |
3076 len = (unsigned)STRLEN(src); | |
168 | 3077 if ((dst = U_ALLOC_LINE(len)) != NULL) |
7 | 3078 mch_memmove(dst, src, (size_t)(len + 1)); |
3079 return (dst); | |
3080 } | |
3081 | |
3082 /* | |
3083 * Check if the 'modified' flag is set, or 'ff' has changed (only need to | |
3084 * check the first character, because it can only be "dos", "unix" or "mac"). | |
3085 * "nofile" and "scratch" type buffers are considered to always be unchanged. | |
3086 */ | |
3087 int | |
3088 bufIsChanged(buf) | |
3089 buf_T *buf; | |
3090 { | |
3091 return | |
3092 #ifdef FEAT_QUICKFIX | |
3093 !bt_dontwrite(buf) && | |
3094 #endif | |
3095 (buf->b_changed || file_ff_differs(buf)); | |
3096 } | |
3097 | |
3098 int | |
3099 curbufIsChanged() | |
3100 { | |
3101 return | |
3102 #ifdef FEAT_QUICKFIX | |
3103 !bt_dontwrite(curbuf) && | |
3104 #endif | |
3105 (curbuf->b_changed || file_ff_differs(curbuf)); | |
3106 } |