annotate src/undo.c @ 2664:033e7b49356c v7.3.083

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