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