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
15 changes: 11 additions & 4 deletions runtime/doc/eval.txt
Expand Up @@ -2302,6 +2302,8 @@ range({expr} [, {max} [, {stride}]])
List items from {expr} to {max}
readfile({fname} [, {binary} [, {max}]])
List get list of lines from file {fname}
register_executing() Number |TRUE| if the register executing
register_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 @@ -6166,10 +6168,6 @@ mode([expr]) Return a string that indicates the current mode.
r? A |:confirm| query of some sort
! Shell or external command is executing
t Terminal-Job mode: keys go to the job
When the full mode, the following letters may be returned in
addition to the above letters.
q In recording into register |recording|
@ In executing the register |@|
This is useful in the 'statusline' option or when used
with |remote_expr()| In most other places it always returns
"c" or "n".
Expand Down Expand Up @@ -6569,6 +6567,15 @@ readfile({fname} [, {binary} [, {max}]])
the result is an empty list.
Also see |writefile()|.

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

register_recording() *register_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 @@ -1016,6 +1016,8 @@ Various: *various-functions*
getreg() get contents of a register
getregtype() get type of a register
setreg() set contents and type of a register
regster_executing() return the state of the register executing
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/regster/reg

regster_recording() return the state of the register recording
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/regster/reg


shiftwidth() effective value of 'shiftwidth'

Expand Down
78 changes: 48 additions & 30 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_register_executing(typval_T *argvars, typval_T *rettv);
static void f_register_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},
{"register_executing", 0, 0, f_register_executing},
{"register_recording", 0, 0, f_register_recording},
{"reltime", 0, 2, f_reltime},
#ifdef FEAT_FLOAT
{"reltimefloat", 1, 1, f_reltimefloat},
Expand Down Expand Up @@ -8078,82 +8082,75 @@ f_mkdir(typval_T *argvars, typval_T *rettv)
static void
f_mode(typval_T *argvars, typval_T *rettv)
{
char_u buf[5];
int i = 0;
char_u buf[3];

vim_memset(buf, 0, sizeof(buf));
buf[1] = NUL;
buf[2] = NUL;

if (time_for_testing == 93784)
{
/* Testing the two-character code. */
buf[i++] = 'x';
buf[i++] = '!';
buf[0] = 'x';
buf[1] = '!';
}
#ifdef FEAT_TERMINAL
else if (term_use_loop())
buf[i++] = 't';
buf[0] = 't';
#endif
else if (VIsual_active)
{
if (VIsual_select)
buf[i++] = VIsual_mode + 's' - 'v';
buf[0] = VIsual_mode + 's' - 'v';
else
buf[i++] = VIsual_mode;
buf[0] = VIsual_mode;
}
else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
|| State == CONFIRM)
{
buf[i++] = 'r';
buf[0] = 'r';
if (State == ASKMORE)
buf[i++] = 'm';
buf[1] = 'm';
else if (State == CONFIRM)
buf[i++] = '?';
buf[1] = '?';
}
else if (State == EXTERNCMD)
buf[i++] = '!';
buf[0] = '!';
else if (State & INSERT)
{
#ifdef FEAT_VREPLACE
if (State & VREPLACE_FLAG)
{
buf[i++] = 'R';
buf[i++] = 'v';
buf[0] = 'R';
buf[1] = 'v';
}
else
#endif
{
if (State & REPLACE_FLAG)
buf[i++] = 'R';
buf[0] = 'R';
else
buf[i++] = 'i';
buf[0] = 'i';
#ifdef FEAT_INS_EXPAND
if (ins_compl_active())
buf[i++] = 'c';
buf[1] = 'c';
else if (ctrl_x_mode_not_defined_yet())
buf[i++] = 'x';
buf[1] = 'x';
#endif
}
}
else if ((State & CMDLINE) || exmode_active)
{
buf[i++] = 'c';
buf[0] = 'c';
if (exmode_active == EXMODE_VIM)
buf[i++] = 'v';
buf[1] = 'v';
else if (exmode_active == EXMODE_NORMAL)
buf[i++] = 'e';
buf[1] = 'e';
}
else
{
buf[i++] = 'n';
buf[0] = 'n';
if (finish_op)
buf[i++] = 'o';
}

if (Recording) {
buf[i++] = 'q';
}
if (Exec_reg) {
buf[i++] = '@';
buf[1] = 'o';
}

/* Clear out the minor mode when the argument is not a non-zero number or
Expand Down Expand Up @@ -8685,6 +8682,27 @@ f_readfile(typval_T *argvars, typval_T *rettv)
fclose(fd);
}

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

/*
* "register_recording()" function
*/
static void
f_register_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
44 changes: 25 additions & 19 deletions src/testdir/test_functions.vim
Expand Up @@ -464,25 +464,6 @@ func Test_mode()
call assert_equal("\<C-S>", mode(1))
call feedkeys("\<Esc>", 'xt')

call feedkeys("qai\<F2>\<Esc>q", 'xt')
call assert_equal('i-iq', g:current_modes)
call feedkeys("@a", 'xt')
call assert_equal('i-i@', g:current_modes)
call feedkeys("qb@aq", 'xt')
call assert_equal('i-iq@', g:current_modes)
call feedkeys("qai\<C-G>uBa\<C-P>\<F2>\<Esc>uq", 'xt')
call assert_equal('i-icq', g:current_modes)
call feedkeys("@a", 'xt')
call assert_equal('i-ic@', g:current_modes)
call feedkeys("qb@aq", 'xt')
call assert_equal('i-icq@', g:current_modes)
call feedkeys("qa\"=Save_mode()\<CR>pq", 'xt')
call assert_equal('n-nq', g:current_modes)
call feedkeys("@a", 'xt')
call assert_equal('n-n@', g:current_modes)
call feedkeys("qb@aq", 'xt')
call assert_equal('n-nq@', g:current_modes)

call feedkeys(":echo \<C-R>=Save_mode()\<C-U>\<CR>", 'xt')
call assert_equal('c-c', g:current_modes)
call feedkeys("gQecho \<C-R>=Save_mode()\<CR>\<CR>vi\<CR>", 'xt')
Expand Down Expand Up @@ -918,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 register_recording() and register_executing()
func Test_register_executing_and_recording()
let s:reg_stat = ''
func s:save_reg_stat()
let s:reg_stat = register_recording() . ':' . register_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