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