Merge branch 'vim'
[MacVim.git] / src / buffer.c
blobee00b5aacf9b9b392f86b1eb46f87f508a83dc94
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, int skip_diff_buffer));
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 int append_arg_number __ARGS((win_T *wp, char_u *buf, int buflen, int add_file));
48 static void free_buffer __ARGS((buf_T *));
49 static void free_buffer_stuff __ARGS((buf_T *buf, int free_options));
50 static void clear_wininfo __ARGS((buf_T *buf));
52 #ifdef UNIX
53 # define dev_T dev_t
54 #else
55 # define dev_T unsigned
56 #endif
58 #if defined(FEAT_SIGNS)
59 static void insert_sign __ARGS((buf_T *buf, signlist_T *prev, signlist_T *next, int id, linenr_T lnum, int typenr));
60 static void buf_delete_signs __ARGS((buf_T *buf));
61 #endif
64 * Open current buffer, that is: open the memfile and read the file into memory
65 * return FAIL for failure, OK otherwise
67 int
68 open_buffer(read_stdin, eap)
69 int read_stdin; /* read file from stdin */
70 exarg_T *eap; /* for forced 'ff' and 'fenc' or NULL */
72 int retval = OK;
73 #ifdef FEAT_AUTOCMD
74 buf_T *old_curbuf;
75 #endif
78 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
79 * When re-entering the same buffer, it should not change, because the
80 * user may have reset the flag by hand.
82 if (readonlymode && curbuf->b_ffname != NULL
83 && (curbuf->b_flags & BF_NEVERLOADED))
84 curbuf->b_p_ro = TRUE;
86 if (ml_open(curbuf) == FAIL)
89 * There MUST be a memfile, otherwise we can't do anything
90 * If we can't create one for the current buffer, take another buffer
92 close_buffer(NULL, curbuf, 0);
93 for (curbuf = firstbuf; curbuf != NULL; curbuf = curbuf->b_next)
94 if (curbuf->b_ml.ml_mfp != NULL)
95 break;
97 * if there is no memfile at all, exit
98 * This is OK, since there are no changes to lose.
100 if (curbuf == NULL)
102 EMSG(_("E82: Cannot allocate any buffer, exiting..."));
103 getout(2);
105 EMSG(_("E83: Cannot allocate buffer, using other one..."));
106 enter_buffer(curbuf);
107 return FAIL;
110 #ifdef FEAT_AUTOCMD
111 /* The autocommands in readfile() may change the buffer, but only AFTER
112 * reading the file. */
113 old_curbuf = curbuf;
114 modified_was_set = FALSE;
115 #endif
117 /* mark cursor position as being invalid */
118 curwin->w_valid = 0;
120 if (curbuf->b_ffname != NULL
121 #ifdef FEAT_NETBEANS_INTG
122 && netbeansReadFile
123 #endif
126 #ifdef FEAT_NETBEANS_INTG
127 int oldFire = netbeansFireChanges;
129 netbeansFireChanges = 0;
130 #endif
131 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
132 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap, READ_NEW);
133 #ifdef FEAT_NETBEANS_INTG
134 netbeansFireChanges = oldFire;
135 #endif
136 /* Help buffer is filtered. */
137 if (curbuf->b_help)
138 fix_help_buffer();
140 else if (read_stdin)
142 int save_bin = curbuf->b_p_bin;
143 linenr_T line_count;
146 * First read the text in binary mode into the buffer.
147 * Then read from that same buffer and append at the end. This makes
148 * it possible to retry when 'fileformat' or 'fileencoding' was
149 * guessed wrong.
151 curbuf->b_p_bin = TRUE;
152 retval = readfile(NULL, NULL, (linenr_T)0,
153 (linenr_T)0, (linenr_T)MAXLNUM, NULL, READ_NEW + READ_STDIN);
154 curbuf->b_p_bin = save_bin;
155 if (retval == OK)
157 line_count = curbuf->b_ml.ml_line_count;
158 retval = readfile(NULL, NULL, (linenr_T)line_count,
159 (linenr_T)0, (linenr_T)MAXLNUM, eap, READ_BUFFER);
160 if (retval == OK)
162 /* Delete the binary lines. */
163 while (--line_count >= 0)
164 ml_delete((linenr_T)1, FALSE);
166 else
168 /* Delete the converted lines. */
169 while (curbuf->b_ml.ml_line_count > line_count)
170 ml_delete(line_count, FALSE);
172 /* Put the cursor on the first line. */
173 curwin->w_cursor.lnum = 1;
174 curwin->w_cursor.col = 0;
176 /* Set or reset 'modified' before executing autocommands, so that
177 * it can be changed there. */
178 if (!readonlymode && !bufempty())
179 changed();
180 else if (retval != FAIL)
181 unchanged(curbuf, FALSE);
182 #ifdef FEAT_AUTOCMD
183 # ifdef FEAT_EVAL
184 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
185 curbuf, &retval);
186 # else
187 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
188 # endif
189 #endif
193 /* if first time loading this buffer, init b_chartab[] */
194 if (curbuf->b_flags & BF_NEVERLOADED)
195 (void)buf_init_chartab(curbuf, FALSE);
198 * Set/reset the Changed flag first, autocmds may change the buffer.
199 * Apply the automatic commands, before processing the modelines.
200 * So the modelines have priority over auto commands.
202 /* When reading stdin, the buffer contents always needs writing, so set
203 * the changed flag. Unless in readonly mode: "ls | gview -".
204 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
205 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
206 #ifdef FEAT_AUTOCMD
207 || modified_was_set /* ":set modified" used in autocmd */
208 # ifdef FEAT_EVAL
209 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
210 # endif
211 #endif
213 changed();
214 else if (retval != FAIL && !read_stdin)
215 unchanged(curbuf, FALSE);
216 save_file_ff(curbuf); /* keep this fileformat */
218 /* require "!" to overwrite the file, because it wasn't read completely */
219 #ifdef FEAT_EVAL
220 if (aborting())
221 #else
222 if (got_int)
223 #endif
224 curbuf->b_flags |= BF_READERR;
226 #ifdef FEAT_FOLDING
227 /* Need to update automatic folding. Do this before the autocommands,
228 * they may use the fold info. */
229 foldUpdateAll(curwin);
230 #endif
232 #ifdef FEAT_AUTOCMD
233 /* need to set w_topline, unless some autocommand already did that. */
234 if (!(curwin->w_valid & VALID_TOPLINE))
236 curwin->w_topline = 1;
237 # ifdef FEAT_DIFF
238 curwin->w_topfill = 0;
239 # endif
241 # ifdef FEAT_EVAL
242 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
243 # else
244 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
245 # endif
246 #endif
248 if (retval != FAIL)
250 #ifdef FEAT_AUTOCMD
252 * The autocommands may have changed the current buffer. Apply the
253 * modelines to the correct buffer, if it still exists and is loaded.
255 if (buf_valid(old_curbuf) && old_curbuf->b_ml.ml_mfp != NULL)
257 aco_save_T aco;
259 /* Go to the buffer that was opened. */
260 aucmd_prepbuf(&aco, old_curbuf);
261 #endif
262 do_modelines(0);
263 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
265 #ifdef FEAT_AUTOCMD
266 # ifdef FEAT_EVAL
267 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
268 &retval);
269 # else
270 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
271 # endif
273 /* restore curwin/curbuf and a few other things */
274 aucmd_restbuf(&aco);
276 #endif
279 return retval;
283 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
286 buf_valid(buf)
287 buf_T *buf;
289 buf_T *bp;
291 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
292 if (bp == buf)
293 return TRUE;
294 return FALSE;
298 * Close the link to a buffer.
299 * "action" is used when there is no longer a window for the buffer.
300 * It can be:
301 * 0 buffer becomes hidden
302 * DOBUF_UNLOAD buffer is unloaded
303 * DOBUF_DELETE buffer is unloaded and removed from buffer list
304 * DOBUF_WIPE buffer is unloaded and really deleted
305 * When doing all but the first one on the current buffer, the caller should
306 * get a new buffer very soon!
308 * The 'bufhidden' option can force freeing and deleting.
310 void
311 close_buffer(win, buf, action)
312 win_T *win; /* if not NULL, set b_last_cursor */
313 buf_T *buf;
314 int action;
316 #ifdef FEAT_AUTOCMD
317 int is_curbuf;
318 int nwindows;
319 #endif
320 int unload_buf = (action != 0);
321 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
322 int wipe_buf = (action == DOBUF_WIPE);
324 #ifdef FEAT_QUICKFIX
326 * Force unloading or deleting when 'bufhidden' says so.
327 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
328 * "hide" (otherwise we could never free or delete a buffer).
330 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
332 del_buf = TRUE;
333 unload_buf = TRUE;
335 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
337 del_buf = TRUE;
338 unload_buf = TRUE;
339 wipe_buf = TRUE;
341 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
342 unload_buf = TRUE;
343 #endif
345 if (win != NULL)
347 /* Set b_last_cursor when closing the last window for the buffer.
348 * Remember the last cursor position and window options of the buffer.
349 * This used to be only for the current window, but then options like
350 * 'foldmethod' may be lost with a ":only" command. */
351 if (buf->b_nwindows == 1)
352 set_last_cursor(win);
353 buflist_setfpos(buf, win,
354 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
355 win->w_cursor.col, TRUE);
358 #ifdef FEAT_AUTOCMD
359 /* When the buffer is no longer in a window, trigger BufWinLeave */
360 if (buf->b_nwindows == 1)
362 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
363 FALSE, buf);
364 if (!buf_valid(buf)) /* autocommands may delete the buffer */
365 return;
367 /* When the buffer becomes hidden, but is not unloaded, trigger
368 * BufHidden */
369 if (!unload_buf)
371 apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
372 FALSE, buf);
373 if (!buf_valid(buf)) /* autocmds may delete the buffer */
374 return;
376 # ifdef FEAT_EVAL
377 if (aborting()) /* autocmds may abort script processing */
378 return;
379 # endif
381 nwindows = buf->b_nwindows;
382 #endif
384 /* decrease the link count from windows (unless not in any window) */
385 if (buf->b_nwindows > 0)
386 --buf->b_nwindows;
388 /* Return when a window is displaying the buffer or when it's not
389 * unloaded. */
390 if (buf->b_nwindows > 0 || !unload_buf)
392 #if 0 /* why was this here? */
393 if (buf == curbuf)
394 u_sync(); /* sync undo before going to another buffer */
395 #endif
396 return;
399 /* Always remove the buffer when there is no file name. */
400 if (buf->b_ffname == NULL)
401 del_buf = TRUE;
404 * Free all things allocated for this buffer.
405 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
407 #ifdef FEAT_AUTOCMD
408 /* Remember if we are closing the current buffer. Restore the number of
409 * windows, so that autocommands in buf_freeall() don't get confused. */
410 is_curbuf = (buf == curbuf);
411 buf->b_nwindows = nwindows;
412 #endif
414 buf_freeall(buf, del_buf, wipe_buf);
416 #ifdef FEAT_AUTOCMD
417 /* Autocommands may have deleted the buffer. */
418 if (!buf_valid(buf))
419 return;
420 # ifdef FEAT_EVAL
421 if (aborting()) /* autocmds may abort script processing */
422 return;
423 # endif
425 /* Autocommands may have opened or closed windows for this buffer.
426 * Decrement the count for the close we do here. */
427 if (buf->b_nwindows > 0)
428 --buf->b_nwindows;
431 * It's possible that autocommands change curbuf to the one being deleted.
432 * This might cause the previous curbuf to be deleted unexpectedly. But
433 * in some cases it's OK to delete the curbuf, because a new one is
434 * obtained anyway. Therefore only return if curbuf changed to the
435 * deleted buffer.
437 if (buf == curbuf && !is_curbuf)
438 return;
439 #endif
441 #ifdef FEAT_ODB_EDITOR
442 odb_buffer_close(buf);
443 #endif
445 /* Change directories when the 'acd' option is set. */
446 DO_AUTOCHDIR
449 * Remove the buffer from the list.
451 if (wipe_buf)
453 #ifdef FEAT_SUN_WORKSHOP
454 if (usingSunWorkShop)
455 workshop_file_closed_lineno((char *)buf->b_ffname,
456 (int)buf->b_last_cursor.lnum);
457 #endif
458 vim_free(buf->b_ffname);
459 vim_free(buf->b_sfname);
460 if (buf->b_prev == NULL)
461 firstbuf = buf->b_next;
462 else
463 buf->b_prev->b_next = buf->b_next;
464 if (buf->b_next == NULL)
465 lastbuf = buf->b_prev;
466 else
467 buf->b_next->b_prev = buf->b_prev;
468 free_buffer(buf);
470 else
472 if (del_buf)
474 /* Free all internal variables and reset option values, to make
475 * ":bdel" compatible with Vim 5.7. */
476 free_buffer_stuff(buf, TRUE);
478 /* Make it look like a new buffer. */
479 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
481 /* Init the options when loaded again. */
482 buf->b_p_initialized = FALSE;
484 buf_clear_file(buf);
485 if (del_buf)
486 buf->b_p_bl = FALSE;
491 * Make buffer not contain a file.
493 void
494 buf_clear_file(buf)
495 buf_T *buf;
497 buf->b_ml.ml_line_count = 1;
498 unchanged(buf, TRUE);
499 #ifndef SHORT_FNAME
500 buf->b_shortname = FALSE;
501 #endif
502 buf->b_p_eol = TRUE;
503 buf->b_start_eol = TRUE;
504 #ifdef FEAT_MBYTE
505 buf->b_p_bomb = FALSE;
506 buf->b_start_bomb = FALSE;
507 #endif
508 buf->b_ml.ml_mfp = NULL;
509 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
510 #ifdef FEAT_NETBEANS_INTG
511 netbeans_deleted_all_lines(buf);
512 #endif
516 * buf_freeall() - free all things allocated for a buffer that are related to
517 * the file.
519 void
520 buf_freeall(buf, del_buf, wipe_buf)
521 buf_T *buf;
522 int del_buf UNUSED; /* buffer is going to be deleted */
523 int wipe_buf UNUSED; /* buffer is going to be wiped out */
525 #ifdef FEAT_AUTOCMD
526 int is_curbuf = (buf == curbuf);
528 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, FALSE, buf);
529 if (!buf_valid(buf)) /* autocommands may delete the buffer */
530 return;
531 if (del_buf && buf->b_p_bl)
533 apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname, FALSE, buf);
534 if (!buf_valid(buf)) /* autocommands may delete the buffer */
535 return;
537 if (wipe_buf)
539 apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
540 FALSE, buf);
541 if (!buf_valid(buf)) /* autocommands may delete the buffer */
542 return;
544 # ifdef FEAT_EVAL
545 if (aborting()) /* autocmds may abort script processing */
546 return;
547 # endif
550 * It's possible that autocommands change curbuf to the one being deleted.
551 * This might cause curbuf to be deleted unexpectedly. But in some cases
552 * it's OK to delete the curbuf, because a new one is obtained anyway.
553 * Therefore only return if curbuf changed to the deleted buffer.
555 if (buf == curbuf && !is_curbuf)
556 return;
557 #endif
558 #ifdef FEAT_DIFF
559 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
560 #endif
562 #ifdef FEAT_FOLDING
563 /* No folds in an empty buffer. */
564 # ifdef FEAT_WINDOWS
566 win_T *win;
567 tabpage_T *tp;
569 FOR_ALL_TAB_WINDOWS(tp, win)
570 if (win->w_buffer == buf)
571 clearFolding(win);
573 # else
574 if (curwin->w_buffer == buf)
575 clearFolding(curwin);
576 # endif
577 #endif
579 #ifdef FEAT_TCL
580 tcl_buffer_free(buf);
581 #endif
582 u_blockfree(buf); /* free the memory allocated for undo */
583 ml_close(buf, TRUE); /* close and delete the memline/memfile */
584 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
585 u_clearall(buf); /* reset all undo information */
586 #ifdef FEAT_SYN_HL
587 syntax_clear(buf); /* reset syntax info */
588 #endif
589 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
593 * Free a buffer structure and the things it contains related to the buffer
594 * itself (not the file, that must have been done already).
596 static void
597 free_buffer(buf)
598 buf_T *buf;
600 free_buffer_stuff(buf, TRUE);
601 #ifdef FEAT_MZSCHEME
602 mzscheme_buffer_free(buf);
603 #endif
604 #ifdef FEAT_PERL
605 perl_buf_free(buf);
606 #endif
607 #ifdef FEAT_PYTHON
608 python_buffer_free(buf);
609 #endif
610 #ifdef FEAT_RUBY
611 ruby_buffer_free(buf);
612 #endif
613 #ifdef FEAT_AUTOCMD
614 aubuflocal_remove(buf);
615 #endif
616 vim_free(buf);
620 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
622 static void
623 free_buffer_stuff(buf, free_options)
624 buf_T *buf;
625 int free_options; /* free options as well */
627 if (free_options)
629 clear_wininfo(buf); /* including window-local options */
630 free_buf_options(buf, TRUE);
632 #ifdef FEAT_EVAL
633 vars_clear(&buf->b_vars.dv_hashtab); /* free all internal variables */
634 hash_init(&buf->b_vars.dv_hashtab);
635 #endif
636 #ifdef FEAT_USR_CMDS
637 uc_clear(&buf->b_ucmds); /* clear local user commands */
638 #endif
639 #ifdef FEAT_SIGNS
640 buf_delete_signs(buf); /* delete any signs */
641 #endif
642 #ifdef FEAT_NETBEANS_INTG
643 if (usingNetbeans)
644 netbeans_file_killed(buf);
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
654 #ifdef FEAT_SPELL
655 ga_clear(&buf->b_langp);
656 #endif
660 * Free the b_wininfo list for buffer "buf".
662 static void
663 clear_wininfo(buf)
664 buf_T *buf;
666 wininfo_T *wip;
668 while (buf->b_wininfo != NULL)
670 wip = buf->b_wininfo;
671 buf->b_wininfo = wip->wi_next;
672 if (wip->wi_optset)
674 clear_winopt(&wip->wi_opt);
675 #ifdef FEAT_FOLDING
676 deleteFoldRecurse(&wip->wi_folds);
677 #endif
679 vim_free(wip);
683 #if defined(FEAT_LISTCMDS) || defined(PROTO)
685 * Go to another buffer. Handles the result of the ATTENTION dialog.
687 void
688 goto_buffer(eap, start, dir, count)
689 exarg_T *eap;
690 int start;
691 int dir;
692 int count;
694 # if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
695 buf_T *old_curbuf = curbuf;
697 swap_exists_action = SEA_DIALOG;
698 # endif
699 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
700 start, dir, count, eap->forceit);
701 # if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
702 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
704 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
705 cleanup_T cs;
707 /* Reset the error/interrupt/exception state here so that
708 * aborting() returns FALSE when closing a window. */
709 enter_cleanup(&cs);
710 # endif
712 /* Quitting means closing the split window, nothing else. */
713 win_close(curwin, TRUE);
714 swap_exists_action = SEA_NONE;
715 swap_exists_did_quit = TRUE;
717 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
718 /* Restore the error/interrupt/exception state if not discarded by a
719 * new aborting error, interrupt, or uncaught exception. */
720 leave_cleanup(&cs);
721 # endif
723 else
724 handle_swap_exists(old_curbuf);
725 # endif
727 #endif
729 #if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
731 * Handle the situation of swap_exists_action being set.
732 * It is allowed for "old_curbuf" to be NULL or invalid.
734 void
735 handle_swap_exists(old_curbuf)
736 buf_T *old_curbuf;
738 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
739 cleanup_T cs;
740 # endif
742 if (swap_exists_action == SEA_QUIT)
744 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
745 /* Reset the error/interrupt/exception state here so that
746 * aborting() returns FALSE when closing a buffer. */
747 enter_cleanup(&cs);
748 # endif
750 /* User selected Quit at ATTENTION prompt. Go back to previous
751 * buffer. If that buffer is gone or the same as the current one,
752 * open a new, empty buffer. */
753 swap_exists_action = SEA_NONE; /* don't want it again */
754 swap_exists_did_quit = TRUE;
755 close_buffer(curwin, curbuf, DOBUF_UNLOAD);
756 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
757 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
758 if (old_curbuf != NULL)
759 enter_buffer(old_curbuf);
760 /* If "old_curbuf" is NULL we are in big trouble here... */
762 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
763 /* Restore the error/interrupt/exception state if not discarded by a
764 * new aborting error, interrupt, or uncaught exception. */
765 leave_cleanup(&cs);
766 # endif
768 else if (swap_exists_action == SEA_RECOVER)
770 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
771 /* Reset the error/interrupt/exception state here so that
772 * aborting() returns FALSE when closing a buffer. */
773 enter_cleanup(&cs);
774 # endif
776 /* User selected Recover at ATTENTION prompt. */
777 msg_scroll = TRUE;
778 ml_recover();
779 MSG_PUTS("\n"); /* don't overwrite the last message */
780 cmdline_row = msg_row;
781 do_modelines(0);
783 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
784 /* Restore the error/interrupt/exception state if not discarded by a
785 * new aborting error, interrupt, or uncaught exception. */
786 leave_cleanup(&cs);
787 # endif
789 swap_exists_action = SEA_NONE;
791 #endif
793 #if defined(FEAT_LISTCMDS) || defined(PROTO)
795 * do_bufdel() - delete or unload buffer(s)
797 * addr_count == 0: ":bdel" - delete current buffer
798 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
799 * buffer "end_bnr", then any other arguments.
800 * addr_count == 2: ":N,N bdel" - delete buffers in range
802 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
803 * DOBUF_DEL (":bdel")
805 * Returns error message or NULL
807 char_u *
808 do_bufdel(command, arg, addr_count, start_bnr, end_bnr, forceit)
809 int command;
810 char_u *arg; /* pointer to extra arguments */
811 int addr_count;
812 int start_bnr; /* first buffer number in a range */
813 int end_bnr; /* buffer nr or last buffer nr in a range */
814 int forceit;
816 int do_current = 0; /* delete current buffer? */
817 int deleted = 0; /* number of buffers deleted */
818 char_u *errormsg = NULL; /* return value */
819 int bnr; /* buffer number */
820 char_u *p;
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);
917 return errormsg;
921 * Implementation of the commands for the buffer list.
923 * action == DOBUF_GOTO go to specified buffer
924 * action == DOBUF_SPLIT split window and go to specified buffer
925 * action == DOBUF_UNLOAD unload specified buffer(s)
926 * action == DOBUF_DEL delete specified buffer(s) from buffer list
927 * action == DOBUF_WIPE delete specified buffer(s) really
929 * start == DOBUF_CURRENT go to "count" buffer from current buffer
930 * start == DOBUF_FIRST go to "count" buffer from first buffer
931 * start == DOBUF_LAST go to "count" buffer from last buffer
932 * start == DOBUF_MOD go to "count" modified buffer from current buffer
934 * Return FAIL or OK.
937 do_buffer(action, start, dir, count, forceit)
938 int action;
939 int start;
940 int dir; /* FORWARD or BACKWARD */
941 int count; /* buffer number or number of buffers */
942 int forceit; /* TRUE for :...! */
944 buf_T *buf;
945 buf_T *bp;
946 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
947 || action == DOBUF_WIPE);
949 switch (start)
951 case DOBUF_FIRST: buf = firstbuf; break;
952 case DOBUF_LAST: buf = lastbuf; break;
953 default: buf = curbuf; break;
955 if (start == DOBUF_MOD) /* find next modified buffer */
957 while (count-- > 0)
961 buf = buf->b_next;
962 if (buf == NULL)
963 buf = firstbuf;
965 while (buf != curbuf && !bufIsChanged(buf));
967 if (!bufIsChanged(buf))
969 EMSG(_("E84: No modified buffer found"));
970 return FAIL;
973 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
975 while (buf != NULL && buf->b_fnum != count)
976 buf = buf->b_next;
978 else
980 bp = NULL;
981 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
983 /* remember the buffer where we start, we come back there when all
984 * buffers are unlisted. */
985 if (bp == NULL)
986 bp = buf;
987 if (dir == FORWARD)
989 buf = buf->b_next;
990 if (buf == NULL)
991 buf = firstbuf;
993 else
995 buf = buf->b_prev;
996 if (buf == NULL)
997 buf = lastbuf;
999 /* don't count unlisted buffers */
1000 if (unload || buf->b_p_bl)
1002 --count;
1003 bp = NULL; /* use this buffer as new starting point */
1005 if (bp == buf)
1007 /* back where we started, didn't find anything. */
1008 EMSG(_("E85: There is no listed buffer"));
1009 return FAIL;
1014 if (buf == NULL) /* could not find it */
1016 if (start == DOBUF_FIRST)
1018 /* don't warn when deleting */
1019 if (!unload)
1020 EMSGN(_("E86: Buffer %ld does not exist"), count);
1022 else if (dir == FORWARD)
1023 EMSG(_("E87: Cannot go beyond last buffer"));
1024 else
1025 EMSG(_("E88: Cannot go before first buffer"));
1026 return FAIL;
1029 #ifdef FEAT_GUI
1030 need_mouse_correct = TRUE;
1031 #endif
1033 #ifdef FEAT_LISTCMDS
1035 * delete buffer buf from memory and/or the list
1037 if (unload)
1039 int forward;
1040 int retval;
1042 /* When unloading or deleting a buffer that's already unloaded and
1043 * unlisted: fail silently. */
1044 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1045 return FAIL;
1047 if (!forceit && bufIsChanged(buf))
1049 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1050 if ((p_confirm || cmdmod.confirm) && p_write)
1052 dialog_changed(buf, FALSE);
1053 # ifdef FEAT_AUTOCMD
1054 if (!buf_valid(buf))
1055 /* Autocommand deleted buffer, oops! It's not changed
1056 * now. */
1057 return FAIL;
1058 # endif
1059 /* If it's still changed fail silently, the dialog already
1060 * mentioned why it fails. */
1061 if (bufIsChanged(buf))
1062 return FAIL;
1064 else
1065 #endif
1067 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1068 buf->b_fnum);
1069 return FAIL;
1074 * If deleting the last (listed) buffer, make it empty.
1075 * The last (listed) buffer cannot be unloaded.
1077 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1078 if (bp->b_p_bl && bp != buf)
1079 break;
1080 if (bp == NULL && buf == curbuf)
1082 if (action == DOBUF_UNLOAD)
1084 EMSG(_("E90: Cannot unload last buffer"));
1085 return FAIL;
1088 /* Close any other windows on this buffer, then make it empty. */
1089 #ifdef FEAT_WINDOWS
1090 close_windows(buf, TRUE);
1091 #endif
1092 setpcmark();
1093 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1094 forceit ? ECMD_FORCEIT : 0, curwin);
1097 * do_ecmd() may create a new buffer, then we have to delete
1098 * the old one. But do_ecmd() may have done that already, check
1099 * if the buffer still exists.
1101 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
1102 close_buffer(NULL, buf, action);
1103 return retval;
1106 #ifdef FEAT_WINDOWS
1108 * If the deleted buffer is the current one, close the current window
1109 * (unless it's the only window). Repeat this so long as we end up in
1110 * a window with this buffer.
1112 while (buf == curbuf
1113 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
1114 win_close(curwin, FALSE);
1115 #endif
1118 * If the buffer to be deleted is not the current one, delete it here.
1120 if (buf != curbuf)
1122 #ifdef FEAT_WINDOWS
1123 close_windows(buf, FALSE);
1124 #endif
1125 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
1126 close_buffer(NULL, buf, action);
1127 return OK;
1131 * Deleting the current buffer: Need to find another buffer to go to.
1132 * There must be another, otherwise it would have been handled above.
1133 * First use au_new_curbuf, if it is valid.
1134 * Then prefer the buffer we most recently visited.
1135 * Else try to find one that is loaded, after the current buffer,
1136 * then before the current buffer.
1137 * Finally use any buffer.
1139 buf = NULL; /* selected buffer */
1140 bp = NULL; /* used when no loaded buffer found */
1141 #ifdef FEAT_AUTOCMD
1142 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1143 buf = au_new_curbuf;
1144 # ifdef FEAT_JUMPLIST
1145 else
1146 # endif
1147 #endif
1148 #ifdef FEAT_JUMPLIST
1149 if (curwin->w_jumplistlen > 0)
1151 int jumpidx;
1153 jumpidx = curwin->w_jumplistidx - 1;
1154 if (jumpidx < 0)
1155 jumpidx = curwin->w_jumplistlen - 1;
1157 forward = jumpidx;
1158 while (jumpidx != curwin->w_jumplistidx)
1160 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1161 if (buf != NULL)
1163 if (buf == curbuf || !buf->b_p_bl)
1164 buf = NULL; /* skip current and unlisted bufs */
1165 else if (buf->b_ml.ml_mfp == NULL)
1167 /* skip unloaded buf, but may keep it for later */
1168 if (bp == NULL)
1169 bp = buf;
1170 buf = NULL;
1173 if (buf != NULL) /* found a valid buffer: stop searching */
1174 break;
1175 /* advance to older entry in jump list */
1176 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1177 break;
1178 if (--jumpidx < 0)
1179 jumpidx = curwin->w_jumplistlen - 1;
1180 if (jumpidx == forward) /* List exhausted for sure */
1181 break;
1184 #endif
1186 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1188 forward = TRUE;
1189 buf = curbuf->b_next;
1190 for (;;)
1192 if (buf == NULL)
1194 if (!forward) /* tried both directions */
1195 break;
1196 buf = curbuf->b_prev;
1197 forward = FALSE;
1198 continue;
1200 /* in non-help buffer, try to skip help buffers, and vv */
1201 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1203 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1204 break;
1205 if (bp == NULL) /* remember unloaded buf for later */
1206 bp = buf;
1208 if (forward)
1209 buf = buf->b_next;
1210 else
1211 buf = buf->b_prev;
1214 if (buf == NULL) /* No loaded buffer, use unloaded one */
1215 buf = bp;
1216 if (buf == NULL) /* No loaded buffer, find listed one */
1218 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1219 if (buf->b_p_bl && buf != curbuf)
1220 break;
1222 if (buf == NULL) /* Still no buffer, just take one */
1224 if (curbuf->b_next != NULL)
1225 buf = curbuf->b_next;
1226 else
1227 buf = curbuf->b_prev;
1232 * make buf current buffer
1234 if (action == DOBUF_SPLIT) /* split window first */
1236 # ifdef FEAT_WINDOWS
1237 /* If 'switchbuf' contains "useopen": jump to first window containing
1238 * "buf" if one exists */
1239 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
1240 return OK;
1241 /* If 'switchbuf' contains "usetab": jump to first window in any tab
1242 * page containing "buf" if one exists */
1243 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
1244 return OK;
1245 if (win_split(0, 0) == FAIL)
1246 # endif
1247 return FAIL;
1249 #endif
1251 /* go to current buffer - nothing to do */
1252 if (buf == curbuf)
1253 return OK;
1256 * Check if the current buffer may be abandoned.
1258 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1260 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1261 if ((p_confirm || cmdmod.confirm) && p_write)
1263 dialog_changed(curbuf, FALSE);
1264 # ifdef FEAT_AUTOCMD
1265 if (!buf_valid(buf))
1266 /* Autocommand deleted buffer, oops! */
1267 return FAIL;
1268 # endif
1270 if (bufIsChanged(curbuf))
1271 #endif
1273 EMSG(_(e_nowrtmsg));
1274 return FAIL;
1278 /* Go to the other buffer. */
1279 set_curbuf(buf, action);
1281 #if defined(FEAT_LISTCMDS) && defined(FEAT_SCROLLBIND)
1282 if (action == DOBUF_SPLIT)
1283 curwin->w_p_scb = FALSE; /* reset 'scrollbind' */
1284 #endif
1286 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1287 if (aborting()) /* autocmds may abort script processing */
1288 return FAIL;
1289 #endif
1291 return OK;
1294 #endif /* FEAT_LISTCMDS */
1297 * Set current buffer to "buf". Executes autocommands and closes current
1298 * buffer. "action" tells how to close the current buffer:
1299 * DOBUF_GOTO free or hide it
1300 * DOBUF_SPLIT nothing
1301 * DOBUF_UNLOAD unload it
1302 * DOBUF_DEL delete it
1303 * DOBUF_WIPE wipe it out
1305 void
1306 set_curbuf(buf, action)
1307 buf_T *buf;
1308 int action;
1310 buf_T *prevbuf;
1311 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1312 || action == DOBUF_WIPE);
1314 setpcmark();
1315 if (!cmdmod.keepalt)
1316 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
1317 buflist_altfpos(curwin); /* remember curpos */
1319 #ifdef FEAT_VISUAL
1320 /* Don't restart Select mode after switching to another buffer. */
1321 VIsual_reselect = FALSE;
1322 #endif
1324 /* close_windows() or apply_autocmds() may change curbuf */
1325 prevbuf = curbuf;
1327 #ifdef FEAT_AUTOCMD
1328 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1329 # ifdef FEAT_EVAL
1330 if (buf_valid(prevbuf) && !aborting())
1331 # else
1332 if (buf_valid(prevbuf))
1333 # endif
1334 #endif
1336 #ifdef FEAT_WINDOWS
1337 if (unload)
1338 close_windows(prevbuf, FALSE);
1339 #endif
1340 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1341 if (buf_valid(prevbuf) && !aborting())
1342 #else
1343 if (buf_valid(prevbuf))
1344 #endif
1346 if (prevbuf == curbuf)
1347 u_sync(FALSE);
1348 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1349 unload ? action : (action == DOBUF_GOTO
1350 && !P_HID(prevbuf)
1351 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0);
1354 #ifdef FEAT_AUTOCMD
1355 /* An autocommand may have deleted "buf", already entered it (e.g., when
1356 * it did ":bunload") or aborted the script processing! */
1357 # ifdef FEAT_EVAL
1358 if (buf_valid(buf) && buf != curbuf && !aborting())
1359 # else
1360 if (buf_valid(buf) && buf != curbuf)
1361 # endif
1362 #endif
1363 enter_buffer(buf);
1367 * Enter a new current buffer.
1368 * Old curbuf must have been abandoned already!
1370 void
1371 enter_buffer(buf)
1372 buf_T *buf;
1374 /* Copy buffer and window local option values. Not for a help buffer. */
1375 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1376 if (!buf->b_help)
1377 get_winopts(buf);
1378 #ifdef FEAT_FOLDING
1379 else
1380 /* Remove all folds in the window. */
1381 clearFolding(curwin);
1382 foldUpdateAll(curwin); /* update folds (later). */
1383 #endif
1385 /* Get the buffer in the current window. */
1386 curwin->w_buffer = buf;
1387 curbuf = buf;
1388 ++curbuf->b_nwindows;
1390 #ifdef FEAT_DIFF
1391 if (curwin->w_p_diff)
1392 diff_buf_add(curbuf);
1393 #endif
1395 /* Cursor on first line by default. */
1396 curwin->w_cursor.lnum = 1;
1397 curwin->w_cursor.col = 0;
1398 #ifdef FEAT_VIRTUALEDIT
1399 curwin->w_cursor.coladd = 0;
1400 #endif
1401 curwin->w_set_curswant = TRUE;
1402 #ifdef FEAT_AUTOCMD
1403 curwin->w_topline_was_set = FALSE;
1404 #endif
1406 /* mark cursor position as being invalid */
1407 curwin->w_valid = 0;
1409 /* Make sure the buffer is loaded. */
1410 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
1412 #ifdef FEAT_AUTOCMD
1413 /* If there is no filetype, allow for detecting one. Esp. useful for
1414 * ":ball" used in a autocommand. If there already is a filetype we
1415 * might prefer to keep it. */
1416 if (*curbuf->b_p_ft == NUL)
1417 did_filetype = FALSE;
1418 #endif
1420 open_buffer(FALSE, NULL);
1422 else
1424 if (!msg_silent)
1425 need_fileinfo = TRUE; /* display file info after redraw */
1426 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1427 #ifdef FEAT_AUTOCMD
1428 curwin->w_topline = 1;
1429 # ifdef FEAT_DIFF
1430 curwin->w_topfill = 0;
1431 # endif
1432 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1433 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1434 #endif
1437 /* If autocommands did not change the cursor position, restore cursor lnum
1438 * and possibly cursor col. */
1439 if (curwin->w_cursor.lnum == 1 && inindent(0))
1440 buflist_getfpos();
1442 check_arg_idx(curwin); /* check for valid arg_idx */
1443 #ifdef FEAT_TITLE
1444 maketitle();
1445 #endif
1446 #ifdef FEAT_AUTOCMD
1447 /* when autocmds didn't change it */
1448 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
1449 #endif
1450 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1452 #ifdef FEAT_NETBEANS_INTG
1453 /* Send fileOpened event because we've changed buffers. */
1454 if (usingNetbeans && isNetbeansBuffer(curbuf))
1455 netbeans_file_activated(curbuf);
1456 #endif
1458 /* Change directories when the 'acd' option is set. */
1459 DO_AUTOCHDIR
1461 #ifdef FEAT_KEYMAP
1462 if (curbuf->b_kmap_state & KEYMAP_INIT)
1463 (void)keymap_init();
1464 #endif
1465 #ifdef FEAT_SPELL
1466 /* May need to set the spell language. Can only do this after the buffer
1467 * has been properly setup. */
1468 if (!curbuf->b_help && curwin->w_p_spell && *curbuf->b_p_spl != NUL)
1469 (void)did_set_spelllang(curbuf);
1470 #endif
1472 redraw_later(NOT_VALID);
1475 #if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1477 * Change to the directory of the current buffer.
1479 void
1480 do_autochdir()
1482 if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK)
1483 shorten_fnames(TRUE);
1485 #endif
1488 * functions for dealing with the buffer list
1492 * Add a file name to the buffer list. Return a pointer to the buffer.
1493 * If the same file name already exists return a pointer to that buffer.
1494 * If it does not exist, or if fname == NULL, a new entry is created.
1495 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1496 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1497 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
1498 * This is the ONLY way to create a new buffer.
1500 static int top_file_num = 1; /* highest file number */
1502 buf_T *
1503 buflist_new(ffname, sfname, lnum, flags)
1504 char_u *ffname; /* full path of fname or relative */
1505 char_u *sfname; /* short fname or NULL */
1506 linenr_T lnum; /* preferred cursor line */
1507 int flags; /* BLN_ defines */
1509 buf_T *buf;
1510 #ifdef UNIX
1511 struct stat st;
1512 #endif
1514 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1517 * If file name already exists in the list, update the entry.
1519 #ifdef UNIX
1520 /* On Unix we can use inode numbers when the file exists. Works better
1521 * for hard links. */
1522 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1523 st.st_dev = (dev_T)-1;
1524 #endif
1525 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1526 #ifdef UNIX
1527 buflist_findname_stat(ffname, &st)
1528 #else
1529 buflist_findname(ffname)
1530 #endif
1531 ) != NULL)
1533 vim_free(ffname);
1534 if (lnum != 0)
1535 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1536 /* copy the options now, if 'cpo' doesn't have 's' and not done
1537 * already */
1538 buf_copy_options(buf, 0);
1539 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1541 buf->b_p_bl = TRUE;
1542 #ifdef FEAT_AUTOCMD
1543 if (!(flags & BLN_DUMMY))
1544 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1545 #endif
1547 return buf;
1551 * If the current buffer has no name and no contents, use the current
1552 * buffer. Otherwise: Need to allocate a new buffer structure.
1554 * This is the ONLY place where a new buffer structure is allocated!
1555 * (A spell file buffer is allocated in spell.c, but that's not a normal
1556 * buffer.)
1558 buf = NULL;
1559 if ((flags & BLN_CURBUF)
1560 && curbuf != NULL
1561 && curbuf->b_ffname == NULL
1562 && curbuf->b_nwindows <= 1
1563 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1565 buf = curbuf;
1566 #ifdef FEAT_AUTOCMD
1567 /* It's like this buffer is deleted. Watch out for autocommands that
1568 * change curbuf! If that happens, allocate a new buffer anyway. */
1569 if (curbuf->b_p_bl)
1570 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1571 if (buf == curbuf)
1572 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1573 # ifdef FEAT_EVAL
1574 if (aborting()) /* autocmds may abort script processing */
1575 return NULL;
1576 # endif
1577 #endif
1578 #ifdef FEAT_QUICKFIX
1579 # ifdef FEAT_AUTOCMD
1580 if (buf == curbuf)
1581 # endif
1583 /* Make sure 'bufhidden' and 'buftype' are empty */
1584 clear_string_option(&buf->b_p_bh);
1585 clear_string_option(&buf->b_p_bt);
1587 #endif
1589 if (buf != curbuf || curbuf == NULL)
1591 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1592 if (buf == NULL)
1594 vim_free(ffname);
1595 return NULL;
1599 if (ffname != NULL)
1601 buf->b_ffname = ffname;
1602 buf->b_sfname = vim_strsave(sfname);
1605 clear_wininfo(buf);
1606 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1608 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1609 || buf->b_wininfo == NULL)
1611 vim_free(buf->b_ffname);
1612 buf->b_ffname = NULL;
1613 vim_free(buf->b_sfname);
1614 buf->b_sfname = NULL;
1615 if (buf != curbuf)
1616 free_buffer(buf);
1617 return NULL;
1620 if (buf == curbuf)
1622 /* free all things allocated for this buffer */
1623 buf_freeall(buf, FALSE, FALSE);
1624 if (buf != curbuf) /* autocommands deleted the buffer! */
1625 return NULL;
1626 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1627 if (aborting()) /* autocmds may abort script processing */
1628 return NULL;
1629 #endif
1630 /* buf->b_nwindows = 0; why was this here? */
1631 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
1632 #ifdef FEAT_KEYMAP
1633 /* need to reload lmaps and set b:keymap_name */
1634 curbuf->b_kmap_state |= KEYMAP_INIT;
1635 #endif
1637 else
1640 * put new buffer at the end of the buffer list
1642 buf->b_next = NULL;
1643 if (firstbuf == NULL) /* buffer list is empty */
1645 buf->b_prev = NULL;
1646 firstbuf = buf;
1648 else /* append new buffer at end of list */
1650 lastbuf->b_next = buf;
1651 buf->b_prev = lastbuf;
1653 lastbuf = buf;
1655 buf->b_fnum = top_file_num++;
1656 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1658 EMSG(_("W14: Warning: List of file names overflow"));
1659 if (emsg_silent == 0)
1661 out_flush();
1662 ui_delay(3000L, TRUE); /* make sure it is noticed */
1664 top_file_num = 1;
1668 * Always copy the options from the current buffer.
1670 buf_copy_options(buf, BCO_ALWAYS);
1673 buf->b_wininfo->wi_fpos.lnum = lnum;
1674 buf->b_wininfo->wi_win = curwin;
1676 #ifdef FEAT_EVAL
1677 init_var_dict(&buf->b_vars, &buf->b_bufvar); /* init b: variables */
1678 #endif
1679 #ifdef FEAT_SYN_HL
1680 hash_init(&buf->b_keywtab);
1681 hash_init(&buf->b_keywtab_ic);
1682 #endif
1684 buf->b_fname = buf->b_sfname;
1685 #ifdef UNIX
1686 if (st.st_dev == (dev_T)-1)
1687 buf->b_dev_valid = FALSE;
1688 else
1690 buf->b_dev_valid = TRUE;
1691 buf->b_dev = st.st_dev;
1692 buf->b_ino = st.st_ino;
1694 #endif
1695 buf->b_u_synced = TRUE;
1696 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
1697 if (flags & BLN_DUMMY)
1698 buf->b_flags |= BF_DUMMY;
1699 buf_clear_file(buf);
1700 clrallmarks(buf); /* clear marks */
1701 fmarks_check_names(buf); /* check file marks for this file */
1702 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1703 #ifdef FEAT_AUTOCMD
1704 if (!(flags & BLN_DUMMY))
1706 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
1707 if (flags & BLN_LISTED)
1708 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1709 # ifdef FEAT_EVAL
1710 if (aborting()) /* autocmds may abort script processing */
1711 return NULL;
1712 # endif
1714 #endif
1716 return buf;
1720 * Free the memory for the options of a buffer.
1721 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1722 * 'fileencoding'.
1724 void
1725 free_buf_options(buf, free_p_ff)
1726 buf_T *buf;
1727 int free_p_ff;
1729 if (free_p_ff)
1731 #ifdef FEAT_MBYTE
1732 clear_string_option(&buf->b_p_fenc);
1733 #endif
1734 clear_string_option(&buf->b_p_ff);
1735 #ifdef FEAT_QUICKFIX
1736 clear_string_option(&buf->b_p_bh);
1737 clear_string_option(&buf->b_p_bt);
1738 #endif
1740 #ifdef FEAT_FIND_ID
1741 clear_string_option(&buf->b_p_def);
1742 clear_string_option(&buf->b_p_inc);
1743 # ifdef FEAT_EVAL
1744 clear_string_option(&buf->b_p_inex);
1745 # endif
1746 #endif
1747 #if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1748 clear_string_option(&buf->b_p_inde);
1749 clear_string_option(&buf->b_p_indk);
1750 #endif
1751 #if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1752 clear_string_option(&buf->b_p_bexpr);
1753 #endif
1754 #if defined(FEAT_EVAL)
1755 clear_string_option(&buf->b_p_fex);
1756 #endif
1757 #ifdef FEAT_CRYPT
1758 clear_string_option(&buf->b_p_key);
1759 #endif
1760 clear_string_option(&buf->b_p_kp);
1761 clear_string_option(&buf->b_p_mps);
1762 clear_string_option(&buf->b_p_fo);
1763 clear_string_option(&buf->b_p_flp);
1764 clear_string_option(&buf->b_p_isk);
1765 #ifdef FEAT_KEYMAP
1766 clear_string_option(&buf->b_p_keymap);
1767 ga_clear(&buf->b_kmap_ga);
1768 #endif
1769 #ifdef FEAT_COMMENTS
1770 clear_string_option(&buf->b_p_com);
1771 #endif
1772 #ifdef FEAT_FOLDING
1773 clear_string_option(&buf->b_p_cms);
1774 #endif
1775 clear_string_option(&buf->b_p_nf);
1776 #ifdef FEAT_SYN_HL
1777 clear_string_option(&buf->b_p_syn);
1778 #endif
1779 #ifdef FEAT_SPELL
1780 clear_string_option(&buf->b_p_spc);
1781 clear_string_option(&buf->b_p_spf);
1782 vim_free(buf->b_cap_prog);
1783 buf->b_cap_prog = NULL;
1784 clear_string_option(&buf->b_p_spl);
1785 #endif
1786 #ifdef FEAT_SEARCHPATH
1787 clear_string_option(&buf->b_p_sua);
1788 #endif
1789 #ifdef FEAT_AUTOCMD
1790 clear_string_option(&buf->b_p_ft);
1791 #endif
1792 #ifdef FEAT_OSFILETYPE
1793 clear_string_option(&buf->b_p_oft);
1794 #endif
1795 #ifdef FEAT_CINDENT
1796 clear_string_option(&buf->b_p_cink);
1797 clear_string_option(&buf->b_p_cino);
1798 #endif
1799 #if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1800 clear_string_option(&buf->b_p_cinw);
1801 #endif
1802 #ifdef FEAT_INS_EXPAND
1803 clear_string_option(&buf->b_p_cpt);
1804 #endif
1805 #ifdef FEAT_COMPL_FUNC
1806 clear_string_option(&buf->b_p_cfu);
1807 clear_string_option(&buf->b_p_ofu);
1808 #endif
1809 #ifdef FEAT_QUICKFIX
1810 clear_string_option(&buf->b_p_gp);
1811 clear_string_option(&buf->b_p_mp);
1812 clear_string_option(&buf->b_p_efm);
1813 #endif
1814 clear_string_option(&buf->b_p_ep);
1815 clear_string_option(&buf->b_p_path);
1816 clear_string_option(&buf->b_p_tags);
1817 #ifdef FEAT_INS_EXPAND
1818 clear_string_option(&buf->b_p_dict);
1819 clear_string_option(&buf->b_p_tsr);
1820 #endif
1821 #ifdef FEAT_TEXTOBJ
1822 clear_string_option(&buf->b_p_qe);
1823 #endif
1824 buf->b_p_ar = -1;
1828 * get alternate file n
1829 * set linenr to lnum or altfpos.lnum if lnum == 0
1830 * also set cursor column to altfpos.col if 'startofline' is not set.
1831 * if (options & GETF_SETMARK) call setpcmark()
1832 * if (options & GETF_ALT) we are jumping to an alternate file.
1833 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1835 * return FAIL for failure, OK for success
1838 buflist_getfile(n, lnum, options, forceit)
1839 int n;
1840 linenr_T lnum;
1841 int options;
1842 int forceit;
1844 buf_T *buf;
1845 #ifdef FEAT_WINDOWS
1846 win_T *wp = NULL;
1847 #endif
1848 pos_T *fpos;
1849 colnr_T col;
1851 buf = buflist_findnr(n);
1852 if (buf == NULL)
1854 if ((options & GETF_ALT) && n == 0)
1855 EMSG(_(e_noalt));
1856 else
1857 EMSGN(_("E92: Buffer %ld not found"), n);
1858 return FAIL;
1861 /* if alternate file is the current buffer, nothing to do */
1862 if (buf == curbuf)
1863 return OK;
1865 if (text_locked())
1867 text_locked_msg();
1868 return FAIL;
1870 #ifdef FEAT_AUTOCMD
1871 if (curbuf_locked())
1872 return FAIL;
1873 #endif
1875 /* altfpos may be changed by getfile(), get it now */
1876 if (lnum == 0)
1878 fpos = buflist_findfpos(buf);
1879 lnum = fpos->lnum;
1880 col = fpos->col;
1882 else
1883 col = 0;
1885 #ifdef FEAT_WINDOWS
1886 if (options & GETF_SWITCH)
1888 /* If 'switchbuf' contains "useopen": jump to first window containing
1889 * "buf" if one exists */
1890 if (swb_flags & SWB_USEOPEN)
1891 wp = buf_jump_open_win(buf);
1892 /* If 'switchbuf' contians "usetab": jump to first window in any tab
1893 * page containing "buf" if one exists */
1894 if (wp == NULL && (swb_flags & SWB_USETAB))
1895 wp = buf_jump_open_tab(buf);
1896 /* If 'switchbuf' contains "split" or "newtab" and the current buffer
1897 * isn't empty: open new window */
1898 if (wp == NULL && (swb_flags & (SWB_SPLIT | SWB_NEWTAB)) && !bufempty())
1900 if (swb_flags & SWB_NEWTAB) /* Open in a new tab */
1901 tabpage_new();
1902 else if (win_split(0, 0) == FAIL) /* Open in a new window */
1903 return FAIL;
1904 # ifdef FEAT_SCROLLBIND
1905 curwin->w_p_scb = FALSE;
1906 # endif
1909 #endif
1911 ++RedrawingDisabled;
1912 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
1913 lnum, forceit) <= 0)
1915 --RedrawingDisabled;
1917 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
1918 if (!p_sol && col != 0)
1920 curwin->w_cursor.col = col;
1921 check_cursor_col();
1922 #ifdef FEAT_VIRTUALEDIT
1923 curwin->w_cursor.coladd = 0;
1924 #endif
1925 curwin->w_set_curswant = TRUE;
1927 return OK;
1929 --RedrawingDisabled;
1930 return FAIL;
1934 * go to the last know line number for the current buffer
1936 void
1937 buflist_getfpos()
1939 pos_T *fpos;
1941 fpos = buflist_findfpos(curbuf);
1943 curwin->w_cursor.lnum = fpos->lnum;
1944 check_cursor_lnum();
1946 if (p_sol)
1947 curwin->w_cursor.col = 0;
1948 else
1950 curwin->w_cursor.col = fpos->col;
1951 check_cursor_col();
1952 #ifdef FEAT_VIRTUALEDIT
1953 curwin->w_cursor.coladd = 0;
1954 #endif
1955 curwin->w_set_curswant = TRUE;
1959 #if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
1961 * Find file in buffer list by name (it has to be for the current window).
1962 * Returns NULL if not found.
1964 buf_T *
1965 buflist_findname_exp(fname)
1966 char_u *fname;
1968 char_u *ffname;
1969 buf_T *buf = NULL;
1971 /* First make the name into a full path name */
1972 ffname = FullName_save(fname,
1973 #ifdef UNIX
1974 TRUE /* force expansion, get rid of symbolic links */
1975 #else
1976 FALSE
1977 #endif
1979 if (ffname != NULL)
1981 buf = buflist_findname(ffname);
1982 vim_free(ffname);
1984 return buf;
1986 #endif
1989 * Find file in buffer list by name (it has to be for the current window).
1990 * "ffname" must have a full path.
1991 * Skips dummy buffers.
1992 * Returns NULL if not found.
1994 buf_T *
1995 buflist_findname(ffname)
1996 char_u *ffname;
1998 #ifdef UNIX
1999 struct stat st;
2001 if (mch_stat((char *)ffname, &st) < 0)
2002 st.st_dev = (dev_T)-1;
2003 return buflist_findname_stat(ffname, &st);
2007 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2008 * twice for the same file.
2009 * Returns NULL if not found.
2011 static buf_T *
2012 buflist_findname_stat(ffname, stp)
2013 char_u *ffname;
2014 struct stat *stp;
2016 #endif
2017 buf_T *buf;
2019 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2020 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
2021 #ifdef UNIX
2022 , stp
2023 #endif
2025 return buf;
2026 return NULL;
2029 #if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO)
2031 * Find file in buffer list by a regexp pattern.
2032 * Return fnum of the found buffer.
2033 * Return < 0 for error.
2036 buflist_findpat(pattern, pattern_end, unlisted, diffmode)
2037 char_u *pattern;
2038 char_u *pattern_end; /* pointer to first char after pattern */
2039 int unlisted; /* find unlisted buffers */
2040 int diffmode UNUSED; /* find diff-mode buffers only */
2042 buf_T *buf;
2043 regprog_T *prog;
2044 int match = -1;
2045 int find_listed;
2046 char_u *pat;
2047 char_u *patend;
2048 int attempt;
2049 char_u *p;
2050 int toggledollar;
2052 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2054 if (*pattern == '%')
2055 match = curbuf->b_fnum;
2056 else
2057 match = curwin->w_alt_fnum;
2058 #ifdef FEAT_DIFF
2059 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2060 match = -1;
2061 #endif
2065 * Try four ways of matching a listed buffer:
2066 * attempt == 0: without '^' or '$' (at any position)
2067 * attempt == 1: with '^' at start (only at position 0)
2068 * attempt == 2: with '$' at end (only match at end)
2069 * attempt == 3: with '^' at start and '$' at end (only full match)
2070 * Repeat this for finding an unlisted buffer if there was no matching
2071 * listed buffer.
2073 else
2075 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2076 if (pat == NULL)
2077 return -1;
2078 patend = pat + STRLEN(pat) - 1;
2079 toggledollar = (patend > pat && *patend == '$');
2081 /* First try finding a listed buffer. If not found and "unlisted"
2082 * is TRUE, try finding an unlisted buffer. */
2083 find_listed = TRUE;
2084 for (;;)
2086 for (attempt = 0; attempt <= 3; ++attempt)
2088 /* may add '^' and '$' */
2089 if (toggledollar)
2090 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2091 p = pat;
2092 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2093 ++p;
2094 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2095 if (prog == NULL)
2097 vim_free(pat);
2098 return -1;
2101 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2102 if (buf->b_p_bl == find_listed
2103 #ifdef FEAT_DIFF
2104 && (!diffmode || diff_mode_buf(buf))
2105 #endif
2106 && buflist_match(prog, buf) != NULL)
2108 if (match >= 0) /* already found a match */
2110 match = -2;
2111 break;
2113 match = buf->b_fnum; /* remember first match */
2116 vim_free(prog);
2117 if (match >= 0) /* found one match */
2118 break;
2121 /* Only search for unlisted buffers if there was no match with
2122 * a listed buffer. */
2123 if (!unlisted || !find_listed || match != -1)
2124 break;
2125 find_listed = FALSE;
2128 vim_free(pat);
2131 if (match == -2)
2132 EMSG2(_("E93: More than one match for %s"), pattern);
2133 else if (match < 0)
2134 EMSG2(_("E94: No matching buffer for %s"), pattern);
2135 return match;
2137 #endif
2139 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2142 * Find all buffer names that match.
2143 * For command line expansion of ":buf" and ":sbuf".
2144 * Return OK if matches found, FAIL otherwise.
2147 ExpandBufnames(pat, num_file, file, options)
2148 char_u *pat;
2149 int *num_file;
2150 char_u ***file;
2151 int options;
2153 int count = 0;
2154 buf_T *buf;
2155 int round;
2156 char_u *p;
2157 int attempt;
2158 regprog_T *prog;
2159 char_u *patc;
2161 *num_file = 0; /* return values in case of FAIL */
2162 *file = NULL;
2164 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2165 if (*pat == '^')
2167 patc = alloc((unsigned)STRLEN(pat) + 11);
2168 if (patc == NULL)
2169 return FAIL;
2170 STRCPY(patc, "\\(^\\|[\\/]\\)");
2171 STRCPY(patc + 11, pat + 1);
2173 else
2174 patc = pat;
2177 * attempt == 0: try match with '\<', match at start of word
2178 * attempt == 1: try match without '\<', match anywhere
2180 for (attempt = 0; attempt <= 1; ++attempt)
2182 if (attempt > 0 && patc == pat)
2183 break; /* there was no anchor, no need to try again */
2184 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2185 if (prog == NULL)
2187 if (patc != pat)
2188 vim_free(patc);
2189 return FAIL;
2193 * round == 1: Count the matches.
2194 * round == 2: Build the array to keep the matches.
2196 for (round = 1; round <= 2; ++round)
2198 count = 0;
2199 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2201 if (!buf->b_p_bl) /* skip unlisted buffers */
2202 continue;
2203 p = buflist_match(prog, buf);
2204 if (p != NULL)
2206 if (round == 1)
2207 ++count;
2208 else
2210 if (options & WILD_HOME_REPLACE)
2211 p = home_replace_save(buf, p);
2212 else
2213 p = vim_strsave(p);
2214 (*file)[count++] = p;
2218 if (count == 0) /* no match found, break here */
2219 break;
2220 if (round == 1)
2222 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2223 if (*file == NULL)
2225 vim_free(prog);
2226 if (patc != pat)
2227 vim_free(patc);
2228 return FAIL;
2232 vim_free(prog);
2233 if (count) /* match(es) found, break here */
2234 break;
2237 if (patc != pat)
2238 vim_free(patc);
2240 *num_file = count;
2241 return (count == 0 ? FAIL : OK);
2244 #endif /* FEAT_CMDL_COMPL */
2246 #ifdef HAVE_BUFLIST_MATCH
2248 * Check for a match on the file name for buffer "buf" with regprog "prog".
2250 static char_u *
2251 buflist_match(prog, buf)
2252 regprog_T *prog;
2253 buf_T *buf;
2255 char_u *match;
2257 /* First try the short file name, then the long file name. */
2258 match = fname_match(prog, buf->b_sfname);
2259 if (match == NULL)
2260 match = fname_match(prog, buf->b_ffname);
2262 return match;
2266 * Try matching the regexp in "prog" with file name "name".
2267 * Return "name" when there is a match, NULL when not.
2269 static char_u *
2270 fname_match(prog, name)
2271 regprog_T *prog;
2272 char_u *name;
2274 char_u *match = NULL;
2275 char_u *p;
2276 regmatch_T regmatch;
2278 if (name != NULL)
2280 regmatch.regprog = prog;
2281 #ifdef CASE_INSENSITIVE_FILENAME
2282 regmatch.rm_ic = TRUE; /* Always ignore case */
2283 #else
2284 regmatch.rm_ic = FALSE; /* Never ignore case */
2285 #endif
2287 if (vim_regexec(&regmatch, name, (colnr_T)0))
2288 match = name;
2289 else
2291 /* Replace $(HOME) with '~' and try matching again. */
2292 p = home_replace_save(NULL, name);
2293 if (p != NULL && vim_regexec(&regmatch, p, (colnr_T)0))
2294 match = name;
2295 vim_free(p);
2299 return match;
2301 #endif
2304 * find file in buffer list by number
2306 buf_T *
2307 buflist_findnr(nr)
2308 int nr;
2310 buf_T *buf;
2312 if (nr == 0)
2313 nr = curwin->w_alt_fnum;
2314 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2315 if (buf->b_fnum == nr)
2316 return (buf);
2317 return NULL;
2321 * Get name of file 'n' in the buffer list.
2322 * When the file has no name an empty string is returned.
2323 * home_replace() is used to shorten the file name (used for marks).
2324 * Returns a pointer to allocated memory, of NULL when failed.
2326 char_u *
2327 buflist_nr2name(n, fullname, helptail)
2328 int n;
2329 int fullname;
2330 int helptail; /* for help buffers return tail only */
2332 buf_T *buf;
2334 buf = buflist_findnr(n);
2335 if (buf == NULL)
2336 return NULL;
2337 return home_replace_save(helptail ? buf : NULL,
2338 fullname ? buf->b_ffname : buf->b_fname);
2342 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2343 * When "copy_options" is TRUE save the local window option values.
2344 * When "lnum" is 0 only do the options.
2346 static void
2347 buflist_setfpos(buf, win, lnum, col, copy_options)
2348 buf_T *buf;
2349 win_T *win;
2350 linenr_T lnum;
2351 colnr_T col;
2352 int copy_options;
2354 wininfo_T *wip;
2356 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2357 if (wip->wi_win == win)
2358 break;
2359 if (wip == NULL)
2361 /* allocate a new entry */
2362 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2363 if (wip == NULL)
2364 return;
2365 wip->wi_win = win;
2366 if (lnum == 0) /* set lnum even when it's 0 */
2367 lnum = 1;
2369 else
2371 /* remove the entry from the list */
2372 if (wip->wi_prev)
2373 wip->wi_prev->wi_next = wip->wi_next;
2374 else
2375 buf->b_wininfo = wip->wi_next;
2376 if (wip->wi_next)
2377 wip->wi_next->wi_prev = wip->wi_prev;
2378 if (copy_options && wip->wi_optset)
2380 clear_winopt(&wip->wi_opt);
2381 #ifdef FEAT_FOLDING
2382 deleteFoldRecurse(&wip->wi_folds);
2383 #endif
2386 if (lnum != 0)
2388 wip->wi_fpos.lnum = lnum;
2389 wip->wi_fpos.col = col;
2391 if (copy_options)
2393 /* Save the window-specific option values. */
2394 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2395 #ifdef FEAT_FOLDING
2396 wip->wi_fold_manual = win->w_fold_manual;
2397 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2398 #endif
2399 wip->wi_optset = TRUE;
2402 /* insert the entry in front of the list */
2403 wip->wi_next = buf->b_wininfo;
2404 buf->b_wininfo = wip;
2405 wip->wi_prev = NULL;
2406 if (wip->wi_next)
2407 wip->wi_next->wi_prev = wip;
2409 return;
2412 #ifdef FEAT_DIFF
2413 static int wininfo_other_tab_diff __ARGS((wininfo_T *wip));
2416 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2417 * page. That's because a diff is local to a tab page.
2419 static int
2420 wininfo_other_tab_diff(wip)
2421 wininfo_T *wip;
2423 win_T *wp;
2425 if (wip->wi_opt.wo_diff)
2427 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2428 /* return FALSE when it's a window in the current tab page, thus
2429 * the buffer was in diff mode here */
2430 if (wip->wi_win == wp)
2431 return FALSE;
2432 return TRUE;
2434 return FALSE;
2436 #endif
2439 * Find info for the current window in buffer "buf".
2440 * If not found, return the info for the most recently used window.
2441 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2442 * another tab page.
2443 * Returns NULL when there isn't any info.
2445 static wininfo_T *
2446 find_wininfo(buf, skip_diff_buffer)
2447 buf_T *buf;
2448 int skip_diff_buffer UNUSED;
2450 wininfo_T *wip;
2452 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2453 if (wip->wi_win == curwin
2454 #ifdef FEAT_DIFF
2455 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2456 #endif
2458 break;
2460 /* If no wininfo for curwin, use the first in the list (that doesn't have
2461 * 'diff' set and is in another tab page). */
2462 if (wip == NULL)
2464 #ifdef FEAT_DIFF
2465 if (skip_diff_buffer)
2467 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2468 if (!wininfo_other_tab_diff(wip))
2469 break;
2471 else
2472 #endif
2473 wip = buf->b_wininfo;
2475 return wip;
2479 * Reset the local window options to the values last used in this window.
2480 * If the buffer wasn't used in this window before, use the values from
2481 * the most recently used window. If the values were never set, use the
2482 * global values for the window.
2484 void
2485 get_winopts(buf)
2486 buf_T *buf;
2488 wininfo_T *wip;
2490 clear_winopt(&curwin->w_onebuf_opt);
2491 #ifdef FEAT_FOLDING
2492 clearFolding(curwin);
2493 #endif
2495 wip = find_wininfo(buf, TRUE);
2496 if (wip != NULL && wip->wi_optset)
2498 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2499 #ifdef FEAT_FOLDING
2500 curwin->w_fold_manual = wip->wi_fold_manual;
2501 curwin->w_foldinvalid = TRUE;
2502 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2503 #endif
2505 else
2506 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2508 #ifdef FEAT_FOLDING
2509 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2510 if (p_fdls >= 0)
2511 curwin->w_p_fdl = p_fdls;
2512 #endif
2516 * Find the position (lnum and col) for the buffer 'buf' for the current
2517 * window.
2518 * Returns a pointer to no_position if no position is found.
2520 pos_T *
2521 buflist_findfpos(buf)
2522 buf_T *buf;
2524 wininfo_T *wip;
2525 static pos_T no_position = INIT_POS_T(1, 0, 0);
2527 wip = find_wininfo(buf, FALSE);
2528 if (wip != NULL)
2529 return &(wip->wi_fpos);
2530 else
2531 return &no_position;
2535 * Find the lnum for the buffer 'buf' for the current window.
2537 linenr_T
2538 buflist_findlnum(buf)
2539 buf_T *buf;
2541 return buflist_findfpos(buf)->lnum;
2544 #if defined(FEAT_LISTCMDS) || defined(PROTO)
2546 * List all know file names (for :files and :buffers command).
2548 void
2549 buflist_list(eap)
2550 exarg_T *eap;
2552 buf_T *buf;
2553 int len;
2554 int i;
2556 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2558 /* skip unlisted buffers, unless ! was used */
2559 if (!buf->b_p_bl && !eap->forceit)
2560 continue;
2561 msg_putchar('\n');
2562 if (buf_spname(buf) != NULL)
2563 STRCPY(NameBuff, buf_spname(buf));
2564 else
2565 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2567 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
2568 buf->b_fnum,
2569 buf->b_p_bl ? ' ' : 'u',
2570 buf == curbuf ? '%' :
2571 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2572 buf->b_ml.ml_mfp == NULL ? ' ' :
2573 (buf->b_nwindows == 0 ? 'h' : 'a'),
2574 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2575 (buf->b_flags & BF_READERR) ? 'x'
2576 : (bufIsChanged(buf) ? '+' : ' '),
2577 NameBuff);
2579 /* put "line 999" in column 40 or after the file name */
2580 i = 40 - vim_strsize(IObuff);
2583 IObuff[len++] = ' ';
2584 } while (--i > 0 && len < IOSIZE - 18);
2585 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2586 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
2587 : (long)buflist_findlnum(buf));
2588 msg_outtrans(IObuff);
2589 out_flush(); /* output one line at a time */
2590 ui_breakcheck();
2593 #endif
2596 * Get file name and line number for file 'fnum'.
2597 * Used by DoOneCmd() for translating '%' and '#'.
2598 * Used by insert_reg() and cmdline_paste() for '#' register.
2599 * Return FAIL if not found, OK for success.
2602 buflist_name_nr(fnum, fname, lnum)
2603 int fnum;
2604 char_u **fname;
2605 linenr_T *lnum;
2607 buf_T *buf;
2609 buf = buflist_findnr(fnum);
2610 if (buf == NULL || buf->b_fname == NULL)
2611 return FAIL;
2613 *fname = buf->b_fname;
2614 *lnum = buflist_findlnum(buf);
2616 return OK;
2620 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2621 * The file name with the full path is also remembered, for when :cd is used.
2622 * Returns FAIL for failure (file name already in use by other buffer)
2623 * OK otherwise.
2626 setfname(buf, ffname, sfname, message)
2627 buf_T *buf;
2628 char_u *ffname, *sfname;
2629 int message; /* give message when buffer already exists */
2631 buf_T *obuf = NULL;
2632 #ifdef UNIX
2633 struct stat st;
2634 #endif
2636 if (ffname == NULL || *ffname == NUL)
2638 /* Removing the name. */
2639 vim_free(buf->b_ffname);
2640 vim_free(buf->b_sfname);
2641 buf->b_ffname = NULL;
2642 buf->b_sfname = NULL;
2643 #ifdef UNIX
2644 st.st_dev = (dev_T)-1;
2645 #endif
2647 else
2649 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2650 if (ffname == NULL) /* out of memory */
2651 return FAIL;
2654 * if the file name is already used in another buffer:
2655 * - if the buffer is loaded, fail
2656 * - if the buffer is not loaded, delete it from the list
2658 #ifdef UNIX
2659 if (mch_stat((char *)ffname, &st) < 0)
2660 st.st_dev = (dev_T)-1;
2661 #endif
2662 if (!(buf->b_flags & BF_DUMMY))
2663 #ifdef UNIX
2664 obuf = buflist_findname_stat(ffname, &st);
2665 #else
2666 obuf = buflist_findname(ffname);
2667 #endif
2668 if (obuf != NULL && obuf != buf)
2670 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2672 if (message)
2673 EMSG(_("E95: Buffer with this name already exists"));
2674 vim_free(ffname);
2675 return FAIL;
2677 close_buffer(NULL, obuf, DOBUF_WIPE); /* delete from the list */
2679 sfname = vim_strsave(sfname);
2680 if (ffname == NULL || sfname == NULL)
2682 vim_free(sfname);
2683 vim_free(ffname);
2684 return FAIL;
2686 #ifdef USE_FNAME_CASE
2687 # ifdef USE_LONG_FNAME
2688 if (USE_LONG_FNAME)
2689 # endif
2690 fname_case(sfname, 0); /* set correct case for short file name */
2691 #endif
2692 vim_free(buf->b_ffname);
2693 vim_free(buf->b_sfname);
2694 buf->b_ffname = ffname;
2695 buf->b_sfname = sfname;
2697 buf->b_fname = buf->b_sfname;
2698 #ifdef UNIX
2699 if (st.st_dev == (dev_T)-1)
2700 buf->b_dev_valid = FALSE;
2701 else
2703 buf->b_dev_valid = TRUE;
2704 buf->b_dev = st.st_dev;
2705 buf->b_ino = st.st_ino;
2707 #endif
2709 #ifndef SHORT_FNAME
2710 buf->b_shortname = FALSE;
2711 #endif
2713 buf_name_changed(buf);
2714 return OK;
2718 * Crude way of changing the name of a buffer. Use with care!
2719 * The name should be relative to the current directory.
2721 void
2722 buf_set_name(fnum, name)
2723 int fnum;
2724 char_u *name;
2726 buf_T *buf;
2728 buf = buflist_findnr(fnum);
2729 if (buf != NULL)
2731 vim_free(buf->b_sfname);
2732 vim_free(buf->b_ffname);
2733 buf->b_ffname = vim_strsave(name);
2734 buf->b_sfname = NULL;
2735 /* Allocate ffname and expand into full path. Also resolves .lnk
2736 * files on Win32. */
2737 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
2738 buf->b_fname = buf->b_sfname;
2743 * Take care of what needs to be done when the name of buffer "buf" has
2744 * changed.
2746 void
2747 buf_name_changed(buf)
2748 buf_T *buf;
2751 * If the file name changed, also change the name of the swapfile
2753 if (buf->b_ml.ml_mfp != NULL)
2754 ml_setname(buf);
2756 if (curwin->w_buffer == buf)
2757 check_arg_idx(curwin); /* check file name for arg list */
2758 #ifdef FEAT_TITLE
2759 maketitle(); /* set window title */
2760 #endif
2761 #ifdef FEAT_WINDOWS
2762 status_redraw_all(); /* status lines need to be redrawn */
2763 #endif
2764 fmarks_check_names(buf); /* check named file marks */
2765 ml_timestamp(buf); /* reset timestamp */
2769 * set alternate file name for current window
2771 * Used by do_one_cmd(), do_write() and do_ecmd().
2772 * Return the buffer.
2774 buf_T *
2775 setaltfname(ffname, sfname, lnum)
2776 char_u *ffname;
2777 char_u *sfname;
2778 linenr_T lnum;
2780 buf_T *buf;
2782 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2783 buf = buflist_new(ffname, sfname, lnum, 0);
2784 if (buf != NULL && !cmdmod.keepalt)
2785 curwin->w_alt_fnum = buf->b_fnum;
2786 return buf;
2790 * Get alternate file name for current window.
2791 * Return NULL if there isn't any, and give error message if requested.
2793 char_u *
2794 getaltfname(errmsg)
2795 int errmsg; /* give error message */
2797 char_u *fname;
2798 linenr_T dummy;
2800 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2802 if (errmsg)
2803 EMSG(_(e_noalt));
2804 return NULL;
2806 return fname;
2810 * Add a file name to the buflist and return its number.
2811 * Uses same flags as buflist_new(), except BLN_DUMMY.
2813 * used by qf_init(), main() and doarglist()
2816 buflist_add(fname, flags)
2817 char_u *fname;
2818 int flags;
2820 buf_T *buf;
2822 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
2823 if (buf != NULL)
2824 return buf->b_fnum;
2825 return 0;
2828 #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2830 * Adjust slashes in file names. Called after 'shellslash' was set.
2832 void
2833 buflist_slash_adjust()
2835 buf_T *bp;
2837 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
2839 if (bp->b_ffname != NULL)
2840 slash_adjust(bp->b_ffname);
2841 if (bp->b_sfname != NULL)
2842 slash_adjust(bp->b_sfname);
2845 #endif
2848 * Set alternate cursor position for the current buffer and window "win".
2849 * Also save the local window option values.
2851 void
2852 buflist_altfpos(win)
2853 win_T *win;
2855 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
2859 * Return TRUE if 'ffname' is not the same file as current file.
2860 * Fname must have a full path (expanded by mch_FullName()).
2863 otherfile(ffname)
2864 char_u *ffname;
2866 return otherfile_buf(curbuf, ffname
2867 #ifdef UNIX
2868 , NULL
2869 #endif
2873 static int
2874 otherfile_buf(buf, ffname
2875 #ifdef UNIX
2876 , stp
2877 #endif
2879 buf_T *buf;
2880 char_u *ffname;
2881 #ifdef UNIX
2882 struct stat *stp;
2883 #endif
2885 /* no name is different */
2886 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
2887 return TRUE;
2888 if (fnamecmp(ffname, buf->b_ffname) == 0)
2889 return FALSE;
2890 #ifdef UNIX
2892 struct stat st;
2894 /* If no struct stat given, get it now */
2895 if (stp == NULL)
2897 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
2898 st.st_dev = (dev_T)-1;
2899 stp = &st;
2901 /* Use dev/ino to check if the files are the same, even when the names
2902 * are different (possible with links). Still need to compare the
2903 * name above, for when the file doesn't exist yet.
2904 * Problem: The dev/ino changes when a file is deleted (and created
2905 * again) and remains the same when renamed/moved. We don't want to
2906 * mch_stat() each buffer each time, that would be too slow. Get the
2907 * dev/ino again when they appear to match, but not when they appear
2908 * to be different: Could skip a buffer when it's actually the same
2909 * file. */
2910 if (buf_same_ino(buf, stp))
2912 buf_setino(buf);
2913 if (buf_same_ino(buf, stp))
2914 return FALSE;
2917 #endif
2918 return TRUE;
2921 #if defined(UNIX) || defined(PROTO)
2923 * Set inode and device number for a buffer.
2924 * Must always be called when b_fname is changed!.
2926 void
2927 buf_setino(buf)
2928 buf_T *buf;
2930 struct stat st;
2932 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
2934 buf->b_dev_valid = TRUE;
2935 buf->b_dev = st.st_dev;
2936 buf->b_ino = st.st_ino;
2938 else
2939 buf->b_dev_valid = FALSE;
2943 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
2945 static int
2946 buf_same_ino(buf, stp)
2947 buf_T *buf;
2948 struct stat *stp;
2950 return (buf->b_dev_valid
2951 && stp->st_dev == buf->b_dev
2952 && stp->st_ino == buf->b_ino);
2954 #endif
2957 * Print info about the current buffer.
2959 void
2960 fileinfo(fullname, shorthelp, dont_truncate)
2961 int fullname; /* when non-zero print full path */
2962 int shorthelp;
2963 int dont_truncate;
2965 char_u *name;
2966 int n;
2967 char_u *p;
2968 char_u *buffer;
2969 size_t len;
2971 buffer = alloc(IOSIZE);
2972 if (buffer == NULL)
2973 return;
2975 if (fullname > 1) /* 2 CTRL-G: include buffer number */
2977 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
2978 p = buffer + STRLEN(buffer);
2980 else
2981 p = buffer;
2983 *p++ = '"';
2984 if (buf_spname(curbuf) != NULL)
2985 STRCPY(p, buf_spname(curbuf));
2986 else
2988 if (!fullname && curbuf->b_fname != NULL)
2989 name = curbuf->b_fname;
2990 else
2991 name = curbuf->b_ffname;
2992 home_replace(shorthelp ? curbuf : NULL, name, p,
2993 (int)(IOSIZE - (p - buffer)), TRUE);
2996 len = STRLEN(buffer);
2997 vim_snprintf((char *)buffer + len, IOSIZE - len,
2998 "\"%s%s%s%s%s%s",
2999 curbufIsChanged() ? (shortmess(SHM_MOD)
3000 ? " [+]" : _(" [Modified]")) : " ",
3001 (curbuf->b_flags & BF_NOTEDITED)
3002 #ifdef FEAT_QUICKFIX
3003 && !bt_dontwrite(curbuf)
3004 #endif
3005 ? _("[Not edited]") : "",
3006 (curbuf->b_flags & BF_NEW)
3007 #ifdef FEAT_QUICKFIX
3008 && !bt_dontwrite(curbuf)
3009 #endif
3010 ? _("[New file]") : "",
3011 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
3012 curbuf->b_p_ro ? (shortmess(SHM_RO) ? "[RO]"
3013 : _("[readonly]")) : "",
3014 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3015 || curbuf->b_p_ro) ?
3016 " " : "");
3017 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3018 * causes an overflow, thus for large numbers divide instead. */
3019 if (curwin->w_cursor.lnum > 1000000L)
3020 n = (int)(((long)curwin->w_cursor.lnum) /
3021 ((long)curbuf->b_ml.ml_line_count / 100L));
3022 else
3023 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3024 (long)curbuf->b_ml.ml_line_count);
3025 len = STRLEN(buffer);
3026 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3028 vim_snprintf((char *)buffer + len, IOSIZE - len, "%s", _(no_lines_msg));
3030 #ifdef FEAT_CMDL_INFO
3031 else if (p_ru)
3033 /* Current line and column are already on the screen -- webb */
3034 if (curbuf->b_ml.ml_line_count == 1)
3035 vim_snprintf((char *)buffer + len, IOSIZE - len,
3036 _("1 line --%d%%--"), n);
3037 else
3038 vim_snprintf((char *)buffer + len, IOSIZE - len,
3039 _("%ld lines --%d%%--"),
3040 (long)curbuf->b_ml.ml_line_count, n);
3042 #endif
3043 else
3045 vim_snprintf((char *)buffer + len, IOSIZE - len,
3046 _("line %ld of %ld --%d%%-- col "),
3047 (long)curwin->w_cursor.lnum,
3048 (long)curbuf->b_ml.ml_line_count,
3050 validate_virtcol();
3051 len = STRLEN(buffer);
3052 col_print(buffer + len, IOSIZE - len,
3053 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3056 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
3058 if (dont_truncate)
3060 /* Temporarily set msg_scroll to avoid the message being truncated.
3061 * First call msg_start() to get the message in the right place. */
3062 msg_start();
3063 n = msg_scroll;
3064 msg_scroll = TRUE;
3065 msg(buffer);
3066 msg_scroll = n;
3068 else
3070 p = msg_trunc_attr(buffer, FALSE, 0);
3071 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
3072 /* Need to repeat the message after redrawing when:
3073 * - When restart_edit is set (otherwise there will be a delay
3074 * before redrawing).
3075 * - When the screen was scrolled but there is no wait-return
3076 * prompt. */
3077 set_keep_msg(p, 0);
3080 vim_free(buffer);
3083 void
3084 col_print(buf, buflen, col, vcol)
3085 char_u *buf;
3086 size_t buflen;
3087 int col;
3088 int vcol;
3090 if (col == vcol)
3091 vim_snprintf((char *)buf, buflen, "%d", col);
3092 else
3093 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
3096 #if defined(FEAT_TITLE) || defined(PROTO)
3098 * put file name in title bar of window and in icon title
3101 static char_u *lasttitle = NULL;
3102 static char_u *lasticon = NULL;
3104 void
3105 maketitle()
3107 char_u *p;
3108 char_u *t_str = NULL;
3109 char_u *i_name;
3110 char_u *i_str = NULL;
3111 int maxlen = 0;
3112 int len;
3113 int mustset;
3114 char_u buf[IOSIZE];
3115 int off;
3117 if (!redrawing())
3119 /* Postpone updating the title when 'lazyredraw' is set. */
3120 need_maketitle = TRUE;
3121 return;
3124 #ifdef FEAT_GUI_MACVIM
3125 gui_macvim_update_modified_flag();
3126 #endif
3128 need_maketitle = FALSE;
3129 if (!p_title && !p_icon)
3130 return;
3132 if (p_title)
3134 if (p_titlelen > 0)
3136 maxlen = p_titlelen * Columns / 100;
3137 if (maxlen < 10)
3138 maxlen = 10;
3141 t_str = buf;
3142 if (*p_titlestring != NUL)
3144 #ifdef FEAT_STL_OPT
3145 if (stl_syntax & STL_IN_TITLE)
3147 int use_sandbox = FALSE;
3148 int save_called_emsg = called_emsg;
3150 # ifdef FEAT_EVAL
3151 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
3152 # endif
3153 called_emsg = FALSE;
3154 build_stl_str_hl(curwin, t_str, sizeof(buf),
3155 p_titlestring, use_sandbox,
3156 0, maxlen, NULL, NULL);
3157 if (called_emsg)
3158 set_string_option_direct((char_u *)"titlestring", -1,
3159 (char_u *)"", OPT_FREE, SID_ERROR);
3160 called_emsg |= save_called_emsg;
3162 else
3163 #endif
3164 t_str = p_titlestring;
3166 else
3168 /* format: "fname + (path) (1 of 2) - VIM" */
3170 if (curbuf->b_fname == NULL)
3171 STRCPY(buf, _("[No Name]"));
3172 else
3174 p = transstr(gettail(curbuf->b_fname));
3175 vim_strncpy(buf, p, IOSIZE - 100);
3176 vim_free(p);
3179 switch (bufIsChanged(curbuf)
3180 + (curbuf->b_p_ro * 2)
3181 + (!curbuf->b_p_ma * 4))
3183 case 1: STRCAT(buf, " +"); break;
3184 case 2: STRCAT(buf, " ="); break;
3185 case 3: STRCAT(buf, " =+"); break;
3186 case 4:
3187 case 6: STRCAT(buf, " -"); break;
3188 case 5:
3189 case 7: STRCAT(buf, " -+"); break;
3192 if (curbuf->b_fname != NULL)
3194 /* Get path of file, replace home dir with ~ */
3195 off = (int)STRLEN(buf);
3196 buf[off++] = ' ';
3197 buf[off++] = '(';
3198 home_replace(curbuf, curbuf->b_ffname,
3199 buf + off, IOSIZE - off, TRUE);
3200 #ifdef BACKSLASH_IN_FILENAME
3201 /* avoid "c:/name" to be reduced to "c" */
3202 if (isalpha(buf[off]) && buf[off + 1] == ':')
3203 off += 2;
3204 #endif
3205 /* remove the file name */
3206 p = gettail_sep(buf + off);
3207 if (p == buf + off)
3208 /* must be a help buffer */
3209 vim_strncpy(buf + off, (char_u *)_("help"),
3210 (size_t)(IOSIZE - off - 1));
3211 else
3212 *p = NUL;
3214 /* translate unprintable chars */
3215 p = transstr(buf + off);
3216 vim_strncpy(buf + off, p, (size_t)(IOSIZE - off - 1));
3217 vim_free(p);
3218 STRCAT(buf, ")");
3221 #ifndef FEAT_GUI_MACVIM
3222 append_arg_number(curwin, buf, FALSE, IOSIZE);
3223 #endif
3225 #if defined(FEAT_CLIENTSERVER)
3226 if (serverName != NULL)
3228 STRCAT(buf, " - ");
3229 STRCAT(buf, serverName);
3231 else
3232 #endif
3233 STRCAT(buf, " - VIM");
3235 if (maxlen > 0)
3237 /* make it shorter by removing a bit in the middle */
3238 len = vim_strsize(buf);
3239 if (len > maxlen)
3240 trunc_string(buf, buf, maxlen);
3244 mustset = ti_change(t_str, &lasttitle);
3246 if (p_icon)
3248 i_str = buf;
3249 if (*p_iconstring != NUL)
3251 #ifdef FEAT_STL_OPT
3252 if (stl_syntax & STL_IN_ICON)
3254 int use_sandbox = FALSE;
3255 int save_called_emsg = called_emsg;
3257 # ifdef FEAT_EVAL
3258 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
3259 # endif
3260 called_emsg = FALSE;
3261 build_stl_str_hl(curwin, i_str, sizeof(buf),
3262 p_iconstring, use_sandbox,
3263 0, 0, NULL, NULL);
3264 if (called_emsg)
3265 set_string_option_direct((char_u *)"iconstring", -1,
3266 (char_u *)"", OPT_FREE, SID_ERROR);
3267 called_emsg |= save_called_emsg;
3269 else
3270 #endif
3271 i_str = p_iconstring;
3273 else
3275 if (buf_spname(curbuf) != NULL)
3276 i_name = (char_u *)buf_spname(curbuf);
3277 else /* use file name only in icon */
3278 i_name = gettail(curbuf->b_ffname);
3279 *i_str = NUL;
3280 /* Truncate name at 100 bytes. */
3281 len = (int)STRLEN(i_name);
3282 if (len > 100)
3284 len -= 100;
3285 #ifdef FEAT_MBYTE
3286 if (has_mbyte)
3287 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3288 #endif
3289 i_name += len;
3291 STRCPY(i_str, i_name);
3292 trans_characters(i_str, IOSIZE);
3296 mustset |= ti_change(i_str, &lasticon);
3298 if (mustset)
3299 resettitle();
3303 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3304 * from "str" if it does.
3305 * Return TRUE when "*last" changed.
3307 static int
3308 ti_change(str, last)
3309 char_u *str;
3310 char_u **last;
3312 if ((str == NULL) != (*last == NULL)
3313 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3315 vim_free(*last);
3316 if (str == NULL)
3317 *last = NULL;
3318 else
3319 *last = vim_strsave(str);
3320 return TRUE;
3322 return FALSE;
3326 * Put current window title back (used after calling a shell)
3328 void
3329 resettitle()
3331 mch_settitle(lasttitle, lasticon);
3334 # if defined(EXITFREE) || defined(PROTO)
3335 void
3336 free_titles()
3338 vim_free(lasttitle);
3339 vim_free(lasticon);
3341 # endif
3343 #endif /* FEAT_TITLE */
3345 #if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
3347 * Build a string from the status line items in "fmt".
3348 * Return length of string in screen cells.
3350 * Normally works for window "wp", except when working for 'tabline' then it
3351 * is "curwin".
3353 * Items are drawn interspersed with the text that surrounds it
3354 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3355 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3357 * If maxwidth is not zero, the string will be filled at any middle marker
3358 * or truncated if too long, fillchar is used for all whitespace.
3361 build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar, maxwidth, hltab, tabtab)
3362 win_T *wp;
3363 char_u *out; /* buffer to write into != NameBuff */
3364 size_t outlen; /* length of out[] */
3365 char_u *fmt;
3366 int use_sandbox UNUSED; /* "fmt" was set insecurely, use sandbox */
3367 int fillchar;
3368 int maxwidth;
3369 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3370 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
3372 char_u *p;
3373 char_u *s;
3374 char_u *t;
3375 char_u *linecont;
3376 #ifdef FEAT_EVAL
3377 win_T *o_curwin;
3378 buf_T *o_curbuf;
3379 #endif
3380 int empty_line;
3381 colnr_T virtcol;
3382 long l;
3383 long n;
3384 int prevchar_isflag;
3385 int prevchar_isitem;
3386 int itemisflag;
3387 int fillable;
3388 char_u *str;
3389 long num;
3390 int width;
3391 int itemcnt;
3392 int curitem;
3393 int groupitem[STL_MAX_ITEM];
3394 int groupdepth;
3395 struct stl_item
3397 char_u *start;
3398 int minwid;
3399 int maxwid;
3400 enum
3402 Normal,
3403 Empty,
3404 Group,
3405 Middle,
3406 Highlight,
3407 TabPage,
3408 Trunc
3409 } type;
3410 } item[STL_MAX_ITEM];
3411 int minwid;
3412 int maxwid;
3413 int zeropad;
3414 char_u base;
3415 char_u opt;
3416 #define TMPLEN 70
3417 char_u tmp[TMPLEN];
3418 char_u *usefmt = fmt;
3419 struct stl_hlrec *sp;
3421 #ifdef FEAT_EVAL
3423 * When the format starts with "%!" then evaluate it as an expression and
3424 * use the result as the actual format string.
3426 if (fmt[0] == '%' && fmt[1] == '!')
3428 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3429 if (usefmt == NULL)
3430 usefmt = fmt;
3432 #endif
3434 if (fillchar == 0)
3435 fillchar = ' ';
3436 #ifdef FEAT_MBYTE
3437 /* Can't handle a multi-byte fill character yet. */
3438 else if (mb_char2len(fillchar) > 1)
3439 fillchar = '-';
3440 #endif
3443 * Get line & check if empty (cursorpos will show "0-1").
3444 * If inversion is possible we use it. Else '=' characters are used.
3446 linecont = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3447 empty_line = (*linecont == NUL);
3449 groupdepth = 0;
3450 p = out;
3451 curitem = 0;
3452 prevchar_isflag = TRUE;
3453 prevchar_isitem = FALSE;
3454 for (s = usefmt; *s; )
3456 if (*s != NUL && *s != '%')
3457 prevchar_isflag = prevchar_isitem = FALSE;
3460 * Handle up to the next '%' or the end.
3462 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3463 *p++ = *s++;
3464 if (*s == NUL || p + 1 >= out + outlen)
3465 break;
3468 * Handle one '%' item.
3470 s++;
3471 if (*s == '%')
3473 if (p + 1 >= out + outlen)
3474 break;
3475 *p++ = *s++;
3476 prevchar_isflag = prevchar_isitem = FALSE;
3477 continue;
3479 if (*s == STL_MIDDLEMARK)
3481 s++;
3482 if (groupdepth > 0)
3483 continue;
3484 item[curitem].type = Middle;
3485 item[curitem++].start = p;
3486 continue;
3488 if (*s == STL_TRUNCMARK)
3490 s++;
3491 item[curitem].type = Trunc;
3492 item[curitem++].start = p;
3493 continue;
3495 if (*s == ')')
3497 s++;
3498 if (groupdepth < 1)
3499 continue;
3500 groupdepth--;
3502 t = item[groupitem[groupdepth]].start;
3503 *p = NUL;
3504 l = vim_strsize(t);
3505 if (curitem > groupitem[groupdepth] + 1
3506 && item[groupitem[groupdepth]].minwid == 0)
3508 /* remove group if all items are empty */
3509 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3510 if (item[n].type == Normal)
3511 break;
3512 if (n == curitem)
3514 p = t;
3515 l = 0;
3518 if (l > item[groupitem[groupdepth]].maxwid)
3520 /* truncate, remove n bytes of text at the start */
3521 #ifdef FEAT_MBYTE
3522 if (has_mbyte)
3524 /* Find the first character that should be included. */
3525 n = 0;
3526 while (l >= item[groupitem[groupdepth]].maxwid)
3528 l -= ptr2cells(t + n);
3529 n += (*mb_ptr2len)(t + n);
3532 else
3533 #endif
3534 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
3536 *t = '<';
3537 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
3538 p = p - n + 1;
3539 #ifdef FEAT_MBYTE
3540 /* Fill up space left over by half a double-wide char. */
3541 while (++l < item[groupitem[groupdepth]].minwid)
3542 *p++ = fillchar;
3543 #endif
3545 /* correct the start of the items for the truncation */
3546 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3548 item[l].start -= n;
3549 if (item[l].start < t)
3550 item[l].start = t;
3553 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3555 /* fill */
3556 n = item[groupitem[groupdepth]].minwid;
3557 if (n < 0)
3559 /* fill by appending characters */
3560 n = 0 - n;
3561 while (l++ < n && p + 1 < out + outlen)
3562 *p++ = fillchar;
3564 else
3566 /* fill by inserting characters */
3567 mch_memmove(t + n - l, t, (size_t)(p - t));
3568 l = n - l;
3569 if (p + l >= out + outlen)
3570 l = (long)((out + outlen) - p - 1);
3571 p += l;
3572 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3573 item[n].start += l;
3574 for ( ; l > 0; l--)
3575 *t++ = fillchar;
3578 continue;
3580 minwid = 0;
3581 maxwid = 9999;
3582 zeropad = FALSE;
3583 l = 1;
3584 if (*s == '0')
3586 s++;
3587 zeropad = TRUE;
3589 if (*s == '-')
3591 s++;
3592 l = -1;
3594 if (VIM_ISDIGIT(*s))
3596 minwid = (int)getdigits(&s);
3597 if (minwid < 0) /* overflow */
3598 minwid = 0;
3600 if (*s == STL_USER_HL)
3602 item[curitem].type = Highlight;
3603 item[curitem].start = p;
3604 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3605 s++;
3606 curitem++;
3607 continue;
3609 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3611 if (*s == STL_TABCLOSENR)
3613 if (minwid == 0)
3615 /* %X ends the close label, go back to the previously
3616 * define tab label nr. */
3617 for (n = curitem - 1; n >= 0; --n)
3618 if (item[n].type == TabPage && item[n].minwid >= 0)
3620 minwid = item[n].minwid;
3621 break;
3624 else
3625 /* close nrs are stored as negative values */
3626 minwid = - minwid;
3628 item[curitem].type = TabPage;
3629 item[curitem].start = p;
3630 item[curitem].minwid = minwid;
3631 s++;
3632 curitem++;
3633 continue;
3635 if (*s == '.')
3637 s++;
3638 if (VIM_ISDIGIT(*s))
3640 maxwid = (int)getdigits(&s);
3641 if (maxwid <= 0) /* overflow */
3642 maxwid = 50;
3645 minwid = (minwid > 50 ? 50 : minwid) * l;
3646 if (*s == '(')
3648 groupitem[groupdepth++] = curitem;
3649 item[curitem].type = Group;
3650 item[curitem].start = p;
3651 item[curitem].minwid = minwid;
3652 item[curitem].maxwid = maxwid;
3653 s++;
3654 curitem++;
3655 continue;
3657 if (vim_strchr(STL_ALL, *s) == NULL)
3659 s++;
3660 continue;
3662 opt = *s++;
3664 /* OK - now for the real work */
3665 base = 'D';
3666 itemisflag = FALSE;
3667 fillable = TRUE;
3668 num = -1;
3669 str = NULL;
3670 switch (opt)
3672 case STL_FILEPATH:
3673 case STL_FULLPATH:
3674 case STL_FILENAME:
3675 fillable = FALSE; /* don't change ' ' to fillchar */
3676 if (buf_spname(wp->w_buffer) != NULL)
3677 STRCPY(NameBuff, buf_spname(wp->w_buffer));
3678 else
3680 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
3681 : wp->w_buffer->b_fname;
3682 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3684 trans_characters(NameBuff, MAXPATHL);
3685 if (opt != STL_FILENAME)
3686 str = NameBuff;
3687 else
3688 str = gettail(NameBuff);
3689 break;
3691 case STL_VIM_EXPR: /* '{' */
3692 itemisflag = TRUE;
3693 t = p;
3694 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3695 *p++ = *s++;
3696 if (*s != '}') /* missing '}' or out of space */
3697 break;
3698 s++;
3699 *p = 0;
3700 p = t;
3702 #ifdef FEAT_EVAL
3703 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
3704 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3706 o_curbuf = curbuf;
3707 o_curwin = curwin;
3708 curwin = wp;
3709 curbuf = wp->w_buffer;
3711 str = eval_to_string_safe(p, &t, use_sandbox);
3713 curwin = o_curwin;
3714 curbuf = o_curbuf;
3715 do_unlet((char_u *)"g:actual_curbuf", TRUE);
3717 if (str != NULL && *str != 0)
3719 if (*skipdigits(str) == NUL)
3721 num = atoi((char *)str);
3722 vim_free(str);
3723 str = NULL;
3724 itemisflag = FALSE;
3727 #endif
3728 break;
3730 case STL_LINE:
3731 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3732 ? 0L : (long)(wp->w_cursor.lnum);
3733 break;
3735 case STL_NUMLINES:
3736 num = wp->w_buffer->b_ml.ml_line_count;
3737 break;
3739 case STL_COLUMN:
3740 num = !(State & INSERT) && empty_line
3741 ? 0 : (int)wp->w_cursor.col + 1;
3742 break;
3744 case STL_VIRTCOL:
3745 case STL_VIRTCOL_ALT:
3746 /* In list mode virtcol needs to be recomputed */
3747 virtcol = wp->w_virtcol;
3748 if (wp->w_p_list && lcs_tab1 == NUL)
3750 wp->w_p_list = FALSE;
3751 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3752 wp->w_p_list = TRUE;
3754 ++virtcol;
3755 /* Don't display %V if it's the same as %c. */
3756 if (opt == STL_VIRTCOL_ALT
3757 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3758 ? 0 : (int)wp->w_cursor.col + 1)))
3759 break;
3760 num = (long)virtcol;
3761 break;
3763 case STL_PERCENTAGE:
3764 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3765 (long)wp->w_buffer->b_ml.ml_line_count);
3766 break;
3768 case STL_ALTPERCENT:
3769 str = tmp;
3770 get_rel_pos(wp, str, TMPLEN);
3771 break;
3773 case STL_ARGLISTSTAT:
3774 fillable = FALSE;
3775 tmp[0] = 0;
3776 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
3777 str = tmp;
3778 break;
3780 case STL_KEYMAP:
3781 fillable = FALSE;
3782 if (get_keymap_str(wp, tmp, TMPLEN))
3783 str = tmp;
3784 break;
3785 case STL_PAGENUM:
3786 #if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
3787 num = printer_page_num;
3788 #else
3789 num = 0;
3790 #endif
3791 break;
3793 case STL_BUFNO:
3794 num = wp->w_buffer->b_fnum;
3795 break;
3797 case STL_OFFSET_X:
3798 base = 'X';
3799 case STL_OFFSET:
3800 #ifdef FEAT_BYTEOFF
3801 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
3802 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
3803 0L : l + 1 + (!(State & INSERT) && empty_line ?
3804 0 : (int)wp->w_cursor.col);
3805 #endif
3806 break;
3808 case STL_BYTEVAL_X:
3809 base = 'X';
3810 case STL_BYTEVAL:
3811 if (wp->w_cursor.col > (colnr_T)STRLEN(linecont))
3812 num = 0;
3813 else
3815 #ifdef FEAT_MBYTE
3816 num = (*mb_ptr2char)(linecont + wp->w_cursor.col);
3817 #else
3818 num = linecont[wp->w_cursor.col];
3819 #endif
3821 if (num == NL)
3822 num = 0;
3823 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
3824 num = NL;
3825 break;
3827 case STL_ROFLAG:
3828 case STL_ROFLAG_ALT:
3829 itemisflag = TRUE;
3830 if (wp->w_buffer->b_p_ro)
3831 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : "[RO]");
3832 break;
3834 case STL_HELPFLAG:
3835 case STL_HELPFLAG_ALT:
3836 itemisflag = TRUE;
3837 if (wp->w_buffer->b_help)
3838 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
3839 : _("[Help]"));
3840 break;
3842 #ifdef FEAT_AUTOCMD
3843 case STL_FILETYPE:
3844 if (*wp->w_buffer->b_p_ft != NUL
3845 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
3847 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
3848 wp->w_buffer->b_p_ft);
3849 str = tmp;
3851 break;
3853 case STL_FILETYPE_ALT:
3854 itemisflag = TRUE;
3855 if (*wp->w_buffer->b_p_ft != NUL
3856 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
3858 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
3859 wp->w_buffer->b_p_ft);
3860 for (t = tmp; *t != 0; t++)
3861 *t = TOUPPER_LOC(*t);
3862 str = tmp;
3864 break;
3865 #endif
3867 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3868 case STL_PREVIEWFLAG:
3869 case STL_PREVIEWFLAG_ALT:
3870 itemisflag = TRUE;
3871 if (wp->w_p_pvw)
3872 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
3873 : _("[Preview]"));
3874 break;
3875 #endif
3877 case STL_MODIFIED:
3878 case STL_MODIFIED_ALT:
3879 itemisflag = TRUE;
3880 switch ((opt == STL_MODIFIED_ALT)
3881 + bufIsChanged(wp->w_buffer) * 2
3882 + (!wp->w_buffer->b_p_ma) * 4)
3884 case 2: str = (char_u *)"[+]"; break;
3885 case 3: str = (char_u *)",+"; break;
3886 case 4: str = (char_u *)"[-]"; break;
3887 case 5: str = (char_u *)",-"; break;
3888 case 6: str = (char_u *)"[+-]"; break;
3889 case 7: str = (char_u *)",+-"; break;
3891 break;
3893 case STL_HIGHLIGHT:
3894 t = s;
3895 while (*s != '#' && *s != NUL)
3896 ++s;
3897 if (*s == '#')
3899 item[curitem].type = Highlight;
3900 item[curitem].start = p;
3901 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
3902 curitem++;
3904 ++s;
3905 continue;
3908 item[curitem].start = p;
3909 item[curitem].type = Normal;
3910 if (str != NULL && *str)
3912 t = str;
3913 if (itemisflag)
3915 if ((t[0] && t[1])
3916 && ((!prevchar_isitem && *t == ',')
3917 || (prevchar_isflag && *t == ' ')))
3918 t++;
3919 prevchar_isflag = TRUE;
3921 l = vim_strsize(t);
3922 if (l > 0)
3923 prevchar_isitem = TRUE;
3924 if (l > maxwid)
3926 while (l >= maxwid)
3927 #ifdef FEAT_MBYTE
3928 if (has_mbyte)
3930 l -= ptr2cells(t);
3931 t += (*mb_ptr2len)(t);
3933 else
3934 #endif
3935 l -= byte2cells(*t++);
3936 if (p + 1 >= out + outlen)
3937 break;
3938 *p++ = '<';
3940 if (minwid > 0)
3942 for (; l < minwid && p + 1 < out + outlen; l++)
3944 /* Don't put a "-" in front of a digit. */
3945 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
3946 *p++ = ' ';
3947 else
3948 *p++ = fillchar;
3950 minwid = 0;
3952 else
3953 minwid *= -1;
3954 while (*t && p + 1 < out + outlen)
3956 *p++ = *t++;
3957 /* Change a space by fillchar, unless fillchar is '-' and a
3958 * digit follows. */
3959 if (fillable && p[-1] == ' '
3960 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
3961 p[-1] = fillchar;
3963 for (; l < minwid && p + 1 < out + outlen; l++)
3964 *p++ = fillchar;
3966 else if (num >= 0)
3968 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
3969 char_u nstr[20];
3971 if (p + 20 >= out + outlen)
3972 break; /* not sufficient space */
3973 prevchar_isitem = TRUE;
3974 t = nstr;
3975 if (opt == STL_VIRTCOL_ALT)
3977 *t++ = '-';
3978 minwid--;
3980 *t++ = '%';
3981 if (zeropad)
3982 *t++ = '0';
3983 *t++ = '*';
3984 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
3985 *t = 0;
3987 for (n = num, l = 1; n >= nbase; n /= nbase)
3988 l++;
3989 if (opt == STL_VIRTCOL_ALT)
3990 l++;
3991 if (l > maxwid)
3993 l += 2;
3994 n = l - maxwid;
3995 while (l-- > maxwid)
3996 num /= nbase;
3997 *t++ = '>';
3998 *t++ = '%';
3999 *t = t[-3];
4000 *++t = 0;
4001 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4002 0, num, n);
4004 else
4005 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4006 minwid, num);
4007 p += STRLEN(p);
4009 else
4010 item[curitem].type = Empty;
4012 if (opt == STL_VIM_EXPR)
4013 vim_free(str);
4015 if (num >= 0 || (!itemisflag && str && *str))
4016 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4017 curitem++;
4019 *p = NUL;
4020 itemcnt = curitem;
4022 #ifdef FEAT_EVAL
4023 if (usefmt != fmt)
4024 vim_free(usefmt);
4025 #endif
4027 width = vim_strsize(out);
4028 if (maxwidth > 0 && width > maxwidth)
4030 /* Result is too long, must truncate somewhere. */
4031 l = 0;
4032 if (itemcnt == 0)
4033 s = out;
4034 else
4036 for ( ; l < itemcnt; l++)
4037 if (item[l].type == Trunc)
4039 /* Truncate at %< item. */
4040 s = item[l].start;
4041 break;
4043 if (l == itemcnt)
4045 /* No %< item, truncate first item. */
4046 s = item[0].start;
4047 l = 0;
4051 if (width - vim_strsize(s) >= maxwidth)
4053 /* Truncation mark is beyond max length */
4054 #ifdef FEAT_MBYTE
4055 if (has_mbyte)
4057 s = out;
4058 width = 0;
4059 for (;;)
4061 width += ptr2cells(s);
4062 if (width >= maxwidth)
4063 break;
4064 s += (*mb_ptr2len)(s);
4066 /* Fill up for half a double-wide character. */
4067 while (++width < maxwidth)
4068 *s++ = fillchar;
4070 else
4071 #endif
4072 s = out + maxwidth - 1;
4073 for (l = 0; l < itemcnt; l++)
4074 if (item[l].start > s)
4075 break;
4076 itemcnt = l;
4077 *s++ = '>';
4078 *s = 0;
4080 else
4082 #ifdef FEAT_MBYTE
4083 if (has_mbyte)
4085 n = 0;
4086 while (width >= maxwidth)
4088 width -= ptr2cells(s + n);
4089 n += (*mb_ptr2len)(s + n);
4092 else
4093 #endif
4094 n = width - maxwidth + 1;
4095 p = s + n;
4096 STRMOVE(s + 1, p);
4097 *s = '<';
4099 /* Fill up for half a double-wide character. */
4100 while (++width < maxwidth)
4102 s = s + STRLEN(s);
4103 *s++ = fillchar;
4104 *s = NUL;
4107 --n; /* count the '<' */
4108 for (; l < itemcnt; l++)
4110 if (item[l].start - n >= s)
4111 item[l].start -= n;
4112 else
4113 item[l].start = s;
4116 width = maxwidth;
4118 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4120 /* Apply STL_MIDDLE if any */
4121 for (l = 0; l < itemcnt; l++)
4122 if (item[l].type == Middle)
4123 break;
4124 if (l < itemcnt)
4126 p = item[l].start + maxwidth - width;
4127 STRMOVE(p, item[l].start);
4128 for (s = item[l].start; s < p; s++)
4129 *s = fillchar;
4130 for (l++; l < itemcnt; l++)
4131 item[l].start += maxwidth - width;
4132 width = maxwidth;
4136 /* Store the info about highlighting. */
4137 if (hltab != NULL)
4139 sp = hltab;
4140 for (l = 0; l < itemcnt; l++)
4142 if (item[l].type == Highlight)
4144 sp->start = item[l].start;
4145 sp->userhl = item[l].minwid;
4146 sp++;
4149 sp->start = NULL;
4150 sp->userhl = 0;
4153 /* Store the info about tab pages labels. */
4154 if (tabtab != NULL)
4156 sp = tabtab;
4157 for (l = 0; l < itemcnt; l++)
4159 if (item[l].type == TabPage)
4161 sp->start = item[l].start;
4162 sp->userhl = item[l].minwid;
4163 sp++;
4166 sp->start = NULL;
4167 sp->userhl = 0;
4170 return width;
4172 #endif /* FEAT_STL_OPT */
4174 #if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4175 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
4177 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4178 * using "Top", "Bot" or "All" when appropriate.
4180 void
4181 get_rel_pos(wp, buf, buflen)
4182 win_T *wp;
4183 char_u *buf;
4184 int buflen;
4186 long above; /* number of lines above window */
4187 long below; /* number of lines below window */
4189 above = wp->w_topline - 1;
4190 #ifdef FEAT_DIFF
4191 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
4192 #endif
4193 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4194 if (below <= 0)
4195 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4196 (size_t)(buflen - 1));
4197 else if (above <= 0)
4198 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
4199 else
4200 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
4201 ? (int)(above / ((above + below) / 100L))
4202 : (int)(above * 100L / (above + below)));
4204 #endif
4207 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
4208 * Return TRUE if it was appended.
4210 static int
4211 append_arg_number(wp, buf, buflen, add_file)
4212 win_T *wp;
4213 char_u *buf;
4214 int buflen;
4215 int add_file; /* Add "file" before the arg number */
4217 char_u *p;
4219 if (ARGCOUNT <= 1) /* nothing to do */
4220 return FALSE;
4222 p = buf + STRLEN(buf); /* go to the end of the buffer */
4223 if (p - buf + 35 >= buflen) /* getting too long */
4224 return FALSE;
4225 *p++ = ' ';
4226 *p++ = '(';
4227 if (add_file)
4229 STRCPY(p, "file ");
4230 p += 5;
4232 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4233 wp->w_arg_idx_invalid ? "(%d) of %d)"
4234 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4235 return TRUE;
4239 * If fname is not a full path, make it a full path.
4240 * Returns pointer to allocated memory (NULL for failure).
4242 char_u *
4243 fix_fname(fname)
4244 char_u *fname;
4247 * Force expanding the path always for Unix, because symbolic links may
4248 * mess up the full path name, even though it starts with a '/'.
4249 * Also expand when there is ".." in the file name, try to remove it,
4250 * because "c:/src/../README" is equal to "c:/README".
4251 * Similarly "c:/src//file" is equal to "c:/src/file".
4252 * For MS-Windows also expand names like "longna~1" to "longname".
4254 #ifdef UNIX
4255 return FullName_save(fname, TRUE);
4256 #else
4257 if (!vim_isAbsName(fname)
4258 || strstr((char *)fname, "..") != NULL
4259 || strstr((char *)fname, "//") != NULL
4260 # ifdef BACKSLASH_IN_FILENAME
4261 || strstr((char *)fname, "\\\\") != NULL
4262 # endif
4263 # if defined(MSWIN) || defined(DJGPP)
4264 || vim_strchr(fname, '~') != NULL
4265 # endif
4267 return FullName_save(fname, FALSE);
4269 fname = vim_strsave(fname);
4271 # ifdef USE_FNAME_CASE
4272 # ifdef USE_LONG_FNAME
4273 if (USE_LONG_FNAME)
4274 # endif
4276 if (fname != NULL)
4277 fname_case(fname, 0); /* set correct case for file name */
4279 # endif
4281 return fname;
4282 #endif
4286 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4287 * "ffname" becomes a pointer to allocated memory (or NULL).
4289 void
4290 fname_expand(buf, ffname, sfname)
4291 buf_T *buf UNUSED;
4292 char_u **ffname;
4293 char_u **sfname;
4295 if (*ffname == NULL) /* if no file name given, nothing to do */
4296 return;
4297 if (*sfname == NULL) /* if no short file name given, use ffname */
4298 *sfname = *ffname;
4299 *ffname = fix_fname(*ffname); /* expand to full path */
4301 #ifdef FEAT_SHORTCUT
4302 if (!buf->b_p_bin)
4304 char_u *rfname;
4306 /* If the file name is a shortcut file, use the file it links to. */
4307 rfname = mch_resolve_shortcut(*ffname);
4308 if (rfname != NULL)
4310 vim_free(*ffname);
4311 *ffname = rfname;
4312 *sfname = rfname;
4315 #endif
4319 * Get the file name for an argument list entry.
4321 char_u *
4322 alist_name(aep)
4323 aentry_T *aep;
4325 buf_T *bp;
4327 /* Use the name from the associated buffer if it exists. */
4328 bp = buflist_findnr(aep->ae_fnum);
4329 if (bp == NULL || bp->b_fname == NULL)
4330 return aep->ae_fname;
4331 return bp->b_fname;
4334 #if defined(FEAT_WINDOWS) || defined(PROTO)
4336 * do_arg_all(): Open up to 'count' windows, one for each argument.
4338 void
4339 do_arg_all(count, forceit, keep_tabs)
4340 int count;
4341 int forceit; /* hide buffers in current windows */
4342 int keep_tabs; /* keep current tabs, for ":tab drop file" */
4344 int i;
4345 win_T *wp, *wpnext;
4346 char_u *opened; /* array of flags for which args are open */
4347 int opened_len; /* length of opened[] */
4348 int use_firstwin = FALSE; /* use first window for arglist */
4349 int split_ret = OK;
4350 int p_ea_save;
4351 alist_T *alist; /* argument list to be used */
4352 buf_T *buf;
4353 tabpage_T *tpnext;
4354 int had_tab = cmdmod.tab;
4355 win_T *new_curwin = NULL;
4356 tabpage_T *new_curtab = NULL;
4358 if (ARGCOUNT <= 0)
4360 /* Don't give an error message. We don't want it when the ":all"
4361 * command is in the .vimrc. */
4362 return;
4364 setpcmark();
4366 opened_len = ARGCOUNT;
4367 opened = alloc_clear((unsigned)opened_len);
4368 if (opened == NULL)
4369 return;
4371 #ifdef FEAT_GUI
4372 need_mouse_correct = TRUE;
4373 #endif
4376 * Try closing all windows that are not in the argument list.
4377 * Also close windows that are not full width;
4378 * When 'hidden' or "forceit" set the buffer becomes hidden.
4379 * Windows that have a changed buffer and can't be hidden won't be closed.
4380 * When the ":tab" modifier was used do this for all tab pages.
4382 if (had_tab > 0)
4383 goto_tabpage_tp(first_tabpage);
4384 for (;;)
4386 tpnext = curtab->tp_next;
4387 for (wp = firstwin; wp != NULL; wp = wpnext)
4389 wpnext = wp->w_next;
4390 buf = wp->w_buffer;
4391 if (buf->b_ffname == NULL
4392 || buf->b_nwindows > 1
4393 #ifdef FEAT_VERTSPLIT
4394 || wp->w_width != Columns
4395 #endif
4397 i = ARGCOUNT;
4398 else
4400 /* check if the buffer in this window is in the arglist */
4401 for (i = 0; i < ARGCOUNT; ++i)
4403 if (ARGLIST[i].ae_fnum == buf->b_fnum
4404 || fullpathcmp(alist_name(&ARGLIST[i]),
4405 buf->b_ffname, TRUE) & FPC_SAME)
4407 if (i < opened_len)
4409 opened[i] = TRUE;
4410 if (i == 0)
4412 new_curwin = wp;
4413 new_curtab = curtab;
4416 if (wp->w_alist != curwin->w_alist)
4418 /* Use the current argument list for all windows
4419 * containing a file from it. */
4420 alist_unlink(wp->w_alist);
4421 wp->w_alist = curwin->w_alist;
4422 ++wp->w_alist->al_refcount;
4424 break;
4428 wp->w_arg_idx = i;
4430 if (i == ARGCOUNT && !keep_tabs) /* close this window */
4432 if (P_HID(buf) || forceit || buf->b_nwindows > 1
4433 || !bufIsChanged(buf))
4435 /* If the buffer was changed, and we would like to hide it,
4436 * try autowriting. */
4437 if (!P_HID(buf) && buf->b_nwindows <= 1
4438 && bufIsChanged(buf))
4440 (void)autowrite(buf, FALSE);
4441 #ifdef FEAT_AUTOCMD
4442 /* check if autocommands removed the window */
4443 if (!win_valid(wp) || !buf_valid(buf))
4445 wpnext = firstwin; /* start all over... */
4446 continue;
4448 #endif
4450 #ifdef FEAT_WINDOWS
4451 /* don't close last window */
4452 if (firstwin == lastwin && first_tabpage->tp_next == NULL)
4453 #endif
4454 use_firstwin = TRUE;
4455 #ifdef FEAT_WINDOWS
4456 else
4458 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4459 # ifdef FEAT_AUTOCMD
4460 /* check if autocommands removed the next window */
4461 if (!win_valid(wpnext))
4462 wpnext = firstwin; /* start all over... */
4463 # endif
4465 #endif
4470 /* Without the ":tab" modifier only do the current tab page. */
4471 if (had_tab == 0 || tpnext == NULL)
4472 break;
4474 # ifdef FEAT_AUTOCMD
4475 /* check if autocommands removed the next tab page */
4476 if (!valid_tabpage(tpnext))
4477 tpnext = first_tabpage; /* start all over...*/
4478 # endif
4479 goto_tabpage_tp(tpnext);
4483 * Open a window for files in the argument list that don't have one.
4484 * ARGCOUNT may change while doing this, because of autocommands.
4486 if (count > ARGCOUNT || count <= 0)
4487 count = ARGCOUNT;
4489 /* Autocommands may do anything to the argument list. Make sure it's not
4490 * freed while we are working here by "locking" it. We still have to
4491 * watch out for its size to be changed. */
4492 alist = curwin->w_alist;
4493 ++alist->al_refcount;
4495 #ifdef FEAT_AUTOCMD
4496 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4497 ++autocmd_no_enter;
4498 ++autocmd_no_leave;
4499 #endif
4500 win_enter(lastwin, FALSE);
4501 #ifdef FEAT_WINDOWS
4502 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4503 * leaving an empty tab page when executed locally. */
4504 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4505 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4506 use_firstwin = TRUE;
4507 #endif
4509 for (i = 0; i < count && i < alist->al_ga.ga_len && !got_int; ++i)
4511 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4512 arg_had_last = TRUE;
4513 if (i < opened_len && opened[i])
4515 /* Move the already present window to below the current window */
4516 if (curwin->w_arg_idx != i)
4518 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4520 if (wpnext->w_arg_idx == i)
4522 win_move_after(wpnext, curwin);
4523 break;
4528 else if (split_ret == OK)
4530 if (!use_firstwin) /* split current window */
4532 p_ea_save = p_ea;
4533 p_ea = TRUE; /* use space from all windows */
4534 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4535 p_ea = p_ea_save;
4536 if (split_ret == FAIL)
4537 continue;
4539 #ifdef FEAT_AUTOCMD
4540 else /* first window: do autocmd for leaving this buffer */
4541 --autocmd_no_leave;
4542 #endif
4545 * edit file "i"
4547 curwin->w_arg_idx = i;
4548 if (i == 0)
4550 new_curwin = curwin;
4551 new_curtab = curtab;
4553 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4554 ECMD_ONE,
4555 ((P_HID(curwin->w_buffer)
4556 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
4557 + ECMD_OLDBUF, curwin);
4558 #ifdef FEAT_AUTOCMD
4559 if (use_firstwin)
4560 ++autocmd_no_leave;
4561 #endif
4562 use_firstwin = FALSE;
4564 ui_breakcheck();
4566 /* When ":tab" was used open a new tab for a new window repeatedly. */
4567 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4568 cmdmod.tab = 9999;
4571 /* Remove the "lock" on the argument list. */
4572 alist_unlink(alist);
4574 #ifdef FEAT_AUTOCMD
4575 --autocmd_no_enter;
4576 #endif
4577 /* to window with first arg */
4578 if (valid_tabpage(new_curtab))
4579 goto_tabpage_tp(new_curtab);
4580 if (win_valid(new_curwin))
4581 win_enter(new_curwin, FALSE);
4583 #ifdef FEAT_AUTOCMD
4584 --autocmd_no_leave;
4585 #endif
4586 vim_free(opened);
4589 # if defined(FEAT_LISTCMDS) || defined(PROTO)
4591 * Open a window for a number of buffers.
4593 void
4594 ex_buffer_all(eap)
4595 exarg_T *eap;
4597 buf_T *buf;
4598 win_T *wp, *wpnext;
4599 int split_ret = OK;
4600 int p_ea_save;
4601 int open_wins = 0;
4602 int r;
4603 int count; /* Maximum number of windows to open. */
4604 int all; /* When TRUE also load inactive buffers. */
4605 #ifdef FEAT_WINDOWS
4606 int had_tab = cmdmod.tab;
4607 tabpage_T *tpnext;
4608 #endif
4610 if (eap->addr_count == 0) /* make as many windows as possible */
4611 count = 9999;
4612 else
4613 count = eap->line2; /* make as many windows as specified */
4614 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4615 all = FALSE;
4616 else
4617 all = TRUE;
4619 setpcmark();
4621 #ifdef FEAT_GUI
4622 need_mouse_correct = TRUE;
4623 #endif
4626 * Close superfluous windows (two windows for the same buffer).
4627 * Also close windows that are not full-width.
4629 #ifdef FEAT_WINDOWS
4630 if (had_tab > 0)
4631 goto_tabpage_tp(first_tabpage);
4632 for (;;)
4634 #endif
4635 tpnext = curtab->tp_next;
4636 for (wp = firstwin; wp != NULL; wp = wpnext)
4638 wpnext = wp->w_next;
4639 if ((wp->w_buffer->b_nwindows > 1
4640 #ifdef FEAT_VERTSPLIT
4641 || ((cmdmod.split & WSP_VERT)
4642 ? wp->w_height + wp->w_status_height < Rows - p_ch
4643 - tabline_height()
4644 : wp->w_width != Columns)
4645 #endif
4646 #ifdef FEAT_WINDOWS
4647 || (had_tab > 0 && wp != firstwin)
4648 #endif
4649 ) && firstwin != lastwin)
4651 win_close(wp, FALSE);
4652 #ifdef FEAT_AUTOCMD
4653 wpnext = firstwin; /* just in case an autocommand does
4654 something strange with windows */
4655 tpnext = first_tabpage; /* start all over...*/
4656 open_wins = 0;
4657 #endif
4659 else
4660 ++open_wins;
4663 #ifdef FEAT_WINDOWS
4664 /* Without the ":tab" modifier only do the current tab page. */
4665 if (had_tab == 0 || tpnext == NULL)
4666 break;
4667 goto_tabpage_tp(tpnext);
4669 #endif
4672 * Go through the buffer list. When a buffer doesn't have a window yet,
4673 * open one. Otherwise move the window to the right position.
4674 * Watch out for autocommands that delete buffers or windows!
4676 #ifdef FEAT_AUTOCMD
4677 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4678 ++autocmd_no_enter;
4679 #endif
4680 win_enter(lastwin, FALSE);
4681 #ifdef FEAT_AUTOCMD
4682 ++autocmd_no_leave;
4683 #endif
4684 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4686 /* Check if this buffer needs a window */
4687 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4688 continue;
4690 #ifdef FEAT_WINDOWS
4691 if (had_tab != 0)
4693 /* With the ":tab" modifier don't move the window. */
4694 if (buf->b_nwindows > 0)
4695 wp = lastwin; /* buffer has a window, skip it */
4696 else
4697 wp = NULL;
4699 else
4700 #endif
4702 /* Check if this buffer already has a window */
4703 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4704 if (wp->w_buffer == buf)
4705 break;
4706 /* If the buffer already has a window, move it */
4707 if (wp != NULL)
4708 win_move_after(wp, curwin);
4711 if (wp == NULL && split_ret == OK)
4713 /* Split the window and put the buffer in it */
4714 p_ea_save = p_ea;
4715 p_ea = TRUE; /* use space from all windows */
4716 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4717 ++open_wins;
4718 p_ea = p_ea_save;
4719 if (split_ret == FAIL)
4720 continue;
4722 /* Open the buffer in this window. */
4723 #if defined(HAS_SWAP_EXISTS_ACTION)
4724 swap_exists_action = SEA_DIALOG;
4725 #endif
4726 set_curbuf(buf, DOBUF_GOTO);
4727 #ifdef FEAT_AUTOCMD
4728 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
4730 #if defined(HAS_SWAP_EXISTS_ACTION)
4731 swap_exists_action = SEA_NONE;
4732 # endif
4733 break;
4735 #endif
4736 #if defined(HAS_SWAP_EXISTS_ACTION)
4737 if (swap_exists_action == SEA_QUIT)
4739 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4740 cleanup_T cs;
4742 /* Reset the error/interrupt/exception state here so that
4743 * aborting() returns FALSE when closing a window. */
4744 enter_cleanup(&cs);
4745 # endif
4747 /* User selected Quit at ATTENTION prompt; close this window. */
4748 win_close(curwin, TRUE);
4749 --open_wins;
4750 swap_exists_action = SEA_NONE;
4751 swap_exists_did_quit = TRUE;
4753 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4754 /* Restore the error/interrupt/exception state if not
4755 * discarded by a new aborting error, interrupt, or uncaught
4756 * exception. */
4757 leave_cleanup(&cs);
4758 # endif
4760 else
4761 handle_swap_exists(NULL);
4762 #endif
4765 ui_breakcheck();
4766 if (got_int)
4768 (void)vgetc(); /* only break the file loading, not the rest */
4769 break;
4771 #ifdef FEAT_EVAL
4772 /* Autocommands deleted the buffer or aborted script processing!!! */
4773 if (aborting())
4774 break;
4775 #endif
4776 #ifdef FEAT_WINDOWS
4777 /* When ":tab" was used open a new tab for a new window repeatedly. */
4778 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4779 cmdmod.tab = 9999;
4780 #endif
4782 #ifdef FEAT_AUTOCMD
4783 --autocmd_no_enter;
4784 #endif
4785 win_enter(firstwin, FALSE); /* back to first window */
4786 #ifdef FEAT_AUTOCMD
4787 --autocmd_no_leave;
4788 #endif
4791 * Close superfluous windows.
4793 for (wp = lastwin; open_wins > count; )
4795 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
4796 || autowrite(wp->w_buffer, FALSE) == OK);
4797 #ifdef FEAT_AUTOCMD
4798 if (!win_valid(wp))
4800 /* BufWrite Autocommands made the window invalid, start over */
4801 wp = lastwin;
4803 else
4804 #endif
4805 if (r)
4807 win_close(wp, !P_HID(wp->w_buffer));
4808 --open_wins;
4809 wp = lastwin;
4811 else
4813 wp = wp->w_prev;
4814 if (wp == NULL)
4815 break;
4819 # endif /* FEAT_LISTCMDS */
4821 #endif /* FEAT_WINDOWS */
4823 static int chk_modeline __ARGS((linenr_T, int));
4826 * do_modelines() - process mode lines for the current file
4828 * "flags" can be:
4829 * OPT_WINONLY only set options local to window
4830 * OPT_NOWIN don't set options local to window
4832 * Returns immediately if the "ml" option isn't set.
4834 void
4835 do_modelines(flags)
4836 int flags;
4838 linenr_T lnum;
4839 int nmlines;
4840 static int entered = 0;
4842 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
4843 return;
4845 /* Disallow recursive entry here. Can happen when executing a modeline
4846 * triggers an autocommand, which reloads modelines with a ":do". */
4847 if (entered)
4848 return;
4850 ++entered;
4851 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
4852 ++lnum)
4853 if (chk_modeline(lnum, flags) == FAIL)
4854 nmlines = 0;
4856 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
4857 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
4858 if (chk_modeline(lnum, flags) == FAIL)
4859 nmlines = 0;
4860 --entered;
4863 #include "version.h" /* for version number */
4866 * chk_modeline() - check a single line for a mode string
4867 * Return FAIL if an error encountered.
4869 static int
4870 chk_modeline(lnum, flags)
4871 linenr_T lnum;
4872 int flags; /* Same as for do_modelines(). */
4874 char_u *s;
4875 char_u *e;
4876 char_u *linecopy; /* local copy of any modeline found */
4877 int prev;
4878 int vers;
4879 int end;
4880 int retval = OK;
4881 char_u *save_sourcing_name;
4882 linenr_T save_sourcing_lnum;
4883 #ifdef FEAT_EVAL
4884 scid_T save_SID;
4885 #endif
4887 prev = -1;
4888 for (s = ml_get(lnum); *s != NUL; ++s)
4890 if (prev == -1 || vim_isspace(prev))
4892 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
4893 || STRNCMP(s, "vi:", (size_t)3) == 0)
4894 break;
4895 if (STRNCMP(s, "vim", 3) == 0)
4897 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
4898 e = s + 4;
4899 else
4900 e = s + 3;
4901 vers = getdigits(&e);
4902 if (*e == ':'
4903 && (s[3] == ':'
4904 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
4905 || (VIM_VERSION_100 < vers && s[3] == '<')
4906 || (VIM_VERSION_100 > vers && s[3] == '>')
4907 || (VIM_VERSION_100 == vers && s[3] == '=')))
4908 break;
4911 prev = *s;
4914 if (*s)
4916 do /* skip over "ex:", "vi:" or "vim:" */
4917 ++s;
4918 while (s[-1] != ':');
4920 s = linecopy = vim_strsave(s); /* copy the line, it will change */
4921 if (linecopy == NULL)
4922 return FAIL;
4924 save_sourcing_lnum = sourcing_lnum;
4925 save_sourcing_name = sourcing_name;
4926 sourcing_lnum = lnum; /* prepare for emsg() */
4927 sourcing_name = (char_u *)"modelines";
4929 end = FALSE;
4930 while (end == FALSE)
4932 s = skipwhite(s);
4933 if (*s == NUL)
4934 break;
4937 * Find end of set command: ':' or end of line.
4938 * Skip over "\:", replacing it with ":".
4940 for (e = s; *e != ':' && *e != NUL; ++e)
4941 if (e[0] == '\\' && e[1] == ':')
4942 STRMOVE(e, e + 1);
4943 if (*e == NUL)
4944 end = TRUE;
4947 * If there is a "set" command, require a terminating ':' and
4948 * ignore the stuff after the ':'.
4949 * "vi:set opt opt opt: foo" -- foo not interpreted
4950 * "vi:opt opt opt: foo" -- foo interpreted
4951 * Accept "se" for compatibility with Elvis.
4953 if (STRNCMP(s, "set ", (size_t)4) == 0
4954 || STRNCMP(s, "se ", (size_t)3) == 0)
4956 if (*e != ':') /* no terminating ':'? */
4957 break;
4958 end = TRUE;
4959 s = vim_strchr(s, ' ') + 1;
4961 *e = NUL; /* truncate the set command */
4963 if (*s != NUL) /* skip over an empty "::" */
4965 #ifdef FEAT_EVAL
4966 save_SID = current_SID;
4967 current_SID = SID_MODELINE;
4968 #endif
4969 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
4970 #ifdef FEAT_EVAL
4971 current_SID = save_SID;
4972 #endif
4973 if (retval == FAIL) /* stop if error found */
4974 break;
4976 s = e + 1; /* advance to next part */
4979 sourcing_lnum = save_sourcing_lnum;
4980 sourcing_name = save_sourcing_name;
4982 vim_free(linecopy);
4984 return retval;
4987 #if defined(FEAT_VIMINFO) || defined(PROTO)
4989 read_viminfo_bufferlist(virp, writing)
4990 vir_T *virp;
4991 int writing;
4993 char_u *tab;
4994 linenr_T lnum;
4995 colnr_T col;
4996 buf_T *buf;
4997 char_u *sfname;
4998 char_u *xline;
5000 /* Handle long line and escaped characters. */
5001 xline = viminfo_readstring(virp, 1, FALSE);
5003 /* don't read in if there are files on the command-line or if writing: */
5004 if (xline != NULL && !writing && ARGCOUNT == 0
5005 && find_viminfo_parameter('%') != NULL)
5007 /* Format is: <fname> Tab <lnum> Tab <col>.
5008 * Watch out for a Tab in the file name, work from the end. */
5009 lnum = 0;
5010 col = 0;
5011 tab = vim_strrchr(xline, '\t');
5012 if (tab != NULL)
5014 *tab++ = '\0';
5015 col = (colnr_T)atoi((char *)tab);
5016 tab = vim_strrchr(xline, '\t');
5017 if (tab != NULL)
5019 *tab++ = '\0';
5020 lnum = atol((char *)tab);
5024 /* Expand "~/" in the file name at "line + 1" to a full path.
5025 * Then try shortening it by comparing with the current directory */
5026 expand_env(xline, NameBuff, MAXPATHL);
5027 sfname = shorten_fname1(NameBuff);
5029 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5030 if (buf != NULL) /* just in case... */
5032 buf->b_last_cursor.lnum = lnum;
5033 buf->b_last_cursor.col = col;
5034 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5037 vim_free(xline);
5039 return viminfo_readline(virp);
5042 void
5043 write_viminfo_bufferlist(fp)
5044 FILE *fp;
5046 buf_T *buf;
5047 #ifdef FEAT_WINDOWS
5048 win_T *win;
5049 tabpage_T *tp;
5050 #endif
5051 char_u *line;
5052 int max_buffers;
5053 size_t len;
5055 if (find_viminfo_parameter('%') == NULL)
5056 return;
5058 /* Without a number -1 is returned: do all buffers. */
5059 max_buffers = get_viminfo_parameter('%');
5061 /* Allocate room for the file name, lnum and col. */
5062 #define LINE_BUF_LEN (MAXPATHL + 40)
5063 line = alloc(LINE_BUF_LEN);
5064 if (line == NULL)
5065 return;
5067 #ifdef FEAT_WINDOWS
5068 FOR_ALL_TAB_WINDOWS(tp, win)
5069 set_last_cursor(win);
5070 #else
5071 set_last_cursor(curwin);
5072 #endif
5074 fprintf(fp, _("\n# Buffer list:\n"));
5075 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5077 if (buf->b_fname == NULL
5078 || !buf->b_p_bl
5079 #ifdef FEAT_QUICKFIX
5080 || bt_quickfix(buf)
5081 #endif
5082 || removable(buf->b_ffname))
5083 continue;
5085 if (max_buffers-- == 0)
5086 break;
5087 putc('%', fp);
5088 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
5089 len = STRLEN(line);
5090 vim_snprintf((char *)line + len, len - LINE_BUF_LEN, "\t%ld\t%d",
5091 (long)buf->b_last_cursor.lnum,
5092 buf->b_last_cursor.col);
5093 viminfo_writestring(fp, line);
5095 vim_free(line);
5097 #endif
5101 * Return special buffer name.
5102 * Returns NULL when the buffer has a normal file name.
5104 char *
5105 buf_spname(buf)
5106 buf_T *buf;
5108 #if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5109 if (bt_quickfix(buf))
5111 win_T *win = NULL;
5112 tabpage_T *tp;
5115 * For location list window, w_llist_ref points to the location list.
5116 * For quickfix window, w_llist_ref is NULL.
5118 FOR_ALL_TAB_WINDOWS(tp, win)
5119 if (win->w_buffer == buf)
5120 goto win_found;
5121 win_found:
5122 if (win != NULL && win->w_llist_ref != NULL)
5123 return _("[Location List]");
5124 else
5125 return _("[Quickfix List]");
5127 #endif
5128 #ifdef FEAT_QUICKFIX
5129 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5130 * contains the name as specified by the user */
5131 if (bt_nofile(buf))
5133 if (buf->b_sfname != NULL)
5134 return (char *)buf->b_sfname;
5135 return _("[Scratch]");
5137 #endif
5138 if (buf->b_fname == NULL)
5139 return _("[No Name]");
5140 return NULL;
5144 #if defined(FEAT_SIGNS) || defined(PROTO)
5146 * Insert the sign into the signlist.
5148 static void
5149 insert_sign(buf, prev, next, id, lnum, typenr)
5150 buf_T *buf; /* buffer to store sign in */
5151 signlist_T *prev; /* previous sign entry */
5152 signlist_T *next; /* next sign entry */
5153 int id; /* sign ID */
5154 linenr_T lnum; /* line number which gets the mark */
5155 int typenr; /* typenr of sign we are adding */
5157 signlist_T *newsign;
5159 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5160 if (newsign != NULL)
5162 newsign->id = id;
5163 newsign->lnum = lnum;
5164 newsign->typenr = typenr;
5165 newsign->next = next;
5166 #ifdef FEAT_NETBEANS_INTG
5167 newsign->prev = prev;
5168 if (next != NULL)
5169 next->prev = newsign;
5170 #endif
5172 if (prev == NULL)
5174 /* When adding first sign need to redraw the windows to create the
5175 * column for signs. */
5176 if (buf->b_signlist == NULL)
5178 redraw_buf_later(buf, NOT_VALID);
5179 changed_cline_bef_curs();
5182 /* first sign in signlist */
5183 buf->b_signlist = newsign;
5185 else
5186 prev->next = newsign;
5191 * Add the sign into the signlist. Find the right spot to do it though.
5193 void
5194 buf_addsign(buf, id, lnum, typenr)
5195 buf_T *buf; /* buffer to store sign in */
5196 int id; /* sign ID */
5197 linenr_T lnum; /* line number which gets the mark */
5198 int typenr; /* typenr of sign we are adding */
5200 signlist_T *sign; /* a sign in the signlist */
5201 signlist_T *prev; /* the previous sign */
5203 prev = NULL;
5204 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5206 if (lnum == sign->lnum && id == sign->id)
5208 sign->typenr = typenr;
5209 return;
5211 else if (
5212 #ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5213 id < 0 &&
5214 #endif
5215 lnum < sign->lnum)
5217 #ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5218 /* XXX - GRP: Is this because of sign slide problem? Or is it
5219 * really needed? Or is it because we allow multiple signs per
5220 * line? If so, should I add that feature to FEAT_SIGNS?
5222 while (prev != NULL && prev->lnum == lnum)
5223 prev = prev->prev;
5224 if (prev == NULL)
5225 sign = buf->b_signlist;
5226 else
5227 sign = prev->next;
5228 #endif
5229 insert_sign(buf, prev, sign, id, lnum, typenr);
5230 return;
5232 prev = sign;
5234 #ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5235 /* XXX - GRP: See previous comment */
5236 while (prev != NULL && prev->lnum == lnum)
5237 prev = prev->prev;
5238 if (prev == NULL)
5239 sign = buf->b_signlist;
5240 else
5241 sign = prev->next;
5242 #endif
5243 insert_sign(buf, prev, sign, id, lnum, typenr);
5245 return;
5248 linenr_T
5249 buf_change_sign_type(buf, markId, typenr)
5250 buf_T *buf; /* buffer to store sign in */
5251 int markId; /* sign ID */
5252 int typenr; /* typenr of sign we are adding */
5254 signlist_T *sign; /* a sign in the signlist */
5256 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5258 if (sign->id == markId)
5260 sign->typenr = typenr;
5261 return sign->lnum;
5265 return (linenr_T)0;
5269 buf_getsigntype(buf, lnum, type)
5270 buf_T *buf;
5271 linenr_T lnum;
5272 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5274 signlist_T *sign; /* a sign in a b_signlist */
5276 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5277 if (sign->lnum == lnum
5278 && (type == SIGN_ANY
5279 # ifdef FEAT_SIGN_ICONS
5280 || (type == SIGN_ICON
5281 && sign_get_image(sign->typenr) != NULL)
5282 # endif
5283 || (type == SIGN_TEXT
5284 && sign_get_text(sign->typenr) != NULL)
5285 || (type == SIGN_LINEHL
5286 && sign_get_attr(sign->typenr, TRUE) != 0)))
5287 return sign->typenr;
5288 return 0;
5292 linenr_T
5293 buf_delsign(buf, id)
5294 buf_T *buf; /* buffer sign is stored in */
5295 int id; /* sign id */
5297 signlist_T **lastp; /* pointer to pointer to current sign */
5298 signlist_T *sign; /* a sign in a b_signlist */
5299 signlist_T *next; /* the next sign in a b_signlist */
5300 linenr_T lnum; /* line number whose sign was deleted */
5302 lastp = &buf->b_signlist;
5303 lnum = 0;
5304 for (sign = buf->b_signlist; sign != NULL; sign = next)
5306 next = sign->next;
5307 if (sign->id == id)
5309 *lastp = next;
5310 #ifdef FEAT_NETBEANS_INTG
5311 if (next != NULL)
5312 next->prev = sign->prev;
5313 #endif
5314 lnum = sign->lnum;
5315 vim_free(sign);
5316 break;
5318 else
5319 lastp = &sign->next;
5322 /* When deleted the last sign need to redraw the windows to remove the
5323 * sign column. */
5324 if (buf->b_signlist == NULL)
5326 redraw_buf_later(buf, NOT_VALID);
5327 changed_cline_bef_curs();
5330 return lnum;
5335 * Find the line number of the sign with the requested id. If the sign does
5336 * not exist, return 0 as the line number. This will still let the correct file
5337 * get loaded.
5340 buf_findsign(buf, id)
5341 buf_T *buf; /* buffer to store sign in */
5342 int id; /* sign ID */
5344 signlist_T *sign; /* a sign in the signlist */
5346 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5347 if (sign->id == id)
5348 return sign->lnum;
5350 return 0;
5354 buf_findsign_id(buf, lnum)
5355 buf_T *buf; /* buffer whose sign we are searching for */
5356 linenr_T lnum; /* line number of sign */
5358 signlist_T *sign; /* a sign in the signlist */
5360 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5361 if (sign->lnum == lnum)
5362 return sign->id;
5364 return 0;
5368 # if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5369 /* see if a given type of sign exists on a specific line */
5371 buf_findsigntype_id(buf, lnum, typenr)
5372 buf_T *buf; /* buffer whose sign we are searching for */
5373 linenr_T lnum; /* line number of sign */
5374 int typenr; /* sign type number */
5376 signlist_T *sign; /* a sign in the signlist */
5378 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5379 if (sign->lnum == lnum && sign->typenr == typenr)
5380 return sign->id;
5382 return 0;
5386 # if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5387 /* return the number of icons on the given line */
5389 buf_signcount(buf, lnum)
5390 buf_T *buf;
5391 linenr_T lnum;
5393 signlist_T *sign; /* a sign in the signlist */
5394 int count = 0;
5396 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5397 if (sign->lnum == lnum)
5398 if (sign_get_image(sign->typenr) != NULL)
5399 count++;
5401 return count;
5403 # endif /* FEAT_SIGN_ICONS */
5404 # endif /* FEAT_NETBEANS_INTG */
5408 * Delete signs in buffer "buf".
5410 static void
5411 buf_delete_signs(buf)
5412 buf_T *buf;
5414 signlist_T *next;
5416 while (buf->b_signlist != NULL)
5418 next = buf->b_signlist->next;
5419 vim_free(buf->b_signlist);
5420 buf->b_signlist = next;
5425 * Delete all signs in all buffers.
5427 void
5428 buf_delete_all_signs()
5430 buf_T *buf; /* buffer we are checking for signs */
5432 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5433 if (buf->b_signlist != NULL)
5435 /* Need to redraw the windows to remove the sign column. */
5436 redraw_buf_later(buf, NOT_VALID);
5437 buf_delete_signs(buf);
5442 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5444 void
5445 sign_list_placed(rbuf)
5446 buf_T *rbuf;
5448 buf_T *buf;
5449 signlist_T *p;
5450 char lbuf[BUFSIZ];
5452 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5453 msg_putchar('\n');
5454 if (rbuf == NULL)
5455 buf = firstbuf;
5456 else
5457 buf = rbuf;
5458 while (buf != NULL)
5460 if (buf->b_signlist != NULL)
5462 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
5463 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5464 msg_putchar('\n');
5466 for (p = buf->b_signlist; p != NULL; p = p->next)
5468 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
5469 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5470 MSG_PUTS(lbuf);
5471 msg_putchar('\n');
5473 if (rbuf != NULL)
5474 break;
5475 buf = buf->b_next;
5480 * Adjust a placed sign for inserted/deleted lines.
5482 void
5483 sign_mark_adjust(line1, line2, amount, amount_after)
5484 linenr_T line1;
5485 linenr_T line2;
5486 long amount;
5487 long amount_after;
5489 signlist_T *sign; /* a sign in a b_signlist */
5491 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5493 if (sign->lnum >= line1 && sign->lnum <= line2)
5495 if (amount == MAXLNUM)
5496 sign->lnum = line1;
5497 else
5498 sign->lnum += amount;
5500 else if (sign->lnum > line2)
5501 sign->lnum += amount_after;
5504 #endif /* FEAT_SIGNS */
5507 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5509 void
5510 set_buflisted(on)
5511 int on;
5513 if (on != curbuf->b_p_bl)
5515 curbuf->b_p_bl = on;
5516 #ifdef FEAT_AUTOCMD
5517 if (on)
5518 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5519 else
5520 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5521 #endif
5526 * Read the file for "buf" again and check if the contents changed.
5527 * Return TRUE if it changed or this could not be checked.
5530 buf_contents_changed(buf)
5531 buf_T *buf;
5533 buf_T *newbuf;
5534 int differ = TRUE;
5535 linenr_T lnum;
5536 aco_save_T aco;
5537 exarg_T ea;
5539 /* Allocate a buffer without putting it in the buffer list. */
5540 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5541 if (newbuf == NULL)
5542 return TRUE;
5544 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5545 if (prep_exarg(&ea, buf) == FAIL)
5547 wipe_buffer(newbuf, FALSE);
5548 return TRUE;
5551 /* set curwin/curbuf to buf and save a few things */
5552 aucmd_prepbuf(&aco, newbuf);
5554 if (ml_open(curbuf) == OK
5555 && readfile(buf->b_ffname, buf->b_fname,
5556 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5557 &ea, READ_NEW | READ_DUMMY) == OK)
5559 /* compare the two files line by line */
5560 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5562 differ = FALSE;
5563 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5564 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5566 differ = TRUE;
5567 break;
5571 vim_free(ea.cmd);
5573 /* restore curwin/curbuf and a few other things */
5574 aucmd_restbuf(&aco);
5576 if (curbuf != newbuf) /* safety check */
5577 wipe_buffer(newbuf, FALSE);
5579 return differ;
5583 * Wipe out a buffer and decrement the last buffer number if it was used for
5584 * this buffer. Call this to wipe out a temp buffer that does not contain any
5585 * marks.
5587 void
5588 wipe_buffer(buf, aucmd)
5589 buf_T *buf;
5590 int aucmd UNUSED; /* When TRUE trigger autocommands. */
5592 if (buf->b_fnum == top_file_num - 1)
5593 --top_file_num;
5595 #ifdef FEAT_AUTOCMD
5596 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
5597 block_autocmds();
5598 #endif
5599 close_buffer(NULL, buf, DOBUF_WIPE);
5600 #ifdef FEAT_AUTOCMD
5601 if (!aucmd)
5602 unblock_autocmds();
5603 #endif