Merge branch 'vim-with-runtime' into feat/var-tabstops
[vim_extended.git] / src / buffer.c
blob4eebe8efc1986730f9aacc4b3b774265a16a1580
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_MZSCHEME
598 mzscheme_buffer_free(buf);
599 #endif
600 #ifdef FEAT_PERL
601 perl_buf_free(buf);
602 #endif
603 #ifdef FEAT_PYTHON
604 python_buffer_free(buf);
605 #endif
606 #ifdef FEAT_RUBY
607 ruby_buffer_free(buf);
608 #endif
609 #ifdef FEAT_AUTOCMD
610 aubuflocal_remove(buf);
611 #endif
612 vim_free(buf);
616 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
618 static void
619 free_buffer_stuff(buf, free_options)
620 buf_T *buf;
621 int free_options; /* free options as well */
623 if (free_options)
625 clear_wininfo(buf); /* including window-local options */
626 free_buf_options(buf, TRUE);
628 #ifdef FEAT_EVAL
629 vars_clear(&buf->b_vars.dv_hashtab); /* free all internal variables */
630 hash_init(&buf->b_vars.dv_hashtab);
631 #endif
632 #ifdef FEAT_USR_CMDS
633 uc_clear(&buf->b_ucmds); /* clear local user commands */
634 #endif
635 #ifdef FEAT_SIGNS
636 buf_delete_signs(buf); /* delete any signs */
637 #endif
638 #ifdef FEAT_NETBEANS_INTG
639 if (usingNetbeans)
640 netbeans_file_killed(buf);
641 #endif
642 #ifdef FEAT_LOCALMAP
643 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
644 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
645 #endif
646 #ifdef FEAT_MBYTE
647 vim_free(buf->b_start_fenc);
648 buf->b_start_fenc = NULL;
649 #endif
650 #ifdef FEAT_SPELL
651 ga_clear(&buf->b_langp);
652 #endif
656 * Free the b_wininfo list for buffer "buf".
658 static void
659 clear_wininfo(buf)
660 buf_T *buf;
662 wininfo_T *wip;
664 while (buf->b_wininfo != NULL)
666 wip = buf->b_wininfo;
667 buf->b_wininfo = wip->wi_next;
668 if (wip->wi_optset)
670 clear_winopt(&wip->wi_opt);
671 #ifdef FEAT_FOLDING
672 deleteFoldRecurse(&wip->wi_folds);
673 #endif
675 vim_free(wip);
679 #if defined(FEAT_LISTCMDS) || defined(PROTO)
681 * Go to another buffer. Handles the result of the ATTENTION dialog.
683 void
684 goto_buffer(eap, start, dir, count)
685 exarg_T *eap;
686 int start;
687 int dir;
688 int count;
690 # if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
691 buf_T *old_curbuf = curbuf;
693 swap_exists_action = SEA_DIALOG;
694 # endif
695 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
696 start, dir, count, eap->forceit);
697 # if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
698 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
700 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
701 cleanup_T cs;
703 /* Reset the error/interrupt/exception state here so that
704 * aborting() returns FALSE when closing a window. */
705 enter_cleanup(&cs);
706 # endif
708 /* Quitting means closing the split window, nothing else. */
709 win_close(curwin, TRUE);
710 swap_exists_action = SEA_NONE;
711 swap_exists_did_quit = TRUE;
713 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
714 /* Restore the error/interrupt/exception state if not discarded by a
715 * new aborting error, interrupt, or uncaught exception. */
716 leave_cleanup(&cs);
717 # endif
719 else
720 handle_swap_exists(old_curbuf);
721 # endif
723 #endif
725 #if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
727 * Handle the situation of swap_exists_action being set.
728 * It is allowed for "old_curbuf" to be NULL or invalid.
730 void
731 handle_swap_exists(old_curbuf)
732 buf_T *old_curbuf;
734 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
735 cleanup_T cs;
736 # endif
738 if (swap_exists_action == SEA_QUIT)
740 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
741 /* Reset the error/interrupt/exception state here so that
742 * aborting() returns FALSE when closing a buffer. */
743 enter_cleanup(&cs);
744 # endif
746 /* User selected Quit at ATTENTION prompt. Go back to previous
747 * buffer. If that buffer is gone or the same as the current one,
748 * open a new, empty buffer. */
749 swap_exists_action = SEA_NONE; /* don't want it again */
750 swap_exists_did_quit = TRUE;
751 close_buffer(curwin, curbuf, DOBUF_UNLOAD);
752 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
753 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
754 if (old_curbuf != NULL)
755 enter_buffer(old_curbuf);
756 /* If "old_curbuf" is NULL we are in big trouble here... */
758 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
759 /* Restore the error/interrupt/exception state if not discarded by a
760 * new aborting error, interrupt, or uncaught exception. */
761 leave_cleanup(&cs);
762 # endif
764 else if (swap_exists_action == SEA_RECOVER)
766 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
767 /* Reset the error/interrupt/exception state here so that
768 * aborting() returns FALSE when closing a buffer. */
769 enter_cleanup(&cs);
770 # endif
772 /* User selected Recover at ATTENTION prompt. */
773 msg_scroll = TRUE;
774 ml_recover();
775 MSG_PUTS("\n"); /* don't overwrite the last message */
776 cmdline_row = msg_row;
777 do_modelines(0);
779 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
780 /* Restore the error/interrupt/exception state if not discarded by a
781 * new aborting error, interrupt, or uncaught exception. */
782 leave_cleanup(&cs);
783 # endif
785 swap_exists_action = SEA_NONE;
787 #endif
789 #if defined(FEAT_LISTCMDS) || defined(PROTO)
791 * do_bufdel() - delete or unload buffer(s)
793 * addr_count == 0: ":bdel" - delete current buffer
794 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
795 * buffer "end_bnr", then any other arguments.
796 * addr_count == 2: ":N,N bdel" - delete buffers in range
798 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
799 * DOBUF_DEL (":bdel")
801 * Returns error message or NULL
803 char_u *
804 do_bufdel(command, arg, addr_count, start_bnr, end_bnr, forceit)
805 int command;
806 char_u *arg; /* pointer to extra arguments */
807 int addr_count;
808 int start_bnr; /* first buffer number in a range */
809 int end_bnr; /* buffer nr or last buffer nr in a range */
810 int forceit;
812 int do_current = 0; /* delete current buffer? */
813 int deleted = 0; /* number of buffers deleted */
814 char_u *errormsg = NULL; /* return value */
815 int bnr; /* buffer number */
816 char_u *p;
818 if (addr_count == 0)
820 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
822 else
824 if (addr_count == 2)
826 if (*arg) /* both range and argument is not allowed */
827 return (char_u *)_(e_trailing);
828 bnr = start_bnr;
830 else /* addr_count == 1 */
831 bnr = end_bnr;
833 for ( ;!got_int; ui_breakcheck())
836 * delete the current buffer last, otherwise when the
837 * current buffer is deleted, the next buffer becomes
838 * the current one and will be loaded, which may then
839 * also be deleted, etc.
841 if (bnr == curbuf->b_fnum)
842 do_current = bnr;
843 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
844 forceit) == OK)
845 ++deleted;
848 * find next buffer number to delete/unload
850 if (addr_count == 2)
852 if (++bnr > end_bnr)
853 break;
855 else /* addr_count == 1 */
857 arg = skipwhite(arg);
858 if (*arg == NUL)
859 break;
860 if (!VIM_ISDIGIT(*arg))
862 p = skiptowhite_esc(arg);
863 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE, FALSE);
864 if (bnr < 0) /* failed */
865 break;
866 arg = p;
868 else
869 bnr = getdigits(&arg);
872 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
873 FORWARD, do_current, forceit) == OK)
874 ++deleted;
876 if (deleted == 0)
878 if (command == DOBUF_UNLOAD)
879 STRCPY(IObuff, _("E515: No buffers were unloaded"));
880 else if (command == DOBUF_DEL)
881 STRCPY(IObuff, _("E516: No buffers were deleted"));
882 else
883 STRCPY(IObuff, _("E517: No buffers were wiped out"));
884 errormsg = IObuff;
886 else if (deleted >= p_report)
888 if (command == DOBUF_UNLOAD)
890 if (deleted == 1)
891 MSG(_("1 buffer unloaded"));
892 else
893 smsg((char_u *)_("%d buffers unloaded"), deleted);
895 else if (command == DOBUF_DEL)
897 if (deleted == 1)
898 MSG(_("1 buffer deleted"));
899 else
900 smsg((char_u *)_("%d buffers deleted"), deleted);
902 else
904 if (deleted == 1)
905 MSG(_("1 buffer wiped out"));
906 else
907 smsg((char_u *)_("%d buffers wiped out"), deleted);
913 return errormsg;
917 * Implementation of the commands for the buffer list.
919 * action == DOBUF_GOTO go to specified buffer
920 * action == DOBUF_SPLIT split window and go to specified buffer
921 * action == DOBUF_UNLOAD unload specified buffer(s)
922 * action == DOBUF_DEL delete specified buffer(s) from buffer list
923 * action == DOBUF_WIPE delete specified buffer(s) really
925 * start == DOBUF_CURRENT go to "count" buffer from current buffer
926 * start == DOBUF_FIRST go to "count" buffer from first buffer
927 * start == DOBUF_LAST go to "count" buffer from last buffer
928 * start == DOBUF_MOD go to "count" modified buffer from current buffer
930 * Return FAIL or OK.
933 do_buffer(action, start, dir, count, forceit)
934 int action;
935 int start;
936 int dir; /* FORWARD or BACKWARD */
937 int count; /* buffer number or number of buffers */
938 int forceit; /* TRUE for :...! */
940 buf_T *buf;
941 buf_T *bp;
942 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
943 || action == DOBUF_WIPE);
945 switch (start)
947 case DOBUF_FIRST: buf = firstbuf; break;
948 case DOBUF_LAST: buf = lastbuf; break;
949 default: buf = curbuf; break;
951 if (start == DOBUF_MOD) /* find next modified buffer */
953 while (count-- > 0)
957 buf = buf->b_next;
958 if (buf == NULL)
959 buf = firstbuf;
961 while (buf != curbuf && !bufIsChanged(buf));
963 if (!bufIsChanged(buf))
965 EMSG(_("E84: No modified buffer found"));
966 return FAIL;
969 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
971 while (buf != NULL && buf->b_fnum != count)
972 buf = buf->b_next;
974 else
976 bp = NULL;
977 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
979 /* remember the buffer where we start, we come back there when all
980 * buffers are unlisted. */
981 if (bp == NULL)
982 bp = buf;
983 if (dir == FORWARD)
985 buf = buf->b_next;
986 if (buf == NULL)
987 buf = firstbuf;
989 else
991 buf = buf->b_prev;
992 if (buf == NULL)
993 buf = lastbuf;
995 /* don't count unlisted buffers */
996 if (unload || buf->b_p_bl)
998 --count;
999 bp = NULL; /* use this buffer as new starting point */
1001 if (bp == buf)
1003 /* back where we started, didn't find anything. */
1004 EMSG(_("E85: There is no listed buffer"));
1005 return FAIL;
1010 if (buf == NULL) /* could not find it */
1012 if (start == DOBUF_FIRST)
1014 /* don't warn when deleting */
1015 if (!unload)
1016 EMSGN(_("E86: Buffer %ld does not exist"), count);
1018 else if (dir == FORWARD)
1019 EMSG(_("E87: Cannot go beyond last buffer"));
1020 else
1021 EMSG(_("E88: Cannot go before first buffer"));
1022 return FAIL;
1025 #ifdef FEAT_GUI
1026 need_mouse_correct = TRUE;
1027 #endif
1029 #ifdef FEAT_LISTCMDS
1031 * delete buffer buf from memory and/or the list
1033 if (unload)
1035 int forward;
1036 int retval;
1038 /* When unloading or deleting a buffer that's already unloaded and
1039 * unlisted: fail silently. */
1040 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1041 return FAIL;
1043 if (!forceit && bufIsChanged(buf))
1045 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1046 if ((p_confirm || cmdmod.confirm) && p_write)
1048 dialog_changed(buf, FALSE);
1049 # ifdef FEAT_AUTOCMD
1050 if (!buf_valid(buf))
1051 /* Autocommand deleted buffer, oops! It's not changed
1052 * now. */
1053 return FAIL;
1054 # endif
1055 /* If it's still changed fail silently, the dialog already
1056 * mentioned why it fails. */
1057 if (bufIsChanged(buf))
1058 return FAIL;
1060 else
1061 #endif
1063 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1064 buf->b_fnum);
1065 return FAIL;
1070 * If deleting the last (listed) buffer, make it empty.
1071 * The last (listed) buffer cannot be unloaded.
1073 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1074 if (bp->b_p_bl && bp != buf)
1075 break;
1076 if (bp == NULL && buf == curbuf)
1078 if (action == DOBUF_UNLOAD)
1080 EMSG(_("E90: Cannot unload last buffer"));
1081 return FAIL;
1084 /* Close any other windows on this buffer, then make it empty. */
1085 #ifdef FEAT_WINDOWS
1086 close_windows(buf, TRUE);
1087 #endif
1088 setpcmark();
1089 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1090 forceit ? ECMD_FORCEIT : 0, curwin);
1093 * do_ecmd() may create a new buffer, then we have to delete
1094 * the old one. But do_ecmd() may have done that already, check
1095 * if the buffer still exists.
1097 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
1098 close_buffer(NULL, buf, action);
1099 return retval;
1102 #ifdef FEAT_WINDOWS
1104 * If the deleted buffer is the current one, close the current window
1105 * (unless it's the only window). Repeat this so long as we end up in
1106 * a window with this buffer.
1108 while (buf == curbuf
1109 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
1110 win_close(curwin, FALSE);
1111 #endif
1114 * If the buffer to be deleted is not the current one, delete it here.
1116 if (buf != curbuf)
1118 #ifdef FEAT_WINDOWS
1119 close_windows(buf, FALSE);
1120 #endif
1121 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
1122 close_buffer(NULL, buf, action);
1123 return OK;
1127 * Deleting the current buffer: Need to find another buffer to go to.
1128 * There must be another, otherwise it would have been handled above.
1129 * First use au_new_curbuf, if it is valid.
1130 * Then prefer the buffer we most recently visited.
1131 * Else try to find one that is loaded, after the current buffer,
1132 * then before the current buffer.
1133 * Finally use any buffer.
1135 buf = NULL; /* selected buffer */
1136 bp = NULL; /* used when no loaded buffer found */
1137 #ifdef FEAT_AUTOCMD
1138 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1139 buf = au_new_curbuf;
1140 # ifdef FEAT_JUMPLIST
1141 else
1142 # endif
1143 #endif
1144 #ifdef FEAT_JUMPLIST
1145 if (curwin->w_jumplistlen > 0)
1147 int jumpidx;
1149 jumpidx = curwin->w_jumplistidx - 1;
1150 if (jumpidx < 0)
1151 jumpidx = curwin->w_jumplistlen - 1;
1153 forward = jumpidx;
1154 while (jumpidx != curwin->w_jumplistidx)
1156 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1157 if (buf != NULL)
1159 if (buf == curbuf || !buf->b_p_bl)
1160 buf = NULL; /* skip current and unlisted bufs */
1161 else if (buf->b_ml.ml_mfp == NULL)
1163 /* skip unloaded buf, but may keep it for later */
1164 if (bp == NULL)
1165 bp = buf;
1166 buf = NULL;
1169 if (buf != NULL) /* found a valid buffer: stop searching */
1170 break;
1171 /* advance to older entry in jump list */
1172 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1173 break;
1174 if (--jumpidx < 0)
1175 jumpidx = curwin->w_jumplistlen - 1;
1176 if (jumpidx == forward) /* List exhausted for sure */
1177 break;
1180 #endif
1182 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1184 forward = TRUE;
1185 buf = curbuf->b_next;
1186 for (;;)
1188 if (buf == NULL)
1190 if (!forward) /* tried both directions */
1191 break;
1192 buf = curbuf->b_prev;
1193 forward = FALSE;
1194 continue;
1196 /* in non-help buffer, try to skip help buffers, and vv */
1197 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1199 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1200 break;
1201 if (bp == NULL) /* remember unloaded buf for later */
1202 bp = buf;
1204 if (forward)
1205 buf = buf->b_next;
1206 else
1207 buf = buf->b_prev;
1210 if (buf == NULL) /* No loaded buffer, use unloaded one */
1211 buf = bp;
1212 if (buf == NULL) /* No loaded buffer, find listed one */
1214 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1215 if (buf->b_p_bl && buf != curbuf)
1216 break;
1218 if (buf == NULL) /* Still no buffer, just take one */
1220 if (curbuf->b_next != NULL)
1221 buf = curbuf->b_next;
1222 else
1223 buf = curbuf->b_prev;
1228 * make buf current buffer
1230 if (action == DOBUF_SPLIT) /* split window first */
1232 # ifdef FEAT_WINDOWS
1233 /* If 'switchbuf' contains "useopen": jump to first window containing
1234 * "buf" if one exists */
1235 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
1236 return OK;
1237 /* If 'switchbuf' contains "usetab": jump to first window in any tab
1238 * page containing "buf" if one exists */
1239 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
1240 return OK;
1241 if (win_split(0, 0) == FAIL)
1242 # endif
1243 return FAIL;
1245 #endif
1247 /* go to current buffer - nothing to do */
1248 if (buf == curbuf)
1249 return OK;
1252 * Check if the current buffer may be abandoned.
1254 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1256 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1257 if ((p_confirm || cmdmod.confirm) && p_write)
1259 dialog_changed(curbuf, FALSE);
1260 # ifdef FEAT_AUTOCMD
1261 if (!buf_valid(buf))
1262 /* Autocommand deleted buffer, oops! */
1263 return FAIL;
1264 # endif
1266 if (bufIsChanged(curbuf))
1267 #endif
1269 EMSG(_(e_nowrtmsg));
1270 return FAIL;
1274 /* Go to the other buffer. */
1275 set_curbuf(buf, action);
1277 #if defined(FEAT_LISTCMDS) && defined(FEAT_SCROLLBIND)
1278 if (action == DOBUF_SPLIT)
1279 curwin->w_p_scb = FALSE; /* reset 'scrollbind' */
1280 #endif
1282 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1283 if (aborting()) /* autocmds may abort script processing */
1284 return FAIL;
1285 #endif
1287 return OK;
1290 #endif /* FEAT_LISTCMDS */
1293 * Set current buffer to "buf". Executes autocommands and closes current
1294 * buffer. "action" tells how to close the current buffer:
1295 * DOBUF_GOTO free or hide it
1296 * DOBUF_SPLIT nothing
1297 * DOBUF_UNLOAD unload it
1298 * DOBUF_DEL delete it
1299 * DOBUF_WIPE wipe it out
1301 void
1302 set_curbuf(buf, action)
1303 buf_T *buf;
1304 int action;
1306 buf_T *prevbuf;
1307 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1308 || action == DOBUF_WIPE);
1310 setpcmark();
1311 if (!cmdmod.keepalt)
1312 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
1313 buflist_altfpos(curwin); /* remember curpos */
1315 #ifdef FEAT_VISUAL
1316 /* Don't restart Select mode after switching to another buffer. */
1317 VIsual_reselect = FALSE;
1318 #endif
1320 /* close_windows() or apply_autocmds() may change curbuf */
1321 prevbuf = curbuf;
1323 #ifdef FEAT_AUTOCMD
1324 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1325 # ifdef FEAT_EVAL
1326 if (buf_valid(prevbuf) && !aborting())
1327 # else
1328 if (buf_valid(prevbuf))
1329 # endif
1330 #endif
1332 #ifdef FEAT_WINDOWS
1333 if (unload)
1334 close_windows(prevbuf, FALSE);
1335 #endif
1336 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1337 if (buf_valid(prevbuf) && !aborting())
1338 #else
1339 if (buf_valid(prevbuf))
1340 #endif
1342 if (prevbuf == curbuf)
1343 u_sync(FALSE);
1344 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1345 unload ? action : (action == DOBUF_GOTO
1346 && !P_HID(prevbuf)
1347 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0);
1350 #ifdef FEAT_AUTOCMD
1351 /* An autocommand may have deleted "buf", already entered it (e.g., when
1352 * it did ":bunload") or aborted the script processing! */
1353 # ifdef FEAT_EVAL
1354 if (buf_valid(buf) && buf != curbuf && !aborting())
1355 # else
1356 if (buf_valid(buf) && buf != curbuf)
1357 # endif
1358 #endif
1359 enter_buffer(buf);
1363 * Enter a new current buffer.
1364 * Old curbuf must have been abandoned already!
1366 void
1367 enter_buffer(buf)
1368 buf_T *buf;
1370 /* Copy buffer and window local option values. Not for a help buffer. */
1371 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1372 if (!buf->b_help)
1373 get_winopts(buf);
1374 #ifdef FEAT_FOLDING
1375 else
1376 /* Remove all folds in the window. */
1377 clearFolding(curwin);
1378 foldUpdateAll(curwin); /* update folds (later). */
1379 #endif
1381 /* Get the buffer in the current window. */
1382 curwin->w_buffer = buf;
1383 curbuf = buf;
1384 ++curbuf->b_nwindows;
1386 #ifdef FEAT_DIFF
1387 if (curwin->w_p_diff)
1388 diff_buf_add(curbuf);
1389 #endif
1391 /* Cursor on first line by default. */
1392 curwin->w_cursor.lnum = 1;
1393 curwin->w_cursor.col = 0;
1394 #ifdef FEAT_VIRTUALEDIT
1395 curwin->w_cursor.coladd = 0;
1396 #endif
1397 curwin->w_set_curswant = TRUE;
1398 #ifdef FEAT_AUTOCMD
1399 curwin->w_topline_was_set = FALSE;
1400 #endif
1402 /* mark cursor position as being invalid */
1403 curwin->w_valid = 0;
1405 /* Make sure the buffer is loaded. */
1406 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
1408 #ifdef FEAT_AUTOCMD
1409 /* If there is no filetype, allow for detecting one. Esp. useful for
1410 * ":ball" used in a autocommand. If there already is a filetype we
1411 * might prefer to keep it. */
1412 if (*curbuf->b_p_ft == NUL)
1413 did_filetype = FALSE;
1414 #endif
1416 open_buffer(FALSE, NULL);
1418 else
1420 if (!msg_silent)
1421 need_fileinfo = TRUE; /* display file info after redraw */
1422 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1423 #ifdef FEAT_AUTOCMD
1424 curwin->w_topline = 1;
1425 # ifdef FEAT_DIFF
1426 curwin->w_topfill = 0;
1427 # endif
1428 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1429 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1430 #endif
1433 /* If autocommands did not change the cursor position, restore cursor lnum
1434 * and possibly cursor col. */
1435 if (curwin->w_cursor.lnum == 1 && inindent(0))
1436 buflist_getfpos();
1438 check_arg_idx(curwin); /* check for valid arg_idx */
1439 #ifdef FEAT_TITLE
1440 maketitle();
1441 #endif
1442 #ifdef FEAT_AUTOCMD
1443 /* when autocmds didn't change it */
1444 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
1445 #endif
1446 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1448 #ifdef FEAT_NETBEANS_INTG
1449 /* Send fileOpened event because we've changed buffers. */
1450 if (usingNetbeans && isNetbeansBuffer(curbuf))
1451 netbeans_file_activated(curbuf);
1452 #endif
1454 /* Change directories when the 'acd' option is set. */
1455 DO_AUTOCHDIR
1457 #ifdef FEAT_KEYMAP
1458 if (curbuf->b_kmap_state & KEYMAP_INIT)
1459 (void)keymap_init();
1460 #endif
1461 #ifdef FEAT_SPELL
1462 /* May need to set the spell language. Can only do this after the buffer
1463 * has been properly setup. */
1464 if (!curbuf->b_help && curwin->w_p_spell && *curbuf->b_p_spl != NUL)
1465 (void)did_set_spelllang(curbuf);
1466 #endif
1468 redraw_later(NOT_VALID);
1471 #if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1473 * Change to the directory of the current buffer.
1475 void
1476 do_autochdir()
1478 if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK)
1479 shorten_fnames(TRUE);
1481 #endif
1484 * functions for dealing with the buffer list
1488 * Add a file name to the buffer list. Return a pointer to the buffer.
1489 * If the same file name already exists return a pointer to that buffer.
1490 * If it does not exist, or if fname == NULL, a new entry is created.
1491 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1492 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1493 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
1494 * This is the ONLY way to create a new buffer.
1496 static int top_file_num = 1; /* highest file number */
1498 buf_T *
1499 buflist_new(ffname, sfname, lnum, flags)
1500 char_u *ffname; /* full path of fname or relative */
1501 char_u *sfname; /* short fname or NULL */
1502 linenr_T lnum; /* preferred cursor line */
1503 int flags; /* BLN_ defines */
1505 buf_T *buf;
1506 #ifdef UNIX
1507 struct stat st;
1508 #endif
1510 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1513 * If file name already exists in the list, update the entry.
1515 #ifdef UNIX
1516 /* On Unix we can use inode numbers when the file exists. Works better
1517 * for hard links. */
1518 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1519 st.st_dev = (dev_T)-1;
1520 #endif
1521 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1522 #ifdef UNIX
1523 buflist_findname_stat(ffname, &st)
1524 #else
1525 buflist_findname(ffname)
1526 #endif
1527 ) != NULL)
1529 vim_free(ffname);
1530 if (lnum != 0)
1531 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1532 /* copy the options now, if 'cpo' doesn't have 's' and not done
1533 * already */
1534 buf_copy_options(buf, 0);
1535 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1537 buf->b_p_bl = TRUE;
1538 #ifdef FEAT_AUTOCMD
1539 if (!(flags & BLN_DUMMY))
1540 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1541 #endif
1543 return buf;
1547 * If the current buffer has no name and no contents, use the current
1548 * buffer. Otherwise: Need to allocate a new buffer structure.
1550 * This is the ONLY place where a new buffer structure is allocated!
1551 * (A spell file buffer is allocated in spell.c, but that's not a normal
1552 * buffer.)
1554 buf = NULL;
1555 if ((flags & BLN_CURBUF)
1556 && curbuf != NULL
1557 && curbuf->b_ffname == NULL
1558 && curbuf->b_nwindows <= 1
1559 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1561 buf = curbuf;
1562 #ifdef FEAT_AUTOCMD
1563 /* It's like this buffer is deleted. Watch out for autocommands that
1564 * change curbuf! If that happens, allocate a new buffer anyway. */
1565 if (curbuf->b_p_bl)
1566 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1567 if (buf == curbuf)
1568 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1569 # ifdef FEAT_EVAL
1570 if (aborting()) /* autocmds may abort script processing */
1571 return NULL;
1572 # endif
1573 #endif
1574 #ifdef FEAT_QUICKFIX
1575 # ifdef FEAT_AUTOCMD
1576 if (buf == curbuf)
1577 # endif
1579 /* Make sure 'bufhidden' and 'buftype' are empty */
1580 clear_string_option(&buf->b_p_bh);
1581 clear_string_option(&buf->b_p_bt);
1583 #endif
1585 if (buf != curbuf || curbuf == NULL)
1587 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1588 if (buf == NULL)
1590 vim_free(ffname);
1591 return NULL;
1595 if (ffname != NULL)
1597 buf->b_ffname = ffname;
1598 buf->b_sfname = vim_strsave(sfname);
1601 clear_wininfo(buf);
1602 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1604 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1605 || buf->b_wininfo == NULL)
1607 vim_free(buf->b_ffname);
1608 buf->b_ffname = NULL;
1609 vim_free(buf->b_sfname);
1610 buf->b_sfname = NULL;
1611 if (buf != curbuf)
1612 free_buffer(buf);
1613 return NULL;
1616 if (buf == curbuf)
1618 /* free all things allocated for this buffer */
1619 buf_freeall(buf, FALSE, FALSE);
1620 if (buf != curbuf) /* autocommands deleted the buffer! */
1621 return NULL;
1622 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1623 if (aborting()) /* autocmds may abort script processing */
1624 return NULL;
1625 #endif
1626 /* buf->b_nwindows = 0; why was this here? */
1627 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
1628 #ifdef FEAT_KEYMAP
1629 /* need to reload lmaps and set b:keymap_name */
1630 curbuf->b_kmap_state |= KEYMAP_INIT;
1631 #endif
1633 else
1636 * put new buffer at the end of the buffer list
1638 buf->b_next = NULL;
1639 if (firstbuf == NULL) /* buffer list is empty */
1641 buf->b_prev = NULL;
1642 firstbuf = buf;
1644 else /* append new buffer at end of list */
1646 lastbuf->b_next = buf;
1647 buf->b_prev = lastbuf;
1649 lastbuf = buf;
1651 buf->b_fnum = top_file_num++;
1652 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1654 EMSG(_("W14: Warning: List of file names overflow"));
1655 if (emsg_silent == 0)
1657 out_flush();
1658 ui_delay(3000L, TRUE); /* make sure it is noticed */
1660 top_file_num = 1;
1664 * Always copy the options from the current buffer.
1666 buf_copy_options(buf, BCO_ALWAYS);
1669 buf->b_wininfo->wi_fpos.lnum = lnum;
1670 buf->b_wininfo->wi_win = curwin;
1672 #ifdef FEAT_EVAL
1673 init_var_dict(&buf->b_vars, &buf->b_bufvar); /* init b: variables */
1674 #endif
1675 #ifdef FEAT_SYN_HL
1676 hash_init(&buf->b_keywtab);
1677 hash_init(&buf->b_keywtab_ic);
1678 #endif
1680 buf->b_fname = buf->b_sfname;
1681 #ifdef UNIX
1682 if (st.st_dev == (dev_T)-1)
1683 buf->b_dev_valid = FALSE;
1684 else
1686 buf->b_dev_valid = TRUE;
1687 buf->b_dev = st.st_dev;
1688 buf->b_ino = st.st_ino;
1690 #endif
1691 buf->b_u_synced = TRUE;
1692 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
1693 if (flags & BLN_DUMMY)
1694 buf->b_flags |= BF_DUMMY;
1695 buf_clear_file(buf);
1696 clrallmarks(buf); /* clear marks */
1697 fmarks_check_names(buf); /* check file marks for this file */
1698 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1699 #ifdef FEAT_AUTOCMD
1700 if (!(flags & BLN_DUMMY))
1702 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
1703 if (flags & BLN_LISTED)
1704 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1705 # ifdef FEAT_EVAL
1706 if (aborting()) /* autocmds may abort script processing */
1707 return NULL;
1708 # endif
1710 #endif
1712 return buf;
1716 * Free the memory for the options of a buffer.
1717 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1718 * 'fileencoding'.
1720 void
1721 free_buf_options(buf, free_p_ff)
1722 buf_T *buf;
1723 int free_p_ff;
1725 if (free_p_ff)
1727 #ifdef FEAT_MBYTE
1728 clear_string_option(&buf->b_p_fenc);
1729 #endif
1730 clear_string_option(&buf->b_p_ff);
1731 #ifdef FEAT_QUICKFIX
1732 clear_string_option(&buf->b_p_bh);
1733 clear_string_option(&buf->b_p_bt);
1734 #endif
1736 #ifdef FEAT_FIND_ID
1737 clear_string_option(&buf->b_p_def);
1738 clear_string_option(&buf->b_p_inc);
1739 # ifdef FEAT_EVAL
1740 clear_string_option(&buf->b_p_inex);
1741 # endif
1742 #endif
1743 #if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1744 clear_string_option(&buf->b_p_inde);
1745 clear_string_option(&buf->b_p_indk);
1746 #endif
1747 #if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1748 clear_string_option(&buf->b_p_bexpr);
1749 #endif
1750 #if defined(FEAT_EVAL)
1751 clear_string_option(&buf->b_p_fex);
1752 #endif
1753 #ifdef FEAT_CRYPT
1754 clear_string_option(&buf->b_p_key);
1755 #endif
1756 clear_string_option(&buf->b_p_kp);
1757 clear_string_option(&buf->b_p_mps);
1758 clear_string_option(&buf->b_p_fo);
1759 clear_string_option(&buf->b_p_flp);
1760 clear_string_option(&buf->b_p_isk);
1761 #ifdef FEAT_VARTABS
1762 clear_string_option(&buf->b_p_vsts);
1763 if (buf->b_p_vsts_nopaste)
1764 vim_free(buf->b_p_vsts_nopaste);
1765 buf->b_p_vsts_nopaste = 0;
1766 if (buf->b_p_vsts_ary)
1767 vim_free(buf->b_p_vsts_ary);
1768 buf->b_p_vsts_ary = 0;
1769 clear_string_option(&buf->b_p_vts);
1770 if (buf->b_p_vts_ary)
1771 vim_free(buf->b_p_vts_ary);
1772 buf->b_p_vts_ary = 0;
1773 #endif
1774 #ifdef FEAT_KEYMAP
1775 clear_string_option(&buf->b_p_keymap);
1776 ga_clear(&buf->b_kmap_ga);
1777 #endif
1778 #ifdef FEAT_COMMENTS
1779 clear_string_option(&buf->b_p_com);
1780 #endif
1781 #ifdef FEAT_FOLDING
1782 clear_string_option(&buf->b_p_cms);
1783 #endif
1784 clear_string_option(&buf->b_p_nf);
1785 #ifdef FEAT_SYN_HL
1786 clear_string_option(&buf->b_p_syn);
1787 #endif
1788 #ifdef FEAT_SPELL
1789 clear_string_option(&buf->b_p_spc);
1790 clear_string_option(&buf->b_p_spf);
1791 vim_free(buf->b_cap_prog);
1792 buf->b_cap_prog = NULL;
1793 clear_string_option(&buf->b_p_spl);
1794 #endif
1795 #ifdef FEAT_SEARCHPATH
1796 clear_string_option(&buf->b_p_sua);
1797 #endif
1798 #ifdef FEAT_AUTOCMD
1799 clear_string_option(&buf->b_p_ft);
1800 #endif
1801 #ifdef FEAT_OSFILETYPE
1802 clear_string_option(&buf->b_p_oft);
1803 #endif
1804 #ifdef FEAT_CINDENT
1805 clear_string_option(&buf->b_p_cink);
1806 clear_string_option(&buf->b_p_cino);
1807 #endif
1808 #if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1809 clear_string_option(&buf->b_p_cinw);
1810 #endif
1811 #ifdef FEAT_INS_EXPAND
1812 clear_string_option(&buf->b_p_cpt);
1813 #endif
1814 #ifdef FEAT_COMPL_FUNC
1815 clear_string_option(&buf->b_p_cfu);
1816 clear_string_option(&buf->b_p_ofu);
1817 #endif
1818 #ifdef FEAT_QUICKFIX
1819 clear_string_option(&buf->b_p_gp);
1820 clear_string_option(&buf->b_p_mp);
1821 clear_string_option(&buf->b_p_efm);
1822 #endif
1823 clear_string_option(&buf->b_p_ep);
1824 clear_string_option(&buf->b_p_path);
1825 clear_string_option(&buf->b_p_tags);
1826 #ifdef FEAT_INS_EXPAND
1827 clear_string_option(&buf->b_p_dict);
1828 clear_string_option(&buf->b_p_tsr);
1829 #endif
1830 #ifdef FEAT_TEXTOBJ
1831 clear_string_option(&buf->b_p_qe);
1832 #endif
1833 buf->b_p_ar = -1;
1837 * get alternate file n
1838 * set linenr to lnum or altfpos.lnum if lnum == 0
1839 * also set cursor column to altfpos.col if 'startofline' is not set.
1840 * if (options & GETF_SETMARK) call setpcmark()
1841 * if (options & GETF_ALT) we are jumping to an alternate file.
1842 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1844 * return FAIL for failure, OK for success
1847 buflist_getfile(n, lnum, options, forceit)
1848 int n;
1849 linenr_T lnum;
1850 int options;
1851 int forceit;
1853 buf_T *buf;
1854 #ifdef FEAT_WINDOWS
1855 win_T *wp = NULL;
1856 #endif
1857 pos_T *fpos;
1858 colnr_T col;
1860 buf = buflist_findnr(n);
1861 if (buf == NULL)
1863 if ((options & GETF_ALT) && n == 0)
1864 EMSG(_(e_noalt));
1865 else
1866 EMSGN(_("E92: Buffer %ld not found"), n);
1867 return FAIL;
1870 /* if alternate file is the current buffer, nothing to do */
1871 if (buf == curbuf)
1872 return OK;
1874 if (text_locked())
1876 text_locked_msg();
1877 return FAIL;
1879 #ifdef FEAT_AUTOCMD
1880 if (curbuf_locked())
1881 return FAIL;
1882 #endif
1884 /* altfpos may be changed by getfile(), get it now */
1885 if (lnum == 0)
1887 fpos = buflist_findfpos(buf);
1888 lnum = fpos->lnum;
1889 col = fpos->col;
1891 else
1892 col = 0;
1894 #ifdef FEAT_WINDOWS
1895 if (options & GETF_SWITCH)
1897 /* If 'switchbuf' contains "useopen": jump to first window containing
1898 * "buf" if one exists */
1899 if (swb_flags & SWB_USEOPEN)
1900 wp = buf_jump_open_win(buf);
1901 /* If 'switchbuf' contians "usetab": jump to first window in any tab
1902 * page containing "buf" if one exists */
1903 if (wp == NULL && (swb_flags & SWB_USETAB))
1904 wp = buf_jump_open_tab(buf);
1905 /* If 'switchbuf' contains "split" or "newtab" and the current buffer
1906 * isn't empty: open new window */
1907 if (wp == NULL && (swb_flags & (SWB_SPLIT | SWB_NEWTAB)) && !bufempty())
1909 if (swb_flags & SWB_NEWTAB) /* Open in a new tab */
1910 tabpage_new();
1911 else if (win_split(0, 0) == FAIL) /* Open in a new window */
1912 return FAIL;
1913 # ifdef FEAT_SCROLLBIND
1914 curwin->w_p_scb = FALSE;
1915 # endif
1918 #endif
1920 ++RedrawingDisabled;
1921 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
1922 lnum, forceit) <= 0)
1924 --RedrawingDisabled;
1926 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
1927 if (!p_sol && col != 0)
1929 curwin->w_cursor.col = col;
1930 check_cursor_col();
1931 #ifdef FEAT_VIRTUALEDIT
1932 curwin->w_cursor.coladd = 0;
1933 #endif
1934 curwin->w_set_curswant = TRUE;
1936 return OK;
1938 --RedrawingDisabled;
1939 return FAIL;
1943 * go to the last know line number for the current buffer
1945 void
1946 buflist_getfpos()
1948 pos_T *fpos;
1950 fpos = buflist_findfpos(curbuf);
1952 curwin->w_cursor.lnum = fpos->lnum;
1953 check_cursor_lnum();
1955 if (p_sol)
1956 curwin->w_cursor.col = 0;
1957 else
1959 curwin->w_cursor.col = fpos->col;
1960 check_cursor_col();
1961 #ifdef FEAT_VIRTUALEDIT
1962 curwin->w_cursor.coladd = 0;
1963 #endif
1964 curwin->w_set_curswant = TRUE;
1968 #if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
1970 * Find file in buffer list by name (it has to be for the current window).
1971 * Returns NULL if not found.
1973 buf_T *
1974 buflist_findname_exp(fname)
1975 char_u *fname;
1977 char_u *ffname;
1978 buf_T *buf = NULL;
1980 /* First make the name into a full path name */
1981 ffname = FullName_save(fname,
1982 #ifdef UNIX
1983 TRUE /* force expansion, get rid of symbolic links */
1984 #else
1985 FALSE
1986 #endif
1988 if (ffname != NULL)
1990 buf = buflist_findname(ffname);
1991 vim_free(ffname);
1993 return buf;
1995 #endif
1998 * Find file in buffer list by name (it has to be for the current window).
1999 * "ffname" must have a full path.
2000 * Skips dummy buffers.
2001 * Returns NULL if not found.
2003 buf_T *
2004 buflist_findname(ffname)
2005 char_u *ffname;
2007 #ifdef UNIX
2008 struct stat st;
2010 if (mch_stat((char *)ffname, &st) < 0)
2011 st.st_dev = (dev_T)-1;
2012 return buflist_findname_stat(ffname, &st);
2016 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2017 * twice for the same file.
2018 * Returns NULL if not found.
2020 static buf_T *
2021 buflist_findname_stat(ffname, stp)
2022 char_u *ffname;
2023 struct stat *stp;
2025 #endif
2026 buf_T *buf;
2028 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2029 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
2030 #ifdef UNIX
2031 , stp
2032 #endif
2034 return buf;
2035 return NULL;
2038 #if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO)
2040 * Find file in buffer list by a regexp pattern.
2041 * Return fnum of the found buffer.
2042 * Return < 0 for error.
2045 buflist_findpat(pattern, pattern_end, unlisted, diffmode)
2046 char_u *pattern;
2047 char_u *pattern_end; /* pointer to first char after pattern */
2048 int unlisted; /* find unlisted buffers */
2049 int diffmode UNUSED; /* find diff-mode buffers only */
2051 buf_T *buf;
2052 regprog_T *prog;
2053 int match = -1;
2054 int find_listed;
2055 char_u *pat;
2056 char_u *patend;
2057 int attempt;
2058 char_u *p;
2059 int toggledollar;
2061 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2063 if (*pattern == '%')
2064 match = curbuf->b_fnum;
2065 else
2066 match = curwin->w_alt_fnum;
2067 #ifdef FEAT_DIFF
2068 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2069 match = -1;
2070 #endif
2074 * Try four ways of matching a listed buffer:
2075 * attempt == 0: without '^' or '$' (at any position)
2076 * attempt == 1: with '^' at start (only at position 0)
2077 * attempt == 2: with '$' at end (only match at end)
2078 * attempt == 3: with '^' at start and '$' at end (only full match)
2079 * Repeat this for finding an unlisted buffer if there was no matching
2080 * listed buffer.
2082 else
2084 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2085 if (pat == NULL)
2086 return -1;
2087 patend = pat + STRLEN(pat) - 1;
2088 toggledollar = (patend > pat && *patend == '$');
2090 /* First try finding a listed buffer. If not found and "unlisted"
2091 * is TRUE, try finding an unlisted buffer. */
2092 find_listed = TRUE;
2093 for (;;)
2095 for (attempt = 0; attempt <= 3; ++attempt)
2097 /* may add '^' and '$' */
2098 if (toggledollar)
2099 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2100 p = pat;
2101 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2102 ++p;
2103 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2104 if (prog == NULL)
2106 vim_free(pat);
2107 return -1;
2110 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2111 if (buf->b_p_bl == find_listed
2112 #ifdef FEAT_DIFF
2113 && (!diffmode || diff_mode_buf(buf))
2114 #endif
2115 && buflist_match(prog, buf) != NULL)
2117 if (match >= 0) /* already found a match */
2119 match = -2;
2120 break;
2122 match = buf->b_fnum; /* remember first match */
2125 vim_free(prog);
2126 if (match >= 0) /* found one match */
2127 break;
2130 /* Only search for unlisted buffers if there was no match with
2131 * a listed buffer. */
2132 if (!unlisted || !find_listed || match != -1)
2133 break;
2134 find_listed = FALSE;
2137 vim_free(pat);
2140 if (match == -2)
2141 EMSG2(_("E93: More than one match for %s"), pattern);
2142 else if (match < 0)
2143 EMSG2(_("E94: No matching buffer for %s"), pattern);
2144 return match;
2146 #endif
2148 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2151 * Find all buffer names that match.
2152 * For command line expansion of ":buf" and ":sbuf".
2153 * Return OK if matches found, FAIL otherwise.
2156 ExpandBufnames(pat, num_file, file, options)
2157 char_u *pat;
2158 int *num_file;
2159 char_u ***file;
2160 int options;
2162 int count = 0;
2163 buf_T *buf;
2164 int round;
2165 char_u *p;
2166 int attempt;
2167 regprog_T *prog;
2168 char_u *patc;
2170 *num_file = 0; /* return values in case of FAIL */
2171 *file = NULL;
2173 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2174 if (*pat == '^')
2176 patc = alloc((unsigned)STRLEN(pat) + 11);
2177 if (patc == NULL)
2178 return FAIL;
2179 STRCPY(patc, "\\(^\\|[\\/]\\)");
2180 STRCPY(patc + 11, pat + 1);
2182 else
2183 patc = pat;
2186 * attempt == 0: try match with '\<', match at start of word
2187 * attempt == 1: try match without '\<', match anywhere
2189 for (attempt = 0; attempt <= 1; ++attempt)
2191 if (attempt > 0 && patc == pat)
2192 break; /* there was no anchor, no need to try again */
2193 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2194 if (prog == NULL)
2196 if (patc != pat)
2197 vim_free(patc);
2198 return FAIL;
2202 * round == 1: Count the matches.
2203 * round == 2: Build the array to keep the matches.
2205 for (round = 1; round <= 2; ++round)
2207 count = 0;
2208 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2210 if (!buf->b_p_bl) /* skip unlisted buffers */
2211 continue;
2212 p = buflist_match(prog, buf);
2213 if (p != NULL)
2215 if (round == 1)
2216 ++count;
2217 else
2219 if (options & WILD_HOME_REPLACE)
2220 p = home_replace_save(buf, p);
2221 else
2222 p = vim_strsave(p);
2223 (*file)[count++] = p;
2227 if (count == 0) /* no match found, break here */
2228 break;
2229 if (round == 1)
2231 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2232 if (*file == NULL)
2234 vim_free(prog);
2235 if (patc != pat)
2236 vim_free(patc);
2237 return FAIL;
2241 vim_free(prog);
2242 if (count) /* match(es) found, break here */
2243 break;
2246 if (patc != pat)
2247 vim_free(patc);
2249 *num_file = count;
2250 return (count == 0 ? FAIL : OK);
2253 #endif /* FEAT_CMDL_COMPL */
2255 #ifdef HAVE_BUFLIST_MATCH
2257 * Check for a match on the file name for buffer "buf" with regprog "prog".
2259 static char_u *
2260 buflist_match(prog, buf)
2261 regprog_T *prog;
2262 buf_T *buf;
2264 char_u *match;
2266 /* First try the short file name, then the long file name. */
2267 match = fname_match(prog, buf->b_sfname);
2268 if (match == NULL)
2269 match = fname_match(prog, buf->b_ffname);
2271 return match;
2275 * Try matching the regexp in "prog" with file name "name".
2276 * Return "name" when there is a match, NULL when not.
2278 static char_u *
2279 fname_match(prog, name)
2280 regprog_T *prog;
2281 char_u *name;
2283 char_u *match = NULL;
2284 char_u *p;
2285 regmatch_T regmatch;
2287 if (name != NULL)
2289 regmatch.regprog = prog;
2290 #ifdef CASE_INSENSITIVE_FILENAME
2291 regmatch.rm_ic = TRUE; /* Always ignore case */
2292 #else
2293 regmatch.rm_ic = FALSE; /* Never ignore case */
2294 #endif
2296 if (vim_regexec(&regmatch, name, (colnr_T)0))
2297 match = name;
2298 else
2300 /* Replace $(HOME) with '~' and try matching again. */
2301 p = home_replace_save(NULL, name);
2302 if (p != NULL && vim_regexec(&regmatch, p, (colnr_T)0))
2303 match = name;
2304 vim_free(p);
2308 return match;
2310 #endif
2313 * find file in buffer list by number
2315 buf_T *
2316 buflist_findnr(nr)
2317 int nr;
2319 buf_T *buf;
2321 if (nr == 0)
2322 nr = curwin->w_alt_fnum;
2323 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2324 if (buf->b_fnum == nr)
2325 return (buf);
2326 return NULL;
2330 * Get name of file 'n' in the buffer list.
2331 * When the file has no name an empty string is returned.
2332 * home_replace() is used to shorten the file name (used for marks).
2333 * Returns a pointer to allocated memory, of NULL when failed.
2335 char_u *
2336 buflist_nr2name(n, fullname, helptail)
2337 int n;
2338 int fullname;
2339 int helptail; /* for help buffers return tail only */
2341 buf_T *buf;
2343 buf = buflist_findnr(n);
2344 if (buf == NULL)
2345 return NULL;
2346 return home_replace_save(helptail ? buf : NULL,
2347 fullname ? buf->b_ffname : buf->b_fname);
2351 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2352 * When "copy_options" is TRUE save the local window option values.
2353 * When "lnum" is 0 only do the options.
2355 static void
2356 buflist_setfpos(buf, win, lnum, col, copy_options)
2357 buf_T *buf;
2358 win_T *win;
2359 linenr_T lnum;
2360 colnr_T col;
2361 int copy_options;
2363 wininfo_T *wip;
2365 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2366 if (wip->wi_win == win)
2367 break;
2368 if (wip == NULL)
2370 /* allocate a new entry */
2371 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2372 if (wip == NULL)
2373 return;
2374 wip->wi_win = win;
2375 if (lnum == 0) /* set lnum even when it's 0 */
2376 lnum = 1;
2378 else
2380 /* remove the entry from the list */
2381 if (wip->wi_prev)
2382 wip->wi_prev->wi_next = wip->wi_next;
2383 else
2384 buf->b_wininfo = wip->wi_next;
2385 if (wip->wi_next)
2386 wip->wi_next->wi_prev = wip->wi_prev;
2387 if (copy_options && wip->wi_optset)
2389 clear_winopt(&wip->wi_opt);
2390 #ifdef FEAT_FOLDING
2391 deleteFoldRecurse(&wip->wi_folds);
2392 #endif
2395 if (lnum != 0)
2397 wip->wi_fpos.lnum = lnum;
2398 wip->wi_fpos.col = col;
2400 if (copy_options)
2402 /* Save the window-specific option values. */
2403 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2404 #ifdef FEAT_FOLDING
2405 wip->wi_fold_manual = win->w_fold_manual;
2406 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2407 #endif
2408 wip->wi_optset = TRUE;
2411 /* insert the entry in front of the list */
2412 wip->wi_next = buf->b_wininfo;
2413 buf->b_wininfo = wip;
2414 wip->wi_prev = NULL;
2415 if (wip->wi_next)
2416 wip->wi_next->wi_prev = wip;
2418 return;
2421 #ifdef FEAT_DIFF
2422 static int wininfo_other_tab_diff __ARGS((wininfo_T *wip));
2425 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2426 * page. That's because a diff is local to a tab page.
2428 static int
2429 wininfo_other_tab_diff(wip)
2430 wininfo_T *wip;
2432 win_T *wp;
2434 if (wip->wi_opt.wo_diff)
2436 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2437 /* return FALSE when it's a window in the current tab page, thus
2438 * the buffer was in diff mode here */
2439 if (wip->wi_win == wp)
2440 return FALSE;
2441 return TRUE;
2443 return FALSE;
2445 #endif
2448 * Find info for the current window in buffer "buf".
2449 * If not found, return the info for the most recently used window.
2450 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2451 * another tab page.
2452 * Returns NULL when there isn't any info.
2454 static wininfo_T *
2455 find_wininfo(buf, skip_diff_buffer)
2456 buf_T *buf;
2457 int skip_diff_buffer UNUSED;
2459 wininfo_T *wip;
2461 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2462 if (wip->wi_win == curwin
2463 #ifdef FEAT_DIFF
2464 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2465 #endif
2467 break;
2469 /* If no wininfo for curwin, use the first in the list (that doesn't have
2470 * 'diff' set and is in another tab page). */
2471 if (wip == NULL)
2473 #ifdef FEAT_DIFF
2474 if (skip_diff_buffer)
2476 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2477 if (!wininfo_other_tab_diff(wip))
2478 break;
2480 else
2481 #endif
2482 wip = buf->b_wininfo;
2484 return wip;
2488 * Reset the local window options to the values last used in this window.
2489 * If the buffer wasn't used in this window before, use the values from
2490 * the most recently used window. If the values were never set, use the
2491 * global values for the window.
2493 void
2494 get_winopts(buf)
2495 buf_T *buf;
2497 wininfo_T *wip;
2499 clear_winopt(&curwin->w_onebuf_opt);
2500 #ifdef FEAT_FOLDING
2501 clearFolding(curwin);
2502 #endif
2504 wip = find_wininfo(buf, TRUE);
2505 if (wip != NULL && wip->wi_optset)
2507 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2508 #ifdef FEAT_FOLDING
2509 curwin->w_fold_manual = wip->wi_fold_manual;
2510 curwin->w_foldinvalid = TRUE;
2511 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2512 #endif
2514 else
2515 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2517 #ifdef FEAT_FOLDING
2518 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2519 if (p_fdls >= 0)
2520 curwin->w_p_fdl = p_fdls;
2521 #endif
2525 * Find the position (lnum and col) for the buffer 'buf' for the current
2526 * window.
2527 * Returns a pointer to no_position if no position is found.
2529 pos_T *
2530 buflist_findfpos(buf)
2531 buf_T *buf;
2533 wininfo_T *wip;
2534 static pos_T no_position = INIT_POS_T(1, 0, 0);
2536 wip = find_wininfo(buf, FALSE);
2537 if (wip != NULL)
2538 return &(wip->wi_fpos);
2539 else
2540 return &no_position;
2544 * Find the lnum for the buffer 'buf' for the current window.
2546 linenr_T
2547 buflist_findlnum(buf)
2548 buf_T *buf;
2550 return buflist_findfpos(buf)->lnum;
2553 #if defined(FEAT_LISTCMDS) || defined(PROTO)
2555 * List all know file names (for :files and :buffers command).
2557 void
2558 buflist_list(eap)
2559 exarg_T *eap;
2561 buf_T *buf;
2562 int len;
2563 int i;
2565 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2567 /* skip unlisted buffers, unless ! was used */
2568 if (!buf->b_p_bl && !eap->forceit)
2569 continue;
2570 msg_putchar('\n');
2571 if (buf_spname(buf) != NULL)
2572 STRCPY(NameBuff, buf_spname(buf));
2573 else
2574 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2576 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
2577 buf->b_fnum,
2578 buf->b_p_bl ? ' ' : 'u',
2579 buf == curbuf ? '%' :
2580 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2581 buf->b_ml.ml_mfp == NULL ? ' ' :
2582 (buf->b_nwindows == 0 ? 'h' : 'a'),
2583 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2584 (buf->b_flags & BF_READERR) ? 'x'
2585 : (bufIsChanged(buf) ? '+' : ' '),
2586 NameBuff);
2588 /* put "line 999" in column 40 or after the file name */
2589 i = 40 - vim_strsize(IObuff);
2592 IObuff[len++] = ' ';
2593 } while (--i > 0 && len < IOSIZE - 18);
2594 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2595 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
2596 : (long)buflist_findlnum(buf));
2597 msg_outtrans(IObuff);
2598 out_flush(); /* output one line at a time */
2599 ui_breakcheck();
2602 #endif
2605 * Get file name and line number for file 'fnum'.
2606 * Used by DoOneCmd() for translating '%' and '#'.
2607 * Used by insert_reg() and cmdline_paste() for '#' register.
2608 * Return FAIL if not found, OK for success.
2611 buflist_name_nr(fnum, fname, lnum)
2612 int fnum;
2613 char_u **fname;
2614 linenr_T *lnum;
2616 buf_T *buf;
2618 buf = buflist_findnr(fnum);
2619 if (buf == NULL || buf->b_fname == NULL)
2620 return FAIL;
2622 *fname = buf->b_fname;
2623 *lnum = buflist_findlnum(buf);
2625 return OK;
2629 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2630 * The file name with the full path is also remembered, for when :cd is used.
2631 * Returns FAIL for failure (file name already in use by other buffer)
2632 * OK otherwise.
2635 setfname(buf, ffname, sfname, message)
2636 buf_T *buf;
2637 char_u *ffname, *sfname;
2638 int message; /* give message when buffer already exists */
2640 buf_T *obuf = NULL;
2641 #ifdef UNIX
2642 struct stat st;
2643 #endif
2645 if (ffname == NULL || *ffname == NUL)
2647 /* Removing the name. */
2648 vim_free(buf->b_ffname);
2649 vim_free(buf->b_sfname);
2650 buf->b_ffname = NULL;
2651 buf->b_sfname = NULL;
2652 #ifdef UNIX
2653 st.st_dev = (dev_T)-1;
2654 #endif
2656 else
2658 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2659 if (ffname == NULL) /* out of memory */
2660 return FAIL;
2663 * if the file name is already used in another buffer:
2664 * - if the buffer is loaded, fail
2665 * - if the buffer is not loaded, delete it from the list
2667 #ifdef UNIX
2668 if (mch_stat((char *)ffname, &st) < 0)
2669 st.st_dev = (dev_T)-1;
2670 #endif
2671 if (!(buf->b_flags & BF_DUMMY))
2672 #ifdef UNIX
2673 obuf = buflist_findname_stat(ffname, &st);
2674 #else
2675 obuf = buflist_findname(ffname);
2676 #endif
2677 if (obuf != NULL && obuf != buf)
2679 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2681 if (message)
2682 EMSG(_("E95: Buffer with this name already exists"));
2683 vim_free(ffname);
2684 return FAIL;
2686 close_buffer(NULL, obuf, DOBUF_WIPE); /* delete from the list */
2688 sfname = vim_strsave(sfname);
2689 if (ffname == NULL || sfname == NULL)
2691 vim_free(sfname);
2692 vim_free(ffname);
2693 return FAIL;
2695 #ifdef USE_FNAME_CASE
2696 # ifdef USE_LONG_FNAME
2697 if (USE_LONG_FNAME)
2698 # endif
2699 fname_case(sfname, 0); /* set correct case for short file name */
2700 #endif
2701 vim_free(buf->b_ffname);
2702 vim_free(buf->b_sfname);
2703 buf->b_ffname = ffname;
2704 buf->b_sfname = sfname;
2706 buf->b_fname = buf->b_sfname;
2707 #ifdef UNIX
2708 if (st.st_dev == (dev_T)-1)
2709 buf->b_dev_valid = FALSE;
2710 else
2712 buf->b_dev_valid = TRUE;
2713 buf->b_dev = st.st_dev;
2714 buf->b_ino = st.st_ino;
2716 #endif
2718 #ifndef SHORT_FNAME
2719 buf->b_shortname = FALSE;
2720 #endif
2722 buf_name_changed(buf);
2723 return OK;
2727 * Crude way of changing the name of a buffer. Use with care!
2728 * The name should be relative to the current directory.
2730 void
2731 buf_set_name(fnum, name)
2732 int fnum;
2733 char_u *name;
2735 buf_T *buf;
2737 buf = buflist_findnr(fnum);
2738 if (buf != NULL)
2740 vim_free(buf->b_sfname);
2741 vim_free(buf->b_ffname);
2742 buf->b_ffname = vim_strsave(name);
2743 buf->b_sfname = NULL;
2744 /* Allocate ffname and expand into full path. Also resolves .lnk
2745 * files on Win32. */
2746 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
2747 buf->b_fname = buf->b_sfname;
2752 * Take care of what needs to be done when the name of buffer "buf" has
2753 * changed.
2755 void
2756 buf_name_changed(buf)
2757 buf_T *buf;
2760 * If the file name changed, also change the name of the swapfile
2762 if (buf->b_ml.ml_mfp != NULL)
2763 ml_setname(buf);
2765 if (curwin->w_buffer == buf)
2766 check_arg_idx(curwin); /* check file name for arg list */
2767 #ifdef FEAT_TITLE
2768 maketitle(); /* set window title */
2769 #endif
2770 #ifdef FEAT_WINDOWS
2771 status_redraw_all(); /* status lines need to be redrawn */
2772 #endif
2773 fmarks_check_names(buf); /* check named file marks */
2774 ml_timestamp(buf); /* reset timestamp */
2778 * set alternate file name for current window
2780 * Used by do_one_cmd(), do_write() and do_ecmd().
2781 * Return the buffer.
2783 buf_T *
2784 setaltfname(ffname, sfname, lnum)
2785 char_u *ffname;
2786 char_u *sfname;
2787 linenr_T lnum;
2789 buf_T *buf;
2791 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2792 buf = buflist_new(ffname, sfname, lnum, 0);
2793 if (buf != NULL && !cmdmod.keepalt)
2794 curwin->w_alt_fnum = buf->b_fnum;
2795 return buf;
2799 * Get alternate file name for current window.
2800 * Return NULL if there isn't any, and give error message if requested.
2802 char_u *
2803 getaltfname(errmsg)
2804 int errmsg; /* give error message */
2806 char_u *fname;
2807 linenr_T dummy;
2809 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2811 if (errmsg)
2812 EMSG(_(e_noalt));
2813 return NULL;
2815 return fname;
2819 * Add a file name to the buflist and return its number.
2820 * Uses same flags as buflist_new(), except BLN_DUMMY.
2822 * used by qf_init(), main() and doarglist()
2825 buflist_add(fname, flags)
2826 char_u *fname;
2827 int flags;
2829 buf_T *buf;
2831 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
2832 if (buf != NULL)
2833 return buf->b_fnum;
2834 return 0;
2837 #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2839 * Adjust slashes in file names. Called after 'shellslash' was set.
2841 void
2842 buflist_slash_adjust()
2844 buf_T *bp;
2846 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
2848 if (bp->b_ffname != NULL)
2849 slash_adjust(bp->b_ffname);
2850 if (bp->b_sfname != NULL)
2851 slash_adjust(bp->b_sfname);
2854 #endif
2857 * Set alternate cursor position for the current buffer and window "win".
2858 * Also save the local window option values.
2860 void
2861 buflist_altfpos(win)
2862 win_T *win;
2864 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
2868 * Return TRUE if 'ffname' is not the same file as current file.
2869 * Fname must have a full path (expanded by mch_FullName()).
2872 otherfile(ffname)
2873 char_u *ffname;
2875 return otherfile_buf(curbuf, ffname
2876 #ifdef UNIX
2877 , NULL
2878 #endif
2882 static int
2883 otherfile_buf(buf, ffname
2884 #ifdef UNIX
2885 , stp
2886 #endif
2888 buf_T *buf;
2889 char_u *ffname;
2890 #ifdef UNIX
2891 struct stat *stp;
2892 #endif
2894 /* no name is different */
2895 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
2896 return TRUE;
2897 if (fnamecmp(ffname, buf->b_ffname) == 0)
2898 return FALSE;
2899 #ifdef UNIX
2901 struct stat st;
2903 /* If no struct stat given, get it now */
2904 if (stp == NULL)
2906 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
2907 st.st_dev = (dev_T)-1;
2908 stp = &st;
2910 /* Use dev/ino to check if the files are the same, even when the names
2911 * are different (possible with links). Still need to compare the
2912 * name above, for when the file doesn't exist yet.
2913 * Problem: The dev/ino changes when a file is deleted (and created
2914 * again) and remains the same when renamed/moved. We don't want to
2915 * mch_stat() each buffer each time, that would be too slow. Get the
2916 * dev/ino again when they appear to match, but not when they appear
2917 * to be different: Could skip a buffer when it's actually the same
2918 * file. */
2919 if (buf_same_ino(buf, stp))
2921 buf_setino(buf);
2922 if (buf_same_ino(buf, stp))
2923 return FALSE;
2926 #endif
2927 return TRUE;
2930 #if defined(UNIX) || defined(PROTO)
2932 * Set inode and device number for a buffer.
2933 * Must always be called when b_fname is changed!.
2935 void
2936 buf_setino(buf)
2937 buf_T *buf;
2939 struct stat st;
2941 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
2943 buf->b_dev_valid = TRUE;
2944 buf->b_dev = st.st_dev;
2945 buf->b_ino = st.st_ino;
2947 else
2948 buf->b_dev_valid = FALSE;
2952 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
2954 static int
2955 buf_same_ino(buf, stp)
2956 buf_T *buf;
2957 struct stat *stp;
2959 return (buf->b_dev_valid
2960 && stp->st_dev == buf->b_dev
2961 && stp->st_ino == buf->b_ino);
2963 #endif
2966 * Print info about the current buffer.
2968 void
2969 fileinfo(fullname, shorthelp, dont_truncate)
2970 int fullname; /* when non-zero print full path */
2971 int shorthelp;
2972 int dont_truncate;
2974 char_u *name;
2975 int n;
2976 char_u *p;
2977 char_u *buffer;
2978 size_t len;
2980 buffer = alloc(IOSIZE);
2981 if (buffer == NULL)
2982 return;
2984 if (fullname > 1) /* 2 CTRL-G: include buffer number */
2986 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
2987 p = buffer + STRLEN(buffer);
2989 else
2990 p = buffer;
2992 *p++ = '"';
2993 if (buf_spname(curbuf) != NULL)
2994 STRCPY(p, buf_spname(curbuf));
2995 else
2997 if (!fullname && curbuf->b_fname != NULL)
2998 name = curbuf->b_fname;
2999 else
3000 name = curbuf->b_ffname;
3001 home_replace(shorthelp ? curbuf : NULL, name, p,
3002 (int)(IOSIZE - (p - buffer)), TRUE);
3005 len = STRLEN(buffer);
3006 vim_snprintf((char *)buffer + len, IOSIZE - len,
3007 "\"%s%s%s%s%s%s",
3008 curbufIsChanged() ? (shortmess(SHM_MOD)
3009 ? " [+]" : _(" [Modified]")) : " ",
3010 (curbuf->b_flags & BF_NOTEDITED)
3011 #ifdef FEAT_QUICKFIX
3012 && !bt_dontwrite(curbuf)
3013 #endif
3014 ? _("[Not edited]") : "",
3015 (curbuf->b_flags & BF_NEW)
3016 #ifdef FEAT_QUICKFIX
3017 && !bt_dontwrite(curbuf)
3018 #endif
3019 ? _("[New file]") : "",
3020 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
3021 curbuf->b_p_ro ? (shortmess(SHM_RO) ? "[RO]"
3022 : _("[readonly]")) : "",
3023 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3024 || curbuf->b_p_ro) ?
3025 " " : "");
3026 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3027 * causes an overflow, thus for large numbers divide instead. */
3028 if (curwin->w_cursor.lnum > 1000000L)
3029 n = (int)(((long)curwin->w_cursor.lnum) /
3030 ((long)curbuf->b_ml.ml_line_count / 100L));
3031 else
3032 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3033 (long)curbuf->b_ml.ml_line_count);
3034 len = STRLEN(buffer);
3035 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3037 vim_snprintf((char *)buffer + len, IOSIZE - len, "%s", _(no_lines_msg));
3039 #ifdef FEAT_CMDL_INFO
3040 else if (p_ru)
3042 /* Current line and column are already on the screen -- webb */
3043 if (curbuf->b_ml.ml_line_count == 1)
3044 vim_snprintf((char *)buffer + len, IOSIZE - len,
3045 _("1 line --%d%%--"), n);
3046 else
3047 vim_snprintf((char *)buffer + len, IOSIZE - len,
3048 _("%ld lines --%d%%--"),
3049 (long)curbuf->b_ml.ml_line_count, n);
3051 #endif
3052 else
3054 vim_snprintf((char *)buffer + len, IOSIZE - len,
3055 _("line %ld of %ld --%d%%-- col "),
3056 (long)curwin->w_cursor.lnum,
3057 (long)curbuf->b_ml.ml_line_count,
3059 validate_virtcol();
3060 len = STRLEN(buffer);
3061 col_print(buffer + len, IOSIZE - len,
3062 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3065 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
3067 if (dont_truncate)
3069 /* Temporarily set msg_scroll to avoid the message being truncated.
3070 * First call msg_start() to get the message in the right place. */
3071 msg_start();
3072 n = msg_scroll;
3073 msg_scroll = TRUE;
3074 msg(buffer);
3075 msg_scroll = n;
3077 else
3079 p = msg_trunc_attr(buffer, FALSE, 0);
3080 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
3081 /* Need to repeat the message after redrawing when:
3082 * - When restart_edit is set (otherwise there will be a delay
3083 * before redrawing).
3084 * - When the screen was scrolled but there is no wait-return
3085 * prompt. */
3086 set_keep_msg(p, 0);
3089 vim_free(buffer);
3092 void
3093 col_print(buf, buflen, col, vcol)
3094 char_u *buf;
3095 size_t buflen;
3096 int col;
3097 int vcol;
3099 if (col == vcol)
3100 vim_snprintf((char *)buf, buflen, "%d", col);
3101 else
3102 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
3105 #if defined(FEAT_TITLE) || defined(PROTO)
3107 * put file name in title bar of window and in icon title
3110 static char_u *lasttitle = NULL;
3111 static char_u *lasticon = NULL;
3113 void
3114 maketitle()
3116 char_u *p;
3117 char_u *t_str = NULL;
3118 char_u *i_name;
3119 char_u *i_str = NULL;
3120 int maxlen = 0;
3121 int len;
3122 int mustset;
3123 char_u buf[IOSIZE];
3124 int off;
3126 if (!redrawing())
3128 /* Postpone updating the title when 'lazyredraw' is set. */
3129 need_maketitle = TRUE;
3130 return;
3133 need_maketitle = FALSE;
3134 if (!p_title && !p_icon)
3135 return;
3137 if (p_title)
3139 if (p_titlelen > 0)
3141 maxlen = p_titlelen * Columns / 100;
3142 if (maxlen < 10)
3143 maxlen = 10;
3146 t_str = buf;
3147 if (*p_titlestring != NUL)
3149 #ifdef FEAT_STL_OPT
3150 if (stl_syntax & STL_IN_TITLE)
3152 int use_sandbox = FALSE;
3153 int save_called_emsg = called_emsg;
3155 # ifdef FEAT_EVAL
3156 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
3157 # endif
3158 called_emsg = FALSE;
3159 build_stl_str_hl(curwin, t_str, sizeof(buf),
3160 p_titlestring, use_sandbox,
3161 0, maxlen, NULL, NULL);
3162 if (called_emsg)
3163 set_string_option_direct((char_u *)"titlestring", -1,
3164 (char_u *)"", OPT_FREE, SID_ERROR);
3165 called_emsg |= save_called_emsg;
3167 else
3168 #endif
3169 t_str = p_titlestring;
3171 else
3173 /* format: "fname + (path) (1 of 2) - VIM" */
3175 if (curbuf->b_fname == NULL)
3176 STRCPY(buf, _("[No Name]"));
3177 else
3179 p = transstr(gettail(curbuf->b_fname));
3180 vim_strncpy(buf, p, IOSIZE - 100);
3181 vim_free(p);
3184 switch (bufIsChanged(curbuf)
3185 + (curbuf->b_p_ro * 2)
3186 + (!curbuf->b_p_ma * 4))
3188 case 1: STRCAT(buf, " +"); break;
3189 case 2: STRCAT(buf, " ="); break;
3190 case 3: STRCAT(buf, " =+"); break;
3191 case 4:
3192 case 6: STRCAT(buf, " -"); break;
3193 case 5:
3194 case 7: STRCAT(buf, " -+"); break;
3197 if (curbuf->b_fname != NULL)
3199 /* Get path of file, replace home dir with ~ */
3200 off = (int)STRLEN(buf);
3201 buf[off++] = ' ';
3202 buf[off++] = '(';
3203 home_replace(curbuf, curbuf->b_ffname,
3204 buf + off, IOSIZE - off, TRUE);
3205 #ifdef BACKSLASH_IN_FILENAME
3206 /* avoid "c:/name" to be reduced to "c" */
3207 if (isalpha(buf[off]) && buf[off + 1] == ':')
3208 off += 2;
3209 #endif
3210 /* remove the file name */
3211 p = gettail_sep(buf + off);
3212 if (p == buf + off)
3213 /* must be a help buffer */
3214 vim_strncpy(buf + off, (char_u *)_("help"),
3215 (size_t)(IOSIZE - off - 1));
3216 else
3217 *p = NUL;
3219 /* translate unprintable chars */
3220 p = transstr(buf + off);
3221 vim_strncpy(buf + off, p, (size_t)(IOSIZE - off - 1));
3222 vim_free(p);
3223 STRCAT(buf, ")");
3226 append_arg_number(curwin, buf, IOSIZE, FALSE);
3228 #if defined(FEAT_CLIENTSERVER)
3229 if (serverName != NULL)
3231 STRCAT(buf, " - ");
3232 STRCAT(buf, serverName);
3234 else
3235 #endif
3236 STRCAT(buf, " - VIM");
3238 if (maxlen > 0)
3240 /* make it shorter by removing a bit in the middle */
3241 len = vim_strsize(buf);
3242 if (len > maxlen)
3243 trunc_string(buf, buf, maxlen);
3247 mustset = ti_change(t_str, &lasttitle);
3249 if (p_icon)
3251 i_str = buf;
3252 if (*p_iconstring != NUL)
3254 #ifdef FEAT_STL_OPT
3255 if (stl_syntax & STL_IN_ICON)
3257 int use_sandbox = FALSE;
3258 int save_called_emsg = called_emsg;
3260 # ifdef FEAT_EVAL
3261 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
3262 # endif
3263 called_emsg = FALSE;
3264 build_stl_str_hl(curwin, i_str, sizeof(buf),
3265 p_iconstring, use_sandbox,
3266 0, 0, NULL, NULL);
3267 if (called_emsg)
3268 set_string_option_direct((char_u *)"iconstring", -1,
3269 (char_u *)"", OPT_FREE, SID_ERROR);
3270 called_emsg |= save_called_emsg;
3272 else
3273 #endif
3274 i_str = p_iconstring;
3276 else
3278 if (buf_spname(curbuf) != NULL)
3279 i_name = (char_u *)buf_spname(curbuf);
3280 else /* use file name only in icon */
3281 i_name = gettail(curbuf->b_ffname);
3282 *i_str = NUL;
3283 /* Truncate name at 100 bytes. */
3284 len = (int)STRLEN(i_name);
3285 if (len > 100)
3287 len -= 100;
3288 #ifdef FEAT_MBYTE
3289 if (has_mbyte)
3290 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3291 #endif
3292 i_name += len;
3294 STRCPY(i_str, i_name);
3295 trans_characters(i_str, IOSIZE);
3299 mustset |= ti_change(i_str, &lasticon);
3301 if (mustset)
3302 resettitle();
3306 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3307 * from "str" if it does.
3308 * Return TRUE when "*last" changed.
3310 static int
3311 ti_change(str, last)
3312 char_u *str;
3313 char_u **last;
3315 if ((str == NULL) != (*last == NULL)
3316 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3318 vim_free(*last);
3319 if (str == NULL)
3320 *last = NULL;
3321 else
3322 *last = vim_strsave(str);
3323 return TRUE;
3325 return FALSE;
3329 * Put current window title back (used after calling a shell)
3331 void
3332 resettitle()
3334 mch_settitle(lasttitle, lasticon);
3337 # if defined(EXITFREE) || defined(PROTO)
3338 void
3339 free_titles()
3341 vim_free(lasttitle);
3342 vim_free(lasticon);
3344 # endif
3346 #endif /* FEAT_TITLE */
3348 #if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
3350 * Build a string from the status line items in "fmt".
3351 * Return length of string in screen cells.
3353 * Normally works for window "wp", except when working for 'tabline' then it
3354 * is "curwin".
3356 * Items are drawn interspersed with the text that surrounds it
3357 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3358 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3360 * If maxwidth is not zero, the string will be filled at any middle marker
3361 * or truncated if too long, fillchar is used for all whitespace.
3364 build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar, maxwidth, hltab, tabtab)
3365 win_T *wp;
3366 char_u *out; /* buffer to write into != NameBuff */
3367 size_t outlen; /* length of out[] */
3368 char_u *fmt;
3369 int use_sandbox UNUSED; /* "fmt" was set insecurely, use sandbox */
3370 int fillchar;
3371 int maxwidth;
3372 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3373 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
3375 char_u *p;
3376 char_u *s;
3377 char_u *t;
3378 char_u *linecont;
3379 #ifdef FEAT_EVAL
3380 win_T *o_curwin;
3381 buf_T *o_curbuf;
3382 #endif
3383 int empty_line;
3384 colnr_T virtcol;
3385 long l;
3386 long n;
3387 int prevchar_isflag;
3388 int prevchar_isitem;
3389 int itemisflag;
3390 int fillable;
3391 char_u *str;
3392 long num;
3393 int width;
3394 int itemcnt;
3395 int curitem;
3396 int groupitem[STL_MAX_ITEM];
3397 int groupdepth;
3398 struct stl_item
3400 char_u *start;
3401 int minwid;
3402 int maxwid;
3403 enum
3405 Normal,
3406 Empty,
3407 Group,
3408 Middle,
3409 Highlight,
3410 TabPage,
3411 Trunc
3412 } type;
3413 } item[STL_MAX_ITEM];
3414 int minwid;
3415 int maxwid;
3416 int zeropad;
3417 char_u base;
3418 char_u opt;
3419 #define TMPLEN 70
3420 char_u tmp[TMPLEN];
3421 char_u *usefmt = fmt;
3422 struct stl_hlrec *sp;
3424 #ifdef FEAT_EVAL
3426 * When the format starts with "%!" then evaluate it as an expression and
3427 * use the result as the actual format string.
3429 if (fmt[0] == '%' && fmt[1] == '!')
3431 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3432 if (usefmt == NULL)
3433 usefmt = fmt;
3435 #endif
3437 if (fillchar == 0)
3438 fillchar = ' ';
3439 #ifdef FEAT_MBYTE
3440 /* Can't handle a multi-byte fill character yet. */
3441 else if (mb_char2len(fillchar) > 1)
3442 fillchar = '-';
3443 #endif
3446 * Get line & check if empty (cursorpos will show "0-1").
3447 * If inversion is possible we use it. Else '=' characters are used.
3449 linecont = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3450 empty_line = (*linecont == NUL);
3452 groupdepth = 0;
3453 p = out;
3454 curitem = 0;
3455 prevchar_isflag = TRUE;
3456 prevchar_isitem = FALSE;
3457 for (s = usefmt; *s; )
3459 if (*s != NUL && *s != '%')
3460 prevchar_isflag = prevchar_isitem = FALSE;
3463 * Handle up to the next '%' or the end.
3465 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3466 *p++ = *s++;
3467 if (*s == NUL || p + 1 >= out + outlen)
3468 break;
3471 * Handle one '%' item.
3473 s++;
3474 if (*s == '%')
3476 if (p + 1 >= out + outlen)
3477 break;
3478 *p++ = *s++;
3479 prevchar_isflag = prevchar_isitem = FALSE;
3480 continue;
3482 if (*s == STL_MIDDLEMARK)
3484 s++;
3485 if (groupdepth > 0)
3486 continue;
3487 item[curitem].type = Middle;
3488 item[curitem++].start = p;
3489 continue;
3491 if (*s == STL_TRUNCMARK)
3493 s++;
3494 item[curitem].type = Trunc;
3495 item[curitem++].start = p;
3496 continue;
3498 if (*s == ')')
3500 s++;
3501 if (groupdepth < 1)
3502 continue;
3503 groupdepth--;
3505 t = item[groupitem[groupdepth]].start;
3506 *p = NUL;
3507 l = vim_strsize(t);
3508 if (curitem > groupitem[groupdepth] + 1
3509 && item[groupitem[groupdepth]].minwid == 0)
3511 /* remove group if all items are empty */
3512 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3513 if (item[n].type == Normal)
3514 break;
3515 if (n == curitem)
3517 p = t;
3518 l = 0;
3521 if (l > item[groupitem[groupdepth]].maxwid)
3523 /* truncate, remove n bytes of text at the start */
3524 #ifdef FEAT_MBYTE
3525 if (has_mbyte)
3527 /* Find the first character that should be included. */
3528 n = 0;
3529 while (l >= item[groupitem[groupdepth]].maxwid)
3531 l -= ptr2cells(t + n);
3532 n += (*mb_ptr2len)(t + n);
3535 else
3536 #endif
3537 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
3539 *t = '<';
3540 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
3541 p = p - n + 1;
3542 #ifdef FEAT_MBYTE
3543 /* Fill up space left over by half a double-wide char. */
3544 while (++l < item[groupitem[groupdepth]].minwid)
3545 *p++ = fillchar;
3546 #endif
3548 /* correct the start of the items for the truncation */
3549 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3551 item[l].start -= n;
3552 if (item[l].start < t)
3553 item[l].start = t;
3556 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3558 /* fill */
3559 n = item[groupitem[groupdepth]].minwid;
3560 if (n < 0)
3562 /* fill by appending characters */
3563 n = 0 - n;
3564 while (l++ < n && p + 1 < out + outlen)
3565 *p++ = fillchar;
3567 else
3569 /* fill by inserting characters */
3570 mch_memmove(t + n - l, t, (size_t)(p - t));
3571 l = n - l;
3572 if (p + l >= out + outlen)
3573 l = (long)((out + outlen) - p - 1);
3574 p += l;
3575 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3576 item[n].start += l;
3577 for ( ; l > 0; l--)
3578 *t++ = fillchar;
3581 continue;
3583 minwid = 0;
3584 maxwid = 9999;
3585 zeropad = FALSE;
3586 l = 1;
3587 if (*s == '0')
3589 s++;
3590 zeropad = TRUE;
3592 if (*s == '-')
3594 s++;
3595 l = -1;
3597 if (VIM_ISDIGIT(*s))
3599 minwid = (int)getdigits(&s);
3600 if (minwid < 0) /* overflow */
3601 minwid = 0;
3603 if (*s == STL_USER_HL)
3605 item[curitem].type = Highlight;
3606 item[curitem].start = p;
3607 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3608 s++;
3609 curitem++;
3610 continue;
3612 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3614 if (*s == STL_TABCLOSENR)
3616 if (minwid == 0)
3618 /* %X ends the close label, go back to the previously
3619 * define tab label nr. */
3620 for (n = curitem - 1; n >= 0; --n)
3621 if (item[n].type == TabPage && item[n].minwid >= 0)
3623 minwid = item[n].minwid;
3624 break;
3627 else
3628 /* close nrs are stored as negative values */
3629 minwid = - minwid;
3631 item[curitem].type = TabPage;
3632 item[curitem].start = p;
3633 item[curitem].minwid = minwid;
3634 s++;
3635 curitem++;
3636 continue;
3638 if (*s == '.')
3640 s++;
3641 if (VIM_ISDIGIT(*s))
3643 maxwid = (int)getdigits(&s);
3644 if (maxwid <= 0) /* overflow */
3645 maxwid = 50;
3648 minwid = (minwid > 50 ? 50 : minwid) * l;
3649 if (*s == '(')
3651 groupitem[groupdepth++] = curitem;
3652 item[curitem].type = Group;
3653 item[curitem].start = p;
3654 item[curitem].minwid = minwid;
3655 item[curitem].maxwid = maxwid;
3656 s++;
3657 curitem++;
3658 continue;
3660 if (vim_strchr(STL_ALL, *s) == NULL)
3662 s++;
3663 continue;
3665 opt = *s++;
3667 /* OK - now for the real work */
3668 base = 'D';
3669 itemisflag = FALSE;
3670 fillable = TRUE;
3671 num = -1;
3672 str = NULL;
3673 switch (opt)
3675 case STL_FILEPATH:
3676 case STL_FULLPATH:
3677 case STL_FILENAME:
3678 fillable = FALSE; /* don't change ' ' to fillchar */
3679 if (buf_spname(wp->w_buffer) != NULL)
3680 STRCPY(NameBuff, buf_spname(wp->w_buffer));
3681 else
3683 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
3684 : wp->w_buffer->b_fname;
3685 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3687 trans_characters(NameBuff, MAXPATHL);
3688 if (opt != STL_FILENAME)
3689 str = NameBuff;
3690 else
3691 str = gettail(NameBuff);
3692 break;
3694 case STL_VIM_EXPR: /* '{' */
3695 itemisflag = TRUE;
3696 t = p;
3697 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3698 *p++ = *s++;
3699 if (*s != '}') /* missing '}' or out of space */
3700 break;
3701 s++;
3702 *p = 0;
3703 p = t;
3705 #ifdef FEAT_EVAL
3706 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
3707 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3709 o_curbuf = curbuf;
3710 o_curwin = curwin;
3711 curwin = wp;
3712 curbuf = wp->w_buffer;
3714 str = eval_to_string_safe(p, &t, use_sandbox);
3716 curwin = o_curwin;
3717 curbuf = o_curbuf;
3718 do_unlet((char_u *)"g:actual_curbuf", TRUE);
3720 if (str != NULL && *str != 0)
3722 if (*skipdigits(str) == NUL)
3724 num = atoi((char *)str);
3725 vim_free(str);
3726 str = NULL;
3727 itemisflag = FALSE;
3730 #endif
3731 break;
3733 case STL_LINE:
3734 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3735 ? 0L : (long)(wp->w_cursor.lnum);
3736 break;
3738 case STL_NUMLINES:
3739 num = wp->w_buffer->b_ml.ml_line_count;
3740 break;
3742 case STL_COLUMN:
3743 num = !(State & INSERT) && empty_line
3744 ? 0 : (int)wp->w_cursor.col + 1;
3745 break;
3747 case STL_VIRTCOL:
3748 case STL_VIRTCOL_ALT:
3749 /* In list mode virtcol needs to be recomputed */
3750 virtcol = wp->w_virtcol;
3751 if (wp->w_p_list && lcs_tab1 == NUL)
3753 wp->w_p_list = FALSE;
3754 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3755 wp->w_p_list = TRUE;
3757 ++virtcol;
3758 /* Don't display %V if it's the same as %c. */
3759 if (opt == STL_VIRTCOL_ALT
3760 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3761 ? 0 : (int)wp->w_cursor.col + 1)))
3762 break;
3763 num = (long)virtcol;
3764 break;
3766 case STL_PERCENTAGE:
3767 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3768 (long)wp->w_buffer->b_ml.ml_line_count);
3769 break;
3771 case STL_ALTPERCENT:
3772 str = tmp;
3773 get_rel_pos(wp, str, TMPLEN);
3774 break;
3776 case STL_ARGLISTSTAT:
3777 fillable = FALSE;
3778 tmp[0] = 0;
3779 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
3780 str = tmp;
3781 break;
3783 case STL_KEYMAP:
3784 fillable = FALSE;
3785 if (get_keymap_str(wp, tmp, TMPLEN))
3786 str = tmp;
3787 break;
3788 case STL_PAGENUM:
3789 #if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
3790 num = printer_page_num;
3791 #else
3792 num = 0;
3793 #endif
3794 break;
3796 case STL_BUFNO:
3797 num = wp->w_buffer->b_fnum;
3798 break;
3800 case STL_OFFSET_X:
3801 base = 'X';
3802 case STL_OFFSET:
3803 #ifdef FEAT_BYTEOFF
3804 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
3805 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
3806 0L : l + 1 + (!(State & INSERT) && empty_line ?
3807 0 : (int)wp->w_cursor.col);
3808 #endif
3809 break;
3811 case STL_BYTEVAL_X:
3812 base = 'X';
3813 case STL_BYTEVAL:
3814 if (wp->w_cursor.col > (colnr_T)STRLEN(linecont))
3815 num = 0;
3816 else
3818 #ifdef FEAT_MBYTE
3819 num = (*mb_ptr2char)(linecont + wp->w_cursor.col);
3820 #else
3821 num = linecont[wp->w_cursor.col];
3822 #endif
3824 if (num == NL)
3825 num = 0;
3826 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
3827 num = NL;
3828 break;
3830 case STL_ROFLAG:
3831 case STL_ROFLAG_ALT:
3832 itemisflag = TRUE;
3833 if (wp->w_buffer->b_p_ro)
3834 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : "[RO]");
3835 break;
3837 case STL_HELPFLAG:
3838 case STL_HELPFLAG_ALT:
3839 itemisflag = TRUE;
3840 if (wp->w_buffer->b_help)
3841 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
3842 : _("[Help]"));
3843 break;
3845 #ifdef FEAT_AUTOCMD
3846 case STL_FILETYPE:
3847 if (*wp->w_buffer->b_p_ft != NUL
3848 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
3850 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
3851 wp->w_buffer->b_p_ft);
3852 str = tmp;
3854 break;
3856 case STL_FILETYPE_ALT:
3857 itemisflag = TRUE;
3858 if (*wp->w_buffer->b_p_ft != NUL
3859 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
3861 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
3862 wp->w_buffer->b_p_ft);
3863 for (t = tmp; *t != 0; t++)
3864 *t = TOUPPER_LOC(*t);
3865 str = tmp;
3867 break;
3868 #endif
3870 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3871 case STL_PREVIEWFLAG:
3872 case STL_PREVIEWFLAG_ALT:
3873 itemisflag = TRUE;
3874 if (wp->w_p_pvw)
3875 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
3876 : _("[Preview]"));
3877 break;
3878 #endif
3880 case STL_MODIFIED:
3881 case STL_MODIFIED_ALT:
3882 itemisflag = TRUE;
3883 switch ((opt == STL_MODIFIED_ALT)
3884 + bufIsChanged(wp->w_buffer) * 2
3885 + (!wp->w_buffer->b_p_ma) * 4)
3887 case 2: str = (char_u *)"[+]"; break;
3888 case 3: str = (char_u *)",+"; break;
3889 case 4: str = (char_u *)"[-]"; break;
3890 case 5: str = (char_u *)",-"; break;
3891 case 6: str = (char_u *)"[+-]"; break;
3892 case 7: str = (char_u *)",+-"; break;
3894 break;
3896 case STL_HIGHLIGHT:
3897 t = s;
3898 while (*s != '#' && *s != NUL)
3899 ++s;
3900 if (*s == '#')
3902 item[curitem].type = Highlight;
3903 item[curitem].start = p;
3904 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
3905 curitem++;
3907 ++s;
3908 continue;
3911 item[curitem].start = p;
3912 item[curitem].type = Normal;
3913 if (str != NULL && *str)
3915 t = str;
3916 if (itemisflag)
3918 if ((t[0] && t[1])
3919 && ((!prevchar_isitem && *t == ',')
3920 || (prevchar_isflag && *t == ' ')))
3921 t++;
3922 prevchar_isflag = TRUE;
3924 l = vim_strsize(t);
3925 if (l > 0)
3926 prevchar_isitem = TRUE;
3927 if (l > maxwid)
3929 while (l >= maxwid)
3930 #ifdef FEAT_MBYTE
3931 if (has_mbyte)
3933 l -= ptr2cells(t);
3934 t += (*mb_ptr2len)(t);
3936 else
3937 #endif
3938 l -= byte2cells(*t++);
3939 if (p + 1 >= out + outlen)
3940 break;
3941 *p++ = '<';
3943 if (minwid > 0)
3945 for (; l < minwid && p + 1 < out + outlen; l++)
3947 /* Don't put a "-" in front of a digit. */
3948 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
3949 *p++ = ' ';
3950 else
3951 *p++ = fillchar;
3953 minwid = 0;
3955 else
3956 minwid *= -1;
3957 while (*t && p + 1 < out + outlen)
3959 *p++ = *t++;
3960 /* Change a space by fillchar, unless fillchar is '-' and a
3961 * digit follows. */
3962 if (fillable && p[-1] == ' '
3963 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
3964 p[-1] = fillchar;
3966 for (; l < minwid && p + 1 < out + outlen; l++)
3967 *p++ = fillchar;
3969 else if (num >= 0)
3971 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
3972 char_u nstr[20];
3974 if (p + 20 >= out + outlen)
3975 break; /* not sufficient space */
3976 prevchar_isitem = TRUE;
3977 t = nstr;
3978 if (opt == STL_VIRTCOL_ALT)
3980 *t++ = '-';
3981 minwid--;
3983 *t++ = '%';
3984 if (zeropad)
3985 *t++ = '0';
3986 *t++ = '*';
3987 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
3988 *t = 0;
3990 for (n = num, l = 1; n >= nbase; n /= nbase)
3991 l++;
3992 if (opt == STL_VIRTCOL_ALT)
3993 l++;
3994 if (l > maxwid)
3996 l += 2;
3997 n = l - maxwid;
3998 while (l-- > maxwid)
3999 num /= nbase;
4000 *t++ = '>';
4001 *t++ = '%';
4002 *t = t[-3];
4003 *++t = 0;
4004 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4005 0, num, n);
4007 else
4008 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4009 minwid, num);
4010 p += STRLEN(p);
4012 else
4013 item[curitem].type = Empty;
4015 if (opt == STL_VIM_EXPR)
4016 vim_free(str);
4018 if (num >= 0 || (!itemisflag && str && *str))
4019 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4020 curitem++;
4022 *p = NUL;
4023 itemcnt = curitem;
4025 #ifdef FEAT_EVAL
4026 if (usefmt != fmt)
4027 vim_free(usefmt);
4028 #endif
4030 width = vim_strsize(out);
4031 if (maxwidth > 0 && width > maxwidth)
4033 /* Result is too long, must truncate somewhere. */
4034 l = 0;
4035 if (itemcnt == 0)
4036 s = out;
4037 else
4039 for ( ; l < itemcnt; l++)
4040 if (item[l].type == Trunc)
4042 /* Truncate at %< item. */
4043 s = item[l].start;
4044 break;
4046 if (l == itemcnt)
4048 /* No %< item, truncate first item. */
4049 s = item[0].start;
4050 l = 0;
4054 if (width - vim_strsize(s) >= maxwidth)
4056 /* Truncation mark is beyond max length */
4057 #ifdef FEAT_MBYTE
4058 if (has_mbyte)
4060 s = out;
4061 width = 0;
4062 for (;;)
4064 width += ptr2cells(s);
4065 if (width >= maxwidth)
4066 break;
4067 s += (*mb_ptr2len)(s);
4069 /* Fill up for half a double-wide character. */
4070 while (++width < maxwidth)
4071 *s++ = fillchar;
4073 else
4074 #endif
4075 s = out + maxwidth - 1;
4076 for (l = 0; l < itemcnt; l++)
4077 if (item[l].start > s)
4078 break;
4079 itemcnt = l;
4080 *s++ = '>';
4081 *s = 0;
4083 else
4085 #ifdef FEAT_MBYTE
4086 if (has_mbyte)
4088 n = 0;
4089 while (width >= maxwidth)
4091 width -= ptr2cells(s + n);
4092 n += (*mb_ptr2len)(s + n);
4095 else
4096 #endif
4097 n = width - maxwidth + 1;
4098 p = s + n;
4099 STRMOVE(s + 1, p);
4100 *s = '<';
4102 /* Fill up for half a double-wide character. */
4103 while (++width < maxwidth)
4105 s = s + STRLEN(s);
4106 *s++ = fillchar;
4107 *s = NUL;
4110 --n; /* count the '<' */
4111 for (; l < itemcnt; l++)
4113 if (item[l].start - n >= s)
4114 item[l].start -= n;
4115 else
4116 item[l].start = s;
4119 width = maxwidth;
4121 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4123 /* Apply STL_MIDDLE if any */
4124 for (l = 0; l < itemcnt; l++)
4125 if (item[l].type == Middle)
4126 break;
4127 if (l < itemcnt)
4129 p = item[l].start + maxwidth - width;
4130 STRMOVE(p, item[l].start);
4131 for (s = item[l].start; s < p; s++)
4132 *s = fillchar;
4133 for (l++; l < itemcnt; l++)
4134 item[l].start += maxwidth - width;
4135 width = maxwidth;
4139 /* Store the info about highlighting. */
4140 if (hltab != NULL)
4142 sp = hltab;
4143 for (l = 0; l < itemcnt; l++)
4145 if (item[l].type == Highlight)
4147 sp->start = item[l].start;
4148 sp->userhl = item[l].minwid;
4149 sp++;
4152 sp->start = NULL;
4153 sp->userhl = 0;
4156 /* Store the info about tab pages labels. */
4157 if (tabtab != NULL)
4159 sp = tabtab;
4160 for (l = 0; l < itemcnt; l++)
4162 if (item[l].type == TabPage)
4164 sp->start = item[l].start;
4165 sp->userhl = item[l].minwid;
4166 sp++;
4169 sp->start = NULL;
4170 sp->userhl = 0;
4173 return width;
4175 #endif /* FEAT_STL_OPT */
4177 #if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4178 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
4180 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4181 * using "Top", "Bot" or "All" when appropriate.
4183 void
4184 get_rel_pos(wp, buf, buflen)
4185 win_T *wp;
4186 char_u *buf;
4187 int buflen;
4189 long above; /* number of lines above window */
4190 long below; /* number of lines below window */
4192 above = wp->w_topline - 1;
4193 #ifdef FEAT_DIFF
4194 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
4195 #endif
4196 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4197 if (below <= 0)
4198 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4199 (size_t)(buflen - 1));
4200 else if (above <= 0)
4201 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
4202 else
4203 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
4204 ? (int)(above / ((above + below) / 100L))
4205 : (int)(above * 100L / (above + below)));
4207 #endif
4210 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
4211 * Return TRUE if it was appended.
4213 static int
4214 append_arg_number(wp, buf, buflen, add_file)
4215 win_T *wp;
4216 char_u *buf;
4217 int buflen;
4218 int add_file; /* Add "file" before the arg number */
4220 char_u *p;
4222 if (ARGCOUNT <= 1) /* nothing to do */
4223 return FALSE;
4225 p = buf + STRLEN(buf); /* go to the end of the buffer */
4226 if (p - buf + 35 >= buflen) /* getting too long */
4227 return FALSE;
4228 *p++ = ' ';
4229 *p++ = '(';
4230 if (add_file)
4232 STRCPY(p, "file ");
4233 p += 5;
4235 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4236 wp->w_arg_idx_invalid ? "(%d) of %d)"
4237 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4238 return TRUE;
4242 * If fname is not a full path, make it a full path.
4243 * Returns pointer to allocated memory (NULL for failure).
4245 char_u *
4246 fix_fname(fname)
4247 char_u *fname;
4250 * Force expanding the path always for Unix, because symbolic links may
4251 * mess up the full path name, even though it starts with a '/'.
4252 * Also expand when there is ".." in the file name, try to remove it,
4253 * because "c:/src/../README" is equal to "c:/README".
4254 * Similarly "c:/src//file" is equal to "c:/src/file".
4255 * For MS-Windows also expand names like "longna~1" to "longname".
4257 #ifdef UNIX
4258 return FullName_save(fname, TRUE);
4259 #else
4260 if (!vim_isAbsName(fname)
4261 || strstr((char *)fname, "..") != NULL
4262 || strstr((char *)fname, "//") != NULL
4263 # ifdef BACKSLASH_IN_FILENAME
4264 || strstr((char *)fname, "\\\\") != NULL
4265 # endif
4266 # if defined(MSWIN) || defined(DJGPP)
4267 || vim_strchr(fname, '~') != NULL
4268 # endif
4270 return FullName_save(fname, FALSE);
4272 fname = vim_strsave(fname);
4274 # ifdef USE_FNAME_CASE
4275 # ifdef USE_LONG_FNAME
4276 if (USE_LONG_FNAME)
4277 # endif
4279 if (fname != NULL)
4280 fname_case(fname, 0); /* set correct case for file name */
4282 # endif
4284 return fname;
4285 #endif
4289 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4290 * "ffname" becomes a pointer to allocated memory (or NULL).
4292 void
4293 fname_expand(buf, ffname, sfname)
4294 buf_T *buf UNUSED;
4295 char_u **ffname;
4296 char_u **sfname;
4298 if (*ffname == NULL) /* if no file name given, nothing to do */
4299 return;
4300 if (*sfname == NULL) /* if no short file name given, use ffname */
4301 *sfname = *ffname;
4302 *ffname = fix_fname(*ffname); /* expand to full path */
4304 #ifdef FEAT_SHORTCUT
4305 if (!buf->b_p_bin)
4307 char_u *rfname;
4309 /* If the file name is a shortcut file, use the file it links to. */
4310 rfname = mch_resolve_shortcut(*ffname);
4311 if (rfname != NULL)
4313 vim_free(*ffname);
4314 *ffname = rfname;
4315 *sfname = rfname;
4318 #endif
4322 * Get the file name for an argument list entry.
4324 char_u *
4325 alist_name(aep)
4326 aentry_T *aep;
4328 buf_T *bp;
4330 /* Use the name from the associated buffer if it exists. */
4331 bp = buflist_findnr(aep->ae_fnum);
4332 if (bp == NULL || bp->b_fname == NULL)
4333 return aep->ae_fname;
4334 return bp->b_fname;
4337 #if defined(FEAT_WINDOWS) || defined(PROTO)
4339 * do_arg_all(): Open up to 'count' windows, one for each argument.
4341 void
4342 do_arg_all(count, forceit, keep_tabs)
4343 int count;
4344 int forceit; /* hide buffers in current windows */
4345 int keep_tabs; /* keep current tabs, for ":tab drop file" */
4347 int i;
4348 win_T *wp, *wpnext;
4349 char_u *opened; /* array of flags for which args are open */
4350 int opened_len; /* length of opened[] */
4351 int use_firstwin = FALSE; /* use first window for arglist */
4352 int split_ret = OK;
4353 int p_ea_save;
4354 alist_T *alist; /* argument list to be used */
4355 buf_T *buf;
4356 tabpage_T *tpnext;
4357 int had_tab = cmdmod.tab;
4358 win_T *new_curwin = NULL;
4359 tabpage_T *new_curtab = NULL;
4361 if (ARGCOUNT <= 0)
4363 /* Don't give an error message. We don't want it when the ":all"
4364 * command is in the .vimrc. */
4365 return;
4367 setpcmark();
4369 opened_len = ARGCOUNT;
4370 opened = alloc_clear((unsigned)opened_len);
4371 if (opened == NULL)
4372 return;
4374 #ifdef FEAT_GUI
4375 need_mouse_correct = TRUE;
4376 #endif
4379 * Try closing all windows that are not in the argument list.
4380 * Also close windows that are not full width;
4381 * When 'hidden' or "forceit" set the buffer becomes hidden.
4382 * Windows that have a changed buffer and can't be hidden won't be closed.
4383 * When the ":tab" modifier was used do this for all tab pages.
4385 if (had_tab > 0)
4386 goto_tabpage_tp(first_tabpage);
4387 for (;;)
4389 tpnext = curtab->tp_next;
4390 for (wp = firstwin; wp != NULL; wp = wpnext)
4392 wpnext = wp->w_next;
4393 buf = wp->w_buffer;
4394 if (buf->b_ffname == NULL
4395 || buf->b_nwindows > 1
4396 #ifdef FEAT_VERTSPLIT
4397 || wp->w_width != Columns
4398 #endif
4400 i = ARGCOUNT;
4401 else
4403 /* check if the buffer in this window is in the arglist */
4404 for (i = 0; i < ARGCOUNT; ++i)
4406 if (ARGLIST[i].ae_fnum == buf->b_fnum
4407 || fullpathcmp(alist_name(&ARGLIST[i]),
4408 buf->b_ffname, TRUE) & FPC_SAME)
4410 if (i < opened_len)
4412 opened[i] = TRUE;
4413 if (i == 0)
4415 new_curwin = wp;
4416 new_curtab = curtab;
4419 if (wp->w_alist != curwin->w_alist)
4421 /* Use the current argument list for all windows
4422 * containing a file from it. */
4423 alist_unlink(wp->w_alist);
4424 wp->w_alist = curwin->w_alist;
4425 ++wp->w_alist->al_refcount;
4427 break;
4431 wp->w_arg_idx = i;
4433 if (i == ARGCOUNT && !keep_tabs) /* close this window */
4435 if (P_HID(buf) || forceit || buf->b_nwindows > 1
4436 || !bufIsChanged(buf))
4438 /* If the buffer was changed, and we would like to hide it,
4439 * try autowriting. */
4440 if (!P_HID(buf) && buf->b_nwindows <= 1
4441 && bufIsChanged(buf))
4443 (void)autowrite(buf, FALSE);
4444 #ifdef FEAT_AUTOCMD
4445 /* check if autocommands removed the window */
4446 if (!win_valid(wp) || !buf_valid(buf))
4448 wpnext = firstwin; /* start all over... */
4449 continue;
4451 #endif
4453 #ifdef FEAT_WINDOWS
4454 /* don't close last window */
4455 if (firstwin == lastwin && first_tabpage->tp_next == NULL)
4456 #endif
4457 use_firstwin = TRUE;
4458 #ifdef FEAT_WINDOWS
4459 else
4461 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4462 # ifdef FEAT_AUTOCMD
4463 /* check if autocommands removed the next window */
4464 if (!win_valid(wpnext))
4465 wpnext = firstwin; /* start all over... */
4466 # endif
4468 #endif
4473 /* Without the ":tab" modifier only do the current tab page. */
4474 if (had_tab == 0 || tpnext == NULL)
4475 break;
4477 # ifdef FEAT_AUTOCMD
4478 /* check if autocommands removed the next tab page */
4479 if (!valid_tabpage(tpnext))
4480 tpnext = first_tabpage; /* start all over...*/
4481 # endif
4482 goto_tabpage_tp(tpnext);
4486 * Open a window for files in the argument list that don't have one.
4487 * ARGCOUNT may change while doing this, because of autocommands.
4489 if (count > ARGCOUNT || count <= 0)
4490 count = ARGCOUNT;
4492 /* Autocommands may do anything to the argument list. Make sure it's not
4493 * freed while we are working here by "locking" it. We still have to
4494 * watch out for its size to be changed. */
4495 alist = curwin->w_alist;
4496 ++alist->al_refcount;
4498 #ifdef FEAT_AUTOCMD
4499 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4500 ++autocmd_no_enter;
4501 ++autocmd_no_leave;
4502 #endif
4503 win_enter(lastwin, FALSE);
4504 #ifdef FEAT_WINDOWS
4505 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4506 * leaving an empty tab page when executed locally. */
4507 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4508 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4509 use_firstwin = TRUE;
4510 #endif
4512 for (i = 0; i < count && i < alist->al_ga.ga_len && !got_int; ++i)
4514 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4515 arg_had_last = TRUE;
4516 if (i < opened_len && opened[i])
4518 /* Move the already present window to below the current window */
4519 if (curwin->w_arg_idx != i)
4521 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4523 if (wpnext->w_arg_idx == i)
4525 win_move_after(wpnext, curwin);
4526 break;
4531 else if (split_ret == OK)
4533 if (!use_firstwin) /* split current window */
4535 p_ea_save = p_ea;
4536 p_ea = TRUE; /* use space from all windows */
4537 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4538 p_ea = p_ea_save;
4539 if (split_ret == FAIL)
4540 continue;
4542 #ifdef FEAT_AUTOCMD
4543 else /* first window: do autocmd for leaving this buffer */
4544 --autocmd_no_leave;
4545 #endif
4548 * edit file "i"
4550 curwin->w_arg_idx = i;
4551 if (i == 0)
4553 new_curwin = curwin;
4554 new_curtab = curtab;
4556 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4557 ECMD_ONE,
4558 ((P_HID(curwin->w_buffer)
4559 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
4560 + ECMD_OLDBUF, curwin);
4561 #ifdef FEAT_AUTOCMD
4562 if (use_firstwin)
4563 ++autocmd_no_leave;
4564 #endif
4565 use_firstwin = FALSE;
4567 ui_breakcheck();
4569 /* When ":tab" was used open a new tab for a new window repeatedly. */
4570 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4571 cmdmod.tab = 9999;
4574 /* Remove the "lock" on the argument list. */
4575 alist_unlink(alist);
4577 #ifdef FEAT_AUTOCMD
4578 --autocmd_no_enter;
4579 #endif
4580 /* to window with first arg */
4581 if (valid_tabpage(new_curtab))
4582 goto_tabpage_tp(new_curtab);
4583 if (win_valid(new_curwin))
4584 win_enter(new_curwin, FALSE);
4586 #ifdef FEAT_AUTOCMD
4587 --autocmd_no_leave;
4588 #endif
4589 vim_free(opened);
4592 # if defined(FEAT_LISTCMDS) || defined(PROTO)
4594 * Open a window for a number of buffers.
4596 void
4597 ex_buffer_all(eap)
4598 exarg_T *eap;
4600 buf_T *buf;
4601 win_T *wp, *wpnext;
4602 int split_ret = OK;
4603 int p_ea_save;
4604 int open_wins = 0;
4605 int r;
4606 int count; /* Maximum number of windows to open. */
4607 int all; /* When TRUE also load inactive buffers. */
4608 #ifdef FEAT_WINDOWS
4609 int had_tab = cmdmod.tab;
4610 tabpage_T *tpnext;
4611 #endif
4613 if (eap->addr_count == 0) /* make as many windows as possible */
4614 count = 9999;
4615 else
4616 count = eap->line2; /* make as many windows as specified */
4617 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4618 all = FALSE;
4619 else
4620 all = TRUE;
4622 setpcmark();
4624 #ifdef FEAT_GUI
4625 need_mouse_correct = TRUE;
4626 #endif
4629 * Close superfluous windows (two windows for the same buffer).
4630 * Also close windows that are not full-width.
4632 #ifdef FEAT_WINDOWS
4633 if (had_tab > 0)
4634 goto_tabpage_tp(first_tabpage);
4635 for (;;)
4637 #endif
4638 tpnext = curtab->tp_next;
4639 for (wp = firstwin; wp != NULL; wp = wpnext)
4641 wpnext = wp->w_next;
4642 if ((wp->w_buffer->b_nwindows > 1
4643 #ifdef FEAT_VERTSPLIT
4644 || ((cmdmod.split & WSP_VERT)
4645 ? wp->w_height + wp->w_status_height < Rows - p_ch
4646 - tabline_height()
4647 : wp->w_width != Columns)
4648 #endif
4649 #ifdef FEAT_WINDOWS
4650 || (had_tab > 0 && wp != firstwin)
4651 #endif
4652 ) && firstwin != lastwin)
4654 win_close(wp, FALSE);
4655 #ifdef FEAT_AUTOCMD
4656 wpnext = firstwin; /* just in case an autocommand does
4657 something strange with windows */
4658 tpnext = first_tabpage; /* start all over...*/
4659 open_wins = 0;
4660 #endif
4662 else
4663 ++open_wins;
4666 #ifdef FEAT_WINDOWS
4667 /* Without the ":tab" modifier only do the current tab page. */
4668 if (had_tab == 0 || tpnext == NULL)
4669 break;
4670 goto_tabpage_tp(tpnext);
4672 #endif
4675 * Go through the buffer list. When a buffer doesn't have a window yet,
4676 * open one. Otherwise move the window to the right position.
4677 * Watch out for autocommands that delete buffers or windows!
4679 #ifdef FEAT_AUTOCMD
4680 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4681 ++autocmd_no_enter;
4682 #endif
4683 win_enter(lastwin, FALSE);
4684 #ifdef FEAT_AUTOCMD
4685 ++autocmd_no_leave;
4686 #endif
4687 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4689 /* Check if this buffer needs a window */
4690 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4691 continue;
4693 #ifdef FEAT_WINDOWS
4694 if (had_tab != 0)
4696 /* With the ":tab" modifier don't move the window. */
4697 if (buf->b_nwindows > 0)
4698 wp = lastwin; /* buffer has a window, skip it */
4699 else
4700 wp = NULL;
4702 else
4703 #endif
4705 /* Check if this buffer already has a window */
4706 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4707 if (wp->w_buffer == buf)
4708 break;
4709 /* If the buffer already has a window, move it */
4710 if (wp != NULL)
4711 win_move_after(wp, curwin);
4714 if (wp == NULL && split_ret == OK)
4716 /* Split the window and put the buffer in it */
4717 p_ea_save = p_ea;
4718 p_ea = TRUE; /* use space from all windows */
4719 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4720 ++open_wins;
4721 p_ea = p_ea_save;
4722 if (split_ret == FAIL)
4723 continue;
4725 /* Open the buffer in this window. */
4726 #if defined(HAS_SWAP_EXISTS_ACTION)
4727 swap_exists_action = SEA_DIALOG;
4728 #endif
4729 set_curbuf(buf, DOBUF_GOTO);
4730 #ifdef FEAT_AUTOCMD
4731 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
4733 #if defined(HAS_SWAP_EXISTS_ACTION)
4734 swap_exists_action = SEA_NONE;
4735 # endif
4736 break;
4738 #endif
4739 #if defined(HAS_SWAP_EXISTS_ACTION)
4740 if (swap_exists_action == SEA_QUIT)
4742 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4743 cleanup_T cs;
4745 /* Reset the error/interrupt/exception state here so that
4746 * aborting() returns FALSE when closing a window. */
4747 enter_cleanup(&cs);
4748 # endif
4750 /* User selected Quit at ATTENTION prompt; close this window. */
4751 win_close(curwin, TRUE);
4752 --open_wins;
4753 swap_exists_action = SEA_NONE;
4754 swap_exists_did_quit = TRUE;
4756 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4757 /* Restore the error/interrupt/exception state if not
4758 * discarded by a new aborting error, interrupt, or uncaught
4759 * exception. */
4760 leave_cleanup(&cs);
4761 # endif
4763 else
4764 handle_swap_exists(NULL);
4765 #endif
4768 ui_breakcheck();
4769 if (got_int)
4771 (void)vgetc(); /* only break the file loading, not the rest */
4772 break;
4774 #ifdef FEAT_EVAL
4775 /* Autocommands deleted the buffer or aborted script processing!!! */
4776 if (aborting())
4777 break;
4778 #endif
4779 #ifdef FEAT_WINDOWS
4780 /* When ":tab" was used open a new tab for a new window repeatedly. */
4781 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4782 cmdmod.tab = 9999;
4783 #endif
4785 #ifdef FEAT_AUTOCMD
4786 --autocmd_no_enter;
4787 #endif
4788 win_enter(firstwin, FALSE); /* back to first window */
4789 #ifdef FEAT_AUTOCMD
4790 --autocmd_no_leave;
4791 #endif
4794 * Close superfluous windows.
4796 for (wp = lastwin; open_wins > count; )
4798 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
4799 || autowrite(wp->w_buffer, FALSE) == OK);
4800 #ifdef FEAT_AUTOCMD
4801 if (!win_valid(wp))
4803 /* BufWrite Autocommands made the window invalid, start over */
4804 wp = lastwin;
4806 else
4807 #endif
4808 if (r)
4810 win_close(wp, !P_HID(wp->w_buffer));
4811 --open_wins;
4812 wp = lastwin;
4814 else
4816 wp = wp->w_prev;
4817 if (wp == NULL)
4818 break;
4822 # endif /* FEAT_LISTCMDS */
4824 #endif /* FEAT_WINDOWS */
4826 static int chk_modeline __ARGS((linenr_T, int));
4829 * do_modelines() - process mode lines for the current file
4831 * "flags" can be:
4832 * OPT_WINONLY only set options local to window
4833 * OPT_NOWIN don't set options local to window
4835 * Returns immediately if the "ml" option isn't set.
4837 void
4838 do_modelines(flags)
4839 int flags;
4841 linenr_T lnum;
4842 int nmlines;
4843 static int entered = 0;
4845 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
4846 return;
4848 /* Disallow recursive entry here. Can happen when executing a modeline
4849 * triggers an autocommand, which reloads modelines with a ":do". */
4850 if (entered)
4851 return;
4853 ++entered;
4854 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
4855 ++lnum)
4856 if (chk_modeline(lnum, flags) == FAIL)
4857 nmlines = 0;
4859 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
4860 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
4861 if (chk_modeline(lnum, flags) == FAIL)
4862 nmlines = 0;
4863 --entered;
4866 #include "version.h" /* for version number */
4869 * chk_modeline() - check a single line for a mode string
4870 * Return FAIL if an error encountered.
4872 static int
4873 chk_modeline(lnum, flags)
4874 linenr_T lnum;
4875 int flags; /* Same as for do_modelines(). */
4877 char_u *s;
4878 char_u *e;
4879 char_u *linecopy; /* local copy of any modeline found */
4880 int prev;
4881 int vers;
4882 int end;
4883 int retval = OK;
4884 char_u *save_sourcing_name;
4885 linenr_T save_sourcing_lnum;
4886 #ifdef FEAT_EVAL
4887 scid_T save_SID;
4888 #endif
4890 prev = -1;
4891 for (s = ml_get(lnum); *s != NUL; ++s)
4893 if (prev == -1 || vim_isspace(prev))
4895 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
4896 || STRNCMP(s, "vi:", (size_t)3) == 0)
4897 break;
4898 if (STRNCMP(s, "vim", 3) == 0)
4900 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
4901 e = s + 4;
4902 else
4903 e = s + 3;
4904 vers = getdigits(&e);
4905 if (*e == ':'
4906 && (s[3] == ':'
4907 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
4908 || (VIM_VERSION_100 < vers && s[3] == '<')
4909 || (VIM_VERSION_100 > vers && s[3] == '>')
4910 || (VIM_VERSION_100 == vers && s[3] == '=')))
4911 break;
4914 prev = *s;
4917 if (*s)
4919 do /* skip over "ex:", "vi:" or "vim:" */
4920 ++s;
4921 while (s[-1] != ':');
4923 s = linecopy = vim_strsave(s); /* copy the line, it will change */
4924 if (linecopy == NULL)
4925 return FAIL;
4927 save_sourcing_lnum = sourcing_lnum;
4928 save_sourcing_name = sourcing_name;
4929 sourcing_lnum = lnum; /* prepare for emsg() */
4930 sourcing_name = (char_u *)"modelines";
4932 end = FALSE;
4933 while (end == FALSE)
4935 s = skipwhite(s);
4936 if (*s == NUL)
4937 break;
4940 * Find end of set command: ':' or end of line.
4941 * Skip over "\:", replacing it with ":".
4943 for (e = s; *e != ':' && *e != NUL; ++e)
4944 if (e[0] == '\\' && e[1] == ':')
4945 STRMOVE(e, e + 1);
4946 if (*e == NUL)
4947 end = TRUE;
4950 * If there is a "set" command, require a terminating ':' and
4951 * ignore the stuff after the ':'.
4952 * "vi:set opt opt opt: foo" -- foo not interpreted
4953 * "vi:opt opt opt: foo" -- foo interpreted
4954 * Accept "se" for compatibility with Elvis.
4956 if (STRNCMP(s, "set ", (size_t)4) == 0
4957 || STRNCMP(s, "se ", (size_t)3) == 0)
4959 if (*e != ':') /* no terminating ':'? */
4960 break;
4961 end = TRUE;
4962 s = vim_strchr(s, ' ') + 1;
4964 *e = NUL; /* truncate the set command */
4966 if (*s != NUL) /* skip over an empty "::" */
4968 #ifdef FEAT_EVAL
4969 save_SID = current_SID;
4970 current_SID = SID_MODELINE;
4971 #endif
4972 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
4973 #ifdef FEAT_EVAL
4974 current_SID = save_SID;
4975 #endif
4976 if (retval == FAIL) /* stop if error found */
4977 break;
4979 s = e + 1; /* advance to next part */
4982 sourcing_lnum = save_sourcing_lnum;
4983 sourcing_name = save_sourcing_name;
4985 vim_free(linecopy);
4987 return retval;
4990 #if defined(FEAT_VIMINFO) || defined(PROTO)
4992 read_viminfo_bufferlist(virp, writing)
4993 vir_T *virp;
4994 int writing;
4996 char_u *tab;
4997 linenr_T lnum;
4998 colnr_T col;
4999 buf_T *buf;
5000 char_u *sfname;
5001 char_u *xline;
5003 /* Handle long line and escaped characters. */
5004 xline = viminfo_readstring(virp, 1, FALSE);
5006 /* don't read in if there are files on the command-line or if writing: */
5007 if (xline != NULL && !writing && ARGCOUNT == 0
5008 && find_viminfo_parameter('%') != NULL)
5010 /* Format is: <fname> Tab <lnum> Tab <col>.
5011 * Watch out for a Tab in the file name, work from the end. */
5012 lnum = 0;
5013 col = 0;
5014 tab = vim_strrchr(xline, '\t');
5015 if (tab != NULL)
5017 *tab++ = '\0';
5018 col = (colnr_T)atoi((char *)tab);
5019 tab = vim_strrchr(xline, '\t');
5020 if (tab != NULL)
5022 *tab++ = '\0';
5023 lnum = atol((char *)tab);
5027 /* Expand "~/" in the file name at "line + 1" to a full path.
5028 * Then try shortening it by comparing with the current directory */
5029 expand_env(xline, NameBuff, MAXPATHL);
5030 sfname = shorten_fname1(NameBuff);
5032 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5033 if (buf != NULL) /* just in case... */
5035 buf->b_last_cursor.lnum = lnum;
5036 buf->b_last_cursor.col = col;
5037 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5040 vim_free(xline);
5042 return viminfo_readline(virp);
5045 void
5046 write_viminfo_bufferlist(fp)
5047 FILE *fp;
5049 buf_T *buf;
5050 #ifdef FEAT_WINDOWS
5051 win_T *win;
5052 tabpage_T *tp;
5053 #endif
5054 char_u *line;
5055 int max_buffers;
5056 size_t len;
5058 if (find_viminfo_parameter('%') == NULL)
5059 return;
5061 /* Without a number -1 is returned: do all buffers. */
5062 max_buffers = get_viminfo_parameter('%');
5064 /* Allocate room for the file name, lnum and col. */
5065 #define LINE_BUF_LEN (MAXPATHL + 40)
5066 line = alloc(LINE_BUF_LEN);
5067 if (line == NULL)
5068 return;
5070 #ifdef FEAT_WINDOWS
5071 FOR_ALL_TAB_WINDOWS(tp, win)
5072 set_last_cursor(win);
5073 #else
5074 set_last_cursor(curwin);
5075 #endif
5077 fprintf(fp, _("\n# Buffer list:\n"));
5078 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5080 if (buf->b_fname == NULL
5081 || !buf->b_p_bl
5082 #ifdef FEAT_QUICKFIX
5083 || bt_quickfix(buf)
5084 #endif
5085 || removable(buf->b_ffname))
5086 continue;
5088 if (max_buffers-- == 0)
5089 break;
5090 putc('%', fp);
5091 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
5092 len = STRLEN(line);
5093 vim_snprintf((char *)line + len, len - LINE_BUF_LEN, "\t%ld\t%d",
5094 (long)buf->b_last_cursor.lnum,
5095 buf->b_last_cursor.col);
5096 viminfo_writestring(fp, line);
5098 vim_free(line);
5100 #endif
5104 * Return special buffer name.
5105 * Returns NULL when the buffer has a normal file name.
5107 char *
5108 buf_spname(buf)
5109 buf_T *buf;
5111 #if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5112 if (bt_quickfix(buf))
5114 win_T *win = NULL;
5115 tabpage_T *tp;
5118 * For location list window, w_llist_ref points to the location list.
5119 * For quickfix window, w_llist_ref is NULL.
5121 FOR_ALL_TAB_WINDOWS(tp, win)
5122 if (win->w_buffer == buf)
5123 goto win_found;
5124 win_found:
5125 if (win != NULL && win->w_llist_ref != NULL)
5126 return _("[Location List]");
5127 else
5128 return _("[Quickfix List]");
5130 #endif
5131 #ifdef FEAT_QUICKFIX
5132 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5133 * contains the name as specified by the user */
5134 if (bt_nofile(buf))
5136 if (buf->b_sfname != NULL)
5137 return (char *)buf->b_sfname;
5138 return _("[Scratch]");
5140 #endif
5141 if (buf->b_fname == NULL)
5142 return _("[No Name]");
5143 return NULL;
5147 #if defined(FEAT_SIGNS) || defined(PROTO)
5149 * Insert the sign into the signlist.
5151 static void
5152 insert_sign(buf, prev, next, id, lnum, typenr)
5153 buf_T *buf; /* buffer to store sign in */
5154 signlist_T *prev; /* previous sign entry */
5155 signlist_T *next; /* next sign entry */
5156 int id; /* sign ID */
5157 linenr_T lnum; /* line number which gets the mark */
5158 int typenr; /* typenr of sign we are adding */
5160 signlist_T *newsign;
5162 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5163 if (newsign != NULL)
5165 newsign->id = id;
5166 newsign->lnum = lnum;
5167 newsign->typenr = typenr;
5168 newsign->next = next;
5169 #ifdef FEAT_NETBEANS_INTG
5170 newsign->prev = prev;
5171 if (next != NULL)
5172 next->prev = newsign;
5173 #endif
5175 if (prev == NULL)
5177 /* When adding first sign need to redraw the windows to create the
5178 * column for signs. */
5179 if (buf->b_signlist == NULL)
5181 redraw_buf_later(buf, NOT_VALID);
5182 changed_cline_bef_curs();
5185 /* first sign in signlist */
5186 buf->b_signlist = newsign;
5188 else
5189 prev->next = newsign;
5194 * Add the sign into the signlist. Find the right spot to do it though.
5196 void
5197 buf_addsign(buf, id, lnum, typenr)
5198 buf_T *buf; /* buffer to store sign in */
5199 int id; /* sign ID */
5200 linenr_T lnum; /* line number which gets the mark */
5201 int typenr; /* typenr of sign we are adding */
5203 signlist_T *sign; /* a sign in the signlist */
5204 signlist_T *prev; /* the previous sign */
5206 prev = NULL;
5207 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5209 if (lnum == sign->lnum && id == sign->id)
5211 sign->typenr = typenr;
5212 return;
5214 else if (
5215 #ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5216 id < 0 &&
5217 #endif
5218 lnum < sign->lnum)
5220 #ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5221 /* XXX - GRP: Is this because of sign slide problem? Or is it
5222 * really needed? Or is it because we allow multiple signs per
5223 * line? If so, should I add that feature to FEAT_SIGNS?
5225 while (prev != NULL && prev->lnum == lnum)
5226 prev = prev->prev;
5227 if (prev == NULL)
5228 sign = buf->b_signlist;
5229 else
5230 sign = prev->next;
5231 #endif
5232 insert_sign(buf, prev, sign, id, lnum, typenr);
5233 return;
5235 prev = sign;
5237 #ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5238 /* XXX - GRP: See previous comment */
5239 while (prev != NULL && prev->lnum == lnum)
5240 prev = prev->prev;
5241 if (prev == NULL)
5242 sign = buf->b_signlist;
5243 else
5244 sign = prev->next;
5245 #endif
5246 insert_sign(buf, prev, sign, id, lnum, typenr);
5248 return;
5251 linenr_T
5252 buf_change_sign_type(buf, markId, typenr)
5253 buf_T *buf; /* buffer to store sign in */
5254 int markId; /* sign ID */
5255 int typenr; /* typenr of sign we are adding */
5257 signlist_T *sign; /* a sign in the signlist */
5259 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5261 if (sign->id == markId)
5263 sign->typenr = typenr;
5264 return sign->lnum;
5268 return (linenr_T)0;
5272 buf_getsigntype(buf, lnum, type)
5273 buf_T *buf;
5274 linenr_T lnum;
5275 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5277 signlist_T *sign; /* a sign in a b_signlist */
5279 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5280 if (sign->lnum == lnum
5281 && (type == SIGN_ANY
5282 # ifdef FEAT_SIGN_ICONS
5283 || (type == SIGN_ICON
5284 && sign_get_image(sign->typenr) != NULL)
5285 # endif
5286 || (type == SIGN_TEXT
5287 && sign_get_text(sign->typenr) != NULL)
5288 || (type == SIGN_LINEHL
5289 && sign_get_attr(sign->typenr, TRUE) != 0)))
5290 return sign->typenr;
5291 return 0;
5295 linenr_T
5296 buf_delsign(buf, id)
5297 buf_T *buf; /* buffer sign is stored in */
5298 int id; /* sign id */
5300 signlist_T **lastp; /* pointer to pointer to current sign */
5301 signlist_T *sign; /* a sign in a b_signlist */
5302 signlist_T *next; /* the next sign in a b_signlist */
5303 linenr_T lnum; /* line number whose sign was deleted */
5305 lastp = &buf->b_signlist;
5306 lnum = 0;
5307 for (sign = buf->b_signlist; sign != NULL; sign = next)
5309 next = sign->next;
5310 if (sign->id == id)
5312 *lastp = next;
5313 #ifdef FEAT_NETBEANS_INTG
5314 if (next != NULL)
5315 next->prev = sign->prev;
5316 #endif
5317 lnum = sign->lnum;
5318 vim_free(sign);
5319 break;
5321 else
5322 lastp = &sign->next;
5325 /* When deleted the last sign need to redraw the windows to remove the
5326 * sign column. */
5327 if (buf->b_signlist == NULL)
5329 redraw_buf_later(buf, NOT_VALID);
5330 changed_cline_bef_curs();
5333 return lnum;
5338 * Find the line number of the sign with the requested id. If the sign does
5339 * not exist, return 0 as the line number. This will still let the correct file
5340 * get loaded.
5343 buf_findsign(buf, id)
5344 buf_T *buf; /* buffer to store sign in */
5345 int id; /* sign ID */
5347 signlist_T *sign; /* a sign in the signlist */
5349 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5350 if (sign->id == id)
5351 return sign->lnum;
5353 return 0;
5357 buf_findsign_id(buf, lnum)
5358 buf_T *buf; /* buffer whose sign we are searching for */
5359 linenr_T lnum; /* line number of sign */
5361 signlist_T *sign; /* a sign in the signlist */
5363 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5364 if (sign->lnum == lnum)
5365 return sign->id;
5367 return 0;
5371 # if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5372 /* see if a given type of sign exists on a specific line */
5374 buf_findsigntype_id(buf, lnum, typenr)
5375 buf_T *buf; /* buffer whose sign we are searching for */
5376 linenr_T lnum; /* line number of sign */
5377 int typenr; /* sign type number */
5379 signlist_T *sign; /* a sign in the signlist */
5381 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5382 if (sign->lnum == lnum && sign->typenr == typenr)
5383 return sign->id;
5385 return 0;
5389 # if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5390 /* return the number of icons on the given line */
5392 buf_signcount(buf, lnum)
5393 buf_T *buf;
5394 linenr_T lnum;
5396 signlist_T *sign; /* a sign in the signlist */
5397 int count = 0;
5399 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5400 if (sign->lnum == lnum)
5401 if (sign_get_image(sign->typenr) != NULL)
5402 count++;
5404 return count;
5406 # endif /* FEAT_SIGN_ICONS */
5407 # endif /* FEAT_NETBEANS_INTG */
5411 * Delete signs in buffer "buf".
5413 static void
5414 buf_delete_signs(buf)
5415 buf_T *buf;
5417 signlist_T *next;
5419 while (buf->b_signlist != NULL)
5421 next = buf->b_signlist->next;
5422 vim_free(buf->b_signlist);
5423 buf->b_signlist = next;
5428 * Delete all signs in all buffers.
5430 void
5431 buf_delete_all_signs()
5433 buf_T *buf; /* buffer we are checking for signs */
5435 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5436 if (buf->b_signlist != NULL)
5438 /* Need to redraw the windows to remove the sign column. */
5439 redraw_buf_later(buf, NOT_VALID);
5440 buf_delete_signs(buf);
5445 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5447 void
5448 sign_list_placed(rbuf)
5449 buf_T *rbuf;
5451 buf_T *buf;
5452 signlist_T *p;
5453 char lbuf[BUFSIZ];
5455 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5456 msg_putchar('\n');
5457 if (rbuf == NULL)
5458 buf = firstbuf;
5459 else
5460 buf = rbuf;
5461 while (buf != NULL)
5463 if (buf->b_signlist != NULL)
5465 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
5466 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5467 msg_putchar('\n');
5469 for (p = buf->b_signlist; p != NULL; p = p->next)
5471 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
5472 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5473 MSG_PUTS(lbuf);
5474 msg_putchar('\n');
5476 if (rbuf != NULL)
5477 break;
5478 buf = buf->b_next;
5483 * Adjust a placed sign for inserted/deleted lines.
5485 void
5486 sign_mark_adjust(line1, line2, amount, amount_after)
5487 linenr_T line1;
5488 linenr_T line2;
5489 long amount;
5490 long amount_after;
5492 signlist_T *sign; /* a sign in a b_signlist */
5494 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5496 if (sign->lnum >= line1 && sign->lnum <= line2)
5498 if (amount == MAXLNUM)
5499 sign->lnum = line1;
5500 else
5501 sign->lnum += amount;
5503 else if (sign->lnum > line2)
5504 sign->lnum += amount_after;
5507 #endif /* FEAT_SIGNS */
5510 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5512 void
5513 set_buflisted(on)
5514 int on;
5516 if (on != curbuf->b_p_bl)
5518 curbuf->b_p_bl = on;
5519 #ifdef FEAT_AUTOCMD
5520 if (on)
5521 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5522 else
5523 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5524 #endif
5529 * Read the file for "buf" again and check if the contents changed.
5530 * Return TRUE if it changed or this could not be checked.
5533 buf_contents_changed(buf)
5534 buf_T *buf;
5536 buf_T *newbuf;
5537 int differ = TRUE;
5538 linenr_T lnum;
5539 aco_save_T aco;
5540 exarg_T ea;
5542 /* Allocate a buffer without putting it in the buffer list. */
5543 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5544 if (newbuf == NULL)
5545 return TRUE;
5547 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5548 if (prep_exarg(&ea, buf) == FAIL)
5550 wipe_buffer(newbuf, FALSE);
5551 return TRUE;
5554 /* set curwin/curbuf to buf and save a few things */
5555 aucmd_prepbuf(&aco, newbuf);
5557 if (ml_open(curbuf) == OK
5558 && readfile(buf->b_ffname, buf->b_fname,
5559 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5560 &ea, READ_NEW | READ_DUMMY) == OK)
5562 /* compare the two files line by line */
5563 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5565 differ = FALSE;
5566 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5567 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5569 differ = TRUE;
5570 break;
5574 vim_free(ea.cmd);
5576 /* restore curwin/curbuf and a few other things */
5577 aucmd_restbuf(&aco);
5579 if (curbuf != newbuf) /* safety check */
5580 wipe_buffer(newbuf, FALSE);
5582 return differ;
5586 * Wipe out a buffer and decrement the last buffer number if it was used for
5587 * this buffer. Call this to wipe out a temp buffer that does not contain any
5588 * marks.
5590 void
5591 wipe_buffer(buf, aucmd)
5592 buf_T *buf;
5593 int aucmd UNUSED; /* When TRUE trigger autocommands. */
5595 if (buf->b_fnum == top_file_num - 1)
5596 --top_file_num;
5598 #ifdef FEAT_AUTOCMD
5599 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
5600 block_autocmds();
5601 #endif
5602 close_buffer(NULL, buf, DOBUF_WIPE);
5603 #ifdef FEAT_AUTOCMD
5604 if (!aucmd)
5605 unblock_autocmds();
5606 #endif