To: vim_dev@googlegroups.com Subject: Patch 7.4.2170 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 7.4.2170 Problem: Cannot get information about timers. Solution: Add timer_info(). Files: src/evalfunc.c, src/ex_cmds2.c, src/proto/ex_cmds2.pro, runtime/doc/eval.txt *** ../vim-7.4.2169/src/evalfunc.c 2016-08-06 14:12:44.950591485 +0200 --- src/evalfunc.c 2016-08-06 20:22:58.870328009 +0200 *************** *** 396,401 **** --- 396,402 ---- static void f_tanh(typval_T *argvars, typval_T *rettv); #endif #ifdef FEAT_TIMERS + static void f_timer_info(typval_T *argvars, typval_T *rettv); static void f_timer_start(typval_T *argvars, typval_T *rettv); static void f_timer_stop(typval_T *argvars, typval_T *rettv); #endif *************** *** 815,820 **** --- 816,822 ---- {"test_null_string", 0, 0, f_test_null_string}, {"test_settime", 1, 1, f_test_settime}, #ifdef FEAT_TIMERS + {"timer_info", 0, 1, f_timer_info}, {"timer_start", 2, 3, f_timer_start}, {"timer_stop", 1, 1, f_timer_stop}, #endif *************** *** 11961,11966 **** --- 11963,11993 ---- #ifdef FEAT_TIMERS /* + * "timer_info([timer])" function + */ + static void + f_timer_info(typval_T *argvars, typval_T *rettv) + { + timer_T *timer = NULL; + + if (rettv_list_alloc(rettv) != OK) + return; + if (argvars[0].v_type != VAR_UNKNOWN) + { + if (argvars[0].v_type != VAR_NUMBER) + EMSG(_(e_number_exp)); + else + { + timer = find_timer((int)get_tv_number(&argvars[0])); + if (timer != NULL) + add_timer_info(rettv, timer); + } + } + else + add_timer_info_all(rettv); + } + + /* * "timer_start(time, callback [, options])" function */ static void *** ../vim-7.4.2169/src/ex_cmds2.c 2016-08-06 19:01:33.980856744 +0200 --- src/ex_cmds2.c 2016-08-06 20:30:41.622673967 +0200 *************** *** 1139,1148 **** timer->tr_id = ++last_timer_id; insert_timer(timer); if (repeat != 0) - { timer->tr_repeat = repeat - 1; ! timer->tr_interval = msec; ! } profile_setlimit(msec, &timer->tr_due); return timer; --- 1139,1146 ---- timer->tr_id = ++last_timer_id; insert_timer(timer); if (repeat != 0) timer->tr_repeat = repeat - 1; ! timer->tr_interval = msec; profile_setlimit(msec, &timer->tr_due); return timer; *************** *** 1253,1258 **** --- 1251,1314 ---- free_timer(timer); } + void + add_timer_info(typval_T *rettv, timer_T *timer) + { + list_T *list = rettv->vval.v_list; + dict_T *dict = dict_alloc(); + dictitem_T *di; + long remaining; + proftime_T now; + + if (dict == NULL) + return; + list_append_dict(list, dict); + + dict_add_nr_str(dict, "id", (long)timer->tr_id, NULL); + dict_add_nr_str(dict, "time", (long)timer->tr_interval, NULL); + + profile_start(&now); + # ifdef WIN3264 + remaining = (long)(((double)(timer->tr_due.QuadPart - now.QuadPart) + / (double)fr.QuadPart) * 1000); + # else + remaining = (timer->tr_due.tv_sec - now.tv_sec) * 1000 + + (timer->tr_due.tv_usec - now.tv_usec) / 1000; + # endif + dict_add_nr_str(dict, "remaining", (long)remaining, NULL); + + dict_add_nr_str(dict, "repeat", + (long)(timer->tr_repeat < 0 ? -1 : timer->tr_repeat + 1), NULL); + + di = dictitem_alloc((char_u *)"callback"); + if (di != NULL) + { + if (dict_add(dict, di) == FAIL) + vim_free(di); + else if (timer->tr_partial != NULL) + { + di->di_tv.v_type = VAR_PARTIAL; + di->di_tv.vval.v_partial = timer->tr_partial; + ++timer->tr_partial->pt_refcount; + } + else + { + di->di_tv.v_type = VAR_FUNC; + di->di_tv.vval.v_string = vim_strsave(timer->tr_callback); + } + di->di_tv.v_lock = 0; + } + } + + void + add_timer_info_all(typval_T *rettv) + { + timer_T *timer; + + for (timer = first_timer; timer != NULL; timer = timer->tr_next) + add_timer_info(rettv, timer); + } + /* * Mark references in partials of timers. */ *** ../vim-7.4.2169/src/proto/ex_cmds2.pro 2016-07-30 22:47:44.322520729 +0200 --- src/proto/ex_cmds2.pro 2016-08-06 20:23:17.770178815 +0200 *************** *** 22,27 **** --- 22,29 ---- long check_due_timer(void); timer_T *find_timer(int id); void stop_timer(timer_T *timer); + void add_timer_info(typval_T *rettv, timer_T *timer); + void add_timer_info_all(typval_T *rettv); int set_ref_in_timer(int copyID); void timer_free_all(void); void profile_divide(proftime_T *tm, int count, proftime_T *tm2); *** ../vim-7.4.2169/runtime/doc/eval.txt 2016-08-01 15:40:24.187878367 +0200 --- runtime/doc/eval.txt 2016-08-06 21:53:45.167505296 +0200 *************** *** 2317,2322 **** --- 2339,2345 ---- test_null_partial() Funcref null value for testing test_null_string() String null value for testing test_settime({expr}) none set current time for testing + timer_info([{id}]) List information about timers timer_start({time}, {callback} [, {options}]) Number create a timer timer_stop({timer}) none stop a timer *************** *** 7430,7439 **** test_settime({expr}) *test_settime()* Set the time Vim uses internally. Currently only used for ! timestamps in the history, as they are used in viminfo. {expr} must evaluate to a number. When the value is zero the normal behavior is restored. *timer_start()* timer_start({time}, {callback} [, {options}]) Create a timer and return the timer ID. --- 7536,7563 ---- test_settime({expr}) *test_settime()* Set the time Vim uses internally. Currently only used for ! timestamps in the history, as they are used in viminfo, and ! for undo. {expr} must evaluate to a number. When the value is zero the normal behavior is restored. + *timer_info()* + timer_info([{id}]) + Return a list with information about timers. + When {id} is given only information about this timer is + returned. When timer {id} does not exist an empty list is + returned. + When {id} is omitted information about all timers is returned. + + For each timer the information is stored in a Dictionary with + these items: + "id" the timer ID + "time" time the timer was started with + "remaining" time until the timer fires + "repeat" number of times the timer will still fire; + -1 means forever + "callback" the callback + *timer_start()* timer_start({time}, {callback} [, {options}]) Create a timer and return the timer ID. *************** *** 7449,7455 **** {options} is a dictionary. Supported entries: "repeat" Number of times to repeat calling the ! callback. -1 means forever. Example: > func MyHandler(timer) --- 7573,7579 ---- {options} is a dictionary. Supported entries: "repeat" Number of times to repeat calling the ! callback. -1 means forever. Example: > func MyHandler(timer) *************** *** 7461,7466 **** --- 7585,7595 ---- intervals. {only available when compiled with the |+timers| feature} + timer_stop({timer}) *timer_stop()* + Stop a timer. The timer callback will no longer be invoked. + {timer} is an ID returned by timer_start(), thus it must be a + Number. If {timer} does not exist there is no error. + tolower({expr}) *tolower()* The result is a copy of the String given, with all uppercase characters turned into lowercase (just like applying |gu| to *** ../vim-7.4.2169/src/version.c 2016-08-06 21:37:23.995218001 +0200 --- src/version.c 2016-08-06 22:02:04.355574399 +0200 *************** *** 765,766 **** --- 765,768 ---- { /* Add new patch number below this line */ + /**/ + 2170, /**/ -- This computer is so slow, it takes forever to execute and endless loop! /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\ \\\ an exciting new programming language -- http://www.Zimbu.org /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///