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