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