2021-01-15 16:46:14 +00:00
|
|
|
" -----------------------------------------------------------------------------
|
|
|
|
" - Vimwiki extensions for working with weekly/monthly/yearly reviews -
|
|
|
|
"
|
|
|
|
" DEPENDS ON VIMWIKI PLUGIN
|
|
|
|
" -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
" Gets path to reviews dir of provided vimwiki (by index)
|
2021-03-11 10:07:37 +00:00
|
|
|
function! GetReviewsDir(vimwiki_index)
|
|
|
|
if a:vimwiki_index == 0
|
|
|
|
let l:current_index = vimwiki#vars#get_bufferlocal('wiki_nr')
|
|
|
|
if (l:current_index < 0)
|
|
|
|
let l:current_index = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
let l:vimwiki = g:vimwiki_list[l:current_index]
|
2021-01-15 16:46:14 +00:00
|
|
|
else
|
2021-03-11 10:07:37 +00:00
|
|
|
let l:vimwiki = g:vimwiki_list[a:vimwiki_index - 1]
|
2021-01-15 16:46:14 +00:00
|
|
|
endif
|
|
|
|
|
|
|
|
return l:vimwiki.path . 'reviews/'
|
|
|
|
endfunction
|
|
|
|
|
2021-01-15 16:58:41 +00:00
|
|
|
" Finds review template path for provided review type
|
2021-03-11 10:07:37 +00:00
|
|
|
function! GetReviewTemplatePath(vimwiki_reviews_path, review_type)
|
2021-01-15 16:58:41 +00:00
|
|
|
return a:vimwiki_reviews_path . 'template-' . a:review_type . '.md'
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
" Edits weekly review template
|
2021-03-11 10:07:37 +00:00
|
|
|
function! OpenReviewWeeklyTemplate(vimwiki_index)
|
|
|
|
let reviews_dir = GetReviewsDir(a:vimwiki_index)
|
|
|
|
execute 'edit ' . GetReviewTemplatePath(l:reviews_dir, 'week')
|
2021-01-15 16:58:41 +00:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
" Edits monthly review template
|
2021-03-11 10:07:37 +00:00
|
|
|
function! OpenReviewMonthlyTemplate(vimwiki_index)
|
|
|
|
let reviews_dir = GetReviewsDir(a:vimwiki_index)
|
|
|
|
execute 'edit ' . GetReviewTemplatePath(l:reviews_dir, 'month')
|
2021-01-15 16:58:41 +00:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
" Edits yearly review template
|
2021-03-11 10:07:37 +00:00
|
|
|
function! OpenReviewYearlyTemplate(vimwiki_index)
|
|
|
|
let reviews_dir = GetReviewsDir(a:vimwiki_index)
|
|
|
|
execute 'edit ' . GetReviewTemplatePath(l:reviews_dir, 'year')
|
2021-01-15 16:58:41 +00:00
|
|
|
endfunction
|
|
|
|
|
2021-01-15 16:46:14 +00:00
|
|
|
" Reads template for provided review type into current buffer
|
|
|
|
" Uses overrides in form of files in reviews directory
|
|
|
|
" Looks for file named template-{review_type}.md:
|
|
|
|
" - template-week.md
|
|
|
|
" - template-month.md
|
|
|
|
" - template-year.md
|
|
|
|
" Templates can use variables using %variable% syntax
|
|
|
|
" Currently supported variables are:
|
|
|
|
" - %date% (Puts different date based on review type)
|
2021-03-11 10:07:37 +00:00
|
|
|
function! ReadReviewTemplateIntoBuffer(vimwiki_reviews_path, review_type)
|
|
|
|
let template_path = GetReviewTemplatePath(a:vimwiki_reviews_path, a:review_type)
|
2021-01-17 21:33:52 +00:00
|
|
|
if filereadable(glob(l:template_path))
|
2021-01-15 16:58:41 +00:00
|
|
|
execute 'read ' . l:template_path
|
2021-01-15 16:46:14 +00:00
|
|
|
else
|
|
|
|
if a:review_type == 'week'
|
2021-01-17 21:33:52 +00:00
|
|
|
call setline(1, '# %date% Weekly Review')
|
2021-01-15 16:46:14 +00:00
|
|
|
elseif a:review_type == 'month'
|
2021-01-17 21:33:52 +00:00
|
|
|
call setline(1, '# %date% Monthly Review')
|
2021-01-15 16:46:14 +00:00
|
|
|
elseif a:review_type == 'year'
|
2021-01-17 21:33:52 +00:00
|
|
|
call setline(1, '# %date% Yearly Review')
|
2021-01-15 16:46:14 +00:00
|
|
|
endif
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
" Open current week weekly review file
|
|
|
|
" Created buffer is dated to Sunday of current week
|
|
|
|
" Opens current week because Sunday is good time to do this review
|
2021-03-11 10:07:37 +00:00
|
|
|
function! VimwikiWeeklyReview(vimwiki_index)
|
|
|
|
let reviews_dir = GetReviewsDir(a:vimwiki_index)
|
2021-01-15 16:46:14 +00:00
|
|
|
let days_to_sunday = 7 - str2nr(strftime('%u'))
|
|
|
|
let week_date = strftime('%Y-%m-%d', localtime() + l:days_to_sunday * 24 * 60 * 60)
|
|
|
|
let file_name = l:reviews_dir . l:week_date . '-week.md'
|
2021-01-17 21:33:52 +00:00
|
|
|
let exists = filereadable(glob(l:file_name))
|
2021-01-15 16:46:14 +00:00
|
|
|
execute 'edit ' . l:file_name
|
|
|
|
if exists == v:false
|
2021-03-11 10:07:37 +00:00
|
|
|
call ReadReviewTemplateIntoBuffer(l:reviews_dir, 'week')
|
2021-01-15 16:46:14 +00:00
|
|
|
execute '%substitute/%date%/' . l:week_date
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
|
|
" Open past month monthly review file
|
|
|
|
" Created buffer is dated to previous month
|
|
|
|
" Previous month is calculated in an erroneous way
|
|
|
|
" 28 days are subtracted from current time to get previous month
|
2021-03-11 10:07:37 +00:00
|
|
|
function! VimwikiMonthlyReview(vimwiki_index)
|
|
|
|
let reviews_dir = GetReviewsDir(a:vimwiki_index)
|
2021-01-15 16:46:14 +00:00
|
|
|
let month_time = localtime() - 28 * 24 * 60 * 60
|
|
|
|
let month_date = strftime('%Y-%m', l:month_time)
|
|
|
|
let file_name = l:reviews_dir . l:month_date .'-month.md'
|
2021-01-17 21:33:52 +00:00
|
|
|
let exists = filereadable(glob(l:file_name))
|
2021-01-15 16:46:14 +00:00
|
|
|
execute 'edit ' . l:file_name
|
|
|
|
if exists == v:false
|
2021-03-11 10:07:37 +00:00
|
|
|
call ReadReviewTemplateIntoBuffer(l:reviews_dir, 'month')
|
2021-01-15 16:46:14 +00:00
|
|
|
execute '%substitute/%date%/' . strftime('%Y %B', l:month_time)
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
" Open past year yearly review file
|
|
|
|
" Created buffer is dated to previous year
|
2021-03-11 10:07:37 +00:00
|
|
|
function! VimwikiYearlyReview(vimwiki_index)
|
|
|
|
let reviews_dir = GetReviewsDir(a:vimwiki_index)
|
2021-01-15 16:46:14 +00:00
|
|
|
let year_date = (str2nr(strftime('%Y')) - 1)
|
|
|
|
let file_name = l:reviews_dir . l:year_date .'-year.md'
|
2021-01-17 21:33:52 +00:00
|
|
|
let exists = filereadable(glob(l:file_name))
|
2021-01-15 16:46:14 +00:00
|
|
|
execute 'edit ' . l:file_name
|
|
|
|
if exists == v:false
|
2021-03-11 10:07:37 +00:00
|
|
|
call ReadReviewTemplateIntoBuffer(l:reviews_dir, 'year')
|
2021-01-15 16:46:14 +00:00
|
|
|
execute '%substitute/%date%/' . l:year_date
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
" Open reviews index file
|
2021-03-11 10:07:37 +00:00
|
|
|
function! VimwikiReviewIndex(vimwiki_index)
|
|
|
|
let reviews_dir = GetReviewsDir(a:vimwiki_index)
|
2021-01-15 16:46:14 +00:00
|
|
|
execute 'edit ' . l:reviews_dir . 'reviews.md'
|
|
|
|
endfunction
|
|
|
|
|
2021-03-11 10:07:37 +00:00
|
|
|
command! -count=0 VimwikiWeeklyReview :call VimwikiWeeklyReview(<count>)
|
|
|
|
command! -count=0 VimwikiWeeklyTemplate :call OpenReviewWeeklyTemplate(<count>)
|
|
|
|
command! -count=0 VimwikiMonthlyReview :call VimwikiMonthlyReview(<count>)
|
|
|
|
command! -count=0 VimwikiMonthlyTemplate :call OpenReviewMonthlyTemplate(<count>)
|
|
|
|
command! -count=0 VimwikiYearlyReview :call VimwikiYearlyReview(<count>)
|
|
|
|
command! -count=0 VimwikiYearlyTemplate :call OpenReviewYearlyTemplate(<count>)
|
|
|
|
command! -count=0 VimwikiReviewIndex :call VimwikiReviewIndex(<count>)
|
2021-01-15 16:46:14 +00:00
|
|
|
|
2021-03-11 10:07:37 +00:00
|
|
|
" TODO - Move functions to autoload
|
|
|
|
nnoremap <Leader>wrw <Cmd>call VimwikiWeeklyReview(v:count)<CR>
|
|
|
|
nnoremap <Leader>wrtw <Cmd>call OpenReviewWeeklyTemplate(v:count)<CR>
|
|
|
|
nnoremap <Leader>wrm <Cmd>call VimwikiMonthlyReview(v:count)<CR>
|
|
|
|
nnoremap <Leader>wrtm <Cmd>call OpenReviewMonthlyTemplate(v:count)<CR>
|
|
|
|
nnoremap <Leader>wry <Cmd>call VimwikiYearlyReview(v:count)<CR>
|
|
|
|
nnoremap <Leader>wrty <Cmd>call OpenReviewYearlyTemplate(v:count)<CR>
|
|
|
|
nnoremap <Leader>wri <Cmd>call VimwikiReviewIndex(v:count)<CR>
|