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