Add previous/next window key binding
[MacVim/KaoriYa.git] / src / buffer.c
blob95e1cd006d77e1e2c1f9efa159158d5270310126
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);
263 /* specified ff and enc, bin in modelines */
264 if (file_ff_differs(curbuf))
266 /* reload buffer */
267 if (eap)
269 /* restore ++ff and ++enc, ++bin if specified */
270 #ifdef FEAT_MBYTE
271 if (eap->force_enc)
273 char_u *fenc = enc_canonize(eap->cmd + eap->force_enc);
274 if (fenc) {
275 vim_free(curbuf->b_p_fenc);
276 curbuf->b_p_fenc = fenc;
279 #endif
280 if (eap->force_ff)
281 set_fileformat(eap->force_ff, OPT_LOCAL);
282 if (eap->force_bin)
283 curbuf->b_p_bin = eap->force_bin;
285 buf_reload(curbuf, curbuf->b_orig_mode);
287 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
289 #ifdef FEAT_AUTOCMD
290 # ifdef FEAT_EVAL
291 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
292 &retval);
293 # else
294 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
295 # endif
297 /* restore curwin/curbuf and a few other things */
298 aucmd_restbuf(&aco);
300 #endif
303 return retval;
307 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
310 buf_valid(buf)
311 buf_T *buf;
313 buf_T *bp;
315 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
316 if (bp == buf)
317 return TRUE;
318 return FALSE;
322 * Close the link to a buffer.
323 * "action" is used when there is no longer a window for the buffer.
324 * It can be:
325 * 0 buffer becomes hidden
326 * DOBUF_UNLOAD buffer is unloaded
327 * DOBUF_DELETE buffer is unloaded and removed from buffer list
328 * DOBUF_WIPE buffer is unloaded and really deleted
329 * When doing all but the first one on the current buffer, the caller should
330 * get a new buffer very soon!
332 * The 'bufhidden' option can force freeing and deleting.
334 void
335 close_buffer(win, buf, action)
336 win_T *win; /* if not NULL, set b_last_cursor */
337 buf_T *buf;
338 int action;
340 #ifdef FEAT_AUTOCMD
341 int is_curbuf;
342 int nwindows = buf->b_nwindows;
343 #endif
344 int unload_buf = (action != 0);
345 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
346 int wipe_buf = (action == DOBUF_WIPE);
348 #ifdef FEAT_QUICKFIX
350 * Force unloading or deleting when 'bufhidden' says so.
351 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
352 * "hide" (otherwise we could never free or delete a buffer).
354 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
356 del_buf = TRUE;
357 unload_buf = TRUE;
359 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
361 del_buf = TRUE;
362 unload_buf = TRUE;
363 wipe_buf = TRUE;
365 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
366 unload_buf = TRUE;
367 #endif
369 if (win != NULL)
371 /* Set b_last_cursor when closing the last window for the buffer.
372 * Remember the last cursor position and window options of the buffer.
373 * This used to be only for the current window, but then options like
374 * 'foldmethod' may be lost with a ":only" command. */
375 if (buf->b_nwindows == 1)
376 set_last_cursor(win);
377 buflist_setfpos(buf, win,
378 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
379 win->w_cursor.col, TRUE);
382 #ifdef FEAT_AUTOCMD
383 /* When the buffer is no longer in a window, trigger BufWinLeave */
384 if (buf->b_nwindows == 1)
386 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
387 FALSE, buf);
388 if (!buf_valid(buf)) /* autocommands may delete the buffer */
389 return;
391 /* When the buffer becomes hidden, but is not unloaded, trigger
392 * BufHidden */
393 if (!unload_buf)
395 apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
396 FALSE, buf);
397 if (!buf_valid(buf)) /* autocmds may delete the buffer */
398 return;
400 # ifdef FEAT_EVAL
401 if (aborting()) /* autocmds may abort script processing */
402 return;
403 # endif
405 nwindows = buf->b_nwindows;
406 #endif
408 /* decrease the link count from windows (unless not in any window) */
409 if (buf->b_nwindows > 0)
410 --buf->b_nwindows;
412 /* Return when a window is displaying the buffer or when it's not
413 * unloaded. */
414 if (buf->b_nwindows > 0 || !unload_buf)
416 #if 0 /* why was this here? */
417 if (buf == curbuf)
418 u_sync(); /* sync undo before going to another buffer */
419 #endif
420 return;
423 /* Always remove the buffer when there is no file name. */
424 if (buf->b_ffname == NULL)
425 del_buf = TRUE;
428 * Free all things allocated for this buffer.
429 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
431 #ifdef FEAT_AUTOCMD
432 /* Remember if we are closing the current buffer. Restore the number of
433 * windows, so that autocommands in buf_freeall() don't get confused. */
434 is_curbuf = (buf == curbuf);
435 buf->b_nwindows = nwindows;
436 #endif
438 buf_freeall(buf, del_buf, wipe_buf);
440 #ifdef FEAT_AUTOCMD
441 /* Autocommands may have deleted the buffer. */
442 if (!buf_valid(buf))
443 return;
444 # ifdef FEAT_EVAL
445 if (aborting()) /* autocmds may abort script processing */
446 return;
447 # endif
449 /* Autocommands may have opened or closed windows for this buffer.
450 * Decrement the count for the close we do here. */
451 if (buf->b_nwindows > 0)
452 --buf->b_nwindows;
455 * It's possible that autocommands change curbuf to the one being deleted.
456 * This might cause the previous curbuf to be deleted unexpectedly. But
457 * in some cases it's OK to delete the curbuf, because a new one is
458 * obtained anyway. Therefore only return if curbuf changed to the
459 * deleted buffer.
461 if (buf == curbuf && !is_curbuf)
462 return;
463 #endif
465 #ifdef FEAT_ODB_EDITOR
466 odb_buffer_close(buf);
467 #endif
469 /* Change directories when the 'acd' option is set. */
470 DO_AUTOCHDIR
473 * Remove the buffer from the list.
475 if (wipe_buf)
477 #ifdef FEAT_SUN_WORKSHOP
478 if (usingSunWorkShop)
479 workshop_file_closed_lineno((char *)buf->b_ffname,
480 (int)buf->b_last_cursor.lnum);
481 #endif
482 vim_free(buf->b_ffname);
483 vim_free(buf->b_sfname);
484 if (buf->b_prev == NULL)
485 firstbuf = buf->b_next;
486 else
487 buf->b_prev->b_next = buf->b_next;
488 if (buf->b_next == NULL)
489 lastbuf = buf->b_prev;
490 else
491 buf->b_next->b_prev = buf->b_prev;
492 free_buffer(buf);
494 else
496 if (del_buf)
498 /* Free all internal variables and reset option values, to make
499 * ":bdel" compatible with Vim 5.7. */
500 free_buffer_stuff(buf, TRUE);
502 /* Make it look like a new buffer. */
503 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
505 /* Init the options when loaded again. */
506 buf->b_p_initialized = FALSE;
508 buf_clear_file(buf);
509 if (del_buf)
510 buf->b_p_bl = FALSE;
515 * Make buffer not contain a file.
517 void
518 buf_clear_file(buf)
519 buf_T *buf;
521 buf->b_ml.ml_line_count = 1;
522 unchanged(buf, TRUE);
523 #ifndef SHORT_FNAME
524 buf->b_shortname = FALSE;
525 #endif
526 buf->b_p_eol = TRUE;
527 buf->b_start_eol = TRUE;
528 #ifdef FEAT_MBYTE
529 buf->b_p_bomb = FALSE;
530 buf->b_start_bomb = FALSE;
531 #endif
532 buf->b_ml.ml_mfp = NULL;
533 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
534 #ifdef FEAT_NETBEANS_INTG
535 netbeans_deleted_all_lines(buf);
536 #endif
540 * buf_freeall() - free all things allocated for a buffer that are related to
541 * the file.
543 /*ARGSUSED*/
544 void
545 buf_freeall(buf, del_buf, wipe_buf)
546 buf_T *buf;
547 int del_buf; /* buffer is going to be deleted */
548 int wipe_buf; /* buffer is going to be wiped out */
550 #ifdef FEAT_AUTOCMD
551 int is_curbuf = (buf == curbuf);
553 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, FALSE, buf);
554 if (!buf_valid(buf)) /* autocommands may delete the buffer */
555 return;
556 if (del_buf && buf->b_p_bl)
558 apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname, FALSE, buf);
559 if (!buf_valid(buf)) /* autocommands may delete the buffer */
560 return;
562 if (wipe_buf)
564 apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
565 FALSE, buf);
566 if (!buf_valid(buf)) /* autocommands may delete the buffer */
567 return;
569 # ifdef FEAT_EVAL
570 if (aborting()) /* autocmds may abort script processing */
571 return;
572 # endif
575 * It's possible that autocommands change curbuf to the one being deleted.
576 * This might cause curbuf to be deleted unexpectedly. But in some cases
577 * it's OK to delete the curbuf, because a new one is obtained anyway.
578 * Therefore only return if curbuf changed to the deleted buffer.
580 if (buf == curbuf && !is_curbuf)
581 return;
582 #endif
583 #ifdef FEAT_DIFF
584 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
585 #endif
587 #ifdef FEAT_FOLDING
588 /* No folds in an empty buffer. */
589 # ifdef FEAT_WINDOWS
591 win_T *win;
592 tabpage_T *tp;
594 FOR_ALL_TAB_WINDOWS(tp, win)
595 if (win->w_buffer == buf)
596 clearFolding(win);
598 # else
599 if (curwin->w_buffer == buf)
600 clearFolding(curwin);
601 # endif
602 #endif
604 #ifdef FEAT_TCL
605 tcl_buffer_free(buf);
606 #endif
607 u_blockfree(buf); /* free the memory allocated for undo */
608 ml_close(buf, TRUE); /* close and delete the memline/memfile */
609 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
610 u_clearall(buf); /* reset all undo information */
611 #ifdef FEAT_SYN_HL
612 syntax_clear(buf); /* reset syntax info */
613 #endif
614 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
618 * Free a buffer structure and the things it contains related to the buffer
619 * itself (not the file, that must have been done already).
621 static void
622 free_buffer(buf)
623 buf_T *buf;
625 free_buffer_stuff(buf, TRUE);
626 #ifdef FEAT_MZSCHEME
627 mzscheme_buffer_free(buf);
628 #endif
629 #ifdef FEAT_PERL
630 perl_buf_free(buf);
631 #endif
632 #ifdef FEAT_PYTHON
633 python_buffer_free(buf);
634 #endif
635 #ifdef FEAT_RUBY
636 ruby_buffer_free(buf);
637 #endif
638 #ifdef FEAT_AUTOCMD
639 aubuflocal_remove(buf);
640 #endif
641 vim_free(buf);
645 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
647 static void
648 free_buffer_stuff(buf, free_options)
649 buf_T *buf;
650 int free_options; /* free options as well */
652 if (free_options)
654 clear_wininfo(buf); /* including window-local options */
655 free_buf_options(buf, TRUE);
657 #ifdef FEAT_EVAL
658 vars_clear(&buf->b_vars.dv_hashtab); /* free all internal variables */
659 hash_init(&buf->b_vars.dv_hashtab);
660 #endif
661 #ifdef FEAT_USR_CMDS
662 uc_clear(&buf->b_ucmds); /* clear local user commands */
663 #endif
664 #ifdef FEAT_SIGNS
665 buf_delete_signs(buf); /* delete any signs */
666 #endif
667 #ifdef FEAT_NETBEANS_INTG
668 if (usingNetbeans)
669 netbeans_file_killed(buf);
670 #endif
671 #ifdef FEAT_LOCALMAP
672 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
673 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
674 #endif
675 #ifdef FEAT_MBYTE
676 vim_free(buf->b_start_fenc);
677 buf->b_start_fenc = NULL;
678 #endif
679 #ifdef FEAT_SPELL
680 ga_clear(&buf->b_langp);
681 #endif
685 * Free the b_wininfo list for buffer "buf".
687 static void
688 clear_wininfo(buf)
689 buf_T *buf;
691 wininfo_T *wip;
693 while (buf->b_wininfo != NULL)
695 wip = buf->b_wininfo;
696 buf->b_wininfo = wip->wi_next;
697 if (wip->wi_optset)
699 clear_winopt(&wip->wi_opt);
700 #ifdef FEAT_FOLDING
701 deleteFoldRecurse(&wip->wi_folds);
702 #endif
704 vim_free(wip);
708 #if defined(FEAT_LISTCMDS) || defined(PROTO)
710 * Go to another buffer. Handles the result of the ATTENTION dialog.
712 void
713 goto_buffer(eap, start, dir, count)
714 exarg_T *eap;
715 int start;
716 int dir;
717 int count;
719 # if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
720 buf_T *old_curbuf = curbuf;
722 swap_exists_action = SEA_DIALOG;
723 # endif
724 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
725 start, dir, count, eap->forceit);
726 # if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
727 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
729 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
730 cleanup_T cs;
732 /* Reset the error/interrupt/exception state here so that
733 * aborting() returns FALSE when closing a window. */
734 enter_cleanup(&cs);
735 # endif
737 /* Quitting means closing the split window, nothing else. */
738 win_close(curwin, TRUE);
739 swap_exists_action = SEA_NONE;
740 swap_exists_did_quit = TRUE;
742 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
743 /* Restore the error/interrupt/exception state if not discarded by a
744 * new aborting error, interrupt, or uncaught exception. */
745 leave_cleanup(&cs);
746 # endif
748 else
749 handle_swap_exists(old_curbuf);
750 # endif
752 #endif
754 #if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
756 * Handle the situation of swap_exists_action being set.
757 * It is allowed for "old_curbuf" to be NULL or invalid.
759 void
760 handle_swap_exists(old_curbuf)
761 buf_T *old_curbuf;
763 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
764 cleanup_T cs;
765 # endif
767 if (swap_exists_action == SEA_QUIT)
769 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
770 /* Reset the error/interrupt/exception state here so that
771 * aborting() returns FALSE when closing a buffer. */
772 enter_cleanup(&cs);
773 # endif
775 /* User selected Quit at ATTENTION prompt. Go back to previous
776 * buffer. If that buffer is gone or the same as the current one,
777 * open a new, empty buffer. */
778 swap_exists_action = SEA_NONE; /* don't want it again */
779 swap_exists_did_quit = TRUE;
780 close_buffer(curwin, curbuf, DOBUF_UNLOAD);
781 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
782 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
783 if (old_curbuf != NULL)
784 enter_buffer(old_curbuf);
785 /* If "old_curbuf" is NULL we are in big trouble here... */
787 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
788 /* Restore the error/interrupt/exception state if not discarded by a
789 * new aborting error, interrupt, or uncaught exception. */
790 leave_cleanup(&cs);
791 # endif
793 else if (swap_exists_action == SEA_RECOVER)
795 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
796 /* Reset the error/interrupt/exception state here so that
797 * aborting() returns FALSE when closing a buffer. */
798 enter_cleanup(&cs);
799 # endif
801 /* User selected Recover at ATTENTION prompt. */
802 msg_scroll = TRUE;
803 ml_recover();
804 MSG_PUTS("\n"); /* don't overwrite the last message */
805 cmdline_row = msg_row;
806 do_modelines(0);
808 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
809 /* Restore the error/interrupt/exception state if not discarded by a
810 * new aborting error, interrupt, or uncaught exception. */
811 leave_cleanup(&cs);
812 # endif
814 swap_exists_action = SEA_NONE;
816 #endif
818 #if defined(FEAT_LISTCMDS) || defined(PROTO)
820 * do_bufdel() - delete or unload buffer(s)
822 * addr_count == 0: ":bdel" - delete current buffer
823 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
824 * buffer "end_bnr", then any other arguments.
825 * addr_count == 2: ":N,N bdel" - delete buffers in range
827 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
828 * DOBUF_DEL (":bdel")
830 * Returns error message or NULL
832 char_u *
833 do_bufdel(command, arg, addr_count, start_bnr, end_bnr, forceit)
834 int command;
835 char_u *arg; /* pointer to extra arguments */
836 int addr_count;
837 int start_bnr; /* first buffer number in a range */
838 int end_bnr; /* buffer nr or last buffer nr in a range */
839 int forceit;
841 int do_current = 0; /* delete current buffer? */
842 int deleted = 0; /* number of buffers deleted */
843 char_u *errormsg = NULL; /* return value */
844 int bnr; /* buffer number */
845 char_u *p;
847 if (addr_count == 0)
849 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
851 else
853 if (addr_count == 2)
855 if (*arg) /* both range and argument is not allowed */
856 return (char_u *)_(e_trailing);
857 bnr = start_bnr;
859 else /* addr_count == 1 */
860 bnr = end_bnr;
862 for ( ;!got_int; ui_breakcheck())
865 * delete the current buffer last, otherwise when the
866 * current buffer is deleted, the next buffer becomes
867 * the current one and will be loaded, which may then
868 * also be deleted, etc.
870 if (bnr == curbuf->b_fnum)
871 do_current = bnr;
872 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
873 forceit) == OK)
874 ++deleted;
877 * find next buffer number to delete/unload
879 if (addr_count == 2)
881 if (++bnr > end_bnr)
882 break;
884 else /* addr_count == 1 */
886 arg = skipwhite(arg);
887 if (*arg == NUL)
888 break;
889 if (!VIM_ISDIGIT(*arg))
891 p = skiptowhite_esc(arg);
892 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE, FALSE);
893 if (bnr < 0) /* failed */
894 break;
895 arg = p;
897 else
898 bnr = getdigits(&arg);
901 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
902 FORWARD, do_current, forceit) == OK)
903 ++deleted;
905 if (deleted == 0)
907 if (command == DOBUF_UNLOAD)
908 STRCPY(IObuff, _("E515: No buffers were unloaded"));
909 else if (command == DOBUF_DEL)
910 STRCPY(IObuff, _("E516: No buffers were deleted"));
911 else
912 STRCPY(IObuff, _("E517: No buffers were wiped out"));
913 errormsg = IObuff;
915 else if (deleted >= p_report)
917 if (command == DOBUF_UNLOAD)
919 if (deleted == 1)
920 MSG(_("1 buffer unloaded"));
921 else
922 smsg((char_u *)_("%d buffers unloaded"), deleted);
924 else if (command == DOBUF_DEL)
926 if (deleted == 1)
927 MSG(_("1 buffer deleted"));
928 else
929 smsg((char_u *)_("%d buffers deleted"), deleted);
931 else
933 if (deleted == 1)
934 MSG(_("1 buffer wiped out"));
935 else
936 smsg((char_u *)_("%d buffers wiped out"), deleted);
942 return errormsg;
946 * Implementation of the commands for the buffer list.
948 * action == DOBUF_GOTO go to specified buffer
949 * action == DOBUF_SPLIT split window and go to specified buffer
950 * action == DOBUF_UNLOAD unload specified buffer(s)
951 * action == DOBUF_DEL delete specified buffer(s) from buffer list
952 * action == DOBUF_WIPE delete specified buffer(s) really
954 * start == DOBUF_CURRENT go to "count" buffer from current buffer
955 * start == DOBUF_FIRST go to "count" buffer from first buffer
956 * start == DOBUF_LAST go to "count" buffer from last buffer
957 * start == DOBUF_MOD go to "count" modified buffer from current buffer
959 * Return FAIL or OK.
962 do_buffer(action, start, dir, count, forceit)
963 int action;
964 int start;
965 int dir; /* FORWARD or BACKWARD */
966 int count; /* buffer number or number of buffers */
967 int forceit; /* TRUE for :...! */
969 buf_T *buf;
970 buf_T *bp;
971 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
972 || action == DOBUF_WIPE);
974 switch (start)
976 case DOBUF_FIRST: buf = firstbuf; break;
977 case DOBUF_LAST: buf = lastbuf; break;
978 default: buf = curbuf; break;
980 if (start == DOBUF_MOD) /* find next modified buffer */
982 while (count-- > 0)
986 buf = buf->b_next;
987 if (buf == NULL)
988 buf = firstbuf;
990 while (buf != curbuf && !bufIsChanged(buf));
992 if (!bufIsChanged(buf))
994 EMSG(_("E84: No modified buffer found"));
995 return FAIL;
998 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1000 while (buf != NULL && buf->b_fnum != count)
1001 buf = buf->b_next;
1003 else
1005 bp = NULL;
1006 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1008 /* remember the buffer where we start, we come back there when all
1009 * buffers are unlisted. */
1010 if (bp == NULL)
1011 bp = buf;
1012 if (dir == FORWARD)
1014 buf = buf->b_next;
1015 if (buf == NULL)
1016 buf = firstbuf;
1018 else
1020 buf = buf->b_prev;
1021 if (buf == NULL)
1022 buf = lastbuf;
1024 /* don't count unlisted buffers */
1025 if (unload || buf->b_p_bl)
1027 --count;
1028 bp = NULL; /* use this buffer as new starting point */
1030 if (bp == buf)
1032 /* back where we started, didn't find anything. */
1033 EMSG(_("E85: There is no listed buffer"));
1034 return FAIL;
1039 if (buf == NULL) /* could not find it */
1041 if (start == DOBUF_FIRST)
1043 /* don't warn when deleting */
1044 if (!unload)
1045 EMSGN(_("E86: Buffer %ld does not exist"), count);
1047 else if (dir == FORWARD)
1048 EMSG(_("E87: Cannot go beyond last buffer"));
1049 else
1050 EMSG(_("E88: Cannot go before first buffer"));
1051 return FAIL;
1054 #ifdef FEAT_GUI
1055 need_mouse_correct = TRUE;
1056 #endif
1058 #ifdef FEAT_LISTCMDS
1060 * delete buffer buf from memory and/or the list
1062 if (unload)
1064 int forward;
1065 int retval;
1067 /* When unloading or deleting a buffer that's already unloaded and
1068 * unlisted: fail silently. */
1069 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1070 return FAIL;
1072 if (!forceit && bufIsChanged(buf))
1074 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1075 if ((p_confirm || cmdmod.confirm) && p_write)
1077 dialog_changed(buf, FALSE);
1078 # ifdef FEAT_AUTOCMD
1079 if (!buf_valid(buf))
1080 /* Autocommand deleted buffer, oops! It's not changed
1081 * now. */
1082 return FAIL;
1083 # endif
1084 /* If it's still changed fail silently, the dialog already
1085 * mentioned why it fails. */
1086 if (bufIsChanged(buf))
1087 return FAIL;
1089 else
1090 #endif
1092 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1093 buf->b_fnum);
1094 return FAIL;
1099 * If deleting the last (listed) buffer, make it empty.
1100 * The last (listed) buffer cannot be unloaded.
1102 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1103 if (bp->b_p_bl && bp != buf)
1104 break;
1105 if (bp == NULL && buf == curbuf)
1107 if (action == DOBUF_UNLOAD)
1109 EMSG(_("E90: Cannot unload last buffer"));
1110 return FAIL;
1113 /* Close any other windows on this buffer, then make it empty. */
1114 #ifdef FEAT_WINDOWS
1115 close_windows(buf, TRUE);
1116 #endif
1117 setpcmark();
1118 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1119 forceit ? ECMD_FORCEIT : 0, curwin);
1122 * do_ecmd() may create a new buffer, then we have to delete
1123 * the old one. But do_ecmd() may have done that already, check
1124 * if the buffer still exists.
1126 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
1127 close_buffer(NULL, buf, action);
1128 return retval;
1131 #ifdef FEAT_WINDOWS
1133 * If the deleted buffer is the current one, close the current window
1134 * (unless it's the only window). Repeat this so long as we end up in
1135 * a window with this buffer.
1137 while (buf == curbuf
1138 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
1139 win_close(curwin, FALSE);
1140 #endif
1143 * If the buffer to be deleted is not the current one, delete it here.
1145 if (buf != curbuf)
1147 #ifdef FEAT_WINDOWS
1148 close_windows(buf, FALSE);
1149 #endif
1150 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
1151 close_buffer(NULL, buf, action);
1152 return OK;
1156 * Deleting the current buffer: Need to find another buffer to go to.
1157 * There must be another, otherwise it would have been handled above.
1158 * First use au_new_curbuf, if it is valid.
1159 * Then prefer the buffer we most recently visited.
1160 * Else try to find one that is loaded, after the current buffer,
1161 * then before the current buffer.
1162 * Finally use any buffer.
1164 buf = NULL; /* selected buffer */
1165 bp = NULL; /* used when no loaded buffer found */
1166 #ifdef FEAT_AUTOCMD
1167 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1168 buf = au_new_curbuf;
1169 # ifdef FEAT_JUMPLIST
1170 else
1171 # endif
1172 #endif
1173 #ifdef FEAT_JUMPLIST
1174 if (curwin->w_jumplistlen > 0)
1176 int jumpidx;
1178 jumpidx = curwin->w_jumplistidx - 1;
1179 if (jumpidx < 0)
1180 jumpidx = curwin->w_jumplistlen - 1;
1182 forward = jumpidx;
1183 while (jumpidx != curwin->w_jumplistidx)
1185 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1186 if (buf != NULL)
1188 if (buf == curbuf || !buf->b_p_bl)
1189 buf = NULL; /* skip current and unlisted bufs */
1190 else if (buf->b_ml.ml_mfp == NULL)
1192 /* skip unloaded buf, but may keep it for later */
1193 if (bp == NULL)
1194 bp = buf;
1195 buf = NULL;
1198 if (buf != NULL) /* found a valid buffer: stop searching */
1199 break;
1200 /* advance to older entry in jump list */
1201 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1202 break;
1203 if (--jumpidx < 0)
1204 jumpidx = curwin->w_jumplistlen - 1;
1205 if (jumpidx == forward) /* List exhausted for sure */
1206 break;
1209 #endif
1211 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1213 forward = TRUE;
1214 buf = curbuf->b_next;
1215 for (;;)
1217 if (buf == NULL)
1219 if (!forward) /* tried both directions */
1220 break;
1221 buf = curbuf->b_prev;
1222 forward = FALSE;
1223 continue;
1225 /* in non-help buffer, try to skip help buffers, and vv */
1226 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1228 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1229 break;
1230 if (bp == NULL) /* remember unloaded buf for later */
1231 bp = buf;
1233 if (forward)
1234 buf = buf->b_next;
1235 else
1236 buf = buf->b_prev;
1239 if (buf == NULL) /* No loaded buffer, use unloaded one */
1240 buf = bp;
1241 if (buf == NULL) /* No loaded buffer, find listed one */
1243 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1244 if (buf->b_p_bl && buf != curbuf)
1245 break;
1247 if (buf == NULL) /* Still no buffer, just take one */
1249 if (curbuf->b_next != NULL)
1250 buf = curbuf->b_next;
1251 else
1252 buf = curbuf->b_prev;
1257 * make buf current buffer
1259 if (action == DOBUF_SPLIT) /* split window first */
1261 # ifdef FEAT_WINDOWS
1262 /* If 'switchbuf' contains "useopen": jump to first window containing
1263 * "buf" if one exists */
1264 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
1265 return OK;
1266 /* If 'switchbuf' contains "usetab": jump to first window in any tab
1267 * page containing "buf" if one exists */
1268 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
1269 return OK;
1270 if (win_split(0, 0) == FAIL)
1271 # endif
1272 return FAIL;
1274 #endif
1276 /* go to current buffer - nothing to do */
1277 if (buf == curbuf)
1278 return OK;
1281 * Check if the current buffer may be abandoned.
1283 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1285 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1286 if ((p_confirm || cmdmod.confirm) && p_write)
1288 dialog_changed(curbuf, FALSE);
1289 # ifdef FEAT_AUTOCMD
1290 if (!buf_valid(buf))
1291 /* Autocommand deleted buffer, oops! */
1292 return FAIL;
1293 # endif
1295 if (bufIsChanged(curbuf))
1296 #endif
1298 EMSG(_(e_nowrtmsg));
1299 return FAIL;
1303 /* Go to the other buffer. */
1304 set_curbuf(buf, action);
1306 #if defined(FEAT_LISTCMDS) && defined(FEAT_SCROLLBIND)
1307 if (action == DOBUF_SPLIT)
1308 curwin->w_p_scb = FALSE; /* reset 'scrollbind' */
1309 #endif
1311 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1312 if (aborting()) /* autocmds may abort script processing */
1313 return FAIL;
1314 #endif
1316 return OK;
1319 #endif /* FEAT_LISTCMDS */
1322 * Set current buffer to "buf". Executes autocommands and closes current
1323 * buffer. "action" tells how to close the current buffer:
1324 * DOBUF_GOTO free or hide it
1325 * DOBUF_SPLIT nothing
1326 * DOBUF_UNLOAD unload it
1327 * DOBUF_DEL delete it
1328 * DOBUF_WIPE wipe it out
1330 void
1331 set_curbuf(buf, action)
1332 buf_T *buf;
1333 int action;
1335 buf_T *prevbuf;
1336 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1337 || action == DOBUF_WIPE);
1339 setpcmark();
1340 if (!cmdmod.keepalt)
1341 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
1342 buflist_altfpos(curwin); /* remember curpos */
1344 #ifdef FEAT_VISUAL
1345 /* Don't restart Select mode after switching to another buffer. */
1346 VIsual_reselect = FALSE;
1347 #endif
1349 /* close_windows() or apply_autocmds() may change curbuf */
1350 prevbuf = curbuf;
1352 #ifdef FEAT_AUTOCMD
1353 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1354 # ifdef FEAT_EVAL
1355 if (buf_valid(prevbuf) && !aborting())
1356 # else
1357 if (buf_valid(prevbuf))
1358 # endif
1359 #endif
1361 #ifdef FEAT_WINDOWS
1362 if (unload)
1363 close_windows(prevbuf, FALSE);
1364 #endif
1365 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1366 if (buf_valid(prevbuf) && !aborting())
1367 #else
1368 if (buf_valid(prevbuf))
1369 #endif
1371 if (prevbuf == curbuf)
1372 u_sync(FALSE);
1373 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1374 unload ? action : (action == DOBUF_GOTO
1375 && !P_HID(prevbuf)
1376 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0);
1379 #ifdef FEAT_AUTOCMD
1380 /* An autocommand may have deleted "buf", already entered it (e.g., when
1381 * it did ":bunload") or aborted the script processing! */
1382 # ifdef FEAT_EVAL
1383 if (buf_valid(buf) && buf != curbuf && !aborting())
1384 # else
1385 if (buf_valid(buf) && buf != curbuf)
1386 # endif
1387 #endif
1388 enter_buffer(buf);
1392 * Enter a new current buffer.
1393 * Old curbuf must have been abandoned already!
1395 void
1396 enter_buffer(buf)
1397 buf_T *buf;
1399 /* Copy buffer and window local option values. Not for a help buffer. */
1400 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1401 if (!buf->b_help)
1402 get_winopts(buf);
1403 #ifdef FEAT_FOLDING
1404 else
1405 /* Remove all folds in the window. */
1406 clearFolding(curwin);
1407 foldUpdateAll(curwin); /* update folds (later). */
1408 #endif
1410 /* Get the buffer in the current window. */
1411 curwin->w_buffer = buf;
1412 curbuf = buf;
1413 ++curbuf->b_nwindows;
1415 #ifdef FEAT_DIFF
1416 if (curwin->w_p_diff)
1417 diff_buf_add(curbuf);
1418 #endif
1420 /* Cursor on first line by default. */
1421 curwin->w_cursor.lnum = 1;
1422 curwin->w_cursor.col = 0;
1423 #ifdef FEAT_VIRTUALEDIT
1424 curwin->w_cursor.coladd = 0;
1425 #endif
1426 curwin->w_set_curswant = TRUE;
1427 #ifdef FEAT_AUTOCMD
1428 curwin->w_topline_was_set = FALSE;
1429 #endif
1431 /* Make sure the buffer is loaded. */
1432 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
1434 #ifdef FEAT_AUTOCMD
1435 /* If there is no filetype, allow for detecting one. Esp. useful for
1436 * ":ball" used in a autocommand. If there already is a filetype we
1437 * might prefer to keep it. */
1438 if (*curbuf->b_p_ft == NUL)
1439 did_filetype = FALSE;
1440 #endif
1442 open_buffer(FALSE, NULL);
1444 else
1446 if (!msg_silent)
1447 need_fileinfo = TRUE; /* display file info after redraw */
1448 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1449 #ifdef FEAT_AUTOCMD
1450 curwin->w_topline = 1;
1451 # ifdef FEAT_DIFF
1452 curwin->w_topfill = 0;
1453 # endif
1454 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1455 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1456 #endif
1459 /* If autocommands did not change the cursor position, restore cursor lnum
1460 * and possibly cursor col. */
1461 if (curwin->w_cursor.lnum == 1 && inindent(0))
1462 buflist_getfpos();
1464 check_arg_idx(curwin); /* check for valid arg_idx */
1465 #ifdef FEAT_TITLE
1466 maketitle();
1467 #endif
1468 #ifdef FEAT_AUTOCMD
1469 /* when autocmds didn't change it */
1470 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
1471 #endif
1472 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1474 #ifdef FEAT_NETBEANS_INTG
1475 /* Send fileOpened event because we've changed buffers. */
1476 if (usingNetbeans && isNetbeansBuffer(curbuf))
1477 netbeans_file_activated(curbuf);
1478 #endif
1480 /* Change directories when the 'acd' option is set. */
1481 DO_AUTOCHDIR
1483 #ifdef FEAT_KEYMAP
1484 if (curbuf->b_kmap_state & KEYMAP_INIT)
1485 keymap_init();
1486 #endif
1487 #ifdef FEAT_SPELL
1488 /* May need to set the spell language. Can only do this after the buffer
1489 * has been properly setup. */
1490 if (!curbuf->b_help && curwin->w_p_spell && *curbuf->b_p_spl != NUL)
1491 did_set_spelllang(curbuf);
1492 #endif
1494 redraw_later(NOT_VALID);
1497 #if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1499 * Change to the directory of the current buffer.
1501 void
1502 do_autochdir()
1504 if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK)
1505 shorten_fnames(TRUE);
1507 #endif
1510 * functions for dealing with the buffer list
1514 * Add a file name to the buffer list. Return a pointer to the buffer.
1515 * If the same file name already exists return a pointer to that buffer.
1516 * If it does not exist, or if fname == NULL, a new entry is created.
1517 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1518 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1519 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
1520 * This is the ONLY way to create a new buffer.
1522 static int top_file_num = 1; /* highest file number */
1524 buf_T *
1525 buflist_new(ffname, sfname, lnum, flags)
1526 char_u *ffname; /* full path of fname or relative */
1527 char_u *sfname; /* short fname or NULL */
1528 linenr_T lnum; /* preferred cursor line */
1529 int flags; /* BLN_ defines */
1531 buf_T *buf;
1532 #ifdef UNIX
1533 struct stat st;
1534 #endif
1536 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1539 * If file name already exists in the list, update the entry.
1541 #ifdef UNIX
1542 /* On Unix we can use inode numbers when the file exists. Works better
1543 * for hard links. */
1544 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1545 st.st_dev = (dev_T)-1;
1546 #endif
1547 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1548 #ifdef UNIX
1549 buflist_findname_stat(ffname, &st)
1550 #else
1551 buflist_findname(ffname)
1552 #endif
1553 ) != NULL)
1555 vim_free(ffname);
1556 if (lnum != 0)
1557 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1558 /* copy the options now, if 'cpo' doesn't have 's' and not done
1559 * already */
1560 buf_copy_options(buf, 0);
1561 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1563 buf->b_p_bl = TRUE;
1564 #ifdef FEAT_AUTOCMD
1565 if (!(flags & BLN_DUMMY))
1566 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1567 #endif
1569 return buf;
1573 * If the current buffer has no name and no contents, use the current
1574 * buffer. Otherwise: Need to allocate a new buffer structure.
1576 * This is the ONLY place where a new buffer structure is allocated!
1577 * (A spell file buffer is allocated in spell.c, but that's not a normal
1578 * buffer.)
1580 buf = NULL;
1581 if ((flags & BLN_CURBUF)
1582 && curbuf != NULL
1583 && curbuf->b_ffname == NULL
1584 && curbuf->b_nwindows <= 1
1585 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1587 buf = curbuf;
1588 #ifdef FEAT_AUTOCMD
1589 /* It's like this buffer is deleted. Watch out for autocommands that
1590 * change curbuf! If that happens, allocate a new buffer anyway. */
1591 if (curbuf->b_p_bl)
1592 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1593 if (buf == curbuf)
1594 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1595 # ifdef FEAT_EVAL
1596 if (aborting()) /* autocmds may abort script processing */
1597 return NULL;
1598 # endif
1599 #endif
1600 #ifdef FEAT_QUICKFIX
1601 # ifdef FEAT_AUTOCMD
1602 if (buf == curbuf)
1603 # endif
1605 /* Make sure 'bufhidden' and 'buftype' are empty */
1606 clear_string_option(&buf->b_p_bh);
1607 clear_string_option(&buf->b_p_bt);
1609 #endif
1611 if (buf != curbuf || curbuf == NULL)
1613 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1614 if (buf == NULL)
1616 vim_free(ffname);
1617 return NULL;
1621 if (ffname != NULL)
1623 buf->b_ffname = ffname;
1624 buf->b_sfname = vim_strsave(sfname);
1627 clear_wininfo(buf);
1628 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1630 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1631 || buf->b_wininfo == NULL)
1633 vim_free(buf->b_ffname);
1634 buf->b_ffname = NULL;
1635 vim_free(buf->b_sfname);
1636 buf->b_sfname = NULL;
1637 if (buf != curbuf)
1638 free_buffer(buf);
1639 return NULL;
1642 if (buf == curbuf)
1644 /* free all things allocated for this buffer */
1645 buf_freeall(buf, FALSE, FALSE);
1646 if (buf != curbuf) /* autocommands deleted the buffer! */
1647 return NULL;
1648 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1649 if (aborting()) /* autocmds may abort script processing */
1650 return NULL;
1651 #endif
1652 /* buf->b_nwindows = 0; why was this here? */
1653 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
1654 #ifdef FEAT_KEYMAP
1655 /* need to reload lmaps and set b:keymap_name */
1656 curbuf->b_kmap_state |= KEYMAP_INIT;
1657 #endif
1659 else
1662 * put new buffer at the end of the buffer list
1664 buf->b_next = NULL;
1665 if (firstbuf == NULL) /* buffer list is empty */
1667 buf->b_prev = NULL;
1668 firstbuf = buf;
1670 else /* append new buffer at end of list */
1672 lastbuf->b_next = buf;
1673 buf->b_prev = lastbuf;
1675 lastbuf = buf;
1677 buf->b_fnum = top_file_num++;
1678 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1680 EMSG(_("W14: Warning: List of file names overflow"));
1681 if (emsg_silent == 0)
1683 out_flush();
1684 ui_delay(3000L, TRUE); /* make sure it is noticed */
1686 top_file_num = 1;
1690 * Always copy the options from the current buffer.
1692 buf_copy_options(buf, BCO_ALWAYS);
1695 buf->b_wininfo->wi_fpos.lnum = lnum;
1696 buf->b_wininfo->wi_win = curwin;
1698 #ifdef FEAT_EVAL
1699 init_var_dict(&buf->b_vars, &buf->b_bufvar); /* init b: variables */
1700 #endif
1701 #ifdef FEAT_SYN_HL
1702 hash_init(&buf->b_keywtab);
1703 hash_init(&buf->b_keywtab_ic);
1704 #endif
1706 buf->b_fname = buf->b_sfname;
1707 #ifdef UNIX
1708 if (st.st_dev == (dev_T)-1)
1709 buf->b_dev = -1;
1710 else
1712 buf->b_dev = st.st_dev;
1713 buf->b_ino = st.st_ino;
1715 #endif
1716 buf->b_u_synced = TRUE;
1717 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
1718 if (flags & BLN_DUMMY)
1719 buf->b_flags |= BF_DUMMY;
1720 buf_clear_file(buf);
1721 clrallmarks(buf); /* clear marks */
1722 fmarks_check_names(buf); /* check file marks for this file */
1723 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1724 #ifdef FEAT_AUTOCMD
1725 if (!(flags & BLN_DUMMY))
1727 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
1728 if (flags & BLN_LISTED)
1729 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1730 # ifdef FEAT_EVAL
1731 if (aborting()) /* autocmds may abort script processing */
1732 return NULL;
1733 # endif
1735 #endif
1737 return buf;
1741 * Free the memory for the options of a buffer.
1742 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1743 * 'fileencoding'.
1745 void
1746 free_buf_options(buf, free_p_ff)
1747 buf_T *buf;
1748 int free_p_ff;
1750 if (free_p_ff)
1752 #ifdef FEAT_MBYTE
1753 clear_string_option(&buf->b_p_fenc);
1754 #endif
1755 clear_string_option(&buf->b_p_ff);
1756 #ifdef FEAT_QUICKFIX
1757 clear_string_option(&buf->b_p_bh);
1758 clear_string_option(&buf->b_p_bt);
1759 #endif
1761 #ifdef FEAT_FIND_ID
1762 clear_string_option(&buf->b_p_def);
1763 clear_string_option(&buf->b_p_inc);
1764 # ifdef FEAT_EVAL
1765 clear_string_option(&buf->b_p_inex);
1766 # endif
1767 #endif
1768 #if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1769 clear_string_option(&buf->b_p_inde);
1770 clear_string_option(&buf->b_p_indk);
1771 #endif
1772 #if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1773 clear_string_option(&buf->b_p_bexpr);
1774 #endif
1775 #if defined(FEAT_EVAL)
1776 clear_string_option(&buf->b_p_fex);
1777 #endif
1778 #ifdef FEAT_CRYPT
1779 clear_string_option(&buf->b_p_key);
1780 #endif
1781 clear_string_option(&buf->b_p_kp);
1782 clear_string_option(&buf->b_p_mps);
1783 clear_string_option(&buf->b_p_fo);
1784 clear_string_option(&buf->b_p_flp);
1785 clear_string_option(&buf->b_p_isk);
1786 #ifdef FEAT_KEYMAP
1787 clear_string_option(&buf->b_p_keymap);
1788 ga_clear(&buf->b_kmap_ga);
1789 #endif
1790 #ifdef FEAT_COMMENTS
1791 clear_string_option(&buf->b_p_com);
1792 #endif
1793 #ifdef FEAT_FOLDING
1794 clear_string_option(&buf->b_p_cms);
1795 #endif
1796 clear_string_option(&buf->b_p_nf);
1797 #ifdef FEAT_SYN_HL
1798 clear_string_option(&buf->b_p_syn);
1799 #endif
1800 #ifdef FEAT_SPELL
1801 clear_string_option(&buf->b_p_spc);
1802 clear_string_option(&buf->b_p_spf);
1803 vim_free(buf->b_cap_prog);
1804 buf->b_cap_prog = NULL;
1805 clear_string_option(&buf->b_p_spl);
1806 #endif
1807 #ifdef FEAT_SEARCHPATH
1808 clear_string_option(&buf->b_p_sua);
1809 #endif
1810 #ifdef FEAT_AUTOCMD
1811 clear_string_option(&buf->b_p_ft);
1812 #endif
1813 #ifdef FEAT_OSFILETYPE
1814 clear_string_option(&buf->b_p_oft);
1815 #endif
1816 #ifdef FEAT_CINDENT
1817 clear_string_option(&buf->b_p_cink);
1818 clear_string_option(&buf->b_p_cino);
1819 #endif
1820 #if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1821 clear_string_option(&buf->b_p_cinw);
1822 #endif
1823 #ifdef FEAT_INS_EXPAND
1824 clear_string_option(&buf->b_p_cpt);
1825 #endif
1826 #ifdef FEAT_COMPL_FUNC
1827 clear_string_option(&buf->b_p_cfu);
1828 clear_string_option(&buf->b_p_ofu);
1829 #endif
1830 #ifdef FEAT_QUICKFIX
1831 clear_string_option(&buf->b_p_gp);
1832 clear_string_option(&buf->b_p_mp);
1833 clear_string_option(&buf->b_p_efm);
1834 #endif
1835 clear_string_option(&buf->b_p_ep);
1836 clear_string_option(&buf->b_p_path);
1837 clear_string_option(&buf->b_p_tags);
1838 #ifdef FEAT_INS_EXPAND
1839 clear_string_option(&buf->b_p_dict);
1840 clear_string_option(&buf->b_p_tsr);
1841 #endif
1842 #ifdef FEAT_TEXTOBJ
1843 clear_string_option(&buf->b_p_qe);
1844 #endif
1845 buf->b_p_ar = -1;
1849 * get alternate file n
1850 * set linenr to lnum or altfpos.lnum if lnum == 0
1851 * also set cursor column to altfpos.col if 'startofline' is not set.
1852 * if (options & GETF_SETMARK) call setpcmark()
1853 * if (options & GETF_ALT) we are jumping to an alternate file.
1854 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1856 * return FAIL for failure, OK for success
1859 buflist_getfile(n, lnum, options, forceit)
1860 int n;
1861 linenr_T lnum;
1862 int options;
1863 int forceit;
1865 buf_T *buf;
1866 #ifdef FEAT_WINDOWS
1867 win_T *wp = NULL;
1868 #endif
1869 pos_T *fpos;
1870 colnr_T col;
1872 buf = buflist_findnr(n);
1873 if (buf == NULL)
1875 if ((options & GETF_ALT) && n == 0)
1876 EMSG(_(e_noalt));
1877 else
1878 EMSGN(_("E92: Buffer %ld not found"), n);
1879 return FAIL;
1882 /* if alternate file is the current buffer, nothing to do */
1883 if (buf == curbuf)
1884 return OK;
1886 if (text_locked())
1888 text_locked_msg();
1889 return FAIL;
1891 #ifdef FEAT_AUTOCMD
1892 if (curbuf_locked())
1893 return FAIL;
1894 #endif
1896 /* altfpos may be changed by getfile(), get it now */
1897 if (lnum == 0)
1899 fpos = buflist_findfpos(buf);
1900 lnum = fpos->lnum;
1901 col = fpos->col;
1903 else
1904 col = 0;
1906 #ifdef FEAT_WINDOWS
1907 if (options & GETF_SWITCH)
1909 /* If 'switchbuf' contains "useopen": jump to first window containing
1910 * "buf" if one exists */
1911 if (swb_flags & SWB_USEOPEN)
1912 wp = buf_jump_open_win(buf);
1913 /* If 'switchbuf' contians "usetab": jump to first window in any tab
1914 * page containing "buf" if one exists */
1915 if (wp == NULL && (swb_flags & SWB_USETAB))
1916 wp = buf_jump_open_tab(buf);
1917 /* If 'switchbuf' contains "split" or "newtab" and the current buffer
1918 * isn't empty: open new window */
1919 if (wp == NULL && (swb_flags & (SWB_SPLIT | SWB_NEWTAB)) && !bufempty())
1921 if (swb_flags & SWB_NEWTAB) /* Open in a new tab */
1922 tabpage_new();
1923 else if (win_split(0, 0) == FAIL) /* Open in a new window */
1924 return FAIL;
1925 # ifdef FEAT_SCROLLBIND
1926 curwin->w_p_scb = FALSE;
1927 # endif
1930 #endif
1932 ++RedrawingDisabled;
1933 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
1934 lnum, forceit) <= 0)
1936 --RedrawingDisabled;
1938 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
1939 if (!p_sol && col != 0)
1941 curwin->w_cursor.col = col;
1942 check_cursor_col();
1943 #ifdef FEAT_VIRTUALEDIT
1944 curwin->w_cursor.coladd = 0;
1945 #endif
1946 curwin->w_set_curswant = TRUE;
1948 return OK;
1950 --RedrawingDisabled;
1951 return FAIL;
1955 * go to the last know line number for the current buffer
1957 void
1958 buflist_getfpos()
1960 pos_T *fpos;
1962 fpos = buflist_findfpos(curbuf);
1964 curwin->w_cursor.lnum = fpos->lnum;
1965 check_cursor_lnum();
1967 if (p_sol)
1968 curwin->w_cursor.col = 0;
1969 else
1971 curwin->w_cursor.col = fpos->col;
1972 check_cursor_col();
1973 #ifdef FEAT_VIRTUALEDIT
1974 curwin->w_cursor.coladd = 0;
1975 #endif
1976 curwin->w_set_curswant = TRUE;
1980 #if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
1982 * Find file in buffer list by name (it has to be for the current window).
1983 * Returns NULL if not found.
1985 buf_T *
1986 buflist_findname_exp(fname)
1987 char_u *fname;
1989 char_u *ffname;
1990 buf_T *buf = NULL;
1992 /* First make the name into a full path name */
1993 ffname = FullName_save(fname,
1994 #ifdef UNIX
1995 TRUE /* force expansion, get rid of symbolic links */
1996 #else
1997 FALSE
1998 #endif
2000 if (ffname != NULL)
2002 buf = buflist_findname(ffname);
2003 vim_free(ffname);
2005 return buf;
2007 #endif
2010 * Find file in buffer list by name (it has to be for the current window).
2011 * "ffname" must have a full path.
2012 * Skips dummy buffers.
2013 * Returns NULL if not found.
2015 buf_T *
2016 buflist_findname(ffname)
2017 char_u *ffname;
2019 #ifdef UNIX
2020 struct stat st;
2022 if (mch_stat((char *)ffname, &st) < 0)
2023 st.st_dev = (dev_T)-1;
2024 return buflist_findname_stat(ffname, &st);
2028 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2029 * twice for the same file.
2030 * Returns NULL if not found.
2032 static buf_T *
2033 buflist_findname_stat(ffname, stp)
2034 char_u *ffname;
2035 struct stat *stp;
2037 #endif
2038 buf_T *buf;
2040 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2041 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
2042 #ifdef UNIX
2043 , stp
2044 #endif
2046 return buf;
2047 return NULL;
2050 #if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO)
2052 * Find file in buffer list by a regexp pattern.
2053 * Return fnum of the found buffer.
2054 * Return < 0 for error.
2056 /*ARGSUSED*/
2058 buflist_findpat(pattern, pattern_end, unlisted, diffmode)
2059 char_u *pattern;
2060 char_u *pattern_end; /* pointer to first char after pattern */
2061 int unlisted; /* find unlisted buffers */
2062 int diffmode; /* find diff-mode buffers only */
2064 buf_T *buf;
2065 regprog_T *prog;
2066 int match = -1;
2067 int find_listed;
2068 char_u *pat;
2069 char_u *patend;
2070 int attempt;
2071 char_u *p;
2072 int toggledollar;
2074 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2076 if (*pattern == '%')
2077 match = curbuf->b_fnum;
2078 else
2079 match = curwin->w_alt_fnum;
2080 #ifdef FEAT_DIFF
2081 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2082 match = -1;
2083 #endif
2087 * Try four ways of matching a listed buffer:
2088 * attempt == 0: without '^' or '$' (at any position)
2089 * attempt == 1: with '^' at start (only at position 0)
2090 * attempt == 2: with '$' at end (only match at end)
2091 * attempt == 3: with '^' at start and '$' at end (only full match)
2092 * Repeat this for finding an unlisted buffer if there was no matching
2093 * listed buffer.
2095 else
2097 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2098 if (pat == NULL)
2099 return -1;
2100 patend = pat + STRLEN(pat) - 1;
2101 toggledollar = (patend > pat && *patend == '$');
2103 /* First try finding a listed buffer. If not found and "unlisted"
2104 * is TRUE, try finding an unlisted buffer. */
2105 find_listed = TRUE;
2106 for (;;)
2108 for (attempt = 0; attempt <= 3; ++attempt)
2110 /* may add '^' and '$' */
2111 if (toggledollar)
2112 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2113 p = pat;
2114 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2115 ++p;
2116 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2117 if (prog == NULL)
2119 vim_free(pat);
2120 return -1;
2123 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2124 if (buf->b_p_bl == find_listed
2125 #ifdef FEAT_DIFF
2126 && (!diffmode || diff_mode_buf(buf))
2127 #endif
2128 && buflist_match(prog, buf) != NULL)
2130 if (match >= 0) /* already found a match */
2132 match = -2;
2133 break;
2135 match = buf->b_fnum; /* remember first match */
2138 vim_free(prog);
2139 if (match >= 0) /* found one match */
2140 break;
2143 /* Only search for unlisted buffers if there was no match with
2144 * a listed buffer. */
2145 if (!unlisted || !find_listed || match != -1)
2146 break;
2147 find_listed = FALSE;
2150 vim_free(pat);
2153 if (match == -2)
2154 EMSG2(_("E93: More than one match for %s"), pattern);
2155 else if (match < 0)
2156 EMSG2(_("E94: No matching buffer for %s"), pattern);
2157 return match;
2159 #endif
2161 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2164 * Find all buffer names that match.
2165 * For command line expansion of ":buf" and ":sbuf".
2166 * Return OK if matches found, FAIL otherwise.
2169 ExpandBufnames(pat, num_file, file, options)
2170 char_u *pat;
2171 int *num_file;
2172 char_u ***file;
2173 int options;
2175 int count = 0;
2176 buf_T *buf;
2177 int round;
2178 char_u *p;
2179 int attempt;
2180 regprog_T *prog;
2181 char_u *patc;
2183 *num_file = 0; /* return values in case of FAIL */
2184 *file = NULL;
2186 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2187 if (*pat == '^')
2189 patc = alloc((unsigned)STRLEN(pat) + 11);
2190 if (patc == NULL)
2191 return FAIL;
2192 STRCPY(patc, "\\(^\\|[\\/]\\)");
2193 STRCPY(patc + 11, pat + 1);
2195 else
2196 patc = pat;
2199 * attempt == 0: try match with '\<', match at start of word
2200 * attempt == 1: try match without '\<', match anywhere
2202 for (attempt = 0; attempt <= 1; ++attempt)
2204 if (attempt > 0 && patc == pat)
2205 break; /* there was no anchor, no need to try again */
2206 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2207 if (prog == NULL)
2209 if (patc != pat)
2210 vim_free(patc);
2211 return FAIL;
2215 * round == 1: Count the matches.
2216 * round == 2: Build the array to keep the matches.
2218 for (round = 1; round <= 2; ++round)
2220 count = 0;
2221 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2223 if (!buf->b_p_bl) /* skip unlisted buffers */
2224 continue;
2225 p = buflist_match(prog, buf);
2226 if (p != NULL)
2228 if (round == 1)
2229 ++count;
2230 else
2232 if (options & WILD_HOME_REPLACE)
2233 p = home_replace_save(buf, p);
2234 else
2235 p = vim_strsave(p);
2236 (*file)[count++] = p;
2240 if (count == 0) /* no match found, break here */
2241 break;
2242 if (round == 1)
2244 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2245 if (*file == NULL)
2247 vim_free(prog);
2248 if (patc != pat)
2249 vim_free(patc);
2250 return FAIL;
2254 vim_free(prog);
2255 if (count) /* match(es) found, break here */
2256 break;
2259 if (patc != pat)
2260 vim_free(patc);
2262 *num_file = count;
2263 return (count == 0 ? FAIL : OK);
2266 #endif /* FEAT_CMDL_COMPL */
2268 #ifdef HAVE_BUFLIST_MATCH
2270 * Check for a match on the file name for buffer "buf" with regprog "prog".
2272 static char_u *
2273 buflist_match(prog, buf)
2274 regprog_T *prog;
2275 buf_T *buf;
2277 char_u *match;
2279 /* First try the short file name, then the long file name. */
2280 match = fname_match(prog, buf->b_sfname);
2281 if (match == NULL)
2282 match = fname_match(prog, buf->b_ffname);
2284 return match;
2288 * Try matching the regexp in "prog" with file name "name".
2289 * Return "name" when there is a match, NULL when not.
2291 static char_u *
2292 fname_match(prog, name)
2293 regprog_T *prog;
2294 char_u *name;
2296 char_u *match = NULL;
2297 char_u *p;
2298 regmatch_T regmatch;
2300 if (name != NULL)
2302 regmatch.regprog = prog;
2303 #ifdef CASE_INSENSITIVE_FILENAME
2304 regmatch.rm_ic = TRUE; /* Always ignore case */
2305 #else
2306 regmatch.rm_ic = FALSE; /* Never ignore case */
2307 #endif
2309 if (vim_regexec(&regmatch, name, (colnr_T)0))
2310 match = name;
2311 else
2313 /* Replace $(HOME) with '~' and try matching again. */
2314 p = home_replace_save(NULL, name);
2315 if (p != NULL && vim_regexec(&regmatch, p, (colnr_T)0))
2316 match = name;
2317 vim_free(p);
2321 return match;
2323 #endif
2326 * find file in buffer list by number
2328 buf_T *
2329 buflist_findnr(nr)
2330 int nr;
2332 buf_T *buf;
2334 if (nr == 0)
2335 nr = curwin->w_alt_fnum;
2336 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2337 if (buf->b_fnum == nr)
2338 return (buf);
2339 return NULL;
2343 * Get name of file 'n' in the buffer list.
2344 * When the file has no name an empty string is returned.
2345 * home_replace() is used to shorten the file name (used for marks).
2346 * Returns a pointer to allocated memory, of NULL when failed.
2348 char_u *
2349 buflist_nr2name(n, fullname, helptail)
2350 int n;
2351 int fullname;
2352 int helptail; /* for help buffers return tail only */
2354 buf_T *buf;
2356 buf = buflist_findnr(n);
2357 if (buf == NULL)
2358 return NULL;
2359 return home_replace_save(helptail ? buf : NULL,
2360 fullname ? buf->b_ffname : buf->b_fname);
2364 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2365 * When "copy_options" is TRUE save the local window option values.
2366 * When "lnum" is 0 only do the options.
2368 static void
2369 buflist_setfpos(buf, win, lnum, col, copy_options)
2370 buf_T *buf;
2371 win_T *win;
2372 linenr_T lnum;
2373 colnr_T col;
2374 int copy_options;
2376 wininfo_T *wip;
2378 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2379 if (wip->wi_win == win)
2380 break;
2381 if (wip == NULL)
2383 /* allocate a new entry */
2384 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2385 if (wip == NULL)
2386 return;
2387 wip->wi_win = win;
2388 if (lnum == 0) /* set lnum even when it's 0 */
2389 lnum = 1;
2391 else
2393 /* remove the entry from the list */
2394 if (wip->wi_prev)
2395 wip->wi_prev->wi_next = wip->wi_next;
2396 else
2397 buf->b_wininfo = wip->wi_next;
2398 if (wip->wi_next)
2399 wip->wi_next->wi_prev = wip->wi_prev;
2400 if (copy_options && wip->wi_optset)
2402 clear_winopt(&wip->wi_opt);
2403 #ifdef FEAT_FOLDING
2404 deleteFoldRecurse(&wip->wi_folds);
2405 #endif
2408 if (lnum != 0)
2410 wip->wi_fpos.lnum = lnum;
2411 wip->wi_fpos.col = col;
2413 if (copy_options)
2415 /* Save the window-specific option values. */
2416 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2417 #ifdef FEAT_FOLDING
2418 wip->wi_fold_manual = win->w_fold_manual;
2419 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2420 #endif
2421 wip->wi_optset = TRUE;
2424 /* insert the entry in front of the list */
2425 wip->wi_next = buf->b_wininfo;
2426 buf->b_wininfo = wip;
2427 wip->wi_prev = NULL;
2428 if (wip->wi_next)
2429 wip->wi_next->wi_prev = wip;
2431 return;
2434 #ifdef FEAT_DIFF
2435 static int wininfo_other_tab_diff __ARGS((wininfo_T *wip));
2438 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2439 * page. That's because a diff is local to a tab page.
2441 static int
2442 wininfo_other_tab_diff(wip)
2443 wininfo_T *wip;
2445 win_T *wp;
2447 if (wip->wi_opt.wo_diff)
2449 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2450 /* return FALSE when it's a window in the current tab page, thus
2451 * the buffer was in diff mode here */
2452 if (wip->wi_win == wp)
2453 return FALSE;
2454 return TRUE;
2456 return FALSE;
2458 #endif
2461 * Find info for the current window in buffer "buf".
2462 * If not found, return the info for the most recently used window.
2463 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2464 * another tab page.
2465 * Returns NULL when there isn't any info.
2467 /*ARGSUSED*/
2468 static wininfo_T *
2469 find_wininfo(buf, skip_diff_buffer)
2470 buf_T *buf;
2471 int skip_diff_buffer;
2473 wininfo_T *wip;
2475 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2476 if (wip->wi_win == curwin
2477 #ifdef FEAT_DIFF
2478 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2479 #endif
2481 break;
2483 /* If no wininfo for curwin, use the first in the list (that doesn't have
2484 * 'diff' set and is in another tab page). */
2485 if (wip == NULL)
2487 #ifdef FEAT_DIFF
2488 if (skip_diff_buffer)
2490 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2491 if (!wininfo_other_tab_diff(wip))
2492 break;
2494 else
2495 #endif
2496 wip = buf->b_wininfo;
2498 return wip;
2502 * Reset the local window options to the values last used in this window.
2503 * If the buffer wasn't used in this window before, use the values from
2504 * the most recently used window. If the values were never set, use the
2505 * global values for the window.
2507 void
2508 get_winopts(buf)
2509 buf_T *buf;
2511 wininfo_T *wip;
2513 clear_winopt(&curwin->w_onebuf_opt);
2514 #ifdef FEAT_FOLDING
2515 clearFolding(curwin);
2516 #endif
2518 wip = find_wininfo(buf, TRUE);
2519 if (wip != NULL && wip->wi_optset)
2521 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2522 #ifdef FEAT_FOLDING
2523 curwin->w_fold_manual = wip->wi_fold_manual;
2524 curwin->w_foldinvalid = TRUE;
2525 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2526 #endif
2528 else
2529 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2531 #ifdef FEAT_FOLDING
2532 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2533 if (p_fdls >= 0)
2534 curwin->w_p_fdl = p_fdls;
2535 #endif
2539 * Find the position (lnum and col) for the buffer 'buf' for the current
2540 * window.
2541 * Returns a pointer to no_position if no position is found.
2543 pos_T *
2544 buflist_findfpos(buf)
2545 buf_T *buf;
2547 wininfo_T *wip;
2548 static pos_T no_position = {1, 0};
2550 wip = find_wininfo(buf, FALSE);
2551 if (wip != NULL)
2552 return &(wip->wi_fpos);
2553 else
2554 return &no_position;
2558 * Find the lnum for the buffer 'buf' for the current window.
2560 linenr_T
2561 buflist_findlnum(buf)
2562 buf_T *buf;
2564 return buflist_findfpos(buf)->lnum;
2567 #if defined(FEAT_LISTCMDS) || defined(PROTO)
2569 * List all know file names (for :files and :buffers command).
2571 /*ARGSUSED*/
2572 void
2573 buflist_list(eap)
2574 exarg_T *eap;
2576 buf_T *buf;
2577 int len;
2578 int i;
2580 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2582 /* skip unlisted buffers, unless ! was used */
2583 if (!buf->b_p_bl && !eap->forceit)
2584 continue;
2585 msg_putchar('\n');
2586 if (buf_spname(buf) != NULL)
2587 STRCPY(NameBuff, buf_spname(buf));
2588 else
2589 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2591 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
2592 buf->b_fnum,
2593 buf->b_p_bl ? ' ' : 'u',
2594 buf == curbuf ? '%' :
2595 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2596 buf->b_ml.ml_mfp == NULL ? ' ' :
2597 (buf->b_nwindows == 0 ? 'h' : 'a'),
2598 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2599 (buf->b_flags & BF_READERR) ? 'x'
2600 : (bufIsChanged(buf) ? '+' : ' '),
2601 NameBuff);
2603 /* put "line 999" in column 40 or after the file name */
2604 i = 40 - vim_strsize(IObuff);
2607 IObuff[len++] = ' ';
2608 } while (--i > 0 && len < IOSIZE - 18);
2609 vim_snprintf((char *)IObuff + len, IOSIZE - len, _("line %ld"),
2610 buf == curbuf ? curwin->w_cursor.lnum
2611 : (long)buflist_findlnum(buf));
2612 msg_outtrans(IObuff);
2613 out_flush(); /* output one line at a time */
2614 ui_breakcheck();
2617 #endif
2620 * Get file name and line number for file 'fnum'.
2621 * Used by DoOneCmd() for translating '%' and '#'.
2622 * Used by insert_reg() and cmdline_paste() for '#' register.
2623 * Return FAIL if not found, OK for success.
2626 buflist_name_nr(fnum, fname, lnum)
2627 int fnum;
2628 char_u **fname;
2629 linenr_T *lnum;
2631 buf_T *buf;
2633 buf = buflist_findnr(fnum);
2634 if (buf == NULL || buf->b_fname == NULL)
2635 return FAIL;
2637 *fname = buf->b_fname;
2638 *lnum = buflist_findlnum(buf);
2640 return OK;
2644 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2645 * The file name with the full path is also remembered, for when :cd is used.
2646 * Returns FAIL for failure (file name already in use by other buffer)
2647 * OK otherwise.
2650 setfname(buf, ffname, sfname, message)
2651 buf_T *buf;
2652 char_u *ffname, *sfname;
2653 int message; /* give message when buffer already exists */
2655 buf_T *obuf = NULL;
2656 #ifdef UNIX
2657 struct stat st;
2658 #endif
2660 if (ffname == NULL || *ffname == NUL)
2662 /* Removing the name. */
2663 vim_free(buf->b_ffname);
2664 vim_free(buf->b_sfname);
2665 buf->b_ffname = NULL;
2666 buf->b_sfname = NULL;
2667 #ifdef UNIX
2668 st.st_dev = (dev_T)-1;
2669 #endif
2671 else
2673 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2674 if (ffname == NULL) /* out of memory */
2675 return FAIL;
2678 * if the file name is already used in another buffer:
2679 * - if the buffer is loaded, fail
2680 * - if the buffer is not loaded, delete it from the list
2682 #ifdef UNIX
2683 if (mch_stat((char *)ffname, &st) < 0)
2684 st.st_dev = (dev_T)-1;
2685 #endif
2686 if (!(buf->b_flags & BF_DUMMY))
2687 #ifdef UNIX
2688 obuf = buflist_findname_stat(ffname, &st);
2689 #else
2690 obuf = buflist_findname(ffname);
2691 #endif
2692 if (obuf != NULL && obuf != buf)
2694 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2696 if (message)
2697 EMSG(_("E95: Buffer with this name already exists"));
2698 vim_free(ffname);
2699 return FAIL;
2701 close_buffer(NULL, obuf, DOBUF_WIPE); /* delete from the list */
2703 sfname = vim_strsave(sfname);
2704 if (ffname == NULL || sfname == NULL)
2706 vim_free(sfname);
2707 vim_free(ffname);
2708 return FAIL;
2710 #ifdef USE_FNAME_CASE
2711 # ifdef USE_LONG_FNAME
2712 if (USE_LONG_FNAME)
2713 # endif
2714 fname_case(sfname, 0); /* set correct case for short file name */
2715 #endif
2716 vim_free(buf->b_ffname);
2717 vim_free(buf->b_sfname);
2718 buf->b_ffname = ffname;
2719 buf->b_sfname = sfname;
2721 buf->b_fname = buf->b_sfname;
2722 #ifdef UNIX
2723 if (st.st_dev == (dev_T)-1)
2724 buf->b_dev = -1;
2725 else
2727 buf->b_dev = st.st_dev;
2728 buf->b_ino = st.st_ino;
2730 #endif
2732 #ifndef SHORT_FNAME
2733 buf->b_shortname = FALSE;
2734 #endif
2736 buf_name_changed(buf);
2737 return OK;
2741 * Crude way of changing the name of a buffer. Use with care!
2742 * The name should be relative to the current directory.
2744 void
2745 buf_set_name(fnum, name)
2746 int fnum;
2747 char_u *name;
2749 buf_T *buf;
2751 buf = buflist_findnr(fnum);
2752 if (buf != NULL)
2754 vim_free(buf->b_sfname);
2755 vim_free(buf->b_ffname);
2756 buf->b_ffname = vim_strsave(name);
2757 buf->b_sfname = NULL;
2758 /* Allocate ffname and expand into full path. Also resolves .lnk
2759 * files on Win32. */
2760 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
2761 buf->b_fname = buf->b_sfname;
2766 * Take care of what needs to be done when the name of buffer "buf" has
2767 * changed.
2769 void
2770 buf_name_changed(buf)
2771 buf_T *buf;
2774 * If the file name changed, also change the name of the swapfile
2776 if (buf->b_ml.ml_mfp != NULL)
2777 ml_setname(buf);
2779 if (curwin->w_buffer == buf)
2780 check_arg_idx(curwin); /* check file name for arg list */
2781 #ifdef FEAT_TITLE
2782 maketitle(); /* set window title */
2783 #endif
2784 #ifdef FEAT_WINDOWS
2785 status_redraw_all(); /* status lines need to be redrawn */
2786 #endif
2787 fmarks_check_names(buf); /* check named file marks */
2788 ml_timestamp(buf); /* reset timestamp */
2792 * set alternate file name for current window
2794 * Used by do_one_cmd(), do_write() and do_ecmd().
2795 * Return the buffer.
2797 buf_T *
2798 setaltfname(ffname, sfname, lnum)
2799 char_u *ffname;
2800 char_u *sfname;
2801 linenr_T lnum;
2803 buf_T *buf;
2805 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2806 buf = buflist_new(ffname, sfname, lnum, 0);
2807 if (buf != NULL && !cmdmod.keepalt)
2808 curwin->w_alt_fnum = buf->b_fnum;
2809 return buf;
2813 * Get alternate file name for current window.
2814 * Return NULL if there isn't any, and give error message if requested.
2816 char_u *
2817 getaltfname(errmsg)
2818 int errmsg; /* give error message */
2820 char_u *fname;
2821 linenr_T dummy;
2823 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2825 if (errmsg)
2826 EMSG(_(e_noalt));
2827 return NULL;
2829 return fname;
2833 * Add a file name to the buflist and return its number.
2834 * Uses same flags as buflist_new(), except BLN_DUMMY.
2836 * used by qf_init(), main() and doarglist()
2839 buflist_add(fname, flags)
2840 char_u *fname;
2841 int flags;
2843 buf_T *buf;
2845 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
2846 if (buf != NULL)
2847 return buf->b_fnum;
2848 return 0;
2851 #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2853 * Adjust slashes in file names. Called after 'shellslash' was set.
2855 void
2856 buflist_slash_adjust()
2858 buf_T *bp;
2860 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
2862 if (bp->b_ffname != NULL)
2863 slash_adjust(bp->b_ffname);
2864 if (bp->b_sfname != NULL)
2865 slash_adjust(bp->b_sfname);
2868 #endif
2871 * Set alternate cursor position for the current buffer and window "win".
2872 * Also save the local window option values.
2874 void
2875 buflist_altfpos(win)
2876 win_T *win;
2878 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
2882 * Return TRUE if 'ffname' is not the same file as current file.
2883 * Fname must have a full path (expanded by mch_FullName()).
2886 otherfile(ffname)
2887 char_u *ffname;
2889 return otherfile_buf(curbuf, ffname
2890 #ifdef UNIX
2891 , NULL
2892 #endif
2896 static int
2897 otherfile_buf(buf, ffname
2898 #ifdef UNIX
2899 , stp
2900 #endif
2902 buf_T *buf;
2903 char_u *ffname;
2904 #ifdef UNIX
2905 struct stat *stp;
2906 #endif
2908 /* no name is different */
2909 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
2910 return TRUE;
2911 if (fnamecmp(ffname, buf->b_ffname) == 0)
2912 return FALSE;
2913 #ifdef UNIX
2915 struct stat st;
2917 /* If no struct stat given, get it now */
2918 if (stp == NULL)
2920 if (buf->b_dev < 0 || mch_stat((char *)ffname, &st) < 0)
2921 st.st_dev = (dev_T)-1;
2922 stp = &st;
2924 /* Use dev/ino to check if the files are the same, even when the names
2925 * are different (possible with links). Still need to compare the
2926 * name above, for when the file doesn't exist yet.
2927 * Problem: The dev/ino changes when a file is deleted (and created
2928 * again) and remains the same when renamed/moved. We don't want to
2929 * mch_stat() each buffer each time, that would be too slow. Get the
2930 * dev/ino again when they appear to match, but not when they appear
2931 * to be different: Could skip a buffer when it's actually the same
2932 * file. */
2933 if (buf_same_ino(buf, stp))
2935 buf_setino(buf);
2936 if (buf_same_ino(buf, stp))
2937 return FALSE;
2940 #endif
2941 return TRUE;
2944 #if defined(UNIX) || defined(PROTO)
2946 * Set inode and device number for a buffer.
2947 * Must always be called when b_fname is changed!.
2949 void
2950 buf_setino(buf)
2951 buf_T *buf;
2953 struct stat st;
2955 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
2957 buf->b_dev = st.st_dev;
2958 buf->b_ino = st.st_ino;
2960 else
2961 buf->b_dev = -1;
2965 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
2967 static int
2968 buf_same_ino(buf, stp)
2969 buf_T *buf;
2970 struct stat *stp;
2972 return (buf->b_dev >= 0
2973 && stp->st_dev == buf->b_dev
2974 && stp->st_ino == buf->b_ino);
2976 #endif
2979 * Print info about the current buffer.
2981 void
2982 fileinfo(fullname, shorthelp, dont_truncate)
2983 int fullname; /* when non-zero print full path */
2984 int shorthelp;
2985 int dont_truncate;
2987 char_u *name;
2988 int n;
2989 char_u *p;
2990 char_u *buffer;
2991 size_t len;
2993 buffer = alloc(IOSIZE);
2994 if (buffer == NULL)
2995 return;
2997 if (fullname > 1) /* 2 CTRL-G: include buffer number */
2999 sprintf((char *)buffer, "buf %d: ", curbuf->b_fnum);
3000 p = buffer + STRLEN(buffer);
3002 else
3003 p = buffer;
3005 *p++ = '"';
3006 if (buf_spname(curbuf) != NULL)
3007 STRCPY(p, buf_spname(curbuf));
3008 else
3010 if (!fullname && curbuf->b_fname != NULL)
3011 name = curbuf->b_fname;
3012 else
3013 name = curbuf->b_ffname;
3014 home_replace(shorthelp ? curbuf : NULL, name, p,
3015 (int)(IOSIZE - (p - buffer)), TRUE);
3018 len = STRLEN(buffer);
3019 vim_snprintf((char *)buffer + len, IOSIZE - len,
3020 "\"%s%s%s%s%s%s",
3021 curbufIsChanged() ? (shortmess(SHM_MOD)
3022 ? " [+]" : _(" [Modified]")) : " ",
3023 (curbuf->b_flags & BF_NOTEDITED)
3024 #ifdef FEAT_QUICKFIX
3025 && !bt_dontwrite(curbuf)
3026 #endif
3027 ? _("[Not edited]") : "",
3028 (curbuf->b_flags & BF_NEW)
3029 #ifdef FEAT_QUICKFIX
3030 && !bt_dontwrite(curbuf)
3031 #endif
3032 ? _("[New file]") : "",
3033 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
3034 curbuf->b_p_ro ? (shortmess(SHM_RO) ? "[RO]"
3035 : _("[readonly]")) : "",
3036 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3037 || curbuf->b_p_ro) ?
3038 " " : "");
3039 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3040 * causes an overflow, thus for large numbers divide instead. */
3041 if (curwin->w_cursor.lnum > 1000000L)
3042 n = (int)(((long)curwin->w_cursor.lnum) /
3043 ((long)curbuf->b_ml.ml_line_count / 100L));
3044 else
3045 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3046 (long)curbuf->b_ml.ml_line_count);
3047 len = STRLEN(buffer);
3048 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3050 vim_snprintf((char *)buffer + len, IOSIZE - len, "%s", _(no_lines_msg));
3052 #ifdef FEAT_CMDL_INFO
3053 else if (p_ru)
3055 /* Current line and column are already on the screen -- webb */
3056 if (curbuf->b_ml.ml_line_count == 1)
3057 vim_snprintf((char *)buffer + len, IOSIZE - len,
3058 _("1 line --%d%%--"), n);
3059 else
3060 vim_snprintf((char *)buffer + len, IOSIZE - len,
3061 _("%ld lines --%d%%--"),
3062 (long)curbuf->b_ml.ml_line_count, n);
3064 #endif
3065 else
3067 vim_snprintf((char *)buffer + len, IOSIZE - len,
3068 _("line %ld of %ld --%d%%-- col "),
3069 (long)curwin->w_cursor.lnum,
3070 (long)curbuf->b_ml.ml_line_count,
3072 validate_virtcol();
3073 col_print(buffer + STRLEN(buffer),
3074 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3077 (void)append_arg_number(curwin, buffer, !shortmess(SHM_FILE), IOSIZE);
3079 if (dont_truncate)
3081 /* Temporarily set msg_scroll to avoid the message being truncated.
3082 * First call msg_start() to get the message in the right place. */
3083 msg_start();
3084 n = msg_scroll;
3085 msg_scroll = TRUE;
3086 msg(buffer);
3087 msg_scroll = n;
3089 else
3091 p = msg_trunc_attr(buffer, FALSE, 0);
3092 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
3093 /* Need to repeat the message after redrawing when:
3094 * - When restart_edit is set (otherwise there will be a delay
3095 * before redrawing).
3096 * - When the screen was scrolled but there is no wait-return
3097 * prompt. */
3098 set_keep_msg(p, 0);
3101 vim_free(buffer);
3104 void
3105 col_print(buf, col, vcol)
3106 char_u *buf;
3107 int col;
3108 int vcol;
3110 if (col == vcol)
3111 sprintf((char *)buf, "%d", col);
3112 else
3113 sprintf((char *)buf, "%d-%d", col, vcol);
3116 #if defined(FEAT_TITLE) || defined(PROTO)
3118 * put file name in title bar of window and in icon title
3121 static char_u *lasttitle = NULL;
3122 static char_u *lasticon = NULL;
3124 void
3125 maketitle()
3127 char_u *p;
3128 char_u *t_str = NULL;
3129 char_u *i_name;
3130 char_u *i_str = NULL;
3131 int maxlen = 0;
3132 int len;
3133 int mustset;
3134 char_u buf[IOSIZE];
3135 int off;
3137 if (!redrawing())
3139 /* Postpone updating the title when 'lazyredraw' is set. */
3140 need_maketitle = TRUE;
3141 return;
3144 #ifdef FEAT_GUI_MACVIM
3145 gui_macvim_update_modified_flag();
3146 #endif
3148 need_maketitle = FALSE;
3149 if (!p_title && !p_icon)
3150 return;
3152 if (p_title)
3154 if (p_titlelen > 0)
3156 maxlen = p_titlelen * Columns / 100;
3157 if (maxlen < 10)
3158 maxlen = 10;
3161 t_str = buf;
3162 if (*p_titlestring != NUL)
3164 #ifdef FEAT_STL_OPT
3165 if (stl_syntax & STL_IN_TITLE)
3167 int use_sandbox = FALSE;
3168 int save_called_emsg = called_emsg;
3170 # ifdef FEAT_EVAL
3171 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
3172 # endif
3173 called_emsg = FALSE;
3174 build_stl_str_hl(curwin, t_str, sizeof(buf),
3175 p_titlestring, use_sandbox,
3176 0, maxlen, NULL, NULL);
3177 if (called_emsg)
3178 set_string_option_direct((char_u *)"titlestring", -1,
3179 (char_u *)"", OPT_FREE, SID_ERROR);
3180 called_emsg |= save_called_emsg;
3182 else
3183 #endif
3184 t_str = p_titlestring;
3186 else
3188 /* format: "fname + (path) (1 of 2) - VIM" */
3190 if (curbuf->b_fname == NULL)
3191 STRCPY(buf, _("[No Name]"));
3192 else
3194 p = transstr(gettail(curbuf->b_fname));
3195 vim_strncpy(buf, p, IOSIZE - 100);
3196 vim_free(p);
3199 switch (bufIsChanged(curbuf)
3200 + (curbuf->b_p_ro * 2)
3201 + (!curbuf->b_p_ma * 4))
3203 case 1: STRCAT(buf, " +"); break;
3204 case 2: STRCAT(buf, " ="); break;
3205 case 3: STRCAT(buf, " =+"); break;
3206 case 4:
3207 case 6: STRCAT(buf, " -"); break;
3208 case 5:
3209 case 7: STRCAT(buf, " -+"); break;
3212 if (curbuf->b_fname != NULL)
3214 /* Get path of file, replace home dir with ~ */
3215 off = (int)STRLEN(buf);
3216 buf[off++] = ' ';
3217 buf[off++] = '(';
3218 home_replace(curbuf, curbuf->b_ffname,
3219 buf + off, IOSIZE - off, TRUE);
3220 #ifdef BACKSLASH_IN_FILENAME
3221 /* avoid "c:/name" to be reduced to "c" */
3222 if (isalpha(buf[off]) && buf[off + 1] == ':')
3223 off += 2;
3224 #endif
3225 /* remove the file name */
3226 p = gettail_sep(buf + off);
3227 if (p == buf + off)
3228 /* must be a help buffer */
3229 vim_strncpy(buf + off, (char_u *)_("help"),
3230 IOSIZE - off - 1);
3231 else
3232 *p = NUL;
3234 /* translate unprintable chars */
3235 p = transstr(buf + off);
3236 vim_strncpy(buf + off, p, IOSIZE - off - 1);
3237 vim_free(p);
3238 STRCAT(buf, ")");
3241 #ifndef FEAT_GUI_MACVIM
3242 append_arg_number(curwin, buf, FALSE, IOSIZE);
3243 #endif
3245 #if defined(FEAT_CLIENTSERVER)
3246 if (serverName != NULL)
3248 STRCAT(buf, " - ");
3249 STRCAT(buf, serverName);
3251 else
3252 #endif
3253 STRCAT(buf, " - VIM");
3255 if (maxlen > 0)
3257 /* make it shorter by removing a bit in the middle */
3258 len = vim_strsize(buf);
3259 if (len > maxlen)
3260 trunc_string(buf, buf, maxlen);
3264 mustset = ti_change(t_str, &lasttitle);
3266 if (p_icon)
3268 i_str = buf;
3269 if (*p_iconstring != NUL)
3271 #ifdef FEAT_STL_OPT
3272 if (stl_syntax & STL_IN_ICON)
3274 int use_sandbox = FALSE;
3275 int save_called_emsg = called_emsg;
3277 # ifdef FEAT_EVAL
3278 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
3279 # endif
3280 called_emsg = FALSE;
3281 build_stl_str_hl(curwin, i_str, sizeof(buf),
3282 p_iconstring, use_sandbox,
3283 0, 0, NULL, NULL);
3284 if (called_emsg)
3285 set_string_option_direct((char_u *)"iconstring", -1,
3286 (char_u *)"", OPT_FREE, SID_ERROR);
3287 called_emsg |= save_called_emsg;
3289 else
3290 #endif
3291 i_str = p_iconstring;
3293 else
3295 if (buf_spname(curbuf) != NULL)
3296 i_name = (char_u *)buf_spname(curbuf);
3297 else /* use file name only in icon */
3298 i_name = gettail(curbuf->b_ffname);
3299 *i_str = NUL;
3300 /* Truncate name at 100 bytes. */
3301 len = (int)STRLEN(i_name);
3302 if (len > 100)
3304 len -= 100;
3305 #ifdef FEAT_MBYTE
3306 if (has_mbyte)
3307 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3308 #endif
3309 i_name += len;
3311 STRCPY(i_str, i_name);
3312 trans_characters(i_str, IOSIZE);
3316 mustset |= ti_change(i_str, &lasticon);
3318 if (mustset)
3319 resettitle();
3323 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3324 * from "str" if it does.
3325 * Return TRUE when "*last" changed.
3327 static int
3328 ti_change(str, last)
3329 char_u *str;
3330 char_u **last;
3332 if ((str == NULL) != (*last == NULL)
3333 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3335 vim_free(*last);
3336 if (str == NULL)
3337 *last = NULL;
3338 else
3339 *last = vim_strsave(str);
3340 return TRUE;
3342 return FALSE;
3346 * Put current window title back (used after calling a shell)
3348 void
3349 resettitle()
3351 mch_settitle(lasttitle, lasticon);
3354 # if defined(EXITFREE) || defined(PROTO)
3355 void
3356 free_titles()
3358 vim_free(lasttitle);
3359 vim_free(lasticon);
3361 # endif
3363 #endif /* FEAT_TITLE */
3365 #if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
3367 * Build a string from the status line items in "fmt".
3368 * Return length of string in screen cells.
3370 * Normally works for window "wp", except when working for 'tabline' then it
3371 * is "curwin".
3373 * Items are drawn interspersed with the text that surrounds it
3374 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3375 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3377 * If maxwidth is not zero, the string will be filled at any middle marker
3378 * or truncated if too long, fillchar is used for all whitespace.
3380 /*ARGSUSED*/
3382 build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar, maxwidth, hltab, tabtab)
3383 win_T *wp;
3384 char_u *out; /* buffer to write into != NameBuff */
3385 size_t outlen; /* length of out[] */
3386 char_u *fmt;
3387 int use_sandbox; /* "fmt" was set insecurely, use sandbox */
3388 int fillchar;
3389 int maxwidth;
3390 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3391 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
3393 char_u *p;
3394 char_u *s;
3395 char_u *t;
3396 char_u *linecont;
3397 #ifdef FEAT_EVAL
3398 win_T *o_curwin;
3399 buf_T *o_curbuf;
3400 #endif
3401 int empty_line;
3402 colnr_T virtcol;
3403 long l;
3404 long n;
3405 int prevchar_isflag;
3406 int prevchar_isitem;
3407 int itemisflag;
3408 int fillable;
3409 char_u *str;
3410 long num;
3411 int width;
3412 int itemcnt;
3413 int curitem;
3414 int groupitem[STL_MAX_ITEM];
3415 int groupdepth;
3416 struct stl_item
3418 char_u *start;
3419 int minwid;
3420 int maxwid;
3421 enum
3423 Normal,
3424 Empty,
3425 Group,
3426 Middle,
3427 Highlight,
3428 TabPage,
3429 Trunc
3430 } type;
3431 } item[STL_MAX_ITEM];
3432 int minwid;
3433 int maxwid;
3434 int zeropad;
3435 char_u base;
3436 char_u opt;
3437 #define TMPLEN 70
3438 char_u tmp[TMPLEN];
3439 char_u *usefmt = fmt;
3440 struct stl_hlrec *sp;
3442 #ifdef FEAT_EVAL
3444 * When the format starts with "%!" then evaluate it as an expression and
3445 * use the result as the actual format string.
3447 if (fmt[0] == '%' && fmt[1] == '!')
3449 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3450 if (usefmt == NULL)
3451 usefmt = fmt;
3453 #endif
3455 if (fillchar == 0)
3456 fillchar = ' ';
3457 #ifdef FEAT_MBYTE
3458 /* Can't handle a multi-byte fill character yet. */
3459 else if (mb_char2len(fillchar) > 1)
3460 fillchar = '-';
3461 #endif
3464 * Get line & check if empty (cursorpos will show "0-1").
3465 * If inversion is possible we use it. Else '=' characters are used.
3467 linecont = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3468 empty_line = (*linecont == NUL);
3470 groupdepth = 0;
3471 p = out;
3472 curitem = 0;
3473 prevchar_isflag = TRUE;
3474 prevchar_isitem = FALSE;
3475 for (s = usefmt; *s; )
3477 if (*s != NUL && *s != '%')
3478 prevchar_isflag = prevchar_isitem = FALSE;
3481 * Handle up to the next '%' or the end.
3483 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3484 *p++ = *s++;
3485 if (*s == NUL || p + 1 >= out + outlen)
3486 break;
3489 * Handle one '%' item.
3491 s++;
3492 if (*s == '%')
3494 if (p + 1 >= out + outlen)
3495 break;
3496 *p++ = *s++;
3497 prevchar_isflag = prevchar_isitem = FALSE;
3498 continue;
3500 if (*s == STL_MIDDLEMARK)
3502 s++;
3503 if (groupdepth > 0)
3504 continue;
3505 item[curitem].type = Middle;
3506 item[curitem++].start = p;
3507 continue;
3509 if (*s == STL_TRUNCMARK)
3511 s++;
3512 item[curitem].type = Trunc;
3513 item[curitem++].start = p;
3514 continue;
3516 if (*s == ')')
3518 s++;
3519 if (groupdepth < 1)
3520 continue;
3521 groupdepth--;
3523 t = item[groupitem[groupdepth]].start;
3524 *p = NUL;
3525 l = vim_strsize(t);
3526 if (curitem > groupitem[groupdepth] + 1
3527 && item[groupitem[groupdepth]].minwid == 0)
3529 /* remove group if all items are empty */
3530 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3531 if (item[n].type == Normal)
3532 break;
3533 if (n == curitem)
3535 p = t;
3536 l = 0;
3539 if (l > item[groupitem[groupdepth]].maxwid)
3541 /* truncate, remove n bytes of text at the start */
3542 #ifdef FEAT_MBYTE
3543 if (has_mbyte)
3545 /* Find the first character that should be included. */
3546 n = 0;
3547 while (l >= item[groupitem[groupdepth]].maxwid)
3549 l -= ptr2cells(t + n);
3550 n += (*mb_ptr2len)(t + n);
3553 else
3554 #endif
3555 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
3557 *t = '<';
3558 mch_memmove(t + 1, t + n, p - (t + n));
3559 p = p - n + 1;
3560 #ifdef FEAT_MBYTE
3561 /* Fill up space left over by half a double-wide char. */
3562 while (++l < item[groupitem[groupdepth]].minwid)
3563 *p++ = fillchar;
3564 #endif
3566 /* correct the start of the items for the truncation */
3567 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3569 item[l].start -= n;
3570 if (item[l].start < t)
3571 item[l].start = t;
3574 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3576 /* fill */
3577 n = item[groupitem[groupdepth]].minwid;
3578 if (n < 0)
3580 /* fill by appending characters */
3581 n = 0 - n;
3582 while (l++ < n && p + 1 < out + outlen)
3583 *p++ = fillchar;
3585 else
3587 /* fill by inserting characters */
3588 mch_memmove(t + n - l, t, p - t);
3589 l = n - l;
3590 if (p + l >= out + outlen)
3591 l = (long)((out + outlen) - p - 1);
3592 p += l;
3593 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3594 item[n].start += l;
3595 for ( ; l > 0; l--)
3596 *t++ = fillchar;
3599 continue;
3601 minwid = 0;
3602 maxwid = 9999;
3603 zeropad = FALSE;
3604 l = 1;
3605 if (*s == '0')
3607 s++;
3608 zeropad = TRUE;
3610 if (*s == '-')
3612 s++;
3613 l = -1;
3615 if (VIM_ISDIGIT(*s))
3617 minwid = (int)getdigits(&s);
3618 if (minwid < 0) /* overflow */
3619 minwid = 0;
3621 if (*s == STL_USER_HL)
3623 item[curitem].type = Highlight;
3624 item[curitem].start = p;
3625 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3626 s++;
3627 curitem++;
3628 continue;
3630 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3632 if (*s == STL_TABCLOSENR)
3634 if (minwid == 0)
3636 /* %X ends the close label, go back to the previously
3637 * define tab label nr. */
3638 for (n = curitem - 1; n >= 0; --n)
3639 if (item[n].type == TabPage && item[n].minwid >= 0)
3641 minwid = item[n].minwid;
3642 break;
3645 else
3646 /* close nrs are stored as negative values */
3647 minwid = - minwid;
3649 item[curitem].type = TabPage;
3650 item[curitem].start = p;
3651 item[curitem].minwid = minwid;
3652 s++;
3653 curitem++;
3654 continue;
3656 if (*s == '.')
3658 s++;
3659 if (VIM_ISDIGIT(*s))
3661 maxwid = (int)getdigits(&s);
3662 if (maxwid <= 0) /* overflow */
3663 maxwid = 50;
3666 minwid = (minwid > 50 ? 50 : minwid) * l;
3667 if (*s == '(')
3669 groupitem[groupdepth++] = curitem;
3670 item[curitem].type = Group;
3671 item[curitem].start = p;
3672 item[curitem].minwid = minwid;
3673 item[curitem].maxwid = maxwid;
3674 s++;
3675 curitem++;
3676 continue;
3678 if (vim_strchr(STL_ALL, *s) == NULL)
3680 s++;
3681 continue;
3683 opt = *s++;
3685 /* OK - now for the real work */
3686 base = 'D';
3687 itemisflag = FALSE;
3688 fillable = TRUE;
3689 num = -1;
3690 str = NULL;
3691 switch (opt)
3693 case STL_FILEPATH:
3694 case STL_FULLPATH:
3695 case STL_FILENAME:
3696 fillable = FALSE; /* don't change ' ' to fillchar */
3697 if (buf_spname(wp->w_buffer) != NULL)
3698 STRCPY(NameBuff, buf_spname(wp->w_buffer));
3699 else
3701 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
3702 : wp->w_buffer->b_fname;
3703 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3705 trans_characters(NameBuff, MAXPATHL);
3706 if (opt != STL_FILENAME)
3707 str = NameBuff;
3708 else
3709 str = gettail(NameBuff);
3710 break;
3712 case STL_VIM_EXPR: /* '{' */
3713 itemisflag = TRUE;
3714 t = p;
3715 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3716 *p++ = *s++;
3717 if (*s != '}') /* missing '}' or out of space */
3718 break;
3719 s++;
3720 *p = 0;
3721 p = t;
3723 #ifdef FEAT_EVAL
3724 sprintf((char *)tmp, "%d", curbuf->b_fnum);
3725 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3727 o_curbuf = curbuf;
3728 o_curwin = curwin;
3729 curwin = wp;
3730 curbuf = wp->w_buffer;
3732 str = eval_to_string_safe(p, &t, use_sandbox);
3734 curwin = o_curwin;
3735 curbuf = o_curbuf;
3736 do_unlet((char_u *)"g:actual_curbuf", TRUE);
3738 if (str != NULL && *str != 0)
3740 if (*skipdigits(str) == NUL)
3742 num = atoi((char *)str);
3743 vim_free(str);
3744 str = NULL;
3745 itemisflag = FALSE;
3748 #endif
3749 break;
3751 case STL_LINE:
3752 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3753 ? 0L : (long)(wp->w_cursor.lnum);
3754 break;
3756 case STL_NUMLINES:
3757 num = wp->w_buffer->b_ml.ml_line_count;
3758 break;
3760 case STL_COLUMN:
3761 num = !(State & INSERT) && empty_line
3762 ? 0 : (int)wp->w_cursor.col + 1;
3763 break;
3765 case STL_VIRTCOL:
3766 case STL_VIRTCOL_ALT:
3767 /* In list mode virtcol needs to be recomputed */
3768 virtcol = wp->w_virtcol;
3769 if (wp->w_p_list && lcs_tab1 == NUL)
3771 wp->w_p_list = FALSE;
3772 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3773 wp->w_p_list = TRUE;
3775 ++virtcol;
3776 /* Don't display %V if it's the same as %c. */
3777 if (opt == STL_VIRTCOL_ALT
3778 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3779 ? 0 : (int)wp->w_cursor.col + 1)))
3780 break;
3781 num = (long)virtcol;
3782 break;
3784 case STL_PERCENTAGE:
3785 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3786 (long)wp->w_buffer->b_ml.ml_line_count);
3787 break;
3789 case STL_ALTPERCENT:
3790 str = tmp;
3791 get_rel_pos(wp, str);
3792 break;
3794 case STL_ARGLISTSTAT:
3795 fillable = FALSE;
3796 tmp[0] = 0;
3797 if (append_arg_number(wp, tmp, FALSE, (int)sizeof(tmp)))
3798 str = tmp;
3799 break;
3801 case STL_KEYMAP:
3802 fillable = FALSE;
3803 if (get_keymap_str(wp, tmp, TMPLEN))
3804 str = tmp;
3805 break;
3806 case STL_PAGENUM:
3807 #if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
3808 num = printer_page_num;
3809 #else
3810 num = 0;
3811 #endif
3812 break;
3814 case STL_BUFNO:
3815 num = wp->w_buffer->b_fnum;
3816 break;
3818 case STL_OFFSET_X:
3819 base = 'X';
3820 case STL_OFFSET:
3821 #ifdef FEAT_BYTEOFF
3822 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
3823 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
3824 0L : l + 1 + (!(State & INSERT) && empty_line ?
3825 0 : (int)wp->w_cursor.col);
3826 #endif
3827 break;
3829 case STL_BYTEVAL_X:
3830 base = 'X';
3831 case STL_BYTEVAL:
3832 if (wp->w_cursor.col > STRLEN(linecont))
3833 num = 0;
3834 else
3836 #ifdef FEAT_MBYTE
3837 num = (*mb_ptr2char)(linecont + wp->w_cursor.col);
3838 #else
3839 num = linecont[wp->w_cursor.col];
3840 #endif
3842 if (num == NL)
3843 num = 0;
3844 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
3845 num = NL;
3846 break;
3848 case STL_ROFLAG:
3849 case STL_ROFLAG_ALT:
3850 itemisflag = TRUE;
3851 if (wp->w_buffer->b_p_ro)
3852 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : "[RO]");
3853 break;
3855 case STL_HELPFLAG:
3856 case STL_HELPFLAG_ALT:
3857 itemisflag = TRUE;
3858 if (wp->w_buffer->b_help)
3859 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
3860 : _("[Help]"));
3861 break;
3863 #ifdef FEAT_AUTOCMD
3864 case STL_FILETYPE:
3865 if (*wp->w_buffer->b_p_ft != NUL
3866 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
3868 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
3869 wp->w_buffer->b_p_ft);
3870 str = tmp;
3872 break;
3874 case STL_FILETYPE_ALT:
3875 itemisflag = TRUE;
3876 if (*wp->w_buffer->b_p_ft != NUL
3877 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
3879 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
3880 wp->w_buffer->b_p_ft);
3881 for (t = tmp; *t != 0; t++)
3882 *t = TOUPPER_LOC(*t);
3883 str = tmp;
3885 break;
3886 #endif
3888 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3889 case STL_PREVIEWFLAG:
3890 case STL_PREVIEWFLAG_ALT:
3891 itemisflag = TRUE;
3892 if (wp->w_p_pvw)
3893 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
3894 : _("[Preview]"));
3895 break;
3896 #endif
3898 case STL_MODIFIED:
3899 case STL_MODIFIED_ALT:
3900 itemisflag = TRUE;
3901 switch ((opt == STL_MODIFIED_ALT)
3902 + bufIsChanged(wp->w_buffer) * 2
3903 + (!wp->w_buffer->b_p_ma) * 4)
3905 case 2: str = (char_u *)"[+]"; break;
3906 case 3: str = (char_u *)",+"; break;
3907 case 4: str = (char_u *)"[-]"; break;
3908 case 5: str = (char_u *)",-"; break;
3909 case 6: str = (char_u *)"[+-]"; break;
3910 case 7: str = (char_u *)",+-"; break;
3912 break;
3914 case STL_HIGHLIGHT:
3915 t = s;
3916 while (*s != '#' && *s != NUL)
3917 ++s;
3918 if (*s == '#')
3920 item[curitem].type = Highlight;
3921 item[curitem].start = p;
3922 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
3923 curitem++;
3925 ++s;
3926 continue;
3929 item[curitem].start = p;
3930 item[curitem].type = Normal;
3931 if (str != NULL && *str)
3933 t = str;
3934 if (itemisflag)
3936 if ((t[0] && t[1])
3937 && ((!prevchar_isitem && *t == ',')
3938 || (prevchar_isflag && *t == ' ')))
3939 t++;
3940 prevchar_isflag = TRUE;
3942 l = vim_strsize(t);
3943 if (l > 0)
3944 prevchar_isitem = TRUE;
3945 if (l > maxwid)
3947 while (l >= maxwid)
3948 #ifdef FEAT_MBYTE
3949 if (has_mbyte)
3951 l -= ptr2cells(t);
3952 t += (*mb_ptr2len)(t);
3954 else
3955 #endif
3956 l -= byte2cells(*t++);
3957 if (p + 1 >= out + outlen)
3958 break;
3959 *p++ = '<';
3961 if (minwid > 0)
3963 for (; l < minwid && p + 1 < out + outlen; l++)
3965 /* Don't put a "-" in front of a digit. */
3966 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
3967 *p++ = ' ';
3968 else
3969 *p++ = fillchar;
3971 minwid = 0;
3973 else
3974 minwid *= -1;
3975 while (*t && p + 1 < out + outlen)
3977 *p++ = *t++;
3978 /* Change a space by fillchar, unless fillchar is '-' and a
3979 * digit follows. */
3980 if (fillable && p[-1] == ' '
3981 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
3982 p[-1] = fillchar;
3984 for (; l < minwid && p + 1 < out + outlen; l++)
3985 *p++ = fillchar;
3987 else if (num >= 0)
3989 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
3990 char_u nstr[20];
3992 if (p + 20 >= out + outlen)
3993 break; /* not sufficient space */
3994 prevchar_isitem = TRUE;
3995 t = nstr;
3996 if (opt == STL_VIRTCOL_ALT)
3998 *t++ = '-';
3999 minwid--;
4001 *t++ = '%';
4002 if (zeropad)
4003 *t++ = '0';
4004 *t++ = '*';
4005 *t++ = nbase == 16 ? base : (nbase == 8 ? 'o' : 'd');
4006 *t = 0;
4008 for (n = num, l = 1; n >= nbase; n /= nbase)
4009 l++;
4010 if (opt == STL_VIRTCOL_ALT)
4011 l++;
4012 if (l > maxwid)
4014 l += 2;
4015 n = l - maxwid;
4016 while (l-- > maxwid)
4017 num /= nbase;
4018 *t++ = '>';
4019 *t++ = '%';
4020 *t = t[-3];
4021 *++t = 0;
4022 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4023 0, num, n);
4025 else
4026 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4027 minwid, num);
4028 p += STRLEN(p);
4030 else
4031 item[curitem].type = Empty;
4033 if (opt == STL_VIM_EXPR)
4034 vim_free(str);
4036 if (num >= 0 || (!itemisflag && str && *str))
4037 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4038 curitem++;
4040 *p = NUL;
4041 itemcnt = curitem;
4043 #ifdef FEAT_EVAL
4044 if (usefmt != fmt)
4045 vim_free(usefmt);
4046 #endif
4048 width = vim_strsize(out);
4049 if (maxwidth > 0 && width > maxwidth)
4051 /* Result is too long, must truncate somewhere. */
4052 l = 0;
4053 if (itemcnt == 0)
4054 s = out;
4055 else
4057 for ( ; l < itemcnt; l++)
4058 if (item[l].type == Trunc)
4060 /* Truncate at %< item. */
4061 s = item[l].start;
4062 break;
4064 if (l == itemcnt)
4066 /* No %< item, truncate first item. */
4067 s = item[0].start;
4068 l = 0;
4072 if (width - vim_strsize(s) >= maxwidth)
4074 /* Truncation mark is beyond max length */
4075 #ifdef FEAT_MBYTE
4076 if (has_mbyte)
4078 s = out;
4079 width = 0;
4080 for (;;)
4082 width += ptr2cells(s);
4083 if (width >= maxwidth)
4084 break;
4085 s += (*mb_ptr2len)(s);
4087 /* Fill up for half a double-wide character. */
4088 while (++width < maxwidth)
4089 *s++ = fillchar;
4091 else
4092 #endif
4093 s = out + maxwidth - 1;
4094 for (l = 0; l < itemcnt; l++)
4095 if (item[l].start > s)
4096 break;
4097 itemcnt = l;
4098 *s++ = '>';
4099 *s = 0;
4101 else
4103 #ifdef FEAT_MBYTE
4104 if (has_mbyte)
4106 n = 0;
4107 while (width >= maxwidth)
4109 width -= ptr2cells(s + n);
4110 n += (*mb_ptr2len)(s + n);
4113 else
4114 #endif
4115 n = width - maxwidth + 1;
4116 p = s + n;
4117 STRMOVE(s + 1, p);
4118 *s = '<';
4120 /* Fill up for half a double-wide character. */
4121 while (++width < maxwidth)
4123 s = s + STRLEN(s);
4124 *s++ = fillchar;
4125 *s = NUL;
4128 --n; /* count the '<' */
4129 for (; l < itemcnt; l++)
4131 if (item[l].start - n >= s)
4132 item[l].start -= n;
4133 else
4134 item[l].start = s;
4137 width = maxwidth;
4139 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4141 /* Apply STL_MIDDLE if any */
4142 for (l = 0; l < itemcnt; l++)
4143 if (item[l].type == Middle)
4144 break;
4145 if (l < itemcnt)
4147 p = item[l].start + maxwidth - width;
4148 STRMOVE(p, item[l].start);
4149 for (s = item[l].start; s < p; s++)
4150 *s = fillchar;
4151 for (l++; l < itemcnt; l++)
4152 item[l].start += maxwidth - width;
4153 width = maxwidth;
4157 /* Store the info about highlighting. */
4158 if (hltab != NULL)
4160 sp = hltab;
4161 for (l = 0; l < itemcnt; l++)
4163 if (item[l].type == Highlight)
4165 sp->start = item[l].start;
4166 sp->userhl = item[l].minwid;
4167 sp++;
4170 sp->start = NULL;
4171 sp->userhl = 0;
4174 /* Store the info about tab pages labels. */
4175 if (tabtab != NULL)
4177 sp = tabtab;
4178 for (l = 0; l < itemcnt; l++)
4180 if (item[l].type == TabPage)
4182 sp->start = item[l].start;
4183 sp->userhl = item[l].minwid;
4184 sp++;
4187 sp->start = NULL;
4188 sp->userhl = 0;
4191 return width;
4193 #endif /* FEAT_STL_OPT */
4195 #if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4196 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
4198 * Get relative cursor position in window into "str[]", in the form 99%, using
4199 * "Top", "Bot" or "All" when appropriate.
4201 void
4202 get_rel_pos(wp, str)
4203 win_T *wp;
4204 char_u *str;
4206 long above; /* number of lines above window */
4207 long below; /* number of lines below window */
4209 above = wp->w_topline - 1;
4210 #ifdef FEAT_DIFF
4211 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
4212 #endif
4213 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4214 if (below <= 0)
4215 STRCPY(str, above == 0 ? _("All") : _("Bot"));
4216 else if (above <= 0)
4217 STRCPY(str, _("Top"));
4218 else
4219 sprintf((char *)str, "%2d%%", above > 1000000L
4220 ? (int)(above / ((above + below) / 100L))
4221 : (int)(above * 100L / (above + below)));
4223 #endif
4226 * Append (file 2 of 8) to 'buf', if editing more than one file.
4227 * Return TRUE if it was appended.
4230 append_arg_number(wp, buf, add_file, maxlen)
4231 win_T *wp;
4232 char_u *buf;
4233 int add_file; /* Add "file" before the arg number */
4234 int maxlen; /* maximum nr of chars in buf or zero*/
4236 char_u *p;
4238 if (ARGCOUNT <= 1) /* nothing to do */
4239 return FALSE;
4241 p = buf + STRLEN(buf); /* go to the end of the buffer */
4242 if (maxlen && p - buf + 35 >= maxlen) /* getting too long */
4243 return FALSE;
4244 *p++ = ' ';
4245 *p++ = '(';
4246 if (add_file)
4248 STRCPY(p, "file ");
4249 p += 5;
4251 sprintf((char *)p, wp->w_arg_idx_invalid ? "(%d) of %d)"
4252 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4253 return TRUE;
4257 * If fname is not a full path, make it a full path.
4258 * Returns pointer to allocated memory (NULL for failure).
4260 char_u *
4261 fix_fname(fname)
4262 char_u *fname;
4265 * Force expanding the path always for Unix, because symbolic links may
4266 * mess up the full path name, even though it starts with a '/'.
4267 * Also expand when there is ".." in the file name, try to remove it,
4268 * because "c:/src/../README" is equal to "c:/README".
4269 * Similarly "c:/src//file" is equal to "c:/src/file".
4270 * For MS-Windows also expand names like "longna~1" to "longname".
4272 #ifdef UNIX
4273 return FullName_save(fname, TRUE);
4274 #else
4275 if (!vim_isAbsName(fname)
4276 || strstr((char *)fname, "..") != NULL
4277 || strstr((char *)fname, "//") != NULL
4278 # ifdef BACKSLASH_IN_FILENAME
4279 || strstr((char *)fname, "\\\\") != NULL
4280 # endif
4281 # if defined(MSWIN) || defined(DJGPP)
4282 || vim_strchr(fname, '~') != NULL
4283 # endif
4285 return FullName_save(fname, FALSE);
4287 fname = vim_strsave(fname);
4289 # ifdef USE_FNAME_CASE
4290 # ifdef USE_LONG_FNAME
4291 if (USE_LONG_FNAME)
4292 # endif
4294 if (fname != NULL)
4295 fname_case(fname, 0); /* set correct case for file name */
4297 # endif
4299 return fname;
4300 #endif
4304 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4305 * "ffname" becomes a pointer to allocated memory (or NULL).
4307 /*ARGSUSED*/
4308 void
4309 fname_expand(buf, ffname, sfname)
4310 buf_T *buf;
4311 char_u **ffname;
4312 char_u **sfname;
4314 if (*ffname == NULL) /* if no file name given, nothing to do */
4315 return;
4316 if (*sfname == NULL) /* if no short file name given, use ffname */
4317 *sfname = *ffname;
4318 *ffname = fix_fname(*ffname); /* expand to full path */
4320 #ifdef FEAT_SHORTCUT
4321 if (!buf->b_p_bin)
4323 char_u *rfname;
4325 /* If the file name is a shortcut file, use the file it links to. */
4326 rfname = mch_resolve_shortcut(*ffname);
4327 if (rfname != NULL)
4329 vim_free(*ffname);
4330 *ffname = rfname;
4331 *sfname = rfname;
4334 #endif
4338 * Get the file name for an argument list entry.
4340 char_u *
4341 alist_name(aep)
4342 aentry_T *aep;
4344 buf_T *bp;
4346 /* Use the name from the associated buffer if it exists. */
4347 bp = buflist_findnr(aep->ae_fnum);
4348 if (bp == NULL || bp->b_fname == NULL)
4349 return aep->ae_fname;
4350 return bp->b_fname;
4353 #if defined(FEAT_WINDOWS) || defined(PROTO)
4355 * do_arg_all(): Open up to 'count' windows, one for each argument.
4357 void
4358 do_arg_all(count, forceit, keep_tabs)
4359 int count;
4360 int forceit; /* hide buffers in current windows */
4361 int keep_tabs; /* keep current tabs, for ":tab drop file" */
4363 int i;
4364 win_T *wp, *wpnext;
4365 char_u *opened; /* array of flags for which args are open */
4366 int opened_len; /* length of opened[] */
4367 int use_firstwin = FALSE; /* use first window for arglist */
4368 int split_ret = OK;
4369 int p_ea_save;
4370 alist_T *alist; /* argument list to be used */
4371 buf_T *buf;
4372 tabpage_T *tpnext;
4373 int had_tab = cmdmod.tab;
4374 win_T *new_curwin = NULL;
4375 tabpage_T *new_curtab = NULL;
4377 if (ARGCOUNT <= 0)
4379 /* Don't give an error message. We don't want it when the ":all"
4380 * command is in the .vimrc. */
4381 return;
4383 setpcmark();
4385 opened_len = ARGCOUNT;
4386 opened = alloc_clear((unsigned)opened_len);
4387 if (opened == NULL)
4388 return;
4390 #ifdef FEAT_GUI
4391 need_mouse_correct = TRUE;
4392 #endif
4395 * Try closing all windows that are not in the argument list.
4396 * Also close windows that are not full width;
4397 * When 'hidden' or "forceit" set the buffer becomes hidden.
4398 * Windows that have a changed buffer and can't be hidden won't be closed.
4399 * When the ":tab" modifier was used do this for all tab pages.
4401 if (had_tab > 0)
4402 goto_tabpage_tp(first_tabpage);
4403 for (;;)
4405 tpnext = curtab->tp_next;
4406 for (wp = firstwin; wp != NULL; wp = wpnext)
4408 wpnext = wp->w_next;
4409 buf = wp->w_buffer;
4410 if (buf->b_ffname == NULL
4411 || buf->b_nwindows > 1
4412 #ifdef FEAT_VERTSPLIT
4413 || wp->w_width != Columns
4414 #endif
4416 i = ARGCOUNT;
4417 else
4419 /* check if the buffer in this window is in the arglist */
4420 for (i = 0; i < ARGCOUNT; ++i)
4422 if (ARGLIST[i].ae_fnum == buf->b_fnum
4423 || fullpathcmp(alist_name(&ARGLIST[i]),
4424 buf->b_ffname, TRUE) & FPC_SAME)
4426 if (i < opened_len)
4428 opened[i] = TRUE;
4429 if (i == 0)
4431 new_curwin = wp;
4432 new_curtab = curtab;
4435 if (wp->w_alist != curwin->w_alist)
4437 /* Use the current argument list for all windows
4438 * containing a file from it. */
4439 alist_unlink(wp->w_alist);
4440 wp->w_alist = curwin->w_alist;
4441 ++wp->w_alist->al_refcount;
4443 break;
4447 wp->w_arg_idx = i;
4449 if (i == ARGCOUNT && !keep_tabs) /* close this window */
4451 if (P_HID(buf) || forceit || buf->b_nwindows > 1
4452 || !bufIsChanged(buf))
4454 /* If the buffer was changed, and we would like to hide it,
4455 * try autowriting. */
4456 if (!P_HID(buf) && buf->b_nwindows <= 1
4457 && bufIsChanged(buf))
4459 (void)autowrite(buf, FALSE);
4460 #ifdef FEAT_AUTOCMD
4461 /* check if autocommands removed the window */
4462 if (!win_valid(wp) || !buf_valid(buf))
4464 wpnext = firstwin; /* start all over... */
4465 continue;
4467 #endif
4469 #ifdef FEAT_WINDOWS
4470 /* don't close last window */
4471 if (firstwin == lastwin && first_tabpage->tp_next == NULL)
4472 #endif
4473 use_firstwin = TRUE;
4474 #ifdef FEAT_WINDOWS
4475 else
4477 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4478 # ifdef FEAT_AUTOCMD
4479 /* check if autocommands removed the next window */
4480 if (!win_valid(wpnext))
4481 wpnext = firstwin; /* start all over... */
4482 # endif
4484 #endif
4489 /* Without the ":tab" modifier only do the current tab page. */
4490 if (had_tab == 0 || tpnext == NULL)
4491 break;
4493 # ifdef FEAT_AUTOCMD
4494 /* check if autocommands removed the next tab page */
4495 if (!valid_tabpage(tpnext))
4496 tpnext = first_tabpage; /* start all over...*/
4497 # endif
4498 goto_tabpage_tp(tpnext);
4502 * Open a window for files in the argument list that don't have one.
4503 * ARGCOUNT may change while doing this, because of autocommands.
4505 if (count > ARGCOUNT || count <= 0)
4506 count = ARGCOUNT;
4508 /* Autocommands may do anything to the argument list. Make sure it's not
4509 * freed while we are working here by "locking" it. We still have to
4510 * watch out for its size to be changed. */
4511 alist = curwin->w_alist;
4512 ++alist->al_refcount;
4514 #ifdef FEAT_AUTOCMD
4515 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4516 ++autocmd_no_enter;
4517 ++autocmd_no_leave;
4518 #endif
4519 win_enter(lastwin, FALSE);
4520 #ifdef FEAT_WINDOWS
4521 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4522 * leaving an empty tab page when executed locally. */
4523 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4524 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4525 use_firstwin = TRUE;
4526 #endif
4528 for (i = 0; i < count && i < alist->al_ga.ga_len && !got_int; ++i)
4530 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4531 arg_had_last = TRUE;
4532 if (i < opened_len && opened[i])
4534 /* Move the already present window to below the current window */
4535 if (curwin->w_arg_idx != i)
4537 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4539 if (wpnext->w_arg_idx == i)
4541 win_move_after(wpnext, curwin);
4542 break;
4547 else if (split_ret == OK)
4549 if (!use_firstwin) /* split current window */
4551 p_ea_save = p_ea;
4552 p_ea = TRUE; /* use space from all windows */
4553 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4554 p_ea = p_ea_save;
4555 if (split_ret == FAIL)
4556 continue;
4558 #ifdef FEAT_AUTOCMD
4559 else /* first window: do autocmd for leaving this buffer */
4560 --autocmd_no_leave;
4561 #endif
4564 * edit file "i"
4566 curwin->w_arg_idx = i;
4567 if (i == 0)
4569 new_curwin = curwin;
4570 new_curtab = curtab;
4572 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4573 ECMD_ONE,
4574 ((P_HID(curwin->w_buffer)
4575 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
4576 + ECMD_OLDBUF, curwin);
4577 #ifdef FEAT_AUTOCMD
4578 if (use_firstwin)
4579 ++autocmd_no_leave;
4580 #endif
4581 use_firstwin = FALSE;
4583 ui_breakcheck();
4585 /* When ":tab" was used open a new tab for a new window repeatedly. */
4586 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4587 cmdmod.tab = 9999;
4590 /* Remove the "lock" on the argument list. */
4591 alist_unlink(alist);
4593 #ifdef FEAT_AUTOCMD
4594 --autocmd_no_enter;
4595 #endif
4596 /* to window with first arg */
4597 if (valid_tabpage(new_curtab))
4598 goto_tabpage_tp(new_curtab);
4599 if (win_valid(new_curwin))
4600 win_enter(new_curwin, FALSE);
4602 #ifdef FEAT_AUTOCMD
4603 --autocmd_no_leave;
4604 #endif
4605 vim_free(opened);
4608 # if defined(FEAT_LISTCMDS) || defined(PROTO)
4610 * Open a window for a number of buffers.
4612 void
4613 ex_buffer_all(eap)
4614 exarg_T *eap;
4616 buf_T *buf;
4617 win_T *wp, *wpnext;
4618 int split_ret = OK;
4619 int p_ea_save;
4620 int open_wins = 0;
4621 int r;
4622 int count; /* Maximum number of windows to open. */
4623 int all; /* When TRUE also load inactive buffers. */
4624 #ifdef FEAT_WINDOWS
4625 int had_tab = cmdmod.tab;
4626 tabpage_T *tpnext;
4627 #endif
4629 if (eap->addr_count == 0) /* make as many windows as possible */
4630 count = 9999;
4631 else
4632 count = eap->line2; /* make as many windows as specified */
4633 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4634 all = FALSE;
4635 else
4636 all = TRUE;
4638 setpcmark();
4640 #ifdef FEAT_GUI
4641 need_mouse_correct = TRUE;
4642 #endif
4645 * Close superfluous windows (two windows for the same buffer).
4646 * Also close windows that are not full-width.
4648 #ifdef FEAT_WINDOWS
4649 if (had_tab > 0)
4650 goto_tabpage_tp(first_tabpage);
4651 for (;;)
4653 #endif
4654 tpnext = curtab->tp_next;
4655 for (wp = firstwin; wp != NULL; wp = wpnext)
4657 wpnext = wp->w_next;
4658 if ((wp->w_buffer->b_nwindows > 1
4659 #ifdef FEAT_VERTSPLIT
4660 || ((cmdmod.split & WSP_VERT)
4661 ? wp->w_height + wp->w_status_height < Rows - p_ch
4662 - tabline_height()
4663 : wp->w_width != Columns)
4664 #endif
4665 #ifdef FEAT_WINDOWS
4666 || (had_tab > 0 && wp != firstwin)
4667 #endif
4668 ) && firstwin != lastwin)
4670 win_close(wp, FALSE);
4671 #ifdef FEAT_AUTOCMD
4672 wpnext = firstwin; /* just in case an autocommand does
4673 something strange with windows */
4674 tpnext = first_tabpage; /* start all over...*/
4675 open_wins = 0;
4676 #endif
4678 else
4679 ++open_wins;
4682 #ifdef FEAT_WINDOWS
4683 /* Without the ":tab" modifier only do the current tab page. */
4684 if (had_tab == 0 || tpnext == NULL)
4685 break;
4686 goto_tabpage_tp(tpnext);
4688 #endif
4691 * Go through the buffer list. When a buffer doesn't have a window yet,
4692 * open one. Otherwise move the window to the right position.
4693 * Watch out for autocommands that delete buffers or windows!
4695 #ifdef FEAT_AUTOCMD
4696 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4697 ++autocmd_no_enter;
4698 #endif
4699 win_enter(lastwin, FALSE);
4700 #ifdef FEAT_AUTOCMD
4701 ++autocmd_no_leave;
4702 #endif
4703 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4705 /* Check if this buffer needs a window */
4706 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4707 continue;
4709 #ifdef FEAT_WINDOWS
4710 if (had_tab != 0)
4712 /* With the ":tab" modifier don't move the window. */
4713 if (buf->b_nwindows > 0)
4714 wp = lastwin; /* buffer has a window, skip it */
4715 else
4716 wp = NULL;
4718 else
4719 #endif
4721 /* Check if this buffer already has a window */
4722 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4723 if (wp->w_buffer == buf)
4724 break;
4725 /* If the buffer already has a window, move it */
4726 if (wp != NULL)
4727 win_move_after(wp, curwin);
4730 if (wp == NULL && split_ret == OK)
4732 /* Split the window and put the buffer in it */
4733 p_ea_save = p_ea;
4734 p_ea = TRUE; /* use space from all windows */
4735 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4736 ++open_wins;
4737 p_ea = p_ea_save;
4738 if (split_ret == FAIL)
4739 continue;
4741 /* Open the buffer in this window. */
4742 #if defined(HAS_SWAP_EXISTS_ACTION)
4743 swap_exists_action = SEA_DIALOG;
4744 #endif
4745 set_curbuf(buf, DOBUF_GOTO);
4746 #ifdef FEAT_AUTOCMD
4747 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
4749 #if defined(HAS_SWAP_EXISTS_ACTION)
4750 swap_exists_action = SEA_NONE;
4751 # endif
4752 break;
4754 #endif
4755 #if defined(HAS_SWAP_EXISTS_ACTION)
4756 if (swap_exists_action == SEA_QUIT)
4758 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4759 cleanup_T cs;
4761 /* Reset the error/interrupt/exception state here so that
4762 * aborting() returns FALSE when closing a window. */
4763 enter_cleanup(&cs);
4764 # endif
4766 /* User selected Quit at ATTENTION prompt; close this window. */
4767 win_close(curwin, TRUE);
4768 --open_wins;
4769 swap_exists_action = SEA_NONE;
4770 swap_exists_did_quit = TRUE;
4772 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4773 /* Restore the error/interrupt/exception state if not
4774 * discarded by a new aborting error, interrupt, or uncaught
4775 * exception. */
4776 leave_cleanup(&cs);
4777 # endif
4779 else
4780 handle_swap_exists(NULL);
4781 #endif
4784 ui_breakcheck();
4785 if (got_int)
4787 (void)vgetc(); /* only break the file loading, not the rest */
4788 break;
4790 #ifdef FEAT_EVAL
4791 /* Autocommands deleted the buffer or aborted script processing!!! */
4792 if (aborting())
4793 break;
4794 #endif
4795 #ifdef FEAT_WINDOWS
4796 /* When ":tab" was used open a new tab for a new window repeatedly. */
4797 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4798 cmdmod.tab = 9999;
4799 #endif
4801 #ifdef FEAT_AUTOCMD
4802 --autocmd_no_enter;
4803 #endif
4804 win_enter(firstwin, FALSE); /* back to first window */
4805 #ifdef FEAT_AUTOCMD
4806 --autocmd_no_leave;
4807 #endif
4810 * Close superfluous windows.
4812 for (wp = lastwin; open_wins > count; )
4814 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
4815 || autowrite(wp->w_buffer, FALSE) == OK);
4816 #ifdef FEAT_AUTOCMD
4817 if (!win_valid(wp))
4819 /* BufWrite Autocommands made the window invalid, start over */
4820 wp = lastwin;
4822 else
4823 #endif
4824 if (r)
4826 win_close(wp, !P_HID(wp->w_buffer));
4827 --open_wins;
4828 wp = lastwin;
4830 else
4832 wp = wp->w_prev;
4833 if (wp == NULL)
4834 break;
4838 # endif /* FEAT_LISTCMDS */
4840 #endif /* FEAT_WINDOWS */
4842 static int chk_modeline __ARGS((linenr_T, int));
4845 * do_modelines() - process mode lines for the current file
4847 * "flags" can be:
4848 * OPT_WINONLY only set options local to window
4849 * OPT_NOWIN don't set options local to window
4851 * Returns immediately if the "ml" option isn't set.
4853 void
4854 do_modelines(flags)
4855 int flags;
4857 linenr_T lnum;
4858 int nmlines;
4859 static int entered = 0;
4861 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
4862 return;
4864 /* Disallow recursive entry here. Can happen when executing a modeline
4865 * triggers an autocommand, which reloads modelines with a ":do". */
4866 if (entered)
4867 return;
4869 ++entered;
4870 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
4871 ++lnum)
4872 if (chk_modeline(lnum, flags) == FAIL)
4873 nmlines = 0;
4875 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
4876 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
4877 if (chk_modeline(lnum, flags) == FAIL)
4878 nmlines = 0;
4879 --entered;
4882 #include "version.h" /* for version number */
4885 * chk_modeline() - check a single line for a mode string
4886 * Return FAIL if an error encountered.
4888 static int
4889 chk_modeline(lnum, flags)
4890 linenr_T lnum;
4891 int flags; /* Same as for do_modelines(). */
4893 char_u *s;
4894 char_u *e;
4895 char_u *linecopy; /* local copy of any modeline found */
4896 int prev;
4897 int vers;
4898 int end;
4899 int retval = OK;
4900 char_u *save_sourcing_name;
4901 linenr_T save_sourcing_lnum;
4902 #ifdef FEAT_EVAL
4903 scid_T save_SID;
4904 #endif
4906 prev = -1;
4907 for (s = ml_get(lnum); *s != NUL; ++s)
4909 if (prev == -1 || vim_isspace(prev))
4911 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
4912 || STRNCMP(s, "vi:", (size_t)3) == 0)
4913 break;
4914 if (STRNCMP(s, "vim", 3) == 0)
4916 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
4917 e = s + 4;
4918 else
4919 e = s + 3;
4920 vers = getdigits(&e);
4921 if (*e == ':'
4922 && (s[3] == ':'
4923 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
4924 || (VIM_VERSION_100 < vers && s[3] == '<')
4925 || (VIM_VERSION_100 > vers && s[3] == '>')
4926 || (VIM_VERSION_100 == vers && s[3] == '=')))
4927 break;
4930 prev = *s;
4933 if (*s)
4935 do /* skip over "ex:", "vi:" or "vim:" */
4936 ++s;
4937 while (s[-1] != ':');
4939 s = linecopy = vim_strsave(s); /* copy the line, it will change */
4940 if (linecopy == NULL)
4941 return FAIL;
4943 save_sourcing_lnum = sourcing_lnum;
4944 save_sourcing_name = sourcing_name;
4945 sourcing_lnum = lnum; /* prepare for emsg() */
4946 sourcing_name = (char_u *)"modelines";
4948 end = FALSE;
4949 while (end == FALSE)
4951 s = skipwhite(s);
4952 if (*s == NUL)
4953 break;
4956 * Find end of set command: ':' or end of line.
4957 * Skip over "\:", replacing it with ":".
4959 for (e = s; *e != ':' && *e != NUL; ++e)
4960 if (e[0] == '\\' && e[1] == ':')
4961 STRMOVE(e, e + 1);
4962 if (*e == NUL)
4963 end = TRUE;
4966 * If there is a "set" command, require a terminating ':' and
4967 * ignore the stuff after the ':'.
4968 * "vi:set opt opt opt: foo" -- foo not interpreted
4969 * "vi:opt opt opt: foo" -- foo interpreted
4970 * Accept "se" for compatibility with Elvis.
4972 if (STRNCMP(s, "set ", (size_t)4) == 0
4973 || STRNCMP(s, "se ", (size_t)3) == 0)
4975 if (*e != ':') /* no terminating ':'? */
4976 break;
4977 end = TRUE;
4978 s = vim_strchr(s, ' ') + 1;
4980 *e = NUL; /* truncate the set command */
4982 if (*s != NUL) /* skip over an empty "::" */
4984 #ifdef FEAT_EVAL
4985 save_SID = current_SID;
4986 current_SID = SID_MODELINE;
4987 #endif
4988 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
4989 #ifdef FEAT_EVAL
4990 current_SID = save_SID;
4991 #endif
4992 if (retval == FAIL) /* stop if error found */
4993 break;
4995 s = e + 1; /* advance to next part */
4998 sourcing_lnum = save_sourcing_lnum;
4999 sourcing_name = save_sourcing_name;
5001 vim_free(linecopy);
5003 return retval;
5006 #if defined(FEAT_VIMINFO) || defined(PROTO)
5008 read_viminfo_bufferlist(virp, writing)
5009 vir_T *virp;
5010 int writing;
5012 char_u *tab;
5013 linenr_T lnum;
5014 colnr_T col;
5015 buf_T *buf;
5016 char_u *sfname;
5017 char_u *xline;
5019 /* Handle long line and escaped characters. */
5020 xline = viminfo_readstring(virp, 1, FALSE);
5022 /* don't read in if there are files on the command-line or if writing: */
5023 if (xline != NULL && !writing && ARGCOUNT == 0
5024 && find_viminfo_parameter('%') != NULL)
5026 /* Format is: <fname> Tab <lnum> Tab <col>.
5027 * Watch out for a Tab in the file name, work from the end. */
5028 lnum = 0;
5029 col = 0;
5030 tab = vim_strrchr(xline, '\t');
5031 if (tab != NULL)
5033 *tab++ = '\0';
5034 col = atoi((char *)tab);
5035 tab = vim_strrchr(xline, '\t');
5036 if (tab != NULL)
5038 *tab++ = '\0';
5039 lnum = atol((char *)tab);
5043 /* Expand "~/" in the file name at "line + 1" to a full path.
5044 * Then try shortening it by comparing with the current directory */
5045 expand_env(xline, NameBuff, MAXPATHL);
5046 sfname = shorten_fname1(NameBuff);
5048 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5049 if (buf != NULL) /* just in case... */
5051 buf->b_last_cursor.lnum = lnum;
5052 buf->b_last_cursor.col = col;
5053 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5056 vim_free(xline);
5058 return viminfo_readline(virp);
5061 void
5062 write_viminfo_bufferlist(fp)
5063 FILE *fp;
5065 buf_T *buf;
5066 #ifdef FEAT_WINDOWS
5067 win_T *win;
5068 tabpage_T *tp;
5069 #endif
5070 char_u *line;
5071 int max_buffers;
5073 if (find_viminfo_parameter('%') == NULL)
5074 return;
5076 /* Without a number -1 is returned: do all buffers. */
5077 max_buffers = get_viminfo_parameter('%');
5079 /* Allocate room for the file name, lnum and col. */
5080 line = alloc(MAXPATHL + 40);
5081 if (line == NULL)
5082 return;
5084 #ifdef FEAT_WINDOWS
5085 FOR_ALL_TAB_WINDOWS(tp, win)
5086 set_last_cursor(win);
5087 #else
5088 set_last_cursor(curwin);
5089 #endif
5091 fprintf(fp, _("\n# Buffer list:\n"));
5092 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5094 if (buf->b_fname == NULL
5095 || !buf->b_p_bl
5096 #ifdef FEAT_QUICKFIX
5097 || bt_quickfix(buf)
5098 #endif
5099 || removable(buf->b_ffname))
5100 continue;
5102 if (max_buffers-- == 0)
5103 break;
5104 putc('%', fp);
5105 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
5106 sprintf((char *)line + STRLEN(line), "\t%ld\t%d",
5107 (long)buf->b_last_cursor.lnum,
5108 buf->b_last_cursor.col);
5109 viminfo_writestring(fp, line);
5111 vim_free(line);
5113 #endif
5117 * Return special buffer name.
5118 * Returns NULL when the buffer has a normal file name.
5120 char *
5121 buf_spname(buf)
5122 buf_T *buf;
5124 #if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5125 if (bt_quickfix(buf))
5127 win_T *win = NULL;
5128 tabpage_T *tp;
5131 * For location list window, w_llist_ref points to the location list.
5132 * For quickfix window, w_llist_ref is NULL.
5134 FOR_ALL_TAB_WINDOWS(tp, win)
5135 if (win->w_buffer == buf)
5136 goto win_found;
5137 win_found:
5138 if (win != NULL && win->w_llist_ref != NULL)
5139 return _("[Location List]");
5140 else
5141 return _("[Quickfix List]");
5143 #endif
5144 #ifdef FEAT_QUICKFIX
5145 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5146 * contains the name as specified by the user */
5147 if (bt_nofile(buf))
5149 if (buf->b_sfname != NULL)
5150 return (char *)buf->b_sfname;
5151 return _("[Scratch]");
5153 #endif
5154 if (buf->b_fname == NULL)
5155 return _("[No Name]");
5156 return NULL;
5160 #if defined(FEAT_SIGNS) || defined(PROTO)
5162 * Insert the sign into the signlist.
5164 static void
5165 insert_sign(buf, prev, next, id, lnum, typenr)
5166 buf_T *buf; /* buffer to store sign in */
5167 signlist_T *prev; /* previous sign entry */
5168 signlist_T *next; /* next sign entry */
5169 int id; /* sign ID */
5170 linenr_T lnum; /* line number which gets the mark */
5171 int typenr; /* typenr of sign we are adding */
5173 signlist_T *newsign;
5175 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5176 if (newsign != NULL)
5178 newsign->id = id;
5179 newsign->lnum = lnum;
5180 newsign->typenr = typenr;
5181 newsign->next = next;
5182 #ifdef FEAT_NETBEANS_INTG
5183 newsign->prev = prev;
5184 if (next != NULL)
5185 next->prev = newsign;
5186 #endif
5188 if (prev == NULL)
5190 /* When adding first sign need to redraw the windows to create the
5191 * column for signs. */
5192 if (buf->b_signlist == NULL)
5194 redraw_buf_later(buf, NOT_VALID);
5195 changed_cline_bef_curs();
5198 /* first sign in signlist */
5199 buf->b_signlist = newsign;
5201 else
5202 prev->next = newsign;
5207 * Add the sign into the signlist. Find the right spot to do it though.
5209 void
5210 buf_addsign(buf, id, lnum, typenr)
5211 buf_T *buf; /* buffer to store sign in */
5212 int id; /* sign ID */
5213 linenr_T lnum; /* line number which gets the mark */
5214 int typenr; /* typenr of sign we are adding */
5216 signlist_T *sign; /* a sign in the signlist */
5217 signlist_T *prev; /* the previous sign */
5219 prev = NULL;
5220 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5222 if (lnum == sign->lnum && id == sign->id)
5224 sign->typenr = typenr;
5225 return;
5227 else if (
5228 #ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5229 id < 0 &&
5230 #endif
5231 lnum < sign->lnum)
5233 #ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5234 /* XXX - GRP: Is this because of sign slide problem? Or is it
5235 * really needed? Or is it because we allow multiple signs per
5236 * line? If so, should I add that feature to FEAT_SIGNS?
5238 while (prev != NULL && prev->lnum == lnum)
5239 prev = prev->prev;
5240 if (prev == NULL)
5241 sign = buf->b_signlist;
5242 else
5243 sign = prev->next;
5244 #endif
5245 insert_sign(buf, prev, sign, id, lnum, typenr);
5246 return;
5248 prev = sign;
5250 #ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5251 /* XXX - GRP: See previous comment */
5252 while (prev != NULL && prev->lnum == lnum)
5253 prev = prev->prev;
5254 if (prev == NULL)
5255 sign = buf->b_signlist;
5256 else
5257 sign = prev->next;
5258 #endif
5259 insert_sign(buf, prev, sign, id, lnum, typenr);
5261 return;
5265 buf_change_sign_type(buf, markId, typenr)
5266 buf_T *buf; /* buffer to store sign in */
5267 int markId; /* sign ID */
5268 int typenr; /* typenr of sign we are adding */
5270 signlist_T *sign; /* a sign in the signlist */
5272 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5274 if (sign->id == markId)
5276 sign->typenr = typenr;
5277 return sign->lnum;
5281 return 0;
5284 int_u
5285 buf_getsigntype(buf, lnum, type)
5286 buf_T *buf;
5287 linenr_T lnum;
5288 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5290 signlist_T *sign; /* a sign in a b_signlist */
5292 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5293 if (sign->lnum == lnum
5294 && (type == SIGN_ANY
5295 # ifdef FEAT_SIGN_ICONS
5296 || (type == SIGN_ICON
5297 && sign_get_image(sign->typenr) != NULL)
5298 # endif
5299 || (type == SIGN_TEXT
5300 && sign_get_text(sign->typenr) != NULL)
5301 || (type == SIGN_LINEHL
5302 && sign_get_attr(sign->typenr, TRUE) != 0)))
5303 return sign->typenr;
5304 return 0;
5308 linenr_T
5309 buf_delsign(buf, id)
5310 buf_T *buf; /* buffer sign is stored in */
5311 int id; /* sign id */
5313 signlist_T **lastp; /* pointer to pointer to current sign */
5314 signlist_T *sign; /* a sign in a b_signlist */
5315 signlist_T *next; /* the next sign in a b_signlist */
5316 linenr_T lnum; /* line number whose sign was deleted */
5318 lastp = &buf->b_signlist;
5319 lnum = 0;
5320 for (sign = buf->b_signlist; sign != NULL; sign = next)
5322 next = sign->next;
5323 if (sign->id == id)
5325 *lastp = next;
5326 #ifdef FEAT_NETBEANS_INTG
5327 if (next != NULL)
5328 next->prev = sign->prev;
5329 #endif
5330 lnum = sign->lnum;
5331 vim_free(sign);
5332 break;
5334 else
5335 lastp = &sign->next;
5338 /* When deleted the last sign need to redraw the windows to remove the
5339 * sign column. */
5340 if (buf->b_signlist == NULL)
5342 redraw_buf_later(buf, NOT_VALID);
5343 changed_cline_bef_curs();
5346 return lnum;
5351 * Find the line number of the sign with the requested id. If the sign does
5352 * not exist, return 0 as the line number. This will still let the correct file
5353 * get loaded.
5356 buf_findsign(buf, id)
5357 buf_T *buf; /* buffer to store sign in */
5358 int id; /* sign ID */
5360 signlist_T *sign; /* a sign in the signlist */
5362 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5363 if (sign->id == id)
5364 return sign->lnum;
5366 return 0;
5370 buf_findsign_id(buf, lnum)
5371 buf_T *buf; /* buffer whose sign we are searching for */
5372 linenr_T lnum; /* line number of sign */
5374 signlist_T *sign; /* a sign in the signlist */
5376 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5377 if (sign->lnum == lnum)
5378 return sign->id;
5380 return 0;
5384 # if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5385 /* see if a given type of sign exists on a specific line */
5387 buf_findsigntype_id(buf, lnum, typenr)
5388 buf_T *buf; /* buffer whose sign we are searching for */
5389 linenr_T lnum; /* line number of sign */
5390 int typenr; /* sign type number */
5392 signlist_T *sign; /* a sign in the signlist */
5394 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5395 if (sign->lnum == lnum && sign->typenr == typenr)
5396 return sign->id;
5398 return 0;
5402 # if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5403 /* return the number of icons on the given line */
5405 buf_signcount(buf, lnum)
5406 buf_T *buf;
5407 linenr_T lnum;
5409 signlist_T *sign; /* a sign in the signlist */
5410 int count = 0;
5412 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5413 if (sign->lnum == lnum)
5414 if (sign_get_image(sign->typenr) != NULL)
5415 count++;
5417 return count;
5419 # endif /* FEAT_SIGN_ICONS */
5420 # endif /* FEAT_NETBEANS_INTG */
5424 * Delete signs in buffer "buf".
5426 static void
5427 buf_delete_signs(buf)
5428 buf_T *buf;
5430 signlist_T *next;
5432 while (buf->b_signlist != NULL)
5434 next = buf->b_signlist->next;
5435 vim_free(buf->b_signlist);
5436 buf->b_signlist = next;
5441 * Delete all signs in all buffers.
5443 void
5444 buf_delete_all_signs()
5446 buf_T *buf; /* buffer we are checking for signs */
5448 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5449 if (buf->b_signlist != NULL)
5451 /* Need to redraw the windows to remove the sign column. */
5452 redraw_buf_later(buf, NOT_VALID);
5453 buf_delete_signs(buf);
5458 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5460 void
5461 sign_list_placed(rbuf)
5462 buf_T *rbuf;
5464 buf_T *buf;
5465 signlist_T *p;
5466 char lbuf[BUFSIZ];
5468 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5469 msg_putchar('\n');
5470 if (rbuf == NULL)
5471 buf = firstbuf;
5472 else
5473 buf = rbuf;
5474 while (buf != NULL)
5476 if (buf->b_signlist != NULL)
5478 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
5479 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5480 msg_putchar('\n');
5482 for (p = buf->b_signlist; p != NULL; p = p->next)
5484 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
5485 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5486 MSG_PUTS(lbuf);
5487 msg_putchar('\n');
5489 if (rbuf != NULL)
5490 break;
5491 buf = buf->b_next;
5496 * Adjust a placed sign for inserted/deleted lines.
5498 void
5499 sign_mark_adjust(line1, line2, amount, amount_after)
5500 linenr_T line1;
5501 linenr_T line2;
5502 long amount;
5503 long amount_after;
5505 signlist_T *sign; /* a sign in a b_signlist */
5507 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5509 if (sign->lnum >= line1 && sign->lnum <= line2)
5511 if (amount == MAXLNUM)
5512 sign->lnum = line1;
5513 else
5514 sign->lnum += amount;
5516 else if (sign->lnum > line2)
5517 sign->lnum += amount_after;
5520 #endif /* FEAT_SIGNS */
5523 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5525 void
5526 set_buflisted(on)
5527 int on;
5529 if (on != curbuf->b_p_bl)
5531 curbuf->b_p_bl = on;
5532 #ifdef FEAT_AUTOCMD
5533 if (on)
5534 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5535 else
5536 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5537 #endif
5542 * Read the file for "buf" again and check if the contents changed.
5543 * Return TRUE if it changed or this could not be checked.
5546 buf_contents_changed(buf)
5547 buf_T *buf;
5549 buf_T *newbuf;
5550 int differ = TRUE;
5551 linenr_T lnum;
5552 aco_save_T aco;
5553 exarg_T ea;
5555 /* Allocate a buffer without putting it in the buffer list. */
5556 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5557 if (newbuf == NULL)
5558 return TRUE;
5560 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5561 if (prep_exarg(&ea, buf) == FAIL)
5563 wipe_buffer(newbuf, FALSE);
5564 return TRUE;
5567 /* set curwin/curbuf to buf and save a few things */
5568 aucmd_prepbuf(&aco, newbuf);
5570 if (ml_open(curbuf) == OK
5571 && readfile(buf->b_ffname, buf->b_fname,
5572 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5573 &ea, READ_NEW | READ_DUMMY) == OK)
5575 /* compare the two files line by line */
5576 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5578 differ = FALSE;
5579 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5580 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5582 differ = TRUE;
5583 break;
5587 vim_free(ea.cmd);
5589 /* restore curwin/curbuf and a few other things */
5590 aucmd_restbuf(&aco);
5592 if (curbuf != newbuf) /* safety check */
5593 wipe_buffer(newbuf, FALSE);
5595 return differ;
5599 * Wipe out a buffer and decrement the last buffer number if it was used for
5600 * this buffer. Call this to wipe out a temp buffer that does not contain any
5601 * marks.
5603 /*ARGSUSED*/
5604 void
5605 wipe_buffer(buf, aucmd)
5606 buf_T *buf;
5607 int aucmd; /* When TRUE trigger autocommands. */
5609 if (buf->b_fnum == top_file_num - 1)
5610 --top_file_num;
5612 #ifdef FEAT_AUTOCMD
5613 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
5614 block_autocmds();
5615 #endif
5616 close_buffer(NULL, buf, DOBUF_WIPE);
5617 #ifdef FEAT_AUTOCMD
5618 if (!aucmd)
5619 unblock_autocmds();
5620 #endif