Mercurial > vim
annotate src/buffer.c @ 3606:02c86cdcf9a2 v7.3.563
updated for version 7.3.563
Problem: Can't build with tiny features.
Solution: Add #ifdef.
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Wed, 20 Jun 2012 17:54:38 +0200 |
parents | a220dd97b5c7 |
children | a34fb0a077c9 |
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; | |
3606 | 1366 #ifdef FEAT_WINDOWS |
3594 | 1367 win_T *prevwin; |
3606 | 1368 #endif |
7 | 1369 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL |
1370 || action == DOBUF_WIPE); | |
1371 | |
1372 setpcmark(); | |
22 | 1373 if (!cmdmod.keepalt) |
1374 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */ | |
1743 | 1375 buflist_altfpos(curwin); /* remember curpos */ |
7 | 1376 |
1377 #ifdef FEAT_VISUAL | |
1378 /* Don't restart Select mode after switching to another buffer. */ | |
1379 VIsual_reselect = FALSE; | |
1380 #endif | |
1381 | |
1382 /* close_windows() or apply_autocmds() may change curbuf */ | |
1383 prevbuf = curbuf; | |
1384 | |
1385 #ifdef FEAT_AUTOCMD | |
1386 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf); | |
1387 # ifdef FEAT_EVAL | |
1388 if (buf_valid(prevbuf) && !aborting()) | |
1389 # else | |
1390 if (buf_valid(prevbuf)) | |
1391 # endif | |
1392 #endif | |
1393 { | |
3068 | 1394 #ifdef FEAT_SYN_HL |
1395 if (prevbuf == curwin->w_buffer) | |
1396 reset_synblock(curwin); | |
1397 #endif | |
7 | 1398 #ifdef FEAT_WINDOWS |
1399 if (unload) | |
671 | 1400 close_windows(prevbuf, FALSE); |
7 | 1401 #endif |
1402 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) | |
1403 if (buf_valid(prevbuf) && !aborting()) | |
1404 #else | |
1405 if (buf_valid(prevbuf)) | |
1406 #endif | |
815 | 1407 { |
3606 | 1408 #ifdef FEAT_WINDOWS |
3594 | 1409 prevwin = curwin; |
3606 | 1410 #endif |
815 | 1411 if (prevbuf == curbuf) |
825 | 1412 u_sync(FALSE); |
7 | 1413 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf, |
1414 unload ? action : (action == DOBUF_GOTO | |
1415 && !P_HID(prevbuf) | |
3365 | 1416 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE); |
3606 | 1417 #ifdef FEAT_WINDOWS |
3594 | 1418 if (curwin != prevwin && win_valid(prevwin)) |
1419 /* autocommands changed curwin, Grr! */ | |
1420 curwin = prevwin; | |
3606 | 1421 #endif |
815 | 1422 } |
7 | 1423 } |
1424 #ifdef FEAT_AUTOCMD | |
1710 | 1425 /* An autocommand may have deleted "buf", already entered it (e.g., when |
3594 | 1426 * it did ":bunload") or aborted the script processing! |
1427 * If curwin->w_buffer is null, enter_buffer() will make it valid again */ | |
1428 if ((buf_valid(buf) && buf != curbuf | |
3606 | 1429 # ifdef FEAT_EVAL |
3594 | 1430 && !aborting() |
3606 | 1431 # endif |
1432 # ifdef FEAT_WINDOWS | |
3594 | 1433 ) || curwin->w_buffer == NULL |
3606 | 1434 # endif |
3594 | 1435 ) |
7 | 1436 #endif |
1437 enter_buffer(buf); | |
1438 } | |
1439 | |
1440 /* | |
1441 * Enter a new current buffer. | |
1442 * Old curbuf must have been abandoned already! | |
1443 */ | |
1444 void | |
1445 enter_buffer(buf) | |
1446 buf_T *buf; | |
1447 { | |
1448 /* Copy buffer and window local option values. Not for a help buffer. */ | |
1449 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP); | |
1450 if (!buf->b_help) | |
1451 get_winopts(buf); | |
1452 #ifdef FEAT_FOLDING | |
1453 else | |
1454 /* Remove all folds in the window. */ | |
1455 clearFolding(curwin); | |
1456 foldUpdateAll(curwin); /* update folds (later). */ | |
1457 #endif | |
1458 | |
1459 /* Get the buffer in the current window. */ | |
1460 curwin->w_buffer = buf; | |
1461 curbuf = buf; | |
1462 ++curbuf->b_nwindows; | |
1463 | |
1464 #ifdef FEAT_DIFF | |
672 | 1465 if (curwin->w_p_diff) |
1466 diff_buf_add(curbuf); | |
7 | 1467 #endif |
1468 | |
3068 | 1469 #ifdef FEAT_SYN_HL |
1470 curwin->w_s = &(buf->b_s); | |
1471 #endif | |
1472 | |
7 | 1473 /* Cursor on first line by default. */ |
1474 curwin->w_cursor.lnum = 1; | |
1475 curwin->w_cursor.col = 0; | |
1476 #ifdef FEAT_VIRTUALEDIT | |
1477 curwin->w_cursor.coladd = 0; | |
1478 #endif | |
1479 curwin->w_set_curswant = TRUE; | |
1744 | 1480 #ifdef FEAT_AUTOCMD |
1481 curwin->w_topline_was_set = FALSE; | |
1482 #endif | |
7 | 1483 |
2091
95b659982b7c
updated for version 7.2.375
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
1484 /* mark cursor position as being invalid */ |
95b659982b7c
updated for version 7.2.375
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
1485 curwin->w_valid = 0; |
95b659982b7c
updated for version 7.2.375
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
1486 |
7 | 1487 /* Make sure the buffer is loaded. */ |
1488 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */ | |
22 | 1489 { |
24 | 1490 #ifdef FEAT_AUTOCMD |
22 | 1491 /* If there is no filetype, allow for detecting one. Esp. useful for |
1492 * ":ball" used in a autocommand. If there already is a filetype we | |
1493 * might prefer to keep it. */ | |
1494 if (*curbuf->b_p_ft == NUL) | |
1495 did_filetype = FALSE; | |
24 | 1496 #endif |
22 | 1497 |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
1498 open_buffer(FALSE, NULL, 0); |
22 | 1499 } |
7 | 1500 else |
1501 { | |
968 | 1502 if (!msg_silent) |
1503 need_fileinfo = TRUE; /* display file info after redraw */ | |
7 | 1504 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */ |
1505 #ifdef FEAT_AUTOCMD | |
1506 curwin->w_topline = 1; | |
1507 # ifdef FEAT_DIFF | |
1508 curwin->w_topfill = 0; | |
1509 # endif | |
1510 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf); | |
1511 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf); | |
1512 #endif | |
1513 } | |
1514 | |
1515 /* If autocommands did not change the cursor position, restore cursor lnum | |
1516 * and possibly cursor col. */ | |
1517 if (curwin->w_cursor.lnum == 1 && inindent(0)) | |
1518 buflist_getfpos(); | |
1519 | |
1520 check_arg_idx(curwin); /* check for valid arg_idx */ | |
1521 #ifdef FEAT_TITLE | |
1522 maketitle(); | |
1523 #endif | |
1524 #ifdef FEAT_AUTOCMD | |
1744 | 1525 /* when autocmds didn't change it */ |
1526 if (curwin->w_topline == 1 && !curwin->w_topline_was_set) | |
7 | 1527 #endif |
1528 scroll_cursor_halfway(FALSE); /* redisplay at correct position */ | |
1529 | |
33 | 1530 #ifdef FEAT_NETBEANS_INTG |
1531 /* Send fileOpened event because we've changed buffers. */ | |
2210 | 1532 netbeans_file_activated(curbuf); |
33 | 1533 #endif |
1534 | |
961 | 1535 /* Change directories when the 'acd' option is set. */ |
1536 DO_AUTOCHDIR | |
7 | 1537 |
1538 #ifdef FEAT_KEYMAP | |
1539 if (curbuf->b_kmap_state & KEYMAP_INIT) | |
1869 | 1540 (void)keymap_init(); |
7 | 1541 #endif |
1185 | 1542 #ifdef FEAT_SPELL |
1543 /* May need to set the spell language. Can only do this after the buffer | |
1544 * has been properly setup. */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2214
diff
changeset
|
1545 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
|
1546 (void)did_set_spelllang(curwin); |
1185 | 1547 #endif |
1548 | |
7 | 1549 redraw_later(NOT_VALID); |
1550 } | |
1551 | |
961 | 1552 #if defined(FEAT_AUTOCHDIR) || defined(PROTO) |
1553 /* | |
1554 * Change to the directory of the current buffer. | |
1555 */ | |
1556 void | |
1557 do_autochdir() | |
1558 { | |
1559 if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK) | |
1560 shorten_fnames(TRUE); | |
1561 } | |
1562 #endif | |
1563 | |
7 | 1564 /* |
1565 * functions for dealing with the buffer list | |
1566 */ | |
1567 | |
1568 /* | |
1569 * Add a file name to the buffer list. Return a pointer to the buffer. | |
1570 * If the same file name already exists return a pointer to that buffer. | |
1571 * If it does not exist, or if fname == NULL, a new entry is created. | |
1572 * If (flags & BLN_CURBUF) is TRUE, may use current buffer. | |
1573 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list. | |
1574 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer. | |
1575 * This is the ONLY way to create a new buffer. | |
1576 */ | |
1577 static int top_file_num = 1; /* highest file number */ | |
1578 | |
1579 buf_T * | |
1580 buflist_new(ffname, sfname, lnum, flags) | |
1581 char_u *ffname; /* full path of fname or relative */ | |
1582 char_u *sfname; /* short fname or NULL */ | |
1583 linenr_T lnum; /* preferred cursor line */ | |
1584 int flags; /* BLN_ defines */ | |
1585 { | |
1586 buf_T *buf; | |
1587 #ifdef UNIX | |
1588 struct stat st; | |
1589 #endif | |
1590 | |
1591 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */ | |
1592 | |
1593 /* | |
1594 * If file name already exists in the list, update the entry. | |
1595 */ | |
1596 #ifdef UNIX | |
1597 /* On Unix we can use inode numbers when the file exists. Works better | |
1598 * for hard links. */ | |
1599 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0) | |
1600 st.st_dev = (dev_T)-1; | |
1601 #endif | |
1602 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf = | |
1603 #ifdef UNIX | |
1604 buflist_findname_stat(ffname, &st) | |
1605 #else | |
1606 buflist_findname(ffname) | |
1607 #endif | |
1608 ) != NULL) | |
1609 { | |
1610 vim_free(ffname); | |
1611 if (lnum != 0) | |
1612 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE); | |
1613 /* copy the options now, if 'cpo' doesn't have 's' and not done | |
1614 * already */ | |
1615 buf_copy_options(buf, 0); | |
1616 if ((flags & BLN_LISTED) && !buf->b_p_bl) | |
1617 { | |
1618 buf->b_p_bl = TRUE; | |
1619 #ifdef FEAT_AUTOCMD | |
1620 if (!(flags & BLN_DUMMY)) | |
1621 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf); | |
1622 #endif | |
1623 } | |
1624 return buf; | |
1625 } | |
1626 | |
1627 /* | |
1628 * If the current buffer has no name and no contents, use the current | |
1629 * buffer. Otherwise: Need to allocate a new buffer structure. | |
1630 * | |
1631 * This is the ONLY place where a new buffer structure is allocated! | |
625 | 1632 * (A spell file buffer is allocated in spell.c, but that's not a normal |
1633 * buffer.) | |
7 | 1634 */ |
1635 buf = NULL; | |
1636 if ((flags & BLN_CURBUF) | |
1637 && curbuf != NULL | |
1638 && curbuf->b_ffname == NULL | |
1639 && curbuf->b_nwindows <= 1 | |
1640 && (curbuf->b_ml.ml_mfp == NULL || bufempty())) | |
1641 { | |
1642 buf = curbuf; | |
1643 #ifdef FEAT_AUTOCMD | |
1644 /* It's like this buffer is deleted. Watch out for autocommands that | |
1645 * change curbuf! If that happens, allocate a new buffer anyway. */ | |
1646 if (curbuf->b_p_bl) | |
1647 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf); | |
1648 if (buf == curbuf) | |
1649 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf); | |
1650 # ifdef FEAT_EVAL | |
36 | 1651 if (aborting()) /* autocmds may abort script processing */ |
7 | 1652 return NULL; |
1653 # endif | |
1654 #endif | |
1655 #ifdef FEAT_QUICKFIX | |
1656 # ifdef FEAT_AUTOCMD | |
1657 if (buf == curbuf) | |
1658 # endif | |
1659 { | |
1660 /* Make sure 'bufhidden' and 'buftype' are empty */ | |
1661 clear_string_option(&buf->b_p_bh); | |
1662 clear_string_option(&buf->b_p_bt); | |
1663 } | |
1664 #endif | |
1665 } | |
1666 if (buf != curbuf || curbuf == NULL) | |
1667 { | |
1668 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T)); | |
1669 if (buf == NULL) | |
1670 { | |
1671 vim_free(ffname); | |
1672 return NULL; | |
1673 } | |
1674 } | |
1675 | |
1676 if (ffname != NULL) | |
1677 { | |
1678 buf->b_ffname = ffname; | |
1679 buf->b_sfname = vim_strsave(sfname); | |
1680 } | |
1681 | |
1682 clear_wininfo(buf); | |
1683 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T)); | |
1684 | |
1685 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL)) | |
1686 || buf->b_wininfo == NULL) | |
1687 { | |
1688 vim_free(buf->b_ffname); | |
1689 buf->b_ffname = NULL; | |
1690 vim_free(buf->b_sfname); | |
1691 buf->b_sfname = NULL; | |
1692 if (buf != curbuf) | |
1693 free_buffer(buf); | |
1694 return NULL; | |
1695 } | |
1696 | |
1697 if (buf == curbuf) | |
1698 { | |
1699 /* 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
|
1700 buf_freeall(buf, 0); |
7 | 1701 if (buf != curbuf) /* autocommands deleted the buffer! */ |
1702 return NULL; | |
1703 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) | |
36 | 1704 if (aborting()) /* autocmds may abort script processing */ |
7 | 1705 return NULL; |
1706 #endif | |
1707 /* buf->b_nwindows = 0; why was this here? */ | |
1708 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */ | |
1709 #ifdef FEAT_KEYMAP | |
1710 /* need to reload lmaps and set b:keymap_name */ | |
1711 curbuf->b_kmap_state |= KEYMAP_INIT; | |
1712 #endif | |
1713 } | |
1714 else | |
1715 { | |
1716 /* | |
1717 * put new buffer at the end of the buffer list | |
1718 */ | |
1719 buf->b_next = NULL; | |
1720 if (firstbuf == NULL) /* buffer list is empty */ | |
1721 { | |
1722 buf->b_prev = NULL; | |
1723 firstbuf = buf; | |
1724 } | |
1725 else /* append new buffer at end of list */ | |
1726 { | |
1727 lastbuf->b_next = buf; | |
1728 buf->b_prev = lastbuf; | |
1729 } | |
1730 lastbuf = buf; | |
1731 | |
1732 buf->b_fnum = top_file_num++; | |
1733 if (top_file_num < 0) /* wrap around (may cause duplicates) */ | |
1734 { | |
1735 EMSG(_("W14: Warning: List of file names overflow")); | |
1736 if (emsg_silent == 0) | |
1737 { | |
1738 out_flush(); | |
1739 ui_delay(3000L, TRUE); /* make sure it is noticed */ | |
1740 } | |
1741 top_file_num = 1; | |
1742 } | |
1743 | |
1744 /* | |
1745 * Always copy the options from the current buffer. | |
1746 */ | |
1747 buf_copy_options(buf, BCO_ALWAYS); | |
1748 } | |
1749 | |
1750 buf->b_wininfo->wi_fpos.lnum = lnum; | |
1751 buf->b_wininfo->wi_win = curwin; | |
1752 | |
1753 #ifdef FEAT_EVAL | |
126 | 1754 init_var_dict(&buf->b_vars, &buf->b_bufvar); /* init b: variables */ |
1755 #endif | |
1756 #ifdef FEAT_SYN_HL | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2214
diff
changeset
|
1757 hash_init(&buf->b_s.b_keywtab); |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2214
diff
changeset
|
1758 hash_init(&buf->b_s.b_keywtab_ic); |
7 | 1759 #endif |
1760 | |
1761 buf->b_fname = buf->b_sfname; | |
1762 #ifdef UNIX | |
1763 if (st.st_dev == (dev_T)-1) | |
1873 | 1764 buf->b_dev_valid = FALSE; |
7 | 1765 else |
1766 { | |
1873 | 1767 buf->b_dev_valid = TRUE; |
7 | 1768 buf->b_dev = st.st_dev; |
1769 buf->b_ino = st.st_ino; | |
1770 } | |
1771 #endif | |
1772 buf->b_u_synced = TRUE; | |
1773 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED; | |
42 | 1774 if (flags & BLN_DUMMY) |
1775 buf->b_flags |= BF_DUMMY; | |
7 | 1776 buf_clear_file(buf); |
1777 clrallmarks(buf); /* clear marks */ | |
1778 fmarks_check_names(buf); /* check file marks for this file */ | |
1779 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */ | |
1780 #ifdef FEAT_AUTOCMD | |
1781 if (!(flags & BLN_DUMMY)) | |
1782 { | |
1783 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf); | |
1784 if (flags & BLN_LISTED) | |
1785 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf); | |
1786 # ifdef FEAT_EVAL | |
36 | 1787 if (aborting()) /* autocmds may abort script processing */ |
7 | 1788 return NULL; |
1789 # endif | |
1790 } | |
1791 #endif | |
1792 | |
1793 return buf; | |
1794 } | |
1795 | |
1796 /* | |
1797 * Free the memory for the options of a buffer. | |
1798 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and | |
1799 * 'fileencoding'. | |
1800 */ | |
1801 void | |
1802 free_buf_options(buf, free_p_ff) | |
1803 buf_T *buf; | |
1804 int free_p_ff; | |
1805 { | |
1806 if (free_p_ff) | |
1807 { | |
1808 #ifdef FEAT_MBYTE | |
1809 clear_string_option(&buf->b_p_fenc); | |
1810 #endif | |
1811 clear_string_option(&buf->b_p_ff); | |
1812 #ifdef FEAT_QUICKFIX | |
1813 clear_string_option(&buf->b_p_bh); | |
1814 clear_string_option(&buf->b_p_bt); | |
1815 #endif | |
1816 } | |
1817 #ifdef FEAT_FIND_ID | |
1818 clear_string_option(&buf->b_p_def); | |
1819 clear_string_option(&buf->b_p_inc); | |
1820 # ifdef FEAT_EVAL | |
1821 clear_string_option(&buf->b_p_inex); | |
1822 # endif | |
1823 #endif | |
1824 #if defined(FEAT_CINDENT) && defined(FEAT_EVAL) | |
1825 clear_string_option(&buf->b_p_inde); | |
1826 clear_string_option(&buf->b_p_indk); | |
1827 #endif | |
788 | 1828 #if defined(FEAT_BEVAL) && defined(FEAT_EVAL) |
1829 clear_string_option(&buf->b_p_bexpr); | |
1830 #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
|
1831 #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
|
1832 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
|
1833 #endif |
667 | 1834 #if defined(FEAT_EVAL) |
1835 clear_string_option(&buf->b_p_fex); | |
1836 #endif | |
7 | 1837 #ifdef FEAT_CRYPT |
1838 clear_string_option(&buf->b_p_key); | |
1839 #endif | |
1840 clear_string_option(&buf->b_p_kp); | |
1841 clear_string_option(&buf->b_p_mps); | |
1842 clear_string_option(&buf->b_p_fo); | |
41 | 1843 clear_string_option(&buf->b_p_flp); |
7 | 1844 clear_string_option(&buf->b_p_isk); |
1845 #ifdef FEAT_KEYMAP | |
1846 clear_string_option(&buf->b_p_keymap); | |
1847 ga_clear(&buf->b_kmap_ga); | |
1848 #endif | |
1849 #ifdef FEAT_COMMENTS | |
1850 clear_string_option(&buf->b_p_com); | |
1851 #endif | |
1852 #ifdef FEAT_FOLDING | |
1853 clear_string_option(&buf->b_p_cms); | |
1854 #endif | |
1855 clear_string_option(&buf->b_p_nf); | |
1856 #ifdef FEAT_SYN_HL | |
1857 clear_string_option(&buf->b_p_syn); | |
737 | 1858 #endif |
1859 #ifdef FEAT_SPELL | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2214
diff
changeset
|
1860 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
|
1861 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
|
1862 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
|
1863 buf->b_s.b_cap_prog = NULL; |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2214
diff
changeset
|
1864 clear_string_option(&buf->b_s.b_p_spl); |
7 | 1865 #endif |
1866 #ifdef FEAT_SEARCHPATH | |
1867 clear_string_option(&buf->b_p_sua); | |
1868 #endif | |
1869 #ifdef FEAT_AUTOCMD | |
1870 clear_string_option(&buf->b_p_ft); | |
1871 #endif | |
1872 #ifdef FEAT_CINDENT | |
1873 clear_string_option(&buf->b_p_cink); | |
1874 clear_string_option(&buf->b_p_cino); | |
1875 #endif | |
1876 #if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT) | |
1877 clear_string_option(&buf->b_p_cinw); | |
1878 #endif | |
1879 #ifdef FEAT_INS_EXPAND | |
1880 clear_string_option(&buf->b_p_cpt); | |
1881 #endif | |
12 | 1882 #ifdef FEAT_COMPL_FUNC |
1883 clear_string_option(&buf->b_p_cfu); | |
502 | 1884 clear_string_option(&buf->b_p_ofu); |
12 | 1885 #endif |
7 | 1886 #ifdef FEAT_QUICKFIX |
1887 clear_string_option(&buf->b_p_gp); | |
1888 clear_string_option(&buf->b_p_mp); | |
1889 clear_string_option(&buf->b_p_efm); | |
1890 #endif | |
1891 clear_string_option(&buf->b_p_ep); | |
1892 clear_string_option(&buf->b_p_path); | |
1893 clear_string_option(&buf->b_p_tags); | |
1894 #ifdef FEAT_INS_EXPAND | |
1895 clear_string_option(&buf->b_p_dict); | |
1896 clear_string_option(&buf->b_p_tsr); | |
1897 #endif | |
12 | 1898 #ifdef FEAT_TEXTOBJ |
1899 clear_string_option(&buf->b_p_qe); | |
1900 #endif | |
7 | 1901 buf->b_p_ar = -1; |
1902 } | |
1903 | |
1904 /* | |
1905 * get alternate file n | |
1906 * set linenr to lnum or altfpos.lnum if lnum == 0 | |
1907 * also set cursor column to altfpos.col if 'startofline' is not set. | |
1908 * if (options & GETF_SETMARK) call setpcmark() | |
1909 * if (options & GETF_ALT) we are jumping to an alternate file. | |
1910 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping | |
1911 * | |
1912 * return FAIL for failure, OK for success | |
1913 */ | |
1914 int | |
1915 buflist_getfile(n, lnum, options, forceit) | |
1916 int n; | |
1917 linenr_T lnum; | |
1918 int options; | |
1919 int forceit; | |
1920 { | |
1921 buf_T *buf; | |
1922 #ifdef FEAT_WINDOWS | |
1923 win_T *wp = NULL; | |
1924 #endif | |
1925 pos_T *fpos; | |
1926 colnr_T col; | |
1927 | |
1928 buf = buflist_findnr(n); | |
1929 if (buf == NULL) | |
1930 { | |
1931 if ((options & GETF_ALT) && n == 0) | |
1932 EMSG(_(e_noalt)); | |
1933 else | |
1934 EMSGN(_("E92: Buffer %ld not found"), n); | |
1935 return FAIL; | |
1936 } | |
1937 | |
1938 /* if alternate file is the current buffer, nothing to do */ | |
1939 if (buf == curbuf) | |
1940 return OK; | |
1941 | |
633 | 1942 if (text_locked()) |
631 | 1943 { |
633 | 1944 text_locked_msg(); |
7 | 1945 return FAIL; |
631 | 1946 } |
819 | 1947 #ifdef FEAT_AUTOCMD |
1948 if (curbuf_locked()) | |
1949 return FAIL; | |
1950 #endif | |
7 | 1951 |
1952 /* altfpos may be changed by getfile(), get it now */ | |
1953 if (lnum == 0) | |
1954 { | |
1955 fpos = buflist_findfpos(buf); | |
1956 lnum = fpos->lnum; | |
1957 col = fpos->col; | |
1958 } | |
1959 else | |
1960 col = 0; | |
1961 | |
1962 #ifdef FEAT_WINDOWS | |
1963 if (options & GETF_SWITCH) | |
1964 { | |
1618 | 1965 /* If 'switchbuf' contains "useopen": jump to first window containing |
1966 * "buf" if one exists */ | |
1967 if (swb_flags & SWB_USEOPEN) | |
7 | 1968 wp = buf_jump_open_win(buf); |
1618 | 1969 /* If 'switchbuf' contians "usetab": jump to first window in any tab |
1970 * page containing "buf" if one exists */ | |
1971 if (wp == NULL && (swb_flags & SWB_USETAB)) | |
825 | 1972 wp = buf_jump_open_tab(buf); |
1618 | 1973 /* If 'switchbuf' contains "split" or "newtab" and the current buffer |
1974 * isn't empty: open new window */ | |
1975 if (wp == NULL && (swb_flags & (SWB_SPLIT | SWB_NEWTAB)) && !bufempty()) | |
7 | 1976 { |
1618 | 1977 if (swb_flags & SWB_NEWTAB) /* Open in a new tab */ |
1978 tabpage_new(); | |
1979 else if (win_split(0, 0) == FAIL) /* Open in a new window */ | |
7 | 1980 return FAIL; |
2583 | 1981 RESET_BINDING(curwin); |
7 | 1982 } |
1983 } | |
1984 #endif | |
1985 | |
1986 ++RedrawingDisabled; | |
1987 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK), | |
1988 lnum, forceit) <= 0) | |
1989 { | |
1990 --RedrawingDisabled; | |
1991 | |
1992 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */ | |
1993 if (!p_sol && col != 0) | |
1994 { | |
1995 curwin->w_cursor.col = col; | |
1996 check_cursor_col(); | |
1997 #ifdef FEAT_VIRTUALEDIT | |
1998 curwin->w_cursor.coladd = 0; | |
1999 #endif | |
2000 curwin->w_set_curswant = TRUE; | |
2001 } | |
2002 return OK; | |
2003 } | |
2004 --RedrawingDisabled; | |
2005 return FAIL; | |
2006 } | |
2007 | |
2008 /* | |
2009 * go to the last know line number for the current buffer | |
2010 */ | |
2011 void | |
2012 buflist_getfpos() | |
2013 { | |
2014 pos_T *fpos; | |
2015 | |
2016 fpos = buflist_findfpos(curbuf); | |
2017 | |
2018 curwin->w_cursor.lnum = fpos->lnum; | |
2019 check_cursor_lnum(); | |
2020 | |
2021 if (p_sol) | |
2022 curwin->w_cursor.col = 0; | |
2023 else | |
2024 { | |
2025 curwin->w_cursor.col = fpos->col; | |
2026 check_cursor_col(); | |
2027 #ifdef FEAT_VIRTUALEDIT | |
2028 curwin->w_cursor.coladd = 0; | |
2029 #endif | |
2030 curwin->w_set_curswant = TRUE; | |
2031 } | |
2032 } | |
2033 | |
42 | 2034 #if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO) |
2035 /* | |
2036 * Find file in buffer list by name (it has to be for the current window). | |
2037 * Returns NULL if not found. | |
2038 */ | |
2039 buf_T * | |
2040 buflist_findname_exp(fname) | |
2041 char_u *fname; | |
2042 { | |
2043 char_u *ffname; | |
2044 buf_T *buf = NULL; | |
2045 | |
2046 /* First make the name into a full path name */ | |
2047 ffname = FullName_save(fname, | |
2048 #ifdef UNIX | |
2049 TRUE /* force expansion, get rid of symbolic links */ | |
2050 #else | |
2051 FALSE | |
2052 #endif | |
2053 ); | |
2054 if (ffname != NULL) | |
2055 { | |
2056 buf = buflist_findname(ffname); | |
2057 vim_free(ffname); | |
2058 } | |
2059 return buf; | |
2060 } | |
2061 #endif | |
2062 | |
7 | 2063 /* |
2064 * Find file in buffer list by name (it has to be for the current window). | |
2065 * "ffname" must have a full path. | |
42 | 2066 * Skips dummy buffers. |
2067 * Returns NULL if not found. | |
7 | 2068 */ |
2069 buf_T * | |
2070 buflist_findname(ffname) | |
2071 char_u *ffname; | |
2072 { | |
2073 #ifdef UNIX | |
2074 struct stat st; | |
2075 | |
2076 if (mch_stat((char *)ffname, &st) < 0) | |
2077 st.st_dev = (dev_T)-1; | |
2078 return buflist_findname_stat(ffname, &st); | |
2079 } | |
2080 | |
2081 /* | |
2082 * Same as buflist_findname(), but pass the stat structure to avoid getting it | |
2083 * twice for the same file. | |
42 | 2084 * Returns NULL if not found. |
7 | 2085 */ |
2086 static buf_T * | |
2087 buflist_findname_stat(ffname, stp) | |
2088 char_u *ffname; | |
2089 struct stat *stp; | |
2090 { | |
2091 #endif | |
2092 buf_T *buf; | |
2093 | |
2094 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
42 | 2095 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname |
7 | 2096 #ifdef UNIX |
2097 , stp | |
2098 #endif | |
2099 )) | |
2100 return buf; | |
2101 return NULL; | |
2102 } | |
2103 | |
2104 #if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO) | |
2105 /* | |
2106 * Find file in buffer list by a regexp pattern. | |
2107 * Return fnum of the found buffer. | |
2108 * Return < 0 for error. | |
2109 */ | |
2110 int | |
2111 buflist_findpat(pattern, pattern_end, unlisted, diffmode) | |
2112 char_u *pattern; | |
2113 char_u *pattern_end; /* pointer to first char after pattern */ | |
2114 int unlisted; /* find unlisted buffers */ | |
1883 | 2115 int diffmode UNUSED; /* find diff-mode buffers only */ |
7 | 2116 { |
2117 buf_T *buf; | |
2118 regprog_T *prog; | |
2119 int match = -1; | |
2120 int find_listed; | |
2121 char_u *pat; | |
2122 char_u *patend; | |
2123 int attempt; | |
2124 char_u *p; | |
2125 int toggledollar; | |
2126 | |
2127 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#')) | |
2128 { | |
2129 if (*pattern == '%') | |
2130 match = curbuf->b_fnum; | |
2131 else | |
2132 match = curwin->w_alt_fnum; | |
2133 #ifdef FEAT_DIFF | |
2134 if (diffmode && !diff_mode_buf(buflist_findnr(match))) | |
2135 match = -1; | |
2136 #endif | |
2137 } | |
2138 | |
2139 /* | |
2140 * Try four ways of matching a listed buffer: | |
2141 * attempt == 0: without '^' or '$' (at any position) | |
1187 | 2142 * attempt == 1: with '^' at start (only at position 0) |
7 | 2143 * attempt == 2: with '$' at end (only match at end) |
2144 * attempt == 3: with '^' at start and '$' at end (only full match) | |
2145 * Repeat this for finding an unlisted buffer if there was no matching | |
2146 * listed buffer. | |
2147 */ | |
2148 else | |
2149 { | |
2150 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE); | |
2151 if (pat == NULL) | |
2152 return -1; | |
2153 patend = pat + STRLEN(pat) - 1; | |
2154 toggledollar = (patend > pat && *patend == '$'); | |
2155 | |
2156 /* First try finding a listed buffer. If not found and "unlisted" | |
2157 * is TRUE, try finding an unlisted buffer. */ | |
2158 find_listed = TRUE; | |
2159 for (;;) | |
2160 { | |
2161 for (attempt = 0; attempt <= 3; ++attempt) | |
2162 { | |
2163 /* may add '^' and '$' */ | |
2164 if (toggledollar) | |
2165 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */ | |
2166 p = pat; | |
2167 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */ | |
2168 ++p; | |
2169 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0); | |
2170 if (prog == NULL) | |
2171 { | |
2172 vim_free(pat); | |
2173 return -1; | |
2174 } | |
2175 | |
2176 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
2177 if (buf->b_p_bl == find_listed | |
2178 #ifdef FEAT_DIFF | |
2179 && (!diffmode || diff_mode_buf(buf)) | |
2180 #endif | |
2181 && buflist_match(prog, buf) != NULL) | |
2182 { | |
2183 if (match >= 0) /* already found a match */ | |
2184 { | |
2185 match = -2; | |
2186 break; | |
2187 } | |
2188 match = buf->b_fnum; /* remember first match */ | |
2189 } | |
2190 | |
2191 vim_free(prog); | |
2192 if (match >= 0) /* found one match */ | |
2193 break; | |
2194 } | |
2195 | |
2196 /* Only search for unlisted buffers if there was no match with | |
2197 * a listed buffer. */ | |
2198 if (!unlisted || !find_listed || match != -1) | |
2199 break; | |
2200 find_listed = FALSE; | |
2201 } | |
2202 | |
2203 vim_free(pat); | |
2204 } | |
2205 | |
2206 if (match == -2) | |
2207 EMSG2(_("E93: More than one match for %s"), pattern); | |
2208 else if (match < 0) | |
2209 EMSG2(_("E94: No matching buffer for %s"), pattern); | |
2210 return match; | |
2211 } | |
2212 #endif | |
2213 | |
2214 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) | |
2215 | |
2216 /* | |
2217 * Find all buffer names that match. | |
2218 * For command line expansion of ":buf" and ":sbuf". | |
2219 * Return OK if matches found, FAIL otherwise. | |
2220 */ | |
2221 int | |
2222 ExpandBufnames(pat, num_file, file, options) | |
2223 char_u *pat; | |
2224 int *num_file; | |
2225 char_u ***file; | |
2226 int options; | |
2227 { | |
2228 int count = 0; | |
2229 buf_T *buf; | |
2230 int round; | |
2231 char_u *p; | |
2232 int attempt; | |
2233 regprog_T *prog; | |
170 | 2234 char_u *patc; |
7 | 2235 |
2236 *num_file = 0; /* return values in case of FAIL */ | |
2237 *file = NULL; | |
2238 | |
170 | 2239 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */ |
2240 if (*pat == '^') | |
2241 { | |
2242 patc = alloc((unsigned)STRLEN(pat) + 11); | |
2243 if (patc == NULL) | |
2244 return FAIL; | |
2245 STRCPY(patc, "\\(^\\|[\\/]\\)"); | |
2246 STRCPY(patc + 11, pat + 1); | |
2247 } | |
2248 else | |
2249 patc = pat; | |
2250 | |
7 | 2251 /* |
170 | 2252 * attempt == 0: try match with '\<', match at start of word |
267 | 2253 * attempt == 1: try match without '\<', match anywhere |
7 | 2254 */ |
267 | 2255 for (attempt = 0; attempt <= 1; ++attempt) |
7 | 2256 { |
267 | 2257 if (attempt > 0 && patc == pat) |
170 | 2258 break; /* there was no anchor, no need to try again */ |
267 | 2259 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC); |
170 | 2260 if (prog == NULL) |
7 | 2261 { |
170 | 2262 if (patc != pat) |
2263 vim_free(patc); | |
2264 return FAIL; | |
7 | 2265 } |
2266 | |
2267 /* | |
2268 * round == 1: Count the matches. | |
2269 * round == 2: Build the array to keep the matches. | |
2270 */ | |
2271 for (round = 1; round <= 2; ++round) | |
2272 { | |
2273 count = 0; | |
2274 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
2275 { | |
2276 if (!buf->b_p_bl) /* skip unlisted buffers */ | |
2277 continue; | |
2278 p = buflist_match(prog, buf); | |
2279 if (p != NULL) | |
2280 { | |
2281 if (round == 1) | |
2282 ++count; | |
2283 else | |
2284 { | |
2285 if (options & WILD_HOME_REPLACE) | |
2286 p = home_replace_save(buf, p); | |
2287 else | |
2288 p = vim_strsave(p); | |
2289 (*file)[count++] = p; | |
2290 } | |
2291 } | |
2292 } | |
2293 if (count == 0) /* no match found, break here */ | |
2294 break; | |
2295 if (round == 1) | |
2296 { | |
2297 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *))); | |
2298 if (*file == NULL) | |
2299 { | |
2300 vim_free(prog); | |
170 | 2301 if (patc != pat) |
2302 vim_free(patc); | |
7 | 2303 return FAIL; |
2304 } | |
2305 } | |
2306 } | |
2307 vim_free(prog); | |
2308 if (count) /* match(es) found, break here */ | |
2309 break; | |
2310 } | |
2311 | |
170 | 2312 if (patc != pat) |
2313 vim_free(patc); | |
2314 | |
7 | 2315 *num_file = count; |
2316 return (count == 0 ? FAIL : OK); | |
2317 } | |
2318 | |
2319 #endif /* FEAT_CMDL_COMPL */ | |
2320 | |
2321 #ifdef HAVE_BUFLIST_MATCH | |
2322 /* | |
2323 * Check for a match on the file name for buffer "buf" with regprog "prog". | |
2324 */ | |
2325 static char_u * | |
2326 buflist_match(prog, buf) | |
2327 regprog_T *prog; | |
2328 buf_T *buf; | |
2329 { | |
2330 char_u *match; | |
2331 | |
2332 /* First try the short file name, then the long file name. */ | |
2333 match = fname_match(prog, buf->b_sfname); | |
2334 if (match == NULL) | |
2335 match = fname_match(prog, buf->b_ffname); | |
2336 | |
2337 return match; | |
2338 } | |
2339 | |
2340 /* | |
2341 * Try matching the regexp in "prog" with file name "name". | |
2342 * Return "name" when there is a match, NULL when not. | |
2343 */ | |
2344 static char_u * | |
2345 fname_match(prog, name) | |
2346 regprog_T *prog; | |
2347 char_u *name; | |
2348 { | |
2349 char_u *match = NULL; | |
2350 char_u *p; | |
2351 regmatch_T regmatch; | |
2352 | |
2353 if (name != NULL) | |
2354 { | |
2355 regmatch.regprog = prog; | |
2356 #ifdef CASE_INSENSITIVE_FILENAME | |
2357 regmatch.rm_ic = TRUE; /* Always ignore case */ | |
2358 #else | |
2359 regmatch.rm_ic = FALSE; /* Never ignore case */ | |
2360 #endif | |
2361 | |
2362 if (vim_regexec(®match, name, (colnr_T)0)) | |
2363 match = name; | |
2364 else | |
2365 { | |
2366 /* Replace $(HOME) with '~' and try matching again. */ | |
2367 p = home_replace_save(NULL, name); | |
2368 if (p != NULL && vim_regexec(®match, p, (colnr_T)0)) | |
2369 match = name; | |
2370 vim_free(p); | |
2371 } | |
2372 } | |
2373 | |
2374 return match; | |
2375 } | |
2376 #endif | |
2377 | |
2378 /* | |
2379 * find file in buffer list by number | |
2380 */ | |
2381 buf_T * | |
2382 buflist_findnr(nr) | |
2383 int nr; | |
2384 { | |
2385 buf_T *buf; | |
2386 | |
2387 if (nr == 0) | |
2388 nr = curwin->w_alt_fnum; | |
2389 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
2390 if (buf->b_fnum == nr) | |
2391 return (buf); | |
2392 return NULL; | |
2393 } | |
2394 | |
2395 /* | |
2396 * Get name of file 'n' in the buffer list. | |
2397 * When the file has no name an empty string is returned. | |
2398 * home_replace() is used to shorten the file name (used for marks). | |
2399 * Returns a pointer to allocated memory, of NULL when failed. | |
2400 */ | |
2401 char_u * | |
2402 buflist_nr2name(n, fullname, helptail) | |
2403 int n; | |
2404 int fullname; | |
2405 int helptail; /* for help buffers return tail only */ | |
2406 { | |
2407 buf_T *buf; | |
2408 | |
2409 buf = buflist_findnr(n); | |
2410 if (buf == NULL) | |
2411 return NULL; | |
2412 return home_replace_save(helptail ? buf : NULL, | |
2413 fullname ? buf->b_ffname : buf->b_fname); | |
2414 } | |
2415 | |
2416 /* | |
2417 * Set the "lnum" and "col" for the buffer "buf" and the current window. | |
2418 * When "copy_options" is TRUE save the local window option values. | |
2419 * When "lnum" is 0 only do the options. | |
2420 */ | |
2421 static void | |
2422 buflist_setfpos(buf, win, lnum, col, copy_options) | |
2423 buf_T *buf; | |
2424 win_T *win; | |
2425 linenr_T lnum; | |
2426 colnr_T col; | |
2427 int copy_options; | |
2428 { | |
2429 wininfo_T *wip; | |
2430 | |
2431 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next) | |
2432 if (wip->wi_win == win) | |
2433 break; | |
2434 if (wip == NULL) | |
2435 { | |
2436 /* allocate a new entry */ | |
2437 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T)); | |
2438 if (wip == NULL) | |
2439 return; | |
2440 wip->wi_win = win; | |
2441 if (lnum == 0) /* set lnum even when it's 0 */ | |
2442 lnum = 1; | |
2443 } | |
2444 else | |
2445 { | |
2446 /* remove the entry from the list */ | |
2447 if (wip->wi_prev) | |
2448 wip->wi_prev->wi_next = wip->wi_next; | |
2449 else | |
2450 buf->b_wininfo = wip->wi_next; | |
2451 if (wip->wi_next) | |
2452 wip->wi_next->wi_prev = wip->wi_prev; | |
2453 if (copy_options && wip->wi_optset) | |
2454 { | |
2455 clear_winopt(&wip->wi_opt); | |
2456 #ifdef FEAT_FOLDING | |
2457 deleteFoldRecurse(&wip->wi_folds); | |
2458 #endif | |
2459 } | |
2460 } | |
2461 if (lnum != 0) | |
2462 { | |
2463 wip->wi_fpos.lnum = lnum; | |
2464 wip->wi_fpos.col = col; | |
2465 } | |
2466 if (copy_options) | |
2467 { | |
2468 /* Save the window-specific option values. */ | |
2469 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt); | |
2470 #ifdef FEAT_FOLDING | |
2471 wip->wi_fold_manual = win->w_fold_manual; | |
2472 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds); | |
2473 #endif | |
2474 wip->wi_optset = TRUE; | |
2475 } | |
2476 | |
2477 /* insert the entry in front of the list */ | |
2478 wip->wi_next = buf->b_wininfo; | |
2479 buf->b_wininfo = wip; | |
2480 wip->wi_prev = NULL; | |
2481 if (wip->wi_next) | |
2482 wip->wi_next->wi_prev = wip; | |
2483 | |
2484 return; | |
2485 } | |
2486 | |
1743 | 2487 #ifdef FEAT_DIFF |
2488 static int wininfo_other_tab_diff __ARGS((wininfo_T *wip)); | |
2489 | |
2490 /* | |
2491 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab | |
2492 * page. That's because a diff is local to a tab page. | |
2493 */ | |
2494 static int | |
2495 wininfo_other_tab_diff(wip) | |
2496 wininfo_T *wip; | |
2497 { | |
2498 win_T *wp; | |
2499 | |
2500 if (wip->wi_opt.wo_diff) | |
2501 { | |
2502 for (wp = firstwin; wp != NULL; wp = wp->w_next) | |
2503 /* return FALSE when it's a window in the current tab page, thus | |
2504 * the buffer was in diff mode here */ | |
2505 if (wip->wi_win == wp) | |
2506 return FALSE; | |
2507 return TRUE; | |
2508 } | |
2509 return FALSE; | |
2510 } | |
2511 #endif | |
2512 | |
7 | 2513 /* |
2514 * Find info for the current window in buffer "buf". | |
2515 * If not found, return the info for the most recently used window. | |
1743 | 2516 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in |
2517 * another tab page. | |
7 | 2518 * Returns NULL when there isn't any info. |
2519 */ | |
2520 static wininfo_T * | |
1743 | 2521 find_wininfo(buf, skip_diff_buffer) |
7 | 2522 buf_T *buf; |
1876 | 2523 int skip_diff_buffer UNUSED; |
7 | 2524 { |
2525 wininfo_T *wip; | |
2526 | |
2527 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next) | |
1743 | 2528 if (wip->wi_win == curwin |
2529 #ifdef FEAT_DIFF | |
2530 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip)) | |
2531 #endif | |
2532 ) | |
7 | 2533 break; |
1743 | 2534 |
2535 /* If no wininfo for curwin, use the first in the list (that doesn't have | |
2536 * 'diff' set and is in another tab page). */ | |
2537 if (wip == NULL) | |
2538 { | |
2539 #ifdef FEAT_DIFF | |
2540 if (skip_diff_buffer) | |
2541 { | |
2542 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next) | |
2543 if (!wininfo_other_tab_diff(wip)) | |
2544 break; | |
2545 } | |
2546 else | |
2547 #endif | |
2548 wip = buf->b_wininfo; | |
2549 } | |
7 | 2550 return wip; |
2551 } | |
2552 | |
2553 /* | |
2554 * Reset the local window options to the values last used in this window. | |
2555 * If the buffer wasn't used in this window before, use the values from | |
2556 * the most recently used window. If the values were never set, use the | |
2557 * global values for the window. | |
2558 */ | |
2559 void | |
2560 get_winopts(buf) | |
2561 buf_T *buf; | |
2562 { | |
2563 wininfo_T *wip; | |
2564 | |
2565 clear_winopt(&curwin->w_onebuf_opt); | |
2566 #ifdef FEAT_FOLDING | |
2567 clearFolding(curwin); | |
2568 #endif | |
2569 | |
1743 | 2570 wip = find_wininfo(buf, TRUE); |
7 | 2571 if (wip != NULL && wip->wi_optset) |
2572 { | |
2573 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt); | |
2574 #ifdef FEAT_FOLDING | |
2575 curwin->w_fold_manual = wip->wi_fold_manual; | |
2576 curwin->w_foldinvalid = TRUE; | |
2577 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds); | |
2578 #endif | |
2579 } | |
2580 else | |
2581 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt); | |
2582 | |
2583 #ifdef FEAT_FOLDING | |
2584 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */ | |
2585 if (p_fdls >= 0) | |
2586 curwin->w_p_fdl = p_fdls; | |
2587 #endif | |
2799 | 2588 #ifdef FEAT_SYN_HL |
2589 check_colorcolumn(curwin); | |
2590 #endif | |
7 | 2591 } |
2592 | |
2593 /* | |
2594 * Find the position (lnum and col) for the buffer 'buf' for the current | |
2595 * window. | |
2596 * Returns a pointer to no_position if no position is found. | |
2597 */ | |
2598 pos_T * | |
2599 buflist_findfpos(buf) | |
2600 buf_T *buf; | |
2601 { | |
2602 wininfo_T *wip; | |
1869 | 2603 static pos_T no_position = INIT_POS_T(1, 0, 0); |
7 | 2604 |
1743 | 2605 wip = find_wininfo(buf, FALSE); |
7 | 2606 if (wip != NULL) |
2607 return &(wip->wi_fpos); | |
2608 else | |
2609 return &no_position; | |
2610 } | |
2611 | |
2612 /* | |
2613 * Find the lnum for the buffer 'buf' for the current window. | |
2614 */ | |
2615 linenr_T | |
2616 buflist_findlnum(buf) | |
2617 buf_T *buf; | |
2618 { | |
2619 return buflist_findfpos(buf)->lnum; | |
2620 } | |
2621 | |
2622 #if defined(FEAT_LISTCMDS) || defined(PROTO) | |
2623 /* | |
2624 * List all know file names (for :files and :buffers command). | |
2625 */ | |
2626 void | |
2627 buflist_list(eap) | |
2628 exarg_T *eap; | |
2629 { | |
2630 buf_T *buf; | |
2631 int len; | |
2632 int i; | |
2633 | |
2634 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next) | |
2635 { | |
2636 /* skip unlisted buffers, unless ! was used */ | |
2637 if (!buf->b_p_bl && !eap->forceit) | |
2638 continue; | |
2639 msg_putchar('\n'); | |
2640 if (buf_spname(buf) != NULL) | |
2641 STRCPY(NameBuff, buf_spname(buf)); | |
2642 else | |
2643 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE); | |
2644 | |
302 | 2645 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"", |
7 | 2646 buf->b_fnum, |
2647 buf->b_p_bl ? ' ' : 'u', | |
2648 buf == curbuf ? '%' : | |
2649 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '), | |
2650 buf->b_ml.ml_mfp == NULL ? ' ' : | |
2651 (buf->b_nwindows == 0 ? 'h' : 'a'), | |
2652 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '), | |
2653 (buf->b_flags & BF_READERR) ? 'x' | |
300 | 2654 : (bufIsChanged(buf) ? '+' : ' '), |
2655 NameBuff); | |
7 | 2656 |
2657 /* put "line 999" in column 40 or after the file name */ | |
2658 i = 40 - vim_strsize(IObuff); | |
2659 do | |
2660 { | |
2661 IObuff[len++] = ' '; | |
2662 } while (--i > 0 && len < IOSIZE - 18); | |
1869 | 2663 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len), |
2664 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum | |
272 | 2665 : (long)buflist_findlnum(buf)); |
7 | 2666 msg_outtrans(IObuff); |
2667 out_flush(); /* output one line at a time */ | |
2668 ui_breakcheck(); | |
2669 } | |
2670 } | |
2671 #endif | |
2672 | |
2673 /* | |
2674 * Get file name and line number for file 'fnum'. | |
2675 * Used by DoOneCmd() for translating '%' and '#'. | |
2676 * Used by insert_reg() and cmdline_paste() for '#' register. | |
2677 * Return FAIL if not found, OK for success. | |
2678 */ | |
2679 int | |
2680 buflist_name_nr(fnum, fname, lnum) | |
2681 int fnum; | |
2682 char_u **fname; | |
2683 linenr_T *lnum; | |
2684 { | |
2685 buf_T *buf; | |
2686 | |
2687 buf = buflist_findnr(fnum); | |
2688 if (buf == NULL || buf->b_fname == NULL) | |
2689 return FAIL; | |
2690 | |
2691 *fname = buf->b_fname; | |
2692 *lnum = buflist_findlnum(buf); | |
2693 | |
2694 return OK; | |
2695 } | |
2696 | |
2697 /* | |
2698 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'. | |
2699 * The file name with the full path is also remembered, for when :cd is used. | |
2700 * Returns FAIL for failure (file name already in use by other buffer) | |
2701 * OK otherwise. | |
2702 */ | |
2703 int | |
2704 setfname(buf, ffname, sfname, message) | |
2705 buf_T *buf; | |
2706 char_u *ffname, *sfname; | |
42 | 2707 int message; /* give message when buffer already exists */ |
7 | 2708 { |
42 | 2709 buf_T *obuf = NULL; |
7 | 2710 #ifdef UNIX |
2711 struct stat st; | |
2712 #endif | |
2713 | |
2714 if (ffname == NULL || *ffname == NUL) | |
2715 { | |
2716 /* Removing the name. */ | |
2717 vim_free(buf->b_ffname); | |
2718 vim_free(buf->b_sfname); | |
2719 buf->b_ffname = NULL; | |
2720 buf->b_sfname = NULL; | |
2721 #ifdef UNIX | |
2722 st.st_dev = (dev_T)-1; | |
2723 #endif | |
2724 } | |
2725 else | |
2726 { | |
2727 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */ | |
2728 if (ffname == NULL) /* out of memory */ | |
2729 return FAIL; | |
2730 | |
2731 /* | |
2732 * if the file name is already used in another buffer: | |
2733 * - if the buffer is loaded, fail | |
2734 * - if the buffer is not loaded, delete it from the list | |
2735 */ | |
2736 #ifdef UNIX | |
2737 if (mch_stat((char *)ffname, &st) < 0) | |
2738 st.st_dev = (dev_T)-1; | |
42 | 2739 #endif |
2740 if (!(buf->b_flags & BF_DUMMY)) | |
2741 #ifdef UNIX | |
2742 obuf = buflist_findname_stat(ffname, &st); | |
7 | 2743 #else |
42 | 2744 obuf = buflist_findname(ffname); |
7 | 2745 #endif |
2746 if (obuf != NULL && obuf != buf) | |
2747 { | |
2748 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */ | |
2749 { | |
2750 if (message) | |
2751 EMSG(_("E95: Buffer with this name already exists")); | |
2752 vim_free(ffname); | |
2753 return FAIL; | |
2754 } | |
3365 | 2755 /* delete from the list */ |
2756 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE); | |
7 | 2757 } |
2758 sfname = vim_strsave(sfname); | |
2759 if (ffname == NULL || sfname == NULL) | |
2760 { | |
2761 vim_free(sfname); | |
2762 vim_free(ffname); | |
2763 return FAIL; | |
2764 } | |
2765 #ifdef USE_FNAME_CASE | |
2766 # ifdef USE_LONG_FNAME | |
2767 if (USE_LONG_FNAME) | |
2768 # endif | |
2769 fname_case(sfname, 0); /* set correct case for short file name */ | |
2770 #endif | |
2771 vim_free(buf->b_ffname); | |
2772 vim_free(buf->b_sfname); | |
2773 buf->b_ffname = ffname; | |
2774 buf->b_sfname = sfname; | |
2775 } | |
2776 buf->b_fname = buf->b_sfname; | |
2777 #ifdef UNIX | |
2778 if (st.st_dev == (dev_T)-1) | |
1873 | 2779 buf->b_dev_valid = FALSE; |
7 | 2780 else |
2781 { | |
1873 | 2782 buf->b_dev_valid = TRUE; |
7 | 2783 buf->b_dev = st.st_dev; |
2784 buf->b_ino = st.st_ino; | |
2785 } | |
2786 #endif | |
2787 | |
2788 #ifndef SHORT_FNAME | |
2789 buf->b_shortname = FALSE; | |
2790 #endif | |
2791 | |
2792 buf_name_changed(buf); | |
2793 return OK; | |
2794 } | |
2795 | |
2796 /* | |
41 | 2797 * Crude way of changing the name of a buffer. Use with care! |
2798 * The name should be relative to the current directory. | |
2799 */ | |
2800 void | |
2801 buf_set_name(fnum, name) | |
2802 int fnum; | |
2803 char_u *name; | |
2804 { | |
2805 buf_T *buf; | |
2806 | |
2807 buf = buflist_findnr(fnum); | |
2808 if (buf != NULL) | |
2809 { | |
2810 vim_free(buf->b_sfname); | |
2811 vim_free(buf->b_ffname); | |
844 | 2812 buf->b_ffname = vim_strsave(name); |
2813 buf->b_sfname = NULL; | |
2814 /* Allocate ffname and expand into full path. Also resolves .lnk | |
2815 * files on Win32. */ | |
2816 fname_expand(buf, &buf->b_ffname, &buf->b_sfname); | |
41 | 2817 buf->b_fname = buf->b_sfname; |
2818 } | |
2819 } | |
2820 | |
2821 /* | |
7 | 2822 * Take care of what needs to be done when the name of buffer "buf" has |
2823 * changed. | |
2824 */ | |
2825 void | |
2826 buf_name_changed(buf) | |
2827 buf_T *buf; | |
2828 { | |
2829 /* | |
2830 * If the file name changed, also change the name of the swapfile | |
2831 */ | |
2832 if (buf->b_ml.ml_mfp != NULL) | |
2833 ml_setname(buf); | |
2834 | |
2835 if (curwin->w_buffer == buf) | |
2836 check_arg_idx(curwin); /* check file name for arg list */ | |
2837 #ifdef FEAT_TITLE | |
2838 maketitle(); /* set window title */ | |
2839 #endif | |
2840 #ifdef FEAT_WINDOWS | |
2841 status_redraw_all(); /* status lines need to be redrawn */ | |
2842 #endif | |
2843 fmarks_check_names(buf); /* check named file marks */ | |
2844 ml_timestamp(buf); /* reset timestamp */ | |
2845 } | |
2846 | |
2847 /* | |
2848 * set alternate file name for current window | |
2849 * | |
2850 * Used by do_one_cmd(), do_write() and do_ecmd(). | |
2851 * Return the buffer. | |
2852 */ | |
2853 buf_T * | |
2854 setaltfname(ffname, sfname, lnum) | |
2855 char_u *ffname; | |
2856 char_u *sfname; | |
2857 linenr_T lnum; | |
2858 { | |
2859 buf_T *buf; | |
2860 | |
2861 /* Create a buffer. 'buflisted' is not set if it's a new buffer */ | |
2862 buf = buflist_new(ffname, sfname, lnum, 0); | |
22 | 2863 if (buf != NULL && !cmdmod.keepalt) |
7 | 2864 curwin->w_alt_fnum = buf->b_fnum; |
2865 return buf; | |
2866 } | |
2867 | |
2868 /* | |
2869 * Get alternate file name for current window. | |
2870 * Return NULL if there isn't any, and give error message if requested. | |
2871 */ | |
2872 char_u * | |
2873 getaltfname(errmsg) | |
2874 int errmsg; /* give error message */ | |
2875 { | |
2876 char_u *fname; | |
2877 linenr_T dummy; | |
2878 | |
2879 if (buflist_name_nr(0, &fname, &dummy) == FAIL) | |
2880 { | |
2881 if (errmsg) | |
2882 EMSG(_(e_noalt)); | |
2883 return NULL; | |
2884 } | |
2885 return fname; | |
2886 } | |
2887 | |
2888 /* | |
2889 * Add a file name to the buflist and return its number. | |
2890 * Uses same flags as buflist_new(), except BLN_DUMMY. | |
2891 * | |
2892 * used by qf_init(), main() and doarglist() | |
2893 */ | |
2894 int | |
2895 buflist_add(fname, flags) | |
2896 char_u *fname; | |
2897 int flags; | |
2898 { | |
2899 buf_T *buf; | |
2900 | |
2901 buf = buflist_new(fname, NULL, (linenr_T)0, flags); | |
2902 if (buf != NULL) | |
2903 return buf->b_fnum; | |
2904 return 0; | |
2905 } | |
2906 | |
2907 #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO) | |
2908 /* | |
2909 * Adjust slashes in file names. Called after 'shellslash' was set. | |
2910 */ | |
2911 void | |
2912 buflist_slash_adjust() | |
2913 { | |
2914 buf_T *bp; | |
2915 | |
2916 for (bp = firstbuf; bp != NULL; bp = bp->b_next) | |
2917 { | |
2918 if (bp->b_ffname != NULL) | |
2919 slash_adjust(bp->b_ffname); | |
2920 if (bp->b_sfname != NULL) | |
2921 slash_adjust(bp->b_sfname); | |
2922 } | |
2923 } | |
2924 #endif | |
2925 | |
2926 /* | |
1743 | 2927 * Set alternate cursor position for the current buffer and window "win". |
7 | 2928 * Also save the local window option values. |
2929 */ | |
2930 void | |
1743 | 2931 buflist_altfpos(win) |
2932 win_T *win; | |
7 | 2933 { |
1743 | 2934 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE); |
7 | 2935 } |
2936 | |
2937 /* | |
2938 * Return TRUE if 'ffname' is not the same file as current file. | |
2939 * Fname must have a full path (expanded by mch_FullName()). | |
2940 */ | |
2941 int | |
2942 otherfile(ffname) | |
2943 char_u *ffname; | |
2944 { | |
2945 return otherfile_buf(curbuf, ffname | |
2946 #ifdef UNIX | |
2947 , NULL | |
2948 #endif | |
2949 ); | |
2950 } | |
2951 | |
2952 static int | |
2953 otherfile_buf(buf, ffname | |
2954 #ifdef UNIX | |
2955 , stp | |
2956 #endif | |
2957 ) | |
2958 buf_T *buf; | |
2959 char_u *ffname; | |
2960 #ifdef UNIX | |
2961 struct stat *stp; | |
2962 #endif | |
2963 { | |
2964 /* no name is different */ | |
2965 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL) | |
2966 return TRUE; | |
2967 if (fnamecmp(ffname, buf->b_ffname) == 0) | |
2968 return FALSE; | |
2969 #ifdef UNIX | |
2970 { | |
2971 struct stat st; | |
2972 | |
2973 /* If no struct stat given, get it now */ | |
2974 if (stp == NULL) | |
2975 { | |
1873 | 2976 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0) |
7 | 2977 st.st_dev = (dev_T)-1; |
2978 stp = &st; | |
2979 } | |
2980 /* Use dev/ino to check if the files are the same, even when the names | |
2981 * are different (possible with links). Still need to compare the | |
2982 * name above, for when the file doesn't exist yet. | |
2983 * Problem: The dev/ino changes when a file is deleted (and created | |
2984 * again) and remains the same when renamed/moved. We don't want to | |
2985 * mch_stat() each buffer each time, that would be too slow. Get the | |
2986 * dev/ino again when they appear to match, but not when they appear | |
2987 * to be different: Could skip a buffer when it's actually the same | |
2988 * file. */ | |
2989 if (buf_same_ino(buf, stp)) | |
2990 { | |
2991 buf_setino(buf); | |
2992 if (buf_same_ino(buf, stp)) | |
2993 return FALSE; | |
2994 } | |
2995 } | |
2996 #endif | |
2997 return TRUE; | |
2998 } | |
2999 | |
3000 #if defined(UNIX) || defined(PROTO) | |
3001 /* | |
3002 * Set inode and device number for a buffer. | |
3003 * Must always be called when b_fname is changed!. | |
3004 */ | |
3005 void | |
3006 buf_setino(buf) | |
3007 buf_T *buf; | |
3008 { | |
3009 struct stat st; | |
3010 | |
3011 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0) | |
3012 { | |
1873 | 3013 buf->b_dev_valid = TRUE; |
7 | 3014 buf->b_dev = st.st_dev; |
3015 buf->b_ino = st.st_ino; | |
3016 } | |
3017 else | |
1873 | 3018 buf->b_dev_valid = FALSE; |
7 | 3019 } |
3020 | |
3021 /* | |
3022 * Return TRUE if dev/ino in buffer "buf" matches with "stp". | |
3023 */ | |
3024 static int | |
3025 buf_same_ino(buf, stp) | |
3026 buf_T *buf; | |
3027 struct stat *stp; | |
3028 { | |
1873 | 3029 return (buf->b_dev_valid |
7 | 3030 && stp->st_dev == buf->b_dev |
3031 && stp->st_ino == buf->b_ino); | |
3032 } | |
3033 #endif | |
3034 | |
667 | 3035 /* |
3036 * Print info about the current buffer. | |
3037 */ | |
7 | 3038 void |
3039 fileinfo(fullname, shorthelp, dont_truncate) | |
667 | 3040 int fullname; /* when non-zero print full path */ |
7 | 3041 int shorthelp; |
3042 int dont_truncate; | |
3043 { | |
3044 char_u *name; | |
3045 int n; | |
3046 char_u *p; | |
3047 char_u *buffer; | |
272 | 3048 size_t len; |
7 | 3049 |
3050 buffer = alloc(IOSIZE); | |
3051 if (buffer == NULL) | |
3052 return; | |
3053 | |
3054 if (fullname > 1) /* 2 CTRL-G: include buffer number */ | |
3055 { | |
1869 | 3056 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum); |
7 | 3057 p = buffer + STRLEN(buffer); |
3058 } | |
3059 else | |
3060 p = buffer; | |
3061 | |
3062 *p++ = '"'; | |
3063 if (buf_spname(curbuf) != NULL) | |
3064 STRCPY(p, buf_spname(curbuf)); | |
3065 else | |
3066 { | |
3067 if (!fullname && curbuf->b_fname != NULL) | |
3068 name = curbuf->b_fname; | |
3069 else | |
3070 name = curbuf->b_ffname; | |
3071 home_replace(shorthelp ? curbuf : NULL, name, p, | |
3072 (int)(IOSIZE - (p - buffer)), TRUE); | |
3073 } | |
3074 | |
2280
941ff1cd317a
Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents:
2253
diff
changeset
|
3075 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s", |
7 | 3076 curbufIsChanged() ? (shortmess(SHM_MOD) |
3077 ? " [+]" : _(" [Modified]")) : " ", | |
3078 (curbuf->b_flags & BF_NOTEDITED) | |
3079 #ifdef FEAT_QUICKFIX | |
3080 && !bt_dontwrite(curbuf) | |
3081 #endif | |
3082 ? _("[Not edited]") : "", | |
3083 (curbuf->b_flags & BF_NEW) | |
3084 #ifdef FEAT_QUICKFIX | |
3085 && !bt_dontwrite(curbuf) | |
3086 #endif | |
3087 ? _("[New file]") : "", | |
3088 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "", | |
3089 curbuf->b_p_ro ? (shortmess(SHM_RO) ? "[RO]" | |
3090 : _("[readonly]")) : "", | |
3091 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK) | |
3092 || curbuf->b_p_ro) ? | |
3093 " " : ""); | |
3094 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100 | |
3095 * causes an overflow, thus for large numbers divide instead. */ | |
3096 if (curwin->w_cursor.lnum > 1000000L) | |
3097 n = (int)(((long)curwin->w_cursor.lnum) / | |
3098 ((long)curbuf->b_ml.ml_line_count / 100L)); | |
3099 else | |
3100 n = (int)(((long)curwin->w_cursor.lnum * 100L) / | |
3101 (long)curbuf->b_ml.ml_line_count); | |
3102 if (curbuf->b_ml.ml_flags & ML_EMPTY) | |
3103 { | |
2280
941ff1cd317a
Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents:
2253
diff
changeset
|
3104 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg)); |
7 | 3105 } |
3106 #ifdef FEAT_CMDL_INFO | |
3107 else if (p_ru) | |
3108 { | |
3109 /* Current line and column are already on the screen -- webb */ | |
3110 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
|
3111 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n); |
7 | 3112 else |
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, _("%ld lines --%d%%--"), |
7 | 3114 (long)curbuf->b_ml.ml_line_count, n); |
3115 } | |
3116 #endif | |
3117 else | |
3118 { | |
2280
941ff1cd317a
Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents:
2253
diff
changeset
|
3119 vim_snprintf_add((char *)buffer, IOSIZE, |
7 | 3120 _("line %ld of %ld --%d%%-- col "), |
3121 (long)curwin->w_cursor.lnum, | |
3122 (long)curbuf->b_ml.ml_line_count, | |
3123 n); | |
3124 validate_virtcol(); | |
1869 | 3125 len = STRLEN(buffer); |
3126 col_print(buffer + len, IOSIZE - len, | |
7 | 3127 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1); |
3128 } | |
3129 | |
1869 | 3130 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE)); |
7 | 3131 |
3132 if (dont_truncate) | |
3133 { | |
3134 /* Temporarily set msg_scroll to avoid the message being truncated. | |
3135 * First call msg_start() to get the message in the right place. */ | |
3136 msg_start(); | |
3137 n = msg_scroll; | |
3138 msg_scroll = TRUE; | |
3139 msg(buffer); | |
3140 msg_scroll = n; | |
3141 } | |
3142 else | |
3143 { | |
3144 p = msg_trunc_attr(buffer, FALSE, 0); | |
3145 if (restart_edit != 0 || (msg_scrolled && !need_wait_return)) | |
3146 /* Need to repeat the message after redrawing when: | |
3147 * - When restart_edit is set (otherwise there will be a delay | |
3148 * before redrawing). | |
3149 * - When the screen was scrolled but there is no wait-return | |
3150 * prompt. */ | |
678 | 3151 set_keep_msg(p, 0); |
7 | 3152 } |
3153 | |
3154 vim_free(buffer); | |
3155 } | |
3156 | |
3157 void | |
1869 | 3158 col_print(buf, buflen, col, vcol) |
7 | 3159 char_u *buf; |
1869 | 3160 size_t buflen; |
7 | 3161 int col; |
3162 int vcol; | |
3163 { | |
3164 if (col == vcol) | |
1869 | 3165 vim_snprintf((char *)buf, buflen, "%d", col); |
7 | 3166 else |
1869 | 3167 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol); |
7 | 3168 } |
3169 | |
3170 #if defined(FEAT_TITLE) || defined(PROTO) | |
3171 /* | |
3172 * put file name in title bar of window and in icon title | |
3173 */ | |
3174 | |
3175 static char_u *lasttitle = NULL; | |
3176 static char_u *lasticon = NULL; | |
3177 | |
3178 void | |
3179 maketitle() | |
3180 { | |
3181 char_u *p; | |
3182 char_u *t_str = NULL; | |
3183 char_u *i_name; | |
3184 char_u *i_str = NULL; | |
3185 int maxlen = 0; | |
3186 int len; | |
3187 int mustset; | |
3188 char_u buf[IOSIZE]; | |
3189 int off; | |
3190 | |
3191 if (!redrawing()) | |
3192 { | |
3193 /* Postpone updating the title when 'lazyredraw' is set. */ | |
3194 need_maketitle = TRUE; | |
3195 return; | |
3196 } | |
3197 | |
3198 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
|
3199 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL) |
7 | 3200 return; |
3201 | |
3202 if (p_title) | |
3203 { | |
3204 if (p_titlelen > 0) | |
3205 { | |
3206 maxlen = p_titlelen * Columns / 100; | |
3207 if (maxlen < 10) | |
3208 maxlen = 10; | |
3209 } | |
3210 | |
3211 t_str = buf; | |
3212 if (*p_titlestring != NUL) | |
3213 { | |
3214 #ifdef FEAT_STL_OPT | |
771 | 3215 if (stl_syntax & STL_IN_TITLE) |
3216 { | |
3217 int use_sandbox = FALSE; | |
3218 int save_called_emsg = called_emsg; | |
675 | 3219 |
3220 # ifdef FEAT_EVAL | |
771 | 3221 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0); |
675 | 3222 # endif |
771 | 3223 called_emsg = FALSE; |
7 | 3224 build_stl_str_hl(curwin, t_str, sizeof(buf), |
675 | 3225 p_titlestring, use_sandbox, |
681 | 3226 0, maxlen, NULL, NULL); |
771 | 3227 if (called_emsg) |
3228 set_string_option_direct((char_u *)"titlestring", -1, | |
3229 (char_u *)"", OPT_FREE, SID_ERROR); | |
3230 called_emsg |= save_called_emsg; | |
3231 } | |
7 | 3232 else |
3233 #endif | |
3234 t_str = p_titlestring; | |
3235 } | |
3236 else | |
3237 { | |
3238 /* format: "fname + (path) (1 of 2) - VIM" */ | |
3239 | |
3240 if (curbuf->b_fname == NULL) | |
2768 | 3241 vim_strncpy(buf, (char_u *)_("[No Name]"), IOSIZE - 100); |
7 | 3242 else |
3243 { | |
3244 p = transstr(gettail(curbuf->b_fname)); | |
416 | 3245 vim_strncpy(buf, p, IOSIZE - 100); |
7 | 3246 vim_free(p); |
3247 } | |
3248 | |
3249 switch (bufIsChanged(curbuf) | |
3250 + (curbuf->b_p_ro * 2) | |
3251 + (!curbuf->b_p_ma * 4)) | |
3252 { | |
3253 case 1: STRCAT(buf, " +"); break; | |
3254 case 2: STRCAT(buf, " ="); break; | |
3255 case 3: STRCAT(buf, " =+"); break; | |
3256 case 4: | |
3257 case 6: STRCAT(buf, " -"); break; | |
3258 case 5: | |
3259 case 7: STRCAT(buf, " -+"); break; | |
3260 } | |
3261 | |
3262 if (curbuf->b_fname != NULL) | |
3263 { | |
3264 /* Get path of file, replace home dir with ~ */ | |
3265 off = (int)STRLEN(buf); | |
3266 buf[off++] = ' '; | |
3267 buf[off++] = '('; | |
3268 home_replace(curbuf, curbuf->b_ffname, | |
3269 buf + off, IOSIZE - off, TRUE); | |
3270 #ifdef BACKSLASH_IN_FILENAME | |
3271 /* avoid "c:/name" to be reduced to "c" */ | |
3272 if (isalpha(buf[off]) && buf[off + 1] == ':') | |
3273 off += 2; | |
3274 #endif | |
3275 /* remove the file name */ | |
39 | 3276 p = gettail_sep(buf + off); |
7 | 3277 if (p == buf + off) |
3278 /* must be a help buffer */ | |
416 | 3279 vim_strncpy(buf + off, (char_u *)_("help"), |
1869 | 3280 (size_t)(IOSIZE - off - 1)); |
7 | 3281 else |
3282 *p = NUL; | |
39 | 3283 |
7 | 3284 /* translate unprintable chars */ |
3285 p = transstr(buf + off); | |
1869 | 3286 vim_strncpy(buf + off, p, (size_t)(IOSIZE - off - 1)); |
7 | 3287 vim_free(p); |
3288 STRCAT(buf, ")"); | |
3289 } | |
3290 | |
1869 | 3291 append_arg_number(curwin, buf, IOSIZE, FALSE); |
7 | 3292 |
3293 #if defined(FEAT_CLIENTSERVER) | |
3294 if (serverName != NULL) | |
3295 { | |
3296 STRCAT(buf, " - "); | |
2768 | 3297 vim_strcat(buf, serverName, IOSIZE); |
7 | 3298 } |
3299 else | |
3300 #endif | |
3301 STRCAT(buf, " - VIM"); | |
3302 | |
3303 if (maxlen > 0) | |
3304 { | |
3305 /* make it shorter by removing a bit in the middle */ | |
3277 | 3306 if (vim_strsize(buf) > maxlen) |
3307 trunc_string(buf, buf, maxlen, IOSIZE); | |
7 | 3308 } |
3309 } | |
3310 } | |
3311 mustset = ti_change(t_str, &lasttitle); | |
3312 | |
3313 if (p_icon) | |
3314 { | |
3315 i_str = buf; | |
3316 if (*p_iconstring != NUL) | |
3317 { | |
3318 #ifdef FEAT_STL_OPT | |
771 | 3319 if (stl_syntax & STL_IN_ICON) |
3320 { | |
3321 int use_sandbox = FALSE; | |
3322 int save_called_emsg = called_emsg; | |
675 | 3323 |
3324 # ifdef FEAT_EVAL | |
771 | 3325 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0); |
675 | 3326 # endif |
771 | 3327 called_emsg = FALSE; |
7 | 3328 build_stl_str_hl(curwin, i_str, sizeof(buf), |
675 | 3329 p_iconstring, use_sandbox, |
681 | 3330 0, 0, NULL, NULL); |
771 | 3331 if (called_emsg) |
3332 set_string_option_direct((char_u *)"iconstring", -1, | |
3333 (char_u *)"", OPT_FREE, SID_ERROR); | |
3334 called_emsg |= save_called_emsg; | |
3335 } | |
7 | 3336 else |
3337 #endif | |
3338 i_str = p_iconstring; | |
3339 } | |
3340 else | |
3341 { | |
3342 if (buf_spname(curbuf) != NULL) | |
3343 i_name = (char_u *)buf_spname(curbuf); | |
3344 else /* use file name only in icon */ | |
3345 i_name = gettail(curbuf->b_ffname); | |
3346 *i_str = NUL; | |
3347 /* Truncate name at 100 bytes. */ | |
835 | 3348 len = (int)STRLEN(i_name); |
7 | 3349 if (len > 100) |
3350 { | |
3351 len -= 100; | |
3352 #ifdef FEAT_MBYTE | |
3353 if (has_mbyte) | |
3354 len += (*mb_tail_off)(i_name, i_name + len) + 1; | |
3355 #endif | |
3356 i_name += len; | |
3357 } | |
3358 STRCPY(i_str, i_name); | |
3359 trans_characters(i_str, IOSIZE); | |
3360 } | |
3361 } | |
3362 | |
3363 mustset |= ti_change(i_str, &lasticon); | |
3364 | |
3365 if (mustset) | |
3366 resettitle(); | |
3367 } | |
3368 | |
3369 /* | |
3370 * Used for title and icon: Check if "str" differs from "*last". Set "*last" | |
3371 * from "str" if it does. | |
3372 * Return TRUE when "*last" changed. | |
3373 */ | |
3374 static int | |
3375 ti_change(str, last) | |
3376 char_u *str; | |
3377 char_u **last; | |
3378 { | |
3379 if ((str == NULL) != (*last == NULL) | |
3380 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0)) | |
3381 { | |
3382 vim_free(*last); | |
3383 if (str == NULL) | |
3384 *last = NULL; | |
3385 else | |
3386 *last = vim_strsave(str); | |
3387 return TRUE; | |
3388 } | |
3389 return FALSE; | |
3390 } | |
3391 | |
3392 /* | |
3393 * Put current window title back (used after calling a shell) | |
3394 */ | |
3395 void | |
3396 resettitle() | |
3397 { | |
3398 mch_settitle(lasttitle, lasticon); | |
3399 } | |
358 | 3400 |
3401 # if defined(EXITFREE) || defined(PROTO) | |
3402 void | |
3403 free_titles() | |
3404 { | |
3405 vim_free(lasttitle); | |
3406 vim_free(lasticon); | |
3407 } | |
3408 # endif | |
3409 | |
7 | 3410 #endif /* FEAT_TITLE */ |
3411 | |
686 | 3412 #if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO) |
7 | 3413 /* |
675 | 3414 * Build a string from the status line items in "fmt". |
7 | 3415 * Return length of string in screen cells. |
3416 * | |
675 | 3417 * Normally works for window "wp", except when working for 'tabline' then it |
3418 * is "curwin". | |
3419 * | |
7 | 3420 * Items are drawn interspersed with the text that surrounds it |
3421 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation | |
3422 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional | |
3423 * | |
3424 * If maxwidth is not zero, the string will be filled at any middle marker | |
3425 * or truncated if too long, fillchar is used for all whitespace. | |
3426 */ | |
3427 int | |
2694 | 3428 build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar, |
3429 maxwidth, hltab, tabtab) | |
7 | 3430 win_T *wp; |
686 | 3431 char_u *out; /* buffer to write into != NameBuff */ |
7 | 3432 size_t outlen; /* length of out[] */ |
3433 char_u *fmt; | |
1883 | 3434 int use_sandbox UNUSED; /* "fmt" was set insecurely, use sandbox */ |
7 | 3435 int fillchar; |
3436 int maxwidth; | |
681 | 3437 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */ |
3438 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */ | |
7 | 3439 { |
3440 char_u *p; | |
3441 char_u *s; | |
3442 char_u *t; | |
3443 char_u *linecont; | |
3444 #ifdef FEAT_EVAL | |
3445 win_T *o_curwin; | |
3446 buf_T *o_curbuf; | |
3447 #endif | |
3448 int empty_line; | |
3449 colnr_T virtcol; | |
3450 long l; | |
3451 long n; | |
3452 int prevchar_isflag; | |
3453 int prevchar_isitem; | |
3454 int itemisflag; | |
3455 int fillable; | |
3456 char_u *str; | |
3457 long num; | |
3458 int width; | |
3459 int itemcnt; | |
3460 int curitem; | |
3461 int groupitem[STL_MAX_ITEM]; | |
3462 int groupdepth; | |
3463 struct stl_item | |
3464 { | |
3465 char_u *start; | |
3466 int minwid; | |
3467 int maxwid; | |
3468 enum | |
3469 { | |
3470 Normal, | |
3471 Empty, | |
3472 Group, | |
3473 Middle, | |
3474 Highlight, | |
681 | 3475 TabPage, |
7 | 3476 Trunc |
3477 } type; | |
3478 } item[STL_MAX_ITEM]; | |
3479 int minwid; | |
3480 int maxwid; | |
3481 int zeropad; | |
3482 char_u base; | |
3483 char_u opt; | |
3484 #define TMPLEN 70 | |
3485 char_u tmp[TMPLEN]; | |
678 | 3486 char_u *usefmt = fmt; |
681 | 3487 struct stl_hlrec *sp; |
678 | 3488 |
3489 #ifdef FEAT_EVAL | |
3490 /* | |
3491 * When the format starts with "%!" then evaluate it as an expression and | |
3492 * use the result as the actual format string. | |
3493 */ | |
3494 if (fmt[0] == '%' && fmt[1] == '!') | |
3495 { | |
3496 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox); | |
3497 if (usefmt == NULL) | |
943 | 3498 usefmt = fmt; |
678 | 3499 } |
3500 #endif | |
7 | 3501 |
3502 if (fillchar == 0) | |
3503 fillchar = ' '; | |
819 | 3504 #ifdef FEAT_MBYTE |
3505 /* Can't handle a multi-byte fill character yet. */ | |
3506 else if (mb_char2len(fillchar) > 1) | |
3507 fillchar = '-'; | |
3508 #endif | |
3509 | |
7 | 3510 /* |
3511 * Get line & check if empty (cursorpos will show "0-1"). | |
3512 * If inversion is possible we use it. Else '=' characters are used. | |
3513 */ | |
3514 linecont = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE); | |
3515 empty_line = (*linecont == NUL); | |
3516 | |
3517 groupdepth = 0; | |
3518 p = out; | |
3519 curitem = 0; | |
3520 prevchar_isflag = TRUE; | |
3521 prevchar_isitem = FALSE; | |
678 | 3522 for (s = usefmt; *s; ) |
7 | 3523 { |
2704 | 3524 if (curitem == STL_MAX_ITEM) |
3525 { | |
3526 /* There are too many items. Add the error code to the statusline | |
3527 * to give the user a hint about what went wrong. */ | |
3528 if (p + 6 < out + outlen) | |
3529 { | |
3530 mch_memmove(p, " E541", (size_t)5); | |
3531 p += 5; | |
3532 } | |
3533 break; | |
3534 } | |
3535 | |
7 | 3536 if (*s != NUL && *s != '%') |
3537 prevchar_isflag = prevchar_isitem = FALSE; | |
3538 | |
3539 /* | |
3540 * Handle up to the next '%' or the end. | |
3541 */ | |
3542 while (*s != NUL && *s != '%' && p + 1 < out + outlen) | |
3543 *p++ = *s++; | |
3544 if (*s == NUL || p + 1 >= out + outlen) | |
3545 break; | |
3546 | |
3547 /* | |
3548 * Handle one '%' item. | |
3549 */ | |
3550 s++; | |
2694 | 3551 if (*s == NUL) /* ignore trailing % */ |
3552 break; | |
7 | 3553 if (*s == '%') |
3554 { | |
3555 if (p + 1 >= out + outlen) | |
3556 break; | |
3557 *p++ = *s++; | |
3558 prevchar_isflag = prevchar_isitem = FALSE; | |
3559 continue; | |
3560 } | |
3561 if (*s == STL_MIDDLEMARK) | |
3562 { | |
3563 s++; | |
3564 if (groupdepth > 0) | |
3565 continue; | |
3566 item[curitem].type = Middle; | |
3567 item[curitem++].start = p; | |
3568 continue; | |
3569 } | |
3570 if (*s == STL_TRUNCMARK) | |
3571 { | |
3572 s++; | |
3573 item[curitem].type = Trunc; | |
3574 item[curitem++].start = p; | |
3575 continue; | |
3576 } | |
3577 if (*s == ')') | |
3578 { | |
3579 s++; | |
3580 if (groupdepth < 1) | |
3581 continue; | |
3582 groupdepth--; | |
3583 | |
3584 t = item[groupitem[groupdepth]].start; | |
3585 *p = NUL; | |
3586 l = vim_strsize(t); | |
3587 if (curitem > groupitem[groupdepth] + 1 | |
3588 && item[groupitem[groupdepth]].minwid == 0) | |
3589 { | |
3590 /* remove group if all items are empty */ | |
3591 for (n = groupitem[groupdepth] + 1; n < curitem; n++) | |
3592 if (item[n].type == Normal) | |
3593 break; | |
3594 if (n == curitem) | |
3595 { | |
3596 p = t; | |
3597 l = 0; | |
3598 } | |
3599 } | |
3600 if (l > item[groupitem[groupdepth]].maxwid) | |
3601 { | |
3602 /* truncate, remove n bytes of text at the start */ | |
3603 #ifdef FEAT_MBYTE | |
3604 if (has_mbyte) | |
3605 { | |
3606 /* Find the first character that should be included. */ | |
3607 n = 0; | |
3608 while (l >= item[groupitem[groupdepth]].maxwid) | |
3609 { | |
3610 l -= ptr2cells(t + n); | |
474 | 3611 n += (*mb_ptr2len)(t + n); |
7 | 3612 } |
3613 } | |
3614 else | |
3615 #endif | |
835 | 3616 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1; |
7 | 3617 |
3618 *t = '<'; | |
1869 | 3619 mch_memmove(t + 1, t + n, (size_t)(p - (t + n))); |
7 | 3620 p = p - n + 1; |
3621 #ifdef FEAT_MBYTE | |
3622 /* Fill up space left over by half a double-wide char. */ | |
3623 while (++l < item[groupitem[groupdepth]].minwid) | |
3624 *p++ = fillchar; | |
3625 #endif | |
3626 | |
3627 /* correct the start of the items for the truncation */ | |
3628 for (l = groupitem[groupdepth] + 1; l < curitem; l++) | |
3629 { | |
3630 item[l].start -= n; | |
3631 if (item[l].start < t) | |
3632 item[l].start = t; | |
3633 } | |
3634 } | |
3635 else if (abs(item[groupitem[groupdepth]].minwid) > l) | |
3636 { | |
3637 /* fill */ | |
3638 n = item[groupitem[groupdepth]].minwid; | |
3639 if (n < 0) | |
3640 { | |
3641 /* fill by appending characters */ | |
3642 n = 0 - n; | |
3643 while (l++ < n && p + 1 < out + outlen) | |
3644 *p++ = fillchar; | |
3645 } | |
3646 else | |
3647 { | |
3648 /* fill by inserting characters */ | |
1869 | 3649 mch_memmove(t + n - l, t, (size_t)(p - t)); |
7 | 3650 l = n - l; |
3651 if (p + l >= out + outlen) | |
835 | 3652 l = (long)((out + outlen) - p - 1); |
7 | 3653 p += l; |
3654 for (n = groupitem[groupdepth] + 1; n < curitem; n++) | |
3655 item[n].start += l; | |
3656 for ( ; l > 0; l--) | |
3657 *t++ = fillchar; | |
3658 } | |
3659 } | |
3660 continue; | |
3661 } | |
3662 minwid = 0; | |
3663 maxwid = 9999; | |
3664 zeropad = FALSE; | |
3665 l = 1; | |
3666 if (*s == '0') | |
3667 { | |
3668 s++; | |
3669 zeropad = TRUE; | |
3670 } | |
3671 if (*s == '-') | |
3672 { | |
3673 s++; | |
3674 l = -1; | |
3675 } | |
3676 if (VIM_ISDIGIT(*s)) | |
3677 { | |
3678 minwid = (int)getdigits(&s); | |
3679 if (minwid < 0) /* overflow */ | |
3680 minwid = 0; | |
3681 } | |
678 | 3682 if (*s == STL_USER_HL) |
7 | 3683 { |
3684 item[curitem].type = Highlight; | |
3685 item[curitem].start = p; | |
3686 item[curitem].minwid = minwid > 9 ? 1 : minwid; | |
3687 s++; | |
3688 curitem++; | |
3689 continue; | |
3690 } | |
681 | 3691 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR) |
3692 { | |
3693 if (*s == STL_TABCLOSENR) | |
3694 { | |
3695 if (minwid == 0) | |
3696 { | |
3697 /* %X ends the close label, go back to the previously | |
3698 * define tab label nr. */ | |
3699 for (n = curitem - 1; n >= 0; --n) | |
3700 if (item[n].type == TabPage && item[n].minwid >= 0) | |
3701 { | |
3702 minwid = item[n].minwid; | |
3703 break; | |
3704 } | |
3705 } | |
3706 else | |
3707 /* close nrs are stored as negative values */ | |
3708 minwid = - minwid; | |
3709 } | |
3710 item[curitem].type = TabPage; | |
3711 item[curitem].start = p; | |
3712 item[curitem].minwid = minwid; | |
3713 s++; | |
3714 curitem++; | |
3715 continue; | |
3716 } | |
7 | 3717 if (*s == '.') |
3718 { | |
3719 s++; | |
3720 if (VIM_ISDIGIT(*s)) | |
3721 { | |
3722 maxwid = (int)getdigits(&s); | |
3723 if (maxwid <= 0) /* overflow */ | |
3724 maxwid = 50; | |
3725 } | |
3726 } | |
3727 minwid = (minwid > 50 ? 50 : minwid) * l; | |
3728 if (*s == '(') | |
3729 { | |
3730 groupitem[groupdepth++] = curitem; | |
3731 item[curitem].type = Group; | |
3732 item[curitem].start = p; | |
3733 item[curitem].minwid = minwid; | |
3734 item[curitem].maxwid = maxwid; | |
3735 s++; | |
3736 curitem++; | |
3737 continue; | |
3738 } | |
3739 if (vim_strchr(STL_ALL, *s) == NULL) | |
3740 { | |
3741 s++; | |
3742 continue; | |
3743 } | |
3744 opt = *s++; | |
3745 | |
3746 /* OK - now for the real work */ | |
3747 base = 'D'; | |
3748 itemisflag = FALSE; | |
3749 fillable = TRUE; | |
3750 num = -1; | |
3751 str = NULL; | |
3752 switch (opt) | |
3753 { | |
3754 case STL_FILEPATH: | |
3755 case STL_FULLPATH: | |
3756 case STL_FILENAME: | |
3757 fillable = FALSE; /* don't change ' ' to fillchar */ | |
3758 if (buf_spname(wp->w_buffer) != NULL) | |
3759 STRCPY(NameBuff, buf_spname(wp->w_buffer)); | |
3760 else | |
3761 { | |
3762 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname | |
667 | 3763 : wp->w_buffer->b_fname; |
7 | 3764 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE); |
3765 } | |
3766 trans_characters(NameBuff, MAXPATHL); | |
3767 if (opt != STL_FILENAME) | |
3768 str = NameBuff; | |
3769 else | |
3770 str = gettail(NameBuff); | |
3771 break; | |
3772 | |
3773 case STL_VIM_EXPR: /* '{' */ | |
3774 itemisflag = TRUE; | |
3775 t = p; | |
3776 while (*s != '}' && *s != NUL && p + 1 < out + outlen) | |
3777 *p++ = *s++; | |
3778 if (*s != '}') /* missing '}' or out of space */ | |
3779 break; | |
3780 s++; | |
3781 *p = 0; | |
3782 p = t; | |
3783 | |
3784 #ifdef FEAT_EVAL | |
1869 | 3785 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum); |
7 | 3786 set_internal_string_var((char_u *)"actual_curbuf", tmp); |
3787 | |
3788 o_curbuf = curbuf; | |
3789 o_curwin = curwin; | |
3790 curwin = wp; | |
3791 curbuf = wp->w_buffer; | |
3792 | |
675 | 3793 str = eval_to_string_safe(p, &t, use_sandbox); |
7 | 3794 |
3795 curwin = o_curwin; | |
3796 curbuf = o_curbuf; | |
145 | 3797 do_unlet((char_u *)"g:actual_curbuf", TRUE); |
7 | 3798 |
3799 if (str != NULL && *str != 0) | |
3800 { | |
3801 if (*skipdigits(str) == NUL) | |
3802 { | |
3803 num = atoi((char *)str); | |
3804 vim_free(str); | |
3805 str = NULL; | |
3806 itemisflag = FALSE; | |
3807 } | |
3808 } | |
3809 #endif | |
3810 break; | |
3811 | |
3812 case STL_LINE: | |
3813 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) | |
3814 ? 0L : (long)(wp->w_cursor.lnum); | |
3815 break; | |
3816 | |
3817 case STL_NUMLINES: | |
3818 num = wp->w_buffer->b_ml.ml_line_count; | |
3819 break; | |
3820 | |
3821 case STL_COLUMN: | |
3822 num = !(State & INSERT) && empty_line | |
3823 ? 0 : (int)wp->w_cursor.col + 1; | |
3824 break; | |
3825 | |
3826 case STL_VIRTCOL: | |
3827 case STL_VIRTCOL_ALT: | |
3828 /* In list mode virtcol needs to be recomputed */ | |
3829 virtcol = wp->w_virtcol; | |
3830 if (wp->w_p_list && lcs_tab1 == NUL) | |
3831 { | |
3832 wp->w_p_list = FALSE; | |
3833 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL); | |
3834 wp->w_p_list = TRUE; | |
3835 } | |
3836 ++virtcol; | |
3837 /* Don't display %V if it's the same as %c. */ | |
3838 if (opt == STL_VIRTCOL_ALT | |
3839 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line | |
3840 ? 0 : (int)wp->w_cursor.col + 1))) | |
3841 break; | |
3842 num = (long)virtcol; | |
3843 break; | |
3844 | |
3845 case STL_PERCENTAGE: | |
3846 num = (int)(((long)wp->w_cursor.lnum * 100L) / | |
3847 (long)wp->w_buffer->b_ml.ml_line_count); | |
3848 break; | |
3849 | |
3850 case STL_ALTPERCENT: | |
3851 str = tmp; | |
1869 | 3852 get_rel_pos(wp, str, TMPLEN); |
7 | 3853 break; |
3854 | |
3855 case STL_ARGLISTSTAT: | |
3856 fillable = FALSE; | |
3857 tmp[0] = 0; | |
1869 | 3858 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE)) |
7 | 3859 str = tmp; |
3860 break; | |
3861 | |
3862 case STL_KEYMAP: | |
3863 fillable = FALSE; | |
3864 if (get_keymap_str(wp, tmp, TMPLEN)) | |
3865 str = tmp; | |
3866 break; | |
3867 case STL_PAGENUM: | |
706 | 3868 #if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE) |
686 | 3869 num = printer_page_num; |
7 | 3870 #else |
3871 num = 0; | |
3872 #endif | |
3873 break; | |
3874 | |
3875 case STL_BUFNO: | |
3876 num = wp->w_buffer->b_fnum; | |
3877 break; | |
3878 | |
3879 case STL_OFFSET_X: | |
3880 base = 'X'; | |
3881 case STL_OFFSET: | |
3882 #ifdef FEAT_BYTEOFF | |
3883 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL); | |
3884 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ? | |
3885 0L : l + 1 + (!(State & INSERT) && empty_line ? | |
3886 0 : (int)wp->w_cursor.col); | |
3887 #endif | |
3888 break; | |
3889 | |
3890 case STL_BYTEVAL_X: | |
3891 base = 'X'; | |
3892 case STL_BYTEVAL: | |
1869 | 3893 if (wp->w_cursor.col > (colnr_T)STRLEN(linecont)) |
7 | 3894 num = 0; |
3895 else | |
3896 { | |
3897 #ifdef FEAT_MBYTE | |
3898 num = (*mb_ptr2char)(linecont + wp->w_cursor.col); | |
3899 #else | |
3900 num = linecont[wp->w_cursor.col]; | |
3901 #endif | |
3902 } | |
3903 if (num == NL) | |
3904 num = 0; | |
3905 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC) | |
3906 num = NL; | |
3907 break; | |
3908 | |
3909 case STL_ROFLAG: | |
3910 case STL_ROFLAG_ALT: | |
3911 itemisflag = TRUE; | |
3912 if (wp->w_buffer->b_p_ro) | |
3913 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : "[RO]"); | |
3914 break; | |
3915 | |
3916 case STL_HELPFLAG: | |
3917 case STL_HELPFLAG_ALT: | |
3918 itemisflag = TRUE; | |
3919 if (wp->w_buffer->b_help) | |
3920 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP" | |
809 | 3921 : _("[Help]")); |
7 | 3922 break; |
3923 | |
3924 #ifdef FEAT_AUTOCMD | |
3925 case STL_FILETYPE: | |
3926 if (*wp->w_buffer->b_p_ft != NUL | |
3927 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3) | |
3928 { | |
272 | 3929 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]", |
3930 wp->w_buffer->b_p_ft); | |
7 | 3931 str = tmp; |
3932 } | |
3933 break; | |
3934 | |
3935 case STL_FILETYPE_ALT: | |
3936 itemisflag = TRUE; | |
3937 if (*wp->w_buffer->b_p_ft != NUL | |
3938 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2) | |
3939 { | |
272 | 3940 vim_snprintf((char *)tmp, sizeof(tmp), ",%s", |
3941 wp->w_buffer->b_p_ft); | |
7 | 3942 for (t = tmp; *t != 0; t++) |
3943 *t = TOUPPER_LOC(*t); | |
3944 str = tmp; | |
3945 } | |
3946 break; | |
3947 #endif | |
3948 | |
3949 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX) | |
3950 case STL_PREVIEWFLAG: | |
3951 case STL_PREVIEWFLAG_ALT: | |
3952 itemisflag = TRUE; | |
3953 if (wp->w_p_pvw) | |
3954 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV" | |
3955 : _("[Preview]")); | |
3956 break; | |
2411
68e394361ca3
Add "q" item for 'statusline'. Add w:quickfix_title. (Lech Lorens)
Bram Moolenaar <bram@vim.org>
parents:
2403
diff
changeset
|
3957 |
68e394361ca3
Add "q" item for 'statusline'. Add w:quickfix_title. (Lech Lorens)
Bram Moolenaar <bram@vim.org>
parents:
2403
diff
changeset
|
3958 case STL_QUICKFIX: |
68e394361ca3
Add "q" item for 'statusline'. Add w:quickfix_title. (Lech Lorens)
Bram Moolenaar <bram@vim.org>
parents:
2403
diff
changeset
|
3959 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
|
3960 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
|
3961 ? _(msg_loclist) |
68e394361ca3
Add "q" item for 'statusline'. Add w:quickfix_title. (Lech Lorens)
Bram Moolenaar <bram@vim.org>
parents:
2403
diff
changeset
|
3962 : _(msg_qflist)); |
68e394361ca3
Add "q" item for 'statusline'. Add w:quickfix_title. (Lech Lorens)
Bram Moolenaar <bram@vim.org>
parents:
2403
diff
changeset
|
3963 break; |
7 | 3964 #endif |
3965 | |
3966 case STL_MODIFIED: | |
3967 case STL_MODIFIED_ALT: | |
3968 itemisflag = TRUE; | |
3969 switch ((opt == STL_MODIFIED_ALT) | |
3970 + bufIsChanged(wp->w_buffer) * 2 | |
3971 + (!wp->w_buffer->b_p_ma) * 4) | |
3972 { | |
3973 case 2: str = (char_u *)"[+]"; break; | |
3974 case 3: str = (char_u *)",+"; break; | |
3975 case 4: str = (char_u *)"[-]"; break; | |
3976 case 5: str = (char_u *)",-"; break; | |
3977 case 6: str = (char_u *)"[+-]"; break; | |
3978 case 7: str = (char_u *)",+-"; break; | |
3979 } | |
3980 break; | |
678 | 3981 |
3982 case STL_HIGHLIGHT: | |
3983 t = s; | |
3984 while (*s != '#' && *s != NUL) | |
3985 ++s; | |
3986 if (*s == '#') | |
3987 { | |
3988 item[curitem].type = Highlight; | |
3989 item[curitem].start = p; | |
835 | 3990 item[curitem].minwid = -syn_namen2id(t, (int)(s - t)); |
678 | 3991 curitem++; |
3992 } | |
3993 ++s; | |
3994 continue; | |
7 | 3995 } |
3996 | |
3997 item[curitem].start = p; | |
3998 item[curitem].type = Normal; | |
3999 if (str != NULL && *str) | |
4000 { | |
4001 t = str; | |
4002 if (itemisflag) | |
4003 { | |
4004 if ((t[0] && t[1]) | |
4005 && ((!prevchar_isitem && *t == ',') | |
4006 || (prevchar_isflag && *t == ' '))) | |
4007 t++; | |
4008 prevchar_isflag = TRUE; | |
4009 } | |
4010 l = vim_strsize(t); | |
4011 if (l > 0) | |
4012 prevchar_isitem = TRUE; | |
4013 if (l > maxwid) | |
4014 { | |
4015 while (l >= maxwid) | |
4016 #ifdef FEAT_MBYTE | |
4017 if (has_mbyte) | |
4018 { | |
4019 l -= ptr2cells(t); | |
474 | 4020 t += (*mb_ptr2len)(t); |
7 | 4021 } |
4022 else | |
4023 #endif | |
4024 l -= byte2cells(*t++); | |
4025 if (p + 1 >= out + outlen) | |
4026 break; | |
4027 *p++ = '<'; | |
4028 } | |
4029 if (minwid > 0) | |
4030 { | |
4031 for (; l < minwid && p + 1 < out + outlen; l++) | |
4032 { | |
4033 /* Don't put a "-" in front of a digit. */ | |
4034 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t)) | |
4035 *p++ = ' '; | |
4036 else | |
4037 *p++ = fillchar; | |
4038 } | |
4039 minwid = 0; | |
4040 } | |
4041 else | |
4042 minwid *= -1; | |
4043 while (*t && p + 1 < out + outlen) | |
4044 { | |
4045 *p++ = *t++; | |
4046 /* Change a space by fillchar, unless fillchar is '-' and a | |
4047 * digit follows. */ | |
4048 if (fillable && p[-1] == ' ' | |
4049 && (!VIM_ISDIGIT(*t) || fillchar != '-')) | |
4050 p[-1] = fillchar; | |
4051 } | |
4052 for (; l < minwid && p + 1 < out + outlen; l++) | |
4053 *p++ = fillchar; | |
4054 } | |
4055 else if (num >= 0) | |
4056 { | |
4057 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16)); | |
4058 char_u nstr[20]; | |
4059 | |
4060 if (p + 20 >= out + outlen) | |
4061 break; /* not sufficient space */ | |
4062 prevchar_isitem = TRUE; | |
4063 t = nstr; | |
4064 if (opt == STL_VIRTCOL_ALT) | |
4065 { | |
4066 *t++ = '-'; | |
4067 minwid--; | |
4068 } | |
4069 *t++ = '%'; | |
4070 if (zeropad) | |
4071 *t++ = '0'; | |
4072 *t++ = '*'; | |
1869 | 4073 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd'); |
7 | 4074 *t = 0; |
4075 | |
4076 for (n = num, l = 1; n >= nbase; n /= nbase) | |
4077 l++; | |
4078 if (opt == STL_VIRTCOL_ALT) | |
4079 l++; | |
4080 if (l > maxwid) | |
4081 { | |
4082 l += 2; | |
4083 n = l - maxwid; | |
4084 while (l-- > maxwid) | |
4085 num /= nbase; | |
4086 *t++ = '>'; | |
4087 *t++ = '%'; | |
4088 *t = t[-3]; | |
4089 *++t = 0; | |
272 | 4090 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr, |
4091 0, num, n); | |
7 | 4092 } |
4093 else | |
272 | 4094 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr, |
4095 minwid, num); | |
7 | 4096 p += STRLEN(p); |
4097 } | |
4098 else | |
4099 item[curitem].type = Empty; | |
4100 | |
4101 if (opt == STL_VIM_EXPR) | |
4102 vim_free(str); | |
4103 | |
4104 if (num >= 0 || (!itemisflag && str && *str)) | |
4105 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */ | |
4106 curitem++; | |
4107 } | |
4108 *p = NUL; | |
4109 itemcnt = curitem; | |
4110 | |
678 | 4111 #ifdef FEAT_EVAL |
4112 if (usefmt != fmt) | |
4113 vim_free(usefmt); | |
4114 #endif | |
4115 | |
7 | 4116 width = vim_strsize(out); |
4117 if (maxwidth > 0 && width > maxwidth) | |
4118 { | |
1736 | 4119 /* Result is too long, must truncate somewhere. */ |
7 | 4120 l = 0; |
4121 if (itemcnt == 0) | |
4122 s = out; | |
4123 else | |
4124 { | |
4125 for ( ; l < itemcnt; l++) | |
4126 if (item[l].type == Trunc) | |
4127 { | |
4128 /* Truncate at %< item. */ | |
4129 s = item[l].start; | |
4130 break; | |
4131 } | |
4132 if (l == itemcnt) | |
4133 { | |
4134 /* No %< item, truncate first item. */ | |
4135 s = item[0].start; | |
4136 l = 0; | |
4137 } | |
4138 } | |
4139 | |
4140 if (width - vim_strsize(s) >= maxwidth) | |
4141 { | |
4142 /* Truncation mark is beyond max length */ | |
4143 #ifdef FEAT_MBYTE | |
4144 if (has_mbyte) | |
4145 { | |
4146 s = out; | |
4147 width = 0; | |
4148 for (;;) | |
4149 { | |
4150 width += ptr2cells(s); | |
4151 if (width >= maxwidth) | |
4152 break; | |
474 | 4153 s += (*mb_ptr2len)(s); |
7 | 4154 } |
4155 /* Fill up for half a double-wide character. */ | |
4156 while (++width < maxwidth) | |
4157 *s++ = fillchar; | |
4158 } | |
4159 else | |
4160 #endif | |
4161 s = out + maxwidth - 1; | |
4162 for (l = 0; l < itemcnt; l++) | |
4163 if (item[l].start > s) | |
4164 break; | |
4165 itemcnt = l; | |
4166 *s++ = '>'; | |
4167 *s = 0; | |
4168 } | |
4169 else | |
4170 { | |
4171 #ifdef FEAT_MBYTE | |
4172 if (has_mbyte) | |
4173 { | |
4174 n = 0; | |
4175 while (width >= maxwidth) | |
4176 { | |
4177 width -= ptr2cells(s + n); | |
474 | 4178 n += (*mb_ptr2len)(s + n); |
7 | 4179 } |
4180 } | |
4181 else | |
4182 #endif | |
4183 n = width - maxwidth + 1; | |
4184 p = s + n; | |
1618 | 4185 STRMOVE(s + 1, p); |
7 | 4186 *s = '<'; |
4187 | |
4188 /* Fill up for half a double-wide character. */ | |
4189 while (++width < maxwidth) | |
4190 { | |
4191 s = s + STRLEN(s); | |
4192 *s++ = fillchar; | |
4193 *s = NUL; | |
4194 } | |
4195 | |
4196 --n; /* count the '<' */ | |
4197 for (; l < itemcnt; l++) | |
4198 { | |
4199 if (item[l].start - n >= s) | |
4200 item[l].start -= n; | |
4201 else | |
4202 item[l].start = s; | |
4203 } | |
4204 } | |
4205 width = maxwidth; | |
4206 } | |
4207 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen) | |
4208 { | |
4209 /* Apply STL_MIDDLE if any */ | |
4210 for (l = 0; l < itemcnt; l++) | |
4211 if (item[l].type == Middle) | |
4212 break; | |
4213 if (l < itemcnt) | |
4214 { | |
4215 p = item[l].start + maxwidth - width; | |
1618 | 4216 STRMOVE(p, item[l].start); |
7 | 4217 for (s = item[l].start; s < p; s++) |
4218 *s = fillchar; | |
4219 for (l++; l < itemcnt; l++) | |
4220 item[l].start += maxwidth - width; | |
4221 width = maxwidth; | |
4222 } | |
4223 } | |
4224 | |
681 | 4225 /* Store the info about highlighting. */ |
4226 if (hltab != NULL) | |
7 | 4227 { |
681 | 4228 sp = hltab; |
7 | 4229 for (l = 0; l < itemcnt; l++) |
4230 { | |
4231 if (item[l].type == Highlight) | |
4232 { | |
681 | 4233 sp->start = item[l].start; |
4234 sp->userhl = item[l].minwid; | |
4235 sp++; | |
7 | 4236 } |
4237 } | |
681 | 4238 sp->start = NULL; |
4239 sp->userhl = 0; | |
4240 } | |
4241 | |
4242 /* Store the info about tab pages labels. */ | |
4243 if (tabtab != NULL) | |
4244 { | |
4245 sp = tabtab; | |
4246 for (l = 0; l < itemcnt; l++) | |
4247 { | |
4248 if (item[l].type == TabPage) | |
4249 { | |
4250 sp->start = item[l].start; | |
4251 sp->userhl = item[l].minwid; | |
4252 sp++; | |
4253 } | |
4254 } | |
4255 sp->start = NULL; | |
4256 sp->userhl = 0; | |
7 | 4257 } |
4258 | |
4259 return width; | |
4260 } | |
4261 #endif /* FEAT_STL_OPT */ | |
4262 | |
686 | 4263 #if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \ |
4264 || defined(FEAT_GUI_TABLINE) || defined(PROTO) | |
7 | 4265 /* |
1869 | 4266 * Get relative cursor position in window into "buf[buflen]", in the form 99%, |
4267 * using "Top", "Bot" or "All" when appropriate. | |
7 | 4268 */ |
4269 void | |
1869 | 4270 get_rel_pos(wp, buf, buflen) |
7 | 4271 win_T *wp; |
1869 | 4272 char_u *buf; |
4273 int buflen; | |
7 | 4274 { |
4275 long above; /* number of lines above window */ | |
4276 long below; /* number of lines below window */ | |
4277 | |
4278 above = wp->w_topline - 1; | |
4279 #ifdef FEAT_DIFF | |
4280 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill; | |
4281 #endif | |
4282 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1; | |
4283 if (below <= 0) | |
1869 | 4284 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")), |
4285 (size_t)(buflen - 1)); | |
7 | 4286 else if (above <= 0) |
1869 | 4287 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1)); |
7 | 4288 else |
1869 | 4289 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L |
7 | 4290 ? (int)(above / ((above + below) / 100L)) |
4291 : (int)(above * 100L / (above + below))); | |
4292 } | |
4293 #endif | |
4294 | |
4295 /* | |
1869 | 4296 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file. |
7 | 4297 * Return TRUE if it was appended. |
4298 */ | |
1869 | 4299 static int |
4300 append_arg_number(wp, buf, buflen, add_file) | |
7 | 4301 win_T *wp; |
4302 char_u *buf; | |
1869 | 4303 int buflen; |
7 | 4304 int add_file; /* Add "file" before the arg number */ |
4305 { | |
4306 char_u *p; | |
4307 | |
4308 if (ARGCOUNT <= 1) /* nothing to do */ | |
4309 return FALSE; | |
4310 | |
1869 | 4311 p = buf + STRLEN(buf); /* go to the end of the buffer */ |
4312 if (p - buf + 35 >= buflen) /* getting too long */ | |
7 | 4313 return FALSE; |
4314 *p++ = ' '; | |
4315 *p++ = '('; | |
4316 if (add_file) | |
4317 { | |
4318 STRCPY(p, "file "); | |
4319 p += 5; | |
4320 } | |
1869 | 4321 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)), |
4322 wp->w_arg_idx_invalid ? "(%d) of %d)" | |
7 | 4323 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT); |
4324 return TRUE; | |
4325 } | |
4326 | |
4327 /* | |
4328 * If fname is not a full path, make it a full path. | |
4329 * Returns pointer to allocated memory (NULL for failure). | |
4330 */ | |
4331 char_u * | |
4332 fix_fname(fname) | |
4333 char_u *fname; | |
4334 { | |
4335 /* | |
4336 * Force expanding the path always for Unix, because symbolic links may | |
4337 * mess up the full path name, even though it starts with a '/'. | |
4338 * Also expand when there is ".." in the file name, try to remove it, | |
4339 * because "c:/src/../README" is equal to "c:/README". | |
1420 | 4340 * Similarly "c:/src//file" is equal to "c:/src/file". |
7 | 4341 * For MS-Windows also expand names like "longna~1" to "longname". |
4342 */ | |
1082 | 4343 #ifdef UNIX |
7 | 4344 return FullName_save(fname, TRUE); |
4345 #else | |
1420 | 4346 if (!vim_isAbsName(fname) |
4347 || strstr((char *)fname, "..") != NULL | |
4348 || strstr((char *)fname, "//") != NULL | |
4349 # ifdef BACKSLASH_IN_FILENAME | |
4350 || strstr((char *)fname, "\\\\") != NULL | |
4351 # endif | |
4352 # if defined(MSWIN) || defined(DJGPP) | |
7 | 4353 || vim_strchr(fname, '~') != NULL |
1420 | 4354 # endif |
7 | 4355 ) |
4356 return FullName_save(fname, FALSE); | |
4357 | |
4358 fname = vim_strsave(fname); | |
4359 | |
1420 | 4360 # ifdef USE_FNAME_CASE |
4361 # ifdef USE_LONG_FNAME | |
7 | 4362 if (USE_LONG_FNAME) |
1420 | 4363 # endif |
7 | 4364 { |
4365 if (fname != NULL) | |
4366 fname_case(fname, 0); /* set correct case for file name */ | |
4367 } | |
1420 | 4368 # endif |
7 | 4369 |
4370 return fname; | |
4371 #endif | |
4372 } | |
4373 | |
4374 /* | |
4375 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL. | |
4376 * "ffname" becomes a pointer to allocated memory (or NULL). | |
4377 */ | |
4378 void | |
4379 fname_expand(buf, ffname, sfname) | |
1876 | 4380 buf_T *buf UNUSED; |
7 | 4381 char_u **ffname; |
4382 char_u **sfname; | |
4383 { | |
4384 if (*ffname == NULL) /* if no file name given, nothing to do */ | |
4385 return; | |
4386 if (*sfname == NULL) /* if no short file name given, use ffname */ | |
4387 *sfname = *ffname; | |
4388 *ffname = fix_fname(*ffname); /* expand to full path */ | |
4389 | |
4390 #ifdef FEAT_SHORTCUT | |
4391 if (!buf->b_p_bin) | |
4392 { | |
844 | 4393 char_u *rfname; |
7 | 4394 |
4395 /* If the file name is a shortcut file, use the file it links to. */ | |
4396 rfname = mch_resolve_shortcut(*ffname); | |
844 | 4397 if (rfname != NULL) |
7 | 4398 { |
4399 vim_free(*ffname); | |
4400 *ffname = rfname; | |
4401 *sfname = rfname; | |
4402 } | |
4403 } | |
4404 #endif | |
4405 } | |
4406 | |
4407 /* | |
4408 * Get the file name for an argument list entry. | |
4409 */ | |
4410 char_u * | |
4411 alist_name(aep) | |
4412 aentry_T *aep; | |
4413 { | |
4414 buf_T *bp; | |
4415 | |
4416 /* Use the name from the associated buffer if it exists. */ | |
4417 bp = buflist_findnr(aep->ae_fnum); | |
1036 | 4418 if (bp == NULL || bp->b_fname == NULL) |
7 | 4419 return aep->ae_fname; |
4420 return bp->b_fname; | |
4421 } | |
4422 | |
4423 #if defined(FEAT_WINDOWS) || defined(PROTO) | |
4424 /* | |
4425 * do_arg_all(): Open up to 'count' windows, one for each argument. | |
4426 */ | |
4427 void | |
726 | 4428 do_arg_all(count, forceit, keep_tabs) |
7 | 4429 int count; |
4430 int forceit; /* hide buffers in current windows */ | |
1411 | 4431 int keep_tabs; /* keep current tabs, for ":tab drop file" */ |
7 | 4432 { |
4433 int i; | |
4434 win_T *wp, *wpnext; | |
3380 | 4435 char_u *opened; /* Array of weight for which args are open: |
4436 * 0: not opened | |
4437 * 1: opened in other tab | |
4438 * 2: opened in curtab | |
4439 * 3: opened in curtab and curwin | |
4440 */ | |
1411 | 4441 int opened_len; /* length of opened[] */ |
7 | 4442 int use_firstwin = FALSE; /* use first window for arglist */ |
4443 int split_ret = OK; | |
4444 int p_ea_save; | |
4445 alist_T *alist; /* argument list to be used */ | |
4446 buf_T *buf; | |
698 | 4447 tabpage_T *tpnext; |
4448 int had_tab = cmdmod.tab; | |
3380 | 4449 win_T *old_curwin, *last_curwin; |
4450 tabpage_T *old_curtab, *last_curtab; | |
726 | 4451 win_T *new_curwin = NULL; |
4452 tabpage_T *new_curtab = NULL; | |
7 | 4453 |
4454 if (ARGCOUNT <= 0) | |
4455 { | |
4456 /* Don't give an error message. We don't want it when the ":all" | |
4457 * command is in the .vimrc. */ | |
4458 return; | |
4459 } | |
4460 setpcmark(); | |
4461 | |
4462 opened_len = ARGCOUNT; | |
4463 opened = alloc_clear((unsigned)opened_len); | |
4464 if (opened == NULL) | |
4465 return; | |
4466 | |
3380 | 4467 /* Autocommands may do anything to the argument list. Make sure it's not |
4468 * freed while we are working here by "locking" it. We still have to | |
4469 * watch out for its size to be changed. */ | |
4470 alist = curwin->w_alist; | |
4471 ++alist->al_refcount; | |
4472 | |
4473 old_curwin = curwin; | |
4474 old_curtab = curtab; | |
4475 | |
7 | 4476 #ifdef FEAT_GUI |
4477 need_mouse_correct = TRUE; | |
4478 #endif | |
4479 | |
4480 /* | |
4481 * Try closing all windows that are not in the argument list. | |
4482 * Also close windows that are not full width; | |
4483 * When 'hidden' or "forceit" set the buffer becomes hidden. | |
4484 * Windows that have a changed buffer and can't be hidden won't be closed. | |
698 | 4485 * When the ":tab" modifier was used do this for all tab pages. |
7 | 4486 */ |
698 | 4487 if (had_tab > 0) |
3582 | 4488 goto_tabpage_tp(first_tabpage, TRUE); |
698 | 4489 for (;;) |
7 | 4490 { |
698 | 4491 tpnext = curtab->tp_next; |
4492 for (wp = firstwin; wp != NULL; wp = wpnext) | |
7 | 4493 { |
698 | 4494 wpnext = wp->w_next; |
4495 buf = wp->w_buffer; | |
4496 if (buf->b_ffname == NULL | |
3380 | 4497 || (!keep_tabs && buf->b_nwindows > 1) |
698 | 4498 #ifdef FEAT_VERTSPLIT |
4499 || wp->w_width != Columns | |
4500 #endif | |
4501 ) | |
3380 | 4502 i = opened_len; |
698 | 4503 else |
7 | 4504 { |
698 | 4505 /* check if the buffer in this window is in the arglist */ |
3380 | 4506 for (i = 0; i < opened_len; ++i) |
7 | 4507 { |
3380 | 4508 if (i < alist->al_ga.ga_len |
4509 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum | |
4510 || fullpathcmp(alist_name(&AARGLIST(alist)[i]), | |
4511 buf->b_ffname, TRUE) & FPC_SAME)) | |
7 | 4512 { |
3380 | 4513 int weight = 1; |
4514 | |
4515 if (old_curtab == curtab) | |
726 | 4516 { |
3380 | 4517 ++weight; |
4518 if (old_curwin == wp) | |
4519 ++weight; | |
4520 } | |
4521 | |
4522 if (weight > (int)opened[i]) | |
4523 { | |
4524 opened[i] = (char_u)weight; | |
726 | 4525 if (i == 0) |
4526 { | |
3380 | 4527 if (new_curwin != NULL) |
4528 new_curwin->w_arg_idx = opened_len; | |
726 | 4529 new_curwin = wp; |
4530 new_curtab = curtab; | |
4531 } | |
4532 } | |
3380 | 4533 else if (keep_tabs) |
4534 i = opened_len; | |
4535 | |
4536 if (wp->w_alist != alist) | |
698 | 4537 { |
4538 /* Use the current argument list for all windows | |
4539 * containing a file from it. */ | |
4540 alist_unlink(wp->w_alist); | |
3380 | 4541 wp->w_alist = alist; |
698 | 4542 ++wp->w_alist->al_refcount; |
4543 } | |
4544 break; | |
7 | 4545 } |
698 | 4546 } |
4547 } | |
4548 wp->w_arg_idx = i; | |
4549 | |
3380 | 4550 if (i == opened_len && !keep_tabs)/* close this window */ |
698 | 4551 { |
4552 if (P_HID(buf) || forceit || buf->b_nwindows > 1 | |
726 | 4553 || !bufIsChanged(buf)) |
698 | 4554 { |
4555 /* If the buffer was changed, and we would like to hide it, | |
4556 * try autowriting. */ | |
726 | 4557 if (!P_HID(buf) && buf->b_nwindows <= 1 |
4558 && bufIsChanged(buf)) | |
698 | 4559 { |
4560 (void)autowrite(buf, FALSE); | |
4561 #ifdef FEAT_AUTOCMD | |
4562 /* check if autocommands removed the window */ | |
4563 if (!win_valid(wp) || !buf_valid(buf)) | |
4564 { | |
4565 wpnext = firstwin; /* start all over... */ | |
4566 continue; | |
4567 } | |
4568 #endif | |
4569 } | |
4570 #ifdef FEAT_WINDOWS | |
726 | 4571 /* don't close last window */ |
3380 | 4572 if (firstwin == lastwin |
4573 && (first_tabpage->tp_next == NULL || !had_tab)) | |
698 | 4574 #endif |
4575 use_firstwin = TRUE; | |
4576 #ifdef FEAT_WINDOWS | |
4577 else | |
4578 { | |
4579 win_close(wp, !P_HID(buf) && !bufIsChanged(buf)); | |
4580 # ifdef FEAT_AUTOCMD | |
4581 /* check if autocommands removed the next window */ | |
4582 if (!win_valid(wpnext)) | |
4583 wpnext = firstwin; /* start all over... */ | |
4584 # endif | |
4585 } | |
4586 #endif | |
7 | 4587 } |
4588 } | |
4589 } | |
698 | 4590 |
4591 /* Without the ":tab" modifier only do the current tab page. */ | |
4592 if (had_tab == 0 || tpnext == NULL) | |
4593 break; | |
4594 | |
7 | 4595 # ifdef FEAT_AUTOCMD |
698 | 4596 /* check if autocommands removed the next tab page */ |
4597 if (!valid_tabpage(tpnext)) | |
4598 tpnext = first_tabpage; /* start all over...*/ | |
7 | 4599 # endif |
3582 | 4600 goto_tabpage_tp(tpnext, TRUE); |
7 | 4601 } |
4602 | |
4603 /* | |
4604 * Open a window for files in the argument list that don't have one. | |
4605 * ARGCOUNT may change while doing this, because of autocommands. | |
4606 */ | |
3380 | 4607 if (count > opened_len || count <= 0) |
4608 count = opened_len; | |
7 | 4609 |
4610 #ifdef FEAT_AUTOCMD | |
4611 /* Don't execute Win/Buf Enter/Leave autocommands here. */ | |
4612 ++autocmd_no_enter; | |
4613 ++autocmd_no_leave; | |
4614 #endif | |
3380 | 4615 last_curwin = curwin; |
4616 last_curtab = curtab; | |
7 | 4617 win_enter(lastwin, FALSE); |
819 | 4618 #ifdef FEAT_WINDOWS |
4619 /* ":drop all" should re-use an empty window to avoid "--remote-tab" | |
4620 * leaving an empty tab page when executed locally. */ | |
4621 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1 | |
4622 && curbuf->b_ffname == NULL && !curbuf->b_changed) | |
4623 use_firstwin = TRUE; | |
4624 #endif | |
4625 | |
3380 | 4626 for (i = 0; i < count && i < opened_len && !got_int; ++i) |
7 | 4627 { |
4628 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1) | |
4629 arg_had_last = TRUE; | |
3380 | 4630 if (opened[i] > 0) |
7 | 4631 { |
4632 /* Move the already present window to below the current window */ | |
4633 if (curwin->w_arg_idx != i) | |
4634 { | |
4635 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next) | |
4636 { | |
4637 if (wpnext->w_arg_idx == i) | |
4638 { | |
3380 | 4639 if (keep_tabs) |
4640 { | |
4641 new_curwin = wpnext; | |
4642 new_curtab = curtab; | |
4643 } | |
4644 else | |
4645 win_move_after(wpnext, curwin); | |
7 | 4646 break; |
4647 } | |
4648 } | |
4649 } | |
4650 } | |
4651 else if (split_ret == OK) | |
4652 { | |
4653 if (!use_firstwin) /* split current window */ | |
4654 { | |
4655 p_ea_save = p_ea; | |
4656 p_ea = TRUE; /* use space from all windows */ | |
4657 split_ret = win_split(0, WSP_ROOM | WSP_BELOW); | |
4658 p_ea = p_ea_save; | |
4659 if (split_ret == FAIL) | |
4660 continue; | |
4661 } | |
4662 #ifdef FEAT_AUTOCMD | |
4663 else /* first window: do autocmd for leaving this buffer */ | |
4664 --autocmd_no_leave; | |
4665 #endif | |
4666 | |
4667 /* | |
726 | 4668 * edit file "i" |
7 | 4669 */ |
4670 curwin->w_arg_idx = i; | |
726 | 4671 if (i == 0) |
4672 { | |
4673 new_curwin = curwin; | |
4674 new_curtab = curtab; | |
4675 } | |
7 | 4676 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL, |
4677 ECMD_ONE, | |
4678 ((P_HID(curwin->w_buffer) | |
4679 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0) | |
1743 | 4680 + ECMD_OLDBUF, curwin); |
7 | 4681 #ifdef FEAT_AUTOCMD |
4682 if (use_firstwin) | |
4683 ++autocmd_no_leave; | |
4684 #endif | |
4685 use_firstwin = FALSE; | |
4686 } | |
4687 ui_breakcheck(); | |
698 | 4688 |
4689 /* When ":tab" was used open a new tab for a new window repeatedly. */ | |
4690 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm) | |
4691 cmdmod.tab = 9999; | |
7 | 4692 } |
4693 | |
4694 /* Remove the "lock" on the argument list. */ | |
4695 alist_unlink(alist); | |
4696 | |
4697 #ifdef FEAT_AUTOCMD | |
4698 --autocmd_no_enter; | |
4699 #endif | |
3380 | 4700 /* restore last referenced tabpage's curwin */ |
4701 if (last_curtab != new_curtab) | |
4702 { | |
4703 if (valid_tabpage(last_curtab)) | |
3582 | 4704 goto_tabpage_tp(last_curtab, TRUE); |
3380 | 4705 if (win_valid(last_curwin)) |
4706 win_enter(last_curwin, FALSE); | |
4707 } | |
726 | 4708 /* to window with first arg */ |
4709 if (valid_tabpage(new_curtab)) | |
3582 | 4710 goto_tabpage_tp(new_curtab, TRUE); |
726 | 4711 if (win_valid(new_curwin)) |
4712 win_enter(new_curwin, FALSE); | |
4713 | |
7 | 4714 #ifdef FEAT_AUTOCMD |
4715 --autocmd_no_leave; | |
4716 #endif | |
4717 vim_free(opened); | |
4718 } | |
4719 | |
4720 # if defined(FEAT_LISTCMDS) || defined(PROTO) | |
4721 /* | |
4722 * Open a window for a number of buffers. | |
4723 */ | |
4724 void | |
4725 ex_buffer_all(eap) | |
4726 exarg_T *eap; | |
4727 { | |
4728 buf_T *buf; | |
4729 win_T *wp, *wpnext; | |
4730 int split_ret = OK; | |
4731 int p_ea_save; | |
4732 int open_wins = 0; | |
4733 int r; | |
4734 int count; /* Maximum number of windows to open. */ | |
4735 int all; /* When TRUE also load inactive buffers. */ | |
698 | 4736 #ifdef FEAT_WINDOWS |
4737 int had_tab = cmdmod.tab; | |
4738 tabpage_T *tpnext; | |
4739 #endif | |
7 | 4740 |
4741 if (eap->addr_count == 0) /* make as many windows as possible */ | |
4742 count = 9999; | |
4743 else | |
4744 count = eap->line2; /* make as many windows as specified */ | |
4745 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide) | |
4746 all = FALSE; | |
4747 else | |
4748 all = TRUE; | |
4749 | |
4750 setpcmark(); | |
4751 | |
4752 #ifdef FEAT_GUI | |
4753 need_mouse_correct = TRUE; | |
4754 #endif | |
4755 | |
4756 /* | |
4757 * Close superfluous windows (two windows for the same buffer). | |
4758 * Also close windows that are not full-width. | |
4759 */ | |
698 | 4760 #ifdef FEAT_WINDOWS |
4761 if (had_tab > 0) | |
3582 | 4762 goto_tabpage_tp(first_tabpage, TRUE); |
698 | 4763 for (;;) |
7 | 4764 { |
698 | 4765 #endif |
4766 tpnext = curtab->tp_next; | |
4767 for (wp = firstwin; wp != NULL; wp = wpnext) | |
7 | 4768 { |
698 | 4769 wpnext = wp->w_next; |
706 | 4770 if ((wp->w_buffer->b_nwindows > 1 |
698 | 4771 #ifdef FEAT_VERTSPLIT |
4772 || ((cmdmod.split & WSP_VERT) | |
4773 ? wp->w_height + wp->w_status_height < Rows - p_ch | |
706 | 4774 - tabline_height() |
698 | 4775 : wp->w_width != Columns) |
4776 #endif | |
701 | 4777 #ifdef FEAT_WINDOWS |
4778 || (had_tab > 0 && wp != firstwin) | |
4779 #endif | |
3570 | 4780 ) && firstwin != lastwin |
4781 #ifdef FEAT_AUTOCMD | |
4782 && !(wp->w_closing || wp->w_buffer->b_closing) | |
4783 #endif | |
4784 ) | |
698 | 4785 { |
4786 win_close(wp, FALSE); | |
7 | 4787 #ifdef FEAT_AUTOCMD |
698 | 4788 wpnext = firstwin; /* just in case an autocommand does |
4789 something strange with windows */ | |
701 | 4790 tpnext = first_tabpage; /* start all over...*/ |
698 | 4791 open_wins = 0; |
4792 #endif | |
4793 } | |
4794 else | |
4795 ++open_wins; | |
7 | 4796 } |
698 | 4797 |
4798 #ifdef FEAT_WINDOWS | |
4799 /* Without the ":tab" modifier only do the current tab page. */ | |
4800 if (had_tab == 0 || tpnext == NULL) | |
4801 break; | |
3582 | 4802 goto_tabpage_tp(tpnext, TRUE); |
7 | 4803 } |
698 | 4804 #endif |
7 | 4805 |
4806 /* | |
4807 * Go through the buffer list. When a buffer doesn't have a window yet, | |
4808 * open one. Otherwise move the window to the right position. | |
4809 * Watch out for autocommands that delete buffers or windows! | |
4810 */ | |
4811 #ifdef FEAT_AUTOCMD | |
4812 /* Don't execute Win/Buf Enter/Leave autocommands here. */ | |
4813 ++autocmd_no_enter; | |
4814 #endif | |
4815 win_enter(lastwin, FALSE); | |
4816 #ifdef FEAT_AUTOCMD | |
4817 ++autocmd_no_leave; | |
4818 #endif | |
4819 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next) | |
4820 { | |
4821 /* Check if this buffer needs a window */ | |
4822 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl) | |
4823 continue; | |
4824 | |
701 | 4825 #ifdef FEAT_WINDOWS |
4826 if (had_tab != 0) | |
4827 { | |
4828 /* With the ":tab" modifier don't move the window. */ | |
4829 if (buf->b_nwindows > 0) | |
4830 wp = lastwin; /* buffer has a window, skip it */ | |
4831 else | |
4832 wp = NULL; | |
4833 } | |
4834 else | |
4835 #endif | |
4836 { | |
4837 /* Check if this buffer already has a window */ | |
4838 for (wp = firstwin; wp != NULL; wp = wp->w_next) | |
4839 if (wp->w_buffer == buf) | |
4840 break; | |
4841 /* If the buffer already has a window, move it */ | |
4842 if (wp != NULL) | |
4843 win_move_after(wp, curwin); | |
4844 } | |
4845 | |
4846 if (wp == NULL && split_ret == OK) | |
7 | 4847 { |
4848 /* Split the window and put the buffer in it */ | |
4849 p_ea_save = p_ea; | |
4850 p_ea = TRUE; /* use space from all windows */ | |
4851 split_ret = win_split(0, WSP_ROOM | WSP_BELOW); | |
4852 ++open_wins; | |
4853 p_ea = p_ea_save; | |
4854 if (split_ret == FAIL) | |
4855 continue; | |
4856 | |
4857 /* Open the buffer in this window. */ | |
576 | 4858 #if defined(HAS_SWAP_EXISTS_ACTION) |
7 | 4859 swap_exists_action = SEA_DIALOG; |
4860 #endif | |
4861 set_curbuf(buf, DOBUF_GOTO); | |
4862 #ifdef FEAT_AUTOCMD | |
4863 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */ | |
20 | 4864 { |
576 | 4865 #if defined(HAS_SWAP_EXISTS_ACTION) |
20 | 4866 swap_exists_action = SEA_NONE; |
7 | 4867 # endif |
4868 break; | |
4869 } | |
4870 #endif | |
576 | 4871 #if defined(HAS_SWAP_EXISTS_ACTION) |
7 | 4872 if (swap_exists_action == SEA_QUIT) |
4873 { | |
24 | 4874 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) |
4875 cleanup_T cs; | |
4876 | |
4877 /* Reset the error/interrupt/exception state here so that | |
4878 * aborting() returns FALSE when closing a window. */ | |
4879 enter_cleanup(&cs); | |
4880 # endif | |
4881 | |
4882 /* User selected Quit at ATTENTION prompt; close this window. */ | |
7 | 4883 win_close(curwin, TRUE); |
4884 --open_wins; | |
4885 swap_exists_action = SEA_NONE; | |
602 | 4886 swap_exists_did_quit = TRUE; |
24 | 4887 |
4888 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) | |
4889 /* Restore the error/interrupt/exception state if not | |
4890 * discarded by a new aborting error, interrupt, or uncaught | |
4891 * exception. */ | |
4892 leave_cleanup(&cs); | |
4893 # endif | |
7 | 4894 } |
4895 else | |
4896 handle_swap_exists(NULL); | |
4897 #endif | |
4898 } | |
4899 | |
4900 ui_breakcheck(); | |
4901 if (got_int) | |
4902 { | |
4903 (void)vgetc(); /* only break the file loading, not the rest */ | |
4904 break; | |
4905 } | |
20 | 4906 #ifdef FEAT_EVAL |
4907 /* Autocommands deleted the buffer or aborted script processing!!! */ | |
4908 if (aborting()) | |
4909 break; | |
4910 #endif | |
698 | 4911 #ifdef FEAT_WINDOWS |
4912 /* When ":tab" was used open a new tab for a new window repeatedly. */ | |
4913 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm) | |
4914 cmdmod.tab = 9999; | |
4915 #endif | |
7 | 4916 } |
4917 #ifdef FEAT_AUTOCMD | |
4918 --autocmd_no_enter; | |
4919 #endif | |
4920 win_enter(firstwin, FALSE); /* back to first window */ | |
4921 #ifdef FEAT_AUTOCMD | |
4922 --autocmd_no_leave; | |
4923 #endif | |
4924 | |
4925 /* | |
4926 * Close superfluous windows. | |
4927 */ | |
4928 for (wp = lastwin; open_wins > count; ) | |
4929 { | |
4930 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer) | |
4931 || autowrite(wp->w_buffer, FALSE) == OK); | |
4932 #ifdef FEAT_AUTOCMD | |
4933 if (!win_valid(wp)) | |
4934 { | |
4935 /* BufWrite Autocommands made the window invalid, start over */ | |
4936 wp = lastwin; | |
4937 } | |
4938 else | |
4939 #endif | |
4940 if (r) | |
4941 { | |
4942 win_close(wp, !P_HID(wp->w_buffer)); | |
4943 --open_wins; | |
4944 wp = lastwin; | |
4945 } | |
4946 else | |
4947 { | |
4948 wp = wp->w_prev; | |
4949 if (wp == NULL) | |
4950 break; | |
4951 } | |
4952 } | |
4953 } | |
4954 # endif /* FEAT_LISTCMDS */ | |
4955 | |
4956 #endif /* FEAT_WINDOWS */ | |
4957 | |
717 | 4958 static int chk_modeline __ARGS((linenr_T, int)); |
4959 | |
7 | 4960 /* |
4961 * do_modelines() - process mode lines for the current file | |
4962 * | |
717 | 4963 * "flags" can be: |
4964 * OPT_WINONLY only set options local to window | |
4965 * OPT_NOWIN don't set options local to window | |
4966 * | |
7 | 4967 * Returns immediately if the "ml" option isn't set. |
4968 */ | |
4969 void | |
717 | 4970 do_modelines(flags) |
4971 int flags; | |
7 | 4972 { |
23 | 4973 linenr_T lnum; |
4974 int nmlines; | |
4975 static int entered = 0; | |
7 | 4976 |
4977 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0) | |
4978 return; | |
4979 | |
4980 /* Disallow recursive entry here. Can happen when executing a modeline | |
4981 * triggers an autocommand, which reloads modelines with a ":do". */ | |
4982 if (entered) | |
4983 return; | |
4984 | |
4985 ++entered; | |
4986 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines; | |
4987 ++lnum) | |
717 | 4988 if (chk_modeline(lnum, flags) == FAIL) |
7 | 4989 nmlines = 0; |
4990 | |
4991 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines | |
4992 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum) | |
717 | 4993 if (chk_modeline(lnum, flags) == FAIL) |
7 | 4994 nmlines = 0; |
4995 --entered; | |
4996 } | |
4997 | |
4998 #include "version.h" /* for version number */ | |
4999 | |
5000 /* | |
5001 * chk_modeline() - check a single line for a mode string | |
5002 * Return FAIL if an error encountered. | |
5003 */ | |
5004 static int | |
717 | 5005 chk_modeline(lnum, flags) |
7 | 5006 linenr_T lnum; |
717 | 5007 int flags; /* Same as for do_modelines(). */ |
7 | 5008 { |
5009 char_u *s; | |
5010 char_u *e; | |
5011 char_u *linecopy; /* local copy of any modeline found */ | |
5012 int prev; | |
5013 int vers; | |
5014 int end; | |
5015 int retval = OK; | |
5016 char_u *save_sourcing_name; | |
5017 linenr_T save_sourcing_lnum; | |
5018 #ifdef FEAT_EVAL | |
5019 scid_T save_SID; | |
5020 #endif | |
5021 | |
5022 prev = -1; | |
5023 for (s = ml_get(lnum); *s != NUL; ++s) | |
5024 { | |
5025 if (prev == -1 || vim_isspace(prev)) | |
5026 { | |
5027 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0) | |
5028 || STRNCMP(s, "vi:", (size_t)3) == 0) | |
5029 break; | |
5030 if (STRNCMP(s, "vim", 3) == 0) | |
5031 { | |
5032 if (s[3] == '<' || s[3] == '=' || s[3] == '>') | |
5033 e = s + 4; | |
5034 else | |
5035 e = s + 3; | |
5036 vers = getdigits(&e); | |
5037 if (*e == ':' | |
5038 && (s[3] == ':' | |
5039 || (VIM_VERSION_100 >= vers && isdigit(s[3])) | |
5040 || (VIM_VERSION_100 < vers && s[3] == '<') | |
5041 || (VIM_VERSION_100 > vers && s[3] == '>') | |
5042 || (VIM_VERSION_100 == vers && s[3] == '='))) | |
5043 break; | |
5044 } | |
5045 } | |
5046 prev = *s; | |
5047 } | |
5048 | |
5049 if (*s) | |
5050 { | |
5051 do /* skip over "ex:", "vi:" or "vim:" */ | |
5052 ++s; | |
5053 while (s[-1] != ':'); | |
5054 | |
5055 s = linecopy = vim_strsave(s); /* copy the line, it will change */ | |
5056 if (linecopy == NULL) | |
5057 return FAIL; | |
5058 | |
5059 save_sourcing_lnum = sourcing_lnum; | |
5060 save_sourcing_name = sourcing_name; | |
5061 sourcing_lnum = lnum; /* prepare for emsg() */ | |
5062 sourcing_name = (char_u *)"modelines"; | |
5063 | |
5064 end = FALSE; | |
5065 while (end == FALSE) | |
5066 { | |
5067 s = skipwhite(s); | |
5068 if (*s == NUL) | |
5069 break; | |
5070 | |
5071 /* | |
5072 * Find end of set command: ':' or end of line. | |
5073 * Skip over "\:", replacing it with ":". | |
5074 */ | |
5075 for (e = s; *e != ':' && *e != NUL; ++e) | |
5076 if (e[0] == '\\' && e[1] == ':') | |
1618 | 5077 STRMOVE(e, e + 1); |
7 | 5078 if (*e == NUL) |
5079 end = TRUE; | |
5080 | |
5081 /* | |
5082 * If there is a "set" command, require a terminating ':' and | |
5083 * ignore the stuff after the ':'. | |
5084 * "vi:set opt opt opt: foo" -- foo not interpreted | |
5085 * "vi:opt opt opt: foo" -- foo interpreted | |
5086 * Accept "se" for compatibility with Elvis. | |
5087 */ | |
5088 if (STRNCMP(s, "set ", (size_t)4) == 0 | |
5089 || STRNCMP(s, "se ", (size_t)3) == 0) | |
5090 { | |
5091 if (*e != ':') /* no terminating ':'? */ | |
5092 break; | |
5093 end = TRUE; | |
5094 s = vim_strchr(s, ' ') + 1; | |
5095 } | |
5096 *e = NUL; /* truncate the set command */ | |
5097 | |
5098 if (*s != NUL) /* skip over an empty "::" */ | |
5099 { | |
5100 #ifdef FEAT_EVAL | |
5101 save_SID = current_SID; | |
5102 current_SID = SID_MODELINE; | |
5103 #endif | |
717 | 5104 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags); |
7 | 5105 #ifdef FEAT_EVAL |
5106 current_SID = save_SID; | |
5107 #endif | |
5108 if (retval == FAIL) /* stop if error found */ | |
5109 break; | |
5110 } | |
5111 s = e + 1; /* advance to next part */ | |
5112 } | |
5113 | |
5114 sourcing_lnum = save_sourcing_lnum; | |
5115 sourcing_name = save_sourcing_name; | |
5116 | |
5117 vim_free(linecopy); | |
5118 } | |
5119 return retval; | |
5120 } | |
5121 | |
1559 | 5122 #if defined(FEAT_VIMINFO) || defined(PROTO) |
7 | 5123 int |
5124 read_viminfo_bufferlist(virp, writing) | |
5125 vir_T *virp; | |
5126 int writing; | |
5127 { | |
5128 char_u *tab; | |
5129 linenr_T lnum; | |
5130 colnr_T col; | |
5131 buf_T *buf; | |
5132 char_u *sfname; | |
5133 char_u *xline; | |
5134 | |
5135 /* Handle long line and escaped characters. */ | |
5136 xline = viminfo_readstring(virp, 1, FALSE); | |
5137 | |
5138 /* don't read in if there are files on the command-line or if writing: */ | |
5139 if (xline != NULL && !writing && ARGCOUNT == 0 | |
5140 && find_viminfo_parameter('%') != NULL) | |
5141 { | |
5142 /* Format is: <fname> Tab <lnum> Tab <col>. | |
5143 * Watch out for a Tab in the file name, work from the end. */ | |
5144 lnum = 0; | |
5145 col = 0; | |
5146 tab = vim_strrchr(xline, '\t'); | |
5147 if (tab != NULL) | |
5148 { | |
5149 *tab++ = '\0'; | |
1869 | 5150 col = (colnr_T)atoi((char *)tab); |
7 | 5151 tab = vim_strrchr(xline, '\t'); |
5152 if (tab != NULL) | |
5153 { | |
5154 *tab++ = '\0'; | |
5155 lnum = atol((char *)tab); | |
5156 } | |
5157 } | |
5158 | |
5159 /* Expand "~/" in the file name at "line + 1" to a full path. | |
5160 * Then try shortening it by comparing with the current directory */ | |
5161 expand_env(xline, NameBuff, MAXPATHL); | |
1411 | 5162 sfname = shorten_fname1(NameBuff); |
7 | 5163 |
5164 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED); | |
5165 if (buf != NULL) /* just in case... */ | |
5166 { | |
5167 buf->b_last_cursor.lnum = lnum; | |
5168 buf->b_last_cursor.col = col; | |
5169 buflist_setfpos(buf, curwin, lnum, col, FALSE); | |
5170 } | |
5171 } | |
5172 vim_free(xline); | |
5173 | |
5174 return viminfo_readline(virp); | |
5175 } | |
5176 | |
5177 void | |
5178 write_viminfo_bufferlist(fp) | |
5179 FILE *fp; | |
5180 { | |
5181 buf_T *buf; | |
5182 #ifdef FEAT_WINDOWS | |
5183 win_T *win; | |
671 | 5184 tabpage_T *tp; |
7 | 5185 #endif |
5186 char_u *line; | |
23 | 5187 int max_buffers; |
7 | 5188 |
5189 if (find_viminfo_parameter('%') == NULL) | |
5190 return; | |
5191 | |
23 | 5192 /* Without a number -1 is returned: do all buffers. */ |
5193 max_buffers = get_viminfo_parameter('%'); | |
5194 | |
7 | 5195 /* Allocate room for the file name, lnum and col. */ |
1869 | 5196 #define LINE_BUF_LEN (MAXPATHL + 40) |
5197 line = alloc(LINE_BUF_LEN); | |
7 | 5198 if (line == NULL) |
5199 return; | |
5200 | |
5201 #ifdef FEAT_WINDOWS | |
671 | 5202 FOR_ALL_TAB_WINDOWS(tp, win) |
7 | 5203 set_last_cursor(win); |
5204 #else | |
5205 set_last_cursor(curwin); | |
5206 #endif | |
5207 | |
2545
298d8d6e69be
Avoid warnings from the clang compiler. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2520
diff
changeset
|
5208 fputs(_("\n# Buffer list:\n"), fp); |
7 | 5209 for (buf = firstbuf; buf != NULL ; buf = buf->b_next) |
5210 { | |
5211 if (buf->b_fname == NULL | |
5212 || !buf->b_p_bl | |
5213 #ifdef FEAT_QUICKFIX | |
5214 || bt_quickfix(buf) | |
5215 #endif | |
5216 || removable(buf->b_ffname)) | |
5217 continue; | |
5218 | |
23 | 5219 if (max_buffers-- == 0) |
5220 break; | |
7 | 5221 putc('%', fp); |
5222 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
|
5223 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d", |
7 | 5224 (long)buf->b_last_cursor.lnum, |
5225 buf->b_last_cursor.col); | |
5226 viminfo_writestring(fp, line); | |
5227 } | |
5228 vim_free(line); | |
5229 } | |
5230 #endif | |
5231 | |
5232 | |
5233 /* | |
5234 * Return special buffer name. | |
5235 * Returns NULL when the buffer has a normal file name. | |
5236 */ | |
5237 char * | |
5238 buf_spname(buf) | |
5239 buf_T *buf; | |
5240 { | |
5241 #if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS) | |
5242 if (bt_quickfix(buf)) | |
643 | 5243 { |
1561 | 5244 win_T *win = NULL; |
1559 | 5245 tabpage_T *tp; |
643 | 5246 |
5247 /* | |
5248 * For location list window, w_llist_ref points to the location list. | |
5249 * For quickfix window, w_llist_ref is NULL. | |
5250 */ | |
1559 | 5251 FOR_ALL_TAB_WINDOWS(tp, win) |
643 | 5252 if (win->w_buffer == buf) |
1819 | 5253 goto win_found; |
5254 win_found: | |
643 | 5255 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
|
5256 return _(msg_loclist); |
643 | 5257 else |
2411
68e394361ca3
Add "q" item for 'statusline'. Add w:quickfix_title. (Lech Lorens)
Bram Moolenaar <bram@vim.org>
parents:
2403
diff
changeset
|
5258 return _(msg_qflist); |
643 | 5259 } |
7 | 5260 #endif |
5261 #ifdef FEAT_QUICKFIX | |
5262 /* There is no _file_ when 'buftype' is "nofile", b_sfname | |
5263 * contains the name as specified by the user */ | |
5264 if (bt_nofile(buf)) | |
5265 { | |
5266 if (buf->b_sfname != NULL) | |
5267 return (char *)buf->b_sfname; | |
1764 | 5268 return _("[Scratch]"); |
7 | 5269 } |
5270 #endif | |
5271 if (buf->b_fname == NULL) | |
9 | 5272 return _("[No Name]"); |
7 | 5273 return NULL; |
5274 } | |
5275 | |
5276 | |
5277 #if defined(FEAT_SIGNS) || defined(PROTO) | |
5278 /* | |
5279 * Insert the sign into the signlist. | |
5280 */ | |
5281 static void | |
5282 insert_sign(buf, prev, next, id, lnum, typenr) | |
5283 buf_T *buf; /* buffer to store sign in */ | |
5284 signlist_T *prev; /* previous sign entry */ | |
5285 signlist_T *next; /* next sign entry */ | |
5286 int id; /* sign ID */ | |
5287 linenr_T lnum; /* line number which gets the mark */ | |
5288 int typenr; /* typenr of sign we are adding */ | |
5289 { | |
5290 signlist_T *newsign; | |
5291 | |
5292 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE); | |
5293 if (newsign != NULL) | |
5294 { | |
5295 newsign->id = id; | |
5296 newsign->lnum = lnum; | |
5297 newsign->typenr = typenr; | |
5298 newsign->next = next; | |
5299 #ifdef FEAT_NETBEANS_INTG | |
5300 newsign->prev = prev; | |
5301 if (next != NULL) | |
5302 next->prev = newsign; | |
5303 #endif | |
5304 | |
5305 if (prev == NULL) | |
5306 { | |
5307 /* When adding first sign need to redraw the windows to create the | |
5308 * column for signs. */ | |
5309 if (buf->b_signlist == NULL) | |
5310 { | |
5311 redraw_buf_later(buf, NOT_VALID); | |
5312 changed_cline_bef_curs(); | |
5313 } | |
5314 | |
5315 /* first sign in signlist */ | |
5316 buf->b_signlist = newsign; | |
5317 } | |
5318 else | |
5319 prev->next = newsign; | |
5320 } | |
5321 } | |
5322 | |
5323 /* | |
5324 * Add the sign into the signlist. Find the right spot to do it though. | |
5325 */ | |
5326 void | |
5327 buf_addsign(buf, id, lnum, typenr) | |
5328 buf_T *buf; /* buffer to store sign in */ | |
5329 int id; /* sign ID */ | |
5330 linenr_T lnum; /* line number which gets the mark */ | |
5331 int typenr; /* typenr of sign we are adding */ | |
5332 { | |
5333 signlist_T *sign; /* a sign in the signlist */ | |
5334 signlist_T *prev; /* the previous sign */ | |
5335 | |
5336 prev = NULL; | |
5337 for (sign = buf->b_signlist; sign != NULL; sign = sign->next) | |
5338 { | |
5339 if (lnum == sign->lnum && id == sign->id) | |
5340 { | |
5341 sign->typenr = typenr; | |
5342 return; | |
5343 } | |
5344 else if ( | |
5345 #ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */ | |
5346 id < 0 && | |
5347 #endif | |
5348 lnum < sign->lnum) | |
5349 { | |
5350 #ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */ | |
5351 /* XXX - GRP: Is this because of sign slide problem? Or is it | |
5352 * really needed? Or is it because we allow multiple signs per | |
5353 * line? If so, should I add that feature to FEAT_SIGNS? | |
5354 */ | |
5355 while (prev != NULL && prev->lnum == lnum) | |
5356 prev = prev->prev; | |
5357 if (prev == NULL) | |
5358 sign = buf->b_signlist; | |
5359 else | |
5360 sign = prev->next; | |
5361 #endif | |
5362 insert_sign(buf, prev, sign, id, lnum, typenr); | |
5363 return; | |
5364 } | |
5365 prev = sign; | |
5366 } | |
5367 #ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */ | |
5368 /* XXX - GRP: See previous comment */ | |
5369 while (prev != NULL && prev->lnum == lnum) | |
5370 prev = prev->prev; | |
5371 if (prev == NULL) | |
5372 sign = buf->b_signlist; | |
5373 else | |
5374 sign = prev->next; | |
5375 #endif | |
5376 insert_sign(buf, prev, sign, id, lnum, typenr); | |
5377 | |
5378 return; | |
5379 } | |
5380 | |
1869 | 5381 linenr_T |
7 | 5382 buf_change_sign_type(buf, markId, typenr) |
5383 buf_T *buf; /* buffer to store sign in */ | |
5384 int markId; /* sign ID */ | |
5385 int typenr; /* typenr of sign we are adding */ | |
5386 { | |
5387 signlist_T *sign; /* a sign in the signlist */ | |
5388 | |
5389 for (sign = buf->b_signlist; sign != NULL; sign = sign->next) | |
5390 { | |
5391 if (sign->id == markId) | |
5392 { | |
5393 sign->typenr = typenr; | |
5394 return sign->lnum; | |
5395 } | |
5396 } | |
5397 | |
1869 | 5398 return (linenr_T)0; |
7 | 5399 } |
5400 | |
1869 | 5401 int |
7 | 5402 buf_getsigntype(buf, lnum, type) |
5403 buf_T *buf; | |
5404 linenr_T lnum; | |
5405 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */ | |
5406 { | |
5407 signlist_T *sign; /* a sign in a b_signlist */ | |
5408 | |
5409 for (sign = buf->b_signlist; sign != NULL; sign = sign->next) | |
5410 if (sign->lnum == lnum | |
5411 && (type == SIGN_ANY | |
5412 # ifdef FEAT_SIGN_ICONS | |
5413 || (type == SIGN_ICON | |
5414 && sign_get_image(sign->typenr) != NULL) | |
5415 # endif | |
5416 || (type == SIGN_TEXT | |
5417 && sign_get_text(sign->typenr) != NULL) | |
5418 || (type == SIGN_LINEHL | |
5419 && sign_get_attr(sign->typenr, TRUE) != 0))) | |
5420 return sign->typenr; | |
5421 return 0; | |
5422 } | |
5423 | |
5424 | |
5425 linenr_T | |
5426 buf_delsign(buf, id) | |
5427 buf_T *buf; /* buffer sign is stored in */ | |
5428 int id; /* sign id */ | |
5429 { | |
5430 signlist_T **lastp; /* pointer to pointer to current sign */ | |
5431 signlist_T *sign; /* a sign in a b_signlist */ | |
5432 signlist_T *next; /* the next sign in a b_signlist */ | |
5433 linenr_T lnum; /* line number whose sign was deleted */ | |
5434 | |
5435 lastp = &buf->b_signlist; | |
5436 lnum = 0; | |
5437 for (sign = buf->b_signlist; sign != NULL; sign = next) | |
5438 { | |
5439 next = sign->next; | |
5440 if (sign->id == id) | |
5441 { | |
5442 *lastp = next; | |
5443 #ifdef FEAT_NETBEANS_INTG | |
5444 if (next != NULL) | |
5445 next->prev = sign->prev; | |
5446 #endif | |
5447 lnum = sign->lnum; | |
5448 vim_free(sign); | |
5449 break; | |
5450 } | |
5451 else | |
5452 lastp = &sign->next; | |
5453 } | |
5454 | |
5455 /* When deleted the last sign need to redraw the windows to remove the | |
5456 * sign column. */ | |
5457 if (buf->b_signlist == NULL) | |
5458 { | |
5459 redraw_buf_later(buf, NOT_VALID); | |
5460 changed_cline_bef_curs(); | |
5461 } | |
5462 | |
5463 return lnum; | |
5464 } | |
5465 | |
5466 | |
5467 /* | |
5468 * Find the line number of the sign with the requested id. If the sign does | |
5469 * not exist, return 0 as the line number. This will still let the correct file | |
5470 * get loaded. | |
5471 */ | |
5472 int | |
5473 buf_findsign(buf, id) | |
5474 buf_T *buf; /* buffer to store sign in */ | |
5475 int id; /* sign ID */ | |
5476 { | |
5477 signlist_T *sign; /* a sign in the signlist */ | |
5478 | |
5479 for (sign = buf->b_signlist; sign != NULL; sign = sign->next) | |
5480 if (sign->id == id) | |
5481 return sign->lnum; | |
5482 | |
5483 return 0; | |
5484 } | |
5485 | |
5486 int | |
5487 buf_findsign_id(buf, lnum) | |
5488 buf_T *buf; /* buffer whose sign we are searching for */ | |
5489 linenr_T lnum; /* line number of sign */ | |
5490 { | |
5491 signlist_T *sign; /* a sign in the signlist */ | |
5492 | |
5493 for (sign = buf->b_signlist; sign != NULL; sign = sign->next) | |
5494 if (sign->lnum == lnum) | |
5495 return sign->id; | |
5496 | |
5497 return 0; | |
5498 } | |
5499 | |
5500 | |
5501 # if defined(FEAT_NETBEANS_INTG) || defined(PROTO) | |
5502 /* see if a given type of sign exists on a specific line */ | |
5503 int | |
5504 buf_findsigntype_id(buf, lnum, typenr) | |
5505 buf_T *buf; /* buffer whose sign we are searching for */ | |
5506 linenr_T lnum; /* line number of sign */ | |
5507 int typenr; /* sign type number */ | |
5508 { | |
5509 signlist_T *sign; /* a sign in the signlist */ | |
5510 | |
5511 for (sign = buf->b_signlist; sign != NULL; sign = sign->next) | |
5512 if (sign->lnum == lnum && sign->typenr == typenr) | |
5513 return sign->id; | |
5514 | |
5515 return 0; | |
5516 } | |
5517 | |
5518 | |
5519 # if defined(FEAT_SIGN_ICONS) || defined(PROTO) | |
5520 /* return the number of icons on the given line */ | |
5521 int | |
5522 buf_signcount(buf, lnum) | |
5523 buf_T *buf; | |
5524 linenr_T lnum; | |
5525 { | |
5526 signlist_T *sign; /* a sign in the signlist */ | |
5527 int count = 0; | |
5528 | |
5529 for (sign = buf->b_signlist; sign != NULL; sign = sign->next) | |
5530 if (sign->lnum == lnum) | |
5531 if (sign_get_image(sign->typenr) != NULL) | |
5532 count++; | |
5533 | |
5534 return count; | |
5535 } | |
5536 # endif /* FEAT_SIGN_ICONS */ | |
5537 # endif /* FEAT_NETBEANS_INTG */ | |
5538 | |
5539 | |
5540 /* | |
5541 * Delete signs in buffer "buf". | |
5542 */ | |
5543 static void | |
5544 buf_delete_signs(buf) | |
5545 buf_T *buf; | |
5546 { | |
5547 signlist_T *next; | |
5548 | |
5549 while (buf->b_signlist != NULL) | |
5550 { | |
5551 next = buf->b_signlist->next; | |
5552 vim_free(buf->b_signlist); | |
5553 buf->b_signlist = next; | |
5554 } | |
5555 } | |
5556 | |
5557 /* | |
5558 * Delete all signs in all buffers. | |
5559 */ | |
5560 void | |
5561 buf_delete_all_signs() | |
5562 { | |
5563 buf_T *buf; /* buffer we are checking for signs */ | |
5564 | |
5565 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
5566 if (buf->b_signlist != NULL) | |
5567 { | |
5568 /* Need to redraw the windows to remove the sign column. */ | |
5569 redraw_buf_later(buf, NOT_VALID); | |
5570 buf_delete_signs(buf); | |
5571 } | |
5572 } | |
5573 | |
5574 /* | |
5575 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers. | |
5576 */ | |
5577 void | |
5578 sign_list_placed(rbuf) | |
5579 buf_T *rbuf; | |
5580 { | |
5581 buf_T *buf; | |
5582 signlist_T *p; | |
5583 char lbuf[BUFSIZ]; | |
5584 | |
5585 MSG_PUTS_TITLE(_("\n--- Signs ---")); | |
5586 msg_putchar('\n'); | |
5587 if (rbuf == NULL) | |
5588 buf = firstbuf; | |
5589 else | |
5590 buf = rbuf; | |
3411 | 5591 while (buf != NULL && !got_int) |
7 | 5592 { |
5593 if (buf->b_signlist != NULL) | |
5594 { | |
272 | 5595 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname); |
7 | 5596 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D)); |
5597 msg_putchar('\n'); | |
5598 } | |
3411 | 5599 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next) |
7 | 5600 { |
272 | 5601 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"), |
7 | 5602 (long)p->lnum, p->id, sign_typenr2name(p->typenr)); |
5603 MSG_PUTS(lbuf); | |
5604 msg_putchar('\n'); | |
5605 } | |
5606 if (rbuf != NULL) | |
5607 break; | |
5608 buf = buf->b_next; | |
5609 } | |
5610 } | |
5611 | |
5612 /* | |
5613 * Adjust a placed sign for inserted/deleted lines. | |
5614 */ | |
5615 void | |
5616 sign_mark_adjust(line1, line2, amount, amount_after) | |
5617 linenr_T line1; | |
5618 linenr_T line2; | |
5619 long amount; | |
5620 long amount_after; | |
5621 { | |
5622 signlist_T *sign; /* a sign in a b_signlist */ | |
5623 | |
5624 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next) | |
5625 { | |
5626 if (sign->lnum >= line1 && sign->lnum <= line2) | |
5627 { | |
5628 if (amount == MAXLNUM) | |
5629 sign->lnum = line1; | |
5630 else | |
5631 sign->lnum += amount; | |
5632 } | |
5633 else if (sign->lnum > line2) | |
5634 sign->lnum += amount_after; | |
5635 } | |
5636 } | |
5637 #endif /* FEAT_SIGNS */ | |
5638 | |
5639 /* | |
5640 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed. | |
5641 */ | |
5642 void | |
5643 set_buflisted(on) | |
5644 int on; | |
5645 { | |
5646 if (on != curbuf->b_p_bl) | |
5647 { | |
5648 curbuf->b_p_bl = on; | |
5649 #ifdef FEAT_AUTOCMD | |
5650 if (on) | |
5651 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf); | |
5652 else | |
5653 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf); | |
5654 #endif | |
5655 } | |
5656 } | |
5657 | |
5658 /* | |
5659 * Read the file for "buf" again and check if the contents changed. | |
5660 * Return TRUE if it changed or this could not be checked. | |
5661 */ | |
5662 int | |
5663 buf_contents_changed(buf) | |
5664 buf_T *buf; | |
5665 { | |
5666 buf_T *newbuf; | |
5667 int differ = TRUE; | |
5668 linenr_T lnum; | |
5669 aco_save_T aco; | |
5670 exarg_T ea; | |
5671 | |
5672 /* Allocate a buffer without putting it in the buffer list. */ | |
5673 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY); | |
5674 if (newbuf == NULL) | |
5675 return TRUE; | |
5676 | |
5677 /* Force the 'fileencoding' and 'fileformat' to be equal. */ | |
5678 if (prep_exarg(&ea, buf) == FAIL) | |
5679 { | |
5680 wipe_buffer(newbuf, FALSE); | |
5681 return TRUE; | |
5682 } | |
5683 | |
5684 /* set curwin/curbuf to buf and save a few things */ | |
5685 aucmd_prepbuf(&aco, newbuf); | |
5686 | |
625 | 5687 if (ml_open(curbuf) == OK |
7 | 5688 && readfile(buf->b_ffname, buf->b_fname, |
5689 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, | |
5690 &ea, READ_NEW | READ_DUMMY) == OK) | |
5691 { | |
5692 /* compare the two files line by line */ | |
5693 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count) | |
5694 { | |
5695 differ = FALSE; | |
5696 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum) | |
5697 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0) | |
5698 { | |
5699 differ = TRUE; | |
5700 break; | |
5701 } | |
5702 } | |
5703 } | |
5704 vim_free(ea.cmd); | |
5705 | |
5706 /* restore curwin/curbuf and a few other things */ | |
5707 aucmd_restbuf(&aco); | |
5708 | |
5709 if (curbuf != newbuf) /* safety check */ | |
5710 wipe_buffer(newbuf, FALSE); | |
5711 | |
5712 return differ; | |
5713 } | |
5714 | |
5715 /* | |
5716 * Wipe out a buffer and decrement the last buffer number if it was used for | |
5717 * this buffer. Call this to wipe out a temp buffer that does not contain any | |
5718 * marks. | |
5719 */ | |
5720 void | |
5721 wipe_buffer(buf, aucmd) | |
5722 buf_T *buf; | |
1876 | 5723 int aucmd UNUSED; /* When TRUE trigger autocommands. */ |
7 | 5724 { |
5725 if (buf->b_fnum == top_file_num - 1) | |
5726 --top_file_num; | |
5727 | |
5728 #ifdef FEAT_AUTOCMD | |
5729 if (!aucmd) /* Don't trigger BufDelete autocommands here. */ | |
1410 | 5730 block_autocmds(); |
7 | 5731 #endif |
3365 | 5732 close_buffer(NULL, buf, DOBUF_WIPE, FALSE); |
7 | 5733 #ifdef FEAT_AUTOCMD |
5734 if (!aucmd) | |
1410 | 5735 unblock_autocmds(); |
7 | 5736 #endif |
5737 } |