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));
|
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
|
753
|
353 uhp->uh_seq = curbuf->b_u_seq_last++;
|
|
354 curbuf->b_u_seq_cur = curbuf->b_u_seq_last;
|
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
|
|
582 u_doit(count)
|
|
583 int count;
|
|
584 {
|
632
|
585 if (!undo_allowed())
|
7
|
586 return;
|
|
587
|
|
588 u_newcount = 0;
|
|
589 u_oldcount = 0;
|
179
|
590 if (curbuf->b_ml.ml_flags & ML_EMPTY)
|
|
591 u_oldcount = -1;
|
7
|
592 while (count--)
|
|
593 {
|
|
594 if (undo_undoes)
|
|
595 {
|
|
596 if (curbuf->b_u_curhead == NULL) /* first undo */
|
|
597 curbuf->b_u_curhead = curbuf->b_u_newhead;
|
|
598 else if (p_ul > 0) /* multi level undo */
|
|
599 /* get next undo */
|
|
600 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_next;
|
|
601 /* nothing to undo */
|
|
602 if (curbuf->b_u_numhead == 0 || curbuf->b_u_curhead == NULL)
|
|
603 {
|
|
604 /* stick curbuf->b_u_curhead at end */
|
|
605 curbuf->b_u_curhead = curbuf->b_u_oldhead;
|
|
606 beep_flush();
|
|
607 break;
|
|
608 }
|
|
609
|
|
610 u_undoredo();
|
|
611 }
|
|
612 else
|
|
613 {
|
|
614 if (curbuf->b_u_curhead == NULL || p_ul <= 0)
|
|
615 {
|
|
616 beep_flush(); /* nothing to redo */
|
|
617 break;
|
|
618 }
|
|
619
|
|
620 u_undoredo();
|
758
|
621 ++curbuf->b_u_seq_cur;
|
|
622 ++curbuf->b_u_seq_time;
|
753
|
623
|
|
624 /* Advance for next redo. Set "newhead" when at the end of the
|
|
625 * redoable changes. */
|
|
626 if (curbuf->b_u_curhead->uh_prev == NULL)
|
|
627 curbuf->b_u_newhead = curbuf->b_u_curhead;
|
7
|
628 curbuf->b_u_curhead = curbuf->b_u_curhead->uh_prev;
|
|
629 }
|
|
630 }
|
753
|
631 u_undo_end();
|
|
632 }
|
|
633
|
|
634 static int lastmark = 0;
|
|
635
|
|
636 /*
|
|
637 * Undo or redo over the timeline.
|
|
638 * When "step" is negative go back in time, otherwise goes forward in time.
|
758
|
639 * When "sec" is FALSE make "step" steps, when "sec" is TRUE use "step" as
|
|
640 * seconds.
|
772
|
641 * When "absolute" is TRUE use "step" as the sequence number to jump to.
|
|
642 * "sec" must be FALSE then.
|
753
|
643 */
|
|
644 void
|
772
|
645 undo_time(step, sec, absolute)
|
758
|
646 long step;
|
|
647 int sec;
|
772
|
648 int absolute;
|
753
|
649 {
|
|
650 long target;
|
|
651 long closest;
|
758
|
652 long closest_start;
|
|
653 long closest_seq = 0;
|
|
654 long val;
|
|
655 long limit;
|
753
|
656 u_header_T *uhp;
|
|
657 u_header_T *last;
|
|
658 int mark;
|
|
659 int nomark;
|
|
660 int round;
|
758
|
661 int dosec = sec;
|
|
662 int above = FALSE;
|
753
|
663
|
766
|
664 /* First make sure the current undoable change is synced. */
|
|
665 if (curbuf->b_u_synced == FALSE)
|
|
666 u_sync();
|
|
667
|
753
|
668 u_newcount = 0;
|
|
669 u_oldcount = 0;
|
179
|
670 if (curbuf->b_ml.ml_flags & ML_EMPTY)
|
753
|
671 u_oldcount = -1;
|
|
672
|
|
673 /* "target" is the node below which we want to be. When going forward
|
|
674 * the current one also counts, thus do one less. */
|
772
|
675 if (absolute)
|
|
676 {
|
|
677 target = step;
|
|
678 closest = -2;
|
|
679 }
|
|
680 else if (step < 0)
|
753
|
681 {
|
758
|
682 if (sec)
|
|
683 target = (long)curbuf->b_u_seq_time + step;
|
|
684 else
|
|
685 target = curbuf->b_u_seq_cur + step;
|
|
686 if (target < 0)
|
|
687 target = -1;
|
|
688 closest = -2;
|
753
|
689 }
|
|
690 else
|
|
691 {
|
758
|
692 if (sec)
|
|
693 {
|
|
694 target = curbuf->b_u_seq_time + step - 1;
|
|
695 closest = time(NULL) + 1;
|
|
696 }
|
|
697 else
|
|
698 {
|
|
699 target = curbuf->b_u_seq_cur + step - 1;
|
|
700 closest = curbuf->b_u_seq_last + 1;
|
|
701 }
|
|
702 if (target >= closest)
|
|
703 target = closest - 1;
|
753
|
704 }
|
758
|
705 closest_start = closest;
|
|
706 if (sec)
|
|
707 limit = curbuf->b_u_seq_time + (step > 0 ? -1 : 1);
|
|
708 else
|
|
709 limit = curbuf->b_u_seq_cur;
|
753
|
710
|
758
|
711 /*
|
|
712 * May do this twice:
|
753
|
713 * 1. Search for "target", update "closest" to the best match found.
|
758
|
714 * 2. If "target" not found search for "closest".
|
|
715 *
|
|
716 * When using the closest time we use the sequence number in the second
|
|
717 * round, because there may be several entries with the same time.
|
|
718 */
|
753
|
719 for (round = 1; round <= 2; ++round)
|
|
720 {
|
|
721 /* Find the path from the current state to where we want to go. The
|
|
722 * desired state can be anywhere in the undo tree, need to go all over
|
|
723 * it. We put "nomark" in uh_walk where we have been without success,
|
|
724 * "mark" where it could possibly be. */
|
|
725 mark = ++lastmark;
|
|
726 nomark = ++lastmark;
|
|
727
|
|
728 if (curbuf->b_u_curhead == NULL) /* at leaf of the tree */
|
|
729 uhp = curbuf->b_u_newhead;
|
|
730 else
|
|
731 uhp = curbuf->b_u_curhead;
|
|
732
|
|
733 while (uhp != NULL)
|
|
734 {
|
|
735 uhp->uh_walk = mark;
|
758
|
736 val = (dosec ? uhp->uh_time : uhp->uh_seq);
|
753
|
737
|
758
|
738 if (round == 1)
|
|
739 {
|
|
740 /* Remember the header that is closest to the target.
|
|
741 * It must be at least in the right direction (checked with
|
|
742 * "limit"). When the timestamp is equal find the
|
|
743 * highest/lowest sequence number. */
|
|
744 if ((dosec && val == closest)
|
|
745 ? (step < 0
|
|
746 ? uhp->uh_seq < closest_seq
|
|
747 : uhp->uh_seq > closest_seq)
|
|
748 : (step < 0
|
|
749 ? (val < limit
|
|
750 && (closest > target
|
|
751 ? (val <= closest)
|
|
752 : (val >= closest)))
|
|
753 : (val > limit
|
|
754 && (closest < target
|
|
755 ? val >= closest
|
|
756 : val <= closest))))
|
|
757 {
|
|
758 closest = val;
|
|
759 closest_seq = uhp->uh_seq;
|
|
760 }
|
|
761 }
|
|
762
|
|
763 /* Quit searching when we found a match. But when searching for a
|
|
764 * time we need to continue looking for the best uh_seq. */
|
|
765 if (target == val && !dosec)
|
|
766 break;
|
753
|
767
|
|
768 /* go down in the tree if we haven't been there */
|
|
769 if (uhp->uh_prev != NULL && uhp->uh_prev->uh_walk != nomark
|
|
770 && uhp->uh_prev->uh_walk != mark)
|
|
771 uhp = uhp->uh_prev;
|
|
772
|
|
773 /* go to alternate branch if we haven't been there */
|
|
774 else if (uhp->uh_alt_next != NULL
|
|
775 && uhp->uh_alt_next->uh_walk != nomark
|
|
776 && uhp->uh_alt_next->uh_walk != mark)
|
|
777 uhp = uhp->uh_alt_next;
|
|
778
|
|
779 /* go up in the tree if we haven't been there and we are at the
|
|
780 * start of alternate branches */
|
|
781 else if (uhp->uh_next != NULL && uhp->uh_alt_prev == NULL
|
|
782 && uhp->uh_next->uh_walk != nomark
|
|
783 && uhp->uh_next->uh_walk != mark)
|
|
784 uhp = uhp->uh_next;
|
|
785
|
|
786 else
|
|
787 {
|
|
788 /* need to backtrack; mark this node as useless */
|
|
789 uhp->uh_walk = nomark;
|
|
790 if (uhp->uh_alt_prev != NULL)
|
|
791 uhp = uhp->uh_alt_prev;
|
|
792 else
|
|
793 uhp = uhp->uh_next;
|
|
794 }
|
|
795 }
|
|
796
|
|
797 if (uhp != NULL) /* found it */
|
|
798 break;
|
772
|
799
|
|
800 if (absolute)
|
|
801 {
|
|
802 EMSGN(_("Undo number %ld not found"), step);
|
|
803 return;
|
|
804 }
|
|
805
|
758
|
806 if (closest == closest_start)
|
753
|
807 {
|
758
|
808 if (step < 0)
|
|
809 MSG(_("Already at oldest change"));
|
|
810 else
|
|
811 MSG(_("Already at newest change"));
|
753
|
812 return;
|
|
813 }
|
|
814
|
758
|
815 target = closest_seq;
|
|
816 dosec = FALSE;
|
|
817 if (step < 0)
|
|
818 above = TRUE; /* stop above the header */
|
753
|
819 }
|
|
820
|
|
821 /* If we found it: Follow the path to go to where we want to be. */
|
|
822 if (uhp != NULL)
|
|
823 {
|
|
824 /*
|
|
825 * First go up the tree as much as needed.
|
|
826 */
|
|
827 for (;;)
|
|
828 {
|
|
829 uhp = curbuf->b_u_curhead;
|
|
830 if (uhp == NULL)
|
|
831 uhp = curbuf->b_u_newhead;
|
|
832 else
|
|
833 {
|
758
|
834 while (uhp->uh_alt_prev != NULL
|
|
835 && uhp->uh_alt_prev->uh_walk == mark)
|
753
|
836 {
|
|
837 uhp->uh_walk = nomark;
|
|
838 uhp = uhp->uh_alt_prev;
|
|
839 }
|
|
840 uhp = uhp->uh_next;
|
|
841 }
|
|
842 if (uhp == NULL || uhp->uh_walk != mark)
|
|
843 break;
|
|
844 curbuf->b_u_curhead = uhp;
|
|
845 u_undoredo();
|
|
846 uhp->uh_walk = nomark; /* don't go back down here */
|
|
847 }
|
|
848
|
|
849 /*
|
758
|
850 * And now go down the tree (redo), branching off where needed.
|
753
|
851 */
|
|
852 uhp = curbuf->b_u_curhead;
|
|
853 for (;;)
|
|
854 {
|
|
855 /* Find the last branch with a mark, that's the one. */
|
|
856 last = uhp;
|
|
857 while (last->uh_alt_next != NULL
|
|
858 && last->uh_alt_next->uh_walk == mark)
|
|
859 last = last->uh_alt_next;
|
|
860 if (last != uhp)
|
|
861 {
|
|
862 /* Make the used branch the first entry in the list of
|
|
863 * alternatives to make "u" and CTRL-R take this branch. */
|
|
864 if (last->uh_alt_next != NULL)
|
|
865 last->uh_alt_next->uh_alt_prev = last->uh_alt_prev;
|
|
866 last->uh_alt_prev->uh_alt_next = last->uh_alt_next;
|
|
867 last->uh_alt_prev = NULL;
|
|
868 last->uh_alt_next = uhp;
|
|
869 uhp->uh_alt_prev = last;
|
|
870
|
|
871 uhp = last;
|
|
872 }
|
758
|
873 curbuf->b_u_curhead = uhp;
|
|
874 curbuf->b_u_seq_cur = uhp->uh_seq;
|
|
875 curbuf->b_u_seq_time = uhp->uh_time;
|
753
|
876
|
|
877 if (uhp->uh_walk != mark)
|
|
878 break; /* must have reached the target */
|
|
879
|
758
|
880 /* Stop when going backwards in time and didn't find the exact
|
|
881 * header we were looking for. */
|
|
882 if (uhp->uh_seq == target && above)
|
|
883 break;
|
|
884
|
753
|
885 u_undoredo();
|
758
|
886 ++curbuf->b_u_seq_cur;
|
|
887 ++curbuf->b_u_seq_time;
|
753
|
888
|
|
889 /* Advance "curhead" to below the header we last used. If it
|
|
890 * becomes NULL then we need to set "newhead" to this leaf. */
|
|
891 if (uhp->uh_prev == NULL)
|
|
892 curbuf->b_u_newhead = uhp;
|
|
893 curbuf->b_u_curhead = uhp->uh_prev;
|
|
894
|
|
895 if (uhp->uh_seq == target) /* found it! */
|
|
896 break;
|
|
897
|
|
898 uhp = uhp->uh_prev;
|
|
899 if (uhp == NULL || uhp->uh_walk != mark)
|
|
900 {
|
|
901 EMSG2(_(e_intern2), "undo_time()");
|
|
902 break;
|
|
903 }
|
|
904 }
|
|
905 }
|
7
|
906 u_undo_end();
|
|
907 }
|
|
908
|
|
909 /*
|
|
910 * u_undoredo: common code for undo and redo
|
|
911 *
|
|
912 * The lines in the file are replaced by the lines in the entry list at
|
|
913 * curbuf->b_u_curhead. The replaced lines in the file are saved in the entry
|
|
914 * list for the next undo/redo.
|
|
915 */
|
|
916 static void
|
|
917 u_undoredo()
|
|
918 {
|
|
919 char_u **newarray = NULL;
|
|
920 linenr_T oldsize;
|
|
921 linenr_T newsize;
|
|
922 linenr_T top, bot;
|
|
923 linenr_T lnum;
|
|
924 linenr_T newlnum = MAXLNUM;
|
|
925 long i;
|
|
926 u_entry_T *uep, *nuep;
|
|
927 u_entry_T *newlist = NULL;
|
|
928 int old_flags;
|
|
929 int new_flags;
|
|
930 pos_T namedm[NMARKS];
|
692
|
931 #ifdef FEAT_VISUAL
|
|
932 visualinfo_T visualinfo;
|
|
933 #endif
|
7
|
934 int empty_buffer; /* buffer became empty */
|
758
|
935 u_header_T *curhead = curbuf->b_u_curhead;
|
7
|
936
|
758
|
937 old_flags = curhead->uh_flags;
|
7
|
938 new_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
|
|
939 ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
|
|
940 setpcmark();
|
|
941
|
|
942 /*
|
|
943 * save marks before undo/redo
|
|
944 */
|
|
945 mch_memmove(namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
|
692
|
946 #ifdef FEAT_VISUAL
|
|
947 visualinfo = curbuf->b_visual;
|
|
948 #endif
|
7
|
949 curbuf->b_op_start.lnum = curbuf->b_ml.ml_line_count;
|
|
950 curbuf->b_op_start.col = 0;
|
|
951 curbuf->b_op_end.lnum = 0;
|
|
952 curbuf->b_op_end.col = 0;
|
|
953
|
758
|
954 for (uep = curhead->uh_entry; uep != NULL; uep = nuep)
|
7
|
955 {
|
|
956 top = uep->ue_top;
|
|
957 bot = uep->ue_bot;
|
|
958 if (bot == 0)
|
|
959 bot = curbuf->b_ml.ml_line_count + 1;
|
|
960 if (top > curbuf->b_ml.ml_line_count || top >= bot
|
|
961 || bot > curbuf->b_ml.ml_line_count + 1)
|
|
962 {
|
|
963 EMSG(_("E438: u_undo: line numbers wrong"));
|
|
964 changed(); /* don't want UNCHANGED now */
|
|
965 return;
|
|
966 }
|
|
967
|
|
968 oldsize = bot - top - 1; /* number of lines before undo */
|
|
969 newsize = uep->ue_size; /* number of lines after undo */
|
|
970
|
|
971 if (top < newlnum)
|
|
972 {
|
|
973 /* If the saved cursor is somewhere in this undo block, move it to
|
|
974 * the remembered position. Makes "gwap" put the cursor back
|
|
975 * where it was. */
|
758
|
976 lnum = curhead->uh_cursor.lnum;
|
7
|
977 if (lnum >= top && lnum <= top + newsize + 1)
|
|
978 {
|
758
|
979 curwin->w_cursor = curhead->uh_cursor;
|
7
|
980 newlnum = curwin->w_cursor.lnum - 1;
|
|
981 }
|
|
982 else
|
|
983 {
|
|
984 /* Use the first line that actually changed. Avoids that
|
|
985 * undoing auto-formatting puts the cursor in the previous
|
|
986 * line. */
|
|
987 for (i = 0; i < newsize && i < oldsize; ++i)
|
|
988 if (STRCMP(uep->ue_array[i], ml_get(top + 1 + i)) != 0)
|
|
989 break;
|
|
990 if (i == newsize && newlnum == MAXLNUM && uep->ue_next == NULL)
|
|
991 {
|
|
992 newlnum = top;
|
|
993 curwin->w_cursor.lnum = newlnum + 1;
|
|
994 }
|
|
995 else if (i < newsize)
|
|
996 {
|
|
997 newlnum = top + i;
|
|
998 curwin->w_cursor.lnum = newlnum + 1;
|
|
999 }
|
|
1000 }
|
|
1001 }
|
|
1002
|
|
1003 empty_buffer = FALSE;
|
|
1004
|
|
1005 /* delete the lines between top and bot and save them in newarray */
|
168
|
1006 if (oldsize > 0)
|
7
|
1007 {
|
168
|
1008 if ((newarray = (char_u **)U_ALLOC_LINE(
|
7
|
1009 (unsigned)(sizeof(char_u *) * oldsize))) == NULL)
|
|
1010 {
|
|
1011 do_outofmem_msg((long_u)(sizeof(char_u *) * oldsize));
|
|
1012 /*
|
|
1013 * We have messed up the entry list, repair is impossible.
|
|
1014 * we have to free the rest of the list.
|
|
1015 */
|
|
1016 while (uep != NULL)
|
|
1017 {
|
|
1018 nuep = uep->ue_next;
|
|
1019 u_freeentry(uep, uep->ue_size);
|
|
1020 uep = nuep;
|
|
1021 }
|
|
1022 break;
|
|
1023 }
|
|
1024 /* delete backwards, it goes faster in most cases */
|
|
1025 for (lnum = bot - 1, i = oldsize; --i >= 0; --lnum)
|
|
1026 {
|
|
1027 /* what can we do when we run out of memory? */
|
|
1028 if ((newarray[i] = u_save_line(lnum)) == NULL)
|
|
1029 do_outofmem_msg((long_u)0);
|
|
1030 /* remember we deleted the last line in the buffer, and a
|
|
1031 * dummy empty line will be inserted */
|
|
1032 if (curbuf->b_ml.ml_line_count == 1)
|
|
1033 empty_buffer = TRUE;
|
|
1034 ml_delete(lnum, FALSE);
|
|
1035 }
|
|
1036 }
|
414
|
1037 else
|
|
1038 newarray = NULL;
|
7
|
1039
|
|
1040 /* insert the lines in u_array between top and bot */
|
|
1041 if (newsize)
|
|
1042 {
|
|
1043 for (lnum = top, i = 0; i < newsize; ++i, ++lnum)
|
|
1044 {
|
|
1045 /*
|
|
1046 * If the file is empty, there is an empty line 1 that we
|
|
1047 * should get rid of, by replacing it with the new line
|
|
1048 */
|
|
1049 if (empty_buffer && lnum == 0)
|
|
1050 ml_replace((linenr_T)1, uep->ue_array[i], TRUE);
|
|
1051 else
|
|
1052 ml_append(lnum, uep->ue_array[i], (colnr_T)0, FALSE);
|
168
|
1053 U_FREE_LINE(uep->ue_array[i]);
|
7
|
1054 }
|
168
|
1055 U_FREE_LINE((char_u *)uep->ue_array);
|
7
|
1056 }
|
|
1057
|
|
1058 /* adjust marks */
|
|
1059 if (oldsize != newsize)
|
|
1060 {
|
|
1061 mark_adjust(top + 1, top + oldsize, (long)MAXLNUM,
|
|
1062 (long)newsize - (long)oldsize);
|
|
1063 if (curbuf->b_op_start.lnum > top + oldsize)
|
|
1064 curbuf->b_op_start.lnum += newsize - oldsize;
|
|
1065 if (curbuf->b_op_end.lnum > top + oldsize)
|
|
1066 curbuf->b_op_end.lnum += newsize - oldsize;
|
|
1067 }
|
|
1068
|
|
1069 changed_lines(top + 1, 0, bot, newsize - oldsize);
|
|
1070
|
|
1071 /* set '[ and '] mark */
|
|
1072 if (top + 1 < curbuf->b_op_start.lnum)
|
|
1073 curbuf->b_op_start.lnum = top + 1;
|
|
1074 if (newsize == 0 && top + 1 > curbuf->b_op_end.lnum)
|
|
1075 curbuf->b_op_end.lnum = top + 1;
|
|
1076 else if (top + newsize > curbuf->b_op_end.lnum)
|
|
1077 curbuf->b_op_end.lnum = top + newsize;
|
|
1078
|
|
1079 u_newcount += newsize;
|
|
1080 u_oldcount += oldsize;
|
|
1081 uep->ue_size = oldsize;
|
|
1082 uep->ue_array = newarray;
|
|
1083 uep->ue_bot = top + newsize + 1;
|
|
1084
|
|
1085 /*
|
|
1086 * insert this entry in front of the new entry list
|
|
1087 */
|
|
1088 nuep = uep->ue_next;
|
|
1089 uep->ue_next = newlist;
|
|
1090 newlist = uep;
|
|
1091 }
|
|
1092
|
758
|
1093 curhead->uh_entry = newlist;
|
|
1094 curhead->uh_flags = new_flags;
|
7
|
1095 if ((old_flags & UH_EMPTYBUF) && bufempty())
|
|
1096 curbuf->b_ml.ml_flags |= ML_EMPTY;
|
|
1097 if (old_flags & UH_CHANGED)
|
|
1098 changed();
|
|
1099 else
|
33
|
1100 #ifdef FEAT_NETBEANS_INTG
|
|
1101 /* per netbeans undo rules, keep it as modified */
|
|
1102 if (!isNetbeansModified(curbuf))
|
|
1103 #endif
|
7
|
1104 unchanged(curbuf, FALSE);
|
|
1105
|
|
1106 /*
|
|
1107 * restore marks from before undo/redo
|
|
1108 */
|
|
1109 for (i = 0; i < NMARKS; ++i)
|
758
|
1110 if (curhead->uh_namedm[i].lnum != 0)
|
7
|
1111 {
|
758
|
1112 curbuf->b_namedm[i] = curhead->uh_namedm[i];
|
|
1113 curhead->uh_namedm[i] = namedm[i];
|
7
|
1114 }
|
692
|
1115 #ifdef FEAT_VISUAL
|
758
|
1116 if (curhead->uh_visual.vi_start.lnum != 0)
|
692
|
1117 {
|
758
|
1118 curbuf->b_visual = curhead->uh_visual;
|
|
1119 curhead->uh_visual = visualinfo;
|
692
|
1120 }
|
|
1121 #endif
|
7
|
1122
|
|
1123 /*
|
|
1124 * If the cursor is only off by one line, put it at the same position as
|
|
1125 * before starting the change (for the "o" command).
|
|
1126 * Otherwise the cursor should go to the first undone line.
|
|
1127 */
|
758
|
1128 if (curhead->uh_cursor.lnum + 1 == curwin->w_cursor.lnum
|
7
|
1129 && curwin->w_cursor.lnum > 1)
|
|
1130 --curwin->w_cursor.lnum;
|
758
|
1131 if (curhead->uh_cursor.lnum == curwin->w_cursor.lnum)
|
7
|
1132 {
|
758
|
1133 curwin->w_cursor.col = curhead->uh_cursor.col;
|
7
|
1134 #ifdef FEAT_VIRTUALEDIT
|
758
|
1135 if (virtual_active() && curhead->uh_cursor_vcol >= 0)
|
|
1136 coladvance((colnr_T)curhead->uh_cursor_vcol);
|
7
|
1137 else
|
|
1138 curwin->w_cursor.coladd = 0;
|
|
1139 #endif
|
|
1140 }
|
|
1141 else if (curwin->w_cursor.lnum <= curbuf->b_ml.ml_line_count)
|
|
1142 beginline(BL_SOL | BL_FIX);
|
|
1143 else
|
|
1144 {
|
|
1145 /* We get here with the current cursor line being past the end (eg
|
|
1146 * after adding lines at the end of the file, and then undoing it).
|
|
1147 * check_cursor() will move the cursor to the last line. Move it to
|
|
1148 * the first column here. */
|
|
1149 curwin->w_cursor.col = 0;
|
|
1150 #ifdef FEAT_VIRTUALEDIT
|
|
1151 curwin->w_cursor.coladd = 0;
|
|
1152 #endif
|
|
1153 }
|
|
1154
|
|
1155 /* Make sure the cursor is on an existing line and column. */
|
|
1156 check_cursor();
|
758
|
1157
|
|
1158 /* Remember where we are for "g-" and ":earlier 10s". */
|
|
1159 curbuf->b_u_seq_cur = curhead->uh_seq;
|
|
1160 curbuf->b_u_seq_time = curhead->uh_time;
|
7
|
1161 }
|
|
1162
|
|
1163 /*
|
|
1164 * If we deleted or added lines, report the number of less/more lines.
|
|
1165 * Otherwise, report the number of changes (this may be incorrect
|
|
1166 * in some cases, but it's better than nothing).
|
|
1167 */
|
|
1168 static void
|
|
1169 u_undo_end()
|
|
1170 {
|
753
|
1171 char *msg;
|
772
|
1172 u_header_T *uhp;
|
|
1173 char_u msgbuf[80];
|
753
|
1174
|
7
|
1175 #ifdef FEAT_FOLDING
|
|
1176 if ((fdo_flags & FDO_UNDO) && KeyTyped)
|
|
1177 foldOpenCursor();
|
|
1178 #endif
|
753
|
1179
|
|
1180 if (global_busy /* no messages now, wait until global is finished */
|
|
1181 || !messaging()) /* 'lazyredraw' set, don't do messages now */
|
|
1182 return;
|
|
1183
|
|
1184 if (curbuf->b_ml.ml_flags & ML_EMPTY)
|
|
1185 --u_newcount;
|
|
1186
|
|
1187 u_oldcount -= u_newcount;
|
|
1188 if (u_oldcount == -1)
|
|
1189 msg = N_("more line");
|
|
1190 else if (u_oldcount < 0)
|
|
1191 msg = N_("more lines");
|
|
1192 else if (u_oldcount == 1)
|
|
1193 msg = N_("line less");
|
|
1194 else if (u_oldcount > 1)
|
|
1195 msg = N_("fewer lines");
|
|
1196 else
|
|
1197 {
|
|
1198 u_oldcount = u_newcount;
|
|
1199 if (u_newcount == 1)
|
|
1200 msg = N_("change");
|
|
1201 else
|
|
1202 msg = N_("changes");
|
|
1203 }
|
|
1204
|
772
|
1205 if (curbuf->b_u_curhead != NULL)
|
|
1206 uhp = curbuf->b_u_curhead;
|
753
|
1207 else
|
772
|
1208 uhp = curbuf->b_u_newhead;
|
753
|
1209
|
772
|
1210 if (uhp == NULL)
|
|
1211 *msgbuf = NUL;
|
|
1212 else
|
|
1213 u_add_time(msgbuf, sizeof(msgbuf), uhp->uh_time);
|
|
1214
|
|
1215 smsg((char_u *)_("%ld %s; #%ld %s"), u_oldcount, _(msg),
|
|
1216 uhp == NULL ? 0L : uhp->uh_seq, msgbuf);
|
7
|
1217 }
|
|
1218
|
|
1219 /*
|
|
1220 * u_sync: stop adding to the current entry list
|
|
1221 */
|
|
1222 void
|
|
1223 u_sync()
|
|
1224 {
|
|
1225 if (curbuf->b_u_synced)
|
|
1226 return; /* already synced */
|
|
1227 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
|
|
1228 if (im_is_preediting())
|
|
1229 return; /* XIM is busy, don't break an undo sequence */
|
|
1230 #endif
|
|
1231 if (p_ul < 0)
|
|
1232 curbuf->b_u_synced = TRUE; /* no entries, nothing to do */
|
|
1233 else
|
|
1234 {
|
|
1235 u_getbot(); /* compute ue_bot of previous u_save */
|
|
1236 curbuf->b_u_curhead = NULL;
|
|
1237 }
|
|
1238 }
|
|
1239
|
|
1240 /*
|
772
|
1241 * ":undolist": List the leafs of the undo tree
|
|
1242 */
|
|
1243 /*ARGSUSED*/
|
|
1244 void
|
|
1245 ex_undolist(eap)
|
|
1246 exarg_T *eap;
|
|
1247 {
|
|
1248 garray_T ga;
|
|
1249 u_header_T *uhp;
|
|
1250 int mark;
|
|
1251 int nomark;
|
|
1252 int changes = 1;
|
|
1253 int i;
|
|
1254
|
|
1255 /*
|
|
1256 * 1: walk the tree to find all leafs, put the info in "ga".
|
|
1257 * 2: sort the lines
|
|
1258 * 3: display the list
|
|
1259 */
|
|
1260 mark = ++lastmark;
|
|
1261 nomark = ++lastmark;
|
|
1262 ga_init2(&ga, (int)sizeof(char *), 20);
|
|
1263
|
|
1264 uhp = curbuf->b_u_oldhead;
|
|
1265 while (uhp != NULL)
|
|
1266 {
|
|
1267 if (uhp->uh_prev == NULL)
|
|
1268 {
|
|
1269 if (ga_grow(&ga, 1) == FAIL)
|
|
1270 break;
|
|
1271 vim_snprintf((char *)IObuff, IOSIZE, "%6ld %7ld ",
|
|
1272 uhp->uh_seq, changes);
|
|
1273 u_add_time(IObuff + STRLEN(IObuff), IOSIZE - STRLEN(IObuff),
|
|
1274 uhp->uh_time);
|
|
1275 ((char_u **)(ga.ga_data))[ga.ga_len++] = vim_strsave(IObuff);
|
|
1276 }
|
|
1277
|
|
1278 uhp->uh_walk = mark;
|
|
1279
|
|
1280 /* go down in the tree if we haven't been there */
|
|
1281 if (uhp->uh_prev != NULL && uhp->uh_prev->uh_walk != nomark
|
|
1282 && uhp->uh_prev->uh_walk != mark)
|
|
1283 {
|
|
1284 uhp = uhp->uh_prev;
|
|
1285 ++changes;
|
|
1286 }
|
|
1287
|
|
1288 /* go to alternate branch if we haven't been there */
|
|
1289 else if (uhp->uh_alt_next != NULL
|
|
1290 && uhp->uh_alt_next->uh_walk != nomark
|
|
1291 && uhp->uh_alt_next->uh_walk != mark)
|
|
1292 uhp = uhp->uh_alt_next;
|
|
1293
|
|
1294 /* go up in the tree if we haven't been there and we are at the
|
|
1295 * start of alternate branches */
|
|
1296 else if (uhp->uh_next != NULL && uhp->uh_alt_prev == NULL
|
|
1297 && uhp->uh_next->uh_walk != nomark
|
|
1298 && uhp->uh_next->uh_walk != mark)
|
|
1299 {
|
|
1300 uhp = uhp->uh_next;
|
|
1301 --changes;
|
|
1302 }
|
|
1303
|
|
1304 else
|
|
1305 {
|
|
1306 /* need to backtrack; mark this node as done */
|
|
1307 uhp->uh_walk = nomark;
|
|
1308 if (uhp->uh_alt_prev != NULL)
|
|
1309 uhp = uhp->uh_alt_prev;
|
|
1310 else
|
|
1311 {
|
|
1312 uhp = uhp->uh_next;
|
|
1313 --changes;
|
|
1314 }
|
|
1315 }
|
|
1316 }
|
|
1317
|
|
1318 if (ga.ga_len == 0)
|
|
1319 MSG(_("Nothing to undo"));
|
|
1320 else
|
|
1321 {
|
|
1322 sort_strings((char_u **)ga.ga_data, ga.ga_len);
|
|
1323
|
|
1324 msg_start();
|
|
1325 msg_puts_attr((char_u *)_("number changes time"), hl_attr(HLF_T));
|
|
1326 for (i = 0; i < ga.ga_len && !got_int; ++i)
|
|
1327 {
|
|
1328 msg_putchar('\n');
|
|
1329 if (got_int)
|
|
1330 break;
|
|
1331 msg_puts(((char_u **)ga.ga_data)[i]);
|
|
1332 }
|
|
1333 msg_end();
|
|
1334
|
|
1335 ga_clear_strings(&ga);
|
|
1336 }
|
|
1337 }
|
|
1338
|
|
1339 /*
|
|
1340 * Put the timestamp of an undo header in "buf[buflen]" in a nice format.
|
|
1341 */
|
|
1342 static void
|
|
1343 u_add_time(buf, buflen, tt)
|
|
1344 char_u *buf;
|
|
1345 size_t buflen;
|
|
1346 time_t tt;
|
|
1347 {
|
|
1348 #ifdef HAVE_STRFTIME
|
|
1349 struct tm *curtime;
|
|
1350
|
|
1351 if (time(NULL) - tt >= 100)
|
|
1352 {
|
|
1353 curtime = localtime(&tt);
|
|
1354 (void)strftime((char *)buf, buflen, "%T", curtime);
|
|
1355 }
|
|
1356 else
|
|
1357 #endif
|
|
1358 vim_snprintf((char *)buf, buflen, "%ld seconds ago",
|
|
1359 (long)(time(NULL) - tt));
|
|
1360 }
|
|
1361
|
|
1362 /*
|
697
|
1363 * ":undojoin": continue adding to the last entry list
|
|
1364 */
|
|
1365 /*ARGSUSED*/
|
|
1366 void
|
|
1367 ex_undojoin(eap)
|
|
1368 exarg_T *eap;
|
|
1369 {
|
|
1370 if (!curbuf->b_u_synced)
|
|
1371 return; /* already unsynced */
|
|
1372 if (curbuf->b_u_newhead == NULL)
|
|
1373 return; /* nothing changed before */
|
|
1374 if (p_ul < 0)
|
|
1375 return; /* no entries, nothing to do */
|
|
1376 else
|
|
1377 {
|
|
1378 /* Go back to the last entry */
|
|
1379 curbuf->b_u_curhead = curbuf->b_u_newhead;
|
|
1380 curbuf->b_u_synced = FALSE; /* no entries, nothing to do */
|
|
1381 }
|
|
1382 }
|
|
1383
|
|
1384 /*
|
7
|
1385 * Called after writing the file and setting b_changed to FALSE.
|
|
1386 * Now an undo means that the buffer is modified.
|
|
1387 */
|
|
1388 void
|
|
1389 u_unchanged(buf)
|
|
1390 buf_T *buf;
|
|
1391 {
|
758
|
1392 u_unch_branch(buf->b_u_oldhead);
|
|
1393 buf->b_did_warn = FALSE;
|
|
1394 }
|
|
1395
|
|
1396 static void
|
|
1397 u_unch_branch(uhp)
|
|
1398 u_header_T *uhp;
|
|
1399 {
|
753
|
1400 u_header_T *uh;
|
7
|
1401
|
758
|
1402 for (uh = uhp; uh != NULL; uh = uh->uh_prev)
|
|
1403 {
|
7
|
1404 uh->uh_flags |= UH_CHANGED;
|
758
|
1405 if (uh->uh_alt_next != NULL)
|
|
1406 u_unch_branch(uh->uh_alt_next); /* recursive */
|
|
1407 }
|
7
|
1408 }
|
|
1409
|
|
1410 /*
|
|
1411 * Get pointer to last added entry.
|
|
1412 * If it's not valid, give an error message and return NULL.
|
|
1413 */
|
|
1414 static u_entry_T *
|
|
1415 u_get_headentry()
|
|
1416 {
|
|
1417 if (curbuf->b_u_newhead == NULL || curbuf->b_u_newhead->uh_entry == NULL)
|
|
1418 {
|
|
1419 EMSG(_("E439: undo list corrupt"));
|
|
1420 return NULL;
|
|
1421 }
|
|
1422 return curbuf->b_u_newhead->uh_entry;
|
|
1423 }
|
|
1424
|
|
1425 /*
|
|
1426 * u_getbot(): compute the line number of the previous u_save
|
|
1427 * It is called only when b_u_synced is FALSE.
|
|
1428 */
|
|
1429 static void
|
|
1430 u_getbot()
|
|
1431 {
|
|
1432 u_entry_T *uep;
|
|
1433 linenr_T extra;
|
|
1434
|
|
1435 uep = u_get_headentry(); /* check for corrupt undo list */
|
|
1436 if (uep == NULL)
|
|
1437 return;
|
|
1438
|
|
1439 uep = curbuf->b_u_newhead->uh_getbot_entry;
|
|
1440 if (uep != NULL)
|
|
1441 {
|
|
1442 /*
|
|
1443 * the new ue_bot is computed from the number of lines that has been
|
|
1444 * inserted (0 - deleted) since calling u_save. This is equal to the
|
|
1445 * old line count subtracted from the current line count.
|
|
1446 */
|
|
1447 extra = curbuf->b_ml.ml_line_count - uep->ue_lcount;
|
|
1448 uep->ue_bot = uep->ue_top + uep->ue_size + 1 + extra;
|
|
1449 if (uep->ue_bot < 1 || uep->ue_bot > curbuf->b_ml.ml_line_count)
|
|
1450 {
|
|
1451 EMSG(_("E440: undo line missing"));
|
|
1452 uep->ue_bot = uep->ue_top + 1; /* assume all lines deleted, will
|
|
1453 * get all the old lines back
|
|
1454 * without deleting the current
|
|
1455 * ones */
|
|
1456 }
|
|
1457
|
|
1458 curbuf->b_u_newhead->uh_getbot_entry = NULL;
|
|
1459 }
|
|
1460
|
|
1461 curbuf->b_u_synced = TRUE;
|
|
1462 }
|
|
1463
|
|
1464 /*
|
753
|
1465 * Free one header and its entry list and adjust the pointers.
|
7
|
1466 */
|
|
1467 static void
|
758
|
1468 u_freeheader(buf, uhp, uhpp)
|
168
|
1469 buf_T *buf;
|
753
|
1470 u_header_T *uhp;
|
|
1471 u_header_T **uhpp; /* if not NULL reset when freeing this header */
|
7
|
1472 {
|
753
|
1473 /* When there is an alternate redo list free that branch completely,
|
|
1474 * because we can never go there. */
|
|
1475 if (uhp->uh_alt_next != NULL)
|
|
1476 u_freebranch(buf, uhp->uh_alt_next, uhpp);
|
7
|
1477
|
753
|
1478 if (uhp->uh_alt_prev != NULL)
|
|
1479 uhp->uh_alt_prev->uh_alt_next = NULL;
|
7
|
1480
|
753
|
1481 /* Update the links in the list to remove the header. */
|
7
|
1482 if (uhp->uh_next == NULL)
|
168
|
1483 buf->b_u_oldhead = uhp->uh_prev;
|
7
|
1484 else
|
|
1485 uhp->uh_next->uh_prev = uhp->uh_prev;
|
|
1486
|
|
1487 if (uhp->uh_prev == NULL)
|
168
|
1488 buf->b_u_newhead = uhp->uh_next;
|
7
|
1489 else
|
|
1490 uhp->uh_prev->uh_next = uhp->uh_next;
|
|
1491
|
753
|
1492 u_freeentries(buf, uhp, uhpp);
|
|
1493 }
|
|
1494
|
|
1495 /*
|
758
|
1496 * Free an alternate branch and any following alternate branches.
|
753
|
1497 */
|
|
1498 static void
|
|
1499 u_freebranch(buf, uhp, uhpp)
|
|
1500 buf_T *buf;
|
|
1501 u_header_T *uhp;
|
|
1502 u_header_T **uhpp; /* if not NULL reset when freeing this header */
|
|
1503 {
|
|
1504 u_header_T *tofree, *next;
|
|
1505
|
|
1506 if (uhp->uh_alt_prev != NULL)
|
|
1507 uhp->uh_alt_prev->uh_alt_next = NULL;
|
|
1508
|
|
1509 next = uhp;
|
|
1510 while (next != NULL)
|
|
1511 {
|
|
1512 tofree = next;
|
|
1513 if (tofree->uh_alt_next != NULL)
|
|
1514 u_freebranch(buf, tofree->uh_alt_next, uhpp); /* recursive */
|
|
1515 next = tofree->uh_prev;
|
|
1516 u_freeentries(buf, tofree, uhpp);
|
|
1517 }
|
|
1518 }
|
|
1519
|
|
1520 /*
|
|
1521 * Free all the undo entries for one header and the header itself.
|
|
1522 * This means that "uhp" is invalid when returning.
|
|
1523 */
|
|
1524 static void
|
|
1525 u_freeentries(buf, uhp, uhpp)
|
|
1526 buf_T *buf;
|
|
1527 u_header_T *uhp;
|
|
1528 u_header_T **uhpp; /* if not NULL reset when freeing this header */
|
|
1529 {
|
|
1530 u_entry_T *uep, *nuep;
|
|
1531
|
|
1532 /* Check for pointers to the header that become invalid now. */
|
|
1533 if (buf->b_u_curhead == uhp)
|
|
1534 buf->b_u_curhead = NULL;
|
|
1535 if (uhpp != NULL && uhp == *uhpp)
|
|
1536 *uhpp = NULL;
|
|
1537
|
|
1538 for (uep = uhp->uh_entry; uep != NULL; uep = nuep)
|
|
1539 {
|
|
1540 nuep = uep->ue_next;
|
|
1541 u_freeentry(uep, uep->ue_size);
|
|
1542 }
|
|
1543
|
168
|
1544 U_FREE_LINE((char_u *)uhp);
|
|
1545 --buf->b_u_numhead;
|
7
|
1546 }
|
|
1547
|
|
1548 /*
|
|
1549 * free entry 'uep' and 'n' lines in uep->ue_array[]
|
|
1550 */
|
|
1551 static void
|
|
1552 u_freeentry(uep, n)
|
|
1553 u_entry_T *uep;
|
|
1554 long n;
|
|
1555 {
|
414
|
1556 while (n > 0)
|
168
|
1557 U_FREE_LINE(uep->ue_array[--n]);
|
359
|
1558 U_FREE_LINE((char_u *)uep->ue_array);
|
168
|
1559 U_FREE_LINE((char_u *)uep);
|
7
|
1560 }
|
|
1561
|
|
1562 /*
|
|
1563 * invalidate the undo buffer; called when storage has already been released
|
|
1564 */
|
|
1565 void
|
|
1566 u_clearall(buf)
|
|
1567 buf_T *buf;
|
|
1568 {
|
|
1569 buf->b_u_newhead = buf->b_u_oldhead = buf->b_u_curhead = NULL;
|
|
1570 buf->b_u_synced = TRUE;
|
|
1571 buf->b_u_numhead = 0;
|
|
1572 buf->b_u_line_ptr = NULL;
|
|
1573 buf->b_u_line_lnum = 0;
|
|
1574 }
|
|
1575
|
|
1576 /*
|
|
1577 * save the line "lnum" for the "U" command
|
|
1578 */
|
|
1579 void
|
|
1580 u_saveline(lnum)
|
|
1581 linenr_T lnum;
|
|
1582 {
|
|
1583 if (lnum == curbuf->b_u_line_lnum) /* line is already saved */
|
|
1584 return;
|
356
|
1585 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) /* should never happen */
|
7
|
1586 return;
|
|
1587 u_clearline();
|
|
1588 curbuf->b_u_line_lnum = lnum;
|
|
1589 if (curwin->w_cursor.lnum == lnum)
|
|
1590 curbuf->b_u_line_colnr = curwin->w_cursor.col;
|
|
1591 else
|
|
1592 curbuf->b_u_line_colnr = 0;
|
|
1593 if ((curbuf->b_u_line_ptr = u_save_line(lnum)) == NULL)
|
|
1594 do_outofmem_msg((long_u)0);
|
|
1595 }
|
|
1596
|
|
1597 /*
|
|
1598 * clear the line saved for the "U" command
|
|
1599 * (this is used externally for crossing a line while in insert mode)
|
|
1600 */
|
|
1601 void
|
|
1602 u_clearline()
|
|
1603 {
|
|
1604 if (curbuf->b_u_line_ptr != NULL)
|
|
1605 {
|
168
|
1606 U_FREE_LINE(curbuf->b_u_line_ptr);
|
7
|
1607 curbuf->b_u_line_ptr = NULL;
|
|
1608 curbuf->b_u_line_lnum = 0;
|
|
1609 }
|
|
1610 }
|
|
1611
|
|
1612 /*
|
|
1613 * Implementation of the "U" command.
|
|
1614 * Differentiation from vi: "U" can be undone with the next "U".
|
|
1615 * We also allow the cursor to be in another line.
|
|
1616 */
|
|
1617 void
|
|
1618 u_undoline()
|
|
1619 {
|
|
1620 colnr_T t;
|
|
1621 char_u *oldp;
|
|
1622
|
|
1623 if (undo_off)
|
|
1624 return;
|
|
1625
|
|
1626 if (curbuf->b_u_line_ptr == NULL ||
|
|
1627 curbuf->b_u_line_lnum > curbuf->b_ml.ml_line_count)
|
|
1628 {
|
|
1629 beep_flush();
|
|
1630 return;
|
|
1631 }
|
|
1632 /* first save the line for the 'u' command */
|
|
1633 if (u_savecommon(curbuf->b_u_line_lnum - 1,
|
|
1634 curbuf->b_u_line_lnum + 1, (linenr_T)0) == FAIL)
|
|
1635 return;
|
|
1636 oldp = u_save_line(curbuf->b_u_line_lnum);
|
|
1637 if (oldp == NULL)
|
|
1638 {
|
|
1639 do_outofmem_msg((long_u)0);
|
|
1640 return;
|
|
1641 }
|
|
1642 ml_replace(curbuf->b_u_line_lnum, curbuf->b_u_line_ptr, TRUE);
|
|
1643 changed_bytes(curbuf->b_u_line_lnum, 0);
|
168
|
1644 U_FREE_LINE(curbuf->b_u_line_ptr);
|
7
|
1645 curbuf->b_u_line_ptr = oldp;
|
|
1646
|
|
1647 t = curbuf->b_u_line_colnr;
|
|
1648 if (curwin->w_cursor.lnum == curbuf->b_u_line_lnum)
|
|
1649 curbuf->b_u_line_colnr = curwin->w_cursor.col;
|
|
1650 curwin->w_cursor.col = t;
|
|
1651 curwin->w_cursor.lnum = curbuf->b_u_line_lnum;
|
|
1652 }
|
|
1653
|
|
1654 /*
|
168
|
1655 * There are two implementations of the memory management for undo:
|
|
1656 * 1. Use the standard malloc()/free() functions.
|
|
1657 * This should be fast for allocating memory, but when a buffer is
|
|
1658 * abandoned every single allocated chunk must be freed, which may be slow.
|
|
1659 * 2. Allocate larger blocks of memory and keep track of chunks ourselves.
|
|
1660 * This is fast for abandoning, but the use of linked lists is slow for
|
|
1661 * finding a free chunk. Esp. when a lot of lines are changed or deleted.
|
|
1662 * A bit of profiling showed that the first method is faster, especially when
|
|
1663 * making a large number of changes, under the condition that malloc()/free()
|
|
1664 * is implemented efficiently.
|
|
1665 */
|
|
1666 #ifdef U_USE_MALLOC
|
|
1667 /*
|
|
1668 * Version of undo memory allocation using malloc()/free()
|
|
1669 *
|
|
1670 * U_FREE_LINE() and U_ALLOC_LINE() are macros that invoke vim_free() and
|
|
1671 * lalloc() directly.
|
|
1672 */
|
|
1673
|
|
1674 /*
|
|
1675 * Free all allocated memory blocks for the buffer 'buf'.
|
|
1676 */
|
|
1677 void
|
|
1678 u_blockfree(buf)
|
|
1679 buf_T *buf;
|
|
1680 {
|
753
|
1681 while (buf->b_u_oldhead != NULL)
|
758
|
1682 u_freeheader(buf, buf->b_u_oldhead, NULL);
|
359
|
1683 U_FREE_LINE(buf->b_u_line_ptr);
|
168
|
1684 }
|
|
1685
|
|
1686 #else
|
|
1687 /*
|
|
1688 * Storage allocation for the undo lines and blocks of the current file.
|
|
1689 * Version where Vim keeps track of the available memory.
|
7
|
1690 */
|
|
1691
|
|
1692 /*
|
|
1693 * Memory is allocated in relatively large blocks. These blocks are linked
|
|
1694 * in the allocated block list, headed by curbuf->b_block_head. They are all
|
|
1695 * freed when abandoning a file, so we don't have to free every single line.
|
|
1696 * The list is kept sorted on memory address.
|
|
1697 * block_alloc() allocates a block.
|
|
1698 * m_blockfree() frees all blocks.
|
|
1699 *
|
|
1700 * The available chunks of memory are kept in free chunk lists. There is
|
|
1701 * one free list for each block of allocated memory. The list is kept sorted
|
|
1702 * on memory address.
|
|
1703 * u_alloc_line() gets a chunk from the free lists.
|
|
1704 * u_free_line() returns a chunk to the free lists.
|
|
1705 * curbuf->b_m_search points to the chunk before the chunk that was
|
|
1706 * freed/allocated the last time.
|
|
1707 * curbuf->b_mb_current points to the b_head where curbuf->b_m_search
|
|
1708 * points into the free list.
|
|
1709 *
|
|
1710 *
|
|
1711 * b_block_head /---> block #1 /---> block #2
|
|
1712 * mb_next ---/ mb_next ---/ mb_next ---> NULL
|
|
1713 * mb_info mb_info mb_info
|
|
1714 * | | |
|
|
1715 * V V V
|
|
1716 * NULL free chunk #1.1 free chunk #2.1
|
|
1717 * | |
|
|
1718 * V V
|
|
1719 * free chunk #1.2 NULL
|
|
1720 * |
|
|
1721 * V
|
|
1722 * NULL
|
|
1723 *
|
|
1724 * When a single free chunk list would have been used, it could take a lot
|
|
1725 * of time in u_free_line() to find the correct place to insert a chunk in the
|
|
1726 * free list. The single free list would become very long when many lines are
|
|
1727 * changed (e.g. with :%s/^M$//).
|
|
1728 */
|
|
1729
|
|
1730 /*
|
|
1731 * this blocksize is used when allocating new lines
|
|
1732 */
|
|
1733 #define MEMBLOCKSIZE 2044
|
|
1734
|
|
1735 /*
|
|
1736 * The size field contains the size of the chunk, including the size field
|
|
1737 * itself.
|
|
1738 *
|
|
1739 * When the chunk is not in-use it is preceded with the m_info structure.
|
|
1740 * The m_next field links it in one of the free chunk lists.
|
|
1741 *
|
|
1742 * On most unix systems structures have to be longword (32 or 64 bit) aligned.
|
|
1743 * On most other systems they are short (16 bit) aligned.
|
|
1744 */
|
|
1745
|
|
1746 /* the structure definitions are now in structs.h */
|
|
1747
|
|
1748 #ifdef ALIGN_LONG
|
|
1749 /* size of m_size */
|
|
1750 # define M_OFFSET (sizeof(long_u))
|
|
1751 #else
|
|
1752 /* size of m_size */
|
|
1753 # define M_OFFSET (sizeof(short_u))
|
|
1754 #endif
|
|
1755
|
168
|
1756 static char_u *u_blockalloc __ARGS((long_u));
|
|
1757
|
7
|
1758 /*
|
|
1759 * Allocate a block of memory and link it in the allocated block list.
|
|
1760 */
|
|
1761 static char_u *
|
|
1762 u_blockalloc(size)
|
|
1763 long_u size;
|
|
1764 {
|
|
1765 mblock_T *p;
|
|
1766 mblock_T *mp, *next;
|
|
1767
|
|
1768 p = (mblock_T *)lalloc(size + sizeof(mblock_T), FALSE);
|
|
1769 if (p != NULL)
|
|
1770 {
|
|
1771 /* Insert the block into the allocated block list, keeping it
|
|
1772 sorted on address. */
|
|
1773 for (mp = &curbuf->b_block_head;
|
|
1774 (next = mp->mb_next) != NULL && next < p;
|
|
1775 mp = next)
|
|
1776 ;
|
|
1777 p->mb_next = next; /* link in block list */
|
|
1778 p->mb_size = size;
|
168
|
1779 p->mb_maxsize = 0; /* nothing free yet */
|
7
|
1780 mp->mb_next = p;
|
|
1781 p->mb_info.m_next = NULL; /* clear free list */
|
|
1782 p->mb_info.m_size = 0;
|
|
1783 curbuf->b_mb_current = p; /* remember current block */
|
|
1784 curbuf->b_m_search = NULL;
|
|
1785 p++; /* return usable memory */
|
|
1786 }
|
|
1787 return (char_u *)p;
|
|
1788 }
|
|
1789
|
|
1790 /*
|
|
1791 * free all allocated memory blocks for the buffer 'buf'
|
|
1792 */
|
|
1793 void
|
|
1794 u_blockfree(buf)
|
|
1795 buf_T *buf;
|
|
1796 {
|
|
1797 mblock_T *p, *np;
|
|
1798
|
|
1799 for (p = buf->b_block_head.mb_next; p != NULL; p = np)
|
|
1800 {
|
|
1801 np = p->mb_next;
|
|
1802 vim_free(p);
|
|
1803 }
|
|
1804 buf->b_block_head.mb_next = NULL;
|
|
1805 buf->b_m_search = NULL;
|
|
1806 buf->b_mb_current = NULL;
|
|
1807 }
|
|
1808
|
|
1809 /*
|
|
1810 * Free a chunk of memory for the current buffer.
|
|
1811 * Insert the chunk into the correct free list, keeping it sorted on address.
|
|
1812 */
|
|
1813 static void
|
|
1814 u_free_line(ptr, keep)
|
|
1815 char_u *ptr;
|
|
1816 int keep; /* don't free the block when it's empty */
|
|
1817 {
|
|
1818 minfo_T *next;
|
|
1819 minfo_T *prev, *curr;
|
|
1820 minfo_T *mp;
|
|
1821 mblock_T *nextb;
|
|
1822 mblock_T *prevb;
|
168
|
1823 long_u maxsize;
|
7
|
1824
|
|
1825 if (ptr == NULL || ptr == IObuff)
|
|
1826 return; /* illegal address can happen in out-of-memory situations */
|
|
1827
|
|
1828 mp = (minfo_T *)(ptr - M_OFFSET);
|
|
1829
|
|
1830 /* find block where chunk could be a part off */
|
|
1831 /* if we change curbuf->b_mb_current, curbuf->b_m_search is set to NULL */
|
|
1832 if (curbuf->b_mb_current == NULL || mp < (minfo_T *)curbuf->b_mb_current)
|
|
1833 {
|
|
1834 curbuf->b_mb_current = curbuf->b_block_head.mb_next;
|
|
1835 curbuf->b_m_search = NULL;
|
|
1836 }
|
|
1837 if ((nextb = curbuf->b_mb_current->mb_next) != NULL
|
|
1838 && (minfo_T *)nextb < mp)
|
|
1839 {
|
|
1840 curbuf->b_mb_current = nextb;
|
|
1841 curbuf->b_m_search = NULL;
|
|
1842 }
|
|
1843 while ((nextb = curbuf->b_mb_current->mb_next) != NULL
|
|
1844 && (minfo_T *)nextb < mp)
|
|
1845 curbuf->b_mb_current = nextb;
|
|
1846
|
|
1847 curr = NULL;
|
|
1848 /*
|
|
1849 * If mp is smaller than curbuf->b_m_search->m_next go to the start of
|
|
1850 * the free list
|
|
1851 */
|
|
1852 if (curbuf->b_m_search == NULL || mp < (curbuf->b_m_search->m_next))
|
|
1853 next = &(curbuf->b_mb_current->mb_info);
|
|
1854 else
|
|
1855 next = curbuf->b_m_search;
|
|
1856 /*
|
|
1857 * The following loop is executed very often.
|
|
1858 * Therefore it has been optimized at the cost of readability.
|
|
1859 * Keep it fast!
|
|
1860 */
|
|
1861 #ifdef SLOW_BUT_EASY_TO_READ
|
|
1862 do
|
|
1863 {
|
|
1864 prev = curr;
|
|
1865 curr = next;
|
|
1866 next = next->m_next;
|
|
1867 }
|
|
1868 while (mp > next && next != NULL);
|
|
1869 #else
|
|
1870 do /* first, middle, last */
|
|
1871 {
|
|
1872 prev = next->m_next; /* curr, next, prev */
|
|
1873 if (prev == NULL || mp <= prev)
|
|
1874 {
|
|
1875 prev = curr;
|
|
1876 curr = next;
|
|
1877 next = next->m_next;
|
|
1878 break;
|
|
1879 }
|
|
1880 curr = prev->m_next; /* next, prev, curr */
|
|
1881 if (curr == NULL || mp <= curr)
|
|
1882 {
|
|
1883 prev = next;
|
|
1884 curr = prev->m_next;
|
|
1885 next = curr->m_next;
|
|
1886 break;
|
|
1887 }
|
|
1888 next = curr->m_next; /* prev, curr, next */
|
|
1889 }
|
|
1890 while (mp > next && next != NULL);
|
|
1891 #endif
|
|
1892
|
|
1893 /* if *mp and *next are concatenated, join them into one chunk */
|
|
1894 if ((char_u *)mp + mp->m_size == (char_u *)next)
|
|
1895 {
|
|
1896 mp->m_size += next->m_size;
|
|
1897 mp->m_next = next->m_next;
|
|
1898 }
|
|
1899 else
|
|
1900 mp->m_next = next;
|
168
|
1901 maxsize = mp->m_size;
|
7
|
1902
|
|
1903 /* if *curr and *mp are concatenated, join them */
|
|
1904 if (prev != NULL && (char_u *)curr + curr->m_size == (char_u *)mp)
|
|
1905 {
|
|
1906 curr->m_size += mp->m_size;
|
168
|
1907 maxsize = curr->m_size;
|
7
|
1908 curr->m_next = mp->m_next;
|
|
1909 curbuf->b_m_search = prev;
|
|
1910 }
|
|
1911 else
|
|
1912 {
|
|
1913 curr->m_next = mp;
|
|
1914 curbuf->b_m_search = curr; /* put curbuf->b_m_search before freed
|
|
1915 chunk */
|
|
1916 }
|
|
1917
|
|
1918 /*
|
|
1919 * If the block only containes free memory now, release it.
|
|
1920 */
|
|
1921 if (!keep && curbuf->b_mb_current->mb_size
|
|
1922 == curbuf->b_mb_current->mb_info.m_next->m_size)
|
|
1923 {
|
|
1924 /* Find the block before the current one to be able to unlink it from
|
|
1925 * the list of blocks. */
|
|
1926 prevb = &curbuf->b_block_head;
|
|
1927 for (nextb = prevb->mb_next; nextb != curbuf->b_mb_current;
|
|
1928 nextb = nextb->mb_next)
|
|
1929 prevb = nextb;
|
|
1930 prevb->mb_next = nextb->mb_next;
|
|
1931 vim_free(nextb);
|
|
1932 curbuf->b_mb_current = NULL;
|
|
1933 curbuf->b_m_search = NULL;
|
|
1934 }
|
168
|
1935 else if (curbuf->b_mb_current->mb_maxsize < maxsize)
|
|
1936 curbuf->b_mb_current->mb_maxsize = maxsize;
|
7
|
1937 }
|
|
1938
|
|
1939 /*
|
|
1940 * Allocate and initialize a new line structure with room for at least
|
|
1941 * 'size' characters plus a terminating NUL.
|
|
1942 */
|
|
1943 static char_u *
|
|
1944 u_alloc_line(size)
|
|
1945 unsigned size;
|
|
1946 {
|
|
1947 minfo_T *mp, *mprev, *mp2;
|
|
1948 mblock_T *mbp;
|
|
1949 int size_align;
|
|
1950
|
|
1951 /*
|
|
1952 * Add room for size field and trailing NUL byte.
|
|
1953 * Adjust for minimal size (must be able to store minfo_T
|
|
1954 * plus a trailing NUL, so the chunk can be released again)
|
|
1955 */
|
|
1956 size += M_OFFSET + 1;
|
|
1957 if (size < sizeof(minfo_T) + 1)
|
|
1958 size = sizeof(minfo_T) + 1;
|
|
1959
|
|
1960 /*
|
|
1961 * round size up for alignment
|
|
1962 */
|
|
1963 size_align = (size + ALIGN_MASK) & ~ALIGN_MASK;
|
|
1964
|
|
1965 /*
|
|
1966 * If curbuf->b_m_search is NULL (uninitialized free list) start at
|
|
1967 * curbuf->b_block_head
|
|
1968 */
|
|
1969 if (curbuf->b_mb_current == NULL || curbuf->b_m_search == NULL)
|
|
1970 {
|
|
1971 curbuf->b_mb_current = &curbuf->b_block_head;
|
|
1972 curbuf->b_m_search = &(curbuf->b_block_head.mb_info);
|
|
1973 }
|
|
1974
|
168
|
1975 /* Search for a block with enough space. */
|
7
|
1976 mbp = curbuf->b_mb_current;
|
168
|
1977 while (mbp->mb_maxsize < size_align)
|
7
|
1978 {
|
168
|
1979 if (mbp->mb_next != NULL)
|
7
|
1980 mbp = mbp->mb_next;
|
|
1981 else
|
|
1982 mbp = &curbuf->b_block_head;
|
168
|
1983 if (mbp == curbuf->b_mb_current)
|
7
|
1984 {
|
168
|
1985 int n = (size_align > (MEMBLOCKSIZE / 4)
|
|
1986 ? size_align : MEMBLOCKSIZE);
|
7
|
1987
|
168
|
1988 /* Back where we started in block list: need to add a new block
|
|
1989 * with enough space. */
|
|
1990 mp = (minfo_T *)u_blockalloc((long_u)n);
|
|
1991 if (mp == NULL)
|
|
1992 return (NULL);
|
|
1993 mp->m_size = n;
|
|
1994 u_free_line((char_u *)mp + M_OFFSET, TRUE);
|
|
1995 mbp = curbuf->b_mb_current;
|
|
1996 break;
|
|
1997 }
|
|
1998 }
|
|
1999 if (mbp != curbuf->b_mb_current)
|
|
2000 curbuf->b_m_search = &(mbp->mb_info);
|
|
2001
|
|
2002 /* In this block find a chunk with enough space. */
|
|
2003 mprev = curbuf->b_m_search;
|
|
2004 mp = curbuf->b_m_search->m_next;
|
407
|
2005 for (;;)
|
168
|
2006 {
|
|
2007 if (mp == NULL) /* at end of the list */
|
|
2008 mp = &(mbp->mb_info); /* wrap around to begin */
|
|
2009 if (mp->m_size >= size)
|
|
2010 break;
|
|
2011 if (mp == curbuf->b_m_search)
|
|
2012 {
|
|
2013 /* back where we started in free chunk list: "cannot happen" */
|
|
2014 EMSG2(_(e_intern2), "u_alloc_line()");
|
|
2015 return NULL;
|
7
|
2016 }
|
|
2017 mprev = mp;
|
168
|
2018 mp = mp->m_next;
|
7
|
2019 }
|
|
2020
|
168
|
2021 /* when using the largest chunk adjust mb_maxsize */
|
|
2022 if (mp->m_size >= mbp->mb_maxsize)
|
|
2023 mbp->mb_maxsize = 0;
|
|
2024
|
7
|
2025 /* if the chunk we found is large enough, split it up in two */
|
|
2026 if ((long)mp->m_size - size_align >= (long)(sizeof(minfo_T) + 1))
|
|
2027 {
|
|
2028 mp2 = (minfo_T *)((char_u *)mp + size_align);
|
|
2029 mp2->m_size = mp->m_size - size_align;
|
|
2030 mp2->m_next = mp->m_next;
|
|
2031 mprev->m_next = mp2;
|
|
2032 mp->m_size = size_align;
|
|
2033 }
|
|
2034 else /* remove *mp from the free list */
|
|
2035 {
|
|
2036 mprev->m_next = mp->m_next;
|
|
2037 }
|
|
2038 curbuf->b_m_search = mprev;
|
|
2039 curbuf->b_mb_current = mbp;
|
|
2040
|
168
|
2041 /* If using the largest chunk need to find the new largest chunk */
|
|
2042 if (mbp->mb_maxsize == 0)
|
|
2043 for (mp2 = &(mbp->mb_info); mp2 != NULL; mp2 = mp2->m_next)
|
|
2044 if (mbp->mb_maxsize < mp2->m_size)
|
|
2045 mbp->mb_maxsize = mp2->m_size;
|
|
2046
|
7
|
2047 mp = (minfo_T *)((char_u *)mp + M_OFFSET);
|
|
2048 *(char_u *)mp = NUL; /* set the first byte to NUL */
|
|
2049
|
|
2050 return ((char_u *)mp);
|
|
2051 }
|
168
|
2052 #endif
|
7
|
2053
|
|
2054 /*
|
|
2055 * u_save_line(): allocate memory with u_alloc_line() and copy line 'lnum'
|
|
2056 * into it.
|
|
2057 */
|
|
2058 static char_u *
|
|
2059 u_save_line(lnum)
|
|
2060 linenr_T lnum;
|
|
2061 {
|
|
2062 char_u *src;
|
|
2063 char_u *dst;
|
|
2064 unsigned len;
|
|
2065
|
|
2066 src = ml_get(lnum);
|
|
2067 len = (unsigned)STRLEN(src);
|
168
|
2068 if ((dst = U_ALLOC_LINE(len)) != NULL)
|
7
|
2069 mch_memmove(dst, src, (size_t)(len + 1));
|
|
2070 return (dst);
|
|
2071 }
|
|
2072
|
|
2073 /*
|
|
2074 * Check if the 'modified' flag is set, or 'ff' has changed (only need to
|
|
2075 * check the first character, because it can only be "dos", "unix" or "mac").
|
|
2076 * "nofile" and "scratch" type buffers are considered to always be unchanged.
|
|
2077 */
|
|
2078 int
|
|
2079 bufIsChanged(buf)
|
|
2080 buf_T *buf;
|
|
2081 {
|
|
2082 return
|
|
2083 #ifdef FEAT_QUICKFIX
|
|
2084 !bt_dontwrite(buf) &&
|
|
2085 #endif
|
|
2086 (buf->b_changed || file_ff_differs(buf));
|
|
2087 }
|
|
2088
|
|
2089 int
|
|
2090 curbufIsChanged()
|
|
2091 {
|
|
2092 return
|
|
2093 #ifdef FEAT_QUICKFIX
|
|
2094 !bt_dontwrite(curbuf) &&
|
|
2095 #endif
|
|
2096 (curbuf->b_changed || file_ff_differs(curbuf));
|
|
2097 }
|