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 | /* for debugging */ |
| 11 | /* #define CHECK(c, s) if (c) EMSG(s) */ |
| 12 | #define CHECK(c, s) |
| 13 | |
| 14 | /* |
| 15 | * memline.c: Contains the functions for appending, deleting and changing the |
| 16 | * text lines. The memfile functions are used to store the information in blocks |
| 17 | * of memory, backed up by a file. The structure of the information is a tree. |
| 18 | * The root of the tree is a pointer block. The leaves of the tree are data |
| 19 | * blocks. In between may be several layers of pointer blocks, forming branches. |
| 20 | * |
| 21 | * Three types of blocks are used: |
| 22 | * - Block nr 0 contains information for recovery |
| 23 | * - Pointer blocks contain list of pointers to other blocks. |
| 24 | * - Data blocks contain the actual text. |
| 25 | * |
| 26 | * Block nr 0 contains the block0 structure (see below). |
| 27 | * |
| 28 | * Block nr 1 is the first pointer block. It is the root of the tree. |
| 29 | * Other pointer blocks are branches. |
| 30 | * |
| 31 | * If a line is too big to fit in a single page, the block containing that |
| 32 | * line is made big enough to hold the line. It may span several pages. |
| 33 | * Otherwise all blocks are one page. |
| 34 | * |
| 35 | * A data block that was filled when starting to edit a file and was not |
| 36 | * changed since then, can have a negative block number. This means that it |
| 37 | * has not yet been assigned a place in the file. When recovering, the lines |
| 38 | * in this data block can be read from the original file. When the block is |
| 39 | * changed (lines appended/deleted/changed) or when it is flushed it gets a |
| 40 | * positive number. Use mf_trans_del() to get the new number, before calling |
| 41 | * mf_get(). |
| 42 | */ |
| 43 | |
| 44 | #if defined(MSDOS) || defined(WIN32) || defined(_WIN64) |
| 45 | # include <io.h> |
| 46 | #endif |
| 47 | |
| 48 | #include "vim.h" |
| 49 | |
| 50 | #ifdef HAVE_FCNTL_H |
| 51 | # include <fcntl.h> |
| 52 | #endif |
| 53 | #ifndef UNIX /* it's in os_unix.h for Unix */ |
| 54 | # include <time.h> |
| 55 | #endif |
| 56 | |
| 57 | #ifdef SASC |
| 58 | # include <proto/dos.h> /* for Open() and Close() */ |
| 59 | #endif |
| 60 | |
| 61 | typedef struct block0 ZERO_BL; /* contents of the first block */ |
| 62 | typedef struct pointer_block PTR_BL; /* contents of a pointer block */ |
| 63 | typedef struct data_block DATA_BL; /* contents of a data block */ |
| 64 | typedef struct pointer_entry PTR_EN; /* block/line-count pair */ |
| 65 | |
| 66 | #define DATA_ID (('d' << 8) + 'a') /* data block id */ |
| 67 | #define PTR_ID (('p' << 8) + 't') /* pointer block id */ |
| 68 | #define BLOCK0_ID0 'b' /* block 0 id 0 */ |
| 69 | #define BLOCK0_ID1 '0' /* block 0 id 1 */ |
| 70 | |
| 71 | /* |
| 72 | * pointer to a block, used in a pointer block |
| 73 | */ |
| 74 | struct pointer_entry |
| 75 | { |
| 76 | blocknr_T pe_bnum; /* block number */ |
| 77 | linenr_T pe_line_count; /* number of lines in this branch */ |
| 78 | linenr_T pe_old_lnum; /* lnum for this block (for recovery) */ |
| 79 | int pe_page_count; /* number of pages in block pe_bnum */ |
| 80 | }; |
| 81 | |
| 82 | /* |
| 83 | * A pointer block contains a list of branches in the tree. |
| 84 | */ |
| 85 | struct pointer_block |
| 86 | { |
| 87 | short_u pb_id; /* ID for pointer block: PTR_ID */ |
| 88 | short_u pb_count; /* number of pointer in this block */ |
| 89 | short_u pb_count_max; /* maximum value for pb_count */ |
| 90 | PTR_EN pb_pointer[1]; /* list of pointers to blocks (actually longer) |
| 91 | * followed by empty space until end of page */ |
| 92 | }; |
| 93 | |
| 94 | /* |
| 95 | * A data block is a leaf in the tree. |
| 96 | * |
| 97 | * The text of the lines is at the end of the block. The text of the first line |
| 98 | * in the block is put at the end, the text of the second line in front of it, |
| 99 | * etc. Thus the order of the lines is the opposite of the line number. |
| 100 | */ |
| 101 | struct data_block |
| 102 | { |
| 103 | short_u db_id; /* ID for data block: DATA_ID */ |
| 104 | unsigned db_free; /* free space available */ |
| 105 | unsigned db_txt_start; /* byte where text starts */ |
| 106 | unsigned db_txt_end; /* byte just after data block */ |
| 107 | linenr_T db_line_count; /* number of lines in this block */ |
| 108 | unsigned db_index[1]; /* index for start of line (actually bigger) |
| 109 | * followed by empty space upto db_txt_start |
| 110 | * followed by the text in the lines until |
| 111 | * end of page */ |
| 112 | }; |
| 113 | |
| 114 | /* |
| 115 | * The low bits of db_index hold the actual index. The topmost bit is |
| 116 | * used for the global command to be able to mark a line. |
| 117 | * This method is not clean, but otherwise there would be at least one extra |
| 118 | * byte used for each line. |
| 119 | * The mark has to be in this place to keep it with the correct line when other |
| 120 | * lines are inserted or deleted. |
| 121 | */ |
| 122 | #define DB_MARKED ((unsigned)1 << ((sizeof(unsigned) * 8) - 1)) |
| 123 | #define DB_INDEX_MASK (~DB_MARKED) |
| 124 | |
| 125 | #define INDEX_SIZE (sizeof(unsigned)) /* size of one db_index entry */ |
| 126 | #define HEADER_SIZE (sizeof(DATA_BL) - INDEX_SIZE) /* size of data block header */ |
| 127 | |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 128 | #define B0_FNAME_SIZE_ORG 900 /* what it was in older versions */ |
| 129 | #define B0_FNAME_SIZE 898 |
| 130 | #define B0_UNAME_SIZE 40 |
| 131 | #define B0_HNAME_SIZE 40 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 132 | /* |
| 133 | * Restrict the numbers to 32 bits, otherwise most compilers will complain. |
| 134 | * This won't detect a 64 bit machine that only swaps a byte in the top 32 |
| 135 | * bits, but that is crazy anyway. |
| 136 | */ |
| 137 | #define B0_MAGIC_LONG 0x30313233L |
| 138 | #define B0_MAGIC_INT 0x20212223L |
| 139 | #define B0_MAGIC_SHORT 0x10111213L |
| 140 | #define B0_MAGIC_CHAR 0x55 |
| 141 | |
| 142 | /* |
| 143 | * Block zero holds all info about the swap file. |
| 144 | * |
| 145 | * NOTE: DEFINITION OF BLOCK 0 SHOULD NOT CHANGE! It would make all existing |
| 146 | * swap files unusable! |
| 147 | * |
| 148 | * If size of block0 changes anyway, adjust MIN_SWAP_PAGE_SIZE in vim.h!! |
| 149 | * |
| 150 | * This block is built up of single bytes, to make it portable accros |
| 151 | * different machines. b0_magic_* is used to check the byte order and size of |
| 152 | * variables, because the rest of the swap file is not portable. |
| 153 | */ |
| 154 | struct block0 |
| 155 | { |
| 156 | char_u b0_id[2]; /* id for block 0: BLOCK0_ID0 and BLOCK0_ID1 */ |
| 157 | char_u b0_version[10]; /* Vim version string */ |
| 158 | char_u b0_page_size[4];/* number of bytes per page */ |
| 159 | char_u b0_mtime[4]; /* last modification time of file */ |
| 160 | char_u b0_ino[4]; /* inode of b0_fname */ |
| 161 | char_u b0_pid[4]; /* process id of creator (or 0) */ |
| 162 | char_u b0_uname[B0_UNAME_SIZE]; /* name of user (uid if no name) */ |
| 163 | char_u b0_hname[B0_HNAME_SIZE]; /* host name (if it has a name) */ |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 164 | char_u b0_fname[B0_FNAME_SIZE_ORG]; /* name of file being edited */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 165 | long b0_magic_long; /* check for byte order of long */ |
| 166 | int b0_magic_int; /* check for byte order of int */ |
| 167 | short b0_magic_short; /* check for byte order of short */ |
| 168 | char_u b0_magic_char; /* check for last char */ |
| 169 | }; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 170 | |
| 171 | /* |
| 172 | * Note: b0_fname and b0_flags are put at the end of the file name. For very |
| 173 | * long file names in older versions of Vim they are invalid. |
| 174 | * The 'fileencoding' comes before b0_flags, with a NUL in front. But only |
| 175 | * when there is room, for very long file names it's omitted. |
| 176 | */ |
| 177 | #define B0_DIRTY 0x55 |
| 178 | #define b0_dirty b0_fname[B0_FNAME_SIZE_ORG-1] |
| 179 | |
| 180 | /* |
| 181 | * The b0_flags field is new in Vim 7.0. |
| 182 | */ |
| 183 | #define b0_flags b0_fname[B0_FNAME_SIZE_ORG-2] |
| 184 | |
| 185 | /* The lowest two bits contain the fileformat. Zero means it's not set |
| 186 | * (compatible with Vim 6.x), otherwise it's EOL_UNIX + 1, EOL_DOS + 1 or |
| 187 | * EOL_MAC + 1. */ |
| 188 | #define B0_FF_MASK 3 |
| 189 | |
| 190 | /* Swap file is in directory of edited file. Used to find the file from |
| 191 | * different mount points. */ |
| 192 | #define B0_SAME_DIR 4 |
| 193 | |
| 194 | /* The 'fileencoding' is at the end of b0_fname[], with a NUL in front of it. |
| 195 | * When empty there is only the NUL. */ |
| 196 | #define B0_HAS_FENC 8 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 197 | |
| 198 | #define STACK_INCR 5 /* nr of entries added to ml_stack at a time */ |
| 199 | |
| 200 | /* |
| 201 | * The line number where the first mark may be is remembered. |
| 202 | * If it is 0 there are no marks at all. |
| 203 | * (always used for the current buffer only, no buffer change possible while |
| 204 | * executing a global command). |
| 205 | */ |
| 206 | static linenr_T lowest_marked = 0; |
| 207 | |
| 208 | /* |
| 209 | * arguments for ml_find_line() |
| 210 | */ |
| 211 | #define ML_DELETE 0x11 /* delete line */ |
| 212 | #define ML_INSERT 0x12 /* insert line */ |
| 213 | #define ML_FIND 0x13 /* just find the line */ |
| 214 | #define ML_FLUSH 0x02 /* flush locked block */ |
| 215 | #define ML_SIMPLE(x) (x & 0x10) /* DEL, INS or FIND */ |
| 216 | |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 217 | static void ml_upd_block0 __ARGS((buf_T *buf, int setfname)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 218 | static void set_b0_fname __ARGS((ZERO_BL *, buf_T *buf)); |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 219 | static void set_b0_dir_flag __ARGS((ZERO_BL *b0p, buf_T *buf)); |
| 220 | #ifdef FEAT_MBYTE |
| 221 | static void add_b0_fenc __ARGS((ZERO_BL *b0p, buf_T *buf)); |
| 222 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 223 | static time_t swapfile_info __ARGS((char_u *)); |
| 224 | static int recov_file_names __ARGS((char_u **, char_u *, int prepend_dot)); |
| 225 | static int ml_append_int __ARGS((buf_T *, linenr_T, char_u *, colnr_T, int, int)); |
| 226 | static int ml_delete_int __ARGS((buf_T *, linenr_T, int)); |
| 227 | static char_u *findswapname __ARGS((buf_T *, char_u **, char_u *)); |
| 228 | static void ml_flush_line __ARGS((buf_T *)); |
| 229 | static bhdr_T *ml_new_data __ARGS((memfile_T *, int, int)); |
| 230 | static bhdr_T *ml_new_ptr __ARGS((memfile_T *)); |
| 231 | static bhdr_T *ml_find_line __ARGS((buf_T *, linenr_T, int)); |
| 232 | static int ml_add_stack __ARGS((buf_T *)); |
| 233 | static char_u *makeswapname __ARGS((buf_T *, char_u *)); |
| 234 | static void ml_lineadd __ARGS((buf_T *, int)); |
| 235 | static int b0_magic_wrong __ARGS((ZERO_BL *)); |
| 236 | #ifdef CHECK_INODE |
| 237 | static int fnamecmp_ino __ARGS((char_u *, char_u *, long)); |
| 238 | #endif |
| 239 | static void long_to_char __ARGS((long, char_u *)); |
| 240 | static long char_to_long __ARGS((char_u *)); |
| 241 | #if defined(UNIX) || defined(WIN3264) |
| 242 | static char_u *make_percent_swname __ARGS((char_u *dir, char_u *name)); |
| 243 | #endif |
| 244 | #ifdef FEAT_BYTEOFF |
| 245 | static void ml_updatechunk __ARGS((buf_T *buf, long line, long len, int updtype)); |
| 246 | #endif |
| 247 | |
| 248 | /* |
| 249 | * open a new memline for 'curbuf' |
| 250 | * |
| 251 | * return FAIL for failure, OK otherwise |
| 252 | */ |
| 253 | int |
| 254 | ml_open() |
| 255 | { |
| 256 | memfile_T *mfp; |
| 257 | bhdr_T *hp = NULL; |
| 258 | ZERO_BL *b0p; |
| 259 | PTR_BL *pp; |
| 260 | DATA_BL *dp; |
| 261 | |
| 262 | /* |
| 263 | * init fields in memline struct |
| 264 | */ |
| 265 | curbuf->b_ml.ml_stack_size = 0; /* no stack yet */ |
| 266 | curbuf->b_ml.ml_stack = NULL; /* no stack yet */ |
| 267 | curbuf->b_ml.ml_stack_top = 0; /* nothing in the stack */ |
| 268 | curbuf->b_ml.ml_locked = NULL; /* no cached block */ |
| 269 | curbuf->b_ml.ml_line_lnum = 0; /* no cached line */ |
| 270 | #ifdef FEAT_BYTEOFF |
| 271 | curbuf->b_ml.ml_chunksize = NULL; |
| 272 | #endif |
| 273 | |
| 274 | /* |
| 275 | * When 'updatecount' is non-zero, flag that a swap file may be opened later. |
| 276 | */ |
| 277 | if (p_uc && curbuf->b_p_swf) |
| 278 | curbuf->b_may_swap = TRUE; |
| 279 | else |
| 280 | curbuf->b_may_swap = FALSE; |
| 281 | |
| 282 | /* |
| 283 | * Open the memfile. No swap file is created yet. |
| 284 | */ |
| 285 | mfp = mf_open(NULL, 0); |
| 286 | if (mfp == NULL) |
| 287 | goto error; |
| 288 | |
| 289 | curbuf->b_ml.ml_mfp = mfp; |
| 290 | curbuf->b_ml.ml_flags = ML_EMPTY; |
| 291 | curbuf->b_ml.ml_line_count = 1; |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 292 | #ifdef FEAT_LINEBREAK |
| 293 | curwin->w_nrwidth_line_count = 0; |
| 294 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 295 | |
| 296 | #if defined(MSDOS) && !defined(DJGPP) |
| 297 | /* for 16 bit MS-DOS create a swapfile now, because we run out of |
| 298 | * memory very quickly */ |
| 299 | if (p_uc != 0) |
| 300 | ml_open_file(curbuf); |
| 301 | #endif |
| 302 | |
| 303 | /* |
| 304 | * fill block0 struct and write page 0 |
| 305 | */ |
| 306 | if ((hp = mf_new(mfp, FALSE, 1)) == NULL) |
| 307 | goto error; |
| 308 | if (hp->bh_bnum != 0) |
| 309 | { |
| 310 | EMSG(_("E298: Didn't get block nr 0?")); |
| 311 | goto error; |
| 312 | } |
| 313 | b0p = (ZERO_BL *)(hp->bh_data); |
| 314 | |
| 315 | b0p->b0_id[0] = BLOCK0_ID0; |
| 316 | b0p->b0_id[1] = BLOCK0_ID1; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 317 | b0p->b0_dirty = curbuf->b_changed ? B0_DIRTY : 0; |
| 318 | b0p->b0_flags = get_fileformat(curbuf) + 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 319 | b0p->b0_magic_long = (long)B0_MAGIC_LONG; |
| 320 | b0p->b0_magic_int = (int)B0_MAGIC_INT; |
| 321 | b0p->b0_magic_short = (short)B0_MAGIC_SHORT; |
| 322 | b0p->b0_magic_char = B0_MAGIC_CHAR; |
| 323 | |
| 324 | STRNCPY(b0p->b0_version, "VIM ", 4); |
| 325 | STRNCPY(b0p->b0_version + 4, Version, 6); |
| 326 | set_b0_fname(b0p, curbuf); |
| 327 | long_to_char((long)mfp->mf_page_size, b0p->b0_page_size); |
| 328 | (void)get_user_name(b0p->b0_uname, B0_UNAME_SIZE); |
| 329 | b0p->b0_uname[B0_UNAME_SIZE - 1] = NUL; |
| 330 | mch_get_host_name(b0p->b0_hname, B0_HNAME_SIZE); |
| 331 | b0p->b0_hname[B0_HNAME_SIZE - 1] = NUL; |
| 332 | long_to_char(mch_get_pid(), b0p->b0_pid); |
| 333 | |
| 334 | /* |
| 335 | * Always sync block number 0 to disk, so we can check the file name in |
| 336 | * the swap file in findswapname(). Don't do this for help files though. |
| 337 | * Only works when there's a swapfile, otherwise it's done when the file |
| 338 | * is created. |
| 339 | */ |
| 340 | mf_put(mfp, hp, TRUE, FALSE); |
| 341 | if (!curbuf->b_help) |
| 342 | (void)mf_sync(mfp, 0); |
| 343 | |
| 344 | /* |
| 345 | * fill in root pointer block and write page 1 |
| 346 | */ |
| 347 | if ((hp = ml_new_ptr(mfp)) == NULL) |
| 348 | goto error; |
| 349 | if (hp->bh_bnum != 1) |
| 350 | { |
| 351 | EMSG(_("E298: Didn't get block nr 1?")); |
| 352 | goto error; |
| 353 | } |
| 354 | pp = (PTR_BL *)(hp->bh_data); |
| 355 | pp->pb_count = 1; |
| 356 | pp->pb_pointer[0].pe_bnum = 2; |
| 357 | pp->pb_pointer[0].pe_page_count = 1; |
| 358 | pp->pb_pointer[0].pe_old_lnum = 1; |
| 359 | pp->pb_pointer[0].pe_line_count = 1; /* line count after insertion */ |
| 360 | mf_put(mfp, hp, TRUE, FALSE); |
| 361 | |
| 362 | /* |
| 363 | * allocate first data block and create an empty line 1. |
| 364 | */ |
| 365 | if ((hp = ml_new_data(mfp, FALSE, 1)) == NULL) |
| 366 | goto error; |
| 367 | if (hp->bh_bnum != 2) |
| 368 | { |
| 369 | EMSG(_("E298: Didn't get block nr 2?")); |
| 370 | goto error; |
| 371 | } |
| 372 | |
| 373 | dp = (DATA_BL *)(hp->bh_data); |
| 374 | dp->db_index[0] = --dp->db_txt_start; /* at end of block */ |
| 375 | dp->db_free -= 1 + INDEX_SIZE; |
| 376 | dp->db_line_count = 1; |
| 377 | *((char_u *)dp + dp->db_txt_start) = NUL; /* emtpy line */ |
| 378 | |
| 379 | return OK; |
| 380 | |
| 381 | error: |
| 382 | if (mfp != NULL) |
| 383 | { |
| 384 | if (hp) |
| 385 | mf_put(mfp, hp, FALSE, FALSE); |
| 386 | mf_close(mfp, TRUE); /* will also free(mfp->mf_fname) */ |
| 387 | } |
| 388 | curbuf->b_ml.ml_mfp = NULL; |
| 389 | return FAIL; |
| 390 | } |
| 391 | |
| 392 | /* |
| 393 | * ml_setname() is called when the file name of "buf" has been changed. |
| 394 | * It may rename the swap file. |
| 395 | */ |
| 396 | void |
| 397 | ml_setname(buf) |
| 398 | buf_T *buf; |
| 399 | { |
| 400 | int success = FALSE; |
| 401 | memfile_T *mfp; |
| 402 | char_u *fname; |
| 403 | char_u *dirp; |
| 404 | #if defined(MSDOS) || defined(MSWIN) |
| 405 | char_u *p; |
| 406 | #endif |
| 407 | |
| 408 | mfp = buf->b_ml.ml_mfp; |
| 409 | if (mfp->mf_fd < 0) /* there is no swap file yet */ |
| 410 | { |
| 411 | /* |
| 412 | * When 'updatecount' is 0 and 'noswapfile' there is no swap file. |
| 413 | * For help files we will make a swap file now. |
| 414 | */ |
| 415 | if (p_uc != 0) |
| 416 | ml_open_file(buf); /* create a swap file */ |
| 417 | return; |
| 418 | } |
| 419 | |
| 420 | /* |
| 421 | * Try all directories in the 'directory' option. |
| 422 | */ |
| 423 | dirp = p_dir; |
| 424 | for (;;) |
| 425 | { |
| 426 | if (*dirp == NUL) /* tried all directories, fail */ |
| 427 | break; |
Bram Moolenaar | 8fc061c | 2004-12-29 21:03:02 +0000 | [diff] [blame] | 428 | fname = findswapname(buf, &dirp, mfp->mf_fname); |
| 429 | /* alloc's fname */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 430 | if (fname == NULL) /* no file name found for this dir */ |
| 431 | continue; |
| 432 | |
| 433 | #if defined(MSDOS) || defined(MSWIN) |
| 434 | /* |
| 435 | * Set full pathname for swap file now, because a ":!cd dir" may |
| 436 | * change directory without us knowing it. |
| 437 | */ |
| 438 | p = FullName_save(fname, FALSE); |
| 439 | vim_free(fname); |
| 440 | fname = p; |
| 441 | if (fname == NULL) |
| 442 | continue; |
| 443 | #endif |
| 444 | /* if the file name is the same we don't have to do anything */ |
| 445 | if (fnamecmp(fname, mfp->mf_fname) == 0) |
| 446 | { |
| 447 | vim_free(fname); |
| 448 | success = TRUE; |
| 449 | break; |
| 450 | } |
| 451 | /* need to close the swap file before renaming */ |
| 452 | if (mfp->mf_fd >= 0) |
| 453 | { |
| 454 | close(mfp->mf_fd); |
| 455 | mfp->mf_fd = -1; |
| 456 | } |
| 457 | |
| 458 | /* try to rename the swap file */ |
| 459 | if (vim_rename(mfp->mf_fname, fname) == 0) |
| 460 | { |
| 461 | success = TRUE; |
| 462 | vim_free(mfp->mf_fname); |
| 463 | mfp->mf_fname = fname; |
| 464 | vim_free(mfp->mf_ffname); |
| 465 | #if defined(MSDOS) || defined(MSWIN) |
| 466 | mfp->mf_ffname = NULL; /* mf_fname is full pathname already */ |
| 467 | #else |
| 468 | mf_set_ffname(mfp); |
| 469 | #endif |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 470 | ml_upd_block0(buf, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 471 | break; |
| 472 | } |
| 473 | vim_free(fname); /* this fname didn't work, try another */ |
| 474 | } |
| 475 | |
| 476 | if (mfp->mf_fd == -1) /* need to (re)open the swap file */ |
| 477 | { |
| 478 | mfp->mf_fd = mch_open((char *)mfp->mf_fname, O_RDWR | O_EXTRA, 0); |
| 479 | if (mfp->mf_fd < 0) |
| 480 | { |
| 481 | /* could not (re)open the swap file, what can we do???? */ |
| 482 | EMSG(_("E301: Oops, lost the swap file!!!")); |
| 483 | return; |
| 484 | } |
| 485 | } |
| 486 | if (!success) |
| 487 | EMSG(_("E302: Could not rename swap file")); |
| 488 | } |
| 489 | |
| 490 | /* |
| 491 | * Open a file for the memfile for all buffers that are not readonly or have |
| 492 | * been modified. |
| 493 | * Used when 'updatecount' changes from zero to non-zero. |
| 494 | */ |
| 495 | void |
| 496 | ml_open_files() |
| 497 | { |
| 498 | buf_T *buf; |
| 499 | |
| 500 | for (buf = firstbuf; buf != NULL; buf = buf->b_next) |
| 501 | if (!buf->b_p_ro || buf->b_changed) |
| 502 | ml_open_file(buf); |
| 503 | } |
| 504 | |
| 505 | /* |
| 506 | * Open a swap file for an existing memfile, if there is no swap file yet. |
| 507 | * If we are unable to find a file name, mf_fname will be NULL |
| 508 | * and the memfile will be in memory only (no recovery possible). |
| 509 | */ |
| 510 | void |
| 511 | ml_open_file(buf) |
| 512 | buf_T *buf; |
| 513 | { |
| 514 | memfile_T *mfp; |
| 515 | char_u *fname; |
| 516 | char_u *dirp; |
| 517 | |
| 518 | mfp = buf->b_ml.ml_mfp; |
| 519 | if (mfp == NULL || mfp->mf_fd >= 0 || !buf->b_p_swf) |
| 520 | return; /* nothing to do */ |
| 521 | |
| 522 | /* |
| 523 | * Try all directories in 'directory' option. |
| 524 | */ |
| 525 | dirp = p_dir; |
| 526 | for (;;) |
| 527 | { |
| 528 | if (*dirp == NUL) |
| 529 | break; |
| 530 | /* There is a small chance that between chosing the swap file name and |
| 531 | * creating it, another Vim creates the file. In that case the |
| 532 | * creation will fail and we will use another directory. */ |
Bram Moolenaar | 8fc061c | 2004-12-29 21:03:02 +0000 | [diff] [blame] | 533 | fname = findswapname(buf, &dirp, NULL); /* allocates fname */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 534 | if (fname == NULL) |
| 535 | continue; |
| 536 | if (mf_open_file(mfp, fname) == OK) /* consumes fname! */ |
| 537 | { |
| 538 | #if defined(MSDOS) || defined(MSWIN) || defined(RISCOS) |
| 539 | /* |
| 540 | * set full pathname for swap file now, because a ":!cd dir" may |
| 541 | * change directory without us knowing it. |
| 542 | */ |
| 543 | mf_fullname(mfp); |
| 544 | #endif |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 545 | ml_upd_block0(buf, FALSE); |
| 546 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 547 | /* Flush block zero, so others can read it */ |
| 548 | if (mf_sync(mfp, MFS_ZERO) == OK) |
| 549 | break; |
| 550 | /* Writing block 0 failed: close the file and try another dir */ |
| 551 | mf_close_file(buf, FALSE); |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | if (mfp->mf_fname == NULL) /* Failed! */ |
| 556 | { |
| 557 | need_wait_return = TRUE; /* call wait_return later */ |
| 558 | ++no_wait_return; |
| 559 | (void)EMSG2(_("E303: Unable to open swap file for \"%s\", recovery impossible"), |
| 560 | buf_spname(buf) != NULL |
| 561 | ? (char_u *)buf_spname(buf) |
| 562 | : buf->b_fname); |
| 563 | --no_wait_return; |
| 564 | } |
| 565 | |
| 566 | /* don't try to open a swap file again */ |
| 567 | buf->b_may_swap = FALSE; |
| 568 | } |
| 569 | |
| 570 | /* |
| 571 | * If still need to create a swap file, and starting to edit a not-readonly |
| 572 | * file, or reading into an existing buffer, create a swap file now. |
| 573 | */ |
| 574 | void |
| 575 | check_need_swap(newfile) |
| 576 | int newfile; /* reading file into new buffer */ |
| 577 | { |
| 578 | if (curbuf->b_may_swap && (!curbuf->b_p_ro || !newfile)) |
| 579 | ml_open_file(curbuf); |
| 580 | } |
| 581 | |
| 582 | /* |
| 583 | * Close memline for buffer 'buf'. |
| 584 | * If 'del_file' is TRUE, delete the swap file |
| 585 | */ |
| 586 | void |
| 587 | ml_close(buf, del_file) |
| 588 | buf_T *buf; |
| 589 | int del_file; |
| 590 | { |
| 591 | if (buf->b_ml.ml_mfp == NULL) /* not open */ |
| 592 | return; |
| 593 | mf_close(buf->b_ml.ml_mfp, del_file); /* close the .swp file */ |
| 594 | if (buf->b_ml.ml_line_lnum != 0 && (buf->b_ml.ml_flags & ML_LINE_DIRTY)) |
| 595 | vim_free(buf->b_ml.ml_line_ptr); |
| 596 | vim_free(buf->b_ml.ml_stack); |
| 597 | #ifdef FEAT_BYTEOFF |
| 598 | vim_free(buf->b_ml.ml_chunksize); |
| 599 | buf->b_ml.ml_chunksize = NULL; |
| 600 | #endif |
| 601 | buf->b_ml.ml_mfp = NULL; |
| 602 | |
| 603 | /* Reset the "recovered" flag, give the ATTENTION prompt the next time |
| 604 | * this buffer is loaded. */ |
| 605 | buf->b_flags &= ~BF_RECOVERED; |
| 606 | } |
| 607 | |
| 608 | /* |
| 609 | * Close all existing memlines and memfiles. |
| 610 | * Only used when exiting. |
| 611 | * When 'del_file' is TRUE, delete the memfiles. |
Bram Moolenaar | 81bf708 | 2005-02-12 14:31:42 +0000 | [diff] [blame] | 612 | * But don't delete files that were ":preserve"d when we are POSIX compatible. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 613 | */ |
| 614 | void |
| 615 | ml_close_all(del_file) |
| 616 | int del_file; |
| 617 | { |
| 618 | buf_T *buf; |
| 619 | |
| 620 | for (buf = firstbuf; buf != NULL; buf = buf->b_next) |
Bram Moolenaar | 81bf708 | 2005-02-12 14:31:42 +0000 | [diff] [blame] | 621 | ml_close(buf, del_file && ((buf->b_flags & BF_PRESERVED) == 0 |
| 622 | || vim_strchr(p_cpo, CPO_PRESERVE) == NULL)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 623 | #ifdef TEMPDIRNAMES |
| 624 | vim_deltempdir(); /* delete created temp directory */ |
| 625 | #endif |
| 626 | } |
| 627 | |
| 628 | /* |
| 629 | * Close all memfiles for not modified buffers. |
| 630 | * Only use just before exiting! |
| 631 | */ |
| 632 | void |
| 633 | ml_close_notmod() |
| 634 | { |
| 635 | buf_T *buf; |
| 636 | |
| 637 | for (buf = firstbuf; buf != NULL; buf = buf->b_next) |
| 638 | if (!bufIsChanged(buf)) |
| 639 | ml_close(buf, TRUE); /* close all not-modified buffers */ |
| 640 | } |
| 641 | |
| 642 | /* |
| 643 | * Update the timestamp in the .swp file. |
| 644 | * Used when the file has been written. |
| 645 | */ |
| 646 | void |
| 647 | ml_timestamp(buf) |
| 648 | buf_T *buf; |
| 649 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 650 | ml_upd_block0(buf, TRUE); |
| 651 | } |
| 652 | |
| 653 | /* |
| 654 | * Update the timestamp or the B0_SAME_DIR flag of the .swp file. |
| 655 | */ |
| 656 | static void |
| 657 | ml_upd_block0(buf, setfname) |
| 658 | buf_T *buf; |
| 659 | int setfname; |
| 660 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 661 | memfile_T *mfp; |
| 662 | bhdr_T *hp; |
| 663 | ZERO_BL *b0p; |
| 664 | |
| 665 | mfp = buf->b_ml.ml_mfp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 666 | if (mfp == NULL || (hp = mf_get(mfp, (blocknr_T)0, 1)) == NULL) |
| 667 | return; |
| 668 | b0p = (ZERO_BL *)(hp->bh_data); |
| 669 | if (b0p->b0_id[0] != BLOCK0_ID0 || b0p->b0_id[1] != BLOCK0_ID1) |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 670 | EMSG(_("E304: ml_upd_block0(): Didn't get block 0??")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 671 | else |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 672 | { |
| 673 | if (setfname) |
| 674 | set_b0_fname(b0p, buf); |
| 675 | else |
| 676 | set_b0_dir_flag(b0p, buf); |
| 677 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 678 | mf_put(mfp, hp, TRUE, FALSE); |
| 679 | } |
| 680 | |
| 681 | /* |
| 682 | * Write file name and timestamp into block 0 of a swap file. |
| 683 | * Also set buf->b_mtime. |
| 684 | * Don't use NameBuff[]!!! |
| 685 | */ |
| 686 | static void |
| 687 | set_b0_fname(b0p, buf) |
| 688 | ZERO_BL *b0p; |
| 689 | buf_T *buf; |
| 690 | { |
| 691 | struct stat st; |
| 692 | |
| 693 | if (buf->b_ffname == NULL) |
| 694 | b0p->b0_fname[0] = NUL; |
| 695 | else |
| 696 | { |
| 697 | #if defined(MSDOS) || defined(MSWIN) || defined(AMIGA) || defined(RISCOS) |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 698 | /* Systems that cannot translate "~user" back into a path: copy the |
| 699 | * file name unmodified. Do use slashes instead of backslashes for |
| 700 | * portability. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 701 | STRNCPY(b0p->b0_fname, buf->b_ffname, B0_FNAME_SIZE); |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 702 | b0p->b0_fname[B0_FNAME_SIZE - 1] = NUL; |
| 703 | # ifdef BACKSLASH_IN_FILENAME |
| 704 | forward_slash(b0p->b0_fname); |
| 705 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 706 | #else |
| 707 | size_t flen, ulen; |
| 708 | char_u uname[B0_UNAME_SIZE]; |
| 709 | |
| 710 | /* |
| 711 | * For a file under the home directory of the current user, we try to |
| 712 | * replace the home directory path with "~user". This helps when |
| 713 | * editing the same file on different machines over a network. |
| 714 | * First replace home dir path with "~/" with home_replace(). |
| 715 | * Then insert the user name to get "~user/". |
| 716 | */ |
| 717 | home_replace(NULL, buf->b_ffname, b0p->b0_fname, B0_FNAME_SIZE, TRUE); |
| 718 | if (b0p->b0_fname[0] == '~') |
| 719 | { |
| 720 | flen = STRLEN(b0p->b0_fname); |
| 721 | /* If there is no user name or it is too long, don't use "~/" */ |
| 722 | if (get_user_name(uname, B0_UNAME_SIZE) == FAIL |
| 723 | || (ulen = STRLEN(uname)) + flen > B0_FNAME_SIZE - 1) |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 724 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 725 | STRNCPY(b0p->b0_fname, buf->b_ffname, B0_FNAME_SIZE); |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 726 | b0p->b0_fname[B0_FNAME_SIZE - 1] = NUL; |
| 727 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 728 | else |
| 729 | { |
| 730 | mch_memmove(b0p->b0_fname + ulen + 1, b0p->b0_fname + 1, flen); |
| 731 | mch_memmove(b0p->b0_fname + 1, uname, ulen); |
| 732 | } |
| 733 | } |
| 734 | #endif |
| 735 | if (mch_stat((char *)buf->b_ffname, &st) >= 0) |
| 736 | { |
| 737 | long_to_char((long)st.st_mtime, b0p->b0_mtime); |
| 738 | #ifdef CHECK_INODE |
| 739 | long_to_char((long)st.st_ino, b0p->b0_ino); |
| 740 | #endif |
| 741 | buf_store_time(buf, &st, buf->b_ffname); |
| 742 | buf->b_mtime_read = buf->b_mtime; |
| 743 | } |
| 744 | else |
| 745 | { |
| 746 | long_to_char(0L, b0p->b0_mtime); |
| 747 | #ifdef CHECK_INODE |
| 748 | long_to_char(0L, b0p->b0_ino); |
| 749 | #endif |
| 750 | buf->b_mtime = 0; |
| 751 | buf->b_mtime_read = 0; |
| 752 | buf->b_orig_size = 0; |
| 753 | buf->b_orig_mode = 0; |
| 754 | } |
| 755 | } |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 756 | |
| 757 | #ifdef FEAT_MBYTE |
| 758 | /* Also add the 'fileencoding' if there is room. */ |
| 759 | add_b0_fenc(b0p, curbuf); |
| 760 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | /* |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 764 | * Update the B0_SAME_DIR flag of the swap file. It's set if the file and the |
| 765 | * swapfile for "buf" are in the same directory. |
| 766 | * This is fail safe: if we are not sure the directories are equal the flag is |
| 767 | * not set. |
| 768 | */ |
| 769 | static void |
| 770 | set_b0_dir_flag(b0p, buf) |
| 771 | ZERO_BL *b0p; |
| 772 | buf_T *buf; |
| 773 | { |
| 774 | if (same_directory(buf->b_ml.ml_mfp->mf_fname, buf->b_ffname)) |
| 775 | b0p->b0_flags |= B0_SAME_DIR; |
| 776 | else |
| 777 | b0p->b0_flags &= ~B0_SAME_DIR; |
| 778 | } |
| 779 | |
| 780 | #ifdef FEAT_MBYTE |
| 781 | /* |
| 782 | * When there is room, add the 'fileencoding' to block zero. |
| 783 | */ |
| 784 | static void |
| 785 | add_b0_fenc(b0p, buf) |
| 786 | ZERO_BL *b0p; |
| 787 | buf_T *buf; |
| 788 | { |
| 789 | int n; |
| 790 | |
| 791 | n = STRLEN(buf->b_p_fenc); |
| 792 | if (STRLEN(b0p->b0_fname) + n + 1 > B0_FNAME_SIZE) |
| 793 | b0p->b0_flags &= ~B0_HAS_FENC; |
| 794 | else |
| 795 | { |
| 796 | mch_memmove((char *)b0p->b0_fname + B0_FNAME_SIZE - n, |
| 797 | (char *)buf->b_p_fenc, (size_t)n); |
| 798 | *(b0p->b0_fname + B0_FNAME_SIZE - n - 1) = NUL; |
| 799 | b0p->b0_flags |= B0_HAS_FENC; |
| 800 | } |
| 801 | } |
| 802 | #endif |
| 803 | |
| 804 | |
| 805 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 806 | * try to recover curbuf from the .swp file |
| 807 | */ |
| 808 | void |
| 809 | ml_recover() |
| 810 | { |
| 811 | buf_T *buf = NULL; |
| 812 | memfile_T *mfp = NULL; |
| 813 | char_u *fname; |
| 814 | bhdr_T *hp = NULL; |
| 815 | ZERO_BL *b0p; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 816 | int b0_ff; |
| 817 | char_u *b0_fenc = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 818 | PTR_BL *pp; |
| 819 | DATA_BL *dp; |
| 820 | infoptr_T *ip; |
| 821 | blocknr_T bnum; |
| 822 | int page_count; |
| 823 | struct stat org_stat, swp_stat; |
| 824 | int len; |
| 825 | int directly; |
| 826 | linenr_T lnum; |
| 827 | char_u *p; |
| 828 | int i; |
| 829 | long error; |
| 830 | int cannot_open; |
| 831 | linenr_T line_count; |
| 832 | int has_error; |
| 833 | int idx; |
| 834 | int top; |
| 835 | int txt_start; |
| 836 | off_t size; |
| 837 | int called_from_main; |
| 838 | int serious_error = TRUE; |
| 839 | long mtime; |
| 840 | int attr; |
| 841 | |
| 842 | recoverymode = TRUE; |
| 843 | called_from_main = (curbuf->b_ml.ml_mfp == NULL); |
| 844 | attr = hl_attr(HLF_E); |
| 845 | /* |
| 846 | * If the file name ends in ".sw?" we use it directly. |
| 847 | * Otherwise a search is done to find the swap file(s). |
| 848 | */ |
| 849 | fname = curbuf->b_fname; |
| 850 | if (fname == NULL) /* When there is no file name */ |
| 851 | fname = (char_u *)""; |
| 852 | len = (int)STRLEN(fname); |
| 853 | if (len >= 4 && |
| 854 | #if defined(VMS) || defined(RISCOS) |
| 855 | STRNICMP(fname + len - 4, "_sw" , 3) |
| 856 | #else |
| 857 | STRNICMP(fname + len - 4, ".sw" , 3) |
| 858 | #endif |
| 859 | == 0) |
| 860 | { |
| 861 | directly = TRUE; |
| 862 | fname = vim_strsave(fname); /* make a copy for mf_open() */ |
| 863 | } |
| 864 | else |
| 865 | { |
| 866 | directly = FALSE; |
| 867 | |
| 868 | /* count the number of matching swap files */ |
| 869 | len = recover_names(&fname, FALSE, 0); |
| 870 | if (len == 0) /* no swap files found */ |
| 871 | { |
| 872 | EMSG2(_("E305: No swap file found for %s"), fname); |
| 873 | goto theend; |
| 874 | } |
| 875 | if (len == 1) /* one swap file found, use it */ |
| 876 | i = 1; |
| 877 | else /* several swap files found, choose */ |
| 878 | { |
| 879 | /* list the names of the swap files */ |
| 880 | (void)recover_names(&fname, TRUE, 0); |
| 881 | msg_putchar('\n'); |
| 882 | MSG_PUTS(_("Enter number of swap file to use (0 to quit): ")); |
| 883 | i = get_number(FALSE); |
| 884 | if (i < 1 || i > len) |
| 885 | goto theend; |
| 886 | } |
| 887 | /* get the swap file name that will be used */ |
| 888 | (void)recover_names(&fname, FALSE, i); |
| 889 | } |
| 890 | if (fname == NULL) |
| 891 | goto theend; /* out of memory */ |
| 892 | |
| 893 | /* When called from main() still need to initialize storage structure */ |
| 894 | if (called_from_main && ml_open() == FAIL) |
| 895 | getout(1); |
| 896 | |
| 897 | /* |
| 898 | * allocate a buffer structure (only the memline in it is really used) |
| 899 | */ |
| 900 | buf = (buf_T *)alloc((unsigned)sizeof(buf_T)); |
| 901 | if (buf == NULL) |
| 902 | { |
| 903 | vim_free(fname); |
| 904 | goto theend; |
| 905 | } |
| 906 | |
| 907 | /* |
| 908 | * init fields in memline struct |
| 909 | */ |
| 910 | buf->b_ml.ml_stack_size = 0; /* no stack yet */ |
| 911 | buf->b_ml.ml_stack = NULL; /* no stack yet */ |
| 912 | buf->b_ml.ml_stack_top = 0; /* nothing in the stack */ |
| 913 | buf->b_ml.ml_line_lnum = 0; /* no cached line */ |
| 914 | buf->b_ml.ml_locked = NULL; /* no locked block */ |
| 915 | buf->b_ml.ml_flags = 0; |
| 916 | |
| 917 | /* |
| 918 | * open the memfile from the old swap file |
| 919 | */ |
| 920 | p = vim_strsave(fname); /* save fname for the message |
| 921 | (mf_open() may free fname) */ |
| 922 | mfp = mf_open(fname, O_RDONLY); /* consumes fname! */ |
| 923 | if (mfp == NULL || mfp->mf_fd < 0) |
| 924 | { |
| 925 | if (p != NULL) |
| 926 | { |
| 927 | EMSG2(_("E306: Cannot open %s"), p); |
| 928 | vim_free(p); |
| 929 | } |
| 930 | goto theend; |
| 931 | } |
| 932 | vim_free(p); |
| 933 | buf->b_ml.ml_mfp = mfp; |
| 934 | |
| 935 | /* |
| 936 | * The page size set in mf_open() might be different from the page size |
| 937 | * used in the swap file, we must get it from block 0. But to read block |
| 938 | * 0 we need a page size. Use the minimal size for block 0 here, it will |
| 939 | * be set to the real value below. |
| 940 | */ |
| 941 | mfp->mf_page_size = MIN_SWAP_PAGE_SIZE; |
| 942 | |
| 943 | /* |
| 944 | * try to read block 0 |
| 945 | */ |
| 946 | if ((hp = mf_get(mfp, (blocknr_T)0, 1)) == NULL) |
| 947 | { |
| 948 | msg_start(); |
| 949 | MSG_PUTS_ATTR(_("Unable to read block 0 from "), attr | MSG_HIST); |
| 950 | msg_outtrans_attr(mfp->mf_fname, attr | MSG_HIST); |
| 951 | MSG_PUTS_ATTR( |
| 952 | _("\nMaybe no changes were made or Vim did not update the swap file."), |
| 953 | attr | MSG_HIST); |
| 954 | msg_end(); |
| 955 | goto theend; |
| 956 | } |
| 957 | b0p = (ZERO_BL *)(hp->bh_data); |
| 958 | if (STRNCMP(b0p->b0_version, "VIM 3.0", 7) == 0) |
| 959 | { |
| 960 | msg_start(); |
| 961 | msg_outtrans_attr(mfp->mf_fname, MSG_HIST); |
| 962 | MSG_PUTS_ATTR(_(" cannot be used with this version of Vim.\n"), |
| 963 | MSG_HIST); |
| 964 | MSG_PUTS_ATTR(_("Use Vim version 3.0.\n"), MSG_HIST); |
| 965 | msg_end(); |
| 966 | goto theend; |
| 967 | } |
| 968 | if (b0p->b0_id[0] != BLOCK0_ID0 || b0p->b0_id[1] != BLOCK0_ID1) |
| 969 | { |
| 970 | EMSG2(_("E307: %s does not look like a Vim swap file"), mfp->mf_fname); |
| 971 | goto theend; |
| 972 | } |
| 973 | if (b0_magic_wrong(b0p)) |
| 974 | { |
| 975 | msg_start(); |
| 976 | msg_outtrans_attr(mfp->mf_fname, attr | MSG_HIST); |
| 977 | #if defined(MSDOS) || defined(MSWIN) |
| 978 | if (STRNCMP(b0p->b0_hname, "PC ", 3) == 0) |
| 979 | MSG_PUTS_ATTR(_(" cannot be used with this version of Vim.\n"), |
| 980 | attr | MSG_HIST); |
| 981 | else |
| 982 | #endif |
| 983 | MSG_PUTS_ATTR(_(" cannot be used on this computer.\n"), |
| 984 | attr | MSG_HIST); |
| 985 | MSG_PUTS_ATTR(_("The file was created on "), attr | MSG_HIST); |
| 986 | /* avoid going past the end of a currupted hostname */ |
| 987 | b0p->b0_fname[0] = NUL; |
| 988 | MSG_PUTS_ATTR(b0p->b0_hname, attr | MSG_HIST); |
| 989 | MSG_PUTS_ATTR(_(",\nor the file has been damaged."), attr | MSG_HIST); |
| 990 | msg_end(); |
| 991 | goto theend; |
| 992 | } |
| 993 | /* |
| 994 | * If we guessed the wrong page size, we have to recalculate the |
| 995 | * highest block number in the file. |
| 996 | */ |
| 997 | if (mfp->mf_page_size != (unsigned)char_to_long(b0p->b0_page_size)) |
| 998 | { |
| 999 | mf_new_page_size(mfp, (unsigned)char_to_long(b0p->b0_page_size)); |
| 1000 | if ((size = lseek(mfp->mf_fd, (off_t)0L, SEEK_END)) <= 0) |
| 1001 | mfp->mf_blocknr_max = 0; /* no file or empty file */ |
| 1002 | else |
| 1003 | mfp->mf_blocknr_max = (blocknr_T)(size / mfp->mf_page_size); |
| 1004 | mfp->mf_infile_count = mfp->mf_blocknr_max; |
| 1005 | } |
| 1006 | |
| 1007 | /* |
| 1008 | * If .swp file name given directly, use name from swap file for buffer. |
| 1009 | */ |
| 1010 | if (directly) |
| 1011 | { |
| 1012 | expand_env(b0p->b0_fname, NameBuff, MAXPATHL); |
| 1013 | if (setfname(curbuf, NameBuff, NULL, TRUE) == FAIL) |
| 1014 | goto theend; |
| 1015 | } |
| 1016 | |
| 1017 | home_replace(NULL, mfp->mf_fname, NameBuff, MAXPATHL, TRUE); |
| 1018 | msg_str((char_u *)_("Using swap file \"%s\""), NameBuff); |
| 1019 | |
| 1020 | if (buf_spname(curbuf) != NULL) |
| 1021 | STRCPY(NameBuff, buf_spname(curbuf)); |
| 1022 | else |
| 1023 | home_replace(NULL, curbuf->b_ffname, NameBuff, MAXPATHL, TRUE); |
| 1024 | msg_str((char_u *)_("Original file \"%s\""), NameBuff); |
| 1025 | msg_putchar('\n'); |
| 1026 | |
| 1027 | /* |
| 1028 | * check date of swap file and original file |
| 1029 | */ |
| 1030 | mtime = char_to_long(b0p->b0_mtime); |
| 1031 | if (curbuf->b_ffname != NULL |
| 1032 | && mch_stat((char *)curbuf->b_ffname, &org_stat) != -1 |
| 1033 | && ((mch_stat((char *)mfp->mf_fname, &swp_stat) != -1 |
| 1034 | && org_stat.st_mtime > swp_stat.st_mtime) |
| 1035 | || org_stat.st_mtime != mtime)) |
| 1036 | { |
| 1037 | EMSG(_("E308: Warning: Original file may have been changed")); |
| 1038 | } |
| 1039 | out_flush(); |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1040 | |
| 1041 | /* Get the 'fileformat' and 'fileencoding' from block zero. */ |
| 1042 | b0_ff = (b0p->b0_flags & B0_FF_MASK); |
| 1043 | if (b0p->b0_flags & B0_HAS_FENC) |
| 1044 | { |
| 1045 | for (p = b0p->b0_fname + B0_FNAME_SIZE; |
| 1046 | p > b0p->b0_fname && p[-1] != NUL; --p) |
| 1047 | ; |
| 1048 | b0_fenc = vim_strnsave(p, b0p->b0_fname + B0_FNAME_SIZE - p); |
| 1049 | } |
| 1050 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1051 | mf_put(mfp, hp, FALSE, FALSE); /* release block 0 */ |
| 1052 | hp = NULL; |
| 1053 | |
| 1054 | /* |
| 1055 | * Now that we are sure that the file is going to be recovered, clear the |
| 1056 | * contents of the current buffer. |
| 1057 | */ |
| 1058 | while (!(curbuf->b_ml.ml_flags & ML_EMPTY)) |
| 1059 | ml_delete((linenr_T)1, FALSE); |
| 1060 | |
| 1061 | /* |
| 1062 | * Try reading the original file to obtain the values of 'fileformat', |
| 1063 | * 'fileencoding', etc. Ignore errors. The text itself is not used. |
| 1064 | */ |
| 1065 | if (curbuf->b_ffname != NULL) |
| 1066 | { |
| 1067 | (void)readfile(curbuf->b_ffname, NULL, (linenr_T)0, |
| 1068 | (linenr_T)0, (linenr_T)MAXLNUM, NULL, READ_NEW); |
| 1069 | while (!(curbuf->b_ml.ml_flags & ML_EMPTY)) |
| 1070 | ml_delete((linenr_T)1, FALSE); |
| 1071 | } |
| 1072 | |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1073 | /* Use the 'fileformat' and 'fileencoding' as stored in the swap file. */ |
| 1074 | if (b0_ff != 0) |
| 1075 | set_fileformat(b0_ff - 1, OPT_LOCAL); |
| 1076 | if (b0_fenc != NULL) |
| 1077 | { |
| 1078 | set_option_value((char_u *)"fenc", 0L, b0_fenc, OPT_LOCAL); |
| 1079 | vim_free(b0_fenc); |
| 1080 | } |
| 1081 | unchanged(curbuf, TRUE); |
| 1082 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1083 | bnum = 1; /* start with block 1 */ |
| 1084 | page_count = 1; /* which is 1 page */ |
| 1085 | lnum = 0; /* append after line 0 in curbuf */ |
| 1086 | line_count = 0; |
| 1087 | idx = 0; /* start with first index in block 1 */ |
| 1088 | error = 0; |
| 1089 | buf->b_ml.ml_stack_top = 0; |
| 1090 | buf->b_ml.ml_stack = NULL; |
| 1091 | buf->b_ml.ml_stack_size = 0; /* no stack yet */ |
| 1092 | |
| 1093 | if (curbuf->b_ffname == NULL) |
| 1094 | cannot_open = TRUE; |
| 1095 | else |
| 1096 | cannot_open = FALSE; |
| 1097 | |
| 1098 | serious_error = FALSE; |
| 1099 | for ( ; !got_int; line_breakcheck()) |
| 1100 | { |
| 1101 | if (hp != NULL) |
| 1102 | mf_put(mfp, hp, FALSE, FALSE); /* release previous block */ |
| 1103 | |
| 1104 | /* |
| 1105 | * get block |
| 1106 | */ |
| 1107 | if ((hp = mf_get(mfp, (blocknr_T)bnum, page_count)) == NULL) |
| 1108 | { |
| 1109 | if (bnum == 1) |
| 1110 | { |
| 1111 | EMSG2(_("E309: Unable to read block 1 from %s"), mfp->mf_fname); |
| 1112 | goto theend; |
| 1113 | } |
| 1114 | ++error; |
| 1115 | ml_append(lnum++, (char_u *)_("???MANY LINES MISSING"), |
| 1116 | (colnr_T)0, TRUE); |
| 1117 | } |
| 1118 | else /* there is a block */ |
| 1119 | { |
| 1120 | pp = (PTR_BL *)(hp->bh_data); |
| 1121 | if (pp->pb_id == PTR_ID) /* it is a pointer block */ |
| 1122 | { |
| 1123 | /* check line count when using pointer block first time */ |
| 1124 | if (idx == 0 && line_count != 0) |
| 1125 | { |
| 1126 | for (i = 0; i < (int)pp->pb_count; ++i) |
| 1127 | line_count -= pp->pb_pointer[i].pe_line_count; |
| 1128 | if (line_count != 0) |
| 1129 | { |
| 1130 | ++error; |
| 1131 | ml_append(lnum++, (char_u *)_("???LINE COUNT WRONG"), |
| 1132 | (colnr_T)0, TRUE); |
| 1133 | } |
| 1134 | } |
| 1135 | |
| 1136 | if (pp->pb_count == 0) |
| 1137 | { |
| 1138 | ml_append(lnum++, (char_u *)_("???EMPTY BLOCK"), |
| 1139 | (colnr_T)0, TRUE); |
| 1140 | ++error; |
| 1141 | } |
| 1142 | else if (idx < (int)pp->pb_count) /* go a block deeper */ |
| 1143 | { |
| 1144 | if (pp->pb_pointer[idx].pe_bnum < 0) |
| 1145 | { |
| 1146 | /* |
| 1147 | * Data block with negative block number. |
| 1148 | * Try to read lines from the original file. |
| 1149 | * This is slow, but it works. |
| 1150 | */ |
| 1151 | if (!cannot_open) |
| 1152 | { |
| 1153 | line_count = pp->pb_pointer[idx].pe_line_count; |
| 1154 | if (readfile(curbuf->b_ffname, NULL, lnum, |
| 1155 | pp->pb_pointer[idx].pe_old_lnum - 1, |
| 1156 | line_count, NULL, 0) == FAIL) |
| 1157 | cannot_open = TRUE; |
| 1158 | else |
| 1159 | lnum += line_count; |
| 1160 | } |
| 1161 | if (cannot_open) |
| 1162 | { |
| 1163 | ++error; |
| 1164 | ml_append(lnum++, (char_u *)_("???LINES MISSING"), |
| 1165 | (colnr_T)0, TRUE); |
| 1166 | } |
| 1167 | ++idx; /* get same block again for next index */ |
| 1168 | continue; |
| 1169 | } |
| 1170 | |
| 1171 | /* |
| 1172 | * going one block deeper in the tree |
| 1173 | */ |
| 1174 | if ((top = ml_add_stack(buf)) < 0) /* new entry in stack */ |
| 1175 | { |
| 1176 | ++error; |
| 1177 | break; /* out of memory */ |
| 1178 | } |
| 1179 | ip = &(buf->b_ml.ml_stack[top]); |
| 1180 | ip->ip_bnum = bnum; |
| 1181 | ip->ip_index = idx; |
| 1182 | |
| 1183 | bnum = pp->pb_pointer[idx].pe_bnum; |
| 1184 | line_count = pp->pb_pointer[idx].pe_line_count; |
| 1185 | page_count = pp->pb_pointer[idx].pe_page_count; |
| 1186 | continue; |
| 1187 | } |
| 1188 | } |
| 1189 | else /* not a pointer block */ |
| 1190 | { |
| 1191 | dp = (DATA_BL *)(hp->bh_data); |
| 1192 | if (dp->db_id != DATA_ID) /* block id wrong */ |
| 1193 | { |
| 1194 | if (bnum == 1) |
| 1195 | { |
| 1196 | EMSG2(_("E310: Block 1 ID wrong (%s not a .swp file?)"), |
| 1197 | mfp->mf_fname); |
| 1198 | goto theend; |
| 1199 | } |
| 1200 | ++error; |
| 1201 | ml_append(lnum++, (char_u *)_("???BLOCK MISSING"), |
| 1202 | (colnr_T)0, TRUE); |
| 1203 | } |
| 1204 | else |
| 1205 | { |
| 1206 | /* |
| 1207 | * it is a data block |
| 1208 | * Append all the lines in this block |
| 1209 | */ |
| 1210 | has_error = FALSE; |
| 1211 | /* |
| 1212 | * check length of block |
| 1213 | * if wrong, use length in pointer block |
| 1214 | */ |
| 1215 | if (page_count * mfp->mf_page_size != dp->db_txt_end) |
| 1216 | { |
| 1217 | ml_append(lnum++, (char_u *)_("??? from here until ???END lines may be messed up"), |
| 1218 | (colnr_T)0, TRUE); |
| 1219 | ++error; |
| 1220 | has_error = TRUE; |
| 1221 | dp->db_txt_end = page_count * mfp->mf_page_size; |
| 1222 | } |
| 1223 | |
| 1224 | /* make sure there is a NUL at the end of the block */ |
| 1225 | *((char_u *)dp + dp->db_txt_end - 1) = NUL; |
| 1226 | |
| 1227 | /* |
| 1228 | * check number of lines in block |
| 1229 | * if wrong, use count in data block |
| 1230 | */ |
| 1231 | if (line_count != dp->db_line_count) |
| 1232 | { |
| 1233 | ml_append(lnum++, (char_u *)_("??? from here until ???END lines may have been inserted/deleted"), |
| 1234 | (colnr_T)0, TRUE); |
| 1235 | ++error; |
| 1236 | has_error = TRUE; |
| 1237 | } |
| 1238 | |
| 1239 | for (i = 0; i < dp->db_line_count; ++i) |
| 1240 | { |
| 1241 | txt_start = (dp->db_index[i] & DB_INDEX_MASK); |
| 1242 | if (txt_start <= HEADER_SIZE |
| 1243 | || txt_start >= (int)dp->db_txt_end) |
| 1244 | { |
| 1245 | p = (char_u *)"???"; |
| 1246 | ++error; |
| 1247 | } |
| 1248 | else |
| 1249 | p = (char_u *)dp + txt_start; |
| 1250 | ml_append(lnum++, p, (colnr_T)0, TRUE); |
| 1251 | } |
| 1252 | if (has_error) |
| 1253 | ml_append(lnum++, (char_u *)_("???END"), (colnr_T)0, TRUE); |
| 1254 | } |
| 1255 | } |
| 1256 | } |
| 1257 | |
| 1258 | if (buf->b_ml.ml_stack_top == 0) /* finished */ |
| 1259 | break; |
| 1260 | |
| 1261 | /* |
| 1262 | * go one block up in the tree |
| 1263 | */ |
| 1264 | ip = &(buf->b_ml.ml_stack[--(buf->b_ml.ml_stack_top)]); |
| 1265 | bnum = ip->ip_bnum; |
| 1266 | idx = ip->ip_index + 1; /* go to next index */ |
| 1267 | page_count = 1; |
| 1268 | } |
| 1269 | |
| 1270 | /* |
| 1271 | * The dummy line from the empty buffer will now be after the last line in |
| 1272 | * the buffer. Delete it. |
| 1273 | */ |
| 1274 | ml_delete(curbuf->b_ml.ml_line_count, FALSE); |
| 1275 | curbuf->b_flags |= BF_RECOVERED; |
| 1276 | |
| 1277 | recoverymode = FALSE; |
| 1278 | if (got_int) |
| 1279 | EMSG(_("E311: Recovery Interrupted")); |
| 1280 | else if (error) |
| 1281 | { |
| 1282 | ++no_wait_return; |
| 1283 | MSG(">>>>>>>>>>>>>"); |
| 1284 | EMSG(_("E312: Errors detected while recovering; look for lines starting with ???")); |
| 1285 | --no_wait_return; |
| 1286 | MSG(_("See \":help E312\" for more information.")); |
| 1287 | MSG(">>>>>>>>>>>>>"); |
| 1288 | } |
| 1289 | else |
| 1290 | { |
| 1291 | MSG(_("Recovery completed. You should check if everything is OK.")); |
| 1292 | MSG_PUTS(_("\n(You might want to write out this file under another name\n")); |
| 1293 | MSG_PUTS(_("and run diff with the original file to check for changes)\n")); |
| 1294 | MSG_PUTS(_("Delete the .swp file afterwards.\n\n")); |
| 1295 | cmdline_row = msg_row; |
| 1296 | } |
| 1297 | redraw_curbuf_later(NOT_VALID); |
| 1298 | |
| 1299 | theend: |
| 1300 | recoverymode = FALSE; |
| 1301 | if (mfp != NULL) |
| 1302 | { |
| 1303 | if (hp != NULL) |
| 1304 | mf_put(mfp, hp, FALSE, FALSE); |
| 1305 | mf_close(mfp, FALSE); /* will also vim_free(mfp->mf_fname) */ |
| 1306 | } |
| 1307 | vim_free(buf); |
| 1308 | if (serious_error && called_from_main) |
| 1309 | ml_close(curbuf, TRUE); |
| 1310 | #ifdef FEAT_AUTOCMD |
| 1311 | else |
| 1312 | { |
| 1313 | apply_autocmds(EVENT_BUFREADPOST, NULL, curbuf->b_fname, FALSE, curbuf); |
| 1314 | apply_autocmds(EVENT_BUFWINENTER, NULL, curbuf->b_fname, FALSE, curbuf); |
| 1315 | } |
| 1316 | #endif |
| 1317 | return; |
| 1318 | } |
| 1319 | |
| 1320 | /* |
| 1321 | * Find the names of swap files in current directory and the directory given |
| 1322 | * with the 'directory' option. |
| 1323 | * |
| 1324 | * Used to: |
| 1325 | * - list the swap files for "vim -r" |
| 1326 | * - count the number of swap files when recovering |
| 1327 | * - list the swap files when recovering |
| 1328 | * - find the name of the n'th swap file when recovering |
| 1329 | */ |
| 1330 | int |
| 1331 | recover_names(fname, list, nr) |
| 1332 | char_u **fname; /* base for swap file name */ |
| 1333 | int list; /* when TRUE, list the swap file names */ |
| 1334 | int nr; /* when non-zero, return nr'th swap file name */ |
| 1335 | { |
| 1336 | int num_names; |
| 1337 | char_u *(names[6]); |
| 1338 | char_u *tail; |
| 1339 | char_u *p; |
| 1340 | int num_files; |
| 1341 | int file_count = 0; |
| 1342 | char_u **files; |
| 1343 | int i; |
| 1344 | char_u *dirp; |
| 1345 | char_u *dir_name; |
| 1346 | |
| 1347 | if (list) |
| 1348 | { |
| 1349 | /* use msg() to start the scrolling properly */ |
| 1350 | msg((char_u *)_("Swap files found:")); |
| 1351 | msg_putchar('\n'); |
| 1352 | } |
| 1353 | |
| 1354 | /* |
| 1355 | * Do the loop for every directory in 'directory'. |
| 1356 | * First allocate some memory to put the directory name in. |
| 1357 | */ |
| 1358 | dir_name = alloc((unsigned)STRLEN(p_dir) + 1); |
| 1359 | dirp = p_dir; |
| 1360 | while (dir_name != NULL && *dirp) |
| 1361 | { |
| 1362 | /* |
| 1363 | * Isolate a directory name from *dirp and put it in dir_name (we know |
| 1364 | * it is large enough, so use 31000 for length). |
| 1365 | * Advance dirp to next directory name. |
| 1366 | */ |
| 1367 | (void)copy_option_part(&dirp, dir_name, 31000, ","); |
| 1368 | |
| 1369 | if (dir_name[0] == '.' && dir_name[1] == NUL) /* check current dir */ |
| 1370 | { |
| 1371 | if (fname == NULL || *fname == NULL) |
| 1372 | { |
| 1373 | #ifdef VMS |
| 1374 | names[0] = vim_strsave((char_u *)"*_sw%"); |
| 1375 | #else |
| 1376 | # ifdef RISCOS |
| 1377 | names[0] = vim_strsave((char_u *)"*_sw#"); |
| 1378 | # else |
| 1379 | names[0] = vim_strsave((char_u *)"*.sw?"); |
| 1380 | # endif |
| 1381 | #endif |
| 1382 | #ifdef UNIX |
| 1383 | /* for Unix names starting with a dot are special */ |
| 1384 | names[1] = vim_strsave((char_u *)".*.sw?"); |
| 1385 | names[2] = vim_strsave((char_u *)".sw?"); |
| 1386 | num_names = 3; |
| 1387 | #else |
| 1388 | # ifdef VMS |
| 1389 | names[1] = vim_strsave((char_u *)".*_sw%"); |
| 1390 | num_names = 2; |
| 1391 | # else |
| 1392 | num_names = 1; |
| 1393 | # endif |
| 1394 | #endif |
| 1395 | } |
| 1396 | else |
| 1397 | num_names = recov_file_names(names, *fname, TRUE); |
| 1398 | } |
| 1399 | else /* check directory dir_name */ |
| 1400 | { |
| 1401 | if (fname == NULL || *fname == NULL) |
| 1402 | { |
| 1403 | #ifdef VMS |
| 1404 | names[0] = concat_fnames(dir_name, (char_u *)"*_sw%", TRUE); |
| 1405 | #else |
| 1406 | # ifdef RISCOS |
| 1407 | names[0] = concat_fnames(dir_name, (char_u *)"*_sw#", TRUE); |
| 1408 | # else |
| 1409 | names[0] = concat_fnames(dir_name, (char_u *)"*.sw?", TRUE); |
| 1410 | # endif |
| 1411 | #endif |
| 1412 | #ifdef UNIX |
| 1413 | /* for Unix names starting with a dot are special */ |
| 1414 | names[1] = concat_fnames(dir_name, (char_u *)".*.sw?", TRUE); |
| 1415 | names[2] = concat_fnames(dir_name, (char_u *)".sw?", TRUE); |
| 1416 | num_names = 3; |
| 1417 | #else |
| 1418 | # ifdef VMS |
| 1419 | names[1] = concat_fnames(dir_name, (char_u *)".*_sw%", TRUE); |
| 1420 | num_names = 2; |
| 1421 | # else |
| 1422 | num_names = 1; |
| 1423 | # endif |
| 1424 | #endif |
| 1425 | } |
| 1426 | else |
| 1427 | { |
| 1428 | #if defined(UNIX) || defined(WIN3264) |
| 1429 | p = dir_name + STRLEN(dir_name); |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1430 | if (after_pathsep(dir_name, p) && p[-1] == p[-2]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1431 | { |
| 1432 | /* Ends with '//', Use Full path for swap name */ |
| 1433 | tail = make_percent_swname(dir_name, *fname); |
| 1434 | } |
| 1435 | else |
| 1436 | #endif |
| 1437 | { |
| 1438 | tail = gettail(*fname); |
| 1439 | tail = concat_fnames(dir_name, tail, TRUE); |
| 1440 | } |
| 1441 | if (tail == NULL) |
| 1442 | num_names = 0; |
| 1443 | else |
| 1444 | { |
| 1445 | num_names = recov_file_names(names, tail, FALSE); |
| 1446 | vim_free(tail); |
| 1447 | } |
| 1448 | } |
| 1449 | } |
| 1450 | |
| 1451 | /* check for out-of-memory */ |
| 1452 | for (i = 0; i < num_names; ++i) |
| 1453 | { |
| 1454 | if (names[i] == NULL) |
| 1455 | { |
| 1456 | for (i = 0; i < num_names; ++i) |
| 1457 | vim_free(names[i]); |
| 1458 | num_names = 0; |
| 1459 | } |
| 1460 | } |
| 1461 | if (num_names == 0) |
| 1462 | num_files = 0; |
| 1463 | else if (expand_wildcards(num_names, names, &num_files, &files, |
| 1464 | EW_KEEPALL|EW_FILE|EW_SILENT) == FAIL) |
| 1465 | num_files = 0; |
| 1466 | |
| 1467 | /* |
| 1468 | * When no swap file found, wildcard expansion might have failed (e.g. |
| 1469 | * not able to execute the shell). |
| 1470 | * Try finding a swap file by simply adding ".swp" to the file name. |
| 1471 | */ |
| 1472 | if (*dirp == NUL && file_count + num_files == 0 |
| 1473 | && fname != NULL && *fname != NULL) |
| 1474 | { |
| 1475 | struct stat st; |
| 1476 | char_u *swapname; |
| 1477 | |
| 1478 | #if defined(VMS) || defined(RISCOS) |
| 1479 | swapname = modname(*fname, (char_u *)"_swp", FALSE); |
| 1480 | #else |
| 1481 | swapname = modname(*fname, (char_u *)".swp", TRUE); |
| 1482 | #endif |
| 1483 | if (swapname != NULL) |
| 1484 | { |
| 1485 | if (mch_stat((char *)swapname, &st) != -1) /* It exists! */ |
| 1486 | { |
| 1487 | files = (char_u **)alloc((unsigned)sizeof(char_u *)); |
| 1488 | if (files != NULL) |
| 1489 | { |
| 1490 | files[0] = swapname; |
| 1491 | swapname = NULL; |
| 1492 | num_files = 1; |
| 1493 | } |
| 1494 | } |
| 1495 | vim_free(swapname); |
| 1496 | } |
| 1497 | } |
| 1498 | |
| 1499 | /* |
| 1500 | * remove swapfile name of the current buffer, it must be ignored |
| 1501 | */ |
| 1502 | if (curbuf->b_ml.ml_mfp != NULL |
| 1503 | && (p = curbuf->b_ml.ml_mfp->mf_fname) != NULL) |
| 1504 | { |
| 1505 | for (i = 0; i < num_files; ++i) |
| 1506 | if (fullpathcmp(p, files[i], TRUE) & FPC_SAME) |
| 1507 | { |
| 1508 | vim_free(files[i]); |
| 1509 | --num_files; |
| 1510 | for ( ; i < num_files; ++i) |
| 1511 | files[i] = files[i + 1]; |
| 1512 | } |
| 1513 | } |
| 1514 | if (nr) |
| 1515 | { |
| 1516 | file_count += num_files; |
| 1517 | if (nr <= file_count) |
| 1518 | { |
| 1519 | *fname = vim_strsave(files[nr - 1 + num_files - file_count]); |
| 1520 | dirp = (char_u *)""; /* stop searching */ |
| 1521 | } |
| 1522 | } |
| 1523 | else if (list) |
| 1524 | { |
| 1525 | if (dir_name[0] == '.' && dir_name[1] == NUL) |
| 1526 | { |
| 1527 | if (fname == NULL || *fname == NULL) |
| 1528 | MSG_PUTS(_(" In current directory:\n")); |
| 1529 | else |
| 1530 | MSG_PUTS(_(" Using specified name:\n")); |
| 1531 | } |
| 1532 | else |
| 1533 | { |
| 1534 | MSG_PUTS(_(" In directory ")); |
| 1535 | msg_home_replace(dir_name); |
| 1536 | MSG_PUTS(":\n"); |
| 1537 | } |
| 1538 | |
| 1539 | if (num_files) |
| 1540 | { |
| 1541 | for (i = 0; i < num_files; ++i) |
| 1542 | { |
| 1543 | /* print the swap file name */ |
| 1544 | msg_outnum((long)++file_count); |
| 1545 | MSG_PUTS(". "); |
| 1546 | msg_puts(gettail(files[i])); |
| 1547 | msg_putchar('\n'); |
| 1548 | (void)swapfile_info(files[i]); |
| 1549 | } |
| 1550 | } |
| 1551 | else |
| 1552 | MSG_PUTS(_(" -- none --\n")); |
| 1553 | out_flush(); |
| 1554 | } |
| 1555 | else |
| 1556 | file_count += num_files; |
| 1557 | |
| 1558 | for (i = 0; i < num_names; ++i) |
| 1559 | vim_free(names[i]); |
| 1560 | FreeWild(num_files, files); |
| 1561 | } |
| 1562 | vim_free(dir_name); |
| 1563 | return file_count; |
| 1564 | } |
| 1565 | |
| 1566 | #if defined(UNIX) || defined(WIN3264) /* Need _very_ long file names */ |
| 1567 | /* |
| 1568 | * Append the full path to name with path separators made into percent |
| 1569 | * signs, to dir. An unnamed buffer is handled as "" (<currentdir>/"") |
| 1570 | */ |
| 1571 | static char_u * |
| 1572 | make_percent_swname(dir, name) |
| 1573 | char_u *dir; |
| 1574 | char_u *name; |
| 1575 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1576 | char_u *d, *s, *f; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1577 | |
| 1578 | f = fix_fname(name != NULL ? name : (char_u *) ""); |
| 1579 | d = NULL; |
| 1580 | if (f != NULL) |
| 1581 | { |
| 1582 | s = alloc((unsigned)(STRLEN(f) + 1)); |
| 1583 | if (s != NULL) |
| 1584 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1585 | STRCPY(s, f); |
| 1586 | for (d = s; *d != NUL; mb_ptr_adv(d)) |
| 1587 | if (vim_ispathsep(*d)) |
| 1588 | *d = '%'; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1589 | d = concat_fnames(dir, s, TRUE); |
| 1590 | vim_free(s); |
| 1591 | } |
| 1592 | vim_free(f); |
| 1593 | } |
| 1594 | return d; |
| 1595 | } |
| 1596 | #endif |
| 1597 | |
| 1598 | #if (defined(UNIX) || defined(__EMX__) || defined(VMS)) && (defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)) |
| 1599 | static int process_still_running; |
| 1600 | #endif |
| 1601 | |
| 1602 | /* |
| 1603 | * Give information about an existing swap file |
| 1604 | * Returns timestamp (0 when unknown). |
| 1605 | */ |
| 1606 | static time_t |
| 1607 | swapfile_info(fname) |
| 1608 | char_u *fname; |
| 1609 | { |
| 1610 | struct stat st; |
| 1611 | int fd; |
| 1612 | struct block0 b0; |
| 1613 | time_t x = (time_t)0; |
| 1614 | #ifdef UNIX |
| 1615 | char_u uname[B0_UNAME_SIZE]; |
| 1616 | #endif |
| 1617 | |
| 1618 | /* print the swap file date */ |
| 1619 | if (mch_stat((char *)fname, &st) != -1) |
| 1620 | { |
| 1621 | #ifdef UNIX |
| 1622 | /* print name of owner of the file */ |
| 1623 | if (mch_get_uname(st.st_uid, uname, B0_UNAME_SIZE) == OK) |
| 1624 | { |
| 1625 | MSG_PUTS(_(" owned by: ")); |
| 1626 | msg_outtrans(uname); |
| 1627 | MSG_PUTS(_(" dated: ")); |
| 1628 | } |
| 1629 | else |
| 1630 | #endif |
| 1631 | MSG_PUTS(_(" dated: ")); |
| 1632 | x = st.st_mtime; /* Manx C can't do &st.st_mtime */ |
| 1633 | MSG_PUTS(ctime(&x)); /* includes '\n' */ |
| 1634 | |
| 1635 | } |
| 1636 | |
| 1637 | /* |
| 1638 | * print the original file name |
| 1639 | */ |
| 1640 | fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); |
| 1641 | if (fd >= 0) |
| 1642 | { |
| 1643 | if (read(fd, (char *)&b0, sizeof(b0)) == sizeof(b0)) |
| 1644 | { |
| 1645 | if (STRNCMP(b0.b0_version, "VIM 3.0", 7) == 0) |
| 1646 | { |
| 1647 | MSG_PUTS(_(" [from Vim version 3.0]")); |
| 1648 | } |
| 1649 | else if (b0.b0_id[0] != BLOCK0_ID0 || b0.b0_id[1] != BLOCK0_ID1) |
| 1650 | { |
| 1651 | MSG_PUTS(_(" [does not look like a Vim swap file]")); |
| 1652 | } |
| 1653 | else |
| 1654 | { |
| 1655 | MSG_PUTS(_(" file name: ")); |
| 1656 | if (b0.b0_fname[0] == NUL) |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 1657 | MSG_PUTS(_("[No Name]")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1658 | else |
| 1659 | msg_outtrans(b0.b0_fname); |
| 1660 | |
| 1661 | MSG_PUTS(_("\n modified: ")); |
| 1662 | MSG_PUTS(b0.b0_dirty ? _("YES") : _("no")); |
| 1663 | |
| 1664 | if (*(b0.b0_uname) != NUL) |
| 1665 | { |
| 1666 | MSG_PUTS(_("\n user name: ")); |
| 1667 | msg_outtrans(b0.b0_uname); |
| 1668 | } |
| 1669 | |
| 1670 | if (*(b0.b0_hname) != NUL) |
| 1671 | { |
| 1672 | if (*(b0.b0_uname) != NUL) |
| 1673 | MSG_PUTS(_(" host name: ")); |
| 1674 | else |
| 1675 | MSG_PUTS(_("\n host name: ")); |
| 1676 | msg_outtrans(b0.b0_hname); |
| 1677 | } |
| 1678 | |
| 1679 | if (char_to_long(b0.b0_pid) != 0L) |
| 1680 | { |
| 1681 | MSG_PUTS(_("\n process ID: ")); |
| 1682 | msg_outnum(char_to_long(b0.b0_pid)); |
| 1683 | #if defined(UNIX) || defined(__EMX__) |
| 1684 | /* EMX kill() not working correctly, it seems */ |
| 1685 | if (kill((pid_t)char_to_long(b0.b0_pid), 0) == 0) |
| 1686 | { |
| 1687 | MSG_PUTS(_(" (still running)")); |
| 1688 | # if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) |
| 1689 | process_still_running = TRUE; |
| 1690 | # endif |
| 1691 | } |
| 1692 | #endif |
| 1693 | } |
| 1694 | |
| 1695 | if (b0_magic_wrong(&b0)) |
| 1696 | { |
| 1697 | #if defined(MSDOS) || defined(MSWIN) |
| 1698 | if (STRNCMP(b0.b0_hname, "PC ", 3) == 0) |
| 1699 | MSG_PUTS(_("\n [not usable with this version of Vim]")); |
| 1700 | else |
| 1701 | #endif |
| 1702 | MSG_PUTS(_("\n [not usable on this computer]")); |
| 1703 | } |
| 1704 | } |
| 1705 | } |
| 1706 | else |
| 1707 | MSG_PUTS(_(" [cannot be read]")); |
| 1708 | close(fd); |
| 1709 | } |
| 1710 | else |
| 1711 | MSG_PUTS(_(" [cannot be opened]")); |
| 1712 | msg_putchar('\n'); |
| 1713 | |
| 1714 | return x; |
| 1715 | } |
| 1716 | |
| 1717 | static int |
| 1718 | recov_file_names(names, path, prepend_dot) |
| 1719 | char_u **names; |
| 1720 | char_u *path; |
| 1721 | int prepend_dot; |
| 1722 | { |
| 1723 | int num_names; |
| 1724 | |
| 1725 | #ifdef SHORT_FNAME |
| 1726 | /* |
| 1727 | * (MS-DOS) always short names |
| 1728 | */ |
| 1729 | names[0] = modname(path, (char_u *)".sw?", FALSE); |
| 1730 | num_names = 1; |
| 1731 | #else /* !SHORT_FNAME */ |
| 1732 | /* |
| 1733 | * (Win32 and Win64) never short names, but do prepend a dot. |
| 1734 | * (Not MS-DOS or Win32 or Win64) maybe short name, maybe not: Try both. |
| 1735 | * Only use the short name if it is different. |
| 1736 | */ |
| 1737 | char_u *p; |
| 1738 | int i; |
| 1739 | # ifndef WIN3264 |
| 1740 | int shortname = curbuf->b_shortname; |
| 1741 | |
| 1742 | curbuf->b_shortname = FALSE; |
| 1743 | # endif |
| 1744 | |
| 1745 | num_names = 0; |
| 1746 | |
| 1747 | /* |
| 1748 | * May also add the file name with a dot prepended, for swap file in same |
| 1749 | * dir as original file. |
| 1750 | */ |
| 1751 | if (prepend_dot) |
| 1752 | { |
| 1753 | names[num_names] = modname(path, (char_u *)".sw?", TRUE); |
| 1754 | if (names[num_names] == NULL) |
| 1755 | goto end; |
| 1756 | ++num_names; |
| 1757 | } |
| 1758 | |
| 1759 | /* |
| 1760 | * Form the normal swap file name pattern by appending ".sw?". |
| 1761 | */ |
| 1762 | #ifdef VMS |
| 1763 | names[num_names] = concat_fnames(path, (char_u *)"_sw%", FALSE); |
| 1764 | #else |
| 1765 | # ifdef RISCOS |
| 1766 | names[num_names] = concat_fnames(path, (char_u *)"_sw#", FALSE); |
| 1767 | # else |
| 1768 | names[num_names] = concat_fnames(path, (char_u *)".sw?", FALSE); |
| 1769 | # endif |
| 1770 | #endif |
| 1771 | if (names[num_names] == NULL) |
| 1772 | goto end; |
| 1773 | if (num_names >= 1) /* check if we have the same name twice */ |
| 1774 | { |
| 1775 | p = names[num_names - 1]; |
| 1776 | i = (int)STRLEN(names[num_names - 1]) - (int)STRLEN(names[num_names]); |
| 1777 | if (i > 0) |
| 1778 | p += i; /* file name has been expanded to full path */ |
| 1779 | |
| 1780 | if (STRCMP(p, names[num_names]) != 0) |
| 1781 | ++num_names; |
| 1782 | else |
| 1783 | vim_free(names[num_names]); |
| 1784 | } |
| 1785 | else |
| 1786 | ++num_names; |
| 1787 | |
| 1788 | # ifndef WIN3264 |
| 1789 | /* |
| 1790 | * Also try with 'shortname' set, in case the file is on a DOS filesystem. |
| 1791 | */ |
| 1792 | curbuf->b_shortname = TRUE; |
| 1793 | #ifdef VMS |
| 1794 | names[num_names] = modname(path, (char_u *)"_sw%", FALSE); |
| 1795 | #else |
| 1796 | # ifdef RISCOS |
| 1797 | names[num_names] = modname(path, (char_u *)"_sw#", FALSE); |
| 1798 | # else |
| 1799 | names[num_names] = modname(path, (char_u *)".sw?", FALSE); |
| 1800 | # endif |
| 1801 | #endif |
| 1802 | if (names[num_names] == NULL) |
| 1803 | goto end; |
| 1804 | |
| 1805 | /* |
| 1806 | * Remove the one from 'shortname', if it's the same as with 'noshortname'. |
| 1807 | */ |
| 1808 | p = names[num_names]; |
| 1809 | i = STRLEN(names[num_names]) - STRLEN(names[num_names - 1]); |
| 1810 | if (i > 0) |
| 1811 | p += i; /* file name has been expanded to full path */ |
| 1812 | if (STRCMP(names[num_names - 1], p) == 0) |
| 1813 | vim_free(names[num_names]); |
| 1814 | else |
| 1815 | ++num_names; |
| 1816 | # endif |
| 1817 | |
| 1818 | end: |
| 1819 | # ifndef WIN3264 |
| 1820 | curbuf->b_shortname = shortname; |
| 1821 | # endif |
| 1822 | |
| 1823 | #endif /* !SHORT_FNAME */ |
| 1824 | |
| 1825 | return num_names; |
| 1826 | } |
| 1827 | |
| 1828 | /* |
| 1829 | * sync all memlines |
| 1830 | * |
| 1831 | * If 'check_file' is TRUE, check if original file exists and was not changed. |
| 1832 | * If 'check_char' is TRUE, stop syncing when character becomes available, but |
| 1833 | * always sync at least one block. |
| 1834 | */ |
| 1835 | void |
| 1836 | ml_sync_all(check_file, check_char) |
| 1837 | int check_file; |
| 1838 | int check_char; |
| 1839 | { |
| 1840 | buf_T *buf; |
| 1841 | struct stat st; |
| 1842 | |
| 1843 | for (buf = firstbuf; buf != NULL; buf = buf->b_next) |
| 1844 | { |
| 1845 | if (buf->b_ml.ml_mfp == NULL || buf->b_ml.ml_mfp->mf_fname == NULL) |
| 1846 | continue; /* no file */ |
| 1847 | |
| 1848 | ml_flush_line(buf); /* flush buffered line */ |
| 1849 | /* flush locked block */ |
| 1850 | (void)ml_find_line(buf, (linenr_T)0, ML_FLUSH); |
| 1851 | if (bufIsChanged(buf) && check_file && mf_need_trans(buf->b_ml.ml_mfp) |
| 1852 | && buf->b_ffname != NULL) |
| 1853 | { |
| 1854 | /* |
| 1855 | * If the original file does not exist anymore or has been changed |
| 1856 | * call ml_preserve() to get rid of all negative numbered blocks. |
| 1857 | */ |
| 1858 | if (mch_stat((char *)buf->b_ffname, &st) == -1 |
| 1859 | || st.st_mtime != buf->b_mtime_read |
| 1860 | || (size_t)st.st_size != buf->b_orig_size) |
| 1861 | { |
| 1862 | ml_preserve(buf, FALSE); |
| 1863 | did_check_timestamps = FALSE; |
| 1864 | need_check_timestamps = TRUE; /* give message later */ |
| 1865 | } |
| 1866 | } |
| 1867 | if (buf->b_ml.ml_mfp->mf_dirty) |
| 1868 | { |
| 1869 | (void)mf_sync(buf->b_ml.ml_mfp, (check_char ? MFS_STOP : 0) |
| 1870 | | (bufIsChanged(buf) ? MFS_FLUSH : 0)); |
| 1871 | if (check_char && ui_char_avail()) /* character available now */ |
| 1872 | break; |
| 1873 | } |
| 1874 | } |
| 1875 | } |
| 1876 | |
| 1877 | /* |
| 1878 | * sync one buffer, including negative blocks |
| 1879 | * |
| 1880 | * after this all the blocks are in the swap file |
| 1881 | * |
| 1882 | * Used for the :preserve command and when the original file has been |
| 1883 | * changed or deleted. |
| 1884 | * |
| 1885 | * when message is TRUE the success of preserving is reported |
| 1886 | */ |
| 1887 | void |
| 1888 | ml_preserve(buf, message) |
| 1889 | buf_T *buf; |
| 1890 | int message; |
| 1891 | { |
| 1892 | bhdr_T *hp; |
| 1893 | linenr_T lnum; |
| 1894 | memfile_T *mfp = buf->b_ml.ml_mfp; |
| 1895 | int status; |
| 1896 | int got_int_save = got_int; |
| 1897 | |
| 1898 | if (mfp == NULL || mfp->mf_fname == NULL) |
| 1899 | { |
| 1900 | if (message) |
| 1901 | EMSG(_("E313: Cannot preserve, there is no swap file")); |
| 1902 | return; |
| 1903 | } |
| 1904 | |
| 1905 | /* We only want to stop when interrupted here, not when interrupted |
| 1906 | * before. */ |
| 1907 | got_int = FALSE; |
| 1908 | |
| 1909 | ml_flush_line(buf); /* flush buffered line */ |
| 1910 | (void)ml_find_line(buf, (linenr_T)0, ML_FLUSH); /* flush locked block */ |
| 1911 | status = mf_sync(mfp, MFS_ALL | MFS_FLUSH); |
| 1912 | |
| 1913 | /* stack is invalid after mf_sync(.., MFS_ALL) */ |
| 1914 | buf->b_ml.ml_stack_top = 0; |
| 1915 | |
| 1916 | /* |
| 1917 | * Some of the data blocks may have been changed from negative to |
| 1918 | * positive block number. In that case the pointer blocks need to be |
| 1919 | * updated. |
| 1920 | * |
| 1921 | * We don't know in which pointer block the references are, so we visit |
| 1922 | * all data blocks until there are no more translations to be done (or |
| 1923 | * we hit the end of the file, which can only happen in case a write fails, |
| 1924 | * e.g. when file system if full). |
| 1925 | * ml_find_line() does the work by translating the negative block numbers |
| 1926 | * when getting the first line of each data block. |
| 1927 | */ |
| 1928 | if (mf_need_trans(mfp) && !got_int) |
| 1929 | { |
| 1930 | lnum = 1; |
| 1931 | while (mf_need_trans(mfp) && lnum <= buf->b_ml.ml_line_count) |
| 1932 | { |
| 1933 | hp = ml_find_line(buf, lnum, ML_FIND); |
| 1934 | if (hp == NULL) |
| 1935 | { |
| 1936 | status = FAIL; |
| 1937 | goto theend; |
| 1938 | } |
| 1939 | CHECK(buf->b_ml.ml_locked_low != lnum, "low != lnum"); |
| 1940 | lnum = buf->b_ml.ml_locked_high + 1; |
| 1941 | } |
| 1942 | (void)ml_find_line(buf, (linenr_T)0, ML_FLUSH); /* flush locked block */ |
| 1943 | /* sync the updated pointer blocks */ |
| 1944 | if (mf_sync(mfp, MFS_ALL | MFS_FLUSH) == FAIL) |
| 1945 | status = FAIL; |
| 1946 | buf->b_ml.ml_stack_top = 0; /* stack is invalid now */ |
| 1947 | } |
| 1948 | theend: |
| 1949 | got_int |= got_int_save; |
| 1950 | |
| 1951 | if (message) |
| 1952 | { |
| 1953 | if (status == OK) |
| 1954 | MSG(_("File preserved")); |
| 1955 | else |
| 1956 | EMSG(_("E314: Preserve failed")); |
| 1957 | } |
| 1958 | } |
| 1959 | |
| 1960 | /* |
| 1961 | * NOTE: The pointer returned by the ml_get_*() functions only remains valid |
| 1962 | * until the next call! |
| 1963 | * line1 = ml_get(1); |
| 1964 | * line2 = ml_get(2); // line1 is now invalid! |
| 1965 | * Make a copy of the line if necessary. |
| 1966 | */ |
| 1967 | /* |
| 1968 | * get a pointer to a (read-only copy of a) line |
| 1969 | * |
| 1970 | * On failure an error message is given and IObuff is returned (to avoid |
| 1971 | * having to check for error everywhere). |
| 1972 | */ |
| 1973 | char_u * |
| 1974 | ml_get(lnum) |
| 1975 | linenr_T lnum; |
| 1976 | { |
| 1977 | return ml_get_buf(curbuf, lnum, FALSE); |
| 1978 | } |
| 1979 | |
| 1980 | /* |
| 1981 | * ml_get_pos: get pointer to position 'pos' |
| 1982 | */ |
| 1983 | char_u * |
| 1984 | ml_get_pos(pos) |
| 1985 | pos_T *pos; |
| 1986 | { |
| 1987 | return (ml_get_buf(curbuf, pos->lnum, FALSE) + pos->col); |
| 1988 | } |
| 1989 | |
| 1990 | /* |
| 1991 | * ml_get_curline: get pointer to cursor line. |
| 1992 | */ |
| 1993 | char_u * |
| 1994 | ml_get_curline() |
| 1995 | { |
| 1996 | return ml_get_buf(curbuf, curwin->w_cursor.lnum, FALSE); |
| 1997 | } |
| 1998 | |
| 1999 | /* |
| 2000 | * ml_get_cursor: get pointer to cursor position |
| 2001 | */ |
| 2002 | char_u * |
| 2003 | ml_get_cursor() |
| 2004 | { |
| 2005 | return (ml_get_buf(curbuf, curwin->w_cursor.lnum, FALSE) + |
| 2006 | curwin->w_cursor.col); |
| 2007 | } |
| 2008 | |
| 2009 | /* |
| 2010 | * get a pointer to a line in a specific buffer |
| 2011 | * |
| 2012 | * "will_change": if TRUE mark the buffer dirty (chars in the line will be |
| 2013 | * changed) |
| 2014 | */ |
| 2015 | char_u * |
| 2016 | ml_get_buf(buf, lnum, will_change) |
| 2017 | buf_T *buf; |
| 2018 | linenr_T lnum; |
| 2019 | int will_change; /* line will be changed */ |
| 2020 | { |
| 2021 | bhdr_T *hp; |
| 2022 | DATA_BL *dp; |
| 2023 | char_u *ptr; |
| 2024 | |
| 2025 | if (lnum > buf->b_ml.ml_line_count) /* invalid line number */ |
| 2026 | { |
| 2027 | EMSGN(_("E315: ml_get: invalid lnum: %ld"), lnum); |
| 2028 | errorret: |
| 2029 | STRCPY(IObuff, "???"); |
| 2030 | return IObuff; |
| 2031 | } |
| 2032 | if (lnum <= 0) /* pretend line 0 is line 1 */ |
| 2033 | lnum = 1; |
| 2034 | |
| 2035 | if (buf->b_ml.ml_mfp == NULL) /* there are no lines */ |
| 2036 | return (char_u *)""; |
| 2037 | |
| 2038 | /* |
| 2039 | * See if it is the same line as requested last time. |
| 2040 | * Otherwise may need to flush last used line. |
| 2041 | */ |
| 2042 | if (buf->b_ml.ml_line_lnum != lnum) |
| 2043 | { |
| 2044 | ml_flush_line(buf); |
| 2045 | |
| 2046 | /* |
| 2047 | * Find the data block containing the line. |
| 2048 | * This also fills the stack with the blocks from the root to the data |
| 2049 | * block and releases any locked block. |
| 2050 | */ |
| 2051 | if ((hp = ml_find_line(buf, lnum, ML_FIND)) == NULL) |
| 2052 | { |
| 2053 | EMSGN(_("E316: ml_get: cannot find line %ld"), lnum); |
| 2054 | goto errorret; |
| 2055 | } |
| 2056 | |
| 2057 | dp = (DATA_BL *)(hp->bh_data); |
| 2058 | |
| 2059 | ptr = (char_u *)dp + ((dp->db_index[lnum - buf->b_ml.ml_locked_low]) & DB_INDEX_MASK); |
| 2060 | buf->b_ml.ml_line_ptr = ptr; |
| 2061 | buf->b_ml.ml_line_lnum = lnum; |
| 2062 | buf->b_ml.ml_flags &= ~ML_LINE_DIRTY; |
| 2063 | } |
| 2064 | if (will_change) |
| 2065 | buf->b_ml.ml_flags |= (ML_LOCKED_DIRTY | ML_LOCKED_POS); |
| 2066 | |
| 2067 | return buf->b_ml.ml_line_ptr; |
| 2068 | } |
| 2069 | |
| 2070 | /* |
| 2071 | * Check if a line that was just obtained by a call to ml_get |
| 2072 | * is in allocated memory. |
| 2073 | */ |
| 2074 | int |
| 2075 | ml_line_alloced() |
| 2076 | { |
| 2077 | return (curbuf->b_ml.ml_flags & ML_LINE_DIRTY); |
| 2078 | } |
| 2079 | |
| 2080 | /* |
| 2081 | * Append a line after lnum (may be 0 to insert a line in front of the file). |
| 2082 | * "line" does not need to be allocated, but can't be another line in a |
| 2083 | * buffer, unlocking may make it invalid. |
| 2084 | * |
| 2085 | * newfile: TRUE when starting to edit a new file, meaning that pe_old_lnum |
| 2086 | * will be set for recovery |
| 2087 | * Check: The caller of this function should probably also call |
| 2088 | * appended_lines(). |
| 2089 | * |
| 2090 | * return FAIL for failure, OK otherwise |
| 2091 | */ |
| 2092 | int |
| 2093 | ml_append(lnum, line, len, newfile) |
| 2094 | linenr_T lnum; /* append after this line (can be 0) */ |
| 2095 | char_u *line; /* text of the new line */ |
| 2096 | colnr_T len; /* length of new line, including NUL, or 0 */ |
| 2097 | int newfile; /* flag, see above */ |
| 2098 | { |
| 2099 | /* When starting up, we might still need to create the memfile */ |
| 2100 | if (curbuf->b_ml.ml_mfp == NULL && open_buffer(FALSE, NULL) == FAIL) |
| 2101 | return FAIL; |
| 2102 | |
| 2103 | if (curbuf->b_ml.ml_line_lnum != 0) |
| 2104 | ml_flush_line(curbuf); |
| 2105 | return ml_append_int(curbuf, lnum, line, len, newfile, FALSE); |
| 2106 | } |
| 2107 | |
| 2108 | static int |
| 2109 | ml_append_int(buf, lnum, line, len, newfile, mark) |
| 2110 | buf_T *buf; |
| 2111 | linenr_T lnum; /* append after this line (can be 0) */ |
| 2112 | char_u *line; /* text of the new line */ |
| 2113 | colnr_T len; /* length of line, including NUL, or 0 */ |
| 2114 | int newfile; /* flag, see above */ |
| 2115 | int mark; /* mark the new line */ |
| 2116 | { |
| 2117 | int i; |
| 2118 | int line_count; /* number of indexes in current block */ |
| 2119 | int offset; |
| 2120 | int from, to; |
| 2121 | int space_needed; /* space needed for new line */ |
| 2122 | int page_size; |
| 2123 | int page_count; |
| 2124 | int db_idx; /* index for lnum in data block */ |
| 2125 | bhdr_T *hp; |
| 2126 | memfile_T *mfp; |
| 2127 | DATA_BL *dp; |
| 2128 | PTR_BL *pp; |
| 2129 | infoptr_T *ip; |
| 2130 | |
| 2131 | /* lnum out of range */ |
| 2132 | if (lnum > buf->b_ml.ml_line_count || buf->b_ml.ml_mfp == NULL) |
| 2133 | return FAIL; |
| 2134 | |
| 2135 | if (lowest_marked && lowest_marked > lnum) |
| 2136 | lowest_marked = lnum + 1; |
| 2137 | |
| 2138 | if (len == 0) |
| 2139 | len = (colnr_T)STRLEN(line) + 1; /* space needed for the text */ |
| 2140 | space_needed = len + INDEX_SIZE; /* space needed for text + index */ |
| 2141 | |
| 2142 | mfp = buf->b_ml.ml_mfp; |
| 2143 | page_size = mfp->mf_page_size; |
| 2144 | |
| 2145 | /* |
| 2146 | * find the data block containing the previous line |
| 2147 | * This also fills the stack with the blocks from the root to the data block |
| 2148 | * This also releases any locked block. |
| 2149 | */ |
| 2150 | if ((hp = ml_find_line(buf, lnum == 0 ? (linenr_T)1 : lnum, |
| 2151 | ML_INSERT)) == NULL) |
| 2152 | return FAIL; |
| 2153 | |
| 2154 | buf->b_ml.ml_flags &= ~ML_EMPTY; |
| 2155 | |
| 2156 | if (lnum == 0) /* got line one instead, correct db_idx */ |
| 2157 | db_idx = -1; /* careful, it is negative! */ |
| 2158 | else |
| 2159 | db_idx = lnum - buf->b_ml.ml_locked_low; |
| 2160 | /* get line count before the insertion */ |
| 2161 | line_count = buf->b_ml.ml_locked_high - buf->b_ml.ml_locked_low; |
| 2162 | |
| 2163 | dp = (DATA_BL *)(hp->bh_data); |
| 2164 | |
| 2165 | /* |
| 2166 | * If |
| 2167 | * - there is not enough room in the current block |
| 2168 | * - appending to the last line in the block |
| 2169 | * - not appending to the last line in the file |
| 2170 | * insert in front of the next block. |
| 2171 | */ |
| 2172 | if ((int)dp->db_free < space_needed && db_idx == line_count - 1 |
| 2173 | && lnum < buf->b_ml.ml_line_count) |
| 2174 | { |
| 2175 | /* |
| 2176 | * Now that the line is not going to be inserted in the block that we |
| 2177 | * expected, the line count has to be adjusted in the pointer blocks |
| 2178 | * by using ml_locked_lineadd. |
| 2179 | */ |
| 2180 | --(buf->b_ml.ml_locked_lineadd); |
| 2181 | --(buf->b_ml.ml_locked_high); |
| 2182 | if ((hp = ml_find_line(buf, lnum + 1, ML_INSERT)) == NULL) |
| 2183 | return FAIL; |
| 2184 | |
| 2185 | db_idx = -1; /* careful, it is negative! */ |
| 2186 | /* get line count before the insertion */ |
| 2187 | line_count = buf->b_ml.ml_locked_high - buf->b_ml.ml_locked_low; |
| 2188 | CHECK(buf->b_ml.ml_locked_low != lnum + 1, "locked_low != lnum + 1"); |
| 2189 | |
| 2190 | dp = (DATA_BL *)(hp->bh_data); |
| 2191 | } |
| 2192 | |
| 2193 | ++buf->b_ml.ml_line_count; |
| 2194 | |
| 2195 | if ((int)dp->db_free >= space_needed) /* enough room in data block */ |
| 2196 | { |
| 2197 | /* |
| 2198 | * Insert new line in existing data block, or in data block allocated above. |
| 2199 | */ |
| 2200 | dp->db_txt_start -= len; |
| 2201 | dp->db_free -= space_needed; |
| 2202 | ++(dp->db_line_count); |
| 2203 | |
| 2204 | /* |
| 2205 | * move the text of the lines that follow to the front |
| 2206 | * adjust the indexes of the lines that follow |
| 2207 | */ |
| 2208 | if (line_count > db_idx + 1) /* if there are following lines */ |
| 2209 | { |
| 2210 | /* |
| 2211 | * Offset is the start of the previous line. |
| 2212 | * This will become the character just after the new line. |
| 2213 | */ |
| 2214 | if (db_idx < 0) |
| 2215 | offset = dp->db_txt_end; |
| 2216 | else |
| 2217 | offset = ((dp->db_index[db_idx]) & DB_INDEX_MASK); |
| 2218 | mch_memmove((char *)dp + dp->db_txt_start, |
| 2219 | (char *)dp + dp->db_txt_start + len, |
| 2220 | (size_t)(offset - (dp->db_txt_start + len))); |
| 2221 | for (i = line_count - 1; i > db_idx; --i) |
| 2222 | dp->db_index[i + 1] = dp->db_index[i] - len; |
| 2223 | dp->db_index[db_idx + 1] = offset - len; |
| 2224 | } |
| 2225 | else /* add line at the end */ |
| 2226 | dp->db_index[db_idx + 1] = dp->db_txt_start; |
| 2227 | |
| 2228 | /* |
| 2229 | * copy the text into the block |
| 2230 | */ |
| 2231 | mch_memmove((char *)dp + dp->db_index[db_idx + 1], line, (size_t)len); |
| 2232 | if (mark) |
| 2233 | dp->db_index[db_idx + 1] |= DB_MARKED; |
| 2234 | |
| 2235 | /* |
| 2236 | * Mark the block dirty. |
| 2237 | */ |
| 2238 | buf->b_ml.ml_flags |= ML_LOCKED_DIRTY; |
| 2239 | if (!newfile) |
| 2240 | buf->b_ml.ml_flags |= ML_LOCKED_POS; |
| 2241 | } |
| 2242 | else /* not enough space in data block */ |
| 2243 | { |
| 2244 | /* |
| 2245 | * If there is not enough room we have to create a new data block and copy some |
| 2246 | * lines into it. |
| 2247 | * Then we have to insert an entry in the pointer block. |
| 2248 | * If this pointer block also is full, we go up another block, and so on, up |
| 2249 | * to the root if necessary. |
| 2250 | * The line counts in the pointer blocks have already been adjusted by |
| 2251 | * ml_find_line(). |
| 2252 | */ |
| 2253 | long line_count_left, line_count_right; |
| 2254 | int page_count_left, page_count_right; |
| 2255 | bhdr_T *hp_left; |
| 2256 | bhdr_T *hp_right; |
| 2257 | bhdr_T *hp_new; |
| 2258 | int lines_moved; |
| 2259 | int data_moved = 0; /* init to shut up gcc */ |
| 2260 | int total_moved = 0; /* init to shut up gcc */ |
| 2261 | DATA_BL *dp_right, *dp_left; |
| 2262 | int stack_idx; |
| 2263 | int in_left; |
| 2264 | int lineadd; |
| 2265 | blocknr_T bnum_left, bnum_right; |
| 2266 | linenr_T lnum_left, lnum_right; |
| 2267 | int pb_idx; |
| 2268 | PTR_BL *pp_new; |
| 2269 | |
| 2270 | /* |
| 2271 | * We are going to allocate a new data block. Depending on the |
| 2272 | * situation it will be put to the left or right of the existing |
| 2273 | * block. If possible we put the new line in the left block and move |
| 2274 | * the lines after it to the right block. Otherwise the new line is |
| 2275 | * also put in the right block. This method is more efficient when |
| 2276 | * inserting a lot of lines at one place. |
| 2277 | */ |
| 2278 | if (db_idx < 0) /* left block is new, right block is existing */ |
| 2279 | { |
| 2280 | lines_moved = 0; |
| 2281 | in_left = TRUE; |
| 2282 | /* space_needed does not change */ |
| 2283 | } |
| 2284 | else /* left block is existing, right block is new */ |
| 2285 | { |
| 2286 | lines_moved = line_count - db_idx - 1; |
| 2287 | if (lines_moved == 0) |
| 2288 | in_left = FALSE; /* put new line in right block */ |
| 2289 | /* space_needed does not change */ |
| 2290 | else |
| 2291 | { |
| 2292 | data_moved = ((dp->db_index[db_idx]) & DB_INDEX_MASK) - |
| 2293 | dp->db_txt_start; |
| 2294 | total_moved = data_moved + lines_moved * INDEX_SIZE; |
| 2295 | if ((int)dp->db_free + total_moved >= space_needed) |
| 2296 | { |
| 2297 | in_left = TRUE; /* put new line in left block */ |
| 2298 | space_needed = total_moved; |
| 2299 | } |
| 2300 | else |
| 2301 | { |
| 2302 | in_left = FALSE; /* put new line in right block */ |
| 2303 | space_needed += total_moved; |
| 2304 | } |
| 2305 | } |
| 2306 | } |
| 2307 | |
| 2308 | page_count = ((space_needed + HEADER_SIZE) + page_size - 1) / page_size; |
| 2309 | if ((hp_new = ml_new_data(mfp, newfile, page_count)) == NULL) |
| 2310 | { |
| 2311 | /* correct line counts in pointer blocks */ |
| 2312 | --(buf->b_ml.ml_locked_lineadd); |
| 2313 | --(buf->b_ml.ml_locked_high); |
| 2314 | return FAIL; |
| 2315 | } |
| 2316 | if (db_idx < 0) /* left block is new */ |
| 2317 | { |
| 2318 | hp_left = hp_new; |
| 2319 | hp_right = hp; |
| 2320 | line_count_left = 0; |
| 2321 | line_count_right = line_count; |
| 2322 | } |
| 2323 | else /* right block is new */ |
| 2324 | { |
| 2325 | hp_left = hp; |
| 2326 | hp_right = hp_new; |
| 2327 | line_count_left = line_count; |
| 2328 | line_count_right = 0; |
| 2329 | } |
| 2330 | dp_right = (DATA_BL *)(hp_right->bh_data); |
| 2331 | dp_left = (DATA_BL *)(hp_left->bh_data); |
| 2332 | bnum_left = hp_left->bh_bnum; |
| 2333 | bnum_right = hp_right->bh_bnum; |
| 2334 | page_count_left = hp_left->bh_page_count; |
| 2335 | page_count_right = hp_right->bh_page_count; |
| 2336 | |
| 2337 | /* |
| 2338 | * May move the new line into the right/new block. |
| 2339 | */ |
| 2340 | if (!in_left) |
| 2341 | { |
| 2342 | dp_right->db_txt_start -= len; |
| 2343 | dp_right->db_free -= len + INDEX_SIZE; |
| 2344 | dp_right->db_index[0] = dp_right->db_txt_start; |
| 2345 | if (mark) |
| 2346 | dp_right->db_index[0] |= DB_MARKED; |
| 2347 | |
| 2348 | mch_memmove((char *)dp_right + dp_right->db_txt_start, |
| 2349 | line, (size_t)len); |
| 2350 | ++line_count_right; |
| 2351 | } |
| 2352 | /* |
| 2353 | * may move lines from the left/old block to the right/new one. |
| 2354 | */ |
| 2355 | if (lines_moved) |
| 2356 | { |
| 2357 | /* |
| 2358 | */ |
| 2359 | dp_right->db_txt_start -= data_moved; |
| 2360 | dp_right->db_free -= total_moved; |
| 2361 | mch_memmove((char *)dp_right + dp_right->db_txt_start, |
| 2362 | (char *)dp_left + dp_left->db_txt_start, |
| 2363 | (size_t)data_moved); |
| 2364 | offset = dp_right->db_txt_start - dp_left->db_txt_start; |
| 2365 | dp_left->db_txt_start += data_moved; |
| 2366 | dp_left->db_free += total_moved; |
| 2367 | |
| 2368 | /* |
| 2369 | * update indexes in the new block |
| 2370 | */ |
| 2371 | for (to = line_count_right, from = db_idx + 1; |
| 2372 | from < line_count_left; ++from, ++to) |
| 2373 | dp_right->db_index[to] = dp->db_index[from] + offset; |
| 2374 | line_count_right += lines_moved; |
| 2375 | line_count_left -= lines_moved; |
| 2376 | } |
| 2377 | |
| 2378 | /* |
| 2379 | * May move the new line into the left (old or new) block. |
| 2380 | */ |
| 2381 | if (in_left) |
| 2382 | { |
| 2383 | dp_left->db_txt_start -= len; |
| 2384 | dp_left->db_free -= len + INDEX_SIZE; |
| 2385 | dp_left->db_index[line_count_left] = dp_left->db_txt_start; |
| 2386 | if (mark) |
| 2387 | dp_left->db_index[line_count_left] |= DB_MARKED; |
| 2388 | mch_memmove((char *)dp_left + dp_left->db_txt_start, |
| 2389 | line, (size_t)len); |
| 2390 | ++line_count_left; |
| 2391 | } |
| 2392 | |
| 2393 | if (db_idx < 0) /* left block is new */ |
| 2394 | { |
| 2395 | lnum_left = lnum + 1; |
| 2396 | lnum_right = 0; |
| 2397 | } |
| 2398 | else /* right block is new */ |
| 2399 | { |
| 2400 | lnum_left = 0; |
| 2401 | if (in_left) |
| 2402 | lnum_right = lnum + 2; |
| 2403 | else |
| 2404 | lnum_right = lnum + 1; |
| 2405 | } |
| 2406 | dp_left->db_line_count = line_count_left; |
| 2407 | dp_right->db_line_count = line_count_right; |
| 2408 | |
| 2409 | /* |
| 2410 | * release the two data blocks |
| 2411 | * The new one (hp_new) already has a correct blocknumber. |
| 2412 | * The old one (hp, in ml_locked) gets a positive blocknumber if |
| 2413 | * we changed it and we are not editing a new file. |
| 2414 | */ |
| 2415 | if (lines_moved || in_left) |
| 2416 | buf->b_ml.ml_flags |= ML_LOCKED_DIRTY; |
| 2417 | if (!newfile && db_idx >= 0 && in_left) |
| 2418 | buf->b_ml.ml_flags |= ML_LOCKED_POS; |
| 2419 | mf_put(mfp, hp_new, TRUE, FALSE); |
| 2420 | |
| 2421 | /* |
| 2422 | * flush the old data block |
| 2423 | * set ml_locked_lineadd to 0, because the updating of the |
| 2424 | * pointer blocks is done below |
| 2425 | */ |
| 2426 | lineadd = buf->b_ml.ml_locked_lineadd; |
| 2427 | buf->b_ml.ml_locked_lineadd = 0; |
| 2428 | ml_find_line(buf, (linenr_T)0, ML_FLUSH); /* flush data block */ |
| 2429 | |
| 2430 | /* |
| 2431 | * update pointer blocks for the new data block |
| 2432 | */ |
| 2433 | for (stack_idx = buf->b_ml.ml_stack_top - 1; stack_idx >= 0; |
| 2434 | --stack_idx) |
| 2435 | { |
| 2436 | ip = &(buf->b_ml.ml_stack[stack_idx]); |
| 2437 | pb_idx = ip->ip_index; |
| 2438 | if ((hp = mf_get(mfp, ip->ip_bnum, 1)) == NULL) |
| 2439 | return FAIL; |
| 2440 | pp = (PTR_BL *)(hp->bh_data); /* must be pointer block */ |
| 2441 | if (pp->pb_id != PTR_ID) |
| 2442 | { |
| 2443 | EMSG(_("E317: pointer block id wrong 3")); |
| 2444 | mf_put(mfp, hp, FALSE, FALSE); |
| 2445 | return FAIL; |
| 2446 | } |
| 2447 | /* |
| 2448 | * TODO: If the pointer block is full and we are adding at the end |
| 2449 | * try to insert in front of the next block |
| 2450 | */ |
| 2451 | /* block not full, add one entry */ |
| 2452 | if (pp->pb_count < pp->pb_count_max) |
| 2453 | { |
| 2454 | if (pb_idx + 1 < (int)pp->pb_count) |
| 2455 | mch_memmove(&pp->pb_pointer[pb_idx + 2], |
| 2456 | &pp->pb_pointer[pb_idx + 1], |
| 2457 | (size_t)(pp->pb_count - pb_idx - 1) * sizeof(PTR_EN)); |
| 2458 | ++pp->pb_count; |
| 2459 | pp->pb_pointer[pb_idx].pe_line_count = line_count_left; |
| 2460 | pp->pb_pointer[pb_idx].pe_bnum = bnum_left; |
| 2461 | pp->pb_pointer[pb_idx].pe_page_count = page_count_left; |
| 2462 | pp->pb_pointer[pb_idx + 1].pe_line_count = line_count_right; |
| 2463 | pp->pb_pointer[pb_idx + 1].pe_bnum = bnum_right; |
| 2464 | pp->pb_pointer[pb_idx + 1].pe_page_count = page_count_right; |
| 2465 | |
| 2466 | if (lnum_left != 0) |
| 2467 | pp->pb_pointer[pb_idx].pe_old_lnum = lnum_left; |
| 2468 | if (lnum_right != 0) |
| 2469 | pp->pb_pointer[pb_idx + 1].pe_old_lnum = lnum_right; |
| 2470 | |
| 2471 | mf_put(mfp, hp, TRUE, FALSE); |
| 2472 | buf->b_ml.ml_stack_top = stack_idx + 1; /* truncate stack */ |
| 2473 | |
| 2474 | if (lineadd) |
| 2475 | { |
| 2476 | --(buf->b_ml.ml_stack_top); |
| 2477 | /* fix line count for rest of blocks in the stack */ |
| 2478 | ml_lineadd(buf, lineadd); |
| 2479 | /* fix stack itself */ |
| 2480 | buf->b_ml.ml_stack[buf->b_ml.ml_stack_top].ip_high += |
| 2481 | lineadd; |
| 2482 | ++(buf->b_ml.ml_stack_top); |
| 2483 | } |
| 2484 | |
| 2485 | /* |
| 2486 | * We are finished, break the loop here. |
| 2487 | */ |
| 2488 | break; |
| 2489 | } |
| 2490 | else /* pointer block full */ |
| 2491 | { |
| 2492 | /* |
| 2493 | * split the pointer block |
| 2494 | * allocate a new pointer block |
| 2495 | * move some of the pointer into the new block |
| 2496 | * prepare for updating the parent block |
| 2497 | */ |
| 2498 | for (;;) /* do this twice when splitting block 1 */ |
| 2499 | { |
| 2500 | hp_new = ml_new_ptr(mfp); |
| 2501 | if (hp_new == NULL) /* TODO: try to fix tree */ |
| 2502 | return FAIL; |
| 2503 | pp_new = (PTR_BL *)(hp_new->bh_data); |
| 2504 | |
| 2505 | if (hp->bh_bnum != 1) |
| 2506 | break; |
| 2507 | |
| 2508 | /* |
| 2509 | * if block 1 becomes full the tree is given an extra level |
| 2510 | * The pointers from block 1 are moved into the new block. |
| 2511 | * block 1 is updated to point to the new block |
| 2512 | * then continue to split the new block |
| 2513 | */ |
| 2514 | mch_memmove(pp_new, pp, (size_t)page_size); |
| 2515 | pp->pb_count = 1; |
| 2516 | pp->pb_pointer[0].pe_bnum = hp_new->bh_bnum; |
| 2517 | pp->pb_pointer[0].pe_line_count = buf->b_ml.ml_line_count; |
| 2518 | pp->pb_pointer[0].pe_old_lnum = 1; |
| 2519 | pp->pb_pointer[0].pe_page_count = 1; |
| 2520 | mf_put(mfp, hp, TRUE, FALSE); /* release block 1 */ |
| 2521 | hp = hp_new; /* new block is to be split */ |
| 2522 | pp = pp_new; |
| 2523 | CHECK(stack_idx != 0, _("stack_idx should be 0")); |
| 2524 | ip->ip_index = 0; |
| 2525 | ++stack_idx; /* do block 1 again later */ |
| 2526 | } |
| 2527 | /* |
| 2528 | * move the pointers after the current one to the new block |
| 2529 | * If there are none, the new entry will be in the new block. |
| 2530 | */ |
| 2531 | total_moved = pp->pb_count - pb_idx - 1; |
| 2532 | if (total_moved) |
| 2533 | { |
| 2534 | mch_memmove(&pp_new->pb_pointer[0], |
| 2535 | &pp->pb_pointer[pb_idx + 1], |
| 2536 | (size_t)(total_moved) * sizeof(PTR_EN)); |
| 2537 | pp_new->pb_count = total_moved; |
| 2538 | pp->pb_count -= total_moved - 1; |
| 2539 | pp->pb_pointer[pb_idx + 1].pe_bnum = bnum_right; |
| 2540 | pp->pb_pointer[pb_idx + 1].pe_line_count = line_count_right; |
| 2541 | pp->pb_pointer[pb_idx + 1].pe_page_count = page_count_right; |
| 2542 | if (lnum_right) |
| 2543 | pp->pb_pointer[pb_idx + 1].pe_old_lnum = lnum_right; |
| 2544 | } |
| 2545 | else |
| 2546 | { |
| 2547 | pp_new->pb_count = 1; |
| 2548 | pp_new->pb_pointer[0].pe_bnum = bnum_right; |
| 2549 | pp_new->pb_pointer[0].pe_line_count = line_count_right; |
| 2550 | pp_new->pb_pointer[0].pe_page_count = page_count_right; |
| 2551 | pp_new->pb_pointer[0].pe_old_lnum = lnum_right; |
| 2552 | } |
| 2553 | pp->pb_pointer[pb_idx].pe_bnum = bnum_left; |
| 2554 | pp->pb_pointer[pb_idx].pe_line_count = line_count_left; |
| 2555 | pp->pb_pointer[pb_idx].pe_page_count = page_count_left; |
| 2556 | if (lnum_left) |
| 2557 | pp->pb_pointer[pb_idx].pe_old_lnum = lnum_left; |
| 2558 | lnum_left = 0; |
| 2559 | lnum_right = 0; |
| 2560 | |
| 2561 | /* |
| 2562 | * recompute line counts |
| 2563 | */ |
| 2564 | line_count_right = 0; |
| 2565 | for (i = 0; i < (int)pp_new->pb_count; ++i) |
| 2566 | line_count_right += pp_new->pb_pointer[i].pe_line_count; |
| 2567 | line_count_left = 0; |
| 2568 | for (i = 0; i < (int)pp->pb_count; ++i) |
| 2569 | line_count_left += pp->pb_pointer[i].pe_line_count; |
| 2570 | |
| 2571 | bnum_left = hp->bh_bnum; |
| 2572 | bnum_right = hp_new->bh_bnum; |
| 2573 | page_count_left = 1; |
| 2574 | page_count_right = 1; |
| 2575 | mf_put(mfp, hp, TRUE, FALSE); |
| 2576 | mf_put(mfp, hp_new, TRUE, FALSE); |
| 2577 | } |
| 2578 | } |
| 2579 | |
| 2580 | /* |
| 2581 | * Safety check: fallen out of for loop? |
| 2582 | */ |
| 2583 | if (stack_idx < 0) |
| 2584 | { |
| 2585 | EMSG(_("E318: Updated too many blocks?")); |
| 2586 | buf->b_ml.ml_stack_top = 0; /* invalidate stack */ |
| 2587 | } |
| 2588 | } |
| 2589 | |
| 2590 | #ifdef FEAT_BYTEOFF |
| 2591 | /* The line was inserted below 'lnum' */ |
| 2592 | ml_updatechunk(buf, lnum + 1, (long)len, ML_CHNK_ADDLINE); |
| 2593 | #endif |
| 2594 | #ifdef FEAT_NETBEANS_INTG |
| 2595 | if (usingNetbeans) |
| 2596 | { |
| 2597 | if (STRLEN(line) > 0) |
Bram Moolenaar | 35a9aaa | 2004-10-24 19:23:07 +0000 | [diff] [blame] | 2598 | netbeans_inserted(buf, lnum+1, (colnr_T)0, line, STRLEN(line)); |
| 2599 | netbeans_inserted(buf, lnum+1, (colnr_T)STRLEN(line), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2600 | (char_u *)"\n", 1); |
| 2601 | } |
| 2602 | #endif |
| 2603 | return OK; |
| 2604 | } |
| 2605 | |
| 2606 | /* |
| 2607 | * replace line lnum, with buffering, in current buffer |
| 2608 | * |
| 2609 | * If copy is TRUE, make a copy of the line, otherwise the line has been |
| 2610 | * copied to allocated memory already. |
| 2611 | * |
| 2612 | * Check: The caller of this function should probably also call |
| 2613 | * changed_lines(), unless update_screen(NOT_VALID) is used. |
| 2614 | * |
| 2615 | * return FAIL for failure, OK otherwise |
| 2616 | */ |
| 2617 | int |
| 2618 | ml_replace(lnum, line, copy) |
| 2619 | linenr_T lnum; |
| 2620 | char_u *line; |
| 2621 | int copy; |
| 2622 | { |
| 2623 | if (line == NULL) /* just checking... */ |
| 2624 | return FAIL; |
| 2625 | |
| 2626 | /* When starting up, we might still need to create the memfile */ |
| 2627 | if (curbuf->b_ml.ml_mfp == NULL && open_buffer(FALSE, NULL) == FAIL) |
| 2628 | return FAIL; |
| 2629 | |
| 2630 | if (copy && (line = vim_strsave(line)) == NULL) /* allocate memory */ |
| 2631 | return FAIL; |
| 2632 | #ifdef FEAT_NETBEANS_INTG |
| 2633 | if (usingNetbeans) |
| 2634 | { |
| 2635 | netbeans_removed(curbuf, lnum, 0, (long)STRLEN(ml_get(lnum))); |
Bram Moolenaar | 35a9aaa | 2004-10-24 19:23:07 +0000 | [diff] [blame] | 2636 | netbeans_inserted(curbuf, lnum, 0, line, STRLEN(line)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2637 | } |
| 2638 | #endif |
| 2639 | if (curbuf->b_ml.ml_line_lnum != lnum) /* other line buffered */ |
| 2640 | ml_flush_line(curbuf); /* flush it */ |
| 2641 | else if (curbuf->b_ml.ml_flags & ML_LINE_DIRTY) /* same line allocated */ |
| 2642 | vim_free(curbuf->b_ml.ml_line_ptr); /* free it */ |
| 2643 | curbuf->b_ml.ml_line_ptr = line; |
| 2644 | curbuf->b_ml.ml_line_lnum = lnum; |
| 2645 | curbuf->b_ml.ml_flags = (curbuf->b_ml.ml_flags | ML_LINE_DIRTY) & ~ML_EMPTY; |
| 2646 | |
| 2647 | return OK; |
| 2648 | } |
| 2649 | |
| 2650 | /* |
| 2651 | * delete line 'lnum' |
| 2652 | * |
| 2653 | * Check: The caller of this function should probably also call |
| 2654 | * deleted_lines() after this. |
| 2655 | * |
| 2656 | * return FAIL for failure, OK otherwise |
| 2657 | */ |
| 2658 | int |
| 2659 | ml_delete(lnum, message) |
| 2660 | linenr_T lnum; |
| 2661 | int message; |
| 2662 | { |
| 2663 | ml_flush_line(curbuf); |
| 2664 | return ml_delete_int(curbuf, lnum, message); |
| 2665 | } |
| 2666 | |
| 2667 | static int |
| 2668 | ml_delete_int(buf, lnum, message) |
| 2669 | buf_T *buf; |
| 2670 | linenr_T lnum; |
| 2671 | int message; |
| 2672 | { |
| 2673 | bhdr_T *hp; |
| 2674 | memfile_T *mfp; |
| 2675 | DATA_BL *dp; |
| 2676 | PTR_BL *pp; |
| 2677 | infoptr_T *ip; |
| 2678 | int count; /* number of entries in block */ |
| 2679 | int idx; |
| 2680 | int stack_idx; |
| 2681 | int text_start; |
| 2682 | int line_start; |
| 2683 | long line_size; |
| 2684 | int i; |
| 2685 | |
| 2686 | if (lnum < 1 || lnum > buf->b_ml.ml_line_count) |
| 2687 | return FAIL; |
| 2688 | |
| 2689 | if (lowest_marked && lowest_marked > lnum) |
| 2690 | lowest_marked--; |
| 2691 | |
| 2692 | /* |
| 2693 | * If the file becomes empty the last line is replaced by an empty line. |
| 2694 | */ |
| 2695 | if (buf->b_ml.ml_line_count == 1) /* file becomes empty */ |
| 2696 | { |
| 2697 | if (message |
| 2698 | #ifdef FEAT_NETBEANS_INTG |
| 2699 | && !netbeansSuppressNoLines |
| 2700 | #endif |
| 2701 | ) |
| 2702 | { |
| 2703 | set_keep_msg((char_u *)_(no_lines_msg)); |
| 2704 | keep_msg_attr = 0; |
| 2705 | } |
| 2706 | /* FEAT_BYTEOFF already handled in there, dont worry 'bout it below */ |
| 2707 | i = ml_replace((linenr_T)1, (char_u *)"", TRUE); |
| 2708 | buf->b_ml.ml_flags |= ML_EMPTY; |
| 2709 | |
| 2710 | return i; |
| 2711 | } |
| 2712 | |
| 2713 | /* |
| 2714 | * find the data block containing the line |
| 2715 | * This also fills the stack with the blocks from the root to the data block |
| 2716 | * This also releases any locked block. |
| 2717 | */ |
| 2718 | mfp = buf->b_ml.ml_mfp; |
| 2719 | if (mfp == NULL) |
| 2720 | return FAIL; |
| 2721 | |
| 2722 | if ((hp = ml_find_line(buf, lnum, ML_DELETE)) == NULL) |
| 2723 | return FAIL; |
| 2724 | |
| 2725 | dp = (DATA_BL *)(hp->bh_data); |
| 2726 | /* compute line count before the delete */ |
| 2727 | count = (long)(buf->b_ml.ml_locked_high) |
| 2728 | - (long)(buf->b_ml.ml_locked_low) + 2; |
| 2729 | idx = lnum - buf->b_ml.ml_locked_low; |
| 2730 | |
| 2731 | --buf->b_ml.ml_line_count; |
| 2732 | |
| 2733 | line_start = ((dp->db_index[idx]) & DB_INDEX_MASK); |
| 2734 | if (idx == 0) /* first line in block, text at the end */ |
| 2735 | line_size = dp->db_txt_end - line_start; |
| 2736 | else |
| 2737 | line_size = ((dp->db_index[idx - 1]) & DB_INDEX_MASK) - line_start; |
| 2738 | |
| 2739 | #ifdef FEAT_NETBEANS_INTG |
| 2740 | if (usingNetbeans) |
Bram Moolenaar | 35a9aaa | 2004-10-24 19:23:07 +0000 | [diff] [blame] | 2741 | netbeans_removed(buf, lnum, 0, (long)line_size); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2742 | #endif |
| 2743 | |
| 2744 | /* |
| 2745 | * special case: If there is only one line in the data block it becomes empty. |
| 2746 | * Then we have to remove the entry, pointing to this data block, from the |
| 2747 | * pointer block. If this pointer block also becomes empty, we go up another |
| 2748 | * block, and so on, up to the root if necessary. |
| 2749 | * The line counts in the pointer blocks have already been adjusted by |
| 2750 | * ml_find_line(). |
| 2751 | */ |
| 2752 | if (count == 1) |
| 2753 | { |
| 2754 | mf_free(mfp, hp); /* free the data block */ |
| 2755 | buf->b_ml.ml_locked = NULL; |
| 2756 | |
| 2757 | for (stack_idx = buf->b_ml.ml_stack_top - 1; stack_idx >= 0; --stack_idx) |
| 2758 | { |
| 2759 | buf->b_ml.ml_stack_top = 0; /* stack is invalid when failing */ |
| 2760 | ip = &(buf->b_ml.ml_stack[stack_idx]); |
| 2761 | idx = ip->ip_index; |
| 2762 | if ((hp = mf_get(mfp, ip->ip_bnum, 1)) == NULL) |
| 2763 | return FAIL; |
| 2764 | pp = (PTR_BL *)(hp->bh_data); /* must be pointer block */ |
| 2765 | if (pp->pb_id != PTR_ID) |
| 2766 | { |
| 2767 | EMSG(_("E317: pointer block id wrong 4")); |
| 2768 | mf_put(mfp, hp, FALSE, FALSE); |
| 2769 | return FAIL; |
| 2770 | } |
| 2771 | count = --(pp->pb_count); |
| 2772 | if (count == 0) /* the pointer block becomes empty! */ |
| 2773 | mf_free(mfp, hp); |
| 2774 | else |
| 2775 | { |
| 2776 | if (count != idx) /* move entries after the deleted one */ |
| 2777 | mch_memmove(&pp->pb_pointer[idx], &pp->pb_pointer[idx + 1], |
| 2778 | (size_t)(count - idx) * sizeof(PTR_EN)); |
| 2779 | mf_put(mfp, hp, TRUE, FALSE); |
| 2780 | |
| 2781 | buf->b_ml.ml_stack_top = stack_idx; /* truncate stack */ |
| 2782 | /* fix line count for rest of blocks in the stack */ |
| 2783 | if (buf->b_ml.ml_locked_lineadd) |
| 2784 | { |
| 2785 | ml_lineadd(buf, buf->b_ml.ml_locked_lineadd); |
| 2786 | buf->b_ml.ml_stack[buf->b_ml.ml_stack_top].ip_high += |
| 2787 | buf->b_ml.ml_locked_lineadd; |
| 2788 | } |
| 2789 | ++(buf->b_ml.ml_stack_top); |
| 2790 | |
| 2791 | break; |
| 2792 | } |
| 2793 | } |
| 2794 | CHECK(stack_idx < 0, _("deleted block 1?")); |
| 2795 | } |
| 2796 | else |
| 2797 | { |
| 2798 | /* |
| 2799 | * delete the text by moving the next lines forwards |
| 2800 | */ |
| 2801 | text_start = dp->db_txt_start; |
| 2802 | mch_memmove((char *)dp + text_start + line_size, |
| 2803 | (char *)dp + text_start, (size_t)(line_start - text_start)); |
| 2804 | |
| 2805 | /* |
| 2806 | * delete the index by moving the next indexes backwards |
| 2807 | * Adjust the indexes for the text movement. |
| 2808 | */ |
| 2809 | for (i = idx; i < count - 1; ++i) |
| 2810 | dp->db_index[i] = dp->db_index[i + 1] + line_size; |
| 2811 | |
| 2812 | dp->db_free += line_size + INDEX_SIZE; |
| 2813 | dp->db_txt_start += line_size; |
| 2814 | --(dp->db_line_count); |
| 2815 | |
| 2816 | /* |
| 2817 | * mark the block dirty and make sure it is in the file (for recovery) |
| 2818 | */ |
| 2819 | buf->b_ml.ml_flags |= (ML_LOCKED_DIRTY | ML_LOCKED_POS); |
| 2820 | } |
| 2821 | |
| 2822 | #ifdef FEAT_BYTEOFF |
| 2823 | ml_updatechunk(buf, lnum, line_size, ML_CHNK_DELLINE); |
| 2824 | #endif |
| 2825 | return OK; |
| 2826 | } |
| 2827 | |
| 2828 | /* |
| 2829 | * set the B_MARKED flag for line 'lnum' |
| 2830 | */ |
| 2831 | void |
| 2832 | ml_setmarked(lnum) |
| 2833 | linenr_T lnum; |
| 2834 | { |
| 2835 | bhdr_T *hp; |
| 2836 | DATA_BL *dp; |
| 2837 | /* invalid line number */ |
| 2838 | if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count |
| 2839 | || curbuf->b_ml.ml_mfp == NULL) |
| 2840 | return; /* give error message? */ |
| 2841 | |
| 2842 | if (lowest_marked == 0 || lowest_marked > lnum) |
| 2843 | lowest_marked = lnum; |
| 2844 | |
| 2845 | /* |
| 2846 | * find the data block containing the line |
| 2847 | * This also fills the stack with the blocks from the root to the data block |
| 2848 | * This also releases any locked block. |
| 2849 | */ |
| 2850 | if ((hp = ml_find_line(curbuf, lnum, ML_FIND)) == NULL) |
| 2851 | return; /* give error message? */ |
| 2852 | |
| 2853 | dp = (DATA_BL *)(hp->bh_data); |
| 2854 | dp->db_index[lnum - curbuf->b_ml.ml_locked_low] |= DB_MARKED; |
| 2855 | curbuf->b_ml.ml_flags |= ML_LOCKED_DIRTY; |
| 2856 | } |
| 2857 | |
| 2858 | /* |
| 2859 | * find the first line with its B_MARKED flag set |
| 2860 | */ |
| 2861 | linenr_T |
| 2862 | ml_firstmarked() |
| 2863 | { |
| 2864 | bhdr_T *hp; |
| 2865 | DATA_BL *dp; |
| 2866 | linenr_T lnum; |
| 2867 | int i; |
| 2868 | |
| 2869 | if (curbuf->b_ml.ml_mfp == NULL) |
| 2870 | return (linenr_T) 0; |
| 2871 | |
| 2872 | /* |
| 2873 | * The search starts with lowest_marked line. This is the last line where |
| 2874 | * a mark was found, adjusted by inserting/deleting lines. |
| 2875 | */ |
| 2876 | for (lnum = lowest_marked; lnum <= curbuf->b_ml.ml_line_count; ) |
| 2877 | { |
| 2878 | /* |
| 2879 | * Find the data block containing the line. |
| 2880 | * This also fills the stack with the blocks from the root to the data |
| 2881 | * block This also releases any locked block. |
| 2882 | */ |
| 2883 | if ((hp = ml_find_line(curbuf, lnum, ML_FIND)) == NULL) |
| 2884 | return (linenr_T)0; /* give error message? */ |
| 2885 | |
| 2886 | dp = (DATA_BL *)(hp->bh_data); |
| 2887 | |
| 2888 | for (i = lnum - curbuf->b_ml.ml_locked_low; |
| 2889 | lnum <= curbuf->b_ml.ml_locked_high; ++i, ++lnum) |
| 2890 | if ((dp->db_index[i]) & DB_MARKED) |
| 2891 | { |
| 2892 | (dp->db_index[i]) &= DB_INDEX_MASK; |
| 2893 | curbuf->b_ml.ml_flags |= ML_LOCKED_DIRTY; |
| 2894 | lowest_marked = lnum + 1; |
| 2895 | return lnum; |
| 2896 | } |
| 2897 | } |
| 2898 | |
| 2899 | return (linenr_T) 0; |
| 2900 | } |
| 2901 | |
| 2902 | #if 0 /* not used */ |
| 2903 | /* |
| 2904 | * return TRUE if line 'lnum' has a mark |
| 2905 | */ |
| 2906 | int |
| 2907 | ml_has_mark(lnum) |
| 2908 | linenr_T lnum; |
| 2909 | { |
| 2910 | bhdr_T *hp; |
| 2911 | DATA_BL *dp; |
| 2912 | |
| 2913 | if (curbuf->b_ml.ml_mfp == NULL |
| 2914 | || (hp = ml_find_line(curbuf, lnum, ML_FIND)) == NULL) |
| 2915 | return FALSE; |
| 2916 | |
| 2917 | dp = (DATA_BL *)(hp->bh_data); |
| 2918 | return (int)((dp->db_index[lnum - curbuf->b_ml.ml_locked_low]) & DB_MARKED); |
| 2919 | } |
| 2920 | #endif |
| 2921 | |
| 2922 | /* |
| 2923 | * clear all DB_MARKED flags |
| 2924 | */ |
| 2925 | void |
| 2926 | ml_clearmarked() |
| 2927 | { |
| 2928 | bhdr_T *hp; |
| 2929 | DATA_BL *dp; |
| 2930 | linenr_T lnum; |
| 2931 | int i; |
| 2932 | |
| 2933 | if (curbuf->b_ml.ml_mfp == NULL) /* nothing to do */ |
| 2934 | return; |
| 2935 | |
| 2936 | /* |
| 2937 | * The search starts with line lowest_marked. |
| 2938 | */ |
| 2939 | for (lnum = lowest_marked; lnum <= curbuf->b_ml.ml_line_count; ) |
| 2940 | { |
| 2941 | /* |
| 2942 | * Find the data block containing the line. |
| 2943 | * This also fills the stack with the blocks from the root to the data |
| 2944 | * block and releases any locked block. |
| 2945 | */ |
| 2946 | if ((hp = ml_find_line(curbuf, lnum, ML_FIND)) == NULL) |
| 2947 | return; /* give error message? */ |
| 2948 | |
| 2949 | dp = (DATA_BL *)(hp->bh_data); |
| 2950 | |
| 2951 | for (i = lnum - curbuf->b_ml.ml_locked_low; |
| 2952 | lnum <= curbuf->b_ml.ml_locked_high; ++i, ++lnum) |
| 2953 | if ((dp->db_index[i]) & DB_MARKED) |
| 2954 | { |
| 2955 | (dp->db_index[i]) &= DB_INDEX_MASK; |
| 2956 | curbuf->b_ml.ml_flags |= ML_LOCKED_DIRTY; |
| 2957 | } |
| 2958 | } |
| 2959 | |
| 2960 | lowest_marked = 0; |
| 2961 | return; |
| 2962 | } |
| 2963 | |
| 2964 | /* |
| 2965 | * flush ml_line if necessary |
| 2966 | */ |
| 2967 | static void |
| 2968 | ml_flush_line(buf) |
| 2969 | buf_T *buf; |
| 2970 | { |
| 2971 | bhdr_T *hp; |
| 2972 | DATA_BL *dp; |
| 2973 | linenr_T lnum; |
| 2974 | char_u *new_line; |
| 2975 | char_u *old_line; |
| 2976 | colnr_T new_len; |
| 2977 | int old_len; |
| 2978 | int extra; |
| 2979 | int idx; |
| 2980 | int start; |
| 2981 | int count; |
| 2982 | int i; |
| 2983 | |
| 2984 | if (buf->b_ml.ml_line_lnum == 0 || buf->b_ml.ml_mfp == NULL) |
| 2985 | return; /* nothing to do */ |
| 2986 | |
| 2987 | if (buf->b_ml.ml_flags & ML_LINE_DIRTY) |
| 2988 | { |
| 2989 | lnum = buf->b_ml.ml_line_lnum; |
| 2990 | new_line = buf->b_ml.ml_line_ptr; |
| 2991 | |
| 2992 | hp = ml_find_line(buf, lnum, ML_FIND); |
| 2993 | if (hp == NULL) |
| 2994 | EMSGN(_("E320: Cannot find line %ld"), lnum); |
| 2995 | else |
| 2996 | { |
| 2997 | dp = (DATA_BL *)(hp->bh_data); |
| 2998 | idx = lnum - buf->b_ml.ml_locked_low; |
| 2999 | start = ((dp->db_index[idx]) & DB_INDEX_MASK); |
| 3000 | old_line = (char_u *)dp + start; |
| 3001 | if (idx == 0) /* line is last in block */ |
| 3002 | old_len = dp->db_txt_end - start; |
| 3003 | else /* text of previous line follows */ |
| 3004 | old_len = (dp->db_index[idx - 1] & DB_INDEX_MASK) - start; |
| 3005 | new_len = (colnr_T)STRLEN(new_line) + 1; |
| 3006 | extra = new_len - old_len; /* negative if lines gets smaller */ |
| 3007 | |
| 3008 | /* |
| 3009 | * if new line fits in data block, replace directly |
| 3010 | */ |
| 3011 | if ((int)dp->db_free >= extra) |
| 3012 | { |
| 3013 | /* if the length changes and there are following lines */ |
| 3014 | count = buf->b_ml.ml_locked_high - buf->b_ml.ml_locked_low + 1; |
| 3015 | if (extra != 0 && idx < count - 1) |
| 3016 | { |
| 3017 | /* move text of following lines */ |
| 3018 | mch_memmove((char *)dp + dp->db_txt_start - extra, |
| 3019 | (char *)dp + dp->db_txt_start, |
| 3020 | (size_t)(start - dp->db_txt_start)); |
| 3021 | |
| 3022 | /* adjust pointers of this and following lines */ |
| 3023 | for (i = idx + 1; i < count; ++i) |
| 3024 | dp->db_index[i] -= extra; |
| 3025 | } |
| 3026 | dp->db_index[idx] -= extra; |
| 3027 | |
| 3028 | /* adjust free space */ |
| 3029 | dp->db_free -= extra; |
| 3030 | dp->db_txt_start -= extra; |
| 3031 | |
| 3032 | /* copy new line into the data block */ |
| 3033 | mch_memmove(old_line - extra, new_line, (size_t)new_len); |
| 3034 | buf->b_ml.ml_flags |= (ML_LOCKED_DIRTY | ML_LOCKED_POS); |
| 3035 | #ifdef FEAT_BYTEOFF |
| 3036 | /* The else case is already covered by the insert and delete */ |
| 3037 | ml_updatechunk(buf, lnum, (long)extra, ML_CHNK_UPDLINE); |
| 3038 | #endif |
| 3039 | } |
| 3040 | else |
| 3041 | { |
| 3042 | /* |
| 3043 | * Cannot do it in one data block: Delete and append. |
| 3044 | * Append first, because ml_delete_int() cannot delete the |
| 3045 | * last line in a buffer, which causes trouble for a buffer |
| 3046 | * that has only one line. |
| 3047 | * Don't forget to copy the mark! |
| 3048 | */ |
| 3049 | /* How about handling errors??? */ |
| 3050 | (void)ml_append_int(buf, lnum, new_line, new_len, FALSE, |
| 3051 | (dp->db_index[idx] & DB_MARKED)); |
| 3052 | (void)ml_delete_int(buf, lnum, FALSE); |
| 3053 | } |
| 3054 | } |
| 3055 | vim_free(new_line); |
| 3056 | } |
| 3057 | |
| 3058 | buf->b_ml.ml_line_lnum = 0; |
| 3059 | } |
| 3060 | |
| 3061 | /* |
| 3062 | * create a new, empty, data block |
| 3063 | */ |
| 3064 | static bhdr_T * |
| 3065 | ml_new_data(mfp, negative, page_count) |
| 3066 | memfile_T *mfp; |
| 3067 | int negative; |
| 3068 | int page_count; |
| 3069 | { |
| 3070 | bhdr_T *hp; |
| 3071 | DATA_BL *dp; |
| 3072 | |
| 3073 | if ((hp = mf_new(mfp, negative, page_count)) == NULL) |
| 3074 | return NULL; |
| 3075 | |
| 3076 | dp = (DATA_BL *)(hp->bh_data); |
| 3077 | dp->db_id = DATA_ID; |
| 3078 | dp->db_txt_start = dp->db_txt_end = page_count * mfp->mf_page_size; |
| 3079 | dp->db_free = dp->db_txt_start - HEADER_SIZE; |
| 3080 | dp->db_line_count = 0; |
| 3081 | |
| 3082 | return hp; |
| 3083 | } |
| 3084 | |
| 3085 | /* |
| 3086 | * create a new, empty, pointer block |
| 3087 | */ |
| 3088 | static bhdr_T * |
| 3089 | ml_new_ptr(mfp) |
| 3090 | memfile_T *mfp; |
| 3091 | { |
| 3092 | bhdr_T *hp; |
| 3093 | PTR_BL *pp; |
| 3094 | |
| 3095 | if ((hp = mf_new(mfp, FALSE, 1)) == NULL) |
| 3096 | return NULL; |
| 3097 | |
| 3098 | pp = (PTR_BL *)(hp->bh_data); |
| 3099 | pp->pb_id = PTR_ID; |
| 3100 | pp->pb_count = 0; |
| 3101 | pp->pb_count_max = (short_u)((mfp->mf_page_size - sizeof(PTR_BL)) / sizeof(PTR_EN) + 1); |
| 3102 | |
| 3103 | return hp; |
| 3104 | } |
| 3105 | |
| 3106 | /* |
| 3107 | * lookup line 'lnum' in a memline |
| 3108 | * |
| 3109 | * action: if ML_DELETE or ML_INSERT the line count is updated while searching |
| 3110 | * if ML_FLUSH only flush a locked block |
| 3111 | * if ML_FIND just find the line |
| 3112 | * |
| 3113 | * If the block was found it is locked and put in ml_locked. |
| 3114 | * The stack is updated to lead to the locked block. The ip_high field in |
| 3115 | * the stack is updated to reflect the last line in the block AFTER the |
| 3116 | * insert or delete, also if the pointer block has not been updated yet. But |
| 3117 | * if if ml_locked != NULL ml_locked_lineadd must be added to ip_high. |
| 3118 | * |
| 3119 | * return: NULL for failure, pointer to block header otherwise |
| 3120 | */ |
| 3121 | static bhdr_T * |
| 3122 | ml_find_line(buf, lnum, action) |
| 3123 | buf_T *buf; |
| 3124 | linenr_T lnum; |
| 3125 | int action; |
| 3126 | { |
| 3127 | DATA_BL *dp; |
| 3128 | PTR_BL *pp; |
| 3129 | infoptr_T *ip; |
| 3130 | bhdr_T *hp; |
| 3131 | memfile_T *mfp; |
| 3132 | linenr_T t; |
| 3133 | blocknr_T bnum, bnum2; |
| 3134 | int dirty; |
| 3135 | linenr_T low, high; |
| 3136 | int top; |
| 3137 | int page_count; |
| 3138 | int idx; |
| 3139 | |
| 3140 | mfp = buf->b_ml.ml_mfp; |
| 3141 | |
| 3142 | /* |
| 3143 | * If there is a locked block check if the wanted line is in it. |
| 3144 | * If not, flush and release the locked block. |
| 3145 | * Don't do this for ML_INSERT_SAME, because the stack need to be updated. |
| 3146 | * Don't do this for ML_FLUSH, because we want to flush the locked block. |
| 3147 | */ |
| 3148 | if (buf->b_ml.ml_locked) |
| 3149 | { |
| 3150 | if (ML_SIMPLE(action) && buf->b_ml.ml_locked_low <= lnum |
| 3151 | && buf->b_ml.ml_locked_high >= lnum) |
| 3152 | { |
| 3153 | /* remember to update pointer blocks and stack later */ |
| 3154 | if (action == ML_INSERT) |
| 3155 | { |
| 3156 | ++(buf->b_ml.ml_locked_lineadd); |
| 3157 | ++(buf->b_ml.ml_locked_high); |
| 3158 | } |
| 3159 | else if (action == ML_DELETE) |
| 3160 | { |
| 3161 | --(buf->b_ml.ml_locked_lineadd); |
| 3162 | --(buf->b_ml.ml_locked_high); |
| 3163 | } |
| 3164 | return (buf->b_ml.ml_locked); |
| 3165 | } |
| 3166 | |
| 3167 | mf_put(mfp, buf->b_ml.ml_locked, buf->b_ml.ml_flags & ML_LOCKED_DIRTY, |
| 3168 | buf->b_ml.ml_flags & ML_LOCKED_POS); |
| 3169 | buf->b_ml.ml_locked = NULL; |
| 3170 | |
| 3171 | /* |
| 3172 | * if lines have been added or deleted in the locked block, need to |
| 3173 | * update the line count in pointer blocks |
| 3174 | */ |
| 3175 | if (buf->b_ml.ml_locked_lineadd) |
| 3176 | ml_lineadd(buf, buf->b_ml.ml_locked_lineadd); |
| 3177 | } |
| 3178 | |
| 3179 | if (action == ML_FLUSH) /* nothing else to do */ |
| 3180 | return NULL; |
| 3181 | |
| 3182 | bnum = 1; /* start at the root of the tree */ |
| 3183 | page_count = 1; |
| 3184 | low = 1; |
| 3185 | high = buf->b_ml.ml_line_count; |
| 3186 | |
| 3187 | if (action == ML_FIND) /* first try stack entries */ |
| 3188 | { |
| 3189 | for (top = buf->b_ml.ml_stack_top - 1; top >= 0; --top) |
| 3190 | { |
| 3191 | ip = &(buf->b_ml.ml_stack[top]); |
| 3192 | if (ip->ip_low <= lnum && ip->ip_high >= lnum) |
| 3193 | { |
| 3194 | bnum = ip->ip_bnum; |
| 3195 | low = ip->ip_low; |
| 3196 | high = ip->ip_high; |
| 3197 | buf->b_ml.ml_stack_top = top; /* truncate stack at prev entry */ |
| 3198 | break; |
| 3199 | } |
| 3200 | } |
| 3201 | if (top < 0) |
| 3202 | buf->b_ml.ml_stack_top = 0; /* not found, start at the root */ |
| 3203 | } |
| 3204 | else /* ML_DELETE or ML_INSERT */ |
| 3205 | buf->b_ml.ml_stack_top = 0; /* start at the root */ |
| 3206 | |
| 3207 | /* |
| 3208 | * search downwards in the tree until a data block is found |
| 3209 | */ |
| 3210 | for (;;) |
| 3211 | { |
| 3212 | if ((hp = mf_get(mfp, bnum, page_count)) == NULL) |
| 3213 | goto error_noblock; |
| 3214 | |
| 3215 | /* |
| 3216 | * update high for insert/delete |
| 3217 | */ |
| 3218 | if (action == ML_INSERT) |
| 3219 | ++high; |
| 3220 | else if (action == ML_DELETE) |
| 3221 | --high; |
| 3222 | |
| 3223 | dp = (DATA_BL *)(hp->bh_data); |
| 3224 | if (dp->db_id == DATA_ID) /* data block */ |
| 3225 | { |
| 3226 | buf->b_ml.ml_locked = hp; |
| 3227 | buf->b_ml.ml_locked_low = low; |
| 3228 | buf->b_ml.ml_locked_high = high; |
| 3229 | buf->b_ml.ml_locked_lineadd = 0; |
| 3230 | buf->b_ml.ml_flags &= ~(ML_LOCKED_DIRTY | ML_LOCKED_POS); |
| 3231 | return hp; |
| 3232 | } |
| 3233 | |
| 3234 | pp = (PTR_BL *)(dp); /* must be pointer block */ |
| 3235 | if (pp->pb_id != PTR_ID) |
| 3236 | { |
| 3237 | EMSG(_("E317: pointer block id wrong")); |
| 3238 | goto error_block; |
| 3239 | } |
| 3240 | |
| 3241 | if ((top = ml_add_stack(buf)) < 0) /* add new entry to stack */ |
| 3242 | goto error_block; |
| 3243 | ip = &(buf->b_ml.ml_stack[top]); |
| 3244 | ip->ip_bnum = bnum; |
| 3245 | ip->ip_low = low; |
| 3246 | ip->ip_high = high; |
| 3247 | ip->ip_index = -1; /* index not known yet */ |
| 3248 | |
| 3249 | dirty = FALSE; |
| 3250 | for (idx = 0; idx < (int)pp->pb_count; ++idx) |
| 3251 | { |
| 3252 | t = pp->pb_pointer[idx].pe_line_count; |
| 3253 | CHECK(t == 0, _("pe_line_count is zero")); |
| 3254 | if ((low += t) > lnum) |
| 3255 | { |
| 3256 | ip->ip_index = idx; |
| 3257 | bnum = pp->pb_pointer[idx].pe_bnum; |
| 3258 | page_count = pp->pb_pointer[idx].pe_page_count; |
| 3259 | high = low - 1; |
| 3260 | low -= t; |
| 3261 | |
| 3262 | /* |
| 3263 | * a negative block number may have been changed |
| 3264 | */ |
| 3265 | if (bnum < 0) |
| 3266 | { |
| 3267 | bnum2 = mf_trans_del(mfp, bnum); |
| 3268 | if (bnum != bnum2) |
| 3269 | { |
| 3270 | bnum = bnum2; |
| 3271 | pp->pb_pointer[idx].pe_bnum = bnum; |
| 3272 | dirty = TRUE; |
| 3273 | } |
| 3274 | } |
| 3275 | |
| 3276 | break; |
| 3277 | } |
| 3278 | } |
| 3279 | if (idx >= (int)pp->pb_count) /* past the end: something wrong! */ |
| 3280 | { |
| 3281 | if (lnum > buf->b_ml.ml_line_count) |
| 3282 | EMSGN(_("E322: line number out of range: %ld past the end"), |
| 3283 | lnum - buf->b_ml.ml_line_count); |
| 3284 | |
| 3285 | else |
| 3286 | EMSGN(_("E323: line count wrong in block %ld"), bnum); |
| 3287 | goto error_block; |
| 3288 | } |
| 3289 | if (action == ML_DELETE) |
| 3290 | { |
| 3291 | pp->pb_pointer[idx].pe_line_count--; |
| 3292 | dirty = TRUE; |
| 3293 | } |
| 3294 | else if (action == ML_INSERT) |
| 3295 | { |
| 3296 | pp->pb_pointer[idx].pe_line_count++; |
| 3297 | dirty = TRUE; |
| 3298 | } |
| 3299 | mf_put(mfp, hp, dirty, FALSE); |
| 3300 | } |
| 3301 | |
| 3302 | error_block: |
| 3303 | mf_put(mfp, hp, FALSE, FALSE); |
| 3304 | error_noblock: |
| 3305 | /* |
| 3306 | * If action is ML_DELETE or ML_INSERT we have to correct the tree for |
| 3307 | * the incremented/decremented line counts, because there won't be a line |
| 3308 | * inserted/deleted after all. |
| 3309 | */ |
| 3310 | if (action == ML_DELETE) |
| 3311 | ml_lineadd(buf, 1); |
| 3312 | else if (action == ML_INSERT) |
| 3313 | ml_lineadd(buf, -1); |
| 3314 | buf->b_ml.ml_stack_top = 0; |
| 3315 | return NULL; |
| 3316 | } |
| 3317 | |
| 3318 | /* |
| 3319 | * add an entry to the info pointer stack |
| 3320 | * |
| 3321 | * return -1 for failure, number of the new entry otherwise |
| 3322 | */ |
| 3323 | static int |
| 3324 | ml_add_stack(buf) |
| 3325 | buf_T *buf; |
| 3326 | { |
| 3327 | int top; |
| 3328 | infoptr_T *newstack; |
| 3329 | |
| 3330 | top = buf->b_ml.ml_stack_top; |
| 3331 | |
| 3332 | /* may have to increase the stack size */ |
| 3333 | if (top == buf->b_ml.ml_stack_size) |
| 3334 | { |
| 3335 | CHECK(top > 0, _("Stack size increases")); /* more than 5 levels??? */ |
| 3336 | |
| 3337 | newstack = (infoptr_T *)alloc((unsigned)sizeof(infoptr_T) * |
| 3338 | (buf->b_ml.ml_stack_size + STACK_INCR)); |
| 3339 | if (newstack == NULL) |
| 3340 | return -1; |
| 3341 | mch_memmove(newstack, buf->b_ml.ml_stack, (size_t)top * sizeof(infoptr_T)); |
| 3342 | vim_free(buf->b_ml.ml_stack); |
| 3343 | buf->b_ml.ml_stack = newstack; |
| 3344 | buf->b_ml.ml_stack_size += STACK_INCR; |
| 3345 | } |
| 3346 | |
| 3347 | buf->b_ml.ml_stack_top++; |
| 3348 | return top; |
| 3349 | } |
| 3350 | |
| 3351 | /* |
| 3352 | * Update the pointer blocks on the stack for inserted/deleted lines. |
| 3353 | * The stack itself is also updated. |
| 3354 | * |
| 3355 | * When a insert/delete line action fails, the line is not inserted/deleted, |
| 3356 | * but the pointer blocks have already been updated. That is fixed here by |
| 3357 | * walking through the stack. |
| 3358 | * |
| 3359 | * Count is the number of lines added, negative if lines have been deleted. |
| 3360 | */ |
| 3361 | static void |
| 3362 | ml_lineadd(buf, count) |
| 3363 | buf_T *buf; |
| 3364 | int count; |
| 3365 | { |
| 3366 | int idx; |
| 3367 | infoptr_T *ip; |
| 3368 | PTR_BL *pp; |
| 3369 | memfile_T *mfp = buf->b_ml.ml_mfp; |
| 3370 | bhdr_T *hp; |
| 3371 | |
| 3372 | for (idx = buf->b_ml.ml_stack_top - 1; idx >= 0; --idx) |
| 3373 | { |
| 3374 | ip = &(buf->b_ml.ml_stack[idx]); |
| 3375 | if ((hp = mf_get(mfp, ip->ip_bnum, 1)) == NULL) |
| 3376 | break; |
| 3377 | pp = (PTR_BL *)(hp->bh_data); /* must be pointer block */ |
| 3378 | if (pp->pb_id != PTR_ID) |
| 3379 | { |
| 3380 | mf_put(mfp, hp, FALSE, FALSE); |
| 3381 | EMSG(_("E317: pointer block id wrong 2")); |
| 3382 | break; |
| 3383 | } |
| 3384 | pp->pb_pointer[ip->ip_index].pe_line_count += count; |
| 3385 | ip->ip_high += count; |
| 3386 | mf_put(mfp, hp, TRUE, FALSE); |
| 3387 | } |
| 3388 | } |
| 3389 | |
| 3390 | /* |
| 3391 | * make swap file name out of the file name and a directory name |
| 3392 | */ |
| 3393 | static char_u * |
| 3394 | makeswapname(buf, dir_name) |
| 3395 | buf_T *buf; |
| 3396 | char_u *dir_name; |
| 3397 | { |
| 3398 | char_u *r, *s; |
| 3399 | |
| 3400 | #if defined(UNIX) || defined(WIN3264) /* Need _very_ long file names */ |
| 3401 | s = dir_name + STRLEN(dir_name); |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 3402 | if (after_pathsep(dir_name, s) && s[-1] == s[-2]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3403 | { /* Ends with '//', Use Full path */ |
| 3404 | r = NULL; |
| 3405 | if ((s = make_percent_swname(dir_name, buf->b_fname)) != NULL) |
| 3406 | { |
| 3407 | r = modname(s, (char_u *)".swp", FALSE); |
| 3408 | vim_free(s); |
| 3409 | } |
| 3410 | return r; |
| 3411 | } |
| 3412 | #endif |
| 3413 | |
| 3414 | r = buf_modname( |
| 3415 | #ifdef SHORT_FNAME |
| 3416 | TRUE, |
| 3417 | #else |
| 3418 | (buf->b_p_sn || buf->b_shortname), |
| 3419 | #endif |
| 3420 | #ifdef RISCOS |
| 3421 | /* Avoid problems if fname has special chars, eg <Wimp$Scrap> */ |
| 3422 | buf->b_ffname, |
| 3423 | #else |
| 3424 | buf->b_fname, |
| 3425 | #endif |
| 3426 | (char_u *) |
| 3427 | #if defined(VMS) || defined(RISCOS) |
| 3428 | "_swp", |
| 3429 | #else |
| 3430 | ".swp", |
| 3431 | #endif |
| 3432 | #ifdef SHORT_FNAME /* always 8.3 file name */ |
| 3433 | FALSE |
| 3434 | #else |
| 3435 | /* Prepend a '.' to the swap file name for the current directory. */ |
| 3436 | dir_name[0] == '.' && dir_name[1] == NUL |
| 3437 | #endif |
| 3438 | ); |
| 3439 | if (r == NULL) /* out of memory */ |
| 3440 | return NULL; |
| 3441 | |
| 3442 | s = get_file_in_dir(r, dir_name); |
| 3443 | vim_free(r); |
| 3444 | return s; |
| 3445 | } |
| 3446 | |
| 3447 | /* |
| 3448 | * Get file name to use for swap file or backup file. |
| 3449 | * Use the name of the edited file "fname" and an entry in the 'dir' or 'bdir' |
| 3450 | * option "dname". |
| 3451 | * - If "dname" is ".", return "fname" (swap file in dir of file). |
| 3452 | * - If "dname" starts with "./", insert "dname" in "fname" (swap file |
| 3453 | * relative to dir of file). |
| 3454 | * - Otherwise, prepend "dname" to the tail of "fname" (swap file in specific |
| 3455 | * dir). |
| 3456 | * |
| 3457 | * The return value is an allocated string and can be NULL. |
| 3458 | */ |
| 3459 | char_u * |
| 3460 | get_file_in_dir(fname, dname) |
| 3461 | char_u *fname; |
| 3462 | char_u *dname; /* don't use "dirname", it is a global for Alpha */ |
| 3463 | { |
| 3464 | char_u *t; |
| 3465 | char_u *tail; |
| 3466 | char_u *retval; |
| 3467 | int save_char; |
| 3468 | |
| 3469 | tail = gettail(fname); |
| 3470 | |
| 3471 | if (dname[0] == '.' && dname[1] == NUL) |
| 3472 | retval = vim_strsave(fname); |
| 3473 | else if (dname[0] == '.' && vim_ispathsep(dname[1])) |
| 3474 | { |
| 3475 | if (tail == fname) /* no path before file name */ |
| 3476 | retval = concat_fnames(dname + 2, tail, TRUE); |
| 3477 | else |
| 3478 | { |
| 3479 | save_char = *tail; |
| 3480 | *tail = NUL; |
| 3481 | t = concat_fnames(fname, dname + 2, TRUE); |
| 3482 | *tail = save_char; |
| 3483 | if (t == NULL) /* out of memory */ |
| 3484 | retval = NULL; |
| 3485 | else |
| 3486 | { |
| 3487 | retval = concat_fnames(t, tail, TRUE); |
| 3488 | vim_free(t); |
| 3489 | } |
| 3490 | } |
| 3491 | } |
| 3492 | else |
| 3493 | retval = concat_fnames(dname, tail, TRUE); |
| 3494 | |
| 3495 | return retval; |
| 3496 | } |
| 3497 | |
| 3498 | /* |
| 3499 | * Find out what name to use for the swap file for buffer 'buf'. |
| 3500 | * |
| 3501 | * Several names are tried to find one that does not exist |
| 3502 | * |
| 3503 | * Note: If BASENAMELEN is not correct, you will get error messages for |
| 3504 | * not being able to open the swapfile |
| 3505 | */ |
| 3506 | static char_u * |
| 3507 | findswapname(buf, dirp, old_fname) |
| 3508 | buf_T *buf; |
| 3509 | char_u **dirp; /* pointer to list of directories */ |
| 3510 | char_u *old_fname; /* don't give warning for this file name */ |
| 3511 | { |
| 3512 | char_u *fname; |
| 3513 | int n; |
| 3514 | time_t x, sx; |
| 3515 | char_u *dir_name; |
| 3516 | #ifdef AMIGA |
| 3517 | BPTR fh; |
| 3518 | #endif |
| 3519 | #ifndef SHORT_FNAME |
| 3520 | int r; |
| 3521 | #endif |
| 3522 | |
| 3523 | #if !defined(SHORT_FNAME) \ |
| 3524 | && ((!defined(UNIX) && !defined(OS2)) || defined(ARCHIE)) |
| 3525 | # define CREATE_DUMMY_FILE |
| 3526 | FILE *dummyfd = NULL; |
| 3527 | |
| 3528 | /* |
| 3529 | * If we start editing a new file, e.g. "test.doc", which resides on an MSDOS |
| 3530 | * compatible filesystem, it is possible that the file "test.doc.swp" which we |
| 3531 | * create will be exactly the same file. To avoid this problem we temporarily |
| 3532 | * create "test.doc". |
| 3533 | * Don't do this when the check below for a 8.3 file name is used. |
| 3534 | */ |
| 3535 | if (!(buf->b_p_sn || buf->b_shortname) && buf->b_fname != NULL |
| 3536 | && mch_getperm(buf->b_fname) < 0) |
| 3537 | dummyfd = mch_fopen((char *)buf->b_fname, "w"); |
| 3538 | #endif |
| 3539 | |
| 3540 | /* |
| 3541 | * Isolate a directory name from *dirp and put it in dir_name. |
| 3542 | * First allocate some memory to put the directory name in. |
| 3543 | */ |
| 3544 | dir_name = alloc((unsigned)STRLEN(*dirp) + 1); |
| 3545 | if (dir_name != NULL) |
| 3546 | (void)copy_option_part(dirp, dir_name, 31000, ","); |
| 3547 | |
| 3548 | /* |
| 3549 | * we try different names until we find one that does not exist yet |
| 3550 | */ |
| 3551 | if (dir_name == NULL) /* out of memory */ |
| 3552 | fname = NULL; |
| 3553 | else |
| 3554 | fname = makeswapname(buf, dir_name); |
| 3555 | |
| 3556 | for (;;) |
| 3557 | { |
| 3558 | if (fname == NULL) /* must be out of memory */ |
| 3559 | break; |
| 3560 | if ((n = (int)STRLEN(fname)) == 0) /* safety check */ |
| 3561 | { |
| 3562 | vim_free(fname); |
| 3563 | fname = NULL; |
| 3564 | break; |
| 3565 | } |
| 3566 | #if (defined(UNIX) || defined(OS2)) && !defined(ARCHIE) && !defined(SHORT_FNAME) |
| 3567 | /* |
| 3568 | * Some systems have a MS-DOS compatible filesystem that use 8.3 character |
| 3569 | * file names. If this is the first try and the swap file name does not fit in |
| 3570 | * 8.3, detect if this is the case, set shortname and try again. |
| 3571 | */ |
| 3572 | if (fname[n - 2] == 'w' && fname[n - 1] == 'p' |
| 3573 | && !(buf->b_p_sn || buf->b_shortname)) |
| 3574 | { |
| 3575 | char_u *tail; |
| 3576 | char_u *fname2; |
| 3577 | struct stat s1, s2; |
| 3578 | int f1, f2; |
| 3579 | int created1 = FALSE, created2 = FALSE; |
| 3580 | int same = FALSE; |
| 3581 | |
| 3582 | /* |
| 3583 | * Check if swapfile name does not fit in 8.3: |
| 3584 | * It either contains two dots, is longer than 8 chars, or starts |
| 3585 | * with a dot. |
| 3586 | */ |
| 3587 | tail = gettail(buf->b_fname); |
| 3588 | if ( vim_strchr(tail, '.') != NULL |
| 3589 | || STRLEN(tail) > (size_t)8 |
| 3590 | || *gettail(fname) == '.') |
| 3591 | { |
| 3592 | fname2 = alloc(n + 2); |
| 3593 | if (fname2 != NULL) |
| 3594 | { |
| 3595 | STRCPY(fname2, fname); |
| 3596 | /* if fname == "xx.xx.swp", fname2 = "xx.xx.swx" |
| 3597 | * if fname == ".xx.swp", fname2 = ".xx.swpx" |
| 3598 | * if fname == "123456789.swp", fname2 = "12345678x.swp" |
| 3599 | */ |
| 3600 | if (vim_strchr(tail, '.') != NULL) |
| 3601 | fname2[n - 1] = 'x'; |
| 3602 | else if (*gettail(fname) == '.') |
| 3603 | { |
| 3604 | fname2[n] = 'x'; |
| 3605 | fname2[n + 1] = NUL; |
| 3606 | } |
| 3607 | else |
| 3608 | fname2[n - 5] += 1; |
| 3609 | /* |
| 3610 | * may need to create the files to be able to use mch_stat() |
| 3611 | */ |
| 3612 | f1 = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); |
| 3613 | if (f1 < 0) |
| 3614 | { |
| 3615 | f1 = mch_open_rw((char *)fname, |
| 3616 | O_RDWR|O_CREAT|O_EXCL|O_EXTRA); |
| 3617 | #if defined(OS2) |
| 3618 | if (f1 < 0 && errno == ENOENT) |
| 3619 | same = TRUE; |
| 3620 | #endif |
| 3621 | created1 = TRUE; |
| 3622 | } |
| 3623 | if (f1 >= 0) |
| 3624 | { |
| 3625 | f2 = mch_open((char *)fname2, O_RDONLY | O_EXTRA, 0); |
| 3626 | if (f2 < 0) |
| 3627 | { |
| 3628 | f2 = mch_open_rw((char *)fname2, |
| 3629 | O_RDWR|O_CREAT|O_EXCL|O_EXTRA); |
| 3630 | created2 = TRUE; |
| 3631 | } |
| 3632 | if (f2 >= 0) |
| 3633 | { |
| 3634 | /* |
| 3635 | * Both files exist now. If mch_stat() returns the |
| 3636 | * same device and inode they are the same file. |
| 3637 | */ |
| 3638 | if (mch_fstat(f1, &s1) != -1 |
| 3639 | && mch_fstat(f2, &s2) != -1 |
| 3640 | && s1.st_dev == s2.st_dev |
| 3641 | && s1.st_ino == s2.st_ino) |
| 3642 | same = TRUE; |
| 3643 | close(f2); |
| 3644 | if (created2) |
| 3645 | mch_remove(fname2); |
| 3646 | } |
| 3647 | close(f1); |
| 3648 | if (created1) |
| 3649 | mch_remove(fname); |
| 3650 | } |
| 3651 | vim_free(fname2); |
| 3652 | if (same) |
| 3653 | { |
| 3654 | buf->b_shortname = TRUE; |
| 3655 | vim_free(fname); |
| 3656 | fname = makeswapname(buf, dir_name); |
| 3657 | continue; /* try again with b_shortname set */ |
| 3658 | } |
| 3659 | } |
| 3660 | } |
| 3661 | } |
| 3662 | #endif |
| 3663 | /* |
| 3664 | * check if the swapfile already exists |
| 3665 | */ |
| 3666 | if (mch_getperm(fname) < 0) /* it does not exist */ |
| 3667 | { |
| 3668 | #ifdef HAVE_LSTAT |
| 3669 | struct stat sb; |
| 3670 | |
| 3671 | /* |
| 3672 | * Extra security check: When a swap file is a symbolic link, this |
| 3673 | * is most likely a symlink attack. |
| 3674 | */ |
| 3675 | if (mch_lstat((char *)fname, &sb) < 0) |
| 3676 | #else |
| 3677 | # ifdef AMIGA |
| 3678 | fh = Open((UBYTE *)fname, (long)MODE_NEWFILE); |
| 3679 | /* |
| 3680 | * on the Amiga mch_getperm() will return -1 when the file exists |
| 3681 | * but is being used by another program. This happens if you edit |
| 3682 | * a file twice. |
| 3683 | */ |
| 3684 | if (fh != (BPTR)NULL) /* can open file, OK */ |
| 3685 | { |
| 3686 | Close(fh); |
| 3687 | mch_remove(fname); |
| 3688 | break; |
| 3689 | } |
| 3690 | if (IoErr() != ERROR_OBJECT_IN_USE |
| 3691 | && IoErr() != ERROR_OBJECT_EXISTS) |
| 3692 | # endif |
| 3693 | #endif |
| 3694 | break; |
| 3695 | } |
| 3696 | |
| 3697 | /* |
| 3698 | * A file name equal to old_fname is OK to use. |
| 3699 | */ |
| 3700 | if (old_fname != NULL && fnamecmp(fname, old_fname) == 0) |
| 3701 | break; |
| 3702 | |
| 3703 | /* |
| 3704 | * get here when file already exists |
| 3705 | */ |
| 3706 | if (fname[n - 2] == 'w' && fname[n - 1] == 'p') /* first try */ |
| 3707 | { |
| 3708 | #ifndef SHORT_FNAME |
| 3709 | /* |
| 3710 | * on MS-DOS compatible filesystems (e.g. messydos) file.doc.swp |
| 3711 | * and file.doc are the same file. To guess if this problem is |
| 3712 | * present try if file.doc.swx exists. If it does, we set |
| 3713 | * buf->b_shortname and try file_doc.swp (dots replaced by |
| 3714 | * underscores for this file), and try again. If it doesn't we |
| 3715 | * assume that "file.doc.swp" already exists. |
| 3716 | */ |
| 3717 | if (!(buf->b_p_sn || buf->b_shortname)) /* not tried yet */ |
| 3718 | { |
| 3719 | fname[n - 1] = 'x'; |
| 3720 | r = mch_getperm(fname); /* try "file.swx" */ |
| 3721 | fname[n - 1] = 'p'; |
| 3722 | if (r >= 0) /* "file.swx" seems to exist */ |
| 3723 | { |
| 3724 | buf->b_shortname = TRUE; |
| 3725 | vim_free(fname); |
| 3726 | fname = makeswapname(buf, dir_name); |
| 3727 | continue; /* try again with '.' replaced with '_' */ |
| 3728 | } |
| 3729 | } |
| 3730 | #endif |
| 3731 | /* |
| 3732 | * If we get here the ".swp" file really exists. |
| 3733 | * Give an error message, unless recovering, no file name, we are |
| 3734 | * viewing a help file or when the path of the file is different |
| 3735 | * (happens when all .swp files are in one directory). |
| 3736 | */ |
Bram Moolenaar | 8fc061c | 2004-12-29 21:03:02 +0000 | [diff] [blame] | 3737 | if (!recoverymode && buf->b_fname != NULL |
| 3738 | && !buf->b_help && !(buf->b_flags & BF_DUMMY)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3739 | { |
| 3740 | int fd; |
| 3741 | struct block0 b0; |
| 3742 | int differ = FALSE; |
| 3743 | |
| 3744 | /* |
| 3745 | * Try to read block 0 from the swap file to get the original |
| 3746 | * file name (and inode number). |
| 3747 | */ |
| 3748 | fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); |
| 3749 | if (fd >= 0) |
| 3750 | { |
| 3751 | if (read(fd, (char *)&b0, sizeof(b0)) == sizeof(b0)) |
| 3752 | { |
| 3753 | /* |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 3754 | * If the swapfile has the same directory as the |
| 3755 | * buffer don't compare the directory names, they can |
| 3756 | * have a different mountpoint. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3757 | */ |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 3758 | if (b0.b0_flags & B0_SAME_DIR) |
| 3759 | { |
| 3760 | if (fnamecmp(gettail(buf->b_ffname), |
| 3761 | gettail(b0.b0_fname)) != 0 |
| 3762 | || !same_directory(fname, buf->b_ffname)) |
| 3763 | differ = TRUE; |
| 3764 | } |
| 3765 | else |
| 3766 | { |
| 3767 | /* |
| 3768 | * The name in the swap file may be |
| 3769 | * "~user/path/file". Expand it first. |
| 3770 | */ |
| 3771 | expand_env(b0.b0_fname, NameBuff, MAXPATHL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3772 | #ifdef CHECK_INODE |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 3773 | if (fnamecmp_ino(buf->b_ffname, NameBuff, |
| 3774 | char_to_long(b0.b0_ino))) |
| 3775 | differ = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3776 | #else |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 3777 | if (fnamecmp(NameBuff, buf->b_ffname) != 0) |
| 3778 | differ = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3779 | #endif |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 3780 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3781 | } |
| 3782 | close(fd); |
| 3783 | } |
| 3784 | #ifdef RISCOS |
| 3785 | else |
| 3786 | /* Can't open swap file, though it does exist. |
| 3787 | * Assume that the user is editing two files with |
| 3788 | * the same name in different directories. No error. |
| 3789 | */ |
| 3790 | differ = TRUE; |
| 3791 | #endif |
| 3792 | |
| 3793 | /* give the ATTENTION message when there is an old swap file |
| 3794 | * for the current file, and the buffer was not recovered. */ |
| 3795 | if (differ == FALSE && !(curbuf->b_flags & BF_RECOVERED) |
| 3796 | && vim_strchr(p_shm, SHM_ATTENTION) == NULL) |
| 3797 | { |
| 3798 | struct stat st; |
| 3799 | #ifdef CREATE_DUMMY_FILE |
| 3800 | int did_use_dummy = FALSE; |
| 3801 | |
| 3802 | /* Avoid getting a warning for the file being created |
| 3803 | * outside of Vim, it was created at the start of this |
| 3804 | * function. Delete the file now, because Vim might exit |
| 3805 | * here if the window is closed. */ |
| 3806 | if (dummyfd != NULL) |
| 3807 | { |
| 3808 | fclose(dummyfd); |
| 3809 | dummyfd = NULL; |
| 3810 | mch_remove(buf->b_fname); |
| 3811 | did_use_dummy = TRUE; |
| 3812 | } |
| 3813 | #endif |
| 3814 | #ifdef FEAT_GUI |
| 3815 | /* If we are supposed to start the GUI but it wasn't |
| 3816 | * completely started yet, start it now. This makes the |
| 3817 | * messages displayed in the Vim window when loading a |
| 3818 | * session from the .gvimrc file. */ |
| 3819 | if (gui.starting && !gui.in_use) |
| 3820 | gui_start(); |
| 3821 | #endif |
| 3822 | |
| 3823 | #if (defined(UNIX) || defined(__EMX__) || defined(VMS)) && (defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)) |
| 3824 | process_still_running = FALSE; |
| 3825 | #endif |
| 3826 | ++no_wait_return; |
| 3827 | (void)EMSG(_("E325: ATTENTION")); |
| 3828 | MSG_PUTS(_("\nFound a swap file by the name \"")); |
| 3829 | msg_home_replace(fname); |
| 3830 | MSG_PUTS("\"\n"); |
| 3831 | sx = swapfile_info(fname); |
| 3832 | MSG_PUTS(_("While opening file \"")); |
| 3833 | msg_outtrans(buf->b_fname); |
| 3834 | MSG_PUTS("\"\n"); |
| 3835 | if (mch_stat((char *)buf->b_fname, &st) != -1) |
| 3836 | { |
| 3837 | MSG_PUTS(_(" dated: ")); |
| 3838 | x = st.st_mtime; /* Manx C can't do &st.st_mtime */ |
| 3839 | MSG_PUTS(ctime(&x)); |
| 3840 | if (sx != 0 && x > sx) |
| 3841 | MSG_PUTS(_(" NEWER than swap file!\n")); |
| 3842 | } |
| 3843 | /* Some of these messages are long to allow translation to |
| 3844 | * other languages. */ |
| 3845 | MSG_PUTS(_("\n(1) Another program may be editing the same file.\n If this is the case, be careful not to end up with two\n different instances of the same file when making changes.\n")); |
| 3846 | MSG_PUTS(_(" Quit, or continue with caution.\n")); |
| 3847 | MSG_PUTS(_("\n(2) An edit session for this file crashed.\n")); |
| 3848 | MSG_PUTS(_(" If this is the case, use \":recover\" or \"vim -r ")); |
| 3849 | msg_outtrans(buf->b_fname); |
| 3850 | MSG_PUTS(_("\"\n to recover the changes (see \":help recovery\").\n")); |
| 3851 | MSG_PUTS(_(" If you did this already, delete the swap file \"")); |
| 3852 | msg_outtrans(fname); |
| 3853 | MSG_PUTS(_("\"\n to avoid this message.\n")); |
| 3854 | cmdline_row = msg_row; |
| 3855 | --no_wait_return; |
| 3856 | |
| 3857 | /* We don't want a 'q' typed at the more-prompt interrupt |
| 3858 | * loading a file. */ |
| 3859 | got_int = FALSE; |
| 3860 | |
| 3861 | #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) |
| 3862 | if (swap_exists_action) |
| 3863 | { |
| 3864 | char_u *name; |
| 3865 | |
| 3866 | name = alloc((unsigned)(STRLEN(fname) |
| 3867 | + STRLEN(_("Swap file \"")) |
| 3868 | + STRLEN(_("\" already exists!")) + 5)); |
| 3869 | if (name != NULL) |
| 3870 | { |
| 3871 | STRCPY(name, _("Swap file \"")); |
| 3872 | home_replace(NULL, fname, name + STRLEN(name), |
| 3873 | 1000, TRUE); |
| 3874 | STRCAT(name, _("\" already exists!")); |
| 3875 | } |
| 3876 | switch (do_dialog(VIM_WARNING, |
| 3877 | (char_u *)_("VIM - ATTENTION"), |
| 3878 | name == NULL |
| 3879 | ? (char_u *)_("Swap file already exists!") |
| 3880 | : name, |
| 3881 | # if defined(UNIX) || defined(__EMX__) || defined(VMS) |
| 3882 | process_still_running |
| 3883 | ? (char_u *)_("&Open Read-Only\n&Edit anyway\n&Recover\n&Quit\n&Abort") : |
| 3884 | # endif |
| 3885 | (char_u *)_("&Open Read-Only\n&Edit anyway\n&Recover\n&Quit\n&Abort\n&Delete it"), 1, NULL)) |
| 3886 | { |
| 3887 | case 1: |
| 3888 | buf->b_p_ro = TRUE; |
| 3889 | break; |
| 3890 | case 2: |
| 3891 | break; |
| 3892 | case 3: |
| 3893 | swap_exists_action = SEA_RECOVER; |
| 3894 | break; |
| 3895 | case 4: |
| 3896 | swap_exists_action = SEA_QUIT; |
| 3897 | break; |
| 3898 | case 5: |
| 3899 | swap_exists_action = SEA_QUIT; |
| 3900 | got_int = TRUE; |
| 3901 | break; |
| 3902 | case 6: |
| 3903 | mch_remove(fname); |
| 3904 | break; |
| 3905 | } |
| 3906 | vim_free(name); |
| 3907 | |
| 3908 | /* pretend screen didn't scroll, need redraw anyway */ |
| 3909 | msg_scrolled = 0; |
| 3910 | redraw_all_later(NOT_VALID); |
| 3911 | |
| 3912 | /* If the file was deleted this fname can be used. */ |
| 3913 | if (mch_getperm(fname) < 0) |
| 3914 | break; |
| 3915 | } |
| 3916 | else |
| 3917 | #endif |
| 3918 | { |
| 3919 | MSG_PUTS("\n"); |
| 3920 | need_wait_return = TRUE; /* call wait_return later */ |
| 3921 | } |
| 3922 | |
| 3923 | #ifdef CREATE_DUMMY_FILE |
| 3924 | /* Going to try another name, need the dummy file again. */ |
| 3925 | if (did_use_dummy) |
| 3926 | dummyfd = mch_fopen((char *)buf->b_fname, "w"); |
| 3927 | #endif |
| 3928 | } |
| 3929 | } |
| 3930 | } |
| 3931 | |
| 3932 | /* |
| 3933 | * Change the ".swp" extension to find another file that can be used. |
| 3934 | * First decrement the last char: ".swo", ".swn", etc. |
| 3935 | * If that still isn't enough decrement the last but one char: ".svz" |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 3936 | * Can happen when editing many "No Name" buffers. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3937 | */ |
| 3938 | if (fname[n - 1] == 'a') /* ".s?a" */ |
| 3939 | { |
| 3940 | if (fname[n - 2] == 'a') /* ".saa": tried enough, give up */ |
| 3941 | { |
| 3942 | EMSG(_("E326: Too many swap files found")); |
| 3943 | vim_free(fname); |
| 3944 | fname = NULL; |
| 3945 | break; |
| 3946 | } |
| 3947 | --fname[n - 2]; /* ".svz", ".suz", etc. */ |
| 3948 | fname[n - 1] = 'z' + 1; |
| 3949 | } |
| 3950 | --fname[n - 1]; /* ".swo", ".swn", etc. */ |
| 3951 | } |
| 3952 | |
| 3953 | vim_free(dir_name); |
| 3954 | #ifdef CREATE_DUMMY_FILE |
| 3955 | if (dummyfd != NULL) /* file has been created temporarily */ |
| 3956 | { |
| 3957 | fclose(dummyfd); |
| 3958 | mch_remove(buf->b_fname); |
| 3959 | } |
| 3960 | #endif |
| 3961 | return fname; |
| 3962 | } |
| 3963 | |
| 3964 | static int |
| 3965 | b0_magic_wrong(b0p) |
| 3966 | ZERO_BL *b0p; |
| 3967 | { |
| 3968 | return (b0p->b0_magic_long != (long)B0_MAGIC_LONG |
| 3969 | || b0p->b0_magic_int != (int)B0_MAGIC_INT |
| 3970 | || b0p->b0_magic_short != (short)B0_MAGIC_SHORT |
| 3971 | || b0p->b0_magic_char != B0_MAGIC_CHAR); |
| 3972 | } |
| 3973 | |
| 3974 | #ifdef CHECK_INODE |
| 3975 | /* |
| 3976 | * Compare current file name with file name from swap file. |
| 3977 | * Try to use inode numbers when possible. |
| 3978 | * Return non-zero when files are different. |
| 3979 | * |
| 3980 | * When comparing file names a few things have to be taken into consideration: |
| 3981 | * - When working over a network the full path of a file depends on the host. |
| 3982 | * We check the inode number if possible. It is not 100% reliable though, |
| 3983 | * because the device number cannot be used over a network. |
| 3984 | * - When a file does not exist yet (editing a new file) there is no inode |
| 3985 | * number. |
| 3986 | * - The file name in a swap file may not be valid on the current host. The |
| 3987 | * "~user" form is used whenever possible to avoid this. |
| 3988 | * |
| 3989 | * This is getting complicated, let's make a table: |
| 3990 | * |
| 3991 | * ino_c ino_s fname_c fname_s differ = |
| 3992 | * |
| 3993 | * both files exist -> compare inode numbers: |
| 3994 | * != 0 != 0 X X ino_c != ino_s |
| 3995 | * |
| 3996 | * inode number(s) unknown, file names available -> compare file names |
| 3997 | * == 0 X OK OK fname_c != fname_s |
| 3998 | * X == 0 OK OK fname_c != fname_s |
| 3999 | * |
| 4000 | * current file doesn't exist, file for swap file exist, file name(s) not |
| 4001 | * available -> probably different |
| 4002 | * == 0 != 0 FAIL X TRUE |
| 4003 | * == 0 != 0 X FAIL TRUE |
| 4004 | * |
| 4005 | * current file exists, inode for swap unknown, file name(s) not |
| 4006 | * available -> probably different |
| 4007 | * != 0 == 0 FAIL X TRUE |
| 4008 | * != 0 == 0 X FAIL TRUE |
| 4009 | * |
| 4010 | * current file doesn't exist, inode for swap unknown, one file name not |
| 4011 | * available -> probably different |
| 4012 | * == 0 == 0 FAIL OK TRUE |
| 4013 | * == 0 == 0 OK FAIL TRUE |
| 4014 | * |
| 4015 | * current file doesn't exist, inode for swap unknown, both file names not |
| 4016 | * available -> probably same file |
| 4017 | * == 0 == 0 FAIL FAIL FALSE |
| 4018 | * |
| 4019 | * Note that when the ino_t is 64 bits, only the last 32 will be used. This |
| 4020 | * can't be changed without making the block 0 incompatible with 32 bit |
| 4021 | * versions. |
| 4022 | */ |
| 4023 | |
| 4024 | static int |
| 4025 | fnamecmp_ino(fname_c, fname_s, ino_block0) |
| 4026 | char_u *fname_c; /* current file name */ |
| 4027 | char_u *fname_s; /* file name from swap file */ |
| 4028 | long ino_block0; |
| 4029 | { |
| 4030 | struct stat st; |
| 4031 | ino_t ino_c = 0; /* ino of current file */ |
| 4032 | ino_t ino_s; /* ino of file from swap file */ |
| 4033 | char_u buf_c[MAXPATHL]; /* full path of fname_c */ |
| 4034 | char_u buf_s[MAXPATHL]; /* full path of fname_s */ |
| 4035 | int retval_c; /* flag: buf_c valid */ |
| 4036 | int retval_s; /* flag: buf_s valid */ |
| 4037 | |
| 4038 | if (mch_stat((char *)fname_c, &st) == 0) |
| 4039 | ino_c = (ino_t)st.st_ino; |
| 4040 | |
| 4041 | /* |
| 4042 | * First we try to get the inode from the file name, because the inode in |
| 4043 | * the swap file may be outdated. If that fails (e.g. this path is not |
| 4044 | * valid on this machine), use the inode from block 0. |
| 4045 | */ |
| 4046 | if (mch_stat((char *)fname_s, &st) == 0) |
| 4047 | ino_s = (ino_t)st.st_ino; |
| 4048 | else |
| 4049 | ino_s = (ino_t)ino_block0; |
| 4050 | |
| 4051 | if (ino_c && ino_s) |
| 4052 | return (ino_c != ino_s); |
| 4053 | |
| 4054 | /* |
| 4055 | * One of the inode numbers is unknown, try a forced vim_FullName() and |
| 4056 | * compare the file names. |
| 4057 | */ |
| 4058 | retval_c = vim_FullName(fname_c, buf_c, MAXPATHL, TRUE); |
| 4059 | retval_s = vim_FullName(fname_s, buf_s, MAXPATHL, TRUE); |
| 4060 | if (retval_c == OK && retval_s == OK) |
| 4061 | return (STRCMP(buf_c, buf_s) != 0); |
| 4062 | |
| 4063 | /* |
| 4064 | * Can't compare inodes or file names, guess that the files are different, |
| 4065 | * unless both appear not to exist at all. |
| 4066 | */ |
| 4067 | if (ino_s == 0 && ino_c == 0 && retval_c == FAIL && retval_s == FAIL) |
| 4068 | return FALSE; |
| 4069 | return TRUE; |
| 4070 | } |
| 4071 | #endif /* CHECK_INODE */ |
| 4072 | |
| 4073 | /* |
| 4074 | * Move a long integer into a four byte character array. |
| 4075 | * Used for machine independency in block zero. |
| 4076 | */ |
| 4077 | static void |
| 4078 | long_to_char(n, s) |
| 4079 | long n; |
| 4080 | char_u *s; |
| 4081 | { |
| 4082 | s[0] = (char_u)(n & 0xff); |
| 4083 | n = (unsigned)n >> 8; |
| 4084 | s[1] = (char_u)(n & 0xff); |
| 4085 | n = (unsigned)n >> 8; |
| 4086 | s[2] = (char_u)(n & 0xff); |
| 4087 | n = (unsigned)n >> 8; |
| 4088 | s[3] = (char_u)(n & 0xff); |
| 4089 | } |
| 4090 | |
| 4091 | static long |
| 4092 | char_to_long(s) |
| 4093 | char_u *s; |
| 4094 | { |
| 4095 | long retval; |
| 4096 | |
| 4097 | retval = s[3]; |
| 4098 | retval <<= 8; |
| 4099 | retval |= s[2]; |
| 4100 | retval <<= 8; |
| 4101 | retval |= s[1]; |
| 4102 | retval <<= 8; |
| 4103 | retval |= s[0]; |
| 4104 | |
| 4105 | return retval; |
| 4106 | } |
| 4107 | |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4108 | /* |
| 4109 | * Set the flags in the first block of the swap file: |
| 4110 | * - file is modified or not: buf->b_changed |
| 4111 | * - 'fileformat' |
| 4112 | * - 'fileencoding' |
| 4113 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4114 | void |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4115 | ml_setflags(buf) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4116 | buf_T *buf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4117 | { |
| 4118 | bhdr_T *hp; |
| 4119 | ZERO_BL *b0p; |
| 4120 | |
| 4121 | if (!buf->b_ml.ml_mfp) |
| 4122 | return; |
| 4123 | for (hp = buf->b_ml.ml_mfp->mf_used_last; hp != NULL; hp = hp->bh_prev) |
| 4124 | { |
| 4125 | if (hp->bh_bnum == 0) |
| 4126 | { |
| 4127 | b0p = (ZERO_BL *)(hp->bh_data); |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4128 | b0p->b0_dirty = buf->b_changed ? B0_DIRTY : 0; |
| 4129 | b0p->b0_flags = (b0p->b0_flags & ~B0_FF_MASK) |
| 4130 | | (get_fileformat(buf) + 1); |
| 4131 | #ifdef FEAT_MBYTE |
| 4132 | add_b0_fenc(b0p, buf); |
| 4133 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4134 | hp->bh_flags |= BH_DIRTY; |
| 4135 | mf_sync(buf->b_ml.ml_mfp, MFS_ZERO); |
| 4136 | break; |
| 4137 | } |
| 4138 | } |
| 4139 | } |
| 4140 | |
| 4141 | #if defined(FEAT_BYTEOFF) || defined(PROTO) |
| 4142 | |
| 4143 | #define MLCS_MAXL 800 /* max no of lines in chunk */ |
| 4144 | #define MLCS_MINL 400 /* should be half of MLCS_MAXL */ |
| 4145 | |
| 4146 | /* |
| 4147 | * Keep information for finding byte offset of a line, updtytpe may be one of: |
| 4148 | * ML_CHNK_ADDLINE: Add len to parent chunk, possibly splitting it |
| 4149 | * Careful: ML_CHNK_ADDLINE may cause ml_find_line() to be called. |
| 4150 | * ML_CHNK_DELLINE: Subtract len from parent chunk, possibly deleting it |
| 4151 | * ML_CHNK_UPDLINE: Add len to parent chunk, as a signed entity. |
| 4152 | */ |
| 4153 | static void |
| 4154 | ml_updatechunk(buf, line, len, updtype) |
| 4155 | buf_T *buf; |
| 4156 | linenr_T line; |
| 4157 | long len; |
| 4158 | int updtype; |
| 4159 | { |
| 4160 | static buf_T *ml_upd_lastbuf = NULL; |
| 4161 | static linenr_T ml_upd_lastline; |
| 4162 | static linenr_T ml_upd_lastcurline; |
| 4163 | static int ml_upd_lastcurix; |
| 4164 | |
| 4165 | linenr_T curline = ml_upd_lastcurline; |
| 4166 | int curix = ml_upd_lastcurix; |
| 4167 | long size; |
| 4168 | chunksize_T *curchnk; |
| 4169 | int rest; |
| 4170 | bhdr_T *hp; |
| 4171 | DATA_BL *dp; |
| 4172 | |
| 4173 | if (buf->b_ml.ml_usedchunks == -1 || len == 0) |
| 4174 | return; |
| 4175 | if (buf->b_ml.ml_chunksize == NULL) |
| 4176 | { |
| 4177 | buf->b_ml.ml_chunksize = (chunksize_T *) |
| 4178 | alloc((unsigned)sizeof(chunksize_T) * 100); |
| 4179 | if (buf->b_ml.ml_chunksize == NULL) |
| 4180 | { |
| 4181 | buf->b_ml.ml_usedchunks = -1; |
| 4182 | return; |
| 4183 | } |
| 4184 | buf->b_ml.ml_numchunks = 100; |
| 4185 | buf->b_ml.ml_usedchunks = 1; |
| 4186 | buf->b_ml.ml_chunksize[0].mlcs_numlines = 1; |
| 4187 | buf->b_ml.ml_chunksize[0].mlcs_totalsize = 1; |
| 4188 | } |
| 4189 | |
| 4190 | if (updtype == ML_CHNK_UPDLINE && buf->b_ml.ml_line_count == 1) |
| 4191 | { |
| 4192 | /* |
| 4193 | * First line in empty buffer from ml_flush_line() -- reset |
| 4194 | */ |
| 4195 | buf->b_ml.ml_usedchunks = 1; |
| 4196 | buf->b_ml.ml_chunksize[0].mlcs_numlines = 1; |
| 4197 | buf->b_ml.ml_chunksize[0].mlcs_totalsize = |
| 4198 | (long)STRLEN(buf->b_ml.ml_line_ptr) + 1; |
| 4199 | return; |
| 4200 | } |
| 4201 | |
| 4202 | /* |
| 4203 | * Find chunk that our line belongs to, curline will be at start of the |
| 4204 | * chunk. |
| 4205 | */ |
| 4206 | if (buf != ml_upd_lastbuf || line != ml_upd_lastline + 1 |
| 4207 | || updtype != ML_CHNK_ADDLINE) |
| 4208 | { |
| 4209 | for (curline = 1, curix = 0; |
| 4210 | curix < buf->b_ml.ml_usedchunks - 1 |
| 4211 | && line >= curline + buf->b_ml.ml_chunksize[curix].mlcs_numlines; |
| 4212 | curix++) |
| 4213 | { |
| 4214 | curline += buf->b_ml.ml_chunksize[curix].mlcs_numlines; |
| 4215 | } |
| 4216 | } |
| 4217 | else if (line >= curline + buf->b_ml.ml_chunksize[curix].mlcs_numlines |
| 4218 | && curix < buf->b_ml.ml_usedchunks - 1) |
| 4219 | { |
| 4220 | /* Adjust cached curix & curline */ |
| 4221 | curline += buf->b_ml.ml_chunksize[curix].mlcs_numlines; |
| 4222 | curix++; |
| 4223 | } |
| 4224 | curchnk = buf->b_ml.ml_chunksize + curix; |
| 4225 | |
| 4226 | if (updtype == ML_CHNK_DELLINE) |
| 4227 | len *= -1; |
| 4228 | curchnk->mlcs_totalsize += len; |
| 4229 | if (updtype == ML_CHNK_ADDLINE) |
| 4230 | { |
| 4231 | curchnk->mlcs_numlines++; |
| 4232 | |
| 4233 | /* May resize here so we don't have to do it in both cases below */ |
| 4234 | if (buf->b_ml.ml_usedchunks + 1 >= buf->b_ml.ml_numchunks) |
| 4235 | { |
| 4236 | buf->b_ml.ml_numchunks = buf->b_ml.ml_numchunks * 3 / 2; |
| 4237 | buf->b_ml.ml_chunksize = (chunksize_T *) |
| 4238 | vim_realloc(buf->b_ml.ml_chunksize, |
| 4239 | sizeof(chunksize_T) * buf->b_ml.ml_numchunks); |
| 4240 | if (buf->b_ml.ml_chunksize == NULL) |
| 4241 | { |
| 4242 | /* Hmmmm, Give up on offset for this buffer */ |
| 4243 | buf->b_ml.ml_usedchunks = -1; |
| 4244 | return; |
| 4245 | } |
| 4246 | } |
| 4247 | |
| 4248 | if (buf->b_ml.ml_chunksize[curix].mlcs_numlines >= MLCS_MAXL) |
| 4249 | { |
| 4250 | int count; /* number of entries in block */ |
| 4251 | int idx; |
| 4252 | int text_end; |
| 4253 | int linecnt; |
| 4254 | |
| 4255 | mch_memmove(buf->b_ml.ml_chunksize + curix + 1, |
| 4256 | buf->b_ml.ml_chunksize + curix, |
| 4257 | (buf->b_ml.ml_usedchunks - curix) * |
| 4258 | sizeof(chunksize_T)); |
| 4259 | /* Compute length of first half of lines in the splitted chunk */ |
| 4260 | size = 0; |
| 4261 | linecnt = 0; |
| 4262 | while (curline < buf->b_ml.ml_line_count |
| 4263 | && linecnt < MLCS_MINL) |
| 4264 | { |
| 4265 | if ((hp = ml_find_line(buf, curline, ML_FIND)) == NULL) |
| 4266 | { |
| 4267 | buf->b_ml.ml_usedchunks = -1; |
| 4268 | return; |
| 4269 | } |
| 4270 | dp = (DATA_BL *)(hp->bh_data); |
| 4271 | count = (long)(buf->b_ml.ml_locked_high) - |
| 4272 | (long)(buf->b_ml.ml_locked_low) + 1; |
| 4273 | idx = curline - buf->b_ml.ml_locked_low; |
| 4274 | curline = buf->b_ml.ml_locked_high + 1; |
| 4275 | if (idx == 0)/* first line in block, text at the end */ |
| 4276 | text_end = dp->db_txt_end; |
| 4277 | else |
| 4278 | text_end = ((dp->db_index[idx - 1]) & DB_INDEX_MASK); |
| 4279 | /* Compute index of last line to use in this MEMLINE */ |
| 4280 | rest = count - idx; |
| 4281 | if (linecnt + rest > MLCS_MINL) |
| 4282 | { |
| 4283 | idx += MLCS_MINL - linecnt - 1; |
| 4284 | linecnt = MLCS_MINL; |
| 4285 | } |
| 4286 | else |
| 4287 | { |
| 4288 | idx = count - 1; |
| 4289 | linecnt += rest; |
| 4290 | } |
| 4291 | size += text_end - ((dp->db_index[idx]) & DB_INDEX_MASK); |
| 4292 | } |
| 4293 | buf->b_ml.ml_chunksize[curix].mlcs_numlines = linecnt; |
| 4294 | buf->b_ml.ml_chunksize[curix + 1].mlcs_numlines -= linecnt; |
| 4295 | buf->b_ml.ml_chunksize[curix].mlcs_totalsize = size; |
| 4296 | buf->b_ml.ml_chunksize[curix + 1].mlcs_totalsize -= size; |
| 4297 | buf->b_ml.ml_usedchunks++; |
| 4298 | ml_upd_lastbuf = NULL; /* Force recalc of curix & curline */ |
| 4299 | return; |
| 4300 | } |
| 4301 | else if (buf->b_ml.ml_chunksize[curix].mlcs_numlines >= MLCS_MINL |
| 4302 | && curix == buf->b_ml.ml_usedchunks - 1 |
| 4303 | && buf->b_ml.ml_line_count - line <= 1) |
| 4304 | { |
| 4305 | /* |
| 4306 | * We are in the last chunk and it is cheap to crate a new one |
| 4307 | * after this. Do it now to avoid the loop above later on |
| 4308 | */ |
| 4309 | curchnk = buf->b_ml.ml_chunksize + curix + 1; |
| 4310 | buf->b_ml.ml_usedchunks++; |
| 4311 | if (line == buf->b_ml.ml_line_count) |
| 4312 | { |
| 4313 | curchnk->mlcs_numlines = 0; |
| 4314 | curchnk->mlcs_totalsize = 0; |
| 4315 | } |
| 4316 | else |
| 4317 | { |
| 4318 | /* |
| 4319 | * Line is just prior to last, move count for last |
| 4320 | * This is the common case when loading a new file |
| 4321 | */ |
| 4322 | hp = ml_find_line(buf, buf->b_ml.ml_line_count, ML_FIND); |
| 4323 | if (hp == NULL) |
| 4324 | { |
| 4325 | buf->b_ml.ml_usedchunks = -1; |
| 4326 | return; |
| 4327 | } |
| 4328 | dp = (DATA_BL *)(hp->bh_data); |
| 4329 | if (dp->db_line_count == 1) |
| 4330 | rest = dp->db_txt_end - dp->db_txt_start; |
| 4331 | else |
| 4332 | rest = |
| 4333 | ((dp->db_index[dp->db_line_count - 2]) & DB_INDEX_MASK) |
| 4334 | - dp->db_txt_start; |
| 4335 | curchnk->mlcs_totalsize = rest; |
| 4336 | curchnk->mlcs_numlines = 1; |
| 4337 | curchnk[-1].mlcs_totalsize -= rest; |
| 4338 | curchnk[-1].mlcs_numlines -= 1; |
| 4339 | } |
| 4340 | } |
| 4341 | } |
| 4342 | else if (updtype == ML_CHNK_DELLINE) |
| 4343 | { |
| 4344 | curchnk->mlcs_numlines--; |
| 4345 | ml_upd_lastbuf = NULL; /* Force recalc of curix & curline */ |
| 4346 | if (curix < (buf->b_ml.ml_usedchunks - 1) |
| 4347 | && (curchnk->mlcs_numlines + curchnk[1].mlcs_numlines) |
| 4348 | <= MLCS_MINL) |
| 4349 | { |
| 4350 | curix++; |
| 4351 | curchnk = buf->b_ml.ml_chunksize + curix; |
| 4352 | } |
| 4353 | else if (curix == 0 && curchnk->mlcs_numlines <= 0) |
| 4354 | { |
| 4355 | buf->b_ml.ml_usedchunks--; |
| 4356 | mch_memmove(buf->b_ml.ml_chunksize, buf->b_ml.ml_chunksize + 1, |
| 4357 | buf->b_ml.ml_usedchunks * sizeof(chunksize_T)); |
| 4358 | return; |
| 4359 | } |
| 4360 | else if (curix == 0 || (curchnk->mlcs_numlines > 10 |
| 4361 | && (curchnk->mlcs_numlines + curchnk[-1].mlcs_numlines) |
| 4362 | > MLCS_MINL)) |
| 4363 | { |
| 4364 | return; |
| 4365 | } |
| 4366 | |
| 4367 | /* Collapse chunks */ |
| 4368 | curchnk[-1].mlcs_numlines += curchnk->mlcs_numlines; |
| 4369 | curchnk[-1].mlcs_totalsize += curchnk->mlcs_totalsize; |
| 4370 | buf->b_ml.ml_usedchunks--; |
| 4371 | if (curix < buf->b_ml.ml_usedchunks) |
| 4372 | { |
| 4373 | mch_memmove(buf->b_ml.ml_chunksize + curix, |
| 4374 | buf->b_ml.ml_chunksize + curix + 1, |
| 4375 | (buf->b_ml.ml_usedchunks - curix) * |
| 4376 | sizeof(chunksize_T)); |
| 4377 | } |
| 4378 | return; |
| 4379 | } |
| 4380 | ml_upd_lastbuf = buf; |
| 4381 | ml_upd_lastline = line; |
| 4382 | ml_upd_lastcurline = curline; |
| 4383 | ml_upd_lastcurix = curix; |
| 4384 | } |
| 4385 | |
| 4386 | /* |
| 4387 | * Find offset for line or line with offset. |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4388 | * Find line with offset if "lnum" is 0; return remaining offset in offp |
| 4389 | * Find offset of line if "lnum" > 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4390 | * return -1 if information is not available |
| 4391 | */ |
| 4392 | long |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4393 | ml_find_line_or_offset(buf, lnum, offp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4394 | buf_T *buf; |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4395 | linenr_T lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4396 | long *offp; |
| 4397 | { |
| 4398 | linenr_T curline; |
| 4399 | int curix; |
| 4400 | long size; |
| 4401 | bhdr_T *hp; |
| 4402 | DATA_BL *dp; |
| 4403 | int count; /* number of entries in block */ |
| 4404 | int idx; |
| 4405 | int start_idx; |
| 4406 | int text_end; |
| 4407 | long offset; |
| 4408 | int len; |
| 4409 | int ffdos = (get_fileformat(buf) == EOL_DOS); |
| 4410 | int extra = 0; |
| 4411 | |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4412 | /* take care of cached line first */ |
| 4413 | ml_flush_line(curbuf); |
| 4414 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4415 | if (buf->b_ml.ml_usedchunks == -1 |
| 4416 | || buf->b_ml.ml_chunksize == NULL |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4417 | || lnum < 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4418 | return -1; |
| 4419 | |
| 4420 | if (offp == NULL) |
| 4421 | offset = 0; |
| 4422 | else |
| 4423 | offset = *offp; |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4424 | if (lnum == 0 && offset <= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4425 | return 1; /* Not a "find offset" and offset 0 _must_ be in line 1 */ |
| 4426 | /* |
| 4427 | * Find the last chunk before the one containing our line. Last chunk is |
| 4428 | * special because it will never qualify |
| 4429 | */ |
| 4430 | curline = 1; |
| 4431 | curix = size = 0; |
| 4432 | while (curix < buf->b_ml.ml_usedchunks - 1 |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4433 | && ((lnum != 0 |
| 4434 | && lnum >= curline + buf->b_ml.ml_chunksize[curix].mlcs_numlines) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4435 | || (offset != 0 |
| 4436 | && offset > size + buf->b_ml.ml_chunksize[curix].mlcs_totalsize |
| 4437 | + ffdos * buf->b_ml.ml_chunksize[curix].mlcs_numlines))) |
| 4438 | { |
| 4439 | curline += buf->b_ml.ml_chunksize[curix].mlcs_numlines; |
| 4440 | size += buf->b_ml.ml_chunksize[curix].mlcs_totalsize; |
| 4441 | if (offset && ffdos) |
| 4442 | size += buf->b_ml.ml_chunksize[curix].mlcs_numlines; |
| 4443 | curix++; |
| 4444 | } |
| 4445 | |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4446 | while ((lnum != 0 && curline < lnum) || (offset != 0 && size < offset)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4447 | { |
| 4448 | if (curline > buf->b_ml.ml_line_count |
| 4449 | || (hp = ml_find_line(buf, curline, ML_FIND)) == NULL) |
| 4450 | return -1; |
| 4451 | dp = (DATA_BL *)(hp->bh_data); |
| 4452 | count = (long)(buf->b_ml.ml_locked_high) - |
| 4453 | (long)(buf->b_ml.ml_locked_low) + 1; |
| 4454 | start_idx = idx = curline - buf->b_ml.ml_locked_low; |
| 4455 | if (idx == 0)/* first line in block, text at the end */ |
| 4456 | text_end = dp->db_txt_end; |
| 4457 | else |
| 4458 | text_end = ((dp->db_index[idx - 1]) & DB_INDEX_MASK); |
| 4459 | /* Compute index of last line to use in this MEMLINE */ |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4460 | if (lnum != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4461 | { |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4462 | if (curline + (count - idx) >= lnum) |
| 4463 | idx += lnum - curline - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4464 | else |
| 4465 | idx = count - 1; |
| 4466 | } |
| 4467 | else |
| 4468 | { |
| 4469 | extra = 0; |
| 4470 | while (offset >= size |
| 4471 | + text_end - (int)((dp->db_index[idx]) & DB_INDEX_MASK) |
| 4472 | + ffdos) |
| 4473 | { |
| 4474 | if (ffdos) |
| 4475 | size++; |
| 4476 | if (idx == count - 1) |
| 4477 | { |
| 4478 | extra = 1; |
| 4479 | break; |
| 4480 | } |
| 4481 | idx++; |
| 4482 | } |
| 4483 | } |
| 4484 | len = text_end - ((dp->db_index[idx]) & DB_INDEX_MASK); |
| 4485 | size += len; |
| 4486 | if (offset != 0 && size >= offset) |
| 4487 | { |
| 4488 | if (size + ffdos == offset) |
| 4489 | *offp = 0; |
| 4490 | else if (idx == start_idx) |
| 4491 | *offp = offset - size + len; |
| 4492 | else |
| 4493 | *offp = offset - size + len |
| 4494 | - (text_end - ((dp->db_index[idx - 1]) & DB_INDEX_MASK)); |
| 4495 | curline += idx - start_idx + extra; |
| 4496 | if (curline > buf->b_ml.ml_line_count) |
| 4497 | return -1; /* exactly one byte beyond the end */ |
| 4498 | return curline; |
| 4499 | } |
| 4500 | curline = buf->b_ml.ml_locked_high + 1; |
| 4501 | } |
| 4502 | |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4503 | if (lnum != 0) |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 4504 | { |
| 4505 | /* Count extra CR characters. */ |
| 4506 | if (ffdos) |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 4507 | size += lnum - 1; |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 4508 | |
| 4509 | /* Don't count the last line break if 'bin' and 'noeol'. */ |
| 4510 | if (buf->b_p_bin && !buf->b_p_eol) |
| 4511 | size -= ffdos + 1; |
| 4512 | } |
| 4513 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4514 | return size; |
| 4515 | } |
| 4516 | |
| 4517 | /* |
| 4518 | * Goto byte in buffer with offset 'cnt'. |
| 4519 | */ |
| 4520 | void |
| 4521 | goto_byte(cnt) |
| 4522 | long cnt; |
| 4523 | { |
| 4524 | long boff = cnt; |
| 4525 | linenr_T lnum; |
| 4526 | |
| 4527 | ml_flush_line(curbuf); /* cached line may be dirty */ |
| 4528 | setpcmark(); |
| 4529 | if (boff) |
| 4530 | --boff; |
| 4531 | lnum = ml_find_line_or_offset(curbuf, (linenr_T)0, &boff); |
| 4532 | if (lnum < 1) /* past the end */ |
| 4533 | { |
| 4534 | curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; |
| 4535 | curwin->w_curswant = MAXCOL; |
| 4536 | coladvance((colnr_T)MAXCOL); |
| 4537 | } |
| 4538 | else |
| 4539 | { |
| 4540 | curwin->w_cursor.lnum = lnum; |
| 4541 | curwin->w_cursor.col = (colnr_T)boff; |
| 4542 | curwin->w_set_curswant = TRUE; |
| 4543 | } |
| 4544 | check_cursor(); |
| 4545 | |
| 4546 | # ifdef FEAT_MBYTE |
| 4547 | /* Make sure the cursor is on the first byte of a multi-byte char. */ |
| 4548 | if (has_mbyte) |
| 4549 | mb_adjust_cursor(); |
| 4550 | # endif |
| 4551 | } |
| 4552 | #endif |