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