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