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