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