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
|
|
|
" ============================================================================
|
|
|
|
|
2015-06-11 21:17:20 +00:00
|
|
|
let s:VERSION = '4.0.2'
|
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
|
|
|
|
2015-05-01 23:08:02 +00:00
|
|
|
" Globals
|
2015-05-21 03:15:23 +00:00
|
|
|
let s:cli_location = expand("<sfile>:p:h") . '/packages/wakatime/cli.py'
|
2015-05-01 23:08:02 +00:00
|
|
|
let s:config_file = expand("$HOME/.wakatime.cfg")
|
|
|
|
let s:config_file_already_setup = 0
|
|
|
|
|
|
|
|
" For backwards compatibility, rename wakatime.conf to wakatime.cfg
|
|
|
|
if !filereadable(s:config_file)
|
|
|
|
if filereadable(expand("$HOME/.wakatime"))
|
|
|
|
exec "silent !mv" expand("$HOME/.wakatime") expand("$HOME/.wakatime.conf")
|
|
|
|
endif
|
|
|
|
if filereadable(expand("$HOME/.wakatime.conf"))
|
|
|
|
if !filereadable(s:config_file)
|
|
|
|
let contents = ['[settings]'] + readfile(expand("$HOME/.wakatime.conf"), '')
|
|
|
|
call writefile(contents, s:config_file)
|
|
|
|
call delete(expand("$HOME/.wakatime.conf"))
|
|
|
|
endif
|
2013-12-13 15:03:09 +00:00
|
|
|
endif
|
|
|
|
endif
|
2013-06-26 05:02:59 +00:00
|
|
|
|
2015-02-13 03:19:40 +00:00
|
|
|
" Set default python binary location
|
|
|
|
if !exists("g:wakatime_PythonBinary")
|
|
|
|
let g:wakatime_PythonBinary = 'python'
|
|
|
|
endif
|
|
|
|
|
|
|
|
" Set default heartbeat frequency in minutes
|
|
|
|
if !exists("g:wakatime_HeartbeatFrequency")
|
|
|
|
let g:wakatime_HeartbeatFrequency = 2
|
2013-08-12 10:30:17 +00:00
|
|
|
endif
|
|
|
|
|
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 {{{
|
|
|
|
|
2015-05-01 23:08:02 +00:00
|
|
|
function! s:StripWhitespace(str)
|
|
|
|
return substitute(a:str, '^\s*\(.\{-}\)\s*$', '\1', '')
|
|
|
|
endfunction
|
|
|
|
|
2015-01-20 04:36:23 +00:00
|
|
|
function! s:SetupConfigFile()
|
2015-05-01 23:08:02 +00:00
|
|
|
if !s:config_file_already_setup
|
|
|
|
|
2015-01-20 04:36:23 +00:00
|
|
|
" Create config file if does not exist
|
2015-05-01 23:08:02 +00:00
|
|
|
if !filereadable(s:config_file)
|
2015-01-20 04:36:23 +00:00
|
|
|
let key = input("[WakaTime] Enter your wakatime.com api key: ")
|
|
|
|
if key != ''
|
2015-05-06 16:18:54 +00:00
|
|
|
call writefile(['[settings]', 'debug = false', printf("api_key = %s", key), 'hidefilenames = false', 'ignore =', ' COMMIT_EDITMSG$', ' PULLREQ_EDITMSG$', ' MERGE_MSG$', ' TAG_EDITMSG$'], s:config_file)
|
2015-05-04 05:10:32 +00:00
|
|
|
echo "[WakaTime] Setup complete! Visit http://wakatime.com to view your logged time."
|
2015-05-01 23:08:02 +00:00
|
|
|
endif
|
2015-05-03 16:52:49 +00:00
|
|
|
|
|
|
|
" Make sure config file has api_key
|
|
|
|
else
|
|
|
|
let found_api_key = 0
|
|
|
|
let lines = readfile(s:config_file)
|
|
|
|
for line in lines
|
|
|
|
let group = split(line, '=')
|
|
|
|
if len(group) == 2 && s:StripWhitespace(group[0]) == 'api_key' && s:StripWhitespace(group[1]) != ''
|
|
|
|
let found_api_key = 1
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
if !found_api_key
|
|
|
|
let key = input("[WakaTime] Enter your wakatime.com api key: ")
|
|
|
|
let lines = lines + [join(['api_key', key], '=')]
|
|
|
|
call writefile(lines, s:config_file)
|
2015-05-04 05:10:32 +00:00
|
|
|
echo "[WakaTime] Setup complete! Visit http://wakatime.com to view your logged time."
|
2015-05-03 16:52:49 +00:00
|
|
|
endif
|
2015-01-20 04:36:23 +00:00
|
|
|
endif
|
2015-05-01 23:08:02 +00:00
|
|
|
|
|
|
|
let s:config_file_already_setup = 1
|
2015-01-20 04:36:23 +00:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2013-07-02 09:24:04 +00:00
|
|
|
function! s:GetCurrentFile()
|
|
|
|
return expand("%:p")
|
|
|
|
endfunction
|
|
|
|
|
2015-06-11 06:38:54 +00:00
|
|
|
function! s:EscapeArg(arg)
|
|
|
|
return substitute(shellescape(a:arg), '!', '\\!', '')
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:JoinArgs(args)
|
|
|
|
let safeArgs = []
|
|
|
|
for arg in a:args
|
|
|
|
let safeArgs = safeArgs + [s:EscapeArg(arg)]
|
|
|
|
endfor
|
|
|
|
return join(safeArgs, ' ')
|
|
|
|
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 != ''
|
2015-02-13 03:19:40 +00:00
|
|
|
let python_bin = g:wakatime_PythonBinary
|
2014-05-29 06:14:25 +00:00
|
|
|
if has('win32') || has('win64')
|
2015-02-13 03:19:40 +00:00
|
|
|
if python_bin == 'python'
|
|
|
|
let python_bin = 'pythonw'
|
|
|
|
endif
|
2014-05-27 05:39:37 +00:00
|
|
|
endif
|
2015-06-11 06:38:54 +00:00
|
|
|
let cmd = [python_bin, '-W', 'ignore', s:cli_location]
|
|
|
|
let cmd = cmd + ['--file', targetFile]
|
|
|
|
let cmd = cmd + ['--plugin', printf('vim/%d vim-wakatime/%s', v:version, 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')
|
2015-06-11 06:38:54 +00:00
|
|
|
exec 'silent !start /min cmd /c "' . s:JoinArgs(cmd) . '"'
|
2014-05-27 05:39:37 +00:00
|
|
|
else
|
2015-06-11 06:38:54 +00:00
|
|
|
exec 'silent !' . s:JoinArgs(cmd) . ' &'
|
2014-05-27 05:39:37 +00:00
|
|
|
endif
|
2015-05-01 23:08:02 +00:00
|
|
|
call s:SetLastHeartbeat(a:time, a:time, targetFile)
|
2013-07-08 04:25:06 +00:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2015-05-01 23:08:02 +00:00
|
|
|
function! s:GetLastHeartbeat()
|
2013-07-08 04:25:06 +00:00
|
|
|
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
|
|
|
|
|
2015-05-01 23:08:02 +00:00
|
|
|
function! s:SetLastHeartbeat(time, last_update, targetFile)
|
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-10 04:13:45 +00:00
|
|
|
function! s:EnoughTimePassed(now, last)
|
|
|
|
let prev = a:last[0]
|
2015-02-13 03:19:40 +00:00
|
|
|
if a:now - prev > g:wakatime_HeartbeatFrequency * 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 {{{
|
|
|
|
|
2015-05-01 23:08:02 +00:00
|
|
|
function! s:handleActivity(is_write)
|
2015-01-20 04:36:23 +00:00
|
|
|
call s:SetupConfigFile()
|
2013-07-08 04:25:06 +00:00
|
|
|
let targetFile = s:GetCurrentFile()
|
2013-12-16 10:07:28 +00:00
|
|
|
let now = localtime()
|
2015-05-01 23:08:02 +00:00
|
|
|
let last = s:GetLastHeartbeat()
|
2015-05-06 16:49:33 +00:00
|
|
|
if targetFile !~ "-MiniBufExplorer-" && targetFile !~ "--NO NAME--" && targetFile != ""
|
|
|
|
if a:is_write || s:EnoughTimePassed(now, last) || targetFile != last[2]
|
|
|
|
call s:Api(targetFile, now, a:is_write, last)
|
|
|
|
endif
|
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-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!
|
2015-05-01 23:08:02 +00:00
|
|
|
autocmd BufEnter * call s:handleActivity(0)
|
|
|
|
autocmd VimEnter * call s:handleActivity(0)
|
|
|
|
autocmd BufWritePost * call s:handleActivity(1)
|
|
|
|
autocmd CursorMoved,CursorMovedI * call s:handleActivity(0)
|
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
|