vim-rana/plugin/wakatime.vim

269 lines
8.8 KiB
VimL
Raw Normal View History

2013-06-25 07:57:15 +00:00
" ============================================================================
" File: wakatime.vim
" 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.
" Website: https://wakatime.com/
2013-06-25 07:57:15 +00:00
" ============================================================================
2017-02-21 00:28:01 +00:00
let s:VERSION = '4.0.14'
2013-06-26 04:09:52 +00:00
2013-06-25 07:57:15 +00:00
" Init {{{
" Check Vim version
if v:version < 700
echoerr "This plugin requires vim >= 7."
finish
2013-06-25 07:57:15 +00:00
endif
" Use constants for truthy check to improve readability
let s:true = 1
let s:false = 0
" Only load plugin once
if exists("g:loaded_wakatime")
finish
endif
let g:loaded_wakatime = s:true
2013-06-25 07:57:15 +00:00
" Backup & Override cpoptions
let s:old_cpo = &cpo
set cpo&vim
2013-06-25 07:57:15 +00:00
" Script Globals
2015-05-21 03:15:23 +00:00
let s:cli_location = expand("<sfile>:p:h") . '/packages/wakatime/cli.py'
let s:config_file = expand("$HOME/.wakatime.cfg")
let s:data_file = expand("$HOME/.wakatime.data")
let s:config_file_already_setup = s:false
let s:debug_mode_already_setup = s:false
let s:is_debug_mode_on = s:false
let s:local_cache_expire = 10 " seconds between reading s:data_file
let s:last_heartbeat = [0, 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
endif
endif
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 {{{
function! s:StripWhitespace(str)
return substitute(a:str, '^\s*\(.\{-}\)\s*$', '\1', '')
endfunction
2017-02-14 07:39:06 +00:00
function! s:SetupConfigFile()
if !s:config_file_already_setup
" Create config file if does not exist
if !filereadable(s:config_file)
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)
echo "[WakaTime] Setup complete! Visit http://wakatime.com to view your logged time."
endif
2015-05-03 16:52:49 +00:00
" Make sure config file has api_key
else
let found_api_key = s:false
if s:GetIniSetting('settings', 'api_key') != '' || s:GetIniSetting('settings', 'apikey') != ''
let found_api_key = s:true
endif
2015-05-03 16:52:49 +00:00
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)
echo "[WakaTime] Setup complete! Visit http://wakatime.com to view your logged time."
2015-05-03 16:52:49 +00:00
endif
endif
let s:config_file_already_setup = s:true
endif
endfunction
function! s:SetupDebugMode()
if !s:debug_mode_already_setup
if s:GetIniSetting('settings', 'debug') == 'true'
let s:is_debug_mode_on = s:true
else
let s:is_debug_mode_on = s:false
endif
let s:debug_mode_already_setup = s:true
endif
endfunction
function! s:GetIniSetting(section, key)
if !filereadable(s:config_file)
return ''
endif
let lines = readfile(s:config_file)
let currentSection = ''
for line in lines
let line = s:StripWhitespace(line)
if matchstr(line, '^\[') != '' && matchstr(line, '\]$') != ''
let currentSection = substitute(line, '^\[\(.\{-}\)\]$', '\1', '')
else
if currentSection == a:section
let group = split(line, '=')
if len(group) == 2 && s:StripWhitespace(group[0]) == a:key
return s:StripWhitespace(group[1])
endif
endif
endif
endfor
return ''
endfunction
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
function! s:IsWindows()
if has('win32') || has('win64')
return s:true
endif
return s:false
endfunction
function! s:SendHeartbeat(file, time, is_write, last)
let file = a:file
if file == ''
let file = a:last[2]
endif
if file != ''
2015-02-13 03:19:40 +00:00
let python_bin = g:wakatime_PythonBinary
if s:IsWindows()
2015-02-13 03:19:40 +00:00
if python_bin == 'python'
let python_bin = 'pythonw'
endif
endif
2015-06-11 06:38:54 +00:00
let cmd = [python_bin, '-W', 'ignore', s:cli_location]
2016-04-28 22:16:26 +00:00
let cmd = cmd + ['--entity', file]
2015-06-11 06:38:54 +00:00
let cmd = cmd + ['--plugin', printf('vim/%d vim-wakatime/%s', v:version, s:VERSION)]
if a:is_write
let cmd = cmd + ['--write']
endif
if !empty(&syntax)
2017-02-21 00:23:13 +00:00
let cmd = cmd + ['--language', &syntax]
else
if !empty(&filetype)
2017-02-21 00:23:13 +00:00
let cmd = cmd + ['--language', &filetype]
endif
endif
if s:is_debug_mode_on
echo 'Sending Heartbeat with Command: ' . s:JoinArgs(cmd)
endif
if s:IsWindows()
2015-06-11 06:38:54 +00:00
exec 'silent !start /min cmd /c "' . s:JoinArgs(cmd) . '"'
else
let stdout = system(s:JoinArgs(cmd) . ' &')
endif
call s:SetLastHeartbeat(a:time, a:time, file)
endif
endfunction
2017-02-14 07:39:06 +00:00
function! s:GetLastHeartbeat()
if !s:last_heartbeat[0] || localtime() - s:last_heartbeat[0] > s:local_cache_expire
if !filereadable(s:data_file)
return [0, 0, '']
endif
let last = readfile(s:data_file, '', 3)
if len(last) == 3
let s:last_heartbeat = [s:last_heartbeat[0], last[1], last[2]]
endif
endif
return s:last_heartbeat
endfunction
2017-02-14 07:39:06 +00:00
function! s:SetLastHeartbeatLocally(time, last_update, file)
let s:last_heartbeat = [a:time, a:last_update, a:file]
endfunction
2017-02-14 07:39:06 +00:00
function! s:SetLastHeartbeat(time, last_update, file)
call s:SetLastHeartbeatLocally(a:time, a:last_update, a:file)
call writefile([substitute(printf('%d', a:time), ',', '.', ''), substitute(printf('%d', a:last_update), ',', '.', ''), a:file], s:data_file)
endfunction
function! s:EnoughTimePassed(now, last)
let prev = a:last[1]
2015-02-13 03:19:40 +00:00
if a:now - prev > g:wakatime_HeartbeatFrequency * 60
return s:true
endif
return s:false
endfunction
2017-02-14 07:39: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 {{{
function! s:handleActivity(is_write)
call s:SetupDebugMode()
call s:SetupConfigFile()
let file = s:GetCurrentFile()
let now = localtime()
let last = s:GetLastHeartbeat()
if !empty(file) && file !~ "-MiniBufExplorer-" && file !~ "--NO NAME--" && file !~ "^term:"
if a:is_write || s:EnoughTimePassed(now, last) || file != last[2]
call s:SendHeartbeat(file, now, a:is_write, last)
else
if now - s:last_heartbeat[0] > s:local_cache_expire
call s:SetLastHeartbeatLocally(now, last[1], last[2])
endif
endif
endif
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 {{{
augroup Wakatime
autocmd!
autocmd BufEnter * call s:handleActivity(s:false)
autocmd VimEnter * call s:handleActivity(s:false)
autocmd BufWritePost * call s:handleActivity(s:true)
autocmd CursorMoved,CursorMovedI * call s:handleActivity(s:false)
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