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