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