Macros | Enumerations | Functions
buffer.c File Reference
#include <stdbool.h>
#include <string.h>
#include <inttypes.h>
#include <assert.h>
#include "nvim/api/private/handle.h"
#include "nvim/api/private/helpers.h"
#include "nvim/api/vim.h"
#include "nvim/ascii.h"
#include "nvim/vim.h"
#include "nvim/buffer.h"
#include "nvim/charset.h"
#include "nvim/cursor.h"
#include "nvim/diff.h"
#include "nvim/digraph.h"
#include "nvim/eval.h"
#include "nvim/ex_cmds2.h"
#include "nvim/ex_cmds.h"
#include "nvim/ex_docmd.h"
#include "nvim/ex_eval.h"
#include "nvim/ex_getln.h"
#include "nvim/fileio.h"
#include "nvim/file_search.h"
#include "nvim/fold.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/memline.h"
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/misc1.h"
#include "nvim/garray.h"
#include "nvim/move.h"
#include "nvim/option.h"
#include "nvim/os_unix.h"
#include "nvim/path.h"
#include "nvim/quickfix.h"
#include "nvim/regexp.h"
#include "nvim/screen.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/window.h"
#include "nvim/shada.h"
#include "nvim/os/os.h"
#include "nvim/os/time.h"
#include "nvim/os/input.h"
#include "nvim/buffer_updates.h"

Macros

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

Enumerations

enum  BufhlLineStatus { kBLSUnchanged = 0, kBLSChanged = 1, kBLSDeleted = 2 }
 
enum  NumberBase { kNumBaseDecimal = 10, kNumBaseHexadecimal = 16 }
 

Functions

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 buf_valid (buf_T *buf) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT
 
void close_buffer (win_T *win, buf_T *buf, int action, int abort_if_last)
 
void buf_clear_file (buf_T *buf)
 
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_udo_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)
 
buf_Tbuflist_new (char_u *ffname, char_u *sfname, linenr_T lnum, int flags)
 
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, int unlisted, int diffmode, int curtab_only)
 
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_ALL
 
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, char_u *sfname, int 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)
 
bool otherfile (char_u *ffname) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL
 
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, struct stl_hlrec *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)
 
void ex_buffer_all (exarg_T *eap)
 
void do_modelines (int flags)
 
bool bt_help (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)
 
void buf_addsign (buf_T *buf, int id, linenr_T lnum, int typenr)
 
linenr_T buf_change_sign_type (buf_T *buf, int markId, int typenr)
 
int buf_getsigntype (buf_T *buf, linenr_T lnum, SignType type)
 
linenr_T buf_delsign (buf_T *buf, int id)
 
int buf_findsign (buf_T *buf, int id)
 
int buf_findsign_id (buf_T *buf, linenr_T lnum)
 
void buf_delete_signs (buf_T *buf)
 
void buf_delete_all_signs (void)
 
void sign_list_placed (buf_T *rbuf)
 
void sign_mark_adjust (linenr_T line1, linenr_T line2, long amount, long amount_after)
 
BufhlLinebufhl_tree_ref (BufhlInfo *b, linenr_T line, bool put)
 
int bufhl_add_hl (buf_T *buf, int src_id, int hl_id, linenr_T lnum, colnr_T col_start, colnr_T col_end)
 
void bufhl_add_hl_pos_offset (buf_T *buf, int src_id, int hl_id, lpos_T pos_start, lpos_T pos_end, colnr_T offset)
 
int bufhl_add_virt_text (buf_T *buf, int src_id, linenr_T lnum, VirtText virt_text)
 
void bufhl_clear_line_range (buf_T *buf, int src_id, linenr_T line_start, linenr_T line_end)
 
void bufhl_clear_all (buf_T *buf)
 Remove all highlights and free the highlight data. More...
 
void bufhl_mark_adjust (buf_T *buf, linenr_T line1, linenr_T line2, long amount, long amount_after, bool end_temp)
 Adjust a placed highlight for inserted/deleted lines. More...
 
bool bufhl_start_line (buf_T *buf, linenr_T lnum, BufhlLineInfo *info)
 
int bufhl_get_attr (BufhlLineInfo *info, colnr_T col)
 
void set_buflisted (int on)
 
bool buf_contents_changed (buf_T *buf) FUNC_ATTR_NONNULL_ALL
 
void wipe_buffer (buf_T *buf, int aucmd)
 
void buf_open_scratch (handle_T bufnr, char *bufname)
 

Macro Definition Documentation

#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

Enumerator
kBLSUnchanged 
kBLSChanged 
kBLSDeleted 
enum NumberBase

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

Enumerator
kNumBaseDecimal 
kNumBaseHexadecimal 

Function Documentation

char_u* alist_name ( aentry_T aep)
bool bt_help ( const buf_T *const  buf)
void buf_addsign ( buf_T buf,
int  id,
linenr_T  lnum,
int  typenr 
)
linenr_T buf_change_sign_type ( buf_T buf,
int  markId,
int  typenr 
)
void buf_clear ( void  )

Clears the current buffer contents.

void buf_clear_file ( buf_T buf)
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.

Parameters
bufbuffer to check
Returns
true if the buffer's contents have changed
void buf_delete_all_signs ( void  )
void buf_delete_signs ( buf_T buf)
linenr_T buf_delsign ( buf_T buf,
int  id 
)
int buf_findsign ( buf_T buf,
int  id 
)
int buf_findsign_id ( buf_T buf,
linenr_T  lnum 
)
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.

Parameters
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
int buf_getsigntype ( buf_T buf,
linenr_T  lnum,
SignType  type 
)

Gets a sign from a given line. In case of multiple signs, returns the most recently placed one.

Parameters
bufBuffer in which to search
lnumLine in which to search
typeType of sign to look for
Returns
Identifier of the first matching sign, or 0
void buf_name_changed ( buf_T buf)
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'
Parameters
bufnrBuffer to switch to, or 0 to create a new buffer.
See also
curbufIsChanged()
void buf_set_file_id ( buf_T buf)
void buf_set_name ( int  fnum,
char_u name 
)
char_u* buf_spname ( buf_T buf)
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().

Parameters
bufThe buffer to check for.
int bufhl_add_hl ( buf_T buf,
int  src_id,
int  hl_id,
linenr_T  lnum,
colnr_T  col_start,
colnr_T  col_end 
)

Adds a highlight to buffer.

Unlike matchaddpos() highlights follow changes to line numbering (as lines are inserted/removed above the highlighted line), like signs and marks do.

When called with "src_id" set to 0, a unique source id is generated and returned. Succesive calls can pass it in as "src_id" to add new highlights to the same source group. All highlights in the same group can be cleared at once. If the highlight never will be manually deleted pass in -1 for "src_id"

if "hl_id" or "lnum" is invalid no highlight is added, but a new src_id is still returned.

Parameters
bufThe buffer to add highlights to
src_idsrc_id to use or 0 to use a new src_id group, or -1 for ungrouped highlight.
hl_idId of the highlight group to use
lnumThe line to highlight
col_startFirst column to highlight
col_endThe last column to highlight, or -1 to highlight to end of line
Returns
The src_id that was used
void bufhl_add_hl_pos_offset ( buf_T buf,
int  src_id,
int  hl_id,
lpos_T  pos_start,
lpos_T  pos_end,
colnr_T  offset 
)

Add highlighting to a buffer, bounded by two cursor positions, with an offset.

Parameters
bufBuffer to add highlights to
src_idsrc_id to use or 0 to use a new src_id group, or -1 for ungrouped highlight.
hl_idHighlight group id
pos_startCursor position to start the hightlighting at
pos_endCursor position to end the highlighting at
offsetMove the whole highlighting this many columns to the right
int bufhl_add_virt_text ( buf_T buf,
int  src_id,
linenr_T  lnum,
VirtText  virt_text 
)
void bufhl_clear_all ( buf_T buf)

Remove all highlights and free the highlight data.

void bufhl_clear_line_range ( buf_T buf,
int  src_id,
linenr_T  line_start,
linenr_T  line_end 
)

Clear bufhl highlights from a given source group and range of lines.

Parameters
bufThe buffer to remove highlights from
src_idhighlight source group to clear, or -1 to clear all groups.
line_startfirst line to clear
line_endlast line to clear or MAXLNUM to clear to end of file.
int bufhl_get_attr ( BufhlLineInfo info,
colnr_T  col 
)

get highlighting at column col

It is is assumed this will be called with non-decreasing column nrs, so that it is possible to only recalculate highlights at endpoints.

Parameters
infoThe info returned by bufhl_start_line
colThe column to get the attr for
Returns
The highilight attr to display at the column
void bufhl_mark_adjust ( buf_T buf,
linenr_T  line1,
linenr_T  line2,
long  amount,
long  amount_after,
bool  end_temp 
)

Adjust a placed highlight for inserted/deleted lines.

bool bufhl_start_line ( buf_T buf,
linenr_T  lnum,
BufhlLineInfo info 
)

Get highlights to display at a specific line

Parameters
bufThe buffer handle
lnumThe line number
[out]infoThe highligts for the line
Returns
true if there was highlights to display
BufhlLine* bufhl_tree_ref ( BufhlInfo *  b,
linenr_T  line,
bool  put 
)

Get reference to line in kbtree_t

Parameters
bthe three
linethe linenumber to lookup
putif true, put a new line when not found if false, return NULL when not found
int buflist_add ( char_u fname,
int  flags 
)
void buflist_altfpos ( win_T win)
pos_T* buflist_findfpos ( buf_T buf)
linenr_T buflist_findlnum ( buf_T buf)
buf_T* buflist_findname ( char_u ffname)
buf_T* buflist_findname_exp ( char_u fname)
buf_T* buflist_findnr ( int  nr)

Find a file in the buffer list by buffer number.

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

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

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.

void buflist_getfpos ( void  )
void buflist_list ( exarg_T eap)
int buflist_name_nr ( int  fnum,
char_u **  fname,
linenr_T lnum 
)
buf_T* buflist_new ( char_u ffname,
char_u sfname,
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.

Parameters
ffnamefull path of fname or relative
sfnameshort fname or NULL
lnumpreferred cursor line
flagsBLN_ defines
bufnr
Returns
pointer to the buffer
char_u* buflist_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 
)

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

Parameters
[in,out]bufBuffer for which line and column are set.
[in,out]winWindow for which line and column are set.
[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.
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.

Parameters
bufrefBuffer reference to check for.
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,
struct stl_hlrec 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.

Parameters
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).
Returns
The final width of the statusline
void close_buffer ( win_T win,
buf_T buf,
int  action,
int  abort_if_last 
)

Close the link to a buffer.

Parameters
winIf not NULL, set b_last_cursor.
buf
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.
void col_print ( char_u buf,
size_t  buflen,
int  col,
int  vcol 
)
void do_arg_all ( int  count,
int  forceit,
int  keep_tabs 
)
void do_autochdir ( void  )
char_u* 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 do_modelines ( int  flags)
void enter_buffer ( buf_T buf)
void ex_buffer_all ( exarg_T eap)
int ExpandBufnames ( char_u pat,
int *  num_file,
char_u ***  file,
int  options 
)
void fileinfo ( int  fullname,
int  shorthelp,
int  dont_truncate 
)
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.

Parameters
bufbuffer to find a window for
[out]wpstores the found window
[out]tpstores the found tabpage
Returns
true if a window was found for the buffer.
void fname_expand ( buf_T buf,
char_u **  ffname,
char_u **  sfname 
)
void free_buf_options ( buf_T buf,
int  free_p_ff 
)
void get_rel_pos ( win_T wp,
char_u buf,
int  buflen 
)
void get_winopts ( buf_T buf)
char_u* getaltfname ( bool  errmsg)
void goto_buffer ( exarg_T eap,
int  start,
int  dir,
int  count 
)
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.

Parameters
old_curbufThe buffer to check for.
void maketitle ( void  )
int open_buffer ( int  read_stdin,
exarg_T eap,
int  flags 
)
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()).

Parameters
ffnamefull path name to check
void resettitle ( void  )

Set current window title.

void set_buflisted ( int  on)
void set_bufref ( bufref_T bufref,
buf_T buf 
)

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

Parameters
bufrefReference to be used for the buffer.
bufThe buffer to reference.
void set_curbuf ( buf_T buf,
int  action 
)
buf_T* setaltfname ( char_u ffname,
char_u sfname,
linenr_T  lnum 
)
int setfname ( buf_T buf,
char_u ffname,
char_u sfname,
int  message 
)
void sign_list_placed ( buf_T rbuf)
void sign_mark_adjust ( linenr_T  line1,
linenr_T  line2,
long  amount,
long  amount_after 
)
void wipe_buffer ( buf_T buf,
int  aucmd 
)