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