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