annotate src/undo.c @ 2217:120502692d82 vim73

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