annotate src/undo.c @ 318:1c408f85789e

updated for version 7.0083
author vimboss
date Wed, 08 Jun 2005 22:00:03 +0000
parents 7fd70926e2e1
children 7033303ea0c0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1 /* vi:set ts=8 sts=4 sw=4:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3 * VIM - Vi IMproved by Bram Moolenaar
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
4 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
5 * Do ":help uganda" in Vim to read copying and usage conditions.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
6 * Do ":help credits" in Vim to see a list of people who contributed.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
7 * See README.txt for an overview of the Vim source code.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
8 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
9
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
10 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
11 * undo.c: multi level undo facility
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
12 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
13 * The saved lines are stored in a list of lists (one for each buffer):
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
14 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
15 * b_u_oldhead------------------------------------------------+
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
16 * |
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
17 * V
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
18 * +--------------+ +--------------+ +--------------+
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
19 * b_u_newhead--->| u_header | | u_header | | u_header |
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
20 * | uh_next------>| uh_next------>| uh_next---->NULL
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
21 * NULL<--------uh_prev |<---------uh_prev |<---------uh_prev |
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
22 * | uh_entry | | uh_entry | | uh_entry |
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
23 * +--------|-----+ +--------|-----+ +--------|-----+
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
24 * | | |
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
25 * V V V
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
26 * +--------------+ +--------------+ +--------------+
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
27 * | u_entry | | u_entry | | u_entry |
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
28 * | ue_next | | ue_next | | ue_next |
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
29 * +--------|-----+ +--------|-----+ +--------|-----+
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
30 * | | |
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
31 * V V V
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
32 * +--------------+ NULL NULL
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
33 * | u_entry |
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
34 * | ue_next |
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
35 * +--------|-----+
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
36 * |
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
37 * V
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
38 * etc.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
39 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
40 * Each u_entry list contains the information for one undo or redo.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
41 * curbuf->b_u_curhead points to the header of the last undo (the next redo),
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
42 * or is NULL if nothing has been undone.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
43 *
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
44 * All data is allocated with U_ALLOC_LINE(), it will be freed as soon as the
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
45 * buffer is unloaded.
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
46 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
47
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
48 #include "vim.h"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
49
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
50 /* See below: use malloc()/free() for memory management. */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
51 #define U_USE_MALLOC 1
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
52
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
53 static u_entry_T *u_get_headentry __ARGS((void));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
54 static void u_getbot __ARGS((void));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
55 static int u_savecommon __ARGS((linenr_T, linenr_T, linenr_T));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
56 static void u_doit __ARGS((int count));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
57 static void u_undoredo __ARGS((void));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
58 static void u_undo_end __ARGS((void));
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
59 static void u_freelist __ARGS((buf_T *buf, struct u_header *));
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
60 static void u_freeentry __ARGS((u_entry_T *, long));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
61
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
62 #ifdef U_USE_MALLOC
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
63 # define U_FREE_LINE(ptr) vim_free(ptr)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
64 # define U_ALLOC_LINE(size) lalloc((long_u)((size) + 1), FALSE)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
65 #else
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
66 static void u_free_line __ARGS((char_u *ptr, int keep));
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
67 static char_u *u_alloc_line __ARGS((unsigned size));
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
68 # define U_FREE_LINE(ptr) u_free_line((ptr), FALSE)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
69 # define U_ALLOC_LINE(size) u_alloc_line(size)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
70 #endif
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
71 static char_u *u_save_line __ARGS((linenr_T));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
72
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
73 static long u_newcount, u_oldcount;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
74
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
75 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
76 * When 'u' flag included in 'cpoptions', we behave like vi. Need to remember
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
77 * the action that "u" should do.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
78 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
79 static int undo_undoes = FALSE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
80
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
81 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
82 * save the current line for both the "u" and "U" command
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
83 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
84 int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
85 u_save_cursor()
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
86 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
87 return (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
88 (linenr_T)(curwin->w_cursor.lnum + 1)));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
89 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
90
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
91 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
92 * Save the lines between "top" and "bot" for both the "u" and "U" command.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
93 * "top" may be 0 and bot may be curbuf->b_ml.ml_line_count + 1.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
94 * Returns FAIL when lines could not be saved, OK otherwise.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
95 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
96 int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
97 u_save(top, bot)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
98 linenr_T top, bot;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
99 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
100 if (undo_off)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
101 return OK;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
102
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
103 if (top > curbuf->b_ml.ml_line_count ||
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
104 top >= bot || bot > curbuf->b_ml.ml_line_count + 1)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
105 return FALSE; /* rely on caller to do error messages */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
106
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
107 if (top + 2 == bot)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
108 u_saveline((linenr_T)(top + 1));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
109
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
110 return (u_savecommon(top, bot, (linenr_T)0));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
111 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
112
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
113 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
114 * save the line "lnum" (used by ":s" and "~" command)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
115 * The line is replaced, so the new bottom line is lnum + 1.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
116 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
117 int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
118 u_savesub(lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
119 linenr_T lnum;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
120 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
121 if (undo_off)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
122 return OK;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
123
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
124 return (u_savecommon(lnum - 1, lnum + 1, lnum + 1));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
125 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
126
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
127 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
128 * a new line is inserted before line "lnum" (used by :s command)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
129 * The line is inserted, so the new bottom line is lnum + 1.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
130 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
131 int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
132 u_inssub(lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
133 linenr_T lnum;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
134 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
135 if (undo_off)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
136 return OK;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
137
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
138 return (u_savecommon(lnum - 1, lnum, lnum + 1));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
139 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
140
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
141 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
142 * save the lines "lnum" - "lnum" + nlines (used by delete command)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
143 * The lines are deleted, so the new bottom line is lnum, unless the buffer
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
144 * becomes empty.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
145 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
146 int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
147 u_savedel(lnum, nlines)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
148 linenr_T lnum;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
149 long nlines;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
150 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
151 if (undo_off)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
152 return OK;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
153
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
154 return (u_savecommon(lnum - 1, lnum + nlines,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
155 nlines == curbuf->b_ml.ml_line_count ? 2 : lnum));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
156 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
157
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
158 static int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
159 u_savecommon(top, bot, newbot)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
160 linenr_T top, bot;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
161 linenr_T newbot;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
162 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
163 linenr_T lnum;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
164 long i;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
165 struct u_header *uhp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
166 u_entry_T *uep;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
167 u_entry_T *prev_uep;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
168 long size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
169
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
170 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
171 * Don't allow changes when 'modifiable' is off. Letting the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
172 * undo fail is a crude way to make all change commands fail.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
173 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
174 if (!curbuf->b_p_ma)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
175 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
176 EMSG(_(e_modifiable));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
177 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
178 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
179
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
180 #ifdef HAVE_SANDBOX
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
181 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
182 * In the sandbox it's not allowed to change the text. Letting the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
183 * undo fail is a crude way to make all change commands fail.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
184 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
185 if (sandbox != 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
186 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
187 EMSG(_(e_sandbox));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
188 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
189 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
190 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
191
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
192 #ifdef FEAT_NETBEANS_INTG
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
193 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
194 * Netbeans defines areas that cannot be modified. Bail out here when
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
195 * trying to change text in a guarded area.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
196 */
33
f6033dcbaf31 updated for version 7.0020
vimboss
parents: 7
diff changeset
197 if (usingNetbeans)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
198 {
33
f6033dcbaf31 updated for version 7.0020
vimboss
parents: 7
diff changeset
199 if (netbeans_is_guarded(top, bot))
f6033dcbaf31 updated for version 7.0020
vimboss
parents: 7
diff changeset
200 {
f6033dcbaf31 updated for version 7.0020
vimboss
parents: 7
diff changeset
201 EMSG(_(e_guarded));
f6033dcbaf31 updated for version 7.0020
vimboss
parents: 7
diff changeset
202 return FAIL;
f6033dcbaf31 updated for version 7.0020
vimboss
parents: 7
diff changeset
203 }
f6033dcbaf31 updated for version 7.0020
vimboss
parents: 7
diff changeset
204 if (curbuf->b_p_ro)
f6033dcbaf31 updated for version 7.0020
vimboss
parents: 7
diff changeset
205 {
f6033dcbaf31 updated for version 7.0020
vimboss
parents: 7
diff changeset
206 EMSG(_(e_nbreadonly));
f6033dcbaf31 updated for version 7.0020
vimboss
parents: 7
diff changeset
207 return FAIL;
f6033dcbaf31 updated for version 7.0020
vimboss
parents: 7
diff changeset
208 }
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
209 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
210 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
211
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
212 #ifdef FEAT_AUTOCMD
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
213 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
214 * Saving text for undo means we are going to make a change. Give a
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
215 * warning for a read-only file before making the change, so that the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
216 * FileChangedRO event can replace the buffer with a read-write version
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
217 * (e.g., obtained from a source control system).
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
218 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
219 change_warning(0);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
220 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
221
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
222 size = bot - top - 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
223
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
224 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
225 * if curbuf->b_u_synced == TRUE make a new header
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
226 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
227 if (curbuf->b_u_synced)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
228 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
229 #ifdef FEAT_JUMPLIST
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
230 /* Need to create new entry in b_changelist. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
231 curbuf->b_new_change = TRUE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
232 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
233
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
234 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
235 * if we undid more than we redid, free the entry lists before and
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
236 * including curbuf->b_u_curhead
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
237 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
238 while (curbuf->b_u_curhead != NULL)
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
239 u_freelist(curbuf, curbuf->b_u_newhead);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
240
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
241 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
242 * free headers to keep the size right
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
243 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
244 while (curbuf->b_u_numhead > p_ul && curbuf->b_u_oldhead != NULL)
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
245 u_freelist(curbuf, curbuf->b_u_oldhead);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
246
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
247 if (p_ul < 0) /* no undo at all */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
248 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
249 curbuf->b_u_synced = FALSE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
250 return OK;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
251 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
252
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
253 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
254 * make a new header entry
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
255 */
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
256 uhp = (struct u_header *)U_ALLOC_LINE((unsigned)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
257 sizeof(struct u_header));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
258 if (uhp == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
259 goto nomem;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
260 uhp->uh_prev = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
261 uhp->uh_next = curbuf->b_u_newhead;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
262 if (curbuf->b_u_newhead != NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
263 curbuf->b_u_newhead->uh_prev = uhp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
264 uhp->uh_entry = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
265 uhp->uh_getbot_entry = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
266 uhp->uh_cursor = curwin->w_cursor; /* save cursor pos. for undo */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
267 #ifdef FEAT_VIRTUALEDIT
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
268 if (virtual_active() && curwin->w_cursor.coladd > 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
269 uhp->uh_cursor_vcol = getviscol();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
270 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
271 uhp->uh_cursor_vcol = -1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
272 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
273
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
274 /* save changed and buffer empty flag for undo */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
275 uhp->uh_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
276 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
277
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
278 /* save named marks for undo */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
279 mch_memmove(uhp->uh_namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
280 curbuf->b_u_newhead = uhp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
281 if (curbuf->b_u_oldhead == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
282 curbuf->b_u_oldhead = uhp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
283 ++curbuf->b_u_numhead;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
284 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
285 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
286 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
287 if (p_ul < 0) /* no undo at all */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
288 return OK;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
289
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
290 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
291 * When saving a single line, and it has been saved just before, it
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
292 * doesn't make sense saving it again. Saves a lot of memory when
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
293 * making lots of changes inside the same line.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
294 * This is only possible if the previous change didn't increase or
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
295 * decrease the number of lines.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
296 * Check the ten last changes. More doesn't make sense and takes too
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
297 * long.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
298 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
299 if (size == 1)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
300 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
301 uep = u_get_headentry();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
302 prev_uep = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
303 for (i = 0; i < 10; ++i)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
304 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
305 if (uep == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
306 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
307
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
308 /* If lines have been inserted/deleted we give up.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
309 * Also when the line was included in a multi-line save. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
310 if ((curbuf->b_u_newhead->uh_getbot_entry != uep
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
311 ? (uep->ue_top + uep->ue_size + 1
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
312 != (uep->ue_bot == 0
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
313 ? curbuf->b_ml.ml_line_count + 1
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
314 : uep->ue_bot))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
315 : uep->ue_lcount != curbuf->b_ml.ml_line_count)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
316 || (uep->ue_size > 1
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
317 && top >= uep->ue_top
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
318 && top + 2 <= uep->ue_top + uep->ue_size + 1))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
319 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
320
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
321 /* If it's the same line we can skip saving it again. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
322 if (uep->ue_size == 1 && uep->ue_top == top)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
323 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
324 if (i > 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
325 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
326 /* It's not the last entry: get ue_bot for the last
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
327 * entry now. Following deleted/inserted lines go to
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
328 * the re-used entry. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
329 u_getbot();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
330 curbuf->b_u_synced = FALSE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
331
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
332 /* Move the found entry to become the last entry. The
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
333 * order of undo/redo doesn't matter for the entries
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
334 * we move it over, since they don't change the line
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
335 * count and don't include this line. It does matter
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
336 * for the found entry if the line count is changed by
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
337 * the executed command. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
338 prev_uep->ue_next = uep->ue_next;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
339 uep->ue_next = curbuf->b_u_newhead->uh_entry;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
340 curbuf->b_u_newhead->uh_entry = uep;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
341 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
342
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
343 /* The executed command may change the line count. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
344 if (newbot != 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
345 uep->ue_bot = newbot;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
346 else if (bot > curbuf->b_ml.ml_line_count)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
347 uep->ue_bot = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
348 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
349 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
350 uep->ue_lcount = curbuf->b_ml.ml_line_count;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
351 curbuf->b_u_newhead->uh_getbot_entry = uep;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
352 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
353 return OK;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
354 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
355 prev_uep = uep;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
356 uep = uep->ue_next;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
357 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
358 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
359
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
360 /* find line number for ue_bot for previous u_save() */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
361 u_getbot();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
362 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
363
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
364 #if !defined(UNIX) && !defined(DJGPP) && !defined(WIN32) && !defined(__EMX__)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
365 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
366 * With Amiga and MSDOS 16 bit we can't handle big undo's, because
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
367 * then u_alloc_line would have to allocate a block larger than 32K
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
368 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
369 if (size >= 8000)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
370 goto nomem;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
371 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
372
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
373 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
374 * add lines in front of entry list
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
375 */
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
376 uep = (u_entry_T *)U_ALLOC_LINE((unsigned)sizeof(u_entry_T));
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
377 if (uep == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
378 goto nomem;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
379
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
380 uep->ue_size = size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
381 uep->ue_top = top;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
382 if (newbot != 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
383 uep->ue_bot = newbot;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
384 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
385 * Use 0 for ue_bot if bot is below last line.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
386 * Otherwise we have to compute ue_bot later.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
387 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
388 else if (bot > curbuf->b_ml.ml_line_count)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
389 uep->ue_bot = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
390 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
391 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
392 uep->ue_lcount = curbuf->b_ml.ml_line_count;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
393 curbuf->b_u_newhead->uh_getbot_entry = uep;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
394 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
395
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
396 if (size > 0)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
397 {
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
398 if ((uep->ue_array = (char_u **)U_ALLOC_LINE(
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
399 (unsigned)(sizeof(char_u *) * size))) == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
400 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
401 u_freeentry(uep, 0L);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
402 goto nomem;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
403 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
404 for (i = 0, lnum = top + 1; i < size; ++i)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
405 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
406 if ((uep->ue_array[i] = u_save_line(lnum++)) == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
407 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
408 u_freeentry(uep, i);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
409 goto nomem;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
410 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
411 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
412 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
413 uep->ue_next = curbuf->b_u_newhead->uh_entry;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
414 curbuf->b_u_newhead->uh_entry = uep;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
415 curbuf->b_u_synced = FALSE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
416 undo_undoes = FALSE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
417
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
418 return OK;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
419
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
420 nomem:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
421 msg_silent = 0; /* must display the prompt */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
422 if (ask_yesno((char_u *)_("No undo possible; continue anyway"), TRUE)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
423 == 'y')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
424 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
425 undo_off = TRUE; /* will be reset when character typed */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
426 return OK;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
427 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
428 do_outofmem_msg((long_u)0);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
429 return FAIL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
430 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
431
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
432 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
433 * If 'cpoptions' contains 'u': Undo the previous undo or redo (vi compatible).
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
434 * If 'cpoptions' does not contain 'u': Always undo.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
435 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
436 void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
437 u_undo(count)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
438 int count;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
439 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
440 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
441 * If we get an undo command while executing a macro, we behave like the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
442 * original vi. If this happens twice in one macro the result will not
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
443 * be compatible.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
444 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
445 if (curbuf->b_u_synced == FALSE)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
446 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
447 u_sync();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
448 count = 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
449 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
450
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
451 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
452 undo_undoes = TRUE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
453 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
454 undo_undoes = !undo_undoes;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
455 u_doit(count);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
456 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
457
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
458 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
459 * If 'cpoptions' contains 'u': Repeat the previous undo or redo.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
460 * If 'cpoptions' does not contain 'u': Always redo.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
461 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
462 void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
463 u_redo(count)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
464 int count;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
465 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
466 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
467 undo_undoes = FALSE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
468 u_doit(count);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
469 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
470
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
471 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
472 * Undo or redo, depending on 'undo_undoes', 'count' times.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
473 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
474 static void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
475 u_doit(count)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
476 int count;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
477 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
478 /* Don't allow changes when 'modifiable' is off. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
479 if (!curbuf->b_p_ma)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
480 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
481 EMSG(_(e_modifiable));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
482 return;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
483 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
484 #ifdef HAVE_SANDBOX
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
485 /* In the sandbox it's not allowed to change the text. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
486 if (sandbox != 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
487 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
488 EMSG(_(e_sandbox));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
489 return;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
490 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
491 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
492
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
493 u_newcount = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
494 u_oldcount = 0;
179
7fd70926e2e1 updated for version 7.0055
vimboss
parents: 168
diff changeset
495 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7fd70926e2e1 updated for version 7.0055
vimboss
parents: 168
diff changeset
496 u_oldcount = -1;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
497 while (count--)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
498 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
499 if (undo_undoes)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
500 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
501 if (curbuf->b_u_curhead == NULL) /* first undo */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
502 curbuf->b_u_curhead = curbuf->b_u_newhead;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
503 else if (p_ul > 0) /* multi level undo */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
504 /* get next undo */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
505 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_next;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
506 /* nothing to undo */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
507 if (curbuf->b_u_numhead == 0 || curbuf->b_u_curhead == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
508 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
509 /* stick curbuf->b_u_curhead at end */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
510 curbuf->b_u_curhead = curbuf->b_u_oldhead;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
511 beep_flush();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
512 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
513 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
514
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
515 u_undoredo();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
516 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
517 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
518 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
519 if (curbuf->b_u_curhead == NULL || p_ul <= 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
520 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
521 beep_flush(); /* nothing to redo */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
522 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
523 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
524
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
525 u_undoredo();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
526 /* advance for next redo */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
527 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_prev;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
528 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
529 }
179
7fd70926e2e1 updated for version 7.0055
vimboss
parents: 168
diff changeset
530 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7fd70926e2e1 updated for version 7.0055
vimboss
parents: 168
diff changeset
531 --u_newcount;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
532 u_undo_end();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
533 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
534
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
535 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
536 * u_undoredo: common code for undo and redo
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
537 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
538 * The lines in the file are replaced by the lines in the entry list at
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
539 * curbuf->b_u_curhead. The replaced lines in the file are saved in the entry
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
540 * list for the next undo/redo.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
541 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
542 static void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
543 u_undoredo()
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
544 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
545 char_u **newarray = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
546 linenr_T oldsize;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
547 linenr_T newsize;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
548 linenr_T top, bot;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
549 linenr_T lnum;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
550 linenr_T newlnum = MAXLNUM;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
551 long i;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
552 u_entry_T *uep, *nuep;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
553 u_entry_T *newlist = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
554 int old_flags;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
555 int new_flags;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
556 pos_T namedm[NMARKS];
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
557 int empty_buffer; /* buffer became empty */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
558
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
559 old_flags = curbuf->b_u_curhead->uh_flags;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
560 new_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
561 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
562 setpcmark();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
563
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
564 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
565 * save marks before undo/redo
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
566 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
567 mch_memmove(namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
568 curbuf->b_op_start.lnum = curbuf->b_ml.ml_line_count;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
569 curbuf->b_op_start.col = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
570 curbuf->b_op_end.lnum = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
571 curbuf->b_op_end.col = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
572
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
573 for (uep = curbuf->b_u_curhead->uh_entry; uep != NULL; uep = nuep)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
574 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
575 top = uep->ue_top;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
576 bot = uep->ue_bot;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
577 if (bot == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
578 bot = curbuf->b_ml.ml_line_count + 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
579 if (top > curbuf->b_ml.ml_line_count || top >= bot
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
580 || bot > curbuf->b_ml.ml_line_count + 1)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
581 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
582 EMSG(_("E438: u_undo: line numbers wrong"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
583 changed(); /* don't want UNCHANGED now */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
584 return;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
585 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
586
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
587 oldsize = bot - top - 1; /* number of lines before undo */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
588 newsize = uep->ue_size; /* number of lines after undo */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
589
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
590 if (top < newlnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
591 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
592 /* If the saved cursor is somewhere in this undo block, move it to
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
593 * the remembered position. Makes "gwap" put the cursor back
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
594 * where it was. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
595 lnum = curbuf->b_u_curhead->uh_cursor.lnum;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
596 if (lnum >= top && lnum <= top + newsize + 1)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
597 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
598 curwin->w_cursor = curbuf->b_u_curhead->uh_cursor;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
599 newlnum = curwin->w_cursor.lnum - 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
600 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
601 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
602 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
603 /* Use the first line that actually changed. Avoids that
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
604 * undoing auto-formatting puts the cursor in the previous
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
605 * line. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
606 for (i = 0; i < newsize && i < oldsize; ++i)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
607 if (STRCMP(uep->ue_array[i], ml_get(top + 1 + i)) != 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
608 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
609 if (i == newsize && newlnum == MAXLNUM && uep->ue_next == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
610 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
611 newlnum = top;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
612 curwin->w_cursor.lnum = newlnum + 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
613 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
614 else if (i < newsize)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
615 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
616 newlnum = top + i;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
617 curwin->w_cursor.lnum = newlnum + 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
618 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
619 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
620 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
621
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
622 empty_buffer = FALSE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
623
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
624 /* delete the lines between top and bot and save them in newarray */
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
625 if (oldsize > 0)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
626 {
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
627 if ((newarray = (char_u **)U_ALLOC_LINE(
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
628 (unsigned)(sizeof(char_u *) * oldsize))) == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
629 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
630 do_outofmem_msg((long_u)(sizeof(char_u *) * oldsize));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
631 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
632 * We have messed up the entry list, repair is impossible.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
633 * we have to free the rest of the list.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
634 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
635 while (uep != NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
636 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
637 nuep = uep->ue_next;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
638 u_freeentry(uep, uep->ue_size);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
639 uep = nuep;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
640 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
641 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
642 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
643 /* delete backwards, it goes faster in most cases */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
644 for (lnum = bot - 1, i = oldsize; --i >= 0; --lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
645 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
646 /* what can we do when we run out of memory? */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
647 if ((newarray[i] = u_save_line(lnum)) == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
648 do_outofmem_msg((long_u)0);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
649 /* remember we deleted the last line in the buffer, and a
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
650 * dummy empty line will be inserted */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
651 if (curbuf->b_ml.ml_line_count == 1)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
652 empty_buffer = TRUE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
653 ml_delete(lnum, FALSE);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
654 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
655 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
656
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
657 /* insert the lines in u_array between top and bot */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
658 if (newsize)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
659 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
660 for (lnum = top, i = 0; i < newsize; ++i, ++lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
661 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
662 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
663 * If the file is empty, there is an empty line 1 that we
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
664 * should get rid of, by replacing it with the new line
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
665 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
666 if (empty_buffer && lnum == 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
667 ml_replace((linenr_T)1, uep->ue_array[i], TRUE);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
668 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
669 ml_append(lnum, uep->ue_array[i], (colnr_T)0, FALSE);
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
670 U_FREE_LINE(uep->ue_array[i]);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
671 }
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
672 U_FREE_LINE((char_u *)uep->ue_array);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
673 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
674
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
675 /* adjust marks */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
676 if (oldsize != newsize)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
677 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
678 mark_adjust(top + 1, top + oldsize, (long)MAXLNUM,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
679 (long)newsize - (long)oldsize);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
680 if (curbuf->b_op_start.lnum > top + oldsize)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
681 curbuf->b_op_start.lnum += newsize - oldsize;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
682 if (curbuf->b_op_end.lnum > top + oldsize)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
683 curbuf->b_op_end.lnum += newsize - oldsize;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
684 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
685
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
686 changed_lines(top + 1, 0, bot, newsize - oldsize);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
687
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
688 /* set '[ and '] mark */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
689 if (top + 1 < curbuf->b_op_start.lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
690 curbuf->b_op_start.lnum = top + 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
691 if (newsize == 0 && top + 1 > curbuf->b_op_end.lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
692 curbuf->b_op_end.lnum = top + 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
693 else if (top + newsize > curbuf->b_op_end.lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
694 curbuf->b_op_end.lnum = top + newsize;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
695
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
696 u_newcount += newsize;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
697 u_oldcount += oldsize;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
698 uep->ue_size = oldsize;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
699 uep->ue_array = newarray;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
700 uep->ue_bot = top + newsize + 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
701
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
702 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
703 * insert this entry in front of the new entry list
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
704 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
705 nuep = uep->ue_next;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
706 uep->ue_next = newlist;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
707 newlist = uep;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
708 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
709
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
710 curbuf->b_u_curhead->uh_entry = newlist;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
711 curbuf->b_u_curhead->uh_flags = new_flags;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
712 if ((old_flags & UH_EMPTYBUF) && bufempty())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
713 curbuf->b_ml.ml_flags |= ML_EMPTY;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
714 if (old_flags & UH_CHANGED)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
715 changed();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
716 else
33
f6033dcbaf31 updated for version 7.0020
vimboss
parents: 7
diff changeset
717 #ifdef FEAT_NETBEANS_INTG
f6033dcbaf31 updated for version 7.0020
vimboss
parents: 7
diff changeset
718 /* per netbeans undo rules, keep it as modified */
f6033dcbaf31 updated for version 7.0020
vimboss
parents: 7
diff changeset
719 if (!isNetbeansModified(curbuf))
f6033dcbaf31 updated for version 7.0020
vimboss
parents: 7
diff changeset
720 #endif
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
721 unchanged(curbuf, FALSE);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
722
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
723 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
724 * restore marks from before undo/redo
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
725 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
726 for (i = 0; i < NMARKS; ++i)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
727 if (curbuf->b_u_curhead->uh_namedm[i].lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
728 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
729 curbuf->b_namedm[i] = curbuf->b_u_curhead->uh_namedm[i];
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
730 curbuf->b_u_curhead->uh_namedm[i] = namedm[i];
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
731 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
732
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
733 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
734 * If the cursor is only off by one line, put it at the same position as
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
735 * before starting the change (for the "o" command).
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
736 * Otherwise the cursor should go to the first undone line.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
737 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
738 if (curbuf->b_u_curhead->uh_cursor.lnum + 1 == curwin->w_cursor.lnum
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
739 && curwin->w_cursor.lnum > 1)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
740 --curwin->w_cursor.lnum;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
741 if (curbuf->b_u_curhead->uh_cursor.lnum == curwin->w_cursor.lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
742 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
743 curwin->w_cursor.col = curbuf->b_u_curhead->uh_cursor.col;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
744 #ifdef FEAT_VIRTUALEDIT
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
745 if (virtual_active() && curbuf->b_u_curhead->uh_cursor_vcol >= 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
746 coladvance((colnr_T)curbuf->b_u_curhead->uh_cursor_vcol);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
747 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
748 curwin->w_cursor.coladd = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
749 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
750 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
751 else if (curwin->w_cursor.lnum <= curbuf->b_ml.ml_line_count)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
752 beginline(BL_SOL | BL_FIX);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
753 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
754 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
755 /* We get here with the current cursor line being past the end (eg
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
756 * after adding lines at the end of the file, and then undoing it).
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
757 * check_cursor() will move the cursor to the last line. Move it to
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
758 * the first column here. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
759 curwin->w_cursor.col = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
760 #ifdef FEAT_VIRTUALEDIT
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
761 curwin->w_cursor.coladd = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
762 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
763 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
764
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
765 /* Make sure the cursor is on an existing line and column. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
766 check_cursor();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
767 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
768
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
769 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
770 * If we deleted or added lines, report the number of less/more lines.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
771 * Otherwise, report the number of changes (this may be incorrect
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
772 * in some cases, but it's better than nothing).
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
773 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
774 static void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
775 u_undo_end()
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
776 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
777 if ((u_oldcount -= u_newcount) != 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
778 msgmore(-u_oldcount);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
779 else if (u_newcount > p_report)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
780 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
781 if (u_newcount == 1)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
782 MSG(_("1 change"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
783 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
784 smsg((char_u *)_("%ld changes"), u_newcount);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
785 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
786 #ifdef FEAT_FOLDING
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
787 if ((fdo_flags & FDO_UNDO) && KeyTyped)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
788 foldOpenCursor();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
789 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
790 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
791
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
792 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
793 * u_sync: stop adding to the current entry list
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
794 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
795 void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
796 u_sync()
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
797 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
798 if (curbuf->b_u_synced)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
799 return; /* already synced */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
800 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
801 if (im_is_preediting())
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
802 return; /* XIM is busy, don't break an undo sequence */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
803 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
804 if (p_ul < 0)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
805 curbuf->b_u_synced = TRUE; /* no entries, nothing to do */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
806 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
807 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
808 u_getbot(); /* compute ue_bot of previous u_save */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
809 curbuf->b_u_curhead = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
810 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
811 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
812
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
813 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
814 * Called after writing the file and setting b_changed to FALSE.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
815 * Now an undo means that the buffer is modified.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
816 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
817 void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
818 u_unchanged(buf)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
819 buf_T *buf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
820 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
821 struct u_header *uh;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
822
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
823 for (uh = buf->b_u_newhead; uh; uh = uh->uh_next)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
824 uh->uh_flags |= UH_CHANGED;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
825 buf->b_did_warn = FALSE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
826 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
827
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
828 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
829 * Get pointer to last added entry.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
830 * If it's not valid, give an error message and return NULL.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
831 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
832 static u_entry_T *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
833 u_get_headentry()
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
834 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
835 if (curbuf->b_u_newhead == NULL || curbuf->b_u_newhead->uh_entry == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
836 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
837 EMSG(_("E439: undo list corrupt"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
838 return NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
839 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
840 return curbuf->b_u_newhead->uh_entry;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
841 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
842
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
843 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
844 * u_getbot(): compute the line number of the previous u_save
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
845 * It is called only when b_u_synced is FALSE.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
846 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
847 static void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
848 u_getbot()
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
849 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
850 u_entry_T *uep;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
851 linenr_T extra;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
852
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
853 uep = u_get_headentry(); /* check for corrupt undo list */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
854 if (uep == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
855 return;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
856
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
857 uep = curbuf->b_u_newhead->uh_getbot_entry;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
858 if (uep != NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
859 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
860 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
861 * the new ue_bot is computed from the number of lines that has been
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
862 * inserted (0 - deleted) since calling u_save. This is equal to the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
863 * old line count subtracted from the current line count.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
864 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
865 extra = curbuf->b_ml.ml_line_count - uep->ue_lcount;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
866 uep->ue_bot = uep->ue_top + uep->ue_size + 1 + extra;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
867 if (uep->ue_bot < 1 || uep->ue_bot > curbuf->b_ml.ml_line_count)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
868 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
869 EMSG(_("E440: undo line missing"));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
870 uep->ue_bot = uep->ue_top + 1; /* assume all lines deleted, will
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
871 * get all the old lines back
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
872 * without deleting the current
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
873 * ones */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
874 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
875
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
876 curbuf->b_u_newhead->uh_getbot_entry = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
877 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
878
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
879 curbuf->b_u_synced = TRUE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
880 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
881
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
882 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
883 * u_freelist: free one entry list and adjust the pointers
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
884 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
885 static void
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
886 u_freelist(buf, uhp)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
887 buf_T *buf;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
888 struct u_header *uhp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
889 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
890 u_entry_T *uep, *nuep;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
891
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
892 for (uep = uhp->uh_entry; uep != NULL; uep = nuep)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
893 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
894 nuep = uep->ue_next;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
895 u_freeentry(uep, uep->ue_size);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
896 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
897
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
898 if (buf->b_u_curhead == uhp)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
899 buf->b_u_curhead = NULL;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
900
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
901 if (uhp->uh_next == NULL)
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
902 buf->b_u_oldhead = uhp->uh_prev;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
903 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
904 uhp->uh_next->uh_prev = uhp->uh_prev;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
905
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
906 if (uhp->uh_prev == NULL)
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
907 buf->b_u_newhead = uhp->uh_next;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
908 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
909 uhp->uh_prev->uh_next = uhp->uh_next;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
910
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
911 U_FREE_LINE((char_u *)uhp);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
912 --buf->b_u_numhead;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
913 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
914
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
915 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
916 * free entry 'uep' and 'n' lines in uep->ue_array[]
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
917 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
918 static void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
919 u_freeentry(uep, n)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
920 u_entry_T *uep;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
921 long n;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
922 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
923 while (n)
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
924 U_FREE_LINE(uep->ue_array[--n]);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
925 U_FREE_LINE((char_u *)uep);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
926 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
927
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
928 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
929 * invalidate the undo buffer; called when storage has already been released
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
930 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
931 void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
932 u_clearall(buf)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
933 buf_T *buf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
934 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
935 buf->b_u_newhead = buf->b_u_oldhead = buf->b_u_curhead = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
936 buf->b_u_synced = TRUE;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
937 buf->b_u_numhead = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
938 buf->b_u_line_ptr = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
939 buf->b_u_line_lnum = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
940 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
941
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
942 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
943 * save the line "lnum" for the "U" command
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
944 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
945 void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
946 u_saveline(lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
947 linenr_T lnum;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
948 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
949 if (lnum == curbuf->b_u_line_lnum) /* line is already saved */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
950 return;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
951 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) /* should never happen */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
952 return;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
953 u_clearline();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
954 curbuf->b_u_line_lnum = lnum;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
955 if (curwin->w_cursor.lnum == lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
956 curbuf->b_u_line_colnr = curwin->w_cursor.col;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
957 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
958 curbuf->b_u_line_colnr = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
959 if ((curbuf->b_u_line_ptr = u_save_line(lnum)) == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
960 do_outofmem_msg((long_u)0);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
961 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
962
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
963 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
964 * clear the line saved for the "U" command
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
965 * (this is used externally for crossing a line while in insert mode)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
966 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
967 void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
968 u_clearline()
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
969 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
970 if (curbuf->b_u_line_ptr != NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
971 {
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
972 U_FREE_LINE(curbuf->b_u_line_ptr);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
973 curbuf->b_u_line_ptr = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
974 curbuf->b_u_line_lnum = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
975 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
976 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
977
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
978 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
979 * Implementation of the "U" command.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
980 * Differentiation from vi: "U" can be undone with the next "U".
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
981 * We also allow the cursor to be in another line.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
982 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
983 void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
984 u_undoline()
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
985 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
986 colnr_T t;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
987 char_u *oldp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
988
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
989 if (undo_off)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
990 return;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
991
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
992 if (curbuf->b_u_line_ptr == NULL ||
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
993 curbuf->b_u_line_lnum > curbuf->b_ml.ml_line_count)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
994 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
995 beep_flush();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
996 return;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
997 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
998 /* first save the line for the 'u' command */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
999 if (u_savecommon(curbuf->b_u_line_lnum - 1,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1000 curbuf->b_u_line_lnum + 1, (linenr_T)0) == FAIL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1001 return;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1002 oldp = u_save_line(curbuf->b_u_line_lnum);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1003 if (oldp == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1004 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1005 do_outofmem_msg((long_u)0);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1006 return;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1007 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1008 ml_replace(curbuf->b_u_line_lnum, curbuf->b_u_line_ptr, TRUE);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1009 changed_bytes(curbuf->b_u_line_lnum, 0);
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1010 U_FREE_LINE(curbuf->b_u_line_ptr);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1011 curbuf->b_u_line_ptr = oldp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1012
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1013 t = curbuf->b_u_line_colnr;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1014 if (curwin->w_cursor.lnum == curbuf->b_u_line_lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1015 curbuf->b_u_line_colnr = curwin->w_cursor.col;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1016 curwin->w_cursor.col = t;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1017 curwin->w_cursor.lnum = curbuf->b_u_line_lnum;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1018 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1019
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1020 /*
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1021 * There are two implementations of the memory management for undo:
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1022 * 1. Use the standard malloc()/free() functions.
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1023 * This should be fast for allocating memory, but when a buffer is
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1024 * abandoned every single allocated chunk must be freed, which may be slow.
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1025 * 2. Allocate larger blocks of memory and keep track of chunks ourselves.
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1026 * This is fast for abandoning, but the use of linked lists is slow for
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1027 * finding a free chunk. Esp. when a lot of lines are changed or deleted.
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1028 * A bit of profiling showed that the first method is faster, especially when
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1029 * making a large number of changes, under the condition that malloc()/free()
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1030 * is implemented efficiently.
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1031 */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1032 #ifdef U_USE_MALLOC
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1033 /*
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1034 * Version of undo memory allocation using malloc()/free()
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1035 *
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1036 * U_FREE_LINE() and U_ALLOC_LINE() are macros that invoke vim_free() and
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1037 * lalloc() directly.
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1038 */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1039
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1040 /*
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1041 * Free all allocated memory blocks for the buffer 'buf'.
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1042 */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1043 void
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1044 u_blockfree(buf)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1045 buf_T *buf;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1046 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1047 while (buf->b_u_newhead != NULL)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1048 u_freelist(buf, buf->b_u_newhead);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1049 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1050
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1051 #else
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1052 /*
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1053 * Storage allocation for the undo lines and blocks of the current file.
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1054 * Version where Vim keeps track of the available memory.
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1055 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1056
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1057 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1058 * Memory is allocated in relatively large blocks. These blocks are linked
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1059 * in the allocated block list, headed by curbuf->b_block_head. They are all
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1060 * freed when abandoning a file, so we don't have to free every single line.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1061 * The list is kept sorted on memory address.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1062 * block_alloc() allocates a block.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1063 * m_blockfree() frees all blocks.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1064 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1065 * The available chunks of memory are kept in free chunk lists. There is
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1066 * one free list for each block of allocated memory. The list is kept sorted
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1067 * on memory address.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1068 * u_alloc_line() gets a chunk from the free lists.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1069 * u_free_line() returns a chunk to the free lists.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1070 * curbuf->b_m_search points to the chunk before the chunk that was
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1071 * freed/allocated the last time.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1072 * curbuf->b_mb_current points to the b_head where curbuf->b_m_search
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1073 * points into the free list.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1074 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1075 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1076 * b_block_head /---> block #1 /---> block #2
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1077 * mb_next ---/ mb_next ---/ mb_next ---> NULL
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1078 * mb_info mb_info mb_info
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1079 * | | |
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1080 * V V V
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1081 * NULL free chunk #1.1 free chunk #2.1
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1082 * | |
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1083 * V V
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1084 * free chunk #1.2 NULL
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1085 * |
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1086 * V
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1087 * NULL
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1088 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1089 * When a single free chunk list would have been used, it could take a lot
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1090 * of time in u_free_line() to find the correct place to insert a chunk in the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1091 * free list. The single free list would become very long when many lines are
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1092 * changed (e.g. with :%s/^M$//).
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1093 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1094
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1095 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1096 * this blocksize is used when allocating new lines
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1097 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1098 #define MEMBLOCKSIZE 2044
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1099
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1100 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1101 * The size field contains the size of the chunk, including the size field
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1102 * itself.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1103 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1104 * When the chunk is not in-use it is preceded with the m_info structure.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1105 * The m_next field links it in one of the free chunk lists.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1106 *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1107 * On most unix systems structures have to be longword (32 or 64 bit) aligned.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1108 * On most other systems they are short (16 bit) aligned.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1109 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1110
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1111 /* the structure definitions are now in structs.h */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1112
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1113 #ifdef ALIGN_LONG
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1114 /* size of m_size */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1115 # define M_OFFSET (sizeof(long_u))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1116 #else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1117 /* size of m_size */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1118 # define M_OFFSET (sizeof(short_u))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1119 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1120
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1121 static char_u *u_blockalloc __ARGS((long_u));
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1122
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1123 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1124 * Allocate a block of memory and link it in the allocated block list.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1125 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1126 static char_u *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1127 u_blockalloc(size)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1128 long_u size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1129 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1130 mblock_T *p;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1131 mblock_T *mp, *next;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1132
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1133 p = (mblock_T *)lalloc(size + sizeof(mblock_T), FALSE);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1134 if (p != NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1135 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1136 /* Insert the block into the allocated block list, keeping it
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1137 sorted on address. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1138 for (mp = &curbuf->b_block_head;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1139 (next = mp->mb_next) != NULL && next < p;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1140 mp = next)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1141 ;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1142 p->mb_next = next; /* link in block list */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1143 p->mb_size = size;
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1144 p->mb_maxsize = 0; /* nothing free yet */
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1145 mp->mb_next = p;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1146 p->mb_info.m_next = NULL; /* clear free list */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1147 p->mb_info.m_size = 0;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1148 curbuf->b_mb_current = p; /* remember current block */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1149 curbuf->b_m_search = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1150 p++; /* return usable memory */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1151 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1152 return (char_u *)p;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1153 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1154
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1155 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1156 * free all allocated memory blocks for the buffer 'buf'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1157 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1158 void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1159 u_blockfree(buf)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1160 buf_T *buf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1161 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1162 mblock_T *p, *np;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1163
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1164 for (p = buf->b_block_head.mb_next; p != NULL; p = np)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1165 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1166 np = p->mb_next;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1167 vim_free(p);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1168 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1169 buf->b_block_head.mb_next = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1170 buf->b_m_search = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1171 buf->b_mb_current = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1172 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1173
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1174 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1175 * Free a chunk of memory for the current buffer.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1176 * Insert the chunk into the correct free list, keeping it sorted on address.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1177 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1178 static void
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1179 u_free_line(ptr, keep)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1180 char_u *ptr;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1181 int keep; /* don't free the block when it's empty */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1182 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1183 minfo_T *next;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1184 minfo_T *prev, *curr;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1185 minfo_T *mp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1186 mblock_T *nextb;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1187 mblock_T *prevb;
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1188 long_u maxsize;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1189
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1190 if (ptr == NULL || ptr == IObuff)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1191 return; /* illegal address can happen in out-of-memory situations */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1192
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1193 mp = (minfo_T *)(ptr - M_OFFSET);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1194
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1195 /* find block where chunk could be a part off */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1196 /* if we change curbuf->b_mb_current, curbuf->b_m_search is set to NULL */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1197 if (curbuf->b_mb_current == NULL || mp < (minfo_T *)curbuf->b_mb_current)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1198 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1199 curbuf->b_mb_current = curbuf->b_block_head.mb_next;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1200 curbuf->b_m_search = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1201 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1202 if ((nextb = curbuf->b_mb_current->mb_next) != NULL
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1203 && (minfo_T *)nextb < mp)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1204 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1205 curbuf->b_mb_current = nextb;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1206 curbuf->b_m_search = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1207 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1208 while ((nextb = curbuf->b_mb_current->mb_next) != NULL
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1209 && (minfo_T *)nextb < mp)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1210 curbuf->b_mb_current = nextb;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1211
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1212 curr = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1213 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1214 * If mp is smaller than curbuf->b_m_search->m_next go to the start of
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1215 * the free list
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1216 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1217 if (curbuf->b_m_search == NULL || mp < (curbuf->b_m_search->m_next))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1218 next = &(curbuf->b_mb_current->mb_info);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1219 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1220 next = curbuf->b_m_search;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1221 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1222 * The following loop is executed very often.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1223 * Therefore it has been optimized at the cost of readability.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1224 * Keep it fast!
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1225 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1226 #ifdef SLOW_BUT_EASY_TO_READ
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1227 do
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1228 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1229 prev = curr;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1230 curr = next;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1231 next = next->m_next;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1232 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1233 while (mp > next && next != NULL);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1234 #else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1235 do /* first, middle, last */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1236 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1237 prev = next->m_next; /* curr, next, prev */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1238 if (prev == NULL || mp <= prev)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1239 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1240 prev = curr;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1241 curr = next;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1242 next = next->m_next;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1243 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1244 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1245 curr = prev->m_next; /* next, prev, curr */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1246 if (curr == NULL || mp <= curr)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1247 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1248 prev = next;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1249 curr = prev->m_next;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1250 next = curr->m_next;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1251 break;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1252 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1253 next = curr->m_next; /* prev, curr, next */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1254 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1255 while (mp > next && next != NULL);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1256 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1257
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1258 /* if *mp and *next are concatenated, join them into one chunk */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1259 if ((char_u *)mp + mp->m_size == (char_u *)next)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1260 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1261 mp->m_size += next->m_size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1262 mp->m_next = next->m_next;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1263 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1264 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1265 mp->m_next = next;
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1266 maxsize = mp->m_size;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1267
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1268 /* if *curr and *mp are concatenated, join them */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1269 if (prev != NULL && (char_u *)curr + curr->m_size == (char_u *)mp)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1270 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1271 curr->m_size += mp->m_size;
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1272 maxsize = curr->m_size;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1273 curr->m_next = mp->m_next;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1274 curbuf->b_m_search = prev;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1275 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1276 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1277 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1278 curr->m_next = mp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1279 curbuf->b_m_search = curr; /* put curbuf->b_m_search before freed
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1280 chunk */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1281 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1282
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1283 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1284 * If the block only containes free memory now, release it.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1285 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1286 if (!keep && curbuf->b_mb_current->mb_size
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1287 == curbuf->b_mb_current->mb_info.m_next->m_size)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1288 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1289 /* Find the block before the current one to be able to unlink it from
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1290 * the list of blocks. */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1291 prevb = &curbuf->b_block_head;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1292 for (nextb = prevb->mb_next; nextb != curbuf->b_mb_current;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1293 nextb = nextb->mb_next)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1294 prevb = nextb;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1295 prevb->mb_next = nextb->mb_next;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1296 vim_free(nextb);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1297 curbuf->b_mb_current = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1298 curbuf->b_m_search = NULL;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1299 }
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1300 else if (curbuf->b_mb_current->mb_maxsize < maxsize)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1301 curbuf->b_mb_current->mb_maxsize = maxsize;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1302 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1303
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1304 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1305 * Allocate and initialize a new line structure with room for at least
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1306 * 'size' characters plus a terminating NUL.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1307 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1308 static char_u *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1309 u_alloc_line(size)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1310 unsigned size;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1311 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1312 minfo_T *mp, *mprev, *mp2;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1313 mblock_T *mbp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1314 int size_align;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1315
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1316 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1317 * Add room for size field and trailing NUL byte.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1318 * Adjust for minimal size (must be able to store minfo_T
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1319 * plus a trailing NUL, so the chunk can be released again)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1320 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1321 size += M_OFFSET + 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1322 if (size < sizeof(minfo_T) + 1)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1323 size = sizeof(minfo_T) + 1;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1324
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1325 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1326 * round size up for alignment
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1327 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1328 size_align = (size + ALIGN_MASK) & ~ALIGN_MASK;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1329
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1330 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1331 * If curbuf->b_m_search is NULL (uninitialized free list) start at
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1332 * curbuf->b_block_head
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1333 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1334 if (curbuf->b_mb_current == NULL || curbuf->b_m_search == NULL)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1335 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1336 curbuf->b_mb_current = &curbuf->b_block_head;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1337 curbuf->b_m_search = &(curbuf->b_block_head.mb_info);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1338 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1339
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1340 /* Search for a block with enough space. */
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1341 mbp = curbuf->b_mb_current;
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1342 while (mbp->mb_maxsize < size_align)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1343 {
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1344 if (mbp->mb_next != NULL)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1345 mbp = mbp->mb_next;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1346 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1347 mbp = &curbuf->b_block_head;
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1348 if (mbp == curbuf->b_mb_current)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1349 {
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1350 int n = (size_align > (MEMBLOCKSIZE / 4)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1351 ? size_align : MEMBLOCKSIZE);
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1352
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1353 /* Back where we started in block list: need to add a new block
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1354 * with enough space. */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1355 mp = (minfo_T *)u_blockalloc((long_u)n);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1356 if (mp == NULL)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1357 return (NULL);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1358 mp->m_size = n;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1359 u_free_line((char_u *)mp + M_OFFSET, TRUE);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1360 mbp = curbuf->b_mb_current;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1361 break;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1362 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1363 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1364 if (mbp != curbuf->b_mb_current)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1365 curbuf->b_m_search = &(mbp->mb_info);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1366
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1367 /* In this block find a chunk with enough space. */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1368 mprev = curbuf->b_m_search;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1369 mp = curbuf->b_m_search->m_next;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1370 while (1)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1371 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1372 if (mp == NULL) /* at end of the list */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1373 mp = &(mbp->mb_info); /* wrap around to begin */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1374 if (mp->m_size >= size)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1375 break;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1376 if (mp == curbuf->b_m_search)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1377 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1378 /* back where we started in free chunk list: "cannot happen" */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1379 EMSG2(_(e_intern2), "u_alloc_line()");
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1380 return NULL;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1381 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1382 mprev = mp;
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1383 mp = mp->m_next;
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1384 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1385
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1386 /* when using the largest chunk adjust mb_maxsize */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1387 if (mp->m_size >= mbp->mb_maxsize)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1388 mbp->mb_maxsize = 0;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1389
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1390 /* if the chunk we found is large enough, split it up in two */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1391 if ((long)mp->m_size - size_align >= (long)(sizeof(minfo_T) + 1))
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1392 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1393 mp2 = (minfo_T *)((char_u *)mp + size_align);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1394 mp2->m_size = mp->m_size - size_align;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1395 mp2->m_next = mp->m_next;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1396 mprev->m_next = mp2;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1397 mp->m_size = size_align;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1398 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1399 else /* remove *mp from the free list */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1400 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1401 mprev->m_next = mp->m_next;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1402 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1403 curbuf->b_m_search = mprev;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1404 curbuf->b_mb_current = mbp;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1405
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1406 /* If using the largest chunk need to find the new largest chunk */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1407 if (mbp->mb_maxsize == 0)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1408 for (mp2 = &(mbp->mb_info); mp2 != NULL; mp2 = mp2->m_next)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1409 if (mbp->mb_maxsize < mp2->m_size)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1410 mbp->mb_maxsize = mp2->m_size;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1411
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1412 mp = (minfo_T *)((char_u *)mp + M_OFFSET);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1413 *(char_u *)mp = NUL; /* set the first byte to NUL */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1414
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1415 return ((char_u *)mp);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1416 }
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1417 #endif
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1418
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1419 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1420 * u_save_line(): allocate memory with u_alloc_line() and copy line 'lnum'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1421 * into it.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1422 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1423 static char_u *
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1424 u_save_line(lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1425 linenr_T lnum;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1426 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1427 char_u *src;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1428 char_u *dst;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1429 unsigned len;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1430
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1431 src = ml_get(lnum);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1432 len = (unsigned)STRLEN(src);
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 33
diff changeset
1433 if ((dst = U_ALLOC_LINE(len)) != NULL)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1434 mch_memmove(dst, src, (size_t)(len + 1));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1435 return (dst);
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1436 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1437
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1438 /*
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1439 * Check if the 'modified' flag is set, or 'ff' has changed (only need to
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1440 * check the first character, because it can only be "dos", "unix" or "mac").
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1441 * "nofile" and "scratch" type buffers are considered to always be unchanged.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1442 */
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1443 int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1444 bufIsChanged(buf)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1445 buf_T *buf;
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1446 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1447 return
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1448 #ifdef FEAT_QUICKFIX
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1449 !bt_dontwrite(buf) &&
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1450 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1451 (buf->b_changed || file_ff_differs(buf));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1452 }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1453
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1454 int
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1455 curbufIsChanged()
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1456 {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1457 return
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1458 #ifdef FEAT_QUICKFIX
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1459 !bt_dontwrite(curbuf) &&
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1460 #endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1461 (curbuf->b_changed || file_ff_differs(curbuf));
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1462 }