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