Add a diary frequency option (#884)
* Add diary_frequency parameter to wikis * Create diary entries according to the frequency * Update date validation * Use timestamps, extract abstract yesterday and today as day-long periods * Revert old changes * Remove debug log * Start the week any day * Add monthly and yearly options * Cleanup
This commit is contained in:
		| @@ -38,10 +38,56 @@ endfunction | |||||||
| " Return: <String> date | " Return: <String> date | ||||||
| function! vimwiki#diary#diary_date_link(...) abort | function! vimwiki#diary#diary_date_link(...) abort | ||||||
|   if a:0 |   if a:0 | ||||||
|     return strftime('%Y-%m-%d', a:1) |     let l:timestamp = a:1 | ||||||
|   else |   else | ||||||
|     return strftime('%Y-%m-%d') |     let l:timestamp = localtime() | ||||||
|   endif |   endif | ||||||
|  |  | ||||||
|  |   let l:delta_periods = 0 | ||||||
|  |   if a:0 > 1 | ||||||
|  |     let l:delta_periods = a:2 | ||||||
|  |   endif | ||||||
|  |  | ||||||
|  |   let l:day_s = 60*60*24 | ||||||
|  |  | ||||||
|  |   let l:weekday_number = { | ||||||
|  |         \ 'monday': 1, 'tuesday': 2, | ||||||
|  |         \ 'wednesday': 3, 'thursday': 4, | ||||||
|  |         \ 'friday': 5, 'saturday': 6, | ||||||
|  |         \ 'sunday': 0} | ||||||
|  |  | ||||||
|  |   let l:frequency = vimwiki#vars#get_wikilocal('diary_frequency') | ||||||
|  |  | ||||||
|  |   if l:frequency == "weekly" | ||||||
|  |     let l:start_week_day = vimwiki#vars#get_wikilocal('diary_start_week_day') | ||||||
|  |     let l:weekday_num = str2nr(strftime("%w", l:timestamp)) | ||||||
|  |     let l:days_to_end_of_week = (7-l:weekday_number[l:start_week_day]+weekday_num) % 7 | ||||||
|  |     let l:computed_timestamp = l:timestamp | ||||||
|  |           \ + 7*l:day_s*l:delta_periods | ||||||
|  |           \ - l:day_s*l:days_to_end_of_week | ||||||
|  |  | ||||||
|  |   elseif l:frequency == "monthly" | ||||||
|  |     let l:day_of_month = str2nr(strftime("%d", l:timestamp)) | ||||||
|  |     let l:beginning_of_month = l:timestamp - (l:day_of_month - 1)*l:day_s | ||||||
|  |     let l:middle_of_month = l:beginning_of_month + 15*l:day_s | ||||||
|  |     let l:middle_of_computed_month = l:middle_of_month + float2nr(30.5*l:day_s*l:delta_periods) | ||||||
|  |     let l:day_of_computed_month = str2nr(strftime("%d", l:middle_of_computed_month)) - 1 | ||||||
|  |     let l:computed_timestamp = l:middle_of_computed_month - l:day_of_computed_month*l:day_s | ||||||
|  |  | ||||||
|  |   elseif l:frequency == "yearly" | ||||||
|  |     let l:day_of_year = str2nr(strftime("%j", l:timestamp)) | ||||||
|  |     let l:beginning_of_year = l:timestamp - (l:day_of_year - 1)*l:day_s | ||||||
|  |     let l:middle_of_year = l:beginning_of_year + float2nr(365.25/2*l:day_s) | ||||||
|  |     let l:middle_of_computed_year = l:middle_of_year + float2nr(365.25*l:day_s*l:delta_periods) | ||||||
|  |     let l:day_of_computed_year = str2nr(strftime("%j", l:middle_of_computed_year)) - 1 | ||||||
|  |     let l:computed_timestamp = l:middle_of_computed_year - l:day_of_computed_year*l:day_s | ||||||
|  |  | ||||||
|  |   else "daily | ||||||
|  |     let l:computed_timestamp = localtime() + l:delta_periods*l:day_s | ||||||
|  |   endif | ||||||
|  |  | ||||||
|  |   return strftime('%Y-%m-%d', l:computed_timestamp) | ||||||
|  |  | ||||||
| endfunction | endfunction | ||||||
|  |  | ||||||
|  |  | ||||||
|   | |||||||
| @@ -363,6 +363,8 @@ function! s:populate_wikilocal_options() abort | |||||||
|         \ 'css_name': {'type': type(''), 'default': 'style.css', 'min_length': 1}, |         \ 'css_name': {'type': type(''), 'default': 'style.css', 'min_length': 1}, | ||||||
|         \ 'custom_wiki2html': {'type': type(''), 'default': ''}, |         \ 'custom_wiki2html': {'type': type(''), 'default': ''}, | ||||||
|         \ 'custom_wiki2html_args': {'type': type(''), 'default': ''}, |         \ 'custom_wiki2html_args': {'type': type(''), 'default': ''}, | ||||||
|  |         \ 'diary_frequency': {'type': type(''), 'default': 'daily', 'possible_values': ['daily', 'weekly', 'monthly', 'yearly']}, | ||||||
|  |         \ 'diary_start_week_day': {'type': type(''), 'default': 'monday', 'possible_values': ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']}, | ||||||
|         \ 'diary_header': {'type': type(''), 'default': 'Diary', 'min_length': 1}, |         \ 'diary_header': {'type': type(''), 'default': 'Diary', 'min_length': 1}, | ||||||
|         \ 'diary_index': {'type': type(''), 'default': 'diary', 'min_length': 1}, |         \ 'diary_index': {'type': type(''), 'default': 'diary', 'min_length': 1}, | ||||||
|         \ 'diary_rel_path': {'type': type(''), 'default': 'diary/', 'min_length': 0}, |         \ 'diary_rel_path': {'type': type(''), 'default': 'diary/', 'min_length': 0}, | ||||||
|   | |||||||
| @@ -1908,10 +1908,11 @@ Example of diary section: > | |||||||
|         * [[2011-12-09]] |         * [[2011-12-09]] | ||||||
|         * [[2011-12-08]] |         * [[2011-12-08]] | ||||||
|  |  | ||||||
|  | The diary can be used with a frequency other than daily. See the appropriate | ||||||
|  | per-wiki options. | ||||||
|  |  | ||||||
| See |g:vimwiki_diary_months| if you would like to rename months. | See |g:vimwiki_diary_months| if you would like to rename months. | ||||||
|  |  | ||||||
|  |  | ||||||
| ------------------------------------------------------------------------------ | ------------------------------------------------------------------------------ | ||||||
| Calendar integration                                        *vimwiki-calendar* | Calendar integration                                        *vimwiki-calendar* | ||||||
|  |  | ||||||
| @@ -2451,6 +2452,35 @@ Possible values: | |||||||
| When the value is >= 1, the primary caption of each diary page is set to the | When the value is >= 1, the primary caption of each diary page is set to the | ||||||
| first header read from that page if it is the unique lowest-level header. | first header read from that page if it is the unique lowest-level header. | ||||||
|  |  | ||||||
|  |  | ||||||
|  | *vimwiki-option-diary_frequency* | ||||||
|  | ------------------------------------------------------------------------------ | ||||||
|  | Key               Default value~ | ||||||
|  | diary_frequency   daily | ||||||
|  |  | ||||||
|  | Description~ | ||||||
|  | Controls the diary frequency used to create the date for which a diary entry | ||||||
|  | is created. | ||||||
|  |  | ||||||
|  | Possible values: | ||||||
|  |   daily:  Create a diary entry dated for each day. | ||||||
|  |  weekly:  Create a diary entry dated for the beginning of each week. | ||||||
|  | monthly:  Create a diary entry dated for the beginning of each month. | ||||||
|  |  yearly:  Create a diary entry dated for the beginning of each year. | ||||||
|  |  | ||||||
|  |  | ||||||
|  | *vimwiki-option-diary_start_week_day* | ||||||
|  | ------------------------------------------------------------------------------ | ||||||
|  | Key                   Default value~ | ||||||
|  | diary_start_week_day  monday | ||||||
|  |  | ||||||
|  | Description~ | ||||||
|  | Set the day to begin each week. | ||||||
|  |  | ||||||
|  | Possible values: | ||||||
|  | monday, tuesday, wednesday, thursday, friday, saturday, sunday | ||||||
|  |  | ||||||
|  |  | ||||||
| *vimwiki-option-custom_wiki2html* | *vimwiki-option-custom_wiki2html* | ||||||
| ------------------------------------------------------------------------------ | ------------------------------------------------------------------------------ | ||||||
| Key               Default value~ | Key               Default value~ | ||||||
| @@ -3681,6 +3711,7 @@ New:~ | |||||||
|     * PR #47: Optimize table formatting for large tables. |     * PR #47: Optimize table formatting for large tables. | ||||||
|     * PR #857: Make default template responsive |     * PR #857: Make default template responsive | ||||||
|     * PR #879: Generate links when diary & wiki dir are the same |     * PR #879: Generate links when diary & wiki dir are the same | ||||||
|  |     * PR #884: Configure diary frequency (daily, weekly, monthly, yearly) | ||||||
|  |  | ||||||
| Changed:~ | Changed:~ | ||||||
|     * Issue #796: Rename |:VimwikiGenerateTags| to |:VimwikiGenerateTagLinks| |     * Issue #796: Rename |:VimwikiGenerateTags| to |:VimwikiGenerateTagLinks| | ||||||
|   | |||||||
| @@ -369,11 +369,11 @@ command! -count=0 VimwikiTabMakeDiaryNote | |||||||
|  |  | ||||||
| command! -count=0 VimwikiMakeYesterdayDiaryNote | command! -count=0 VimwikiMakeYesterdayDiaryNote | ||||||
|       \ call vimwiki#diary#make_note(<count>, 0, |       \ call vimwiki#diary#make_note(<count>, 0, | ||||||
|       \ vimwiki#diary#diary_date_link(localtime() - 60*60*24)) |       \ vimwiki#diary#diary_date_link(localtime(), -1)) | ||||||
|  |  | ||||||
| command! -count=0 VimwikiMakeTomorrowDiaryNote | command! -count=0 VimwikiMakeTomorrowDiaryNote | ||||||
|       \ call vimwiki#diary#make_note(<count>, 0, |       \ call vimwiki#diary#make_note(<count>, 0, | ||||||
|       \ vimwiki#diary#diary_date_link(localtime() + 60*60*24)) |       \ vimwiki#diary#diary_date_link(localtime(), 1)) | ||||||
|  |  | ||||||
| command! VimwikiDiaryGenerateLinks | command! VimwikiDiaryGenerateLinks | ||||||
|       \ call vimwiki#diary#generate_diary_section() |       \ call vimwiki#diary#generate_diary_section() | ||||||
| @@ -399,10 +399,10 @@ nnoremap <silent><script> <Plug>VimwikiTabMakeDiaryNote | |||||||
|     \ :<C-U>call vimwiki#diary#make_note(v:count, 1)<CR> |     \ :<C-U>call vimwiki#diary#make_note(v:count, 1)<CR> | ||||||
| nnoremap <silent><script> <Plug>VimwikiMakeYesterdayDiaryNote | nnoremap <silent><script> <Plug>VimwikiMakeYesterdayDiaryNote | ||||||
|     \ :<C-U>call vimwiki#diary#make_note(v:count, 0, |     \ :<C-U>call vimwiki#diary#make_note(v:count, 0, | ||||||
|     \ vimwiki#diary#diary_date_link(localtime() - 60*60*24))<CR> |     \ vimwiki#diary#diary_date_link(localtime(), -1))<CR> | ||||||
| nnoremap <silent><script> <Plug>VimwikiMakeTomorrowDiaryNote | nnoremap <silent><script> <Plug>VimwikiMakeTomorrowDiaryNote | ||||||
|     \ :<C-U>call vimwiki#diary#make_note(v:count, 0, |     \ :<C-U>call vimwiki#diary#make_note(v:count, 0, | ||||||
|     \ vimwiki#diary#diary_date_link(localtime() + 60*60*24))<CR> |     \ vimwiki#diary#diary_date_link(localtime(), 1))<CR> | ||||||
|  |  | ||||||
| " Get the user defined prefix (default <leader>w) | " Get the user defined prefix (default <leader>w) | ||||||
| let s:map_prefix = vimwiki#vars#get_global('map_prefix') | let s:map_prefix = vimwiki#vars#get_global('map_prefix') | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user