项目作者: machakann

项目描述 :
Handling tasks
高级语言: Vim script
项目地址: git://github.com/machakann/vital-Schedule.git
创建时间: 2018-03-03T10:45:58Z
项目社区:https://github.com/machakann/vital-Schedule

开源协议:

下载


vital-Schedule

Build Status
Build status

Handling auto-command and timer

Motivation

When I make a vim plugin, I frequently write codes to run a function later. Currently, vim script API gives two choices for the case, that is, auto-command event and timer. However, these measures have quite different interfaces; auto-command events are handled by commands while a timer is controlled by functions. What I want is an easy and unified interface to them.

Usage

This vital-Schedule module provides another interface to handle auto-command and timer. It enables us to write complicated scheduled tasks at ease.

For example, it is a little hassle to run a function only once by using native auto-command interface; the function should unset the auto-command events hooked to by itself.

  1. function! s:main() abort
  2. " set the autocmd event for s:run_once()
  3. augroup example-augroup
  4. autocmd!
  5. autocmd WinLeave * call s:run_once()
  6. augroup END
  7. endfunction
  8. function! s:run_once() abort
  9. call s:do_something()
  10. " unset the autocmd events
  11. augroup example-augroup
  12. autocmd! WinLeave
  13. augroup END
  14. endfunction
  15. function! s:do_something() abort
  16. " do something here
  17. endfunction

Task object in this module makes the situation simpler. s:run_once() is no longer required with it.

  1. let s:Schedule = vital#{pluginname}#new().import('Schedule')
  2. function! s:main() abort
  3. " set task
  4. let task = s:Schedule.Task()
  5. call task.call(function('s:do_something'), [])
  6. call task.waitfor(['WinLeave'])
  7. endfunction
  8. function! s:do_something() abort
  9. " do something here
  10. endfunction

Replace {pluginname} with your plugin name. Check :help vital.vim and :help Vitalizer.

Moreover, it can accept multiple triggers. For example, if you want to run a function after 3000 milliseconds passed or when user starts editing:

  1. let s:Schedule = vital#{pluginname}#new().import('Schedule')
  2. function! s:main() abort
  3. " set task
  4. let task = s:Schedule.Task()
  5. call task.call(function('s:do_something'), [])
  6. call task.waitfor([3000, 'TextChanged', 'TextChangedI'])
  7. endfunction
  8. function! s:do_something() abort
  9. " do something here
  10. endfunction

s:do_something() will be triggered by the earliest one in 3000 milliseconds, TextChanged or TextChangedI.

Dependency

This is an external module of vital.vim plugin. Install vital.vim and vital-Schedule, then use :Vitalize command. See :help Vitalizer.

  1. :Vitalize --name={pluginname} . Schedule

License: NYSL license