Merge branch 'MacVim'
[MacVim/KaoriYa.git] / src / buffer.c
blobe651a61037d9c520e1f31d4b719483a094d52fe0
1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
11 * buffer.c: functions for dealing with the buffer structure
15 * The buffer list is a double linked list of all buffers.
16 * Each buffer can be in one of these states:
17 * never loaded: BF_NEVERLOADED is set, only the file name is valid
18 * not loaded: b_ml.ml_mfp == NULL, no memfile allocated
19 * hidden: b_nwindows == 0, loaded but not displayed in a window
20 * normal: loaded and displayed in a window
22 * Instead of storing file names all over the place, each file name is
23 * stored in the buffer list. It can be referenced by a number.
25 * The current implementation remembers all file names ever used.
28 #include "vim.h"
30 #if defined(FEAT_CMDL_COMPL) || defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL)
31 static char_u *buflist_match __ARGS((regprog_T *prog, buf_T *buf));
32 # define HAVE_BUFLIST_MATCH
33 static char_u *fname_match __ARGS((regprog_T *prog, char_u *name));
34 #endif
35 static void buflist_setfpos __ARGS((buf_T *buf, win_T *win, linenr_T lnum, colnr_T col, int copy_options));
36 static wininfo_T *find_wininfo __ARGS((buf_T *buf, int skip_diff_buffer));
37 #ifdef UNIX
38 static buf_T *buflist_findname_stat __ARGS((char_u *ffname, struct stat *st));
39 static int otherfile_buf __ARGS((buf_T *buf, char_u *ffname, struct stat *stp));
40 static int buf_same_ino __ARGS((buf_T *buf, struct stat *stp));
41 #else
42 static int otherfile_buf __ARGS((buf_T *buf, char_u *ffname));
43 #endif
44 #ifdef FEAT_TITLE
45 static int ti_change __ARGS((char_u *str, char_u **last));
46 #endif
47 static int append_arg_number __ARGS((win_T *wp, char_u *buf, int buflen, int add_file));
48 static void free_buffer __ARGS((buf_T *));
49 static void free_buffer_stuff __ARGS((buf_T *buf, int free_options));
50 static void clear_wininfo __ARGS((buf_T *buf));
52 #ifdef UNIX
53 # define dev_T dev_t
54 #else
55 # define dev_T unsigned
56 #endif
58 #if defined(FEAT_SIGNS)
59 static void insert_sign __ARGS((buf_T *buf, signlist_T *prev, signlist_T *next, int id, linenr_T lnum, int typenr));
60 static void buf_delete_signs __ARGS((buf_T *buf));
61 #endif
64 * Open current buffer, that is: open the memfile and read the file into memory
65 * return FAIL for failure, OK otherwise
67 int
68 open_buffer(read_stdin, eap)
69 int read_stdin; /* read file from stdin */
70 exarg_T *eap; /* for forced 'ff' and 'fenc' or NULL */
72 int retval = OK;
73 #ifdef FEAT_AUTOCMD
74 buf_T *old_curbuf;
75 #endif
78 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
79 * When re-entering the same buffer, it should not change, because the
80 * user may have reset the flag by hand.
82 if (readonlymode && curbuf->b_ffname != NULL
83 && (curbuf->b_flags & BF_NEVERLOADED))
84 curbuf->b_p_ro = TRUE;
86 if (ml_open(curbuf) == FAIL)
89 * There MUST be a memfile, otherwise we can't do anything
90 * If we can't create one for the current buffer, take another buffer
92 close_buffer(NULL, curbuf, 0);
93 for (curbuf = firstbuf; curbuf != NULL; curbuf = curbuf->b_next)
94 if (curbuf->b_ml.ml_mfp != NULL)
95 break;
97 * if there is no memfile at all, exit
98 * This is OK, since there are no changes to lose.
100 if (curbuf == NULL)
102 EMSG(_("E82: Cannot allocate any buffer, exiting..."));
103 getout(2);
105 EMSG(_("E83: Cannot allocate buffer, using other one..."));
106 enter_buffer(curbuf);
107 return FAIL;
110 #ifdef FEAT_AUTOCMD
111 /* The autocommands in readfile() may change the buffer, but only AFTER
112 * reading the file. */
113 old_curbuf = curbuf;
114 modified_was_set = FALSE;
115 #endif
117 /* mark cursor position as being invalid */
118 curwin->w_valid = 0;
120 if (curbuf->b_ffname != NULL
121 #ifdef FEAT_NETBEANS_INTG
122 && netbeansReadFile
123 #endif
126 #ifdef FEAT_NETBEANS_INTG
127 int oldFire = netbeansFireChanges;
129 netbeansFireChanges = 0;
130 #endif
131 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
132 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap, READ_NEW);
133 #ifdef FEAT_NETBEANS_INTG
134 netbeansFireChanges = oldFire;
135 #endif
136 /* Help buffer is filtered. */
137 if (curbuf->b_help)
138 fix_help_buffer();
140 else if (read_stdin)
142 int save_bin = curbuf->b_p_bin;
143 linenr_T line_count;
146 * First read the text in binary mode into the buffer.
147 * Then read from that same buffer and append at the end. This makes
148 * it possible to retry when 'fileformat' or 'fileencoding' was
149 * guessed wrong.
151 curbuf->b_p_bin = TRUE;
152 retval = readfile(NULL, NULL, (linenr_T)0,
153 (linenr_T)0, (linenr_T)MAXLNUM, NULL, READ_NEW + READ_STDIN);
154 curbuf->b_p_bin = save_bin;
155 if (retval == OK)
157 line_count = curbuf->b_ml.ml_line_count;
158 retval = readfile(NULL, NULL, (linenr_T)line_count,
159 (linenr_T)0, (linenr_T)MAXLNUM, eap, READ_BUFFER);
160 if (retval == OK)
162 /* Delete the binary lines. */
163 while (--line_count >= 0)
164 ml_delete((linenr_T)1, FALSE);
166 else
168 /* Delete the converted lines. */
169 while (curbuf->b_ml.ml_line_count > line_count)
170 ml_delete(line_count, FALSE);
172 /* Put the cursor on the first line. */
173 curwin->w_cursor.lnum = 1;
174 curwin->w_cursor.col = 0;
176 /* Set or reset 'modified' before executing autocommands, so that
177 * it can be changed there. */
178 if (!readonlymode && !bufempty())
179 changed();
180 else if (retval != FAIL)
181 unchanged(curbuf, FALSE);
182 #ifdef FEAT_AUTOCMD
183 # ifdef FEAT_EVAL
184 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
185 curbuf, &retval);
186 # else
187 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
188 # endif
189 #endif
193 /* if first time loading this buffer, init b_chartab[] */
194 if (curbuf->b_flags & BF_NEVERLOADED)
195 (void)buf_init_chartab(curbuf, FALSE);
198 * Set/reset the Changed flag first, autocmds may change the buffer.
199 * Apply the automatic commands, before processing the modelines.
200 * So the modelines have priority over auto commands.
202 /* When reading stdin, the buffer contents always needs writing, so set
203 * the changed flag. Unless in readonly mode: "ls | gview -".
204 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
205 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
206 #ifdef FEAT_AUTOCMD
207 || modified_was_set /* ":set modified" used in autocmd */
208 # ifdef FEAT_EVAL
209 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
210 # endif
211 #endif
213 changed();
214 else if (retval != FAIL && !read_stdin)
215 unchanged(curbuf, FALSE);
216 save_file_ff(curbuf); /* keep this fileformat */
218 /* require "!" to overwrite the file, because it wasn't read completely */
219 #ifdef FEAT_EVAL
220 if (aborting())
221 #else
222 if (got_int)
223 #endif
224 curbuf->b_flags |= BF_READERR;
226 #ifdef FEAT_FOLDING
227 /* Need to update automatic folding. Do this before the autocommands,
228 * they may use the fold info. */
229 foldUpdateAll(curwin);
230 #endif
232 #ifdef FEAT_AUTOCMD
233 /* need to set w_topline, unless some autocommand already did that. */
234 if (!(curwin->w_valid & VALID_TOPLINE))
236 curwin->w_topline = 1;
237 # ifdef FEAT_DIFF
238 curwin->w_topfill = 0;
239 # endif
241 # ifdef FEAT_EVAL
242 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
243 # else
244 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
245 # endif
246 #endif
248 if (retval != FAIL)
250 #ifdef FEAT_AUTOCMD
252 * The autocommands may have changed the current buffer. Apply the
253 * modelines to the correct buffer, if it still exists and is loaded.
255 if (buf_valid(old_curbuf) && old_curbuf->b_ml.ml_mfp != NULL)
257 aco_save_T aco;
259 /* Go to the buffer that was opened. */
260 aucmd_prepbuf(&aco, old_curbuf);
261 #endif
262 do_modelines(0);
264 /* specified ff and enc, bin in modelines */
265 if (file_ff_differs(curbuf))
267 /* reload buffer */
268 if (eap)
270 /* restore ++ff and ++enc, ++bin if specified */
271 #ifdef FEAT_MBYTE
272 if (eap->force_enc)
274 char_u *fenc = enc_canonize(eap->cmd + eap->force_enc);
275 if (fenc) {
276 vim_free(curbuf->b_p_fenc);
277 curbuf->b_p_fenc = fenc;
280 #endif
281 if (eap->force_ff)
282 set_fileformat(eap->force_ff, OPT_LOCAL);
283 if (eap->force_bin)
284 curbuf->b_p_bin = eap->force_bin;
286 buf_reload(curbuf, curbuf->b_orig_mode);
288 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
290 #ifdef FEAT_AUTOCMD
291 # ifdef FEAT_EVAL
292 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
293 &retval);
294 # else
295 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
296 # endif
298 /* restore curwin/curbuf and a few other things */
299 aucmd_restbuf(&aco);
301 #endif
304 return retval;
308 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
311 buf_valid(buf)
312 buf_T *buf;
314 buf_T *bp;
316 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
317 if (bp == buf)
318 return TRUE;
319 return FALSE;
323 * Close the link to a buffer.
324 * "action" is used when there is no longer a window for the buffer.
325 * It can be:
326 * 0 buffer becomes hidden
327 * DOBUF_UNLOAD buffer is unloaded
328 * DOBUF_DELETE buffer is unloaded and removed from buffer list
329 * DOBUF_WIPE buffer is unloaded and really deleted
330 * When doing all but the first one on the current buffer, the caller should
331 * get a new buffer very soon!
333 * The 'bufhidden' option can force freeing and deleting.
335 void
336 close_buffer(win, buf, action)
337 win_T *win; /* if not NULL, set b_last_cursor */
338 buf_T *buf;
339 int action;
341 #ifdef FEAT_AUTOCMD
342 int is_curbuf;
343 int nwindows;
344 #endif
345 int unload_buf = (action != 0);
346 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
347 int wipe_buf = (action == DOBUF_WIPE);
349 #ifdef FEAT_QUICKFIX
351 * Force unloading or deleting when 'bufhidden' says so.
352 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
353 * "hide" (otherwise we could never free or delete a buffer).
355 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
357 del_buf = TRUE;
358 unload_buf = TRUE;
360 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
362 del_buf = TRUE;
363 unload_buf = TRUE;
364 wipe_buf = TRUE;
366 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
367 unload_buf = TRUE;
368 #endif
370 if (win != NULL)
372 /* Set b_last_cursor when closing the last window for the buffer.
373 * Remember the last cursor position and window options of the buffer.
374 * This used to be only for the current window, but then options like
375 * 'foldmethod' may be lost with a ":only" command. */
376 if (buf->b_nwindows == 1)
377 set_last_cursor(win);
378 buflist_setfpos(buf, win,
379 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
380 win->w_cursor.col, TRUE);
383 #ifdef FEAT_AUTOCMD
384 /* When the buffer is no longer in a window, trigger BufWinLeave */
385 if (buf->b_nwindows == 1)
387 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
388 FALSE, buf);
389 if (!buf_valid(buf)) /* autocommands may delete the buffer */
390 return;
392 /* When the buffer becomes hidden, but is not unloaded, trigger
393 * BufHidden */
394 if (!unload_buf)
396 apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
397 FALSE, buf);
398 if (!buf_valid(buf)) /* autocmds may delete the buffer */
399 return;
401 # ifdef FEAT_EVAL
402 if (aborting()) /* autocmds may abort script processing */
403 return;
404 # endif
406 nwindows = buf->b_nwindows;
407 #endif
409 /* decrease the link count from windows (unless not in any window) */
410 if (buf->b_nwindows > 0)
411 --buf->b_nwindows;
413 /* Return when a window is displaying the buffer or when it's not
414 * unloaded. */
415 if (buf->b_nwindows > 0 || !unload_buf)
417 #if 0 /* why was this here? */
418 if (buf == curbuf)
419 u_sync(); /* sync undo before going to another buffer */
420 #endif
421 return;
424 /* Always remove the buffer when there is no file name. */
425 if (buf->b_ffname == NULL)
426 del_buf = TRUE;
429 * Free all things allocated for this buffer.
430 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
432 #ifdef FEAT_AUTOCMD
433 /* Remember if we are closing the current buffer. Restore the number of
434 * windows, so that autocommands in buf_freeall() don't get confused. */
435 is_curbuf = (buf == curbuf);
436 buf->b_nwindows = nwindows;
437 #endif
439 buf_freeall(buf, del_buf, wipe_buf);
441 #ifdef FEAT_AUTOCMD
442 /* Autocommands may have deleted the buffer. */
443 if (!buf_valid(buf))
444 return;
445 # ifdef FEAT_EVAL
446 if (aborting()) /* autocmds may abort script processing */
447 return;
448 # endif
450 /* Autocommands may have opened or closed windows for this buffer.
451 * Decrement the count for the close we do here. */
452 if (buf->b_nwindows > 0)
453 --buf->b_nwindows;
456 * It's possible that autocommands change curbuf to the one being deleted.
457 * This might cause the previous curbuf to be deleted unexpectedly. But
458 * in some cases it's OK to delete the curbuf, because a new one is
459 * obtained anyway. Therefore only return if curbuf changed to the
460 * deleted buffer.
462 if (buf == curbuf && !is_curbuf)
463 return;
464 #endif
466 #ifdef FEAT_ODB_EDITOR
467 odb_buffer_close(buf);
468 #endif
470 /* Change directories when the 'acd' option is set. */
471 DO_AUTOCHDIR
474 * Remove the buffer from the list.
476 if (wipe_buf)
478 #ifdef FEAT_SUN_WORKSHOP
479 if (usingSunWorkShop)
480 workshop_file_closed_lineno((char *)buf->b_ffname,
481 (int)buf->b_last_cursor.lnum);
482 #endif
483 vim_free(buf->b_ffname);
484 vim_free(buf->b_sfname);
485 if (buf->b_prev == NULL)
486 firstbuf = buf->b_next;
487 else
488 buf->b_prev->b_next = buf->b_next;
489 if (buf->b_next == NULL)
490 lastbuf = buf->b_prev;
491 else
492 buf->b_next->b_prev = buf->b_prev;
493 free_buffer(buf);
495 else
497 if (del_buf)
499 /* Free all internal variables and reset option values, to make
500 * ":bdel" compatible with Vim 5.7. */
501 free_buffer_stuff(buf, TRUE);
503 /* Make it look like a new buffer. */
504 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
506 /* Init the options when loaded again. */
507 buf->b_p_initialized = FALSE;
509 buf_clear_file(buf);
510 if (del_buf)
511 buf->b_p_bl = FALSE;
516 * Make buffer not contain a file.
518 void
519 buf_clear_file(buf)
520 buf_T *buf;
522 buf->b_ml.ml_line_count = 1;
523 unchanged(buf, TRUE);
524 #ifndef SHORT_FNAME
525 buf->b_shortname = FALSE;
526 #endif
527 buf->b_p_eol = TRUE;
528 buf->b_start_eol = TRUE;
529 #ifdef FEAT_MBYTE
530 buf->b_p_bomb = FALSE;
531 buf->b_start_bomb = FALSE;
532 #endif
533 buf->b_ml.ml_mfp = NULL;
534 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
535 #ifdef FEAT_NETBEANS_INTG
536 netbeans_deleted_all_lines(buf);
537 #endif
541 * buf_freeall() - free all things allocated for a buffer that are related to
542 * the file.
544 void
545 buf_freeall(buf, del_buf, wipe_buf)
546 buf_T *buf;
547 int del_buf UNUSED; /* buffer is going to be deleted */
548 int wipe_buf UNUSED; /* 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 /* mark cursor position as being invalid */
1432 curwin->w_valid = 0;
1434 /* Make sure the buffer is loaded. */
1435 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
1437 #ifdef FEAT_AUTOCMD
1438 /* If there is no filetype, allow for detecting one. Esp. useful for
1439 * ":ball" used in a autocommand. If there already is a filetype we
1440 * might prefer to keep it. */
1441 if (*curbuf->b_p_ft == NUL)
1442 did_filetype = FALSE;
1443 #endif
1445 open_buffer(FALSE, NULL);
1447 else
1449 if (!msg_silent)
1450 need_fileinfo = TRUE; /* display file info after redraw */
1451 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1452 #ifdef FEAT_AUTOCMD
1453 curwin->w_topline = 1;
1454 # ifdef FEAT_DIFF
1455 curwin->w_topfill = 0;
1456 # endif
1457 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1458 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1459 #endif
1462 /* If autocommands did not change the cursor position, restore cursor lnum
1463 * and possibly cursor col. */
1464 if (curwin->w_cursor.lnum == 1 && inindent(0))
1465 buflist_getfpos();
1467 check_arg_idx(curwin); /* check for valid arg_idx */
1468 #ifdef FEAT_TITLE
1469 maketitle();
1470 #endif
1471 #ifdef FEAT_AUTOCMD
1472 /* when autocmds didn't change it */
1473 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
1474 #endif
1475 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1477 #ifdef FEAT_NETBEANS_INTG
1478 /* Send fileOpened event because we've changed buffers. */
1479 if (usingNetbeans && isNetbeansBuffer(curbuf))
1480 netbeans_file_activated(curbuf);
1481 #endif
1483 /* Change directories when the 'acd' option is set. */
1484 DO_AUTOCHDIR
1486 #ifdef FEAT_KEYMAP
1487 if (curbuf->b_kmap_state & KEYMAP_INIT)
1488 (void)keymap_init();
1489 #endif
1490 #ifdef FEAT_SPELL
1491 /* May need to set the spell language. Can only do this after the buffer
1492 * has been properly setup. */
1493 if (!curbuf->b_help && curwin->w_p_spell && *curbuf->b_p_spl != NUL)
1494 (void)did_set_spelllang(curbuf);
1495 #endif
1497 redraw_later(NOT_VALID);
1500 #if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1502 * Change to the directory of the current buffer.
1504 void
1505 do_autochdir()
1507 if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK)
1508 shorten_fnames(TRUE);
1510 #endif
1513 * functions for dealing with the buffer list
1517 * Add a file name to the buffer list. Return a pointer to the buffer.
1518 * If the same file name already exists return a pointer to that buffer.
1519 * If it does not exist, or if fname == NULL, a new entry is created.
1520 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1521 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1522 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
1523 * This is the ONLY way to create a new buffer.
1525 static int top_file_num = 1; /* highest file number */
1527 buf_T *
1528 buflist_new(ffname, sfname, lnum, flags)
1529 char_u *ffname; /* full path of fname or relative */
1530 char_u *sfname; /* short fname or NULL */
1531 linenr_T lnum; /* preferred cursor line */
1532 int flags; /* BLN_ defines */
1534 buf_T *buf;
1535 #ifdef UNIX
1536 struct stat st;
1537 #endif
1539 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1542 * If file name already exists in the list, update the entry.
1544 #ifdef UNIX
1545 /* On Unix we can use inode numbers when the file exists. Works better
1546 * for hard links. */
1547 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1548 st.st_dev = (dev_T)-1;
1549 #endif
1550 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1551 #ifdef UNIX
1552 buflist_findname_stat(ffname, &st)
1553 #else
1554 buflist_findname(ffname)
1555 #endif
1556 ) != NULL)
1558 vim_free(ffname);
1559 if (lnum != 0)
1560 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1561 /* copy the options now, if 'cpo' doesn't have 's' and not done
1562 * already */
1563 buf_copy_options(buf, 0);
1564 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1566 buf->b_p_bl = TRUE;
1567 #ifdef FEAT_AUTOCMD
1568 if (!(flags & BLN_DUMMY))
1569 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1570 #endif
1572 return buf;
1576 * If the current buffer has no name and no contents, use the current
1577 * buffer. Otherwise: Need to allocate a new buffer structure.
1579 * This is the ONLY place where a new buffer structure is allocated!
1580 * (A spell file buffer is allocated in spell.c, but that's not a normal
1581 * buffer.)
1583 buf = NULL;
1584 if ((flags & BLN_CURBUF)
1585 && curbuf != NULL
1586 && curbuf->b_ffname == NULL
1587 && curbuf->b_nwindows <= 1
1588 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1590 buf = curbuf;
1591 #ifdef FEAT_AUTOCMD
1592 /* It's like this buffer is deleted. Watch out for autocommands that
1593 * change curbuf! If that happens, allocate a new buffer anyway. */
1594 if (curbuf->b_p_bl)
1595 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1596 if (buf == curbuf)
1597 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1598 # ifdef FEAT_EVAL
1599 if (aborting()) /* autocmds may abort script processing */
1600 return NULL;
1601 # endif
1602 #endif
1603 #ifdef FEAT_QUICKFIX
1604 # ifdef FEAT_AUTOCMD
1605 if (buf == curbuf)
1606 # endif
1608 /* Make sure 'bufhidden' and 'buftype' are empty */
1609 clear_string_option(&buf->b_p_bh);
1610 clear_string_option(&buf->b_p_bt);
1612 #endif
1614 if (buf != curbuf || curbuf == NULL)
1616 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1617 if (buf == NULL)
1619 vim_free(ffname);
1620 return NULL;
1624 if (ffname != NULL)
1626 buf->b_ffname = ffname;
1627 buf->b_sfname = vim_strsave(sfname);
1630 clear_wininfo(buf);
1631 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1633 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1634 || buf->b_wininfo == NULL)
1636 vim_free(buf->b_ffname);
1637 buf->b_ffname = NULL;
1638 vim_free(buf->b_sfname);
1639 buf->b_sfname = NULL;
1640 if (buf != curbuf)
1641 free_buffer(buf);
1642 return NULL;
1645 if (buf == curbuf)
1647 /* free all things allocated for this buffer */
1648 buf_freeall(buf, FALSE, FALSE);
1649 if (buf != curbuf) /* autocommands deleted the buffer! */
1650 return NULL;
1651 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1652 if (aborting()) /* autocmds may abort script processing */
1653 return NULL;
1654 #endif
1655 /* buf->b_nwindows = 0; why was this here? */
1656 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
1657 #ifdef FEAT_KEYMAP
1658 /* need to reload lmaps and set b:keymap_name */
1659 curbuf->b_kmap_state |= KEYMAP_INIT;
1660 #endif
1662 else
1665 * put new buffer at the end of the buffer list
1667 buf->b_next = NULL;
1668 if (firstbuf == NULL) /* buffer list is empty */
1670 buf->b_prev = NULL;
1671 firstbuf = buf;
1673 else /* append new buffer at end of list */
1675 lastbuf->b_next = buf;
1676 buf->b_prev = lastbuf;
1678 lastbuf = buf;
1680 buf->b_fnum = top_file_num++;
1681 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1683 EMSG(_("W14: Warning: List of file names overflow"));
1684 if (emsg_silent == 0)
1686 out_flush();
1687 ui_delay(3000L, TRUE); /* make sure it is noticed */
1689 top_file_num = 1;
1693 * Always copy the options from the current buffer.
1695 buf_copy_options(buf, BCO_ALWAYS);
1698 buf->b_wininfo->wi_fpos.lnum = lnum;
1699 buf->b_wininfo->wi_win = curwin;
1701 #ifdef FEAT_EVAL
1702 init_var_dict(&buf->b_vars, &buf->b_bufvar); /* init b: variables */
1703 #endif
1704 #ifdef FEAT_SYN_HL
1705 hash_init(&buf->b_keywtab);
1706 hash_init(&buf->b_keywtab_ic);
1707 #endif
1709 buf->b_fname = buf->b_sfname;
1710 #ifdef UNIX
1711 if (st.st_dev == (dev_T)-1)
1712 buf->b_dev_valid = FALSE;
1713 else
1715 buf->b_dev_valid = TRUE;
1716 buf->b_dev = st.st_dev;
1717 buf->b_ino = st.st_ino;
1719 #endif
1720 buf->b_u_synced = TRUE;
1721 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
1722 if (flags & BLN_DUMMY)
1723 buf->b_flags |= BF_DUMMY;
1724 buf_clear_file(buf);
1725 clrallmarks(buf); /* clear marks */
1726 fmarks_check_names(buf); /* check file marks for this file */
1727 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1728 #ifdef FEAT_AUTOCMD
1729 if (!(flags & BLN_DUMMY))
1731 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
1732 if (flags & BLN_LISTED)
1733 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1734 # ifdef FEAT_EVAL
1735 if (aborting()) /* autocmds may abort script processing */
1736 return NULL;
1737 # endif
1739 #endif
1741 return buf;
1745 * Free the memory for the options of a buffer.
1746 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1747 * 'fileencoding'.
1749 void
1750 free_buf_options(buf, free_p_ff)
1751 buf_T *buf;
1752 int free_p_ff;
1754 if (free_p_ff)
1756 #ifdef FEAT_MBYTE
1757 clear_string_option(&buf->b_p_fenc);
1758 #endif
1759 clear_string_option(&buf->b_p_ff);
1760 #ifdef FEAT_QUICKFIX
1761 clear_string_option(&buf->b_p_bh);
1762 clear_string_option(&buf->b_p_bt);
1763 #endif
1765 #ifdef FEAT_FIND_ID
1766 clear_string_option(&buf->b_p_def);
1767 clear_string_option(&buf->b_p_inc);
1768 # ifdef FEAT_EVAL
1769 clear_string_option(&buf->b_p_inex);
1770 # endif
1771 #endif
1772 #if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1773 clear_string_option(&buf->b_p_inde);
1774 clear_string_option(&buf->b_p_indk);
1775 #endif
1776 #if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1777 clear_string_option(&buf->b_p_bexpr);
1778 #endif
1779 #if defined(FEAT_EVAL)
1780 clear_string_option(&buf->b_p_fex);
1781 #endif
1782 #ifdef FEAT_CRYPT
1783 clear_string_option(&buf->b_p_key);
1784 #endif
1785 clear_string_option(&buf->b_p_kp);
1786 clear_string_option(&buf->b_p_mps);
1787 clear_string_option(&buf->b_p_fo);
1788 clear_string_option(&buf->b_p_flp);
1789 clear_string_option(&buf->b_p_isk);
1790 #ifdef FEAT_KEYMAP
1791 clear_string_option(&buf->b_p_keymap);
1792 ga_clear(&buf->b_kmap_ga);
1793 #endif
1794 #ifdef FEAT_COMMENTS
1795 clear_string_option(&buf->b_p_com);
1796 #endif
1797 #ifdef FEAT_FOLDING
1798 clear_string_option(&buf->b_p_cms);
1799 #endif
1800 clear_string_option(&buf->b_p_nf);
1801 #ifdef FEAT_SYN_HL
1802 clear_string_option(&buf->b_p_syn);
1803 #endif
1804 #ifdef FEAT_SPELL
1805 clear_string_option(&buf->b_p_spc);
1806 clear_string_option(&buf->b_p_spf);
1807 vim_free(buf->b_cap_prog);
1808 buf->b_cap_prog = NULL;
1809 clear_string_option(&buf->b_p_spl);
1810 #endif
1811 #ifdef FEAT_SEARCHPATH
1812 clear_string_option(&buf->b_p_sua);
1813 #endif
1814 #ifdef FEAT_AUTOCMD
1815 clear_string_option(&buf->b_p_ft);
1816 #endif
1817 #ifdef FEAT_OSFILETYPE
1818 clear_string_option(&buf->b_p_oft);
1819 #endif
1820 #ifdef FEAT_CINDENT
1821 clear_string_option(&buf->b_p_cink);
1822 clear_string_option(&buf->b_p_cino);
1823 #endif
1824 #if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1825 clear_string_option(&buf->b_p_cinw);
1826 #endif
1827 #ifdef FEAT_INS_EXPAND
1828 clear_string_option(&buf->b_p_cpt);
1829 #endif
1830 #ifdef FEAT_COMPL_FUNC
1831 clear_string_option(&buf->b_p_cfu);
1832 clear_string_option(&buf->b_p_ofu);
1833 #endif
1834 #ifdef FEAT_QUICKFIX
1835 clear_string_option(&buf->b_p_gp);
1836 clear_string_option(&buf->b_p_mp);
1837 clear_string_option(&buf->b_p_efm);
1838 #endif
1839 clear_string_option(&buf->b_p_ep);
1840 clear_string_option(&buf->b_p_path);
1841 clear_string_option(&buf->b_p_tags);
1842 #ifdef FEAT_INS_EXPAND
1843 clear_string_option(&buf->b_p_dict);
1844 clear_string_option(&buf->b_p_tsr);
1845 #endif
1846 #ifdef FEAT_TEXTOBJ
1847 clear_string_option(&buf->b_p_qe);
1848 #endif
1849 buf->b_p_ar = -1;
1853 * get alternate file n
1854 * set linenr to lnum or altfpos.lnum if lnum == 0
1855 * also set cursor column to altfpos.col if 'startofline' is not set.
1856 * if (options & GETF_SETMARK) call setpcmark()
1857 * if (options & GETF_ALT) we are jumping to an alternate file.
1858 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1860 * return FAIL for failure, OK for success
1863 buflist_getfile(n, lnum, options, forceit)
1864 int n;
1865 linenr_T lnum;
1866 int options;
1867 int forceit;
1869 buf_T *buf;
1870 #ifdef FEAT_WINDOWS
1871 win_T *wp = NULL;
1872 #endif
1873 pos_T *fpos;
1874 colnr_T col;
1876 buf = buflist_findnr(n);
1877 if (buf == NULL)
1879 if ((options & GETF_ALT) && n == 0)
1880 EMSG(_(e_noalt));
1881 else
1882 EMSGN(_("E92: Buffer %ld not found"), n);
1883 return FAIL;
1886 /* if alternate file is the current buffer, nothing to do */
1887 if (buf == curbuf)
1888 return OK;
1890 if (text_locked())
1892 text_locked_msg();
1893 return FAIL;
1895 #ifdef FEAT_AUTOCMD
1896 if (curbuf_locked())
1897 return FAIL;
1898 #endif
1900 /* altfpos may be changed by getfile(), get it now */
1901 if (lnum == 0)
1903 fpos = buflist_findfpos(buf);
1904 lnum = fpos->lnum;
1905 col = fpos->col;
1907 else
1908 col = 0;
1910 #ifdef FEAT_WINDOWS
1911 if (options & GETF_SWITCH)
1913 /* If 'switchbuf' contains "useopen": jump to first window containing
1914 * "buf" if one exists */
1915 if (swb_flags & SWB_USEOPEN)
1916 wp = buf_jump_open_win(buf);
1917 /* If 'switchbuf' contians "usetab": jump to first window in any tab
1918 * page containing "buf" if one exists */
1919 if (wp == NULL && (swb_flags & SWB_USETAB))
1920 wp = buf_jump_open_tab(buf);
1921 /* If 'switchbuf' contains "split" or "newtab" and the current buffer
1922 * isn't empty: open new window */
1923 if (wp == NULL && (swb_flags & (SWB_SPLIT | SWB_NEWTAB)) && !bufempty())
1925 if (swb_flags & SWB_NEWTAB) /* Open in a new tab */
1926 tabpage_new();
1927 else if (win_split(0, 0) == FAIL) /* Open in a new window */
1928 return FAIL;
1929 # ifdef FEAT_SCROLLBIND
1930 curwin->w_p_scb = FALSE;
1931 # endif
1934 #endif
1936 ++RedrawingDisabled;
1937 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
1938 lnum, forceit) <= 0)
1940 --RedrawingDisabled;
1942 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
1943 if (!p_sol && col != 0)
1945 curwin->w_cursor.col = col;
1946 check_cursor_col();
1947 #ifdef FEAT_VIRTUALEDIT
1948 curwin->w_cursor.coladd = 0;
1949 #endif
1950 curwin->w_set_curswant = TRUE;
1952 return OK;
1954 --RedrawingDisabled;
1955 return FAIL;
1959 * go to the last know line number for the current buffer
1961 void
1962 buflist_getfpos()
1964 pos_T *fpos;
1966 fpos = buflist_findfpos(curbuf);
1968 curwin->w_cursor.lnum = fpos->lnum;
1969 check_cursor_lnum();
1971 if (p_sol)
1972 curwin->w_cursor.col = 0;
1973 else
1975 curwin->w_cursor.col = fpos->col;
1976 check_cursor_col();
1977 #ifdef FEAT_VIRTUALEDIT
1978 curwin->w_cursor.coladd = 0;
1979 #endif
1980 curwin->w_set_curswant = TRUE;
1984 #if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
1986 * Find file in buffer list by name (it has to be for the current window).
1987 * Returns NULL if not found.
1989 buf_T *
1990 buflist_findname_exp(fname)
1991 char_u *fname;
1993 char_u *ffname;
1994 buf_T *buf = NULL;
1996 /* First make the name into a full path name */
1997 ffname = FullName_save(fname,
1998 #ifdef UNIX
1999 TRUE /* force expansion, get rid of symbolic links */
2000 #else
2001 FALSE
2002 #endif
2004 if (ffname != NULL)
2006 buf = buflist_findname(ffname);
2007 vim_free(ffname);
2009 return buf;
2011 #endif
2014 * Find file in buffer list by name (it has to be for the current window).
2015 * "ffname" must have a full path.
2016 * Skips dummy buffers.
2017 * Returns NULL if not found.
2019 buf_T *
2020 buflist_findname(ffname)
2021 char_u *ffname;
2023 #ifdef UNIX
2024 struct stat st;
2026 if (mch_stat((char *)ffname, &st) < 0)
2027 st.st_dev = (dev_T)-1;
2028 return buflist_findname_stat(ffname, &st);
2032 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2033 * twice for the same file.
2034 * Returns NULL if not found.
2036 static buf_T *
2037 buflist_findname_stat(ffname, stp)
2038 char_u *ffname;
2039 struct stat *stp;
2041 #endif
2042 buf_T *buf;
2044 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2045 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
2046 #ifdef UNIX
2047 , stp
2048 #endif
2050 return buf;
2051 return NULL;
2054 #if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO)
2056 * Find file in buffer list by a regexp pattern.
2057 * Return fnum of the found buffer.
2058 * Return < 0 for error.
2061 buflist_findpat(pattern, pattern_end, unlisted, diffmode)
2062 char_u *pattern;
2063 char_u *pattern_end; /* pointer to first char after pattern */
2064 int unlisted; /* find unlisted buffers */
2065 int diffmode UNUSED; /* find diff-mode buffers only */
2067 buf_T *buf;
2068 regprog_T *prog;
2069 int match = -1;
2070 int find_listed;
2071 char_u *pat;
2072 char_u *patend;
2073 int attempt;
2074 char_u *p;
2075 int toggledollar;
2077 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2079 if (*pattern == '%')
2080 match = curbuf->b_fnum;
2081 else
2082 match = curwin->w_alt_fnum;
2083 #ifdef FEAT_DIFF
2084 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2085 match = -1;
2086 #endif
2090 * Try four ways of matching a listed buffer:
2091 * attempt == 0: without '^' or '$' (at any position)
2092 * attempt == 1: with '^' at start (only at position 0)
2093 * attempt == 2: with '$' at end (only match at end)
2094 * attempt == 3: with '^' at start and '$' at end (only full match)
2095 * Repeat this for finding an unlisted buffer if there was no matching
2096 * listed buffer.
2098 else
2100 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2101 if (pat == NULL)
2102 return -1;
2103 patend = pat + STRLEN(pat) - 1;
2104 toggledollar = (patend > pat && *patend == '$');
2106 /* First try finding a listed buffer. If not found and "unlisted"
2107 * is TRUE, try finding an unlisted buffer. */
2108 find_listed = TRUE;
2109 for (;;)
2111 for (attempt = 0; attempt <= 3; ++attempt)
2113 /* may add '^' and '$' */
2114 if (toggledollar)
2115 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2116 p = pat;
2117 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2118 ++p;
2119 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2120 if (prog == NULL)
2122 vim_free(pat);
2123 return -1;
2126 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2127 if (buf->b_p_bl == find_listed
2128 #ifdef FEAT_DIFF
2129 && (!diffmode || diff_mode_buf(buf))
2130 #endif
2131 && buflist_match(prog, buf) != NULL)
2133 if (match >= 0) /* already found a match */
2135 match = -2;
2136 break;
2138 match = buf->b_fnum; /* remember first match */
2141 vim_free(prog);
2142 if (match >= 0) /* found one match */
2143 break;
2146 /* Only search for unlisted buffers if there was no match with
2147 * a listed buffer. */
2148 if (!unlisted || !find_listed || match != -1)
2149 break;
2150 find_listed = FALSE;
2153 vim_free(pat);
2156 if (match == -2)
2157 EMSG2(_("E93: More than one match for %s"), pattern);
2158 else if (match < 0)
2159 EMSG2(_("E94: No matching buffer for %s"), pattern);
2160 return match;
2162 #endif
2164 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2167 * Find all buffer names that match.
2168 * For command line expansion of ":buf" and ":sbuf".
2169 * Return OK if matches found, FAIL otherwise.
2172 ExpandBufnames(pat, num_file, file, options)
2173 char_u *pat;
2174 int *num_file;
2175 char_u ***file;
2176 int options;
2178 int count = 0;
2179 buf_T *buf;
2180 int round;
2181 char_u *p;
2182 int attempt;
2183 regprog_T *prog;
2184 char_u *patc;
2186 *num_file = 0; /* return values in case of FAIL */
2187 *file = NULL;
2189 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2190 if (*pat == '^')
2192 patc = alloc((unsigned)STRLEN(pat) + 11);
2193 if (patc == NULL)
2194 return FAIL;
2195 STRCPY(patc, "\\(^\\|[\\/]\\)");
2196 STRCPY(patc + 11, pat + 1);
2198 else
2199 patc = pat;
2202 * attempt == 0: try match with '\<', match at start of word
2203 * attempt == 1: try match without '\<', match anywhere
2205 for (attempt = 0; attempt <= 1; ++attempt)
2207 if (attempt > 0 && patc == pat)
2208 break; /* there was no anchor, no need to try again */
2209 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2210 if (prog == NULL)
2212 if (patc != pat)
2213 vim_free(patc);
2214 return FAIL;
2218 * round == 1: Count the matches.
2219 * round == 2: Build the array to keep the matches.
2221 for (round = 1; round <= 2; ++round)
2223 count = 0;
2224 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2226 if (!buf->b_p_bl) /* skip unlisted buffers */
2227 continue;
2228 p = buflist_match(prog, buf);
2229 if (p != NULL)
2231 if (round == 1)
2232 ++count;
2233 else
2235 if (options & WILD_HOME_REPLACE)
2236 p = home_replace_save(buf, p);
2237 else
2238 p = vim_strsave(p);
2239 (*file)[count++] = p;
2243 if (count == 0) /* no match found, break here */
2244 break;
2245 if (round == 1)
2247 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2248 if (*file == NULL)
2250 vim_free(prog);
2251 if (patc != pat)
2252 vim_free(patc);
2253 return FAIL;
2257 vim_free(prog);
2258 if (count) /* match(es) found, break here */
2259 break;
2262 if (patc != pat)
2263 vim_free(patc);
2265 *num_file = count;
2266 return (count == 0 ? FAIL : OK);
2269 #endif /* FEAT_CMDL_COMPL */
2271 #ifdef HAVE_BUFLIST_MATCH
2273 * Check for a match on the file name for buffer "buf" with regprog "prog".
2275 static char_u *
2276 buflist_match(prog, buf)
2277 regprog_T *prog;
2278 buf_T *buf;
2280 char_u *match;
2282 /* First try the short file name, then the long file name. */
2283 match = fname_match(prog, buf->b_sfname);
2284 if (match == NULL)
2285 match = fname_match(prog, buf->b_ffname);
2287 return match;
2291 * Try matching the regexp in "prog" with file name "name".
2292 * Return "name" when there is a match, NULL when not.
2294 static char_u *
2295 fname_match(prog, name)
2296 regprog_T *prog;
2297 char_u *name;
2299 char_u *match = NULL;
2300 char_u *p;
2301 regmatch_T regmatch;
2303 if (name != NULL)
2305 regmatch.regprog = prog;
2306 #ifdef CASE_INSENSITIVE_FILENAME
2307 regmatch.rm_ic = TRUE; /* Always ignore case */
2308 #else
2309 regmatch.rm_ic = FALSE; /* Never ignore case */
2310 #endif
2312 if (vim_regexec(&regmatch, name, (colnr_T)0))
2313 match = name;
2314 else
2316 /* Replace $(HOME) with '~' and try matching again. */
2317 p = home_replace_save(NULL, name);
2318 if (p != NULL && vim_regexec(&regmatch, p, (colnr_T)0))
2319 match = name;
2320 vim_free(p);
2324 return match;
2326 #endif
2329 * find file in buffer list by number
2331 buf_T *
2332 buflist_findnr(nr)
2333 int nr;
2335 buf_T *buf;
2337 if (nr == 0)
2338 nr = curwin->w_alt_fnum;
2339 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2340 if (buf->b_fnum == nr)
2341 return (buf);
2342 return NULL;
2346 * Get name of file 'n' in the buffer list.
2347 * When the file has no name an empty string is returned.
2348 * home_replace() is used to shorten the file name (used for marks).
2349 * Returns a pointer to allocated memory, of NULL when failed.
2351 char_u *
2352 buflist_nr2name(n, fullname, helptail)
2353 int n;
2354 int fullname;
2355 int helptail; /* for help buffers return tail only */
2357 buf_T *buf;
2359 buf = buflist_findnr(n);
2360 if (buf == NULL)
2361 return NULL;
2362 return home_replace_save(helptail ? buf : NULL,
2363 fullname ? buf->b_ffname : buf->b_fname);
2367 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2368 * When "copy_options" is TRUE save the local window option values.
2369 * When "lnum" is 0 only do the options.
2371 static void
2372 buflist_setfpos(buf, win, lnum, col, copy_options)
2373 buf_T *buf;
2374 win_T *win;
2375 linenr_T lnum;
2376 colnr_T col;
2377 int copy_options;
2379 wininfo_T *wip;
2381 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2382 if (wip->wi_win == win)
2383 break;
2384 if (wip == NULL)
2386 /* allocate a new entry */
2387 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2388 if (wip == NULL)
2389 return;
2390 wip->wi_win = win;
2391 if (lnum == 0) /* set lnum even when it's 0 */
2392 lnum = 1;
2394 else
2396 /* remove the entry from the list */
2397 if (wip->wi_prev)
2398 wip->wi_prev->wi_next = wip->wi_next;
2399 else
2400 buf->b_wininfo = wip->wi_next;
2401 if (wip->wi_next)
2402 wip->wi_next->wi_prev = wip->wi_prev;
2403 if (copy_options && wip->wi_optset)
2405 clear_winopt(&wip->wi_opt);
2406 #ifdef FEAT_FOLDING
2407 deleteFoldRecurse(&wip->wi_folds);
2408 #endif
2411 if (lnum != 0)
2413 wip->wi_fpos.lnum = lnum;
2414 wip->wi_fpos.col = col;
2416 if (copy_options)
2418 /* Save the window-specific option values. */
2419 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2420 #ifdef FEAT_FOLDING
2421 wip->wi_fold_manual = win->w_fold_manual;
2422 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2423 #endif
2424 wip->wi_optset = TRUE;
2427 /* insert the entry in front of the list */
2428 wip->wi_next = buf->b_wininfo;
2429 buf->b_wininfo = wip;
2430 wip->wi_prev = NULL;
2431 if (wip->wi_next)
2432 wip->wi_next->wi_prev = wip;
2434 return;
2437 #ifdef FEAT_DIFF
2438 static int wininfo_other_tab_diff __ARGS((wininfo_T *wip));
2441 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2442 * page. That's because a diff is local to a tab page.
2444 static int
2445 wininfo_other_tab_diff(wip)
2446 wininfo_T *wip;
2448 win_T *wp;
2450 if (wip->wi_opt.wo_diff)
2452 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2453 /* return FALSE when it's a window in the current tab page, thus
2454 * the buffer was in diff mode here */
2455 if (wip->wi_win == wp)
2456 return FALSE;
2457 return TRUE;
2459 return FALSE;
2461 #endif
2464 * Find info for the current window in buffer "buf".
2465 * If not found, return the info for the most recently used window.
2466 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2467 * another tab page.
2468 * Returns NULL when there isn't any info.
2470 static wininfo_T *
2471 find_wininfo(buf, skip_diff_buffer)
2472 buf_T *buf;
2473 int skip_diff_buffer UNUSED;
2475 wininfo_T *wip;
2477 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2478 if (wip->wi_win == curwin
2479 #ifdef FEAT_DIFF
2480 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2481 #endif
2483 break;
2485 /* If no wininfo for curwin, use the first in the list (that doesn't have
2486 * 'diff' set and is in another tab page). */
2487 if (wip == NULL)
2489 #ifdef FEAT_DIFF
2490 if (skip_diff_buffer)
2492 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2493 if (!wininfo_other_tab_diff(wip))
2494 break;
2496 else
2497 #endif
2498 wip = buf->b_wininfo;
2500 return wip;
2504 * Reset the local window options to the values last used in this window.
2505 * If the buffer wasn't used in this window before, use the values from
2506 * the most recently used window. If the values were never set, use the
2507 * global values for the window.
2509 void
2510 get_winopts(buf)
2511 buf_T *buf;
2513 wininfo_T *wip;
2515 clear_winopt(&curwin->w_onebuf_opt);
2516 #ifdef FEAT_FOLDING
2517 clearFolding(curwin);
2518 #endif
2520 wip = find_wininfo(buf, TRUE);
2521 if (wip != NULL && wip->wi_optset)
2523 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2524 #ifdef FEAT_FOLDING
2525 curwin->w_fold_manual = wip->wi_fold_manual;
2526 curwin->w_foldinvalid = TRUE;
2527 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2528 #endif
2530 else
2531 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2533 #ifdef FEAT_FOLDING
2534 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2535 if (p_fdls >= 0)
2536 curwin->w_p_fdl = p_fdls;
2537 #endif
2541 * Find the position (lnum and col) for the buffer 'buf' for the current
2542 * window.
2543 * Returns a pointer to no_position if no position is found.
2545 pos_T *
2546 buflist_findfpos(buf)
2547 buf_T *buf;
2549 wininfo_T *wip;
2550 static pos_T no_position = INIT_POS_T(1, 0, 0);
2552 wip = find_wininfo(buf, FALSE);
2553 if (wip != NULL)
2554 return &(wip->wi_fpos);
2555 else
2556 return &no_position;
2560 * Find the lnum for the buffer 'buf' for the current window.
2562 linenr_T
2563 buflist_findlnum(buf)
2564 buf_T *buf;
2566 return buflist_findfpos(buf)->lnum;
2569 #if defined(FEAT_LISTCMDS) || defined(PROTO)
2571 * List all know file names (for :files and :buffers command).
2573 void
2574 buflist_list(eap)
2575 exarg_T *eap;
2577 buf_T *buf;
2578 int len;
2579 int i;
2581 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2583 /* skip unlisted buffers, unless ! was used */
2584 if (!buf->b_p_bl && !eap->forceit)
2585 continue;
2586 msg_putchar('\n');
2587 if (buf_spname(buf) != NULL)
2588 STRCPY(NameBuff, buf_spname(buf));
2589 else
2590 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2592 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
2593 buf->b_fnum,
2594 buf->b_p_bl ? ' ' : 'u',
2595 buf == curbuf ? '%' :
2596 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2597 buf->b_ml.ml_mfp == NULL ? ' ' :
2598 (buf->b_nwindows == 0 ? 'h' : 'a'),
2599 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2600 (buf->b_flags & BF_READERR) ? 'x'
2601 : (bufIsChanged(buf) ? '+' : ' '),
2602 NameBuff);
2604 /* put "line 999" in column 40 or after the file name */
2605 i = 40 - vim_strsize(IObuff);
2608 IObuff[len++] = ' ';
2609 } while (--i > 0 && len < IOSIZE - 18);
2610 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2611 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
2612 : (long)buflist_findlnum(buf));
2613 msg_outtrans(IObuff);
2614 out_flush(); /* output one line at a time */
2615 ui_breakcheck();
2618 #endif
2621 * Get file name and line number for file 'fnum'.
2622 * Used by DoOneCmd() for translating '%' and '#'.
2623 * Used by insert_reg() and cmdline_paste() for '#' register.
2624 * Return FAIL if not found, OK for success.
2627 buflist_name_nr(fnum, fname, lnum)
2628 int fnum;
2629 char_u **fname;
2630 linenr_T *lnum;
2632 buf_T *buf;
2634 buf = buflist_findnr(fnum);
2635 if (buf == NULL || buf->b_fname == NULL)
2636 return FAIL;
2638 *fname = buf->b_fname;
2639 *lnum = buflist_findlnum(buf);
2641 return OK;
2645 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2646 * The file name with the full path is also remembered, for when :cd is used.
2647 * Returns FAIL for failure (file name already in use by other buffer)
2648 * OK otherwise.
2651 setfname(buf, ffname, sfname, message)
2652 buf_T *buf;
2653 char_u *ffname, *sfname;
2654 int message; /* give message when buffer already exists */
2656 buf_T *obuf = NULL;
2657 #ifdef UNIX
2658 struct stat st;
2659 #endif
2661 if (ffname == NULL || *ffname == NUL)
2663 /* Removing the name. */
2664 vim_free(buf->b_ffname);
2665 vim_free(buf->b_sfname);
2666 buf->b_ffname = NULL;
2667 buf->b_sfname = NULL;
2668 #ifdef UNIX
2669 st.st_dev = (dev_T)-1;
2670 #endif
2672 else
2674 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2675 if (ffname == NULL) /* out of memory */
2676 return FAIL;
2679 * if the file name is already used in another buffer:
2680 * - if the buffer is loaded, fail
2681 * - if the buffer is not loaded, delete it from the list
2683 #ifdef UNIX
2684 if (mch_stat((char *)ffname, &st) < 0)
2685 st.st_dev = (dev_T)-1;
2686 #endif
2687 if (!(buf->b_flags & BF_DUMMY))
2688 #ifdef UNIX
2689 obuf = buflist_findname_stat(ffname, &st);
2690 #else
2691 obuf = buflist_findname(ffname);
2692 #endif
2693 if (obuf != NULL && obuf != buf)
2695 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2697 if (message)
2698 EMSG(_("E95: Buffer with this name already exists"));
2699 vim_free(ffname);
2700 return FAIL;
2702 close_buffer(NULL, obuf, DOBUF_WIPE); /* delete from the list */
2704 sfname = vim_strsave(sfname);
2705 if (ffname == NULL || sfname == NULL)
2707 vim_free(sfname);
2708 vim_free(ffname);
2709 return FAIL;
2711 #ifdef USE_FNAME_CASE
2712 # ifdef USE_LONG_FNAME
2713 if (USE_LONG_FNAME)
2714 # endif
2715 fname_case(sfname, 0); /* set correct case for short file name */
2716 #endif
2717 vim_free(buf->b_ffname);
2718 vim_free(buf->b_sfname);
2719 buf->b_ffname = ffname;
2720 buf->b_sfname = sfname;
2722 buf->b_fname = buf->b_sfname;
2723 #ifdef UNIX
2724 if (st.st_dev == (dev_T)-1)
2725 buf->b_dev_valid = FALSE;
2726 else
2728 buf->b_dev_valid = TRUE;
2729 buf->b_dev = st.st_dev;
2730 buf->b_ino = st.st_ino;
2732 #endif
2734 #ifndef SHORT_FNAME
2735 buf->b_shortname = FALSE;
2736 #endif
2738 buf_name_changed(buf);
2739 return OK;
2743 * Crude way of changing the name of a buffer. Use with care!
2744 * The name should be relative to the current directory.
2746 void
2747 buf_set_name(fnum, name)
2748 int fnum;
2749 char_u *name;
2751 buf_T *buf;
2753 buf = buflist_findnr(fnum);
2754 if (buf != NULL)
2756 vim_free(buf->b_sfname);
2757 vim_free(buf->b_ffname);
2758 buf->b_ffname = vim_strsave(name);
2759 buf->b_sfname = NULL;
2760 /* Allocate ffname and expand into full path. Also resolves .lnk
2761 * files on Win32. */
2762 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
2763 buf->b_fname = buf->b_sfname;
2768 * Take care of what needs to be done when the name of buffer "buf" has
2769 * changed.
2771 void
2772 buf_name_changed(buf)
2773 buf_T *buf;
2776 * If the file name changed, also change the name of the swapfile
2778 if (buf->b_ml.ml_mfp != NULL)
2779 ml_setname(buf);
2781 if (curwin->w_buffer == buf)
2782 check_arg_idx(curwin); /* check file name for arg list */
2783 #ifdef FEAT_TITLE
2784 maketitle(); /* set window title */
2785 #endif
2786 #ifdef FEAT_WINDOWS
2787 status_redraw_all(); /* status lines need to be redrawn */
2788 #endif
2789 fmarks_check_names(buf); /* check named file marks */
2790 ml_timestamp(buf); /* reset timestamp */
2794 * set alternate file name for current window
2796 * Used by do_one_cmd(), do_write() and do_ecmd().
2797 * Return the buffer.
2799 buf_T *
2800 setaltfname(ffname, sfname, lnum)
2801 char_u *ffname;
2802 char_u *sfname;
2803 linenr_T lnum;
2805 buf_T *buf;
2807 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2808 buf = buflist_new(ffname, sfname, lnum, 0);
2809 if (buf != NULL && !cmdmod.keepalt)
2810 curwin->w_alt_fnum = buf->b_fnum;
2811 return buf;
2815 * Get alternate file name for current window.
2816 * Return NULL if there isn't any, and give error message if requested.
2818 char_u *
2819 getaltfname(errmsg)
2820 int errmsg; /* give error message */
2822 char_u *fname;
2823 linenr_T dummy;
2825 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2827 if (errmsg)
2828 EMSG(_(e_noalt));
2829 return NULL;
2831 return fname;
2835 * Add a file name to the buflist and return its number.
2836 * Uses same flags as buflist_new(), except BLN_DUMMY.
2838 * used by qf_init(), main() and doarglist()
2841 buflist_add(fname, flags)
2842 char_u *fname;
2843 int flags;
2845 buf_T *buf;
2847 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
2848 if (buf != NULL)
2849 return buf->b_fnum;
2850 return 0;
2853 #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2855 * Adjust slashes in file names. Called after 'shellslash' was set.
2857 void
2858 buflist_slash_adjust()
2860 buf_T *bp;
2862 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
2864 if (bp->b_ffname != NULL)
2865 slash_adjust(bp->b_ffname);
2866 if (bp->b_sfname != NULL)
2867 slash_adjust(bp->b_sfname);
2870 #endif
2873 * Set alternate cursor position for the current buffer and window "win".
2874 * Also save the local window option values.
2876 void
2877 buflist_altfpos(win)
2878 win_T *win;
2880 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
2884 * Return TRUE if 'ffname' is not the same file as current file.
2885 * Fname must have a full path (expanded by mch_FullName()).
2888 otherfile(ffname)
2889 char_u *ffname;
2891 return otherfile_buf(curbuf, ffname
2892 #ifdef UNIX
2893 , NULL
2894 #endif
2898 static int
2899 otherfile_buf(buf, ffname
2900 #ifdef UNIX
2901 , stp
2902 #endif
2904 buf_T *buf;
2905 char_u *ffname;
2906 #ifdef UNIX
2907 struct stat *stp;
2908 #endif
2910 /* no name is different */
2911 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
2912 return TRUE;
2913 if (fnamecmp(ffname, buf->b_ffname) == 0)
2914 return FALSE;
2915 #ifdef UNIX
2917 struct stat st;
2919 /* If no struct stat given, get it now */
2920 if (stp == NULL)
2922 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
2923 st.st_dev = (dev_T)-1;
2924 stp = &st;
2926 /* Use dev/ino to check if the files are the same, even when the names
2927 * are different (possible with links). Still need to compare the
2928 * name above, for when the file doesn't exist yet.
2929 * Problem: The dev/ino changes when a file is deleted (and created
2930 * again) and remains the same when renamed/moved. We don't want to
2931 * mch_stat() each buffer each time, that would be too slow. Get the
2932 * dev/ino again when they appear to match, but not when they appear
2933 * to be different: Could skip a buffer when it's actually the same
2934 * file. */
2935 if (buf_same_ino(buf, stp))
2937 buf_setino(buf);
2938 if (buf_same_ino(buf, stp))
2939 return FALSE;
2942 #endif
2943 return TRUE;
2946 #if defined(UNIX) || defined(PROTO)
2948 * Set inode and device number for a buffer.
2949 * Must always be called when b_fname is changed!.
2951 void
2952 buf_setino(buf)
2953 buf_T *buf;
2955 struct stat st;
2957 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
2959 buf->b_dev_valid = TRUE;
2960 buf->b_dev = st.st_dev;
2961 buf->b_ino = st.st_ino;
2963 else
2964 buf->b_dev_valid = FALSE;
2968 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
2970 static int
2971 buf_same_ino(buf, stp)
2972 buf_T *buf;
2973 struct stat *stp;
2975 return (buf->b_dev_valid
2976 && stp->st_dev == buf->b_dev
2977 && stp->st_ino == buf->b_ino);
2979 #endif
2982 * Print info about the current buffer.
2984 void
2985 fileinfo(fullname, shorthelp, dont_truncate)
2986 int fullname; /* when non-zero print full path */
2987 int shorthelp;
2988 int dont_truncate;
2990 char_u *name;
2991 int n;
2992 char_u *p;
2993 char_u *buffer;
2994 size_t len;
2996 buffer = alloc(IOSIZE);
2997 if (buffer == NULL)
2998 return;
3000 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3002 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
3003 p = buffer + STRLEN(buffer);
3005 else
3006 p = buffer;
3008 *p++ = '"';
3009 if (buf_spname(curbuf) != NULL)
3010 STRCPY(p, buf_spname(curbuf));
3011 else
3013 if (!fullname && curbuf->b_fname != NULL)
3014 name = curbuf->b_fname;
3015 else
3016 name = curbuf->b_ffname;
3017 home_replace(shorthelp ? curbuf : NULL, name, p,
3018 (int)(IOSIZE - (p - buffer)), TRUE);
3021 len = STRLEN(buffer);
3022 vim_snprintf((char *)buffer + len, IOSIZE - len,
3023 "\"%s%s%s%s%s%s",
3024 curbufIsChanged() ? (shortmess(SHM_MOD)
3025 ? " [+]" : _(" [Modified]")) : " ",
3026 (curbuf->b_flags & BF_NOTEDITED)
3027 #ifdef FEAT_QUICKFIX
3028 && !bt_dontwrite(curbuf)
3029 #endif
3030 ? _("[Not edited]") : "",
3031 (curbuf->b_flags & BF_NEW)
3032 #ifdef FEAT_QUICKFIX
3033 && !bt_dontwrite(curbuf)
3034 #endif
3035 ? _("[New file]") : "",
3036 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
3037 curbuf->b_p_ro ? (shortmess(SHM_RO) ? "[RO]"
3038 : _("[readonly]")) : "",
3039 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3040 || curbuf->b_p_ro) ?
3041 " " : "");
3042 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3043 * causes an overflow, thus for large numbers divide instead. */
3044 if (curwin->w_cursor.lnum > 1000000L)
3045 n = (int)(((long)curwin->w_cursor.lnum) /
3046 ((long)curbuf->b_ml.ml_line_count / 100L));
3047 else
3048 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3049 (long)curbuf->b_ml.ml_line_count);
3050 len = STRLEN(buffer);
3051 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3053 vim_snprintf((char *)buffer + len, IOSIZE - len, "%s", _(no_lines_msg));
3055 #ifdef FEAT_CMDL_INFO
3056 else if (p_ru)
3058 /* Current line and column are already on the screen -- webb */
3059 if (curbuf->b_ml.ml_line_count == 1)
3060 vim_snprintf((char *)buffer + len, IOSIZE - len,
3061 _("1 line --%d%%--"), n);
3062 else
3063 vim_snprintf((char *)buffer + len, IOSIZE - len,
3064 _("%ld lines --%d%%--"),
3065 (long)curbuf->b_ml.ml_line_count, n);
3067 #endif
3068 else
3070 vim_snprintf((char *)buffer + len, IOSIZE - len,
3071 _("line %ld of %ld --%d%%-- col "),
3072 (long)curwin->w_cursor.lnum,
3073 (long)curbuf->b_ml.ml_line_count,
3075 validate_virtcol();
3076 len = STRLEN(buffer);
3077 col_print(buffer + len, IOSIZE - len,
3078 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3081 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
3083 if (dont_truncate)
3085 /* Temporarily set msg_scroll to avoid the message being truncated.
3086 * First call msg_start() to get the message in the right place. */
3087 msg_start();
3088 n = msg_scroll;
3089 msg_scroll = TRUE;
3090 msg(buffer);
3091 msg_scroll = n;
3093 else
3095 p = msg_trunc_attr(buffer, FALSE, 0);
3096 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
3097 /* Need to repeat the message after redrawing when:
3098 * - When restart_edit is set (otherwise there will be a delay
3099 * before redrawing).
3100 * - When the screen was scrolled but there is no wait-return
3101 * prompt. */
3102 set_keep_msg(p, 0);
3105 vim_free(buffer);
3108 void
3109 col_print(buf, buflen, col, vcol)
3110 char_u *buf;
3111 size_t buflen;
3112 int col;
3113 int vcol;
3115 if (col == vcol)
3116 vim_snprintf((char *)buf, buflen, "%d", col);
3117 else
3118 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
3121 #if defined(FEAT_TITLE) || defined(PROTO)
3123 * put file name in title bar of window and in icon title
3126 static char_u *lasttitle = NULL;
3127 static char_u *lasticon = NULL;
3129 void
3130 maketitle()
3132 char_u *p;
3133 char_u *t_str = NULL;
3134 char_u *i_name;
3135 char_u *i_str = NULL;
3136 int maxlen = 0;
3137 int len;
3138 int mustset;
3139 char_u buf[IOSIZE];
3140 int off;
3142 if (!redrawing())
3144 /* Postpone updating the title when 'lazyredraw' is set. */
3145 need_maketitle = TRUE;
3146 return;
3149 #ifdef FEAT_GUI_MACVIM
3150 gui_macvim_update_modified_flag();
3151 #endif
3153 need_maketitle = FALSE;
3154 if (!p_title && !p_icon)
3155 return;
3157 if (p_title)
3159 if (p_titlelen > 0)
3161 maxlen = p_titlelen * Columns / 100;
3162 if (maxlen < 10)
3163 maxlen = 10;
3166 t_str = buf;
3167 if (*p_titlestring != NUL)
3169 #ifdef FEAT_STL_OPT
3170 if (stl_syntax & STL_IN_TITLE)
3172 int use_sandbox = FALSE;
3173 int save_called_emsg = called_emsg;
3175 # ifdef FEAT_EVAL
3176 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
3177 # endif
3178 called_emsg = FALSE;
3179 build_stl_str_hl(curwin, t_str, sizeof(buf),
3180 p_titlestring, use_sandbox,
3181 0, maxlen, NULL, NULL);
3182 if (called_emsg)
3183 set_string_option_direct((char_u *)"titlestring", -1,
3184 (char_u *)"", OPT_FREE, SID_ERROR);
3185 called_emsg |= save_called_emsg;
3187 else
3188 #endif
3189 t_str = p_titlestring;
3191 else
3193 /* format: "fname + (path) (1 of 2) - VIM" */
3195 if (curbuf->b_fname == NULL)
3196 STRCPY(buf, _("[No Name]"));
3197 else
3199 p = transstr(gettail(curbuf->b_fname));
3200 vim_strncpy(buf, p, IOSIZE - 100);
3201 vim_free(p);
3204 switch (bufIsChanged(curbuf)
3205 + (curbuf->b_p_ro * 2)
3206 + (!curbuf->b_p_ma * 4))
3208 case 1: STRCAT(buf, " +"); break;
3209 case 2: STRCAT(buf, " ="); break;
3210 case 3: STRCAT(buf, " =+"); break;
3211 case 4:
3212 case 6: STRCAT(buf, " -"); break;
3213 case 5:
3214 case 7: STRCAT(buf, " -+"); break;
3217 if (curbuf->b_fname != NULL)
3219 /* Get path of file, replace home dir with ~ */
3220 off = (int)STRLEN(buf);
3221 buf[off++] = ' ';
3222 buf[off++] = '(';
3223 home_replace(curbuf, curbuf->b_ffname,
3224 buf + off, IOSIZE - off, TRUE);
3225 #ifdef BACKSLASH_IN_FILENAME
3226 /* avoid "c:/name" to be reduced to "c" */
3227 if (isalpha(buf[off]) && buf[off + 1] == ':')
3228 off += 2;
3229 #endif
3230 /* remove the file name */
3231 p = gettail_sep(buf + off);
3232 if (p == buf + off)
3233 /* must be a help buffer */
3234 vim_strncpy(buf + off, (char_u *)_("help"),
3235 (size_t)(IOSIZE - off - 1));
3236 else
3237 *p = NUL;
3239 /* translate unprintable chars */
3240 p = transstr(buf + off);
3241 vim_strncpy(buf + off, p, (size_t)(IOSIZE - off - 1));
3242 vim_free(p);
3243 STRCAT(buf, ")");
3246 #ifndef FEAT_GUI_MACVIM
3247 append_arg_number(curwin, buf, FALSE, IOSIZE);
3248 #endif
3250 #if defined(FEAT_CLIENTSERVER)
3251 if (serverName != NULL)
3253 STRCAT(buf, " - ");
3254 STRCAT(buf, serverName);
3256 else
3257 #endif
3258 STRCAT(buf, " - VIM");
3260 if (maxlen > 0)
3262 /* make it shorter by removing a bit in the middle */
3263 len = vim_strsize(buf);
3264 if (len > maxlen)
3265 trunc_string(buf, buf, maxlen);
3269 mustset = ti_change(t_str, &lasttitle);
3271 if (p_icon)
3273 i_str = buf;
3274 if (*p_iconstring != NUL)
3276 #ifdef FEAT_STL_OPT
3277 if (stl_syntax & STL_IN_ICON)
3279 int use_sandbox = FALSE;
3280 int save_called_emsg = called_emsg;
3282 # ifdef FEAT_EVAL
3283 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
3284 # endif
3285 called_emsg = FALSE;
3286 build_stl_str_hl(curwin, i_str, sizeof(buf),
3287 p_iconstring, use_sandbox,
3288 0, 0, NULL, NULL);
3289 if (called_emsg)
3290 set_string_option_direct((char_u *)"iconstring", -1,
3291 (char_u *)"", OPT_FREE, SID_ERROR);
3292 called_emsg |= save_called_emsg;
3294 else
3295 #endif
3296 i_str = p_iconstring;
3298 else
3300 if (buf_spname(curbuf) != NULL)
3301 i_name = (char_u *)buf_spname(curbuf);
3302 else /* use file name only in icon */
3303 i_name = gettail(curbuf->b_ffname);
3304 *i_str = NUL;
3305 /* Truncate name at 100 bytes. */
3306 len = (int)STRLEN(i_name);
3307 if (len > 100)
3309 len -= 100;
3310 #ifdef FEAT_MBYTE
3311 if (has_mbyte)
3312 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3313 #endif
3314 i_name += len;
3316 STRCPY(i_str, i_name);
3317 trans_characters(i_str, IOSIZE);
3321 mustset |= ti_change(i_str, &lasticon);
3323 if (mustset)
3324 resettitle();
3328 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3329 * from "str" if it does.
3330 * Return TRUE when "*last" changed.
3332 static int
3333 ti_change(str, last)
3334 char_u *str;
3335 char_u **last;
3337 if ((str == NULL) != (*last == NULL)
3338 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3340 vim_free(*last);
3341 if (str == NULL)
3342 *last = NULL;
3343 else
3344 *last = vim_strsave(str);
3345 return TRUE;
3347 return FALSE;
3351 * Put current window title back (used after calling a shell)
3353 void
3354 resettitle()
3356 mch_settitle(lasttitle, lasticon);
3359 # if defined(EXITFREE) || defined(PROTO)
3360 void
3361 free_titles()
3363 vim_free(lasttitle);
3364 vim_free(lasticon);
3366 # endif
3368 #endif /* FEAT_TITLE */
3370 #if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
3372 * Build a string from the status line items in "fmt".
3373 * Return length of string in screen cells.
3375 * Normally works for window "wp", except when working for 'tabline' then it
3376 * is "curwin".
3378 * Items are drawn interspersed with the text that surrounds it
3379 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3380 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3382 * If maxwidth is not zero, the string will be filled at any middle marker
3383 * or truncated if too long, fillchar is used for all whitespace.
3386 build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar, maxwidth, hltab, tabtab)
3387 win_T *wp;
3388 char_u *out; /* buffer to write into != NameBuff */
3389 size_t outlen; /* length of out[] */
3390 char_u *fmt;
3391 int use_sandbox UNUSED; /* "fmt" was set insecurely, use sandbox */
3392 int fillchar;
3393 int maxwidth;
3394 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3395 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
3397 char_u *p;
3398 char_u *s;
3399 char_u *t;
3400 char_u *linecont;
3401 #ifdef FEAT_EVAL
3402 win_T *o_curwin;
3403 buf_T *o_curbuf;
3404 #endif
3405 int empty_line;
3406 colnr_T virtcol;
3407 long l;
3408 long n;
3409 int prevchar_isflag;
3410 int prevchar_isitem;
3411 int itemisflag;
3412 int fillable;
3413 char_u *str;
3414 long num;
3415 int width;
3416 int itemcnt;
3417 int curitem;
3418 int groupitem[STL_MAX_ITEM];
3419 int groupdepth;
3420 struct stl_item
3422 char_u *start;
3423 int minwid;
3424 int maxwid;
3425 enum
3427 Normal,
3428 Empty,
3429 Group,
3430 Middle,
3431 Highlight,
3432 TabPage,
3433 Trunc
3434 } type;
3435 } item[STL_MAX_ITEM];
3436 int minwid;
3437 int maxwid;
3438 int zeropad;
3439 char_u base;
3440 char_u opt;
3441 #define TMPLEN 70
3442 char_u tmp[TMPLEN];
3443 char_u *usefmt = fmt;
3444 struct stl_hlrec *sp;
3446 #ifdef FEAT_EVAL
3448 * When the format starts with "%!" then evaluate it as an expression and
3449 * use the result as the actual format string.
3451 if (fmt[0] == '%' && fmt[1] == '!')
3453 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3454 if (usefmt == NULL)
3455 usefmt = fmt;
3457 #endif
3459 if (fillchar == 0)
3460 fillchar = ' ';
3461 #ifdef FEAT_MBYTE
3462 /* Can't handle a multi-byte fill character yet. */
3463 else if (mb_char2len(fillchar) > 1)
3464 fillchar = '-';
3465 #endif
3468 * Get line & check if empty (cursorpos will show "0-1").
3469 * If inversion is possible we use it. Else '=' characters are used.
3471 linecont = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3472 empty_line = (*linecont == NUL);
3474 groupdepth = 0;
3475 p = out;
3476 curitem = 0;
3477 prevchar_isflag = TRUE;
3478 prevchar_isitem = FALSE;
3479 for (s = usefmt; *s; )
3481 if (*s != NUL && *s != '%')
3482 prevchar_isflag = prevchar_isitem = FALSE;
3485 * Handle up to the next '%' or the end.
3487 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3488 *p++ = *s++;
3489 if (*s == NUL || p + 1 >= out + outlen)
3490 break;
3493 * Handle one '%' item.
3495 s++;
3496 if (*s == '%')
3498 if (p + 1 >= out + outlen)
3499 break;
3500 *p++ = *s++;
3501 prevchar_isflag = prevchar_isitem = FALSE;
3502 continue;
3504 if (*s == STL_MIDDLEMARK)
3506 s++;
3507 if (groupdepth > 0)
3508 continue;
3509 item[curitem].type = Middle;
3510 item[curitem++].start = p;
3511 continue;
3513 if (*s == STL_TRUNCMARK)
3515 s++;
3516 item[curitem].type = Trunc;
3517 item[curitem++].start = p;
3518 continue;
3520 if (*s == ')')
3522 s++;
3523 if (groupdepth < 1)
3524 continue;
3525 groupdepth--;
3527 t = item[groupitem[groupdepth]].start;
3528 *p = NUL;
3529 l = vim_strsize(t);
3530 if (curitem > groupitem[groupdepth] + 1
3531 && item[groupitem[groupdepth]].minwid == 0)
3533 /* remove group if all items are empty */
3534 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3535 if (item[n].type == Normal)
3536 break;
3537 if (n == curitem)
3539 p = t;
3540 l = 0;
3543 if (l > item[groupitem[groupdepth]].maxwid)
3545 /* truncate, remove n bytes of text at the start */
3546 #ifdef FEAT_MBYTE
3547 if (has_mbyte)
3549 /* Find the first character that should be included. */
3550 n = 0;
3551 while (l >= item[groupitem[groupdepth]].maxwid)
3553 l -= ptr2cells(t + n);
3554 n += (*mb_ptr2len)(t + n);
3557 else
3558 #endif
3559 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
3561 *t = '<';
3562 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
3563 p = p - n + 1;
3564 #ifdef FEAT_MBYTE
3565 /* Fill up space left over by half a double-wide char. */
3566 while (++l < item[groupitem[groupdepth]].minwid)
3567 *p++ = fillchar;
3568 #endif
3570 /* correct the start of the items for the truncation */
3571 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3573 item[l].start -= n;
3574 if (item[l].start < t)
3575 item[l].start = t;
3578 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3580 /* fill */
3581 n = item[groupitem[groupdepth]].minwid;
3582 if (n < 0)
3584 /* fill by appending characters */
3585 n = 0 - n;
3586 while (l++ < n && p + 1 < out + outlen)
3587 *p++ = fillchar;
3589 else
3591 /* fill by inserting characters */
3592 mch_memmove(t + n - l, t, (size_t)(p - t));
3593 l = n - l;
3594 if (p + l >= out + outlen)
3595 l = (long)((out + outlen) - p - 1);
3596 p += l;
3597 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3598 item[n].start += l;
3599 for ( ; l > 0; l--)
3600 *t++ = fillchar;
3603 continue;
3605 minwid = 0;
3606 maxwid = 9999;
3607 zeropad = FALSE;
3608 l = 1;
3609 if (*s == '0')
3611 s++;
3612 zeropad = TRUE;
3614 if (*s == '-')
3616 s++;
3617 l = -1;
3619 if (VIM_ISDIGIT(*s))
3621 minwid = (int)getdigits(&s);
3622 if (minwid < 0) /* overflow */
3623 minwid = 0;
3625 if (*s == STL_USER_HL)
3627 item[curitem].type = Highlight;
3628 item[curitem].start = p;
3629 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3630 s++;
3631 curitem++;
3632 continue;
3634 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3636 if (*s == STL_TABCLOSENR)
3638 if (minwid == 0)
3640 /* %X ends the close label, go back to the previously
3641 * define tab label nr. */
3642 for (n = curitem - 1; n >= 0; --n)
3643 if (item[n].type == TabPage && item[n].minwid >= 0)
3645 minwid = item[n].minwid;
3646 break;
3649 else
3650 /* close nrs are stored as negative values */
3651 minwid = - minwid;
3653 item[curitem].type = TabPage;
3654 item[curitem].start = p;
3655 item[curitem].minwid = minwid;
3656 s++;
3657 curitem++;
3658 continue;
3660 if (*s == '.')
3662 s++;
3663 if (VIM_ISDIGIT(*s))
3665 maxwid = (int)getdigits(&s);
3666 if (maxwid <= 0) /* overflow */
3667 maxwid = 50;
3670 minwid = (minwid > 50 ? 50 : minwid) * l;
3671 if (*s == '(')
3673 groupitem[groupdepth++] = curitem;
3674 item[curitem].type = Group;
3675 item[curitem].start = p;
3676 item[curitem].minwid = minwid;
3677 item[curitem].maxwid = maxwid;
3678 s++;
3679 curitem++;
3680 continue;
3682 if (vim_strchr(STL_ALL, *s) == NULL)
3684 s++;
3685 continue;
3687 opt = *s++;
3689 /* OK - now for the real work */
3690 base = 'D';
3691 itemisflag = FALSE;
3692 fillable = TRUE;
3693 num = -1;
3694 str = NULL;
3695 switch (opt)
3697 case STL_FILEPATH:
3698 case STL_FULLPATH:
3699 case STL_FILENAME:
3700 fillable = FALSE; /* don't change ' ' to fillchar */
3701 if (buf_spname(wp->w_buffer) != NULL)
3702 STRCPY(NameBuff, buf_spname(wp->w_buffer));
3703 else
3705 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
3706 : wp->w_buffer->b_fname;
3707 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3709 trans_characters(NameBuff, MAXPATHL);
3710 if (opt != STL_FILENAME)
3711 str = NameBuff;
3712 else
3713 str = gettail(NameBuff);
3714 break;
3716 case STL_VIM_EXPR: /* '{' */
3717 itemisflag = TRUE;
3718 t = p;
3719 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3720 *p++ = *s++;
3721 if (*s != '}') /* missing '}' or out of space */
3722 break;
3723 s++;
3724 *p = 0;
3725 p = t;
3727 #ifdef FEAT_EVAL
3728 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
3729 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3731 o_curbuf = curbuf;
3732 o_curwin = curwin;
3733 curwin = wp;
3734 curbuf = wp->w_buffer;
3736 str = eval_to_string_safe(p, &t, use_sandbox);
3738 curwin = o_curwin;
3739 curbuf = o_curbuf;
3740 do_unlet((char_u *)"g:actual_curbuf", TRUE);
3742 if (str != NULL && *str != 0)
3744 if (*skipdigits(str) == NUL)
3746 num = atoi((char *)str);
3747 vim_free(str);
3748 str = NULL;
3749 itemisflag = FALSE;
3752 #endif
3753 break;
3755 case STL_LINE:
3756 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3757 ? 0L : (long)(wp->w_cursor.lnum);
3758 break;
3760 case STL_NUMLINES:
3761 num = wp->w_buffer->b_ml.ml_line_count;
3762 break;
3764 case STL_COLUMN:
3765 num = !(State & INSERT) && empty_line
3766 ? 0 : (int)wp->w_cursor.col + 1;
3767 break;
3769 case STL_VIRTCOL:
3770 case STL_VIRTCOL_ALT:
3771 /* In list mode virtcol needs to be recomputed */
3772 virtcol = wp->w_virtcol;
3773 if (wp->w_p_list && lcs_tab1 == NUL)
3775 wp->w_p_list = FALSE;
3776 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3777 wp->w_p_list = TRUE;
3779 ++virtcol;
3780 /* Don't display %V if it's the same as %c. */
3781 if (opt == STL_VIRTCOL_ALT
3782 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3783 ? 0 : (int)wp->w_cursor.col + 1)))
3784 break;
3785 num = (long)virtcol;
3786 break;
3788 case STL_PERCENTAGE:
3789 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3790 (long)wp->w_buffer->b_ml.ml_line_count);
3791 break;
3793 case STL_ALTPERCENT:
3794 str = tmp;
3795 get_rel_pos(wp, str, TMPLEN);
3796 break;
3798 case STL_ARGLISTSTAT:
3799 fillable = FALSE;
3800 tmp[0] = 0;
3801 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
3802 str = tmp;
3803 break;
3805 case STL_KEYMAP:
3806 fillable = FALSE;
3807 if (get_keymap_str(wp, tmp, TMPLEN))
3808 str = tmp;
3809 break;
3810 case STL_PAGENUM:
3811 #if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
3812 num = printer_page_num;
3813 #else
3814 num = 0;
3815 #endif
3816 break;
3818 case STL_BUFNO:
3819 num = wp->w_buffer->b_fnum;
3820 break;
3822 case STL_OFFSET_X:
3823 base = 'X';
3824 case STL_OFFSET:
3825 #ifdef FEAT_BYTEOFF
3826 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
3827 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
3828 0L : l + 1 + (!(State & INSERT) && empty_line ?
3829 0 : (int)wp->w_cursor.col);
3830 #endif
3831 break;
3833 case STL_BYTEVAL_X:
3834 base = 'X';
3835 case STL_BYTEVAL:
3836 if (wp->w_cursor.col > (colnr_T)STRLEN(linecont))
3837 num = 0;
3838 else
3840 #ifdef FEAT_MBYTE
3841 num = (*mb_ptr2char)(linecont + wp->w_cursor.col);
3842 #else
3843 num = linecont[wp->w_cursor.col];
3844 #endif
3846 if (num == NL)
3847 num = 0;
3848 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
3849 num = NL;
3850 break;
3852 case STL_ROFLAG:
3853 case STL_ROFLAG_ALT:
3854 itemisflag = TRUE;
3855 if (wp->w_buffer->b_p_ro)
3856 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : "[RO]");
3857 break;
3859 case STL_HELPFLAG:
3860 case STL_HELPFLAG_ALT:
3861 itemisflag = TRUE;
3862 if (wp->w_buffer->b_help)
3863 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
3864 : _("[Help]"));
3865 break;
3867 #ifdef FEAT_AUTOCMD
3868 case STL_FILETYPE:
3869 if (*wp->w_buffer->b_p_ft != NUL
3870 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
3872 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
3873 wp->w_buffer->b_p_ft);
3874 str = tmp;
3876 break;
3878 case STL_FILETYPE_ALT:
3879 itemisflag = TRUE;
3880 if (*wp->w_buffer->b_p_ft != NUL
3881 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
3883 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
3884 wp->w_buffer->b_p_ft);
3885 for (t = tmp; *t != 0; t++)
3886 *t = TOUPPER_LOC(*t);
3887 str = tmp;
3889 break;
3890 #endif
3892 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3893 case STL_PREVIEWFLAG:
3894 case STL_PREVIEWFLAG_ALT:
3895 itemisflag = TRUE;
3896 if (wp->w_p_pvw)
3897 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
3898 : _("[Preview]"));
3899 break;
3900 #endif
3902 case STL_MODIFIED:
3903 case STL_MODIFIED_ALT:
3904 itemisflag = TRUE;
3905 switch ((opt == STL_MODIFIED_ALT)
3906 + bufIsChanged(wp->w_buffer) * 2
3907 + (!wp->w_buffer->b_p_ma) * 4)
3909 case 2: str = (char_u *)"[+]"; break;
3910 case 3: str = (char_u *)",+"; break;
3911 case 4: str = (char_u *)"[-]"; break;
3912 case 5: str = (char_u *)",-"; break;
3913 case 6: str = (char_u *)"[+-]"; break;
3914 case 7: str = (char_u *)",+-"; break;
3916 break;
3918 case STL_HIGHLIGHT:
3919 t = s;
3920 while (*s != '#' && *s != NUL)
3921 ++s;
3922 if (*s == '#')
3924 item[curitem].type = Highlight;
3925 item[curitem].start = p;
3926 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
3927 curitem++;
3929 ++s;
3930 continue;
3933 item[curitem].start = p;
3934 item[curitem].type = Normal;
3935 if (str != NULL && *str)
3937 t = str;
3938 if (itemisflag)
3940 if ((t[0] && t[1])
3941 && ((!prevchar_isitem && *t == ',')
3942 || (prevchar_isflag && *t == ' ')))
3943 t++;
3944 prevchar_isflag = TRUE;
3946 l = vim_strsize(t);
3947 if (l > 0)
3948 prevchar_isitem = TRUE;
3949 if (l > maxwid)
3951 while (l >= maxwid)
3952 #ifdef FEAT_MBYTE
3953 if (has_mbyte)
3955 l -= ptr2cells(t);
3956 t += (*mb_ptr2len)(t);
3958 else
3959 #endif
3960 l -= byte2cells(*t++);
3961 if (p + 1 >= out + outlen)
3962 break;
3963 *p++ = '<';
3965 if (minwid > 0)
3967 for (; l < minwid && p + 1 < out + outlen; l++)
3969 /* Don't put a "-" in front of a digit. */
3970 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
3971 *p++ = ' ';
3972 else
3973 *p++ = fillchar;
3975 minwid = 0;
3977 else
3978 minwid *= -1;
3979 while (*t && p + 1 < out + outlen)
3981 *p++ = *t++;
3982 /* Change a space by fillchar, unless fillchar is '-' and a
3983 * digit follows. */
3984 if (fillable && p[-1] == ' '
3985 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
3986 p[-1] = fillchar;
3988 for (; l < minwid && p + 1 < out + outlen; l++)
3989 *p++ = fillchar;
3991 else if (num >= 0)
3993 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
3994 char_u nstr[20];
3996 if (p + 20 >= out + outlen)
3997 break; /* not sufficient space */
3998 prevchar_isitem = TRUE;
3999 t = nstr;
4000 if (opt == STL_VIRTCOL_ALT)
4002 *t++ = '-';
4003 minwid--;
4005 *t++ = '%';
4006 if (zeropad)
4007 *t++ = '0';
4008 *t++ = '*';
4009 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
4010 *t = 0;
4012 for (n = num, l = 1; n >= nbase; n /= nbase)
4013 l++;
4014 if (opt == STL_VIRTCOL_ALT)
4015 l++;
4016 if (l > maxwid)
4018 l += 2;
4019 n = l - maxwid;
4020 while (l-- > maxwid)
4021 num /= nbase;
4022 *t++ = '>';
4023 *t++ = '%';
4024 *t = t[-3];
4025 *++t = 0;
4026 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4027 0, num, n);
4029 else
4030 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4031 minwid, num);
4032 p += STRLEN(p);
4034 else
4035 item[curitem].type = Empty;
4037 if (opt == STL_VIM_EXPR)
4038 vim_free(str);
4040 if (num >= 0 || (!itemisflag && str && *str))
4041 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4042 curitem++;
4044 *p = NUL;
4045 itemcnt = curitem;
4047 #ifdef FEAT_EVAL
4048 if (usefmt != fmt)
4049 vim_free(usefmt);
4050 #endif
4052 width = vim_strsize(out);
4053 if (maxwidth > 0 && width > maxwidth)
4055 /* Result is too long, must truncate somewhere. */
4056 l = 0;
4057 if (itemcnt == 0)
4058 s = out;
4059 else
4061 for ( ; l < itemcnt; l++)
4062 if (item[l].type == Trunc)
4064 /* Truncate at %< item. */
4065 s = item[l].start;
4066 break;
4068 if (l == itemcnt)
4070 /* No %< item, truncate first item. */
4071 s = item[0].start;
4072 l = 0;
4076 if (width - vim_strsize(s) >= maxwidth)
4078 /* Truncation mark is beyond max length */
4079 #ifdef FEAT_MBYTE
4080 if (has_mbyte)
4082 s = out;
4083 width = 0;
4084 for (;;)
4086 width += ptr2cells(s);
4087 if (width >= maxwidth)
4088 break;
4089 s += (*mb_ptr2len)(s);
4091 /* Fill up for half a double-wide character. */
4092 while (++width < maxwidth)
4093 *s++ = fillchar;
4095 else
4096 #endif
4097 s = out + maxwidth - 1;
4098 for (l = 0; l < itemcnt; l++)
4099 if (item[l].start > s)
4100 break;
4101 itemcnt = l;
4102 *s++ = '>';
4103 *s = 0;
4105 else
4107 #ifdef FEAT_MBYTE
4108 if (has_mbyte)
4110 n = 0;
4111 while (width >= maxwidth)
4113 width -= ptr2cells(s + n);
4114 n += (*mb_ptr2len)(s + n);
4117 else
4118 #endif
4119 n = width - maxwidth + 1;
4120 p = s + n;
4121 STRMOVE(s + 1, p);
4122 *s = '<';
4124 /* Fill up for half a double-wide character. */
4125 while (++width < maxwidth)
4127 s = s + STRLEN(s);
4128 *s++ = fillchar;
4129 *s = NUL;
4132 --n; /* count the '<' */
4133 for (; l < itemcnt; l++)
4135 if (item[l].start - n >= s)
4136 item[l].start -= n;
4137 else
4138 item[l].start = s;
4141 width = maxwidth;
4143 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4145 /* Apply STL_MIDDLE if any */
4146 for (l = 0; l < itemcnt; l++)
4147 if (item[l].type == Middle)
4148 break;
4149 if (l < itemcnt)
4151 p = item[l].start + maxwidth - width;
4152 STRMOVE(p, item[l].start);
4153 for (s = item[l].start; s < p; s++)
4154 *s = fillchar;
4155 for (l++; l < itemcnt; l++)
4156 item[l].start += maxwidth - width;
4157 width = maxwidth;
4161 /* Store the info about highlighting. */
4162 if (hltab != NULL)
4164 sp = hltab;
4165 for (l = 0; l < itemcnt; l++)
4167 if (item[l].type == Highlight)
4169 sp->start = item[l].start;
4170 sp->userhl = item[l].minwid;
4171 sp++;
4174 sp->start = NULL;
4175 sp->userhl = 0;
4178 /* Store the info about tab pages labels. */
4179 if (tabtab != NULL)
4181 sp = tabtab;
4182 for (l = 0; l < itemcnt; l++)
4184 if (item[l].type == TabPage)
4186 sp->start = item[l].start;
4187 sp->userhl = item[l].minwid;
4188 sp++;
4191 sp->start = NULL;
4192 sp->userhl = 0;
4195 return width;
4197 #endif /* FEAT_STL_OPT */
4199 #if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4200 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
4202 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4203 * using "Top", "Bot" or "All" when appropriate.
4205 void
4206 get_rel_pos(wp, buf, buflen)
4207 win_T *wp;
4208 char_u *buf;
4209 int buflen;
4211 long above; /* number of lines above window */
4212 long below; /* number of lines below window */
4214 above = wp->w_topline - 1;
4215 #ifdef FEAT_DIFF
4216 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
4217 #endif
4218 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4219 if (below <= 0)
4220 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4221 (size_t)(buflen - 1));
4222 else if (above <= 0)
4223 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
4224 else
4225 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
4226 ? (int)(above / ((above + below) / 100L))
4227 : (int)(above * 100L / (above + below)));
4229 #endif
4232 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
4233 * Return TRUE if it was appended.
4235 static int
4236 append_arg_number(wp, buf, buflen, add_file)
4237 win_T *wp;
4238 char_u *buf;
4239 int buflen;
4240 int add_file; /* Add "file" before the arg number */
4242 char_u *p;
4244 if (ARGCOUNT <= 1) /* nothing to do */
4245 return FALSE;
4247 p = buf + STRLEN(buf); /* go to the end of the buffer */
4248 if (p - buf + 35 >= buflen) /* getting too long */
4249 return FALSE;
4250 *p++ = ' ';
4251 *p++ = '(';
4252 if (add_file)
4254 STRCPY(p, "file ");
4255 p += 5;
4257 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4258 wp->w_arg_idx_invalid ? "(%d) of %d)"
4259 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4260 return TRUE;
4264 * If fname is not a full path, make it a full path.
4265 * Returns pointer to allocated memory (NULL for failure).
4267 char_u *
4268 fix_fname(fname)
4269 char_u *fname;
4272 * Force expanding the path always for Unix, because symbolic links may
4273 * mess up the full path name, even though it starts with a '/'.
4274 * Also expand when there is ".." in the file name, try to remove it,
4275 * because "c:/src/../README" is equal to "c:/README".
4276 * Similarly "c:/src//file" is equal to "c:/src/file".
4277 * For MS-Windows also expand names like "longna~1" to "longname".
4279 #ifdef UNIX
4280 return FullName_save(fname, TRUE);
4281 #else
4282 if (!vim_isAbsName(fname)
4283 || strstr((char *)fname, "..") != NULL
4284 || strstr((char *)fname, "//") != NULL
4285 # ifdef BACKSLASH_IN_FILENAME
4286 || strstr((char *)fname, "\\\\") != NULL
4287 # endif
4288 # if defined(MSWIN) || defined(DJGPP)
4289 || vim_strchr(fname, '~') != NULL
4290 # endif
4292 return FullName_save(fname, FALSE);
4294 fname = vim_strsave(fname);
4296 # ifdef USE_FNAME_CASE
4297 # ifdef USE_LONG_FNAME
4298 if (USE_LONG_FNAME)
4299 # endif
4301 if (fname != NULL)
4302 fname_case(fname, 0); /* set correct case for file name */
4304 # endif
4306 return fname;
4307 #endif
4311 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4312 * "ffname" becomes a pointer to allocated memory (or NULL).
4314 void
4315 fname_expand(buf, ffname, sfname)
4316 buf_T *buf UNUSED;
4317 char_u **ffname;
4318 char_u **sfname;
4320 if (*ffname == NULL) /* if no file name given, nothing to do */
4321 return;
4322 if (*sfname == NULL) /* if no short file name given, use ffname */
4323 *sfname = *ffname;
4324 *ffname = fix_fname(*ffname); /* expand to full path */
4326 #ifdef FEAT_SHORTCUT
4327 if (!buf->b_p_bin)
4329 char_u *rfname;
4331 /* If the file name is a shortcut file, use the file it links to. */
4332 rfname = mch_resolve_shortcut(*ffname);
4333 if (rfname != NULL)
4335 vim_free(*ffname);
4336 *ffname = rfname;
4337 *sfname = rfname;
4340 #endif
4344 * Get the file name for an argument list entry.
4346 char_u *
4347 alist_name(aep)
4348 aentry_T *aep;
4350 buf_T *bp;
4352 /* Use the name from the associated buffer if it exists. */
4353 bp = buflist_findnr(aep->ae_fnum);
4354 if (bp == NULL || bp->b_fname == NULL)
4355 return aep->ae_fname;
4356 return bp->b_fname;
4359 #if defined(FEAT_WINDOWS) || defined(PROTO)
4361 * do_arg_all(): Open up to 'count' windows, one for each argument.
4363 void
4364 do_arg_all(count, forceit, keep_tabs)
4365 int count;
4366 int forceit; /* hide buffers in current windows */
4367 int keep_tabs; /* keep current tabs, for ":tab drop file" */
4369 int i;
4370 win_T *wp, *wpnext;
4371 char_u *opened; /* array of flags for which args are open */
4372 int opened_len; /* length of opened[] */
4373 int use_firstwin = FALSE; /* use first window for arglist */
4374 int split_ret = OK;
4375 int p_ea_save;
4376 alist_T *alist; /* argument list to be used */
4377 buf_T *buf;
4378 tabpage_T *tpnext;
4379 int had_tab = cmdmod.tab;
4380 win_T *new_curwin = NULL;
4381 tabpage_T *new_curtab = NULL;
4383 if (ARGCOUNT <= 0)
4385 /* Don't give an error message. We don't want it when the ":all"
4386 * command is in the .vimrc. */
4387 return;
4389 setpcmark();
4391 opened_len = ARGCOUNT;
4392 opened = alloc_clear((unsigned)opened_len);
4393 if (opened == NULL)
4394 return;
4396 #ifdef FEAT_GUI
4397 need_mouse_correct = TRUE;
4398 #endif
4401 * Try closing all windows that are not in the argument list.
4402 * Also close windows that are not full width;
4403 * When 'hidden' or "forceit" set the buffer becomes hidden.
4404 * Windows that have a changed buffer and can't be hidden won't be closed.
4405 * When the ":tab" modifier was used do this for all tab pages.
4407 if (had_tab > 0)
4408 goto_tabpage_tp(first_tabpage);
4409 for (;;)
4411 tpnext = curtab->tp_next;
4412 for (wp = firstwin; wp != NULL; wp = wpnext)
4414 wpnext = wp->w_next;
4415 buf = wp->w_buffer;
4416 if (buf->b_ffname == NULL
4417 || buf->b_nwindows > 1
4418 #ifdef FEAT_VERTSPLIT
4419 || wp->w_width != Columns
4420 #endif
4422 i = ARGCOUNT;
4423 else
4425 /* check if the buffer in this window is in the arglist */
4426 for (i = 0; i < ARGCOUNT; ++i)
4428 if (ARGLIST[i].ae_fnum == buf->b_fnum
4429 || fullpathcmp(alist_name(&ARGLIST[i]),
4430 buf->b_ffname, TRUE) & FPC_SAME)
4432 if (i < opened_len)
4434 opened[i] = TRUE;
4435 if (i == 0)
4437 new_curwin = wp;
4438 new_curtab = curtab;
4441 if (wp->w_alist != curwin->w_alist)
4443 /* Use the current argument list for all windows
4444 * containing a file from it. */
4445 alist_unlink(wp->w_alist);
4446 wp->w_alist = curwin->w_alist;
4447 ++wp->w_alist->al_refcount;
4449 break;
4453 wp->w_arg_idx = i;
4455 if (i == ARGCOUNT && !keep_tabs) /* close this window */
4457 if (P_HID(buf) || forceit || buf->b_nwindows > 1
4458 || !bufIsChanged(buf))
4460 /* If the buffer was changed, and we would like to hide it,
4461 * try autowriting. */
4462 if (!P_HID(buf) && buf->b_nwindows <= 1
4463 && bufIsChanged(buf))
4465 (void)autowrite(buf, FALSE);
4466 #ifdef FEAT_AUTOCMD
4467 /* check if autocommands removed the window */
4468 if (!win_valid(wp) || !buf_valid(buf))
4470 wpnext = firstwin; /* start all over... */
4471 continue;
4473 #endif
4475 #ifdef FEAT_WINDOWS
4476 /* don't close last window */
4477 if (firstwin == lastwin && first_tabpage->tp_next == NULL)
4478 #endif
4479 use_firstwin = TRUE;
4480 #ifdef FEAT_WINDOWS
4481 else
4483 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4484 # ifdef FEAT_AUTOCMD
4485 /* check if autocommands removed the next window */
4486 if (!win_valid(wpnext))
4487 wpnext = firstwin; /* start all over... */
4488 # endif
4490 #endif
4495 /* Without the ":tab" modifier only do the current tab page. */
4496 if (had_tab == 0 || tpnext == NULL)
4497 break;
4499 # ifdef FEAT_AUTOCMD
4500 /* check if autocommands removed the next tab page */
4501 if (!valid_tabpage(tpnext))
4502 tpnext = first_tabpage; /* start all over...*/
4503 # endif
4504 goto_tabpage_tp(tpnext);
4508 * Open a window for files in the argument list that don't have one.
4509 * ARGCOUNT may change while doing this, because of autocommands.
4511 if (count > ARGCOUNT || count <= 0)
4512 count = ARGCOUNT;
4514 /* Autocommands may do anything to the argument list. Make sure it's not
4515 * freed while we are working here by "locking" it. We still have to
4516 * watch out for its size to be changed. */
4517 alist = curwin->w_alist;
4518 ++alist->al_refcount;
4520 #ifdef FEAT_AUTOCMD
4521 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4522 ++autocmd_no_enter;
4523 ++autocmd_no_leave;
4524 #endif
4525 win_enter(lastwin, FALSE);
4526 #ifdef FEAT_WINDOWS
4527 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4528 * leaving an empty tab page when executed locally. */
4529 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4530 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4531 use_firstwin = TRUE;
4532 #endif
4534 for (i = 0; i < count && i < alist->al_ga.ga_len && !got_int; ++i)
4536 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4537 arg_had_last = TRUE;
4538 if (i < opened_len && opened[i])
4540 /* Move the already present window to below the current window */
4541 if (curwin->w_arg_idx != i)
4543 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4545 if (wpnext->w_arg_idx == i)
4547 win_move_after(wpnext, curwin);
4548 break;
4553 else if (split_ret == OK)
4555 if (!use_firstwin) /* split current window */
4557 p_ea_save = p_ea;
4558 p_ea = TRUE; /* use space from all windows */
4559 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4560 p_ea = p_ea_save;
4561 if (split_ret == FAIL)
4562 continue;
4564 #ifdef FEAT_AUTOCMD
4565 else /* first window: do autocmd for leaving this buffer */
4566 --autocmd_no_leave;
4567 #endif
4570 * edit file "i"
4572 curwin->w_arg_idx = i;
4573 if (i == 0)
4575 new_curwin = curwin;
4576 new_curtab = curtab;
4578 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4579 ECMD_ONE,
4580 ((P_HID(curwin->w_buffer)
4581 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
4582 + ECMD_OLDBUF, curwin);
4583 #ifdef FEAT_AUTOCMD
4584 if (use_firstwin)
4585 ++autocmd_no_leave;
4586 #endif
4587 use_firstwin = FALSE;
4589 ui_breakcheck();
4591 /* When ":tab" was used open a new tab for a new window repeatedly. */
4592 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4593 cmdmod.tab = 9999;
4596 /* Remove the "lock" on the argument list. */
4597 alist_unlink(alist);
4599 #ifdef FEAT_AUTOCMD
4600 --autocmd_no_enter;
4601 #endif
4602 /* to window with first arg */
4603 if (valid_tabpage(new_curtab))
4604 goto_tabpage_tp(new_curtab);
4605 if (win_valid(new_curwin))
4606 win_enter(new_curwin, FALSE);
4608 #ifdef FEAT_AUTOCMD
4609 --autocmd_no_leave;
4610 #endif
4611 vim_free(opened);
4614 # if defined(FEAT_LISTCMDS) || defined(PROTO)
4616 * Open a window for a number of buffers.
4618 void
4619 ex_buffer_all(eap)
4620 exarg_T *eap;
4622 buf_T *buf;
4623 win_T *wp, *wpnext;
4624 int split_ret = OK;
4625 int p_ea_save;
4626 int open_wins = 0;
4627 int r;
4628 int count; /* Maximum number of windows to open. */
4629 int all; /* When TRUE also load inactive buffers. */
4630 #ifdef FEAT_WINDOWS
4631 int had_tab = cmdmod.tab;
4632 tabpage_T *tpnext;
4633 #endif
4635 if (eap->addr_count == 0) /* make as many windows as possible */
4636 count = 9999;
4637 else
4638 count = eap->line2; /* make as many windows as specified */
4639 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4640 all = FALSE;
4641 else
4642 all = TRUE;
4644 setpcmark();
4646 #ifdef FEAT_GUI
4647 need_mouse_correct = TRUE;
4648 #endif
4651 * Close superfluous windows (two windows for the same buffer).
4652 * Also close windows that are not full-width.
4654 #ifdef FEAT_WINDOWS
4655 if (had_tab > 0)
4656 goto_tabpage_tp(first_tabpage);
4657 for (;;)
4659 #endif
4660 tpnext = curtab->tp_next;
4661 for (wp = firstwin; wp != NULL; wp = wpnext)
4663 wpnext = wp->w_next;
4664 if ((wp->w_buffer->b_nwindows > 1
4665 #ifdef FEAT_VERTSPLIT
4666 || ((cmdmod.split & WSP_VERT)
4667 ? wp->w_height + wp->w_status_height < Rows - p_ch
4668 - tabline_height()
4669 : wp->w_width != Columns)
4670 #endif
4671 #ifdef FEAT_WINDOWS
4672 || (had_tab > 0 && wp != firstwin)
4673 #endif
4674 ) && firstwin != lastwin)
4676 win_close(wp, FALSE);
4677 #ifdef FEAT_AUTOCMD
4678 wpnext = firstwin; /* just in case an autocommand does
4679 something strange with windows */
4680 tpnext = first_tabpage; /* start all over...*/
4681 open_wins = 0;
4682 #endif
4684 else
4685 ++open_wins;
4688 #ifdef FEAT_WINDOWS
4689 /* Without the ":tab" modifier only do the current tab page. */
4690 if (had_tab == 0 || tpnext == NULL)
4691 break;
4692 goto_tabpage_tp(tpnext);
4694 #endif
4697 * Go through the buffer list. When a buffer doesn't have a window yet,
4698 * open one. Otherwise move the window to the right position.
4699 * Watch out for autocommands that delete buffers or windows!
4701 #ifdef FEAT_AUTOCMD
4702 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4703 ++autocmd_no_enter;
4704 #endif
4705 win_enter(lastwin, FALSE);
4706 #ifdef FEAT_AUTOCMD
4707 ++autocmd_no_leave;
4708 #endif
4709 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4711 /* Check if this buffer needs a window */
4712 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4713 continue;
4715 #ifdef FEAT_WINDOWS
4716 if (had_tab != 0)
4718 /* With the ":tab" modifier don't move the window. */
4719 if (buf->b_nwindows > 0)
4720 wp = lastwin; /* buffer has a window, skip it */
4721 else
4722 wp = NULL;
4724 else
4725 #endif
4727 /* Check if this buffer already has a window */
4728 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4729 if (wp->w_buffer == buf)
4730 break;
4731 /* If the buffer already has a window, move it */
4732 if (wp != NULL)
4733 win_move_after(wp, curwin);
4736 if (wp == NULL && split_ret == OK)
4738 /* Split the window and put the buffer in it */
4739 p_ea_save = p_ea;
4740 p_ea = TRUE; /* use space from all windows */
4741 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4742 ++open_wins;
4743 p_ea = p_ea_save;
4744 if (split_ret == FAIL)
4745 continue;
4747 /* Open the buffer in this window. */
4748 #if defined(HAS_SWAP_EXISTS_ACTION)
4749 swap_exists_action = SEA_DIALOG;
4750 #endif
4751 set_curbuf(buf, DOBUF_GOTO);
4752 #ifdef FEAT_AUTOCMD
4753 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
4755 #if defined(HAS_SWAP_EXISTS_ACTION)
4756 swap_exists_action = SEA_NONE;
4757 # endif
4758 break;
4760 #endif
4761 #if defined(HAS_SWAP_EXISTS_ACTION)
4762 if (swap_exists_action == SEA_QUIT)
4764 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4765 cleanup_T cs;
4767 /* Reset the error/interrupt/exception state here so that
4768 * aborting() returns FALSE when closing a window. */
4769 enter_cleanup(&cs);
4770 # endif
4772 /* User selected Quit at ATTENTION prompt; close this window. */
4773 win_close(curwin, TRUE);
4774 --open_wins;
4775 swap_exists_action = SEA_NONE;
4776 swap_exists_did_quit = TRUE;
4778 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4779 /* Restore the error/interrupt/exception state if not
4780 * discarded by a new aborting error, interrupt, or uncaught
4781 * exception. */
4782 leave_cleanup(&cs);
4783 # endif
4785 else
4786 handle_swap_exists(NULL);
4787 #endif
4790 ui_breakcheck();
4791 if (got_int)
4793 (void)vgetc(); /* only break the file loading, not the rest */
4794 break;
4796 #ifdef FEAT_EVAL
4797 /* Autocommands deleted the buffer or aborted script processing!!! */
4798 if (aborting())
4799 break;
4800 #endif
4801 #ifdef FEAT_WINDOWS
4802 /* When ":tab" was used open a new tab for a new window repeatedly. */
4803 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4804 cmdmod.tab = 9999;
4805 #endif
4807 #ifdef FEAT_AUTOCMD
4808 --autocmd_no_enter;
4809 #endif
4810 win_enter(firstwin, FALSE); /* back to first window */
4811 #ifdef FEAT_AUTOCMD
4812 --autocmd_no_leave;
4813 #endif
4816 * Close superfluous windows.
4818 for (wp = lastwin; open_wins > count; )
4820 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
4821 || autowrite(wp->w_buffer, FALSE) == OK);
4822 #ifdef FEAT_AUTOCMD
4823 if (!win_valid(wp))
4825 /* BufWrite Autocommands made the window invalid, start over */
4826 wp = lastwin;
4828 else
4829 #endif
4830 if (r)
4832 win_close(wp, !P_HID(wp->w_buffer));
4833 --open_wins;
4834 wp = lastwin;
4836 else
4838 wp = wp->w_prev;
4839 if (wp == NULL)
4840 break;
4844 # endif /* FEAT_LISTCMDS */
4846 #endif /* FEAT_WINDOWS */
4848 static int chk_modeline __ARGS((linenr_T, int));
4851 * do_modelines() - process mode lines for the current file
4853 * "flags" can be:
4854 * OPT_WINONLY only set options local to window
4855 * OPT_NOWIN don't set options local to window
4857 * Returns immediately if the "ml" option isn't set.
4859 void
4860 do_modelines(flags)
4861 int flags;
4863 linenr_T lnum;
4864 int nmlines;
4865 static int entered = 0;
4867 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
4868 return;
4870 /* Disallow recursive entry here. Can happen when executing a modeline
4871 * triggers an autocommand, which reloads modelines with a ":do". */
4872 if (entered)
4873 return;
4875 ++entered;
4876 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
4877 ++lnum)
4878 if (chk_modeline(lnum, flags) == FAIL)
4879 nmlines = 0;
4881 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
4882 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
4883 if (chk_modeline(lnum, flags) == FAIL)
4884 nmlines = 0;
4885 --entered;
4888 #include "version.h" /* for version number */
4891 * chk_modeline() - check a single line for a mode string
4892 * Return FAIL if an error encountered.
4894 static int
4895 chk_modeline(lnum, flags)
4896 linenr_T lnum;
4897 int flags; /* Same as for do_modelines(). */
4899 char_u *s;
4900 char_u *e;
4901 char_u *linecopy; /* local copy of any modeline found */
4902 int prev;
4903 int vers;
4904 int end;
4905 int retval = OK;
4906 char_u *save_sourcing_name;
4907 linenr_T save_sourcing_lnum;
4908 #ifdef FEAT_EVAL
4909 scid_T save_SID;
4910 #endif
4912 prev = -1;
4913 for (s = ml_get(lnum); *s != NUL; ++s)
4915 if (prev == -1 || vim_isspace(prev))
4917 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
4918 || STRNCMP(s, "vi:", (size_t)3) == 0)
4919 break;
4920 if (STRNCMP(s, "vim", 3) == 0)
4922 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
4923 e = s + 4;
4924 else
4925 e = s + 3;
4926 vers = getdigits(&e);
4927 if (*e == ':'
4928 && (s[3] == ':'
4929 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
4930 || (VIM_VERSION_100 < vers && s[3] == '<')
4931 || (VIM_VERSION_100 > vers && s[3] == '>')
4932 || (VIM_VERSION_100 == vers && s[3] == '=')))
4933 break;
4936 prev = *s;
4939 if (*s)
4941 do /* skip over "ex:", "vi:" or "vim:" */
4942 ++s;
4943 while (s[-1] != ':');
4945 s = linecopy = vim_strsave(s); /* copy the line, it will change */
4946 if (linecopy == NULL)
4947 return FAIL;
4949 save_sourcing_lnum = sourcing_lnum;
4950 save_sourcing_name = sourcing_name;
4951 sourcing_lnum = lnum; /* prepare for emsg() */
4952 sourcing_name = (char_u *)"modelines";
4954 end = FALSE;
4955 while (end == FALSE)
4957 s = skipwhite(s);
4958 if (*s == NUL)
4959 break;
4962 * Find end of set command: ':' or end of line.
4963 * Skip over "\:", replacing it with ":".
4965 for (e = s; *e != ':' && *e != NUL; ++e)
4966 if (e[0] == '\\' && e[1] == ':')
4967 STRMOVE(e, e + 1);
4968 if (*e == NUL)
4969 end = TRUE;
4972 * If there is a "set" command, require a terminating ':' and
4973 * ignore the stuff after the ':'.
4974 * "vi:set opt opt opt: foo" -- foo not interpreted
4975 * "vi:opt opt opt: foo" -- foo interpreted
4976 * Accept "se" for compatibility with Elvis.
4978 if (STRNCMP(s, "set ", (size_t)4) == 0
4979 || STRNCMP(s, "se ", (size_t)3) == 0)
4981 if (*e != ':') /* no terminating ':'? */
4982 break;
4983 end = TRUE;
4984 s = vim_strchr(s, ' ') + 1;
4986 *e = NUL; /* truncate the set command */
4988 if (*s != NUL) /* skip over an empty "::" */
4990 #ifdef FEAT_EVAL
4991 save_SID = current_SID;
4992 current_SID = SID_MODELINE;
4993 #endif
4994 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
4995 #ifdef FEAT_EVAL
4996 current_SID = save_SID;
4997 #endif
4998 if (retval == FAIL) /* stop if error found */
4999 break;
5001 s = e + 1; /* advance to next part */
5004 sourcing_lnum = save_sourcing_lnum;
5005 sourcing_name = save_sourcing_name;
5007 vim_free(linecopy);
5009 return retval;
5012 #if defined(FEAT_VIMINFO) || defined(PROTO)
5014 read_viminfo_bufferlist(virp, writing)
5015 vir_T *virp;
5016 int writing;
5018 char_u *tab;
5019 linenr_T lnum;
5020 colnr_T col;
5021 buf_T *buf;
5022 char_u *sfname;
5023 char_u *xline;
5025 /* Handle long line and escaped characters. */
5026 xline = viminfo_readstring(virp, 1, FALSE);
5028 /* don't read in if there are files on the command-line or if writing: */
5029 if (xline != NULL && !writing && ARGCOUNT == 0
5030 && find_viminfo_parameter('%') != NULL)
5032 /* Format is: <fname> Tab <lnum> Tab <col>.
5033 * Watch out for a Tab in the file name, work from the end. */
5034 lnum = 0;
5035 col = 0;
5036 tab = vim_strrchr(xline, '\t');
5037 if (tab != NULL)
5039 *tab++ = '\0';
5040 col = (colnr_T)atoi((char *)tab);
5041 tab = vim_strrchr(xline, '\t');
5042 if (tab != NULL)
5044 *tab++ = '\0';
5045 lnum = atol((char *)tab);
5049 /* Expand "~/" in the file name at "line + 1" to a full path.
5050 * Then try shortening it by comparing with the current directory */
5051 expand_env(xline, NameBuff, MAXPATHL);
5052 sfname = shorten_fname1(NameBuff);
5054 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5055 if (buf != NULL) /* just in case... */
5057 buf->b_last_cursor.lnum = lnum;
5058 buf->b_last_cursor.col = col;
5059 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5062 vim_free(xline);
5064 return viminfo_readline(virp);
5067 void
5068 write_viminfo_bufferlist(fp)
5069 FILE *fp;
5071 buf_T *buf;
5072 #ifdef FEAT_WINDOWS
5073 win_T *win;
5074 tabpage_T *tp;
5075 #endif
5076 char_u *line;
5077 int max_buffers;
5078 size_t len;
5080 if (find_viminfo_parameter('%') == NULL)
5081 return;
5083 /* Without a number -1 is returned: do all buffers. */
5084 max_buffers = get_viminfo_parameter('%');
5086 /* Allocate room for the file name, lnum and col. */
5087 #define LINE_BUF_LEN (MAXPATHL + 40)
5088 line = alloc(LINE_BUF_LEN);
5089 if (line == NULL)
5090 return;
5092 #ifdef FEAT_WINDOWS
5093 FOR_ALL_TAB_WINDOWS(tp, win)
5094 set_last_cursor(win);
5095 #else
5096 set_last_cursor(curwin);
5097 #endif
5099 fprintf(fp, _("\n# Buffer list:\n"));
5100 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5102 if (buf->b_fname == NULL
5103 || !buf->b_p_bl
5104 #ifdef FEAT_QUICKFIX
5105 || bt_quickfix(buf)
5106 #endif
5107 || removable(buf->b_ffname))
5108 continue;
5110 if (max_buffers-- == 0)
5111 break;
5112 putc('%', fp);
5113 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
5114 len = STRLEN(line);
5115 vim_snprintf((char *)line + len, len - LINE_BUF_LEN, "\t%ld\t%d",
5116 (long)buf->b_last_cursor.lnum,
5117 buf->b_last_cursor.col);
5118 viminfo_writestring(fp, line);
5120 vim_free(line);
5122 #endif
5126 * Return special buffer name.
5127 * Returns NULL when the buffer has a normal file name.
5129 char *
5130 buf_spname(buf)
5131 buf_T *buf;
5133 #if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5134 if (bt_quickfix(buf))
5136 win_T *win = NULL;
5137 tabpage_T *tp;
5140 * For location list window, w_llist_ref points to the location list.
5141 * For quickfix window, w_llist_ref is NULL.
5143 FOR_ALL_TAB_WINDOWS(tp, win)
5144 if (win->w_buffer == buf)
5145 goto win_found;
5146 win_found:
5147 if (win != NULL && win->w_llist_ref != NULL)
5148 return _("[Location List]");
5149 else
5150 return _("[Quickfix List]");
5152 #endif
5153 #ifdef FEAT_QUICKFIX
5154 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5155 * contains the name as specified by the user */
5156 if (bt_nofile(buf))
5158 if (buf->b_sfname != NULL)
5159 return (char *)buf->b_sfname;
5160 return _("[Scratch]");
5162 #endif
5163 if (buf->b_fname == NULL)
5164 return _("[No Name]");
5165 return NULL;
5169 #if defined(FEAT_SIGNS) || defined(PROTO)
5171 * Insert the sign into the signlist.
5173 static void
5174 insert_sign(buf, prev, next, id, lnum, typenr)
5175 buf_T *buf; /* buffer to store sign in */
5176 signlist_T *prev; /* previous sign entry */
5177 signlist_T *next; /* next sign entry */
5178 int id; /* sign ID */
5179 linenr_T lnum; /* line number which gets the mark */
5180 int typenr; /* typenr of sign we are adding */
5182 signlist_T *newsign;
5184 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5185 if (newsign != NULL)
5187 newsign->id = id;
5188 newsign->lnum = lnum;
5189 newsign->typenr = typenr;
5190 newsign->next = next;
5191 #ifdef FEAT_NETBEANS_INTG
5192 newsign->prev = prev;
5193 if (next != NULL)
5194 next->prev = newsign;
5195 #endif
5197 if (prev == NULL)
5199 /* When adding first sign need to redraw the windows to create the
5200 * column for signs. */
5201 if (buf->b_signlist == NULL)
5203 redraw_buf_later(buf, NOT_VALID);
5204 changed_cline_bef_curs();
5207 /* first sign in signlist */
5208 buf->b_signlist = newsign;
5210 else
5211 prev->next = newsign;
5216 * Add the sign into the signlist. Find the right spot to do it though.
5218 void
5219 buf_addsign(buf, id, lnum, typenr)
5220 buf_T *buf; /* buffer to store sign in */
5221 int id; /* sign ID */
5222 linenr_T lnum; /* line number which gets the mark */
5223 int typenr; /* typenr of sign we are adding */
5225 signlist_T *sign; /* a sign in the signlist */
5226 signlist_T *prev; /* the previous sign */
5228 prev = NULL;
5229 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5231 if (lnum == sign->lnum && id == sign->id)
5233 sign->typenr = typenr;
5234 return;
5236 else if (
5237 #ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5238 id < 0 &&
5239 #endif
5240 lnum < sign->lnum)
5242 #ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5243 /* XXX - GRP: Is this because of sign slide problem? Or is it
5244 * really needed? Or is it because we allow multiple signs per
5245 * line? If so, should I add that feature to FEAT_SIGNS?
5247 while (prev != NULL && prev->lnum == lnum)
5248 prev = prev->prev;
5249 if (prev == NULL)
5250 sign = buf->b_signlist;
5251 else
5252 sign = prev->next;
5253 #endif
5254 insert_sign(buf, prev, sign, id, lnum, typenr);
5255 return;
5257 prev = sign;
5259 #ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5260 /* XXX - GRP: See previous comment */
5261 while (prev != NULL && prev->lnum == lnum)
5262 prev = prev->prev;
5263 if (prev == NULL)
5264 sign = buf->b_signlist;
5265 else
5266 sign = prev->next;
5267 #endif
5268 insert_sign(buf, prev, sign, id, lnum, typenr);
5270 return;
5273 linenr_T
5274 buf_change_sign_type(buf, markId, typenr)
5275 buf_T *buf; /* buffer to store sign in */
5276 int markId; /* sign ID */
5277 int typenr; /* typenr of sign we are adding */
5279 signlist_T *sign; /* a sign in the signlist */
5281 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5283 if (sign->id == markId)
5285 sign->typenr = typenr;
5286 return sign->lnum;
5290 return (linenr_T)0;
5294 buf_getsigntype(buf, lnum, type)
5295 buf_T *buf;
5296 linenr_T lnum;
5297 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5299 signlist_T *sign; /* a sign in a b_signlist */
5301 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5302 if (sign->lnum == lnum
5303 && (type == SIGN_ANY
5304 # ifdef FEAT_SIGN_ICONS
5305 || (type == SIGN_ICON
5306 && sign_get_image(sign->typenr) != NULL)
5307 # endif
5308 || (type == SIGN_TEXT
5309 && sign_get_text(sign->typenr) != NULL)
5310 || (type == SIGN_LINEHL
5311 && sign_get_attr(sign->typenr, TRUE) != 0)))
5312 return sign->typenr;
5313 return 0;
5317 linenr_T
5318 buf_delsign(buf, id)
5319 buf_T *buf; /* buffer sign is stored in */
5320 int id; /* sign id */
5322 signlist_T **lastp; /* pointer to pointer to current sign */
5323 signlist_T *sign; /* a sign in a b_signlist */
5324 signlist_T *next; /* the next sign in a b_signlist */
5325 linenr_T lnum; /* line number whose sign was deleted */
5327 lastp = &buf->b_signlist;
5328 lnum = 0;
5329 for (sign = buf->b_signlist; sign != NULL; sign = next)
5331 next = sign->next;
5332 if (sign->id == id)
5334 *lastp = next;
5335 #ifdef FEAT_NETBEANS_INTG
5336 if (next != NULL)
5337 next->prev = sign->prev;
5338 #endif
5339 lnum = sign->lnum;
5340 vim_free(sign);
5341 break;
5343 else
5344 lastp = &sign->next;
5347 /* When deleted the last sign need to redraw the windows to remove the
5348 * sign column. */
5349 if (buf->b_signlist == NULL)
5351 redraw_buf_later(buf, NOT_VALID);
5352 changed_cline_bef_curs();
5355 return lnum;
5360 * Find the line number of the sign with the requested id. If the sign does
5361 * not exist, return 0 as the line number. This will still let the correct file
5362 * get loaded.
5365 buf_findsign(buf, id)
5366 buf_T *buf; /* buffer to store sign in */
5367 int id; /* sign ID */
5369 signlist_T *sign; /* a sign in the signlist */
5371 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5372 if (sign->id == id)
5373 return sign->lnum;
5375 return 0;
5379 buf_findsign_id(buf, lnum)
5380 buf_T *buf; /* buffer whose sign we are searching for */
5381 linenr_T lnum; /* line number of sign */
5383 signlist_T *sign; /* a sign in the signlist */
5385 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5386 if (sign->lnum == lnum)
5387 return sign->id;
5389 return 0;
5393 # if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5394 /* see if a given type of sign exists on a specific line */
5396 buf_findsigntype_id(buf, lnum, typenr)
5397 buf_T *buf; /* buffer whose sign we are searching for */
5398 linenr_T lnum; /* line number of sign */
5399 int typenr; /* sign type number */
5401 signlist_T *sign; /* a sign in the signlist */
5403 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5404 if (sign->lnum == lnum && sign->typenr == typenr)
5405 return sign->id;
5407 return 0;
5411 # if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5412 /* return the number of icons on the given line */
5414 buf_signcount(buf, lnum)
5415 buf_T *buf;
5416 linenr_T lnum;
5418 signlist_T *sign; /* a sign in the signlist */
5419 int count = 0;
5421 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5422 if (sign->lnum == lnum)
5423 if (sign_get_image(sign->typenr) != NULL)
5424 count++;
5426 return count;
5428 # endif /* FEAT_SIGN_ICONS */
5429 # endif /* FEAT_NETBEANS_INTG */
5433 * Delete signs in buffer "buf".
5435 static void
5436 buf_delete_signs(buf)
5437 buf_T *buf;
5439 signlist_T *next;
5441 while (buf->b_signlist != NULL)
5443 next = buf->b_signlist->next;
5444 vim_free(buf->b_signlist);
5445 buf->b_signlist = next;
5450 * Delete all signs in all buffers.
5452 void
5453 buf_delete_all_signs()
5455 buf_T *buf; /* buffer we are checking for signs */
5457 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5458 if (buf->b_signlist != NULL)
5460 /* Need to redraw the windows to remove the sign column. */
5461 redraw_buf_later(buf, NOT_VALID);
5462 buf_delete_signs(buf);
5467 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5469 void
5470 sign_list_placed(rbuf)
5471 buf_T *rbuf;
5473 buf_T *buf;
5474 signlist_T *p;
5475 char lbuf[BUFSIZ];
5477 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5478 msg_putchar('\n');
5479 if (rbuf == NULL)
5480 buf = firstbuf;
5481 else
5482 buf = rbuf;
5483 while (buf != NULL)
5485 if (buf->b_signlist != NULL)
5487 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
5488 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5489 msg_putchar('\n');
5491 for (p = buf->b_signlist; p != NULL; p = p->next)
5493 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
5494 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5495 MSG_PUTS(lbuf);
5496 msg_putchar('\n');
5498 if (rbuf != NULL)
5499 break;
5500 buf = buf->b_next;
5505 * Adjust a placed sign for inserted/deleted lines.
5507 void
5508 sign_mark_adjust(line1, line2, amount, amount_after)
5509 linenr_T line1;
5510 linenr_T line2;
5511 long amount;
5512 long amount_after;
5514 signlist_T *sign; /* a sign in a b_signlist */
5516 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5518 if (sign->lnum >= line1 && sign->lnum <= line2)
5520 if (amount == MAXLNUM)
5521 sign->lnum = line1;
5522 else
5523 sign->lnum += amount;
5525 else if (sign->lnum > line2)
5526 sign->lnum += amount_after;
5529 #endif /* FEAT_SIGNS */
5532 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5534 void
5535 set_buflisted(on)
5536 int on;
5538 if (on != curbuf->b_p_bl)
5540 curbuf->b_p_bl = on;
5541 #ifdef FEAT_AUTOCMD
5542 if (on)
5543 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5544 else
5545 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5546 #endif
5551 * Read the file for "buf" again and check if the contents changed.
5552 * Return TRUE if it changed or this could not be checked.
5555 buf_contents_changed(buf)
5556 buf_T *buf;
5558 buf_T *newbuf;
5559 int differ = TRUE;
5560 linenr_T lnum;
5561 aco_save_T aco;
5562 exarg_T ea;
5564 /* Allocate a buffer without putting it in the buffer list. */
5565 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5566 if (newbuf == NULL)
5567 return TRUE;
5569 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5570 if (prep_exarg(&ea, buf) == FAIL)
5572 wipe_buffer(newbuf, FALSE);
5573 return TRUE;
5576 /* set curwin/curbuf to buf and save a few things */
5577 aucmd_prepbuf(&aco, newbuf);
5579 if (ml_open(curbuf) == OK
5580 && readfile(buf->b_ffname, buf->b_fname,
5581 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5582 &ea, READ_NEW | READ_DUMMY) == OK)
5584 /* compare the two files line by line */
5585 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5587 differ = FALSE;
5588 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5589 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5591 differ = TRUE;
5592 break;
5596 vim_free(ea.cmd);
5598 /* restore curwin/curbuf and a few other things */
5599 aucmd_restbuf(&aco);
5601 if (curbuf != newbuf) /* safety check */
5602 wipe_buffer(newbuf, FALSE);
5604 return differ;
5608 * Wipe out a buffer and decrement the last buffer number if it was used for
5609 * this buffer. Call this to wipe out a temp buffer that does not contain any
5610 * marks.
5612 void
5613 wipe_buffer(buf, aucmd)
5614 buf_T *buf;
5615 int aucmd UNUSED; /* When TRUE trigger autocommands. */
5617 if (buf->b_fnum == top_file_num - 1)
5618 --top_file_num;
5620 #ifdef FEAT_AUTOCMD
5621 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
5622 block_autocmds();
5623 #endif
5624 close_buffer(NULL, buf, DOBUF_WIPE);
5625 #ifdef FEAT_AUTOCMD
5626 if (!aucmd)
5627 unblock_autocmds();
5628 #endif