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