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
, int skip_diff_buffer
));
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
);
263 /* specified ff and enc, bin in modelines */
264 if (file_ff_differs(curbuf
))
269 /* restore ++ff and ++enc, ++bin if specified */
273 char_u
*fenc
= enc_canonize(eap
->cmd
+ eap
->force_enc
);
275 vim_free(curbuf
->b_p_fenc
);
276 curbuf
->b_p_fenc
= fenc
;
281 set_fileformat(eap
->force_ff
, OPT_LOCAL
);
283 curbuf
->b_p_bin
= eap
->force_bin
;
285 buf_reload(curbuf
, curbuf
->b_orig_mode
);
287 curbuf
->b_flags
&= ~(BF_CHECK_RO
| BF_NEVERLOADED
);
291 apply_autocmds_retval(EVENT_BUFWINENTER
, NULL
, NULL
, FALSE
, curbuf
,
294 apply_autocmds(EVENT_BUFWINENTER
, NULL
, NULL
, FALSE
, curbuf
);
297 /* restore curwin/curbuf and a few other things */
307 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
315 for (bp
= firstbuf
; bp
!= NULL
; bp
= bp
->b_next
)
322 * Close the link to a buffer.
323 * "action" is used when there is no longer a window for the buffer.
325 * 0 buffer becomes hidden
326 * DOBUF_UNLOAD buffer is unloaded
327 * DOBUF_DELETE buffer is unloaded and removed from buffer list
328 * DOBUF_WIPE buffer is unloaded and really deleted
329 * When doing all but the first one on the current buffer, the caller should
330 * get a new buffer very soon!
332 * The 'bufhidden' option can force freeing and deleting.
335 close_buffer(win
, buf
, action
)
336 win_T
*win
; /* if not NULL, set b_last_cursor */
342 int nwindows
= buf
->b_nwindows
;
344 int unload_buf
= (action
!= 0);
345 int del_buf
= (action
== DOBUF_DEL
|| action
== DOBUF_WIPE
);
346 int wipe_buf
= (action
== DOBUF_WIPE
);
350 * Force unloading or deleting when 'bufhidden' says so.
351 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
352 * "hide" (otherwise we could never free or delete a buffer).
354 if (buf
->b_p_bh
[0] == 'd') /* 'bufhidden' == "delete" */
359 else if (buf
->b_p_bh
[0] == 'w') /* 'bufhidden' == "wipe" */
365 else if (buf
->b_p_bh
[0] == 'u') /* 'bufhidden' == "unload" */
371 /* Set b_last_cursor when closing the last window for the buffer.
372 * Remember the last cursor position and window options of the buffer.
373 * This used to be only for the current window, but then options like
374 * 'foldmethod' may be lost with a ":only" command. */
375 if (buf
->b_nwindows
== 1)
376 set_last_cursor(win
);
377 buflist_setfpos(buf
, win
,
378 win
->w_cursor
.lnum
== 1 ? 0 : win
->w_cursor
.lnum
,
379 win
->w_cursor
.col
, TRUE
);
383 /* When the buffer is no longer in a window, trigger BufWinLeave */
384 if (buf
->b_nwindows
== 1)
386 apply_autocmds(EVENT_BUFWINLEAVE
, buf
->b_fname
, buf
->b_fname
,
388 if (!buf_valid(buf
)) /* autocommands may delete the buffer */
391 /* When the buffer becomes hidden, but is not unloaded, trigger
395 apply_autocmds(EVENT_BUFHIDDEN
, buf
->b_fname
, buf
->b_fname
,
397 if (!buf_valid(buf
)) /* autocmds may delete the buffer */
401 if (aborting()) /* autocmds may abort script processing */
405 nwindows
= buf
->b_nwindows
;
408 /* decrease the link count from windows (unless not in any window) */
409 if (buf
->b_nwindows
> 0)
412 /* Return when a window is displaying the buffer or when it's not
414 if (buf
->b_nwindows
> 0 || !unload_buf
)
416 #if 0 /* why was this here? */
418 u_sync(); /* sync undo before going to another buffer */
423 /* Always remove the buffer when there is no file name. */
424 if (buf
->b_ffname
== NULL
)
428 * Free all things allocated for this buffer.
429 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
432 /* Remember if we are closing the current buffer. Restore the number of
433 * windows, so that autocommands in buf_freeall() don't get confused. */
434 is_curbuf
= (buf
== curbuf
);
435 buf
->b_nwindows
= nwindows
;
438 buf_freeall(buf
, del_buf
, wipe_buf
);
441 /* Autocommands may have deleted the buffer. */
445 if (aborting()) /* autocmds may abort script processing */
449 /* Autocommands may have opened or closed windows for this buffer.
450 * Decrement the count for the close we do here. */
451 if (buf
->b_nwindows
> 0)
455 * It's possible that autocommands change curbuf to the one being deleted.
456 * This might cause the previous curbuf to be deleted unexpectedly. But
457 * in some cases it's OK to delete the curbuf, because a new one is
458 * obtained anyway. Therefore only return if curbuf changed to the
461 if (buf
== curbuf
&& !is_curbuf
)
465 #ifdef FEAT_ODB_EDITOR
466 odb_buffer_close(buf
);
469 /* Change directories when the 'acd' option is set. */
473 * Remove the buffer from the list.
477 #ifdef FEAT_SUN_WORKSHOP
478 if (usingSunWorkShop
)
479 workshop_file_closed_lineno((char *)buf
->b_ffname
,
480 (int)buf
->b_last_cursor
.lnum
);
482 vim_free(buf
->b_ffname
);
483 vim_free(buf
->b_sfname
);
484 if (buf
->b_prev
== NULL
)
485 firstbuf
= buf
->b_next
;
487 buf
->b_prev
->b_next
= buf
->b_next
;
488 if (buf
->b_next
== NULL
)
489 lastbuf
= buf
->b_prev
;
491 buf
->b_next
->b_prev
= buf
->b_prev
;
498 /* Free all internal variables and reset option values, to make
499 * ":bdel" compatible with Vim 5.7. */
500 free_buffer_stuff(buf
, TRUE
);
502 /* Make it look like a new buffer. */
503 buf
->b_flags
= BF_CHECK_RO
| BF_NEVERLOADED
;
505 /* Init the options when loaded again. */
506 buf
->b_p_initialized
= FALSE
;
515 * Make buffer not contain a file.
521 buf
->b_ml
.ml_line_count
= 1;
522 unchanged(buf
, TRUE
);
524 buf
->b_shortname
= FALSE
;
527 buf
->b_start_eol
= TRUE
;
529 buf
->b_p_bomb
= FALSE
;
530 buf
->b_start_bomb
= FALSE
;
532 buf
->b_ml
.ml_mfp
= NULL
;
533 buf
->b_ml
.ml_flags
= ML_EMPTY
; /* empty buffer */
534 #ifdef FEAT_NETBEANS_INTG
535 netbeans_deleted_all_lines(buf
);
540 * buf_freeall() - free all things allocated for a buffer that are related to
545 buf_freeall(buf
, del_buf
, wipe_buf
)
547 int del_buf
; /* buffer is going to be deleted */
548 int wipe_buf
; /* buffer is going to be wiped out */
551 int is_curbuf
= (buf
== curbuf
);
553 apply_autocmds(EVENT_BUFUNLOAD
, buf
->b_fname
, buf
->b_fname
, FALSE
, buf
);
554 if (!buf_valid(buf
)) /* autocommands may delete the buffer */
556 if (del_buf
&& buf
->b_p_bl
)
558 apply_autocmds(EVENT_BUFDELETE
, buf
->b_fname
, buf
->b_fname
, FALSE
, buf
);
559 if (!buf_valid(buf
)) /* autocommands may delete the buffer */
564 apply_autocmds(EVENT_BUFWIPEOUT
, buf
->b_fname
, buf
->b_fname
,
566 if (!buf_valid(buf
)) /* autocommands may delete the buffer */
570 if (aborting()) /* autocmds may abort script processing */
575 * It's possible that autocommands change curbuf to the one being deleted.
576 * This might cause curbuf to be deleted unexpectedly. But in some cases
577 * it's OK to delete the curbuf, because a new one is obtained anyway.
578 * Therefore only return if curbuf changed to the deleted buffer.
580 if (buf
== curbuf
&& !is_curbuf
)
584 diff_buf_delete(buf
); /* Can't use 'diff' for unloaded buffer. */
588 /* No folds in an empty buffer. */
594 FOR_ALL_TAB_WINDOWS(tp
, win
)
595 if (win
->w_buffer
== buf
)
599 if (curwin
->w_buffer
== buf
)
600 clearFolding(curwin
);
605 tcl_buffer_free(buf
);
607 u_blockfree(buf
); /* free the memory allocated for undo */
608 ml_close(buf
, TRUE
); /* close and delete the memline/memfile */
609 buf
->b_ml
.ml_line_count
= 0; /* no lines in buffer */
610 u_clearall(buf
); /* reset all undo information */
612 syntax_clear(buf
); /* reset syntax info */
614 buf
->b_flags
&= ~BF_READERR
; /* a read error is no longer relevant */
618 * Free a buffer structure and the things it contains related to the buffer
619 * itself (not the file, that must have been done already).
625 free_buffer_stuff(buf
, TRUE
);
627 mzscheme_buffer_free(buf
);
633 python_buffer_free(buf
);
636 ruby_buffer_free(buf
);
639 aubuflocal_remove(buf
);
645 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
648 free_buffer_stuff(buf
, free_options
)
650 int free_options
; /* free options as well */
654 clear_wininfo(buf
); /* including window-local options */
655 free_buf_options(buf
, TRUE
);
658 vars_clear(&buf
->b_vars
.dv_hashtab
); /* free all internal variables */
659 hash_init(&buf
->b_vars
.dv_hashtab
);
662 uc_clear(&buf
->b_ucmds
); /* clear local user commands */
665 buf_delete_signs(buf
); /* delete any signs */
667 #ifdef FEAT_NETBEANS_INTG
669 netbeans_file_killed(buf
);
672 map_clear_int(buf
, MAP_ALL_MODES
, TRUE
, FALSE
); /* clear local mappings */
673 map_clear_int(buf
, MAP_ALL_MODES
, TRUE
, TRUE
); /* clear local abbrevs */
676 vim_free(buf
->b_start_fenc
);
677 buf
->b_start_fenc
= NULL
;
680 ga_clear(&buf
->b_langp
);
685 * Free the b_wininfo list for buffer "buf".
693 while (buf
->b_wininfo
!= NULL
)
695 wip
= buf
->b_wininfo
;
696 buf
->b_wininfo
= wip
->wi_next
;
699 clear_winopt(&wip
->wi_opt
);
701 deleteFoldRecurse(&wip
->wi_folds
);
708 #if defined(FEAT_LISTCMDS) || defined(PROTO)
710 * Go to another buffer. Handles the result of the ATTENTION dialog.
713 goto_buffer(eap
, start
, dir
, count
)
719 # if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
720 buf_T
*old_curbuf
= curbuf
;
722 swap_exists_action
= SEA_DIALOG
;
724 (void)do_buffer(*eap
->cmd
== 's' ? DOBUF_SPLIT
: DOBUF_GOTO
,
725 start
, dir
, count
, eap
->forceit
);
726 # if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
727 if (swap_exists_action
== SEA_QUIT
&& *eap
->cmd
== 's')
729 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
732 /* Reset the error/interrupt/exception state here so that
733 * aborting() returns FALSE when closing a window. */
737 /* Quitting means closing the split window, nothing else. */
738 win_close(curwin
, TRUE
);
739 swap_exists_action
= SEA_NONE
;
740 swap_exists_did_quit
= TRUE
;
742 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
743 /* Restore the error/interrupt/exception state if not discarded by a
744 * new aborting error, interrupt, or uncaught exception. */
749 handle_swap_exists(old_curbuf
);
754 #if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
756 * Handle the situation of swap_exists_action being set.
757 * It is allowed for "old_curbuf" to be NULL or invalid.
760 handle_swap_exists(old_curbuf
)
763 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
767 if (swap_exists_action
== SEA_QUIT
)
769 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
770 /* Reset the error/interrupt/exception state here so that
771 * aborting() returns FALSE when closing a buffer. */
775 /* User selected Quit at ATTENTION prompt. Go back to previous
776 * buffer. If that buffer is gone or the same as the current one,
777 * open a new, empty buffer. */
778 swap_exists_action
= SEA_NONE
; /* don't want it again */
779 swap_exists_did_quit
= TRUE
;
780 close_buffer(curwin
, curbuf
, DOBUF_UNLOAD
);
781 if (!buf_valid(old_curbuf
) || old_curbuf
== curbuf
)
782 old_curbuf
= buflist_new(NULL
, NULL
, 1L, BLN_CURBUF
| BLN_LISTED
);
783 if (old_curbuf
!= NULL
)
784 enter_buffer(old_curbuf
);
785 /* If "old_curbuf" is NULL we are in big trouble here... */
787 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
788 /* Restore the error/interrupt/exception state if not discarded by a
789 * new aborting error, interrupt, or uncaught exception. */
793 else if (swap_exists_action
== SEA_RECOVER
)
795 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
796 /* Reset the error/interrupt/exception state here so that
797 * aborting() returns FALSE when closing a buffer. */
801 /* User selected Recover at ATTENTION prompt. */
804 MSG_PUTS("\n"); /* don't overwrite the last message */
805 cmdline_row
= msg_row
;
808 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
809 /* Restore the error/interrupt/exception state if not discarded by a
810 * new aborting error, interrupt, or uncaught exception. */
814 swap_exists_action
= SEA_NONE
;
818 #if defined(FEAT_LISTCMDS) || defined(PROTO)
820 * do_bufdel() - delete or unload buffer(s)
822 * addr_count == 0: ":bdel" - delete current buffer
823 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
824 * buffer "end_bnr", then any other arguments.
825 * addr_count == 2: ":N,N bdel" - delete buffers in range
827 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
828 * DOBUF_DEL (":bdel")
830 * Returns error message or NULL
833 do_bufdel(command
, arg
, addr_count
, start_bnr
, end_bnr
, forceit
)
835 char_u
*arg
; /* pointer to extra arguments */
837 int start_bnr
; /* first buffer number in a range */
838 int end_bnr
; /* buffer nr or last buffer nr in a range */
841 int do_current
= 0; /* delete current buffer? */
842 int deleted
= 0; /* number of buffers deleted */
843 char_u
*errormsg
= NULL
; /* return value */
844 int bnr
; /* buffer number */
849 (void)do_buffer(command
, DOBUF_CURRENT
, FORWARD
, 0, forceit
);
855 if (*arg
) /* both range and argument is not allowed */
856 return (char_u
*)_(e_trailing
);
859 else /* addr_count == 1 */
862 for ( ;!got_int
; ui_breakcheck())
865 * delete the current buffer last, otherwise when the
866 * current buffer is deleted, the next buffer becomes
867 * the current one and will be loaded, which may then
868 * also be deleted, etc.
870 if (bnr
== curbuf
->b_fnum
)
872 else if (do_buffer(command
, DOBUF_FIRST
, FORWARD
, (int)bnr
,
877 * find next buffer number to delete/unload
884 else /* addr_count == 1 */
886 arg
= skipwhite(arg
);
889 if (!VIM_ISDIGIT(*arg
))
891 p
= skiptowhite_esc(arg
);
892 bnr
= buflist_findpat(arg
, p
, command
== DOBUF_WIPE
, FALSE
);
893 if (bnr
< 0) /* failed */
898 bnr
= getdigits(&arg
);
901 if (!got_int
&& do_current
&& do_buffer(command
, DOBUF_FIRST
,
902 FORWARD
, do_current
, forceit
) == OK
)
907 if (command
== DOBUF_UNLOAD
)
908 STRCPY(IObuff
, _("E515: No buffers were unloaded"));
909 else if (command
== DOBUF_DEL
)
910 STRCPY(IObuff
, _("E516: No buffers were deleted"));
912 STRCPY(IObuff
, _("E517: No buffers were wiped out"));
915 else if (deleted
>= p_report
)
917 if (command
== DOBUF_UNLOAD
)
920 MSG(_("1 buffer unloaded"));
922 smsg((char_u
*)_("%d buffers unloaded"), deleted
);
924 else if (command
== DOBUF_DEL
)
927 MSG(_("1 buffer deleted"));
929 smsg((char_u
*)_("%d buffers deleted"), deleted
);
934 MSG(_("1 buffer wiped out"));
936 smsg((char_u
*)_("%d buffers wiped out"), deleted
);
946 * Implementation of the commands for the buffer list.
948 * action == DOBUF_GOTO go to specified buffer
949 * action == DOBUF_SPLIT split window and go to specified buffer
950 * action == DOBUF_UNLOAD unload specified buffer(s)
951 * action == DOBUF_DEL delete specified buffer(s) from buffer list
952 * action == DOBUF_WIPE delete specified buffer(s) really
954 * start == DOBUF_CURRENT go to "count" buffer from current buffer
955 * start == DOBUF_FIRST go to "count" buffer from first buffer
956 * start == DOBUF_LAST go to "count" buffer from last buffer
957 * start == DOBUF_MOD go to "count" modified buffer from current buffer
962 do_buffer(action
, start
, dir
, count
, forceit
)
965 int dir
; /* FORWARD or BACKWARD */
966 int count
; /* buffer number or number of buffers */
967 int forceit
; /* TRUE for :...! */
971 int unload
= (action
== DOBUF_UNLOAD
|| action
== DOBUF_DEL
972 || action
== DOBUF_WIPE
);
976 case DOBUF_FIRST
: buf
= firstbuf
; break;
977 case DOBUF_LAST
: buf
= lastbuf
; break;
978 default: buf
= curbuf
; break;
980 if (start
== DOBUF_MOD
) /* find next modified buffer */
990 while (buf
!= curbuf
&& !bufIsChanged(buf
));
992 if (!bufIsChanged(buf
))
994 EMSG(_("E84: No modified buffer found"));
998 else if (start
== DOBUF_FIRST
&& count
) /* find specified buffer number */
1000 while (buf
!= NULL
&& buf
->b_fnum
!= count
)
1006 while (count
> 0 || (!unload
&& !buf
->b_p_bl
&& bp
!= buf
))
1008 /* remember the buffer where we start, we come back there when all
1009 * buffers are unlisted. */
1024 /* don't count unlisted buffers */
1025 if (unload
|| buf
->b_p_bl
)
1028 bp
= NULL
; /* use this buffer as new starting point */
1032 /* back where we started, didn't find anything. */
1033 EMSG(_("E85: There is no listed buffer"));
1039 if (buf
== NULL
) /* could not find it */
1041 if (start
== DOBUF_FIRST
)
1043 /* don't warn when deleting */
1045 EMSGN(_("E86: Buffer %ld does not exist"), count
);
1047 else if (dir
== FORWARD
)
1048 EMSG(_("E87: Cannot go beyond last buffer"));
1050 EMSG(_("E88: Cannot go before first buffer"));
1055 need_mouse_correct
= TRUE
;
1058 #ifdef FEAT_LISTCMDS
1060 * delete buffer buf from memory and/or the list
1067 /* When unloading or deleting a buffer that's already unloaded and
1068 * unlisted: fail silently. */
1069 if (action
!= DOBUF_WIPE
&& buf
->b_ml
.ml_mfp
== NULL
&& !buf
->b_p_bl
)
1072 if (!forceit
&& bufIsChanged(buf
))
1074 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1075 if ((p_confirm
|| cmdmod
.confirm
) && p_write
)
1077 dialog_changed(buf
, FALSE
);
1078 # ifdef FEAT_AUTOCMD
1079 if (!buf_valid(buf
))
1080 /* Autocommand deleted buffer, oops! It's not changed
1084 /* If it's still changed fail silently, the dialog already
1085 * mentioned why it fails. */
1086 if (bufIsChanged(buf
))
1092 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1099 * If deleting the last (listed) buffer, make it empty.
1100 * The last (listed) buffer cannot be unloaded.
1102 for (bp
= firstbuf
; bp
!= NULL
; bp
= bp
->b_next
)
1103 if (bp
->b_p_bl
&& bp
!= buf
)
1105 if (bp
== NULL
&& buf
== curbuf
)
1107 if (action
== DOBUF_UNLOAD
)
1109 EMSG(_("E90: Cannot unload last buffer"));
1113 /* Close any other windows on this buffer, then make it empty. */
1115 close_windows(buf
, TRUE
);
1118 retval
= do_ecmd(0, NULL
, NULL
, NULL
, ECMD_ONE
,
1119 forceit
? ECMD_FORCEIT
: 0, curwin
);
1122 * do_ecmd() may create a new buffer, then we have to delete
1123 * the old one. But do_ecmd() may have done that already, check
1124 * if the buffer still exists.
1126 if (buf
!= curbuf
&& buf_valid(buf
) && buf
->b_nwindows
== 0)
1127 close_buffer(NULL
, buf
, action
);
1133 * If the deleted buffer is the current one, close the current window
1134 * (unless it's the only window). Repeat this so long as we end up in
1135 * a window with this buffer.
1137 while (buf
== curbuf
1138 && (firstwin
!= lastwin
|| first_tabpage
->tp_next
!= NULL
))
1139 win_close(curwin
, FALSE
);
1143 * If the buffer to be deleted is not the current one, delete it here.
1148 close_windows(buf
, FALSE
);
1150 if (buf
!= curbuf
&& buf_valid(buf
) && buf
->b_nwindows
<= 0)
1151 close_buffer(NULL
, buf
, action
);
1156 * Deleting the current buffer: Need to find another buffer to go to.
1157 * There must be another, otherwise it would have been handled above.
1158 * First use au_new_curbuf, if it is valid.
1159 * Then prefer the buffer we most recently visited.
1160 * Else try to find one that is loaded, after the current buffer,
1161 * then before the current buffer.
1162 * Finally use any buffer.
1164 buf
= NULL
; /* selected buffer */
1165 bp
= NULL
; /* used when no loaded buffer found */
1167 if (au_new_curbuf
!= NULL
&& buf_valid(au_new_curbuf
))
1168 buf
= au_new_curbuf
;
1169 # ifdef FEAT_JUMPLIST
1173 #ifdef FEAT_JUMPLIST
1174 if (curwin
->w_jumplistlen
> 0)
1178 jumpidx
= curwin
->w_jumplistidx
- 1;
1180 jumpidx
= curwin
->w_jumplistlen
- 1;
1183 while (jumpidx
!= curwin
->w_jumplistidx
)
1185 buf
= buflist_findnr(curwin
->w_jumplist
[jumpidx
].fmark
.fnum
);
1188 if (buf
== curbuf
|| !buf
->b_p_bl
)
1189 buf
= NULL
; /* skip current and unlisted bufs */
1190 else if (buf
->b_ml
.ml_mfp
== NULL
)
1192 /* skip unloaded buf, but may keep it for later */
1198 if (buf
!= NULL
) /* found a valid buffer: stop searching */
1200 /* advance to older entry in jump list */
1201 if (!jumpidx
&& curwin
->w_jumplistidx
== curwin
->w_jumplistlen
)
1204 jumpidx
= curwin
->w_jumplistlen
- 1;
1205 if (jumpidx
== forward
) /* List exhausted for sure */
1211 if (buf
== NULL
) /* No previous buffer, Try 2'nd approach */
1214 buf
= curbuf
->b_next
;
1219 if (!forward
) /* tried both directions */
1221 buf
= curbuf
->b_prev
;
1225 /* in non-help buffer, try to skip help buffers, and vv */
1226 if (buf
->b_help
== curbuf
->b_help
&& buf
->b_p_bl
)
1228 if (buf
->b_ml
.ml_mfp
!= NULL
) /* found loaded buffer */
1230 if (bp
== NULL
) /* remember unloaded buf for later */
1239 if (buf
== NULL
) /* No loaded buffer, use unloaded one */
1241 if (buf
== NULL
) /* No loaded buffer, find listed one */
1243 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
1244 if (buf
->b_p_bl
&& buf
!= curbuf
)
1247 if (buf
== NULL
) /* Still no buffer, just take one */
1249 if (curbuf
->b_next
!= NULL
)
1250 buf
= curbuf
->b_next
;
1252 buf
= curbuf
->b_prev
;
1257 * make buf current buffer
1259 if (action
== DOBUF_SPLIT
) /* split window first */
1261 # ifdef FEAT_WINDOWS
1262 /* If 'switchbuf' contains "useopen": jump to first window containing
1263 * "buf" if one exists */
1264 if ((swb_flags
& SWB_USEOPEN
) && buf_jump_open_win(buf
))
1266 /* If 'switchbuf' contains "usetab": jump to first window in any tab
1267 * page containing "buf" if one exists */
1268 if ((swb_flags
& SWB_USETAB
) && buf_jump_open_tab(buf
))
1270 if (win_split(0, 0) == FAIL
)
1276 /* go to current buffer - nothing to do */
1281 * Check if the current buffer may be abandoned.
1283 if (action
== DOBUF_GOTO
&& !can_abandon(curbuf
, forceit
))
1285 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1286 if ((p_confirm
|| cmdmod
.confirm
) && p_write
)
1288 dialog_changed(curbuf
, FALSE
);
1289 # ifdef FEAT_AUTOCMD
1290 if (!buf_valid(buf
))
1291 /* Autocommand deleted buffer, oops! */
1295 if (bufIsChanged(curbuf
))
1298 EMSG(_(e_nowrtmsg
));
1303 /* Go to the other buffer. */
1304 set_curbuf(buf
, action
);
1306 #if defined(FEAT_LISTCMDS) && defined(FEAT_SCROLLBIND)
1307 if (action
== DOBUF_SPLIT
)
1308 curwin
->w_p_scb
= FALSE
; /* reset 'scrollbind' */
1311 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1312 if (aborting()) /* autocmds may abort script processing */
1319 #endif /* FEAT_LISTCMDS */
1322 * Set current buffer to "buf". Executes autocommands and closes current
1323 * buffer. "action" tells how to close the current buffer:
1324 * DOBUF_GOTO free or hide it
1325 * DOBUF_SPLIT nothing
1326 * DOBUF_UNLOAD unload it
1327 * DOBUF_DEL delete it
1328 * DOBUF_WIPE wipe it out
1331 set_curbuf(buf
, action
)
1336 int unload
= (action
== DOBUF_UNLOAD
|| action
== DOBUF_DEL
1337 || action
== DOBUF_WIPE
);
1340 if (!cmdmod
.keepalt
)
1341 curwin
->w_alt_fnum
= curbuf
->b_fnum
; /* remember alternate file */
1342 buflist_altfpos(curwin
); /* remember curpos */
1345 /* Don't restart Select mode after switching to another buffer. */
1346 VIsual_reselect
= FALSE
;
1349 /* close_windows() or apply_autocmds() may change curbuf */
1353 apply_autocmds(EVENT_BUFLEAVE
, NULL
, NULL
, FALSE
, curbuf
);
1355 if (buf_valid(prevbuf
) && !aborting())
1357 if (buf_valid(prevbuf
))
1363 close_windows(prevbuf
, FALSE
);
1365 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1366 if (buf_valid(prevbuf
) && !aborting())
1368 if (buf_valid(prevbuf
))
1371 if (prevbuf
== curbuf
)
1373 close_buffer(prevbuf
== curwin
->w_buffer
? curwin
: NULL
, prevbuf
,
1374 unload
? action
: (action
== DOBUF_GOTO
1376 && !bufIsChanged(prevbuf
)) ? DOBUF_UNLOAD
: 0);
1380 /* An autocommand may have deleted "buf", already entered it (e.g., when
1381 * it did ":bunload") or aborted the script processing! */
1383 if (buf_valid(buf
) && buf
!= curbuf
&& !aborting())
1385 if (buf_valid(buf
) && buf
!= curbuf
)
1392 * Enter a new current buffer.
1393 * Old curbuf must have been abandoned already!
1399 /* Copy buffer and window local option values. Not for a help buffer. */
1400 buf_copy_options(buf
, BCO_ENTER
| BCO_NOHELP
);
1405 /* Remove all folds in the window. */
1406 clearFolding(curwin
);
1407 foldUpdateAll(curwin
); /* update folds (later). */
1410 /* Get the buffer in the current window. */
1411 curwin
->w_buffer
= buf
;
1413 ++curbuf
->b_nwindows
;
1416 if (curwin
->w_p_diff
)
1417 diff_buf_add(curbuf
);
1420 /* Cursor on first line by default. */
1421 curwin
->w_cursor
.lnum
= 1;
1422 curwin
->w_cursor
.col
= 0;
1423 #ifdef FEAT_VIRTUALEDIT
1424 curwin
->w_cursor
.coladd
= 0;
1426 curwin
->w_set_curswant
= TRUE
;
1428 curwin
->w_topline_was_set
= FALSE
;
1431 /* Make sure the buffer is loaded. */
1432 if (curbuf
->b_ml
.ml_mfp
== NULL
) /* need to load the file */
1435 /* If there is no filetype, allow for detecting one. Esp. useful for
1436 * ":ball" used in a autocommand. If there already is a filetype we
1437 * might prefer to keep it. */
1438 if (*curbuf
->b_p_ft
== NUL
)
1439 did_filetype
= FALSE
;
1442 open_buffer(FALSE
, NULL
);
1447 need_fileinfo
= TRUE
; /* display file info after redraw */
1448 (void)buf_check_timestamp(curbuf
, FALSE
); /* check if file changed */
1450 curwin
->w_topline
= 1;
1452 curwin
->w_topfill
= 0;
1454 apply_autocmds(EVENT_BUFENTER
, NULL
, NULL
, FALSE
, curbuf
);
1455 apply_autocmds(EVENT_BUFWINENTER
, NULL
, NULL
, FALSE
, curbuf
);
1459 /* If autocommands did not change the cursor position, restore cursor lnum
1460 * and possibly cursor col. */
1461 if (curwin
->w_cursor
.lnum
== 1 && inindent(0))
1464 check_arg_idx(curwin
); /* check for valid arg_idx */
1469 /* when autocmds didn't change it */
1470 if (curwin
->w_topline
== 1 && !curwin
->w_topline_was_set
)
1472 scroll_cursor_halfway(FALSE
); /* redisplay at correct position */
1474 #ifdef FEAT_NETBEANS_INTG
1475 /* Send fileOpened event because we've changed buffers. */
1476 if (usingNetbeans
&& isNetbeansBuffer(curbuf
))
1477 netbeans_file_activated(curbuf
);
1480 /* Change directories when the 'acd' option is set. */
1484 if (curbuf
->b_kmap_state
& KEYMAP_INIT
)
1488 /* May need to set the spell language. Can only do this after the buffer
1489 * has been properly setup. */
1490 if (!curbuf
->b_help
&& curwin
->w_p_spell
&& *curbuf
->b_p_spl
!= NUL
)
1491 did_set_spelllang(curbuf
);
1494 redraw_later(NOT_VALID
);
1497 #if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1499 * Change to the directory of the current buffer.
1504 if (curbuf
->b_ffname
!= NULL
&& vim_chdirfile(curbuf
->b_ffname
) == OK
)
1505 shorten_fnames(TRUE
);
1510 * functions for dealing with the buffer list
1514 * Add a file name to the buffer list. Return a pointer to the buffer.
1515 * If the same file name already exists return a pointer to that buffer.
1516 * If it does not exist, or if fname == NULL, a new entry is created.
1517 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1518 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1519 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
1520 * This is the ONLY way to create a new buffer.
1522 static int top_file_num
= 1; /* highest file number */
1525 buflist_new(ffname
, sfname
, lnum
, flags
)
1526 char_u
*ffname
; /* full path of fname or relative */
1527 char_u
*sfname
; /* short fname or NULL */
1528 linenr_T lnum
; /* preferred cursor line */
1529 int flags
; /* BLN_ defines */
1536 fname_expand(curbuf
, &ffname
, &sfname
); /* will allocate ffname */
1539 * If file name already exists in the list, update the entry.
1542 /* On Unix we can use inode numbers when the file exists. Works better
1543 * for hard links. */
1544 if (sfname
== NULL
|| mch_stat((char *)sfname
, &st
) < 0)
1545 st
.st_dev
= (dev_T
)-1;
1547 if (ffname
!= NULL
&& !(flags
& BLN_DUMMY
) && (buf
=
1549 buflist_findname_stat(ffname
, &st
)
1551 buflist_findname(ffname
)
1557 buflist_setfpos(buf
, curwin
, lnum
, (colnr_T
)0, FALSE
);
1558 /* copy the options now, if 'cpo' doesn't have 's' and not done
1560 buf_copy_options(buf
, 0);
1561 if ((flags
& BLN_LISTED
) && !buf
->b_p_bl
)
1565 if (!(flags
& BLN_DUMMY
))
1566 apply_autocmds(EVENT_BUFADD
, NULL
, NULL
, FALSE
, buf
);
1573 * If the current buffer has no name and no contents, use the current
1574 * buffer. Otherwise: Need to allocate a new buffer structure.
1576 * This is the ONLY place where a new buffer structure is allocated!
1577 * (A spell file buffer is allocated in spell.c, but that's not a normal
1581 if ((flags
& BLN_CURBUF
)
1583 && curbuf
->b_ffname
== NULL
1584 && curbuf
->b_nwindows
<= 1
1585 && (curbuf
->b_ml
.ml_mfp
== NULL
|| bufempty()))
1589 /* It's like this buffer is deleted. Watch out for autocommands that
1590 * change curbuf! If that happens, allocate a new buffer anyway. */
1592 apply_autocmds(EVENT_BUFDELETE
, NULL
, NULL
, FALSE
, curbuf
);
1594 apply_autocmds(EVENT_BUFWIPEOUT
, NULL
, NULL
, FALSE
, curbuf
);
1596 if (aborting()) /* autocmds may abort script processing */
1600 #ifdef FEAT_QUICKFIX
1601 # ifdef FEAT_AUTOCMD
1605 /* Make sure 'bufhidden' and 'buftype' are empty */
1606 clear_string_option(&buf
->b_p_bh
);
1607 clear_string_option(&buf
->b_p_bt
);
1611 if (buf
!= curbuf
|| curbuf
== NULL
)
1613 buf
= (buf_T
*)alloc_clear((unsigned)sizeof(buf_T
));
1623 buf
->b_ffname
= ffname
;
1624 buf
->b_sfname
= vim_strsave(sfname
);
1628 buf
->b_wininfo
= (wininfo_T
*)alloc_clear((unsigned)sizeof(wininfo_T
));
1630 if ((ffname
!= NULL
&& (buf
->b_ffname
== NULL
|| buf
->b_sfname
== NULL
))
1631 || buf
->b_wininfo
== NULL
)
1633 vim_free(buf
->b_ffname
);
1634 buf
->b_ffname
= NULL
;
1635 vim_free(buf
->b_sfname
);
1636 buf
->b_sfname
= NULL
;
1644 /* free all things allocated for this buffer */
1645 buf_freeall(buf
, FALSE
, FALSE
);
1646 if (buf
!= curbuf
) /* autocommands deleted the buffer! */
1648 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1649 if (aborting()) /* autocmds may abort script processing */
1652 /* buf->b_nwindows = 0; why was this here? */
1653 free_buffer_stuff(buf
, FALSE
); /* delete local variables et al. */
1655 /* need to reload lmaps and set b:keymap_name */
1656 curbuf
->b_kmap_state
|= KEYMAP_INIT
;
1662 * put new buffer at the end of the buffer list
1665 if (firstbuf
== NULL
) /* buffer list is empty */
1670 else /* append new buffer at end of list */
1672 lastbuf
->b_next
= buf
;
1673 buf
->b_prev
= lastbuf
;
1677 buf
->b_fnum
= top_file_num
++;
1678 if (top_file_num
< 0) /* wrap around (may cause duplicates) */
1680 EMSG(_("W14: Warning: List of file names overflow"));
1681 if (emsg_silent
== 0)
1684 ui_delay(3000L, TRUE
); /* make sure it is noticed */
1690 * Always copy the options from the current buffer.
1692 buf_copy_options(buf
, BCO_ALWAYS
);
1695 buf
->b_wininfo
->wi_fpos
.lnum
= lnum
;
1696 buf
->b_wininfo
->wi_win
= curwin
;
1699 init_var_dict(&buf
->b_vars
, &buf
->b_bufvar
); /* init b: variables */
1702 hash_init(&buf
->b_keywtab
);
1703 hash_init(&buf
->b_keywtab_ic
);
1706 buf
->b_fname
= buf
->b_sfname
;
1708 if (st
.st_dev
== (dev_T
)-1)
1712 buf
->b_dev
= st
.st_dev
;
1713 buf
->b_ino
= st
.st_ino
;
1716 buf
->b_u_synced
= TRUE
;
1717 buf
->b_flags
= BF_CHECK_RO
| BF_NEVERLOADED
;
1718 if (flags
& BLN_DUMMY
)
1719 buf
->b_flags
|= BF_DUMMY
;
1720 buf_clear_file(buf
);
1721 clrallmarks(buf
); /* clear marks */
1722 fmarks_check_names(buf
); /* check file marks for this file */
1723 buf
->b_p_bl
= (flags
& BLN_LISTED
) ? TRUE
: FALSE
; /* init 'buflisted' */
1725 if (!(flags
& BLN_DUMMY
))
1727 apply_autocmds(EVENT_BUFNEW
, NULL
, NULL
, FALSE
, buf
);
1728 if (flags
& BLN_LISTED
)
1729 apply_autocmds(EVENT_BUFADD
, NULL
, NULL
, FALSE
, buf
);
1731 if (aborting()) /* autocmds may abort script processing */
1741 * Free the memory for the options of a buffer.
1742 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1746 free_buf_options(buf
, free_p_ff
)
1753 clear_string_option(&buf
->b_p_fenc
);
1755 clear_string_option(&buf
->b_p_ff
);
1756 #ifdef FEAT_QUICKFIX
1757 clear_string_option(&buf
->b_p_bh
);
1758 clear_string_option(&buf
->b_p_bt
);
1762 clear_string_option(&buf
->b_p_def
);
1763 clear_string_option(&buf
->b_p_inc
);
1765 clear_string_option(&buf
->b_p_inex
);
1768 #if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1769 clear_string_option(&buf
->b_p_inde
);
1770 clear_string_option(&buf
->b_p_indk
);
1772 #if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1773 clear_string_option(&buf
->b_p_bexpr
);
1775 #if defined(FEAT_EVAL)
1776 clear_string_option(&buf
->b_p_fex
);
1779 clear_string_option(&buf
->b_p_key
);
1781 clear_string_option(&buf
->b_p_kp
);
1782 clear_string_option(&buf
->b_p_mps
);
1783 clear_string_option(&buf
->b_p_fo
);
1784 clear_string_option(&buf
->b_p_flp
);
1785 clear_string_option(&buf
->b_p_isk
);
1787 clear_string_option(&buf
->b_p_keymap
);
1788 ga_clear(&buf
->b_kmap_ga
);
1790 #ifdef FEAT_COMMENTS
1791 clear_string_option(&buf
->b_p_com
);
1794 clear_string_option(&buf
->b_p_cms
);
1796 clear_string_option(&buf
->b_p_nf
);
1798 clear_string_option(&buf
->b_p_syn
);
1801 clear_string_option(&buf
->b_p_spc
);
1802 clear_string_option(&buf
->b_p_spf
);
1803 vim_free(buf
->b_cap_prog
);
1804 buf
->b_cap_prog
= NULL
;
1805 clear_string_option(&buf
->b_p_spl
);
1807 #ifdef FEAT_SEARCHPATH
1808 clear_string_option(&buf
->b_p_sua
);
1811 clear_string_option(&buf
->b_p_ft
);
1813 #ifdef FEAT_OSFILETYPE
1814 clear_string_option(&buf
->b_p_oft
);
1817 clear_string_option(&buf
->b_p_cink
);
1818 clear_string_option(&buf
->b_p_cino
);
1820 #if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1821 clear_string_option(&buf
->b_p_cinw
);
1823 #ifdef FEAT_INS_EXPAND
1824 clear_string_option(&buf
->b_p_cpt
);
1826 #ifdef FEAT_COMPL_FUNC
1827 clear_string_option(&buf
->b_p_cfu
);
1828 clear_string_option(&buf
->b_p_ofu
);
1830 #ifdef FEAT_QUICKFIX
1831 clear_string_option(&buf
->b_p_gp
);
1832 clear_string_option(&buf
->b_p_mp
);
1833 clear_string_option(&buf
->b_p_efm
);
1835 clear_string_option(&buf
->b_p_ep
);
1836 clear_string_option(&buf
->b_p_path
);
1837 clear_string_option(&buf
->b_p_tags
);
1838 #ifdef FEAT_INS_EXPAND
1839 clear_string_option(&buf
->b_p_dict
);
1840 clear_string_option(&buf
->b_p_tsr
);
1843 clear_string_option(&buf
->b_p_qe
);
1849 * get alternate file n
1850 * set linenr to lnum or altfpos.lnum if lnum == 0
1851 * also set cursor column to altfpos.col if 'startofline' is not set.
1852 * if (options & GETF_SETMARK) call setpcmark()
1853 * if (options & GETF_ALT) we are jumping to an alternate file.
1854 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1856 * return FAIL for failure, OK for success
1859 buflist_getfile(n
, lnum
, options
, forceit
)
1872 buf
= buflist_findnr(n
);
1875 if ((options
& GETF_ALT
) && n
== 0)
1878 EMSGN(_("E92: Buffer %ld not found"), n
);
1882 /* if alternate file is the current buffer, nothing to do */
1892 if (curbuf_locked())
1896 /* altfpos may be changed by getfile(), get it now */
1899 fpos
= buflist_findfpos(buf
);
1907 if (options
& GETF_SWITCH
)
1909 /* If 'switchbuf' contains "useopen": jump to first window containing
1910 * "buf" if one exists */
1911 if (swb_flags
& SWB_USEOPEN
)
1912 wp
= buf_jump_open_win(buf
);
1913 /* If 'switchbuf' contians "usetab": jump to first window in any tab
1914 * page containing "buf" if one exists */
1915 if (wp
== NULL
&& (swb_flags
& SWB_USETAB
))
1916 wp
= buf_jump_open_tab(buf
);
1917 /* If 'switchbuf' contains "split" or "newtab" and the current buffer
1918 * isn't empty: open new window */
1919 if (wp
== NULL
&& (swb_flags
& (SWB_SPLIT
| SWB_NEWTAB
)) && !bufempty())
1921 if (swb_flags
& SWB_NEWTAB
) /* Open in a new tab */
1923 else if (win_split(0, 0) == FAIL
) /* Open in a new window */
1925 # ifdef FEAT_SCROLLBIND
1926 curwin
->w_p_scb
= FALSE
;
1932 ++RedrawingDisabled
;
1933 if (getfile(buf
->b_fnum
, NULL
, NULL
, (options
& GETF_SETMARK
),
1934 lnum
, forceit
) <= 0)
1936 --RedrawingDisabled
;
1938 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
1939 if (!p_sol
&& col
!= 0)
1941 curwin
->w_cursor
.col
= col
;
1943 #ifdef FEAT_VIRTUALEDIT
1944 curwin
->w_cursor
.coladd
= 0;
1946 curwin
->w_set_curswant
= TRUE
;
1950 --RedrawingDisabled
;
1955 * go to the last know line number for the current buffer
1962 fpos
= buflist_findfpos(curbuf
);
1964 curwin
->w_cursor
.lnum
= fpos
->lnum
;
1965 check_cursor_lnum();
1968 curwin
->w_cursor
.col
= 0;
1971 curwin
->w_cursor
.col
= fpos
->col
;
1973 #ifdef FEAT_VIRTUALEDIT
1974 curwin
->w_cursor
.coladd
= 0;
1976 curwin
->w_set_curswant
= TRUE
;
1980 #if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
1982 * Find file in buffer list by name (it has to be for the current window).
1983 * Returns NULL if not found.
1986 buflist_findname_exp(fname
)
1992 /* First make the name into a full path name */
1993 ffname
= FullName_save(fname
,
1995 TRUE
/* force expansion, get rid of symbolic links */
2002 buf
= buflist_findname(ffname
);
2010 * Find file in buffer list by name (it has to be for the current window).
2011 * "ffname" must have a full path.
2012 * Skips dummy buffers.
2013 * Returns NULL if not found.
2016 buflist_findname(ffname
)
2022 if (mch_stat((char *)ffname
, &st
) < 0)
2023 st
.st_dev
= (dev_T
)-1;
2024 return buflist_findname_stat(ffname
, &st
);
2028 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2029 * twice for the same file.
2030 * Returns NULL if not found.
2033 buflist_findname_stat(ffname
, stp
)
2040 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
2041 if ((buf
->b_flags
& BF_DUMMY
) == 0 && !otherfile_buf(buf
, ffname
2050 #if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO)
2052 * Find file in buffer list by a regexp pattern.
2053 * Return fnum of the found buffer.
2054 * Return < 0 for error.
2058 buflist_findpat(pattern
, pattern_end
, unlisted
, diffmode
)
2060 char_u
*pattern_end
; /* pointer to first char after pattern */
2061 int unlisted
; /* find unlisted buffers */
2062 int diffmode
; /* find diff-mode buffers only */
2074 if (pattern_end
== pattern
+ 1 && (*pattern
== '%' || *pattern
== '#'))
2076 if (*pattern
== '%')
2077 match
= curbuf
->b_fnum
;
2079 match
= curwin
->w_alt_fnum
;
2081 if (diffmode
&& !diff_mode_buf(buflist_findnr(match
)))
2087 * Try four ways of matching a listed buffer:
2088 * attempt == 0: without '^' or '$' (at any position)
2089 * attempt == 1: with '^' at start (only at position 0)
2090 * attempt == 2: with '$' at end (only match at end)
2091 * attempt == 3: with '^' at start and '$' at end (only full match)
2092 * Repeat this for finding an unlisted buffer if there was no matching
2097 pat
= file_pat_to_reg_pat(pattern
, pattern_end
, NULL
, FALSE
);
2100 patend
= pat
+ STRLEN(pat
) - 1;
2101 toggledollar
= (patend
> pat
&& *patend
== '$');
2103 /* First try finding a listed buffer. If not found and "unlisted"
2104 * is TRUE, try finding an unlisted buffer. */
2108 for (attempt
= 0; attempt
<= 3; ++attempt
)
2110 /* may add '^' and '$' */
2112 *patend
= (attempt
< 2) ? NUL
: '$'; /* add/remove '$' */
2114 if (*p
== '^' && !(attempt
& 1)) /* add/remove '^' */
2116 prog
= vim_regcomp(p
, p_magic
? RE_MAGIC
: 0);
2123 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
2124 if (buf
->b_p_bl
== find_listed
2126 && (!diffmode
|| diff_mode_buf(buf
))
2128 && buflist_match(prog
, buf
) != NULL
)
2130 if (match
>= 0) /* already found a match */
2135 match
= buf
->b_fnum
; /* remember first match */
2139 if (match
>= 0) /* found one match */
2143 /* Only search for unlisted buffers if there was no match with
2144 * a listed buffer. */
2145 if (!unlisted
|| !find_listed
|| match
!= -1)
2147 find_listed
= FALSE
;
2154 EMSG2(_("E93: More than one match for %s"), pattern
);
2156 EMSG2(_("E94: No matching buffer for %s"), pattern
);
2161 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2164 * Find all buffer names that match.
2165 * For command line expansion of ":buf" and ":sbuf".
2166 * Return OK if matches found, FAIL otherwise.
2169 ExpandBufnames(pat
, num_file
, file
, options
)
2183 *num_file
= 0; /* return values in case of FAIL */
2186 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2189 patc
= alloc((unsigned)STRLEN(pat
) + 11);
2192 STRCPY(patc
, "\\(^\\|[\\/]\\)");
2193 STRCPY(patc
+ 11, pat
+ 1);
2199 * attempt == 0: try match with '\<', match at start of word
2200 * attempt == 1: try match without '\<', match anywhere
2202 for (attempt
= 0; attempt
<= 1; ++attempt
)
2204 if (attempt
> 0 && patc
== pat
)
2205 break; /* there was no anchor, no need to try again */
2206 prog
= vim_regcomp(patc
+ attempt
* 11, RE_MAGIC
);
2215 * round == 1: Count the matches.
2216 * round == 2: Build the array to keep the matches.
2218 for (round
= 1; round
<= 2; ++round
)
2221 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
2223 if (!buf
->b_p_bl
) /* skip unlisted buffers */
2225 p
= buflist_match(prog
, buf
);
2232 if (options
& WILD_HOME_REPLACE
)
2233 p
= home_replace_save(buf
, p
);
2236 (*file
)[count
++] = p
;
2240 if (count
== 0) /* no match found, break here */
2244 *file
= (char_u
**)alloc((unsigned)(count
* sizeof(char_u
*)));
2255 if (count
) /* match(es) found, break here */
2263 return (count
== 0 ? FAIL
: OK
);
2266 #endif /* FEAT_CMDL_COMPL */
2268 #ifdef HAVE_BUFLIST_MATCH
2270 * Check for a match on the file name for buffer "buf" with regprog "prog".
2273 buflist_match(prog
, buf
)
2279 /* First try the short file name, then the long file name. */
2280 match
= fname_match(prog
, buf
->b_sfname
);
2282 match
= fname_match(prog
, buf
->b_ffname
);
2288 * Try matching the regexp in "prog" with file name "name".
2289 * Return "name" when there is a match, NULL when not.
2292 fname_match(prog
, name
)
2296 char_u
*match
= NULL
;
2298 regmatch_T regmatch
;
2302 regmatch
.regprog
= prog
;
2303 #ifdef CASE_INSENSITIVE_FILENAME
2304 regmatch
.rm_ic
= TRUE
; /* Always ignore case */
2306 regmatch
.rm_ic
= FALSE
; /* Never ignore case */
2309 if (vim_regexec(®match
, name
, (colnr_T
)0))
2313 /* Replace $(HOME) with '~' and try matching again. */
2314 p
= home_replace_save(NULL
, name
);
2315 if (p
!= NULL
&& vim_regexec(®match
, p
, (colnr_T
)0))
2326 * find file in buffer list by number
2335 nr
= curwin
->w_alt_fnum
;
2336 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
2337 if (buf
->b_fnum
== nr
)
2343 * Get name of file 'n' in the buffer list.
2344 * When the file has no name an empty string is returned.
2345 * home_replace() is used to shorten the file name (used for marks).
2346 * Returns a pointer to allocated memory, of NULL when failed.
2349 buflist_nr2name(n
, fullname
, helptail
)
2352 int helptail
; /* for help buffers return tail only */
2356 buf
= buflist_findnr(n
);
2359 return home_replace_save(helptail
? buf
: NULL
,
2360 fullname
? buf
->b_ffname
: buf
->b_fname
);
2364 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2365 * When "copy_options" is TRUE save the local window option values.
2366 * When "lnum" is 0 only do the options.
2369 buflist_setfpos(buf
, win
, lnum
, col
, copy_options
)
2378 for (wip
= buf
->b_wininfo
; wip
!= NULL
; wip
= wip
->wi_next
)
2379 if (wip
->wi_win
== win
)
2383 /* allocate a new entry */
2384 wip
= (wininfo_T
*)alloc_clear((unsigned)sizeof(wininfo_T
));
2388 if (lnum
== 0) /* set lnum even when it's 0 */
2393 /* remove the entry from the list */
2395 wip
->wi_prev
->wi_next
= wip
->wi_next
;
2397 buf
->b_wininfo
= wip
->wi_next
;
2399 wip
->wi_next
->wi_prev
= wip
->wi_prev
;
2400 if (copy_options
&& wip
->wi_optset
)
2402 clear_winopt(&wip
->wi_opt
);
2404 deleteFoldRecurse(&wip
->wi_folds
);
2410 wip
->wi_fpos
.lnum
= lnum
;
2411 wip
->wi_fpos
.col
= col
;
2415 /* Save the window-specific option values. */
2416 copy_winopt(&win
->w_onebuf_opt
, &wip
->wi_opt
);
2418 wip
->wi_fold_manual
= win
->w_fold_manual
;
2419 cloneFoldGrowArray(&win
->w_folds
, &wip
->wi_folds
);
2421 wip
->wi_optset
= TRUE
;
2424 /* insert the entry in front of the list */
2425 wip
->wi_next
= buf
->b_wininfo
;
2426 buf
->b_wininfo
= wip
;
2427 wip
->wi_prev
= NULL
;
2429 wip
->wi_next
->wi_prev
= wip
;
2435 static int wininfo_other_tab_diff
__ARGS((wininfo_T
*wip
));
2438 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2439 * page. That's because a diff is local to a tab page.
2442 wininfo_other_tab_diff(wip
)
2447 if (wip
->wi_opt
.wo_diff
)
2449 for (wp
= firstwin
; wp
!= NULL
; wp
= wp
->w_next
)
2450 /* return FALSE when it's a window in the current tab page, thus
2451 * the buffer was in diff mode here */
2452 if (wip
->wi_win
== wp
)
2461 * Find info for the current window in buffer "buf".
2462 * If not found, return the info for the most recently used window.
2463 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2465 * Returns NULL when there isn't any info.
2469 find_wininfo(buf
, skip_diff_buffer
)
2471 int skip_diff_buffer
;
2475 for (wip
= buf
->b_wininfo
; wip
!= NULL
; wip
= wip
->wi_next
)
2476 if (wip
->wi_win
== curwin
2478 && (!skip_diff_buffer
|| !wininfo_other_tab_diff(wip
))
2483 /* If no wininfo for curwin, use the first in the list (that doesn't have
2484 * 'diff' set and is in another tab page). */
2488 if (skip_diff_buffer
)
2490 for (wip
= buf
->b_wininfo
; wip
!= NULL
; wip
= wip
->wi_next
)
2491 if (!wininfo_other_tab_diff(wip
))
2496 wip
= buf
->b_wininfo
;
2502 * Reset the local window options to the values last used in this window.
2503 * If the buffer wasn't used in this window before, use the values from
2504 * the most recently used window. If the values were never set, use the
2505 * global values for the window.
2513 clear_winopt(&curwin
->w_onebuf_opt
);
2515 clearFolding(curwin
);
2518 wip
= find_wininfo(buf
, TRUE
);
2519 if (wip
!= NULL
&& wip
->wi_optset
)
2521 copy_winopt(&wip
->wi_opt
, &curwin
->w_onebuf_opt
);
2523 curwin
->w_fold_manual
= wip
->wi_fold_manual
;
2524 curwin
->w_foldinvalid
= TRUE
;
2525 cloneFoldGrowArray(&wip
->wi_folds
, &curwin
->w_folds
);
2529 copy_winopt(&curwin
->w_allbuf_opt
, &curwin
->w_onebuf_opt
);
2532 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2534 curwin
->w_p_fdl
= p_fdls
;
2539 * Find the position (lnum and col) for the buffer 'buf' for the current
2541 * Returns a pointer to no_position if no position is found.
2544 buflist_findfpos(buf
)
2548 static pos_T no_position
= {1, 0};
2550 wip
= find_wininfo(buf
, FALSE
);
2552 return &(wip
->wi_fpos
);
2554 return &no_position
;
2558 * Find the lnum for the buffer 'buf' for the current window.
2561 buflist_findlnum(buf
)
2564 return buflist_findfpos(buf
)->lnum
;
2567 #if defined(FEAT_LISTCMDS) || defined(PROTO)
2569 * List all know file names (for :files and :buffers command).
2580 for (buf
= firstbuf
; buf
!= NULL
&& !got_int
; buf
= buf
->b_next
)
2582 /* skip unlisted buffers, unless ! was used */
2583 if (!buf
->b_p_bl
&& !eap
->forceit
)
2586 if (buf_spname(buf
) != NULL
)
2587 STRCPY(NameBuff
, buf_spname(buf
));
2589 home_replace(buf
, buf
->b_fname
, NameBuff
, MAXPATHL
, TRUE
);
2591 len
= vim_snprintf((char *)IObuff
, IOSIZE
- 20, "%3d%c%c%c%c%c \"%s\"",
2593 buf
->b_p_bl
? ' ' : 'u',
2594 buf
== curbuf
? '%' :
2595 (curwin
->w_alt_fnum
== buf
->b_fnum
? '#' : ' '),
2596 buf
->b_ml
.ml_mfp
== NULL
? ' ' :
2597 (buf
->b_nwindows
== 0 ? 'h' : 'a'),
2598 !buf
->b_p_ma
? '-' : (buf
->b_p_ro
? '=' : ' '),
2599 (buf
->b_flags
& BF_READERR
) ? 'x'
2600 : (bufIsChanged(buf
) ? '+' : ' '),
2603 /* put "line 999" in column 40 or after the file name */
2604 i
= 40 - vim_strsize(IObuff
);
2607 IObuff
[len
++] = ' ';
2608 } while (--i
> 0 && len
< IOSIZE
- 18);
2609 vim_snprintf((char *)IObuff
+ len
, IOSIZE
- len
, _("line %ld"),
2610 buf
== curbuf
? curwin
->w_cursor
.lnum
2611 : (long)buflist_findlnum(buf
));
2612 msg_outtrans(IObuff
);
2613 out_flush(); /* output one line at a time */
2620 * Get file name and line number for file 'fnum'.
2621 * Used by DoOneCmd() for translating '%' and '#'.
2622 * Used by insert_reg() and cmdline_paste() for '#' register.
2623 * Return FAIL if not found, OK for success.
2626 buflist_name_nr(fnum
, fname
, lnum
)
2633 buf
= buflist_findnr(fnum
);
2634 if (buf
== NULL
|| buf
->b_fname
== NULL
)
2637 *fname
= buf
->b_fname
;
2638 *lnum
= buflist_findlnum(buf
);
2644 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2645 * The file name with the full path is also remembered, for when :cd is used.
2646 * Returns FAIL for failure (file name already in use by other buffer)
2650 setfname(buf
, ffname
, sfname
, message
)
2652 char_u
*ffname
, *sfname
;
2653 int message
; /* give message when buffer already exists */
2660 if (ffname
== NULL
|| *ffname
== NUL
)
2662 /* Removing the name. */
2663 vim_free(buf
->b_ffname
);
2664 vim_free(buf
->b_sfname
);
2665 buf
->b_ffname
= NULL
;
2666 buf
->b_sfname
= NULL
;
2668 st
.st_dev
= (dev_T
)-1;
2673 fname_expand(buf
, &ffname
, &sfname
); /* will allocate ffname */
2674 if (ffname
== NULL
) /* out of memory */
2678 * if the file name is already used in another buffer:
2679 * - if the buffer is loaded, fail
2680 * - if the buffer is not loaded, delete it from the list
2683 if (mch_stat((char *)ffname
, &st
) < 0)
2684 st
.st_dev
= (dev_T
)-1;
2686 if (!(buf
->b_flags
& BF_DUMMY
))
2688 obuf
= buflist_findname_stat(ffname
, &st
);
2690 obuf
= buflist_findname(ffname
);
2692 if (obuf
!= NULL
&& obuf
!= buf
)
2694 if (obuf
->b_ml
.ml_mfp
!= NULL
) /* it's loaded, fail */
2697 EMSG(_("E95: Buffer with this name already exists"));
2701 close_buffer(NULL
, obuf
, DOBUF_WIPE
); /* delete from the list */
2703 sfname
= vim_strsave(sfname
);
2704 if (ffname
== NULL
|| sfname
== NULL
)
2710 #ifdef USE_FNAME_CASE
2711 # ifdef USE_LONG_FNAME
2714 fname_case(sfname
, 0); /* set correct case for short file name */
2716 vim_free(buf
->b_ffname
);
2717 vim_free(buf
->b_sfname
);
2718 buf
->b_ffname
= ffname
;
2719 buf
->b_sfname
= sfname
;
2721 buf
->b_fname
= buf
->b_sfname
;
2723 if (st
.st_dev
== (dev_T
)-1)
2727 buf
->b_dev
= st
.st_dev
;
2728 buf
->b_ino
= st
.st_ino
;
2733 buf
->b_shortname
= FALSE
;
2736 buf_name_changed(buf
);
2741 * Crude way of changing the name of a buffer. Use with care!
2742 * The name should be relative to the current directory.
2745 buf_set_name(fnum
, name
)
2751 buf
= buflist_findnr(fnum
);
2754 vim_free(buf
->b_sfname
);
2755 vim_free(buf
->b_ffname
);
2756 buf
->b_ffname
= vim_strsave(name
);
2757 buf
->b_sfname
= NULL
;
2758 /* Allocate ffname and expand into full path. Also resolves .lnk
2759 * files on Win32. */
2760 fname_expand(buf
, &buf
->b_ffname
, &buf
->b_sfname
);
2761 buf
->b_fname
= buf
->b_sfname
;
2766 * Take care of what needs to be done when the name of buffer "buf" has
2770 buf_name_changed(buf
)
2774 * If the file name changed, also change the name of the swapfile
2776 if (buf
->b_ml
.ml_mfp
!= NULL
)
2779 if (curwin
->w_buffer
== buf
)
2780 check_arg_idx(curwin
); /* check file name for arg list */
2782 maketitle(); /* set window title */
2785 status_redraw_all(); /* status lines need to be redrawn */
2787 fmarks_check_names(buf
); /* check named file marks */
2788 ml_timestamp(buf
); /* reset timestamp */
2792 * set alternate file name for current window
2794 * Used by do_one_cmd(), do_write() and do_ecmd().
2795 * Return the buffer.
2798 setaltfname(ffname
, sfname
, lnum
)
2805 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2806 buf
= buflist_new(ffname
, sfname
, lnum
, 0);
2807 if (buf
!= NULL
&& !cmdmod
.keepalt
)
2808 curwin
->w_alt_fnum
= buf
->b_fnum
;
2813 * Get alternate file name for current window.
2814 * Return NULL if there isn't any, and give error message if requested.
2818 int errmsg
; /* give error message */
2823 if (buflist_name_nr(0, &fname
, &dummy
) == FAIL
)
2833 * Add a file name to the buflist and return its number.
2834 * Uses same flags as buflist_new(), except BLN_DUMMY.
2836 * used by qf_init(), main() and doarglist()
2839 buflist_add(fname
, flags
)
2845 buf
= buflist_new(fname
, NULL
, (linenr_T
)0, flags
);
2851 #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2853 * Adjust slashes in file names. Called after 'shellslash' was set.
2856 buflist_slash_adjust()
2860 for (bp
= firstbuf
; bp
!= NULL
; bp
= bp
->b_next
)
2862 if (bp
->b_ffname
!= NULL
)
2863 slash_adjust(bp
->b_ffname
);
2864 if (bp
->b_sfname
!= NULL
)
2865 slash_adjust(bp
->b_sfname
);
2871 * Set alternate cursor position for the current buffer and window "win".
2872 * Also save the local window option values.
2875 buflist_altfpos(win
)
2878 buflist_setfpos(curbuf
, win
, win
->w_cursor
.lnum
, win
->w_cursor
.col
, TRUE
);
2882 * Return TRUE if 'ffname' is not the same file as current file.
2883 * Fname must have a full path (expanded by mch_FullName()).
2889 return otherfile_buf(curbuf
, ffname
2897 otherfile_buf(buf
, ffname
2908 /* no name is different */
2909 if (ffname
== NULL
|| *ffname
== NUL
|| buf
->b_ffname
== NULL
)
2911 if (fnamecmp(ffname
, buf
->b_ffname
) == 0)
2917 /* If no struct stat given, get it now */
2920 if (buf
->b_dev
< 0 || mch_stat((char *)ffname
, &st
) < 0)
2921 st
.st_dev
= (dev_T
)-1;
2924 /* Use dev/ino to check if the files are the same, even when the names
2925 * are different (possible with links). Still need to compare the
2926 * name above, for when the file doesn't exist yet.
2927 * Problem: The dev/ino changes when a file is deleted (and created
2928 * again) and remains the same when renamed/moved. We don't want to
2929 * mch_stat() each buffer each time, that would be too slow. Get the
2930 * dev/ino again when they appear to match, but not when they appear
2931 * to be different: Could skip a buffer when it's actually the same
2933 if (buf_same_ino(buf
, stp
))
2936 if (buf_same_ino(buf
, stp
))
2944 #if defined(UNIX) || defined(PROTO)
2946 * Set inode and device number for a buffer.
2947 * Must always be called when b_fname is changed!.
2955 if (buf
->b_fname
!= NULL
&& mch_stat((char *)buf
->b_fname
, &st
) >= 0)
2957 buf
->b_dev
= st
.st_dev
;
2958 buf
->b_ino
= st
.st_ino
;
2965 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
2968 buf_same_ino(buf
, stp
)
2972 return (buf
->b_dev
>= 0
2973 && stp
->st_dev
== buf
->b_dev
2974 && stp
->st_ino
== buf
->b_ino
);
2979 * Print info about the current buffer.
2982 fileinfo(fullname
, shorthelp
, dont_truncate
)
2983 int fullname
; /* when non-zero print full path */
2993 buffer
= alloc(IOSIZE
);
2997 if (fullname
> 1) /* 2 CTRL-G: include buffer number */
2999 sprintf((char *)buffer
, "buf %d: ", curbuf
->b_fnum
);
3000 p
= buffer
+ STRLEN(buffer
);
3006 if (buf_spname(curbuf
) != NULL
)
3007 STRCPY(p
, buf_spname(curbuf
));
3010 if (!fullname
&& curbuf
->b_fname
!= NULL
)
3011 name
= curbuf
->b_fname
;
3013 name
= curbuf
->b_ffname
;
3014 home_replace(shorthelp
? curbuf
: NULL
, name
, p
,
3015 (int)(IOSIZE
- (p
- buffer
)), TRUE
);
3018 len
= STRLEN(buffer
);
3019 vim_snprintf((char *)buffer
+ len
, IOSIZE
- len
,
3021 curbufIsChanged() ? (shortmess(SHM_MOD
)
3022 ? " [+]" : _(" [Modified]")) : " ",
3023 (curbuf
->b_flags
& BF_NOTEDITED
)
3024 #ifdef FEAT_QUICKFIX
3025 && !bt_dontwrite(curbuf
)
3027 ? _("[Not edited]") : "",
3028 (curbuf
->b_flags
& BF_NEW
)
3029 #ifdef FEAT_QUICKFIX
3030 && !bt_dontwrite(curbuf
)
3032 ? _("[New file]") : "",
3033 (curbuf
->b_flags
& BF_READERR
) ? _("[Read errors]") : "",
3034 curbuf
->b_p_ro
? (shortmess(SHM_RO
) ? "[RO]"
3035 : _("[readonly]")) : "",
3036 (curbufIsChanged() || (curbuf
->b_flags
& BF_WRITE_MASK
)
3037 || curbuf
->b_p_ro
) ?
3039 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3040 * causes an overflow, thus for large numbers divide instead. */
3041 if (curwin
->w_cursor
.lnum
> 1000000L)
3042 n
= (int)(((long)curwin
->w_cursor
.lnum
) /
3043 ((long)curbuf
->b_ml
.ml_line_count
/ 100L));
3045 n
= (int)(((long)curwin
->w_cursor
.lnum
* 100L) /
3046 (long)curbuf
->b_ml
.ml_line_count
);
3047 len
= STRLEN(buffer
);
3048 if (curbuf
->b_ml
.ml_flags
& ML_EMPTY
)
3050 vim_snprintf((char *)buffer
+ len
, IOSIZE
- len
, "%s", _(no_lines_msg
));
3052 #ifdef FEAT_CMDL_INFO
3055 /* Current line and column are already on the screen -- webb */
3056 if (curbuf
->b_ml
.ml_line_count
== 1)
3057 vim_snprintf((char *)buffer
+ len
, IOSIZE
- len
,
3058 _("1 line --%d%%--"), n
);
3060 vim_snprintf((char *)buffer
+ len
, IOSIZE
- len
,
3061 _("%ld lines --%d%%--"),
3062 (long)curbuf
->b_ml
.ml_line_count
, n
);
3067 vim_snprintf((char *)buffer
+ len
, IOSIZE
- len
,
3068 _("line %ld of %ld --%d%%-- col "),
3069 (long)curwin
->w_cursor
.lnum
,
3070 (long)curbuf
->b_ml
.ml_line_count
,
3073 col_print(buffer
+ STRLEN(buffer
),
3074 (int)curwin
->w_cursor
.col
+ 1, (int)curwin
->w_virtcol
+ 1);
3077 (void)append_arg_number(curwin
, buffer
, !shortmess(SHM_FILE
), IOSIZE
);
3081 /* Temporarily set msg_scroll to avoid the message being truncated.
3082 * First call msg_start() to get the message in the right place. */
3091 p
= msg_trunc_attr(buffer
, FALSE
, 0);
3092 if (restart_edit
!= 0 || (msg_scrolled
&& !need_wait_return
))
3093 /* Need to repeat the message after redrawing when:
3094 * - When restart_edit is set (otherwise there will be a delay
3095 * before redrawing).
3096 * - When the screen was scrolled but there is no wait-return
3105 col_print(buf
, col
, vcol
)
3111 sprintf((char *)buf
, "%d", col
);
3113 sprintf((char *)buf
, "%d-%d", col
, vcol
);
3116 #if defined(FEAT_TITLE) || defined(PROTO)
3118 * put file name in title bar of window and in icon title
3121 static char_u
*lasttitle
= NULL
;
3122 static char_u
*lasticon
= NULL
;
3128 char_u
*t_str
= NULL
;
3130 char_u
*i_str
= NULL
;
3139 /* Postpone updating the title when 'lazyredraw' is set. */
3140 need_maketitle
= TRUE
;
3144 #ifdef FEAT_GUI_MACVIM
3145 gui_macvim_update_modified_flag();
3148 need_maketitle
= FALSE
;
3149 if (!p_title
&& !p_icon
)
3156 maxlen
= p_titlelen
* Columns
/ 100;
3162 if (*p_titlestring
!= NUL
)
3165 if (stl_syntax
& STL_IN_TITLE
)
3167 int use_sandbox
= FALSE
;
3168 int save_called_emsg
= called_emsg
;
3171 use_sandbox
= was_set_insecurely((char_u
*)"titlestring", 0);
3173 called_emsg
= FALSE
;
3174 build_stl_str_hl(curwin
, t_str
, sizeof(buf
),
3175 p_titlestring
, use_sandbox
,
3176 0, maxlen
, NULL
, NULL
);
3178 set_string_option_direct((char_u
*)"titlestring", -1,
3179 (char_u
*)"", OPT_FREE
, SID_ERROR
);
3180 called_emsg
|= save_called_emsg
;
3184 t_str
= p_titlestring
;
3188 /* format: "fname + (path) (1 of 2) - VIM" */
3190 if (curbuf
->b_fname
== NULL
)
3191 STRCPY(buf
, _("[No Name]"));
3194 p
= transstr(gettail(curbuf
->b_fname
));
3195 vim_strncpy(buf
, p
, IOSIZE
- 100);
3199 switch (bufIsChanged(curbuf
)
3200 + (curbuf
->b_p_ro
* 2)
3201 + (!curbuf
->b_p_ma
* 4))
3203 case 1: STRCAT(buf
, " +"); break;
3204 case 2: STRCAT(buf
, " ="); break;
3205 case 3: STRCAT(buf
, " =+"); break;
3207 case 6: STRCAT(buf
, " -"); break;
3209 case 7: STRCAT(buf
, " -+"); break;
3212 if (curbuf
->b_fname
!= NULL
)
3214 /* Get path of file, replace home dir with ~ */
3215 off
= (int)STRLEN(buf
);
3218 home_replace(curbuf
, curbuf
->b_ffname
,
3219 buf
+ off
, IOSIZE
- off
, TRUE
);
3220 #ifdef BACKSLASH_IN_FILENAME
3221 /* avoid "c:/name" to be reduced to "c" */
3222 if (isalpha(buf
[off
]) && buf
[off
+ 1] == ':')
3225 /* remove the file name */
3226 p
= gettail_sep(buf
+ off
);
3228 /* must be a help buffer */
3229 vim_strncpy(buf
+ off
, (char_u
*)_("help"),
3234 /* translate unprintable chars */
3235 p
= transstr(buf
+ off
);
3236 vim_strncpy(buf
+ off
, p
, IOSIZE
- off
- 1);
3241 #ifndef FEAT_GUI_MACVIM
3242 append_arg_number(curwin
, buf
, FALSE
, IOSIZE
);
3245 #if defined(FEAT_CLIENTSERVER)
3246 if (serverName
!= NULL
)
3249 STRCAT(buf
, serverName
);
3253 STRCAT(buf
, " - VIM");
3257 /* make it shorter by removing a bit in the middle */
3258 len
= vim_strsize(buf
);
3260 trunc_string(buf
, buf
, maxlen
);
3264 mustset
= ti_change(t_str
, &lasttitle
);
3269 if (*p_iconstring
!= NUL
)
3272 if (stl_syntax
& STL_IN_ICON
)
3274 int use_sandbox
= FALSE
;
3275 int save_called_emsg
= called_emsg
;
3278 use_sandbox
= was_set_insecurely((char_u
*)"iconstring", 0);
3280 called_emsg
= FALSE
;
3281 build_stl_str_hl(curwin
, i_str
, sizeof(buf
),
3282 p_iconstring
, use_sandbox
,
3285 set_string_option_direct((char_u
*)"iconstring", -1,
3286 (char_u
*)"", OPT_FREE
, SID_ERROR
);
3287 called_emsg
|= save_called_emsg
;
3291 i_str
= p_iconstring
;
3295 if (buf_spname(curbuf
) != NULL
)
3296 i_name
= (char_u
*)buf_spname(curbuf
);
3297 else /* use file name only in icon */
3298 i_name
= gettail(curbuf
->b_ffname
);
3300 /* Truncate name at 100 bytes. */
3301 len
= (int)STRLEN(i_name
);
3307 len
+= (*mb_tail_off
)(i_name
, i_name
+ len
) + 1;
3311 STRCPY(i_str
, i_name
);
3312 trans_characters(i_str
, IOSIZE
);
3316 mustset
|= ti_change(i_str
, &lasticon
);
3323 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3324 * from "str" if it does.
3325 * Return TRUE when "*last" changed.
3328 ti_change(str
, last
)
3332 if ((str
== NULL
) != (*last
== NULL
)
3333 || (str
!= NULL
&& *last
!= NULL
&& STRCMP(str
, *last
) != 0))
3339 *last
= vim_strsave(str
);
3346 * Put current window title back (used after calling a shell)
3351 mch_settitle(lasttitle
, lasticon
);
3354 # if defined(EXITFREE) || defined(PROTO)
3358 vim_free(lasttitle
);
3363 #endif /* FEAT_TITLE */
3365 #if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
3367 * Build a string from the status line items in "fmt".
3368 * Return length of string in screen cells.
3370 * Normally works for window "wp", except when working for 'tabline' then it
3373 * Items are drawn interspersed with the text that surrounds it
3374 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3375 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3377 * If maxwidth is not zero, the string will be filled at any middle marker
3378 * or truncated if too long, fillchar is used for all whitespace.
3382 build_stl_str_hl(wp
, out
, outlen
, fmt
, use_sandbox
, fillchar
, maxwidth
, hltab
, tabtab
)
3384 char_u
*out
; /* buffer to write into != NameBuff */
3385 size_t outlen
; /* length of out[] */
3387 int use_sandbox
; /* "fmt" was set insecurely, use sandbox */
3390 struct stl_hlrec
*hltab
; /* return: HL attributes (can be NULL) */
3391 struct stl_hlrec
*tabtab
; /* return: tab page nrs (can be NULL) */
3405 int prevchar_isflag
;
3406 int prevchar_isitem
;
3414 int groupitem
[STL_MAX_ITEM
];
3431 } item
[STL_MAX_ITEM
];
3439 char_u
*usefmt
= fmt
;
3440 struct stl_hlrec
*sp
;
3444 * When the format starts with "%!" then evaluate it as an expression and
3445 * use the result as the actual format string.
3447 if (fmt
[0] == '%' && fmt
[1] == '!')
3449 usefmt
= eval_to_string_safe(fmt
+ 2, NULL
, use_sandbox
);
3458 /* Can't handle a multi-byte fill character yet. */
3459 else if (mb_char2len(fillchar
) > 1)
3464 * Get line & check if empty (cursorpos will show "0-1").
3465 * If inversion is possible we use it. Else '=' characters are used.
3467 linecont
= ml_get_buf(wp
->w_buffer
, wp
->w_cursor
.lnum
, FALSE
);
3468 empty_line
= (*linecont
== NUL
);
3473 prevchar_isflag
= TRUE
;
3474 prevchar_isitem
= FALSE
;
3475 for (s
= usefmt
; *s
; )
3477 if (*s
!= NUL
&& *s
!= '%')
3478 prevchar_isflag
= prevchar_isitem
= FALSE
;
3481 * Handle up to the next '%' or the end.
3483 while (*s
!= NUL
&& *s
!= '%' && p
+ 1 < out
+ outlen
)
3485 if (*s
== NUL
|| p
+ 1 >= out
+ outlen
)
3489 * Handle one '%' item.
3494 if (p
+ 1 >= out
+ outlen
)
3497 prevchar_isflag
= prevchar_isitem
= FALSE
;
3500 if (*s
== STL_MIDDLEMARK
)
3505 item
[curitem
].type
= Middle
;
3506 item
[curitem
++].start
= p
;
3509 if (*s
== STL_TRUNCMARK
)
3512 item
[curitem
].type
= Trunc
;
3513 item
[curitem
++].start
= p
;
3523 t
= item
[groupitem
[groupdepth
]].start
;
3526 if (curitem
> groupitem
[groupdepth
] + 1
3527 && item
[groupitem
[groupdepth
]].minwid
== 0)
3529 /* remove group if all items are empty */
3530 for (n
= groupitem
[groupdepth
] + 1; n
< curitem
; n
++)
3531 if (item
[n
].type
== Normal
)
3539 if (l
> item
[groupitem
[groupdepth
]].maxwid
)
3541 /* truncate, remove n bytes of text at the start */
3545 /* Find the first character that should be included. */
3547 while (l
>= item
[groupitem
[groupdepth
]].maxwid
)
3549 l
-= ptr2cells(t
+ n
);
3550 n
+= (*mb_ptr2len
)(t
+ n
);
3555 n
= (long)(p
- t
) - item
[groupitem
[groupdepth
]].maxwid
+ 1;
3558 mch_memmove(t
+ 1, t
+ n
, p
- (t
+ n
));
3561 /* Fill up space left over by half a double-wide char. */
3562 while (++l
< item
[groupitem
[groupdepth
]].minwid
)
3566 /* correct the start of the items for the truncation */
3567 for (l
= groupitem
[groupdepth
] + 1; l
< curitem
; l
++)
3570 if (item
[l
].start
< t
)
3574 else if (abs(item
[groupitem
[groupdepth
]].minwid
) > l
)
3577 n
= item
[groupitem
[groupdepth
]].minwid
;
3580 /* fill by appending characters */
3582 while (l
++ < n
&& p
+ 1 < out
+ outlen
)
3587 /* fill by inserting characters */
3588 mch_memmove(t
+ n
- l
, t
, p
- t
);
3590 if (p
+ l
>= out
+ outlen
)
3591 l
= (long)((out
+ outlen
) - p
- 1);
3593 for (n
= groupitem
[groupdepth
] + 1; n
< curitem
; n
++)
3615 if (VIM_ISDIGIT(*s
))
3617 minwid
= (int)getdigits(&s
);
3618 if (minwid
< 0) /* overflow */
3621 if (*s
== STL_USER_HL
)
3623 item
[curitem
].type
= Highlight
;
3624 item
[curitem
].start
= p
;
3625 item
[curitem
].minwid
= minwid
> 9 ? 1 : minwid
;
3630 if (*s
== STL_TABPAGENR
|| *s
== STL_TABCLOSENR
)
3632 if (*s
== STL_TABCLOSENR
)
3636 /* %X ends the close label, go back to the previously
3637 * define tab label nr. */
3638 for (n
= curitem
- 1; n
>= 0; --n
)
3639 if (item
[n
].type
== TabPage
&& item
[n
].minwid
>= 0)
3641 minwid
= item
[n
].minwid
;
3646 /* close nrs are stored as negative values */
3649 item
[curitem
].type
= TabPage
;
3650 item
[curitem
].start
= p
;
3651 item
[curitem
].minwid
= minwid
;
3659 if (VIM_ISDIGIT(*s
))
3661 maxwid
= (int)getdigits(&s
);
3662 if (maxwid
<= 0) /* overflow */
3666 minwid
= (minwid
> 50 ? 50 : minwid
) * l
;
3669 groupitem
[groupdepth
++] = curitem
;
3670 item
[curitem
].type
= Group
;
3671 item
[curitem
].start
= p
;
3672 item
[curitem
].minwid
= minwid
;
3673 item
[curitem
].maxwid
= maxwid
;
3678 if (vim_strchr(STL_ALL
, *s
) == NULL
)
3685 /* OK - now for the real work */
3696 fillable
= FALSE
; /* don't change ' ' to fillchar */
3697 if (buf_spname(wp
->w_buffer
) != NULL
)
3698 STRCPY(NameBuff
, buf_spname(wp
->w_buffer
));
3701 t
= (opt
== STL_FULLPATH
) ? wp
->w_buffer
->b_ffname
3702 : wp
->w_buffer
->b_fname
;
3703 home_replace(wp
->w_buffer
, t
, NameBuff
, MAXPATHL
, TRUE
);
3705 trans_characters(NameBuff
, MAXPATHL
);
3706 if (opt
!= STL_FILENAME
)
3709 str
= gettail(NameBuff
);
3712 case STL_VIM_EXPR
: /* '{' */
3715 while (*s
!= '}' && *s
!= NUL
&& p
+ 1 < out
+ outlen
)
3717 if (*s
!= '}') /* missing '}' or out of space */
3724 sprintf((char *)tmp
, "%d", curbuf
->b_fnum
);
3725 set_internal_string_var((char_u
*)"actual_curbuf", tmp
);
3730 curbuf
= wp
->w_buffer
;
3732 str
= eval_to_string_safe(p
, &t
, use_sandbox
);
3736 do_unlet((char_u
*)"g:actual_curbuf", TRUE
);
3738 if (str
!= NULL
&& *str
!= 0)
3740 if (*skipdigits(str
) == NUL
)
3742 num
= atoi((char *)str
);
3752 num
= (wp
->w_buffer
->b_ml
.ml_flags
& ML_EMPTY
)
3753 ? 0L : (long)(wp
->w_cursor
.lnum
);
3757 num
= wp
->w_buffer
->b_ml
.ml_line_count
;
3761 num
= !(State
& INSERT
) && empty_line
3762 ? 0 : (int)wp
->w_cursor
.col
+ 1;
3766 case STL_VIRTCOL_ALT
:
3767 /* In list mode virtcol needs to be recomputed */
3768 virtcol
= wp
->w_virtcol
;
3769 if (wp
->w_p_list
&& lcs_tab1
== NUL
)
3771 wp
->w_p_list
= FALSE
;
3772 getvcol(wp
, &wp
->w_cursor
, NULL
, &virtcol
, NULL
);
3773 wp
->w_p_list
= TRUE
;
3776 /* Don't display %V if it's the same as %c. */
3777 if (opt
== STL_VIRTCOL_ALT
3778 && (virtcol
== (colnr_T
)(!(State
& INSERT
) && empty_line
3779 ? 0 : (int)wp
->w_cursor
.col
+ 1)))
3781 num
= (long)virtcol
;
3784 case STL_PERCENTAGE
:
3785 num
= (int)(((long)wp
->w_cursor
.lnum
* 100L) /
3786 (long)wp
->w_buffer
->b_ml
.ml_line_count
);
3789 case STL_ALTPERCENT
:
3791 get_rel_pos(wp
, str
);
3794 case STL_ARGLISTSTAT
:
3797 if (append_arg_number(wp
, tmp
, FALSE
, (int)sizeof(tmp
)))
3803 if (get_keymap_str(wp
, tmp
, TMPLEN
))
3807 #if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
3808 num
= printer_page_num
;
3815 num
= wp
->w_buffer
->b_fnum
;
3822 l
= ml_find_line_or_offset(wp
->w_buffer
, wp
->w_cursor
.lnum
, NULL
);
3823 num
= (wp
->w_buffer
->b_ml
.ml_flags
& ML_EMPTY
) || l
< 0 ?
3824 0L : l
+ 1 + (!(State
& INSERT
) && empty_line
?
3825 0 : (int)wp
->w_cursor
.col
);
3832 if (wp
->w_cursor
.col
> STRLEN(linecont
))
3837 num
= (*mb_ptr2char
)(linecont
+ wp
->w_cursor
.col
);
3839 num
= linecont
[wp
->w_cursor
.col
];
3844 else if (num
== CAR
&& get_fileformat(wp
->w_buffer
) == EOL_MAC
)
3849 case STL_ROFLAG_ALT
:
3851 if (wp
->w_buffer
->b_p_ro
)
3852 str
= (char_u
*)((opt
== STL_ROFLAG_ALT
) ? ",RO" : "[RO]");
3856 case STL_HELPFLAG_ALT
:
3858 if (wp
->w_buffer
->b_help
)
3859 str
= (char_u
*)((opt
== STL_HELPFLAG_ALT
) ? ",HLP"
3865 if (*wp
->w_buffer
->b_p_ft
!= NUL
3866 && STRLEN(wp
->w_buffer
->b_p_ft
) < TMPLEN
- 3)
3868 vim_snprintf((char *)tmp
, sizeof(tmp
), "[%s]",
3869 wp
->w_buffer
->b_p_ft
);
3874 case STL_FILETYPE_ALT
:
3876 if (*wp
->w_buffer
->b_p_ft
!= NUL
3877 && STRLEN(wp
->w_buffer
->b_p_ft
) < TMPLEN
- 2)
3879 vim_snprintf((char *)tmp
, sizeof(tmp
), ",%s",
3880 wp
->w_buffer
->b_p_ft
);
3881 for (t
= tmp
; *t
!= 0; t
++)
3882 *t
= TOUPPER_LOC(*t
);
3888 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3889 case STL_PREVIEWFLAG
:
3890 case STL_PREVIEWFLAG_ALT
:
3893 str
= (char_u
*)((opt
== STL_PREVIEWFLAG_ALT
) ? ",PRV"
3899 case STL_MODIFIED_ALT
:
3901 switch ((opt
== STL_MODIFIED_ALT
)
3902 + bufIsChanged(wp
->w_buffer
) * 2
3903 + (!wp
->w_buffer
->b_p_ma
) * 4)
3905 case 2: str
= (char_u
*)"[+]"; break;
3906 case 3: str
= (char_u
*)",+"; break;
3907 case 4: str
= (char_u
*)"[-]"; break;
3908 case 5: str
= (char_u
*)",-"; break;
3909 case 6: str
= (char_u
*)"[+-]"; break;
3910 case 7: str
= (char_u
*)",+-"; break;
3916 while (*s
!= '#' && *s
!= NUL
)
3920 item
[curitem
].type
= Highlight
;
3921 item
[curitem
].start
= p
;
3922 item
[curitem
].minwid
= -syn_namen2id(t
, (int)(s
- t
));
3929 item
[curitem
].start
= p
;
3930 item
[curitem
].type
= Normal
;
3931 if (str
!= NULL
&& *str
)
3937 && ((!prevchar_isitem
&& *t
== ',')
3938 || (prevchar_isflag
&& *t
== ' ')))
3940 prevchar_isflag
= TRUE
;
3944 prevchar_isitem
= TRUE
;
3952 t
+= (*mb_ptr2len
)(t
);
3956 l
-= byte2cells(*t
++);
3957 if (p
+ 1 >= out
+ outlen
)
3963 for (; l
< minwid
&& p
+ 1 < out
+ outlen
; l
++)
3965 /* Don't put a "-" in front of a digit. */
3966 if (l
+ 1 == minwid
&& fillchar
== '-' && VIM_ISDIGIT(*t
))
3975 while (*t
&& p
+ 1 < out
+ outlen
)
3978 /* Change a space by fillchar, unless fillchar is '-' and a
3980 if (fillable
&& p
[-1] == ' '
3981 && (!VIM_ISDIGIT(*t
) || fillchar
!= '-'))
3984 for (; l
< minwid
&& p
+ 1 < out
+ outlen
; l
++)
3989 int nbase
= (base
== 'D' ? 10 : (base
== 'O' ? 8 : 16));
3992 if (p
+ 20 >= out
+ outlen
)
3993 break; /* not sufficient space */
3994 prevchar_isitem
= TRUE
;
3996 if (opt
== STL_VIRTCOL_ALT
)
4005 *t
++ = nbase
== 16 ? base
: (nbase
== 8 ? 'o' : 'd');
4008 for (n
= num
, l
= 1; n
>= nbase
; n
/= nbase
)
4010 if (opt
== STL_VIRTCOL_ALT
)
4016 while (l
-- > maxwid
)
4022 vim_snprintf((char *)p
, outlen
- (p
- out
), (char *)nstr
,
4026 vim_snprintf((char *)p
, outlen
- (p
- out
), (char *)nstr
,
4031 item
[curitem
].type
= Empty
;
4033 if (opt
== STL_VIM_EXPR
)
4036 if (num
>= 0 || (!itemisflag
&& str
&& *str
))
4037 prevchar_isflag
= FALSE
; /* Item not NULL, but not a flag */
4048 width
= vim_strsize(out
);
4049 if (maxwidth
> 0 && width
> maxwidth
)
4051 /* Result is too long, must truncate somewhere. */
4057 for ( ; l
< itemcnt
; l
++)
4058 if (item
[l
].type
== Trunc
)
4060 /* Truncate at %< item. */
4066 /* No %< item, truncate first item. */
4072 if (width
- vim_strsize(s
) >= maxwidth
)
4074 /* Truncation mark is beyond max length */
4082 width
+= ptr2cells(s
);
4083 if (width
>= maxwidth
)
4085 s
+= (*mb_ptr2len
)(s
);
4087 /* Fill up for half a double-wide character. */
4088 while (++width
< maxwidth
)
4093 s
= out
+ maxwidth
- 1;
4094 for (l
= 0; l
< itemcnt
; l
++)
4095 if (item
[l
].start
> s
)
4107 while (width
>= maxwidth
)
4109 width
-= ptr2cells(s
+ n
);
4110 n
+= (*mb_ptr2len
)(s
+ n
);
4115 n
= width
- maxwidth
+ 1;
4120 /* Fill up for half a double-wide character. */
4121 while (++width
< maxwidth
)
4128 --n
; /* count the '<' */
4129 for (; l
< itemcnt
; l
++)
4131 if (item
[l
].start
- n
>= s
)
4139 else if (width
< maxwidth
&& STRLEN(out
) + maxwidth
- width
+ 1 < outlen
)
4141 /* Apply STL_MIDDLE if any */
4142 for (l
= 0; l
< itemcnt
; l
++)
4143 if (item
[l
].type
== Middle
)
4147 p
= item
[l
].start
+ maxwidth
- width
;
4148 STRMOVE(p
, item
[l
].start
);
4149 for (s
= item
[l
].start
; s
< p
; s
++)
4151 for (l
++; l
< itemcnt
; l
++)
4152 item
[l
].start
+= maxwidth
- width
;
4157 /* Store the info about highlighting. */
4161 for (l
= 0; l
< itemcnt
; l
++)
4163 if (item
[l
].type
== Highlight
)
4165 sp
->start
= item
[l
].start
;
4166 sp
->userhl
= item
[l
].minwid
;
4174 /* Store the info about tab pages labels. */
4178 for (l
= 0; l
< itemcnt
; l
++)
4180 if (item
[l
].type
== TabPage
)
4182 sp
->start
= item
[l
].start
;
4183 sp
->userhl
= item
[l
].minwid
;
4193 #endif /* FEAT_STL_OPT */
4195 #if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4196 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
4198 * Get relative cursor position in window into "str[]", in the form 99%, using
4199 * "Top", "Bot" or "All" when appropriate.
4202 get_rel_pos(wp
, str
)
4206 long above
; /* number of lines above window */
4207 long below
; /* number of lines below window */
4209 above
= wp
->w_topline
- 1;
4211 above
+= diff_check_fill(wp
, wp
->w_topline
) - wp
->w_topfill
;
4213 below
= wp
->w_buffer
->b_ml
.ml_line_count
- wp
->w_botline
+ 1;
4215 STRCPY(str
, above
== 0 ? _("All") : _("Bot"));
4216 else if (above
<= 0)
4217 STRCPY(str
, _("Top"));
4219 sprintf((char *)str
, "%2d%%", above
> 1000000L
4220 ? (int)(above
/ ((above
+ below
) / 100L))
4221 : (int)(above
* 100L / (above
+ below
)));
4226 * Append (file 2 of 8) to 'buf', if editing more than one file.
4227 * Return TRUE if it was appended.
4230 append_arg_number(wp
, buf
, add_file
, maxlen
)
4233 int add_file
; /* Add "file" before the arg number */
4234 int maxlen
; /* maximum nr of chars in buf or zero*/
4238 if (ARGCOUNT
<= 1) /* nothing to do */
4241 p
= buf
+ STRLEN(buf
); /* go to the end of the buffer */
4242 if (maxlen
&& p
- buf
+ 35 >= maxlen
) /* getting too long */
4251 sprintf((char *)p
, wp
->w_arg_idx_invalid
? "(%d) of %d)"
4252 : "%d of %d)", wp
->w_arg_idx
+ 1, ARGCOUNT
);
4257 * If fname is not a full path, make it a full path.
4258 * Returns pointer to allocated memory (NULL for failure).
4265 * Force expanding the path always for Unix, because symbolic links may
4266 * mess up the full path name, even though it starts with a '/'.
4267 * Also expand when there is ".." in the file name, try to remove it,
4268 * because "c:/src/../README" is equal to "c:/README".
4269 * Similarly "c:/src//file" is equal to "c:/src/file".
4270 * For MS-Windows also expand names like "longna~1" to "longname".
4273 return FullName_save(fname
, TRUE
);
4275 if (!vim_isAbsName(fname
)
4276 || strstr((char *)fname
, "..") != NULL
4277 || strstr((char *)fname
, "//") != NULL
4278 # ifdef BACKSLASH_IN_FILENAME
4279 || strstr((char *)fname
, "\\\\") != NULL
4281 # if defined(MSWIN) || defined(DJGPP)
4282 || vim_strchr(fname
, '~') != NULL
4285 return FullName_save(fname
, FALSE
);
4287 fname
= vim_strsave(fname
);
4289 # ifdef USE_FNAME_CASE
4290 # ifdef USE_LONG_FNAME
4295 fname_case(fname
, 0); /* set correct case for file name */
4304 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4305 * "ffname" becomes a pointer to allocated memory (or NULL).
4309 fname_expand(buf
, ffname
, sfname
)
4314 if (*ffname
== NULL
) /* if no file name given, nothing to do */
4316 if (*sfname
== NULL
) /* if no short file name given, use ffname */
4318 *ffname
= fix_fname(*ffname
); /* expand to full path */
4320 #ifdef FEAT_SHORTCUT
4325 /* If the file name is a shortcut file, use the file it links to. */
4326 rfname
= mch_resolve_shortcut(*ffname
);
4338 * Get the file name for an argument list entry.
4346 /* Use the name from the associated buffer if it exists. */
4347 bp
= buflist_findnr(aep
->ae_fnum
);
4348 if (bp
== NULL
|| bp
->b_fname
== NULL
)
4349 return aep
->ae_fname
;
4353 #if defined(FEAT_WINDOWS) || defined(PROTO)
4355 * do_arg_all(): Open up to 'count' windows, one for each argument.
4358 do_arg_all(count
, forceit
, keep_tabs
)
4360 int forceit
; /* hide buffers in current windows */
4361 int keep_tabs
; /* keep current tabs, for ":tab drop file" */
4365 char_u
*opened
; /* array of flags for which args are open */
4366 int opened_len
; /* length of opened[] */
4367 int use_firstwin
= FALSE
; /* use first window for arglist */
4370 alist_T
*alist
; /* argument list to be used */
4373 int had_tab
= cmdmod
.tab
;
4374 win_T
*new_curwin
= NULL
;
4375 tabpage_T
*new_curtab
= NULL
;
4379 /* Don't give an error message. We don't want it when the ":all"
4380 * command is in the .vimrc. */
4385 opened_len
= ARGCOUNT
;
4386 opened
= alloc_clear((unsigned)opened_len
);
4391 need_mouse_correct
= TRUE
;
4395 * Try closing all windows that are not in the argument list.
4396 * Also close windows that are not full width;
4397 * When 'hidden' or "forceit" set the buffer becomes hidden.
4398 * Windows that have a changed buffer and can't be hidden won't be closed.
4399 * When the ":tab" modifier was used do this for all tab pages.
4402 goto_tabpage_tp(first_tabpage
);
4405 tpnext
= curtab
->tp_next
;
4406 for (wp
= firstwin
; wp
!= NULL
; wp
= wpnext
)
4408 wpnext
= wp
->w_next
;
4410 if (buf
->b_ffname
== NULL
4411 || buf
->b_nwindows
> 1
4412 #ifdef FEAT_VERTSPLIT
4413 || wp
->w_width
!= Columns
4419 /* check if the buffer in this window is in the arglist */
4420 for (i
= 0; i
< ARGCOUNT
; ++i
)
4422 if (ARGLIST
[i
].ae_fnum
== buf
->b_fnum
4423 || fullpathcmp(alist_name(&ARGLIST
[i
]),
4424 buf
->b_ffname
, TRUE
) & FPC_SAME
)
4432 new_curtab
= curtab
;
4435 if (wp
->w_alist
!= curwin
->w_alist
)
4437 /* Use the current argument list for all windows
4438 * containing a file from it. */
4439 alist_unlink(wp
->w_alist
);
4440 wp
->w_alist
= curwin
->w_alist
;
4441 ++wp
->w_alist
->al_refcount
;
4449 if (i
== ARGCOUNT
&& !keep_tabs
) /* close this window */
4451 if (P_HID(buf
) || forceit
|| buf
->b_nwindows
> 1
4452 || !bufIsChanged(buf
))
4454 /* If the buffer was changed, and we would like to hide it,
4455 * try autowriting. */
4456 if (!P_HID(buf
) && buf
->b_nwindows
<= 1
4457 && bufIsChanged(buf
))
4459 (void)autowrite(buf
, FALSE
);
4461 /* check if autocommands removed the window */
4462 if (!win_valid(wp
) || !buf_valid(buf
))
4464 wpnext
= firstwin
; /* start all over... */
4470 /* don't close last window */
4471 if (firstwin
== lastwin
&& first_tabpage
->tp_next
== NULL
)
4473 use_firstwin
= TRUE
;
4477 win_close(wp
, !P_HID(buf
) && !bufIsChanged(buf
));
4478 # ifdef FEAT_AUTOCMD
4479 /* check if autocommands removed the next window */
4480 if (!win_valid(wpnext
))
4481 wpnext
= firstwin
; /* start all over... */
4489 /* Without the ":tab" modifier only do the current tab page. */
4490 if (had_tab
== 0 || tpnext
== NULL
)
4493 # ifdef FEAT_AUTOCMD
4494 /* check if autocommands removed the next tab page */
4495 if (!valid_tabpage(tpnext
))
4496 tpnext
= first_tabpage
; /* start all over...*/
4498 goto_tabpage_tp(tpnext
);
4502 * Open a window for files in the argument list that don't have one.
4503 * ARGCOUNT may change while doing this, because of autocommands.
4505 if (count
> ARGCOUNT
|| count
<= 0)
4508 /* Autocommands may do anything to the argument list. Make sure it's not
4509 * freed while we are working here by "locking" it. We still have to
4510 * watch out for its size to be changed. */
4511 alist
= curwin
->w_alist
;
4512 ++alist
->al_refcount
;
4515 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4519 win_enter(lastwin
, FALSE
);
4521 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4522 * leaving an empty tab page when executed locally. */
4523 if (keep_tabs
&& bufempty() && curbuf
->b_nwindows
== 1
4524 && curbuf
->b_ffname
== NULL
&& !curbuf
->b_changed
)
4525 use_firstwin
= TRUE
;
4528 for (i
= 0; i
< count
&& i
< alist
->al_ga
.ga_len
&& !got_int
; ++i
)
4530 if (alist
== &global_alist
&& i
== global_alist
.al_ga
.ga_len
- 1)
4531 arg_had_last
= TRUE
;
4532 if (i
< opened_len
&& opened
[i
])
4534 /* Move the already present window to below the current window */
4535 if (curwin
->w_arg_idx
!= i
)
4537 for (wpnext
= firstwin
; wpnext
!= NULL
; wpnext
= wpnext
->w_next
)
4539 if (wpnext
->w_arg_idx
== i
)
4541 win_move_after(wpnext
, curwin
);
4547 else if (split_ret
== OK
)
4549 if (!use_firstwin
) /* split current window */
4552 p_ea
= TRUE
; /* use space from all windows */
4553 split_ret
= win_split(0, WSP_ROOM
| WSP_BELOW
);
4555 if (split_ret
== FAIL
)
4559 else /* first window: do autocmd for leaving this buffer */
4566 curwin
->w_arg_idx
= i
;
4569 new_curwin
= curwin
;
4570 new_curtab
= curtab
;
4572 (void)do_ecmd(0, alist_name(&AARGLIST(alist
)[i
]), NULL
, NULL
,
4574 ((P_HID(curwin
->w_buffer
)
4575 || bufIsChanged(curwin
->w_buffer
)) ? ECMD_HIDE
: 0)
4576 + ECMD_OLDBUF
, curwin
);
4581 use_firstwin
= FALSE
;
4585 /* When ":tab" was used open a new tab for a new window repeatedly. */
4586 if (had_tab
> 0 && tabpage_index(NULL
) <= p_tpm
)
4590 /* Remove the "lock" on the argument list. */
4591 alist_unlink(alist
);
4596 /* to window with first arg */
4597 if (valid_tabpage(new_curtab
))
4598 goto_tabpage_tp(new_curtab
);
4599 if (win_valid(new_curwin
))
4600 win_enter(new_curwin
, FALSE
);
4608 # if defined(FEAT_LISTCMDS) || defined(PROTO)
4610 * Open a window for a number of buffers.
4622 int count
; /* Maximum number of windows to open. */
4623 int all
; /* When TRUE also load inactive buffers. */
4625 int had_tab
= cmdmod
.tab
;
4629 if (eap
->addr_count
== 0) /* make as many windows as possible */
4632 count
= eap
->line2
; /* make as many windows as specified */
4633 if (eap
->cmdidx
== CMD_unhide
|| eap
->cmdidx
== CMD_sunhide
)
4641 need_mouse_correct
= TRUE
;
4645 * Close superfluous windows (two windows for the same buffer).
4646 * Also close windows that are not full-width.
4650 goto_tabpage_tp(first_tabpage
);
4654 tpnext
= curtab
->tp_next
;
4655 for (wp
= firstwin
; wp
!= NULL
; wp
= wpnext
)
4657 wpnext
= wp
->w_next
;
4658 if ((wp
->w_buffer
->b_nwindows
> 1
4659 #ifdef FEAT_VERTSPLIT
4660 || ((cmdmod
.split
& WSP_VERT
)
4661 ? wp
->w_height
+ wp
->w_status_height
< Rows
- p_ch
4663 : wp
->w_width
!= Columns
)
4666 || (had_tab
> 0 && wp
!= firstwin
)
4668 ) && firstwin
!= lastwin
)
4670 win_close(wp
, FALSE
);
4672 wpnext
= firstwin
; /* just in case an autocommand does
4673 something strange with windows */
4674 tpnext
= first_tabpage
; /* start all over...*/
4683 /* Without the ":tab" modifier only do the current tab page. */
4684 if (had_tab
== 0 || tpnext
== NULL
)
4686 goto_tabpage_tp(tpnext
);
4691 * Go through the buffer list. When a buffer doesn't have a window yet,
4692 * open one. Otherwise move the window to the right position.
4693 * Watch out for autocommands that delete buffers or windows!
4696 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4699 win_enter(lastwin
, FALSE
);
4703 for (buf
= firstbuf
; buf
!= NULL
&& open_wins
< count
; buf
= buf
->b_next
)
4705 /* Check if this buffer needs a window */
4706 if ((!all
&& buf
->b_ml
.ml_mfp
== NULL
) || !buf
->b_p_bl
)
4712 /* With the ":tab" modifier don't move the window. */
4713 if (buf
->b_nwindows
> 0)
4714 wp
= lastwin
; /* buffer has a window, skip it */
4721 /* Check if this buffer already has a window */
4722 for (wp
= firstwin
; wp
!= NULL
; wp
= wp
->w_next
)
4723 if (wp
->w_buffer
== buf
)
4725 /* If the buffer already has a window, move it */
4727 win_move_after(wp
, curwin
);
4730 if (wp
== NULL
&& split_ret
== OK
)
4732 /* Split the window and put the buffer in it */
4734 p_ea
= TRUE
; /* use space from all windows */
4735 split_ret
= win_split(0, WSP_ROOM
| WSP_BELOW
);
4738 if (split_ret
== FAIL
)
4741 /* Open the buffer in this window. */
4742 #if defined(HAS_SWAP_EXISTS_ACTION)
4743 swap_exists_action
= SEA_DIALOG
;
4745 set_curbuf(buf
, DOBUF_GOTO
);
4747 if (!buf_valid(buf
)) /* autocommands deleted the buffer!!! */
4749 #if defined(HAS_SWAP_EXISTS_ACTION)
4750 swap_exists_action
= SEA_NONE
;
4755 #if defined(HAS_SWAP_EXISTS_ACTION)
4756 if (swap_exists_action
== SEA_QUIT
)
4758 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4761 /* Reset the error/interrupt/exception state here so that
4762 * aborting() returns FALSE when closing a window. */
4766 /* User selected Quit at ATTENTION prompt; close this window. */
4767 win_close(curwin
, TRUE
);
4769 swap_exists_action
= SEA_NONE
;
4770 swap_exists_did_quit
= TRUE
;
4772 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4773 /* Restore the error/interrupt/exception state if not
4774 * discarded by a new aborting error, interrupt, or uncaught
4780 handle_swap_exists(NULL
);
4787 (void)vgetc(); /* only break the file loading, not the rest */
4791 /* Autocommands deleted the buffer or aborted script processing!!! */
4796 /* When ":tab" was used open a new tab for a new window repeatedly. */
4797 if (had_tab
> 0 && tabpage_index(NULL
) <= p_tpm
)
4804 win_enter(firstwin
, FALSE
); /* back to first window */
4810 * Close superfluous windows.
4812 for (wp
= lastwin
; open_wins
> count
; )
4814 r
= (P_HID(wp
->w_buffer
) || !bufIsChanged(wp
->w_buffer
)
4815 || autowrite(wp
->w_buffer
, FALSE
) == OK
);
4819 /* BufWrite Autocommands made the window invalid, start over */
4826 win_close(wp
, !P_HID(wp
->w_buffer
));
4838 # endif /* FEAT_LISTCMDS */
4840 #endif /* FEAT_WINDOWS */
4842 static int chk_modeline
__ARGS((linenr_T
, int));
4845 * do_modelines() - process mode lines for the current file
4848 * OPT_WINONLY only set options local to window
4849 * OPT_NOWIN don't set options local to window
4851 * Returns immediately if the "ml" option isn't set.
4859 static int entered
= 0;
4861 if (!curbuf
->b_p_ml
|| (nmlines
= (int)p_mls
) == 0)
4864 /* Disallow recursive entry here. Can happen when executing a modeline
4865 * triggers an autocommand, which reloads modelines with a ":do". */
4870 for (lnum
= 1; lnum
<= curbuf
->b_ml
.ml_line_count
&& lnum
<= nmlines
;
4872 if (chk_modeline(lnum
, flags
) == FAIL
)
4875 for (lnum
= curbuf
->b_ml
.ml_line_count
; lnum
> 0 && lnum
> nmlines
4876 && lnum
> curbuf
->b_ml
.ml_line_count
- nmlines
; --lnum
)
4877 if (chk_modeline(lnum
, flags
) == FAIL
)
4882 #include "version.h" /* for version number */
4885 * chk_modeline() - check a single line for a mode string
4886 * Return FAIL if an error encountered.
4889 chk_modeline(lnum
, flags
)
4891 int flags
; /* Same as for do_modelines(). */
4895 char_u
*linecopy
; /* local copy of any modeline found */
4900 char_u
*save_sourcing_name
;
4901 linenr_T save_sourcing_lnum
;
4907 for (s
= ml_get(lnum
); *s
!= NUL
; ++s
)
4909 if (prev
== -1 || vim_isspace(prev
))
4911 if ((prev
!= -1 && STRNCMP(s
, "ex:", (size_t)3) == 0)
4912 || STRNCMP(s
, "vi:", (size_t)3) == 0)
4914 if (STRNCMP(s
, "vim", 3) == 0)
4916 if (s
[3] == '<' || s
[3] == '=' || s
[3] == '>')
4920 vers
= getdigits(&e
);
4923 || (VIM_VERSION_100
>= vers
&& isdigit(s
[3]))
4924 || (VIM_VERSION_100
< vers
&& s
[3] == '<')
4925 || (VIM_VERSION_100
> vers
&& s
[3] == '>')
4926 || (VIM_VERSION_100
== vers
&& s
[3] == '=')))
4935 do /* skip over "ex:", "vi:" or "vim:" */
4937 while (s
[-1] != ':');
4939 s
= linecopy
= vim_strsave(s
); /* copy the line, it will change */
4940 if (linecopy
== NULL
)
4943 save_sourcing_lnum
= sourcing_lnum
;
4944 save_sourcing_name
= sourcing_name
;
4945 sourcing_lnum
= lnum
; /* prepare for emsg() */
4946 sourcing_name
= (char_u
*)"modelines";
4949 while (end
== FALSE
)
4956 * Find end of set command: ':' or end of line.
4957 * Skip over "\:", replacing it with ":".
4959 for (e
= s
; *e
!= ':' && *e
!= NUL
; ++e
)
4960 if (e
[0] == '\\' && e
[1] == ':')
4966 * If there is a "set" command, require a terminating ':' and
4967 * ignore the stuff after the ':'.
4968 * "vi:set opt opt opt: foo" -- foo not interpreted
4969 * "vi:opt opt opt: foo" -- foo interpreted
4970 * Accept "se" for compatibility with Elvis.
4972 if (STRNCMP(s
, "set ", (size_t)4) == 0
4973 || STRNCMP(s
, "se ", (size_t)3) == 0)
4975 if (*e
!= ':') /* no terminating ':'? */
4978 s
= vim_strchr(s
, ' ') + 1;
4980 *e
= NUL
; /* truncate the set command */
4982 if (*s
!= NUL
) /* skip over an empty "::" */
4985 save_SID
= current_SID
;
4986 current_SID
= SID_MODELINE
;
4988 retval
= do_set(s
, OPT_MODELINE
| OPT_LOCAL
| flags
);
4990 current_SID
= save_SID
;
4992 if (retval
== FAIL
) /* stop if error found */
4995 s
= e
+ 1; /* advance to next part */
4998 sourcing_lnum
= save_sourcing_lnum
;
4999 sourcing_name
= save_sourcing_name
;
5006 #if defined(FEAT_VIMINFO) || defined(PROTO)
5008 read_viminfo_bufferlist(virp
, writing
)
5019 /* Handle long line and escaped characters. */
5020 xline
= viminfo_readstring(virp
, 1, FALSE
);
5022 /* don't read in if there are files on the command-line or if writing: */
5023 if (xline
!= NULL
&& !writing
&& ARGCOUNT
== 0
5024 && find_viminfo_parameter('%') != NULL
)
5026 /* Format is: <fname> Tab <lnum> Tab <col>.
5027 * Watch out for a Tab in the file name, work from the end. */
5030 tab
= vim_strrchr(xline
, '\t');
5034 col
= atoi((char *)tab
);
5035 tab
= vim_strrchr(xline
, '\t');
5039 lnum
= atol((char *)tab
);
5043 /* Expand "~/" in the file name at "line + 1" to a full path.
5044 * Then try shortening it by comparing with the current directory */
5045 expand_env(xline
, NameBuff
, MAXPATHL
);
5046 sfname
= shorten_fname1(NameBuff
);
5048 buf
= buflist_new(NameBuff
, sfname
, (linenr_T
)0, BLN_LISTED
);
5049 if (buf
!= NULL
) /* just in case... */
5051 buf
->b_last_cursor
.lnum
= lnum
;
5052 buf
->b_last_cursor
.col
= col
;
5053 buflist_setfpos(buf
, curwin
, lnum
, col
, FALSE
);
5058 return viminfo_readline(virp
);
5062 write_viminfo_bufferlist(fp
)
5073 if (find_viminfo_parameter('%') == NULL
)
5076 /* Without a number -1 is returned: do all buffers. */
5077 max_buffers
= get_viminfo_parameter('%');
5079 /* Allocate room for the file name, lnum and col. */
5080 line
= alloc(MAXPATHL
+ 40);
5085 FOR_ALL_TAB_WINDOWS(tp
, win
)
5086 set_last_cursor(win
);
5088 set_last_cursor(curwin
);
5091 fprintf(fp
, _("\n# Buffer list:\n"));
5092 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
5094 if (buf
->b_fname
== NULL
5096 #ifdef FEAT_QUICKFIX
5099 || removable(buf
->b_ffname
))
5102 if (max_buffers
-- == 0)
5105 home_replace(NULL
, buf
->b_ffname
, line
, MAXPATHL
, TRUE
);
5106 sprintf((char *)line
+ STRLEN(line
), "\t%ld\t%d",
5107 (long)buf
->b_last_cursor
.lnum
,
5108 buf
->b_last_cursor
.col
);
5109 viminfo_writestring(fp
, line
);
5117 * Return special buffer name.
5118 * Returns NULL when the buffer has a normal file name.
5124 #if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5125 if (bt_quickfix(buf
))
5131 * For location list window, w_llist_ref points to the location list.
5132 * For quickfix window, w_llist_ref is NULL.
5134 FOR_ALL_TAB_WINDOWS(tp
, win
)
5135 if (win
->w_buffer
== buf
)
5138 if (win
!= NULL
&& win
->w_llist_ref
!= NULL
)
5139 return _("[Location List]");
5141 return _("[Quickfix List]");
5144 #ifdef FEAT_QUICKFIX
5145 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5146 * contains the name as specified by the user */
5149 if (buf
->b_sfname
!= NULL
)
5150 return (char *)buf
->b_sfname
;
5151 return _("[Scratch]");
5154 if (buf
->b_fname
== NULL
)
5155 return _("[No Name]");
5160 #if defined(FEAT_SIGNS) || defined(PROTO)
5162 * Insert the sign into the signlist.
5165 insert_sign(buf
, prev
, next
, id
, lnum
, typenr
)
5166 buf_T
*buf
; /* buffer to store sign in */
5167 signlist_T
*prev
; /* previous sign entry */
5168 signlist_T
*next
; /* next sign entry */
5169 int id
; /* sign ID */
5170 linenr_T lnum
; /* line number which gets the mark */
5171 int typenr
; /* typenr of sign we are adding */
5173 signlist_T
*newsign
;
5175 newsign
= (signlist_T
*)lalloc((long_u
)sizeof(signlist_T
), FALSE
);
5176 if (newsign
!= NULL
)
5179 newsign
->lnum
= lnum
;
5180 newsign
->typenr
= typenr
;
5181 newsign
->next
= next
;
5182 #ifdef FEAT_NETBEANS_INTG
5183 newsign
->prev
= prev
;
5185 next
->prev
= newsign
;
5190 /* When adding first sign need to redraw the windows to create the
5191 * column for signs. */
5192 if (buf
->b_signlist
== NULL
)
5194 redraw_buf_later(buf
, NOT_VALID
);
5195 changed_cline_bef_curs();
5198 /* first sign in signlist */
5199 buf
->b_signlist
= newsign
;
5202 prev
->next
= newsign
;
5207 * Add the sign into the signlist. Find the right spot to do it though.
5210 buf_addsign(buf
, id
, lnum
, typenr
)
5211 buf_T
*buf
; /* buffer to store sign in */
5212 int id
; /* sign ID */
5213 linenr_T lnum
; /* line number which gets the mark */
5214 int typenr
; /* typenr of sign we are adding */
5216 signlist_T
*sign
; /* a sign in the signlist */
5217 signlist_T
*prev
; /* the previous sign */
5220 for (sign
= buf
->b_signlist
; sign
!= NULL
; sign
= sign
->next
)
5222 if (lnum
== sign
->lnum
&& id
== sign
->id
)
5224 sign
->typenr
= typenr
;
5228 #ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5233 #ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5234 /* XXX - GRP: Is this because of sign slide problem? Or is it
5235 * really needed? Or is it because we allow multiple signs per
5236 * line? If so, should I add that feature to FEAT_SIGNS?
5238 while (prev
!= NULL
&& prev
->lnum
== lnum
)
5241 sign
= buf
->b_signlist
;
5245 insert_sign(buf
, prev
, sign
, id
, lnum
, typenr
);
5250 #ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5251 /* XXX - GRP: See previous comment */
5252 while (prev
!= NULL
&& prev
->lnum
== lnum
)
5255 sign
= buf
->b_signlist
;
5259 insert_sign(buf
, prev
, sign
, id
, lnum
, typenr
);
5265 buf_change_sign_type(buf
, markId
, typenr
)
5266 buf_T
*buf
; /* buffer to store sign in */
5267 int markId
; /* sign ID */
5268 int typenr
; /* typenr of sign we are adding */
5270 signlist_T
*sign
; /* a sign in the signlist */
5272 for (sign
= buf
->b_signlist
; sign
!= NULL
; sign
= sign
->next
)
5274 if (sign
->id
== markId
)
5276 sign
->typenr
= typenr
;
5285 buf_getsigntype(buf
, lnum
, type
)
5288 int type
; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5290 signlist_T
*sign
; /* a sign in a b_signlist */
5292 for (sign
= buf
->b_signlist
; sign
!= NULL
; sign
= sign
->next
)
5293 if (sign
->lnum
== lnum
5294 && (type
== SIGN_ANY
5295 # ifdef FEAT_SIGN_ICONS
5296 || (type
== SIGN_ICON
5297 && sign_get_image(sign
->typenr
) != NULL
)
5299 || (type
== SIGN_TEXT
5300 && sign_get_text(sign
->typenr
) != NULL
)
5301 || (type
== SIGN_LINEHL
5302 && sign_get_attr(sign
->typenr
, TRUE
) != 0)))
5303 return sign
->typenr
;
5309 buf_delsign(buf
, id
)
5310 buf_T
*buf
; /* buffer sign is stored in */
5311 int id
; /* sign id */
5313 signlist_T
**lastp
; /* pointer to pointer to current sign */
5314 signlist_T
*sign
; /* a sign in a b_signlist */
5315 signlist_T
*next
; /* the next sign in a b_signlist */
5316 linenr_T lnum
; /* line number whose sign was deleted */
5318 lastp
= &buf
->b_signlist
;
5320 for (sign
= buf
->b_signlist
; sign
!= NULL
; sign
= next
)
5326 #ifdef FEAT_NETBEANS_INTG
5328 next
->prev
= sign
->prev
;
5335 lastp
= &sign
->next
;
5338 /* When deleted the last sign need to redraw the windows to remove the
5340 if (buf
->b_signlist
== NULL
)
5342 redraw_buf_later(buf
, NOT_VALID
);
5343 changed_cline_bef_curs();
5351 * Find the line number of the sign with the requested id. If the sign does
5352 * not exist, return 0 as the line number. This will still let the correct file
5356 buf_findsign(buf
, id
)
5357 buf_T
*buf
; /* buffer to store sign in */
5358 int id
; /* sign ID */
5360 signlist_T
*sign
; /* a sign in the signlist */
5362 for (sign
= buf
->b_signlist
; sign
!= NULL
; sign
= sign
->next
)
5370 buf_findsign_id(buf
, lnum
)
5371 buf_T
*buf
; /* buffer whose sign we are searching for */
5372 linenr_T lnum
; /* line number of sign */
5374 signlist_T
*sign
; /* a sign in the signlist */
5376 for (sign
= buf
->b_signlist
; sign
!= NULL
; sign
= sign
->next
)
5377 if (sign
->lnum
== lnum
)
5384 # if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5385 /* see if a given type of sign exists on a specific line */
5387 buf_findsigntype_id(buf
, lnum
, typenr
)
5388 buf_T
*buf
; /* buffer whose sign we are searching for */
5389 linenr_T lnum
; /* line number of sign */
5390 int typenr
; /* sign type number */
5392 signlist_T
*sign
; /* a sign in the signlist */
5394 for (sign
= buf
->b_signlist
; sign
!= NULL
; sign
= sign
->next
)
5395 if (sign
->lnum
== lnum
&& sign
->typenr
== typenr
)
5402 # if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5403 /* return the number of icons on the given line */
5405 buf_signcount(buf
, lnum
)
5409 signlist_T
*sign
; /* a sign in the signlist */
5412 for (sign
= buf
->b_signlist
; sign
!= NULL
; sign
= sign
->next
)
5413 if (sign
->lnum
== lnum
)
5414 if (sign_get_image(sign
->typenr
) != NULL
)
5419 # endif /* FEAT_SIGN_ICONS */
5420 # endif /* FEAT_NETBEANS_INTG */
5424 * Delete signs in buffer "buf".
5427 buf_delete_signs(buf
)
5432 while (buf
->b_signlist
!= NULL
)
5434 next
= buf
->b_signlist
->next
;
5435 vim_free(buf
->b_signlist
);
5436 buf
->b_signlist
= next
;
5441 * Delete all signs in all buffers.
5444 buf_delete_all_signs()
5446 buf_T
*buf
; /* buffer we are checking for signs */
5448 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
5449 if (buf
->b_signlist
!= NULL
)
5451 /* Need to redraw the windows to remove the sign column. */
5452 redraw_buf_later(buf
, NOT_VALID
);
5453 buf_delete_signs(buf
);
5458 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5461 sign_list_placed(rbuf
)
5468 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5476 if (buf
->b_signlist
!= NULL
)
5478 vim_snprintf(lbuf
, BUFSIZ
, _("Signs for %s:"), buf
->b_fname
);
5479 MSG_PUTS_ATTR(lbuf
, hl_attr(HLF_D
));
5482 for (p
= buf
->b_signlist
; p
!= NULL
; p
= p
->next
)
5484 vim_snprintf(lbuf
, BUFSIZ
, _(" line=%ld id=%d name=%s"),
5485 (long)p
->lnum
, p
->id
, sign_typenr2name(p
->typenr
));
5496 * Adjust a placed sign for inserted/deleted lines.
5499 sign_mark_adjust(line1
, line2
, amount
, amount_after
)
5505 signlist_T
*sign
; /* a sign in a b_signlist */
5507 for (sign
= curbuf
->b_signlist
; sign
!= NULL
; sign
= sign
->next
)
5509 if (sign
->lnum
>= line1
&& sign
->lnum
<= line2
)
5511 if (amount
== MAXLNUM
)
5514 sign
->lnum
+= amount
;
5516 else if (sign
->lnum
> line2
)
5517 sign
->lnum
+= amount_after
;
5520 #endif /* FEAT_SIGNS */
5523 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5529 if (on
!= curbuf
->b_p_bl
)
5531 curbuf
->b_p_bl
= on
;
5534 apply_autocmds(EVENT_BUFADD
, NULL
, NULL
, FALSE
, curbuf
);
5536 apply_autocmds(EVENT_BUFDELETE
, NULL
, NULL
, FALSE
, curbuf
);
5542 * Read the file for "buf" again and check if the contents changed.
5543 * Return TRUE if it changed or this could not be checked.
5546 buf_contents_changed(buf
)
5555 /* Allocate a buffer without putting it in the buffer list. */
5556 newbuf
= buflist_new(NULL
, NULL
, (linenr_T
)1, BLN_DUMMY
);
5560 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5561 if (prep_exarg(&ea
, buf
) == FAIL
)
5563 wipe_buffer(newbuf
, FALSE
);
5567 /* set curwin/curbuf to buf and save a few things */
5568 aucmd_prepbuf(&aco
, newbuf
);
5570 if (ml_open(curbuf
) == OK
5571 && readfile(buf
->b_ffname
, buf
->b_fname
,
5572 (linenr_T
)0, (linenr_T
)0, (linenr_T
)MAXLNUM
,
5573 &ea
, READ_NEW
| READ_DUMMY
) == OK
)
5575 /* compare the two files line by line */
5576 if (buf
->b_ml
.ml_line_count
== curbuf
->b_ml
.ml_line_count
)
5579 for (lnum
= 1; lnum
<= curbuf
->b_ml
.ml_line_count
; ++lnum
)
5580 if (STRCMP(ml_get_buf(buf
, lnum
, FALSE
), ml_get(lnum
)) != 0)
5589 /* restore curwin/curbuf and a few other things */
5590 aucmd_restbuf(&aco
);
5592 if (curbuf
!= newbuf
) /* safety check */
5593 wipe_buffer(newbuf
, FALSE
);
5599 * Wipe out a buffer and decrement the last buffer number if it was used for
5600 * this buffer. Call this to wipe out a temp buffer that does not contain any
5605 wipe_buffer(buf
, aucmd
)
5607 int aucmd
; /* When TRUE trigger autocommands. */
5609 if (buf
->b_fnum
== top_file_num
- 1)
5613 if (!aucmd
) /* Don't trigger BufDelete autocommands here. */
5616 close_buffer(NULL
, buf
, DOBUF_WIPE
);