2013-06-25 07:57:15 +00:00
|
|
|
" ============================================================================
|
|
|
|
" File: wakatime.vim
|
2013-07-02 09:24:04 +00:00
|
|
|
" Description: Automatic time tracking for Vim.
|
2013-09-05 05:32:20 +00:00
|
|
|
" Maintainer: WakaTime <support@wakatime.com>
|
2014-03-13 00:32:05 +00:00
|
|
|
" License: BSD, see LICENSE.txt for more details.
|
2014-03-14 20:35:11 +00:00
|
|
|
" Website: https://wakatime.com/
|
2013-06-25 07:57:15 +00:00
|
|
|
" ============================================================================
|
|
|
|
|
2014-11-13 03:35:42 +00:00
|
|
|
let s:VERSION = '2.0.11'
|
2013-07-09 16:55:22 +00:00
|
|
|
|
2013-06-26 04:09:52 +00:00
|
|
|
|
2013-06-25 07:57:15 +00:00
|
|
|
" Init {{{
|
|
|
|
|
2013-07-02 09:24:04 +00:00
|
|
|
" Check Vim version
|
|
|
|
if v:version < 700
|
|
|
|
echoerr "This plugin requires vim >= 7."
|
|
|
|
finish
|
2013-06-25 07:57:15 +00:00
|
|
|
endif
|
2013-07-02 09:24:04 +00:00
|
|
|
|
|
|
|
" Only load plugin once
|
|
|
|
if exists("g:loaded_wakatime")
|
|
|
|
finish
|
|
|
|
endif
|
|
|
|
let g:loaded_wakatime = 1
|
2013-06-25 07:57:15 +00:00
|
|
|
|
2013-07-02 09:24:04 +00:00
|
|
|
" Backup & Override cpoptions
|
|
|
|
let s:old_cpo = &cpo
|
|
|
|
set cpo&vim
|
2013-06-25 07:57:15 +00:00
|
|
|
|
2013-07-09 00:09:43 +00:00
|
|
|
" To be backwards compatible, rename config file
|
|
|
|
if filereadable(expand("$HOME/.wakatime"))
|
|
|
|
exec "silent !mv" expand("$HOME/.wakatime") expand("$HOME/.wakatime.conf")
|
|
|
|
endif
|
2013-12-13 15:03:09 +00:00
|
|
|
if filereadable(expand("$HOME/.wakatime.conf"))
|
|
|
|
if !filereadable(expand("$HOME/.wakatime.cfg"))
|
|
|
|
let contents = ['[settings]'] + readfile(expand("$HOME/.wakatime.conf"), '')
|
|
|
|
call writefile(contents, expand("$HOME/.wakatime.cfg"))
|
|
|
|
call delete(expand("$HOME/.wakatime.conf"))
|
|
|
|
endif
|
|
|
|
endif
|
2013-07-11 02:47:42 +00:00
|
|
|
|
|
|
|
" Create config file if does not exist
|
2013-12-13 15:03:09 +00:00
|
|
|
if !filereadable(expand("$HOME/.wakatime.cfg"))
|
|
|
|
let key = input("[WakaTime] Enter your wakatime.com api key: ")
|
2013-07-11 02:47:42 +00:00
|
|
|
if key != ''
|
2013-12-13 15:03:09 +00:00
|
|
|
call writefile(['[settings]', printf("api_key=%s", key)], expand("$HOME/.wakatime.cfg"))
|
|
|
|
echo "[WakaTime] Setup complete! Visit http://wakatime.com to view your logged time."
|
2013-07-11 02:47:42 +00:00
|
|
|
endif
|
|
|
|
endif
|
2013-06-26 05:02:59 +00:00
|
|
|
|
2013-08-12 10:30:17 +00:00
|
|
|
" Set default action frequency in minutes
|
|
|
|
if !exists("g:wakatime_ActionFrequency")
|
2013-11-28 11:21:48 +00:00
|
|
|
let g:wakatime_ActionFrequency = 2
|
2013-08-12 10:30:17 +00:00
|
|
|
endif
|
|
|
|
|
2013-07-08 04:25:06 +00:00
|
|
|
" Globals
|
|
|
|
let s:plugin_directory = expand("<sfile>:p:h") . '/'
|
|
|
|
let s:last_action = 0
|
2013-07-08 05:18:47 +00:00
|
|
|
let s:fresh = 1
|
2013-07-02 04:07:38 +00:00
|
|
|
|
2013-06-25 07:57:15 +00:00
|
|
|
" }}}
|
|
|
|
|
2013-06-26 04:09:52 +00:00
|
|
|
|
2013-06-25 07:57:15 +00:00
|
|
|
" Function Definitions {{{
|
|
|
|
|
2013-07-02 09:24:04 +00:00
|
|
|
function! s:GetCurrentFile()
|
|
|
|
return expand("%:p")
|
|
|
|
endfunction
|
|
|
|
|
2013-08-12 10:30:17 +00:00
|
|
|
function! s:Api(targetFile, time, is_write, last)
|
2013-07-08 04:25:06 +00:00
|
|
|
let targetFile = a:targetFile
|
|
|
|
if targetFile == ''
|
2013-07-10 04:13:45 +00:00
|
|
|
let targetFile = a:last[2]
|
2013-07-08 04:25:06 +00:00
|
|
|
endif
|
|
|
|
if targetFile != ''
|
2014-05-27 05:39:37 +00:00
|
|
|
let python_bin = 'python'
|
2014-05-29 06:14:25 +00:00
|
|
|
if has('win32') || has('win64')
|
2014-05-27 05:39:37 +00:00
|
|
|
let python_bin = 'pythonw'
|
|
|
|
endif
|
|
|
|
let cmd = [python_bin, '-W', 'ignore', s:plugin_directory . 'packages/wakatime/wakatime-cli.py']
|
2013-07-09 16:55:22 +00:00
|
|
|
let cmd = cmd + ['--file', shellescape(targetFile)]
|
|
|
|
let cmd = cmd + ['--plugin', printf('vim-wakatime/%s', s:VERSION)]
|
2013-07-08 04:25:06 +00:00
|
|
|
if a:is_write
|
2013-07-09 16:55:22 +00:00
|
|
|
let cmd = cmd + ['--write']
|
2013-07-08 04:25:06 +00:00
|
|
|
endif
|
2013-07-09 22:34:03 +00:00
|
|
|
"let cmd = cmd + ['--verbose']
|
2014-05-29 06:14:25 +00:00
|
|
|
if has('win32') || has('win64')
|
2014-05-27 05:39:37 +00:00
|
|
|
exec 'silent !start /min cmd /c "' . join(cmd, ' ') . '"'
|
|
|
|
else
|
|
|
|
exec 'silent !' . join(cmd, ' ') . ' &'
|
|
|
|
endif
|
2013-08-12 10:30:17 +00:00
|
|
|
call s:SetLastAction(a:time, a:time, targetFile)
|
2013-07-08 04:25:06 +00:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:GetLastAction()
|
|
|
|
if !filereadable(expand("$HOME/.wakatime.data"))
|
2013-12-16 10:07:28 +00:00
|
|
|
return [0, 0, '']
|
2013-07-08 04:25:06 +00:00
|
|
|
endif
|
2013-07-10 08:11:50 +00:00
|
|
|
let last = readfile(expand("$HOME/.wakatime.data"), '', 3)
|
|
|
|
if len(last) != 3
|
2013-12-16 10:07:28 +00:00
|
|
|
return [0, 0, '']
|
2013-07-02 09:24:04 +00:00
|
|
|
endif
|
2013-12-16 10:07:28 +00:00
|
|
|
return last
|
2013-07-08 04:25:06 +00:00
|
|
|
endfunction
|
|
|
|
|
2013-07-10 07:55:42 +00:00
|
|
|
function! s:SetLastAction(time, last_update, targetFile)
|
2013-07-08 05:18:47 +00:00
|
|
|
let s:fresh = 0
|
2013-12-16 10:07:28 +00:00
|
|
|
call writefile([substitute(printf('%d', a:time), ',', '.', ''), substitute(printf('%d', a:last_update), ',', '.', ''), a:targetFile], expand("$HOME/.wakatime.data"))
|
2013-07-02 09:24:04 +00:00
|
|
|
endfunction
|
|
|
|
|
2013-07-08 05:18:47 +00:00
|
|
|
function! s:GetChar()
|
2013-07-02 09:24:04 +00:00
|
|
|
let c = getchar()
|
|
|
|
if c =~ '^\d\+$'
|
|
|
|
let c = nr2char(c)
|
|
|
|
endif
|
|
|
|
return c
|
|
|
|
endfunction
|
2013-07-10 04:13:45 +00:00
|
|
|
|
|
|
|
function! s:EnoughTimePassed(now, last)
|
|
|
|
let prev = a:last[0]
|
|
|
|
if a:now - prev > g:wakatime_ActionFrequency * 60
|
2013-07-08 04:25:06 +00:00
|
|
|
return 1
|
|
|
|
endif
|
|
|
|
return 0
|
2013-07-02 09:24:04 +00:00
|
|
|
endfunction
|
2013-07-08 04:25:06 +00:00
|
|
|
|
2013-06-25 07:57:15 +00:00
|
|
|
" }}}
|
|
|
|
|
2013-06-26 04:09:52 +00:00
|
|
|
|
2013-06-25 07:57:15 +00:00
|
|
|
" Event Handlers {{{
|
|
|
|
|
2013-07-08 04:25:06 +00:00
|
|
|
function! s:normalAction()
|
|
|
|
let targetFile = s:GetCurrentFile()
|
2013-12-16 10:07:28 +00:00
|
|
|
let now = localtime()
|
2013-07-08 04:25:06 +00:00
|
|
|
let last = s:GetLastAction()
|
2013-07-10 04:13:45 +00:00
|
|
|
if s:EnoughTimePassed(now, last) || targetFile != last[2]
|
2013-08-12 10:30:17 +00:00
|
|
|
call s:Api(targetFile, now, 0, last)
|
2013-07-02 09:24:04 +00:00
|
|
|
endif
|
2013-07-08 04:25:06 +00:00
|
|
|
endfunction
|
2013-07-02 09:24:04 +00:00
|
|
|
|
2013-07-08 04:25:06 +00:00
|
|
|
function! s:writeAction()
|
2013-07-10 04:13:45 +00:00
|
|
|
let targetFile = s:GetCurrentFile()
|
2013-12-16 10:07:28 +00:00
|
|
|
let now = localtime()
|
2013-07-10 04:13:45 +00:00
|
|
|
let last = s:GetLastAction()
|
2013-08-12 10:30:17 +00:00
|
|
|
call s:Api(targetFile, now, 1, last)
|
2013-07-02 09:24:04 +00:00
|
|
|
endfunction
|
2013-06-25 07:57:15 +00:00
|
|
|
|
|
|
|
" }}}
|
|
|
|
|
2013-06-26 04:09:52 +00:00
|
|
|
|
2013-06-25 07:57:15 +00:00
|
|
|
" Autocommand Events {{{
|
|
|
|
|
2013-07-02 09:24:04 +00:00
|
|
|
augroup Wakatime
|
|
|
|
autocmd!
|
2013-07-08 04:25:06 +00:00
|
|
|
autocmd BufEnter * call s:normalAction()
|
|
|
|
autocmd VimEnter * call s:normalAction()
|
|
|
|
autocmd BufWritePost * call s:writeAction()
|
|
|
|
autocmd CursorMoved,CursorMovedI * call s:normalAction()
|
2013-07-02 09:24:04 +00:00
|
|
|
augroup END
|
2013-06-25 07:57:15 +00:00
|
|
|
|
|
|
|
" }}}
|
|
|
|
|
2013-06-26 04:09:52 +00:00
|
|
|
|
2013-06-25 07:57:15 +00:00
|
|
|
" Restore cpoptions
|
|
|
|
let &cpo = s:old_cpo
|