Mercurial > vim
annotate src/buffer.c @ 3604:b2763055bdf2 v7.3.562
updated for version 7.3.562
Problem: ":profdel" should not work when the +profile feature is disabled.
Solution: Call ex_ni(). (Yasuhiro Matsumoto)
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Wed, 20 Jun 2012 15:48:57 +0200 |
parents | a220dd97b5c7 |
children | 02c86cdcf9a2 |
rev | line source |
---|---|
7 | 1 /* vi:set ts=8 sts=4 sw=4: |
2 * | |
3 * VIM - Vi IMproved by Bram Moolenaar | |
4 * | |
5 * Do ":help uganda" in Vim to read copying and usage conditions. | |
6 * Do ":help credits" in Vim to see a list of people who contributed. | |
7 * See README.txt for an overview of the Vim source code. | |
8 */ | |
9 | |
10 /* | |
11 * buffer.c: functions for dealing with the buffer structure | |
12 */ | |
13 | |
14 /* | |
15 * The buffer list is a double linked list of all buffers. | |
16 * Each buffer can be in one of these states: | |
17 * never loaded: BF_NEVERLOADED is set, only the file name is valid | |
18 * not loaded: b_ml.ml_mfp == NULL, no memfile allocated | |
19 * hidden: b_nwindows == 0, loaded but not displayed in a window | |
20 * normal: loaded and displayed in a window | |
21 * | |
22 * Instead of storing file names all over the place, each file name is | |
23 * stored in the buffer list. It can be referenced by a number. | |
24 * | |
25 * The current implementation remembers all file names ever used. | |
26 */ | |
27 | |
28 #include "vim.h" | |
29 | |
30 #if defined(FEAT_CMDL_COMPL) || defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) | |
31 static char_u *buflist_match __ARGS((regprog_T *prog, buf_T *buf)); | |
32 # define HAVE_BUFLIST_MATCH | |
33 static char_u *fname_match __ARGS((regprog_T *prog, char_u *name)); | |
34 #endif | |
35 static void buflist_setfpos __ARGS((buf_T *buf, win_T *win, linenr_T lnum, colnr_T col, int copy_options)); | |
1743 | 36 static wininfo_T *find_wininfo __ARGS((buf_T *buf, int skip_diff_buffer)); |
7 | 37 #ifdef UNIX |
38 static buf_T *buflist_findname_stat __ARGS((char_u *ffname, struct stat *st)); | |
39 static int otherfile_buf __ARGS((buf_T *buf, char_u *ffname, struct stat *stp)); | |
40 static int buf_same_ino __ARGS((buf_T *buf, struct stat *stp)); | |
41 #else | |
42 static int otherfile_buf __ARGS((buf_T *buf, char_u *ffname)); | |
43 #endif | |
44 #ifdef FEAT_TITLE | |
45 static int ti_change __ARGS((char_u *str, char_u **last)); | |
46 #endif | |
1869 | 47 static int append_arg_number __ARGS((win_T *wp, char_u *buf, int buflen, int add_file)); |
7 | 48 static void free_buffer __ARGS((buf_T *)); |
49 static void free_buffer_stuff __ARGS((buf_T *buf, int free_options)); | |
50 static void clear_wininfo __ARGS((buf_T *buf)); | |
51 | |
52 #ifdef UNIX | |
53 # define dev_T dev_t | |
54 #else | |
55 # define dev_T unsigned | |
56 #endif | |
57 | |
58 #if defined(FEAT_SIGNS) | |
59 static void insert_sign __ARGS((buf_T *buf, signlist_T *prev, signlist_T *next, int id, linenr_T lnum, int typenr)); | |
60 static void buf_delete_signs __ARGS((buf_T *buf)); | |
61 #endif | |
62 | |
2411
68e394361ca3
Add "q" item for 'statusline'. Add w:quickfix_title. (Lech Lorens)
Bram Moolenaar <bram@vim.org>
parents:
2403
diff
changeset
|
63 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX) |
68e394361ca3
Add "q" item for 'statusline'. Add w:quickfix_title. (Lech Lorens)
Bram Moolenaar <bram@vim.org>
parents:
2403
diff
changeset
|
64 static char *msg_loclist = N_("[Location List]"); |
68e394361ca3
Add "q" item for 'statusline'. Add w:quickfix_title. (Lech Lorens)
Bram Moolenaar <bram@vim.org>
parents:
2403
diff
changeset
|
65 static char *msg_qflist = N_("[Quickfix List]"); |
68e394361ca3
Add "q" item for 'statusline'. Add w:quickfix_title. (Lech Lorens)
Bram Moolenaar <bram@vim.org>
parents:
2403
diff
changeset
|
66 #endif |
3365 | 67 #ifdef FEAT_AUTOCMD |
68 static char *e_auabort = N_("E855: Autocommands caused command to abort"); | |
69 #endif | |
2411
68e394361ca3
Add "q" item for 'statusline'. Add w:quickfix_title. (Lech Lorens)
Bram Moolenaar <bram@vim.org>
parents:
2403
diff
changeset
|
70 |
7 | 71 /* |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
72 * Open current buffer, that is: open the memfile and read the file into |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
73 * memory. |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
74 * Return FAIL for failure, OK otherwise. |
7 | 75 */ |
76 int | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
77 open_buffer(read_stdin, eap, flags) |
7 | 78 int read_stdin; /* read file from stdin */ |
79 exarg_T *eap; /* for forced 'ff' and 'fenc' or NULL */ | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
80 int flags; /* extra flags for readfile() */ |
7 | 81 { |
82 int retval = OK; | |
83 #ifdef FEAT_AUTOCMD | |
84 buf_T *old_curbuf; | |
85 #endif | |
86 | |
87 /* | |
88 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset. | |
89 * When re-entering the same buffer, it should not change, because the | |
90 * user may have reset the flag by hand. | |
91 */ | |
92 if (readonlymode && curbuf->b_ffname != NULL | |
93 && (curbuf->b_flags & BF_NEVERLOADED)) | |
94 curbuf->b_p_ro = TRUE; | |
95 | |
625 | 96 if (ml_open(curbuf) == FAIL) |
7 | 97 { |
98 /* | |
99 * There MUST be a memfile, otherwise we can't do anything | |
100 * If we can't create one for the current buffer, take another buffer | |
101 */ | |
3365 | 102 close_buffer(NULL, curbuf, 0, FALSE); |
7 | 103 for (curbuf = firstbuf; curbuf != NULL; curbuf = curbuf->b_next) |
104 if (curbuf->b_ml.ml_mfp != NULL) | |
105 break; | |
106 /* | |
107 * if there is no memfile at all, exit | |
1698 | 108 * This is OK, since there are no changes to lose. |
7 | 109 */ |
110 if (curbuf == NULL) | |
111 { | |
112 EMSG(_("E82: Cannot allocate any buffer, exiting...")); | |
113 getout(2); | |
114 } | |
115 EMSG(_("E83: Cannot allocate buffer, using other one...")); | |
116 enter_buffer(curbuf); | |
117 return FAIL; | |
118 } | |
119 | |
120 #ifdef FEAT_AUTOCMD | |
121 /* The autocommands in readfile() may change the buffer, but only AFTER | |
122 * reading the file. */ | |
123 old_curbuf = curbuf; | |
124 modified_was_set = FALSE; | |
125 #endif | |
126 | |
127 /* mark cursor position as being invalid */ | |
2091
95b659982b7c
updated for version 7.2.375
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
128 curwin->w_valid = 0; |
7 | 129 |
130 if (curbuf->b_ffname != NULL | |
131 #ifdef FEAT_NETBEANS_INTG | |
132 && netbeansReadFile | |
133 #endif | |
134 ) | |
135 { | |
136 #ifdef FEAT_NETBEANS_INTG | |
137 int oldFire = netbeansFireChanges; | |
138 | |
139 netbeansFireChanges = 0; | |
140 #endif | |
141 retval = readfile(curbuf->b_ffname, curbuf->b_fname, | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
142 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap, |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
143 flags | READ_NEW); |
7 | 144 #ifdef FEAT_NETBEANS_INTG |
145 netbeansFireChanges = oldFire; | |
146 #endif | |
147 /* Help buffer is filtered. */ | |
148 if (curbuf->b_help) | |
149 fix_help_buffer(); | |
150 } | |
151 else if (read_stdin) | |
152 { | |
153 int save_bin = curbuf->b_p_bin; | |
154 linenr_T line_count; | |
155 | |
156 /* | |
157 * First read the text in binary mode into the buffer. | |
158 * Then read from that same buffer and append at the end. This makes | |
159 * it possible to retry when 'fileformat' or 'fileencoding' was | |
160 * guessed wrong. | |
161 */ | |
162 curbuf->b_p_bin = TRUE; | |
163 retval = readfile(NULL, NULL, (linenr_T)0, | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
164 (linenr_T)0, (linenr_T)MAXLNUM, NULL, |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
165 flags | (READ_NEW + READ_STDIN)); |
7 | 166 curbuf->b_p_bin = save_bin; |
167 if (retval == OK) | |
168 { | |
169 line_count = curbuf->b_ml.ml_line_count; | |
170 retval = readfile(NULL, NULL, (linenr_T)line_count, | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
171 (linenr_T)0, (linenr_T)MAXLNUM, eap, |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
172 flags | READ_BUFFER); |
7 | 173 if (retval == OK) |
174 { | |
175 /* Delete the binary lines. */ | |
176 while (--line_count >= 0) | |
177 ml_delete((linenr_T)1, FALSE); | |
178 } | |
179 else | |
180 { | |
181 /* Delete the converted lines. */ | |
182 while (curbuf->b_ml.ml_line_count > line_count) | |
183 ml_delete(line_count, FALSE); | |
184 } | |
185 /* Put the cursor on the first line. */ | |
186 curwin->w_cursor.lnum = 1; | |
187 curwin->w_cursor.col = 0; | |
1291 | 188 |
189 /* Set or reset 'modified' before executing autocommands, so that | |
190 * it can be changed there. */ | |
191 if (!readonlymode && !bufempty()) | |
192 changed(); | |
193 else if (retval != FAIL) | |
194 unchanged(curbuf, FALSE); | |
7 | 195 #ifdef FEAT_AUTOCMD |
196 # ifdef FEAT_EVAL | |
197 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE, | |
198 curbuf, &retval); | |
199 # else | |
200 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf); | |
201 # endif | |
202 #endif | |
203 } | |
204 } | |
205 | |
206 /* if first time loading this buffer, init b_chartab[] */ | |
207 if (curbuf->b_flags & BF_NEVERLOADED) | |
208 (void)buf_init_chartab(curbuf, FALSE); | |
209 | |
210 /* | |
211 * Set/reset the Changed flag first, autocmds may change the buffer. | |
212 * Apply the automatic commands, before processing the modelines. | |
213 * So the modelines have priority over auto commands. | |
214 */ | |
215 /* When reading stdin, the buffer contents always needs writing, so set | |
216 * the changed flag. Unless in readonly mode: "ls | gview -". | |
217 * When interrupted and 'cpoptions' contains 'i' set changed flag. */ | |
1291 | 218 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL) |
7 | 219 #ifdef FEAT_AUTOCMD |
220 || modified_was_set /* ":set modified" used in autocmd */ | |
221 # ifdef FEAT_EVAL | |
222 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL) | |
223 # endif | |
224 #endif | |
1291 | 225 ) |
7 | 226 changed(); |
1291 | 227 else if (retval != FAIL && !read_stdin) |
7 | 228 unchanged(curbuf, FALSE); |
229 save_file_ff(curbuf); /* keep this fileformat */ | |
230 | |
231 /* require "!" to overwrite the file, because it wasn't read completely */ | |
232 #ifdef FEAT_EVAL | |
233 if (aborting()) | |
234 #else | |
235 if (got_int) | |
236 #endif | |
237 curbuf->b_flags |= BF_READERR; | |
238 | |
20 | 239 #ifdef FEAT_FOLDING |
240 /* Need to update automatic folding. Do this before the autocommands, | |
241 * they may use the fold info. */ | |
242 foldUpdateAll(curwin); | |
243 #endif | |
244 | |
7 | 245 #ifdef FEAT_AUTOCMD |
246 /* need to set w_topline, unless some autocommand already did that. */ | |
247 if (!(curwin->w_valid & VALID_TOPLINE)) | |
248 { | |
249 curwin->w_topline = 1; | |
250 # ifdef FEAT_DIFF | |
251 curwin->w_topfill = 0; | |
252 # endif | |
253 } | |
254 # ifdef FEAT_EVAL | |
255 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval); | |
256 # else | |
257 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf); | |
258 # endif | |
259 #endif | |
260 | |
261 if (retval != FAIL) | |
262 { | |
263 #ifdef FEAT_AUTOCMD | |
264 /* | |
265 * The autocommands may have changed the current buffer. Apply the | |
266 * modelines to the correct buffer, if it still exists and is loaded. | |
267 */ | |
268 if (buf_valid(old_curbuf) && old_curbuf->b_ml.ml_mfp != NULL) | |
269 { | |
270 aco_save_T aco; | |
271 | |
272 /* Go to the buffer that was opened. */ | |
273 aucmd_prepbuf(&aco, old_curbuf); | |
274 #endif | |
717 | 275 do_modelines(0); |
7 | 276 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED); |
277 | |
278 #ifdef FEAT_AUTOCMD | |
279 # ifdef FEAT_EVAL | |
280 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf, | |
281 &retval); | |
282 # else | |
283 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf); | |
284 # endif | |
285 | |
286 /* restore curwin/curbuf and a few other things */ | |
287 aucmd_restbuf(&aco); | |
288 } | |
289 #endif | |
290 } | |
291 | |
292 return retval; | |
293 } | |
294 | |
295 /* | |
296 * Return TRUE if "buf" points to a valid buffer (in the buffer list). | |
297 */ | |
298 int | |
299 buf_valid(buf) | |
300 buf_T *buf; | |
301 { | |
302 buf_T *bp; | |
303 | |
304 for (bp = firstbuf; bp != NULL; bp = bp->b_next) | |
305 if (bp == buf) | |
306 return TRUE; | |
307 return FALSE; | |
308 } | |
309 | |
310 /* | |
311 * Close the link to a buffer. | |
312 * "action" is used when there is no longer a window for the buffer. | |
313 * It can be: | |
314 * 0 buffer becomes hidden | |
315 * DOBUF_UNLOAD buffer is unloaded | |
316 * DOBUF_DELETE buffer is unloaded and removed from buffer list | |
317 * DOBUF_WIPE buffer is unloaded and really deleted | |
318 * When doing all but the first one on the current buffer, the caller should | |
319 * get a new buffer very soon! | |
320 * | |
321 * The 'bufhidden' option can force freeing and deleting. | |
3365 | 322 * |
323 * When "abort_if_last" is TRUE then do not close the buffer if autocommands | |
324 * cause there to be only one window with this buffer. e.g. when ":quit" is | |
325 * supposed to close the window but autocommands close all other windows. | |
7 | 326 */ |
327 void | |
3365 | 328 close_buffer(win, buf, action, abort_if_last) |
7 | 329 win_T *win; /* if not NULL, set b_last_cursor */ |
330 buf_T *buf; | |
331 int action; | |
3406 | 332 int abort_if_last UNUSED; |
7 | 333 { |
334 #ifdef FEAT_AUTOCMD | |
335 int is_curbuf; | |
2047
85da03763130
updated for version 7.2.333
Bram Moolenaar <bram@zimbu.org>
parents:
1883
diff
changeset
|
336 int nwindows; |
7 | 337 #endif |
338 int unload_buf = (action != 0); | |
339 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE); | |
340 int wipe_buf = (action == DOBUF_WIPE); | |
341 | |
342 #ifdef FEAT_QUICKFIX | |
343 /* | |
344 * Force unloading or deleting when 'bufhidden' says so. | |
345 * The caller must take care of NOT deleting/freeing when 'bufhidden' is | |
346 * "hide" (otherwise we could never free or delete a buffer). | |
347 */ | |
348 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */ | |
349 { | |
350 del_buf = TRUE; | |
351 unload_buf = TRUE; | |
352 } | |
353 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */ | |
354 { | |
355 del_buf = TRUE; | |
356 unload_buf = TRUE; | |
357 wipe_buf = TRUE; | |
358 } | |
359 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */ | |
360 unload_buf = TRUE; | |
361 #endif | |
362 | |
363 if (win != NULL) | |
364 { | |
365 /* Set b_last_cursor when closing the last window for the buffer. | |
366 * Remember the last cursor position and window options of the buffer. | |
367 * This used to be only for the current window, but then options like | |
368 * 'foldmethod' may be lost with a ":only" command. */ | |
369 if (buf->b_nwindows == 1) | |
370 set_last_cursor(win); | |
371 buflist_setfpos(buf, win, | |
372 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum, | |
373 win->w_cursor.col, TRUE); | |
374 } | |
375 | |
376 #ifdef FEAT_AUTOCMD | |
377 /* When the buffer is no longer in a window, trigger BufWinLeave */ | |
378 if (buf->b_nwindows == 1) | |
379 { | |
3570 | 380 buf->b_closing = TRUE; |
7 | 381 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname, |
382 FALSE, buf); | |
3570 | 383 if (!buf_valid(buf)) |
3365 | 384 { |
3570 | 385 /* Autocommands deleted the buffer. */ |
386 aucmd_abort: | |
3365 | 387 EMSG(_(e_auabort)); |
7 | 388 return; |
3365 | 389 } |
3570 | 390 buf->b_closing = FALSE; |
391 if (abort_if_last && one_window()) | |
392 /* Autocommands made this the only window. */ | |
393 goto aucmd_abort; | |
7 | 394 |
395 /* When the buffer becomes hidden, but is not unloaded, trigger | |
396 * BufHidden */ | |
397 if (!unload_buf) | |
398 { | |
3570 | 399 buf->b_closing = TRUE; |
7 | 400 apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname, |
401 FALSE, buf); | |
3570 | 402 if (!buf_valid(buf)) |
403 /* Autocommands deleted the buffer. */ | |
404 goto aucmd_abort; | |
405 buf->b_closing = FALSE; | |
406 if (abort_if_last && one_window()) | |
407 /* Autocommands made this the only window. */ | |
408 goto aucmd_abort; | |
7 | 409 } |
410 # ifdef FEAT_EVAL | |
411 if (aborting()) /* autocmds may abort script processing */ | |
412 return; | |
413 # endif | |
414 } | |
415 nwindows = buf->b_nwindows; | |
416 #endif | |
417 | |
418 /* decrease the link count from windows (unless not in any window) */ | |
419 if (buf->b_nwindows > 0) | |
420 --buf->b_nwindows; | |
421 | |
422 /* Return when a window is displaying the buffer or when it's not | |
423 * unloaded. */ | |
424 if (buf->b_nwindows > 0 || !unload_buf) | |
425 return; | |
426 | |
427 /* Always remove the buffer when there is no file name. */ | |
428 if (buf->b_ffname == NULL) | |
429 del_buf = TRUE; | |
430 | |
431 /* | |
432 * Free all things allocated for this buffer. | |
433 * Also calls the "BufDelete" autocommands when del_buf is TRUE. | |
434 */ | |
435 #ifdef FEAT_AUTOCMD | |
436 /* Remember if we are closing the current buffer. Restore the number of | |
437 * windows, so that autocommands in buf_freeall() don't get confused. */ | |
438 is_curbuf = (buf == curbuf); | |
439 buf->b_nwindows = nwindows; | |
440 #endif | |
441 | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
442 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0)); |
3080 | 443 if ( |
444 #ifdef FEAT_WINDOWS | |
445 win_valid(win) && | |
3238 | 446 #else |
447 win != NULL && | |
3080 | 448 #endif |
449 win->w_buffer == buf) | |
3068 | 450 win->w_buffer = NULL; /* make sure we don't use the buffer now */ |
7 | 451 |
452 #ifdef FEAT_AUTOCMD | |
453 /* Autocommands may have deleted the buffer. */ | |
454 if (!buf_valid(buf)) | |
455 return; | |
456 # ifdef FEAT_EVAL | |
20 | 457 if (aborting()) /* autocmds may abort script processing */ |
7 | 458 return; |
459 # endif | |
460 | |
461 /* Autocommands may have opened or closed windows for this buffer. | |
462 * Decrement the count for the close we do here. */ | |
463 if (buf->b_nwindows > 0) | |
464 --buf->b_nwindows; | |
465 | |
466 /* | |
467 * It's possible that autocommands change curbuf to the one being deleted. | |
468 * This might cause the previous curbuf to be deleted unexpectedly. But | |
469 * in some cases it's OK to delete the curbuf, because a new one is | |
470 * obtained anyway. Therefore only return if curbuf changed to the | |
471 * deleted buffer. | |
472 */ | |
473 if (buf == curbuf && !is_curbuf) | |
474 return; | |
475 #endif | |
476 | |
961 | 477 /* Change directories when the 'acd' option is set. */ |
478 DO_AUTOCHDIR | |
7 | 479 |
480 /* | |
481 * Remove the buffer from the list. | |
482 */ | |
483 if (wipe_buf) | |
484 { | |
485 #ifdef FEAT_SUN_WORKSHOP | |
486 if (usingSunWorkShop) | |
487 workshop_file_closed_lineno((char *)buf->b_ffname, | |
488 (int)buf->b_last_cursor.lnum); | |
489 #endif | |
490 vim_free(buf->b_ffname); | |
491 vim_free(buf->b_sfname); | |
492 if (buf->b_prev == NULL) | |
493 firstbuf = buf->b_next; | |
494 else | |
495 buf->b_prev->b_next = buf->b_next; | |
496 if (buf->b_next == NULL) | |
497 lastbuf = buf->b_prev; | |
498 else | |
499 buf->b_next->b_prev = buf->b_prev; | |
500 free_buffer(buf); | |
501 } | |
502 else | |
503 { | |
504 if (del_buf) | |
505 { | |
506 /* Free all internal variables and reset option values, to make | |
507 * ":bdel" compatible with Vim 5.7. */ | |
508 free_buffer_stuff(buf, TRUE); | |
509 | |
510 /* Make it look like a new buffer. */ | |
511 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED; | |
512 | |
513 /* Init the options when loaded again. */ | |
514 buf->b_p_initialized = FALSE; | |
515 } | |
516 buf_clear_file(buf); | |
517 if (del_buf) | |
518 buf->b_p_bl = FALSE; | |
519 } | |
520 } | |
521 | |
522 /* | |
523 * Make buffer not contain a file. | |
524 */ | |
525 void | |
526 buf_clear_file(buf) | |
527 buf_T *buf; | |
528 { | |
529 buf->b_ml.ml_line_count = 1; | |
530 unchanged(buf, TRUE); | |
531 #ifndef SHORT_FNAME | |
532 buf->b_shortname = FALSE; | |
533 #endif | |
534 buf->b_p_eol = TRUE; | |
535 buf->b_start_eol = TRUE; | |
536 #ifdef FEAT_MBYTE | |
537 buf->b_p_bomb = FALSE; | |
1352 | 538 buf->b_start_bomb = FALSE; |
7 | 539 #endif |
540 buf->b_ml.ml_mfp = NULL; | |
541 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */ | |
542 #ifdef FEAT_NETBEANS_INTG | |
543 netbeans_deleted_all_lines(buf); | |
544 #endif | |
545 } | |
546 | |
547 /* | |
548 * buf_freeall() - free all things allocated for a buffer that are related to | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
549 * the file. flags: |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
550 * BFA_DEL buffer is going to be deleted |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
551 * BFA_WIPE buffer is going to be wiped out |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
552 * BFA_KEEP_UNDO do not free undo information |
7 | 553 */ |
554 void | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
555 buf_freeall(buf, flags) |
7 | 556 buf_T *buf; |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
557 int flags; |
7 | 558 { |
559 #ifdef FEAT_AUTOCMD | |
560 int is_curbuf = (buf == curbuf); | |
561 | |
3570 | 562 buf->b_closing = TRUE; |
7 | 563 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, FALSE, buf); |
564 if (!buf_valid(buf)) /* autocommands may delete the buffer */ | |
565 return; | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
566 if ((flags & BFA_DEL) && buf->b_p_bl) |
7 | 567 { |
568 apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname, FALSE, buf); | |
569 if (!buf_valid(buf)) /* autocommands may delete the buffer */ | |
570 return; | |
571 } | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
572 if (flags & BFA_WIPE) |
7 | 573 { |
574 apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname, | |
575 FALSE, buf); | |
576 if (!buf_valid(buf)) /* autocommands may delete the buffer */ | |
577 return; | |
578 } | |
3570 | 579 buf->b_closing = FALSE; |
7 | 580 # ifdef FEAT_EVAL |
36 | 581 if (aborting()) /* autocmds may abort script processing */ |
7 | 582 return; |
583 # endif | |
584 | |
585 /* | |
586 * It's possible that autocommands change curbuf to the one being deleted. | |
587 * This might cause curbuf to be deleted unexpectedly. But in some cases | |
588 * it's OK to delete the curbuf, because a new one is obtained anyway. | |
589 * Therefore only return if curbuf changed to the deleted buffer. | |
590 */ | |
591 if (buf == curbuf && !is_curbuf) | |
592 return; | |
593 #endif | |
594 #ifdef FEAT_DIFF | |
595 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */ | |
596 #endif | |
3068 | 597 #ifdef FEAT_SYN_HL |
3182 | 598 /* Remove any ownsyntax, unless exiting. */ |
599 if (firstwin != NULL && curwin->w_buffer == buf) | |
600 reset_synblock(curwin); | |
3068 | 601 #endif |
1187 | 602 |
603 #ifdef FEAT_FOLDING | |
604 /* No folds in an empty buffer. */ | |
605 # ifdef FEAT_WINDOWS | |
606 { | |
607 win_T *win; | |
608 tabpage_T *tp; | |
609 | |
610 FOR_ALL_TAB_WINDOWS(tp, win) | |
611 if (win->w_buffer == buf) | |
612 clearFolding(win); | |
613 } | |
614 # else | |
615 if (curwin->w_buffer == buf) | |
616 clearFolding(curwin); | |
617 # endif | |
618 #endif | |
619 | |
7 | 620 #ifdef FEAT_TCL |
621 tcl_buffer_free(buf); | |
622 #endif | |
623 ml_close(buf, TRUE); /* close and delete the memline/memfile */ | |
624 buf->b_ml.ml_line_count = 0; /* no lines in buffer */ | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
625 if ((flags & BFA_KEEP_UNDO) == 0) |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
626 { |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
627 u_blockfree(buf); /* free the memory allocated for undo */ |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
628 u_clearall(buf); /* reset all undo information */ |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
629 } |
7 | 630 #ifdef FEAT_SYN_HL |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2214
diff
changeset
|
631 syntax_clear(&buf->b_s); /* reset syntax info */ |
7 | 632 #endif |
24 | 633 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */ |
7 | 634 } |
635 | |
636 /* | |
637 * Free a buffer structure and the things it contains related to the buffer | |
638 * itself (not the file, that must have been done already). | |
639 */ | |
640 static void | |
641 free_buffer(buf) | |
642 buf_T *buf; | |
643 { | |
644 free_buffer_stuff(buf, TRUE); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2280
diff
changeset
|
645 #ifdef FEAT_LUA |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2280
diff
changeset
|
646 lua_buffer_free(buf); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2280
diff
changeset
|
647 #endif |
14 | 648 #ifdef FEAT_MZSCHEME |
649 mzscheme_buffer_free(buf); | |
650 #endif | |
7 | 651 #ifdef FEAT_PERL |
652 perl_buf_free(buf); | |
653 #endif | |
654 #ifdef FEAT_PYTHON | |
655 python_buffer_free(buf); | |
656 #endif | |
2329
ad2889f48843
Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
2320
diff
changeset
|
657 #ifdef FEAT_PYTHON3 |
ad2889f48843
Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
2320
diff
changeset
|
658 python3_buffer_free(buf); |
ad2889f48843
Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
2320
diff
changeset
|
659 #endif |
7 | 660 #ifdef FEAT_RUBY |
661 ruby_buffer_free(buf); | |
662 #endif | |
40 | 663 #ifdef FEAT_AUTOCMD |
664 aubuflocal_remove(buf); | |
665 #endif | |
7 | 666 vim_free(buf); |
667 } | |
668 | |
669 /* | |
670 * Free stuff in the buffer for ":bdel" and when wiping out the buffer. | |
671 */ | |
672 static void | |
673 free_buffer_stuff(buf, free_options) | |
674 buf_T *buf; | |
675 int free_options; /* free options as well */ | |
676 { | |
677 if (free_options) | |
678 { | |
679 clear_wininfo(buf); /* including window-local options */ | |
680 free_buf_options(buf, TRUE); | |
2620 | 681 #ifdef FEAT_SPELL |
682 ga_clear(&buf->b_s.b_langp); | |
683 #endif | |
7 | 684 } |
685 #ifdef FEAT_EVAL | |
126 | 686 vars_clear(&buf->b_vars.dv_hashtab); /* free all internal variables */ |
687 hash_init(&buf->b_vars.dv_hashtab); | |
7 | 688 #endif |
689 #ifdef FEAT_USR_CMDS | |
690 uc_clear(&buf->b_ucmds); /* clear local user commands */ | |
691 #endif | |
692 #ifdef FEAT_SIGNS | |
693 buf_delete_signs(buf); /* delete any signs */ | |
694 #endif | |
1781 | 695 #ifdef FEAT_NETBEANS_INTG |
2210 | 696 netbeans_file_killed(buf); |
1781 | 697 #endif |
7 | 698 #ifdef FEAT_LOCALMAP |
699 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */ | |
700 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */ | |
701 #endif | |
702 #ifdef FEAT_MBYTE | |
703 vim_free(buf->b_start_fenc); | |
704 buf->b_start_fenc = NULL; | |
705 #endif | |
706 } | |
707 | |
708 /* | |
709 * Free the b_wininfo list for buffer "buf". | |
710 */ | |
711 static void | |
712 clear_wininfo(buf) | |
713 buf_T *buf; | |
714 { | |
715 wininfo_T *wip; | |
716 | |
717 while (buf->b_wininfo != NULL) | |
718 { | |
719 wip = buf->b_wininfo; | |
720 buf->b_wininfo = wip->wi_next; | |
721 if (wip->wi_optset) | |
722 { | |
723 clear_winopt(&wip->wi_opt); | |
724 #ifdef FEAT_FOLDING | |
725 deleteFoldRecurse(&wip->wi_folds); | |
726 #endif | |
727 } | |
728 vim_free(wip); | |
729 } | |
730 } | |
731 | |
732 #if defined(FEAT_LISTCMDS) || defined(PROTO) | |
733 /* | |
734 * Go to another buffer. Handles the result of the ATTENTION dialog. | |
735 */ | |
736 void | |
737 goto_buffer(eap, start, dir, count) | |
738 exarg_T *eap; | |
739 int start; | |
740 int dir; | |
741 int count; | |
742 { | |
576 | 743 # if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION) |
7 | 744 buf_T *old_curbuf = curbuf; |
745 | |
746 swap_exists_action = SEA_DIALOG; | |
747 # endif | |
748 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO, | |
749 start, dir, count, eap->forceit); | |
576 | 750 # if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION) |
7 | 751 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's') |
752 { | |
24 | 753 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) |
754 cleanup_T cs; | |
755 | |
756 /* Reset the error/interrupt/exception state here so that | |
757 * aborting() returns FALSE when closing a window. */ | |
758 enter_cleanup(&cs); | |
759 # endif | |
760 | |
761 /* Quitting means closing the split window, nothing else. */ | |
7 | 762 win_close(curwin, TRUE); |
763 swap_exists_action = SEA_NONE; | |
602 | 764 swap_exists_did_quit = TRUE; |
24 | 765 |
766 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) | |
767 /* Restore the error/interrupt/exception state if not discarded by a | |
768 * new aborting error, interrupt, or uncaught exception. */ | |
769 leave_cleanup(&cs); | |
770 # endif | |
7 | 771 } |
772 else | |
773 handle_swap_exists(old_curbuf); | |
774 # endif | |
775 } | |
776 #endif | |
777 | |
576 | 778 #if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO) |
7 | 779 /* |
780 * Handle the situation of swap_exists_action being set. | |
781 * It is allowed for "old_curbuf" to be NULL or invalid. | |
782 */ | |
783 void | |
784 handle_swap_exists(old_curbuf) | |
785 buf_T *old_curbuf; | |
786 { | |
24 | 787 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) |
788 cleanup_T cs; | |
789 # endif | |
20 | 790 |
7 | 791 if (swap_exists_action == SEA_QUIT) |
792 { | |
24 | 793 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) |
794 /* Reset the error/interrupt/exception state here so that | |
795 * aborting() returns FALSE when closing a buffer. */ | |
796 enter_cleanup(&cs); | |
797 # endif | |
798 | |
7 | 799 /* User selected Quit at ATTENTION prompt. Go back to previous |
800 * buffer. If that buffer is gone or the same as the current one, | |
801 * open a new, empty buffer. */ | |
802 swap_exists_action = SEA_NONE; /* don't want it again */ | |
602 | 803 swap_exists_did_quit = TRUE; |
3365 | 804 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE); |
7 | 805 if (!buf_valid(old_curbuf) || old_curbuf == curbuf) |
24 | 806 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED); |
7 | 807 if (old_curbuf != NULL) |
808 enter_buffer(old_curbuf); | |
809 /* If "old_curbuf" is NULL we are in big trouble here... */ | |
24 | 810 |
811 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) | |
812 /* Restore the error/interrupt/exception state if not discarded by a | |
813 * new aborting error, interrupt, or uncaught exception. */ | |
814 leave_cleanup(&cs); | |
815 # endif | |
7 | 816 } |
817 else if (swap_exists_action == SEA_RECOVER) | |
818 { | |
24 | 819 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) |
820 /* Reset the error/interrupt/exception state here so that | |
821 * aborting() returns FALSE when closing a buffer. */ | |
822 enter_cleanup(&cs); | |
823 # endif | |
824 | |
7 | 825 /* User selected Recover at ATTENTION prompt. */ |
826 msg_scroll = TRUE; | |
827 ml_recover(); | |
828 MSG_PUTS("\n"); /* don't overwrite the last message */ | |
829 cmdline_row = msg_row; | |
717 | 830 do_modelines(0); |
24 | 831 |
832 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) | |
833 /* Restore the error/interrupt/exception state if not discarded by a | |
834 * new aborting error, interrupt, or uncaught exception. */ | |
835 leave_cleanup(&cs); | |
836 # endif | |
7 | 837 } |
838 swap_exists_action = SEA_NONE; | |
839 } | |
840 #endif | |
841 | |
842 #if defined(FEAT_LISTCMDS) || defined(PROTO) | |
843 /* | |
844 * do_bufdel() - delete or unload buffer(s) | |
845 * | |
846 * addr_count == 0: ":bdel" - delete current buffer | |
847 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete | |
848 * buffer "end_bnr", then any other arguments. | |
849 * addr_count == 2: ":N,N bdel" - delete buffers in range | |
850 * | |
851 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or | |
852 * DOBUF_DEL (":bdel") | |
853 * | |
854 * Returns error message or NULL | |
855 */ | |
856 char_u * | |
857 do_bufdel(command, arg, addr_count, start_bnr, end_bnr, forceit) | |
858 int command; | |
859 char_u *arg; /* pointer to extra arguments */ | |
860 int addr_count; | |
861 int start_bnr; /* first buffer number in a range */ | |
862 int end_bnr; /* buffer nr or last buffer nr in a range */ | |
863 int forceit; | |
864 { | |
865 int do_current = 0; /* delete current buffer? */ | |
866 int deleted = 0; /* number of buffers deleted */ | |
867 char_u *errormsg = NULL; /* return value */ | |
868 int bnr; /* buffer number */ | |
869 char_u *p; | |
870 | |
871 if (addr_count == 0) | |
872 { | |
873 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit); | |
874 } | |
875 else | |
876 { | |
877 if (addr_count == 2) | |
878 { | |
879 if (*arg) /* both range and argument is not allowed */ | |
880 return (char_u *)_(e_trailing); | |
881 bnr = start_bnr; | |
882 } | |
883 else /* addr_count == 1 */ | |
884 bnr = end_bnr; | |
885 | |
886 for ( ;!got_int; ui_breakcheck()) | |
887 { | |
888 /* | |
889 * delete the current buffer last, otherwise when the | |
890 * current buffer is deleted, the next buffer becomes | |
891 * the current one and will be loaded, which may then | |
892 * also be deleted, etc. | |
893 */ | |
894 if (bnr == curbuf->b_fnum) | |
895 do_current = bnr; | |
896 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr, | |
897 forceit) == OK) | |
898 ++deleted; | |
899 | |
900 /* | |
901 * find next buffer number to delete/unload | |
902 */ | |
903 if (addr_count == 2) | |
904 { | |
905 if (++bnr > end_bnr) | |
906 break; | |
907 } | |
908 else /* addr_count == 1 */ | |
909 { | |
910 arg = skipwhite(arg); | |
911 if (*arg == NUL) | |
912 break; | |
913 if (!VIM_ISDIGIT(*arg)) | |
914 { | |
915 p = skiptowhite_esc(arg); | |
916 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE, FALSE); | |
917 if (bnr < 0) /* failed */ | |
918 break; | |
919 arg = p; | |
920 } | |
921 else | |
922 bnr = getdigits(&arg); | |
923 } | |
924 } | |
925 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST, | |
926 FORWARD, do_current, forceit) == OK) | |
927 ++deleted; | |
928 | |
929 if (deleted == 0) | |
930 { | |
931 if (command == DOBUF_UNLOAD) | |
300 | 932 STRCPY(IObuff, _("E515: No buffers were unloaded")); |
7 | 933 else if (command == DOBUF_DEL) |
300 | 934 STRCPY(IObuff, _("E516: No buffers were deleted")); |
7 | 935 else |
300 | 936 STRCPY(IObuff, _("E517: No buffers were wiped out")); |
7 | 937 errormsg = IObuff; |
938 } | |
939 else if (deleted >= p_report) | |
940 { | |
941 if (command == DOBUF_UNLOAD) | |
942 { | |
943 if (deleted == 1) | |
944 MSG(_("1 buffer unloaded")); | |
945 else | |
946 smsg((char_u *)_("%d buffers unloaded"), deleted); | |
947 } | |
948 else if (command == DOBUF_DEL) | |
949 { | |
950 if (deleted == 1) | |
951 MSG(_("1 buffer deleted")); | |
952 else | |
953 smsg((char_u *)_("%d buffers deleted"), deleted); | |
954 } | |
955 else | |
956 { | |
957 if (deleted == 1) | |
958 MSG(_("1 buffer wiped out")); | |
959 else | |
960 smsg((char_u *)_("%d buffers wiped out"), deleted); | |
961 } | |
962 } | |
963 } | |
964 | |
965 | |
966 return errormsg; | |
967 } | |
968 | |
969 /* | |
970 * Implementation of the commands for the buffer list. | |
971 * | |
972 * action == DOBUF_GOTO go to specified buffer | |
973 * action == DOBUF_SPLIT split window and go to specified buffer | |
974 * action == DOBUF_UNLOAD unload specified buffer(s) | |
975 * action == DOBUF_DEL delete specified buffer(s) from buffer list | |
976 * action == DOBUF_WIPE delete specified buffer(s) really | |
977 * | |
978 * start == DOBUF_CURRENT go to "count" buffer from current buffer | |
979 * start == DOBUF_FIRST go to "count" buffer from first buffer | |
980 * start == DOBUF_LAST go to "count" buffer from last buffer | |
981 * start == DOBUF_MOD go to "count" modified buffer from current buffer | |
982 * | |
983 * Return FAIL or OK. | |
984 */ | |
985 int | |
986 do_buffer(action, start, dir, count, forceit) | |
987 int action; | |
988 int start; | |
989 int dir; /* FORWARD or BACKWARD */ | |
990 int count; /* buffer number or number of buffers */ | |
991 int forceit; /* TRUE for :...! */ | |
992 { | |
993 buf_T *buf; | |
994 buf_T *bp; | |
995 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL | |
996 || action == DOBUF_WIPE); | |
997 | |
998 switch (start) | |
999 { | |
1000 case DOBUF_FIRST: buf = firstbuf; break; | |
1001 case DOBUF_LAST: buf = lastbuf; break; | |
1002 default: buf = curbuf; break; | |
1003 } | |
1004 if (start == DOBUF_MOD) /* find next modified buffer */ | |
1005 { | |
1006 while (count-- > 0) | |
1007 { | |
1008 do | |
1009 { | |
1010 buf = buf->b_next; | |
1011 if (buf == NULL) | |
1012 buf = firstbuf; | |
1013 } | |
1014 while (buf != curbuf && !bufIsChanged(buf)); | |
1015 } | |
1016 if (!bufIsChanged(buf)) | |
1017 { | |
1018 EMSG(_("E84: No modified buffer found")); | |
1019 return FAIL; | |
1020 } | |
1021 } | |
1022 else if (start == DOBUF_FIRST && count) /* find specified buffer number */ | |
1023 { | |
1024 while (buf != NULL && buf->b_fnum != count) | |
1025 buf = buf->b_next; | |
1026 } | |
1027 else | |
1028 { | |
1029 bp = NULL; | |
1030 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf)) | |
1031 { | |
1032 /* remember the buffer where we start, we come back there when all | |
1033 * buffers are unlisted. */ | |
1034 if (bp == NULL) | |
1035 bp = buf; | |
1036 if (dir == FORWARD) | |
1037 { | |
1038 buf = buf->b_next; | |
1039 if (buf == NULL) | |
1040 buf = firstbuf; | |
1041 } | |
1042 else | |
1043 { | |
1044 buf = buf->b_prev; | |
1045 if (buf == NULL) | |
1046 buf = lastbuf; | |
1047 } | |
1048 /* don't count unlisted buffers */ | |
1049 if (unload || buf->b_p_bl) | |
1050 { | |
1051 --count; | |
1052 bp = NULL; /* use this buffer as new starting point */ | |
1053 } | |
1054 if (bp == buf) | |
1055 { | |
1056 /* back where we started, didn't find anything. */ | |
1057 EMSG(_("E85: There is no listed buffer")); | |
1058 return FAIL; | |
1059 } | |
1060 } | |
1061 } | |
1062 | |
1063 if (buf == NULL) /* could not find it */ | |
1064 { | |
1065 if (start == DOBUF_FIRST) | |
1066 { | |
1067 /* don't warn when deleting */ | |
1068 if (!unload) | |
1069 EMSGN(_("E86: Buffer %ld does not exist"), count); | |
1070 } | |
1071 else if (dir == FORWARD) | |
1072 EMSG(_("E87: Cannot go beyond last buffer")); | |
1073 else | |
1074 EMSG(_("E88: Cannot go before first buffer")); | |
1075 return FAIL; | |
1076 } | |
1077 | |
1078 #ifdef FEAT_GUI | |
1079 need_mouse_correct = TRUE; | |
1080 #endif | |
1081 | |
1082 #ifdef FEAT_LISTCMDS | |
1083 /* | |
1084 * delete buffer buf from memory and/or the list | |
1085 */ | |
1086 if (unload) | |
1087 { | |
1088 int forward; | |
1089 int retval; | |
1090 | |
1091 /* When unloading or deleting a buffer that's already unloaded and | |
1092 * unlisted: fail silently. */ | |
1093 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl) | |
1094 return FAIL; | |
1095 | |
1096 if (!forceit && bufIsChanged(buf)) | |
1097 { | |
1098 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) | |
1099 if ((p_confirm || cmdmod.confirm) && p_write) | |
1100 { | |
1101 dialog_changed(buf, FALSE); | |
1102 # ifdef FEAT_AUTOCMD | |
1103 if (!buf_valid(buf)) | |
1104 /* Autocommand deleted buffer, oops! It's not changed | |
1105 * now. */ | |
1106 return FAIL; | |
1107 # endif | |
36 | 1108 /* If it's still changed fail silently, the dialog already |
1109 * mentioned why it fails. */ | |
1110 if (bufIsChanged(buf)) | |
1111 return FAIL; | |
7 | 1112 } |
36 | 1113 else |
7 | 1114 #endif |
1115 { | |
1116 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"), | |
1117 buf->b_fnum); | |
1118 return FAIL; | |
1119 } | |
1120 } | |
1121 | |
1122 /* | |
1123 * If deleting the last (listed) buffer, make it empty. | |
1124 * The last (listed) buffer cannot be unloaded. | |
1125 */ | |
1126 for (bp = firstbuf; bp != NULL; bp = bp->b_next) | |
1127 if (bp->b_p_bl && bp != buf) | |
1128 break; | |
1129 if (bp == NULL && buf == curbuf) | |
1130 { | |
1131 if (action == DOBUF_UNLOAD) | |
1132 { | |
1133 EMSG(_("E90: Cannot unload last buffer")); | |
1134 return FAIL; | |
1135 } | |
1136 | |
1137 /* Close any other windows on this buffer, then make it empty. */ | |
1138 #ifdef FEAT_WINDOWS | |
671 | 1139 close_windows(buf, TRUE); |
7 | 1140 #endif |
1141 setpcmark(); | |
1142 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, | |
1743 | 1143 forceit ? ECMD_FORCEIT : 0, curwin); |
7 | 1144 |
1145 /* | |
1146 * do_ecmd() may create a new buffer, then we have to delete | |
1147 * the old one. But do_ecmd() may have done that already, check | |
1148 * if the buffer still exists. | |
1149 */ | |
1150 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0) | |
3365 | 1151 close_buffer(NULL, buf, action, FALSE); |
7 | 1152 return retval; |
1153 } | |
1154 | |
1155 #ifdef FEAT_WINDOWS | |
1156 /* | |
1157 * If the deleted buffer is the current one, close the current window | |
671 | 1158 * (unless it's the only window). Repeat this so long as we end up in |
1159 * a window with this buffer. | |
7 | 1160 */ |
671 | 1161 while (buf == curbuf |
3570 | 1162 # ifdef FEAT_AUTOCMD |
1163 && !(curwin->w_closing || curwin->w_buffer->b_closing) | |
1164 # endif | |
671 | 1165 && (firstwin != lastwin || first_tabpage->tp_next != NULL)) |
7 | 1166 win_close(curwin, FALSE); |
1167 #endif | |
1168 | |
1169 /* | |
1170 * If the buffer to be deleted is not the current one, delete it here. | |
1171 */ | |
1172 if (buf != curbuf) | |
1173 { | |
1174 #ifdef FEAT_WINDOWS | |
671 | 1175 close_windows(buf, FALSE); |
7 | 1176 #endif |
1177 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0) | |
3365 | 1178 close_buffer(NULL, buf, action, FALSE); |
7 | 1179 return OK; |
1180 } | |
1181 | |
1182 /* | |
1183 * Deleting the current buffer: Need to find another buffer to go to. | |
1184 * There must be another, otherwise it would have been handled above. | |
1185 * First use au_new_curbuf, if it is valid. | |
1186 * Then prefer the buffer we most recently visited. | |
1187 * Else try to find one that is loaded, after the current buffer, | |
1188 * then before the current buffer. | |
1189 * Finally use any buffer. | |
1190 */ | |
1191 buf = NULL; /* selected buffer */ | |
1192 bp = NULL; /* used when no loaded buffer found */ | |
1193 #ifdef FEAT_AUTOCMD | |
1194 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf)) | |
1195 buf = au_new_curbuf; | |
1196 # ifdef FEAT_JUMPLIST | |
1197 else | |
1198 # endif | |
1199 #endif | |
1200 #ifdef FEAT_JUMPLIST | |
1201 if (curwin->w_jumplistlen > 0) | |
1202 { | |
1203 int jumpidx; | |
1204 | |
1205 jumpidx = curwin->w_jumplistidx - 1; | |
1206 if (jumpidx < 0) | |
1207 jumpidx = curwin->w_jumplistlen - 1; | |
1208 | |
1209 forward = jumpidx; | |
1210 while (jumpidx != curwin->w_jumplistidx) | |
1211 { | |
1212 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum); | |
1213 if (buf != NULL) | |
1214 { | |
1215 if (buf == curbuf || !buf->b_p_bl) | |
1216 buf = NULL; /* skip current and unlisted bufs */ | |
1217 else if (buf->b_ml.ml_mfp == NULL) | |
1218 { | |
1219 /* skip unloaded buf, but may keep it for later */ | |
1220 if (bp == NULL) | |
1221 bp = buf; | |
1222 buf = NULL; | |
1223 } | |
1224 } | |
1225 if (buf != NULL) /* found a valid buffer: stop searching */ | |
1226 break; | |
1227 /* advance to older entry in jump list */ | |
1228 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen) | |
1229 break; | |
1230 if (--jumpidx < 0) | |
1231 jumpidx = curwin->w_jumplistlen - 1; | |
1232 if (jumpidx == forward) /* List exhausted for sure */ | |
1233 break; | |
1234 } | |
1235 } | |
1236 #endif | |
1237 | |
1238 if (buf == NULL) /* No previous buffer, Try 2'nd approach */ | |
1239 { | |
1240 forward = TRUE; | |
1241 buf = curbuf->b_next; | |
1242 for (;;) | |
1243 { | |
1244 if (buf == NULL) | |
1245 { | |
1246 if (!forward) /* tried both directions */ | |
1247 break; | |
1248 buf = curbuf->b_prev; | |
1249 forward = FALSE; | |
1250 continue; | |
1251 } | |
1252 /* in non-help buffer, try to skip help buffers, and vv */ | |
1253 if (buf->b_help == curbuf->b_help && buf->b_p_bl) | |
1254 { | |
1255 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */ | |
1256 break; | |
1257 if (bp == NULL) /* remember unloaded buf for later */ | |
1258 bp = buf; | |
1259 } | |
1260 if (forward) | |
1261 buf = buf->b_next; | |
1262 else | |
1263 buf = buf->b_prev; | |
1264 } | |
1265 } | |
1266 if (buf == NULL) /* No loaded buffer, use unloaded one */ | |
1267 buf = bp; | |
1268 if (buf == NULL) /* No loaded buffer, find listed one */ | |
1269 { | |
1270 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
1271 if (buf->b_p_bl && buf != curbuf) | |
1272 break; | |
1273 } | |
1274 if (buf == NULL) /* Still no buffer, just take one */ | |
1275 { | |
1276 if (curbuf->b_next != NULL) | |
1277 buf = curbuf->b_next; | |
1278 else | |
1279 buf = curbuf->b_prev; | |
1280 } | |
1281 } | |
1282 | |
1283 /* | |
1284 * make buf current buffer | |
1285 */ | |
1286 if (action == DOBUF_SPLIT) /* split window first */ | |
1287 { | |
1288 # ifdef FEAT_WINDOWS | |
1618 | 1289 /* If 'switchbuf' contains "useopen": jump to first window containing |
1290 * "buf" if one exists */ | |
1291 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf)) | |
825 | 1292 return OK; |
1736 | 1293 /* If 'switchbuf' contains "usetab": jump to first window in any tab |
1618 | 1294 * page containing "buf" if one exists */ |
1295 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf)) | |
7 | 1296 return OK; |
1297 if (win_split(0, 0) == FAIL) | |
1298 # endif | |
1299 return FAIL; | |
1300 } | |
1301 #endif | |
1302 | |
1303 /* go to current buffer - nothing to do */ | |
1304 if (buf == curbuf) | |
1305 return OK; | |
1306 | |
1307 /* | |
1308 * Check if the current buffer may be abandoned. | |
1309 */ | |
1310 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit)) | |
1311 { | |
1312 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) | |
1313 if ((p_confirm || cmdmod.confirm) && p_write) | |
1314 { | |
1315 dialog_changed(curbuf, FALSE); | |
1316 # ifdef FEAT_AUTOCMD | |
1317 if (!buf_valid(buf)) | |
1318 /* Autocommand deleted buffer, oops! */ | |
1319 return FAIL; | |
1320 # endif | |
1321 } | |
1322 if (bufIsChanged(curbuf)) | |
1323 #endif | |
1324 { | |
1325 EMSG(_(e_nowrtmsg)); | |
1326 return FAIL; | |
1327 } | |
1328 } | |
1329 | |
1330 /* Go to the other buffer. */ | |
1331 set_curbuf(buf, action); | |
1332 | |
2583 | 1333 #if defined(FEAT_LISTCMDS) \ |
1334 && (defined(FEAT_SCROLLBIND) || defined(FEAT_CURSORBIND)) | |
7 | 1335 if (action == DOBUF_SPLIT) |
2583 | 1336 { |
1337 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */ | |
1338 } | |
7 | 1339 #endif |
1340 | |
1341 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) | |
1342 if (aborting()) /* autocmds may abort script processing */ | |
1343 return FAIL; | |
1344 #endif | |
1345 | |
1346 return OK; | |
1347 } | |
1348 | |
1349 #endif /* FEAT_LISTCMDS */ | |
1350 | |
1351 /* | |
1352 * Set current buffer to "buf". Executes autocommands and closes current | |
1353 * buffer. "action" tells how to close the current buffer: | |
1354 * DOBUF_GOTO free or hide it | |
1355 * DOBUF_SPLIT nothing | |
1356 * DOBUF_UNLOAD unload it | |
1357 * DOBUF_DEL delete it | |
1358 * DOBUF_WIPE wipe it out | |
1359 */ | |
1360 void | |
1361 set_curbuf(buf, action) | |
1362 buf_T *buf; | |
1363 int action; | |
1364 { | |
1365 buf_T *prevbuf; | |
3594 | 1366 win_T *prevwin; |
7 | 1367 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL |
1368 || action == DOBUF_WIPE); | |
1369 | |
1370 setpcmark(); | |
22 | 1371 if (!cmdmod.keepalt) |
1372 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */ | |
1743 | 1373 buflist_altfpos(curwin); /* remember curpos */ |
7 | 1374 |
1375 #ifdef FEAT_VISUAL | |
1376 /* Don't restart Select mode after switching to another buffer. */ | |
1377 VIsual_reselect = FALSE; | |
1378 #endif | |
1379 | |
1380 /* close_windows() or apply_autocmds() may change curbuf */ | |
1381 prevbuf = curbuf; | |
1382 | |
1383 #ifdef FEAT_AUTOCMD | |
1384 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf); | |
1385 # ifdef FEAT_EVAL | |
1386 if (buf_valid(prevbuf) && !aborting()) | |
1387 # else | |
1388 if (buf_valid(prevbuf)) | |
1389 # endif | |
1390 #endif | |
1391 { | |
3068 | 1392 #ifdef FEAT_SYN_HL |
1393 if (prevbuf == curwin->w_buffer) | |
1394 reset_synblock(curwin); | |
1395 #endif | |
7 | 1396 #ifdef FEAT_WINDOWS |
1397 if (unload) | |
671 | 1398 close_windows(prevbuf, FALSE); |
7 | 1399 #endif |
1400 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) | |
1401 if (buf_valid(prevbuf) && !aborting()) | |
1402 #else | |
1403 if (buf_valid(prevbuf)) | |
1404 #endif | |
815 | 1405 { |
3594 | 1406 prevwin = curwin; |
815 | 1407 if (prevbuf == curbuf) |
825 | 1408 u_sync(FALSE); |
7 | 1409 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf, |
1410 unload ? action : (action == DOBUF_GOTO | |
1411 && !P_HID(prevbuf) | |
3365 | 1412 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE); |
3594 | 1413 if (curwin != prevwin && win_valid(prevwin)) |
1414 /* autocommands changed curwin, Grr! */ | |
1415 curwin = prevwin; | |
815 | 1416 } |
7 | 1417 } |
1418 #ifdef FEAT_AUTOCMD | |
1710 | 1419 /* An autocommand may have deleted "buf", already entered it (e.g., when |
3594 | 1420 * it did ":bunload") or aborted the script processing! |
1421 * If curwin->w_buffer is null, enter_buffer() will make it valid again */ | |
1422 if ((buf_valid(buf) && buf != curbuf | |
1423 #ifdef FEAT_EVAL | |
1424 && !aborting() | |
1425 #endif | |
1426 #ifdef FEAT_WINDOWS | |
1427 ) || curwin->w_buffer == NULL | |
1428 #endif | |
1429 ) | |
7 | 1430 #endif |
1431 enter_buffer(buf); | |
1432 } | |
1433 | |
1434 /* | |
1435 * Enter a new current buffer. | |
1436 * Old curbuf must have been abandoned already! | |
1437 */ | |
1438 void | |
1439 enter_buffer(buf) | |
1440 buf_T *buf; | |
1441 { | |
1442 /* Copy buffer and window local option values. Not for a help buffer. */ | |
1443 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP); | |
1444 if (!buf->b_help) | |
1445 get_winopts(buf); | |
1446 #ifdef FEAT_FOLDING | |
1447 else | |
1448 /* Remove all folds in the window. */ | |
1449 clearFolding(curwin); | |
1450 foldUpdateAll(curwin); /* update folds (later). */ | |
1451 #endif | |
1452 | |
1453 /* Get the buffer in the current window. */ | |
1454 curwin->w_buffer = buf; | |
1455 curbuf = buf; | |
1456 ++curbuf->b_nwindows; | |
1457 | |
1458 #ifdef FEAT_DIFF | |
672 | 1459 if (curwin->w_p_diff) |
1460 diff_buf_add(curbuf); | |
7 | 1461 #endif |
1462 | |
3068 | 1463 #ifdef FEAT_SYN_HL |
1464 curwin->w_s = &(buf->b_s); | |
1465 #endif | |
1466 | |
7 | 1467 /* Cursor on first line by default. */ |
1468 curwin->w_cursor.lnum = 1; | |
1469 curwin->w_cursor.col = 0; | |
1470 #ifdef FEAT_VIRTUALEDIT | |
1471 curwin->w_cursor.coladd = 0; | |
1472 #endif | |
1473 curwin->w_set_curswant = TRUE; | |
1744 | 1474 #ifdef FEAT_AUTOCMD |
1475 curwin->w_topline_was_set = FALSE; | |
1476 #endif | |
7 | 1477 |
2091
95b659982b7c
updated for version 7.2.375
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
1478 /* mark cursor position as being invalid */ |
95b659982b7c
updated for version 7.2.375
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
1479 curwin->w_valid = 0; |
95b659982b7c
updated for version 7.2.375
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
1480 |
7 | 1481 /* Make sure the buffer is loaded. */ |
1482 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */ | |
22 | 1483 { |
24 | 1484 #ifdef FEAT_AUTOCMD |
22 | 1485 /* If there is no filetype, allow for detecting one. Esp. useful for |
1486 * ":ball" used in a autocommand. If there already is a filetype we | |
1487 * might prefer to keep it. */ | |
1488 if (*curbuf->b_p_ft == NUL) | |
1489 did_filetype = FALSE; | |
24 | 1490 #endif |
22 | 1491 |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
1492 open_buffer(FALSE, NULL, 0); |
22 | 1493 } |
7 | 1494 else |
1495 { | |
968 | 1496 if (!msg_silent) |
1497 need_fileinfo = TRUE; /* display file info after redraw */ | |
7 | 1498 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */ |
1499 #ifdef FEAT_AUTOCMD | |
1500 curwin->w_topline = 1; | |
1501 # ifdef FEAT_DIFF | |
1502 curwin->w_topfill = 0; | |
1503 # endif | |
1504 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf); | |
1505 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf); | |
1506 #endif | |
1507 } | |
1508 | |
1509 /* If autocommands did not change the cursor position, restore cursor lnum | |
1510 * and possibly cursor col. */ | |
1511 if (curwin->w_cursor.lnum == 1 && inindent(0)) | |
1512 buflist_getfpos(); | |
1513 | |
1514 check_arg_idx(curwin); /* check for valid arg_idx */ | |
1515 #ifdef FEAT_TITLE | |
1516 maketitle(); | |
1517 #endif | |
1518 #ifdef FEAT_AUTOCMD | |
1744 | 1519 /* when autocmds didn't change it */ |
1520 if (curwin->w_topline == 1 && !curwin->w_topline_was_set) | |
7 | 1521 #endif |
1522 scroll_cursor_halfway(FALSE); /* redisplay at correct position */ | |
1523 | |
33 | 1524 #ifdef FEAT_NETBEANS_INTG |
1525 /* Send fileOpened event because we've changed buffers. */ | |
2210 | 1526 netbeans_file_activated(curbuf); |
33 | 1527 #endif |
1528 | |
961 | 1529 /* Change directories when the 'acd' option is set. */ |
1530 DO_AUTOCHDIR | |
7 | 1531 |
1532 #ifdef FEAT_KEYMAP | |
1533 if (curbuf->b_kmap_state & KEYMAP_INIT) | |
1869 | 1534 (void)keymap_init(); |
7 | 1535 #endif |
1185 | 1536 #ifdef FEAT_SPELL |
1537 /* May need to set the spell language. Can only do this after the buffer | |
1538 * has been properly setup. */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2214
diff
changeset
|
1539 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL) |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2214
diff
changeset
|
1540 (void)did_set_spelllang(curwin); |
1185 | 1541 #endif |
1542 | |
7 | 1543 redraw_later(NOT_VALID); |
1544 } | |
1545 | |
961 | 1546 #if defined(FEAT_AUTOCHDIR) || defined(PROTO) |
1547 /* | |
1548 * Change to the directory of the current buffer. | |
1549 */ | |
1550 void | |
1551 do_autochdir() | |
1552 { | |
1553 if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK) | |
1554 shorten_fnames(TRUE); | |
1555 } | |
1556 #endif | |
1557 | |
7 | 1558 /* |
1559 * functions for dealing with the buffer list | |
1560 */ | |
1561 | |
1562 /* | |
1563 * Add a file name to the buffer list. Return a pointer to the buffer. | |
1564 * If the same file name already exists return a pointer to that buffer. | |
1565 * If it does not exist, or if fname == NULL, a new entry is created. | |
1566 * If (flags & BLN_CURBUF) is TRUE, may use current buffer. | |
1567 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list. | |
1568 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer. | |
1569 * This is the ONLY way to create a new buffer. | |
1570 */ | |
1571 static int top_file_num = 1; /* highest file number */ | |
1572 | |
1573 buf_T * | |
1574 buflist_new(ffname, sfname, lnum, flags) | |
1575 char_u *ffname; /* full path of fname or relative */ | |
1576 char_u *sfname; /* short fname or NULL */ | |
1577 linenr_T lnum; /* preferred cursor line */ | |
1578 int flags; /* BLN_ defines */ | |
1579 { | |
1580 buf_T *buf; | |
1581 #ifdef UNIX | |
1582 struct stat st; | |
1583 #endif | |
1584 | |
1585 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */ | |
1586 | |
1587 /* | |
1588 * If file name already exists in the list, update the entry. | |
1589 */ | |
1590 #ifdef UNIX | |
1591 /* On Unix we can use inode numbers when the file exists. Works better | |
1592 * for hard links. */ | |
1593 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0) | |
1594 st.st_dev = (dev_T)-1; | |
1595 #endif | |
1596 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf = | |
1597 #ifdef UNIX | |
1598 buflist_findname_stat(ffname, &st) | |
1599 #else | |
1600 buflist_findname(ffname) | |
1601 #endif | |
1602 ) != NULL) | |
1603 { | |
1604 vim_free(ffname); | |
1605 if (lnum != 0) | |
1606 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE); | |
1607 /* copy the options now, if 'cpo' doesn't have 's' and not done | |
1608 * already */ | |
1609 buf_copy_options(buf, 0); | |
1610 if ((flags & BLN_LISTED) && !buf->b_p_bl) | |
1611 { | |
1612 buf->b_p_bl = TRUE; | |
1613 #ifdef FEAT_AUTOCMD | |
1614 if (!(flags & BLN_DUMMY)) | |
1615 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf); | |
1616 #endif | |
1617 } | |
1618 return buf; | |
1619 } | |
1620 | |
1621 /* | |
1622 * If the current buffer has no name and no contents, use the current | |
1623 * buffer. Otherwise: Need to allocate a new buffer structure. | |
1624 * | |
1625 * This is the ONLY place where a new buffer structure is allocated! | |
625 | 1626 * (A spell file buffer is allocated in spell.c, but that's not a normal |
1627 * buffer.) | |
7 | 1628 */ |
1629 buf = NULL; | |
1630 if ((flags & BLN_CURBUF) | |
1631 && curbuf != NULL | |
1632 && curbuf->b_ffname == NULL | |
1633 && curbuf->b_nwindows <= 1 | |
1634 && (curbuf->b_ml.ml_mfp == NULL || bufempty())) | |
1635 { | |
1636 buf = curbuf; | |
1637 #ifdef FEAT_AUTOCMD | |
1638 /* It's like this buffer is deleted. Watch out for autocommands that | |
1639 * change curbuf! If that happens, allocate a new buffer anyway. */ | |
1640 if (curbuf->b_p_bl) | |
1641 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf); | |
1642 if (buf == curbuf) | |
1643 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf); | |
1644 # ifdef FEAT_EVAL | |
36 | 1645 if (aborting()) /* autocmds may abort script processing */ |
7 | 1646 return NULL; |
1647 # endif | |
1648 #endif | |
1649 #ifdef FEAT_QUICKFIX | |
1650 # ifdef FEAT_AUTOCMD | |
1651 if (buf == curbuf) | |
1652 # endif | |
1653 { | |
1654 /* Make sure 'bufhidden' and 'buftype' are empty */ | |
1655 clear_string_option(&buf->b_p_bh); | |
1656 clear_string_option(&buf->b_p_bt); | |
1657 } | |
1658 #endif | |
1659 } | |
1660 if (buf != curbuf || curbuf == NULL) | |
1661 { | |
1662 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T)); | |
1663 if (buf == NULL) | |
1664 { | |
1665 vim_free(ffname); | |
1666 return NULL; | |
1667 } | |
1668 } | |
1669 | |
1670 if (ffname != NULL) | |
1671 { | |
1672 buf->b_ffname = ffname; | |
1673 buf->b_sfname = vim_strsave(sfname); | |
1674 } | |
1675 | |
1676 clear_wininfo(buf); | |
1677 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T)); | |
1678 | |
1679 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL)) | |
1680 || buf->b_wininfo == NULL) | |
1681 { | |
1682 vim_free(buf->b_ffname); | |
1683 buf->b_ffname = NULL; | |
1684 vim_free(buf->b_sfname); | |
1685 buf->b_sfname = NULL; | |
1686 if (buf != curbuf) | |
1687 free_buffer(buf); | |
1688 return NULL; | |
1689 } | |
1690 | |
1691 if (buf == curbuf) | |
1692 { | |
1693 /* free all things allocated for this buffer */ | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
1694 buf_freeall(buf, 0); |
7 | 1695 if (buf != curbuf) /* autocommands deleted the buffer! */ |
1696 return NULL; | |
1697 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) | |
36 | 1698 if (aborting()) /* autocmds may abort script processing */ |
7 | 1699 return NULL; |
1700 #endif | |
1701 /* buf->b_nwindows = 0; why was this here? */ | |
1702 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */ | |
1703 #ifdef FEAT_KEYMAP | |
1704 /* need to reload lmaps and set b:keymap_name */ | |
1705 curbuf->b_kmap_state |= KEYMAP_INIT; | |
1706 #endif | |
1707 } | |
1708 else | |
1709 { | |
1710 /* | |
1711 * put new buffer at the end of the buffer list | |
1712 */ | |
1713 buf->b_next = NULL; | |
1714 if (firstbuf == NULL) /* buffer list is empty */ | |
1715 { | |
1716 buf->b_prev = NULL; | |
1717 firstbuf = buf; | |
1718 } | |
1719 else /* append new buffer at end of list */ | |
1720 { | |
1721 lastbuf->b_next = buf; | |
1722 buf->b_prev = lastbuf; | |
1723 } | |
1724 lastbuf = buf; | |
1725 | |
1726 buf->b_fnum = top_file_num++; | |
1727 if (top_file_num < 0) /* wrap around (may cause duplicates) */ | |
1728 { | |
1729 EMSG(_("W14: Warning: List of file names overflow")); | |
1730 if (emsg_silent == 0) | |
1731 { | |
1732 out_flush(); | |
1733 ui_delay(3000L, TRUE); /* make sure it is noticed */ | |
1734 } | |
1735 top_file_num = 1; | |
1736 } | |
1737 | |
1738 /* | |
1739 * Always copy the options from the current buffer. | |
1740 */ | |
1741 buf_copy_options(buf, BCO_ALWAYS); | |
1742 } | |
1743 | |
1744 buf->b_wininfo->wi_fpos.lnum = lnum; | |
1745 buf->b_wininfo->wi_win = curwin; | |
1746 | |
1747 #ifdef FEAT_EVAL | |
126 | 1748 init_var_dict(&buf->b_vars, &buf->b_bufvar); /* init b: variables */ |
1749 #endif | |
1750 #ifdef FEAT_SYN_HL | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2214
diff
changeset
|
1751 hash_init(&buf->b_s.b_keywtab); |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2214
diff
changeset
|
1752 hash_init(&buf->b_s.b_keywtab_ic); |
7 | 1753 #endif |
1754 | |
1755 buf->b_fname = buf->b_sfname; | |
1756 #ifdef UNIX | |
1757 if (st.st_dev == (dev_T)-1) | |
1873 | 1758 buf->b_dev_valid = FALSE; |
7 | 1759 else |
1760 { | |
1873 | 1761 buf->b_dev_valid = TRUE; |
7 | 1762 buf->b_dev = st.st_dev; |
1763 buf->b_ino = st.st_ino; | |
1764 } | |
1765 #endif | |
1766 buf->b_u_synced = TRUE; | |
1767 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED; | |
42 | 1768 if (flags & BLN_DUMMY) |
1769 buf->b_flags |= BF_DUMMY; | |
7 | 1770 buf_clear_file(buf); |
1771 clrallmarks(buf); /* clear marks */ | |
1772 fmarks_check_names(buf); /* check file marks for this file */ | |
1773 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */ | |
1774 #ifdef FEAT_AUTOCMD | |
1775 if (!(flags & BLN_DUMMY)) | |
1776 { | |
1777 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf); | |
1778 if (flags & BLN_LISTED) | |
1779 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf); | |
1780 # ifdef FEAT_EVAL | |
36 | 1781 if (aborting()) /* autocmds may abort script processing */ |
7 | 1782 return NULL; |
1783 # endif | |
1784 } | |
1785 #endif | |
1786 | |
1787 return buf; | |
1788 } | |
1789 | |
1790 /* | |
1791 * Free the memory for the options of a buffer. | |
1792 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and | |
1793 * 'fileencoding'. | |
1794 */ | |
1795 void | |
1796 free_buf_options(buf, free_p_ff) | |
1797 buf_T *buf; | |
1798 int free_p_ff; | |
1799 { | |
1800 if (free_p_ff) | |
1801 { | |
1802 #ifdef FEAT_MBYTE | |
1803 clear_string_option(&buf->b_p_fenc); | |
1804 #endif | |
1805 clear_string_option(&buf->b_p_ff); | |
1806 #ifdef FEAT_QUICKFIX | |
1807 clear_string_option(&buf->b_p_bh); | |
1808 clear_string_option(&buf->b_p_bt); | |
1809 #endif | |
1810 } | |
1811 #ifdef FEAT_FIND_ID | |
1812 clear_string_option(&buf->b_p_def); | |
1813 clear_string_option(&buf->b_p_inc); | |
1814 # ifdef FEAT_EVAL | |
1815 clear_string_option(&buf->b_p_inex); | |
1816 # endif | |
1817 #endif | |
1818 #if defined(FEAT_CINDENT) && defined(FEAT_EVAL) | |
1819 clear_string_option(&buf->b_p_inde); | |
1820 clear_string_option(&buf->b_p_indk); | |
1821 #endif | |
788 | 1822 #if defined(FEAT_BEVAL) && defined(FEAT_EVAL) |
1823 clear_string_option(&buf->b_p_bexpr); | |
1824 #endif | |
2360
d8e4b27cef80
Change 'cryptmethod' from a number to a string option. Make it global-local.
Bram Moolenaar <bram@vim.org>
parents:
2329
diff
changeset
|
1825 #if defined(FEAT_CRYPT) |
d8e4b27cef80
Change 'cryptmethod' from a number to a string option. Make it global-local.
Bram Moolenaar <bram@vim.org>
parents:
2329
diff
changeset
|
1826 clear_string_option(&buf->b_p_cm); |
d8e4b27cef80
Change 'cryptmethod' from a number to a string option. Make it global-local.
Bram Moolenaar <bram@vim.org>
parents:
2329
diff
changeset
|
1827 #endif |
667 | 1828 #if defined(FEAT_EVAL) |
1829 clear_string_option(&buf->b_p_fex); | |
1830 #endif | |
7 | 1831 #ifdef FEAT_CRYPT |
1832 clear_string_option(&buf->b_p_key); | |
1833 #endif | |
1834 clear_string_option(&buf->b_p_kp); | |
1835 clear_string_option(&buf->b_p_mps); | |
1836 clear_string_option(&buf->b_p_fo); | |
41 | 1837 clear_string_option(&buf->b_p_flp); |
7 | 1838 clear_string_option(&buf->b_p_isk); |
1839 #ifdef FEAT_KEYMAP | |
1840 clear_string_option(&buf->b_p_keymap); | |
1841 ga_clear(&buf->b_kmap_ga); | |
1842 #endif | |
1843 #ifdef FEAT_COMMENTS | |
1844 clear_string_option(&buf->b_p_com); | |
1845 #endif | |
1846 #ifdef FEAT_FOLDING | |
1847 clear_string_option(&buf->b_p_cms); | |
1848 #endif | |
1849 clear_string_option(&buf->b_p_nf); | |
1850 #ifdef FEAT_SYN_HL | |
1851 clear_string_option(&buf->b_p_syn); | |
737 | 1852 #endif |
1853 #ifdef FEAT_SPELL | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2214
diff
changeset
|
1854 clear_string_option(&buf->b_s.b_p_spc); |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2214
diff
changeset
|
1855 clear_string_option(&buf->b_s.b_p_spf); |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2214
diff
changeset
|
1856 vim_free(buf->b_s.b_cap_prog); |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2214
diff
changeset
|
1857 buf->b_s.b_cap_prog = NULL; |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2214
diff
changeset
|
1858 clear_string_option(&buf->b_s.b_p_spl); |
7 | 1859 #endif |
1860 #ifdef FEAT_SEARCHPATH | |
1861 clear_string_option(&buf->b_p_sua); | |
1862 #endif | |
1863 #ifdef FEAT_AUTOCMD | |
1864 clear_string_option(&buf->b_p_ft); | |
1865 #endif | |
1866 #ifdef FEAT_CINDENT | |
1867 clear_string_option(&buf->b_p_cink); | |
1868 clear_string_option(&buf->b_p_cino); | |
1869 #endif | |
1870 #if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT) | |
1871 clear_string_option(&buf->b_p_cinw); | |
1872 #endif | |
1873 #ifdef FEAT_INS_EXPAND | |
1874 clear_string_option(&buf->b_p_cpt); | |
1875 #endif | |
12 | 1876 #ifdef FEAT_COMPL_FUNC |
1877 clear_string_option(&buf->b_p_cfu); | |
502 | 1878 clear_string_option(&buf->b_p_ofu); |
12 | 1879 #endif |
7 | 1880 #ifdef FEAT_QUICKFIX |
1881 clear_string_option(&buf->b_p_gp); | |
1882 clear_string_option(&buf->b_p_mp); | |
1883 clear_string_option(&buf->b_p_efm); | |
1884 #endif | |
1885 clear_string_option(&buf->b_p_ep); | |
1886 clear_string_option(&buf->b_p_path); | |
1887 clear_string_option(&buf->b_p_tags); | |
1888 #ifdef FEAT_INS_EXPAND | |
1889 clear_string_option(&buf->b_p_dict); | |
1890 clear_string_option(&buf->b_p_tsr); | |
1891 #endif | |
12 | 1892 #ifdef FEAT_TEXTOBJ |
1893 clear_string_option(&buf->b_p_qe); | |
1894 #endif | |
7 | 1895 buf->b_p_ar = -1; |
1896 } | |
1897 | |
1898 /* | |
1899 * get alternate file n | |
1900 * set linenr to lnum or altfpos.lnum if lnum == 0 | |
1901 * also set cursor column to altfpos.col if 'startofline' is not set. | |
1902 * if (options & GETF_SETMARK) call setpcmark() | |
1903 * if (options & GETF_ALT) we are jumping to an alternate file. | |
1904 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping | |
1905 * | |
1906 * return FAIL for failure, OK for success | |
1907 */ | |
1908 int | |
1909 buflist_getfile(n, lnum, options, forceit) | |
1910 int n; | |
1911 linenr_T lnum; | |
1912 int options; | |
1913 int forceit; | |
1914 { | |
1915 buf_T *buf; | |
1916 #ifdef FEAT_WINDOWS | |
1917 win_T *wp = NULL; | |
1918 #endif | |
1919 pos_T *fpos; | |
1920 colnr_T col; | |
1921 | |
1922 buf = buflist_findnr(n); | |
1923 if (buf == NULL) | |
1924 { | |
1925 if ((options & GETF_ALT) && n == 0) | |
1926 EMSG(_(e_noalt)); | |
1927 else | |
1928 EMSGN(_("E92: Buffer %ld not found"), n); | |
1929 return FAIL; | |
1930 } | |
1931 | |
1932 /* if alternate file is the current buffer, nothing to do */ | |
1933 if (buf == curbuf) | |
1934 return OK; | |
1935 | |
633 | 1936 if (text_locked()) |
631 | 1937 { |
633 | 1938 text_locked_msg(); |
7 | 1939 return FAIL; |
631 | 1940 } |
819 | 1941 #ifdef FEAT_AUTOCMD |
1942 if (curbuf_locked()) | |
1943 return FAIL; | |
1944 #endif | |
7 | 1945 |
1946 /* altfpos may be changed by getfile(), get it now */ | |
1947 if (lnum == 0) | |
1948 { | |
1949 fpos = buflist_findfpos(buf); | |
1950 lnum = fpos->lnum; | |
1951 col = fpos->col; | |
1952 } | |
1953 else | |
1954 col = 0; | |
1955 | |
1956 #ifdef FEAT_WINDOWS | |
1957 if (options & GETF_SWITCH) | |
1958 { | |
1618 | 1959 /* If 'switchbuf' contains "useopen": jump to first window containing |
1960 * "buf" if one exists */ | |
1961 if (swb_flags & SWB_USEOPEN) | |
7 | 1962 wp = buf_jump_open_win(buf); |
1618 | 1963 /* If 'switchbuf' contians "usetab": jump to first window in any tab |
1964 * page containing "buf" if one exists */ | |
1965 if (wp == NULL && (swb_flags & SWB_USETAB)) | |
825 | 1966 wp = buf_jump_open_tab(buf); |
1618 | 1967 /* If 'switchbuf' contains "split" or "newtab" and the current buffer |
1968 * isn't empty: open new window */ | |
1969 if (wp == NULL && (swb_flags & (SWB_SPLIT | SWB_NEWTAB)) && !bufempty()) | |
7 | 1970 { |
1618 | 1971 if (swb_flags & SWB_NEWTAB) /* Open in a new tab */ |
1972 tabpage_new(); | |
1973 else if (win_split(0, 0) == FAIL) /* Open in a new window */ | |
7 | 1974 return FAIL; |
2583 | 1975 RESET_BINDING(curwin); |
7 | 1976 } |
1977 } | |
1978 #endif | |
1979 | |
1980 ++RedrawingDisabled; | |
1981 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK), | |
1982 lnum, forceit) <= 0) | |
1983 { | |
1984 --RedrawingDisabled; | |
1985 | |
1986 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */ | |
1987 if (!p_sol && col != 0) | |
1988 { | |
1989 curwin->w_cursor.col = col; | |
1990 check_cursor_col(); | |
1991 #ifdef FEAT_VIRTUALEDIT | |
1992 curwin->w_cursor.coladd = 0; | |
1993 #endif | |
1994 curwin->w_set_curswant = TRUE; | |
1995 } | |
1996 return OK; | |
1997 } | |
1998 --RedrawingDisabled; | |
1999 return FAIL; | |
2000 } | |
2001 | |
2002 /* | |
2003 * go to the last know line number for the current buffer | |
2004 */ | |
2005 void | |
2006 buflist_getfpos() | |
2007 { | |
2008 pos_T *fpos; | |
2009 | |
2010 fpos = buflist_findfpos(curbuf); | |
2011 | |
2012 curwin->w_cursor.lnum = fpos->lnum; | |
2013 check_cursor_lnum(); | |
2014 | |
2015 if (p_sol) | |
2016 curwin->w_cursor.col = 0; | |
2017 else | |
2018 { | |
2019 curwin->w_cursor.col = fpos->col; | |
2020 check_cursor_col(); | |
2021 #ifdef FEAT_VIRTUALEDIT | |
2022 curwin->w_cursor.coladd = 0; | |
2023 #endif | |
2024 curwin->w_set_curswant = TRUE; | |
2025 } | |
2026 } | |
2027 | |
42 | 2028 #if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO) |
2029 /* | |
2030 * Find file in buffer list by name (it has to be for the current window). | |
2031 * Returns NULL if not found. | |
2032 */ | |
2033 buf_T * | |
2034 buflist_findname_exp(fname) | |
2035 char_u *fname; | |
2036 { | |
2037 char_u *ffname; | |
2038 buf_T *buf = NULL; | |
2039 | |
2040 /* First make the name into a full path name */ | |
2041 ffname = FullName_save(fname, | |
2042 #ifdef UNIX | |
2043 TRUE /* force expansion, get rid of symbolic links */ | |
2044 #else | |
2045 FALSE | |
2046 #endif | |
2047 ); | |
2048 if (ffname != NULL) | |
2049 { | |
2050 buf = buflist_findname(ffname); | |
2051 vim_free(ffname); | |
2052 } | |
2053 return buf; | |
2054 } | |
2055 #endif | |
2056 | |
7 | 2057 /* |
2058 * Find file in buffer list by name (it has to be for the current window). | |
2059 * "ffname" must have a full path. | |
42 | 2060 * Skips dummy buffers. |
2061 * Returns NULL if not found. | |
7 | 2062 */ |
2063 buf_T * | |
2064 buflist_findname(ffname) | |
2065 char_u *ffname; | |
2066 { | |
2067 #ifdef UNIX | |
2068 struct stat st; | |
2069 | |
2070 if (mch_stat((char *)ffname, &st) < 0) | |
2071 st.st_dev = (dev_T)-1; | |
2072 return buflist_findname_stat(ffname, &st); | |
2073 } | |
2074 | |
2075 /* | |
2076 * Same as buflist_findname(), but pass the stat structure to avoid getting it | |
2077 * twice for the same file. | |
42 | 2078 * Returns NULL if not found. |
7 | 2079 */ |
2080 static buf_T * | |
2081 buflist_findname_stat(ffname, stp) | |
2082 char_u *ffname; | |
2083 struct stat *stp; | |
2084 { | |
2085 #endif | |
2086 buf_T *buf; | |
2087 | |
2088 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
42 | 2089 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname |
7 | 2090 #ifdef UNIX |
2091 , stp | |
2092 #endif | |
2093 )) | |
2094 return buf; | |
2095 return NULL; | |
2096 } | |
2097 | |
2098 #if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO) | |
2099 /* | |
2100 * Find file in buffer list by a regexp pattern. | |
2101 * Return fnum of the found buffer. | |
2102 * Return < 0 for error. | |
2103 */ | |
2104 int | |
2105 buflist_findpat(pattern, pattern_end, unlisted, diffmode) | |
2106 char_u *pattern; | |
2107 char_u *pattern_end; /* pointer to first char after pattern */ | |
2108 int unlisted; /* find unlisted buffers */ | |
1883 | 2109 int diffmode UNUSED; /* find diff-mode buffers only */ |
7 | 2110 { |
2111 buf_T *buf; | |
2112 regprog_T *prog; | |
2113 int match = -1; | |
2114 int find_listed; | |
2115 char_u *pat; | |
2116 char_u *patend; | |
2117 int attempt; | |
2118 char_u *p; | |
2119 int toggledollar; | |
2120 | |
2121 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#')) | |
2122 { | |
2123 if (*pattern == '%') | |
2124 match = curbuf->b_fnum; | |
2125 else | |
2126 match = curwin->w_alt_fnum; | |
2127 #ifdef FEAT_DIFF | |
2128 if (diffmode && !diff_mode_buf(buflist_findnr(match))) | |
2129 match = -1; | |
2130 #endif | |
2131 } | |
2132 | |
2133 /* | |
2134 * Try four ways of matching a listed buffer: | |
2135 * attempt == 0: without '^' or '$' (at any position) | |
1187 | 2136 * attempt == 1: with '^' at start (only at position 0) |
7 | 2137 * attempt == 2: with '$' at end (only match at end) |
2138 * attempt == 3: with '^' at start and '$' at end (only full match) | |
2139 * Repeat this for finding an unlisted buffer if there was no matching | |
2140 * listed buffer. | |
2141 */ | |
2142 else | |
2143 { | |
2144 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE); | |
2145 if (pat == NULL) | |
2146 return -1; | |
2147 patend = pat + STRLEN(pat) - 1; | |
2148 toggledollar = (patend > pat && *patend == '$'); | |
2149 | |
2150 /* First try finding a listed buffer. If not found and "unlisted" | |
2151 * is TRUE, try finding an unlisted buffer. */ | |
2152 find_listed = TRUE; | |
2153 for (;;) | |
2154 { | |
2155 for (attempt = 0; attempt <= 3; ++attempt) | |
2156 { | |
2157 /* may add '^' and '$' */ | |
2158 if (toggledollar) | |
2159 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */ | |
2160 p = pat; | |
2161 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */ | |
2162 ++p; | |
2163 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0); | |
2164 if (prog == NULL) | |
2165 { | |
2166 vim_free(pat); | |
2167 return -1; | |
2168 } | |
2169 | |
2170 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
2171 if (buf->b_p_bl == find_listed | |
2172 #ifdef FEAT_DIFF | |
2173 && (!diffmode || diff_mode_buf(buf)) | |
2174 #endif | |
2175 && buflist_match(prog, buf) != NULL) | |
2176 { | |
2177 if (match >= 0) /* already found a match */ | |
2178 { | |
2179 match = -2; | |
2180 break; | |
2181 } | |
2182 match = buf->b_fnum; /* remember first match */ | |
2183 } | |
2184 | |
2185 vim_free(prog); | |
2186 if (match >= 0) /* found one match */ | |
2187 break; | |
2188 } | |
2189 | |
2190 /* Only search for unlisted buffers if there was no match with | |
2191 * a listed buffer. */ | |
2192 if (!unlisted || !find_listed || match != -1) | |
2193 break; | |
2194 find_listed = FALSE; | |
2195 } | |
2196 | |
2197 vim_free(pat); | |
2198 } | |
2199 | |
2200 if (match == -2) | |
2201 EMSG2(_("E93: More than one match for %s"), pattern); | |
2202 else if (match < 0) | |
2203 EMSG2(_("E94: No matching buffer for %s"), pattern); | |
2204 return match; | |
2205 } | |
2206 #endif | |
2207 | |
2208 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) | |
2209 | |
2210 /* | |
2211 * Find all buffer names that match. | |
2212 * For command line expansion of ":buf" and ":sbuf". | |
2213 * Return OK if matches found, FAIL otherwise. | |
2214 */ | |
2215 int | |
2216 ExpandBufnames(pat, num_file, file, options) | |
2217 char_u *pat; | |
2218 int *num_file; | |
2219 char_u ***file; | |
2220 int options; | |
2221 { | |
2222 int count = 0; | |
2223 buf_T *buf; | |
2224 int round; | |
2225 char_u *p; | |
2226 int attempt; | |
2227 regprog_T *prog; | |
170 | 2228 char_u *patc; |
7 | 2229 |
2230 *num_file = 0; /* return values in case of FAIL */ | |
2231 *file = NULL; | |
2232 | |
170 | 2233 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */ |
2234 if (*pat == '^') | |
2235 { | |
2236 patc = alloc((unsigned)STRLEN(pat) + 11); | |
2237 if (patc == NULL) | |
2238 return FAIL; | |
2239 STRCPY(patc, "\\(^\\|[\\/]\\)"); | |
2240 STRCPY(patc + 11, pat + 1); | |
2241 } | |
2242 else | |
2243 patc = pat; | |
2244 | |
7 | 2245 /* |
170 | 2246 * attempt == 0: try match with '\<', match at start of word |
267 | 2247 * attempt == 1: try match without '\<', match anywhere |
7 | 2248 */ |
267 | 2249 for (attempt = 0; attempt <= 1; ++attempt) |
7 | 2250 { |
267 | 2251 if (attempt > 0 && patc == pat) |
170 | 2252 break; /* there was no anchor, no need to try again */ |
267 | 2253 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC); |
170 | 2254 if (prog == NULL) |
7 | 2255 { |
170 | 2256 if (patc != pat) |
2257 vim_free(patc); | |
2258 return FAIL; | |
7 | 2259 } |
2260 | |
2261 /* | |
2262 * round == 1: Count the matches. | |
2263 * round == 2: Build the array to keep the matches. | |
2264 */ | |
2265 for (round = 1; round <= 2; ++round) | |
2266 { | |
2267 count = 0; | |
2268 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
2269 { | |
2270 if (!buf->b_p_bl) /* skip unlisted buffers */ | |
2271 continue; | |
2272 p = buflist_match(prog, buf); | |
2273 if (p != NULL) | |
2274 { | |
2275 if (round == 1) | |
2276 ++count; | |
2277 else | |
2278 { | |
2279 if (options & WILD_HOME_REPLACE) | |
2280 p = home_replace_save(buf, p); | |
2281 else | |
2282 p = vim_strsave(p); | |
2283 (*file)[count++] = p; | |
2284 } | |
2285 } | |
2286 } | |
2287 if (count == 0) /* no match found, break here */ | |
2288 break; | |
2289 if (round == 1) | |
2290 { | |
2291 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *))); | |
2292 if (*file == NULL) | |
2293 { | |
2294 vim_free(prog); | |
170 | 2295 if (patc != pat) |
2296 vim_free(patc); | |
7 | 2297 return FAIL; |
2298 } | |
2299 } | |
2300 } | |
2301 vim_free(prog); | |
2302 if (count) /* match(es) found, break here */ | |
2303 break; | |
2304 } | |
2305 | |
170 | 2306 if (patc != pat) |
2307 vim_free(patc); | |
2308 | |
7 | 2309 *num_file = count; |
2310 return (count == 0 ? FAIL : OK); | |
2311 } | |
2312 | |
2313 #endif /* FEAT_CMDL_COMPL */ | |
2314 | |
2315 #ifdef HAVE_BUFLIST_MATCH | |
2316 /* | |
2317 * Check for a match on the file name for buffer "buf" with regprog "prog". | |
2318 */ | |
2319 static char_u * | |
2320 buflist_match(prog, buf) | |
2321 regprog_T *prog; | |
2322 buf_T *buf; | |
2323 { | |
2324 char_u *match; | |
2325 | |
2326 /* First try the short file name, then the long file name. */ | |
2327 match = fname_match(prog, buf->b_sfname); | |
2328 if (match == NULL) | |
2329 match = fname_match(prog, buf->b_ffname); | |
2330 | |
2331 return match; | |
2332 } | |
2333 | |
2334 /* | |
2335 * Try matching the regexp in "prog" with file name "name". | |
2336 * Return "name" when there is a match, NULL when not. | |
2337 */ | |
2338 static char_u * | |
2339 fname_match(prog, name) | |
2340 regprog_T *prog; | |
2341 char_u *name; | |
2342 { | |
2343 char_u *match = NULL; | |
2344 char_u *p; | |
2345 regmatch_T regmatch; | |
2346 | |
2347 if (name != NULL) | |
2348 { | |
2349 regmatch.regprog = prog; | |
2350 #ifdef CASE_INSENSITIVE_FILENAME | |
2351 regmatch.rm_ic = TRUE; /* Always ignore case */ | |
2352 #else | |
2353 regmatch.rm_ic = FALSE; /* Never ignore case */ | |
2354 #endif | |
2355 | |
2356 if (vim_regexec(®match, name, (colnr_T)0)) | |
2357 match = name; | |
2358 else | |
2359 { | |
2360 /* Replace $(HOME) with '~' and try matching again. */ | |
2361 p = home_replace_save(NULL, name); | |
2362 if (p != NULL && vim_regexec(®match, p, (colnr_T)0)) | |
2363 match = name; | |
2364 vim_free(p); | |
2365 } | |
2366 } | |
2367 | |
2368 return match; | |
2369 } | |
2370 #endif | |
2371 | |
2372 /* | |
2373 * find file in buffer list by number | |
2374 */ | |
2375 buf_T * | |
2376 buflist_findnr(nr) | |
2377 int nr; | |
2378 { | |
2379 buf_T *buf; | |
2380 | |
2381 if (nr == 0) | |
2382 nr = curwin->w_alt_fnum; | |
2383 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
2384 if (buf->b_fnum == nr) | |
2385 return (buf); | |
2386 return NULL; | |
2387 } | |
2388 | |
2389 /* | |
2390 * Get name of file 'n' in the buffer list. | |
2391 * When the file has no name an empty string is returned. | |
2392 * home_replace() is used to shorten the file name (used for marks). | |
2393 * Returns a pointer to allocated memory, of NULL when failed. | |
2394 */ | |
2395 char_u * | |
2396 buflist_nr2name(n, fullname, helptail) | |
2397 int n; | |
2398 int fullname; | |
2399 int helptail; /* for help buffers return tail only */ | |
2400 { | |
2401 buf_T *buf; | |
2402 | |
2403 buf = buflist_findnr(n); | |
2404 if (buf == NULL) | |
2405 return NULL; | |
2406 return home_replace_save(helptail ? buf : NULL, | |
2407 fullname ? buf->b_ffname : buf->b_fname); | |
2408 } | |
2409 | |
2410 /* | |
2411 * Set the "lnum" and "col" for the buffer "buf" and the current window. | |
2412 * When "copy_options" is TRUE save the local window option values. | |
2413 * When "lnum" is 0 only do the options. | |
2414 */ | |
2415 static void | |
2416 buflist_setfpos(buf, win, lnum, col, copy_options) | |
2417 buf_T *buf; | |
2418 win_T *win; | |
2419 linenr_T lnum; | |
2420 colnr_T col; | |
2421 int copy_options; | |
2422 { | |
2423 wininfo_T *wip; | |
2424 | |
2425 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next) | |
2426 if (wip->wi_win == win) | |
2427 break; | |
2428 if (wip == NULL) | |
2429 { | |
2430 /* allocate a new entry */ | |
2431 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T)); | |
2432 if (wip == NULL) | |
2433 return; | |
2434 wip->wi_win = win; | |
2435 if (lnum == 0) /* set lnum even when it's 0 */ | |
2436 lnum = 1; | |
2437 } | |
2438 else | |
2439 { | |
2440 /* remove the entry from the list */ | |
2441 if (wip->wi_prev) | |
2442 wip->wi_prev->wi_next = wip->wi_next; | |
2443 else | |
2444 buf->b_wininfo = wip->wi_next; | |
2445 if (wip->wi_next) | |
2446 wip->wi_next->wi_prev = wip->wi_prev; | |
2447 if (copy_options && wip->wi_optset) | |
2448 { | |
2449 clear_winopt(&wip->wi_opt); | |
2450 #ifdef FEAT_FOLDING | |
2451 deleteFoldRecurse(&wip->wi_folds); | |
2452 #endif | |
2453 } | |
2454 } | |
2455 if (lnum != 0) | |
2456 { | |
2457 wip->wi_fpos.lnum = lnum; | |
2458 wip->wi_fpos.col = col; | |
2459 } | |
2460 if (copy_options) | |
2461 { | |
2462 /* Save the window-specific option values. */ | |
2463 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt); | |
2464 #ifdef FEAT_FOLDING | |
2465 wip->wi_fold_manual = win->w_fold_manual; | |
2466 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds); | |
2467 #endif | |
2468 wip->wi_optset = TRUE; | |
2469 } | |
2470 | |
2471 /* insert the entry in front of the list */ | |
2472 wip->wi_next = buf->b_wininfo; | |
2473 buf->b_wininfo = wip; | |
2474 wip->wi_prev = NULL; | |
2475 if (wip->wi_next) | |
2476 wip->wi_next->wi_prev = wip; | |
2477 | |
2478 return; | |
2479 } | |
2480 | |
1743 | 2481 #ifdef FEAT_DIFF |
2482 static int wininfo_other_tab_diff __ARGS((wininfo_T *wip)); | |
2483 | |
2484 /* | |
2485 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab | |
2486 * page. That's because a diff is local to a tab page. | |
2487 */ | |
2488 static int | |
2489 wininfo_other_tab_diff(wip) | |
2490 wininfo_T *wip; | |
2491 { | |
2492 win_T *wp; | |
2493 | |
2494 if (wip->wi_opt.wo_diff) | |
2495 { | |
2496 for (wp = firstwin; wp != NULL; wp = wp->w_next) | |
2497 /* return FALSE when it's a window in the current tab page, thus | |
2498 * the buffer was in diff mode here */ | |
2499 if (wip->wi_win == wp) | |
2500 return FALSE; | |
2501 return TRUE; | |
2502 } | |
2503 return FALSE; | |
2504 } | |
2505 #endif | |
2506 | |
7 | 2507 /* |
2508 * Find info for the current window in buffer "buf". | |
2509 * If not found, return the info for the most recently used window. | |
1743 | 2510 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in |
2511 * another tab page. | |
7 | 2512 * Returns NULL when there isn't any info. |
2513 */ | |
2514 static wininfo_T * | |
1743 | 2515 find_wininfo(buf, skip_diff_buffer) |
7 | 2516 buf_T *buf; |
1876 | 2517 int skip_diff_buffer UNUSED; |
7 | 2518 { |
2519 wininfo_T *wip; | |
2520 | |
2521 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next) | |
1743 | 2522 if (wip->wi_win == curwin |
2523 #ifdef FEAT_DIFF | |
2524 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip)) | |
2525 #endif | |
2526 ) | |
7 | 2527 break; |
1743 | 2528 |
2529 /* If no wininfo for curwin, use the first in the list (that doesn't have | |
2530 * 'diff' set and is in another tab page). */ | |
2531 if (wip == NULL) | |
2532 { | |
2533 #ifdef FEAT_DIFF | |
2534 if (skip_diff_buffer) | |
2535 { | |
2536 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next) | |
2537 if (!wininfo_other_tab_diff(wip)) | |
2538 break; | |
2539 } | |
2540 else | |
2541 #endif | |
2542 wip = buf->b_wininfo; | |
2543 } | |
7 | 2544 return wip; |
2545 } | |
2546 | |
2547 /* | |
2548 * Reset the local window options to the values last used in this window. | |
2549 * If the buffer wasn't used in this window before, use the values from | |
2550 * the most recently used window. If the values were never set, use the | |
2551 * global values for the window. | |
2552 */ | |
2553 void | |
2554 get_winopts(buf) | |
2555 buf_T *buf; | |
2556 { | |
2557 wininfo_T *wip; | |
2558 | |
2559 clear_winopt(&curwin->w_onebuf_opt); | |
2560 #ifdef FEAT_FOLDING | |
2561 clearFolding(curwin); | |
2562 #endif | |
2563 | |
1743 | 2564 wip = find_wininfo(buf, TRUE); |
7 | 2565 if (wip != NULL && wip->wi_optset) |
2566 { | |
2567 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt); | |
2568 #ifdef FEAT_FOLDING | |
2569 curwin->w_fold_manual = wip->wi_fold_manual; | |
2570 curwin->w_foldinvalid = TRUE; | |
2571 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds); | |
2572 #endif | |
2573 } | |
2574 else | |
2575 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt); | |
2576 | |
2577 #ifdef FEAT_FOLDING | |
2578 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */ | |
2579 if (p_fdls >= 0) | |
2580 curwin->w_p_fdl = p_fdls; | |
2581 #endif | |
2799 | 2582 #ifdef FEAT_SYN_HL |
2583 check_colorcolumn(curwin); | |
2584 #endif | |
7 | 2585 } |
2586 | |
2587 /* | |
2588 * Find the position (lnum and col) for the buffer 'buf' for the current | |
2589 * window. | |
2590 * Returns a pointer to no_position if no position is found. | |
2591 */ | |
2592 pos_T * | |
2593 buflist_findfpos(buf) | |
2594 buf_T *buf; | |
2595 { | |
2596 wininfo_T *wip; | |
1869 | 2597 static pos_T no_position = INIT_POS_T(1, 0, 0); |
7 | 2598 |
1743 | 2599 wip = find_wininfo(buf, FALSE); |
7 | 2600 if (wip != NULL) |
2601 return &(wip->wi_fpos); | |
2602 else | |
2603 return &no_position; | |
2604 } | |
2605 | |
2606 /* | |
2607 * Find the lnum for the buffer 'buf' for the current window. | |
2608 */ | |
2609 linenr_T | |
2610 buflist_findlnum(buf) | |
2611 buf_T *buf; | |
2612 { | |
2613 return buflist_findfpos(buf)->lnum; | |
2614 } | |
2615 | |
2616 #if defined(FEAT_LISTCMDS) || defined(PROTO) | |
2617 /* | |
2618 * List all know file names (for :files and :buffers command). | |
2619 */ | |
2620 void | |
2621 buflist_list(eap) | |
2622 exarg_T *eap; | |
2623 { | |
2624 buf_T *buf; | |
2625 int len; | |
2626 int i; | |
2627 | |
2628 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next) | |
2629 { | |
2630 /* skip unlisted buffers, unless ! was used */ | |
2631 if (!buf->b_p_bl && !eap->forceit) | |
2632 continue; | |
2633 msg_putchar('\n'); | |
2634 if (buf_spname(buf) != NULL) | |
2635 STRCPY(NameBuff, buf_spname(buf)); | |
2636 else | |
2637 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE); | |
2638 | |
302 | 2639 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"", |
7 | 2640 buf->b_fnum, |
2641 buf->b_p_bl ? ' ' : 'u', | |
2642 buf == curbuf ? '%' : | |
2643 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '), | |
2644 buf->b_ml.ml_mfp == NULL ? ' ' : | |
2645 (buf->b_nwindows == 0 ? 'h' : 'a'), | |
2646 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '), | |
2647 (buf->b_flags & BF_READERR) ? 'x' | |
300 | 2648 : (bufIsChanged(buf) ? '+' : ' '), |
2649 NameBuff); | |
7 | 2650 |
2651 /* put "line 999" in column 40 or after the file name */ | |
2652 i = 40 - vim_strsize(IObuff); | |
2653 do | |
2654 { | |
2655 IObuff[len++] = ' '; | |
2656 } while (--i > 0 && len < IOSIZE - 18); | |
1869 | 2657 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len), |
2658 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum | |
272 | 2659 : (long)buflist_findlnum(buf)); |
7 | 2660 msg_outtrans(IObuff); |
2661 out_flush(); /* output one line at a time */ | |
2662 ui_breakcheck(); | |
2663 } | |
2664 } | |
2665 #endif | |
2666 | |
2667 /* | |
2668 * Get file name and line number for file 'fnum'. | |
2669 * Used by DoOneCmd() for translating '%' and '#'. | |
2670 * Used by insert_reg() and cmdline_paste() for '#' register. | |
2671 * Return FAIL if not found, OK for success. | |
2672 */ | |
2673 int | |
2674 buflist_name_nr(fnum, fname, lnum) | |
2675 int fnum; | |
2676 char_u **fname; | |
2677 linenr_T *lnum; | |
2678 { | |
2679 buf_T *buf; | |
2680 | |
2681 buf = buflist_findnr(fnum); | |
2682 if (buf == NULL || buf->b_fname == NULL) | |
2683 return FAIL; | |
2684 | |
2685 *fname = buf->b_fname; | |
2686 *lnum = buflist_findlnum(buf); | |
2687 | |
2688 return OK; | |
2689 } | |
2690 | |
2691 /* | |
2692 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'. | |
2693 * The file name with the full path is also remembered, for when :cd is used. | |
2694 * Returns FAIL for failure (file name already in use by other buffer) | |
2695 * OK otherwise. | |
2696 */ | |
2697 int | |
2698 setfname(buf, ffname, sfname, message) | |
2699 buf_T *buf; | |
2700 char_u *ffname, *sfname; | |
42 | 2701 int message; /* give message when buffer already exists */ |
7 | 2702 { |
42 | 2703 buf_T *obuf = NULL; |
7 | 2704 #ifdef UNIX |
2705 struct stat st; | |
2706 #endif | |
2707 | |
2708 if (ffname == NULL || *ffname == NUL) | |
2709 { | |
2710 /* Removing the name. */ | |
2711 vim_free(buf->b_ffname); | |
2712 vim_free(buf->b_sfname); | |
2713 buf->b_ffname = NULL; | |
2714 buf->b_sfname = NULL; | |
2715 #ifdef UNIX | |
2716 st.st_dev = (dev_T)-1; | |
2717 #endif | |
2718 } | |
2719 else | |
2720 { | |
2721 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */ | |
2722 if (ffname == NULL) /* out of memory */ | |
2723 return FAIL; | |
2724 | |
2725 /* | |
2726 * if the file name is already used in another buffer: | |
2727 * - if the buffer is loaded, fail | |
2728 * - if the buffer is not loaded, delete it from the list | |
2729 */ | |
2730 #ifdef UNIX | |
2731 if (mch_stat((char *)ffname, &st) < 0) | |
2732 st.st_dev = (dev_T)-1; | |
42 | 2733 #endif |
2734 if (!(buf->b_flags & BF_DUMMY)) | |
2735 #ifdef UNIX | |
2736 obuf = buflist_findname_stat(ffname, &st); | |
7 | 2737 #else |
42 | 2738 obuf = buflist_findname(ffname); |
7 | 2739 #endif |
2740 if (obuf != NULL && obuf != buf) | |
2741 { | |
2742 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */ | |
2743 { | |
2744 if (message) | |
2745 EMSG(_("E95: Buffer with this name already exists")); | |
2746 vim_free(ffname); | |
2747 return FAIL; | |
2748 } | |
3365 | 2749 /* delete from the list */ |
2750 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE); | |
7 | 2751 } |
2752 sfname = vim_strsave(sfname); | |
2753 if (ffname == NULL || sfname == NULL) | |
2754 { | |
2755 vim_free(sfname); | |
2756 vim_free(ffname); | |
2757 return FAIL; | |
2758 } | |
2759 #ifdef USE_FNAME_CASE | |
2760 # ifdef USE_LONG_FNAME | |
2761 if (USE_LONG_FNAME) | |
2762 # endif | |
2763 fname_case(sfname, 0); /* set correct case for short file name */ | |
2764 #endif | |
2765 vim_free(buf->b_ffname); | |
2766 vim_free(buf->b_sfname); | |
2767 buf->b_ffname = ffname; | |
2768 buf->b_sfname = sfname; | |
2769 } | |
2770 buf->b_fname = buf->b_sfname; | |
2771 #ifdef UNIX | |
2772 if (st.st_dev == (dev_T)-1) | |
1873 | 2773 buf->b_dev_valid = FALSE; |
7 | 2774 else |
2775 { | |
1873 | 2776 buf->b_dev_valid = TRUE; |
7 | 2777 buf->b_dev = st.st_dev; |
2778 buf->b_ino = st.st_ino; | |
2779 } | |
2780 #endif | |
2781 | |
2782 #ifndef SHORT_FNAME | |
2783 buf->b_shortname = FALSE; | |
2784 #endif | |
2785 | |
2786 buf_name_changed(buf); | |
2787 return OK; | |
2788 } | |
2789 | |
2790 /* | |
41 | 2791 * Crude way of changing the name of a buffer. Use with care! |
2792 * The name should be relative to the current directory. | |
2793 */ | |
2794 void | |
2795 buf_set_name(fnum, name) | |
2796 int fnum; | |
2797 char_u *name; | |
2798 { | |
2799 buf_T *buf; | |
2800 | |
2801 buf = buflist_findnr(fnum); | |
2802 if (buf != NULL) | |
2803 { | |
2804 vim_free(buf->b_sfname); | |
2805 vim_free(buf->b_ffname); | |
844 | 2806 buf->b_ffname = vim_strsave(name); |
2807 buf->b_sfname = NULL; | |
2808 /* Allocate ffname and expand into full path. Also resolves .lnk | |
2809 * files on Win32. */ | |
2810 fname_expand(buf, &buf->b_ffname, &buf->b_sfname); | |
41 | 2811 buf->b_fname = buf->b_sfname; |
2812 } | |
2813 } | |
2814 | |
2815 /* | |
7 | 2816 * Take care of what needs to be done when the name of buffer "buf" has |
2817 * changed. | |
2818 */ | |
2819 void | |
2820 buf_name_changed(buf) | |
2821 buf_T *buf; | |
2822 { | |
2823 /* | |
2824 * If the file name changed, also change the name of the swapfile | |
2825 */ | |
2826 if (buf->b_ml.ml_mfp != NULL) | |
2827 ml_setname(buf); | |
2828 | |
2829 if (curwin->w_buffer == buf) | |
2830 check_arg_idx(curwin); /* check file name for arg list */ | |
2831 #ifdef FEAT_TITLE | |
2832 maketitle(); /* set window title */ | |
2833 #endif | |
2834 #ifdef FEAT_WINDOWS | |
2835 status_redraw_all(); /* status lines need to be redrawn */ | |
2836 #endif | |
2837 fmarks_check_names(buf); /* check named file marks */ | |
2838 ml_timestamp(buf); /* reset timestamp */ | |
2839 } | |
2840 | |
2841 /* | |
2842 * set alternate file name for current window | |
2843 * | |
2844 * Used by do_one_cmd(), do_write() and do_ecmd(). | |
2845 * Return the buffer. | |
2846 */ | |
2847 buf_T * | |
2848 setaltfname(ffname, sfname, lnum) | |
2849 char_u *ffname; | |
2850 char_u *sfname; | |
2851 linenr_T lnum; | |
2852 { | |
2853 buf_T *buf; | |
2854 | |
2855 /* Create a buffer. 'buflisted' is not set if it's a new buffer */ | |
2856 buf = buflist_new(ffname, sfname, lnum, 0); | |
22 | 2857 if (buf != NULL && !cmdmod.keepalt) |
7 | 2858 curwin->w_alt_fnum = buf->b_fnum; |
2859 return buf; | |
2860 } | |
2861 | |
2862 /* | |
2863 * Get alternate file name for current window. | |
2864 * Return NULL if there isn't any, and give error message if requested. | |
2865 */ | |
2866 char_u * | |
2867 getaltfname(errmsg) | |
2868 int errmsg; /* give error message */ | |
2869 { | |
2870 char_u *fname; | |
2871 linenr_T dummy; | |
2872 | |
2873 if (buflist_name_nr(0, &fname, &dummy) == FAIL) | |
2874 { | |
2875 if (errmsg) | |
2876 EMSG(_(e_noalt)); | |
2877 return NULL; | |
2878 } | |
2879 return fname; | |
2880 } | |
2881 | |
2882 /* | |
2883 * Add a file name to the buflist and return its number. | |
2884 * Uses same flags as buflist_new(), except BLN_DUMMY. | |
2885 * | |
2886 * used by qf_init(), main() and doarglist() | |
2887 */ | |
2888 int | |
2889 buflist_add(fname, flags) | |
2890 char_u *fname; | |
2891 int flags; | |
2892 { | |
2893 buf_T *buf; | |
2894 | |
2895 buf = buflist_new(fname, NULL, (linenr_T)0, flags); | |
2896 if (buf != NULL) | |
2897 return buf->b_fnum; | |
2898 return 0; | |
2899 } | |
2900 | |
2901 #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO) | |
2902 /* | |
2903 * Adjust slashes in file names. Called after 'shellslash' was set. | |
2904 */ | |
2905 void | |
2906 buflist_slash_adjust() | |
2907 { | |
2908 buf_T *bp; | |
2909 | |
2910 for (bp = firstbuf; bp != NULL; bp = bp->b_next) | |
2911 { | |
2912 if (bp->b_ffname != NULL) | |
2913 slash_adjust(bp->b_ffname); | |
2914 if (bp->b_sfname != NULL) | |
2915 slash_adjust(bp->b_sfname); | |
2916 } | |
2917 } | |
2918 #endif | |
2919 | |
2920 /* | |
1743 | 2921 * Set alternate cursor position for the current buffer and window "win". |
7 | 2922 * Also save the local window option values. |
2923 */ | |
2924 void | |
1743 | 2925 buflist_altfpos(win) |
2926 win_T *win; | |
7 | 2927 { |
1743 | 2928 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE); |
7 | 2929 } |
2930 | |
2931 /* | |
2932 * Return TRUE if 'ffname' is not the same file as current file. | |
2933 * Fname must have a full path (expanded by mch_FullName()). | |
2934 */ | |
2935 int | |
2936 otherfile(ffname) | |
2937 char_u *ffname; | |
2938 { | |
2939 return otherfile_buf(curbuf, ffname | |
2940 #ifdef UNIX | |
2941 , NULL | |
2942 #endif | |
2943 ); | |
2944 } | |
2945 | |
2946 static int | |
2947 otherfile_buf(buf, ffname | |
2948 #ifdef UNIX | |
2949 , stp | |
2950 #endif | |
2951 ) | |
2952 buf_T *buf; | |
2953 char_u *ffname; | |
2954 #ifdef UNIX | |
2955 struct stat *stp; | |
2956 #endif | |
2957 { | |
2958 /* no name is different */ | |
2959 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL) | |
2960 return TRUE; | |
2961 if (fnamecmp(ffname, buf->b_ffname) == 0) | |
2962 return FALSE; | |
2963 #ifdef UNIX | |
2964 { | |
2965 struct stat st; | |
2966 | |
2967 /* If no struct stat given, get it now */ | |
2968 if (stp == NULL) | |
2969 { | |
1873 | 2970 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0) |
7 | 2971 st.st_dev = (dev_T)-1; |
2972 stp = &st; | |
2973 } | |
2974 /* Use dev/ino to check if the files are the same, even when the names | |
2975 * are different (possible with links). Still need to compare the | |
2976 * name above, for when the file doesn't exist yet. | |
2977 * Problem: The dev/ino changes when a file is deleted (and created | |
2978 * again) and remains the same when renamed/moved. We don't want to | |
2979 * mch_stat() each buffer each time, that would be too slow. Get the | |
2980 * dev/ino again when they appear to match, but not when they appear | |
2981 * to be different: Could skip a buffer when it's actually the same | |
2982 * file. */ | |
2983 if (buf_same_ino(buf, stp)) | |
2984 { | |
2985 buf_setino(buf); | |
2986 if (buf_same_ino(buf, stp)) | |
2987 return FALSE; | |
2988 } | |
2989 } | |
2990 #endif | |
2991 return TRUE; | |
2992 } | |
2993 | |
2994 #if defined(UNIX) || defined(PROTO) | |
2995 /* | |
2996 * Set inode and device number for a buffer. | |
2997 * Must always be called when b_fname is changed!. | |
2998 */ | |
2999 void | |
3000 buf_setino(buf) | |
3001 buf_T *buf; | |
3002 { | |
3003 struct stat st; | |
3004 | |
3005 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0) | |
3006 { | |
1873 | 3007 buf->b_dev_valid = TRUE; |
7 | 3008 buf->b_dev = st.st_dev; |
3009 buf->b_ino = st.st_ino; | |
3010 } | |
3011 else | |
1873 | 3012 buf->b_dev_valid = FALSE; |
7 | 3013 } |
3014 | |
3015 /* | |
3016 * Return TRUE if dev/ino in buffer "buf" matches with "stp". | |
3017 */ | |
3018 static int | |
3019 buf_same_ino(buf, stp) | |
3020 buf_T *buf; | |
3021 struct stat *stp; | |
3022 { | |
1873 | 3023 return (buf->b_dev_valid |
7 | 3024 && stp->st_dev == buf->b_dev |
3025 && stp->st_ino == buf->b_ino); | |
3026 } | |
3027 #endif | |
3028 | |
667 | 3029 /* |
3030 * Print info about the current buffer. | |
3031 */ | |
7 | 3032 void |
3033 fileinfo(fullname, shorthelp, dont_truncate) | |
667 | 3034 int fullname; /* when non-zero print full path */ |
7 | 3035 int shorthelp; |
3036 int dont_truncate; | |
3037 { | |
3038 char_u *name; | |
3039 int n; | |
3040 char_u *p; | |
3041 char_u *buffer; | |
272 | 3042 size_t len; |
7 | 3043 |
3044 buffer = alloc(IOSIZE); | |
3045 if (buffer == NULL) | |
3046 return; | |
3047 | |
3048 if (fullname > 1) /* 2 CTRL-G: include buffer number */ | |
3049 { | |
1869 | 3050 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum); |
7 | 3051 p = buffer + STRLEN(buffer); |
3052 } | |
3053 else | |
3054 p = buffer; | |
3055 | |
3056 *p++ = '"'; | |
3057 if (buf_spname(curbuf) != NULL) | |
3058 STRCPY(p, buf_spname(curbuf)); | |
3059 else | |
3060 { | |
3061 if (!fullname && curbuf->b_fname != NULL) | |
3062 name = curbuf->b_fname; | |
3063 else | |
3064 name = curbuf->b_ffname; | |
3065 home_replace(shorthelp ? curbuf : NULL, name, p, | |
3066 (int)(IOSIZE - (p - buffer)), TRUE); | |
3067 } | |
3068 | |
2280
941ff1cd317a
Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents:
2253
diff
changeset
|
3069 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s", |
7 | 3070 curbufIsChanged() ? (shortmess(SHM_MOD) |
3071 ? " [+]" : _(" [Modified]")) : " ", | |
3072 (curbuf->b_flags & BF_NOTEDITED) | |
3073 #ifdef FEAT_QUICKFIX | |
3074 && !bt_dontwrite(curbuf) | |
3075 #endif | |
3076 ? _("[Not edited]") : "", | |
3077 (curbuf->b_flags & BF_NEW) | |
3078 #ifdef FEAT_QUICKFIX | |
3079 && !bt_dontwrite(curbuf) | |
3080 #endif | |
3081 ? _("[New file]") : "", | |
3082 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "", | |
3083 curbuf->b_p_ro ? (shortmess(SHM_RO) ? "[RO]" | |
3084 : _("[readonly]")) : "", | |
3085 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK) | |
3086 || curbuf->b_p_ro) ? | |
3087 " " : ""); | |
3088 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100 | |
3089 * causes an overflow, thus for large numbers divide instead. */ | |
3090 if (curwin->w_cursor.lnum > 1000000L) | |
3091 n = (int)(((long)curwin->w_cursor.lnum) / | |
3092 ((long)curbuf->b_ml.ml_line_count / 100L)); | |
3093 else | |
3094 n = (int)(((long)curwin->w_cursor.lnum * 100L) / | |
3095 (long)curbuf->b_ml.ml_line_count); | |
3096 if (curbuf->b_ml.ml_flags & ML_EMPTY) | |
3097 { | |
2280
941ff1cd317a
Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents:
2253
diff
changeset
|
3098 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg)); |
7 | 3099 } |
3100 #ifdef FEAT_CMDL_INFO | |
3101 else if (p_ru) | |
3102 { | |
3103 /* Current line and column are already on the screen -- webb */ | |
3104 if (curbuf->b_ml.ml_line_count == 1) | |
2280
941ff1cd317a
Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents:
2253
diff
changeset
|
3105 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n); |
7 | 3106 else |
2280
941ff1cd317a
Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents:
2253
diff
changeset
|
3107 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"), |
7 | 3108 (long)curbuf->b_ml.ml_line_count, n); |
3109 } | |
3110 #endif | |
3111 else | |
3112 { | |
2280
941ff1cd317a
Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents:
2253
diff
changeset
|
3113 vim_snprintf_add((char *)buffer, IOSIZE, |
7 | 3114 _("line %ld of %ld --%d%%-- col "), |
3115 (long)curwin->w_cursor.lnum, | |
3116 (long)curbuf->b_ml.ml_line_count, | |
3117 n); | |
3118 validate_virtcol(); | |
1869 | 3119 len = STRLEN(buffer); |
3120 col_print(buffer + len, IOSIZE - len, | |
7 | 3121 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1); |
3122 } | |
3123 | |
1869 | 3124 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE)); |
7 | 3125 |
3126 if (dont_truncate) | |
3127 { | |
3128 /* Temporarily set msg_scroll to avoid the message being truncated. | |
3129 * First call msg_start() to get the message in the right place. */ | |
3130 msg_start(); | |
3131 n = msg_scroll; | |
3132 msg_scroll = TRUE; | |
3133 msg(buffer); | |
3134 msg_scroll = n; | |
3135 } | |
3136 else | |
3137 { | |
3138 p = msg_trunc_attr(buffer, FALSE, 0); | |
3139 if (restart_edit != 0 || (msg_scrolled && !need_wait_return)) | |
3140 /* Need to repeat the message after redrawing when: | |
3141 * - When restart_edit is set (otherwise there will be a delay | |
3142 * before redrawing). | |
3143 * - When the screen was scrolled but there is no wait-return | |
3144 * prompt. */ | |
678 | 3145 set_keep_msg(p, 0); |
7 | 3146 } |
3147 | |
3148 vim_free(buffer); | |
3149 } | |
3150 | |
3151 void | |
1869 | 3152 col_print(buf, buflen, col, vcol) |
7 | 3153 char_u *buf; |
1869 | 3154 size_t buflen; |
7 | 3155 int col; |
3156 int vcol; | |
3157 { | |
3158 if (col == vcol) | |
1869 | 3159 vim_snprintf((char *)buf, buflen, "%d", col); |
7 | 3160 else |
1869 | 3161 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol); |
7 | 3162 } |
3163 | |
3164 #if defined(FEAT_TITLE) || defined(PROTO) | |
3165 /* | |
3166 * put file name in title bar of window and in icon title | |
3167 */ | |
3168 | |
3169 static char_u *lasttitle = NULL; | |
3170 static char_u *lasticon = NULL; | |
3171 | |
3172 void | |
3173 maketitle() | |
3174 { | |
3175 char_u *p; | |
3176 char_u *t_str = NULL; | |
3177 char_u *i_name; | |
3178 char_u *i_str = NULL; | |
3179 int maxlen = 0; | |
3180 int len; | |
3181 int mustset; | |
3182 char_u buf[IOSIZE]; | |
3183 int off; | |
3184 | |
3185 if (!redrawing()) | |
3186 { | |
3187 /* Postpone updating the title when 'lazyredraw' is set. */ | |
3188 need_maketitle = TRUE; | |
3189 return; | |
3190 } | |
3191 | |
3192 need_maketitle = FALSE; | |
2403
ce5a380d5144
Fix: when resetting both 'title' and 'icon' the title would be set after a
Bram Moolenaar <bram@vim.org>
parents:
2394
diff
changeset
|
3193 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL) |
7 | 3194 return; |
3195 | |
3196 if (p_title) | |
3197 { | |
3198 if (p_titlelen > 0) | |
3199 { | |
3200 maxlen = p_titlelen * Columns / 100; | |
3201 if (maxlen < 10) | |
3202 maxlen = 10; | |
3203 } | |
3204 | |
3205 t_str = buf; | |
3206 if (*p_titlestring != NUL) | |
3207 { | |
3208 #ifdef FEAT_STL_OPT | |
771 | 3209 if (stl_syntax & STL_IN_TITLE) |
3210 { | |
3211 int use_sandbox = FALSE; | |
3212 int save_called_emsg = called_emsg; | |
675 | 3213 |
3214 # ifdef FEAT_EVAL | |
771 | 3215 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0); |
675 | 3216 # endif |
771 | 3217 called_emsg = FALSE; |
7 | 3218 build_stl_str_hl(curwin, t_str, sizeof(buf), |
675 | 3219 p_titlestring, use_sandbox, |
681 | 3220 0, maxlen, NULL, NULL); |
771 | 3221 if (called_emsg) |
3222 set_string_option_direct((char_u *)"titlestring", -1, | |
3223 (char_u *)"", OPT_FREE, SID_ERROR); | |
3224 called_emsg |= save_called_emsg; | |
3225 } | |
7 | 3226 else |
3227 #endif | |
3228 t_str = p_titlestring; | |
3229 } | |
3230 else | |
3231 { | |
3232 /* format: "fname + (path) (1 of 2) - VIM" */ | |
3233 | |
3234 if (curbuf->b_fname == NULL) | |
2768 | 3235 vim_strncpy(buf, (char_u *)_("[No Name]"), IOSIZE - 100); |
7 | 3236 else |
3237 { | |
3238 p = transstr(gettail(curbuf->b_fname)); | |
416 | 3239 vim_strncpy(buf, p, IOSIZE - 100); |
7 | 3240 vim_free(p); |
3241 } | |
3242 | |
3243 switch (bufIsChanged(curbuf) | |
3244 + (curbuf->b_p_ro * 2) | |
3245 + (!curbuf->b_p_ma * 4)) | |
3246 { | |
3247 case 1: STRCAT(buf, " +"); break; | |
3248 case 2: STRCAT(buf, " ="); break; | |
3249 case 3: STRCAT(buf, " =+"); break; | |
3250 case 4: | |
3251 case 6: STRCAT(buf, " -"); break; | |
3252 case 5: | |
3253 case 7: STRCAT(buf, " -+"); break; | |
3254 } | |
3255 | |
3256 if (curbuf->b_fname != NULL) | |
3257 { | |
3258 /* Get path of file, replace home dir with ~ */ | |
3259 off = (int)STRLEN(buf); | |
3260 buf[off++] = ' '; | |
3261 buf[off++] = '('; | |
3262 home_replace(curbuf, curbuf->b_ffname, | |
3263 buf + off, IOSIZE - off, TRUE); | |
3264 #ifdef BACKSLASH_IN_FILENAME | |
3265 /* avoid "c:/name" to be reduced to "c" */ | |
3266 if (isalpha(buf[off]) && buf[off + 1] == ':') | |
3267 off += 2; | |
3268 #endif | |
3269 /* remove the file name */ | |
39 | 3270 p = gettail_sep(buf + off); |
7 | 3271 if (p == buf + off) |
3272 /* must be a help buffer */ | |
416 | 3273 vim_strncpy(buf + off, (char_u *)_("help"), |
1869 | 3274 (size_t)(IOSIZE - off - 1)); |
7 | 3275 else |
3276 *p = NUL; | |
39 | 3277 |
7 | 3278 /* translate unprintable chars */ |
3279 p = transstr(buf + off); | |
1869 | 3280 vim_strncpy(buf + off, p, (size_t)(IOSIZE - off - 1)); |
7 | 3281 vim_free(p); |
3282 STRCAT(buf, ")"); | |
3283 } | |
3284 | |
1869 | 3285 append_arg_number(curwin, buf, IOSIZE, FALSE); |
7 | 3286 |
3287 #if defined(FEAT_CLIENTSERVER) | |
3288 if (serverName != NULL) | |
3289 { | |
3290 STRCAT(buf, " - "); | |
2768 | 3291 vim_strcat(buf, serverName, IOSIZE); |
7 | 3292 } |
3293 else | |
3294 #endif | |
3295 STRCAT(buf, " - VIM"); | |
3296 | |
3297 if (maxlen > 0) | |
3298 { | |
3299 /* make it shorter by removing a bit in the middle */ | |
3277 | 3300 if (vim_strsize(buf) > maxlen) |
3301 trunc_string(buf, buf, maxlen, IOSIZE); | |
7 | 3302 } |
3303 } | |
3304 } | |
3305 mustset = ti_change(t_str, &lasttitle); | |
3306 | |
3307 if (p_icon) | |
3308 { | |
3309 i_str = buf; | |
3310 if (*p_iconstring != NUL) | |
3311 { | |
3312 #ifdef FEAT_STL_OPT | |
771 | 3313 if (stl_syntax & STL_IN_ICON) |
3314 { | |
3315 int use_sandbox = FALSE; | |
3316 int save_called_emsg = called_emsg; | |
675 | 3317 |
3318 # ifdef FEAT_EVAL | |
771 | 3319 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0); |
675 | 3320 # endif |
771 | 3321 called_emsg = FALSE; |
7 | 3322 build_stl_str_hl(curwin, i_str, sizeof(buf), |
675 | 3323 p_iconstring, use_sandbox, |
681 | 3324 0, 0, NULL, NULL); |
771 | 3325 if (called_emsg) |
3326 set_string_option_direct((char_u *)"iconstring", -1, | |
3327 (char_u *)"", OPT_FREE, SID_ERROR); | |
3328 called_emsg |= save_called_emsg; | |
3329 } | |
7 | 3330 else |
3331 #endif | |
3332 i_str = p_iconstring; | |
3333 } | |
3334 else | |
3335 { | |
3336 if (buf_spname(curbuf) != NULL) | |
3337 i_name = (char_u *)buf_spname(curbuf); | |
3338 else /* use file name only in icon */ | |
3339 i_name = gettail(curbuf->b_ffname); | |
3340 *i_str = NUL; | |
3341 /* Truncate name at 100 bytes. */ | |
835 | 3342 len = (int)STRLEN(i_name); |
7 | 3343 if (len > 100) |
3344 { | |
3345 len -= 100; | |
3346 #ifdef FEAT_MBYTE | |
3347 if (has_mbyte) | |
3348 len += (*mb_tail_off)(i_name, i_name + len) + 1; | |
3349 #endif | |
3350 i_name += len; | |
3351 } | |
3352 STRCPY(i_str, i_name); | |
3353 trans_characters(i_str, IOSIZE); | |
3354 } | |
3355 } | |
3356 | |
3357 mustset |= ti_change(i_str, &lasticon); | |
3358 | |
3359 if (mustset) | |
3360 resettitle(); | |
3361 } | |
3362 | |
3363 /* | |
3364 * Used for title and icon: Check if "str" differs from "*last". Set "*last" | |
3365 * from "str" if it does. | |
3366 * Return TRUE when "*last" changed. | |
3367 */ | |
3368 static int | |
3369 ti_change(str, last) | |
3370 char_u *str; | |
3371 char_u **last; | |
3372 { | |
3373 if ((str == NULL) != (*last == NULL) | |
3374 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0)) | |
3375 { | |
3376 vim_free(*last); | |
3377 if (str == NULL) | |
3378 *last = NULL; | |
3379 else | |
3380 *last = vim_strsave(str); | |
3381 return TRUE; | |
3382 } | |
3383 return FALSE; | |
3384 } | |
3385 | |
3386 /* | |
3387 * Put current window title back (used after calling a shell) | |
3388 */ | |
3389 void | |
3390 resettitle() | |
3391 { | |
3392 mch_settitle(lasttitle, lasticon); | |
3393 } | |
358 | 3394 |
3395 # if defined(EXITFREE) || defined(PROTO) | |
3396 void | |
3397 free_titles() | |
3398 { | |
3399 vim_free(lasttitle); | |
3400 vim_free(lasticon); | |
3401 } | |
3402 # endif | |
3403 | |
7 | 3404 #endif /* FEAT_TITLE */ |
3405 | |
686 | 3406 #if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO) |
7 | 3407 /* |
675 | 3408 * Build a string from the status line items in "fmt". |
7 | 3409 * Return length of string in screen cells. |
3410 * | |
675 | 3411 * Normally works for window "wp", except when working for 'tabline' then it |
3412 * is "curwin". | |
3413 * | |
7 | 3414 * Items are drawn interspersed with the text that surrounds it |
3415 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation | |
3416 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional | |
3417 * | |
3418 * If maxwidth is not zero, the string will be filled at any middle marker | |
3419 * or truncated if too long, fillchar is used for all whitespace. | |
3420 */ | |
3421 int | |
2694 | 3422 build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar, |
3423 maxwidth, hltab, tabtab) | |
7 | 3424 win_T *wp; |
686 | 3425 char_u *out; /* buffer to write into != NameBuff */ |
7 | 3426 size_t outlen; /* length of out[] */ |
3427 char_u *fmt; | |
1883 | 3428 int use_sandbox UNUSED; /* "fmt" was set insecurely, use sandbox */ |
7 | 3429 int fillchar; |
3430 int maxwidth; | |
681 | 3431 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */ |
3432 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */ | |
7 | 3433 { |
3434 char_u *p; | |
3435 char_u *s; | |
3436 char_u *t; | |
3437 char_u *linecont; | |
3438 #ifdef FEAT_EVAL | |
3439 win_T *o_curwin; | |
3440 buf_T *o_curbuf; | |
3441 #endif | |
3442 int empty_line; | |
3443 colnr_T virtcol; | |
3444 long l; | |
3445 long n; | |
3446 int prevchar_isflag; | |
3447 int prevchar_isitem; | |
3448 int itemisflag; | |
3449 int fillable; | |
3450 char_u *str; | |
3451 long num; | |
3452 int width; | |
3453 int itemcnt; | |
3454 int curitem; | |
3455 int groupitem[STL_MAX_ITEM]; | |
3456 int groupdepth; | |
3457 struct stl_item | |
3458 { | |
3459 char_u *start; | |
3460 int minwid; | |
3461 int maxwid; | |
3462 enum | |
3463 { | |
3464 Normal, | |
3465 Empty, | |
3466 Group, | |
3467 Middle, | |
3468 Highlight, | |
681 | 3469 TabPage, |
7 | 3470 Trunc |
3471 } type; | |
3472 } item[STL_MAX_ITEM]; | |
3473 int minwid; | |
3474 int maxwid; | |
3475 int zeropad; | |
3476 char_u base; | |
3477 char_u opt; | |
3478 #define TMPLEN 70 | |
3479 char_u tmp[TMPLEN]; | |
678 | 3480 char_u *usefmt = fmt; |
681 | 3481 struct stl_hlrec *sp; |
678 | 3482 |
3483 #ifdef FEAT_EVAL | |
3484 /* | |
3485 * When the format starts with "%!" then evaluate it as an expression and | |
3486 * use the result as the actual format string. | |
3487 */ | |
3488 if (fmt[0] == '%' && fmt[1] == '!') | |
3489 { | |
3490 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox); | |
3491 if (usefmt == NULL) | |
943 | 3492 usefmt = fmt; |
678 | 3493 } |
3494 #endif | |
7 | 3495 |
3496 if (fillchar == 0) | |
3497 fillchar = ' '; | |
819 | 3498 #ifdef FEAT_MBYTE |
3499 /* Can't handle a multi-byte fill character yet. */ | |
3500 else if (mb_char2len(fillchar) > 1) | |
3501 fillchar = '-'; | |
3502 #endif | |
3503 | |
7 | 3504 /* |
3505 * Get line & check if empty (cursorpos will show "0-1"). | |
3506 * If inversion is possible we use it. Else '=' characters are used. | |
3507 */ | |
3508 linecont = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE); | |
3509 empty_line = (*linecont == NUL); | |
3510 | |
3511 groupdepth = 0; | |
3512 p = out; | |
3513 curitem = 0; | |
3514 prevchar_isflag = TRUE; | |
3515 prevchar_isitem = FALSE; | |
678 | 3516 for (s = usefmt; *s; ) |
7 | 3517 { |
2704 | 3518 if (curitem == STL_MAX_ITEM) |
3519 { | |
3520 /* There are too many items. Add the error code to the statusline | |
3521 * to give the user a hint about what went wrong. */ | |
3522 if (p + 6 < out + outlen) | |
3523 { | |
3524 mch_memmove(p, " E541", (size_t)5); | |
3525 p += 5; | |
3526 } | |
3527 break; | |
3528 } | |
3529 | |
7 | 3530 if (*s != NUL && *s != '%') |
3531 prevchar_isflag = prevchar_isitem = FALSE; | |
3532 | |
3533 /* | |
3534 * Handle up to the next '%' or the end. | |
3535 */ | |
3536 while (*s != NUL && *s != '%' && p + 1 < out + outlen) | |
3537 *p++ = *s++; | |
3538 if (*s == NUL || p + 1 >= out + outlen) | |
3539 break; | |
3540 | |
3541 /* | |
3542 * Handle one '%' item. | |
3543 */ | |
3544 s++; | |
2694 | 3545 if (*s == NUL) /* ignore trailing % */ |
3546 break; | |
7 | 3547 if (*s == '%') |
3548 { | |
3549 if (p + 1 >= out + outlen) | |
3550 break; | |
3551 *p++ = *s++; | |
3552 prevchar_isflag = prevchar_isitem = FALSE; | |
3553 continue; | |
3554 } | |
3555 if (*s == STL_MIDDLEMARK) | |
3556 { | |
3557 s++; | |
3558 if (groupdepth > 0) | |
3559 continue; | |
3560 item[curitem].type = Middle; | |
3561 item[curitem++].start = p; | |
3562 continue; | |
3563 } | |
3564 if (*s == STL_TRUNCMARK) | |
3565 { | |
3566 s++; | |
3567 item[curitem].type = Trunc; | |
3568 item[curitem++].start = p; | |
3569 continue; | |
3570 } | |
3571 if (*s == ')') | |
3572 { | |
3573 s++; | |
3574 if (groupdepth < 1) | |
3575 continue; | |
3576 groupdepth--; | |
3577 | |
3578 t = item[groupitem[groupdepth]].start; | |
3579 *p = NUL; | |
3580 l = vim_strsize(t); | |
3581 if (curitem > groupitem[groupdepth] + 1 | |
3582 && item[groupitem[groupdepth]].minwid == 0) | |
3583 { | |
3584 /* remove group if all items are empty */ | |
3585 for (n = groupitem[groupdepth] + 1; n < curitem; n++) | |
3586 if (item[n].type == Normal) | |
3587 break; | |
3588 if (n == curitem) | |
3589 { | |
3590 p = t; | |
3591 l = 0; | |
3592 } | |
3593 } | |
3594 if (l > item[groupitem[groupdepth]].maxwid) | |
3595 { | |
3596 /* truncate, remove n bytes of text at the start */ | |
3597 #ifdef FEAT_MBYTE | |
3598 if (has_mbyte) | |
3599 { | |
3600 /* Find the first character that should be included. */ | |
3601 n = 0; | |
3602 while (l >= item[groupitem[groupdepth]].maxwid) | |
3603 { | |
3604 l -= ptr2cells(t + n); | |
474 | 3605 n += (*mb_ptr2len)(t + n); |
7 | 3606 } |
3607 } | |
3608 else | |
3609 #endif | |
835 | 3610 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1; |
7 | 3611 |
3612 *t = '<'; | |
1869 | 3613 mch_memmove(t + 1, t + n, (size_t)(p - (t + n))); |
7 | 3614 p = p - n + 1; |
3615 #ifdef FEAT_MBYTE | |
3616 /* Fill up space left over by half a double-wide char. */ | |
3617 while (++l < item[groupitem[groupdepth]].minwid) | |
3618 *p++ = fillchar; | |
3619 #endif | |
3620 | |
3621 /* correct the start of the items for the truncation */ | |
3622 for (l = groupitem[groupdepth] + 1; l < curitem; l++) | |
3623 { | |
3624 item[l].start -= n; | |
3625 if (item[l].start < t) | |
3626 item[l].start = t; | |
3627 } | |
3628 } | |
3629 else if (abs(item[groupitem[groupdepth]].minwid) > l) | |
3630 { | |
3631 /* fill */ | |
3632 n = item[groupitem[groupdepth]].minwid; | |
3633 if (n < 0) | |
3634 { | |
3635 /* fill by appending characters */ | |
3636 n = 0 - n; | |
3637 while (l++ < n && p + 1 < out + outlen) | |
3638 *p++ = fillchar; | |
3639 } | |
3640 else | |
3641 { | |
3642 /* fill by inserting characters */ | |
1869 | 3643 mch_memmove(t + n - l, t, (size_t)(p - t)); |
7 | 3644 l = n - l; |
3645 if (p + l >= out + outlen) | |
835 | 3646 l = (long)((out + outlen) - p - 1); |
7 | 3647 p += l; |
3648 for (n = groupitem[groupdepth] + 1; n < curitem; n++) | |
3649 item[n].start += l; | |
3650 for ( ; l > 0; l--) | |
3651 *t++ = fillchar; | |
3652 } | |
3653 } | |
3654 continue; | |
3655 } | |
3656 minwid = 0; | |
3657 maxwid = 9999; | |
3658 zeropad = FALSE; | |
3659 l = 1; | |
3660 if (*s == '0') | |
3661 { | |
3662 s++; | |
3663 zeropad = TRUE; | |
3664 } | |
3665 if (*s == '-') | |
3666 { | |
3667 s++; | |
3668 l = -1; | |
3669 } | |
3670 if (VIM_ISDIGIT(*s)) | |
3671 { | |
3672 minwid = (int)getdigits(&s); | |
3673 if (minwid < 0) /* overflow */ | |
3674 minwid = 0; | |
3675 } | |
678 | 3676 if (*s == STL_USER_HL) |
7 | 3677 { |
3678 item[curitem].type = Highlight; | |
3679 item[curitem].start = p; | |
3680 item[curitem].minwid = minwid > 9 ? 1 : minwid; | |
3681 s++; | |
3682 curitem++; | |
3683 continue; | |
3684 } | |
681 | 3685 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR) |
3686 { | |
3687 if (*s == STL_TABCLOSENR) | |
3688 { | |
3689 if (minwid == 0) | |
3690 { | |
3691 /* %X ends the close label, go back to the previously | |
3692 * define tab label nr. */ | |
3693 for (n = curitem - 1; n >= 0; --n) | |
3694 if (item[n].type == TabPage && item[n].minwid >= 0) | |
3695 { | |
3696 minwid = item[n].minwid; | |
3697 break; | |
3698 } | |
3699 } | |
3700 else | |
3701 /* close nrs are stored as negative values */ | |
3702 minwid = - minwid; | |
3703 } | |
3704 item[curitem].type = TabPage; | |
3705 item[curitem].start = p; | |
3706 item[curitem].minwid = minwid; | |
3707 s++; | |
3708 curitem++; | |
3709 continue; | |
3710 } | |
7 | 3711 if (*s == '.') |
3712 { | |
3713 s++; | |
3714 if (VIM_ISDIGIT(*s)) | |
3715 { | |
3716 maxwid = (int)getdigits(&s); | |
3717 if (maxwid <= 0) /* overflow */ | |
3718 maxwid = 50; | |
3719 } | |
3720 } | |
3721 minwid = (minwid > 50 ? 50 : minwid) * l; | |
3722 if (*s == '(') | |
3723 { | |
3724 groupitem[groupdepth++] = curitem; | |
3725 item[curitem].type = Group; | |
3726 item[curitem].start = p; | |
3727 item[curitem].minwid = minwid; | |
3728 item[curitem].maxwid = maxwid; | |
3729 s++; | |
3730 curitem++; | |
3731 continue; | |
3732 } | |
3733 if (vim_strchr(STL_ALL, *s) == NULL) | |
3734 { | |
3735 s++; | |
3736 continue; | |
3737 } | |
3738 opt = *s++; | |
3739 | |
3740 /* OK - now for the real work */ | |
3741 base = 'D'; | |
3742 itemisflag = FALSE; | |
3743 fillable = TRUE; | |
3744 num = -1; | |
3745 str = NULL; | |
3746 switch (opt) | |
3747 { | |
3748 case STL_FILEPATH: | |
3749 case STL_FULLPATH: | |
3750 case STL_FILENAME: | |
3751 fillable = FALSE; /* don't change ' ' to fillchar */ | |
3752 if (buf_spname(wp->w_buffer) != NULL) | |
3753 STRCPY(NameBuff, buf_spname(wp->w_buffer)); | |
3754 else | |
3755 { | |
3756 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname | |
667 | 3757 : wp->w_buffer->b_fname; |
7 | 3758 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE); |
3759 } | |
3760 trans_characters(NameBuff, MAXPATHL); | |
3761 if (opt != STL_FILENAME) | |
3762 str = NameBuff; | |
3763 else | |
3764 str = gettail(NameBuff); | |
3765 break; | |
3766 | |
3767 case STL_VIM_EXPR: /* '{' */ | |
3768 itemisflag = TRUE; | |
3769 t = p; | |
3770 while (*s != '}' && *s != NUL && p + 1 < out + outlen) | |
3771 *p++ = *s++; | |
3772 if (*s != '}') /* missing '}' or out of space */ | |
3773 break; | |
3774 s++; | |
3775 *p = 0; | |
3776 p = t; | |
3777 | |
3778 #ifdef FEAT_EVAL | |
1869 | 3779 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum); |
7 | 3780 set_internal_string_var((char_u *)"actual_curbuf", tmp); |
3781 | |
3782 o_curbuf = curbuf; | |
3783 o_curwin = curwin; | |
3784 curwin = wp; | |
3785 curbuf = wp->w_buffer; | |
3786 | |
675 | 3787 str = eval_to_string_safe(p, &t, use_sandbox); |
7 | 3788 |
3789 curwin = o_curwin; | |
3790 curbuf = o_curbuf; | |
145 | 3791 do_unlet((char_u *)"g:actual_curbuf", TRUE); |
7 | 3792 |
3793 if (str != NULL && *str != 0) | |
3794 { | |
3795 if (*skipdigits(str) == NUL) | |
3796 { | |
3797 num = atoi((char *)str); | |
3798 vim_free(str); | |
3799 str = NULL; | |
3800 itemisflag = FALSE; | |
3801 } | |
3802 } | |
3803 #endif | |
3804 break; | |
3805 | |
3806 case STL_LINE: | |
3807 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) | |
3808 ? 0L : (long)(wp->w_cursor.lnum); | |
3809 break; | |
3810 | |
3811 case STL_NUMLINES: | |
3812 num = wp->w_buffer->b_ml.ml_line_count; | |
3813 break; | |
3814 | |
3815 case STL_COLUMN: | |
3816 num = !(State & INSERT) && empty_line | |
3817 ? 0 : (int)wp->w_cursor.col + 1; | |
3818 break; | |
3819 | |
3820 case STL_VIRTCOL: | |
3821 case STL_VIRTCOL_ALT: | |
3822 /* In list mode virtcol needs to be recomputed */ | |
3823 virtcol = wp->w_virtcol; | |
3824 if (wp->w_p_list && lcs_tab1 == NUL) | |
3825 { | |
3826 wp->w_p_list = FALSE; | |
3827 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL); | |
3828 wp->w_p_list = TRUE; | |
3829 } | |
3830 ++virtcol; | |
3831 /* Don't display %V if it's the same as %c. */ | |
3832 if (opt == STL_VIRTCOL_ALT | |
3833 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line | |
3834 ? 0 : (int)wp->w_cursor.col + 1))) | |
3835 break; | |
3836 num = (long)virtcol; | |
3837 break; | |
3838 | |
3839 case STL_PERCENTAGE: | |
3840 num = (int)(((long)wp->w_cursor.lnum * 100L) / | |
3841 (long)wp->w_buffer->b_ml.ml_line_count); | |
3842 break; | |
3843 | |
3844 case STL_ALTPERCENT: | |
3845 str = tmp; | |
1869 | 3846 get_rel_pos(wp, str, TMPLEN); |
7 | 3847 break; |
3848 | |
3849 case STL_ARGLISTSTAT: | |
3850 fillable = FALSE; | |
3851 tmp[0] = 0; | |
1869 | 3852 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE)) |
7 | 3853 str = tmp; |
3854 break; | |
3855 | |
3856 case STL_KEYMAP: | |
3857 fillable = FALSE; | |
3858 if (get_keymap_str(wp, tmp, TMPLEN)) | |
3859 str = tmp; | |
3860 break; | |
3861 case STL_PAGENUM: | |
706 | 3862 #if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE) |
686 | 3863 num = printer_page_num; |
7 | 3864 #else |
3865 num = 0; | |
3866 #endif | |
3867 break; | |
3868 | |
3869 case STL_BUFNO: | |
3870 num = wp->w_buffer->b_fnum; | |
3871 break; | |
3872 | |
3873 case STL_OFFSET_X: | |
3874 base = 'X'; | |
3875 case STL_OFFSET: | |
3876 #ifdef FEAT_BYTEOFF | |
3877 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL); | |
3878 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ? | |
3879 0L : l + 1 + (!(State & INSERT) && empty_line ? | |
3880 0 : (int)wp->w_cursor.col); | |
3881 #endif | |
3882 break; | |
3883 | |
3884 case STL_BYTEVAL_X: | |
3885 base = 'X'; | |
3886 case STL_BYTEVAL: | |
1869 | 3887 if (wp->w_cursor.col > (colnr_T)STRLEN(linecont)) |
7 | 3888 num = 0; |
3889 else | |
3890 { | |
3891 #ifdef FEAT_MBYTE | |
3892 num = (*mb_ptr2char)(linecont + wp->w_cursor.col); | |
3893 #else | |
3894 num = linecont[wp->w_cursor.col]; | |
3895 #endif | |
3896 } | |
3897 if (num == NL) | |
3898 num = 0; | |
3899 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC) | |
3900 num = NL; | |
3901 break; | |
3902 | |
3903 case STL_ROFLAG: | |
3904 case STL_ROFLAG_ALT: | |
3905 itemisflag = TRUE; | |
3906 if (wp->w_buffer->b_p_ro) | |
3907 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : "[RO]"); | |
3908 break; | |
3909 | |
3910 case STL_HELPFLAG: | |
3911 case STL_HELPFLAG_ALT: | |
3912 itemisflag = TRUE; | |
3913 if (wp->w_buffer->b_help) | |
3914 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP" | |
809 | 3915 : _("[Help]")); |
7 | 3916 break; |
3917 | |
3918 #ifdef FEAT_AUTOCMD | |
3919 case STL_FILETYPE: | |
3920 if (*wp->w_buffer->b_p_ft != NUL | |
3921 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3) | |
3922 { | |
272 | 3923 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]", |
3924 wp->w_buffer->b_p_ft); | |
7 | 3925 str = tmp; |
3926 } | |
3927 break; | |
3928 | |
3929 case STL_FILETYPE_ALT: | |
3930 itemisflag = TRUE; | |
3931 if (*wp->w_buffer->b_p_ft != NUL | |
3932 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2) | |
3933 { | |
272 | 3934 vim_snprintf((char *)tmp, sizeof(tmp), ",%s", |
3935 wp->w_buffer->b_p_ft); | |
7 | 3936 for (t = tmp; *t != 0; t++) |
3937 *t = TOUPPER_LOC(*t); | |
3938 str = tmp; | |
3939 } | |
3940 break; | |
3941 #endif | |
3942 | |
3943 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX) | |
3944 case STL_PREVIEWFLAG: | |
3945 case STL_PREVIEWFLAG_ALT: | |
3946 itemisflag = TRUE; | |
3947 if (wp->w_p_pvw) | |
3948 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV" | |
3949 : _("[Preview]")); | |
3950 break; | |
2411
68e394361ca3
Add "q" item for 'statusline'. Add w:quickfix_title. (Lech Lorens)
Bram Moolenaar <bram@vim.org>
parents:
2403
diff
changeset
|
3951 |
68e394361ca3
Add "q" item for 'statusline'. Add w:quickfix_title. (Lech Lorens)
Bram Moolenaar <bram@vim.org>
parents:
2403
diff
changeset
|
3952 case STL_QUICKFIX: |
68e394361ca3
Add "q" item for 'statusline'. Add w:quickfix_title. (Lech Lorens)
Bram Moolenaar <bram@vim.org>
parents:
2403
diff
changeset
|
3953 if (bt_quickfix(wp->w_buffer)) |
68e394361ca3
Add "q" item for 'statusline'. Add w:quickfix_title. (Lech Lorens)
Bram Moolenaar <bram@vim.org>
parents:
2403
diff
changeset
|
3954 str = (char_u *)(wp->w_llist_ref |
68e394361ca3
Add "q" item for 'statusline'. Add w:quickfix_title. (Lech Lorens)
Bram Moolenaar <bram@vim.org>
parents:
2403
diff
changeset
|
3955 ? _(msg_loclist) |
68e394361ca3
Add "q" item for 'statusline'. Add w:quickfix_title. (Lech Lorens)
Bram Moolenaar <bram@vim.org>
parents:
2403
diff
changeset
|
3956 : _(msg_qflist)); |
68e394361ca3
Add "q" item for 'statusline'. Add w:quickfix_title. (Lech Lorens)
Bram Moolenaar <bram@vim.org>
parents:
2403
diff
changeset
|
3957 break; |
7 | 3958 #endif |
3959 | |
3960 case STL_MODIFIED: | |
3961 case STL_MODIFIED_ALT: | |
3962 itemisflag = TRUE; | |
3963 switch ((opt == STL_MODIFIED_ALT) | |
3964 + bufIsChanged(wp->w_buffer) * 2 | |
3965 + (!wp->w_buffer->b_p_ma) * 4) | |
3966 { | |
3967 case 2: str = (char_u *)"[+]"; break; | |
3968 case 3: str = (char_u *)",+"; break; | |
3969 case 4: str = (char_u *)"[-]"; break; | |
3970 case 5: str = (char_u *)",-"; break; | |
3971 case 6: str = (char_u *)"[+-]"; break; | |
3972 case 7: str = (char_u *)",+-"; break; | |
3973 } | |
3974 break; | |
678 | 3975 |
3976 case STL_HIGHLIGHT: | |
3977 t = s; | |
3978 while (*s != '#' && *s != NUL) | |
3979 ++s; | |
3980 if (*s == '#') | |
3981 { | |
3982 item[curitem].type = Highlight; | |
3983 item[curitem].start = p; | |
835 | 3984 item[curitem].minwid = -syn_namen2id(t, (int)(s - t)); |
678 | 3985 curitem++; |
3986 } | |
3987 ++s; | |
3988 continue; | |
7 | 3989 } |
3990 | |
3991 item[curitem].start = p; | |
3992 item[curitem].type = Normal; | |
3993 if (str != NULL && *str) | |
3994 { | |
3995 t = str; | |
3996 if (itemisflag) | |
3997 { | |
3998 if ((t[0] && t[1]) | |
3999 && ((!prevchar_isitem && *t == ',') | |
4000 || (prevchar_isflag && *t == ' '))) | |
4001 t++; | |
4002 prevchar_isflag = TRUE; | |
4003 } | |
4004 l = vim_strsize(t); | |
4005 if (l > 0) | |
4006 prevchar_isitem = TRUE; | |
4007 if (l > maxwid) | |
4008 { | |
4009 while (l >= maxwid) | |
4010 #ifdef FEAT_MBYTE | |
4011 if (has_mbyte) | |
4012 { | |
4013 l -= ptr2cells(t); | |
474 | 4014 t += (*mb_ptr2len)(t); |
7 | 4015 } |
4016 else | |
4017 #endif | |
4018 l -= byte2cells(*t++); | |
4019 if (p + 1 >= out + outlen) | |
4020 break; | |
4021 *p++ = '<'; | |
4022 } | |
4023 if (minwid > 0) | |
4024 { | |
4025 for (; l < minwid && p + 1 < out + outlen; l++) | |
4026 { | |
4027 /* Don't put a "-" in front of a digit. */ | |
4028 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t)) | |
4029 *p++ = ' '; | |
4030 else | |
4031 *p++ = fillchar; | |
4032 } | |
4033 minwid = 0; | |
4034 } | |
4035 else | |
4036 minwid *= -1; | |
4037 while (*t && p + 1 < out + outlen) | |
4038 { | |
4039 *p++ = *t++; | |
4040 /* Change a space by fillchar, unless fillchar is '-' and a | |
4041 * digit follows. */ | |
4042 if (fillable && p[-1] == ' ' | |
4043 && (!VIM_ISDIGIT(*t) || fillchar != '-')) | |
4044 p[-1] = fillchar; | |
4045 } | |
4046 for (; l < minwid && p + 1 < out + outlen; l++) | |
4047 *p++ = fillchar; | |
4048 } | |
4049 else if (num >= 0) | |
4050 { | |
4051 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16)); | |
4052 char_u nstr[20]; | |
4053 | |
4054 if (p + 20 >= out + outlen) | |
4055 break; /* not sufficient space */ | |
4056 prevchar_isitem = TRUE; | |
4057 t = nstr; | |
4058 if (opt == STL_VIRTCOL_ALT) | |
4059 { | |
4060 *t++ = '-'; | |
4061 minwid--; | |
4062 } | |
4063 *t++ = '%'; | |
4064 if (zeropad) | |
4065 *t++ = '0'; | |
4066 *t++ = '*'; | |
1869 | 4067 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd'); |
7 | 4068 *t = 0; |
4069 | |
4070 for (n = num, l = 1; n >= nbase; n /= nbase) | |
4071 l++; | |
4072 if (opt == STL_VIRTCOL_ALT) | |
4073 l++; | |
4074 if (l > maxwid) | |
4075 { | |
4076 l += 2; | |
4077 n = l - maxwid; | |
4078 while (l-- > maxwid) | |
4079 num /= nbase; | |
4080 *t++ = '>'; | |
4081 *t++ = '%'; | |
4082 *t = t[-3]; | |
4083 *++t = 0; | |
272 | 4084 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr, |
4085 0, num, n); | |
7 | 4086 } |
4087 else | |
272 | 4088 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr, |
4089 minwid, num); | |
7 | 4090 p += STRLEN(p); |
4091 } | |
4092 else | |
4093 item[curitem].type = Empty; | |
4094 | |
4095 if (opt == STL_VIM_EXPR) | |
4096 vim_free(str); | |
4097 | |
4098 if (num >= 0 || (!itemisflag && str && *str)) | |
4099 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */ | |
4100 curitem++; | |
4101 } | |
4102 *p = NUL; | |
4103 itemcnt = curitem; | |
4104 | |
678 | 4105 #ifdef FEAT_EVAL |
4106 if (usefmt != fmt) | |
4107 vim_free(usefmt); | |
4108 #endif | |
4109 | |
7 | 4110 width = vim_strsize(out); |
4111 if (maxwidth > 0 && width > maxwidth) | |
4112 { | |
1736 | 4113 /* Result is too long, must truncate somewhere. */ |
7 | 4114 l = 0; |
4115 if (itemcnt == 0) | |
4116 s = out; | |
4117 else | |
4118 { | |
4119 for ( ; l < itemcnt; l++) | |
4120 if (item[l].type == Trunc) | |
4121 { | |
4122 /* Truncate at %< item. */ | |
4123 s = item[l].start; | |
4124 break; | |
4125 } | |
4126 if (l == itemcnt) | |
4127 { | |
4128 /* No %< item, truncate first item. */ | |
4129 s = item[0].start; | |
4130 l = 0; | |
4131 } | |
4132 } | |
4133 | |
4134 if (width - vim_strsize(s) >= maxwidth) | |
4135 { | |
4136 /* Truncation mark is beyond max length */ | |
4137 #ifdef FEAT_MBYTE | |
4138 if (has_mbyte) | |
4139 { | |
4140 s = out; | |
4141 width = 0; | |
4142 for (;;) | |
4143 { | |
4144 width += ptr2cells(s); | |
4145 if (width >= maxwidth) | |
4146 break; | |
474 | 4147 s += (*mb_ptr2len)(s); |
7 | 4148 } |
4149 /* Fill up for half a double-wide character. */ | |
4150 while (++width < maxwidth) | |
4151 *s++ = fillchar; | |
4152 } | |
4153 else | |
4154 #endif | |
4155 s = out + maxwidth - 1; | |
4156 for (l = 0; l < itemcnt; l++) | |
4157 if (item[l].start > s) | |
4158 break; | |
4159 itemcnt = l; | |
4160 *s++ = '>'; | |
4161 *s = 0; | |
4162 } | |
4163 else | |
4164 { | |
4165 #ifdef FEAT_MBYTE | |
4166 if (has_mbyte) | |
4167 { | |
4168 n = 0; | |
4169 while (width >= maxwidth) | |
4170 { | |
4171 width -= ptr2cells(s + n); | |
474 | 4172 n += (*mb_ptr2len)(s + n); |
7 | 4173 } |
4174 } | |
4175 else | |
4176 #endif | |
4177 n = width - maxwidth + 1; | |
4178 p = s + n; | |
1618 | 4179 STRMOVE(s + 1, p); |
7 | 4180 *s = '<'; |
4181 | |
4182 /* Fill up for half a double-wide character. */ | |
4183 while (++width < maxwidth) | |
4184 { | |
4185 s = s + STRLEN(s); | |
4186 *s++ = fillchar; | |
4187 *s = NUL; | |
4188 } | |
4189 | |
4190 --n; /* count the '<' */ | |
4191 for (; l < itemcnt; l++) | |
4192 { | |
4193 if (item[l].start - n >= s) | |
4194 item[l].start -= n; | |
4195 else | |
4196 item[l].start = s; | |
4197 } | |
4198 } | |
4199 width = maxwidth; | |
4200 } | |
4201 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen) | |
4202 { | |
4203 /* Apply STL_MIDDLE if any */ | |
4204 for (l = 0; l < itemcnt; l++) | |
4205 if (item[l].type == Middle) | |
4206 break; | |
4207 if (l < itemcnt) | |
4208 { | |
4209 p = item[l].start + maxwidth - width; | |
1618 | 4210 STRMOVE(p, item[l].start); |
7 | 4211 for (s = item[l].start; s < p; s++) |
4212 *s = fillchar; | |
4213 for (l++; l < itemcnt; l++) | |
4214 item[l].start += maxwidth - width; | |
4215 width = maxwidth; | |
4216 } | |
4217 } | |
4218 | |
681 | 4219 /* Store the info about highlighting. */ |
4220 if (hltab != NULL) | |
7 | 4221 { |
681 | 4222 sp = hltab; |
7 | 4223 for (l = 0; l < itemcnt; l++) |
4224 { | |
4225 if (item[l].type == Highlight) | |
4226 { | |
681 | 4227 sp->start = item[l].start; |
4228 sp->userhl = item[l].minwid; | |
4229 sp++; | |
7 | 4230 } |
4231 } | |
681 | 4232 sp->start = NULL; |
4233 sp->userhl = 0; | |
4234 } | |
4235 | |
4236 /* Store the info about tab pages labels. */ | |
4237 if (tabtab != NULL) | |
4238 { | |
4239 sp = tabtab; | |
4240 for (l = 0; l < itemcnt; l++) | |
4241 { | |
4242 if (item[l].type == TabPage) | |
4243 { | |
4244 sp->start = item[l].start; | |
4245 sp->userhl = item[l].minwid; | |
4246 sp++; | |
4247 } | |
4248 } | |
4249 sp->start = NULL; | |
4250 sp->userhl = 0; | |
7 | 4251 } |
4252 | |
4253 return width; | |
4254 } | |
4255 #endif /* FEAT_STL_OPT */ | |
4256 | |
686 | 4257 #if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \ |
4258 || defined(FEAT_GUI_TABLINE) || defined(PROTO) | |
7 | 4259 /* |
1869 | 4260 * Get relative cursor position in window into "buf[buflen]", in the form 99%, |
4261 * using "Top", "Bot" or "All" when appropriate. | |
7 | 4262 */ |
4263 void | |
1869 | 4264 get_rel_pos(wp, buf, buflen) |
7 | 4265 win_T *wp; |
1869 | 4266 char_u *buf; |
4267 int buflen; | |
7 | 4268 { |
4269 long above; /* number of lines above window */ | |
4270 long below; /* number of lines below window */ | |
4271 | |
4272 above = wp->w_topline - 1; | |
4273 #ifdef FEAT_DIFF | |
4274 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill; | |
4275 #endif | |
4276 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1; | |
4277 if (below <= 0) | |
1869 | 4278 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")), |
4279 (size_t)(buflen - 1)); | |
7 | 4280 else if (above <= 0) |
1869 | 4281 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1)); |
7 | 4282 else |
1869 | 4283 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L |
7 | 4284 ? (int)(above / ((above + below) / 100L)) |
4285 : (int)(above * 100L / (above + below))); | |
4286 } | |
4287 #endif | |
4288 | |
4289 /* | |
1869 | 4290 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file. |
7 | 4291 * Return TRUE if it was appended. |
4292 */ | |
1869 | 4293 static int |
4294 append_arg_number(wp, buf, buflen, add_file) | |
7 | 4295 win_T *wp; |
4296 char_u *buf; | |
1869 | 4297 int buflen; |
7 | 4298 int add_file; /* Add "file" before the arg number */ |
4299 { | |
4300 char_u *p; | |
4301 | |
4302 if (ARGCOUNT <= 1) /* nothing to do */ | |
4303 return FALSE; | |
4304 | |
1869 | 4305 p = buf + STRLEN(buf); /* go to the end of the buffer */ |
4306 if (p - buf + 35 >= buflen) /* getting too long */ | |
7 | 4307 return FALSE; |
4308 *p++ = ' '; | |
4309 *p++ = '('; | |
4310 if (add_file) | |
4311 { | |
4312 STRCPY(p, "file "); | |
4313 p += 5; | |
4314 } | |
1869 | 4315 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)), |
4316 wp->w_arg_idx_invalid ? "(%d) of %d)" | |
7 | 4317 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT); |
4318 return TRUE; | |
4319 } | |
4320 | |
4321 /* | |
4322 * If fname is not a full path, make it a full path. | |
4323 * Returns pointer to allocated memory (NULL for failure). | |
4324 */ | |
4325 char_u * | |
4326 fix_fname(fname) | |
4327 char_u *fname; | |
4328 { | |
4329 /* | |
4330 * Force expanding the path always for Unix, because symbolic links may | |
4331 * mess up the full path name, even though it starts with a '/'. | |
4332 * Also expand when there is ".." in the file name, try to remove it, | |
4333 * because "c:/src/../README" is equal to "c:/README". | |
1420 | 4334 * Similarly "c:/src//file" is equal to "c:/src/file". |
7 | 4335 * For MS-Windows also expand names like "longna~1" to "longname". |
4336 */ | |
1082 | 4337 #ifdef UNIX |
7 | 4338 return FullName_save(fname, TRUE); |
4339 #else | |
1420 | 4340 if (!vim_isAbsName(fname) |
4341 || strstr((char *)fname, "..") != NULL | |
4342 || strstr((char *)fname, "//") != NULL | |
4343 # ifdef BACKSLASH_IN_FILENAME | |
4344 || strstr((char *)fname, "\\\\") != NULL | |
4345 # endif | |
4346 # if defined(MSWIN) || defined(DJGPP) | |
7 | 4347 || vim_strchr(fname, '~') != NULL |
1420 | 4348 # endif |
7 | 4349 ) |
4350 return FullName_save(fname, FALSE); | |
4351 | |
4352 fname = vim_strsave(fname); | |
4353 | |
1420 | 4354 # ifdef USE_FNAME_CASE |
4355 # ifdef USE_LONG_FNAME | |
7 | 4356 if (USE_LONG_FNAME) |
1420 | 4357 # endif |
7 | 4358 { |
4359 if (fname != NULL) | |
4360 fname_case(fname, 0); /* set correct case for file name */ | |
4361 } | |
1420 | 4362 # endif |
7 | 4363 |
4364 return fname; | |
4365 #endif | |
4366 } | |
4367 | |
4368 /* | |
4369 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL. | |
4370 * "ffname" becomes a pointer to allocated memory (or NULL). | |
4371 */ | |
4372 void | |
4373 fname_expand(buf, ffname, sfname) | |
1876 | 4374 buf_T *buf UNUSED; |
7 | 4375 char_u **ffname; |
4376 char_u **sfname; | |
4377 { | |
4378 if (*ffname == NULL) /* if no file name given, nothing to do */ | |
4379 return; | |
4380 if (*sfname == NULL) /* if no short file name given, use ffname */ | |
4381 *sfname = *ffname; | |
4382 *ffname = fix_fname(*ffname); /* expand to full path */ | |
4383 | |
4384 #ifdef FEAT_SHORTCUT | |
4385 if (!buf->b_p_bin) | |
4386 { | |
844 | 4387 char_u *rfname; |
7 | 4388 |
4389 /* If the file name is a shortcut file, use the file it links to. */ | |
4390 rfname = mch_resolve_shortcut(*ffname); | |
844 | 4391 if (rfname != NULL) |
7 | 4392 { |
4393 vim_free(*ffname); | |
4394 *ffname = rfname; | |
4395 *sfname = rfname; | |
4396 } | |
4397 } | |
4398 #endif | |
4399 } | |
4400 | |
4401 /* | |
4402 * Get the file name for an argument list entry. | |
4403 */ | |
4404 char_u * | |
4405 alist_name(aep) | |
4406 aentry_T *aep; | |
4407 { | |
4408 buf_T *bp; | |
4409 | |
4410 /* Use the name from the associated buffer if it exists. */ | |
4411 bp = buflist_findnr(aep->ae_fnum); | |
1036 | 4412 if (bp == NULL || bp->b_fname == NULL) |
7 | 4413 return aep->ae_fname; |
4414 return bp->b_fname; | |
4415 } | |
4416 | |
4417 #if defined(FEAT_WINDOWS) || defined(PROTO) | |
4418 /* | |
4419 * do_arg_all(): Open up to 'count' windows, one for each argument. | |
4420 */ | |
4421 void | |
726 | 4422 do_arg_all(count, forceit, keep_tabs) |
7 | 4423 int count; |
4424 int forceit; /* hide buffers in current windows */ | |
1411 | 4425 int keep_tabs; /* keep current tabs, for ":tab drop file" */ |
7 | 4426 { |
4427 int i; | |
4428 win_T *wp, *wpnext; | |
3380 | 4429 char_u *opened; /* Array of weight for which args are open: |
4430 * 0: not opened | |
4431 * 1: opened in other tab | |
4432 * 2: opened in curtab | |
4433 * 3: opened in curtab and curwin | |
4434 */ | |
1411 | 4435 int opened_len; /* length of opened[] */ |
7 | 4436 int use_firstwin = FALSE; /* use first window for arglist */ |
4437 int split_ret = OK; | |
4438 int p_ea_save; | |
4439 alist_T *alist; /* argument list to be used */ | |
4440 buf_T *buf; | |
698 | 4441 tabpage_T *tpnext; |
4442 int had_tab = cmdmod.tab; | |
3380 | 4443 win_T *old_curwin, *last_curwin; |
4444 tabpage_T *old_curtab, *last_curtab; | |
726 | 4445 win_T *new_curwin = NULL; |
4446 tabpage_T *new_curtab = NULL; | |
7 | 4447 |
4448 if (ARGCOUNT <= 0) | |
4449 { | |
4450 /* Don't give an error message. We don't want it when the ":all" | |
4451 * command is in the .vimrc. */ | |
4452 return; | |
4453 } | |
4454 setpcmark(); | |
4455 | |
4456 opened_len = ARGCOUNT; | |
4457 opened = alloc_clear((unsigned)opened_len); | |
4458 if (opened == NULL) | |
4459 return; | |
4460 | |
3380 | 4461 /* Autocommands may do anything to the argument list. Make sure it's not |
4462 * freed while we are working here by "locking" it. We still have to | |
4463 * watch out for its size to be changed. */ | |
4464 alist = curwin->w_alist; | |
4465 ++alist->al_refcount; | |
4466 | |
4467 old_curwin = curwin; | |
4468 old_curtab = curtab; | |
4469 | |
7 | 4470 #ifdef FEAT_GUI |
4471 need_mouse_correct = TRUE; | |
4472 #endif | |
4473 | |
4474 /* | |
4475 * Try closing all windows that are not in the argument list. | |
4476 * Also close windows that are not full width; | |
4477 * When 'hidden' or "forceit" set the buffer becomes hidden. | |
4478 * Windows that have a changed buffer and can't be hidden won't be closed. | |
698 | 4479 * When the ":tab" modifier was used do this for all tab pages. |
7 | 4480 */ |
698 | 4481 if (had_tab > 0) |
3582 | 4482 goto_tabpage_tp(first_tabpage, TRUE); |
698 | 4483 for (;;) |
7 | 4484 { |
698 | 4485 tpnext = curtab->tp_next; |
4486 for (wp = firstwin; wp != NULL; wp = wpnext) | |
7 | 4487 { |
698 | 4488 wpnext = wp->w_next; |
4489 buf = wp->w_buffer; | |
4490 if (buf->b_ffname == NULL | |
3380 | 4491 || (!keep_tabs && buf->b_nwindows > 1) |
698 | 4492 #ifdef FEAT_VERTSPLIT |
4493 || wp->w_width != Columns | |
4494 #endif | |
4495 ) | |
3380 | 4496 i = opened_len; |
698 | 4497 else |
7 | 4498 { |
698 | 4499 /* check if the buffer in this window is in the arglist */ |
3380 | 4500 for (i = 0; i < opened_len; ++i) |
7 | 4501 { |
3380 | 4502 if (i < alist->al_ga.ga_len |
4503 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum | |
4504 || fullpathcmp(alist_name(&AARGLIST(alist)[i]), | |
4505 buf->b_ffname, TRUE) & FPC_SAME)) | |
7 | 4506 { |
3380 | 4507 int weight = 1; |
4508 | |
4509 if (old_curtab == curtab) | |
726 | 4510 { |
3380 | 4511 ++weight; |
4512 if (old_curwin == wp) | |
4513 ++weight; | |
4514 } | |
4515 | |
4516 if (weight > (int)opened[i]) | |
4517 { | |
4518 opened[i] = (char_u)weight; | |
726 | 4519 if (i == 0) |
4520 { | |
3380 | 4521 if (new_curwin != NULL) |
4522 new_curwin->w_arg_idx = opened_len; | |
726 | 4523 new_curwin = wp; |
4524 new_curtab = curtab; | |
4525 } | |
4526 } | |
3380 | 4527 else if (keep_tabs) |
4528 i = opened_len; | |
4529 | |
4530 if (wp->w_alist != alist) | |
698 | 4531 { |
4532 /* Use the current argument list for all windows | |
4533 * containing a file from it. */ | |
4534 alist_unlink(wp->w_alist); | |
3380 | 4535 wp->w_alist = alist; |
698 | 4536 ++wp->w_alist->al_refcount; |
4537 } | |
4538 break; | |
7 | 4539 } |
698 | 4540 } |
4541 } | |
4542 wp->w_arg_idx = i; | |
4543 | |
3380 | 4544 if (i == opened_len && !keep_tabs)/* close this window */ |
698 | 4545 { |
4546 if (P_HID(buf) || forceit || buf->b_nwindows > 1 | |
726 | 4547 || !bufIsChanged(buf)) |
698 | 4548 { |
4549 /* If the buffer was changed, and we would like to hide it, | |
4550 * try autowriting. */ | |
726 | 4551 if (!P_HID(buf) && buf->b_nwindows <= 1 |
4552 && bufIsChanged(buf)) | |
698 | 4553 { |
4554 (void)autowrite(buf, FALSE); | |
4555 #ifdef FEAT_AUTOCMD | |
4556 /* check if autocommands removed the window */ | |
4557 if (!win_valid(wp) || !buf_valid(buf)) | |
4558 { | |
4559 wpnext = firstwin; /* start all over... */ | |
4560 continue; | |
4561 } | |
4562 #endif | |
4563 } | |
4564 #ifdef FEAT_WINDOWS | |
726 | 4565 /* don't close last window */ |
3380 | 4566 if (firstwin == lastwin |
4567 && (first_tabpage->tp_next == NULL || !had_tab)) | |
698 | 4568 #endif |
4569 use_firstwin = TRUE; | |
4570 #ifdef FEAT_WINDOWS | |
4571 else | |
4572 { | |
4573 win_close(wp, !P_HID(buf) && !bufIsChanged(buf)); | |
4574 # ifdef FEAT_AUTOCMD | |
4575 /* check if autocommands removed the next window */ | |
4576 if (!win_valid(wpnext)) | |
4577 wpnext = firstwin; /* start all over... */ | |
4578 # endif | |
4579 } | |
4580 #endif | |
7 | 4581 } |
4582 } | |
4583 } | |
698 | 4584 |
4585 /* Without the ":tab" modifier only do the current tab page. */ | |
4586 if (had_tab == 0 || tpnext == NULL) | |
4587 break; | |
4588 | |
7 | 4589 # ifdef FEAT_AUTOCMD |
698 | 4590 /* check if autocommands removed the next tab page */ |
4591 if (!valid_tabpage(tpnext)) | |
4592 tpnext = first_tabpage; /* start all over...*/ | |
7 | 4593 # endif |
3582 | 4594 goto_tabpage_tp(tpnext, TRUE); |
7 | 4595 } |
4596 | |
4597 /* | |
4598 * Open a window for files in the argument list that don't have one. | |
4599 * ARGCOUNT may change while doing this, because of autocommands. | |
4600 */ | |
3380 | 4601 if (count > opened_len || count <= 0) |
4602 count = opened_len; | |
7 | 4603 |
4604 #ifdef FEAT_AUTOCMD | |
4605 /* Don't execute Win/Buf Enter/Leave autocommands here. */ | |
4606 ++autocmd_no_enter; | |
4607 ++autocmd_no_leave; | |
4608 #endif | |
3380 | 4609 last_curwin = curwin; |
4610 last_curtab = curtab; | |
7 | 4611 win_enter(lastwin, FALSE); |
819 | 4612 #ifdef FEAT_WINDOWS |
4613 /* ":drop all" should re-use an empty window to avoid "--remote-tab" | |
4614 * leaving an empty tab page when executed locally. */ | |
4615 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1 | |
4616 && curbuf->b_ffname == NULL && !curbuf->b_changed) | |
4617 use_firstwin = TRUE; | |
4618 #endif | |
4619 | |
3380 | 4620 for (i = 0; i < count && i < opened_len && !got_int; ++i) |
7 | 4621 { |
4622 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1) | |
4623 arg_had_last = TRUE; | |
3380 | 4624 if (opened[i] > 0) |
7 | 4625 { |
4626 /* Move the already present window to below the current window */ | |
4627 if (curwin->w_arg_idx != i) | |
4628 { | |
4629 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next) | |
4630 { | |
4631 if (wpnext->w_arg_idx == i) | |
4632 { | |
3380 | 4633 if (keep_tabs) |
4634 { | |
4635 new_curwin = wpnext; | |
4636 new_curtab = curtab; | |
4637 } | |
4638 else | |
4639 win_move_after(wpnext, curwin); | |
7 | 4640 break; |
4641 } | |
4642 } | |
4643 } | |
4644 } | |
4645 else if (split_ret == OK) | |
4646 { | |
4647 if (!use_firstwin) /* split current window */ | |
4648 { | |
4649 p_ea_save = p_ea; | |
4650 p_ea = TRUE; /* use space from all windows */ | |
4651 split_ret = win_split(0, WSP_ROOM | WSP_BELOW); | |
4652 p_ea = p_ea_save; | |
4653 if (split_ret == FAIL) | |
4654 continue; | |
4655 } | |
4656 #ifdef FEAT_AUTOCMD | |
4657 else /* first window: do autocmd for leaving this buffer */ | |
4658 --autocmd_no_leave; | |
4659 #endif | |
4660 | |
4661 /* | |
726 | 4662 * edit file "i" |
7 | 4663 */ |
4664 curwin->w_arg_idx = i; | |
726 | 4665 if (i == 0) |
4666 { | |
4667 new_curwin = curwin; | |
4668 new_curtab = curtab; | |
4669 } | |
7 | 4670 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL, |
4671 ECMD_ONE, | |
4672 ((P_HID(curwin->w_buffer) | |
4673 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0) | |
1743 | 4674 + ECMD_OLDBUF, curwin); |
7 | 4675 #ifdef FEAT_AUTOCMD |
4676 if (use_firstwin) | |
4677 ++autocmd_no_leave; | |
4678 #endif | |
4679 use_firstwin = FALSE; | |
4680 } | |
4681 ui_breakcheck(); | |
698 | 4682 |
4683 /* When ":tab" was used open a new tab for a new window repeatedly. */ | |
4684 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm) | |
4685 cmdmod.tab = 9999; | |
7 | 4686 } |
4687 | |
4688 /* Remove the "lock" on the argument list. */ | |
4689 alist_unlink(alist); | |
4690 | |
4691 #ifdef FEAT_AUTOCMD | |
4692 --autocmd_no_enter; | |
4693 #endif | |
3380 | 4694 /* restore last referenced tabpage's curwin */ |
4695 if (last_curtab != new_curtab) | |
4696 { | |
4697 if (valid_tabpage(last_curtab)) | |
3582 | 4698 goto_tabpage_tp(last_curtab, TRUE); |
3380 | 4699 if (win_valid(last_curwin)) |
4700 win_enter(last_curwin, FALSE); | |
4701 } | |
726 | 4702 /* to window with first arg */ |
4703 if (valid_tabpage(new_curtab)) | |
3582 | 4704 goto_tabpage_tp(new_curtab, TRUE); |
726 | 4705 if (win_valid(new_curwin)) |
4706 win_enter(new_curwin, FALSE); | |
4707 | |
7 | 4708 #ifdef FEAT_AUTOCMD |
4709 --autocmd_no_leave; | |
4710 #endif | |
4711 vim_free(opened); | |
4712 } | |
4713 | |
4714 # if defined(FEAT_LISTCMDS) || defined(PROTO) | |
4715 /* | |
4716 * Open a window for a number of buffers. | |
4717 */ | |
4718 void | |
4719 ex_buffer_all(eap) | |
4720 exarg_T *eap; | |
4721 { | |
4722 buf_T *buf; | |
4723 win_T *wp, *wpnext; | |
4724 int split_ret = OK; | |
4725 int p_ea_save; | |
4726 int open_wins = 0; | |
4727 int r; | |
4728 int count; /* Maximum number of windows to open. */ | |
4729 int all; /* When TRUE also load inactive buffers. */ | |
698 | 4730 #ifdef FEAT_WINDOWS |
4731 int had_tab = cmdmod.tab; | |
4732 tabpage_T *tpnext; | |
4733 #endif | |
7 | 4734 |
4735 if (eap->addr_count == 0) /* make as many windows as possible */ | |
4736 count = 9999; | |
4737 else | |
4738 count = eap->line2; /* make as many windows as specified */ | |
4739 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide) | |
4740 all = FALSE; | |
4741 else | |
4742 all = TRUE; | |
4743 | |
4744 setpcmark(); | |
4745 | |
4746 #ifdef FEAT_GUI | |
4747 need_mouse_correct = TRUE; | |
4748 #endif | |
4749 | |
4750 /* | |
4751 * Close superfluous windows (two windows for the same buffer). | |
4752 * Also close windows that are not full-width. | |
4753 */ | |
698 | 4754 #ifdef FEAT_WINDOWS |
4755 if (had_tab > 0) | |
3582 | 4756 goto_tabpage_tp(first_tabpage, TRUE); |
698 | 4757 for (;;) |
7 | 4758 { |
698 | 4759 #endif |
4760 tpnext = curtab->tp_next; | |
4761 for (wp = firstwin; wp != NULL; wp = wpnext) | |
7 | 4762 { |
698 | 4763 wpnext = wp->w_next; |
706 | 4764 if ((wp->w_buffer->b_nwindows > 1 |
698 | 4765 #ifdef FEAT_VERTSPLIT |
4766 || ((cmdmod.split & WSP_VERT) | |
4767 ? wp->w_height + wp->w_status_height < Rows - p_ch | |
706 | 4768 - tabline_height() |
698 | 4769 : wp->w_width != Columns) |
4770 #endif | |
701 | 4771 #ifdef FEAT_WINDOWS |
4772 || (had_tab > 0 && wp != firstwin) | |
4773 #endif | |
3570 | 4774 ) && firstwin != lastwin |
4775 #ifdef FEAT_AUTOCMD | |
4776 && !(wp->w_closing || wp->w_buffer->b_closing) | |
4777 #endif | |
4778 ) | |
698 | 4779 { |
4780 win_close(wp, FALSE); | |
7 | 4781 #ifdef FEAT_AUTOCMD |
698 | 4782 wpnext = firstwin; /* just in case an autocommand does |
4783 something strange with windows */ | |
701 | 4784 tpnext = first_tabpage; /* start all over...*/ |
698 | 4785 open_wins = 0; |
4786 #endif | |
4787 } | |
4788 else | |
4789 ++open_wins; | |
7 | 4790 } |
698 | 4791 |
4792 #ifdef FEAT_WINDOWS | |
4793 /* Without the ":tab" modifier only do the current tab page. */ | |
4794 if (had_tab == 0 || tpnext == NULL) | |
4795 break; | |
3582 | 4796 goto_tabpage_tp(tpnext, TRUE); |
7 | 4797 } |
698 | 4798 #endif |
7 | 4799 |
4800 /* | |
4801 * Go through the buffer list. When a buffer doesn't have a window yet, | |
4802 * open one. Otherwise move the window to the right position. | |
4803 * Watch out for autocommands that delete buffers or windows! | |
4804 */ | |
4805 #ifdef FEAT_AUTOCMD | |
4806 /* Don't execute Win/Buf Enter/Leave autocommands here. */ | |
4807 ++autocmd_no_enter; | |
4808 #endif | |
4809 win_enter(lastwin, FALSE); | |
4810 #ifdef FEAT_AUTOCMD | |
4811 ++autocmd_no_leave; | |
4812 #endif | |
4813 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next) | |
4814 { | |
4815 /* Check if this buffer needs a window */ | |
4816 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl) | |
4817 continue; | |
4818 | |
701 | 4819 #ifdef FEAT_WINDOWS |
4820 if (had_tab != 0) | |
4821 { | |
4822 /* With the ":tab" modifier don't move the window. */ | |
4823 if (buf->b_nwindows > 0) | |
4824 wp = lastwin; /* buffer has a window, skip it */ | |
4825 else | |
4826 wp = NULL; | |
4827 } | |
4828 else | |
4829 #endif | |
4830 { | |
4831 /* Check if this buffer already has a window */ | |
4832 for (wp = firstwin; wp != NULL; wp = wp->w_next) | |
4833 if (wp->w_buffer == buf) | |
4834 break; | |
4835 /* If the buffer already has a window, move it */ | |
4836 if (wp != NULL) | |
4837 win_move_after(wp, curwin); | |
4838 } | |
4839 | |
4840 if (wp == NULL && split_ret == OK) | |
7 | 4841 { |
4842 /* Split the window and put the buffer in it */ | |
4843 p_ea_save = p_ea; | |
4844 p_ea = TRUE; /* use space from all windows */ | |
4845 split_ret = win_split(0, WSP_ROOM | WSP_BELOW); | |
4846 ++open_wins; | |
4847 p_ea = p_ea_save; | |
4848 if (split_ret == FAIL) | |
4849 continue; | |
4850 | |
4851 /* Open the buffer in this window. */ | |
576 | 4852 #if defined(HAS_SWAP_EXISTS_ACTION) |
7 | 4853 swap_exists_action = SEA_DIALOG; |
4854 #endif | |
4855 set_curbuf(buf, DOBUF_GOTO); | |
4856 #ifdef FEAT_AUTOCMD | |
4857 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */ | |
20 | 4858 { |
576 | 4859 #if defined(HAS_SWAP_EXISTS_ACTION) |
20 | 4860 swap_exists_action = SEA_NONE; |
7 | 4861 # endif |
4862 break; | |
4863 } | |
4864 #endif | |
576 | 4865 #if defined(HAS_SWAP_EXISTS_ACTION) |
7 | 4866 if (swap_exists_action == SEA_QUIT) |
4867 { | |
24 | 4868 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) |
4869 cleanup_T cs; | |
4870 | |
4871 /* Reset the error/interrupt/exception state here so that | |
4872 * aborting() returns FALSE when closing a window. */ | |
4873 enter_cleanup(&cs); | |
4874 # endif | |
4875 | |
4876 /* User selected Quit at ATTENTION prompt; close this window. */ | |
7 | 4877 win_close(curwin, TRUE); |
4878 --open_wins; | |
4879 swap_exists_action = SEA_NONE; | |
602 | 4880 swap_exists_did_quit = TRUE; |
24 | 4881 |
4882 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) | |
4883 /* Restore the error/interrupt/exception state if not | |
4884 * discarded by a new aborting error, interrupt, or uncaught | |
4885 * exception. */ | |
4886 leave_cleanup(&cs); | |
4887 # endif | |
7 | 4888 } |
4889 else | |
4890 handle_swap_exists(NULL); | |
4891 #endif | |
4892 } | |
4893 | |
4894 ui_breakcheck(); | |
4895 if (got_int) | |
4896 { | |
4897 (void)vgetc(); /* only break the file loading, not the rest */ | |
4898 break; | |
4899 } | |
20 | 4900 #ifdef FEAT_EVAL |
4901 /* Autocommands deleted the buffer or aborted script processing!!! */ | |
4902 if (aborting()) | |
4903 break; | |
4904 #endif | |
698 | 4905 #ifdef FEAT_WINDOWS |
4906 /* When ":tab" was used open a new tab for a new window repeatedly. */ | |
4907 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm) | |
4908 cmdmod.tab = 9999; | |
4909 #endif | |
7 | 4910 } |
4911 #ifdef FEAT_AUTOCMD | |
4912 --autocmd_no_enter; | |
4913 #endif | |
4914 win_enter(firstwin, FALSE); /* back to first window */ | |
4915 #ifdef FEAT_AUTOCMD | |
4916 --autocmd_no_leave; | |
4917 #endif | |
4918 | |
4919 /* | |
4920 * Close superfluous windows. | |
4921 */ | |
4922 for (wp = lastwin; open_wins > count; ) | |
4923 { | |
4924 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer) | |
4925 || autowrite(wp->w_buffer, FALSE) == OK); | |
4926 #ifdef FEAT_AUTOCMD | |
4927 if (!win_valid(wp)) | |
4928 { | |
4929 /* BufWrite Autocommands made the window invalid, start over */ | |
4930 wp = lastwin; | |
4931 } | |
4932 else | |
4933 #endif | |
4934 if (r) | |
4935 { | |
4936 win_close(wp, !P_HID(wp->w_buffer)); | |
4937 --open_wins; | |
4938 wp = lastwin; | |
4939 } | |
4940 else | |
4941 { | |
4942 wp = wp->w_prev; | |
4943 if (wp == NULL) | |
4944 break; | |
4945 } | |
4946 } | |
4947 } | |
4948 # endif /* FEAT_LISTCMDS */ | |
4949 | |
4950 #endif /* FEAT_WINDOWS */ | |
4951 | |
717 | 4952 static int chk_modeline __ARGS((linenr_T, int)); |
4953 | |
7 | 4954 /* |
4955 * do_modelines() - process mode lines for the current file | |
4956 * | |
717 | 4957 * "flags" can be: |
4958 * OPT_WINONLY only set options local to window | |
4959 * OPT_NOWIN don't set options local to window | |
4960 * | |
7 | 4961 * Returns immediately if the "ml" option isn't set. |
4962 */ | |
4963 void | |
717 | 4964 do_modelines(flags) |
4965 int flags; | |
7 | 4966 { |
23 | 4967 linenr_T lnum; |
4968 int nmlines; | |
4969 static int entered = 0; | |
7 | 4970 |
4971 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0) | |
4972 return; | |
4973 | |
4974 /* Disallow recursive entry here. Can happen when executing a modeline | |
4975 * triggers an autocommand, which reloads modelines with a ":do". */ | |
4976 if (entered) | |
4977 return; | |
4978 | |
4979 ++entered; | |
4980 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines; | |
4981 ++lnum) | |
717 | 4982 if (chk_modeline(lnum, flags) == FAIL) |
7 | 4983 nmlines = 0; |
4984 | |
4985 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines | |
4986 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum) | |
717 | 4987 if (chk_modeline(lnum, flags) == FAIL) |
7 | 4988 nmlines = 0; |
4989 --entered; | |
4990 } | |
4991 | |
4992 #include "version.h" /* for version number */ | |
4993 | |
4994 /* | |
4995 * chk_modeline() - check a single line for a mode string | |
4996 * Return FAIL if an error encountered. | |
4997 */ | |
4998 static int | |
717 | 4999 chk_modeline(lnum, flags) |
7 | 5000 linenr_T lnum; |
717 | 5001 int flags; /* Same as for do_modelines(). */ |
7 | 5002 { |
5003 char_u *s; | |
5004 char_u *e; | |
5005 char_u *linecopy; /* local copy of any modeline found */ | |
5006 int prev; | |
5007 int vers; | |
5008 int end; | |
5009 int retval = OK; | |
5010 char_u *save_sourcing_name; | |
5011 linenr_T save_sourcing_lnum; | |
5012 #ifdef FEAT_EVAL | |
5013 scid_T save_SID; | |
5014 #endif | |
5015 | |
5016 prev = -1; | |
5017 for (s = ml_get(lnum); *s != NUL; ++s) | |
5018 { | |
5019 if (prev == -1 || vim_isspace(prev)) | |
5020 { | |
5021 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0) | |
5022 || STRNCMP(s, "vi:", (size_t)3) == 0) | |
5023 break; | |
5024 if (STRNCMP(s, "vim", 3) == 0) | |
5025 { | |
5026 if (s[3] == '<' || s[3] == '=' || s[3] == '>') | |
5027 e = s + 4; | |
5028 else | |
5029 e = s + 3; | |
5030 vers = getdigits(&e); | |
5031 if (*e == ':' | |
5032 && (s[3] == ':' | |
5033 || (VIM_VERSION_100 >= vers && isdigit(s[3])) | |
5034 || (VIM_VERSION_100 < vers && s[3] == '<') | |
5035 || (VIM_VERSION_100 > vers && s[3] == '>') | |
5036 || (VIM_VERSION_100 == vers && s[3] == '='))) | |
5037 break; | |
5038 } | |
5039 } | |
5040 prev = *s; | |
5041 } | |
5042 | |
5043 if (*s) | |
5044 { | |
5045 do /* skip over "ex:", "vi:" or "vim:" */ | |
5046 ++s; | |
5047 while (s[-1] != ':'); | |
5048 | |
5049 s = linecopy = vim_strsave(s); /* copy the line, it will change */ | |
5050 if (linecopy == NULL) | |
5051 return FAIL; | |
5052 | |
5053 save_sourcing_lnum = sourcing_lnum; | |
5054 save_sourcing_name = sourcing_name; | |
5055 sourcing_lnum = lnum; /* prepare for emsg() */ | |
5056 sourcing_name = (char_u *)"modelines"; | |
5057 | |
5058 end = FALSE; | |
5059 while (end == FALSE) | |
5060 { | |
5061 s = skipwhite(s); | |
5062 if (*s == NUL) | |
5063 break; | |
5064 | |
5065 /* | |
5066 * Find end of set command: ':' or end of line. | |
5067 * Skip over "\:", replacing it with ":". | |
5068 */ | |
5069 for (e = s; *e != ':' && *e != NUL; ++e) | |
5070 if (e[0] == '\\' && e[1] == ':') | |
1618 | 5071 STRMOVE(e, e + 1); |
7 | 5072 if (*e == NUL) |
5073 end = TRUE; | |
5074 | |
5075 /* | |
5076 * If there is a "set" command, require a terminating ':' and | |
5077 * ignore the stuff after the ':'. | |
5078 * "vi:set opt opt opt: foo" -- foo not interpreted | |
5079 * "vi:opt opt opt: foo" -- foo interpreted | |
5080 * Accept "se" for compatibility with Elvis. | |
5081 */ | |
5082 if (STRNCMP(s, "set ", (size_t)4) == 0 | |
5083 || STRNCMP(s, "se ", (size_t)3) == 0) | |
5084 { | |
5085 if (*e != ':') /* no terminating ':'? */ | |
5086 break; | |
5087 end = TRUE; | |
5088 s = vim_strchr(s, ' ') + 1; | |
5089 } | |
5090 *e = NUL; /* truncate the set command */ | |
5091 | |
5092 if (*s != NUL) /* skip over an empty "::" */ | |
5093 { | |
5094 #ifdef FEAT_EVAL | |
5095 save_SID = current_SID; | |
5096 current_SID = SID_MODELINE; | |
5097 #endif | |
717 | 5098 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags); |
7 | 5099 #ifdef FEAT_EVAL |
5100 current_SID = save_SID; | |
5101 #endif | |
5102 if (retval == FAIL) /* stop if error found */ | |
5103 break; | |
5104 } | |
5105 s = e + 1; /* advance to next part */ | |
5106 } | |
5107 | |
5108 sourcing_lnum = save_sourcing_lnum; | |
5109 sourcing_name = save_sourcing_name; | |
5110 | |
5111 vim_free(linecopy); | |
5112 } | |
5113 return retval; | |
5114 } | |
5115 | |
1559 | 5116 #if defined(FEAT_VIMINFO) || defined(PROTO) |
7 | 5117 int |
5118 read_viminfo_bufferlist(virp, writing) | |
5119 vir_T *virp; | |
5120 int writing; | |
5121 { | |
5122 char_u *tab; | |
5123 linenr_T lnum; | |
5124 colnr_T col; | |
5125 buf_T *buf; | |
5126 char_u *sfname; | |
5127 char_u *xline; | |
5128 | |
5129 /* Handle long line and escaped characters. */ | |
5130 xline = viminfo_readstring(virp, 1, FALSE); | |
5131 | |
5132 /* don't read in if there are files on the command-line or if writing: */ | |
5133 if (xline != NULL && !writing && ARGCOUNT == 0 | |
5134 && find_viminfo_parameter('%') != NULL) | |
5135 { | |
5136 /* Format is: <fname> Tab <lnum> Tab <col>. | |
5137 * Watch out for a Tab in the file name, work from the end. */ | |
5138 lnum = 0; | |
5139 col = 0; | |
5140 tab = vim_strrchr(xline, '\t'); | |
5141 if (tab != NULL) | |
5142 { | |
5143 *tab++ = '\0'; | |
1869 | 5144 col = (colnr_T)atoi((char *)tab); |
7 | 5145 tab = vim_strrchr(xline, '\t'); |
5146 if (tab != NULL) | |
5147 { | |
5148 *tab++ = '\0'; | |
5149 lnum = atol((char *)tab); | |
5150 } | |
5151 } | |
5152 | |
5153 /* Expand "~/" in the file name at "line + 1" to a full path. | |
5154 * Then try shortening it by comparing with the current directory */ | |
5155 expand_env(xline, NameBuff, MAXPATHL); | |
1411 | 5156 sfname = shorten_fname1(NameBuff); |
7 | 5157 |
5158 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED); | |
5159 if (buf != NULL) /* just in case... */ | |
5160 { | |
5161 buf->b_last_cursor.lnum = lnum; | |
5162 buf->b_last_cursor.col = col; | |
5163 buflist_setfpos(buf, curwin, lnum, col, FALSE); | |
5164 } | |
5165 } | |
5166 vim_free(xline); | |
5167 | |
5168 return viminfo_readline(virp); | |
5169 } | |
5170 | |
5171 void | |
5172 write_viminfo_bufferlist(fp) | |
5173 FILE *fp; | |
5174 { | |
5175 buf_T *buf; | |
5176 #ifdef FEAT_WINDOWS | |
5177 win_T *win; | |
671 | 5178 tabpage_T *tp; |
7 | 5179 #endif |
5180 char_u *line; | |
23 | 5181 int max_buffers; |
7 | 5182 |
5183 if (find_viminfo_parameter('%') == NULL) | |
5184 return; | |
5185 | |
23 | 5186 /* Without a number -1 is returned: do all buffers. */ |
5187 max_buffers = get_viminfo_parameter('%'); | |
5188 | |
7 | 5189 /* Allocate room for the file name, lnum and col. */ |
1869 | 5190 #define LINE_BUF_LEN (MAXPATHL + 40) |
5191 line = alloc(LINE_BUF_LEN); | |
7 | 5192 if (line == NULL) |
5193 return; | |
5194 | |
5195 #ifdef FEAT_WINDOWS | |
671 | 5196 FOR_ALL_TAB_WINDOWS(tp, win) |
7 | 5197 set_last_cursor(win); |
5198 #else | |
5199 set_last_cursor(curwin); | |
5200 #endif | |
5201 | |
2545
298d8d6e69be
Avoid warnings from the clang compiler. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2520
diff
changeset
|
5202 fputs(_("\n# Buffer list:\n"), fp); |
7 | 5203 for (buf = firstbuf; buf != NULL ; buf = buf->b_next) |
5204 { | |
5205 if (buf->b_fname == NULL | |
5206 || !buf->b_p_bl | |
5207 #ifdef FEAT_QUICKFIX | |
5208 || bt_quickfix(buf) | |
5209 #endif | |
5210 || removable(buf->b_ffname)) | |
5211 continue; | |
5212 | |
23 | 5213 if (max_buffers-- == 0) |
5214 break; | |
7 | 5215 putc('%', fp); |
5216 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE); | |
2280
941ff1cd317a
Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents:
2253
diff
changeset
|
5217 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d", |
7 | 5218 (long)buf->b_last_cursor.lnum, |
5219 buf->b_last_cursor.col); | |
5220 viminfo_writestring(fp, line); | |
5221 } | |
5222 vim_free(line); | |
5223 } | |
5224 #endif | |
5225 | |
5226 | |
5227 /* | |
5228 * Return special buffer name. | |
5229 * Returns NULL when the buffer has a normal file name. | |
5230 */ | |
5231 char * | |
5232 buf_spname(buf) | |
5233 buf_T *buf; | |
5234 { | |
5235 #if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS) | |
5236 if (bt_quickfix(buf)) | |
643 | 5237 { |
1561 | 5238 win_T *win = NULL; |
1559 | 5239 tabpage_T *tp; |
643 | 5240 |
5241 /* | |
5242 * For location list window, w_llist_ref points to the location list. | |
5243 * For quickfix window, w_llist_ref is NULL. | |
5244 */ | |
1559 | 5245 FOR_ALL_TAB_WINDOWS(tp, win) |
643 | 5246 if (win->w_buffer == buf) |
1819 | 5247 goto win_found; |
5248 win_found: | |
643 | 5249 if (win != NULL && win->w_llist_ref != NULL) |
2411
68e394361ca3
Add "q" item for 'statusline'. Add w:quickfix_title. (Lech Lorens)
Bram Moolenaar <bram@vim.org>
parents:
2403
diff
changeset
|
5250 return _(msg_loclist); |
643 | 5251 else |
2411
68e394361ca3
Add "q" item for 'statusline'. Add w:quickfix_title. (Lech Lorens)
Bram Moolenaar <bram@vim.org>
parents:
2403
diff
changeset
|
5252 return _(msg_qflist); |
643 | 5253 } |
7 | 5254 #endif |
5255 #ifdef FEAT_QUICKFIX | |
5256 /* There is no _file_ when 'buftype' is "nofile", b_sfname | |
5257 * contains the name as specified by the user */ | |
5258 if (bt_nofile(buf)) | |
5259 { | |
5260 if (buf->b_sfname != NULL) | |
5261 return (char *)buf->b_sfname; | |
1764 | 5262 return _("[Scratch]"); |
7 | 5263 } |
5264 #endif | |
5265 if (buf->b_fname == NULL) | |
9 | 5266 return _("[No Name]"); |
7 | 5267 return NULL; |
5268 } | |
5269 | |
5270 | |
5271 #if defined(FEAT_SIGNS) || defined(PROTO) | |
5272 /* | |
5273 * Insert the sign into the signlist. | |
5274 */ | |
5275 static void | |
5276 insert_sign(buf, prev, next, id, lnum, typenr) | |
5277 buf_T *buf; /* buffer to store sign in */ | |
5278 signlist_T *prev; /* previous sign entry */ | |
5279 signlist_T *next; /* next sign entry */ | |
5280 int id; /* sign ID */ | |
5281 linenr_T lnum; /* line number which gets the mark */ | |
5282 int typenr; /* typenr of sign we are adding */ | |
5283 { | |
5284 signlist_T *newsign; | |
5285 | |
5286 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE); | |
5287 if (newsign != NULL) | |
5288 { | |
5289 newsign->id = id; | |
5290 newsign->lnum = lnum; | |
5291 newsign->typenr = typenr; | |
5292 newsign->next = next; | |
5293 #ifdef FEAT_NETBEANS_INTG | |
5294 newsign->prev = prev; | |
5295 if (next != NULL) | |
5296 next->prev = newsign; | |
5297 #endif | |
5298 | |
5299 if (prev == NULL) | |
5300 { | |
5301 /* When adding first sign need to redraw the windows to create the | |
5302 * column for signs. */ | |
5303 if (buf->b_signlist == NULL) | |
5304 { | |
5305 redraw_buf_later(buf, NOT_VALID); | |
5306 changed_cline_bef_curs(); | |
5307 } | |
5308 | |
5309 /* first sign in signlist */ | |
5310 buf->b_signlist = newsign; | |
5311 } | |
5312 else | |
5313 prev->next = newsign; | |
5314 } | |
5315 } | |
5316 | |
5317 /* | |
5318 * Add the sign into the signlist. Find the right spot to do it though. | |
5319 */ | |
5320 void | |
5321 buf_addsign(buf, id, lnum, typenr) | |
5322 buf_T *buf; /* buffer to store sign in */ | |
5323 int id; /* sign ID */ | |
5324 linenr_T lnum; /* line number which gets the mark */ | |
5325 int typenr; /* typenr of sign we are adding */ | |
5326 { | |
5327 signlist_T *sign; /* a sign in the signlist */ | |
5328 signlist_T *prev; /* the previous sign */ | |
5329 | |
5330 prev = NULL; | |
5331 for (sign = buf->b_signlist; sign != NULL; sign = sign->next) | |
5332 { | |
5333 if (lnum == sign->lnum && id == sign->id) | |
5334 { | |
5335 sign->typenr = typenr; | |
5336 return; | |
5337 } | |
5338 else if ( | |
5339 #ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */ | |
5340 id < 0 && | |
5341 #endif | |
5342 lnum < sign->lnum) | |
5343 { | |
5344 #ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */ | |
5345 /* XXX - GRP: Is this because of sign slide problem? Or is it | |
5346 * really needed? Or is it because we allow multiple signs per | |
5347 * line? If so, should I add that feature to FEAT_SIGNS? | |
5348 */ | |
5349 while (prev != NULL && prev->lnum == lnum) | |
5350 prev = prev->prev; | |
5351 if (prev == NULL) | |
5352 sign = buf->b_signlist; | |
5353 else | |
5354 sign = prev->next; | |
5355 #endif | |
5356 insert_sign(buf, prev, sign, id, lnum, typenr); | |
5357 return; | |
5358 } | |
5359 prev = sign; | |
5360 } | |
5361 #ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */ | |
5362 /* XXX - GRP: See previous comment */ | |
5363 while (prev != NULL && prev->lnum == lnum) | |
5364 prev = prev->prev; | |
5365 if (prev == NULL) | |
5366 sign = buf->b_signlist; | |
5367 else | |
5368 sign = prev->next; | |
5369 #endif | |
5370 insert_sign(buf, prev, sign, id, lnum, typenr); | |
5371 | |
5372 return; | |
5373 } | |
5374 | |
1869 | 5375 linenr_T |
7 | 5376 buf_change_sign_type(buf, markId, typenr) |
5377 buf_T *buf; /* buffer to store sign in */ | |
5378 int markId; /* sign ID */ | |
5379 int typenr; /* typenr of sign we are adding */ | |
5380 { | |
5381 signlist_T *sign; /* a sign in the signlist */ | |
5382 | |
5383 for (sign = buf->b_signlist; sign != NULL; sign = sign->next) | |
5384 { | |
5385 if (sign->id == markId) | |
5386 { | |
5387 sign->typenr = typenr; | |
5388 return sign->lnum; | |
5389 } | |
5390 } | |
5391 | |
1869 | 5392 return (linenr_T)0; |
7 | 5393 } |
5394 | |
1869 | 5395 int |
7 | 5396 buf_getsigntype(buf, lnum, type) |
5397 buf_T *buf; | |
5398 linenr_T lnum; | |
5399 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */ | |
5400 { | |
5401 signlist_T *sign; /* a sign in a b_signlist */ | |
5402 | |
5403 for (sign = buf->b_signlist; sign != NULL; sign = sign->next) | |
5404 if (sign->lnum == lnum | |
5405 && (type == SIGN_ANY | |
5406 # ifdef FEAT_SIGN_ICONS | |
5407 || (type == SIGN_ICON | |
5408 && sign_get_image(sign->typenr) != NULL) | |
5409 # endif | |
5410 || (type == SIGN_TEXT | |
5411 && sign_get_text(sign->typenr) != NULL) | |
5412 || (type == SIGN_LINEHL | |
5413 && sign_get_attr(sign->typenr, TRUE) != 0))) | |
5414 return sign->typenr; | |
5415 return 0; | |
5416 } | |
5417 | |
5418 | |
5419 linenr_T | |
5420 buf_delsign(buf, id) | |
5421 buf_T *buf; /* buffer sign is stored in */ | |
5422 int id; /* sign id */ | |
5423 { | |
5424 signlist_T **lastp; /* pointer to pointer to current sign */ | |
5425 signlist_T *sign; /* a sign in a b_signlist */ | |
5426 signlist_T *next; /* the next sign in a b_signlist */ | |
5427 linenr_T lnum; /* line number whose sign was deleted */ | |
5428 | |
5429 lastp = &buf->b_signlist; | |
5430 lnum = 0; | |
5431 for (sign = buf->b_signlist; sign != NULL; sign = next) | |
5432 { | |
5433 next = sign->next; | |
5434 if (sign->id == id) | |
5435 { | |
5436 *lastp = next; | |
5437 #ifdef FEAT_NETBEANS_INTG | |
5438 if (next != NULL) | |
5439 next->prev = sign->prev; | |
5440 #endif | |
5441 lnum = sign->lnum; | |
5442 vim_free(sign); | |
5443 break; | |
5444 } | |
5445 else | |
5446 lastp = &sign->next; | |
5447 } | |
5448 | |
5449 /* When deleted the last sign need to redraw the windows to remove the | |
5450 * sign column. */ | |
5451 if (buf->b_signlist == NULL) | |
5452 { | |
5453 redraw_buf_later(buf, NOT_VALID); | |
5454 changed_cline_bef_curs(); | |
5455 } | |
5456 | |
5457 return lnum; | |
5458 } | |
5459 | |
5460 | |
5461 /* | |
5462 * Find the line number of the sign with the requested id. If the sign does | |
5463 * not exist, return 0 as the line number. This will still let the correct file | |
5464 * get loaded. | |
5465 */ | |
5466 int | |
5467 buf_findsign(buf, id) | |
5468 buf_T *buf; /* buffer to store sign in */ | |
5469 int id; /* sign ID */ | |
5470 { | |
5471 signlist_T *sign; /* a sign in the signlist */ | |
5472 | |
5473 for (sign = buf->b_signlist; sign != NULL; sign = sign->next) | |
5474 if (sign->id == id) | |
5475 return sign->lnum; | |
5476 | |
5477 return 0; | |
5478 } | |
5479 | |
5480 int | |
5481 buf_findsign_id(buf, lnum) | |
5482 buf_T *buf; /* buffer whose sign we are searching for */ | |
5483 linenr_T lnum; /* line number of sign */ | |
5484 { | |
5485 signlist_T *sign; /* a sign in the signlist */ | |
5486 | |
5487 for (sign = buf->b_signlist; sign != NULL; sign = sign->next) | |
5488 if (sign->lnum == lnum) | |
5489 return sign->id; | |
5490 | |
5491 return 0; | |
5492 } | |
5493 | |
5494 | |
5495 # if defined(FEAT_NETBEANS_INTG) || defined(PROTO) | |
5496 /* see if a given type of sign exists on a specific line */ | |
5497 int | |
5498 buf_findsigntype_id(buf, lnum, typenr) | |
5499 buf_T *buf; /* buffer whose sign we are searching for */ | |
5500 linenr_T lnum; /* line number of sign */ | |
5501 int typenr; /* sign type number */ | |
5502 { | |
5503 signlist_T *sign; /* a sign in the signlist */ | |
5504 | |
5505 for (sign = buf->b_signlist; sign != NULL; sign = sign->next) | |
5506 if (sign->lnum == lnum && sign->typenr == typenr) | |
5507 return sign->id; | |
5508 | |
5509 return 0; | |
5510 } | |
5511 | |
5512 | |
5513 # if defined(FEAT_SIGN_ICONS) || defined(PROTO) | |
5514 /* return the number of icons on the given line */ | |
5515 int | |
5516 buf_signcount(buf, lnum) | |
5517 buf_T *buf; | |
5518 linenr_T lnum; | |
5519 { | |
5520 signlist_T *sign; /* a sign in the signlist */ | |
5521 int count = 0; | |
5522 | |
5523 for (sign = buf->b_signlist; sign != NULL; sign = sign->next) | |
5524 if (sign->lnum == lnum) | |
5525 if (sign_get_image(sign->typenr) != NULL) | |
5526 count++; | |
5527 | |
5528 return count; | |
5529 } | |
5530 # endif /* FEAT_SIGN_ICONS */ | |
5531 # endif /* FEAT_NETBEANS_INTG */ | |
5532 | |
5533 | |
5534 /* | |
5535 * Delete signs in buffer "buf". | |
5536 */ | |
5537 static void | |
5538 buf_delete_signs(buf) | |
5539 buf_T *buf; | |
5540 { | |
5541 signlist_T *next; | |
5542 | |
5543 while (buf->b_signlist != NULL) | |
5544 { | |
5545 next = buf->b_signlist->next; | |
5546 vim_free(buf->b_signlist); | |
5547 buf->b_signlist = next; | |
5548 } | |
5549 } | |
5550 | |
5551 /* | |
5552 * Delete all signs in all buffers. | |
5553 */ | |
5554 void | |
5555 buf_delete_all_signs() | |
5556 { | |
5557 buf_T *buf; /* buffer we are checking for signs */ | |
5558 | |
5559 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
5560 if (buf->b_signlist != NULL) | |
5561 { | |
5562 /* Need to redraw the windows to remove the sign column. */ | |
5563 redraw_buf_later(buf, NOT_VALID); | |
5564 buf_delete_signs(buf); | |
5565 } | |
5566 } | |
5567 | |
5568 /* | |
5569 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers. | |
5570 */ | |
5571 void | |
5572 sign_list_placed(rbuf) | |
5573 buf_T *rbuf; | |
5574 { | |
5575 buf_T *buf; | |
5576 signlist_T *p; | |
5577 char lbuf[BUFSIZ]; | |
5578 | |
5579 MSG_PUTS_TITLE(_("\n--- Signs ---")); | |
5580 msg_putchar('\n'); | |
5581 if (rbuf == NULL) | |
5582 buf = firstbuf; | |
5583 else | |
5584 buf = rbuf; | |
3411 | 5585 while (buf != NULL && !got_int) |
7 | 5586 { |
5587 if (buf->b_signlist != NULL) | |
5588 { | |
272 | 5589 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname); |
7 | 5590 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D)); |
5591 msg_putchar('\n'); | |
5592 } | |
3411 | 5593 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next) |
7 | 5594 { |
272 | 5595 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"), |
7 | 5596 (long)p->lnum, p->id, sign_typenr2name(p->typenr)); |
5597 MSG_PUTS(lbuf); | |
5598 msg_putchar('\n'); | |
5599 } | |
5600 if (rbuf != NULL) | |
5601 break; | |
5602 buf = buf->b_next; | |
5603 } | |
5604 } | |
5605 | |
5606 /* | |
5607 * Adjust a placed sign for inserted/deleted lines. | |
5608 */ | |
5609 void | |
5610 sign_mark_adjust(line1, line2, amount, amount_after) | |
5611 linenr_T line1; | |
5612 linenr_T line2; | |
5613 long amount; | |
5614 long amount_after; | |
5615 { | |
5616 signlist_T *sign; /* a sign in a b_signlist */ | |
5617 | |
5618 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next) | |
5619 { | |
5620 if (sign->lnum >= line1 && sign->lnum <= line2) | |
5621 { | |
5622 if (amount == MAXLNUM) | |
5623 sign->lnum = line1; | |
5624 else | |
5625 sign->lnum += amount; | |
5626 } | |
5627 else if (sign->lnum > line2) | |
5628 sign->lnum += amount_after; | |
5629 } | |
5630 } | |
5631 #endif /* FEAT_SIGNS */ | |
5632 | |
5633 /* | |
5634 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed. | |
5635 */ | |
5636 void | |
5637 set_buflisted(on) | |
5638 int on; | |
5639 { | |
5640 if (on != curbuf->b_p_bl) | |
5641 { | |
5642 curbuf->b_p_bl = on; | |
5643 #ifdef FEAT_AUTOCMD | |
5644 if (on) | |
5645 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf); | |
5646 else | |
5647 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf); | |
5648 #endif | |
5649 } | |
5650 } | |
5651 | |
5652 /* | |
5653 * Read the file for "buf" again and check if the contents changed. | |
5654 * Return TRUE if it changed or this could not be checked. | |
5655 */ | |
5656 int | |
5657 buf_contents_changed(buf) | |
5658 buf_T *buf; | |
5659 { | |
5660 buf_T *newbuf; | |
5661 int differ = TRUE; | |
5662 linenr_T lnum; | |
5663 aco_save_T aco; | |
5664 exarg_T ea; | |
5665 | |
5666 /* Allocate a buffer without putting it in the buffer list. */ | |
5667 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY); | |
5668 if (newbuf == NULL) | |
5669 return TRUE; | |
5670 | |
5671 /* Force the 'fileencoding' and 'fileformat' to be equal. */ | |
5672 if (prep_exarg(&ea, buf) == FAIL) | |
5673 { | |
5674 wipe_buffer(newbuf, FALSE); | |
5675 return TRUE; | |
5676 } | |
5677 | |
5678 /* set curwin/curbuf to buf and save a few things */ | |
5679 aucmd_prepbuf(&aco, newbuf); | |
5680 | |
625 | 5681 if (ml_open(curbuf) == OK |
7 | 5682 && readfile(buf->b_ffname, buf->b_fname, |
5683 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, | |
5684 &ea, READ_NEW | READ_DUMMY) == OK) | |
5685 { | |
5686 /* compare the two files line by line */ | |
5687 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count) | |
5688 { | |
5689 differ = FALSE; | |
5690 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum) | |
5691 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0) | |
5692 { | |
5693 differ = TRUE; | |
5694 break; | |
5695 } | |
5696 } | |
5697 } | |
5698 vim_free(ea.cmd); | |
5699 | |
5700 /* restore curwin/curbuf and a few other things */ | |
5701 aucmd_restbuf(&aco); | |
5702 | |
5703 if (curbuf != newbuf) /* safety check */ | |
5704 wipe_buffer(newbuf, FALSE); | |
5705 | |
5706 return differ; | |
5707 } | |
5708 | |
5709 /* | |
5710 * Wipe out a buffer and decrement the last buffer number if it was used for | |
5711 * this buffer. Call this to wipe out a temp buffer that does not contain any | |
5712 * marks. | |
5713 */ | |
5714 void | |
5715 wipe_buffer(buf, aucmd) | |
5716 buf_T *buf; | |
1876 | 5717 int aucmd UNUSED; /* When TRUE trigger autocommands. */ |
7 | 5718 { |
5719 if (buf->b_fnum == top_file_num - 1) | |
5720 --top_file_num; | |
5721 | |
5722 #ifdef FEAT_AUTOCMD | |
5723 if (!aucmd) /* Don't trigger BufDelete autocommands here. */ | |
1410 | 5724 block_autocmds(); |
7 | 5725 #endif |
3365 | 5726 close_buffer(NULL, buf, DOBUF_WIPE, FALSE); |
7 | 5727 #ifdef FEAT_AUTOCMD |
5728 if (!aucmd) | |
1410 | 5729 unblock_autocmds(); |
7 | 5730 #endif |
5731 } |