Merge branch 'feat/tagfunc'
[vim_extended.git] / src / buffer.c
blobaa5e3ec88f401f68691395bb5761d17de9e2d2ff
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 /* Change directories when the 'acd' option is set. */
442 DO_AUTOCHDIR
445 * Remove the buffer from the list.
447 if (wipe_buf)
449 #ifdef FEAT_SUN_WORKSHOP
450 if (usingSunWorkShop)
451 workshop_file_closed_lineno((char *)buf->b_ffname,
452 (int)buf->b_last_cursor.lnum);
453 #endif
454 vim_free(buf->b_ffname);
455 vim_free(buf->b_sfname);
456 if (buf->b_prev == NULL)
457 firstbuf = buf->b_next;
458 else
459 buf->b_prev->b_next = buf->b_next;
460 if (buf->b_next == NULL)
461 lastbuf = buf->b_prev;
462 else
463 buf->b_next->b_prev = buf->b_prev;
464 free_buffer(buf);
466 else
468 if (del_buf)
470 /* Free all internal variables and reset option values, to make
471 * ":bdel" compatible with Vim 5.7. */
472 free_buffer_stuff(buf, TRUE);
474 /* Make it look like a new buffer. */
475 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
477 /* Init the options when loaded again. */
478 buf->b_p_initialized = FALSE;
480 buf_clear_file(buf);
481 if (del_buf)
482 buf->b_p_bl = FALSE;
487 * Make buffer not contain a file.
489 void
490 buf_clear_file(buf)
491 buf_T *buf;
493 buf->b_ml.ml_line_count = 1;
494 unchanged(buf, TRUE);
495 #ifndef SHORT_FNAME
496 buf->b_shortname = FALSE;
497 #endif
498 buf->b_p_eol = TRUE;
499 buf->b_start_eol = TRUE;
500 #ifdef FEAT_MBYTE
501 buf->b_p_bomb = FALSE;
502 buf->b_start_bomb = FALSE;
503 #endif
504 buf->b_ml.ml_mfp = NULL;
505 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
506 #ifdef FEAT_NETBEANS_INTG
507 netbeans_deleted_all_lines(buf);
508 #endif
512 * buf_freeall() - free all things allocated for a buffer that are related to
513 * the file.
515 void
516 buf_freeall(buf, del_buf, wipe_buf)
517 buf_T *buf;
518 int del_buf UNUSED; /* buffer is going to be deleted */
519 int wipe_buf UNUSED; /* buffer is going to be wiped out */
521 #ifdef FEAT_AUTOCMD
522 int is_curbuf = (buf == curbuf);
524 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, FALSE, buf);
525 if (!buf_valid(buf)) /* autocommands may delete the buffer */
526 return;
527 if (del_buf && buf->b_p_bl)
529 apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname, FALSE, buf);
530 if (!buf_valid(buf)) /* autocommands may delete the buffer */
531 return;
533 if (wipe_buf)
535 apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
536 FALSE, buf);
537 if (!buf_valid(buf)) /* autocommands may delete the buffer */
538 return;
540 # ifdef FEAT_EVAL
541 if (aborting()) /* autocmds may abort script processing */
542 return;
543 # endif
546 * It's possible that autocommands change curbuf to the one being deleted.
547 * This might cause curbuf to be deleted unexpectedly. But in some cases
548 * it's OK to delete the curbuf, because a new one is obtained anyway.
549 * Therefore only return if curbuf changed to the deleted buffer.
551 if (buf == curbuf && !is_curbuf)
552 return;
553 #endif
554 #ifdef FEAT_DIFF
555 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
556 #endif
558 #ifdef FEAT_FOLDING
559 /* No folds in an empty buffer. */
560 # ifdef FEAT_WINDOWS
562 win_T *win;
563 tabpage_T *tp;
565 FOR_ALL_TAB_WINDOWS(tp, win)
566 if (win->w_buffer == buf)
567 clearFolding(win);
569 # else
570 if (curwin->w_buffer == buf)
571 clearFolding(curwin);
572 # endif
573 #endif
575 #ifdef FEAT_TCL
576 tcl_buffer_free(buf);
577 #endif
578 u_blockfree(buf); /* free the memory allocated for undo */
579 ml_close(buf, TRUE); /* close and delete the memline/memfile */
580 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
581 u_clearall(buf); /* reset all undo information */
582 #ifdef FEAT_SYN_HL
583 syntax_clear(buf); /* reset syntax info */
584 #endif
585 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
589 * Free a buffer structure and the things it contains related to the buffer
590 * itself (not the file, that must have been done already).
592 static void
593 free_buffer(buf)
594 buf_T *buf;
596 free_buffer_stuff(buf, TRUE);
597 #ifdef FEAT_LUA
598 lua_buffer_free(buf);
599 #endif
600 #ifdef FEAT_MZSCHEME
601 mzscheme_buffer_free(buf);
602 #endif
603 #ifdef FEAT_PERL
604 perl_buf_free(buf);
605 #endif
606 #ifdef FEAT_PYTHON
607 python_buffer_free(buf);
608 #endif
609 #ifdef FEAT_RUBY
610 ruby_buffer_free(buf);
611 #endif
612 #ifdef FEAT_AUTOCMD
613 aubuflocal_remove(buf);
614 #endif
615 vim_free(buf);
619 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
621 static void
622 free_buffer_stuff(buf, free_options)
623 buf_T *buf;
624 int free_options; /* free options as well */
626 if (free_options)
628 clear_wininfo(buf); /* including window-local options */
629 free_buf_options(buf, TRUE);
631 #ifdef FEAT_EVAL
632 vars_clear(&buf->b_vars.dv_hashtab); /* free all internal variables */
633 hash_init(&buf->b_vars.dv_hashtab);
634 #endif
635 #ifdef FEAT_USR_CMDS
636 uc_clear(&buf->b_ucmds); /* clear local user commands */
637 #endif
638 #ifdef FEAT_SIGNS
639 buf_delete_signs(buf); /* delete any signs */
640 #endif
641 #ifdef FEAT_NETBEANS_INTG
642 if (usingNetbeans)
643 netbeans_file_killed(buf);
644 #endif
645 #ifdef FEAT_LOCALMAP
646 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
647 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
648 #endif
649 #ifdef FEAT_MBYTE
650 vim_free(buf->b_start_fenc);
651 buf->b_start_fenc = NULL;
652 #endif
653 #ifdef FEAT_SPELL
654 ga_clear(&buf->b_langp);
655 #endif
659 * Free the b_wininfo list for buffer "buf".
661 static void
662 clear_wininfo(buf)
663 buf_T *buf;
665 wininfo_T *wip;
667 while (buf->b_wininfo != NULL)
669 wip = buf->b_wininfo;
670 buf->b_wininfo = wip->wi_next;
671 if (wip->wi_optset)
673 clear_winopt(&wip->wi_opt);
674 #ifdef FEAT_FOLDING
675 deleteFoldRecurse(&wip->wi_folds);
676 #endif
678 vim_free(wip);
682 #if defined(FEAT_LISTCMDS) || defined(PROTO)
684 * Go to another buffer. Handles the result of the ATTENTION dialog.
686 void
687 goto_buffer(eap, start, dir, count)
688 exarg_T *eap;
689 int start;
690 int dir;
691 int count;
693 # if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
694 buf_T *old_curbuf = curbuf;
696 swap_exists_action = SEA_DIALOG;
697 # endif
698 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
699 start, dir, count, eap->forceit);
700 # if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
701 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
703 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
704 cleanup_T cs;
706 /* Reset the error/interrupt/exception state here so that
707 * aborting() returns FALSE when closing a window. */
708 enter_cleanup(&cs);
709 # endif
711 /* Quitting means closing the split window, nothing else. */
712 win_close(curwin, TRUE);
713 swap_exists_action = SEA_NONE;
714 swap_exists_did_quit = TRUE;
716 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
717 /* Restore the error/interrupt/exception state if not discarded by a
718 * new aborting error, interrupt, or uncaught exception. */
719 leave_cleanup(&cs);
720 # endif
722 else
723 handle_swap_exists(old_curbuf);
724 # endif
726 #endif
728 #if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
730 * Handle the situation of swap_exists_action being set.
731 * It is allowed for "old_curbuf" to be NULL or invalid.
733 void
734 handle_swap_exists(old_curbuf)
735 buf_T *old_curbuf;
737 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
738 cleanup_T cs;
739 # endif
741 if (swap_exists_action == SEA_QUIT)
743 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
744 /* Reset the error/interrupt/exception state here so that
745 * aborting() returns FALSE when closing a buffer. */
746 enter_cleanup(&cs);
747 # endif
749 /* User selected Quit at ATTENTION prompt. Go back to previous
750 * buffer. If that buffer is gone or the same as the current one,
751 * open a new, empty buffer. */
752 swap_exists_action = SEA_NONE; /* don't want it again */
753 swap_exists_did_quit = TRUE;
754 close_buffer(curwin, curbuf, DOBUF_UNLOAD);
755 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
756 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
757 if (old_curbuf != NULL)
758 enter_buffer(old_curbuf);
759 /* If "old_curbuf" is NULL we are in big trouble here... */
761 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
762 /* Restore the error/interrupt/exception state if not discarded by a
763 * new aborting error, interrupt, or uncaught exception. */
764 leave_cleanup(&cs);
765 # endif
767 else if (swap_exists_action == SEA_RECOVER)
769 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
770 /* Reset the error/interrupt/exception state here so that
771 * aborting() returns FALSE when closing a buffer. */
772 enter_cleanup(&cs);
773 # endif
775 /* User selected Recover at ATTENTION prompt. */
776 msg_scroll = TRUE;
777 ml_recover();
778 MSG_PUTS("\n"); /* don't overwrite the last message */
779 cmdline_row = msg_row;
780 do_modelines(0);
782 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
783 /* Restore the error/interrupt/exception state if not discarded by a
784 * new aborting error, interrupt, or uncaught exception. */
785 leave_cleanup(&cs);
786 # endif
788 swap_exists_action = SEA_NONE;
790 #endif
792 #if defined(FEAT_LISTCMDS) || defined(PROTO)
794 * do_bufdel() - delete or unload buffer(s)
796 * addr_count == 0: ":bdel" - delete current buffer
797 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
798 * buffer "end_bnr", then any other arguments.
799 * addr_count == 2: ":N,N bdel" - delete buffers in range
801 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
802 * DOBUF_DEL (":bdel")
804 * Returns error message or NULL
806 char_u *
807 do_bufdel(command, arg, addr_count, start_bnr, end_bnr, forceit)
808 int command;
809 char_u *arg; /* pointer to extra arguments */
810 int addr_count;
811 int start_bnr; /* first buffer number in a range */
812 int end_bnr; /* buffer nr or last buffer nr in a range */
813 int forceit;
815 int do_current = 0; /* delete current buffer? */
816 int deleted = 0; /* number of buffers deleted */
817 char_u *errormsg = NULL; /* return value */
818 int bnr; /* buffer number */
819 char_u *p;
821 if (addr_count == 0)
823 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
825 else
827 if (addr_count == 2)
829 if (*arg) /* both range and argument is not allowed */
830 return (char_u *)_(e_trailing);
831 bnr = start_bnr;
833 else /* addr_count == 1 */
834 bnr = end_bnr;
836 for ( ;!got_int; ui_breakcheck())
839 * delete the current buffer last, otherwise when the
840 * current buffer is deleted, the next buffer becomes
841 * the current one and will be loaded, which may then
842 * also be deleted, etc.
844 if (bnr == curbuf->b_fnum)
845 do_current = bnr;
846 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
847 forceit) == OK)
848 ++deleted;
851 * find next buffer number to delete/unload
853 if (addr_count == 2)
855 if (++bnr > end_bnr)
856 break;
858 else /* addr_count == 1 */
860 arg = skipwhite(arg);
861 if (*arg == NUL)
862 break;
863 if (!VIM_ISDIGIT(*arg))
865 p = skiptowhite_esc(arg);
866 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE, FALSE);
867 if (bnr < 0) /* failed */
868 break;
869 arg = p;
871 else
872 bnr = getdigits(&arg);
875 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
876 FORWARD, do_current, forceit) == OK)
877 ++deleted;
879 if (deleted == 0)
881 if (command == DOBUF_UNLOAD)
882 STRCPY(IObuff, _("E515: No buffers were unloaded"));
883 else if (command == DOBUF_DEL)
884 STRCPY(IObuff, _("E516: No buffers were deleted"));
885 else
886 STRCPY(IObuff, _("E517: No buffers were wiped out"));
887 errormsg = IObuff;
889 else if (deleted >= p_report)
891 if (command == DOBUF_UNLOAD)
893 if (deleted == 1)
894 MSG(_("1 buffer unloaded"));
895 else
896 smsg((char_u *)_("%d buffers unloaded"), deleted);
898 else if (command == DOBUF_DEL)
900 if (deleted == 1)
901 MSG(_("1 buffer deleted"));
902 else
903 smsg((char_u *)_("%d buffers deleted"), deleted);
905 else
907 if (deleted == 1)
908 MSG(_("1 buffer wiped out"));
909 else
910 smsg((char_u *)_("%d buffers wiped out"), deleted);
916 return errormsg;
920 * Implementation of the commands for the buffer list.
922 * action == DOBUF_GOTO go to specified buffer
923 * action == DOBUF_SPLIT split window and go to specified buffer
924 * action == DOBUF_UNLOAD unload specified buffer(s)
925 * action == DOBUF_DEL delete specified buffer(s) from buffer list
926 * action == DOBUF_WIPE delete specified buffer(s) really
928 * start == DOBUF_CURRENT go to "count" buffer from current buffer
929 * start == DOBUF_FIRST go to "count" buffer from first buffer
930 * start == DOBUF_LAST go to "count" buffer from last buffer
931 * start == DOBUF_MOD go to "count" modified buffer from current buffer
933 * Return FAIL or OK.
936 do_buffer(action, start, dir, count, forceit)
937 int action;
938 int start;
939 int dir; /* FORWARD or BACKWARD */
940 int count; /* buffer number or number of buffers */
941 int forceit; /* TRUE for :...! */
943 buf_T *buf;
944 buf_T *bp;
945 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
946 || action == DOBUF_WIPE);
948 switch (start)
950 case DOBUF_FIRST: buf = firstbuf; break;
951 case DOBUF_LAST: buf = lastbuf; break;
952 default: buf = curbuf; break;
954 if (start == DOBUF_MOD) /* find next modified buffer */
956 while (count-- > 0)
960 buf = buf->b_next;
961 if (buf == NULL)
962 buf = firstbuf;
964 while (buf != curbuf && !bufIsChanged(buf));
966 if (!bufIsChanged(buf))
968 EMSG(_("E84: No modified buffer found"));
969 return FAIL;
972 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
974 while (buf != NULL && buf->b_fnum != count)
975 buf = buf->b_next;
977 else
979 bp = NULL;
980 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
982 /* remember the buffer where we start, we come back there when all
983 * buffers are unlisted. */
984 if (bp == NULL)
985 bp = buf;
986 if (dir == FORWARD)
988 buf = buf->b_next;
989 if (buf == NULL)
990 buf = firstbuf;
992 else
994 buf = buf->b_prev;
995 if (buf == NULL)
996 buf = lastbuf;
998 /* don't count unlisted buffers */
999 if (unload || buf->b_p_bl)
1001 --count;
1002 bp = NULL; /* use this buffer as new starting point */
1004 if (bp == buf)
1006 /* back where we started, didn't find anything. */
1007 EMSG(_("E85: There is no listed buffer"));
1008 return FAIL;
1013 if (buf == NULL) /* could not find it */
1015 if (start == DOBUF_FIRST)
1017 /* don't warn when deleting */
1018 if (!unload)
1019 EMSGN(_("E86: Buffer %ld does not exist"), count);
1021 else if (dir == FORWARD)
1022 EMSG(_("E87: Cannot go beyond last buffer"));
1023 else
1024 EMSG(_("E88: Cannot go before first buffer"));
1025 return FAIL;
1028 #ifdef FEAT_GUI
1029 need_mouse_correct = TRUE;
1030 #endif
1032 #ifdef FEAT_LISTCMDS
1034 * delete buffer buf from memory and/or the list
1036 if (unload)
1038 int forward;
1039 int retval;
1041 /* When unloading or deleting a buffer that's already unloaded and
1042 * unlisted: fail silently. */
1043 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1044 return FAIL;
1046 if (!forceit && bufIsChanged(buf))
1048 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1049 if ((p_confirm || cmdmod.confirm) && p_write)
1051 dialog_changed(buf, FALSE);
1052 # ifdef FEAT_AUTOCMD
1053 if (!buf_valid(buf))
1054 /* Autocommand deleted buffer, oops! It's not changed
1055 * now. */
1056 return FAIL;
1057 # endif
1058 /* If it's still changed fail silently, the dialog already
1059 * mentioned why it fails. */
1060 if (bufIsChanged(buf))
1061 return FAIL;
1063 else
1064 #endif
1066 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1067 buf->b_fnum);
1068 return FAIL;
1073 * If deleting the last (listed) buffer, make it empty.
1074 * The last (listed) buffer cannot be unloaded.
1076 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1077 if (bp->b_p_bl && bp != buf)
1078 break;
1079 if (bp == NULL && buf == curbuf)
1081 if (action == DOBUF_UNLOAD)
1083 EMSG(_("E90: Cannot unload last buffer"));
1084 return FAIL;
1087 /* Close any other windows on this buffer, then make it empty. */
1088 #ifdef FEAT_WINDOWS
1089 close_windows(buf, TRUE);
1090 #endif
1091 setpcmark();
1092 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1093 forceit ? ECMD_FORCEIT : 0, curwin);
1096 * do_ecmd() may create a new buffer, then we have to delete
1097 * the old one. But do_ecmd() may have done that already, check
1098 * if the buffer still exists.
1100 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
1101 close_buffer(NULL, buf, action);
1102 return retval;
1105 #ifdef FEAT_WINDOWS
1107 * If the deleted buffer is the current one, close the current window
1108 * (unless it's the only window). Repeat this so long as we end up in
1109 * a window with this buffer.
1111 while (buf == curbuf
1112 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
1113 win_close(curwin, FALSE);
1114 #endif
1117 * If the buffer to be deleted is not the current one, delete it here.
1119 if (buf != curbuf)
1121 #ifdef FEAT_WINDOWS
1122 close_windows(buf, FALSE);
1123 #endif
1124 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
1125 close_buffer(NULL, buf, action);
1126 return OK;
1130 * Deleting the current buffer: Need to find another buffer to go to.
1131 * There must be another, otherwise it would have been handled above.
1132 * First use au_new_curbuf, if it is valid.
1133 * Then prefer the buffer we most recently visited.
1134 * Else try to find one that is loaded, after the current buffer,
1135 * then before the current buffer.
1136 * Finally use any buffer.
1138 buf = NULL; /* selected buffer */
1139 bp = NULL; /* used when no loaded buffer found */
1140 #ifdef FEAT_AUTOCMD
1141 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1142 buf = au_new_curbuf;
1143 # ifdef FEAT_JUMPLIST
1144 else
1145 # endif
1146 #endif
1147 #ifdef FEAT_JUMPLIST
1148 if (curwin->w_jumplistlen > 0)
1150 int jumpidx;
1152 jumpidx = curwin->w_jumplistidx - 1;
1153 if (jumpidx < 0)
1154 jumpidx = curwin->w_jumplistlen - 1;
1156 forward = jumpidx;
1157 while (jumpidx != curwin->w_jumplistidx)
1159 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1160 if (buf != NULL)
1162 if (buf == curbuf || !buf->b_p_bl)
1163 buf = NULL; /* skip current and unlisted bufs */
1164 else if (buf->b_ml.ml_mfp == NULL)
1166 /* skip unloaded buf, but may keep it for later */
1167 if (bp == NULL)
1168 bp = buf;
1169 buf = NULL;
1172 if (buf != NULL) /* found a valid buffer: stop searching */
1173 break;
1174 /* advance to older entry in jump list */
1175 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1176 break;
1177 if (--jumpidx < 0)
1178 jumpidx = curwin->w_jumplistlen - 1;
1179 if (jumpidx == forward) /* List exhausted for sure */
1180 break;
1183 #endif
1185 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1187 forward = TRUE;
1188 buf = curbuf->b_next;
1189 for (;;)
1191 if (buf == NULL)
1193 if (!forward) /* tried both directions */
1194 break;
1195 buf = curbuf->b_prev;
1196 forward = FALSE;
1197 continue;
1199 /* in non-help buffer, try to skip help buffers, and vv */
1200 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1202 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1203 break;
1204 if (bp == NULL) /* remember unloaded buf for later */
1205 bp = buf;
1207 if (forward)
1208 buf = buf->b_next;
1209 else
1210 buf = buf->b_prev;
1213 if (buf == NULL) /* No loaded buffer, use unloaded one */
1214 buf = bp;
1215 if (buf == NULL) /* No loaded buffer, find listed one */
1217 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1218 if (buf->b_p_bl && buf != curbuf)
1219 break;
1221 if (buf == NULL) /* Still no buffer, just take one */
1223 if (curbuf->b_next != NULL)
1224 buf = curbuf->b_next;
1225 else
1226 buf = curbuf->b_prev;
1231 * make buf current buffer
1233 if (action == DOBUF_SPLIT) /* split window first */
1235 # ifdef FEAT_WINDOWS
1236 /* If 'switchbuf' contains "useopen": jump to first window containing
1237 * "buf" if one exists */
1238 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
1239 return OK;
1240 /* If 'switchbuf' contains "usetab": jump to first window in any tab
1241 * page containing "buf" if one exists */
1242 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
1243 return OK;
1244 if (win_split(0, 0) == FAIL)
1245 # endif
1246 return FAIL;
1248 #endif
1250 /* go to current buffer - nothing to do */
1251 if (buf == curbuf)
1252 return OK;
1255 * Check if the current buffer may be abandoned.
1257 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1259 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1260 if ((p_confirm || cmdmod.confirm) && p_write)
1262 dialog_changed(curbuf, FALSE);
1263 # ifdef FEAT_AUTOCMD
1264 if (!buf_valid(buf))
1265 /* Autocommand deleted buffer, oops! */
1266 return FAIL;
1267 # endif
1269 if (bufIsChanged(curbuf))
1270 #endif
1272 EMSG(_(e_nowrtmsg));
1273 return FAIL;
1277 /* Go to the other buffer. */
1278 set_curbuf(buf, action);
1280 #if defined(FEAT_LISTCMDS) && defined(FEAT_SCROLLBIND)
1281 if (action == DOBUF_SPLIT)
1282 curwin->w_p_scb = FALSE; /* reset 'scrollbind' */
1283 #endif
1285 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1286 if (aborting()) /* autocmds may abort script processing */
1287 return FAIL;
1288 #endif
1290 return OK;
1293 #endif /* FEAT_LISTCMDS */
1296 * Set current buffer to "buf". Executes autocommands and closes current
1297 * buffer. "action" tells how to close the current buffer:
1298 * DOBUF_GOTO free or hide it
1299 * DOBUF_SPLIT nothing
1300 * DOBUF_UNLOAD unload it
1301 * DOBUF_DEL delete it
1302 * DOBUF_WIPE wipe it out
1304 void
1305 set_curbuf(buf, action)
1306 buf_T *buf;
1307 int action;
1309 buf_T *prevbuf;
1310 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1311 || action == DOBUF_WIPE);
1313 setpcmark();
1314 if (!cmdmod.keepalt)
1315 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
1316 buflist_altfpos(curwin); /* remember curpos */
1318 #ifdef FEAT_VISUAL
1319 /* Don't restart Select mode after switching to another buffer. */
1320 VIsual_reselect = FALSE;
1321 #endif
1323 /* close_windows() or apply_autocmds() may change curbuf */
1324 prevbuf = curbuf;
1326 #ifdef FEAT_AUTOCMD
1327 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1328 # ifdef FEAT_EVAL
1329 if (buf_valid(prevbuf) && !aborting())
1330 # else
1331 if (buf_valid(prevbuf))
1332 # endif
1333 #endif
1335 #ifdef FEAT_WINDOWS
1336 if (unload)
1337 close_windows(prevbuf, FALSE);
1338 #endif
1339 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1340 if (buf_valid(prevbuf) && !aborting())
1341 #else
1342 if (buf_valid(prevbuf))
1343 #endif
1345 if (prevbuf == curbuf)
1346 u_sync(FALSE);
1347 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1348 unload ? action : (action == DOBUF_GOTO
1349 && !P_HID(prevbuf)
1350 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0);
1353 #ifdef FEAT_AUTOCMD
1354 /* An autocommand may have deleted "buf", already entered it (e.g., when
1355 * it did ":bunload") or aborted the script processing! */
1356 # ifdef FEAT_EVAL
1357 if (buf_valid(buf) && buf != curbuf && !aborting())
1358 # else
1359 if (buf_valid(buf) && buf != curbuf)
1360 # endif
1361 #endif
1362 enter_buffer(buf);
1366 * Enter a new current buffer.
1367 * Old curbuf must have been abandoned already!
1369 void
1370 enter_buffer(buf)
1371 buf_T *buf;
1373 /* Copy buffer and window local option values. Not for a help buffer. */
1374 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1375 if (!buf->b_help)
1376 get_winopts(buf);
1377 #ifdef FEAT_FOLDING
1378 else
1379 /* Remove all folds in the window. */
1380 clearFolding(curwin);
1381 foldUpdateAll(curwin); /* update folds (later). */
1382 #endif
1384 /* Get the buffer in the current window. */
1385 curwin->w_buffer = buf;
1386 curbuf = buf;
1387 ++curbuf->b_nwindows;
1389 #ifdef FEAT_DIFF
1390 if (curwin->w_p_diff)
1391 diff_buf_add(curbuf);
1392 #endif
1394 /* Cursor on first line by default. */
1395 curwin->w_cursor.lnum = 1;
1396 curwin->w_cursor.col = 0;
1397 #ifdef FEAT_VIRTUALEDIT
1398 curwin->w_cursor.coladd = 0;
1399 #endif
1400 curwin->w_set_curswant = TRUE;
1401 #ifdef FEAT_AUTOCMD
1402 curwin->w_topline_was_set = FALSE;
1403 #endif
1405 /* mark cursor position as being invalid */
1406 curwin->w_valid = 0;
1408 /* Make sure the buffer is loaded. */
1409 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
1411 #ifdef FEAT_AUTOCMD
1412 /* If there is no filetype, allow for detecting one. Esp. useful for
1413 * ":ball" used in a autocommand. If there already is a filetype we
1414 * might prefer to keep it. */
1415 if (*curbuf->b_p_ft == NUL)
1416 did_filetype = FALSE;
1417 #endif
1419 open_buffer(FALSE, NULL);
1420 #ifdef FEAT_PERSISTENT_UNDO
1421 if (curbuf->b_p_udf)
1422 u_rundo(NULL, 1);
1423 #endif
1425 else
1427 if (!msg_silent)
1428 need_fileinfo = TRUE; /* display file info after redraw */
1429 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1430 #ifdef FEAT_AUTOCMD
1431 curwin->w_topline = 1;
1432 # ifdef FEAT_DIFF
1433 curwin->w_topfill = 0;
1434 # endif
1435 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1436 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1437 #endif
1440 /* If autocommands did not change the cursor position, restore cursor lnum
1441 * and possibly cursor col. */
1442 if (curwin->w_cursor.lnum == 1 && inindent(0))
1443 buflist_getfpos();
1445 check_arg_idx(curwin); /* check for valid arg_idx */
1446 #ifdef FEAT_TITLE
1447 maketitle();
1448 #endif
1449 #ifdef FEAT_AUTOCMD
1450 /* when autocmds didn't change it */
1451 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
1452 #endif
1453 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1455 #ifdef FEAT_NETBEANS_INTG
1456 /* Send fileOpened event because we've changed buffers. */
1457 if (usingNetbeans && isNetbeansBuffer(curbuf))
1458 netbeans_file_activated(curbuf);
1459 #endif
1461 /* Change directories when the 'acd' option is set. */
1462 DO_AUTOCHDIR
1464 #ifdef FEAT_KEYMAP
1465 if (curbuf->b_kmap_state & KEYMAP_INIT)
1466 (void)keymap_init();
1467 #endif
1468 #ifdef FEAT_SPELL
1469 /* May need to set the spell language. Can only do this after the buffer
1470 * has been properly setup. */
1471 if (!curbuf->b_help && curwin->w_p_spell && *curbuf->b_p_spl != NUL)
1472 (void)did_set_spelllang(curbuf);
1473 #endif
1475 redraw_later(NOT_VALID);
1478 #if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1480 * Change to the directory of the current buffer.
1482 void
1483 do_autochdir()
1485 if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK)
1486 shorten_fnames(TRUE);
1488 #endif
1491 * functions for dealing with the buffer list
1495 * Add a file name to the buffer list. Return a pointer to the buffer.
1496 * If the same file name already exists return a pointer to that buffer.
1497 * If it does not exist, or if fname == NULL, a new entry is created.
1498 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1499 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1500 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
1501 * This is the ONLY way to create a new buffer.
1503 static int top_file_num = 1; /* highest file number */
1505 buf_T *
1506 buflist_new(ffname, sfname, lnum, flags)
1507 char_u *ffname; /* full path of fname or relative */
1508 char_u *sfname; /* short fname or NULL */
1509 linenr_T lnum; /* preferred cursor line */
1510 int flags; /* BLN_ defines */
1512 buf_T *buf;
1513 #ifdef UNIX
1514 struct stat st;
1515 #endif
1517 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1520 * If file name already exists in the list, update the entry.
1522 #ifdef UNIX
1523 /* On Unix we can use inode numbers when the file exists. Works better
1524 * for hard links. */
1525 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1526 st.st_dev = (dev_T)-1;
1527 #endif
1528 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1529 #ifdef UNIX
1530 buflist_findname_stat(ffname, &st)
1531 #else
1532 buflist_findname(ffname)
1533 #endif
1534 ) != NULL)
1536 vim_free(ffname);
1537 if (lnum != 0)
1538 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1539 /* copy the options now, if 'cpo' doesn't have 's' and not done
1540 * already */
1541 buf_copy_options(buf, 0);
1542 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1544 buf->b_p_bl = TRUE;
1545 #ifdef FEAT_AUTOCMD
1546 if (!(flags & BLN_DUMMY))
1547 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1548 #endif
1550 return buf;
1554 * If the current buffer has no name and no contents, use the current
1555 * buffer. Otherwise: Need to allocate a new buffer structure.
1557 * This is the ONLY place where a new buffer structure is allocated!
1558 * (A spell file buffer is allocated in spell.c, but that's not a normal
1559 * buffer.)
1561 buf = NULL;
1562 if ((flags & BLN_CURBUF)
1563 && curbuf != NULL
1564 && curbuf->b_ffname == NULL
1565 && curbuf->b_nwindows <= 1
1566 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1568 buf = curbuf;
1569 #ifdef FEAT_AUTOCMD
1570 /* It's like this buffer is deleted. Watch out for autocommands that
1571 * change curbuf! If that happens, allocate a new buffer anyway. */
1572 if (curbuf->b_p_bl)
1573 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1574 if (buf == curbuf)
1575 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1576 # ifdef FEAT_EVAL
1577 if (aborting()) /* autocmds may abort script processing */
1578 return NULL;
1579 # endif
1580 #endif
1581 #ifdef FEAT_QUICKFIX
1582 # ifdef FEAT_AUTOCMD
1583 if (buf == curbuf)
1584 # endif
1586 /* Make sure 'bufhidden' and 'buftype' are empty */
1587 clear_string_option(&buf->b_p_bh);
1588 clear_string_option(&buf->b_p_bt);
1590 #endif
1592 if (buf != curbuf || curbuf == NULL)
1594 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1595 if (buf == NULL)
1597 vim_free(ffname);
1598 return NULL;
1602 if (ffname != NULL)
1604 buf->b_ffname = ffname;
1605 buf->b_sfname = vim_strsave(sfname);
1608 clear_wininfo(buf);
1609 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1611 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1612 || buf->b_wininfo == NULL)
1614 vim_free(buf->b_ffname);
1615 buf->b_ffname = NULL;
1616 vim_free(buf->b_sfname);
1617 buf->b_sfname = NULL;
1618 if (buf != curbuf)
1619 free_buffer(buf);
1620 return NULL;
1623 if (buf == curbuf)
1625 /* free all things allocated for this buffer */
1626 buf_freeall(buf, FALSE, FALSE);
1627 if (buf != curbuf) /* autocommands deleted the buffer! */
1628 return NULL;
1629 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1630 if (aborting()) /* autocmds may abort script processing */
1631 return NULL;
1632 #endif
1633 /* buf->b_nwindows = 0; why was this here? */
1634 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
1635 #ifdef FEAT_KEYMAP
1636 /* need to reload lmaps and set b:keymap_name */
1637 curbuf->b_kmap_state |= KEYMAP_INIT;
1638 #endif
1640 else
1643 * put new buffer at the end of the buffer list
1645 buf->b_next = NULL;
1646 if (firstbuf == NULL) /* buffer list is empty */
1648 buf->b_prev = NULL;
1649 firstbuf = buf;
1651 else /* append new buffer at end of list */
1653 lastbuf->b_next = buf;
1654 buf->b_prev = lastbuf;
1656 lastbuf = buf;
1658 buf->b_fnum = top_file_num++;
1659 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1661 EMSG(_("W14: Warning: List of file names overflow"));
1662 if (emsg_silent == 0)
1664 out_flush();
1665 ui_delay(3000L, TRUE); /* make sure it is noticed */
1667 top_file_num = 1;
1671 * Always copy the options from the current buffer.
1673 buf_copy_options(buf, BCO_ALWAYS);
1676 buf->b_wininfo->wi_fpos.lnum = lnum;
1677 buf->b_wininfo->wi_win = curwin;
1679 #ifdef FEAT_EVAL
1680 init_var_dict(&buf->b_vars, &buf->b_bufvar); /* init b: variables */
1681 #endif
1682 #ifdef FEAT_SYN_HL
1683 hash_init(&buf->b_keywtab);
1684 hash_init(&buf->b_keywtab_ic);
1685 #endif
1687 buf->b_fname = buf->b_sfname;
1688 #ifdef UNIX
1689 if (st.st_dev == (dev_T)-1)
1690 buf->b_dev_valid = FALSE;
1691 else
1693 buf->b_dev_valid = TRUE;
1694 buf->b_dev = st.st_dev;
1695 buf->b_ino = st.st_ino;
1697 #endif
1698 buf->b_u_synced = TRUE;
1699 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
1700 if (flags & BLN_DUMMY)
1701 buf->b_flags |= BF_DUMMY;
1702 buf_clear_file(buf);
1703 clrallmarks(buf); /* clear marks */
1704 fmarks_check_names(buf); /* check file marks for this file */
1705 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1706 #ifdef FEAT_AUTOCMD
1707 if (!(flags & BLN_DUMMY))
1709 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
1710 if (flags & BLN_LISTED)
1711 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1712 # ifdef FEAT_EVAL
1713 if (aborting()) /* autocmds may abort script processing */
1714 return NULL;
1715 # endif
1717 #endif
1719 return buf;
1723 * Free the memory for the options of a buffer.
1724 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1725 * 'fileencoding'.
1727 void
1728 free_buf_options(buf, free_p_ff)
1729 buf_T *buf;
1730 int free_p_ff;
1732 if (free_p_ff)
1734 #ifdef FEAT_MBYTE
1735 clear_string_option(&buf->b_p_fenc);
1736 #endif
1737 clear_string_option(&buf->b_p_ff);
1738 #ifdef FEAT_QUICKFIX
1739 clear_string_option(&buf->b_p_bh);
1740 clear_string_option(&buf->b_p_bt);
1741 #endif
1743 #ifdef FEAT_FIND_ID
1744 clear_string_option(&buf->b_p_def);
1745 clear_string_option(&buf->b_p_inc);
1746 # ifdef FEAT_EVAL
1747 clear_string_option(&buf->b_p_inex);
1748 # endif
1749 #endif
1750 #if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1751 clear_string_option(&buf->b_p_inde);
1752 clear_string_option(&buf->b_p_indk);
1753 #endif
1754 #if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1755 clear_string_option(&buf->b_p_bexpr);
1756 #endif
1757 #if defined(FEAT_EVAL)
1758 clear_string_option(&buf->b_p_fex);
1759 #endif
1760 #ifdef FEAT_CRYPT
1761 clear_string_option(&buf->b_p_key);
1762 #endif
1763 clear_string_option(&buf->b_p_kp);
1764 clear_string_option(&buf->b_p_mps);
1765 clear_string_option(&buf->b_p_fo);
1766 clear_string_option(&buf->b_p_flp);
1767 clear_string_option(&buf->b_p_isk);
1768 #ifdef FEAT_VARTABS
1769 clear_string_option(&buf->b_p_vsts);
1770 if (buf->b_p_vsts_nopaste)
1771 vim_free(buf->b_p_vsts_nopaste);
1772 buf->b_p_vsts_nopaste = 0;
1773 if (buf->b_p_vsts_ary)
1774 vim_free(buf->b_p_vsts_ary);
1775 buf->b_p_vsts_ary = 0;
1776 clear_string_option(&buf->b_p_vts);
1777 if (buf->b_p_vts_ary)
1778 vim_free(buf->b_p_vts_ary);
1779 buf->b_p_vts_ary = 0;
1780 #endif
1781 #ifdef FEAT_KEYMAP
1782 clear_string_option(&buf->b_p_keymap);
1783 ga_clear(&buf->b_kmap_ga);
1784 #endif
1785 #ifdef FEAT_COMMENTS
1786 clear_string_option(&buf->b_p_com);
1787 #endif
1788 #ifdef FEAT_FOLDING
1789 clear_string_option(&buf->b_p_cms);
1790 #endif
1791 clear_string_option(&buf->b_p_nf);
1792 #ifdef FEAT_SYN_HL
1793 clear_string_option(&buf->b_p_syn);
1794 #endif
1795 #ifdef FEAT_SPELL
1796 clear_string_option(&buf->b_p_spc);
1797 clear_string_option(&buf->b_p_spf);
1798 vim_free(buf->b_cap_prog);
1799 buf->b_cap_prog = NULL;
1800 clear_string_option(&buf->b_p_spl);
1801 #endif
1802 #ifdef FEAT_SEARCHPATH
1803 clear_string_option(&buf->b_p_sua);
1804 #endif
1805 #ifdef FEAT_AUTOCMD
1806 clear_string_option(&buf->b_p_ft);
1807 #endif
1808 #ifdef FEAT_OSFILETYPE
1809 clear_string_option(&buf->b_p_oft);
1810 #endif
1811 #ifdef FEAT_CINDENT
1812 clear_string_option(&buf->b_p_cink);
1813 clear_string_option(&buf->b_p_cino);
1814 #endif
1815 #if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1816 clear_string_option(&buf->b_p_cinw);
1817 #endif
1818 #ifdef FEAT_INS_EXPAND
1819 clear_string_option(&buf->b_p_cpt);
1820 #endif
1821 #ifdef FEAT_COMPL_FUNC
1822 clear_string_option(&buf->b_p_cfu);
1823 clear_string_option(&buf->b_p_ofu);
1824 clear_string_option(&buf->b_p_tfu);
1825 #endif
1826 #ifdef FEAT_QUICKFIX
1827 clear_string_option(&buf->b_p_gp);
1828 clear_string_option(&buf->b_p_mp);
1829 clear_string_option(&buf->b_p_efm);
1830 #endif
1831 clear_string_option(&buf->b_p_ep);
1832 clear_string_option(&buf->b_p_path);
1833 clear_string_option(&buf->b_p_tags);
1834 #ifdef FEAT_INS_EXPAND
1835 clear_string_option(&buf->b_p_dict);
1836 clear_string_option(&buf->b_p_tsr);
1837 #endif
1838 #ifdef FEAT_TEXTOBJ
1839 clear_string_option(&buf->b_p_qe);
1840 #endif
1841 buf->b_p_ar = -1;
1845 * get alternate file n
1846 * set linenr to lnum or altfpos.lnum if lnum == 0
1847 * also set cursor column to altfpos.col if 'startofline' is not set.
1848 * if (options & GETF_SETMARK) call setpcmark()
1849 * if (options & GETF_ALT) we are jumping to an alternate file.
1850 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1852 * return FAIL for failure, OK for success
1855 buflist_getfile(n, lnum, options, forceit)
1856 int n;
1857 linenr_T lnum;
1858 int options;
1859 int forceit;
1861 buf_T *buf;
1862 #ifdef FEAT_WINDOWS
1863 win_T *wp = NULL;
1864 #endif
1865 pos_T *fpos;
1866 colnr_T col;
1868 buf = buflist_findnr(n);
1869 if (buf == NULL)
1871 if ((options & GETF_ALT) && n == 0)
1872 EMSG(_(e_noalt));
1873 else
1874 EMSGN(_("E92: Buffer %ld not found"), n);
1875 return FAIL;
1878 /* if alternate file is the current buffer, nothing to do */
1879 if (buf == curbuf)
1880 return OK;
1882 if (text_locked())
1884 text_locked_msg();
1885 return FAIL;
1887 #ifdef FEAT_AUTOCMD
1888 if (curbuf_locked())
1889 return FAIL;
1890 #endif
1892 /* altfpos may be changed by getfile(), get it now */
1893 if (lnum == 0)
1895 fpos = buflist_findfpos(buf);
1896 lnum = fpos->lnum;
1897 col = fpos->col;
1899 else
1900 col = 0;
1902 #ifdef FEAT_WINDOWS
1903 if (options & GETF_SWITCH)
1905 /* If 'switchbuf' contains "useopen": jump to first window containing
1906 * "buf" if one exists */
1907 if (swb_flags & SWB_USEOPEN)
1908 wp = buf_jump_open_win(buf);
1909 /* If 'switchbuf' contians "usetab": jump to first window in any tab
1910 * page containing "buf" if one exists */
1911 if (wp == NULL && (swb_flags & SWB_USETAB))
1912 wp = buf_jump_open_tab(buf);
1913 /* If 'switchbuf' contains "split" or "newtab" and the current buffer
1914 * isn't empty: open new window */
1915 if (wp == NULL && (swb_flags & (SWB_SPLIT | SWB_NEWTAB)) && !bufempty())
1917 if (swb_flags & SWB_NEWTAB) /* Open in a new tab */
1918 tabpage_new();
1919 else if (win_split(0, 0) == FAIL) /* Open in a new window */
1920 return FAIL;
1921 # ifdef FEAT_SCROLLBIND
1922 curwin->w_p_scb = FALSE;
1923 # endif
1926 #endif
1928 ++RedrawingDisabled;
1929 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
1930 lnum, forceit) <= 0)
1932 --RedrawingDisabled;
1934 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
1935 if (!p_sol && col != 0)
1937 curwin->w_cursor.col = col;
1938 check_cursor_col();
1939 #ifdef FEAT_VIRTUALEDIT
1940 curwin->w_cursor.coladd = 0;
1941 #endif
1942 curwin->w_set_curswant = TRUE;
1944 return OK;
1946 --RedrawingDisabled;
1947 return FAIL;
1951 * go to the last know line number for the current buffer
1953 void
1954 buflist_getfpos()
1956 pos_T *fpos;
1958 fpos = buflist_findfpos(curbuf);
1960 curwin->w_cursor.lnum = fpos->lnum;
1961 check_cursor_lnum();
1963 if (p_sol)
1964 curwin->w_cursor.col = 0;
1965 else
1967 curwin->w_cursor.col = fpos->col;
1968 check_cursor_col();
1969 #ifdef FEAT_VIRTUALEDIT
1970 curwin->w_cursor.coladd = 0;
1971 #endif
1972 curwin->w_set_curswant = TRUE;
1976 #if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
1978 * Find file in buffer list by name (it has to be for the current window).
1979 * Returns NULL if not found.
1981 buf_T *
1982 buflist_findname_exp(fname)
1983 char_u *fname;
1985 char_u *ffname;
1986 buf_T *buf = NULL;
1988 /* First make the name into a full path name */
1989 ffname = FullName_save(fname,
1990 #ifdef UNIX
1991 TRUE /* force expansion, get rid of symbolic links */
1992 #else
1993 FALSE
1994 #endif
1996 if (ffname != NULL)
1998 buf = buflist_findname(ffname);
1999 vim_free(ffname);
2001 return buf;
2003 #endif
2006 * Find file in buffer list by name (it has to be for the current window).
2007 * "ffname" must have a full path.
2008 * Skips dummy buffers.
2009 * Returns NULL if not found.
2011 buf_T *
2012 buflist_findname(ffname)
2013 char_u *ffname;
2015 #ifdef UNIX
2016 struct stat st;
2018 if (mch_stat((char *)ffname, &st) < 0)
2019 st.st_dev = (dev_T)-1;
2020 return buflist_findname_stat(ffname, &st);
2024 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2025 * twice for the same file.
2026 * Returns NULL if not found.
2028 static buf_T *
2029 buflist_findname_stat(ffname, stp)
2030 char_u *ffname;
2031 struct stat *stp;
2033 #endif
2034 buf_T *buf;
2036 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2037 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
2038 #ifdef UNIX
2039 , stp
2040 #endif
2042 return buf;
2043 return NULL;
2046 #if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO)
2048 * Find file in buffer list by a regexp pattern.
2049 * Return fnum of the found buffer.
2050 * Return < 0 for error.
2053 buflist_findpat(pattern, pattern_end, unlisted, diffmode)
2054 char_u *pattern;
2055 char_u *pattern_end; /* pointer to first char after pattern */
2056 int unlisted; /* find unlisted buffers */
2057 int diffmode UNUSED; /* find diff-mode buffers only */
2059 buf_T *buf;
2060 regprog_T *prog;
2061 int match = -1;
2062 int find_listed;
2063 char_u *pat;
2064 char_u *patend;
2065 int attempt;
2066 char_u *p;
2067 int toggledollar;
2069 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2071 if (*pattern == '%')
2072 match = curbuf->b_fnum;
2073 else
2074 match = curwin->w_alt_fnum;
2075 #ifdef FEAT_DIFF
2076 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2077 match = -1;
2078 #endif
2082 * Try four ways of matching a listed buffer:
2083 * attempt == 0: without '^' or '$' (at any position)
2084 * attempt == 1: with '^' at start (only at position 0)
2085 * attempt == 2: with '$' at end (only match at end)
2086 * attempt == 3: with '^' at start and '$' at end (only full match)
2087 * Repeat this for finding an unlisted buffer if there was no matching
2088 * listed buffer.
2090 else
2092 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2093 if (pat == NULL)
2094 return -1;
2095 patend = pat + STRLEN(pat) - 1;
2096 toggledollar = (patend > pat && *patend == '$');
2098 /* First try finding a listed buffer. If not found and "unlisted"
2099 * is TRUE, try finding an unlisted buffer. */
2100 find_listed = TRUE;
2101 for (;;)
2103 for (attempt = 0; attempt <= 3; ++attempt)
2105 /* may add '^' and '$' */
2106 if (toggledollar)
2107 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2108 p = pat;
2109 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2110 ++p;
2111 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2112 if (prog == NULL)
2114 vim_free(pat);
2115 return -1;
2118 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2119 if (buf->b_p_bl == find_listed
2120 #ifdef FEAT_DIFF
2121 && (!diffmode || diff_mode_buf(buf))
2122 #endif
2123 && buflist_match(prog, buf) != NULL)
2125 if (match >= 0) /* already found a match */
2127 match = -2;
2128 break;
2130 match = buf->b_fnum; /* remember first match */
2133 vim_free(prog);
2134 if (match >= 0) /* found one match */
2135 break;
2138 /* Only search for unlisted buffers if there was no match with
2139 * a listed buffer. */
2140 if (!unlisted || !find_listed || match != -1)
2141 break;
2142 find_listed = FALSE;
2145 vim_free(pat);
2148 if (match == -2)
2149 EMSG2(_("E93: More than one match for %s"), pattern);
2150 else if (match < 0)
2151 EMSG2(_("E94: No matching buffer for %s"), pattern);
2152 return match;
2154 #endif
2156 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2159 * Find all buffer names that match.
2160 * For command line expansion of ":buf" and ":sbuf".
2161 * Return OK if matches found, FAIL otherwise.
2164 ExpandBufnames(pat, num_file, file, options)
2165 char_u *pat;
2166 int *num_file;
2167 char_u ***file;
2168 int options;
2170 int count = 0;
2171 buf_T *buf;
2172 int round;
2173 char_u *p;
2174 int attempt;
2175 regprog_T *prog;
2176 char_u *patc;
2178 *num_file = 0; /* return values in case of FAIL */
2179 *file = NULL;
2181 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2182 if (*pat == '^')
2184 patc = alloc((unsigned)STRLEN(pat) + 11);
2185 if (patc == NULL)
2186 return FAIL;
2187 STRCPY(patc, "\\(^\\|[\\/]\\)");
2188 STRCPY(patc + 11, pat + 1);
2190 else
2191 patc = pat;
2194 * attempt == 0: try match with '\<', match at start of word
2195 * attempt == 1: try match without '\<', match anywhere
2197 for (attempt = 0; attempt <= 1; ++attempt)
2199 if (attempt > 0 && patc == pat)
2200 break; /* there was no anchor, no need to try again */
2201 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2202 if (prog == NULL)
2204 if (patc != pat)
2205 vim_free(patc);
2206 return FAIL;
2210 * round == 1: Count the matches.
2211 * round == 2: Build the array to keep the matches.
2213 for (round = 1; round <= 2; ++round)
2215 count = 0;
2216 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2218 if (!buf->b_p_bl) /* skip unlisted buffers */
2219 continue;
2220 p = buflist_match(prog, buf);
2221 if (p != NULL)
2223 if (round == 1)
2224 ++count;
2225 else
2227 if (options & WILD_HOME_REPLACE)
2228 p = home_replace_save(buf, p);
2229 else
2230 p = vim_strsave(p);
2231 (*file)[count++] = p;
2235 if (count == 0) /* no match found, break here */
2236 break;
2237 if (round == 1)
2239 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2240 if (*file == NULL)
2242 vim_free(prog);
2243 if (patc != pat)
2244 vim_free(patc);
2245 return FAIL;
2249 vim_free(prog);
2250 if (count) /* match(es) found, break here */
2251 break;
2254 if (patc != pat)
2255 vim_free(patc);
2257 *num_file = count;
2258 return (count == 0 ? FAIL : OK);
2261 #endif /* FEAT_CMDL_COMPL */
2263 #ifdef HAVE_BUFLIST_MATCH
2265 * Check for a match on the file name for buffer "buf" with regprog "prog".
2267 static char_u *
2268 buflist_match(prog, buf)
2269 regprog_T *prog;
2270 buf_T *buf;
2272 char_u *match;
2274 /* First try the short file name, then the long file name. */
2275 match = fname_match(prog, buf->b_sfname);
2276 if (match == NULL)
2277 match = fname_match(prog, buf->b_ffname);
2279 return match;
2283 * Try matching the regexp in "prog" with file name "name".
2284 * Return "name" when there is a match, NULL when not.
2286 static char_u *
2287 fname_match(prog, name)
2288 regprog_T *prog;
2289 char_u *name;
2291 char_u *match = NULL;
2292 char_u *p;
2293 regmatch_T regmatch;
2295 if (name != NULL)
2297 regmatch.regprog = prog;
2298 #ifdef CASE_INSENSITIVE_FILENAME
2299 regmatch.rm_ic = TRUE; /* Always ignore case */
2300 #else
2301 regmatch.rm_ic = FALSE; /* Never ignore case */
2302 #endif
2304 if (vim_regexec(&regmatch, name, (colnr_T)0))
2305 match = name;
2306 else
2308 /* Replace $(HOME) with '~' and try matching again. */
2309 p = home_replace_save(NULL, name);
2310 if (p != NULL && vim_regexec(&regmatch, p, (colnr_T)0))
2311 match = name;
2312 vim_free(p);
2316 return match;
2318 #endif
2321 * find file in buffer list by number
2323 buf_T *
2324 buflist_findnr(nr)
2325 int nr;
2327 buf_T *buf;
2329 if (nr == 0)
2330 nr = curwin->w_alt_fnum;
2331 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2332 if (buf->b_fnum == nr)
2333 return (buf);
2334 return NULL;
2338 * Get name of file 'n' in the buffer list.
2339 * When the file has no name an empty string is returned.
2340 * home_replace() is used to shorten the file name (used for marks).
2341 * Returns a pointer to allocated memory, of NULL when failed.
2343 char_u *
2344 buflist_nr2name(n, fullname, helptail)
2345 int n;
2346 int fullname;
2347 int helptail; /* for help buffers return tail only */
2349 buf_T *buf;
2351 buf = buflist_findnr(n);
2352 if (buf == NULL)
2353 return NULL;
2354 return home_replace_save(helptail ? buf : NULL,
2355 fullname ? buf->b_ffname : buf->b_fname);
2359 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2360 * When "copy_options" is TRUE save the local window option values.
2361 * When "lnum" is 0 only do the options.
2363 static void
2364 buflist_setfpos(buf, win, lnum, col, copy_options)
2365 buf_T *buf;
2366 win_T *win;
2367 linenr_T lnum;
2368 colnr_T col;
2369 int copy_options;
2371 wininfo_T *wip;
2373 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2374 if (wip->wi_win == win)
2375 break;
2376 if (wip == NULL)
2378 /* allocate a new entry */
2379 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2380 if (wip == NULL)
2381 return;
2382 wip->wi_win = win;
2383 if (lnum == 0) /* set lnum even when it's 0 */
2384 lnum = 1;
2386 else
2388 /* remove the entry from the list */
2389 if (wip->wi_prev)
2390 wip->wi_prev->wi_next = wip->wi_next;
2391 else
2392 buf->b_wininfo = wip->wi_next;
2393 if (wip->wi_next)
2394 wip->wi_next->wi_prev = wip->wi_prev;
2395 if (copy_options && wip->wi_optset)
2397 clear_winopt(&wip->wi_opt);
2398 #ifdef FEAT_FOLDING
2399 deleteFoldRecurse(&wip->wi_folds);
2400 #endif
2403 if (lnum != 0)
2405 wip->wi_fpos.lnum = lnum;
2406 wip->wi_fpos.col = col;
2408 if (copy_options)
2410 /* Save the window-specific option values. */
2411 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2412 #ifdef FEAT_FOLDING
2413 wip->wi_fold_manual = win->w_fold_manual;
2414 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2415 #endif
2416 wip->wi_optset = TRUE;
2419 /* insert the entry in front of the list */
2420 wip->wi_next = buf->b_wininfo;
2421 buf->b_wininfo = wip;
2422 wip->wi_prev = NULL;
2423 if (wip->wi_next)
2424 wip->wi_next->wi_prev = wip;
2426 return;
2429 #ifdef FEAT_DIFF
2430 static int wininfo_other_tab_diff __ARGS((wininfo_T *wip));
2433 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2434 * page. That's because a diff is local to a tab page.
2436 static int
2437 wininfo_other_tab_diff(wip)
2438 wininfo_T *wip;
2440 win_T *wp;
2442 if (wip->wi_opt.wo_diff)
2444 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2445 /* return FALSE when it's a window in the current tab page, thus
2446 * the buffer was in diff mode here */
2447 if (wip->wi_win == wp)
2448 return FALSE;
2449 return TRUE;
2451 return FALSE;
2453 #endif
2456 * Find info for the current window in buffer "buf".
2457 * If not found, return the info for the most recently used window.
2458 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2459 * another tab page.
2460 * Returns NULL when there isn't any info.
2462 static wininfo_T *
2463 find_wininfo(buf, skip_diff_buffer)
2464 buf_T *buf;
2465 int skip_diff_buffer UNUSED;
2467 wininfo_T *wip;
2469 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2470 if (wip->wi_win == curwin
2471 #ifdef FEAT_DIFF
2472 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2473 #endif
2475 break;
2477 /* If no wininfo for curwin, use the first in the list (that doesn't have
2478 * 'diff' set and is in another tab page). */
2479 if (wip == NULL)
2481 #ifdef FEAT_DIFF
2482 if (skip_diff_buffer)
2484 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2485 if (!wininfo_other_tab_diff(wip))
2486 break;
2488 else
2489 #endif
2490 wip = buf->b_wininfo;
2492 return wip;
2496 * Reset the local window options to the values last used in this window.
2497 * If the buffer wasn't used in this window before, use the values from
2498 * the most recently used window. If the values were never set, use the
2499 * global values for the window.
2501 void
2502 get_winopts(buf)
2503 buf_T *buf;
2505 wininfo_T *wip;
2507 clear_winopt(&curwin->w_onebuf_opt);
2508 #ifdef FEAT_FOLDING
2509 clearFolding(curwin);
2510 #endif
2512 wip = find_wininfo(buf, TRUE);
2513 if (wip != NULL && wip->wi_optset)
2515 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2516 #ifdef FEAT_FOLDING
2517 curwin->w_fold_manual = wip->wi_fold_manual;
2518 curwin->w_foldinvalid = TRUE;
2519 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2520 #endif
2522 else
2523 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2525 #ifdef FEAT_FOLDING
2526 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2527 if (p_fdls >= 0)
2528 curwin->w_p_fdl = p_fdls;
2529 #endif
2533 * Find the position (lnum and col) for the buffer 'buf' for the current
2534 * window.
2535 * Returns a pointer to no_position if no position is found.
2537 pos_T *
2538 buflist_findfpos(buf)
2539 buf_T *buf;
2541 wininfo_T *wip;
2542 static pos_T no_position = INIT_POS_T(1, 0, 0);
2544 wip = find_wininfo(buf, FALSE);
2545 if (wip != NULL)
2546 return &(wip->wi_fpos);
2547 else
2548 return &no_position;
2552 * Find the lnum for the buffer 'buf' for the current window.
2554 linenr_T
2555 buflist_findlnum(buf)
2556 buf_T *buf;
2558 return buflist_findfpos(buf)->lnum;
2561 #if defined(FEAT_LISTCMDS) || defined(PROTO)
2563 * List all know file names (for :files and :buffers command).
2565 void
2566 buflist_list(eap)
2567 exarg_T *eap;
2569 buf_T *buf;
2570 int len;
2571 int i;
2572 char_u *bname;
2574 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2576 /* skip unlisted buffers, unless ! was used */
2577 if (!buf->b_p_bl && !eap->forceit)
2578 continue;
2579 msg_putchar('\n');
2580 if ((bname = (char_u *)buf_spname(buf)) != NULL)
2582 vim_strncpy(NameBuff, bname, MAXPATHL - 1);
2583 vim_free(bname);
2585 else
2586 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2588 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
2589 buf->b_fnum,
2590 buf->b_p_bl ? ' ' : 'u',
2591 buf == curbuf ? '%' :
2592 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2593 buf->b_ml.ml_mfp == NULL ? ' ' :
2594 (buf->b_nwindows == 0 ? 'h' : 'a'),
2595 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2596 (buf->b_flags & BF_READERR) ? 'x'
2597 : (bufIsChanged(buf) ? '+' : ' '),
2598 NameBuff);
2600 /* put "line 999" in column 40 or after the file name */
2601 i = 40 - vim_strsize(IObuff);
2604 IObuff[len++] = ' ';
2605 } while (--i > 0 && len < IOSIZE - 18);
2606 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2607 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
2608 : (long)buflist_findlnum(buf));
2609 msg_outtrans(IObuff);
2610 out_flush(); /* output one line at a time */
2611 ui_breakcheck();
2614 #endif
2617 * Get file name and line number for file 'fnum'.
2618 * Used by DoOneCmd() for translating '%' and '#'.
2619 * Used by insert_reg() and cmdline_paste() for '#' register.
2620 * Return FAIL if not found, OK for success.
2623 buflist_name_nr(fnum, fname, lnum)
2624 int fnum;
2625 char_u **fname;
2626 linenr_T *lnum;
2628 buf_T *buf;
2630 buf = buflist_findnr(fnum);
2631 if (buf == NULL || buf->b_fname == NULL)
2632 return FAIL;
2634 *fname = buf->b_fname;
2635 *lnum = buflist_findlnum(buf);
2637 return OK;
2641 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2642 * The file name with the full path is also remembered, for when :cd is used.
2643 * Returns FAIL for failure (file name already in use by other buffer)
2644 * OK otherwise.
2647 setfname(buf, ffname, sfname, message)
2648 buf_T *buf;
2649 char_u *ffname, *sfname;
2650 int message; /* give message when buffer already exists */
2652 buf_T *obuf = NULL;
2653 #ifdef UNIX
2654 struct stat st;
2655 #endif
2657 if (ffname == NULL || *ffname == NUL)
2659 /* Removing the name. */
2660 vim_free(buf->b_ffname);
2661 vim_free(buf->b_sfname);
2662 buf->b_ffname = NULL;
2663 buf->b_sfname = NULL;
2664 #ifdef UNIX
2665 st.st_dev = (dev_T)-1;
2666 #endif
2668 else
2670 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2671 if (ffname == NULL) /* out of memory */
2672 return FAIL;
2675 * if the file name is already used in another buffer:
2676 * - if the buffer is loaded, fail
2677 * - if the buffer is not loaded, delete it from the list
2679 #ifdef UNIX
2680 if (mch_stat((char *)ffname, &st) < 0)
2681 st.st_dev = (dev_T)-1;
2682 #endif
2683 if (!(buf->b_flags & BF_DUMMY))
2684 #ifdef UNIX
2685 obuf = buflist_findname_stat(ffname, &st);
2686 #else
2687 obuf = buflist_findname(ffname);
2688 #endif
2689 if (obuf != NULL && obuf != buf)
2691 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2693 if (message)
2694 EMSG(_("E95: Buffer with this name already exists"));
2695 vim_free(ffname);
2696 return FAIL;
2698 close_buffer(NULL, obuf, DOBUF_WIPE); /* delete from the list */
2700 sfname = vim_strsave(sfname);
2701 if (ffname == NULL || sfname == NULL)
2703 vim_free(sfname);
2704 vim_free(ffname);
2705 return FAIL;
2707 #ifdef USE_FNAME_CASE
2708 # ifdef USE_LONG_FNAME
2709 if (USE_LONG_FNAME)
2710 # endif
2711 fname_case(sfname, 0); /* set correct case for short file name */
2712 #endif
2713 vim_free(buf->b_ffname);
2714 vim_free(buf->b_sfname);
2715 buf->b_ffname = ffname;
2716 buf->b_sfname = sfname;
2718 buf->b_fname = buf->b_sfname;
2719 #ifdef UNIX
2720 if (st.st_dev == (dev_T)-1)
2721 buf->b_dev_valid = FALSE;
2722 else
2724 buf->b_dev_valid = TRUE;
2725 buf->b_dev = st.st_dev;
2726 buf->b_ino = st.st_ino;
2728 #endif
2730 #ifndef SHORT_FNAME
2731 buf->b_shortname = FALSE;
2732 #endif
2734 buf_name_changed(buf);
2735 return OK;
2739 * Crude way of changing the name of a buffer. Use with care!
2740 * The name should be relative to the current directory.
2742 void
2743 buf_set_name(fnum, name)
2744 int fnum;
2745 char_u *name;
2747 buf_T *buf;
2749 buf = buflist_findnr(fnum);
2750 if (buf != NULL)
2752 vim_free(buf->b_sfname);
2753 vim_free(buf->b_ffname);
2754 buf->b_ffname = vim_strsave(name);
2755 buf->b_sfname = NULL;
2756 /* Allocate ffname and expand into full path. Also resolves .lnk
2757 * files on Win32. */
2758 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
2759 buf->b_fname = buf->b_sfname;
2764 * Take care of what needs to be done when the name of buffer "buf" has
2765 * changed.
2767 void
2768 buf_name_changed(buf)
2769 buf_T *buf;
2772 * If the file name changed, also change the name of the swapfile
2774 if (buf->b_ml.ml_mfp != NULL)
2775 ml_setname(buf);
2777 if (curwin->w_buffer == buf)
2778 check_arg_idx(curwin); /* check file name for arg list */
2779 #ifdef FEAT_TITLE
2780 maketitle(); /* set window title */
2781 #endif
2782 #ifdef FEAT_WINDOWS
2783 status_redraw_all(); /* status lines need to be redrawn */
2784 #endif
2785 fmarks_check_names(buf); /* check named file marks */
2786 ml_timestamp(buf); /* reset timestamp */
2790 * set alternate file name for current window
2792 * Used by do_one_cmd(), do_write() and do_ecmd().
2793 * Return the buffer.
2795 buf_T *
2796 setaltfname(ffname, sfname, lnum)
2797 char_u *ffname;
2798 char_u *sfname;
2799 linenr_T lnum;
2801 buf_T *buf;
2803 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2804 buf = buflist_new(ffname, sfname, lnum, 0);
2805 if (buf != NULL && !cmdmod.keepalt)
2806 curwin->w_alt_fnum = buf->b_fnum;
2807 return buf;
2811 * Get alternate file name for current window.
2812 * Return NULL if there isn't any, and give error message if requested.
2814 char_u *
2815 getaltfname(errmsg)
2816 int errmsg; /* give error message */
2818 char_u *fname;
2819 linenr_T dummy;
2821 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2823 if (errmsg)
2824 EMSG(_(e_noalt));
2825 return NULL;
2827 return fname;
2831 * Add a file name to the buflist and return its number.
2832 * Uses same flags as buflist_new(), except BLN_DUMMY.
2834 * used by qf_init(), main() and doarglist()
2837 buflist_add(fname, flags)
2838 char_u *fname;
2839 int flags;
2841 buf_T *buf;
2843 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
2844 if (buf != NULL)
2845 return buf->b_fnum;
2846 return 0;
2849 #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2851 * Adjust slashes in file names. Called after 'shellslash' was set.
2853 void
2854 buflist_slash_adjust()
2856 buf_T *bp;
2858 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
2860 if (bp->b_ffname != NULL)
2861 slash_adjust(bp->b_ffname);
2862 if (bp->b_sfname != NULL)
2863 slash_adjust(bp->b_sfname);
2866 #endif
2869 * Set alternate cursor position for the current buffer and window "win".
2870 * Also save the local window option values.
2872 void
2873 buflist_altfpos(win)
2874 win_T *win;
2876 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
2880 * Return TRUE if 'ffname' is not the same file as current file.
2881 * Fname must have a full path (expanded by mch_FullName()).
2884 otherfile(ffname)
2885 char_u *ffname;
2887 return otherfile_buf(curbuf, ffname
2888 #ifdef UNIX
2889 , NULL
2890 #endif
2894 static int
2895 otherfile_buf(buf, ffname
2896 #ifdef UNIX
2897 , stp
2898 #endif
2900 buf_T *buf;
2901 char_u *ffname;
2902 #ifdef UNIX
2903 struct stat *stp;
2904 #endif
2906 /* no name is different */
2907 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
2908 return TRUE;
2909 if (fnamecmp(ffname, buf->b_ffname) == 0)
2910 return FALSE;
2911 #ifdef UNIX
2913 struct stat st;
2915 /* If no struct stat given, get it now */
2916 if (stp == NULL)
2918 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
2919 st.st_dev = (dev_T)-1;
2920 stp = &st;
2922 /* Use dev/ino to check if the files are the same, even when the names
2923 * are different (possible with links). Still need to compare the
2924 * name above, for when the file doesn't exist yet.
2925 * Problem: The dev/ino changes when a file is deleted (and created
2926 * again) and remains the same when renamed/moved. We don't want to
2927 * mch_stat() each buffer each time, that would be too slow. Get the
2928 * dev/ino again when they appear to match, but not when they appear
2929 * to be different: Could skip a buffer when it's actually the same
2930 * file. */
2931 if (buf_same_ino(buf, stp))
2933 buf_setino(buf);
2934 if (buf_same_ino(buf, stp))
2935 return FALSE;
2938 #endif
2939 return TRUE;
2942 #if defined(UNIX) || defined(PROTO)
2944 * Set inode and device number for a buffer.
2945 * Must always be called when b_fname is changed!.
2947 void
2948 buf_setino(buf)
2949 buf_T *buf;
2951 struct stat st;
2953 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
2955 buf->b_dev_valid = TRUE;
2956 buf->b_dev = st.st_dev;
2957 buf->b_ino = st.st_ino;
2959 else
2960 buf->b_dev_valid = FALSE;
2964 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
2966 static int
2967 buf_same_ino(buf, stp)
2968 buf_T *buf;
2969 struct stat *stp;
2971 return (buf->b_dev_valid
2972 && stp->st_dev == buf->b_dev
2973 && stp->st_ino == buf->b_ino);
2975 #endif
2978 * Print info about the current buffer.
2980 void
2981 fileinfo(fullname, shorthelp, dont_truncate)
2982 int fullname; /* when non-zero print full path */
2983 int shorthelp;
2984 int dont_truncate;
2986 char_u *name;
2987 int n;
2988 char_u *p;
2989 char_u *buffer;
2990 size_t len;
2991 char_u *bname;
2993 buffer = alloc(IOSIZE);
2994 if (buffer == NULL)
2995 return;
2997 if (fullname > 1) /* 2 CTRL-G: include buffer number */
2999 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
3000 p = buffer + STRLEN(buffer);
3002 else
3003 p = buffer;
3005 *p++ = '"';
3006 if ((bname = (char_u *)buf_spname(curbuf)) != NULL)
3008 vim_strncpy(p, bname, (size_t)(IOSIZE - (p - buffer) - 1));
3009 vim_free(bname);
3011 else
3013 if (!fullname && curbuf->b_fname != NULL)
3014 name = curbuf->b_fname;
3015 else
3016 name = curbuf->b_ffname;
3017 home_replace(shorthelp ? curbuf : NULL, name, p,
3018 (int)(IOSIZE - (p - buffer)), TRUE);
3021 len = STRLEN(buffer);
3022 vim_snprintf((char *)buffer + len, IOSIZE - len,
3023 "\"%s%s%s%s%s%s",
3024 curbufIsChanged() ? (shortmess(SHM_MOD)
3025 ? " [+]" : _(" [Modified]")) : " ",
3026 (curbuf->b_flags & BF_NOTEDITED)
3027 #ifdef FEAT_QUICKFIX
3028 && !bt_dontwrite(curbuf)
3029 #endif
3030 ? _("[Not edited]") : "",
3031 (curbuf->b_flags & BF_NEW)
3032 #ifdef FEAT_QUICKFIX
3033 && !bt_dontwrite(curbuf)
3034 #endif
3035 ? _("[New file]") : "",
3036 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
3037 curbuf->b_p_ro ? (shortmess(SHM_RO) ? "[RO]"
3038 : _("[readonly]")) : "",
3039 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3040 || curbuf->b_p_ro) ?
3041 " " : "");
3042 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3043 * causes an overflow, thus for large numbers divide instead. */
3044 if (curwin->w_cursor.lnum > 1000000L)
3045 n = (int)(((long)curwin->w_cursor.lnum) /
3046 ((long)curbuf->b_ml.ml_line_count / 100L));
3047 else
3048 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3049 (long)curbuf->b_ml.ml_line_count);
3050 len = STRLEN(buffer);
3051 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3053 vim_snprintf((char *)buffer + len, IOSIZE - len, "%s", _(no_lines_msg));
3055 #ifdef FEAT_CMDL_INFO
3056 else if (p_ru)
3058 /* Current line and column are already on the screen -- webb */
3059 if (curbuf->b_ml.ml_line_count == 1)
3060 vim_snprintf((char *)buffer + len, IOSIZE - len,
3061 _("1 line --%d%%--"), n);
3062 else
3063 vim_snprintf((char *)buffer + len, IOSIZE - len,
3064 _("%ld lines --%d%%--"),
3065 (long)curbuf->b_ml.ml_line_count, n);
3067 #endif
3068 else
3070 vim_snprintf((char *)buffer + len, IOSIZE - len,
3071 _("line %ld of %ld --%d%%-- col "),
3072 (long)curwin->w_cursor.lnum,
3073 (long)curbuf->b_ml.ml_line_count,
3075 validate_virtcol();
3076 len = STRLEN(buffer);
3077 col_print(buffer + len, IOSIZE - len,
3078 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3081 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
3083 if (dont_truncate)
3085 /* Temporarily set msg_scroll to avoid the message being truncated.
3086 * First call msg_start() to get the message in the right place. */
3087 msg_start();
3088 n = msg_scroll;
3089 msg_scroll = TRUE;
3090 msg(buffer);
3091 msg_scroll = n;
3093 else
3095 p = msg_trunc_attr(buffer, FALSE, 0);
3096 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
3097 /* Need to repeat the message after redrawing when:
3098 * - When restart_edit is set (otherwise there will be a delay
3099 * before redrawing).
3100 * - When the screen was scrolled but there is no wait-return
3101 * prompt. */
3102 set_keep_msg(p, 0);
3105 vim_free(buffer);
3108 void
3109 col_print(buf, buflen, col, vcol)
3110 char_u *buf;
3111 size_t buflen;
3112 int col;
3113 int vcol;
3115 if (col == vcol)
3116 vim_snprintf((char *)buf, buflen, "%d", col);
3117 else
3118 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
3121 #if defined(FEAT_TITLE) || defined(PROTO)
3123 * put file name in title bar of window and in icon title
3126 static char_u *lasttitle = NULL;
3127 static char_u *lasticon = NULL;
3129 void
3130 maketitle()
3132 char_u *p;
3133 char_u *t_str = NULL;
3134 char_u *i_name;
3135 char_u *i_str = NULL;
3136 int maxlen = 0;
3137 int len;
3138 int mustset;
3139 char_u buf[IOSIZE];
3140 int off;
3142 if (!redrawing())
3144 /* Postpone updating the title when 'lazyredraw' is set. */
3145 need_maketitle = TRUE;
3146 return;
3149 need_maketitle = FALSE;
3150 if (!p_title && !p_icon)
3151 return;
3153 if (p_title)
3155 if (p_titlelen > 0)
3157 maxlen = p_titlelen * Columns / 100;
3158 if (maxlen < 10)
3159 maxlen = 10;
3162 t_str = buf;
3163 if (*p_titlestring != NUL)
3165 #ifdef FEAT_STL_OPT
3166 if (stl_syntax & STL_IN_TITLE)
3168 int use_sandbox = FALSE;
3169 int save_called_emsg = called_emsg;
3171 # ifdef FEAT_EVAL
3172 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
3173 # endif
3174 called_emsg = FALSE;
3175 build_stl_str_hl(curwin, t_str, sizeof(buf),
3176 p_titlestring, use_sandbox,
3177 0, maxlen, NULL, NULL);
3178 if (called_emsg)
3179 set_string_option_direct((char_u *)"titlestring", -1,
3180 (char_u *)"", OPT_FREE, SID_ERROR);
3181 called_emsg |= save_called_emsg;
3183 else
3184 #endif
3185 t_str = p_titlestring;
3187 else
3189 /* format: "fname + (path) (1 of 2) - VIM" */
3191 if (curbuf->b_fname == NULL)
3192 STRCPY(buf, _("[No Name]"));
3193 else
3195 p = transstr(gettail(curbuf->b_fname));
3196 vim_strncpy(buf, p, IOSIZE - 100);
3197 vim_free(p);
3200 switch (bufIsChanged(curbuf)
3201 + (curbuf->b_p_ro * 2)
3202 + (!curbuf->b_p_ma * 4))
3204 case 1: STRCAT(buf, " +"); break;
3205 case 2: STRCAT(buf, " ="); break;
3206 case 3: STRCAT(buf, " =+"); break;
3207 case 4:
3208 case 6: STRCAT(buf, " -"); break;
3209 case 5:
3210 case 7: STRCAT(buf, " -+"); break;
3213 if (curbuf->b_fname != NULL)
3215 /* Get path of file, replace home dir with ~ */
3216 off = (int)STRLEN(buf);
3217 buf[off++] = ' ';
3218 buf[off++] = '(';
3219 home_replace(curbuf, curbuf->b_ffname,
3220 buf + off, IOSIZE - off, TRUE);
3221 #ifdef BACKSLASH_IN_FILENAME
3222 /* avoid "c:/name" to be reduced to "c" */
3223 if (isalpha(buf[off]) && buf[off + 1] == ':')
3224 off += 2;
3225 #endif
3226 /* remove the file name */
3227 p = gettail_sep(buf + off);
3228 if (p == buf + off)
3229 /* must be a help buffer */
3230 vim_strncpy(buf + off, (char_u *)_("help"),
3231 (size_t)(IOSIZE - off - 1));
3232 else
3233 *p = NUL;
3235 /* translate unprintable chars */
3236 p = transstr(buf + off);
3237 vim_strncpy(buf + off, p, (size_t)(IOSIZE - off - 1));
3238 vim_free(p);
3239 STRCAT(buf, ")");
3242 append_arg_number(curwin, buf, IOSIZE, FALSE);
3244 #if defined(FEAT_CLIENTSERVER)
3245 if (serverName != NULL)
3247 STRCAT(buf, " - ");
3248 STRCAT(buf, serverName);
3250 else
3251 #endif
3252 STRCAT(buf, " - VIM");
3254 if (maxlen > 0)
3256 /* make it shorter by removing a bit in the middle */
3257 len = vim_strsize(buf);
3258 if (len > maxlen)
3259 trunc_string(buf, buf, maxlen);
3263 mustset = ti_change(t_str, &lasttitle);
3265 if (p_icon)
3267 i_str = buf;
3268 if (*p_iconstring != NUL)
3270 #ifdef FEAT_STL_OPT
3271 if (stl_syntax & STL_IN_ICON)
3273 int use_sandbox = FALSE;
3274 int save_called_emsg = called_emsg;
3276 # ifdef FEAT_EVAL
3277 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
3278 # endif
3279 called_emsg = FALSE;
3280 build_stl_str_hl(curwin, i_str, sizeof(buf),
3281 p_iconstring, use_sandbox,
3282 0, 0, NULL, NULL);
3283 if (called_emsg)
3284 set_string_option_direct((char_u *)"iconstring", -1,
3285 (char_u *)"", OPT_FREE, SID_ERROR);
3286 called_emsg |= save_called_emsg;
3288 else
3289 #endif
3290 i_str = p_iconstring;
3292 else
3294 char_u *bname;
3295 if ((bname = (char_u *)buf_spname(curbuf)) != NULL)
3296 i_name = bname;
3297 else /* use file name only in icon */
3298 i_name = gettail(curbuf->b_ffname);
3299 *i_str = NUL;
3300 /* Truncate name at 100 bytes. */
3301 len = (int)STRLEN(i_name);
3302 if (len > 100)
3304 len -= 100;
3305 #ifdef FEAT_MBYTE
3306 if (has_mbyte)
3307 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3308 #endif
3309 i_name += len;
3311 STRCPY(i_str, i_name);
3312 vim_free(bname);
3313 trans_characters(i_str, IOSIZE);
3317 mustset |= ti_change(i_str, &lasticon);
3319 if (mustset)
3320 resettitle();
3324 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3325 * from "str" if it does.
3326 * Return TRUE when "*last" changed.
3328 static int
3329 ti_change(str, last)
3330 char_u *str;
3331 char_u **last;
3333 if ((str == NULL) != (*last == NULL)
3334 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3336 vim_free(*last);
3337 if (str == NULL)
3338 *last = NULL;
3339 else
3340 *last = vim_strsave(str);
3341 return TRUE;
3343 return FALSE;
3347 * Put current window title back (used after calling a shell)
3349 void
3350 resettitle()
3352 mch_settitle(lasttitle, lasticon);
3355 # if defined(EXITFREE) || defined(PROTO)
3356 void
3357 free_titles()
3359 vim_free(lasttitle);
3360 vim_free(lasticon);
3362 # endif
3364 #endif /* FEAT_TITLE */
3366 #if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
3368 * Build a string from the status line items in "fmt".
3369 * Return length of string in screen cells.
3371 * Normally works for window "wp", except when working for 'tabline' then it
3372 * is "curwin".
3374 * Items are drawn interspersed with the text that surrounds it
3375 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3376 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3378 * If maxwidth is not zero, the string will be filled at any middle marker
3379 * or truncated if too long, fillchar is used for all whitespace.
3382 build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar, maxwidth, hltab, tabtab)
3383 win_T *wp;
3384 char_u *out; /* buffer to write into != NameBuff */
3385 size_t outlen; /* length of out[] */
3386 char_u *fmt;
3387 int use_sandbox UNUSED; /* "fmt" was set insecurely, use sandbox */
3388 int fillchar;
3389 int maxwidth;
3390 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3391 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
3393 char_u *p;
3394 char_u *s;
3395 char_u *t;
3396 char_u *linecont;
3397 #ifdef FEAT_EVAL
3398 win_T *o_curwin;
3399 buf_T *o_curbuf;
3400 #endif
3401 int empty_line;
3402 colnr_T virtcol;
3403 long l;
3404 long n;
3405 int prevchar_isflag;
3406 int prevchar_isitem;
3407 int itemisflag;
3408 int fillable;
3409 char_u *str;
3410 long num;
3411 int width;
3412 int itemcnt;
3413 int curitem;
3414 int groupitem[STL_MAX_ITEM];
3415 int groupdepth;
3416 char_u *bname;
3417 struct stl_item
3419 char_u *start;
3420 int minwid;
3421 int maxwid;
3422 enum
3424 Normal,
3425 Empty,
3426 Group,
3427 Middle,
3428 Highlight,
3429 TabPage,
3430 Trunc
3431 } type;
3432 } item[STL_MAX_ITEM];
3433 int minwid;
3434 int maxwid;
3435 int zeropad;
3436 char_u base;
3437 char_u opt;
3438 #define TMPLEN 70
3439 char_u tmp[TMPLEN];
3440 char_u *usefmt = fmt;
3441 struct stl_hlrec *sp;
3443 #ifdef FEAT_EVAL
3445 * When the format starts with "%!" then evaluate it as an expression and
3446 * use the result as the actual format string.
3448 if (fmt[0] == '%' && fmt[1] == '!')
3450 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3451 if (usefmt == NULL)
3452 usefmt = fmt;
3454 #endif
3456 if (fillchar == 0)
3457 fillchar = ' ';
3458 #ifdef FEAT_MBYTE
3459 /* Can't handle a multi-byte fill character yet. */
3460 else if (mb_char2len(fillchar) > 1)
3461 fillchar = '-';
3462 #endif
3465 * Get line & check if empty (cursorpos will show "0-1").
3466 * If inversion is possible we use it. Else '=' characters are used.
3468 linecont = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3469 empty_line = (*linecont == NUL);
3471 groupdepth = 0;
3472 p = out;
3473 curitem = 0;
3474 prevchar_isflag = TRUE;
3475 prevchar_isitem = FALSE;
3476 for (s = usefmt; *s; )
3478 if (*s != NUL && *s != '%')
3479 prevchar_isflag = prevchar_isitem = FALSE;
3482 * Handle up to the next '%' or the end.
3484 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3485 *p++ = *s++;
3486 if (*s == NUL || p + 1 >= out + outlen)
3487 break;
3490 * Handle one '%' item.
3492 s++;
3493 if (*s == '%')
3495 if (p + 1 >= out + outlen)
3496 break;
3497 *p++ = *s++;
3498 prevchar_isflag = prevchar_isitem = FALSE;
3499 continue;
3501 if (*s == STL_MIDDLEMARK)
3503 s++;
3504 if (groupdepth > 0)
3505 continue;
3506 item[curitem].type = Middle;
3507 item[curitem++].start = p;
3508 continue;
3510 if (*s == STL_TRUNCMARK)
3512 s++;
3513 item[curitem].type = Trunc;
3514 item[curitem++].start = p;
3515 continue;
3517 if (*s == ')')
3519 s++;
3520 if (groupdepth < 1)
3521 continue;
3522 groupdepth--;
3524 t = item[groupitem[groupdepth]].start;
3525 *p = NUL;
3526 l = vim_strsize(t);
3527 if (curitem > groupitem[groupdepth] + 1
3528 && item[groupitem[groupdepth]].minwid == 0)
3530 /* remove group if all items are empty */
3531 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3532 if (item[n].type == Normal)
3533 break;
3534 if (n == curitem)
3536 p = t;
3537 l = 0;
3540 if (l > item[groupitem[groupdepth]].maxwid)
3542 /* truncate, remove n bytes of text at the start */
3543 #ifdef FEAT_MBYTE
3544 if (has_mbyte)
3546 /* Find the first character that should be included. */
3547 n = 0;
3548 while (l >= item[groupitem[groupdepth]].maxwid)
3550 l -= ptr2cells(t + n);
3551 n += (*mb_ptr2len)(t + n);
3554 else
3555 #endif
3556 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
3558 *t = '<';
3559 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
3560 p = p - n + 1;
3561 #ifdef FEAT_MBYTE
3562 /* Fill up space left over by half a double-wide char. */
3563 while (++l < item[groupitem[groupdepth]].minwid)
3564 *p++ = fillchar;
3565 #endif
3567 /* correct the start of the items for the truncation */
3568 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3570 item[l].start -= n;
3571 if (item[l].start < t)
3572 item[l].start = t;
3575 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3577 /* fill */
3578 n = item[groupitem[groupdepth]].minwid;
3579 if (n < 0)
3581 /* fill by appending characters */
3582 n = 0 - n;
3583 while (l++ < n && p + 1 < out + outlen)
3584 *p++ = fillchar;
3586 else
3588 /* fill by inserting characters */
3589 mch_memmove(t + n - l, t, (size_t)(p - t));
3590 l = n - l;
3591 if (p + l >= out + outlen)
3592 l = (long)((out + outlen) - p - 1);
3593 p += l;
3594 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3595 item[n].start += l;
3596 for ( ; l > 0; l--)
3597 *t++ = fillchar;
3600 continue;
3602 minwid = 0;
3603 maxwid = 9999;
3604 zeropad = FALSE;
3605 l = 1;
3606 if (*s == '0')
3608 s++;
3609 zeropad = TRUE;
3611 if (*s == '-')
3613 s++;
3614 l = -1;
3616 if (VIM_ISDIGIT(*s))
3618 minwid = (int)getdigits(&s);
3619 if (minwid < 0) /* overflow */
3620 minwid = 0;
3622 if (*s == STL_USER_HL)
3624 item[curitem].type = Highlight;
3625 item[curitem].start = p;
3626 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3627 s++;
3628 curitem++;
3629 continue;
3631 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3633 if (*s == STL_TABCLOSENR)
3635 if (minwid == 0)
3637 /* %X ends the close label, go back to the previously
3638 * define tab label nr. */
3639 for (n = curitem - 1; n >= 0; --n)
3640 if (item[n].type == TabPage && item[n].minwid >= 0)
3642 minwid = item[n].minwid;
3643 break;
3646 else
3647 /* close nrs are stored as negative values */
3648 minwid = - minwid;
3650 item[curitem].type = TabPage;
3651 item[curitem].start = p;
3652 item[curitem].minwid = minwid;
3653 s++;
3654 curitem++;
3655 continue;
3657 if (*s == '.')
3659 s++;
3660 if (VIM_ISDIGIT(*s))
3662 maxwid = (int)getdigits(&s);
3663 if (maxwid <= 0) /* overflow */
3664 maxwid = 50;
3667 minwid = (minwid > 50 ? 50 : minwid) * l;
3668 if (*s == '(')
3670 groupitem[groupdepth++] = curitem;
3671 item[curitem].type = Group;
3672 item[curitem].start = p;
3673 item[curitem].minwid = minwid;
3674 item[curitem].maxwid = maxwid;
3675 s++;
3676 curitem++;
3677 continue;
3679 if (vim_strchr(STL_ALL, *s) == NULL)
3681 s++;
3682 continue;
3684 opt = *s++;
3686 /* OK - now for the real work */
3687 base = 'D';
3688 itemisflag = FALSE;
3689 fillable = TRUE;
3690 num = -1;
3691 str = NULL;
3692 switch (opt)
3694 case STL_FILEPATH:
3695 case STL_FULLPATH:
3696 case STL_FILENAME:
3697 fillable = FALSE; /* don't change ' ' to fillchar */
3698 if ((bname = (char_u *)buf_spname(wp->w_buffer)) != NULL)
3700 vim_strncpy(NameBuff, bname, MAXPATHL - 1);
3701 vim_free(bname);
3703 else
3705 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
3706 : wp->w_buffer->b_fname;
3707 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3709 trans_characters(NameBuff, MAXPATHL);
3710 if (opt != STL_FILENAME)
3711 str = NameBuff;
3712 else
3713 str = gettail(NameBuff);
3714 break;
3716 case STL_VIM_EXPR: /* '{' */
3717 itemisflag = TRUE;
3718 t = p;
3719 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3720 *p++ = *s++;
3721 if (*s != '}') /* missing '}' or out of space */
3722 break;
3723 s++;
3724 *p = 0;
3725 p = t;
3727 #ifdef FEAT_EVAL
3728 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
3729 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3731 o_curbuf = curbuf;
3732 o_curwin = curwin;
3733 curwin = wp;
3734 curbuf = wp->w_buffer;
3736 str = eval_to_string_safe(p, &t, use_sandbox);
3738 curwin = o_curwin;
3739 curbuf = o_curbuf;
3740 do_unlet((char_u *)"g:actual_curbuf", TRUE);
3742 if (str != NULL && *str != 0)
3744 if (*skipdigits(str) == NUL)
3746 num = atoi((char *)str);
3747 vim_free(str);
3748 str = NULL;
3749 itemisflag = FALSE;
3752 #endif
3753 break;
3755 case STL_LINE:
3756 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3757 ? 0L : (long)(wp->w_cursor.lnum);
3758 break;
3760 case STL_NUMLINES:
3761 num = wp->w_buffer->b_ml.ml_line_count;
3762 break;
3764 case STL_COLUMN:
3765 num = !(State & INSERT) && empty_line
3766 ? 0 : (int)wp->w_cursor.col + 1;
3767 break;
3769 case STL_VIRTCOL:
3770 case STL_VIRTCOL_ALT:
3771 /* In list mode virtcol needs to be recomputed */
3772 virtcol = wp->w_virtcol;
3773 if (wp->w_p_list && lcs_tab1 == NUL)
3775 wp->w_p_list = FALSE;
3776 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3777 wp->w_p_list = TRUE;
3779 ++virtcol;
3780 /* Don't display %V if it's the same as %c. */
3781 if (opt == STL_VIRTCOL_ALT
3782 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3783 ? 0 : (int)wp->w_cursor.col + 1)))
3784 break;
3785 num = (long)virtcol;
3786 break;
3788 case STL_PERCENTAGE:
3789 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3790 (long)wp->w_buffer->b_ml.ml_line_count);
3791 break;
3793 case STL_ALTPERCENT:
3794 str = tmp;
3795 get_rel_pos(wp, str, TMPLEN);
3796 break;
3798 case STL_ARGLISTSTAT:
3799 fillable = FALSE;
3800 tmp[0] = 0;
3801 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
3802 str = tmp;
3803 break;
3805 case STL_KEYMAP:
3806 fillable = FALSE;
3807 if (get_keymap_str(wp, tmp, TMPLEN))
3808 str = tmp;
3809 break;
3810 case STL_PAGENUM:
3811 #if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
3812 num = printer_page_num;
3813 #else
3814 num = 0;
3815 #endif
3816 break;
3818 case STL_BUFNO:
3819 num = wp->w_buffer->b_fnum;
3820 break;
3822 case STL_OFFSET_X:
3823 base = 'X';
3824 case STL_OFFSET:
3825 #ifdef FEAT_BYTEOFF
3826 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
3827 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
3828 0L : l + 1 + (!(State & INSERT) && empty_line ?
3829 0 : (int)wp->w_cursor.col);
3830 #endif
3831 break;
3833 case STL_BYTEVAL_X:
3834 base = 'X';
3835 case STL_BYTEVAL:
3836 if (wp->w_cursor.col > (colnr_T)STRLEN(linecont))
3837 num = 0;
3838 else
3840 #ifdef FEAT_MBYTE
3841 num = (*mb_ptr2char)(linecont + wp->w_cursor.col);
3842 #else
3843 num = linecont[wp->w_cursor.col];
3844 #endif
3846 if (num == NL)
3847 num = 0;
3848 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
3849 num = NL;
3850 break;
3852 case STL_ROFLAG:
3853 case STL_ROFLAG_ALT:
3854 itemisflag = TRUE;
3855 if (wp->w_buffer->b_p_ro)
3856 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : "[RO]");
3857 break;
3859 case STL_HELPFLAG:
3860 case STL_HELPFLAG_ALT:
3861 itemisflag = TRUE;
3862 if (wp->w_buffer->b_help)
3863 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
3864 : _("[Help]"));
3865 break;
3867 #ifdef FEAT_AUTOCMD
3868 case STL_FILETYPE:
3869 if (*wp->w_buffer->b_p_ft != NUL
3870 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
3872 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
3873 wp->w_buffer->b_p_ft);
3874 str = tmp;
3876 break;
3878 case STL_FILETYPE_ALT:
3879 itemisflag = TRUE;
3880 if (*wp->w_buffer->b_p_ft != NUL
3881 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
3883 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
3884 wp->w_buffer->b_p_ft);
3885 for (t = tmp; *t != 0; t++)
3886 *t = TOUPPER_LOC(*t);
3887 str = tmp;
3889 break;
3890 #endif
3892 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3893 case STL_PREVIEWFLAG:
3894 case STL_PREVIEWFLAG_ALT:
3895 itemisflag = TRUE;
3896 if (wp->w_p_pvw)
3897 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
3898 : _("[Preview]"));
3899 break;
3900 #endif
3902 case STL_MODIFIED:
3903 case STL_MODIFIED_ALT:
3904 itemisflag = TRUE;
3905 switch ((opt == STL_MODIFIED_ALT)
3906 + bufIsChanged(wp->w_buffer) * 2
3907 + (!wp->w_buffer->b_p_ma) * 4)
3909 case 2: str = (char_u *)"[+]"; break;
3910 case 3: str = (char_u *)",+"; break;
3911 case 4: str = (char_u *)"[-]"; break;
3912 case 5: str = (char_u *)",-"; break;
3913 case 6: str = (char_u *)"[+-]"; break;
3914 case 7: str = (char_u *)",+-"; break;
3916 break;
3918 case STL_HIGHLIGHT:
3919 t = s;
3920 while (*s != '#' && *s != NUL)
3921 ++s;
3922 if (*s == '#')
3924 item[curitem].type = Highlight;
3925 item[curitem].start = p;
3926 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
3927 curitem++;
3929 ++s;
3930 continue;
3933 item[curitem].start = p;
3934 item[curitem].type = Normal;
3935 if (str != NULL && *str)
3937 t = str;
3938 if (itemisflag)
3940 if ((t[0] && t[1])
3941 && ((!prevchar_isitem && *t == ',')
3942 || (prevchar_isflag && *t == ' ')))
3943 t++;
3944 prevchar_isflag = TRUE;
3946 l = vim_strsize(t);
3947 if (l > 0)
3948 prevchar_isitem = TRUE;
3949 if (l > maxwid)
3951 while (l >= maxwid)
3952 #ifdef FEAT_MBYTE
3953 if (has_mbyte)
3955 l -= ptr2cells(t);
3956 t += (*mb_ptr2len)(t);
3958 else
3959 #endif
3960 l -= byte2cells(*t++);
3961 if (p + 1 >= out + outlen)
3962 break;
3963 *p++ = '<';
3965 if (minwid > 0)
3967 for (; l < minwid && p + 1 < out + outlen; l++)
3969 /* Don't put a "-" in front of a digit. */
3970 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
3971 *p++ = ' ';
3972 else
3973 *p++ = fillchar;
3975 minwid = 0;
3977 else
3978 minwid *= -1;
3979 while (*t && p + 1 < out + outlen)
3981 *p++ = *t++;
3982 /* Change a space by fillchar, unless fillchar is '-' and a
3983 * digit follows. */
3984 if (fillable && p[-1] == ' '
3985 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
3986 p[-1] = fillchar;
3988 for (; l < minwid && p + 1 < out + outlen; l++)
3989 *p++ = fillchar;
3991 else if (num >= 0)
3993 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
3994 char_u nstr[20];
3996 if (p + 20 >= out + outlen)
3997 break; /* not sufficient space */
3998 prevchar_isitem = TRUE;
3999 t = nstr;
4000 if (opt == STL_VIRTCOL_ALT)
4002 *t++ = '-';
4003 minwid--;
4005 *t++ = '%';
4006 if (zeropad)
4007 *t++ = '0';
4008 *t++ = '*';
4009 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
4010 *t = 0;
4012 for (n = num, l = 1; n >= nbase; n /= nbase)
4013 l++;
4014 if (opt == STL_VIRTCOL_ALT)
4015 l++;
4016 if (l > maxwid)
4018 l += 2;
4019 n = l - maxwid;
4020 while (l-- > maxwid)
4021 num /= nbase;
4022 *t++ = '>';
4023 *t++ = '%';
4024 *t = t[-3];
4025 *++t = 0;
4026 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4027 0, num, n);
4029 else
4030 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4031 minwid, num);
4032 p += STRLEN(p);
4034 else
4035 item[curitem].type = Empty;
4037 if (opt == STL_VIM_EXPR)
4038 vim_free(str);
4040 if (num >= 0 || (!itemisflag && str && *str))
4041 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4042 curitem++;
4044 *p = NUL;
4045 itemcnt = curitem;
4047 #ifdef FEAT_EVAL
4048 if (usefmt != fmt)
4049 vim_free(usefmt);
4050 #endif
4052 width = vim_strsize(out);
4053 if (maxwidth > 0 && width > maxwidth)
4055 /* Result is too long, must truncate somewhere. */
4056 l = 0;
4057 if (itemcnt == 0)
4058 s = out;
4059 else
4061 for ( ; l < itemcnt; l++)
4062 if (item[l].type == Trunc)
4064 /* Truncate at %< item. */
4065 s = item[l].start;
4066 break;
4068 if (l == itemcnt)
4070 /* No %< item, truncate first item. */
4071 s = item[0].start;
4072 l = 0;
4076 if (width - vim_strsize(s) >= maxwidth)
4078 /* Truncation mark is beyond max length */
4079 #ifdef FEAT_MBYTE
4080 if (has_mbyte)
4082 s = out;
4083 width = 0;
4084 for (;;)
4086 width += ptr2cells(s);
4087 if (width >= maxwidth)
4088 break;
4089 s += (*mb_ptr2len)(s);
4091 /* Fill up for half a double-wide character. */
4092 while (++width < maxwidth)
4093 *s++ = fillchar;
4095 else
4096 #endif
4097 s = out + maxwidth - 1;
4098 for (l = 0; l < itemcnt; l++)
4099 if (item[l].start > s)
4100 break;
4101 itemcnt = l;
4102 *s++ = '>';
4103 *s = 0;
4105 else
4107 #ifdef FEAT_MBYTE
4108 if (has_mbyte)
4110 n = 0;
4111 while (width >= maxwidth)
4113 width -= ptr2cells(s + n);
4114 n += (*mb_ptr2len)(s + n);
4117 else
4118 #endif
4119 n = width - maxwidth + 1;
4120 p = s + n;
4121 STRMOVE(s + 1, p);
4122 *s = '<';
4124 /* Fill up for half a double-wide character. */
4125 while (++width < maxwidth)
4127 s = s + STRLEN(s);
4128 *s++ = fillchar;
4129 *s = NUL;
4132 --n; /* count the '<' */
4133 for (; l < itemcnt; l++)
4135 if (item[l].start - n >= s)
4136 item[l].start -= n;
4137 else
4138 item[l].start = s;
4141 width = maxwidth;
4143 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4145 /* Apply STL_MIDDLE if any */
4146 for (l = 0; l < itemcnt; l++)
4147 if (item[l].type == Middle)
4148 break;
4149 if (l < itemcnt)
4151 p = item[l].start + maxwidth - width;
4152 STRMOVE(p, item[l].start);
4153 for (s = item[l].start; s < p; s++)
4154 *s = fillchar;
4155 for (l++; l < itemcnt; l++)
4156 item[l].start += maxwidth - width;
4157 width = maxwidth;
4161 /* Store the info about highlighting. */
4162 if (hltab != NULL)
4164 sp = hltab;
4165 for (l = 0; l < itemcnt; l++)
4167 if (item[l].type == Highlight)
4169 sp->start = item[l].start;
4170 sp->userhl = item[l].minwid;
4171 sp++;
4174 sp->start = NULL;
4175 sp->userhl = 0;
4178 /* Store the info about tab pages labels. */
4179 if (tabtab != NULL)
4181 sp = tabtab;
4182 for (l = 0; l < itemcnt; l++)
4184 if (item[l].type == TabPage)
4186 sp->start = item[l].start;
4187 sp->userhl = item[l].minwid;
4188 sp++;
4191 sp->start = NULL;
4192 sp->userhl = 0;
4195 return width;
4197 #endif /* FEAT_STL_OPT */
4199 #if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4200 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
4202 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4203 * using "Top", "Bot" or "All" when appropriate.
4205 void
4206 get_rel_pos(wp, buf, buflen)
4207 win_T *wp;
4208 char_u *buf;
4209 int buflen;
4211 long above; /* number of lines above window */
4212 long below; /* number of lines below window */
4214 above = wp->w_topline - 1;
4215 #ifdef FEAT_DIFF
4216 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
4217 #endif
4218 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4219 if (below <= 0)
4220 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4221 (size_t)(buflen - 1));
4222 else if (above <= 0)
4223 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
4224 else
4225 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
4226 ? (int)(above / ((above + below) / 100L))
4227 : (int)(above * 100L / (above + below)));
4229 #endif
4232 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
4233 * Return TRUE if it was appended.
4235 static int
4236 append_arg_number(wp, buf, buflen, add_file)
4237 win_T *wp;
4238 char_u *buf;
4239 int buflen;
4240 int add_file; /* Add "file" before the arg number */
4242 char_u *p;
4244 if (ARGCOUNT <= 1) /* nothing to do */
4245 return FALSE;
4247 p = buf + STRLEN(buf); /* go to the end of the buffer */
4248 if (p - buf + 35 >= buflen) /* getting too long */
4249 return FALSE;
4250 *p++ = ' ';
4251 *p++ = '(';
4252 if (add_file)
4254 STRCPY(p, "file ");
4255 p += 5;
4257 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4258 wp->w_arg_idx_invalid ? "(%d) of %d)"
4259 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4260 return TRUE;
4264 * If fname is not a full path, make it a full path.
4265 * Returns pointer to allocated memory (NULL for failure).
4267 char_u *
4268 fix_fname(fname)
4269 char_u *fname;
4272 * Force expanding the path always for Unix, because symbolic links may
4273 * mess up the full path name, even though it starts with a '/'.
4274 * Also expand when there is ".." in the file name, try to remove it,
4275 * because "c:/src/../README" is equal to "c:/README".
4276 * Similarly "c:/src//file" is equal to "c:/src/file".
4277 * For MS-Windows also expand names like "longna~1" to "longname".
4279 #ifdef UNIX
4280 return FullName_save(fname, TRUE);
4281 #else
4282 if (!vim_isAbsName(fname)
4283 || strstr((char *)fname, "..") != NULL
4284 || strstr((char *)fname, "//") != NULL
4285 # ifdef BACKSLASH_IN_FILENAME
4286 || strstr((char *)fname, "\\\\") != NULL
4287 # endif
4288 # if defined(MSWIN) || defined(DJGPP)
4289 || vim_strchr(fname, '~') != NULL
4290 # endif
4292 return FullName_save(fname, FALSE);
4294 fname = vim_strsave(fname);
4296 # ifdef USE_FNAME_CASE
4297 # ifdef USE_LONG_FNAME
4298 if (USE_LONG_FNAME)
4299 # endif
4301 if (fname != NULL)
4302 fname_case(fname, 0); /* set correct case for file name */
4304 # endif
4306 return fname;
4307 #endif
4311 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4312 * "ffname" becomes a pointer to allocated memory (or NULL).
4314 void
4315 fname_expand(buf, ffname, sfname)
4316 buf_T *buf UNUSED;
4317 char_u **ffname;
4318 char_u **sfname;
4320 if (*ffname == NULL) /* if no file name given, nothing to do */
4321 return;
4322 if (*sfname == NULL) /* if no short file name given, use ffname */
4323 *sfname = *ffname;
4324 *ffname = fix_fname(*ffname); /* expand to full path */
4326 #ifdef FEAT_SHORTCUT
4327 if (!buf->b_p_bin)
4329 char_u *rfname;
4331 /* If the file name is a shortcut file, use the file it links to. */
4332 rfname = mch_resolve_shortcut(*ffname);
4333 if (rfname != NULL)
4335 vim_free(*ffname);
4336 *ffname = rfname;
4337 *sfname = rfname;
4340 #endif
4344 * Get the file name for an argument list entry.
4346 char_u *
4347 alist_name(aep)
4348 aentry_T *aep;
4350 buf_T *bp;
4352 /* Use the name from the associated buffer if it exists. */
4353 bp = buflist_findnr(aep->ae_fnum);
4354 if (bp == NULL || bp->b_fname == NULL)
4355 return aep->ae_fname;
4356 return bp->b_fname;
4359 #if defined(FEAT_WINDOWS) || defined(PROTO)
4361 * do_arg_all(): Open up to 'count' windows, one for each argument.
4363 void
4364 do_arg_all(count, forceit, keep_tabs)
4365 int count;
4366 int forceit; /* hide buffers in current windows */
4367 int keep_tabs; /* keep current tabs, for ":tab drop file" */
4369 int i;
4370 win_T *wp, *wpnext;
4371 char_u *opened; /* array of flags for which args are open */
4372 int opened_len; /* length of opened[] */
4373 int use_firstwin = FALSE; /* use first window for arglist */
4374 int split_ret = OK;
4375 int p_ea_save;
4376 alist_T *alist; /* argument list to be used */
4377 buf_T *buf;
4378 tabpage_T *tpnext;
4379 int had_tab = cmdmod.tab;
4380 win_T *new_curwin = NULL;
4381 tabpage_T *new_curtab = NULL;
4383 if (ARGCOUNT <= 0)
4385 /* Don't give an error message. We don't want it when the ":all"
4386 * command is in the .vimrc. */
4387 return;
4389 setpcmark();
4391 opened_len = ARGCOUNT;
4392 opened = alloc_clear((unsigned)opened_len);
4393 if (opened == NULL)
4394 return;
4396 #ifdef FEAT_GUI
4397 need_mouse_correct = TRUE;
4398 #endif
4401 * Try closing all windows that are not in the argument list.
4402 * Also close windows that are not full width;
4403 * When 'hidden' or "forceit" set the buffer becomes hidden.
4404 * Windows that have a changed buffer and can't be hidden won't be closed.
4405 * When the ":tab" modifier was used do this for all tab pages.
4407 if (had_tab > 0)
4408 goto_tabpage_tp(first_tabpage);
4409 for (;;)
4411 tpnext = curtab->tp_next;
4412 for (wp = firstwin; wp != NULL; wp = wpnext)
4414 wpnext = wp->w_next;
4415 buf = wp->w_buffer;
4416 if (buf->b_ffname == NULL
4417 || buf->b_nwindows > 1
4418 #ifdef FEAT_VERTSPLIT
4419 || wp->w_width != Columns
4420 #endif
4422 i = ARGCOUNT;
4423 else
4425 /* check if the buffer in this window is in the arglist */
4426 for (i = 0; i < ARGCOUNT; ++i)
4428 if (ARGLIST[i].ae_fnum == buf->b_fnum
4429 || fullpathcmp(alist_name(&ARGLIST[i]),
4430 buf->b_ffname, TRUE) & FPC_SAME)
4432 if (i < opened_len)
4434 opened[i] = TRUE;
4435 if (i == 0)
4437 new_curwin = wp;
4438 new_curtab = curtab;
4441 if (wp->w_alist != curwin->w_alist)
4443 /* Use the current argument list for all windows
4444 * containing a file from it. */
4445 alist_unlink(wp->w_alist);
4446 wp->w_alist = curwin->w_alist;
4447 ++wp->w_alist->al_refcount;
4449 break;
4453 wp->w_arg_idx = i;
4455 if (i == ARGCOUNT && !keep_tabs) /* close this window */
4457 if (P_HID(buf) || forceit || buf->b_nwindows > 1
4458 || !bufIsChanged(buf))
4460 /* If the buffer was changed, and we would like to hide it,
4461 * try autowriting. */
4462 if (!P_HID(buf) && buf->b_nwindows <= 1
4463 && bufIsChanged(buf))
4465 (void)autowrite(buf, FALSE);
4466 #ifdef FEAT_AUTOCMD
4467 /* check if autocommands removed the window */
4468 if (!win_valid(wp) || !buf_valid(buf))
4470 wpnext = firstwin; /* start all over... */
4471 continue;
4473 #endif
4475 #ifdef FEAT_WINDOWS
4476 /* don't close last window */
4477 if (firstwin == lastwin && first_tabpage->tp_next == NULL)
4478 #endif
4479 use_firstwin = TRUE;
4480 #ifdef FEAT_WINDOWS
4481 else
4483 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4484 # ifdef FEAT_AUTOCMD
4485 /* check if autocommands removed the next window */
4486 if (!win_valid(wpnext))
4487 wpnext = firstwin; /* start all over... */
4488 # endif
4490 #endif
4495 /* Without the ":tab" modifier only do the current tab page. */
4496 if (had_tab == 0 || tpnext == NULL)
4497 break;
4499 # ifdef FEAT_AUTOCMD
4500 /* check if autocommands removed the next tab page */
4501 if (!valid_tabpage(tpnext))
4502 tpnext = first_tabpage; /* start all over...*/
4503 # endif
4504 goto_tabpage_tp(tpnext);
4508 * Open a window for files in the argument list that don't have one.
4509 * ARGCOUNT may change while doing this, because of autocommands.
4511 if (count > ARGCOUNT || count <= 0)
4512 count = ARGCOUNT;
4514 /* Autocommands may do anything to the argument list. Make sure it's not
4515 * freed while we are working here by "locking" it. We still have to
4516 * watch out for its size to be changed. */
4517 alist = curwin->w_alist;
4518 ++alist->al_refcount;
4520 #ifdef FEAT_AUTOCMD
4521 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4522 ++autocmd_no_enter;
4523 ++autocmd_no_leave;
4524 #endif
4525 win_enter(lastwin, FALSE);
4526 #ifdef FEAT_WINDOWS
4527 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4528 * leaving an empty tab page when executed locally. */
4529 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4530 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4531 use_firstwin = TRUE;
4532 #endif
4534 for (i = 0; i < count && i < alist->al_ga.ga_len && !got_int; ++i)
4536 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4537 arg_had_last = TRUE;
4538 if (i < opened_len && opened[i])
4540 /* Move the already present window to below the current window */
4541 if (curwin->w_arg_idx != i)
4543 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4545 if (wpnext->w_arg_idx == i)
4547 win_move_after(wpnext, curwin);
4548 break;
4553 else if (split_ret == OK)
4555 if (!use_firstwin) /* split current window */
4557 p_ea_save = p_ea;
4558 p_ea = TRUE; /* use space from all windows */
4559 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4560 p_ea = p_ea_save;
4561 if (split_ret == FAIL)
4562 continue;
4564 #ifdef FEAT_AUTOCMD
4565 else /* first window: do autocmd for leaving this buffer */
4566 --autocmd_no_leave;
4567 #endif
4570 * edit file "i"
4572 curwin->w_arg_idx = i;
4573 if (i == 0)
4575 new_curwin = curwin;
4576 new_curtab = curtab;
4578 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4579 ECMD_ONE,
4580 ((P_HID(curwin->w_buffer)
4581 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
4582 + ECMD_OLDBUF, curwin);
4583 #ifdef FEAT_AUTOCMD
4584 if (use_firstwin)
4585 ++autocmd_no_leave;
4586 #endif
4587 use_firstwin = FALSE;
4589 ui_breakcheck();
4591 /* When ":tab" was used open a new tab for a new window repeatedly. */
4592 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4593 cmdmod.tab = 9999;
4596 /* Remove the "lock" on the argument list. */
4597 alist_unlink(alist);
4599 #ifdef FEAT_AUTOCMD
4600 --autocmd_no_enter;
4601 #endif
4602 /* to window with first arg */
4603 if (valid_tabpage(new_curtab))
4604 goto_tabpage_tp(new_curtab);
4605 if (win_valid(new_curwin))
4606 win_enter(new_curwin, FALSE);
4608 #ifdef FEAT_AUTOCMD
4609 --autocmd_no_leave;
4610 #endif
4611 vim_free(opened);
4614 # if defined(FEAT_LISTCMDS) || defined(PROTO)
4616 * Open a window for a number of buffers.
4618 void
4619 ex_buffer_all(eap)
4620 exarg_T *eap;
4622 buf_T *buf;
4623 win_T *wp, *wpnext;
4624 int split_ret = OK;
4625 int p_ea_save;
4626 int open_wins = 0;
4627 int r;
4628 int count; /* Maximum number of windows to open. */
4629 int all; /* When TRUE also load inactive buffers. */
4630 #ifdef FEAT_WINDOWS
4631 int had_tab = cmdmod.tab;
4632 tabpage_T *tpnext;
4633 #endif
4635 if (eap->addr_count == 0) /* make as many windows as possible */
4636 count = 9999;
4637 else
4638 count = eap->line2; /* make as many windows as specified */
4639 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4640 all = FALSE;
4641 else
4642 all = TRUE;
4644 setpcmark();
4646 #ifdef FEAT_GUI
4647 need_mouse_correct = TRUE;
4648 #endif
4651 * Close superfluous windows (two windows for the same buffer).
4652 * Also close windows that are not full-width.
4654 #ifdef FEAT_WINDOWS
4655 if (had_tab > 0)
4656 goto_tabpage_tp(first_tabpage);
4657 for (;;)
4659 #endif
4660 tpnext = curtab->tp_next;
4661 for (wp = firstwin; wp != NULL; wp = wpnext)
4663 wpnext = wp->w_next;
4664 if ((wp->w_buffer->b_nwindows > 1
4665 #ifdef FEAT_VERTSPLIT
4666 || ((cmdmod.split & WSP_VERT)
4667 ? wp->w_height + wp->w_status_height < Rows - p_ch
4668 - tabline_height()
4669 : wp->w_width != Columns)
4670 #endif
4671 #ifdef FEAT_WINDOWS
4672 || (had_tab > 0 && wp != firstwin)
4673 #endif
4674 ) && firstwin != lastwin)
4676 win_close(wp, FALSE);
4677 #ifdef FEAT_AUTOCMD
4678 wpnext = firstwin; /* just in case an autocommand does
4679 something strange with windows */
4680 tpnext = first_tabpage; /* start all over...*/
4681 open_wins = 0;
4682 #endif
4684 else
4685 ++open_wins;
4688 #ifdef FEAT_WINDOWS
4689 /* Without the ":tab" modifier only do the current tab page. */
4690 if (had_tab == 0 || tpnext == NULL)
4691 break;
4692 goto_tabpage_tp(tpnext);
4694 #endif
4697 * Go through the buffer list. When a buffer doesn't have a window yet,
4698 * open one. Otherwise move the window to the right position.
4699 * Watch out for autocommands that delete buffers or windows!
4701 #ifdef FEAT_AUTOCMD
4702 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4703 ++autocmd_no_enter;
4704 #endif
4705 win_enter(lastwin, FALSE);
4706 #ifdef FEAT_AUTOCMD
4707 ++autocmd_no_leave;
4708 #endif
4709 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4711 /* Check if this buffer needs a window */
4712 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4713 continue;
4715 #ifdef FEAT_WINDOWS
4716 if (had_tab != 0)
4718 /* With the ":tab" modifier don't move the window. */
4719 if (buf->b_nwindows > 0)
4720 wp = lastwin; /* buffer has a window, skip it */
4721 else
4722 wp = NULL;
4724 else
4725 #endif
4727 /* Check if this buffer already has a window */
4728 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4729 if (wp->w_buffer == buf)
4730 break;
4731 /* If the buffer already has a window, move it */
4732 if (wp != NULL)
4733 win_move_after(wp, curwin);
4736 if (wp == NULL && split_ret == OK)
4738 /* Split the window and put the buffer in it */
4739 p_ea_save = p_ea;
4740 p_ea = TRUE; /* use space from all windows */
4741 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4742 ++open_wins;
4743 p_ea = p_ea_save;
4744 if (split_ret == FAIL)
4745 continue;
4747 /* Open the buffer in this window. */
4748 #if defined(HAS_SWAP_EXISTS_ACTION)
4749 swap_exists_action = SEA_DIALOG;
4750 #endif
4751 set_curbuf(buf, DOBUF_GOTO);
4752 #ifdef FEAT_AUTOCMD
4753 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
4755 #if defined(HAS_SWAP_EXISTS_ACTION)
4756 swap_exists_action = SEA_NONE;
4757 # endif
4758 break;
4760 #endif
4761 #if defined(HAS_SWAP_EXISTS_ACTION)
4762 if (swap_exists_action == SEA_QUIT)
4764 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4765 cleanup_T cs;
4767 /* Reset the error/interrupt/exception state here so that
4768 * aborting() returns FALSE when closing a window. */
4769 enter_cleanup(&cs);
4770 # endif
4772 /* User selected Quit at ATTENTION prompt; close this window. */
4773 win_close(curwin, TRUE);
4774 --open_wins;
4775 swap_exists_action = SEA_NONE;
4776 swap_exists_did_quit = TRUE;
4778 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4779 /* Restore the error/interrupt/exception state if not
4780 * discarded by a new aborting error, interrupt, or uncaught
4781 * exception. */
4782 leave_cleanup(&cs);
4783 # endif
4785 else
4786 handle_swap_exists(NULL);
4787 #endif
4790 ui_breakcheck();
4791 if (got_int)
4793 (void)vgetc(); /* only break the file loading, not the rest */
4794 break;
4796 #ifdef FEAT_EVAL
4797 /* Autocommands deleted the buffer or aborted script processing!!! */
4798 if (aborting())
4799 break;
4800 #endif
4801 #ifdef FEAT_WINDOWS
4802 /* When ":tab" was used open a new tab for a new window repeatedly. */
4803 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4804 cmdmod.tab = 9999;
4805 #endif
4807 #ifdef FEAT_AUTOCMD
4808 --autocmd_no_enter;
4809 #endif
4810 win_enter(firstwin, FALSE); /* back to first window */
4811 #ifdef FEAT_AUTOCMD
4812 --autocmd_no_leave;
4813 #endif
4816 * Close superfluous windows.
4818 for (wp = lastwin; open_wins > count; )
4820 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
4821 || autowrite(wp->w_buffer, FALSE) == OK);
4822 #ifdef FEAT_AUTOCMD
4823 if (!win_valid(wp))
4825 /* BufWrite Autocommands made the window invalid, start over */
4826 wp = lastwin;
4828 else
4829 #endif
4830 if (r)
4832 win_close(wp, !P_HID(wp->w_buffer));
4833 --open_wins;
4834 wp = lastwin;
4836 else
4838 wp = wp->w_prev;
4839 if (wp == NULL)
4840 break;
4844 # endif /* FEAT_LISTCMDS */
4846 #endif /* FEAT_WINDOWS */
4848 static int chk_modeline __ARGS((linenr_T, int));
4851 * do_modelines() - process mode lines for the current file
4853 * "flags" can be:
4854 * OPT_WINONLY only set options local to window
4855 * OPT_NOWIN don't set options local to window
4857 * Returns immediately if the "ml" option isn't set.
4859 void
4860 do_modelines(flags)
4861 int flags;
4863 linenr_T lnum;
4864 int nmlines;
4865 static int entered = 0;
4867 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
4868 return;
4870 /* Disallow recursive entry here. Can happen when executing a modeline
4871 * triggers an autocommand, which reloads modelines with a ":do". */
4872 if (entered)
4873 return;
4875 ++entered;
4876 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
4877 ++lnum)
4878 if (chk_modeline(lnum, flags) == FAIL)
4879 nmlines = 0;
4881 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
4882 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
4883 if (chk_modeline(lnum, flags) == FAIL)
4884 nmlines = 0;
4885 --entered;
4888 #include "version.h" /* for version number */
4891 * chk_modeline() - check a single line for a mode string
4892 * Return FAIL if an error encountered.
4894 static int
4895 chk_modeline(lnum, flags)
4896 linenr_T lnum;
4897 int flags; /* Same as for do_modelines(). */
4899 char_u *s;
4900 char_u *e;
4901 char_u *linecopy; /* local copy of any modeline found */
4902 int prev;
4903 int vers;
4904 int end;
4905 int retval = OK;
4906 char_u *save_sourcing_name;
4907 linenr_T save_sourcing_lnum;
4908 #ifdef FEAT_EVAL
4909 scid_T save_SID;
4910 #endif
4912 prev = -1;
4913 for (s = ml_get(lnum); *s != NUL; ++s)
4915 if (prev == -1 || vim_isspace(prev))
4917 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
4918 || STRNCMP(s, "vi:", (size_t)3) == 0)
4919 break;
4920 if (STRNCMP(s, "vim", 3) == 0)
4922 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
4923 e = s + 4;
4924 else
4925 e = s + 3;
4926 vers = getdigits(&e);
4927 if (*e == ':'
4928 && (s[3] == ':'
4929 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
4930 || (VIM_VERSION_100 < vers && s[3] == '<')
4931 || (VIM_VERSION_100 > vers && s[3] == '>')
4932 || (VIM_VERSION_100 == vers && s[3] == '=')))
4933 break;
4936 prev = *s;
4939 if (*s)
4941 do /* skip over "ex:", "vi:" or "vim:" */
4942 ++s;
4943 while (s[-1] != ':');
4945 s = linecopy = vim_strsave(s); /* copy the line, it will change */
4946 if (linecopy == NULL)
4947 return FAIL;
4949 save_sourcing_lnum = sourcing_lnum;
4950 save_sourcing_name = sourcing_name;
4951 sourcing_lnum = lnum; /* prepare for emsg() */
4952 sourcing_name = (char_u *)"modelines";
4954 end = FALSE;
4955 while (end == FALSE)
4957 s = skipwhite(s);
4958 if (*s == NUL)
4959 break;
4962 * Find end of set command: ':' or end of line.
4963 * Skip over "\:", replacing it with ":".
4965 for (e = s; *e != ':' && *e != NUL; ++e)
4966 if (e[0] == '\\' && e[1] == ':')
4967 STRMOVE(e, e + 1);
4968 if (*e == NUL)
4969 end = TRUE;
4972 * If there is a "set" command, require a terminating ':' and
4973 * ignore the stuff after the ':'.
4974 * "vi:set opt opt opt: foo" -- foo not interpreted
4975 * "vi:opt opt opt: foo" -- foo interpreted
4976 * Accept "se" for compatibility with Elvis.
4978 if (STRNCMP(s, "set ", (size_t)4) == 0
4979 || STRNCMP(s, "se ", (size_t)3) == 0)
4981 if (*e != ':') /* no terminating ':'? */
4982 break;
4983 end = TRUE;
4984 s = vim_strchr(s, ' ') + 1;
4986 *e = NUL; /* truncate the set command */
4988 if (*s != NUL) /* skip over an empty "::" */
4990 #ifdef FEAT_EVAL
4991 save_SID = current_SID;
4992 current_SID = SID_MODELINE;
4993 #endif
4994 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
4995 #ifdef FEAT_EVAL
4996 current_SID = save_SID;
4997 #endif
4998 if (retval == FAIL) /* stop if error found */
4999 break;
5001 s = e + 1; /* advance to next part */
5004 sourcing_lnum = save_sourcing_lnum;
5005 sourcing_name = save_sourcing_name;
5007 vim_free(linecopy);
5009 return retval;
5012 #if defined(FEAT_VIMINFO) || defined(PROTO)
5014 read_viminfo_bufferlist(virp, writing)
5015 vir_T *virp;
5016 int writing;
5018 char_u *tab;
5019 linenr_T lnum;
5020 colnr_T col;
5021 buf_T *buf;
5022 char_u *sfname;
5023 char_u *xline;
5025 /* Handle long line and escaped characters. */
5026 xline = viminfo_readstring(virp, 1, FALSE);
5028 /* don't read in if there are files on the command-line or if writing: */
5029 if (xline != NULL && !writing && ARGCOUNT == 0
5030 && find_viminfo_parameter('%') != NULL)
5032 /* Format is: <fname> Tab <lnum> Tab <col>.
5033 * Watch out for a Tab in the file name, work from the end. */
5034 lnum = 0;
5035 col = 0;
5036 tab = vim_strrchr(xline, '\t');
5037 if (tab != NULL)
5039 *tab++ = '\0';
5040 col = (colnr_T)atoi((char *)tab);
5041 tab = vim_strrchr(xline, '\t');
5042 if (tab != NULL)
5044 *tab++ = '\0';
5045 lnum = atol((char *)tab);
5049 /* Expand "~/" in the file name at "line + 1" to a full path.
5050 * Then try shortening it by comparing with the current directory */
5051 expand_env(xline, NameBuff, MAXPATHL);
5052 sfname = shorten_fname1(NameBuff);
5054 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5055 if (buf != NULL) /* just in case... */
5057 buf->b_last_cursor.lnum = lnum;
5058 buf->b_last_cursor.col = col;
5059 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5062 vim_free(xline);
5064 return viminfo_readline(virp);
5067 void
5068 write_viminfo_bufferlist(fp)
5069 FILE *fp;
5071 buf_T *buf;
5072 #ifdef FEAT_WINDOWS
5073 win_T *win;
5074 tabpage_T *tp;
5075 #endif
5076 char_u *line;
5077 int max_buffers;
5078 size_t len;
5080 if (find_viminfo_parameter('%') == NULL)
5081 return;
5083 /* Without a number -1 is returned: do all buffers. */
5084 max_buffers = get_viminfo_parameter('%');
5086 /* Allocate room for the file name, lnum and col. */
5087 #define LINE_BUF_LEN (MAXPATHL + 40)
5088 line = alloc(LINE_BUF_LEN);
5089 if (line == NULL)
5090 return;
5092 #ifdef FEAT_WINDOWS
5093 FOR_ALL_TAB_WINDOWS(tp, win)
5094 set_last_cursor(win);
5095 #else
5096 set_last_cursor(curwin);
5097 #endif
5099 fprintf(fp, _("\n# Buffer list:\n"));
5100 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5102 if (buf->b_fname == NULL
5103 || !buf->b_p_bl
5104 #ifdef FEAT_QUICKFIX
5105 || bt_quickfix(buf)
5106 #endif
5107 || removable(buf->b_ffname))
5108 continue;
5110 if (max_buffers-- == 0)
5111 break;
5112 putc('%', fp);
5113 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
5114 len = STRLEN(line);
5115 vim_snprintf((char *)line + len, len - LINE_BUF_LEN, "\t%ld\t%d",
5116 (long)buf->b_last_cursor.lnum,
5117 buf->b_last_cursor.col);
5118 viminfo_writestring(fp, line);
5120 vim_free(line);
5122 #endif
5126 * Return special buffer name.
5127 * Returns NULL when the buffer has a normal file name.
5129 char *
5130 buf_spname(buf)
5131 buf_T *buf;
5133 #if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5134 if (bt_quickfix(buf))
5136 char *qlname;
5137 const char *bt_name;
5138 size_t len;
5139 char *ret_ptr;
5140 win_T *win = NULL;
5141 tabpage_T *tp;
5144 * For location list window, w_llist_ref points to the location list.
5145 * For quickfix window, w_llist_ref is NULL.
5147 FOR_ALL_TAB_WINDOWS(tp, win)
5148 if (win->w_buffer == buf)
5149 goto win_found;
5150 win_found:
5151 if (win != NULL && win->w_llist_ref != NULL)
5153 qlname = qf_get_name(win->w_llist_ref);
5154 bt_name = _("[Location List]");
5156 else
5158 qlname = qf_get_name(NULL);
5159 bt_name = _("[Quickfix List]");
5162 len = STRLEN(bt_name);
5163 if (qlname)
5165 len += STRLEN(qlname);
5166 ret_ptr = (char *)alloc(len + 2);
5167 sprintf(ret_ptr, "%s %s", bt_name,
5168 qlname);
5170 else
5172 ret_ptr = (char *)alloc(len + 1);
5173 sprintf(ret_ptr, "%s", bt_name);
5175 return ret_ptr;
5177 #endif
5178 #ifdef FEAT_QUICKFIX
5179 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5180 * contains the name as specified by the user */
5181 if (bt_nofile(buf))
5183 if (buf->b_sfname != NULL)
5184 return (char *)vim_strsave(buf->b_sfname);
5185 return (char *)vim_strsave((char_u *)_("[Scratch]"));
5187 #endif
5188 if (buf->b_fname == NULL)
5189 return (char *)vim_strsave((char_u *)_("[No Name]"));
5190 return NULL;
5194 #if defined(FEAT_SIGNS) || defined(PROTO)
5196 * Insert the sign into the signlist.
5198 static void
5199 insert_sign(buf, prev, next, id, lnum, typenr)
5200 buf_T *buf; /* buffer to store sign in */
5201 signlist_T *prev; /* previous sign entry */
5202 signlist_T *next; /* next sign entry */
5203 int id; /* sign ID */
5204 linenr_T lnum; /* line number which gets the mark */
5205 int typenr; /* typenr of sign we are adding */
5207 signlist_T *newsign;
5209 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5210 if (newsign != NULL)
5212 newsign->id = id;
5213 newsign->lnum = lnum;
5214 newsign->typenr = typenr;
5215 newsign->next = next;
5216 #ifdef FEAT_NETBEANS_INTG
5217 newsign->prev = prev;
5218 if (next != NULL)
5219 next->prev = newsign;
5220 #endif
5222 if (prev == NULL)
5224 /* When adding first sign need to redraw the windows to create the
5225 * column for signs. */
5226 if (buf->b_signlist == NULL)
5228 redraw_buf_later(buf, NOT_VALID);
5229 changed_cline_bef_curs();
5232 /* first sign in signlist */
5233 buf->b_signlist = newsign;
5235 else
5236 prev->next = newsign;
5241 * Add the sign into the signlist. Find the right spot to do it though.
5243 void
5244 buf_addsign(buf, id, lnum, typenr)
5245 buf_T *buf; /* buffer to store sign in */
5246 int id; /* sign ID */
5247 linenr_T lnum; /* line number which gets the mark */
5248 int typenr; /* typenr of sign we are adding */
5250 signlist_T *sign; /* a sign in the signlist */
5251 signlist_T *prev; /* the previous sign */
5253 prev = NULL;
5254 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5256 if (lnum == sign->lnum && id == sign->id)
5258 sign->typenr = typenr;
5259 return;
5261 else if (
5262 #ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5263 id < 0 &&
5264 #endif
5265 lnum < sign->lnum)
5267 #ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5268 /* XXX - GRP: Is this because of sign slide problem? Or is it
5269 * really needed? Or is it because we allow multiple signs per
5270 * line? If so, should I add that feature to FEAT_SIGNS?
5272 while (prev != NULL && prev->lnum == lnum)
5273 prev = prev->prev;
5274 if (prev == NULL)
5275 sign = buf->b_signlist;
5276 else
5277 sign = prev->next;
5278 #endif
5279 insert_sign(buf, prev, sign, id, lnum, typenr);
5280 return;
5282 prev = sign;
5284 #ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5285 /* XXX - GRP: See previous comment */
5286 while (prev != NULL && prev->lnum == lnum)
5287 prev = prev->prev;
5288 if (prev == NULL)
5289 sign = buf->b_signlist;
5290 else
5291 sign = prev->next;
5292 #endif
5293 insert_sign(buf, prev, sign, id, lnum, typenr);
5295 return;
5298 linenr_T
5299 buf_change_sign_type(buf, markId, typenr)
5300 buf_T *buf; /* buffer to store sign in */
5301 int markId; /* sign ID */
5302 int typenr; /* typenr of sign we are adding */
5304 signlist_T *sign; /* a sign in the signlist */
5306 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5308 if (sign->id == markId)
5310 sign->typenr = typenr;
5311 return sign->lnum;
5315 return (linenr_T)0;
5319 buf_getsigntype(buf, lnum, type)
5320 buf_T *buf;
5321 linenr_T lnum;
5322 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5324 signlist_T *sign; /* a sign in a b_signlist */
5326 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5327 if (sign->lnum == lnum
5328 && (type == SIGN_ANY
5329 # ifdef FEAT_SIGN_ICONS
5330 || (type == SIGN_ICON
5331 && sign_get_image(sign->typenr) != NULL)
5332 # endif
5333 || (type == SIGN_TEXT
5334 && sign_get_text(sign->typenr) != NULL)
5335 || (type == SIGN_LINEHL
5336 && sign_get_attr(sign->typenr, TRUE) != 0)))
5337 return sign->typenr;
5338 return 0;
5342 linenr_T
5343 buf_delsign(buf, id)
5344 buf_T *buf; /* buffer sign is stored in */
5345 int id; /* sign id */
5347 signlist_T **lastp; /* pointer to pointer to current sign */
5348 signlist_T *sign; /* a sign in a b_signlist */
5349 signlist_T *next; /* the next sign in a b_signlist */
5350 linenr_T lnum; /* line number whose sign was deleted */
5352 lastp = &buf->b_signlist;
5353 lnum = 0;
5354 for (sign = buf->b_signlist; sign != NULL; sign = next)
5356 next = sign->next;
5357 if (sign->id == id)
5359 *lastp = next;
5360 #ifdef FEAT_NETBEANS_INTG
5361 if (next != NULL)
5362 next->prev = sign->prev;
5363 #endif
5364 lnum = sign->lnum;
5365 vim_free(sign);
5366 break;
5368 else
5369 lastp = &sign->next;
5372 /* When deleted the last sign need to redraw the windows to remove the
5373 * sign column. */
5374 if (buf->b_signlist == NULL)
5376 redraw_buf_later(buf, NOT_VALID);
5377 changed_cline_bef_curs();
5380 return lnum;
5385 * Find the line number of the sign with the requested id. If the sign does
5386 * not exist, return 0 as the line number. This will still let the correct file
5387 * get loaded.
5390 buf_findsign(buf, id)
5391 buf_T *buf; /* buffer to store sign in */
5392 int id; /* sign ID */
5394 signlist_T *sign; /* a sign in the signlist */
5396 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5397 if (sign->id == id)
5398 return sign->lnum;
5400 return 0;
5404 buf_findsign_id(buf, lnum)
5405 buf_T *buf; /* buffer whose sign we are searching for */
5406 linenr_T lnum; /* line number of sign */
5408 signlist_T *sign; /* a sign in the signlist */
5410 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5411 if (sign->lnum == lnum)
5412 return sign->id;
5414 return 0;
5418 # if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5419 /* see if a given type of sign exists on a specific line */
5421 buf_findsigntype_id(buf, lnum, typenr)
5422 buf_T *buf; /* buffer whose sign we are searching for */
5423 linenr_T lnum; /* line number of sign */
5424 int typenr; /* sign type number */
5426 signlist_T *sign; /* a sign in the signlist */
5428 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5429 if (sign->lnum == lnum && sign->typenr == typenr)
5430 return sign->id;
5432 return 0;
5436 # if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5437 /* return the number of icons on the given line */
5439 buf_signcount(buf, lnum)
5440 buf_T *buf;
5441 linenr_T lnum;
5443 signlist_T *sign; /* a sign in the signlist */
5444 int count = 0;
5446 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5447 if (sign->lnum == lnum)
5448 if (sign_get_image(sign->typenr) != NULL)
5449 count++;
5451 return count;
5453 # endif /* FEAT_SIGN_ICONS */
5454 # endif /* FEAT_NETBEANS_INTG */
5458 * Delete signs in buffer "buf".
5460 static void
5461 buf_delete_signs(buf)
5462 buf_T *buf;
5464 signlist_T *next;
5466 while (buf->b_signlist != NULL)
5468 next = buf->b_signlist->next;
5469 vim_free(buf->b_signlist);
5470 buf->b_signlist = next;
5475 * Delete all signs in all buffers.
5477 void
5478 buf_delete_all_signs()
5480 buf_T *buf; /* buffer we are checking for signs */
5482 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5483 if (buf->b_signlist != NULL)
5485 /* Need to redraw the windows to remove the sign column. */
5486 redraw_buf_later(buf, NOT_VALID);
5487 buf_delete_signs(buf);
5492 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5494 void
5495 sign_list_placed(rbuf)
5496 buf_T *rbuf;
5498 buf_T *buf;
5499 signlist_T *p;
5500 char lbuf[BUFSIZ];
5502 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5503 msg_putchar('\n');
5504 if (rbuf == NULL)
5505 buf = firstbuf;
5506 else
5507 buf = rbuf;
5508 while (buf != NULL)
5510 if (buf->b_signlist != NULL)
5512 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
5513 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5514 msg_putchar('\n');
5516 for (p = buf->b_signlist; p != NULL; p = p->next)
5518 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
5519 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5520 MSG_PUTS(lbuf);
5521 msg_putchar('\n');
5523 if (rbuf != NULL)
5524 break;
5525 buf = buf->b_next;
5530 * Adjust a placed sign for inserted/deleted lines.
5532 void
5533 sign_mark_adjust(line1, line2, amount, amount_after)
5534 linenr_T line1;
5535 linenr_T line2;
5536 long amount;
5537 long amount_after;
5539 signlist_T *sign; /* a sign in a b_signlist */
5541 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5543 if (sign->lnum >= line1 && sign->lnum <= line2)
5545 if (amount == MAXLNUM)
5546 sign->lnum = line1;
5547 else
5548 sign->lnum += amount;
5550 else if (sign->lnum > line2)
5551 sign->lnum += amount_after;
5554 #endif /* FEAT_SIGNS */
5557 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5559 void
5560 set_buflisted(on)
5561 int on;
5563 if (on != curbuf->b_p_bl)
5565 curbuf->b_p_bl = on;
5566 #ifdef FEAT_AUTOCMD
5567 if (on)
5568 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5569 else
5570 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5571 #endif
5576 * Read the file for "buf" again and check if the contents changed.
5577 * Return TRUE if it changed or this could not be checked.
5580 buf_contents_changed(buf)
5581 buf_T *buf;
5583 buf_T *newbuf;
5584 int differ = TRUE;
5585 linenr_T lnum;
5586 aco_save_T aco;
5587 exarg_T ea;
5589 /* Allocate a buffer without putting it in the buffer list. */
5590 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5591 if (newbuf == NULL)
5592 return TRUE;
5594 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5595 if (prep_exarg(&ea, buf) == FAIL)
5597 wipe_buffer(newbuf, FALSE);
5598 return TRUE;
5601 /* set curwin/curbuf to buf and save a few things */
5602 aucmd_prepbuf(&aco, newbuf);
5604 if (ml_open(curbuf) == OK
5605 && readfile(buf->b_ffname, buf->b_fname,
5606 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5607 &ea, READ_NEW | READ_DUMMY) == OK)
5609 /* compare the two files line by line */
5610 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5612 differ = FALSE;
5613 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5614 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5616 differ = TRUE;
5617 break;
5621 vim_free(ea.cmd);
5623 /* restore curwin/curbuf and a few other things */
5624 aucmd_restbuf(&aco);
5626 if (curbuf != newbuf) /* safety check */
5627 wipe_buffer(newbuf, FALSE);
5629 return differ;
5633 * Wipe out a buffer and decrement the last buffer number if it was used for
5634 * this buffer. Call this to wipe out a temp buffer that does not contain any
5635 * marks.
5637 void
5638 wipe_buffer(buf, aucmd)
5639 buf_T *buf;
5640 int aucmd UNUSED; /* When TRUE trigger autocommands. */
5642 if (buf->b_fnum == top_file_num - 1)
5643 --top_file_num;
5645 #ifdef FEAT_AUTOCMD
5646 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
5647 block_autocmds();
5648 #endif
5649 close_buffer(NULL, buf, DOBUF_WIPE);
5650 #ifdef FEAT_AUTOCMD
5651 if (!aucmd)
5652 unblock_autocmds();
5653 #endif