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