Cleanup
[MacVim.git] / src / buffer.c
blob488c3136af96d10e62c5821563437f5c0a0e80c2
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_ODB_EDITOR
441 odb_buffer_close(buf);
442 #endif
444 /* Change directories when the 'acd' option is set. */
445 DO_AUTOCHDIR
448 * Remove the buffer from the list.
450 if (wipe_buf)
452 #ifdef FEAT_SUN_WORKSHOP
453 if (usingSunWorkShop)
454 workshop_file_closed_lineno((char *)buf->b_ffname,
455 (int)buf->b_last_cursor.lnum);
456 #endif
457 vim_free(buf->b_ffname);
458 vim_free(buf->b_sfname);
459 if (buf->b_prev == NULL)
460 firstbuf = buf->b_next;
461 else
462 buf->b_prev->b_next = buf->b_next;
463 if (buf->b_next == NULL)
464 lastbuf = buf->b_prev;
465 else
466 buf->b_next->b_prev = buf->b_prev;
467 free_buffer(buf);
469 else
471 if (del_buf)
473 /* Free all internal variables and reset option values, to make
474 * ":bdel" compatible with Vim 5.7. */
475 free_buffer_stuff(buf, TRUE);
477 /* Make it look like a new buffer. */
478 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
480 /* Init the options when loaded again. */
481 buf->b_p_initialized = FALSE;
483 buf_clear_file(buf);
484 if (del_buf)
485 buf->b_p_bl = FALSE;
490 * Make buffer not contain a file.
492 void
493 buf_clear_file(buf)
494 buf_T *buf;
496 buf->b_ml.ml_line_count = 1;
497 unchanged(buf, TRUE);
498 #ifndef SHORT_FNAME
499 buf->b_shortname = FALSE;
500 #endif
501 buf->b_p_eol = TRUE;
502 buf->b_start_eol = TRUE;
503 #ifdef FEAT_MBYTE
504 buf->b_p_bomb = FALSE;
505 buf->b_start_bomb = FALSE;
506 #endif
507 buf->b_ml.ml_mfp = NULL;
508 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
509 #ifdef FEAT_NETBEANS_INTG
510 netbeans_deleted_all_lines(buf);
511 #endif
515 * buf_freeall() - free all things allocated for a buffer that are related to
516 * the file.
518 /*ARGSUSED*/
519 void
520 buf_freeall(buf, del_buf, wipe_buf)
521 buf_T *buf;
522 int del_buf; /* buffer is going to be deleted */
523 int wipe_buf; /* buffer is going to be wiped out */
525 #ifdef FEAT_AUTOCMD
526 int is_curbuf = (buf == curbuf);
528 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, FALSE, buf);
529 if (!buf_valid(buf)) /* autocommands may delete the buffer */
530 return;
531 if (del_buf && buf->b_p_bl)
533 apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname, FALSE, buf);
534 if (!buf_valid(buf)) /* autocommands may delete the buffer */
535 return;
537 if (wipe_buf)
539 apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
540 FALSE, buf);
541 if (!buf_valid(buf)) /* autocommands may delete the buffer */
542 return;
544 # ifdef FEAT_EVAL
545 if (aborting()) /* autocmds may abort script processing */
546 return;
547 # endif
550 * It's possible that autocommands change curbuf to the one being deleted.
551 * This might cause curbuf to be deleted unexpectedly. But in some cases
552 * it's OK to delete the curbuf, because a new one is obtained anyway.
553 * Therefore only return if curbuf changed to the deleted buffer.
555 if (buf == curbuf && !is_curbuf)
556 return;
557 #endif
558 #ifdef FEAT_DIFF
559 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
560 #endif
562 #ifdef FEAT_FOLDING
563 /* No folds in an empty buffer. */
564 # ifdef FEAT_WINDOWS
566 win_T *win;
567 tabpage_T *tp;
569 FOR_ALL_TAB_WINDOWS(tp, win)
570 if (win->w_buffer == buf)
571 clearFolding(win);
573 # else
574 if (curwin->w_buffer == buf)
575 clearFolding(curwin);
576 # endif
577 #endif
579 #ifdef FEAT_TCL
580 tcl_buffer_free(buf);
581 #endif
582 u_blockfree(buf); /* free the memory allocated for undo */
583 ml_close(buf, TRUE); /* close and delete the memline/memfile */
584 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
585 u_clearall(buf); /* reset all undo information */
586 #ifdef FEAT_SYN_HL
587 syntax_clear(buf); /* reset syntax info */
588 #endif
589 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
593 * Free a buffer structure and the things it contains related to the buffer
594 * itself (not the file, that must have been done already).
596 static void
597 free_buffer(buf)
598 buf_T *buf;
600 free_buffer_stuff(buf, TRUE);
601 #ifdef FEAT_MZSCHEME
602 mzscheme_buffer_free(buf);
603 #endif
604 #ifdef FEAT_PERL
605 perl_buf_free(buf);
606 #endif
607 #ifdef FEAT_PYTHON
608 python_buffer_free(buf);
609 #endif
610 #ifdef FEAT_RUBY
611 ruby_buffer_free(buf);
612 #endif
613 #ifdef FEAT_AUTOCMD
614 aubuflocal_remove(buf);
615 #endif
616 vim_free(buf);
620 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
622 static void
623 free_buffer_stuff(buf, free_options)
624 buf_T *buf;
625 int free_options; /* free options as well */
627 if (free_options)
629 clear_wininfo(buf); /* including window-local options */
630 free_buf_options(buf, TRUE);
632 #ifdef FEAT_EVAL
633 vars_clear(&buf->b_vars.dv_hashtab); /* free all internal variables */
634 hash_init(&buf->b_vars.dv_hashtab);
635 #endif
636 #ifdef FEAT_USR_CMDS
637 uc_clear(&buf->b_ucmds); /* clear local user commands */
638 #endif
639 #ifdef FEAT_SIGNS
640 buf_delete_signs(buf); /* delete any signs */
641 #endif
642 #ifdef FEAT_NETBEANS_INTG
643 if (usingNetbeans)
644 netbeans_file_killed(buf);
645 #endif
646 #ifdef FEAT_LOCALMAP
647 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
648 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
649 #endif
650 #ifdef FEAT_MBYTE
651 vim_free(buf->b_start_fenc);
652 buf->b_start_fenc = NULL;
653 #endif
654 #ifdef FEAT_SPELL
655 ga_clear(&buf->b_langp);
656 #endif
660 * Free the b_wininfo list for buffer "buf".
662 static void
663 clear_wininfo(buf)
664 buf_T *buf;
666 wininfo_T *wip;
668 while (buf->b_wininfo != NULL)
670 wip = buf->b_wininfo;
671 buf->b_wininfo = wip->wi_next;
672 if (wip->wi_optset)
674 clear_winopt(&wip->wi_opt);
675 #ifdef FEAT_FOLDING
676 deleteFoldRecurse(&wip->wi_folds);
677 #endif
679 vim_free(wip);
683 #if defined(FEAT_LISTCMDS) || defined(PROTO)
685 * Go to another buffer. Handles the result of the ATTENTION dialog.
687 void
688 goto_buffer(eap, start, dir, count)
689 exarg_T *eap;
690 int start;
691 int dir;
692 int count;
694 # if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
695 buf_T *old_curbuf = curbuf;
697 swap_exists_action = SEA_DIALOG;
698 # endif
699 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
700 start, dir, count, eap->forceit);
701 # if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
702 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
704 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
705 cleanup_T cs;
707 /* Reset the error/interrupt/exception state here so that
708 * aborting() returns FALSE when closing a window. */
709 enter_cleanup(&cs);
710 # endif
712 /* Quitting means closing the split window, nothing else. */
713 win_close(curwin, TRUE);
714 swap_exists_action = SEA_NONE;
715 swap_exists_did_quit = TRUE;
717 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
718 /* Restore the error/interrupt/exception state if not discarded by a
719 * new aborting error, interrupt, or uncaught exception. */
720 leave_cleanup(&cs);
721 # endif
723 else
724 handle_swap_exists(old_curbuf);
725 # endif
727 #endif
729 #if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
731 * Handle the situation of swap_exists_action being set.
732 * It is allowed for "old_curbuf" to be NULL or invalid.
734 void
735 handle_swap_exists(old_curbuf)
736 buf_T *old_curbuf;
738 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
739 cleanup_T cs;
740 # endif
742 if (swap_exists_action == SEA_QUIT)
744 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
745 /* Reset the error/interrupt/exception state here so that
746 * aborting() returns FALSE when closing a buffer. */
747 enter_cleanup(&cs);
748 # endif
750 /* User selected Quit at ATTENTION prompt. Go back to previous
751 * buffer. If that buffer is gone or the same as the current one,
752 * open a new, empty buffer. */
753 swap_exists_action = SEA_NONE; /* don't want it again */
754 swap_exists_did_quit = TRUE;
755 close_buffer(curwin, curbuf, DOBUF_UNLOAD);
756 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
757 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
758 if (old_curbuf != NULL)
759 enter_buffer(old_curbuf);
760 /* If "old_curbuf" is NULL we are in big trouble here... */
762 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
763 /* Restore the error/interrupt/exception state if not discarded by a
764 * new aborting error, interrupt, or uncaught exception. */
765 leave_cleanup(&cs);
766 # endif
768 else if (swap_exists_action == SEA_RECOVER)
770 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
771 /* Reset the error/interrupt/exception state here so that
772 * aborting() returns FALSE when closing a buffer. */
773 enter_cleanup(&cs);
774 # endif
776 /* User selected Recover at ATTENTION prompt. */
777 msg_scroll = TRUE;
778 ml_recover();
779 MSG_PUTS("\n"); /* don't overwrite the last message */
780 cmdline_row = msg_row;
781 do_modelines(0);
783 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
784 /* Restore the error/interrupt/exception state if not discarded by a
785 * new aborting error, interrupt, or uncaught exception. */
786 leave_cleanup(&cs);
787 # endif
789 swap_exists_action = SEA_NONE;
791 #endif
793 #if defined(FEAT_LISTCMDS) || defined(PROTO)
795 * do_bufdel() - delete or unload buffer(s)
797 * addr_count == 0: ":bdel" - delete current buffer
798 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
799 * buffer "end_bnr", then any other arguments.
800 * addr_count == 2: ":N,N bdel" - delete buffers in range
802 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
803 * DOBUF_DEL (":bdel")
805 * Returns error message or NULL
807 char_u *
808 do_bufdel(command, arg, addr_count, start_bnr, end_bnr, forceit)
809 int command;
810 char_u *arg; /* pointer to extra arguments */
811 int addr_count;
812 int start_bnr; /* first buffer number in a range */
813 int end_bnr; /* buffer nr or last buffer nr in a range */
814 int forceit;
816 int do_current = 0; /* delete current buffer? */
817 int deleted = 0; /* number of buffers deleted */
818 char_u *errormsg = NULL; /* return value */
819 int bnr; /* buffer number */
820 char_u *p;
822 if (addr_count == 0)
824 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
826 else
828 if (addr_count == 2)
830 if (*arg) /* both range and argument is not allowed */
831 return (char_u *)_(e_trailing);
832 bnr = start_bnr;
834 else /* addr_count == 1 */
835 bnr = end_bnr;
837 for ( ;!got_int; ui_breakcheck())
840 * delete the current buffer last, otherwise when the
841 * current buffer is deleted, the next buffer becomes
842 * the current one and will be loaded, which may then
843 * also be deleted, etc.
845 if (bnr == curbuf->b_fnum)
846 do_current = bnr;
847 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
848 forceit) == OK)
849 ++deleted;
852 * find next buffer number to delete/unload
854 if (addr_count == 2)
856 if (++bnr > end_bnr)
857 break;
859 else /* addr_count == 1 */
861 arg = skipwhite(arg);
862 if (*arg == NUL)
863 break;
864 if (!VIM_ISDIGIT(*arg))
866 p = skiptowhite_esc(arg);
867 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE, FALSE);
868 if (bnr < 0) /* failed */
869 break;
870 arg = p;
872 else
873 bnr = getdigits(&arg);
876 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
877 FORWARD, do_current, forceit) == OK)
878 ++deleted;
880 if (deleted == 0)
882 if (command == DOBUF_UNLOAD)
883 STRCPY(IObuff, _("E515: No buffers were unloaded"));
884 else if (command == DOBUF_DEL)
885 STRCPY(IObuff, _("E516: No buffers were deleted"));
886 else
887 STRCPY(IObuff, _("E517: No buffers were wiped out"));
888 errormsg = IObuff;
890 else if (deleted >= p_report)
892 if (command == DOBUF_UNLOAD)
894 if (deleted == 1)
895 MSG(_("1 buffer unloaded"));
896 else
897 smsg((char_u *)_("%d buffers unloaded"), deleted);
899 else if (command == DOBUF_DEL)
901 if (deleted == 1)
902 MSG(_("1 buffer deleted"));
903 else
904 smsg((char_u *)_("%d buffers deleted"), deleted);
906 else
908 if (deleted == 1)
909 MSG(_("1 buffer wiped out"));
910 else
911 smsg((char_u *)_("%d buffers wiped out"), deleted);
917 return errormsg;
921 * Implementation of the commands for the buffer list.
923 * action == DOBUF_GOTO go to specified buffer
924 * action == DOBUF_SPLIT split window and go to specified buffer
925 * action == DOBUF_UNLOAD unload specified buffer(s)
926 * action == DOBUF_DEL delete specified buffer(s) from buffer list
927 * action == DOBUF_WIPE delete specified buffer(s) really
929 * start == DOBUF_CURRENT go to "count" buffer from current buffer
930 * start == DOBUF_FIRST go to "count" buffer from first buffer
931 * start == DOBUF_LAST go to "count" buffer from last buffer
932 * start == DOBUF_MOD go to "count" modified buffer from current buffer
934 * Return FAIL or OK.
937 do_buffer(action, start, dir, count, forceit)
938 int action;
939 int start;
940 int dir; /* FORWARD or BACKWARD */
941 int count; /* buffer number or number of buffers */
942 int forceit; /* TRUE for :...! */
944 buf_T *buf;
945 buf_T *bp;
946 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
947 || action == DOBUF_WIPE);
949 switch (start)
951 case DOBUF_FIRST: buf = firstbuf; break;
952 case DOBUF_LAST: buf = lastbuf; break;
953 default: buf = curbuf; break;
955 if (start == DOBUF_MOD) /* find next modified buffer */
957 while (count-- > 0)
961 buf = buf->b_next;
962 if (buf == NULL)
963 buf = firstbuf;
965 while (buf != curbuf && !bufIsChanged(buf));
967 if (!bufIsChanged(buf))
969 EMSG(_("E84: No modified buffer found"));
970 return FAIL;
973 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
975 while (buf != NULL && buf->b_fnum != count)
976 buf = buf->b_next;
978 else
980 bp = NULL;
981 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
983 /* remember the buffer where we start, we come back there when all
984 * buffers are unlisted. */
985 if (bp == NULL)
986 bp = buf;
987 if (dir == FORWARD)
989 buf = buf->b_next;
990 if (buf == NULL)
991 buf = firstbuf;
993 else
995 buf = buf->b_prev;
996 if (buf == NULL)
997 buf = lastbuf;
999 /* don't count unlisted buffers */
1000 if (unload || buf->b_p_bl)
1002 --count;
1003 bp = NULL; /* use this buffer as new starting point */
1005 if (bp == buf)
1007 /* back where we started, didn't find anything. */
1008 EMSG(_("E85: There is no listed buffer"));
1009 return FAIL;
1014 if (buf == NULL) /* could not find it */
1016 if (start == DOBUF_FIRST)
1018 /* don't warn when deleting */
1019 if (!unload)
1020 EMSGN(_("E86: Buffer %ld does not exist"), count);
1022 else if (dir == FORWARD)
1023 EMSG(_("E87: Cannot go beyond last buffer"));
1024 else
1025 EMSG(_("E88: Cannot go before first buffer"));
1026 return FAIL;
1029 #ifdef FEAT_GUI
1030 need_mouse_correct = TRUE;
1031 #endif
1033 #ifdef FEAT_LISTCMDS
1035 * delete buffer buf from memory and/or the list
1037 if (unload)
1039 int forward;
1040 int retval;
1042 /* When unloading or deleting a buffer that's already unloaded and
1043 * unlisted: fail silently. */
1044 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1045 return FAIL;
1047 if (!forceit && bufIsChanged(buf))
1049 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1050 if ((p_confirm || cmdmod.confirm) && p_write)
1052 dialog_changed(buf, FALSE);
1053 # ifdef FEAT_AUTOCMD
1054 if (!buf_valid(buf))
1055 /* Autocommand deleted buffer, oops! It's not changed
1056 * now. */
1057 return FAIL;
1058 # endif
1059 /* If it's still changed fail silently, the dialog already
1060 * mentioned why it fails. */
1061 if (bufIsChanged(buf))
1062 return FAIL;
1064 else
1065 #endif
1067 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1068 buf->b_fnum);
1069 return FAIL;
1074 * If deleting the last (listed) buffer, make it empty.
1075 * The last (listed) buffer cannot be unloaded.
1077 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1078 if (bp->b_p_bl && bp != buf)
1079 break;
1080 if (bp == NULL && buf == curbuf)
1082 if (action == DOBUF_UNLOAD)
1084 EMSG(_("E90: Cannot unload last buffer"));
1085 return FAIL;
1088 /* Close any other windows on this buffer, then make it empty. */
1089 #ifdef FEAT_WINDOWS
1090 close_windows(buf, TRUE);
1091 #endif
1092 setpcmark();
1093 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1094 forceit ? ECMD_FORCEIT : 0, curwin);
1097 * do_ecmd() may create a new buffer, then we have to delete
1098 * the old one. But do_ecmd() may have done that already, check
1099 * if the buffer still exists.
1101 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
1102 close_buffer(NULL, buf, action);
1103 return retval;
1106 #ifdef FEAT_WINDOWS
1108 * If the deleted buffer is the current one, close the current window
1109 * (unless it's the only window). Repeat this so long as we end up in
1110 * a window with this buffer.
1112 while (buf == curbuf
1113 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
1114 win_close(curwin, FALSE);
1115 #endif
1118 * If the buffer to be deleted is not the current one, delete it here.
1120 if (buf != curbuf)
1122 #ifdef FEAT_WINDOWS
1123 close_windows(buf, FALSE);
1124 #endif
1125 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
1126 close_buffer(NULL, buf, action);
1127 return OK;
1131 * Deleting the current buffer: Need to find another buffer to go to.
1132 * There must be another, otherwise it would have been handled above.
1133 * First use au_new_curbuf, if it is valid.
1134 * Then prefer the buffer we most recently visited.
1135 * Else try to find one that is loaded, after the current buffer,
1136 * then before the current buffer.
1137 * Finally use any buffer.
1139 buf = NULL; /* selected buffer */
1140 bp = NULL; /* used when no loaded buffer found */
1141 #ifdef FEAT_AUTOCMD
1142 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1143 buf = au_new_curbuf;
1144 # ifdef FEAT_JUMPLIST
1145 else
1146 # endif
1147 #endif
1148 #ifdef FEAT_JUMPLIST
1149 if (curwin->w_jumplistlen > 0)
1151 int jumpidx;
1153 jumpidx = curwin->w_jumplistidx - 1;
1154 if (jumpidx < 0)
1155 jumpidx = curwin->w_jumplistlen - 1;
1157 forward = jumpidx;
1158 while (jumpidx != curwin->w_jumplistidx)
1160 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1161 if (buf != NULL)
1163 if (buf == curbuf || !buf->b_p_bl)
1164 buf = NULL; /* skip current and unlisted bufs */
1165 else if (buf->b_ml.ml_mfp == NULL)
1167 /* skip unloaded buf, but may keep it for later */
1168 if (bp == NULL)
1169 bp = buf;
1170 buf = NULL;
1173 if (buf != NULL) /* found a valid buffer: stop searching */
1174 break;
1175 /* advance to older entry in jump list */
1176 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1177 break;
1178 if (--jumpidx < 0)
1179 jumpidx = curwin->w_jumplistlen - 1;
1180 if (jumpidx == forward) /* List exhausted for sure */
1181 break;
1184 #endif
1186 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1188 forward = TRUE;
1189 buf = curbuf->b_next;
1190 for (;;)
1192 if (buf == NULL)
1194 if (!forward) /* tried both directions */
1195 break;
1196 buf = curbuf->b_prev;
1197 forward = FALSE;
1198 continue;
1200 /* in non-help buffer, try to skip help buffers, and vv */
1201 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1203 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1204 break;
1205 if (bp == NULL) /* remember unloaded buf for later */
1206 bp = buf;
1208 if (forward)
1209 buf = buf->b_next;
1210 else
1211 buf = buf->b_prev;
1214 if (buf == NULL) /* No loaded buffer, use unloaded one */
1215 buf = bp;
1216 if (buf == NULL) /* No loaded buffer, find listed one */
1218 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1219 if (buf->b_p_bl && buf != curbuf)
1220 break;
1222 if (buf == NULL) /* Still no buffer, just take one */
1224 if (curbuf->b_next != NULL)
1225 buf = curbuf->b_next;
1226 else
1227 buf = curbuf->b_prev;
1232 * make buf current buffer
1234 if (action == DOBUF_SPLIT) /* split window first */
1236 # ifdef FEAT_WINDOWS
1237 /* If 'switchbuf' contains "useopen": jump to first window containing
1238 * "buf" if one exists */
1239 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
1240 return OK;
1241 /* If 'switchbuf' contains "usetab": jump to first window in any tab
1242 * page containing "buf" if one exists */
1243 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
1244 return OK;
1245 if (win_split(0, 0) == FAIL)
1246 # endif
1247 return FAIL;
1249 #endif
1251 /* go to current buffer - nothing to do */
1252 if (buf == curbuf)
1253 return OK;
1256 * Check if the current buffer may be abandoned.
1258 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1260 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1261 if ((p_confirm || cmdmod.confirm) && p_write)
1263 dialog_changed(curbuf, FALSE);
1264 # ifdef FEAT_AUTOCMD
1265 if (!buf_valid(buf))
1266 /* Autocommand deleted buffer, oops! */
1267 return FAIL;
1268 # endif
1270 if (bufIsChanged(curbuf))
1271 #endif
1273 EMSG(_(e_nowrtmsg));
1274 return FAIL;
1278 /* Go to the other buffer. */
1279 set_curbuf(buf, action);
1281 #if defined(FEAT_LISTCMDS) && defined(FEAT_SCROLLBIND)
1282 if (action == DOBUF_SPLIT)
1283 curwin->w_p_scb = FALSE; /* reset 'scrollbind' */
1284 #endif
1286 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1287 if (aborting()) /* autocmds may abort script processing */
1288 return FAIL;
1289 #endif
1291 return OK;
1294 #endif /* FEAT_LISTCMDS */
1297 * Set current buffer to "buf". Executes autocommands and closes current
1298 * buffer. "action" tells how to close the current buffer:
1299 * DOBUF_GOTO free or hide it
1300 * DOBUF_SPLIT nothing
1301 * DOBUF_UNLOAD unload it
1302 * DOBUF_DEL delete it
1303 * DOBUF_WIPE wipe it out
1305 void
1306 set_curbuf(buf, action)
1307 buf_T *buf;
1308 int action;
1310 buf_T *prevbuf;
1311 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1312 || action == DOBUF_WIPE);
1314 setpcmark();
1315 if (!cmdmod.keepalt)
1316 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
1317 buflist_altfpos(curwin); /* remember curpos */
1319 #ifdef FEAT_VISUAL
1320 /* Don't restart Select mode after switching to another buffer. */
1321 VIsual_reselect = FALSE;
1322 #endif
1324 /* close_windows() or apply_autocmds() may change curbuf */
1325 prevbuf = curbuf;
1327 #ifdef FEAT_AUTOCMD
1328 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1329 # ifdef FEAT_EVAL
1330 if (buf_valid(prevbuf) && !aborting())
1331 # else
1332 if (buf_valid(prevbuf))
1333 # endif
1334 #endif
1336 #ifdef FEAT_WINDOWS
1337 if (unload)
1338 close_windows(prevbuf, FALSE);
1339 #endif
1340 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1341 if (buf_valid(prevbuf) && !aborting())
1342 #else
1343 if (buf_valid(prevbuf))
1344 #endif
1346 if (prevbuf == curbuf)
1347 u_sync(FALSE);
1348 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1349 unload ? action : (action == DOBUF_GOTO
1350 && !P_HID(prevbuf)
1351 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0);
1354 #ifdef FEAT_AUTOCMD
1355 /* An autocommand may have deleted "buf", already entered it (e.g., when
1356 * it did ":bunload") or aborted the script processing! */
1357 # ifdef FEAT_EVAL
1358 if (buf_valid(buf) && buf != curbuf && !aborting())
1359 # else
1360 if (buf_valid(buf) && buf != curbuf)
1361 # endif
1362 #endif
1363 enter_buffer(buf);
1367 * Enter a new current buffer.
1368 * Old curbuf must have been abandoned already!
1370 void
1371 enter_buffer(buf)
1372 buf_T *buf;
1374 /* Copy buffer and window local option values. Not for a help buffer. */
1375 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1376 if (!buf->b_help)
1377 get_winopts(buf);
1378 #ifdef FEAT_FOLDING
1379 else
1380 /* Remove all folds in the window. */
1381 clearFolding(curwin);
1382 foldUpdateAll(curwin); /* update folds (later). */
1383 #endif
1385 /* Get the buffer in the current window. */
1386 curwin->w_buffer = buf;
1387 curbuf = buf;
1388 ++curbuf->b_nwindows;
1390 #ifdef FEAT_DIFF
1391 if (curwin->w_p_diff)
1392 diff_buf_add(curbuf);
1393 #endif
1395 /* Cursor on first line by default. */
1396 curwin->w_cursor.lnum = 1;
1397 curwin->w_cursor.col = 0;
1398 #ifdef FEAT_VIRTUALEDIT
1399 curwin->w_cursor.coladd = 0;
1400 #endif
1401 curwin->w_set_curswant = TRUE;
1402 #ifdef FEAT_AUTOCMD
1403 curwin->w_topline_was_set = FALSE;
1404 #endif
1406 /* Make sure the buffer is loaded. */
1407 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
1409 #ifdef FEAT_AUTOCMD
1410 /* If there is no filetype, allow for detecting one. Esp. useful for
1411 * ":ball" used in a autocommand. If there already is a filetype we
1412 * might prefer to keep it. */
1413 if (*curbuf->b_p_ft == NUL)
1414 did_filetype = FALSE;
1415 #endif
1417 open_buffer(FALSE, NULL);
1419 else
1421 if (!msg_silent)
1422 need_fileinfo = TRUE; /* display file info after redraw */
1423 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1424 #ifdef FEAT_AUTOCMD
1425 curwin->w_topline = 1;
1426 # ifdef FEAT_DIFF
1427 curwin->w_topfill = 0;
1428 # endif
1429 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1430 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1431 #endif
1434 /* If autocommands did not change the cursor position, restore cursor lnum
1435 * and possibly cursor col. */
1436 if (curwin->w_cursor.lnum == 1 && inindent(0))
1437 buflist_getfpos();
1439 check_arg_idx(curwin); /* check for valid arg_idx */
1440 #ifdef FEAT_TITLE
1441 maketitle();
1442 #endif
1443 #ifdef FEAT_AUTOCMD
1444 /* when autocmds didn't change it */
1445 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
1446 #endif
1447 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1449 #ifdef FEAT_NETBEANS_INTG
1450 /* Send fileOpened event because we've changed buffers. */
1451 if (usingNetbeans && isNetbeansBuffer(curbuf))
1452 netbeans_file_activated(curbuf);
1453 #endif
1455 /* Change directories when the 'acd' option is set. */
1456 DO_AUTOCHDIR
1458 #ifdef FEAT_KEYMAP
1459 if (curbuf->b_kmap_state & KEYMAP_INIT)
1460 keymap_init();
1461 #endif
1462 #ifdef FEAT_SPELL
1463 /* May need to set the spell language. Can only do this after the buffer
1464 * has been properly setup. */
1465 if (!curbuf->b_help && curwin->w_p_spell && *curbuf->b_p_spl != NUL)
1466 did_set_spelllang(curbuf);
1467 #endif
1469 redraw_later(NOT_VALID);
1472 #if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1474 * Change to the directory of the current buffer.
1476 void
1477 do_autochdir()
1479 if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK)
1480 shorten_fnames(TRUE);
1482 #endif
1485 * functions for dealing with the buffer list
1489 * Add a file name to the buffer list. Return a pointer to the buffer.
1490 * If the same file name already exists return a pointer to that buffer.
1491 * If it does not exist, or if fname == NULL, a new entry is created.
1492 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1493 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1494 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
1495 * This is the ONLY way to create a new buffer.
1497 static int top_file_num = 1; /* highest file number */
1499 buf_T *
1500 buflist_new(ffname, sfname, lnum, flags)
1501 char_u *ffname; /* full path of fname or relative */
1502 char_u *sfname; /* short fname or NULL */
1503 linenr_T lnum; /* preferred cursor line */
1504 int flags; /* BLN_ defines */
1506 buf_T *buf;
1507 #ifdef UNIX
1508 struct stat st;
1509 #endif
1511 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1514 * If file name already exists in the list, update the entry.
1516 #ifdef UNIX
1517 /* On Unix we can use inode numbers when the file exists. Works better
1518 * for hard links. */
1519 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1520 st.st_dev = (dev_T)-1;
1521 #endif
1522 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1523 #ifdef UNIX
1524 buflist_findname_stat(ffname, &st)
1525 #else
1526 buflist_findname(ffname)
1527 #endif
1528 ) != NULL)
1530 vim_free(ffname);
1531 if (lnum != 0)
1532 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1533 /* copy the options now, if 'cpo' doesn't have 's' and not done
1534 * already */
1535 buf_copy_options(buf, 0);
1536 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1538 buf->b_p_bl = TRUE;
1539 #ifdef FEAT_AUTOCMD
1540 if (!(flags & BLN_DUMMY))
1541 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1542 #endif
1544 return buf;
1548 * If the current buffer has no name and no contents, use the current
1549 * buffer. Otherwise: Need to allocate a new buffer structure.
1551 * This is the ONLY place where a new buffer structure is allocated!
1552 * (A spell file buffer is allocated in spell.c, but that's not a normal
1553 * buffer.)
1555 buf = NULL;
1556 if ((flags & BLN_CURBUF)
1557 && curbuf != NULL
1558 && curbuf->b_ffname == NULL
1559 && curbuf->b_nwindows <= 1
1560 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1562 buf = curbuf;
1563 #ifdef FEAT_AUTOCMD
1564 /* It's like this buffer is deleted. Watch out for autocommands that
1565 * change curbuf! If that happens, allocate a new buffer anyway. */
1566 if (curbuf->b_p_bl)
1567 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1568 if (buf == curbuf)
1569 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1570 # ifdef FEAT_EVAL
1571 if (aborting()) /* autocmds may abort script processing */
1572 return NULL;
1573 # endif
1574 #endif
1575 #ifdef FEAT_QUICKFIX
1576 # ifdef FEAT_AUTOCMD
1577 if (buf == curbuf)
1578 # endif
1580 /* Make sure 'bufhidden' and 'buftype' are empty */
1581 clear_string_option(&buf->b_p_bh);
1582 clear_string_option(&buf->b_p_bt);
1584 #endif
1586 if (buf != curbuf || curbuf == NULL)
1588 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1589 if (buf == NULL)
1591 vim_free(ffname);
1592 return NULL;
1596 if (ffname != NULL)
1598 buf->b_ffname = ffname;
1599 buf->b_sfname = vim_strsave(sfname);
1602 clear_wininfo(buf);
1603 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1605 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1606 || buf->b_wininfo == NULL)
1608 vim_free(buf->b_ffname);
1609 buf->b_ffname = NULL;
1610 vim_free(buf->b_sfname);
1611 buf->b_sfname = NULL;
1612 if (buf != curbuf)
1613 free_buffer(buf);
1614 return NULL;
1617 if (buf == curbuf)
1619 /* free all things allocated for this buffer */
1620 buf_freeall(buf, FALSE, FALSE);
1621 if (buf != curbuf) /* autocommands deleted the buffer! */
1622 return NULL;
1623 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1624 if (aborting()) /* autocmds may abort script processing */
1625 return NULL;
1626 #endif
1627 /* buf->b_nwindows = 0; why was this here? */
1628 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
1629 #ifdef FEAT_KEYMAP
1630 /* need to reload lmaps and set b:keymap_name */
1631 curbuf->b_kmap_state |= KEYMAP_INIT;
1632 #endif
1634 else
1637 * put new buffer at the end of the buffer list
1639 buf->b_next = NULL;
1640 if (firstbuf == NULL) /* buffer list is empty */
1642 buf->b_prev = NULL;
1643 firstbuf = buf;
1645 else /* append new buffer at end of list */
1647 lastbuf->b_next = buf;
1648 buf->b_prev = lastbuf;
1650 lastbuf = buf;
1652 buf->b_fnum = top_file_num++;
1653 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1655 EMSG(_("W14: Warning: List of file names overflow"));
1656 if (emsg_silent == 0)
1658 out_flush();
1659 ui_delay(3000L, TRUE); /* make sure it is noticed */
1661 top_file_num = 1;
1665 * Always copy the options from the current buffer.
1667 buf_copy_options(buf, BCO_ALWAYS);
1670 buf->b_wininfo->wi_fpos.lnum = lnum;
1671 buf->b_wininfo->wi_win = curwin;
1673 #ifdef FEAT_EVAL
1674 init_var_dict(&buf->b_vars, &buf->b_bufvar); /* init b: variables */
1675 #endif
1676 #ifdef FEAT_SYN_HL
1677 hash_init(&buf->b_keywtab);
1678 hash_init(&buf->b_keywtab_ic);
1679 #endif
1681 buf->b_fname = buf->b_sfname;
1682 #ifdef UNIX
1683 if (st.st_dev == (dev_T)-1)
1684 buf->b_dev = -1;
1685 else
1687 buf->b_dev = st.st_dev;
1688 buf->b_ino = st.st_ino;
1690 #endif
1691 buf->b_u_synced = TRUE;
1692 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
1693 if (flags & BLN_DUMMY)
1694 buf->b_flags |= BF_DUMMY;
1695 buf_clear_file(buf);
1696 clrallmarks(buf); /* clear marks */
1697 fmarks_check_names(buf); /* check file marks for this file */
1698 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1699 #ifdef FEAT_AUTOCMD
1700 if (!(flags & BLN_DUMMY))
1702 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
1703 if (flags & BLN_LISTED)
1704 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1705 # ifdef FEAT_EVAL
1706 if (aborting()) /* autocmds may abort script processing */
1707 return NULL;
1708 # endif
1710 #endif
1712 return buf;
1716 * Free the memory for the options of a buffer.
1717 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1718 * 'fileencoding'.
1720 void
1721 free_buf_options(buf, free_p_ff)
1722 buf_T *buf;
1723 int free_p_ff;
1725 if (free_p_ff)
1727 #ifdef FEAT_MBYTE
1728 clear_string_option(&buf->b_p_fenc);
1729 #endif
1730 clear_string_option(&buf->b_p_ff);
1731 #ifdef FEAT_QUICKFIX
1732 clear_string_option(&buf->b_p_bh);
1733 clear_string_option(&buf->b_p_bt);
1734 #endif
1736 #ifdef FEAT_FIND_ID
1737 clear_string_option(&buf->b_p_def);
1738 clear_string_option(&buf->b_p_inc);
1739 # ifdef FEAT_EVAL
1740 clear_string_option(&buf->b_p_inex);
1741 # endif
1742 #endif
1743 #if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1744 clear_string_option(&buf->b_p_inde);
1745 clear_string_option(&buf->b_p_indk);
1746 #endif
1747 #if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1748 clear_string_option(&buf->b_p_bexpr);
1749 #endif
1750 #if defined(FEAT_EVAL)
1751 clear_string_option(&buf->b_p_fex);
1752 #endif
1753 #ifdef FEAT_CRYPT
1754 clear_string_option(&buf->b_p_key);
1755 #endif
1756 clear_string_option(&buf->b_p_kp);
1757 clear_string_option(&buf->b_p_mps);
1758 clear_string_option(&buf->b_p_fo);
1759 clear_string_option(&buf->b_p_flp);
1760 clear_string_option(&buf->b_p_isk);
1761 #ifdef FEAT_KEYMAP
1762 clear_string_option(&buf->b_p_keymap);
1763 ga_clear(&buf->b_kmap_ga);
1764 #endif
1765 #ifdef FEAT_COMMENTS
1766 clear_string_option(&buf->b_p_com);
1767 #endif
1768 #ifdef FEAT_FOLDING
1769 clear_string_option(&buf->b_p_cms);
1770 #endif
1771 clear_string_option(&buf->b_p_nf);
1772 #ifdef FEAT_SYN_HL
1773 clear_string_option(&buf->b_p_syn);
1774 #endif
1775 #ifdef FEAT_SPELL
1776 clear_string_option(&buf->b_p_spc);
1777 clear_string_option(&buf->b_p_spf);
1778 vim_free(buf->b_cap_prog);
1779 buf->b_cap_prog = NULL;
1780 clear_string_option(&buf->b_p_spl);
1781 #endif
1782 #ifdef FEAT_SEARCHPATH
1783 clear_string_option(&buf->b_p_sua);
1784 #endif
1785 #ifdef FEAT_AUTOCMD
1786 clear_string_option(&buf->b_p_ft);
1787 #endif
1788 #ifdef FEAT_OSFILETYPE
1789 clear_string_option(&buf->b_p_oft);
1790 #endif
1791 #ifdef FEAT_CINDENT
1792 clear_string_option(&buf->b_p_cink);
1793 clear_string_option(&buf->b_p_cino);
1794 #endif
1795 #if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1796 clear_string_option(&buf->b_p_cinw);
1797 #endif
1798 #ifdef FEAT_INS_EXPAND
1799 clear_string_option(&buf->b_p_cpt);
1800 #endif
1801 #ifdef FEAT_COMPL_FUNC
1802 clear_string_option(&buf->b_p_cfu);
1803 clear_string_option(&buf->b_p_ofu);
1804 #endif
1805 #ifdef FEAT_QUICKFIX
1806 clear_string_option(&buf->b_p_gp);
1807 clear_string_option(&buf->b_p_mp);
1808 clear_string_option(&buf->b_p_efm);
1809 #endif
1810 clear_string_option(&buf->b_p_ep);
1811 clear_string_option(&buf->b_p_path);
1812 clear_string_option(&buf->b_p_tags);
1813 #ifdef FEAT_INS_EXPAND
1814 clear_string_option(&buf->b_p_dict);
1815 clear_string_option(&buf->b_p_tsr);
1816 #endif
1817 #ifdef FEAT_TEXTOBJ
1818 clear_string_option(&buf->b_p_qe);
1819 #endif
1820 buf->b_p_ar = -1;
1824 * get alternate file n
1825 * set linenr to lnum or altfpos.lnum if lnum == 0
1826 * also set cursor column to altfpos.col if 'startofline' is not set.
1827 * if (options & GETF_SETMARK) call setpcmark()
1828 * if (options & GETF_ALT) we are jumping to an alternate file.
1829 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1831 * return FAIL for failure, OK for success
1834 buflist_getfile(n, lnum, options, forceit)
1835 int n;
1836 linenr_T lnum;
1837 int options;
1838 int forceit;
1840 buf_T *buf;
1841 #ifdef FEAT_WINDOWS
1842 win_T *wp = NULL;
1843 #endif
1844 pos_T *fpos;
1845 colnr_T col;
1847 buf = buflist_findnr(n);
1848 if (buf == NULL)
1850 if ((options & GETF_ALT) && n == 0)
1851 EMSG(_(e_noalt));
1852 else
1853 EMSGN(_("E92: Buffer %ld not found"), n);
1854 return FAIL;
1857 /* if alternate file is the current buffer, nothing to do */
1858 if (buf == curbuf)
1859 return OK;
1861 if (text_locked())
1863 text_locked_msg();
1864 return FAIL;
1866 #ifdef FEAT_AUTOCMD
1867 if (curbuf_locked())
1868 return FAIL;
1869 #endif
1871 /* altfpos may be changed by getfile(), get it now */
1872 if (lnum == 0)
1874 fpos = buflist_findfpos(buf);
1875 lnum = fpos->lnum;
1876 col = fpos->col;
1878 else
1879 col = 0;
1881 #ifdef FEAT_WINDOWS
1882 if (options & GETF_SWITCH)
1884 /* If 'switchbuf' contains "useopen": jump to first window containing
1885 * "buf" if one exists */
1886 if (swb_flags & SWB_USEOPEN)
1887 wp = buf_jump_open_win(buf);
1888 /* If 'switchbuf' contians "usetab": jump to first window in any tab
1889 * page containing "buf" if one exists */
1890 if (wp == NULL && (swb_flags & SWB_USETAB))
1891 wp = buf_jump_open_tab(buf);
1892 /* If 'switchbuf' contains "split" or "newtab" and the current buffer
1893 * isn't empty: open new window */
1894 if (wp == NULL && (swb_flags & (SWB_SPLIT | SWB_NEWTAB)) && !bufempty())
1896 if (swb_flags & SWB_NEWTAB) /* Open in a new tab */
1897 tabpage_new();
1898 else if (win_split(0, 0) == FAIL) /* Open in a new window */
1899 return FAIL;
1900 # ifdef FEAT_SCROLLBIND
1901 curwin->w_p_scb = FALSE;
1902 # endif
1905 #endif
1907 ++RedrawingDisabled;
1908 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
1909 lnum, forceit) <= 0)
1911 --RedrawingDisabled;
1913 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
1914 if (!p_sol && col != 0)
1916 curwin->w_cursor.col = col;
1917 check_cursor_col();
1918 #ifdef FEAT_VIRTUALEDIT
1919 curwin->w_cursor.coladd = 0;
1920 #endif
1921 curwin->w_set_curswant = TRUE;
1923 return OK;
1925 --RedrawingDisabled;
1926 return FAIL;
1930 * go to the last know line number for the current buffer
1932 void
1933 buflist_getfpos()
1935 pos_T *fpos;
1937 fpos = buflist_findfpos(curbuf);
1939 curwin->w_cursor.lnum = fpos->lnum;
1940 check_cursor_lnum();
1942 if (p_sol)
1943 curwin->w_cursor.col = 0;
1944 else
1946 curwin->w_cursor.col = fpos->col;
1947 check_cursor_col();
1948 #ifdef FEAT_VIRTUALEDIT
1949 curwin->w_cursor.coladd = 0;
1950 #endif
1951 curwin->w_set_curswant = TRUE;
1955 #if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
1957 * Find file in buffer list by name (it has to be for the current window).
1958 * Returns NULL if not found.
1960 buf_T *
1961 buflist_findname_exp(fname)
1962 char_u *fname;
1964 char_u *ffname;
1965 buf_T *buf = NULL;
1967 /* First make the name into a full path name */
1968 ffname = FullName_save(fname,
1969 #ifdef UNIX
1970 TRUE /* force expansion, get rid of symbolic links */
1971 #else
1972 FALSE
1973 #endif
1975 if (ffname != NULL)
1977 buf = buflist_findname(ffname);
1978 vim_free(ffname);
1980 return buf;
1982 #endif
1985 * Find file in buffer list by name (it has to be for the current window).
1986 * "ffname" must have a full path.
1987 * Skips dummy buffers.
1988 * Returns NULL if not found.
1990 buf_T *
1991 buflist_findname(ffname)
1992 char_u *ffname;
1994 #ifdef UNIX
1995 struct stat st;
1997 if (mch_stat((char *)ffname, &st) < 0)
1998 st.st_dev = (dev_T)-1;
1999 return buflist_findname_stat(ffname, &st);
2003 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2004 * twice for the same file.
2005 * Returns NULL if not found.
2007 static buf_T *
2008 buflist_findname_stat(ffname, stp)
2009 char_u *ffname;
2010 struct stat *stp;
2012 #endif
2013 buf_T *buf;
2015 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2016 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
2017 #ifdef UNIX
2018 , stp
2019 #endif
2021 return buf;
2022 return NULL;
2025 #if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO)
2027 * Find file in buffer list by a regexp pattern.
2028 * Return fnum of the found buffer.
2029 * Return < 0 for error.
2031 /*ARGSUSED*/
2033 buflist_findpat(pattern, pattern_end, unlisted, diffmode)
2034 char_u *pattern;
2035 char_u *pattern_end; /* pointer to first char after pattern */
2036 int unlisted; /* find unlisted buffers */
2037 int diffmode; /* find diff-mode buffers only */
2039 buf_T *buf;
2040 regprog_T *prog;
2041 int match = -1;
2042 int find_listed;
2043 char_u *pat;
2044 char_u *patend;
2045 int attempt;
2046 char_u *p;
2047 int toggledollar;
2049 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2051 if (*pattern == '%')
2052 match = curbuf->b_fnum;
2053 else
2054 match = curwin->w_alt_fnum;
2055 #ifdef FEAT_DIFF
2056 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2057 match = -1;
2058 #endif
2062 * Try four ways of matching a listed buffer:
2063 * attempt == 0: without '^' or '$' (at any position)
2064 * attempt == 1: with '^' at start (only at position 0)
2065 * attempt == 2: with '$' at end (only match at end)
2066 * attempt == 3: with '^' at start and '$' at end (only full match)
2067 * Repeat this for finding an unlisted buffer if there was no matching
2068 * listed buffer.
2070 else
2072 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2073 if (pat == NULL)
2074 return -1;
2075 patend = pat + STRLEN(pat) - 1;
2076 toggledollar = (patend > pat && *patend == '$');
2078 /* First try finding a listed buffer. If not found and "unlisted"
2079 * is TRUE, try finding an unlisted buffer. */
2080 find_listed = TRUE;
2081 for (;;)
2083 for (attempt = 0; attempt <= 3; ++attempt)
2085 /* may add '^' and '$' */
2086 if (toggledollar)
2087 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2088 p = pat;
2089 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2090 ++p;
2091 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2092 if (prog == NULL)
2094 vim_free(pat);
2095 return -1;
2098 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2099 if (buf->b_p_bl == find_listed
2100 #ifdef FEAT_DIFF
2101 && (!diffmode || diff_mode_buf(buf))
2102 #endif
2103 && buflist_match(prog, buf) != NULL)
2105 if (match >= 0) /* already found a match */
2107 match = -2;
2108 break;
2110 match = buf->b_fnum; /* remember first match */
2113 vim_free(prog);
2114 if (match >= 0) /* found one match */
2115 break;
2118 /* Only search for unlisted buffers if there was no match with
2119 * a listed buffer. */
2120 if (!unlisted || !find_listed || match != -1)
2121 break;
2122 find_listed = FALSE;
2125 vim_free(pat);
2128 if (match == -2)
2129 EMSG2(_("E93: More than one match for %s"), pattern);
2130 else if (match < 0)
2131 EMSG2(_("E94: No matching buffer for %s"), pattern);
2132 return match;
2134 #endif
2136 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2139 * Find all buffer names that match.
2140 * For command line expansion of ":buf" and ":sbuf".
2141 * Return OK if matches found, FAIL otherwise.
2144 ExpandBufnames(pat, num_file, file, options)
2145 char_u *pat;
2146 int *num_file;
2147 char_u ***file;
2148 int options;
2150 int count = 0;
2151 buf_T *buf;
2152 int round;
2153 char_u *p;
2154 int attempt;
2155 regprog_T *prog;
2156 char_u *patc;
2158 *num_file = 0; /* return values in case of FAIL */
2159 *file = NULL;
2161 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2162 if (*pat == '^')
2164 patc = alloc((unsigned)STRLEN(pat) + 11);
2165 if (patc == NULL)
2166 return FAIL;
2167 STRCPY(patc, "\\(^\\|[\\/]\\)");
2168 STRCPY(patc + 11, pat + 1);
2170 else
2171 patc = pat;
2174 * attempt == 0: try match with '\<', match at start of word
2175 * attempt == 1: try match without '\<', match anywhere
2177 for (attempt = 0; attempt <= 1; ++attempt)
2179 if (attempt > 0 && patc == pat)
2180 break; /* there was no anchor, no need to try again */
2181 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2182 if (prog == NULL)
2184 if (patc != pat)
2185 vim_free(patc);
2186 return FAIL;
2190 * round == 1: Count the matches.
2191 * round == 2: Build the array to keep the matches.
2193 for (round = 1; round <= 2; ++round)
2195 count = 0;
2196 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2198 if (!buf->b_p_bl) /* skip unlisted buffers */
2199 continue;
2200 p = buflist_match(prog, buf);
2201 if (p != NULL)
2203 if (round == 1)
2204 ++count;
2205 else
2207 if (options & WILD_HOME_REPLACE)
2208 p = home_replace_save(buf, p);
2209 else
2210 p = vim_strsave(p);
2211 (*file)[count++] = p;
2215 if (count == 0) /* no match found, break here */
2216 break;
2217 if (round == 1)
2219 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2220 if (*file == NULL)
2222 vim_free(prog);
2223 if (patc != pat)
2224 vim_free(patc);
2225 return FAIL;
2229 vim_free(prog);
2230 if (count) /* match(es) found, break here */
2231 break;
2234 if (patc != pat)
2235 vim_free(patc);
2237 *num_file = count;
2238 return (count == 0 ? FAIL : OK);
2241 #endif /* FEAT_CMDL_COMPL */
2243 #ifdef HAVE_BUFLIST_MATCH
2245 * Check for a match on the file name for buffer "buf" with regprog "prog".
2247 static char_u *
2248 buflist_match(prog, buf)
2249 regprog_T *prog;
2250 buf_T *buf;
2252 char_u *match;
2254 /* First try the short file name, then the long file name. */
2255 match = fname_match(prog, buf->b_sfname);
2256 if (match == NULL)
2257 match = fname_match(prog, buf->b_ffname);
2259 return match;
2263 * Try matching the regexp in "prog" with file name "name".
2264 * Return "name" when there is a match, NULL when not.
2266 static char_u *
2267 fname_match(prog, name)
2268 regprog_T *prog;
2269 char_u *name;
2271 char_u *match = NULL;
2272 char_u *p;
2273 regmatch_T regmatch;
2275 if (name != NULL)
2277 regmatch.regprog = prog;
2278 #ifdef CASE_INSENSITIVE_FILENAME
2279 regmatch.rm_ic = TRUE; /* Always ignore case */
2280 #else
2281 regmatch.rm_ic = FALSE; /* Never ignore case */
2282 #endif
2284 if (vim_regexec(&regmatch, name, (colnr_T)0))
2285 match = name;
2286 else
2288 /* Replace $(HOME) with '~' and try matching again. */
2289 p = home_replace_save(NULL, name);
2290 if (p != NULL && vim_regexec(&regmatch, p, (colnr_T)0))
2291 match = name;
2292 vim_free(p);
2296 return match;
2298 #endif
2301 * find file in buffer list by number
2303 buf_T *
2304 buflist_findnr(nr)
2305 int nr;
2307 buf_T *buf;
2309 if (nr == 0)
2310 nr = curwin->w_alt_fnum;
2311 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2312 if (buf->b_fnum == nr)
2313 return (buf);
2314 return NULL;
2318 * Get name of file 'n' in the buffer list.
2319 * When the file has no name an empty string is returned.
2320 * home_replace() is used to shorten the file name (used for marks).
2321 * Returns a pointer to allocated memory, of NULL when failed.
2323 char_u *
2324 buflist_nr2name(n, fullname, helptail)
2325 int n;
2326 int fullname;
2327 int helptail; /* for help buffers return tail only */
2329 buf_T *buf;
2331 buf = buflist_findnr(n);
2332 if (buf == NULL)
2333 return NULL;
2334 return home_replace_save(helptail ? buf : NULL,
2335 fullname ? buf->b_ffname : buf->b_fname);
2339 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2340 * When "copy_options" is TRUE save the local window option values.
2341 * When "lnum" is 0 only do the options.
2343 static void
2344 buflist_setfpos(buf, win, lnum, col, copy_options)
2345 buf_T *buf;
2346 win_T *win;
2347 linenr_T lnum;
2348 colnr_T col;
2349 int copy_options;
2351 wininfo_T *wip;
2353 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2354 if (wip->wi_win == win)
2355 break;
2356 if (wip == NULL)
2358 /* allocate a new entry */
2359 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2360 if (wip == NULL)
2361 return;
2362 wip->wi_win = win;
2363 if (lnum == 0) /* set lnum even when it's 0 */
2364 lnum = 1;
2366 else
2368 /* remove the entry from the list */
2369 if (wip->wi_prev)
2370 wip->wi_prev->wi_next = wip->wi_next;
2371 else
2372 buf->b_wininfo = wip->wi_next;
2373 if (wip->wi_next)
2374 wip->wi_next->wi_prev = wip->wi_prev;
2375 if (copy_options && wip->wi_optset)
2377 clear_winopt(&wip->wi_opt);
2378 #ifdef FEAT_FOLDING
2379 deleteFoldRecurse(&wip->wi_folds);
2380 #endif
2383 if (lnum != 0)
2385 wip->wi_fpos.lnum = lnum;
2386 wip->wi_fpos.col = col;
2388 if (copy_options)
2390 /* Save the window-specific option values. */
2391 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2392 #ifdef FEAT_FOLDING
2393 wip->wi_fold_manual = win->w_fold_manual;
2394 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2395 #endif
2396 wip->wi_optset = TRUE;
2399 /* insert the entry in front of the list */
2400 wip->wi_next = buf->b_wininfo;
2401 buf->b_wininfo = wip;
2402 wip->wi_prev = NULL;
2403 if (wip->wi_next)
2404 wip->wi_next->wi_prev = wip;
2406 return;
2409 #ifdef FEAT_DIFF
2410 static int wininfo_other_tab_diff __ARGS((wininfo_T *wip));
2413 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2414 * page. That's because a diff is local to a tab page.
2416 static int
2417 wininfo_other_tab_diff(wip)
2418 wininfo_T *wip;
2420 win_T *wp;
2422 if (wip->wi_opt.wo_diff)
2424 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2425 /* return FALSE when it's a window in the current tab page, thus
2426 * the buffer was in diff mode here */
2427 if (wip->wi_win == wp)
2428 return FALSE;
2429 return TRUE;
2431 return FALSE;
2433 #endif
2436 * Find info for the current window in buffer "buf".
2437 * If not found, return the info for the most recently used window.
2438 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2439 * another tab page.
2440 * Returns NULL when there isn't any info.
2442 /*ARGSUSED*/
2443 static wininfo_T *
2444 find_wininfo(buf, skip_diff_buffer)
2445 buf_T *buf;
2446 int skip_diff_buffer;
2448 wininfo_T *wip;
2450 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2451 if (wip->wi_win == curwin
2452 #ifdef FEAT_DIFF
2453 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2454 #endif
2456 break;
2458 /* If no wininfo for curwin, use the first in the list (that doesn't have
2459 * 'diff' set and is in another tab page). */
2460 if (wip == NULL)
2462 #ifdef FEAT_DIFF
2463 if (skip_diff_buffer)
2465 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2466 if (!wininfo_other_tab_diff(wip))
2467 break;
2469 else
2470 #endif
2471 wip = buf->b_wininfo;
2473 return wip;
2477 * Reset the local window options to the values last used in this window.
2478 * If the buffer wasn't used in this window before, use the values from
2479 * the most recently used window. If the values were never set, use the
2480 * global values for the window.
2482 void
2483 get_winopts(buf)
2484 buf_T *buf;
2486 wininfo_T *wip;
2488 clear_winopt(&curwin->w_onebuf_opt);
2489 #ifdef FEAT_FOLDING
2490 clearFolding(curwin);
2491 #endif
2493 wip = find_wininfo(buf, TRUE);
2494 if (wip != NULL && wip->wi_optset)
2496 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2497 #ifdef FEAT_FOLDING
2498 curwin->w_fold_manual = wip->wi_fold_manual;
2499 curwin->w_foldinvalid = TRUE;
2500 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2501 #endif
2503 else
2504 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2506 #ifdef FEAT_FOLDING
2507 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2508 if (p_fdls >= 0)
2509 curwin->w_p_fdl = p_fdls;
2510 #endif
2514 * Find the position (lnum and col) for the buffer 'buf' for the current
2515 * window.
2516 * Returns a pointer to no_position if no position is found.
2518 pos_T *
2519 buflist_findfpos(buf)
2520 buf_T *buf;
2522 wininfo_T *wip;
2523 static pos_T no_position = {1, 0};
2525 wip = find_wininfo(buf, FALSE);
2526 if (wip != NULL)
2527 return &(wip->wi_fpos);
2528 else
2529 return &no_position;
2533 * Find the lnum for the buffer 'buf' for the current window.
2535 linenr_T
2536 buflist_findlnum(buf)
2537 buf_T *buf;
2539 return buflist_findfpos(buf)->lnum;
2542 #if defined(FEAT_LISTCMDS) || defined(PROTO)
2544 * List all know file names (for :files and :buffers command).
2546 /*ARGSUSED*/
2547 void
2548 buflist_list(eap)
2549 exarg_T *eap;
2551 buf_T *buf;
2552 int len;
2553 int i;
2555 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2557 /* skip unlisted buffers, unless ! was used */
2558 if (!buf->b_p_bl && !eap->forceit)
2559 continue;
2560 msg_putchar('\n');
2561 if (buf_spname(buf) != NULL)
2562 STRCPY(NameBuff, buf_spname(buf));
2563 else
2564 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2566 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
2567 buf->b_fnum,
2568 buf->b_p_bl ? ' ' : 'u',
2569 buf == curbuf ? '%' :
2570 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2571 buf->b_ml.ml_mfp == NULL ? ' ' :
2572 (buf->b_nwindows == 0 ? 'h' : 'a'),
2573 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2574 (buf->b_flags & BF_READERR) ? 'x'
2575 : (bufIsChanged(buf) ? '+' : ' '),
2576 NameBuff);
2578 /* put "line 999" in column 40 or after the file name */
2579 i = 40 - vim_strsize(IObuff);
2582 IObuff[len++] = ' ';
2583 } while (--i > 0 && len < IOSIZE - 18);
2584 vim_snprintf((char *)IObuff + len, IOSIZE - len, _("line %ld"),
2585 buf == curbuf ? curwin->w_cursor.lnum
2586 : (long)buflist_findlnum(buf));
2587 msg_outtrans(IObuff);
2588 out_flush(); /* output one line at a time */
2589 ui_breakcheck();
2592 #endif
2595 * Get file name and line number for file 'fnum'.
2596 * Used by DoOneCmd() for translating '%' and '#'.
2597 * Used by insert_reg() and cmdline_paste() for '#' register.
2598 * Return FAIL if not found, OK for success.
2601 buflist_name_nr(fnum, fname, lnum)
2602 int fnum;
2603 char_u **fname;
2604 linenr_T *lnum;
2606 buf_T *buf;
2608 buf = buflist_findnr(fnum);
2609 if (buf == NULL || buf->b_fname == NULL)
2610 return FAIL;
2612 *fname = buf->b_fname;
2613 *lnum = buflist_findlnum(buf);
2615 return OK;
2619 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2620 * The file name with the full path is also remembered, for when :cd is used.
2621 * Returns FAIL for failure (file name already in use by other buffer)
2622 * OK otherwise.
2625 setfname(buf, ffname, sfname, message)
2626 buf_T *buf;
2627 char_u *ffname, *sfname;
2628 int message; /* give message when buffer already exists */
2630 buf_T *obuf = NULL;
2631 #ifdef UNIX
2632 struct stat st;
2633 #endif
2635 if (ffname == NULL || *ffname == NUL)
2637 /* Removing the name. */
2638 vim_free(buf->b_ffname);
2639 vim_free(buf->b_sfname);
2640 buf->b_ffname = NULL;
2641 buf->b_sfname = NULL;
2642 #ifdef UNIX
2643 st.st_dev = (dev_T)-1;
2644 #endif
2646 else
2648 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2649 if (ffname == NULL) /* out of memory */
2650 return FAIL;
2653 * if the file name is already used in another buffer:
2654 * - if the buffer is loaded, fail
2655 * - if the buffer is not loaded, delete it from the list
2657 #ifdef UNIX
2658 if (mch_stat((char *)ffname, &st) < 0)
2659 st.st_dev = (dev_T)-1;
2660 #endif
2661 if (!(buf->b_flags & BF_DUMMY))
2662 #ifdef UNIX
2663 obuf = buflist_findname_stat(ffname, &st);
2664 #else
2665 obuf = buflist_findname(ffname);
2666 #endif
2667 if (obuf != NULL && obuf != buf)
2669 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2671 if (message)
2672 EMSG(_("E95: Buffer with this name already exists"));
2673 vim_free(ffname);
2674 return FAIL;
2676 close_buffer(NULL, obuf, DOBUF_WIPE); /* delete from the list */
2678 sfname = vim_strsave(sfname);
2679 if (ffname == NULL || sfname == NULL)
2681 vim_free(sfname);
2682 vim_free(ffname);
2683 return FAIL;
2685 #ifdef USE_FNAME_CASE
2686 # ifdef USE_LONG_FNAME
2687 if (USE_LONG_FNAME)
2688 # endif
2689 fname_case(sfname, 0); /* set correct case for short file name */
2690 #endif
2691 vim_free(buf->b_ffname);
2692 vim_free(buf->b_sfname);
2693 buf->b_ffname = ffname;
2694 buf->b_sfname = sfname;
2696 buf->b_fname = buf->b_sfname;
2697 #ifdef UNIX
2698 if (st.st_dev == (dev_T)-1)
2699 buf->b_dev = -1;
2700 else
2702 buf->b_dev = st.st_dev;
2703 buf->b_ino = st.st_ino;
2705 #endif
2707 #ifndef SHORT_FNAME
2708 buf->b_shortname = FALSE;
2709 #endif
2711 buf_name_changed(buf);
2712 return OK;
2716 * Crude way of changing the name of a buffer. Use with care!
2717 * The name should be relative to the current directory.
2719 void
2720 buf_set_name(fnum, name)
2721 int fnum;
2722 char_u *name;
2724 buf_T *buf;
2726 buf = buflist_findnr(fnum);
2727 if (buf != NULL)
2729 vim_free(buf->b_sfname);
2730 vim_free(buf->b_ffname);
2731 buf->b_ffname = vim_strsave(name);
2732 buf->b_sfname = NULL;
2733 /* Allocate ffname and expand into full path. Also resolves .lnk
2734 * files on Win32. */
2735 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
2736 buf->b_fname = buf->b_sfname;
2741 * Take care of what needs to be done when the name of buffer "buf" has
2742 * changed.
2744 void
2745 buf_name_changed(buf)
2746 buf_T *buf;
2749 * If the file name changed, also change the name of the swapfile
2751 if (buf->b_ml.ml_mfp != NULL)
2752 ml_setname(buf);
2754 if (curwin->w_buffer == buf)
2755 check_arg_idx(curwin); /* check file name for arg list */
2756 #ifdef FEAT_TITLE
2757 maketitle(); /* set window title */
2758 #endif
2759 #ifdef FEAT_WINDOWS
2760 status_redraw_all(); /* status lines need to be redrawn */
2761 #endif
2762 fmarks_check_names(buf); /* check named file marks */
2763 ml_timestamp(buf); /* reset timestamp */
2767 * set alternate file name for current window
2769 * Used by do_one_cmd(), do_write() and do_ecmd().
2770 * Return the buffer.
2772 buf_T *
2773 setaltfname(ffname, sfname, lnum)
2774 char_u *ffname;
2775 char_u *sfname;
2776 linenr_T lnum;
2778 buf_T *buf;
2780 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2781 buf = buflist_new(ffname, sfname, lnum, 0);
2782 if (buf != NULL && !cmdmod.keepalt)
2783 curwin->w_alt_fnum = buf->b_fnum;
2784 return buf;
2788 * Get alternate file name for current window.
2789 * Return NULL if there isn't any, and give error message if requested.
2791 char_u *
2792 getaltfname(errmsg)
2793 int errmsg; /* give error message */
2795 char_u *fname;
2796 linenr_T dummy;
2798 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2800 if (errmsg)
2801 EMSG(_(e_noalt));
2802 return NULL;
2804 return fname;
2808 * Add a file name to the buflist and return its number.
2809 * Uses same flags as buflist_new(), except BLN_DUMMY.
2811 * used by qf_init(), main() and doarglist()
2814 buflist_add(fname, flags)
2815 char_u *fname;
2816 int flags;
2818 buf_T *buf;
2820 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
2821 if (buf != NULL)
2822 return buf->b_fnum;
2823 return 0;
2826 #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2828 * Adjust slashes in file names. Called after 'shellslash' was set.
2830 void
2831 buflist_slash_adjust()
2833 buf_T *bp;
2835 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
2837 if (bp->b_ffname != NULL)
2838 slash_adjust(bp->b_ffname);
2839 if (bp->b_sfname != NULL)
2840 slash_adjust(bp->b_sfname);
2843 #endif
2846 * Set alternate cursor position for the current buffer and window "win".
2847 * Also save the local window option values.
2849 void
2850 buflist_altfpos(win)
2851 win_T *win;
2853 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
2857 * Return TRUE if 'ffname' is not the same file as current file.
2858 * Fname must have a full path (expanded by mch_FullName()).
2861 otherfile(ffname)
2862 char_u *ffname;
2864 return otherfile_buf(curbuf, ffname
2865 #ifdef UNIX
2866 , NULL
2867 #endif
2871 static int
2872 otherfile_buf(buf, ffname
2873 #ifdef UNIX
2874 , stp
2875 #endif
2877 buf_T *buf;
2878 char_u *ffname;
2879 #ifdef UNIX
2880 struct stat *stp;
2881 #endif
2883 /* no name is different */
2884 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
2885 return TRUE;
2886 if (fnamecmp(ffname, buf->b_ffname) == 0)
2887 return FALSE;
2888 #ifdef UNIX
2890 struct stat st;
2892 /* If no struct stat given, get it now */
2893 if (stp == NULL)
2895 if (buf->b_dev < 0 || mch_stat((char *)ffname, &st) < 0)
2896 st.st_dev = (dev_T)-1;
2897 stp = &st;
2899 /* Use dev/ino to check if the files are the same, even when the names
2900 * are different (possible with links). Still need to compare the
2901 * name above, for when the file doesn't exist yet.
2902 * Problem: The dev/ino changes when a file is deleted (and created
2903 * again) and remains the same when renamed/moved. We don't want to
2904 * mch_stat() each buffer each time, that would be too slow. Get the
2905 * dev/ino again when they appear to match, but not when they appear
2906 * to be different: Could skip a buffer when it's actually the same
2907 * file. */
2908 if (buf_same_ino(buf, stp))
2910 buf_setino(buf);
2911 if (buf_same_ino(buf, stp))
2912 return FALSE;
2915 #endif
2916 return TRUE;
2919 #if defined(UNIX) || defined(PROTO)
2921 * Set inode and device number for a buffer.
2922 * Must always be called when b_fname is changed!.
2924 void
2925 buf_setino(buf)
2926 buf_T *buf;
2928 struct stat st;
2930 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
2932 buf->b_dev = st.st_dev;
2933 buf->b_ino = st.st_ino;
2935 else
2936 buf->b_dev = -1;
2940 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
2942 static int
2943 buf_same_ino(buf, stp)
2944 buf_T *buf;
2945 struct stat *stp;
2947 return (buf->b_dev >= 0
2948 && stp->st_dev == buf->b_dev
2949 && stp->st_ino == buf->b_ino);
2951 #endif
2954 * Print info about the current buffer.
2956 void
2957 fileinfo(fullname, shorthelp, dont_truncate)
2958 int fullname; /* when non-zero print full path */
2959 int shorthelp;
2960 int dont_truncate;
2962 char_u *name;
2963 int n;
2964 char_u *p;
2965 char_u *buffer;
2966 size_t len;
2968 buffer = alloc(IOSIZE);
2969 if (buffer == NULL)
2970 return;
2972 if (fullname > 1) /* 2 CTRL-G: include buffer number */
2974 sprintf((char *)buffer, "buf %d: ", curbuf->b_fnum);
2975 p = buffer + STRLEN(buffer);
2977 else
2978 p = buffer;
2980 *p++ = '"';
2981 if (buf_spname(curbuf) != NULL)
2982 STRCPY(p, buf_spname(curbuf));
2983 else
2985 if (!fullname && curbuf->b_fname != NULL)
2986 name = curbuf->b_fname;
2987 else
2988 name = curbuf->b_ffname;
2989 home_replace(shorthelp ? curbuf : NULL, name, p,
2990 (int)(IOSIZE - (p - buffer)), TRUE);
2993 len = STRLEN(buffer);
2994 vim_snprintf((char *)buffer + len, IOSIZE - len,
2995 "\"%s%s%s%s%s%s",
2996 curbufIsChanged() ? (shortmess(SHM_MOD)
2997 ? " [+]" : _(" [Modified]")) : " ",
2998 (curbuf->b_flags & BF_NOTEDITED)
2999 #ifdef FEAT_QUICKFIX
3000 && !bt_dontwrite(curbuf)
3001 #endif
3002 ? _("[Not edited]") : "",
3003 (curbuf->b_flags & BF_NEW)
3004 #ifdef FEAT_QUICKFIX
3005 && !bt_dontwrite(curbuf)
3006 #endif
3007 ? _("[New file]") : "",
3008 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
3009 curbuf->b_p_ro ? (shortmess(SHM_RO) ? "[RO]"
3010 : _("[readonly]")) : "",
3011 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3012 || curbuf->b_p_ro) ?
3013 " " : "");
3014 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3015 * causes an overflow, thus for large numbers divide instead. */
3016 if (curwin->w_cursor.lnum > 1000000L)
3017 n = (int)(((long)curwin->w_cursor.lnum) /
3018 ((long)curbuf->b_ml.ml_line_count / 100L));
3019 else
3020 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3021 (long)curbuf->b_ml.ml_line_count);
3022 len = STRLEN(buffer);
3023 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3025 vim_snprintf((char *)buffer + len, IOSIZE - len, "%s", _(no_lines_msg));
3027 #ifdef FEAT_CMDL_INFO
3028 else if (p_ru)
3030 /* Current line and column are already on the screen -- webb */
3031 if (curbuf->b_ml.ml_line_count == 1)
3032 vim_snprintf((char *)buffer + len, IOSIZE - len,
3033 _("1 line --%d%%--"), n);
3034 else
3035 vim_snprintf((char *)buffer + len, IOSIZE - len,
3036 _("%ld lines --%d%%--"),
3037 (long)curbuf->b_ml.ml_line_count, n);
3039 #endif
3040 else
3042 vim_snprintf((char *)buffer + len, IOSIZE - len,
3043 _("line %ld of %ld --%d%%-- col "),
3044 (long)curwin->w_cursor.lnum,
3045 (long)curbuf->b_ml.ml_line_count,
3047 validate_virtcol();
3048 col_print(buffer + STRLEN(buffer),
3049 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3052 (void)append_arg_number(curwin, buffer, !shortmess(SHM_FILE), IOSIZE);
3054 if (dont_truncate)
3056 /* Temporarily set msg_scroll to avoid the message being truncated.
3057 * First call msg_start() to get the message in the right place. */
3058 msg_start();
3059 n = msg_scroll;
3060 msg_scroll = TRUE;
3061 msg(buffer);
3062 msg_scroll = n;
3064 else
3066 p = msg_trunc_attr(buffer, FALSE, 0);
3067 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
3068 /* Need to repeat the message after redrawing when:
3069 * - When restart_edit is set (otherwise there will be a delay
3070 * before redrawing).
3071 * - When the screen was scrolled but there is no wait-return
3072 * prompt. */
3073 set_keep_msg(p, 0);
3076 vim_free(buffer);
3079 void
3080 col_print(buf, col, vcol)
3081 char_u *buf;
3082 int col;
3083 int vcol;
3085 if (col == vcol)
3086 sprintf((char *)buf, "%d", col);
3087 else
3088 sprintf((char *)buf, "%d-%d", col, vcol);
3091 #if defined(FEAT_TITLE) || defined(PROTO)
3093 * put file name in title bar of window and in icon title
3096 static char_u *lasttitle = NULL;
3097 static char_u *lasticon = NULL;
3099 void
3100 maketitle()
3102 char_u *p;
3103 char_u *t_str = NULL;
3104 char_u *i_name;
3105 char_u *i_str = NULL;
3106 int maxlen = 0;
3107 int len;
3108 int mustset;
3109 char_u buf[IOSIZE];
3110 int off;
3112 if (!redrawing())
3114 /* Postpone updating the title when 'lazyredraw' is set. */
3115 need_maketitle = TRUE;
3116 return;
3119 #ifdef FEAT_GUI_MACVIM
3120 gui_macvim_update_modified_flag();
3121 #endif
3123 need_maketitle = FALSE;
3124 if (!p_title && !p_icon)
3125 return;
3127 if (p_title)
3129 if (p_titlelen > 0)
3131 maxlen = p_titlelen * Columns / 100;
3132 if (maxlen < 10)
3133 maxlen = 10;
3136 t_str = buf;
3137 if (*p_titlestring != NUL)
3139 #ifdef FEAT_STL_OPT
3140 if (stl_syntax & STL_IN_TITLE)
3142 int use_sandbox = FALSE;
3143 int save_called_emsg = called_emsg;
3145 # ifdef FEAT_EVAL
3146 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
3147 # endif
3148 called_emsg = FALSE;
3149 build_stl_str_hl(curwin, t_str, sizeof(buf),
3150 p_titlestring, use_sandbox,
3151 0, maxlen, NULL, NULL);
3152 if (called_emsg)
3153 set_string_option_direct((char_u *)"titlestring", -1,
3154 (char_u *)"", OPT_FREE, SID_ERROR);
3155 called_emsg |= save_called_emsg;
3157 else
3158 #endif
3159 t_str = p_titlestring;
3161 else
3163 /* format: "fname + (path) (1 of 2) - VIM" */
3165 if (curbuf->b_fname == NULL)
3166 STRCPY(buf, _("[No Name]"));
3167 else
3169 p = transstr(gettail(curbuf->b_fname));
3170 vim_strncpy(buf, p, IOSIZE - 100);
3171 vim_free(p);
3174 switch (bufIsChanged(curbuf)
3175 + (curbuf->b_p_ro * 2)
3176 + (!curbuf->b_p_ma * 4))
3178 case 1: STRCAT(buf, " +"); break;
3179 case 2: STRCAT(buf, " ="); break;
3180 case 3: STRCAT(buf, " =+"); break;
3181 case 4:
3182 case 6: STRCAT(buf, " -"); break;
3183 case 5:
3184 case 7: STRCAT(buf, " -+"); break;
3187 if (curbuf->b_fname != NULL)
3189 /* Get path of file, replace home dir with ~ */
3190 off = (int)STRLEN(buf);
3191 buf[off++] = ' ';
3192 buf[off++] = '(';
3193 home_replace(curbuf, curbuf->b_ffname,
3194 buf + off, IOSIZE - off, TRUE);
3195 #ifdef BACKSLASH_IN_FILENAME
3196 /* avoid "c:/name" to be reduced to "c" */
3197 if (isalpha(buf[off]) && buf[off + 1] == ':')
3198 off += 2;
3199 #endif
3200 /* remove the file name */
3201 p = gettail_sep(buf + off);
3202 if (p == buf + off)
3203 /* must be a help buffer */
3204 vim_strncpy(buf + off, (char_u *)_("help"),
3205 IOSIZE - off - 1);
3206 else
3207 *p = NUL;
3209 /* translate unprintable chars */
3210 p = transstr(buf + off);
3211 vim_strncpy(buf + off, p, IOSIZE - off - 1);
3212 vim_free(p);
3213 STRCAT(buf, ")");
3216 #ifndef FEAT_GUI_MACVIM
3217 append_arg_number(curwin, buf, FALSE, IOSIZE);
3218 #endif
3220 #if defined(FEAT_CLIENTSERVER)
3221 if (serverName != NULL)
3223 STRCAT(buf, " - ");
3224 STRCAT(buf, serverName);
3226 else
3227 #endif
3228 STRCAT(buf, " - VIM");
3230 if (maxlen > 0)
3232 /* make it shorter by removing a bit in the middle */
3233 len = vim_strsize(buf);
3234 if (len > maxlen)
3235 trunc_string(buf, buf, maxlen);
3239 mustset = ti_change(t_str, &lasttitle);
3241 if (p_icon)
3243 i_str = buf;
3244 if (*p_iconstring != NUL)
3246 #ifdef FEAT_STL_OPT
3247 if (stl_syntax & STL_IN_ICON)
3249 int use_sandbox = FALSE;
3250 int save_called_emsg = called_emsg;
3252 # ifdef FEAT_EVAL
3253 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
3254 # endif
3255 called_emsg = FALSE;
3256 build_stl_str_hl(curwin, i_str, sizeof(buf),
3257 p_iconstring, use_sandbox,
3258 0, 0, NULL, NULL);
3259 if (called_emsg)
3260 set_string_option_direct((char_u *)"iconstring", -1,
3261 (char_u *)"", OPT_FREE, SID_ERROR);
3262 called_emsg |= save_called_emsg;
3264 else
3265 #endif
3266 i_str = p_iconstring;
3268 else
3270 if (buf_spname(curbuf) != NULL)
3271 i_name = (char_u *)buf_spname(curbuf);
3272 else /* use file name only in icon */
3273 i_name = gettail(curbuf->b_ffname);
3274 *i_str = NUL;
3275 /* Truncate name at 100 bytes. */
3276 len = (int)STRLEN(i_name);
3277 if (len > 100)
3279 len -= 100;
3280 #ifdef FEAT_MBYTE
3281 if (has_mbyte)
3282 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3283 #endif
3284 i_name += len;
3286 STRCPY(i_str, i_name);
3287 trans_characters(i_str, IOSIZE);
3291 mustset |= ti_change(i_str, &lasticon);
3293 if (mustset)
3294 resettitle();
3298 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3299 * from "str" if it does.
3300 * Return TRUE when "*last" changed.
3302 static int
3303 ti_change(str, last)
3304 char_u *str;
3305 char_u **last;
3307 if ((str == NULL) != (*last == NULL)
3308 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3310 vim_free(*last);
3311 if (str == NULL)
3312 *last = NULL;
3313 else
3314 *last = vim_strsave(str);
3315 return TRUE;
3317 return FALSE;
3321 * Put current window title back (used after calling a shell)
3323 void
3324 resettitle()
3326 mch_settitle(lasttitle, lasticon);
3329 # if defined(EXITFREE) || defined(PROTO)
3330 void
3331 free_titles()
3333 vim_free(lasttitle);
3334 vim_free(lasticon);
3336 # endif
3338 #endif /* FEAT_TITLE */
3340 #if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
3342 * Build a string from the status line items in "fmt".
3343 * Return length of string in screen cells.
3345 * Normally works for window "wp", except when working for 'tabline' then it
3346 * is "curwin".
3348 * Items are drawn interspersed with the text that surrounds it
3349 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3350 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3352 * If maxwidth is not zero, the string will be filled at any middle marker
3353 * or truncated if too long, fillchar is used for all whitespace.
3355 /*ARGSUSED*/
3357 build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar, maxwidth, hltab, tabtab)
3358 win_T *wp;
3359 char_u *out; /* buffer to write into != NameBuff */
3360 size_t outlen; /* length of out[] */
3361 char_u *fmt;
3362 int use_sandbox; /* "fmt" was set insecurely, use sandbox */
3363 int fillchar;
3364 int maxwidth;
3365 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3366 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
3368 char_u *p;
3369 char_u *s;
3370 char_u *t;
3371 char_u *linecont;
3372 #ifdef FEAT_EVAL
3373 win_T *o_curwin;
3374 buf_T *o_curbuf;
3375 #endif
3376 int empty_line;
3377 colnr_T virtcol;
3378 long l;
3379 long n;
3380 int prevchar_isflag;
3381 int prevchar_isitem;
3382 int itemisflag;
3383 int fillable;
3384 char_u *str;
3385 long num;
3386 int width;
3387 int itemcnt;
3388 int curitem;
3389 int groupitem[STL_MAX_ITEM];
3390 int groupdepth;
3391 struct stl_item
3393 char_u *start;
3394 int minwid;
3395 int maxwid;
3396 enum
3398 Normal,
3399 Empty,
3400 Group,
3401 Middle,
3402 Highlight,
3403 TabPage,
3404 Trunc
3405 } type;
3406 } item[STL_MAX_ITEM];
3407 int minwid;
3408 int maxwid;
3409 int zeropad;
3410 char_u base;
3411 char_u opt;
3412 #define TMPLEN 70
3413 char_u tmp[TMPLEN];
3414 char_u *usefmt = fmt;
3415 struct stl_hlrec *sp;
3417 #ifdef FEAT_EVAL
3419 * When the format starts with "%!" then evaluate it as an expression and
3420 * use the result as the actual format string.
3422 if (fmt[0] == '%' && fmt[1] == '!')
3424 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3425 if (usefmt == NULL)
3426 usefmt = fmt;
3428 #endif
3430 if (fillchar == 0)
3431 fillchar = ' ';
3432 #ifdef FEAT_MBYTE
3433 /* Can't handle a multi-byte fill character yet. */
3434 else if (mb_char2len(fillchar) > 1)
3435 fillchar = '-';
3436 #endif
3439 * Get line & check if empty (cursorpos will show "0-1").
3440 * If inversion is possible we use it. Else '=' characters are used.
3442 linecont = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3443 empty_line = (*linecont == NUL);
3445 groupdepth = 0;
3446 p = out;
3447 curitem = 0;
3448 prevchar_isflag = TRUE;
3449 prevchar_isitem = FALSE;
3450 for (s = usefmt; *s; )
3452 if (*s != NUL && *s != '%')
3453 prevchar_isflag = prevchar_isitem = FALSE;
3456 * Handle up to the next '%' or the end.
3458 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3459 *p++ = *s++;
3460 if (*s == NUL || p + 1 >= out + outlen)
3461 break;
3464 * Handle one '%' item.
3466 s++;
3467 if (*s == '%')
3469 if (p + 1 >= out + outlen)
3470 break;
3471 *p++ = *s++;
3472 prevchar_isflag = prevchar_isitem = FALSE;
3473 continue;
3475 if (*s == STL_MIDDLEMARK)
3477 s++;
3478 if (groupdepth > 0)
3479 continue;
3480 item[curitem].type = Middle;
3481 item[curitem++].start = p;
3482 continue;
3484 if (*s == STL_TRUNCMARK)
3486 s++;
3487 item[curitem].type = Trunc;
3488 item[curitem++].start = p;
3489 continue;
3491 if (*s == ')')
3493 s++;
3494 if (groupdepth < 1)
3495 continue;
3496 groupdepth--;
3498 t = item[groupitem[groupdepth]].start;
3499 *p = NUL;
3500 l = vim_strsize(t);
3501 if (curitem > groupitem[groupdepth] + 1
3502 && item[groupitem[groupdepth]].minwid == 0)
3504 /* remove group if all items are empty */
3505 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3506 if (item[n].type == Normal)
3507 break;
3508 if (n == curitem)
3510 p = t;
3511 l = 0;
3514 if (l > item[groupitem[groupdepth]].maxwid)
3516 /* truncate, remove n bytes of text at the start */
3517 #ifdef FEAT_MBYTE
3518 if (has_mbyte)
3520 /* Find the first character that should be included. */
3521 n = 0;
3522 while (l >= item[groupitem[groupdepth]].maxwid)
3524 l -= ptr2cells(t + n);
3525 n += (*mb_ptr2len)(t + n);
3528 else
3529 #endif
3530 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
3532 *t = '<';
3533 mch_memmove(t + 1, t + n, p - (t + n));
3534 p = p - n + 1;
3535 #ifdef FEAT_MBYTE
3536 /* Fill up space left over by half a double-wide char. */
3537 while (++l < item[groupitem[groupdepth]].minwid)
3538 *p++ = fillchar;
3539 #endif
3541 /* correct the start of the items for the truncation */
3542 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3544 item[l].start -= n;
3545 if (item[l].start < t)
3546 item[l].start = t;
3549 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3551 /* fill */
3552 n = item[groupitem[groupdepth]].minwid;
3553 if (n < 0)
3555 /* fill by appending characters */
3556 n = 0 - n;
3557 while (l++ < n && p + 1 < out + outlen)
3558 *p++ = fillchar;
3560 else
3562 /* fill by inserting characters */
3563 mch_memmove(t + n - l, t, p - t);
3564 l = n - l;
3565 if (p + l >= out + outlen)
3566 l = (long)((out + outlen) - p - 1);
3567 p += l;
3568 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3569 item[n].start += l;
3570 for ( ; l > 0; l--)
3571 *t++ = fillchar;
3574 continue;
3576 minwid = 0;
3577 maxwid = 9999;
3578 zeropad = FALSE;
3579 l = 1;
3580 if (*s == '0')
3582 s++;
3583 zeropad = TRUE;
3585 if (*s == '-')
3587 s++;
3588 l = -1;
3590 if (VIM_ISDIGIT(*s))
3592 minwid = (int)getdigits(&s);
3593 if (minwid < 0) /* overflow */
3594 minwid = 0;
3596 if (*s == STL_USER_HL)
3598 item[curitem].type = Highlight;
3599 item[curitem].start = p;
3600 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3601 s++;
3602 curitem++;
3603 continue;
3605 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3607 if (*s == STL_TABCLOSENR)
3609 if (minwid == 0)
3611 /* %X ends the close label, go back to the previously
3612 * define tab label nr. */
3613 for (n = curitem - 1; n >= 0; --n)
3614 if (item[n].type == TabPage && item[n].minwid >= 0)
3616 minwid = item[n].minwid;
3617 break;
3620 else
3621 /* close nrs are stored as negative values */
3622 minwid = - minwid;
3624 item[curitem].type = TabPage;
3625 item[curitem].start = p;
3626 item[curitem].minwid = minwid;
3627 s++;
3628 curitem++;
3629 continue;
3631 if (*s == '.')
3633 s++;
3634 if (VIM_ISDIGIT(*s))
3636 maxwid = (int)getdigits(&s);
3637 if (maxwid <= 0) /* overflow */
3638 maxwid = 50;
3641 minwid = (minwid > 50 ? 50 : minwid) * l;
3642 if (*s == '(')
3644 groupitem[groupdepth++] = curitem;
3645 item[curitem].type = Group;
3646 item[curitem].start = p;
3647 item[curitem].minwid = minwid;
3648 item[curitem].maxwid = maxwid;
3649 s++;
3650 curitem++;
3651 continue;
3653 if (vim_strchr(STL_ALL, *s) == NULL)
3655 s++;
3656 continue;
3658 opt = *s++;
3660 /* OK - now for the real work */
3661 base = 'D';
3662 itemisflag = FALSE;
3663 fillable = TRUE;
3664 num = -1;
3665 str = NULL;
3666 switch (opt)
3668 case STL_FILEPATH:
3669 case STL_FULLPATH:
3670 case STL_FILENAME:
3671 fillable = FALSE; /* don't change ' ' to fillchar */
3672 if (buf_spname(wp->w_buffer) != NULL)
3673 STRCPY(NameBuff, buf_spname(wp->w_buffer));
3674 else
3676 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
3677 : wp->w_buffer->b_fname;
3678 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3680 trans_characters(NameBuff, MAXPATHL);
3681 if (opt != STL_FILENAME)
3682 str = NameBuff;
3683 else
3684 str = gettail(NameBuff);
3685 break;
3687 case STL_VIM_EXPR: /* '{' */
3688 itemisflag = TRUE;
3689 t = p;
3690 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3691 *p++ = *s++;
3692 if (*s != '}') /* missing '}' or out of space */
3693 break;
3694 s++;
3695 *p = 0;
3696 p = t;
3698 #ifdef FEAT_EVAL
3699 sprintf((char *)tmp, "%d", curbuf->b_fnum);
3700 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3702 o_curbuf = curbuf;
3703 o_curwin = curwin;
3704 curwin = wp;
3705 curbuf = wp->w_buffer;
3707 str = eval_to_string_safe(p, &t, use_sandbox);
3709 curwin = o_curwin;
3710 curbuf = o_curbuf;
3711 do_unlet((char_u *)"g:actual_curbuf", TRUE);
3713 if (str != NULL && *str != 0)
3715 if (*skipdigits(str) == NUL)
3717 num = atoi((char *)str);
3718 vim_free(str);
3719 str = NULL;
3720 itemisflag = FALSE;
3723 #endif
3724 break;
3726 case STL_LINE:
3727 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3728 ? 0L : (long)(wp->w_cursor.lnum);
3729 break;
3731 case STL_NUMLINES:
3732 num = wp->w_buffer->b_ml.ml_line_count;
3733 break;
3735 case STL_COLUMN:
3736 num = !(State & INSERT) && empty_line
3737 ? 0 : (int)wp->w_cursor.col + 1;
3738 break;
3740 case STL_VIRTCOL:
3741 case STL_VIRTCOL_ALT:
3742 /* In list mode virtcol needs to be recomputed */
3743 virtcol = wp->w_virtcol;
3744 if (wp->w_p_list && lcs_tab1 == NUL)
3746 wp->w_p_list = FALSE;
3747 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3748 wp->w_p_list = TRUE;
3750 ++virtcol;
3751 /* Don't display %V if it's the same as %c. */
3752 if (opt == STL_VIRTCOL_ALT
3753 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3754 ? 0 : (int)wp->w_cursor.col + 1)))
3755 break;
3756 num = (long)virtcol;
3757 break;
3759 case STL_PERCENTAGE:
3760 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3761 (long)wp->w_buffer->b_ml.ml_line_count);
3762 break;
3764 case STL_ALTPERCENT:
3765 str = tmp;
3766 get_rel_pos(wp, str);
3767 break;
3769 case STL_ARGLISTSTAT:
3770 fillable = FALSE;
3771 tmp[0] = 0;
3772 if (append_arg_number(wp, tmp, FALSE, (int)sizeof(tmp)))
3773 str = tmp;
3774 break;
3776 case STL_KEYMAP:
3777 fillable = FALSE;
3778 if (get_keymap_str(wp, tmp, TMPLEN))
3779 str = tmp;
3780 break;
3781 case STL_PAGENUM:
3782 #if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
3783 num = printer_page_num;
3784 #else
3785 num = 0;
3786 #endif
3787 break;
3789 case STL_BUFNO:
3790 num = wp->w_buffer->b_fnum;
3791 break;
3793 case STL_OFFSET_X:
3794 base = 'X';
3795 case STL_OFFSET:
3796 #ifdef FEAT_BYTEOFF
3797 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
3798 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
3799 0L : l + 1 + (!(State & INSERT) && empty_line ?
3800 0 : (int)wp->w_cursor.col);
3801 #endif
3802 break;
3804 case STL_BYTEVAL_X:
3805 base = 'X';
3806 case STL_BYTEVAL:
3807 if (wp->w_cursor.col > STRLEN(linecont))
3808 num = 0;
3809 else
3811 #ifdef FEAT_MBYTE
3812 num = (*mb_ptr2char)(linecont + wp->w_cursor.col);
3813 #else
3814 num = linecont[wp->w_cursor.col];
3815 #endif
3817 if (num == NL)
3818 num = 0;
3819 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
3820 num = NL;
3821 break;
3823 case STL_ROFLAG:
3824 case STL_ROFLAG_ALT:
3825 itemisflag = TRUE;
3826 if (wp->w_buffer->b_p_ro)
3827 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : "[RO]");
3828 break;
3830 case STL_HELPFLAG:
3831 case STL_HELPFLAG_ALT:
3832 itemisflag = TRUE;
3833 if (wp->w_buffer->b_help)
3834 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
3835 : _("[Help]"));
3836 break;
3838 #ifdef FEAT_AUTOCMD
3839 case STL_FILETYPE:
3840 if (*wp->w_buffer->b_p_ft != NUL
3841 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
3843 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
3844 wp->w_buffer->b_p_ft);
3845 str = tmp;
3847 break;
3849 case STL_FILETYPE_ALT:
3850 itemisflag = TRUE;
3851 if (*wp->w_buffer->b_p_ft != NUL
3852 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
3854 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
3855 wp->w_buffer->b_p_ft);
3856 for (t = tmp; *t != 0; t++)
3857 *t = TOUPPER_LOC(*t);
3858 str = tmp;
3860 break;
3861 #endif
3863 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3864 case STL_PREVIEWFLAG:
3865 case STL_PREVIEWFLAG_ALT:
3866 itemisflag = TRUE;
3867 if (wp->w_p_pvw)
3868 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
3869 : _("[Preview]"));
3870 break;
3871 #endif
3873 case STL_MODIFIED:
3874 case STL_MODIFIED_ALT:
3875 itemisflag = TRUE;
3876 switch ((opt == STL_MODIFIED_ALT)
3877 + bufIsChanged(wp->w_buffer) * 2
3878 + (!wp->w_buffer->b_p_ma) * 4)
3880 case 2: str = (char_u *)"[+]"; break;
3881 case 3: str = (char_u *)",+"; break;
3882 case 4: str = (char_u *)"[-]"; break;
3883 case 5: str = (char_u *)",-"; break;
3884 case 6: str = (char_u *)"[+-]"; break;
3885 case 7: str = (char_u *)",+-"; break;
3887 break;
3889 case STL_HIGHLIGHT:
3890 t = s;
3891 while (*s != '#' && *s != NUL)
3892 ++s;
3893 if (*s == '#')
3895 item[curitem].type = Highlight;
3896 item[curitem].start = p;
3897 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
3898 curitem++;
3900 ++s;
3901 continue;
3904 item[curitem].start = p;
3905 item[curitem].type = Normal;
3906 if (str != NULL && *str)
3908 t = str;
3909 if (itemisflag)
3911 if ((t[0] && t[1])
3912 && ((!prevchar_isitem && *t == ',')
3913 || (prevchar_isflag && *t == ' ')))
3914 t++;
3915 prevchar_isflag = TRUE;
3917 l = vim_strsize(t);
3918 if (l > 0)
3919 prevchar_isitem = TRUE;
3920 if (l > maxwid)
3922 while (l >= maxwid)
3923 #ifdef FEAT_MBYTE
3924 if (has_mbyte)
3926 l -= ptr2cells(t);
3927 t += (*mb_ptr2len)(t);
3929 else
3930 #endif
3931 l -= byte2cells(*t++);
3932 if (p + 1 >= out + outlen)
3933 break;
3934 *p++ = '<';
3936 if (minwid > 0)
3938 for (; l < minwid && p + 1 < out + outlen; l++)
3940 /* Don't put a "-" in front of a digit. */
3941 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
3942 *p++ = ' ';
3943 else
3944 *p++ = fillchar;
3946 minwid = 0;
3948 else
3949 minwid *= -1;
3950 while (*t && p + 1 < out + outlen)
3952 *p++ = *t++;
3953 /* Change a space by fillchar, unless fillchar is '-' and a
3954 * digit follows. */
3955 if (fillable && p[-1] == ' '
3956 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
3957 p[-1] = fillchar;
3959 for (; l < minwid && p + 1 < out + outlen; l++)
3960 *p++ = fillchar;
3962 else if (num >= 0)
3964 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
3965 char_u nstr[20];
3967 if (p + 20 >= out + outlen)
3968 break; /* not sufficient space */
3969 prevchar_isitem = TRUE;
3970 t = nstr;
3971 if (opt == STL_VIRTCOL_ALT)
3973 *t++ = '-';
3974 minwid--;
3976 *t++ = '%';
3977 if (zeropad)
3978 *t++ = '0';
3979 *t++ = '*';
3980 *t++ = nbase == 16 ? base : (nbase == 8 ? 'o' : 'd');
3981 *t = 0;
3983 for (n = num, l = 1; n >= nbase; n /= nbase)
3984 l++;
3985 if (opt == STL_VIRTCOL_ALT)
3986 l++;
3987 if (l > maxwid)
3989 l += 2;
3990 n = l - maxwid;
3991 while (l-- > maxwid)
3992 num /= nbase;
3993 *t++ = '>';
3994 *t++ = '%';
3995 *t = t[-3];
3996 *++t = 0;
3997 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
3998 0, num, n);
4000 else
4001 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4002 minwid, num);
4003 p += STRLEN(p);
4005 else
4006 item[curitem].type = Empty;
4008 if (opt == STL_VIM_EXPR)
4009 vim_free(str);
4011 if (num >= 0 || (!itemisflag && str && *str))
4012 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4013 curitem++;
4015 *p = NUL;
4016 itemcnt = curitem;
4018 #ifdef FEAT_EVAL
4019 if (usefmt != fmt)
4020 vim_free(usefmt);
4021 #endif
4023 width = vim_strsize(out);
4024 if (maxwidth > 0 && width > maxwidth)
4026 /* Result is too long, must truncate somewhere. */
4027 l = 0;
4028 if (itemcnt == 0)
4029 s = out;
4030 else
4032 for ( ; l < itemcnt; l++)
4033 if (item[l].type == Trunc)
4035 /* Truncate at %< item. */
4036 s = item[l].start;
4037 break;
4039 if (l == itemcnt)
4041 /* No %< item, truncate first item. */
4042 s = item[0].start;
4043 l = 0;
4047 if (width - vim_strsize(s) >= maxwidth)
4049 /* Truncation mark is beyond max length */
4050 #ifdef FEAT_MBYTE
4051 if (has_mbyte)
4053 s = out;
4054 width = 0;
4055 for (;;)
4057 width += ptr2cells(s);
4058 if (width >= maxwidth)
4059 break;
4060 s += (*mb_ptr2len)(s);
4062 /* Fill up for half a double-wide character. */
4063 while (++width < maxwidth)
4064 *s++ = fillchar;
4066 else
4067 #endif
4068 s = out + maxwidth - 1;
4069 for (l = 0; l < itemcnt; l++)
4070 if (item[l].start > s)
4071 break;
4072 itemcnt = l;
4073 *s++ = '>';
4074 *s = 0;
4076 else
4078 #ifdef FEAT_MBYTE
4079 if (has_mbyte)
4081 n = 0;
4082 while (width >= maxwidth)
4084 width -= ptr2cells(s + n);
4085 n += (*mb_ptr2len)(s + n);
4088 else
4089 #endif
4090 n = width - maxwidth + 1;
4091 p = s + n;
4092 STRMOVE(s + 1, p);
4093 *s = '<';
4095 /* Fill up for half a double-wide character. */
4096 while (++width < maxwidth)
4098 s = s + STRLEN(s);
4099 *s++ = fillchar;
4100 *s = NUL;
4103 --n; /* count the '<' */
4104 for (; l < itemcnt; l++)
4106 if (item[l].start - n >= s)
4107 item[l].start -= n;
4108 else
4109 item[l].start = s;
4112 width = maxwidth;
4114 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4116 /* Apply STL_MIDDLE if any */
4117 for (l = 0; l < itemcnt; l++)
4118 if (item[l].type == Middle)
4119 break;
4120 if (l < itemcnt)
4122 p = item[l].start + maxwidth - width;
4123 STRMOVE(p, item[l].start);
4124 for (s = item[l].start; s < p; s++)
4125 *s = fillchar;
4126 for (l++; l < itemcnt; l++)
4127 item[l].start += maxwidth - width;
4128 width = maxwidth;
4132 /* Store the info about highlighting. */
4133 if (hltab != NULL)
4135 sp = hltab;
4136 for (l = 0; l < itemcnt; l++)
4138 if (item[l].type == Highlight)
4140 sp->start = item[l].start;
4141 sp->userhl = item[l].minwid;
4142 sp++;
4145 sp->start = NULL;
4146 sp->userhl = 0;
4149 /* Store the info about tab pages labels. */
4150 if (tabtab != NULL)
4152 sp = tabtab;
4153 for (l = 0; l < itemcnt; l++)
4155 if (item[l].type == TabPage)
4157 sp->start = item[l].start;
4158 sp->userhl = item[l].minwid;
4159 sp++;
4162 sp->start = NULL;
4163 sp->userhl = 0;
4166 return width;
4168 #endif /* FEAT_STL_OPT */
4170 #if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4171 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
4173 * Get relative cursor position in window into "str[]", in the form 99%, using
4174 * "Top", "Bot" or "All" when appropriate.
4176 void
4177 get_rel_pos(wp, str)
4178 win_T *wp;
4179 char_u *str;
4181 long above; /* number of lines above window */
4182 long below; /* number of lines below window */
4184 above = wp->w_topline - 1;
4185 #ifdef FEAT_DIFF
4186 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
4187 #endif
4188 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4189 if (below <= 0)
4190 STRCPY(str, above == 0 ? _("All") : _("Bot"));
4191 else if (above <= 0)
4192 STRCPY(str, _("Top"));
4193 else
4194 sprintf((char *)str, "%2d%%", above > 1000000L
4195 ? (int)(above / ((above + below) / 100L))
4196 : (int)(above * 100L / (above + below)));
4198 #endif
4201 * Append (file 2 of 8) to 'buf', if editing more than one file.
4202 * Return TRUE if it was appended.
4205 append_arg_number(wp, buf, add_file, maxlen)
4206 win_T *wp;
4207 char_u *buf;
4208 int add_file; /* Add "file" before the arg number */
4209 int maxlen; /* maximum nr of chars in buf or zero*/
4211 char_u *p;
4213 if (ARGCOUNT <= 1) /* nothing to do */
4214 return FALSE;
4216 p = buf + STRLEN(buf); /* go to the end of the buffer */
4217 if (maxlen && p - buf + 35 >= maxlen) /* getting too long */
4218 return FALSE;
4219 *p++ = ' ';
4220 *p++ = '(';
4221 if (add_file)
4223 STRCPY(p, "file ");
4224 p += 5;
4226 sprintf((char *)p, wp->w_arg_idx_invalid ? "(%d) of %d)"
4227 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4228 return TRUE;
4232 * If fname is not a full path, make it a full path.
4233 * Returns pointer to allocated memory (NULL for failure).
4235 char_u *
4236 fix_fname(fname)
4237 char_u *fname;
4240 * Force expanding the path always for Unix, because symbolic links may
4241 * mess up the full path name, even though it starts with a '/'.
4242 * Also expand when there is ".." in the file name, try to remove it,
4243 * because "c:/src/../README" is equal to "c:/README".
4244 * Similarly "c:/src//file" is equal to "c:/src/file".
4245 * For MS-Windows also expand names like "longna~1" to "longname".
4247 #ifdef UNIX
4248 return FullName_save(fname, TRUE);
4249 #else
4250 if (!vim_isAbsName(fname)
4251 || strstr((char *)fname, "..") != NULL
4252 || strstr((char *)fname, "//") != NULL
4253 # ifdef BACKSLASH_IN_FILENAME
4254 || strstr((char *)fname, "\\\\") != NULL
4255 # endif
4256 # if defined(MSWIN) || defined(DJGPP)
4257 || vim_strchr(fname, '~') != NULL
4258 # endif
4260 return FullName_save(fname, FALSE);
4262 fname = vim_strsave(fname);
4264 # ifdef USE_FNAME_CASE
4265 # ifdef USE_LONG_FNAME
4266 if (USE_LONG_FNAME)
4267 # endif
4269 if (fname != NULL)
4270 fname_case(fname, 0); /* set correct case for file name */
4272 # endif
4274 return fname;
4275 #endif
4279 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4280 * "ffname" becomes a pointer to allocated memory (or NULL).
4282 /*ARGSUSED*/
4283 void
4284 fname_expand(buf, ffname, sfname)
4285 buf_T *buf;
4286 char_u **ffname;
4287 char_u **sfname;
4289 if (*ffname == NULL) /* if no file name given, nothing to do */
4290 return;
4291 if (*sfname == NULL) /* if no short file name given, use ffname */
4292 *sfname = *ffname;
4293 *ffname = fix_fname(*ffname); /* expand to full path */
4295 #ifdef FEAT_SHORTCUT
4296 if (!buf->b_p_bin)
4298 char_u *rfname;
4300 /* If the file name is a shortcut file, use the file it links to. */
4301 rfname = mch_resolve_shortcut(*ffname);
4302 if (rfname != NULL)
4304 vim_free(*ffname);
4305 *ffname = rfname;
4306 *sfname = rfname;
4309 #endif
4313 * Get the file name for an argument list entry.
4315 char_u *
4316 alist_name(aep)
4317 aentry_T *aep;
4319 buf_T *bp;
4321 /* Use the name from the associated buffer if it exists. */
4322 bp = buflist_findnr(aep->ae_fnum);
4323 if (bp == NULL || bp->b_fname == NULL)
4324 return aep->ae_fname;
4325 return bp->b_fname;
4328 #if defined(FEAT_WINDOWS) || defined(PROTO)
4330 * do_arg_all(): Open up to 'count' windows, one for each argument.
4332 void
4333 do_arg_all(count, forceit, keep_tabs)
4334 int count;
4335 int forceit; /* hide buffers in current windows */
4336 int keep_tabs; /* keep current tabs, for ":tab drop file" */
4338 int i;
4339 win_T *wp, *wpnext;
4340 char_u *opened; /* array of flags for which args are open */
4341 int opened_len; /* length of opened[] */
4342 int use_firstwin = FALSE; /* use first window for arglist */
4343 int split_ret = OK;
4344 int p_ea_save;
4345 alist_T *alist; /* argument list to be used */
4346 buf_T *buf;
4347 tabpage_T *tpnext;
4348 int had_tab = cmdmod.tab;
4349 win_T *new_curwin = NULL;
4350 tabpage_T *new_curtab = NULL;
4352 if (ARGCOUNT <= 0)
4354 /* Don't give an error message. We don't want it when the ":all"
4355 * command is in the .vimrc. */
4356 return;
4358 setpcmark();
4360 opened_len = ARGCOUNT;
4361 opened = alloc_clear((unsigned)opened_len);
4362 if (opened == NULL)
4363 return;
4365 #ifdef FEAT_GUI
4366 need_mouse_correct = TRUE;
4367 #endif
4370 * Try closing all windows that are not in the argument list.
4371 * Also close windows that are not full width;
4372 * When 'hidden' or "forceit" set the buffer becomes hidden.
4373 * Windows that have a changed buffer and can't be hidden won't be closed.
4374 * When the ":tab" modifier was used do this for all tab pages.
4376 if (had_tab > 0)
4377 goto_tabpage_tp(first_tabpage);
4378 for (;;)
4380 tpnext = curtab->tp_next;
4381 for (wp = firstwin; wp != NULL; wp = wpnext)
4383 wpnext = wp->w_next;
4384 buf = wp->w_buffer;
4385 if (buf->b_ffname == NULL
4386 || buf->b_nwindows > 1
4387 #ifdef FEAT_VERTSPLIT
4388 || wp->w_width != Columns
4389 #endif
4391 i = ARGCOUNT;
4392 else
4394 /* check if the buffer in this window is in the arglist */
4395 for (i = 0; i < ARGCOUNT; ++i)
4397 if (ARGLIST[i].ae_fnum == buf->b_fnum
4398 || fullpathcmp(alist_name(&ARGLIST[i]),
4399 buf->b_ffname, TRUE) & FPC_SAME)
4401 if (i < opened_len)
4403 opened[i] = TRUE;
4404 if (i == 0)
4406 new_curwin = wp;
4407 new_curtab = curtab;
4410 if (wp->w_alist != curwin->w_alist)
4412 /* Use the current argument list for all windows
4413 * containing a file from it. */
4414 alist_unlink(wp->w_alist);
4415 wp->w_alist = curwin->w_alist;
4416 ++wp->w_alist->al_refcount;
4418 break;
4422 wp->w_arg_idx = i;
4424 if (i == ARGCOUNT && !keep_tabs) /* close this window */
4426 if (P_HID(buf) || forceit || buf->b_nwindows > 1
4427 || !bufIsChanged(buf))
4429 /* If the buffer was changed, and we would like to hide it,
4430 * try autowriting. */
4431 if (!P_HID(buf) && buf->b_nwindows <= 1
4432 && bufIsChanged(buf))
4434 (void)autowrite(buf, FALSE);
4435 #ifdef FEAT_AUTOCMD
4436 /* check if autocommands removed the window */
4437 if (!win_valid(wp) || !buf_valid(buf))
4439 wpnext = firstwin; /* start all over... */
4440 continue;
4442 #endif
4444 #ifdef FEAT_WINDOWS
4445 /* don't close last window */
4446 if (firstwin == lastwin && first_tabpage->tp_next == NULL)
4447 #endif
4448 use_firstwin = TRUE;
4449 #ifdef FEAT_WINDOWS
4450 else
4452 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4453 # ifdef FEAT_AUTOCMD
4454 /* check if autocommands removed the next window */
4455 if (!win_valid(wpnext))
4456 wpnext = firstwin; /* start all over... */
4457 # endif
4459 #endif
4464 /* Without the ":tab" modifier only do the current tab page. */
4465 if (had_tab == 0 || tpnext == NULL)
4466 break;
4468 # ifdef FEAT_AUTOCMD
4469 /* check if autocommands removed the next tab page */
4470 if (!valid_tabpage(tpnext))
4471 tpnext = first_tabpage; /* start all over...*/
4472 # endif
4473 goto_tabpage_tp(tpnext);
4477 * Open a window for files in the argument list that don't have one.
4478 * ARGCOUNT may change while doing this, because of autocommands.
4480 if (count > ARGCOUNT || count <= 0)
4481 count = ARGCOUNT;
4483 /* Autocommands may do anything to the argument list. Make sure it's not
4484 * freed while we are working here by "locking" it. We still have to
4485 * watch out for its size to be changed. */
4486 alist = curwin->w_alist;
4487 ++alist->al_refcount;
4489 #ifdef FEAT_AUTOCMD
4490 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4491 ++autocmd_no_enter;
4492 ++autocmd_no_leave;
4493 #endif
4494 win_enter(lastwin, FALSE);
4495 #ifdef FEAT_WINDOWS
4496 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4497 * leaving an empty tab page when executed locally. */
4498 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4499 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4500 use_firstwin = TRUE;
4501 #endif
4503 for (i = 0; i < count && i < alist->al_ga.ga_len && !got_int; ++i)
4505 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4506 arg_had_last = TRUE;
4507 if (i < opened_len && opened[i])
4509 /* Move the already present window to below the current window */
4510 if (curwin->w_arg_idx != i)
4512 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4514 if (wpnext->w_arg_idx == i)
4516 win_move_after(wpnext, curwin);
4517 break;
4522 else if (split_ret == OK)
4524 if (!use_firstwin) /* split current window */
4526 p_ea_save = p_ea;
4527 p_ea = TRUE; /* use space from all windows */
4528 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4529 p_ea = p_ea_save;
4530 if (split_ret == FAIL)
4531 continue;
4533 #ifdef FEAT_AUTOCMD
4534 else /* first window: do autocmd for leaving this buffer */
4535 --autocmd_no_leave;
4536 #endif
4539 * edit file "i"
4541 curwin->w_arg_idx = i;
4542 if (i == 0)
4544 new_curwin = curwin;
4545 new_curtab = curtab;
4547 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4548 ECMD_ONE,
4549 ((P_HID(curwin->w_buffer)
4550 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
4551 + ECMD_OLDBUF, curwin);
4552 #ifdef FEAT_AUTOCMD
4553 if (use_firstwin)
4554 ++autocmd_no_leave;
4555 #endif
4556 use_firstwin = FALSE;
4558 ui_breakcheck();
4560 /* When ":tab" was used open a new tab for a new window repeatedly. */
4561 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4562 cmdmod.tab = 9999;
4565 /* Remove the "lock" on the argument list. */
4566 alist_unlink(alist);
4568 #ifdef FEAT_AUTOCMD
4569 --autocmd_no_enter;
4570 #endif
4571 /* to window with first arg */
4572 if (valid_tabpage(new_curtab))
4573 goto_tabpage_tp(new_curtab);
4574 if (win_valid(new_curwin))
4575 win_enter(new_curwin, FALSE);
4577 #ifdef FEAT_AUTOCMD
4578 --autocmd_no_leave;
4579 #endif
4580 vim_free(opened);
4583 # if defined(FEAT_LISTCMDS) || defined(PROTO)
4585 * Open a window for a number of buffers.
4587 void
4588 ex_buffer_all(eap)
4589 exarg_T *eap;
4591 buf_T *buf;
4592 win_T *wp, *wpnext;
4593 int split_ret = OK;
4594 int p_ea_save;
4595 int open_wins = 0;
4596 int r;
4597 int count; /* Maximum number of windows to open. */
4598 int all; /* When TRUE also load inactive buffers. */
4599 #ifdef FEAT_WINDOWS
4600 int had_tab = cmdmod.tab;
4601 tabpage_T *tpnext;
4602 #endif
4604 if (eap->addr_count == 0) /* make as many windows as possible */
4605 count = 9999;
4606 else
4607 count = eap->line2; /* make as many windows as specified */
4608 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4609 all = FALSE;
4610 else
4611 all = TRUE;
4613 setpcmark();
4615 #ifdef FEAT_GUI
4616 need_mouse_correct = TRUE;
4617 #endif
4620 * Close superfluous windows (two windows for the same buffer).
4621 * Also close windows that are not full-width.
4623 #ifdef FEAT_WINDOWS
4624 if (had_tab > 0)
4625 goto_tabpage_tp(first_tabpage);
4626 for (;;)
4628 #endif
4629 tpnext = curtab->tp_next;
4630 for (wp = firstwin; wp != NULL; wp = wpnext)
4632 wpnext = wp->w_next;
4633 if ((wp->w_buffer->b_nwindows > 1
4634 #ifdef FEAT_VERTSPLIT
4635 || ((cmdmod.split & WSP_VERT)
4636 ? wp->w_height + wp->w_status_height < Rows - p_ch
4637 - tabline_height()
4638 : wp->w_width != Columns)
4639 #endif
4640 #ifdef FEAT_WINDOWS
4641 || (had_tab > 0 && wp != firstwin)
4642 #endif
4643 ) && firstwin != lastwin)
4645 win_close(wp, FALSE);
4646 #ifdef FEAT_AUTOCMD
4647 wpnext = firstwin; /* just in case an autocommand does
4648 something strange with windows */
4649 tpnext = first_tabpage; /* start all over...*/
4650 open_wins = 0;
4651 #endif
4653 else
4654 ++open_wins;
4657 #ifdef FEAT_WINDOWS
4658 /* Without the ":tab" modifier only do the current tab page. */
4659 if (had_tab == 0 || tpnext == NULL)
4660 break;
4661 goto_tabpage_tp(tpnext);
4663 #endif
4666 * Go through the buffer list. When a buffer doesn't have a window yet,
4667 * open one. Otherwise move the window to the right position.
4668 * Watch out for autocommands that delete buffers or windows!
4670 #ifdef FEAT_AUTOCMD
4671 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4672 ++autocmd_no_enter;
4673 #endif
4674 win_enter(lastwin, FALSE);
4675 #ifdef FEAT_AUTOCMD
4676 ++autocmd_no_leave;
4677 #endif
4678 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4680 /* Check if this buffer needs a window */
4681 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4682 continue;
4684 #ifdef FEAT_WINDOWS
4685 if (had_tab != 0)
4687 /* With the ":tab" modifier don't move the window. */
4688 if (buf->b_nwindows > 0)
4689 wp = lastwin; /* buffer has a window, skip it */
4690 else
4691 wp = NULL;
4693 else
4694 #endif
4696 /* Check if this buffer already has a window */
4697 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4698 if (wp->w_buffer == buf)
4699 break;
4700 /* If the buffer already has a window, move it */
4701 if (wp != NULL)
4702 win_move_after(wp, curwin);
4705 if (wp == NULL && split_ret == OK)
4707 /* Split the window and put the buffer in it */
4708 p_ea_save = p_ea;
4709 p_ea = TRUE; /* use space from all windows */
4710 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4711 ++open_wins;
4712 p_ea = p_ea_save;
4713 if (split_ret == FAIL)
4714 continue;
4716 /* Open the buffer in this window. */
4717 #if defined(HAS_SWAP_EXISTS_ACTION)
4718 swap_exists_action = SEA_DIALOG;
4719 #endif
4720 set_curbuf(buf, DOBUF_GOTO);
4721 #ifdef FEAT_AUTOCMD
4722 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
4724 #if defined(HAS_SWAP_EXISTS_ACTION)
4725 swap_exists_action = SEA_NONE;
4726 # endif
4727 break;
4729 #endif
4730 #if defined(HAS_SWAP_EXISTS_ACTION)
4731 if (swap_exists_action == SEA_QUIT)
4733 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4734 cleanup_T cs;
4736 /* Reset the error/interrupt/exception state here so that
4737 * aborting() returns FALSE when closing a window. */
4738 enter_cleanup(&cs);
4739 # endif
4741 /* User selected Quit at ATTENTION prompt; close this window. */
4742 win_close(curwin, TRUE);
4743 --open_wins;
4744 swap_exists_action = SEA_NONE;
4745 swap_exists_did_quit = TRUE;
4747 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4748 /* Restore the error/interrupt/exception state if not
4749 * discarded by a new aborting error, interrupt, or uncaught
4750 * exception. */
4751 leave_cleanup(&cs);
4752 # endif
4754 else
4755 handle_swap_exists(NULL);
4756 #endif
4759 ui_breakcheck();
4760 if (got_int)
4762 (void)vgetc(); /* only break the file loading, not the rest */
4763 break;
4765 #ifdef FEAT_EVAL
4766 /* Autocommands deleted the buffer or aborted script processing!!! */
4767 if (aborting())
4768 break;
4769 #endif
4770 #ifdef FEAT_WINDOWS
4771 /* When ":tab" was used open a new tab for a new window repeatedly. */
4772 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4773 cmdmod.tab = 9999;
4774 #endif
4776 #ifdef FEAT_AUTOCMD
4777 --autocmd_no_enter;
4778 #endif
4779 win_enter(firstwin, FALSE); /* back to first window */
4780 #ifdef FEAT_AUTOCMD
4781 --autocmd_no_leave;
4782 #endif
4785 * Close superfluous windows.
4787 for (wp = lastwin; open_wins > count; )
4789 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
4790 || autowrite(wp->w_buffer, FALSE) == OK);
4791 #ifdef FEAT_AUTOCMD
4792 if (!win_valid(wp))
4794 /* BufWrite Autocommands made the window invalid, start over */
4795 wp = lastwin;
4797 else
4798 #endif
4799 if (r)
4801 win_close(wp, !P_HID(wp->w_buffer));
4802 --open_wins;
4803 wp = lastwin;
4805 else
4807 wp = wp->w_prev;
4808 if (wp == NULL)
4809 break;
4813 # endif /* FEAT_LISTCMDS */
4815 #endif /* FEAT_WINDOWS */
4817 static int chk_modeline __ARGS((linenr_T, int));
4820 * do_modelines() - process mode lines for the current file
4822 * "flags" can be:
4823 * OPT_WINONLY only set options local to window
4824 * OPT_NOWIN don't set options local to window
4826 * Returns immediately if the "ml" option isn't set.
4828 void
4829 do_modelines(flags)
4830 int flags;
4832 linenr_T lnum;
4833 int nmlines;
4834 static int entered = 0;
4836 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
4837 return;
4839 /* Disallow recursive entry here. Can happen when executing a modeline
4840 * triggers an autocommand, which reloads modelines with a ":do". */
4841 if (entered)
4842 return;
4844 ++entered;
4845 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
4846 ++lnum)
4847 if (chk_modeline(lnum, flags) == FAIL)
4848 nmlines = 0;
4850 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
4851 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
4852 if (chk_modeline(lnum, flags) == FAIL)
4853 nmlines = 0;
4854 --entered;
4857 #include "version.h" /* for version number */
4860 * chk_modeline() - check a single line for a mode string
4861 * Return FAIL if an error encountered.
4863 static int
4864 chk_modeline(lnum, flags)
4865 linenr_T lnum;
4866 int flags; /* Same as for do_modelines(). */
4868 char_u *s;
4869 char_u *e;
4870 char_u *linecopy; /* local copy of any modeline found */
4871 int prev;
4872 int vers;
4873 int end;
4874 int retval = OK;
4875 char_u *save_sourcing_name;
4876 linenr_T save_sourcing_lnum;
4877 #ifdef FEAT_EVAL
4878 scid_T save_SID;
4879 #endif
4881 prev = -1;
4882 for (s = ml_get(lnum); *s != NUL; ++s)
4884 if (prev == -1 || vim_isspace(prev))
4886 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
4887 || STRNCMP(s, "vi:", (size_t)3) == 0)
4888 break;
4889 if (STRNCMP(s, "vim", 3) == 0)
4891 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
4892 e = s + 4;
4893 else
4894 e = s + 3;
4895 vers = getdigits(&e);
4896 if (*e == ':'
4897 && (s[3] == ':'
4898 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
4899 || (VIM_VERSION_100 < vers && s[3] == '<')
4900 || (VIM_VERSION_100 > vers && s[3] == '>')
4901 || (VIM_VERSION_100 == vers && s[3] == '=')))
4902 break;
4905 prev = *s;
4908 if (*s)
4910 do /* skip over "ex:", "vi:" or "vim:" */
4911 ++s;
4912 while (s[-1] != ':');
4914 s = linecopy = vim_strsave(s); /* copy the line, it will change */
4915 if (linecopy == NULL)
4916 return FAIL;
4918 save_sourcing_lnum = sourcing_lnum;
4919 save_sourcing_name = sourcing_name;
4920 sourcing_lnum = lnum; /* prepare for emsg() */
4921 sourcing_name = (char_u *)"modelines";
4923 end = FALSE;
4924 while (end == FALSE)
4926 s = skipwhite(s);
4927 if (*s == NUL)
4928 break;
4931 * Find end of set command: ':' or end of line.
4932 * Skip over "\:", replacing it with ":".
4934 for (e = s; *e != ':' && *e != NUL; ++e)
4935 if (e[0] == '\\' && e[1] == ':')
4936 STRMOVE(e, e + 1);
4937 if (*e == NUL)
4938 end = TRUE;
4941 * If there is a "set" command, require a terminating ':' and
4942 * ignore the stuff after the ':'.
4943 * "vi:set opt opt opt: foo" -- foo not interpreted
4944 * "vi:opt opt opt: foo" -- foo interpreted
4945 * Accept "se" for compatibility with Elvis.
4947 if (STRNCMP(s, "set ", (size_t)4) == 0
4948 || STRNCMP(s, "se ", (size_t)3) == 0)
4950 if (*e != ':') /* no terminating ':'? */
4951 break;
4952 end = TRUE;
4953 s = vim_strchr(s, ' ') + 1;
4955 *e = NUL; /* truncate the set command */
4957 if (*s != NUL) /* skip over an empty "::" */
4959 #ifdef FEAT_EVAL
4960 save_SID = current_SID;
4961 current_SID = SID_MODELINE;
4962 #endif
4963 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
4964 #ifdef FEAT_EVAL
4965 current_SID = save_SID;
4966 #endif
4967 if (retval == FAIL) /* stop if error found */
4968 break;
4970 s = e + 1; /* advance to next part */
4973 sourcing_lnum = save_sourcing_lnum;
4974 sourcing_name = save_sourcing_name;
4976 vim_free(linecopy);
4978 return retval;
4981 #if defined(FEAT_VIMINFO) || defined(PROTO)
4983 read_viminfo_bufferlist(virp, writing)
4984 vir_T *virp;
4985 int writing;
4987 char_u *tab;
4988 linenr_T lnum;
4989 colnr_T col;
4990 buf_T *buf;
4991 char_u *sfname;
4992 char_u *xline;
4994 /* Handle long line and escaped characters. */
4995 xline = viminfo_readstring(virp, 1, FALSE);
4997 /* don't read in if there are files on the command-line or if writing: */
4998 if (xline != NULL && !writing && ARGCOUNT == 0
4999 && find_viminfo_parameter('%') != NULL)
5001 /* Format is: <fname> Tab <lnum> Tab <col>.
5002 * Watch out for a Tab in the file name, work from the end. */
5003 lnum = 0;
5004 col = 0;
5005 tab = vim_strrchr(xline, '\t');
5006 if (tab != NULL)
5008 *tab++ = '\0';
5009 col = atoi((char *)tab);
5010 tab = vim_strrchr(xline, '\t');
5011 if (tab != NULL)
5013 *tab++ = '\0';
5014 lnum = atol((char *)tab);
5018 /* Expand "~/" in the file name at "line + 1" to a full path.
5019 * Then try shortening it by comparing with the current directory */
5020 expand_env(xline, NameBuff, MAXPATHL);
5021 sfname = shorten_fname1(NameBuff);
5023 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5024 if (buf != NULL) /* just in case... */
5026 buf->b_last_cursor.lnum = lnum;
5027 buf->b_last_cursor.col = col;
5028 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5031 vim_free(xline);
5033 return viminfo_readline(virp);
5036 void
5037 write_viminfo_bufferlist(fp)
5038 FILE *fp;
5040 buf_T *buf;
5041 #ifdef FEAT_WINDOWS
5042 win_T *win;
5043 tabpage_T *tp;
5044 #endif
5045 char_u *line;
5046 int max_buffers;
5048 if (find_viminfo_parameter('%') == NULL)
5049 return;
5051 /* Without a number -1 is returned: do all buffers. */
5052 max_buffers = get_viminfo_parameter('%');
5054 /* Allocate room for the file name, lnum and col. */
5055 line = alloc(MAXPATHL + 40);
5056 if (line == NULL)
5057 return;
5059 #ifdef FEAT_WINDOWS
5060 FOR_ALL_TAB_WINDOWS(tp, win)
5061 set_last_cursor(win);
5062 #else
5063 set_last_cursor(curwin);
5064 #endif
5066 fprintf(fp, _("\n# Buffer list:\n"));
5067 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5069 if (buf->b_fname == NULL
5070 || !buf->b_p_bl
5071 #ifdef FEAT_QUICKFIX
5072 || bt_quickfix(buf)
5073 #endif
5074 || removable(buf->b_ffname))
5075 continue;
5077 if (max_buffers-- == 0)
5078 break;
5079 putc('%', fp);
5080 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
5081 sprintf((char *)line + STRLEN(line), "\t%ld\t%d",
5082 (long)buf->b_last_cursor.lnum,
5083 buf->b_last_cursor.col);
5084 viminfo_writestring(fp, line);
5086 vim_free(line);
5088 #endif
5092 * Return special buffer name.
5093 * Returns NULL when the buffer has a normal file name.
5095 char *
5096 buf_spname(buf)
5097 buf_T *buf;
5099 #if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5100 if (bt_quickfix(buf))
5102 win_T *win = NULL;
5103 tabpage_T *tp;
5106 * For location list window, w_llist_ref points to the location list.
5107 * For quickfix window, w_llist_ref is NULL.
5109 FOR_ALL_TAB_WINDOWS(tp, win)
5110 if (win->w_buffer == buf)
5111 goto win_found;
5112 win_found:
5113 if (win != NULL && win->w_llist_ref != NULL)
5114 return _("[Location List]");
5115 else
5116 return _("[Quickfix List]");
5118 #endif
5119 #ifdef FEAT_QUICKFIX
5120 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5121 * contains the name as specified by the user */
5122 if (bt_nofile(buf))
5124 if (buf->b_sfname != NULL)
5125 return (char *)buf->b_sfname;
5126 return _("[Scratch]");
5128 #endif
5129 if (buf->b_fname == NULL)
5130 return _("[No Name]");
5131 return NULL;
5135 #if defined(FEAT_SIGNS) || defined(PROTO)
5137 * Insert the sign into the signlist.
5139 static void
5140 insert_sign(buf, prev, next, id, lnum, typenr)
5141 buf_T *buf; /* buffer to store sign in */
5142 signlist_T *prev; /* previous sign entry */
5143 signlist_T *next; /* next sign entry */
5144 int id; /* sign ID */
5145 linenr_T lnum; /* line number which gets the mark */
5146 int typenr; /* typenr of sign we are adding */
5148 signlist_T *newsign;
5150 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5151 if (newsign != NULL)
5153 newsign->id = id;
5154 newsign->lnum = lnum;
5155 newsign->typenr = typenr;
5156 newsign->next = next;
5157 #ifdef FEAT_NETBEANS_INTG
5158 newsign->prev = prev;
5159 if (next != NULL)
5160 next->prev = newsign;
5161 #endif
5163 if (prev == NULL)
5165 /* When adding first sign need to redraw the windows to create the
5166 * column for signs. */
5167 if (buf->b_signlist == NULL)
5169 redraw_buf_later(buf, NOT_VALID);
5170 changed_cline_bef_curs();
5173 /* first sign in signlist */
5174 buf->b_signlist = newsign;
5176 else
5177 prev->next = newsign;
5182 * Add the sign into the signlist. Find the right spot to do it though.
5184 void
5185 buf_addsign(buf, id, lnum, typenr)
5186 buf_T *buf; /* buffer to store sign in */
5187 int id; /* sign ID */
5188 linenr_T lnum; /* line number which gets the mark */
5189 int typenr; /* typenr of sign we are adding */
5191 signlist_T *sign; /* a sign in the signlist */
5192 signlist_T *prev; /* the previous sign */
5194 prev = NULL;
5195 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5197 if (lnum == sign->lnum && id == sign->id)
5199 sign->typenr = typenr;
5200 return;
5202 else if (
5203 #ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5204 id < 0 &&
5205 #endif
5206 lnum < sign->lnum)
5208 #ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5209 /* XXX - GRP: Is this because of sign slide problem? Or is it
5210 * really needed? Or is it because we allow multiple signs per
5211 * line? If so, should I add that feature to FEAT_SIGNS?
5213 while (prev != NULL && prev->lnum == lnum)
5214 prev = prev->prev;
5215 if (prev == NULL)
5216 sign = buf->b_signlist;
5217 else
5218 sign = prev->next;
5219 #endif
5220 insert_sign(buf, prev, sign, id, lnum, typenr);
5221 return;
5223 prev = sign;
5225 #ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5226 /* XXX - GRP: See previous comment */
5227 while (prev != NULL && prev->lnum == lnum)
5228 prev = prev->prev;
5229 if (prev == NULL)
5230 sign = buf->b_signlist;
5231 else
5232 sign = prev->next;
5233 #endif
5234 insert_sign(buf, prev, sign, id, lnum, typenr);
5236 return;
5240 buf_change_sign_type(buf, markId, typenr)
5241 buf_T *buf; /* buffer to store sign in */
5242 int markId; /* sign ID */
5243 int typenr; /* typenr of sign we are adding */
5245 signlist_T *sign; /* a sign in the signlist */
5247 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5249 if (sign->id == markId)
5251 sign->typenr = typenr;
5252 return sign->lnum;
5256 return 0;
5259 int_u
5260 buf_getsigntype(buf, lnum, type)
5261 buf_T *buf;
5262 linenr_T lnum;
5263 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5265 signlist_T *sign; /* a sign in a b_signlist */
5267 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5268 if (sign->lnum == lnum
5269 && (type == SIGN_ANY
5270 # ifdef FEAT_SIGN_ICONS
5271 || (type == SIGN_ICON
5272 && sign_get_image(sign->typenr) != NULL)
5273 # endif
5274 || (type == SIGN_TEXT
5275 && sign_get_text(sign->typenr) != NULL)
5276 || (type == SIGN_LINEHL
5277 && sign_get_attr(sign->typenr, TRUE) != 0)))
5278 return sign->typenr;
5279 return 0;
5283 linenr_T
5284 buf_delsign(buf, id)
5285 buf_T *buf; /* buffer sign is stored in */
5286 int id; /* sign id */
5288 signlist_T **lastp; /* pointer to pointer to current sign */
5289 signlist_T *sign; /* a sign in a b_signlist */
5290 signlist_T *next; /* the next sign in a b_signlist */
5291 linenr_T lnum; /* line number whose sign was deleted */
5293 lastp = &buf->b_signlist;
5294 lnum = 0;
5295 for (sign = buf->b_signlist; sign != NULL; sign = next)
5297 next = sign->next;
5298 if (sign->id == id)
5300 *lastp = next;
5301 #ifdef FEAT_NETBEANS_INTG
5302 if (next != NULL)
5303 next->prev = sign->prev;
5304 #endif
5305 lnum = sign->lnum;
5306 vim_free(sign);
5307 break;
5309 else
5310 lastp = &sign->next;
5313 /* When deleted the last sign need to redraw the windows to remove the
5314 * sign column. */
5315 if (buf->b_signlist == NULL)
5317 redraw_buf_later(buf, NOT_VALID);
5318 changed_cline_bef_curs();
5321 return lnum;
5326 * Find the line number of the sign with the requested id. If the sign does
5327 * not exist, return 0 as the line number. This will still let the correct file
5328 * get loaded.
5331 buf_findsign(buf, id)
5332 buf_T *buf; /* buffer to store sign in */
5333 int id; /* sign ID */
5335 signlist_T *sign; /* a sign in the signlist */
5337 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5338 if (sign->id == id)
5339 return sign->lnum;
5341 return 0;
5345 buf_findsign_id(buf, lnum)
5346 buf_T *buf; /* buffer whose sign we are searching for */
5347 linenr_T lnum; /* line number of sign */
5349 signlist_T *sign; /* a sign in the signlist */
5351 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5352 if (sign->lnum == lnum)
5353 return sign->id;
5355 return 0;
5359 # if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5360 /* see if a given type of sign exists on a specific line */
5362 buf_findsigntype_id(buf, lnum, typenr)
5363 buf_T *buf; /* buffer whose sign we are searching for */
5364 linenr_T lnum; /* line number of sign */
5365 int typenr; /* sign type number */
5367 signlist_T *sign; /* a sign in the signlist */
5369 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5370 if (sign->lnum == lnum && sign->typenr == typenr)
5371 return sign->id;
5373 return 0;
5377 # if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5378 /* return the number of icons on the given line */
5380 buf_signcount(buf, lnum)
5381 buf_T *buf;
5382 linenr_T lnum;
5384 signlist_T *sign; /* a sign in the signlist */
5385 int count = 0;
5387 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5388 if (sign->lnum == lnum)
5389 if (sign_get_image(sign->typenr) != NULL)
5390 count++;
5392 return count;
5394 # endif /* FEAT_SIGN_ICONS */
5395 # endif /* FEAT_NETBEANS_INTG */
5399 * Delete signs in buffer "buf".
5401 static void
5402 buf_delete_signs(buf)
5403 buf_T *buf;
5405 signlist_T *next;
5407 while (buf->b_signlist != NULL)
5409 next = buf->b_signlist->next;
5410 vim_free(buf->b_signlist);
5411 buf->b_signlist = next;
5416 * Delete all signs in all buffers.
5418 void
5419 buf_delete_all_signs()
5421 buf_T *buf; /* buffer we are checking for signs */
5423 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5424 if (buf->b_signlist != NULL)
5426 /* Need to redraw the windows to remove the sign column. */
5427 redraw_buf_later(buf, NOT_VALID);
5428 buf_delete_signs(buf);
5433 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5435 void
5436 sign_list_placed(rbuf)
5437 buf_T *rbuf;
5439 buf_T *buf;
5440 signlist_T *p;
5441 char lbuf[BUFSIZ];
5443 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5444 msg_putchar('\n');
5445 if (rbuf == NULL)
5446 buf = firstbuf;
5447 else
5448 buf = rbuf;
5449 while (buf != NULL)
5451 if (buf->b_signlist != NULL)
5453 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
5454 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5455 msg_putchar('\n');
5457 for (p = buf->b_signlist; p != NULL; p = p->next)
5459 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
5460 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5461 MSG_PUTS(lbuf);
5462 msg_putchar('\n');
5464 if (rbuf != NULL)
5465 break;
5466 buf = buf->b_next;
5471 * Adjust a placed sign for inserted/deleted lines.
5473 void
5474 sign_mark_adjust(line1, line2, amount, amount_after)
5475 linenr_T line1;
5476 linenr_T line2;
5477 long amount;
5478 long amount_after;
5480 signlist_T *sign; /* a sign in a b_signlist */
5482 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5484 if (sign->lnum >= line1 && sign->lnum <= line2)
5486 if (amount == MAXLNUM)
5487 sign->lnum = line1;
5488 else
5489 sign->lnum += amount;
5491 else if (sign->lnum > line2)
5492 sign->lnum += amount_after;
5495 #endif /* FEAT_SIGNS */
5498 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5500 void
5501 set_buflisted(on)
5502 int on;
5504 if (on != curbuf->b_p_bl)
5506 curbuf->b_p_bl = on;
5507 #ifdef FEAT_AUTOCMD
5508 if (on)
5509 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5510 else
5511 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5512 #endif
5517 * Read the file for "buf" again and check if the contents changed.
5518 * Return TRUE if it changed or this could not be checked.
5521 buf_contents_changed(buf)
5522 buf_T *buf;
5524 buf_T *newbuf;
5525 int differ = TRUE;
5526 linenr_T lnum;
5527 aco_save_T aco;
5528 exarg_T ea;
5530 /* Allocate a buffer without putting it in the buffer list. */
5531 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5532 if (newbuf == NULL)
5533 return TRUE;
5535 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5536 if (prep_exarg(&ea, buf) == FAIL)
5538 wipe_buffer(newbuf, FALSE);
5539 return TRUE;
5542 /* set curwin/curbuf to buf and save a few things */
5543 aucmd_prepbuf(&aco, newbuf);
5545 if (ml_open(curbuf) == OK
5546 && readfile(buf->b_ffname, buf->b_fname,
5547 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5548 &ea, READ_NEW | READ_DUMMY) == OK)
5550 /* compare the two files line by line */
5551 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5553 differ = FALSE;
5554 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5555 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5557 differ = TRUE;
5558 break;
5562 vim_free(ea.cmd);
5564 /* restore curwin/curbuf and a few other things */
5565 aucmd_restbuf(&aco);
5567 if (curbuf != newbuf) /* safety check */
5568 wipe_buffer(newbuf, FALSE);
5570 return differ;
5574 * Wipe out a buffer and decrement the last buffer number if it was used for
5575 * this buffer. Call this to wipe out a temp buffer that does not contain any
5576 * marks.
5578 /*ARGSUSED*/
5579 void
5580 wipe_buffer(buf, aucmd)
5581 buf_T *buf;
5582 int aucmd; /* When TRUE trigger autocommands. */
5584 if (buf->b_fnum == top_file_num - 1)
5585 --top_file_num;
5587 #ifdef FEAT_AUTOCMD
5588 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
5589 block_autocmds();
5590 #endif
5591 close_buffer(NULL, buf, DOBUF_WIPE);
5592 #ifdef FEAT_AUTOCMD
5593 if (!aucmd)
5594 unblock_autocmds();
5595 #endif