ODB Editor protocol (aka 'external editor') support
[MacVim.git] / src / buffer.c
blob7cddc11f936561228adf04dbadf75b3e3dc799c7
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 loose.
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 /* jump to first window containing buf if one exists ("useopen") */
1241 if (vim_strchr(p_swb, 'o') != NULL && buf_jump_open_win(buf))
1242 return OK;
1243 /* jump to first window in any tab page containing buf if one exists
1244 * ("usetab") */
1245 if (vim_strchr(p_swb, 'a') != NULL && buf_jump_open_tab(buf))
1246 return OK;
1247 if (win_split(0, 0) == FAIL)
1248 # endif
1249 return FAIL;
1251 #endif
1253 /* go to current buffer - nothing to do */
1254 if (buf == curbuf)
1255 return OK;
1258 * Check if the current buffer may be abandoned.
1260 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1262 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1263 if ((p_confirm || cmdmod.confirm) && p_write)
1265 dialog_changed(curbuf, FALSE);
1266 # ifdef FEAT_AUTOCMD
1267 if (!buf_valid(buf))
1268 /* Autocommand deleted buffer, oops! */
1269 return FAIL;
1270 # endif
1272 if (bufIsChanged(curbuf))
1273 #endif
1275 EMSG(_(e_nowrtmsg));
1276 return FAIL;
1280 /* Go to the other buffer. */
1281 set_curbuf(buf, action);
1283 #if defined(FEAT_LISTCMDS) && defined(FEAT_SCROLLBIND)
1284 if (action == DOBUF_SPLIT)
1285 curwin->w_p_scb = FALSE; /* reset 'scrollbind' */
1286 #endif
1288 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1289 if (aborting()) /* autocmds may abort script processing */
1290 return FAIL;
1291 #endif
1293 return OK;
1296 #endif /* FEAT_LISTCMDS */
1299 * Set current buffer to "buf". Executes autocommands and closes current
1300 * buffer. "action" tells how to close the current buffer:
1301 * DOBUF_GOTO free or hide it
1302 * DOBUF_SPLIT nothing
1303 * DOBUF_UNLOAD unload it
1304 * DOBUF_DEL delete it
1305 * DOBUF_WIPE wipe it out
1307 void
1308 set_curbuf(buf, action)
1309 buf_T *buf;
1310 int action;
1312 buf_T *prevbuf;
1313 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1314 || action == DOBUF_WIPE);
1316 setpcmark();
1317 if (!cmdmod.keepalt)
1318 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
1319 buflist_altfpos(); /* remember curpos */
1321 #ifdef FEAT_VISUAL
1322 /* Don't restart Select mode after switching to another buffer. */
1323 VIsual_reselect = FALSE;
1324 #endif
1326 /* close_windows() or apply_autocmds() may change curbuf */
1327 prevbuf = curbuf;
1329 #ifdef FEAT_AUTOCMD
1330 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1331 # ifdef FEAT_EVAL
1332 if (buf_valid(prevbuf) && !aborting())
1333 # else
1334 if (buf_valid(prevbuf))
1335 # endif
1336 #endif
1338 #ifdef FEAT_WINDOWS
1339 if (unload)
1340 close_windows(prevbuf, FALSE);
1341 #endif
1342 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1343 if (buf_valid(prevbuf) && !aborting())
1344 #else
1345 if (buf_valid(prevbuf))
1346 #endif
1348 if (prevbuf == curbuf)
1349 u_sync(FALSE);
1350 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1351 unload ? action : (action == DOBUF_GOTO
1352 && !P_HID(prevbuf)
1353 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0);
1356 #ifdef FEAT_AUTOCMD
1357 # ifdef FEAT_EVAL
1358 /* An autocommand may have deleted buf or aborted the script processing! */
1359 if (buf_valid(buf) && !aborting())
1360 # else
1361 if (buf_valid(buf)) /* an autocommand may have deleted buf! */
1362 # endif
1363 #endif
1364 enter_buffer(buf);
1368 * Enter a new current buffer.
1369 * Old curbuf must have been abandoned already!
1371 void
1372 enter_buffer(buf)
1373 buf_T *buf;
1375 /* Copy buffer and window local option values. Not for a help buffer. */
1376 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1377 if (!buf->b_help)
1378 get_winopts(buf);
1379 #ifdef FEAT_FOLDING
1380 else
1381 /* Remove all folds in the window. */
1382 clearFolding(curwin);
1383 foldUpdateAll(curwin); /* update folds (later). */
1384 #endif
1386 /* Get the buffer in the current window. */
1387 curwin->w_buffer = buf;
1388 curbuf = buf;
1389 ++curbuf->b_nwindows;
1391 #ifdef FEAT_DIFF
1392 if (curwin->w_p_diff)
1393 diff_buf_add(curbuf);
1394 #endif
1396 /* Cursor on first line by default. */
1397 curwin->w_cursor.lnum = 1;
1398 curwin->w_cursor.col = 0;
1399 #ifdef FEAT_VIRTUALEDIT
1400 curwin->w_cursor.coladd = 0;
1401 #endif
1402 curwin->w_set_curswant = TRUE;
1404 /* Make sure the buffer is loaded. */
1405 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
1407 #ifdef FEAT_AUTOCMD
1408 /* If there is no filetype, allow for detecting one. Esp. useful for
1409 * ":ball" used in a autocommand. If there already is a filetype we
1410 * might prefer to keep it. */
1411 if (*curbuf->b_p_ft == NUL)
1412 did_filetype = FALSE;
1413 #endif
1415 open_buffer(FALSE, NULL);
1417 else
1419 if (!msg_silent)
1420 need_fileinfo = TRUE; /* display file info after redraw */
1421 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1422 #ifdef FEAT_AUTOCMD
1423 curwin->w_topline = 1;
1424 # ifdef FEAT_DIFF
1425 curwin->w_topfill = 0;
1426 # endif
1427 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1428 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1429 #endif
1432 /* If autocommands did not change the cursor position, restore cursor lnum
1433 * and possibly cursor col. */
1434 if (curwin->w_cursor.lnum == 1 && inindent(0))
1435 buflist_getfpos();
1437 check_arg_idx(curwin); /* check for valid arg_idx */
1438 #ifdef FEAT_TITLE
1439 maketitle();
1440 #endif
1441 #ifdef FEAT_AUTOCMD
1442 if (curwin->w_topline == 1) /* when autocmds didn't change it */
1443 #endif
1444 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1446 #ifdef FEAT_NETBEANS_INTG
1447 /* Send fileOpened event because we've changed buffers. */
1448 if (usingNetbeans && isNetbeansBuffer(curbuf))
1449 netbeans_file_activated(curbuf);
1450 #endif
1452 /* Change directories when the 'acd' option is set. */
1453 DO_AUTOCHDIR
1455 #ifdef FEAT_KEYMAP
1456 if (curbuf->b_kmap_state & KEYMAP_INIT)
1457 keymap_init();
1458 #endif
1459 #ifdef FEAT_SPELL
1460 /* May need to set the spell language. Can only do this after the buffer
1461 * has been properly setup. */
1462 if (!curbuf->b_help && curwin->w_p_spell && *curbuf->b_p_spl != NUL)
1463 did_set_spelllang(curbuf);
1464 #endif
1466 redraw_later(NOT_VALID);
1469 #if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1471 * Change to the directory of the current buffer.
1473 void
1474 do_autochdir()
1476 if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK)
1477 shorten_fnames(TRUE);
1479 #endif
1482 * functions for dealing with the buffer list
1486 * Add a file name to the buffer list. Return a pointer to the buffer.
1487 * If the same file name already exists return a pointer to that buffer.
1488 * If it does not exist, or if fname == NULL, a new entry is created.
1489 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1490 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1491 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
1492 * This is the ONLY way to create a new buffer.
1494 static int top_file_num = 1; /* highest file number */
1496 buf_T *
1497 buflist_new(ffname, sfname, lnum, flags)
1498 char_u *ffname; /* full path of fname or relative */
1499 char_u *sfname; /* short fname or NULL */
1500 linenr_T lnum; /* preferred cursor line */
1501 int flags; /* BLN_ defines */
1503 buf_T *buf;
1504 #ifdef UNIX
1505 struct stat st;
1506 #endif
1508 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1511 * If file name already exists in the list, update the entry.
1513 #ifdef UNIX
1514 /* On Unix we can use inode numbers when the file exists. Works better
1515 * for hard links. */
1516 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1517 st.st_dev = (dev_T)-1;
1518 #endif
1519 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1520 #ifdef UNIX
1521 buflist_findname_stat(ffname, &st)
1522 #else
1523 buflist_findname(ffname)
1524 #endif
1525 ) != NULL)
1527 vim_free(ffname);
1528 if (lnum != 0)
1529 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1530 /* copy the options now, if 'cpo' doesn't have 's' and not done
1531 * already */
1532 buf_copy_options(buf, 0);
1533 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1535 buf->b_p_bl = TRUE;
1536 #ifdef FEAT_AUTOCMD
1537 if (!(flags & BLN_DUMMY))
1538 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1539 #endif
1541 return buf;
1545 * If the current buffer has no name and no contents, use the current
1546 * buffer. Otherwise: Need to allocate a new buffer structure.
1548 * This is the ONLY place where a new buffer structure is allocated!
1549 * (A spell file buffer is allocated in spell.c, but that's not a normal
1550 * buffer.)
1552 buf = NULL;
1553 if ((flags & BLN_CURBUF)
1554 && curbuf != NULL
1555 && curbuf->b_ffname == NULL
1556 && curbuf->b_nwindows <= 1
1557 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1559 buf = curbuf;
1560 #ifdef FEAT_AUTOCMD
1561 /* It's like this buffer is deleted. Watch out for autocommands that
1562 * change curbuf! If that happens, allocate a new buffer anyway. */
1563 if (curbuf->b_p_bl)
1564 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1565 if (buf == curbuf)
1566 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1567 # ifdef FEAT_EVAL
1568 if (aborting()) /* autocmds may abort script processing */
1569 return NULL;
1570 # endif
1571 #endif
1572 #ifdef FEAT_QUICKFIX
1573 # ifdef FEAT_AUTOCMD
1574 if (buf == curbuf)
1575 # endif
1577 /* Make sure 'bufhidden' and 'buftype' are empty */
1578 clear_string_option(&buf->b_p_bh);
1579 clear_string_option(&buf->b_p_bt);
1581 #endif
1583 if (buf != curbuf || curbuf == NULL)
1585 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1586 if (buf == NULL)
1588 vim_free(ffname);
1589 return NULL;
1593 if (ffname != NULL)
1595 buf->b_ffname = ffname;
1596 buf->b_sfname = vim_strsave(sfname);
1599 clear_wininfo(buf);
1600 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1602 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1603 || buf->b_wininfo == NULL)
1605 vim_free(buf->b_ffname);
1606 buf->b_ffname = NULL;
1607 vim_free(buf->b_sfname);
1608 buf->b_sfname = NULL;
1609 if (buf != curbuf)
1610 free_buffer(buf);
1611 return NULL;
1614 if (buf == curbuf)
1616 /* free all things allocated for this buffer */
1617 buf_freeall(buf, FALSE, FALSE);
1618 if (buf != curbuf) /* autocommands deleted the buffer! */
1619 return NULL;
1620 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1621 if (aborting()) /* autocmds may abort script processing */
1622 return NULL;
1623 #endif
1624 /* buf->b_nwindows = 0; why was this here? */
1625 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
1626 #ifdef FEAT_KEYMAP
1627 /* need to reload lmaps and set b:keymap_name */
1628 curbuf->b_kmap_state |= KEYMAP_INIT;
1629 #endif
1631 else
1634 * put new buffer at the end of the buffer list
1636 buf->b_next = NULL;
1637 if (firstbuf == NULL) /* buffer list is empty */
1639 buf->b_prev = NULL;
1640 firstbuf = buf;
1642 else /* append new buffer at end of list */
1644 lastbuf->b_next = buf;
1645 buf->b_prev = lastbuf;
1647 lastbuf = buf;
1649 buf->b_fnum = top_file_num++;
1650 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1652 EMSG(_("W14: Warning: List of file names overflow"));
1653 if (emsg_silent == 0)
1655 out_flush();
1656 ui_delay(3000L, TRUE); /* make sure it is noticed */
1658 top_file_num = 1;
1662 * Always copy the options from the current buffer.
1664 buf_copy_options(buf, BCO_ALWAYS);
1667 buf->b_wininfo->wi_fpos.lnum = lnum;
1668 buf->b_wininfo->wi_win = curwin;
1670 #ifdef FEAT_EVAL
1671 init_var_dict(&buf->b_vars, &buf->b_bufvar); /* init b: variables */
1672 #endif
1673 #ifdef FEAT_SYN_HL
1674 hash_init(&buf->b_keywtab);
1675 hash_init(&buf->b_keywtab_ic);
1676 #endif
1678 buf->b_fname = buf->b_sfname;
1679 #ifdef UNIX
1680 if (st.st_dev == (dev_T)-1)
1681 buf->b_dev = -1;
1682 else
1684 buf->b_dev = st.st_dev;
1685 buf->b_ino = st.st_ino;
1687 #endif
1688 buf->b_u_synced = TRUE;
1689 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
1690 if (flags & BLN_DUMMY)
1691 buf->b_flags |= BF_DUMMY;
1692 buf_clear_file(buf);
1693 clrallmarks(buf); /* clear marks */
1694 fmarks_check_names(buf); /* check file marks for this file */
1695 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1696 #ifdef FEAT_AUTOCMD
1697 if (!(flags & BLN_DUMMY))
1699 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
1700 if (flags & BLN_LISTED)
1701 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1702 # ifdef FEAT_EVAL
1703 if (aborting()) /* autocmds may abort script processing */
1704 return NULL;
1705 # endif
1707 #endif
1709 return buf;
1713 * Free the memory for the options of a buffer.
1714 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1715 * 'fileencoding'.
1717 void
1718 free_buf_options(buf, free_p_ff)
1719 buf_T *buf;
1720 int free_p_ff;
1722 if (free_p_ff)
1724 #ifdef FEAT_MBYTE
1725 clear_string_option(&buf->b_p_fenc);
1726 #endif
1727 clear_string_option(&buf->b_p_ff);
1728 #ifdef FEAT_QUICKFIX
1729 clear_string_option(&buf->b_p_bh);
1730 clear_string_option(&buf->b_p_bt);
1731 #endif
1733 #ifdef FEAT_FIND_ID
1734 clear_string_option(&buf->b_p_def);
1735 clear_string_option(&buf->b_p_inc);
1736 # ifdef FEAT_EVAL
1737 clear_string_option(&buf->b_p_inex);
1738 # endif
1739 #endif
1740 #if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1741 clear_string_option(&buf->b_p_inde);
1742 clear_string_option(&buf->b_p_indk);
1743 #endif
1744 #if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1745 clear_string_option(&buf->b_p_bexpr);
1746 #endif
1747 #if defined(FEAT_EVAL)
1748 clear_string_option(&buf->b_p_fex);
1749 #endif
1750 #ifdef FEAT_CRYPT
1751 clear_string_option(&buf->b_p_key);
1752 #endif
1753 clear_string_option(&buf->b_p_kp);
1754 clear_string_option(&buf->b_p_mps);
1755 clear_string_option(&buf->b_p_fo);
1756 clear_string_option(&buf->b_p_flp);
1757 clear_string_option(&buf->b_p_isk);
1758 #ifdef FEAT_KEYMAP
1759 clear_string_option(&buf->b_p_keymap);
1760 ga_clear(&buf->b_kmap_ga);
1761 #endif
1762 #ifdef FEAT_COMMENTS
1763 clear_string_option(&buf->b_p_com);
1764 #endif
1765 #ifdef FEAT_FOLDING
1766 clear_string_option(&buf->b_p_cms);
1767 #endif
1768 clear_string_option(&buf->b_p_nf);
1769 #ifdef FEAT_SYN_HL
1770 clear_string_option(&buf->b_p_syn);
1771 #endif
1772 #ifdef FEAT_SPELL
1773 clear_string_option(&buf->b_p_spc);
1774 clear_string_option(&buf->b_p_spf);
1775 vim_free(buf->b_cap_prog);
1776 buf->b_cap_prog = NULL;
1777 clear_string_option(&buf->b_p_spl);
1778 #endif
1779 #ifdef FEAT_SEARCHPATH
1780 clear_string_option(&buf->b_p_sua);
1781 #endif
1782 #ifdef FEAT_AUTOCMD
1783 clear_string_option(&buf->b_p_ft);
1784 #endif
1785 #ifdef FEAT_OSFILETYPE
1786 clear_string_option(&buf->b_p_oft);
1787 #endif
1788 #ifdef FEAT_CINDENT
1789 clear_string_option(&buf->b_p_cink);
1790 clear_string_option(&buf->b_p_cino);
1791 #endif
1792 #if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1793 clear_string_option(&buf->b_p_cinw);
1794 #endif
1795 #ifdef FEAT_INS_EXPAND
1796 clear_string_option(&buf->b_p_cpt);
1797 #endif
1798 #ifdef FEAT_COMPL_FUNC
1799 clear_string_option(&buf->b_p_cfu);
1800 clear_string_option(&buf->b_p_ofu);
1801 #endif
1802 #ifdef FEAT_QUICKFIX
1803 clear_string_option(&buf->b_p_gp);
1804 clear_string_option(&buf->b_p_mp);
1805 clear_string_option(&buf->b_p_efm);
1806 #endif
1807 clear_string_option(&buf->b_p_ep);
1808 clear_string_option(&buf->b_p_path);
1809 clear_string_option(&buf->b_p_tags);
1810 #ifdef FEAT_INS_EXPAND
1811 clear_string_option(&buf->b_p_dict);
1812 clear_string_option(&buf->b_p_tsr);
1813 #endif
1814 #ifdef FEAT_TEXTOBJ
1815 clear_string_option(&buf->b_p_qe);
1816 #endif
1817 buf->b_p_ar = -1;
1821 * get alternate file n
1822 * set linenr to lnum or altfpos.lnum if lnum == 0
1823 * also set cursor column to altfpos.col if 'startofline' is not set.
1824 * if (options & GETF_SETMARK) call setpcmark()
1825 * if (options & GETF_ALT) we are jumping to an alternate file.
1826 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1828 * return FAIL for failure, OK for success
1831 buflist_getfile(n, lnum, options, forceit)
1832 int n;
1833 linenr_T lnum;
1834 int options;
1835 int forceit;
1837 buf_T *buf;
1838 #ifdef FEAT_WINDOWS
1839 win_T *wp = NULL;
1840 #endif
1841 pos_T *fpos;
1842 colnr_T col;
1844 buf = buflist_findnr(n);
1845 if (buf == NULL)
1847 if ((options & GETF_ALT) && n == 0)
1848 EMSG(_(e_noalt));
1849 else
1850 EMSGN(_("E92: Buffer %ld not found"), n);
1851 return FAIL;
1854 /* if alternate file is the current buffer, nothing to do */
1855 if (buf == curbuf)
1856 return OK;
1858 if (text_locked())
1860 text_locked_msg();
1861 return FAIL;
1863 #ifdef FEAT_AUTOCMD
1864 if (curbuf_locked())
1865 return FAIL;
1866 #endif
1868 /* altfpos may be changed by getfile(), get it now */
1869 if (lnum == 0)
1871 fpos = buflist_findfpos(buf);
1872 lnum = fpos->lnum;
1873 col = fpos->col;
1875 else
1876 col = 0;
1878 #ifdef FEAT_WINDOWS
1879 if (options & GETF_SWITCH)
1881 /* use existing open window for buffer if wanted */
1882 if (vim_strchr(p_swb, 'o') != NULL) /* useopen */
1883 wp = buf_jump_open_win(buf);
1884 /* use existing open window in any tab page for buffer if wanted */
1885 if (vim_strchr(p_swb, 'a') != NULL) /* usetab */
1886 wp = buf_jump_open_tab(buf);
1887 /* split window if wanted ("split") */
1888 if (wp == NULL && vim_strchr(p_swb, 'l') != NULL && !bufempty())
1890 if (win_split(0, 0) == FAIL)
1891 return FAIL;
1892 # ifdef FEAT_SCROLLBIND
1893 curwin->w_p_scb = FALSE;
1894 # endif
1897 #endif
1899 ++RedrawingDisabled;
1900 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
1901 lnum, forceit) <= 0)
1903 --RedrawingDisabled;
1905 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
1906 if (!p_sol && col != 0)
1908 curwin->w_cursor.col = col;
1909 check_cursor_col();
1910 #ifdef FEAT_VIRTUALEDIT
1911 curwin->w_cursor.coladd = 0;
1912 #endif
1913 curwin->w_set_curswant = TRUE;
1915 return OK;
1917 --RedrawingDisabled;
1918 return FAIL;
1922 * go to the last know line number for the current buffer
1924 void
1925 buflist_getfpos()
1927 pos_T *fpos;
1929 fpos = buflist_findfpos(curbuf);
1931 curwin->w_cursor.lnum = fpos->lnum;
1932 check_cursor_lnum();
1934 if (p_sol)
1935 curwin->w_cursor.col = 0;
1936 else
1938 curwin->w_cursor.col = fpos->col;
1939 check_cursor_col();
1940 #ifdef FEAT_VIRTUALEDIT
1941 curwin->w_cursor.coladd = 0;
1942 #endif
1943 curwin->w_set_curswant = TRUE;
1947 #if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
1949 * Find file in buffer list by name (it has to be for the current window).
1950 * Returns NULL if not found.
1952 buf_T *
1953 buflist_findname_exp(fname)
1954 char_u *fname;
1956 char_u *ffname;
1957 buf_T *buf = NULL;
1959 /* First make the name into a full path name */
1960 ffname = FullName_save(fname,
1961 #ifdef UNIX
1962 TRUE /* force expansion, get rid of symbolic links */
1963 #else
1964 FALSE
1965 #endif
1967 if (ffname != NULL)
1969 buf = buflist_findname(ffname);
1970 vim_free(ffname);
1972 return buf;
1974 #endif
1977 * Find file in buffer list by name (it has to be for the current window).
1978 * "ffname" must have a full path.
1979 * Skips dummy buffers.
1980 * Returns NULL if not found.
1982 buf_T *
1983 buflist_findname(ffname)
1984 char_u *ffname;
1986 #ifdef UNIX
1987 struct stat st;
1989 if (mch_stat((char *)ffname, &st) < 0)
1990 st.st_dev = (dev_T)-1;
1991 return buflist_findname_stat(ffname, &st);
1995 * Same as buflist_findname(), but pass the stat structure to avoid getting it
1996 * twice for the same file.
1997 * Returns NULL if not found.
1999 static buf_T *
2000 buflist_findname_stat(ffname, stp)
2001 char_u *ffname;
2002 struct stat *stp;
2004 #endif
2005 buf_T *buf;
2007 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2008 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
2009 #ifdef UNIX
2010 , stp
2011 #endif
2013 return buf;
2014 return NULL;
2017 #if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO)
2019 * Find file in buffer list by a regexp pattern.
2020 * Return fnum of the found buffer.
2021 * Return < 0 for error.
2023 /*ARGSUSED*/
2025 buflist_findpat(pattern, pattern_end, unlisted, diffmode)
2026 char_u *pattern;
2027 char_u *pattern_end; /* pointer to first char after pattern */
2028 int unlisted; /* find unlisted buffers */
2029 int diffmode; /* find diff-mode buffers only */
2031 buf_T *buf;
2032 regprog_T *prog;
2033 int match = -1;
2034 int find_listed;
2035 char_u *pat;
2036 char_u *patend;
2037 int attempt;
2038 char_u *p;
2039 int toggledollar;
2041 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2043 if (*pattern == '%')
2044 match = curbuf->b_fnum;
2045 else
2046 match = curwin->w_alt_fnum;
2047 #ifdef FEAT_DIFF
2048 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2049 match = -1;
2050 #endif
2054 * Try four ways of matching a listed buffer:
2055 * attempt == 0: without '^' or '$' (at any position)
2056 * attempt == 1: with '^' at start (only at position 0)
2057 * attempt == 2: with '$' at end (only match at end)
2058 * attempt == 3: with '^' at start and '$' at end (only full match)
2059 * Repeat this for finding an unlisted buffer if there was no matching
2060 * listed buffer.
2062 else
2064 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2065 if (pat == NULL)
2066 return -1;
2067 patend = pat + STRLEN(pat) - 1;
2068 toggledollar = (patend > pat && *patend == '$');
2070 /* First try finding a listed buffer. If not found and "unlisted"
2071 * is TRUE, try finding an unlisted buffer. */
2072 find_listed = TRUE;
2073 for (;;)
2075 for (attempt = 0; attempt <= 3; ++attempt)
2077 /* may add '^' and '$' */
2078 if (toggledollar)
2079 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2080 p = pat;
2081 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2082 ++p;
2083 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2084 if (prog == NULL)
2086 vim_free(pat);
2087 return -1;
2090 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2091 if (buf->b_p_bl == find_listed
2092 #ifdef FEAT_DIFF
2093 && (!diffmode || diff_mode_buf(buf))
2094 #endif
2095 && buflist_match(prog, buf) != NULL)
2097 if (match >= 0) /* already found a match */
2099 match = -2;
2100 break;
2102 match = buf->b_fnum; /* remember first match */
2105 vim_free(prog);
2106 if (match >= 0) /* found one match */
2107 break;
2110 /* Only search for unlisted buffers if there was no match with
2111 * a listed buffer. */
2112 if (!unlisted || !find_listed || match != -1)
2113 break;
2114 find_listed = FALSE;
2117 vim_free(pat);
2120 if (match == -2)
2121 EMSG2(_("E93: More than one match for %s"), pattern);
2122 else if (match < 0)
2123 EMSG2(_("E94: No matching buffer for %s"), pattern);
2124 return match;
2126 #endif
2128 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2131 * Find all buffer names that match.
2132 * For command line expansion of ":buf" and ":sbuf".
2133 * Return OK if matches found, FAIL otherwise.
2136 ExpandBufnames(pat, num_file, file, options)
2137 char_u *pat;
2138 int *num_file;
2139 char_u ***file;
2140 int options;
2142 int count = 0;
2143 buf_T *buf;
2144 int round;
2145 char_u *p;
2146 int attempt;
2147 regprog_T *prog;
2148 char_u *patc;
2150 *num_file = 0; /* return values in case of FAIL */
2151 *file = NULL;
2153 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2154 if (*pat == '^')
2156 patc = alloc((unsigned)STRLEN(pat) + 11);
2157 if (patc == NULL)
2158 return FAIL;
2159 STRCPY(patc, "\\(^\\|[\\/]\\)");
2160 STRCPY(patc + 11, pat + 1);
2162 else
2163 patc = pat;
2166 * attempt == 0: try match with '\<', match at start of word
2167 * attempt == 1: try match without '\<', match anywhere
2169 for (attempt = 0; attempt <= 1; ++attempt)
2171 if (attempt > 0 && patc == pat)
2172 break; /* there was no anchor, no need to try again */
2173 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2174 if (prog == NULL)
2176 if (patc != pat)
2177 vim_free(patc);
2178 return FAIL;
2182 * round == 1: Count the matches.
2183 * round == 2: Build the array to keep the matches.
2185 for (round = 1; round <= 2; ++round)
2187 count = 0;
2188 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2190 if (!buf->b_p_bl) /* skip unlisted buffers */
2191 continue;
2192 p = buflist_match(prog, buf);
2193 if (p != NULL)
2195 if (round == 1)
2196 ++count;
2197 else
2199 if (options & WILD_HOME_REPLACE)
2200 p = home_replace_save(buf, p);
2201 else
2202 p = vim_strsave(p);
2203 (*file)[count++] = p;
2207 if (count == 0) /* no match found, break here */
2208 break;
2209 if (round == 1)
2211 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2212 if (*file == NULL)
2214 vim_free(prog);
2215 if (patc != pat)
2216 vim_free(patc);
2217 return FAIL;
2221 vim_free(prog);
2222 if (count) /* match(es) found, break here */
2223 break;
2226 if (patc != pat)
2227 vim_free(patc);
2229 *num_file = count;
2230 return (count == 0 ? FAIL : OK);
2233 #endif /* FEAT_CMDL_COMPL */
2235 #ifdef HAVE_BUFLIST_MATCH
2237 * Check for a match on the file name for buffer "buf" with regprog "prog".
2239 static char_u *
2240 buflist_match(prog, buf)
2241 regprog_T *prog;
2242 buf_T *buf;
2244 char_u *match;
2246 /* First try the short file name, then the long file name. */
2247 match = fname_match(prog, buf->b_sfname);
2248 if (match == NULL)
2249 match = fname_match(prog, buf->b_ffname);
2251 return match;
2255 * Try matching the regexp in "prog" with file name "name".
2256 * Return "name" when there is a match, NULL when not.
2258 static char_u *
2259 fname_match(prog, name)
2260 regprog_T *prog;
2261 char_u *name;
2263 char_u *match = NULL;
2264 char_u *p;
2265 regmatch_T regmatch;
2267 if (name != NULL)
2269 regmatch.regprog = prog;
2270 #ifdef CASE_INSENSITIVE_FILENAME
2271 regmatch.rm_ic = TRUE; /* Always ignore case */
2272 #else
2273 regmatch.rm_ic = FALSE; /* Never ignore case */
2274 #endif
2276 if (vim_regexec(&regmatch, name, (colnr_T)0))
2277 match = name;
2278 else
2280 /* Replace $(HOME) with '~' and try matching again. */
2281 p = home_replace_save(NULL, name);
2282 if (p != NULL && vim_regexec(&regmatch, p, (colnr_T)0))
2283 match = name;
2284 vim_free(p);
2288 return match;
2290 #endif
2293 * find file in buffer list by number
2295 buf_T *
2296 buflist_findnr(nr)
2297 int nr;
2299 buf_T *buf;
2301 if (nr == 0)
2302 nr = curwin->w_alt_fnum;
2303 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2304 if (buf->b_fnum == nr)
2305 return (buf);
2306 return NULL;
2310 * Get name of file 'n' in the buffer list.
2311 * When the file has no name an empty string is returned.
2312 * home_replace() is used to shorten the file name (used for marks).
2313 * Returns a pointer to allocated memory, of NULL when failed.
2315 char_u *
2316 buflist_nr2name(n, fullname, helptail)
2317 int n;
2318 int fullname;
2319 int helptail; /* for help buffers return tail only */
2321 buf_T *buf;
2323 buf = buflist_findnr(n);
2324 if (buf == NULL)
2325 return NULL;
2326 return home_replace_save(helptail ? buf : NULL,
2327 fullname ? buf->b_ffname : buf->b_fname);
2331 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2332 * When "copy_options" is TRUE save the local window option values.
2333 * When "lnum" is 0 only do the options.
2335 static void
2336 buflist_setfpos(buf, win, lnum, col, copy_options)
2337 buf_T *buf;
2338 win_T *win;
2339 linenr_T lnum;
2340 colnr_T col;
2341 int copy_options;
2343 wininfo_T *wip;
2345 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2346 if (wip->wi_win == win)
2347 break;
2348 if (wip == NULL)
2350 /* allocate a new entry */
2351 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2352 if (wip == NULL)
2353 return;
2354 wip->wi_win = win;
2355 if (lnum == 0) /* set lnum even when it's 0 */
2356 lnum = 1;
2358 else
2360 /* remove the entry from the list */
2361 if (wip->wi_prev)
2362 wip->wi_prev->wi_next = wip->wi_next;
2363 else
2364 buf->b_wininfo = wip->wi_next;
2365 if (wip->wi_next)
2366 wip->wi_next->wi_prev = wip->wi_prev;
2367 if (copy_options && wip->wi_optset)
2369 clear_winopt(&wip->wi_opt);
2370 #ifdef FEAT_FOLDING
2371 deleteFoldRecurse(&wip->wi_folds);
2372 #endif
2375 if (lnum != 0)
2377 wip->wi_fpos.lnum = lnum;
2378 wip->wi_fpos.col = col;
2380 if (copy_options)
2382 /* Save the window-specific option values. */
2383 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2384 #ifdef FEAT_FOLDING
2385 wip->wi_fold_manual = win->w_fold_manual;
2386 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2387 #endif
2388 wip->wi_optset = TRUE;
2391 /* insert the entry in front of the list */
2392 wip->wi_next = buf->b_wininfo;
2393 buf->b_wininfo = wip;
2394 wip->wi_prev = NULL;
2395 if (wip->wi_next)
2396 wip->wi_next->wi_prev = wip;
2398 return;
2402 * Find info for the current window in buffer "buf".
2403 * If not found, return the info for the most recently used window.
2404 * Returns NULL when there isn't any info.
2406 static wininfo_T *
2407 find_wininfo(buf)
2408 buf_T *buf;
2410 wininfo_T *wip;
2412 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2413 if (wip->wi_win == curwin)
2414 break;
2415 if (wip == NULL) /* if no fpos for curwin, use the first in the list */
2416 wip = buf->b_wininfo;
2417 return wip;
2421 * Reset the local window options to the values last used in this window.
2422 * If the buffer wasn't used in this window before, use the values from
2423 * the most recently used window. If the values were never set, use the
2424 * global values for the window.
2426 void
2427 get_winopts(buf)
2428 buf_T *buf;
2430 wininfo_T *wip;
2432 clear_winopt(&curwin->w_onebuf_opt);
2433 #ifdef FEAT_FOLDING
2434 clearFolding(curwin);
2435 #endif
2437 wip = find_wininfo(buf);
2438 if (wip != NULL && wip->wi_optset)
2440 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2441 #ifdef FEAT_FOLDING
2442 curwin->w_fold_manual = wip->wi_fold_manual;
2443 curwin->w_foldinvalid = TRUE;
2444 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2445 #endif
2447 else
2448 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2450 #ifdef FEAT_FOLDING
2451 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2452 if (p_fdls >= 0)
2453 curwin->w_p_fdl = p_fdls;
2454 #endif
2458 * Find the position (lnum and col) for the buffer 'buf' for the current
2459 * window.
2460 * Returns a pointer to no_position if no position is found.
2462 pos_T *
2463 buflist_findfpos(buf)
2464 buf_T *buf;
2466 wininfo_T *wip;
2467 static pos_T no_position = {1, 0};
2469 wip = find_wininfo(buf);
2470 if (wip != NULL)
2471 return &(wip->wi_fpos);
2472 else
2473 return &no_position;
2477 * Find the lnum for the buffer 'buf' for the current window.
2479 linenr_T
2480 buflist_findlnum(buf)
2481 buf_T *buf;
2483 return buflist_findfpos(buf)->lnum;
2486 #if defined(FEAT_LISTCMDS) || defined(PROTO)
2488 * List all know file names (for :files and :buffers command).
2490 /*ARGSUSED*/
2491 void
2492 buflist_list(eap)
2493 exarg_T *eap;
2495 buf_T *buf;
2496 int len;
2497 int i;
2499 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2501 /* skip unlisted buffers, unless ! was used */
2502 if (!buf->b_p_bl && !eap->forceit)
2503 continue;
2504 msg_putchar('\n');
2505 if (buf_spname(buf) != NULL)
2506 STRCPY(NameBuff, buf_spname(buf));
2507 else
2508 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2510 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
2511 buf->b_fnum,
2512 buf->b_p_bl ? ' ' : 'u',
2513 buf == curbuf ? '%' :
2514 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2515 buf->b_ml.ml_mfp == NULL ? ' ' :
2516 (buf->b_nwindows == 0 ? 'h' : 'a'),
2517 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2518 (buf->b_flags & BF_READERR) ? 'x'
2519 : (bufIsChanged(buf) ? '+' : ' '),
2520 NameBuff);
2522 /* put "line 999" in column 40 or after the file name */
2523 i = 40 - vim_strsize(IObuff);
2526 IObuff[len++] = ' ';
2527 } while (--i > 0 && len < IOSIZE - 18);
2528 vim_snprintf((char *)IObuff + len, IOSIZE - len, _("line %ld"),
2529 buf == curbuf ? curwin->w_cursor.lnum
2530 : (long)buflist_findlnum(buf));
2531 msg_outtrans(IObuff);
2532 out_flush(); /* output one line at a time */
2533 ui_breakcheck();
2536 #endif
2539 * Get file name and line number for file 'fnum'.
2540 * Used by DoOneCmd() for translating '%' and '#'.
2541 * Used by insert_reg() and cmdline_paste() for '#' register.
2542 * Return FAIL if not found, OK for success.
2545 buflist_name_nr(fnum, fname, lnum)
2546 int fnum;
2547 char_u **fname;
2548 linenr_T *lnum;
2550 buf_T *buf;
2552 buf = buflist_findnr(fnum);
2553 if (buf == NULL || buf->b_fname == NULL)
2554 return FAIL;
2556 *fname = buf->b_fname;
2557 *lnum = buflist_findlnum(buf);
2559 return OK;
2563 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2564 * The file name with the full path is also remembered, for when :cd is used.
2565 * Returns FAIL for failure (file name already in use by other buffer)
2566 * OK otherwise.
2569 setfname(buf, ffname, sfname, message)
2570 buf_T *buf;
2571 char_u *ffname, *sfname;
2572 int message; /* give message when buffer already exists */
2574 buf_T *obuf = NULL;
2575 #ifdef UNIX
2576 struct stat st;
2577 #endif
2579 if (ffname == NULL || *ffname == NUL)
2581 /* Removing the name. */
2582 vim_free(buf->b_ffname);
2583 vim_free(buf->b_sfname);
2584 buf->b_ffname = NULL;
2585 buf->b_sfname = NULL;
2586 #ifdef UNIX
2587 st.st_dev = (dev_T)-1;
2588 #endif
2590 else
2592 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2593 if (ffname == NULL) /* out of memory */
2594 return FAIL;
2597 * if the file name is already used in another buffer:
2598 * - if the buffer is loaded, fail
2599 * - if the buffer is not loaded, delete it from the list
2601 #ifdef UNIX
2602 if (mch_stat((char *)ffname, &st) < 0)
2603 st.st_dev = (dev_T)-1;
2604 #endif
2605 if (!(buf->b_flags & BF_DUMMY))
2606 #ifdef UNIX
2607 obuf = buflist_findname_stat(ffname, &st);
2608 #else
2609 obuf = buflist_findname(ffname);
2610 #endif
2611 if (obuf != NULL && obuf != buf)
2613 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2615 if (message)
2616 EMSG(_("E95: Buffer with this name already exists"));
2617 vim_free(ffname);
2618 return FAIL;
2620 close_buffer(NULL, obuf, DOBUF_WIPE); /* delete from the list */
2622 sfname = vim_strsave(sfname);
2623 if (ffname == NULL || sfname == NULL)
2625 vim_free(sfname);
2626 vim_free(ffname);
2627 return FAIL;
2629 #ifdef USE_FNAME_CASE
2630 # ifdef USE_LONG_FNAME
2631 if (USE_LONG_FNAME)
2632 # endif
2633 fname_case(sfname, 0); /* set correct case for short file name */
2634 #endif
2635 vim_free(buf->b_ffname);
2636 vim_free(buf->b_sfname);
2637 buf->b_ffname = ffname;
2638 buf->b_sfname = sfname;
2640 buf->b_fname = buf->b_sfname;
2641 #ifdef UNIX
2642 if (st.st_dev == (dev_T)-1)
2643 buf->b_dev = -1;
2644 else
2646 buf->b_dev = st.st_dev;
2647 buf->b_ino = st.st_ino;
2649 #endif
2651 #ifndef SHORT_FNAME
2652 buf->b_shortname = FALSE;
2653 #endif
2655 buf_name_changed(buf);
2656 return OK;
2660 * Crude way of changing the name of a buffer. Use with care!
2661 * The name should be relative to the current directory.
2663 void
2664 buf_set_name(fnum, name)
2665 int fnum;
2666 char_u *name;
2668 buf_T *buf;
2670 buf = buflist_findnr(fnum);
2671 if (buf != NULL)
2673 vim_free(buf->b_sfname);
2674 vim_free(buf->b_ffname);
2675 buf->b_ffname = vim_strsave(name);
2676 buf->b_sfname = NULL;
2677 /* Allocate ffname and expand into full path. Also resolves .lnk
2678 * files on Win32. */
2679 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
2680 buf->b_fname = buf->b_sfname;
2685 * Take care of what needs to be done when the name of buffer "buf" has
2686 * changed.
2688 void
2689 buf_name_changed(buf)
2690 buf_T *buf;
2693 * If the file name changed, also change the name of the swapfile
2695 if (buf->b_ml.ml_mfp != NULL)
2696 ml_setname(buf);
2698 if (curwin->w_buffer == buf)
2699 check_arg_idx(curwin); /* check file name for arg list */
2700 #ifdef FEAT_TITLE
2701 maketitle(); /* set window title */
2702 #endif
2703 #ifdef FEAT_WINDOWS
2704 status_redraw_all(); /* status lines need to be redrawn */
2705 #endif
2706 fmarks_check_names(buf); /* check named file marks */
2707 ml_timestamp(buf); /* reset timestamp */
2711 * set alternate file name for current window
2713 * Used by do_one_cmd(), do_write() and do_ecmd().
2714 * Return the buffer.
2716 buf_T *
2717 setaltfname(ffname, sfname, lnum)
2718 char_u *ffname;
2719 char_u *sfname;
2720 linenr_T lnum;
2722 buf_T *buf;
2724 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2725 buf = buflist_new(ffname, sfname, lnum, 0);
2726 if (buf != NULL && !cmdmod.keepalt)
2727 curwin->w_alt_fnum = buf->b_fnum;
2728 return buf;
2732 * Get alternate file name for current window.
2733 * Return NULL if there isn't any, and give error message if requested.
2735 char_u *
2736 getaltfname(errmsg)
2737 int errmsg; /* give error message */
2739 char_u *fname;
2740 linenr_T dummy;
2742 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2744 if (errmsg)
2745 EMSG(_(e_noalt));
2746 return NULL;
2748 return fname;
2752 * Add a file name to the buflist and return its number.
2753 * Uses same flags as buflist_new(), except BLN_DUMMY.
2755 * used by qf_init(), main() and doarglist()
2758 buflist_add(fname, flags)
2759 char_u *fname;
2760 int flags;
2762 buf_T *buf;
2764 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
2765 if (buf != NULL)
2766 return buf->b_fnum;
2767 return 0;
2770 #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2772 * Adjust slashes in file names. Called after 'shellslash' was set.
2774 void
2775 buflist_slash_adjust()
2777 buf_T *bp;
2779 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
2781 if (bp->b_ffname != NULL)
2782 slash_adjust(bp->b_ffname);
2783 if (bp->b_sfname != NULL)
2784 slash_adjust(bp->b_sfname);
2787 #endif
2790 * Set alternate cursor position for current window.
2791 * Also save the local window option values.
2793 void
2794 buflist_altfpos()
2796 buflist_setfpos(curbuf, curwin, curwin->w_cursor.lnum,
2797 curwin->w_cursor.col, TRUE);
2801 * Return TRUE if 'ffname' is not the same file as current file.
2802 * Fname must have a full path (expanded by mch_FullName()).
2805 otherfile(ffname)
2806 char_u *ffname;
2808 return otherfile_buf(curbuf, ffname
2809 #ifdef UNIX
2810 , NULL
2811 #endif
2815 static int
2816 otherfile_buf(buf, ffname
2817 #ifdef UNIX
2818 , stp
2819 #endif
2821 buf_T *buf;
2822 char_u *ffname;
2823 #ifdef UNIX
2824 struct stat *stp;
2825 #endif
2827 /* no name is different */
2828 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
2829 return TRUE;
2830 if (fnamecmp(ffname, buf->b_ffname) == 0)
2831 return FALSE;
2832 #ifdef UNIX
2834 struct stat st;
2836 /* If no struct stat given, get it now */
2837 if (stp == NULL)
2839 if (buf->b_dev < 0 || mch_stat((char *)ffname, &st) < 0)
2840 st.st_dev = (dev_T)-1;
2841 stp = &st;
2843 /* Use dev/ino to check if the files are the same, even when the names
2844 * are different (possible with links). Still need to compare the
2845 * name above, for when the file doesn't exist yet.
2846 * Problem: The dev/ino changes when a file is deleted (and created
2847 * again) and remains the same when renamed/moved. We don't want to
2848 * mch_stat() each buffer each time, that would be too slow. Get the
2849 * dev/ino again when they appear to match, but not when they appear
2850 * to be different: Could skip a buffer when it's actually the same
2851 * file. */
2852 if (buf_same_ino(buf, stp))
2854 buf_setino(buf);
2855 if (buf_same_ino(buf, stp))
2856 return FALSE;
2859 #endif
2860 return TRUE;
2863 #if defined(UNIX) || defined(PROTO)
2865 * Set inode and device number for a buffer.
2866 * Must always be called when b_fname is changed!.
2868 void
2869 buf_setino(buf)
2870 buf_T *buf;
2872 struct stat st;
2874 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
2876 buf->b_dev = st.st_dev;
2877 buf->b_ino = st.st_ino;
2879 else
2880 buf->b_dev = -1;
2884 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
2886 static int
2887 buf_same_ino(buf, stp)
2888 buf_T *buf;
2889 struct stat *stp;
2891 return (buf->b_dev >= 0
2892 && stp->st_dev == buf->b_dev
2893 && stp->st_ino == buf->b_ino);
2895 #endif
2898 * Print info about the current buffer.
2900 void
2901 fileinfo(fullname, shorthelp, dont_truncate)
2902 int fullname; /* when non-zero print full path */
2903 int shorthelp;
2904 int dont_truncate;
2906 char_u *name;
2907 int n;
2908 char_u *p;
2909 char_u *buffer;
2910 size_t len;
2912 buffer = alloc(IOSIZE);
2913 if (buffer == NULL)
2914 return;
2916 if (fullname > 1) /* 2 CTRL-G: include buffer number */
2918 sprintf((char *)buffer, "buf %d: ", curbuf->b_fnum);
2919 p = buffer + STRLEN(buffer);
2921 else
2922 p = buffer;
2924 *p++ = '"';
2925 if (buf_spname(curbuf) != NULL)
2926 STRCPY(p, buf_spname(curbuf));
2927 else
2929 if (!fullname && curbuf->b_fname != NULL)
2930 name = curbuf->b_fname;
2931 else
2932 name = curbuf->b_ffname;
2933 home_replace(shorthelp ? curbuf : NULL, name, p,
2934 (int)(IOSIZE - (p - buffer)), TRUE);
2937 len = STRLEN(buffer);
2938 vim_snprintf((char *)buffer + len, IOSIZE - len,
2939 "\"%s%s%s%s%s%s",
2940 curbufIsChanged() ? (shortmess(SHM_MOD)
2941 ? " [+]" : _(" [Modified]")) : " ",
2942 (curbuf->b_flags & BF_NOTEDITED)
2943 #ifdef FEAT_QUICKFIX
2944 && !bt_dontwrite(curbuf)
2945 #endif
2946 ? _("[Not edited]") : "",
2947 (curbuf->b_flags & BF_NEW)
2948 #ifdef FEAT_QUICKFIX
2949 && !bt_dontwrite(curbuf)
2950 #endif
2951 ? _("[New file]") : "",
2952 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
2953 curbuf->b_p_ro ? (shortmess(SHM_RO) ? "[RO]"
2954 : _("[readonly]")) : "",
2955 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
2956 || curbuf->b_p_ro) ?
2957 " " : "");
2958 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
2959 * causes an overflow, thus for large numbers divide instead. */
2960 if (curwin->w_cursor.lnum > 1000000L)
2961 n = (int)(((long)curwin->w_cursor.lnum) /
2962 ((long)curbuf->b_ml.ml_line_count / 100L));
2963 else
2964 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
2965 (long)curbuf->b_ml.ml_line_count);
2966 len = STRLEN(buffer);
2967 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2969 vim_snprintf((char *)buffer + len, IOSIZE - len, "%s", _(no_lines_msg));
2971 #ifdef FEAT_CMDL_INFO
2972 else if (p_ru)
2974 /* Current line and column are already on the screen -- webb */
2975 if (curbuf->b_ml.ml_line_count == 1)
2976 vim_snprintf((char *)buffer + len, IOSIZE - len,
2977 _("1 line --%d%%--"), n);
2978 else
2979 vim_snprintf((char *)buffer + len, IOSIZE - len,
2980 _("%ld lines --%d%%--"),
2981 (long)curbuf->b_ml.ml_line_count, n);
2983 #endif
2984 else
2986 vim_snprintf((char *)buffer + len, IOSIZE - len,
2987 _("line %ld of %ld --%d%%-- col "),
2988 (long)curwin->w_cursor.lnum,
2989 (long)curbuf->b_ml.ml_line_count,
2991 validate_virtcol();
2992 col_print(buffer + STRLEN(buffer),
2993 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
2996 (void)append_arg_number(curwin, buffer, !shortmess(SHM_FILE), IOSIZE);
2998 if (dont_truncate)
3000 /* Temporarily set msg_scroll to avoid the message being truncated.
3001 * First call msg_start() to get the message in the right place. */
3002 msg_start();
3003 n = msg_scroll;
3004 msg_scroll = TRUE;
3005 msg(buffer);
3006 msg_scroll = n;
3008 else
3010 p = msg_trunc_attr(buffer, FALSE, 0);
3011 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
3012 /* Need to repeat the message after redrawing when:
3013 * - When restart_edit is set (otherwise there will be a delay
3014 * before redrawing).
3015 * - When the screen was scrolled but there is no wait-return
3016 * prompt. */
3017 set_keep_msg(p, 0);
3020 vim_free(buffer);
3023 void
3024 col_print(buf, col, vcol)
3025 char_u *buf;
3026 int col;
3027 int vcol;
3029 if (col == vcol)
3030 sprintf((char *)buf, "%d", col);
3031 else
3032 sprintf((char *)buf, "%d-%d", col, vcol);
3035 #if defined(FEAT_TITLE) || defined(PROTO)
3037 * put file name in title bar of window and in icon title
3040 static char_u *lasttitle = NULL;
3041 static char_u *lasticon = NULL;
3043 void
3044 maketitle()
3046 char_u *p;
3047 char_u *t_str = NULL;
3048 char_u *i_name;
3049 char_u *i_str = NULL;
3050 int maxlen = 0;
3051 int len;
3052 int mustset;
3053 char_u buf[IOSIZE];
3054 int off;
3056 if (!redrawing())
3058 /* Postpone updating the title when 'lazyredraw' is set. */
3059 need_maketitle = TRUE;
3060 return;
3063 #ifdef FEAT_GUI_MACVIM
3064 gui_macvim_update_modified_flag();
3065 #endif
3067 need_maketitle = FALSE;
3068 if (!p_title && !p_icon)
3069 return;
3071 if (p_title)
3073 if (p_titlelen > 0)
3075 maxlen = p_titlelen * Columns / 100;
3076 if (maxlen < 10)
3077 maxlen = 10;
3080 t_str = buf;
3081 if (*p_titlestring != NUL)
3083 #ifdef FEAT_STL_OPT
3084 if (stl_syntax & STL_IN_TITLE)
3086 int use_sandbox = FALSE;
3087 int save_called_emsg = called_emsg;
3089 # ifdef FEAT_EVAL
3090 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
3091 # endif
3092 called_emsg = FALSE;
3093 build_stl_str_hl(curwin, t_str, sizeof(buf),
3094 p_titlestring, use_sandbox,
3095 0, maxlen, NULL, NULL);
3096 if (called_emsg)
3097 set_string_option_direct((char_u *)"titlestring", -1,
3098 (char_u *)"", OPT_FREE, SID_ERROR);
3099 called_emsg |= save_called_emsg;
3101 else
3102 #endif
3103 t_str = p_titlestring;
3105 else
3107 /* format: "fname + (path) (1 of 2) - VIM" */
3109 if (curbuf->b_fname == NULL)
3110 STRCPY(buf, _("[No Name]"));
3111 else
3113 p = transstr(gettail(curbuf->b_fname));
3114 vim_strncpy(buf, p, IOSIZE - 100);
3115 vim_free(p);
3118 switch (bufIsChanged(curbuf)
3119 + (curbuf->b_p_ro * 2)
3120 + (!curbuf->b_p_ma * 4))
3122 case 1: STRCAT(buf, " +"); break;
3123 case 2: STRCAT(buf, " ="); break;
3124 case 3: STRCAT(buf, " =+"); break;
3125 case 4:
3126 case 6: STRCAT(buf, " -"); break;
3127 case 5:
3128 case 7: STRCAT(buf, " -+"); break;
3131 if (curbuf->b_fname != NULL)
3133 /* Get path of file, replace home dir with ~ */
3134 off = (int)STRLEN(buf);
3135 buf[off++] = ' ';
3136 buf[off++] = '(';
3137 home_replace(curbuf, curbuf->b_ffname,
3138 buf + off, IOSIZE - off, TRUE);
3139 #ifdef BACKSLASH_IN_FILENAME
3140 /* avoid "c:/name" to be reduced to "c" */
3141 if (isalpha(buf[off]) && buf[off + 1] == ':')
3142 off += 2;
3143 #endif
3144 /* remove the file name */
3145 p = gettail_sep(buf + off);
3146 if (p == buf + off)
3147 /* must be a help buffer */
3148 vim_strncpy(buf + off, (char_u *)_("help"),
3149 IOSIZE - off - 1);
3150 else
3151 *p = NUL;
3153 /* translate unprintable chars */
3154 p = transstr(buf + off);
3155 vim_strncpy(buf + off, p, IOSIZE - off - 1);
3156 vim_free(p);
3157 STRCAT(buf, ")");
3160 #ifndef FEAT_GUI_MACVIM
3161 append_arg_number(curwin, buf, FALSE, IOSIZE);
3162 #endif
3164 #if defined(FEAT_CLIENTSERVER)
3165 if (serverName != NULL)
3167 STRCAT(buf, " - ");
3168 STRCAT(buf, serverName);
3170 else
3171 #endif
3172 STRCAT(buf, " - VIM");
3174 if (maxlen > 0)
3176 /* make it shorter by removing a bit in the middle */
3177 len = vim_strsize(buf);
3178 if (len > maxlen)
3179 trunc_string(buf, buf, maxlen);
3183 mustset = ti_change(t_str, &lasttitle);
3185 if (p_icon)
3187 i_str = buf;
3188 if (*p_iconstring != NUL)
3190 #ifdef FEAT_STL_OPT
3191 if (stl_syntax & STL_IN_ICON)
3193 int use_sandbox = FALSE;
3194 int save_called_emsg = called_emsg;
3196 # ifdef FEAT_EVAL
3197 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
3198 # endif
3199 called_emsg = FALSE;
3200 build_stl_str_hl(curwin, i_str, sizeof(buf),
3201 p_iconstring, use_sandbox,
3202 0, 0, NULL, NULL);
3203 if (called_emsg)
3204 set_string_option_direct((char_u *)"iconstring", -1,
3205 (char_u *)"", OPT_FREE, SID_ERROR);
3206 called_emsg |= save_called_emsg;
3208 else
3209 #endif
3210 i_str = p_iconstring;
3212 else
3214 if (buf_spname(curbuf) != NULL)
3215 i_name = (char_u *)buf_spname(curbuf);
3216 else /* use file name only in icon */
3217 i_name = gettail(curbuf->b_ffname);
3218 *i_str = NUL;
3219 /* Truncate name at 100 bytes. */
3220 len = (int)STRLEN(i_name);
3221 if (len > 100)
3223 len -= 100;
3224 #ifdef FEAT_MBYTE
3225 if (has_mbyte)
3226 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3227 #endif
3228 i_name += len;
3230 STRCPY(i_str, i_name);
3231 trans_characters(i_str, IOSIZE);
3235 mustset |= ti_change(i_str, &lasticon);
3237 if (mustset)
3238 resettitle();
3242 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3243 * from "str" if it does.
3244 * Return TRUE when "*last" changed.
3246 static int
3247 ti_change(str, last)
3248 char_u *str;
3249 char_u **last;
3251 if ((str == NULL) != (*last == NULL)
3252 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3254 vim_free(*last);
3255 if (str == NULL)
3256 *last = NULL;
3257 else
3258 *last = vim_strsave(str);
3259 return TRUE;
3261 return FALSE;
3265 * Put current window title back (used after calling a shell)
3267 void
3268 resettitle()
3270 mch_settitle(lasttitle, lasticon);
3273 # if defined(EXITFREE) || defined(PROTO)
3274 void
3275 free_titles()
3277 vim_free(lasttitle);
3278 vim_free(lasticon);
3280 # endif
3282 #endif /* FEAT_TITLE */
3284 #if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
3286 * Build a string from the status line items in "fmt".
3287 * Return length of string in screen cells.
3289 * Normally works for window "wp", except when working for 'tabline' then it
3290 * is "curwin".
3292 * Items are drawn interspersed with the text that surrounds it
3293 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3294 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3296 * If maxwidth is not zero, the string will be filled at any middle marker
3297 * or truncated if too long, fillchar is used for all whitespace.
3299 /*ARGSUSED*/
3301 build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar, maxwidth, hltab, tabtab)
3302 win_T *wp;
3303 char_u *out; /* buffer to write into != NameBuff */
3304 size_t outlen; /* length of out[] */
3305 char_u *fmt;
3306 int use_sandbox; /* "fmt" was set insecurely, use sandbox */
3307 int fillchar;
3308 int maxwidth;
3309 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3310 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
3312 char_u *p;
3313 char_u *s;
3314 char_u *t;
3315 char_u *linecont;
3316 #ifdef FEAT_EVAL
3317 win_T *o_curwin;
3318 buf_T *o_curbuf;
3319 #endif
3320 int empty_line;
3321 colnr_T virtcol;
3322 long l;
3323 long n;
3324 int prevchar_isflag;
3325 int prevchar_isitem;
3326 int itemisflag;
3327 int fillable;
3328 char_u *str;
3329 long num;
3330 int width;
3331 int itemcnt;
3332 int curitem;
3333 int groupitem[STL_MAX_ITEM];
3334 int groupdepth;
3335 struct stl_item
3337 char_u *start;
3338 int minwid;
3339 int maxwid;
3340 enum
3342 Normal,
3343 Empty,
3344 Group,
3345 Middle,
3346 Highlight,
3347 TabPage,
3348 Trunc
3349 } type;
3350 } item[STL_MAX_ITEM];
3351 int minwid;
3352 int maxwid;
3353 int zeropad;
3354 char_u base;
3355 char_u opt;
3356 #define TMPLEN 70
3357 char_u tmp[TMPLEN];
3358 char_u *usefmt = fmt;
3359 struct stl_hlrec *sp;
3361 #ifdef FEAT_EVAL
3363 * When the format starts with "%!" then evaluate it as an expression and
3364 * use the result as the actual format string.
3366 if (fmt[0] == '%' && fmt[1] == '!')
3368 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3369 if (usefmt == NULL)
3370 usefmt = fmt;
3372 #endif
3374 if (fillchar == 0)
3375 fillchar = ' ';
3376 #ifdef FEAT_MBYTE
3377 /* Can't handle a multi-byte fill character yet. */
3378 else if (mb_char2len(fillchar) > 1)
3379 fillchar = '-';
3380 #endif
3383 * Get line & check if empty (cursorpos will show "0-1").
3384 * If inversion is possible we use it. Else '=' characters are used.
3386 linecont = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3387 empty_line = (*linecont == NUL);
3389 groupdepth = 0;
3390 p = out;
3391 curitem = 0;
3392 prevchar_isflag = TRUE;
3393 prevchar_isitem = FALSE;
3394 for (s = usefmt; *s; )
3396 if (*s != NUL && *s != '%')
3397 prevchar_isflag = prevchar_isitem = FALSE;
3400 * Handle up to the next '%' or the end.
3402 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3403 *p++ = *s++;
3404 if (*s == NUL || p + 1 >= out + outlen)
3405 break;
3408 * Handle one '%' item.
3410 s++;
3411 if (*s == '%')
3413 if (p + 1 >= out + outlen)
3414 break;
3415 *p++ = *s++;
3416 prevchar_isflag = prevchar_isitem = FALSE;
3417 continue;
3419 if (*s == STL_MIDDLEMARK)
3421 s++;
3422 if (groupdepth > 0)
3423 continue;
3424 item[curitem].type = Middle;
3425 item[curitem++].start = p;
3426 continue;
3428 if (*s == STL_TRUNCMARK)
3430 s++;
3431 item[curitem].type = Trunc;
3432 item[curitem++].start = p;
3433 continue;
3435 if (*s == ')')
3437 s++;
3438 if (groupdepth < 1)
3439 continue;
3440 groupdepth--;
3442 t = item[groupitem[groupdepth]].start;
3443 *p = NUL;
3444 l = vim_strsize(t);
3445 if (curitem > groupitem[groupdepth] + 1
3446 && item[groupitem[groupdepth]].minwid == 0)
3448 /* remove group if all items are empty */
3449 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3450 if (item[n].type == Normal)
3451 break;
3452 if (n == curitem)
3454 p = t;
3455 l = 0;
3458 if (l > item[groupitem[groupdepth]].maxwid)
3460 /* truncate, remove n bytes of text at the start */
3461 #ifdef FEAT_MBYTE
3462 if (has_mbyte)
3464 /* Find the first character that should be included. */
3465 n = 0;
3466 while (l >= item[groupitem[groupdepth]].maxwid)
3468 l -= ptr2cells(t + n);
3469 n += (*mb_ptr2len)(t + n);
3472 else
3473 #endif
3474 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
3476 *t = '<';
3477 mch_memmove(t + 1, t + n, p - (t + n));
3478 p = p - n + 1;
3479 #ifdef FEAT_MBYTE
3480 /* Fill up space left over by half a double-wide char. */
3481 while (++l < item[groupitem[groupdepth]].minwid)
3482 *p++ = fillchar;
3483 #endif
3485 /* correct the start of the items for the truncation */
3486 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3488 item[l].start -= n;
3489 if (item[l].start < t)
3490 item[l].start = t;
3493 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3495 /* fill */
3496 n = item[groupitem[groupdepth]].minwid;
3497 if (n < 0)
3499 /* fill by appending characters */
3500 n = 0 - n;
3501 while (l++ < n && p + 1 < out + outlen)
3502 *p++ = fillchar;
3504 else
3506 /* fill by inserting characters */
3507 mch_memmove(t + n - l, t, p - t);
3508 l = n - l;
3509 if (p + l >= out + outlen)
3510 l = (long)((out + outlen) - p - 1);
3511 p += l;
3512 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3513 item[n].start += l;
3514 for ( ; l > 0; l--)
3515 *t++ = fillchar;
3518 continue;
3520 minwid = 0;
3521 maxwid = 9999;
3522 zeropad = FALSE;
3523 l = 1;
3524 if (*s == '0')
3526 s++;
3527 zeropad = TRUE;
3529 if (*s == '-')
3531 s++;
3532 l = -1;
3534 if (VIM_ISDIGIT(*s))
3536 minwid = (int)getdigits(&s);
3537 if (minwid < 0) /* overflow */
3538 minwid = 0;
3540 if (*s == STL_USER_HL)
3542 item[curitem].type = Highlight;
3543 item[curitem].start = p;
3544 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3545 s++;
3546 curitem++;
3547 continue;
3549 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3551 if (*s == STL_TABCLOSENR)
3553 if (minwid == 0)
3555 /* %X ends the close label, go back to the previously
3556 * define tab label nr. */
3557 for (n = curitem - 1; n >= 0; --n)
3558 if (item[n].type == TabPage && item[n].minwid >= 0)
3560 minwid = item[n].minwid;
3561 break;
3564 else
3565 /* close nrs are stored as negative values */
3566 minwid = - minwid;
3568 item[curitem].type = TabPage;
3569 item[curitem].start = p;
3570 item[curitem].minwid = minwid;
3571 s++;
3572 curitem++;
3573 continue;
3575 if (*s == '.')
3577 s++;
3578 if (VIM_ISDIGIT(*s))
3580 maxwid = (int)getdigits(&s);
3581 if (maxwid <= 0) /* overflow */
3582 maxwid = 50;
3585 minwid = (minwid > 50 ? 50 : minwid) * l;
3586 if (*s == '(')
3588 groupitem[groupdepth++] = curitem;
3589 item[curitem].type = Group;
3590 item[curitem].start = p;
3591 item[curitem].minwid = minwid;
3592 item[curitem].maxwid = maxwid;
3593 s++;
3594 curitem++;
3595 continue;
3597 if (vim_strchr(STL_ALL, *s) == NULL)
3599 s++;
3600 continue;
3602 opt = *s++;
3604 /* OK - now for the real work */
3605 base = 'D';
3606 itemisflag = FALSE;
3607 fillable = TRUE;
3608 num = -1;
3609 str = NULL;
3610 switch (opt)
3612 case STL_FILEPATH:
3613 case STL_FULLPATH:
3614 case STL_FILENAME:
3615 fillable = FALSE; /* don't change ' ' to fillchar */
3616 if (buf_spname(wp->w_buffer) != NULL)
3617 STRCPY(NameBuff, buf_spname(wp->w_buffer));
3618 else
3620 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
3621 : wp->w_buffer->b_fname;
3622 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3624 trans_characters(NameBuff, MAXPATHL);
3625 if (opt != STL_FILENAME)
3626 str = NameBuff;
3627 else
3628 str = gettail(NameBuff);
3629 break;
3631 case STL_VIM_EXPR: /* '{' */
3632 itemisflag = TRUE;
3633 t = p;
3634 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3635 *p++ = *s++;
3636 if (*s != '}') /* missing '}' or out of space */
3637 break;
3638 s++;
3639 *p = 0;
3640 p = t;
3642 #ifdef FEAT_EVAL
3643 sprintf((char *)tmp, "%d", curbuf->b_fnum);
3644 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3646 o_curbuf = curbuf;
3647 o_curwin = curwin;
3648 curwin = wp;
3649 curbuf = wp->w_buffer;
3651 str = eval_to_string_safe(p, &t, use_sandbox);
3653 curwin = o_curwin;
3654 curbuf = o_curbuf;
3655 do_unlet((char_u *)"g:actual_curbuf", TRUE);
3657 if (str != NULL && *str != 0)
3659 if (*skipdigits(str) == NUL)
3661 num = atoi((char *)str);
3662 vim_free(str);
3663 str = NULL;
3664 itemisflag = FALSE;
3667 #endif
3668 break;
3670 case STL_LINE:
3671 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3672 ? 0L : (long)(wp->w_cursor.lnum);
3673 break;
3675 case STL_NUMLINES:
3676 num = wp->w_buffer->b_ml.ml_line_count;
3677 break;
3679 case STL_COLUMN:
3680 num = !(State & INSERT) && empty_line
3681 ? 0 : (int)wp->w_cursor.col + 1;
3682 break;
3684 case STL_VIRTCOL:
3685 case STL_VIRTCOL_ALT:
3686 /* In list mode virtcol needs to be recomputed */
3687 virtcol = wp->w_virtcol;
3688 if (wp->w_p_list && lcs_tab1 == NUL)
3690 wp->w_p_list = FALSE;
3691 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3692 wp->w_p_list = TRUE;
3694 ++virtcol;
3695 /* Don't display %V if it's the same as %c. */
3696 if (opt == STL_VIRTCOL_ALT
3697 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3698 ? 0 : (int)wp->w_cursor.col + 1)))
3699 break;
3700 num = (long)virtcol;
3701 break;
3703 case STL_PERCENTAGE:
3704 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3705 (long)wp->w_buffer->b_ml.ml_line_count);
3706 break;
3708 case STL_ALTPERCENT:
3709 str = tmp;
3710 get_rel_pos(wp, str);
3711 break;
3713 case STL_ARGLISTSTAT:
3714 fillable = FALSE;
3715 tmp[0] = 0;
3716 if (append_arg_number(wp, tmp, FALSE, (int)sizeof(tmp)))
3717 str = tmp;
3718 break;
3720 case STL_KEYMAP:
3721 fillable = FALSE;
3722 if (get_keymap_str(wp, tmp, TMPLEN))
3723 str = tmp;
3724 break;
3725 case STL_PAGENUM:
3726 #if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
3727 num = printer_page_num;
3728 #else
3729 num = 0;
3730 #endif
3731 break;
3733 case STL_BUFNO:
3734 num = wp->w_buffer->b_fnum;
3735 break;
3737 case STL_OFFSET_X:
3738 base = 'X';
3739 case STL_OFFSET:
3740 #ifdef FEAT_BYTEOFF
3741 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
3742 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
3743 0L : l + 1 + (!(State & INSERT) && empty_line ?
3744 0 : (int)wp->w_cursor.col);
3745 #endif
3746 break;
3748 case STL_BYTEVAL_X:
3749 base = 'X';
3750 case STL_BYTEVAL:
3751 if (wp->w_cursor.col > STRLEN(linecont))
3752 num = 0;
3753 else
3755 #ifdef FEAT_MBYTE
3756 num = (*mb_ptr2char)(linecont + wp->w_cursor.col);
3757 #else
3758 num = linecont[wp->w_cursor.col];
3759 #endif
3761 if (num == NL)
3762 num = 0;
3763 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
3764 num = NL;
3765 break;
3767 case STL_ROFLAG:
3768 case STL_ROFLAG_ALT:
3769 itemisflag = TRUE;
3770 if (wp->w_buffer->b_p_ro)
3771 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : "[RO]");
3772 break;
3774 case STL_HELPFLAG:
3775 case STL_HELPFLAG_ALT:
3776 itemisflag = TRUE;
3777 if (wp->w_buffer->b_help)
3778 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
3779 : _("[Help]"));
3780 break;
3782 #ifdef FEAT_AUTOCMD
3783 case STL_FILETYPE:
3784 if (*wp->w_buffer->b_p_ft != NUL
3785 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
3787 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
3788 wp->w_buffer->b_p_ft);
3789 str = tmp;
3791 break;
3793 case STL_FILETYPE_ALT:
3794 itemisflag = TRUE;
3795 if (*wp->w_buffer->b_p_ft != NUL
3796 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
3798 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
3799 wp->w_buffer->b_p_ft);
3800 for (t = tmp; *t != 0; t++)
3801 *t = TOUPPER_LOC(*t);
3802 str = tmp;
3804 break;
3805 #endif
3807 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3808 case STL_PREVIEWFLAG:
3809 case STL_PREVIEWFLAG_ALT:
3810 itemisflag = TRUE;
3811 if (wp->w_p_pvw)
3812 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
3813 : _("[Preview]"));
3814 break;
3815 #endif
3817 case STL_MODIFIED:
3818 case STL_MODIFIED_ALT:
3819 itemisflag = TRUE;
3820 switch ((opt == STL_MODIFIED_ALT)
3821 + bufIsChanged(wp->w_buffer) * 2
3822 + (!wp->w_buffer->b_p_ma) * 4)
3824 case 2: str = (char_u *)"[+]"; break;
3825 case 3: str = (char_u *)",+"; break;
3826 case 4: str = (char_u *)"[-]"; break;
3827 case 5: str = (char_u *)",-"; break;
3828 case 6: str = (char_u *)"[+-]"; break;
3829 case 7: str = (char_u *)",+-"; break;
3831 break;
3833 case STL_HIGHLIGHT:
3834 t = s;
3835 while (*s != '#' && *s != NUL)
3836 ++s;
3837 if (*s == '#')
3839 item[curitem].type = Highlight;
3840 item[curitem].start = p;
3841 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
3842 curitem++;
3844 ++s;
3845 continue;
3848 item[curitem].start = p;
3849 item[curitem].type = Normal;
3850 if (str != NULL && *str)
3852 t = str;
3853 if (itemisflag)
3855 if ((t[0] && t[1])
3856 && ((!prevchar_isitem && *t == ',')
3857 || (prevchar_isflag && *t == ' ')))
3858 t++;
3859 prevchar_isflag = TRUE;
3861 l = vim_strsize(t);
3862 if (l > 0)
3863 prevchar_isitem = TRUE;
3864 if (l > maxwid)
3866 while (l >= maxwid)
3867 #ifdef FEAT_MBYTE
3868 if (has_mbyte)
3870 l -= ptr2cells(t);
3871 t += (*mb_ptr2len)(t);
3873 else
3874 #endif
3875 l -= byte2cells(*t++);
3876 if (p + 1 >= out + outlen)
3877 break;
3878 *p++ = '<';
3880 if (minwid > 0)
3882 for (; l < minwid && p + 1 < out + outlen; l++)
3884 /* Don't put a "-" in front of a digit. */
3885 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
3886 *p++ = ' ';
3887 else
3888 *p++ = fillchar;
3890 minwid = 0;
3892 else
3893 minwid *= -1;
3894 while (*t && p + 1 < out + outlen)
3896 *p++ = *t++;
3897 /* Change a space by fillchar, unless fillchar is '-' and a
3898 * digit follows. */
3899 if (fillable && p[-1] == ' '
3900 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
3901 p[-1] = fillchar;
3903 for (; l < minwid && p + 1 < out + outlen; l++)
3904 *p++ = fillchar;
3906 else if (num >= 0)
3908 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
3909 char_u nstr[20];
3911 if (p + 20 >= out + outlen)
3912 break; /* not sufficient space */
3913 prevchar_isitem = TRUE;
3914 t = nstr;
3915 if (opt == STL_VIRTCOL_ALT)
3917 *t++ = '-';
3918 minwid--;
3920 *t++ = '%';
3921 if (zeropad)
3922 *t++ = '0';
3923 *t++ = '*';
3924 *t++ = nbase == 16 ? base : (nbase == 8 ? 'o' : 'd');
3925 *t = 0;
3927 for (n = num, l = 1; n >= nbase; n /= nbase)
3928 l++;
3929 if (opt == STL_VIRTCOL_ALT)
3930 l++;
3931 if (l > maxwid)
3933 l += 2;
3934 n = l - maxwid;
3935 while (l-- > maxwid)
3936 num /= nbase;
3937 *t++ = '>';
3938 *t++ = '%';
3939 *t = t[-3];
3940 *++t = 0;
3941 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
3942 0, num, n);
3944 else
3945 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
3946 minwid, num);
3947 p += STRLEN(p);
3949 else
3950 item[curitem].type = Empty;
3952 if (opt == STL_VIM_EXPR)
3953 vim_free(str);
3955 if (num >= 0 || (!itemisflag && str && *str))
3956 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
3957 curitem++;
3959 *p = NUL;
3960 itemcnt = curitem;
3962 #ifdef FEAT_EVAL
3963 if (usefmt != fmt)
3964 vim_free(usefmt);
3965 #endif
3967 width = vim_strsize(out);
3968 if (maxwidth > 0 && width > maxwidth)
3970 /* Result is too long, must trunctate somewhere. */
3971 l = 0;
3972 if (itemcnt == 0)
3973 s = out;
3974 else
3976 for ( ; l < itemcnt; l++)
3977 if (item[l].type == Trunc)
3979 /* Truncate at %< item. */
3980 s = item[l].start;
3981 break;
3983 if (l == itemcnt)
3985 /* No %< item, truncate first item. */
3986 s = item[0].start;
3987 l = 0;
3991 if (width - vim_strsize(s) >= maxwidth)
3993 /* Truncation mark is beyond max length */
3994 #ifdef FEAT_MBYTE
3995 if (has_mbyte)
3997 s = out;
3998 width = 0;
3999 for (;;)
4001 width += ptr2cells(s);
4002 if (width >= maxwidth)
4003 break;
4004 s += (*mb_ptr2len)(s);
4006 /* Fill up for half a double-wide character. */
4007 while (++width < maxwidth)
4008 *s++ = fillchar;
4010 else
4011 #endif
4012 s = out + maxwidth - 1;
4013 for (l = 0; l < itemcnt; l++)
4014 if (item[l].start > s)
4015 break;
4016 itemcnt = l;
4017 *s++ = '>';
4018 *s = 0;
4020 else
4022 #ifdef FEAT_MBYTE
4023 if (has_mbyte)
4025 n = 0;
4026 while (width >= maxwidth)
4028 width -= ptr2cells(s + n);
4029 n += (*mb_ptr2len)(s + n);
4032 else
4033 #endif
4034 n = width - maxwidth + 1;
4035 p = s + n;
4036 mch_memmove(s + 1, p, STRLEN(p) + 1);
4037 *s = '<';
4039 /* Fill up for half a double-wide character. */
4040 while (++width < maxwidth)
4042 s = s + STRLEN(s);
4043 *s++ = fillchar;
4044 *s = NUL;
4047 --n; /* count the '<' */
4048 for (; l < itemcnt; l++)
4050 if (item[l].start - n >= s)
4051 item[l].start -= n;
4052 else
4053 item[l].start = s;
4056 width = maxwidth;
4058 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4060 /* Apply STL_MIDDLE if any */
4061 for (l = 0; l < itemcnt; l++)
4062 if (item[l].type == Middle)
4063 break;
4064 if (l < itemcnt)
4066 p = item[l].start + maxwidth - width;
4067 mch_memmove(p, item[l].start, STRLEN(item[l].start) + 1);
4068 for (s = item[l].start; s < p; s++)
4069 *s = fillchar;
4070 for (l++; l < itemcnt; l++)
4071 item[l].start += maxwidth - width;
4072 width = maxwidth;
4076 /* Store the info about highlighting. */
4077 if (hltab != NULL)
4079 sp = hltab;
4080 for (l = 0; l < itemcnt; l++)
4082 if (item[l].type == Highlight)
4084 sp->start = item[l].start;
4085 sp->userhl = item[l].minwid;
4086 sp++;
4089 sp->start = NULL;
4090 sp->userhl = 0;
4093 /* Store the info about tab pages labels. */
4094 if (tabtab != NULL)
4096 sp = tabtab;
4097 for (l = 0; l < itemcnt; l++)
4099 if (item[l].type == TabPage)
4101 sp->start = item[l].start;
4102 sp->userhl = item[l].minwid;
4103 sp++;
4106 sp->start = NULL;
4107 sp->userhl = 0;
4110 return width;
4112 #endif /* FEAT_STL_OPT */
4114 #if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4115 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
4117 * Get relative cursor position in window into "str[]", in the form 99%, using
4118 * "Top", "Bot" or "All" when appropriate.
4120 void
4121 get_rel_pos(wp, str)
4122 win_T *wp;
4123 char_u *str;
4125 long above; /* number of lines above window */
4126 long below; /* number of lines below window */
4128 above = wp->w_topline - 1;
4129 #ifdef FEAT_DIFF
4130 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
4131 #endif
4132 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4133 if (below <= 0)
4134 STRCPY(str, above == 0 ? _("All") : _("Bot"));
4135 else if (above <= 0)
4136 STRCPY(str, _("Top"));
4137 else
4138 sprintf((char *)str, "%2d%%", above > 1000000L
4139 ? (int)(above / ((above + below) / 100L))
4140 : (int)(above * 100L / (above + below)));
4142 #endif
4145 * Append (file 2 of 8) to 'buf', if editing more than one file.
4146 * Return TRUE if it was appended.
4149 append_arg_number(wp, buf, add_file, maxlen)
4150 win_T *wp;
4151 char_u *buf;
4152 int add_file; /* Add "file" before the arg number */
4153 int maxlen; /* maximum nr of chars in buf or zero*/
4155 char_u *p;
4157 if (ARGCOUNT <= 1) /* nothing to do */
4158 return FALSE;
4160 p = buf + STRLEN(buf); /* go to the end of the buffer */
4161 if (maxlen && p - buf + 35 >= maxlen) /* getting too long */
4162 return FALSE;
4163 *p++ = ' ';
4164 *p++ = '(';
4165 if (add_file)
4167 STRCPY(p, "file ");
4168 p += 5;
4170 sprintf((char *)p, wp->w_arg_idx_invalid ? "(%d) of %d)"
4171 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4172 return TRUE;
4176 * If fname is not a full path, make it a full path.
4177 * Returns pointer to allocated memory (NULL for failure).
4179 char_u *
4180 fix_fname(fname)
4181 char_u *fname;
4184 * Force expanding the path always for Unix, because symbolic links may
4185 * mess up the full path name, even though it starts with a '/'.
4186 * Also expand when there is ".." in the file name, try to remove it,
4187 * because "c:/src/../README" is equal to "c:/README".
4188 * Similarly "c:/src//file" is equal to "c:/src/file".
4189 * For MS-Windows also expand names like "longna~1" to "longname".
4191 #ifdef UNIX
4192 return FullName_save(fname, TRUE);
4193 #else
4194 if (!vim_isAbsName(fname)
4195 || strstr((char *)fname, "..") != NULL
4196 || strstr((char *)fname, "//") != NULL
4197 # ifdef BACKSLASH_IN_FILENAME
4198 || strstr((char *)fname, "\\\\") != NULL
4199 # endif
4200 # if defined(MSWIN) || defined(DJGPP)
4201 || vim_strchr(fname, '~') != NULL
4202 # endif
4204 return FullName_save(fname, FALSE);
4206 fname = vim_strsave(fname);
4208 # ifdef USE_FNAME_CASE
4209 # ifdef USE_LONG_FNAME
4210 if (USE_LONG_FNAME)
4211 # endif
4213 if (fname != NULL)
4214 fname_case(fname, 0); /* set correct case for file name */
4216 # endif
4218 return fname;
4219 #endif
4223 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4224 * "ffname" becomes a pointer to allocated memory (or NULL).
4226 /*ARGSUSED*/
4227 void
4228 fname_expand(buf, ffname, sfname)
4229 buf_T *buf;
4230 char_u **ffname;
4231 char_u **sfname;
4233 if (*ffname == NULL) /* if no file name given, nothing to do */
4234 return;
4235 if (*sfname == NULL) /* if no short file name given, use ffname */
4236 *sfname = *ffname;
4237 *ffname = fix_fname(*ffname); /* expand to full path */
4239 #ifdef FEAT_SHORTCUT
4240 if (!buf->b_p_bin)
4242 char_u *rfname;
4244 /* If the file name is a shortcut file, use the file it links to. */
4245 rfname = mch_resolve_shortcut(*ffname);
4246 if (rfname != NULL)
4248 vim_free(*ffname);
4249 *ffname = rfname;
4250 *sfname = rfname;
4253 #endif
4257 * Get the file name for an argument list entry.
4259 char_u *
4260 alist_name(aep)
4261 aentry_T *aep;
4263 buf_T *bp;
4265 /* Use the name from the associated buffer if it exists. */
4266 bp = buflist_findnr(aep->ae_fnum);
4267 if (bp == NULL || bp->b_fname == NULL)
4268 return aep->ae_fname;
4269 return bp->b_fname;
4272 #if defined(FEAT_WINDOWS) || defined(PROTO)
4274 * do_arg_all(): Open up to 'count' windows, one for each argument.
4276 void
4277 do_arg_all(count, forceit, keep_tabs)
4278 int count;
4279 int forceit; /* hide buffers in current windows */
4280 int keep_tabs; /* keep current tabs, for ":tab drop file" */
4282 int i;
4283 win_T *wp, *wpnext;
4284 char_u *opened; /* array of flags for which args are open */
4285 int opened_len; /* length of opened[] */
4286 int use_firstwin = FALSE; /* use first window for arglist */
4287 int split_ret = OK;
4288 int p_ea_save;
4289 alist_T *alist; /* argument list to be used */
4290 buf_T *buf;
4291 tabpage_T *tpnext;
4292 int had_tab = cmdmod.tab;
4293 win_T *new_curwin = NULL;
4294 tabpage_T *new_curtab = NULL;
4296 if (ARGCOUNT <= 0)
4298 /* Don't give an error message. We don't want it when the ":all"
4299 * command is in the .vimrc. */
4300 return;
4302 setpcmark();
4304 opened_len = ARGCOUNT;
4305 opened = alloc_clear((unsigned)opened_len);
4306 if (opened == NULL)
4307 return;
4309 #ifdef FEAT_GUI
4310 need_mouse_correct = TRUE;
4311 #endif
4314 * Try closing all windows that are not in the argument list.
4315 * Also close windows that are not full width;
4316 * When 'hidden' or "forceit" set the buffer becomes hidden.
4317 * Windows that have a changed buffer and can't be hidden won't be closed.
4318 * When the ":tab" modifier was used do this for all tab pages.
4320 if (had_tab > 0)
4321 goto_tabpage_tp(first_tabpage);
4322 for (;;)
4324 tpnext = curtab->tp_next;
4325 for (wp = firstwin; wp != NULL; wp = wpnext)
4327 wpnext = wp->w_next;
4328 buf = wp->w_buffer;
4329 if (buf->b_ffname == NULL
4330 || buf->b_nwindows > 1
4331 #ifdef FEAT_VERTSPLIT
4332 || wp->w_width != Columns
4333 #endif
4335 i = ARGCOUNT;
4336 else
4338 /* check if the buffer in this window is in the arglist */
4339 for (i = 0; i < ARGCOUNT; ++i)
4341 if (ARGLIST[i].ae_fnum == buf->b_fnum
4342 || fullpathcmp(alist_name(&ARGLIST[i]),
4343 buf->b_ffname, TRUE) & FPC_SAME)
4345 if (i < opened_len)
4347 opened[i] = TRUE;
4348 if (i == 0)
4350 new_curwin = wp;
4351 new_curtab = curtab;
4354 if (wp->w_alist != curwin->w_alist)
4356 /* Use the current argument list for all windows
4357 * containing a file from it. */
4358 alist_unlink(wp->w_alist);
4359 wp->w_alist = curwin->w_alist;
4360 ++wp->w_alist->al_refcount;
4362 break;
4366 wp->w_arg_idx = i;
4368 if (i == ARGCOUNT && !keep_tabs) /* close this window */
4370 if (P_HID(buf) || forceit || buf->b_nwindows > 1
4371 || !bufIsChanged(buf))
4373 /* If the buffer was changed, and we would like to hide it,
4374 * try autowriting. */
4375 if (!P_HID(buf) && buf->b_nwindows <= 1
4376 && bufIsChanged(buf))
4378 (void)autowrite(buf, FALSE);
4379 #ifdef FEAT_AUTOCMD
4380 /* check if autocommands removed the window */
4381 if (!win_valid(wp) || !buf_valid(buf))
4383 wpnext = firstwin; /* start all over... */
4384 continue;
4386 #endif
4388 #ifdef FEAT_WINDOWS
4389 /* don't close last window */
4390 if (firstwin == lastwin && first_tabpage->tp_next == NULL)
4391 #endif
4392 use_firstwin = TRUE;
4393 #ifdef FEAT_WINDOWS
4394 else
4396 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4397 # ifdef FEAT_AUTOCMD
4398 /* check if autocommands removed the next window */
4399 if (!win_valid(wpnext))
4400 wpnext = firstwin; /* start all over... */
4401 # endif
4403 #endif
4408 /* Without the ":tab" modifier only do the current tab page. */
4409 if (had_tab == 0 || tpnext == NULL)
4410 break;
4412 # ifdef FEAT_AUTOCMD
4413 /* check if autocommands removed the next tab page */
4414 if (!valid_tabpage(tpnext))
4415 tpnext = first_tabpage; /* start all over...*/
4416 # endif
4417 goto_tabpage_tp(tpnext);
4421 * Open a window for files in the argument list that don't have one.
4422 * ARGCOUNT may change while doing this, because of autocommands.
4424 if (count > ARGCOUNT || count <= 0)
4425 count = ARGCOUNT;
4427 /* Autocommands may do anything to the argument list. Make sure it's not
4428 * freed while we are working here by "locking" it. We still have to
4429 * watch out for its size to be changed. */
4430 alist = curwin->w_alist;
4431 ++alist->al_refcount;
4433 #ifdef FEAT_AUTOCMD
4434 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4435 ++autocmd_no_enter;
4436 ++autocmd_no_leave;
4437 #endif
4438 win_enter(lastwin, FALSE);
4439 #ifdef FEAT_WINDOWS
4440 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4441 * leaving an empty tab page when executed locally. */
4442 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4443 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4444 use_firstwin = TRUE;
4445 #endif
4447 for (i = 0; i < count && i < alist->al_ga.ga_len && !got_int; ++i)
4449 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4450 arg_had_last = TRUE;
4451 if (i < opened_len && opened[i])
4453 /* Move the already present window to below the current window */
4454 if (curwin->w_arg_idx != i)
4456 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4458 if (wpnext->w_arg_idx == i)
4460 win_move_after(wpnext, curwin);
4461 break;
4466 else if (split_ret == OK)
4468 if (!use_firstwin) /* split current window */
4470 p_ea_save = p_ea;
4471 p_ea = TRUE; /* use space from all windows */
4472 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4473 p_ea = p_ea_save;
4474 if (split_ret == FAIL)
4475 continue;
4477 #ifdef FEAT_AUTOCMD
4478 else /* first window: do autocmd for leaving this buffer */
4479 --autocmd_no_leave;
4480 #endif
4483 * edit file "i"
4485 curwin->w_arg_idx = i;
4486 if (i == 0)
4488 new_curwin = curwin;
4489 new_curtab = curtab;
4491 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4492 ECMD_ONE,
4493 ((P_HID(curwin->w_buffer)
4494 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
4495 + ECMD_OLDBUF);
4496 #ifdef FEAT_AUTOCMD
4497 if (use_firstwin)
4498 ++autocmd_no_leave;
4499 #endif
4500 use_firstwin = FALSE;
4502 ui_breakcheck();
4504 /* When ":tab" was used open a new tab for a new window repeatedly. */
4505 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4506 cmdmod.tab = 9999;
4509 /* Remove the "lock" on the argument list. */
4510 alist_unlink(alist);
4512 #ifdef FEAT_AUTOCMD
4513 --autocmd_no_enter;
4514 #endif
4515 /* to window with first arg */
4516 if (valid_tabpage(new_curtab))
4517 goto_tabpage_tp(new_curtab);
4518 if (win_valid(new_curwin))
4519 win_enter(new_curwin, FALSE);
4521 #ifdef FEAT_AUTOCMD
4522 --autocmd_no_leave;
4523 #endif
4524 vim_free(opened);
4527 # if defined(FEAT_LISTCMDS) || defined(PROTO)
4529 * Open a window for a number of buffers.
4531 void
4532 ex_buffer_all(eap)
4533 exarg_T *eap;
4535 buf_T *buf;
4536 win_T *wp, *wpnext;
4537 int split_ret = OK;
4538 int p_ea_save;
4539 int open_wins = 0;
4540 int r;
4541 int count; /* Maximum number of windows to open. */
4542 int all; /* When TRUE also load inactive buffers. */
4543 #ifdef FEAT_WINDOWS
4544 int had_tab = cmdmod.tab;
4545 tabpage_T *tpnext;
4546 #endif
4548 if (eap->addr_count == 0) /* make as many windows as possible */
4549 count = 9999;
4550 else
4551 count = eap->line2; /* make as many windows as specified */
4552 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4553 all = FALSE;
4554 else
4555 all = TRUE;
4557 setpcmark();
4559 #ifdef FEAT_GUI
4560 need_mouse_correct = TRUE;
4561 #endif
4564 * Close superfluous windows (two windows for the same buffer).
4565 * Also close windows that are not full-width.
4567 #ifdef FEAT_WINDOWS
4568 if (had_tab > 0)
4569 goto_tabpage_tp(first_tabpage);
4570 for (;;)
4572 #endif
4573 tpnext = curtab->tp_next;
4574 for (wp = firstwin; wp != NULL; wp = wpnext)
4576 wpnext = wp->w_next;
4577 if ((wp->w_buffer->b_nwindows > 1
4578 #ifdef FEAT_VERTSPLIT
4579 || ((cmdmod.split & WSP_VERT)
4580 ? wp->w_height + wp->w_status_height < Rows - p_ch
4581 - tabline_height()
4582 : wp->w_width != Columns)
4583 #endif
4584 #ifdef FEAT_WINDOWS
4585 || (had_tab > 0 && wp != firstwin)
4586 #endif
4587 ) && firstwin != lastwin)
4589 win_close(wp, FALSE);
4590 #ifdef FEAT_AUTOCMD
4591 wpnext = firstwin; /* just in case an autocommand does
4592 something strange with windows */
4593 tpnext = first_tabpage; /* start all over...*/
4594 open_wins = 0;
4595 #endif
4597 else
4598 ++open_wins;
4601 #ifdef FEAT_WINDOWS
4602 /* Without the ":tab" modifier only do the current tab page. */
4603 if (had_tab == 0 || tpnext == NULL)
4604 break;
4605 goto_tabpage_tp(tpnext);
4607 #endif
4610 * Go through the buffer list. When a buffer doesn't have a window yet,
4611 * open one. Otherwise move the window to the right position.
4612 * Watch out for autocommands that delete buffers or windows!
4614 #ifdef FEAT_AUTOCMD
4615 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4616 ++autocmd_no_enter;
4617 #endif
4618 win_enter(lastwin, FALSE);
4619 #ifdef FEAT_AUTOCMD
4620 ++autocmd_no_leave;
4621 #endif
4622 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4624 /* Check if this buffer needs a window */
4625 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4626 continue;
4628 #ifdef FEAT_WINDOWS
4629 if (had_tab != 0)
4631 /* With the ":tab" modifier don't move the window. */
4632 if (buf->b_nwindows > 0)
4633 wp = lastwin; /* buffer has a window, skip it */
4634 else
4635 wp = NULL;
4637 else
4638 #endif
4640 /* Check if this buffer already has a window */
4641 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4642 if (wp->w_buffer == buf)
4643 break;
4644 /* If the buffer already has a window, move it */
4645 if (wp != NULL)
4646 win_move_after(wp, curwin);
4649 if (wp == NULL && split_ret == OK)
4651 /* Split the window and put the buffer in it */
4652 p_ea_save = p_ea;
4653 p_ea = TRUE; /* use space from all windows */
4654 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4655 ++open_wins;
4656 p_ea = p_ea_save;
4657 if (split_ret == FAIL)
4658 continue;
4660 /* Open the buffer in this window. */
4661 #if defined(HAS_SWAP_EXISTS_ACTION)
4662 swap_exists_action = SEA_DIALOG;
4663 #endif
4664 set_curbuf(buf, DOBUF_GOTO);
4665 #ifdef FEAT_AUTOCMD
4666 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
4668 #if defined(HAS_SWAP_EXISTS_ACTION)
4669 swap_exists_action = SEA_NONE;
4670 # endif
4671 break;
4673 #endif
4674 #if defined(HAS_SWAP_EXISTS_ACTION)
4675 if (swap_exists_action == SEA_QUIT)
4677 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4678 cleanup_T cs;
4680 /* Reset the error/interrupt/exception state here so that
4681 * aborting() returns FALSE when closing a window. */
4682 enter_cleanup(&cs);
4683 # endif
4685 /* User selected Quit at ATTENTION prompt; close this window. */
4686 win_close(curwin, TRUE);
4687 --open_wins;
4688 swap_exists_action = SEA_NONE;
4689 swap_exists_did_quit = TRUE;
4691 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4692 /* Restore the error/interrupt/exception state if not
4693 * discarded by a new aborting error, interrupt, or uncaught
4694 * exception. */
4695 leave_cleanup(&cs);
4696 # endif
4698 else
4699 handle_swap_exists(NULL);
4700 #endif
4703 ui_breakcheck();
4704 if (got_int)
4706 (void)vgetc(); /* only break the file loading, not the rest */
4707 break;
4709 #ifdef FEAT_EVAL
4710 /* Autocommands deleted the buffer or aborted script processing!!! */
4711 if (aborting())
4712 break;
4713 #endif
4714 #ifdef FEAT_WINDOWS
4715 /* When ":tab" was used open a new tab for a new window repeatedly. */
4716 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4717 cmdmod.tab = 9999;
4718 #endif
4720 #ifdef FEAT_AUTOCMD
4721 --autocmd_no_enter;
4722 #endif
4723 win_enter(firstwin, FALSE); /* back to first window */
4724 #ifdef FEAT_AUTOCMD
4725 --autocmd_no_leave;
4726 #endif
4729 * Close superfluous windows.
4731 for (wp = lastwin; open_wins > count; )
4733 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
4734 || autowrite(wp->w_buffer, FALSE) == OK);
4735 #ifdef FEAT_AUTOCMD
4736 if (!win_valid(wp))
4738 /* BufWrite Autocommands made the window invalid, start over */
4739 wp = lastwin;
4741 else
4742 #endif
4743 if (r)
4745 win_close(wp, !P_HID(wp->w_buffer));
4746 --open_wins;
4747 wp = lastwin;
4749 else
4751 wp = wp->w_prev;
4752 if (wp == NULL)
4753 break;
4757 # endif /* FEAT_LISTCMDS */
4759 #endif /* FEAT_WINDOWS */
4761 static int chk_modeline __ARGS((linenr_T, int));
4764 * do_modelines() - process mode lines for the current file
4766 * "flags" can be:
4767 * OPT_WINONLY only set options local to window
4768 * OPT_NOWIN don't set options local to window
4770 * Returns immediately if the "ml" option isn't set.
4772 void
4773 do_modelines(flags)
4774 int flags;
4776 linenr_T lnum;
4777 int nmlines;
4778 static int entered = 0;
4780 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
4781 return;
4783 /* Disallow recursive entry here. Can happen when executing a modeline
4784 * triggers an autocommand, which reloads modelines with a ":do". */
4785 if (entered)
4786 return;
4788 ++entered;
4789 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
4790 ++lnum)
4791 if (chk_modeline(lnum, flags) == FAIL)
4792 nmlines = 0;
4794 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
4795 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
4796 if (chk_modeline(lnum, flags) == FAIL)
4797 nmlines = 0;
4798 --entered;
4801 #include "version.h" /* for version number */
4804 * chk_modeline() - check a single line for a mode string
4805 * Return FAIL if an error encountered.
4807 static int
4808 chk_modeline(lnum, flags)
4809 linenr_T lnum;
4810 int flags; /* Same as for do_modelines(). */
4812 char_u *s;
4813 char_u *e;
4814 char_u *linecopy; /* local copy of any modeline found */
4815 int prev;
4816 int vers;
4817 int end;
4818 int retval = OK;
4819 char_u *save_sourcing_name;
4820 linenr_T save_sourcing_lnum;
4821 #ifdef FEAT_EVAL
4822 scid_T save_SID;
4823 #endif
4825 prev = -1;
4826 for (s = ml_get(lnum); *s != NUL; ++s)
4828 if (prev == -1 || vim_isspace(prev))
4830 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
4831 || STRNCMP(s, "vi:", (size_t)3) == 0)
4832 break;
4833 if (STRNCMP(s, "vim", 3) == 0)
4835 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
4836 e = s + 4;
4837 else
4838 e = s + 3;
4839 vers = getdigits(&e);
4840 if (*e == ':'
4841 && (s[3] == ':'
4842 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
4843 || (VIM_VERSION_100 < vers && s[3] == '<')
4844 || (VIM_VERSION_100 > vers && s[3] == '>')
4845 || (VIM_VERSION_100 == vers && s[3] == '=')))
4846 break;
4849 prev = *s;
4852 if (*s)
4854 do /* skip over "ex:", "vi:" or "vim:" */
4855 ++s;
4856 while (s[-1] != ':');
4858 s = linecopy = vim_strsave(s); /* copy the line, it will change */
4859 if (linecopy == NULL)
4860 return FAIL;
4862 save_sourcing_lnum = sourcing_lnum;
4863 save_sourcing_name = sourcing_name;
4864 sourcing_lnum = lnum; /* prepare for emsg() */
4865 sourcing_name = (char_u *)"modelines";
4867 end = FALSE;
4868 while (end == FALSE)
4870 s = skipwhite(s);
4871 if (*s == NUL)
4872 break;
4875 * Find end of set command: ':' or end of line.
4876 * Skip over "\:", replacing it with ":".
4878 for (e = s; *e != ':' && *e != NUL; ++e)
4879 if (e[0] == '\\' && e[1] == ':')
4880 mch_memmove(e, e + 1, STRLEN(e));
4881 if (*e == NUL)
4882 end = TRUE;
4885 * If there is a "set" command, require a terminating ':' and
4886 * ignore the stuff after the ':'.
4887 * "vi:set opt opt opt: foo" -- foo not interpreted
4888 * "vi:opt opt opt: foo" -- foo interpreted
4889 * Accept "se" for compatibility with Elvis.
4891 if (STRNCMP(s, "set ", (size_t)4) == 0
4892 || STRNCMP(s, "se ", (size_t)3) == 0)
4894 if (*e != ':') /* no terminating ':'? */
4895 break;
4896 end = TRUE;
4897 s = vim_strchr(s, ' ') + 1;
4899 *e = NUL; /* truncate the set command */
4901 if (*s != NUL) /* skip over an empty "::" */
4903 #ifdef FEAT_EVAL
4904 save_SID = current_SID;
4905 current_SID = SID_MODELINE;
4906 #endif
4907 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
4908 #ifdef FEAT_EVAL
4909 current_SID = save_SID;
4910 #endif
4911 if (retval == FAIL) /* stop if error found */
4912 break;
4914 s = e + 1; /* advance to next part */
4917 sourcing_lnum = save_sourcing_lnum;
4918 sourcing_name = save_sourcing_name;
4920 vim_free(linecopy);
4922 return retval;
4925 #ifdef FEAT_VIMINFO
4927 read_viminfo_bufferlist(virp, writing)
4928 vir_T *virp;
4929 int writing;
4931 char_u *tab;
4932 linenr_T lnum;
4933 colnr_T col;
4934 buf_T *buf;
4935 char_u *sfname;
4936 char_u *xline;
4938 /* Handle long line and escaped characters. */
4939 xline = viminfo_readstring(virp, 1, FALSE);
4941 /* don't read in if there are files on the command-line or if writing: */
4942 if (xline != NULL && !writing && ARGCOUNT == 0
4943 && find_viminfo_parameter('%') != NULL)
4945 /* Format is: <fname> Tab <lnum> Tab <col>.
4946 * Watch out for a Tab in the file name, work from the end. */
4947 lnum = 0;
4948 col = 0;
4949 tab = vim_strrchr(xline, '\t');
4950 if (tab != NULL)
4952 *tab++ = '\0';
4953 col = atoi((char *)tab);
4954 tab = vim_strrchr(xline, '\t');
4955 if (tab != NULL)
4957 *tab++ = '\0';
4958 lnum = atol((char *)tab);
4962 /* Expand "~/" in the file name at "line + 1" to a full path.
4963 * Then try shortening it by comparing with the current directory */
4964 expand_env(xline, NameBuff, MAXPATHL);
4965 sfname = shorten_fname1(NameBuff);
4967 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
4968 if (buf != NULL) /* just in case... */
4970 buf->b_last_cursor.lnum = lnum;
4971 buf->b_last_cursor.col = col;
4972 buflist_setfpos(buf, curwin, lnum, col, FALSE);
4975 vim_free(xline);
4977 return viminfo_readline(virp);
4980 void
4981 write_viminfo_bufferlist(fp)
4982 FILE *fp;
4984 buf_T *buf;
4985 #ifdef FEAT_WINDOWS
4986 win_T *win;
4987 tabpage_T *tp;
4988 #endif
4989 char_u *line;
4990 int max_buffers;
4992 if (find_viminfo_parameter('%') == NULL)
4993 return;
4995 /* Without a number -1 is returned: do all buffers. */
4996 max_buffers = get_viminfo_parameter('%');
4998 /* Allocate room for the file name, lnum and col. */
4999 line = alloc(MAXPATHL + 40);
5000 if (line == NULL)
5001 return;
5003 #ifdef FEAT_WINDOWS
5004 FOR_ALL_TAB_WINDOWS(tp, win)
5005 set_last_cursor(win);
5006 #else
5007 set_last_cursor(curwin);
5008 #endif
5010 fprintf(fp, _("\n# Buffer list:\n"));
5011 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5013 if (buf->b_fname == NULL
5014 || !buf->b_p_bl
5015 #ifdef FEAT_QUICKFIX
5016 || bt_quickfix(buf)
5017 #endif
5018 || removable(buf->b_ffname))
5019 continue;
5021 if (max_buffers-- == 0)
5022 break;
5023 putc('%', fp);
5024 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
5025 sprintf((char *)line + STRLEN(line), "\t%ld\t%d",
5026 (long)buf->b_last_cursor.lnum,
5027 buf->b_last_cursor.col);
5028 viminfo_writestring(fp, line);
5030 vim_free(line);
5032 #endif
5036 * Return special buffer name.
5037 * Returns NULL when the buffer has a normal file name.
5039 char *
5040 buf_spname(buf)
5041 buf_T *buf;
5043 #if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5044 if (bt_quickfix(buf))
5046 win_T *win;
5049 * For location list window, w_llist_ref points to the location list.
5050 * For quickfix window, w_llist_ref is NULL.
5052 FOR_ALL_WINDOWS(win)
5053 if (win->w_buffer == buf)
5054 break;
5055 if (win != NULL && win->w_llist_ref != NULL)
5056 return _("[Location List]");
5057 else
5058 return _("[Quickfix List]");
5060 #endif
5061 #ifdef FEAT_QUICKFIX
5062 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5063 * contains the name as specified by the user */
5064 if (bt_nofile(buf))
5066 if (buf->b_sfname != NULL)
5067 return (char *)buf->b_sfname;
5068 return "[Scratch]";
5070 #endif
5071 if (buf->b_fname == NULL)
5072 return _("[No Name]");
5073 return NULL;
5077 #if defined(FEAT_SIGNS) || defined(PROTO)
5079 * Insert the sign into the signlist.
5081 static void
5082 insert_sign(buf, prev, next, id, lnum, typenr)
5083 buf_T *buf; /* buffer to store sign in */
5084 signlist_T *prev; /* previous sign entry */
5085 signlist_T *next; /* next sign entry */
5086 int id; /* sign ID */
5087 linenr_T lnum; /* line number which gets the mark */
5088 int typenr; /* typenr of sign we are adding */
5090 signlist_T *newsign;
5092 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5093 if (newsign != NULL)
5095 newsign->id = id;
5096 newsign->lnum = lnum;
5097 newsign->typenr = typenr;
5098 newsign->next = next;
5099 #ifdef FEAT_NETBEANS_INTG
5100 newsign->prev = prev;
5101 if (next != NULL)
5102 next->prev = newsign;
5103 #endif
5105 if (prev == NULL)
5107 /* When adding first sign need to redraw the windows to create the
5108 * column for signs. */
5109 if (buf->b_signlist == NULL)
5111 redraw_buf_later(buf, NOT_VALID);
5112 changed_cline_bef_curs();
5115 /* first sign in signlist */
5116 buf->b_signlist = newsign;
5118 else
5119 prev->next = newsign;
5124 * Add the sign into the signlist. Find the right spot to do it though.
5126 void
5127 buf_addsign(buf, id, lnum, typenr)
5128 buf_T *buf; /* buffer to store sign in */
5129 int id; /* sign ID */
5130 linenr_T lnum; /* line number which gets the mark */
5131 int typenr; /* typenr of sign we are adding */
5133 signlist_T *sign; /* a sign in the signlist */
5134 signlist_T *prev; /* the previous sign */
5136 prev = NULL;
5137 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5139 if (lnum == sign->lnum && id == sign->id)
5141 sign->typenr = typenr;
5142 return;
5144 else if (
5145 #ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5146 id < 0 &&
5147 #endif
5148 lnum < sign->lnum)
5150 #ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5151 /* XXX - GRP: Is this because of sign slide problem? Or is it
5152 * really needed? Or is it because we allow multiple signs per
5153 * line? If so, should I add that feature to FEAT_SIGNS?
5155 while (prev != NULL && prev->lnum == lnum)
5156 prev = prev->prev;
5157 if (prev == NULL)
5158 sign = buf->b_signlist;
5159 else
5160 sign = prev->next;
5161 #endif
5162 insert_sign(buf, prev, sign, id, lnum, typenr);
5163 return;
5165 prev = sign;
5167 #ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5168 /* XXX - GRP: See previous comment */
5169 while (prev != NULL && prev->lnum == lnum)
5170 prev = prev->prev;
5171 if (prev == NULL)
5172 sign = buf->b_signlist;
5173 else
5174 sign = prev->next;
5175 #endif
5176 insert_sign(buf, prev, sign, id, lnum, typenr);
5178 return;
5182 buf_change_sign_type(buf, markId, typenr)
5183 buf_T *buf; /* buffer to store sign in */
5184 int markId; /* sign ID */
5185 int typenr; /* typenr of sign we are adding */
5187 signlist_T *sign; /* a sign in the signlist */
5189 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5191 if (sign->id == markId)
5193 sign->typenr = typenr;
5194 return sign->lnum;
5198 return 0;
5201 int_u
5202 buf_getsigntype(buf, lnum, type)
5203 buf_T *buf;
5204 linenr_T lnum;
5205 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5207 signlist_T *sign; /* a sign in a b_signlist */
5209 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5210 if (sign->lnum == lnum
5211 && (type == SIGN_ANY
5212 # ifdef FEAT_SIGN_ICONS
5213 || (type == SIGN_ICON
5214 && sign_get_image(sign->typenr) != NULL)
5215 # endif
5216 || (type == SIGN_TEXT
5217 && sign_get_text(sign->typenr) != NULL)
5218 || (type == SIGN_LINEHL
5219 && sign_get_attr(sign->typenr, TRUE) != 0)))
5220 return sign->typenr;
5221 return 0;
5225 linenr_T
5226 buf_delsign(buf, id)
5227 buf_T *buf; /* buffer sign is stored in */
5228 int id; /* sign id */
5230 signlist_T **lastp; /* pointer to pointer to current sign */
5231 signlist_T *sign; /* a sign in a b_signlist */
5232 signlist_T *next; /* the next sign in a b_signlist */
5233 linenr_T lnum; /* line number whose sign was deleted */
5235 lastp = &buf->b_signlist;
5236 lnum = 0;
5237 for (sign = buf->b_signlist; sign != NULL; sign = next)
5239 next = sign->next;
5240 if (sign->id == id)
5242 *lastp = next;
5243 #ifdef FEAT_NETBEANS_INTG
5244 if (next != NULL)
5245 next->prev = sign->prev;
5246 #endif
5247 lnum = sign->lnum;
5248 vim_free(sign);
5249 break;
5251 else
5252 lastp = &sign->next;
5255 /* When deleted the last sign need to redraw the windows to remove the
5256 * sign column. */
5257 if (buf->b_signlist == NULL)
5259 redraw_buf_later(buf, NOT_VALID);
5260 changed_cline_bef_curs();
5263 return lnum;
5268 * Find the line number of the sign with the requested id. If the sign does
5269 * not exist, return 0 as the line number. This will still let the correct file
5270 * get loaded.
5273 buf_findsign(buf, id)
5274 buf_T *buf; /* buffer to store sign in */
5275 int id; /* sign ID */
5277 signlist_T *sign; /* a sign in the signlist */
5279 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5280 if (sign->id == id)
5281 return sign->lnum;
5283 return 0;
5287 buf_findsign_id(buf, lnum)
5288 buf_T *buf; /* buffer whose sign we are searching for */
5289 linenr_T lnum; /* line number of sign */
5291 signlist_T *sign; /* a sign in the signlist */
5293 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5294 if (sign->lnum == lnum)
5295 return sign->id;
5297 return 0;
5301 # if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5302 /* see if a given type of sign exists on a specific line */
5304 buf_findsigntype_id(buf, lnum, typenr)
5305 buf_T *buf; /* buffer whose sign we are searching for */
5306 linenr_T lnum; /* line number of sign */
5307 int typenr; /* sign type number */
5309 signlist_T *sign; /* a sign in the signlist */
5311 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5312 if (sign->lnum == lnum && sign->typenr == typenr)
5313 return sign->id;
5315 return 0;
5319 # if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5320 /* return the number of icons on the given line */
5322 buf_signcount(buf, lnum)
5323 buf_T *buf;
5324 linenr_T lnum;
5326 signlist_T *sign; /* a sign in the signlist */
5327 int count = 0;
5329 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5330 if (sign->lnum == lnum)
5331 if (sign_get_image(sign->typenr) != NULL)
5332 count++;
5334 return count;
5336 # endif /* FEAT_SIGN_ICONS */
5337 # endif /* FEAT_NETBEANS_INTG */
5341 * Delete signs in buffer "buf".
5343 static void
5344 buf_delete_signs(buf)
5345 buf_T *buf;
5347 signlist_T *next;
5349 while (buf->b_signlist != NULL)
5351 next = buf->b_signlist->next;
5352 vim_free(buf->b_signlist);
5353 buf->b_signlist = next;
5358 * Delete all signs in all buffers.
5360 void
5361 buf_delete_all_signs()
5363 buf_T *buf; /* buffer we are checking for signs */
5365 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5366 if (buf->b_signlist != NULL)
5368 /* Need to redraw the windows to remove the sign column. */
5369 redraw_buf_later(buf, NOT_VALID);
5370 buf_delete_signs(buf);
5375 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5377 void
5378 sign_list_placed(rbuf)
5379 buf_T *rbuf;
5381 buf_T *buf;
5382 signlist_T *p;
5383 char lbuf[BUFSIZ];
5385 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5386 msg_putchar('\n');
5387 if (rbuf == NULL)
5388 buf = firstbuf;
5389 else
5390 buf = rbuf;
5391 while (buf != NULL)
5393 if (buf->b_signlist != NULL)
5395 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
5396 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5397 msg_putchar('\n');
5399 for (p = buf->b_signlist; p != NULL; p = p->next)
5401 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
5402 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5403 MSG_PUTS(lbuf);
5404 msg_putchar('\n');
5406 if (rbuf != NULL)
5407 break;
5408 buf = buf->b_next;
5413 * Adjust a placed sign for inserted/deleted lines.
5415 void
5416 sign_mark_adjust(line1, line2, amount, amount_after)
5417 linenr_T line1;
5418 linenr_T line2;
5419 long amount;
5420 long amount_after;
5422 signlist_T *sign; /* a sign in a b_signlist */
5424 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5426 if (sign->lnum >= line1 && sign->lnum <= line2)
5428 if (amount == MAXLNUM)
5429 sign->lnum = line1;
5430 else
5431 sign->lnum += amount;
5433 else if (sign->lnum > line2)
5434 sign->lnum += amount_after;
5437 #endif /* FEAT_SIGNS */
5440 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5442 void
5443 set_buflisted(on)
5444 int on;
5446 if (on != curbuf->b_p_bl)
5448 curbuf->b_p_bl = on;
5449 #ifdef FEAT_AUTOCMD
5450 if (on)
5451 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5452 else
5453 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5454 #endif
5459 * Read the file for "buf" again and check if the contents changed.
5460 * Return TRUE if it changed or this could not be checked.
5463 buf_contents_changed(buf)
5464 buf_T *buf;
5466 buf_T *newbuf;
5467 int differ = TRUE;
5468 linenr_T lnum;
5469 aco_save_T aco;
5470 exarg_T ea;
5472 /* Allocate a buffer without putting it in the buffer list. */
5473 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5474 if (newbuf == NULL)
5475 return TRUE;
5477 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5478 if (prep_exarg(&ea, buf) == FAIL)
5480 wipe_buffer(newbuf, FALSE);
5481 return TRUE;
5484 /* set curwin/curbuf to buf and save a few things */
5485 aucmd_prepbuf(&aco, newbuf);
5487 if (ml_open(curbuf) == OK
5488 && readfile(buf->b_ffname, buf->b_fname,
5489 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5490 &ea, READ_NEW | READ_DUMMY) == OK)
5492 /* compare the two files line by line */
5493 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5495 differ = FALSE;
5496 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5497 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5499 differ = TRUE;
5500 break;
5504 vim_free(ea.cmd);
5506 /* restore curwin/curbuf and a few other things */
5507 aucmd_restbuf(&aco);
5509 if (curbuf != newbuf) /* safety check */
5510 wipe_buffer(newbuf, FALSE);
5512 return differ;
5516 * Wipe out a buffer and decrement the last buffer number if it was used for
5517 * this buffer. Call this to wipe out a temp buffer that does not contain any
5518 * marks.
5520 /*ARGSUSED*/
5521 void
5522 wipe_buffer(buf, aucmd)
5523 buf_T *buf;
5524 int aucmd; /* When TRUE trigger autocommands. */
5526 if (buf->b_fnum == top_file_num - 1)
5527 --top_file_num;
5529 #ifdef FEAT_AUTOCMD
5530 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
5531 block_autocmds();
5532 #endif
5533 close_buffer(NULL, buf, DOBUF_WIPE);
5534 #ifdef FEAT_AUTOCMD
5535 if (!aucmd)
5536 unblock_autocmds();
5537 #endif