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