Skip to content

Commit 82535db

Browse files
committed
add :UpdateTaskLists and insert filters by :InsertTasks
1 parent c04d95e commit 82535db

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

autoload/vimwiki_tasks.vim

+34-2
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ function! vimwiki_tasks#read()
8686
endif
8787
let l:i += 1
8888
endwhile
89+
if vimwiki_tasks#config('update_tasklists', 1)
90+
call vimwiki_tasks#update_task_lists()
91+
endif
8992
endfunction
9093

9194
function! vimwiki_tasks#get_defaults()
@@ -366,7 +369,11 @@ function! s:UuidsInBuffer()
366369
return l:uuids
367370
endfunction
368371

369-
function! vimwiki_tasks#insert_tasks(filter, bang)
372+
" a:1 boolean, if 1 the tasklist filter will be inserted, otherwise not (not
373+
" that the config parameter is also taken into account, but it can be
374+
" overriden by setting a:1 to a value other than 1. This case is used during
375+
" the updating of the taskslists where the filter should not be inserted again
376+
function! vimwiki_tasks#insert_tasks(filter, bang, line, ...)
370377
echo "Loading tasks..."
371378
redraw
372379
let l:report = vimwiki_tasks#config('report', 'all')
@@ -384,15 +391,24 @@ function! vimwiki_tasks#insert_tasks(filter, bang)
384391
call add(l:lines, l:line)
385392
endif
386393
endfor
394+
let l:num_added = 0
387395
if len(l:lines) > 0
388-
call append(line('.'), l:lines)
396+
" XXX: check if current line is empty and replace it, otherwise
397+
" append?
398+
if vimwiki_tasks#config('include_tasklist', 1) && (a:0 == 0 || a:0 > 0 && a:1 == 1)
399+
let l:num_added += 1
400+
" TODO: add correct indent?
401+
call append(a:line, '%% TaskList: '.a:filter)
402+
endif
403+
call append(a:line + l:num_added, l:lines)
389404
" fix the indent of the new lines
390405
exec "normal! ".(len(l:lines)+1)."=="
391406
redraw " get rid of the 'XX lines indented' message
392407
" and set the '[ to the first new line
393408
normal jm[
394409
endif
395410
echo "Inserted ".len(l:lines)." task(s)"
411+
return len(l:lines) + l:num_added
396412
endfunction
397413

398414
function! vimwiki_tasks#current_task_do(task_cmd)
@@ -413,3 +429,19 @@ function! vimwiki_tasks#current_task_do(task_cmd)
413429
echo "Could not find a task on this line!"
414430
endif
415431
endfunction
432+
433+
function! vimwiki_tasks#update_task_lists()
434+
let l:i = 1
435+
while l:i <= line('$')
436+
let l:line = getline(l:i)
437+
let l:tasklist = matchstr(l:line, '\v\%\%\s*TaskList:\s*\zs(.*$)')
438+
if l:tasklist != ''
439+
" ok we found a tasklist, try to add new tasks
440+
let l:num_added = 0
441+
let l:num_added += vimwiki_tasks#insert_tasks(l:tasklist, '', l:i, 0)
442+
let l:i += l:num_added + 1
443+
else
444+
let l:i += 1
445+
endif
446+
endwhile
447+
endfunction

ftplugin/vimwiki.vim

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ if vimwiki_tasks#config('taskwarrior_integration', 1)
1212
command! CopyTaskID call vimwiki_tasks#display_task_id(1)
1313
command! DisplayTaskUUID call vimwiki_tasks#display_task_uuid(0)
1414
command! CopyTaskUUID call vimwiki_tasks#display_task_uuid(1)
15-
command! -nargs=1 -bang InsertTasks call vimwiki_tasks#insert_tasks(<q-args>, '<bang>')
15+
command! -nargs=1 -bang InsertTasks call vimwiki_tasks#insert_tasks(<q-args>, '<bang>', line('.'))
1616
command! -nargs=1 TaskCmd call vimwiki_tasks#current_task_do(<q-args>)
17+
command! UpdateTaskLists call vimwiki_tasks#update_task_lists()
1718
endif

0 commit comments

Comments
 (0)