annotate src/undo.c @ 2378:85b7dc8da5eb vim73

Add the 'concealcursor' option to decide when the cursor line is to be concealed or not. Rename 'conc' to 'cole' as the short name for 'conceallevel'.
author Bram Moolenaar <bram@vim.org>
date Fri, 23 Jul 2010 22:10:27 +0200
parents 76b7ba68a098
children a3aca345aafa
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 */
2367
76b7ba68a098 Fix build problem when fchown() not available. (Gary Johnson)
Bram Moolenaar <bram@vim.org>
parents: 2348
diff changeset
1440 if (st_old_valid
76b7ba68a098 Fix build problem when fchown() not available. (Gary Johnson)
Bram Moolenaar <bram@vim.org>
parents: 2348
diff changeset
1441 && mch_stat((char *)file_name, &st_new) >= 0
76b7ba68a098 Fix build problem when fchown() not available. (Gary Johnson)
Bram Moolenaar <bram@vim.org>
parents: 2348
diff changeset
1442 && st_new.st_gid != st_old.st_gid
2231
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1443 # ifdef HAVE_FCHOWN /* sequent-ptx lacks fchown() */
2367
76b7ba68a098 Fix build problem when fchown() not available. (Gary Johnson)
Bram Moolenaar <bram@vim.org>
parents: 2348
diff changeset
1444 && fchown(fd, (uid_t)-1, st_old.st_gid) != 0
2231
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1445 # endif
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1446 )
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1447 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
1448 # ifdef HAVE_SELINUX
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1449 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
1450 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
1451 # endif
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1452 #endif
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1453
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1454 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
1455 if (fp == NULL)
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1456 {
2311
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1457 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
1458 close(fd);
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1459 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
1460 goto theend;
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1461 }
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1462
2233
43cad213cb7f A bit of cleanup and simplification for undofile.
Bram Moolenaar <bram@vim.org>
parents: 2232
diff changeset
1463 /* Undo must be synced. */
43cad213cb7f A bit of cleanup and simplification for undofile.
Bram Moolenaar <bram@vim.org>
parents: 2232
diff changeset
1464 u_sync(TRUE);
43cad213cb7f A bit of cleanup and simplification for undofile.
Bram Moolenaar <bram@vim.org>
parents: 2232
diff changeset
1465
2238
3d0a7beb0d75 Made reading/writing undo info a bit more robust.
Bram Moolenaar <bram@vim.org>
parents: 2236
diff changeset
1466 /*
3d0a7beb0d75 Made reading/writing undo info a bit more robust.
Bram Moolenaar <bram@vim.org>
parents: 2236
diff changeset
1467 * Write the header.
3d0a7beb0d75 Made reading/writing undo info a bit more robust.
Bram Moolenaar <bram@vim.org>
parents: 2236
diff changeset
1468 */
3d0a7beb0d75 Made reading/writing undo info a bit more robust.
Bram Moolenaar <bram@vim.org>
parents: 2236
diff changeset
1469 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
1470 goto write_error;
2267
c08f91142c41 Crypt the swapfile.
Bram Moolenaar <bram@vim.org>
parents: 2266
diff changeset
1471 #ifdef FEAT_CRYPT
c08f91142c41 Crypt the swapfile.
Bram Moolenaar <bram@vim.org>
parents: 2266
diff changeset
1472 if (*buf->b_p_key)
c08f91142c41 Crypt the swapfile.
Bram Moolenaar <bram@vim.org>
parents: 2266
diff changeset
1473 do_crypt = TRUE;
c08f91142c41 Crypt the swapfile.
Bram Moolenaar <bram@vim.org>
parents: 2266
diff changeset
1474 #endif
2231
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 /*
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1477 * 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
1478 */
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1479 mark = ++lastmark;
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1480 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
1481 while (uhp != NULL)
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1482 {
2311
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1483 /* 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
1484 if (uhp->uh_walk != mark)
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1485 {
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1486 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
1487 #ifdef U_DEBUG
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1488 ++headers_written;
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1489 #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
1490 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
1491 goto write_error;
2311
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1492 }
2231
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1493
2311
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1494 /* 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
1495 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
1496 uhp = uhp->uh_prev.ptr;
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1497 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
1498 && 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
1499 uhp = uhp->uh_alt_next.ptr;
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1500 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
1501 && uhp->uh_next.ptr->uh_walk != mark)
2311
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1502 uhp = uhp->uh_next.ptr;
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1503 else if (uhp->uh_alt_prev.ptr != NULL)
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1504 uhp = uhp->uh_alt_prev.ptr;
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1505 else
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1506 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
1507 }
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1508
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1509 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
1510 write_ok = TRUE;
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1511 #ifdef U_DEBUG
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1512 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
1513 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
1514 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
1515 #endif
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1516
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1517 write_error:
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1518 fclose(fp);
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1519 if (!write_ok)
2311
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1520 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
1521
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1522 #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
1523 /* 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
1524 * 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
1525 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
1526 (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
1527 #endif
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1528 #ifdef HAVE_ACL
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1529 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
1530 {
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1531 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
1532
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1533 /* 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
1534 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
1535 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
1536 }
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1537 #endif
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1538
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1539 theend:
2267
c08f91142c41 Crypt the swapfile.
Bram Moolenaar <bram@vim.org>
parents: 2266
diff changeset
1540 #ifdef FEAT_CRYPT
c08f91142c41 Crypt the swapfile.
Bram Moolenaar <bram@vim.org>
parents: 2266
diff changeset
1541 if (do_crypt)
c08f91142c41 Crypt the swapfile.
Bram Moolenaar <bram@vim.org>
parents: 2266
diff changeset
1542 crypt_pop_state();
c08f91142c41 Crypt the swapfile.
Bram Moolenaar <bram@vim.org>
parents: 2266
diff changeset
1543 #endif
2231
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1544 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
1545 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
1546 }
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1547
2214
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1548 /*
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1549 * 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
1550 * 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
1551 * 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
1552 * 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
1553 * "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
1554 */
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1555 void
2238
3d0a7beb0d75 Made reading/writing undo info a bit more robust.
Bram Moolenaar <bram@vim.org>
parents: 2236
diff changeset
1556 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
1557 char_u *name;
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1558 char_u *hash;
2238
3d0a7beb0d75 Made reading/writing undo info a bit more robust.
Bram Moolenaar <bram@vim.org>
parents: 2236
diff changeset
1559 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
1560 {
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1561 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
1562 FILE *fp;
2231
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1563 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
1564 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
1565 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
1566 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
1567 linenr_T line_count;
2217
120502692d82 Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents: 2215
diff changeset
1568 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
1569 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
1570 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
1571 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
1572 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
1573 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
1574 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
1575 int i, j;
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1576 int c;
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;
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1578 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
1579 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
1580 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
1581 #ifdef U_DEBUG
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1582 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
1583 #endif
2238
3d0a7beb0d75 Made reading/writing undo info a bit more robust.
Bram Moolenaar <bram@vim.org>
parents: 2236
diff changeset
1584 #ifdef UNIX
3d0a7beb0d75 Made reading/writing undo info a bit more robust.
Bram Moolenaar <bram@vim.org>
parents: 2236
diff changeset
1585 struct stat st_orig;
3d0a7beb0d75 Made reading/writing undo info a bit more robust.
Bram Moolenaar <bram@vim.org>
parents: 2236
diff changeset
1586 struct stat st_undo;
3d0a7beb0d75 Made reading/writing undo info a bit more robust.
Bram Moolenaar <bram@vim.org>
parents: 2236
diff changeset
1587 #endif
2267
c08f91142c41 Crypt the swapfile.
Bram Moolenaar <bram@vim.org>
parents: 2266
diff changeset
1588 #ifdef FEAT_CRYPT
c08f91142c41 Crypt the swapfile.
Bram Moolenaar <bram@vim.org>
parents: 2266
diff changeset
1589 int do_decrypt = FALSE;
c08f91142c41 Crypt the swapfile.
Bram Moolenaar <bram@vim.org>
parents: 2266
diff changeset
1590 #endif
2214
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1591
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1592 if (name == NULL)
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1593 {
2311
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1594 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
1595 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
1596 return;
2238
3d0a7beb0d75 Made reading/writing undo info a bit more robust.
Bram Moolenaar <bram@vim.org>
parents: 2236
diff changeset
1597
3d0a7beb0d75 Made reading/writing undo info a bit more robust.
Bram Moolenaar <bram@vim.org>
parents: 2236
diff changeset
1598 #ifdef UNIX
3d0a7beb0d75 Made reading/writing undo info a bit more robust.
Bram Moolenaar <bram@vim.org>
parents: 2236
diff changeset
1599 /* 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
1600 * owner of the text file. */
3d0a7beb0d75 Made reading/writing undo info a bit more robust.
Bram Moolenaar <bram@vim.org>
parents: 2236
diff changeset
1601 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
1602 && 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
1603 && 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
1604 {
3d0a7beb0d75 Made reading/writing undo info a bit more robust.
Bram Moolenaar <bram@vim.org>
parents: 2236
diff changeset
1605 if (p_verbose > 0)
3d0a7beb0d75 Made reading/writing undo info a bit more robust.
Bram Moolenaar <bram@vim.org>
parents: 2236
diff changeset
1606 {
3d0a7beb0d75 Made reading/writing undo info a bit more robust.
Bram Moolenaar <bram@vim.org>
parents: 2236
diff changeset
1607 verbose_enter();
3d0a7beb0d75 Made reading/writing undo info a bit more robust.
Bram Moolenaar <bram@vim.org>
parents: 2236
diff changeset
1608 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
1609 file_name);
3d0a7beb0d75 Made reading/writing undo info a bit more robust.
Bram Moolenaar <bram@vim.org>
parents: 2236
diff changeset
1610 verbose_leave();
3d0a7beb0d75 Made reading/writing undo info a bit more robust.
Bram Moolenaar <bram@vim.org>
parents: 2236
diff changeset
1611 }
3d0a7beb0d75 Made reading/writing undo info a bit more robust.
Bram Moolenaar <bram@vim.org>
parents: 2236
diff changeset
1612 return;
3d0a7beb0d75 Made reading/writing undo info a bit more robust.
Bram Moolenaar <bram@vim.org>
parents: 2236
diff changeset
1613 }
3d0a7beb0d75 Made reading/writing undo info a bit more robust.
Bram Moolenaar <bram@vim.org>
parents: 2236
diff changeset
1614 #endif
2214
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1615 }
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1616 else
2311
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1617 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
1618
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1619 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
1620 {
4ba83ae8d505 Do not write an undo file if there is nothing to undo.
Bram Moolenaar <bram@vim.org>
parents: 2234
diff changeset
1621 verbose_enter();
2214
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1622 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
1623 verbose_leave();
4ba83ae8d505 Do not write an undo file if there is nothing to undo.
Bram Moolenaar <bram@vim.org>
parents: 2234
diff changeset
1624 }
2238
3d0a7beb0d75 Made reading/writing undo info a bit more robust.
Bram Moolenaar <bram@vim.org>
parents: 2236
diff changeset
1625
2214
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1626 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
1627 if (fp == NULL)
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1628 {
2311
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1629 if (name != NULL || p_verbose > 0)
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1630 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
1631 goto error;
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1632 }
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1633
2231
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1634 /*
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1635 * 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
1636 */
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1637 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
1638 || 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
1639 {
2231
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1640 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
1641 goto error;
2214
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1642 }
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1643 version = get2c(fp);
2342
f6540762173d Fixes and improvements for MS-Windows build.
Bram Moolenaar <bram@vim.org>
parents: 2311
diff changeset
1644 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
1645 {
732cb7b31956 Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents: 2238
diff changeset
1646 #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
1647 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
1648 {
646d34788036 Fix a few compiler warnings. Fix crash with encrypted undo file.
Bram Moolenaar <bram@vim.org>
parents: 2245
diff changeset
1649 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
1650 file_name);
646d34788036 Fix a few compiler warnings. Fix crash with encrypted undo file.
Bram Moolenaar <bram@vim.org>
parents: 2245
diff changeset
1651 goto error;
646d34788036 Fix a few compiler warnings. Fix crash with encrypted undo file.
Bram Moolenaar <bram@vim.org>
parents: 2245
diff changeset
1652 }
2239
732cb7b31956 Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents: 2238
diff changeset
1653 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
1654 {
732cb7b31956 Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents: 2238
diff changeset
1655 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
1656 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
1657 }
2267
c08f91142c41 Crypt the swapfile.
Bram Moolenaar <bram@vim.org>
parents: 2266
diff changeset
1658 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
1659 #else
2311
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1660 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
1661 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
1662 #endif
732cb7b31956 Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents: 2238
diff changeset
1663 }
2342
f6540762173d Fixes and improvements for MS-Windows build.
Bram Moolenaar <bram@vim.org>
parents: 2311
diff changeset
1664 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
1665 {
2311
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1666 EMSG2(_("E824: Incompatible undo file: %s"), file_name);
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1667 goto error;
2214
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1668 }
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1669
2229
d45902a5c61c Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents: 2223
diff changeset
1670 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
1671 {
2231
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1672 corruption_error("hash", file_name);
2311
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1673 goto error;
2229
d45902a5c61c Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents: 2223
diff changeset
1674 }
2214
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1675 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
1676 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
1677 || 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
1678 {
2311
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1679 if (p_verbose > 0 || name != NULL)
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1680 {
2235
4ba83ae8d505 Do not write an undo file if there is nothing to undo.
Bram Moolenaar <bram@vim.org>
parents: 2234
diff changeset
1681 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
1682 verbose_enter();
2311
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1683 give_warning((char_u *)
2238
3d0a7beb0d75 Made reading/writing undo info a bit more robust.
Bram Moolenaar <bram@vim.org>
parents: 2236
diff changeset
1684 _("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
1685 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
1686 verbose_leave();
2311
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1687 }
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1688 goto error;
2214
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1689 }
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1690
2238
3d0a7beb0d75 Made reading/writing undo info a bit more robust.
Bram Moolenaar <bram@vim.org>
parents: 2236
diff changeset
1691 /* 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
1692 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
1693 if (str_len < 0)
2311
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1694 goto error;
2238
3d0a7beb0d75 Made reading/writing undo info a bit more robust.
Bram Moolenaar <bram@vim.org>
parents: 2236
diff changeset
1695 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
1696 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
1697 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
1698 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
1699 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
1700 {
3d0a7beb0d75 Made reading/writing undo info a bit more robust.
Bram Moolenaar <bram@vim.org>
parents: 2236
diff changeset
1701 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
1702 goto error;
3d0a7beb0d75 Made reading/writing undo info a bit more robust.
Bram Moolenaar <bram@vim.org>
parents: 2236
diff changeset
1703 }
2214
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1704
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1705 /* 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
1706 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
1707 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
1708 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
1709 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
1710 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
1711 seq_cur = get4c(fp);
2229
d45902a5c61c Fix a few more things for persistent undo.
Bram Moolenaar <bram@vim.org>
parents: 2223
diff changeset
1712 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
1713
2342
f6540762173d Fixes and improvements for MS-Windows build.
Bram Moolenaar <bram@vim.org>
parents: 2311
diff changeset
1714 /* Optional header fields. */
f6540762173d Fixes and improvements for MS-Windows build.
Bram Moolenaar <bram@vim.org>
parents: 2311
diff changeset
1715 for (;;)
f6540762173d Fixes and improvements for MS-Windows build.
Bram Moolenaar <bram@vim.org>
parents: 2311
diff changeset
1716 {
f6540762173d Fixes and improvements for MS-Windows build.
Bram Moolenaar <bram@vim.org>
parents: 2311
diff changeset
1717 int len = getc(fp);
f6540762173d Fixes and improvements for MS-Windows build.
Bram Moolenaar <bram@vim.org>
parents: 2311
diff changeset
1718 int what;
f6540762173d Fixes and improvements for MS-Windows build.
Bram Moolenaar <bram@vim.org>
parents: 2311
diff changeset
1719
f6540762173d Fixes and improvements for MS-Windows build.
Bram Moolenaar <bram@vim.org>
parents: 2311
diff changeset
1720 if (len == 0 || len == EOF)
f6540762173d Fixes and improvements for MS-Windows build.
Bram Moolenaar <bram@vim.org>
parents: 2311
diff changeset
1721 break;
f6540762173d Fixes and improvements for MS-Windows build.
Bram Moolenaar <bram@vim.org>
parents: 2311
diff changeset
1722 what = getc(fp);
f6540762173d Fixes and improvements for MS-Windows build.
Bram Moolenaar <bram@vim.org>
parents: 2311
diff changeset
1723 switch (what)
2280
941ff1cd317a Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents: 2271
diff changeset
1724 {
2342
f6540762173d Fixes and improvements for MS-Windows build.
Bram Moolenaar <bram@vim.org>
parents: 2311
diff changeset
1725 case UF_LAST_SAVE_NR:
f6540762173d Fixes and improvements for MS-Windows build.
Bram Moolenaar <bram@vim.org>
parents: 2311
diff changeset
1726 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
1727 break;
2342
f6540762173d Fixes and improvements for MS-Windows build.
Bram Moolenaar <bram@vim.org>
parents: 2311
diff changeset
1728 default:
f6540762173d Fixes and improvements for MS-Windows build.
Bram Moolenaar <bram@vim.org>
parents: 2311
diff changeset
1729 /* field not supported, skip */
f6540762173d Fixes and improvements for MS-Windows build.
Bram Moolenaar <bram@vim.org>
parents: 2311
diff changeset
1730 while (--len >= 0)
f6540762173d Fixes and improvements for MS-Windows build.
Bram Moolenaar <bram@vim.org>
parents: 2311
diff changeset
1731 (void)getc(fp);
2280
941ff1cd317a Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents: 2271
diff changeset
1732 }
2342
f6540762173d Fixes and improvements for MS-Windows build.
Bram Moolenaar <bram@vim.org>
parents: 2311
diff changeset
1733 }
2280
941ff1cd317a Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents: 2271
diff changeset
1734
2214
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1735 /* 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
1736 * 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
1737 * 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
1738 * 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
1739 if (num_head > 0)
290ee42cae85 Remove old and unused method to allocate memory for undo.
Bram Moolenaar <bram@vim.org>
parents: 2229
diff changeset
1740 {
290ee42cae85 Remove old and unused method to allocate memory for undo.
Bram Moolenaar <bram@vim.org>
parents: 2229
diff changeset
1741 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
1742 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
1743 if (uhp_table == NULL)
290ee42cae85 Remove old and unused method to allocate memory for undo.
Bram Moolenaar <bram@vim.org>
parents: 2229
diff changeset
1744 goto error;
290ee42cae85 Remove old and unused method to allocate memory for undo.
Bram Moolenaar <bram@vim.org>
parents: 2229
diff changeset
1745 }
2214
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1746
2231
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1747 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
1748 {
2223
81b83a19e127 More strict checks for the undo file.
Bram Moolenaar <bram@vim.org>
parents: 2222
diff changeset
1749 if (num_read_uhps >= num_head)
81b83a19e127 More strict checks for the undo file.
Bram Moolenaar <bram@vim.org>
parents: 2222
diff changeset
1750 {
2238
3d0a7beb0d75 Made reading/writing undo info a bit more robust.
Bram Moolenaar <bram@vim.org>
parents: 2236
diff changeset
1751 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
1752 goto error;
81b83a19e127 More strict checks for the undo file.
Bram Moolenaar <bram@vim.org>
parents: 2222
diff changeset
1753 }
81b83a19e127 More strict checks for the undo file.
Bram Moolenaar <bram@vim.org>
parents: 2222
diff changeset
1754
2342
f6540762173d Fixes and improvements for MS-Windows build.
Bram Moolenaar <bram@vim.org>
parents: 2311
diff changeset
1755 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
1756 if (uhp == NULL)
3d0a7beb0d75 Made reading/writing undo info a bit more robust.
Bram Moolenaar <bram@vim.org>
parents: 2236
diff changeset
1757 goto error;
2233
43cad213cb7f A bit of cleanup and simplification for undofile.
Bram Moolenaar <bram@vim.org>
parents: 2232
diff changeset
1758 uhp_table[num_read_uhps++] = uhp;
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
43cad213cb7f A bit of cleanup and simplification for undofile.
Bram Moolenaar <bram@vim.org>
parents: 2232
diff changeset
1761 if (num_read_uhps != num_head)
43cad213cb7f A bit of cleanup and simplification for undofile.
Bram Moolenaar <bram@vim.org>
parents: 2232
diff changeset
1762 {
43cad213cb7f A bit of cleanup and simplification for undofile.
Bram Moolenaar <bram@vim.org>
parents: 2232
diff changeset
1763 corruption_error("num_head", file_name);
43cad213cb7f A bit of cleanup and simplification for undofile.
Bram Moolenaar <bram@vim.org>
parents: 2232
diff changeset
1764 goto error;
2214
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1765 }
2231
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1766 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
1767 {
2231
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1768 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
1769 goto error;
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1770 }
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1771
2231
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1772 #ifdef U_DEBUG
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1773 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
1774 (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
1775 # 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
1776 #else
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1777 # 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
1778 #endif
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1779
2238
3d0a7beb0d75 Made reading/writing undo info a bit more robust.
Bram Moolenaar <bram@vim.org>
parents: 2236
diff changeset
1780 /* 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
1781 * 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
1782 * 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
1783 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
1784 {
2311
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1785 uhp = uhp_table[i];
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1786 if (uhp == NULL)
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1787 continue;
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1788 for (j = 0; j < num_head; j++)
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1789 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
1790 && 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
1791 {
2233
43cad213cb7f A bit of cleanup and simplification for undofile.
Bram Moolenaar <bram@vim.org>
parents: 2232
diff changeset
1792 corruption_error("duplicate uh_seq", file_name);
2311
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1793 goto error;
2233
43cad213cb7f A bit of cleanup and simplification for undofile.
Bram Moolenaar <bram@vim.org>
parents: 2232
diff changeset
1794 }
2311
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1795 for (j = 0; j < num_head; j++)
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1796 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
1797 && 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
1798 {
2311
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1799 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
1800 SET_FLAG(j);
2242
bc4685345719 Don't use pointers to store numbers, use a union.
Bram Moolenaar <bram@vim.org>
parents: 2239
diff changeset
1801 break;
2231
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1802 }
2311
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1803 for (j = 0; j < num_head; j++)
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1804 if (uhp_table[j] != NULL
2242
bc4685345719 Don't use pointers to store numbers, use a union.
Bram Moolenaar <bram@vim.org>
parents: 2239
diff changeset
1805 && uhp_table[j]->uh_seq == uhp->uh_prev.seq)
2231
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1806 {
2311
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1807 uhp->uh_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
1808 SET_FLAG(j);
2242
bc4685345719 Don't use pointers to store numbers, use a union.
Bram Moolenaar <bram@vim.org>
parents: 2239
diff changeset
1809 break;
2231
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1810 }
2311
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1811 for (j = 0; j < num_head; j++)
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1812 if (uhp_table[j] != NULL
2242
bc4685345719 Don't use pointers to store numbers, use a union.
Bram Moolenaar <bram@vim.org>
parents: 2239
diff changeset
1813 && uhp_table[j]->uh_seq == uhp->uh_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
1814 {
2311
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1815 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
1816 SET_FLAG(j);
2242
bc4685345719 Don't use pointers to store numbers, use a union.
Bram Moolenaar <bram@vim.org>
parents: 2239
diff changeset
1817 break;
2231
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1818 }
2311
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1819 for (j = 0; j < num_head; j++)
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1820 if (uhp_table[j] != NULL
2242
bc4685345719 Don't use pointers to store numbers, use a union.
Bram Moolenaar <bram@vim.org>
parents: 2239
diff changeset
1821 && uhp_table[j]->uh_seq == uhp->uh_alt_prev.seq)
2231
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1822 {
2311
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1823 uhp->uh_alt_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
1824 SET_FLAG(j);
2242
bc4685345719 Don't use pointers to store numbers, use a union.
Bram Moolenaar <bram@vim.org>
parents: 2239
diff changeset
1825 break;
2231
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1826 }
2311
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1827 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
1828 {
2311
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1829 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
1830 SET_FLAG(i);
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1831 }
2311
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1832 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
1833 {
2311
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1834 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
1835 SET_FLAG(i);
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1836 }
2311
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1837 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
1838 {
2311
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1839 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
1840 SET_FLAG(i);
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1841 }
2214
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1842 }
2231
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1843
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1844 /* 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
1845 * 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
1846 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
1847 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
1848 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
1849 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
1850 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
1851 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
1852 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
1853 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
1854 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
1855 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
1856 curbuf->b_u_time_cur = seq_time;
2281
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
1857 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
1858
43cad213cb7f A bit of cleanup and simplification for undofile.
Bram Moolenaar <bram@vim.org>
parents: 2232
diff changeset
1859 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
1860 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
1861
2214
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1862 #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
1863 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
1864 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
1865 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
1866 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
1867 u_check(TRUE);
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1868 #endif
2231
aa6412cab544 Various improvements to undo file code to make it more robust.
Bram Moolenaar <bram@vim.org>
parents: 2230
diff changeset
1869
2214
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1870 if (name != NULL)
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1871 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
1872 goto theend;
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1873
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1874 error:
2230
290ee42cae85 Remove old and unused method to allocate memory for undo.
Bram Moolenaar <bram@vim.org>
parents: 2229
diff changeset
1875 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
1876 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
1877 {
2311
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1878 for (i = 0; i < num_read_uhps; i++)
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1879 if (uhp_table[i] != NULL)
2223
81b83a19e127 More strict checks for the undo file.
Bram Moolenaar <bram@vim.org>
parents: 2222
diff changeset
1880 u_free_uhp(uhp_table[i]);
2311
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1881 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
1882 }
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1883
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1884 theend:
2267
c08f91142c41 Crypt the swapfile.
Bram Moolenaar <bram@vim.org>
parents: 2266
diff changeset
1885 #ifdef FEAT_CRYPT
c08f91142c41 Crypt the swapfile.
Bram Moolenaar <bram@vim.org>
parents: 2266
diff changeset
1886 if (do_decrypt)
c08f91142c41 Crypt the swapfile.
Bram Moolenaar <bram@vim.org>
parents: 2266
diff changeset
1887 crypt_pop_state();
c08f91142c41 Crypt the swapfile.
Bram Moolenaar <bram@vim.org>
parents: 2266
diff changeset
1888 #endif
2214
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1889 if (fp != NULL)
2311
ccda151dde4e Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents: 2289
diff changeset
1890 fclose(fp);
2214
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1891 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
1892 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
1893 return;
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
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1896 #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
1897
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
1898
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1899 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1900 * If 'cpoptions' contains 'u': Undo the previous undo or redo (vi compatible).
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1901 * If 'cpoptions' does not contain 'u': Always undo.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1902 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1903 void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1904 u_undo(count)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1905 int count;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1906 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1907 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1908 * If we get an undo command while executing a macro, we behave like the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1909 * original vi. If this happens twice in one macro the result will not
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1910 * be compatible.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1911 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1912 if (curbuf->b_u_synced == FALSE)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1913 {
825
6675076019ae updated for version 7.0d
vimboss
parents: 810
diff changeset
1914 u_sync(TRUE);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1915 count = 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1916 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1917
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1918 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1919 undo_undoes = TRUE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1920 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1921 undo_undoes = !undo_undoes;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1922 u_doit(count);
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 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1926 * If 'cpoptions' contains 'u': Repeat the previous undo or redo.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1927 * If 'cpoptions' does not contain 'u': Always redo.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1928 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1929 void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1930 u_redo(count)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1931 int count;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1932 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1933 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1934 undo_undoes = FALSE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1935 u_doit(count);
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 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1939 * Undo or redo, depending on 'undo_undoes', 'count' times.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1940 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1941 static void
777
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
1942 u_doit(startcount)
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
1943 int startcount;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1944 {
777
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
1945 int count = startcount;
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
1946
632
b6632d553df3 updated for version 7.0182
vimboss
parents: 481
diff changeset
1947 if (!undo_allowed())
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1948 return;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1949
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1950 u_newcount = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1951 u_oldcount = 0;
179
7fd70926e2e1 updated for version 7.0055
vimboss
parents: 168
diff changeset
1952 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7fd70926e2e1 updated for version 7.0055
vimboss
parents: 168
diff changeset
1953 u_oldcount = -1;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1954 while (count--)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1955 {
2288
4dda2bd944e1 Apply patch 7.2.445.
Bram Moolenaar <bram@vim.org>
parents: 2282
diff changeset
1956 /* 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
1957 * 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
1958 * 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
1959 * and more. */
4dda2bd944e1 Apply patch 7.2.445.
Bram Moolenaar <bram@vim.org>
parents: 2282
diff changeset
1960 change_warning(0);
4dda2bd944e1 Apply patch 7.2.445.
Bram Moolenaar <bram@vim.org>
parents: 2282
diff changeset
1961
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1962 if (undo_undoes)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1963 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1964 if (curbuf->b_u_curhead == NULL) /* first undo */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1965 curbuf->b_u_curhead = curbuf->b_u_newhead;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1966 else if (p_ul > 0) /* multi level undo */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1967 /* get next undo */
2242
bc4685345719 Don't use pointers to store numbers, use a union.
Bram Moolenaar <bram@vim.org>
parents: 2239
diff changeset
1968 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_next.ptr;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1969 /* nothing to undo */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1970 if (curbuf->b_u_numhead == 0 || curbuf->b_u_curhead == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1971 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1972 /* stick curbuf->b_u_curhead at end */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1973 curbuf->b_u_curhead = curbuf->b_u_oldhead;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1974 beep_flush();
777
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
1975 if (count == startcount - 1)
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
1976 {
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
1977 MSG(_("Already at oldest change"));
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
1978 return;
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
1979 }
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1980 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1981 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1982
777
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
1983 u_undoredo(TRUE);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1984 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1985 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1986 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1987 if (curbuf->b_u_curhead == NULL || p_ul <= 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1988 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1989 beep_flush(); /* nothing to redo */
777
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
1990 if (count == startcount - 1)
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
1991 {
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
1992 MSG(_("Already at newest change"));
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
1993 return;
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
1994 }
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1995 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1996 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1997
777
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
1998 u_undoredo(FALSE);
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
1999
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2000 /* Advance for next redo. Set "newhead" when at the end of the
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2001 * redoable changes. */
2242
bc4685345719 Don't use pointers to store numbers, use a union.
Bram Moolenaar <bram@vim.org>
parents: 2239
diff changeset
2002 if (curbuf->b_u_curhead->uh_prev.ptr == NULL)
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2003 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
2004 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_prev.ptr;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2005 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2006 }
798
95dac6af3b3a updated for version 7.0232
vimboss
parents: 794
diff changeset
2007 u_undo_end(undo_undoes, FALSE);
753
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 /*
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2011 * Undo or redo over the timeline.
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2012 * 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
2013 * 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
2014 * seconds.
2281
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2015 * When "file" is TRUE use "step" as a number of file writes.
772
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2016 * When "absolute" is TRUE use "step" as the sequence number to jump to.
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2017 * "sec" must be FALSE then.
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2018 */
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2019 void
2281
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2020 undo_time(step, sec, file, absolute)
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2021 long step;
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2022 int sec;
2281
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2023 int file;
772
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2024 int absolute;
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2025 {
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2026 long target;
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2027 long closest;
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2028 long closest_start;
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2029 long closest_seq = 0;
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2030 long val;
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2031 u_header_T *uhp;
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2032 u_header_T *last;
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2033 int mark;
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2034 int nomark;
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2035 int round;
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2036 int dosec = sec;
2281
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2037 int dofile = file;
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2038 int above = FALSE;
794
f19994020dad updated for version 7.0231
vimboss
parents: 777
diff changeset
2039 int did_undo = TRUE;
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2040
766
f0d0d3d3a1e2 updated for version 7.0225
vimboss
parents: 758
diff changeset
2041 /* First make sure the current undoable change is synced. */
f0d0d3d3a1e2 updated for version 7.0225
vimboss
parents: 758
diff changeset
2042 if (curbuf->b_u_synced == FALSE)
825
6675076019ae updated for version 7.0d
vimboss
parents: 810
diff changeset
2043 u_sync(TRUE);
766
f0d0d3d3a1e2 updated for version 7.0225
vimboss
parents: 758
diff changeset
2044
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2045 u_newcount = 0;
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2046 u_oldcount = 0;
179
7fd70926e2e1 updated for version 7.0055
vimboss
parents: 168
diff changeset
2047 if (curbuf->b_ml.ml_flags & ML_EMPTY)
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2048 u_oldcount = -1;
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2049
777
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2050 /* "target" is the node below which we want to be.
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2051 * Init "closest" to a value we can't reach. */
772
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2052 if (absolute)
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2053 {
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2054 target = step;
777
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2055 closest = -1;
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2056 }
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2057 else
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2058 {
777
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2059 /* When doing computations with time_t subtract starttime, because
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2060 * 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
2061 if (dosec)
2280
941ff1cd317a Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents: 2271
diff changeset
2062 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
2063 else if (dofile)
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2064 {
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2065 if (step < 0)
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2066 {
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2067 /* 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
2068 * 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
2069 * 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
2070 uhp = curbuf->b_u_curhead;
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2071 if (uhp != NULL)
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2072 uhp = uhp->uh_next.ptr;
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2073 else
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2074 uhp = curbuf->b_u_newhead;
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2075 if (uhp != NULL && uhp->uh_save_nr != 0)
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2076 /* "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
2077 * there were no changes since the last write */
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2078 target = curbuf->b_u_save_nr_cur + step;
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2079 else
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2080 /* 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
2081 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
2082 if (target <= 0)
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2083 /* 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
2084 * the sequence number for that. */
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2085 dofile = FALSE;
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2086 }
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2087 else
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2088 {
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2089 /* Moving forward to a newer write. */
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2090 target = curbuf->b_u_save_nr_cur + step;
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2091 if (target > curbuf->b_u_save_nr_last)
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2092 {
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2093 /* 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
2094 * the sequence number for that. */
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2095 target = curbuf->b_u_seq_last + 1;
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2096 dofile = FALSE;
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 }
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2099 }
777
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2100 else
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2101 target = curbuf->b_u_seq_cur + step;
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2102 if (step < 0)
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2103 {
777
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2104 if (target < 0)
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2105 target = 0;
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2106 closest = -1;
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2107 }
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2108 else
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2109 {
2281
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2110 if (dosec)
798
95dac6af3b3a updated for version 7.0232
vimboss
parents: 794
diff changeset
2111 closest = (long)(time(NULL) - starttime + 1);
2281
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2112 else if (dofile)
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2113 closest = curbuf->b_u_save_nr_last + 2;
777
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2114 else
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2115 closest = curbuf->b_u_seq_last + 2;
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2116 if (target >= closest)
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2117 target = closest - 1;
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2118 }
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2119 }
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2120 closest_start = closest;
777
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2121 closest_seq = curbuf->b_u_seq_cur;
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2122
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2123 /*
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2124 * May do this twice:
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2125 * 1. Search for "target", update "closest" to the best match found.
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2126 * 2. If "target" not found search for "closest".
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2127 *
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2128 * When using the closest time we use the sequence number in the second
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2129 * round, because there may be several entries with the same time.
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2130 */
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2131 for (round = 1; round <= 2; ++round)
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2132 {
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2133 /* 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
2134 * 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
2135 * it. We put "nomark" in uh_walk where we have been without success,
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2136 * "mark" where it could possibly be. */
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2137 mark = ++lastmark;
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2138 nomark = ++lastmark;
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2139
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2140 if (curbuf->b_u_curhead == NULL) /* at leaf of the tree */
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2141 uhp = curbuf->b_u_newhead;
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2142 else
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2143 uhp = curbuf->b_u_curhead;
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2144
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2145 while (uhp != NULL)
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2146 {
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2147 uhp->uh_walk = mark;
2281
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2148 if (dosec)
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2149 val = (long)(uhp->uh_time - starttime);
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2150 else if (dofile)
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2151 val = uhp->uh_save_nr;
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2152 else
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2153 val = uhp->uh_seq;
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2154
2281
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2155 if (round == 1 && !(dofile && val == 0))
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2156 {
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2157 /* Remember the header that is closest to the target.
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2158 * It must be at least in the right direction (checked with
777
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2159 * "b_u_seq_cur"). When the timestamp is equal find the
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2160 * highest/lowest sequence number. */
777
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2161 if ((step < 0 ? uhp->uh_seq <= curbuf->b_u_seq_cur
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2162 : uhp->uh_seq > curbuf->b_u_seq_cur)
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2163 && ((dosec && val == closest)
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2164 ? (step < 0
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 : uhp->uh_seq > closest_seq)
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2167 : closest == closest_start
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2168 || (val > target
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2169 ? (closest > target
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2170 ? val - target <= closest - target
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2171 : val - target <= target - closest)
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2172 : (closest > target
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2173 ? target - val <= closest - target
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2174 : target - val <= target - closest))))
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2175 {
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2176 closest = val;
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2177 closest_seq = uhp->uh_seq;
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
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2181 /* Quit searching when we found a match. But when searching for a
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2182 * time we need to continue looking for the best uh_seq. */
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2183 if (target == val && !dosec)
2281
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2184 {
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2185 target = uhp->uh_seq;
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2186 break;
2281
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2187 }
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2188
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2189 /* 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
2190 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
2191 && 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
2192 uhp = uhp->uh_prev.ptr;
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2193
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2194 /* 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
2195 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
2196 && 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
2197 && 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
2198 uhp = uhp->uh_alt_next.ptr;
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2199
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2200 /* 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
2201 * 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
2202 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
2203 && 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
2204 && uhp->uh_next.ptr->uh_walk != mark)
798
95dac6af3b3a updated for version 7.0232
vimboss
parents: 794
diff changeset
2205 {
95dac6af3b3a updated for version 7.0232
vimboss
parents: 794
diff changeset
2206 /* If still at the start we don't go through this change. */
95dac6af3b3a updated for version 7.0232
vimboss
parents: 794
diff changeset
2207 if (uhp == curbuf->b_u_curhead)
95dac6af3b3a updated for version 7.0232
vimboss
parents: 794
diff changeset
2208 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
2209 uhp = uhp->uh_next.ptr;
798
95dac6af3b3a updated for version 7.0232
vimboss
parents: 794
diff changeset
2210 }
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2211
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2212 else
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2213 {
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2214 /* need to backtrack; mark this node as useless */
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2215 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
2216 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
2217 uhp = uhp->uh_alt_prev.ptr;
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2218 else
2242
bc4685345719 Don't use pointers to store numbers, use a union.
Bram Moolenaar <bram@vim.org>
parents: 2239
diff changeset
2219 uhp = uhp->uh_next.ptr;
753
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
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2223 if (uhp != NULL) /* found it */
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2224 break;
772
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2225
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2226 if (absolute)
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2227 {
2214
f8222d1f9a73 Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents: 2210
diff changeset
2228 EMSGN(_("E830: Undo number %ld not found"), step);
772
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2229 return;
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2230 }
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2231
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2232 if (closest == closest_start)
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2233 {
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2234 if (step < 0)
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2235 MSG(_("Already at oldest change"));
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2236 else
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2237 MSG(_("Already at newest change"));
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2238 return;
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2239 }
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2240
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2241 target = closest_seq;
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2242 dosec = FALSE;
2281
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2243 dofile = FALSE;
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2244 if (step < 0)
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2245 above = TRUE; /* stop above the header */
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2246 }
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2247
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2248 /* 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
2249 if (uhp != NULL)
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2250 {
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2251 /*
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2252 * First go up the tree as much as needed.
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2253 */
2288
4dda2bd944e1 Apply patch 7.2.445.
Bram Moolenaar <bram@vim.org>
parents: 2282
diff changeset
2254 while (!got_int)
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2255 {
2288
4dda2bd944e1 Apply patch 7.2.445.
Bram Moolenaar <bram@vim.org>
parents: 2282
diff changeset
2256 /* 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
2257 change_warning(0);
4dda2bd944e1 Apply patch 7.2.445.
Bram Moolenaar <bram@vim.org>
parents: 2282
diff changeset
2258
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2259 uhp = curbuf->b_u_curhead;
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2260 if (uhp == NULL)
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2261 uhp = curbuf->b_u_newhead;
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2262 else
2242
bc4685345719 Don't use pointers to store numbers, use a union.
Bram Moolenaar <bram@vim.org>
parents: 2239
diff changeset
2263 uhp = uhp->uh_next.ptr;
777
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2264 if (uhp == NULL || uhp->uh_walk != mark
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2265 || (uhp->uh_seq == target && !above))
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2266 break;
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2267 curbuf->b_u_curhead = uhp;
777
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2268 u_undoredo(TRUE);
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2269 uhp->uh_walk = nomark; /* don't go back down here */
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2270 }
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2271
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2272 /*
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2273 * And now go down the tree (redo), branching off where needed.
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2274 */
2288
4dda2bd944e1 Apply patch 7.2.445.
Bram Moolenaar <bram@vim.org>
parents: 2282
diff changeset
2275 while (!got_int)
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2276 {
2288
4dda2bd944e1 Apply patch 7.2.445.
Bram Moolenaar <bram@vim.org>
parents: 2282
diff changeset
2277 /* 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
2278 change_warning(0);
4dda2bd944e1 Apply patch 7.2.445.
Bram Moolenaar <bram@vim.org>
parents: 2282
diff changeset
2279
4dda2bd944e1 Apply patch 7.2.445.
Bram Moolenaar <bram@vim.org>
parents: 2282
diff changeset
2280 uhp = curbuf->b_u_curhead;
4dda2bd944e1 Apply patch 7.2.445.
Bram Moolenaar <bram@vim.org>
parents: 2282
diff changeset
2281 if (uhp == NULL)
4dda2bd944e1 Apply patch 7.2.445.
Bram Moolenaar <bram@vim.org>
parents: 2282
diff changeset
2282 break;
4dda2bd944e1 Apply patch 7.2.445.
Bram Moolenaar <bram@vim.org>
parents: 2282
diff changeset
2283
1056
e6d25347de2c updated for version 7.0-182
vimboss
parents: 944
diff changeset
2284 /* 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
2285 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
2286 && 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
2287 uhp = uhp->uh_alt_prev.ptr;
1056
e6d25347de2c updated for version 7.0-182
vimboss
parents: 944
diff changeset
2288
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2289 /* Find the last branch with a mark, that's the one. */
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2290 last = uhp;
2242
bc4685345719 Don't use pointers to store numbers, use a union.
Bram Moolenaar <bram@vim.org>
parents: 2239
diff changeset
2291 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
2292 && 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
2293 last = last->uh_alt_next.ptr;
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2294 if (last != uhp)
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2295 {
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2296 /* Make the used branch the first entry in the list of
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2297 * 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
2298 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
2299 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
2300 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
2301 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
2302 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
2303 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
2304 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
2305 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
2306 uhp->uh_alt_prev.ptr = last;
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2307
2234
3b241fd8d7c0 updated for version 7.2.441
Bram Moolenaar <bram@vim.org>
parents: 2233
diff changeset
2308 if (curbuf->b_u_oldhead == uhp)
3b241fd8d7c0 updated for version 7.2.441
Bram Moolenaar <bram@vim.org>
parents: 2233
diff changeset
2309 curbuf->b_u_oldhead = last;
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2310 uhp = last;
2242
bc4685345719 Don't use pointers to store numbers, use a union.
Bram Moolenaar <bram@vim.org>
parents: 2239
diff changeset
2311 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
2312 uhp->uh_next.ptr->uh_prev.ptr = uhp;
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2313 }
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2314 curbuf->b_u_curhead = uhp;
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2315
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2316 if (uhp->uh_walk != mark)
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2317 break; /* must have reached the target */
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2318
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2319 /* Stop when going backwards in time and didn't find the exact
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2320 * header we were looking for. */
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2321 if (uhp->uh_seq == target && above)
798
95dac6af3b3a updated for version 7.0232
vimboss
parents: 794
diff changeset
2322 {
95dac6af3b3a updated for version 7.0232
vimboss
parents: 794
diff changeset
2323 curbuf->b_u_seq_cur = target - 1;
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2324 break;
798
95dac6af3b3a updated for version 7.0232
vimboss
parents: 794
diff changeset
2325 }
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2326
777
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2327 u_undoredo(FALSE);
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2328
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2329 /* Advance "curhead" to below the header we last used. If it
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2330 * 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
2331 if (uhp->uh_prev.ptr == NULL)
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2332 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
2333 curbuf->b_u_curhead = uhp->uh_prev.ptr;
794
f19994020dad updated for version 7.0231
vimboss
parents: 777
diff changeset
2334 did_undo = FALSE;
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2335
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2336 if (uhp->uh_seq == target) /* found it! */
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2337 break;
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2338
2242
bc4685345719 Don't use pointers to store numbers, use a union.
Bram Moolenaar <bram@vim.org>
parents: 2239
diff changeset
2339 uhp = uhp->uh_prev.ptr;
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2340 if (uhp == NULL || uhp->uh_walk != mark)
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2341 {
777
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2342 /* Need to redo more but can't find it... */
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2343 EMSG2(_(e_intern2), "undo_time()");
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2344 break;
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2345 }
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2346 }
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2347 }
798
95dac6af3b3a updated for version 7.0232
vimboss
parents: 794
diff changeset
2348 u_undo_end(did_undo, absolute);
7
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 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2352 * u_undoredo: common code for undo and redo
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2353 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2354 * 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
2355 * 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
2356 * list for the next undo/redo.
777
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2357 *
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2358 * 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
2359 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2360 static void
777
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2361 u_undoredo(undo)
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2362 int undo;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2363 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2364 char_u **newarray = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2365 linenr_T oldsize;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2366 linenr_T newsize;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2367 linenr_T top, bot;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2368 linenr_T lnum;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2369 linenr_T newlnum = MAXLNUM;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2370 long i;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2371 u_entry_T *uep, *nuep;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2372 u_entry_T *newlist = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2373 int old_flags;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2374 int new_flags;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2375 pos_T namedm[NMARKS];
692
a28f83d37113 updated for version 7.0208
vimboss
parents: 634
diff changeset
2376 #ifdef FEAT_VISUAL
a28f83d37113 updated for version 7.0208
vimboss
parents: 634
diff changeset
2377 visualinfo_T visualinfo;
a28f83d37113 updated for version 7.0208
vimboss
parents: 634
diff changeset
2378 #endif
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2379 int empty_buffer; /* buffer became empty */
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2380 u_header_T *curhead = curbuf->b_u_curhead;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2381
2288
4dda2bd944e1 Apply patch 7.2.445.
Bram Moolenaar <bram@vim.org>
parents: 2282
diff changeset
2382 #ifdef FEAT_AUTOCMD
4dda2bd944e1 Apply patch 7.2.445.
Bram Moolenaar <bram@vim.org>
parents: 2282
diff changeset
2383 /* 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
2384 * invalid till the end. */
4dda2bd944e1 Apply patch 7.2.445.
Bram Moolenaar <bram@vim.org>
parents: 2282
diff changeset
2385 block_autocmds();
4dda2bd944e1 Apply patch 7.2.445.
Bram Moolenaar <bram@vim.org>
parents: 2282
diff changeset
2386 #endif
4dda2bd944e1 Apply patch 7.2.445.
Bram Moolenaar <bram@vim.org>
parents: 2282
diff changeset
2387
1415
20b52d44daaf updated for version 7.1-130
vimboss
parents: 1210
diff changeset
2388 #ifdef U_DEBUG
20b52d44daaf updated for version 7.1-130
vimboss
parents: 1210
diff changeset
2389 u_check(FALSE);
20b52d44daaf updated for version 7.1-130
vimboss
parents: 1210
diff changeset
2390 #endif
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2391 old_flags = curhead->uh_flags;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2392 new_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2393 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2394 setpcmark();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2395
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2396 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2397 * save marks before undo/redo
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2398 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2399 mch_memmove(namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
692
a28f83d37113 updated for version 7.0208
vimboss
parents: 634
diff changeset
2400 #ifdef FEAT_VISUAL
a28f83d37113 updated for version 7.0208
vimboss
parents: 634
diff changeset
2401 visualinfo = curbuf->b_visual;
a28f83d37113 updated for version 7.0208
vimboss
parents: 634
diff changeset
2402 #endif
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2403 curbuf->b_op_start.lnum = curbuf->b_ml.ml_line_count;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2404 curbuf->b_op_start.col = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2405 curbuf->b_op_end.lnum = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2406 curbuf->b_op_end.col = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2407
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2408 for (uep = curhead->uh_entry; uep != NULL; uep = nuep)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2409 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2410 top = uep->ue_top;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2411 bot = uep->ue_bot;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2412 if (bot == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2413 bot = curbuf->b_ml.ml_line_count + 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2414 if (top > curbuf->b_ml.ml_line_count || top >= bot
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2415 || bot > curbuf->b_ml.ml_line_count + 1)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2416 {
2288
4dda2bd944e1 Apply patch 7.2.445.
Bram Moolenaar <bram@vim.org>
parents: 2282
diff changeset
2417 #ifdef FEAT_AUTOCMD
4dda2bd944e1 Apply patch 7.2.445.
Bram Moolenaar <bram@vim.org>
parents: 2282
diff changeset
2418 unblock_autocmds();
4dda2bd944e1 Apply patch 7.2.445.
Bram Moolenaar <bram@vim.org>
parents: 2282
diff changeset
2419 #endif
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2420 EMSG(_("E438: u_undo: line numbers wrong"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2421 changed(); /* don't want UNCHANGED now */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2422 return;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2423 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2424
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2425 oldsize = bot - top - 1; /* number of lines before undo */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2426 newsize = uep->ue_size; /* number of lines after undo */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2427
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2428 if (top < newlnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2429 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2430 /* If the saved cursor is somewhere in this undo block, move it to
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2431 * the remembered position. Makes "gwap" put the cursor back
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2432 * where it was. */
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2433 lnum = curhead->uh_cursor.lnum;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2434 if (lnum >= top && lnum <= top + newsize + 1)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2435 {
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2436 curwin->w_cursor = curhead->uh_cursor;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2437 newlnum = curwin->w_cursor.lnum - 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2438 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2439 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2440 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2441 /* Use the first line that actually changed. Avoids that
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2442 * undoing auto-formatting puts the cursor in the previous
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2443 * line. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2444 for (i = 0; i < newsize && i < oldsize; ++i)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2445 if (STRCMP(uep->ue_array[i], ml_get(top + 1 + i)) != 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2446 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2447 if (i == newsize && newlnum == MAXLNUM && uep->ue_next == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2448 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2449 newlnum = top;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2450 curwin->w_cursor.lnum = newlnum + 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2451 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2452 else if (i < newsize)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2453 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2454 newlnum = top + i;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2455 curwin->w_cursor.lnum = newlnum + 1;
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
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2460 empty_buffer = FALSE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2461
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2462 /* delete the lines between top and bot and save them in newarray */
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
2463 if (oldsize > 0)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2464 {
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
2465 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
2466 sizeof(char_u *) * oldsize)) == NULL)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2467 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2468 do_outofmem_msg((long_u)(sizeof(char_u *) * oldsize));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2469 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2470 * We have messed up the entry list, repair is impossible.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2471 * we have to free the rest of the list.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2472 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2473 while (uep != NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2474 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2475 nuep = uep->ue_next;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2476 u_freeentry(uep, uep->ue_size);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2477 uep = nuep;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2478 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2479 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2480 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2481 /* delete backwards, it goes faster in most cases */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2482 for (lnum = bot - 1, i = oldsize; --i >= 0; --lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2483 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2484 /* what can we do when we run out of memory? */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2485 if ((newarray[i] = u_save_line(lnum)) == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2486 do_outofmem_msg((long_u)0);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2487 /* remember we deleted the last line in the buffer, and a
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2488 * dummy empty line will be inserted */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2489 if (curbuf->b_ml.ml_line_count == 1)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2490 empty_buffer = TRUE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2491 ml_delete(lnum, FALSE);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2492 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2493 }
414
8ab9c77240d4 updated for version 7.0108
vimboss
parents: 407
diff changeset
2494 else
8ab9c77240d4 updated for version 7.0108
vimboss
parents: 407
diff changeset
2495 newarray = NULL;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2496
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2497 /* insert the lines in u_array between top and bot */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2498 if (newsize)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2499 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2500 for (lnum = top, i = 0; i < newsize; ++i, ++lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2501 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2502 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2503 * If the file is empty, there is an empty line 1 that we
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2504 * should get rid of, by replacing it with the new line
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2505 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2506 if (empty_buffer && lnum == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2507 ml_replace((linenr_T)1, uep->ue_array[i], TRUE);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2508 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2509 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
2510 vim_free(uep->ue_array[i]);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2511 }
2230
290ee42cae85 Remove old and unused method to allocate memory for undo.
Bram Moolenaar <bram@vim.org>
parents: 2229
diff changeset
2512 vim_free((char_u *)uep->ue_array);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2513 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2514
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2515 /* adjust marks */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2516 if (oldsize != newsize)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2517 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2518 mark_adjust(top + 1, top + oldsize, (long)MAXLNUM,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2519 (long)newsize - (long)oldsize);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2520 if (curbuf->b_op_start.lnum > top + oldsize)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2521 curbuf->b_op_start.lnum += newsize - oldsize;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2522 if (curbuf->b_op_end.lnum > top + oldsize)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2523 curbuf->b_op_end.lnum += newsize - oldsize;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2524 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2525
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2526 changed_lines(top + 1, 0, bot, newsize - oldsize);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2527
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2528 /* set '[ and '] mark */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2529 if (top + 1 < curbuf->b_op_start.lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2530 curbuf->b_op_start.lnum = top + 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2531 if (newsize == 0 && top + 1 > curbuf->b_op_end.lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2532 curbuf->b_op_end.lnum = top + 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2533 else if (top + newsize > curbuf->b_op_end.lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2534 curbuf->b_op_end.lnum = top + newsize;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2535
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2536 u_newcount += newsize;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2537 u_oldcount += oldsize;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2538 uep->ue_size = oldsize;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2539 uep->ue_array = newarray;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2540 uep->ue_bot = top + newsize + 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2541
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2542 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2543 * insert this entry in front of the new entry list
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2544 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2545 nuep = uep->ue_next;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2546 uep->ue_next = newlist;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2547 newlist = uep;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2548 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2549
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2550 curhead->uh_entry = newlist;
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2551 curhead->uh_flags = new_flags;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2552 if ((old_flags & UH_EMPTYBUF) && bufempty())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2553 curbuf->b_ml.ml_flags |= ML_EMPTY;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2554 if (old_flags & UH_CHANGED)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2555 changed();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2556 else
33
f6033dcbaf31 updated for version 7.0020
vimboss
parents: 7
diff changeset
2557 #ifdef FEAT_NETBEANS_INTG
f6033dcbaf31 updated for version 7.0020
vimboss
parents: 7
diff changeset
2558 /* per netbeans undo rules, keep it as modified */
f6033dcbaf31 updated for version 7.0020
vimboss
parents: 7
diff changeset
2559 if (!isNetbeansModified(curbuf))
f6033dcbaf31 updated for version 7.0020
vimboss
parents: 7
diff changeset
2560 #endif
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2561 unchanged(curbuf, FALSE);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2562
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2563 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2564 * restore marks from before undo/redo
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2565 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2566 for (i = 0; i < NMARKS; ++i)
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2567 if (curhead->uh_namedm[i].lnum != 0)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2568 {
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2569 curbuf->b_namedm[i] = curhead->uh_namedm[i];
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2570 curhead->uh_namedm[i] = namedm[i];
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2571 }
692
a28f83d37113 updated for version 7.0208
vimboss
parents: 634
diff changeset
2572 #ifdef FEAT_VISUAL
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2573 if (curhead->uh_visual.vi_start.lnum != 0)
692
a28f83d37113 updated for version 7.0208
vimboss
parents: 634
diff changeset
2574 {
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2575 curbuf->b_visual = curhead->uh_visual;
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2576 curhead->uh_visual = visualinfo;
692
a28f83d37113 updated for version 7.0208
vimboss
parents: 634
diff changeset
2577 }
a28f83d37113 updated for version 7.0208
vimboss
parents: 634
diff changeset
2578 #endif
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2579
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2580 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2581 * 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
2582 * before starting the change (for the "o" command).
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2583 * Otherwise the cursor should go to the first undone line.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2584 */
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2585 if (curhead->uh_cursor.lnum + 1 == curwin->w_cursor.lnum
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2586 && curwin->w_cursor.lnum > 1)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2587 --curwin->w_cursor.lnum;
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2588 if (curhead->uh_cursor.lnum == curwin->w_cursor.lnum)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2589 {
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2590 curwin->w_cursor.col = curhead->uh_cursor.col;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2591 #ifdef FEAT_VIRTUALEDIT
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2592 if (virtual_active() && curhead->uh_cursor_vcol >= 0)
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2593 coladvance((colnr_T)curhead->uh_cursor_vcol);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2594 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2595 curwin->w_cursor.coladd = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2596 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2597 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2598 else if (curwin->w_cursor.lnum <= curbuf->b_ml.ml_line_count)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2599 beginline(BL_SOL | BL_FIX);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2600 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2601 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2602 /* We get here with the current cursor line being past the end (eg
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2603 * after adding lines at the end of the file, and then undoing it).
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2604 * check_cursor() will move the cursor to the last line. Move it to
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2605 * the first column here. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2606 curwin->w_cursor.col = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2607 #ifdef FEAT_VIRTUALEDIT
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2608 curwin->w_cursor.coladd = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2609 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2610 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2611
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2612 /* Make sure the cursor is on an existing line and column. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2613 check_cursor();
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2614
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2615 /* Remember where we are for "g-" and ":earlier 10s". */
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2616 curbuf->b_u_seq_cur = curhead->uh_seq;
777
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2617 if (undo)
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2618 /* We are below the previous undo. However, to make ":earlier 1s"
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2619 * work we compute this as being just above the just undone change. */
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2620 --curbuf->b_u_seq_cur;
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2621
2281
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2622 /* 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
2623 if (curhead->uh_save_nr != 0)
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2624 {
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2625 if (undo)
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2626 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
2627 else
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2628 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
2629 }
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2630
777
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2631 /* 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
2632 * 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
2633 curbuf->b_u_time_cur = curhead->uh_time;
2288
4dda2bd944e1 Apply patch 7.2.445.
Bram Moolenaar <bram@vim.org>
parents: 2282
diff changeset
2634
4dda2bd944e1 Apply patch 7.2.445.
Bram Moolenaar <bram@vim.org>
parents: 2282
diff changeset
2635 #ifdef FEAT_AUTOCMD
4dda2bd944e1 Apply patch 7.2.445.
Bram Moolenaar <bram@vim.org>
parents: 2282
diff changeset
2636 unblock_autocmds();
4dda2bd944e1 Apply patch 7.2.445.
Bram Moolenaar <bram@vim.org>
parents: 2282
diff changeset
2637 #endif
1415
20b52d44daaf updated for version 7.1-130
vimboss
parents: 1210
diff changeset
2638 #ifdef U_DEBUG
20b52d44daaf updated for version 7.1-130
vimboss
parents: 1210
diff changeset
2639 u_check(FALSE);
20b52d44daaf updated for version 7.1-130
vimboss
parents: 1210
diff changeset
2640 #endif
7
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 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2644 * If we deleted or added lines, report the number of less/more lines.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2645 * Otherwise, report the number of changes (this may be incorrect
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2646 * in some cases, but it's better than nothing).
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2647 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2648 static void
798
95dac6af3b3a updated for version 7.0232
vimboss
parents: 794
diff changeset
2649 u_undo_end(did_undo, absolute)
794
f19994020dad updated for version 7.0231
vimboss
parents: 777
diff changeset
2650 int did_undo; /* just did an undo */
798
95dac6af3b3a updated for version 7.0232
vimboss
parents: 794
diff changeset
2651 int absolute; /* used ":undo N" */
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2652 {
944
b2dcb8457067 updated for version 7.0-070
vimboss
parents: 912
diff changeset
2653 char *msgstr;
772
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2654 u_header_T *uhp;
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2655 char_u msgbuf[80];
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2656
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2657 #ifdef FEAT_FOLDING
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2658 if ((fdo_flags & FDO_UNDO) && KeyTyped)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2659 foldOpenCursor();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2660 #endif
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2661
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2662 if (global_busy /* no messages now, wait until global is finished */
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2663 || !messaging()) /* 'lazyredraw' set, don't do messages now */
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2664 return;
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2665
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2666 if (curbuf->b_ml.ml_flags & ML_EMPTY)
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2667 --u_newcount;
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2668
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2669 u_oldcount -= u_newcount;
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2670 if (u_oldcount == -1)
944
b2dcb8457067 updated for version 7.0-070
vimboss
parents: 912
diff changeset
2671 msgstr = N_("more line");
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2672 else if (u_oldcount < 0)
944
b2dcb8457067 updated for version 7.0-070
vimboss
parents: 912
diff changeset
2673 msgstr = N_("more lines");
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2674 else if (u_oldcount == 1)
944
b2dcb8457067 updated for version 7.0-070
vimboss
parents: 912
diff changeset
2675 msgstr = N_("line less");
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2676 else if (u_oldcount > 1)
944
b2dcb8457067 updated for version 7.0-070
vimboss
parents: 912
diff changeset
2677 msgstr = N_("fewer lines");
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2678 else
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2679 {
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2680 u_oldcount = u_newcount;
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2681 if (u_newcount == 1)
944
b2dcb8457067 updated for version 7.0-070
vimboss
parents: 912
diff changeset
2682 msgstr = N_("change");
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2683 else
944
b2dcb8457067 updated for version 7.0-070
vimboss
parents: 912
diff changeset
2684 msgstr = N_("changes");
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2685 }
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2686
772
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2687 if (curbuf->b_u_curhead != NULL)
794
f19994020dad updated for version 7.0231
vimboss
parents: 777
diff changeset
2688 {
798
95dac6af3b3a updated for version 7.0232
vimboss
parents: 794
diff changeset
2689 /* 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
2690 if (absolute && curbuf->b_u_curhead->uh_next.ptr != NULL)
798
95dac6af3b3a updated for version 7.0232
vimboss
parents: 794
diff changeset
2691 {
2242
bc4685345719 Don't use pointers to store numbers, use a union.
Bram Moolenaar <bram@vim.org>
parents: 2239
diff changeset
2692 uhp = curbuf->b_u_curhead->uh_next.ptr;
798
95dac6af3b3a updated for version 7.0232
vimboss
parents: 794
diff changeset
2693 did_undo = FALSE;
95dac6af3b3a updated for version 7.0232
vimboss
parents: 794
diff changeset
2694 }
95dac6af3b3a updated for version 7.0232
vimboss
parents: 794
diff changeset
2695 else if (did_undo)
794
f19994020dad updated for version 7.0231
vimboss
parents: 777
diff changeset
2696 uhp = curbuf->b_u_curhead;
f19994020dad updated for version 7.0231
vimboss
parents: 777
diff changeset
2697 else
2242
bc4685345719 Don't use pointers to store numbers, use a union.
Bram Moolenaar <bram@vim.org>
parents: 2239
diff changeset
2698 uhp = curbuf->b_u_curhead->uh_next.ptr;
794
f19994020dad updated for version 7.0231
vimboss
parents: 777
diff changeset
2699 }
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2700 else
772
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2701 uhp = curbuf->b_u_newhead;
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2702
772
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2703 if (uhp == NULL)
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2704 *msgbuf = NUL;
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2705 else
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2706 u_add_time(msgbuf, sizeof(msgbuf), uhp->uh_time);
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2707
2282
a888ed7ba375 Make updating text for conceal mode simpler. A few compiler warning fixes.
Bram Moolenaar <bram@vim.org>
parents: 2281
diff changeset
2708 #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
2709 {
a888ed7ba375 Make updating text for conceal mode simpler. A few compiler warning fixes.
Bram Moolenaar <bram@vim.org>
parents: 2281
diff changeset
2710 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
2711
a888ed7ba375 Make updating text for conceal mode simpler. A few compiler warning fixes.
Bram Moolenaar <bram@vim.org>
parents: 2281
diff changeset
2712 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
2713 {
2378
85b7dc8da5eb Add the 'concealcursor' option to decide when the cursor line is to be
Bram Moolenaar <bram@vim.org>
parents: 2367
diff changeset
2714 if (wp->w_buffer == curbuf && wp->w_p_cole > 0)
2282
a888ed7ba375 Make updating text for conceal mode simpler. A few compiler warning fixes.
Bram Moolenaar <bram@vim.org>
parents: 2281
diff changeset
2715 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
2716 }
a888ed7ba375 Make updating text for conceal mode simpler. A few compiler warning fixes.
Bram Moolenaar <bram@vim.org>
parents: 2281
diff changeset
2717 }
a888ed7ba375 Make updating text for conceal mode simpler. A few compiler warning fixes.
Bram Moolenaar <bram@vim.org>
parents: 2281
diff changeset
2718 #endif
a888ed7ba375 Make updating text for conceal mode simpler. A few compiler warning fixes.
Bram Moolenaar <bram@vim.org>
parents: 2281
diff changeset
2719
794
f19994020dad updated for version 7.0231
vimboss
parents: 777
diff changeset
2720 smsg((char_u *)_("%ld %s; %s #%ld %s"),
777
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2721 u_oldcount < 0 ? -u_oldcount : u_oldcount,
944
b2dcb8457067 updated for version 7.0-070
vimboss
parents: 912
diff changeset
2722 _(msgstr),
794
f19994020dad updated for version 7.0231
vimboss
parents: 777
diff changeset
2723 did_undo ? _("before") : _("after"),
f19994020dad updated for version 7.0231
vimboss
parents: 777
diff changeset
2724 uhp == NULL ? 0L : uhp->uh_seq,
f19994020dad updated for version 7.0231
vimboss
parents: 777
diff changeset
2725 msgbuf);
7
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 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2729 * u_sync: stop adding to the current entry list
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2730 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2731 void
825
6675076019ae updated for version 7.0d
vimboss
parents: 810
diff changeset
2732 u_sync(force)
6675076019ae updated for version 7.0d
vimboss
parents: 810
diff changeset
2733 int force; /* Also sync when no_u_sync is set. */
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2734 {
825
6675076019ae updated for version 7.0d
vimboss
parents: 810
diff changeset
2735 /* Skip it when already synced or syncing is disabled. */
6675076019ae updated for version 7.0d
vimboss
parents: 810
diff changeset
2736 if (curbuf->b_u_synced || (!force && no_u_sync > 0))
6675076019ae updated for version 7.0d
vimboss
parents: 810
diff changeset
2737 return;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2738 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2739 if (im_is_preediting())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2740 return; /* XIM is busy, don't break an undo sequence */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2741 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2742 if (p_ul < 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2743 curbuf->b_u_synced = TRUE; /* no entries, nothing to do */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2744 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2745 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2746 u_getbot(); /* compute ue_bot of previous u_save */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2747 curbuf->b_u_curhead = NULL;
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
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2751 /*
772
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2752 * ":undolist": List the leafs of the undo tree
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2753 */
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2754 void
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2755 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
2756 exarg_T *eap UNUSED;
772
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2757 {
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2758 garray_T ga;
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2759 u_header_T *uhp;
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2760 int mark;
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2761 int nomark;
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2762 int changes = 1;
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2763 int i;
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2764
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2765 /*
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2766 * 1: walk the tree to find all leafs, put the info in "ga".
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2767 * 2: sort the lines
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2768 * 3: display the list
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2769 */
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2770 mark = ++lastmark;
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2771 nomark = ++lastmark;
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2772 ga_init2(&ga, (int)sizeof(char *), 20);
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2773
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2774 uhp = curbuf->b_u_oldhead;
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2775 while (uhp != NULL)
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2776 {
2242
bc4685345719 Don't use pointers to store numbers, use a union.
Bram Moolenaar <bram@vim.org>
parents: 2239
diff changeset
2777 if (uhp->uh_prev.ptr == NULL && uhp->uh_walk != nomark
777
f664cc974a7a updated for version 7.0227
vimboss
parents: 772
diff changeset
2778 && uhp->uh_walk != mark)
772
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2779 {
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2780 if (ga_grow(&ga, 1) == FAIL)
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2781 break;
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2782 vim_snprintf((char *)IObuff, IOSIZE, "%6ld %7ld ",
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2783 uhp->uh_seq, changes);
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2784 u_add_time(IObuff + STRLEN(IObuff), IOSIZE - STRLEN(IObuff),
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2785 uhp->uh_time);
2280
941ff1cd317a Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents: 2271
diff changeset
2786 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
2787 {
941ff1cd317a Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents: 2271
diff changeset
2788 while (STRLEN(IObuff) < 32)
941ff1cd317a Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents: 2271
diff changeset
2789 STRCAT(IObuff, " ");
941ff1cd317a Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents: 2271
diff changeset
2790 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
2791 " %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
2792 }
772
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2793 ((char_u **)(ga.ga_data))[ga.ga_len++] = vim_strsave(IObuff);
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2794 }
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2795
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2796 uhp->uh_walk = mark;
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2797
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2798 /* 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
2799 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
2800 && uhp->uh_prev.ptr->uh_walk != mark)
772
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2801 {
2242
bc4685345719 Don't use pointers to store numbers, use a union.
Bram Moolenaar <bram@vim.org>
parents: 2239
diff changeset
2802 uhp = uhp->uh_prev.ptr;
772
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2803 ++changes;
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2804 }
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2805
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2806 /* 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
2807 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
2808 && 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
2809 && 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
2810 uhp = uhp->uh_alt_next.ptr;
772
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2811
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2812 /* 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
2813 * 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
2814 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
2815 && 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
2816 && uhp->uh_next.ptr->uh_walk != mark)
772
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2817 {
2242
bc4685345719 Don't use pointers to store numbers, use a union.
Bram Moolenaar <bram@vim.org>
parents: 2239
diff changeset
2818 uhp = uhp->uh_next.ptr;
772
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2819 --changes;
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2820 }
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2821
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2822 else
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2823 {
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2824 /* need to backtrack; mark this node as done */
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2825 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
2826 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
2827 uhp = uhp->uh_alt_prev.ptr;
772
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2828 else
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2829 {
2242
bc4685345719 Don't use pointers to store numbers, use a union.
Bram Moolenaar <bram@vim.org>
parents: 2239
diff changeset
2830 uhp = uhp->uh_next.ptr;
772
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2831 --changes;
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
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2836 if (ga.ga_len == 0)
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2837 MSG(_("Nothing to undo"));
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2838 else
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2839 {
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2840 sort_strings((char_u **)ga.ga_data, ga.ga_len);
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2841
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2842 msg_start();
2280
941ff1cd317a Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents: 2271
diff changeset
2843 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
2844 hl_attr(HLF_T));
772
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2845 for (i = 0; i < ga.ga_len && !got_int; ++i)
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2846 {
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2847 msg_putchar('\n');
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2848 if (got_int)
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2849 break;
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2850 msg_puts(((char_u **)ga.ga_data)[i]);
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2851 }
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2852 msg_end();
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2853
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2854 ga_clear_strings(&ga);
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 /*
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2859 * 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
2860 */
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2861 static void
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2862 u_add_time(buf, buflen, tt)
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2863 char_u *buf;
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2864 size_t buflen;
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2865 time_t tt;
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2866 {
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2867 #ifdef HAVE_STRFTIME
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2868 struct tm *curtime;
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2869
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2870 if (time(NULL) - tt >= 100)
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2871 {
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2872 curtime = localtime(&tt);
810
9f345c48220b updated for version 7.0c
vimboss
parents: 798
diff changeset
2873 (void)strftime((char *)buf, buflen, "%H:%M:%S", curtime);
772
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2874 }
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2875 else
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2876 #endif
835
8bebcabccc2c updated for version 7.0e01
vimboss
parents: 825
diff changeset
2877 vim_snprintf((char *)buf, buflen, _("%ld seconds ago"),
772
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2878 (long)(time(NULL) - tt));
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2879 }
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2880
aaaca5077255 updated for version 7.0226
vimboss
parents: 766
diff changeset
2881 /*
697
f08390485cd3 updated for version 7.0210
vimboss
parents: 692
diff changeset
2882 * ":undojoin": continue adding to the last entry list
f08390485cd3 updated for version 7.0210
vimboss
parents: 692
diff changeset
2883 */
f08390485cd3 updated for version 7.0210
vimboss
parents: 692
diff changeset
2884 void
f08390485cd3 updated for version 7.0210
vimboss
parents: 692
diff changeset
2885 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
2886 exarg_T *eap UNUSED;
697
f08390485cd3 updated for version 7.0210
vimboss
parents: 692
diff changeset
2887 {
839
1f3b1021f002 updated for version 7.0e05
vimboss
parents: 835
diff changeset
2888 if (curbuf->b_u_newhead == NULL)
1f3b1021f002 updated for version 7.0e05
vimboss
parents: 835
diff changeset
2889 return; /* nothing changed before */
1f3b1021f002 updated for version 7.0e05
vimboss
parents: 835
diff changeset
2890 if (curbuf->b_u_curhead != NULL)
1f3b1021f002 updated for version 7.0e05
vimboss
parents: 835
diff changeset
2891 {
1f3b1021f002 updated for version 7.0e05
vimboss
parents: 835
diff changeset
2892 EMSG(_("E790: undojoin is not allowed after undo"));
1f3b1021f002 updated for version 7.0e05
vimboss
parents: 835
diff changeset
2893 return;
1f3b1021f002 updated for version 7.0e05
vimboss
parents: 835
diff changeset
2894 }
697
f08390485cd3 updated for version 7.0210
vimboss
parents: 692
diff changeset
2895 if (!curbuf->b_u_synced)
f08390485cd3 updated for version 7.0210
vimboss
parents: 692
diff changeset
2896 return; /* already unsynced */
f08390485cd3 updated for version 7.0210
vimboss
parents: 692
diff changeset
2897 if (p_ul < 0)
f08390485cd3 updated for version 7.0210
vimboss
parents: 692
diff changeset
2898 return; /* no entries, nothing to do */
f08390485cd3 updated for version 7.0210
vimboss
parents: 692
diff changeset
2899 else
f08390485cd3 updated for version 7.0210
vimboss
parents: 692
diff changeset
2900 {
f08390485cd3 updated for version 7.0210
vimboss
parents: 692
diff changeset
2901 /* Go back to the last entry */
f08390485cd3 updated for version 7.0210
vimboss
parents: 692
diff changeset
2902 curbuf->b_u_curhead = curbuf->b_u_newhead;
f08390485cd3 updated for version 7.0210
vimboss
parents: 692
diff changeset
2903 curbuf->b_u_synced = FALSE; /* no entries, nothing to do */
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
f08390485cd3 updated for version 7.0210
vimboss
parents: 692
diff changeset
2907 /*
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2908 * Called after writing the file and setting b_changed to FALSE.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2909 * Now an undo means that the buffer is modified.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2910 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2911 void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2912 u_unchanged(buf)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2913 buf_T *buf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2914 {
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2915 u_unch_branch(buf->b_u_oldhead);
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2916 buf->b_did_warn = FALSE;
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2917 }
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2918
2281
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2919 /*
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2920 * 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
2921 * used for "u".
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2922 */
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2923 void
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2924 u_update_save_nr(buf)
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2925 buf_T *buf;
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2926 {
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2927 u_header_T *uhp;
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2928
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2929 ++buf->b_u_save_nr_last;
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2930 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
2931 uhp = buf->b_u_curhead;
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2932 if (uhp != NULL)
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2933 uhp = uhp->uh_next.ptr;
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2934 else
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2935 uhp = buf->b_u_newhead;
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2936 if (uhp != NULL)
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2937 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
2938 }
e41433ea71df Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents: 2280
diff changeset
2939
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2940 static void
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2941 u_unch_branch(uhp)
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2942 u_header_T *uhp;
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2943 {
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
2944 u_header_T *uh;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2945
2242
bc4685345719 Don't use pointers to store numbers, use a union.
Bram Moolenaar <bram@vim.org>
parents: 2239
diff changeset
2946 for (uh = uhp; uh != NULL; uh = uh->uh_prev.ptr)
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2947 {
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2948 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
2949 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
2950 u_unch_branch(uh->uh_alt_next.ptr); /* recursive */
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
2951 }
7
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 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2955 * Get pointer to last added entry.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2956 * If it's not valid, give an error message and return NULL.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2957 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2958 static u_entry_T *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2959 u_get_headentry()
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2960 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2961 if (curbuf->b_u_newhead == NULL || curbuf->b_u_newhead->uh_entry == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2962 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2963 EMSG(_("E439: undo list corrupt"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2964 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2965 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2966 return curbuf->b_u_newhead->uh_entry;
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 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2970 * u_getbot(): compute the line number of the previous u_save
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2971 * It is called only when b_u_synced is FALSE.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2972 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2973 static void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2974 u_getbot()
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2975 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2976 u_entry_T *uep;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2977 linenr_T extra;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2978
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2979 uep = u_get_headentry(); /* check for corrupt undo list */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2980 if (uep == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2981 return;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2982
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2983 uep = curbuf->b_u_newhead->uh_getbot_entry;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2984 if (uep != NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2985 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2986 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2987 * the new ue_bot is computed from the number of lines that has been
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2988 * inserted (0 - deleted) since calling u_save. This is equal to the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2989 * old line count subtracted from the current line count.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2990 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2991 extra = curbuf->b_ml.ml_line_count - uep->ue_lcount;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2992 uep->ue_bot = uep->ue_top + uep->ue_size + 1 + extra;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2993 if (uep->ue_bot < 1 || uep->ue_bot > curbuf->b_ml.ml_line_count)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2994 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2995 EMSG(_("E440: undo line missing"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2996 uep->ue_bot = uep->ue_top + 1; /* assume all lines deleted, will
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2997 * get all the old lines back
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2998 * without deleting the current
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2999 * ones */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3000 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3001
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3002 curbuf->b_u_newhead->uh_getbot_entry = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3003 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3004
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3005 curbuf->b_u_synced = TRUE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3006 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3007
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3008 /*
1415
20b52d44daaf updated for version 7.1-130
vimboss
parents: 1210
diff changeset
3009 * Free one header "uhp" and its entry list and adjust the pointers.
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3010 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3011 static void
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
3012 u_freeheader(buf, uhp, uhpp)
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
3013 buf_T *buf;
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3014 u_header_T *uhp;
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3015 u_header_T **uhpp; /* if not NULL reset when freeing this header */
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3016 {
1415
20b52d44daaf updated for version 7.1-130
vimboss
parents: 1210
diff changeset
3017 u_header_T *uhap;
20b52d44daaf updated for version 7.1-130
vimboss
parents: 1210
diff changeset
3018
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3019 /* When there is an alternate redo list free that branch completely,
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3020 * 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
3021 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
3022 u_freebranch(buf, uhp->uh_alt_next.ptr, uhpp);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3023
2242
bc4685345719 Don't use pointers to store numbers, use a union.
Bram Moolenaar <bram@vim.org>
parents: 2239
diff changeset
3024 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
3025 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = NULL;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3026
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3027 /* 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
3028 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
3029 buf->b_u_oldhead = uhp->uh_prev.ptr;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3030 else
2242
bc4685345719 Don't use pointers to store numbers, use a union.
Bram Moolenaar <bram@vim.org>
parents: 2239
diff changeset
3031 uhp->uh_next.ptr->uh_prev.ptr = uhp->uh_prev.ptr;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3032
2242
bc4685345719 Don't use pointers to store numbers, use a union.
Bram Moolenaar <bram@vim.org>
parents: 2239
diff changeset
3033 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
3034 buf->b_u_newhead = uhp->uh_next.ptr;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3035 else
2242
bc4685345719 Don't use pointers to store numbers, use a union.
Bram Moolenaar <bram@vim.org>
parents: 2239
diff changeset
3036 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
3037 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
3038 uhap->uh_next.ptr = uhp->uh_next.ptr;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3039
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3040 u_freeentries(buf, uhp, uhpp);
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3041 }
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3042
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3043 /*
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
3044 * Free an alternate branch and any following alternate branches.
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3045 */
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3046 static void
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3047 u_freebranch(buf, uhp, uhpp)
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3048 buf_T *buf;
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3049 u_header_T *uhp;
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3050 u_header_T **uhpp; /* if not NULL reset when freeing this header */
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3051 {
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3052 u_header_T *tofree, *next;
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3053
1440
93ffa40b5320 updated for version 7.1-155
vimboss
parents: 1415
diff changeset
3054 /* 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
3055 * all the pointers. */
93ffa40b5320 updated for version 7.1-155
vimboss
parents: 1415
diff changeset
3056 if (uhp == buf->b_u_oldhead)
93ffa40b5320 updated for version 7.1-155
vimboss
parents: 1415
diff changeset
3057 {
93ffa40b5320 updated for version 7.1-155
vimboss
parents: 1415
diff changeset
3058 u_freeheader(buf, uhp, uhpp);
93ffa40b5320 updated for version 7.1-155
vimboss
parents: 1415
diff changeset
3059 return;
93ffa40b5320 updated for version 7.1-155
vimboss
parents: 1415
diff changeset
3060 }
93ffa40b5320 updated for version 7.1-155
vimboss
parents: 1415
diff changeset
3061
2242
bc4685345719 Don't use pointers to store numbers, use a union.
Bram Moolenaar <bram@vim.org>
parents: 2239
diff changeset
3062 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
3063 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = NULL;
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3064
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3065 next = uhp;
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3066 while (next != NULL)
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3067 {
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3068 tofree = next;
2242
bc4685345719 Don't use pointers to store numbers, use a union.
Bram Moolenaar <bram@vim.org>
parents: 2239
diff changeset
3069 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
3070 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
3071 next = tofree->uh_prev.ptr;
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3072 u_freeentries(buf, tofree, uhpp);
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 /*
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3077 * Free all the undo entries for one header and the header itself.
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3078 * This means that "uhp" is invalid when returning.
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3079 */
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3080 static void
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3081 u_freeentries(buf, uhp, uhpp)
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3082 buf_T *buf;
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3083 u_header_T *uhp;
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3084 u_header_T **uhpp; /* if not NULL reset when freeing this header */
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3085 {
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3086 u_entry_T *uep, *nuep;
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3087
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3088 /* Check for pointers to the header that become invalid now. */
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3089 if (buf->b_u_curhead == uhp)
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3090 buf->b_u_curhead = NULL;
1415
20b52d44daaf updated for version 7.1-130
vimboss
parents: 1210
diff changeset
3091 if (buf->b_u_newhead == uhp)
20b52d44daaf updated for version 7.1-130
vimboss
parents: 1210
diff changeset
3092 buf->b_u_newhead = NULL; /* freeing the newest entry */
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3093 if (uhpp != NULL && uhp == *uhpp)
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3094 *uhpp = NULL;
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3095
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3096 for (uep = uhp->uh_entry; uep != NULL; uep = nuep)
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3097 {
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3098 nuep = uep->ue_next;
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3099 u_freeentry(uep, uep->ue_size);
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3100 }
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3101
1415
20b52d44daaf updated for version 7.1-130
vimboss
parents: 1210
diff changeset
3102 #ifdef U_DEBUG
20b52d44daaf updated for version 7.1-130
vimboss
parents: 1210
diff changeset
3103 uhp->uh_magic = 0;
20b52d44daaf updated for version 7.1-130
vimboss
parents: 1210
diff changeset
3104 #endif
2230
290ee42cae85 Remove old and unused method to allocate memory for undo.
Bram Moolenaar <bram@vim.org>
parents: 2229
diff changeset
3105 vim_free((char_u *)uhp);
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
3106 --buf->b_u_numhead;
7
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 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3110 * free entry 'uep' and 'n' lines in uep->ue_array[]
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3111 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3112 static void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3113 u_freeentry(uep, n)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3114 u_entry_T *uep;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3115 long n;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3116 {
414
8ab9c77240d4 updated for version 7.0108
vimboss
parents: 407
diff changeset
3117 while (n > 0)
2230
290ee42cae85 Remove old and unused method to allocate memory for undo.
Bram Moolenaar <bram@vim.org>
parents: 2229
diff changeset
3118 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
3119 vim_free((char_u *)uep->ue_array);
1415
20b52d44daaf updated for version 7.1-130
vimboss
parents: 1210
diff changeset
3120 #ifdef U_DEBUG
20b52d44daaf updated for version 7.1-130
vimboss
parents: 1210
diff changeset
3121 uep->ue_magic = 0;
20b52d44daaf updated for version 7.1-130
vimboss
parents: 1210
diff changeset
3122 #endif
2230
290ee42cae85 Remove old and unused method to allocate memory for undo.
Bram Moolenaar <bram@vim.org>
parents: 2229
diff changeset
3123 vim_free((char_u *)uep);
7
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 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3127 * invalidate the undo buffer; called when storage has already been released
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3128 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3129 void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3130 u_clearall(buf)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3131 buf_T *buf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3132 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3133 buf->b_u_newhead = buf->b_u_oldhead = buf->b_u_curhead = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3134 buf->b_u_synced = TRUE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3135 buf->b_u_numhead = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3136 buf->b_u_line_ptr = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3137 buf->b_u_line_lnum = 0;
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 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3141 * save the line "lnum" for the "U" command
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3142 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3143 void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3144 u_saveline(lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3145 linenr_T lnum;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3146 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3147 if (lnum == curbuf->b_u_line_lnum) /* line is already saved */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3148 return;
356
0f2b5d1b8117 updated for version 7.0092
vimboss
parents: 344
diff changeset
3149 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) /* should never happen */
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3150 return;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3151 u_clearline();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3152 curbuf->b_u_line_lnum = lnum;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3153 if (curwin->w_cursor.lnum == lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3154 curbuf->b_u_line_colnr = curwin->w_cursor.col;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3155 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3156 curbuf->b_u_line_colnr = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3157 if ((curbuf->b_u_line_ptr = u_save_line(lnum)) == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3158 do_outofmem_msg((long_u)0);
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 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3162 * clear the line saved for the "U" command
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3163 * (this is used externally for crossing a line while in insert mode)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3164 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3165 void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3166 u_clearline()
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3167 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3168 if (curbuf->b_u_line_ptr != NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3169 {
2230
290ee42cae85 Remove old and unused method to allocate memory for undo.
Bram Moolenaar <bram@vim.org>
parents: 2229
diff changeset
3170 vim_free(curbuf->b_u_line_ptr);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3171 curbuf->b_u_line_ptr = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3172 curbuf->b_u_line_lnum = 0;
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 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3177 * Implementation of the "U" command.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3178 * Differentiation from vi: "U" can be undone with the next "U".
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3179 * 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
3180 * Careful: may trigger autocommands that reload the buffer.
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3181 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3182 void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3183 u_undoline()
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3184 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3185 colnr_T t;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3186 char_u *oldp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3187
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3188 if (undo_off)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3189 return;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3190
1534
bdfbf8ef447a updated for version 7.1-249
vimboss
parents: 1440
diff changeset
3191 if (curbuf->b_u_line_ptr == NULL
bdfbf8ef447a updated for version 7.1-249
vimboss
parents: 1440
diff changeset
3192 || curbuf->b_u_line_lnum > curbuf->b_ml.ml_line_count)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3193 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3194 beep_flush();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3195 return;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3196 }
1534
bdfbf8ef447a updated for version 7.1-249
vimboss
parents: 1440
diff changeset
3197
bdfbf8ef447a updated for version 7.1-249
vimboss
parents: 1440
diff changeset
3198 /* first save the line for the 'u' command */
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3199 if (u_savecommon(curbuf->b_u_line_lnum - 1,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3200 curbuf->b_u_line_lnum + 1, (linenr_T)0) == FAIL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3201 return;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3202 oldp = u_save_line(curbuf->b_u_line_lnum);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3203 if (oldp == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3204 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3205 do_outofmem_msg((long_u)0);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3206 return;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3207 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3208 ml_replace(curbuf->b_u_line_lnum, curbuf->b_u_line_ptr, TRUE);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3209 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
3210 vim_free(curbuf->b_u_line_ptr);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3211 curbuf->b_u_line_ptr = oldp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3212
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3213 t = curbuf->b_u_line_colnr;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3214 if (curwin->w_cursor.lnum == curbuf->b_u_line_lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3215 curbuf->b_u_line_colnr = curwin->w_cursor.col;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3216 curwin->w_cursor.col = t;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3217 curwin->w_cursor.lnum = curbuf->b_u_line_lnum;
1534
bdfbf8ef447a updated for version 7.1-249
vimboss
parents: 1440
diff changeset
3218 check_cursor_col();
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3219 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3220
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3221 /*
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
3222 * Free all allocated memory blocks for the buffer 'buf'.
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
3223 */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
3224 void
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
3225 u_blockfree(buf)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
3226 buf_T *buf;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
3227 {
753
ac005a544e24 updated for version 7.0223
vimboss
parents: 697
diff changeset
3228 while (buf->b_u_oldhead != NULL)
758
d591d4ceeaee updated for version 7.0224
vimboss
parents: 753
diff changeset
3229 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
3230 vim_free(buf->b_u_line_ptr);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3231 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3232
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3233 /*
2230
290ee42cae85 Remove old and unused method to allocate memory for undo.
Bram Moolenaar <bram@vim.org>
parents: 2229
diff changeset
3234 * 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
3235 * Returns NULL when out of memory.
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3236 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3237 static char_u *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3238 u_save_line(lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3239 linenr_T lnum;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3240 {
2230
290ee42cae85 Remove old and unused method to allocate memory for undo.
Bram Moolenaar <bram@vim.org>
parents: 2229
diff changeset
3241 return vim_strsave(ml_get(lnum));
7
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 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3245 * Check if the 'modified' flag is set, or 'ff' has changed (only need to
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3246 * check the first character, because it can only be "dos", "unix" or "mac").
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3247 * "nofile" and "scratch" type buffers are considered to always be unchanged.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3248 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3249 int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3250 bufIsChanged(buf)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3251 buf_T *buf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3252 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3253 return
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3254 #ifdef FEAT_QUICKFIX
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3255 !bt_dontwrite(buf) &&
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3256 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3257 (buf->b_changed || file_ff_differs(buf));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3258 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3259
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3260 int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3261 curbufIsChanged()
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3262 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3263 return
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3264 #ifdef FEAT_QUICKFIX
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3265 !bt_dontwrite(curbuf) &&
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3266 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3267 (curbuf->b_changed || file_ff_differs(curbuf));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3268 }
2280
941ff1cd317a Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents: 2271
diff changeset
3269
941ff1cd317a Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents: 2271
diff changeset
3270 #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
3271 /*
941ff1cd317a Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents: 2271
diff changeset
3272 * 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
3273 * Recursive.
941ff1cd317a Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents: 2271
diff changeset
3274 */
941ff1cd317a Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents: 2271
diff changeset
3275 void
941ff1cd317a Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents: 2271
diff changeset
3276 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
3277 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
3278 list_T *list;
941ff1cd317a Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents: 2271
diff changeset
3279 {
941ff1cd317a Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents: 2271
diff changeset
3280 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
3281 dict_T *dict;
941ff1cd317a Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents: 2271
diff changeset
3282
941ff1cd317a Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents: 2271
diff changeset
3283 while (uhp != NULL)
941ff1cd317a Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents: 2271
diff changeset
3284 {
941ff1cd317a Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents: 2271
diff changeset
3285 dict = dict_alloc();
941ff1cd317a Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents: 2271
diff changeset
3286 if (dict == NULL)
941ff1cd317a Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents: 2271
diff changeset
3287 return;
941ff1cd317a Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents: 2271
diff changeset
3288 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
3289 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
3290 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
3291 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
3292 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
3293 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
3294 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
3295 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
3296
941ff1cd317a Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents: 2271
diff changeset
3297 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
3298 {
941ff1cd317a Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents: 2271
diff changeset
3299 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
3300
941ff1cd317a Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents: 2271
diff changeset
3301 if (alt_list != NULL)
941ff1cd317a Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents: 2271
diff changeset
3302 {
941ff1cd317a Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents: 2271
diff changeset
3303 /* 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
3304 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
3305 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
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
941ff1cd317a Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents: 2271
diff changeset
3309 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
3310 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
3311 }
941ff1cd317a Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents: 2271
diff changeset
3312 }
941ff1cd317a Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents: 2271
diff changeset
3313 #endif