Skip to content

Commit 8e97bd7

Browse files
committed
patch 7.4.2170
Problem: Cannot get information about timers. Solution: Add timer_info().
1 parent 446cce6 commit 8e97bd7

5 files changed

Lines changed: 119 additions & 10 deletions

File tree

runtime/doc/eval.txt

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*eval.txt* For Vim version 7.4. Last change: 2016 Aug 02
1+
*eval.txt* For Vim version 7.4. Last change: 2016 Aug 06
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -2339,6 +2339,7 @@ test_null_list() List null value for testing
23392339
test_null_partial() Funcref null value for testing
23402340
test_null_string() String null value for testing
23412341
test_settime({expr}) none set current time for testing
2342+
timer_info([{id}]) List information about timers
23422343
timer_start({time}, {callback} [, {options}])
23432344
Number create a timer
23442345
timer_stop({timer}) none stop a timer
@@ -3392,9 +3393,13 @@ exepath({expr}) *exepath()*
33923393
an empty string is returned.
33933394

33943395
*exists()*
3395-
exists({expr}) The result is a Number, which is |TRUE| if {expr} is
3396-
defined, zero otherwise. The {expr} argument is a string,
3397-
which contains one of these:
3396+
exists({expr}) The result is a Number, which is |TRUE| if {expr} is defined,
3397+
zero otherwise.
3398+
3399+
For checking for a supported feature use |has()|.
3400+
For checking if a file exists use |filereadable()|.
3401+
3402+
The {expr} argument is a string, which contains one of these:
33983403
&option-name Vim option (only checks if it exists,
33993404
not if it really works)
34003405
+option-name Vim option that works.
@@ -3442,7 +3447,6 @@ exists({expr}) The result is a Number, which is |TRUE| if {expr} is
34423447
event and pattern.
34433448
##event autocommand for this event is
34443449
supported.
3445-
For checking for a supported feature use |has()|.
34463450

34473451
Examples: >
34483452
exists("&shortname")
@@ -5476,7 +5480,8 @@ matchadd({group}, {pattern}[, {priority}[, {id}[, {dict}]]])
54765480
available from |getmatches()|. All matches can be deleted in
54775481
one operation by |clearmatches()|.
54785482

5479-
matchaddpos({group}, {pos}[, {priority}[, {id}[, {dict}]]]) *matchaddpos()*
5483+
*matchaddpos()*
5484+
matchaddpos({group}, {pos}[, {priority}[, {id}[, {dict}]]])
54805485
Same as |matchadd()|, but requires a list of positions {pos}
54815486
instead of a pattern. This command is faster than |matchadd()|
54825487
because it does not require to handle regular expressions and
@@ -7536,6 +7541,23 @@ test_settime({expr}) *test_settime()*
75367541
{expr} must evaluate to a number. When the value is zero the
75377542
normal behavior is restored.
75387543

7544+
*timer_info()*
7545+
timer_info([{id}])
7546+
Return a list with information about timers.
7547+
When {id} is given only information about this timer is
7548+
returned. When timer {id} does not exist an empty list is
7549+
returned.
7550+
When {id} is omitted information about all timers is returned.
7551+
7552+
For each timer the information is stored in a Dictionary with
7553+
these items:
7554+
"id" the timer ID
7555+
"time" time the timer was started with
7556+
"remaining" time until the timer fires
7557+
"repeat" number of times the timer will still fire;
7558+
-1 means forever
7559+
"callback" the callback
7560+
75397561
*timer_start()*
75407562
timer_start({time}, {callback} [, {options}])
75417563
Create a timer and return the timer ID.
@@ -7566,7 +7588,7 @@ timer_start({time}, {callback} [, {options}])
75667588
timer_stop({timer}) *timer_stop()*
75677589
Stop a timer. The timer callback will no longer be invoked.
75687590
{timer} is an ID returned by timer_start(), thus it must be a
7569-
Number.
7591+
Number. If {timer} does not exist there is no error.
75707592

75717593
tolower({expr}) *tolower()*
75727594
The result is a copy of the String given, with all uppercase

src/evalfunc.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ static void f_tan(typval_T *argvars, typval_T *rettv);
396396
static void f_tanh(typval_T *argvars, typval_T *rettv);
397397
#endif
398398
#ifdef FEAT_TIMERS
399+
static void f_timer_info(typval_T *argvars, typval_T *rettv);
399400
static void f_timer_start(typval_T *argvars, typval_T *rettv);
400401
static void f_timer_stop(typval_T *argvars, typval_T *rettv);
401402
#endif
@@ -815,6 +816,7 @@ static struct fst
815816
{"test_null_string", 0, 0, f_test_null_string},
816817
{"test_settime", 1, 1, f_test_settime},
817818
#ifdef FEAT_TIMERS
819+
{"timer_info", 0, 1, f_timer_info},
818820
{"timer_start", 2, 3, f_timer_start},
819821
{"timer_stop", 1, 1, f_timer_stop},
820822
#endif
@@ -11960,6 +11962,31 @@ free_callback(char_u *callback, partial_T *partial)
1196011962
#endif
1196111963

1196211964
#ifdef FEAT_TIMERS
11965+
/*
11966+
* "timer_info([timer])" function
11967+
*/
11968+
static void
11969+
f_timer_info(typval_T *argvars, typval_T *rettv)
11970+
{
11971+
timer_T *timer = NULL;
11972+
11973+
if (rettv_list_alloc(rettv) != OK)
11974+
return;
11975+
if (argvars[0].v_type != VAR_UNKNOWN)
11976+
{
11977+
if (argvars[0].v_type != VAR_NUMBER)
11978+
EMSG(_(e_number_exp));
11979+
else
11980+
{
11981+
timer = find_timer((int)get_tv_number(&argvars[0]));
11982+
if (timer != NULL)
11983+
add_timer_info(rettv, timer);
11984+
}
11985+
}
11986+
else
11987+
add_timer_info_all(rettv);
11988+
}
11989+
1196311990
/*
1196411991
* "timer_start(time, callback [, options])" function
1196511992
*/

src/ex_cmds2.c

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,10 +1139,8 @@ create_timer(long msec, int repeat)
11391139
timer->tr_id = ++last_timer_id;
11401140
insert_timer(timer);
11411141
if (repeat != 0)
1142-
{
11431142
timer->tr_repeat = repeat - 1;
1144-
timer->tr_interval = msec;
1145-
}
1143+
timer->tr_interval = msec;
11461144

11471145
profile_setlimit(msec, &timer->tr_due);
11481146
return timer;
@@ -1253,6 +1251,64 @@ stop_timer(timer_T *timer)
12531251
free_timer(timer);
12541252
}
12551253

1254+
void
1255+
add_timer_info(typval_T *rettv, timer_T *timer)
1256+
{
1257+
list_T *list = rettv->vval.v_list;
1258+
dict_T *dict = dict_alloc();
1259+
dictitem_T *di;
1260+
long remaining;
1261+
proftime_T now;
1262+
1263+
if (dict == NULL)
1264+
return;
1265+
list_append_dict(list, dict);
1266+
1267+
dict_add_nr_str(dict, "id", (long)timer->tr_id, NULL);
1268+
dict_add_nr_str(dict, "time", (long)timer->tr_interval, NULL);
1269+
1270+
profile_start(&now);
1271+
# ifdef WIN3264
1272+
remaining = (long)(((double)(timer->tr_due.QuadPart - now.QuadPart)
1273+
/ (double)fr.QuadPart) * 1000);
1274+
# else
1275+
remaining = (timer->tr_due.tv_sec - now.tv_sec) * 1000
1276+
+ (timer->tr_due.tv_usec - now.tv_usec) / 1000;
1277+
# endif
1278+
dict_add_nr_str(dict, "remaining", (long)remaining, NULL);
1279+
1280+
dict_add_nr_str(dict, "repeat",
1281+
(long)(timer->tr_repeat < 0 ? -1 : timer->tr_repeat + 1), NULL);
1282+
1283+
di = dictitem_alloc((char_u *)"callback");
1284+
if (di != NULL)
1285+
{
1286+
if (dict_add(dict, di) == FAIL)
1287+
vim_free(di);
1288+
else if (timer->tr_partial != NULL)
1289+
{
1290+
di->di_tv.v_type = VAR_PARTIAL;
1291+
di->di_tv.vval.v_partial = timer->tr_partial;
1292+
++timer->tr_partial->pt_refcount;
1293+
}
1294+
else
1295+
{
1296+
di->di_tv.v_type = VAR_FUNC;
1297+
di->di_tv.vval.v_string = vim_strsave(timer->tr_callback);
1298+
}
1299+
di->di_tv.v_lock = 0;
1300+
}
1301+
}
1302+
1303+
void
1304+
add_timer_info_all(typval_T *rettv)
1305+
{
1306+
timer_T *timer;
1307+
1308+
for (timer = first_timer; timer != NULL; timer = timer->tr_next)
1309+
add_timer_info(rettv, timer);
1310+
}
1311+
12561312
/*
12571313
* Mark references in partials of timers.
12581314
*/

src/proto/ex_cmds2.pro

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ timer_T *create_timer(long msec, int repeat);
2222
long check_due_timer(void);
2323
timer_T *find_timer(int id);
2424
void stop_timer(timer_T *timer);
25+
void add_timer_info(typval_T *rettv, timer_T *timer);
26+
void add_timer_info_all(typval_T *rettv);
2527
int set_ref_in_timer(int copyID);
2628
void timer_free_all(void);
2729
void profile_divide(proftime_T *tm, int count, proftime_T *tm2);

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,8 @@ static char *(features[]) =
763763

764764
static int included_patches[] =
765765
{ /* Add new patch number below this line */
766+
/**/
767+
2170,
766768
/**/
767769
2169,
768770
/**/

0 commit comments

Comments
 (0)