Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Proposal] Add built-in function register_executing() and register_recording() #2745

Closed
wants to merge 10 commits into from
11 changes: 11 additions & 0 deletions runtime/doc/eval.txt
Expand Up @@ -2304,6 +2304,8 @@ range({expr} [, {max} [, {stride}]])
List items from {expr} to {max}
readfile({fname} [, {binary} [, {max}]])
List get list of lines from file {fname}
reg_executing() Number |TRUE| if the register executing
reg_recording() String get the recording register name
reltime([{start} [, {end}]]) List get time value
reltimefloat({time}) Float turn the time value into a Float
reltimestr({time}) String turn time value into a String
Expand Down Expand Up @@ -6560,6 +6562,15 @@ readfile({fname} [, {binary} [, {max}]])
the result is an empty list.
Also see |writefile()|.

reg_executing() *reg_executing()*
Returns |TRUE| when the register contents are executing.
See |@|.

reg_recording() *reg_recording()*
Returns the named register character {0-9a-zA-Z"} when the
register contents are recording. Otherwise, an empty string
is returned. See |q|.

reltime([{start} [, {end}]]) *reltime()*
Return an item that represents a time value. The format of
the item depends on the system. It can be passed to
Expand Down
2 changes: 2 additions & 0 deletions runtime/doc/usr_41.txt
Expand Up @@ -1018,6 +1018,8 @@ Various: *various-functions*
getreg() get contents of a register
getregtype() get type of a register
setreg() set contents and type of a register
reg_executing() return the state of the register executing
reg_recording() return the state of the register recording

shiftwidth() effective value of 'shiftwidth'

Expand Down
25 changes: 25 additions & 0 deletions src/evalfunc.c
Expand Up @@ -306,6 +306,8 @@ static void f_pyxeval(typval_T *argvars, typval_T *rettv);
#endif
static void f_range(typval_T *argvars, typval_T *rettv);
static void f_readfile(typval_T *argvars, typval_T *rettv);
static void f_reg_executing(typval_T *argvars, typval_T *rettv);
static void f_reg_recording(typval_T *argvars, typval_T *rettv);
static void f_reltime(typval_T *argvars, typval_T *rettv);
#ifdef FEAT_FLOAT
static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
Expand Down Expand Up @@ -754,6 +756,8 @@ static struct fst
#endif
{"range", 1, 3, f_range},
{"readfile", 1, 3, f_readfile},
{"reg_executing", 0, 0, f_reg_executing},
{"reg_recording", 0, 0, f_reg_recording},
{"reltime", 0, 2, f_reltime},
#ifdef FEAT_FLOAT
{"reltimefloat", 1, 1, f_reltimefloat},
Expand Down Expand Up @@ -8698,6 +8702,27 @@ f_readfile(typval_T *argvars, typval_T *rettv)
fclose(fd);
}

/*
* "reg_executing()" function
*/
static void
f_reg_executing(typval_T *argvars, typval_T *rettv)
{
rettv->vval.v_number = Exec_reg;
}

/*
* "reg_recording()" function
*/
static void
f_reg_recording(typval_T *argvars, typval_T *rettv)
{
char_u regname[2] = {0, 0};
regname[0] = (char_u)Recording;
rettv->v_type = VAR_STRING;
rettv->vval.v_string = vim_strsave(regname);
}

#if defined(FEAT_RELTIME)
static int list2proftime(typval_T *arg, proftime_T *tm);

Expand Down
25 changes: 25 additions & 0 deletions src/testdir/test_functions.vim
Expand Up @@ -899,3 +899,28 @@ func Test_trim()
let chars = join(map(range(1, 0x20) + [0xa0], {n -> nr2char(n)}), '')
call assert_equal("x", trim(chars . "x" . chars))
endfunc

" Test for reg_recording() and reg_executing()
func Test_reg_executing_and_recording()
let s:reg_stat = ''
func s:save_reg_stat()
let s:reg_stat = reg_recording() . ':' . reg_executing()
return ''
endfunc

new
call s:save_reg_stat()
call assert_equal(':0', s:reg_stat)
call feedkeys("qa\"=s:save_reg_stat()\<CR>pq", 'xt')
call assert_equal('a:0', s:reg_stat)
call feedkeys("@a", 'xt')
call assert_equal(':1', s:reg_stat)
call feedkeys("qb@aq", 'xt')
call assert_equal('b:1', s:reg_stat)
call feedkeys("q\"\"=s:save_reg_stat()\<CR>pq", 'xt')
call assert_equal('":0', s:reg_stat)

bwipe!
delfunc s:save_reg_stat
unlet s:reg_stat
endfunc