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)); |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 102 | #ifdef FEAT_PERSISTENT_UNDO |
| 103 | static void unserialize_pos __ARGS((pos_T *pos, FILE *fp)); |
| 104 | static void unserialize_visualinfo __ARGS((visualinfo_T *info, FILE *fp)); |
| 105 | static char_u *u_get_undo_file_name __ARGS((char_u *, int reading)); |
| 106 | static int serialize_uep __ARGS((u_entry_T *uep, FILE *fp)); |
| 107 | static void serialize_pos __ARGS((pos_T pos, FILE *fp)); |
| 108 | static void serialize_visualinfo __ARGS((visualinfo_T info, FILE *fp)); |
| 109 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 110 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 111 | #ifdef U_USE_MALLOC |
| 112 | # define U_FREE_LINE(ptr) vim_free(ptr) |
| 113 | # define U_ALLOC_LINE(size) lalloc((long_u)((size) + 1), FALSE) |
| 114 | #else |
| 115 | static void u_free_line __ARGS((char_u *ptr, int keep)); |
| 116 | static char_u *u_alloc_line __ARGS((unsigned size)); |
| 117 | # define U_FREE_LINE(ptr) u_free_line((ptr), FALSE) |
| 118 | # define U_ALLOC_LINE(size) u_alloc_line(size) |
| 119 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 120 | static char_u *u_save_line __ARGS((linenr_T)); |
| 121 | |
| 122 | static long u_newcount, u_oldcount; |
| 123 | |
| 124 | /* |
| 125 | * When 'u' flag included in 'cpoptions', we behave like vi. Need to remember |
| 126 | * the action that "u" should do. |
| 127 | */ |
| 128 | static int undo_undoes = FALSE; |
| 129 | |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 130 | static int lastmark = 0; |
| 131 | |
Bram Moolenaar | fecb660 | 2007-10-01 20:54:15 +0000 | [diff] [blame] | 132 | #ifdef U_DEBUG |
| 133 | /* |
| 134 | * Check the undo structures for being valid. Print a warning when something |
| 135 | * looks wrong. |
| 136 | */ |
| 137 | static int seen_b_u_curhead; |
| 138 | static int seen_b_u_newhead; |
| 139 | static int header_count; |
| 140 | |
| 141 | static void |
| 142 | u_check_tree(u_header_T *uhp, |
| 143 | u_header_T *exp_uh_next, |
| 144 | u_header_T *exp_uh_alt_prev) |
| 145 | { |
| 146 | u_entry_T *uep; |
| 147 | |
| 148 | if (uhp == NULL) |
| 149 | return; |
| 150 | ++header_count; |
| 151 | if (uhp == curbuf->b_u_curhead && ++seen_b_u_curhead > 1) |
| 152 | { |
| 153 | EMSG("b_u_curhead found twice (looping?)"); |
| 154 | return; |
| 155 | } |
| 156 | if (uhp == curbuf->b_u_newhead && ++seen_b_u_newhead > 1) |
| 157 | { |
| 158 | EMSG("b_u_newhead found twice (looping?)"); |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | if (uhp->uh_magic != UH_MAGIC) |
| 163 | EMSG("uh_magic wrong (may be using freed memory)"); |
| 164 | else |
| 165 | { |
| 166 | /* Check pointers back are correct. */ |
| 167 | if (uhp->uh_next != exp_uh_next) |
| 168 | { |
| 169 | EMSG("uh_next wrong"); |
| 170 | smsg((char_u *)"expected: 0x%x, actual: 0x%x", |
| 171 | exp_uh_next, uhp->uh_next); |
| 172 | } |
| 173 | if (uhp->uh_alt_prev != exp_uh_alt_prev) |
| 174 | { |
| 175 | EMSG("uh_alt_prev wrong"); |
| 176 | smsg((char_u *)"expected: 0x%x, actual: 0x%x", |
| 177 | exp_uh_alt_prev, uhp->uh_alt_prev); |
| 178 | } |
| 179 | |
| 180 | /* Check the undo tree at this header. */ |
| 181 | for (uep = uhp->uh_entry; uep != NULL; uep = uep->ue_next) |
| 182 | { |
| 183 | if (uep->ue_magic != UE_MAGIC) |
| 184 | { |
| 185 | EMSG("ue_magic wrong (may be using freed memory)"); |
| 186 | break; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /* Check the next alt tree. */ |
| 191 | u_check_tree(uhp->uh_alt_next, uhp->uh_next, uhp); |
| 192 | |
| 193 | /* Check the next header in this branch. */ |
| 194 | u_check_tree(uhp->uh_prev, uhp, NULL); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | void |
| 199 | u_check(int newhead_may_be_NULL) |
| 200 | { |
| 201 | seen_b_u_newhead = 0; |
| 202 | seen_b_u_curhead = 0; |
| 203 | header_count = 0; |
| 204 | |
| 205 | u_check_tree(curbuf->b_u_oldhead, NULL, NULL); |
| 206 | |
| 207 | if (seen_b_u_newhead == 0 && curbuf->b_u_oldhead != NULL |
| 208 | && !(newhead_may_be_NULL && curbuf->b_u_newhead == NULL)) |
| 209 | EMSGN("b_u_newhead invalid: 0x%x", curbuf->b_u_newhead); |
| 210 | if (curbuf->b_u_curhead != NULL && seen_b_u_curhead == 0) |
| 211 | EMSGN("b_u_curhead invalid: 0x%x", curbuf->b_u_curhead); |
| 212 | if (header_count != curbuf->b_u_numhead) |
| 213 | { |
| 214 | EMSG("b_u_numhead invalid"); |
| 215 | smsg((char_u *)"expected: %ld, actual: %ld", |
| 216 | (long)header_count, (long)curbuf->b_u_numhead); |
| 217 | } |
| 218 | } |
| 219 | #endif |
| 220 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 221 | /* |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 222 | * Save the current line for both the "u" and "U" command. |
| 223 | * Returns OK or FAIL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 224 | */ |
| 225 | int |
| 226 | u_save_cursor() |
| 227 | { |
| 228 | return (u_save((linenr_T)(curwin->w_cursor.lnum - 1), |
| 229 | (linenr_T)(curwin->w_cursor.lnum + 1))); |
| 230 | } |
| 231 | |
| 232 | /* |
| 233 | * Save the lines between "top" and "bot" for both the "u" and "U" command. |
| 234 | * "top" may be 0 and bot may be curbuf->b_ml.ml_line_count + 1. |
| 235 | * Returns FAIL when lines could not be saved, OK otherwise. |
| 236 | */ |
| 237 | int |
| 238 | u_save(top, bot) |
| 239 | linenr_T top, bot; |
| 240 | { |
| 241 | if (undo_off) |
| 242 | return OK; |
| 243 | |
| 244 | if (top > curbuf->b_ml.ml_line_count || |
| 245 | top >= bot || bot > curbuf->b_ml.ml_line_count + 1) |
| 246 | return FALSE; /* rely on caller to do error messages */ |
| 247 | |
| 248 | if (top + 2 == bot) |
| 249 | u_saveline((linenr_T)(top + 1)); |
| 250 | |
| 251 | return (u_savecommon(top, bot, (linenr_T)0)); |
| 252 | } |
| 253 | |
| 254 | /* |
Bram Moolenaar | fff2bee | 2010-05-15 13:56:02 +0200 | [diff] [blame] | 255 | * Save the line "lnum" (used by ":s" and "~" command). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 256 | * The line is replaced, so the new bottom line is lnum + 1. |
| 257 | */ |
| 258 | int |
| 259 | u_savesub(lnum) |
| 260 | linenr_T lnum; |
| 261 | { |
| 262 | if (undo_off) |
| 263 | return OK; |
| 264 | |
| 265 | return (u_savecommon(lnum - 1, lnum + 1, lnum + 1)); |
| 266 | } |
| 267 | |
| 268 | /* |
Bram Moolenaar | fff2bee | 2010-05-15 13:56:02 +0200 | [diff] [blame] | 269 | * A new line is inserted before line "lnum" (used by :s command). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 270 | * The line is inserted, so the new bottom line is lnum + 1. |
| 271 | */ |
| 272 | int |
| 273 | u_inssub(lnum) |
| 274 | linenr_T lnum; |
| 275 | { |
| 276 | if (undo_off) |
| 277 | return OK; |
| 278 | |
| 279 | return (u_savecommon(lnum - 1, lnum, lnum + 1)); |
| 280 | } |
| 281 | |
| 282 | /* |
Bram Moolenaar | fff2bee | 2010-05-15 13:56:02 +0200 | [diff] [blame] | 283 | * Save the lines "lnum" - "lnum" + nlines (used by delete command). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 284 | * The lines are deleted, so the new bottom line is lnum, unless the buffer |
| 285 | * becomes empty. |
| 286 | */ |
| 287 | int |
| 288 | u_savedel(lnum, nlines) |
| 289 | linenr_T lnum; |
| 290 | long nlines; |
| 291 | { |
| 292 | if (undo_off) |
| 293 | return OK; |
| 294 | |
| 295 | return (u_savecommon(lnum - 1, lnum + nlines, |
| 296 | nlines == curbuf->b_ml.ml_line_count ? 2 : lnum)); |
| 297 | } |
| 298 | |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 299 | /* |
| 300 | * Return TRUE when undo is allowed. Otherwise give an error message and |
| 301 | * return FALSE. |
| 302 | */ |
Bram Moolenaar | ce6ef25 | 2006-07-12 19:49:41 +0000 | [diff] [blame] | 303 | int |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 304 | undo_allowed() |
| 305 | { |
| 306 | /* Don't allow changes when 'modifiable' is off. */ |
| 307 | if (!curbuf->b_p_ma) |
| 308 | { |
| 309 | EMSG(_(e_modifiable)); |
| 310 | return FALSE; |
| 311 | } |
| 312 | |
| 313 | #ifdef HAVE_SANDBOX |
| 314 | /* In the sandbox it's not allowed to change the text. */ |
| 315 | if (sandbox != 0) |
| 316 | { |
| 317 | EMSG(_(e_sandbox)); |
| 318 | return FALSE; |
| 319 | } |
| 320 | #endif |
| 321 | |
| 322 | /* Don't allow changes in the buffer while editing the cmdline. The |
| 323 | * caller of getcmdline() may get confused. */ |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 324 | if (textlock != 0) |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 325 | { |
| 326 | EMSG(_(e_secure)); |
| 327 | return FALSE; |
| 328 | } |
| 329 | |
| 330 | return TRUE; |
| 331 | } |
| 332 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 333 | static int |
| 334 | u_savecommon(top, bot, newbot) |
| 335 | linenr_T top, bot; |
| 336 | linenr_T newbot; |
| 337 | { |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 338 | linenr_T lnum; |
| 339 | long i; |
| 340 | u_header_T *uhp; |
| 341 | u_header_T *old_curhead; |
| 342 | u_entry_T *uep; |
| 343 | u_entry_T *prev_uep; |
| 344 | long size; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 345 | |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 346 | /* When making changes is not allowed return FAIL. It's a crude way to |
| 347 | * make all change commands fail. */ |
| 348 | if (!undo_allowed()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 349 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 350 | |
Bram Moolenaar | fecb660 | 2007-10-01 20:54:15 +0000 | [diff] [blame] | 351 | #ifdef U_DEBUG |
| 352 | u_check(FALSE); |
| 353 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 354 | #ifdef FEAT_NETBEANS_INTG |
| 355 | /* |
| 356 | * Netbeans defines areas that cannot be modified. Bail out here when |
| 357 | * trying to change text in a guarded area. |
| 358 | */ |
Bram Moolenaar | b26e632 | 2010-05-22 21:34:09 +0200 | [diff] [blame] | 359 | if (netbeans_active()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 360 | { |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 361 | if (netbeans_is_guarded(top, bot)) |
| 362 | { |
| 363 | EMSG(_(e_guarded)); |
| 364 | return FAIL; |
| 365 | } |
| 366 | if (curbuf->b_p_ro) |
| 367 | { |
| 368 | EMSG(_(e_nbreadonly)); |
| 369 | return FAIL; |
| 370 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 371 | } |
| 372 | #endif |
| 373 | |
| 374 | #ifdef FEAT_AUTOCMD |
| 375 | /* |
| 376 | * Saving text for undo means we are going to make a change. Give a |
| 377 | * warning for a read-only file before making the change, so that the |
| 378 | * FileChangedRO event can replace the buffer with a read-write version |
| 379 | * (e.g., obtained from a source control system). |
| 380 | */ |
| 381 | change_warning(0); |
| 382 | #endif |
| 383 | |
| 384 | size = bot - top - 1; |
| 385 | |
| 386 | /* |
| 387 | * if curbuf->b_u_synced == TRUE make a new header |
| 388 | */ |
| 389 | if (curbuf->b_u_synced) |
| 390 | { |
| 391 | #ifdef FEAT_JUMPLIST |
| 392 | /* Need to create new entry in b_changelist. */ |
| 393 | curbuf->b_new_change = TRUE; |
| 394 | #endif |
| 395 | |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 396 | if (p_ul >= 0) |
| 397 | { |
| 398 | /* |
| 399 | * Make a new header entry. Do this first so that we don't mess |
| 400 | * up the undo info when out of memory. |
| 401 | */ |
| 402 | uhp = (u_header_T *)U_ALLOC_LINE((unsigned)sizeof(u_header_T)); |
| 403 | if (uhp == NULL) |
| 404 | goto nomem; |
Bram Moolenaar | fecb660 | 2007-10-01 20:54:15 +0000 | [diff] [blame] | 405 | #ifdef U_DEBUG |
| 406 | uhp->uh_magic = UH_MAGIC; |
| 407 | #endif |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 408 | } |
Bram Moolenaar | 7d47b6e | 2006-03-15 22:59:18 +0000 | [diff] [blame] | 409 | else |
| 410 | uhp = NULL; |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 411 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 412 | /* |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 413 | * If we undid more than we redid, move the entry lists before and |
| 414 | * including curbuf->b_u_curhead to an alternate branch. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 415 | */ |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 416 | old_curhead = curbuf->b_u_curhead; |
| 417 | if (old_curhead != NULL) |
| 418 | { |
| 419 | curbuf->b_u_newhead = old_curhead->uh_next; |
| 420 | curbuf->b_u_curhead = NULL; |
| 421 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 422 | |
| 423 | /* |
| 424 | * free headers to keep the size right |
| 425 | */ |
| 426 | while (curbuf->b_u_numhead > p_ul && curbuf->b_u_oldhead != NULL) |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 427 | { |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 428 | u_header_T *uhfree = curbuf->b_u_oldhead; |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 429 | |
Bram Moolenaar | fecb660 | 2007-10-01 20:54:15 +0000 | [diff] [blame] | 430 | if (uhfree == old_curhead) |
| 431 | /* Can't reconnect the branch, delete all of it. */ |
| 432 | u_freebranch(curbuf, uhfree, &old_curhead); |
| 433 | else if (uhfree->uh_alt_next == NULL) |
| 434 | /* There is no branch, only free one header. */ |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 435 | u_freeheader(curbuf, uhfree, &old_curhead); |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 436 | else |
| 437 | { |
| 438 | /* Free the oldest alternate branch as a whole. */ |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 439 | while (uhfree->uh_alt_next != NULL) |
| 440 | uhfree = uhfree->uh_alt_next; |
| 441 | u_freebranch(curbuf, uhfree, &old_curhead); |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 442 | } |
Bram Moolenaar | fecb660 | 2007-10-01 20:54:15 +0000 | [diff] [blame] | 443 | #ifdef U_DEBUG |
| 444 | u_check(TRUE); |
| 445 | #endif |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 446 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 447 | |
Bram Moolenaar | 7d47b6e | 2006-03-15 22:59:18 +0000 | [diff] [blame] | 448 | if (uhp == NULL) /* no undo at all */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 449 | { |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 450 | if (old_curhead != NULL) |
| 451 | u_freebranch(curbuf, old_curhead, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 452 | curbuf->b_u_synced = FALSE; |
| 453 | return OK; |
| 454 | } |
| 455 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 456 | uhp->uh_prev = NULL; |
| 457 | uhp->uh_next = curbuf->b_u_newhead; |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 458 | uhp->uh_alt_next = old_curhead; |
| 459 | if (old_curhead != NULL) |
| 460 | { |
Bram Moolenaar | 89ed3df | 2007-01-09 19:23:12 +0000 | [diff] [blame] | 461 | uhp->uh_alt_prev = old_curhead->uh_alt_prev; |
| 462 | if (uhp->uh_alt_prev != NULL) |
| 463 | uhp->uh_alt_prev->uh_alt_next = uhp; |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 464 | old_curhead->uh_alt_prev = uhp; |
| 465 | if (curbuf->b_u_oldhead == old_curhead) |
| 466 | curbuf->b_u_oldhead = uhp; |
| 467 | } |
Bram Moolenaar | 89ed3df | 2007-01-09 19:23:12 +0000 | [diff] [blame] | 468 | else |
| 469 | uhp->uh_alt_prev = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 470 | if (curbuf->b_u_newhead != NULL) |
| 471 | curbuf->b_u_newhead->uh_prev = uhp; |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 472 | |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 473 | uhp->uh_seq = ++curbuf->b_u_seq_last; |
| 474 | curbuf->b_u_seq_cur = uhp->uh_seq; |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 475 | uhp->uh_time = time(NULL); |
| 476 | curbuf->b_u_seq_time = uhp->uh_time + 1; |
| 477 | |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 478 | uhp->uh_walk = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 479 | uhp->uh_entry = NULL; |
| 480 | uhp->uh_getbot_entry = NULL; |
| 481 | uhp->uh_cursor = curwin->w_cursor; /* save cursor pos. for undo */ |
| 482 | #ifdef FEAT_VIRTUALEDIT |
| 483 | if (virtual_active() && curwin->w_cursor.coladd > 0) |
| 484 | uhp->uh_cursor_vcol = getviscol(); |
| 485 | else |
| 486 | uhp->uh_cursor_vcol = -1; |
| 487 | #endif |
| 488 | |
| 489 | /* save changed and buffer empty flag for undo */ |
| 490 | uhp->uh_flags = (curbuf->b_changed ? UH_CHANGED : 0) + |
| 491 | ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0); |
| 492 | |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 493 | /* save named marks and Visual marks for undo */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 494 | mch_memmove(uhp->uh_namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS); |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 495 | #ifdef FEAT_VISUAL |
| 496 | uhp->uh_visual = curbuf->b_visual; |
| 497 | #endif |
| 498 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 499 | curbuf->b_u_newhead = uhp; |
| 500 | if (curbuf->b_u_oldhead == NULL) |
| 501 | curbuf->b_u_oldhead = uhp; |
| 502 | ++curbuf->b_u_numhead; |
| 503 | } |
| 504 | else |
| 505 | { |
| 506 | if (p_ul < 0) /* no undo at all */ |
| 507 | return OK; |
| 508 | |
| 509 | /* |
| 510 | * When saving a single line, and it has been saved just before, it |
| 511 | * doesn't make sense saving it again. Saves a lot of memory when |
| 512 | * making lots of changes inside the same line. |
| 513 | * This is only possible if the previous change didn't increase or |
| 514 | * decrease the number of lines. |
| 515 | * Check the ten last changes. More doesn't make sense and takes too |
| 516 | * long. |
| 517 | */ |
| 518 | if (size == 1) |
| 519 | { |
| 520 | uep = u_get_headentry(); |
| 521 | prev_uep = NULL; |
| 522 | for (i = 0; i < 10; ++i) |
| 523 | { |
| 524 | if (uep == NULL) |
| 525 | break; |
| 526 | |
| 527 | /* If lines have been inserted/deleted we give up. |
| 528 | * Also when the line was included in a multi-line save. */ |
| 529 | if ((curbuf->b_u_newhead->uh_getbot_entry != uep |
| 530 | ? (uep->ue_top + uep->ue_size + 1 |
| 531 | != (uep->ue_bot == 0 |
| 532 | ? curbuf->b_ml.ml_line_count + 1 |
| 533 | : uep->ue_bot)) |
| 534 | : uep->ue_lcount != curbuf->b_ml.ml_line_count) |
| 535 | || (uep->ue_size > 1 |
| 536 | && top >= uep->ue_top |
| 537 | && top + 2 <= uep->ue_top + uep->ue_size + 1)) |
| 538 | break; |
| 539 | |
| 540 | /* If it's the same line we can skip saving it again. */ |
| 541 | if (uep->ue_size == 1 && uep->ue_top == top) |
| 542 | { |
| 543 | if (i > 0) |
| 544 | { |
| 545 | /* It's not the last entry: get ue_bot for the last |
| 546 | * entry now. Following deleted/inserted lines go to |
| 547 | * the re-used entry. */ |
| 548 | u_getbot(); |
| 549 | curbuf->b_u_synced = FALSE; |
| 550 | |
| 551 | /* Move the found entry to become the last entry. The |
| 552 | * order of undo/redo doesn't matter for the entries |
| 553 | * we move it over, since they don't change the line |
| 554 | * count and don't include this line. It does matter |
| 555 | * for the found entry if the line count is changed by |
| 556 | * the executed command. */ |
| 557 | prev_uep->ue_next = uep->ue_next; |
| 558 | uep->ue_next = curbuf->b_u_newhead->uh_entry; |
| 559 | curbuf->b_u_newhead->uh_entry = uep; |
| 560 | } |
| 561 | |
| 562 | /* The executed command may change the line count. */ |
| 563 | if (newbot != 0) |
| 564 | uep->ue_bot = newbot; |
| 565 | else if (bot > curbuf->b_ml.ml_line_count) |
| 566 | uep->ue_bot = 0; |
| 567 | else |
| 568 | { |
| 569 | uep->ue_lcount = curbuf->b_ml.ml_line_count; |
| 570 | curbuf->b_u_newhead->uh_getbot_entry = uep; |
| 571 | } |
| 572 | return OK; |
| 573 | } |
| 574 | prev_uep = uep; |
| 575 | uep = uep->ue_next; |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | /* find line number for ue_bot for previous u_save() */ |
| 580 | u_getbot(); |
| 581 | } |
| 582 | |
| 583 | #if !defined(UNIX) && !defined(DJGPP) && !defined(WIN32) && !defined(__EMX__) |
| 584 | /* |
| 585 | * With Amiga and MSDOS 16 bit we can't handle big undo's, because |
| 586 | * then u_alloc_line would have to allocate a block larger than 32K |
| 587 | */ |
| 588 | if (size >= 8000) |
| 589 | goto nomem; |
| 590 | #endif |
| 591 | |
| 592 | /* |
| 593 | * add lines in front of entry list |
| 594 | */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 595 | uep = (u_entry_T *)U_ALLOC_LINE((unsigned)sizeof(u_entry_T)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 596 | if (uep == NULL) |
| 597 | goto nomem; |
Bram Moolenaar | 7db5fc8 | 2010-05-24 11:59:29 +0200 | [diff] [blame] | 598 | vim_memset(uep, 0, sizeof(u_entry_T)); |
Bram Moolenaar | fecb660 | 2007-10-01 20:54:15 +0000 | [diff] [blame] | 599 | #ifdef U_DEBUG |
| 600 | uep->ue_magic = UE_MAGIC; |
| 601 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 602 | |
| 603 | uep->ue_size = size; |
| 604 | uep->ue_top = top; |
| 605 | if (newbot != 0) |
| 606 | uep->ue_bot = newbot; |
| 607 | /* |
| 608 | * Use 0 for ue_bot if bot is below last line. |
| 609 | * Otherwise we have to compute ue_bot later. |
| 610 | */ |
| 611 | else if (bot > curbuf->b_ml.ml_line_count) |
| 612 | uep->ue_bot = 0; |
| 613 | else |
| 614 | { |
| 615 | uep->ue_lcount = curbuf->b_ml.ml_line_count; |
| 616 | curbuf->b_u_newhead->uh_getbot_entry = uep; |
| 617 | } |
| 618 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 619 | if (size > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 620 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 621 | if ((uep->ue_array = (char_u **)U_ALLOC_LINE( |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 622 | (unsigned)(sizeof(char_u *) * size))) == NULL) |
| 623 | { |
| 624 | u_freeentry(uep, 0L); |
| 625 | goto nomem; |
| 626 | } |
| 627 | for (i = 0, lnum = top + 1; i < size; ++i) |
| 628 | { |
Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 629 | fast_breakcheck(); |
| 630 | if (got_int) |
| 631 | { |
| 632 | u_freeentry(uep, i); |
| 633 | return FAIL; |
| 634 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 635 | if ((uep->ue_array[i] = u_save_line(lnum++)) == NULL) |
| 636 | { |
| 637 | u_freeentry(uep, i); |
| 638 | goto nomem; |
| 639 | } |
| 640 | } |
| 641 | } |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 642 | else |
| 643 | uep->ue_array = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 644 | uep->ue_next = curbuf->b_u_newhead->uh_entry; |
| 645 | curbuf->b_u_newhead->uh_entry = uep; |
| 646 | curbuf->b_u_synced = FALSE; |
| 647 | undo_undoes = FALSE; |
| 648 | |
Bram Moolenaar | fecb660 | 2007-10-01 20:54:15 +0000 | [diff] [blame] | 649 | #ifdef U_DEBUG |
| 650 | u_check(FALSE); |
| 651 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 652 | return OK; |
| 653 | |
| 654 | nomem: |
| 655 | msg_silent = 0; /* must display the prompt */ |
| 656 | if (ask_yesno((char_u *)_("No undo possible; continue anyway"), TRUE) |
| 657 | == 'y') |
| 658 | { |
| 659 | undo_off = TRUE; /* will be reset when character typed */ |
| 660 | return OK; |
| 661 | } |
| 662 | do_outofmem_msg((long_u)0); |
| 663 | return FAIL; |
| 664 | } |
| 665 | |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 666 | #ifdef FEAT_PERSISTENT_UNDO |
| 667 | |
| 668 | # define UF_START_MAGIC 0xfeac /* magic at start of undofile */ |
| 669 | # define UF_HEADER_MAGIC 0x5fd0 /* magic at start of header */ |
| 670 | # define UF_END_MAGIC 0xe7aa /* magic after last header */ |
| 671 | # define UF_VERSION 1 /* 2-byte undofile version number */ |
| 672 | |
| 673 | /* |
| 674 | * Compute the hash for the current buffer text into hash[UNDO_HASH_SIZE]. |
| 675 | */ |
| 676 | void |
| 677 | u_compute_hash(hash) |
| 678 | char_u *hash; |
| 679 | { |
| 680 | context_sha256_T ctx; |
| 681 | linenr_T lnum; |
| 682 | char_u *p; |
| 683 | |
| 684 | sha256_start(&ctx); |
| 685 | for (lnum = 1; lnum < curbuf->b_ml.ml_line_count; ++lnum) |
| 686 | { |
| 687 | p = ml_get(lnum); |
| 688 | sha256_update(&ctx, p, STRLEN(p) + 1); |
| 689 | } |
| 690 | sha256_finish(&ctx, hash); |
| 691 | } |
| 692 | |
| 693 | /* |
| 694 | * Unserialize the pos_T at the current position in fp. |
| 695 | */ |
| 696 | static void |
| 697 | unserialize_pos(pos, fp) |
| 698 | pos_T *pos; |
| 699 | FILE *fp; |
| 700 | { |
| 701 | pos->lnum = get4c(fp); |
| 702 | pos->col = get4c(fp); |
| 703 | #ifdef FEAT_VIRTUALEDIT |
| 704 | pos->coladd = get4c(fp); |
| 705 | #else |
| 706 | (void)get4c(fp); |
| 707 | #endif |
| 708 | } |
| 709 | |
| 710 | /* |
| 711 | * Unserialize the visualinfo_T at the current position in fp. |
| 712 | */ |
| 713 | static void |
| 714 | unserialize_visualinfo(info, fp) |
| 715 | visualinfo_T *info; |
| 716 | FILE *fp; |
| 717 | { |
| 718 | unserialize_pos(&info->vi_start, fp); |
| 719 | unserialize_pos(&info->vi_end, fp); |
| 720 | info->vi_mode = get4c(fp); |
| 721 | info->vi_curswant = get4c(fp); |
| 722 | } |
| 723 | |
| 724 | /* |
| 725 | * Return an allocated string of the full path of the target undofile. |
| 726 | * When "reading" is TRUE find the file to read, go over all directories in |
| 727 | * 'undodir'. |
| 728 | * When "reading" is FALSE use the first name where the directory exists. |
| 729 | */ |
| 730 | static char_u * |
| 731 | u_get_undo_file_name(buf_ffname, reading) |
| 732 | char_u *buf_ffname; |
| 733 | int reading; |
| 734 | { |
| 735 | char_u *dirp; |
| 736 | char_u dir_name[IOSIZE + 1]; |
| 737 | char_u *munged_name = NULL; |
| 738 | char_u *undo_file_name = NULL; |
| 739 | int dir_len; |
| 740 | char_u *p; |
| 741 | struct stat st; |
| 742 | char_u *ffname = buf_ffname; |
| 743 | #ifdef HAVE_READLINK |
| 744 | char_u fname_buf[MAXPATHL]; |
| 745 | #endif |
| 746 | |
| 747 | if (ffname == NULL) |
| 748 | return NULL; |
| 749 | |
| 750 | #ifdef HAVE_READLINK |
| 751 | /* Expand symlink in the file name, so that we put the undo file with the |
| 752 | * actual file instead of with the symlink. */ |
| 753 | if (resolve_symlink(ffname, fname_buf) == OK) |
| 754 | ffname = fname_buf; |
| 755 | #endif |
| 756 | |
| 757 | /* Loop over 'undodir'. When reading find the first file that exists. |
| 758 | * When not reading use the first directory that exists or ".". */ |
| 759 | dirp = p_udir; |
| 760 | while (*dirp != NUL) |
| 761 | { |
| 762 | dir_len = copy_option_part(&dirp, dir_name, IOSIZE, ","); |
| 763 | if (dir_len == 1 && dir_name[0] == '.') |
| 764 | { |
| 765 | /* Use same directory as the ffname, |
| 766 | * "dir/name" -> "dir/.name.un~" */ |
| 767 | undo_file_name = vim_strnsave(ffname, STRLEN(ffname) + 5); |
| 768 | if (undo_file_name == NULL) |
| 769 | break; |
| 770 | p = gettail(undo_file_name); |
| 771 | mch_memmove(p + 1, p, STRLEN(p) + 1); |
| 772 | *p = '.'; |
| 773 | STRCAT(p, ".un~"); |
| 774 | } |
| 775 | else |
| 776 | { |
| 777 | dir_name[dir_len] = NUL; |
| 778 | if (mch_isdir(dir_name)) |
| 779 | { |
| 780 | if (munged_name == NULL) |
| 781 | { |
| 782 | munged_name = vim_strsave(ffname); |
| 783 | if (munged_name == NULL) |
| 784 | return NULL; |
| 785 | for (p = munged_name; *p != NUL; mb_ptr_adv(p)) |
| 786 | if (vim_ispathsep(*p)) |
| 787 | *p = '%'; |
| 788 | } |
| 789 | undo_file_name = concat_fnames(dir_name, munged_name, TRUE); |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | /* When reading check if the file exists. */ |
| 794 | if (undo_file_name != NULL && (!reading |
| 795 | || mch_stat((char *)undo_file_name, &st) >= 0)) |
| 796 | break; |
| 797 | vim_free(undo_file_name); |
| 798 | undo_file_name = NULL; |
| 799 | } |
| 800 | |
| 801 | vim_free(munged_name); |
| 802 | return undo_file_name; |
| 803 | } |
| 804 | |
| 805 | /* |
| 806 | * Load the undo tree from an undo file. |
| 807 | * If "name" is not NULL use it as the undo file name. This also means being |
| 808 | * a bit more verbose. |
| 809 | * Otherwise use curbuf->b_ffname to generate the undo file name. |
| 810 | * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text. |
| 811 | */ |
| 812 | void |
| 813 | u_read_undo(name, hash) |
| 814 | char_u *name; |
| 815 | char_u *hash; |
| 816 | { |
| 817 | char_u *file_name; |
| 818 | FILE *fp; |
| 819 | long magic, version, str_len; |
| 820 | char_u *line_ptr = NULL; |
| 821 | linenr_T line_lnum; |
| 822 | colnr_T line_colnr; |
| 823 | linenr_T line_count; |
| 824 | int uep_len; |
| 825 | int line_len; |
| 826 | int num_head; |
| 827 | long old_header_seq, new_header_seq, cur_header_seq; |
| 828 | long seq_last, seq_cur; |
| 829 | short old_idx = -1, new_idx = -1, cur_idx = -1; |
| 830 | long num_read_uhps = 0; |
| 831 | time_t seq_time; |
| 832 | int i, j; |
| 833 | int c; |
| 834 | short found_first_uep = 0; |
| 835 | char_u **array; |
| 836 | char_u *line; |
| 837 | u_entry_T *uep, *last_uep, *nuep; |
| 838 | u_header_T *uhp; |
| 839 | u_header_T **uhp_table = NULL; |
| 840 | char_u read_hash[UNDO_HASH_SIZE]; |
| 841 | |
| 842 | if (name == NULL) |
| 843 | { |
| 844 | file_name = u_get_undo_file_name(curbuf->b_ffname, TRUE); |
| 845 | if (file_name == NULL) |
| 846 | return; |
| 847 | } |
| 848 | else |
| 849 | file_name = name; |
| 850 | |
| 851 | if (p_verbose > 0) |
| 852 | smsg((char_u *)_("Reading undo file: %s"), file_name); |
| 853 | fp = mch_fopen((char *)file_name, "r"); |
| 854 | if (fp == NULL) |
| 855 | { |
| 856 | if (name != NULL || p_verbose > 0) |
| 857 | EMSG2(_("E822: Cannot open undo file for reading: %s"), file_name); |
| 858 | goto error; |
| 859 | } |
| 860 | |
| 861 | /* Begin overall file information */ |
| 862 | magic = get2c(fp); |
| 863 | if (magic != UF_START_MAGIC) |
| 864 | { |
| 865 | EMSG2(_("E823: Corrupted undo file: %s"), file_name); |
| 866 | goto error; |
| 867 | } |
| 868 | version = get2c(fp); |
| 869 | if (version != UF_VERSION) |
| 870 | { |
| 871 | EMSG2(_("E824: Incompatible undo file: %s"), file_name); |
| 872 | goto error; |
| 873 | } |
| 874 | |
| 875 | fread(read_hash, UNDO_HASH_SIZE, 1, fp); |
| 876 | line_count = (linenr_T)get4c(fp); |
| 877 | if (memcmp(hash, read_hash, UNDO_HASH_SIZE) != 0 |
| 878 | || line_count != curbuf->b_ml.ml_line_count) |
| 879 | { |
| 880 | if (p_verbose > 0 || name != NULL) |
| 881 | { |
| 882 | verbose_enter(); |
Bram Moolenaar | 7db5fc8 | 2010-05-24 11:59:29 +0200 | [diff] [blame] | 883 | give_warning((char_u *)_("File contents changed, cannot use undo info"), TRUE); |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 884 | verbose_leave(); |
| 885 | } |
| 886 | goto error; |
| 887 | } |
| 888 | |
| 889 | /* Begin undo data for U */ |
| 890 | str_len = get4c(fp); |
| 891 | if (str_len < 0) |
| 892 | goto error; |
| 893 | else if (str_len > 0) |
| 894 | { |
| 895 | if ((line_ptr = U_ALLOC_LINE(str_len)) == NULL) |
| 896 | goto error; |
| 897 | for (i = 0; i < str_len; i++) |
| 898 | line_ptr[i] = (char_u)getc(fp); |
| 899 | line_ptr[i] = NUL; |
| 900 | } |
| 901 | line_lnum = (linenr_T)get4c(fp); |
| 902 | line_colnr = (colnr_T)get4c(fp); |
| 903 | |
| 904 | /* Begin general undo data */ |
| 905 | old_header_seq = get4c(fp); |
| 906 | new_header_seq = get4c(fp); |
| 907 | cur_header_seq = get4c(fp); |
| 908 | num_head = get4c(fp); |
| 909 | seq_last = get4c(fp); |
| 910 | seq_cur = get4c(fp); |
| 911 | seq_time = get4c(fp); |
| 912 | |
| 913 | /* uhp_table will store the freshly created undo headers we allocate |
| 914 | * until we insert them into curbuf. The table remains sorted by the |
| 915 | * sequence numbers of the headers. */ |
| 916 | uhp_table = (u_header_T **)U_ALLOC_LINE(num_head * sizeof(u_header_T *)); |
| 917 | if (uhp_table == NULL) |
| 918 | goto error; |
| 919 | vim_memset(uhp_table, 0, num_head * sizeof(u_header_T *)); |
| 920 | |
| 921 | c = get2c(fp); |
| 922 | while (c == UF_HEADER_MAGIC) |
| 923 | { |
| 924 | found_first_uep = 0; |
| 925 | uhp = (u_header_T *)U_ALLOC_LINE((unsigned)sizeof(u_header_T)); |
| 926 | if (uhp == NULL) |
| 927 | goto error; |
| 928 | vim_memset(uhp, 0, sizeof(u_header_T)); |
| 929 | /* We're not actually trying to store pointers here. We're just storing |
| 930 | * IDs so we can swizzle them into pointers later - hence the type |
| 931 | * cast. */ |
| 932 | uhp->uh_next = (u_header_T *)(long)get4c(fp); |
| 933 | uhp->uh_prev = (u_header_T *)(long)get4c(fp); |
| 934 | uhp->uh_alt_next = (u_header_T *)(long)get4c(fp); |
| 935 | uhp->uh_alt_prev = (u_header_T *)(long)get4c(fp); |
| 936 | uhp->uh_seq = get4c(fp); |
| 937 | if (uhp->uh_seq <= 0) |
| 938 | { |
| 939 | EMSG2(_("E825: Undo file corruption: invalid uh_seq.: %s"), |
| 940 | file_name); |
| 941 | U_FREE_LINE(uhp); |
| 942 | goto error; |
| 943 | } |
| 944 | uhp->uh_walk = 0; |
| 945 | unserialize_pos(&uhp->uh_cursor, fp); |
| 946 | #ifdef FEAT_VIRTUALEDIT |
| 947 | uhp->uh_cursor_vcol = get4c(fp); |
| 948 | #else |
| 949 | (void)get4c(fp); |
| 950 | #endif |
| 951 | uhp->uh_flags = get2c(fp); |
| 952 | for (i = 0; i < NMARKS; ++i) |
| 953 | unserialize_pos(&uhp->uh_namedm[i], fp); |
| 954 | #ifdef FEAT_VISUAL |
| 955 | unserialize_visualinfo(&uhp->uh_visual, fp); |
| 956 | #else |
| 957 | { |
| 958 | visualinfo_T info; |
| 959 | unserialize_visualinfo(&info, fp); |
| 960 | } |
| 961 | #endif |
| 962 | uhp->uh_time = get4c(fp); |
| 963 | |
| 964 | /* Unserialize uep list. The first 4 bytes is the length of the |
| 965 | * entire uep in bytes minus the length of the strings within. |
| 966 | * -1 is a sentinel value meaning no more ueps.*/ |
| 967 | last_uep = NULL; |
| 968 | while ((uep_len = get4c(fp)) != -1) |
| 969 | { |
| 970 | uep = (u_entry_T *)U_ALLOC_LINE((unsigned)sizeof(u_entry_T)); |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 971 | if (uep == NULL) |
| 972 | goto error; |
Bram Moolenaar | 7db5fc8 | 2010-05-24 11:59:29 +0200 | [diff] [blame] | 973 | vim_memset(uep, 0, sizeof(u_entry_T)); |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 974 | uep->ue_top = get4c(fp); |
| 975 | uep->ue_bot = get4c(fp); |
| 976 | uep->ue_lcount = get4c(fp); |
| 977 | uep->ue_size = get4c(fp); |
| 978 | uep->ue_next = NULL; |
| 979 | array = (char_u **)U_ALLOC_LINE( |
| 980 | (unsigned)(sizeof(char_u *) * uep->ue_size)); |
| 981 | for (i = 0; i < uep->ue_size; i++) |
| 982 | { |
| 983 | line_len = get4c(fp); |
| 984 | /* U_ALLOC_LINE provides an extra byte for the NUL terminator.*/ |
| 985 | line = (char_u *)U_ALLOC_LINE( |
| 986 | (unsigned) (sizeof(char_u) * line_len)); |
| 987 | if (line == NULL) |
| 988 | goto error; |
| 989 | for (j = 0; j < line_len; j++) |
| 990 | { |
| 991 | line[j] = getc(fp); |
| 992 | } |
| 993 | line[j] = '\0'; |
| 994 | array[i] = line; |
| 995 | } |
| 996 | uep->ue_array = array; |
| 997 | if (found_first_uep == 0) |
| 998 | { |
| 999 | uhp->uh_entry = uep; |
| 1000 | found_first_uep = 1; |
| 1001 | } |
| 1002 | else |
| 1003 | { |
| 1004 | last_uep->ue_next = uep; |
| 1005 | } |
| 1006 | last_uep = uep; |
| 1007 | } |
| 1008 | |
| 1009 | /* Insertion sort the uhp into the table by its uh_seq. This is |
| 1010 | * required because, while the number of uhps is limited to |
| 1011 | * num_heads, and the uh_seq order is monotonic with respect to |
| 1012 | * creation time, the starting uh_seq can be > 0 if any undolevel |
| 1013 | * culling was done at undofile write time, and there can be uh_seq |
| 1014 | * gaps in the uhps. |
| 1015 | */ |
| 1016 | for (i = num_read_uhps - 1; i >= -1; i--) |
| 1017 | { |
| 1018 | /* if i == -1, we've hit the leftmost side of the table, so insert |
| 1019 | * at uhp_table[0]. */ |
| 1020 | if (i == -1 || uhp->uh_seq > uhp_table[i]->uh_seq) |
| 1021 | { |
| 1022 | /* If we've had to move from the rightmost side of the table, |
| 1023 | * we have to shift everything to the right by one spot. */ |
| 1024 | if (i < num_read_uhps - 1) |
| 1025 | { |
| 1026 | memmove(uhp_table + i + 2, uhp_table + i + 1, |
| 1027 | (num_read_uhps - i) * sizeof(u_header_T *)); |
| 1028 | } |
| 1029 | uhp_table[i + 1] = uhp; |
| 1030 | break; |
| 1031 | } |
| 1032 | else if (uhp->uh_seq == uhp_table[i]->uh_seq) |
| 1033 | { |
| 1034 | EMSG2(_("E826 Undo file corruption: duplicate uh_seq: %s"), |
| 1035 | file_name); |
| 1036 | goto error; |
| 1037 | } |
| 1038 | } |
| 1039 | num_read_uhps++; |
| 1040 | c = get2c(fp); |
| 1041 | } |
| 1042 | |
| 1043 | if (c != UF_END_MAGIC) |
| 1044 | { |
| 1045 | EMSG2(_("E827: Undo file corruption; no end marker: %s"), file_name); |
| 1046 | goto error; |
| 1047 | } |
| 1048 | |
| 1049 | /* We've organized all of the uhps into a table sorted by uh_seq. Now we |
| 1050 | * iterate through the table and swizzle each sequence number we've |
| 1051 | * stored in uh_foo into a pointer corresponding to the header with that |
| 1052 | * sequence number. Then free curbuf's old undo structure, give curbuf |
| 1053 | * the updated {old,new,cur}head pointers, and then free the table. */ |
| 1054 | for (i = 0; i < num_head; i++) |
| 1055 | { |
| 1056 | uhp = uhp_table[i]; |
| 1057 | if (uhp == NULL) |
| 1058 | continue; |
| 1059 | for (j = 0; j < num_head; j++) |
| 1060 | { |
| 1061 | if (uhp_table[j] == NULL) |
| 1062 | continue; |
| 1063 | if (uhp_table[j]->uh_seq == (long)uhp->uh_next) |
| 1064 | uhp->uh_next = uhp_table[j]; |
| 1065 | if (uhp_table[j]->uh_seq == (long)uhp->uh_prev) |
| 1066 | uhp->uh_prev = uhp_table[j]; |
| 1067 | if (uhp_table[j]->uh_seq == (long)uhp->uh_alt_next) |
| 1068 | uhp->uh_alt_next = uhp_table[j]; |
| 1069 | if (uhp_table[j]->uh_seq == (long)uhp->uh_alt_prev) |
| 1070 | uhp->uh_alt_prev = uhp_table[j]; |
| 1071 | } |
| 1072 | if (old_header_seq > 0 && old_idx < 0 && uhp->uh_seq == old_header_seq) |
| 1073 | old_idx = i; |
| 1074 | if (new_header_seq > 0 && new_idx < 0 && uhp->uh_seq == new_header_seq) |
| 1075 | new_idx = i; |
| 1076 | if (cur_header_seq > 0 && cur_idx < 0 && uhp->uh_seq == cur_header_seq) |
| 1077 | cur_idx = i; |
| 1078 | } |
| 1079 | u_blockfree(curbuf); |
| 1080 | curbuf->b_u_oldhead = old_idx < 0 ? 0 : uhp_table[old_idx]; |
| 1081 | curbuf->b_u_newhead = new_idx < 0 ? 0 : uhp_table[new_idx]; |
| 1082 | curbuf->b_u_curhead = cur_idx < 0 ? 0 : uhp_table[cur_idx]; |
| 1083 | curbuf->b_u_line_ptr = line_ptr; |
| 1084 | curbuf->b_u_line_lnum = line_lnum; |
| 1085 | curbuf->b_u_line_colnr = line_colnr; |
| 1086 | curbuf->b_u_numhead = num_head; |
| 1087 | curbuf->b_u_seq_last = seq_last; |
| 1088 | curbuf->b_u_seq_cur = seq_cur; |
| 1089 | curbuf->b_u_seq_time = seq_time; |
| 1090 | U_FREE_LINE(uhp_table); |
| 1091 | #ifdef U_DEBUG |
| 1092 | u_check(TRUE); |
| 1093 | #endif |
| 1094 | if (name != NULL) |
| 1095 | smsg((char_u *)_("Finished reading undo file %s"), file_name); |
| 1096 | goto theend; |
| 1097 | |
| 1098 | error: |
| 1099 | if (line_ptr != NULL) |
| 1100 | U_FREE_LINE(line_ptr); |
| 1101 | if (uhp_table != NULL) |
| 1102 | { |
| 1103 | for (i = 0; i < num_head; i++) |
| 1104 | { |
| 1105 | if (uhp_table[i] != NULL) |
| 1106 | { |
| 1107 | uep = uhp_table[i]->uh_entry; |
| 1108 | while (uep != NULL) |
| 1109 | { |
| 1110 | nuep = uep->ue_next; |
| 1111 | u_freeentry(uep, uep->ue_size); |
| 1112 | uep = nuep; |
| 1113 | } |
| 1114 | U_FREE_LINE(uhp_table[i]); |
| 1115 | } |
| 1116 | } |
| 1117 | U_FREE_LINE(uhp_table); |
| 1118 | } |
| 1119 | |
| 1120 | theend: |
| 1121 | if (fp != NULL) |
| 1122 | fclose(fp); |
| 1123 | if (file_name != name) |
| 1124 | vim_free(file_name); |
| 1125 | return; |
| 1126 | } |
| 1127 | |
| 1128 | /* |
| 1129 | * Serialize "uep" to "fp". |
| 1130 | */ |
| 1131 | static int |
| 1132 | serialize_uep(uep, fp) |
| 1133 | u_entry_T *uep; |
| 1134 | FILE *fp; |
| 1135 | { |
| 1136 | int i; |
| 1137 | int uep_len; |
| 1138 | int *entry_lens; |
| 1139 | |
| 1140 | if (uep->ue_size > 0) |
| 1141 | entry_lens = (int *)alloc(uep->ue_size * sizeof(int)); |
| 1142 | |
| 1143 | /* Define uep_len to be the size of the entire uep minus the size of its |
| 1144 | * component strings, in bytes. The sizes of the component strings |
| 1145 | * are written before each individual string. |
| 1146 | * We have 4 entries each of 4 bytes, plus ue_size * 4 bytes |
| 1147 | * of string size information. */ |
| 1148 | |
| 1149 | uep_len = uep->ue_size * 4; |
| 1150 | /* Collect sizing information for later serialization. */ |
| 1151 | for (i = 0; i < uep->ue_size; i++) |
| 1152 | { |
| 1153 | entry_lens[i] = (int)STRLEN(uep->ue_array[i]); |
| 1154 | uep_len += entry_lens[i]; |
| 1155 | } |
| 1156 | put_bytes(fp, (long_u)uep_len, 4); |
| 1157 | put_bytes(fp, (long_u)uep->ue_top, 4); |
| 1158 | put_bytes(fp, (long_u)uep->ue_bot, 4); |
| 1159 | put_bytes(fp, (long_u)uep->ue_lcount, 4); |
| 1160 | put_bytes(fp, (long_u)uep->ue_size, 4); |
| 1161 | for (i = 0; i < uep->ue_size; i++) |
| 1162 | { |
| 1163 | if (put_bytes(fp, (long_u)entry_lens[i], 4) == FAIL) |
| 1164 | return FAIL; |
| 1165 | fprintf(fp, "%s", uep->ue_array[i]); |
| 1166 | } |
| 1167 | if (uep->ue_size > 0) |
| 1168 | vim_free(entry_lens); |
| 1169 | return OK; |
| 1170 | } |
| 1171 | |
| 1172 | /* |
| 1173 | * Serialize "pos" to "fp". |
| 1174 | */ |
| 1175 | static void |
| 1176 | serialize_pos(pos, fp) |
| 1177 | pos_T pos; |
| 1178 | FILE *fp; |
| 1179 | { |
| 1180 | put_bytes(fp, (long_u)pos.lnum, 4); |
| 1181 | put_bytes(fp, (long_u)pos.col, 4); |
| 1182 | #ifdef FEAT_VIRTUALEDIT |
| 1183 | put_bytes(fp, (long_u)pos.coladd, 4); |
| 1184 | #else |
| 1185 | put_bytes(fp, (long_u)0, 4); |
| 1186 | #endif |
| 1187 | } |
| 1188 | |
| 1189 | /* |
| 1190 | * Serialize "info" to "fp". |
| 1191 | */ |
| 1192 | static void |
| 1193 | serialize_visualinfo(info, fp) |
| 1194 | visualinfo_T info; |
| 1195 | FILE *fp; |
| 1196 | { |
| 1197 | serialize_pos(info.vi_start, fp); |
| 1198 | serialize_pos(info.vi_end, fp); |
| 1199 | put_bytes(fp, (long_u)info.vi_mode, 4); |
| 1200 | put_bytes(fp, (long_u)info.vi_curswant, 4); |
| 1201 | } |
| 1202 | |
| 1203 | static char_u e_not_open[] = N_("E828: Cannot open undo file for writing: %s"); |
| 1204 | |
| 1205 | /* |
| 1206 | * Write the undo tree in an undo file. |
| 1207 | * When "name" is not NULL, use it as the name of the undo file. |
| 1208 | * Otherwise use buf->b_ffname to generate the undo file name. |
| 1209 | * "buf" must never be null, buf->b_ffname is used to obtain the original file |
| 1210 | * permissions. |
| 1211 | * "forceit" is TRUE for ":wundo!", FALSE otherwise. |
| 1212 | * "hash[UNDO_HASH_SIZE]" must be the hash value of the buffer text. |
| 1213 | */ |
| 1214 | void |
| 1215 | u_write_undo(name, forceit, buf, hash) |
| 1216 | char_u *name; |
| 1217 | int forceit; |
| 1218 | buf_T *buf; |
| 1219 | char_u *hash; |
| 1220 | { |
| 1221 | u_header_T *uhp; |
| 1222 | u_entry_T *uep; |
| 1223 | char_u *file_name; |
| 1224 | int str_len, i, uep_len, mark; |
| 1225 | int fd; |
| 1226 | FILE *fp = NULL; |
| 1227 | int perm; |
| 1228 | int write_ok = FALSE; |
| 1229 | #ifdef UNIX |
| 1230 | struct stat st_old; |
| 1231 | struct stat st_new; |
| 1232 | #endif |
| 1233 | |
| 1234 | if (name == NULL) |
| 1235 | { |
| 1236 | file_name = u_get_undo_file_name(buf->b_ffname, FALSE); |
| 1237 | if (file_name == NULL) |
| 1238 | return; |
| 1239 | } |
| 1240 | else |
| 1241 | file_name = name; |
| 1242 | |
| 1243 | #ifdef UNIX |
| 1244 | if (mch_stat((char *)buf->b_ffname, &st_old) >= 0) |
| 1245 | perm = st_old.st_mode; |
| 1246 | else |
| 1247 | perm = 0600; |
| 1248 | #else |
| 1249 | perm = mch_getperm(buf->b_ffname); |
| 1250 | if (perm < 0) |
| 1251 | perm = 0600; |
| 1252 | #endif |
| 1253 | /* set file protection same as original file, but strip s-bit */ |
| 1254 | perm = perm & 0777; |
| 1255 | |
| 1256 | /* If the undo file exists, verify that it actually is an undo file, and |
| 1257 | * delete it. */ |
| 1258 | if (mch_getperm(file_name) >= 0) |
| 1259 | { |
| 1260 | if (name == NULL || !forceit) |
| 1261 | { |
| 1262 | /* Check we can read it and it's an undo file. */ |
| 1263 | fd = mch_open((char *)file_name, O_RDONLY|O_EXTRA, 0); |
| 1264 | if (fd < 0) |
| 1265 | { |
| 1266 | if (name != NULL || p_verbose > 0) |
| 1267 | smsg((char_u *)_("Will not overwrite with undo file, cannot read: %s"), |
| 1268 | file_name); |
| 1269 | goto theend; |
| 1270 | } |
| 1271 | else |
| 1272 | { |
| 1273 | char_u buf[2]; |
| 1274 | |
| 1275 | vim_read(fd, buf, 2); |
| 1276 | close(fd); |
| 1277 | if ((buf[0] << 8) + buf[1] != UF_START_MAGIC) |
| 1278 | { |
| 1279 | if (name != NULL || p_verbose > 0) |
| 1280 | smsg((char_u *)_("Will not overwrite, this is not an undo file: %s"), |
| 1281 | file_name); |
| 1282 | goto theend; |
| 1283 | } |
| 1284 | } |
| 1285 | } |
| 1286 | mch_remove(file_name); |
| 1287 | } |
| 1288 | |
| 1289 | fd = mch_open((char *)file_name, |
| 1290 | O_CREAT|O_EXTRA|O_WRONLY|O_EXCL|O_NOFOLLOW, perm); |
| 1291 | (void)mch_setperm(file_name, perm); |
| 1292 | if (fd < 0) |
| 1293 | { |
| 1294 | EMSG2(_(e_not_open), file_name); |
| 1295 | goto theend; |
| 1296 | } |
| 1297 | if (p_verbose > 0) |
| 1298 | smsg((char_u *)_("Writing undo file: %s"), file_name); |
| 1299 | |
| 1300 | #ifdef UNIX |
| 1301 | /* |
| 1302 | * Try to set the group of the undo file same as the original file. If |
| 1303 | * this fails, set the protection bits for the group same as the |
| 1304 | * protection bits for others. |
| 1305 | */ |
| 1306 | if (mch_stat((char *)file_name, &st_new) >= 0 |
| 1307 | && st_new.st_gid != st_old.st_gid |
| 1308 | # ifdef HAVE_FCHOWN /* sequent-ptx lacks fchown() */ |
| 1309 | && fchown(fd, (uid_t)-1, st_old.st_gid) != 0 |
| 1310 | # endif |
| 1311 | ) |
| 1312 | mch_setperm(file_name, (perm & 0707) | ((perm & 07) << 3)); |
| 1313 | # ifdef HAVE_SELINUX |
| 1314 | mch_copy_sec(buf->b_ffname, file_name); |
| 1315 | # endif |
| 1316 | #endif |
| 1317 | |
| 1318 | fp = fdopen(fd, "w"); |
| 1319 | if (fp == NULL) |
| 1320 | { |
| 1321 | EMSG2(_(e_not_open), file_name); |
| 1322 | close(fd); |
| 1323 | mch_remove(file_name); |
| 1324 | goto theend; |
| 1325 | } |
| 1326 | |
| 1327 | /* Start writing, first overall file information */ |
| 1328 | put_bytes(fp, (long_u)UF_START_MAGIC, 2); |
| 1329 | put_bytes(fp, (long_u)UF_VERSION, 2); |
| 1330 | |
| 1331 | /* Write a hash of the buffer text, so that we can verify it is still the |
| 1332 | * same when reading the buffer text. */ |
| 1333 | if (fwrite(hash, (size_t)UNDO_HASH_SIZE, (size_t)1, fp) != 1) |
| 1334 | goto write_error; |
| 1335 | put_bytes(fp, (long_u)buf->b_ml.ml_line_count, 4); |
| 1336 | |
| 1337 | /* Begin undo data for U */ |
| 1338 | str_len = buf->b_u_line_ptr != NULL ? STRLEN(buf->b_u_line_ptr) : 0; |
| 1339 | put_bytes(fp, (long_u)str_len, 4); |
| 1340 | if (str_len > 0 && fwrite(buf->b_u_line_ptr, (size_t)str_len, |
| 1341 | (size_t)1, fp) != 1) |
| 1342 | goto write_error; |
| 1343 | |
| 1344 | put_bytes(fp, (long_u)buf->b_u_line_lnum, 4); |
| 1345 | put_bytes(fp, (long_u)buf->b_u_line_colnr, 4); |
| 1346 | |
| 1347 | /* Begin general undo data */ |
| 1348 | uhp = buf->b_u_oldhead; |
| 1349 | put_bytes(fp, (long_u)(uhp != NULL ? uhp->uh_seq : 0), 4); |
| 1350 | |
| 1351 | uhp = buf->b_u_newhead; |
| 1352 | put_bytes(fp, (long_u)(uhp != NULL ? uhp->uh_seq : 0), 4); |
| 1353 | |
| 1354 | uhp = buf->b_u_curhead; |
| 1355 | put_bytes(fp, (long_u)(uhp != NULL ? uhp->uh_seq : 0), 4); |
| 1356 | |
| 1357 | put_bytes(fp, (long_u)buf->b_u_numhead, 4); |
| 1358 | put_bytes(fp, (long_u)buf->b_u_seq_last, 4); |
| 1359 | put_bytes(fp, (long_u)buf->b_u_seq_cur, 4); |
| 1360 | put_bytes(fp, (long_u)buf->b_u_seq_time, 4); |
| 1361 | |
| 1362 | /* Iteratively serialize UHPs and their UEPs from the top down. */ |
| 1363 | mark = ++lastmark; |
| 1364 | uhp = buf->b_u_oldhead; |
| 1365 | while (uhp != NULL) |
| 1366 | { |
| 1367 | /* Serialize current UHP if we haven't seen it */ |
| 1368 | if (uhp->uh_walk != mark) |
| 1369 | { |
| 1370 | if (put_bytes(fp, (long_u)UF_HEADER_MAGIC, 2) == FAIL) |
| 1371 | goto write_error; |
| 1372 | |
| 1373 | put_bytes(fp, (long_u)((uhp->uh_next != NULL) |
| 1374 | ? uhp->uh_next->uh_seq : 0), 4); |
| 1375 | put_bytes(fp, (long_u)((uhp->uh_prev != NULL) |
| 1376 | ? uhp->uh_prev->uh_seq : 0), 4); |
| 1377 | put_bytes(fp, (long_u)((uhp->uh_alt_next != NULL) |
| 1378 | ? uhp->uh_alt_next->uh_seq : 0), 4); |
| 1379 | put_bytes(fp, (long_u)((uhp->uh_alt_prev != NULL) |
| 1380 | ? uhp->uh_alt_prev->uh_seq : 0), 4); |
| 1381 | put_bytes(fp, uhp->uh_seq, 4); |
| 1382 | serialize_pos(uhp->uh_cursor, fp); |
| 1383 | #ifdef FEAT_VIRTUALEDIT |
| 1384 | put_bytes(fp, (long_u)uhp->uh_cursor_vcol, 4); |
| 1385 | #else |
| 1386 | put_bytes(fp, (long_u)0, 4); |
| 1387 | #endif |
| 1388 | put_bytes(fp, (long_u)uhp->uh_flags, 2); |
| 1389 | /* Assume NMARKS will stay the same. */ |
| 1390 | for (i = 0; i < NMARKS; ++i) |
| 1391 | { |
| 1392 | serialize_pos(uhp->uh_namedm[i], fp); |
| 1393 | } |
| 1394 | #ifdef FEAT_VISUAL |
| 1395 | serialize_visualinfo(uhp->uh_visual, fp); |
| 1396 | #endif |
| 1397 | put_bytes(fp, (long_u)uhp->uh_time, 4); |
| 1398 | |
| 1399 | uep = uhp->uh_entry; |
| 1400 | while (uep != NULL) |
| 1401 | { |
| 1402 | if (serialize_uep(uep, fp) == FAIL) |
| 1403 | goto write_error; |
| 1404 | uep = uep->ue_next; |
| 1405 | } |
| 1406 | /* Sentinel value: no more ueps */ |
| 1407 | uep_len = -1; |
| 1408 | put_bytes(fp, (long_u)uep_len, 4); |
| 1409 | uhp->uh_walk = mark; |
| 1410 | } |
| 1411 | |
| 1412 | /* Now walk through the tree - algorithm from undo_time */ |
| 1413 | if (uhp->uh_prev != NULL && uhp->uh_prev->uh_walk != mark) |
| 1414 | uhp = uhp->uh_prev; |
| 1415 | else if (uhp->uh_alt_next != NULL && uhp->uh_alt_next->uh_walk != mark) |
| 1416 | uhp = uhp->uh_alt_next; |
| 1417 | else if (uhp->uh_next != NULL && uhp->uh_alt_prev == NULL |
| 1418 | && uhp->uh_next->uh_walk != mark) |
| 1419 | uhp = uhp->uh_next; |
| 1420 | else if (uhp->uh_alt_prev != NULL) |
| 1421 | uhp = uhp->uh_alt_prev; |
| 1422 | else |
| 1423 | uhp = uhp->uh_next; |
| 1424 | } |
| 1425 | |
| 1426 | if (put_bytes(fp, (long_u)UF_END_MAGIC, 2) == OK) |
| 1427 | write_ok = TRUE; |
| 1428 | |
| 1429 | write_error: |
| 1430 | fclose(fp); |
| 1431 | if (!write_ok) |
| 1432 | EMSG2(_("E829: write error in undo file: %s"), file_name); |
| 1433 | |
| 1434 | #if defined(MACOS_CLASSIC) || defined(WIN3264) |
| 1435 | (void)mch_copy_file_attribute(buf->b_ffname, file_name); |
| 1436 | #endif |
| 1437 | #ifdef HAVE_ACL |
| 1438 | { |
| 1439 | vim_acl_T acl; |
| 1440 | |
| 1441 | /* For systems that support ACL: get the ACL from the original file. */ |
| 1442 | acl = mch_get_acl(buf->b_ffname); |
| 1443 | mch_set_acl(file_name, acl); |
| 1444 | } |
| 1445 | #endif |
| 1446 | |
| 1447 | theend: |
| 1448 | if (file_name != name) |
| 1449 | vim_free(file_name); |
| 1450 | } |
| 1451 | |
| 1452 | #endif /* FEAT_PERSISTENT_UNDO */ |
| 1453 | |
| 1454 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1455 | /* |
| 1456 | * If 'cpoptions' contains 'u': Undo the previous undo or redo (vi compatible). |
| 1457 | * If 'cpoptions' does not contain 'u': Always undo. |
| 1458 | */ |
| 1459 | void |
| 1460 | u_undo(count) |
| 1461 | int count; |
| 1462 | { |
| 1463 | /* |
| 1464 | * If we get an undo command while executing a macro, we behave like the |
| 1465 | * original vi. If this happens twice in one macro the result will not |
| 1466 | * be compatible. |
| 1467 | */ |
| 1468 | if (curbuf->b_u_synced == FALSE) |
| 1469 | { |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 1470 | u_sync(TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1471 | count = 1; |
| 1472 | } |
| 1473 | |
| 1474 | if (vim_strchr(p_cpo, CPO_UNDO) == NULL) |
| 1475 | undo_undoes = TRUE; |
| 1476 | else |
| 1477 | undo_undoes = !undo_undoes; |
| 1478 | u_doit(count); |
| 1479 | } |
| 1480 | |
| 1481 | /* |
| 1482 | * If 'cpoptions' contains 'u': Repeat the previous undo or redo. |
| 1483 | * If 'cpoptions' does not contain 'u': Always redo. |
| 1484 | */ |
| 1485 | void |
| 1486 | u_redo(count) |
| 1487 | int count; |
| 1488 | { |
| 1489 | if (vim_strchr(p_cpo, CPO_UNDO) == NULL) |
| 1490 | undo_undoes = FALSE; |
| 1491 | u_doit(count); |
| 1492 | } |
| 1493 | |
| 1494 | /* |
| 1495 | * Undo or redo, depending on 'undo_undoes', 'count' times. |
| 1496 | */ |
| 1497 | static void |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 1498 | u_doit(startcount) |
| 1499 | int startcount; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1500 | { |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 1501 | int count = startcount; |
| 1502 | |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 1503 | if (!undo_allowed()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1504 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1505 | |
| 1506 | u_newcount = 0; |
| 1507 | u_oldcount = 0; |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 1508 | if (curbuf->b_ml.ml_flags & ML_EMPTY) |
| 1509 | u_oldcount = -1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1510 | while (count--) |
| 1511 | { |
| 1512 | if (undo_undoes) |
| 1513 | { |
| 1514 | if (curbuf->b_u_curhead == NULL) /* first undo */ |
| 1515 | curbuf->b_u_curhead = curbuf->b_u_newhead; |
| 1516 | else if (p_ul > 0) /* multi level undo */ |
| 1517 | /* get next undo */ |
| 1518 | curbuf->b_u_curhead = curbuf->b_u_curhead->uh_next; |
| 1519 | /* nothing to undo */ |
| 1520 | if (curbuf->b_u_numhead == 0 || curbuf->b_u_curhead == NULL) |
| 1521 | { |
| 1522 | /* stick curbuf->b_u_curhead at end */ |
| 1523 | curbuf->b_u_curhead = curbuf->b_u_oldhead; |
| 1524 | beep_flush(); |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 1525 | if (count == startcount - 1) |
| 1526 | { |
| 1527 | MSG(_("Already at oldest change")); |
| 1528 | return; |
| 1529 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1530 | break; |
| 1531 | } |
| 1532 | |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 1533 | u_undoredo(TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1534 | } |
| 1535 | else |
| 1536 | { |
| 1537 | if (curbuf->b_u_curhead == NULL || p_ul <= 0) |
| 1538 | { |
| 1539 | beep_flush(); /* nothing to redo */ |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 1540 | if (count == startcount - 1) |
| 1541 | { |
| 1542 | MSG(_("Already at newest change")); |
| 1543 | return; |
| 1544 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1545 | break; |
| 1546 | } |
| 1547 | |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 1548 | u_undoredo(FALSE); |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1549 | |
| 1550 | /* Advance for next redo. Set "newhead" when at the end of the |
| 1551 | * redoable changes. */ |
| 1552 | if (curbuf->b_u_curhead->uh_prev == NULL) |
| 1553 | curbuf->b_u_newhead = curbuf->b_u_curhead; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1554 | curbuf->b_u_curhead = curbuf->b_u_curhead->uh_prev; |
| 1555 | } |
| 1556 | } |
Bram Moolenaar | db552d60 | 2006-03-23 22:59:57 +0000 | [diff] [blame] | 1557 | u_undo_end(undo_undoes, FALSE); |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1558 | } |
| 1559 | |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1560 | /* |
| 1561 | * Undo or redo over the timeline. |
| 1562 | * 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] | 1563 | * When "sec" is FALSE make "step" steps, when "sec" is TRUE use "step" as |
| 1564 | * seconds. |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1565 | * When "absolute" is TRUE use "step" as the sequence number to jump to. |
| 1566 | * "sec" must be FALSE then. |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1567 | */ |
| 1568 | void |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1569 | undo_time(step, sec, absolute) |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1570 | long step; |
| 1571 | int sec; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1572 | int absolute; |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1573 | { |
| 1574 | long target; |
| 1575 | long closest; |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1576 | long closest_start; |
| 1577 | long closest_seq = 0; |
| 1578 | long val; |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1579 | u_header_T *uhp; |
| 1580 | u_header_T *last; |
| 1581 | int mark; |
| 1582 | int nomark; |
| 1583 | int round; |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1584 | int dosec = sec; |
| 1585 | int above = FALSE; |
Bram Moolenaar | 433f7c8 | 2006-03-21 21:29:36 +0000 | [diff] [blame] | 1586 | int did_undo = TRUE; |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1587 | |
Bram Moolenaar | 7d47b6e | 2006-03-15 22:59:18 +0000 | [diff] [blame] | 1588 | /* First make sure the current undoable change is synced. */ |
| 1589 | if (curbuf->b_u_synced == FALSE) |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 1590 | u_sync(TRUE); |
Bram Moolenaar | 7d47b6e | 2006-03-15 22:59:18 +0000 | [diff] [blame] | 1591 | |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1592 | u_newcount = 0; |
| 1593 | u_oldcount = 0; |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 1594 | if (curbuf->b_ml.ml_flags & ML_EMPTY) |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1595 | u_oldcount = -1; |
| 1596 | |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 1597 | /* "target" is the node below which we want to be. |
| 1598 | * Init "closest" to a value we can't reach. */ |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1599 | if (absolute) |
| 1600 | { |
| 1601 | target = step; |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 1602 | closest = -1; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1603 | } |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 1604 | else |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1605 | { |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 1606 | /* When doing computations with time_t subtract starttime, because |
| 1607 | * time_t converted to a long may result in a wrong number. */ |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1608 | if (sec) |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 1609 | target = (long)(curbuf->b_u_seq_time - starttime) + step; |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1610 | else |
| 1611 | target = curbuf->b_u_seq_cur + step; |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 1612 | if (step < 0) |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1613 | { |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 1614 | if (target < 0) |
| 1615 | target = 0; |
| 1616 | closest = -1; |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1617 | } |
| 1618 | else |
| 1619 | { |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 1620 | if (sec) |
Bram Moolenaar | db552d60 | 2006-03-23 22:59:57 +0000 | [diff] [blame] | 1621 | closest = (long)(time(NULL) - starttime + 1); |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 1622 | else |
| 1623 | closest = curbuf->b_u_seq_last + 2; |
| 1624 | if (target >= closest) |
| 1625 | target = closest - 1; |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1626 | } |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1627 | } |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1628 | closest_start = closest; |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 1629 | closest_seq = curbuf->b_u_seq_cur; |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1630 | |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1631 | /* |
| 1632 | * May do this twice: |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1633 | * 1. Search for "target", update "closest" to the best match found. |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1634 | * 2. If "target" not found search for "closest". |
| 1635 | * |
| 1636 | * When using the closest time we use the sequence number in the second |
| 1637 | * round, because there may be several entries with the same time. |
| 1638 | */ |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1639 | for (round = 1; round <= 2; ++round) |
| 1640 | { |
| 1641 | /* Find the path from the current state to where we want to go. The |
| 1642 | * desired state can be anywhere in the undo tree, need to go all over |
| 1643 | * it. We put "nomark" in uh_walk where we have been without success, |
| 1644 | * "mark" where it could possibly be. */ |
| 1645 | mark = ++lastmark; |
| 1646 | nomark = ++lastmark; |
| 1647 | |
| 1648 | if (curbuf->b_u_curhead == NULL) /* at leaf of the tree */ |
| 1649 | uhp = curbuf->b_u_newhead; |
| 1650 | else |
| 1651 | uhp = curbuf->b_u_curhead; |
| 1652 | |
| 1653 | while (uhp != NULL) |
| 1654 | { |
| 1655 | uhp->uh_walk = mark; |
Bram Moolenaar | db552d60 | 2006-03-23 22:59:57 +0000 | [diff] [blame] | 1656 | val = (long)(dosec ? (uhp->uh_time - starttime) : uhp->uh_seq); |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1657 | |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1658 | if (round == 1) |
| 1659 | { |
| 1660 | /* Remember the header that is closest to the target. |
| 1661 | * It must be at least in the right direction (checked with |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 1662 | * "b_u_seq_cur"). When the timestamp is equal find the |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1663 | * highest/lowest sequence number. */ |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 1664 | if ((step < 0 ? uhp->uh_seq <= curbuf->b_u_seq_cur |
| 1665 | : uhp->uh_seq > curbuf->b_u_seq_cur) |
| 1666 | && ((dosec && val == closest) |
| 1667 | ? (step < 0 |
| 1668 | ? uhp->uh_seq < closest_seq |
| 1669 | : uhp->uh_seq > closest_seq) |
| 1670 | : closest == closest_start |
| 1671 | || (val > target |
| 1672 | ? (closest > target |
| 1673 | ? val - target <= closest - target |
| 1674 | : val - target <= target - closest) |
| 1675 | : (closest > target |
| 1676 | ? target - val <= closest - target |
| 1677 | : target - val <= target - closest)))) |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1678 | { |
| 1679 | closest = val; |
| 1680 | closest_seq = uhp->uh_seq; |
| 1681 | } |
| 1682 | } |
| 1683 | |
| 1684 | /* Quit searching when we found a match. But when searching for a |
| 1685 | * time we need to continue looking for the best uh_seq. */ |
| 1686 | if (target == val && !dosec) |
| 1687 | break; |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1688 | |
| 1689 | /* go down in the tree if we haven't been there */ |
| 1690 | if (uhp->uh_prev != NULL && uhp->uh_prev->uh_walk != nomark |
| 1691 | && uhp->uh_prev->uh_walk != mark) |
| 1692 | uhp = uhp->uh_prev; |
| 1693 | |
| 1694 | /* go to alternate branch if we haven't been there */ |
| 1695 | else if (uhp->uh_alt_next != NULL |
| 1696 | && uhp->uh_alt_next->uh_walk != nomark |
| 1697 | && uhp->uh_alt_next->uh_walk != mark) |
| 1698 | uhp = uhp->uh_alt_next; |
| 1699 | |
| 1700 | /* go up in the tree if we haven't been there and we are at the |
| 1701 | * start of alternate branches */ |
| 1702 | else if (uhp->uh_next != NULL && uhp->uh_alt_prev == NULL |
| 1703 | && uhp->uh_next->uh_walk != nomark |
| 1704 | && uhp->uh_next->uh_walk != mark) |
Bram Moolenaar | db552d60 | 2006-03-23 22:59:57 +0000 | [diff] [blame] | 1705 | { |
| 1706 | /* If still at the start we don't go through this change. */ |
| 1707 | if (uhp == curbuf->b_u_curhead) |
| 1708 | uhp->uh_walk = nomark; |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1709 | uhp = uhp->uh_next; |
Bram Moolenaar | db552d60 | 2006-03-23 22:59:57 +0000 | [diff] [blame] | 1710 | } |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1711 | |
| 1712 | else |
| 1713 | { |
| 1714 | /* need to backtrack; mark this node as useless */ |
| 1715 | uhp->uh_walk = nomark; |
| 1716 | if (uhp->uh_alt_prev != NULL) |
| 1717 | uhp = uhp->uh_alt_prev; |
| 1718 | else |
| 1719 | uhp = uhp->uh_next; |
| 1720 | } |
| 1721 | } |
| 1722 | |
| 1723 | if (uhp != NULL) /* found it */ |
| 1724 | break; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1725 | |
| 1726 | if (absolute) |
| 1727 | { |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 1728 | EMSGN(_("E830: Undo number %ld not found"), step); |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1729 | return; |
| 1730 | } |
| 1731 | |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1732 | if (closest == closest_start) |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1733 | { |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1734 | if (step < 0) |
| 1735 | MSG(_("Already at oldest change")); |
| 1736 | else |
| 1737 | MSG(_("Already at newest change")); |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1738 | return; |
| 1739 | } |
| 1740 | |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1741 | target = closest_seq; |
| 1742 | dosec = FALSE; |
| 1743 | if (step < 0) |
| 1744 | above = TRUE; /* stop above the header */ |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1745 | } |
| 1746 | |
| 1747 | /* If we found it: Follow the path to go to where we want to be. */ |
| 1748 | if (uhp != NULL) |
| 1749 | { |
| 1750 | /* |
| 1751 | * First go up the tree as much as needed. |
| 1752 | */ |
| 1753 | for (;;) |
| 1754 | { |
| 1755 | uhp = curbuf->b_u_curhead; |
| 1756 | if (uhp == NULL) |
| 1757 | uhp = curbuf->b_u_newhead; |
| 1758 | else |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1759 | uhp = uhp->uh_next; |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 1760 | if (uhp == NULL || uhp->uh_walk != mark |
| 1761 | || (uhp->uh_seq == target && !above)) |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1762 | break; |
| 1763 | curbuf->b_u_curhead = uhp; |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 1764 | u_undoredo(TRUE); |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1765 | uhp->uh_walk = nomark; /* don't go back down here */ |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
| 1768 | /* |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1769 | * And now go down the tree (redo), branching off where needed. |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1770 | */ |
| 1771 | uhp = curbuf->b_u_curhead; |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 1772 | while (uhp != NULL) |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1773 | { |
Bram Moolenaar | 89ed3df | 2007-01-09 19:23:12 +0000 | [diff] [blame] | 1774 | /* Go back to the first branch with a mark. */ |
| 1775 | while (uhp->uh_alt_prev != NULL |
| 1776 | && uhp->uh_alt_prev->uh_walk == mark) |
| 1777 | uhp = uhp->uh_alt_prev; |
| 1778 | |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1779 | /* Find the last branch with a mark, that's the one. */ |
| 1780 | last = uhp; |
| 1781 | while (last->uh_alt_next != NULL |
| 1782 | && last->uh_alt_next->uh_walk == mark) |
| 1783 | last = last->uh_alt_next; |
| 1784 | if (last != uhp) |
| 1785 | { |
| 1786 | /* Make the used branch the first entry in the list of |
| 1787 | * alternatives to make "u" and CTRL-R take this branch. */ |
Bram Moolenaar | 89ed3df | 2007-01-09 19:23:12 +0000 | [diff] [blame] | 1788 | while (uhp->uh_alt_prev != NULL) |
| 1789 | uhp = uhp->uh_alt_prev; |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1790 | if (last->uh_alt_next != NULL) |
| 1791 | last->uh_alt_next->uh_alt_prev = last->uh_alt_prev; |
| 1792 | last->uh_alt_prev->uh_alt_next = last->uh_alt_next; |
| 1793 | last->uh_alt_prev = NULL; |
| 1794 | last->uh_alt_next = uhp; |
| 1795 | uhp->uh_alt_prev = last; |
| 1796 | |
| 1797 | uhp = last; |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 1798 | if (uhp->uh_next != NULL) |
| 1799 | uhp->uh_next->uh_prev = uhp; |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1800 | } |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1801 | curbuf->b_u_curhead = uhp; |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1802 | |
| 1803 | if (uhp->uh_walk != mark) |
| 1804 | break; /* must have reached the target */ |
| 1805 | |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1806 | /* Stop when going backwards in time and didn't find the exact |
| 1807 | * header we were looking for. */ |
| 1808 | if (uhp->uh_seq == target && above) |
Bram Moolenaar | db552d60 | 2006-03-23 22:59:57 +0000 | [diff] [blame] | 1809 | { |
| 1810 | curbuf->b_u_seq_cur = target - 1; |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1811 | break; |
Bram Moolenaar | db552d60 | 2006-03-23 22:59:57 +0000 | [diff] [blame] | 1812 | } |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1813 | |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 1814 | u_undoredo(FALSE); |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1815 | |
| 1816 | /* Advance "curhead" to below the header we last used. If it |
| 1817 | * becomes NULL then we need to set "newhead" to this leaf. */ |
| 1818 | if (uhp->uh_prev == NULL) |
| 1819 | curbuf->b_u_newhead = uhp; |
| 1820 | curbuf->b_u_curhead = uhp->uh_prev; |
Bram Moolenaar | 433f7c8 | 2006-03-21 21:29:36 +0000 | [diff] [blame] | 1821 | did_undo = FALSE; |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1822 | |
| 1823 | if (uhp->uh_seq == target) /* found it! */ |
| 1824 | break; |
| 1825 | |
| 1826 | uhp = uhp->uh_prev; |
| 1827 | if (uhp == NULL || uhp->uh_walk != mark) |
| 1828 | { |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 1829 | /* Need to redo more but can't find it... */ |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1830 | EMSG2(_(e_intern2), "undo_time()"); |
| 1831 | break; |
| 1832 | } |
| 1833 | } |
| 1834 | } |
Bram Moolenaar | db552d60 | 2006-03-23 22:59:57 +0000 | [diff] [blame] | 1835 | u_undo_end(did_undo, absolute); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
| 1838 | /* |
| 1839 | * u_undoredo: common code for undo and redo |
| 1840 | * |
| 1841 | * The lines in the file are replaced by the lines in the entry list at |
| 1842 | * curbuf->b_u_curhead. The replaced lines in the file are saved in the entry |
| 1843 | * list for the next undo/redo. |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 1844 | * |
| 1845 | * 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] | 1846 | */ |
| 1847 | static void |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 1848 | u_undoredo(undo) |
| 1849 | int undo; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1850 | { |
| 1851 | char_u **newarray = NULL; |
| 1852 | linenr_T oldsize; |
| 1853 | linenr_T newsize; |
| 1854 | linenr_T top, bot; |
| 1855 | linenr_T lnum; |
| 1856 | linenr_T newlnum = MAXLNUM; |
| 1857 | long i; |
| 1858 | u_entry_T *uep, *nuep; |
| 1859 | u_entry_T *newlist = NULL; |
| 1860 | int old_flags; |
| 1861 | int new_flags; |
| 1862 | pos_T namedm[NMARKS]; |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1863 | #ifdef FEAT_VISUAL |
| 1864 | visualinfo_T visualinfo; |
| 1865 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1866 | int empty_buffer; /* buffer became empty */ |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1867 | u_header_T *curhead = curbuf->b_u_curhead; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1868 | |
Bram Moolenaar | fecb660 | 2007-10-01 20:54:15 +0000 | [diff] [blame] | 1869 | #ifdef U_DEBUG |
| 1870 | u_check(FALSE); |
| 1871 | #endif |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1872 | old_flags = curhead->uh_flags; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1873 | new_flags = (curbuf->b_changed ? UH_CHANGED : 0) + |
| 1874 | ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0); |
| 1875 | setpcmark(); |
| 1876 | |
| 1877 | /* |
| 1878 | * save marks before undo/redo |
| 1879 | */ |
| 1880 | mch_memmove(namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS); |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1881 | #ifdef FEAT_VISUAL |
| 1882 | visualinfo = curbuf->b_visual; |
| 1883 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1884 | curbuf->b_op_start.lnum = curbuf->b_ml.ml_line_count; |
| 1885 | curbuf->b_op_start.col = 0; |
| 1886 | curbuf->b_op_end.lnum = 0; |
| 1887 | curbuf->b_op_end.col = 0; |
| 1888 | |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1889 | for (uep = curhead->uh_entry; uep != NULL; uep = nuep) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1890 | { |
| 1891 | top = uep->ue_top; |
| 1892 | bot = uep->ue_bot; |
| 1893 | if (bot == 0) |
| 1894 | bot = curbuf->b_ml.ml_line_count + 1; |
| 1895 | if (top > curbuf->b_ml.ml_line_count || top >= bot |
| 1896 | || bot > curbuf->b_ml.ml_line_count + 1) |
| 1897 | { |
| 1898 | EMSG(_("E438: u_undo: line numbers wrong")); |
| 1899 | changed(); /* don't want UNCHANGED now */ |
| 1900 | return; |
| 1901 | } |
| 1902 | |
| 1903 | oldsize = bot - top - 1; /* number of lines before undo */ |
| 1904 | newsize = uep->ue_size; /* number of lines after undo */ |
| 1905 | |
| 1906 | if (top < newlnum) |
| 1907 | { |
| 1908 | /* If the saved cursor is somewhere in this undo block, move it to |
| 1909 | * the remembered position. Makes "gwap" put the cursor back |
| 1910 | * where it was. */ |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1911 | lnum = curhead->uh_cursor.lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1912 | if (lnum >= top && lnum <= top + newsize + 1) |
| 1913 | { |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1914 | curwin->w_cursor = curhead->uh_cursor; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1915 | newlnum = curwin->w_cursor.lnum - 1; |
| 1916 | } |
| 1917 | else |
| 1918 | { |
| 1919 | /* Use the first line that actually changed. Avoids that |
| 1920 | * undoing auto-formatting puts the cursor in the previous |
| 1921 | * line. */ |
| 1922 | for (i = 0; i < newsize && i < oldsize; ++i) |
| 1923 | if (STRCMP(uep->ue_array[i], ml_get(top + 1 + i)) != 0) |
| 1924 | break; |
| 1925 | if (i == newsize && newlnum == MAXLNUM && uep->ue_next == NULL) |
| 1926 | { |
| 1927 | newlnum = top; |
| 1928 | curwin->w_cursor.lnum = newlnum + 1; |
| 1929 | } |
| 1930 | else if (i < newsize) |
| 1931 | { |
| 1932 | newlnum = top + i; |
| 1933 | curwin->w_cursor.lnum = newlnum + 1; |
| 1934 | } |
| 1935 | } |
| 1936 | } |
| 1937 | |
| 1938 | empty_buffer = FALSE; |
| 1939 | |
| 1940 | /* delete the lines between top and bot and save them in newarray */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1941 | if (oldsize > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1942 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1943 | if ((newarray = (char_u **)U_ALLOC_LINE( |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1944 | (unsigned)(sizeof(char_u *) * oldsize))) == NULL) |
| 1945 | { |
| 1946 | do_outofmem_msg((long_u)(sizeof(char_u *) * oldsize)); |
| 1947 | /* |
| 1948 | * We have messed up the entry list, repair is impossible. |
| 1949 | * we have to free the rest of the list. |
| 1950 | */ |
| 1951 | while (uep != NULL) |
| 1952 | { |
| 1953 | nuep = uep->ue_next; |
| 1954 | u_freeentry(uep, uep->ue_size); |
| 1955 | uep = nuep; |
| 1956 | } |
| 1957 | break; |
| 1958 | } |
| 1959 | /* delete backwards, it goes faster in most cases */ |
| 1960 | for (lnum = bot - 1, i = oldsize; --i >= 0; --lnum) |
| 1961 | { |
| 1962 | /* what can we do when we run out of memory? */ |
| 1963 | if ((newarray[i] = u_save_line(lnum)) == NULL) |
| 1964 | do_outofmem_msg((long_u)0); |
| 1965 | /* remember we deleted the last line in the buffer, and a |
| 1966 | * dummy empty line will be inserted */ |
| 1967 | if (curbuf->b_ml.ml_line_count == 1) |
| 1968 | empty_buffer = TRUE; |
| 1969 | ml_delete(lnum, FALSE); |
| 1970 | } |
| 1971 | } |
Bram Moolenaar | 8d34330 | 2005-07-12 22:46:17 +0000 | [diff] [blame] | 1972 | else |
| 1973 | newarray = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1974 | |
| 1975 | /* insert the lines in u_array between top and bot */ |
| 1976 | if (newsize) |
| 1977 | { |
| 1978 | for (lnum = top, i = 0; i < newsize; ++i, ++lnum) |
| 1979 | { |
| 1980 | /* |
| 1981 | * If the file is empty, there is an empty line 1 that we |
| 1982 | * should get rid of, by replacing it with the new line |
| 1983 | */ |
| 1984 | if (empty_buffer && lnum == 0) |
| 1985 | ml_replace((linenr_T)1, uep->ue_array[i], TRUE); |
| 1986 | else |
| 1987 | ml_append(lnum, uep->ue_array[i], (colnr_T)0, FALSE); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1988 | U_FREE_LINE(uep->ue_array[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1989 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1990 | U_FREE_LINE((char_u *)uep->ue_array); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1991 | } |
| 1992 | |
| 1993 | /* adjust marks */ |
| 1994 | if (oldsize != newsize) |
| 1995 | { |
| 1996 | mark_adjust(top + 1, top + oldsize, (long)MAXLNUM, |
| 1997 | (long)newsize - (long)oldsize); |
| 1998 | if (curbuf->b_op_start.lnum > top + oldsize) |
| 1999 | curbuf->b_op_start.lnum += newsize - oldsize; |
| 2000 | if (curbuf->b_op_end.lnum > top + oldsize) |
| 2001 | curbuf->b_op_end.lnum += newsize - oldsize; |
| 2002 | } |
| 2003 | |
| 2004 | changed_lines(top + 1, 0, bot, newsize - oldsize); |
| 2005 | |
| 2006 | /* set '[ and '] mark */ |
| 2007 | if (top + 1 < curbuf->b_op_start.lnum) |
| 2008 | curbuf->b_op_start.lnum = top + 1; |
| 2009 | if (newsize == 0 && top + 1 > curbuf->b_op_end.lnum) |
| 2010 | curbuf->b_op_end.lnum = top + 1; |
| 2011 | else if (top + newsize > curbuf->b_op_end.lnum) |
| 2012 | curbuf->b_op_end.lnum = top + newsize; |
| 2013 | |
| 2014 | u_newcount += newsize; |
| 2015 | u_oldcount += oldsize; |
| 2016 | uep->ue_size = oldsize; |
| 2017 | uep->ue_array = newarray; |
| 2018 | uep->ue_bot = top + newsize + 1; |
| 2019 | |
| 2020 | /* |
| 2021 | * insert this entry in front of the new entry list |
| 2022 | */ |
| 2023 | nuep = uep->ue_next; |
| 2024 | uep->ue_next = newlist; |
| 2025 | newlist = uep; |
| 2026 | } |
| 2027 | |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 2028 | curhead->uh_entry = newlist; |
| 2029 | curhead->uh_flags = new_flags; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2030 | if ((old_flags & UH_EMPTYBUF) && bufempty()) |
| 2031 | curbuf->b_ml.ml_flags |= ML_EMPTY; |
| 2032 | if (old_flags & UH_CHANGED) |
| 2033 | changed(); |
| 2034 | else |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 2035 | #ifdef FEAT_NETBEANS_INTG |
| 2036 | /* per netbeans undo rules, keep it as modified */ |
| 2037 | if (!isNetbeansModified(curbuf)) |
| 2038 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2039 | unchanged(curbuf, FALSE); |
| 2040 | |
| 2041 | /* |
| 2042 | * restore marks from before undo/redo |
| 2043 | */ |
| 2044 | for (i = 0; i < NMARKS; ++i) |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 2045 | if (curhead->uh_namedm[i].lnum != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2046 | { |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 2047 | curbuf->b_namedm[i] = curhead->uh_namedm[i]; |
| 2048 | curhead->uh_namedm[i] = namedm[i]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2049 | } |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 2050 | #ifdef FEAT_VISUAL |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 2051 | if (curhead->uh_visual.vi_start.lnum != 0) |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 2052 | { |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 2053 | curbuf->b_visual = curhead->uh_visual; |
| 2054 | curhead->uh_visual = visualinfo; |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 2055 | } |
| 2056 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2057 | |
| 2058 | /* |
| 2059 | * If the cursor is only off by one line, put it at the same position as |
| 2060 | * before starting the change (for the "o" command). |
| 2061 | * Otherwise the cursor should go to the first undone line. |
| 2062 | */ |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 2063 | if (curhead->uh_cursor.lnum + 1 == curwin->w_cursor.lnum |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2064 | && curwin->w_cursor.lnum > 1) |
| 2065 | --curwin->w_cursor.lnum; |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 2066 | if (curhead->uh_cursor.lnum == curwin->w_cursor.lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2067 | { |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 2068 | curwin->w_cursor.col = curhead->uh_cursor.col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2069 | #ifdef FEAT_VIRTUALEDIT |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 2070 | if (virtual_active() && curhead->uh_cursor_vcol >= 0) |
| 2071 | coladvance((colnr_T)curhead->uh_cursor_vcol); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2072 | else |
| 2073 | curwin->w_cursor.coladd = 0; |
| 2074 | #endif |
| 2075 | } |
| 2076 | else if (curwin->w_cursor.lnum <= curbuf->b_ml.ml_line_count) |
| 2077 | beginline(BL_SOL | BL_FIX); |
| 2078 | else |
| 2079 | { |
| 2080 | /* We get here with the current cursor line being past the end (eg |
| 2081 | * after adding lines at the end of the file, and then undoing it). |
| 2082 | * check_cursor() will move the cursor to the last line. Move it to |
| 2083 | * the first column here. */ |
| 2084 | curwin->w_cursor.col = 0; |
| 2085 | #ifdef FEAT_VIRTUALEDIT |
| 2086 | curwin->w_cursor.coladd = 0; |
| 2087 | #endif |
| 2088 | } |
| 2089 | |
| 2090 | /* Make sure the cursor is on an existing line and column. */ |
| 2091 | check_cursor(); |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 2092 | |
| 2093 | /* Remember where we are for "g-" and ":earlier 10s". */ |
| 2094 | curbuf->b_u_seq_cur = curhead->uh_seq; |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 2095 | if (undo) |
| 2096 | /* We are below the previous undo. However, to make ":earlier 1s" |
| 2097 | * work we compute this as being just above the just undone change. */ |
| 2098 | --curbuf->b_u_seq_cur; |
| 2099 | |
| 2100 | /* The timestamp can be the same for multiple changes, just use the one of |
| 2101 | * the undone/redone change. */ |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 2102 | curbuf->b_u_seq_time = curhead->uh_time; |
Bram Moolenaar | fecb660 | 2007-10-01 20:54:15 +0000 | [diff] [blame] | 2103 | #ifdef U_DEBUG |
| 2104 | u_check(FALSE); |
| 2105 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2106 | } |
| 2107 | |
| 2108 | /* |
| 2109 | * If we deleted or added lines, report the number of less/more lines. |
| 2110 | * Otherwise, report the number of changes (this may be incorrect |
| 2111 | * in some cases, but it's better than nothing). |
| 2112 | */ |
| 2113 | static void |
Bram Moolenaar | db552d60 | 2006-03-23 22:59:57 +0000 | [diff] [blame] | 2114 | u_undo_end(did_undo, absolute) |
Bram Moolenaar | 433f7c8 | 2006-03-21 21:29:36 +0000 | [diff] [blame] | 2115 | int did_undo; /* just did an undo */ |
Bram Moolenaar | db552d60 | 2006-03-23 22:59:57 +0000 | [diff] [blame] | 2116 | int absolute; /* used ":undo N" */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2117 | { |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 2118 | char *msgstr; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 2119 | u_header_T *uhp; |
| 2120 | char_u msgbuf[80]; |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 2121 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2122 | #ifdef FEAT_FOLDING |
| 2123 | if ((fdo_flags & FDO_UNDO) && KeyTyped) |
| 2124 | foldOpenCursor(); |
| 2125 | #endif |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 2126 | |
| 2127 | if (global_busy /* no messages now, wait until global is finished */ |
| 2128 | || !messaging()) /* 'lazyredraw' set, don't do messages now */ |
| 2129 | return; |
| 2130 | |
| 2131 | if (curbuf->b_ml.ml_flags & ML_EMPTY) |
| 2132 | --u_newcount; |
| 2133 | |
| 2134 | u_oldcount -= u_newcount; |
| 2135 | if (u_oldcount == -1) |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 2136 | msgstr = N_("more line"); |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 2137 | else if (u_oldcount < 0) |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 2138 | msgstr = N_("more lines"); |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 2139 | else if (u_oldcount == 1) |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 2140 | msgstr = N_("line less"); |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 2141 | else if (u_oldcount > 1) |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 2142 | msgstr = N_("fewer lines"); |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 2143 | else |
| 2144 | { |
| 2145 | u_oldcount = u_newcount; |
| 2146 | if (u_newcount == 1) |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 2147 | msgstr = N_("change"); |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 2148 | else |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 2149 | msgstr = N_("changes"); |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 2150 | } |
| 2151 | |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 2152 | if (curbuf->b_u_curhead != NULL) |
Bram Moolenaar | 433f7c8 | 2006-03-21 21:29:36 +0000 | [diff] [blame] | 2153 | { |
Bram Moolenaar | db552d60 | 2006-03-23 22:59:57 +0000 | [diff] [blame] | 2154 | /* For ":undo N" we prefer a "after #N" message. */ |
| 2155 | if (absolute && curbuf->b_u_curhead->uh_next != NULL) |
| 2156 | { |
| 2157 | uhp = curbuf->b_u_curhead->uh_next; |
| 2158 | did_undo = FALSE; |
| 2159 | } |
| 2160 | else if (did_undo) |
Bram Moolenaar | 433f7c8 | 2006-03-21 21:29:36 +0000 | [diff] [blame] | 2161 | uhp = curbuf->b_u_curhead; |
| 2162 | else |
| 2163 | uhp = curbuf->b_u_curhead->uh_next; |
| 2164 | } |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 2165 | else |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 2166 | uhp = curbuf->b_u_newhead; |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 2167 | |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 2168 | if (uhp == NULL) |
| 2169 | *msgbuf = NUL; |
| 2170 | else |
| 2171 | u_add_time(msgbuf, sizeof(msgbuf), uhp->uh_time); |
| 2172 | |
Bram Moolenaar | 433f7c8 | 2006-03-21 21:29:36 +0000 | [diff] [blame] | 2173 | smsg((char_u *)_("%ld %s; %s #%ld %s"), |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 2174 | u_oldcount < 0 ? -u_oldcount : u_oldcount, |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 2175 | _(msgstr), |
Bram Moolenaar | 433f7c8 | 2006-03-21 21:29:36 +0000 | [diff] [blame] | 2176 | did_undo ? _("before") : _("after"), |
| 2177 | uhp == NULL ? 0L : uhp->uh_seq, |
| 2178 | msgbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2179 | } |
| 2180 | |
| 2181 | /* |
| 2182 | * u_sync: stop adding to the current entry list |
| 2183 | */ |
| 2184 | void |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 2185 | u_sync(force) |
| 2186 | int force; /* Also sync when no_u_sync is set. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2187 | { |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 2188 | /* Skip it when already synced or syncing is disabled. */ |
| 2189 | if (curbuf->b_u_synced || (!force && no_u_sync > 0)) |
| 2190 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2191 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
| 2192 | if (im_is_preediting()) |
| 2193 | return; /* XIM is busy, don't break an undo sequence */ |
| 2194 | #endif |
| 2195 | if (p_ul < 0) |
| 2196 | curbuf->b_u_synced = TRUE; /* no entries, nothing to do */ |
| 2197 | else |
| 2198 | { |
| 2199 | u_getbot(); /* compute ue_bot of previous u_save */ |
| 2200 | curbuf->b_u_curhead = NULL; |
| 2201 | } |
| 2202 | } |
| 2203 | |
| 2204 | /* |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 2205 | * ":undolist": List the leafs of the undo tree |
| 2206 | */ |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 2207 | void |
| 2208 | ex_undolist(eap) |
Bram Moolenaar | fff2bee | 2010-05-15 13:56:02 +0200 | [diff] [blame] | 2209 | exarg_T *eap UNUSED; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 2210 | { |
| 2211 | garray_T ga; |
| 2212 | u_header_T *uhp; |
| 2213 | int mark; |
| 2214 | int nomark; |
| 2215 | int changes = 1; |
| 2216 | int i; |
| 2217 | |
| 2218 | /* |
| 2219 | * 1: walk the tree to find all leafs, put the info in "ga". |
| 2220 | * 2: sort the lines |
| 2221 | * 3: display the list |
| 2222 | */ |
| 2223 | mark = ++lastmark; |
| 2224 | nomark = ++lastmark; |
| 2225 | ga_init2(&ga, (int)sizeof(char *), 20); |
| 2226 | |
| 2227 | uhp = curbuf->b_u_oldhead; |
| 2228 | while (uhp != NULL) |
| 2229 | { |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 2230 | if (uhp->uh_prev == NULL && uhp->uh_walk != nomark |
| 2231 | && uhp->uh_walk != mark) |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 2232 | { |
| 2233 | if (ga_grow(&ga, 1) == FAIL) |
| 2234 | break; |
| 2235 | vim_snprintf((char *)IObuff, IOSIZE, "%6ld %7ld ", |
| 2236 | uhp->uh_seq, changes); |
| 2237 | u_add_time(IObuff + STRLEN(IObuff), IOSIZE - STRLEN(IObuff), |
| 2238 | uhp->uh_time); |
| 2239 | ((char_u **)(ga.ga_data))[ga.ga_len++] = vim_strsave(IObuff); |
| 2240 | } |
| 2241 | |
| 2242 | uhp->uh_walk = mark; |
| 2243 | |
| 2244 | /* go down in the tree if we haven't been there */ |
| 2245 | if (uhp->uh_prev != NULL && uhp->uh_prev->uh_walk != nomark |
| 2246 | && uhp->uh_prev->uh_walk != mark) |
| 2247 | { |
| 2248 | uhp = uhp->uh_prev; |
| 2249 | ++changes; |
| 2250 | } |
| 2251 | |
| 2252 | /* go to alternate branch if we haven't been there */ |
| 2253 | else if (uhp->uh_alt_next != NULL |
| 2254 | && uhp->uh_alt_next->uh_walk != nomark |
| 2255 | && uhp->uh_alt_next->uh_walk != mark) |
| 2256 | uhp = uhp->uh_alt_next; |
| 2257 | |
| 2258 | /* go up in the tree if we haven't been there and we are at the |
| 2259 | * start of alternate branches */ |
| 2260 | else if (uhp->uh_next != NULL && uhp->uh_alt_prev == NULL |
| 2261 | && uhp->uh_next->uh_walk != nomark |
| 2262 | && uhp->uh_next->uh_walk != mark) |
| 2263 | { |
| 2264 | uhp = uhp->uh_next; |
| 2265 | --changes; |
| 2266 | } |
| 2267 | |
| 2268 | else |
| 2269 | { |
| 2270 | /* need to backtrack; mark this node as done */ |
| 2271 | uhp->uh_walk = nomark; |
| 2272 | if (uhp->uh_alt_prev != NULL) |
| 2273 | uhp = uhp->uh_alt_prev; |
| 2274 | else |
| 2275 | { |
| 2276 | uhp = uhp->uh_next; |
| 2277 | --changes; |
| 2278 | } |
| 2279 | } |
| 2280 | } |
| 2281 | |
| 2282 | if (ga.ga_len == 0) |
| 2283 | MSG(_("Nothing to undo")); |
| 2284 | else |
| 2285 | { |
| 2286 | sort_strings((char_u **)ga.ga_data, ga.ga_len); |
| 2287 | |
| 2288 | msg_start(); |
| 2289 | msg_puts_attr((char_u *)_("number changes time"), hl_attr(HLF_T)); |
| 2290 | for (i = 0; i < ga.ga_len && !got_int; ++i) |
| 2291 | { |
| 2292 | msg_putchar('\n'); |
| 2293 | if (got_int) |
| 2294 | break; |
| 2295 | msg_puts(((char_u **)ga.ga_data)[i]); |
| 2296 | } |
| 2297 | msg_end(); |
| 2298 | |
| 2299 | ga_clear_strings(&ga); |
| 2300 | } |
| 2301 | } |
| 2302 | |
| 2303 | /* |
| 2304 | * Put the timestamp of an undo header in "buf[buflen]" in a nice format. |
| 2305 | */ |
| 2306 | static void |
| 2307 | u_add_time(buf, buflen, tt) |
| 2308 | char_u *buf; |
| 2309 | size_t buflen; |
| 2310 | time_t tt; |
| 2311 | { |
| 2312 | #ifdef HAVE_STRFTIME |
| 2313 | struct tm *curtime; |
| 2314 | |
| 2315 | if (time(NULL) - tt >= 100) |
| 2316 | { |
| 2317 | curtime = localtime(&tt); |
Bram Moolenaar | 3991dab | 2006-03-27 17:01:56 +0000 | [diff] [blame] | 2318 | (void)strftime((char *)buf, buflen, "%H:%M:%S", curtime); |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 2319 | } |
| 2320 | else |
| 2321 | #endif |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 2322 | vim_snprintf((char *)buf, buflen, _("%ld seconds ago"), |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 2323 | (long)(time(NULL) - tt)); |
| 2324 | } |
| 2325 | |
| 2326 | /* |
Bram Moolenaar | e224ffa | 2006-03-01 00:01:28 +0000 | [diff] [blame] | 2327 | * ":undojoin": continue adding to the last entry list |
| 2328 | */ |
Bram Moolenaar | e224ffa | 2006-03-01 00:01:28 +0000 | [diff] [blame] | 2329 | void |
| 2330 | ex_undojoin(eap) |
Bram Moolenaar | fff2bee | 2010-05-15 13:56:02 +0200 | [diff] [blame] | 2331 | exarg_T *eap UNUSED; |
Bram Moolenaar | e224ffa | 2006-03-01 00:01:28 +0000 | [diff] [blame] | 2332 | { |
Bram Moolenaar | e224ffa | 2006-03-01 00:01:28 +0000 | [diff] [blame] | 2333 | if (curbuf->b_u_newhead == NULL) |
| 2334 | return; /* nothing changed before */ |
Bram Moolenaar | 57657d8 | 2006-04-21 22:12:41 +0000 | [diff] [blame] | 2335 | if (curbuf->b_u_curhead != NULL) |
| 2336 | { |
| 2337 | EMSG(_("E790: undojoin is not allowed after undo")); |
| 2338 | return; |
| 2339 | } |
| 2340 | if (!curbuf->b_u_synced) |
| 2341 | return; /* already unsynced */ |
Bram Moolenaar | e224ffa | 2006-03-01 00:01:28 +0000 | [diff] [blame] | 2342 | if (p_ul < 0) |
| 2343 | return; /* no entries, nothing to do */ |
| 2344 | else |
| 2345 | { |
| 2346 | /* Go back to the last entry */ |
| 2347 | curbuf->b_u_curhead = curbuf->b_u_newhead; |
| 2348 | curbuf->b_u_synced = FALSE; /* no entries, nothing to do */ |
| 2349 | } |
| 2350 | } |
| 2351 | |
| 2352 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2353 | * Called after writing the file and setting b_changed to FALSE. |
| 2354 | * Now an undo means that the buffer is modified. |
| 2355 | */ |
| 2356 | void |
| 2357 | u_unchanged(buf) |
| 2358 | buf_T *buf; |
| 2359 | { |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 2360 | u_unch_branch(buf->b_u_oldhead); |
| 2361 | buf->b_did_warn = FALSE; |
| 2362 | } |
| 2363 | |
| 2364 | static void |
| 2365 | u_unch_branch(uhp) |
| 2366 | u_header_T *uhp; |
| 2367 | { |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 2368 | u_header_T *uh; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2369 | |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 2370 | for (uh = uhp; uh != NULL; uh = uh->uh_prev) |
| 2371 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2372 | uh->uh_flags |= UH_CHANGED; |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 2373 | if (uh->uh_alt_next != NULL) |
| 2374 | u_unch_branch(uh->uh_alt_next); /* recursive */ |
| 2375 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2376 | } |
| 2377 | |
| 2378 | /* |
| 2379 | * Get pointer to last added entry. |
| 2380 | * If it's not valid, give an error message and return NULL. |
| 2381 | */ |
| 2382 | static u_entry_T * |
| 2383 | u_get_headentry() |
| 2384 | { |
| 2385 | if (curbuf->b_u_newhead == NULL || curbuf->b_u_newhead->uh_entry == NULL) |
| 2386 | { |
| 2387 | EMSG(_("E439: undo list corrupt")); |
| 2388 | return NULL; |
| 2389 | } |
| 2390 | return curbuf->b_u_newhead->uh_entry; |
| 2391 | } |
| 2392 | |
| 2393 | /* |
| 2394 | * u_getbot(): compute the line number of the previous u_save |
| 2395 | * It is called only when b_u_synced is FALSE. |
| 2396 | */ |
| 2397 | static void |
| 2398 | u_getbot() |
| 2399 | { |
| 2400 | u_entry_T *uep; |
| 2401 | linenr_T extra; |
| 2402 | |
| 2403 | uep = u_get_headentry(); /* check for corrupt undo list */ |
| 2404 | if (uep == NULL) |
| 2405 | return; |
| 2406 | |
| 2407 | uep = curbuf->b_u_newhead->uh_getbot_entry; |
| 2408 | if (uep != NULL) |
| 2409 | { |
| 2410 | /* |
| 2411 | * the new ue_bot is computed from the number of lines that has been |
| 2412 | * inserted (0 - deleted) since calling u_save. This is equal to the |
| 2413 | * old line count subtracted from the current line count. |
| 2414 | */ |
| 2415 | extra = curbuf->b_ml.ml_line_count - uep->ue_lcount; |
| 2416 | uep->ue_bot = uep->ue_top + uep->ue_size + 1 + extra; |
| 2417 | if (uep->ue_bot < 1 || uep->ue_bot > curbuf->b_ml.ml_line_count) |
| 2418 | { |
| 2419 | EMSG(_("E440: undo line missing")); |
| 2420 | uep->ue_bot = uep->ue_top + 1; /* assume all lines deleted, will |
| 2421 | * get all the old lines back |
| 2422 | * without deleting the current |
| 2423 | * ones */ |
| 2424 | } |
| 2425 | |
| 2426 | curbuf->b_u_newhead->uh_getbot_entry = NULL; |
| 2427 | } |
| 2428 | |
| 2429 | curbuf->b_u_synced = TRUE; |
| 2430 | } |
| 2431 | |
| 2432 | /* |
Bram Moolenaar | fecb660 | 2007-10-01 20:54:15 +0000 | [diff] [blame] | 2433 | * Free one header "uhp" and its entry list and adjust the pointers. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2434 | */ |
| 2435 | static void |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 2436 | u_freeheader(buf, uhp, uhpp) |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2437 | buf_T *buf; |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 2438 | u_header_T *uhp; |
| 2439 | u_header_T **uhpp; /* if not NULL reset when freeing this header */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2440 | { |
Bram Moolenaar | fecb660 | 2007-10-01 20:54:15 +0000 | [diff] [blame] | 2441 | u_header_T *uhap; |
| 2442 | |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 2443 | /* When there is an alternate redo list free that branch completely, |
| 2444 | * because we can never go there. */ |
| 2445 | if (uhp->uh_alt_next != NULL) |
| 2446 | u_freebranch(buf, uhp->uh_alt_next, uhpp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2447 | |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 2448 | if (uhp->uh_alt_prev != NULL) |
| 2449 | uhp->uh_alt_prev->uh_alt_next = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2450 | |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 2451 | /* Update the links in the list to remove the header. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2452 | if (uhp->uh_next == NULL) |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2453 | buf->b_u_oldhead = uhp->uh_prev; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2454 | else |
| 2455 | uhp->uh_next->uh_prev = uhp->uh_prev; |
| 2456 | |
| 2457 | if (uhp->uh_prev == NULL) |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2458 | buf->b_u_newhead = uhp->uh_next; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2459 | else |
Bram Moolenaar | fecb660 | 2007-10-01 20:54:15 +0000 | [diff] [blame] | 2460 | for (uhap = uhp->uh_prev; uhap != NULL; uhap = uhap->uh_alt_next) |
| 2461 | uhap->uh_next = uhp->uh_next; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2462 | |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 2463 | u_freeentries(buf, uhp, uhpp); |
| 2464 | } |
| 2465 | |
| 2466 | /* |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 2467 | * Free an alternate branch and any following alternate branches. |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 2468 | */ |
| 2469 | static void |
| 2470 | u_freebranch(buf, uhp, uhpp) |
| 2471 | buf_T *buf; |
| 2472 | u_header_T *uhp; |
| 2473 | u_header_T **uhpp; /* if not NULL reset when freeing this header */ |
| 2474 | { |
| 2475 | u_header_T *tofree, *next; |
| 2476 | |
Bram Moolenaar | 07d0677 | 2007-11-10 21:51:15 +0000 | [diff] [blame] | 2477 | /* If this is the top branch we may need to use u_freeheader() to update |
| 2478 | * all the pointers. */ |
| 2479 | if (uhp == buf->b_u_oldhead) |
| 2480 | { |
| 2481 | u_freeheader(buf, uhp, uhpp); |
| 2482 | return; |
| 2483 | } |
| 2484 | |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 2485 | if (uhp->uh_alt_prev != NULL) |
| 2486 | uhp->uh_alt_prev->uh_alt_next = NULL; |
| 2487 | |
| 2488 | next = uhp; |
| 2489 | while (next != NULL) |
| 2490 | { |
| 2491 | tofree = next; |
| 2492 | if (tofree->uh_alt_next != NULL) |
| 2493 | u_freebranch(buf, tofree->uh_alt_next, uhpp); /* recursive */ |
| 2494 | next = tofree->uh_prev; |
| 2495 | u_freeentries(buf, tofree, uhpp); |
| 2496 | } |
| 2497 | } |
| 2498 | |
| 2499 | /* |
| 2500 | * Free all the undo entries for one header and the header itself. |
| 2501 | * This means that "uhp" is invalid when returning. |
| 2502 | */ |
| 2503 | static void |
| 2504 | u_freeentries(buf, uhp, uhpp) |
| 2505 | buf_T *buf; |
| 2506 | u_header_T *uhp; |
| 2507 | u_header_T **uhpp; /* if not NULL reset when freeing this header */ |
| 2508 | { |
| 2509 | u_entry_T *uep, *nuep; |
| 2510 | |
| 2511 | /* Check for pointers to the header that become invalid now. */ |
| 2512 | if (buf->b_u_curhead == uhp) |
| 2513 | buf->b_u_curhead = NULL; |
Bram Moolenaar | fecb660 | 2007-10-01 20:54:15 +0000 | [diff] [blame] | 2514 | if (buf->b_u_newhead == uhp) |
| 2515 | buf->b_u_newhead = NULL; /* freeing the newest entry */ |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 2516 | if (uhpp != NULL && uhp == *uhpp) |
| 2517 | *uhpp = NULL; |
| 2518 | |
| 2519 | for (uep = uhp->uh_entry; uep != NULL; uep = nuep) |
| 2520 | { |
| 2521 | nuep = uep->ue_next; |
| 2522 | u_freeentry(uep, uep->ue_size); |
| 2523 | } |
| 2524 | |
Bram Moolenaar | fecb660 | 2007-10-01 20:54:15 +0000 | [diff] [blame] | 2525 | #ifdef U_DEBUG |
| 2526 | uhp->uh_magic = 0; |
| 2527 | #endif |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2528 | U_FREE_LINE((char_u *)uhp); |
| 2529 | --buf->b_u_numhead; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2530 | } |
| 2531 | |
| 2532 | /* |
| 2533 | * free entry 'uep' and 'n' lines in uep->ue_array[] |
| 2534 | */ |
| 2535 | static void |
| 2536 | u_freeentry(uep, n) |
| 2537 | u_entry_T *uep; |
| 2538 | long n; |
| 2539 | { |
Bram Moolenaar | 8d34330 | 2005-07-12 22:46:17 +0000 | [diff] [blame] | 2540 | while (n > 0) |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2541 | U_FREE_LINE(uep->ue_array[--n]); |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2542 | U_FREE_LINE((char_u *)uep->ue_array); |
Bram Moolenaar | fecb660 | 2007-10-01 20:54:15 +0000 | [diff] [blame] | 2543 | #ifdef U_DEBUG |
| 2544 | uep->ue_magic = 0; |
| 2545 | #endif |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2546 | U_FREE_LINE((char_u *)uep); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2547 | } |
| 2548 | |
| 2549 | /* |
| 2550 | * invalidate the undo buffer; called when storage has already been released |
| 2551 | */ |
| 2552 | void |
| 2553 | u_clearall(buf) |
| 2554 | buf_T *buf; |
| 2555 | { |
| 2556 | buf->b_u_newhead = buf->b_u_oldhead = buf->b_u_curhead = NULL; |
| 2557 | buf->b_u_synced = TRUE; |
| 2558 | buf->b_u_numhead = 0; |
| 2559 | buf->b_u_line_ptr = NULL; |
| 2560 | buf->b_u_line_lnum = 0; |
| 2561 | } |
| 2562 | |
| 2563 | /* |
| 2564 | * save the line "lnum" for the "U" command |
| 2565 | */ |
| 2566 | void |
| 2567 | u_saveline(lnum) |
| 2568 | linenr_T lnum; |
| 2569 | { |
| 2570 | if (lnum == curbuf->b_u_line_lnum) /* line is already saved */ |
| 2571 | return; |
Bram Moolenaar | 1ec484f | 2005-06-24 23:07:47 +0000 | [diff] [blame] | 2572 | 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] | 2573 | return; |
| 2574 | u_clearline(); |
| 2575 | curbuf->b_u_line_lnum = lnum; |
| 2576 | if (curwin->w_cursor.lnum == lnum) |
| 2577 | curbuf->b_u_line_colnr = curwin->w_cursor.col; |
| 2578 | else |
| 2579 | curbuf->b_u_line_colnr = 0; |
| 2580 | if ((curbuf->b_u_line_ptr = u_save_line(lnum)) == NULL) |
| 2581 | do_outofmem_msg((long_u)0); |
| 2582 | } |
| 2583 | |
| 2584 | /* |
| 2585 | * clear the line saved for the "U" command |
| 2586 | * (this is used externally for crossing a line while in insert mode) |
| 2587 | */ |
| 2588 | void |
| 2589 | u_clearline() |
| 2590 | { |
| 2591 | if (curbuf->b_u_line_ptr != NULL) |
| 2592 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2593 | U_FREE_LINE(curbuf->b_u_line_ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2594 | curbuf->b_u_line_ptr = NULL; |
| 2595 | curbuf->b_u_line_lnum = 0; |
| 2596 | } |
| 2597 | } |
| 2598 | |
| 2599 | /* |
| 2600 | * Implementation of the "U" command. |
| 2601 | * Differentiation from vi: "U" can be undone with the next "U". |
| 2602 | * We also allow the cursor to be in another line. |
| 2603 | */ |
| 2604 | void |
| 2605 | u_undoline() |
| 2606 | { |
| 2607 | colnr_T t; |
| 2608 | char_u *oldp; |
| 2609 | |
| 2610 | if (undo_off) |
| 2611 | return; |
| 2612 | |
Bram Moolenaar | e3300c8 | 2008-02-13 14:21:38 +0000 | [diff] [blame] | 2613 | if (curbuf->b_u_line_ptr == NULL |
| 2614 | || curbuf->b_u_line_lnum > curbuf->b_ml.ml_line_count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2615 | { |
| 2616 | beep_flush(); |
| 2617 | return; |
| 2618 | } |
Bram Moolenaar | e3300c8 | 2008-02-13 14:21:38 +0000 | [diff] [blame] | 2619 | |
| 2620 | /* first save the line for the 'u' command */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2621 | if (u_savecommon(curbuf->b_u_line_lnum - 1, |
| 2622 | curbuf->b_u_line_lnum + 1, (linenr_T)0) == FAIL) |
| 2623 | return; |
| 2624 | oldp = u_save_line(curbuf->b_u_line_lnum); |
| 2625 | if (oldp == NULL) |
| 2626 | { |
| 2627 | do_outofmem_msg((long_u)0); |
| 2628 | return; |
| 2629 | } |
| 2630 | ml_replace(curbuf->b_u_line_lnum, curbuf->b_u_line_ptr, TRUE); |
| 2631 | changed_bytes(curbuf->b_u_line_lnum, 0); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2632 | U_FREE_LINE(curbuf->b_u_line_ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2633 | curbuf->b_u_line_ptr = oldp; |
| 2634 | |
| 2635 | t = curbuf->b_u_line_colnr; |
| 2636 | if (curwin->w_cursor.lnum == curbuf->b_u_line_lnum) |
| 2637 | curbuf->b_u_line_colnr = curwin->w_cursor.col; |
| 2638 | curwin->w_cursor.col = t; |
| 2639 | curwin->w_cursor.lnum = curbuf->b_u_line_lnum; |
Bram Moolenaar | e3300c8 | 2008-02-13 14:21:38 +0000 | [diff] [blame] | 2640 | check_cursor_col(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2641 | } |
| 2642 | |
| 2643 | /* |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2644 | * There are two implementations of the memory management for undo: |
| 2645 | * 1. Use the standard malloc()/free() functions. |
| 2646 | * This should be fast for allocating memory, but when a buffer is |
| 2647 | * abandoned every single allocated chunk must be freed, which may be slow. |
| 2648 | * 2. Allocate larger blocks of memory and keep track of chunks ourselves. |
| 2649 | * This is fast for abandoning, but the use of linked lists is slow for |
| 2650 | * finding a free chunk. Esp. when a lot of lines are changed or deleted. |
| 2651 | * A bit of profiling showed that the first method is faster, especially when |
| 2652 | * making a large number of changes, under the condition that malloc()/free() |
| 2653 | * is implemented efficiently. |
| 2654 | */ |
| 2655 | #ifdef U_USE_MALLOC |
| 2656 | /* |
| 2657 | * Version of undo memory allocation using malloc()/free() |
| 2658 | * |
| 2659 | * U_FREE_LINE() and U_ALLOC_LINE() are macros that invoke vim_free() and |
| 2660 | * lalloc() directly. |
| 2661 | */ |
| 2662 | |
| 2663 | /* |
| 2664 | * Free all allocated memory blocks for the buffer 'buf'. |
| 2665 | */ |
| 2666 | void |
| 2667 | u_blockfree(buf) |
| 2668 | buf_T *buf; |
| 2669 | { |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 2670 | while (buf->b_u_oldhead != NULL) |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 2671 | u_freeheader(buf, buf->b_u_oldhead, NULL); |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2672 | U_FREE_LINE(buf->b_u_line_ptr); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2673 | } |
| 2674 | |
| 2675 | #else |
| 2676 | /* |
| 2677 | * Storage allocation for the undo lines and blocks of the current file. |
| 2678 | * Version where Vim keeps track of the available memory. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2679 | */ |
| 2680 | |
| 2681 | /* |
| 2682 | * Memory is allocated in relatively large blocks. These blocks are linked |
| 2683 | * in the allocated block list, headed by curbuf->b_block_head. They are all |
| 2684 | * freed when abandoning a file, so we don't have to free every single line. |
| 2685 | * The list is kept sorted on memory address. |
| 2686 | * block_alloc() allocates a block. |
| 2687 | * m_blockfree() frees all blocks. |
| 2688 | * |
| 2689 | * The available chunks of memory are kept in free chunk lists. There is |
| 2690 | * one free list for each block of allocated memory. The list is kept sorted |
| 2691 | * on memory address. |
| 2692 | * u_alloc_line() gets a chunk from the free lists. |
| 2693 | * u_free_line() returns a chunk to the free lists. |
| 2694 | * curbuf->b_m_search points to the chunk before the chunk that was |
| 2695 | * freed/allocated the last time. |
| 2696 | * curbuf->b_mb_current points to the b_head where curbuf->b_m_search |
| 2697 | * points into the free list. |
| 2698 | * |
| 2699 | * |
| 2700 | * b_block_head /---> block #1 /---> block #2 |
| 2701 | * mb_next ---/ mb_next ---/ mb_next ---> NULL |
| 2702 | * mb_info mb_info mb_info |
| 2703 | * | | | |
| 2704 | * V V V |
| 2705 | * NULL free chunk #1.1 free chunk #2.1 |
| 2706 | * | | |
| 2707 | * V V |
| 2708 | * free chunk #1.2 NULL |
| 2709 | * | |
| 2710 | * V |
| 2711 | * NULL |
| 2712 | * |
| 2713 | * When a single free chunk list would have been used, it could take a lot |
| 2714 | * of time in u_free_line() to find the correct place to insert a chunk in the |
| 2715 | * free list. The single free list would become very long when many lines are |
| 2716 | * changed (e.g. with :%s/^M$//). |
| 2717 | */ |
| 2718 | |
| 2719 | /* |
| 2720 | * this blocksize is used when allocating new lines |
| 2721 | */ |
| 2722 | #define MEMBLOCKSIZE 2044 |
| 2723 | |
| 2724 | /* |
| 2725 | * The size field contains the size of the chunk, including the size field |
| 2726 | * itself. |
| 2727 | * |
| 2728 | * When the chunk is not in-use it is preceded with the m_info structure. |
| 2729 | * The m_next field links it in one of the free chunk lists. |
| 2730 | * |
| 2731 | * On most unix systems structures have to be longword (32 or 64 bit) aligned. |
| 2732 | * On most other systems they are short (16 bit) aligned. |
| 2733 | */ |
| 2734 | |
| 2735 | /* the structure definitions are now in structs.h */ |
| 2736 | |
| 2737 | #ifdef ALIGN_LONG |
| 2738 | /* size of m_size */ |
| 2739 | # define M_OFFSET (sizeof(long_u)) |
| 2740 | #else |
| 2741 | /* size of m_size */ |
| 2742 | # define M_OFFSET (sizeof(short_u)) |
| 2743 | #endif |
| 2744 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2745 | static char_u *u_blockalloc __ARGS((long_u)); |
| 2746 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2747 | /* |
| 2748 | * Allocate a block of memory and link it in the allocated block list. |
| 2749 | */ |
| 2750 | static char_u * |
| 2751 | u_blockalloc(size) |
| 2752 | long_u size; |
| 2753 | { |
| 2754 | mblock_T *p; |
| 2755 | mblock_T *mp, *next; |
| 2756 | |
| 2757 | p = (mblock_T *)lalloc(size + sizeof(mblock_T), FALSE); |
| 2758 | if (p != NULL) |
| 2759 | { |
| 2760 | /* Insert the block into the allocated block list, keeping it |
| 2761 | sorted on address. */ |
| 2762 | for (mp = &curbuf->b_block_head; |
| 2763 | (next = mp->mb_next) != NULL && next < p; |
| 2764 | mp = next) |
| 2765 | ; |
| 2766 | p->mb_next = next; /* link in block list */ |
| 2767 | p->mb_size = size; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2768 | p->mb_maxsize = 0; /* nothing free yet */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2769 | mp->mb_next = p; |
| 2770 | p->mb_info.m_next = NULL; /* clear free list */ |
| 2771 | p->mb_info.m_size = 0; |
| 2772 | curbuf->b_mb_current = p; /* remember current block */ |
| 2773 | curbuf->b_m_search = NULL; |
| 2774 | p++; /* return usable memory */ |
| 2775 | } |
| 2776 | return (char_u *)p; |
| 2777 | } |
| 2778 | |
| 2779 | /* |
| 2780 | * free all allocated memory blocks for the buffer 'buf' |
| 2781 | */ |
| 2782 | void |
| 2783 | u_blockfree(buf) |
| 2784 | buf_T *buf; |
| 2785 | { |
| 2786 | mblock_T *p, *np; |
| 2787 | |
| 2788 | for (p = buf->b_block_head.mb_next; p != NULL; p = np) |
| 2789 | { |
| 2790 | np = p->mb_next; |
| 2791 | vim_free(p); |
| 2792 | } |
| 2793 | buf->b_block_head.mb_next = NULL; |
| 2794 | buf->b_m_search = NULL; |
| 2795 | buf->b_mb_current = NULL; |
| 2796 | } |
| 2797 | |
| 2798 | /* |
| 2799 | * Free a chunk of memory for the current buffer. |
| 2800 | * Insert the chunk into the correct free list, keeping it sorted on address. |
| 2801 | */ |
| 2802 | static void |
| 2803 | u_free_line(ptr, keep) |
| 2804 | char_u *ptr; |
| 2805 | int keep; /* don't free the block when it's empty */ |
| 2806 | { |
| 2807 | minfo_T *next; |
| 2808 | minfo_T *prev, *curr; |
| 2809 | minfo_T *mp; |
| 2810 | mblock_T *nextb; |
| 2811 | mblock_T *prevb; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2812 | long_u maxsize; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2813 | |
| 2814 | if (ptr == NULL || ptr == IObuff) |
| 2815 | return; /* illegal address can happen in out-of-memory situations */ |
| 2816 | |
| 2817 | mp = (minfo_T *)(ptr - M_OFFSET); |
| 2818 | |
| 2819 | /* find block where chunk could be a part off */ |
| 2820 | /* if we change curbuf->b_mb_current, curbuf->b_m_search is set to NULL */ |
| 2821 | if (curbuf->b_mb_current == NULL || mp < (minfo_T *)curbuf->b_mb_current) |
| 2822 | { |
| 2823 | curbuf->b_mb_current = curbuf->b_block_head.mb_next; |
| 2824 | curbuf->b_m_search = NULL; |
| 2825 | } |
| 2826 | if ((nextb = curbuf->b_mb_current->mb_next) != NULL |
| 2827 | && (minfo_T *)nextb < mp) |
| 2828 | { |
| 2829 | curbuf->b_mb_current = nextb; |
| 2830 | curbuf->b_m_search = NULL; |
| 2831 | } |
| 2832 | while ((nextb = curbuf->b_mb_current->mb_next) != NULL |
| 2833 | && (minfo_T *)nextb < mp) |
| 2834 | curbuf->b_mb_current = nextb; |
| 2835 | |
| 2836 | curr = NULL; |
| 2837 | /* |
| 2838 | * If mp is smaller than curbuf->b_m_search->m_next go to the start of |
| 2839 | * the free list |
| 2840 | */ |
| 2841 | if (curbuf->b_m_search == NULL || mp < (curbuf->b_m_search->m_next)) |
| 2842 | next = &(curbuf->b_mb_current->mb_info); |
| 2843 | else |
| 2844 | next = curbuf->b_m_search; |
| 2845 | /* |
| 2846 | * The following loop is executed very often. |
| 2847 | * Therefore it has been optimized at the cost of readability. |
| 2848 | * Keep it fast! |
| 2849 | */ |
| 2850 | #ifdef SLOW_BUT_EASY_TO_READ |
| 2851 | do |
| 2852 | { |
| 2853 | prev = curr; |
| 2854 | curr = next; |
| 2855 | next = next->m_next; |
| 2856 | } |
| 2857 | while (mp > next && next != NULL); |
| 2858 | #else |
| 2859 | do /* first, middle, last */ |
| 2860 | { |
| 2861 | prev = next->m_next; /* curr, next, prev */ |
| 2862 | if (prev == NULL || mp <= prev) |
| 2863 | { |
| 2864 | prev = curr; |
| 2865 | curr = next; |
| 2866 | next = next->m_next; |
| 2867 | break; |
| 2868 | } |
| 2869 | curr = prev->m_next; /* next, prev, curr */ |
| 2870 | if (curr == NULL || mp <= curr) |
| 2871 | { |
| 2872 | prev = next; |
| 2873 | curr = prev->m_next; |
| 2874 | next = curr->m_next; |
| 2875 | break; |
| 2876 | } |
| 2877 | next = curr->m_next; /* prev, curr, next */ |
| 2878 | } |
| 2879 | while (mp > next && next != NULL); |
| 2880 | #endif |
| 2881 | |
| 2882 | /* if *mp and *next are concatenated, join them into one chunk */ |
| 2883 | if ((char_u *)mp + mp->m_size == (char_u *)next) |
| 2884 | { |
| 2885 | mp->m_size += next->m_size; |
| 2886 | mp->m_next = next->m_next; |
| 2887 | } |
| 2888 | else |
| 2889 | mp->m_next = next; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2890 | maxsize = mp->m_size; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2891 | |
| 2892 | /* if *curr and *mp are concatenated, join them */ |
| 2893 | if (prev != NULL && (char_u *)curr + curr->m_size == (char_u *)mp) |
| 2894 | { |
| 2895 | curr->m_size += mp->m_size; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2896 | maxsize = curr->m_size; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2897 | curr->m_next = mp->m_next; |
| 2898 | curbuf->b_m_search = prev; |
| 2899 | } |
| 2900 | else |
| 2901 | { |
| 2902 | curr->m_next = mp; |
| 2903 | curbuf->b_m_search = curr; /* put curbuf->b_m_search before freed |
| 2904 | chunk */ |
| 2905 | } |
| 2906 | |
| 2907 | /* |
Bram Moolenaar | 035db9f | 2007-05-10 18:02:27 +0000 | [diff] [blame] | 2908 | * If the block only contains free memory now, release it. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2909 | */ |
| 2910 | if (!keep && curbuf->b_mb_current->mb_size |
| 2911 | == curbuf->b_mb_current->mb_info.m_next->m_size) |
| 2912 | { |
| 2913 | /* Find the block before the current one to be able to unlink it from |
| 2914 | * the list of blocks. */ |
| 2915 | prevb = &curbuf->b_block_head; |
| 2916 | for (nextb = prevb->mb_next; nextb != curbuf->b_mb_current; |
| 2917 | nextb = nextb->mb_next) |
| 2918 | prevb = nextb; |
| 2919 | prevb->mb_next = nextb->mb_next; |
| 2920 | vim_free(nextb); |
| 2921 | curbuf->b_mb_current = NULL; |
| 2922 | curbuf->b_m_search = NULL; |
| 2923 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2924 | else if (curbuf->b_mb_current->mb_maxsize < maxsize) |
| 2925 | curbuf->b_mb_current->mb_maxsize = maxsize; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2926 | } |
| 2927 | |
| 2928 | /* |
| 2929 | * Allocate and initialize a new line structure with room for at least |
| 2930 | * 'size' characters plus a terminating NUL. |
| 2931 | */ |
| 2932 | static char_u * |
| 2933 | u_alloc_line(size) |
| 2934 | unsigned size; |
| 2935 | { |
| 2936 | minfo_T *mp, *mprev, *mp2; |
| 2937 | mblock_T *mbp; |
| 2938 | int size_align; |
| 2939 | |
| 2940 | /* |
| 2941 | * Add room for size field and trailing NUL byte. |
| 2942 | * Adjust for minimal size (must be able to store minfo_T |
| 2943 | * plus a trailing NUL, so the chunk can be released again) |
| 2944 | */ |
| 2945 | size += M_OFFSET + 1; |
| 2946 | if (size < sizeof(minfo_T) + 1) |
| 2947 | size = sizeof(minfo_T) + 1; |
| 2948 | |
| 2949 | /* |
| 2950 | * round size up for alignment |
| 2951 | */ |
| 2952 | size_align = (size + ALIGN_MASK) & ~ALIGN_MASK; |
| 2953 | |
| 2954 | /* |
| 2955 | * If curbuf->b_m_search is NULL (uninitialized free list) start at |
| 2956 | * curbuf->b_block_head |
| 2957 | */ |
| 2958 | if (curbuf->b_mb_current == NULL || curbuf->b_m_search == NULL) |
| 2959 | { |
| 2960 | curbuf->b_mb_current = &curbuf->b_block_head; |
| 2961 | curbuf->b_m_search = &(curbuf->b_block_head.mb_info); |
| 2962 | } |
| 2963 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2964 | /* Search for a block with enough space. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2965 | mbp = curbuf->b_mb_current; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2966 | while (mbp->mb_maxsize < size_align) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2967 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2968 | if (mbp->mb_next != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2969 | mbp = mbp->mb_next; |
| 2970 | else |
| 2971 | mbp = &curbuf->b_block_head; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2972 | if (mbp == curbuf->b_mb_current) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2973 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2974 | int n = (size_align > (MEMBLOCKSIZE / 4) |
| 2975 | ? size_align : MEMBLOCKSIZE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2976 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2977 | /* Back where we started in block list: need to add a new block |
| 2978 | * with enough space. */ |
| 2979 | mp = (minfo_T *)u_blockalloc((long_u)n); |
| 2980 | if (mp == NULL) |
| 2981 | return (NULL); |
| 2982 | mp->m_size = n; |
| 2983 | u_free_line((char_u *)mp + M_OFFSET, TRUE); |
| 2984 | mbp = curbuf->b_mb_current; |
| 2985 | break; |
| 2986 | } |
| 2987 | } |
| 2988 | if (mbp != curbuf->b_mb_current) |
| 2989 | curbuf->b_m_search = &(mbp->mb_info); |
| 2990 | |
| 2991 | /* In this block find a chunk with enough space. */ |
| 2992 | mprev = curbuf->b_m_search; |
| 2993 | mp = curbuf->b_m_search->m_next; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 2994 | for (;;) |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2995 | { |
| 2996 | if (mp == NULL) /* at end of the list */ |
| 2997 | mp = &(mbp->mb_info); /* wrap around to begin */ |
| 2998 | if (mp->m_size >= size) |
| 2999 | break; |
| 3000 | if (mp == curbuf->b_m_search) |
| 3001 | { |
| 3002 | /* back where we started in free chunk list: "cannot happen" */ |
| 3003 | EMSG2(_(e_intern2), "u_alloc_line()"); |
| 3004 | return NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3005 | } |
| 3006 | mprev = mp; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 3007 | mp = mp->m_next; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3008 | } |
| 3009 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 3010 | /* when using the largest chunk adjust mb_maxsize */ |
| 3011 | if (mp->m_size >= mbp->mb_maxsize) |
| 3012 | mbp->mb_maxsize = 0; |
| 3013 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3014 | /* if the chunk we found is large enough, split it up in two */ |
| 3015 | if ((long)mp->m_size - size_align >= (long)(sizeof(minfo_T) + 1)) |
| 3016 | { |
| 3017 | mp2 = (minfo_T *)((char_u *)mp + size_align); |
| 3018 | mp2->m_size = mp->m_size - size_align; |
| 3019 | mp2->m_next = mp->m_next; |
| 3020 | mprev->m_next = mp2; |
| 3021 | mp->m_size = size_align; |
| 3022 | } |
| 3023 | else /* remove *mp from the free list */ |
| 3024 | { |
| 3025 | mprev->m_next = mp->m_next; |
| 3026 | } |
| 3027 | curbuf->b_m_search = mprev; |
| 3028 | curbuf->b_mb_current = mbp; |
| 3029 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 3030 | /* If using the largest chunk need to find the new largest chunk */ |
| 3031 | if (mbp->mb_maxsize == 0) |
| 3032 | for (mp2 = &(mbp->mb_info); mp2 != NULL; mp2 = mp2->m_next) |
| 3033 | if (mbp->mb_maxsize < mp2->m_size) |
| 3034 | mbp->mb_maxsize = mp2->m_size; |
| 3035 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3036 | mp = (minfo_T *)((char_u *)mp + M_OFFSET); |
| 3037 | *(char_u *)mp = NUL; /* set the first byte to NUL */ |
| 3038 | |
| 3039 | return ((char_u *)mp); |
| 3040 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 3041 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3042 | |
| 3043 | /* |
| 3044 | * u_save_line(): allocate memory with u_alloc_line() and copy line 'lnum' |
| 3045 | * into it. |
| 3046 | */ |
| 3047 | static char_u * |
| 3048 | u_save_line(lnum) |
| 3049 | linenr_T lnum; |
| 3050 | { |
| 3051 | char_u *src; |
| 3052 | char_u *dst; |
| 3053 | unsigned len; |
| 3054 | |
| 3055 | src = ml_get(lnum); |
| 3056 | len = (unsigned)STRLEN(src); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 3057 | if ((dst = U_ALLOC_LINE(len)) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3058 | mch_memmove(dst, src, (size_t)(len + 1)); |
| 3059 | return (dst); |
| 3060 | } |
| 3061 | |
| 3062 | /* |
| 3063 | * Check if the 'modified' flag is set, or 'ff' has changed (only need to |
| 3064 | * check the first character, because it can only be "dos", "unix" or "mac"). |
| 3065 | * "nofile" and "scratch" type buffers are considered to always be unchanged. |
| 3066 | */ |
| 3067 | int |
| 3068 | bufIsChanged(buf) |
| 3069 | buf_T *buf; |
| 3070 | { |
| 3071 | return |
| 3072 | #ifdef FEAT_QUICKFIX |
| 3073 | !bt_dontwrite(buf) && |
| 3074 | #endif |
| 3075 | (buf->b_changed || file_ff_differs(buf)); |
| 3076 | } |
| 3077 | |
| 3078 | int |
| 3079 | curbufIsChanged() |
| 3080 | { |
| 3081 | return |
| 3082 | #ifdef FEAT_QUICKFIX |
| 3083 | !bt_dontwrite(curbuf) && |
| 3084 | #endif |
| 3085 | (curbuf->b_changed || file_ff_differs(curbuf)); |
| 3086 | } |