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