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