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