1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
11 * buffer.c: functions for dealing with the buffer structure
15 * The buffer list is a double linked list of all buffers.
16 * Each buffer can be in one of these states:
17 * never loaded: BF_NEVERLOADED is set, only the file name is valid
18 * not loaded: b_ml.ml_mfp == NULL, no memfile allocated
19 * hidden: b_nwindows == 0, loaded but not displayed in a window
20 * normal: loaded and displayed in a window
22 * Instead of storing file names all over the place, each file name is
23 * stored in the buffer list. It can be referenced by a number.
25 * The current implementation remembers all file names ever used.
30 #if defined(FEAT_CMDL_COMPL) || defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL)
31 static char_u
*buflist_match
__ARGS((regprog_T
*prog
, buf_T
*buf
));
32 # define HAVE_BUFLIST_MATCH
33 static char_u
*fname_match
__ARGS((regprog_T
*prog
, char_u
*name
));
35 static void buflist_setfpos
__ARGS((buf_T
*buf
, win_T
*win
, linenr_T lnum
, colnr_T col
, int copy_options
));
36 static wininfo_T
*find_wininfo
__ARGS((buf_T
*buf
, int skip_diff_buffer
));
38 static buf_T
*buflist_findname_stat
__ARGS((char_u
*ffname
, struct stat
*st
));
39 static int otherfile_buf
__ARGS((buf_T
*buf
, char_u
*ffname
, struct stat
*stp
));
40 static int buf_same_ino
__ARGS((buf_T
*buf
, struct stat
*stp
));
42 static int otherfile_buf
__ARGS((buf_T
*buf
, char_u
*ffname
));
45 static int ti_change
__ARGS((char_u
*str
, char_u
**last
));
47 static int append_arg_number
__ARGS((win_T
*wp
, char_u
*buf
, int buflen
, int add_file
));
48 static void free_buffer
__ARGS((buf_T
*));
49 static void free_buffer_stuff
__ARGS((buf_T
*buf
, int free_options
));
50 static void clear_wininfo
__ARGS((buf_T
*buf
));
55 # define dev_T unsigned
58 #if defined(FEAT_SIGNS)
59 static void insert_sign
__ARGS((buf_T
*buf
, signlist_T
*prev
, signlist_T
*next
, int id
, linenr_T lnum
, int typenr
));
60 static void buf_delete_signs
__ARGS((buf_T
*buf
));
64 * Open current buffer, that is: open the memfile and read the file into memory
65 * return FAIL for failure, OK otherwise
68 open_buffer(read_stdin
, eap
)
69 int read_stdin
; /* read file from stdin */
70 exarg_T
*eap
; /* for forced 'ff' and 'fenc' or NULL */
78 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
79 * When re-entering the same buffer, it should not change, because the
80 * user may have reset the flag by hand.
82 if (readonlymode
&& curbuf
->b_ffname
!= NULL
83 && (curbuf
->b_flags
& BF_NEVERLOADED
))
84 curbuf
->b_p_ro
= TRUE
;
86 if (ml_open(curbuf
) == FAIL
)
89 * There MUST be a memfile, otherwise we can't do anything
90 * If we can't create one for the current buffer, take another buffer
92 close_buffer(NULL
, curbuf
, 0);
93 for (curbuf
= firstbuf
; curbuf
!= NULL
; curbuf
= curbuf
->b_next
)
94 if (curbuf
->b_ml
.ml_mfp
!= NULL
)
97 * if there is no memfile at all, exit
98 * This is OK, since there are no changes to lose.
102 EMSG(_("E82: Cannot allocate any buffer, exiting..."));
105 EMSG(_("E83: Cannot allocate buffer, using other one..."));
106 enter_buffer(curbuf
);
111 /* The autocommands in readfile() may change the buffer, but only AFTER
112 * reading the file. */
114 modified_was_set
= FALSE
;
117 /* mark cursor position as being invalid */
118 changed_line_abv_curs();
120 if (curbuf
->b_ffname
!= NULL
121 #ifdef FEAT_NETBEANS_INTG
126 #ifdef FEAT_NETBEANS_INTG
127 int oldFire
= netbeansFireChanges
;
129 netbeansFireChanges
= 0;
131 retval
= readfile(curbuf
->b_ffname
, curbuf
->b_fname
,
132 (linenr_T
)0, (linenr_T
)0, (linenr_T
)MAXLNUM
, eap
, READ_NEW
);
133 #ifdef FEAT_NETBEANS_INTG
134 netbeansFireChanges
= oldFire
;
136 /* Help buffer is filtered. */
142 int save_bin
= curbuf
->b_p_bin
;
146 * First read the text in binary mode into the buffer.
147 * Then read from that same buffer and append at the end. This makes
148 * it possible to retry when 'fileformat' or 'fileencoding' was
151 curbuf
->b_p_bin
= TRUE
;
152 retval
= readfile(NULL
, NULL
, (linenr_T
)0,
153 (linenr_T
)0, (linenr_T
)MAXLNUM
, NULL
, READ_NEW
+ READ_STDIN
);
154 curbuf
->b_p_bin
= save_bin
;
157 line_count
= curbuf
->b_ml
.ml_line_count
;
158 retval
= readfile(NULL
, NULL
, (linenr_T
)line_count
,
159 (linenr_T
)0, (linenr_T
)MAXLNUM
, eap
, READ_BUFFER
);
162 /* Delete the binary lines. */
163 while (--line_count
>= 0)
164 ml_delete((linenr_T
)1, FALSE
);
168 /* Delete the converted lines. */
169 while (curbuf
->b_ml
.ml_line_count
> line_count
)
170 ml_delete(line_count
, FALSE
);
172 /* Put the cursor on the first line. */
173 curwin
->w_cursor
.lnum
= 1;
174 curwin
->w_cursor
.col
= 0;
176 /* Set or reset 'modified' before executing autocommands, so that
177 * it can be changed there. */
178 if (!readonlymode
&& !bufempty())
180 else if (retval
!= FAIL
)
181 unchanged(curbuf
, FALSE
);
184 apply_autocmds_retval(EVENT_STDINREADPOST
, NULL
, NULL
, FALSE
,
187 apply_autocmds(EVENT_STDINREADPOST
, NULL
, NULL
, FALSE
, curbuf
);
193 /* if first time loading this buffer, init b_chartab[] */
194 if (curbuf
->b_flags
& BF_NEVERLOADED
)
195 (void)buf_init_chartab(curbuf
, FALSE
);
198 * Set/reset the Changed flag first, autocmds may change the buffer.
199 * Apply the automatic commands, before processing the modelines.
200 * So the modelines have priority over auto commands.
202 /* When reading stdin, the buffer contents always needs writing, so set
203 * the changed flag. Unless in readonly mode: "ls | gview -".
204 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
205 if ((got_int
&& vim_strchr(p_cpo
, CPO_INTMOD
) != NULL
)
207 || modified_was_set
/* ":set modified" used in autocmd */
209 || (aborting() && vim_strchr(p_cpo
, CPO_INTMOD
) != NULL
)
214 else if (retval
!= FAIL
&& !read_stdin
)
215 unchanged(curbuf
, FALSE
);
216 save_file_ff(curbuf
); /* keep this fileformat */
218 /* require "!" to overwrite the file, because it wasn't read completely */
224 curbuf
->b_flags
|= BF_READERR
;
227 /* Need to update automatic folding. Do this before the autocommands,
228 * they may use the fold info. */
229 foldUpdateAll(curwin
);
233 /* need to set w_topline, unless some autocommand already did that. */
234 if (!(curwin
->w_valid
& VALID_TOPLINE
))
236 curwin
->w_topline
= 1;
238 curwin
->w_topfill
= 0;
242 apply_autocmds_retval(EVENT_BUFENTER
, NULL
, NULL
, FALSE
, curbuf
, &retval
);
244 apply_autocmds(EVENT_BUFENTER
, NULL
, NULL
, FALSE
, curbuf
);
252 * The autocommands may have changed the current buffer. Apply the
253 * modelines to the correct buffer, if it still exists and is loaded.
255 if (buf_valid(old_curbuf
) && old_curbuf
->b_ml
.ml_mfp
!= NULL
)
259 /* Go to the buffer that was opened. */
260 aucmd_prepbuf(&aco
, old_curbuf
);
263 curbuf
->b_flags
&= ~(BF_CHECK_RO
| BF_NEVERLOADED
);
267 apply_autocmds_retval(EVENT_BUFWINENTER
, NULL
, NULL
, FALSE
, curbuf
,
270 apply_autocmds(EVENT_BUFWINENTER
, NULL
, NULL
, FALSE
, curbuf
);
273 /* restore curwin/curbuf and a few other things */
283 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
291 for (bp
= firstbuf
; bp
!= NULL
; bp
= bp
->b_next
)
298 * Close the link to a buffer.
299 * "action" is used when there is no longer a window for the buffer.
301 * 0 buffer becomes hidden
302 * DOBUF_UNLOAD buffer is unloaded
303 * DOBUF_DELETE buffer is unloaded and removed from buffer list
304 * DOBUF_WIPE buffer is unloaded and really deleted
305 * When doing all but the first one on the current buffer, the caller should
306 * get a new buffer very soon!
308 * The 'bufhidden' option can force freeing and deleting.
311 close_buffer(win
, buf
, action
)
312 win_T
*win
; /* if not NULL, set b_last_cursor */
318 int nwindows
= buf
->b_nwindows
;
320 int unload_buf
= (action
!= 0);
321 int del_buf
= (action
== DOBUF_DEL
|| action
== DOBUF_WIPE
);
322 int wipe_buf
= (action
== DOBUF_WIPE
);
326 * Force unloading or deleting when 'bufhidden' says so.
327 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
328 * "hide" (otherwise we could never free or delete a buffer).
330 if (buf
->b_p_bh
[0] == 'd') /* 'bufhidden' == "delete" */
335 else if (buf
->b_p_bh
[0] == 'w') /* 'bufhidden' == "wipe" */
341 else if (buf
->b_p_bh
[0] == 'u') /* 'bufhidden' == "unload" */
347 /* Set b_last_cursor when closing the last window for the buffer.
348 * Remember the last cursor position and window options of the buffer.
349 * This used to be only for the current window, but then options like
350 * 'foldmethod' may be lost with a ":only" command. */
351 if (buf
->b_nwindows
== 1)
352 set_last_cursor(win
);
353 buflist_setfpos(buf
, win
,
354 win
->w_cursor
.lnum
== 1 ? 0 : win
->w_cursor
.lnum
,
355 win
->w_cursor
.col
, TRUE
);
359 /* When the buffer is no longer in a window, trigger BufWinLeave */
360 if (buf
->b_nwindows
== 1)
362 apply_autocmds(EVENT_BUFWINLEAVE
, buf
->b_fname
, buf
->b_fname
,
364 if (!buf_valid(buf
)) /* autocommands may delete the buffer */
367 /* When the buffer becomes hidden, but is not unloaded, trigger
371 apply_autocmds(EVENT_BUFHIDDEN
, buf
->b_fname
, buf
->b_fname
,
373 if (!buf_valid(buf
)) /* autocmds may delete the buffer */
377 if (aborting()) /* autocmds may abort script processing */
381 nwindows
= buf
->b_nwindows
;
384 /* decrease the link count from windows (unless not in any window) */
385 if (buf
->b_nwindows
> 0)
388 /* Return when a window is displaying the buffer or when it's not
390 if (buf
->b_nwindows
> 0 || !unload_buf
)
392 #if 0 /* why was this here? */
394 u_sync(); /* sync undo before going to another buffer */
399 /* Always remove the buffer when there is no file name. */
400 if (buf
->b_ffname
== NULL
)
404 * Free all things allocated for this buffer.
405 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
408 /* Remember if we are closing the current buffer. Restore the number of
409 * windows, so that autocommands in buf_freeall() don't get confused. */
410 is_curbuf
= (buf
== curbuf
);
411 buf
->b_nwindows
= nwindows
;
414 buf_freeall(buf
, del_buf
, wipe_buf
);
417 /* Autocommands may have deleted the buffer. */
421 if (aborting()) /* autocmds may abort script processing */
425 /* Autocommands may have opened or closed windows for this buffer.
426 * Decrement the count for the close we do here. */
427 if (buf
->b_nwindows
> 0)
431 * It's possible that autocommands change curbuf to the one being deleted.
432 * This might cause the previous curbuf to be deleted unexpectedly. But
433 * in some cases it's OK to delete the curbuf, because a new one is
434 * obtained anyway. Therefore only return if curbuf changed to the
437 if (buf
== curbuf
&& !is_curbuf
)
441 /* Change directories when the 'acd' option is set. */
445 * Remove the buffer from the list.
449 #ifdef FEAT_SUN_WORKSHOP
450 if (usingSunWorkShop
)
451 workshop_file_closed_lineno((char *)buf
->b_ffname
,
452 (int)buf
->b_last_cursor
.lnum
);
454 vim_free(buf
->b_ffname
);
455 vim_free(buf
->b_sfname
);
456 if (buf
->b_prev
== NULL
)
457 firstbuf
= buf
->b_next
;
459 buf
->b_prev
->b_next
= buf
->b_next
;
460 if (buf
->b_next
== NULL
)
461 lastbuf
= buf
->b_prev
;
463 buf
->b_next
->b_prev
= buf
->b_prev
;
470 /* Free all internal variables and reset option values, to make
471 * ":bdel" compatible with Vim 5.7. */
472 free_buffer_stuff(buf
, TRUE
);
474 /* Make it look like a new buffer. */
475 buf
->b_flags
= BF_CHECK_RO
| BF_NEVERLOADED
;
477 /* Init the options when loaded again. */
478 buf
->b_p_initialized
= FALSE
;
487 * Make buffer not contain a file.
493 buf
->b_ml
.ml_line_count
= 1;
494 unchanged(buf
, TRUE
);
496 buf
->b_shortname
= FALSE
;
499 buf
->b_start_eol
= TRUE
;
501 buf
->b_p_bomb
= FALSE
;
502 buf
->b_start_bomb
= FALSE
;
504 buf
->b_ml
.ml_mfp
= NULL
;
505 buf
->b_ml
.ml_flags
= ML_EMPTY
; /* empty buffer */
506 #ifdef FEAT_NETBEANS_INTG
507 netbeans_deleted_all_lines(buf
);
512 * buf_freeall() - free all things allocated for a buffer that are related to
516 buf_freeall(buf
, del_buf
, wipe_buf
)
518 int del_buf UNUSED
; /* buffer is going to be deleted */
519 int wipe_buf UNUSED
; /* buffer is going to be wiped out */
522 int is_curbuf
= (buf
== curbuf
);
524 apply_autocmds(EVENT_BUFUNLOAD
, buf
->b_fname
, buf
->b_fname
, FALSE
, buf
);
525 if (!buf_valid(buf
)) /* autocommands may delete the buffer */
527 if (del_buf
&& buf
->b_p_bl
)
529 apply_autocmds(EVENT_BUFDELETE
, buf
->b_fname
, buf
->b_fname
, FALSE
, buf
);
530 if (!buf_valid(buf
)) /* autocommands may delete the buffer */
535 apply_autocmds(EVENT_BUFWIPEOUT
, buf
->b_fname
, buf
->b_fname
,
537 if (!buf_valid(buf
)) /* autocommands may delete the buffer */
541 if (aborting()) /* autocmds may abort script processing */
546 * It's possible that autocommands change curbuf to the one being deleted.
547 * This might cause curbuf to be deleted unexpectedly. But in some cases
548 * it's OK to delete the curbuf, because a new one is obtained anyway.
549 * Therefore only return if curbuf changed to the deleted buffer.
551 if (buf
== curbuf
&& !is_curbuf
)
555 diff_buf_delete(buf
); /* Can't use 'diff' for unloaded buffer. */
559 /* No folds in an empty buffer. */
565 FOR_ALL_TAB_WINDOWS(tp
, win
)
566 if (win
->w_buffer
== buf
)
570 if (curwin
->w_buffer
== buf
)
571 clearFolding(curwin
);
576 tcl_buffer_free(buf
);
578 u_blockfree(buf
); /* free the memory allocated for undo */
579 ml_close(buf
, TRUE
); /* close and delete the memline/memfile */
580 buf
->b_ml
.ml_line_count
= 0; /* no lines in buffer */
581 u_clearall(buf
); /* reset all undo information */
583 syntax_clear(buf
); /* reset syntax info */
585 buf
->b_flags
&= ~BF_READERR
; /* a read error is no longer relevant */
589 * Free a buffer structure and the things it contains related to the buffer
590 * itself (not the file, that must have been done already).
596 free_buffer_stuff(buf
, TRUE
);
598 mzscheme_buffer_free(buf
);
604 python_buffer_free(buf
);
607 ruby_buffer_free(buf
);
610 aubuflocal_remove(buf
);
616 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
619 free_buffer_stuff(buf
, free_options
)
621 int free_options
; /* free options as well */
625 clear_wininfo(buf
); /* including window-local options */
626 free_buf_options(buf
, TRUE
);
629 vars_clear(&buf
->b_vars
.dv_hashtab
); /* free all internal variables */
630 hash_init(&buf
->b_vars
.dv_hashtab
);
633 uc_clear(&buf
->b_ucmds
); /* clear local user commands */
636 buf_delete_signs(buf
); /* delete any signs */
638 #ifdef FEAT_NETBEANS_INTG
640 netbeans_file_killed(buf
);
643 map_clear_int(buf
, MAP_ALL_MODES
, TRUE
, FALSE
); /* clear local mappings */
644 map_clear_int(buf
, MAP_ALL_MODES
, TRUE
, TRUE
); /* clear local abbrevs */
647 vim_free(buf
->b_start_fenc
);
648 buf
->b_start_fenc
= NULL
;
651 ga_clear(&buf
->b_langp
);
656 * Free the b_wininfo list for buffer "buf".
664 while (buf
->b_wininfo
!= NULL
)
666 wip
= buf
->b_wininfo
;
667 buf
->b_wininfo
= wip
->wi_next
;
670 clear_winopt(&wip
->wi_opt
);
672 deleteFoldRecurse(&wip
->wi_folds
);
679 #if defined(FEAT_LISTCMDS) || defined(PROTO)
681 * Go to another buffer. Handles the result of the ATTENTION dialog.
684 goto_buffer(eap
, start
, dir
, count
)
690 # if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
691 buf_T
*old_curbuf
= curbuf
;
693 swap_exists_action
= SEA_DIALOG
;
695 (void)do_buffer(*eap
->cmd
== 's' ? DOBUF_SPLIT
: DOBUF_GOTO
,
696 start
, dir
, count
, eap
->forceit
);
697 # if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
698 if (swap_exists_action
== SEA_QUIT
&& *eap
->cmd
== 's')
700 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
703 /* Reset the error/interrupt/exception state here so that
704 * aborting() returns FALSE when closing a window. */
708 /* Quitting means closing the split window, nothing else. */
709 win_close(curwin
, TRUE
);
710 swap_exists_action
= SEA_NONE
;
711 swap_exists_did_quit
= TRUE
;
713 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
714 /* Restore the error/interrupt/exception state if not discarded by a
715 * new aborting error, interrupt, or uncaught exception. */
720 handle_swap_exists(old_curbuf
);
725 #if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
727 * Handle the situation of swap_exists_action being set.
728 * It is allowed for "old_curbuf" to be NULL or invalid.
731 handle_swap_exists(old_curbuf
)
734 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
738 if (swap_exists_action
== SEA_QUIT
)
740 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
741 /* Reset the error/interrupt/exception state here so that
742 * aborting() returns FALSE when closing a buffer. */
746 /* User selected Quit at ATTENTION prompt. Go back to previous
747 * buffer. If that buffer is gone or the same as the current one,
748 * open a new, empty buffer. */
749 swap_exists_action
= SEA_NONE
; /* don't want it again */
750 swap_exists_did_quit
= TRUE
;
751 close_buffer(curwin
, curbuf
, DOBUF_UNLOAD
);
752 if (!buf_valid(old_curbuf
) || old_curbuf
== curbuf
)
753 old_curbuf
= buflist_new(NULL
, NULL
, 1L, BLN_CURBUF
| BLN_LISTED
);
754 if (old_curbuf
!= NULL
)
755 enter_buffer(old_curbuf
);
756 /* If "old_curbuf" is NULL we are in big trouble here... */
758 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
759 /* Restore the error/interrupt/exception state if not discarded by a
760 * new aborting error, interrupt, or uncaught exception. */
764 else if (swap_exists_action
== SEA_RECOVER
)
766 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
767 /* Reset the error/interrupt/exception state here so that
768 * aborting() returns FALSE when closing a buffer. */
772 /* User selected Recover at ATTENTION prompt. */
775 MSG_PUTS("\n"); /* don't overwrite the last message */
776 cmdline_row
= msg_row
;
779 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
780 /* Restore the error/interrupt/exception state if not discarded by a
781 * new aborting error, interrupt, or uncaught exception. */
785 swap_exists_action
= SEA_NONE
;
789 #if defined(FEAT_LISTCMDS) || defined(PROTO)
791 * do_bufdel() - delete or unload buffer(s)
793 * addr_count == 0: ":bdel" - delete current buffer
794 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
795 * buffer "end_bnr", then any other arguments.
796 * addr_count == 2: ":N,N bdel" - delete buffers in range
798 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
799 * DOBUF_DEL (":bdel")
801 * Returns error message or NULL
804 do_bufdel(command
, arg
, addr_count
, start_bnr
, end_bnr
, forceit
)
806 char_u
*arg
; /* pointer to extra arguments */
808 int start_bnr
; /* first buffer number in a range */
809 int end_bnr
; /* buffer nr or last buffer nr in a range */
812 int do_current
= 0; /* delete current buffer? */
813 int deleted
= 0; /* number of buffers deleted */
814 char_u
*errormsg
= NULL
; /* return value */
815 int bnr
; /* buffer number */
820 (void)do_buffer(command
, DOBUF_CURRENT
, FORWARD
, 0, forceit
);
826 if (*arg
) /* both range and argument is not allowed */
827 return (char_u
*)_(e_trailing
);
830 else /* addr_count == 1 */
833 for ( ;!got_int
; ui_breakcheck())
836 * delete the current buffer last, otherwise when the
837 * current buffer is deleted, the next buffer becomes
838 * the current one and will be loaded, which may then
839 * also be deleted, etc.
841 if (bnr
== curbuf
->b_fnum
)
843 else if (do_buffer(command
, DOBUF_FIRST
, FORWARD
, (int)bnr
,
848 * find next buffer number to delete/unload
855 else /* addr_count == 1 */
857 arg
= skipwhite(arg
);
860 if (!VIM_ISDIGIT(*arg
))
862 p
= skiptowhite_esc(arg
);
863 bnr
= buflist_findpat(arg
, p
, command
== DOBUF_WIPE
, FALSE
);
864 if (bnr
< 0) /* failed */
869 bnr
= getdigits(&arg
);
872 if (!got_int
&& do_current
&& do_buffer(command
, DOBUF_FIRST
,
873 FORWARD
, do_current
, forceit
) == OK
)
878 if (command
== DOBUF_UNLOAD
)
879 STRCPY(IObuff
, _("E515: No buffers were unloaded"));
880 else if (command
== DOBUF_DEL
)
881 STRCPY(IObuff
, _("E516: No buffers were deleted"));
883 STRCPY(IObuff
, _("E517: No buffers were wiped out"));
886 else if (deleted
>= p_report
)
888 if (command
== DOBUF_UNLOAD
)
891 MSG(_("1 buffer unloaded"));
893 smsg((char_u
*)_("%d buffers unloaded"), deleted
);
895 else if (command
== DOBUF_DEL
)
898 MSG(_("1 buffer deleted"));
900 smsg((char_u
*)_("%d buffers deleted"), deleted
);
905 MSG(_("1 buffer wiped out"));
907 smsg((char_u
*)_("%d buffers wiped out"), deleted
);
917 * Implementation of the commands for the buffer list.
919 * action == DOBUF_GOTO go to specified buffer
920 * action == DOBUF_SPLIT split window and go to specified buffer
921 * action == DOBUF_UNLOAD unload specified buffer(s)
922 * action == DOBUF_DEL delete specified buffer(s) from buffer list
923 * action == DOBUF_WIPE delete specified buffer(s) really
925 * start == DOBUF_CURRENT go to "count" buffer from current buffer
926 * start == DOBUF_FIRST go to "count" buffer from first buffer
927 * start == DOBUF_LAST go to "count" buffer from last buffer
928 * start == DOBUF_MOD go to "count" modified buffer from current buffer
933 do_buffer(action
, start
, dir
, count
, forceit
)
936 int dir
; /* FORWARD or BACKWARD */
937 int count
; /* buffer number or number of buffers */
938 int forceit
; /* TRUE for :...! */
942 int unload
= (action
== DOBUF_UNLOAD
|| action
== DOBUF_DEL
943 || action
== DOBUF_WIPE
);
947 case DOBUF_FIRST
: buf
= firstbuf
; break;
948 case DOBUF_LAST
: buf
= lastbuf
; break;
949 default: buf
= curbuf
; break;
951 if (start
== DOBUF_MOD
) /* find next modified buffer */
961 while (buf
!= curbuf
&& !bufIsChanged(buf
));
963 if (!bufIsChanged(buf
))
965 EMSG(_("E84: No modified buffer found"));
969 else if (start
== DOBUF_FIRST
&& count
) /* find specified buffer number */
971 while (buf
!= NULL
&& buf
->b_fnum
!= count
)
977 while (count
> 0 || (!unload
&& !buf
->b_p_bl
&& bp
!= buf
))
979 /* remember the buffer where we start, we come back there when all
980 * buffers are unlisted. */
995 /* don't count unlisted buffers */
996 if (unload
|| buf
->b_p_bl
)
999 bp
= NULL
; /* use this buffer as new starting point */
1003 /* back where we started, didn't find anything. */
1004 EMSG(_("E85: There is no listed buffer"));
1010 if (buf
== NULL
) /* could not find it */
1012 if (start
== DOBUF_FIRST
)
1014 /* don't warn when deleting */
1016 EMSGN(_("E86: Buffer %ld does not exist"), count
);
1018 else if (dir
== FORWARD
)
1019 EMSG(_("E87: Cannot go beyond last buffer"));
1021 EMSG(_("E88: Cannot go before first buffer"));
1026 need_mouse_correct
= TRUE
;
1029 #ifdef FEAT_LISTCMDS
1031 * delete buffer buf from memory and/or the list
1038 /* When unloading or deleting a buffer that's already unloaded and
1039 * unlisted: fail silently. */
1040 if (action
!= DOBUF_WIPE
&& buf
->b_ml
.ml_mfp
== NULL
&& !buf
->b_p_bl
)
1043 if (!forceit
&& bufIsChanged(buf
))
1045 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1046 if ((p_confirm
|| cmdmod
.confirm
) && p_write
)
1048 dialog_changed(buf
, FALSE
);
1049 # ifdef FEAT_AUTOCMD
1050 if (!buf_valid(buf
))
1051 /* Autocommand deleted buffer, oops! It's not changed
1055 /* If it's still changed fail silently, the dialog already
1056 * mentioned why it fails. */
1057 if (bufIsChanged(buf
))
1063 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1070 * If deleting the last (listed) buffer, make it empty.
1071 * The last (listed) buffer cannot be unloaded.
1073 for (bp
= firstbuf
; bp
!= NULL
; bp
= bp
->b_next
)
1074 if (bp
->b_p_bl
&& bp
!= buf
)
1076 if (bp
== NULL
&& buf
== curbuf
)
1078 if (action
== DOBUF_UNLOAD
)
1080 EMSG(_("E90: Cannot unload last buffer"));
1084 /* Close any other windows on this buffer, then make it empty. */
1086 close_windows(buf
, TRUE
);
1089 retval
= do_ecmd(0, NULL
, NULL
, NULL
, ECMD_ONE
,
1090 forceit
? ECMD_FORCEIT
: 0, curwin
);
1093 * do_ecmd() may create a new buffer, then we have to delete
1094 * the old one. But do_ecmd() may have done that already, check
1095 * if the buffer still exists.
1097 if (buf
!= curbuf
&& buf_valid(buf
) && buf
->b_nwindows
== 0)
1098 close_buffer(NULL
, buf
, action
);
1104 * If the deleted buffer is the current one, close the current window
1105 * (unless it's the only window). Repeat this so long as we end up in
1106 * a window with this buffer.
1108 while (buf
== curbuf
1109 && (firstwin
!= lastwin
|| first_tabpage
->tp_next
!= NULL
))
1110 win_close(curwin
, FALSE
);
1114 * If the buffer to be deleted is not the current one, delete it here.
1119 close_windows(buf
, FALSE
);
1121 if (buf
!= curbuf
&& buf_valid(buf
) && buf
->b_nwindows
<= 0)
1122 close_buffer(NULL
, buf
, action
);
1127 * Deleting the current buffer: Need to find another buffer to go to.
1128 * There must be another, otherwise it would have been handled above.
1129 * First use au_new_curbuf, if it is valid.
1130 * Then prefer the buffer we most recently visited.
1131 * Else try to find one that is loaded, after the current buffer,
1132 * then before the current buffer.
1133 * Finally use any buffer.
1135 buf
= NULL
; /* selected buffer */
1136 bp
= NULL
; /* used when no loaded buffer found */
1138 if (au_new_curbuf
!= NULL
&& buf_valid(au_new_curbuf
))
1139 buf
= au_new_curbuf
;
1140 # ifdef FEAT_JUMPLIST
1144 #ifdef FEAT_JUMPLIST
1145 if (curwin
->w_jumplistlen
> 0)
1149 jumpidx
= curwin
->w_jumplistidx
- 1;
1151 jumpidx
= curwin
->w_jumplistlen
- 1;
1154 while (jumpidx
!= curwin
->w_jumplistidx
)
1156 buf
= buflist_findnr(curwin
->w_jumplist
[jumpidx
].fmark
.fnum
);
1159 if (buf
== curbuf
|| !buf
->b_p_bl
)
1160 buf
= NULL
; /* skip current and unlisted bufs */
1161 else if (buf
->b_ml
.ml_mfp
== NULL
)
1163 /* skip unloaded buf, but may keep it for later */
1169 if (buf
!= NULL
) /* found a valid buffer: stop searching */
1171 /* advance to older entry in jump list */
1172 if (!jumpidx
&& curwin
->w_jumplistidx
== curwin
->w_jumplistlen
)
1175 jumpidx
= curwin
->w_jumplistlen
- 1;
1176 if (jumpidx
== forward
) /* List exhausted for sure */
1182 if (buf
== NULL
) /* No previous buffer, Try 2'nd approach */
1185 buf
= curbuf
->b_next
;
1190 if (!forward
) /* tried both directions */
1192 buf
= curbuf
->b_prev
;
1196 /* in non-help buffer, try to skip help buffers, and vv */
1197 if (buf
->b_help
== curbuf
->b_help
&& buf
->b_p_bl
)
1199 if (buf
->b_ml
.ml_mfp
!= NULL
) /* found loaded buffer */
1201 if (bp
== NULL
) /* remember unloaded buf for later */
1210 if (buf
== NULL
) /* No loaded buffer, use unloaded one */
1212 if (buf
== NULL
) /* No loaded buffer, find listed one */
1214 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
1215 if (buf
->b_p_bl
&& buf
!= curbuf
)
1218 if (buf
== NULL
) /* Still no buffer, just take one */
1220 if (curbuf
->b_next
!= NULL
)
1221 buf
= curbuf
->b_next
;
1223 buf
= curbuf
->b_prev
;
1228 * make buf current buffer
1230 if (action
== DOBUF_SPLIT
) /* split window first */
1232 # ifdef FEAT_WINDOWS
1233 /* If 'switchbuf' contains "useopen": jump to first window containing
1234 * "buf" if one exists */
1235 if ((swb_flags
& SWB_USEOPEN
) && buf_jump_open_win(buf
))
1237 /* If 'switchbuf' contains "usetab": jump to first window in any tab
1238 * page containing "buf" if one exists */
1239 if ((swb_flags
& SWB_USETAB
) && buf_jump_open_tab(buf
))
1241 if (win_split(0, 0) == FAIL
)
1247 /* go to current buffer - nothing to do */
1252 * Check if the current buffer may be abandoned.
1254 if (action
== DOBUF_GOTO
&& !can_abandon(curbuf
, forceit
))
1256 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1257 if ((p_confirm
|| cmdmod
.confirm
) && p_write
)
1259 dialog_changed(curbuf
, FALSE
);
1260 # ifdef FEAT_AUTOCMD
1261 if (!buf_valid(buf
))
1262 /* Autocommand deleted buffer, oops! */
1266 if (bufIsChanged(curbuf
))
1269 EMSG(_(e_nowrtmsg
));
1274 /* Go to the other buffer. */
1275 set_curbuf(buf
, action
);
1277 #if defined(FEAT_LISTCMDS) && defined(FEAT_SCROLLBIND)
1278 if (action
== DOBUF_SPLIT
)
1279 curwin
->w_p_scb
= FALSE
; /* reset 'scrollbind' */
1282 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1283 if (aborting()) /* autocmds may abort script processing */
1290 #endif /* FEAT_LISTCMDS */
1293 * Set current buffer to "buf". Executes autocommands and closes current
1294 * buffer. "action" tells how to close the current buffer:
1295 * DOBUF_GOTO free or hide it
1296 * DOBUF_SPLIT nothing
1297 * DOBUF_UNLOAD unload it
1298 * DOBUF_DEL delete it
1299 * DOBUF_WIPE wipe it out
1302 set_curbuf(buf
, action
)
1307 int unload
= (action
== DOBUF_UNLOAD
|| action
== DOBUF_DEL
1308 || action
== DOBUF_WIPE
);
1311 if (!cmdmod
.keepalt
)
1312 curwin
->w_alt_fnum
= curbuf
->b_fnum
; /* remember alternate file */
1313 buflist_altfpos(curwin
); /* remember curpos */
1316 /* Don't restart Select mode after switching to another buffer. */
1317 VIsual_reselect
= FALSE
;
1320 /* close_windows() or apply_autocmds() may change curbuf */
1324 apply_autocmds(EVENT_BUFLEAVE
, NULL
, NULL
, FALSE
, curbuf
);
1326 if (buf_valid(prevbuf
) && !aborting())
1328 if (buf_valid(prevbuf
))
1334 close_windows(prevbuf
, FALSE
);
1336 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1337 if (buf_valid(prevbuf
) && !aborting())
1339 if (buf_valid(prevbuf
))
1342 if (prevbuf
== curbuf
)
1344 close_buffer(prevbuf
== curwin
->w_buffer
? curwin
: NULL
, prevbuf
,
1345 unload
? action
: (action
== DOBUF_GOTO
1347 && !bufIsChanged(prevbuf
)) ? DOBUF_UNLOAD
: 0);
1351 /* An autocommand may have deleted "buf", already entered it (e.g., when
1352 * it did ":bunload") or aborted the script processing! */
1354 if (buf_valid(buf
) && buf
!= curbuf
&& !aborting())
1356 if (buf_valid(buf
) && buf
!= curbuf
)
1363 * Enter a new current buffer.
1364 * Old curbuf must have been abandoned already!
1370 /* Copy buffer and window local option values. Not for a help buffer. */
1371 buf_copy_options(buf
, BCO_ENTER
| BCO_NOHELP
);
1376 /* Remove all folds in the window. */
1377 clearFolding(curwin
);
1378 foldUpdateAll(curwin
); /* update folds (later). */
1381 /* Get the buffer in the current window. */
1382 curwin
->w_buffer
= buf
;
1384 ++curbuf
->b_nwindows
;
1387 if (curwin
->w_p_diff
)
1388 diff_buf_add(curbuf
);
1391 /* Cursor on first line by default. */
1392 curwin
->w_cursor
.lnum
= 1;
1393 curwin
->w_cursor
.col
= 0;
1394 #ifdef FEAT_VIRTUALEDIT
1395 curwin
->w_cursor
.coladd
= 0;
1397 curwin
->w_set_curswant
= TRUE
;
1399 curwin
->w_topline_was_set
= FALSE
;
1402 /* Make sure the buffer is loaded. */
1403 if (curbuf
->b_ml
.ml_mfp
== NULL
) /* need to load the file */
1406 /* If there is no filetype, allow for detecting one. Esp. useful for
1407 * ":ball" used in a autocommand. If there already is a filetype we
1408 * might prefer to keep it. */
1409 if (*curbuf
->b_p_ft
== NUL
)
1410 did_filetype
= FALSE
;
1413 open_buffer(FALSE
, NULL
);
1418 need_fileinfo
= TRUE
; /* display file info after redraw */
1419 (void)buf_check_timestamp(curbuf
, FALSE
); /* check if file changed */
1421 curwin
->w_topline
= 1;
1423 curwin
->w_topfill
= 0;
1425 apply_autocmds(EVENT_BUFENTER
, NULL
, NULL
, FALSE
, curbuf
);
1426 apply_autocmds(EVENT_BUFWINENTER
, NULL
, NULL
, FALSE
, curbuf
);
1430 /* If autocommands did not change the cursor position, restore cursor lnum
1431 * and possibly cursor col. */
1432 if (curwin
->w_cursor
.lnum
== 1 && inindent(0))
1435 check_arg_idx(curwin
); /* check for valid arg_idx */
1440 /* when autocmds didn't change it */
1441 if (curwin
->w_topline
== 1 && !curwin
->w_topline_was_set
)
1443 scroll_cursor_halfway(FALSE
); /* redisplay at correct position */
1445 #ifdef FEAT_NETBEANS_INTG
1446 /* Send fileOpened event because we've changed buffers. */
1447 if (usingNetbeans
&& isNetbeansBuffer(curbuf
))
1448 netbeans_file_activated(curbuf
);
1451 /* Change directories when the 'acd' option is set. */
1455 if (curbuf
->b_kmap_state
& KEYMAP_INIT
)
1456 (void)keymap_init();
1459 /* May need to set the spell language. Can only do this after the buffer
1460 * has been properly setup. */
1461 if (!curbuf
->b_help
&& curwin
->w_p_spell
&& *curbuf
->b_p_spl
!= NUL
)
1462 (void)did_set_spelllang(curbuf
);
1465 redraw_later(NOT_VALID
);
1468 #if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1470 * Change to the directory of the current buffer.
1475 if (curbuf
->b_ffname
!= NULL
&& vim_chdirfile(curbuf
->b_ffname
) == OK
)
1476 shorten_fnames(TRUE
);
1481 * functions for dealing with the buffer list
1485 * Add a file name to the buffer list. Return a pointer to the buffer.
1486 * If the same file name already exists return a pointer to that buffer.
1487 * If it does not exist, or if fname == NULL, a new entry is created.
1488 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1489 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1490 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
1491 * This is the ONLY way to create a new buffer.
1493 static int top_file_num
= 1; /* highest file number */
1496 buflist_new(ffname
, sfname
, lnum
, flags
)
1497 char_u
*ffname
; /* full path of fname or relative */
1498 char_u
*sfname
; /* short fname or NULL */
1499 linenr_T lnum
; /* preferred cursor line */
1500 int flags
; /* BLN_ defines */
1507 fname_expand(curbuf
, &ffname
, &sfname
); /* will allocate ffname */
1510 * If file name already exists in the list, update the entry.
1513 /* On Unix we can use inode numbers when the file exists. Works better
1514 * for hard links. */
1515 if (sfname
== NULL
|| mch_stat((char *)sfname
, &st
) < 0)
1516 st
.st_dev
= (dev_T
)-1;
1518 if (ffname
!= NULL
&& !(flags
& BLN_DUMMY
) && (buf
=
1520 buflist_findname_stat(ffname
, &st
)
1522 buflist_findname(ffname
)
1528 buflist_setfpos(buf
, curwin
, lnum
, (colnr_T
)0, FALSE
);
1529 /* copy the options now, if 'cpo' doesn't have 's' and not done
1531 buf_copy_options(buf
, 0);
1532 if ((flags
& BLN_LISTED
) && !buf
->b_p_bl
)
1536 if (!(flags
& BLN_DUMMY
))
1537 apply_autocmds(EVENT_BUFADD
, NULL
, NULL
, FALSE
, buf
);
1544 * If the current buffer has no name and no contents, use the current
1545 * buffer. Otherwise: Need to allocate a new buffer structure.
1547 * This is the ONLY place where a new buffer structure is allocated!
1548 * (A spell file buffer is allocated in spell.c, but that's not a normal
1552 if ((flags
& BLN_CURBUF
)
1554 && curbuf
->b_ffname
== NULL
1555 && curbuf
->b_nwindows
<= 1
1556 && (curbuf
->b_ml
.ml_mfp
== NULL
|| bufempty()))
1560 /* It's like this buffer is deleted. Watch out for autocommands that
1561 * change curbuf! If that happens, allocate a new buffer anyway. */
1563 apply_autocmds(EVENT_BUFDELETE
, NULL
, NULL
, FALSE
, curbuf
);
1565 apply_autocmds(EVENT_BUFWIPEOUT
, NULL
, NULL
, FALSE
, curbuf
);
1567 if (aborting()) /* autocmds may abort script processing */
1571 #ifdef FEAT_QUICKFIX
1572 # ifdef FEAT_AUTOCMD
1576 /* Make sure 'bufhidden' and 'buftype' are empty */
1577 clear_string_option(&buf
->b_p_bh
);
1578 clear_string_option(&buf
->b_p_bt
);
1582 if (buf
!= curbuf
|| curbuf
== NULL
)
1584 buf
= (buf_T
*)alloc_clear((unsigned)sizeof(buf_T
));
1594 buf
->b_ffname
= ffname
;
1595 buf
->b_sfname
= vim_strsave(sfname
);
1599 buf
->b_wininfo
= (wininfo_T
*)alloc_clear((unsigned)sizeof(wininfo_T
));
1601 if ((ffname
!= NULL
&& (buf
->b_ffname
== NULL
|| buf
->b_sfname
== NULL
))
1602 || buf
->b_wininfo
== NULL
)
1604 vim_free(buf
->b_ffname
);
1605 buf
->b_ffname
= NULL
;
1606 vim_free(buf
->b_sfname
);
1607 buf
->b_sfname
= NULL
;
1615 /* free all things allocated for this buffer */
1616 buf_freeall(buf
, FALSE
, FALSE
);
1617 if (buf
!= curbuf
) /* autocommands deleted the buffer! */
1619 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1620 if (aborting()) /* autocmds may abort script processing */
1623 /* buf->b_nwindows = 0; why was this here? */
1624 free_buffer_stuff(buf
, FALSE
); /* delete local variables et al. */
1626 /* need to reload lmaps and set b:keymap_name */
1627 curbuf
->b_kmap_state
|= KEYMAP_INIT
;
1633 * put new buffer at the end of the buffer list
1636 if (firstbuf
== NULL
) /* buffer list is empty */
1641 else /* append new buffer at end of list */
1643 lastbuf
->b_next
= buf
;
1644 buf
->b_prev
= lastbuf
;
1648 buf
->b_fnum
= top_file_num
++;
1649 if (top_file_num
< 0) /* wrap around (may cause duplicates) */
1651 EMSG(_("W14: Warning: List of file names overflow"));
1652 if (emsg_silent
== 0)
1655 ui_delay(3000L, TRUE
); /* make sure it is noticed */
1661 * Always copy the options from the current buffer.
1663 buf_copy_options(buf
, BCO_ALWAYS
);
1666 buf
->b_wininfo
->wi_fpos
.lnum
= lnum
;
1667 buf
->b_wininfo
->wi_win
= curwin
;
1670 init_var_dict(&buf
->b_vars
, &buf
->b_bufvar
); /* init b: variables */
1673 hash_init(&buf
->b_keywtab
);
1674 hash_init(&buf
->b_keywtab_ic
);
1677 buf
->b_fname
= buf
->b_sfname
;
1679 if (st
.st_dev
== (dev_T
)-1)
1680 buf
->b_dev_valid
= FALSE
;
1683 buf
->b_dev_valid
= TRUE
;
1684 buf
->b_dev
= st
.st_dev
;
1685 buf
->b_ino
= st
.st_ino
;
1688 buf
->b_u_synced
= TRUE
;
1689 buf
->b_flags
= BF_CHECK_RO
| BF_NEVERLOADED
;
1690 if (flags
& BLN_DUMMY
)
1691 buf
->b_flags
|= BF_DUMMY
;
1692 buf_clear_file(buf
);
1693 clrallmarks(buf
); /* clear marks */
1694 fmarks_check_names(buf
); /* check file marks for this file */
1695 buf
->b_p_bl
= (flags
& BLN_LISTED
) ? TRUE
: FALSE
; /* init 'buflisted' */
1697 if (!(flags
& BLN_DUMMY
))
1699 apply_autocmds(EVENT_BUFNEW
, NULL
, NULL
, FALSE
, buf
);
1700 if (flags
& BLN_LISTED
)
1701 apply_autocmds(EVENT_BUFADD
, NULL
, NULL
, FALSE
, buf
);
1703 if (aborting()) /* autocmds may abort script processing */
1713 * Free the memory for the options of a buffer.
1714 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1718 free_buf_options(buf
, free_p_ff
)
1725 clear_string_option(&buf
->b_p_fenc
);
1727 clear_string_option(&buf
->b_p_ff
);
1728 #ifdef FEAT_QUICKFIX
1729 clear_string_option(&buf
->b_p_bh
);
1730 clear_string_option(&buf
->b_p_bt
);
1734 clear_string_option(&buf
->b_p_def
);
1735 clear_string_option(&buf
->b_p_inc
);
1737 clear_string_option(&buf
->b_p_inex
);
1740 #if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1741 clear_string_option(&buf
->b_p_inde
);
1742 clear_string_option(&buf
->b_p_indk
);
1744 #if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1745 clear_string_option(&buf
->b_p_bexpr
);
1747 #if defined(FEAT_EVAL)
1748 clear_string_option(&buf
->b_p_fex
);
1751 clear_string_option(&buf
->b_p_key
);
1753 clear_string_option(&buf
->b_p_kp
);
1754 clear_string_option(&buf
->b_p_mps
);
1755 clear_string_option(&buf
->b_p_fo
);
1756 clear_string_option(&buf
->b_p_flp
);
1757 clear_string_option(&buf
->b_p_isk
);
1759 clear_string_option(&buf
->b_p_keymap
);
1760 ga_clear(&buf
->b_kmap_ga
);
1762 #ifdef FEAT_COMMENTS
1763 clear_string_option(&buf
->b_p_com
);
1766 clear_string_option(&buf
->b_p_cms
);
1768 clear_string_option(&buf
->b_p_nf
);
1770 clear_string_option(&buf
->b_p_syn
);
1773 clear_string_option(&buf
->b_p_spc
);
1774 clear_string_option(&buf
->b_p_spf
);
1775 vim_free(buf
->b_cap_prog
);
1776 buf
->b_cap_prog
= NULL
;
1777 clear_string_option(&buf
->b_p_spl
);
1779 #ifdef FEAT_SEARCHPATH
1780 clear_string_option(&buf
->b_p_sua
);
1783 clear_string_option(&buf
->b_p_ft
);
1785 #ifdef FEAT_OSFILETYPE
1786 clear_string_option(&buf
->b_p_oft
);
1789 clear_string_option(&buf
->b_p_cink
);
1790 clear_string_option(&buf
->b_p_cino
);
1792 #if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1793 clear_string_option(&buf
->b_p_cinw
);
1795 #ifdef FEAT_INS_EXPAND
1796 clear_string_option(&buf
->b_p_cpt
);
1798 #ifdef FEAT_COMPL_FUNC
1799 clear_string_option(&buf
->b_p_cfu
);
1800 clear_string_option(&buf
->b_p_ofu
);
1802 #ifdef FEAT_QUICKFIX
1803 clear_string_option(&buf
->b_p_gp
);
1804 clear_string_option(&buf
->b_p_mp
);
1805 clear_string_option(&buf
->b_p_efm
);
1807 clear_string_option(&buf
->b_p_ep
);
1808 clear_string_option(&buf
->b_p_path
);
1809 clear_string_option(&buf
->b_p_tags
);
1810 #ifdef FEAT_INS_EXPAND
1811 clear_string_option(&buf
->b_p_dict
);
1812 clear_string_option(&buf
->b_p_tsr
);
1815 clear_string_option(&buf
->b_p_qe
);
1821 * get alternate file n
1822 * set linenr to lnum or altfpos.lnum if lnum == 0
1823 * also set cursor column to altfpos.col if 'startofline' is not set.
1824 * if (options & GETF_SETMARK) call setpcmark()
1825 * if (options & GETF_ALT) we are jumping to an alternate file.
1826 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1828 * return FAIL for failure, OK for success
1831 buflist_getfile(n
, lnum
, options
, forceit
)
1844 buf
= buflist_findnr(n
);
1847 if ((options
& GETF_ALT
) && n
== 0)
1850 EMSGN(_("E92: Buffer %ld not found"), n
);
1854 /* if alternate file is the current buffer, nothing to do */
1864 if (curbuf_locked())
1868 /* altfpos may be changed by getfile(), get it now */
1871 fpos
= buflist_findfpos(buf
);
1879 if (options
& GETF_SWITCH
)
1881 /* If 'switchbuf' contains "useopen": jump to first window containing
1882 * "buf" if one exists */
1883 if (swb_flags
& SWB_USEOPEN
)
1884 wp
= buf_jump_open_win(buf
);
1885 /* If 'switchbuf' contians "usetab": jump to first window in any tab
1886 * page containing "buf" if one exists */
1887 if (wp
== NULL
&& (swb_flags
& SWB_USETAB
))
1888 wp
= buf_jump_open_tab(buf
);
1889 /* If 'switchbuf' contains "split" or "newtab" and the current buffer
1890 * isn't empty: open new window */
1891 if (wp
== NULL
&& (swb_flags
& (SWB_SPLIT
| SWB_NEWTAB
)) && !bufempty())
1893 if (swb_flags
& SWB_NEWTAB
) /* Open in a new tab */
1895 else if (win_split(0, 0) == FAIL
) /* Open in a new window */
1897 # ifdef FEAT_SCROLLBIND
1898 curwin
->w_p_scb
= FALSE
;
1904 ++RedrawingDisabled
;
1905 if (getfile(buf
->b_fnum
, NULL
, NULL
, (options
& GETF_SETMARK
),
1906 lnum
, forceit
) <= 0)
1908 --RedrawingDisabled
;
1910 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
1911 if (!p_sol
&& col
!= 0)
1913 curwin
->w_cursor
.col
= col
;
1915 #ifdef FEAT_VIRTUALEDIT
1916 curwin
->w_cursor
.coladd
= 0;
1918 curwin
->w_set_curswant
= TRUE
;
1922 --RedrawingDisabled
;
1927 * go to the last know line number for the current buffer
1934 fpos
= buflist_findfpos(curbuf
);
1936 curwin
->w_cursor
.lnum
= fpos
->lnum
;
1937 check_cursor_lnum();
1940 curwin
->w_cursor
.col
= 0;
1943 curwin
->w_cursor
.col
= fpos
->col
;
1945 #ifdef FEAT_VIRTUALEDIT
1946 curwin
->w_cursor
.coladd
= 0;
1948 curwin
->w_set_curswant
= TRUE
;
1952 #if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
1954 * Find file in buffer list by name (it has to be for the current window).
1955 * Returns NULL if not found.
1958 buflist_findname_exp(fname
)
1964 /* First make the name into a full path name */
1965 ffname
= FullName_save(fname
,
1967 TRUE
/* force expansion, get rid of symbolic links */
1974 buf
= buflist_findname(ffname
);
1982 * Find file in buffer list by name (it has to be for the current window).
1983 * "ffname" must have a full path.
1984 * Skips dummy buffers.
1985 * Returns NULL if not found.
1988 buflist_findname(ffname
)
1994 if (mch_stat((char *)ffname
, &st
) < 0)
1995 st
.st_dev
= (dev_T
)-1;
1996 return buflist_findname_stat(ffname
, &st
);
2000 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2001 * twice for the same file.
2002 * Returns NULL if not found.
2005 buflist_findname_stat(ffname
, stp
)
2012 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
2013 if ((buf
->b_flags
& BF_DUMMY
) == 0 && !otherfile_buf(buf
, ffname
2022 #if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO)
2024 * Find file in buffer list by a regexp pattern.
2025 * Return fnum of the found buffer.
2026 * Return < 0 for error.
2029 buflist_findpat(pattern
, pattern_end
, unlisted
, diffmode
)
2031 char_u
*pattern_end
; /* pointer to first char after pattern */
2032 int unlisted
; /* find unlisted buffers */
2033 int diffmode UNUSED
; /* find diff-mode buffers only */
2045 if (pattern_end
== pattern
+ 1 && (*pattern
== '%' || *pattern
== '#'))
2047 if (*pattern
== '%')
2048 match
= curbuf
->b_fnum
;
2050 match
= curwin
->w_alt_fnum
;
2052 if (diffmode
&& !diff_mode_buf(buflist_findnr(match
)))
2058 * Try four ways of matching a listed buffer:
2059 * attempt == 0: without '^' or '$' (at any position)
2060 * attempt == 1: with '^' at start (only at position 0)
2061 * attempt == 2: with '$' at end (only match at end)
2062 * attempt == 3: with '^' at start and '$' at end (only full match)
2063 * Repeat this for finding an unlisted buffer if there was no matching
2068 pat
= file_pat_to_reg_pat(pattern
, pattern_end
, NULL
, FALSE
);
2071 patend
= pat
+ STRLEN(pat
) - 1;
2072 toggledollar
= (patend
> pat
&& *patend
== '$');
2074 /* First try finding a listed buffer. If not found and "unlisted"
2075 * is TRUE, try finding an unlisted buffer. */
2079 for (attempt
= 0; attempt
<= 3; ++attempt
)
2081 /* may add '^' and '$' */
2083 *patend
= (attempt
< 2) ? NUL
: '$'; /* add/remove '$' */
2085 if (*p
== '^' && !(attempt
& 1)) /* add/remove '^' */
2087 prog
= vim_regcomp(p
, p_magic
? RE_MAGIC
: 0);
2094 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
2095 if (buf
->b_p_bl
== find_listed
2097 && (!diffmode
|| diff_mode_buf(buf
))
2099 && buflist_match(prog
, buf
) != NULL
)
2101 if (match
>= 0) /* already found a match */
2106 match
= buf
->b_fnum
; /* remember first match */
2110 if (match
>= 0) /* found one match */
2114 /* Only search for unlisted buffers if there was no match with
2115 * a listed buffer. */
2116 if (!unlisted
|| !find_listed
|| match
!= -1)
2118 find_listed
= FALSE
;
2125 EMSG2(_("E93: More than one match for %s"), pattern
);
2127 EMSG2(_("E94: No matching buffer for %s"), pattern
);
2132 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2135 * Find all buffer names that match.
2136 * For command line expansion of ":buf" and ":sbuf".
2137 * Return OK if matches found, FAIL otherwise.
2140 ExpandBufnames(pat
, num_file
, file
, options
)
2154 *num_file
= 0; /* return values in case of FAIL */
2157 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2160 patc
= alloc((unsigned)STRLEN(pat
) + 11);
2163 STRCPY(patc
, "\\(^\\|[\\/]\\)");
2164 STRCPY(patc
+ 11, pat
+ 1);
2170 * attempt == 0: try match with '\<', match at start of word
2171 * attempt == 1: try match without '\<', match anywhere
2173 for (attempt
= 0; attempt
<= 1; ++attempt
)
2175 if (attempt
> 0 && patc
== pat
)
2176 break; /* there was no anchor, no need to try again */
2177 prog
= vim_regcomp(patc
+ attempt
* 11, RE_MAGIC
);
2186 * round == 1: Count the matches.
2187 * round == 2: Build the array to keep the matches.
2189 for (round
= 1; round
<= 2; ++round
)
2192 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
2194 if (!buf
->b_p_bl
) /* skip unlisted buffers */
2196 p
= buflist_match(prog
, buf
);
2203 if (options
& WILD_HOME_REPLACE
)
2204 p
= home_replace_save(buf
, p
);
2207 (*file
)[count
++] = p
;
2211 if (count
== 0) /* no match found, break here */
2215 *file
= (char_u
**)alloc((unsigned)(count
* sizeof(char_u
*)));
2226 if (count
) /* match(es) found, break here */
2234 return (count
== 0 ? FAIL
: OK
);
2237 #endif /* FEAT_CMDL_COMPL */
2239 #ifdef HAVE_BUFLIST_MATCH
2241 * Check for a match on the file name for buffer "buf" with regprog "prog".
2244 buflist_match(prog
, buf
)
2250 /* First try the short file name, then the long file name. */
2251 match
= fname_match(prog
, buf
->b_sfname
);
2253 match
= fname_match(prog
, buf
->b_ffname
);
2259 * Try matching the regexp in "prog" with file name "name".
2260 * Return "name" when there is a match, NULL when not.
2263 fname_match(prog
, name
)
2267 char_u
*match
= NULL
;
2269 regmatch_T regmatch
;
2273 regmatch
.regprog
= prog
;
2274 #ifdef CASE_INSENSITIVE_FILENAME
2275 regmatch
.rm_ic
= TRUE
; /* Always ignore case */
2277 regmatch
.rm_ic
= FALSE
; /* Never ignore case */
2280 if (vim_regexec(®match
, name
, (colnr_T
)0))
2284 /* Replace $(HOME) with '~' and try matching again. */
2285 p
= home_replace_save(NULL
, name
);
2286 if (p
!= NULL
&& vim_regexec(®match
, p
, (colnr_T
)0))
2297 * find file in buffer list by number
2306 nr
= curwin
->w_alt_fnum
;
2307 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
2308 if (buf
->b_fnum
== nr
)
2314 * Get name of file 'n' in the buffer list.
2315 * When the file has no name an empty string is returned.
2316 * home_replace() is used to shorten the file name (used for marks).
2317 * Returns a pointer to allocated memory, of NULL when failed.
2320 buflist_nr2name(n
, fullname
, helptail
)
2323 int helptail
; /* for help buffers return tail only */
2327 buf
= buflist_findnr(n
);
2330 return home_replace_save(helptail
? buf
: NULL
,
2331 fullname
? buf
->b_ffname
: buf
->b_fname
);
2335 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2336 * When "copy_options" is TRUE save the local window option values.
2337 * When "lnum" is 0 only do the options.
2340 buflist_setfpos(buf
, win
, lnum
, col
, copy_options
)
2349 for (wip
= buf
->b_wininfo
; wip
!= NULL
; wip
= wip
->wi_next
)
2350 if (wip
->wi_win
== win
)
2354 /* allocate a new entry */
2355 wip
= (wininfo_T
*)alloc_clear((unsigned)sizeof(wininfo_T
));
2359 if (lnum
== 0) /* set lnum even when it's 0 */
2364 /* remove the entry from the list */
2366 wip
->wi_prev
->wi_next
= wip
->wi_next
;
2368 buf
->b_wininfo
= wip
->wi_next
;
2370 wip
->wi_next
->wi_prev
= wip
->wi_prev
;
2371 if (copy_options
&& wip
->wi_optset
)
2373 clear_winopt(&wip
->wi_opt
);
2375 deleteFoldRecurse(&wip
->wi_folds
);
2381 wip
->wi_fpos
.lnum
= lnum
;
2382 wip
->wi_fpos
.col
= col
;
2386 /* Save the window-specific option values. */
2387 copy_winopt(&win
->w_onebuf_opt
, &wip
->wi_opt
);
2389 wip
->wi_fold_manual
= win
->w_fold_manual
;
2390 cloneFoldGrowArray(&win
->w_folds
, &wip
->wi_folds
);
2392 wip
->wi_optset
= TRUE
;
2395 /* insert the entry in front of the list */
2396 wip
->wi_next
= buf
->b_wininfo
;
2397 buf
->b_wininfo
= wip
;
2398 wip
->wi_prev
= NULL
;
2400 wip
->wi_next
->wi_prev
= wip
;
2406 static int wininfo_other_tab_diff
__ARGS((wininfo_T
*wip
));
2409 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2410 * page. That's because a diff is local to a tab page.
2413 wininfo_other_tab_diff(wip
)
2418 if (wip
->wi_opt
.wo_diff
)
2420 for (wp
= firstwin
; wp
!= NULL
; wp
= wp
->w_next
)
2421 /* return FALSE when it's a window in the current tab page, thus
2422 * the buffer was in diff mode here */
2423 if (wip
->wi_win
== wp
)
2432 * Find info for the current window in buffer "buf".
2433 * If not found, return the info for the most recently used window.
2434 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2436 * Returns NULL when there isn't any info.
2439 find_wininfo(buf
, skip_diff_buffer
)
2441 int skip_diff_buffer UNUSED
;
2445 for (wip
= buf
->b_wininfo
; wip
!= NULL
; wip
= wip
->wi_next
)
2446 if (wip
->wi_win
== curwin
2448 && (!skip_diff_buffer
|| !wininfo_other_tab_diff(wip
))
2453 /* If no wininfo for curwin, use the first in the list (that doesn't have
2454 * 'diff' set and is in another tab page). */
2458 if (skip_diff_buffer
)
2460 for (wip
= buf
->b_wininfo
; wip
!= NULL
; wip
= wip
->wi_next
)
2461 if (!wininfo_other_tab_diff(wip
))
2466 wip
= buf
->b_wininfo
;
2472 * Reset the local window options to the values last used in this window.
2473 * If the buffer wasn't used in this window before, use the values from
2474 * the most recently used window. If the values were never set, use the
2475 * global values for the window.
2483 clear_winopt(&curwin
->w_onebuf_opt
);
2485 clearFolding(curwin
);
2488 wip
= find_wininfo(buf
, TRUE
);
2489 if (wip
!= NULL
&& wip
->wi_optset
)
2491 copy_winopt(&wip
->wi_opt
, &curwin
->w_onebuf_opt
);
2493 curwin
->w_fold_manual
= wip
->wi_fold_manual
;
2494 curwin
->w_foldinvalid
= TRUE
;
2495 cloneFoldGrowArray(&wip
->wi_folds
, &curwin
->w_folds
);
2499 copy_winopt(&curwin
->w_allbuf_opt
, &curwin
->w_onebuf_opt
);
2502 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2504 curwin
->w_p_fdl
= p_fdls
;
2509 * Find the position (lnum and col) for the buffer 'buf' for the current
2511 * Returns a pointer to no_position if no position is found.
2514 buflist_findfpos(buf
)
2518 static pos_T no_position
= INIT_POS_T(1, 0, 0);
2520 wip
= find_wininfo(buf
, FALSE
);
2522 return &(wip
->wi_fpos
);
2524 return &no_position
;
2528 * Find the lnum for the buffer 'buf' for the current window.
2531 buflist_findlnum(buf
)
2534 return buflist_findfpos(buf
)->lnum
;
2537 #if defined(FEAT_LISTCMDS) || defined(PROTO)
2539 * List all know file names (for :files and :buffers command).
2549 for (buf
= firstbuf
; buf
!= NULL
&& !got_int
; buf
= buf
->b_next
)
2551 /* skip unlisted buffers, unless ! was used */
2552 if (!buf
->b_p_bl
&& !eap
->forceit
)
2555 if (buf_spname(buf
) != NULL
)
2556 STRCPY(NameBuff
, buf_spname(buf
));
2558 home_replace(buf
, buf
->b_fname
, NameBuff
, MAXPATHL
, TRUE
);
2560 len
= vim_snprintf((char *)IObuff
, IOSIZE
- 20, "%3d%c%c%c%c%c \"%s\"",
2562 buf
->b_p_bl
? ' ' : 'u',
2563 buf
== curbuf
? '%' :
2564 (curwin
->w_alt_fnum
== buf
->b_fnum
? '#' : ' '),
2565 buf
->b_ml
.ml_mfp
== NULL
? ' ' :
2566 (buf
->b_nwindows
== 0 ? 'h' : 'a'),
2567 !buf
->b_p_ma
? '-' : (buf
->b_p_ro
? '=' : ' '),
2568 (buf
->b_flags
& BF_READERR
) ? 'x'
2569 : (bufIsChanged(buf
) ? '+' : ' '),
2572 /* put "line 999" in column 40 or after the file name */
2573 i
= 40 - vim_strsize(IObuff
);
2576 IObuff
[len
++] = ' ';
2577 } while (--i
> 0 && len
< IOSIZE
- 18);
2578 vim_snprintf((char *)IObuff
+ len
, (size_t)(IOSIZE
- len
),
2579 _("line %ld"), buf
== curbuf
? curwin
->w_cursor
.lnum
2580 : (long)buflist_findlnum(buf
));
2581 msg_outtrans(IObuff
);
2582 out_flush(); /* output one line at a time */
2589 * Get file name and line number for file 'fnum'.
2590 * Used by DoOneCmd() for translating '%' and '#'.
2591 * Used by insert_reg() and cmdline_paste() for '#' register.
2592 * Return FAIL if not found, OK for success.
2595 buflist_name_nr(fnum
, fname
, lnum
)
2602 buf
= buflist_findnr(fnum
);
2603 if (buf
== NULL
|| buf
->b_fname
== NULL
)
2606 *fname
= buf
->b_fname
;
2607 *lnum
= buflist_findlnum(buf
);
2613 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2614 * The file name with the full path is also remembered, for when :cd is used.
2615 * Returns FAIL for failure (file name already in use by other buffer)
2619 setfname(buf
, ffname
, sfname
, message
)
2621 char_u
*ffname
, *sfname
;
2622 int message
; /* give message when buffer already exists */
2629 if (ffname
== NULL
|| *ffname
== NUL
)
2631 /* Removing the name. */
2632 vim_free(buf
->b_ffname
);
2633 vim_free(buf
->b_sfname
);
2634 buf
->b_ffname
= NULL
;
2635 buf
->b_sfname
= NULL
;
2637 st
.st_dev
= (dev_T
)-1;
2642 fname_expand(buf
, &ffname
, &sfname
); /* will allocate ffname */
2643 if (ffname
== NULL
) /* out of memory */
2647 * if the file name is already used in another buffer:
2648 * - if the buffer is loaded, fail
2649 * - if the buffer is not loaded, delete it from the list
2652 if (mch_stat((char *)ffname
, &st
) < 0)
2653 st
.st_dev
= (dev_T
)-1;
2655 if (!(buf
->b_flags
& BF_DUMMY
))
2657 obuf
= buflist_findname_stat(ffname
, &st
);
2659 obuf
= buflist_findname(ffname
);
2661 if (obuf
!= NULL
&& obuf
!= buf
)
2663 if (obuf
->b_ml
.ml_mfp
!= NULL
) /* it's loaded, fail */
2666 EMSG(_("E95: Buffer with this name already exists"));
2670 close_buffer(NULL
, obuf
, DOBUF_WIPE
); /* delete from the list */
2672 sfname
= vim_strsave(sfname
);
2673 if (ffname
== NULL
|| sfname
== NULL
)
2679 #ifdef USE_FNAME_CASE
2680 # ifdef USE_LONG_FNAME
2683 fname_case(sfname
, 0); /* set correct case for short file name */
2685 vim_free(buf
->b_ffname
);
2686 vim_free(buf
->b_sfname
);
2687 buf
->b_ffname
= ffname
;
2688 buf
->b_sfname
= sfname
;
2690 buf
->b_fname
= buf
->b_sfname
;
2692 if (st
.st_dev
== (dev_T
)-1)
2693 buf
->b_dev_valid
= FALSE
;
2696 buf
->b_dev_valid
= TRUE
;
2697 buf
->b_dev
= st
.st_dev
;
2698 buf
->b_ino
= st
.st_ino
;
2703 buf
->b_shortname
= FALSE
;
2706 buf_name_changed(buf
);
2711 * Crude way of changing the name of a buffer. Use with care!
2712 * The name should be relative to the current directory.
2715 buf_set_name(fnum
, name
)
2721 buf
= buflist_findnr(fnum
);
2724 vim_free(buf
->b_sfname
);
2725 vim_free(buf
->b_ffname
);
2726 buf
->b_ffname
= vim_strsave(name
);
2727 buf
->b_sfname
= NULL
;
2728 /* Allocate ffname and expand into full path. Also resolves .lnk
2729 * files on Win32. */
2730 fname_expand(buf
, &buf
->b_ffname
, &buf
->b_sfname
);
2731 buf
->b_fname
= buf
->b_sfname
;
2736 * Take care of what needs to be done when the name of buffer "buf" has
2740 buf_name_changed(buf
)
2744 * If the file name changed, also change the name of the swapfile
2746 if (buf
->b_ml
.ml_mfp
!= NULL
)
2749 if (curwin
->w_buffer
== buf
)
2750 check_arg_idx(curwin
); /* check file name for arg list */
2752 maketitle(); /* set window title */
2755 status_redraw_all(); /* status lines need to be redrawn */
2757 fmarks_check_names(buf
); /* check named file marks */
2758 ml_timestamp(buf
); /* reset timestamp */
2762 * set alternate file name for current window
2764 * Used by do_one_cmd(), do_write() and do_ecmd().
2765 * Return the buffer.
2768 setaltfname(ffname
, sfname
, lnum
)
2775 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2776 buf
= buflist_new(ffname
, sfname
, lnum
, 0);
2777 if (buf
!= NULL
&& !cmdmod
.keepalt
)
2778 curwin
->w_alt_fnum
= buf
->b_fnum
;
2783 * Get alternate file name for current window.
2784 * Return NULL if there isn't any, and give error message if requested.
2788 int errmsg
; /* give error message */
2793 if (buflist_name_nr(0, &fname
, &dummy
) == FAIL
)
2803 * Add a file name to the buflist and return its number.
2804 * Uses same flags as buflist_new(), except BLN_DUMMY.
2806 * used by qf_init(), main() and doarglist()
2809 buflist_add(fname
, flags
)
2815 buf
= buflist_new(fname
, NULL
, (linenr_T
)0, flags
);
2821 #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2823 * Adjust slashes in file names. Called after 'shellslash' was set.
2826 buflist_slash_adjust()
2830 for (bp
= firstbuf
; bp
!= NULL
; bp
= bp
->b_next
)
2832 if (bp
->b_ffname
!= NULL
)
2833 slash_adjust(bp
->b_ffname
);
2834 if (bp
->b_sfname
!= NULL
)
2835 slash_adjust(bp
->b_sfname
);
2841 * Set alternate cursor position for the current buffer and window "win".
2842 * Also save the local window option values.
2845 buflist_altfpos(win
)
2848 buflist_setfpos(curbuf
, win
, win
->w_cursor
.lnum
, win
->w_cursor
.col
, TRUE
);
2852 * Return TRUE if 'ffname' is not the same file as current file.
2853 * Fname must have a full path (expanded by mch_FullName()).
2859 return otherfile_buf(curbuf
, ffname
2867 otherfile_buf(buf
, ffname
2878 /* no name is different */
2879 if (ffname
== NULL
|| *ffname
== NUL
|| buf
->b_ffname
== NULL
)
2881 if (fnamecmp(ffname
, buf
->b_ffname
) == 0)
2887 /* If no struct stat given, get it now */
2890 if (!buf
->b_dev_valid
|| mch_stat((char *)ffname
, &st
) < 0)
2891 st
.st_dev
= (dev_T
)-1;
2894 /* Use dev/ino to check if the files are the same, even when the names
2895 * are different (possible with links). Still need to compare the
2896 * name above, for when the file doesn't exist yet.
2897 * Problem: The dev/ino changes when a file is deleted (and created
2898 * again) and remains the same when renamed/moved. We don't want to
2899 * mch_stat() each buffer each time, that would be too slow. Get the
2900 * dev/ino again when they appear to match, but not when they appear
2901 * to be different: Could skip a buffer when it's actually the same
2903 if (buf_same_ino(buf
, stp
))
2906 if (buf_same_ino(buf
, stp
))
2914 #if defined(UNIX) || defined(PROTO)
2916 * Set inode and device number for a buffer.
2917 * Must always be called when b_fname is changed!.
2925 if (buf
->b_fname
!= NULL
&& mch_stat((char *)buf
->b_fname
, &st
) >= 0)
2927 buf
->b_dev_valid
= TRUE
;
2928 buf
->b_dev
= st
.st_dev
;
2929 buf
->b_ino
= st
.st_ino
;
2932 buf
->b_dev_valid
= FALSE
;
2936 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
2939 buf_same_ino(buf
, stp
)
2943 return (buf
->b_dev_valid
2944 && stp
->st_dev
== buf
->b_dev
2945 && stp
->st_ino
== buf
->b_ino
);
2950 * Print info about the current buffer.
2953 fileinfo(fullname
, shorthelp
, dont_truncate
)
2954 int fullname
; /* when non-zero print full path */
2964 buffer
= alloc(IOSIZE
);
2968 if (fullname
> 1) /* 2 CTRL-G: include buffer number */
2970 vim_snprintf((char *)buffer
, IOSIZE
, "buf %d: ", curbuf
->b_fnum
);
2971 p
= buffer
+ STRLEN(buffer
);
2977 if (buf_spname(curbuf
) != NULL
)
2978 STRCPY(p
, buf_spname(curbuf
));
2981 if (!fullname
&& curbuf
->b_fname
!= NULL
)
2982 name
= curbuf
->b_fname
;
2984 name
= curbuf
->b_ffname
;
2985 home_replace(shorthelp
? curbuf
: NULL
, name
, p
,
2986 (int)(IOSIZE
- (p
- buffer
)), TRUE
);
2989 len
= STRLEN(buffer
);
2990 vim_snprintf((char *)buffer
+ len
, IOSIZE
- len
,
2992 curbufIsChanged() ? (shortmess(SHM_MOD
)
2993 ? " [+]" : _(" [Modified]")) : " ",
2994 (curbuf
->b_flags
& BF_NOTEDITED
)
2995 #ifdef FEAT_QUICKFIX
2996 && !bt_dontwrite(curbuf
)
2998 ? _("[Not edited]") : "",
2999 (curbuf
->b_flags
& BF_NEW
)
3000 #ifdef FEAT_QUICKFIX
3001 && !bt_dontwrite(curbuf
)
3003 ? _("[New file]") : "",
3004 (curbuf
->b_flags
& BF_READERR
) ? _("[Read errors]") : "",
3005 curbuf
->b_p_ro
? (shortmess(SHM_RO
) ? "[RO]"
3006 : _("[readonly]")) : "",
3007 (curbufIsChanged() || (curbuf
->b_flags
& BF_WRITE_MASK
)
3008 || curbuf
->b_p_ro
) ?
3010 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3011 * causes an overflow, thus for large numbers divide instead. */
3012 if (curwin
->w_cursor
.lnum
> 1000000L)
3013 n
= (int)(((long)curwin
->w_cursor
.lnum
) /
3014 ((long)curbuf
->b_ml
.ml_line_count
/ 100L));
3016 n
= (int)(((long)curwin
->w_cursor
.lnum
* 100L) /
3017 (long)curbuf
->b_ml
.ml_line_count
);
3018 len
= STRLEN(buffer
);
3019 if (curbuf
->b_ml
.ml_flags
& ML_EMPTY
)
3021 vim_snprintf((char *)buffer
+ len
, IOSIZE
- len
, "%s", _(no_lines_msg
));
3023 #ifdef FEAT_CMDL_INFO
3026 /* Current line and column are already on the screen -- webb */
3027 if (curbuf
->b_ml
.ml_line_count
== 1)
3028 vim_snprintf((char *)buffer
+ len
, IOSIZE
- len
,
3029 _("1 line --%d%%--"), n
);
3031 vim_snprintf((char *)buffer
+ len
, IOSIZE
- len
,
3032 _("%ld lines --%d%%--"),
3033 (long)curbuf
->b_ml
.ml_line_count
, n
);
3038 vim_snprintf((char *)buffer
+ len
, IOSIZE
- len
,
3039 _("line %ld of %ld --%d%%-- col "),
3040 (long)curwin
->w_cursor
.lnum
,
3041 (long)curbuf
->b_ml
.ml_line_count
,
3044 len
= STRLEN(buffer
);
3045 col_print(buffer
+ len
, IOSIZE
- len
,
3046 (int)curwin
->w_cursor
.col
+ 1, (int)curwin
->w_virtcol
+ 1);
3049 (void)append_arg_number(curwin
, buffer
, IOSIZE
, !shortmess(SHM_FILE
));
3053 /* Temporarily set msg_scroll to avoid the message being truncated.
3054 * First call msg_start() to get the message in the right place. */
3063 p
= msg_trunc_attr(buffer
, FALSE
, 0);
3064 if (restart_edit
!= 0 || (msg_scrolled
&& !need_wait_return
))
3065 /* Need to repeat the message after redrawing when:
3066 * - When restart_edit is set (otherwise there will be a delay
3067 * before redrawing).
3068 * - When the screen was scrolled but there is no wait-return
3077 col_print(buf
, buflen
, col
, vcol
)
3084 vim_snprintf((char *)buf
, buflen
, "%d", col
);
3086 vim_snprintf((char *)buf
, buflen
, "%d-%d", col
, vcol
);
3089 #if defined(FEAT_TITLE) || defined(PROTO)
3091 * put file name in title bar of window and in icon title
3094 static char_u
*lasttitle
= NULL
;
3095 static char_u
*lasticon
= NULL
;
3101 char_u
*t_str
= NULL
;
3103 char_u
*i_str
= NULL
;
3112 /* Postpone updating the title when 'lazyredraw' is set. */
3113 need_maketitle
= TRUE
;
3117 need_maketitle
= FALSE
;
3118 if (!p_title
&& !p_icon
)
3125 maxlen
= p_titlelen
* Columns
/ 100;
3131 if (*p_titlestring
!= NUL
)
3134 if (stl_syntax
& STL_IN_TITLE
)
3136 int use_sandbox
= FALSE
;
3137 int save_called_emsg
= called_emsg
;
3140 use_sandbox
= was_set_insecurely((char_u
*)"titlestring", 0);
3142 called_emsg
= FALSE
;
3143 build_stl_str_hl(curwin
, t_str
, sizeof(buf
),
3144 p_titlestring
, use_sandbox
,
3145 0, maxlen
, NULL
, NULL
);
3147 set_string_option_direct((char_u
*)"titlestring", -1,
3148 (char_u
*)"", OPT_FREE
, SID_ERROR
);
3149 called_emsg
|= save_called_emsg
;
3153 t_str
= p_titlestring
;
3157 /* format: "fname + (path) (1 of 2) - VIM" */
3159 if (curbuf
->b_fname
== NULL
)
3160 STRCPY(buf
, _("[No Name]"));
3163 p
= transstr(gettail(curbuf
->b_fname
));
3164 vim_strncpy(buf
, p
, IOSIZE
- 100);
3168 switch (bufIsChanged(curbuf
)
3169 + (curbuf
->b_p_ro
* 2)
3170 + (!curbuf
->b_p_ma
* 4))
3172 case 1: STRCAT(buf
, " +"); break;
3173 case 2: STRCAT(buf
, " ="); break;
3174 case 3: STRCAT(buf
, " =+"); break;
3176 case 6: STRCAT(buf
, " -"); break;
3178 case 7: STRCAT(buf
, " -+"); break;
3181 if (curbuf
->b_fname
!= NULL
)
3183 /* Get path of file, replace home dir with ~ */
3184 off
= (int)STRLEN(buf
);
3187 home_replace(curbuf
, curbuf
->b_ffname
,
3188 buf
+ off
, IOSIZE
- off
, TRUE
);
3189 #ifdef BACKSLASH_IN_FILENAME
3190 /* avoid "c:/name" to be reduced to "c" */
3191 if (isalpha(buf
[off
]) && buf
[off
+ 1] == ':')
3194 /* remove the file name */
3195 p
= gettail_sep(buf
+ off
);
3197 /* must be a help buffer */
3198 vim_strncpy(buf
+ off
, (char_u
*)_("help"),
3199 (size_t)(IOSIZE
- off
- 1));
3203 /* translate unprintable chars */
3204 p
= transstr(buf
+ off
);
3205 vim_strncpy(buf
+ off
, p
, (size_t)(IOSIZE
- off
- 1));
3210 append_arg_number(curwin
, buf
, IOSIZE
, FALSE
);
3212 #if defined(FEAT_CLIENTSERVER)
3213 if (serverName
!= NULL
)
3216 STRCAT(buf
, serverName
);
3220 STRCAT(buf
, " - VIM");
3224 /* make it shorter by removing a bit in the middle */
3225 len
= vim_strsize(buf
);
3227 trunc_string(buf
, buf
, maxlen
);
3231 mustset
= ti_change(t_str
, &lasttitle
);
3236 if (*p_iconstring
!= NUL
)
3239 if (stl_syntax
& STL_IN_ICON
)
3241 int use_sandbox
= FALSE
;
3242 int save_called_emsg
= called_emsg
;
3245 use_sandbox
= was_set_insecurely((char_u
*)"iconstring", 0);
3247 called_emsg
= FALSE
;
3248 build_stl_str_hl(curwin
, i_str
, sizeof(buf
),
3249 p_iconstring
, use_sandbox
,
3252 set_string_option_direct((char_u
*)"iconstring", -1,
3253 (char_u
*)"", OPT_FREE
, SID_ERROR
);
3254 called_emsg
|= save_called_emsg
;
3258 i_str
= p_iconstring
;
3262 if (buf_spname(curbuf
) != NULL
)
3263 i_name
= (char_u
*)buf_spname(curbuf
);
3264 else /* use file name only in icon */
3265 i_name
= gettail(curbuf
->b_ffname
);
3267 /* Truncate name at 100 bytes. */
3268 len
= (int)STRLEN(i_name
);
3274 len
+= (*mb_tail_off
)(i_name
, i_name
+ len
) + 1;
3278 STRCPY(i_str
, i_name
);
3279 trans_characters(i_str
, IOSIZE
);
3283 mustset
|= ti_change(i_str
, &lasticon
);
3290 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3291 * from "str" if it does.
3292 * Return TRUE when "*last" changed.
3295 ti_change(str
, last
)
3299 if ((str
== NULL
) != (*last
== NULL
)
3300 || (str
!= NULL
&& *last
!= NULL
&& STRCMP(str
, *last
) != 0))
3306 *last
= vim_strsave(str
);
3313 * Put current window title back (used after calling a shell)
3318 mch_settitle(lasttitle
, lasticon
);
3321 # if defined(EXITFREE) || defined(PROTO)
3325 vim_free(lasttitle
);
3330 #endif /* FEAT_TITLE */
3332 #if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
3334 * Build a string from the status line items in "fmt".
3335 * Return length of string in screen cells.
3337 * Normally works for window "wp", except when working for 'tabline' then it
3340 * Items are drawn interspersed with the text that surrounds it
3341 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3342 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3344 * If maxwidth is not zero, the string will be filled at any middle marker
3345 * or truncated if too long, fillchar is used for all whitespace.
3348 build_stl_str_hl(wp
, out
, outlen
, fmt
, use_sandbox
, fillchar
, maxwidth
, hltab
, tabtab
)
3350 char_u
*out
; /* buffer to write into != NameBuff */
3351 size_t outlen
; /* length of out[] */
3353 int use_sandbox UNUSED
; /* "fmt" was set insecurely, use sandbox */
3356 struct stl_hlrec
*hltab
; /* return: HL attributes (can be NULL) */
3357 struct stl_hlrec
*tabtab
; /* return: tab page nrs (can be NULL) */
3371 int prevchar_isflag
;
3372 int prevchar_isitem
;
3380 int groupitem
[STL_MAX_ITEM
];
3397 } item
[STL_MAX_ITEM
];
3405 char_u
*usefmt
= fmt
;
3406 struct stl_hlrec
*sp
;
3410 * When the format starts with "%!" then evaluate it as an expression and
3411 * use the result as the actual format string.
3413 if (fmt
[0] == '%' && fmt
[1] == '!')
3415 usefmt
= eval_to_string_safe(fmt
+ 2, NULL
, use_sandbox
);
3424 /* Can't handle a multi-byte fill character yet. */
3425 else if (mb_char2len(fillchar
) > 1)
3430 * Get line & check if empty (cursorpos will show "0-1").
3431 * If inversion is possible we use it. Else '=' characters are used.
3433 linecont
= ml_get_buf(wp
->w_buffer
, wp
->w_cursor
.lnum
, FALSE
);
3434 empty_line
= (*linecont
== NUL
);
3439 prevchar_isflag
= TRUE
;
3440 prevchar_isitem
= FALSE
;
3441 for (s
= usefmt
; *s
; )
3443 if (*s
!= NUL
&& *s
!= '%')
3444 prevchar_isflag
= prevchar_isitem
= FALSE
;
3447 * Handle up to the next '%' or the end.
3449 while (*s
!= NUL
&& *s
!= '%' && p
+ 1 < out
+ outlen
)
3451 if (*s
== NUL
|| p
+ 1 >= out
+ outlen
)
3455 * Handle one '%' item.
3460 if (p
+ 1 >= out
+ outlen
)
3463 prevchar_isflag
= prevchar_isitem
= FALSE
;
3466 if (*s
== STL_MIDDLEMARK
)
3471 item
[curitem
].type
= Middle
;
3472 item
[curitem
++].start
= p
;
3475 if (*s
== STL_TRUNCMARK
)
3478 item
[curitem
].type
= Trunc
;
3479 item
[curitem
++].start
= p
;
3489 t
= item
[groupitem
[groupdepth
]].start
;
3492 if (curitem
> groupitem
[groupdepth
] + 1
3493 && item
[groupitem
[groupdepth
]].minwid
== 0)
3495 /* remove group if all items are empty */
3496 for (n
= groupitem
[groupdepth
] + 1; n
< curitem
; n
++)
3497 if (item
[n
].type
== Normal
)
3505 if (l
> item
[groupitem
[groupdepth
]].maxwid
)
3507 /* truncate, remove n bytes of text at the start */
3511 /* Find the first character that should be included. */
3513 while (l
>= item
[groupitem
[groupdepth
]].maxwid
)
3515 l
-= ptr2cells(t
+ n
);
3516 n
+= (*mb_ptr2len
)(t
+ n
);
3521 n
= (long)(p
- t
) - item
[groupitem
[groupdepth
]].maxwid
+ 1;
3524 mch_memmove(t
+ 1, t
+ n
, (size_t)(p
- (t
+ n
)));
3527 /* Fill up space left over by half a double-wide char. */
3528 while (++l
< item
[groupitem
[groupdepth
]].minwid
)
3532 /* correct the start of the items for the truncation */
3533 for (l
= groupitem
[groupdepth
] + 1; l
< curitem
; l
++)
3536 if (item
[l
].start
< t
)
3540 else if (abs(item
[groupitem
[groupdepth
]].minwid
) > l
)
3543 n
= item
[groupitem
[groupdepth
]].minwid
;
3546 /* fill by appending characters */
3548 while (l
++ < n
&& p
+ 1 < out
+ outlen
)
3553 /* fill by inserting characters */
3554 mch_memmove(t
+ n
- l
, t
, (size_t)(p
- t
));
3556 if (p
+ l
>= out
+ outlen
)
3557 l
= (long)((out
+ outlen
) - p
- 1);
3559 for (n
= groupitem
[groupdepth
] + 1; n
< curitem
; n
++)
3581 if (VIM_ISDIGIT(*s
))
3583 minwid
= (int)getdigits(&s
);
3584 if (minwid
< 0) /* overflow */
3587 if (*s
== STL_USER_HL
)
3589 item
[curitem
].type
= Highlight
;
3590 item
[curitem
].start
= p
;
3591 item
[curitem
].minwid
= minwid
> 9 ? 1 : minwid
;
3596 if (*s
== STL_TABPAGENR
|| *s
== STL_TABCLOSENR
)
3598 if (*s
== STL_TABCLOSENR
)
3602 /* %X ends the close label, go back to the previously
3603 * define tab label nr. */
3604 for (n
= curitem
- 1; n
>= 0; --n
)
3605 if (item
[n
].type
== TabPage
&& item
[n
].minwid
>= 0)
3607 minwid
= item
[n
].minwid
;
3612 /* close nrs are stored as negative values */
3615 item
[curitem
].type
= TabPage
;
3616 item
[curitem
].start
= p
;
3617 item
[curitem
].minwid
= minwid
;
3625 if (VIM_ISDIGIT(*s
))
3627 maxwid
= (int)getdigits(&s
);
3628 if (maxwid
<= 0) /* overflow */
3632 minwid
= (minwid
> 50 ? 50 : minwid
) * l
;
3635 groupitem
[groupdepth
++] = curitem
;
3636 item
[curitem
].type
= Group
;
3637 item
[curitem
].start
= p
;
3638 item
[curitem
].minwid
= minwid
;
3639 item
[curitem
].maxwid
= maxwid
;
3644 if (vim_strchr(STL_ALL
, *s
) == NULL
)
3651 /* OK - now for the real work */
3662 fillable
= FALSE
; /* don't change ' ' to fillchar */
3663 if (buf_spname(wp
->w_buffer
) != NULL
)
3664 STRCPY(NameBuff
, buf_spname(wp
->w_buffer
));
3667 t
= (opt
== STL_FULLPATH
) ? wp
->w_buffer
->b_ffname
3668 : wp
->w_buffer
->b_fname
;
3669 home_replace(wp
->w_buffer
, t
, NameBuff
, MAXPATHL
, TRUE
);
3671 trans_characters(NameBuff
, MAXPATHL
);
3672 if (opt
!= STL_FILENAME
)
3675 str
= gettail(NameBuff
);
3678 case STL_VIM_EXPR
: /* '{' */
3681 while (*s
!= '}' && *s
!= NUL
&& p
+ 1 < out
+ outlen
)
3683 if (*s
!= '}') /* missing '}' or out of space */
3690 vim_snprintf((char *)tmp
, sizeof(tmp
), "%d", curbuf
->b_fnum
);
3691 set_internal_string_var((char_u
*)"actual_curbuf", tmp
);
3696 curbuf
= wp
->w_buffer
;
3698 str
= eval_to_string_safe(p
, &t
, use_sandbox
);
3702 do_unlet((char_u
*)"g:actual_curbuf", TRUE
);
3704 if (str
!= NULL
&& *str
!= 0)
3706 if (*skipdigits(str
) == NUL
)
3708 num
= atoi((char *)str
);
3718 num
= (wp
->w_buffer
->b_ml
.ml_flags
& ML_EMPTY
)
3719 ? 0L : (long)(wp
->w_cursor
.lnum
);
3723 num
= wp
->w_buffer
->b_ml
.ml_line_count
;
3727 num
= !(State
& INSERT
) && empty_line
3728 ? 0 : (int)wp
->w_cursor
.col
+ 1;
3732 case STL_VIRTCOL_ALT
:
3733 /* In list mode virtcol needs to be recomputed */
3734 virtcol
= wp
->w_virtcol
;
3735 if (wp
->w_p_list
&& lcs_tab1
== NUL
)
3737 wp
->w_p_list
= FALSE
;
3738 getvcol(wp
, &wp
->w_cursor
, NULL
, &virtcol
, NULL
);
3739 wp
->w_p_list
= TRUE
;
3742 /* Don't display %V if it's the same as %c. */
3743 if (opt
== STL_VIRTCOL_ALT
3744 && (virtcol
== (colnr_T
)(!(State
& INSERT
) && empty_line
3745 ? 0 : (int)wp
->w_cursor
.col
+ 1)))
3747 num
= (long)virtcol
;
3750 case STL_PERCENTAGE
:
3751 num
= (int)(((long)wp
->w_cursor
.lnum
* 100L) /
3752 (long)wp
->w_buffer
->b_ml
.ml_line_count
);
3755 case STL_ALTPERCENT
:
3757 get_rel_pos(wp
, str
, TMPLEN
);
3760 case STL_ARGLISTSTAT
:
3763 if (append_arg_number(wp
, tmp
, (int)sizeof(tmp
), FALSE
))
3769 if (get_keymap_str(wp
, tmp
, TMPLEN
))
3773 #if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
3774 num
= printer_page_num
;
3781 num
= wp
->w_buffer
->b_fnum
;
3788 l
= ml_find_line_or_offset(wp
->w_buffer
, wp
->w_cursor
.lnum
, NULL
);
3789 num
= (wp
->w_buffer
->b_ml
.ml_flags
& ML_EMPTY
) || l
< 0 ?
3790 0L : l
+ 1 + (!(State
& INSERT
) && empty_line
?
3791 0 : (int)wp
->w_cursor
.col
);
3798 if (wp
->w_cursor
.col
> (colnr_T
)STRLEN(linecont
))
3803 num
= (*mb_ptr2char
)(linecont
+ wp
->w_cursor
.col
);
3805 num
= linecont
[wp
->w_cursor
.col
];
3810 else if (num
== CAR
&& get_fileformat(wp
->w_buffer
) == EOL_MAC
)
3815 case STL_ROFLAG_ALT
:
3817 if (wp
->w_buffer
->b_p_ro
)
3818 str
= (char_u
*)((opt
== STL_ROFLAG_ALT
) ? ",RO" : "[RO]");
3822 case STL_HELPFLAG_ALT
:
3824 if (wp
->w_buffer
->b_help
)
3825 str
= (char_u
*)((opt
== STL_HELPFLAG_ALT
) ? ",HLP"
3831 if (*wp
->w_buffer
->b_p_ft
!= NUL
3832 && STRLEN(wp
->w_buffer
->b_p_ft
) < TMPLEN
- 3)
3834 vim_snprintf((char *)tmp
, sizeof(tmp
), "[%s]",
3835 wp
->w_buffer
->b_p_ft
);
3840 case STL_FILETYPE_ALT
:
3842 if (*wp
->w_buffer
->b_p_ft
!= NUL
3843 && STRLEN(wp
->w_buffer
->b_p_ft
) < TMPLEN
- 2)
3845 vim_snprintf((char *)tmp
, sizeof(tmp
), ",%s",
3846 wp
->w_buffer
->b_p_ft
);
3847 for (t
= tmp
; *t
!= 0; t
++)
3848 *t
= TOUPPER_LOC(*t
);
3854 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3855 case STL_PREVIEWFLAG
:
3856 case STL_PREVIEWFLAG_ALT
:
3859 str
= (char_u
*)((opt
== STL_PREVIEWFLAG_ALT
) ? ",PRV"
3865 case STL_MODIFIED_ALT
:
3867 switch ((opt
== STL_MODIFIED_ALT
)
3868 + bufIsChanged(wp
->w_buffer
) * 2
3869 + (!wp
->w_buffer
->b_p_ma
) * 4)
3871 case 2: str
= (char_u
*)"[+]"; break;
3872 case 3: str
= (char_u
*)",+"; break;
3873 case 4: str
= (char_u
*)"[-]"; break;
3874 case 5: str
= (char_u
*)",-"; break;
3875 case 6: str
= (char_u
*)"[+-]"; break;
3876 case 7: str
= (char_u
*)",+-"; break;
3882 while (*s
!= '#' && *s
!= NUL
)
3886 item
[curitem
].type
= Highlight
;
3887 item
[curitem
].start
= p
;
3888 item
[curitem
].minwid
= -syn_namen2id(t
, (int)(s
- t
));
3895 item
[curitem
].start
= p
;
3896 item
[curitem
].type
= Normal
;
3897 if (str
!= NULL
&& *str
)
3903 && ((!prevchar_isitem
&& *t
== ',')
3904 || (prevchar_isflag
&& *t
== ' ')))
3906 prevchar_isflag
= TRUE
;
3910 prevchar_isitem
= TRUE
;
3918 t
+= (*mb_ptr2len
)(t
);
3922 l
-= byte2cells(*t
++);
3923 if (p
+ 1 >= out
+ outlen
)
3929 for (; l
< minwid
&& p
+ 1 < out
+ outlen
; l
++)
3931 /* Don't put a "-" in front of a digit. */
3932 if (l
+ 1 == minwid
&& fillchar
== '-' && VIM_ISDIGIT(*t
))
3941 while (*t
&& p
+ 1 < out
+ outlen
)
3944 /* Change a space by fillchar, unless fillchar is '-' and a
3946 if (fillable
&& p
[-1] == ' '
3947 && (!VIM_ISDIGIT(*t
) || fillchar
!= '-'))
3950 for (; l
< minwid
&& p
+ 1 < out
+ outlen
; l
++)
3955 int nbase
= (base
== 'D' ? 10 : (base
== 'O' ? 8 : 16));
3958 if (p
+ 20 >= out
+ outlen
)
3959 break; /* not sufficient space */
3960 prevchar_isitem
= TRUE
;
3962 if (opt
== STL_VIRTCOL_ALT
)
3971 *t
++ = nbase
== 16 ? base
: (char_u
)(nbase
== 8 ? 'o' : 'd');
3974 for (n
= num
, l
= 1; n
>= nbase
; n
/= nbase
)
3976 if (opt
== STL_VIRTCOL_ALT
)
3982 while (l
-- > maxwid
)
3988 vim_snprintf((char *)p
, outlen
- (p
- out
), (char *)nstr
,
3992 vim_snprintf((char *)p
, outlen
- (p
- out
), (char *)nstr
,
3997 item
[curitem
].type
= Empty
;
3999 if (opt
== STL_VIM_EXPR
)
4002 if (num
>= 0 || (!itemisflag
&& str
&& *str
))
4003 prevchar_isflag
= FALSE
; /* Item not NULL, but not a flag */
4014 width
= vim_strsize(out
);
4015 if (maxwidth
> 0 && width
> maxwidth
)
4017 /* Result is too long, must truncate somewhere. */
4023 for ( ; l
< itemcnt
; l
++)
4024 if (item
[l
].type
== Trunc
)
4026 /* Truncate at %< item. */
4032 /* No %< item, truncate first item. */
4038 if (width
- vim_strsize(s
) >= maxwidth
)
4040 /* Truncation mark is beyond max length */
4048 width
+= ptr2cells(s
);
4049 if (width
>= maxwidth
)
4051 s
+= (*mb_ptr2len
)(s
);
4053 /* Fill up for half a double-wide character. */
4054 while (++width
< maxwidth
)
4059 s
= out
+ maxwidth
- 1;
4060 for (l
= 0; l
< itemcnt
; l
++)
4061 if (item
[l
].start
> s
)
4073 while (width
>= maxwidth
)
4075 width
-= ptr2cells(s
+ n
);
4076 n
+= (*mb_ptr2len
)(s
+ n
);
4081 n
= width
- maxwidth
+ 1;
4086 /* Fill up for half a double-wide character. */
4087 while (++width
< maxwidth
)
4094 --n
; /* count the '<' */
4095 for (; l
< itemcnt
; l
++)
4097 if (item
[l
].start
- n
>= s
)
4105 else if (width
< maxwidth
&& STRLEN(out
) + maxwidth
- width
+ 1 < outlen
)
4107 /* Apply STL_MIDDLE if any */
4108 for (l
= 0; l
< itemcnt
; l
++)
4109 if (item
[l
].type
== Middle
)
4113 p
= item
[l
].start
+ maxwidth
- width
;
4114 STRMOVE(p
, item
[l
].start
);
4115 for (s
= item
[l
].start
; s
< p
; s
++)
4117 for (l
++; l
< itemcnt
; l
++)
4118 item
[l
].start
+= maxwidth
- width
;
4123 /* Store the info about highlighting. */
4127 for (l
= 0; l
< itemcnt
; l
++)
4129 if (item
[l
].type
== Highlight
)
4131 sp
->start
= item
[l
].start
;
4132 sp
->userhl
= item
[l
].minwid
;
4140 /* Store the info about tab pages labels. */
4144 for (l
= 0; l
< itemcnt
; l
++)
4146 if (item
[l
].type
== TabPage
)
4148 sp
->start
= item
[l
].start
;
4149 sp
->userhl
= item
[l
].minwid
;
4159 #endif /* FEAT_STL_OPT */
4161 #if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4162 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
4164 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4165 * using "Top", "Bot" or "All" when appropriate.
4168 get_rel_pos(wp
, buf
, buflen
)
4173 long above
; /* number of lines above window */
4174 long below
; /* number of lines below window */
4176 above
= wp
->w_topline
- 1;
4178 above
+= diff_check_fill(wp
, wp
->w_topline
) - wp
->w_topfill
;
4180 below
= wp
->w_buffer
->b_ml
.ml_line_count
- wp
->w_botline
+ 1;
4182 vim_strncpy(buf
, (char_u
*)(above
== 0 ? _("All") : _("Bot")),
4183 (size_t)(buflen
- 1));
4184 else if (above
<= 0)
4185 vim_strncpy(buf
, (char_u
*)_("Top"), (size_t)(buflen
- 1));
4187 vim_snprintf((char *)buf
, (size_t)buflen
, "%2d%%", above
> 1000000L
4188 ? (int)(above
/ ((above
+ below
) / 100L))
4189 : (int)(above
* 100L / (above
+ below
)));
4194 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
4195 * Return TRUE if it was appended.
4198 append_arg_number(wp
, buf
, buflen
, add_file
)
4202 int add_file
; /* Add "file" before the arg number */
4206 if (ARGCOUNT
<= 1) /* nothing to do */
4209 p
= buf
+ STRLEN(buf
); /* go to the end of the buffer */
4210 if (p
- buf
+ 35 >= buflen
) /* getting too long */
4219 vim_snprintf((char *)p
, (size_t)(buflen
- (p
- buf
)),
4220 wp
->w_arg_idx_invalid
? "(%d) of %d)"
4221 : "%d of %d)", wp
->w_arg_idx
+ 1, ARGCOUNT
);
4226 * If fname is not a full path, make it a full path.
4227 * Returns pointer to allocated memory (NULL for failure).
4234 * Force expanding the path always for Unix, because symbolic links may
4235 * mess up the full path name, even though it starts with a '/'.
4236 * Also expand when there is ".." in the file name, try to remove it,
4237 * because "c:/src/../README" is equal to "c:/README".
4238 * Similarly "c:/src//file" is equal to "c:/src/file".
4239 * For MS-Windows also expand names like "longna~1" to "longname".
4242 return FullName_save(fname
, TRUE
);
4244 if (!vim_isAbsName(fname
)
4245 || strstr((char *)fname
, "..") != NULL
4246 || strstr((char *)fname
, "//") != NULL
4247 # ifdef BACKSLASH_IN_FILENAME
4248 || strstr((char *)fname
, "\\\\") != NULL
4250 # if defined(MSWIN) || defined(DJGPP)
4251 || vim_strchr(fname
, '~') != NULL
4254 return FullName_save(fname
, FALSE
);
4256 fname
= vim_strsave(fname
);
4258 # ifdef USE_FNAME_CASE
4259 # ifdef USE_LONG_FNAME
4264 fname_case(fname
, 0); /* set correct case for file name */
4273 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4274 * "ffname" becomes a pointer to allocated memory (or NULL).
4277 fname_expand(buf
, ffname
, sfname
)
4282 if (*ffname
== NULL
) /* if no file name given, nothing to do */
4284 if (*sfname
== NULL
) /* if no short file name given, use ffname */
4286 *ffname
= fix_fname(*ffname
); /* expand to full path */
4288 #ifdef FEAT_SHORTCUT
4293 /* If the file name is a shortcut file, use the file it links to. */
4294 rfname
= mch_resolve_shortcut(*ffname
);
4306 * Get the file name for an argument list entry.
4314 /* Use the name from the associated buffer if it exists. */
4315 bp
= buflist_findnr(aep
->ae_fnum
);
4316 if (bp
== NULL
|| bp
->b_fname
== NULL
)
4317 return aep
->ae_fname
;
4321 #if defined(FEAT_WINDOWS) || defined(PROTO)
4323 * do_arg_all(): Open up to 'count' windows, one for each argument.
4326 do_arg_all(count
, forceit
, keep_tabs
)
4328 int forceit
; /* hide buffers in current windows */
4329 int keep_tabs
; /* keep current tabs, for ":tab drop file" */
4333 char_u
*opened
; /* array of flags for which args are open */
4334 int opened_len
; /* length of opened[] */
4335 int use_firstwin
= FALSE
; /* use first window for arglist */
4338 alist_T
*alist
; /* argument list to be used */
4341 int had_tab
= cmdmod
.tab
;
4342 win_T
*new_curwin
= NULL
;
4343 tabpage_T
*new_curtab
= NULL
;
4347 /* Don't give an error message. We don't want it when the ":all"
4348 * command is in the .vimrc. */
4353 opened_len
= ARGCOUNT
;
4354 opened
= alloc_clear((unsigned)opened_len
);
4359 need_mouse_correct
= TRUE
;
4363 * Try closing all windows that are not in the argument list.
4364 * Also close windows that are not full width;
4365 * When 'hidden' or "forceit" set the buffer becomes hidden.
4366 * Windows that have a changed buffer and can't be hidden won't be closed.
4367 * When the ":tab" modifier was used do this for all tab pages.
4370 goto_tabpage_tp(first_tabpage
);
4373 tpnext
= curtab
->tp_next
;
4374 for (wp
= firstwin
; wp
!= NULL
; wp
= wpnext
)
4376 wpnext
= wp
->w_next
;
4378 if (buf
->b_ffname
== NULL
4379 || buf
->b_nwindows
> 1
4380 #ifdef FEAT_VERTSPLIT
4381 || wp
->w_width
!= Columns
4387 /* check if the buffer in this window is in the arglist */
4388 for (i
= 0; i
< ARGCOUNT
; ++i
)
4390 if (ARGLIST
[i
].ae_fnum
== buf
->b_fnum
4391 || fullpathcmp(alist_name(&ARGLIST
[i
]),
4392 buf
->b_ffname
, TRUE
) & FPC_SAME
)
4400 new_curtab
= curtab
;
4403 if (wp
->w_alist
!= curwin
->w_alist
)
4405 /* Use the current argument list for all windows
4406 * containing a file from it. */
4407 alist_unlink(wp
->w_alist
);
4408 wp
->w_alist
= curwin
->w_alist
;
4409 ++wp
->w_alist
->al_refcount
;
4417 if (i
== ARGCOUNT
&& !keep_tabs
) /* close this window */
4419 if (P_HID(buf
) || forceit
|| buf
->b_nwindows
> 1
4420 || !bufIsChanged(buf
))
4422 /* If the buffer was changed, and we would like to hide it,
4423 * try autowriting. */
4424 if (!P_HID(buf
) && buf
->b_nwindows
<= 1
4425 && bufIsChanged(buf
))
4427 (void)autowrite(buf
, FALSE
);
4429 /* check if autocommands removed the window */
4430 if (!win_valid(wp
) || !buf_valid(buf
))
4432 wpnext
= firstwin
; /* start all over... */
4438 /* don't close last window */
4439 if (firstwin
== lastwin
&& first_tabpage
->tp_next
== NULL
)
4441 use_firstwin
= TRUE
;
4445 win_close(wp
, !P_HID(buf
) && !bufIsChanged(buf
));
4446 # ifdef FEAT_AUTOCMD
4447 /* check if autocommands removed the next window */
4448 if (!win_valid(wpnext
))
4449 wpnext
= firstwin
; /* start all over... */
4457 /* Without the ":tab" modifier only do the current tab page. */
4458 if (had_tab
== 0 || tpnext
== NULL
)
4461 # ifdef FEAT_AUTOCMD
4462 /* check if autocommands removed the next tab page */
4463 if (!valid_tabpage(tpnext
))
4464 tpnext
= first_tabpage
; /* start all over...*/
4466 goto_tabpage_tp(tpnext
);
4470 * Open a window for files in the argument list that don't have one.
4471 * ARGCOUNT may change while doing this, because of autocommands.
4473 if (count
> ARGCOUNT
|| count
<= 0)
4476 /* Autocommands may do anything to the argument list. Make sure it's not
4477 * freed while we are working here by "locking" it. We still have to
4478 * watch out for its size to be changed. */
4479 alist
= curwin
->w_alist
;
4480 ++alist
->al_refcount
;
4483 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4487 win_enter(lastwin
, FALSE
);
4489 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4490 * leaving an empty tab page when executed locally. */
4491 if (keep_tabs
&& bufempty() && curbuf
->b_nwindows
== 1
4492 && curbuf
->b_ffname
== NULL
&& !curbuf
->b_changed
)
4493 use_firstwin
= TRUE
;
4496 for (i
= 0; i
< count
&& i
< alist
->al_ga
.ga_len
&& !got_int
; ++i
)
4498 if (alist
== &global_alist
&& i
== global_alist
.al_ga
.ga_len
- 1)
4499 arg_had_last
= TRUE
;
4500 if (i
< opened_len
&& opened
[i
])
4502 /* Move the already present window to below the current window */
4503 if (curwin
->w_arg_idx
!= i
)
4505 for (wpnext
= firstwin
; wpnext
!= NULL
; wpnext
= wpnext
->w_next
)
4507 if (wpnext
->w_arg_idx
== i
)
4509 win_move_after(wpnext
, curwin
);
4515 else if (split_ret
== OK
)
4517 if (!use_firstwin
) /* split current window */
4520 p_ea
= TRUE
; /* use space from all windows */
4521 split_ret
= win_split(0, WSP_ROOM
| WSP_BELOW
);
4523 if (split_ret
== FAIL
)
4527 else /* first window: do autocmd for leaving this buffer */
4534 curwin
->w_arg_idx
= i
;
4537 new_curwin
= curwin
;
4538 new_curtab
= curtab
;
4540 (void)do_ecmd(0, alist_name(&AARGLIST(alist
)[i
]), NULL
, NULL
,
4542 ((P_HID(curwin
->w_buffer
)
4543 || bufIsChanged(curwin
->w_buffer
)) ? ECMD_HIDE
: 0)
4544 + ECMD_OLDBUF
, curwin
);
4549 use_firstwin
= FALSE
;
4553 /* When ":tab" was used open a new tab for a new window repeatedly. */
4554 if (had_tab
> 0 && tabpage_index(NULL
) <= p_tpm
)
4558 /* Remove the "lock" on the argument list. */
4559 alist_unlink(alist
);
4564 /* to window with first arg */
4565 if (valid_tabpage(new_curtab
))
4566 goto_tabpage_tp(new_curtab
);
4567 if (win_valid(new_curwin
))
4568 win_enter(new_curwin
, FALSE
);
4576 # if defined(FEAT_LISTCMDS) || defined(PROTO)
4578 * Open a window for a number of buffers.
4590 int count
; /* Maximum number of windows to open. */
4591 int all
; /* When TRUE also load inactive buffers. */
4593 int had_tab
= cmdmod
.tab
;
4597 if (eap
->addr_count
== 0) /* make as many windows as possible */
4600 count
= eap
->line2
; /* make as many windows as specified */
4601 if (eap
->cmdidx
== CMD_unhide
|| eap
->cmdidx
== CMD_sunhide
)
4609 need_mouse_correct
= TRUE
;
4613 * Close superfluous windows (two windows for the same buffer).
4614 * Also close windows that are not full-width.
4618 goto_tabpage_tp(first_tabpage
);
4622 tpnext
= curtab
->tp_next
;
4623 for (wp
= firstwin
; wp
!= NULL
; wp
= wpnext
)
4625 wpnext
= wp
->w_next
;
4626 if ((wp
->w_buffer
->b_nwindows
> 1
4627 #ifdef FEAT_VERTSPLIT
4628 || ((cmdmod
.split
& WSP_VERT
)
4629 ? wp
->w_height
+ wp
->w_status_height
< Rows
- p_ch
4631 : wp
->w_width
!= Columns
)
4634 || (had_tab
> 0 && wp
!= firstwin
)
4636 ) && firstwin
!= lastwin
)
4638 win_close(wp
, FALSE
);
4640 wpnext
= firstwin
; /* just in case an autocommand does
4641 something strange with windows */
4642 tpnext
= first_tabpage
; /* start all over...*/
4651 /* Without the ":tab" modifier only do the current tab page. */
4652 if (had_tab
== 0 || tpnext
== NULL
)
4654 goto_tabpage_tp(tpnext
);
4659 * Go through the buffer list. When a buffer doesn't have a window yet,
4660 * open one. Otherwise move the window to the right position.
4661 * Watch out for autocommands that delete buffers or windows!
4664 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4667 win_enter(lastwin
, FALSE
);
4671 for (buf
= firstbuf
; buf
!= NULL
&& open_wins
< count
; buf
= buf
->b_next
)
4673 /* Check if this buffer needs a window */
4674 if ((!all
&& buf
->b_ml
.ml_mfp
== NULL
) || !buf
->b_p_bl
)
4680 /* With the ":tab" modifier don't move the window. */
4681 if (buf
->b_nwindows
> 0)
4682 wp
= lastwin
; /* buffer has a window, skip it */
4689 /* Check if this buffer already has a window */
4690 for (wp
= firstwin
; wp
!= NULL
; wp
= wp
->w_next
)
4691 if (wp
->w_buffer
== buf
)
4693 /* If the buffer already has a window, move it */
4695 win_move_after(wp
, curwin
);
4698 if (wp
== NULL
&& split_ret
== OK
)
4700 /* Split the window and put the buffer in it */
4702 p_ea
= TRUE
; /* use space from all windows */
4703 split_ret
= win_split(0, WSP_ROOM
| WSP_BELOW
);
4706 if (split_ret
== FAIL
)
4709 /* Open the buffer in this window. */
4710 #if defined(HAS_SWAP_EXISTS_ACTION)
4711 swap_exists_action
= SEA_DIALOG
;
4713 set_curbuf(buf
, DOBUF_GOTO
);
4715 if (!buf_valid(buf
)) /* autocommands deleted the buffer!!! */
4717 #if defined(HAS_SWAP_EXISTS_ACTION)
4718 swap_exists_action
= SEA_NONE
;
4723 #if defined(HAS_SWAP_EXISTS_ACTION)
4724 if (swap_exists_action
== SEA_QUIT
)
4726 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4729 /* Reset the error/interrupt/exception state here so that
4730 * aborting() returns FALSE when closing a window. */
4734 /* User selected Quit at ATTENTION prompt; close this window. */
4735 win_close(curwin
, TRUE
);
4737 swap_exists_action
= SEA_NONE
;
4738 swap_exists_did_quit
= TRUE
;
4740 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4741 /* Restore the error/interrupt/exception state if not
4742 * discarded by a new aborting error, interrupt, or uncaught
4748 handle_swap_exists(NULL
);
4755 (void)vgetc(); /* only break the file loading, not the rest */
4759 /* Autocommands deleted the buffer or aborted script processing!!! */
4764 /* When ":tab" was used open a new tab for a new window repeatedly. */
4765 if (had_tab
> 0 && tabpage_index(NULL
) <= p_tpm
)
4772 win_enter(firstwin
, FALSE
); /* back to first window */
4778 * Close superfluous windows.
4780 for (wp
= lastwin
; open_wins
> count
; )
4782 r
= (P_HID(wp
->w_buffer
) || !bufIsChanged(wp
->w_buffer
)
4783 || autowrite(wp
->w_buffer
, FALSE
) == OK
);
4787 /* BufWrite Autocommands made the window invalid, start over */
4794 win_close(wp
, !P_HID(wp
->w_buffer
));
4806 # endif /* FEAT_LISTCMDS */
4808 #endif /* FEAT_WINDOWS */
4810 static int chk_modeline
__ARGS((linenr_T
, int));
4813 * do_modelines() - process mode lines for the current file
4816 * OPT_WINONLY only set options local to window
4817 * OPT_NOWIN don't set options local to window
4819 * Returns immediately if the "ml" option isn't set.
4827 static int entered
= 0;
4829 if (!curbuf
->b_p_ml
|| (nmlines
= (int)p_mls
) == 0)
4832 /* Disallow recursive entry here. Can happen when executing a modeline
4833 * triggers an autocommand, which reloads modelines with a ":do". */
4838 for (lnum
= 1; lnum
<= curbuf
->b_ml
.ml_line_count
&& lnum
<= nmlines
;
4840 if (chk_modeline(lnum
, flags
) == FAIL
)
4843 for (lnum
= curbuf
->b_ml
.ml_line_count
; lnum
> 0 && lnum
> nmlines
4844 && lnum
> curbuf
->b_ml
.ml_line_count
- nmlines
; --lnum
)
4845 if (chk_modeline(lnum
, flags
) == FAIL
)
4850 #include "version.h" /* for version number */
4853 * chk_modeline() - check a single line for a mode string
4854 * Return FAIL if an error encountered.
4857 chk_modeline(lnum
, flags
)
4859 int flags
; /* Same as for do_modelines(). */
4863 char_u
*linecopy
; /* local copy of any modeline found */
4868 char_u
*save_sourcing_name
;
4869 linenr_T save_sourcing_lnum
;
4875 for (s
= ml_get(lnum
); *s
!= NUL
; ++s
)
4877 if (prev
== -1 || vim_isspace(prev
))
4879 if ((prev
!= -1 && STRNCMP(s
, "ex:", (size_t)3) == 0)
4880 || STRNCMP(s
, "vi:", (size_t)3) == 0)
4882 if (STRNCMP(s
, "vim", 3) == 0)
4884 if (s
[3] == '<' || s
[3] == '=' || s
[3] == '>')
4888 vers
= getdigits(&e
);
4891 || (VIM_VERSION_100
>= vers
&& isdigit(s
[3]))
4892 || (VIM_VERSION_100
< vers
&& s
[3] == '<')
4893 || (VIM_VERSION_100
> vers
&& s
[3] == '>')
4894 || (VIM_VERSION_100
== vers
&& s
[3] == '=')))
4903 do /* skip over "ex:", "vi:" or "vim:" */
4905 while (s
[-1] != ':');
4907 s
= linecopy
= vim_strsave(s
); /* copy the line, it will change */
4908 if (linecopy
== NULL
)
4911 save_sourcing_lnum
= sourcing_lnum
;
4912 save_sourcing_name
= sourcing_name
;
4913 sourcing_lnum
= lnum
; /* prepare for emsg() */
4914 sourcing_name
= (char_u
*)"modelines";
4917 while (end
== FALSE
)
4924 * Find end of set command: ':' or end of line.
4925 * Skip over "\:", replacing it with ":".
4927 for (e
= s
; *e
!= ':' && *e
!= NUL
; ++e
)
4928 if (e
[0] == '\\' && e
[1] == ':')
4934 * If there is a "set" command, require a terminating ':' and
4935 * ignore the stuff after the ':'.
4936 * "vi:set opt opt opt: foo" -- foo not interpreted
4937 * "vi:opt opt opt: foo" -- foo interpreted
4938 * Accept "se" for compatibility with Elvis.
4940 if (STRNCMP(s
, "set ", (size_t)4) == 0
4941 || STRNCMP(s
, "se ", (size_t)3) == 0)
4943 if (*e
!= ':') /* no terminating ':'? */
4946 s
= vim_strchr(s
, ' ') + 1;
4948 *e
= NUL
; /* truncate the set command */
4950 if (*s
!= NUL
) /* skip over an empty "::" */
4953 save_SID
= current_SID
;
4954 current_SID
= SID_MODELINE
;
4956 retval
= do_set(s
, OPT_MODELINE
| OPT_LOCAL
| flags
);
4958 current_SID
= save_SID
;
4960 if (retval
== FAIL
) /* stop if error found */
4963 s
= e
+ 1; /* advance to next part */
4966 sourcing_lnum
= save_sourcing_lnum
;
4967 sourcing_name
= save_sourcing_name
;
4974 #if defined(FEAT_VIMINFO) || defined(PROTO)
4976 read_viminfo_bufferlist(virp
, writing
)
4987 /* Handle long line and escaped characters. */
4988 xline
= viminfo_readstring(virp
, 1, FALSE
);
4990 /* don't read in if there are files on the command-line or if writing: */
4991 if (xline
!= NULL
&& !writing
&& ARGCOUNT
== 0
4992 && find_viminfo_parameter('%') != NULL
)
4994 /* Format is: <fname> Tab <lnum> Tab <col>.
4995 * Watch out for a Tab in the file name, work from the end. */
4998 tab
= vim_strrchr(xline
, '\t');
5002 col
= (colnr_T
)atoi((char *)tab
);
5003 tab
= vim_strrchr(xline
, '\t');
5007 lnum
= atol((char *)tab
);
5011 /* Expand "~/" in the file name at "line + 1" to a full path.
5012 * Then try shortening it by comparing with the current directory */
5013 expand_env(xline
, NameBuff
, MAXPATHL
);
5014 sfname
= shorten_fname1(NameBuff
);
5016 buf
= buflist_new(NameBuff
, sfname
, (linenr_T
)0, BLN_LISTED
);
5017 if (buf
!= NULL
) /* just in case... */
5019 buf
->b_last_cursor
.lnum
= lnum
;
5020 buf
->b_last_cursor
.col
= col
;
5021 buflist_setfpos(buf
, curwin
, lnum
, col
, FALSE
);
5026 return viminfo_readline(virp
);
5030 write_viminfo_bufferlist(fp
)
5042 if (find_viminfo_parameter('%') == NULL
)
5045 /* Without a number -1 is returned: do all buffers. */
5046 max_buffers
= get_viminfo_parameter('%');
5048 /* Allocate room for the file name, lnum and col. */
5049 #define LINE_BUF_LEN (MAXPATHL + 40)
5050 line
= alloc(LINE_BUF_LEN
);
5055 FOR_ALL_TAB_WINDOWS(tp
, win
)
5056 set_last_cursor(win
);
5058 set_last_cursor(curwin
);
5061 fprintf(fp
, _("\n# Buffer list:\n"));
5062 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
5064 if (buf
->b_fname
== NULL
5066 #ifdef FEAT_QUICKFIX
5069 || removable(buf
->b_ffname
))
5072 if (max_buffers
-- == 0)
5075 home_replace(NULL
, buf
->b_ffname
, line
, MAXPATHL
, TRUE
);
5077 vim_snprintf((char *)line
+ len
, len
- LINE_BUF_LEN
, "\t%ld\t%d",
5078 (long)buf
->b_last_cursor
.lnum
,
5079 buf
->b_last_cursor
.col
);
5080 viminfo_writestring(fp
, line
);
5088 * Return special buffer name.
5089 * Returns NULL when the buffer has a normal file name.
5095 #if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5096 if (bt_quickfix(buf
))
5102 * For location list window, w_llist_ref points to the location list.
5103 * For quickfix window, w_llist_ref is NULL.
5105 FOR_ALL_TAB_WINDOWS(tp
, win
)
5106 if (win
->w_buffer
== buf
)
5109 if (win
!= NULL
&& win
->w_llist_ref
!= NULL
)
5110 return _("[Location List]");
5112 return _("[Quickfix List]");
5115 #ifdef FEAT_QUICKFIX
5116 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5117 * contains the name as specified by the user */
5120 if (buf
->b_sfname
!= NULL
)
5121 return (char *)buf
->b_sfname
;
5122 return _("[Scratch]");
5125 if (buf
->b_fname
== NULL
)
5126 return _("[No Name]");
5131 #if defined(FEAT_SIGNS) || defined(PROTO)
5133 * Insert the sign into the signlist.
5136 insert_sign(buf
, prev
, next
, id
, lnum
, typenr
)
5137 buf_T
*buf
; /* buffer to store sign in */
5138 signlist_T
*prev
; /* previous sign entry */
5139 signlist_T
*next
; /* next sign entry */
5140 int id
; /* sign ID */
5141 linenr_T lnum
; /* line number which gets the mark */
5142 int typenr
; /* typenr of sign we are adding */
5144 signlist_T
*newsign
;
5146 newsign
= (signlist_T
*)lalloc((long_u
)sizeof(signlist_T
), FALSE
);
5147 if (newsign
!= NULL
)
5150 newsign
->lnum
= lnum
;
5151 newsign
->typenr
= typenr
;
5152 newsign
->next
= next
;
5153 #ifdef FEAT_NETBEANS_INTG
5154 newsign
->prev
= prev
;
5156 next
->prev
= newsign
;
5161 /* When adding first sign need to redraw the windows to create the
5162 * column for signs. */
5163 if (buf
->b_signlist
== NULL
)
5165 redraw_buf_later(buf
, NOT_VALID
);
5166 changed_cline_bef_curs();
5169 /* first sign in signlist */
5170 buf
->b_signlist
= newsign
;
5173 prev
->next
= newsign
;
5178 * Add the sign into the signlist. Find the right spot to do it though.
5181 buf_addsign(buf
, id
, lnum
, typenr
)
5182 buf_T
*buf
; /* buffer to store sign in */
5183 int id
; /* sign ID */
5184 linenr_T lnum
; /* line number which gets the mark */
5185 int typenr
; /* typenr of sign we are adding */
5187 signlist_T
*sign
; /* a sign in the signlist */
5188 signlist_T
*prev
; /* the previous sign */
5191 for (sign
= buf
->b_signlist
; sign
!= NULL
; sign
= sign
->next
)
5193 if (lnum
== sign
->lnum
&& id
== sign
->id
)
5195 sign
->typenr
= typenr
;
5199 #ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5204 #ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5205 /* XXX - GRP: Is this because of sign slide problem? Or is it
5206 * really needed? Or is it because we allow multiple signs per
5207 * line? If so, should I add that feature to FEAT_SIGNS?
5209 while (prev
!= NULL
&& prev
->lnum
== lnum
)
5212 sign
= buf
->b_signlist
;
5216 insert_sign(buf
, prev
, sign
, id
, lnum
, typenr
);
5221 #ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5222 /* XXX - GRP: See previous comment */
5223 while (prev
!= NULL
&& prev
->lnum
== lnum
)
5226 sign
= buf
->b_signlist
;
5230 insert_sign(buf
, prev
, sign
, id
, lnum
, typenr
);
5236 buf_change_sign_type(buf
, markId
, typenr
)
5237 buf_T
*buf
; /* buffer to store sign in */
5238 int markId
; /* sign ID */
5239 int typenr
; /* typenr of sign we are adding */
5241 signlist_T
*sign
; /* a sign in the signlist */
5243 for (sign
= buf
->b_signlist
; sign
!= NULL
; sign
= sign
->next
)
5245 if (sign
->id
== markId
)
5247 sign
->typenr
= typenr
;
5256 buf_getsigntype(buf
, lnum
, type
)
5259 int type
; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5261 signlist_T
*sign
; /* a sign in a b_signlist */
5263 for (sign
= buf
->b_signlist
; sign
!= NULL
; sign
= sign
->next
)
5264 if (sign
->lnum
== lnum
5265 && (type
== SIGN_ANY
5266 # ifdef FEAT_SIGN_ICONS
5267 || (type
== SIGN_ICON
5268 && sign_get_image(sign
->typenr
) != NULL
)
5270 || (type
== SIGN_TEXT
5271 && sign_get_text(sign
->typenr
) != NULL
)
5272 || (type
== SIGN_LINEHL
5273 && sign_get_attr(sign
->typenr
, TRUE
) != 0)))
5274 return sign
->typenr
;
5280 buf_delsign(buf
, id
)
5281 buf_T
*buf
; /* buffer sign is stored in */
5282 int id
; /* sign id */
5284 signlist_T
**lastp
; /* pointer to pointer to current sign */
5285 signlist_T
*sign
; /* a sign in a b_signlist */
5286 signlist_T
*next
; /* the next sign in a b_signlist */
5287 linenr_T lnum
; /* line number whose sign was deleted */
5289 lastp
= &buf
->b_signlist
;
5291 for (sign
= buf
->b_signlist
; sign
!= NULL
; sign
= next
)
5297 #ifdef FEAT_NETBEANS_INTG
5299 next
->prev
= sign
->prev
;
5306 lastp
= &sign
->next
;
5309 /* When deleted the last sign need to redraw the windows to remove the
5311 if (buf
->b_signlist
== NULL
)
5313 redraw_buf_later(buf
, NOT_VALID
);
5314 changed_cline_bef_curs();
5322 * Find the line number of the sign with the requested id. If the sign does
5323 * not exist, return 0 as the line number. This will still let the correct file
5327 buf_findsign(buf
, id
)
5328 buf_T
*buf
; /* buffer to store sign in */
5329 int id
; /* sign ID */
5331 signlist_T
*sign
; /* a sign in the signlist */
5333 for (sign
= buf
->b_signlist
; sign
!= NULL
; sign
= sign
->next
)
5341 buf_findsign_id(buf
, lnum
)
5342 buf_T
*buf
; /* buffer whose sign we are searching for */
5343 linenr_T lnum
; /* line number of sign */
5345 signlist_T
*sign
; /* a sign in the signlist */
5347 for (sign
= buf
->b_signlist
; sign
!= NULL
; sign
= sign
->next
)
5348 if (sign
->lnum
== lnum
)
5355 # if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5356 /* see if a given type of sign exists on a specific line */
5358 buf_findsigntype_id(buf
, lnum
, typenr
)
5359 buf_T
*buf
; /* buffer whose sign we are searching for */
5360 linenr_T lnum
; /* line number of sign */
5361 int typenr
; /* sign type number */
5363 signlist_T
*sign
; /* a sign in the signlist */
5365 for (sign
= buf
->b_signlist
; sign
!= NULL
; sign
= sign
->next
)
5366 if (sign
->lnum
== lnum
&& sign
->typenr
== typenr
)
5373 # if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5374 /* return the number of icons on the given line */
5376 buf_signcount(buf
, lnum
)
5380 signlist_T
*sign
; /* a sign in the signlist */
5383 for (sign
= buf
->b_signlist
; sign
!= NULL
; sign
= sign
->next
)
5384 if (sign
->lnum
== lnum
)
5385 if (sign_get_image(sign
->typenr
) != NULL
)
5390 # endif /* FEAT_SIGN_ICONS */
5391 # endif /* FEAT_NETBEANS_INTG */
5395 * Delete signs in buffer "buf".
5398 buf_delete_signs(buf
)
5403 while (buf
->b_signlist
!= NULL
)
5405 next
= buf
->b_signlist
->next
;
5406 vim_free(buf
->b_signlist
);
5407 buf
->b_signlist
= next
;
5412 * Delete all signs in all buffers.
5415 buf_delete_all_signs()
5417 buf_T
*buf
; /* buffer we are checking for signs */
5419 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
5420 if (buf
->b_signlist
!= NULL
)
5422 /* Need to redraw the windows to remove the sign column. */
5423 redraw_buf_later(buf
, NOT_VALID
);
5424 buf_delete_signs(buf
);
5429 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5432 sign_list_placed(rbuf
)
5439 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5447 if (buf
->b_signlist
!= NULL
)
5449 vim_snprintf(lbuf
, BUFSIZ
, _("Signs for %s:"), buf
->b_fname
);
5450 MSG_PUTS_ATTR(lbuf
, hl_attr(HLF_D
));
5453 for (p
= buf
->b_signlist
; p
!= NULL
; p
= p
->next
)
5455 vim_snprintf(lbuf
, BUFSIZ
, _(" line=%ld id=%d name=%s"),
5456 (long)p
->lnum
, p
->id
, sign_typenr2name(p
->typenr
));
5467 * Adjust a placed sign for inserted/deleted lines.
5470 sign_mark_adjust(line1
, line2
, amount
, amount_after
)
5476 signlist_T
*sign
; /* a sign in a b_signlist */
5478 for (sign
= curbuf
->b_signlist
; sign
!= NULL
; sign
= sign
->next
)
5480 if (sign
->lnum
>= line1
&& sign
->lnum
<= line2
)
5482 if (amount
== MAXLNUM
)
5485 sign
->lnum
+= amount
;
5487 else if (sign
->lnum
> line2
)
5488 sign
->lnum
+= amount_after
;
5491 #endif /* FEAT_SIGNS */
5494 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5500 if (on
!= curbuf
->b_p_bl
)
5502 curbuf
->b_p_bl
= on
;
5505 apply_autocmds(EVENT_BUFADD
, NULL
, NULL
, FALSE
, curbuf
);
5507 apply_autocmds(EVENT_BUFDELETE
, NULL
, NULL
, FALSE
, curbuf
);
5513 * Read the file for "buf" again and check if the contents changed.
5514 * Return TRUE if it changed or this could not be checked.
5517 buf_contents_changed(buf
)
5526 /* Allocate a buffer without putting it in the buffer list. */
5527 newbuf
= buflist_new(NULL
, NULL
, (linenr_T
)1, BLN_DUMMY
);
5531 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5532 if (prep_exarg(&ea
, buf
) == FAIL
)
5534 wipe_buffer(newbuf
, FALSE
);
5538 /* set curwin/curbuf to buf and save a few things */
5539 aucmd_prepbuf(&aco
, newbuf
);
5541 if (ml_open(curbuf
) == OK
5542 && readfile(buf
->b_ffname
, buf
->b_fname
,
5543 (linenr_T
)0, (linenr_T
)0, (linenr_T
)MAXLNUM
,
5544 &ea
, READ_NEW
| READ_DUMMY
) == OK
)
5546 /* compare the two files line by line */
5547 if (buf
->b_ml
.ml_line_count
== curbuf
->b_ml
.ml_line_count
)
5550 for (lnum
= 1; lnum
<= curbuf
->b_ml
.ml_line_count
; ++lnum
)
5551 if (STRCMP(ml_get_buf(buf
, lnum
, FALSE
), ml_get(lnum
)) != 0)
5560 /* restore curwin/curbuf and a few other things */
5561 aucmd_restbuf(&aco
);
5563 if (curbuf
!= newbuf
) /* safety check */
5564 wipe_buffer(newbuf
, FALSE
);
5570 * Wipe out a buffer and decrement the last buffer number if it was used for
5571 * this buffer. Call this to wipe out a temp buffer that does not contain any
5575 wipe_buffer(buf
, aucmd
)
5577 int aucmd UNUSED
; /* When TRUE trigger autocommands. */
5579 if (buf
->b_fnum
== top_file_num
- 1)
5583 if (!aucmd
) /* Don't trigger BufDelete autocommands here. */
5586 close_buffer(NULL
, buf
, DOBUF_WIPE
);