Snapshot 42
[MacVim.git] / src / buffer.c
blob589a108112090a71cf404c69a4ea1495153144a3
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 void free_buffer __ARGS((buf_T *));
48 static void free_buffer_stuff __ARGS((buf_T *buf, int free_options));
49 static void clear_wininfo __ARGS((buf_T *buf));
51 #ifdef UNIX
52 # define dev_T dev_t
53 #else
54 # define dev_T unsigned
55 #endif
57 #if defined(FEAT_SIGNS)
58 static void insert_sign __ARGS((buf_T *buf, signlist_T *prev, signlist_T *next, int id, linenr_T lnum, int typenr));
59 static void buf_delete_signs __ARGS((buf_T *buf));
60 #endif
63 * Open current buffer, that is: open the memfile and read the file into memory
64 * return FAIL for failure, OK otherwise
66 int
67 open_buffer(read_stdin, eap)
68 int read_stdin; /* read file from stdin */
69 exarg_T *eap; /* for forced 'ff' and 'fenc' or NULL */
71 int retval = OK;
72 #ifdef FEAT_AUTOCMD
73 buf_T *old_curbuf;
74 #endif
77 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
78 * When re-entering the same buffer, it should not change, because the
79 * user may have reset the flag by hand.
81 if (readonlymode && curbuf->b_ffname != NULL
82 && (curbuf->b_flags & BF_NEVERLOADED))
83 curbuf->b_p_ro = TRUE;
85 if (ml_open(curbuf) == FAIL)
88 * There MUST be a memfile, otherwise we can't do anything
89 * If we can't create one for the current buffer, take another buffer
91 close_buffer(NULL, curbuf, 0);
92 for (curbuf = firstbuf; curbuf != NULL; curbuf = curbuf->b_next)
93 if (curbuf->b_ml.ml_mfp != NULL)
94 break;
96 * if there is no memfile at all, exit
97 * This is OK, since there are no changes to lose.
99 if (curbuf == NULL)
101 EMSG(_("E82: Cannot allocate any buffer, exiting..."));
102 getout(2);
104 EMSG(_("E83: Cannot allocate buffer, using other one..."));
105 enter_buffer(curbuf);
106 return FAIL;
109 #ifdef FEAT_AUTOCMD
110 /* The autocommands in readfile() may change the buffer, but only AFTER
111 * reading the file. */
112 old_curbuf = curbuf;
113 modified_was_set = FALSE;
114 #endif
116 /* mark cursor position as being invalid */
117 changed_line_abv_curs();
119 if (curbuf->b_ffname != NULL
120 #ifdef FEAT_NETBEANS_INTG
121 && netbeansReadFile
122 #endif
125 #ifdef FEAT_NETBEANS_INTG
126 int oldFire = netbeansFireChanges;
128 netbeansFireChanges = 0;
129 #endif
130 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
131 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap, READ_NEW);
132 #ifdef FEAT_NETBEANS_INTG
133 netbeansFireChanges = oldFire;
134 #endif
135 /* Help buffer is filtered. */
136 if (curbuf->b_help)
137 fix_help_buffer();
139 else if (read_stdin)
141 int save_bin = curbuf->b_p_bin;
142 linenr_T line_count;
145 * First read the text in binary mode into the buffer.
146 * Then read from that same buffer and append at the end. This makes
147 * it possible to retry when 'fileformat' or 'fileencoding' was
148 * guessed wrong.
150 curbuf->b_p_bin = TRUE;
151 retval = readfile(NULL, NULL, (linenr_T)0,
152 (linenr_T)0, (linenr_T)MAXLNUM, NULL, READ_NEW + READ_STDIN);
153 curbuf->b_p_bin = save_bin;
154 if (retval == OK)
156 line_count = curbuf->b_ml.ml_line_count;
157 retval = readfile(NULL, NULL, (linenr_T)line_count,
158 (linenr_T)0, (linenr_T)MAXLNUM, eap, READ_BUFFER);
159 if (retval == OK)
161 /* Delete the binary lines. */
162 while (--line_count >= 0)
163 ml_delete((linenr_T)1, FALSE);
165 else
167 /* Delete the converted lines. */
168 while (curbuf->b_ml.ml_line_count > line_count)
169 ml_delete(line_count, FALSE);
171 /* Put the cursor on the first line. */
172 curwin->w_cursor.lnum = 1;
173 curwin->w_cursor.col = 0;
175 /* Set or reset 'modified' before executing autocommands, so that
176 * it can be changed there. */
177 if (!readonlymode && !bufempty())
178 changed();
179 else if (retval != FAIL)
180 unchanged(curbuf, FALSE);
181 #ifdef FEAT_AUTOCMD
182 # ifdef FEAT_EVAL
183 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
184 curbuf, &retval);
185 # else
186 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
187 # endif
188 #endif
192 /* if first time loading this buffer, init b_chartab[] */
193 if (curbuf->b_flags & BF_NEVERLOADED)
194 (void)buf_init_chartab(curbuf, FALSE);
197 * Set/reset the Changed flag first, autocmds may change the buffer.
198 * Apply the automatic commands, before processing the modelines.
199 * So the modelines have priority over auto commands.
201 /* When reading stdin, the buffer contents always needs writing, so set
202 * the changed flag. Unless in readonly mode: "ls | gview -".
203 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
204 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
205 #ifdef FEAT_AUTOCMD
206 || modified_was_set /* ":set modified" used in autocmd */
207 # ifdef FEAT_EVAL
208 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
209 # endif
210 #endif
212 changed();
213 else if (retval != FAIL && !read_stdin)
214 unchanged(curbuf, FALSE);
215 save_file_ff(curbuf); /* keep this fileformat */
217 /* require "!" to overwrite the file, because it wasn't read completely */
218 #ifdef FEAT_EVAL
219 if (aborting())
220 #else
221 if (got_int)
222 #endif
223 curbuf->b_flags |= BF_READERR;
225 #ifdef FEAT_FOLDING
226 /* Need to update automatic folding. Do this before the autocommands,
227 * they may use the fold info. */
228 foldUpdateAll(curwin);
229 #endif
231 #ifdef FEAT_AUTOCMD
232 /* need to set w_topline, unless some autocommand already did that. */
233 if (!(curwin->w_valid & VALID_TOPLINE))
235 curwin->w_topline = 1;
236 # ifdef FEAT_DIFF
237 curwin->w_topfill = 0;
238 # endif
240 # ifdef FEAT_EVAL
241 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
242 # else
243 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
244 # endif
245 #endif
247 if (retval != FAIL)
249 #ifdef FEAT_AUTOCMD
251 * The autocommands may have changed the current buffer. Apply the
252 * modelines to the correct buffer, if it still exists and is loaded.
254 if (buf_valid(old_curbuf) && old_curbuf->b_ml.ml_mfp != NULL)
256 aco_save_T aco;
258 /* Go to the buffer that was opened. */
259 aucmd_prepbuf(&aco, old_curbuf);
260 #endif
261 do_modelines(0);
262 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
264 #ifdef FEAT_AUTOCMD
265 # ifdef FEAT_EVAL
266 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
267 &retval);
268 # else
269 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
270 # endif
272 /* restore curwin/curbuf and a few other things */
273 aucmd_restbuf(&aco);
275 #endif
278 return retval;
282 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
285 buf_valid(buf)
286 buf_T *buf;
288 buf_T *bp;
290 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
291 if (bp == buf)
292 return TRUE;
293 return FALSE;
297 * Close the link to a buffer.
298 * "action" is used when there is no longer a window for the buffer.
299 * It can be:
300 * 0 buffer becomes hidden
301 * DOBUF_UNLOAD buffer is unloaded
302 * DOBUF_DELETE buffer is unloaded and removed from buffer list
303 * DOBUF_WIPE buffer is unloaded and really deleted
304 * When doing all but the first one on the current buffer, the caller should
305 * get a new buffer very soon!
307 * The 'bufhidden' option can force freeing and deleting.
309 void
310 close_buffer(win, buf, action)
311 win_T *win; /* if not NULL, set b_last_cursor */
312 buf_T *buf;
313 int action;
315 #ifdef FEAT_AUTOCMD
316 int is_curbuf;
317 int nwindows = buf->b_nwindows;
318 #endif
319 int unload_buf = (action != 0);
320 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
321 int wipe_buf = (action == DOBUF_WIPE);
323 #ifdef FEAT_QUICKFIX
325 * Force unloading or deleting when 'bufhidden' says so.
326 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
327 * "hide" (otherwise we could never free or delete a buffer).
329 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
331 del_buf = TRUE;
332 unload_buf = TRUE;
334 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
336 del_buf = TRUE;
337 unload_buf = TRUE;
338 wipe_buf = TRUE;
340 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
341 unload_buf = TRUE;
342 #endif
344 if (win != NULL)
346 /* Set b_last_cursor when closing the last window for the buffer.
347 * Remember the last cursor position and window options of the buffer.
348 * This used to be only for the current window, but then options like
349 * 'foldmethod' may be lost with a ":only" command. */
350 if (buf->b_nwindows == 1)
351 set_last_cursor(win);
352 buflist_setfpos(buf, win,
353 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
354 win->w_cursor.col, TRUE);
357 #ifdef FEAT_AUTOCMD
358 /* When the buffer is no longer in a window, trigger BufWinLeave */
359 if (buf->b_nwindows == 1)
361 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
362 FALSE, buf);
363 if (!buf_valid(buf)) /* autocommands may delete the buffer */
364 return;
366 /* When the buffer becomes hidden, but is not unloaded, trigger
367 * BufHidden */
368 if (!unload_buf)
370 apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
371 FALSE, buf);
372 if (!buf_valid(buf)) /* autocmds may delete the buffer */
373 return;
375 # ifdef FEAT_EVAL
376 if (aborting()) /* autocmds may abort script processing */
377 return;
378 # endif
380 nwindows = buf->b_nwindows;
381 #endif
383 /* decrease the link count from windows (unless not in any window) */
384 if (buf->b_nwindows > 0)
385 --buf->b_nwindows;
387 /* Return when a window is displaying the buffer or when it's not
388 * unloaded. */
389 if (buf->b_nwindows > 0 || !unload_buf)
391 #if 0 /* why was this here? */
392 if (buf == curbuf)
393 u_sync(); /* sync undo before going to another buffer */
394 #endif
395 return;
398 /* Always remove the buffer when there is no file name. */
399 if (buf->b_ffname == NULL)
400 del_buf = TRUE;
403 * Free all things allocated for this buffer.
404 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
406 #ifdef FEAT_AUTOCMD
407 /* Remember if we are closing the current buffer. Restore the number of
408 * windows, so that autocommands in buf_freeall() don't get confused. */
409 is_curbuf = (buf == curbuf);
410 buf->b_nwindows = nwindows;
411 #endif
413 buf_freeall(buf, del_buf, wipe_buf);
415 #ifdef FEAT_AUTOCMD
416 /* Autocommands may have deleted the buffer. */
417 if (!buf_valid(buf))
418 return;
419 # ifdef FEAT_EVAL
420 if (aborting()) /* autocmds may abort script processing */
421 return;
422 # endif
424 /* Autocommands may have opened or closed windows for this buffer.
425 * Decrement the count for the close we do here. */
426 if (buf->b_nwindows > 0)
427 --buf->b_nwindows;
430 * It's possible that autocommands change curbuf to the one being deleted.
431 * This might cause the previous curbuf to be deleted unexpectedly. But
432 * in some cases it's OK to delete the curbuf, because a new one is
433 * obtained anyway. Therefore only return if curbuf changed to the
434 * deleted buffer.
436 if (buf == curbuf && !is_curbuf)
437 return;
438 #endif
440 #ifdef FEAT_NETBEANS_INTG
441 if (usingNetbeans)
442 netbeans_file_closed(buf);
443 #endif
444 #ifdef FEAT_ODB_EDITOR
445 odb_buffer_close(buf);
446 #endif
448 /* Change directories when the 'acd' option is set. */
449 DO_AUTOCHDIR
452 * Remove the buffer from the list.
454 if (wipe_buf)
456 #ifdef FEAT_SUN_WORKSHOP
457 if (usingSunWorkShop)
458 workshop_file_closed_lineno((char *)buf->b_ffname,
459 (int)buf->b_last_cursor.lnum);
460 #endif
461 vim_free(buf->b_ffname);
462 vim_free(buf->b_sfname);
463 if (buf->b_prev == NULL)
464 firstbuf = buf->b_next;
465 else
466 buf->b_prev->b_next = buf->b_next;
467 if (buf->b_next == NULL)
468 lastbuf = buf->b_prev;
469 else
470 buf->b_next->b_prev = buf->b_prev;
471 free_buffer(buf);
473 else
475 if (del_buf)
477 /* Free all internal variables and reset option values, to make
478 * ":bdel" compatible with Vim 5.7. */
479 free_buffer_stuff(buf, TRUE);
481 /* Make it look like a new buffer. */
482 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
484 /* Init the options when loaded again. */
485 buf->b_p_initialized = FALSE;
487 buf_clear_file(buf);
488 if (del_buf)
489 buf->b_p_bl = FALSE;
494 * Make buffer not contain a file.
496 void
497 buf_clear_file(buf)
498 buf_T *buf;
500 buf->b_ml.ml_line_count = 1;
501 unchanged(buf, TRUE);
502 #ifndef SHORT_FNAME
503 buf->b_shortname = FALSE;
504 #endif
505 buf->b_p_eol = TRUE;
506 buf->b_start_eol = TRUE;
507 #ifdef FEAT_MBYTE
508 buf->b_p_bomb = FALSE;
509 buf->b_start_bomb = FALSE;
510 #endif
511 buf->b_ml.ml_mfp = NULL;
512 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
513 #ifdef FEAT_NETBEANS_INTG
514 netbeans_deleted_all_lines(buf);
515 #endif
519 * buf_freeall() - free all things allocated for a buffer that are related to
520 * the file.
522 /*ARGSUSED*/
523 void
524 buf_freeall(buf, del_buf, wipe_buf)
525 buf_T *buf;
526 int del_buf; /* buffer is going to be deleted */
527 int wipe_buf; /* buffer is going to be wiped out */
529 #ifdef FEAT_AUTOCMD
530 int is_curbuf = (buf == curbuf);
532 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, FALSE, buf);
533 if (!buf_valid(buf)) /* autocommands may delete the buffer */
534 return;
535 if (del_buf && buf->b_p_bl)
537 apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname, FALSE, buf);
538 if (!buf_valid(buf)) /* autocommands may delete the buffer */
539 return;
541 if (wipe_buf)
543 apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
544 FALSE, buf);
545 if (!buf_valid(buf)) /* autocommands may delete the buffer */
546 return;
548 # ifdef FEAT_EVAL
549 if (aborting()) /* autocmds may abort script processing */
550 return;
551 # endif
554 * It's possible that autocommands change curbuf to the one being deleted.
555 * This might cause curbuf to be deleted unexpectedly. But in some cases
556 * it's OK to delete the curbuf, because a new one is obtained anyway.
557 * Therefore only return if curbuf changed to the deleted buffer.
559 if (buf == curbuf && !is_curbuf)
560 return;
561 #endif
562 #ifdef FEAT_DIFF
563 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
564 #endif
566 #ifdef FEAT_FOLDING
567 /* No folds in an empty buffer. */
568 # ifdef FEAT_WINDOWS
570 win_T *win;
571 tabpage_T *tp;
573 FOR_ALL_TAB_WINDOWS(tp, win)
574 if (win->w_buffer == buf)
575 clearFolding(win);
577 # else
578 if (curwin->w_buffer == buf)
579 clearFolding(curwin);
580 # endif
581 #endif
583 #ifdef FEAT_TCL
584 tcl_buffer_free(buf);
585 #endif
586 u_blockfree(buf); /* free the memory allocated for undo */
587 ml_close(buf, TRUE); /* close and delete the memline/memfile */
588 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
589 u_clearall(buf); /* reset all undo information */
590 #ifdef FEAT_SYN_HL
591 syntax_clear(buf); /* reset syntax info */
592 #endif
593 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
597 * Free a buffer structure and the things it contains related to the buffer
598 * itself (not the file, that must have been done already).
600 static void
601 free_buffer(buf)
602 buf_T *buf;
604 free_buffer_stuff(buf, TRUE);
605 #ifdef FEAT_MZSCHEME
606 mzscheme_buffer_free(buf);
607 #endif
608 #ifdef FEAT_PERL
609 perl_buf_free(buf);
610 #endif
611 #ifdef FEAT_PYTHON
612 python_buffer_free(buf);
613 #endif
614 #ifdef FEAT_RUBY
615 ruby_buffer_free(buf);
616 #endif
617 #ifdef FEAT_AUTOCMD
618 aubuflocal_remove(buf);
619 #endif
620 vim_free(buf);
624 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
626 static void
627 free_buffer_stuff(buf, free_options)
628 buf_T *buf;
629 int free_options; /* free options as well */
631 if (free_options)
633 clear_wininfo(buf); /* including window-local options */
634 free_buf_options(buf, TRUE);
636 #ifdef FEAT_EVAL
637 vars_clear(&buf->b_vars.dv_hashtab); /* free all internal variables */
638 hash_init(&buf->b_vars.dv_hashtab);
639 #endif
640 #ifdef FEAT_USR_CMDS
641 uc_clear(&buf->b_ucmds); /* clear local user commands */
642 #endif
643 #ifdef FEAT_SIGNS
644 buf_delete_signs(buf); /* delete any signs */
645 #endif
646 #ifdef FEAT_LOCALMAP
647 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
648 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
649 #endif
650 #ifdef FEAT_MBYTE
651 vim_free(buf->b_start_fenc);
652 buf->b_start_fenc = NULL;
653 #endif
654 #ifdef FEAT_SPELL
655 ga_clear(&buf->b_langp);
656 #endif
660 * Free the b_wininfo list for buffer "buf".
662 static void
663 clear_wininfo(buf)
664 buf_T *buf;
666 wininfo_T *wip;
668 while (buf->b_wininfo != NULL)
670 wip = buf->b_wininfo;
671 buf->b_wininfo = wip->wi_next;
672 if (wip->wi_optset)
674 clear_winopt(&wip->wi_opt);
675 #ifdef FEAT_FOLDING
676 deleteFoldRecurse(&wip->wi_folds);
677 #endif
679 vim_free(wip);
683 #if defined(FEAT_LISTCMDS) || defined(PROTO)
685 * Go to another buffer. Handles the result of the ATTENTION dialog.
687 void
688 goto_buffer(eap, start, dir, count)
689 exarg_T *eap;
690 int start;
691 int dir;
692 int count;
694 # if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
695 buf_T *old_curbuf = curbuf;
697 swap_exists_action = SEA_DIALOG;
698 # endif
699 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
700 start, dir, count, eap->forceit);
701 # if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
702 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
704 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
705 cleanup_T cs;
707 /* Reset the error/interrupt/exception state here so that
708 * aborting() returns FALSE when closing a window. */
709 enter_cleanup(&cs);
710 # endif
712 /* Quitting means closing the split window, nothing else. */
713 win_close(curwin, TRUE);
714 swap_exists_action = SEA_NONE;
715 swap_exists_did_quit = TRUE;
717 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
718 /* Restore the error/interrupt/exception state if not discarded by a
719 * new aborting error, interrupt, or uncaught exception. */
720 leave_cleanup(&cs);
721 # endif
723 else
724 handle_swap_exists(old_curbuf);
725 # endif
727 #endif
729 #if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
731 * Handle the situation of swap_exists_action being set.
732 * It is allowed for "old_curbuf" to be NULL or invalid.
734 void
735 handle_swap_exists(old_curbuf)
736 buf_T *old_curbuf;
738 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
739 cleanup_T cs;
740 # endif
742 if (swap_exists_action == SEA_QUIT)
744 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
745 /* Reset the error/interrupt/exception state here so that
746 * aborting() returns FALSE when closing a buffer. */
747 enter_cleanup(&cs);
748 # endif
750 /* User selected Quit at ATTENTION prompt. Go back to previous
751 * buffer. If that buffer is gone or the same as the current one,
752 * open a new, empty buffer. */
753 swap_exists_action = SEA_NONE; /* don't want it again */
754 swap_exists_did_quit = TRUE;
755 close_buffer(curwin, curbuf, DOBUF_UNLOAD);
756 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
757 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
758 if (old_curbuf != NULL)
759 enter_buffer(old_curbuf);
760 /* If "old_curbuf" is NULL we are in big trouble here... */
762 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
763 /* Restore the error/interrupt/exception state if not discarded by a
764 * new aborting error, interrupt, or uncaught exception. */
765 leave_cleanup(&cs);
766 # endif
768 else if (swap_exists_action == SEA_RECOVER)
770 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
771 /* Reset the error/interrupt/exception state here so that
772 * aborting() returns FALSE when closing a buffer. */
773 enter_cleanup(&cs);
774 # endif
776 /* User selected Recover at ATTENTION prompt. */
777 msg_scroll = TRUE;
778 ml_recover();
779 MSG_PUTS("\n"); /* don't overwrite the last message */
780 cmdline_row = msg_row;
781 do_modelines(0);
783 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
784 /* Restore the error/interrupt/exception state if not discarded by a
785 * new aborting error, interrupt, or uncaught exception. */
786 leave_cleanup(&cs);
787 # endif
789 swap_exists_action = SEA_NONE;
791 #endif
793 #if defined(FEAT_LISTCMDS) || defined(PROTO)
795 * do_bufdel() - delete or unload buffer(s)
797 * addr_count == 0: ":bdel" - delete current buffer
798 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
799 * buffer "end_bnr", then any other arguments.
800 * addr_count == 2: ":N,N bdel" - delete buffers in range
802 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
803 * DOBUF_DEL (":bdel")
805 * Returns error message or NULL
807 char_u *
808 do_bufdel(command, arg, addr_count, start_bnr, end_bnr, forceit)
809 int command;
810 char_u *arg; /* pointer to extra arguments */
811 int addr_count;
812 int start_bnr; /* first buffer number in a range */
813 int end_bnr; /* buffer nr or last buffer nr in a range */
814 int forceit;
816 int do_current = 0; /* delete current buffer? */
817 int deleted = 0; /* number of buffers deleted */
818 char_u *errormsg = NULL; /* return value */
819 int bnr; /* buffer number */
820 char_u *p;
822 #ifdef FEAT_NETBEANS_INTG
823 netbeansCloseFile = 1;
824 #endif
825 if (addr_count == 0)
827 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
829 else
831 if (addr_count == 2)
833 if (*arg) /* both range and argument is not allowed */
834 return (char_u *)_(e_trailing);
835 bnr = start_bnr;
837 else /* addr_count == 1 */
838 bnr = end_bnr;
840 for ( ;!got_int; ui_breakcheck())
843 * delete the current buffer last, otherwise when the
844 * current buffer is deleted, the next buffer becomes
845 * the current one and will be loaded, which may then
846 * also be deleted, etc.
848 if (bnr == curbuf->b_fnum)
849 do_current = bnr;
850 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
851 forceit) == OK)
852 ++deleted;
855 * find next buffer number to delete/unload
857 if (addr_count == 2)
859 if (++bnr > end_bnr)
860 break;
862 else /* addr_count == 1 */
864 arg = skipwhite(arg);
865 if (*arg == NUL)
866 break;
867 if (!VIM_ISDIGIT(*arg))
869 p = skiptowhite_esc(arg);
870 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE, FALSE);
871 if (bnr < 0) /* failed */
872 break;
873 arg = p;
875 else
876 bnr = getdigits(&arg);
879 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
880 FORWARD, do_current, forceit) == OK)
881 ++deleted;
883 if (deleted == 0)
885 if (command == DOBUF_UNLOAD)
886 STRCPY(IObuff, _("E515: No buffers were unloaded"));
887 else if (command == DOBUF_DEL)
888 STRCPY(IObuff, _("E516: No buffers were deleted"));
889 else
890 STRCPY(IObuff, _("E517: No buffers were wiped out"));
891 errormsg = IObuff;
893 else if (deleted >= p_report)
895 if (command == DOBUF_UNLOAD)
897 if (deleted == 1)
898 MSG(_("1 buffer unloaded"));
899 else
900 smsg((char_u *)_("%d buffers unloaded"), deleted);
902 else if (command == DOBUF_DEL)
904 if (deleted == 1)
905 MSG(_("1 buffer deleted"));
906 else
907 smsg((char_u *)_("%d buffers deleted"), deleted);
909 else
911 if (deleted == 1)
912 MSG(_("1 buffer wiped out"));
913 else
914 smsg((char_u *)_("%d buffers wiped out"), deleted);
919 #ifdef FEAT_NETBEANS_INTG
920 netbeansCloseFile = 0;
921 #endif
923 return errormsg;
927 * Implementation of the commands for the buffer list.
929 * action == DOBUF_GOTO go to specified buffer
930 * action == DOBUF_SPLIT split window and go to specified buffer
931 * action == DOBUF_UNLOAD unload specified buffer(s)
932 * action == DOBUF_DEL delete specified buffer(s) from buffer list
933 * action == DOBUF_WIPE delete specified buffer(s) really
935 * start == DOBUF_CURRENT go to "count" buffer from current buffer
936 * start == DOBUF_FIRST go to "count" buffer from first buffer
937 * start == DOBUF_LAST go to "count" buffer from last buffer
938 * start == DOBUF_MOD go to "count" modified buffer from current buffer
940 * Return FAIL or OK.
943 do_buffer(action, start, dir, count, forceit)
944 int action;
945 int start;
946 int dir; /* FORWARD or BACKWARD */
947 int count; /* buffer number or number of buffers */
948 int forceit; /* TRUE for :...! */
950 buf_T *buf;
951 buf_T *bp;
952 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
953 || action == DOBUF_WIPE);
955 switch (start)
957 case DOBUF_FIRST: buf = firstbuf; break;
958 case DOBUF_LAST: buf = lastbuf; break;
959 default: buf = curbuf; break;
961 if (start == DOBUF_MOD) /* find next modified buffer */
963 while (count-- > 0)
967 buf = buf->b_next;
968 if (buf == NULL)
969 buf = firstbuf;
971 while (buf != curbuf && !bufIsChanged(buf));
973 if (!bufIsChanged(buf))
975 EMSG(_("E84: No modified buffer found"));
976 return FAIL;
979 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
981 while (buf != NULL && buf->b_fnum != count)
982 buf = buf->b_next;
984 else
986 bp = NULL;
987 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
989 /* remember the buffer where we start, we come back there when all
990 * buffers are unlisted. */
991 if (bp == NULL)
992 bp = buf;
993 if (dir == FORWARD)
995 buf = buf->b_next;
996 if (buf == NULL)
997 buf = firstbuf;
999 else
1001 buf = buf->b_prev;
1002 if (buf == NULL)
1003 buf = lastbuf;
1005 /* don't count unlisted buffers */
1006 if (unload || buf->b_p_bl)
1008 --count;
1009 bp = NULL; /* use this buffer as new starting point */
1011 if (bp == buf)
1013 /* back where we started, didn't find anything. */
1014 EMSG(_("E85: There is no listed buffer"));
1015 return FAIL;
1020 if (buf == NULL) /* could not find it */
1022 if (start == DOBUF_FIRST)
1024 /* don't warn when deleting */
1025 if (!unload)
1026 EMSGN(_("E86: Buffer %ld does not exist"), count);
1028 else if (dir == FORWARD)
1029 EMSG(_("E87: Cannot go beyond last buffer"));
1030 else
1031 EMSG(_("E88: Cannot go before first buffer"));
1032 return FAIL;
1035 #ifdef FEAT_GUI
1036 need_mouse_correct = TRUE;
1037 #endif
1039 #ifdef FEAT_LISTCMDS
1041 * delete buffer buf from memory and/or the list
1043 if (unload)
1045 int forward;
1046 int retval;
1048 /* When unloading or deleting a buffer that's already unloaded and
1049 * unlisted: fail silently. */
1050 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1051 return FAIL;
1053 if (!forceit && bufIsChanged(buf))
1055 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1056 if ((p_confirm || cmdmod.confirm) && p_write)
1058 dialog_changed(buf, FALSE);
1059 # ifdef FEAT_AUTOCMD
1060 if (!buf_valid(buf))
1061 /* Autocommand deleted buffer, oops! It's not changed
1062 * now. */
1063 return FAIL;
1064 # endif
1065 /* If it's still changed fail silently, the dialog already
1066 * mentioned why it fails. */
1067 if (bufIsChanged(buf))
1068 return FAIL;
1070 else
1071 #endif
1073 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1074 buf->b_fnum);
1075 return FAIL;
1080 * If deleting the last (listed) buffer, make it empty.
1081 * The last (listed) buffer cannot be unloaded.
1083 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1084 if (bp->b_p_bl && bp != buf)
1085 break;
1086 if (bp == NULL && buf == curbuf)
1088 if (action == DOBUF_UNLOAD)
1090 EMSG(_("E90: Cannot unload last buffer"));
1091 return FAIL;
1094 /* Close any other windows on this buffer, then make it empty. */
1095 #ifdef FEAT_WINDOWS
1096 close_windows(buf, TRUE);
1097 #endif
1098 setpcmark();
1099 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1100 forceit ? ECMD_FORCEIT : 0, curwin);
1103 * do_ecmd() may create a new buffer, then we have to delete
1104 * the old one. But do_ecmd() may have done that already, check
1105 * if the buffer still exists.
1107 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
1108 close_buffer(NULL, buf, action);
1109 return retval;
1112 #ifdef FEAT_WINDOWS
1114 * If the deleted buffer is the current one, close the current window
1115 * (unless it's the only window). Repeat this so long as we end up in
1116 * a window with this buffer.
1118 while (buf == curbuf
1119 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
1120 win_close(curwin, FALSE);
1121 #endif
1124 * If the buffer to be deleted is not the current one, delete it here.
1126 if (buf != curbuf)
1128 #ifdef FEAT_WINDOWS
1129 close_windows(buf, FALSE);
1130 #endif
1131 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
1132 close_buffer(NULL, buf, action);
1133 return OK;
1137 * Deleting the current buffer: Need to find another buffer to go to.
1138 * There must be another, otherwise it would have been handled above.
1139 * First use au_new_curbuf, if it is valid.
1140 * Then prefer the buffer we most recently visited.
1141 * Else try to find one that is loaded, after the current buffer,
1142 * then before the current buffer.
1143 * Finally use any buffer.
1145 buf = NULL; /* selected buffer */
1146 bp = NULL; /* used when no loaded buffer found */
1147 #ifdef FEAT_AUTOCMD
1148 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1149 buf = au_new_curbuf;
1150 # ifdef FEAT_JUMPLIST
1151 else
1152 # endif
1153 #endif
1154 #ifdef FEAT_JUMPLIST
1155 if (curwin->w_jumplistlen > 0)
1157 int jumpidx;
1159 jumpidx = curwin->w_jumplistidx - 1;
1160 if (jumpidx < 0)
1161 jumpidx = curwin->w_jumplistlen - 1;
1163 forward = jumpidx;
1164 while (jumpidx != curwin->w_jumplistidx)
1166 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1167 if (buf != NULL)
1169 if (buf == curbuf || !buf->b_p_bl)
1170 buf = NULL; /* skip current and unlisted bufs */
1171 else if (buf->b_ml.ml_mfp == NULL)
1173 /* skip unloaded buf, but may keep it for later */
1174 if (bp == NULL)
1175 bp = buf;
1176 buf = NULL;
1179 if (buf != NULL) /* found a valid buffer: stop searching */
1180 break;
1181 /* advance to older entry in jump list */
1182 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1183 break;
1184 if (--jumpidx < 0)
1185 jumpidx = curwin->w_jumplistlen - 1;
1186 if (jumpidx == forward) /* List exhausted for sure */
1187 break;
1190 #endif
1192 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1194 forward = TRUE;
1195 buf = curbuf->b_next;
1196 for (;;)
1198 if (buf == NULL)
1200 if (!forward) /* tried both directions */
1201 break;
1202 buf = curbuf->b_prev;
1203 forward = FALSE;
1204 continue;
1206 /* in non-help buffer, try to skip help buffers, and vv */
1207 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1209 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1210 break;
1211 if (bp == NULL) /* remember unloaded buf for later */
1212 bp = buf;
1214 if (forward)
1215 buf = buf->b_next;
1216 else
1217 buf = buf->b_prev;
1220 if (buf == NULL) /* No loaded buffer, use unloaded one */
1221 buf = bp;
1222 if (buf == NULL) /* No loaded buffer, find listed one */
1224 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1225 if (buf->b_p_bl && buf != curbuf)
1226 break;
1228 if (buf == NULL) /* Still no buffer, just take one */
1230 if (curbuf->b_next != NULL)
1231 buf = curbuf->b_next;
1232 else
1233 buf = curbuf->b_prev;
1238 * make buf current buffer
1240 if (action == DOBUF_SPLIT) /* split window first */
1242 # ifdef FEAT_WINDOWS
1243 /* If 'switchbuf' contains "useopen": jump to first window containing
1244 * "buf" if one exists */
1245 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
1246 return OK;
1247 /* If 'switchbuf' contains "usetab": jump to first window in any tab
1248 * page containing "buf" if one exists */
1249 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
1250 return OK;
1251 if (win_split(0, 0) == FAIL)
1252 # endif
1253 return FAIL;
1255 #endif
1257 /* go to current buffer - nothing to do */
1258 if (buf == curbuf)
1259 return OK;
1262 * Check if the current buffer may be abandoned.
1264 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1266 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1267 if ((p_confirm || cmdmod.confirm) && p_write)
1269 dialog_changed(curbuf, FALSE);
1270 # ifdef FEAT_AUTOCMD
1271 if (!buf_valid(buf))
1272 /* Autocommand deleted buffer, oops! */
1273 return FAIL;
1274 # endif
1276 if (bufIsChanged(curbuf))
1277 #endif
1279 EMSG(_(e_nowrtmsg));
1280 return FAIL;
1284 /* Go to the other buffer. */
1285 set_curbuf(buf, action);
1287 #if defined(FEAT_LISTCMDS) && defined(FEAT_SCROLLBIND)
1288 if (action == DOBUF_SPLIT)
1289 curwin->w_p_scb = FALSE; /* reset 'scrollbind' */
1290 #endif
1292 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1293 if (aborting()) /* autocmds may abort script processing */
1294 return FAIL;
1295 #endif
1297 return OK;
1300 #endif /* FEAT_LISTCMDS */
1303 * Set current buffer to "buf". Executes autocommands and closes current
1304 * buffer. "action" tells how to close the current buffer:
1305 * DOBUF_GOTO free or hide it
1306 * DOBUF_SPLIT nothing
1307 * DOBUF_UNLOAD unload it
1308 * DOBUF_DEL delete it
1309 * DOBUF_WIPE wipe it out
1311 void
1312 set_curbuf(buf, action)
1313 buf_T *buf;
1314 int action;
1316 buf_T *prevbuf;
1317 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1318 || action == DOBUF_WIPE);
1320 setpcmark();
1321 if (!cmdmod.keepalt)
1322 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
1323 buflist_altfpos(curwin); /* remember curpos */
1325 #ifdef FEAT_VISUAL
1326 /* Don't restart Select mode after switching to another buffer. */
1327 VIsual_reselect = FALSE;
1328 #endif
1330 /* close_windows() or apply_autocmds() may change curbuf */
1331 prevbuf = curbuf;
1333 #ifdef FEAT_AUTOCMD
1334 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1335 # ifdef FEAT_EVAL
1336 if (buf_valid(prevbuf) && !aborting())
1337 # else
1338 if (buf_valid(prevbuf))
1339 # endif
1340 #endif
1342 #ifdef FEAT_WINDOWS
1343 if (unload)
1344 close_windows(prevbuf, FALSE);
1345 #endif
1346 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1347 if (buf_valid(prevbuf) && !aborting())
1348 #else
1349 if (buf_valid(prevbuf))
1350 #endif
1352 if (prevbuf == curbuf)
1353 u_sync(FALSE);
1354 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1355 unload ? action : (action == DOBUF_GOTO
1356 && !P_HID(prevbuf)
1357 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0);
1360 #ifdef FEAT_AUTOCMD
1361 /* An autocommand may have deleted "buf", already entered it (e.g., when
1362 * it did ":bunload") or aborted the script processing! */
1363 # ifdef FEAT_EVAL
1364 if (buf_valid(buf) && buf != curbuf && !aborting())
1365 # else
1366 if (buf_valid(buf) && buf != curbuf)
1367 # endif
1368 #endif
1369 enter_buffer(buf);
1373 * Enter a new current buffer.
1374 * Old curbuf must have been abandoned already!
1376 void
1377 enter_buffer(buf)
1378 buf_T *buf;
1380 /* Copy buffer and window local option values. Not for a help buffer. */
1381 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1382 if (!buf->b_help)
1383 get_winopts(buf);
1384 #ifdef FEAT_FOLDING
1385 else
1386 /* Remove all folds in the window. */
1387 clearFolding(curwin);
1388 foldUpdateAll(curwin); /* update folds (later). */
1389 #endif
1391 /* Get the buffer in the current window. */
1392 curwin->w_buffer = buf;
1393 curbuf = buf;
1394 ++curbuf->b_nwindows;
1396 #ifdef FEAT_DIFF
1397 if (curwin->w_p_diff)
1398 diff_buf_add(curbuf);
1399 #endif
1401 /* Cursor on first line by default. */
1402 curwin->w_cursor.lnum = 1;
1403 curwin->w_cursor.col = 0;
1404 #ifdef FEAT_VIRTUALEDIT
1405 curwin->w_cursor.coladd = 0;
1406 #endif
1407 curwin->w_set_curswant = TRUE;
1408 #ifdef FEAT_AUTOCMD
1409 curwin->w_topline_was_set = FALSE;
1410 #endif
1412 /* Make sure the buffer is loaded. */
1413 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
1415 #ifdef FEAT_AUTOCMD
1416 /* If there is no filetype, allow for detecting one. Esp. useful for
1417 * ":ball" used in a autocommand. If there already is a filetype we
1418 * might prefer to keep it. */
1419 if (*curbuf->b_p_ft == NUL)
1420 did_filetype = FALSE;
1421 #endif
1423 open_buffer(FALSE, NULL);
1425 else
1427 if (!msg_silent)
1428 need_fileinfo = TRUE; /* display file info after redraw */
1429 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1430 #ifdef FEAT_AUTOCMD
1431 curwin->w_topline = 1;
1432 # ifdef FEAT_DIFF
1433 curwin->w_topfill = 0;
1434 # endif
1435 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1436 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1437 #endif
1440 /* If autocommands did not change the cursor position, restore cursor lnum
1441 * and possibly cursor col. */
1442 if (curwin->w_cursor.lnum == 1 && inindent(0))
1443 buflist_getfpos();
1445 check_arg_idx(curwin); /* check for valid arg_idx */
1446 #ifdef FEAT_TITLE
1447 maketitle();
1448 #endif
1449 #ifdef FEAT_AUTOCMD
1450 /* when autocmds didn't change it */
1451 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
1452 #endif
1453 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1455 #ifdef FEAT_NETBEANS_INTG
1456 /* Send fileOpened event because we've changed buffers. */
1457 if (usingNetbeans && isNetbeansBuffer(curbuf))
1458 netbeans_file_activated(curbuf);
1459 #endif
1461 /* Change directories when the 'acd' option is set. */
1462 DO_AUTOCHDIR
1464 #ifdef FEAT_KEYMAP
1465 if (curbuf->b_kmap_state & KEYMAP_INIT)
1466 keymap_init();
1467 #endif
1468 #ifdef FEAT_SPELL
1469 /* May need to set the spell language. Can only do this after the buffer
1470 * has been properly setup. */
1471 if (!curbuf->b_help && curwin->w_p_spell && *curbuf->b_p_spl != NUL)
1472 did_set_spelllang(curbuf);
1473 #endif
1475 redraw_later(NOT_VALID);
1478 #if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1480 * Change to the directory of the current buffer.
1482 void
1483 do_autochdir()
1485 if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK)
1486 shorten_fnames(TRUE);
1488 #endif
1491 * functions for dealing with the buffer list
1495 * Add a file name to the buffer list. Return a pointer to the buffer.
1496 * If the same file name already exists return a pointer to that buffer.
1497 * If it does not exist, or if fname == NULL, a new entry is created.
1498 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1499 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1500 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
1501 * This is the ONLY way to create a new buffer.
1503 static int top_file_num = 1; /* highest file number */
1505 buf_T *
1506 buflist_new(ffname, sfname, lnum, flags)
1507 char_u *ffname; /* full path of fname or relative */
1508 char_u *sfname; /* short fname or NULL */
1509 linenr_T lnum; /* preferred cursor line */
1510 int flags; /* BLN_ defines */
1512 buf_T *buf;
1513 #ifdef UNIX
1514 struct stat st;
1515 #endif
1517 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1520 * If file name already exists in the list, update the entry.
1522 #ifdef UNIX
1523 /* On Unix we can use inode numbers when the file exists. Works better
1524 * for hard links. */
1525 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1526 st.st_dev = (dev_T)-1;
1527 #endif
1528 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1529 #ifdef UNIX
1530 buflist_findname_stat(ffname, &st)
1531 #else
1532 buflist_findname(ffname)
1533 #endif
1534 ) != NULL)
1536 vim_free(ffname);
1537 if (lnum != 0)
1538 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1539 /* copy the options now, if 'cpo' doesn't have 's' and not done
1540 * already */
1541 buf_copy_options(buf, 0);
1542 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1544 buf->b_p_bl = TRUE;
1545 #ifdef FEAT_AUTOCMD
1546 if (!(flags & BLN_DUMMY))
1547 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1548 #endif
1550 return buf;
1554 * If the current buffer has no name and no contents, use the current
1555 * buffer. Otherwise: Need to allocate a new buffer structure.
1557 * This is the ONLY place where a new buffer structure is allocated!
1558 * (A spell file buffer is allocated in spell.c, but that's not a normal
1559 * buffer.)
1561 buf = NULL;
1562 if ((flags & BLN_CURBUF)
1563 && curbuf != NULL
1564 && curbuf->b_ffname == NULL
1565 && curbuf->b_nwindows <= 1
1566 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1568 buf = curbuf;
1569 #ifdef FEAT_AUTOCMD
1570 /* It's like this buffer is deleted. Watch out for autocommands that
1571 * change curbuf! If that happens, allocate a new buffer anyway. */
1572 if (curbuf->b_p_bl)
1573 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1574 if (buf == curbuf)
1575 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1576 # ifdef FEAT_EVAL
1577 if (aborting()) /* autocmds may abort script processing */
1578 return NULL;
1579 # endif
1580 #endif
1581 #ifdef FEAT_QUICKFIX
1582 # ifdef FEAT_AUTOCMD
1583 if (buf == curbuf)
1584 # endif
1586 /* Make sure 'bufhidden' and 'buftype' are empty */
1587 clear_string_option(&buf->b_p_bh);
1588 clear_string_option(&buf->b_p_bt);
1590 #endif
1592 if (buf != curbuf || curbuf == NULL)
1594 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1595 if (buf == NULL)
1597 vim_free(ffname);
1598 return NULL;
1602 if (ffname != NULL)
1604 buf->b_ffname = ffname;
1605 buf->b_sfname = vim_strsave(sfname);
1608 clear_wininfo(buf);
1609 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1611 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1612 || buf->b_wininfo == NULL)
1614 vim_free(buf->b_ffname);
1615 buf->b_ffname = NULL;
1616 vim_free(buf->b_sfname);
1617 buf->b_sfname = NULL;
1618 if (buf != curbuf)
1619 free_buffer(buf);
1620 return NULL;
1623 if (buf == curbuf)
1625 /* free all things allocated for this buffer */
1626 buf_freeall(buf, FALSE, FALSE);
1627 if (buf != curbuf) /* autocommands deleted the buffer! */
1628 return NULL;
1629 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1630 if (aborting()) /* autocmds may abort script processing */
1631 return NULL;
1632 #endif
1633 /* buf->b_nwindows = 0; why was this here? */
1634 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
1635 #ifdef FEAT_KEYMAP
1636 /* need to reload lmaps and set b:keymap_name */
1637 curbuf->b_kmap_state |= KEYMAP_INIT;
1638 #endif
1640 else
1643 * put new buffer at the end of the buffer list
1645 buf->b_next = NULL;
1646 if (firstbuf == NULL) /* buffer list is empty */
1648 buf->b_prev = NULL;
1649 firstbuf = buf;
1651 else /* append new buffer at end of list */
1653 lastbuf->b_next = buf;
1654 buf->b_prev = lastbuf;
1656 lastbuf = buf;
1658 buf->b_fnum = top_file_num++;
1659 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1661 EMSG(_("W14: Warning: List of file names overflow"));
1662 if (emsg_silent == 0)
1664 out_flush();
1665 ui_delay(3000L, TRUE); /* make sure it is noticed */
1667 top_file_num = 1;
1671 * Always copy the options from the current buffer.
1673 buf_copy_options(buf, BCO_ALWAYS);
1676 buf->b_wininfo->wi_fpos.lnum = lnum;
1677 buf->b_wininfo->wi_win = curwin;
1679 #ifdef FEAT_EVAL
1680 init_var_dict(&buf->b_vars, &buf->b_bufvar); /* init b: variables */
1681 #endif
1682 #ifdef FEAT_SYN_HL
1683 hash_init(&buf->b_keywtab);
1684 hash_init(&buf->b_keywtab_ic);
1685 #endif
1687 buf->b_fname = buf->b_sfname;
1688 #ifdef UNIX
1689 if (st.st_dev == (dev_T)-1)
1690 buf->b_dev = -1;
1691 else
1693 buf->b_dev = st.st_dev;
1694 buf->b_ino = st.st_ino;
1696 #endif
1697 buf->b_u_synced = TRUE;
1698 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
1699 if (flags & BLN_DUMMY)
1700 buf->b_flags |= BF_DUMMY;
1701 buf_clear_file(buf);
1702 clrallmarks(buf); /* clear marks */
1703 fmarks_check_names(buf); /* check file marks for this file */
1704 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1705 #ifdef FEAT_AUTOCMD
1706 if (!(flags & BLN_DUMMY))
1708 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
1709 if (flags & BLN_LISTED)
1710 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1711 # ifdef FEAT_EVAL
1712 if (aborting()) /* autocmds may abort script processing */
1713 return NULL;
1714 # endif
1716 #endif
1718 return buf;
1722 * Free the memory for the options of a buffer.
1723 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1724 * 'fileencoding'.
1726 void
1727 free_buf_options(buf, free_p_ff)
1728 buf_T *buf;
1729 int free_p_ff;
1731 if (free_p_ff)
1733 #ifdef FEAT_MBYTE
1734 clear_string_option(&buf->b_p_fenc);
1735 #endif
1736 clear_string_option(&buf->b_p_ff);
1737 #ifdef FEAT_QUICKFIX
1738 clear_string_option(&buf->b_p_bh);
1739 clear_string_option(&buf->b_p_bt);
1740 #endif
1742 #ifdef FEAT_FIND_ID
1743 clear_string_option(&buf->b_p_def);
1744 clear_string_option(&buf->b_p_inc);
1745 # ifdef FEAT_EVAL
1746 clear_string_option(&buf->b_p_inex);
1747 # endif
1748 #endif
1749 #if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1750 clear_string_option(&buf->b_p_inde);
1751 clear_string_option(&buf->b_p_indk);
1752 #endif
1753 #if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1754 clear_string_option(&buf->b_p_bexpr);
1755 #endif
1756 #if defined(FEAT_EVAL)
1757 clear_string_option(&buf->b_p_fex);
1758 #endif
1759 #ifdef FEAT_CRYPT
1760 clear_string_option(&buf->b_p_key);
1761 #endif
1762 clear_string_option(&buf->b_p_kp);
1763 clear_string_option(&buf->b_p_mps);
1764 clear_string_option(&buf->b_p_fo);
1765 clear_string_option(&buf->b_p_flp);
1766 clear_string_option(&buf->b_p_isk);
1767 #ifdef FEAT_KEYMAP
1768 clear_string_option(&buf->b_p_keymap);
1769 ga_clear(&buf->b_kmap_ga);
1770 #endif
1771 #ifdef FEAT_COMMENTS
1772 clear_string_option(&buf->b_p_com);
1773 #endif
1774 #ifdef FEAT_FOLDING
1775 clear_string_option(&buf->b_p_cms);
1776 #endif
1777 clear_string_option(&buf->b_p_nf);
1778 #ifdef FEAT_SYN_HL
1779 clear_string_option(&buf->b_p_syn);
1780 #endif
1781 #ifdef FEAT_SPELL
1782 clear_string_option(&buf->b_p_spc);
1783 clear_string_option(&buf->b_p_spf);
1784 vim_free(buf->b_cap_prog);
1785 buf->b_cap_prog = NULL;
1786 clear_string_option(&buf->b_p_spl);
1787 #endif
1788 #ifdef FEAT_SEARCHPATH
1789 clear_string_option(&buf->b_p_sua);
1790 #endif
1791 #ifdef FEAT_AUTOCMD
1792 clear_string_option(&buf->b_p_ft);
1793 #endif
1794 #ifdef FEAT_OSFILETYPE
1795 clear_string_option(&buf->b_p_oft);
1796 #endif
1797 #ifdef FEAT_CINDENT
1798 clear_string_option(&buf->b_p_cink);
1799 clear_string_option(&buf->b_p_cino);
1800 #endif
1801 #if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1802 clear_string_option(&buf->b_p_cinw);
1803 #endif
1804 #ifdef FEAT_INS_EXPAND
1805 clear_string_option(&buf->b_p_cpt);
1806 #endif
1807 #ifdef FEAT_COMPL_FUNC
1808 clear_string_option(&buf->b_p_cfu);
1809 clear_string_option(&buf->b_p_ofu);
1810 #endif
1811 #ifdef FEAT_QUICKFIX
1812 clear_string_option(&buf->b_p_gp);
1813 clear_string_option(&buf->b_p_mp);
1814 clear_string_option(&buf->b_p_efm);
1815 #endif
1816 clear_string_option(&buf->b_p_ep);
1817 clear_string_option(&buf->b_p_path);
1818 clear_string_option(&buf->b_p_tags);
1819 #ifdef FEAT_INS_EXPAND
1820 clear_string_option(&buf->b_p_dict);
1821 clear_string_option(&buf->b_p_tsr);
1822 #endif
1823 #ifdef FEAT_TEXTOBJ
1824 clear_string_option(&buf->b_p_qe);
1825 #endif
1826 buf->b_p_ar = -1;
1830 * get alternate file n
1831 * set linenr to lnum or altfpos.lnum if lnum == 0
1832 * also set cursor column to altfpos.col if 'startofline' is not set.
1833 * if (options & GETF_SETMARK) call setpcmark()
1834 * if (options & GETF_ALT) we are jumping to an alternate file.
1835 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1837 * return FAIL for failure, OK for success
1840 buflist_getfile(n, lnum, options, forceit)
1841 int n;
1842 linenr_T lnum;
1843 int options;
1844 int forceit;
1846 buf_T *buf;
1847 #ifdef FEAT_WINDOWS
1848 win_T *wp = NULL;
1849 #endif
1850 pos_T *fpos;
1851 colnr_T col;
1853 buf = buflist_findnr(n);
1854 if (buf == NULL)
1856 if ((options & GETF_ALT) && n == 0)
1857 EMSG(_(e_noalt));
1858 else
1859 EMSGN(_("E92: Buffer %ld not found"), n);
1860 return FAIL;
1863 /* if alternate file is the current buffer, nothing to do */
1864 if (buf == curbuf)
1865 return OK;
1867 if (text_locked())
1869 text_locked_msg();
1870 return FAIL;
1872 #ifdef FEAT_AUTOCMD
1873 if (curbuf_locked())
1874 return FAIL;
1875 #endif
1877 /* altfpos may be changed by getfile(), get it now */
1878 if (lnum == 0)
1880 fpos = buflist_findfpos(buf);
1881 lnum = fpos->lnum;
1882 col = fpos->col;
1884 else
1885 col = 0;
1887 #ifdef FEAT_WINDOWS
1888 if (options & GETF_SWITCH)
1890 /* If 'switchbuf' contains "useopen": jump to first window containing
1891 * "buf" if one exists */
1892 if (swb_flags & SWB_USEOPEN)
1893 wp = buf_jump_open_win(buf);
1894 /* If 'switchbuf' contians "usetab": jump to first window in any tab
1895 * page containing "buf" if one exists */
1896 if (wp == NULL && (swb_flags & SWB_USETAB))
1897 wp = buf_jump_open_tab(buf);
1898 /* If 'switchbuf' contains "split" or "newtab" and the current buffer
1899 * isn't empty: open new window */
1900 if (wp == NULL && (swb_flags & (SWB_SPLIT | SWB_NEWTAB)) && !bufempty())
1902 if (swb_flags & SWB_NEWTAB) /* Open in a new tab */
1903 tabpage_new();
1904 else if (win_split(0, 0) == FAIL) /* Open in a new window */
1905 return FAIL;
1906 # ifdef FEAT_SCROLLBIND
1907 curwin->w_p_scb = FALSE;
1908 # endif
1911 #endif
1913 ++RedrawingDisabled;
1914 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
1915 lnum, forceit) <= 0)
1917 --RedrawingDisabled;
1919 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
1920 if (!p_sol && col != 0)
1922 curwin->w_cursor.col = col;
1923 check_cursor_col();
1924 #ifdef FEAT_VIRTUALEDIT
1925 curwin->w_cursor.coladd = 0;
1926 #endif
1927 curwin->w_set_curswant = TRUE;
1929 return OK;
1931 --RedrawingDisabled;
1932 return FAIL;
1936 * go to the last know line number for the current buffer
1938 void
1939 buflist_getfpos()
1941 pos_T *fpos;
1943 fpos = buflist_findfpos(curbuf);
1945 curwin->w_cursor.lnum = fpos->lnum;
1946 check_cursor_lnum();
1948 if (p_sol)
1949 curwin->w_cursor.col = 0;
1950 else
1952 curwin->w_cursor.col = fpos->col;
1953 check_cursor_col();
1954 #ifdef FEAT_VIRTUALEDIT
1955 curwin->w_cursor.coladd = 0;
1956 #endif
1957 curwin->w_set_curswant = TRUE;
1961 #if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
1963 * Find file in buffer list by name (it has to be for the current window).
1964 * Returns NULL if not found.
1966 buf_T *
1967 buflist_findname_exp(fname)
1968 char_u *fname;
1970 char_u *ffname;
1971 buf_T *buf = NULL;
1973 /* First make the name into a full path name */
1974 ffname = FullName_save(fname,
1975 #ifdef UNIX
1976 TRUE /* force expansion, get rid of symbolic links */
1977 #else
1978 FALSE
1979 #endif
1981 if (ffname != NULL)
1983 buf = buflist_findname(ffname);
1984 vim_free(ffname);
1986 return buf;
1988 #endif
1991 * Find file in buffer list by name (it has to be for the current window).
1992 * "ffname" must have a full path.
1993 * Skips dummy buffers.
1994 * Returns NULL if not found.
1996 buf_T *
1997 buflist_findname(ffname)
1998 char_u *ffname;
2000 #ifdef UNIX
2001 struct stat st;
2003 if (mch_stat((char *)ffname, &st) < 0)
2004 st.st_dev = (dev_T)-1;
2005 return buflist_findname_stat(ffname, &st);
2009 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2010 * twice for the same file.
2011 * Returns NULL if not found.
2013 static buf_T *
2014 buflist_findname_stat(ffname, stp)
2015 char_u *ffname;
2016 struct stat *stp;
2018 #endif
2019 buf_T *buf;
2021 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2022 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
2023 #ifdef UNIX
2024 , stp
2025 #endif
2027 return buf;
2028 return NULL;
2031 #if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO)
2033 * Find file in buffer list by a regexp pattern.
2034 * Return fnum of the found buffer.
2035 * Return < 0 for error.
2037 /*ARGSUSED*/
2039 buflist_findpat(pattern, pattern_end, unlisted, diffmode)
2040 char_u *pattern;
2041 char_u *pattern_end; /* pointer to first char after pattern */
2042 int unlisted; /* find unlisted buffers */
2043 int diffmode; /* find diff-mode buffers only */
2045 buf_T *buf;
2046 regprog_T *prog;
2047 int match = -1;
2048 int find_listed;
2049 char_u *pat;
2050 char_u *patend;
2051 int attempt;
2052 char_u *p;
2053 int toggledollar;
2055 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2057 if (*pattern == '%')
2058 match = curbuf->b_fnum;
2059 else
2060 match = curwin->w_alt_fnum;
2061 #ifdef FEAT_DIFF
2062 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2063 match = -1;
2064 #endif
2068 * Try four ways of matching a listed buffer:
2069 * attempt == 0: without '^' or '$' (at any position)
2070 * attempt == 1: with '^' at start (only at position 0)
2071 * attempt == 2: with '$' at end (only match at end)
2072 * attempt == 3: with '^' at start and '$' at end (only full match)
2073 * Repeat this for finding an unlisted buffer if there was no matching
2074 * listed buffer.
2076 else
2078 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2079 if (pat == NULL)
2080 return -1;
2081 patend = pat + STRLEN(pat) - 1;
2082 toggledollar = (patend > pat && *patend == '$');
2084 /* First try finding a listed buffer. If not found and "unlisted"
2085 * is TRUE, try finding an unlisted buffer. */
2086 find_listed = TRUE;
2087 for (;;)
2089 for (attempt = 0; attempt <= 3; ++attempt)
2091 /* may add '^' and '$' */
2092 if (toggledollar)
2093 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2094 p = pat;
2095 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2096 ++p;
2097 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2098 if (prog == NULL)
2100 vim_free(pat);
2101 return -1;
2104 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2105 if (buf->b_p_bl == find_listed
2106 #ifdef FEAT_DIFF
2107 && (!diffmode || diff_mode_buf(buf))
2108 #endif
2109 && buflist_match(prog, buf) != NULL)
2111 if (match >= 0) /* already found a match */
2113 match = -2;
2114 break;
2116 match = buf->b_fnum; /* remember first match */
2119 vim_free(prog);
2120 if (match >= 0) /* found one match */
2121 break;
2124 /* Only search for unlisted buffers if there was no match with
2125 * a listed buffer. */
2126 if (!unlisted || !find_listed || match != -1)
2127 break;
2128 find_listed = FALSE;
2131 vim_free(pat);
2134 if (match == -2)
2135 EMSG2(_("E93: More than one match for %s"), pattern);
2136 else if (match < 0)
2137 EMSG2(_("E94: No matching buffer for %s"), pattern);
2138 return match;
2140 #endif
2142 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2145 * Find all buffer names that match.
2146 * For command line expansion of ":buf" and ":sbuf".
2147 * Return OK if matches found, FAIL otherwise.
2150 ExpandBufnames(pat, num_file, file, options)
2151 char_u *pat;
2152 int *num_file;
2153 char_u ***file;
2154 int options;
2156 int count = 0;
2157 buf_T *buf;
2158 int round;
2159 char_u *p;
2160 int attempt;
2161 regprog_T *prog;
2162 char_u *patc;
2164 *num_file = 0; /* return values in case of FAIL */
2165 *file = NULL;
2167 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2168 if (*pat == '^')
2170 patc = alloc((unsigned)STRLEN(pat) + 11);
2171 if (patc == NULL)
2172 return FAIL;
2173 STRCPY(patc, "\\(^\\|[\\/]\\)");
2174 STRCPY(patc + 11, pat + 1);
2176 else
2177 patc = pat;
2180 * attempt == 0: try match with '\<', match at start of word
2181 * attempt == 1: try match without '\<', match anywhere
2183 for (attempt = 0; attempt <= 1; ++attempt)
2185 if (attempt > 0 && patc == pat)
2186 break; /* there was no anchor, no need to try again */
2187 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2188 if (prog == NULL)
2190 if (patc != pat)
2191 vim_free(patc);
2192 return FAIL;
2196 * round == 1: Count the matches.
2197 * round == 2: Build the array to keep the matches.
2199 for (round = 1; round <= 2; ++round)
2201 count = 0;
2202 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2204 if (!buf->b_p_bl) /* skip unlisted buffers */
2205 continue;
2206 p = buflist_match(prog, buf);
2207 if (p != NULL)
2209 if (round == 1)
2210 ++count;
2211 else
2213 if (options & WILD_HOME_REPLACE)
2214 p = home_replace_save(buf, p);
2215 else
2216 p = vim_strsave(p);
2217 (*file)[count++] = p;
2221 if (count == 0) /* no match found, break here */
2222 break;
2223 if (round == 1)
2225 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2226 if (*file == NULL)
2228 vim_free(prog);
2229 if (patc != pat)
2230 vim_free(patc);
2231 return FAIL;
2235 vim_free(prog);
2236 if (count) /* match(es) found, break here */
2237 break;
2240 if (patc != pat)
2241 vim_free(patc);
2243 *num_file = count;
2244 return (count == 0 ? FAIL : OK);
2247 #endif /* FEAT_CMDL_COMPL */
2249 #ifdef HAVE_BUFLIST_MATCH
2251 * Check for a match on the file name for buffer "buf" with regprog "prog".
2253 static char_u *
2254 buflist_match(prog, buf)
2255 regprog_T *prog;
2256 buf_T *buf;
2258 char_u *match;
2260 /* First try the short file name, then the long file name. */
2261 match = fname_match(prog, buf->b_sfname);
2262 if (match == NULL)
2263 match = fname_match(prog, buf->b_ffname);
2265 return match;
2269 * Try matching the regexp in "prog" with file name "name".
2270 * Return "name" when there is a match, NULL when not.
2272 static char_u *
2273 fname_match(prog, name)
2274 regprog_T *prog;
2275 char_u *name;
2277 char_u *match = NULL;
2278 char_u *p;
2279 regmatch_T regmatch;
2281 if (name != NULL)
2283 regmatch.regprog = prog;
2284 #ifdef CASE_INSENSITIVE_FILENAME
2285 regmatch.rm_ic = TRUE; /* Always ignore case */
2286 #else
2287 regmatch.rm_ic = FALSE; /* Never ignore case */
2288 #endif
2290 if (vim_regexec(&regmatch, name, (colnr_T)0))
2291 match = name;
2292 else
2294 /* Replace $(HOME) with '~' and try matching again. */
2295 p = home_replace_save(NULL, name);
2296 if (p != NULL && vim_regexec(&regmatch, p, (colnr_T)0))
2297 match = name;
2298 vim_free(p);
2302 return match;
2304 #endif
2307 * find file in buffer list by number
2309 buf_T *
2310 buflist_findnr(nr)
2311 int nr;
2313 buf_T *buf;
2315 if (nr == 0)
2316 nr = curwin->w_alt_fnum;
2317 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2318 if (buf->b_fnum == nr)
2319 return (buf);
2320 return NULL;
2324 * Get name of file 'n' in the buffer list.
2325 * When the file has no name an empty string is returned.
2326 * home_replace() is used to shorten the file name (used for marks).
2327 * Returns a pointer to allocated memory, of NULL when failed.
2329 char_u *
2330 buflist_nr2name(n, fullname, helptail)
2331 int n;
2332 int fullname;
2333 int helptail; /* for help buffers return tail only */
2335 buf_T *buf;
2337 buf = buflist_findnr(n);
2338 if (buf == NULL)
2339 return NULL;
2340 return home_replace_save(helptail ? buf : NULL,
2341 fullname ? buf->b_ffname : buf->b_fname);
2345 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2346 * When "copy_options" is TRUE save the local window option values.
2347 * When "lnum" is 0 only do the options.
2349 static void
2350 buflist_setfpos(buf, win, lnum, col, copy_options)
2351 buf_T *buf;
2352 win_T *win;
2353 linenr_T lnum;
2354 colnr_T col;
2355 int copy_options;
2357 wininfo_T *wip;
2359 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2360 if (wip->wi_win == win)
2361 break;
2362 if (wip == NULL)
2364 /* allocate a new entry */
2365 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2366 if (wip == NULL)
2367 return;
2368 wip->wi_win = win;
2369 if (lnum == 0) /* set lnum even when it's 0 */
2370 lnum = 1;
2372 else
2374 /* remove the entry from the list */
2375 if (wip->wi_prev)
2376 wip->wi_prev->wi_next = wip->wi_next;
2377 else
2378 buf->b_wininfo = wip->wi_next;
2379 if (wip->wi_next)
2380 wip->wi_next->wi_prev = wip->wi_prev;
2381 if (copy_options && wip->wi_optset)
2383 clear_winopt(&wip->wi_opt);
2384 #ifdef FEAT_FOLDING
2385 deleteFoldRecurse(&wip->wi_folds);
2386 #endif
2389 if (lnum != 0)
2391 wip->wi_fpos.lnum = lnum;
2392 wip->wi_fpos.col = col;
2394 if (copy_options)
2396 /* Save the window-specific option values. */
2397 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2398 #ifdef FEAT_FOLDING
2399 wip->wi_fold_manual = win->w_fold_manual;
2400 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2401 #endif
2402 wip->wi_optset = TRUE;
2405 /* insert the entry in front of the list */
2406 wip->wi_next = buf->b_wininfo;
2407 buf->b_wininfo = wip;
2408 wip->wi_prev = NULL;
2409 if (wip->wi_next)
2410 wip->wi_next->wi_prev = wip;
2412 return;
2415 #ifdef FEAT_DIFF
2416 static int wininfo_other_tab_diff __ARGS((wininfo_T *wip));
2419 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2420 * page. That's because a diff is local to a tab page.
2422 static int
2423 wininfo_other_tab_diff(wip)
2424 wininfo_T *wip;
2426 win_T *wp;
2428 if (wip->wi_opt.wo_diff)
2430 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2431 /* return FALSE when it's a window in the current tab page, thus
2432 * the buffer was in diff mode here */
2433 if (wip->wi_win == wp)
2434 return FALSE;
2435 return TRUE;
2437 return FALSE;
2439 #endif
2442 * Find info for the current window in buffer "buf".
2443 * If not found, return the info for the most recently used window.
2444 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2445 * another tab page.
2446 * Returns NULL when there isn't any info.
2448 /*ARGSUSED*/
2449 static wininfo_T *
2450 find_wininfo(buf, skip_diff_buffer)
2451 buf_T *buf;
2452 int skip_diff_buffer;
2454 wininfo_T *wip;
2456 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2457 if (wip->wi_win == curwin
2458 #ifdef FEAT_DIFF
2459 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2460 #endif
2462 break;
2464 /* If no wininfo for curwin, use the first in the list (that doesn't have
2465 * 'diff' set and is in another tab page). */
2466 if (wip == NULL)
2468 #ifdef FEAT_DIFF
2469 if (skip_diff_buffer)
2471 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2472 if (!wininfo_other_tab_diff(wip))
2473 break;
2475 else
2476 #endif
2477 wip = buf->b_wininfo;
2479 return wip;
2483 * Reset the local window options to the values last used in this window.
2484 * If the buffer wasn't used in this window before, use the values from
2485 * the most recently used window. If the values were never set, use the
2486 * global values for the window.
2488 void
2489 get_winopts(buf)
2490 buf_T *buf;
2492 wininfo_T *wip;
2494 clear_winopt(&curwin->w_onebuf_opt);
2495 #ifdef FEAT_FOLDING
2496 clearFolding(curwin);
2497 #endif
2499 wip = find_wininfo(buf, TRUE);
2500 if (wip != NULL && wip->wi_optset)
2502 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2503 #ifdef FEAT_FOLDING
2504 curwin->w_fold_manual = wip->wi_fold_manual;
2505 curwin->w_foldinvalid = TRUE;
2506 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2507 #endif
2509 else
2510 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2512 #ifdef FEAT_FOLDING
2513 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2514 if (p_fdls >= 0)
2515 curwin->w_p_fdl = p_fdls;
2516 #endif
2520 * Find the position (lnum and col) for the buffer 'buf' for the current
2521 * window.
2522 * Returns a pointer to no_position if no position is found.
2524 pos_T *
2525 buflist_findfpos(buf)
2526 buf_T *buf;
2528 wininfo_T *wip;
2529 static pos_T no_position = {1, 0};
2531 wip = find_wininfo(buf, FALSE);
2532 if (wip != NULL)
2533 return &(wip->wi_fpos);
2534 else
2535 return &no_position;
2539 * Find the lnum for the buffer 'buf' for the current window.
2541 linenr_T
2542 buflist_findlnum(buf)
2543 buf_T *buf;
2545 return buflist_findfpos(buf)->lnum;
2548 #if defined(FEAT_LISTCMDS) || defined(PROTO)
2550 * List all know file names (for :files and :buffers command).
2552 /*ARGSUSED*/
2553 void
2554 buflist_list(eap)
2555 exarg_T *eap;
2557 buf_T *buf;
2558 int len;
2559 int i;
2561 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2563 /* skip unlisted buffers, unless ! was used */
2564 if (!buf->b_p_bl && !eap->forceit)
2565 continue;
2566 msg_putchar('\n');
2567 if (buf_spname(buf) != NULL)
2568 STRCPY(NameBuff, buf_spname(buf));
2569 else
2570 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2572 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
2573 buf->b_fnum,
2574 buf->b_p_bl ? ' ' : 'u',
2575 buf == curbuf ? '%' :
2576 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2577 buf->b_ml.ml_mfp == NULL ? ' ' :
2578 (buf->b_nwindows == 0 ? 'h' : 'a'),
2579 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2580 (buf->b_flags & BF_READERR) ? 'x'
2581 : (bufIsChanged(buf) ? '+' : ' '),
2582 NameBuff);
2584 /* put "line 999" in column 40 or after the file name */
2585 i = 40 - vim_strsize(IObuff);
2588 IObuff[len++] = ' ';
2589 } while (--i > 0 && len < IOSIZE - 18);
2590 vim_snprintf((char *)IObuff + len, IOSIZE - len, _("line %ld"),
2591 buf == curbuf ? curwin->w_cursor.lnum
2592 : (long)buflist_findlnum(buf));
2593 msg_outtrans(IObuff);
2594 out_flush(); /* output one line at a time */
2595 ui_breakcheck();
2598 #endif
2601 * Get file name and line number for file 'fnum'.
2602 * Used by DoOneCmd() for translating '%' and '#'.
2603 * Used by insert_reg() and cmdline_paste() for '#' register.
2604 * Return FAIL if not found, OK for success.
2607 buflist_name_nr(fnum, fname, lnum)
2608 int fnum;
2609 char_u **fname;
2610 linenr_T *lnum;
2612 buf_T *buf;
2614 buf = buflist_findnr(fnum);
2615 if (buf == NULL || buf->b_fname == NULL)
2616 return FAIL;
2618 *fname = buf->b_fname;
2619 *lnum = buflist_findlnum(buf);
2621 return OK;
2625 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2626 * The file name with the full path is also remembered, for when :cd is used.
2627 * Returns FAIL for failure (file name already in use by other buffer)
2628 * OK otherwise.
2631 setfname(buf, ffname, sfname, message)
2632 buf_T *buf;
2633 char_u *ffname, *sfname;
2634 int message; /* give message when buffer already exists */
2636 buf_T *obuf = NULL;
2637 #ifdef UNIX
2638 struct stat st;
2639 #endif
2641 if (ffname == NULL || *ffname == NUL)
2643 /* Removing the name. */
2644 vim_free(buf->b_ffname);
2645 vim_free(buf->b_sfname);
2646 buf->b_ffname = NULL;
2647 buf->b_sfname = NULL;
2648 #ifdef UNIX
2649 st.st_dev = (dev_T)-1;
2650 #endif
2652 else
2654 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2655 if (ffname == NULL) /* out of memory */
2656 return FAIL;
2659 * if the file name is already used in another buffer:
2660 * - if the buffer is loaded, fail
2661 * - if the buffer is not loaded, delete it from the list
2663 #ifdef UNIX
2664 if (mch_stat((char *)ffname, &st) < 0)
2665 st.st_dev = (dev_T)-1;
2666 #endif
2667 if (!(buf->b_flags & BF_DUMMY))
2668 #ifdef UNIX
2669 obuf = buflist_findname_stat(ffname, &st);
2670 #else
2671 obuf = buflist_findname(ffname);
2672 #endif
2673 if (obuf != NULL && obuf != buf)
2675 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2677 if (message)
2678 EMSG(_("E95: Buffer with this name already exists"));
2679 vim_free(ffname);
2680 return FAIL;
2682 close_buffer(NULL, obuf, DOBUF_WIPE); /* delete from the list */
2684 sfname = vim_strsave(sfname);
2685 if (ffname == NULL || sfname == NULL)
2687 vim_free(sfname);
2688 vim_free(ffname);
2689 return FAIL;
2691 #ifdef USE_FNAME_CASE
2692 # ifdef USE_LONG_FNAME
2693 if (USE_LONG_FNAME)
2694 # endif
2695 fname_case(sfname, 0); /* set correct case for short file name */
2696 #endif
2697 vim_free(buf->b_ffname);
2698 vim_free(buf->b_sfname);
2699 buf->b_ffname = ffname;
2700 buf->b_sfname = sfname;
2702 buf->b_fname = buf->b_sfname;
2703 #ifdef UNIX
2704 if (st.st_dev == (dev_T)-1)
2705 buf->b_dev = -1;
2706 else
2708 buf->b_dev = st.st_dev;
2709 buf->b_ino = st.st_ino;
2711 #endif
2713 #ifndef SHORT_FNAME
2714 buf->b_shortname = FALSE;
2715 #endif
2717 buf_name_changed(buf);
2718 return OK;
2722 * Crude way of changing the name of a buffer. Use with care!
2723 * The name should be relative to the current directory.
2725 void
2726 buf_set_name(fnum, name)
2727 int fnum;
2728 char_u *name;
2730 buf_T *buf;
2732 buf = buflist_findnr(fnum);
2733 if (buf != NULL)
2735 vim_free(buf->b_sfname);
2736 vim_free(buf->b_ffname);
2737 buf->b_ffname = vim_strsave(name);
2738 buf->b_sfname = NULL;
2739 /* Allocate ffname and expand into full path. Also resolves .lnk
2740 * files on Win32. */
2741 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
2742 buf->b_fname = buf->b_sfname;
2747 * Take care of what needs to be done when the name of buffer "buf" has
2748 * changed.
2750 void
2751 buf_name_changed(buf)
2752 buf_T *buf;
2755 * If the file name changed, also change the name of the swapfile
2757 if (buf->b_ml.ml_mfp != NULL)
2758 ml_setname(buf);
2760 if (curwin->w_buffer == buf)
2761 check_arg_idx(curwin); /* check file name for arg list */
2762 #ifdef FEAT_TITLE
2763 maketitle(); /* set window title */
2764 #endif
2765 #ifdef FEAT_WINDOWS
2766 status_redraw_all(); /* status lines need to be redrawn */
2767 #endif
2768 fmarks_check_names(buf); /* check named file marks */
2769 ml_timestamp(buf); /* reset timestamp */
2773 * set alternate file name for current window
2775 * Used by do_one_cmd(), do_write() and do_ecmd().
2776 * Return the buffer.
2778 buf_T *
2779 setaltfname(ffname, sfname, lnum)
2780 char_u *ffname;
2781 char_u *sfname;
2782 linenr_T lnum;
2784 buf_T *buf;
2786 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2787 buf = buflist_new(ffname, sfname, lnum, 0);
2788 if (buf != NULL && !cmdmod.keepalt)
2789 curwin->w_alt_fnum = buf->b_fnum;
2790 return buf;
2794 * Get alternate file name for current window.
2795 * Return NULL if there isn't any, and give error message if requested.
2797 char_u *
2798 getaltfname(errmsg)
2799 int errmsg; /* give error message */
2801 char_u *fname;
2802 linenr_T dummy;
2804 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2806 if (errmsg)
2807 EMSG(_(e_noalt));
2808 return NULL;
2810 return fname;
2814 * Add a file name to the buflist and return its number.
2815 * Uses same flags as buflist_new(), except BLN_DUMMY.
2817 * used by qf_init(), main() and doarglist()
2820 buflist_add(fname, flags)
2821 char_u *fname;
2822 int flags;
2824 buf_T *buf;
2826 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
2827 if (buf != NULL)
2828 return buf->b_fnum;
2829 return 0;
2832 #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2834 * Adjust slashes in file names. Called after 'shellslash' was set.
2836 void
2837 buflist_slash_adjust()
2839 buf_T *bp;
2841 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
2843 if (bp->b_ffname != NULL)
2844 slash_adjust(bp->b_ffname);
2845 if (bp->b_sfname != NULL)
2846 slash_adjust(bp->b_sfname);
2849 #endif
2852 * Set alternate cursor position for the current buffer and window "win".
2853 * Also save the local window option values.
2855 void
2856 buflist_altfpos(win)
2857 win_T *win;
2859 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
2863 * Return TRUE if 'ffname' is not the same file as current file.
2864 * Fname must have a full path (expanded by mch_FullName()).
2867 otherfile(ffname)
2868 char_u *ffname;
2870 return otherfile_buf(curbuf, ffname
2871 #ifdef UNIX
2872 , NULL
2873 #endif
2877 static int
2878 otherfile_buf(buf, ffname
2879 #ifdef UNIX
2880 , stp
2881 #endif
2883 buf_T *buf;
2884 char_u *ffname;
2885 #ifdef UNIX
2886 struct stat *stp;
2887 #endif
2889 /* no name is different */
2890 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
2891 return TRUE;
2892 if (fnamecmp(ffname, buf->b_ffname) == 0)
2893 return FALSE;
2894 #ifdef UNIX
2896 struct stat st;
2898 /* If no struct stat given, get it now */
2899 if (stp == NULL)
2901 if (buf->b_dev < 0 || mch_stat((char *)ffname, &st) < 0)
2902 st.st_dev = (dev_T)-1;
2903 stp = &st;
2905 /* Use dev/ino to check if the files are the same, even when the names
2906 * are different (possible with links). Still need to compare the
2907 * name above, for when the file doesn't exist yet.
2908 * Problem: The dev/ino changes when a file is deleted (and created
2909 * again) and remains the same when renamed/moved. We don't want to
2910 * mch_stat() each buffer each time, that would be too slow. Get the
2911 * dev/ino again when they appear to match, but not when they appear
2912 * to be different: Could skip a buffer when it's actually the same
2913 * file. */
2914 if (buf_same_ino(buf, stp))
2916 buf_setino(buf);
2917 if (buf_same_ino(buf, stp))
2918 return FALSE;
2921 #endif
2922 return TRUE;
2925 #if defined(UNIX) || defined(PROTO)
2927 * Set inode and device number for a buffer.
2928 * Must always be called when b_fname is changed!.
2930 void
2931 buf_setino(buf)
2932 buf_T *buf;
2934 struct stat st;
2936 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
2938 buf->b_dev = st.st_dev;
2939 buf->b_ino = st.st_ino;
2941 else
2942 buf->b_dev = -1;
2946 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
2948 static int
2949 buf_same_ino(buf, stp)
2950 buf_T *buf;
2951 struct stat *stp;
2953 return (buf->b_dev >= 0
2954 && stp->st_dev == buf->b_dev
2955 && stp->st_ino == buf->b_ino);
2957 #endif
2960 * Print info about the current buffer.
2962 void
2963 fileinfo(fullname, shorthelp, dont_truncate)
2964 int fullname; /* when non-zero print full path */
2965 int shorthelp;
2966 int dont_truncate;
2968 char_u *name;
2969 int n;
2970 char_u *p;
2971 char_u *buffer;
2972 size_t len;
2974 buffer = alloc(IOSIZE);
2975 if (buffer == NULL)
2976 return;
2978 if (fullname > 1) /* 2 CTRL-G: include buffer number */
2980 sprintf((char *)buffer, "buf %d: ", curbuf->b_fnum);
2981 p = buffer + STRLEN(buffer);
2983 else
2984 p = buffer;
2986 *p++ = '"';
2987 if (buf_spname(curbuf) != NULL)
2988 STRCPY(p, buf_spname(curbuf));
2989 else
2991 if (!fullname && curbuf->b_fname != NULL)
2992 name = curbuf->b_fname;
2993 else
2994 name = curbuf->b_ffname;
2995 home_replace(shorthelp ? curbuf : NULL, name, p,
2996 (int)(IOSIZE - (p - buffer)), TRUE);
2999 len = STRLEN(buffer);
3000 vim_snprintf((char *)buffer + len, IOSIZE - len,
3001 "\"%s%s%s%s%s%s",
3002 curbufIsChanged() ? (shortmess(SHM_MOD)
3003 ? " [+]" : _(" [Modified]")) : " ",
3004 (curbuf->b_flags & BF_NOTEDITED)
3005 #ifdef FEAT_QUICKFIX
3006 && !bt_dontwrite(curbuf)
3007 #endif
3008 ? _("[Not edited]") : "",
3009 (curbuf->b_flags & BF_NEW)
3010 #ifdef FEAT_QUICKFIX
3011 && !bt_dontwrite(curbuf)
3012 #endif
3013 ? _("[New file]") : "",
3014 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
3015 curbuf->b_p_ro ? (shortmess(SHM_RO) ? "[RO]"
3016 : _("[readonly]")) : "",
3017 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3018 || curbuf->b_p_ro) ?
3019 " " : "");
3020 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3021 * causes an overflow, thus for large numbers divide instead. */
3022 if (curwin->w_cursor.lnum > 1000000L)
3023 n = (int)(((long)curwin->w_cursor.lnum) /
3024 ((long)curbuf->b_ml.ml_line_count / 100L));
3025 else
3026 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3027 (long)curbuf->b_ml.ml_line_count);
3028 len = STRLEN(buffer);
3029 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3031 vim_snprintf((char *)buffer + len, IOSIZE - len, "%s", _(no_lines_msg));
3033 #ifdef FEAT_CMDL_INFO
3034 else if (p_ru)
3036 /* Current line and column are already on the screen -- webb */
3037 if (curbuf->b_ml.ml_line_count == 1)
3038 vim_snprintf((char *)buffer + len, IOSIZE - len,
3039 _("1 line --%d%%--"), n);
3040 else
3041 vim_snprintf((char *)buffer + len, IOSIZE - len,
3042 _("%ld lines --%d%%--"),
3043 (long)curbuf->b_ml.ml_line_count, n);
3045 #endif
3046 else
3048 vim_snprintf((char *)buffer + len, IOSIZE - len,
3049 _("line %ld of %ld --%d%%-- col "),
3050 (long)curwin->w_cursor.lnum,
3051 (long)curbuf->b_ml.ml_line_count,
3053 validate_virtcol();
3054 col_print(buffer + STRLEN(buffer),
3055 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3058 (void)append_arg_number(curwin, buffer, !shortmess(SHM_FILE), IOSIZE);
3060 if (dont_truncate)
3062 /* Temporarily set msg_scroll to avoid the message being truncated.
3063 * First call msg_start() to get the message in the right place. */
3064 msg_start();
3065 n = msg_scroll;
3066 msg_scroll = TRUE;
3067 msg(buffer);
3068 msg_scroll = n;
3070 else
3072 p = msg_trunc_attr(buffer, FALSE, 0);
3073 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
3074 /* Need to repeat the message after redrawing when:
3075 * - When restart_edit is set (otherwise there will be a delay
3076 * before redrawing).
3077 * - When the screen was scrolled but there is no wait-return
3078 * prompt. */
3079 set_keep_msg(p, 0);
3082 vim_free(buffer);
3085 void
3086 col_print(buf, col, vcol)
3087 char_u *buf;
3088 int col;
3089 int vcol;
3091 if (col == vcol)
3092 sprintf((char *)buf, "%d", col);
3093 else
3094 sprintf((char *)buf, "%d-%d", col, vcol);
3097 #if defined(FEAT_TITLE) || defined(PROTO)
3099 * put file name in title bar of window and in icon title
3102 static char_u *lasttitle = NULL;
3103 static char_u *lasticon = NULL;
3105 void
3106 maketitle()
3108 char_u *p;
3109 char_u *t_str = NULL;
3110 char_u *i_name;
3111 char_u *i_str = NULL;
3112 int maxlen = 0;
3113 int len;
3114 int mustset;
3115 char_u buf[IOSIZE];
3116 int off;
3118 if (!redrawing())
3120 /* Postpone updating the title when 'lazyredraw' is set. */
3121 need_maketitle = TRUE;
3122 return;
3125 #ifdef FEAT_GUI_MACVIM
3126 gui_macvim_update_modified_flag();
3127 #endif
3129 need_maketitle = FALSE;
3130 if (!p_title && !p_icon)
3131 return;
3133 if (p_title)
3135 if (p_titlelen > 0)
3137 maxlen = p_titlelen * Columns / 100;
3138 if (maxlen < 10)
3139 maxlen = 10;
3142 t_str = buf;
3143 if (*p_titlestring != NUL)
3145 #ifdef FEAT_STL_OPT
3146 if (stl_syntax & STL_IN_TITLE)
3148 int use_sandbox = FALSE;
3149 int save_called_emsg = called_emsg;
3151 # ifdef FEAT_EVAL
3152 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
3153 # endif
3154 called_emsg = FALSE;
3155 build_stl_str_hl(curwin, t_str, sizeof(buf),
3156 p_titlestring, use_sandbox,
3157 0, maxlen, NULL, NULL);
3158 if (called_emsg)
3159 set_string_option_direct((char_u *)"titlestring", -1,
3160 (char_u *)"", OPT_FREE, SID_ERROR);
3161 called_emsg |= save_called_emsg;
3163 else
3164 #endif
3165 t_str = p_titlestring;
3167 else
3169 /* format: "fname + (path) (1 of 2) - VIM" */
3171 if (curbuf->b_fname == NULL)
3172 STRCPY(buf, _("[No Name]"));
3173 else
3175 p = transstr(gettail(curbuf->b_fname));
3176 vim_strncpy(buf, p, IOSIZE - 100);
3177 vim_free(p);
3180 switch (bufIsChanged(curbuf)
3181 + (curbuf->b_p_ro * 2)
3182 + (!curbuf->b_p_ma * 4))
3184 case 1: STRCAT(buf, " +"); break;
3185 case 2: STRCAT(buf, " ="); break;
3186 case 3: STRCAT(buf, " =+"); break;
3187 case 4:
3188 case 6: STRCAT(buf, " -"); break;
3189 case 5:
3190 case 7: STRCAT(buf, " -+"); break;
3193 if (curbuf->b_fname != NULL)
3195 /* Get path of file, replace home dir with ~ */
3196 off = (int)STRLEN(buf);
3197 buf[off++] = ' ';
3198 buf[off++] = '(';
3199 home_replace(curbuf, curbuf->b_ffname,
3200 buf + off, IOSIZE - off, TRUE);
3201 #ifdef BACKSLASH_IN_FILENAME
3202 /* avoid "c:/name" to be reduced to "c" */
3203 if (isalpha(buf[off]) && buf[off + 1] == ':')
3204 off += 2;
3205 #endif
3206 /* remove the file name */
3207 p = gettail_sep(buf + off);
3208 if (p == buf + off)
3209 /* must be a help buffer */
3210 vim_strncpy(buf + off, (char_u *)_("help"),
3211 IOSIZE - off - 1);
3212 else
3213 *p = NUL;
3215 /* translate unprintable chars */
3216 p = transstr(buf + off);
3217 vim_strncpy(buf + off, p, IOSIZE - off - 1);
3218 vim_free(p);
3219 STRCAT(buf, ")");
3222 #ifndef FEAT_GUI_MACVIM
3223 append_arg_number(curwin, buf, FALSE, IOSIZE);
3224 #endif
3226 #if defined(FEAT_CLIENTSERVER)
3227 if (serverName != NULL)
3229 STRCAT(buf, " - ");
3230 STRCAT(buf, serverName);
3232 else
3233 #endif
3234 STRCAT(buf, " - VIM");
3236 if (maxlen > 0)
3238 /* make it shorter by removing a bit in the middle */
3239 len = vim_strsize(buf);
3240 if (len > maxlen)
3241 trunc_string(buf, buf, maxlen);
3245 mustset = ti_change(t_str, &lasttitle);
3247 if (p_icon)
3249 i_str = buf;
3250 if (*p_iconstring != NUL)
3252 #ifdef FEAT_STL_OPT
3253 if (stl_syntax & STL_IN_ICON)
3255 int use_sandbox = FALSE;
3256 int save_called_emsg = called_emsg;
3258 # ifdef FEAT_EVAL
3259 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
3260 # endif
3261 called_emsg = FALSE;
3262 build_stl_str_hl(curwin, i_str, sizeof(buf),
3263 p_iconstring, use_sandbox,
3264 0, 0, NULL, NULL);
3265 if (called_emsg)
3266 set_string_option_direct((char_u *)"iconstring", -1,
3267 (char_u *)"", OPT_FREE, SID_ERROR);
3268 called_emsg |= save_called_emsg;
3270 else
3271 #endif
3272 i_str = p_iconstring;
3274 else
3276 if (buf_spname(curbuf) != NULL)
3277 i_name = (char_u *)buf_spname(curbuf);
3278 else /* use file name only in icon */
3279 i_name = gettail(curbuf->b_ffname);
3280 *i_str = NUL;
3281 /* Truncate name at 100 bytes. */
3282 len = (int)STRLEN(i_name);
3283 if (len > 100)
3285 len -= 100;
3286 #ifdef FEAT_MBYTE
3287 if (has_mbyte)
3288 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3289 #endif
3290 i_name += len;
3292 STRCPY(i_str, i_name);
3293 trans_characters(i_str, IOSIZE);
3297 mustset |= ti_change(i_str, &lasticon);
3299 if (mustset)
3300 resettitle();
3304 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3305 * from "str" if it does.
3306 * Return TRUE when "*last" changed.
3308 static int
3309 ti_change(str, last)
3310 char_u *str;
3311 char_u **last;
3313 if ((str == NULL) != (*last == NULL)
3314 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3316 vim_free(*last);
3317 if (str == NULL)
3318 *last = NULL;
3319 else
3320 *last = vim_strsave(str);
3321 return TRUE;
3323 return FALSE;
3327 * Put current window title back (used after calling a shell)
3329 void
3330 resettitle()
3332 mch_settitle(lasttitle, lasticon);
3335 # if defined(EXITFREE) || defined(PROTO)
3336 void
3337 free_titles()
3339 vim_free(lasttitle);
3340 vim_free(lasticon);
3342 # endif
3344 #endif /* FEAT_TITLE */
3346 #if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
3348 * Build a string from the status line items in "fmt".
3349 * Return length of string in screen cells.
3351 * Normally works for window "wp", except when working for 'tabline' then it
3352 * is "curwin".
3354 * Items are drawn interspersed with the text that surrounds it
3355 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3356 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3358 * If maxwidth is not zero, the string will be filled at any middle marker
3359 * or truncated if too long, fillchar is used for all whitespace.
3361 /*ARGSUSED*/
3363 build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar, maxwidth, hltab, tabtab)
3364 win_T *wp;
3365 char_u *out; /* buffer to write into != NameBuff */
3366 size_t outlen; /* length of out[] */
3367 char_u *fmt;
3368 int use_sandbox; /* "fmt" was set insecurely, use sandbox */
3369 int fillchar;
3370 int maxwidth;
3371 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3372 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
3374 char_u *p;
3375 char_u *s;
3376 char_u *t;
3377 char_u *linecont;
3378 #ifdef FEAT_EVAL
3379 win_T *o_curwin;
3380 buf_T *o_curbuf;
3381 #endif
3382 int empty_line;
3383 colnr_T virtcol;
3384 long l;
3385 long n;
3386 int prevchar_isflag;
3387 int prevchar_isitem;
3388 int itemisflag;
3389 int fillable;
3390 char_u *str;
3391 long num;
3392 int width;
3393 int itemcnt;
3394 int curitem;
3395 int groupitem[STL_MAX_ITEM];
3396 int groupdepth;
3397 struct stl_item
3399 char_u *start;
3400 int minwid;
3401 int maxwid;
3402 enum
3404 Normal,
3405 Empty,
3406 Group,
3407 Middle,
3408 Highlight,
3409 TabPage,
3410 Trunc
3411 } type;
3412 } item[STL_MAX_ITEM];
3413 int minwid;
3414 int maxwid;
3415 int zeropad;
3416 char_u base;
3417 char_u opt;
3418 #define TMPLEN 70
3419 char_u tmp[TMPLEN];
3420 char_u *usefmt = fmt;
3421 struct stl_hlrec *sp;
3423 #ifdef FEAT_EVAL
3425 * When the format starts with "%!" then evaluate it as an expression and
3426 * use the result as the actual format string.
3428 if (fmt[0] == '%' && fmt[1] == '!')
3430 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3431 if (usefmt == NULL)
3432 usefmt = fmt;
3434 #endif
3436 if (fillchar == 0)
3437 fillchar = ' ';
3438 #ifdef FEAT_MBYTE
3439 /* Can't handle a multi-byte fill character yet. */
3440 else if (mb_char2len(fillchar) > 1)
3441 fillchar = '-';
3442 #endif
3445 * Get line & check if empty (cursorpos will show "0-1").
3446 * If inversion is possible we use it. Else '=' characters are used.
3448 linecont = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3449 empty_line = (*linecont == NUL);
3451 groupdepth = 0;
3452 p = out;
3453 curitem = 0;
3454 prevchar_isflag = TRUE;
3455 prevchar_isitem = FALSE;
3456 for (s = usefmt; *s; )
3458 if (*s != NUL && *s != '%')
3459 prevchar_isflag = prevchar_isitem = FALSE;
3462 * Handle up to the next '%' or the end.
3464 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3465 *p++ = *s++;
3466 if (*s == NUL || p + 1 >= out + outlen)
3467 break;
3470 * Handle one '%' item.
3472 s++;
3473 if (*s == '%')
3475 if (p + 1 >= out + outlen)
3476 break;
3477 *p++ = *s++;
3478 prevchar_isflag = prevchar_isitem = FALSE;
3479 continue;
3481 if (*s == STL_MIDDLEMARK)
3483 s++;
3484 if (groupdepth > 0)
3485 continue;
3486 item[curitem].type = Middle;
3487 item[curitem++].start = p;
3488 continue;
3490 if (*s == STL_TRUNCMARK)
3492 s++;
3493 item[curitem].type = Trunc;
3494 item[curitem++].start = p;
3495 continue;
3497 if (*s == ')')
3499 s++;
3500 if (groupdepth < 1)
3501 continue;
3502 groupdepth--;
3504 t = item[groupitem[groupdepth]].start;
3505 *p = NUL;
3506 l = vim_strsize(t);
3507 if (curitem > groupitem[groupdepth] + 1
3508 && item[groupitem[groupdepth]].minwid == 0)
3510 /* remove group if all items are empty */
3511 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3512 if (item[n].type == Normal)
3513 break;
3514 if (n == curitem)
3516 p = t;
3517 l = 0;
3520 if (l > item[groupitem[groupdepth]].maxwid)
3522 /* truncate, remove n bytes of text at the start */
3523 #ifdef FEAT_MBYTE
3524 if (has_mbyte)
3526 /* Find the first character that should be included. */
3527 n = 0;
3528 while (l >= item[groupitem[groupdepth]].maxwid)
3530 l -= ptr2cells(t + n);
3531 n += (*mb_ptr2len)(t + n);
3534 else
3535 #endif
3536 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
3538 *t = '<';
3539 mch_memmove(t + 1, t + n, p - (t + n));
3540 p = p - n + 1;
3541 #ifdef FEAT_MBYTE
3542 /* Fill up space left over by half a double-wide char. */
3543 while (++l < item[groupitem[groupdepth]].minwid)
3544 *p++ = fillchar;
3545 #endif
3547 /* correct the start of the items for the truncation */
3548 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3550 item[l].start -= n;
3551 if (item[l].start < t)
3552 item[l].start = t;
3555 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3557 /* fill */
3558 n = item[groupitem[groupdepth]].minwid;
3559 if (n < 0)
3561 /* fill by appending characters */
3562 n = 0 - n;
3563 while (l++ < n && p + 1 < out + outlen)
3564 *p++ = fillchar;
3566 else
3568 /* fill by inserting characters */
3569 mch_memmove(t + n - l, t, p - t);
3570 l = n - l;
3571 if (p + l >= out + outlen)
3572 l = (long)((out + outlen) - p - 1);
3573 p += l;
3574 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3575 item[n].start += l;
3576 for ( ; l > 0; l--)
3577 *t++ = fillchar;
3580 continue;
3582 minwid = 0;
3583 maxwid = 9999;
3584 zeropad = FALSE;
3585 l = 1;
3586 if (*s == '0')
3588 s++;
3589 zeropad = TRUE;
3591 if (*s == '-')
3593 s++;
3594 l = -1;
3596 if (VIM_ISDIGIT(*s))
3598 minwid = (int)getdigits(&s);
3599 if (minwid < 0) /* overflow */
3600 minwid = 0;
3602 if (*s == STL_USER_HL)
3604 item[curitem].type = Highlight;
3605 item[curitem].start = p;
3606 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3607 s++;
3608 curitem++;
3609 continue;
3611 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3613 if (*s == STL_TABCLOSENR)
3615 if (minwid == 0)
3617 /* %X ends the close label, go back to the previously
3618 * define tab label nr. */
3619 for (n = curitem - 1; n >= 0; --n)
3620 if (item[n].type == TabPage && item[n].minwid >= 0)
3622 minwid = item[n].minwid;
3623 break;
3626 else
3627 /* close nrs are stored as negative values */
3628 minwid = - minwid;
3630 item[curitem].type = TabPage;
3631 item[curitem].start = p;
3632 item[curitem].minwid = minwid;
3633 s++;
3634 curitem++;
3635 continue;
3637 if (*s == '.')
3639 s++;
3640 if (VIM_ISDIGIT(*s))
3642 maxwid = (int)getdigits(&s);
3643 if (maxwid <= 0) /* overflow */
3644 maxwid = 50;
3647 minwid = (minwid > 50 ? 50 : minwid) * l;
3648 if (*s == '(')
3650 groupitem[groupdepth++] = curitem;
3651 item[curitem].type = Group;
3652 item[curitem].start = p;
3653 item[curitem].minwid = minwid;
3654 item[curitem].maxwid = maxwid;
3655 s++;
3656 curitem++;
3657 continue;
3659 if (vim_strchr(STL_ALL, *s) == NULL)
3661 s++;
3662 continue;
3664 opt = *s++;
3666 /* OK - now for the real work */
3667 base = 'D';
3668 itemisflag = FALSE;
3669 fillable = TRUE;
3670 num = -1;
3671 str = NULL;
3672 switch (opt)
3674 case STL_FILEPATH:
3675 case STL_FULLPATH:
3676 case STL_FILENAME:
3677 fillable = FALSE; /* don't change ' ' to fillchar */
3678 if (buf_spname(wp->w_buffer) != NULL)
3679 STRCPY(NameBuff, buf_spname(wp->w_buffer));
3680 else
3682 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
3683 : wp->w_buffer->b_fname;
3684 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3686 trans_characters(NameBuff, MAXPATHL);
3687 if (opt != STL_FILENAME)
3688 str = NameBuff;
3689 else
3690 str = gettail(NameBuff);
3691 break;
3693 case STL_VIM_EXPR: /* '{' */
3694 itemisflag = TRUE;
3695 t = p;
3696 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3697 *p++ = *s++;
3698 if (*s != '}') /* missing '}' or out of space */
3699 break;
3700 s++;
3701 *p = 0;
3702 p = t;
3704 #ifdef FEAT_EVAL
3705 sprintf((char *)tmp, "%d", curbuf->b_fnum);
3706 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3708 o_curbuf = curbuf;
3709 o_curwin = curwin;
3710 curwin = wp;
3711 curbuf = wp->w_buffer;
3713 str = eval_to_string_safe(p, &t, use_sandbox);
3715 curwin = o_curwin;
3716 curbuf = o_curbuf;
3717 do_unlet((char_u *)"g:actual_curbuf", TRUE);
3719 if (str != NULL && *str != 0)
3721 if (*skipdigits(str) == NUL)
3723 num = atoi((char *)str);
3724 vim_free(str);
3725 str = NULL;
3726 itemisflag = FALSE;
3729 #endif
3730 break;
3732 case STL_LINE:
3733 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3734 ? 0L : (long)(wp->w_cursor.lnum);
3735 break;
3737 case STL_NUMLINES:
3738 num = wp->w_buffer->b_ml.ml_line_count;
3739 break;
3741 case STL_COLUMN:
3742 num = !(State & INSERT) && empty_line
3743 ? 0 : (int)wp->w_cursor.col + 1;
3744 break;
3746 case STL_VIRTCOL:
3747 case STL_VIRTCOL_ALT:
3748 /* In list mode virtcol needs to be recomputed */
3749 virtcol = wp->w_virtcol;
3750 if (wp->w_p_list && lcs_tab1 == NUL)
3752 wp->w_p_list = FALSE;
3753 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3754 wp->w_p_list = TRUE;
3756 ++virtcol;
3757 /* Don't display %V if it's the same as %c. */
3758 if (opt == STL_VIRTCOL_ALT
3759 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3760 ? 0 : (int)wp->w_cursor.col + 1)))
3761 break;
3762 num = (long)virtcol;
3763 break;
3765 case STL_PERCENTAGE:
3766 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3767 (long)wp->w_buffer->b_ml.ml_line_count);
3768 break;
3770 case STL_ALTPERCENT:
3771 str = tmp;
3772 get_rel_pos(wp, str);
3773 break;
3775 case STL_ARGLISTSTAT:
3776 fillable = FALSE;
3777 tmp[0] = 0;
3778 if (append_arg_number(wp, tmp, FALSE, (int)sizeof(tmp)))
3779 str = tmp;
3780 break;
3782 case STL_KEYMAP:
3783 fillable = FALSE;
3784 if (get_keymap_str(wp, tmp, TMPLEN))
3785 str = tmp;
3786 break;
3787 case STL_PAGENUM:
3788 #if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
3789 num = printer_page_num;
3790 #else
3791 num = 0;
3792 #endif
3793 break;
3795 case STL_BUFNO:
3796 num = wp->w_buffer->b_fnum;
3797 break;
3799 case STL_OFFSET_X:
3800 base = 'X';
3801 case STL_OFFSET:
3802 #ifdef FEAT_BYTEOFF
3803 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
3804 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
3805 0L : l + 1 + (!(State & INSERT) && empty_line ?
3806 0 : (int)wp->w_cursor.col);
3807 #endif
3808 break;
3810 case STL_BYTEVAL_X:
3811 base = 'X';
3812 case STL_BYTEVAL:
3813 if (wp->w_cursor.col > STRLEN(linecont))
3814 num = 0;
3815 else
3817 #ifdef FEAT_MBYTE
3818 num = (*mb_ptr2char)(linecont + wp->w_cursor.col);
3819 #else
3820 num = linecont[wp->w_cursor.col];
3821 #endif
3823 if (num == NL)
3824 num = 0;
3825 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
3826 num = NL;
3827 break;
3829 case STL_ROFLAG:
3830 case STL_ROFLAG_ALT:
3831 itemisflag = TRUE;
3832 if (wp->w_buffer->b_p_ro)
3833 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : "[RO]");
3834 break;
3836 case STL_HELPFLAG:
3837 case STL_HELPFLAG_ALT:
3838 itemisflag = TRUE;
3839 if (wp->w_buffer->b_help)
3840 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
3841 : _("[Help]"));
3842 break;
3844 #ifdef FEAT_AUTOCMD
3845 case STL_FILETYPE:
3846 if (*wp->w_buffer->b_p_ft != NUL
3847 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
3849 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
3850 wp->w_buffer->b_p_ft);
3851 str = tmp;
3853 break;
3855 case STL_FILETYPE_ALT:
3856 itemisflag = TRUE;
3857 if (*wp->w_buffer->b_p_ft != NUL
3858 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
3860 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
3861 wp->w_buffer->b_p_ft);
3862 for (t = tmp; *t != 0; t++)
3863 *t = TOUPPER_LOC(*t);
3864 str = tmp;
3866 break;
3867 #endif
3869 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3870 case STL_PREVIEWFLAG:
3871 case STL_PREVIEWFLAG_ALT:
3872 itemisflag = TRUE;
3873 if (wp->w_p_pvw)
3874 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
3875 : _("[Preview]"));
3876 break;
3877 #endif
3879 case STL_MODIFIED:
3880 case STL_MODIFIED_ALT:
3881 itemisflag = TRUE;
3882 switch ((opt == STL_MODIFIED_ALT)
3883 + bufIsChanged(wp->w_buffer) * 2
3884 + (!wp->w_buffer->b_p_ma) * 4)
3886 case 2: str = (char_u *)"[+]"; break;
3887 case 3: str = (char_u *)",+"; break;
3888 case 4: str = (char_u *)"[-]"; break;
3889 case 5: str = (char_u *)",-"; break;
3890 case 6: str = (char_u *)"[+-]"; break;
3891 case 7: str = (char_u *)",+-"; break;
3893 break;
3895 case STL_HIGHLIGHT:
3896 t = s;
3897 while (*s != '#' && *s != NUL)
3898 ++s;
3899 if (*s == '#')
3901 item[curitem].type = Highlight;
3902 item[curitem].start = p;
3903 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
3904 curitem++;
3906 ++s;
3907 continue;
3910 item[curitem].start = p;
3911 item[curitem].type = Normal;
3912 if (str != NULL && *str)
3914 t = str;
3915 if (itemisflag)
3917 if ((t[0] && t[1])
3918 && ((!prevchar_isitem && *t == ',')
3919 || (prevchar_isflag && *t == ' ')))
3920 t++;
3921 prevchar_isflag = TRUE;
3923 l = vim_strsize(t);
3924 if (l > 0)
3925 prevchar_isitem = TRUE;
3926 if (l > maxwid)
3928 while (l >= maxwid)
3929 #ifdef FEAT_MBYTE
3930 if (has_mbyte)
3932 l -= ptr2cells(t);
3933 t += (*mb_ptr2len)(t);
3935 else
3936 #endif
3937 l -= byte2cells(*t++);
3938 if (p + 1 >= out + outlen)
3939 break;
3940 *p++ = '<';
3942 if (minwid > 0)
3944 for (; l < minwid && p + 1 < out + outlen; l++)
3946 /* Don't put a "-" in front of a digit. */
3947 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
3948 *p++ = ' ';
3949 else
3950 *p++ = fillchar;
3952 minwid = 0;
3954 else
3955 minwid *= -1;
3956 while (*t && p + 1 < out + outlen)
3958 *p++ = *t++;
3959 /* Change a space by fillchar, unless fillchar is '-' and a
3960 * digit follows. */
3961 if (fillable && p[-1] == ' '
3962 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
3963 p[-1] = fillchar;
3965 for (; l < minwid && p + 1 < out + outlen; l++)
3966 *p++ = fillchar;
3968 else if (num >= 0)
3970 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
3971 char_u nstr[20];
3973 if (p + 20 >= out + outlen)
3974 break; /* not sufficient space */
3975 prevchar_isitem = TRUE;
3976 t = nstr;
3977 if (opt == STL_VIRTCOL_ALT)
3979 *t++ = '-';
3980 minwid--;
3982 *t++ = '%';
3983 if (zeropad)
3984 *t++ = '0';
3985 *t++ = '*';
3986 *t++ = nbase == 16 ? base : (nbase == 8 ? 'o' : 'd');
3987 *t = 0;
3989 for (n = num, l = 1; n >= nbase; n /= nbase)
3990 l++;
3991 if (opt == STL_VIRTCOL_ALT)
3992 l++;
3993 if (l > maxwid)
3995 l += 2;
3996 n = l - maxwid;
3997 while (l-- > maxwid)
3998 num /= nbase;
3999 *t++ = '>';
4000 *t++ = '%';
4001 *t = t[-3];
4002 *++t = 0;
4003 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4004 0, num, n);
4006 else
4007 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4008 minwid, num);
4009 p += STRLEN(p);
4011 else
4012 item[curitem].type = Empty;
4014 if (opt == STL_VIM_EXPR)
4015 vim_free(str);
4017 if (num >= 0 || (!itemisflag && str && *str))
4018 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4019 curitem++;
4021 *p = NUL;
4022 itemcnt = curitem;
4024 #ifdef FEAT_EVAL
4025 if (usefmt != fmt)
4026 vim_free(usefmt);
4027 #endif
4029 width = vim_strsize(out);
4030 if (maxwidth > 0 && width > maxwidth)
4032 /* Result is too long, must truncate somewhere. */
4033 l = 0;
4034 if (itemcnt == 0)
4035 s = out;
4036 else
4038 for ( ; l < itemcnt; l++)
4039 if (item[l].type == Trunc)
4041 /* Truncate at %< item. */
4042 s = item[l].start;
4043 break;
4045 if (l == itemcnt)
4047 /* No %< item, truncate first item. */
4048 s = item[0].start;
4049 l = 0;
4053 if (width - vim_strsize(s) >= maxwidth)
4055 /* Truncation mark is beyond max length */
4056 #ifdef FEAT_MBYTE
4057 if (has_mbyte)
4059 s = out;
4060 width = 0;
4061 for (;;)
4063 width += ptr2cells(s);
4064 if (width >= maxwidth)
4065 break;
4066 s += (*mb_ptr2len)(s);
4068 /* Fill up for half a double-wide character. */
4069 while (++width < maxwidth)
4070 *s++ = fillchar;
4072 else
4073 #endif
4074 s = out + maxwidth - 1;
4075 for (l = 0; l < itemcnt; l++)
4076 if (item[l].start > s)
4077 break;
4078 itemcnt = l;
4079 *s++ = '>';
4080 *s = 0;
4082 else
4084 #ifdef FEAT_MBYTE
4085 if (has_mbyte)
4087 n = 0;
4088 while (width >= maxwidth)
4090 width -= ptr2cells(s + n);
4091 n += (*mb_ptr2len)(s + n);
4094 else
4095 #endif
4096 n = width - maxwidth + 1;
4097 p = s + n;
4098 STRMOVE(s + 1, p);
4099 *s = '<';
4101 /* Fill up for half a double-wide character. */
4102 while (++width < maxwidth)
4104 s = s + STRLEN(s);
4105 *s++ = fillchar;
4106 *s = NUL;
4109 --n; /* count the '<' */
4110 for (; l < itemcnt; l++)
4112 if (item[l].start - n >= s)
4113 item[l].start -= n;
4114 else
4115 item[l].start = s;
4118 width = maxwidth;
4120 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4122 /* Apply STL_MIDDLE if any */
4123 for (l = 0; l < itemcnt; l++)
4124 if (item[l].type == Middle)
4125 break;
4126 if (l < itemcnt)
4128 p = item[l].start + maxwidth - width;
4129 STRMOVE(p, item[l].start);
4130 for (s = item[l].start; s < p; s++)
4131 *s = fillchar;
4132 for (l++; l < itemcnt; l++)
4133 item[l].start += maxwidth - width;
4134 width = maxwidth;
4138 /* Store the info about highlighting. */
4139 if (hltab != NULL)
4141 sp = hltab;
4142 for (l = 0; l < itemcnt; l++)
4144 if (item[l].type == Highlight)
4146 sp->start = item[l].start;
4147 sp->userhl = item[l].minwid;
4148 sp++;
4151 sp->start = NULL;
4152 sp->userhl = 0;
4155 /* Store the info about tab pages labels. */
4156 if (tabtab != NULL)
4158 sp = tabtab;
4159 for (l = 0; l < itemcnt; l++)
4161 if (item[l].type == TabPage)
4163 sp->start = item[l].start;
4164 sp->userhl = item[l].minwid;
4165 sp++;
4168 sp->start = NULL;
4169 sp->userhl = 0;
4172 return width;
4174 #endif /* FEAT_STL_OPT */
4176 #if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4177 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
4179 * Get relative cursor position in window into "str[]", in the form 99%, using
4180 * "Top", "Bot" or "All" when appropriate.
4182 void
4183 get_rel_pos(wp, str)
4184 win_T *wp;
4185 char_u *str;
4187 long above; /* number of lines above window */
4188 long below; /* number of lines below window */
4190 above = wp->w_topline - 1;
4191 #ifdef FEAT_DIFF
4192 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
4193 #endif
4194 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4195 if (below <= 0)
4196 STRCPY(str, above == 0 ? _("All") : _("Bot"));
4197 else if (above <= 0)
4198 STRCPY(str, _("Top"));
4199 else
4200 sprintf((char *)str, "%2d%%", above > 1000000L
4201 ? (int)(above / ((above + below) / 100L))
4202 : (int)(above * 100L / (above + below)));
4204 #endif
4207 * Append (file 2 of 8) to 'buf', if editing more than one file.
4208 * Return TRUE if it was appended.
4211 append_arg_number(wp, buf, add_file, maxlen)
4212 win_T *wp;
4213 char_u *buf;
4214 int add_file; /* Add "file" before the arg number */
4215 int maxlen; /* maximum nr of chars in buf or zero*/
4217 char_u *p;
4219 if (ARGCOUNT <= 1) /* nothing to do */
4220 return FALSE;
4222 p = buf + STRLEN(buf); /* go to the end of the buffer */
4223 if (maxlen && p - buf + 35 >= maxlen) /* getting too long */
4224 return FALSE;
4225 *p++ = ' ';
4226 *p++ = '(';
4227 if (add_file)
4229 STRCPY(p, "file ");
4230 p += 5;
4232 sprintf((char *)p, wp->w_arg_idx_invalid ? "(%d) of %d)"
4233 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4234 return TRUE;
4238 * If fname is not a full path, make it a full path.
4239 * Returns pointer to allocated memory (NULL for failure).
4241 char_u *
4242 fix_fname(fname)
4243 char_u *fname;
4246 * Force expanding the path always for Unix, because symbolic links may
4247 * mess up the full path name, even though it starts with a '/'.
4248 * Also expand when there is ".." in the file name, try to remove it,
4249 * because "c:/src/../README" is equal to "c:/README".
4250 * Similarly "c:/src//file" is equal to "c:/src/file".
4251 * For MS-Windows also expand names like "longna~1" to "longname".
4253 #ifdef UNIX
4254 return FullName_save(fname, TRUE);
4255 #else
4256 if (!vim_isAbsName(fname)
4257 || strstr((char *)fname, "..") != NULL
4258 || strstr((char *)fname, "//") != NULL
4259 # ifdef BACKSLASH_IN_FILENAME
4260 || strstr((char *)fname, "\\\\") != NULL
4261 # endif
4262 # if defined(MSWIN) || defined(DJGPP)
4263 || vim_strchr(fname, '~') != NULL
4264 # endif
4266 return FullName_save(fname, FALSE);
4268 fname = vim_strsave(fname);
4270 # ifdef USE_FNAME_CASE
4271 # ifdef USE_LONG_FNAME
4272 if (USE_LONG_FNAME)
4273 # endif
4275 if (fname != NULL)
4276 fname_case(fname, 0); /* set correct case for file name */
4278 # endif
4280 return fname;
4281 #endif
4285 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4286 * "ffname" becomes a pointer to allocated memory (or NULL).
4288 /*ARGSUSED*/
4289 void
4290 fname_expand(buf, ffname, sfname)
4291 buf_T *buf;
4292 char_u **ffname;
4293 char_u **sfname;
4295 if (*ffname == NULL) /* if no file name given, nothing to do */
4296 return;
4297 if (*sfname == NULL) /* if no short file name given, use ffname */
4298 *sfname = *ffname;
4299 *ffname = fix_fname(*ffname); /* expand to full path */
4301 #ifdef FEAT_SHORTCUT
4302 if (!buf->b_p_bin)
4304 char_u *rfname;
4306 /* If the file name is a shortcut file, use the file it links to. */
4307 rfname = mch_resolve_shortcut(*ffname);
4308 if (rfname != NULL)
4310 vim_free(*ffname);
4311 *ffname = rfname;
4312 *sfname = rfname;
4315 #endif
4319 * Get the file name for an argument list entry.
4321 char_u *
4322 alist_name(aep)
4323 aentry_T *aep;
4325 buf_T *bp;
4327 /* Use the name from the associated buffer if it exists. */
4328 bp = buflist_findnr(aep->ae_fnum);
4329 if (bp == NULL || bp->b_fname == NULL)
4330 return aep->ae_fname;
4331 return bp->b_fname;
4334 #if defined(FEAT_WINDOWS) || defined(PROTO)
4336 * do_arg_all(): Open up to 'count' windows, one for each argument.
4338 void
4339 do_arg_all(count, forceit, keep_tabs)
4340 int count;
4341 int forceit; /* hide buffers in current windows */
4342 int keep_tabs; /* keep current tabs, for ":tab drop file" */
4344 int i;
4345 win_T *wp, *wpnext;
4346 char_u *opened; /* array of flags for which args are open */
4347 int opened_len; /* length of opened[] */
4348 int use_firstwin = FALSE; /* use first window for arglist */
4349 int split_ret = OK;
4350 int p_ea_save;
4351 alist_T *alist; /* argument list to be used */
4352 buf_T *buf;
4353 tabpage_T *tpnext;
4354 int had_tab = cmdmod.tab;
4355 win_T *new_curwin = NULL;
4356 tabpage_T *new_curtab = NULL;
4358 if (ARGCOUNT <= 0)
4360 /* Don't give an error message. We don't want it when the ":all"
4361 * command is in the .vimrc. */
4362 return;
4364 setpcmark();
4366 opened_len = ARGCOUNT;
4367 opened = alloc_clear((unsigned)opened_len);
4368 if (opened == NULL)
4369 return;
4371 #ifdef FEAT_GUI
4372 need_mouse_correct = TRUE;
4373 #endif
4376 * Try closing all windows that are not in the argument list.
4377 * Also close windows that are not full width;
4378 * When 'hidden' or "forceit" set the buffer becomes hidden.
4379 * Windows that have a changed buffer and can't be hidden won't be closed.
4380 * When the ":tab" modifier was used do this for all tab pages.
4382 if (had_tab > 0)
4383 goto_tabpage_tp(first_tabpage);
4384 for (;;)
4386 tpnext = curtab->tp_next;
4387 for (wp = firstwin; wp != NULL; wp = wpnext)
4389 wpnext = wp->w_next;
4390 buf = wp->w_buffer;
4391 if (buf->b_ffname == NULL
4392 || buf->b_nwindows > 1
4393 #ifdef FEAT_VERTSPLIT
4394 || wp->w_width != Columns
4395 #endif
4397 i = ARGCOUNT;
4398 else
4400 /* check if the buffer in this window is in the arglist */
4401 for (i = 0; i < ARGCOUNT; ++i)
4403 if (ARGLIST[i].ae_fnum == buf->b_fnum
4404 || fullpathcmp(alist_name(&ARGLIST[i]),
4405 buf->b_ffname, TRUE) & FPC_SAME)
4407 if (i < opened_len)
4409 opened[i] = TRUE;
4410 if (i == 0)
4412 new_curwin = wp;
4413 new_curtab = curtab;
4416 if (wp->w_alist != curwin->w_alist)
4418 /* Use the current argument list for all windows
4419 * containing a file from it. */
4420 alist_unlink(wp->w_alist);
4421 wp->w_alist = curwin->w_alist;
4422 ++wp->w_alist->al_refcount;
4424 break;
4428 wp->w_arg_idx = i;
4430 if (i == ARGCOUNT && !keep_tabs) /* close this window */
4432 if (P_HID(buf) || forceit || buf->b_nwindows > 1
4433 || !bufIsChanged(buf))
4435 /* If the buffer was changed, and we would like to hide it,
4436 * try autowriting. */
4437 if (!P_HID(buf) && buf->b_nwindows <= 1
4438 && bufIsChanged(buf))
4440 (void)autowrite(buf, FALSE);
4441 #ifdef FEAT_AUTOCMD
4442 /* check if autocommands removed the window */
4443 if (!win_valid(wp) || !buf_valid(buf))
4445 wpnext = firstwin; /* start all over... */
4446 continue;
4448 #endif
4450 #ifdef FEAT_WINDOWS
4451 /* don't close last window */
4452 if (firstwin == lastwin && first_tabpage->tp_next == NULL)
4453 #endif
4454 use_firstwin = TRUE;
4455 #ifdef FEAT_WINDOWS
4456 else
4458 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4459 # ifdef FEAT_AUTOCMD
4460 /* check if autocommands removed the next window */
4461 if (!win_valid(wpnext))
4462 wpnext = firstwin; /* start all over... */
4463 # endif
4465 #endif
4470 /* Without the ":tab" modifier only do the current tab page. */
4471 if (had_tab == 0 || tpnext == NULL)
4472 break;
4474 # ifdef FEAT_AUTOCMD
4475 /* check if autocommands removed the next tab page */
4476 if (!valid_tabpage(tpnext))
4477 tpnext = first_tabpage; /* start all over...*/
4478 # endif
4479 goto_tabpage_tp(tpnext);
4483 * Open a window for files in the argument list that don't have one.
4484 * ARGCOUNT may change while doing this, because of autocommands.
4486 if (count > ARGCOUNT || count <= 0)
4487 count = ARGCOUNT;
4489 /* Autocommands may do anything to the argument list. Make sure it's not
4490 * freed while we are working here by "locking" it. We still have to
4491 * watch out for its size to be changed. */
4492 alist = curwin->w_alist;
4493 ++alist->al_refcount;
4495 #ifdef FEAT_AUTOCMD
4496 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4497 ++autocmd_no_enter;
4498 ++autocmd_no_leave;
4499 #endif
4500 win_enter(lastwin, FALSE);
4501 #ifdef FEAT_WINDOWS
4502 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4503 * leaving an empty tab page when executed locally. */
4504 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4505 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4506 use_firstwin = TRUE;
4507 #endif
4509 for (i = 0; i < count && i < alist->al_ga.ga_len && !got_int; ++i)
4511 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4512 arg_had_last = TRUE;
4513 if (i < opened_len && opened[i])
4515 /* Move the already present window to below the current window */
4516 if (curwin->w_arg_idx != i)
4518 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4520 if (wpnext->w_arg_idx == i)
4522 win_move_after(wpnext, curwin);
4523 break;
4528 else if (split_ret == OK)
4530 if (!use_firstwin) /* split current window */
4532 p_ea_save = p_ea;
4533 p_ea = TRUE; /* use space from all windows */
4534 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4535 p_ea = p_ea_save;
4536 if (split_ret == FAIL)
4537 continue;
4539 #ifdef FEAT_AUTOCMD
4540 else /* first window: do autocmd for leaving this buffer */
4541 --autocmd_no_leave;
4542 #endif
4545 * edit file "i"
4547 curwin->w_arg_idx = i;
4548 if (i == 0)
4550 new_curwin = curwin;
4551 new_curtab = curtab;
4553 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4554 ECMD_ONE,
4555 ((P_HID(curwin->w_buffer)
4556 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
4557 + ECMD_OLDBUF, curwin);
4558 #ifdef FEAT_AUTOCMD
4559 if (use_firstwin)
4560 ++autocmd_no_leave;
4561 #endif
4562 use_firstwin = FALSE;
4564 ui_breakcheck();
4566 /* When ":tab" was used open a new tab for a new window repeatedly. */
4567 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4568 cmdmod.tab = 9999;
4571 /* Remove the "lock" on the argument list. */
4572 alist_unlink(alist);
4574 #ifdef FEAT_AUTOCMD
4575 --autocmd_no_enter;
4576 #endif
4577 /* to window with first arg */
4578 if (valid_tabpage(new_curtab))
4579 goto_tabpage_tp(new_curtab);
4580 if (win_valid(new_curwin))
4581 win_enter(new_curwin, FALSE);
4583 #ifdef FEAT_AUTOCMD
4584 --autocmd_no_leave;
4585 #endif
4586 vim_free(opened);
4589 # if defined(FEAT_LISTCMDS) || defined(PROTO)
4591 * Open a window for a number of buffers.
4593 void
4594 ex_buffer_all(eap)
4595 exarg_T *eap;
4597 buf_T *buf;
4598 win_T *wp, *wpnext;
4599 int split_ret = OK;
4600 int p_ea_save;
4601 int open_wins = 0;
4602 int r;
4603 int count; /* Maximum number of windows to open. */
4604 int all; /* When TRUE also load inactive buffers. */
4605 #ifdef FEAT_WINDOWS
4606 int had_tab = cmdmod.tab;
4607 tabpage_T *tpnext;
4608 #endif
4610 if (eap->addr_count == 0) /* make as many windows as possible */
4611 count = 9999;
4612 else
4613 count = eap->line2; /* make as many windows as specified */
4614 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4615 all = FALSE;
4616 else
4617 all = TRUE;
4619 setpcmark();
4621 #ifdef FEAT_GUI
4622 need_mouse_correct = TRUE;
4623 #endif
4626 * Close superfluous windows (two windows for the same buffer).
4627 * Also close windows that are not full-width.
4629 #ifdef FEAT_WINDOWS
4630 if (had_tab > 0)
4631 goto_tabpage_tp(first_tabpage);
4632 for (;;)
4634 #endif
4635 tpnext = curtab->tp_next;
4636 for (wp = firstwin; wp != NULL; wp = wpnext)
4638 wpnext = wp->w_next;
4639 if ((wp->w_buffer->b_nwindows > 1
4640 #ifdef FEAT_VERTSPLIT
4641 || ((cmdmod.split & WSP_VERT)
4642 ? wp->w_height + wp->w_status_height < Rows - p_ch
4643 - tabline_height()
4644 : wp->w_width != Columns)
4645 #endif
4646 #ifdef FEAT_WINDOWS
4647 || (had_tab > 0 && wp != firstwin)
4648 #endif
4649 ) && firstwin != lastwin)
4651 win_close(wp, FALSE);
4652 #ifdef FEAT_AUTOCMD
4653 wpnext = firstwin; /* just in case an autocommand does
4654 something strange with windows */
4655 tpnext = first_tabpage; /* start all over...*/
4656 open_wins = 0;
4657 #endif
4659 else
4660 ++open_wins;
4663 #ifdef FEAT_WINDOWS
4664 /* Without the ":tab" modifier only do the current tab page. */
4665 if (had_tab == 0 || tpnext == NULL)
4666 break;
4667 goto_tabpage_tp(tpnext);
4669 #endif
4672 * Go through the buffer list. When a buffer doesn't have a window yet,
4673 * open one. Otherwise move the window to the right position.
4674 * Watch out for autocommands that delete buffers or windows!
4676 #ifdef FEAT_AUTOCMD
4677 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4678 ++autocmd_no_enter;
4679 #endif
4680 win_enter(lastwin, FALSE);
4681 #ifdef FEAT_AUTOCMD
4682 ++autocmd_no_leave;
4683 #endif
4684 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4686 /* Check if this buffer needs a window */
4687 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4688 continue;
4690 #ifdef FEAT_WINDOWS
4691 if (had_tab != 0)
4693 /* With the ":tab" modifier don't move the window. */
4694 if (buf->b_nwindows > 0)
4695 wp = lastwin; /* buffer has a window, skip it */
4696 else
4697 wp = NULL;
4699 else
4700 #endif
4702 /* Check if this buffer already has a window */
4703 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4704 if (wp->w_buffer == buf)
4705 break;
4706 /* If the buffer already has a window, move it */
4707 if (wp != NULL)
4708 win_move_after(wp, curwin);
4711 if (wp == NULL && split_ret == OK)
4713 /* Split the window and put the buffer in it */
4714 p_ea_save = p_ea;
4715 p_ea = TRUE; /* use space from all windows */
4716 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4717 ++open_wins;
4718 p_ea = p_ea_save;
4719 if (split_ret == FAIL)
4720 continue;
4722 /* Open the buffer in this window. */
4723 #if defined(HAS_SWAP_EXISTS_ACTION)
4724 swap_exists_action = SEA_DIALOG;
4725 #endif
4726 set_curbuf(buf, DOBUF_GOTO);
4727 #ifdef FEAT_AUTOCMD
4728 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
4730 #if defined(HAS_SWAP_EXISTS_ACTION)
4731 swap_exists_action = SEA_NONE;
4732 # endif
4733 break;
4735 #endif
4736 #if defined(HAS_SWAP_EXISTS_ACTION)
4737 if (swap_exists_action == SEA_QUIT)
4739 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4740 cleanup_T cs;
4742 /* Reset the error/interrupt/exception state here so that
4743 * aborting() returns FALSE when closing a window. */
4744 enter_cleanup(&cs);
4745 # endif
4747 /* User selected Quit at ATTENTION prompt; close this window. */
4748 win_close(curwin, TRUE);
4749 --open_wins;
4750 swap_exists_action = SEA_NONE;
4751 swap_exists_did_quit = TRUE;
4753 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4754 /* Restore the error/interrupt/exception state if not
4755 * discarded by a new aborting error, interrupt, or uncaught
4756 * exception. */
4757 leave_cleanup(&cs);
4758 # endif
4760 else
4761 handle_swap_exists(NULL);
4762 #endif
4765 ui_breakcheck();
4766 if (got_int)
4768 (void)vgetc(); /* only break the file loading, not the rest */
4769 break;
4771 #ifdef FEAT_EVAL
4772 /* Autocommands deleted the buffer or aborted script processing!!! */
4773 if (aborting())
4774 break;
4775 #endif
4776 #ifdef FEAT_WINDOWS
4777 /* When ":tab" was used open a new tab for a new window repeatedly. */
4778 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4779 cmdmod.tab = 9999;
4780 #endif
4782 #ifdef FEAT_AUTOCMD
4783 --autocmd_no_enter;
4784 #endif
4785 win_enter(firstwin, FALSE); /* back to first window */
4786 #ifdef FEAT_AUTOCMD
4787 --autocmd_no_leave;
4788 #endif
4791 * Close superfluous windows.
4793 for (wp = lastwin; open_wins > count; )
4795 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
4796 || autowrite(wp->w_buffer, FALSE) == OK);
4797 #ifdef FEAT_AUTOCMD
4798 if (!win_valid(wp))
4800 /* BufWrite Autocommands made the window invalid, start over */
4801 wp = lastwin;
4803 else
4804 #endif
4805 if (r)
4807 win_close(wp, !P_HID(wp->w_buffer));
4808 --open_wins;
4809 wp = lastwin;
4811 else
4813 wp = wp->w_prev;
4814 if (wp == NULL)
4815 break;
4819 # endif /* FEAT_LISTCMDS */
4821 #endif /* FEAT_WINDOWS */
4823 static int chk_modeline __ARGS((linenr_T, int));
4826 * do_modelines() - process mode lines for the current file
4828 * "flags" can be:
4829 * OPT_WINONLY only set options local to window
4830 * OPT_NOWIN don't set options local to window
4832 * Returns immediately if the "ml" option isn't set.
4834 void
4835 do_modelines(flags)
4836 int flags;
4838 linenr_T lnum;
4839 int nmlines;
4840 static int entered = 0;
4842 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
4843 return;
4845 /* Disallow recursive entry here. Can happen when executing a modeline
4846 * triggers an autocommand, which reloads modelines with a ":do". */
4847 if (entered)
4848 return;
4850 ++entered;
4851 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
4852 ++lnum)
4853 if (chk_modeline(lnum, flags) == FAIL)
4854 nmlines = 0;
4856 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
4857 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
4858 if (chk_modeline(lnum, flags) == FAIL)
4859 nmlines = 0;
4860 --entered;
4863 #include "version.h" /* for version number */
4866 * chk_modeline() - check a single line for a mode string
4867 * Return FAIL if an error encountered.
4869 static int
4870 chk_modeline(lnum, flags)
4871 linenr_T lnum;
4872 int flags; /* Same as for do_modelines(). */
4874 char_u *s;
4875 char_u *e;
4876 char_u *linecopy; /* local copy of any modeline found */
4877 int prev;
4878 int vers;
4879 int end;
4880 int retval = OK;
4881 char_u *save_sourcing_name;
4882 linenr_T save_sourcing_lnum;
4883 #ifdef FEAT_EVAL
4884 scid_T save_SID;
4885 #endif
4887 prev = -1;
4888 for (s = ml_get(lnum); *s != NUL; ++s)
4890 if (prev == -1 || vim_isspace(prev))
4892 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
4893 || STRNCMP(s, "vi:", (size_t)3) == 0)
4894 break;
4895 if (STRNCMP(s, "vim", 3) == 0)
4897 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
4898 e = s + 4;
4899 else
4900 e = s + 3;
4901 vers = getdigits(&e);
4902 if (*e == ':'
4903 && (s[3] == ':'
4904 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
4905 || (VIM_VERSION_100 < vers && s[3] == '<')
4906 || (VIM_VERSION_100 > vers && s[3] == '>')
4907 || (VIM_VERSION_100 == vers && s[3] == '=')))
4908 break;
4911 prev = *s;
4914 if (*s)
4916 do /* skip over "ex:", "vi:" or "vim:" */
4917 ++s;
4918 while (s[-1] != ':');
4920 s = linecopy = vim_strsave(s); /* copy the line, it will change */
4921 if (linecopy == NULL)
4922 return FAIL;
4924 save_sourcing_lnum = sourcing_lnum;
4925 save_sourcing_name = sourcing_name;
4926 sourcing_lnum = lnum; /* prepare for emsg() */
4927 sourcing_name = (char_u *)"modelines";
4929 end = FALSE;
4930 while (end == FALSE)
4932 s = skipwhite(s);
4933 if (*s == NUL)
4934 break;
4937 * Find end of set command: ':' or end of line.
4938 * Skip over "\:", replacing it with ":".
4940 for (e = s; *e != ':' && *e != NUL; ++e)
4941 if (e[0] == '\\' && e[1] == ':')
4942 STRMOVE(e, e + 1);
4943 if (*e == NUL)
4944 end = TRUE;
4947 * If there is a "set" command, require a terminating ':' and
4948 * ignore the stuff after the ':'.
4949 * "vi:set opt opt opt: foo" -- foo not interpreted
4950 * "vi:opt opt opt: foo" -- foo interpreted
4951 * Accept "se" for compatibility with Elvis.
4953 if (STRNCMP(s, "set ", (size_t)4) == 0
4954 || STRNCMP(s, "se ", (size_t)3) == 0)
4956 if (*e != ':') /* no terminating ':'? */
4957 break;
4958 end = TRUE;
4959 s = vim_strchr(s, ' ') + 1;
4961 *e = NUL; /* truncate the set command */
4963 if (*s != NUL) /* skip over an empty "::" */
4965 #ifdef FEAT_EVAL
4966 save_SID = current_SID;
4967 current_SID = SID_MODELINE;
4968 #endif
4969 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
4970 #ifdef FEAT_EVAL
4971 current_SID = save_SID;
4972 #endif
4973 if (retval == FAIL) /* stop if error found */
4974 break;
4976 s = e + 1; /* advance to next part */
4979 sourcing_lnum = save_sourcing_lnum;
4980 sourcing_name = save_sourcing_name;
4982 vim_free(linecopy);
4984 return retval;
4987 #if defined(FEAT_VIMINFO) || defined(PROTO)
4989 read_viminfo_bufferlist(virp, writing)
4990 vir_T *virp;
4991 int writing;
4993 char_u *tab;
4994 linenr_T lnum;
4995 colnr_T col;
4996 buf_T *buf;
4997 char_u *sfname;
4998 char_u *xline;
5000 /* Handle long line and escaped characters. */
5001 xline = viminfo_readstring(virp, 1, FALSE);
5003 /* don't read in if there are files on the command-line or if writing: */
5004 if (xline != NULL && !writing && ARGCOUNT == 0
5005 && find_viminfo_parameter('%') != NULL)
5007 /* Format is: <fname> Tab <lnum> Tab <col>.
5008 * Watch out for a Tab in the file name, work from the end. */
5009 lnum = 0;
5010 col = 0;
5011 tab = vim_strrchr(xline, '\t');
5012 if (tab != NULL)
5014 *tab++ = '\0';
5015 col = atoi((char *)tab);
5016 tab = vim_strrchr(xline, '\t');
5017 if (tab != NULL)
5019 *tab++ = '\0';
5020 lnum = atol((char *)tab);
5024 /* Expand "~/" in the file name at "line + 1" to a full path.
5025 * Then try shortening it by comparing with the current directory */
5026 expand_env(xline, NameBuff, MAXPATHL);
5027 sfname = shorten_fname1(NameBuff);
5029 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5030 if (buf != NULL) /* just in case... */
5032 buf->b_last_cursor.lnum = lnum;
5033 buf->b_last_cursor.col = col;
5034 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5037 vim_free(xline);
5039 return viminfo_readline(virp);
5042 void
5043 write_viminfo_bufferlist(fp)
5044 FILE *fp;
5046 buf_T *buf;
5047 #ifdef FEAT_WINDOWS
5048 win_T *win;
5049 tabpage_T *tp;
5050 #endif
5051 char_u *line;
5052 int max_buffers;
5054 if (find_viminfo_parameter('%') == NULL)
5055 return;
5057 /* Without a number -1 is returned: do all buffers. */
5058 max_buffers = get_viminfo_parameter('%');
5060 /* Allocate room for the file name, lnum and col. */
5061 line = alloc(MAXPATHL + 40);
5062 if (line == NULL)
5063 return;
5065 #ifdef FEAT_WINDOWS
5066 FOR_ALL_TAB_WINDOWS(tp, win)
5067 set_last_cursor(win);
5068 #else
5069 set_last_cursor(curwin);
5070 #endif
5072 fprintf(fp, _("\n# Buffer list:\n"));
5073 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5075 if (buf->b_fname == NULL
5076 || !buf->b_p_bl
5077 #ifdef FEAT_QUICKFIX
5078 || bt_quickfix(buf)
5079 #endif
5080 || removable(buf->b_ffname))
5081 continue;
5083 if (max_buffers-- == 0)
5084 break;
5085 putc('%', fp);
5086 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
5087 sprintf((char *)line + STRLEN(line), "\t%ld\t%d",
5088 (long)buf->b_last_cursor.lnum,
5089 buf->b_last_cursor.col);
5090 viminfo_writestring(fp, line);
5092 vim_free(line);
5094 #endif
5098 * Return special buffer name.
5099 * Returns NULL when the buffer has a normal file name.
5101 char *
5102 buf_spname(buf)
5103 buf_T *buf;
5105 #if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5106 if (bt_quickfix(buf))
5108 win_T *win = NULL;
5109 tabpage_T *tp;
5112 * For location list window, w_llist_ref points to the location list.
5113 * For quickfix window, w_llist_ref is NULL.
5115 FOR_ALL_TAB_WINDOWS(tp, win)
5116 if (win->w_buffer == buf)
5117 break;
5118 if (win != NULL && win->w_llist_ref != NULL)
5119 return _("[Location List]");
5120 else
5121 return _("[Quickfix List]");
5123 #endif
5124 #ifdef FEAT_QUICKFIX
5125 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5126 * contains the name as specified by the user */
5127 if (bt_nofile(buf))
5129 if (buf->b_sfname != NULL)
5130 return (char *)buf->b_sfname;
5131 return _("[Scratch]");
5133 #endif
5134 if (buf->b_fname == NULL)
5135 return _("[No Name]");
5136 return NULL;
5140 #if defined(FEAT_SIGNS) || defined(PROTO)
5142 * Insert the sign into the signlist.
5144 static void
5145 insert_sign(buf, prev, next, id, lnum, typenr)
5146 buf_T *buf; /* buffer to store sign in */
5147 signlist_T *prev; /* previous sign entry */
5148 signlist_T *next; /* next sign entry */
5149 int id; /* sign ID */
5150 linenr_T lnum; /* line number which gets the mark */
5151 int typenr; /* typenr of sign we are adding */
5153 signlist_T *newsign;
5155 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5156 if (newsign != NULL)
5158 newsign->id = id;
5159 newsign->lnum = lnum;
5160 newsign->typenr = typenr;
5161 newsign->next = next;
5162 #ifdef FEAT_NETBEANS_INTG
5163 newsign->prev = prev;
5164 if (next != NULL)
5165 next->prev = newsign;
5166 #endif
5168 if (prev == NULL)
5170 /* When adding first sign need to redraw the windows to create the
5171 * column for signs. */
5172 if (buf->b_signlist == NULL)
5174 redraw_buf_later(buf, NOT_VALID);
5175 changed_cline_bef_curs();
5178 /* first sign in signlist */
5179 buf->b_signlist = newsign;
5181 else
5182 prev->next = newsign;
5187 * Add the sign into the signlist. Find the right spot to do it though.
5189 void
5190 buf_addsign(buf, id, lnum, typenr)
5191 buf_T *buf; /* buffer to store sign in */
5192 int id; /* sign ID */
5193 linenr_T lnum; /* line number which gets the mark */
5194 int typenr; /* typenr of sign we are adding */
5196 signlist_T *sign; /* a sign in the signlist */
5197 signlist_T *prev; /* the previous sign */
5199 prev = NULL;
5200 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5202 if (lnum == sign->lnum && id == sign->id)
5204 sign->typenr = typenr;
5205 return;
5207 else if (
5208 #ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5209 id < 0 &&
5210 #endif
5211 lnum < sign->lnum)
5213 #ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5214 /* XXX - GRP: Is this because of sign slide problem? Or is it
5215 * really needed? Or is it because we allow multiple signs per
5216 * line? If so, should I add that feature to FEAT_SIGNS?
5218 while (prev != NULL && prev->lnum == lnum)
5219 prev = prev->prev;
5220 if (prev == NULL)
5221 sign = buf->b_signlist;
5222 else
5223 sign = prev->next;
5224 #endif
5225 insert_sign(buf, prev, sign, id, lnum, typenr);
5226 return;
5228 prev = sign;
5230 #ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5231 /* XXX - GRP: See previous comment */
5232 while (prev != NULL && prev->lnum == lnum)
5233 prev = prev->prev;
5234 if (prev == NULL)
5235 sign = buf->b_signlist;
5236 else
5237 sign = prev->next;
5238 #endif
5239 insert_sign(buf, prev, sign, id, lnum, typenr);
5241 return;
5245 buf_change_sign_type(buf, markId, typenr)
5246 buf_T *buf; /* buffer to store sign in */
5247 int markId; /* sign ID */
5248 int typenr; /* typenr of sign we are adding */
5250 signlist_T *sign; /* a sign in the signlist */
5252 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5254 if (sign->id == markId)
5256 sign->typenr = typenr;
5257 return sign->lnum;
5261 return 0;
5264 int_u
5265 buf_getsigntype(buf, lnum, type)
5266 buf_T *buf;
5267 linenr_T lnum;
5268 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5270 signlist_T *sign; /* a sign in a b_signlist */
5272 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5273 if (sign->lnum == lnum
5274 && (type == SIGN_ANY
5275 # ifdef FEAT_SIGN_ICONS
5276 || (type == SIGN_ICON
5277 && sign_get_image(sign->typenr) != NULL)
5278 # endif
5279 || (type == SIGN_TEXT
5280 && sign_get_text(sign->typenr) != NULL)
5281 || (type == SIGN_LINEHL
5282 && sign_get_attr(sign->typenr, TRUE) != 0)))
5283 return sign->typenr;
5284 return 0;
5288 linenr_T
5289 buf_delsign(buf, id)
5290 buf_T *buf; /* buffer sign is stored in */
5291 int id; /* sign id */
5293 signlist_T **lastp; /* pointer to pointer to current sign */
5294 signlist_T *sign; /* a sign in a b_signlist */
5295 signlist_T *next; /* the next sign in a b_signlist */
5296 linenr_T lnum; /* line number whose sign was deleted */
5298 lastp = &buf->b_signlist;
5299 lnum = 0;
5300 for (sign = buf->b_signlist; sign != NULL; sign = next)
5302 next = sign->next;
5303 if (sign->id == id)
5305 *lastp = next;
5306 #ifdef FEAT_NETBEANS_INTG
5307 if (next != NULL)
5308 next->prev = sign->prev;
5309 #endif
5310 lnum = sign->lnum;
5311 vim_free(sign);
5312 break;
5314 else
5315 lastp = &sign->next;
5318 /* When deleted the last sign need to redraw the windows to remove the
5319 * sign column. */
5320 if (buf->b_signlist == NULL)
5322 redraw_buf_later(buf, NOT_VALID);
5323 changed_cline_bef_curs();
5326 return lnum;
5331 * Find the line number of the sign with the requested id. If the sign does
5332 * not exist, return 0 as the line number. This will still let the correct file
5333 * get loaded.
5336 buf_findsign(buf, id)
5337 buf_T *buf; /* buffer to store sign in */
5338 int id; /* sign ID */
5340 signlist_T *sign; /* a sign in the signlist */
5342 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5343 if (sign->id == id)
5344 return sign->lnum;
5346 return 0;
5350 buf_findsign_id(buf, lnum)
5351 buf_T *buf; /* buffer whose sign we are searching for */
5352 linenr_T lnum; /* line number of sign */
5354 signlist_T *sign; /* a sign in the signlist */
5356 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5357 if (sign->lnum == lnum)
5358 return sign->id;
5360 return 0;
5364 # if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5365 /* see if a given type of sign exists on a specific line */
5367 buf_findsigntype_id(buf, lnum, typenr)
5368 buf_T *buf; /* buffer whose sign we are searching for */
5369 linenr_T lnum; /* line number of sign */
5370 int typenr; /* sign type number */
5372 signlist_T *sign; /* a sign in the signlist */
5374 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5375 if (sign->lnum == lnum && sign->typenr == typenr)
5376 return sign->id;
5378 return 0;
5382 # if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5383 /* return the number of icons on the given line */
5385 buf_signcount(buf, lnum)
5386 buf_T *buf;
5387 linenr_T lnum;
5389 signlist_T *sign; /* a sign in the signlist */
5390 int count = 0;
5392 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5393 if (sign->lnum == lnum)
5394 if (sign_get_image(sign->typenr) != NULL)
5395 count++;
5397 return count;
5399 # endif /* FEAT_SIGN_ICONS */
5400 # endif /* FEAT_NETBEANS_INTG */
5404 * Delete signs in buffer "buf".
5406 static void
5407 buf_delete_signs(buf)
5408 buf_T *buf;
5410 signlist_T *next;
5412 while (buf->b_signlist != NULL)
5414 next = buf->b_signlist->next;
5415 vim_free(buf->b_signlist);
5416 buf->b_signlist = next;
5421 * Delete all signs in all buffers.
5423 void
5424 buf_delete_all_signs()
5426 buf_T *buf; /* buffer we are checking for signs */
5428 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5429 if (buf->b_signlist != NULL)
5431 /* Need to redraw the windows to remove the sign column. */
5432 redraw_buf_later(buf, NOT_VALID);
5433 buf_delete_signs(buf);
5438 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5440 void
5441 sign_list_placed(rbuf)
5442 buf_T *rbuf;
5444 buf_T *buf;
5445 signlist_T *p;
5446 char lbuf[BUFSIZ];
5448 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5449 msg_putchar('\n');
5450 if (rbuf == NULL)
5451 buf = firstbuf;
5452 else
5453 buf = rbuf;
5454 while (buf != NULL)
5456 if (buf->b_signlist != NULL)
5458 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
5459 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5460 msg_putchar('\n');
5462 for (p = buf->b_signlist; p != NULL; p = p->next)
5464 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
5465 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5466 MSG_PUTS(lbuf);
5467 msg_putchar('\n');
5469 if (rbuf != NULL)
5470 break;
5471 buf = buf->b_next;
5476 * Adjust a placed sign for inserted/deleted lines.
5478 void
5479 sign_mark_adjust(line1, line2, amount, amount_after)
5480 linenr_T line1;
5481 linenr_T line2;
5482 long amount;
5483 long amount_after;
5485 signlist_T *sign; /* a sign in a b_signlist */
5487 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5489 if (sign->lnum >= line1 && sign->lnum <= line2)
5491 if (amount == MAXLNUM)
5492 sign->lnum = line1;
5493 else
5494 sign->lnum += amount;
5496 else if (sign->lnum > line2)
5497 sign->lnum += amount_after;
5500 #endif /* FEAT_SIGNS */
5503 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5505 void
5506 set_buflisted(on)
5507 int on;
5509 if (on != curbuf->b_p_bl)
5511 curbuf->b_p_bl = on;
5512 #ifdef FEAT_AUTOCMD
5513 if (on)
5514 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5515 else
5516 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5517 #endif
5522 * Read the file for "buf" again and check if the contents changed.
5523 * Return TRUE if it changed or this could not be checked.
5526 buf_contents_changed(buf)
5527 buf_T *buf;
5529 buf_T *newbuf;
5530 int differ = TRUE;
5531 linenr_T lnum;
5532 aco_save_T aco;
5533 exarg_T ea;
5535 /* Allocate a buffer without putting it in the buffer list. */
5536 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5537 if (newbuf == NULL)
5538 return TRUE;
5540 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5541 if (prep_exarg(&ea, buf) == FAIL)
5543 wipe_buffer(newbuf, FALSE);
5544 return TRUE;
5547 /* set curwin/curbuf to buf and save a few things */
5548 aucmd_prepbuf(&aco, newbuf);
5550 if (ml_open(curbuf) == OK
5551 && readfile(buf->b_ffname, buf->b_fname,
5552 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5553 &ea, READ_NEW | READ_DUMMY) == OK)
5555 /* compare the two files line by line */
5556 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5558 differ = FALSE;
5559 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5560 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5562 differ = TRUE;
5563 break;
5567 vim_free(ea.cmd);
5569 /* restore curwin/curbuf and a few other things */
5570 aucmd_restbuf(&aco);
5572 if (curbuf != newbuf) /* safety check */
5573 wipe_buffer(newbuf, FALSE);
5575 return differ;
5579 * Wipe out a buffer and decrement the last buffer number if it was used for
5580 * this buffer. Call this to wipe out a temp buffer that does not contain any
5581 * marks.
5583 /*ARGSUSED*/
5584 void
5585 wipe_buffer(buf, aucmd)
5586 buf_T *buf;
5587 int aucmd; /* When TRUE trigger autocommands. */
5589 if (buf->b_fnum == top_file_num - 1)
5590 --top_file_num;
5592 #ifdef FEAT_AUTOCMD
5593 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
5594 block_autocmds();
5595 #endif
5596 close_buffer(NULL, buf, DOBUF_WIPE);
5597 #ifdef FEAT_AUTOCMD
5598 if (!aucmd)
5599 unblock_autocmds();
5600 #endif