1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
11 * buffer.c: functions for dealing with the buffer structure
15 * The buffer list is a double linked list of all buffers.
16 * Each buffer can be in one of these states:
17 * never loaded: BF_NEVERLOADED is set, only the file name is valid
18 * not loaded: b_ml.ml_mfp == NULL, no memfile allocated
19 * hidden: b_nwindows == 0, loaded but not displayed in a window
20 * normal: loaded and displayed in a window
22 * Instead of storing file names all over the place, each file name is
23 * stored in the buffer list. It can be referenced by a number.
25 * The current implementation remembers all file names ever used.
30 #if defined(FEAT_CMDL_COMPL) || defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL)
31 static char_u
*buflist_match
__ARGS((regprog_T
*prog
, buf_T
*buf
));
32 # define HAVE_BUFLIST_MATCH
33 static char_u
*fname_match
__ARGS((regprog_T
*prog
, char_u
*name
));
35 static void buflist_setfpos
__ARGS((buf_T
*buf
, win_T
*win
, linenr_T lnum
, colnr_T col
, int copy_options
));
36 static wininfo_T
*find_wininfo
__ARGS((buf_T
*buf
));
38 static buf_T
*buflist_findname_stat
__ARGS((char_u
*ffname
, struct stat
*st
));
39 static int otherfile_buf
__ARGS((buf_T
*buf
, char_u
*ffname
, struct stat
*stp
));
40 static int buf_same_ino
__ARGS((buf_T
*buf
, struct stat
*stp
));
42 static int otherfile_buf
__ARGS((buf_T
*buf
, char_u
*ffname
));
45 static int ti_change
__ARGS((char_u
*str
, char_u
**last
));
47 static void free_buffer
__ARGS((buf_T
*));
48 static void free_buffer_stuff
__ARGS((buf_T
*buf
, int free_options
));
49 static void clear_wininfo
__ARGS((buf_T
*buf
));
54 # define dev_T unsigned
57 #if defined(FEAT_SIGNS)
58 static void insert_sign
__ARGS((buf_T
*buf
, signlist_T
*prev
, signlist_T
*next
, int id
, linenr_T lnum
, int typenr
));
59 static void buf_delete_signs
__ARGS((buf_T
*buf
));
63 * Open current buffer, that is: open the memfile and read the file into memory
64 * return FAIL for failure, OK otherwise
67 open_buffer(read_stdin
, eap
)
68 int read_stdin
; /* read file from stdin */
69 exarg_T
*eap
; /* for forced 'ff' and 'fenc' or NULL */
77 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
78 * When re-entering the same buffer, it should not change, because the
79 * user may have reset the flag by hand.
81 if (readonlymode
&& curbuf
->b_ffname
!= NULL
82 && (curbuf
->b_flags
& BF_NEVERLOADED
))
83 curbuf
->b_p_ro
= TRUE
;
85 if (ml_open(curbuf
) == FAIL
)
88 * There MUST be a memfile, otherwise we can't do anything
89 * If we can't create one for the current buffer, take another buffer
91 close_buffer(NULL
, curbuf
, 0);
92 for (curbuf
= firstbuf
; curbuf
!= NULL
; curbuf
= curbuf
->b_next
)
93 if (curbuf
->b_ml
.ml_mfp
!= NULL
)
96 * if there is no memfile at all, exit
97 * This is OK, since there are no changes to loose.
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;
176 apply_autocmds_retval(EVENT_STDINREADPOST
, NULL
, NULL
, FALSE
,
179 apply_autocmds(EVENT_STDINREADPOST
, NULL
, NULL
, FALSE
, curbuf
);
185 /* if first time loading this buffer, init b_chartab[] */
186 if (curbuf
->b_flags
& BF_NEVERLOADED
)
187 (void)buf_init_chartab(curbuf
, FALSE
);
190 * Set/reset the Changed flag first, autocmds may change the buffer.
191 * Apply the automatic commands, before processing the modelines.
192 * So the modelines have priority over auto commands.
194 /* When reading stdin, the buffer contents always needs writing, so set
195 * the changed flag. Unless in readonly mode: "ls | gview -".
196 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
197 if ((read_stdin
&& !readonlymode
&& !bufempty())
199 || modified_was_set
/* ":set modified" used in autocmd */
201 || (aborting() && vim_strchr(p_cpo
, CPO_INTMOD
) != NULL
)
204 || (got_int
&& vim_strchr(p_cpo
, CPO_INTMOD
) != NULL
))
206 else if (retval
!= FAIL
)
207 unchanged(curbuf
, FALSE
);
208 save_file_ff(curbuf
); /* keep this fileformat */
210 /* require "!" to overwrite the file, because it wasn't read completely */
216 curbuf
->b_flags
|= BF_READERR
;
219 /* Need to update automatic folding. Do this before the autocommands,
220 * they may use the fold info. */
221 foldUpdateAll(curwin
);
225 /* need to set w_topline, unless some autocommand already did that. */
226 if (!(curwin
->w_valid
& VALID_TOPLINE
))
228 curwin
->w_topline
= 1;
230 curwin
->w_topfill
= 0;
234 apply_autocmds_retval(EVENT_BUFENTER
, NULL
, NULL
, FALSE
, curbuf
, &retval
);
236 apply_autocmds(EVENT_BUFENTER
, NULL
, NULL
, FALSE
, curbuf
);
244 * The autocommands may have changed the current buffer. Apply the
245 * modelines to the correct buffer, if it still exists and is loaded.
247 if (buf_valid(old_curbuf
) && old_curbuf
->b_ml
.ml_mfp
!= NULL
)
251 /* Go to the buffer that was opened. */
252 aucmd_prepbuf(&aco
, old_curbuf
);
255 curbuf
->b_flags
&= ~(BF_CHECK_RO
| BF_NEVERLOADED
);
259 apply_autocmds_retval(EVENT_BUFWINENTER
, NULL
, NULL
, FALSE
, curbuf
,
262 apply_autocmds(EVENT_BUFWINENTER
, NULL
, NULL
, FALSE
, curbuf
);
265 /* restore curwin/curbuf and a few other things */
275 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
283 for (bp
= firstbuf
; bp
!= NULL
; bp
= bp
->b_next
)
290 * Close the link to a buffer.
291 * "action" is used when there is no longer a window for the buffer.
293 * 0 buffer becomes hidden
294 * DOBUF_UNLOAD buffer is unloaded
295 * DOBUF_DELETE buffer is unloaded and removed from buffer list
296 * DOBUF_WIPE buffer is unloaded and really deleted
297 * When doing all but the first one on the current buffer, the caller should
298 * get a new buffer very soon!
300 * The 'bufhidden' option can force freeing and deleting.
303 close_buffer(win
, buf
, action
)
304 win_T
*win
; /* if not NULL, set b_last_cursor */
310 int nwindows
= buf
->b_nwindows
;
312 int unload_buf
= (action
!= 0);
313 int del_buf
= (action
== DOBUF_DEL
|| action
== DOBUF_WIPE
);
314 int wipe_buf
= (action
== DOBUF_WIPE
);
318 * Force unloading or deleting when 'bufhidden' says so.
319 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
320 * "hide" (otherwise we could never free or delete a buffer).
322 if (buf
->b_p_bh
[0] == 'd') /* 'bufhidden' == "delete" */
327 else if (buf
->b_p_bh
[0] == 'w') /* 'bufhidden' == "wipe" */
333 else if (buf
->b_p_bh
[0] == 'u') /* 'bufhidden' == "unload" */
339 /* Set b_last_cursor when closing the last window for the buffer.
340 * Remember the last cursor position and window options of the buffer.
341 * This used to be only for the current window, but then options like
342 * 'foldmethod' may be lost with a ":only" command. */
343 if (buf
->b_nwindows
== 1)
344 set_last_cursor(win
);
345 buflist_setfpos(buf
, win
,
346 win
->w_cursor
.lnum
== 1 ? 0 : win
->w_cursor
.lnum
,
347 win
->w_cursor
.col
, TRUE
);
351 /* When the buffer is no longer in a window, trigger BufWinLeave */
352 if (buf
->b_nwindows
== 1)
354 apply_autocmds(EVENT_BUFWINLEAVE
, buf
->b_fname
, buf
->b_fname
,
356 if (!buf_valid(buf
)) /* autocommands may delete the buffer */
359 /* When the buffer becomes hidden, but is not unloaded, trigger
363 apply_autocmds(EVENT_BUFHIDDEN
, buf
->b_fname
, buf
->b_fname
,
365 if (!buf_valid(buf
)) /* autocmds may delete the buffer */
369 if (aborting()) /* autocmds may abort script processing */
373 nwindows
= buf
->b_nwindows
;
376 /* decrease the link count from windows (unless not in any window) */
377 if (buf
->b_nwindows
> 0)
380 /* Return when a window is displaying the buffer or when it's not
382 if (buf
->b_nwindows
> 0 || !unload_buf
)
384 #if 0 /* why was this here? */
386 u_sync(); /* sync undo before going to another buffer */
391 /* Always remove the buffer when there is no file name. */
392 if (buf
->b_ffname
== NULL
)
396 * Free all things allocated for this buffer.
397 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
400 /* Remember if we are closing the current buffer. Restore the number of
401 * windows, so that autocommands in buf_freeall() don't get confused. */
402 is_curbuf
= (buf
== curbuf
);
403 buf
->b_nwindows
= nwindows
;
406 buf_freeall(buf
, del_buf
, wipe_buf
);
409 /* Autocommands may have deleted the buffer. */
413 if (aborting()) /* autocmds may abort script processing */
417 /* Autocommands may have opened or closed windows for this buffer.
418 * Decrement the count for the close we do here. */
419 if (buf
->b_nwindows
> 0)
423 * It's possible that autocommands change curbuf to the one being deleted.
424 * This might cause the previous curbuf to be deleted unexpectedly. But
425 * in some cases it's OK to delete the curbuf, because a new one is
426 * obtained anyway. Therefore only return if curbuf changed to the
429 if (buf
== curbuf
&& !is_curbuf
)
433 #ifdef FEAT_NETBEANS_INTG
435 netbeans_file_closed(buf
);
437 /* Change directories when the 'acd' option is set. */
441 * Remove the buffer from the list.
445 #ifdef FEAT_SUN_WORKSHOP
446 if (usingSunWorkShop
)
447 workshop_file_closed_lineno((char *)buf
->b_ffname
,
448 (int)buf
->b_last_cursor
.lnum
);
450 vim_free(buf
->b_ffname
);
451 vim_free(buf
->b_sfname
);
452 if (buf
->b_prev
== NULL
)
453 firstbuf
= buf
->b_next
;
455 buf
->b_prev
->b_next
= buf
->b_next
;
456 if (buf
->b_next
== NULL
)
457 lastbuf
= buf
->b_prev
;
459 buf
->b_next
->b_prev
= buf
->b_prev
;
466 /* Free all internal variables and reset option values, to make
467 * ":bdel" compatible with Vim 5.7. */
468 free_buffer_stuff(buf
, TRUE
);
470 /* Make it look like a new buffer. */
471 buf
->b_flags
= BF_CHECK_RO
| BF_NEVERLOADED
;
473 /* Init the options when loaded again. */
474 buf
->b_p_initialized
= FALSE
;
483 * Make buffer not contain a file.
489 buf
->b_ml
.ml_line_count
= 1;
490 unchanged(buf
, TRUE
);
492 buf
->b_shortname
= FALSE
;
495 buf
->b_start_eol
= TRUE
;
497 buf
->b_p_bomb
= FALSE
;
499 buf
->b_ml
.ml_mfp
= NULL
;
500 buf
->b_ml
.ml_flags
= ML_EMPTY
; /* empty buffer */
501 #ifdef FEAT_NETBEANS_INTG
502 netbeans_deleted_all_lines(buf
);
507 * buf_freeall() - free all things allocated for a buffer that are related to
512 buf_freeall(buf
, del_buf
, wipe_buf
)
514 int del_buf
; /* buffer is going to be deleted */
515 int wipe_buf
; /* buffer is going to be wiped out */
518 int is_curbuf
= (buf
== curbuf
);
520 apply_autocmds(EVENT_BUFUNLOAD
, buf
->b_fname
, buf
->b_fname
, FALSE
, buf
);
521 if (!buf_valid(buf
)) /* autocommands may delete the buffer */
523 if (del_buf
&& buf
->b_p_bl
)
525 apply_autocmds(EVENT_BUFDELETE
, buf
->b_fname
, buf
->b_fname
, FALSE
, buf
);
526 if (!buf_valid(buf
)) /* autocommands may delete the buffer */
531 apply_autocmds(EVENT_BUFWIPEOUT
, buf
->b_fname
, buf
->b_fname
,
533 if (!buf_valid(buf
)) /* autocommands may delete the buffer */
537 if (aborting()) /* autocmds may abort script processing */
542 * It's possible that autocommands change curbuf to the one being deleted.
543 * This might cause curbuf to be deleted unexpectedly. But in some cases
544 * it's OK to delete the curbuf, because a new one is obtained anyway.
545 * Therefore only return if curbuf changed to the deleted buffer.
547 if (buf
== curbuf
&& !is_curbuf
)
551 diff_buf_delete(buf
); /* Can't use 'diff' for unloaded buffer. */
554 tcl_buffer_free(buf
);
556 u_blockfree(buf
); /* free the memory allocated for undo */
557 ml_close(buf
, TRUE
); /* close and delete the memline/memfile */
558 buf
->b_ml
.ml_line_count
= 0; /* no lines in buffer */
559 u_clearall(buf
); /* reset all undo information */
561 syntax_clear(buf
); /* reset syntax info */
563 buf
->b_flags
&= ~BF_READERR
; /* a read error is no longer relevant */
567 * Free a buffer structure and the things it contains related to the buffer
568 * itself (not the file, that must have been done already).
574 free_buffer_stuff(buf
, TRUE
);
576 mzscheme_buffer_free(buf
);
582 python_buffer_free(buf
);
585 ruby_buffer_free(buf
);
588 aubuflocal_remove(buf
);
594 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
597 free_buffer_stuff(buf
, free_options
)
599 int free_options
; /* free options as well */
603 clear_wininfo(buf
); /* including window-local options */
604 free_buf_options(buf
, TRUE
);
607 vars_clear(&buf
->b_vars
.dv_hashtab
); /* free all internal variables */
608 hash_init(&buf
->b_vars
.dv_hashtab
);
611 uc_clear(&buf
->b_ucmds
); /* clear local user commands */
614 buf_delete_signs(buf
); /* delete any signs */
617 map_clear_int(buf
, MAP_ALL_MODES
, TRUE
, FALSE
); /* clear local mappings */
618 map_clear_int(buf
, MAP_ALL_MODES
, TRUE
, TRUE
); /* clear local abbrevs */
621 vim_free(buf
->b_start_fenc
);
622 buf
->b_start_fenc
= NULL
;
627 * Free the b_wininfo list for buffer "buf".
635 while (buf
->b_wininfo
!= NULL
)
637 wip
= buf
->b_wininfo
;
638 buf
->b_wininfo
= wip
->wi_next
;
641 clear_winopt(&wip
->wi_opt
);
643 deleteFoldRecurse(&wip
->wi_folds
);
650 #if defined(FEAT_LISTCMDS) || defined(PROTO)
652 * Go to another buffer. Handles the result of the ATTENTION dialog.
655 goto_buffer(eap
, start
, dir
, count
)
661 # if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
662 buf_T
*old_curbuf
= curbuf
;
664 swap_exists_action
= SEA_DIALOG
;
666 (void)do_buffer(*eap
->cmd
== 's' ? DOBUF_SPLIT
: DOBUF_GOTO
,
667 start
, dir
, count
, eap
->forceit
);
668 # if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
669 if (swap_exists_action
== SEA_QUIT
&& *eap
->cmd
== 's')
671 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
674 /* Reset the error/interrupt/exception state here so that
675 * aborting() returns FALSE when closing a window. */
679 /* Quitting means closing the split window, nothing else. */
680 win_close(curwin
, TRUE
);
681 swap_exists_action
= SEA_NONE
;
682 swap_exists_did_quit
= TRUE
;
684 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
685 /* Restore the error/interrupt/exception state if not discarded by a
686 * new aborting error, interrupt, or uncaught exception. */
691 handle_swap_exists(old_curbuf
);
696 #if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
698 * Handle the situation of swap_exists_action being set.
699 * It is allowed for "old_curbuf" to be NULL or invalid.
702 handle_swap_exists(old_curbuf
)
705 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
709 if (swap_exists_action
== SEA_QUIT
)
711 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
712 /* Reset the error/interrupt/exception state here so that
713 * aborting() returns FALSE when closing a buffer. */
717 /* User selected Quit at ATTENTION prompt. Go back to previous
718 * buffer. If that buffer is gone or the same as the current one,
719 * open a new, empty buffer. */
720 swap_exists_action
= SEA_NONE
; /* don't want it again */
721 swap_exists_did_quit
= TRUE
;
722 close_buffer(curwin
, curbuf
, DOBUF_UNLOAD
);
723 if (!buf_valid(old_curbuf
) || old_curbuf
== curbuf
)
724 old_curbuf
= buflist_new(NULL
, NULL
, 1L, BLN_CURBUF
| BLN_LISTED
);
725 if (old_curbuf
!= NULL
)
726 enter_buffer(old_curbuf
);
727 /* If "old_curbuf" is NULL we are in big trouble here... */
729 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
730 /* Restore the error/interrupt/exception state if not discarded by a
731 * new aborting error, interrupt, or uncaught exception. */
735 else if (swap_exists_action
== SEA_RECOVER
)
737 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
738 /* Reset the error/interrupt/exception state here so that
739 * aborting() returns FALSE when closing a buffer. */
743 /* User selected Recover at ATTENTION prompt. */
746 MSG_PUTS("\n"); /* don't overwrite the last message */
747 cmdline_row
= msg_row
;
750 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
751 /* Restore the error/interrupt/exception state if not discarded by a
752 * new aborting error, interrupt, or uncaught exception. */
756 swap_exists_action
= SEA_NONE
;
760 #if defined(FEAT_LISTCMDS) || defined(PROTO)
762 * do_bufdel() - delete or unload buffer(s)
764 * addr_count == 0: ":bdel" - delete current buffer
765 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
766 * buffer "end_bnr", then any other arguments.
767 * addr_count == 2: ":N,N bdel" - delete buffers in range
769 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
770 * DOBUF_DEL (":bdel")
772 * Returns error message or NULL
775 do_bufdel(command
, arg
, addr_count
, start_bnr
, end_bnr
, forceit
)
777 char_u
*arg
; /* pointer to extra arguments */
779 int start_bnr
; /* first buffer number in a range */
780 int end_bnr
; /* buffer nr or last buffer nr in a range */
783 int do_current
= 0; /* delete current buffer? */
784 int deleted
= 0; /* number of buffers deleted */
785 char_u
*errormsg
= NULL
; /* return value */
786 int bnr
; /* buffer number */
789 #ifdef FEAT_NETBEANS_INTG
790 netbeansCloseFile
= 1;
794 (void)do_buffer(command
, DOBUF_CURRENT
, FORWARD
, 0, forceit
);
800 if (*arg
) /* both range and argument is not allowed */
801 return (char_u
*)_(e_trailing
);
804 else /* addr_count == 1 */
807 for ( ;!got_int
; ui_breakcheck())
810 * delete the current buffer last, otherwise when the
811 * current buffer is deleted, the next buffer becomes
812 * the current one and will be loaded, which may then
813 * also be deleted, etc.
815 if (bnr
== curbuf
->b_fnum
)
817 else if (do_buffer(command
, DOBUF_FIRST
, FORWARD
, (int)bnr
,
822 * find next buffer number to delete/unload
829 else /* addr_count == 1 */
831 arg
= skipwhite(arg
);
834 if (!VIM_ISDIGIT(*arg
))
836 p
= skiptowhite_esc(arg
);
837 bnr
= buflist_findpat(arg
, p
, command
== DOBUF_WIPE
, FALSE
);
838 if (bnr
< 0) /* failed */
843 bnr
= getdigits(&arg
);
846 if (!got_int
&& do_current
&& do_buffer(command
, DOBUF_FIRST
,
847 FORWARD
, do_current
, forceit
) == OK
)
852 if (command
== DOBUF_UNLOAD
)
853 STRCPY(IObuff
, _("E515: No buffers were unloaded"));
854 else if (command
== DOBUF_DEL
)
855 STRCPY(IObuff
, _("E516: No buffers were deleted"));
857 STRCPY(IObuff
, _("E517: No buffers were wiped out"));
860 else if (deleted
>= p_report
)
862 if (command
== DOBUF_UNLOAD
)
865 MSG(_("1 buffer unloaded"));
867 smsg((char_u
*)_("%d buffers unloaded"), deleted
);
869 else if (command
== DOBUF_DEL
)
872 MSG(_("1 buffer deleted"));
874 smsg((char_u
*)_("%d buffers deleted"), deleted
);
879 MSG(_("1 buffer wiped out"));
881 smsg((char_u
*)_("%d buffers wiped out"), deleted
);
886 #ifdef FEAT_NETBEANS_INTG
887 netbeansCloseFile
= 0;
894 * Implementation of the commands for the buffer list.
896 * action == DOBUF_GOTO go to specified buffer
897 * action == DOBUF_SPLIT split window and go to specified buffer
898 * action == DOBUF_UNLOAD unload specified buffer(s)
899 * action == DOBUF_DEL delete specified buffer(s) from buffer list
900 * action == DOBUF_WIPE delete specified buffer(s) really
902 * start == DOBUF_CURRENT go to "count" buffer from current buffer
903 * start == DOBUF_FIRST go to "count" buffer from first buffer
904 * start == DOBUF_LAST go to "count" buffer from last buffer
905 * start == DOBUF_MOD go to "count" modified buffer from current buffer
910 do_buffer(action
, start
, dir
, count
, forceit
)
913 int dir
; /* FORWARD or BACKWARD */
914 int count
; /* buffer number or number of buffers */
915 int forceit
; /* TRUE for :...! */
919 int unload
= (action
== DOBUF_UNLOAD
|| action
== DOBUF_DEL
920 || action
== DOBUF_WIPE
);
924 case DOBUF_FIRST
: buf
= firstbuf
; break;
925 case DOBUF_LAST
: buf
= lastbuf
; break;
926 default: buf
= curbuf
; break;
928 if (start
== DOBUF_MOD
) /* find next modified buffer */
938 while (buf
!= curbuf
&& !bufIsChanged(buf
));
940 if (!bufIsChanged(buf
))
942 EMSG(_("E84: No modified buffer found"));
946 else if (start
== DOBUF_FIRST
&& count
) /* find specified buffer number */
948 while (buf
!= NULL
&& buf
->b_fnum
!= count
)
954 while (count
> 0 || (!unload
&& !buf
->b_p_bl
&& bp
!= buf
))
956 /* remember the buffer where we start, we come back there when all
957 * buffers are unlisted. */
972 /* don't count unlisted buffers */
973 if (unload
|| buf
->b_p_bl
)
976 bp
= NULL
; /* use this buffer as new starting point */
980 /* back where we started, didn't find anything. */
981 EMSG(_("E85: There is no listed buffer"));
987 if (buf
== NULL
) /* could not find it */
989 if (start
== DOBUF_FIRST
)
991 /* don't warn when deleting */
993 EMSGN(_("E86: Buffer %ld does not exist"), count
);
995 else if (dir
== FORWARD
)
996 EMSG(_("E87: Cannot go beyond last buffer"));
998 EMSG(_("E88: Cannot go before first buffer"));
1003 need_mouse_correct
= TRUE
;
1006 #ifdef FEAT_LISTCMDS
1008 * delete buffer buf from memory and/or the list
1015 /* When unloading or deleting a buffer that's already unloaded and
1016 * unlisted: fail silently. */
1017 if (action
!= DOBUF_WIPE
&& buf
->b_ml
.ml_mfp
== NULL
&& !buf
->b_p_bl
)
1020 if (!forceit
&& bufIsChanged(buf
))
1022 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1023 if ((p_confirm
|| cmdmod
.confirm
) && p_write
)
1025 dialog_changed(buf
, FALSE
);
1026 # ifdef FEAT_AUTOCMD
1027 if (!buf_valid(buf
))
1028 /* Autocommand deleted buffer, oops! It's not changed
1032 /* If it's still changed fail silently, the dialog already
1033 * mentioned why it fails. */
1034 if (bufIsChanged(buf
))
1040 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1047 * If deleting the last (listed) buffer, make it empty.
1048 * The last (listed) buffer cannot be unloaded.
1050 for (bp
= firstbuf
; bp
!= NULL
; bp
= bp
->b_next
)
1051 if (bp
->b_p_bl
&& bp
!= buf
)
1053 if (bp
== NULL
&& buf
== curbuf
)
1055 if (action
== DOBUF_UNLOAD
)
1057 EMSG(_("E90: Cannot unload last buffer"));
1061 /* Close any other windows on this buffer, then make it empty. */
1063 close_windows(buf
, TRUE
);
1066 retval
= do_ecmd(0, NULL
, NULL
, NULL
, ECMD_ONE
,
1067 forceit
? ECMD_FORCEIT
: 0);
1070 * do_ecmd() may create a new buffer, then we have to delete
1071 * the old one. But do_ecmd() may have done that already, check
1072 * if the buffer still exists.
1074 if (buf
!= curbuf
&& buf_valid(buf
) && buf
->b_nwindows
== 0)
1075 close_buffer(NULL
, buf
, action
);
1081 * If the deleted buffer is the current one, close the current window
1082 * (unless it's the only window). Repeat this so long as we end up in
1083 * a window with this buffer.
1085 while (buf
== curbuf
1086 && (firstwin
!= lastwin
|| first_tabpage
->tp_next
!= NULL
))
1087 win_close(curwin
, FALSE
);
1091 * If the buffer to be deleted is not the current one, delete it here.
1096 close_windows(buf
, FALSE
);
1098 if (buf
!= curbuf
&& buf_valid(buf
) && buf
->b_nwindows
<= 0)
1099 close_buffer(NULL
, buf
, action
);
1104 * Deleting the current buffer: Need to find another buffer to go to.
1105 * There must be another, otherwise it would have been handled above.
1106 * First use au_new_curbuf, if it is valid.
1107 * Then prefer the buffer we most recently visited.
1108 * Else try to find one that is loaded, after the current buffer,
1109 * then before the current buffer.
1110 * Finally use any buffer.
1112 buf
= NULL
; /* selected buffer */
1113 bp
= NULL
; /* used when no loaded buffer found */
1115 if (au_new_curbuf
!= NULL
&& buf_valid(au_new_curbuf
))
1116 buf
= au_new_curbuf
;
1117 # ifdef FEAT_JUMPLIST
1121 #ifdef FEAT_JUMPLIST
1122 if (curwin
->w_jumplistlen
> 0)
1126 jumpidx
= curwin
->w_jumplistidx
- 1;
1128 jumpidx
= curwin
->w_jumplistlen
- 1;
1131 while (jumpidx
!= curwin
->w_jumplistidx
)
1133 buf
= buflist_findnr(curwin
->w_jumplist
[jumpidx
].fmark
.fnum
);
1136 if (buf
== curbuf
|| !buf
->b_p_bl
)
1137 buf
= NULL
; /* skip current and unlisted bufs */
1138 else if (buf
->b_ml
.ml_mfp
== NULL
)
1140 /* skip unloaded buf, but may keep it for later */
1146 if (buf
!= NULL
) /* found a valid buffer: stop searching */
1148 /* advance to older entry in jump list */
1149 if (!jumpidx
&& curwin
->w_jumplistidx
== curwin
->w_jumplistlen
)
1152 jumpidx
= curwin
->w_jumplistlen
- 1;
1153 if (jumpidx
== forward
) /* List exhausted for sure */
1159 if (buf
== NULL
) /* No previous buffer, Try 2'nd approach */
1162 buf
= curbuf
->b_next
;
1167 if (!forward
) /* tried both directions */
1169 buf
= curbuf
->b_prev
;
1173 /* in non-help buffer, try to skip help buffers, and vv */
1174 if (buf
->b_help
== curbuf
->b_help
&& buf
->b_p_bl
)
1176 if (buf
->b_ml
.ml_mfp
!= NULL
) /* found loaded buffer */
1178 if (bp
== NULL
) /* remember unloaded buf for later */
1187 if (buf
== NULL
) /* No loaded buffer, use unloaded one */
1189 if (buf
== NULL
) /* No loaded buffer, find listed one */
1191 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
1192 if (buf
->b_p_bl
&& buf
!= curbuf
)
1195 if (buf
== NULL
) /* Still no buffer, just take one */
1197 if (curbuf
->b_next
!= NULL
)
1198 buf
= curbuf
->b_next
;
1200 buf
= curbuf
->b_prev
;
1205 * make buf current buffer
1207 if (action
== DOBUF_SPLIT
) /* split window first */
1209 # ifdef FEAT_WINDOWS
1210 /* jump to first window containing buf if one exists ("useopen") */
1211 if (vim_strchr(p_swb
, 'o') != NULL
&& buf_jump_open_win(buf
))
1213 /* jump to first window in any tab page containing buf if one exists
1215 if (vim_strchr(p_swb
, 'a') != NULL
&& buf_jump_open_tab(buf
))
1217 if (win_split(0, 0) == FAIL
)
1223 /* go to current buffer - nothing to do */
1228 * Check if the current buffer may be abandoned.
1230 if (action
== DOBUF_GOTO
&& !can_abandon(curbuf
, forceit
))
1232 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1233 if ((p_confirm
|| cmdmod
.confirm
) && p_write
)
1235 dialog_changed(curbuf
, FALSE
);
1236 # ifdef FEAT_AUTOCMD
1237 if (!buf_valid(buf
))
1238 /* Autocommand deleted buffer, oops! */
1242 if (bufIsChanged(curbuf
))
1245 EMSG(_(e_nowrtmsg
));
1250 /* Go to the other buffer. */
1251 set_curbuf(buf
, action
);
1253 #if defined(FEAT_LISTCMDS) && defined(FEAT_SCROLLBIND)
1254 if (action
== DOBUF_SPLIT
)
1255 curwin
->w_p_scb
= FALSE
; /* reset 'scrollbind' */
1258 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1259 if (aborting()) /* autocmds may abort script processing */
1266 #endif /* FEAT_LISTCMDS */
1269 * Set current buffer to "buf". Executes autocommands and closes current
1270 * buffer. "action" tells how to close the current buffer:
1271 * DOBUF_GOTO free or hide it
1272 * DOBUF_SPLIT nothing
1273 * DOBUF_UNLOAD unload it
1274 * DOBUF_DEL delete it
1275 * DOBUF_WIPE wipe it out
1278 set_curbuf(buf
, action
)
1283 int unload
= (action
== DOBUF_UNLOAD
|| action
== DOBUF_DEL
1284 || action
== DOBUF_WIPE
);
1287 if (!cmdmod
.keepalt
)
1288 curwin
->w_alt_fnum
= curbuf
->b_fnum
; /* remember alternate file */
1289 buflist_altfpos(); /* remember curpos */
1292 /* Don't restart Select mode after switching to another buffer. */
1293 VIsual_reselect
= FALSE
;
1296 /* close_windows() or apply_autocmds() may change curbuf */
1300 apply_autocmds(EVENT_BUFLEAVE
, NULL
, NULL
, FALSE
, curbuf
);
1302 if (buf_valid(prevbuf
) && !aborting())
1304 if (buf_valid(prevbuf
))
1310 close_windows(prevbuf
, FALSE
);
1312 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1313 if (buf_valid(prevbuf
) && !aborting())
1315 if (buf_valid(prevbuf
))
1318 if (prevbuf
== curbuf
)
1320 close_buffer(prevbuf
== curwin
->w_buffer
? curwin
: NULL
, prevbuf
,
1321 unload
? action
: (action
== DOBUF_GOTO
1323 && !bufIsChanged(prevbuf
)) ? DOBUF_UNLOAD
: 0);
1328 /* An autocommand may have deleted buf or aborted the script processing! */
1329 if (buf_valid(buf
) && !aborting())
1331 if (buf_valid(buf
)) /* an autocommand may have deleted buf! */
1338 * Enter a new current buffer.
1339 * Old curbuf must have been abandoned already!
1345 /* Copy buffer and window local option values. Not for a help buffer. */
1346 buf_copy_options(buf
, BCO_ENTER
| BCO_NOHELP
);
1351 /* Remove all folds in the window. */
1352 clearFolding(curwin
);
1353 foldUpdateAll(curwin
); /* update folds (later). */
1356 /* Get the buffer in the current window. */
1357 curwin
->w_buffer
= buf
;
1359 ++curbuf
->b_nwindows
;
1362 if (curwin
->w_p_diff
)
1363 diff_buf_add(curbuf
);
1366 /* Cursor on first line by default. */
1367 curwin
->w_cursor
.lnum
= 1;
1368 curwin
->w_cursor
.col
= 0;
1369 #ifdef FEAT_VIRTUALEDIT
1370 curwin
->w_cursor
.coladd
= 0;
1372 curwin
->w_set_curswant
= TRUE
;
1374 /* Make sure the buffer is loaded. */
1375 if (curbuf
->b_ml
.ml_mfp
== NULL
) /* need to load the file */
1378 /* If there is no filetype, allow for detecting one. Esp. useful for
1379 * ":ball" used in a autocommand. If there already is a filetype we
1380 * might prefer to keep it. */
1381 if (*curbuf
->b_p_ft
== NUL
)
1382 did_filetype
= FALSE
;
1385 open_buffer(FALSE
, NULL
);
1390 need_fileinfo
= TRUE
; /* display file info after redraw */
1391 (void)buf_check_timestamp(curbuf
, FALSE
); /* check if file changed */
1393 curwin
->w_topline
= 1;
1395 curwin
->w_topfill
= 0;
1397 apply_autocmds(EVENT_BUFENTER
, NULL
, NULL
, FALSE
, curbuf
);
1398 apply_autocmds(EVENT_BUFWINENTER
, NULL
, NULL
, FALSE
, curbuf
);
1402 /* If autocommands did not change the cursor position, restore cursor lnum
1403 * and possibly cursor col. */
1404 if (curwin
->w_cursor
.lnum
== 1 && inindent(0))
1407 check_arg_idx(curwin
); /* check for valid arg_idx */
1412 if (curwin
->w_topline
== 1) /* when autocmds didn't change it */
1414 scroll_cursor_halfway(FALSE
); /* redisplay at correct position */
1416 #ifdef FEAT_NETBEANS_INTG
1417 /* Send fileOpened event because we've changed buffers. */
1418 if (usingNetbeans
&& isNetbeansBuffer(curbuf
))
1419 netbeans_file_activated(curbuf
);
1422 /* Change directories when the 'acd' option is set. */
1426 if (curbuf
->b_kmap_state
& KEYMAP_INIT
)
1429 redraw_later(NOT_VALID
);
1432 #if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1434 * Change to the directory of the current buffer.
1439 if (curbuf
->b_ffname
!= NULL
&& vim_chdirfile(curbuf
->b_ffname
) == OK
)
1440 shorten_fnames(TRUE
);
1445 * functions for dealing with the buffer list
1449 * Add a file name to the buffer list. Return a pointer to the buffer.
1450 * If the same file name already exists return a pointer to that buffer.
1451 * If it does not exist, or if fname == NULL, a new entry is created.
1452 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1453 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1454 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
1455 * This is the ONLY way to create a new buffer.
1457 static int top_file_num
= 1; /* highest file number */
1460 buflist_new(ffname
, sfname
, lnum
, flags
)
1461 char_u
*ffname
; /* full path of fname or relative */
1462 char_u
*sfname
; /* short fname or NULL */
1463 linenr_T lnum
; /* preferred cursor line */
1464 int flags
; /* BLN_ defines */
1471 fname_expand(curbuf
, &ffname
, &sfname
); /* will allocate ffname */
1474 * If file name already exists in the list, update the entry.
1477 /* On Unix we can use inode numbers when the file exists. Works better
1478 * for hard links. */
1479 if (sfname
== NULL
|| mch_stat((char *)sfname
, &st
) < 0)
1480 st
.st_dev
= (dev_T
)-1;
1482 if (ffname
!= NULL
&& !(flags
& BLN_DUMMY
) && (buf
=
1484 buflist_findname_stat(ffname
, &st
)
1486 buflist_findname(ffname
)
1492 buflist_setfpos(buf
, curwin
, lnum
, (colnr_T
)0, FALSE
);
1493 /* copy the options now, if 'cpo' doesn't have 's' and not done
1495 buf_copy_options(buf
, 0);
1496 if ((flags
& BLN_LISTED
) && !buf
->b_p_bl
)
1500 if (!(flags
& BLN_DUMMY
))
1501 apply_autocmds(EVENT_BUFADD
, NULL
, NULL
, FALSE
, buf
);
1508 * If the current buffer has no name and no contents, use the current
1509 * buffer. Otherwise: Need to allocate a new buffer structure.
1511 * This is the ONLY place where a new buffer structure is allocated!
1512 * (A spell file buffer is allocated in spell.c, but that's not a normal
1516 if ((flags
& BLN_CURBUF
)
1518 && curbuf
->b_ffname
== NULL
1519 && curbuf
->b_nwindows
<= 1
1520 && (curbuf
->b_ml
.ml_mfp
== NULL
|| bufempty()))
1524 /* It's like this buffer is deleted. Watch out for autocommands that
1525 * change curbuf! If that happens, allocate a new buffer anyway. */
1527 apply_autocmds(EVENT_BUFDELETE
, NULL
, NULL
, FALSE
, curbuf
);
1529 apply_autocmds(EVENT_BUFWIPEOUT
, NULL
, NULL
, FALSE
, curbuf
);
1531 if (aborting()) /* autocmds may abort script processing */
1535 #ifdef FEAT_QUICKFIX
1536 # ifdef FEAT_AUTOCMD
1540 /* Make sure 'bufhidden' and 'buftype' are empty */
1541 clear_string_option(&buf
->b_p_bh
);
1542 clear_string_option(&buf
->b_p_bt
);
1546 if (buf
!= curbuf
|| curbuf
== NULL
)
1548 buf
= (buf_T
*)alloc_clear((unsigned)sizeof(buf_T
));
1558 buf
->b_ffname
= ffname
;
1559 buf
->b_sfname
= vim_strsave(sfname
);
1563 buf
->b_wininfo
= (wininfo_T
*)alloc_clear((unsigned)sizeof(wininfo_T
));
1565 if ((ffname
!= NULL
&& (buf
->b_ffname
== NULL
|| buf
->b_sfname
== NULL
))
1566 || buf
->b_wininfo
== NULL
)
1568 vim_free(buf
->b_ffname
);
1569 buf
->b_ffname
= NULL
;
1570 vim_free(buf
->b_sfname
);
1571 buf
->b_sfname
= NULL
;
1579 /* free all things allocated for this buffer */
1580 buf_freeall(buf
, FALSE
, FALSE
);
1581 if (buf
!= curbuf
) /* autocommands deleted the buffer! */
1583 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1584 if (aborting()) /* autocmds may abort script processing */
1587 /* buf->b_nwindows = 0; why was this here? */
1588 free_buffer_stuff(buf
, FALSE
); /* delete local variables et al. */
1590 /* need to reload lmaps and set b:keymap_name */
1591 curbuf
->b_kmap_state
|= KEYMAP_INIT
;
1597 * put new buffer at the end of the buffer list
1600 if (firstbuf
== NULL
) /* buffer list is empty */
1605 else /* append new buffer at end of list */
1607 lastbuf
->b_next
= buf
;
1608 buf
->b_prev
= lastbuf
;
1612 buf
->b_fnum
= top_file_num
++;
1613 if (top_file_num
< 0) /* wrap around (may cause duplicates) */
1615 EMSG(_("W14: Warning: List of file names overflow"));
1616 if (emsg_silent
== 0)
1619 ui_delay(3000L, TRUE
); /* make sure it is noticed */
1625 * Always copy the options from the current buffer.
1627 buf_copy_options(buf
, BCO_ALWAYS
);
1630 buf
->b_wininfo
->wi_fpos
.lnum
= lnum
;
1631 buf
->b_wininfo
->wi_win
= curwin
;
1634 init_var_dict(&buf
->b_vars
, &buf
->b_bufvar
); /* init b: variables */
1637 hash_init(&buf
->b_keywtab
);
1638 hash_init(&buf
->b_keywtab_ic
);
1641 buf
->b_fname
= buf
->b_sfname
;
1643 if (st
.st_dev
== (dev_T
)-1)
1647 buf
->b_dev
= st
.st_dev
;
1648 buf
->b_ino
= st
.st_ino
;
1651 buf
->b_u_synced
= TRUE
;
1652 buf
->b_flags
= BF_CHECK_RO
| BF_NEVERLOADED
;
1653 if (flags
& BLN_DUMMY
)
1654 buf
->b_flags
|= BF_DUMMY
;
1655 buf_clear_file(buf
);
1656 clrallmarks(buf
); /* clear marks */
1657 fmarks_check_names(buf
); /* check file marks for this file */
1658 buf
->b_p_bl
= (flags
& BLN_LISTED
) ? TRUE
: FALSE
; /* init 'buflisted' */
1660 if (!(flags
& BLN_DUMMY
))
1662 apply_autocmds(EVENT_BUFNEW
, NULL
, NULL
, FALSE
, buf
);
1663 if (flags
& BLN_LISTED
)
1664 apply_autocmds(EVENT_BUFADD
, NULL
, NULL
, FALSE
, buf
);
1666 if (aborting()) /* autocmds may abort script processing */
1676 * Free the memory for the options of a buffer.
1677 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1681 free_buf_options(buf
, free_p_ff
)
1688 clear_string_option(&buf
->b_p_fenc
);
1690 clear_string_option(&buf
->b_p_ff
);
1691 #ifdef FEAT_QUICKFIX
1692 clear_string_option(&buf
->b_p_bh
);
1693 clear_string_option(&buf
->b_p_bt
);
1697 clear_string_option(&buf
->b_p_def
);
1698 clear_string_option(&buf
->b_p_inc
);
1700 clear_string_option(&buf
->b_p_inex
);
1703 #if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1704 clear_string_option(&buf
->b_p_inde
);
1705 clear_string_option(&buf
->b_p_indk
);
1707 #if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1708 clear_string_option(&buf
->b_p_bexpr
);
1710 #if defined(FEAT_EVAL)
1711 clear_string_option(&buf
->b_p_fex
);
1714 clear_string_option(&buf
->b_p_key
);
1716 clear_string_option(&buf
->b_p_kp
);
1717 clear_string_option(&buf
->b_p_mps
);
1718 clear_string_option(&buf
->b_p_fo
);
1719 clear_string_option(&buf
->b_p_flp
);
1720 clear_string_option(&buf
->b_p_isk
);
1722 clear_string_option(&buf
->b_p_keymap
);
1723 ga_clear(&buf
->b_kmap_ga
);
1725 #ifdef FEAT_COMMENTS
1726 clear_string_option(&buf
->b_p_com
);
1729 clear_string_option(&buf
->b_p_cms
);
1731 clear_string_option(&buf
->b_p_nf
);
1733 clear_string_option(&buf
->b_p_syn
);
1736 clear_string_option(&buf
->b_p_spc
);
1737 clear_string_option(&buf
->b_p_spf
);
1738 vim_free(buf
->b_cap_prog
);
1739 buf
->b_cap_prog
= NULL
;
1740 clear_string_option(&buf
->b_p_spl
);
1742 #ifdef FEAT_SEARCHPATH
1743 clear_string_option(&buf
->b_p_sua
);
1746 clear_string_option(&buf
->b_p_ft
);
1748 #ifdef FEAT_OSFILETYPE
1749 clear_string_option(&buf
->b_p_oft
);
1752 clear_string_option(&buf
->b_p_cink
);
1753 clear_string_option(&buf
->b_p_cino
);
1755 #if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1756 clear_string_option(&buf
->b_p_cinw
);
1758 #ifdef FEAT_INS_EXPAND
1759 clear_string_option(&buf
->b_p_cpt
);
1761 #ifdef FEAT_COMPL_FUNC
1762 clear_string_option(&buf
->b_p_cfu
);
1763 clear_string_option(&buf
->b_p_ofu
);
1765 #ifdef FEAT_QUICKFIX
1766 clear_string_option(&buf
->b_p_gp
);
1767 clear_string_option(&buf
->b_p_mp
);
1768 clear_string_option(&buf
->b_p_efm
);
1770 clear_string_option(&buf
->b_p_ep
);
1771 clear_string_option(&buf
->b_p_path
);
1772 clear_string_option(&buf
->b_p_tags
);
1773 #ifdef FEAT_INS_EXPAND
1774 clear_string_option(&buf
->b_p_dict
);
1775 clear_string_option(&buf
->b_p_tsr
);
1778 clear_string_option(&buf
->b_p_qe
);
1784 * get alternate file n
1785 * set linenr to lnum or altfpos.lnum if lnum == 0
1786 * also set cursor column to altfpos.col if 'startofline' is not set.
1787 * if (options & GETF_SETMARK) call setpcmark()
1788 * if (options & GETF_ALT) we are jumping to an alternate file.
1789 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1791 * return FAIL for failure, OK for success
1794 buflist_getfile(n
, lnum
, options
, forceit
)
1807 buf
= buflist_findnr(n
);
1810 if ((options
& GETF_ALT
) && n
== 0)
1813 EMSGN(_("E92: Buffer %ld not found"), n
);
1817 /* if alternate file is the current buffer, nothing to do */
1827 if (curbuf_locked())
1831 /* altfpos may be changed by getfile(), get it now */
1834 fpos
= buflist_findfpos(buf
);
1842 if (options
& GETF_SWITCH
)
1844 /* use existing open window for buffer if wanted */
1845 if (vim_strchr(p_swb
, 'o') != NULL
) /* useopen */
1846 wp
= buf_jump_open_win(buf
);
1847 /* use existing open window in any tab page for buffer if wanted */
1848 if (vim_strchr(p_swb
, 'a') != NULL
) /* usetab */
1849 wp
= buf_jump_open_tab(buf
);
1850 /* split window if wanted ("split") */
1851 if (wp
== NULL
&& vim_strchr(p_swb
, 'l') != NULL
&& !bufempty())
1853 if (win_split(0, 0) == FAIL
)
1855 # ifdef FEAT_SCROLLBIND
1856 curwin
->w_p_scb
= FALSE
;
1862 ++RedrawingDisabled
;
1863 if (getfile(buf
->b_fnum
, NULL
, NULL
, (options
& GETF_SETMARK
),
1864 lnum
, forceit
) <= 0)
1866 --RedrawingDisabled
;
1868 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
1869 if (!p_sol
&& col
!= 0)
1871 curwin
->w_cursor
.col
= col
;
1873 #ifdef FEAT_VIRTUALEDIT
1874 curwin
->w_cursor
.coladd
= 0;
1876 curwin
->w_set_curswant
= TRUE
;
1880 --RedrawingDisabled
;
1885 * go to the last know line number for the current buffer
1892 fpos
= buflist_findfpos(curbuf
);
1894 curwin
->w_cursor
.lnum
= fpos
->lnum
;
1895 check_cursor_lnum();
1898 curwin
->w_cursor
.col
= 0;
1901 curwin
->w_cursor
.col
= fpos
->col
;
1903 #ifdef FEAT_VIRTUALEDIT
1904 curwin
->w_cursor
.coladd
= 0;
1906 curwin
->w_set_curswant
= TRUE
;
1910 #if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
1912 * Find file in buffer list by name (it has to be for the current window).
1913 * Returns NULL if not found.
1916 buflist_findname_exp(fname
)
1922 /* First make the name into a full path name */
1923 ffname
= FullName_save(fname
,
1925 TRUE
/* force expansion, get rid of symbolic links */
1932 buf
= buflist_findname(ffname
);
1940 * Find file in buffer list by name (it has to be for the current window).
1941 * "ffname" must have a full path.
1942 * Skips dummy buffers.
1943 * Returns NULL if not found.
1946 buflist_findname(ffname
)
1952 if (mch_stat((char *)ffname
, &st
) < 0)
1953 st
.st_dev
= (dev_T
)-1;
1954 return buflist_findname_stat(ffname
, &st
);
1958 * Same as buflist_findname(), but pass the stat structure to avoid getting it
1959 * twice for the same file.
1960 * Returns NULL if not found.
1963 buflist_findname_stat(ffname
, stp
)
1970 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
1971 if ((buf
->b_flags
& BF_DUMMY
) == 0 && !otherfile_buf(buf
, ffname
1980 #if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO)
1982 * Find file in buffer list by a regexp pattern.
1983 * Return fnum of the found buffer.
1984 * Return < 0 for error.
1988 buflist_findpat(pattern
, pattern_end
, unlisted
, diffmode
)
1990 char_u
*pattern_end
; /* pointer to first char after pattern */
1991 int unlisted
; /* find unlisted buffers */
1992 int diffmode
; /* find diff-mode buffers only */
2004 if (pattern_end
== pattern
+ 1 && (*pattern
== '%' || *pattern
== '#'))
2006 if (*pattern
== '%')
2007 match
= curbuf
->b_fnum
;
2009 match
= curwin
->w_alt_fnum
;
2011 if (diffmode
&& !diff_mode_buf(buflist_findnr(match
)))
2017 * Try four ways of matching a listed buffer:
2018 * attempt == 0: without '^' or '$' (at any position)
2019 * attempt == 1: with '^' at start (only at postion 0)
2020 * attempt == 2: with '$' at end (only match at end)
2021 * attempt == 3: with '^' at start and '$' at end (only full match)
2022 * Repeat this for finding an unlisted buffer if there was no matching
2027 pat
= file_pat_to_reg_pat(pattern
, pattern_end
, NULL
, FALSE
);
2030 patend
= pat
+ STRLEN(pat
) - 1;
2031 toggledollar
= (patend
> pat
&& *patend
== '$');
2033 /* First try finding a listed buffer. If not found and "unlisted"
2034 * is TRUE, try finding an unlisted buffer. */
2038 for (attempt
= 0; attempt
<= 3; ++attempt
)
2040 /* may add '^' and '$' */
2042 *patend
= (attempt
< 2) ? NUL
: '$'; /* add/remove '$' */
2044 if (*p
== '^' && !(attempt
& 1)) /* add/remove '^' */
2046 prog
= vim_regcomp(p
, p_magic
? RE_MAGIC
: 0);
2053 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
2054 if (buf
->b_p_bl
== find_listed
2056 && (!diffmode
|| diff_mode_buf(buf
))
2058 && buflist_match(prog
, buf
) != NULL
)
2060 if (match
>= 0) /* already found a match */
2065 match
= buf
->b_fnum
; /* remember first match */
2069 if (match
>= 0) /* found one match */
2073 /* Only search for unlisted buffers if there was no match with
2074 * a listed buffer. */
2075 if (!unlisted
|| !find_listed
|| match
!= -1)
2077 find_listed
= FALSE
;
2084 EMSG2(_("E93: More than one match for %s"), pattern
);
2086 EMSG2(_("E94: No matching buffer for %s"), pattern
);
2091 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2094 * Find all buffer names that match.
2095 * For command line expansion of ":buf" and ":sbuf".
2096 * Return OK if matches found, FAIL otherwise.
2099 ExpandBufnames(pat
, num_file
, file
, options
)
2113 *num_file
= 0; /* return values in case of FAIL */
2116 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2119 patc
= alloc((unsigned)STRLEN(pat
) + 11);
2122 STRCPY(patc
, "\\(^\\|[\\/]\\)");
2123 STRCPY(patc
+ 11, pat
+ 1);
2129 * attempt == 0: try match with '\<', match at start of word
2130 * attempt == 1: try match without '\<', match anywhere
2132 for (attempt
= 0; attempt
<= 1; ++attempt
)
2134 if (attempt
> 0 && patc
== pat
)
2135 break; /* there was no anchor, no need to try again */
2136 prog
= vim_regcomp(patc
+ attempt
* 11, RE_MAGIC
);
2145 * round == 1: Count the matches.
2146 * round == 2: Build the array to keep the matches.
2148 for (round
= 1; round
<= 2; ++round
)
2151 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
2153 if (!buf
->b_p_bl
) /* skip unlisted buffers */
2155 p
= buflist_match(prog
, buf
);
2162 if (options
& WILD_HOME_REPLACE
)
2163 p
= home_replace_save(buf
, p
);
2166 (*file
)[count
++] = p
;
2170 if (count
== 0) /* no match found, break here */
2174 *file
= (char_u
**)alloc((unsigned)(count
* sizeof(char_u
*)));
2185 if (count
) /* match(es) found, break here */
2193 return (count
== 0 ? FAIL
: OK
);
2196 #endif /* FEAT_CMDL_COMPL */
2198 #ifdef HAVE_BUFLIST_MATCH
2200 * Check for a match on the file name for buffer "buf" with regprog "prog".
2203 buflist_match(prog
, buf
)
2209 /* First try the short file name, then the long file name. */
2210 match
= fname_match(prog
, buf
->b_sfname
);
2212 match
= fname_match(prog
, buf
->b_ffname
);
2218 * Try matching the regexp in "prog" with file name "name".
2219 * Return "name" when there is a match, NULL when not.
2222 fname_match(prog
, name
)
2226 char_u
*match
= NULL
;
2228 regmatch_T regmatch
;
2232 regmatch
.regprog
= prog
;
2233 #ifdef CASE_INSENSITIVE_FILENAME
2234 regmatch
.rm_ic
= TRUE
; /* Always ignore case */
2236 regmatch
.rm_ic
= FALSE
; /* Never ignore case */
2239 if (vim_regexec(®match
, name
, (colnr_T
)0))
2243 /* Replace $(HOME) with '~' and try matching again. */
2244 p
= home_replace_save(NULL
, name
);
2245 if (p
!= NULL
&& vim_regexec(®match
, p
, (colnr_T
)0))
2256 * find file in buffer list by number
2265 nr
= curwin
->w_alt_fnum
;
2266 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
2267 if (buf
->b_fnum
== nr
)
2273 * Get name of file 'n' in the buffer list.
2274 * When the file has no name an empty string is returned.
2275 * home_replace() is used to shorten the file name (used for marks).
2276 * Returns a pointer to allocated memory, of NULL when failed.
2279 buflist_nr2name(n
, fullname
, helptail
)
2282 int helptail
; /* for help buffers return tail only */
2286 buf
= buflist_findnr(n
);
2289 return home_replace_save(helptail
? buf
: NULL
,
2290 fullname
? buf
->b_ffname
: buf
->b_fname
);
2294 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2295 * When "copy_options" is TRUE save the local window option values.
2296 * When "lnum" is 0 only do the options.
2299 buflist_setfpos(buf
, win
, lnum
, col
, copy_options
)
2308 for (wip
= buf
->b_wininfo
; wip
!= NULL
; wip
= wip
->wi_next
)
2309 if (wip
->wi_win
== win
)
2313 /* allocate a new entry */
2314 wip
= (wininfo_T
*)alloc_clear((unsigned)sizeof(wininfo_T
));
2318 if (lnum
== 0) /* set lnum even when it's 0 */
2323 /* remove the entry from the list */
2325 wip
->wi_prev
->wi_next
= wip
->wi_next
;
2327 buf
->b_wininfo
= wip
->wi_next
;
2329 wip
->wi_next
->wi_prev
= wip
->wi_prev
;
2330 if (copy_options
&& wip
->wi_optset
)
2332 clear_winopt(&wip
->wi_opt
);
2334 deleteFoldRecurse(&wip
->wi_folds
);
2340 wip
->wi_fpos
.lnum
= lnum
;
2341 wip
->wi_fpos
.col
= col
;
2345 /* Save the window-specific option values. */
2346 copy_winopt(&win
->w_onebuf_opt
, &wip
->wi_opt
);
2348 wip
->wi_fold_manual
= win
->w_fold_manual
;
2349 cloneFoldGrowArray(&win
->w_folds
, &wip
->wi_folds
);
2351 wip
->wi_optset
= TRUE
;
2354 /* insert the entry in front of the list */
2355 wip
->wi_next
= buf
->b_wininfo
;
2356 buf
->b_wininfo
= wip
;
2357 wip
->wi_prev
= NULL
;
2359 wip
->wi_next
->wi_prev
= wip
;
2365 * Find info for the current window in buffer "buf".
2366 * If not found, return the info for the most recently used window.
2367 * Returns NULL when there isn't any info.
2375 for (wip
= buf
->b_wininfo
; wip
!= NULL
; wip
= wip
->wi_next
)
2376 if (wip
->wi_win
== curwin
)
2378 if (wip
== NULL
) /* if no fpos for curwin, use the first in the list */
2379 wip
= buf
->b_wininfo
;
2384 * Reset the local window options to the values last used in this window.
2385 * If the buffer wasn't used in this window before, use the values from
2386 * the most recently used window. If the values were never set, use the
2387 * global values for the window.
2395 clear_winopt(&curwin
->w_onebuf_opt
);
2397 clearFolding(curwin
);
2400 wip
= find_wininfo(buf
);
2401 if (wip
!= NULL
&& wip
->wi_optset
)
2403 copy_winopt(&wip
->wi_opt
, &curwin
->w_onebuf_opt
);
2405 curwin
->w_fold_manual
= wip
->wi_fold_manual
;
2406 curwin
->w_foldinvalid
= TRUE
;
2407 cloneFoldGrowArray(&wip
->wi_folds
, &curwin
->w_folds
);
2411 copy_winopt(&curwin
->w_allbuf_opt
, &curwin
->w_onebuf_opt
);
2414 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2416 curwin
->w_p_fdl
= p_fdls
;
2420 if (curwin
->w_p_spell
&& *buf
->b_p_spl
!= NUL
)
2421 did_set_spelllang(buf
);
2426 * Find the position (lnum and col) for the buffer 'buf' for the current
2428 * Returns a pointer to no_position if no position is found.
2431 buflist_findfpos(buf
)
2435 static pos_T no_position
= {1, 0};
2437 wip
= find_wininfo(buf
);
2439 return &(wip
->wi_fpos
);
2441 return &no_position
;
2445 * Find the lnum for the buffer 'buf' for the current window.
2448 buflist_findlnum(buf
)
2451 return buflist_findfpos(buf
)->lnum
;
2454 #if defined(FEAT_LISTCMDS) || defined(PROTO)
2456 * List all know file names (for :files and :buffers command).
2467 for (buf
= firstbuf
; buf
!= NULL
&& !got_int
; buf
= buf
->b_next
)
2469 /* skip unlisted buffers, unless ! was used */
2470 if (!buf
->b_p_bl
&& !eap
->forceit
)
2473 if (buf_spname(buf
) != NULL
)
2474 STRCPY(NameBuff
, buf_spname(buf
));
2476 home_replace(buf
, buf
->b_fname
, NameBuff
, MAXPATHL
, TRUE
);
2478 len
= vim_snprintf((char *)IObuff
, IOSIZE
- 20, "%3d%c%c%c%c%c \"%s\"",
2480 buf
->b_p_bl
? ' ' : 'u',
2481 buf
== curbuf
? '%' :
2482 (curwin
->w_alt_fnum
== buf
->b_fnum
? '#' : ' '),
2483 buf
->b_ml
.ml_mfp
== NULL
? ' ' :
2484 (buf
->b_nwindows
== 0 ? 'h' : 'a'),
2485 !buf
->b_p_ma
? '-' : (buf
->b_p_ro
? '=' : ' '),
2486 (buf
->b_flags
& BF_READERR
) ? 'x'
2487 : (bufIsChanged(buf
) ? '+' : ' '),
2490 /* put "line 999" in column 40 or after the file name */
2491 i
= 40 - vim_strsize(IObuff
);
2494 IObuff
[len
++] = ' ';
2495 } while (--i
> 0 && len
< IOSIZE
- 18);
2496 vim_snprintf((char *)IObuff
+ len
, IOSIZE
- len
, _("line %ld"),
2497 buf
== curbuf
? curwin
->w_cursor
.lnum
2498 : (long)buflist_findlnum(buf
));
2499 msg_outtrans(IObuff
);
2500 out_flush(); /* output one line at a time */
2507 * Get file name and line number for file 'fnum'.
2508 * Used by DoOneCmd() for translating '%' and '#'.
2509 * Used by insert_reg() and cmdline_paste() for '#' register.
2510 * Return FAIL if not found, OK for success.
2513 buflist_name_nr(fnum
, fname
, lnum
)
2520 buf
= buflist_findnr(fnum
);
2521 if (buf
== NULL
|| buf
->b_fname
== NULL
)
2524 *fname
= buf
->b_fname
;
2525 *lnum
= buflist_findlnum(buf
);
2531 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2532 * The file name with the full path is also remembered, for when :cd is used.
2533 * Returns FAIL for failure (file name already in use by other buffer)
2537 setfname(buf
, ffname
, sfname
, message
)
2539 char_u
*ffname
, *sfname
;
2540 int message
; /* give message when buffer already exists */
2547 if (ffname
== NULL
|| *ffname
== NUL
)
2549 /* Removing the name. */
2550 vim_free(buf
->b_ffname
);
2551 vim_free(buf
->b_sfname
);
2552 buf
->b_ffname
= NULL
;
2553 buf
->b_sfname
= NULL
;
2555 st
.st_dev
= (dev_T
)-1;
2560 fname_expand(buf
, &ffname
, &sfname
); /* will allocate ffname */
2561 if (ffname
== NULL
) /* out of memory */
2565 * if the file name is already used in another buffer:
2566 * - if the buffer is loaded, fail
2567 * - if the buffer is not loaded, delete it from the list
2570 if (mch_stat((char *)ffname
, &st
) < 0)
2571 st
.st_dev
= (dev_T
)-1;
2573 if (!(buf
->b_flags
& BF_DUMMY
))
2575 obuf
= buflist_findname_stat(ffname
, &st
);
2577 obuf
= buflist_findname(ffname
);
2579 if (obuf
!= NULL
&& obuf
!= buf
)
2581 if (obuf
->b_ml
.ml_mfp
!= NULL
) /* it's loaded, fail */
2584 EMSG(_("E95: Buffer with this name already exists"));
2588 close_buffer(NULL
, obuf
, DOBUF_WIPE
); /* delete from the list */
2590 sfname
= vim_strsave(sfname
);
2591 if (ffname
== NULL
|| sfname
== NULL
)
2597 #ifdef USE_FNAME_CASE
2598 # ifdef USE_LONG_FNAME
2601 fname_case(sfname
, 0); /* set correct case for short file name */
2603 vim_free(buf
->b_ffname
);
2604 vim_free(buf
->b_sfname
);
2605 buf
->b_ffname
= ffname
;
2606 buf
->b_sfname
= sfname
;
2608 buf
->b_fname
= buf
->b_sfname
;
2610 if (st
.st_dev
== (dev_T
)-1)
2614 buf
->b_dev
= st
.st_dev
;
2615 buf
->b_ino
= st
.st_ino
;
2620 buf
->b_shortname
= FALSE
;
2623 buf_name_changed(buf
);
2628 * Crude way of changing the name of a buffer. Use with care!
2629 * The name should be relative to the current directory.
2632 buf_set_name(fnum
, name
)
2638 buf
= buflist_findnr(fnum
);
2641 vim_free(buf
->b_sfname
);
2642 vim_free(buf
->b_ffname
);
2643 buf
->b_ffname
= vim_strsave(name
);
2644 buf
->b_sfname
= NULL
;
2645 /* Allocate ffname and expand into full path. Also resolves .lnk
2646 * files on Win32. */
2647 fname_expand(buf
, &buf
->b_ffname
, &buf
->b_sfname
);
2648 buf
->b_fname
= buf
->b_sfname
;
2653 * Take care of what needs to be done when the name of buffer "buf" has
2657 buf_name_changed(buf
)
2661 * If the file name changed, also change the name of the swapfile
2663 if (buf
->b_ml
.ml_mfp
!= NULL
)
2666 if (curwin
->w_buffer
== buf
)
2667 check_arg_idx(curwin
); /* check file name for arg list */
2669 maketitle(); /* set window title */
2672 status_redraw_all(); /* status lines need to be redrawn */
2674 fmarks_check_names(buf
); /* check named file marks */
2675 ml_timestamp(buf
); /* reset timestamp */
2679 * set alternate file name for current window
2681 * Used by do_one_cmd(), do_write() and do_ecmd().
2682 * Return the buffer.
2685 setaltfname(ffname
, sfname
, lnum
)
2692 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2693 buf
= buflist_new(ffname
, sfname
, lnum
, 0);
2694 if (buf
!= NULL
&& !cmdmod
.keepalt
)
2695 curwin
->w_alt_fnum
= buf
->b_fnum
;
2700 * Get alternate file name for current window.
2701 * Return NULL if there isn't any, and give error message if requested.
2705 int errmsg
; /* give error message */
2710 if (buflist_name_nr(0, &fname
, &dummy
) == FAIL
)
2720 * Add a file name to the buflist and return its number.
2721 * Uses same flags as buflist_new(), except BLN_DUMMY.
2723 * used by qf_init(), main() and doarglist()
2726 buflist_add(fname
, flags
)
2732 buf
= buflist_new(fname
, NULL
, (linenr_T
)0, flags
);
2738 #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2740 * Adjust slashes in file names. Called after 'shellslash' was set.
2743 buflist_slash_adjust()
2747 for (bp
= firstbuf
; bp
!= NULL
; bp
= bp
->b_next
)
2749 if (bp
->b_ffname
!= NULL
)
2750 slash_adjust(bp
->b_ffname
);
2751 if (bp
->b_sfname
!= NULL
)
2752 slash_adjust(bp
->b_sfname
);
2758 * Set alternate cursor position for current window.
2759 * Also save the local window option values.
2764 buflist_setfpos(curbuf
, curwin
, curwin
->w_cursor
.lnum
,
2765 curwin
->w_cursor
.col
, TRUE
);
2769 * Return TRUE if 'ffname' is not the same file as current file.
2770 * Fname must have a full path (expanded by mch_FullName()).
2776 return otherfile_buf(curbuf
, ffname
2784 otherfile_buf(buf
, ffname
2795 /* no name is different */
2796 if (ffname
== NULL
|| *ffname
== NUL
|| buf
->b_ffname
== NULL
)
2798 if (fnamecmp(ffname
, buf
->b_ffname
) == 0)
2804 /* If no struct stat given, get it now */
2807 if (buf
->b_dev
< 0 || mch_stat((char *)ffname
, &st
) < 0)
2808 st
.st_dev
= (dev_T
)-1;
2811 /* Use dev/ino to check if the files are the same, even when the names
2812 * are different (possible with links). Still need to compare the
2813 * name above, for when the file doesn't exist yet.
2814 * Problem: The dev/ino changes when a file is deleted (and created
2815 * again) and remains the same when renamed/moved. We don't want to
2816 * mch_stat() each buffer each time, that would be too slow. Get the
2817 * dev/ino again when they appear to match, but not when they appear
2818 * to be different: Could skip a buffer when it's actually the same
2820 if (buf_same_ino(buf
, stp
))
2823 if (buf_same_ino(buf
, stp
))
2831 #if defined(UNIX) || defined(PROTO)
2833 * Set inode and device number for a buffer.
2834 * Must always be called when b_fname is changed!.
2842 if (buf
->b_fname
!= NULL
&& mch_stat((char *)buf
->b_fname
, &st
) >= 0)
2844 buf
->b_dev
= st
.st_dev
;
2845 buf
->b_ino
= st
.st_ino
;
2852 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
2855 buf_same_ino(buf
, stp
)
2859 return (buf
->b_dev
>= 0
2860 && stp
->st_dev
== buf
->b_dev
2861 && stp
->st_ino
== buf
->b_ino
);
2866 * Print info about the current buffer.
2869 fileinfo(fullname
, shorthelp
, dont_truncate
)
2870 int fullname
; /* when non-zero print full path */
2880 buffer
= alloc(IOSIZE
);
2884 if (fullname
> 1) /* 2 CTRL-G: include buffer number */
2886 sprintf((char *)buffer
, "buf %d: ", curbuf
->b_fnum
);
2887 p
= buffer
+ STRLEN(buffer
);
2893 if (buf_spname(curbuf
) != NULL
)
2894 STRCPY(p
, buf_spname(curbuf
));
2897 if (!fullname
&& curbuf
->b_fname
!= NULL
)
2898 name
= curbuf
->b_fname
;
2900 name
= curbuf
->b_ffname
;
2901 home_replace(shorthelp
? curbuf
: NULL
, name
, p
,
2902 (int)(IOSIZE
- (p
- buffer
)), TRUE
);
2905 len
= STRLEN(buffer
);
2906 vim_snprintf((char *)buffer
+ len
, IOSIZE
- len
,
2908 curbufIsChanged() ? (shortmess(SHM_MOD
)
2909 ? " [+]" : _(" [Modified]")) : " ",
2910 (curbuf
->b_flags
& BF_NOTEDITED
)
2911 #ifdef FEAT_QUICKFIX
2912 && !bt_dontwrite(curbuf
)
2914 ? _("[Not edited]") : "",
2915 (curbuf
->b_flags
& BF_NEW
)
2916 #ifdef FEAT_QUICKFIX
2917 && !bt_dontwrite(curbuf
)
2919 ? _("[New file]") : "",
2920 (curbuf
->b_flags
& BF_READERR
) ? _("[Read errors]") : "",
2921 curbuf
->b_p_ro
? (shortmess(SHM_RO
) ? "[RO]"
2922 : _("[readonly]")) : "",
2923 (curbufIsChanged() || (curbuf
->b_flags
& BF_WRITE_MASK
)
2924 || curbuf
->b_p_ro
) ?
2926 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
2927 * causes an overflow, thus for large numbers divide instead. */
2928 if (curwin
->w_cursor
.lnum
> 1000000L)
2929 n
= (int)(((long)curwin
->w_cursor
.lnum
) /
2930 ((long)curbuf
->b_ml
.ml_line_count
/ 100L));
2932 n
= (int)(((long)curwin
->w_cursor
.lnum
* 100L) /
2933 (long)curbuf
->b_ml
.ml_line_count
);
2934 len
= STRLEN(buffer
);
2935 if (curbuf
->b_ml
.ml_flags
& ML_EMPTY
)
2937 vim_snprintf((char *)buffer
+ len
, IOSIZE
- len
, "%s", _(no_lines_msg
));
2939 #ifdef FEAT_CMDL_INFO
2942 /* Current line and column are already on the screen -- webb */
2943 if (curbuf
->b_ml
.ml_line_count
== 1)
2944 vim_snprintf((char *)buffer
+ len
, IOSIZE
- len
,
2945 _("1 line --%d%%--"), n
);
2947 vim_snprintf((char *)buffer
+ len
, IOSIZE
- len
,
2948 _("%ld lines --%d%%--"),
2949 (long)curbuf
->b_ml
.ml_line_count
, n
);
2954 vim_snprintf((char *)buffer
+ len
, IOSIZE
- len
,
2955 _("line %ld of %ld --%d%%-- col "),
2956 (long)curwin
->w_cursor
.lnum
,
2957 (long)curbuf
->b_ml
.ml_line_count
,
2960 col_print(buffer
+ STRLEN(buffer
),
2961 (int)curwin
->w_cursor
.col
+ 1, (int)curwin
->w_virtcol
+ 1);
2964 (void)append_arg_number(curwin
, buffer
, !shortmess(SHM_FILE
), IOSIZE
);
2968 /* Temporarily set msg_scroll to avoid the message being truncated.
2969 * First call msg_start() to get the message in the right place. */
2978 p
= msg_trunc_attr(buffer
, FALSE
, 0);
2979 if (restart_edit
!= 0 || (msg_scrolled
&& !need_wait_return
))
2980 /* Need to repeat the message after redrawing when:
2981 * - When restart_edit is set (otherwise there will be a delay
2982 * before redrawing).
2983 * - When the screen was scrolled but there is no wait-return
2992 col_print(buf
, col
, vcol
)
2998 sprintf((char *)buf
, "%d", col
);
3000 sprintf((char *)buf
, "%d-%d", col
, vcol
);
3003 #if defined(FEAT_TITLE) || defined(PROTO)
3005 * put file name in title bar of window and in icon title
3008 static char_u
*lasttitle
= NULL
;
3009 static char_u
*lasticon
= NULL
;
3015 char_u
*t_str
= NULL
;
3017 char_u
*i_str
= NULL
;
3026 /* Postpone updating the title when 'lazyredraw' is set. */
3027 need_maketitle
= TRUE
;
3031 need_maketitle
= FALSE
;
3032 if (!p_title
&& !p_icon
)
3039 maxlen
= p_titlelen
* Columns
/ 100;
3045 if (*p_titlestring
!= NUL
)
3048 if (stl_syntax
& STL_IN_TITLE
)
3050 int use_sandbox
= FALSE
;
3051 int save_called_emsg
= called_emsg
;
3054 use_sandbox
= was_set_insecurely((char_u
*)"titlestring", 0);
3056 called_emsg
= FALSE
;
3057 build_stl_str_hl(curwin
, t_str
, sizeof(buf
),
3058 p_titlestring
, use_sandbox
,
3059 0, maxlen
, NULL
, NULL
);
3061 set_string_option_direct((char_u
*)"titlestring", -1,
3062 (char_u
*)"", OPT_FREE
, SID_ERROR
);
3063 called_emsg
|= save_called_emsg
;
3067 t_str
= p_titlestring
;
3071 /* format: "fname + (path) (1 of 2) - VIM" */
3073 if (curbuf
->b_fname
== NULL
)
3074 STRCPY(buf
, _("[No Name]"));
3077 p
= transstr(gettail(curbuf
->b_fname
));
3078 vim_strncpy(buf
, p
, IOSIZE
- 100);
3082 switch (bufIsChanged(curbuf
)
3083 + (curbuf
->b_p_ro
* 2)
3084 + (!curbuf
->b_p_ma
* 4))
3086 case 1: STRCAT(buf
, " +"); break;
3087 case 2: STRCAT(buf
, " ="); break;
3088 case 3: STRCAT(buf
, " =+"); break;
3090 case 6: STRCAT(buf
, " -"); break;
3092 case 7: STRCAT(buf
, " -+"); break;
3095 if (curbuf
->b_fname
!= NULL
)
3097 /* Get path of file, replace home dir with ~ */
3098 off
= (int)STRLEN(buf
);
3101 home_replace(curbuf
, curbuf
->b_ffname
,
3102 buf
+ off
, IOSIZE
- off
, TRUE
);
3103 #ifdef BACKSLASH_IN_FILENAME
3104 /* avoid "c:/name" to be reduced to "c" */
3105 if (isalpha(buf
[off
]) && buf
[off
+ 1] == ':')
3108 /* remove the file name */
3109 p
= gettail_sep(buf
+ off
);
3111 /* must be a help buffer */
3112 vim_strncpy(buf
+ off
, (char_u
*)_("help"),
3117 /* translate unprintable chars */
3118 p
= transstr(buf
+ off
);
3119 vim_strncpy(buf
+ off
, p
, IOSIZE
- off
- 1);
3124 append_arg_number(curwin
, buf
, FALSE
, IOSIZE
);
3126 #if defined(FEAT_CLIENTSERVER)
3127 if (serverName
!= NULL
)
3130 STRCAT(buf
, serverName
);
3134 STRCAT(buf
, " - VIM");
3138 /* make it shorter by removing a bit in the middle */
3139 len
= vim_strsize(buf
);
3141 trunc_string(buf
, buf
, maxlen
);
3145 mustset
= ti_change(t_str
, &lasttitle
);
3150 if (*p_iconstring
!= NUL
)
3153 if (stl_syntax
& STL_IN_ICON
)
3155 int use_sandbox
= FALSE
;
3156 int save_called_emsg
= called_emsg
;
3159 use_sandbox
= was_set_insecurely((char_u
*)"iconstring", 0);
3161 called_emsg
= FALSE
;
3162 build_stl_str_hl(curwin
, i_str
, sizeof(buf
),
3163 p_iconstring
, use_sandbox
,
3166 set_string_option_direct((char_u
*)"iconstring", -1,
3167 (char_u
*)"", OPT_FREE
, SID_ERROR
);
3168 called_emsg
|= save_called_emsg
;
3172 i_str
= p_iconstring
;
3176 if (buf_spname(curbuf
) != NULL
)
3177 i_name
= (char_u
*)buf_spname(curbuf
);
3178 else /* use file name only in icon */
3179 i_name
= gettail(curbuf
->b_ffname
);
3181 /* Truncate name at 100 bytes. */
3182 len
= (int)STRLEN(i_name
);
3188 len
+= (*mb_tail_off
)(i_name
, i_name
+ len
) + 1;
3192 STRCPY(i_str
, i_name
);
3193 trans_characters(i_str
, IOSIZE
);
3197 mustset
|= ti_change(i_str
, &lasticon
);
3204 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3205 * from "str" if it does.
3206 * Return TRUE when "*last" changed.
3209 ti_change(str
, last
)
3213 if ((str
== NULL
) != (*last
== NULL
)
3214 || (str
!= NULL
&& *last
!= NULL
&& STRCMP(str
, *last
) != 0))
3220 *last
= vim_strsave(str
);
3227 * Put current window title back (used after calling a shell)
3232 mch_settitle(lasttitle
, lasticon
);
3235 # if defined(EXITFREE) || defined(PROTO)
3239 vim_free(lasttitle
);
3244 #endif /* FEAT_TITLE */
3246 #if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
3248 * Build a string from the status line items in "fmt".
3249 * Return length of string in screen cells.
3251 * Normally works for window "wp", except when working for 'tabline' then it
3254 * Items are drawn interspersed with the text that surrounds it
3255 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3256 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3258 * If maxwidth is not zero, the string will be filled at any middle marker
3259 * or truncated if too long, fillchar is used for all whitespace.
3263 build_stl_str_hl(wp
, out
, outlen
, fmt
, use_sandbox
, fillchar
, maxwidth
, hltab
, tabtab
)
3265 char_u
*out
; /* buffer to write into != NameBuff */
3266 size_t outlen
; /* length of out[] */
3268 int use_sandbox
; /* "fmt" was set insecurely, use sandbox */
3271 struct stl_hlrec
*hltab
; /* return: HL attributes (can be NULL) */
3272 struct stl_hlrec
*tabtab
; /* return: tab page nrs (can be NULL) */
3286 int prevchar_isflag
;
3287 int prevchar_isitem
;
3295 int groupitem
[STL_MAX_ITEM
];
3312 } item
[STL_MAX_ITEM
];
3320 char_u
*usefmt
= fmt
;
3321 struct stl_hlrec
*sp
;
3325 * When the format starts with "%!" then evaluate it as an expression and
3326 * use the result as the actual format string.
3328 if (fmt
[0] == '%' && fmt
[1] == '!')
3330 usefmt
= eval_to_string_safe(fmt
+ 2, NULL
, use_sandbox
);
3339 /* Can't handle a multi-byte fill character yet. */
3340 else if (mb_char2len(fillchar
) > 1)
3345 * Get line & check if empty (cursorpos will show "0-1").
3346 * If inversion is possible we use it. Else '=' characters are used.
3348 linecont
= ml_get_buf(wp
->w_buffer
, wp
->w_cursor
.lnum
, FALSE
);
3349 empty_line
= (*linecont
== NUL
);
3354 prevchar_isflag
= TRUE
;
3355 prevchar_isitem
= FALSE
;
3356 for (s
= usefmt
; *s
; )
3358 if (*s
!= NUL
&& *s
!= '%')
3359 prevchar_isflag
= prevchar_isitem
= FALSE
;
3362 * Handle up to the next '%' or the end.
3364 while (*s
!= NUL
&& *s
!= '%' && p
+ 1 < out
+ outlen
)
3366 if (*s
== NUL
|| p
+ 1 >= out
+ outlen
)
3370 * Handle one '%' item.
3375 if (p
+ 1 >= out
+ outlen
)
3378 prevchar_isflag
= prevchar_isitem
= FALSE
;
3381 if (*s
== STL_MIDDLEMARK
)
3386 item
[curitem
].type
= Middle
;
3387 item
[curitem
++].start
= p
;
3390 if (*s
== STL_TRUNCMARK
)
3393 item
[curitem
].type
= Trunc
;
3394 item
[curitem
++].start
= p
;
3404 t
= item
[groupitem
[groupdepth
]].start
;
3407 if (curitem
> groupitem
[groupdepth
] + 1
3408 && item
[groupitem
[groupdepth
]].minwid
== 0)
3410 /* remove group if all items are empty */
3411 for (n
= groupitem
[groupdepth
] + 1; n
< curitem
; n
++)
3412 if (item
[n
].type
== Normal
)
3420 if (l
> item
[groupitem
[groupdepth
]].maxwid
)
3422 /* truncate, remove n bytes of text at the start */
3426 /* Find the first character that should be included. */
3428 while (l
>= item
[groupitem
[groupdepth
]].maxwid
)
3430 l
-= ptr2cells(t
+ n
);
3431 n
+= (*mb_ptr2len
)(t
+ n
);
3436 n
= (long)(p
- t
) - item
[groupitem
[groupdepth
]].maxwid
+ 1;
3439 mch_memmove(t
+ 1, t
+ n
, p
- (t
+ n
));
3442 /* Fill up space left over by half a double-wide char. */
3443 while (++l
< item
[groupitem
[groupdepth
]].minwid
)
3447 /* correct the start of the items for the truncation */
3448 for (l
= groupitem
[groupdepth
] + 1; l
< curitem
; l
++)
3451 if (item
[l
].start
< t
)
3455 else if (abs(item
[groupitem
[groupdepth
]].minwid
) > l
)
3458 n
= item
[groupitem
[groupdepth
]].minwid
;
3461 /* fill by appending characters */
3463 while (l
++ < n
&& p
+ 1 < out
+ outlen
)
3468 /* fill by inserting characters */
3469 mch_memmove(t
+ n
- l
, t
, p
- t
);
3471 if (p
+ l
>= out
+ outlen
)
3472 l
= (long)((out
+ outlen
) - p
- 1);
3474 for (n
= groupitem
[groupdepth
] + 1; n
< curitem
; n
++)
3496 if (VIM_ISDIGIT(*s
))
3498 minwid
= (int)getdigits(&s
);
3499 if (minwid
< 0) /* overflow */
3502 if (*s
== STL_USER_HL
)
3504 item
[curitem
].type
= Highlight
;
3505 item
[curitem
].start
= p
;
3506 item
[curitem
].minwid
= minwid
> 9 ? 1 : minwid
;
3511 if (*s
== STL_TABPAGENR
|| *s
== STL_TABCLOSENR
)
3513 if (*s
== STL_TABCLOSENR
)
3517 /* %X ends the close label, go back to the previously
3518 * define tab label nr. */
3519 for (n
= curitem
- 1; n
>= 0; --n
)
3520 if (item
[n
].type
== TabPage
&& item
[n
].minwid
>= 0)
3522 minwid
= item
[n
].minwid
;
3527 /* close nrs are stored as negative values */
3530 item
[curitem
].type
= TabPage
;
3531 item
[curitem
].start
= p
;
3532 item
[curitem
].minwid
= minwid
;
3540 if (VIM_ISDIGIT(*s
))
3542 maxwid
= (int)getdigits(&s
);
3543 if (maxwid
<= 0) /* overflow */
3547 minwid
= (minwid
> 50 ? 50 : minwid
) * l
;
3550 groupitem
[groupdepth
++] = curitem
;
3551 item
[curitem
].type
= Group
;
3552 item
[curitem
].start
= p
;
3553 item
[curitem
].minwid
= minwid
;
3554 item
[curitem
].maxwid
= maxwid
;
3559 if (vim_strchr(STL_ALL
, *s
) == NULL
)
3566 /* OK - now for the real work */
3577 fillable
= FALSE
; /* don't change ' ' to fillchar */
3578 if (buf_spname(wp
->w_buffer
) != NULL
)
3579 STRCPY(NameBuff
, buf_spname(wp
->w_buffer
));
3582 t
= (opt
== STL_FULLPATH
) ? wp
->w_buffer
->b_ffname
3583 : wp
->w_buffer
->b_fname
;
3584 home_replace(wp
->w_buffer
, t
, NameBuff
, MAXPATHL
, TRUE
);
3586 trans_characters(NameBuff
, MAXPATHL
);
3587 if (opt
!= STL_FILENAME
)
3590 str
= gettail(NameBuff
);
3593 case STL_VIM_EXPR
: /* '{' */
3596 while (*s
!= '}' && *s
!= NUL
&& p
+ 1 < out
+ outlen
)
3598 if (*s
!= '}') /* missing '}' or out of space */
3605 sprintf((char *)tmp
, "%d", curbuf
->b_fnum
);
3606 set_internal_string_var((char_u
*)"actual_curbuf", tmp
);
3611 curbuf
= wp
->w_buffer
;
3613 str
= eval_to_string_safe(p
, &t
, use_sandbox
);
3617 do_unlet((char_u
*)"g:actual_curbuf", TRUE
);
3619 if (str
!= NULL
&& *str
!= 0)
3621 if (*skipdigits(str
) == NUL
)
3623 num
= atoi((char *)str
);
3633 num
= (wp
->w_buffer
->b_ml
.ml_flags
& ML_EMPTY
)
3634 ? 0L : (long)(wp
->w_cursor
.lnum
);
3638 num
= wp
->w_buffer
->b_ml
.ml_line_count
;
3642 num
= !(State
& INSERT
) && empty_line
3643 ? 0 : (int)wp
->w_cursor
.col
+ 1;
3647 case STL_VIRTCOL_ALT
:
3648 /* In list mode virtcol needs to be recomputed */
3649 virtcol
= wp
->w_virtcol
;
3650 if (wp
->w_p_list
&& lcs_tab1
== NUL
)
3652 wp
->w_p_list
= FALSE
;
3653 getvcol(wp
, &wp
->w_cursor
, NULL
, &virtcol
, NULL
);
3654 wp
->w_p_list
= TRUE
;
3657 /* Don't display %V if it's the same as %c. */
3658 if (opt
== STL_VIRTCOL_ALT
3659 && (virtcol
== (colnr_T
)(!(State
& INSERT
) && empty_line
3660 ? 0 : (int)wp
->w_cursor
.col
+ 1)))
3662 num
= (long)virtcol
;
3665 case STL_PERCENTAGE
:
3666 num
= (int)(((long)wp
->w_cursor
.lnum
* 100L) /
3667 (long)wp
->w_buffer
->b_ml
.ml_line_count
);
3670 case STL_ALTPERCENT
:
3672 get_rel_pos(wp
, str
);
3675 case STL_ARGLISTSTAT
:
3678 if (append_arg_number(wp
, tmp
, FALSE
, (int)sizeof(tmp
)))
3684 if (get_keymap_str(wp
, tmp
, TMPLEN
))
3688 #if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
3689 num
= printer_page_num
;
3696 num
= wp
->w_buffer
->b_fnum
;
3703 l
= ml_find_line_or_offset(wp
->w_buffer
, wp
->w_cursor
.lnum
, NULL
);
3704 num
= (wp
->w_buffer
->b_ml
.ml_flags
& ML_EMPTY
) || l
< 0 ?
3705 0L : l
+ 1 + (!(State
& INSERT
) && empty_line
?
3706 0 : (int)wp
->w_cursor
.col
);
3713 if (((State
& INSERT
) && wp
== curwin
) || empty_line
)
3718 num
= (*mb_ptr2char
)(linecont
+ wp
->w_cursor
.col
);
3720 num
= linecont
[wp
->w_cursor
.col
];
3725 else if (num
== CAR
&& get_fileformat(wp
->w_buffer
) == EOL_MAC
)
3730 case STL_ROFLAG_ALT
:
3732 if (wp
->w_buffer
->b_p_ro
)
3733 str
= (char_u
*)((opt
== STL_ROFLAG_ALT
) ? ",RO" : "[RO]");
3737 case STL_HELPFLAG_ALT
:
3739 if (wp
->w_buffer
->b_help
)
3740 str
= (char_u
*)((opt
== STL_HELPFLAG_ALT
) ? ",HLP"
3746 if (*wp
->w_buffer
->b_p_ft
!= NUL
3747 && STRLEN(wp
->w_buffer
->b_p_ft
) < TMPLEN
- 3)
3749 vim_snprintf((char *)tmp
, sizeof(tmp
), "[%s]",
3750 wp
->w_buffer
->b_p_ft
);
3755 case STL_FILETYPE_ALT
:
3757 if (*wp
->w_buffer
->b_p_ft
!= NUL
3758 && STRLEN(wp
->w_buffer
->b_p_ft
) < TMPLEN
- 2)
3760 vim_snprintf((char *)tmp
, sizeof(tmp
), ",%s",
3761 wp
->w_buffer
->b_p_ft
);
3762 for (t
= tmp
; *t
!= 0; t
++)
3763 *t
= TOUPPER_LOC(*t
);
3769 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3770 case STL_PREVIEWFLAG
:
3771 case STL_PREVIEWFLAG_ALT
:
3774 str
= (char_u
*)((opt
== STL_PREVIEWFLAG_ALT
) ? ",PRV"
3780 case STL_MODIFIED_ALT
:
3782 switch ((opt
== STL_MODIFIED_ALT
)
3783 + bufIsChanged(wp
->w_buffer
) * 2
3784 + (!wp
->w_buffer
->b_p_ma
) * 4)
3786 case 2: str
= (char_u
*)"[+]"; break;
3787 case 3: str
= (char_u
*)",+"; break;
3788 case 4: str
= (char_u
*)"[-]"; break;
3789 case 5: str
= (char_u
*)",-"; break;
3790 case 6: str
= (char_u
*)"[+-]"; break;
3791 case 7: str
= (char_u
*)",+-"; break;
3797 while (*s
!= '#' && *s
!= NUL
)
3801 item
[curitem
].type
= Highlight
;
3802 item
[curitem
].start
= p
;
3803 item
[curitem
].minwid
= -syn_namen2id(t
, (int)(s
- t
));
3810 item
[curitem
].start
= p
;
3811 item
[curitem
].type
= Normal
;
3812 if (str
!= NULL
&& *str
)
3818 && ((!prevchar_isitem
&& *t
== ',')
3819 || (prevchar_isflag
&& *t
== ' ')))
3821 prevchar_isflag
= TRUE
;
3825 prevchar_isitem
= TRUE
;
3833 t
+= (*mb_ptr2len
)(t
);
3837 l
-= byte2cells(*t
++);
3838 if (p
+ 1 >= out
+ outlen
)
3844 for (; l
< minwid
&& p
+ 1 < out
+ outlen
; l
++)
3846 /* Don't put a "-" in front of a digit. */
3847 if (l
+ 1 == minwid
&& fillchar
== '-' && VIM_ISDIGIT(*t
))
3856 while (*t
&& p
+ 1 < out
+ outlen
)
3859 /* Change a space by fillchar, unless fillchar is '-' and a
3861 if (fillable
&& p
[-1] == ' '
3862 && (!VIM_ISDIGIT(*t
) || fillchar
!= '-'))
3865 for (; l
< minwid
&& p
+ 1 < out
+ outlen
; l
++)
3870 int nbase
= (base
== 'D' ? 10 : (base
== 'O' ? 8 : 16));
3873 if (p
+ 20 >= out
+ outlen
)
3874 break; /* not sufficient space */
3875 prevchar_isitem
= TRUE
;
3877 if (opt
== STL_VIRTCOL_ALT
)
3886 *t
++ = nbase
== 16 ? base
: (nbase
== 8 ? 'o' : 'd');
3889 for (n
= num
, l
= 1; n
>= nbase
; n
/= nbase
)
3891 if (opt
== STL_VIRTCOL_ALT
)
3897 while (l
-- > maxwid
)
3903 vim_snprintf((char *)p
, outlen
- (p
- out
), (char *)nstr
,
3907 vim_snprintf((char *)p
, outlen
- (p
- out
), (char *)nstr
,
3912 item
[curitem
].type
= Empty
;
3914 if (opt
== STL_VIM_EXPR
)
3917 if (num
>= 0 || (!itemisflag
&& str
&& *str
))
3918 prevchar_isflag
= FALSE
; /* Item not NULL, but not a flag */
3929 width
= vim_strsize(out
);
3930 if (maxwidth
> 0 && width
> maxwidth
)
3932 /* Result is too long, must trunctate somewhere. */
3938 for ( ; l
< itemcnt
; l
++)
3939 if (item
[l
].type
== Trunc
)
3941 /* Truncate at %< item. */
3947 /* No %< item, truncate first item. */
3953 if (width
- vim_strsize(s
) >= maxwidth
)
3955 /* Truncation mark is beyond max length */
3963 width
+= ptr2cells(s
);
3964 if (width
>= maxwidth
)
3966 s
+= (*mb_ptr2len
)(s
);
3968 /* Fill up for half a double-wide character. */
3969 while (++width
< maxwidth
)
3974 s
= out
+ maxwidth
- 1;
3975 for (l
= 0; l
< itemcnt
; l
++)
3976 if (item
[l
].start
> s
)
3988 while (width
>= maxwidth
)
3990 width
-= ptr2cells(s
+ n
);
3991 n
+= (*mb_ptr2len
)(s
+ n
);
3996 n
= width
- maxwidth
+ 1;
3998 mch_memmove(s
+ 1, p
, STRLEN(p
) + 1);
4001 /* Fill up for half a double-wide character. */
4002 while (++width
< maxwidth
)
4009 --n
; /* count the '<' */
4010 for (; l
< itemcnt
; l
++)
4012 if (item
[l
].start
- n
>= s
)
4020 else if (width
< maxwidth
&& STRLEN(out
) + maxwidth
- width
+ 1 < outlen
)
4022 /* Apply STL_MIDDLE if any */
4023 for (l
= 0; l
< itemcnt
; l
++)
4024 if (item
[l
].type
== Middle
)
4028 p
= item
[l
].start
+ maxwidth
- width
;
4029 mch_memmove(p
, item
[l
].start
, STRLEN(item
[l
].start
) + 1);
4030 for (s
= item
[l
].start
; s
< p
; s
++)
4032 for (l
++; l
< itemcnt
; l
++)
4033 item
[l
].start
+= maxwidth
- width
;
4038 /* Store the info about highlighting. */
4042 for (l
= 0; l
< itemcnt
; l
++)
4044 if (item
[l
].type
== Highlight
)
4046 sp
->start
= item
[l
].start
;
4047 sp
->userhl
= item
[l
].minwid
;
4055 /* Store the info about tab pages labels. */
4059 for (l
= 0; l
< itemcnt
; l
++)
4061 if (item
[l
].type
== TabPage
)
4063 sp
->start
= item
[l
].start
;
4064 sp
->userhl
= item
[l
].minwid
;
4074 #endif /* FEAT_STL_OPT */
4076 #if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4077 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
4079 * Get relative cursor position in window into "str[]", in the form 99%, using
4080 * "Top", "Bot" or "All" when appropriate.
4083 get_rel_pos(wp
, str
)
4087 long above
; /* number of lines above window */
4088 long below
; /* number of lines below window */
4090 above
= wp
->w_topline
- 1;
4092 above
+= diff_check_fill(wp
, wp
->w_topline
) - wp
->w_topfill
;
4094 below
= wp
->w_buffer
->b_ml
.ml_line_count
- wp
->w_botline
+ 1;
4096 STRCPY(str
, above
== 0 ? _("All") : _("Bot"));
4097 else if (above
<= 0)
4098 STRCPY(str
, _("Top"));
4100 sprintf((char *)str
, "%2d%%", above
> 1000000L
4101 ? (int)(above
/ ((above
+ below
) / 100L))
4102 : (int)(above
* 100L / (above
+ below
)));
4107 * Append (file 2 of 8) to 'buf', if editing more than one file.
4108 * Return TRUE if it was appended.
4111 append_arg_number(wp
, buf
, add_file
, maxlen
)
4114 int add_file
; /* Add "file" before the arg number */
4115 int maxlen
; /* maximum nr of chars in buf or zero*/
4119 if (ARGCOUNT
<= 1) /* nothing to do */
4122 p
= buf
+ STRLEN(buf
); /* go to the end of the buffer */
4123 if (maxlen
&& p
- buf
+ 35 >= maxlen
) /* getting too long */
4132 sprintf((char *)p
, wp
->w_arg_idx_invalid
? "(%d) of %d)"
4133 : "%d of %d)", wp
->w_arg_idx
+ 1, ARGCOUNT
);
4138 * If fname is not a full path, make it a full path.
4139 * Returns pointer to allocated memory (NULL for failure).
4146 * Force expanding the path always for Unix, because symbolic links may
4147 * mess up the full path name, even though it starts with a '/'.
4148 * Also expand when there is ".." in the file name, try to remove it,
4149 * because "c:/src/../README" is equal to "c:/README".
4150 * For MS-Windows also expand names like "longna~1" to "longname".
4153 return FullName_save(fname
, TRUE
);
4155 if (!vim_isAbsName(fname
) || strstr((char *)fname
, "..") != NULL
4156 #if defined(MSWIN) || defined(DJGPP)
4157 || vim_strchr(fname
, '~') != NULL
4160 return FullName_save(fname
, FALSE
);
4162 fname
= vim_strsave(fname
);
4164 #ifdef USE_FNAME_CASE
4165 # ifdef USE_LONG_FNAME
4170 fname_case(fname
, 0); /* set correct case for file name */
4179 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4180 * "ffname" becomes a pointer to allocated memory (or NULL).
4184 fname_expand(buf
, ffname
, sfname
)
4189 if (*ffname
== NULL
) /* if no file name given, nothing to do */
4191 if (*sfname
== NULL
) /* if no short file name given, use ffname */
4193 *ffname
= fix_fname(*ffname
); /* expand to full path */
4195 #ifdef FEAT_SHORTCUT
4200 /* If the file name is a shortcut file, use the file it links to. */
4201 rfname
= mch_resolve_shortcut(*ffname
);
4213 * Get the file name for an argument list entry.
4221 /* Use the name from the associated buffer if it exists. */
4222 bp
= buflist_findnr(aep
->ae_fnum
);
4223 if (bp
== NULL
|| bp
->b_fname
== NULL
)
4224 return aep
->ae_fname
;
4228 #if defined(FEAT_WINDOWS) || defined(PROTO)
4230 * do_arg_all(): Open up to 'count' windows, one for each argument.
4233 do_arg_all(count
, forceit
, keep_tabs
)
4235 int forceit
; /* hide buffers in current windows */
4236 int keep_tabs
; /* keep curren tabs, for ":tab drop file" */
4240 char_u
*opened
; /* array of flags for which args are open */
4241 int opened_len
; /* lenght of opened[] */
4242 int use_firstwin
= FALSE
; /* use first window for arglist */
4245 alist_T
*alist
; /* argument list to be used */
4248 int had_tab
= cmdmod
.tab
;
4249 win_T
*new_curwin
= NULL
;
4250 tabpage_T
*new_curtab
= NULL
;
4254 /* Don't give an error message. We don't want it when the ":all"
4255 * command is in the .vimrc. */
4260 opened_len
= ARGCOUNT
;
4261 opened
= alloc_clear((unsigned)opened_len
);
4266 need_mouse_correct
= TRUE
;
4270 * Try closing all windows that are not in the argument list.
4271 * Also close windows that are not full width;
4272 * When 'hidden' or "forceit" set the buffer becomes hidden.
4273 * Windows that have a changed buffer and can't be hidden won't be closed.
4274 * When the ":tab" modifier was used do this for all tab pages.
4277 goto_tabpage_tp(first_tabpage
);
4280 tpnext
= curtab
->tp_next
;
4281 for (wp
= firstwin
; wp
!= NULL
; wp
= wpnext
)
4283 wpnext
= wp
->w_next
;
4285 if (buf
->b_ffname
== NULL
4286 || buf
->b_nwindows
> 1
4287 #ifdef FEAT_VERTSPLIT
4288 || wp
->w_width
!= Columns
4294 /* check if the buffer in this window is in the arglist */
4295 for (i
= 0; i
< ARGCOUNT
; ++i
)
4297 if (ARGLIST
[i
].ae_fnum
== buf
->b_fnum
4298 || fullpathcmp(alist_name(&ARGLIST
[i
]),
4299 buf
->b_ffname
, TRUE
) & FPC_SAME
)
4307 new_curtab
= curtab
;
4310 if (wp
->w_alist
!= curwin
->w_alist
)
4312 /* Use the current argument list for all windows
4313 * containing a file from it. */
4314 alist_unlink(wp
->w_alist
);
4315 wp
->w_alist
= curwin
->w_alist
;
4316 ++wp
->w_alist
->al_refcount
;
4324 if (i
== ARGCOUNT
&& !keep_tabs
) /* close this window */
4326 if (P_HID(buf
) || forceit
|| buf
->b_nwindows
> 1
4327 || !bufIsChanged(buf
))
4329 /* If the buffer was changed, and we would like to hide it,
4330 * try autowriting. */
4331 if (!P_HID(buf
) && buf
->b_nwindows
<= 1
4332 && bufIsChanged(buf
))
4334 (void)autowrite(buf
, FALSE
);
4336 /* check if autocommands removed the window */
4337 if (!win_valid(wp
) || !buf_valid(buf
))
4339 wpnext
= firstwin
; /* start all over... */
4345 /* don't close last window */
4346 if (firstwin
== lastwin
&& first_tabpage
->tp_next
== NULL
)
4348 use_firstwin
= TRUE
;
4352 win_close(wp
, !P_HID(buf
) && !bufIsChanged(buf
));
4353 # ifdef FEAT_AUTOCMD
4354 /* check if autocommands removed the next window */
4355 if (!win_valid(wpnext
))
4356 wpnext
= firstwin
; /* start all over... */
4364 /* Without the ":tab" modifier only do the current tab page. */
4365 if (had_tab
== 0 || tpnext
== NULL
)
4368 # ifdef FEAT_AUTOCMD
4369 /* check if autocommands removed the next tab page */
4370 if (!valid_tabpage(tpnext
))
4371 tpnext
= first_tabpage
; /* start all over...*/
4373 goto_tabpage_tp(tpnext
);
4377 * Open a window for files in the argument list that don't have one.
4378 * ARGCOUNT may change while doing this, because of autocommands.
4380 if (count
> ARGCOUNT
|| count
<= 0)
4383 /* Autocommands may do anything to the argument list. Make sure it's not
4384 * freed while we are working here by "locking" it. We still have to
4385 * watch out for its size to be changed. */
4386 alist
= curwin
->w_alist
;
4387 ++alist
->al_refcount
;
4390 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4394 win_enter(lastwin
, FALSE
);
4396 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4397 * leaving an empty tab page when executed locally. */
4398 if (keep_tabs
&& bufempty() && curbuf
->b_nwindows
== 1
4399 && curbuf
->b_ffname
== NULL
&& !curbuf
->b_changed
)
4400 use_firstwin
= TRUE
;
4403 for (i
= 0; i
< count
&& i
< alist
->al_ga
.ga_len
&& !got_int
; ++i
)
4405 if (alist
== &global_alist
&& i
== global_alist
.al_ga
.ga_len
- 1)
4406 arg_had_last
= TRUE
;
4407 if (i
< opened_len
&& opened
[i
])
4409 /* Move the already present window to below the current window */
4410 if (curwin
->w_arg_idx
!= i
)
4412 for (wpnext
= firstwin
; wpnext
!= NULL
; wpnext
= wpnext
->w_next
)
4414 if (wpnext
->w_arg_idx
== i
)
4416 win_move_after(wpnext
, curwin
);
4422 else if (split_ret
== OK
)
4424 if (!use_firstwin
) /* split current window */
4427 p_ea
= TRUE
; /* use space from all windows */
4428 split_ret
= win_split(0, WSP_ROOM
| WSP_BELOW
);
4430 if (split_ret
== FAIL
)
4434 else /* first window: do autocmd for leaving this buffer */
4441 curwin
->w_arg_idx
= i
;
4444 new_curwin
= curwin
;
4445 new_curtab
= curtab
;
4447 (void)do_ecmd(0, alist_name(&AARGLIST(alist
)[i
]), NULL
, NULL
,
4449 ((P_HID(curwin
->w_buffer
)
4450 || bufIsChanged(curwin
->w_buffer
)) ? ECMD_HIDE
: 0)
4456 use_firstwin
= FALSE
;
4460 /* When ":tab" was used open a new tab for a new window repeatedly. */
4461 if (had_tab
> 0 && tabpage_index(NULL
) <= p_tpm
)
4465 /* Remove the "lock" on the argument list. */
4466 alist_unlink(alist
);
4471 /* to window with first arg */
4472 if (valid_tabpage(new_curtab
))
4473 goto_tabpage_tp(new_curtab
);
4474 if (win_valid(new_curwin
))
4475 win_enter(new_curwin
, FALSE
);
4483 # if defined(FEAT_LISTCMDS) || defined(PROTO)
4485 * Open a window for a number of buffers.
4497 int count
; /* Maximum number of windows to open. */
4498 int all
; /* When TRUE also load inactive buffers. */
4500 int had_tab
= cmdmod
.tab
;
4504 if (eap
->addr_count
== 0) /* make as many windows as possible */
4507 count
= eap
->line2
; /* make as many windows as specified */
4508 if (eap
->cmdidx
== CMD_unhide
|| eap
->cmdidx
== CMD_sunhide
)
4516 need_mouse_correct
= TRUE
;
4520 * Close superfluous windows (two windows for the same buffer).
4521 * Also close windows that are not full-width.
4525 goto_tabpage_tp(first_tabpage
);
4529 tpnext
= curtab
->tp_next
;
4530 for (wp
= firstwin
; wp
!= NULL
; wp
= wpnext
)
4532 wpnext
= wp
->w_next
;
4533 if ((wp
->w_buffer
->b_nwindows
> 1
4534 #ifdef FEAT_VERTSPLIT
4535 || ((cmdmod
.split
& WSP_VERT
)
4536 ? wp
->w_height
+ wp
->w_status_height
< Rows
- p_ch
4538 : wp
->w_width
!= Columns
)
4541 || (had_tab
> 0 && wp
!= firstwin
)
4543 ) && firstwin
!= lastwin
)
4545 win_close(wp
, FALSE
);
4547 wpnext
= firstwin
; /* just in case an autocommand does
4548 something strange with windows */
4549 tpnext
= first_tabpage
; /* start all over...*/
4558 /* Without the ":tab" modifier only do the current tab page. */
4559 if (had_tab
== 0 || tpnext
== NULL
)
4561 goto_tabpage_tp(tpnext
);
4566 * Go through the buffer list. When a buffer doesn't have a window yet,
4567 * open one. Otherwise move the window to the right position.
4568 * Watch out for autocommands that delete buffers or windows!
4571 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4574 win_enter(lastwin
, FALSE
);
4578 for (buf
= firstbuf
; buf
!= NULL
&& open_wins
< count
; buf
= buf
->b_next
)
4580 /* Check if this buffer needs a window */
4581 if ((!all
&& buf
->b_ml
.ml_mfp
== NULL
) || !buf
->b_p_bl
)
4587 /* With the ":tab" modifier don't move the window. */
4588 if (buf
->b_nwindows
> 0)
4589 wp
= lastwin
; /* buffer has a window, skip it */
4596 /* Check if this buffer already has a window */
4597 for (wp
= firstwin
; wp
!= NULL
; wp
= wp
->w_next
)
4598 if (wp
->w_buffer
== buf
)
4600 /* If the buffer already has a window, move it */
4602 win_move_after(wp
, curwin
);
4605 if (wp
== NULL
&& split_ret
== OK
)
4607 /* Split the window and put the buffer in it */
4609 p_ea
= TRUE
; /* use space from all windows */
4610 split_ret
= win_split(0, WSP_ROOM
| WSP_BELOW
);
4613 if (split_ret
== FAIL
)
4616 /* Open the buffer in this window. */
4617 #if defined(HAS_SWAP_EXISTS_ACTION)
4618 swap_exists_action
= SEA_DIALOG
;
4620 set_curbuf(buf
, DOBUF_GOTO
);
4622 if (!buf_valid(buf
)) /* autocommands deleted the buffer!!! */
4624 #if defined(HAS_SWAP_EXISTS_ACTION)
4625 swap_exists_action
= SEA_NONE
;
4630 #if defined(HAS_SWAP_EXISTS_ACTION)
4631 if (swap_exists_action
== SEA_QUIT
)
4633 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4636 /* Reset the error/interrupt/exception state here so that
4637 * aborting() returns FALSE when closing a window. */
4641 /* User selected Quit at ATTENTION prompt; close this window. */
4642 win_close(curwin
, TRUE
);
4644 swap_exists_action
= SEA_NONE
;
4645 swap_exists_did_quit
= TRUE
;
4647 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4648 /* Restore the error/interrupt/exception state if not
4649 * discarded by a new aborting error, interrupt, or uncaught
4655 handle_swap_exists(NULL
);
4662 (void)vgetc(); /* only break the file loading, not the rest */
4666 /* Autocommands deleted the buffer or aborted script processing!!! */
4671 /* When ":tab" was used open a new tab for a new window repeatedly. */
4672 if (had_tab
> 0 && tabpage_index(NULL
) <= p_tpm
)
4679 win_enter(firstwin
, FALSE
); /* back to first window */
4685 * Close superfluous windows.
4687 for (wp
= lastwin
; open_wins
> count
; )
4689 r
= (P_HID(wp
->w_buffer
) || !bufIsChanged(wp
->w_buffer
)
4690 || autowrite(wp
->w_buffer
, FALSE
) == OK
);
4694 /* BufWrite Autocommands made the window invalid, start over */
4701 win_close(wp
, !P_HID(wp
->w_buffer
));
4713 # endif /* FEAT_LISTCMDS */
4715 #endif /* FEAT_WINDOWS */
4717 static int chk_modeline
__ARGS((linenr_T
, int));
4720 * do_modelines() - process mode lines for the current file
4723 * OPT_WINONLY only set options local to window
4724 * OPT_NOWIN don't set options local to window
4726 * Returns immediately if the "ml" option isn't set.
4734 static int entered
= 0;
4736 if (!curbuf
->b_p_ml
|| (nmlines
= (int)p_mls
) == 0)
4739 /* Disallow recursive entry here. Can happen when executing a modeline
4740 * triggers an autocommand, which reloads modelines with a ":do". */
4745 for (lnum
= 1; lnum
<= curbuf
->b_ml
.ml_line_count
&& lnum
<= nmlines
;
4747 if (chk_modeline(lnum
, flags
) == FAIL
)
4750 for (lnum
= curbuf
->b_ml
.ml_line_count
; lnum
> 0 && lnum
> nmlines
4751 && lnum
> curbuf
->b_ml
.ml_line_count
- nmlines
; --lnum
)
4752 if (chk_modeline(lnum
, flags
) == FAIL
)
4757 #include "version.h" /* for version number */
4760 * chk_modeline() - check a single line for a mode string
4761 * Return FAIL if an error encountered.
4764 chk_modeline(lnum
, flags
)
4766 int flags
; /* Same as for do_modelines(). */
4770 char_u
*linecopy
; /* local copy of any modeline found */
4775 char_u
*save_sourcing_name
;
4776 linenr_T save_sourcing_lnum
;
4782 for (s
= ml_get(lnum
); *s
!= NUL
; ++s
)
4784 if (prev
== -1 || vim_isspace(prev
))
4786 if ((prev
!= -1 && STRNCMP(s
, "ex:", (size_t)3) == 0)
4787 || STRNCMP(s
, "vi:", (size_t)3) == 0)
4789 if (STRNCMP(s
, "vim", 3) == 0)
4791 if (s
[3] == '<' || s
[3] == '=' || s
[3] == '>')
4795 vers
= getdigits(&e
);
4798 || (VIM_VERSION_100
>= vers
&& isdigit(s
[3]))
4799 || (VIM_VERSION_100
< vers
&& s
[3] == '<')
4800 || (VIM_VERSION_100
> vers
&& s
[3] == '>')
4801 || (VIM_VERSION_100
== vers
&& s
[3] == '=')))
4810 do /* skip over "ex:", "vi:" or "vim:" */
4812 while (s
[-1] != ':');
4814 s
= linecopy
= vim_strsave(s
); /* copy the line, it will change */
4815 if (linecopy
== NULL
)
4818 save_sourcing_lnum
= sourcing_lnum
;
4819 save_sourcing_name
= sourcing_name
;
4820 sourcing_lnum
= lnum
; /* prepare for emsg() */
4821 sourcing_name
= (char_u
*)"modelines";
4824 while (end
== FALSE
)
4831 * Find end of set command: ':' or end of line.
4832 * Skip over "\:", replacing it with ":".
4834 for (e
= s
; *e
!= ':' && *e
!= NUL
; ++e
)
4835 if (e
[0] == '\\' && e
[1] == ':')
4841 * If there is a "set" command, require a terminating ':' and
4842 * ignore the stuff after the ':'.
4843 * "vi:set opt opt opt: foo" -- foo not interpreted
4844 * "vi:opt opt opt: foo" -- foo interpreted
4845 * Accept "se" for compatibility with Elvis.
4847 if (STRNCMP(s
, "set ", (size_t)4) == 0
4848 || STRNCMP(s
, "se ", (size_t)3) == 0)
4850 if (*e
!= ':') /* no terminating ':'? */
4853 s
= vim_strchr(s
, ' ') + 1;
4855 *e
= NUL
; /* truncate the set command */
4857 if (*s
!= NUL
) /* skip over an empty "::" */
4860 save_SID
= current_SID
;
4861 current_SID
= SID_MODELINE
;
4863 retval
= do_set(s
, OPT_MODELINE
| OPT_LOCAL
| flags
);
4865 current_SID
= save_SID
;
4867 if (retval
== FAIL
) /* stop if error found */
4870 s
= e
+ 1; /* advance to next part */
4873 sourcing_lnum
= save_sourcing_lnum
;
4874 sourcing_name
= save_sourcing_name
;
4883 read_viminfo_bufferlist(virp
, writing
)
4894 /* Handle long line and escaped characters. */
4895 xline
= viminfo_readstring(virp
, 1, FALSE
);
4897 /* don't read in if there are files on the command-line or if writing: */
4898 if (xline
!= NULL
&& !writing
&& ARGCOUNT
== 0
4899 && find_viminfo_parameter('%') != NULL
)
4901 /* Format is: <fname> Tab <lnum> Tab <col>.
4902 * Watch out for a Tab in the file name, work from the end. */
4905 tab
= vim_strrchr(xline
, '\t');
4909 col
= atoi((char *)tab
);
4910 tab
= vim_strrchr(xline
, '\t');
4914 lnum
= atol((char *)tab
);
4918 /* Expand "~/" in the file name at "line + 1" to a full path.
4919 * Then try shortening it by comparing with the current directory */
4920 expand_env(xline
, NameBuff
, MAXPATHL
);
4921 mch_dirname(IObuff
, IOSIZE
);
4922 sfname
= shorten_fname(NameBuff
, IObuff
);
4926 buf
= buflist_new(NameBuff
, sfname
, (linenr_T
)0, BLN_LISTED
);
4927 if (buf
!= NULL
) /* just in case... */
4929 buf
->b_last_cursor
.lnum
= lnum
;
4930 buf
->b_last_cursor
.col
= col
;
4931 buflist_setfpos(buf
, curwin
, lnum
, col
, FALSE
);
4936 return viminfo_readline(virp
);
4940 write_viminfo_bufferlist(fp
)
4951 if (find_viminfo_parameter('%') == NULL
)
4954 /* Without a number -1 is returned: do all buffers. */
4955 max_buffers
= get_viminfo_parameter('%');
4957 /* Allocate room for the file name, lnum and col. */
4958 line
= alloc(MAXPATHL
+ 40);
4963 FOR_ALL_TAB_WINDOWS(tp
, win
)
4964 set_last_cursor(win
);
4966 set_last_cursor(curwin
);
4969 fprintf(fp
, _("\n# Buffer list:\n"));
4970 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
4972 if (buf
->b_fname
== NULL
4974 #ifdef FEAT_QUICKFIX
4977 || removable(buf
->b_ffname
))
4980 if (max_buffers
-- == 0)
4983 home_replace(NULL
, buf
->b_ffname
, line
, MAXPATHL
, TRUE
);
4984 sprintf((char *)line
+ STRLEN(line
), "\t%ld\t%d",
4985 (long)buf
->b_last_cursor
.lnum
,
4986 buf
->b_last_cursor
.col
);
4987 viminfo_writestring(fp
, line
);
4995 * Return special buffer name.
4996 * Returns NULL when the buffer has a normal file name.
5002 #if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5003 if (bt_quickfix(buf
))
5008 * For location list window, w_llist_ref points to the location list.
5009 * For quickfix window, w_llist_ref is NULL.
5011 FOR_ALL_WINDOWS(win
)
5012 if (win
->w_buffer
== buf
)
5014 if (win
!= NULL
&& win
->w_llist_ref
!= NULL
)
5015 return _("[Location List]");
5017 return _("[Quickfix List]");
5020 #ifdef FEAT_QUICKFIX
5021 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5022 * contains the name as specified by the user */
5025 if (buf
->b_sfname
!= NULL
)
5026 return (char *)buf
->b_sfname
;
5030 if (buf
->b_fname
== NULL
)
5031 return _("[No Name]");
5036 #if defined(FEAT_SIGNS) || defined(PROTO)
5038 * Insert the sign into the signlist.
5041 insert_sign(buf
, prev
, next
, id
, lnum
, typenr
)
5042 buf_T
*buf
; /* buffer to store sign in */
5043 signlist_T
*prev
; /* previous sign entry */
5044 signlist_T
*next
; /* next sign entry */
5045 int id
; /* sign ID */
5046 linenr_T lnum
; /* line number which gets the mark */
5047 int typenr
; /* typenr of sign we are adding */
5049 signlist_T
*newsign
;
5051 newsign
= (signlist_T
*)lalloc((long_u
)sizeof(signlist_T
), FALSE
);
5052 if (newsign
!= NULL
)
5055 newsign
->lnum
= lnum
;
5056 newsign
->typenr
= typenr
;
5057 newsign
->next
= next
;
5058 #ifdef FEAT_NETBEANS_INTG
5059 newsign
->prev
= prev
;
5061 next
->prev
= newsign
;
5066 /* When adding first sign need to redraw the windows to create the
5067 * column for signs. */
5068 if (buf
->b_signlist
== NULL
)
5070 redraw_buf_later(buf
, NOT_VALID
);
5071 changed_cline_bef_curs();
5074 /* first sign in signlist */
5075 buf
->b_signlist
= newsign
;
5078 prev
->next
= newsign
;
5083 * Add the sign into the signlist. Find the right spot to do it though.
5086 buf_addsign(buf
, id
, lnum
, typenr
)
5087 buf_T
*buf
; /* buffer to store sign in */
5088 int id
; /* sign ID */
5089 linenr_T lnum
; /* line number which gets the mark */
5090 int typenr
; /* typenr of sign we are adding */
5092 signlist_T
*sign
; /* a sign in the signlist */
5093 signlist_T
*prev
; /* the previous sign */
5096 for (sign
= buf
->b_signlist
; sign
!= NULL
; sign
= sign
->next
)
5098 if (lnum
== sign
->lnum
&& id
== sign
->id
)
5100 sign
->typenr
= typenr
;
5104 #ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5109 #ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5110 /* XXX - GRP: Is this because of sign slide problem? Or is it
5111 * really needed? Or is it because we allow multiple signs per
5112 * line? If so, should I add that feature to FEAT_SIGNS?
5114 while (prev
!= NULL
&& prev
->lnum
== lnum
)
5117 sign
= buf
->b_signlist
;
5121 insert_sign(buf
, prev
, sign
, id
, lnum
, typenr
);
5126 #ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5127 /* XXX - GRP: See previous comment */
5128 while (prev
!= NULL
&& prev
->lnum
== lnum
)
5131 sign
= buf
->b_signlist
;
5135 insert_sign(buf
, prev
, sign
, id
, lnum
, typenr
);
5141 buf_change_sign_type(buf
, markId
, typenr
)
5142 buf_T
*buf
; /* buffer to store sign in */
5143 int markId
; /* sign ID */
5144 int typenr
; /* typenr of sign we are adding */
5146 signlist_T
*sign
; /* a sign in the signlist */
5148 for (sign
= buf
->b_signlist
; sign
!= NULL
; sign
= sign
->next
)
5150 if (sign
->id
== markId
)
5152 sign
->typenr
= typenr
;
5161 buf_getsigntype(buf
, lnum
, type
)
5164 int type
; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5166 signlist_T
*sign
; /* a sign in a b_signlist */
5168 for (sign
= buf
->b_signlist
; sign
!= NULL
; sign
= sign
->next
)
5169 if (sign
->lnum
== lnum
5170 && (type
== SIGN_ANY
5171 # ifdef FEAT_SIGN_ICONS
5172 || (type
== SIGN_ICON
5173 && sign_get_image(sign
->typenr
) != NULL
)
5175 || (type
== SIGN_TEXT
5176 && sign_get_text(sign
->typenr
) != NULL
)
5177 || (type
== SIGN_LINEHL
5178 && sign_get_attr(sign
->typenr
, TRUE
) != 0)))
5179 return sign
->typenr
;
5185 buf_delsign(buf
, id
)
5186 buf_T
*buf
; /* buffer sign is stored in */
5187 int id
; /* sign id */
5189 signlist_T
**lastp
; /* pointer to pointer to current sign */
5190 signlist_T
*sign
; /* a sign in a b_signlist */
5191 signlist_T
*next
; /* the next sign in a b_signlist */
5192 linenr_T lnum
; /* line number whose sign was deleted */
5194 lastp
= &buf
->b_signlist
;
5196 for (sign
= buf
->b_signlist
; sign
!= NULL
; sign
= next
)
5202 #ifdef FEAT_NETBEANS_INTG
5204 next
->prev
= sign
->prev
;
5211 lastp
= &sign
->next
;
5214 /* When deleted the last sign need to redraw the windows to remove the
5216 if (buf
->b_signlist
== NULL
)
5218 redraw_buf_later(buf
, NOT_VALID
);
5219 changed_cline_bef_curs();
5227 * Find the line number of the sign with the requested id. If the sign does
5228 * not exist, return 0 as the line number. This will still let the correct file
5232 buf_findsign(buf
, id
)
5233 buf_T
*buf
; /* buffer to store sign in */
5234 int id
; /* sign ID */
5236 signlist_T
*sign
; /* a sign in the signlist */
5238 for (sign
= buf
->b_signlist
; sign
!= NULL
; sign
= sign
->next
)
5246 buf_findsign_id(buf
, lnum
)
5247 buf_T
*buf
; /* buffer whose sign we are searching for */
5248 linenr_T lnum
; /* line number of sign */
5250 signlist_T
*sign
; /* a sign in the signlist */
5252 for (sign
= buf
->b_signlist
; sign
!= NULL
; sign
= sign
->next
)
5253 if (sign
->lnum
== lnum
)
5260 # if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5261 /* see if a given type of sign exists on a specific line */
5263 buf_findsigntype_id(buf
, lnum
, typenr
)
5264 buf_T
*buf
; /* buffer whose sign we are searching for */
5265 linenr_T lnum
; /* line number of sign */
5266 int typenr
; /* sign type number */
5268 signlist_T
*sign
; /* a sign in the signlist */
5270 for (sign
= buf
->b_signlist
; sign
!= NULL
; sign
= sign
->next
)
5271 if (sign
->lnum
== lnum
&& sign
->typenr
== typenr
)
5278 # if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5279 /* return the number of icons on the given line */
5281 buf_signcount(buf
, lnum
)
5285 signlist_T
*sign
; /* a sign in the signlist */
5288 for (sign
= buf
->b_signlist
; sign
!= NULL
; sign
= sign
->next
)
5289 if (sign
->lnum
== lnum
)
5290 if (sign_get_image(sign
->typenr
) != NULL
)
5295 # endif /* FEAT_SIGN_ICONS */
5296 # endif /* FEAT_NETBEANS_INTG */
5300 * Delete signs in buffer "buf".
5303 buf_delete_signs(buf
)
5308 while (buf
->b_signlist
!= NULL
)
5310 next
= buf
->b_signlist
->next
;
5311 vim_free(buf
->b_signlist
);
5312 buf
->b_signlist
= next
;
5317 * Delete all signs in all buffers.
5320 buf_delete_all_signs()
5322 buf_T
*buf
; /* buffer we are checking for signs */
5324 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
5325 if (buf
->b_signlist
!= NULL
)
5327 /* Need to redraw the windows to remove the sign column. */
5328 redraw_buf_later(buf
, NOT_VALID
);
5329 buf_delete_signs(buf
);
5334 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5337 sign_list_placed(rbuf
)
5344 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5352 if (buf
->b_signlist
!= NULL
)
5354 vim_snprintf(lbuf
, BUFSIZ
, _("Signs for %s:"), buf
->b_fname
);
5355 MSG_PUTS_ATTR(lbuf
, hl_attr(HLF_D
));
5358 for (p
= buf
->b_signlist
; p
!= NULL
; p
= p
->next
)
5360 vim_snprintf(lbuf
, BUFSIZ
, _(" line=%ld id=%d name=%s"),
5361 (long)p
->lnum
, p
->id
, sign_typenr2name(p
->typenr
));
5372 * Adjust a placed sign for inserted/deleted lines.
5375 sign_mark_adjust(line1
, line2
, amount
, amount_after
)
5381 signlist_T
*sign
; /* a sign in a b_signlist */
5383 for (sign
= curbuf
->b_signlist
; sign
!= NULL
; sign
= sign
->next
)
5385 if (sign
->lnum
>= line1
&& sign
->lnum
<= line2
)
5387 if (amount
== MAXLNUM
)
5390 sign
->lnum
+= amount
;
5392 else if (sign
->lnum
> line2
)
5393 sign
->lnum
+= amount_after
;
5396 #endif /* FEAT_SIGNS */
5399 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5405 if (on
!= curbuf
->b_p_bl
)
5407 curbuf
->b_p_bl
= on
;
5410 apply_autocmds(EVENT_BUFADD
, NULL
, NULL
, FALSE
, curbuf
);
5412 apply_autocmds(EVENT_BUFDELETE
, NULL
, NULL
, FALSE
, curbuf
);
5418 * Read the file for "buf" again and check if the contents changed.
5419 * Return TRUE if it changed or this could not be checked.
5422 buf_contents_changed(buf
)
5431 /* Allocate a buffer without putting it in the buffer list. */
5432 newbuf
= buflist_new(NULL
, NULL
, (linenr_T
)1, BLN_DUMMY
);
5436 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5437 if (prep_exarg(&ea
, buf
) == FAIL
)
5439 wipe_buffer(newbuf
, FALSE
);
5443 /* set curwin/curbuf to buf and save a few things */
5444 aucmd_prepbuf(&aco
, newbuf
);
5446 if (ml_open(curbuf
) == OK
5447 && readfile(buf
->b_ffname
, buf
->b_fname
,
5448 (linenr_T
)0, (linenr_T
)0, (linenr_T
)MAXLNUM
,
5449 &ea
, READ_NEW
| READ_DUMMY
) == OK
)
5451 /* compare the two files line by line */
5452 if (buf
->b_ml
.ml_line_count
== curbuf
->b_ml
.ml_line_count
)
5455 for (lnum
= 1; lnum
<= curbuf
->b_ml
.ml_line_count
; ++lnum
)
5456 if (STRCMP(ml_get_buf(buf
, lnum
, FALSE
), ml_get(lnum
)) != 0)
5465 /* restore curwin/curbuf and a few other things */
5466 aucmd_restbuf(&aco
);
5468 if (curbuf
!= newbuf
) /* safety check */
5469 wipe_buffer(newbuf
, FALSE
);
5475 * Wipe out a buffer and decrement the last buffer number if it was used for
5476 * this buffer. Call this to wipe out a temp buffer that does not contain any
5481 wipe_buffer(buf
, aucmd
)
5483 int aucmd
; /* When TRUE trigger autocommands. */
5485 if (buf
->b_fnum
== top_file_num
- 1)
5489 if (!aucmd
) /* Don't trigger BufDelete autocommands here. */
5492 close_buffer(NULL
, buf
, DOBUF_WIPE
);