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.
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.
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
));
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
));
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
));
42 static int otherfile_buf
__ARGS((buf_T
*buf
, char_u
*ffname
));
45 static int ti_change
__ARGS((char_u
*str
, char_u
**last
));
47 static void free_buffer
__ARGS((buf_T
*));
48 static void free_buffer_stuff
__ARGS((buf_T
*buf
, int free_options
));
49 static void clear_wininfo
__ARGS((buf_T
*buf
));
54 # define dev_T unsigned
57 #if defined(FEAT_SIGNS)
58 static void insert_sign
__ARGS((buf_T
*buf
, signlist_T
*prev
, signlist_T
*next
, int id
, linenr_T lnum
, int typenr
));
59 static void buf_delete_signs
__ARGS((buf_T
*buf
));
63 * Open current buffer, that is: open the memfile and read the file into memory
64 * return FAIL for failure, OK otherwise
67 open_buffer(read_stdin
, eap
)
68 int read_stdin
; /* read file from stdin */
69 exarg_T
*eap
; /* for forced 'ff' and 'fenc' or NULL */
77 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
78 * When re-entering the same buffer, it should not change, because the
79 * user may have reset the flag by hand.
81 if (readonlymode
&& curbuf
->b_ffname
!= NULL
82 && (curbuf
->b_flags
& BF_NEVERLOADED
))
83 curbuf
->b_p_ro
= TRUE
;
85 if (ml_open(curbuf
) == FAIL
)
88 * There MUST be a memfile, otherwise we can't do anything
89 * If we can't create one for the current buffer, take another buffer
91 close_buffer(NULL
, curbuf
, 0);
92 for (curbuf
= firstbuf
; curbuf
!= NULL
; curbuf
= curbuf
->b_next
)
93 if (curbuf
->b_ml
.ml_mfp
!= NULL
)
96 * if there is no memfile at all, exit
97 * This is OK, since there are no changes to lose.
101 EMSG(_("E82: Cannot allocate any buffer, exiting..."));
104 EMSG(_("E83: Cannot allocate buffer, using other one..."));
105 enter_buffer(curbuf
);
110 /* The autocommands in readfile() may change the buffer, but only AFTER
111 * reading the file. */
113 modified_was_set
= FALSE
;
116 /* mark cursor position as being invalid */
117 changed_line_abv_curs();
119 if (curbuf
->b_ffname
!= NULL
120 #ifdef FEAT_NETBEANS_INTG
125 #ifdef FEAT_NETBEANS_INTG
126 int oldFire
= netbeansFireChanges
;
128 netbeansFireChanges
= 0;
130 retval
= readfile(curbuf
->b_ffname
, curbuf
->b_fname
,
131 (linenr_T
)0, (linenr_T
)0, (linenr_T
)MAXLNUM
, eap
, READ_NEW
);
132 #ifdef FEAT_NETBEANS_INTG
133 netbeansFireChanges
= oldFire
;
135 /* Help buffer is filtered. */
141 int save_bin
= curbuf
->b_p_bin
;
145 * First read the text in binary mode into the buffer.
146 * Then read from that same buffer and append at the end. This makes
147 * it possible to retry when 'fileformat' or 'fileencoding' was
150 curbuf
->b_p_bin
= TRUE
;
151 retval
= readfile(NULL
, NULL
, (linenr_T
)0,
152 (linenr_T
)0, (linenr_T
)MAXLNUM
, NULL
, READ_NEW
+ READ_STDIN
);
153 curbuf
->b_p_bin
= save_bin
;
156 line_count
= curbuf
->b_ml
.ml_line_count
;
157 retval
= readfile(NULL
, NULL
, (linenr_T
)line_count
,
158 (linenr_T
)0, (linenr_T
)MAXLNUM
, eap
, READ_BUFFER
);
161 /* Delete the binary lines. */
162 while (--line_count
>= 0)
163 ml_delete((linenr_T
)1, FALSE
);
167 /* Delete the converted lines. */
168 while (curbuf
->b_ml
.ml_line_count
> line_count
)
169 ml_delete(line_count
, FALSE
);
171 /* Put the cursor on the first line. */
172 curwin
->w_cursor
.lnum
= 1;
173 curwin
->w_cursor
.col
= 0;
175 /* Set or reset 'modified' before executing autocommands, so that
176 * it can be changed there. */
177 if (!readonlymode
&& !bufempty())
179 else if (retval
!= FAIL
)
180 unchanged(curbuf
, FALSE
);
183 apply_autocmds_retval(EVENT_STDINREADPOST
, NULL
, NULL
, FALSE
,
186 apply_autocmds(EVENT_STDINREADPOST
, NULL
, NULL
, FALSE
, curbuf
);
192 /* if first time loading this buffer, init b_chartab[] */
193 if (curbuf
->b_flags
& BF_NEVERLOADED
)
194 (void)buf_init_chartab(curbuf
, FALSE
);
197 * Set/reset the Changed flag first, autocmds may change the buffer.
198 * Apply the automatic commands, before processing the modelines.
199 * So the modelines have priority over auto commands.
201 /* When reading stdin, the buffer contents always needs writing, so set
202 * the changed flag. Unless in readonly mode: "ls | gview -".
203 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
204 if ((got_int
&& vim_strchr(p_cpo
, CPO_INTMOD
) != NULL
)
206 || modified_was_set
/* ":set modified" used in autocmd */
208 || (aborting() && vim_strchr(p_cpo
, CPO_INTMOD
) != NULL
)
213 else if (retval
!= FAIL
&& !read_stdin
)
214 unchanged(curbuf
, FALSE
);
215 save_file_ff(curbuf
); /* keep this fileformat */
217 /* require "!" to overwrite the file, because it wasn't read completely */
223 curbuf
->b_flags
|= BF_READERR
;
226 /* Need to update automatic folding. Do this before the autocommands,
227 * they may use the fold info. */
228 foldUpdateAll(curwin
);
232 /* need to set w_topline, unless some autocommand already did that. */
233 if (!(curwin
->w_valid
& VALID_TOPLINE
))
235 curwin
->w_topline
= 1;
237 curwin
->w_topfill
= 0;
241 apply_autocmds_retval(EVENT_BUFENTER
, NULL
, NULL
, FALSE
, curbuf
, &retval
);
243 apply_autocmds(EVENT_BUFENTER
, NULL
, NULL
, FALSE
, curbuf
);
251 * The autocommands may have changed the current buffer. Apply the
252 * modelines to the correct buffer, if it still exists and is loaded.
254 if (buf_valid(old_curbuf
) && old_curbuf
->b_ml
.ml_mfp
!= NULL
)
258 /* Go to the buffer that was opened. */
259 aucmd_prepbuf(&aco
, old_curbuf
);
262 curbuf
->b_flags
&= ~(BF_CHECK_RO
| BF_NEVERLOADED
);
266 apply_autocmds_retval(EVENT_BUFWINENTER
, NULL
, NULL
, FALSE
, curbuf
,
269 apply_autocmds(EVENT_BUFWINENTER
, NULL
, NULL
, FALSE
, curbuf
);
272 /* restore curwin/curbuf and a few other things */
282 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
290 for (bp
= firstbuf
; bp
!= NULL
; bp
= bp
->b_next
)
297 * Close the link to a buffer.
298 * "action" is used when there is no longer a window for the buffer.
300 * 0 buffer becomes hidden
301 * DOBUF_UNLOAD buffer is unloaded
302 * DOBUF_DELETE buffer is unloaded and removed from buffer list
303 * DOBUF_WIPE buffer is unloaded and really deleted
304 * When doing all but the first one on the current buffer, the caller should
305 * get a new buffer very soon!
307 * The 'bufhidden' option can force freeing and deleting.
310 close_buffer(win
, buf
, action
)
311 win_T
*win
; /* if not NULL, set b_last_cursor */
317 int nwindows
= buf
->b_nwindows
;
319 int unload_buf
= (action
!= 0);
320 int del_buf
= (action
== DOBUF_DEL
|| action
== DOBUF_WIPE
);
321 int wipe_buf
= (action
== DOBUF_WIPE
);
325 * Force unloading or deleting when 'bufhidden' says so.
326 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
327 * "hide" (otherwise we could never free or delete a buffer).
329 if (buf
->b_p_bh
[0] == 'd') /* 'bufhidden' == "delete" */
334 else if (buf
->b_p_bh
[0] == 'w') /* 'bufhidden' == "wipe" */
340 else if (buf
->b_p_bh
[0] == 'u') /* 'bufhidden' == "unload" */
346 /* Set b_last_cursor when closing the last window for the buffer.
347 * Remember the last cursor position and window options of the buffer.
348 * This used to be only for the current window, but then options like
349 * 'foldmethod' may be lost with a ":only" command. */
350 if (buf
->b_nwindows
== 1)
351 set_last_cursor(win
);
352 buflist_setfpos(buf
, win
,
353 win
->w_cursor
.lnum
== 1 ? 0 : win
->w_cursor
.lnum
,
354 win
->w_cursor
.col
, TRUE
);
358 /* When the buffer is no longer in a window, trigger BufWinLeave */
359 if (buf
->b_nwindows
== 1)
361 apply_autocmds(EVENT_BUFWINLEAVE
, buf
->b_fname
, buf
->b_fname
,
363 if (!buf_valid(buf
)) /* autocommands may delete the buffer */
366 /* When the buffer becomes hidden, but is not unloaded, trigger
370 apply_autocmds(EVENT_BUFHIDDEN
, buf
->b_fname
, buf
->b_fname
,
372 if (!buf_valid(buf
)) /* autocmds may delete the buffer */
376 if (aborting()) /* autocmds may abort script processing */
380 nwindows
= buf
->b_nwindows
;
383 /* decrease the link count from windows (unless not in any window) */
384 if (buf
->b_nwindows
> 0)
387 /* Return when a window is displaying the buffer or when it's not
389 if (buf
->b_nwindows
> 0 || !unload_buf
)
391 #if 0 /* why was this here? */
393 u_sync(); /* sync undo before going to another buffer */
398 /* Always remove the buffer when there is no file name. */
399 if (buf
->b_ffname
== NULL
)
403 * Free all things allocated for this buffer.
404 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
407 /* Remember if we are closing the current buffer. Restore the number of
408 * windows, so that autocommands in buf_freeall() don't get confused. */
409 is_curbuf
= (buf
== curbuf
);
410 buf
->b_nwindows
= nwindows
;
413 buf_freeall(buf
, del_buf
, wipe_buf
);
416 /* Autocommands may have deleted the buffer. */
420 if (aborting()) /* autocmds may abort script processing */
424 /* Autocommands may have opened or closed windows for this buffer.
425 * Decrement the count for the close we do here. */
426 if (buf
->b_nwindows
> 0)
430 * It's possible that autocommands change curbuf to the one being deleted.
431 * This might cause the previous curbuf to be deleted unexpectedly. But
432 * in some cases it's OK to delete the curbuf, because a new one is
433 * obtained anyway. Therefore only return if curbuf changed to the
436 if (buf
== curbuf
&& !is_curbuf
)
440 #ifdef FEAT_NETBEANS_INTG
442 netbeans_file_closed(buf
);
444 #ifdef FEAT_ODB_EDITOR
445 odb_buffer_close(buf
);
448 /* Change directories when the 'acd' option is set. */
452 * Remove the buffer from the list.
456 #ifdef FEAT_SUN_WORKSHOP
457 if (usingSunWorkShop
)
458 workshop_file_closed_lineno((char *)buf
->b_ffname
,
459 (int)buf
->b_last_cursor
.lnum
);
461 vim_free(buf
->b_ffname
);
462 vim_free(buf
->b_sfname
);
463 if (buf
->b_prev
== NULL
)
464 firstbuf
= buf
->b_next
;
466 buf
->b_prev
->b_next
= buf
->b_next
;
467 if (buf
->b_next
== NULL
)
468 lastbuf
= buf
->b_prev
;
470 buf
->b_next
->b_prev
= buf
->b_prev
;
477 /* Free all internal variables and reset option values, to make
478 * ":bdel" compatible with Vim 5.7. */
479 free_buffer_stuff(buf
, TRUE
);
481 /* Make it look like a new buffer. */
482 buf
->b_flags
= BF_CHECK_RO
| BF_NEVERLOADED
;
484 /* Init the options when loaded again. */
485 buf
->b_p_initialized
= FALSE
;
494 * Make buffer not contain a file.
500 buf
->b_ml
.ml_line_count
= 1;
501 unchanged(buf
, TRUE
);
503 buf
->b_shortname
= FALSE
;
506 buf
->b_start_eol
= TRUE
;
508 buf
->b_p_bomb
= FALSE
;
509 buf
->b_start_bomb
= FALSE
;
511 buf
->b_ml
.ml_mfp
= NULL
;
512 buf
->b_ml
.ml_flags
= ML_EMPTY
; /* empty buffer */
513 #ifdef FEAT_NETBEANS_INTG
514 netbeans_deleted_all_lines(buf
);
519 * buf_freeall() - free all things allocated for a buffer that are related to
524 buf_freeall(buf
, del_buf
, wipe_buf
)
526 int del_buf
; /* buffer is going to be deleted */
527 int wipe_buf
; /* buffer is going to be wiped out */
530 int is_curbuf
= (buf
== curbuf
);
532 apply_autocmds(EVENT_BUFUNLOAD
, buf
->b_fname
, buf
->b_fname
, FALSE
, buf
);
533 if (!buf_valid(buf
)) /* autocommands may delete the buffer */
535 if (del_buf
&& buf
->b_p_bl
)
537 apply_autocmds(EVENT_BUFDELETE
, buf
->b_fname
, buf
->b_fname
, FALSE
, buf
);
538 if (!buf_valid(buf
)) /* autocommands may delete the buffer */
543 apply_autocmds(EVENT_BUFWIPEOUT
, buf
->b_fname
, buf
->b_fname
,
545 if (!buf_valid(buf
)) /* autocommands may delete the buffer */
549 if (aborting()) /* autocmds may abort script processing */
554 * It's possible that autocommands change curbuf to the one being deleted.
555 * This might cause curbuf to be deleted unexpectedly. But in some cases
556 * it's OK to delete the curbuf, because a new one is obtained anyway.
557 * Therefore only return if curbuf changed to the deleted buffer.
559 if (buf
== curbuf
&& !is_curbuf
)
563 diff_buf_delete(buf
); /* Can't use 'diff' for unloaded buffer. */
567 /* No folds in an empty buffer. */
573 FOR_ALL_TAB_WINDOWS(tp
, win
)
574 if (win
->w_buffer
== buf
)
578 if (curwin
->w_buffer
== buf
)
579 clearFolding(curwin
);
584 tcl_buffer_free(buf
);
586 u_blockfree(buf
); /* free the memory allocated for undo */
587 ml_close(buf
, TRUE
); /* close and delete the memline/memfile */
588 buf
->b_ml
.ml_line_count
= 0; /* no lines in buffer */
589 u_clearall(buf
); /* reset all undo information */
591 syntax_clear(buf
); /* reset syntax info */
593 buf
->b_flags
&= ~BF_READERR
; /* a read error is no longer relevant */
597 * Free a buffer structure and the things it contains related to the buffer
598 * itself (not the file, that must have been done already).
604 free_buffer_stuff(buf
, TRUE
);
606 mzscheme_buffer_free(buf
);
612 python_buffer_free(buf
);
615 ruby_buffer_free(buf
);
618 aubuflocal_remove(buf
);
624 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
627 free_buffer_stuff(buf
, free_options
)
629 int free_options
; /* free options as well */
633 clear_wininfo(buf
); /* including window-local options */
634 free_buf_options(buf
, TRUE
);
637 vars_clear(&buf
->b_vars
.dv_hashtab
); /* free all internal variables */
638 hash_init(&buf
->b_vars
.dv_hashtab
);
641 uc_clear(&buf
->b_ucmds
); /* clear local user commands */
644 buf_delete_signs(buf
); /* delete any signs */
647 map_clear_int(buf
, MAP_ALL_MODES
, TRUE
, FALSE
); /* clear local mappings */
648 map_clear_int(buf
, MAP_ALL_MODES
, TRUE
, TRUE
); /* clear local abbrevs */
651 vim_free(buf
->b_start_fenc
);
652 buf
->b_start_fenc
= NULL
;
657 * Free the b_wininfo list for buffer "buf".
665 while (buf
->b_wininfo
!= NULL
)
667 wip
= buf
->b_wininfo
;
668 buf
->b_wininfo
= wip
->wi_next
;
671 clear_winopt(&wip
->wi_opt
);
673 deleteFoldRecurse(&wip
->wi_folds
);
680 #if defined(FEAT_LISTCMDS) || defined(PROTO)
682 * Go to another buffer. Handles the result of the ATTENTION dialog.
685 goto_buffer(eap
, start
, dir
, count
)
691 # if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
692 buf_T
*old_curbuf
= curbuf
;
694 swap_exists_action
= SEA_DIALOG
;
696 (void)do_buffer(*eap
->cmd
== 's' ? DOBUF_SPLIT
: DOBUF_GOTO
,
697 start
, dir
, count
, eap
->forceit
);
698 # if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
699 if (swap_exists_action
== SEA_QUIT
&& *eap
->cmd
== 's')
701 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
704 /* Reset the error/interrupt/exception state here so that
705 * aborting() returns FALSE when closing a window. */
709 /* Quitting means closing the split window, nothing else. */
710 win_close(curwin
, TRUE
);
711 swap_exists_action
= SEA_NONE
;
712 swap_exists_did_quit
= TRUE
;
714 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
715 /* Restore the error/interrupt/exception state if not discarded by a
716 * new aborting error, interrupt, or uncaught exception. */
721 handle_swap_exists(old_curbuf
);
726 #if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
728 * Handle the situation of swap_exists_action being set.
729 * It is allowed for "old_curbuf" to be NULL or invalid.
732 handle_swap_exists(old_curbuf
)
735 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
739 if (swap_exists_action
== SEA_QUIT
)
741 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
742 /* Reset the error/interrupt/exception state here so that
743 * aborting() returns FALSE when closing a buffer. */
747 /* User selected Quit at ATTENTION prompt. Go back to previous
748 * buffer. If that buffer is gone or the same as the current one,
749 * open a new, empty buffer. */
750 swap_exists_action
= SEA_NONE
; /* don't want it again */
751 swap_exists_did_quit
= TRUE
;
752 close_buffer(curwin
, curbuf
, DOBUF_UNLOAD
);
753 if (!buf_valid(old_curbuf
) || old_curbuf
== curbuf
)
754 old_curbuf
= buflist_new(NULL
, NULL
, 1L, BLN_CURBUF
| BLN_LISTED
);
755 if (old_curbuf
!= NULL
)
756 enter_buffer(old_curbuf
);
757 /* If "old_curbuf" is NULL we are in big trouble here... */
759 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
760 /* Restore the error/interrupt/exception state if not discarded by a
761 * new aborting error, interrupt, or uncaught exception. */
765 else if (swap_exists_action
== SEA_RECOVER
)
767 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
768 /* Reset the error/interrupt/exception state here so that
769 * aborting() returns FALSE when closing a buffer. */
773 /* User selected Recover at ATTENTION prompt. */
776 MSG_PUTS("\n"); /* don't overwrite the last message */
777 cmdline_row
= msg_row
;
780 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
781 /* Restore the error/interrupt/exception state if not discarded by a
782 * new aborting error, interrupt, or uncaught exception. */
786 swap_exists_action
= SEA_NONE
;
790 #if defined(FEAT_LISTCMDS) || defined(PROTO)
792 * do_bufdel() - delete or unload buffer(s)
794 * addr_count == 0: ":bdel" - delete current buffer
795 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
796 * buffer "end_bnr", then any other arguments.
797 * addr_count == 2: ":N,N bdel" - delete buffers in range
799 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
800 * DOBUF_DEL (":bdel")
802 * Returns error message or NULL
805 do_bufdel(command
, arg
, addr_count
, start_bnr
, end_bnr
, forceit
)
807 char_u
*arg
; /* pointer to extra arguments */
809 int start_bnr
; /* first buffer number in a range */
810 int end_bnr
; /* buffer nr or last buffer nr in a range */
813 int do_current
= 0; /* delete current buffer? */
814 int deleted
= 0; /* number of buffers deleted */
815 char_u
*errormsg
= NULL
; /* return value */
816 int bnr
; /* buffer number */
819 #ifdef FEAT_NETBEANS_INTG
820 netbeansCloseFile
= 1;
824 (void)do_buffer(command
, DOBUF_CURRENT
, FORWARD
, 0, forceit
);
830 if (*arg
) /* both range and argument is not allowed */
831 return (char_u
*)_(e_trailing
);
834 else /* addr_count == 1 */
837 for ( ;!got_int
; ui_breakcheck())
840 * delete the current buffer last, otherwise when the
841 * current buffer is deleted, the next buffer becomes
842 * the current one and will be loaded, which may then
843 * also be deleted, etc.
845 if (bnr
== curbuf
->b_fnum
)
847 else if (do_buffer(command
, DOBUF_FIRST
, FORWARD
, (int)bnr
,
852 * find next buffer number to delete/unload
859 else /* addr_count == 1 */
861 arg
= skipwhite(arg
);
864 if (!VIM_ISDIGIT(*arg
))
866 p
= skiptowhite_esc(arg
);
867 bnr
= buflist_findpat(arg
, p
, command
== DOBUF_WIPE
, FALSE
);
868 if (bnr
< 0) /* failed */
873 bnr
= getdigits(&arg
);
876 if (!got_int
&& do_current
&& do_buffer(command
, DOBUF_FIRST
,
877 FORWARD
, do_current
, forceit
) == OK
)
882 if (command
== DOBUF_UNLOAD
)
883 STRCPY(IObuff
, _("E515: No buffers were unloaded"));
884 else if (command
== DOBUF_DEL
)
885 STRCPY(IObuff
, _("E516: No buffers were deleted"));
887 STRCPY(IObuff
, _("E517: No buffers were wiped out"));
890 else if (deleted
>= p_report
)
892 if (command
== DOBUF_UNLOAD
)
895 MSG(_("1 buffer unloaded"));
897 smsg((char_u
*)_("%d buffers unloaded"), deleted
);
899 else if (command
== DOBUF_DEL
)
902 MSG(_("1 buffer deleted"));
904 smsg((char_u
*)_("%d buffers deleted"), deleted
);
909 MSG(_("1 buffer wiped out"));
911 smsg((char_u
*)_("%d buffers wiped out"), deleted
);
916 #ifdef FEAT_NETBEANS_INTG
917 netbeansCloseFile
= 0;
924 * Implementation of the commands for the buffer list.
926 * action == DOBUF_GOTO go to specified buffer
927 * action == DOBUF_SPLIT split window and go to specified buffer
928 * action == DOBUF_UNLOAD unload specified buffer(s)
929 * action == DOBUF_DEL delete specified buffer(s) from buffer list
930 * action == DOBUF_WIPE delete specified buffer(s) really
932 * start == DOBUF_CURRENT go to "count" buffer from current buffer
933 * start == DOBUF_FIRST go to "count" buffer from first buffer
934 * start == DOBUF_LAST go to "count" buffer from last buffer
935 * start == DOBUF_MOD go to "count" modified buffer from current buffer
940 do_buffer(action
, start
, dir
, count
, forceit
)
943 int dir
; /* FORWARD or BACKWARD */
944 int count
; /* buffer number or number of buffers */
945 int forceit
; /* TRUE for :...! */
949 int unload
= (action
== DOBUF_UNLOAD
|| action
== DOBUF_DEL
950 || action
== DOBUF_WIPE
);
954 case DOBUF_FIRST
: buf
= firstbuf
; break;
955 case DOBUF_LAST
: buf
= lastbuf
; break;
956 default: buf
= curbuf
; break;
958 if (start
== DOBUF_MOD
) /* find next modified buffer */
968 while (buf
!= curbuf
&& !bufIsChanged(buf
));
970 if (!bufIsChanged(buf
))
972 EMSG(_("E84: No modified buffer found"));
976 else if (start
== DOBUF_FIRST
&& count
) /* find specified buffer number */
978 while (buf
!= NULL
&& buf
->b_fnum
!= count
)
984 while (count
> 0 || (!unload
&& !buf
->b_p_bl
&& bp
!= buf
))
986 /* remember the buffer where we start, we come back there when all
987 * buffers are unlisted. */
1002 /* don't count unlisted buffers */
1003 if (unload
|| buf
->b_p_bl
)
1006 bp
= NULL
; /* use this buffer as new starting point */
1010 /* back where we started, didn't find anything. */
1011 EMSG(_("E85: There is no listed buffer"));
1017 if (buf
== NULL
) /* could not find it */
1019 if (start
== DOBUF_FIRST
)
1021 /* don't warn when deleting */
1023 EMSGN(_("E86: Buffer %ld does not exist"), count
);
1025 else if (dir
== FORWARD
)
1026 EMSG(_("E87: Cannot go beyond last buffer"));
1028 EMSG(_("E88: Cannot go before first buffer"));
1033 need_mouse_correct
= TRUE
;
1036 #ifdef FEAT_LISTCMDS
1038 * delete buffer buf from memory and/or the list
1045 /* When unloading or deleting a buffer that's already unloaded and
1046 * unlisted: fail silently. */
1047 if (action
!= DOBUF_WIPE
&& buf
->b_ml
.ml_mfp
== NULL
&& !buf
->b_p_bl
)
1050 if (!forceit
&& bufIsChanged(buf
))
1052 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1053 if ((p_confirm
|| cmdmod
.confirm
) && p_write
)
1055 dialog_changed(buf
, FALSE
);
1056 # ifdef FEAT_AUTOCMD
1057 if (!buf_valid(buf
))
1058 /* Autocommand deleted buffer, oops! It's not changed
1062 /* If it's still changed fail silently, the dialog already
1063 * mentioned why it fails. */
1064 if (bufIsChanged(buf
))
1070 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1077 * If deleting the last (listed) buffer, make it empty.
1078 * The last (listed) buffer cannot be unloaded.
1080 for (bp
= firstbuf
; bp
!= NULL
; bp
= bp
->b_next
)
1081 if (bp
->b_p_bl
&& bp
!= buf
)
1083 if (bp
== NULL
&& buf
== curbuf
)
1085 if (action
== DOBUF_UNLOAD
)
1087 EMSG(_("E90: Cannot unload last buffer"));
1091 /* Close any other windows on this buffer, then make it empty. */
1093 close_windows(buf
, TRUE
);
1096 retval
= do_ecmd(0, NULL
, NULL
, NULL
, ECMD_ONE
,
1097 forceit
? ECMD_FORCEIT
: 0);
1100 * do_ecmd() may create a new buffer, then we have to delete
1101 * the old one. But do_ecmd() may have done that already, check
1102 * if the buffer still exists.
1104 if (buf
!= curbuf
&& buf_valid(buf
) && buf
->b_nwindows
== 0)
1105 close_buffer(NULL
, buf
, action
);
1111 * If the deleted buffer is the current one, close the current window
1112 * (unless it's the only window). Repeat this so long as we end up in
1113 * a window with this buffer.
1115 while (buf
== curbuf
1116 && (firstwin
!= lastwin
|| first_tabpage
->tp_next
!= NULL
))
1117 win_close(curwin
, FALSE
);
1121 * If the buffer to be deleted is not the current one, delete it here.
1126 close_windows(buf
, FALSE
);
1128 if (buf
!= curbuf
&& buf_valid(buf
) && buf
->b_nwindows
<= 0)
1129 close_buffer(NULL
, buf
, action
);
1134 * Deleting the current buffer: Need to find another buffer to go to.
1135 * There must be another, otherwise it would have been handled above.
1136 * First use au_new_curbuf, if it is valid.
1137 * Then prefer the buffer we most recently visited.
1138 * Else try to find one that is loaded, after the current buffer,
1139 * then before the current buffer.
1140 * Finally use any buffer.
1142 buf
= NULL
; /* selected buffer */
1143 bp
= NULL
; /* used when no loaded buffer found */
1145 if (au_new_curbuf
!= NULL
&& buf_valid(au_new_curbuf
))
1146 buf
= au_new_curbuf
;
1147 # ifdef FEAT_JUMPLIST
1151 #ifdef FEAT_JUMPLIST
1152 if (curwin
->w_jumplistlen
> 0)
1156 jumpidx
= curwin
->w_jumplistidx
- 1;
1158 jumpidx
= curwin
->w_jumplistlen
- 1;
1161 while (jumpidx
!= curwin
->w_jumplistidx
)
1163 buf
= buflist_findnr(curwin
->w_jumplist
[jumpidx
].fmark
.fnum
);
1166 if (buf
== curbuf
|| !buf
->b_p_bl
)
1167 buf
= NULL
; /* skip current and unlisted bufs */
1168 else if (buf
->b_ml
.ml_mfp
== NULL
)
1170 /* skip unloaded buf, but may keep it for later */
1176 if (buf
!= NULL
) /* found a valid buffer: stop searching */
1178 /* advance to older entry in jump list */
1179 if (!jumpidx
&& curwin
->w_jumplistidx
== curwin
->w_jumplistlen
)
1182 jumpidx
= curwin
->w_jumplistlen
- 1;
1183 if (jumpidx
== forward
) /* List exhausted for sure */
1189 if (buf
== NULL
) /* No previous buffer, Try 2'nd approach */
1192 buf
= curbuf
->b_next
;
1197 if (!forward
) /* tried both directions */
1199 buf
= curbuf
->b_prev
;
1203 /* in non-help buffer, try to skip help buffers, and vv */
1204 if (buf
->b_help
== curbuf
->b_help
&& buf
->b_p_bl
)
1206 if (buf
->b_ml
.ml_mfp
!= NULL
) /* found loaded buffer */
1208 if (bp
== NULL
) /* remember unloaded buf for later */
1217 if (buf
== NULL
) /* No loaded buffer, use unloaded one */
1219 if (buf
== NULL
) /* No loaded buffer, find listed one */
1221 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
1222 if (buf
->b_p_bl
&& buf
!= curbuf
)
1225 if (buf
== NULL
) /* Still no buffer, just take one */
1227 if (curbuf
->b_next
!= NULL
)
1228 buf
= curbuf
->b_next
;
1230 buf
= curbuf
->b_prev
;
1235 * make buf current buffer
1237 if (action
== DOBUF_SPLIT
) /* split window first */
1239 # ifdef FEAT_WINDOWS
1240 /* If 'switchbuf' contains "useopen": jump to first window containing
1241 * "buf" if one exists */
1242 if ((swb_flags
& SWB_USEOPEN
) && buf_jump_open_win(buf
))
1244 /* If 'switchbuf' contians "usetab": jump to first window in any tab
1245 * page containing "buf" if one exists */
1246 if ((swb_flags
& SWB_USETAB
) && buf_jump_open_tab(buf
))
1248 if (win_split(0, 0) == FAIL
)
1254 /* go to current buffer - nothing to do */
1259 * Check if the current buffer may be abandoned.
1261 if (action
== DOBUF_GOTO
&& !can_abandon(curbuf
, forceit
))
1263 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1264 if ((p_confirm
|| cmdmod
.confirm
) && p_write
)
1266 dialog_changed(curbuf
, FALSE
);
1267 # ifdef FEAT_AUTOCMD
1268 if (!buf_valid(buf
))
1269 /* Autocommand deleted buffer, oops! */
1273 if (bufIsChanged(curbuf
))
1276 EMSG(_(e_nowrtmsg
));
1281 /* Go to the other buffer. */
1282 set_curbuf(buf
, action
);
1284 #if defined(FEAT_LISTCMDS) && defined(FEAT_SCROLLBIND)
1285 if (action
== DOBUF_SPLIT
)
1286 curwin
->w_p_scb
= FALSE
; /* reset 'scrollbind' */
1289 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1290 if (aborting()) /* autocmds may abort script processing */
1297 #endif /* FEAT_LISTCMDS */
1300 * Set current buffer to "buf". Executes autocommands and closes current
1301 * buffer. "action" tells how to close the current buffer:
1302 * DOBUF_GOTO free or hide it
1303 * DOBUF_SPLIT nothing
1304 * DOBUF_UNLOAD unload it
1305 * DOBUF_DEL delete it
1306 * DOBUF_WIPE wipe it out
1309 set_curbuf(buf
, action
)
1314 int unload
= (action
== DOBUF_UNLOAD
|| action
== DOBUF_DEL
1315 || action
== DOBUF_WIPE
);
1318 if (!cmdmod
.keepalt
)
1319 curwin
->w_alt_fnum
= curbuf
->b_fnum
; /* remember alternate file */
1320 buflist_altfpos(); /* remember curpos */
1323 /* Don't restart Select mode after switching to another buffer. */
1324 VIsual_reselect
= FALSE
;
1327 /* close_windows() or apply_autocmds() may change curbuf */
1331 apply_autocmds(EVENT_BUFLEAVE
, NULL
, NULL
, FALSE
, curbuf
);
1333 if (buf_valid(prevbuf
) && !aborting())
1335 if (buf_valid(prevbuf
))
1341 close_windows(prevbuf
, FALSE
);
1343 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1344 if (buf_valid(prevbuf
) && !aborting())
1346 if (buf_valid(prevbuf
))
1349 if (prevbuf
== curbuf
)
1351 close_buffer(prevbuf
== curwin
->w_buffer
? curwin
: NULL
, prevbuf
,
1352 unload
? action
: (action
== DOBUF_GOTO
1354 && !bufIsChanged(prevbuf
)) ? DOBUF_UNLOAD
: 0);
1358 /* An autocommand may have deleted "buf", already entered it (e.g., when
1359 * it did ":bunload") or aborted the script processing! */
1361 if (buf_valid(buf
) && buf
!= curbuf
&& !aborting())
1363 if (buf_valid(buf
) && buf
!= curbuf
)
1370 * Enter a new current buffer.
1371 * Old curbuf must have been abandoned already!
1377 /* Copy buffer and window local option values. Not for a help buffer. */
1378 buf_copy_options(buf
, BCO_ENTER
| BCO_NOHELP
);
1383 /* Remove all folds in the window. */
1384 clearFolding(curwin
);
1385 foldUpdateAll(curwin
); /* update folds (later). */
1388 /* Get the buffer in the current window. */
1389 curwin
->w_buffer
= buf
;
1391 ++curbuf
->b_nwindows
;
1394 if (curwin
->w_p_diff
)
1395 diff_buf_add(curbuf
);
1398 /* Cursor on first line by default. */
1399 curwin
->w_cursor
.lnum
= 1;
1400 curwin
->w_cursor
.col
= 0;
1401 #ifdef FEAT_VIRTUALEDIT
1402 curwin
->w_cursor
.coladd
= 0;
1404 curwin
->w_set_curswant
= TRUE
;
1406 /* Make sure the buffer is loaded. */
1407 if (curbuf
->b_ml
.ml_mfp
== NULL
) /* need to load the file */
1410 /* If there is no filetype, allow for detecting one. Esp. useful for
1411 * ":ball" used in a autocommand. If there already is a filetype we
1412 * might prefer to keep it. */
1413 if (*curbuf
->b_p_ft
== NUL
)
1414 did_filetype
= FALSE
;
1417 open_buffer(FALSE
, NULL
);
1422 need_fileinfo
= TRUE
; /* display file info after redraw */
1423 (void)buf_check_timestamp(curbuf
, FALSE
); /* check if file changed */
1425 curwin
->w_topline
= 1;
1427 curwin
->w_topfill
= 0;
1429 apply_autocmds(EVENT_BUFENTER
, NULL
, NULL
, FALSE
, curbuf
);
1430 apply_autocmds(EVENT_BUFWINENTER
, NULL
, NULL
, FALSE
, curbuf
);
1434 /* If autocommands did not change the cursor position, restore cursor lnum
1435 * and possibly cursor col. */
1436 if (curwin
->w_cursor
.lnum
== 1 && inindent(0))
1439 check_arg_idx(curwin
); /* check for valid arg_idx */
1444 if (curwin
->w_topline
== 1) /* when autocmds didn't change it */
1446 scroll_cursor_halfway(FALSE
); /* redisplay at correct position */
1448 #ifdef FEAT_NETBEANS_INTG
1449 /* Send fileOpened event because we've changed buffers. */
1450 if (usingNetbeans
&& isNetbeansBuffer(curbuf
))
1451 netbeans_file_activated(curbuf
);
1454 /* Change directories when the 'acd' option is set. */
1458 if (curbuf
->b_kmap_state
& KEYMAP_INIT
)
1462 /* May need to set the spell language. Can only do this after the buffer
1463 * has been properly setup. */
1464 if (!curbuf
->b_help
&& curwin
->w_p_spell
&& *curbuf
->b_p_spl
!= NUL
)
1465 did_set_spelllang(curbuf
);
1468 redraw_later(NOT_VALID
);
1471 #if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1473 * Change to the directory of the current buffer.
1478 if (curbuf
->b_ffname
!= NULL
&& vim_chdirfile(curbuf
->b_ffname
) == OK
)
1479 shorten_fnames(TRUE
);
1484 * functions for dealing with the buffer list
1488 * Add a file name to the buffer list. Return a pointer to the buffer.
1489 * If the same file name already exists return a pointer to that buffer.
1490 * If it does not exist, or if fname == NULL, a new entry is created.
1491 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1492 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1493 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
1494 * This is the ONLY way to create a new buffer.
1496 static int top_file_num
= 1; /* highest file number */
1499 buflist_new(ffname
, sfname
, lnum
, flags
)
1500 char_u
*ffname
; /* full path of fname or relative */
1501 char_u
*sfname
; /* short fname or NULL */
1502 linenr_T lnum
; /* preferred cursor line */
1503 int flags
; /* BLN_ defines */
1510 fname_expand(curbuf
, &ffname
, &sfname
); /* will allocate ffname */
1513 * If file name already exists in the list, update the entry.
1516 /* On Unix we can use inode numbers when the file exists. Works better
1517 * for hard links. */
1518 if (sfname
== NULL
|| mch_stat((char *)sfname
, &st
) < 0)
1519 st
.st_dev
= (dev_T
)-1;
1521 if (ffname
!= NULL
&& !(flags
& BLN_DUMMY
) && (buf
=
1523 buflist_findname_stat(ffname
, &st
)
1525 buflist_findname(ffname
)
1531 buflist_setfpos(buf
, curwin
, lnum
, (colnr_T
)0, FALSE
);
1532 /* copy the options now, if 'cpo' doesn't have 's' and not done
1534 buf_copy_options(buf
, 0);
1535 if ((flags
& BLN_LISTED
) && !buf
->b_p_bl
)
1539 if (!(flags
& BLN_DUMMY
))
1540 apply_autocmds(EVENT_BUFADD
, NULL
, NULL
, FALSE
, buf
);
1547 * If the current buffer has no name and no contents, use the current
1548 * buffer. Otherwise: Need to allocate a new buffer structure.
1550 * This is the ONLY place where a new buffer structure is allocated!
1551 * (A spell file buffer is allocated in spell.c, but that's not a normal
1555 if ((flags
& BLN_CURBUF
)
1557 && curbuf
->b_ffname
== NULL
1558 && curbuf
->b_nwindows
<= 1
1559 && (curbuf
->b_ml
.ml_mfp
== NULL
|| bufempty()))
1563 /* It's like this buffer is deleted. Watch out for autocommands that
1564 * change curbuf! If that happens, allocate a new buffer anyway. */
1566 apply_autocmds(EVENT_BUFDELETE
, NULL
, NULL
, FALSE
, curbuf
);
1568 apply_autocmds(EVENT_BUFWIPEOUT
, NULL
, NULL
, FALSE
, curbuf
);
1570 if (aborting()) /* autocmds may abort script processing */
1574 #ifdef FEAT_QUICKFIX
1575 # ifdef FEAT_AUTOCMD
1579 /* Make sure 'bufhidden' and 'buftype' are empty */
1580 clear_string_option(&buf
->b_p_bh
);
1581 clear_string_option(&buf
->b_p_bt
);
1585 if (buf
!= curbuf
|| curbuf
== NULL
)
1587 buf
= (buf_T
*)alloc_clear((unsigned)sizeof(buf_T
));
1597 buf
->b_ffname
= ffname
;
1598 buf
->b_sfname
= vim_strsave(sfname
);
1602 buf
->b_wininfo
= (wininfo_T
*)alloc_clear((unsigned)sizeof(wininfo_T
));
1604 if ((ffname
!= NULL
&& (buf
->b_ffname
== NULL
|| buf
->b_sfname
== NULL
))
1605 || buf
->b_wininfo
== NULL
)
1607 vim_free(buf
->b_ffname
);
1608 buf
->b_ffname
= NULL
;
1609 vim_free(buf
->b_sfname
);
1610 buf
->b_sfname
= NULL
;
1618 /* free all things allocated for this buffer */
1619 buf_freeall(buf
, FALSE
, FALSE
);
1620 if (buf
!= curbuf
) /* autocommands deleted the buffer! */
1622 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1623 if (aborting()) /* autocmds may abort script processing */
1626 /* buf->b_nwindows = 0; why was this here? */
1627 free_buffer_stuff(buf
, FALSE
); /* delete local variables et al. */
1629 /* need to reload lmaps and set b:keymap_name */
1630 curbuf
->b_kmap_state
|= KEYMAP_INIT
;
1636 * put new buffer at the end of the buffer list
1639 if (firstbuf
== NULL
) /* buffer list is empty */
1644 else /* append new buffer at end of list */
1646 lastbuf
->b_next
= buf
;
1647 buf
->b_prev
= lastbuf
;
1651 buf
->b_fnum
= top_file_num
++;
1652 if (top_file_num
< 0) /* wrap around (may cause duplicates) */
1654 EMSG(_("W14: Warning: List of file names overflow"));
1655 if (emsg_silent
== 0)
1658 ui_delay(3000L, TRUE
); /* make sure it is noticed */
1664 * Always copy the options from the current buffer.
1666 buf_copy_options(buf
, BCO_ALWAYS
);
1669 buf
->b_wininfo
->wi_fpos
.lnum
= lnum
;
1670 buf
->b_wininfo
->wi_win
= curwin
;
1673 init_var_dict(&buf
->b_vars
, &buf
->b_bufvar
); /* init b: variables */
1676 hash_init(&buf
->b_keywtab
);
1677 hash_init(&buf
->b_keywtab_ic
);
1680 buf
->b_fname
= buf
->b_sfname
;
1682 if (st
.st_dev
== (dev_T
)-1)
1686 buf
->b_dev
= st
.st_dev
;
1687 buf
->b_ino
= st
.st_ino
;
1690 buf
->b_u_synced
= TRUE
;
1691 buf
->b_flags
= BF_CHECK_RO
| BF_NEVERLOADED
;
1692 if (flags
& BLN_DUMMY
)
1693 buf
->b_flags
|= BF_DUMMY
;
1694 buf_clear_file(buf
);
1695 clrallmarks(buf
); /* clear marks */
1696 fmarks_check_names(buf
); /* check file marks for this file */
1697 buf
->b_p_bl
= (flags
& BLN_LISTED
) ? TRUE
: FALSE
; /* init 'buflisted' */
1699 if (!(flags
& BLN_DUMMY
))
1701 apply_autocmds(EVENT_BUFNEW
, NULL
, NULL
, FALSE
, buf
);
1702 if (flags
& BLN_LISTED
)
1703 apply_autocmds(EVENT_BUFADD
, NULL
, NULL
, FALSE
, buf
);
1705 if (aborting()) /* autocmds may abort script processing */
1715 * Free the memory for the options of a buffer.
1716 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1720 free_buf_options(buf
, free_p_ff
)
1727 clear_string_option(&buf
->b_p_fenc
);
1729 clear_string_option(&buf
->b_p_ff
);
1730 #ifdef FEAT_QUICKFIX
1731 clear_string_option(&buf
->b_p_bh
);
1732 clear_string_option(&buf
->b_p_bt
);
1736 clear_string_option(&buf
->b_p_def
);
1737 clear_string_option(&buf
->b_p_inc
);
1739 clear_string_option(&buf
->b_p_inex
);
1742 #if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1743 clear_string_option(&buf
->b_p_inde
);
1744 clear_string_option(&buf
->b_p_indk
);
1746 #if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1747 clear_string_option(&buf
->b_p_bexpr
);
1749 #if defined(FEAT_EVAL)
1750 clear_string_option(&buf
->b_p_fex
);
1753 clear_string_option(&buf
->b_p_key
);
1755 clear_string_option(&buf
->b_p_kp
);
1756 clear_string_option(&buf
->b_p_mps
);
1757 clear_string_option(&buf
->b_p_fo
);
1758 clear_string_option(&buf
->b_p_flp
);
1759 clear_string_option(&buf
->b_p_isk
);
1761 clear_string_option(&buf
->b_p_keymap
);
1762 ga_clear(&buf
->b_kmap_ga
);
1764 #ifdef FEAT_COMMENTS
1765 clear_string_option(&buf
->b_p_com
);
1768 clear_string_option(&buf
->b_p_cms
);
1770 clear_string_option(&buf
->b_p_nf
);
1772 clear_string_option(&buf
->b_p_syn
);
1775 clear_string_option(&buf
->b_p_spc
);
1776 clear_string_option(&buf
->b_p_spf
);
1777 vim_free(buf
->b_cap_prog
);
1778 buf
->b_cap_prog
= NULL
;
1779 clear_string_option(&buf
->b_p_spl
);
1781 #ifdef FEAT_SEARCHPATH
1782 clear_string_option(&buf
->b_p_sua
);
1785 clear_string_option(&buf
->b_p_ft
);
1787 #ifdef FEAT_OSFILETYPE
1788 clear_string_option(&buf
->b_p_oft
);
1791 clear_string_option(&buf
->b_p_cink
);
1792 clear_string_option(&buf
->b_p_cino
);
1794 #if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1795 clear_string_option(&buf
->b_p_cinw
);
1797 #ifdef FEAT_INS_EXPAND
1798 clear_string_option(&buf
->b_p_cpt
);
1800 #ifdef FEAT_COMPL_FUNC
1801 clear_string_option(&buf
->b_p_cfu
);
1802 clear_string_option(&buf
->b_p_ofu
);
1804 #ifdef FEAT_QUICKFIX
1805 clear_string_option(&buf
->b_p_gp
);
1806 clear_string_option(&buf
->b_p_mp
);
1807 clear_string_option(&buf
->b_p_efm
);
1809 clear_string_option(&buf
->b_p_ep
);
1810 clear_string_option(&buf
->b_p_path
);
1811 clear_string_option(&buf
->b_p_tags
);
1812 #ifdef FEAT_INS_EXPAND
1813 clear_string_option(&buf
->b_p_dict
);
1814 clear_string_option(&buf
->b_p_tsr
);
1817 clear_string_option(&buf
->b_p_qe
);
1823 * get alternate file n
1824 * set linenr to lnum or altfpos.lnum if lnum == 0
1825 * also set cursor column to altfpos.col if 'startofline' is not set.
1826 * if (options & GETF_SETMARK) call setpcmark()
1827 * if (options & GETF_ALT) we are jumping to an alternate file.
1828 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1830 * return FAIL for failure, OK for success
1833 buflist_getfile(n
, lnum
, options
, forceit
)
1846 buf
= buflist_findnr(n
);
1849 if ((options
& GETF_ALT
) && n
== 0)
1852 EMSGN(_("E92: Buffer %ld not found"), n
);
1856 /* if alternate file is the current buffer, nothing to do */
1866 if (curbuf_locked())
1870 /* altfpos may be changed by getfile(), get it now */
1873 fpos
= buflist_findfpos(buf
);
1881 if (options
& GETF_SWITCH
)
1883 /* If 'switchbuf' contains "useopen": jump to first window containing
1884 * "buf" if one exists */
1885 if (swb_flags
& SWB_USEOPEN
)
1886 wp
= buf_jump_open_win(buf
);
1887 /* If 'switchbuf' contians "usetab": jump to first window in any tab
1888 * page containing "buf" if one exists */
1889 if (wp
== NULL
&& (swb_flags
& SWB_USETAB
))
1890 wp
= buf_jump_open_tab(buf
);
1891 /* If 'switchbuf' contains "split" or "newtab" and the current buffer
1892 * isn't empty: open new window */
1893 if (wp
== NULL
&& (swb_flags
& (SWB_SPLIT
| SWB_NEWTAB
)) && !bufempty())
1895 if (swb_flags
& SWB_NEWTAB
) /* Open in a new tab */
1897 else if (win_split(0, 0) == FAIL
) /* Open in a new window */
1899 # ifdef FEAT_SCROLLBIND
1900 curwin
->w_p_scb
= FALSE
;
1906 ++RedrawingDisabled
;
1907 if (getfile(buf
->b_fnum
, NULL
, NULL
, (options
& GETF_SETMARK
),
1908 lnum
, forceit
) <= 0)
1910 --RedrawingDisabled
;
1912 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
1913 if (!p_sol
&& col
!= 0)
1915 curwin
->w_cursor
.col
= col
;
1917 #ifdef FEAT_VIRTUALEDIT
1918 curwin
->w_cursor
.coladd
= 0;
1920 curwin
->w_set_curswant
= TRUE
;
1924 --RedrawingDisabled
;
1929 * go to the last know line number for the current buffer
1936 fpos
= buflist_findfpos(curbuf
);
1938 curwin
->w_cursor
.lnum
= fpos
->lnum
;
1939 check_cursor_lnum();
1942 curwin
->w_cursor
.col
= 0;
1945 curwin
->w_cursor
.col
= fpos
->col
;
1947 #ifdef FEAT_VIRTUALEDIT
1948 curwin
->w_cursor
.coladd
= 0;
1950 curwin
->w_set_curswant
= TRUE
;
1954 #if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
1956 * Find file in buffer list by name (it has to be for the current window).
1957 * Returns NULL if not found.
1960 buflist_findname_exp(fname
)
1966 /* First make the name into a full path name */
1967 ffname
= FullName_save(fname
,
1969 TRUE
/* force expansion, get rid of symbolic links */
1976 buf
= buflist_findname(ffname
);
1984 * Find file in buffer list by name (it has to be for the current window).
1985 * "ffname" must have a full path.
1986 * Skips dummy buffers.
1987 * Returns NULL if not found.
1990 buflist_findname(ffname
)
1996 if (mch_stat((char *)ffname
, &st
) < 0)
1997 st
.st_dev
= (dev_T
)-1;
1998 return buflist_findname_stat(ffname
, &st
);
2002 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2003 * twice for the same file.
2004 * Returns NULL if not found.
2007 buflist_findname_stat(ffname
, stp
)
2014 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
2015 if ((buf
->b_flags
& BF_DUMMY
) == 0 && !otherfile_buf(buf
, ffname
2024 #if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO)
2026 * Find file in buffer list by a regexp pattern.
2027 * Return fnum of the found buffer.
2028 * Return < 0 for error.
2032 buflist_findpat(pattern
, pattern_end
, unlisted
, diffmode
)
2034 char_u
*pattern_end
; /* pointer to first char after pattern */
2035 int unlisted
; /* find unlisted buffers */
2036 int diffmode
; /* find diff-mode buffers only */
2048 if (pattern_end
== pattern
+ 1 && (*pattern
== '%' || *pattern
== '#'))
2050 if (*pattern
== '%')
2051 match
= curbuf
->b_fnum
;
2053 match
= curwin
->w_alt_fnum
;
2055 if (diffmode
&& !diff_mode_buf(buflist_findnr(match
)))
2061 * Try four ways of matching a listed buffer:
2062 * attempt == 0: without '^' or '$' (at any position)
2063 * attempt == 1: with '^' at start (only at position 0)
2064 * attempt == 2: with '$' at end (only match at end)
2065 * attempt == 3: with '^' at start and '$' at end (only full match)
2066 * Repeat this for finding an unlisted buffer if there was no matching
2071 pat
= file_pat_to_reg_pat(pattern
, pattern_end
, NULL
, FALSE
);
2074 patend
= pat
+ STRLEN(pat
) - 1;
2075 toggledollar
= (patend
> pat
&& *patend
== '$');
2077 /* First try finding a listed buffer. If not found and "unlisted"
2078 * is TRUE, try finding an unlisted buffer. */
2082 for (attempt
= 0; attempt
<= 3; ++attempt
)
2084 /* may add '^' and '$' */
2086 *patend
= (attempt
< 2) ? NUL
: '$'; /* add/remove '$' */
2088 if (*p
== '^' && !(attempt
& 1)) /* add/remove '^' */
2090 prog
= vim_regcomp(p
, p_magic
? RE_MAGIC
: 0);
2097 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
2098 if (buf
->b_p_bl
== find_listed
2100 && (!diffmode
|| diff_mode_buf(buf
))
2102 && buflist_match(prog
, buf
) != NULL
)
2104 if (match
>= 0) /* already found a match */
2109 match
= buf
->b_fnum
; /* remember first match */
2113 if (match
>= 0) /* found one match */
2117 /* Only search for unlisted buffers if there was no match with
2118 * a listed buffer. */
2119 if (!unlisted
|| !find_listed
|| match
!= -1)
2121 find_listed
= FALSE
;
2128 EMSG2(_("E93: More than one match for %s"), pattern
);
2130 EMSG2(_("E94: No matching buffer for %s"), pattern
);
2135 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2138 * Find all buffer names that match.
2139 * For command line expansion of ":buf" and ":sbuf".
2140 * Return OK if matches found, FAIL otherwise.
2143 ExpandBufnames(pat
, num_file
, file
, options
)
2157 *num_file
= 0; /* return values in case of FAIL */
2160 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2163 patc
= alloc((unsigned)STRLEN(pat
) + 11);
2166 STRCPY(patc
, "\\(^\\|[\\/]\\)");
2167 STRCPY(patc
+ 11, pat
+ 1);
2173 * attempt == 0: try match with '\<', match at start of word
2174 * attempt == 1: try match without '\<', match anywhere
2176 for (attempt
= 0; attempt
<= 1; ++attempt
)
2178 if (attempt
> 0 && patc
== pat
)
2179 break; /* there was no anchor, no need to try again */
2180 prog
= vim_regcomp(patc
+ attempt
* 11, RE_MAGIC
);
2189 * round == 1: Count the matches.
2190 * round == 2: Build the array to keep the matches.
2192 for (round
= 1; round
<= 2; ++round
)
2195 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
2197 if (!buf
->b_p_bl
) /* skip unlisted buffers */
2199 p
= buflist_match(prog
, buf
);
2206 if (options
& WILD_HOME_REPLACE
)
2207 p
= home_replace_save(buf
, p
);
2210 (*file
)[count
++] = p
;
2214 if (count
== 0) /* no match found, break here */
2218 *file
= (char_u
**)alloc((unsigned)(count
* sizeof(char_u
*)));
2229 if (count
) /* match(es) found, break here */
2237 return (count
== 0 ? FAIL
: OK
);
2240 #endif /* FEAT_CMDL_COMPL */
2242 #ifdef HAVE_BUFLIST_MATCH
2244 * Check for a match on the file name for buffer "buf" with regprog "prog".
2247 buflist_match(prog
, buf
)
2253 /* First try the short file name, then the long file name. */
2254 match
= fname_match(prog
, buf
->b_sfname
);
2256 match
= fname_match(prog
, buf
->b_ffname
);
2262 * Try matching the regexp in "prog" with file name "name".
2263 * Return "name" when there is a match, NULL when not.
2266 fname_match(prog
, name
)
2270 char_u
*match
= NULL
;
2272 regmatch_T regmatch
;
2276 regmatch
.regprog
= prog
;
2277 #ifdef CASE_INSENSITIVE_FILENAME
2278 regmatch
.rm_ic
= TRUE
; /* Always ignore case */
2280 regmatch
.rm_ic
= FALSE
; /* Never ignore case */
2283 if (vim_regexec(®match
, name
, (colnr_T
)0))
2287 /* Replace $(HOME) with '~' and try matching again. */
2288 p
= home_replace_save(NULL
, name
);
2289 if (p
!= NULL
&& vim_regexec(®match
, p
, (colnr_T
)0))
2300 * find file in buffer list by number
2309 nr
= curwin
->w_alt_fnum
;
2310 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
2311 if (buf
->b_fnum
== nr
)
2317 * Get name of file 'n' in the buffer list.
2318 * When the file has no name an empty string is returned.
2319 * home_replace() is used to shorten the file name (used for marks).
2320 * Returns a pointer to allocated memory, of NULL when failed.
2323 buflist_nr2name(n
, fullname
, helptail
)
2326 int helptail
; /* for help buffers return tail only */
2330 buf
= buflist_findnr(n
);
2333 return home_replace_save(helptail
? buf
: NULL
,
2334 fullname
? buf
->b_ffname
: buf
->b_fname
);
2338 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2339 * When "copy_options" is TRUE save the local window option values.
2340 * When "lnum" is 0 only do the options.
2343 buflist_setfpos(buf
, win
, lnum
, col
, copy_options
)
2352 for (wip
= buf
->b_wininfo
; wip
!= NULL
; wip
= wip
->wi_next
)
2353 if (wip
->wi_win
== win
)
2357 /* allocate a new entry */
2358 wip
= (wininfo_T
*)alloc_clear((unsigned)sizeof(wininfo_T
));
2362 if (lnum
== 0) /* set lnum even when it's 0 */
2367 /* remove the entry from the list */
2369 wip
->wi_prev
->wi_next
= wip
->wi_next
;
2371 buf
->b_wininfo
= wip
->wi_next
;
2373 wip
->wi_next
->wi_prev
= wip
->wi_prev
;
2374 if (copy_options
&& wip
->wi_optset
)
2376 clear_winopt(&wip
->wi_opt
);
2378 deleteFoldRecurse(&wip
->wi_folds
);
2384 wip
->wi_fpos
.lnum
= lnum
;
2385 wip
->wi_fpos
.col
= col
;
2389 /* Save the window-specific option values. */
2390 copy_winopt(&win
->w_onebuf_opt
, &wip
->wi_opt
);
2392 wip
->wi_fold_manual
= win
->w_fold_manual
;
2393 cloneFoldGrowArray(&win
->w_folds
, &wip
->wi_folds
);
2395 wip
->wi_optset
= TRUE
;
2398 /* insert the entry in front of the list */
2399 wip
->wi_next
= buf
->b_wininfo
;
2400 buf
->b_wininfo
= wip
;
2401 wip
->wi_prev
= NULL
;
2403 wip
->wi_next
->wi_prev
= wip
;
2409 * Find info for the current window in buffer "buf".
2410 * If not found, return the info for the most recently used window.
2411 * Returns NULL when there isn't any info.
2419 for (wip
= buf
->b_wininfo
; wip
!= NULL
; wip
= wip
->wi_next
)
2420 if (wip
->wi_win
== curwin
)
2422 if (wip
== NULL
) /* if no fpos for curwin, use the first in the list */
2423 wip
= buf
->b_wininfo
;
2428 * Reset the local window options to the values last used in this window.
2429 * If the buffer wasn't used in this window before, use the values from
2430 * the most recently used window. If the values were never set, use the
2431 * global values for the window.
2439 clear_winopt(&curwin
->w_onebuf_opt
);
2441 clearFolding(curwin
);
2444 wip
= find_wininfo(buf
);
2445 if (wip
!= NULL
&& wip
->wi_optset
)
2447 copy_winopt(&wip
->wi_opt
, &curwin
->w_onebuf_opt
);
2449 curwin
->w_fold_manual
= wip
->wi_fold_manual
;
2450 curwin
->w_foldinvalid
= TRUE
;
2451 cloneFoldGrowArray(&wip
->wi_folds
, &curwin
->w_folds
);
2455 copy_winopt(&curwin
->w_allbuf_opt
, &curwin
->w_onebuf_opt
);
2458 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2460 curwin
->w_p_fdl
= p_fdls
;
2465 * Find the position (lnum and col) for the buffer 'buf' for the current
2467 * Returns a pointer to no_position if no position is found.
2470 buflist_findfpos(buf
)
2474 static pos_T no_position
= {1, 0};
2476 wip
= find_wininfo(buf
);
2478 return &(wip
->wi_fpos
);
2480 return &no_position
;
2484 * Find the lnum for the buffer 'buf' for the current window.
2487 buflist_findlnum(buf
)
2490 return buflist_findfpos(buf
)->lnum
;
2493 #if defined(FEAT_LISTCMDS) || defined(PROTO)
2495 * List all know file names (for :files and :buffers command).
2506 for (buf
= firstbuf
; buf
!= NULL
&& !got_int
; buf
= buf
->b_next
)
2508 /* skip unlisted buffers, unless ! was used */
2509 if (!buf
->b_p_bl
&& !eap
->forceit
)
2512 if (buf_spname(buf
) != NULL
)
2513 STRCPY(NameBuff
, buf_spname(buf
));
2515 home_replace(buf
, buf
->b_fname
, NameBuff
, MAXPATHL
, TRUE
);
2517 len
= vim_snprintf((char *)IObuff
, IOSIZE
- 20, "%3d%c%c%c%c%c \"%s\"",
2519 buf
->b_p_bl
? ' ' : 'u',
2520 buf
== curbuf
? '%' :
2521 (curwin
->w_alt_fnum
== buf
->b_fnum
? '#' : ' '),
2522 buf
->b_ml
.ml_mfp
== NULL
? ' ' :
2523 (buf
->b_nwindows
== 0 ? 'h' : 'a'),
2524 !buf
->b_p_ma
? '-' : (buf
->b_p_ro
? '=' : ' '),
2525 (buf
->b_flags
& BF_READERR
) ? 'x'
2526 : (bufIsChanged(buf
) ? '+' : ' '),
2529 /* put "line 999" in column 40 or after the file name */
2530 i
= 40 - vim_strsize(IObuff
);
2533 IObuff
[len
++] = ' ';
2534 } while (--i
> 0 && len
< IOSIZE
- 18);
2535 vim_snprintf((char *)IObuff
+ len
, IOSIZE
- len
, _("line %ld"),
2536 buf
== curbuf
? curwin
->w_cursor
.lnum
2537 : (long)buflist_findlnum(buf
));
2538 msg_outtrans(IObuff
);
2539 out_flush(); /* output one line at a time */
2546 * Get file name and line number for file 'fnum'.
2547 * Used by DoOneCmd() for translating '%' and '#'.
2548 * Used by insert_reg() and cmdline_paste() for '#' register.
2549 * Return FAIL if not found, OK for success.
2552 buflist_name_nr(fnum
, fname
, lnum
)
2559 buf
= buflist_findnr(fnum
);
2560 if (buf
== NULL
|| buf
->b_fname
== NULL
)
2563 *fname
= buf
->b_fname
;
2564 *lnum
= buflist_findlnum(buf
);
2570 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2571 * The file name with the full path is also remembered, for when :cd is used.
2572 * Returns FAIL for failure (file name already in use by other buffer)
2576 setfname(buf
, ffname
, sfname
, message
)
2578 char_u
*ffname
, *sfname
;
2579 int message
; /* give message when buffer already exists */
2586 if (ffname
== NULL
|| *ffname
== NUL
)
2588 /* Removing the name. */
2589 vim_free(buf
->b_ffname
);
2590 vim_free(buf
->b_sfname
);
2591 buf
->b_ffname
= NULL
;
2592 buf
->b_sfname
= NULL
;
2594 st
.st_dev
= (dev_T
)-1;
2599 fname_expand(buf
, &ffname
, &sfname
); /* will allocate ffname */
2600 if (ffname
== NULL
) /* out of memory */
2604 * if the file name is already used in another buffer:
2605 * - if the buffer is loaded, fail
2606 * - if the buffer is not loaded, delete it from the list
2609 if (mch_stat((char *)ffname
, &st
) < 0)
2610 st
.st_dev
= (dev_T
)-1;
2612 if (!(buf
->b_flags
& BF_DUMMY
))
2614 obuf
= buflist_findname_stat(ffname
, &st
);
2616 obuf
= buflist_findname(ffname
);
2618 if (obuf
!= NULL
&& obuf
!= buf
)
2620 if (obuf
->b_ml
.ml_mfp
!= NULL
) /* it's loaded, fail */
2623 EMSG(_("E95: Buffer with this name already exists"));
2627 close_buffer(NULL
, obuf
, DOBUF_WIPE
); /* delete from the list */
2629 sfname
= vim_strsave(sfname
);
2630 if (ffname
== NULL
|| sfname
== NULL
)
2636 #ifdef USE_FNAME_CASE
2637 # ifdef USE_LONG_FNAME
2640 fname_case(sfname
, 0); /* set correct case for short file name */
2642 vim_free(buf
->b_ffname
);
2643 vim_free(buf
->b_sfname
);
2644 buf
->b_ffname
= ffname
;
2645 buf
->b_sfname
= sfname
;
2647 buf
->b_fname
= buf
->b_sfname
;
2649 if (st
.st_dev
== (dev_T
)-1)
2653 buf
->b_dev
= st
.st_dev
;
2654 buf
->b_ino
= st
.st_ino
;
2659 buf
->b_shortname
= FALSE
;
2662 buf_name_changed(buf
);
2667 * Crude way of changing the name of a buffer. Use with care!
2668 * The name should be relative to the current directory.
2671 buf_set_name(fnum
, name
)
2677 buf
= buflist_findnr(fnum
);
2680 vim_free(buf
->b_sfname
);
2681 vim_free(buf
->b_ffname
);
2682 buf
->b_ffname
= vim_strsave(name
);
2683 buf
->b_sfname
= NULL
;
2684 /* Allocate ffname and expand into full path. Also resolves .lnk
2685 * files on Win32. */
2686 fname_expand(buf
, &buf
->b_ffname
, &buf
->b_sfname
);
2687 buf
->b_fname
= buf
->b_sfname
;
2692 * Take care of what needs to be done when the name of buffer "buf" has
2696 buf_name_changed(buf
)
2700 * If the file name changed, also change the name of the swapfile
2702 if (buf
->b_ml
.ml_mfp
!= NULL
)
2705 if (curwin
->w_buffer
== buf
)
2706 check_arg_idx(curwin
); /* check file name for arg list */
2708 maketitle(); /* set window title */
2711 status_redraw_all(); /* status lines need to be redrawn */
2713 fmarks_check_names(buf
); /* check named file marks */
2714 ml_timestamp(buf
); /* reset timestamp */
2718 * set alternate file name for current window
2720 * Used by do_one_cmd(), do_write() and do_ecmd().
2721 * Return the buffer.
2724 setaltfname(ffname
, sfname
, lnum
)
2731 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2732 buf
= buflist_new(ffname
, sfname
, lnum
, 0);
2733 if (buf
!= NULL
&& !cmdmod
.keepalt
)
2734 curwin
->w_alt_fnum
= buf
->b_fnum
;
2739 * Get alternate file name for current window.
2740 * Return NULL if there isn't any, and give error message if requested.
2744 int errmsg
; /* give error message */
2749 if (buflist_name_nr(0, &fname
, &dummy
) == FAIL
)
2759 * Add a file name to the buflist and return its number.
2760 * Uses same flags as buflist_new(), except BLN_DUMMY.
2762 * used by qf_init(), main() and doarglist()
2765 buflist_add(fname
, flags
)
2771 buf
= buflist_new(fname
, NULL
, (linenr_T
)0, flags
);
2777 #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2779 * Adjust slashes in file names. Called after 'shellslash' was set.
2782 buflist_slash_adjust()
2786 for (bp
= firstbuf
; bp
!= NULL
; bp
= bp
->b_next
)
2788 if (bp
->b_ffname
!= NULL
)
2789 slash_adjust(bp
->b_ffname
);
2790 if (bp
->b_sfname
!= NULL
)
2791 slash_adjust(bp
->b_sfname
);
2797 * Set alternate cursor position for current window.
2798 * Also save the local window option values.
2803 buflist_setfpos(curbuf
, curwin
, curwin
->w_cursor
.lnum
,
2804 curwin
->w_cursor
.col
, TRUE
);
2808 * Return TRUE if 'ffname' is not the same file as current file.
2809 * Fname must have a full path (expanded by mch_FullName()).
2815 return otherfile_buf(curbuf
, ffname
2823 otherfile_buf(buf
, ffname
2834 /* no name is different */
2835 if (ffname
== NULL
|| *ffname
== NUL
|| buf
->b_ffname
== NULL
)
2837 if (fnamecmp(ffname
, buf
->b_ffname
) == 0)
2843 /* If no struct stat given, get it now */
2846 if (buf
->b_dev
< 0 || mch_stat((char *)ffname
, &st
) < 0)
2847 st
.st_dev
= (dev_T
)-1;
2850 /* Use dev/ino to check if the files are the same, even when the names
2851 * are different (possible with links). Still need to compare the
2852 * name above, for when the file doesn't exist yet.
2853 * Problem: The dev/ino changes when a file is deleted (and created
2854 * again) and remains the same when renamed/moved. We don't want to
2855 * mch_stat() each buffer each time, that would be too slow. Get the
2856 * dev/ino again when they appear to match, but not when they appear
2857 * to be different: Could skip a buffer when it's actually the same
2859 if (buf_same_ino(buf
, stp
))
2862 if (buf_same_ino(buf
, stp
))
2870 #if defined(UNIX) || defined(PROTO)
2872 * Set inode and device number for a buffer.
2873 * Must always be called when b_fname is changed!.
2881 if (buf
->b_fname
!= NULL
&& mch_stat((char *)buf
->b_fname
, &st
) >= 0)
2883 buf
->b_dev
= st
.st_dev
;
2884 buf
->b_ino
= st
.st_ino
;
2891 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
2894 buf_same_ino(buf
, stp
)
2898 return (buf
->b_dev
>= 0
2899 && stp
->st_dev
== buf
->b_dev
2900 && stp
->st_ino
== buf
->b_ino
);
2905 * Print info about the current buffer.
2908 fileinfo(fullname
, shorthelp
, dont_truncate
)
2909 int fullname
; /* when non-zero print full path */
2919 buffer
= alloc(IOSIZE
);
2923 if (fullname
> 1) /* 2 CTRL-G: include buffer number */
2925 sprintf((char *)buffer
, "buf %d: ", curbuf
->b_fnum
);
2926 p
= buffer
+ STRLEN(buffer
);
2932 if (buf_spname(curbuf
) != NULL
)
2933 STRCPY(p
, buf_spname(curbuf
));
2936 if (!fullname
&& curbuf
->b_fname
!= NULL
)
2937 name
= curbuf
->b_fname
;
2939 name
= curbuf
->b_ffname
;
2940 home_replace(shorthelp
? curbuf
: NULL
, name
, p
,
2941 (int)(IOSIZE
- (p
- buffer
)), TRUE
);
2944 len
= STRLEN(buffer
);
2945 vim_snprintf((char *)buffer
+ len
, IOSIZE
- len
,
2947 curbufIsChanged() ? (shortmess(SHM_MOD
)
2948 ? " [+]" : _(" [Modified]")) : " ",
2949 (curbuf
->b_flags
& BF_NOTEDITED
)
2950 #ifdef FEAT_QUICKFIX
2951 && !bt_dontwrite(curbuf
)
2953 ? _("[Not edited]") : "",
2954 (curbuf
->b_flags
& BF_NEW
)
2955 #ifdef FEAT_QUICKFIX
2956 && !bt_dontwrite(curbuf
)
2958 ? _("[New file]") : "",
2959 (curbuf
->b_flags
& BF_READERR
) ? _("[Read errors]") : "",
2960 curbuf
->b_p_ro
? (shortmess(SHM_RO
) ? "[RO]"
2961 : _("[readonly]")) : "",
2962 (curbufIsChanged() || (curbuf
->b_flags
& BF_WRITE_MASK
)
2963 || curbuf
->b_p_ro
) ?
2965 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
2966 * causes an overflow, thus for large numbers divide instead. */
2967 if (curwin
->w_cursor
.lnum
> 1000000L)
2968 n
= (int)(((long)curwin
->w_cursor
.lnum
) /
2969 ((long)curbuf
->b_ml
.ml_line_count
/ 100L));
2971 n
= (int)(((long)curwin
->w_cursor
.lnum
* 100L) /
2972 (long)curbuf
->b_ml
.ml_line_count
);
2973 len
= STRLEN(buffer
);
2974 if (curbuf
->b_ml
.ml_flags
& ML_EMPTY
)
2976 vim_snprintf((char *)buffer
+ len
, IOSIZE
- len
, "%s", _(no_lines_msg
));
2978 #ifdef FEAT_CMDL_INFO
2981 /* Current line and column are already on the screen -- webb */
2982 if (curbuf
->b_ml
.ml_line_count
== 1)
2983 vim_snprintf((char *)buffer
+ len
, IOSIZE
- len
,
2984 _("1 line --%d%%--"), n
);
2986 vim_snprintf((char *)buffer
+ len
, IOSIZE
- len
,
2987 _("%ld lines --%d%%--"),
2988 (long)curbuf
->b_ml
.ml_line_count
, n
);
2993 vim_snprintf((char *)buffer
+ len
, IOSIZE
- len
,
2994 _("line %ld of %ld --%d%%-- col "),
2995 (long)curwin
->w_cursor
.lnum
,
2996 (long)curbuf
->b_ml
.ml_line_count
,
2999 col_print(buffer
+ STRLEN(buffer
),
3000 (int)curwin
->w_cursor
.col
+ 1, (int)curwin
->w_virtcol
+ 1);
3003 (void)append_arg_number(curwin
, buffer
, !shortmess(SHM_FILE
), IOSIZE
);
3007 /* Temporarily set msg_scroll to avoid the message being truncated.
3008 * First call msg_start() to get the message in the right place. */
3017 p
= msg_trunc_attr(buffer
, FALSE
, 0);
3018 if (restart_edit
!= 0 || (msg_scrolled
&& !need_wait_return
))
3019 /* Need to repeat the message after redrawing when:
3020 * - When restart_edit is set (otherwise there will be a delay
3021 * before redrawing).
3022 * - When the screen was scrolled but there is no wait-return
3031 col_print(buf
, col
, vcol
)
3037 sprintf((char *)buf
, "%d", col
);
3039 sprintf((char *)buf
, "%d-%d", col
, vcol
);
3042 #if defined(FEAT_TITLE) || defined(PROTO)
3044 * put file name in title bar of window and in icon title
3047 static char_u
*lasttitle
= NULL
;
3048 static char_u
*lasticon
= NULL
;
3054 char_u
*t_str
= NULL
;
3056 char_u
*i_str
= NULL
;
3065 /* Postpone updating the title when 'lazyredraw' is set. */
3066 need_maketitle
= TRUE
;
3070 #ifdef FEAT_GUI_MACVIM
3071 gui_macvim_update_modified_flag();
3074 need_maketitle
= FALSE
;
3075 if (!p_title
&& !p_icon
)
3082 maxlen
= p_titlelen
* Columns
/ 100;
3088 if (*p_titlestring
!= NUL
)
3091 if (stl_syntax
& STL_IN_TITLE
)
3093 int use_sandbox
= FALSE
;
3094 int save_called_emsg
= called_emsg
;
3097 use_sandbox
= was_set_insecurely((char_u
*)"titlestring", 0);
3099 called_emsg
= FALSE
;
3100 build_stl_str_hl(curwin
, t_str
, sizeof(buf
),
3101 p_titlestring
, use_sandbox
,
3102 0, maxlen
, NULL
, NULL
);
3104 set_string_option_direct((char_u
*)"titlestring", -1,
3105 (char_u
*)"", OPT_FREE
, SID_ERROR
);
3106 called_emsg
|= save_called_emsg
;
3110 t_str
= p_titlestring
;
3114 /* format: "fname + (path) (1 of 2) - VIM" */
3116 if (curbuf
->b_fname
== NULL
)
3117 STRCPY(buf
, _("[No Name]"));
3120 p
= transstr(gettail(curbuf
->b_fname
));
3121 vim_strncpy(buf
, p
, IOSIZE
- 100);
3125 switch (bufIsChanged(curbuf
)
3126 + (curbuf
->b_p_ro
* 2)
3127 + (!curbuf
->b_p_ma
* 4))
3129 case 1: STRCAT(buf
, " +"); break;
3130 case 2: STRCAT(buf
, " ="); break;
3131 case 3: STRCAT(buf
, " =+"); break;
3133 case 6: STRCAT(buf
, " -"); break;
3135 case 7: STRCAT(buf
, " -+"); break;
3138 if (curbuf
->b_fname
!= NULL
)
3140 /* Get path of file, replace home dir with ~ */
3141 off
= (int)STRLEN(buf
);
3144 home_replace(curbuf
, curbuf
->b_ffname
,
3145 buf
+ off
, IOSIZE
- off
, TRUE
);
3146 #ifdef BACKSLASH_IN_FILENAME
3147 /* avoid "c:/name" to be reduced to "c" */
3148 if (isalpha(buf
[off
]) && buf
[off
+ 1] == ':')
3151 /* remove the file name */
3152 p
= gettail_sep(buf
+ off
);
3154 /* must be a help buffer */
3155 vim_strncpy(buf
+ off
, (char_u
*)_("help"),
3160 /* translate unprintable chars */
3161 p
= transstr(buf
+ off
);
3162 vim_strncpy(buf
+ off
, p
, IOSIZE
- off
- 1);
3167 #ifndef FEAT_GUI_MACVIM
3168 append_arg_number(curwin
, buf
, FALSE
, IOSIZE
);
3171 #if defined(FEAT_CLIENTSERVER)
3172 if (serverName
!= NULL
)
3175 STRCAT(buf
, serverName
);
3179 STRCAT(buf
, " - VIM");
3183 /* make it shorter by removing a bit in the middle */
3184 len
= vim_strsize(buf
);
3186 trunc_string(buf
, buf
, maxlen
);
3190 mustset
= ti_change(t_str
, &lasttitle
);
3195 if (*p_iconstring
!= NUL
)
3198 if (stl_syntax
& STL_IN_ICON
)
3200 int use_sandbox
= FALSE
;
3201 int save_called_emsg
= called_emsg
;
3204 use_sandbox
= was_set_insecurely((char_u
*)"iconstring", 0);
3206 called_emsg
= FALSE
;
3207 build_stl_str_hl(curwin
, i_str
, sizeof(buf
),
3208 p_iconstring
, use_sandbox
,
3211 set_string_option_direct((char_u
*)"iconstring", -1,
3212 (char_u
*)"", OPT_FREE
, SID_ERROR
);
3213 called_emsg
|= save_called_emsg
;
3217 i_str
= p_iconstring
;
3221 if (buf_spname(curbuf
) != NULL
)
3222 i_name
= (char_u
*)buf_spname(curbuf
);
3223 else /* use file name only in icon */
3224 i_name
= gettail(curbuf
->b_ffname
);
3226 /* Truncate name at 100 bytes. */
3227 len
= (int)STRLEN(i_name
);
3233 len
+= (*mb_tail_off
)(i_name
, i_name
+ len
) + 1;
3237 STRCPY(i_str
, i_name
);
3238 trans_characters(i_str
, IOSIZE
);
3242 mustset
|= ti_change(i_str
, &lasticon
);
3249 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3250 * from "str" if it does.
3251 * Return TRUE when "*last" changed.
3254 ti_change(str
, last
)
3258 if ((str
== NULL
) != (*last
== NULL
)
3259 || (str
!= NULL
&& *last
!= NULL
&& STRCMP(str
, *last
) != 0))
3265 *last
= vim_strsave(str
);
3272 * Put current window title back (used after calling a shell)
3277 mch_settitle(lasttitle
, lasticon
);
3280 # if defined(EXITFREE) || defined(PROTO)
3284 vim_free(lasttitle
);
3289 #endif /* FEAT_TITLE */
3291 #if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
3293 * Build a string from the status line items in "fmt".
3294 * Return length of string in screen cells.
3296 * Normally works for window "wp", except when working for 'tabline' then it
3299 * Items are drawn interspersed with the text that surrounds it
3300 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3301 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3303 * If maxwidth is not zero, the string will be filled at any middle marker
3304 * or truncated if too long, fillchar is used for all whitespace.
3308 build_stl_str_hl(wp
, out
, outlen
, fmt
, use_sandbox
, fillchar
, maxwidth
, hltab
, tabtab
)
3310 char_u
*out
; /* buffer to write into != NameBuff */
3311 size_t outlen
; /* length of out[] */
3313 int use_sandbox
; /* "fmt" was set insecurely, use sandbox */
3316 struct stl_hlrec
*hltab
; /* return: HL attributes (can be NULL) */
3317 struct stl_hlrec
*tabtab
; /* return: tab page nrs (can be NULL) */
3331 int prevchar_isflag
;
3332 int prevchar_isitem
;
3340 int groupitem
[STL_MAX_ITEM
];
3357 } item
[STL_MAX_ITEM
];
3365 char_u
*usefmt
= fmt
;
3366 struct stl_hlrec
*sp
;
3370 * When the format starts with "%!" then evaluate it as an expression and
3371 * use the result as the actual format string.
3373 if (fmt
[0] == '%' && fmt
[1] == '!')
3375 usefmt
= eval_to_string_safe(fmt
+ 2, NULL
, use_sandbox
);
3384 /* Can't handle a multi-byte fill character yet. */
3385 else if (mb_char2len(fillchar
) > 1)
3390 * Get line & check if empty (cursorpos will show "0-1").
3391 * If inversion is possible we use it. Else '=' characters are used.
3393 linecont
= ml_get_buf(wp
->w_buffer
, wp
->w_cursor
.lnum
, FALSE
);
3394 empty_line
= (*linecont
== NUL
);
3399 prevchar_isflag
= TRUE
;
3400 prevchar_isitem
= FALSE
;
3401 for (s
= usefmt
; *s
; )
3403 if (*s
!= NUL
&& *s
!= '%')
3404 prevchar_isflag
= prevchar_isitem
= FALSE
;
3407 * Handle up to the next '%' or the end.
3409 while (*s
!= NUL
&& *s
!= '%' && p
+ 1 < out
+ outlen
)
3411 if (*s
== NUL
|| p
+ 1 >= out
+ outlen
)
3415 * Handle one '%' item.
3420 if (p
+ 1 >= out
+ outlen
)
3423 prevchar_isflag
= prevchar_isitem
= FALSE
;
3426 if (*s
== STL_MIDDLEMARK
)
3431 item
[curitem
].type
= Middle
;
3432 item
[curitem
++].start
= p
;
3435 if (*s
== STL_TRUNCMARK
)
3438 item
[curitem
].type
= Trunc
;
3439 item
[curitem
++].start
= p
;
3449 t
= item
[groupitem
[groupdepth
]].start
;
3452 if (curitem
> groupitem
[groupdepth
] + 1
3453 && item
[groupitem
[groupdepth
]].minwid
== 0)
3455 /* remove group if all items are empty */
3456 for (n
= groupitem
[groupdepth
] + 1; n
< curitem
; n
++)
3457 if (item
[n
].type
== Normal
)
3465 if (l
> item
[groupitem
[groupdepth
]].maxwid
)
3467 /* truncate, remove n bytes of text at the start */
3471 /* Find the first character that should be included. */
3473 while (l
>= item
[groupitem
[groupdepth
]].maxwid
)
3475 l
-= ptr2cells(t
+ n
);
3476 n
+= (*mb_ptr2len
)(t
+ n
);
3481 n
= (long)(p
- t
) - item
[groupitem
[groupdepth
]].maxwid
+ 1;
3484 mch_memmove(t
+ 1, t
+ n
, p
- (t
+ n
));
3487 /* Fill up space left over by half a double-wide char. */
3488 while (++l
< item
[groupitem
[groupdepth
]].minwid
)
3492 /* correct the start of the items for the truncation */
3493 for (l
= groupitem
[groupdepth
] + 1; l
< curitem
; l
++)
3496 if (item
[l
].start
< t
)
3500 else if (abs(item
[groupitem
[groupdepth
]].minwid
) > l
)
3503 n
= item
[groupitem
[groupdepth
]].minwid
;
3506 /* fill by appending characters */
3508 while (l
++ < n
&& p
+ 1 < out
+ outlen
)
3513 /* fill by inserting characters */
3514 mch_memmove(t
+ n
- l
, t
, p
- t
);
3516 if (p
+ l
>= out
+ outlen
)
3517 l
= (long)((out
+ outlen
) - p
- 1);
3519 for (n
= groupitem
[groupdepth
] + 1; n
< curitem
; n
++)
3541 if (VIM_ISDIGIT(*s
))
3543 minwid
= (int)getdigits(&s
);
3544 if (minwid
< 0) /* overflow */
3547 if (*s
== STL_USER_HL
)
3549 item
[curitem
].type
= Highlight
;
3550 item
[curitem
].start
= p
;
3551 item
[curitem
].minwid
= minwid
> 9 ? 1 : minwid
;
3556 if (*s
== STL_TABPAGENR
|| *s
== STL_TABCLOSENR
)
3558 if (*s
== STL_TABCLOSENR
)
3562 /* %X ends the close label, go back to the previously
3563 * define tab label nr. */
3564 for (n
= curitem
- 1; n
>= 0; --n
)
3565 if (item
[n
].type
== TabPage
&& item
[n
].minwid
>= 0)
3567 minwid
= item
[n
].minwid
;
3572 /* close nrs are stored as negative values */
3575 item
[curitem
].type
= TabPage
;
3576 item
[curitem
].start
= p
;
3577 item
[curitem
].minwid
= minwid
;
3585 if (VIM_ISDIGIT(*s
))
3587 maxwid
= (int)getdigits(&s
);
3588 if (maxwid
<= 0) /* overflow */
3592 minwid
= (minwid
> 50 ? 50 : minwid
) * l
;
3595 groupitem
[groupdepth
++] = curitem
;
3596 item
[curitem
].type
= Group
;
3597 item
[curitem
].start
= p
;
3598 item
[curitem
].minwid
= minwid
;
3599 item
[curitem
].maxwid
= maxwid
;
3604 if (vim_strchr(STL_ALL
, *s
) == NULL
)
3611 /* OK - now for the real work */
3622 fillable
= FALSE
; /* don't change ' ' to fillchar */
3623 if (buf_spname(wp
->w_buffer
) != NULL
)
3624 STRCPY(NameBuff
, buf_spname(wp
->w_buffer
));
3627 t
= (opt
== STL_FULLPATH
) ? wp
->w_buffer
->b_ffname
3628 : wp
->w_buffer
->b_fname
;
3629 home_replace(wp
->w_buffer
, t
, NameBuff
, MAXPATHL
, TRUE
);
3631 trans_characters(NameBuff
, MAXPATHL
);
3632 if (opt
!= STL_FILENAME
)
3635 str
= gettail(NameBuff
);
3638 case STL_VIM_EXPR
: /* '{' */
3641 while (*s
!= '}' && *s
!= NUL
&& p
+ 1 < out
+ outlen
)
3643 if (*s
!= '}') /* missing '}' or out of space */
3650 sprintf((char *)tmp
, "%d", curbuf
->b_fnum
);
3651 set_internal_string_var((char_u
*)"actual_curbuf", tmp
);
3656 curbuf
= wp
->w_buffer
;
3658 str
= eval_to_string_safe(p
, &t
, use_sandbox
);
3662 do_unlet((char_u
*)"g:actual_curbuf", TRUE
);
3664 if (str
!= NULL
&& *str
!= 0)
3666 if (*skipdigits(str
) == NUL
)
3668 num
= atoi((char *)str
);
3678 num
= (wp
->w_buffer
->b_ml
.ml_flags
& ML_EMPTY
)
3679 ? 0L : (long)(wp
->w_cursor
.lnum
);
3683 num
= wp
->w_buffer
->b_ml
.ml_line_count
;
3687 num
= !(State
& INSERT
) && empty_line
3688 ? 0 : (int)wp
->w_cursor
.col
+ 1;
3692 case STL_VIRTCOL_ALT
:
3693 /* In list mode virtcol needs to be recomputed */
3694 virtcol
= wp
->w_virtcol
;
3695 if (wp
->w_p_list
&& lcs_tab1
== NUL
)
3697 wp
->w_p_list
= FALSE
;
3698 getvcol(wp
, &wp
->w_cursor
, NULL
, &virtcol
, NULL
);
3699 wp
->w_p_list
= TRUE
;
3702 /* Don't display %V if it's the same as %c. */
3703 if (opt
== STL_VIRTCOL_ALT
3704 && (virtcol
== (colnr_T
)(!(State
& INSERT
) && empty_line
3705 ? 0 : (int)wp
->w_cursor
.col
+ 1)))
3707 num
= (long)virtcol
;
3710 case STL_PERCENTAGE
:
3711 num
= (int)(((long)wp
->w_cursor
.lnum
* 100L) /
3712 (long)wp
->w_buffer
->b_ml
.ml_line_count
);
3715 case STL_ALTPERCENT
:
3717 get_rel_pos(wp
, str
);
3720 case STL_ARGLISTSTAT
:
3723 if (append_arg_number(wp
, tmp
, FALSE
, (int)sizeof(tmp
)))
3729 if (get_keymap_str(wp
, tmp
, TMPLEN
))
3733 #if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
3734 num
= printer_page_num
;
3741 num
= wp
->w_buffer
->b_fnum
;
3748 l
= ml_find_line_or_offset(wp
->w_buffer
, wp
->w_cursor
.lnum
, NULL
);
3749 num
= (wp
->w_buffer
->b_ml
.ml_flags
& ML_EMPTY
) || l
< 0 ?
3750 0L : l
+ 1 + (!(State
& INSERT
) && empty_line
?
3751 0 : (int)wp
->w_cursor
.col
);
3758 if (wp
->w_cursor
.col
> STRLEN(linecont
))
3763 num
= (*mb_ptr2char
)(linecont
+ wp
->w_cursor
.col
);
3765 num
= linecont
[wp
->w_cursor
.col
];
3770 else if (num
== CAR
&& get_fileformat(wp
->w_buffer
) == EOL_MAC
)
3775 case STL_ROFLAG_ALT
:
3777 if (wp
->w_buffer
->b_p_ro
)
3778 str
= (char_u
*)((opt
== STL_ROFLAG_ALT
) ? ",RO" : "[RO]");
3782 case STL_HELPFLAG_ALT
:
3784 if (wp
->w_buffer
->b_help
)
3785 str
= (char_u
*)((opt
== STL_HELPFLAG_ALT
) ? ",HLP"
3791 if (*wp
->w_buffer
->b_p_ft
!= NUL
3792 && STRLEN(wp
->w_buffer
->b_p_ft
) < TMPLEN
- 3)
3794 vim_snprintf((char *)tmp
, sizeof(tmp
), "[%s]",
3795 wp
->w_buffer
->b_p_ft
);
3800 case STL_FILETYPE_ALT
:
3802 if (*wp
->w_buffer
->b_p_ft
!= NUL
3803 && STRLEN(wp
->w_buffer
->b_p_ft
) < TMPLEN
- 2)
3805 vim_snprintf((char *)tmp
, sizeof(tmp
), ",%s",
3806 wp
->w_buffer
->b_p_ft
);
3807 for (t
= tmp
; *t
!= 0; t
++)
3808 *t
= TOUPPER_LOC(*t
);
3814 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3815 case STL_PREVIEWFLAG
:
3816 case STL_PREVIEWFLAG_ALT
:
3819 str
= (char_u
*)((opt
== STL_PREVIEWFLAG_ALT
) ? ",PRV"
3825 case STL_MODIFIED_ALT
:
3827 switch ((opt
== STL_MODIFIED_ALT
)
3828 + bufIsChanged(wp
->w_buffer
) * 2
3829 + (!wp
->w_buffer
->b_p_ma
) * 4)
3831 case 2: str
= (char_u
*)"[+]"; break;
3832 case 3: str
= (char_u
*)",+"; break;
3833 case 4: str
= (char_u
*)"[-]"; break;
3834 case 5: str
= (char_u
*)",-"; break;
3835 case 6: str
= (char_u
*)"[+-]"; break;
3836 case 7: str
= (char_u
*)",+-"; break;
3842 while (*s
!= '#' && *s
!= NUL
)
3846 item
[curitem
].type
= Highlight
;
3847 item
[curitem
].start
= p
;
3848 item
[curitem
].minwid
= -syn_namen2id(t
, (int)(s
- t
));
3855 item
[curitem
].start
= p
;
3856 item
[curitem
].type
= Normal
;
3857 if (str
!= NULL
&& *str
)
3863 && ((!prevchar_isitem
&& *t
== ',')
3864 || (prevchar_isflag
&& *t
== ' ')))
3866 prevchar_isflag
= TRUE
;
3870 prevchar_isitem
= TRUE
;
3878 t
+= (*mb_ptr2len
)(t
);
3882 l
-= byte2cells(*t
++);
3883 if (p
+ 1 >= out
+ outlen
)
3889 for (; l
< minwid
&& p
+ 1 < out
+ outlen
; l
++)
3891 /* Don't put a "-" in front of a digit. */
3892 if (l
+ 1 == minwid
&& fillchar
== '-' && VIM_ISDIGIT(*t
))
3901 while (*t
&& p
+ 1 < out
+ outlen
)
3904 /* Change a space by fillchar, unless fillchar is '-' and a
3906 if (fillable
&& p
[-1] == ' '
3907 && (!VIM_ISDIGIT(*t
) || fillchar
!= '-'))
3910 for (; l
< minwid
&& p
+ 1 < out
+ outlen
; l
++)
3915 int nbase
= (base
== 'D' ? 10 : (base
== 'O' ? 8 : 16));
3918 if (p
+ 20 >= out
+ outlen
)
3919 break; /* not sufficient space */
3920 prevchar_isitem
= TRUE
;
3922 if (opt
== STL_VIRTCOL_ALT
)
3931 *t
++ = nbase
== 16 ? base
: (nbase
== 8 ? 'o' : 'd');
3934 for (n
= num
, l
= 1; n
>= nbase
; n
/= nbase
)
3936 if (opt
== STL_VIRTCOL_ALT
)
3942 while (l
-- > maxwid
)
3948 vim_snprintf((char *)p
, outlen
- (p
- out
), (char *)nstr
,
3952 vim_snprintf((char *)p
, outlen
- (p
- out
), (char *)nstr
,
3957 item
[curitem
].type
= Empty
;
3959 if (opt
== STL_VIM_EXPR
)
3962 if (num
>= 0 || (!itemisflag
&& str
&& *str
))
3963 prevchar_isflag
= FALSE
; /* Item not NULL, but not a flag */
3974 width
= vim_strsize(out
);
3975 if (maxwidth
> 0 && width
> maxwidth
)
3977 /* Result is too long, must trunctate somewhere. */
3983 for ( ; l
< itemcnt
; l
++)
3984 if (item
[l
].type
== Trunc
)
3986 /* Truncate at %< item. */
3992 /* No %< item, truncate first item. */
3998 if (width
- vim_strsize(s
) >= maxwidth
)
4000 /* Truncation mark is beyond max length */
4008 width
+= ptr2cells(s
);
4009 if (width
>= maxwidth
)
4011 s
+= (*mb_ptr2len
)(s
);
4013 /* Fill up for half a double-wide character. */
4014 while (++width
< maxwidth
)
4019 s
= out
+ maxwidth
- 1;
4020 for (l
= 0; l
< itemcnt
; l
++)
4021 if (item
[l
].start
> s
)
4033 while (width
>= maxwidth
)
4035 width
-= ptr2cells(s
+ n
);
4036 n
+= (*mb_ptr2len
)(s
+ n
);
4041 n
= width
- maxwidth
+ 1;
4046 /* Fill up for half a double-wide character. */
4047 while (++width
< maxwidth
)
4054 --n
; /* count the '<' */
4055 for (; l
< itemcnt
; l
++)
4057 if (item
[l
].start
- n
>= s
)
4065 else if (width
< maxwidth
&& STRLEN(out
) + maxwidth
- width
+ 1 < outlen
)
4067 /* Apply STL_MIDDLE if any */
4068 for (l
= 0; l
< itemcnt
; l
++)
4069 if (item
[l
].type
== Middle
)
4073 p
= item
[l
].start
+ maxwidth
- width
;
4074 STRMOVE(p
, item
[l
].start
);
4075 for (s
= item
[l
].start
; s
< p
; s
++)
4077 for (l
++; l
< itemcnt
; l
++)
4078 item
[l
].start
+= maxwidth
- width
;
4083 /* Store the info about highlighting. */
4087 for (l
= 0; l
< itemcnt
; l
++)
4089 if (item
[l
].type
== Highlight
)
4091 sp
->start
= item
[l
].start
;
4092 sp
->userhl
= item
[l
].minwid
;
4100 /* Store the info about tab pages labels. */
4104 for (l
= 0; l
< itemcnt
; l
++)
4106 if (item
[l
].type
== TabPage
)
4108 sp
->start
= item
[l
].start
;
4109 sp
->userhl
= item
[l
].minwid
;
4119 #endif /* FEAT_STL_OPT */
4121 #if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4122 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
4124 * Get relative cursor position in window into "str[]", in the form 99%, using
4125 * "Top", "Bot" or "All" when appropriate.
4128 get_rel_pos(wp
, str
)
4132 long above
; /* number of lines above window */
4133 long below
; /* number of lines below window */
4135 above
= wp
->w_topline
- 1;
4137 above
+= diff_check_fill(wp
, wp
->w_topline
) - wp
->w_topfill
;
4139 below
= wp
->w_buffer
->b_ml
.ml_line_count
- wp
->w_botline
+ 1;
4141 STRCPY(str
, above
== 0 ? _("All") : _("Bot"));
4142 else if (above
<= 0)
4143 STRCPY(str
, _("Top"));
4145 sprintf((char *)str
, "%2d%%", above
> 1000000L
4146 ? (int)(above
/ ((above
+ below
) / 100L))
4147 : (int)(above
* 100L / (above
+ below
)));
4152 * Append (file 2 of 8) to 'buf', if editing more than one file.
4153 * Return TRUE if it was appended.
4156 append_arg_number(wp
, buf
, add_file
, maxlen
)
4159 int add_file
; /* Add "file" before the arg number */
4160 int maxlen
; /* maximum nr of chars in buf or zero*/
4164 if (ARGCOUNT
<= 1) /* nothing to do */
4167 p
= buf
+ STRLEN(buf
); /* go to the end of the buffer */
4168 if (maxlen
&& p
- buf
+ 35 >= maxlen
) /* getting too long */
4177 sprintf((char *)p
, wp
->w_arg_idx_invalid
? "(%d) of %d)"
4178 : "%d of %d)", wp
->w_arg_idx
+ 1, ARGCOUNT
);
4183 * If fname is not a full path, make it a full path.
4184 * Returns pointer to allocated memory (NULL for failure).
4191 * Force expanding the path always for Unix, because symbolic links may
4192 * mess up the full path name, even though it starts with a '/'.
4193 * Also expand when there is ".." in the file name, try to remove it,
4194 * because "c:/src/../README" is equal to "c:/README".
4195 * Similarly "c:/src//file" is equal to "c:/src/file".
4196 * For MS-Windows also expand names like "longna~1" to "longname".
4199 return FullName_save(fname
, TRUE
);
4201 if (!vim_isAbsName(fname
)
4202 || strstr((char *)fname
, "..") != NULL
4203 || strstr((char *)fname
, "//") != NULL
4204 # ifdef BACKSLASH_IN_FILENAME
4205 || strstr((char *)fname
, "\\\\") != NULL
4207 # if defined(MSWIN) || defined(DJGPP)
4208 || vim_strchr(fname
, '~') != NULL
4211 return FullName_save(fname
, FALSE
);
4213 fname
= vim_strsave(fname
);
4215 # ifdef USE_FNAME_CASE
4216 # ifdef USE_LONG_FNAME
4221 fname_case(fname
, 0); /* set correct case for file name */
4230 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4231 * "ffname" becomes a pointer to allocated memory (or NULL).
4235 fname_expand(buf
, ffname
, sfname
)
4240 if (*ffname
== NULL
) /* if no file name given, nothing to do */
4242 if (*sfname
== NULL
) /* if no short file name given, use ffname */
4244 *ffname
= fix_fname(*ffname
); /* expand to full path */
4246 #ifdef FEAT_SHORTCUT
4251 /* If the file name is a shortcut file, use the file it links to. */
4252 rfname
= mch_resolve_shortcut(*ffname
);
4264 * Get the file name for an argument list entry.
4272 /* Use the name from the associated buffer if it exists. */
4273 bp
= buflist_findnr(aep
->ae_fnum
);
4274 if (bp
== NULL
|| bp
->b_fname
== NULL
)
4275 return aep
->ae_fname
;
4279 #if defined(FEAT_WINDOWS) || defined(PROTO)
4281 * do_arg_all(): Open up to 'count' windows, one for each argument.
4284 do_arg_all(count
, forceit
, keep_tabs
)
4286 int forceit
; /* hide buffers in current windows */
4287 int keep_tabs
; /* keep current tabs, for ":tab drop file" */
4291 char_u
*opened
; /* array of flags for which args are open */
4292 int opened_len
; /* length of opened[] */
4293 int use_firstwin
= FALSE
; /* use first window for arglist */
4296 alist_T
*alist
; /* argument list to be used */
4299 int had_tab
= cmdmod
.tab
;
4300 win_T
*new_curwin
= NULL
;
4301 tabpage_T
*new_curtab
= NULL
;
4305 /* Don't give an error message. We don't want it when the ":all"
4306 * command is in the .vimrc. */
4311 opened_len
= ARGCOUNT
;
4312 opened
= alloc_clear((unsigned)opened_len
);
4317 need_mouse_correct
= TRUE
;
4321 * Try closing all windows that are not in the argument list.
4322 * Also close windows that are not full width;
4323 * When 'hidden' or "forceit" set the buffer becomes hidden.
4324 * Windows that have a changed buffer and can't be hidden won't be closed.
4325 * When the ":tab" modifier was used do this for all tab pages.
4328 goto_tabpage_tp(first_tabpage
);
4331 tpnext
= curtab
->tp_next
;
4332 for (wp
= firstwin
; wp
!= NULL
; wp
= wpnext
)
4334 wpnext
= wp
->w_next
;
4336 if (buf
->b_ffname
== NULL
4337 || buf
->b_nwindows
> 1
4338 #ifdef FEAT_VERTSPLIT
4339 || wp
->w_width
!= Columns
4345 /* check if the buffer in this window is in the arglist */
4346 for (i
= 0; i
< ARGCOUNT
; ++i
)
4348 if (ARGLIST
[i
].ae_fnum
== buf
->b_fnum
4349 || fullpathcmp(alist_name(&ARGLIST
[i
]),
4350 buf
->b_ffname
, TRUE
) & FPC_SAME
)
4358 new_curtab
= curtab
;
4361 if (wp
->w_alist
!= curwin
->w_alist
)
4363 /* Use the current argument list for all windows
4364 * containing a file from it. */
4365 alist_unlink(wp
->w_alist
);
4366 wp
->w_alist
= curwin
->w_alist
;
4367 ++wp
->w_alist
->al_refcount
;
4375 if (i
== ARGCOUNT
&& !keep_tabs
) /* close this window */
4377 if (P_HID(buf
) || forceit
|| buf
->b_nwindows
> 1
4378 || !bufIsChanged(buf
))
4380 /* If the buffer was changed, and we would like to hide it,
4381 * try autowriting. */
4382 if (!P_HID(buf
) && buf
->b_nwindows
<= 1
4383 && bufIsChanged(buf
))
4385 (void)autowrite(buf
, FALSE
);
4387 /* check if autocommands removed the window */
4388 if (!win_valid(wp
) || !buf_valid(buf
))
4390 wpnext
= firstwin
; /* start all over... */
4396 /* don't close last window */
4397 if (firstwin
== lastwin
&& first_tabpage
->tp_next
== NULL
)
4399 use_firstwin
= TRUE
;
4403 win_close(wp
, !P_HID(buf
) && !bufIsChanged(buf
));
4404 # ifdef FEAT_AUTOCMD
4405 /* check if autocommands removed the next window */
4406 if (!win_valid(wpnext
))
4407 wpnext
= firstwin
; /* start all over... */
4415 /* Without the ":tab" modifier only do the current tab page. */
4416 if (had_tab
== 0 || tpnext
== NULL
)
4419 # ifdef FEAT_AUTOCMD
4420 /* check if autocommands removed the next tab page */
4421 if (!valid_tabpage(tpnext
))
4422 tpnext
= first_tabpage
; /* start all over...*/
4424 goto_tabpage_tp(tpnext
);
4428 * Open a window for files in the argument list that don't have one.
4429 * ARGCOUNT may change while doing this, because of autocommands.
4431 if (count
> ARGCOUNT
|| count
<= 0)
4434 /* Autocommands may do anything to the argument list. Make sure it's not
4435 * freed while we are working here by "locking" it. We still have to
4436 * watch out for its size to be changed. */
4437 alist
= curwin
->w_alist
;
4438 ++alist
->al_refcount
;
4441 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4445 win_enter(lastwin
, FALSE
);
4447 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4448 * leaving an empty tab page when executed locally. */
4449 if (keep_tabs
&& bufempty() && curbuf
->b_nwindows
== 1
4450 && curbuf
->b_ffname
== NULL
&& !curbuf
->b_changed
)
4451 use_firstwin
= TRUE
;
4454 for (i
= 0; i
< count
&& i
< alist
->al_ga
.ga_len
&& !got_int
; ++i
)
4456 if (alist
== &global_alist
&& i
== global_alist
.al_ga
.ga_len
- 1)
4457 arg_had_last
= TRUE
;
4458 if (i
< opened_len
&& opened
[i
])
4460 /* Move the already present window to below the current window */
4461 if (curwin
->w_arg_idx
!= i
)
4463 for (wpnext
= firstwin
; wpnext
!= NULL
; wpnext
= wpnext
->w_next
)
4465 if (wpnext
->w_arg_idx
== i
)
4467 win_move_after(wpnext
, curwin
);
4473 else if (split_ret
== OK
)
4475 if (!use_firstwin
) /* split current window */
4478 p_ea
= TRUE
; /* use space from all windows */
4479 split_ret
= win_split(0, WSP_ROOM
| WSP_BELOW
);
4481 if (split_ret
== FAIL
)
4485 else /* first window: do autocmd for leaving this buffer */
4492 curwin
->w_arg_idx
= i
;
4495 new_curwin
= curwin
;
4496 new_curtab
= curtab
;
4498 (void)do_ecmd(0, alist_name(&AARGLIST(alist
)[i
]), NULL
, NULL
,
4500 ((P_HID(curwin
->w_buffer
)
4501 || bufIsChanged(curwin
->w_buffer
)) ? ECMD_HIDE
: 0)
4507 use_firstwin
= FALSE
;
4511 /* When ":tab" was used open a new tab for a new window repeatedly. */
4512 if (had_tab
> 0 && tabpage_index(NULL
) <= p_tpm
)
4516 /* Remove the "lock" on the argument list. */
4517 alist_unlink(alist
);
4522 /* to window with first arg */
4523 if (valid_tabpage(new_curtab
))
4524 goto_tabpage_tp(new_curtab
);
4525 if (win_valid(new_curwin
))
4526 win_enter(new_curwin
, FALSE
);
4534 # if defined(FEAT_LISTCMDS) || defined(PROTO)
4536 * Open a window for a number of buffers.
4548 int count
; /* Maximum number of windows to open. */
4549 int all
; /* When TRUE also load inactive buffers. */
4551 int had_tab
= cmdmod
.tab
;
4555 if (eap
->addr_count
== 0) /* make as many windows as possible */
4558 count
= eap
->line2
; /* make as many windows as specified */
4559 if (eap
->cmdidx
== CMD_unhide
|| eap
->cmdidx
== CMD_sunhide
)
4567 need_mouse_correct
= TRUE
;
4571 * Close superfluous windows (two windows for the same buffer).
4572 * Also close windows that are not full-width.
4576 goto_tabpage_tp(first_tabpage
);
4580 tpnext
= curtab
->tp_next
;
4581 for (wp
= firstwin
; wp
!= NULL
; wp
= wpnext
)
4583 wpnext
= wp
->w_next
;
4584 if ((wp
->w_buffer
->b_nwindows
> 1
4585 #ifdef FEAT_VERTSPLIT
4586 || ((cmdmod
.split
& WSP_VERT
)
4587 ? wp
->w_height
+ wp
->w_status_height
< Rows
- p_ch
4589 : wp
->w_width
!= Columns
)
4592 || (had_tab
> 0 && wp
!= firstwin
)
4594 ) && firstwin
!= lastwin
)
4596 win_close(wp
, FALSE
);
4598 wpnext
= firstwin
; /* just in case an autocommand does
4599 something strange with windows */
4600 tpnext
= first_tabpage
; /* start all over...*/
4609 /* Without the ":tab" modifier only do the current tab page. */
4610 if (had_tab
== 0 || tpnext
== NULL
)
4612 goto_tabpage_tp(tpnext
);
4617 * Go through the buffer list. When a buffer doesn't have a window yet,
4618 * open one. Otherwise move the window to the right position.
4619 * Watch out for autocommands that delete buffers or windows!
4622 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4625 win_enter(lastwin
, FALSE
);
4629 for (buf
= firstbuf
; buf
!= NULL
&& open_wins
< count
; buf
= buf
->b_next
)
4631 /* Check if this buffer needs a window */
4632 if ((!all
&& buf
->b_ml
.ml_mfp
== NULL
) || !buf
->b_p_bl
)
4638 /* With the ":tab" modifier don't move the window. */
4639 if (buf
->b_nwindows
> 0)
4640 wp
= lastwin
; /* buffer has a window, skip it */
4647 /* Check if this buffer already has a window */
4648 for (wp
= firstwin
; wp
!= NULL
; wp
= wp
->w_next
)
4649 if (wp
->w_buffer
== buf
)
4651 /* If the buffer already has a window, move it */
4653 win_move_after(wp
, curwin
);
4656 if (wp
== NULL
&& split_ret
== OK
)
4658 /* Split the window and put the buffer in it */
4660 p_ea
= TRUE
; /* use space from all windows */
4661 split_ret
= win_split(0, WSP_ROOM
| WSP_BELOW
);
4664 if (split_ret
== FAIL
)
4667 /* Open the buffer in this window. */
4668 #if defined(HAS_SWAP_EXISTS_ACTION)
4669 swap_exists_action
= SEA_DIALOG
;
4671 set_curbuf(buf
, DOBUF_GOTO
);
4673 if (!buf_valid(buf
)) /* autocommands deleted the buffer!!! */
4675 #if defined(HAS_SWAP_EXISTS_ACTION)
4676 swap_exists_action
= SEA_NONE
;
4681 #if defined(HAS_SWAP_EXISTS_ACTION)
4682 if (swap_exists_action
== SEA_QUIT
)
4684 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4687 /* Reset the error/interrupt/exception state here so that
4688 * aborting() returns FALSE when closing a window. */
4692 /* User selected Quit at ATTENTION prompt; close this window. */
4693 win_close(curwin
, TRUE
);
4695 swap_exists_action
= SEA_NONE
;
4696 swap_exists_did_quit
= TRUE
;
4698 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4699 /* Restore the error/interrupt/exception state if not
4700 * discarded by a new aborting error, interrupt, or uncaught
4706 handle_swap_exists(NULL
);
4713 (void)vgetc(); /* only break the file loading, not the rest */
4717 /* Autocommands deleted the buffer or aborted script processing!!! */
4722 /* When ":tab" was used open a new tab for a new window repeatedly. */
4723 if (had_tab
> 0 && tabpage_index(NULL
) <= p_tpm
)
4730 win_enter(firstwin
, FALSE
); /* back to first window */
4736 * Close superfluous windows.
4738 for (wp
= lastwin
; open_wins
> count
; )
4740 r
= (P_HID(wp
->w_buffer
) || !bufIsChanged(wp
->w_buffer
)
4741 || autowrite(wp
->w_buffer
, FALSE
) == OK
);
4745 /* BufWrite Autocommands made the window invalid, start over */
4752 win_close(wp
, !P_HID(wp
->w_buffer
));
4764 # endif /* FEAT_LISTCMDS */
4766 #endif /* FEAT_WINDOWS */
4768 static int chk_modeline
__ARGS((linenr_T
, int));
4771 * do_modelines() - process mode lines for the current file
4774 * OPT_WINONLY only set options local to window
4775 * OPT_NOWIN don't set options local to window
4777 * Returns immediately if the "ml" option isn't set.
4785 static int entered
= 0;
4787 if (!curbuf
->b_p_ml
|| (nmlines
= (int)p_mls
) == 0)
4790 /* Disallow recursive entry here. Can happen when executing a modeline
4791 * triggers an autocommand, which reloads modelines with a ":do". */
4796 for (lnum
= 1; lnum
<= curbuf
->b_ml
.ml_line_count
&& lnum
<= nmlines
;
4798 if (chk_modeline(lnum
, flags
) == FAIL
)
4801 for (lnum
= curbuf
->b_ml
.ml_line_count
; lnum
> 0 && lnum
> nmlines
4802 && lnum
> curbuf
->b_ml
.ml_line_count
- nmlines
; --lnum
)
4803 if (chk_modeline(lnum
, flags
) == FAIL
)
4808 #include "version.h" /* for version number */
4811 * chk_modeline() - check a single line for a mode string
4812 * Return FAIL if an error encountered.
4815 chk_modeline(lnum
, flags
)
4817 int flags
; /* Same as for do_modelines(). */
4821 char_u
*linecopy
; /* local copy of any modeline found */
4826 char_u
*save_sourcing_name
;
4827 linenr_T save_sourcing_lnum
;
4833 for (s
= ml_get(lnum
); *s
!= NUL
; ++s
)
4835 if (prev
== -1 || vim_isspace(prev
))
4837 if ((prev
!= -1 && STRNCMP(s
, "ex:", (size_t)3) == 0)
4838 || STRNCMP(s
, "vi:", (size_t)3) == 0)
4840 if (STRNCMP(s
, "vim", 3) == 0)
4842 if (s
[3] == '<' || s
[3] == '=' || s
[3] == '>')
4846 vers
= getdigits(&e
);
4849 || (VIM_VERSION_100
>= vers
&& isdigit(s
[3]))
4850 || (VIM_VERSION_100
< vers
&& s
[3] == '<')
4851 || (VIM_VERSION_100
> vers
&& s
[3] == '>')
4852 || (VIM_VERSION_100
== vers
&& s
[3] == '=')))
4861 do /* skip over "ex:", "vi:" or "vim:" */
4863 while (s
[-1] != ':');
4865 s
= linecopy
= vim_strsave(s
); /* copy the line, it will change */
4866 if (linecopy
== NULL
)
4869 save_sourcing_lnum
= sourcing_lnum
;
4870 save_sourcing_name
= sourcing_name
;
4871 sourcing_lnum
= lnum
; /* prepare for emsg() */
4872 sourcing_name
= (char_u
*)"modelines";
4875 while (end
== FALSE
)
4882 * Find end of set command: ':' or end of line.
4883 * Skip over "\:", replacing it with ":".
4885 for (e
= s
; *e
!= ':' && *e
!= NUL
; ++e
)
4886 if (e
[0] == '\\' && e
[1] == ':')
4892 * If there is a "set" command, require a terminating ':' and
4893 * ignore the stuff after the ':'.
4894 * "vi:set opt opt opt: foo" -- foo not interpreted
4895 * "vi:opt opt opt: foo" -- foo interpreted
4896 * Accept "se" for compatibility with Elvis.
4898 if (STRNCMP(s
, "set ", (size_t)4) == 0
4899 || STRNCMP(s
, "se ", (size_t)3) == 0)
4901 if (*e
!= ':') /* no terminating ':'? */
4904 s
= vim_strchr(s
, ' ') + 1;
4906 *e
= NUL
; /* truncate the set command */
4908 if (*s
!= NUL
) /* skip over an empty "::" */
4911 save_SID
= current_SID
;
4912 current_SID
= SID_MODELINE
;
4914 retval
= do_set(s
, OPT_MODELINE
| OPT_LOCAL
| flags
);
4916 current_SID
= save_SID
;
4918 if (retval
== FAIL
) /* stop if error found */
4921 s
= e
+ 1; /* advance to next part */
4924 sourcing_lnum
= save_sourcing_lnum
;
4925 sourcing_name
= save_sourcing_name
;
4932 #if defined(FEAT_VIMINFO) || defined(PROTO)
4934 read_viminfo_bufferlist(virp
, writing
)
4945 /* Handle long line and escaped characters. */
4946 xline
= viminfo_readstring(virp
, 1, FALSE
);
4948 /* don't read in if there are files on the command-line or if writing: */
4949 if (xline
!= NULL
&& !writing
&& ARGCOUNT
== 0
4950 && find_viminfo_parameter('%') != NULL
)
4952 /* Format is: <fname> Tab <lnum> Tab <col>.
4953 * Watch out for a Tab in the file name, work from the end. */
4956 tab
= vim_strrchr(xline
, '\t');
4960 col
= atoi((char *)tab
);
4961 tab
= vim_strrchr(xline
, '\t');
4965 lnum
= atol((char *)tab
);
4969 /* Expand "~/" in the file name at "line + 1" to a full path.
4970 * Then try shortening it by comparing with the current directory */
4971 expand_env(xline
, NameBuff
, MAXPATHL
);
4972 sfname
= shorten_fname1(NameBuff
);
4974 buf
= buflist_new(NameBuff
, sfname
, (linenr_T
)0, BLN_LISTED
);
4975 if (buf
!= NULL
) /* just in case... */
4977 buf
->b_last_cursor
.lnum
= lnum
;
4978 buf
->b_last_cursor
.col
= col
;
4979 buflist_setfpos(buf
, curwin
, lnum
, col
, FALSE
);
4984 return viminfo_readline(virp
);
4988 write_viminfo_bufferlist(fp
)
4999 if (find_viminfo_parameter('%') == NULL
)
5002 /* Without a number -1 is returned: do all buffers. */
5003 max_buffers
= get_viminfo_parameter('%');
5005 /* Allocate room for the file name, lnum and col. */
5006 line
= alloc(MAXPATHL
+ 40);
5011 FOR_ALL_TAB_WINDOWS(tp
, win
)
5012 set_last_cursor(win
);
5014 set_last_cursor(curwin
);
5017 fprintf(fp
, _("\n# Buffer list:\n"));
5018 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
5020 if (buf
->b_fname
== NULL
5022 #ifdef FEAT_QUICKFIX
5025 || removable(buf
->b_ffname
))
5028 if (max_buffers
-- == 0)
5031 home_replace(NULL
, buf
->b_ffname
, line
, MAXPATHL
, TRUE
);
5032 sprintf((char *)line
+ STRLEN(line
), "\t%ld\t%d",
5033 (long)buf
->b_last_cursor
.lnum
,
5034 buf
->b_last_cursor
.col
);
5035 viminfo_writestring(fp
, line
);
5043 * Return special buffer name.
5044 * Returns NULL when the buffer has a normal file name.
5050 #if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5051 if (bt_quickfix(buf
))
5057 * For location list window, w_llist_ref points to the location list.
5058 * For quickfix window, w_llist_ref is NULL.
5060 FOR_ALL_TAB_WINDOWS(tp
, win
)
5061 if (win
->w_buffer
== buf
)
5063 if (win
!= NULL
&& win
->w_llist_ref
!= NULL
)
5064 return _("[Location List]");
5066 return _("[Quickfix List]");
5069 #ifdef FEAT_QUICKFIX
5070 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5071 * contains the name as specified by the user */
5074 if (buf
->b_sfname
!= NULL
)
5075 return (char *)buf
->b_sfname
;
5079 if (buf
->b_fname
== NULL
)
5080 return _("[No Name]");
5085 #if defined(FEAT_SIGNS) || defined(PROTO)
5087 * Insert the sign into the signlist.
5090 insert_sign(buf
, prev
, next
, id
, lnum
, typenr
)
5091 buf_T
*buf
; /* buffer to store sign in */
5092 signlist_T
*prev
; /* previous sign entry */
5093 signlist_T
*next
; /* next sign entry */
5094 int id
; /* sign ID */
5095 linenr_T lnum
; /* line number which gets the mark */
5096 int typenr
; /* typenr of sign we are adding */
5098 signlist_T
*newsign
;
5100 newsign
= (signlist_T
*)lalloc((long_u
)sizeof(signlist_T
), FALSE
);
5101 if (newsign
!= NULL
)
5104 newsign
->lnum
= lnum
;
5105 newsign
->typenr
= typenr
;
5106 newsign
->next
= next
;
5107 #ifdef FEAT_NETBEANS_INTG
5108 newsign
->prev
= prev
;
5110 next
->prev
= newsign
;
5115 /* When adding first sign need to redraw the windows to create the
5116 * column for signs. */
5117 if (buf
->b_signlist
== NULL
)
5119 redraw_buf_later(buf
, NOT_VALID
);
5120 changed_cline_bef_curs();
5123 /* first sign in signlist */
5124 buf
->b_signlist
= newsign
;
5127 prev
->next
= newsign
;
5132 * Add the sign into the signlist. Find the right spot to do it though.
5135 buf_addsign(buf
, id
, lnum
, typenr
)
5136 buf_T
*buf
; /* buffer to store sign in */
5137 int id
; /* sign ID */
5138 linenr_T lnum
; /* line number which gets the mark */
5139 int typenr
; /* typenr of sign we are adding */
5141 signlist_T
*sign
; /* a sign in the signlist */
5142 signlist_T
*prev
; /* the previous sign */
5145 for (sign
= buf
->b_signlist
; sign
!= NULL
; sign
= sign
->next
)
5147 if (lnum
== sign
->lnum
&& id
== sign
->id
)
5149 sign
->typenr
= typenr
;
5153 #ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5158 #ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5159 /* XXX - GRP: Is this because of sign slide problem? Or is it
5160 * really needed? Or is it because we allow multiple signs per
5161 * line? If so, should I add that feature to FEAT_SIGNS?
5163 while (prev
!= NULL
&& prev
->lnum
== lnum
)
5166 sign
= buf
->b_signlist
;
5170 insert_sign(buf
, prev
, sign
, id
, lnum
, typenr
);
5175 #ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5176 /* XXX - GRP: See previous comment */
5177 while (prev
!= NULL
&& prev
->lnum
== lnum
)
5180 sign
= buf
->b_signlist
;
5184 insert_sign(buf
, prev
, sign
, id
, lnum
, typenr
);
5190 buf_change_sign_type(buf
, markId
, typenr
)
5191 buf_T
*buf
; /* buffer to store sign in */
5192 int markId
; /* sign ID */
5193 int typenr
; /* typenr of sign we are adding */
5195 signlist_T
*sign
; /* a sign in the signlist */
5197 for (sign
= buf
->b_signlist
; sign
!= NULL
; sign
= sign
->next
)
5199 if (sign
->id
== markId
)
5201 sign
->typenr
= typenr
;
5210 buf_getsigntype(buf
, lnum
, type
)
5213 int type
; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5215 signlist_T
*sign
; /* a sign in a b_signlist */
5217 for (sign
= buf
->b_signlist
; sign
!= NULL
; sign
= sign
->next
)
5218 if (sign
->lnum
== lnum
5219 && (type
== SIGN_ANY
5220 # ifdef FEAT_SIGN_ICONS
5221 || (type
== SIGN_ICON
5222 && sign_get_image(sign
->typenr
) != NULL
)
5224 || (type
== SIGN_TEXT
5225 && sign_get_text(sign
->typenr
) != NULL
)
5226 || (type
== SIGN_LINEHL
5227 && sign_get_attr(sign
->typenr
, TRUE
) != 0)))
5228 return sign
->typenr
;
5234 buf_delsign(buf
, id
)
5235 buf_T
*buf
; /* buffer sign is stored in */
5236 int id
; /* sign id */
5238 signlist_T
**lastp
; /* pointer to pointer to current sign */
5239 signlist_T
*sign
; /* a sign in a b_signlist */
5240 signlist_T
*next
; /* the next sign in a b_signlist */
5241 linenr_T lnum
; /* line number whose sign was deleted */
5243 lastp
= &buf
->b_signlist
;
5245 for (sign
= buf
->b_signlist
; sign
!= NULL
; sign
= next
)
5251 #ifdef FEAT_NETBEANS_INTG
5253 next
->prev
= sign
->prev
;
5260 lastp
= &sign
->next
;
5263 /* When deleted the last sign need to redraw the windows to remove the
5265 if (buf
->b_signlist
== NULL
)
5267 redraw_buf_later(buf
, NOT_VALID
);
5268 changed_cline_bef_curs();
5276 * Find the line number of the sign with the requested id. If the sign does
5277 * not exist, return 0 as the line number. This will still let the correct file
5281 buf_findsign(buf
, id
)
5282 buf_T
*buf
; /* buffer to store sign in */
5283 int id
; /* sign ID */
5285 signlist_T
*sign
; /* a sign in the signlist */
5287 for (sign
= buf
->b_signlist
; sign
!= NULL
; sign
= sign
->next
)
5295 buf_findsign_id(buf
, lnum
)
5296 buf_T
*buf
; /* buffer whose sign we are searching for */
5297 linenr_T lnum
; /* line number of sign */
5299 signlist_T
*sign
; /* a sign in the signlist */
5301 for (sign
= buf
->b_signlist
; sign
!= NULL
; sign
= sign
->next
)
5302 if (sign
->lnum
== lnum
)
5309 # if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5310 /* see if a given type of sign exists on a specific line */
5312 buf_findsigntype_id(buf
, lnum
, typenr
)
5313 buf_T
*buf
; /* buffer whose sign we are searching for */
5314 linenr_T lnum
; /* line number of sign */
5315 int typenr
; /* sign type number */
5317 signlist_T
*sign
; /* a sign in the signlist */
5319 for (sign
= buf
->b_signlist
; sign
!= NULL
; sign
= sign
->next
)
5320 if (sign
->lnum
== lnum
&& sign
->typenr
== typenr
)
5327 # if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5328 /* return the number of icons on the given line */
5330 buf_signcount(buf
, lnum
)
5334 signlist_T
*sign
; /* a sign in the signlist */
5337 for (sign
= buf
->b_signlist
; sign
!= NULL
; sign
= sign
->next
)
5338 if (sign
->lnum
== lnum
)
5339 if (sign_get_image(sign
->typenr
) != NULL
)
5344 # endif /* FEAT_SIGN_ICONS */
5345 # endif /* FEAT_NETBEANS_INTG */
5349 * Delete signs in buffer "buf".
5352 buf_delete_signs(buf
)
5357 while (buf
->b_signlist
!= NULL
)
5359 next
= buf
->b_signlist
->next
;
5360 vim_free(buf
->b_signlist
);
5361 buf
->b_signlist
= next
;
5366 * Delete all signs in all buffers.
5369 buf_delete_all_signs()
5371 buf_T
*buf
; /* buffer we are checking for signs */
5373 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
5374 if (buf
->b_signlist
!= NULL
)
5376 /* Need to redraw the windows to remove the sign column. */
5377 redraw_buf_later(buf
, NOT_VALID
);
5378 buf_delete_signs(buf
);
5383 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5386 sign_list_placed(rbuf
)
5393 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5401 if (buf
->b_signlist
!= NULL
)
5403 vim_snprintf(lbuf
, BUFSIZ
, _("Signs for %s:"), buf
->b_fname
);
5404 MSG_PUTS_ATTR(lbuf
, hl_attr(HLF_D
));
5407 for (p
= buf
->b_signlist
; p
!= NULL
; p
= p
->next
)
5409 vim_snprintf(lbuf
, BUFSIZ
, _(" line=%ld id=%d name=%s"),
5410 (long)p
->lnum
, p
->id
, sign_typenr2name(p
->typenr
));
5421 * Adjust a placed sign for inserted/deleted lines.
5424 sign_mark_adjust(line1
, line2
, amount
, amount_after
)
5430 signlist_T
*sign
; /* a sign in a b_signlist */
5432 for (sign
= curbuf
->b_signlist
; sign
!= NULL
; sign
= sign
->next
)
5434 if (sign
->lnum
>= line1
&& sign
->lnum
<= line2
)
5436 if (amount
== MAXLNUM
)
5439 sign
->lnum
+= amount
;
5441 else if (sign
->lnum
> line2
)
5442 sign
->lnum
+= amount_after
;
5445 #endif /* FEAT_SIGNS */
5448 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5454 if (on
!= curbuf
->b_p_bl
)
5456 curbuf
->b_p_bl
= on
;
5459 apply_autocmds(EVENT_BUFADD
, NULL
, NULL
, FALSE
, curbuf
);
5461 apply_autocmds(EVENT_BUFDELETE
, NULL
, NULL
, FALSE
, curbuf
);
5467 * Read the file for "buf" again and check if the contents changed.
5468 * Return TRUE if it changed or this could not be checked.
5471 buf_contents_changed(buf
)
5480 /* Allocate a buffer without putting it in the buffer list. */
5481 newbuf
= buflist_new(NULL
, NULL
, (linenr_T
)1, BLN_DUMMY
);
5485 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5486 if (prep_exarg(&ea
, buf
) == FAIL
)
5488 wipe_buffer(newbuf
, FALSE
);
5492 /* set curwin/curbuf to buf and save a few things */
5493 aucmd_prepbuf(&aco
, newbuf
);
5495 if (ml_open(curbuf
) == OK
5496 && readfile(buf
->b_ffname
, buf
->b_fname
,
5497 (linenr_T
)0, (linenr_T
)0, (linenr_T
)MAXLNUM
,
5498 &ea
, READ_NEW
| READ_DUMMY
) == OK
)
5500 /* compare the two files line by line */
5501 if (buf
->b_ml
.ml_line_count
== curbuf
->b_ml
.ml_line_count
)
5504 for (lnum
= 1; lnum
<= curbuf
->b_ml
.ml_line_count
; ++lnum
)
5505 if (STRCMP(ml_get_buf(buf
, lnum
, FALSE
), ml_get(lnum
)) != 0)
5514 /* restore curwin/curbuf and a few other things */
5515 aucmd_restbuf(&aco
);
5517 if (curbuf
!= newbuf
) /* safety check */
5518 wipe_buffer(newbuf
, FALSE
);
5524 * Wipe out a buffer and decrement the last buffer number if it was used for
5525 * this buffer. Call this to wipe out a temp buffer that does not contain any
5530 wipe_buffer(buf
, aucmd
)
5532 int aucmd
; /* When TRUE trigger autocommands. */
5534 if (buf
->b_fnum
== top_file_num
- 1)
5538 if (!aucmd
) /* Don't trigger BufDelete autocommands here. */
5541 close_buffer(NULL
, buf
, DOBUF_WIPE
);