annotate src/undo.c @ 32669:448aef880252

normalize line endings
author Christian Brabandt <cb@256bit.org>
date Mon, 26 Jun 2023 09:54:34 +0200
parents bb5458706799
children 695b50472e85
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
32669
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1 /* vi:set ts=8 sts=4 sw=4 noet:
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2 *
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3 * VIM - Vi IMproved by Bram Moolenaar
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
4 *
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
5 * Do ":help uganda" in Vim to read copying and usage conditions.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
6 * Do ":help credits" in Vim to see a list of people who contributed.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
7 * See README.txt for an overview of the Vim source code.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
8 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
9
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
10 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
11 * undo.c: multi level undo facility
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
12 *
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
13 * The saved lines are stored in a list of lists (one for each buffer):
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
14 *
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
15 * b_u_oldhead------------------------------------------------+
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
16 * |
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
17 * V
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
18 * +--------------+ +--------------+ +--------------+
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
19 * b_u_newhead--->| u_header | | u_header | | u_header |
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
20 * | uh_next------>| uh_next------>| uh_next---->NULL
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
21 * NULL<--------uh_prev |<---------uh_prev |<---------uh_prev |
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
22 * | uh_entry | | uh_entry | | uh_entry |
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
23 * +--------|-----+ +--------|-----+ +--------|-----+
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
24 * | | |
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
25 * V V V
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
26 * +--------------+ +--------------+ +--------------+
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
27 * | u_entry | | u_entry | | u_entry |
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
28 * | ue_next | | ue_next | | ue_next |
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
29 * +--------|-----+ +--------|-----+ +--------|-----+
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
30 * | | |
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
31 * V V V
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
32 * +--------------+ NULL NULL
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
33 * | u_entry |
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
34 * | ue_next |
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
35 * +--------|-----+
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
36 * |
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
37 * V
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
38 * etc.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
39 *
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
40 * Each u_entry list contains the information for one undo or redo.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
41 * curbuf->b_u_curhead points to the header of the last undo (the next redo),
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
42 * or is NULL if nothing has been undone (end of the branch).
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
43 *
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
44 * For keeping alternate undo/redo branches the uh_alt field is used. Thus at
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
45 * each point in the list a branch may appear for an alternate to redo. The
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
46 * uh_seq field is numbered sequentially to be able to find a newer or older
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
47 * branch.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
48 *
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
49 * +---------------+ +---------------+
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
50 * b_u_oldhead --->| u_header | | u_header |
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
51 * | uh_alt_next ---->| uh_alt_next ----> NULL
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
52 * NULL <----- uh_alt_prev |<------ uh_alt_prev |
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
53 * | uh_prev | | uh_prev |
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
54 * +-----|---------+ +-----|---------+
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
55 * | |
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
56 * V V
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
57 * +---------------+ +---------------+
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
58 * | u_header | | u_header |
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
59 * | uh_alt_next | | uh_alt_next |
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
60 * b_u_newhead --->| uh_alt_prev | | uh_alt_prev |
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
61 * | uh_prev | | uh_prev |
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
62 * +-----|---------+ +-----|---------+
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
63 * | |
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
64 * V V
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
65 * NULL +---------------+ +---------------+
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
66 * | u_header | | u_header |
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
67 * | uh_alt_next ---->| uh_alt_next |
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
68 * | uh_alt_prev |<------ uh_alt_prev |
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
69 * | uh_prev | | uh_prev |
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
70 * +-----|---------+ +-----|---------+
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
71 * | |
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
72 * etc. etc.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
73 *
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
74 *
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
75 * All data is allocated and will all be freed when the buffer is unloaded.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
76 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
77
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
78 // Uncomment the next line for including the u_check() function. This warns
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
79 // for errors in the debug information.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
80 // #define U_DEBUG 1
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
81 #define UH_MAGIC 0x18dade // value for uh_magic when in use
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
82 #define UE_MAGIC 0xabc123 // value for ue_magic when in use
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
83
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
84 // Size of buffer used for encryption.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
85 #define CRYPT_BUF_SIZE 8192
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
86
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
87 #include "vim.h"
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
88
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
89 // Structure passed around between functions.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
90 // Avoids passing cryptstate_T when encryption not available.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
91 typedef struct {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
92 buf_T *bi_buf;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
93 FILE *bi_fp;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
94 #ifdef FEAT_CRYPT
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
95 cryptstate_T *bi_state;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
96 char_u *bi_buffer; // CRYPT_BUF_SIZE, NULL when not buffering
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
97 size_t bi_used; // bytes written to/read from bi_buffer
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
98 size_t bi_avail; // bytes available in bi_buffer
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
99 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
100 } bufinfo_T;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
101
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
102
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
103 static void u_unch_branch(u_header_T *uhp);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
104 static u_entry_T *u_get_headentry(void);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
105 static void u_getbot(void);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
106 static void u_doit(int count);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
107 static void u_undoredo(int undo);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
108 static void u_undo_end(int did_undo, int absolute);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
109 static void u_freeheader(buf_T *buf, u_header_T *uhp, u_header_T **uhpp);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
110 static void u_freebranch(buf_T *buf, u_header_T *uhp, u_header_T **uhpp);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
111 static void u_freeentries(buf_T *buf, u_header_T *uhp, u_header_T **uhpp);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
112 static void u_freeentry(u_entry_T *, long);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
113 #ifdef FEAT_PERSISTENT_UNDO
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
114 # ifdef FEAT_CRYPT
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
115 static int undo_flush(bufinfo_T *bi);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
116 # endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
117 static int undo_read(bufinfo_T *bi, char_u *buffer, size_t size);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
118 static int serialize_uep(bufinfo_T *bi, u_entry_T *uep);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
119 static u_entry_T *unserialize_uep(bufinfo_T *bi, int *error, char_u *file_name);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
120 static void serialize_pos(bufinfo_T *bi, pos_T pos);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
121 static void unserialize_pos(bufinfo_T *bi, pos_T *pos);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
122 static void serialize_visualinfo(bufinfo_T *bi, visualinfo_T *info);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
123 static void unserialize_visualinfo(bufinfo_T *bi, visualinfo_T *info);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
124 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
125 static void u_saveline(linenr_T lnum);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
126
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
127 #define U_ALLOC_LINE(size) lalloc(size, FALSE)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
128
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
129 // used in undo_end() to report number of added and deleted lines
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
130 static long u_newcount, u_oldcount;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
131
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
132 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
133 * When 'u' flag included in 'cpoptions', we behave like vi. Need to remember
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
134 * the action that "u" should do.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
135 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
136 static int undo_undoes = FALSE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
137
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
138 static int lastmark = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
139
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
140 #if defined(U_DEBUG) || defined(PROTO)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
141 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
142 * Check the undo structures for being valid. Print a warning when something
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
143 * looks wrong.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
144 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
145 static int seen_b_u_curhead;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
146 static int seen_b_u_newhead;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
147 static int header_count;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
148
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
149 static void
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
150 u_check_tree(u_header_T *uhp,
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
151 u_header_T *exp_uh_next,
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
152 u_header_T *exp_uh_alt_prev)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
153 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
154 u_entry_T *uep;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
155
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
156 if (uhp == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
157 return;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
158 ++header_count;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
159 if (uhp == curbuf->b_u_curhead && ++seen_b_u_curhead > 1)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
160 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
161 emsg("b_u_curhead found twice (looping?)");
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
162 return;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
163 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
164 if (uhp == curbuf->b_u_newhead && ++seen_b_u_newhead > 1)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
165 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
166 emsg("b_u_newhead found twice (looping?)");
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
167 return;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
168 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
169
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
170 if (uhp->uh_magic != UH_MAGIC)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
171 emsg("uh_magic wrong (may be using freed memory)");
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
172 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
173 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
174 // Check pointers back are correct.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
175 if (uhp->uh_next.ptr != exp_uh_next)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
176 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
177 emsg("uh_next wrong");
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
178 smsg("expected: 0x%x, actual: 0x%x",
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
179 exp_uh_next, uhp->uh_next.ptr);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
180 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
181 if (uhp->uh_alt_prev.ptr != exp_uh_alt_prev)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
182 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
183 emsg("uh_alt_prev wrong");
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
184 smsg("expected: 0x%x, actual: 0x%x",
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
185 exp_uh_alt_prev, uhp->uh_alt_prev.ptr);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
186 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
187
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
188 // Check the undo tree at this header.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
189 for (uep = uhp->uh_entry; uep != NULL; uep = uep->ue_next)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
190 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
191 if (uep->ue_magic != UE_MAGIC)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
192 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
193 emsg("ue_magic wrong (may be using freed memory)");
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
194 break;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
195 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
196 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
197
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
198 // Check the next alt tree.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
199 u_check_tree(uhp->uh_alt_next.ptr, uhp->uh_next.ptr, uhp);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
200
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
201 // Check the next header in this branch.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
202 u_check_tree(uhp->uh_prev.ptr, uhp, NULL);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
203 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
204 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
205
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
206 static void
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
207 u_check(int newhead_may_be_NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
208 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
209 seen_b_u_newhead = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
210 seen_b_u_curhead = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
211 header_count = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
212
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
213 u_check_tree(curbuf->b_u_oldhead, NULL, NULL);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
214
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
215 if (seen_b_u_newhead == 0 && curbuf->b_u_oldhead != NULL
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
216 && !(newhead_may_be_NULL && curbuf->b_u_newhead == NULL))
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
217 semsg("b_u_newhead invalid: 0x%x", curbuf->b_u_newhead);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
218 if (curbuf->b_u_curhead != NULL && seen_b_u_curhead == 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
219 semsg("b_u_curhead invalid: 0x%x", curbuf->b_u_curhead);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
220 if (header_count != curbuf->b_u_numhead)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
221 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
222 emsg("b_u_numhead invalid");
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
223 smsg("expected: %ld, actual: %ld",
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
224 (long)header_count, (long)curbuf->b_u_numhead);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
225 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
226 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
227 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
228
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
229 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
230 * Save the current line for both the "u" and "U" command.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
231 * Careful: may trigger autocommands that reload the buffer.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
232 * Returns OK or FAIL.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
233 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
234 int
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
235 u_save_cursor(void)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
236 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
237 return (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
238 (linenr_T)(curwin->w_cursor.lnum + 1)));
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
239 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
240
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
241 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
242 * Save the lines between "top" and "bot" for both the "u" and "U" command.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
243 * "top" may be 0 and "bot" may be curbuf->b_ml.ml_line_count + 1.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
244 * Careful: may trigger autocommands that reload the buffer.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
245 * Returns FAIL when lines could not be saved, OK otherwise.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
246 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
247 int
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
248 u_save(linenr_T top, linenr_T bot)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
249 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
250 if (undo_off)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
251 return OK;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
252
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
253 if (top >= bot || bot > curbuf->b_ml.ml_line_count + 1)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
254 return FAIL; // rely on caller to give an error message
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
255
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
256 if (top + 2 == bot)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
257 u_saveline((linenr_T)(top + 1));
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
258
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
259 return (u_savecommon(top, bot, (linenr_T)0, FALSE));
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
260 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
261
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
262 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
263 * Save the line "lnum" (used by ":s" and "~" command).
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
264 * The line is replaced, so the new bottom line is lnum + 1.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
265 * Careful: may trigger autocommands that reload the buffer.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
266 * Returns FAIL when lines could not be saved, OK otherwise.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
267 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
268 int
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
269 u_savesub(linenr_T lnum)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
270 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
271 if (undo_off)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
272 return OK;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
273
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
274 return (u_savecommon(lnum - 1, lnum + 1, lnum + 1, FALSE));
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
275 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
276
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
277 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
278 * A new line is inserted before line "lnum" (used by :s command).
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
279 * The line is inserted, so the new bottom line is lnum + 1.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
280 * Careful: may trigger autocommands that reload the buffer.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
281 * Returns FAIL when lines could not be saved, OK otherwise.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
282 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
283 int
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
284 u_inssub(linenr_T lnum)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
285 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
286 if (undo_off)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
287 return OK;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
288
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
289 return (u_savecommon(lnum - 1, lnum, lnum + 1, FALSE));
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
290 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
291
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
292 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
293 * Save the lines "lnum" - "lnum" + nlines (used by delete command).
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
294 * The lines are deleted, so the new bottom line is lnum, unless the buffer
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
295 * becomes empty.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
296 * Careful: may trigger autocommands that reload the buffer.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
297 * Returns FAIL when lines could not be saved, OK otherwise.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
298 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
299 int
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
300 u_savedel(linenr_T lnum, long nlines)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
301 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
302 if (undo_off)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
303 return OK;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
304
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
305 return (u_savecommon(lnum - 1, lnum + nlines,
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
306 nlines == curbuf->b_ml.ml_line_count ? 2 : lnum, FALSE));
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
307 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
308
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
309 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
310 * Return TRUE when undo is allowed. Otherwise give an error message and
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
311 * return FALSE.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
312 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
313 int
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
314 undo_allowed(void)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
315 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
316 // Don't allow changes when 'modifiable' is off.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
317 if (!curbuf->b_p_ma)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
318 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
319 emsg(_(e_cannot_make_changes_modifiable_is_off));
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
320 return FALSE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
321 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
322
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
323 #ifdef HAVE_SANDBOX
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
324 // In the sandbox it's not allowed to change the text.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
325 if (sandbox != 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
326 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
327 emsg(_(e_not_allowed_in_sandbox));
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
328 return FALSE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
329 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
330 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
331
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
332 // Don't allow changes in the buffer while editing the cmdline. The
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
333 // caller of getcmdline() may get confused.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
334 if (textlock != 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
335 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
336 emsg(_(e_not_allowed_to_change_text_or_change_window));
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
337 return FALSE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
338 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
339
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
340 return TRUE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
341 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
342
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
343 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
344 * Get the undolevel value for the current buffer.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
345 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
346 static long
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
347 get_undolevel(void)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
348 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
349 if (curbuf->b_p_ul == NO_LOCAL_UNDOLEVEL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
350 return p_ul;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
351 return curbuf->b_p_ul;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
352 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
353
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
354 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
355 * u_save_line(): save an allocated copy of line "lnum" into "ul".
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
356 * Returns FAIL when out of memory.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
357 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
358 static int
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
359 u_save_line(undoline_T *ul, linenr_T lnum)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
360 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
361 char_u *line = ml_get(lnum);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
362
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
363 if (curbuf->b_ml.ml_line_len == 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
364 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
365 ul->ul_len = 1;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
366 ul->ul_line = vim_strsave((char_u *)"");
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
367 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
368 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
369 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
370 // This uses the length in the memline, thus text properties are
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
371 // included.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
372 ul->ul_len = curbuf->b_ml.ml_line_len;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
373 ul->ul_line = vim_memsave(line, ul->ul_len);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
374 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
375 return ul->ul_line == NULL ? FAIL : OK;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
376 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
377
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
378 #ifdef FEAT_PROP_POPUP
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
379 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
380 * return TRUE if line "lnum" has text property "flags".
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
381 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
382 static int
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
383 has_prop_w_flags(linenr_T lnum, int flags)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
384 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
385 char_u *props;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
386 int i;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
387 int proplen = get_text_props(curbuf, lnum, &props, FALSE);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
388
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
389 for (i = 0; i < proplen; ++i)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
390 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
391 textprop_T prop;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
392
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
393 mch_memmove(&prop, props + i * sizeof prop, sizeof prop);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
394 if (prop.tp_flags & flags)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
395 return TRUE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
396 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
397 return FALSE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
398 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
399 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
400
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
401 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
402 * Common code for various ways to save text before a change.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
403 * "top" is the line above the first changed line.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
404 * "bot" is the line below the last changed line.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
405 * "newbot" is the new bottom line. Use zero when not known.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
406 * "reload" is TRUE when saving for a buffer reload.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
407 * Careful: may trigger autocommands that reload the buffer.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
408 * Returns FAIL when lines could not be saved, OK otherwise.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
409 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
410 int
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
411 u_savecommon(
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
412 linenr_T top,
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
413 linenr_T bot,
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
414 linenr_T newbot,
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
415 int reload)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
416 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
417 linenr_T lnum;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
418 long i;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
419 u_header_T *uhp;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
420 u_header_T *old_curhead;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
421 u_entry_T *uep;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
422 u_entry_T *prev_uep;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
423 long size;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
424
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
425 if (!reload)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
426 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
427 // When making changes is not allowed return FAIL. It's a crude way
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
428 // to make all change commands fail.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
429 if (!undo_allowed())
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
430 return FAIL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
431
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
432 #ifdef FEAT_NETBEANS_INTG
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
433 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
434 * Netbeans defines areas that cannot be modified. Bail out here when
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
435 * trying to change text in a guarded area.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
436 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
437 if (netbeans_active())
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
438 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
439 if (netbeans_is_guarded(top, bot))
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
440 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
441 emsg(_(e_region_is_guarded_cannot_modify));
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
442 return FAIL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
443 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
444 if (curbuf->b_p_ro)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
445 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
446 emsg(_(e_netbeans_does_not_allow_changes_in_read_only_files));
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
447 return FAIL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
448 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
449 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
450 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
451 #ifdef FEAT_TERMINAL
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
452 // A change in a terminal buffer removes the highlighting.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
453 term_change_in_curbuf();
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
454 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
455
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
456 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
457 * Saving text for undo means we are going to make a change. Give a
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
458 * warning for a read-only file before making the change, so that the
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
459 * FileChangedRO event can replace the buffer with a read-write version
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
460 * (e.g., obtained from a source control system).
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
461 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
462 change_warning(0);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
463 if (bot > curbuf->b_ml.ml_line_count + 1)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
464 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
465 // This happens when the FileChangedRO autocommand changes the
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
466 // file in a way it becomes shorter.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
467 emsg(_(e_line_count_changed_unexpectedly));
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
468 return FAIL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
469 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
470 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
471
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
472 #ifdef U_DEBUG
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
473 u_check(FALSE);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
474 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
475
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
476 #ifdef FEAT_PROP_POPUP
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
477 // Include the line above if a text property continues from it.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
478 // Include the line below if a text property continues to it.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
479 if (bot - top > 1)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
480 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
481 if (top > 0 && has_prop_w_flags(top + 1, TP_FLAG_CONT_PREV))
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
482 --top;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
483 if (bot <= curbuf->b_ml.ml_line_count
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
484 && has_prop_w_flags(bot - 1, TP_FLAG_CONT_NEXT))
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
485 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
486 ++bot;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
487 if (newbot != 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
488 ++newbot;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
489 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
490 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
491 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
492
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
493 size = bot - top - 1;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
494
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
495 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
496 * If curbuf->b_u_synced == TRUE make a new header.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
497 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
498 if (curbuf->b_u_synced)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
499 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
500 // Need to create new entry in b_changelist.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
501 curbuf->b_new_change = TRUE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
502
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
503 if (get_undolevel() >= 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
504 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
505 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
506 * Make a new header entry. Do this first so that we don't mess
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
507 * up the undo info when out of memory.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
508 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
509 uhp = U_ALLOC_LINE(sizeof(u_header_T));
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
510 if (uhp == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
511 goto nomem;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
512 #ifdef U_DEBUG
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
513 uhp->uh_magic = UH_MAGIC;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
514 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
515 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
516 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
517 uhp = NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
518
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
519 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
520 * If we undid more than we redid, move the entry lists before and
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
521 * including curbuf->b_u_curhead to an alternate branch.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
522 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
523 old_curhead = curbuf->b_u_curhead;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
524 if (old_curhead != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
525 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
526 curbuf->b_u_newhead = old_curhead->uh_next.ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
527 curbuf->b_u_curhead = NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
528 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
529
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
530 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
531 * free headers to keep the size right
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
532 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
533 while (curbuf->b_u_numhead > get_undolevel()
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
534 && curbuf->b_u_oldhead != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
535 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
536 u_header_T *uhfree = curbuf->b_u_oldhead;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
537
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
538 if (uhfree == old_curhead)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
539 // Can't reconnect the branch, delete all of it.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
540 u_freebranch(curbuf, uhfree, &old_curhead);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
541 else if (uhfree->uh_alt_next.ptr == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
542 // There is no branch, only free one header.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
543 u_freeheader(curbuf, uhfree, &old_curhead);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
544 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
545 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
546 // Free the oldest alternate branch as a whole.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
547 while (uhfree->uh_alt_next.ptr != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
548 uhfree = uhfree->uh_alt_next.ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
549 u_freebranch(curbuf, uhfree, &old_curhead);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
550 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
551 #ifdef U_DEBUG
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
552 u_check(TRUE);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
553 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
554 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
555
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
556 if (uhp == NULL) // no undo at all
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
557 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
558 if (old_curhead != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
559 u_freebranch(curbuf, old_curhead, NULL);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
560 curbuf->b_u_synced = FALSE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
561 return OK;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
562 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
563
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
564 uhp->uh_prev.ptr = NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
565 uhp->uh_next.ptr = curbuf->b_u_newhead;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
566 uhp->uh_alt_next.ptr = old_curhead;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
567 if (old_curhead != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
568 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
569 uhp->uh_alt_prev.ptr = old_curhead->uh_alt_prev.ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
570 if (uhp->uh_alt_prev.ptr != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
571 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = uhp;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
572 old_curhead->uh_alt_prev.ptr = uhp;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
573 if (curbuf->b_u_oldhead == old_curhead)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
574 curbuf->b_u_oldhead = uhp;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
575 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
576 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
577 uhp->uh_alt_prev.ptr = NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
578 if (curbuf->b_u_newhead != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
579 curbuf->b_u_newhead->uh_prev.ptr = uhp;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
580
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
581 uhp->uh_seq = ++curbuf->b_u_seq_last;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
582 curbuf->b_u_seq_cur = uhp->uh_seq;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
583 uhp->uh_time = vim_time();
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
584 uhp->uh_save_nr = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
585 curbuf->b_u_time_cur = uhp->uh_time + 1;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
586
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
587 uhp->uh_walk = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
588 uhp->uh_entry = NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
589 uhp->uh_getbot_entry = NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
590 uhp->uh_cursor = curwin->w_cursor; // save cursor pos. for undo
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
591 if (virtual_active() && curwin->w_cursor.coladd > 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
592 uhp->uh_cursor_vcol = getviscol();
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
593 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
594 uhp->uh_cursor_vcol = -1;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
595
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
596 // save changed and buffer empty flag for undo
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
597 uhp->uh_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
598 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
599
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
600 // save named marks and Visual marks for undo
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
601 mch_memmove(uhp->uh_namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
602 uhp->uh_visual = curbuf->b_visual;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
603
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
604 curbuf->b_u_newhead = uhp;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
605 if (curbuf->b_u_oldhead == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
606 curbuf->b_u_oldhead = uhp;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
607 ++curbuf->b_u_numhead;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
608 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
609 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
610 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
611 if (get_undolevel() < 0) // no undo at all
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
612 return OK;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
613
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
614 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
615 * When saving a single line, and it has been saved just before, it
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
616 * doesn't make sense saving it again. Saves a lot of memory when
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
617 * making lots of changes inside the same line.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
618 * This is only possible if the previous change didn't increase or
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
619 * decrease the number of lines.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
620 * Check the ten last changes. More doesn't make sense and takes too
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
621 * long.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
622 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
623 if (size == 1)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
624 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
625 uep = u_get_headentry();
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
626 prev_uep = NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
627 for (i = 0; i < 10; ++i)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
628 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
629 if (uep == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
630 break;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
631
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
632 // If lines have been inserted/deleted we give up.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
633 // Also when the line was included in a multi-line save.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
634 if ((curbuf->b_u_newhead->uh_getbot_entry != uep
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
635 ? (uep->ue_top + uep->ue_size + 1
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
636 != (uep->ue_bot == 0
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
637 ? curbuf->b_ml.ml_line_count + 1
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
638 : uep->ue_bot))
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
639 : uep->ue_lcount != curbuf->b_ml.ml_line_count)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
640 || (uep->ue_size > 1
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
641 && top >= uep->ue_top
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
642 && top + 2 <= uep->ue_top + uep->ue_size + 1))
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
643 break;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
644
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
645 // If it's the same line we can skip saving it again.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
646 if (uep->ue_size == 1 && uep->ue_top == top)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
647 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
648 if (i > 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
649 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
650 // It's not the last entry: get ue_bot for the last
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
651 // entry now. Following deleted/inserted lines go to
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
652 // the re-used entry.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
653 u_getbot();
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
654 curbuf->b_u_synced = FALSE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
655
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
656 // Move the found entry to become the last entry. The
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
657 // order of undo/redo doesn't matter for the entries
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
658 // we move it over, since they don't change the line
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
659 // count and don't include this line. It does matter
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
660 // for the found entry if the line count is changed by
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
661 // the executed command.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
662 prev_uep->ue_next = uep->ue_next;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
663 uep->ue_next = curbuf->b_u_newhead->uh_entry;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
664 curbuf->b_u_newhead->uh_entry = uep;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
665 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
666
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
667 // The executed command may change the line count.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
668 if (newbot != 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
669 uep->ue_bot = newbot;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
670 else if (bot > curbuf->b_ml.ml_line_count)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
671 uep->ue_bot = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
672 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
673 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
674 uep->ue_lcount = curbuf->b_ml.ml_line_count;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
675 curbuf->b_u_newhead->uh_getbot_entry = uep;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
676 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
677 return OK;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
678 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
679 prev_uep = uep;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
680 uep = uep->ue_next;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
681 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
682 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
683
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
684 // find line number for ue_bot for previous u_save()
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
685 u_getbot();
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
686 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
687
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
688 #if !defined(UNIX) && !defined(MSWIN)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
689 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
690 * With Amiga we can't handle big undo's, because
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
691 * then u_alloc_line would have to allocate a block larger than 32K
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
692 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
693 if (size >= 8000)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
694 goto nomem;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
695 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
696
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
697 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
698 * add lines in front of entry list
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
699 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
700 uep = U_ALLOC_LINE(sizeof(u_entry_T));
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
701 if (uep == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
702 goto nomem;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
703 CLEAR_POINTER(uep);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
704 #ifdef U_DEBUG
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
705 uep->ue_magic = UE_MAGIC;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
706 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
707
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
708 uep->ue_size = size;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
709 uep->ue_top = top;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
710 if (newbot != 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
711 uep->ue_bot = newbot;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
712 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
713 * Use 0 for ue_bot if bot is below last line.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
714 * Otherwise we have to compute ue_bot later.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
715 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
716 else if (bot > curbuf->b_ml.ml_line_count)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
717 uep->ue_bot = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
718 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
719 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
720 uep->ue_lcount = curbuf->b_ml.ml_line_count;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
721 curbuf->b_u_newhead->uh_getbot_entry = uep;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
722 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
723
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
724 if (size > 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
725 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
726 if ((uep->ue_array = U_ALLOC_LINE(sizeof(undoline_T) * size)) == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
727 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
728 u_freeentry(uep, 0L);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
729 goto nomem;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
730 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
731 for (i = 0, lnum = top + 1; i < size; ++i)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
732 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
733 fast_breakcheck();
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
734 if (got_int)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
735 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
736 u_freeentry(uep, i);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
737 return FAIL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
738 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
739 if (u_save_line(&uep->ue_array[i], lnum++) == FAIL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
740 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
741 u_freeentry(uep, i);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
742 goto nomem;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
743 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
744 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
745 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
746 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
747 uep->ue_array = NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
748 uep->ue_next = curbuf->b_u_newhead->uh_entry;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
749 curbuf->b_u_newhead->uh_entry = uep;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
750 curbuf->b_u_synced = FALSE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
751 undo_undoes = FALSE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
752
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
753 #ifdef U_DEBUG
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
754 u_check(FALSE);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
755 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
756 return OK;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
757
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
758 nomem:
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
759 msg_silent = 0; // must display the prompt
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
760 if (ask_yesno((char_u *)_("No undo possible; continue anyway"), TRUE)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
761 == 'y')
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
762 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
763 undo_off = TRUE; // will be reset when character typed
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
764 return OK;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
765 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
766 do_outofmem_msg((long_u)0);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
767 return FAIL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
768 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
769
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
770 #if defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
771
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
772 # define UF_START_MAGIC "Vim\237UnDo\345" // magic at start of undofile
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
773 # define UF_START_MAGIC_LEN 9
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
774 # define UF_HEADER_MAGIC 0x5fd0 // magic at start of header
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
775 # define UF_HEADER_END_MAGIC 0xe7aa // magic after last header
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
776 # define UF_ENTRY_MAGIC 0xf518 // magic at start of entry
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
777 # define UF_ENTRY_END_MAGIC 0x3581 // magic after last entry
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
778 # define UF_VERSION 2 // 2-byte undofile version number
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
779 # define UF_VERSION_CRYPT 0x8002 // idem, encrypted
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
780
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
781 // extra fields for header
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
782 # define UF_LAST_SAVE_NR 1
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
783
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
784 // extra fields for uhp
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
785 # define UHP_SAVE_NR 1
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
786
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
787 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
788 * Compute the hash for the current buffer text into hash[UNDO_HASH_SIZE].
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
789 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
790 void
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
791 u_compute_hash(char_u *hash)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
792 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
793 context_sha256_T ctx;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
794 linenr_T lnum;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
795 char_u *p;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
796
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
797 sha256_start(&ctx);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
798 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
799 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
800 p = ml_get(lnum);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
801 sha256_update(&ctx, p, (UINT32_T)(STRLEN(p) + 1));
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
802 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
803 sha256_finish(&ctx, hash);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
804 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
805
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
806 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
807 * Return an allocated string of the full path of the target undofile.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
808 * When "reading" is TRUE find the file to read, go over all directories in
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
809 * 'undodir'.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
810 * When "reading" is FALSE use the first name where the directory exists.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
811 * Returns NULL when there is no place to write or no file to read.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
812 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
813 static char_u *
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
814 u_get_undo_file_name(char_u *buf_ffname, int reading)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
815 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
816 char_u *dirp;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
817 char_u dir_name[IOSIZE + 1];
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
818 char_u *munged_name = NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
819 char_u *undo_file_name = NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
820 int dir_len;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
821 char_u *p;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
822 stat_T st;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
823 char_u *ffname = buf_ffname;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
824 #ifdef HAVE_READLINK
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
825 char_u fname_buf[MAXPATHL];
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
826 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
827
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
828 if (ffname == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
829 return NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
830
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
831 #ifdef HAVE_READLINK
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
832 // Expand symlink in the file name, so that we put the undo file with the
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
833 // actual file instead of with the symlink.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
834 if (resolve_symlink(ffname, fname_buf) == OK)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
835 ffname = fname_buf;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
836 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
837
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
838 // Loop over 'undodir'. When reading find the first file that exists.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
839 // When not reading use the first directory that exists or ".".
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
840 dirp = p_udir;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
841 while (*dirp != NUL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
842 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
843 dir_len = copy_option_part(&dirp, dir_name, IOSIZE, ",");
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
844 if (dir_len == 1 && dir_name[0] == '.')
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
845 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
846 // Use same directory as the ffname,
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
847 // "dir/name" -> "dir/.name.un~"
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
848 undo_file_name = vim_strnsave(ffname, STRLEN(ffname) + 5);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
849 if (undo_file_name == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
850 break;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
851 p = gettail(undo_file_name);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
852 #ifdef VMS
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
853 // VMS can not handle more than one dot in the filenames
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
854 // use "dir/name" -> "dir/_un_name" - add _un_
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
855 // at the beginning to keep the extension
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
856 mch_memmove(p + 4, p, STRLEN(p) + 1);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
857 mch_memmove(p, "_un_", 4);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
858
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
859 #else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
860 // Use same directory as the ffname,
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
861 // "dir/name" -> "dir/.name.un~"
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
862 mch_memmove(p + 1, p, STRLEN(p) + 1);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
863 *p = '.';
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
864 STRCAT(p, ".un~");
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
865 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
866 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
867 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
868 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
869 dir_name[dir_len] = NUL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
870 if (mch_isdir(dir_name))
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
871 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
872 if (munged_name == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
873 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
874 munged_name = vim_strsave(ffname);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
875 if (munged_name == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
876 return NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
877 for (p = munged_name; *p != NUL; MB_PTR_ADV(p))
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
878 if (vim_ispathsep(*p))
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
879 *p = '%';
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
880 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
881 undo_file_name = concat_fnames(dir_name, munged_name, TRUE);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
882 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
883 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
884
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
885 // When reading check if the file exists.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
886 if (undo_file_name != NULL && (!reading
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
887 || mch_stat((char *)undo_file_name, &st) >= 0))
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
888 break;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
889 VIM_CLEAR(undo_file_name);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
890 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
891
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
892 vim_free(munged_name);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
893 return undo_file_name;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
894 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
895
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
896 static void
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
897 corruption_error(char *mesg, char_u *file_name)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
898 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
899 semsg(_(e_corrupted_undo_file_str_str), mesg, file_name);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
900 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
901
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
902 static void
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
903 u_free_uhp(u_header_T *uhp)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
904 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
905 u_entry_T *nuep;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
906 u_entry_T *uep;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
907
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
908 uep = uhp->uh_entry;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
909 while (uep != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
910 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
911 nuep = uep->ue_next;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
912 u_freeentry(uep, uep->ue_size);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
913 uep = nuep;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
914 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
915 vim_free(uhp);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
916 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
917
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
918 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
919 * Write a sequence of bytes to the undo file.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
920 * Buffers and encrypts as needed.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
921 * Returns OK or FAIL.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
922 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
923 static int
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
924 undo_write(bufinfo_T *bi, char_u *ptr, size_t len)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
925 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
926 #ifdef FEAT_CRYPT
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
927 if (bi->bi_buffer != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
928 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
929 size_t len_todo = len;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
930 char_u *p = ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
931
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
932 while (bi->bi_used + len_todo >= CRYPT_BUF_SIZE)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
933 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
934 size_t n = CRYPT_BUF_SIZE - bi->bi_used;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
935
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
936 mch_memmove(bi->bi_buffer + bi->bi_used, p, n);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
937 len_todo -= n;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
938 p += n;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
939 bi->bi_used = CRYPT_BUF_SIZE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
940 if (undo_flush(bi) == FAIL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
941 return FAIL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
942 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
943 if (len_todo > 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
944 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
945 mch_memmove(bi->bi_buffer + bi->bi_used, p, len_todo);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
946 bi->bi_used += len_todo;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
947 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
948 return OK;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
949 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
950 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
951 if (fwrite(ptr, len, (size_t)1, bi->bi_fp) != 1)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
952 return FAIL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
953 return OK;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
954 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
955
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
956 #ifdef FEAT_CRYPT
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
957 static int
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
958 undo_flush(bufinfo_T *bi)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
959 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
960 if (bi->bi_buffer != NULL && bi->bi_state != NULL && bi->bi_used > 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
961 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
962 // Last parameter is only used for sodium encryption and that
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
963 // explicitly disables encryption of undofiles.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
964 crypt_encode_inplace(bi->bi_state, bi->bi_buffer, bi->bi_used, FALSE);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
965 if (fwrite(bi->bi_buffer, bi->bi_used, (size_t)1, bi->bi_fp) != 1)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
966 return FAIL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
967 bi->bi_used = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
968 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
969 return OK;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
970 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
971 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
972
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
973 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
974 * Write "ptr[len]" and crypt the bytes when needed.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
975 * Returns OK or FAIL.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
976 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
977 static int
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
978 fwrite_crypt(bufinfo_T *bi, char_u *ptr, size_t len)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
979 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
980 #ifdef FEAT_CRYPT
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
981 char_u *copy;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
982 char_u small_buf[100];
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
983 size_t i;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
984
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
985 if (bi->bi_state != NULL && bi->bi_buffer == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
986 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
987 // crypting every piece of text separately
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
988 if (len < 100)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
989 copy = small_buf; // no malloc()/free() for short strings
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
990 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
991 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
992 copy = lalloc(len, FALSE);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
993 if (copy == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
994 return 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
995 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
996 // Last parameter is only used for sodium encryption and that
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
997 // explicitly disables encryption of undofiles.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
998 crypt_encode(bi->bi_state, ptr, len, copy, TRUE);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
999 i = fwrite(copy, len, (size_t)1, bi->bi_fp);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1000 if (copy != small_buf)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1001 vim_free(copy);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1002 return i == 1 ? OK : FAIL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1003 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1004 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1005 return undo_write(bi, ptr, len);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1006 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1007
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1008 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1009 * Write a number, MSB first, in "len" bytes.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1010 * Must match with undo_read_?c() functions.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1011 * Returns OK or FAIL.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1012 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1013 static int
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1014 undo_write_bytes(bufinfo_T *bi, long_u nr, int len)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1015 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1016 char_u buf[8];
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1017 int i;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1018 int bufi = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1019
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1020 for (i = len - 1; i >= 0; --i)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1021 buf[bufi++] = (char_u)(nr >> (i * 8));
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1022 return undo_write(bi, buf, (size_t)len);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1023 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1024
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1025 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1026 * Write the pointer to an undo header. Instead of writing the pointer itself
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1027 * we use the sequence number of the header. This is converted back to
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1028 * pointers when reading. */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1029 static void
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1030 put_header_ptr(bufinfo_T *bi, u_header_T *uhp)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1031 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1032 undo_write_bytes(bi, (long_u)(uhp != NULL ? uhp->uh_seq : 0), 4);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1033 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1034
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1035 static int
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1036 undo_read_4c(bufinfo_T *bi)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1037 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1038 #ifdef FEAT_CRYPT
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1039 if (bi->bi_buffer != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1040 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1041 char_u buf[4];
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1042 int n;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1043
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1044 undo_read(bi, buf, (size_t)4);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1045 n = ((unsigned)buf[0] << 24) + (buf[1] << 16) + (buf[2] << 8) + buf[3];
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1046 return n;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1047 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1048 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1049 return get4c(bi->bi_fp);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1050 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1051
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1052 static int
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1053 undo_read_2c(bufinfo_T *bi)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1054 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1055 #ifdef FEAT_CRYPT
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1056 if (bi->bi_buffer != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1057 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1058 char_u buf[2];
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1059 int n;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1060
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1061 undo_read(bi, buf, (size_t)2);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1062 n = (buf[0] << 8) + buf[1];
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1063 return n;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1064 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1065 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1066 return get2c(bi->bi_fp);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1067 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1068
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1069 static int
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1070 undo_read_byte(bufinfo_T *bi)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1071 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1072 #ifdef FEAT_CRYPT
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1073 if (bi->bi_buffer != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1074 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1075 char_u buf[1];
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1076
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1077 undo_read(bi, buf, (size_t)1);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1078 return buf[0];
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1079 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1080 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1081 return getc(bi->bi_fp);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1082 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1083
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1084 static time_t
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1085 undo_read_time(bufinfo_T *bi)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1086 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1087 #ifdef FEAT_CRYPT
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1088 if (bi->bi_buffer != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1089 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1090 char_u buf[8];
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1091 time_t n = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1092 int i;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1093
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1094 undo_read(bi, buf, (size_t)8);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1095 for (i = 0; i < 8; ++i)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1096 n = (n << 8) + buf[i];
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1097 return n;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1098 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1099 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1100 return get8ctime(bi->bi_fp);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1101 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1102
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1103 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1104 * Read "buffer[size]" from the undo file.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1105 * Return OK or FAIL.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1106 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1107 static int
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1108 undo_read(bufinfo_T *bi, char_u *buffer, size_t size)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1109 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1110 int retval = OK;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1111
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1112 #ifdef FEAT_CRYPT
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1113 if (bi->bi_buffer != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1114 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1115 int size_todo = (int)size;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1116 char_u *p = buffer;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1117
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1118 while (size_todo > 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1119 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1120 size_t n;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1121
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1122 if (bi->bi_used >= bi->bi_avail)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1123 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1124 n = fread(bi->bi_buffer, 1, (size_t)CRYPT_BUF_SIZE, bi->bi_fp);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1125 if (n == 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1126 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1127 retval = FAIL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1128 break;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1129 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1130 bi->bi_avail = n;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1131 bi->bi_used = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1132 crypt_decode_inplace(bi->bi_state, bi->bi_buffer, bi->bi_avail, FALSE);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1133 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1134 n = size_todo;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1135 if (n > bi->bi_avail - bi->bi_used)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1136 n = bi->bi_avail - bi->bi_used;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1137 mch_memmove(p, bi->bi_buffer + bi->bi_used, n);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1138 bi->bi_used += n;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1139 size_todo -= (int)n;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1140 p += n;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1141 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1142 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1143 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1144 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1145 if (fread(buffer, size, 1, bi->bi_fp) != 1)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1146 retval = FAIL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1147
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1148 if (retval == FAIL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1149 // Error may be checked for only later. Fill with zeros,
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1150 // so that the reader won't use garbage.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1151 vim_memset(buffer, 0, size);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1152 return retval;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1153 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1154
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1155 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1156 * Read a string of length "len" from "bi->bi_fd".
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1157 * "len" can be zero to allocate an empty line.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1158 * Decrypt the bytes if needed.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1159 * Append a NUL.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1160 * Returns a pointer to allocated memory or NULL for failure.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1161 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1162 static char_u *
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1163 read_string_decrypt(bufinfo_T *bi, int len)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1164 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1165 char_u *ptr = alloc(len + 1);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1166
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1167 if (ptr == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1168 return NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1169
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1170 if (len > 0 && undo_read(bi, ptr, len) == FAIL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1171 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1172 vim_free(ptr);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1173 return NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1174 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1175 // In case there are text properties there already is a NUL, but
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1176 // checking for that is more expensive than just adding a dummy byte.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1177 ptr[len] = NUL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1178 #ifdef FEAT_CRYPT
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1179 if (bi->bi_state != NULL && bi->bi_buffer == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1180 crypt_decode_inplace(bi->bi_state, ptr, len, FALSE);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1181 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1182 return ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1183 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1184
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1185 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1186 * Writes the (not encrypted) header and initializes encryption if needed.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1187 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1188 static int
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1189 serialize_header(bufinfo_T *bi, char_u *hash)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1190 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1191 long len;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1192 buf_T *buf = bi->bi_buf;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1193 FILE *fp = bi->bi_fp;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1194 char_u time_buf[8];
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1195
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1196 // Start writing, first the magic marker and undo info version.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1197 if (fwrite(UF_START_MAGIC, (size_t)UF_START_MAGIC_LEN, (size_t)1, fp) != 1)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1198 return FAIL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1199
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1200 // If the buffer is encrypted then all text bytes following will be
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1201 // encrypted. Numbers and other info is not crypted.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1202 #ifdef FEAT_CRYPT
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1203 if (*buf->b_p_key != NUL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1204 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1205 char_u *header;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1206 int header_len;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1207
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1208 undo_write_bytes(bi, (long_u)UF_VERSION_CRYPT, 2);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1209 bi->bi_state = crypt_create_for_writing(crypt_get_method_nr(buf),
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1210 buf->b_p_key, &header, &header_len);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1211 if (bi->bi_state == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1212 return FAIL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1213 len = (long)fwrite(header, (size_t)header_len, (size_t)1, fp);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1214 vim_free(header);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1215 if (len != 1)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1216 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1217 crypt_free_state(bi->bi_state);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1218 bi->bi_state = NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1219 return FAIL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1220 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1221
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1222 if (crypt_whole_undofile(crypt_get_method_nr(buf)))
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1223 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1224 bi->bi_buffer = alloc(CRYPT_BUF_SIZE);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1225 if (bi->bi_buffer == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1226 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1227 crypt_free_state(bi->bi_state);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1228 bi->bi_state = NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1229 return FAIL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1230 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1231 bi->bi_used = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1232 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1233 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1234 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1235 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1236 undo_write_bytes(bi, (long_u)UF_VERSION, 2);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1237
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1238
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1239 // Write a hash of the buffer text, so that we can verify it is still the
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1240 // same when reading the buffer text.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1241 if (undo_write(bi, hash, (size_t)UNDO_HASH_SIZE) == FAIL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1242 return FAIL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1243
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1244 // buffer-specific data
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1245 undo_write_bytes(bi, (long_u)buf->b_ml.ml_line_count, 4);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1246 len = buf->b_u_line_ptr.ul_line == NULL
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1247 ? 0L : (long)STRLEN(buf->b_u_line_ptr.ul_line);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1248 undo_write_bytes(bi, (long_u)len, 4);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1249 if (len > 0 && fwrite_crypt(bi, buf->b_u_line_ptr.ul_line, (size_t)len)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1250 == FAIL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1251 return FAIL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1252 undo_write_bytes(bi, (long_u)buf->b_u_line_lnum, 4);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1253 undo_write_bytes(bi, (long_u)buf->b_u_line_colnr, 4);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1254
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1255 // Undo structures header data
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1256 put_header_ptr(bi, buf->b_u_oldhead);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1257 put_header_ptr(bi, buf->b_u_newhead);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1258 put_header_ptr(bi, buf->b_u_curhead);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1259
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1260 undo_write_bytes(bi, (long_u)buf->b_u_numhead, 4);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1261 undo_write_bytes(bi, (long_u)buf->b_u_seq_last, 4);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1262 undo_write_bytes(bi, (long_u)buf->b_u_seq_cur, 4);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1263 time_to_bytes(buf->b_u_time_cur, time_buf);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1264 undo_write(bi, time_buf, 8);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1265
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1266 // Optional fields.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1267 undo_write_bytes(bi, 4, 1);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1268 undo_write_bytes(bi, UF_LAST_SAVE_NR, 1);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1269 undo_write_bytes(bi, (long_u)buf->b_u_save_nr_last, 4);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1270
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1271 undo_write_bytes(bi, 0, 1); // end marker
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1272
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1273 return OK;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1274 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1275
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1276 static int
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1277 serialize_uhp(bufinfo_T *bi, u_header_T *uhp)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1278 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1279 int i;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1280 u_entry_T *uep;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1281 char_u time_buf[8];
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1282
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1283 if (undo_write_bytes(bi, (long_u)UF_HEADER_MAGIC, 2) == FAIL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1284 return FAIL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1285
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1286 put_header_ptr(bi, uhp->uh_next.ptr);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1287 put_header_ptr(bi, uhp->uh_prev.ptr);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1288 put_header_ptr(bi, uhp->uh_alt_next.ptr);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1289 put_header_ptr(bi, uhp->uh_alt_prev.ptr);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1290 undo_write_bytes(bi, uhp->uh_seq, 4);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1291 serialize_pos(bi, uhp->uh_cursor);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1292 undo_write_bytes(bi, (long_u)uhp->uh_cursor_vcol, 4);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1293 undo_write_bytes(bi, (long_u)uhp->uh_flags, 2);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1294 // Assume NMARKS will stay the same.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1295 for (i = 0; i < NMARKS; ++i)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1296 serialize_pos(bi, uhp->uh_namedm[i]);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1297 serialize_visualinfo(bi, &uhp->uh_visual);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1298 time_to_bytes(uhp->uh_time, time_buf);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1299 undo_write(bi, time_buf, 8);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1300
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1301 // Optional fields.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1302 undo_write_bytes(bi, 4, 1);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1303 undo_write_bytes(bi, UHP_SAVE_NR, 1);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1304 undo_write_bytes(bi, (long_u)uhp->uh_save_nr, 4);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1305
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1306 undo_write_bytes(bi, 0, 1); // end marker
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1307
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1308 // Write all the entries.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1309 for (uep = uhp->uh_entry; uep != NULL; uep = uep->ue_next)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1310 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1311 undo_write_bytes(bi, (long_u)UF_ENTRY_MAGIC, 2);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1312 if (serialize_uep(bi, uep) == FAIL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1313 return FAIL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1314 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1315 undo_write_bytes(bi, (long_u)UF_ENTRY_END_MAGIC, 2);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1316 return OK;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1317 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1318
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1319 static u_header_T *
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1320 unserialize_uhp(bufinfo_T *bi, char_u *file_name)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1321 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1322 u_header_T *uhp;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1323 int i;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1324 u_entry_T *uep, *last_uep;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1325 int c;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1326 int error;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1327
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1328 uhp = U_ALLOC_LINE(sizeof(u_header_T));
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1329 if (uhp == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1330 return NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1331 CLEAR_POINTER(uhp);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1332 #ifdef U_DEBUG
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1333 uhp->uh_magic = UH_MAGIC;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1334 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1335 uhp->uh_next.seq = undo_read_4c(bi);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1336 uhp->uh_prev.seq = undo_read_4c(bi);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1337 uhp->uh_alt_next.seq = undo_read_4c(bi);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1338 uhp->uh_alt_prev.seq = undo_read_4c(bi);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1339 uhp->uh_seq = undo_read_4c(bi);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1340 if (uhp->uh_seq <= 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1341 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1342 corruption_error("uh_seq", file_name);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1343 vim_free(uhp);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1344 return NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1345 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1346 unserialize_pos(bi, &uhp->uh_cursor);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1347 uhp->uh_cursor_vcol = undo_read_4c(bi);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1348 uhp->uh_flags = undo_read_2c(bi);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1349 for (i = 0; i < NMARKS; ++i)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1350 unserialize_pos(bi, &uhp->uh_namedm[i]);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1351 unserialize_visualinfo(bi, &uhp->uh_visual);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1352 uhp->uh_time = undo_read_time(bi);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1353
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1354 // Optional fields.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1355 for (;;)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1356 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1357 int len = undo_read_byte(bi);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1358 int what;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1359
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1360 if (len == EOF)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1361 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1362 corruption_error("truncated", file_name);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1363 u_free_uhp(uhp);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1364 return NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1365 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1366 if (len == 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1367 break;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1368 what = undo_read_byte(bi);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1369 switch (what)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1370 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1371 case UHP_SAVE_NR:
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1372 uhp->uh_save_nr = undo_read_4c(bi);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1373 break;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1374 default:
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1375 // field not supported, skip
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1376 while (--len >= 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1377 (void)undo_read_byte(bi);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1378 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1379 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1380
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1381 // Unserialize the uep list.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1382 last_uep = NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1383 while ((c = undo_read_2c(bi)) == UF_ENTRY_MAGIC)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1384 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1385 error = FALSE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1386 uep = unserialize_uep(bi, &error, file_name);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1387 if (last_uep == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1388 uhp->uh_entry = uep;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1389 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1390 last_uep->ue_next = uep;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1391 last_uep = uep;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1392 if (uep == NULL || error)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1393 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1394 u_free_uhp(uhp);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1395 return NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1396 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1397 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1398 if (c != UF_ENTRY_END_MAGIC)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1399 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1400 corruption_error("entry end", file_name);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1401 u_free_uhp(uhp);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1402 return NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1403 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1404
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1405 return uhp;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1406 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1407
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1408 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1409 * Serialize "uep".
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1410 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1411 static int
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1412 serialize_uep(
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1413 bufinfo_T *bi,
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1414 u_entry_T *uep)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1415 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1416 int i;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1417 size_t len;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1418
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1419 undo_write_bytes(bi, (long_u)uep->ue_top, 4);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1420 undo_write_bytes(bi, (long_u)uep->ue_bot, 4);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1421 undo_write_bytes(bi, (long_u)uep->ue_lcount, 4);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1422 undo_write_bytes(bi, (long_u)uep->ue_size, 4);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1423 for (i = 0; i < uep->ue_size; ++i)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1424 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1425 // Text is written without the text properties, since we cannot restore
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1426 // the text property types.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1427 len = STRLEN(uep->ue_array[i].ul_line);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1428 if (undo_write_bytes(bi, (long_u)len, 4) == FAIL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1429 return FAIL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1430 if (len > 0 && fwrite_crypt(bi, uep->ue_array[i].ul_line, len) == FAIL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1431 return FAIL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1432 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1433 return OK;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1434 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1435
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1436 static u_entry_T *
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1437 unserialize_uep(bufinfo_T *bi, int *error, char_u *file_name)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1438 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1439 int i;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1440 u_entry_T *uep;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1441 undoline_T *array = NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1442 char_u *line;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1443 int line_len;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1444
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1445 uep = U_ALLOC_LINE(sizeof(u_entry_T));
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1446 if (uep == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1447 return NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1448 CLEAR_POINTER(uep);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1449 #ifdef U_DEBUG
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1450 uep->ue_magic = UE_MAGIC;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1451 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1452 uep->ue_top = undo_read_4c(bi);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1453 uep->ue_bot = undo_read_4c(bi);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1454 uep->ue_lcount = undo_read_4c(bi);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1455 uep->ue_size = undo_read_4c(bi);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1456 if (uep->ue_size > 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1457 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1458 if (uep->ue_size < LONG_MAX / (int)sizeof(char_u *))
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1459 array = U_ALLOC_LINE(sizeof(undoline_T) * uep->ue_size);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1460 if (array == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1461 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1462 *error = TRUE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1463 return uep;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1464 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1465 vim_memset(array, 0, sizeof(undoline_T) * uep->ue_size);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1466 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1467 uep->ue_array = array;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1468
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1469 for (i = 0; i < uep->ue_size; ++i)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1470 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1471 line_len = undo_read_4c(bi);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1472 if (line_len >= 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1473 line = read_string_decrypt(bi, line_len);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1474 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1475 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1476 line = NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1477 corruption_error("line length", file_name);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1478 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1479 if (line == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1480 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1481 *error = TRUE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1482 return uep;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1483 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1484 array[i].ul_line = line;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1485 array[i].ul_len = line_len + 1;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1486 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1487 return uep;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1488 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1489
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1490 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1491 * Serialize "pos".
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1492 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1493 static void
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1494 serialize_pos(bufinfo_T *bi, pos_T pos)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1495 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1496 undo_write_bytes(bi, (long_u)pos.lnum, 4);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1497 undo_write_bytes(bi, (long_u)pos.col, 4);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1498 undo_write_bytes(bi, (long_u)pos.coladd, 4);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1499 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1500
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1501 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1502 * Unserialize the pos_T at the current position.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1503 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1504 static void
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1505 unserialize_pos(bufinfo_T *bi, pos_T *pos)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1506 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1507 pos->lnum = undo_read_4c(bi);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1508 if (pos->lnum < 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1509 pos->lnum = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1510 pos->col = undo_read_4c(bi);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1511 if (pos->col < 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1512 pos->col = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1513 pos->coladd = undo_read_4c(bi);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1514 if (pos->coladd < 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1515 pos->coladd = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1516 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1517
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1518 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1519 * Serialize "info".
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1520 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1521 static void
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1522 serialize_visualinfo(bufinfo_T *bi, visualinfo_T *info)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1523 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1524 serialize_pos(bi, info->vi_start);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1525 serialize_pos(bi, info->vi_end);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1526 undo_write_bytes(bi, (long_u)info->vi_mode, 4);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1527 undo_write_bytes(bi, (long_u)info->vi_curswant, 4);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1528 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1529
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1530 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1531 * Unserialize the visualinfo_T at the current position.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1532 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1533 static void
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1534 unserialize_visualinfo(bufinfo_T *bi, visualinfo_T *info)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1535 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1536 unserialize_pos(bi, &info->vi_start);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1537 unserialize_pos(bi, &info->vi_end);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1538 info->vi_mode = undo_read_4c(bi);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1539 info->vi_curswant = undo_read_4c(bi);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1540 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1541
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1542 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1543 * Write the undo tree in an undo file.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1544 * When "name" is not NULL, use it as the name of the undo file.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1545 * Otherwise use buf->b_ffname to generate the undo file name.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1546 * "buf" must never be null, buf->b_ffname is used to obtain the original file
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1547 * permissions.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1548 * "forceit" is TRUE for ":wundo!", FALSE otherwise.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1549 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1550 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1551 void
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1552 u_write_undo(
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1553 char_u *name,
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1554 int forceit,
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1555 buf_T *buf,
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1556 char_u *hash)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1557 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1558 u_header_T *uhp;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1559 char_u *file_name;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1560 int mark;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1561 #ifdef U_DEBUG
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1562 int headers_written = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1563 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1564 int fd;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1565 FILE *fp = NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1566 int perm;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1567 int write_ok = FALSE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1568 #ifdef UNIX
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1569 int st_old_valid = FALSE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1570 stat_T st_old;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1571 stat_T st_new;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1572 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1573 bufinfo_T bi;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1574
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1575 CLEAR_FIELD(bi);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1576
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1577 if (name == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1578 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1579 file_name = u_get_undo_file_name(buf->b_ffname, FALSE);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1580 if (file_name == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1581 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1582 if (p_verbose > 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1583 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1584 verbose_enter();
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1585 smsg(
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1586 _("Cannot write undo file in any directory in 'undodir'"));
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1587 verbose_leave();
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1588 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1589 return;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1590 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1591 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1592 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1593 file_name = name;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1594
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1595 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1596 * Decide about the permission to use for the undo file. If the buffer
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1597 * has a name use the permission of the original file. Otherwise only
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1598 * allow the user to access the undo file.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1599 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1600 perm = 0600;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1601 if (buf->b_ffname != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1602 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1603 #ifdef UNIX
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1604 if (mch_stat((char *)buf->b_ffname, &st_old) >= 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1605 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1606 perm = st_old.st_mode;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1607 st_old_valid = TRUE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1608 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1609 #else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1610 perm = mch_getperm(buf->b_ffname);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1611 if (perm < 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1612 perm = 0600;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1613 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1614 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1615
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1616 // strip any s-bit and executable bit
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1617 perm = perm & 0666;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1618
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1619 // If the undo file already exists, verify that it actually is an undo
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1620 // file, and delete it.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1621 if (mch_getperm(file_name) >= 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1622 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1623 if (name == NULL || !forceit)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1624 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1625 // Check we can read it and it's an undo file.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1626 fd = mch_open((char *)file_name, O_RDONLY|O_EXTRA, 0);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1627 if (fd < 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1628 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1629 if (name != NULL || p_verbose > 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1630 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1631 if (name == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1632 verbose_enter();
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1633 smsg(
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1634 _("Will not overwrite with undo file, cannot read: %s"),
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1635 file_name);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1636 if (name == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1637 verbose_leave();
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1638 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1639 goto theend;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1640 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1641 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1642 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1643 char_u mbuf[UF_START_MAGIC_LEN];
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1644 int len;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1645
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1646 len = read_eintr(fd, mbuf, UF_START_MAGIC_LEN);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1647 close(fd);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1648 if (len < UF_START_MAGIC_LEN
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1649 || memcmp(mbuf, UF_START_MAGIC, UF_START_MAGIC_LEN) != 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1650 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1651 if (name != NULL || p_verbose > 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1652 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1653 if (name == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1654 verbose_enter();
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1655 smsg(
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1656 _("Will not overwrite, this is not an undo file: %s"),
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1657 file_name);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1658 if (name == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1659 verbose_leave();
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1660 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1661 goto theend;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1662 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1663 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1664 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1665 mch_remove(file_name);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1666 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1667
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1668 // If there is no undo information at all, quit here after deleting any
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1669 // existing undo file.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1670 if (buf->b_u_numhead == 0 && buf->b_u_line_ptr.ul_line == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1671 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1672 if (p_verbose > 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1673 verb_msg(_("Skipping undo file write, nothing to undo"));
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1674 goto theend;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1675 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1676
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1677 fd = mch_open((char *)file_name,
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1678 O_CREAT|O_EXTRA|O_WRONLY|O_EXCL|O_NOFOLLOW, perm);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1679 if (fd < 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1680 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1681 semsg(_(e_cannot_open_undo_file_for_writing_str), file_name);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1682 goto theend;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1683 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1684 (void)mch_setperm(file_name, perm);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1685 if (p_verbose > 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1686 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1687 verbose_enter();
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1688 smsg(_("Writing undo file: %s"), file_name);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1689 verbose_leave();
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1690 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1691
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1692 #ifdef U_DEBUG
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1693 // Check there is no problem in undo info before writing.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1694 u_check(FALSE);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1695 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1696
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1697 #ifdef UNIX
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1698 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1699 * Try to set the group of the undo file same as the original file. If
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1700 * this fails, set the protection bits for the group same as the
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1701 * protection bits for others.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1702 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1703 if (st_old_valid
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1704 && mch_stat((char *)file_name, &st_new) >= 0
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1705 && st_new.st_gid != st_old.st_gid
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1706 # ifdef HAVE_FCHOWN // sequent-ptx lacks fchown()
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1707 && fchown(fd, (uid_t)-1, st_old.st_gid) != 0
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1708 # endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1709 )
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1710 mch_setperm(file_name, (perm & 0707) | ((perm & 07) << 3));
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1711 # if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1712 if (buf->b_ffname != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1713 mch_copy_sec(buf->b_ffname, file_name);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1714 # endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1715 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1716
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1717 fp = fdopen(fd, "w");
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1718 if (fp == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1719 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1720 semsg(_(e_cannot_open_undo_file_for_writing_str), file_name);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1721 close(fd);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1722 mch_remove(file_name);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1723 goto theend;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1724 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1725
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1726 // Undo must be synced.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1727 u_sync(TRUE);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1728
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1729 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1730 * Write the header. Initializes encryption, if enabled.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1731 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1732 bi.bi_buf = buf;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1733 bi.bi_fp = fp;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1734 if (serialize_header(&bi, hash) == FAIL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1735 goto write_error;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1736
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1737 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1738 * Iteratively serialize UHPs and their UEPs from the top down.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1739 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1740 mark = ++lastmark;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1741 uhp = buf->b_u_oldhead;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1742 while (uhp != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1743 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1744 // Serialize current UHP if we haven't seen it
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1745 if (uhp->uh_walk != mark)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1746 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1747 uhp->uh_walk = mark;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1748 #ifdef U_DEBUG
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1749 ++headers_written;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1750 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1751 if (serialize_uhp(&bi, uhp) == FAIL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1752 goto write_error;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1753 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1754
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1755 // Now walk through the tree - algorithm from undo_time().
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1756 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != mark)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1757 uhp = uhp->uh_prev.ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1758 else if (uhp->uh_alt_next.ptr != NULL
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1759 && uhp->uh_alt_next.ptr->uh_walk != mark)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1760 uhp = uhp->uh_alt_next.ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1761 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1762 && uhp->uh_next.ptr->uh_walk != mark)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1763 uhp = uhp->uh_next.ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1764 else if (uhp->uh_alt_prev.ptr != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1765 uhp = uhp->uh_alt_prev.ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1766 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1767 uhp = uhp->uh_next.ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1768 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1769
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1770 if (undo_write_bytes(&bi, (long_u)UF_HEADER_END_MAGIC, 2) == OK)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1771 write_ok = TRUE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1772 #ifdef U_DEBUG
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1773 if (headers_written != buf->b_u_numhead)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1774 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1775 semsg("Written %ld headers, ...", headers_written);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1776 semsg("... but numhead is %ld", buf->b_u_numhead);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1777 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1778 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1779
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1780 #ifdef FEAT_CRYPT
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1781 if (bi.bi_state != NULL && undo_flush(&bi) == FAIL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1782 write_ok = FALSE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1783 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1784
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1785 #if defined(UNIX) && defined(HAVE_FSYNC)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1786 if (p_fs && fflush(fp) == 0 && vim_fsync(fd) != 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1787 write_ok = FALSE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1788 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1789
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1790 write_error:
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1791 fclose(fp);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1792 if (!write_ok)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1793 semsg(_(e_write_error_in_undo_file_str), file_name);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1794
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1795 #if defined(MSWIN)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1796 // Copy file attributes; for systems where this can only be done after
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1797 // closing the file.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1798 if (buf->b_ffname != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1799 (void)mch_copy_file_attribute(buf->b_ffname, file_name);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1800 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1801 #ifdef HAVE_ACL
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1802 if (buf->b_ffname != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1803 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1804 vim_acl_T acl;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1805
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1806 // For systems that support ACL: get the ACL from the original file.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1807 acl = mch_get_acl(buf->b_ffname);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1808 mch_set_acl(file_name, acl);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1809 mch_free_acl(acl);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1810 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1811 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1812
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1813 theend:
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1814 #ifdef FEAT_CRYPT
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1815 if (bi.bi_state != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1816 crypt_free_state(bi.bi_state);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1817 vim_free(bi.bi_buffer);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1818 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1819 if (file_name != name)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1820 vim_free(file_name);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1821 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1822
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1823 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1824 * Load the undo tree from an undo file.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1825 * If "name" is not NULL use it as the undo file name. This also means being
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1826 * a bit more verbose.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1827 * Otherwise use curbuf->b_ffname to generate the undo file name.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1828 * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1829 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1830 void
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1831 u_read_undo(char_u *name, char_u *hash, char_u *orig_name UNUSED)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1832 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1833 char_u *file_name;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1834 FILE *fp;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1835 long version, str_len;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1836 undoline_T line_ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1837 linenr_T line_lnum;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1838 colnr_T line_colnr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1839 linenr_T line_count;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1840 long num_head = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1841 long old_header_seq, new_header_seq, cur_header_seq;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1842 long seq_last, seq_cur;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1843 long last_save_nr = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1844 short old_idx = -1, new_idx = -1, cur_idx = -1;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1845 long num_read_uhps = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1846 time_t seq_time;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1847 int i, j;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1848 int c;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1849 u_header_T *uhp;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1850 u_header_T **uhp_table = NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1851 char_u read_hash[UNDO_HASH_SIZE];
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1852 char_u magic_buf[UF_START_MAGIC_LEN];
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1853 #ifdef U_DEBUG
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1854 int *uhp_table_used;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1855 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1856 #ifdef UNIX
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1857 stat_T st_orig;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1858 stat_T st_undo;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1859 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1860 bufinfo_T bi;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1861
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1862 CLEAR_FIELD(bi);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1863 line_ptr.ul_len = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1864 line_ptr.ul_line = NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1865
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1866 if (name == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1867 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1868 file_name = u_get_undo_file_name(curbuf->b_ffname, TRUE);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1869 if (file_name == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1870 return;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1871
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1872 #ifdef UNIX
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1873 // For safety we only read an undo file if the owner is equal to the
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1874 // owner of the text file or equal to the current user.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1875 if (mch_stat((char *)orig_name, &st_orig) >= 0
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1876 && mch_stat((char *)file_name, &st_undo) >= 0
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1877 && st_orig.st_uid != st_undo.st_uid
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1878 && st_undo.st_uid != getuid())
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1879 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1880 if (p_verbose > 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1881 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1882 verbose_enter();
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1883 smsg(_("Not reading undo file, owner differs: %s"),
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1884 file_name);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1885 verbose_leave();
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1886 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1887 return;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1888 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1889 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1890 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1891 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1892 file_name = name;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1893
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1894 if (p_verbose > 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1895 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1896 verbose_enter();
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1897 smsg(_("Reading undo file: %s"), file_name);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1898 verbose_leave();
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1899 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1900
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1901 fp = mch_fopen((char *)file_name, "r");
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1902 if (fp == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1903 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1904 if (name != NULL || p_verbose > 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1905 semsg(_(e_cannot_open_undo_file_for_reading_str), file_name);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1906 goto error;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1907 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1908 bi.bi_buf = curbuf;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1909 bi.bi_fp = fp;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1910
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1911 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1912 * Read the undo file header.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1913 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1914 if (fread(magic_buf, UF_START_MAGIC_LEN, 1, fp) != 1
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1915 || memcmp(magic_buf, UF_START_MAGIC, UF_START_MAGIC_LEN) != 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1916 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1917 semsg(_(e_not_an_undo_file_str), file_name);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1918 goto error;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1919 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1920 version = get2c(fp);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1921 if (version == UF_VERSION_CRYPT)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1922 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1923 #ifdef FEAT_CRYPT
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1924 if (*curbuf->b_p_key == NUL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1925 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1926 semsg(_(e_non_encrypted_file_has_encrypted_undo_file_str),
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1927 file_name);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1928 goto error;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1929 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1930 bi.bi_state = crypt_create_from_file(fp, curbuf->b_p_key);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1931 if (bi.bi_state == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1932 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1933 semsg(_(e_undo_file_decryption_failed), file_name);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1934 goto error;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1935 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1936 if (crypt_whole_undofile(bi.bi_state->method_nr))
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1937 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1938 bi.bi_buffer = alloc(CRYPT_BUF_SIZE);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1939 if (bi.bi_buffer == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1940 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1941 crypt_free_state(bi.bi_state);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1942 bi.bi_state = NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1943 goto error;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1944 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1945 bi.bi_avail = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1946 bi.bi_used = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1947 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1948 #else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1949 semsg(_(e_undo_file_is_encrypted_str), file_name);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1950 goto error;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1951 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1952 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1953 else if (version != UF_VERSION)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1954 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1955 semsg(_(e_incompatible_undo_file_str), file_name);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1956 goto error;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1957 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1958
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1959 if (undo_read(&bi, read_hash, (size_t)UNDO_HASH_SIZE) == FAIL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1960 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1961 corruption_error("hash", file_name);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1962 goto error;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1963 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1964 line_count = (linenr_T)undo_read_4c(&bi);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1965 if (memcmp(hash, read_hash, UNDO_HASH_SIZE) != 0
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1966 || line_count != curbuf->b_ml.ml_line_count)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1967 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1968 if (p_verbose > 0 || name != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1969 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1970 if (name == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1971 verbose_enter();
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1972 give_warning((char_u *)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1973 _("File contents changed, cannot use undo info"), TRUE);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1974 if (name == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1975 verbose_leave();
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1976 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1977 goto error;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1978 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1979
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1980 // Read undo data for "U" command.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1981 str_len = undo_read_4c(&bi);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1982 if (str_len < 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1983 goto error;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1984 if (str_len > 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1985 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1986 line_ptr.ul_line = read_string_decrypt(&bi, str_len);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1987 line_ptr.ul_len = str_len + 1;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1988 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1989 line_lnum = (linenr_T)undo_read_4c(&bi);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1990 line_colnr = (colnr_T)undo_read_4c(&bi);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1991 if (line_lnum < 0 || line_colnr < 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1992 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1993 corruption_error("line lnum/col", file_name);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1994 goto error;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1995 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1996
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1997 // Begin general undo data
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1998 old_header_seq = undo_read_4c(&bi);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
1999 new_header_seq = undo_read_4c(&bi);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2000 cur_header_seq = undo_read_4c(&bi);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2001 num_head = undo_read_4c(&bi);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2002 seq_last = undo_read_4c(&bi);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2003 seq_cur = undo_read_4c(&bi);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2004 seq_time = undo_read_time(&bi);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2005
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2006 // Optional header fields.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2007 for (;;)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2008 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2009 int len = undo_read_byte(&bi);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2010 int what;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2011
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2012 if (len == 0 || len == EOF)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2013 break;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2014 what = undo_read_byte(&bi);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2015 switch (what)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2016 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2017 case UF_LAST_SAVE_NR:
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2018 last_save_nr = undo_read_4c(&bi);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2019 break;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2020 default:
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2021 // field not supported, skip
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2022 while (--len >= 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2023 (void)undo_read_byte(&bi);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2024 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2025 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2026
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2027 // uhp_table will store the freshly created undo headers we allocate
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2028 // until we insert them into curbuf. The table remains sorted by the
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2029 // sequence numbers of the headers.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2030 // When there are no headers uhp_table is NULL.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2031 if (num_head > 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2032 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2033 if (num_head < LONG_MAX / (long)sizeof(u_header_T *))
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2034 uhp_table = U_ALLOC_LINE(num_head * sizeof(u_header_T *));
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2035 if (uhp_table == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2036 goto error;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2037 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2038
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2039 while ((c = undo_read_2c(&bi)) == UF_HEADER_MAGIC)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2040 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2041 if (num_read_uhps >= num_head)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2042 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2043 corruption_error("num_head too small", file_name);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2044 goto error;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2045 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2046
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2047 uhp = unserialize_uhp(&bi, file_name);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2048 if (uhp == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2049 goto error;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2050 uhp_table[num_read_uhps++] = uhp;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2051 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2052
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2053 if (num_read_uhps != num_head)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2054 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2055 corruption_error("num_head", file_name);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2056 goto error;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2057 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2058 if (c != UF_HEADER_END_MAGIC)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2059 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2060 corruption_error("end marker", file_name);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2061 goto error;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2062 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2063
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2064 #ifdef U_DEBUG
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2065 uhp_table_used = alloc_clear(sizeof(int) * num_head + 1);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2066 # define SET_FLAG(j) ++uhp_table_used[j]
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2067 #else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2068 # define SET_FLAG(j)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2069 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2070
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2071 // We have put all of the headers into a table. Now we iterate through the
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2072 // table and swizzle each sequence number we have stored in uh_*_seq into
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2073 // a pointer corresponding to the header with that sequence number.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2074 for (i = 0; i < num_head; i++)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2075 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2076 uhp = uhp_table[i];
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2077 if (uhp == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2078 continue;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2079 for (j = 0; j < num_head; j++)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2080 if (uhp_table[j] != NULL && i != j
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2081 && uhp_table[i]->uh_seq == uhp_table[j]->uh_seq)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2082 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2083 corruption_error("duplicate uh_seq", file_name);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2084 goto error;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2085 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2086 for (j = 0; j < num_head; j++)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2087 if (uhp_table[j] != NULL
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2088 && uhp_table[j]->uh_seq == uhp->uh_next.seq)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2089 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2090 uhp->uh_next.ptr = uhp_table[j];
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2091 SET_FLAG(j);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2092 break;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2093 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2094 for (j = 0; j < num_head; j++)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2095 if (uhp_table[j] != NULL
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2096 && uhp_table[j]->uh_seq == uhp->uh_prev.seq)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2097 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2098 uhp->uh_prev.ptr = uhp_table[j];
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2099 SET_FLAG(j);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2100 break;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2101 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2102 for (j = 0; j < num_head; j++)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2103 if (uhp_table[j] != NULL
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2104 && uhp_table[j]->uh_seq == uhp->uh_alt_next.seq)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2105 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2106 uhp->uh_alt_next.ptr = uhp_table[j];
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2107 SET_FLAG(j);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2108 break;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2109 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2110 for (j = 0; j < num_head; j++)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2111 if (uhp_table[j] != NULL
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2112 && uhp_table[j]->uh_seq == uhp->uh_alt_prev.seq)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2113 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2114 uhp->uh_alt_prev.ptr = uhp_table[j];
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2115 SET_FLAG(j);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2116 break;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2117 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2118 if (old_header_seq > 0 && old_idx < 0 && uhp->uh_seq == old_header_seq)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2119 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2120 old_idx = i;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2121 SET_FLAG(i);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2122 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2123 if (new_header_seq > 0 && new_idx < 0 && uhp->uh_seq == new_header_seq)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2124 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2125 new_idx = i;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2126 SET_FLAG(i);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2127 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2128 if (cur_header_seq > 0 && cur_idx < 0 && uhp->uh_seq == cur_header_seq)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2129 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2130 cur_idx = i;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2131 SET_FLAG(i);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2132 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2133 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2134
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2135 // Now that we have read the undo info successfully, free the current undo
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2136 // info and use the info from the file.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2137 u_blockfree(curbuf);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2138 curbuf->b_u_oldhead = old_idx < 0 ? NULL : uhp_table[old_idx];
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2139 curbuf->b_u_newhead = new_idx < 0 ? NULL : uhp_table[new_idx];
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2140 curbuf->b_u_curhead = cur_idx < 0 ? NULL : uhp_table[cur_idx];
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2141 curbuf->b_u_line_ptr = line_ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2142 curbuf->b_u_line_lnum = line_lnum;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2143 curbuf->b_u_line_colnr = line_colnr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2144 curbuf->b_u_numhead = num_head;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2145 curbuf->b_u_seq_last = seq_last;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2146 curbuf->b_u_seq_cur = seq_cur;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2147 curbuf->b_u_time_cur = seq_time;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2148 curbuf->b_u_save_nr_last = last_save_nr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2149 curbuf->b_u_save_nr_cur = last_save_nr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2150
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2151 curbuf->b_u_synced = TRUE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2152 vim_free(uhp_table);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2153
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2154 #ifdef U_DEBUG
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2155 for (i = 0; i < num_head; ++i)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2156 if (uhp_table_used[i] == 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2157 semsg("uhp_table entry %ld not used, leaking memory", i);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2158 vim_free(uhp_table_used);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2159 u_check(TRUE);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2160 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2161
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2162 if (name != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2163 smsg(_("Finished reading undo file %s"), file_name);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2164 goto theend;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2165
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2166 error:
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2167 vim_free(line_ptr.ul_line);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2168 if (uhp_table != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2169 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2170 for (i = 0; i < num_read_uhps; i++)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2171 if (uhp_table[i] != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2172 u_free_uhp(uhp_table[i]);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2173 vim_free(uhp_table);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2174 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2175
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2176 theend:
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2177 #ifdef FEAT_CRYPT
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2178 if (bi.bi_state != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2179 crypt_free_state(bi.bi_state);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2180 vim_free(bi.bi_buffer);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2181 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2182 if (fp != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2183 fclose(fp);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2184 if (file_name != name)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2185 vim_free(file_name);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2186 return;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2187 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2188
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2189 #endif // FEAT_PERSISTENT_UNDO
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2190
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2191
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2192 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2193 * If 'cpoptions' contains 'u': Undo the previous undo or redo (vi compatible).
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2194 * If 'cpoptions' does not contain 'u': Always undo.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2195 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2196 void
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2197 u_undo(int count)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2198 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2199 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2200 * If we get an undo command while executing a macro, we behave like the
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2201 * original vi. If this happens twice in one macro the result will not
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2202 * be compatible.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2203 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2204 if (curbuf->b_u_synced == FALSE)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2205 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2206 u_sync(TRUE);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2207 count = 1;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2208 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2209
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2210 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2211 undo_undoes = TRUE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2212 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2213 undo_undoes = !undo_undoes;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2214 u_doit(count);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2215 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2216
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2217 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2218 * If 'cpoptions' contains 'u': Repeat the previous undo or redo.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2219 * If 'cpoptions' does not contain 'u': Always redo.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2220 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2221 void
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2222 u_redo(int count)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2223 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2224 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2225 undo_undoes = FALSE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2226 u_doit(count);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2227 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2228
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2229 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2230 * Undo or redo, depending on 'undo_undoes', 'count' times.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2231 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2232 static void
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2233 u_doit(int startcount)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2234 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2235 int count = startcount;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2236
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2237 if (!undo_allowed())
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2238 return;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2239
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2240 u_newcount = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2241 u_oldcount = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2242 if (curbuf->b_ml.ml_flags & ML_EMPTY)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2243 u_oldcount = -1;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2244 while (count--)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2245 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2246 // Do the change warning now, so that it triggers FileChangedRO when
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2247 // needed. This may cause the file to be reloaded, that must happen
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2248 // before we do anything, because it may change curbuf->b_u_curhead
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2249 // and more.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2250 change_warning(0);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2251
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2252 if (undo_undoes)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2253 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2254 if (curbuf->b_u_curhead == NULL) // first undo
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2255 curbuf->b_u_curhead = curbuf->b_u_newhead;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2256 else if (get_undolevel() > 0) // multi level undo
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2257 // get next undo
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2258 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_next.ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2259 // nothing to undo
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2260 if (curbuf->b_u_numhead == 0 || curbuf->b_u_curhead == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2261 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2262 // stick curbuf->b_u_curhead at end
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2263 curbuf->b_u_curhead = curbuf->b_u_oldhead;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2264 beep_flush();
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2265 if (count == startcount - 1)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2266 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2267 msg(_("Already at oldest change"));
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2268 return;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2269 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2270 break;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2271 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2272
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2273 u_undoredo(TRUE);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2274 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2275 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2276 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2277 if (curbuf->b_u_curhead == NULL || get_undolevel() <= 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2278 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2279 beep_flush(); // nothing to redo
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2280 if (count == startcount - 1)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2281 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2282 msg(_("Already at newest change"));
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2283 return;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2284 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2285 break;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2286 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2287
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2288 u_undoredo(FALSE);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2289
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2290 // Advance for next redo. Set "newhead" when at the end of the
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2291 // redoable changes.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2292 if (curbuf->b_u_curhead->uh_prev.ptr == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2293 curbuf->b_u_newhead = curbuf->b_u_curhead;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2294 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_prev.ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2295 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2296 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2297 u_undo_end(undo_undoes, FALSE);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2298 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2299
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2300 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2301 * Undo or redo over the timeline.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2302 * When "step" is negative go back in time, otherwise goes forward in time.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2303 * When "sec" is FALSE make "step" steps, when "sec" is TRUE use "step" as
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2304 * seconds.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2305 * When "file" is TRUE use "step" as a number of file writes.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2306 * When "absolute" is TRUE use "step" as the sequence number to jump to.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2307 * "sec" must be FALSE then.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2308 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2309 void
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2310 undo_time(
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2311 long step,
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2312 int sec,
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2313 int file,
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2314 int absolute)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2315 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2316 long target;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2317 long closest;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2318 long closest_start;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2319 long closest_seq = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2320 long val;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2321 u_header_T *uhp = NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2322 u_header_T *last;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2323 int mark;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2324 int nomark = 0; // shut up compiler
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2325 int round;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2326 int dosec = sec;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2327 int dofile = file;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2328 int above = FALSE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2329 int did_undo = TRUE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2330
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2331 if (text_locked())
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2332 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2333 text_locked_msg();
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2334 return;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2335 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2336
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2337 // First make sure the current undoable change is synced.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2338 if (curbuf->b_u_synced == FALSE)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2339 u_sync(TRUE);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2340
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2341 u_newcount = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2342 u_oldcount = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2343 if (curbuf->b_ml.ml_flags & ML_EMPTY)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2344 u_oldcount = -1;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2345
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2346 // "target" is the node below which we want to be.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2347 // Init "closest" to a value we can't reach.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2348 if (absolute)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2349 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2350 target = step;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2351 closest = -1;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2352 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2353 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2354 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2355 if (dosec)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2356 target = (long)(curbuf->b_u_time_cur) + step;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2357 else if (dofile)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2358 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2359 if (step < 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2360 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2361 // Going back to a previous write. If there were changes after
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2362 // the last write, count that as moving one file-write, so
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2363 // that ":earlier 1f" undoes all changes since the last save.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2364 uhp = curbuf->b_u_curhead;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2365 if (uhp != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2366 uhp = uhp->uh_next.ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2367 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2368 uhp = curbuf->b_u_newhead;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2369 if (uhp != NULL && uhp->uh_save_nr != 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2370 // "uh_save_nr" was set in the last block, that means
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2371 // there were no changes since the last write
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2372 target = curbuf->b_u_save_nr_cur + step;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2373 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2374 // count the changes since the last write as one step
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2375 target = curbuf->b_u_save_nr_cur + step + 1;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2376 if (target <= 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2377 // Go to before first write: before the oldest change. Use
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2378 // the sequence number for that.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2379 dofile = FALSE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2380 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2381 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2382 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2383 // Moving forward to a newer write.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2384 target = curbuf->b_u_save_nr_cur + step;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2385 if (target > curbuf->b_u_save_nr_last)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2386 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2387 // Go to after last write: after the latest change. Use
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2388 // the sequence number for that.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2389 target = curbuf->b_u_seq_last + 1;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2390 dofile = FALSE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2391 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2392 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2393 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2394 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2395 target = curbuf->b_u_seq_cur + step;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2396 if (step < 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2397 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2398 if (target < 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2399 target = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2400 closest = -1;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2401 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2402 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2403 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2404 if (dosec)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2405 closest = (long)(vim_time() + 1);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2406 else if (dofile)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2407 closest = curbuf->b_u_save_nr_last + 2;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2408 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2409 closest = curbuf->b_u_seq_last + 2;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2410 if (target >= closest)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2411 target = closest - 1;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2412 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2413 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2414 closest_start = closest;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2415 closest_seq = curbuf->b_u_seq_cur;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2416
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2417 // When "target" is 0; Back to origin.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2418 if (target == 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2419 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2420 mark = lastmark; // avoid that GCC complains
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2421 goto target_zero;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2422 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2423
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2424 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2425 * May do this twice:
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2426 * 1. Search for "target", update "closest" to the best match found.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2427 * 2. If "target" not found search for "closest".
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2428 *
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2429 * When using the closest time we use the sequence number in the second
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2430 * round, because there may be several entries with the same time.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2431 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2432 for (round = 1; round <= 2; ++round)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2433 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2434 // Find the path from the current state to where we want to go. The
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2435 // desired state can be anywhere in the undo tree, need to go all over
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2436 // it. We put "nomark" in uh_walk where we have been without success,
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2437 // "mark" where it could possibly be.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2438 mark = ++lastmark;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2439 nomark = ++lastmark;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2440
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2441 if (curbuf->b_u_curhead == NULL) // at leaf of the tree
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2442 uhp = curbuf->b_u_newhead;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2443 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2444 uhp = curbuf->b_u_curhead;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2445
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2446 while (uhp != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2447 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2448 uhp->uh_walk = mark;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2449 if (dosec)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2450 val = (long)(uhp->uh_time);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2451 else if (dofile)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2452 val = uhp->uh_save_nr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2453 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2454 val = uhp->uh_seq;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2455
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2456 if (round == 1 && !(dofile && val == 0))
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2457 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2458 // Remember the header that is closest to the target.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2459 // It must be at least in the right direction (checked with
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2460 // "b_u_seq_cur"). When the timestamp is equal find the
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2461 // highest/lowest sequence number.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2462 if ((step < 0 ? uhp->uh_seq <= curbuf->b_u_seq_cur
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2463 : uhp->uh_seq > curbuf->b_u_seq_cur)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2464 && ((dosec && val == closest)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2465 ? (step < 0
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2466 ? uhp->uh_seq < closest_seq
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2467 : uhp->uh_seq > closest_seq)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2468 : closest == closest_start
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2469 || (val > target
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2470 ? (closest > target
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2471 ? val - target <= closest - target
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2472 : val - target <= target - closest)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2473 : (closest > target
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2474 ? target - val <= closest - target
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2475 : target - val <= target - closest))))
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2476 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2477 closest = val;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2478 closest_seq = uhp->uh_seq;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2479 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2480 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2481
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2482 // Quit searching when we found a match. But when searching for a
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2483 // time we need to continue looking for the best uh_seq.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2484 if (target == val && !dosec)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2485 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2486 target = uhp->uh_seq;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2487 break;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2488 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2489
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2490 // go down in the tree if we haven't been there
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2491 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != nomark
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2492 && uhp->uh_prev.ptr->uh_walk != mark)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2493 uhp = uhp->uh_prev.ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2494
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2495 // go to alternate branch if we haven't been there
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2496 else if (uhp->uh_alt_next.ptr != NULL
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2497 && uhp->uh_alt_next.ptr->uh_walk != nomark
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2498 && uhp->uh_alt_next.ptr->uh_walk != mark)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2499 uhp = uhp->uh_alt_next.ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2500
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2501 // go up in the tree if we haven't been there and we are at the
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2502 // start of alternate branches
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2503 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2504 && uhp->uh_next.ptr->uh_walk != nomark
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2505 && uhp->uh_next.ptr->uh_walk != mark)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2506 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2507 // If still at the start we don't go through this change.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2508 if (uhp == curbuf->b_u_curhead)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2509 uhp->uh_walk = nomark;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2510 uhp = uhp->uh_next.ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2511 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2512
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2513 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2514 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2515 // need to backtrack; mark this node as useless
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2516 uhp->uh_walk = nomark;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2517 if (uhp->uh_alt_prev.ptr != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2518 uhp = uhp->uh_alt_prev.ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2519 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2520 uhp = uhp->uh_next.ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2521 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2522 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2523
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2524 if (uhp != NULL) // found it
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2525 break;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2526
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2527 if (absolute)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2528 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2529 semsg(_(e_undo_number_nr_not_found), step);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2530 return;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2531 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2532
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2533 if (closest == closest_start)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2534 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2535 if (step < 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2536 msg(_("Already at oldest change"));
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2537 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2538 msg(_("Already at newest change"));
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2539 return;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2540 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2541
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2542 target = closest_seq;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2543 dosec = FALSE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2544 dofile = FALSE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2545 if (step < 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2546 above = TRUE; // stop above the header
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2547 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2548
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2549 target_zero:
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2550 // If we found it: Follow the path to go to where we want to be.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2551 if (uhp != NULL || target == 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2552 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2553 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2554 * First go up the tree as much as needed.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2555 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2556 while (!got_int)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2557 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2558 // Do the change warning now, for the same reason as above.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2559 change_warning(0);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2560
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2561 uhp = curbuf->b_u_curhead;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2562 if (uhp == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2563 uhp = curbuf->b_u_newhead;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2564 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2565 uhp = uhp->uh_next.ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2566 if (uhp == NULL || (target > 0 && uhp->uh_walk != mark)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2567 || (uhp->uh_seq == target && !above))
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2568 break;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2569 curbuf->b_u_curhead = uhp;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2570 u_undoredo(TRUE);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2571 if (target > 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2572 uhp->uh_walk = nomark; // don't go back down here
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2573 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2574
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2575 // When back to origin, redo is not needed.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2576 if (target > 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2577 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2578 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2579 * And now go down the tree (redo), branching off where needed.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2580 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2581 while (!got_int)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2582 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2583 // Do the change warning now, for the same reason as above.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2584 change_warning(0);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2585
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2586 uhp = curbuf->b_u_curhead;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2587 if (uhp == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2588 break;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2589
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2590 // Go back to the first branch with a mark.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2591 while (uhp->uh_alt_prev.ptr != NULL
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2592 && uhp->uh_alt_prev.ptr->uh_walk == mark)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2593 uhp = uhp->uh_alt_prev.ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2594
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2595 // Find the last branch with a mark, that's the one.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2596 last = uhp;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2597 while (last->uh_alt_next.ptr != NULL
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2598 && last->uh_alt_next.ptr->uh_walk == mark)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2599 last = last->uh_alt_next.ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2600 if (last != uhp)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2601 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2602 // Make the used branch the first entry in the list of
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2603 // alternatives to make "u" and CTRL-R take this branch.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2604 while (uhp->uh_alt_prev.ptr != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2605 uhp = uhp->uh_alt_prev.ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2606 if (last->uh_alt_next.ptr != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2607 last->uh_alt_next.ptr->uh_alt_prev.ptr =
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2608 last->uh_alt_prev.ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2609 last->uh_alt_prev.ptr->uh_alt_next.ptr =
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2610 last->uh_alt_next.ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2611 last->uh_alt_prev.ptr = NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2612 last->uh_alt_next.ptr = uhp;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2613 uhp->uh_alt_prev.ptr = last;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2614
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2615 if (curbuf->b_u_oldhead == uhp)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2616 curbuf->b_u_oldhead = last;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2617 uhp = last;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2618 if (uhp->uh_next.ptr != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2619 uhp->uh_next.ptr->uh_prev.ptr = uhp;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2620 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2621 curbuf->b_u_curhead = uhp;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2622
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2623 if (uhp->uh_walk != mark)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2624 break; // must have reached the target
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2625
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2626 // Stop when going backwards in time and didn't find the exact
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2627 // header we were looking for.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2628 if (uhp->uh_seq == target && above)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2629 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2630 curbuf->b_u_seq_cur = target - 1;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2631 break;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2632 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2633
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2634 u_undoredo(FALSE);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2635
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2636 // Advance "curhead" to below the header we last used. If it
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2637 // becomes NULL then we need to set "newhead" to this leaf.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2638 if (uhp->uh_prev.ptr == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2639 curbuf->b_u_newhead = uhp;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2640 curbuf->b_u_curhead = uhp->uh_prev.ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2641 did_undo = FALSE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2642
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2643 if (uhp->uh_seq == target) // found it!
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2644 break;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2645
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2646 uhp = uhp->uh_prev.ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2647 if (uhp == NULL || uhp->uh_walk != mark)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2648 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2649 // Need to redo more but can't find it...
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2650 internal_error("undo_time()");
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2651 break;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2652 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2653 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2654 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2655 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2656 u_undo_end(did_undo, absolute);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2657 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2658
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2659 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2660 * u_undoredo: common code for undo and redo
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2661 *
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2662 * The lines in the file are replaced by the lines in the entry list at
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2663 * curbuf->b_u_curhead. The replaced lines in the file are saved in the entry
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2664 * list for the next undo/redo.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2665 *
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2666 * When "undo" is TRUE we go up in the tree, when FALSE we go down.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2667 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2668 static void
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2669 u_undoredo(int undo)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2670 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2671 undoline_T *newarray = NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2672 linenr_T oldsize;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2673 linenr_T newsize;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2674 linenr_T top, bot;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2675 linenr_T lnum;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2676 linenr_T newlnum = MAXLNUM;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2677 pos_T new_curpos = curwin->w_cursor;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2678 long i;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2679 u_entry_T *uep, *nuep;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2680 u_entry_T *newlist = NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2681 int old_flags;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2682 int new_flags;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2683 pos_T namedm[NMARKS];
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2684 visualinfo_T visualinfo;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2685 int empty_buffer; // buffer became empty
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2686 u_header_T *curhead = curbuf->b_u_curhead;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2687
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2688 // Don't want autocommands using the undo structures here, they are
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2689 // invalid till the end.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2690 block_autocmds();
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2691
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2692 #ifdef U_DEBUG
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2693 u_check(FALSE);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2694 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2695 old_flags = curhead->uh_flags;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2696 new_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2697 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2698 setpcmark();
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2699
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2700 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2701 * save marks before undo/redo
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2702 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2703 mch_memmove(namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2704 visualinfo = curbuf->b_visual;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2705 curbuf->b_op_start.lnum = curbuf->b_ml.ml_line_count;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2706 curbuf->b_op_start.col = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2707 curbuf->b_op_end.lnum = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2708 curbuf->b_op_end.col = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2709
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2710 for (uep = curhead->uh_entry; uep != NULL; uep = nuep)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2711 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2712 top = uep->ue_top;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2713 bot = uep->ue_bot;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2714 if (bot == 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2715 bot = curbuf->b_ml.ml_line_count + 1;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2716 if (top > curbuf->b_ml.ml_line_count || top >= bot
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2717 || bot > curbuf->b_ml.ml_line_count + 1)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2718 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2719 unblock_autocmds();
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2720 iemsg(e_u_undo_line_numbers_wrong);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2721 changed(); // don't want UNCHANGED now
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2722 return;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2723 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2724
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2725 oldsize = bot - top - 1; // number of lines before undo
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2726 newsize = uep->ue_size; // number of lines after undo
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2727
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2728 // Decide about the cursor position, depending on what text changed.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2729 // Don't set it yet, it may be invalid if lines are going to be added.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2730 if (top < newlnum)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2731 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2732 // If the saved cursor is somewhere in this undo block, move it to
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2733 // the remembered position. Makes "gwap" put the cursor back
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2734 // where it was.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2735 lnum = curhead->uh_cursor.lnum;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2736 if (lnum >= top && lnum <= top + newsize + 1)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2737 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2738 new_curpos = curhead->uh_cursor;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2739 newlnum = new_curpos.lnum - 1;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2740 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2741 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2742 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2743 // Use the first line that actually changed. Avoids that
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2744 // undoing auto-formatting puts the cursor in the previous
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2745 // line.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2746 for (i = 0; i < newsize && i < oldsize; ++i)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2747 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2748 char_u *p = ml_get(top + 1 + i);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2749
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2750 if (curbuf->b_ml.ml_line_len != uep->ue_array[i].ul_len
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2751 || memcmp(uep->ue_array[i].ul_line, p,
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2752 curbuf->b_ml.ml_line_len) != 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2753 break;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2754 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2755 if (i == newsize && newlnum == MAXLNUM && uep->ue_next == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2756 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2757 newlnum = top;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2758 new_curpos.lnum = newlnum + 1;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2759 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2760 else if (i < newsize)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2761 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2762 newlnum = top + i;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2763 new_curpos.lnum = newlnum + 1;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2764 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2765 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2766 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2767
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2768 empty_buffer = FALSE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2769
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2770 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2771 * Delete the lines between top and bot and save them in newarray.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2772 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2773 if (oldsize > 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2774 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2775 if ((newarray = U_ALLOC_LINE(sizeof(undoline_T) * oldsize)) == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2776 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2777 do_outofmem_msg((long_u)(sizeof(undoline_T) * oldsize));
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2778
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2779 // We have messed up the entry list, repair is impossible.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2780 // we have to free the rest of the list.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2781 while (uep != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2782 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2783 nuep = uep->ue_next;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2784 u_freeentry(uep, uep->ue_size);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2785 uep = nuep;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2786 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2787 break;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2788 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2789 // delete backwards, it goes faster in most cases
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2790 for (lnum = bot - 1, i = oldsize; --i >= 0; --lnum)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2791 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2792 // what can we do when we run out of memory?
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2793 if (u_save_line(&newarray[i], lnum) == FAIL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2794 do_outofmem_msg((long_u)0);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2795 // remember we deleted the last line in the buffer, and a
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2796 // dummy empty line will be inserted
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2797 if (curbuf->b_ml.ml_line_count == 1)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2798 empty_buffer = TRUE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2799 ml_delete_flags(lnum, ML_DEL_UNDO);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2800 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2801 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2802 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2803 newarray = NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2804
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2805 // make sure the cursor is on a valid line after the deletions
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2806 check_cursor_lnum();
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2807
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2808 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2809 * Insert the lines in u_array between top and bot.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2810 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2811 if (newsize)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2812 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2813 for (lnum = top, i = 0; i < newsize; ++i, ++lnum)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2814 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2815 // If the file is empty, there is an empty line 1 that we
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2816 // should get rid of, by replacing it with the new line.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2817 if (empty_buffer && lnum == 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2818 ml_replace_len((linenr_T)1, uep->ue_array[i].ul_line,
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2819 uep->ue_array[i].ul_len, TRUE, TRUE);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2820 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2821 ml_append_flags(lnum, uep->ue_array[i].ul_line,
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2822 (colnr_T)uep->ue_array[i].ul_len, ML_APPEND_UNDO);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2823 vim_free(uep->ue_array[i].ul_line);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2824 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2825 vim_free((char_u *)uep->ue_array);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2826 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2827
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2828 // adjust marks
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2829 if (oldsize != newsize)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2830 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2831 mark_adjust(top + 1, top + oldsize, (long)MAXLNUM,
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2832 (long)newsize - (long)oldsize);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2833 if (curbuf->b_op_start.lnum > top + oldsize)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2834 curbuf->b_op_start.lnum += newsize - oldsize;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2835 if (curbuf->b_op_end.lnum > top + oldsize)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2836 curbuf->b_op_end.lnum += newsize - oldsize;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2837 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2838 if (oldsize > 0 || newsize > 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2839 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2840 changed_lines(top + 1, 0, bot, newsize - oldsize);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2841 #ifdef FEAT_SPELL
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2842 // When text has been changed, possibly the start of the next line
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2843 // may have SpellCap that should be removed or it needs to be
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2844 // displayed. Schedule the next line for redrawing just in case.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2845 if (spell_check_window(curwin) && bot <= curbuf->b_ml.ml_line_count)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2846 redrawWinline(curwin, bot);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2847 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2848 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2849
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2850 // Set the '[ mark.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2851 if (top + 1 < curbuf->b_op_start.lnum)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2852 curbuf->b_op_start.lnum = top + 1;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2853 // Set the '] mark.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2854 if (newsize == 0 && top + 1 > curbuf->b_op_end.lnum)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2855 curbuf->b_op_end.lnum = top + 1;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2856 else if (top + newsize > curbuf->b_op_end.lnum)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2857 curbuf->b_op_end.lnum = top + newsize;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2858
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2859 u_newcount += newsize;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2860 u_oldcount += oldsize;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2861 uep->ue_size = oldsize;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2862 uep->ue_array = newarray;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2863 uep->ue_bot = top + newsize + 1;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2864
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2865 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2866 * insert this entry in front of the new entry list
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2867 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2868 nuep = uep->ue_next;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2869 uep->ue_next = newlist;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2870 newlist = uep;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2871 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2872
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2873 // Ensure the '[ and '] marks are within bounds.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2874 if (curbuf->b_op_start.lnum > curbuf->b_ml.ml_line_count)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2875 curbuf->b_op_start.lnum = curbuf->b_ml.ml_line_count;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2876 if (curbuf->b_op_end.lnum > curbuf->b_ml.ml_line_count)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2877 curbuf->b_op_end.lnum = curbuf->b_ml.ml_line_count;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2878
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2879 // Set the cursor to the desired position. Check that the line is valid.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2880 curwin->w_cursor = new_curpos;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2881 check_cursor_lnum();
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2882
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2883 curhead->uh_entry = newlist;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2884 curhead->uh_flags = new_flags;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2885 if ((old_flags & UH_EMPTYBUF) && BUFEMPTY())
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2886 curbuf->b_ml.ml_flags |= ML_EMPTY;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2887 if (old_flags & UH_CHANGED)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2888 changed();
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2889 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2890 #ifdef FEAT_NETBEANS_INTG
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2891 // per netbeans undo rules, keep it as modified
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2892 if (!isNetbeansModified(curbuf))
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2893 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2894 unchanged(curbuf, FALSE, TRUE);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2895
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2896 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2897 * restore marks from before undo/redo
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2898 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2899 for (i = 0; i < NMARKS; ++i)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2900 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2901 if (curhead->uh_namedm[i].lnum != 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2902 curbuf->b_namedm[i] = curhead->uh_namedm[i];
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2903 if (namedm[i].lnum != 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2904 curhead->uh_namedm[i] = namedm[i];
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2905 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2906 curhead->uh_namedm[i].lnum = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2907 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2908 if (curhead->uh_visual.vi_start.lnum != 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2909 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2910 curbuf->b_visual = curhead->uh_visual;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2911 curhead->uh_visual = visualinfo;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2912 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2913
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2914 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2915 * If the cursor is only off by one line, put it at the same position as
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2916 * before starting the change (for the "o" command).
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2917 * Otherwise the cursor should go to the first undone line.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2918 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2919 if (curhead->uh_cursor.lnum + 1 == curwin->w_cursor.lnum
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2920 && curwin->w_cursor.lnum > 1)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2921 --curwin->w_cursor.lnum;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2922 if (curwin->w_cursor.lnum <= curbuf->b_ml.ml_line_count)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2923 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2924 if (curhead->uh_cursor.lnum == curwin->w_cursor.lnum)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2925 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2926 curwin->w_cursor.col = curhead->uh_cursor.col;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2927 if (virtual_active() && curhead->uh_cursor_vcol >= 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2928 coladvance((colnr_T)curhead->uh_cursor_vcol);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2929 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2930 curwin->w_cursor.coladd = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2931 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2932 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2933 beginline(BL_SOL | BL_FIX);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2934 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2935 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2936 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2937 // We get here with the current cursor line being past the end (eg
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2938 // after adding lines at the end of the file, and then undoing it).
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2939 // check_cursor() will move the cursor to the last line. Move it to
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2940 // the first column here.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2941 curwin->w_cursor.col = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2942 curwin->w_cursor.coladd = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2943 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2944
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2945 // Make sure the cursor is on an existing line and column.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2946 check_cursor();
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2947
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2948 // Remember where we are for "g-" and ":earlier 10s".
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2949 curbuf->b_u_seq_cur = curhead->uh_seq;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2950 if (undo)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2951 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2952 // We are below the previous undo. However, to make ":earlier 1s"
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2953 // work we compute this as being just above the just undone change.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2954 if (curhead->uh_next.ptr != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2955 curbuf->b_u_seq_cur = curhead->uh_next.ptr->uh_seq;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2956 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2957 curbuf->b_u_seq_cur = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2958 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2959
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2960 // Remember where we are for ":earlier 1f" and ":later 1f".
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2961 if (curhead->uh_save_nr != 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2962 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2963 if (undo)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2964 curbuf->b_u_save_nr_cur = curhead->uh_save_nr - 1;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2965 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2966 curbuf->b_u_save_nr_cur = curhead->uh_save_nr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2967 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2968
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2969 // The timestamp can be the same for multiple changes, just use the one of
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2970 // the undone/redone change.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2971 curbuf->b_u_time_cur = curhead->uh_time;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2972
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2973 unblock_autocmds();
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2974 #ifdef U_DEBUG
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2975 u_check(FALSE);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2976 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2977 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2978
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2979 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2980 * If we deleted or added lines, report the number of less/more lines.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2981 * Otherwise, report the number of changes (this may be incorrect
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2982 * in some cases, but it's better than nothing).
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2983 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2984 static void
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2985 u_undo_end(
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2986 int did_undo, // just did an undo
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2987 int absolute) // used ":undo N"
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2988 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2989 char *msgstr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2990 u_header_T *uhp;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2991 char_u msgbuf[80];
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2992
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2993 #ifdef FEAT_FOLDING
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2994 if ((fdo_flags & FDO_UNDO) && KeyTyped)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2995 foldOpenCursor();
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2996 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2997
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2998 if (global_busy // no messages now, wait until global is finished
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
2999 || !messaging()) // 'lazyredraw' set, don't do messages now
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3000 return;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3001
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3002 if (curbuf->b_ml.ml_flags & ML_EMPTY)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3003 --u_newcount;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3004
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3005 u_oldcount -= u_newcount;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3006 if (u_oldcount == -1)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3007 msgstr = N_("more line");
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3008 else if (u_oldcount < 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3009 msgstr = N_("more lines");
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3010 else if (u_oldcount == 1)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3011 msgstr = N_("line less");
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3012 else if (u_oldcount > 1)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3013 msgstr = N_("fewer lines");
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3014 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3015 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3016 u_oldcount = u_newcount;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3017 if (u_newcount == 1)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3018 msgstr = N_("change");
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3019 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3020 msgstr = N_("changes");
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3021 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3022
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3023 if (curbuf->b_u_curhead != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3024 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3025 // For ":undo N" we prefer a "after #N" message.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3026 if (absolute && curbuf->b_u_curhead->uh_next.ptr != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3027 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3028 uhp = curbuf->b_u_curhead->uh_next.ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3029 did_undo = FALSE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3030 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3031 else if (did_undo)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3032 uhp = curbuf->b_u_curhead;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3033 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3034 uhp = curbuf->b_u_curhead->uh_next.ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3035 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3036 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3037 uhp = curbuf->b_u_newhead;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3038
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3039 if (uhp == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3040 *msgbuf = NUL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3041 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3042 add_time(msgbuf, sizeof(msgbuf), uhp->uh_time);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3043
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3044 #ifdef FEAT_CONCEAL
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3045 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3046 win_T *wp;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3047
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3048 FOR_ALL_WINDOWS(wp)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3049 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3050 if (wp->w_buffer == curbuf && wp->w_p_cole > 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3051 redraw_win_later(wp, UPD_NOT_VALID);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3052 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3053 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3054 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3055 if (VIsual_active)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3056 check_pos(curbuf, &VIsual);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3057
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3058 smsg_attr_keep(0, _("%ld %s; %s #%ld %s"),
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3059 u_oldcount < 0 ? -u_oldcount : u_oldcount,
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3060 _(msgstr),
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3061 did_undo ? _("before") : _("after"),
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3062 uhp == NULL ? 0L : uhp->uh_seq,
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3063 msgbuf);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3064 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3065
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3066 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3067 * u_sync: stop adding to the current entry list
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3068 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3069 void
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3070 u_sync(
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3071 int force) // Also sync when no_u_sync is set.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3072 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3073 // Skip it when already synced or syncing is disabled.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3074 if (curbuf->b_u_synced || (!force && no_u_sync > 0))
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3075 return;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3076 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3077 if (p_imst == IM_ON_THE_SPOT && im_is_preediting())
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3078 return; // XIM is busy, don't break an undo sequence
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3079 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3080 if (get_undolevel() < 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3081 curbuf->b_u_synced = TRUE; // no entries, nothing to do
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3082 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3083 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3084 u_getbot(); // compute ue_bot of previous u_save
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3085 curbuf->b_u_curhead = NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3086 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3087 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3088
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3089 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3090 * ":undolist": List the leafs of the undo tree
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3091 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3092 void
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3093 ex_undolist(exarg_T *eap UNUSED)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3094 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3095 garray_T ga;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3096 u_header_T *uhp;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3097 int mark;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3098 int nomark;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3099 int changes = 1;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3100 int i;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3101
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3102 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3103 * 1: walk the tree to find all leafs, put the info in "ga".
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3104 * 2: sort the lines
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3105 * 3: display the list
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3106 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3107 mark = ++lastmark;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3108 nomark = ++lastmark;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3109 ga_init2(&ga, sizeof(char *), 20);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3110
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3111 uhp = curbuf->b_u_oldhead;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3112 while (uhp != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3113 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3114 if (uhp->uh_prev.ptr == NULL && uhp->uh_walk != nomark
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3115 && uhp->uh_walk != mark)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3116 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3117 if (ga_grow(&ga, 1) == FAIL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3118 break;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3119 vim_snprintf((char *)IObuff, IOSIZE, "%6ld %7d ",
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3120 uhp->uh_seq, changes);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3121 add_time(IObuff + STRLEN(IObuff), IOSIZE - STRLEN(IObuff),
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3122 uhp->uh_time);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3123 if (uhp->uh_save_nr > 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3124 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3125 while (STRLEN(IObuff) < 33)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3126 STRCAT(IObuff, " ");
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3127 vim_snprintf_add((char *)IObuff, IOSIZE,
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3128 " %3ld", uhp->uh_save_nr);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3129 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3130 ((char_u **)(ga.ga_data))[ga.ga_len++] = vim_strsave(IObuff);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3131 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3132
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3133 uhp->uh_walk = mark;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3134
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3135 // go down in the tree if we haven't been there
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3136 if (uhp->uh_prev.ptr != NULL && uhp->uh_prev.ptr->uh_walk != nomark
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3137 && uhp->uh_prev.ptr->uh_walk != mark)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3138 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3139 uhp = uhp->uh_prev.ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3140 ++changes;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3141 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3142
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3143 // go to alternate branch if we haven't been there
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3144 else if (uhp->uh_alt_next.ptr != NULL
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3145 && uhp->uh_alt_next.ptr->uh_walk != nomark
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3146 && uhp->uh_alt_next.ptr->uh_walk != mark)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3147 uhp = uhp->uh_alt_next.ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3148
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3149 // go up in the tree if we haven't been there and we are at the
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3150 // start of alternate branches
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3151 else if (uhp->uh_next.ptr != NULL && uhp->uh_alt_prev.ptr == NULL
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3152 && uhp->uh_next.ptr->uh_walk != nomark
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3153 && uhp->uh_next.ptr->uh_walk != mark)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3154 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3155 uhp = uhp->uh_next.ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3156 --changes;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3157 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3158
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3159 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3160 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3161 // need to backtrack; mark this node as done
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3162 uhp->uh_walk = nomark;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3163 if (uhp->uh_alt_prev.ptr != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3164 uhp = uhp->uh_alt_prev.ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3165 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3166 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3167 uhp = uhp->uh_next.ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3168 --changes;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3169 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3170 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3171 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3172
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3173 if (ga.ga_len == 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3174 msg(_("Nothing to undo"));
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3175 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3176 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3177 sort_strings((char_u **)ga.ga_data, ga.ga_len);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3178
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3179 msg_start();
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3180 msg_puts_attr(_("number changes when saved"),
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3181 HL_ATTR(HLF_T));
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3182 for (i = 0; i < ga.ga_len && !got_int; ++i)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3183 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3184 msg_putchar('\n');
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3185 if (got_int)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3186 break;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3187 msg_puts(((char **)ga.ga_data)[i]);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3188 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3189 msg_end();
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3190
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3191 ga_clear_strings(&ga);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3192 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3193 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3194
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3195 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3196 * ":undojoin": continue adding to the last entry list
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3197 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3198 void
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3199 ex_undojoin(exarg_T *eap UNUSED)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3200 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3201 if (curbuf->b_u_newhead == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3202 return; // nothing changed before
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3203 if (curbuf->b_u_curhead != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3204 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3205 emsg(_(e_undojoin_is_not_allowed_after_undo));
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3206 return;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3207 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3208 if (!curbuf->b_u_synced)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3209 return; // already unsynced
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3210 if (get_undolevel() < 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3211 return; // no entries, nothing to do
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3212 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3213 // Append next change to the last entry
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3214 curbuf->b_u_synced = FALSE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3215 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3216
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3217 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3218 * Called after writing or reloading the file and setting b_changed to FALSE.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3219 * Now an undo means that the buffer is modified.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3220 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3221 void
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3222 u_unchanged(buf_T *buf)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3223 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3224 u_unch_branch(buf->b_u_oldhead);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3225 buf->b_did_warn = FALSE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3226 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3227
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3228 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3229 * After reloading a buffer which was saved for 'undoreload': Find the first
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3230 * line that was changed and set the cursor there.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3231 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3232 void
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3233 u_find_first_changed(void)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3234 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3235 u_header_T *uhp = curbuf->b_u_newhead;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3236 u_entry_T *uep;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3237 linenr_T lnum;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3238
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3239 if (curbuf->b_u_curhead != NULL || uhp == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3240 return; // undid something in an autocmd?
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3241
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3242 // Check that the last undo block was for the whole file.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3243 uep = uhp->uh_entry;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3244 if (uep->ue_top != 0 || uep->ue_bot != 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3245 return;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3246
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3247 for (lnum = 1; lnum < curbuf->b_ml.ml_line_count
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3248 && lnum <= uep->ue_size; ++lnum)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3249 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3250 char_u *p = ml_get_buf(curbuf, lnum, FALSE);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3251
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3252 if (uep->ue_array[lnum - 1].ul_len != curbuf->b_ml.ml_line_len
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3253 || memcmp(p, uep->ue_array[lnum - 1].ul_line, uep->ue_array[lnum - 1].ul_len) != 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3254 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3255 CLEAR_POS(&(uhp->uh_cursor));
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3256 uhp->uh_cursor.lnum = lnum;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3257 return;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3258 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3259 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3260 if (curbuf->b_ml.ml_line_count != uep->ue_size)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3261 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3262 // lines added or deleted at the end, put the cursor there
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3263 CLEAR_POS(&(uhp->uh_cursor));
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3264 uhp->uh_cursor.lnum = lnum;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3265 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3266 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3267
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3268 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3269 * Increase the write count, store it in the last undo header, what would be
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3270 * used for "u".
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3271 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3272 void
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3273 u_update_save_nr(buf_T *buf)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3274 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3275 u_header_T *uhp;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3276
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3277 ++buf->b_u_save_nr_last;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3278 buf->b_u_save_nr_cur = buf->b_u_save_nr_last;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3279 uhp = buf->b_u_curhead;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3280 if (uhp != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3281 uhp = uhp->uh_next.ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3282 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3283 uhp = buf->b_u_newhead;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3284 if (uhp != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3285 uhp->uh_save_nr = buf->b_u_save_nr_last;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3286 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3287
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3288 static void
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3289 u_unch_branch(u_header_T *uhp)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3290 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3291 u_header_T *uh;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3292
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3293 for (uh = uhp; uh != NULL; uh = uh->uh_prev.ptr)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3294 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3295 uh->uh_flags |= UH_CHANGED;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3296 if (uh->uh_alt_next.ptr != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3297 u_unch_branch(uh->uh_alt_next.ptr); // recursive
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3298 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3299 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3300
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3301 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3302 * Get pointer to last added entry.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3303 * If it's not valid, give an error message and return NULL.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3304 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3305 static u_entry_T *
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3306 u_get_headentry(void)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3307 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3308 if (curbuf->b_u_newhead == NULL || curbuf->b_u_newhead->uh_entry == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3309 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3310 iemsg(e_undo_list_corrupt);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3311 return NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3312 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3313 return curbuf->b_u_newhead->uh_entry;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3314 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3315
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3316 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3317 * u_getbot(): compute the line number of the previous u_save
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3318 * It is called only when b_u_synced is FALSE.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3319 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3320 static void
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3321 u_getbot(void)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3322 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3323 u_entry_T *uep;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3324 linenr_T extra;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3325
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3326 uep = u_get_headentry(); // check for corrupt undo list
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3327 if (uep == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3328 return;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3329
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3330 uep = curbuf->b_u_newhead->uh_getbot_entry;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3331 if (uep != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3332 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3333 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3334 * the new ue_bot is computed from the number of lines that has been
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3335 * inserted (0 - deleted) since calling u_save. This is equal to the
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3336 * old line count subtracted from the current line count.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3337 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3338 extra = curbuf->b_ml.ml_line_count - uep->ue_lcount;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3339 uep->ue_bot = uep->ue_top + uep->ue_size + 1 + extra;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3340 if (uep->ue_bot < 1 || uep->ue_bot > curbuf->b_ml.ml_line_count)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3341 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3342 iemsg(e_undo_line_missing);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3343 uep->ue_bot = uep->ue_top + 1; // assume all lines deleted, will
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3344 // get all the old lines back
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3345 // without deleting the current
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3346 // ones
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3347 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3348
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3349 curbuf->b_u_newhead->uh_getbot_entry = NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3350 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3351
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3352 curbuf->b_u_synced = TRUE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3353 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3354
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3355 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3356 * Free one header "uhp" and its entry list and adjust the pointers.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3357 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3358 static void
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3359 u_freeheader(
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3360 buf_T *buf,
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3361 u_header_T *uhp,
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3362 u_header_T **uhpp) // if not NULL reset when freeing this header
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3363 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3364 u_header_T *uhap;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3365
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3366 // When there is an alternate redo list free that branch completely,
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3367 // because we can never go there.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3368 if (uhp->uh_alt_next.ptr != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3369 u_freebranch(buf, uhp->uh_alt_next.ptr, uhpp);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3370
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3371 if (uhp->uh_alt_prev.ptr != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3372 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3373
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3374 // Update the links in the list to remove the header.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3375 if (uhp->uh_next.ptr == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3376 buf->b_u_oldhead = uhp->uh_prev.ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3377 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3378 uhp->uh_next.ptr->uh_prev.ptr = uhp->uh_prev.ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3379
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3380 if (uhp->uh_prev.ptr == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3381 buf->b_u_newhead = uhp->uh_next.ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3382 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3383 for (uhap = uhp->uh_prev.ptr; uhap != NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3384 uhap = uhap->uh_alt_next.ptr)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3385 uhap->uh_next.ptr = uhp->uh_next.ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3386
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3387 u_freeentries(buf, uhp, uhpp);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3388 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3389
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3390 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3391 * Free an alternate branch and any following alternate branches.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3392 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3393 static void
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3394 u_freebranch(
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3395 buf_T *buf,
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3396 u_header_T *uhp,
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3397 u_header_T **uhpp) // if not NULL reset when freeing this header
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3398 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3399 u_header_T *tofree, *next;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3400
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3401 // If this is the top branch we may need to use u_freeheader() to update
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3402 // all the pointers.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3403 if (uhp == buf->b_u_oldhead)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3404 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3405 while (buf->b_u_oldhead != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3406 u_freeheader(buf, buf->b_u_oldhead, uhpp);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3407 return;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3408 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3409
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3410 if (uhp->uh_alt_prev.ptr != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3411 uhp->uh_alt_prev.ptr->uh_alt_next.ptr = NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3412
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3413 next = uhp;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3414 while (next != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3415 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3416 tofree = next;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3417 if (tofree->uh_alt_next.ptr != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3418 u_freebranch(buf, tofree->uh_alt_next.ptr, uhpp); // recursive
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3419 next = tofree->uh_prev.ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3420 u_freeentries(buf, tofree, uhpp);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3421 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3422 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3423
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3424 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3425 * Free all the undo entries for one header and the header itself.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3426 * This means that "uhp" is invalid when returning.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3427 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3428 static void
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3429 u_freeentries(
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3430 buf_T *buf,
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3431 u_header_T *uhp,
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3432 u_header_T **uhpp) // if not NULL reset when freeing this header
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3433 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3434 u_entry_T *uep, *nuep;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3435
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3436 // Check for pointers to the header that become invalid now.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3437 if (buf->b_u_curhead == uhp)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3438 buf->b_u_curhead = NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3439 if (buf->b_u_newhead == uhp)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3440 buf->b_u_newhead = NULL; // freeing the newest entry
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3441 if (uhpp != NULL && uhp == *uhpp)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3442 *uhpp = NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3443
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3444 for (uep = uhp->uh_entry; uep != NULL; uep = nuep)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3445 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3446 nuep = uep->ue_next;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3447 u_freeentry(uep, uep->ue_size);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3448 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3449
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3450 #ifdef U_DEBUG
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3451 uhp->uh_magic = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3452 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3453 vim_free((char_u *)uhp);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3454 --buf->b_u_numhead;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3455 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3456
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3457 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3458 * free entry 'uep' and 'n' lines in uep->ue_array[]
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3459 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3460 static void
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3461 u_freeentry(u_entry_T *uep, long n)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3462 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3463 while (n > 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3464 vim_free(uep->ue_array[--n].ul_line);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3465 vim_free((char_u *)uep->ue_array);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3466 #ifdef U_DEBUG
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3467 uep->ue_magic = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3468 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3469 vim_free((char_u *)uep);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3470 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3471
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3472 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3473 * invalidate the undo buffer; called when storage has already been released
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3474 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3475 void
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3476 u_clearall(buf_T *buf)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3477 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3478 buf->b_u_newhead = buf->b_u_oldhead = buf->b_u_curhead = NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3479 buf->b_u_synced = TRUE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3480 buf->b_u_numhead = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3481 buf->b_u_line_ptr.ul_line = NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3482 buf->b_u_line_ptr.ul_len = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3483 buf->b_u_line_lnum = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3484 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3485
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3486 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3487 * Save the line "lnum" for the "U" command.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3488 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3489 static void
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3490 u_saveline(linenr_T lnum)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3491 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3492 if (lnum == curbuf->b_u_line_lnum) // line is already saved
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3493 return;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3494 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) // should never happen
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3495 return;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3496 u_clearline();
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3497 curbuf->b_u_line_lnum = lnum;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3498 if (curwin->w_cursor.lnum == lnum)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3499 curbuf->b_u_line_colnr = curwin->w_cursor.col;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3500 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3501 curbuf->b_u_line_colnr = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3502 if (u_save_line(&curbuf->b_u_line_ptr, lnum) == FAIL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3503 do_outofmem_msg((long_u)0);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3504 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3505
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3506 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3507 * clear the line saved for the "U" command
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3508 * (this is used externally for crossing a line while in insert mode)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3509 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3510 void
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3511 u_clearline(void)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3512 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3513 if (curbuf->b_u_line_ptr.ul_line == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3514 return;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3515
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3516 VIM_CLEAR(curbuf->b_u_line_ptr.ul_line);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3517 curbuf->b_u_line_ptr.ul_len = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3518 curbuf->b_u_line_lnum = 0;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3519 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3520
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3521 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3522 * Implementation of the "U" command.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3523 * Differentiation from vi: "U" can be undone with the next "U".
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3524 * We also allow the cursor to be in another line.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3525 * Careful: may trigger autocommands that reload the buffer.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3526 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3527 void
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3528 u_undoline(void)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3529 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3530 colnr_T t;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3531 undoline_T oldp;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3532
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3533 if (undo_off)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3534 return;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3535
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3536 if (curbuf->b_u_line_ptr.ul_line == NULL
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3537 || curbuf->b_u_line_lnum > curbuf->b_ml.ml_line_count)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3538 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3539 beep_flush();
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3540 return;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3541 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3542
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3543 // first save the line for the 'u' command
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3544 if (u_savecommon(curbuf->b_u_line_lnum - 1,
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3545 curbuf->b_u_line_lnum + 1, (linenr_T)0, FALSE) == FAIL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3546 return;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3547 if (u_save_line(&oldp, curbuf->b_u_line_lnum) == FAIL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3548 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3549 do_outofmem_msg((long_u)0);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3550 return;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3551 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3552 ml_replace_len(curbuf->b_u_line_lnum, curbuf->b_u_line_ptr.ul_line,
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3553 curbuf->b_u_line_ptr.ul_len, TRUE, FALSE);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3554 changed_bytes(curbuf->b_u_line_lnum, 0);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3555 curbuf->b_u_line_ptr = oldp;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3556
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3557 t = curbuf->b_u_line_colnr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3558 if (curwin->w_cursor.lnum == curbuf->b_u_line_lnum)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3559 curbuf->b_u_line_colnr = curwin->w_cursor.col;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3560 curwin->w_cursor.col = t;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3561 curwin->w_cursor.lnum = curbuf->b_u_line_lnum;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3562 check_cursor_col();
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3563 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3564
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3565 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3566 * Free all allocated memory blocks for the buffer 'buf'.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3567 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3568 void
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3569 u_blockfree(buf_T *buf)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3570 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3571 while (buf->b_u_oldhead != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3572 u_freeheader(buf, buf->b_u_oldhead, NULL);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3573 vim_free(buf->b_u_line_ptr.ul_line);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3574 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3575
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3576 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3577 * Check if the 'modified' flag is set, or 'ff' has changed (only need to
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3578 * check the first character, because it can only be "dos", "unix" or "mac").
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3579 * "nofile" and "scratch" type buffers are considered to always be unchanged.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3580 * Also considers a buffer changed when a terminal window contains a running
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3581 * job.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3582 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3583 int
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3584 bufIsChanged(buf_T *buf)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3585 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3586 #ifdef FEAT_TERMINAL
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3587 if (term_job_running_not_none(buf->b_term))
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3588 return TRUE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3589 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3590 return bufIsChangedNotTerm(buf);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3591 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3592
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3593 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3594 * Return TRUE if any buffer has changes. Also buffers that are not written.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3595 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3596 int
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3597 anyBufIsChanged(void)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3598 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3599 buf_T *buf;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3600
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3601 FOR_ALL_BUFFERS(buf)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3602 if (bufIsChanged(buf))
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3603 return TRUE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3604 return FALSE;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3605 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3606
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3607 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3608 * Like bufIsChanged() but ignoring a terminal window.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3609 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3610 int
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3611 bufIsChangedNotTerm(buf_T *buf)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3612 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3613 // In a "prompt" buffer we do respect 'modified', so that we can control
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3614 // closing the window by setting or resetting that option.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3615 return (!bt_dontwrite(buf) || bt_prompt(buf))
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3616 && (buf->b_changed || file_ff_differs(buf, TRUE));
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3617 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3618
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3619 int
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3620 curbufIsChanged(void)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3621 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3622 return bufIsChanged(curbuf);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3623 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3624
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3625 #if defined(FEAT_EVAL) || defined(PROTO)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3626
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3627 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3628 * For undotree(): Append the list of undo blocks at "first_uhp" to "list".
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3629 * Recursive.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3630 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3631 static void
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3632 u_eval_tree(u_header_T *first_uhp, list_T *list)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3633 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3634 u_header_T *uhp = first_uhp;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3635 dict_T *dict;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3636
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3637 while (uhp != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3638 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3639 dict = dict_alloc();
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3640 if (dict == NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3641 return;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3642 dict_add_number(dict, "seq", uhp->uh_seq);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3643 dict_add_number(dict, "time", (long)uhp->uh_time);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3644 if (uhp == curbuf->b_u_newhead)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3645 dict_add_number(dict, "newhead", 1);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3646 if (uhp == curbuf->b_u_curhead)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3647 dict_add_number(dict, "curhead", 1);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3648 if (uhp->uh_save_nr > 0)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3649 dict_add_number(dict, "save", uhp->uh_save_nr);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3650
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3651 if (uhp->uh_alt_next.ptr != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3652 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3653 list_T *alt_list = list_alloc();
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3654
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3655 if (alt_list != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3656 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3657 // Recursive call to add alternate undo tree.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3658 u_eval_tree(uhp->uh_alt_next.ptr, alt_list);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3659 dict_add_list(dict, "alt", alt_list);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3660 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3661 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3662
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3663 list_append_dict(list, dict);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3664 uhp = uhp->uh_prev.ptr;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3665 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3666 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3667
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3668 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3669 * "undofile(name)" function
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3670 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3671 void
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3672 f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3673 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3674 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3675 return;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3676
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3677 rettv->v_type = VAR_STRING;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3678 #ifdef FEAT_PERSISTENT_UNDO
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3679 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3680 char_u *fname = tv_get_string(&argvars[0]);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3681
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3682 if (*fname == NUL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3683 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3684 // If there is no file name there will be no undo file.
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3685 rettv->vval.v_string = NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3686 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3687 else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3688 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3689 char_u *ffname = FullName_save(fname, TRUE);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3690
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3691 if (ffname != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3692 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3693 vim_free(ffname);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3694 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3695 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3696 #else
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3697 rettv->vval.v_string = NULL;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3698 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3699 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3700 #ifdef FEAT_PERSISTENT_UNDO
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3701 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3702 * Reset undofile option and delete the undofile
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3703 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3704 void
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3705 u_undofile_reset_and_delete(buf_T *buf)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3706 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3707 char_u *file_name;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3708
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3709 if (!buf->b_p_udf)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3710 return;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3711
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3712 file_name = u_get_undo_file_name(buf->b_ffname, TRUE);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3713 if (file_name != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3714 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3715 mch_remove(file_name);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3716 vim_free(file_name);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3717 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3718
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3719 set_option_value_give_err((char_u *)"undofile", 0L, NULL, OPT_LOCAL);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3720 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3721 #endif
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3722
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3723 /*
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3724 * "undotree()" function
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3725 */
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3726 void
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3727 f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3728 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3729 if (rettv_dict_alloc(rettv) == FAIL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3730 return;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3731
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3732 dict_T *dict = rettv->vval.v_dict;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3733 list_T *list;
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3734
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3735 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3736 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3737 dict_add_number(dict, "save_last", curbuf->b_u_save_nr_last);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3738 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3739 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3740 dict_add_number(dict, "save_cur", curbuf->b_u_save_nr_cur);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3741
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3742 list = list_alloc();
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3743 if (list != NULL)
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3744 {
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3745 u_eval_tree(curbuf->b_u_oldhead, list);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3746 dict_add_list(dict, "entries", list);
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3747 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3748 }
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3749
448aef880252 normalize line endings
Christian Brabandt <cb@256bit.org>
parents: 32525
diff changeset
3750 #endif