Data Structures | Macros | Typedefs | Functions | Variables
fileio.c File Reference
#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <string.h>
#include <inttypes.h>
#include <fcntl.h>
#include "nvim/vim.h"
#include "nvim/ascii.h"
#include "nvim/fileio.h"
#include "nvim/buffer.h"
#include "nvim/charset.h"
#include "nvim/cursor.h"
#include "nvim/diff.h"
#include "nvim/edit.h"
#include "nvim/eval.h"
#include "nvim/ex_cmds.h"
#include "nvim/ex_docmd.h"
#include "nvim/ex_eval.h"
#include "nvim/fold.h"
#include "nvim/func_attr.h"
#include "nvim/getchar.h"
#include "nvim/hashtab.h"
#include "nvim/iconv.h"
#include "nvim/mbyte.h"
#include "nvim/memfile.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/normal.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/search.h"
#include "nvim/sha256.h"
#include "nvim/state.h"
#include "nvim/strings.h"
#include "nvim/ui.h"
#include "nvim/types.h"
#include "nvim/undo.h"
#include "nvim/window.h"
#include "nvim/shada.h"
#include "nvim/os/os.h"
#include "nvim/os/os_defs.h"
#include "nvim/os/time.h"
#include "nvim/os/input.h"

Data Structures

struct  AutoCmd
struct  AutoPat
struct  AutoPatCmd
struct  bw_info


#define BUFSIZE   8192 /* size of normal write buffer */
#define SMBUFSIZE   256 /* size of emergency write buffer */
#define AUGROUP_DEFAULT   -1 /* default autocmd group */
#define AUGROUP_ERROR   -2 /* erroneous autocmd group */
#define AUGROUP_ALL   -3 /* all autocmd groups */
#define HAS_BW_FLAGS
#define FIO_LATIN1   0x01 /* convert Latin1 */
#define FIO_UTF8   0x02 /* convert UTF-8 */
#define FIO_UCS2   0x04 /* convert UCS-2 */
#define FIO_UCS4   0x08 /* convert UCS-4 */
#define FIO_UTF16   0x10 /* convert UTF-16 */
#define FIO_ENDIAN_L   0x80 /* little endian */
#define FIO_NOCONVERT   0x2000 /* skip encoding conversion */
#define FIO_UCSBOM   0x4000 /* check for BOM at start of file */
#define FIO_ALL   -1 /* allow all formats */
#define CONV_RESTLEN   30
#define ICONV_MULT   8
#define SET_ERRMSG_NUM(num, msg)   errnum = num, errmsg = msg, errmsgarg = 0
#define SET_ERRMSG_ARG(msg, error)   errnum = NULL, errmsg = msg, errmsgarg = error
#define SET_ERRMSG(msg)   errnum = NULL, errmsg = msg, errmsgarg = 0
#define AUGROUP_NAME(i)   (((char **)augroups.ga_data)[i])


typedef struct AutoCmd AutoCmd
typedef struct AutoPat AutoPat
typedef struct AutoPatCmd AutoPatCmd


void filemess (buf_T *buf, char_u *name, char_u *s, int attr)
int readfile (char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_skip, linenr_T lines_to_read, exarg_T *eap, int flags)
void prep_exarg (exarg_T *eap, buf_T *buf)
void set_file_options (int set_options, exarg_T *eap)
void set_forced_fenc (exarg_T *eap)
int buf_write (buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_T end, exarg_T *eap, int append, int forceit, int reset_changed, int filtering)
void msg_add_lines (int insert_space, long lnum, off_T nchars)
void shorten_buf_fname (buf_T *buf, char_u *dirname, int force)
void shorten_fnames (int force)
char * modname (const char *fname, const char *ext, bool prepend_dot) FUNC_ATTR_NONNULL_ARG(2)
bool vim_fgets (char_u *buf, int size, FILE *fp) FUNC_ATTR_NONNULL_ALL
int get2c (FILE *fd)
int get3c (FILE *fd)
int get4c (FILE *fd)
time_t get8ctime (FILE *fd)
char * read_string (FILE *fd, size_t cnt)
bool put_bytes (FILE *fd, uintmax_t number, size_t len)
int put_time (FILE *fd, time_t time_)
int vim_rename (const char_u *from, const char_u *to) FUNC_ATTR_NONNULL_ALL
int check_timestamps (int focus)
int buf_check_timestamp (buf_T *buf, int focus) FUNC_ATTR_NONNULL_ALL
void buf_reload (buf_T *buf, int orig_mode)
void buf_store_file_info (buf_T *buf, FileInfo *file_info) FUNC_ATTR_NONNULL_ALL
void write_lnum_adjust (linenr_T offset)
int delete_recursive (const char *name)
void vim_deltempdir (void)
 Delete the temp directory and all files it contains. More...
char_uvim_gettempdir (void)
char_uvim_tempname (void)
void aubuflocal_remove (buf_T *buf)
bool au_has_group (const char_u *name) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT
void do_augroup (char_u *arg, int del_group)
int check_ei (void)
char_uau_event_disable (char *what)
void au_event_restore (char_u *old_ei)
void do_autocmd (char_u *arg_in, int forceit)
int do_doautocmd (char_u *arg, int do_msg, bool *did_something)
void ex_doautoall (exarg_T *eap)
bool check_nomodeline (char_u **argp) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
void aucmd_prepbuf (aco_save_T *aco, buf_T *buf)
void aucmd_restbuf (aco_save_T *aco)
bool apply_autocmds (event_T event, char_u *fname, char_u *fname_io, bool force, buf_T *buf)
bool apply_autocmds_retval (event_T event, char_u *fname, char_u *fname_io, bool force, buf_T *buf, int *retval)
bool has_cursorhold (void)
bool trigger_cursorhold (void)
 Return true if the CursorHold/CursorHoldI event can be triggered. More...
bool has_event (event_T event) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT
void block_autocmds (void)
void unblock_autocmds (void)
char_ugetnextac (int c, void *cookie, int indent)
bool has_autocmd (event_T event, char_u *sfname, buf_T *buf) FUNC_ATTR_WARN_UNUSED_RESULT
char_uget_augroup_name (expand_T *xp, int idx)
char_uset_context_in_autocmd (expand_T *xp, char_u *arg, int doautocmd)
char_uget_event_name (expand_T *xp, int idx)
bool autocmd_supported (const char *const event) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT
bool au_exists (const char *const arg) FUNC_ATTR_WARN_UNUSED_RESULT
bool match_file_list (char_u *list, char_u *sfname, char_u *ffname) FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ARG(1
 if (regpat==NULL)
 xfree (regpat)
 if (match)
char_ufile_pat_to_reg_pat (const char_u *pat, const char_u *pat_end, char *allow_dirs, int no_bslash) FUNC_ATTR_NONNULL_ARG(1)


bool char_utail
char_uregpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, false)
char allow_dirs
bool match = match_file_pat(regpat, NULL, ffname, sfname, tail, (int)allow_dirs)
char_up = list
return false

Macro Definition Documentation

#define AUGROUP_ALL   -3 /* all autocmd groups */
#define AUGROUP_DEFAULT   -1 /* default autocmd group */
#define AUGROUP_ERROR   -2 /* erroneous autocmd group */
#define AUGROUP_NAME (   i)    (((char **)augroups.ga_data)[i])
#define BUFSIZE   8192 /* size of normal write buffer */
#define CONV_RESTLEN   30
#define FIO_ALL   -1 /* allow all formats */
#define FIO_ENDIAN_L   0x80 /* little endian */
#define FIO_LATIN1   0x01 /* convert Latin1 */
#define FIO_NOCONVERT   0x2000 /* skip encoding conversion */
#define FIO_UCS2   0x04 /* convert UCS-2 */
#define FIO_UCS4   0x08 /* convert UCS-4 */
#define FIO_UCSBOM   0x4000 /* check for BOM at start of file */
#define FIO_UTF16   0x10 /* convert UTF-16 */
#define FIO_UTF8   0x02 /* convert UTF-8 */
#define HAS_BW_FLAGS
#define ICONV_MULT   8
#define SET_ERRMSG (   msg)    errnum = NULL, errmsg = msg, errmsgarg = 0
#define SET_ERRMSG_ARG (   msg,
)    errnum = NULL, errmsg = msg, errmsgarg = error
#define SET_ERRMSG_NUM (   num,
)    errnum = num, errmsg = msg, errmsgarg = 0
#define SMBUFSIZE   256 /* size of emergency write buffer */

Typedef Documentation

typedef struct AutoCmd AutoCmd
typedef struct AutoPat AutoPat
typedef struct AutoPatCmd AutoPatCmd

Function Documentation

bool apply_autocmds ( event_T  event,
char_u fname,
char_u fname_io,
bool  force,
buf_T buf 

Execute autocommands for "event" and file name "fname".

eventevent that occured
fnamefilename, NULL or empty means use actual file name
fname_iofilename to use for <afile> on cmdline
forceWhen true, ignore autocmd_busy
bufBuffer for <abuf>
true if some commands were executed.
bool apply_autocmds_retval ( event_T  event,
char_u fname,
char_u fname_io,
bool  force,
buf_T buf,
int *  retval 

Like apply_autocmds(), but handles the caller's retval. If the script processing is being aborted or if retval is FAIL when inside a try conditional, no autocommands are executed. If otherwise the autocommands cause the script to be aborted, retval is set to FAIL.

eventevent that occured
fnameNULL or empty means use actual file name
fname_iofname to use for <afile> on cmdline
forceWhen true, ignore autocmd_busy
bufBuffer for <abuf>
[in,out]retvalcaller's retval
true if some autocommands were executed
char_u* au_event_disable ( char *  what)
void au_event_restore ( char_u old_ei)
bool au_exists ( const char *const  arg)

Return true if an autocommand is defined for a group, event and pattern: The group can be omitted to accept any group. event and pattern can be omitted to accept any event and pattern. Buffer-local patterns <buffer> or <buffer=N> are accepted. Used for: exists("#Group") or exists("#Group#Event") or exists("#Group#Event#pat") or exists("#Event") or exists("#Event#pat")

argautocommand string
bool au_has_group ( const char_u name)

Return true if augroup "name" exists.

nameaugroup name
void aubuflocal_remove ( buf_T buf)
void aucmd_prepbuf ( aco_save_T aco,
buf_T buf 
void aucmd_restbuf ( aco_save_T aco)

Cleanup after executing autocommands for a (hidden) buffer. Restore the window as it was (if possible).

acostructure holding saved values
bool autocmd_supported ( const char *const  event)

Check whether given autocommand is supported

[in]eventEvent to check.
True if it is, false otherwise.
void block_autocmds ( void  )
int buf_check_timestamp ( buf_T buf,
int  focus 
void buf_reload ( buf_T buf,
int  orig_mode 
void buf_store_file_info ( buf_T buf,
FileInfo file_info 
int buf_write ( buf_T buf,
char_u fname,
char_u sfname,
linenr_T  start,
linenr_T  end,
exarg_T eap,
int  append,
int  forceit,
int  reset_changed,
int  filtering 
int check_ei ( void  )
bool check_nomodeline ( char_u **  argp)

Check *argp for <nomodeline>. When it is present return false, otherwise return true and advance *argp to after it. Thus do_modelines() should be called when true is returned.

[in,out]argpargument string
int check_timestamps ( int  focus)
int delete_recursive ( const char *  name)

Delete "name" and everything in it, recursively.

nameThe path which should be deleted.
0 for success, -1 if some file was not deleted.
void do_augroup ( char_u arg,
int  del_group 
void do_autocmd ( char_u arg_in,
int  forceit 
int do_doautocmd ( char_u arg,
int  do_msg,
bool *  did_something 
void ex_doautoall ( exarg_T eap)
char_u* file_pat_to_reg_pat ( const char_u pat,
const char_u pat_end,
char *  allow_dirs,
int  no_bslash 

Convert the given pattern "pat" which has shell style wildcards in it, into a regular expression, and return the result in allocated memory. If there is a directory path separator to be matched, then TRUE is put in allow_dirs, otherwise FALSE is put there – webb. Handle backslashes before special characters, like "\*" and "\ ".

Returns NULL on failure.

void filemess ( buf_T buf,
char_u name,
char_u s,
int  attr 
int get2c ( FILE *  fd)

Read 2 bytes from "fd" and turn them into an int, MSB first. Returns -1 when encountering EOF.

int get3c ( FILE *  fd)

Read 3 bytes from "fd" and turn them into an int, MSB first. Returns -1 when encountering EOF.

int get4c ( FILE *  fd)

Read 4 bytes from "fd" and turn them into an int, MSB first. Returns -1 when encountering EOF.

time_t get8ctime ( FILE *  fd)

Read 8 bytes from fd and turn them into a time_t, MSB first. Returns -1 when encountering EOF.

char_u* get_augroup_name ( expand_T xp,
int  idx 
char_u* get_event_name ( expand_T xp,
int  idx 
char_u* getnextac ( int  c,
void *  cookie,
int  indent 
bool has_autocmd ( event_T  event,
char_u sfname,
buf_T buf 

Return true if there is a matching autocommand for "fname". To account for buffer-local autocommands, function needs to know in which buffer the file will be opened.

eventevent that occured.
sfnamefilename the event occured in.
bufbuffer the file is open in
bool has_cursorhold ( void  )

Return true when there is a CursorHold/CursorHoldI autocommand defined for the current mode.

bool has_event ( event_T  event)

Return true if "event" autocommand is defined.

eventthe autocommand to check
if ( regpat  = NULL)
if ( match  )
bool match_file_list ( char_u list,
char_u sfname,
char_u ffname 

Check if a file matches with a pattern in "list". "list" is a comma-separated list of patterns, like 'wildignore'. "sfname" is the short file name or NULL, "ffname" the long file name.

listlist of patterns to match
sfnameshort file name
ffnamefull file name
true if there was a match
char* modname ( const char *  fname,
const char *  ext,
bool  prepend_dot 

Get new filename ended by given extension.

fnameThe original filename. If NULL, use current directory name and ext to compute new filename.
extThe extension to add to the filename. 4 chars max if prefixed with a dot, 3 otherwise.
prepend_dotIf true, prefix ext with a dot. Does nothing if ext already starts with a dot, or if fname is NULL.
[allocated] - A new filename, made up from:
  • fname + ext, if fname not NULL.
  • current dir + ext, if fname is NULL. Result is guaranteed to:
  • be ended by <ext>.
  • have a basename with at most BASENAMELEN chars: original basename is truncated if necessary.
  • be different than original: basename chars are replaced by "_" if necessary. If that can't be done because truncated value of original filename was made of all underscores, replace first "_" by "v".
  • NULL, if fname is NULL and there was a problem trying to get current directory.
void msg_add_lines ( int  insert_space,
long  lnum,
off_T  nchars 
void prep_exarg ( exarg_T eap,
buf_T buf 
bool put_bytes ( FILE *  fd,
uintmax_t  number,
size_t  len 

Writes a number to file "fd", most significant bit first, in "len" bytes.

false in case of an error.
int put_time ( FILE *  fd,
time_t  time_ 

Writes time_t to file "fd" in 8 bytes.

FAIL when the write failed.
char* read_string ( FILE *  fd,
size_t  cnt 

Reads a string of length "cnt" from "fd" into allocated memory.

pointer to the string or NULL when unable to read that many bytes.
int readfile ( char_u fname,
char_u sfname,
linenr_T  from,
linenr_T  lines_to_skip,
linenr_T  lines_to_read,
exarg_T eap,
int  flags 
char_u* set_context_in_autocmd ( expand_T xp,
char_u arg,
int  doautocmd 
void set_file_options ( int  set_options,
exarg_T eap 
void set_forced_fenc ( exarg_T eap)
void shorten_buf_fname ( buf_T buf,
char_u dirname,
int  force 

Shorten filename of a buffer. When "force" is TRUE: Use full path from now on for files currently being edited, both for file name and swap file name. Try to shorten the file names a bit, if safe to do so. When "force" is FALSE: Only try to shorten absolute file names. For buffers that have buftype "nofile" or "scratch": never change the file name.

void shorten_fnames ( int  force)
bool trigger_cursorhold ( void  )

Return true if the CursorHold/CursorHoldI event can be triggered.

void unblock_autocmds ( void  )
void vim_deltempdir ( void  )

Delete the temp directory and all files it contains.

bool vim_fgets ( char_u buf,
int  size,
FILE *  fp 

Like fgets(), but if the file line is too long, it is truncated and the rest of the line is thrown away.

[out]bufbuffer to fill
sizesize of the buffer
fpfile to read from
true for EOF or error
char_u* vim_gettempdir ( void  )

Get the name of temp directory. This directory would be created on the first call to this function.

int vim_rename ( const char_u from,
const char_u to 

os_rename() only works if both files are on the same file system, this function will (attempts to?) copy the file across if rename fails – webb

-1 for failure, 0 for success
char_u* vim_tempname ( void  )

Return a unique name that can be used for a temp file.

The temp file is NOT created.
pointer to the temp file name or NULL if Neovim can't create temporary directory for its own temporary files.
void write_lnum_adjust ( linenr_T  offset)
xfree ( regpat  )

Variable Documentation

char allow_dirs
return false
match = match_file_pat(regpat, NULL, ffname, sfname, tail, (int)allow_dirs)
while p = list
Initial value:
char_u buf[100]
char *const buf
Definition: encode.c:235
unsigned char char_u
Definition: types.h:11