7
|
1 /* vi:set ts=8 sts=4 sw=4:
|
|
2 *
|
|
3 * VIM - Vi IMproved by Bram Moolenaar
|
|
4 *
|
|
5 * Do ":help uganda" in Vim to read copying and usage conditions.
|
|
6 * Do ":help credits" in Vim to see a list of people who contributed.
|
|
7 * See README.txt for an overview of the Vim source code.
|
|
8 */
|
|
9
|
|
10 /*
|
|
11 * undo.c: multi level undo facility
|
|
12 *
|
|
13 * The saved lines are stored in a list of lists (one for each buffer):
|
|
14 *
|
|
15 * b_u_oldhead------------------------------------------------+
|
|
16 * |
|
|
17 * V
|
|
18 * +--------------+ +--------------+ +--------------+
|
|
19 * b_u_newhead--->| u_header | | u_header | | u_header |
|
|
20 * | uh_next------>| uh_next------>| uh_next---->NULL
|
|
21 * NULL<--------uh_prev |<---------uh_prev |<---------uh_prev |
|
|
22 * | uh_entry | | uh_entry | | uh_entry |
|
|
23 * +--------|-----+ +--------|-----+ +--------|-----+
|
|
24 * | | |
|
|
25 * V V V
|
|
26 * +--------------+ +--------------+ +--------------+
|
|
27 * | u_entry | | u_entry | | u_entry |
|
|
28 * | ue_next | | ue_next | | ue_next |
|
|
29 * +--------|-----+ +--------|-----+ +--------|-----+
|
|
30 * | | |
|
|
31 * V V V
|
|
32 * +--------------+ NULL NULL
|
|
33 * | u_entry |
|
|
34 * | ue_next |
|
|
35 * +--------|-----+
|
|
36 * |
|
|
37 * V
|
|
38 * etc.
|
|
39 *
|
|
40 * Each u_entry list contains the information for one undo or redo.
|
|
41 * curbuf->b_u_curhead points to the header of the last undo (the next redo),
|
758
|
42 * or is NULL if nothing has been undone (end of the branch).
|
7
|
43 *
|
753
|
44 * For keeping alternate undo/redo branches the uh_alt field is used. Thus at
|
|
45 * each point in the list a branch may appear for an alternate to redo. The
|
|
46 * uh_seq field is numbered sequentially to be able to find a newer or older
|
|
47 * branch.
|
|
48 *
|
758
|
49 * +---------------+ +---------------+
|
|
50 * b_u_oldhead --->| u_header | | u_header |
|
|
51 * | uh_alt_next ---->| uh_alt_next ----> NULL
|
|
52 * NULL <----- uh_alt_prev |<------ uh_alt_prev |
|
|
53 * | uh_prev | | uh_prev |
|
|
54 * +-----|---------+ +-----|---------+
|
|
55 * | |
|
|
56 * V V
|
|
57 * +---------------+ +---------------+
|
|
58 * | u_header | | u_header |
|
|
59 * | uh_alt_next | | uh_alt_next |
|
|
60 * b_u_newhead --->| uh_alt_prev | | uh_alt_prev |
|
|
61 * | uh_prev | | uh_prev |
|
|
62 * +-----|---------+ +-----|---------+
|
|
63 * | |
|
|
64 * V V
|
|
65 * NULL +---------------+ +---------------+
|
|
66 * | u_header | | u_header |
|
|
67 * | uh_alt_next ---->| uh_alt_next |
|
|
68 * | uh_alt_prev |<------ uh_alt_prev |
|
|
69 * | uh_prev | | uh_prev |
|
|
70 * +-----|---------+ +-----|---------+
|
|
71 * | |
|
|
72 * etc. etc.
|
|
73 *
|
|
74 *
|
168
|
75 * All data is allocated with U_ALLOC_LINE(), it will be freed as soon as the
|
|
76 * buffer is unloaded.
|
7
|
77 */
|
|
78
|
|
79 #include "vim.h"
|
|
80
|
168
|
81 /* See below: use malloc()/free() for memory management. */
|
|
82 #define U_USE_MALLOC 1
|
|
83
|
758
|
84 static void u_unch_branch __ARGS((u_header_T *uhp));
|
7
|
85 static u_entry_T *u_get_headentry __ARGS((void));
|
|
86 static void u_getbot __ARGS((void));
|
632
|
87 static int undo_allowed __ARGS((void));
|
7
|
88 static int u_savecommon __ARGS((linenr_T, linenr_T, linenr_T));
|
|
89 static void u_doit __ARGS((int count));
|
|
90 static void u_undoredo __ARGS((void));
|
|
91 static void u_undo_end __ARGS((void));
|
758
|
92 static void u_freeheader __ARGS((buf_T *buf, u_header_T *uhp, u_header_T **uhpp));
|
753
|
93 static void u_freebranch __ARGS((buf_T *buf, u_header_T *uhp, u_header_T **uhpp));
|
|
94 static void u_freeentries __ARGS((buf_T *buf, u_header_T *uhp, u_header_T **uhpp));
|
7
|
95 static void u_freeentry __ARGS((u_entry_T *, long));
|
|
96
|
168
|
97 #ifdef U_USE_MALLOC
|
|
98 # define U_FREE_LINE(ptr) vim_free(ptr)
|
|
99 # define U_ALLOC_LINE(size) lalloc((long_u)((size) + 1), FALSE)
|
|
100 #else
|
|
101 static void u_free_line __ARGS((char_u *ptr, int keep));
|
|
102 static char_u *u_alloc_line __ARGS((unsigned size));
|
|
103 # define U_FREE_LINE(ptr) u_free_line((ptr), FALSE)
|
|
104 # define U_ALLOC_LINE(size) u_alloc_line(size)
|
|
105 #endif
|
7
|
106 static char_u *u_save_line __ARGS((linenr_T));
|
|
107
|
|
108 static long u_newcount, u_oldcount;
|
|
109
|
|
110 /*
|
|
111 * When 'u' flag included in 'cpoptions', we behave like vi. Need to remember
|
|
112 * the action that "u" should do.
|
|
113 */
|
|
114 static int undo_undoes = FALSE;
|
|
115
|
|
116 /*
|
344
|
117 * Save the current line for both the "u" and "U" command.
|
|
118 * Returns OK or FAIL.
|
7
|
119 */
|
|
120 int
|
|
121 u_save_cursor()
|
|
122 {
|
|
123 return (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
|
|
124 (linenr_T)(curwin->w_cursor.lnum + 1)));
|
|
125 }
|
|
126
|
|
127 /*
|
|
128 * Save the lines between "top" and "bot" for both the "u" and "U" command.
|
|
129 * "top" may be 0 and bot may be curbuf->b_ml.ml_line_count + 1.
|
|
130 * Returns FAIL when lines could not be saved, OK otherwise.
|
|
131 */
|
|
132 int
|
|
133 u_save(top, bot)
|
|
134 linenr_T top, bot;
|
|
135 {
|
|
136 if (undo_off)
|
|
137 return OK;
|
|
138
|
|
139 if (top > curbuf->b_ml.ml_line_count ||
|
|
140 top >= bot || bot > curbuf->b_ml.ml_line_count + 1)
|
|
141 return FALSE; /* rely on caller to do error messages */
|
|
142
|
|
143 if (top + 2 == bot)
|
|
144 u_saveline((linenr_T)(top + 1));
|
|
145
|
|
146 return (u_savecommon(top, bot, (linenr_T)0));
|
|
147 }
|
|
148
|
|
149 /*
|
|
150 * save the line "lnum" (used by ":s" and "~" command)
|
|
151 * The line is replaced, so the new bottom line is lnum + 1.
|
|
152 */
|
|
153 int
|
|
154 u_savesub(lnum)
|
|
155 linenr_T lnum;
|
|
156 {
|
|
157 if (undo_off)
|
|
158 return OK;
|
|
159
|
|
160 return (u_savecommon(lnum - 1, lnum + 1, lnum + 1));
|
|
161 }
|
|
162
|
|
163 /*
|
|
164 * a new line is inserted before line "lnum" (used by :s command)
|
|
165 * The line is inserted, so the new bottom line is lnum + 1.
|
|
166 */
|
|
167 int
|
|
168 u_inssub(lnum)
|
|
169 linenr_T lnum;
|
|
170 {
|
|
171 if (undo_off)
|
|
172 return OK;
|
|
173
|
|
174 return (u_savecommon(lnum - 1, lnum, lnum + 1));
|
|
175 }
|
|
176
|
|
177 /*
|
|
178 * save the lines "lnum" - "lnum" + nlines (used by delete command)
|
|
179 * The lines are deleted, so the new bottom line is lnum, unless the buffer
|
|
180 * becomes empty.
|
|
181 */
|
|
182 int
|
|
183 u_savedel(lnum, nlines)
|
|
184 linenr_T lnum;
|
|
185 long nlines;
|
|
186 {
|
|
187 if (undo_off)
|
|
188 return OK;
|
|
189
|
|
190 return (u_savecommon(lnum - 1, lnum + nlines,
|
|
191 nlines == curbuf->b_ml.ml_line_count ? 2 : lnum));
|
|
192 }
|
|
193
|
632
|
194 /*
|
|
195 * Return TRUE when undo is allowed. Otherwise give an error message and
|
|
196 * return FALSE.
|
|
197 */
|
|
198 static int
|
|
199 undo_allowed()
|
|
200 {
|
|
201 /* Don't allow changes when 'modifiable' is off. */
|
|
202 if (!curbuf->b_p_ma)
|
|
203 {
|
|
204 EMSG(_(e_modifiable));
|
|
205 return FALSE;
|
|
206 }
|
|
207
|
|
208 #ifdef HAVE_SANDBOX
|
|
209 /* In the sandbox it's not allowed to change the text. */
|
|
210 if (sandbox != 0)
|
|
211 {
|
|
212 EMSG(_(e_sandbox));
|
|
213 return FALSE;
|
|
214 }
|
|
215 #endif
|
|
216
|
|
217 /* Don't allow changes in the buffer while editing the cmdline. The
|
|
218 * caller of getcmdline() may get confused. */
|
634
|
219 if (textlock != 0)
|
632
|
220 {
|
|
221 EMSG(_(e_secure));
|
|
222 return FALSE;
|
|
223 }
|
|
224
|
|
225 return TRUE;
|
|
226 }
|
|
227
|
7
|
228 static int
|
|
229 u_savecommon(top, bot, newbot)
|
|
230 linenr_T top, bot;
|
|
231 linenr_T newbot;
|
|
232 {
|
753
|
233 linenr_T lnum;
|
|
234 long i;
|
|
235 u_header_T *uhp;
|
|
236 u_header_T *old_curhead;
|
|
237 u_entry_T *uep;
|
|
238 u_entry_T *prev_uep;
|
|
239 long size;
|
7
|
240
|
632
|
241 /* When making changes is not allowed return FAIL. It's a crude way to
|
|
242 * make all change commands fail. */
|
|
243 if (!undo_allowed())
|
7
|
244 return FAIL;
|
|
245
|
|
246 #ifdef FEAT_NETBEANS_INTG
|
|
247 /*
|
|
248 * Netbeans defines areas that cannot be modified. Bail out here when
|
|
249 * trying to change text in a guarded area.
|
|
250 */
|
33
|
251 if (usingNetbeans)
|
7
|
252 {
|
33
|
253 if (netbeans_is_guarded(top, bot))
|
|
254 {
|
|
255 EMSG(_(e_guarded));
|
|
256 return FAIL;
|
|
257 }
|
|
258 if (curbuf->b_p_ro)
|
|
259 {
|
|
260 EMSG(_(e_nbreadonly));
|
|
261 return FAIL;
|
|
262 }
|
7
|
263 }
|
|
264 #endif
|
|
265
|
|
266 #ifdef FEAT_AUTOCMD
|
|
267 /*
|
|
268 * Saving text for undo means we are going to make a change. Give a
|
|
269 * warning for a read-only file before making the change, so that the
|
|
270 * FileChangedRO event can replace the buffer with a read-write version
|
|
271 * (e.g., obtained from a source control system).
|
|
272 */
|
|
273 change_warning(0);
|
|
274 #endif
|
|
275
|
|
276 size = bot - top - 1;
|
|
277
|
|
278 /*
|
|
279 * if curbuf->b_u_synced == TRUE make a new header
|
|
280 */
|
|
281 if (curbuf->b_u_synced)
|
|
282 {
|
|
283 #ifdef FEAT_JUMPLIST
|
|
284 /* Need to create new entry in b_changelist. */
|
|
285 curbuf->b_new_change = TRUE;
|
|
286 #endif
|
|
287
|
753
|
288 if (p_ul >= 0)
|
|
289 {
|
|
290 /*
|
|
291 * Make a new header entry. Do this first so that we don't mess
|
|
292 * up the undo info when out of memory.
|
|
293 */
|
|
294 uhp = (u_header_T *)U_ALLOC_LINE((unsigned)sizeof(u_header_T));
|
|
295 if (uhp == NULL)
|
|
296 goto nomem;
|
|
297 }
|
|
298
|
7
|
299 /*
|
758
|
300 * If we undid more than we redid, move the entry lists before and
|
|
301 * including curbuf->b_u_curhead to an alternate branch.
|
7
|
302 */
|
753
|
303 old_curhead = curbuf->b_u_curhead;
|
|
304 if (old_curhead != NULL)
|
|
305 {
|
|
306 curbuf->b_u_newhead = old_curhead->uh_next;
|
|
307 curbuf->b_u_curhead = NULL;
|
|
308 }
|
7
|
309
|
|
310 /*
|
|
311 * free headers to keep the size right
|
|
312 */
|
|
313 while (curbuf->b_u_numhead > p_ul && curbuf->b_u_oldhead != NULL)
|
753
|
314 {
|
758
|
315 u_header_T *uhfree = curbuf->b_u_oldhead;
|
753
|
316
|
|
317 /* If there is no branch only free one header. */
|
758
|
318 if (uhfree->uh_alt_next == NULL)
|
|
319 u_freeheader(curbuf, uhfree, &old_curhead);
|
753
|
320 else
|
|
321 {
|
|
322 /* Free the oldest alternate branch as a whole. */
|
758
|
323 while (uhfree->uh_alt_next != NULL)
|
|
324 uhfree = uhfree->uh_alt_next;
|
|
325 u_freebranch(curbuf, uhfree, &old_curhead);
|
753
|
326 }
|
|
327 }
|
7
|
328
|
|
329 if (p_ul < 0) /* no undo at all */
|
|
330 {
|
753
|
331 if (old_curhead != NULL)
|
|
332 u_freebranch(curbuf, old_curhead, NULL);
|
7
|
333 curbuf->b_u_synced = FALSE;
|
|
334 return OK;
|
|
335 }
|
|
336
|
|
337 uhp->uh_prev = NULL;
|
|
338 uhp->uh_next = curbuf->b_u_newhead;
|
753
|
339 uhp->uh_alt_next = old_curhead;
|
|
340 if (old_curhead != NULL)
|
|
341 {
|
|
342 old_curhead->uh_alt_prev = uhp;
|
|
343 if (curbuf->b_u_oldhead == old_curhead)
|
|
344 curbuf->b_u_oldhead = uhp;
|
|
345 }
|
|
346 uhp->uh_alt_prev = NULL;
|
758
|
347 if (curbuf->b_u_newhead != NULL)
|
|
348 curbuf->b_u_newhead->uh_prev = uhp;
|
|
349
|
753
|
350 uhp->uh_seq = curbuf->b_u_seq_last++;
|
|
351 curbuf->b_u_seq_cur = curbuf->b_u_seq_last;
|
758
|
352 uhp->uh_time = time(NULL);
|
|
353 curbuf->b_u_seq_time = uhp->uh_time + 1;
|
|
354
|
753
|
355 uhp->uh_walk = 0;
|
7
|
356 uhp->uh_entry = NULL;
|
|
357 uhp->uh_getbot_entry = NULL;
|
|
358 uhp->uh_cursor = curwin->w_cursor; /* save cursor pos. for undo */
|
|
359 #ifdef FEAT_VIRTUALEDIT
|
|
360 if (virtual_active() && curwin->w_cursor.coladd > 0)
|
|
361 uhp->uh_cursor_vcol = getviscol();
|
|
362 else
|
|
363 uhp->uh_cursor_vcol = -1;
|
|
364 #endif
|
|
365
|
|
366 /* save changed and buffer empty flag for undo */
|
|
367 uhp->uh_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
|
|
368 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
|
|
369
|
692
|
370 /* save named marks and Visual marks for undo */
|
7
|
371 mch_memmove(uhp->uh_namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
|
692
|
372 #ifdef FEAT_VISUAL
|
|
373 uhp->uh_visual = curbuf->b_visual;
|
|
374 #endif
|
|
375
|
7
|
376 curbuf->b_u_newhead = uhp;
|
|
377 if (curbuf->b_u_oldhead == NULL)
|
|
378 curbuf->b_u_oldhead = uhp;
|
|
379 ++curbuf->b_u_numhead;
|
|
380 }
|
|
381 else
|
|
382 {
|
|
383 if (p_ul < 0) /* no undo at all */
|
|
384 return OK;
|
|
385
|
|
386 /*
|
|
387 * When saving a single line, and it has been saved just before, it
|
|
388 * doesn't make sense saving it again. Saves a lot of memory when
|
|
389 * making lots of changes inside the same line.
|
|
390 * This is only possible if the previous change didn't increase or
|
|
391 * decrease the number of lines.
|
|
392 * Check the ten last changes. More doesn't make sense and takes too
|
|
393 * long.
|
|
394 */
|
|
395 if (size == 1)
|
|
396 {
|
|
397 uep = u_get_headentry();
|
|
398 prev_uep = NULL;
|
|
399 for (i = 0; i < 10; ++i)
|
|
400 {
|
|
401 if (uep == NULL)
|
|
402 break;
|
|
403
|
|
404 /* If lines have been inserted/deleted we give up.
|
|
405 * Also when the line was included in a multi-line save. */
|
|
406 if ((curbuf->b_u_newhead->uh_getbot_entry != uep
|
|
407 ? (uep->ue_top + uep->ue_size + 1
|
|
408 != (uep->ue_bot == 0
|
|
409 ? curbuf->b_ml.ml_line_count + 1
|
|
410 : uep->ue_bot))
|
|
411 : uep->ue_lcount != curbuf->b_ml.ml_line_count)
|
|
412 || (uep->ue_size > 1
|
|
413 && top >= uep->ue_top
|
|
414 && top + 2 <= uep->ue_top + uep->ue_size + 1))
|
|
415 break;
|
|
416
|
|
417 /* If it's the same line we can skip saving it again. */
|
|
418 if (uep->ue_size == 1 && uep->ue_top == top)
|
|
419 {
|
|
420 if (i > 0)
|
|
421 {
|
|
422 /* It's not the last entry: get ue_bot for the last
|
|
423 * entry now. Following deleted/inserted lines go to
|
|
424 * the re-used entry. */
|
|
425 u_getbot();
|
|
426 curbuf->b_u_synced = FALSE;
|
|
427
|
|
428 /* Move the found entry to become the last entry. The
|
|
429 * order of undo/redo doesn't matter for the entries
|
|
430 * we move it over, since they don't change the line
|
|
431 * count and don't include this line. It does matter
|
|
432 * for the found entry if the line count is changed by
|
|
433 * the executed command. */
|
|
434 prev_uep->ue_next = uep->ue_next;
|
|
435 uep->ue_next = curbuf->b_u_newhead->uh_entry;
|
|
436 curbuf->b_u_newhead->uh_entry = uep;
|
|
437 }
|
|
438
|
|
439 /* The executed command may change the line count. */
|
|
440 if (newbot != 0)
|
|
441 uep->ue_bot = newbot;
|
|
442 else if (bot > curbuf->b_ml.ml_line_count)
|
|
443 uep->ue_bot = 0;
|
|
444 else
|
|
445 {
|
|
446 uep->ue_lcount = curbuf->b_ml.ml_line_count;
|
|
447 curbuf->b_u_newhead->uh_getbot_entry = uep;
|
|
448 }
|
|
449 return OK;
|
|
450 }
|
|
451 prev_uep = uep;
|
|
452 uep = uep->ue_next;
|
|
453 }
|
|
454 }
|
|
455
|
|
456 /* find line number for ue_bot for previous u_save() */
|
|
457 u_getbot();
|
|
458 }
|
|
459
|
|
460 #if !defined(UNIX) && !defined(DJGPP) && !defined(WIN32) && !defined(__EMX__)
|
|
461 /*
|
|
462 * With Amiga and MSDOS 16 bit we can't handle big undo's, because
|
|
463 * then u_alloc_line would have to allocate a block larger than 32K
|
|
464 */
|
|
465 if (size >= 8000)
|
|
466 goto nomem;
|
|
467 #endif
|
|
468
|
|
469 /*
|
|
470 * add lines in front of entry list
|
|
471 */
|
168
|
472 uep = (u_entry_T *)U_ALLOC_LINE((unsigned)sizeof(u_entry_T));
|
7
|
473 if (uep == NULL)
|
|
474 goto nomem;
|
|
475
|
|
476 uep->ue_size = size;
|
|
477 uep->ue_top = top;
|
|
478 if (newbot != 0)
|
|
479 uep->ue_bot = newbot;
|
|
480 /*
|
|
481 * Use 0 for ue_bot if bot is below last line.
|
|
482 * Otherwise we have to compute ue_bot later.
|
|
483 */
|
|
484 else if (bot > curbuf->b_ml.ml_line_count)
|
|
485 uep->ue_bot = 0;
|
|
486 else
|
|
487 {
|
|
488 uep->ue_lcount = curbuf->b_ml.ml_line_count;
|
|
489 curbuf->b_u_newhead->uh_getbot_entry = uep;
|
|
490 }
|
|
491
|
168
|
492 if (size > 0)
|
7
|
493 {
|
168
|
494 if ((uep->ue_array = (char_u **)U_ALLOC_LINE(
|
7
|
495 (unsigned)(sizeof(char_u *) * size))) == NULL)
|
|
496 {
|
|
497 u_freeentry(uep, 0L);
|
|
498 goto nomem;
|
|
499 }
|
|
500 for (i = 0, lnum = top + 1; i < size; ++i)
|
|
501 {
|
481
|
502 fast_breakcheck();
|
|
503 if (got_int)
|
|
504 {
|
|
505 u_freeentry(uep, i);
|
|
506 return FAIL;
|
|
507 }
|
7
|
508 if ((uep->ue_array[i] = u_save_line(lnum++)) == NULL)
|
|
509 {
|
|
510 u_freeentry(uep, i);
|
|
511 goto nomem;
|
|
512 }
|
|
513 }
|
|
514 }
|
359
|
515 else
|
|
516 uep->ue_array = NULL;
|
7
|
517 uep->ue_next = curbuf->b_u_newhead->uh_entry;
|
|
518 curbuf->b_u_newhead->uh_entry = uep;
|
|
519 curbuf->b_u_synced = FALSE;
|
|
520 undo_undoes = FALSE;
|
|
521
|
|
522 return OK;
|
|
523
|
|
524 nomem:
|
|
525 msg_silent = 0; /* must display the prompt */
|
|
526 if (ask_yesno((char_u *)_("No undo possible; continue anyway"), TRUE)
|
|
527 == 'y')
|
|
528 {
|
|
529 undo_off = TRUE; /* will be reset when character typed */
|
|
530 return OK;
|
|
531 }
|
|
532 do_outofmem_msg((long_u)0);
|
|
533 return FAIL;
|
|
534 }
|
|
535
|
|
536 /*
|
|
537 * If 'cpoptions' contains 'u': Undo the previous undo or redo (vi compatible).
|
|
538 * If 'cpoptions' does not contain 'u': Always undo.
|
|
539 */
|
|
540 void
|
|
541 u_undo(count)
|
|
542 int count;
|
|
543 {
|
|
544 /*
|
|
545 * If we get an undo command while executing a macro, we behave like the
|
|
546 * original vi. If this happens twice in one macro the result will not
|
|
547 * be compatible.
|
|
548 */
|
|
549 if (curbuf->b_u_synced == FALSE)
|
|
550 {
|
|
551 u_sync();
|
|
552 count = 1;
|
|
553 }
|
|
554
|
|
555 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
|
|
556 undo_undoes = TRUE;
|
|
557 else
|
|
558 undo_undoes = !undo_undoes;
|
|
559 u_doit(count);
|
|
560 }
|
|
561
|
|
562 /*
|
|
563 * If 'cpoptions' contains 'u': Repeat the previous undo or redo.
|
|
564 * If 'cpoptions' does not contain 'u': Always redo.
|
|
565 */
|
|
566 void
|
|
567 u_redo(count)
|
|
568 int count;
|
|
569 {
|
|
570 if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
|
|
571 undo_undoes = FALSE;
|
|
572 u_doit(count);
|
|
573 }
|
|
574
|
|
575 /*
|
|
576 * Undo or redo, depending on 'undo_undoes', 'count' times.
|
|
577 */
|
|
578 static void
|
|
579 u_doit(count)
|
|
580 int count;
|
|
581 {
|
632
|
582 if (!undo_allowed())
|
7
|
583 return;
|
|
584
|
|
585 u_newcount = 0;
|
|
586 u_oldcount = 0;
|
179
|
587 if (curbuf->b_ml.ml_flags & ML_EMPTY)
|
|
588 u_oldcount = -1;
|
7
|
589 while (count--)
|
|
590 {
|
|
591 if (undo_undoes)
|
|
592 {
|
|
593 if (curbuf->b_u_curhead == NULL) /* first undo */
|
|
594 curbuf->b_u_curhead = curbuf->b_u_newhead;
|
|
595 else if (p_ul > 0) /* multi level undo */
|
|
596 /* get next undo */
|
|
597 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_next;
|
|
598 /* nothing to undo */
|
|
599 if (curbuf->b_u_numhead == 0 || curbuf->b_u_curhead == NULL)
|
|
600 {
|
|
601 /* stick curbuf->b_u_curhead at end */
|
|
602 curbuf->b_u_curhead = curbuf->b_u_oldhead;
|
|
603 beep_flush();
|
|
604 break;
|
|
605 }
|
|
606
|
|
607 u_undoredo();
|
|
608 }
|
|
609 else
|
|
610 {
|
|
611 if (curbuf->b_u_curhead == NULL || p_ul <= 0)
|
|
612 {
|
|
613 beep_flush(); /* nothing to redo */
|
|
614 break;
|
|
615 }
|
|
616
|
|
617 u_undoredo();
|
758
|
618 ++curbuf->b_u_seq_cur;
|
|
619 ++curbuf->b_u_seq_time;
|
753
|
620
|
|
621 /* Advance for next redo. Set "newhead" when at the end of the
|
|
622 * redoable changes. */
|
|
623 if (curbuf->b_u_curhead->uh_prev == NULL)
|
|
624 curbuf->b_u_newhead = curbuf->b_u_curhead;
|
7
|
625 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_prev;
|
|
626 }
|
|
627 }
|
753
|
628 u_undo_end();
|
|
629 }
|
|
630
|
|
631 static int lastmark = 0;
|
|
632
|
|
633 /*
|
|
634 * Undo or redo over the timeline.
|
|
635 * When "step" is negative go back in time, otherwise goes forward in time.
|
758
|
636 * When "sec" is FALSE make "step" steps, when "sec" is TRUE use "step" as
|
|
637 * seconds.
|
753
|
638 */
|
|
639 void
|
758
|
640 undo_time(step, sec)
|
|
641 long step;
|
|
642 int sec;
|
753
|
643 {
|
|
644 long target;
|
|
645 long closest;
|
758
|
646 long closest_start;
|
|
647 long closest_seq = 0;
|
|
648 long val;
|
|
649 long limit;
|
753
|
650 u_header_T *uhp;
|
|
651 u_header_T *last;
|
|
652 int mark;
|
|
653 int nomark;
|
|
654 int round;
|
758
|
655 int dosec = sec;
|
|
656 int above = FALSE;
|
753
|
657
|
|
658 u_newcount = 0;
|
|
659 u_oldcount = 0;
|
179
|
660 if (curbuf->b_ml.ml_flags & ML_EMPTY)
|
753
|
661 u_oldcount = -1;
|
|
662
|
|
663 /* "target" is the node below which we want to be. When going forward
|
|
664 * the current one also counts, thus do one less. */
|
|
665 if (step < 0)
|
|
666 {
|
758
|
667 if (sec)
|
|
668 target = (long)curbuf->b_u_seq_time + step;
|
|
669 else
|
|
670 target = curbuf->b_u_seq_cur + step;
|
|
671 if (target < 0)
|
|
672 target = -1;
|
|
673 closest = -2;
|
753
|
674 }
|
|
675 else
|
|
676 {
|
758
|
677 if (sec)
|
|
678 {
|
|
679 target = curbuf->b_u_seq_time + step - 1;
|
|
680 closest = time(NULL) + 1;
|
|
681 }
|
|
682 else
|
|
683 {
|
|
684 target = curbuf->b_u_seq_cur + step - 1;
|
|
685 closest = curbuf->b_u_seq_last + 1;
|
|
686 }
|
|
687 if (target >= closest)
|
|
688 target = closest - 1;
|
753
|
689 }
|
758
|
690 closest_start = closest;
|
|
691 if (sec)
|
|
692 limit = curbuf->b_u_seq_time + (step > 0 ? -1 : 1);
|
|
693 else
|
|
694 limit = curbuf->b_u_seq_cur;
|
753
|
695
|
758
|
696 /*
|
|
697 * May do this twice:
|
753
|
698 * 1. Search for "target", update "closest" to the best match found.
|
758
|
699 * 2. If "target" not found search for "closest".
|
|
700 *
|
|
701 * When using the closest time we use the sequence number in the second
|
|
702 * round, because there may be several entries with the same time.
|
|
703 */
|
753
|
704 for (round = 1; round <= 2; ++round)
|
|
705 {
|
|
706 /* Find the path from the current state to where we want to go. The
|
|
707 * desired state can be anywhere in the undo tree, need to go all over
|
|
708 * it. We put "nomark" in uh_walk where we have been without success,
|
|
709 * "mark" where it could possibly be. */
|
|
710 mark = ++lastmark;
|
|
711 nomark = ++lastmark;
|
|
712
|
|
713 if (curbuf->b_u_curhead == NULL) /* at leaf of the tree */
|
|
714 uhp = curbuf->b_u_newhead;
|
|
715 else
|
|
716 uhp = curbuf->b_u_curhead;
|
|
717
|
|
718 while (uhp != NULL)
|
|
719 {
|
|
720 uhp->uh_walk = mark;
|
758
|
721 val = (dosec ? uhp->uh_time : uhp->uh_seq);
|
753
|
722
|
758
|
723 if (round == 1)
|
|
724 {
|
|
725 /* Remember the header that is closest to the target.
|
|
726 * It must be at least in the right direction (checked with
|
|
727 * "limit"). When the timestamp is equal find the
|
|
728 * highest/lowest sequence number. */
|
|
729 if ((dosec && val == closest)
|
|
730 ? (step < 0
|
|
731 ? uhp->uh_seq < closest_seq
|
|
732 : uhp->uh_seq > closest_seq)
|
|
733 : (step < 0
|
|
734 ? (val < limit
|
|
735 && (closest > target
|
|
736 ? (val <= closest)
|
|
737 : (val >= closest)))
|
|
738 : (val > limit
|
|
739 && (closest < target
|
|
740 ? val >= closest
|
|
741 : val <= closest))))
|
|
742 {
|
|
743 closest = val;
|
|
744 closest_seq = uhp->uh_seq;
|
|
745 }
|
|
746 }
|
|
747
|
|
748 /* Quit searching when we found a match. But when searching for a
|
|
749 * time we need to continue looking for the best uh_seq. */
|
|
750 if (target == val && !dosec)
|
|
751 break;
|
753
|
752
|
|
753 /* go down in the tree if we haven't been there */
|
|
754 if (uhp->uh_prev != NULL && uhp->uh_prev->uh_walk != nomark
|
|
755 && uhp->uh_prev->uh_walk != mark)
|
|
756 uhp = uhp->uh_prev;
|
|
757
|
|
758 /* go to alternate branch if we haven't been there */
|
|
759 else if (uhp->uh_alt_next != NULL
|
|
760 && uhp->uh_alt_next->uh_walk != nomark
|
|
761 && uhp->uh_alt_next->uh_walk != mark)
|
|
762 uhp = uhp->uh_alt_next;
|
|
763
|
|
764 /* go up in the tree if we haven't been there and we are at the
|
|
765 * start of alternate branches */
|
|
766 else if (uhp->uh_next != NULL && uhp->uh_alt_prev == NULL
|
|
767 && uhp->uh_next->uh_walk != nomark
|
|
768 && uhp->uh_next->uh_walk != mark)
|
|
769 uhp = uhp->uh_next;
|
|
770
|
|
771 else
|
|
772 {
|
|
773 /* need to backtrack; mark this node as useless */
|
|
774 uhp->uh_walk = nomark;
|
|
775 if (uhp->uh_alt_prev != NULL)
|
|
776 uhp = uhp->uh_alt_prev;
|
|
777 else
|
|
778 uhp = uhp->uh_next;
|
|
779 }
|
|
780 }
|
|
781
|
|
782 if (uhp != NULL) /* found it */
|
|
783 break;
|
758
|
784 if (closest == closest_start)
|
753
|
785 {
|
758
|
786 if (step < 0)
|
|
787 MSG(_("Already at oldest change"));
|
|
788 else
|
|
789 MSG(_("Already at newest change"));
|
753
|
790 return;
|
|
791 }
|
|
792
|
758
|
793 target = closest_seq;
|
|
794 dosec = FALSE;
|
|
795 if (step < 0)
|
|
796 above = TRUE; /* stop above the header */
|
753
|
797 }
|
|
798
|
|
799 /* If we found it: Follow the path to go to where we want to be. */
|
|
800 if (uhp != NULL)
|
|
801 {
|
|
802 /*
|
|
803 * First go up the tree as much as needed.
|
|
804 */
|
|
805 for (;;)
|
|
806 {
|
|
807 uhp = curbuf->b_u_curhead;
|
|
808 if (uhp == NULL)
|
|
809 uhp = curbuf->b_u_newhead;
|
|
810 else
|
|
811 {
|
758
|
812 while (uhp->uh_alt_prev != NULL
|
|
813 && uhp->uh_alt_prev->uh_walk == mark)
|
753
|
814 {
|
|
815 uhp->uh_walk = nomark;
|
|
816 uhp = uhp->uh_alt_prev;
|
|
817 }
|
|
818 uhp = uhp->uh_next;
|
|
819 }
|
|
820 if (uhp == NULL || uhp->uh_walk != mark)
|
|
821 break;
|
|
822 curbuf->b_u_curhead = uhp;
|
|
823 u_undoredo();
|
|
824 uhp->uh_walk = nomark; /* don't go back down here */
|
|
825 }
|
|
826
|
|
827 /*
|
758
|
828 * And now go down the tree (redo), branching off where needed.
|
753
|
829 */
|
|
830 uhp = curbuf->b_u_curhead;
|
|
831 for (;;)
|
|
832 {
|
|
833 /* Find the last branch with a mark, that's the one. */
|
|
834 last = uhp;
|
|
835 while (last->uh_alt_next != NULL
|
|
836 && last->uh_alt_next->uh_walk == mark)
|
|
837 last = last->uh_alt_next;
|
|
838 if (last != uhp)
|
|
839 {
|
|
840 /* Make the used branch the first entry in the list of
|
|
841 * alternatives to make "u" and CTRL-R take this branch. */
|
|
842 if (last->uh_alt_next != NULL)
|
|
843 last->uh_alt_next->uh_alt_prev = last->uh_alt_prev;
|
|
844 last->uh_alt_prev->uh_alt_next = last->uh_alt_next;
|
|
845 last->uh_alt_prev = NULL;
|
|
846 last->uh_alt_next = uhp;
|
|
847 uhp->uh_alt_prev = last;
|
|
848
|
|
849 uhp = last;
|
|
850 }
|
758
|
851 curbuf->b_u_curhead = uhp;
|
|
852 curbuf->b_u_seq_cur = uhp->uh_seq;
|
|
853 curbuf->b_u_seq_time = uhp->uh_time;
|
753
|
854
|
|
855 if (uhp->uh_walk != mark)
|
|
856 break; /* must have reached the target */
|
|
857
|
758
|
858 /* Stop when going backwards in time and didn't find the exact
|
|
859 * header we were looking for. */
|
|
860 if (uhp->uh_seq == target && above)
|
|
861 break;
|
|
862
|
753
|
863 u_undoredo();
|
758
|
864 ++curbuf->b_u_seq_cur;
|
|
865 ++curbuf->b_u_seq_time;
|
753
|
866
|
|
867 /* Advance "curhead" to below the header we last used. If it
|
|
868 * becomes NULL then we need to set "newhead" to this leaf. */
|
|
869 if (uhp->uh_prev == NULL)
|
|
870 curbuf->b_u_newhead = uhp;
|
|
871 curbuf->b_u_curhead = uhp->uh_prev;
|
|
872
|
|
873 if (uhp->uh_seq == target) /* found it! */
|
|
874 break;
|
|
875
|
|
876 uhp = uhp->uh_prev;
|
|
877 if (uhp == NULL || uhp->uh_walk != mark)
|
|
878 {
|
|
879 EMSG2(_(e_intern2), "undo_time()");
|
|
880 break;
|
|
881 }
|
|
882 }
|
|
883 }
|
7
|
884 u_undo_end();
|
|
885 }
|
|
886
|
|
887 /*
|
|
888 * u_undoredo: common code for undo and redo
|
|
889 *
|
|
890 * The lines in the file are replaced by the lines in the entry list at
|
|
891 * curbuf->b_u_curhead. The replaced lines in the file are saved in the entry
|
|
892 * list for the next undo/redo.
|
|
893 */
|
|
894 static void
|
|
895 u_undoredo()
|
|
896 {
|
|
897 char_u **newarray = NULL;
|
|
898 linenr_T oldsize;
|
|
899 linenr_T newsize;
|
|
900 linenr_T top, bot;
|
|
901 linenr_T lnum;
|
|
902 linenr_T newlnum = MAXLNUM;
|
|
903 long i;
|
|
904 u_entry_T *uep, *nuep;
|
|
905 u_entry_T *newlist = NULL;
|
|
906 int old_flags;
|
|
907 int new_flags;
|
|
908 pos_T namedm[NMARKS];
|
692
|
909 #ifdef FEAT_VISUAL
|
|
910 visualinfo_T visualinfo;
|
|
911 #endif
|
7
|
912 int empty_buffer; /* buffer became empty */
|
758
|
913 u_header_T *curhead = curbuf->b_u_curhead;
|
7
|
914
|
758
|
915 old_flags = curhead->uh_flags;
|
7
|
916 new_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
|
|
917 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
|
|
918 setpcmark();
|
|
919
|
|
920 /*
|
|
921 * save marks before undo/redo
|
|
922 */
|
|
923 mch_memmove(namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
|
692
|
924 #ifdef FEAT_VISUAL
|
|
925 visualinfo = curbuf->b_visual;
|
|
926 #endif
|
7
|
927 curbuf->b_op_start.lnum = curbuf->b_ml.ml_line_count;
|
|
928 curbuf->b_op_start.col = 0;
|
|
929 curbuf->b_op_end.lnum = 0;
|
|
930 curbuf->b_op_end.col = 0;
|
|
931
|
758
|
932 for (uep = curhead->uh_entry; uep != NULL; uep = nuep)
|
7
|
933 {
|
|
934 top = uep->ue_top;
|
|
935 bot = uep->ue_bot;
|
|
936 if (bot == 0)
|
|
937 bot = curbuf->b_ml.ml_line_count + 1;
|
|
938 if (top > curbuf->b_ml.ml_line_count || top >= bot
|
|
939 || bot > curbuf->b_ml.ml_line_count + 1)
|
|
940 {
|
|
941 EMSG(_("E438: u_undo: line numbers wrong"));
|
|
942 changed(); /* don't want UNCHANGED now */
|
|
943 return;
|
|
944 }
|
|
945
|
|
946 oldsize = bot - top - 1; /* number of lines before undo */
|
|
947 newsize = uep->ue_size; /* number of lines after undo */
|
|
948
|
|
949 if (top < newlnum)
|
|
950 {
|
|
951 /* If the saved cursor is somewhere in this undo block, move it to
|
|
952 * the remembered position. Makes "gwap" put the cursor back
|
|
953 * where it was. */
|
758
|
954 lnum = curhead->uh_cursor.lnum;
|
7
|
955 if (lnum >= top && lnum <= top + newsize + 1)
|
|
956 {
|
758
|
957 curwin->w_cursor = curhead->uh_cursor;
|
7
|
958 newlnum = curwin->w_cursor.lnum - 1;
|
|
959 }
|
|
960 else
|
|
961 {
|
|
962 /* Use the first line that actually changed. Avoids that
|
|
963 * undoing auto-formatting puts the cursor in the previous
|
|
964 * line. */
|
|
965 for (i = 0; i < newsize && i < oldsize; ++i)
|
|
966 if (STRCMP(uep->ue_array[i], ml_get(top + 1 + i)) != 0)
|
|
967 break;
|
|
968 if (i == newsize && newlnum == MAXLNUM && uep->ue_next == NULL)
|
|
969 {
|
|
970 newlnum = top;
|
|
971 curwin->w_cursor.lnum = newlnum + 1;
|
|
972 }
|
|
973 else if (i < newsize)
|
|
974 {
|
|
975 newlnum = top + i;
|
|
976 curwin->w_cursor.lnum = newlnum + 1;
|
|
977 }
|
|
978 }
|
|
979 }
|
|
980
|
|
981 empty_buffer = FALSE;
|
|
982
|
|
983 /* delete the lines between top and bot and save them in newarray */
|
168
|
984 if (oldsize > 0)
|
7
|
985 {
|
168
|
986 if ((newarray = (char_u **)U_ALLOC_LINE(
|
7
|
987 (unsigned)(sizeof(char_u *) * oldsize))) == NULL)
|
|
988 {
|
|
989 do_outofmem_msg((long_u)(sizeof(char_u *) * oldsize));
|
|
990 /*
|
|
991 * We have messed up the entry list, repair is impossible.
|
|
992 * we have to free the rest of the list.
|
|
993 */
|
|
994 while (uep != NULL)
|
|
995 {
|
|
996 nuep = uep->ue_next;
|
|
997 u_freeentry(uep, uep->ue_size);
|
|
998 uep = nuep;
|
|
999 }
|
|
1000 break;
|
|
1001 }
|
|
1002 /* delete backwards, it goes faster in most cases */
|
|
1003 for (lnum = bot - 1, i = oldsize; --i >= 0; --lnum)
|
|
1004 {
|
|
1005 /* what can we do when we run out of memory? */
|
|
1006 if ((newarray[i] = u_save_line(lnum)) == NULL)
|
|
1007 do_outofmem_msg((long_u)0);
|
|
1008 /* remember we deleted the last line in the buffer, and a
|
|
1009 * dummy empty line will be inserted */
|
|
1010 if (curbuf->b_ml.ml_line_count == 1)
|
|
1011 empty_buffer = TRUE;
|
|
1012 ml_delete(lnum, FALSE);
|
|
1013 }
|
|
1014 }
|
414
|
1015 else
|
|
1016 newarray = NULL;
|
7
|
1017
|
|
1018 /* insert the lines in u_array between top and bot */
|
|
1019 if (newsize)
|
|
1020 {
|
|
1021 for (lnum = top, i = 0; i < newsize; ++i, ++lnum)
|
|
1022 {
|
|
1023 /*
|
|
1024 * If the file is empty, there is an empty line 1 that we
|
|
1025 * should get rid of, by replacing it with the new line
|
|
1026 */
|
|
1027 if (empty_buffer && lnum == 0)
|
|
1028 ml_replace((linenr_T)1, uep->ue_array[i], TRUE);
|
|
1029 else
|
|
1030 ml_append(lnum, uep->ue_array[i], (colnr_T)0, FALSE);
|
168
|
1031 U_FREE_LINE(uep->ue_array[i]);
|
7
|
1032 }
|
168
|
1033 U_FREE_LINE((char_u *)uep->ue_array);
|
7
|
1034 }
|
|
1035
|
|
1036 /* adjust marks */
|
|
1037 if (oldsize != newsize)
|
|
1038 {
|
|
1039 mark_adjust(top + 1, top + oldsize, (long)MAXLNUM,
|
|
1040 (long)newsize - (long)oldsize);
|
|
1041 if (curbuf->b_op_start.lnum > top + oldsize)
|
|
1042 curbuf->b_op_start.lnum += newsize - oldsize;
|
|
1043 if (curbuf->b_op_end.lnum > top + oldsize)
|
|
1044 curbuf->b_op_end.lnum += newsize - oldsize;
|
|
1045 }
|
|
1046
|
|
1047 changed_lines(top + 1, 0, bot, newsize - oldsize);
|
|
1048
|
|
1049 /* set '[ and '] mark */
|
|
1050 if (top + 1 < curbuf->b_op_start.lnum)
|
|
1051 curbuf->b_op_start.lnum = top + 1;
|
|
1052 if (newsize == 0 && top + 1 > curbuf->b_op_end.lnum)
|
|
1053 curbuf->b_op_end.lnum = top + 1;
|
|
1054 else if (top + newsize > curbuf->b_op_end.lnum)
|
|
1055 curbuf->b_op_end.lnum = top + newsize;
|
|
1056
|
|
1057 u_newcount += newsize;
|
|
1058 u_oldcount += oldsize;
|
|
1059 uep->ue_size = oldsize;
|
|
1060 uep->ue_array = newarray;
|
|
1061 uep->ue_bot = top + newsize + 1;
|
|
1062
|
|
1063 /*
|
|
1064 * insert this entry in front of the new entry list
|
|
1065 */
|
|
1066 nuep = uep->ue_next;
|
|
1067 uep->ue_next = newlist;
|
|
1068 newlist = uep;
|
|
1069 }
|
|
1070
|
758
|
1071 curhead->uh_entry = newlist;
|
|
1072 curhead->uh_flags = new_flags;
|
7
|
1073 if ((old_flags & UH_EMPTYBUF) && bufempty())
|
|
1074 curbuf->b_ml.ml_flags |= ML_EMPTY;
|
|
1075 if (old_flags & UH_CHANGED)
|
|
1076 changed();
|
|
1077 else
|
33
|
1078 #ifdef FEAT_NETBEANS_INTG
|
|
1079 /* per netbeans undo rules, keep it as modified */
|
|
1080 if (!isNetbeansModified(curbuf))
|
|
1081 #endif
|
7
|
1082 unchanged(curbuf, FALSE);
|
|
1083
|
|
1084 /*
|
|
1085 * restore marks from before undo/redo
|
|
1086 */
|
|
1087 for (i = 0; i < NMARKS; ++i)
|
758
|
1088 if (curhead->uh_namedm[i].lnum != 0)
|
7
|
1089 {
|
758
|
1090 curbuf->b_namedm[i] = curhead->uh_namedm[i];
|
|
1091 curhead->uh_namedm[i] = namedm[i];
|
7
|
1092 }
|
692
|
1093 #ifdef FEAT_VISUAL
|
758
|
1094 if (curhead->uh_visual.vi_start.lnum != 0)
|
692
|
1095 {
|
758
|
1096 curbuf->b_visual = curhead->uh_visual;
|
|
1097 curhead->uh_visual = visualinfo;
|
692
|
1098 }
|
|
1099 #endif
|
7
|
1100
|
|
1101 /*
|
|
1102 * If the cursor is only off by one line, put it at the same position as
|
|
1103 * before starting the change (for the "o" command).
|
|
1104 * Otherwise the cursor should go to the first undone line.
|
|
1105 */
|
758
|
1106 if (curhead->uh_cursor.lnum + 1 == curwin->w_cursor.lnum
|
7
|
1107 && curwin->w_cursor.lnum > 1)
|
|
1108 --curwin->w_cursor.lnum;
|
758
|
1109 if (curhead->uh_cursor.lnum == curwin->w_cursor.lnum)
|
7
|
1110 {
|
758
|
1111 curwin->w_cursor.col = curhead->uh_cursor.col;
|
7
|
1112 #ifdef FEAT_VIRTUALEDIT
|
758
|
1113 if (virtual_active() && curhead->uh_cursor_vcol >= 0)
|
|
1114 coladvance((colnr_T)curhead->uh_cursor_vcol);
|
7
|
1115 else
|
|
1116 curwin->w_cursor.coladd = 0;
|
|
1117 #endif
|
|
1118 }
|
|
1119 else if (curwin->w_cursor.lnum <= curbuf->b_ml.ml_line_count)
|
|
1120 beginline(BL_SOL | BL_FIX);
|
|
1121 else
|
|
1122 {
|
|
1123 /* We get here with the current cursor line being past the end (eg
|
|
1124 * after adding lines at the end of the file, and then undoing it).
|
|
1125 * check_cursor() will move the cursor to the last line. Move it to
|
|
1126 * the first column here. */
|
|
1127 curwin->w_cursor.col = 0;
|
|
1128 #ifdef FEAT_VIRTUALEDIT
|
|
1129 curwin->w_cursor.coladd = 0;
|
|
1130 #endif
|
|
1131 }
|
|
1132
|
|
1133 /* Make sure the cursor is on an existing line and column. */
|
|
1134 check_cursor();
|
758
|
1135
|
|
1136 /* Remember where we are for "g-" and ":earlier 10s". */
|
|
1137 curbuf->b_u_seq_cur = curhead->uh_seq;
|
|
1138 curbuf->b_u_seq_time = curhead->uh_time;
|
7
|
1139 }
|
|
1140
|
|
1141 /*
|
|
1142 * If we deleted or added lines, report the number of less/more lines.
|
|
1143 * Otherwise, report the number of changes (this may be incorrect
|
|
1144 * in some cases, but it's better than nothing).
|
|
1145 */
|
|
1146 static void
|
|
1147 u_undo_end()
|
|
1148 {
|
753
|
1149 long sec;
|
|
1150 char *msg;
|
|
1151
|
7
|
1152 #ifdef FEAT_FOLDING
|
|
1153 if ((fdo_flags & FDO_UNDO) && KeyTyped)
|
|
1154 foldOpenCursor();
|
|
1155 #endif
|
753
|
1156
|
|
1157 if (global_busy /* no messages now, wait until global is finished */
|
|
1158 || !messaging()) /* 'lazyredraw' set, don't do messages now */
|
|
1159 return;
|
|
1160
|
|
1161 if (curbuf->b_ml.ml_flags & ML_EMPTY)
|
|
1162 --u_newcount;
|
|
1163
|
|
1164 u_oldcount -= u_newcount;
|
|
1165 if (u_oldcount == -1)
|
|
1166 msg = N_("more line");
|
|
1167 else if (u_oldcount < 0)
|
|
1168 msg = N_("more lines");
|
|
1169 else if (u_oldcount == 1)
|
|
1170 msg = N_("line less");
|
|
1171 else if (u_oldcount > 1)
|
|
1172 msg = N_("fewer lines");
|
|
1173 else
|
|
1174 {
|
|
1175 u_oldcount = u_newcount;
|
|
1176 if (u_newcount == 1)
|
|
1177 msg = N_("change");
|
|
1178 else
|
|
1179 msg = N_("changes");
|
|
1180 }
|
|
1181
|
|
1182 if (curbuf->b_u_curhead == 0)
|
|
1183 sec = 0;
|
|
1184 else
|
|
1185 sec = time(NULL) - curbuf->b_u_curhead->uh_time;
|
|
1186
|
|
1187 smsg((char_u *)_("%ld %s; %ld seconds ago"), u_oldcount, _(msg), sec);
|
7
|
1188 }
|
|
1189
|
|
1190 /*
|
|
1191 * u_sync: stop adding to the current entry list
|
|
1192 */
|
|
1193 void
|
|
1194 u_sync()
|
|
1195 {
|
|
1196 if (curbuf->b_u_synced)
|
|
1197 return; /* already synced */
|
|
1198 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
|
|
1199 if (im_is_preediting())
|
|
1200 return; /* XIM is busy, don't break an undo sequence */
|
|
1201 #endif
|
|
1202 if (p_ul < 0)
|
|
1203 curbuf->b_u_synced = TRUE; /* no entries, nothing to do */
|
|
1204 else
|
|
1205 {
|
|
1206 u_getbot(); /* compute ue_bot of previous u_save */
|
|
1207 curbuf->b_u_curhead = NULL;
|
|
1208 }
|
|
1209 }
|
|
1210
|
|
1211 /*
|
697
|
1212 * ":undojoin": continue adding to the last entry list
|
|
1213 */
|
|
1214 /*ARGSUSED*/
|
|
1215 void
|
|
1216 ex_undojoin(eap)
|
|
1217 exarg_T *eap;
|
|
1218 {
|
|
1219 if (!curbuf->b_u_synced)
|
|
1220 return; /* already unsynced */
|
|
1221 if (curbuf->b_u_newhead == NULL)
|
|
1222 return; /* nothing changed before */
|
|
1223 if (p_ul < 0)
|
|
1224 return; /* no entries, nothing to do */
|
|
1225 else
|
|
1226 {
|
|
1227 /* Go back to the last entry */
|
|
1228 curbuf->b_u_curhead = curbuf->b_u_newhead;
|
|
1229 curbuf->b_u_synced = FALSE; /* no entries, nothing to do */
|
|
1230 }
|
|
1231 }
|
|
1232
|
|
1233 /*
|
7
|
1234 * Called after writing the file and setting b_changed to FALSE.
|
|
1235 * Now an undo means that the buffer is modified.
|
|
1236 */
|
|
1237 void
|
|
1238 u_unchanged(buf)
|
|
1239 buf_T *buf;
|
|
1240 {
|
758
|
1241 u_unch_branch(buf->b_u_oldhead);
|
|
1242 buf->b_did_warn = FALSE;
|
|
1243 }
|
|
1244
|
|
1245 static void
|
|
1246 u_unch_branch(uhp)
|
|
1247 u_header_T *uhp;
|
|
1248 {
|
753
|
1249 u_header_T *uh;
|
7
|
1250
|
758
|
1251 for (uh = uhp; uh != NULL; uh = uh->uh_prev)
|
|
1252 {
|
7
|
1253 uh->uh_flags |= UH_CHANGED;
|
758
|
1254 if (uh->uh_alt_next != NULL)
|
|
1255 u_unch_branch(uh->uh_alt_next); /* recursive */
|
|
1256 }
|
7
|
1257 }
|
|
1258
|
|
1259 /*
|
|
1260 * Get pointer to last added entry.
|
|
1261 * If it's not valid, give an error message and return NULL.
|
|
1262 */
|
|
1263 static u_entry_T *
|
|
1264 u_get_headentry()
|
|
1265 {
|
|
1266 if (curbuf->b_u_newhead == NULL || curbuf->b_u_newhead->uh_entry == NULL)
|
|
1267 {
|
|
1268 EMSG(_("E439: undo list corrupt"));
|
|
1269 return NULL;
|
|
1270 }
|
|
1271 return curbuf->b_u_newhead->uh_entry;
|
|
1272 }
|
|
1273
|
|
1274 /*
|
|
1275 * u_getbot(): compute the line number of the previous u_save
|
|
1276 * It is called only when b_u_synced is FALSE.
|
|
1277 */
|
|
1278 static void
|
|
1279 u_getbot()
|
|
1280 {
|
|
1281 u_entry_T *uep;
|
|
1282 linenr_T extra;
|
|
1283
|
|
1284 uep = u_get_headentry(); /* check for corrupt undo list */
|
|
1285 if (uep == NULL)
|
|
1286 return;
|
|
1287
|
|
1288 uep = curbuf->b_u_newhead->uh_getbot_entry;
|
|
1289 if (uep != NULL)
|
|
1290 {
|
|
1291 /*
|
|
1292 * the new ue_bot is computed from the number of lines that has been
|
|
1293 * inserted (0 - deleted) since calling u_save. This is equal to the
|
|
1294 * old line count subtracted from the current line count.
|
|
1295 */
|
|
1296 extra = curbuf->b_ml.ml_line_count - uep->ue_lcount;
|
|
1297 uep->ue_bot = uep->ue_top + uep->ue_size + 1 + extra;
|
|
1298 if (uep->ue_bot < 1 || uep->ue_bot > curbuf->b_ml.ml_line_count)
|
|
1299 {
|
|
1300 EMSG(_("E440: undo line missing"));
|
|
1301 uep->ue_bot = uep->ue_top + 1; /* assume all lines deleted, will
|
|
1302 * get all the old lines back
|
|
1303 * without deleting the current
|
|
1304 * ones */
|
|
1305 }
|
|
1306
|
|
1307 curbuf->b_u_newhead->uh_getbot_entry = NULL;
|
|
1308 }
|
|
1309
|
|
1310 curbuf->b_u_synced = TRUE;
|
|
1311 }
|
|
1312
|
|
1313 /*
|
753
|
1314 * Free one header and its entry list and adjust the pointers.
|
7
|
1315 */
|
|
1316 static void
|
758
|
1317 u_freeheader(buf, uhp, uhpp)
|
168
|
1318 buf_T *buf;
|
753
|
1319 u_header_T *uhp;
|
|
1320 u_header_T **uhpp; /* if not NULL reset when freeing this header */
|
7
|
1321 {
|
753
|
1322 /* When there is an alternate redo list free that branch completely,
|
|
1323 * because we can never go there. */
|
|
1324 if (uhp->uh_alt_next != NULL)
|
|
1325 u_freebranch(buf, uhp->uh_alt_next, uhpp);
|
7
|
1326
|
753
|
1327 if (uhp->uh_alt_prev != NULL)
|
|
1328 uhp->uh_alt_prev->uh_alt_next = NULL;
|
7
|
1329
|
753
|
1330 /* Update the links in the list to remove the header. */
|
7
|
1331 if (uhp->uh_next == NULL)
|
168
|
1332 buf->b_u_oldhead = uhp->uh_prev;
|
7
|
1333 else
|
|
1334 uhp->uh_next->uh_prev = uhp->uh_prev;
|
|
1335
|
|
1336 if (uhp->uh_prev == NULL)
|
168
|
1337 buf->b_u_newhead = uhp->uh_next;
|
7
|
1338 else
|
|
1339 uhp->uh_prev->uh_next = uhp->uh_next;
|
|
1340
|
753
|
1341 u_freeentries(buf, uhp, uhpp);
|
|
1342 }
|
|
1343
|
|
1344 /*
|
758
|
1345 * Free an alternate branch and any following alternate branches.
|
753
|
1346 */
|
|
1347 static void
|
|
1348 u_freebranch(buf, uhp, uhpp)
|
|
1349 buf_T *buf;
|
|
1350 u_header_T *uhp;
|
|
1351 u_header_T **uhpp; /* if not NULL reset when freeing this header */
|
|
1352 {
|
|
1353 u_header_T *tofree, *next;
|
|
1354
|
|
1355 if (uhp->uh_alt_prev != NULL)
|
|
1356 uhp->uh_alt_prev->uh_alt_next = NULL;
|
|
1357
|
|
1358 next = uhp;
|
|
1359 while (next != NULL)
|
|
1360 {
|
|
1361 tofree = next;
|
|
1362 if (tofree->uh_alt_next != NULL)
|
|
1363 u_freebranch(buf, tofree->uh_alt_next, uhpp); /* recursive */
|
|
1364 next = tofree->uh_prev;
|
|
1365 u_freeentries(buf, tofree, uhpp);
|
|
1366 }
|
|
1367 }
|
|
1368
|
|
1369 /*
|
|
1370 * Free all the undo entries for one header and the header itself.
|
|
1371 * This means that "uhp" is invalid when returning.
|
|
1372 */
|
|
1373 static void
|
|
1374 u_freeentries(buf, uhp, uhpp)
|
|
1375 buf_T *buf;
|
|
1376 u_header_T *uhp;
|
|
1377 u_header_T **uhpp; /* if not NULL reset when freeing this header */
|
|
1378 {
|
|
1379 u_entry_T *uep, *nuep;
|
|
1380
|
|
1381 /* Check for pointers to the header that become invalid now. */
|
|
1382 if (buf->b_u_curhead == uhp)
|
|
1383 buf->b_u_curhead = NULL;
|
|
1384 if (uhpp != NULL && uhp == *uhpp)
|
|
1385 *uhpp = NULL;
|
|
1386
|
|
1387 for (uep = uhp->uh_entry; uep != NULL; uep = nuep)
|
|
1388 {
|
|
1389 nuep = uep->ue_next;
|
|
1390 u_freeentry(uep, uep->ue_size);
|
|
1391 }
|
|
1392
|
168
|
1393 U_FREE_LINE((char_u *)uhp);
|
|
1394 --buf->b_u_numhead;
|
7
|
1395 }
|
|
1396
|
|
1397 /*
|
|
1398 * free entry 'uep' and 'n' lines in uep->ue_array[]
|
|
1399 */
|
|
1400 static void
|
|
1401 u_freeentry(uep, n)
|
|
1402 u_entry_T *uep;
|
|
1403 long n;
|
|
1404 {
|
414
|
1405 while (n > 0)
|
168
|
1406 U_FREE_LINE(uep->ue_array[--n]);
|
359
|
1407 U_FREE_LINE((char_u *)uep->ue_array);
|
168
|
1408 U_FREE_LINE((char_u *)uep);
|
7
|
1409 }
|
|
1410
|
|
1411 /*
|
|
1412 * invalidate the undo buffer; called when storage has already been released
|
|
1413 */
|
|
1414 void
|
|
1415 u_clearall(buf)
|
|
1416 buf_T *buf;
|
|
1417 {
|
|
1418 buf->b_u_newhead = buf->b_u_oldhead = buf->b_u_curhead = NULL;
|
|
1419 buf->b_u_synced = TRUE;
|
|
1420 buf->b_u_numhead = 0;
|
|
1421 buf->b_u_line_ptr = NULL;
|
|
1422 buf->b_u_line_lnum = 0;
|
|
1423 }
|
|
1424
|
|
1425 /*
|
|
1426 * save the line "lnum" for the "U" command
|
|
1427 */
|
|
1428 void
|
|
1429 u_saveline(lnum)
|
|
1430 linenr_T lnum;
|
|
1431 {
|
|
1432 if (lnum == curbuf->b_u_line_lnum) /* line is already saved */
|
|
1433 return;
|
356
|
1434 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) /* should never happen */
|
7
|
1435 return;
|
|
1436 u_clearline();
|
|
1437 curbuf->b_u_line_lnum = lnum;
|
|
1438 if (curwin->w_cursor.lnum == lnum)
|
|
1439 curbuf->b_u_line_colnr = curwin->w_cursor.col;
|
|
1440 else
|
|
1441 curbuf->b_u_line_colnr = 0;
|
|
1442 if ((curbuf->b_u_line_ptr = u_save_line(lnum)) == NULL)
|
|
1443 do_outofmem_msg((long_u)0);
|
|
1444 }
|
|
1445
|
|
1446 /*
|
|
1447 * clear the line saved for the "U" command
|
|
1448 * (this is used externally for crossing a line while in insert mode)
|
|
1449 */
|
|
1450 void
|
|
1451 u_clearline()
|
|
1452 {
|
|
1453 if (curbuf->b_u_line_ptr != NULL)
|
|
1454 {
|
168
|
1455 U_FREE_LINE(curbuf->b_u_line_ptr);
|
7
|
1456 curbuf->b_u_line_ptr = NULL;
|
|
1457 curbuf->b_u_line_lnum = 0;
|
|
1458 }
|
|
1459 }
|
|
1460
|
|
1461 /*
|
|
1462 * Implementation of the "U" command.
|
|
1463 * Differentiation from vi: "U" can be undone with the next "U".
|
|
1464 * We also allow the cursor to be in another line.
|
|
1465 */
|
|
1466 void
|
|
1467 u_undoline()
|
|
1468 {
|
|
1469 colnr_T t;
|
|
1470 char_u *oldp;
|
|
1471
|
|
1472 if (undo_off)
|
|
1473 return;
|
|
1474
|
|
1475 if (curbuf->b_u_line_ptr == NULL ||
|
|
1476 curbuf->b_u_line_lnum > curbuf->b_ml.ml_line_count)
|
|
1477 {
|
|
1478 beep_flush();
|
|
1479 return;
|
|
1480 }
|
|
1481 /* first save the line for the 'u' command */
|
|
1482 if (u_savecommon(curbuf->b_u_line_lnum - 1,
|
|
1483 curbuf->b_u_line_lnum + 1, (linenr_T)0) == FAIL)
|
|
1484 return;
|
|
1485 oldp = u_save_line(curbuf->b_u_line_lnum);
|
|
1486 if (oldp == NULL)
|
|
1487 {
|
|
1488 do_outofmem_msg((long_u)0);
|
|
1489 return;
|
|
1490 }
|
|
1491 ml_replace(curbuf->b_u_line_lnum, curbuf->b_u_line_ptr, TRUE);
|
|
1492 changed_bytes(curbuf->b_u_line_lnum, 0);
|
168
|
1493 U_FREE_LINE(curbuf->b_u_line_ptr);
|
7
|
1494 curbuf->b_u_line_ptr = oldp;
|
|
1495
|
|
1496 t = curbuf->b_u_line_colnr;
|
|
1497 if (curwin->w_cursor.lnum == curbuf->b_u_line_lnum)
|
|
1498 curbuf->b_u_line_colnr = curwin->w_cursor.col;
|
|
1499 curwin->w_cursor.col = t;
|
|
1500 curwin->w_cursor.lnum = curbuf->b_u_line_lnum;
|
|
1501 }
|
|
1502
|
|
1503 /*
|
168
|
1504 * There are two implementations of the memory management for undo:
|
|
1505 * 1. Use the standard malloc()/free() functions.
|
|
1506 * This should be fast for allocating memory, but when a buffer is
|
|
1507 * abandoned every single allocated chunk must be freed, which may be slow.
|
|
1508 * 2. Allocate larger blocks of memory and keep track of chunks ourselves.
|
|
1509 * This is fast for abandoning, but the use of linked lists is slow for
|
|
1510 * finding a free chunk. Esp. when a lot of lines are changed or deleted.
|
|
1511 * A bit of profiling showed that the first method is faster, especially when
|
|
1512 * making a large number of changes, under the condition that malloc()/free()
|
|
1513 * is implemented efficiently.
|
|
1514 */
|
|
1515 #ifdef U_USE_MALLOC
|
|
1516 /*
|
|
1517 * Version of undo memory allocation using malloc()/free()
|
|
1518 *
|
|
1519 * U_FREE_LINE() and U_ALLOC_LINE() are macros that invoke vim_free() and
|
|
1520 * lalloc() directly.
|
|
1521 */
|
|
1522
|
|
1523 /*
|
|
1524 * Free all allocated memory blocks for the buffer 'buf'.
|
|
1525 */
|
|
1526 void
|
|
1527 u_blockfree(buf)
|
|
1528 buf_T *buf;
|
|
1529 {
|
753
|
1530 while (buf->b_u_oldhead != NULL)
|
758
|
1531 u_freeheader(buf, buf->b_u_oldhead, NULL);
|
359
|
1532 U_FREE_LINE(buf->b_u_line_ptr);
|
168
|
1533 }
|
|
1534
|
|
1535 #else
|
|
1536 /*
|
|
1537 * Storage allocation for the undo lines and blocks of the current file.
|
|
1538 * Version where Vim keeps track of the available memory.
|
7
|
1539 */
|
|
1540
|
|
1541 /*
|
|
1542 * Memory is allocated in relatively large blocks. These blocks are linked
|
|
1543 * in the allocated block list, headed by curbuf->b_block_head. They are all
|
|
1544 * freed when abandoning a file, so we don't have to free every single line.
|
|
1545 * The list is kept sorted on memory address.
|
|
1546 * block_alloc() allocates a block.
|
|
1547 * m_blockfree() frees all blocks.
|
|
1548 *
|
|
1549 * The available chunks of memory are kept in free chunk lists. There is
|
|
1550 * one free list for each block of allocated memory. The list is kept sorted
|
|
1551 * on memory address.
|
|
1552 * u_alloc_line() gets a chunk from the free lists.
|
|
1553 * u_free_line() returns a chunk to the free lists.
|
|
1554 * curbuf->b_m_search points to the chunk before the chunk that was
|
|
1555 * freed/allocated the last time.
|
|
1556 * curbuf->b_mb_current points to the b_head where curbuf->b_m_search
|
|
1557 * points into the free list.
|
|
1558 *
|
|
1559 *
|
|
1560 * b_block_head /---> block #1 /---> block #2
|
|
1561 * mb_next ---/ mb_next ---/ mb_next ---> NULL
|
|
1562 * mb_info mb_info mb_info
|
|
1563 * | | |
|
|
1564 * V V V
|
|
1565 * NULL free chunk #1.1 free chunk #2.1
|
|
1566 * | |
|
|
1567 * V V
|
|
1568 * free chunk #1.2 NULL
|
|
1569 * |
|
|
1570 * V
|
|
1571 * NULL
|
|
1572 *
|
|
1573 * When a single free chunk list would have been used, it could take a lot
|
|
1574 * of time in u_free_line() to find the correct place to insert a chunk in the
|
|
1575 * free list. The single free list would become very long when many lines are
|
|
1576 * changed (e.g. with :%s/^M$//).
|
|
1577 */
|
|
1578
|
|
1579 /*
|
|
1580 * this blocksize is used when allocating new lines
|
|
1581 */
|
|
1582 #define MEMBLOCKSIZE 2044
|
|
1583
|
|
1584 /*
|
|
1585 * The size field contains the size of the chunk, including the size field
|
|
1586 * itself.
|
|
1587 *
|
|
1588 * When the chunk is not in-use it is preceded with the m_info structure.
|
|
1589 * The m_next field links it in one of the free chunk lists.
|
|
1590 *
|
|
1591 * On most unix systems structures have to be longword (32 or 64 bit) aligned.
|
|
1592 * On most other systems they are short (16 bit) aligned.
|
|
1593 */
|
|
1594
|
|
1595 /* the structure definitions are now in structs.h */
|
|
1596
|
|
1597 #ifdef ALIGN_LONG
|
|
1598 /* size of m_size */
|
|
1599 # define M_OFFSET (sizeof(long_u))
|
|
1600 #else
|
|
1601 /* size of m_size */
|
|
1602 # define M_OFFSET (sizeof(short_u))
|
|
1603 #endif
|
|
1604
|
168
|
1605 static char_u *u_blockalloc __ARGS((long_u));
|
|
1606
|
7
|
1607 /*
|
|
1608 * Allocate a block of memory and link it in the allocated block list.
|
|
1609 */
|
|
1610 static char_u *
|
|
1611 u_blockalloc(size)
|
|
1612 long_u size;
|
|
1613 {
|
|
1614 mblock_T *p;
|
|
1615 mblock_T *mp, *next;
|
|
1616
|
|
1617 p = (mblock_T *)lalloc(size + sizeof(mblock_T), FALSE);
|
|
1618 if (p != NULL)
|
|
1619 {
|
|
1620 /* Insert the block into the allocated block list, keeping it
|
|
1621 sorted on address. */
|
|
1622 for (mp = &curbuf->b_block_head;
|
|
1623 (next = mp->mb_next) != NULL && next < p;
|
|
1624 mp = next)
|
|
1625 ;
|
|
1626 p->mb_next = next; /* link in block list */
|
|
1627 p->mb_size = size;
|
168
|
1628 p->mb_maxsize = 0; /* nothing free yet */
|
7
|
1629 mp->mb_next = p;
|
|
1630 p->mb_info.m_next = NULL; /* clear free list */
|
|
1631 p->mb_info.m_size = 0;
|
|
1632 curbuf->b_mb_current = p; /* remember current block */
|
|
1633 curbuf->b_m_search = NULL;
|
|
1634 p++; /* return usable memory */
|
|
1635 }
|
|
1636 return (char_u *)p;
|
|
1637 }
|
|
1638
|
|
1639 /*
|
|
1640 * free all allocated memory blocks for the buffer 'buf'
|
|
1641 */
|
|
1642 void
|
|
1643 u_blockfree(buf)
|
|
1644 buf_T *buf;
|
|
1645 {
|
|
1646 mblock_T *p, *np;
|
|
1647
|
|
1648 for (p = buf->b_block_head.mb_next; p != NULL; p = np)
|
|
1649 {
|
|
1650 np = p->mb_next;
|
|
1651 vim_free(p);
|
|
1652 }
|
|
1653 buf->b_block_head.mb_next = NULL;
|
|
1654 buf->b_m_search = NULL;
|
|
1655 buf->b_mb_current = NULL;
|
|
1656 }
|
|
1657
|
|
1658 /*
|
|
1659 * Free a chunk of memory for the current buffer.
|
|
1660 * Insert the chunk into the correct free list, keeping it sorted on address.
|
|
1661 */
|
|
1662 static void
|
|
1663 u_free_line(ptr, keep)
|
|
1664 char_u *ptr;
|
|
1665 int keep; /* don't free the block when it's empty */
|
|
1666 {
|
|
1667 minfo_T *next;
|
|
1668 minfo_T *prev, *curr;
|
|
1669 minfo_T *mp;
|
|
1670 mblock_T *nextb;
|
|
1671 mblock_T *prevb;
|
168
|
1672 long_u maxsize;
|
7
|
1673
|
|
1674 if (ptr == NULL || ptr == IObuff)
|
|
1675 return; /* illegal address can happen in out-of-memory situations */
|
|
1676
|
|
1677 mp = (minfo_T *)(ptr - M_OFFSET);
|
|
1678
|
|
1679 /* find block where chunk could be a part off */
|
|
1680 /* if we change curbuf->b_mb_current, curbuf->b_m_search is set to NULL */
|
|
1681 if (curbuf->b_mb_current == NULL || mp < (minfo_T *)curbuf->b_mb_current)
|
|
1682 {
|
|
1683 curbuf->b_mb_current = curbuf->b_block_head.mb_next;
|
|
1684 curbuf->b_m_search = NULL;
|
|
1685 }
|
|
1686 if ((nextb = curbuf->b_mb_current->mb_next) != NULL
|
|
1687 && (minfo_T *)nextb < mp)
|
|
1688 {
|
|
1689 curbuf->b_mb_current = nextb;
|
|
1690 curbuf->b_m_search = NULL;
|
|
1691 }
|
|
1692 while ((nextb = curbuf->b_mb_current->mb_next) != NULL
|
|
1693 && (minfo_T *)nextb < mp)
|
|
1694 curbuf->b_mb_current = nextb;
|
|
1695
|
|
1696 curr = NULL;
|
|
1697 /*
|
|
1698 * If mp is smaller than curbuf->b_m_search->m_next go to the start of
|
|
1699 * the free list
|
|
1700 */
|
|
1701 if (curbuf->b_m_search == NULL || mp < (curbuf->b_m_search->m_next))
|
|
1702 next = &(curbuf->b_mb_current->mb_info);
|
|
1703 else
|
|
1704 next = curbuf->b_m_search;
|
|
1705 /*
|
|
1706 * The following loop is executed very often.
|
|
1707 * Therefore it has been optimized at the cost of readability.
|
|
1708 * Keep it fast!
|
|
1709 */
|
|
1710 #ifdef SLOW_BUT_EASY_TO_READ
|
|
1711 do
|
|
1712 {
|
|
1713 prev = curr;
|
|
1714 curr = next;
|
|
1715 next = next->m_next;
|
|
1716 }
|
|
1717 while (mp > next && next != NULL);
|
|
1718 #else
|
|
1719 do /* first, middle, last */
|
|
1720 {
|
|
1721 prev = next->m_next; /* curr, next, prev */
|
|
1722 if (prev == NULL || mp <= prev)
|
|
1723 {
|
|
1724 prev = curr;
|
|
1725 curr = next;
|
|
1726 next = next->m_next;
|
|
1727 break;
|
|
1728 }
|
|
1729 curr = prev->m_next; /* next, prev, curr */
|
|
1730 if (curr == NULL || mp <= curr)
|
|
1731 {
|
|
1732 prev = next;
|
|
1733 curr = prev->m_next;
|
|
1734 next = curr->m_next;
|
|
1735 break;
|
|
1736 }
|
|
1737 next = curr->m_next; /* prev, curr, next */
|
|
1738 }
|
|
1739 while (mp > next && next != NULL);
|
|
1740 #endif
|
|
1741
|
|
1742 /* if *mp and *next are concatenated, join them into one chunk */
|
|
1743 if ((char_u *)mp + mp->m_size == (char_u *)next)
|
|
1744 {
|
|
1745 mp->m_size += next->m_size;
|
|
1746 mp->m_next = next->m_next;
|
|
1747 }
|
|
1748 else
|
|
1749 mp->m_next = next;
|
168
|
1750 maxsize = mp->m_size;
|
7
|
1751
|
|
1752 /* if *curr and *mp are concatenated, join them */
|
|
1753 if (prev != NULL && (char_u *)curr + curr->m_size == (char_u *)mp)
|
|
1754 {
|
|
1755 curr->m_size += mp->m_size;
|
168
|
1756 maxsize = curr->m_size;
|
7
|
1757 curr->m_next = mp->m_next;
|
|
1758 curbuf->b_m_search = prev;
|
|
1759 }
|
|
1760 else
|
|
1761 {
|
|
1762 curr->m_next = mp;
|
|
1763 curbuf->b_m_search = curr; /* put curbuf->b_m_search before freed
|
|
1764 chunk */
|
|
1765 }
|
|
1766
|
|
1767 /*
|
|
1768 * If the block only containes free memory now, release it.
|
|
1769 */
|
|
1770 if (!keep && curbuf->b_mb_current->mb_size
|
|
1771 == curbuf->b_mb_current->mb_info.m_next->m_size)
|
|
1772 {
|
|
1773 /* Find the block before the current one to be able to unlink it from
|
|
1774 * the list of blocks. */
|
|
1775 prevb = &curbuf->b_block_head;
|
|
1776 for (nextb = prevb->mb_next; nextb != curbuf->b_mb_current;
|
|
1777 nextb = nextb->mb_next)
|
|
1778 prevb = nextb;
|
|
1779 prevb->mb_next = nextb->mb_next;
|
|
1780 vim_free(nextb);
|
|
1781 curbuf->b_mb_current = NULL;
|
|
1782 curbuf->b_m_search = NULL;
|
|
1783 }
|
168
|
1784 else if (curbuf->b_mb_current->mb_maxsize < maxsize)
|
|
1785 curbuf->b_mb_current->mb_maxsize = maxsize;
|
7
|
1786 }
|
|
1787
|
|
1788 /*
|
|
1789 * Allocate and initialize a new line structure with room for at least
|
|
1790 * 'size' characters plus a terminating NUL.
|
|
1791 */
|
|
1792 static char_u *
|
|
1793 u_alloc_line(size)
|
|
1794 unsigned size;
|
|
1795 {
|
|
1796 minfo_T *mp, *mprev, *mp2;
|
|
1797 mblock_T *mbp;
|
|
1798 int size_align;
|
|
1799
|
|
1800 /*
|
|
1801 * Add room for size field and trailing NUL byte.
|
|
1802 * Adjust for minimal size (must be able to store minfo_T
|
|
1803 * plus a trailing NUL, so the chunk can be released again)
|
|
1804 */
|
|
1805 size += M_OFFSET + 1;
|
|
1806 if (size < sizeof(minfo_T) + 1)
|
|
1807 size = sizeof(minfo_T) + 1;
|
|
1808
|
|
1809 /*
|
|
1810 * round size up for alignment
|
|
1811 */
|
|
1812 size_align = (size + ALIGN_MASK) & ~ALIGN_MASK;
|
|
1813
|
|
1814 /*
|
|
1815 * If curbuf->b_m_search is NULL (uninitialized free list) start at
|
|
1816 * curbuf->b_block_head
|
|
1817 */
|
|
1818 if (curbuf->b_mb_current == NULL || curbuf->b_m_search == NULL)
|
|
1819 {
|
|
1820 curbuf->b_mb_current = &curbuf->b_block_head;
|
|
1821 curbuf->b_m_search = &(curbuf->b_block_head.mb_info);
|
|
1822 }
|
|
1823
|
168
|
1824 /* Search for a block with enough space. */
|
7
|
1825 mbp = curbuf->b_mb_current;
|
168
|
1826 while (mbp->mb_maxsize < size_align)
|
7
|
1827 {
|
168
|
1828 if (mbp->mb_next != NULL)
|
7
|
1829 mbp = mbp->mb_next;
|
|
1830 else
|
|
1831 mbp = &curbuf->b_block_head;
|
168
|
1832 if (mbp == curbuf->b_mb_current)
|
7
|
1833 {
|
168
|
1834 int n = (size_align > (MEMBLOCKSIZE / 4)
|
|
1835 ? size_align : MEMBLOCKSIZE);
|
7
|
1836
|
168
|
1837 /* Back where we started in block list: need to add a new block
|
|
1838 * with enough space. */
|
|
1839 mp = (minfo_T *)u_blockalloc((long_u)n);
|
|
1840 if (mp == NULL)
|
|
1841 return (NULL);
|
|
1842 mp->m_size = n;
|
|
1843 u_free_line((char_u *)mp + M_OFFSET, TRUE);
|
|
1844 mbp = curbuf->b_mb_current;
|
|
1845 break;
|
|
1846 }
|
|
1847 }
|
|
1848 if (mbp != curbuf->b_mb_current)
|
|
1849 curbuf->b_m_search = &(mbp->mb_info);
|
|
1850
|
|
1851 /* In this block find a chunk with enough space. */
|
|
1852 mprev = curbuf->b_m_search;
|
|
1853 mp = curbuf->b_m_search->m_next;
|
407
|
1854 for (;;)
|
168
|
1855 {
|
|
1856 if (mp == NULL) /* at end of the list */
|
|
1857 mp = &(mbp->mb_info); /* wrap around to begin */
|
|
1858 if (mp->m_size >= size)
|
|
1859 break;
|
|
1860 if (mp == curbuf->b_m_search)
|
|
1861 {
|
|
1862 /* back where we started in free chunk list: "cannot happen" */
|
|
1863 EMSG2(_(e_intern2), "u_alloc_line()");
|
|
1864 return NULL;
|
7
|
1865 }
|
|
1866 mprev = mp;
|
168
|
1867 mp = mp->m_next;
|
7
|
1868 }
|
|
1869
|
168
|
1870 /* when using the largest chunk adjust mb_maxsize */
|
|
1871 if (mp->m_size >= mbp->mb_maxsize)
|
|
1872 mbp->mb_maxsize = 0;
|
|
1873
|
7
|
1874 /* if the chunk we found is large enough, split it up in two */
|
|
1875 if ((long)mp->m_size - size_align >= (long)(sizeof(minfo_T) + 1))
|
|
1876 {
|
|
1877 mp2 = (minfo_T *)((char_u *)mp + size_align);
|
|
1878 mp2->m_size = mp->m_size - size_align;
|
|
1879 mp2->m_next = mp->m_next;
|
|
1880 mprev->m_next = mp2;
|
|
1881 mp->m_size = size_align;
|
|
1882 }
|
|
1883 else /* remove *mp from the free list */
|
|
1884 {
|
|
1885 mprev->m_next = mp->m_next;
|
|
1886 }
|
|
1887 curbuf->b_m_search = mprev;
|
|
1888 curbuf->b_mb_current = mbp;
|
|
1889
|
168
|
1890 /* If using the largest chunk need to find the new largest chunk */
|
|
1891 if (mbp->mb_maxsize == 0)
|
|
1892 for (mp2 = &(mbp->mb_info); mp2 != NULL; mp2 = mp2->m_next)
|
|
1893 if (mbp->mb_maxsize < mp2->m_size)
|
|
1894 mbp->mb_maxsize = mp2->m_size;
|
|
1895
|
7
|
1896 mp = (minfo_T *)((char_u *)mp + M_OFFSET);
|
|
1897 *(char_u *)mp = NUL; /* set the first byte to NUL */
|
|
1898
|
|
1899 return ((char_u *)mp);
|
|
1900 }
|
168
|
1901 #endif
|
7
|
1902
|
|
1903 /*
|
|
1904 * u_save_line(): allocate memory with u_alloc_line() and copy line 'lnum'
|
|
1905 * into it.
|
|
1906 */
|
|
1907 static char_u *
|
|
1908 u_save_line(lnum)
|
|
1909 linenr_T lnum;
|
|
1910 {
|
|
1911 char_u *src;
|
|
1912 char_u *dst;
|
|
1913 unsigned len;
|
|
1914
|
|
1915 src = ml_get(lnum);
|
|
1916 len = (unsigned)STRLEN(src);
|
168
|
1917 if ((dst = U_ALLOC_LINE(len)) != NULL)
|
7
|
1918 mch_memmove(dst, src, (size_t)(len + 1));
|
|
1919 return (dst);
|
|
1920 }
|
|
1921
|
|
1922 /*
|
|
1923 * Check if the 'modified' flag is set, or 'ff' has changed (only need to
|
|
1924 * check the first character, because it can only be "dos", "unix" or "mac").
|
|
1925 * "nofile" and "scratch" type buffers are considered to always be unchanged.
|
|
1926 */
|
|
1927 int
|
|
1928 bufIsChanged(buf)
|
|
1929 buf_T *buf;
|
|
1930 {
|
|
1931 return
|
|
1932 #ifdef FEAT_QUICKFIX
|
|
1933 !bt_dontwrite(buf) &&
|
|
1934 #endif
|
|
1935 (buf->b_changed || file_ff_differs(buf));
|
|
1936 }
|
|
1937
|
|
1938 int
|
|
1939 curbufIsChanged()
|
|
1940 {
|
|
1941 return
|
|
1942 #ifdef FEAT_QUICKFIX
|
|
1943 !bt_dontwrite(curbuf) &&
|
|
1944 #endif
|
|
1945 (curbuf->b_changed || file_ff_differs(curbuf));
|
|
1946 }
|