Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4: |
| 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * undo.c: multi level undo facility |
| 12 | * |
| 13 | * The saved lines are stored in a list of lists (one for each buffer): |
| 14 | * |
| 15 | * b_u_oldhead------------------------------------------------+ |
| 16 | * | |
| 17 | * V |
| 18 | * +--------------+ +--------------+ +--------------+ |
| 19 | * b_u_newhead--->| u_header | | u_header | | u_header | |
| 20 | * | uh_next------>| uh_next------>| uh_next---->NULL |
| 21 | * NULL<--------uh_prev |<---------uh_prev |<---------uh_prev | |
| 22 | * | uh_entry | | uh_entry | | uh_entry | |
| 23 | * +--------|-----+ +--------|-----+ +--------|-----+ |
| 24 | * | | | |
| 25 | * V V V |
| 26 | * +--------------+ +--------------+ +--------------+ |
| 27 | * | u_entry | | u_entry | | u_entry | |
| 28 | * | ue_next | | ue_next | | ue_next | |
| 29 | * +--------|-----+ +--------|-----+ +--------|-----+ |
| 30 | * | | | |
| 31 | * V V V |
| 32 | * +--------------+ NULL NULL |
| 33 | * | u_entry | |
| 34 | * | ue_next | |
| 35 | * +--------|-----+ |
| 36 | * | |
| 37 | * V |
| 38 | * etc. |
| 39 | * |
| 40 | * Each u_entry list contains the information for one undo or redo. |
| 41 | * curbuf->b_u_curhead points to the header of the last undo (the next redo), |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 42 | * or is NULL if nothing has been undone (end of the branch). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 43 | * |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 44 | * For keeping alternate undo/redo branches the uh_alt field is used. Thus at |
| 45 | * each point in the list a branch may appear for an alternate to redo. The |
| 46 | * uh_seq field is numbered sequentially to be able to find a newer or older |
| 47 | * branch. |
| 48 | * |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 49 | * +---------------+ +---------------+ |
| 50 | * b_u_oldhead --->| u_header | | u_header | |
| 51 | * | uh_alt_next ---->| uh_alt_next ----> NULL |
| 52 | * NULL <----- uh_alt_prev |<------ uh_alt_prev | |
| 53 | * | uh_prev | | uh_prev | |
| 54 | * +-----|---------+ +-----|---------+ |
| 55 | * | | |
| 56 | * V V |
| 57 | * +---------------+ +---------------+ |
| 58 | * | u_header | | u_header | |
| 59 | * | uh_alt_next | | uh_alt_next | |
| 60 | * b_u_newhead --->| uh_alt_prev | | uh_alt_prev | |
| 61 | * | uh_prev | | uh_prev | |
| 62 | * +-----|---------+ +-----|---------+ |
| 63 | * | | |
| 64 | * V V |
| 65 | * NULL +---------------+ +---------------+ |
| 66 | * | u_header | | u_header | |
| 67 | * | uh_alt_next ---->| uh_alt_next | |
| 68 | * | uh_alt_prev |<------ uh_alt_prev | |
| 69 | * | uh_prev | | uh_prev | |
| 70 | * +-----|---------+ +-----|---------+ |
| 71 | * | | |
| 72 | * etc. etc. |
| 73 | * |
| 74 | * |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 75 | * All data is allocated with U_ALLOC_LINE(), it will be freed as soon as the |
| 76 | * buffer is unloaded. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 77 | */ |
| 78 | |
| 79 | #include "vim.h" |
| 80 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 81 | /* See below: use malloc()/free() for memory management. */ |
| 82 | #define U_USE_MALLOC 1 |
| 83 | |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 84 | static void u_unch_branch __ARGS((u_header_T *uhp)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 85 | static u_entry_T *u_get_headentry __ARGS((void)); |
| 86 | static void u_getbot __ARGS((void)); |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 87 | static int undo_allowed __ARGS((void)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 88 | static int u_savecommon __ARGS((linenr_T, linenr_T, linenr_T)); |
| 89 | static void u_doit __ARGS((int count)); |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 90 | static void u_undoredo __ARGS((int undo)); |
Bram Moolenaar | 433f7c8 | 2006-03-21 21:29:36 +0000 | [diff] [blame] | 91 | static void u_undo_end __ARGS((int did_undo)); |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 92 | static void u_add_time __ARGS((char_u *buf, size_t buflen, time_t tt)); |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 93 | static void u_freeheader __ARGS((buf_T *buf, u_header_T *uhp, u_header_T **uhpp)); |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 94 | static void u_freebranch __ARGS((buf_T *buf, u_header_T *uhp, u_header_T **uhpp)); |
| 95 | static void u_freeentries __ARGS((buf_T *buf, u_header_T *uhp, u_header_T **uhpp)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 96 | static void u_freeentry __ARGS((u_entry_T *, long)); |
| 97 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 98 | #ifdef U_USE_MALLOC |
| 99 | # define U_FREE_LINE(ptr) vim_free(ptr) |
| 100 | # define U_ALLOC_LINE(size) lalloc((long_u)((size) + 1), FALSE) |
| 101 | #else |
| 102 | static void u_free_line __ARGS((char_u *ptr, int keep)); |
| 103 | static char_u *u_alloc_line __ARGS((unsigned size)); |
| 104 | # define U_FREE_LINE(ptr) u_free_line((ptr), FALSE) |
| 105 | # define U_ALLOC_LINE(size) u_alloc_line(size) |
| 106 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 107 | static char_u *u_save_line __ARGS((linenr_T)); |
| 108 | |
| 109 | static long u_newcount, u_oldcount; |
| 110 | |
| 111 | /* |
| 112 | * When 'u' flag included in 'cpoptions', we behave like vi. Need to remember |
| 113 | * the action that "u" should do. |
| 114 | */ |
| 115 | static int undo_undoes = FALSE; |
| 116 | |
| 117 | /* |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 118 | * Save the current line for both the "u" and "U" command. |
| 119 | * Returns OK or FAIL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 120 | */ |
| 121 | int |
| 122 | u_save_cursor() |
| 123 | { |
| 124 | return (u_save((linenr_T)(curwin->w_cursor.lnum - 1), |
| 125 | (linenr_T)(curwin->w_cursor.lnum + 1))); |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * Save the lines between "top" and "bot" for both the "u" and "U" command. |
| 130 | * "top" may be 0 and bot may be curbuf->b_ml.ml_line_count + 1. |
| 131 | * Returns FAIL when lines could not be saved, OK otherwise. |
| 132 | */ |
| 133 | int |
| 134 | u_save(top, bot) |
| 135 | linenr_T top, bot; |
| 136 | { |
| 137 | if (undo_off) |
| 138 | return OK; |
| 139 | |
| 140 | if (top > curbuf->b_ml.ml_line_count || |
| 141 | top >= bot || bot > curbuf->b_ml.ml_line_count + 1) |
| 142 | return FALSE; /* rely on caller to do error messages */ |
| 143 | |
| 144 | if (top + 2 == bot) |
| 145 | u_saveline((linenr_T)(top + 1)); |
| 146 | |
| 147 | return (u_savecommon(top, bot, (linenr_T)0)); |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * save the line "lnum" (used by ":s" and "~" command) |
| 152 | * The line is replaced, so the new bottom line is lnum + 1. |
| 153 | */ |
| 154 | int |
| 155 | u_savesub(lnum) |
| 156 | linenr_T lnum; |
| 157 | { |
| 158 | if (undo_off) |
| 159 | return OK; |
| 160 | |
| 161 | return (u_savecommon(lnum - 1, lnum + 1, lnum + 1)); |
| 162 | } |
| 163 | |
| 164 | /* |
| 165 | * a new line is inserted before line "lnum" (used by :s command) |
| 166 | * The line is inserted, so the new bottom line is lnum + 1. |
| 167 | */ |
| 168 | int |
| 169 | u_inssub(lnum) |
| 170 | linenr_T lnum; |
| 171 | { |
| 172 | if (undo_off) |
| 173 | return OK; |
| 174 | |
| 175 | return (u_savecommon(lnum - 1, lnum, lnum + 1)); |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * save the lines "lnum" - "lnum" + nlines (used by delete command) |
| 180 | * The lines are deleted, so the new bottom line is lnum, unless the buffer |
| 181 | * becomes empty. |
| 182 | */ |
| 183 | int |
| 184 | u_savedel(lnum, nlines) |
| 185 | linenr_T lnum; |
| 186 | long nlines; |
| 187 | { |
| 188 | if (undo_off) |
| 189 | return OK; |
| 190 | |
| 191 | return (u_savecommon(lnum - 1, lnum + nlines, |
| 192 | nlines == curbuf->b_ml.ml_line_count ? 2 : lnum)); |
| 193 | } |
| 194 | |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 195 | /* |
| 196 | * Return TRUE when undo is allowed. Otherwise give an error message and |
| 197 | * return FALSE. |
| 198 | */ |
| 199 | static int |
| 200 | undo_allowed() |
| 201 | { |
| 202 | /* Don't allow changes when 'modifiable' is off. */ |
| 203 | if (!curbuf->b_p_ma) |
| 204 | { |
| 205 | EMSG(_(e_modifiable)); |
| 206 | return FALSE; |
| 207 | } |
| 208 | |
| 209 | #ifdef HAVE_SANDBOX |
| 210 | /* In the sandbox it's not allowed to change the text. */ |
| 211 | if (sandbox != 0) |
| 212 | { |
| 213 | EMSG(_(e_sandbox)); |
| 214 | return FALSE; |
| 215 | } |
| 216 | #endif |
| 217 | |
| 218 | /* Don't allow changes in the buffer while editing the cmdline. The |
| 219 | * caller of getcmdline() may get confused. */ |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 220 | if (textlock != 0) |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 221 | { |
| 222 | EMSG(_(e_secure)); |
| 223 | return FALSE; |
| 224 | } |
| 225 | |
| 226 | return TRUE; |
| 227 | } |
| 228 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 229 | static int |
| 230 | u_savecommon(top, bot, newbot) |
| 231 | linenr_T top, bot; |
| 232 | linenr_T newbot; |
| 233 | { |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 234 | linenr_T lnum; |
| 235 | long i; |
| 236 | u_header_T *uhp; |
| 237 | u_header_T *old_curhead; |
| 238 | u_entry_T *uep; |
| 239 | u_entry_T *prev_uep; |
| 240 | long size; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 241 | |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 242 | /* When making changes is not allowed return FAIL. It's a crude way to |
| 243 | * make all change commands fail. */ |
| 244 | if (!undo_allowed()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 245 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 246 | |
| 247 | #ifdef FEAT_NETBEANS_INTG |
| 248 | /* |
| 249 | * Netbeans defines areas that cannot be modified. Bail out here when |
| 250 | * trying to change text in a guarded area. |
| 251 | */ |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 252 | if (usingNetbeans) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 253 | { |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 254 | if (netbeans_is_guarded(top, bot)) |
| 255 | { |
| 256 | EMSG(_(e_guarded)); |
| 257 | return FAIL; |
| 258 | } |
| 259 | if (curbuf->b_p_ro) |
| 260 | { |
| 261 | EMSG(_(e_nbreadonly)); |
| 262 | return FAIL; |
| 263 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 264 | } |
| 265 | #endif |
| 266 | |
| 267 | #ifdef FEAT_AUTOCMD |
| 268 | /* |
| 269 | * Saving text for undo means we are going to make a change. Give a |
| 270 | * warning for a read-only file before making the change, so that the |
| 271 | * FileChangedRO event can replace the buffer with a read-write version |
| 272 | * (e.g., obtained from a source control system). |
| 273 | */ |
| 274 | change_warning(0); |
| 275 | #endif |
| 276 | |
| 277 | size = bot - top - 1; |
| 278 | |
| 279 | /* |
| 280 | * if curbuf->b_u_synced == TRUE make a new header |
| 281 | */ |
| 282 | if (curbuf->b_u_synced) |
| 283 | { |
| 284 | #ifdef FEAT_JUMPLIST |
| 285 | /* Need to create new entry in b_changelist. */ |
| 286 | curbuf->b_new_change = TRUE; |
| 287 | #endif |
| 288 | |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 289 | if (p_ul >= 0) |
| 290 | { |
| 291 | /* |
| 292 | * Make a new header entry. Do this first so that we don't mess |
| 293 | * up the undo info when out of memory. |
| 294 | */ |
| 295 | uhp = (u_header_T *)U_ALLOC_LINE((unsigned)sizeof(u_header_T)); |
| 296 | if (uhp == NULL) |
| 297 | goto nomem; |
| 298 | } |
Bram Moolenaar | 7d47b6e | 2006-03-15 22:59:18 +0000 | [diff] [blame] | 299 | else |
| 300 | uhp = NULL; |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 301 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 302 | /* |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 303 | * If we undid more than we redid, move the entry lists before and |
| 304 | * including curbuf->b_u_curhead to an alternate branch. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 305 | */ |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 306 | old_curhead = curbuf->b_u_curhead; |
| 307 | if (old_curhead != NULL) |
| 308 | { |
| 309 | curbuf->b_u_newhead = old_curhead->uh_next; |
| 310 | curbuf->b_u_curhead = NULL; |
| 311 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 312 | |
| 313 | /* |
| 314 | * free headers to keep the size right |
| 315 | */ |
| 316 | while (curbuf->b_u_numhead > p_ul && curbuf->b_u_oldhead != NULL) |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 317 | { |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 318 | u_header_T *uhfree = curbuf->b_u_oldhead; |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 319 | |
| 320 | /* If there is no branch only free one header. */ |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 321 | if (uhfree->uh_alt_next == NULL) |
| 322 | u_freeheader(curbuf, uhfree, &old_curhead); |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 323 | else |
| 324 | { |
| 325 | /* Free the oldest alternate branch as a whole. */ |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 326 | while (uhfree->uh_alt_next != NULL) |
| 327 | uhfree = uhfree->uh_alt_next; |
| 328 | u_freebranch(curbuf, uhfree, &old_curhead); |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 329 | } |
| 330 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 331 | |
Bram Moolenaar | 7d47b6e | 2006-03-15 22:59:18 +0000 | [diff] [blame] | 332 | if (uhp == NULL) /* no undo at all */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 333 | { |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 334 | if (old_curhead != NULL) |
| 335 | u_freebranch(curbuf, old_curhead, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 336 | curbuf->b_u_synced = FALSE; |
| 337 | return OK; |
| 338 | } |
| 339 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 340 | uhp->uh_prev = NULL; |
| 341 | uhp->uh_next = curbuf->b_u_newhead; |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 342 | uhp->uh_alt_next = old_curhead; |
| 343 | if (old_curhead != NULL) |
| 344 | { |
| 345 | old_curhead->uh_alt_prev = uhp; |
| 346 | if (curbuf->b_u_oldhead == old_curhead) |
| 347 | curbuf->b_u_oldhead = uhp; |
| 348 | } |
| 349 | uhp->uh_alt_prev = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 350 | if (curbuf->b_u_newhead != NULL) |
| 351 | curbuf->b_u_newhead->uh_prev = uhp; |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 352 | |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 353 | uhp->uh_seq = ++curbuf->b_u_seq_last; |
| 354 | curbuf->b_u_seq_cur = uhp->uh_seq; |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 355 | uhp->uh_time = time(NULL); |
| 356 | curbuf->b_u_seq_time = uhp->uh_time + 1; |
| 357 | |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 358 | uhp->uh_walk = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 359 | uhp->uh_entry = NULL; |
| 360 | uhp->uh_getbot_entry = NULL; |
| 361 | uhp->uh_cursor = curwin->w_cursor; /* save cursor pos. for undo */ |
| 362 | #ifdef FEAT_VIRTUALEDIT |
| 363 | if (virtual_active() && curwin->w_cursor.coladd > 0) |
| 364 | uhp->uh_cursor_vcol = getviscol(); |
| 365 | else |
| 366 | uhp->uh_cursor_vcol = -1; |
| 367 | #endif |
| 368 | |
| 369 | /* save changed and buffer empty flag for undo */ |
| 370 | uhp->uh_flags = (curbuf->b_changed ? UH_CHANGED : 0) + |
| 371 | ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0); |
| 372 | |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 373 | /* save named marks and Visual marks for undo */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 374 | mch_memmove(uhp->uh_namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS); |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 375 | #ifdef FEAT_VISUAL |
| 376 | uhp->uh_visual = curbuf->b_visual; |
| 377 | #endif |
| 378 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 379 | curbuf->b_u_newhead = uhp; |
| 380 | if (curbuf->b_u_oldhead == NULL) |
| 381 | curbuf->b_u_oldhead = uhp; |
| 382 | ++curbuf->b_u_numhead; |
| 383 | } |
| 384 | else |
| 385 | { |
| 386 | if (p_ul < 0) /* no undo at all */ |
| 387 | return OK; |
| 388 | |
| 389 | /* |
| 390 | * When saving a single line, and it has been saved just before, it |
| 391 | * doesn't make sense saving it again. Saves a lot of memory when |
| 392 | * making lots of changes inside the same line. |
| 393 | * This is only possible if the previous change didn't increase or |
| 394 | * decrease the number of lines. |
| 395 | * Check the ten last changes. More doesn't make sense and takes too |
| 396 | * long. |
| 397 | */ |
| 398 | if (size == 1) |
| 399 | { |
| 400 | uep = u_get_headentry(); |
| 401 | prev_uep = NULL; |
| 402 | for (i = 0; i < 10; ++i) |
| 403 | { |
| 404 | if (uep == NULL) |
| 405 | break; |
| 406 | |
| 407 | /* If lines have been inserted/deleted we give up. |
| 408 | * Also when the line was included in a multi-line save. */ |
| 409 | if ((curbuf->b_u_newhead->uh_getbot_entry != uep |
| 410 | ? (uep->ue_top + uep->ue_size + 1 |
| 411 | != (uep->ue_bot == 0 |
| 412 | ? curbuf->b_ml.ml_line_count + 1 |
| 413 | : uep->ue_bot)) |
| 414 | : uep->ue_lcount != curbuf->b_ml.ml_line_count) |
| 415 | || (uep->ue_size > 1 |
| 416 | && top >= uep->ue_top |
| 417 | && top + 2 <= uep->ue_top + uep->ue_size + 1)) |
| 418 | break; |
| 419 | |
| 420 | /* If it's the same line we can skip saving it again. */ |
| 421 | if (uep->ue_size == 1 && uep->ue_top == top) |
| 422 | { |
| 423 | if (i > 0) |
| 424 | { |
| 425 | /* It's not the last entry: get ue_bot for the last |
| 426 | * entry now. Following deleted/inserted lines go to |
| 427 | * the re-used entry. */ |
| 428 | u_getbot(); |
| 429 | curbuf->b_u_synced = FALSE; |
| 430 | |
| 431 | /* Move the found entry to become the last entry. The |
| 432 | * order of undo/redo doesn't matter for the entries |
| 433 | * we move it over, since they don't change the line |
| 434 | * count and don't include this line. It does matter |
| 435 | * for the found entry if the line count is changed by |
| 436 | * the executed command. */ |
| 437 | prev_uep->ue_next = uep->ue_next; |
| 438 | uep->ue_next = curbuf->b_u_newhead->uh_entry; |
| 439 | curbuf->b_u_newhead->uh_entry = uep; |
| 440 | } |
| 441 | |
| 442 | /* The executed command may change the line count. */ |
| 443 | if (newbot != 0) |
| 444 | uep->ue_bot = newbot; |
| 445 | else if (bot > curbuf->b_ml.ml_line_count) |
| 446 | uep->ue_bot = 0; |
| 447 | else |
| 448 | { |
| 449 | uep->ue_lcount = curbuf->b_ml.ml_line_count; |
| 450 | curbuf->b_u_newhead->uh_getbot_entry = uep; |
| 451 | } |
| 452 | return OK; |
| 453 | } |
| 454 | prev_uep = uep; |
| 455 | uep = uep->ue_next; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | /* find line number for ue_bot for previous u_save() */ |
| 460 | u_getbot(); |
| 461 | } |
| 462 | |
| 463 | #if !defined(UNIX) && !defined(DJGPP) && !defined(WIN32) && !defined(__EMX__) |
| 464 | /* |
| 465 | * With Amiga and MSDOS 16 bit we can't handle big undo's, because |
| 466 | * then u_alloc_line would have to allocate a block larger than 32K |
| 467 | */ |
| 468 | if (size >= 8000) |
| 469 | goto nomem; |
| 470 | #endif |
| 471 | |
| 472 | /* |
| 473 | * add lines in front of entry list |
| 474 | */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 475 | uep = (u_entry_T *)U_ALLOC_LINE((unsigned)sizeof(u_entry_T)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 476 | if (uep == NULL) |
| 477 | goto nomem; |
| 478 | |
| 479 | uep->ue_size = size; |
| 480 | uep->ue_top = top; |
| 481 | if (newbot != 0) |
| 482 | uep->ue_bot = newbot; |
| 483 | /* |
| 484 | * Use 0 for ue_bot if bot is below last line. |
| 485 | * Otherwise we have to compute ue_bot later. |
| 486 | */ |
| 487 | else if (bot > curbuf->b_ml.ml_line_count) |
| 488 | uep->ue_bot = 0; |
| 489 | else |
| 490 | { |
| 491 | uep->ue_lcount = curbuf->b_ml.ml_line_count; |
| 492 | curbuf->b_u_newhead->uh_getbot_entry = uep; |
| 493 | } |
| 494 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 495 | if (size > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 496 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 497 | if ((uep->ue_array = (char_u **)U_ALLOC_LINE( |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 498 | (unsigned)(sizeof(char_u *) * size))) == NULL) |
| 499 | { |
| 500 | u_freeentry(uep, 0L); |
| 501 | goto nomem; |
| 502 | } |
| 503 | for (i = 0, lnum = top + 1; i < size; ++i) |
| 504 | { |
Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 505 | fast_breakcheck(); |
| 506 | if (got_int) |
| 507 | { |
| 508 | u_freeentry(uep, i); |
| 509 | return FAIL; |
| 510 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 511 | if ((uep->ue_array[i] = u_save_line(lnum++)) == NULL) |
| 512 | { |
| 513 | u_freeentry(uep, i); |
| 514 | goto nomem; |
| 515 | } |
| 516 | } |
| 517 | } |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 518 | else |
| 519 | uep->ue_array = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 520 | uep->ue_next = curbuf->b_u_newhead->uh_entry; |
| 521 | curbuf->b_u_newhead->uh_entry = uep; |
| 522 | curbuf->b_u_synced = FALSE; |
| 523 | undo_undoes = FALSE; |
| 524 | |
| 525 | return OK; |
| 526 | |
| 527 | nomem: |
| 528 | msg_silent = 0; /* must display the prompt */ |
| 529 | if (ask_yesno((char_u *)_("No undo possible; continue anyway"), TRUE) |
| 530 | == 'y') |
| 531 | { |
| 532 | undo_off = TRUE; /* will be reset when character typed */ |
| 533 | return OK; |
| 534 | } |
| 535 | do_outofmem_msg((long_u)0); |
| 536 | return FAIL; |
| 537 | } |
| 538 | |
| 539 | /* |
| 540 | * If 'cpoptions' contains 'u': Undo the previous undo or redo (vi compatible). |
| 541 | * If 'cpoptions' does not contain 'u': Always undo. |
| 542 | */ |
| 543 | void |
| 544 | u_undo(count) |
| 545 | int count; |
| 546 | { |
| 547 | /* |
| 548 | * If we get an undo command while executing a macro, we behave like the |
| 549 | * original vi. If this happens twice in one macro the result will not |
| 550 | * be compatible. |
| 551 | */ |
| 552 | if (curbuf->b_u_synced == FALSE) |
| 553 | { |
| 554 | u_sync(); |
| 555 | count = 1; |
| 556 | } |
| 557 | |
| 558 | if (vim_strchr(p_cpo, CPO_UNDO) == NULL) |
| 559 | undo_undoes = TRUE; |
| 560 | else |
| 561 | undo_undoes = !undo_undoes; |
| 562 | u_doit(count); |
| 563 | } |
| 564 | |
| 565 | /* |
| 566 | * If 'cpoptions' contains 'u': Repeat the previous undo or redo. |
| 567 | * If 'cpoptions' does not contain 'u': Always redo. |
| 568 | */ |
| 569 | void |
| 570 | u_redo(count) |
| 571 | int count; |
| 572 | { |
| 573 | if (vim_strchr(p_cpo, CPO_UNDO) == NULL) |
| 574 | undo_undoes = FALSE; |
| 575 | u_doit(count); |
| 576 | } |
| 577 | |
| 578 | /* |
| 579 | * Undo or redo, depending on 'undo_undoes', 'count' times. |
| 580 | */ |
| 581 | static void |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 582 | u_doit(startcount) |
| 583 | int startcount; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 584 | { |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 585 | int count = startcount; |
| 586 | |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 587 | if (!undo_allowed()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 588 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 589 | |
| 590 | u_newcount = 0; |
| 591 | u_oldcount = 0; |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 592 | if (curbuf->b_ml.ml_flags & ML_EMPTY) |
| 593 | u_oldcount = -1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 594 | while (count--) |
| 595 | { |
| 596 | if (undo_undoes) |
| 597 | { |
| 598 | if (curbuf->b_u_curhead == NULL) /* first undo */ |
| 599 | curbuf->b_u_curhead = curbuf->b_u_newhead; |
| 600 | else if (p_ul > 0) /* multi level undo */ |
| 601 | /* get next undo */ |
| 602 | curbuf->b_u_curhead = curbuf->b_u_curhead->uh_next; |
| 603 | /* nothing to undo */ |
| 604 | if (curbuf->b_u_numhead == 0 || curbuf->b_u_curhead == NULL) |
| 605 | { |
| 606 | /* stick curbuf->b_u_curhead at end */ |
| 607 | curbuf->b_u_curhead = curbuf->b_u_oldhead; |
| 608 | beep_flush(); |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 609 | if (count == startcount - 1) |
| 610 | { |
| 611 | MSG(_("Already at oldest change")); |
| 612 | return; |
| 613 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 614 | break; |
| 615 | } |
| 616 | |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 617 | u_undoredo(TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 618 | } |
| 619 | else |
| 620 | { |
| 621 | if (curbuf->b_u_curhead == NULL || p_ul <= 0) |
| 622 | { |
| 623 | beep_flush(); /* nothing to redo */ |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 624 | if (count == startcount - 1) |
| 625 | { |
| 626 | MSG(_("Already at newest change")); |
| 627 | return; |
| 628 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 629 | break; |
| 630 | } |
| 631 | |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 632 | u_undoredo(FALSE); |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 633 | |
| 634 | /* Advance for next redo. Set "newhead" when at the end of the |
| 635 | * redoable changes. */ |
| 636 | if (curbuf->b_u_curhead->uh_prev == NULL) |
| 637 | curbuf->b_u_newhead = curbuf->b_u_curhead; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 638 | curbuf->b_u_curhead = curbuf->b_u_curhead->uh_prev; |
| 639 | } |
| 640 | } |
Bram Moolenaar | 433f7c8 | 2006-03-21 21:29:36 +0000 | [diff] [blame] | 641 | u_undo_end(undo_undoes); |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | static int lastmark = 0; |
| 645 | |
| 646 | /* |
| 647 | * Undo or redo over the timeline. |
| 648 | * 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] | 649 | * When "sec" is FALSE make "step" steps, when "sec" is TRUE use "step" as |
| 650 | * seconds. |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 651 | * When "absolute" is TRUE use "step" as the sequence number to jump to. |
| 652 | * "sec" must be FALSE then. |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 653 | */ |
| 654 | void |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 655 | undo_time(step, sec, absolute) |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 656 | long step; |
| 657 | int sec; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 658 | int absolute; |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 659 | { |
| 660 | long target; |
| 661 | long closest; |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 662 | long closest_start; |
| 663 | long closest_seq = 0; |
| 664 | long val; |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 665 | u_header_T *uhp; |
| 666 | u_header_T *last; |
| 667 | int mark; |
| 668 | int nomark; |
| 669 | int round; |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 670 | int dosec = sec; |
| 671 | int above = FALSE; |
Bram Moolenaar | 433f7c8 | 2006-03-21 21:29:36 +0000 | [diff] [blame] | 672 | int did_undo = TRUE; |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 673 | |
Bram Moolenaar | 7d47b6e | 2006-03-15 22:59:18 +0000 | [diff] [blame] | 674 | /* First make sure the current undoable change is synced. */ |
| 675 | if (curbuf->b_u_synced == FALSE) |
| 676 | u_sync(); |
| 677 | |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 678 | u_newcount = 0; |
| 679 | u_oldcount = 0; |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 680 | if (curbuf->b_ml.ml_flags & ML_EMPTY) |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 681 | u_oldcount = -1; |
| 682 | |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 683 | /* "target" is the node below which we want to be. |
| 684 | * Init "closest" to a value we can't reach. */ |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 685 | if (absolute) |
| 686 | { |
| 687 | target = step; |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 688 | closest = -1; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 689 | } |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 690 | else |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 691 | { |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 692 | /* When doing computations with time_t subtract starttime, because |
| 693 | * time_t converted to a long may result in a wrong number. */ |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 694 | if (sec) |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 695 | target = (long)(curbuf->b_u_seq_time - starttime) + step; |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 696 | else |
| 697 | target = curbuf->b_u_seq_cur + step; |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 698 | if (step < 0) |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 699 | { |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 700 | if (target < 0) |
| 701 | target = 0; |
| 702 | closest = -1; |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 703 | } |
| 704 | else |
| 705 | { |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 706 | if (sec) |
| 707 | closest = time(NULL) - starttime + 1; |
| 708 | else |
| 709 | closest = curbuf->b_u_seq_last + 2; |
| 710 | if (target >= closest) |
| 711 | target = closest - 1; |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 712 | } |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 713 | } |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 714 | closest_start = closest; |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 715 | closest_seq = curbuf->b_u_seq_cur; |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 716 | |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 717 | /* |
| 718 | * May do this twice: |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 719 | * 1. Search for "target", update "closest" to the best match found. |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 720 | * 2. If "target" not found search for "closest". |
| 721 | * |
| 722 | * When using the closest time we use the sequence number in the second |
| 723 | * round, because there may be several entries with the same time. |
| 724 | */ |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 725 | for (round = 1; round <= 2; ++round) |
| 726 | { |
| 727 | /* Find the path from the current state to where we want to go. The |
| 728 | * desired state can be anywhere in the undo tree, need to go all over |
| 729 | * it. We put "nomark" in uh_walk where we have been without success, |
| 730 | * "mark" where it could possibly be. */ |
| 731 | mark = ++lastmark; |
| 732 | nomark = ++lastmark; |
| 733 | |
| 734 | if (curbuf->b_u_curhead == NULL) /* at leaf of the tree */ |
| 735 | uhp = curbuf->b_u_newhead; |
| 736 | else |
| 737 | uhp = curbuf->b_u_curhead; |
| 738 | |
| 739 | while (uhp != NULL) |
| 740 | { |
| 741 | uhp->uh_walk = mark; |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 742 | val = (dosec ? (uhp->uh_time - starttime) : uhp->uh_seq); |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 743 | |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 744 | if (round == 1) |
| 745 | { |
| 746 | /* Remember the header that is closest to the target. |
| 747 | * It must be at least in the right direction (checked with |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 748 | * "b_u_seq_cur"). When the timestamp is equal find the |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 749 | * highest/lowest sequence number. */ |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 750 | if ((step < 0 ? uhp->uh_seq <= curbuf->b_u_seq_cur |
| 751 | : uhp->uh_seq > curbuf->b_u_seq_cur) |
| 752 | && ((dosec && val == closest) |
| 753 | ? (step < 0 |
| 754 | ? uhp->uh_seq < closest_seq |
| 755 | : uhp->uh_seq > closest_seq) |
| 756 | : closest == closest_start |
| 757 | || (val > target |
| 758 | ? (closest > target |
| 759 | ? val - target <= closest - target |
| 760 | : val - target <= target - closest) |
| 761 | : (closest > target |
| 762 | ? target - val <= closest - target |
| 763 | : target - val <= target - closest)))) |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 764 | { |
| 765 | closest = val; |
| 766 | closest_seq = uhp->uh_seq; |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | /* Quit searching when we found a match. But when searching for a |
| 771 | * time we need to continue looking for the best uh_seq. */ |
| 772 | if (target == val && !dosec) |
| 773 | break; |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 774 | |
| 775 | /* go down in the tree if we haven't been there */ |
| 776 | if (uhp->uh_prev != NULL && uhp->uh_prev->uh_walk != nomark |
| 777 | && uhp->uh_prev->uh_walk != mark) |
| 778 | uhp = uhp->uh_prev; |
| 779 | |
| 780 | /* go to alternate branch if we haven't been there */ |
| 781 | else if (uhp->uh_alt_next != NULL |
| 782 | && uhp->uh_alt_next->uh_walk != nomark |
| 783 | && uhp->uh_alt_next->uh_walk != mark) |
| 784 | uhp = uhp->uh_alt_next; |
| 785 | |
| 786 | /* go up in the tree if we haven't been there and we are at the |
| 787 | * start of alternate branches */ |
| 788 | else if (uhp->uh_next != NULL && uhp->uh_alt_prev == NULL |
| 789 | && uhp->uh_next->uh_walk != nomark |
| 790 | && uhp->uh_next->uh_walk != mark) |
| 791 | uhp = uhp->uh_next; |
| 792 | |
| 793 | else |
| 794 | { |
| 795 | /* need to backtrack; mark this node as useless */ |
| 796 | uhp->uh_walk = nomark; |
| 797 | if (uhp->uh_alt_prev != NULL) |
| 798 | uhp = uhp->uh_alt_prev; |
| 799 | else |
| 800 | uhp = uhp->uh_next; |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | if (uhp != NULL) /* found it */ |
| 805 | break; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 806 | |
| 807 | if (absolute) |
| 808 | { |
| 809 | EMSGN(_("Undo number %ld not found"), step); |
| 810 | return; |
| 811 | } |
| 812 | |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 813 | if (closest == closest_start) |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 814 | { |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 815 | if (step < 0) |
| 816 | MSG(_("Already at oldest change")); |
| 817 | else |
| 818 | MSG(_("Already at newest change")); |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 819 | return; |
| 820 | } |
| 821 | |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 822 | target = closest_seq; |
| 823 | dosec = FALSE; |
| 824 | if (step < 0) |
| 825 | above = TRUE; /* stop above the header */ |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | /* If we found it: Follow the path to go to where we want to be. */ |
| 829 | if (uhp != NULL) |
| 830 | { |
| 831 | /* |
| 832 | * First go up the tree as much as needed. |
| 833 | */ |
| 834 | for (;;) |
| 835 | { |
| 836 | uhp = curbuf->b_u_curhead; |
| 837 | if (uhp == NULL) |
| 838 | uhp = curbuf->b_u_newhead; |
| 839 | else |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 840 | uhp = uhp->uh_next; |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 841 | if (uhp == NULL || uhp->uh_walk != mark |
| 842 | || (uhp->uh_seq == target && !above)) |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 843 | break; |
| 844 | curbuf->b_u_curhead = uhp; |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 845 | u_undoredo(TRUE); |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 846 | uhp->uh_walk = nomark; /* don't go back down here */ |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | /* |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 850 | * And now go down the tree (redo), branching off where needed. |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 851 | */ |
| 852 | uhp = curbuf->b_u_curhead; |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 853 | while (uhp != NULL) |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 854 | { |
| 855 | /* Find the last branch with a mark, that's the one. */ |
| 856 | last = uhp; |
| 857 | while (last->uh_alt_next != NULL |
| 858 | && last->uh_alt_next->uh_walk == mark) |
| 859 | last = last->uh_alt_next; |
| 860 | if (last != uhp) |
| 861 | { |
| 862 | /* Make the used branch the first entry in the list of |
| 863 | * alternatives to make "u" and CTRL-R take this branch. */ |
| 864 | if (last->uh_alt_next != NULL) |
| 865 | last->uh_alt_next->uh_alt_prev = last->uh_alt_prev; |
| 866 | last->uh_alt_prev->uh_alt_next = last->uh_alt_next; |
| 867 | last->uh_alt_prev = NULL; |
| 868 | last->uh_alt_next = uhp; |
| 869 | uhp->uh_alt_prev = last; |
| 870 | |
| 871 | uhp = last; |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 872 | if (uhp->uh_next != NULL) |
| 873 | uhp->uh_next->uh_prev = uhp; |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 874 | } |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 875 | curbuf->b_u_curhead = uhp; |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 876 | |
| 877 | if (uhp->uh_walk != mark) |
| 878 | break; /* must have reached the target */ |
| 879 | |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 880 | /* Stop when going backwards in time and didn't find the exact |
| 881 | * header we were looking for. */ |
| 882 | if (uhp->uh_seq == target && above) |
| 883 | break; |
| 884 | |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 885 | u_undoredo(FALSE); |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 886 | |
| 887 | /* Advance "curhead" to below the header we last used. If it |
| 888 | * becomes NULL then we need to set "newhead" to this leaf. */ |
| 889 | if (uhp->uh_prev == NULL) |
| 890 | curbuf->b_u_newhead = uhp; |
| 891 | curbuf->b_u_curhead = uhp->uh_prev; |
Bram Moolenaar | 433f7c8 | 2006-03-21 21:29:36 +0000 | [diff] [blame] | 892 | did_undo = FALSE; |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 893 | |
| 894 | if (uhp->uh_seq == target) /* found it! */ |
| 895 | break; |
| 896 | |
| 897 | uhp = uhp->uh_prev; |
| 898 | if (uhp == NULL || uhp->uh_walk != mark) |
| 899 | { |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 900 | /* Need to redo more but can't find it... */ |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 901 | EMSG2(_(e_intern2), "undo_time()"); |
| 902 | break; |
| 903 | } |
| 904 | } |
| 905 | } |
Bram Moolenaar | 433f7c8 | 2006-03-21 21:29:36 +0000 | [diff] [blame] | 906 | u_undo_end(did_undo); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | /* |
| 910 | * u_undoredo: common code for undo and redo |
| 911 | * |
| 912 | * The lines in the file are replaced by the lines in the entry list at |
| 913 | * curbuf->b_u_curhead. The replaced lines in the file are saved in the entry |
| 914 | * list for the next undo/redo. |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 915 | * |
| 916 | * 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] | 917 | */ |
| 918 | static void |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 919 | u_undoredo(undo) |
| 920 | int undo; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 921 | { |
| 922 | char_u **newarray = NULL; |
| 923 | linenr_T oldsize; |
| 924 | linenr_T newsize; |
| 925 | linenr_T top, bot; |
| 926 | linenr_T lnum; |
| 927 | linenr_T newlnum = MAXLNUM; |
| 928 | long i; |
| 929 | u_entry_T *uep, *nuep; |
| 930 | u_entry_T *newlist = NULL; |
| 931 | int old_flags; |
| 932 | int new_flags; |
| 933 | pos_T namedm[NMARKS]; |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 934 | #ifdef FEAT_VISUAL |
| 935 | visualinfo_T visualinfo; |
| 936 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 937 | int empty_buffer; /* buffer became empty */ |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 938 | u_header_T *curhead = curbuf->b_u_curhead; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 939 | |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 940 | old_flags = curhead->uh_flags; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 941 | new_flags = (curbuf->b_changed ? UH_CHANGED : 0) + |
| 942 | ((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0); |
| 943 | setpcmark(); |
| 944 | |
| 945 | /* |
| 946 | * save marks before undo/redo |
| 947 | */ |
| 948 | mch_memmove(namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS); |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 949 | #ifdef FEAT_VISUAL |
| 950 | visualinfo = curbuf->b_visual; |
| 951 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 952 | curbuf->b_op_start.lnum = curbuf->b_ml.ml_line_count; |
| 953 | curbuf->b_op_start.col = 0; |
| 954 | curbuf->b_op_end.lnum = 0; |
| 955 | curbuf->b_op_end.col = 0; |
| 956 | |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 957 | for (uep = curhead->uh_entry; uep != NULL; uep = nuep) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 958 | { |
| 959 | top = uep->ue_top; |
| 960 | bot = uep->ue_bot; |
| 961 | if (bot == 0) |
| 962 | bot = curbuf->b_ml.ml_line_count + 1; |
| 963 | if (top > curbuf->b_ml.ml_line_count || top >= bot |
| 964 | || bot > curbuf->b_ml.ml_line_count + 1) |
| 965 | { |
| 966 | EMSG(_("E438: u_undo: line numbers wrong")); |
| 967 | changed(); /* don't want UNCHANGED now */ |
| 968 | return; |
| 969 | } |
| 970 | |
| 971 | oldsize = bot - top - 1; /* number of lines before undo */ |
| 972 | newsize = uep->ue_size; /* number of lines after undo */ |
| 973 | |
| 974 | if (top < newlnum) |
| 975 | { |
| 976 | /* If the saved cursor is somewhere in this undo block, move it to |
| 977 | * the remembered position. Makes "gwap" put the cursor back |
| 978 | * where it was. */ |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 979 | lnum = curhead->uh_cursor.lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 980 | if (lnum >= top && lnum <= top + newsize + 1) |
| 981 | { |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 982 | curwin->w_cursor = curhead->uh_cursor; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 983 | newlnum = curwin->w_cursor.lnum - 1; |
| 984 | } |
| 985 | else |
| 986 | { |
| 987 | /* Use the first line that actually changed. Avoids that |
| 988 | * undoing auto-formatting puts the cursor in the previous |
| 989 | * line. */ |
| 990 | for (i = 0; i < newsize && i < oldsize; ++i) |
| 991 | if (STRCMP(uep->ue_array[i], ml_get(top + 1 + i)) != 0) |
| 992 | break; |
| 993 | if (i == newsize && newlnum == MAXLNUM && uep->ue_next == NULL) |
| 994 | { |
| 995 | newlnum = top; |
| 996 | curwin->w_cursor.lnum = newlnum + 1; |
| 997 | } |
| 998 | else if (i < newsize) |
| 999 | { |
| 1000 | newlnum = top + i; |
| 1001 | curwin->w_cursor.lnum = newlnum + 1; |
| 1002 | } |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | empty_buffer = FALSE; |
| 1007 | |
| 1008 | /* delete the lines between top and bot and save them in newarray */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1009 | if (oldsize > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1010 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1011 | if ((newarray = (char_u **)U_ALLOC_LINE( |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1012 | (unsigned)(sizeof(char_u *) * oldsize))) == NULL) |
| 1013 | { |
| 1014 | do_outofmem_msg((long_u)(sizeof(char_u *) * oldsize)); |
| 1015 | /* |
| 1016 | * We have messed up the entry list, repair is impossible. |
| 1017 | * we have to free the rest of the list. |
| 1018 | */ |
| 1019 | while (uep != NULL) |
| 1020 | { |
| 1021 | nuep = uep->ue_next; |
| 1022 | u_freeentry(uep, uep->ue_size); |
| 1023 | uep = nuep; |
| 1024 | } |
| 1025 | break; |
| 1026 | } |
| 1027 | /* delete backwards, it goes faster in most cases */ |
| 1028 | for (lnum = bot - 1, i = oldsize; --i >= 0; --lnum) |
| 1029 | { |
| 1030 | /* what can we do when we run out of memory? */ |
| 1031 | if ((newarray[i] = u_save_line(lnum)) == NULL) |
| 1032 | do_outofmem_msg((long_u)0); |
| 1033 | /* remember we deleted the last line in the buffer, and a |
| 1034 | * dummy empty line will be inserted */ |
| 1035 | if (curbuf->b_ml.ml_line_count == 1) |
| 1036 | empty_buffer = TRUE; |
| 1037 | ml_delete(lnum, FALSE); |
| 1038 | } |
| 1039 | } |
Bram Moolenaar | 8d34330 | 2005-07-12 22:46:17 +0000 | [diff] [blame] | 1040 | else |
| 1041 | newarray = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1042 | |
| 1043 | /* insert the lines in u_array between top and bot */ |
| 1044 | if (newsize) |
| 1045 | { |
| 1046 | for (lnum = top, i = 0; i < newsize; ++i, ++lnum) |
| 1047 | { |
| 1048 | /* |
| 1049 | * If the file is empty, there is an empty line 1 that we |
| 1050 | * should get rid of, by replacing it with the new line |
| 1051 | */ |
| 1052 | if (empty_buffer && lnum == 0) |
| 1053 | ml_replace((linenr_T)1, uep->ue_array[i], TRUE); |
| 1054 | else |
| 1055 | ml_append(lnum, uep->ue_array[i], (colnr_T)0, FALSE); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1056 | U_FREE_LINE(uep->ue_array[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1057 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1058 | U_FREE_LINE((char_u *)uep->ue_array); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | /* adjust marks */ |
| 1062 | if (oldsize != newsize) |
| 1063 | { |
| 1064 | mark_adjust(top + 1, top + oldsize, (long)MAXLNUM, |
| 1065 | (long)newsize - (long)oldsize); |
| 1066 | if (curbuf->b_op_start.lnum > top + oldsize) |
| 1067 | curbuf->b_op_start.lnum += newsize - oldsize; |
| 1068 | if (curbuf->b_op_end.lnum > top + oldsize) |
| 1069 | curbuf->b_op_end.lnum += newsize - oldsize; |
| 1070 | } |
| 1071 | |
| 1072 | changed_lines(top + 1, 0, bot, newsize - oldsize); |
| 1073 | |
| 1074 | /* set '[ and '] mark */ |
| 1075 | if (top + 1 < curbuf->b_op_start.lnum) |
| 1076 | curbuf->b_op_start.lnum = top + 1; |
| 1077 | if (newsize == 0 && top + 1 > curbuf->b_op_end.lnum) |
| 1078 | curbuf->b_op_end.lnum = top + 1; |
| 1079 | else if (top + newsize > curbuf->b_op_end.lnum) |
| 1080 | curbuf->b_op_end.lnum = top + newsize; |
| 1081 | |
| 1082 | u_newcount += newsize; |
| 1083 | u_oldcount += oldsize; |
| 1084 | uep->ue_size = oldsize; |
| 1085 | uep->ue_array = newarray; |
| 1086 | uep->ue_bot = top + newsize + 1; |
| 1087 | |
| 1088 | /* |
| 1089 | * insert this entry in front of the new entry list |
| 1090 | */ |
| 1091 | nuep = uep->ue_next; |
| 1092 | uep->ue_next = newlist; |
| 1093 | newlist = uep; |
| 1094 | } |
| 1095 | |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1096 | curhead->uh_entry = newlist; |
| 1097 | curhead->uh_flags = new_flags; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1098 | if ((old_flags & UH_EMPTYBUF) && bufempty()) |
| 1099 | curbuf->b_ml.ml_flags |= ML_EMPTY; |
| 1100 | if (old_flags & UH_CHANGED) |
| 1101 | changed(); |
| 1102 | else |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 1103 | #ifdef FEAT_NETBEANS_INTG |
| 1104 | /* per netbeans undo rules, keep it as modified */ |
| 1105 | if (!isNetbeansModified(curbuf)) |
| 1106 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1107 | unchanged(curbuf, FALSE); |
| 1108 | |
| 1109 | /* |
| 1110 | * restore marks from before undo/redo |
| 1111 | */ |
| 1112 | for (i = 0; i < NMARKS; ++i) |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1113 | if (curhead->uh_namedm[i].lnum != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1114 | { |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1115 | curbuf->b_namedm[i] = curhead->uh_namedm[i]; |
| 1116 | curhead->uh_namedm[i] = namedm[i]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1117 | } |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1118 | #ifdef FEAT_VISUAL |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1119 | if (curhead->uh_visual.vi_start.lnum != 0) |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1120 | { |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1121 | curbuf->b_visual = curhead->uh_visual; |
| 1122 | curhead->uh_visual = visualinfo; |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1123 | } |
| 1124 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1125 | |
| 1126 | /* |
| 1127 | * If the cursor is only off by one line, put it at the same position as |
| 1128 | * before starting the change (for the "o" command). |
| 1129 | * Otherwise the cursor should go to the first undone line. |
| 1130 | */ |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1131 | if (curhead->uh_cursor.lnum + 1 == curwin->w_cursor.lnum |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1132 | && curwin->w_cursor.lnum > 1) |
| 1133 | --curwin->w_cursor.lnum; |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1134 | if (curhead->uh_cursor.lnum == curwin->w_cursor.lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1135 | { |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1136 | curwin->w_cursor.col = curhead->uh_cursor.col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1137 | #ifdef FEAT_VIRTUALEDIT |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1138 | if (virtual_active() && curhead->uh_cursor_vcol >= 0) |
| 1139 | coladvance((colnr_T)curhead->uh_cursor_vcol); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1140 | else |
| 1141 | curwin->w_cursor.coladd = 0; |
| 1142 | #endif |
| 1143 | } |
| 1144 | else if (curwin->w_cursor.lnum <= curbuf->b_ml.ml_line_count) |
| 1145 | beginline(BL_SOL | BL_FIX); |
| 1146 | else |
| 1147 | { |
| 1148 | /* We get here with the current cursor line being past the end (eg |
| 1149 | * after adding lines at the end of the file, and then undoing it). |
| 1150 | * check_cursor() will move the cursor to the last line. Move it to |
| 1151 | * the first column here. */ |
| 1152 | curwin->w_cursor.col = 0; |
| 1153 | #ifdef FEAT_VIRTUALEDIT |
| 1154 | curwin->w_cursor.coladd = 0; |
| 1155 | #endif |
| 1156 | } |
| 1157 | |
| 1158 | /* Make sure the cursor is on an existing line and column. */ |
| 1159 | check_cursor(); |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1160 | |
| 1161 | /* Remember where we are for "g-" and ":earlier 10s". */ |
| 1162 | curbuf->b_u_seq_cur = curhead->uh_seq; |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 1163 | if (undo) |
| 1164 | /* We are below the previous undo. However, to make ":earlier 1s" |
| 1165 | * work we compute this as being just above the just undone change. */ |
| 1166 | --curbuf->b_u_seq_cur; |
| 1167 | |
| 1168 | /* The timestamp can be the same for multiple changes, just use the one of |
| 1169 | * the undone/redone change. */ |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1170 | curbuf->b_u_seq_time = curhead->uh_time; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | /* |
| 1174 | * If we deleted or added lines, report the number of less/more lines. |
| 1175 | * Otherwise, report the number of changes (this may be incorrect |
| 1176 | * in some cases, but it's better than nothing). |
| 1177 | */ |
| 1178 | static void |
Bram Moolenaar | 433f7c8 | 2006-03-21 21:29:36 +0000 | [diff] [blame] | 1179 | u_undo_end(did_undo) |
| 1180 | int did_undo; /* just did an undo */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1181 | { |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1182 | char *msg; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1183 | u_header_T *uhp; |
| 1184 | char_u msgbuf[80]; |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1185 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1186 | #ifdef FEAT_FOLDING |
| 1187 | if ((fdo_flags & FDO_UNDO) && KeyTyped) |
| 1188 | foldOpenCursor(); |
| 1189 | #endif |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1190 | |
| 1191 | if (global_busy /* no messages now, wait until global is finished */ |
| 1192 | || !messaging()) /* 'lazyredraw' set, don't do messages now */ |
| 1193 | return; |
| 1194 | |
| 1195 | if (curbuf->b_ml.ml_flags & ML_EMPTY) |
| 1196 | --u_newcount; |
| 1197 | |
| 1198 | u_oldcount -= u_newcount; |
| 1199 | if (u_oldcount == -1) |
| 1200 | msg = N_("more line"); |
| 1201 | else if (u_oldcount < 0) |
| 1202 | msg = N_("more lines"); |
| 1203 | else if (u_oldcount == 1) |
| 1204 | msg = N_("line less"); |
| 1205 | else if (u_oldcount > 1) |
| 1206 | msg = N_("fewer lines"); |
| 1207 | else |
| 1208 | { |
| 1209 | u_oldcount = u_newcount; |
| 1210 | if (u_newcount == 1) |
| 1211 | msg = N_("change"); |
| 1212 | else |
| 1213 | msg = N_("changes"); |
| 1214 | } |
| 1215 | |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1216 | if (curbuf->b_u_curhead != NULL) |
Bram Moolenaar | 433f7c8 | 2006-03-21 21:29:36 +0000 | [diff] [blame] | 1217 | { |
| 1218 | if (did_undo) |
| 1219 | uhp = curbuf->b_u_curhead; |
| 1220 | else |
| 1221 | uhp = curbuf->b_u_curhead->uh_next; |
| 1222 | } |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1223 | else |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1224 | uhp = curbuf->b_u_newhead; |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1225 | |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1226 | if (uhp == NULL) |
| 1227 | *msgbuf = NUL; |
| 1228 | else |
| 1229 | u_add_time(msgbuf, sizeof(msgbuf), uhp->uh_time); |
| 1230 | |
Bram Moolenaar | 433f7c8 | 2006-03-21 21:29:36 +0000 | [diff] [blame] | 1231 | smsg((char_u *)_("%ld %s; %s #%ld %s"), |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 1232 | u_oldcount < 0 ? -u_oldcount : u_oldcount, |
Bram Moolenaar | 433f7c8 | 2006-03-21 21:29:36 +0000 | [diff] [blame] | 1233 | _(msg), |
| 1234 | did_undo ? _("before") : _("after"), |
| 1235 | uhp == NULL ? 0L : uhp->uh_seq, |
| 1236 | msgbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1237 | } |
| 1238 | |
| 1239 | /* |
| 1240 | * u_sync: stop adding to the current entry list |
| 1241 | */ |
| 1242 | void |
| 1243 | u_sync() |
| 1244 | { |
| 1245 | if (curbuf->b_u_synced) |
| 1246 | return; /* already synced */ |
| 1247 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
| 1248 | if (im_is_preediting()) |
| 1249 | return; /* XIM is busy, don't break an undo sequence */ |
| 1250 | #endif |
| 1251 | if (p_ul < 0) |
| 1252 | curbuf->b_u_synced = TRUE; /* no entries, nothing to do */ |
| 1253 | else |
| 1254 | { |
| 1255 | u_getbot(); /* compute ue_bot of previous u_save */ |
| 1256 | curbuf->b_u_curhead = NULL; |
| 1257 | } |
| 1258 | } |
| 1259 | |
| 1260 | /* |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1261 | * ":undolist": List the leafs of the undo tree |
| 1262 | */ |
| 1263 | /*ARGSUSED*/ |
| 1264 | void |
| 1265 | ex_undolist(eap) |
| 1266 | exarg_T *eap; |
| 1267 | { |
| 1268 | garray_T ga; |
| 1269 | u_header_T *uhp; |
| 1270 | int mark; |
| 1271 | int nomark; |
| 1272 | int changes = 1; |
| 1273 | int i; |
| 1274 | |
| 1275 | /* |
| 1276 | * 1: walk the tree to find all leafs, put the info in "ga". |
| 1277 | * 2: sort the lines |
| 1278 | * 3: display the list |
| 1279 | */ |
| 1280 | mark = ++lastmark; |
| 1281 | nomark = ++lastmark; |
| 1282 | ga_init2(&ga, (int)sizeof(char *), 20); |
| 1283 | |
| 1284 | uhp = curbuf->b_u_oldhead; |
| 1285 | while (uhp != NULL) |
| 1286 | { |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 1287 | if (uhp->uh_prev == NULL && uhp->uh_walk != nomark |
| 1288 | && uhp->uh_walk != mark) |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1289 | { |
| 1290 | if (ga_grow(&ga, 1) == FAIL) |
| 1291 | break; |
| 1292 | vim_snprintf((char *)IObuff, IOSIZE, "%6ld %7ld ", |
| 1293 | uhp->uh_seq, changes); |
| 1294 | u_add_time(IObuff + STRLEN(IObuff), IOSIZE - STRLEN(IObuff), |
| 1295 | uhp->uh_time); |
| 1296 | ((char_u **)(ga.ga_data))[ga.ga_len++] = vim_strsave(IObuff); |
| 1297 | } |
| 1298 | |
| 1299 | uhp->uh_walk = mark; |
| 1300 | |
| 1301 | /* go down in the tree if we haven't been there */ |
| 1302 | if (uhp->uh_prev != NULL && uhp->uh_prev->uh_walk != nomark |
| 1303 | && uhp->uh_prev->uh_walk != mark) |
| 1304 | { |
| 1305 | uhp = uhp->uh_prev; |
| 1306 | ++changes; |
| 1307 | } |
| 1308 | |
| 1309 | /* go to alternate branch if we haven't been there */ |
| 1310 | else if (uhp->uh_alt_next != NULL |
| 1311 | && uhp->uh_alt_next->uh_walk != nomark |
| 1312 | && uhp->uh_alt_next->uh_walk != mark) |
| 1313 | uhp = uhp->uh_alt_next; |
| 1314 | |
| 1315 | /* go up in the tree if we haven't been there and we are at the |
| 1316 | * start of alternate branches */ |
| 1317 | else if (uhp->uh_next != NULL && uhp->uh_alt_prev == NULL |
| 1318 | && uhp->uh_next->uh_walk != nomark |
| 1319 | && uhp->uh_next->uh_walk != mark) |
| 1320 | { |
| 1321 | uhp = uhp->uh_next; |
| 1322 | --changes; |
| 1323 | } |
| 1324 | |
| 1325 | else |
| 1326 | { |
| 1327 | /* need to backtrack; mark this node as done */ |
| 1328 | uhp->uh_walk = nomark; |
| 1329 | if (uhp->uh_alt_prev != NULL) |
| 1330 | uhp = uhp->uh_alt_prev; |
| 1331 | else |
| 1332 | { |
| 1333 | uhp = uhp->uh_next; |
| 1334 | --changes; |
| 1335 | } |
| 1336 | } |
| 1337 | } |
| 1338 | |
| 1339 | if (ga.ga_len == 0) |
| 1340 | MSG(_("Nothing to undo")); |
| 1341 | else |
| 1342 | { |
| 1343 | sort_strings((char_u **)ga.ga_data, ga.ga_len); |
| 1344 | |
| 1345 | msg_start(); |
| 1346 | msg_puts_attr((char_u *)_("number changes time"), hl_attr(HLF_T)); |
| 1347 | for (i = 0; i < ga.ga_len && !got_int; ++i) |
| 1348 | { |
| 1349 | msg_putchar('\n'); |
| 1350 | if (got_int) |
| 1351 | break; |
| 1352 | msg_puts(((char_u **)ga.ga_data)[i]); |
| 1353 | } |
| 1354 | msg_end(); |
| 1355 | |
| 1356 | ga_clear_strings(&ga); |
| 1357 | } |
| 1358 | } |
| 1359 | |
| 1360 | /* |
| 1361 | * Put the timestamp of an undo header in "buf[buflen]" in a nice format. |
| 1362 | */ |
| 1363 | static void |
| 1364 | u_add_time(buf, buflen, tt) |
| 1365 | char_u *buf; |
| 1366 | size_t buflen; |
| 1367 | time_t tt; |
| 1368 | { |
| 1369 | #ifdef HAVE_STRFTIME |
| 1370 | struct tm *curtime; |
| 1371 | |
| 1372 | if (time(NULL) - tt >= 100) |
| 1373 | { |
| 1374 | curtime = localtime(&tt); |
| 1375 | (void)strftime((char *)buf, buflen, "%T", curtime); |
| 1376 | } |
| 1377 | else |
| 1378 | #endif |
| 1379 | vim_snprintf((char *)buf, buflen, "%ld seconds ago", |
| 1380 | (long)(time(NULL) - tt)); |
| 1381 | } |
| 1382 | |
| 1383 | /* |
Bram Moolenaar | e224ffa | 2006-03-01 00:01:28 +0000 | [diff] [blame] | 1384 | * ":undojoin": continue adding to the last entry list |
| 1385 | */ |
| 1386 | /*ARGSUSED*/ |
| 1387 | void |
| 1388 | ex_undojoin(eap) |
| 1389 | exarg_T *eap; |
| 1390 | { |
| 1391 | if (!curbuf->b_u_synced) |
| 1392 | return; /* already unsynced */ |
| 1393 | if (curbuf->b_u_newhead == NULL) |
| 1394 | return; /* nothing changed before */ |
| 1395 | if (p_ul < 0) |
| 1396 | return; /* no entries, nothing to do */ |
| 1397 | else |
| 1398 | { |
| 1399 | /* Go back to the last entry */ |
| 1400 | curbuf->b_u_curhead = curbuf->b_u_newhead; |
| 1401 | curbuf->b_u_synced = FALSE; /* no entries, nothing to do */ |
| 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1406 | * Called after writing the file and setting b_changed to FALSE. |
| 1407 | * Now an undo means that the buffer is modified. |
| 1408 | */ |
| 1409 | void |
| 1410 | u_unchanged(buf) |
| 1411 | buf_T *buf; |
| 1412 | { |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1413 | u_unch_branch(buf->b_u_oldhead); |
| 1414 | buf->b_did_warn = FALSE; |
| 1415 | } |
| 1416 | |
| 1417 | static void |
| 1418 | u_unch_branch(uhp) |
| 1419 | u_header_T *uhp; |
| 1420 | { |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1421 | u_header_T *uh; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1422 | |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1423 | for (uh = uhp; uh != NULL; uh = uh->uh_prev) |
| 1424 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1425 | uh->uh_flags |= UH_CHANGED; |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1426 | if (uh->uh_alt_next != NULL) |
| 1427 | u_unch_branch(uh->uh_alt_next); /* recursive */ |
| 1428 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
| 1431 | /* |
| 1432 | * Get pointer to last added entry. |
| 1433 | * If it's not valid, give an error message and return NULL. |
| 1434 | */ |
| 1435 | static u_entry_T * |
| 1436 | u_get_headentry() |
| 1437 | { |
| 1438 | if (curbuf->b_u_newhead == NULL || curbuf->b_u_newhead->uh_entry == NULL) |
| 1439 | { |
| 1440 | EMSG(_("E439: undo list corrupt")); |
| 1441 | return NULL; |
| 1442 | } |
| 1443 | return curbuf->b_u_newhead->uh_entry; |
| 1444 | } |
| 1445 | |
| 1446 | /* |
| 1447 | * u_getbot(): compute the line number of the previous u_save |
| 1448 | * It is called only when b_u_synced is FALSE. |
| 1449 | */ |
| 1450 | static void |
| 1451 | u_getbot() |
| 1452 | { |
| 1453 | u_entry_T *uep; |
| 1454 | linenr_T extra; |
| 1455 | |
| 1456 | uep = u_get_headentry(); /* check for corrupt undo list */ |
| 1457 | if (uep == NULL) |
| 1458 | return; |
| 1459 | |
| 1460 | uep = curbuf->b_u_newhead->uh_getbot_entry; |
| 1461 | if (uep != NULL) |
| 1462 | { |
| 1463 | /* |
| 1464 | * the new ue_bot is computed from the number of lines that has been |
| 1465 | * inserted (0 - deleted) since calling u_save. This is equal to the |
| 1466 | * old line count subtracted from the current line count. |
| 1467 | */ |
| 1468 | extra = curbuf->b_ml.ml_line_count - uep->ue_lcount; |
| 1469 | uep->ue_bot = uep->ue_top + uep->ue_size + 1 + extra; |
| 1470 | if (uep->ue_bot < 1 || uep->ue_bot > curbuf->b_ml.ml_line_count) |
| 1471 | { |
| 1472 | EMSG(_("E440: undo line missing")); |
| 1473 | uep->ue_bot = uep->ue_top + 1; /* assume all lines deleted, will |
| 1474 | * get all the old lines back |
| 1475 | * without deleting the current |
| 1476 | * ones */ |
| 1477 | } |
| 1478 | |
| 1479 | curbuf->b_u_newhead->uh_getbot_entry = NULL; |
| 1480 | } |
| 1481 | |
| 1482 | curbuf->b_u_synced = TRUE; |
| 1483 | } |
| 1484 | |
| 1485 | /* |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1486 | * Free one header and its entry list and adjust the pointers. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1487 | */ |
| 1488 | static void |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1489 | u_freeheader(buf, uhp, uhpp) |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1490 | buf_T *buf; |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1491 | u_header_T *uhp; |
| 1492 | u_header_T **uhpp; /* if not NULL reset when freeing this header */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1493 | { |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1494 | /* When there is an alternate redo list free that branch completely, |
| 1495 | * because we can never go there. */ |
| 1496 | if (uhp->uh_alt_next != NULL) |
| 1497 | u_freebranch(buf, uhp->uh_alt_next, uhpp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1498 | |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1499 | if (uhp->uh_alt_prev != NULL) |
| 1500 | uhp->uh_alt_prev->uh_alt_next = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1501 | |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1502 | /* Update the links in the list to remove the header. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1503 | if (uhp->uh_next == NULL) |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1504 | buf->b_u_oldhead = uhp->uh_prev; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1505 | else |
| 1506 | uhp->uh_next->uh_prev = uhp->uh_prev; |
| 1507 | |
| 1508 | if (uhp->uh_prev == NULL) |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1509 | buf->b_u_newhead = uhp->uh_next; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1510 | else |
| 1511 | uhp->uh_prev->uh_next = uhp->uh_next; |
| 1512 | |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1513 | u_freeentries(buf, uhp, uhpp); |
| 1514 | } |
| 1515 | |
| 1516 | /* |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1517 | * Free an alternate branch and any following alternate branches. |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1518 | */ |
| 1519 | static void |
| 1520 | u_freebranch(buf, uhp, uhpp) |
| 1521 | buf_T *buf; |
| 1522 | u_header_T *uhp; |
| 1523 | u_header_T **uhpp; /* if not NULL reset when freeing this header */ |
| 1524 | { |
| 1525 | u_header_T *tofree, *next; |
| 1526 | |
| 1527 | if (uhp->uh_alt_prev != NULL) |
| 1528 | uhp->uh_alt_prev->uh_alt_next = NULL; |
| 1529 | |
| 1530 | next = uhp; |
| 1531 | while (next != NULL) |
| 1532 | { |
| 1533 | tofree = next; |
| 1534 | if (tofree->uh_alt_next != NULL) |
| 1535 | u_freebranch(buf, tofree->uh_alt_next, uhpp); /* recursive */ |
| 1536 | next = tofree->uh_prev; |
| 1537 | u_freeentries(buf, tofree, uhpp); |
| 1538 | } |
| 1539 | } |
| 1540 | |
| 1541 | /* |
| 1542 | * Free all the undo entries for one header and the header itself. |
| 1543 | * This means that "uhp" is invalid when returning. |
| 1544 | */ |
| 1545 | static void |
| 1546 | u_freeentries(buf, uhp, uhpp) |
| 1547 | buf_T *buf; |
| 1548 | u_header_T *uhp; |
| 1549 | u_header_T **uhpp; /* if not NULL reset when freeing this header */ |
| 1550 | { |
| 1551 | u_entry_T *uep, *nuep; |
| 1552 | |
| 1553 | /* Check for pointers to the header that become invalid now. */ |
| 1554 | if (buf->b_u_curhead == uhp) |
| 1555 | buf->b_u_curhead = NULL; |
| 1556 | if (uhpp != NULL && uhp == *uhpp) |
| 1557 | *uhpp = NULL; |
| 1558 | |
| 1559 | for (uep = uhp->uh_entry; uep != NULL; uep = nuep) |
| 1560 | { |
| 1561 | nuep = uep->ue_next; |
| 1562 | u_freeentry(uep, uep->ue_size); |
| 1563 | } |
| 1564 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1565 | U_FREE_LINE((char_u *)uhp); |
| 1566 | --buf->b_u_numhead; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1567 | } |
| 1568 | |
| 1569 | /* |
| 1570 | * free entry 'uep' and 'n' lines in uep->ue_array[] |
| 1571 | */ |
| 1572 | static void |
| 1573 | u_freeentry(uep, n) |
| 1574 | u_entry_T *uep; |
| 1575 | long n; |
| 1576 | { |
Bram Moolenaar | 8d34330 | 2005-07-12 22:46:17 +0000 | [diff] [blame] | 1577 | while (n > 0) |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1578 | U_FREE_LINE(uep->ue_array[--n]); |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 1579 | U_FREE_LINE((char_u *)uep->ue_array); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1580 | U_FREE_LINE((char_u *)uep); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1581 | } |
| 1582 | |
| 1583 | /* |
| 1584 | * invalidate the undo buffer; called when storage has already been released |
| 1585 | */ |
| 1586 | void |
| 1587 | u_clearall(buf) |
| 1588 | buf_T *buf; |
| 1589 | { |
| 1590 | buf->b_u_newhead = buf->b_u_oldhead = buf->b_u_curhead = NULL; |
| 1591 | buf->b_u_synced = TRUE; |
| 1592 | buf->b_u_numhead = 0; |
| 1593 | buf->b_u_line_ptr = NULL; |
| 1594 | buf->b_u_line_lnum = 0; |
| 1595 | } |
| 1596 | |
| 1597 | /* |
| 1598 | * save the line "lnum" for the "U" command |
| 1599 | */ |
| 1600 | void |
| 1601 | u_saveline(lnum) |
| 1602 | linenr_T lnum; |
| 1603 | { |
| 1604 | if (lnum == curbuf->b_u_line_lnum) /* line is already saved */ |
| 1605 | return; |
Bram Moolenaar | 1ec484f | 2005-06-24 23:07:47 +0000 | [diff] [blame] | 1606 | 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] | 1607 | return; |
| 1608 | u_clearline(); |
| 1609 | curbuf->b_u_line_lnum = lnum; |
| 1610 | if (curwin->w_cursor.lnum == lnum) |
| 1611 | curbuf->b_u_line_colnr = curwin->w_cursor.col; |
| 1612 | else |
| 1613 | curbuf->b_u_line_colnr = 0; |
| 1614 | if ((curbuf->b_u_line_ptr = u_save_line(lnum)) == NULL) |
| 1615 | do_outofmem_msg((long_u)0); |
| 1616 | } |
| 1617 | |
| 1618 | /* |
| 1619 | * clear the line saved for the "U" command |
| 1620 | * (this is used externally for crossing a line while in insert mode) |
| 1621 | */ |
| 1622 | void |
| 1623 | u_clearline() |
| 1624 | { |
| 1625 | if (curbuf->b_u_line_ptr != NULL) |
| 1626 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1627 | U_FREE_LINE(curbuf->b_u_line_ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1628 | curbuf->b_u_line_ptr = NULL; |
| 1629 | curbuf->b_u_line_lnum = 0; |
| 1630 | } |
| 1631 | } |
| 1632 | |
| 1633 | /* |
| 1634 | * Implementation of the "U" command. |
| 1635 | * Differentiation from vi: "U" can be undone with the next "U". |
| 1636 | * We also allow the cursor to be in another line. |
| 1637 | */ |
| 1638 | void |
| 1639 | u_undoline() |
| 1640 | { |
| 1641 | colnr_T t; |
| 1642 | char_u *oldp; |
| 1643 | |
| 1644 | if (undo_off) |
| 1645 | return; |
| 1646 | |
| 1647 | if (curbuf->b_u_line_ptr == NULL || |
| 1648 | curbuf->b_u_line_lnum > curbuf->b_ml.ml_line_count) |
| 1649 | { |
| 1650 | beep_flush(); |
| 1651 | return; |
| 1652 | } |
| 1653 | /* first save the line for the 'u' command */ |
| 1654 | if (u_savecommon(curbuf->b_u_line_lnum - 1, |
| 1655 | curbuf->b_u_line_lnum + 1, (linenr_T)0) == FAIL) |
| 1656 | return; |
| 1657 | oldp = u_save_line(curbuf->b_u_line_lnum); |
| 1658 | if (oldp == NULL) |
| 1659 | { |
| 1660 | do_outofmem_msg((long_u)0); |
| 1661 | return; |
| 1662 | } |
| 1663 | ml_replace(curbuf->b_u_line_lnum, curbuf->b_u_line_ptr, TRUE); |
| 1664 | changed_bytes(curbuf->b_u_line_lnum, 0); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1665 | U_FREE_LINE(curbuf->b_u_line_ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1666 | curbuf->b_u_line_ptr = oldp; |
| 1667 | |
| 1668 | t = curbuf->b_u_line_colnr; |
| 1669 | if (curwin->w_cursor.lnum == curbuf->b_u_line_lnum) |
| 1670 | curbuf->b_u_line_colnr = curwin->w_cursor.col; |
| 1671 | curwin->w_cursor.col = t; |
| 1672 | curwin->w_cursor.lnum = curbuf->b_u_line_lnum; |
| 1673 | } |
| 1674 | |
| 1675 | /* |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1676 | * There are two implementations of the memory management for undo: |
| 1677 | * 1. Use the standard malloc()/free() functions. |
| 1678 | * This should be fast for allocating memory, but when a buffer is |
| 1679 | * abandoned every single allocated chunk must be freed, which may be slow. |
| 1680 | * 2. Allocate larger blocks of memory and keep track of chunks ourselves. |
| 1681 | * This is fast for abandoning, but the use of linked lists is slow for |
| 1682 | * finding a free chunk. Esp. when a lot of lines are changed or deleted. |
| 1683 | * A bit of profiling showed that the first method is faster, especially when |
| 1684 | * making a large number of changes, under the condition that malloc()/free() |
| 1685 | * is implemented efficiently. |
| 1686 | */ |
| 1687 | #ifdef U_USE_MALLOC |
| 1688 | /* |
| 1689 | * Version of undo memory allocation using malloc()/free() |
| 1690 | * |
| 1691 | * U_FREE_LINE() and U_ALLOC_LINE() are macros that invoke vim_free() and |
| 1692 | * lalloc() directly. |
| 1693 | */ |
| 1694 | |
| 1695 | /* |
| 1696 | * Free all allocated memory blocks for the buffer 'buf'. |
| 1697 | */ |
| 1698 | void |
| 1699 | u_blockfree(buf) |
| 1700 | buf_T *buf; |
| 1701 | { |
Bram Moolenaar | 1e60789 | 2006-03-13 22:15:53 +0000 | [diff] [blame] | 1702 | while (buf->b_u_oldhead != NULL) |
Bram Moolenaar | 1f4d4de | 2006-03-14 23:00:46 +0000 | [diff] [blame] | 1703 | u_freeheader(buf, buf->b_u_oldhead, NULL); |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 1704 | U_FREE_LINE(buf->b_u_line_ptr); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
| 1707 | #else |
| 1708 | /* |
| 1709 | * Storage allocation for the undo lines and blocks of the current file. |
| 1710 | * Version where Vim keeps track of the available memory. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1711 | */ |
| 1712 | |
| 1713 | /* |
| 1714 | * Memory is allocated in relatively large blocks. These blocks are linked |
| 1715 | * in the allocated block list, headed by curbuf->b_block_head. They are all |
| 1716 | * freed when abandoning a file, so we don't have to free every single line. |
| 1717 | * The list is kept sorted on memory address. |
| 1718 | * block_alloc() allocates a block. |
| 1719 | * m_blockfree() frees all blocks. |
| 1720 | * |
| 1721 | * The available chunks of memory are kept in free chunk lists. There is |
| 1722 | * one free list for each block of allocated memory. The list is kept sorted |
| 1723 | * on memory address. |
| 1724 | * u_alloc_line() gets a chunk from the free lists. |
| 1725 | * u_free_line() returns a chunk to the free lists. |
| 1726 | * curbuf->b_m_search points to the chunk before the chunk that was |
| 1727 | * freed/allocated the last time. |
| 1728 | * curbuf->b_mb_current points to the b_head where curbuf->b_m_search |
| 1729 | * points into the free list. |
| 1730 | * |
| 1731 | * |
| 1732 | * b_block_head /---> block #1 /---> block #2 |
| 1733 | * mb_next ---/ mb_next ---/ mb_next ---> NULL |
| 1734 | * mb_info mb_info mb_info |
| 1735 | * | | | |
| 1736 | * V V V |
| 1737 | * NULL free chunk #1.1 free chunk #2.1 |
| 1738 | * | | |
| 1739 | * V V |
| 1740 | * free chunk #1.2 NULL |
| 1741 | * | |
| 1742 | * V |
| 1743 | * NULL |
| 1744 | * |
| 1745 | * When a single free chunk list would have been used, it could take a lot |
| 1746 | * of time in u_free_line() to find the correct place to insert a chunk in the |
| 1747 | * free list. The single free list would become very long when many lines are |
| 1748 | * changed (e.g. with :%s/^M$//). |
| 1749 | */ |
| 1750 | |
| 1751 | /* |
| 1752 | * this blocksize is used when allocating new lines |
| 1753 | */ |
| 1754 | #define MEMBLOCKSIZE 2044 |
| 1755 | |
| 1756 | /* |
| 1757 | * The size field contains the size of the chunk, including the size field |
| 1758 | * itself. |
| 1759 | * |
| 1760 | * When the chunk is not in-use it is preceded with the m_info structure. |
| 1761 | * The m_next field links it in one of the free chunk lists. |
| 1762 | * |
| 1763 | * On most unix systems structures have to be longword (32 or 64 bit) aligned. |
| 1764 | * On most other systems they are short (16 bit) aligned. |
| 1765 | */ |
| 1766 | |
| 1767 | /* the structure definitions are now in structs.h */ |
| 1768 | |
| 1769 | #ifdef ALIGN_LONG |
| 1770 | /* size of m_size */ |
| 1771 | # define M_OFFSET (sizeof(long_u)) |
| 1772 | #else |
| 1773 | /* size of m_size */ |
| 1774 | # define M_OFFSET (sizeof(short_u)) |
| 1775 | #endif |
| 1776 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1777 | static char_u *u_blockalloc __ARGS((long_u)); |
| 1778 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1779 | /* |
| 1780 | * Allocate a block of memory and link it in the allocated block list. |
| 1781 | */ |
| 1782 | static char_u * |
| 1783 | u_blockalloc(size) |
| 1784 | long_u size; |
| 1785 | { |
| 1786 | mblock_T *p; |
| 1787 | mblock_T *mp, *next; |
| 1788 | |
| 1789 | p = (mblock_T *)lalloc(size + sizeof(mblock_T), FALSE); |
| 1790 | if (p != NULL) |
| 1791 | { |
| 1792 | /* Insert the block into the allocated block list, keeping it |
| 1793 | sorted on address. */ |
| 1794 | for (mp = &curbuf->b_block_head; |
| 1795 | (next = mp->mb_next) != NULL && next < p; |
| 1796 | mp = next) |
| 1797 | ; |
| 1798 | p->mb_next = next; /* link in block list */ |
| 1799 | p->mb_size = size; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1800 | p->mb_maxsize = 0; /* nothing free yet */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1801 | mp->mb_next = p; |
| 1802 | p->mb_info.m_next = NULL; /* clear free list */ |
| 1803 | p->mb_info.m_size = 0; |
| 1804 | curbuf->b_mb_current = p; /* remember current block */ |
| 1805 | curbuf->b_m_search = NULL; |
| 1806 | p++; /* return usable memory */ |
| 1807 | } |
| 1808 | return (char_u *)p; |
| 1809 | } |
| 1810 | |
| 1811 | /* |
| 1812 | * free all allocated memory blocks for the buffer 'buf' |
| 1813 | */ |
| 1814 | void |
| 1815 | u_blockfree(buf) |
| 1816 | buf_T *buf; |
| 1817 | { |
| 1818 | mblock_T *p, *np; |
| 1819 | |
| 1820 | for (p = buf->b_block_head.mb_next; p != NULL; p = np) |
| 1821 | { |
| 1822 | np = p->mb_next; |
| 1823 | vim_free(p); |
| 1824 | } |
| 1825 | buf->b_block_head.mb_next = NULL; |
| 1826 | buf->b_m_search = NULL; |
| 1827 | buf->b_mb_current = NULL; |
| 1828 | } |
| 1829 | |
| 1830 | /* |
| 1831 | * Free a chunk of memory for the current buffer. |
| 1832 | * Insert the chunk into the correct free list, keeping it sorted on address. |
| 1833 | */ |
| 1834 | static void |
| 1835 | u_free_line(ptr, keep) |
| 1836 | char_u *ptr; |
| 1837 | int keep; /* don't free the block when it's empty */ |
| 1838 | { |
| 1839 | minfo_T *next; |
| 1840 | minfo_T *prev, *curr; |
| 1841 | minfo_T *mp; |
| 1842 | mblock_T *nextb; |
| 1843 | mblock_T *prevb; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1844 | long_u maxsize; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1845 | |
| 1846 | if (ptr == NULL || ptr == IObuff) |
| 1847 | return; /* illegal address can happen in out-of-memory situations */ |
| 1848 | |
| 1849 | mp = (minfo_T *)(ptr - M_OFFSET); |
| 1850 | |
| 1851 | /* find block where chunk could be a part off */ |
| 1852 | /* if we change curbuf->b_mb_current, curbuf->b_m_search is set to NULL */ |
| 1853 | if (curbuf->b_mb_current == NULL || mp < (minfo_T *)curbuf->b_mb_current) |
| 1854 | { |
| 1855 | curbuf->b_mb_current = curbuf->b_block_head.mb_next; |
| 1856 | curbuf->b_m_search = NULL; |
| 1857 | } |
| 1858 | if ((nextb = curbuf->b_mb_current->mb_next) != NULL |
| 1859 | && (minfo_T *)nextb < mp) |
| 1860 | { |
| 1861 | curbuf->b_mb_current = nextb; |
| 1862 | curbuf->b_m_search = NULL; |
| 1863 | } |
| 1864 | while ((nextb = curbuf->b_mb_current->mb_next) != NULL |
| 1865 | && (minfo_T *)nextb < mp) |
| 1866 | curbuf->b_mb_current = nextb; |
| 1867 | |
| 1868 | curr = NULL; |
| 1869 | /* |
| 1870 | * If mp is smaller than curbuf->b_m_search->m_next go to the start of |
| 1871 | * the free list |
| 1872 | */ |
| 1873 | if (curbuf->b_m_search == NULL || mp < (curbuf->b_m_search->m_next)) |
| 1874 | next = &(curbuf->b_mb_current->mb_info); |
| 1875 | else |
| 1876 | next = curbuf->b_m_search; |
| 1877 | /* |
| 1878 | * The following loop is executed very often. |
| 1879 | * Therefore it has been optimized at the cost of readability. |
| 1880 | * Keep it fast! |
| 1881 | */ |
| 1882 | #ifdef SLOW_BUT_EASY_TO_READ |
| 1883 | do |
| 1884 | { |
| 1885 | prev = curr; |
| 1886 | curr = next; |
| 1887 | next = next->m_next; |
| 1888 | } |
| 1889 | while (mp > next && next != NULL); |
| 1890 | #else |
| 1891 | do /* first, middle, last */ |
| 1892 | { |
| 1893 | prev = next->m_next; /* curr, next, prev */ |
| 1894 | if (prev == NULL || mp <= prev) |
| 1895 | { |
| 1896 | prev = curr; |
| 1897 | curr = next; |
| 1898 | next = next->m_next; |
| 1899 | break; |
| 1900 | } |
| 1901 | curr = prev->m_next; /* next, prev, curr */ |
| 1902 | if (curr == NULL || mp <= curr) |
| 1903 | { |
| 1904 | prev = next; |
| 1905 | curr = prev->m_next; |
| 1906 | next = curr->m_next; |
| 1907 | break; |
| 1908 | } |
| 1909 | next = curr->m_next; /* prev, curr, next */ |
| 1910 | } |
| 1911 | while (mp > next && next != NULL); |
| 1912 | #endif |
| 1913 | |
| 1914 | /* if *mp and *next are concatenated, join them into one chunk */ |
| 1915 | if ((char_u *)mp + mp->m_size == (char_u *)next) |
| 1916 | { |
| 1917 | mp->m_size += next->m_size; |
| 1918 | mp->m_next = next->m_next; |
| 1919 | } |
| 1920 | else |
| 1921 | mp->m_next = next; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1922 | maxsize = mp->m_size; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1923 | |
| 1924 | /* if *curr and *mp are concatenated, join them */ |
| 1925 | if (prev != NULL && (char_u *)curr + curr->m_size == (char_u *)mp) |
| 1926 | { |
| 1927 | curr->m_size += mp->m_size; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1928 | maxsize = curr->m_size; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1929 | curr->m_next = mp->m_next; |
| 1930 | curbuf->b_m_search = prev; |
| 1931 | } |
| 1932 | else |
| 1933 | { |
| 1934 | curr->m_next = mp; |
| 1935 | curbuf->b_m_search = curr; /* put curbuf->b_m_search before freed |
| 1936 | chunk */ |
| 1937 | } |
| 1938 | |
| 1939 | /* |
| 1940 | * If the block only containes free memory now, release it. |
| 1941 | */ |
| 1942 | if (!keep && curbuf->b_mb_current->mb_size |
| 1943 | == curbuf->b_mb_current->mb_info.m_next->m_size) |
| 1944 | { |
| 1945 | /* Find the block before the current one to be able to unlink it from |
| 1946 | * the list of blocks. */ |
| 1947 | prevb = &curbuf->b_block_head; |
| 1948 | for (nextb = prevb->mb_next; nextb != curbuf->b_mb_current; |
| 1949 | nextb = nextb->mb_next) |
| 1950 | prevb = nextb; |
| 1951 | prevb->mb_next = nextb->mb_next; |
| 1952 | vim_free(nextb); |
| 1953 | curbuf->b_mb_current = NULL; |
| 1954 | curbuf->b_m_search = NULL; |
| 1955 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1956 | else if (curbuf->b_mb_current->mb_maxsize < maxsize) |
| 1957 | curbuf->b_mb_current->mb_maxsize = maxsize; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1958 | } |
| 1959 | |
| 1960 | /* |
| 1961 | * Allocate and initialize a new line structure with room for at least |
| 1962 | * 'size' characters plus a terminating NUL. |
| 1963 | */ |
| 1964 | static char_u * |
| 1965 | u_alloc_line(size) |
| 1966 | unsigned size; |
| 1967 | { |
| 1968 | minfo_T *mp, *mprev, *mp2; |
| 1969 | mblock_T *mbp; |
| 1970 | int size_align; |
| 1971 | |
| 1972 | /* |
| 1973 | * Add room for size field and trailing NUL byte. |
| 1974 | * Adjust for minimal size (must be able to store minfo_T |
| 1975 | * plus a trailing NUL, so the chunk can be released again) |
| 1976 | */ |
| 1977 | size += M_OFFSET + 1; |
| 1978 | if (size < sizeof(minfo_T) + 1) |
| 1979 | size = sizeof(minfo_T) + 1; |
| 1980 | |
| 1981 | /* |
| 1982 | * round size up for alignment |
| 1983 | */ |
| 1984 | size_align = (size + ALIGN_MASK) & ~ALIGN_MASK; |
| 1985 | |
| 1986 | /* |
| 1987 | * If curbuf->b_m_search is NULL (uninitialized free list) start at |
| 1988 | * curbuf->b_block_head |
| 1989 | */ |
| 1990 | if (curbuf->b_mb_current == NULL || curbuf->b_m_search == NULL) |
| 1991 | { |
| 1992 | curbuf->b_mb_current = &curbuf->b_block_head; |
| 1993 | curbuf->b_m_search = &(curbuf->b_block_head.mb_info); |
| 1994 | } |
| 1995 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1996 | /* Search for a block with enough space. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1997 | mbp = curbuf->b_mb_current; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1998 | while (mbp->mb_maxsize < size_align) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1999 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2000 | if (mbp->mb_next != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2001 | mbp = mbp->mb_next; |
| 2002 | else |
| 2003 | mbp = &curbuf->b_block_head; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2004 | if (mbp == curbuf->b_mb_current) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2005 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2006 | int n = (size_align > (MEMBLOCKSIZE / 4) |
| 2007 | ? size_align : MEMBLOCKSIZE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2008 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2009 | /* Back where we started in block list: need to add a new block |
| 2010 | * with enough space. */ |
| 2011 | mp = (minfo_T *)u_blockalloc((long_u)n); |
| 2012 | if (mp == NULL) |
| 2013 | return (NULL); |
| 2014 | mp->m_size = n; |
| 2015 | u_free_line((char_u *)mp + M_OFFSET, TRUE); |
| 2016 | mbp = curbuf->b_mb_current; |
| 2017 | break; |
| 2018 | } |
| 2019 | } |
| 2020 | if (mbp != curbuf->b_mb_current) |
| 2021 | curbuf->b_m_search = &(mbp->mb_info); |
| 2022 | |
| 2023 | /* In this block find a chunk with enough space. */ |
| 2024 | mprev = curbuf->b_m_search; |
| 2025 | mp = curbuf->b_m_search->m_next; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 2026 | for (;;) |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2027 | { |
| 2028 | if (mp == NULL) /* at end of the list */ |
| 2029 | mp = &(mbp->mb_info); /* wrap around to begin */ |
| 2030 | if (mp->m_size >= size) |
| 2031 | break; |
| 2032 | if (mp == curbuf->b_m_search) |
| 2033 | { |
| 2034 | /* back where we started in free chunk list: "cannot happen" */ |
| 2035 | EMSG2(_(e_intern2), "u_alloc_line()"); |
| 2036 | return NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2037 | } |
| 2038 | mprev = mp; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2039 | mp = mp->m_next; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2040 | } |
| 2041 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2042 | /* when using the largest chunk adjust mb_maxsize */ |
| 2043 | if (mp->m_size >= mbp->mb_maxsize) |
| 2044 | mbp->mb_maxsize = 0; |
| 2045 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2046 | /* if the chunk we found is large enough, split it up in two */ |
| 2047 | if ((long)mp->m_size - size_align >= (long)(sizeof(minfo_T) + 1)) |
| 2048 | { |
| 2049 | mp2 = (minfo_T *)((char_u *)mp + size_align); |
| 2050 | mp2->m_size = mp->m_size - size_align; |
| 2051 | mp2->m_next = mp->m_next; |
| 2052 | mprev->m_next = mp2; |
| 2053 | mp->m_size = size_align; |
| 2054 | } |
| 2055 | else /* remove *mp from the free list */ |
| 2056 | { |
| 2057 | mprev->m_next = mp->m_next; |
| 2058 | } |
| 2059 | curbuf->b_m_search = mprev; |
| 2060 | curbuf->b_mb_current = mbp; |
| 2061 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2062 | /* If using the largest chunk need to find the new largest chunk */ |
| 2063 | if (mbp->mb_maxsize == 0) |
| 2064 | for (mp2 = &(mbp->mb_info); mp2 != NULL; mp2 = mp2->m_next) |
| 2065 | if (mbp->mb_maxsize < mp2->m_size) |
| 2066 | mbp->mb_maxsize = mp2->m_size; |
| 2067 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2068 | mp = (minfo_T *)((char_u *)mp + M_OFFSET); |
| 2069 | *(char_u *)mp = NUL; /* set the first byte to NUL */ |
| 2070 | |
| 2071 | return ((char_u *)mp); |
| 2072 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2073 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2074 | |
| 2075 | /* |
| 2076 | * u_save_line(): allocate memory with u_alloc_line() and copy line 'lnum' |
| 2077 | * into it. |
| 2078 | */ |
| 2079 | static char_u * |
| 2080 | u_save_line(lnum) |
| 2081 | linenr_T lnum; |
| 2082 | { |
| 2083 | char_u *src; |
| 2084 | char_u *dst; |
| 2085 | unsigned len; |
| 2086 | |
| 2087 | src = ml_get(lnum); |
| 2088 | len = (unsigned)STRLEN(src); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2089 | if ((dst = U_ALLOC_LINE(len)) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2090 | mch_memmove(dst, src, (size_t)(len + 1)); |
| 2091 | return (dst); |
| 2092 | } |
| 2093 | |
| 2094 | /* |
| 2095 | * Check if the 'modified' flag is set, or 'ff' has changed (only need to |
| 2096 | * check the first character, because it can only be "dos", "unix" or "mac"). |
| 2097 | * "nofile" and "scratch" type buffers are considered to always be unchanged. |
| 2098 | */ |
| 2099 | int |
| 2100 | bufIsChanged(buf) |
| 2101 | buf_T *buf; |
| 2102 | { |
| 2103 | return |
| 2104 | #ifdef FEAT_QUICKFIX |
| 2105 | !bt_dontwrite(buf) && |
| 2106 | #endif |
| 2107 | (buf->b_changed || file_ff_differs(buf)); |
| 2108 | } |
| 2109 | |
| 2110 | int |
| 2111 | curbufIsChanged() |
| 2112 | { |
| 2113 | return |
| 2114 | #ifdef FEAT_QUICKFIX |
| 2115 | !bt_dontwrite(curbuf) && |
| 2116 | #endif |
| 2117 | (curbuf->b_changed || file_ff_differs(curbuf)); |
| 2118 | } |