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