Data Structures | Macros | Enumerations | Functions
buffer.c File Reference
#include <assert.h>
#include <inttypes.h>
#include <stdbool.h>
#include <string.h>
#include "nvim/api/private/helpers.h"
#include "nvim/api/vim.h"
#include "nvim/ascii.h"
#include "nvim/buffer.h"
#include "nvim/buffer_updates.h"
#include "nvim/change.h"
#include "nvim/channel.h"
#include "nvim/charset.h"
#include "nvim/cursor.h"
#include "nvim/diff.h"
#include "nvim/digraph.h"
#include "nvim/eval.h"
#include "nvim/ex_cmds.h"
#include "nvim/ex_cmds2.h"
#include "nvim/ex_docmd.h"
#include "nvim/ex_eval.h"
#include "nvim/ex_getln.h"
#include "nvim/extmark.h"
#include "nvim/file_search.h"
#include "nvim/fileio.h"
#include "nvim/fold.h"
#include "nvim/garray.h"
#include "nvim/getchar.h"
#include "nvim/hashtab.h"
#include "nvim/highlight.h"
#include "nvim/indent.h"
#include "nvim/indent_c.h"
#include "nvim/main.h"
#include "nvim/mark.h"
#include "nvim/mbyte.h"
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/misc1.h"
#include "nvim/move.h"
#include "nvim/option.h"
#include "nvim/os/input.h"
#include "nvim/os/os.h"
#include "nvim/os/time.h"
#include "nvim/os_unix.h"
#include "nvim/path.h"
#include "nvim/plines.h"
#include "nvim/quickfix.h"
#include "nvim/regexp.h"
#include "nvim/screen.h"
#include "nvim/shada.h"
#include "nvim/sign.h"
#include "nvim/spell.h"
#include "nvim/strings.h"
#include "nvim/syntax.h"
#include "nvim/ui.h"
#include "nvim/undo.h"
#include "nvim/version.h"
#include "nvim/vim.h"
#include "nvim/window.h"

Data Structures

struct  bufmatch_T


#define MAX_STL_EVAL_DEPTH   100
#define SPACE_FOR_FNAME   (sizeof(buf) - 100)
#define SPACE_FOR_DIR   (sizeof(buf) - 20)
#define SPACE_FOR_ARGNR   (sizeof(buf) - 10)
#define TMPLEN   70


enum  BufFreeFlags { kBffClearWinInfo = 1, kBffInitChangedtick = 2 }
enum  NumberBase { kNumBaseDecimal = 10, kNumBaseHexadecimal = 16 }


int open_buffer (int read_stdin, exarg_T *eap, int flags)
void set_bufref (bufref_T *bufref, buf_T *buf)
bool bufref_valid (bufref_T *bufref)
bool close_buffer (win_T *win, buf_T *buf, int action, bool abort_if_last)
void buf_clear_file (buf_T *buf)
 Make buffer not contain a file. More...
void buf_clear (void)
 Clears the current buffer contents. More...
void buf_freeall (buf_T *buf, int flags)
void goto_buffer (exarg_T *eap, int start, int dir, int count)
void handle_swap_exists (bufref_T *old_curbuf)
char * do_bufdel (int command, char_u *arg, int addr_count, int start_bnr, int end_bnr, int forceit)
int do_buffer (int action, int start, int dir, int count, int forceit)
void set_curbuf (buf_T *buf, int action)
void enter_buffer (buf_T *buf)
void do_autochdir (void)
void no_write_message (void)
void no_write_message_nobang (const buf_T *const buf) FUNC_ATTR_NONNULL_ALL
buf_Tbuflist_new (char_u *ffname_arg, char_u *sfname_arg, linenr_T lnum, int flags)
bool curbuf_reusable (void)
void free_buf_options (buf_T *buf, int free_p_ff)
int buflist_getfile (int n, linenr_T lnum, int options, int forceit)
void buflist_getfpos (void)
buf_Tbuflist_findname_exp (char_u *fname)
buf_Tbuflist_findname (char_u *ffname)
int buflist_findpat (const char_u *pattern, const char_u *pattern_end, bool unlisted, bool diffmode, bool curtab_only) FUNC_ATTR_NONNULL_ARG(1)
int ExpandBufnames (char_u *pat, int *num_file, char_u ***file, int options)
buf_Tbuflist_findnr (int nr)
 Find a file in the buffer list by buffer number. More...
char_ubuflist_nr2name (int n, int fullname, int helptail)
void buflist_setfpos (buf_T *const buf, win_T *const win, linenr_T lnum, colnr_T col, bool copy_options) FUNC_ATTR_NONNULL_ARG(1)
void get_winopts (buf_T *buf)
pos_Tbuflist_findfpos (buf_T *buf)
linenr_T buflist_findlnum (buf_T *buf)
void buflist_list (exarg_T *eap)
int buflist_name_nr (int fnum, char_u **fname, linenr_T *lnum)
int setfname (buf_T *buf, char_u *ffname_arg, char_u *sfname_arg, bool message)
void buf_set_name (int fnum, char_u *name)
void buf_name_changed (buf_T *buf)
buf_Tsetaltfname (char_u *ffname, char_u *sfname, linenr_T lnum)
char_ugetaltfname (bool errmsg)
int buflist_add (char_u *fname, int flags)
void buflist_altfpos (win_T *win)
void buf_set_file_id (buf_T *buf)
void fileinfo (int fullname, int shorthelp, int dont_truncate)
void col_print (char_u *buf, size_t buflen, int col, int vcol)
void maketitle (void)
void resettitle (void)
 Set current window title. More...
int build_stl_str_hl (win_T *wp, char_u *out, size_t outlen, char_u *fmt, int use_sandbox, char_u fillchar, int maxwidth, stl_hlrec_t **hltab, StlClickRecord **tabtab)
void get_rel_pos (win_T *wp, char_u *buf, int buflen)
void fname_expand (buf_T *buf, char_u **ffname, char_u **sfname)
char_ualist_name (aentry_T *aep)
void do_arg_all (int count, int forceit, int keep_tabs)
bool bt_prompt (buf_T *buf)
void ex_buffer_all (exarg_T *eap)
void do_modelines (int flags)
bool bt_help (const buf_T *const buf) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT
bool bt_normal (const buf_T *const buf) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT
bool bt_quickfix (const buf_T *const buf) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT
bool bt_terminal (const buf_T *const buf) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT
bool bt_nofile (const buf_T *const buf) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT
bool bt_dontwrite (const buf_T *const buf) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT
bool bt_dontwrite_msg (const buf_T *const buf) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT
bool buf_hide (const buf_T *const buf)
char_ubuf_spname (buf_T *buf)
bool find_win_for_buf (buf_T *buf, win_T **wp, tabpage_T **tp)
int buf_signcols (buf_T *buf)
void set_buflisted (int on)
bool buf_contents_changed (buf_T *buf) FUNC_ATTR_NONNULL_ALL
void wipe_buffer (buf_T *buf, bool aucmd)
void buf_open_scratch (handle_T bufnr, char *bufname)

Macro Definition Documentation


#define MAX_STL_EVAL_DEPTH   100


#define SPACE_FOR_ARGNR   (sizeof(buf) - 10)


#define SPACE_FOR_DIR   (sizeof(buf) - 20)


#define SPACE_FOR_FNAME   (sizeof(buf) - 100)


#define TMPLEN   70

Enumeration Type Documentation

◆ BufFreeFlags


◆ NumberBase

enum NumberBase

Enumeration specifying the valid numeric bases that can be used when printing numbers in the status line.


Function Documentation

◆ alist_name()

char_u* alist_name ( aentry_T aep)

◆ bt_dontwrite()

bool bt_dontwrite ( const buf_T *const  buf)

◆ bt_dontwrite_msg()

bool bt_dontwrite_msg ( const buf_T *const  buf)

◆ bt_help()

bool bt_help ( const buf_T *const  buf)

◆ bt_nofile()

bool bt_nofile ( const buf_T *const  buf)

◆ bt_normal()

bool bt_normal ( const buf_T *const  buf)

◆ bt_prompt()

bool bt_prompt ( buf_T buf)
true if "buf" is a prompt buffer.

◆ bt_quickfix()

bool bt_quickfix ( const buf_T *const  buf)

◆ bt_terminal()

bool bt_terminal ( const buf_T *const  buf)

◆ buf_clear()

void buf_clear ( void  )

Clears the current buffer contents.

◆ buf_clear_file()

void buf_clear_file ( buf_T buf)

Make buffer not contain a file.

◆ buf_contents_changed()

bool buf_contents_changed ( buf_T buf)

Read the file for "buf" again and check if the contents changed. Return true if it changed or this could not be checked.

bufbuffer to check
true if the buffer's contents have changed

◆ buf_freeall()

void buf_freeall ( buf_T buf,
int  flags 

buf_freeall() - free all things allocated for a buffer that are related to the file. Careful: get here with "curwin" NULL when exiting.

flagsBFA_DEL buffer is going to be deleted BFA_WIPE buffer is going to be wiped out BFA_KEEP_UNDO do not free undo information

◆ buf_get_fname()

char_u* buf_get_fname ( const buf_T buf)

◆ buf_hide()

bool buf_hide ( const buf_T *const  buf)

◆ buf_name_changed()

void buf_name_changed ( buf_T buf)

◆ buf_open_scratch()

void buf_open_scratch ( handle_T  bufnr,
char *  bufname 

Creates or switches to a scratch buffer. :h special-buffers Scratch buffer is:

  • buftype=nofile bufhidden=hide noswapfile
  • Always considered 'nomodified'
bufnrBuffer to switch to, or 0 to create a new buffer.
See also

◆ buf_set_file_id()

void buf_set_file_id ( buf_T buf)

◆ buf_set_name()

void buf_set_name ( int  fnum,
char_u name 

◆ buf_signcols()

int buf_signcols ( buf_T buf)

◆ buf_spname()

char_u* buf_spname ( buf_T buf)

◆ buf_valid()

bool buf_valid ( buf_T buf)

Check that "buf" points to a valid buffer in the buffer list.

Can be slow if there are many buffers, prefer using bufref_valid().

bufThe buffer to check for.

◆ buflist_add()

int buflist_add ( char_u fname,
int  flags 

◆ buflist_altfpos()

void buflist_altfpos ( win_T win)

◆ buflist_findfpos()

pos_T* buflist_findfpos ( buf_T buf)

◆ buflist_findlnum()

linenr_T buflist_findlnum ( buf_T buf)

◆ buflist_findname()

buf_T* buflist_findname ( char_u ffname)

◆ buflist_findname_exp()

buf_T* buflist_findname_exp ( char_u fname)

◆ buflist_findnr()

buf_T* buflist_findnr ( int  nr)

Find a file in the buffer list by buffer number.

◆ buflist_findpat()

int buflist_findpat ( const char_u pattern,
const char_u pattern_end,
bool  unlisted,
bool  diffmode,
bool  curtab_only 

Find file in buffer list by a regexp pattern. Return fnum of the found buffer. Return < 0 for error.

pattern_endpointer to first char after pattern
unlistedfind unlisted buffers
diffmodefind diff-mode buffers only
curtab_onlyfind buffers in current tab only

◆ buflist_getfile()

int buflist_getfile ( int  n,
linenr_T  lnum,
int  options,
int  forceit 

Get alternate file "n". Set linenr to "lnum" or altfpos.lnum if "lnum" == 0. Also set cursor column to altfpos.col if 'startofline' is not set. if (options & GETF_SETMARK) call setpcmark() if (options & GETF_ALT) we are jumping to an alternate file. if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping

Return FAIL for failure, OK for success.

◆ buflist_getfpos()

void buflist_getfpos ( void  )

◆ buflist_list()

void buflist_list ( exarg_T eap)

◆ buflist_name_nr()

int buflist_name_nr ( int  fnum,
char_u **  fname,
linenr_T lnum 

◆ buflist_new()

buf_T* buflist_new ( char_u ffname_arg,
char_u sfname_arg,
linenr_T  lnum,
int  flags 

Add a file name to the buffer list. If the same file name already exists return a pointer to that buffer. If it does not exist, or if fname == NULL, a new entry is created. If (flags & BLN_CURBUF) is true, may use current buffer. If (flags & BLN_LISTED) is true, add new buffer to buffer list. If (flags & BLN_DUMMY) is true, don't count it as a real buffer. If (flags & BLN_NEW) is true, don't use an existing buffer. If (flags & BLN_NOOPT) is true, don't copy options from the current buffer if the buffer already exists. This is the ONLY way to create a new buffer.

ffname_argfull path of fname or relative
sfname_argshort fname or NULL
lnumpreferred cursor line
flagsBLN_ defines
pointer to the buffer

◆ buflist_nr2name()

char_u* buflist_nr2name ( int  n,
int  fullname,
int  helptail 

Get name of file 'n' in the buffer list. When the file has no name an empty string is returned. home_replace() is used to shorten the file name (used for marks).

helptailfor help buffers return tail only
a pointer to allocated memory, of NULL when failed.

◆ buflist_setfpos()

void buflist_setfpos ( buf_T *const  buf,
win_T *const  win,
linenr_T  lnum,
colnr_T  col,
bool  copy_options 

Set the line and column numbers for the given buffer and window

[in,out]bufBuffer for which line and column are set.
[in,out]winWindow for which line and column are set. May be NULL when using :badd.
[in]lnumLine number to be set. If it is zero then only options are touched.
[in]colColumn number to be set.
[in]copy_optionsIf true save the local window option values.

◆ bufref_valid()

bool bufref_valid ( bufref_T bufref)

Return true if "bufref->br_buf" points to the same buffer as when set_bufref() was called and it is a valid buffer. Only goes through the buffer list if buf_free_count changed. Also checks if b_fnum is still the same, a :bwipe followed by :new might get the same allocated memory, but it's a different buffer.

bufrefBuffer reference to check for.

◆ build_stl_str_hl()

int build_stl_str_hl ( win_T wp,
char_u out,
size_t  outlen,
char_u fmt,
int  use_sandbox,
char_u  fillchar,
int  maxwidth,
stl_hlrec_t **  hltab,
StlClickRecord **  tabtab 

Build a string from the status line items in "fmt". Return length of string in screen cells.

Normally works for window "wp", except when working for 'tabline' then it is "curwin".

Items are drawn interspersed with the text that surrounds it Specials: %-<wid>(xxx%) => group, %= => separation marker, %< => truncation Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional

If maxwidth is not zero, the string will be filled at any middle marker or truncated if too long, fillchar is used for all whitespace.

wpThe window to build a statusline for
outThe output buffer to write the statusline to Note: This should not be NameBuff
outlenThe length of the output buffer
fmtThe statusline format string
use_sandboxUse a sandboxed environment when evaluating fmt
fillcharCharacter to use when filling empty space in the statusline
maxwidthThe maximum width to make the statusline
hltabHL attributes (can be NULL)
tabtabTab clicks definition (can be NULL).
The final width of the statusline

◆ close_buffer()

bool close_buffer ( win_T win,
buf_T buf,
int  action,
bool  abort_if_last 

Close the link to a buffer.

winIf not NULL, set b_last_cursor.
actionUsed when there is no longer a window for the buffer. Possible values: 0 buffer becomes hidden DOBUF_UNLOAD buffer is unloaded DOBUF_DELETE buffer is unloaded and removed from buffer list DOBUF_WIPE buffer is unloaded and really deleted When doing all but the first one on the current buffer, the caller should get a new buffer very soon! The 'bufhidden' option can force freeing and deleting.
abort_if_lastIf true, do not close the buffer if autocommands cause there to be only one window with this buffer. e.g. when ":quit" is supposed to close the window but autocommands close all other windows.
true when we got to the end and b_nwindows was decremented.

◆ col_print()

void col_print ( char_u buf,
size_t  buflen,
int  col,
int  vcol 

◆ curbuf_reusable()

bool curbuf_reusable ( void  )

Return true if the current buffer is empty, unnamed, unmodified and used in only one window. That means it can be reused.

◆ do_arg_all()

void do_arg_all ( int  count,
int  forceit,
int  keep_tabs 

do_arg_all(): Open up to 'count' windows, one for each argument.

forceithide buffers in current windows
keep_tabskeep current tabs, for ":tab drop file"

◆ do_autochdir()

void do_autochdir ( void  )

◆ do_bufdel()

char* do_bufdel ( int  command,
char_u arg,
int  addr_count,
int  start_bnr,
int  end_bnr,
int  forceit 

do_bufdel() - delete or unload buffer(s)

addr_count == 0: ":bdel" - delete current buffer addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete buffer "end_bnr", then any other arguments. addr_count == 2: ":N,N bdel" - delete buffers in range

command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or DOBUF_DEL (":bdel")

argpointer to extra arguments
start_bnrfirst buffer number in a range
end_bnrbuffer nr or last buffer nr in a range
error message or NULL

◆ do_buffer()

int do_buffer ( int  action,
int  start,
int  dir,
int  count,
int  forceit 

Implementation of the commands for the buffer list.

action == DOBUF_GOTO go to specified buffer action == DOBUF_SPLIT split window and go to specified buffer action == DOBUF_UNLOAD unload specified buffer(s) action == DOBUF_DEL delete specified buffer(s) from buffer list action == DOBUF_WIPE delete specified buffer(s) really

start == DOBUF_CURRENT go to "count" buffer from current buffer start == DOBUF_FIRST go to "count" buffer from first buffer start == DOBUF_LAST go to "count" buffer from last buffer start == DOBUF_MOD go to "count" modified buffer from current buffer

countbuffer number or number of buffers
forceittrue for :...!

◆ do_modelines()

void do_modelines ( int  flags)

◆ enter_buffer()

void enter_buffer ( buf_T buf)

◆ ex_buffer_all()

void ex_buffer_all ( exarg_T eap)

◆ ExpandBufnames()

int ExpandBufnames ( char_u pat,
int *  num_file,
char_u ***  file,
int  options 

◆ fileinfo()

void fileinfo ( int  fullname,
int  shorthelp,
int  dont_truncate 

Print info about the current buffer.

fullnamewhen non-zero print full path

◆ find_win_for_buf()

bool find_win_for_buf ( buf_T buf,
win_T **  wp,
tabpage_T **  tp 

Find a window for buffer "buf". If found true is returned and "wp" and "tp" are set to the window and tabpage. If not found, false is returned.

bufbuffer to find a window for
[out]wpstores the found window
[out]tpstores the found tabpage
true if a window was found for the buffer.

◆ fname_expand()

void fname_expand ( buf_T buf,
char_u **  ffname,
char_u **  sfname 

◆ free_buf_options()

void free_buf_options ( buf_T buf,
int  free_p_ff 

◆ get_rel_pos()

void get_rel_pos ( win_T wp,
char_u buf,
int  buflen 

◆ get_winopts()

void get_winopts ( buf_T buf)

◆ getaltfname()

char_u* getaltfname ( bool  errmsg)

Get alternate file name for current window. Return NULL if there isn't any, and give error message if requested.

errmsggive error message

◆ goto_buffer()

void goto_buffer ( exarg_T eap,
int  start,
int  dir,
int  count 

◆ handle_swap_exists()

void handle_swap_exists ( bufref_T old_curbuf)

Handle the situation of swap_exists_action being set.

It is allowed for "old_curbuf" to be NULL or invalid.

old_curbufThe buffer to check for.

◆ maketitle()

void maketitle ( void  )

◆ no_write_message()

void no_write_message ( void  )

◆ no_write_message_nobang()

void no_write_message_nobang ( const buf_T *const  buf)

◆ open_buffer()

int open_buffer ( int  read_stdin,
exarg_T eap,
int  flags 

Open current buffer, that is: open the memfile and read the file into memory.

read_stdinread file from stdin
eapfor forced 'ff' and 'fenc' or NULL
flagsextra flags for readfile()
FAIL for failure, OK otherwise.

◆ otherfile()

bool otherfile ( char_u ffname)

Check that "ffname" is not the same file as current file. Fname must have a full path (expanded by path_to_absolute()).

ffnamefull path name to check

◆ resettitle()

void resettitle ( void  )

Set current window title.

◆ set_buflisted()

void set_buflisted ( int  on)

◆ set_bufref()

void set_bufref ( bufref_T bufref,
buf_T buf 

Store "buf" in "bufref" and set the free count.

bufrefReference to be used for the buffer.
bufThe buffer to reference.

◆ set_curbuf()

void set_curbuf ( buf_T buf,
int  action 

◆ setaltfname()

buf_T* setaltfname ( char_u ffname,
char_u sfname,
linenr_T  lnum 

◆ setfname()

int setfname ( buf_T buf,
char_u ffname_arg,
char_u sfname_arg,
bool  message 

Set the file name for "buf" to "ffname_arg", short file name to "sfname_arg". The file name with the full path is also remembered, for when :cd is used.

messagegive message when buffer already exists
FAIL for failure (file name already in use by other buffer) OK otherwise.

◆ wipe_buffer()

void wipe_buffer ( buf_T buf,
bool  aucmd 

Wipe out a buffer and decrement the last buffer number if it was used for this buffer. Call this to wipe out a temp buffer that does not contain any marks.

aucmdWhen true trigger autocommands.