vim-rana-local/plugin/wakatime.vim

223 lines
5.3 KiB
VimL
Raw Normal View History

2013-06-25 07:57:15 +00:00
" ============================================================================
" File: wakatime.vim
" Description: invisible time tracker using Wakati.Me
" Maintainer: Wakati.Me <support@wakatime.com>
2013-06-25 08:23:55 +00:00
" Version: 0.1.2
2013-06-25 07:57:15 +00:00
" ============================================================================
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
endif
" Check for Python support
if !has('python')
echoerr "This plugin requires Vim to be compiled with Python support."
finish
endif
" Check for required user-defined settings
if !exists("g:wakatime_api_key")
if filereadable(expand("$HOME/.wakatime"))
for s:line in readfile(expand("$HOME/.wakatime"))
let s:setting = split(s:line, "=")
if s:setting[0] == "api_key"
let g:wakatime_api_key = s:setting[1]
endif
endfor
endif
if !exists("g:wakatime_api_key")
finish
endif
endif
" Only load plugin once
if exists("g:loaded_wakatime")
finish
endif
let g:loaded_wakatime = 1
" Backup & Override cpoptions
let s:old_cpo = &cpo
set cpo&vim
let s:plugin_directory = expand("<sfile>:p:h")
2013-06-26 04:09:52 +00:00
" Set default updatetime
if !exists("g:wakatime_updatetime")
let g:wakatime_updatetime = 15 " 15 minutes
2013-06-25 07:57:15 +00:00
endif
" We are not away until getting a CursorHold event
let s:away_start = 0
2013-06-25 07:57:15 +00:00
python << ENDPYTHON
import vim
import uuid
import time
instance_id = str(uuid.uuid4())
vim.command('let s:instance_id = "%s"' % instance_id)
ENDPYTHON
" }}}
2013-06-26 04:09:52 +00:00
2013-06-25 07:57:15 +00:00
" Function Definitions {{{
function! s:initVariable(var, value)
if !exists(a:var)
exec 'let ' . a:var . ' = ' . "'" . substitute(a:value, "'", "''", "g") . "'"
return 1
endif
return 0
endfunction
2013-06-26 04:09:52 +00:00
function! s:setUpdateTime()
if &updatetime < 60 * 1000 * 2
let &updatetime = g:wakatime_updatetime * 60 * 1000
endif
endfunction
2013-06-25 07:57:15 +00:00
function! s:GetCurrentFile()
return expand("%:p")
endfunction
function! s:api(type, task)
if a:task != ''
exec "silent !python " . s:plugin_directory . "/wakatime.py --key" g:wakatime_api_key "--instance" s:instance_id "--action" a:type "--task" shellescape(a:task) . " &"
endif
2013-06-25 07:57:15 +00:00
endfunction
function! s:api_with_time(type, task, time)
if a:task != ''
exec "silent !python " . s:plugin_directory . "/wakatime.py --key" g:wakatime_api_key "--instance" s:instance_id "--action" a:type "--task" shellescape(a:task) "--time" printf("%f", a:time) . " &"
endif
2013-06-25 07:57:15 +00:00
endfunction
function! s:getchar()
let c = getchar()
if c =~ '^\d\+$'
let c = nr2char(c)
endif
return c
endfunction
function! Wakatime_isAway()
return s:away_start
endfunction
function! s:allServersAway()
if has('clientserver')
let servers = split(serverlist())
for server in servers
if server != v:servername
if !remote_expr(server,'Wakatime_isAway()')
return 0
endif
endif
endfor
endif
return 1
2013-06-25 07:57:15 +00:00
endfunction
" }}}
2013-06-26 04:09:52 +00:00
2013-06-25 07:57:15 +00:00
" Event Handlers {{{
function! s:bufenter()
let task = s:GetCurrentFile()
call s:api("open_file", task)
2013-06-25 07:57:15 +00:00
endfunction
function! s:bufleave()
let task = s:GetCurrentFile()
call s:api("close_file", task)
2013-06-25 07:57:15 +00:00
endfunction
function! s:vimenter()
let task = s:GetCurrentFile()
call s:api("open_editor", task)
2013-06-25 07:57:15 +00:00
endfunction
function! s:vimleave()
let task = s:GetCurrentFile()
call s:api("quit_editor", task)
2013-06-25 07:57:15 +00:00
endfunction
function! s:bufwrite()
let task = s:GetCurrentFile()
call s:api("write_file", task)
2013-06-25 07:57:15 +00:00
endfunction
function! s:cursorhold()
let s:away_task = s:GetCurrentFile()
python vim.command("let s:away_start=%f" % (time.time() - (float(vim.eval("&updatetime")) / 1000.0)))
autocmd Wakatime CursorMoved,CursorMovedI * call s:cursormoved()
endfunction
function! s:cursormoved()
autocmd! Wakatime CursorMoved,CursorMovedI *
" Don't do anything unless all other Vim instances are also away
if !s:allServersAway()
let s:away_start = 0
call s:api("ping", s:away_task)
return
endif
2013-06-25 07:57:15 +00:00
python vim.command("let away_end=%f" % time.time())
let away_unit = "minutes"
let away_duration = (away_end - s:away_start) / 60
2013-06-26 04:09:52 +00:00
if away_duration < 2
call s:setUpdateTime()
return
endif
2013-06-25 07:57:15 +00:00
if away_duration > 59
let away_duration = away_duration / 60
let away_unit = "hours"
endif
if away_duration > 59
let away_duration = away_duration / 60
let away_unit = "days"
endif
let answer = input(printf("You were away %.f %s. Add time to current file? (y/n)", away_duration, away_unit))
if answer != "y"
call s:api_with_time("minimize_editor", s:away_task, s:away_start)
call s:api_with_time("maximize_editor", s:away_task, away_end)
2013-06-25 07:57:15 +00:00
else
call s:api("ping", s:away_task)
2013-06-25 07:57:15 +00:00
endif
let s:away_start = 0
2013-06-25 07:57:15 +00:00
"redraw!
endfunction
" }}}
2013-06-26 04:09:52 +00:00
call s:setUpdateTime()
2013-06-25 07:57:15 +00:00
" Autocommand Events {{{
augroup Wakatime
autocmd!
autocmd BufEnter * call s:bufenter()
autocmd BufLeave * call s:bufleave()
autocmd VimEnter * call s:vimenter()
autocmd VimLeave * call s:vimleave()
autocmd BufWritePost * call s:bufwrite()
autocmd CursorHold,CursorHoldI * call s:cursorhold()
augroup END
" }}}
2013-06-26 04:09:52 +00:00
2013-06-25 07:57:15 +00:00
" Restore cpoptions
let &cpo = s:old_cpo