refactor: move cmdline functions to ex_getln.c

This commit is contained in:
zeertzjq
2022-08-29 15:12:30 +08:00
parent f58a979599
commit 2c83d7b2dd
2 changed files with 93 additions and 124 deletions

View File

@@ -2721,40 +2721,6 @@ static void f_getcharsearch(typval_T *argvars, typval_T *rettv, EvalFuncData fpt
tv_dict_add_nr(dict, S_LEN("until"), last_csearch_until()); tv_dict_add_nr(dict, S_LEN("until"), last_csearch_until());
} }
/// "getcmdcompltype()" function
static void f_getcmdcompltype(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{
rettv->v_type = VAR_STRING;
rettv->vval.v_string = (char *)get_cmdline_completion();
}
/// "getcmdline()" function
static void f_getcmdline(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{
rettv->v_type = VAR_STRING;
rettv->vval.v_string = (char *)get_cmdline_str();
}
/// "getcmdpos()" function
static void f_getcmdpos(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{
rettv->vval.v_number = get_cmdline_pos() + 1;
}
/// "getcmdscreenpos()" function
static void f_getcmdscreenpos(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{
rettv->vval.v_number = get_cmdline_screen_pos() + 1;
}
/// "getcmdtype()" function
static void f_getcmdtype(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{
rettv->v_type = VAR_STRING;
rettv->vval.v_string = xmallocz(1);
rettv->vval.v_string[0] = (char)get_cmdline_type();
}
/// "getcmdwintype()" function /// "getcmdwintype()" function
static void f_getcmdwintype(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) static void f_getcmdwintype(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{ {
@@ -7622,41 +7588,6 @@ static void f_setcharsearch(typval_T *argvars, typval_T *rettv, EvalFuncData fpt
} }
} }
/// "setcmdline()" function
static void f_setcmdline(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{
if (argvars[0].v_type != VAR_STRING || argvars[0].vval.v_string == NULL) {
emsg(_(e_stringreq));
return;
}
int pos = -1;
if (argvars[1].v_type != VAR_UNKNOWN) {
bool error = false;
pos = (int)tv_get_number_chk(&argvars[1], &error) - 1;
if (error) {
return;
}
if (pos < 0) {
emsg(_(e_positive));
return;
}
}
rettv->vval.v_number = set_cmdline_str(argvars[0].vval.v_string, pos);
}
/// "setcmdpos()" function
static void f_setcmdpos(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{
const int pos = (int)tv_get_number(&argvars[0]) - 1;
if (pos >= 0) {
rettv->vval.v_number = set_cmdline_pos(pos);
}
}
/// "setcursorcharpos" function /// "setcursorcharpos" function
static void f_setcursorcharpos(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) static void f_setcursorcharpos(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{ {

View File

@@ -3933,8 +3933,41 @@ static CmdlineInfo *get_ccline_ptr(void)
} }
} }
/// Get the current command-line type.
/// Returns ':' or '/' or '?' or '@' or '>' or '-'
/// Only works when the command line is being edited.
/// Returns NUL when something is wrong.
static int get_cmdline_type(void)
{
CmdlineInfo *p = get_ccline_ptr();
if (p == NULL) {
return NUL;
}
if (p->cmdfirstc == NUL) {
return (p->input_fn) ? '@' : '-';
}
return p->cmdfirstc;
}
/// Get the current command line in allocated memory.
/// Only works when the command line is being edited.
/// Returns NULL when something is wrong.
static char_u *get_cmdline_str(void)
{
if (cmdline_star > 0) {
return NULL;
}
CmdlineInfo *p = get_ccline_ptr();
if (p == NULL) {
return NULL;
}
return vim_strnsave(p->cmdbuff, (size_t)p->cmdlen);
}
/// Get the current command-line completion type. /// Get the current command-line completion type.
char_u *get_cmdline_completion(void) static char_u *get_cmdline_completion(void)
{ {
if (cmdline_star > 0) { if (cmdline_star > 0) {
return NULL; return NULL;
@@ -3952,54 +3985,45 @@ char_u *get_cmdline_completion(void)
return NULL; return NULL;
} }
/* /// "getcmdcompltype()" function
* Get the current command line in allocated memory. void f_getcmdcompltype(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
* Only works when the command line is being edited.
* Returns NULL when something is wrong.
*/
char_u *get_cmdline_str(void)
{ {
if (cmdline_star > 0) { rettv->v_type = VAR_STRING;
return NULL; rettv->vval.v_string = (char *)get_cmdline_completion();
}
CmdlineInfo *p = get_ccline_ptr();
if (p == NULL) {
return NULL;
}
return vim_strnsave(p->cmdbuff, (size_t)p->cmdlen);
} }
/* /// "getcmdline()" function
* Get the current command line position, counted in bytes. void f_getcmdline(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
* Zero is the first position.
* Only works when the command line is being edited.
* Returns -1 when something is wrong.
*/
int get_cmdline_pos(void)
{ {
CmdlineInfo *p = get_ccline_ptr(); rettv->v_type = VAR_STRING;
rettv->vval.v_string = (char *)get_cmdline_str();
if (p == NULL) {
return -1;
}
return p->cmdpos;
} }
/// Get the command line cursor screen position. /// "getcmdpos()" function
int get_cmdline_screen_pos(void) void f_getcmdpos(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{ {
CmdlineInfo *p = get_ccline_ptr(); CmdlineInfo *p = get_ccline_ptr();
rettv->vval.v_number = p != NULL ? p->cmdpos + 1 : 0;
}
if (p == NULL) { /// "getcmdscreenpos()" function
return -1; void f_getcmdscreenpos(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
} {
return p->cmdspos; CmdlineInfo *p = get_ccline_ptr();
rettv->vval.v_number = p != NULL ? p->cmdspos + 1 : 0;
}
/// "getcmdtype()" function
void f_getcmdtype(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{
rettv->v_type = VAR_STRING;
rettv->vval.v_string = xmallocz(1);
rettv->vval.v_string[0] = (char)get_cmdline_type();
} }
/// Set the command line str to "str". /// Set the command line str to "str".
/// @return 1 when failed, 0 when OK. /// @return 1 when failed, 0 when OK.
int set_cmdline_str(const char *str, int pos) static int set_cmdline_str(const char *str, int pos)
{ {
CmdlineInfo *p = get_ccline_ptr(); CmdlineInfo *p = get_ccline_ptr();
@@ -4023,12 +4047,10 @@ int set_cmdline_str(const char *str, int pos)
return 0; return 0;
} }
/* /// Set the command line byte position to "pos". Zero is the first position.
* Set the command line byte position to "pos". Zero is the first position. /// Only works when the command line is being edited.
* Only works when the command line is being edited. /// @return 1 when failed, 0 when OK.
* Returns 1 when failed, 0 when OK. static int set_cmdline_pos(int pos)
*/
int set_cmdline_pos(int pos)
{ {
CmdlineInfo *p = get_ccline_ptr(); CmdlineInfo *p = get_ccline_ptr();
@@ -4046,23 +4068,39 @@ int set_cmdline_pos(int pos)
return 0; return 0;
} }
/* /// "setcmdline()" function
* Get the current command-line type. void f_setcmdline(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
* Returns ':' or '/' or '?' or '@' or '>' or '-'
* Only works when the command line is being edited.
* Returns NUL when something is wrong.
*/
int get_cmdline_type(void)
{ {
CmdlineInfo *p = get_ccline_ptr(); if (argvars[0].v_type != VAR_STRING || argvars[0].vval.v_string == NULL) {
emsg(_(e_stringreq));
return;
}
if (p == NULL) { int pos = -1;
return NUL; if (argvars[1].v_type != VAR_UNKNOWN) {
bool error = false;
pos = (int)tv_get_number_chk(&argvars[1], &error) - 1;
if (error) {
return;
} }
if (p->cmdfirstc == NUL) { if (pos < 0) {
return (p->input_fn) ? '@' : '-'; emsg(_(e_positive));
return;
}
}
rettv->vval.v_number = set_cmdline_str(argvars[0].vval.v_string, pos);
}
/// "setcmdpos()" function
void f_setcmdpos(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{
const int pos = (int)tv_get_number(&argvars[0]) - 1;
if (pos >= 0) {
rettv->vval.v_number = set_cmdline_pos(pos);
} }
return p->cmdfirstc;
} }
/// Return the first character of the current command line. /// Return the first character of the current command line.