Data Structures | Macros | Enumerations | Functions
ex_cmds.c File Reference
#include <assert.h>
#include <float.h>
#include <inttypes.h>
#include <math.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "nvim/api/buffer.h"
#include "nvim/api/extmark.h"
#include "nvim/api/private/defs.h"
#include "nvim/ascii.h"
#include "nvim/buffer.h"
#include "nvim/buffer_updates.h"
#include "nvim/change.h"
#include "nvim/charset.h"
#include "nvim/cursor.h"
#include "nvim/decoration.h"
#include "nvim/diff.h"
#include "nvim/digraph.h"
#include "nvim/edit.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/fileio.h"
#include "nvim/fold.h"
#include "nvim/garray.h"
#include "nvim/getchar.h"
#include "nvim/highlight.h"
#include "nvim/highlight_group.h"
#include "nvim/indent.h"
#include "nvim/input.h"
#include "nvim/log.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/mouse.h"
#include "nvim/move.h"
#include "nvim/normal.h"
#include "nvim/ops.h"
#include "nvim/option.h"
#include "nvim/os/input.h"
#include "nvim/os/os.h"
#include "nvim/os/shell.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/search.h"
#include "nvim/spell.h"
#include "nvim/strings.h"
#include "nvim/syntax.h"
#include "nvim/tag.h"
#include "nvim/ui.h"
#include "nvim/undo.h"
#include "nvim/vim.h"
#include "nvim/window.h"

Data Structures

struct  subflags_T
 Flags kept between calls to :substitute. More...
 
struct  SubResult
 
struct  PreviewLines
 
struct  sorti_T
 Struct to store info to be sorted. More...
 

Macros

#define SPACE_FOR_DESC   (1 + 1 + 1 + MB_MAXBYTES + 16 + 4 + 3 + 3 + 1)
 
#define ADJUST_SUB_FIRSTLNUM()
 
#define PUSH_PREVIEW_LINES()
 

Enumerations

enum  SubIgnoreType { kSubHonorOptions = 0, kSubIgnoreCase, kSubMatchCase }
 Case matching style to use for :substitute. More...
 

Functions

void do_ascii (const exarg_T *const eap)
 ":ascii" and "ga" implementation More...
 
void ex_align (exarg_T *eap)
 ":left", ":center" and ":right": align text. More...
 
void ex_sort (exarg_T *eap)
 ":sort". More...
 
void ex_retab (exarg_T *eap)
 ":retab". More...
 
int do_move (linenr_T line1, linenr_T line2, linenr_T dest)
 
void ex_copy (linenr_T line1, linenr_T line2, linenr_T n)
 ":copy" More...
 
void do_bang (int addr_count, exarg_T *eap, bool forceit, bool do_in, bool do_out) FUNC_ATTR_NONNULL_ALL
 
void do_shell (char *cmd, int flags)
 
char * make_filter_cmd (char *cmd, char *itmp, char *otmp)
 
void append_redir (char *const buf, const size_t buflen, const char *const opt, const char *const fname)
 
void print_line_no_prefix (linenr_T lnum, int use_number, int list)
 
void print_line (linenr_T lnum, int use_number, int list)
 Print a text line. Also in silent mode ("ex -s"). More...
 
int rename_buffer (char *new_fname)
 
void ex_file (exarg_T *eap)
 ":file[!] [fname]". More...
 
void ex_update (exarg_T *eap)
 ":update". More...
 
void ex_write (exarg_T *eap)
 ":write" and ":saveas". More...
 
int do_write (exarg_T *eap)
 
int check_overwrite (exarg_T *eap, buf_T *buf, char *fname, char *ffname, int other)
 
void ex_wnext (exarg_T *eap)
 Handle ":wnext", ":wNext" and ":wprevious" commands. More...
 
void do_wqall (exarg_T *eap)
 ":wall", ":wqall" and ":xall": Write all changed files (and exit). More...
 
bool not_writing (void)
 
int getfile (int fnum, char *ffname_arg, char *sfname_arg, int setpm, linenr_T lnum, int forceit)
 
int do_ecmd (int fnum, char *ffname, char *sfname, exarg_T *eap, linenr_T newlnum, int flags, win_T *oldwin)
 
void ex_append (exarg_T *eap)
 ":insert" and ":append", also used by ":change" More...
 
void ex_change (exarg_T *eap)
 ":change" More...
 
void ex_z (exarg_T *eap)
 
bool check_secure (void)
 
void sub_get_replacement (SubReplacementString *const ret_sub) FUNC_ATTR_NONNULL_ALL
 
void sub_set_replacement (SubReplacementString sub)
 
bool do_sub_msg (bool count_only)
 
void ex_global (exarg_T *eap)
 
void global_exe (char *cmd)
 Execute cmd on lines marked with ml_setmarked(). More...
 
bool prepare_tagpreview (bool undo_sync)
 
void ex_help (exarg_T *eap)
 ":help": open a read-only window on a help file More...
 
char * check_help_lang (char *arg)
 
int help_heuristic (char *matched_string, int offset, int wrong_case) FUNC_ATTR_PURE
 
int find_help_tags (const char *arg, int *num_matches, char ***matches, bool keep_lang)
 
void fix_help_buffer (void)
 
void ex_exusage (exarg_T *eap)
 ":exusage" More...
 
void ex_viusage (exarg_T *eap)
 ":viusage" More...
 
void ex_helptags (exarg_T *eap)
 ":helptags" More...
 
void ex_helpclose (exarg_T *eap)
 ":helpclose": Close one help window More...
 
int sub_preview_win (buf_T *preview_buf)
 
void close_preview_windows (void)
 Closes any open windows for inccommand preview buffer. More...
 
void ex_substitute (exarg_T *eap)
 
char * skip_vimgrep_pat (char *p, char **s, int *flags)
 
void ex_oldfiles (exarg_T *eap)
 List v:oldfiles in a nice way. More...
 

Macro Definition Documentation

◆ ADJUST_SUB_FIRSTLNUM

#define ADJUST_SUB_FIRSTLNUM ( )
Value:
do { \
/* For a multi-line match, make a copy of the last matched */ \
/* line and continue in that one. */ \
if (nmatch > 1) { \
sub_firstlnum += nmatch - 1; \
xfree(sub_firstline); \
sub_firstline = (char *)vim_strsave(ml_get(sub_firstlnum)); \
/* When going beyond the last line, stop substituting. */ \
if (sub_firstlnum <= line2) { \
do_again = true; \
} else { \
subflags.do_all = false; \
} \
} \
if (skip_match) { \
/* Already hit end of the buffer, sub_firstlnum is one */ \
/* less than what it ought to be. */ \
xfree(sub_firstline); \
sub_firstline = xstrdup(""); \
copycol = 0; \
} \
} while (0)

◆ PUSH_PREVIEW_LINES

#define PUSH_PREVIEW_LINES ( )
Value:
do { \
if (preview) { \
linenr_T match_lines = current_match.end.lnum \
- current_match.start.lnum +1; \
if (preview_lines.subresults.size > 0) { \
linenr_T last = kv_last(preview_lines.subresults).end.lnum; \
if (last == current_match.start.lnum) { \
preview_lines.lines_needed += match_lines - 1; \
} else { \
preview_lines.lines_needed += match_lines; \
} \
} else { \
preview_lines.lines_needed += match_lines; \
} \
kv_push(preview_lines.subresults, current_match); \
} \
} while (0)

◆ SPACE_FOR_DESC

#define SPACE_FOR_DESC   (1 + 1 + 1 + MB_MAXBYTES + 16 + 4 + 3 + 3 + 1)

Enumeration Type Documentation

◆ SubIgnoreType

Case matching style to use for :substitute.

Enumerator
kSubHonorOptions 

Honor the user's 'ignorecase'/'smartcase' options.

kSubIgnoreCase 

Ignore case of the search.

kSubMatchCase 

Match case of the search.

Function Documentation

◆ append_redir()

void append_redir ( char *const  buf,
const size_t  buflen,
const char *const  opt,
const char *const  fname 
)

Append output redirection for the given file to the end of the buffer

Parameters
[out]bufBuffer to append to.
[in]buflenBuffer length.
[in]optSeparator or format string to append: will append ‘printf(’ ' . opt, fname)ifsis found inoptor a space, opt, a space and then fname ifs` is not found there.
[in]fnameFile name to append.

◆ check_help_lang()

char* check_help_lang ( char *  arg)

In an argument search for a language specifiers in the form "@xx". Changes the "@" to NUL if found, and returns a pointer to "xx".

Returns
NULL if not found.

◆ check_overwrite()

int check_overwrite ( exarg_T eap,
buf_T buf,
char *  fname,
char *  ffname,
int  other 
)

Check if it is allowed to overwrite a file. If b_flags has BF_NOTEDITED, BF_NEW or BF_READERR, check for overwriting current file. May set eap->forceit if a dialog says it's OK to overwrite.

Parameters
fnamefile name to be used (can differ from buf->ffname)
ffnamefull path version of fname
otherwriting under other name
Returns
OK if it's OK, FAIL if it is not.

◆ check_secure()

bool check_secure ( void  )
Returns
true if the secure flag is set (.exrc or .vimrc in current directory) and also give an error message. Otherwise, return false.

◆ close_preview_windows()

void close_preview_windows ( void  )

Closes any open windows for inccommand preview buffer.

◆ do_ascii()

void do_ascii ( const exarg_T *const  eap)

":ascii" and "ga" implementation

◆ do_bang()

void do_bang ( int  addr_count,
exarg_T eap,
bool  forceit,
bool  do_in,
bool  do_out 
)

Handle the ":!cmd" command. Also for ":r !cmd" and ":w !cmd" Bangs in the argument are replaced with the previously entered command. Remember the argument.

◆ do_ecmd()

int do_ecmd ( int  fnum,
char *  ffname,
char *  sfname,
exarg_T eap,
linenr_T  newlnum,
int  flags,
win_T oldwin 
)

start editing a new file

Parameters
fnumfile number; if zero use ffname/sfname
ffnamethe file name
  • full path if sfname used,
  • any file name if sfname is NULL
  • empty string to re-edit with the same file name (but may be in a different directory)
  • NULL to start an empty buffer
sfnamethe short file name (or NULL)
eapcontains the command to be executed after loading the file and forced 'ff' and 'fenc'. Can be NULL!
newlnumif > 0: put cursor on this line number (if possible) ECMD_LASTL: use last position in loaded file ECMD_LAST: use last position in all files ECMD_ONE: use first line
flagsECMD_HIDE: if TRUE don't free the current buffer ECMD_SET_HELP: set b_help flag of (new) buffer before opening file ECMD_OLDBUF: use existing buffer if it exists ECMD_FORCEIT: ! used for Ex command ECMD_ADDBUF: don't edit, just add to buffer list ECMD_ALTBUF: like ECMD_ADDBUF and also set the alternate file ECMD_NOWINENTER: Do not trigger BufWinEnter
oldwinShould be "curwin" when editing a new buffer in the current window, NULL when splitting the window first. When not NULL info of the previous buffer for "oldwin" is stored.
Returns
FAIL for failure, OK otherwise

◆ do_move()

int do_move ( linenr_T  line1,
linenr_T  line2,
linenr_T  dest 
)

:move command - move lines line1-line2 to line dest

Returns
FAIL for failure, OK otherwise

◆ do_shell()

void do_shell ( char *  cmd,
int  flags 
)

Call a shell to execute a command. When "cmd" is NULL start an interactive shell.

Parameters
flagsmay be SHELL_DOOUT when output is redirected

◆ do_sub_msg()

bool do_sub_msg ( bool  count_only)

Give message for number of substitutions. Can also be used after a ":global" command.

Parameters
count_onlyused 'n' flag for ":s"
Returns
true if a message was given.

◆ do_wqall()

void do_wqall ( exarg_T eap)

":wall", ":wqall" and ":xall": Write all changed files (and exit).

◆ do_write()

int do_write ( exarg_T eap)

write current buffer to file 'eap->arg' if 'eap->append' is TRUE, append to the file

if *eap->arg == NUL write to current file

Returns
FAIL for failure, OK otherwise

◆ ex_align()

void ex_align ( exarg_T eap)

":left", ":center" and ":right": align text.

◆ ex_append()

void ex_append ( exarg_T eap)

":insert" and ":append", also used by ":change"

◆ ex_change()

void ex_change ( exarg_T eap)

":change"

◆ ex_copy()

void ex_copy ( linenr_T  line1,
linenr_T  line2,
linenr_T  n 
)

":copy"

◆ ex_exusage()

void ex_exusage ( exarg_T eap)

":exusage"

◆ ex_file()

void ex_file ( exarg_T eap)

":file[!] [fname]".

◆ ex_global()

void ex_global ( exarg_T eap)

Execute a global command of the form:

g/pattern/X : execute X on all lines where pattern matches v/pattern/X : execute X on all lines where pattern does not match

where 'X' is an EX command

The command character (as well as the trailing slash) is optional, and is assumed to be 'p' if missing.

This is implemented in two passes: first we scan the file for the pattern and set a mark for each line that (not) matches. Secondly we execute the command for each line that has a mark. This is required because after deleting lines we do not know where to search for the next match.

◆ ex_help()

void ex_help ( exarg_T eap)

":help": open a read-only window on a help file

◆ ex_helpclose()

void ex_helpclose ( exarg_T eap)

":helpclose": Close one help window

◆ ex_helptags()

void ex_helptags ( exarg_T eap)

":helptags"

◆ ex_oldfiles()

void ex_oldfiles ( exarg_T eap)

List v:oldfiles in a nice way.

◆ ex_retab()

void ex_retab ( exarg_T eap)

":retab".

◆ ex_sort()

void ex_sort ( exarg_T eap)

":sort".

◆ ex_substitute()

void ex_substitute ( exarg_T eap)

:substitute command

If 'inccommand' is empty: calls do_sub(). If 'inccommand' is set: shows a "live" preview then removes the changes. from undo history.

◆ ex_update()

void ex_update ( exarg_T eap)

":update".

◆ ex_viusage()

void ex_viusage ( exarg_T eap)

":viusage"

◆ ex_wnext()

void ex_wnext ( exarg_T eap)

Handle ":wnext", ":wNext" and ":wprevious" commands.

◆ ex_write()

void ex_write ( exarg_T eap)

":write" and ":saveas".

◆ ex_z()

void ex_z ( exarg_T eap)

◆ find_help_tags()

int find_help_tags ( const char *  arg,
int *  num_matches,
char ***  matches,
bool  keep_lang 
)

Find all help tags matching "arg", sort them and return in matches[], with the number of matches in num_matches. The matches will be sorted with a "best" match algorithm. When "keep_lang" is true try keeping the language of the current buffer.

◆ fix_help_buffer()

void fix_help_buffer ( void  )

After reading a help file: May cleanup a help buffer when syntax highlighting is not used.

◆ getfile()

int getfile ( int  fnum,
char *  ffname_arg,
char *  sfname_arg,
int  setpm,
linenr_T  lnum,
int  forceit 
)

Try to abandon the current file and edit a new or existing file.

Parameters
fnumthe number of the file, if zero use "ffname_arg"/"sfname_arg".
lnumthe line number for the cursor in the new file (if non-zero).
Returns
: GETFILE_ERROR for "normal" error, GETFILE_NOT_WRITTEN for "not written" error, GETFILE_SAME_FILE for success GETFILE_OPEN_OTHER for successfully opening another file.

◆ global_exe()

void global_exe ( char *  cmd)

Execute cmd on lines marked with ml_setmarked().

◆ help_heuristic()

int help_heuristic ( char *  matched_string,
int  offset,
int  wrong_case 
)

Return a heuristic indicating how well the given string matches. The smaller the number, the better the match. This is the order of priorities, from best match to worst match:

  • Match with least alphanumeric characters is better.
  • Match with least total characters is better.
  • Match towards the start is better.
  • Match starting with "+" is worse (feature instead of command) Assumption is made that the matched_string passed has already been found to match some string for which help is requested. webb.
Parameters
offsetoffset for match
wrong_caseno matching case
Returns
a heuristic indicating how well the given string matches.

◆ make_filter_cmd()

char* make_filter_cmd ( char *  cmd,
char *  itmp,
char *  otmp 
)

Create a shell command from a command string, input redirection file and output redirection file.

Parameters
cmdCommand to execute.
itmpNULL or the input file.
otmpNULL or the output file.
Returns
an allocated string with the shell command.

◆ not_writing()

bool not_writing ( void  )

Check the 'write' option.

Returns
true and give a message when it's not st.

◆ prepare_tagpreview()

bool prepare_tagpreview ( bool  undo_sync)

Set up for a tagpreview.

Parameters
undo_syncsync undo when leaving the window
Returns
true when it was created.

◆ print_line()

void print_line ( linenr_T  lnum,
int  use_number,
int  list 
)

Print a text line. Also in silent mode ("ex -s").

◆ print_line_no_prefix()

void print_line_no_prefix ( linenr_T  lnum,
int  use_number,
int  list 
)

◆ rename_buffer()

int rename_buffer ( char *  new_fname)

◆ skip_vimgrep_pat()

char* skip_vimgrep_pat ( char *  p,
char **  s,
int *  flags 
)

Skip over the pattern argument of ":vimgrep /pat/[g][j]". Put the start of the pattern in "*s", unless "s" is NULL.

Parameters
flagsif not NULL, put the flags in it: VGR_GLOBAL, VGR_NOJUMP.
sif not NULL, terminate the pattern with a NUL.
Returns
a pointer to the char just past the pattern plus flags.

◆ sub_get_replacement()

void sub_get_replacement ( SubReplacementString *const  ret_sub)

Get old substitute replacement string

Parameters
[out]ret_subLocation where old string will be saved.

◆ sub_preview_win()

int sub_preview_win ( buf_T preview_buf)

Tries to enter to an existing window of given buffer. If no existing buffer is found, creates a new split.

Returns
OK/FAIL.

◆ sub_set_replacement()

void sub_set_replacement ( SubReplacementString  sub)

Set substitute string and timestamp

Warning
sub must be in allocated memory. It is not copied.
Parameters
[in]subNew replacement string.
xstrdup
char * xstrdup(const char *str) FUNC_ATTR_MALLOC FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_RET FUNC_ATTR_NONNULL_ALL
Definition: memory.c:420
ml_get
char_u * ml_get(linenr_T lnum)
Definition: memline.c:1806
vim_strsave
char_u * vim_strsave(const char_u *string) FUNC_ATTR_NONNULL_RET FUNC_ATTR_MALLOC FUNC_ATTR_NONNULL_ALL
Copy "string" into newly allocated memory.
Definition: strings.c:53
kv_last
#define kv_last(v)
Definition: kvec.h:71