annotate src/undo.c @ 2348:8878a9f8db87 vim73

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