Fix "Special Characters" palette bug
[MacVim.git] / src / buffer.c
blob53d593ce1c41fdf5165257bd3c01534755c4d2ed
1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
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 */
11 * buffer.c: functions for dealing with the buffer structure
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
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.
25 * The current implementation remembers all file names ever used.
28 #include "vim.h"
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));
36 static wininfo_T *find_wininfo __ARGS((buf_T *buf));
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
47 static void free_buffer __ARGS((buf_T *));
48 static void free_buffer_stuff __ARGS((buf_T *buf, int free_options));
49 static void clear_wininfo __ARGS((buf_T *buf));
51 #ifdef UNIX
52 # define dev_T dev_t
53 #else
54 # define dev_T unsigned
55 #endif
57 #if defined(FEAT_SIGNS)
58 static void insert_sign __ARGS((buf_T *buf, signlist_T *prev, signlist_T *next, int id, linenr_T lnum, int typenr));
59 static void buf_delete_signs __ARGS((buf_T *buf));
60 #endif
63 * Open current buffer, that is: open the memfile and read the file into memory
64 * return FAIL for failure, OK otherwise
66 int
67 open_buffer(read_stdin, eap)
68 int read_stdin; /* read file from stdin */
69 exarg_T *eap; /* for forced 'ff' and 'fenc' or NULL */
71 int retval = OK;
72 #ifdef FEAT_AUTOCMD
73 buf_T *old_curbuf;
74 #endif
77 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
78 * When re-entering the same buffer, it should not change, because the
79 * user may have reset the flag by hand.
81 if (readonlymode && curbuf->b_ffname != NULL
82 && (curbuf->b_flags & BF_NEVERLOADED))
83 curbuf->b_p_ro = TRUE;
85 if (ml_open(curbuf) == FAIL)
88 * There MUST be a memfile, otherwise we can't do anything
89 * If we can't create one for the current buffer, take another buffer
91 close_buffer(NULL, curbuf, 0);
92 for (curbuf = firstbuf; curbuf != NULL; curbuf = curbuf->b_next)
93 if (curbuf->b_ml.ml_mfp != NULL)
94 break;
96 * if there is no memfile at all, exit
97 * This is OK, since there are no changes to lose.
99 if (curbuf == NULL)
101 EMSG(_("E82: Cannot allocate any buffer, exiting..."));
102 getout(2);
104 EMSG(_("E83: Cannot allocate buffer, using other one..."));
105 enter_buffer(curbuf);
106 return FAIL;
109 #ifdef FEAT_AUTOCMD
110 /* The autocommands in readfile() may change the buffer, but only AFTER
111 * reading the file. */
112 old_curbuf = curbuf;
113 modified_was_set = FALSE;
114 #endif
116 /* mark cursor position as being invalid */
117 changed_line_abv_curs();
119 if (curbuf->b_ffname != NULL
120 #ifdef FEAT_NETBEANS_INTG
121 && netbeansReadFile
122 #endif
125 #ifdef FEAT_NETBEANS_INTG
126 int oldFire = netbeansFireChanges;
128 netbeansFireChanges = 0;
129 #endif
130 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
131 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap, READ_NEW);
132 #ifdef FEAT_NETBEANS_INTG
133 netbeansFireChanges = oldFire;
134 #endif
135 /* Help buffer is filtered. */
136 if (curbuf->b_help)
137 fix_help_buffer();
139 else if (read_stdin)
141 int save_bin = curbuf->b_p_bin;
142 linenr_T line_count;
145 * First read the text in binary mode into the buffer.
146 * Then read from that same buffer and append at the end. This makes
147 * it possible to retry when 'fileformat' or 'fileencoding' was
148 * guessed wrong.
150 curbuf->b_p_bin = TRUE;
151 retval = readfile(NULL, NULL, (linenr_T)0,
152 (linenr_T)0, (linenr_T)MAXLNUM, NULL, READ_NEW + READ_STDIN);
153 curbuf->b_p_bin = save_bin;
154 if (retval == OK)
156 line_count = curbuf->b_ml.ml_line_count;
157 retval = readfile(NULL, NULL, (linenr_T)line_count,
158 (linenr_T)0, (linenr_T)MAXLNUM, eap, READ_BUFFER);
159 if (retval == OK)
161 /* Delete the binary lines. */
162 while (--line_count >= 0)
163 ml_delete((linenr_T)1, FALSE);
165 else
167 /* Delete the converted lines. */
168 while (curbuf->b_ml.ml_line_count > line_count)
169 ml_delete(line_count, FALSE);
171 /* Put the cursor on the first line. */
172 curwin->w_cursor.lnum = 1;
173 curwin->w_cursor.col = 0;
175 /* Set or reset 'modified' before executing autocommands, so that
176 * it can be changed there. */
177 if (!readonlymode && !bufempty())
178 changed();
179 else if (retval != FAIL)
180 unchanged(curbuf, FALSE);
181 #ifdef FEAT_AUTOCMD
182 # ifdef FEAT_EVAL
183 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
184 curbuf, &retval);
185 # else
186 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
187 # endif
188 #endif
192 /* if first time loading this buffer, init b_chartab[] */
193 if (curbuf->b_flags & BF_NEVERLOADED)
194 (void)buf_init_chartab(curbuf, FALSE);
197 * Set/reset the Changed flag first, autocmds may change the buffer.
198 * Apply the automatic commands, before processing the modelines.
199 * So the modelines have priority over auto commands.
201 /* When reading stdin, the buffer contents always needs writing, so set
202 * the changed flag. Unless in readonly mode: "ls | gview -".
203 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
204 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
205 #ifdef FEAT_AUTOCMD
206 || modified_was_set /* ":set modified" used in autocmd */
207 # ifdef FEAT_EVAL
208 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
209 # endif
210 #endif
212 changed();
213 else if (retval != FAIL && !read_stdin)
214 unchanged(curbuf, FALSE);
215 save_file_ff(curbuf); /* keep this fileformat */
217 /* require "!" to overwrite the file, because it wasn't read completely */
218 #ifdef FEAT_EVAL
219 if (aborting())
220 #else
221 if (got_int)
222 #endif
223 curbuf->b_flags |= BF_READERR;
225 #ifdef FEAT_FOLDING
226 /* Need to update automatic folding. Do this before the autocommands,
227 * they may use the fold info. */
228 foldUpdateAll(curwin);
229 #endif
231 #ifdef FEAT_AUTOCMD
232 /* need to set w_topline, unless some autocommand already did that. */
233 if (!(curwin->w_valid & VALID_TOPLINE))
235 curwin->w_topline = 1;
236 # ifdef FEAT_DIFF
237 curwin->w_topfill = 0;
238 # endif
240 # ifdef FEAT_EVAL
241 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
242 # else
243 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
244 # endif
245 #endif
247 if (retval != FAIL)
249 #ifdef FEAT_AUTOCMD
251 * The autocommands may have changed the current buffer. Apply the
252 * modelines to the correct buffer, if it still exists and is loaded.
254 if (buf_valid(old_curbuf) && old_curbuf->b_ml.ml_mfp != NULL)
256 aco_save_T aco;
258 /* Go to the buffer that was opened. */
259 aucmd_prepbuf(&aco, old_curbuf);
260 #endif
261 do_modelines(0);
262 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
264 #ifdef FEAT_AUTOCMD
265 # ifdef FEAT_EVAL
266 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
267 &retval);
268 # else
269 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
270 # endif
272 /* restore curwin/curbuf and a few other things */
273 aucmd_restbuf(&aco);
275 #endif
278 return retval;
282 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
285 buf_valid(buf)
286 buf_T *buf;
288 buf_T *bp;
290 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
291 if (bp == buf)
292 return TRUE;
293 return FALSE;
297 * Close the link to a buffer.
298 * "action" is used when there is no longer a window for the buffer.
299 * It can be:
300 * 0 buffer becomes hidden
301 * DOBUF_UNLOAD buffer is unloaded
302 * DOBUF_DELETE buffer is unloaded and removed from buffer list
303 * DOBUF_WIPE buffer is unloaded and really deleted
304 * When doing all but the first one on the current buffer, the caller should
305 * get a new buffer very soon!
307 * The 'bufhidden' option can force freeing and deleting.
309 void
310 close_buffer(win, buf, action)
311 win_T *win; /* if not NULL, set b_last_cursor */
312 buf_T *buf;
313 int action;
315 #ifdef FEAT_AUTOCMD
316 int is_curbuf;
317 int nwindows = buf->b_nwindows;
318 #endif
319 int unload_buf = (action != 0);
320 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
321 int wipe_buf = (action == DOBUF_WIPE);
323 #ifdef FEAT_QUICKFIX
325 * Force unloading or deleting when 'bufhidden' says so.
326 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
327 * "hide" (otherwise we could never free or delete a buffer).
329 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
331 del_buf = TRUE;
332 unload_buf = TRUE;
334 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
336 del_buf = TRUE;
337 unload_buf = TRUE;
338 wipe_buf = TRUE;
340 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
341 unload_buf = TRUE;
342 #endif
344 if (win != NULL)
346 /* Set b_last_cursor when closing the last window for the buffer.
347 * Remember the last cursor position and window options of the buffer.
348 * This used to be only for the current window, but then options like
349 * 'foldmethod' may be lost with a ":only" command. */
350 if (buf->b_nwindows == 1)
351 set_last_cursor(win);
352 buflist_setfpos(buf, win,
353 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
354 win->w_cursor.col, TRUE);
357 #ifdef FEAT_AUTOCMD
358 /* When the buffer is no longer in a window, trigger BufWinLeave */
359 if (buf->b_nwindows == 1)
361 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
362 FALSE, buf);
363 if (!buf_valid(buf)) /* autocommands may delete the buffer */
364 return;
366 /* When the buffer becomes hidden, but is not unloaded, trigger
367 * BufHidden */
368 if (!unload_buf)
370 apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
371 FALSE, buf);
372 if (!buf_valid(buf)) /* autocmds may delete the buffer */
373 return;
375 # ifdef FEAT_EVAL
376 if (aborting()) /* autocmds may abort script processing */
377 return;
378 # endif
380 nwindows = buf->b_nwindows;
381 #endif
383 /* decrease the link count from windows (unless not in any window) */
384 if (buf->b_nwindows > 0)
385 --buf->b_nwindows;
387 /* Return when a window is displaying the buffer or when it's not
388 * unloaded. */
389 if (buf->b_nwindows > 0 || !unload_buf)
391 #if 0 /* why was this here? */
392 if (buf == curbuf)
393 u_sync(); /* sync undo before going to another buffer */
394 #endif
395 return;
398 /* Always remove the buffer when there is no file name. */
399 if (buf->b_ffname == NULL)
400 del_buf = TRUE;
403 * Free all things allocated for this buffer.
404 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
406 #ifdef FEAT_AUTOCMD
407 /* Remember if we are closing the current buffer. Restore the number of
408 * windows, so that autocommands in buf_freeall() don't get confused. */
409 is_curbuf = (buf == curbuf);
410 buf->b_nwindows = nwindows;
411 #endif
413 buf_freeall(buf, del_buf, wipe_buf);
415 #ifdef FEAT_AUTOCMD
416 /* Autocommands may have deleted the buffer. */
417 if (!buf_valid(buf))
418 return;
419 # ifdef FEAT_EVAL
420 if (aborting()) /* autocmds may abort script processing */
421 return;
422 # endif
424 /* Autocommands may have opened or closed windows for this buffer.
425 * Decrement the count for the close we do here. */
426 if (buf->b_nwindows > 0)
427 --buf->b_nwindows;
430 * It's possible that autocommands change curbuf to the one being deleted.
431 * This might cause the previous curbuf to be deleted unexpectedly. But
432 * in some cases it's OK to delete the curbuf, because a new one is
433 * obtained anyway. Therefore only return if curbuf changed to the
434 * deleted buffer.
436 if (buf == curbuf && !is_curbuf)
437 return;
438 #endif
440 #ifdef FEAT_NETBEANS_INTG
441 if (usingNetbeans)
442 netbeans_file_closed(buf);
443 #endif
444 #ifdef FEAT_ODB_EDITOR
445 odb_buffer_close(buf);
446 #endif
448 /* Change directories when the 'acd' option is set. */
449 DO_AUTOCHDIR
452 * Remove the buffer from the list.
454 if (wipe_buf)
456 #ifdef FEAT_SUN_WORKSHOP
457 if (usingSunWorkShop)
458 workshop_file_closed_lineno((char *)buf->b_ffname,
459 (int)buf->b_last_cursor.lnum);
460 #endif
461 vim_free(buf->b_ffname);
462 vim_free(buf->b_sfname);
463 if (buf->b_prev == NULL)
464 firstbuf = buf->b_next;
465 else
466 buf->b_prev->b_next = buf->b_next;
467 if (buf->b_next == NULL)
468 lastbuf = buf->b_prev;
469 else
470 buf->b_next->b_prev = buf->b_prev;
471 free_buffer(buf);
473 else
475 if (del_buf)
477 /* Free all internal variables and reset option values, to make
478 * ":bdel" compatible with Vim 5.7. */
479 free_buffer_stuff(buf, TRUE);
481 /* Make it look like a new buffer. */
482 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
484 /* Init the options when loaded again. */
485 buf->b_p_initialized = FALSE;
487 buf_clear_file(buf);
488 if (del_buf)
489 buf->b_p_bl = FALSE;
494 * Make buffer not contain a file.
496 void
497 buf_clear_file(buf)
498 buf_T *buf;
500 buf->b_ml.ml_line_count = 1;
501 unchanged(buf, TRUE);
502 #ifndef SHORT_FNAME
503 buf->b_shortname = FALSE;
504 #endif
505 buf->b_p_eol = TRUE;
506 buf->b_start_eol = TRUE;
507 #ifdef FEAT_MBYTE
508 buf->b_p_bomb = FALSE;
509 buf->b_start_bomb = FALSE;
510 #endif
511 buf->b_ml.ml_mfp = NULL;
512 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
513 #ifdef FEAT_NETBEANS_INTG
514 netbeans_deleted_all_lines(buf);
515 #endif
519 * buf_freeall() - free all things allocated for a buffer that are related to
520 * the file.
522 /*ARGSUSED*/
523 void
524 buf_freeall(buf, del_buf, wipe_buf)
525 buf_T *buf;
526 int del_buf; /* buffer is going to be deleted */
527 int wipe_buf; /* buffer is going to be wiped out */
529 #ifdef FEAT_AUTOCMD
530 int is_curbuf = (buf == curbuf);
532 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, FALSE, buf);
533 if (!buf_valid(buf)) /* autocommands may delete the buffer */
534 return;
535 if (del_buf && buf->b_p_bl)
537 apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname, FALSE, buf);
538 if (!buf_valid(buf)) /* autocommands may delete the buffer */
539 return;
541 if (wipe_buf)
543 apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
544 FALSE, buf);
545 if (!buf_valid(buf)) /* autocommands may delete the buffer */
546 return;
548 # ifdef FEAT_EVAL
549 if (aborting()) /* autocmds may abort script processing */
550 return;
551 # endif
554 * It's possible that autocommands change curbuf to the one being deleted.
555 * This might cause curbuf to be deleted unexpectedly. But in some cases
556 * it's OK to delete the curbuf, because a new one is obtained anyway.
557 * Therefore only return if curbuf changed to the deleted buffer.
559 if (buf == curbuf && !is_curbuf)
560 return;
561 #endif
562 #ifdef FEAT_DIFF
563 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
564 #endif
566 #ifdef FEAT_FOLDING
567 /* No folds in an empty buffer. */
568 # ifdef FEAT_WINDOWS
570 win_T *win;
571 tabpage_T *tp;
573 FOR_ALL_TAB_WINDOWS(tp, win)
574 if (win->w_buffer == buf)
575 clearFolding(win);
577 # else
578 if (curwin->w_buffer == buf)
579 clearFolding(curwin);
580 # endif
581 #endif
583 #ifdef FEAT_TCL
584 tcl_buffer_free(buf);
585 #endif
586 u_blockfree(buf); /* free the memory allocated for undo */
587 ml_close(buf, TRUE); /* close and delete the memline/memfile */
588 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
589 u_clearall(buf); /* reset all undo information */
590 #ifdef FEAT_SYN_HL
591 syntax_clear(buf); /* reset syntax info */
592 #endif
593 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
597 * Free a buffer structure and the things it contains related to the buffer
598 * itself (not the file, that must have been done already).
600 static void
601 free_buffer(buf)
602 buf_T *buf;
604 free_buffer_stuff(buf, TRUE);
605 #ifdef FEAT_MZSCHEME
606 mzscheme_buffer_free(buf);
607 #endif
608 #ifdef FEAT_PERL
609 perl_buf_free(buf);
610 #endif
611 #ifdef FEAT_PYTHON
612 python_buffer_free(buf);
613 #endif
614 #ifdef FEAT_RUBY
615 ruby_buffer_free(buf);
616 #endif
617 #ifdef FEAT_AUTOCMD
618 aubuflocal_remove(buf);
619 #endif
620 vim_free(buf);
624 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
626 static void
627 free_buffer_stuff(buf, free_options)
628 buf_T *buf;
629 int free_options; /* free options as well */
631 if (free_options)
633 clear_wininfo(buf); /* including window-local options */
634 free_buf_options(buf, TRUE);
636 #ifdef FEAT_EVAL
637 vars_clear(&buf->b_vars.dv_hashtab); /* free all internal variables */
638 hash_init(&buf->b_vars.dv_hashtab);
639 #endif
640 #ifdef FEAT_USR_CMDS
641 uc_clear(&buf->b_ucmds); /* clear local user commands */
642 #endif
643 #ifdef FEAT_SIGNS
644 buf_delete_signs(buf); /* delete any signs */
645 #endif
646 #ifdef FEAT_LOCALMAP
647 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
648 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
649 #endif
650 #ifdef FEAT_MBYTE
651 vim_free(buf->b_start_fenc);
652 buf->b_start_fenc = NULL;
653 #endif
657 * Free the b_wininfo list for buffer "buf".
659 static void
660 clear_wininfo(buf)
661 buf_T *buf;
663 wininfo_T *wip;
665 while (buf->b_wininfo != NULL)
667 wip = buf->b_wininfo;
668 buf->b_wininfo = wip->wi_next;
669 if (wip->wi_optset)
671 clear_winopt(&wip->wi_opt);
672 #ifdef FEAT_FOLDING
673 deleteFoldRecurse(&wip->wi_folds);
674 #endif
676 vim_free(wip);
680 #if defined(FEAT_LISTCMDS) || defined(PROTO)
682 * Go to another buffer. Handles the result of the ATTENTION dialog.
684 void
685 goto_buffer(eap, start, dir, count)
686 exarg_T *eap;
687 int start;
688 int dir;
689 int count;
691 # if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
692 buf_T *old_curbuf = curbuf;
694 swap_exists_action = SEA_DIALOG;
695 # endif
696 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
697 start, dir, count, eap->forceit);
698 # if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
699 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
701 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
702 cleanup_T cs;
704 /* Reset the error/interrupt/exception state here so that
705 * aborting() returns FALSE when closing a window. */
706 enter_cleanup(&cs);
707 # endif
709 /* Quitting means closing the split window, nothing else. */
710 win_close(curwin, TRUE);
711 swap_exists_action = SEA_NONE;
712 swap_exists_did_quit = TRUE;
714 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
715 /* Restore the error/interrupt/exception state if not discarded by a
716 * new aborting error, interrupt, or uncaught exception. */
717 leave_cleanup(&cs);
718 # endif
720 else
721 handle_swap_exists(old_curbuf);
722 # endif
724 #endif
726 #if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
728 * Handle the situation of swap_exists_action being set.
729 * It is allowed for "old_curbuf" to be NULL or invalid.
731 void
732 handle_swap_exists(old_curbuf)
733 buf_T *old_curbuf;
735 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
736 cleanup_T cs;
737 # endif
739 if (swap_exists_action == SEA_QUIT)
741 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
742 /* Reset the error/interrupt/exception state here so that
743 * aborting() returns FALSE when closing a buffer. */
744 enter_cleanup(&cs);
745 # endif
747 /* User selected Quit at ATTENTION prompt. Go back to previous
748 * buffer. If that buffer is gone or the same as the current one,
749 * open a new, empty buffer. */
750 swap_exists_action = SEA_NONE; /* don't want it again */
751 swap_exists_did_quit = TRUE;
752 close_buffer(curwin, curbuf, DOBUF_UNLOAD);
753 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
754 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
755 if (old_curbuf != NULL)
756 enter_buffer(old_curbuf);
757 /* If "old_curbuf" is NULL we are in big trouble here... */
759 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
760 /* Restore the error/interrupt/exception state if not discarded by a
761 * new aborting error, interrupt, or uncaught exception. */
762 leave_cleanup(&cs);
763 # endif
765 else if (swap_exists_action == SEA_RECOVER)
767 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
768 /* Reset the error/interrupt/exception state here so that
769 * aborting() returns FALSE when closing a buffer. */
770 enter_cleanup(&cs);
771 # endif
773 /* User selected Recover at ATTENTION prompt. */
774 msg_scroll = TRUE;
775 ml_recover();
776 MSG_PUTS("\n"); /* don't overwrite the last message */
777 cmdline_row = msg_row;
778 do_modelines(0);
780 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
781 /* Restore the error/interrupt/exception state if not discarded by a
782 * new aborting error, interrupt, or uncaught exception. */
783 leave_cleanup(&cs);
784 # endif
786 swap_exists_action = SEA_NONE;
788 #endif
790 #if defined(FEAT_LISTCMDS) || defined(PROTO)
792 * do_bufdel() - delete or unload buffer(s)
794 * addr_count == 0: ":bdel" - delete current buffer
795 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
796 * buffer "end_bnr", then any other arguments.
797 * addr_count == 2: ":N,N bdel" - delete buffers in range
799 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
800 * DOBUF_DEL (":bdel")
802 * Returns error message or NULL
804 char_u *
805 do_bufdel(command, arg, addr_count, start_bnr, end_bnr, forceit)
806 int command;
807 char_u *arg; /* pointer to extra arguments */
808 int addr_count;
809 int start_bnr; /* first buffer number in a range */
810 int end_bnr; /* buffer nr or last buffer nr in a range */
811 int forceit;
813 int do_current = 0; /* delete current buffer? */
814 int deleted = 0; /* number of buffers deleted */
815 char_u *errormsg = NULL; /* return value */
816 int bnr; /* buffer number */
817 char_u *p;
819 #ifdef FEAT_NETBEANS_INTG
820 netbeansCloseFile = 1;
821 #endif
822 if (addr_count == 0)
824 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
826 else
828 if (addr_count == 2)
830 if (*arg) /* both range and argument is not allowed */
831 return (char_u *)_(e_trailing);
832 bnr = start_bnr;
834 else /* addr_count == 1 */
835 bnr = end_bnr;
837 for ( ;!got_int; ui_breakcheck())
840 * delete the current buffer last, otherwise when the
841 * current buffer is deleted, the next buffer becomes
842 * the current one and will be loaded, which may then
843 * also be deleted, etc.
845 if (bnr == curbuf->b_fnum)
846 do_current = bnr;
847 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
848 forceit) == OK)
849 ++deleted;
852 * find next buffer number to delete/unload
854 if (addr_count == 2)
856 if (++bnr > end_bnr)
857 break;
859 else /* addr_count == 1 */
861 arg = skipwhite(arg);
862 if (*arg == NUL)
863 break;
864 if (!VIM_ISDIGIT(*arg))
866 p = skiptowhite_esc(arg);
867 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE, FALSE);
868 if (bnr < 0) /* failed */
869 break;
870 arg = p;
872 else
873 bnr = getdigits(&arg);
876 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
877 FORWARD, do_current, forceit) == OK)
878 ++deleted;
880 if (deleted == 0)
882 if (command == DOBUF_UNLOAD)
883 STRCPY(IObuff, _("E515: No buffers were unloaded"));
884 else if (command == DOBUF_DEL)
885 STRCPY(IObuff, _("E516: No buffers were deleted"));
886 else
887 STRCPY(IObuff, _("E517: No buffers were wiped out"));
888 errormsg = IObuff;
890 else if (deleted >= p_report)
892 if (command == DOBUF_UNLOAD)
894 if (deleted == 1)
895 MSG(_("1 buffer unloaded"));
896 else
897 smsg((char_u *)_("%d buffers unloaded"), deleted);
899 else if (command == DOBUF_DEL)
901 if (deleted == 1)
902 MSG(_("1 buffer deleted"));
903 else
904 smsg((char_u *)_("%d buffers deleted"), deleted);
906 else
908 if (deleted == 1)
909 MSG(_("1 buffer wiped out"));
910 else
911 smsg((char_u *)_("%d buffers wiped out"), deleted);
916 #ifdef FEAT_NETBEANS_INTG
917 netbeansCloseFile = 0;
918 #endif
920 return errormsg;
924 * Implementation of the commands for the buffer list.
926 * action == DOBUF_GOTO go to specified buffer
927 * action == DOBUF_SPLIT split window and go to specified buffer
928 * action == DOBUF_UNLOAD unload specified buffer(s)
929 * action == DOBUF_DEL delete specified buffer(s) from buffer list
930 * action == DOBUF_WIPE delete specified buffer(s) really
932 * start == DOBUF_CURRENT go to "count" buffer from current buffer
933 * start == DOBUF_FIRST go to "count" buffer from first buffer
934 * start == DOBUF_LAST go to "count" buffer from last buffer
935 * start == DOBUF_MOD go to "count" modified buffer from current buffer
937 * Return FAIL or OK.
940 do_buffer(action, start, dir, count, forceit)
941 int action;
942 int start;
943 int dir; /* FORWARD or BACKWARD */
944 int count; /* buffer number or number of buffers */
945 int forceit; /* TRUE for :...! */
947 buf_T *buf;
948 buf_T *bp;
949 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
950 || action == DOBUF_WIPE);
952 switch (start)
954 case DOBUF_FIRST: buf = firstbuf; break;
955 case DOBUF_LAST: buf = lastbuf; break;
956 default: buf = curbuf; break;
958 if (start == DOBUF_MOD) /* find next modified buffer */
960 while (count-- > 0)
964 buf = buf->b_next;
965 if (buf == NULL)
966 buf = firstbuf;
968 while (buf != curbuf && !bufIsChanged(buf));
970 if (!bufIsChanged(buf))
972 EMSG(_("E84: No modified buffer found"));
973 return FAIL;
976 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
978 while (buf != NULL && buf->b_fnum != count)
979 buf = buf->b_next;
981 else
983 bp = NULL;
984 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
986 /* remember the buffer where we start, we come back there when all
987 * buffers are unlisted. */
988 if (bp == NULL)
989 bp = buf;
990 if (dir == FORWARD)
992 buf = buf->b_next;
993 if (buf == NULL)
994 buf = firstbuf;
996 else
998 buf = buf->b_prev;
999 if (buf == NULL)
1000 buf = lastbuf;
1002 /* don't count unlisted buffers */
1003 if (unload || buf->b_p_bl)
1005 --count;
1006 bp = NULL; /* use this buffer as new starting point */
1008 if (bp == buf)
1010 /* back where we started, didn't find anything. */
1011 EMSG(_("E85: There is no listed buffer"));
1012 return FAIL;
1017 if (buf == NULL) /* could not find it */
1019 if (start == DOBUF_FIRST)
1021 /* don't warn when deleting */
1022 if (!unload)
1023 EMSGN(_("E86: Buffer %ld does not exist"), count);
1025 else if (dir == FORWARD)
1026 EMSG(_("E87: Cannot go beyond last buffer"));
1027 else
1028 EMSG(_("E88: Cannot go before first buffer"));
1029 return FAIL;
1032 #ifdef FEAT_GUI
1033 need_mouse_correct = TRUE;
1034 #endif
1036 #ifdef FEAT_LISTCMDS
1038 * delete buffer buf from memory and/or the list
1040 if (unload)
1042 int forward;
1043 int retval;
1045 /* When unloading or deleting a buffer that's already unloaded and
1046 * unlisted: fail silently. */
1047 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1048 return FAIL;
1050 if (!forceit && bufIsChanged(buf))
1052 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1053 if ((p_confirm || cmdmod.confirm) && p_write)
1055 dialog_changed(buf, FALSE);
1056 # ifdef FEAT_AUTOCMD
1057 if (!buf_valid(buf))
1058 /* Autocommand deleted buffer, oops! It's not changed
1059 * now. */
1060 return FAIL;
1061 # endif
1062 /* If it's still changed fail silently, the dialog already
1063 * mentioned why it fails. */
1064 if (bufIsChanged(buf))
1065 return FAIL;
1067 else
1068 #endif
1070 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1071 buf->b_fnum);
1072 return FAIL;
1077 * If deleting the last (listed) buffer, make it empty.
1078 * The last (listed) buffer cannot be unloaded.
1080 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1081 if (bp->b_p_bl && bp != buf)
1082 break;
1083 if (bp == NULL && buf == curbuf)
1085 if (action == DOBUF_UNLOAD)
1087 EMSG(_("E90: Cannot unload last buffer"));
1088 return FAIL;
1091 /* Close any other windows on this buffer, then make it empty. */
1092 #ifdef FEAT_WINDOWS
1093 close_windows(buf, TRUE);
1094 #endif
1095 setpcmark();
1096 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1097 forceit ? ECMD_FORCEIT : 0);
1100 * do_ecmd() may create a new buffer, then we have to delete
1101 * the old one. But do_ecmd() may have done that already, check
1102 * if the buffer still exists.
1104 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
1105 close_buffer(NULL, buf, action);
1106 return retval;
1109 #ifdef FEAT_WINDOWS
1111 * If the deleted buffer is the current one, close the current window
1112 * (unless it's the only window). Repeat this so long as we end up in
1113 * a window with this buffer.
1115 while (buf == curbuf
1116 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
1117 win_close(curwin, FALSE);
1118 #endif
1121 * If the buffer to be deleted is not the current one, delete it here.
1123 if (buf != curbuf)
1125 #ifdef FEAT_WINDOWS
1126 close_windows(buf, FALSE);
1127 #endif
1128 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
1129 close_buffer(NULL, buf, action);
1130 return OK;
1134 * Deleting the current buffer: Need to find another buffer to go to.
1135 * There must be another, otherwise it would have been handled above.
1136 * First use au_new_curbuf, if it is valid.
1137 * Then prefer the buffer we most recently visited.
1138 * Else try to find one that is loaded, after the current buffer,
1139 * then before the current buffer.
1140 * Finally use any buffer.
1142 buf = NULL; /* selected buffer */
1143 bp = NULL; /* used when no loaded buffer found */
1144 #ifdef FEAT_AUTOCMD
1145 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1146 buf = au_new_curbuf;
1147 # ifdef FEAT_JUMPLIST
1148 else
1149 # endif
1150 #endif
1151 #ifdef FEAT_JUMPLIST
1152 if (curwin->w_jumplistlen > 0)
1154 int jumpidx;
1156 jumpidx = curwin->w_jumplistidx - 1;
1157 if (jumpidx < 0)
1158 jumpidx = curwin->w_jumplistlen - 1;
1160 forward = jumpidx;
1161 while (jumpidx != curwin->w_jumplistidx)
1163 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1164 if (buf != NULL)
1166 if (buf == curbuf || !buf->b_p_bl)
1167 buf = NULL; /* skip current and unlisted bufs */
1168 else if (buf->b_ml.ml_mfp == NULL)
1170 /* skip unloaded buf, but may keep it for later */
1171 if (bp == NULL)
1172 bp = buf;
1173 buf = NULL;
1176 if (buf != NULL) /* found a valid buffer: stop searching */
1177 break;
1178 /* advance to older entry in jump list */
1179 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1180 break;
1181 if (--jumpidx < 0)
1182 jumpidx = curwin->w_jumplistlen - 1;
1183 if (jumpidx == forward) /* List exhausted for sure */
1184 break;
1187 #endif
1189 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1191 forward = TRUE;
1192 buf = curbuf->b_next;
1193 for (;;)
1195 if (buf == NULL)
1197 if (!forward) /* tried both directions */
1198 break;
1199 buf = curbuf->b_prev;
1200 forward = FALSE;
1201 continue;
1203 /* in non-help buffer, try to skip help buffers, and vv */
1204 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1206 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1207 break;
1208 if (bp == NULL) /* remember unloaded buf for later */
1209 bp = buf;
1211 if (forward)
1212 buf = buf->b_next;
1213 else
1214 buf = buf->b_prev;
1217 if (buf == NULL) /* No loaded buffer, use unloaded one */
1218 buf = bp;
1219 if (buf == NULL) /* No loaded buffer, find listed one */
1221 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1222 if (buf->b_p_bl && buf != curbuf)
1223 break;
1225 if (buf == NULL) /* Still no buffer, just take one */
1227 if (curbuf->b_next != NULL)
1228 buf = curbuf->b_next;
1229 else
1230 buf = curbuf->b_prev;
1235 * make buf current buffer
1237 if (action == DOBUF_SPLIT) /* split window first */
1239 # ifdef FEAT_WINDOWS
1240 /* If 'switchbuf' contains "useopen": jump to first window containing
1241 * "buf" if one exists */
1242 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
1243 return OK;
1244 /* If 'switchbuf' contians "usetab": jump to first window in any tab
1245 * page containing "buf" if one exists */
1246 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
1247 return OK;
1248 if (win_split(0, 0) == FAIL)
1249 # endif
1250 return FAIL;
1252 #endif
1254 /* go to current buffer - nothing to do */
1255 if (buf == curbuf)
1256 return OK;
1259 * Check if the current buffer may be abandoned.
1261 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1263 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1264 if ((p_confirm || cmdmod.confirm) && p_write)
1266 dialog_changed(curbuf, FALSE);
1267 # ifdef FEAT_AUTOCMD
1268 if (!buf_valid(buf))
1269 /* Autocommand deleted buffer, oops! */
1270 return FAIL;
1271 # endif
1273 if (bufIsChanged(curbuf))
1274 #endif
1276 EMSG(_(e_nowrtmsg));
1277 return FAIL;
1281 /* Go to the other buffer. */
1282 set_curbuf(buf, action);
1284 #if defined(FEAT_LISTCMDS) && defined(FEAT_SCROLLBIND)
1285 if (action == DOBUF_SPLIT)
1286 curwin->w_p_scb = FALSE; /* reset 'scrollbind' */
1287 #endif
1289 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1290 if (aborting()) /* autocmds may abort script processing */
1291 return FAIL;
1292 #endif
1294 return OK;
1297 #endif /* FEAT_LISTCMDS */
1300 * Set current buffer to "buf". Executes autocommands and closes current
1301 * buffer. "action" tells how to close the current buffer:
1302 * DOBUF_GOTO free or hide it
1303 * DOBUF_SPLIT nothing
1304 * DOBUF_UNLOAD unload it
1305 * DOBUF_DEL delete it
1306 * DOBUF_WIPE wipe it out
1308 void
1309 set_curbuf(buf, action)
1310 buf_T *buf;
1311 int action;
1313 buf_T *prevbuf;
1314 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1315 || action == DOBUF_WIPE);
1317 setpcmark();
1318 if (!cmdmod.keepalt)
1319 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
1320 buflist_altfpos(); /* remember curpos */
1322 #ifdef FEAT_VISUAL
1323 /* Don't restart Select mode after switching to another buffer. */
1324 VIsual_reselect = FALSE;
1325 #endif
1327 /* close_windows() or apply_autocmds() may change curbuf */
1328 prevbuf = curbuf;
1330 #ifdef FEAT_AUTOCMD
1331 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1332 # ifdef FEAT_EVAL
1333 if (buf_valid(prevbuf) && !aborting())
1334 # else
1335 if (buf_valid(prevbuf))
1336 # endif
1337 #endif
1339 #ifdef FEAT_WINDOWS
1340 if (unload)
1341 close_windows(prevbuf, FALSE);
1342 #endif
1343 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1344 if (buf_valid(prevbuf) && !aborting())
1345 #else
1346 if (buf_valid(prevbuf))
1347 #endif
1349 if (prevbuf == curbuf)
1350 u_sync(FALSE);
1351 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1352 unload ? action : (action == DOBUF_GOTO
1353 && !P_HID(prevbuf)
1354 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0);
1357 #ifdef FEAT_AUTOCMD
1358 /* An autocommand may have deleted "buf", already entered it (e.g., when
1359 * it did ":bunload") or aborted the script processing! */
1360 # ifdef FEAT_EVAL
1361 if (buf_valid(buf) && buf != curbuf && !aborting())
1362 # else
1363 if (buf_valid(buf) && buf != curbuf)
1364 # endif
1365 #endif
1366 enter_buffer(buf);
1370 * Enter a new current buffer.
1371 * Old curbuf must have been abandoned already!
1373 void
1374 enter_buffer(buf)
1375 buf_T *buf;
1377 /* Copy buffer and window local option values. Not for a help buffer. */
1378 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1379 if (!buf->b_help)
1380 get_winopts(buf);
1381 #ifdef FEAT_FOLDING
1382 else
1383 /* Remove all folds in the window. */
1384 clearFolding(curwin);
1385 foldUpdateAll(curwin); /* update folds (later). */
1386 #endif
1388 /* Get the buffer in the current window. */
1389 curwin->w_buffer = buf;
1390 curbuf = buf;
1391 ++curbuf->b_nwindows;
1393 #ifdef FEAT_DIFF
1394 if (curwin->w_p_diff)
1395 diff_buf_add(curbuf);
1396 #endif
1398 /* Cursor on first line by default. */
1399 curwin->w_cursor.lnum = 1;
1400 curwin->w_cursor.col = 0;
1401 #ifdef FEAT_VIRTUALEDIT
1402 curwin->w_cursor.coladd = 0;
1403 #endif
1404 curwin->w_set_curswant = TRUE;
1406 /* Make sure the buffer is loaded. */
1407 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
1409 #ifdef FEAT_AUTOCMD
1410 /* If there is no filetype, allow for detecting one. Esp. useful for
1411 * ":ball" used in a autocommand. If there already is a filetype we
1412 * might prefer to keep it. */
1413 if (*curbuf->b_p_ft == NUL)
1414 did_filetype = FALSE;
1415 #endif
1417 open_buffer(FALSE, NULL);
1419 else
1421 if (!msg_silent)
1422 need_fileinfo = TRUE; /* display file info after redraw */
1423 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1424 #ifdef FEAT_AUTOCMD
1425 curwin->w_topline = 1;
1426 # ifdef FEAT_DIFF
1427 curwin->w_topfill = 0;
1428 # endif
1429 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1430 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1431 #endif
1434 /* If autocommands did not change the cursor position, restore cursor lnum
1435 * and possibly cursor col. */
1436 if (curwin->w_cursor.lnum == 1 && inindent(0))
1437 buflist_getfpos();
1439 check_arg_idx(curwin); /* check for valid arg_idx */
1440 #ifdef FEAT_TITLE
1441 maketitle();
1442 #endif
1443 #ifdef FEAT_AUTOCMD
1444 if (curwin->w_topline == 1) /* when autocmds didn't change it */
1445 #endif
1446 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1448 #ifdef FEAT_NETBEANS_INTG
1449 /* Send fileOpened event because we've changed buffers. */
1450 if (usingNetbeans && isNetbeansBuffer(curbuf))
1451 netbeans_file_activated(curbuf);
1452 #endif
1454 /* Change directories when the 'acd' option is set. */
1455 DO_AUTOCHDIR
1457 #ifdef FEAT_KEYMAP
1458 if (curbuf->b_kmap_state & KEYMAP_INIT)
1459 keymap_init();
1460 #endif
1461 #ifdef FEAT_SPELL
1462 /* May need to set the spell language. Can only do this after the buffer
1463 * has been properly setup. */
1464 if (!curbuf->b_help && curwin->w_p_spell && *curbuf->b_p_spl != NUL)
1465 did_set_spelllang(curbuf);
1466 #endif
1468 redraw_later(NOT_VALID);
1471 #if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1473 * Change to the directory of the current buffer.
1475 void
1476 do_autochdir()
1478 if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK)
1479 shorten_fnames(TRUE);
1481 #endif
1484 * functions for dealing with the buffer list
1488 * Add a file name to the buffer list. Return a pointer to the buffer.
1489 * If the same file name already exists return a pointer to that buffer.
1490 * If it does not exist, or if fname == NULL, a new entry is created.
1491 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1492 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1493 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
1494 * This is the ONLY way to create a new buffer.
1496 static int top_file_num = 1; /* highest file number */
1498 buf_T *
1499 buflist_new(ffname, sfname, lnum, flags)
1500 char_u *ffname; /* full path of fname or relative */
1501 char_u *sfname; /* short fname or NULL */
1502 linenr_T lnum; /* preferred cursor line */
1503 int flags; /* BLN_ defines */
1505 buf_T *buf;
1506 #ifdef UNIX
1507 struct stat st;
1508 #endif
1510 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1513 * If file name already exists in the list, update the entry.
1515 #ifdef UNIX
1516 /* On Unix we can use inode numbers when the file exists. Works better
1517 * for hard links. */
1518 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1519 st.st_dev = (dev_T)-1;
1520 #endif
1521 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1522 #ifdef UNIX
1523 buflist_findname_stat(ffname, &st)
1524 #else
1525 buflist_findname(ffname)
1526 #endif
1527 ) != NULL)
1529 vim_free(ffname);
1530 if (lnum != 0)
1531 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1532 /* copy the options now, if 'cpo' doesn't have 's' and not done
1533 * already */
1534 buf_copy_options(buf, 0);
1535 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1537 buf->b_p_bl = TRUE;
1538 #ifdef FEAT_AUTOCMD
1539 if (!(flags & BLN_DUMMY))
1540 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1541 #endif
1543 return buf;
1547 * If the current buffer has no name and no contents, use the current
1548 * buffer. Otherwise: Need to allocate a new buffer structure.
1550 * This is the ONLY place where a new buffer structure is allocated!
1551 * (A spell file buffer is allocated in spell.c, but that's not a normal
1552 * buffer.)
1554 buf = NULL;
1555 if ((flags & BLN_CURBUF)
1556 && curbuf != NULL
1557 && curbuf->b_ffname == NULL
1558 && curbuf->b_nwindows <= 1
1559 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1561 buf = curbuf;
1562 #ifdef FEAT_AUTOCMD
1563 /* It's like this buffer is deleted. Watch out for autocommands that
1564 * change curbuf! If that happens, allocate a new buffer anyway. */
1565 if (curbuf->b_p_bl)
1566 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1567 if (buf == curbuf)
1568 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1569 # ifdef FEAT_EVAL
1570 if (aborting()) /* autocmds may abort script processing */
1571 return NULL;
1572 # endif
1573 #endif
1574 #ifdef FEAT_QUICKFIX
1575 # ifdef FEAT_AUTOCMD
1576 if (buf == curbuf)
1577 # endif
1579 /* Make sure 'bufhidden' and 'buftype' are empty */
1580 clear_string_option(&buf->b_p_bh);
1581 clear_string_option(&buf->b_p_bt);
1583 #endif
1585 if (buf != curbuf || curbuf == NULL)
1587 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1588 if (buf == NULL)
1590 vim_free(ffname);
1591 return NULL;
1595 if (ffname != NULL)
1597 buf->b_ffname = ffname;
1598 buf->b_sfname = vim_strsave(sfname);
1601 clear_wininfo(buf);
1602 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1604 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1605 || buf->b_wininfo == NULL)
1607 vim_free(buf->b_ffname);
1608 buf->b_ffname = NULL;
1609 vim_free(buf->b_sfname);
1610 buf->b_sfname = NULL;
1611 if (buf != curbuf)
1612 free_buffer(buf);
1613 return NULL;
1616 if (buf == curbuf)
1618 /* free all things allocated for this buffer */
1619 buf_freeall(buf, FALSE, FALSE);
1620 if (buf != curbuf) /* autocommands deleted the buffer! */
1621 return NULL;
1622 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1623 if (aborting()) /* autocmds may abort script processing */
1624 return NULL;
1625 #endif
1626 /* buf->b_nwindows = 0; why was this here? */
1627 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
1628 #ifdef FEAT_KEYMAP
1629 /* need to reload lmaps and set b:keymap_name */
1630 curbuf->b_kmap_state |= KEYMAP_INIT;
1631 #endif
1633 else
1636 * put new buffer at the end of the buffer list
1638 buf->b_next = NULL;
1639 if (firstbuf == NULL) /* buffer list is empty */
1641 buf->b_prev = NULL;
1642 firstbuf = buf;
1644 else /* append new buffer at end of list */
1646 lastbuf->b_next = buf;
1647 buf->b_prev = lastbuf;
1649 lastbuf = buf;
1651 buf->b_fnum = top_file_num++;
1652 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1654 EMSG(_("W14: Warning: List of file names overflow"));
1655 if (emsg_silent == 0)
1657 out_flush();
1658 ui_delay(3000L, TRUE); /* make sure it is noticed */
1660 top_file_num = 1;
1664 * Always copy the options from the current buffer.
1666 buf_copy_options(buf, BCO_ALWAYS);
1669 buf->b_wininfo->wi_fpos.lnum = lnum;
1670 buf->b_wininfo->wi_win = curwin;
1672 #ifdef FEAT_EVAL
1673 init_var_dict(&buf->b_vars, &buf->b_bufvar); /* init b: variables */
1674 #endif
1675 #ifdef FEAT_SYN_HL
1676 hash_init(&buf->b_keywtab);
1677 hash_init(&buf->b_keywtab_ic);
1678 #endif
1680 buf->b_fname = buf->b_sfname;
1681 #ifdef UNIX
1682 if (st.st_dev == (dev_T)-1)
1683 buf->b_dev = -1;
1684 else
1686 buf->b_dev = st.st_dev;
1687 buf->b_ino = st.st_ino;
1689 #endif
1690 buf->b_u_synced = TRUE;
1691 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
1692 if (flags & BLN_DUMMY)
1693 buf->b_flags |= BF_DUMMY;
1694 buf_clear_file(buf);
1695 clrallmarks(buf); /* clear marks */
1696 fmarks_check_names(buf); /* check file marks for this file */
1697 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1698 #ifdef FEAT_AUTOCMD
1699 if (!(flags & BLN_DUMMY))
1701 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
1702 if (flags & BLN_LISTED)
1703 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1704 # ifdef FEAT_EVAL
1705 if (aborting()) /* autocmds may abort script processing */
1706 return NULL;
1707 # endif
1709 #endif
1711 return buf;
1715 * Free the memory for the options of a buffer.
1716 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1717 * 'fileencoding'.
1719 void
1720 free_buf_options(buf, free_p_ff)
1721 buf_T *buf;
1722 int free_p_ff;
1724 if (free_p_ff)
1726 #ifdef FEAT_MBYTE
1727 clear_string_option(&buf->b_p_fenc);
1728 #endif
1729 clear_string_option(&buf->b_p_ff);
1730 #ifdef FEAT_QUICKFIX
1731 clear_string_option(&buf->b_p_bh);
1732 clear_string_option(&buf->b_p_bt);
1733 #endif
1735 #ifdef FEAT_FIND_ID
1736 clear_string_option(&buf->b_p_def);
1737 clear_string_option(&buf->b_p_inc);
1738 # ifdef FEAT_EVAL
1739 clear_string_option(&buf->b_p_inex);
1740 # endif
1741 #endif
1742 #if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1743 clear_string_option(&buf->b_p_inde);
1744 clear_string_option(&buf->b_p_indk);
1745 #endif
1746 #if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1747 clear_string_option(&buf->b_p_bexpr);
1748 #endif
1749 #if defined(FEAT_EVAL)
1750 clear_string_option(&buf->b_p_fex);
1751 #endif
1752 #ifdef FEAT_CRYPT
1753 clear_string_option(&buf->b_p_key);
1754 #endif
1755 clear_string_option(&buf->b_p_kp);
1756 clear_string_option(&buf->b_p_mps);
1757 clear_string_option(&buf->b_p_fo);
1758 clear_string_option(&buf->b_p_flp);
1759 clear_string_option(&buf->b_p_isk);
1760 #ifdef FEAT_KEYMAP
1761 clear_string_option(&buf->b_p_keymap);
1762 ga_clear(&buf->b_kmap_ga);
1763 #endif
1764 #ifdef FEAT_COMMENTS
1765 clear_string_option(&buf->b_p_com);
1766 #endif
1767 #ifdef FEAT_FOLDING
1768 clear_string_option(&buf->b_p_cms);
1769 #endif
1770 clear_string_option(&buf->b_p_nf);
1771 #ifdef FEAT_SYN_HL
1772 clear_string_option(&buf->b_p_syn);
1773 #endif
1774 #ifdef FEAT_SPELL
1775 clear_string_option(&buf->b_p_spc);
1776 clear_string_option(&buf->b_p_spf);
1777 vim_free(buf->b_cap_prog);
1778 buf->b_cap_prog = NULL;
1779 clear_string_option(&buf->b_p_spl);
1780 #endif
1781 #ifdef FEAT_SEARCHPATH
1782 clear_string_option(&buf->b_p_sua);
1783 #endif
1784 #ifdef FEAT_AUTOCMD
1785 clear_string_option(&buf->b_p_ft);
1786 #endif
1787 #ifdef FEAT_OSFILETYPE
1788 clear_string_option(&buf->b_p_oft);
1789 #endif
1790 #ifdef FEAT_CINDENT
1791 clear_string_option(&buf->b_p_cink);
1792 clear_string_option(&buf->b_p_cino);
1793 #endif
1794 #if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1795 clear_string_option(&buf->b_p_cinw);
1796 #endif
1797 #ifdef FEAT_INS_EXPAND
1798 clear_string_option(&buf->b_p_cpt);
1799 #endif
1800 #ifdef FEAT_COMPL_FUNC
1801 clear_string_option(&buf->b_p_cfu);
1802 clear_string_option(&buf->b_p_ofu);
1803 #endif
1804 #ifdef FEAT_QUICKFIX
1805 clear_string_option(&buf->b_p_gp);
1806 clear_string_option(&buf->b_p_mp);
1807 clear_string_option(&buf->b_p_efm);
1808 #endif
1809 clear_string_option(&buf->b_p_ep);
1810 clear_string_option(&buf->b_p_path);
1811 clear_string_option(&buf->b_p_tags);
1812 #ifdef FEAT_INS_EXPAND
1813 clear_string_option(&buf->b_p_dict);
1814 clear_string_option(&buf->b_p_tsr);
1815 #endif
1816 #ifdef FEAT_TEXTOBJ
1817 clear_string_option(&buf->b_p_qe);
1818 #endif
1819 buf->b_p_ar = -1;
1823 * get alternate file n
1824 * set linenr to lnum or altfpos.lnum if lnum == 0
1825 * also set cursor column to altfpos.col if 'startofline' is not set.
1826 * if (options & GETF_SETMARK) call setpcmark()
1827 * if (options & GETF_ALT) we are jumping to an alternate file.
1828 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1830 * return FAIL for failure, OK for success
1833 buflist_getfile(n, lnum, options, forceit)
1834 int n;
1835 linenr_T lnum;
1836 int options;
1837 int forceit;
1839 buf_T *buf;
1840 #ifdef FEAT_WINDOWS
1841 win_T *wp = NULL;
1842 #endif
1843 pos_T *fpos;
1844 colnr_T col;
1846 buf = buflist_findnr(n);
1847 if (buf == NULL)
1849 if ((options & GETF_ALT) && n == 0)
1850 EMSG(_(e_noalt));
1851 else
1852 EMSGN(_("E92: Buffer %ld not found"), n);
1853 return FAIL;
1856 /* if alternate file is the current buffer, nothing to do */
1857 if (buf == curbuf)
1858 return OK;
1860 if (text_locked())
1862 text_locked_msg();
1863 return FAIL;
1865 #ifdef FEAT_AUTOCMD
1866 if (curbuf_locked())
1867 return FAIL;
1868 #endif
1870 /* altfpos may be changed by getfile(), get it now */
1871 if (lnum == 0)
1873 fpos = buflist_findfpos(buf);
1874 lnum = fpos->lnum;
1875 col = fpos->col;
1877 else
1878 col = 0;
1880 #ifdef FEAT_WINDOWS
1881 if (options & GETF_SWITCH)
1883 /* If 'switchbuf' contains "useopen": jump to first window containing
1884 * "buf" if one exists */
1885 if (swb_flags & SWB_USEOPEN)
1886 wp = buf_jump_open_win(buf);
1887 /* If 'switchbuf' contians "usetab": jump to first window in any tab
1888 * page containing "buf" if one exists */
1889 if (wp == NULL && (swb_flags & SWB_USETAB))
1890 wp = buf_jump_open_tab(buf);
1891 /* If 'switchbuf' contains "split" or "newtab" and the current buffer
1892 * isn't empty: open new window */
1893 if (wp == NULL && (swb_flags & (SWB_SPLIT | SWB_NEWTAB)) && !bufempty())
1895 if (swb_flags & SWB_NEWTAB) /* Open in a new tab */
1896 tabpage_new();
1897 else if (win_split(0, 0) == FAIL) /* Open in a new window */
1898 return FAIL;
1899 # ifdef FEAT_SCROLLBIND
1900 curwin->w_p_scb = FALSE;
1901 # endif
1904 #endif
1906 ++RedrawingDisabled;
1907 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
1908 lnum, forceit) <= 0)
1910 --RedrawingDisabled;
1912 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
1913 if (!p_sol && col != 0)
1915 curwin->w_cursor.col = col;
1916 check_cursor_col();
1917 #ifdef FEAT_VIRTUALEDIT
1918 curwin->w_cursor.coladd = 0;
1919 #endif
1920 curwin->w_set_curswant = TRUE;
1922 return OK;
1924 --RedrawingDisabled;
1925 return FAIL;
1929 * go to the last know line number for the current buffer
1931 void
1932 buflist_getfpos()
1934 pos_T *fpos;
1936 fpos = buflist_findfpos(curbuf);
1938 curwin->w_cursor.lnum = fpos->lnum;
1939 check_cursor_lnum();
1941 if (p_sol)
1942 curwin->w_cursor.col = 0;
1943 else
1945 curwin->w_cursor.col = fpos->col;
1946 check_cursor_col();
1947 #ifdef FEAT_VIRTUALEDIT
1948 curwin->w_cursor.coladd = 0;
1949 #endif
1950 curwin->w_set_curswant = TRUE;
1954 #if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
1956 * Find file in buffer list by name (it has to be for the current window).
1957 * Returns NULL if not found.
1959 buf_T *
1960 buflist_findname_exp(fname)
1961 char_u *fname;
1963 char_u *ffname;
1964 buf_T *buf = NULL;
1966 /* First make the name into a full path name */
1967 ffname = FullName_save(fname,
1968 #ifdef UNIX
1969 TRUE /* force expansion, get rid of symbolic links */
1970 #else
1971 FALSE
1972 #endif
1974 if (ffname != NULL)
1976 buf = buflist_findname(ffname);
1977 vim_free(ffname);
1979 return buf;
1981 #endif
1984 * Find file in buffer list by name (it has to be for the current window).
1985 * "ffname" must have a full path.
1986 * Skips dummy buffers.
1987 * Returns NULL if not found.
1989 buf_T *
1990 buflist_findname(ffname)
1991 char_u *ffname;
1993 #ifdef UNIX
1994 struct stat st;
1996 if (mch_stat((char *)ffname, &st) < 0)
1997 st.st_dev = (dev_T)-1;
1998 return buflist_findname_stat(ffname, &st);
2002 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2003 * twice for the same file.
2004 * Returns NULL if not found.
2006 static buf_T *
2007 buflist_findname_stat(ffname, stp)
2008 char_u *ffname;
2009 struct stat *stp;
2011 #endif
2012 buf_T *buf;
2014 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2015 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
2016 #ifdef UNIX
2017 , stp
2018 #endif
2020 return buf;
2021 return NULL;
2024 #if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO)
2026 * Find file in buffer list by a regexp pattern.
2027 * Return fnum of the found buffer.
2028 * Return < 0 for error.
2030 /*ARGSUSED*/
2032 buflist_findpat(pattern, pattern_end, unlisted, diffmode)
2033 char_u *pattern;
2034 char_u *pattern_end; /* pointer to first char after pattern */
2035 int unlisted; /* find unlisted buffers */
2036 int diffmode; /* find diff-mode buffers only */
2038 buf_T *buf;
2039 regprog_T *prog;
2040 int match = -1;
2041 int find_listed;
2042 char_u *pat;
2043 char_u *patend;
2044 int attempt;
2045 char_u *p;
2046 int toggledollar;
2048 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2050 if (*pattern == '%')
2051 match = curbuf->b_fnum;
2052 else
2053 match = curwin->w_alt_fnum;
2054 #ifdef FEAT_DIFF
2055 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2056 match = -1;
2057 #endif
2061 * Try four ways of matching a listed buffer:
2062 * attempt == 0: without '^' or '$' (at any position)
2063 * attempt == 1: with '^' at start (only at position 0)
2064 * attempt == 2: with '$' at end (only match at end)
2065 * attempt == 3: with '^' at start and '$' at end (only full match)
2066 * Repeat this for finding an unlisted buffer if there was no matching
2067 * listed buffer.
2069 else
2071 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2072 if (pat == NULL)
2073 return -1;
2074 patend = pat + STRLEN(pat) - 1;
2075 toggledollar = (patend > pat && *patend == '$');
2077 /* First try finding a listed buffer. If not found and "unlisted"
2078 * is TRUE, try finding an unlisted buffer. */
2079 find_listed = TRUE;
2080 for (;;)
2082 for (attempt = 0; attempt <= 3; ++attempt)
2084 /* may add '^' and '$' */
2085 if (toggledollar)
2086 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2087 p = pat;
2088 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2089 ++p;
2090 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2091 if (prog == NULL)
2093 vim_free(pat);
2094 return -1;
2097 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2098 if (buf->b_p_bl == find_listed
2099 #ifdef FEAT_DIFF
2100 && (!diffmode || diff_mode_buf(buf))
2101 #endif
2102 && buflist_match(prog, buf) != NULL)
2104 if (match >= 0) /* already found a match */
2106 match = -2;
2107 break;
2109 match = buf->b_fnum; /* remember first match */
2112 vim_free(prog);
2113 if (match >= 0) /* found one match */
2114 break;
2117 /* Only search for unlisted buffers if there was no match with
2118 * a listed buffer. */
2119 if (!unlisted || !find_listed || match != -1)
2120 break;
2121 find_listed = FALSE;
2124 vim_free(pat);
2127 if (match == -2)
2128 EMSG2(_("E93: More than one match for %s"), pattern);
2129 else if (match < 0)
2130 EMSG2(_("E94: No matching buffer for %s"), pattern);
2131 return match;
2133 #endif
2135 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2138 * Find all buffer names that match.
2139 * For command line expansion of ":buf" and ":sbuf".
2140 * Return OK if matches found, FAIL otherwise.
2143 ExpandBufnames(pat, num_file, file, options)
2144 char_u *pat;
2145 int *num_file;
2146 char_u ***file;
2147 int options;
2149 int count = 0;
2150 buf_T *buf;
2151 int round;
2152 char_u *p;
2153 int attempt;
2154 regprog_T *prog;
2155 char_u *patc;
2157 *num_file = 0; /* return values in case of FAIL */
2158 *file = NULL;
2160 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2161 if (*pat == '^')
2163 patc = alloc((unsigned)STRLEN(pat) + 11);
2164 if (patc == NULL)
2165 return FAIL;
2166 STRCPY(patc, "\\(^\\|[\\/]\\)");
2167 STRCPY(patc + 11, pat + 1);
2169 else
2170 patc = pat;
2173 * attempt == 0: try match with '\<', match at start of word
2174 * attempt == 1: try match without '\<', match anywhere
2176 for (attempt = 0; attempt <= 1; ++attempt)
2178 if (attempt > 0 && patc == pat)
2179 break; /* there was no anchor, no need to try again */
2180 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2181 if (prog == NULL)
2183 if (patc != pat)
2184 vim_free(patc);
2185 return FAIL;
2189 * round == 1: Count the matches.
2190 * round == 2: Build the array to keep the matches.
2192 for (round = 1; round <= 2; ++round)
2194 count = 0;
2195 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2197 if (!buf->b_p_bl) /* skip unlisted buffers */
2198 continue;
2199 p = buflist_match(prog, buf);
2200 if (p != NULL)
2202 if (round == 1)
2203 ++count;
2204 else
2206 if (options & WILD_HOME_REPLACE)
2207 p = home_replace_save(buf, p);
2208 else
2209 p = vim_strsave(p);
2210 (*file)[count++] = p;
2214 if (count == 0) /* no match found, break here */
2215 break;
2216 if (round == 1)
2218 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2219 if (*file == NULL)
2221 vim_free(prog);
2222 if (patc != pat)
2223 vim_free(patc);
2224 return FAIL;
2228 vim_free(prog);
2229 if (count) /* match(es) found, break here */
2230 break;
2233 if (patc != pat)
2234 vim_free(patc);
2236 *num_file = count;
2237 return (count == 0 ? FAIL : OK);
2240 #endif /* FEAT_CMDL_COMPL */
2242 #ifdef HAVE_BUFLIST_MATCH
2244 * Check for a match on the file name for buffer "buf" with regprog "prog".
2246 static char_u *
2247 buflist_match(prog, buf)
2248 regprog_T *prog;
2249 buf_T *buf;
2251 char_u *match;
2253 /* First try the short file name, then the long file name. */
2254 match = fname_match(prog, buf->b_sfname);
2255 if (match == NULL)
2256 match = fname_match(prog, buf->b_ffname);
2258 return match;
2262 * Try matching the regexp in "prog" with file name "name".
2263 * Return "name" when there is a match, NULL when not.
2265 static char_u *
2266 fname_match(prog, name)
2267 regprog_T *prog;
2268 char_u *name;
2270 char_u *match = NULL;
2271 char_u *p;
2272 regmatch_T regmatch;
2274 if (name != NULL)
2276 regmatch.regprog = prog;
2277 #ifdef CASE_INSENSITIVE_FILENAME
2278 regmatch.rm_ic = TRUE; /* Always ignore case */
2279 #else
2280 regmatch.rm_ic = FALSE; /* Never ignore case */
2281 #endif
2283 if (vim_regexec(&regmatch, name, (colnr_T)0))
2284 match = name;
2285 else
2287 /* Replace $(HOME) with '~' and try matching again. */
2288 p = home_replace_save(NULL, name);
2289 if (p != NULL && vim_regexec(&regmatch, p, (colnr_T)0))
2290 match = name;
2291 vim_free(p);
2295 return match;
2297 #endif
2300 * find file in buffer list by number
2302 buf_T *
2303 buflist_findnr(nr)
2304 int nr;
2306 buf_T *buf;
2308 if (nr == 0)
2309 nr = curwin->w_alt_fnum;
2310 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2311 if (buf->b_fnum == nr)
2312 return (buf);
2313 return NULL;
2317 * Get name of file 'n' in the buffer list.
2318 * When the file has no name an empty string is returned.
2319 * home_replace() is used to shorten the file name (used for marks).
2320 * Returns a pointer to allocated memory, of NULL when failed.
2322 char_u *
2323 buflist_nr2name(n, fullname, helptail)
2324 int n;
2325 int fullname;
2326 int helptail; /* for help buffers return tail only */
2328 buf_T *buf;
2330 buf = buflist_findnr(n);
2331 if (buf == NULL)
2332 return NULL;
2333 return home_replace_save(helptail ? buf : NULL,
2334 fullname ? buf->b_ffname : buf->b_fname);
2338 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2339 * When "copy_options" is TRUE save the local window option values.
2340 * When "lnum" is 0 only do the options.
2342 static void
2343 buflist_setfpos(buf, win, lnum, col, copy_options)
2344 buf_T *buf;
2345 win_T *win;
2346 linenr_T lnum;
2347 colnr_T col;
2348 int copy_options;
2350 wininfo_T *wip;
2352 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2353 if (wip->wi_win == win)
2354 break;
2355 if (wip == NULL)
2357 /* allocate a new entry */
2358 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2359 if (wip == NULL)
2360 return;
2361 wip->wi_win = win;
2362 if (lnum == 0) /* set lnum even when it's 0 */
2363 lnum = 1;
2365 else
2367 /* remove the entry from the list */
2368 if (wip->wi_prev)
2369 wip->wi_prev->wi_next = wip->wi_next;
2370 else
2371 buf->b_wininfo = wip->wi_next;
2372 if (wip->wi_next)
2373 wip->wi_next->wi_prev = wip->wi_prev;
2374 if (copy_options && wip->wi_optset)
2376 clear_winopt(&wip->wi_opt);
2377 #ifdef FEAT_FOLDING
2378 deleteFoldRecurse(&wip->wi_folds);
2379 #endif
2382 if (lnum != 0)
2384 wip->wi_fpos.lnum = lnum;
2385 wip->wi_fpos.col = col;
2387 if (copy_options)
2389 /* Save the window-specific option values. */
2390 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2391 #ifdef FEAT_FOLDING
2392 wip->wi_fold_manual = win->w_fold_manual;
2393 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2394 #endif
2395 wip->wi_optset = TRUE;
2398 /* insert the entry in front of the list */
2399 wip->wi_next = buf->b_wininfo;
2400 buf->b_wininfo = wip;
2401 wip->wi_prev = NULL;
2402 if (wip->wi_next)
2403 wip->wi_next->wi_prev = wip;
2405 return;
2409 * Find info for the current window in buffer "buf".
2410 * If not found, return the info for the most recently used window.
2411 * Returns NULL when there isn't any info.
2413 static wininfo_T *
2414 find_wininfo(buf)
2415 buf_T *buf;
2417 wininfo_T *wip;
2419 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2420 if (wip->wi_win == curwin)
2421 break;
2422 if (wip == NULL) /* if no fpos for curwin, use the first in the list */
2423 wip = buf->b_wininfo;
2424 return wip;
2428 * Reset the local window options to the values last used in this window.
2429 * If the buffer wasn't used in this window before, use the values from
2430 * the most recently used window. If the values were never set, use the
2431 * global values for the window.
2433 void
2434 get_winopts(buf)
2435 buf_T *buf;
2437 wininfo_T *wip;
2439 clear_winopt(&curwin->w_onebuf_opt);
2440 #ifdef FEAT_FOLDING
2441 clearFolding(curwin);
2442 #endif
2444 wip = find_wininfo(buf);
2445 if (wip != NULL && wip->wi_optset)
2447 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2448 #ifdef FEAT_FOLDING
2449 curwin->w_fold_manual = wip->wi_fold_manual;
2450 curwin->w_foldinvalid = TRUE;
2451 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2452 #endif
2454 else
2455 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2457 #ifdef FEAT_FOLDING
2458 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2459 if (p_fdls >= 0)
2460 curwin->w_p_fdl = p_fdls;
2461 #endif
2465 * Find the position (lnum and col) for the buffer 'buf' for the current
2466 * window.
2467 * Returns a pointer to no_position if no position is found.
2469 pos_T *
2470 buflist_findfpos(buf)
2471 buf_T *buf;
2473 wininfo_T *wip;
2474 static pos_T no_position = {1, 0};
2476 wip = find_wininfo(buf);
2477 if (wip != NULL)
2478 return &(wip->wi_fpos);
2479 else
2480 return &no_position;
2484 * Find the lnum for the buffer 'buf' for the current window.
2486 linenr_T
2487 buflist_findlnum(buf)
2488 buf_T *buf;
2490 return buflist_findfpos(buf)->lnum;
2493 #if defined(FEAT_LISTCMDS) || defined(PROTO)
2495 * List all know file names (for :files and :buffers command).
2497 /*ARGSUSED*/
2498 void
2499 buflist_list(eap)
2500 exarg_T *eap;
2502 buf_T *buf;
2503 int len;
2504 int i;
2506 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2508 /* skip unlisted buffers, unless ! was used */
2509 if (!buf->b_p_bl && !eap->forceit)
2510 continue;
2511 msg_putchar('\n');
2512 if (buf_spname(buf) != NULL)
2513 STRCPY(NameBuff, buf_spname(buf));
2514 else
2515 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2517 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
2518 buf->b_fnum,
2519 buf->b_p_bl ? ' ' : 'u',
2520 buf == curbuf ? '%' :
2521 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2522 buf->b_ml.ml_mfp == NULL ? ' ' :
2523 (buf->b_nwindows == 0 ? 'h' : 'a'),
2524 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2525 (buf->b_flags & BF_READERR) ? 'x'
2526 : (bufIsChanged(buf) ? '+' : ' '),
2527 NameBuff);
2529 /* put "line 999" in column 40 or after the file name */
2530 i = 40 - vim_strsize(IObuff);
2533 IObuff[len++] = ' ';
2534 } while (--i > 0 && len < IOSIZE - 18);
2535 vim_snprintf((char *)IObuff + len, IOSIZE - len, _("line %ld"),
2536 buf == curbuf ? curwin->w_cursor.lnum
2537 : (long)buflist_findlnum(buf));
2538 msg_outtrans(IObuff);
2539 out_flush(); /* output one line at a time */
2540 ui_breakcheck();
2543 #endif
2546 * Get file name and line number for file 'fnum'.
2547 * Used by DoOneCmd() for translating '%' and '#'.
2548 * Used by insert_reg() and cmdline_paste() for '#' register.
2549 * Return FAIL if not found, OK for success.
2552 buflist_name_nr(fnum, fname, lnum)
2553 int fnum;
2554 char_u **fname;
2555 linenr_T *lnum;
2557 buf_T *buf;
2559 buf = buflist_findnr(fnum);
2560 if (buf == NULL || buf->b_fname == NULL)
2561 return FAIL;
2563 *fname = buf->b_fname;
2564 *lnum = buflist_findlnum(buf);
2566 return OK;
2570 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2571 * The file name with the full path is also remembered, for when :cd is used.
2572 * Returns FAIL for failure (file name already in use by other buffer)
2573 * OK otherwise.
2576 setfname(buf, ffname, sfname, message)
2577 buf_T *buf;
2578 char_u *ffname, *sfname;
2579 int message; /* give message when buffer already exists */
2581 buf_T *obuf = NULL;
2582 #ifdef UNIX
2583 struct stat st;
2584 #endif
2586 if (ffname == NULL || *ffname == NUL)
2588 /* Removing the name. */
2589 vim_free(buf->b_ffname);
2590 vim_free(buf->b_sfname);
2591 buf->b_ffname = NULL;
2592 buf->b_sfname = NULL;
2593 #ifdef UNIX
2594 st.st_dev = (dev_T)-1;
2595 #endif
2597 else
2599 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2600 if (ffname == NULL) /* out of memory */
2601 return FAIL;
2604 * if the file name is already used in another buffer:
2605 * - if the buffer is loaded, fail
2606 * - if the buffer is not loaded, delete it from the list
2608 #ifdef UNIX
2609 if (mch_stat((char *)ffname, &st) < 0)
2610 st.st_dev = (dev_T)-1;
2611 #endif
2612 if (!(buf->b_flags & BF_DUMMY))
2613 #ifdef UNIX
2614 obuf = buflist_findname_stat(ffname, &st);
2615 #else
2616 obuf = buflist_findname(ffname);
2617 #endif
2618 if (obuf != NULL && obuf != buf)
2620 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2622 if (message)
2623 EMSG(_("E95: Buffer with this name already exists"));
2624 vim_free(ffname);
2625 return FAIL;
2627 close_buffer(NULL, obuf, DOBUF_WIPE); /* delete from the list */
2629 sfname = vim_strsave(sfname);
2630 if (ffname == NULL || sfname == NULL)
2632 vim_free(sfname);
2633 vim_free(ffname);
2634 return FAIL;
2636 #ifdef USE_FNAME_CASE
2637 # ifdef USE_LONG_FNAME
2638 if (USE_LONG_FNAME)
2639 # endif
2640 fname_case(sfname, 0); /* set correct case for short file name */
2641 #endif
2642 vim_free(buf->b_ffname);
2643 vim_free(buf->b_sfname);
2644 buf->b_ffname = ffname;
2645 buf->b_sfname = sfname;
2647 buf->b_fname = buf->b_sfname;
2648 #ifdef UNIX
2649 if (st.st_dev == (dev_T)-1)
2650 buf->b_dev = -1;
2651 else
2653 buf->b_dev = st.st_dev;
2654 buf->b_ino = st.st_ino;
2656 #endif
2658 #ifndef SHORT_FNAME
2659 buf->b_shortname = FALSE;
2660 #endif
2662 buf_name_changed(buf);
2663 return OK;
2667 * Crude way of changing the name of a buffer. Use with care!
2668 * The name should be relative to the current directory.
2670 void
2671 buf_set_name(fnum, name)
2672 int fnum;
2673 char_u *name;
2675 buf_T *buf;
2677 buf = buflist_findnr(fnum);
2678 if (buf != NULL)
2680 vim_free(buf->b_sfname);
2681 vim_free(buf->b_ffname);
2682 buf->b_ffname = vim_strsave(name);
2683 buf->b_sfname = NULL;
2684 /* Allocate ffname and expand into full path. Also resolves .lnk
2685 * files on Win32. */
2686 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
2687 buf->b_fname = buf->b_sfname;
2692 * Take care of what needs to be done when the name of buffer "buf" has
2693 * changed.
2695 void
2696 buf_name_changed(buf)
2697 buf_T *buf;
2700 * If the file name changed, also change the name of the swapfile
2702 if (buf->b_ml.ml_mfp != NULL)
2703 ml_setname(buf);
2705 if (curwin->w_buffer == buf)
2706 check_arg_idx(curwin); /* check file name for arg list */
2707 #ifdef FEAT_TITLE
2708 maketitle(); /* set window title */
2709 #endif
2710 #ifdef FEAT_WINDOWS
2711 status_redraw_all(); /* status lines need to be redrawn */
2712 #endif
2713 fmarks_check_names(buf); /* check named file marks */
2714 ml_timestamp(buf); /* reset timestamp */
2718 * set alternate file name for current window
2720 * Used by do_one_cmd(), do_write() and do_ecmd().
2721 * Return the buffer.
2723 buf_T *
2724 setaltfname(ffname, sfname, lnum)
2725 char_u *ffname;
2726 char_u *sfname;
2727 linenr_T lnum;
2729 buf_T *buf;
2731 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2732 buf = buflist_new(ffname, sfname, lnum, 0);
2733 if (buf != NULL && !cmdmod.keepalt)
2734 curwin->w_alt_fnum = buf->b_fnum;
2735 return buf;
2739 * Get alternate file name for current window.
2740 * Return NULL if there isn't any, and give error message if requested.
2742 char_u *
2743 getaltfname(errmsg)
2744 int errmsg; /* give error message */
2746 char_u *fname;
2747 linenr_T dummy;
2749 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2751 if (errmsg)
2752 EMSG(_(e_noalt));
2753 return NULL;
2755 return fname;
2759 * Add a file name to the buflist and return its number.
2760 * Uses same flags as buflist_new(), except BLN_DUMMY.
2762 * used by qf_init(), main() and doarglist()
2765 buflist_add(fname, flags)
2766 char_u *fname;
2767 int flags;
2769 buf_T *buf;
2771 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
2772 if (buf != NULL)
2773 return buf->b_fnum;
2774 return 0;
2777 #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2779 * Adjust slashes in file names. Called after 'shellslash' was set.
2781 void
2782 buflist_slash_adjust()
2784 buf_T *bp;
2786 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
2788 if (bp->b_ffname != NULL)
2789 slash_adjust(bp->b_ffname);
2790 if (bp->b_sfname != NULL)
2791 slash_adjust(bp->b_sfname);
2794 #endif
2797 * Set alternate cursor position for current window.
2798 * Also save the local window option values.
2800 void
2801 buflist_altfpos()
2803 buflist_setfpos(curbuf, curwin, curwin->w_cursor.lnum,
2804 curwin->w_cursor.col, TRUE);
2808 * Return TRUE if 'ffname' is not the same file as current file.
2809 * Fname must have a full path (expanded by mch_FullName()).
2812 otherfile(ffname)
2813 char_u *ffname;
2815 return otherfile_buf(curbuf, ffname
2816 #ifdef UNIX
2817 , NULL
2818 #endif
2822 static int
2823 otherfile_buf(buf, ffname
2824 #ifdef UNIX
2825 , stp
2826 #endif
2828 buf_T *buf;
2829 char_u *ffname;
2830 #ifdef UNIX
2831 struct stat *stp;
2832 #endif
2834 /* no name is different */
2835 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
2836 return TRUE;
2837 if (fnamecmp(ffname, buf->b_ffname) == 0)
2838 return FALSE;
2839 #ifdef UNIX
2841 struct stat st;
2843 /* If no struct stat given, get it now */
2844 if (stp == NULL)
2846 if (buf->b_dev < 0 || mch_stat((char *)ffname, &st) < 0)
2847 st.st_dev = (dev_T)-1;
2848 stp = &st;
2850 /* Use dev/ino to check if the files are the same, even when the names
2851 * are different (possible with links). Still need to compare the
2852 * name above, for when the file doesn't exist yet.
2853 * Problem: The dev/ino changes when a file is deleted (and created
2854 * again) and remains the same when renamed/moved. We don't want to
2855 * mch_stat() each buffer each time, that would be too slow. Get the
2856 * dev/ino again when they appear to match, but not when they appear
2857 * to be different: Could skip a buffer when it's actually the same
2858 * file. */
2859 if (buf_same_ino(buf, stp))
2861 buf_setino(buf);
2862 if (buf_same_ino(buf, stp))
2863 return FALSE;
2866 #endif
2867 return TRUE;
2870 #if defined(UNIX) || defined(PROTO)
2872 * Set inode and device number for a buffer.
2873 * Must always be called when b_fname is changed!.
2875 void
2876 buf_setino(buf)
2877 buf_T *buf;
2879 struct stat st;
2881 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
2883 buf->b_dev = st.st_dev;
2884 buf->b_ino = st.st_ino;
2886 else
2887 buf->b_dev = -1;
2891 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
2893 static int
2894 buf_same_ino(buf, stp)
2895 buf_T *buf;
2896 struct stat *stp;
2898 return (buf->b_dev >= 0
2899 && stp->st_dev == buf->b_dev
2900 && stp->st_ino == buf->b_ino);
2902 #endif
2905 * Print info about the current buffer.
2907 void
2908 fileinfo(fullname, shorthelp, dont_truncate)
2909 int fullname; /* when non-zero print full path */
2910 int shorthelp;
2911 int dont_truncate;
2913 char_u *name;
2914 int n;
2915 char_u *p;
2916 char_u *buffer;
2917 size_t len;
2919 buffer = alloc(IOSIZE);
2920 if (buffer == NULL)
2921 return;
2923 if (fullname > 1) /* 2 CTRL-G: include buffer number */
2925 sprintf((char *)buffer, "buf %d: ", curbuf->b_fnum);
2926 p = buffer + STRLEN(buffer);
2928 else
2929 p = buffer;
2931 *p++ = '"';
2932 if (buf_spname(curbuf) != NULL)
2933 STRCPY(p, buf_spname(curbuf));
2934 else
2936 if (!fullname && curbuf->b_fname != NULL)
2937 name = curbuf->b_fname;
2938 else
2939 name = curbuf->b_ffname;
2940 home_replace(shorthelp ? curbuf : NULL, name, p,
2941 (int)(IOSIZE - (p - buffer)), TRUE);
2944 len = STRLEN(buffer);
2945 vim_snprintf((char *)buffer + len, IOSIZE - len,
2946 "\"%s%s%s%s%s%s",
2947 curbufIsChanged() ? (shortmess(SHM_MOD)
2948 ? " [+]" : _(" [Modified]")) : " ",
2949 (curbuf->b_flags & BF_NOTEDITED)
2950 #ifdef FEAT_QUICKFIX
2951 && !bt_dontwrite(curbuf)
2952 #endif
2953 ? _("[Not edited]") : "",
2954 (curbuf->b_flags & BF_NEW)
2955 #ifdef FEAT_QUICKFIX
2956 && !bt_dontwrite(curbuf)
2957 #endif
2958 ? _("[New file]") : "",
2959 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
2960 curbuf->b_p_ro ? (shortmess(SHM_RO) ? "[RO]"
2961 : _("[readonly]")) : "",
2962 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
2963 || curbuf->b_p_ro) ?
2964 " " : "");
2965 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
2966 * causes an overflow, thus for large numbers divide instead. */
2967 if (curwin->w_cursor.lnum > 1000000L)
2968 n = (int)(((long)curwin->w_cursor.lnum) /
2969 ((long)curbuf->b_ml.ml_line_count / 100L));
2970 else
2971 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
2972 (long)curbuf->b_ml.ml_line_count);
2973 len = STRLEN(buffer);
2974 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2976 vim_snprintf((char *)buffer + len, IOSIZE - len, "%s", _(no_lines_msg));
2978 #ifdef FEAT_CMDL_INFO
2979 else if (p_ru)
2981 /* Current line and column are already on the screen -- webb */
2982 if (curbuf->b_ml.ml_line_count == 1)
2983 vim_snprintf((char *)buffer + len, IOSIZE - len,
2984 _("1 line --%d%%--"), n);
2985 else
2986 vim_snprintf((char *)buffer + len, IOSIZE - len,
2987 _("%ld lines --%d%%--"),
2988 (long)curbuf->b_ml.ml_line_count, n);
2990 #endif
2991 else
2993 vim_snprintf((char *)buffer + len, IOSIZE - len,
2994 _("line %ld of %ld --%d%%-- col "),
2995 (long)curwin->w_cursor.lnum,
2996 (long)curbuf->b_ml.ml_line_count,
2998 validate_virtcol();
2999 col_print(buffer + STRLEN(buffer),
3000 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3003 (void)append_arg_number(curwin, buffer, !shortmess(SHM_FILE), IOSIZE);
3005 if (dont_truncate)
3007 /* Temporarily set msg_scroll to avoid the message being truncated.
3008 * First call msg_start() to get the message in the right place. */
3009 msg_start();
3010 n = msg_scroll;
3011 msg_scroll = TRUE;
3012 msg(buffer);
3013 msg_scroll = n;
3015 else
3017 p = msg_trunc_attr(buffer, FALSE, 0);
3018 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
3019 /* Need to repeat the message after redrawing when:
3020 * - When restart_edit is set (otherwise there will be a delay
3021 * before redrawing).
3022 * - When the screen was scrolled but there is no wait-return
3023 * prompt. */
3024 set_keep_msg(p, 0);
3027 vim_free(buffer);
3030 void
3031 col_print(buf, col, vcol)
3032 char_u *buf;
3033 int col;
3034 int vcol;
3036 if (col == vcol)
3037 sprintf((char *)buf, "%d", col);
3038 else
3039 sprintf((char *)buf, "%d-%d", col, vcol);
3042 #if defined(FEAT_TITLE) || defined(PROTO)
3044 * put file name in title bar of window and in icon title
3047 static char_u *lasttitle = NULL;
3048 static char_u *lasticon = NULL;
3050 void
3051 maketitle()
3053 char_u *p;
3054 char_u *t_str = NULL;
3055 char_u *i_name;
3056 char_u *i_str = NULL;
3057 int maxlen = 0;
3058 int len;
3059 int mustset;
3060 char_u buf[IOSIZE];
3061 int off;
3063 if (!redrawing())
3065 /* Postpone updating the title when 'lazyredraw' is set. */
3066 need_maketitle = TRUE;
3067 return;
3070 #ifdef FEAT_GUI_MACVIM
3071 gui_macvim_update_modified_flag();
3072 #endif
3074 need_maketitle = FALSE;
3075 if (!p_title && !p_icon)
3076 return;
3078 if (p_title)
3080 if (p_titlelen > 0)
3082 maxlen = p_titlelen * Columns / 100;
3083 if (maxlen < 10)
3084 maxlen = 10;
3087 t_str = buf;
3088 if (*p_titlestring != NUL)
3090 #ifdef FEAT_STL_OPT
3091 if (stl_syntax & STL_IN_TITLE)
3093 int use_sandbox = FALSE;
3094 int save_called_emsg = called_emsg;
3096 # ifdef FEAT_EVAL
3097 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
3098 # endif
3099 called_emsg = FALSE;
3100 build_stl_str_hl(curwin, t_str, sizeof(buf),
3101 p_titlestring, use_sandbox,
3102 0, maxlen, NULL, NULL);
3103 if (called_emsg)
3104 set_string_option_direct((char_u *)"titlestring", -1,
3105 (char_u *)"", OPT_FREE, SID_ERROR);
3106 called_emsg |= save_called_emsg;
3108 else
3109 #endif
3110 t_str = p_titlestring;
3112 else
3114 /* format: "fname + (path) (1 of 2) - VIM" */
3116 if (curbuf->b_fname == NULL)
3117 STRCPY(buf, _("[No Name]"));
3118 else
3120 p = transstr(gettail(curbuf->b_fname));
3121 vim_strncpy(buf, p, IOSIZE - 100);
3122 vim_free(p);
3125 switch (bufIsChanged(curbuf)
3126 + (curbuf->b_p_ro * 2)
3127 + (!curbuf->b_p_ma * 4))
3129 case 1: STRCAT(buf, " +"); break;
3130 case 2: STRCAT(buf, " ="); break;
3131 case 3: STRCAT(buf, " =+"); break;
3132 case 4:
3133 case 6: STRCAT(buf, " -"); break;
3134 case 5:
3135 case 7: STRCAT(buf, " -+"); break;
3138 if (curbuf->b_fname != NULL)
3140 /* Get path of file, replace home dir with ~ */
3141 off = (int)STRLEN(buf);
3142 buf[off++] = ' ';
3143 buf[off++] = '(';
3144 home_replace(curbuf, curbuf->b_ffname,
3145 buf + off, IOSIZE - off, TRUE);
3146 #ifdef BACKSLASH_IN_FILENAME
3147 /* avoid "c:/name" to be reduced to "c" */
3148 if (isalpha(buf[off]) && buf[off + 1] == ':')
3149 off += 2;
3150 #endif
3151 /* remove the file name */
3152 p = gettail_sep(buf + off);
3153 if (p == buf + off)
3154 /* must be a help buffer */
3155 vim_strncpy(buf + off, (char_u *)_("help"),
3156 IOSIZE - off - 1);
3157 else
3158 *p = NUL;
3160 /* translate unprintable chars */
3161 p = transstr(buf + off);
3162 vim_strncpy(buf + off, p, IOSIZE - off - 1);
3163 vim_free(p);
3164 STRCAT(buf, ")");
3167 #ifndef FEAT_GUI_MACVIM
3168 append_arg_number(curwin, buf, FALSE, IOSIZE);
3169 #endif
3171 #if defined(FEAT_CLIENTSERVER)
3172 if (serverName != NULL)
3174 STRCAT(buf, " - ");
3175 STRCAT(buf, serverName);
3177 else
3178 #endif
3179 STRCAT(buf, " - VIM");
3181 if (maxlen > 0)
3183 /* make it shorter by removing a bit in the middle */
3184 len = vim_strsize(buf);
3185 if (len > maxlen)
3186 trunc_string(buf, buf, maxlen);
3190 mustset = ti_change(t_str, &lasttitle);
3192 if (p_icon)
3194 i_str = buf;
3195 if (*p_iconstring != NUL)
3197 #ifdef FEAT_STL_OPT
3198 if (stl_syntax & STL_IN_ICON)
3200 int use_sandbox = FALSE;
3201 int save_called_emsg = called_emsg;
3203 # ifdef FEAT_EVAL
3204 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
3205 # endif
3206 called_emsg = FALSE;
3207 build_stl_str_hl(curwin, i_str, sizeof(buf),
3208 p_iconstring, use_sandbox,
3209 0, 0, NULL, NULL);
3210 if (called_emsg)
3211 set_string_option_direct((char_u *)"iconstring", -1,
3212 (char_u *)"", OPT_FREE, SID_ERROR);
3213 called_emsg |= save_called_emsg;
3215 else
3216 #endif
3217 i_str = p_iconstring;
3219 else
3221 if (buf_spname(curbuf) != NULL)
3222 i_name = (char_u *)buf_spname(curbuf);
3223 else /* use file name only in icon */
3224 i_name = gettail(curbuf->b_ffname);
3225 *i_str = NUL;
3226 /* Truncate name at 100 bytes. */
3227 len = (int)STRLEN(i_name);
3228 if (len > 100)
3230 len -= 100;
3231 #ifdef FEAT_MBYTE
3232 if (has_mbyte)
3233 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3234 #endif
3235 i_name += len;
3237 STRCPY(i_str, i_name);
3238 trans_characters(i_str, IOSIZE);
3242 mustset |= ti_change(i_str, &lasticon);
3244 if (mustset)
3245 resettitle();
3249 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3250 * from "str" if it does.
3251 * Return TRUE when "*last" changed.
3253 static int
3254 ti_change(str, last)
3255 char_u *str;
3256 char_u **last;
3258 if ((str == NULL) != (*last == NULL)
3259 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3261 vim_free(*last);
3262 if (str == NULL)
3263 *last = NULL;
3264 else
3265 *last = vim_strsave(str);
3266 return TRUE;
3268 return FALSE;
3272 * Put current window title back (used after calling a shell)
3274 void
3275 resettitle()
3277 mch_settitle(lasttitle, lasticon);
3280 # if defined(EXITFREE) || defined(PROTO)
3281 void
3282 free_titles()
3284 vim_free(lasttitle);
3285 vim_free(lasticon);
3287 # endif
3289 #endif /* FEAT_TITLE */
3291 #if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
3293 * Build a string from the status line items in "fmt".
3294 * Return length of string in screen cells.
3296 * Normally works for window "wp", except when working for 'tabline' then it
3297 * is "curwin".
3299 * Items are drawn interspersed with the text that surrounds it
3300 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3301 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3303 * If maxwidth is not zero, the string will be filled at any middle marker
3304 * or truncated if too long, fillchar is used for all whitespace.
3306 /*ARGSUSED*/
3308 build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar, maxwidth, hltab, tabtab)
3309 win_T *wp;
3310 char_u *out; /* buffer to write into != NameBuff */
3311 size_t outlen; /* length of out[] */
3312 char_u *fmt;
3313 int use_sandbox; /* "fmt" was set insecurely, use sandbox */
3314 int fillchar;
3315 int maxwidth;
3316 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3317 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
3319 char_u *p;
3320 char_u *s;
3321 char_u *t;
3322 char_u *linecont;
3323 #ifdef FEAT_EVAL
3324 win_T *o_curwin;
3325 buf_T *o_curbuf;
3326 #endif
3327 int empty_line;
3328 colnr_T virtcol;
3329 long l;
3330 long n;
3331 int prevchar_isflag;
3332 int prevchar_isitem;
3333 int itemisflag;
3334 int fillable;
3335 char_u *str;
3336 long num;
3337 int width;
3338 int itemcnt;
3339 int curitem;
3340 int groupitem[STL_MAX_ITEM];
3341 int groupdepth;
3342 struct stl_item
3344 char_u *start;
3345 int minwid;
3346 int maxwid;
3347 enum
3349 Normal,
3350 Empty,
3351 Group,
3352 Middle,
3353 Highlight,
3354 TabPage,
3355 Trunc
3356 } type;
3357 } item[STL_MAX_ITEM];
3358 int minwid;
3359 int maxwid;
3360 int zeropad;
3361 char_u base;
3362 char_u opt;
3363 #define TMPLEN 70
3364 char_u tmp[TMPLEN];
3365 char_u *usefmt = fmt;
3366 struct stl_hlrec *sp;
3368 #ifdef FEAT_EVAL
3370 * When the format starts with "%!" then evaluate it as an expression and
3371 * use the result as the actual format string.
3373 if (fmt[0] == '%' && fmt[1] == '!')
3375 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3376 if (usefmt == NULL)
3377 usefmt = fmt;
3379 #endif
3381 if (fillchar == 0)
3382 fillchar = ' ';
3383 #ifdef FEAT_MBYTE
3384 /* Can't handle a multi-byte fill character yet. */
3385 else if (mb_char2len(fillchar) > 1)
3386 fillchar = '-';
3387 #endif
3390 * Get line & check if empty (cursorpos will show "0-1").
3391 * If inversion is possible we use it. Else '=' characters are used.
3393 linecont = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3394 empty_line = (*linecont == NUL);
3396 groupdepth = 0;
3397 p = out;
3398 curitem = 0;
3399 prevchar_isflag = TRUE;
3400 prevchar_isitem = FALSE;
3401 for (s = usefmt; *s; )
3403 if (*s != NUL && *s != '%')
3404 prevchar_isflag = prevchar_isitem = FALSE;
3407 * Handle up to the next '%' or the end.
3409 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3410 *p++ = *s++;
3411 if (*s == NUL || p + 1 >= out + outlen)
3412 break;
3415 * Handle one '%' item.
3417 s++;
3418 if (*s == '%')
3420 if (p + 1 >= out + outlen)
3421 break;
3422 *p++ = *s++;
3423 prevchar_isflag = prevchar_isitem = FALSE;
3424 continue;
3426 if (*s == STL_MIDDLEMARK)
3428 s++;
3429 if (groupdepth > 0)
3430 continue;
3431 item[curitem].type = Middle;
3432 item[curitem++].start = p;
3433 continue;
3435 if (*s == STL_TRUNCMARK)
3437 s++;
3438 item[curitem].type = Trunc;
3439 item[curitem++].start = p;
3440 continue;
3442 if (*s == ')')
3444 s++;
3445 if (groupdepth < 1)
3446 continue;
3447 groupdepth--;
3449 t = item[groupitem[groupdepth]].start;
3450 *p = NUL;
3451 l = vim_strsize(t);
3452 if (curitem > groupitem[groupdepth] + 1
3453 && item[groupitem[groupdepth]].minwid == 0)
3455 /* remove group if all items are empty */
3456 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3457 if (item[n].type == Normal)
3458 break;
3459 if (n == curitem)
3461 p = t;
3462 l = 0;
3465 if (l > item[groupitem[groupdepth]].maxwid)
3467 /* truncate, remove n bytes of text at the start */
3468 #ifdef FEAT_MBYTE
3469 if (has_mbyte)
3471 /* Find the first character that should be included. */
3472 n = 0;
3473 while (l >= item[groupitem[groupdepth]].maxwid)
3475 l -= ptr2cells(t + n);
3476 n += (*mb_ptr2len)(t + n);
3479 else
3480 #endif
3481 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
3483 *t = '<';
3484 mch_memmove(t + 1, t + n, p - (t + n));
3485 p = p - n + 1;
3486 #ifdef FEAT_MBYTE
3487 /* Fill up space left over by half a double-wide char. */
3488 while (++l < item[groupitem[groupdepth]].minwid)
3489 *p++ = fillchar;
3490 #endif
3492 /* correct the start of the items for the truncation */
3493 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3495 item[l].start -= n;
3496 if (item[l].start < t)
3497 item[l].start = t;
3500 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3502 /* fill */
3503 n = item[groupitem[groupdepth]].minwid;
3504 if (n < 0)
3506 /* fill by appending characters */
3507 n = 0 - n;
3508 while (l++ < n && p + 1 < out + outlen)
3509 *p++ = fillchar;
3511 else
3513 /* fill by inserting characters */
3514 mch_memmove(t + n - l, t, p - t);
3515 l = n - l;
3516 if (p + l >= out + outlen)
3517 l = (long)((out + outlen) - p - 1);
3518 p += l;
3519 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3520 item[n].start += l;
3521 for ( ; l > 0; l--)
3522 *t++ = fillchar;
3525 continue;
3527 minwid = 0;
3528 maxwid = 9999;
3529 zeropad = FALSE;
3530 l = 1;
3531 if (*s == '0')
3533 s++;
3534 zeropad = TRUE;
3536 if (*s == '-')
3538 s++;
3539 l = -1;
3541 if (VIM_ISDIGIT(*s))
3543 minwid = (int)getdigits(&s);
3544 if (minwid < 0) /* overflow */
3545 minwid = 0;
3547 if (*s == STL_USER_HL)
3549 item[curitem].type = Highlight;
3550 item[curitem].start = p;
3551 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3552 s++;
3553 curitem++;
3554 continue;
3556 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3558 if (*s == STL_TABCLOSENR)
3560 if (minwid == 0)
3562 /* %X ends the close label, go back to the previously
3563 * define tab label nr. */
3564 for (n = curitem - 1; n >= 0; --n)
3565 if (item[n].type == TabPage && item[n].minwid >= 0)
3567 minwid = item[n].minwid;
3568 break;
3571 else
3572 /* close nrs are stored as negative values */
3573 minwid = - minwid;
3575 item[curitem].type = TabPage;
3576 item[curitem].start = p;
3577 item[curitem].minwid = minwid;
3578 s++;
3579 curitem++;
3580 continue;
3582 if (*s == '.')
3584 s++;
3585 if (VIM_ISDIGIT(*s))
3587 maxwid = (int)getdigits(&s);
3588 if (maxwid <= 0) /* overflow */
3589 maxwid = 50;
3592 minwid = (minwid > 50 ? 50 : minwid) * l;
3593 if (*s == '(')
3595 groupitem[groupdepth++] = curitem;
3596 item[curitem].type = Group;
3597 item[curitem].start = p;
3598 item[curitem].minwid = minwid;
3599 item[curitem].maxwid = maxwid;
3600 s++;
3601 curitem++;
3602 continue;
3604 if (vim_strchr(STL_ALL, *s) == NULL)
3606 s++;
3607 continue;
3609 opt = *s++;
3611 /* OK - now for the real work */
3612 base = 'D';
3613 itemisflag = FALSE;
3614 fillable = TRUE;
3615 num = -1;
3616 str = NULL;
3617 switch (opt)
3619 case STL_FILEPATH:
3620 case STL_FULLPATH:
3621 case STL_FILENAME:
3622 fillable = FALSE; /* don't change ' ' to fillchar */
3623 if (buf_spname(wp->w_buffer) != NULL)
3624 STRCPY(NameBuff, buf_spname(wp->w_buffer));
3625 else
3627 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
3628 : wp->w_buffer->b_fname;
3629 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3631 trans_characters(NameBuff, MAXPATHL);
3632 if (opt != STL_FILENAME)
3633 str = NameBuff;
3634 else
3635 str = gettail(NameBuff);
3636 break;
3638 case STL_VIM_EXPR: /* '{' */
3639 itemisflag = TRUE;
3640 t = p;
3641 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3642 *p++ = *s++;
3643 if (*s != '}') /* missing '}' or out of space */
3644 break;
3645 s++;
3646 *p = 0;
3647 p = t;
3649 #ifdef FEAT_EVAL
3650 sprintf((char *)tmp, "%d", curbuf->b_fnum);
3651 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3653 o_curbuf = curbuf;
3654 o_curwin = curwin;
3655 curwin = wp;
3656 curbuf = wp->w_buffer;
3658 str = eval_to_string_safe(p, &t, use_sandbox);
3660 curwin = o_curwin;
3661 curbuf = o_curbuf;
3662 do_unlet((char_u *)"g:actual_curbuf", TRUE);
3664 if (str != NULL && *str != 0)
3666 if (*skipdigits(str) == NUL)
3668 num = atoi((char *)str);
3669 vim_free(str);
3670 str = NULL;
3671 itemisflag = FALSE;
3674 #endif
3675 break;
3677 case STL_LINE:
3678 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3679 ? 0L : (long)(wp->w_cursor.lnum);
3680 break;
3682 case STL_NUMLINES:
3683 num = wp->w_buffer->b_ml.ml_line_count;
3684 break;
3686 case STL_COLUMN:
3687 num = !(State & INSERT) && empty_line
3688 ? 0 : (int)wp->w_cursor.col + 1;
3689 break;
3691 case STL_VIRTCOL:
3692 case STL_VIRTCOL_ALT:
3693 /* In list mode virtcol needs to be recomputed */
3694 virtcol = wp->w_virtcol;
3695 if (wp->w_p_list && lcs_tab1 == NUL)
3697 wp->w_p_list = FALSE;
3698 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3699 wp->w_p_list = TRUE;
3701 ++virtcol;
3702 /* Don't display %V if it's the same as %c. */
3703 if (opt == STL_VIRTCOL_ALT
3704 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3705 ? 0 : (int)wp->w_cursor.col + 1)))
3706 break;
3707 num = (long)virtcol;
3708 break;
3710 case STL_PERCENTAGE:
3711 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3712 (long)wp->w_buffer->b_ml.ml_line_count);
3713 break;
3715 case STL_ALTPERCENT:
3716 str = tmp;
3717 get_rel_pos(wp, str);
3718 break;
3720 case STL_ARGLISTSTAT:
3721 fillable = FALSE;
3722 tmp[0] = 0;
3723 if (append_arg_number(wp, tmp, FALSE, (int)sizeof(tmp)))
3724 str = tmp;
3725 break;
3727 case STL_KEYMAP:
3728 fillable = FALSE;
3729 if (get_keymap_str(wp, tmp, TMPLEN))
3730 str = tmp;
3731 break;
3732 case STL_PAGENUM:
3733 #if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
3734 num = printer_page_num;
3735 #else
3736 num = 0;
3737 #endif
3738 break;
3740 case STL_BUFNO:
3741 num = wp->w_buffer->b_fnum;
3742 break;
3744 case STL_OFFSET_X:
3745 base = 'X';
3746 case STL_OFFSET:
3747 #ifdef FEAT_BYTEOFF
3748 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
3749 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
3750 0L : l + 1 + (!(State & INSERT) && empty_line ?
3751 0 : (int)wp->w_cursor.col);
3752 #endif
3753 break;
3755 case STL_BYTEVAL_X:
3756 base = 'X';
3757 case STL_BYTEVAL:
3758 if (wp->w_cursor.col > STRLEN(linecont))
3759 num = 0;
3760 else
3762 #ifdef FEAT_MBYTE
3763 num = (*mb_ptr2char)(linecont + wp->w_cursor.col);
3764 #else
3765 num = linecont[wp->w_cursor.col];
3766 #endif
3768 if (num == NL)
3769 num = 0;
3770 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
3771 num = NL;
3772 break;
3774 case STL_ROFLAG:
3775 case STL_ROFLAG_ALT:
3776 itemisflag = TRUE;
3777 if (wp->w_buffer->b_p_ro)
3778 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : "[RO]");
3779 break;
3781 case STL_HELPFLAG:
3782 case STL_HELPFLAG_ALT:
3783 itemisflag = TRUE;
3784 if (wp->w_buffer->b_help)
3785 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
3786 : _("[Help]"));
3787 break;
3789 #ifdef FEAT_AUTOCMD
3790 case STL_FILETYPE:
3791 if (*wp->w_buffer->b_p_ft != NUL
3792 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
3794 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
3795 wp->w_buffer->b_p_ft);
3796 str = tmp;
3798 break;
3800 case STL_FILETYPE_ALT:
3801 itemisflag = TRUE;
3802 if (*wp->w_buffer->b_p_ft != NUL
3803 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
3805 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
3806 wp->w_buffer->b_p_ft);
3807 for (t = tmp; *t != 0; t++)
3808 *t = TOUPPER_LOC(*t);
3809 str = tmp;
3811 break;
3812 #endif
3814 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3815 case STL_PREVIEWFLAG:
3816 case STL_PREVIEWFLAG_ALT:
3817 itemisflag = TRUE;
3818 if (wp->w_p_pvw)
3819 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
3820 : _("[Preview]"));
3821 break;
3822 #endif
3824 case STL_MODIFIED:
3825 case STL_MODIFIED_ALT:
3826 itemisflag = TRUE;
3827 switch ((opt == STL_MODIFIED_ALT)
3828 + bufIsChanged(wp->w_buffer) * 2
3829 + (!wp->w_buffer->b_p_ma) * 4)
3831 case 2: str = (char_u *)"[+]"; break;
3832 case 3: str = (char_u *)",+"; break;
3833 case 4: str = (char_u *)"[-]"; break;
3834 case 5: str = (char_u *)",-"; break;
3835 case 6: str = (char_u *)"[+-]"; break;
3836 case 7: str = (char_u *)",+-"; break;
3838 break;
3840 case STL_HIGHLIGHT:
3841 t = s;
3842 while (*s != '#' && *s != NUL)
3843 ++s;
3844 if (*s == '#')
3846 item[curitem].type = Highlight;
3847 item[curitem].start = p;
3848 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
3849 curitem++;
3851 ++s;
3852 continue;
3855 item[curitem].start = p;
3856 item[curitem].type = Normal;
3857 if (str != NULL && *str)
3859 t = str;
3860 if (itemisflag)
3862 if ((t[0] && t[1])
3863 && ((!prevchar_isitem && *t == ',')
3864 || (prevchar_isflag && *t == ' ')))
3865 t++;
3866 prevchar_isflag = TRUE;
3868 l = vim_strsize(t);
3869 if (l > 0)
3870 prevchar_isitem = TRUE;
3871 if (l > maxwid)
3873 while (l >= maxwid)
3874 #ifdef FEAT_MBYTE
3875 if (has_mbyte)
3877 l -= ptr2cells(t);
3878 t += (*mb_ptr2len)(t);
3880 else
3881 #endif
3882 l -= byte2cells(*t++);
3883 if (p + 1 >= out + outlen)
3884 break;
3885 *p++ = '<';
3887 if (minwid > 0)
3889 for (; l < minwid && p + 1 < out + outlen; l++)
3891 /* Don't put a "-" in front of a digit. */
3892 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
3893 *p++ = ' ';
3894 else
3895 *p++ = fillchar;
3897 minwid = 0;
3899 else
3900 minwid *= -1;
3901 while (*t && p + 1 < out + outlen)
3903 *p++ = *t++;
3904 /* Change a space by fillchar, unless fillchar is '-' and a
3905 * digit follows. */
3906 if (fillable && p[-1] == ' '
3907 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
3908 p[-1] = fillchar;
3910 for (; l < minwid && p + 1 < out + outlen; l++)
3911 *p++ = fillchar;
3913 else if (num >= 0)
3915 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
3916 char_u nstr[20];
3918 if (p + 20 >= out + outlen)
3919 break; /* not sufficient space */
3920 prevchar_isitem = TRUE;
3921 t = nstr;
3922 if (opt == STL_VIRTCOL_ALT)
3924 *t++ = '-';
3925 minwid--;
3927 *t++ = '%';
3928 if (zeropad)
3929 *t++ = '0';
3930 *t++ = '*';
3931 *t++ = nbase == 16 ? base : (nbase == 8 ? 'o' : 'd');
3932 *t = 0;
3934 for (n = num, l = 1; n >= nbase; n /= nbase)
3935 l++;
3936 if (opt == STL_VIRTCOL_ALT)
3937 l++;
3938 if (l > maxwid)
3940 l += 2;
3941 n = l - maxwid;
3942 while (l-- > maxwid)
3943 num /= nbase;
3944 *t++ = '>';
3945 *t++ = '%';
3946 *t = t[-3];
3947 *++t = 0;
3948 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
3949 0, num, n);
3951 else
3952 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
3953 minwid, num);
3954 p += STRLEN(p);
3956 else
3957 item[curitem].type = Empty;
3959 if (opt == STL_VIM_EXPR)
3960 vim_free(str);
3962 if (num >= 0 || (!itemisflag && str && *str))
3963 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
3964 curitem++;
3966 *p = NUL;
3967 itemcnt = curitem;
3969 #ifdef FEAT_EVAL
3970 if (usefmt != fmt)
3971 vim_free(usefmt);
3972 #endif
3974 width = vim_strsize(out);
3975 if (maxwidth > 0 && width > maxwidth)
3977 /* Result is too long, must trunctate somewhere. */
3978 l = 0;
3979 if (itemcnt == 0)
3980 s = out;
3981 else
3983 for ( ; l < itemcnt; l++)
3984 if (item[l].type == Trunc)
3986 /* Truncate at %< item. */
3987 s = item[l].start;
3988 break;
3990 if (l == itemcnt)
3992 /* No %< item, truncate first item. */
3993 s = item[0].start;
3994 l = 0;
3998 if (width - vim_strsize(s) >= maxwidth)
4000 /* Truncation mark is beyond max length */
4001 #ifdef FEAT_MBYTE
4002 if (has_mbyte)
4004 s = out;
4005 width = 0;
4006 for (;;)
4008 width += ptr2cells(s);
4009 if (width >= maxwidth)
4010 break;
4011 s += (*mb_ptr2len)(s);
4013 /* Fill up for half a double-wide character. */
4014 while (++width < maxwidth)
4015 *s++ = fillchar;
4017 else
4018 #endif
4019 s = out + maxwidth - 1;
4020 for (l = 0; l < itemcnt; l++)
4021 if (item[l].start > s)
4022 break;
4023 itemcnt = l;
4024 *s++ = '>';
4025 *s = 0;
4027 else
4029 #ifdef FEAT_MBYTE
4030 if (has_mbyte)
4032 n = 0;
4033 while (width >= maxwidth)
4035 width -= ptr2cells(s + n);
4036 n += (*mb_ptr2len)(s + n);
4039 else
4040 #endif
4041 n = width - maxwidth + 1;
4042 p = s + n;
4043 STRMOVE(s + 1, p);
4044 *s = '<';
4046 /* Fill up for half a double-wide character. */
4047 while (++width < maxwidth)
4049 s = s + STRLEN(s);
4050 *s++ = fillchar;
4051 *s = NUL;
4054 --n; /* count the '<' */
4055 for (; l < itemcnt; l++)
4057 if (item[l].start - n >= s)
4058 item[l].start -= n;
4059 else
4060 item[l].start = s;
4063 width = maxwidth;
4065 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4067 /* Apply STL_MIDDLE if any */
4068 for (l = 0; l < itemcnt; l++)
4069 if (item[l].type == Middle)
4070 break;
4071 if (l < itemcnt)
4073 p = item[l].start + maxwidth - width;
4074 STRMOVE(p, item[l].start);
4075 for (s = item[l].start; s < p; s++)
4076 *s = fillchar;
4077 for (l++; l < itemcnt; l++)
4078 item[l].start += maxwidth - width;
4079 width = maxwidth;
4083 /* Store the info about highlighting. */
4084 if (hltab != NULL)
4086 sp = hltab;
4087 for (l = 0; l < itemcnt; l++)
4089 if (item[l].type == Highlight)
4091 sp->start = item[l].start;
4092 sp->userhl = item[l].minwid;
4093 sp++;
4096 sp->start = NULL;
4097 sp->userhl = 0;
4100 /* Store the info about tab pages labels. */
4101 if (tabtab != NULL)
4103 sp = tabtab;
4104 for (l = 0; l < itemcnt; l++)
4106 if (item[l].type == TabPage)
4108 sp->start = item[l].start;
4109 sp->userhl = item[l].minwid;
4110 sp++;
4113 sp->start = NULL;
4114 sp->userhl = 0;
4117 return width;
4119 #endif /* FEAT_STL_OPT */
4121 #if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4122 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
4124 * Get relative cursor position in window into "str[]", in the form 99%, using
4125 * "Top", "Bot" or "All" when appropriate.
4127 void
4128 get_rel_pos(wp, str)
4129 win_T *wp;
4130 char_u *str;
4132 long above; /* number of lines above window */
4133 long below; /* number of lines below window */
4135 above = wp->w_topline - 1;
4136 #ifdef FEAT_DIFF
4137 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
4138 #endif
4139 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4140 if (below <= 0)
4141 STRCPY(str, above == 0 ? _("All") : _("Bot"));
4142 else if (above <= 0)
4143 STRCPY(str, _("Top"));
4144 else
4145 sprintf((char *)str, "%2d%%", above > 1000000L
4146 ? (int)(above / ((above + below) / 100L))
4147 : (int)(above * 100L / (above + below)));
4149 #endif
4152 * Append (file 2 of 8) to 'buf', if editing more than one file.
4153 * Return TRUE if it was appended.
4156 append_arg_number(wp, buf, add_file, maxlen)
4157 win_T *wp;
4158 char_u *buf;
4159 int add_file; /* Add "file" before the arg number */
4160 int maxlen; /* maximum nr of chars in buf or zero*/
4162 char_u *p;
4164 if (ARGCOUNT <= 1) /* nothing to do */
4165 return FALSE;
4167 p = buf + STRLEN(buf); /* go to the end of the buffer */
4168 if (maxlen && p - buf + 35 >= maxlen) /* getting too long */
4169 return FALSE;
4170 *p++ = ' ';
4171 *p++ = '(';
4172 if (add_file)
4174 STRCPY(p, "file ");
4175 p += 5;
4177 sprintf((char *)p, wp->w_arg_idx_invalid ? "(%d) of %d)"
4178 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4179 return TRUE;
4183 * If fname is not a full path, make it a full path.
4184 * Returns pointer to allocated memory (NULL for failure).
4186 char_u *
4187 fix_fname(fname)
4188 char_u *fname;
4191 * Force expanding the path always for Unix, because symbolic links may
4192 * mess up the full path name, even though it starts with a '/'.
4193 * Also expand when there is ".." in the file name, try to remove it,
4194 * because "c:/src/../README" is equal to "c:/README".
4195 * Similarly "c:/src//file" is equal to "c:/src/file".
4196 * For MS-Windows also expand names like "longna~1" to "longname".
4198 #ifdef UNIX
4199 return FullName_save(fname, TRUE);
4200 #else
4201 if (!vim_isAbsName(fname)
4202 || strstr((char *)fname, "..") != NULL
4203 || strstr((char *)fname, "//") != NULL
4204 # ifdef BACKSLASH_IN_FILENAME
4205 || strstr((char *)fname, "\\\\") != NULL
4206 # endif
4207 # if defined(MSWIN) || defined(DJGPP)
4208 || vim_strchr(fname, '~') != NULL
4209 # endif
4211 return FullName_save(fname, FALSE);
4213 fname = vim_strsave(fname);
4215 # ifdef USE_FNAME_CASE
4216 # ifdef USE_LONG_FNAME
4217 if (USE_LONG_FNAME)
4218 # endif
4220 if (fname != NULL)
4221 fname_case(fname, 0); /* set correct case for file name */
4223 # endif
4225 return fname;
4226 #endif
4230 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4231 * "ffname" becomes a pointer to allocated memory (or NULL).
4233 /*ARGSUSED*/
4234 void
4235 fname_expand(buf, ffname, sfname)
4236 buf_T *buf;
4237 char_u **ffname;
4238 char_u **sfname;
4240 if (*ffname == NULL) /* if no file name given, nothing to do */
4241 return;
4242 if (*sfname == NULL) /* if no short file name given, use ffname */
4243 *sfname = *ffname;
4244 *ffname = fix_fname(*ffname); /* expand to full path */
4246 #ifdef FEAT_SHORTCUT
4247 if (!buf->b_p_bin)
4249 char_u *rfname;
4251 /* If the file name is a shortcut file, use the file it links to. */
4252 rfname = mch_resolve_shortcut(*ffname);
4253 if (rfname != NULL)
4255 vim_free(*ffname);
4256 *ffname = rfname;
4257 *sfname = rfname;
4260 #endif
4264 * Get the file name for an argument list entry.
4266 char_u *
4267 alist_name(aep)
4268 aentry_T *aep;
4270 buf_T *bp;
4272 /* Use the name from the associated buffer if it exists. */
4273 bp = buflist_findnr(aep->ae_fnum);
4274 if (bp == NULL || bp->b_fname == NULL)
4275 return aep->ae_fname;
4276 return bp->b_fname;
4279 #if defined(FEAT_WINDOWS) || defined(PROTO)
4281 * do_arg_all(): Open up to 'count' windows, one for each argument.
4283 void
4284 do_arg_all(count, forceit, keep_tabs)
4285 int count;
4286 int forceit; /* hide buffers in current windows */
4287 int keep_tabs; /* keep current tabs, for ":tab drop file" */
4289 int i;
4290 win_T *wp, *wpnext;
4291 char_u *opened; /* array of flags for which args are open */
4292 int opened_len; /* length of opened[] */
4293 int use_firstwin = FALSE; /* use first window for arglist */
4294 int split_ret = OK;
4295 int p_ea_save;
4296 alist_T *alist; /* argument list to be used */
4297 buf_T *buf;
4298 tabpage_T *tpnext;
4299 int had_tab = cmdmod.tab;
4300 win_T *new_curwin = NULL;
4301 tabpage_T *new_curtab = NULL;
4303 if (ARGCOUNT <= 0)
4305 /* Don't give an error message. We don't want it when the ":all"
4306 * command is in the .vimrc. */
4307 return;
4309 setpcmark();
4311 opened_len = ARGCOUNT;
4312 opened = alloc_clear((unsigned)opened_len);
4313 if (opened == NULL)
4314 return;
4316 #ifdef FEAT_GUI
4317 need_mouse_correct = TRUE;
4318 #endif
4321 * Try closing all windows that are not in the argument list.
4322 * Also close windows that are not full width;
4323 * When 'hidden' or "forceit" set the buffer becomes hidden.
4324 * Windows that have a changed buffer and can't be hidden won't be closed.
4325 * When the ":tab" modifier was used do this for all tab pages.
4327 if (had_tab > 0)
4328 goto_tabpage_tp(first_tabpage);
4329 for (;;)
4331 tpnext = curtab->tp_next;
4332 for (wp = firstwin; wp != NULL; wp = wpnext)
4334 wpnext = wp->w_next;
4335 buf = wp->w_buffer;
4336 if (buf->b_ffname == NULL
4337 || buf->b_nwindows > 1
4338 #ifdef FEAT_VERTSPLIT
4339 || wp->w_width != Columns
4340 #endif
4342 i = ARGCOUNT;
4343 else
4345 /* check if the buffer in this window is in the arglist */
4346 for (i = 0; i < ARGCOUNT; ++i)
4348 if (ARGLIST[i].ae_fnum == buf->b_fnum
4349 || fullpathcmp(alist_name(&ARGLIST[i]),
4350 buf->b_ffname, TRUE) & FPC_SAME)
4352 if (i < opened_len)
4354 opened[i] = TRUE;
4355 if (i == 0)
4357 new_curwin = wp;
4358 new_curtab = curtab;
4361 if (wp->w_alist != curwin->w_alist)
4363 /* Use the current argument list for all windows
4364 * containing a file from it. */
4365 alist_unlink(wp->w_alist);
4366 wp->w_alist = curwin->w_alist;
4367 ++wp->w_alist->al_refcount;
4369 break;
4373 wp->w_arg_idx = i;
4375 if (i == ARGCOUNT && !keep_tabs) /* close this window */
4377 if (P_HID(buf) || forceit || buf->b_nwindows > 1
4378 || !bufIsChanged(buf))
4380 /* If the buffer was changed, and we would like to hide it,
4381 * try autowriting. */
4382 if (!P_HID(buf) && buf->b_nwindows <= 1
4383 && bufIsChanged(buf))
4385 (void)autowrite(buf, FALSE);
4386 #ifdef FEAT_AUTOCMD
4387 /* check if autocommands removed the window */
4388 if (!win_valid(wp) || !buf_valid(buf))
4390 wpnext = firstwin; /* start all over... */
4391 continue;
4393 #endif
4395 #ifdef FEAT_WINDOWS
4396 /* don't close last window */
4397 if (firstwin == lastwin && first_tabpage->tp_next == NULL)
4398 #endif
4399 use_firstwin = TRUE;
4400 #ifdef FEAT_WINDOWS
4401 else
4403 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4404 # ifdef FEAT_AUTOCMD
4405 /* check if autocommands removed the next window */
4406 if (!win_valid(wpnext))
4407 wpnext = firstwin; /* start all over... */
4408 # endif
4410 #endif
4415 /* Without the ":tab" modifier only do the current tab page. */
4416 if (had_tab == 0 || tpnext == NULL)
4417 break;
4419 # ifdef FEAT_AUTOCMD
4420 /* check if autocommands removed the next tab page */
4421 if (!valid_tabpage(tpnext))
4422 tpnext = first_tabpage; /* start all over...*/
4423 # endif
4424 goto_tabpage_tp(tpnext);
4428 * Open a window for files in the argument list that don't have one.
4429 * ARGCOUNT may change while doing this, because of autocommands.
4431 if (count > ARGCOUNT || count <= 0)
4432 count = ARGCOUNT;
4434 /* Autocommands may do anything to the argument list. Make sure it's not
4435 * freed while we are working here by "locking" it. We still have to
4436 * watch out for its size to be changed. */
4437 alist = curwin->w_alist;
4438 ++alist->al_refcount;
4440 #ifdef FEAT_AUTOCMD
4441 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4442 ++autocmd_no_enter;
4443 ++autocmd_no_leave;
4444 #endif
4445 win_enter(lastwin, FALSE);
4446 #ifdef FEAT_WINDOWS
4447 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4448 * leaving an empty tab page when executed locally. */
4449 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4450 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4451 use_firstwin = TRUE;
4452 #endif
4454 for (i = 0; i < count && i < alist->al_ga.ga_len && !got_int; ++i)
4456 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4457 arg_had_last = TRUE;
4458 if (i < opened_len && opened[i])
4460 /* Move the already present window to below the current window */
4461 if (curwin->w_arg_idx != i)
4463 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4465 if (wpnext->w_arg_idx == i)
4467 win_move_after(wpnext, curwin);
4468 break;
4473 else if (split_ret == OK)
4475 if (!use_firstwin) /* split current window */
4477 p_ea_save = p_ea;
4478 p_ea = TRUE; /* use space from all windows */
4479 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4480 p_ea = p_ea_save;
4481 if (split_ret == FAIL)
4482 continue;
4484 #ifdef FEAT_AUTOCMD
4485 else /* first window: do autocmd for leaving this buffer */
4486 --autocmd_no_leave;
4487 #endif
4490 * edit file "i"
4492 curwin->w_arg_idx = i;
4493 if (i == 0)
4495 new_curwin = curwin;
4496 new_curtab = curtab;
4498 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4499 ECMD_ONE,
4500 ((P_HID(curwin->w_buffer)
4501 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
4502 + ECMD_OLDBUF);
4503 #ifdef FEAT_AUTOCMD
4504 if (use_firstwin)
4505 ++autocmd_no_leave;
4506 #endif
4507 use_firstwin = FALSE;
4509 ui_breakcheck();
4511 /* When ":tab" was used open a new tab for a new window repeatedly. */
4512 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4513 cmdmod.tab = 9999;
4516 /* Remove the "lock" on the argument list. */
4517 alist_unlink(alist);
4519 #ifdef FEAT_AUTOCMD
4520 --autocmd_no_enter;
4521 #endif
4522 /* to window with first arg */
4523 if (valid_tabpage(new_curtab))
4524 goto_tabpage_tp(new_curtab);
4525 if (win_valid(new_curwin))
4526 win_enter(new_curwin, FALSE);
4528 #ifdef FEAT_AUTOCMD
4529 --autocmd_no_leave;
4530 #endif
4531 vim_free(opened);
4534 # if defined(FEAT_LISTCMDS) || defined(PROTO)
4536 * Open a window for a number of buffers.
4538 void
4539 ex_buffer_all(eap)
4540 exarg_T *eap;
4542 buf_T *buf;
4543 win_T *wp, *wpnext;
4544 int split_ret = OK;
4545 int p_ea_save;
4546 int open_wins = 0;
4547 int r;
4548 int count; /* Maximum number of windows to open. */
4549 int all; /* When TRUE also load inactive buffers. */
4550 #ifdef FEAT_WINDOWS
4551 int had_tab = cmdmod.tab;
4552 tabpage_T *tpnext;
4553 #endif
4555 if (eap->addr_count == 0) /* make as many windows as possible */
4556 count = 9999;
4557 else
4558 count = eap->line2; /* make as many windows as specified */
4559 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4560 all = FALSE;
4561 else
4562 all = TRUE;
4564 setpcmark();
4566 #ifdef FEAT_GUI
4567 need_mouse_correct = TRUE;
4568 #endif
4571 * Close superfluous windows (two windows for the same buffer).
4572 * Also close windows that are not full-width.
4574 #ifdef FEAT_WINDOWS
4575 if (had_tab > 0)
4576 goto_tabpage_tp(first_tabpage);
4577 for (;;)
4579 #endif
4580 tpnext = curtab->tp_next;
4581 for (wp = firstwin; wp != NULL; wp = wpnext)
4583 wpnext = wp->w_next;
4584 if ((wp->w_buffer->b_nwindows > 1
4585 #ifdef FEAT_VERTSPLIT
4586 || ((cmdmod.split & WSP_VERT)
4587 ? wp->w_height + wp->w_status_height < Rows - p_ch
4588 - tabline_height()
4589 : wp->w_width != Columns)
4590 #endif
4591 #ifdef FEAT_WINDOWS
4592 || (had_tab > 0 && wp != firstwin)
4593 #endif
4594 ) && firstwin != lastwin)
4596 win_close(wp, FALSE);
4597 #ifdef FEAT_AUTOCMD
4598 wpnext = firstwin; /* just in case an autocommand does
4599 something strange with windows */
4600 tpnext = first_tabpage; /* start all over...*/
4601 open_wins = 0;
4602 #endif
4604 else
4605 ++open_wins;
4608 #ifdef FEAT_WINDOWS
4609 /* Without the ":tab" modifier only do the current tab page. */
4610 if (had_tab == 0 || tpnext == NULL)
4611 break;
4612 goto_tabpage_tp(tpnext);
4614 #endif
4617 * Go through the buffer list. When a buffer doesn't have a window yet,
4618 * open one. Otherwise move the window to the right position.
4619 * Watch out for autocommands that delete buffers or windows!
4621 #ifdef FEAT_AUTOCMD
4622 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4623 ++autocmd_no_enter;
4624 #endif
4625 win_enter(lastwin, FALSE);
4626 #ifdef FEAT_AUTOCMD
4627 ++autocmd_no_leave;
4628 #endif
4629 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4631 /* Check if this buffer needs a window */
4632 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4633 continue;
4635 #ifdef FEAT_WINDOWS
4636 if (had_tab != 0)
4638 /* With the ":tab" modifier don't move the window. */
4639 if (buf->b_nwindows > 0)
4640 wp = lastwin; /* buffer has a window, skip it */
4641 else
4642 wp = NULL;
4644 else
4645 #endif
4647 /* Check if this buffer already has a window */
4648 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4649 if (wp->w_buffer == buf)
4650 break;
4651 /* If the buffer already has a window, move it */
4652 if (wp != NULL)
4653 win_move_after(wp, curwin);
4656 if (wp == NULL && split_ret == OK)
4658 /* Split the window and put the buffer in it */
4659 p_ea_save = p_ea;
4660 p_ea = TRUE; /* use space from all windows */
4661 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4662 ++open_wins;
4663 p_ea = p_ea_save;
4664 if (split_ret == FAIL)
4665 continue;
4667 /* Open the buffer in this window. */
4668 #if defined(HAS_SWAP_EXISTS_ACTION)
4669 swap_exists_action = SEA_DIALOG;
4670 #endif
4671 set_curbuf(buf, DOBUF_GOTO);
4672 #ifdef FEAT_AUTOCMD
4673 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
4675 #if defined(HAS_SWAP_EXISTS_ACTION)
4676 swap_exists_action = SEA_NONE;
4677 # endif
4678 break;
4680 #endif
4681 #if defined(HAS_SWAP_EXISTS_ACTION)
4682 if (swap_exists_action == SEA_QUIT)
4684 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4685 cleanup_T cs;
4687 /* Reset the error/interrupt/exception state here so that
4688 * aborting() returns FALSE when closing a window. */
4689 enter_cleanup(&cs);
4690 # endif
4692 /* User selected Quit at ATTENTION prompt; close this window. */
4693 win_close(curwin, TRUE);
4694 --open_wins;
4695 swap_exists_action = SEA_NONE;
4696 swap_exists_did_quit = TRUE;
4698 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4699 /* Restore the error/interrupt/exception state if not
4700 * discarded by a new aborting error, interrupt, or uncaught
4701 * exception. */
4702 leave_cleanup(&cs);
4703 # endif
4705 else
4706 handle_swap_exists(NULL);
4707 #endif
4710 ui_breakcheck();
4711 if (got_int)
4713 (void)vgetc(); /* only break the file loading, not the rest */
4714 break;
4716 #ifdef FEAT_EVAL
4717 /* Autocommands deleted the buffer or aborted script processing!!! */
4718 if (aborting())
4719 break;
4720 #endif
4721 #ifdef FEAT_WINDOWS
4722 /* When ":tab" was used open a new tab for a new window repeatedly. */
4723 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4724 cmdmod.tab = 9999;
4725 #endif
4727 #ifdef FEAT_AUTOCMD
4728 --autocmd_no_enter;
4729 #endif
4730 win_enter(firstwin, FALSE); /* back to first window */
4731 #ifdef FEAT_AUTOCMD
4732 --autocmd_no_leave;
4733 #endif
4736 * Close superfluous windows.
4738 for (wp = lastwin; open_wins > count; )
4740 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
4741 || autowrite(wp->w_buffer, FALSE) == OK);
4742 #ifdef FEAT_AUTOCMD
4743 if (!win_valid(wp))
4745 /* BufWrite Autocommands made the window invalid, start over */
4746 wp = lastwin;
4748 else
4749 #endif
4750 if (r)
4752 win_close(wp, !P_HID(wp->w_buffer));
4753 --open_wins;
4754 wp = lastwin;
4756 else
4758 wp = wp->w_prev;
4759 if (wp == NULL)
4760 break;
4764 # endif /* FEAT_LISTCMDS */
4766 #endif /* FEAT_WINDOWS */
4768 static int chk_modeline __ARGS((linenr_T, int));
4771 * do_modelines() - process mode lines for the current file
4773 * "flags" can be:
4774 * OPT_WINONLY only set options local to window
4775 * OPT_NOWIN don't set options local to window
4777 * Returns immediately if the "ml" option isn't set.
4779 void
4780 do_modelines(flags)
4781 int flags;
4783 linenr_T lnum;
4784 int nmlines;
4785 static int entered = 0;
4787 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
4788 return;
4790 /* Disallow recursive entry here. Can happen when executing a modeline
4791 * triggers an autocommand, which reloads modelines with a ":do". */
4792 if (entered)
4793 return;
4795 ++entered;
4796 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
4797 ++lnum)
4798 if (chk_modeline(lnum, flags) == FAIL)
4799 nmlines = 0;
4801 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
4802 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
4803 if (chk_modeline(lnum, flags) == FAIL)
4804 nmlines = 0;
4805 --entered;
4808 #include "version.h" /* for version number */
4811 * chk_modeline() - check a single line for a mode string
4812 * Return FAIL if an error encountered.
4814 static int
4815 chk_modeline(lnum, flags)
4816 linenr_T lnum;
4817 int flags; /* Same as for do_modelines(). */
4819 char_u *s;
4820 char_u *e;
4821 char_u *linecopy; /* local copy of any modeline found */
4822 int prev;
4823 int vers;
4824 int end;
4825 int retval = OK;
4826 char_u *save_sourcing_name;
4827 linenr_T save_sourcing_lnum;
4828 #ifdef FEAT_EVAL
4829 scid_T save_SID;
4830 #endif
4832 prev = -1;
4833 for (s = ml_get(lnum); *s != NUL; ++s)
4835 if (prev == -1 || vim_isspace(prev))
4837 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
4838 || STRNCMP(s, "vi:", (size_t)3) == 0)
4839 break;
4840 if (STRNCMP(s, "vim", 3) == 0)
4842 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
4843 e = s + 4;
4844 else
4845 e = s + 3;
4846 vers = getdigits(&e);
4847 if (*e == ':'
4848 && (s[3] == ':'
4849 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
4850 || (VIM_VERSION_100 < vers && s[3] == '<')
4851 || (VIM_VERSION_100 > vers && s[3] == '>')
4852 || (VIM_VERSION_100 == vers && s[3] == '=')))
4853 break;
4856 prev = *s;
4859 if (*s)
4861 do /* skip over "ex:", "vi:" or "vim:" */
4862 ++s;
4863 while (s[-1] != ':');
4865 s = linecopy = vim_strsave(s); /* copy the line, it will change */
4866 if (linecopy == NULL)
4867 return FAIL;
4869 save_sourcing_lnum = sourcing_lnum;
4870 save_sourcing_name = sourcing_name;
4871 sourcing_lnum = lnum; /* prepare for emsg() */
4872 sourcing_name = (char_u *)"modelines";
4874 end = FALSE;
4875 while (end == FALSE)
4877 s = skipwhite(s);
4878 if (*s == NUL)
4879 break;
4882 * Find end of set command: ':' or end of line.
4883 * Skip over "\:", replacing it with ":".
4885 for (e = s; *e != ':' && *e != NUL; ++e)
4886 if (e[0] == '\\' && e[1] == ':')
4887 STRMOVE(e, e + 1);
4888 if (*e == NUL)
4889 end = TRUE;
4892 * If there is a "set" command, require a terminating ':' and
4893 * ignore the stuff after the ':'.
4894 * "vi:set opt opt opt: foo" -- foo not interpreted
4895 * "vi:opt opt opt: foo" -- foo interpreted
4896 * Accept "se" for compatibility with Elvis.
4898 if (STRNCMP(s, "set ", (size_t)4) == 0
4899 || STRNCMP(s, "se ", (size_t)3) == 0)
4901 if (*e != ':') /* no terminating ':'? */
4902 break;
4903 end = TRUE;
4904 s = vim_strchr(s, ' ') + 1;
4906 *e = NUL; /* truncate the set command */
4908 if (*s != NUL) /* skip over an empty "::" */
4910 #ifdef FEAT_EVAL
4911 save_SID = current_SID;
4912 current_SID = SID_MODELINE;
4913 #endif
4914 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
4915 #ifdef FEAT_EVAL
4916 current_SID = save_SID;
4917 #endif
4918 if (retval == FAIL) /* stop if error found */
4919 break;
4921 s = e + 1; /* advance to next part */
4924 sourcing_lnum = save_sourcing_lnum;
4925 sourcing_name = save_sourcing_name;
4927 vim_free(linecopy);
4929 return retval;
4932 #if defined(FEAT_VIMINFO) || defined(PROTO)
4934 read_viminfo_bufferlist(virp, writing)
4935 vir_T *virp;
4936 int writing;
4938 char_u *tab;
4939 linenr_T lnum;
4940 colnr_T col;
4941 buf_T *buf;
4942 char_u *sfname;
4943 char_u *xline;
4945 /* Handle long line and escaped characters. */
4946 xline = viminfo_readstring(virp, 1, FALSE);
4948 /* don't read in if there are files on the command-line or if writing: */
4949 if (xline != NULL && !writing && ARGCOUNT == 0
4950 && find_viminfo_parameter('%') != NULL)
4952 /* Format is: <fname> Tab <lnum> Tab <col>.
4953 * Watch out for a Tab in the file name, work from the end. */
4954 lnum = 0;
4955 col = 0;
4956 tab = vim_strrchr(xline, '\t');
4957 if (tab != NULL)
4959 *tab++ = '\0';
4960 col = atoi((char *)tab);
4961 tab = vim_strrchr(xline, '\t');
4962 if (tab != NULL)
4964 *tab++ = '\0';
4965 lnum = atol((char *)tab);
4969 /* Expand "~/" in the file name at "line + 1" to a full path.
4970 * Then try shortening it by comparing with the current directory */
4971 expand_env(xline, NameBuff, MAXPATHL);
4972 sfname = shorten_fname1(NameBuff);
4974 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
4975 if (buf != NULL) /* just in case... */
4977 buf->b_last_cursor.lnum = lnum;
4978 buf->b_last_cursor.col = col;
4979 buflist_setfpos(buf, curwin, lnum, col, FALSE);
4982 vim_free(xline);
4984 return viminfo_readline(virp);
4987 void
4988 write_viminfo_bufferlist(fp)
4989 FILE *fp;
4991 buf_T *buf;
4992 #ifdef FEAT_WINDOWS
4993 win_T *win;
4994 tabpage_T *tp;
4995 #endif
4996 char_u *line;
4997 int max_buffers;
4999 if (find_viminfo_parameter('%') == NULL)
5000 return;
5002 /* Without a number -1 is returned: do all buffers. */
5003 max_buffers = get_viminfo_parameter('%');
5005 /* Allocate room for the file name, lnum and col. */
5006 line = alloc(MAXPATHL + 40);
5007 if (line == NULL)
5008 return;
5010 #ifdef FEAT_WINDOWS
5011 FOR_ALL_TAB_WINDOWS(tp, win)
5012 set_last_cursor(win);
5013 #else
5014 set_last_cursor(curwin);
5015 #endif
5017 fprintf(fp, _("\n# Buffer list:\n"));
5018 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5020 if (buf->b_fname == NULL
5021 || !buf->b_p_bl
5022 #ifdef FEAT_QUICKFIX
5023 || bt_quickfix(buf)
5024 #endif
5025 || removable(buf->b_ffname))
5026 continue;
5028 if (max_buffers-- == 0)
5029 break;
5030 putc('%', fp);
5031 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
5032 sprintf((char *)line + STRLEN(line), "\t%ld\t%d",
5033 (long)buf->b_last_cursor.lnum,
5034 buf->b_last_cursor.col);
5035 viminfo_writestring(fp, line);
5037 vim_free(line);
5039 #endif
5043 * Return special buffer name.
5044 * Returns NULL when the buffer has a normal file name.
5046 char *
5047 buf_spname(buf)
5048 buf_T *buf;
5050 #if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5051 if (bt_quickfix(buf))
5053 win_T *win = NULL;
5054 tabpage_T *tp;
5057 * For location list window, w_llist_ref points to the location list.
5058 * For quickfix window, w_llist_ref is NULL.
5060 FOR_ALL_TAB_WINDOWS(tp, win)
5061 if (win->w_buffer == buf)
5062 break;
5063 if (win != NULL && win->w_llist_ref != NULL)
5064 return _("[Location List]");
5065 else
5066 return _("[Quickfix List]");
5068 #endif
5069 #ifdef FEAT_QUICKFIX
5070 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5071 * contains the name as specified by the user */
5072 if (bt_nofile(buf))
5074 if (buf->b_sfname != NULL)
5075 return (char *)buf->b_sfname;
5076 return "[Scratch]";
5078 #endif
5079 if (buf->b_fname == NULL)
5080 return _("[No Name]");
5081 return NULL;
5085 #if defined(FEAT_SIGNS) || defined(PROTO)
5087 * Insert the sign into the signlist.
5089 static void
5090 insert_sign(buf, prev, next, id, lnum, typenr)
5091 buf_T *buf; /* buffer to store sign in */
5092 signlist_T *prev; /* previous sign entry */
5093 signlist_T *next; /* next sign entry */
5094 int id; /* sign ID */
5095 linenr_T lnum; /* line number which gets the mark */
5096 int typenr; /* typenr of sign we are adding */
5098 signlist_T *newsign;
5100 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5101 if (newsign != NULL)
5103 newsign->id = id;
5104 newsign->lnum = lnum;
5105 newsign->typenr = typenr;
5106 newsign->next = next;
5107 #ifdef FEAT_NETBEANS_INTG
5108 newsign->prev = prev;
5109 if (next != NULL)
5110 next->prev = newsign;
5111 #endif
5113 if (prev == NULL)
5115 /* When adding first sign need to redraw the windows to create the
5116 * column for signs. */
5117 if (buf->b_signlist == NULL)
5119 redraw_buf_later(buf, NOT_VALID);
5120 changed_cline_bef_curs();
5123 /* first sign in signlist */
5124 buf->b_signlist = newsign;
5126 else
5127 prev->next = newsign;
5132 * Add the sign into the signlist. Find the right spot to do it though.
5134 void
5135 buf_addsign(buf, id, lnum, typenr)
5136 buf_T *buf; /* buffer to store sign in */
5137 int id; /* sign ID */
5138 linenr_T lnum; /* line number which gets the mark */
5139 int typenr; /* typenr of sign we are adding */
5141 signlist_T *sign; /* a sign in the signlist */
5142 signlist_T *prev; /* the previous sign */
5144 prev = NULL;
5145 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5147 if (lnum == sign->lnum && id == sign->id)
5149 sign->typenr = typenr;
5150 return;
5152 else if (
5153 #ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5154 id < 0 &&
5155 #endif
5156 lnum < sign->lnum)
5158 #ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5159 /* XXX - GRP: Is this because of sign slide problem? Or is it
5160 * really needed? Or is it because we allow multiple signs per
5161 * line? If so, should I add that feature to FEAT_SIGNS?
5163 while (prev != NULL && prev->lnum == lnum)
5164 prev = prev->prev;
5165 if (prev == NULL)
5166 sign = buf->b_signlist;
5167 else
5168 sign = prev->next;
5169 #endif
5170 insert_sign(buf, prev, sign, id, lnum, typenr);
5171 return;
5173 prev = sign;
5175 #ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5176 /* XXX - GRP: See previous comment */
5177 while (prev != NULL && prev->lnum == lnum)
5178 prev = prev->prev;
5179 if (prev == NULL)
5180 sign = buf->b_signlist;
5181 else
5182 sign = prev->next;
5183 #endif
5184 insert_sign(buf, prev, sign, id, lnum, typenr);
5186 return;
5190 buf_change_sign_type(buf, markId, typenr)
5191 buf_T *buf; /* buffer to store sign in */
5192 int markId; /* sign ID */
5193 int typenr; /* typenr of sign we are adding */
5195 signlist_T *sign; /* a sign in the signlist */
5197 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5199 if (sign->id == markId)
5201 sign->typenr = typenr;
5202 return sign->lnum;
5206 return 0;
5209 int_u
5210 buf_getsigntype(buf, lnum, type)
5211 buf_T *buf;
5212 linenr_T lnum;
5213 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5215 signlist_T *sign; /* a sign in a b_signlist */
5217 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5218 if (sign->lnum == lnum
5219 && (type == SIGN_ANY
5220 # ifdef FEAT_SIGN_ICONS
5221 || (type == SIGN_ICON
5222 && sign_get_image(sign->typenr) != NULL)
5223 # endif
5224 || (type == SIGN_TEXT
5225 && sign_get_text(sign->typenr) != NULL)
5226 || (type == SIGN_LINEHL
5227 && sign_get_attr(sign->typenr, TRUE) != 0)))
5228 return sign->typenr;
5229 return 0;
5233 linenr_T
5234 buf_delsign(buf, id)
5235 buf_T *buf; /* buffer sign is stored in */
5236 int id; /* sign id */
5238 signlist_T **lastp; /* pointer to pointer to current sign */
5239 signlist_T *sign; /* a sign in a b_signlist */
5240 signlist_T *next; /* the next sign in a b_signlist */
5241 linenr_T lnum; /* line number whose sign was deleted */
5243 lastp = &buf->b_signlist;
5244 lnum = 0;
5245 for (sign = buf->b_signlist; sign != NULL; sign = next)
5247 next = sign->next;
5248 if (sign->id == id)
5250 *lastp = next;
5251 #ifdef FEAT_NETBEANS_INTG
5252 if (next != NULL)
5253 next->prev = sign->prev;
5254 #endif
5255 lnum = sign->lnum;
5256 vim_free(sign);
5257 break;
5259 else
5260 lastp = &sign->next;
5263 /* When deleted the last sign need to redraw the windows to remove the
5264 * sign column. */
5265 if (buf->b_signlist == NULL)
5267 redraw_buf_later(buf, NOT_VALID);
5268 changed_cline_bef_curs();
5271 return lnum;
5276 * Find the line number of the sign with the requested id. If the sign does
5277 * not exist, return 0 as the line number. This will still let the correct file
5278 * get loaded.
5281 buf_findsign(buf, id)
5282 buf_T *buf; /* buffer to store sign in */
5283 int id; /* sign ID */
5285 signlist_T *sign; /* a sign in the signlist */
5287 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5288 if (sign->id == id)
5289 return sign->lnum;
5291 return 0;
5295 buf_findsign_id(buf, lnum)
5296 buf_T *buf; /* buffer whose sign we are searching for */
5297 linenr_T lnum; /* line number of sign */
5299 signlist_T *sign; /* a sign in the signlist */
5301 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5302 if (sign->lnum == lnum)
5303 return sign->id;
5305 return 0;
5309 # if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5310 /* see if a given type of sign exists on a specific line */
5312 buf_findsigntype_id(buf, lnum, typenr)
5313 buf_T *buf; /* buffer whose sign we are searching for */
5314 linenr_T lnum; /* line number of sign */
5315 int typenr; /* sign type number */
5317 signlist_T *sign; /* a sign in the signlist */
5319 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5320 if (sign->lnum == lnum && sign->typenr == typenr)
5321 return sign->id;
5323 return 0;
5327 # if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5328 /* return the number of icons on the given line */
5330 buf_signcount(buf, lnum)
5331 buf_T *buf;
5332 linenr_T lnum;
5334 signlist_T *sign; /* a sign in the signlist */
5335 int count = 0;
5337 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5338 if (sign->lnum == lnum)
5339 if (sign_get_image(sign->typenr) != NULL)
5340 count++;
5342 return count;
5344 # endif /* FEAT_SIGN_ICONS */
5345 # endif /* FEAT_NETBEANS_INTG */
5349 * Delete signs in buffer "buf".
5351 static void
5352 buf_delete_signs(buf)
5353 buf_T *buf;
5355 signlist_T *next;
5357 while (buf->b_signlist != NULL)
5359 next = buf->b_signlist->next;
5360 vim_free(buf->b_signlist);
5361 buf->b_signlist = next;
5366 * Delete all signs in all buffers.
5368 void
5369 buf_delete_all_signs()
5371 buf_T *buf; /* buffer we are checking for signs */
5373 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5374 if (buf->b_signlist != NULL)
5376 /* Need to redraw the windows to remove the sign column. */
5377 redraw_buf_later(buf, NOT_VALID);
5378 buf_delete_signs(buf);
5383 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5385 void
5386 sign_list_placed(rbuf)
5387 buf_T *rbuf;
5389 buf_T *buf;
5390 signlist_T *p;
5391 char lbuf[BUFSIZ];
5393 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5394 msg_putchar('\n');
5395 if (rbuf == NULL)
5396 buf = firstbuf;
5397 else
5398 buf = rbuf;
5399 while (buf != NULL)
5401 if (buf->b_signlist != NULL)
5403 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
5404 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5405 msg_putchar('\n');
5407 for (p = buf->b_signlist; p != NULL; p = p->next)
5409 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
5410 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5411 MSG_PUTS(lbuf);
5412 msg_putchar('\n');
5414 if (rbuf != NULL)
5415 break;
5416 buf = buf->b_next;
5421 * Adjust a placed sign for inserted/deleted lines.
5423 void
5424 sign_mark_adjust(line1, line2, amount, amount_after)
5425 linenr_T line1;
5426 linenr_T line2;
5427 long amount;
5428 long amount_after;
5430 signlist_T *sign; /* a sign in a b_signlist */
5432 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5434 if (sign->lnum >= line1 && sign->lnum <= line2)
5436 if (amount == MAXLNUM)
5437 sign->lnum = line1;
5438 else
5439 sign->lnum += amount;
5441 else if (sign->lnum > line2)
5442 sign->lnum += amount_after;
5445 #endif /* FEAT_SIGNS */
5448 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5450 void
5451 set_buflisted(on)
5452 int on;
5454 if (on != curbuf->b_p_bl)
5456 curbuf->b_p_bl = on;
5457 #ifdef FEAT_AUTOCMD
5458 if (on)
5459 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5460 else
5461 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5462 #endif
5467 * Read the file for "buf" again and check if the contents changed.
5468 * Return TRUE if it changed or this could not be checked.
5471 buf_contents_changed(buf)
5472 buf_T *buf;
5474 buf_T *newbuf;
5475 int differ = TRUE;
5476 linenr_T lnum;
5477 aco_save_T aco;
5478 exarg_T ea;
5480 /* Allocate a buffer without putting it in the buffer list. */
5481 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5482 if (newbuf == NULL)
5483 return TRUE;
5485 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5486 if (prep_exarg(&ea, buf) == FAIL)
5488 wipe_buffer(newbuf, FALSE);
5489 return TRUE;
5492 /* set curwin/curbuf to buf and save a few things */
5493 aucmd_prepbuf(&aco, newbuf);
5495 if (ml_open(curbuf) == OK
5496 && readfile(buf->b_ffname, buf->b_fname,
5497 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5498 &ea, READ_NEW | READ_DUMMY) == OK)
5500 /* compare the two files line by line */
5501 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5503 differ = FALSE;
5504 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5505 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5507 differ = TRUE;
5508 break;
5512 vim_free(ea.cmd);
5514 /* restore curwin/curbuf and a few other things */
5515 aucmd_restbuf(&aco);
5517 if (curbuf != newbuf) /* safety check */
5518 wipe_buffer(newbuf, FALSE);
5520 return differ;
5524 * Wipe out a buffer and decrement the last buffer number if it was used for
5525 * this buffer. Call this to wipe out a temp buffer that does not contain any
5526 * marks.
5528 /*ARGSUSED*/
5529 void
5530 wipe_buffer(buf, aucmd)
5531 buf_T *buf;
5532 int aucmd; /* When TRUE trigger autocommands. */
5534 if (buf->b_fnum == top_file_num - 1)
5535 --top_file_num;
5537 #ifdef FEAT_AUTOCMD
5538 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
5539 block_autocmds();
5540 #endif
5541 close_buffer(NULL, buf, DOBUF_WIPE);
5542 #ifdef FEAT_AUTOCMD
5543 if (!aucmd)
5544 unblock_autocmds();
5545 #endif