Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 0220abeae7
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 1045 lines (917 sloc) 34.865 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
" vikitasks.vim
" @Author: Tom Link (mailto:micathom AT gmail com?subject=[vim])
" @Website: http://www.vim.org/account/profile.php?user_id=4037
" @License: GPL (see http://www.gnu.org/licenses/gpl.txt)
" @Revision: 1029


" A list of glob patterns (or files) that will be searched for task
" lists.
" Can be buffer-local.
" If you add ! to 'viminfo', this variable will be automatically saved
" between editing sessions.
" Alternatively, add new items in ~/vimfiles/after/plugin/vikitasks.vim
TLet g:vikitasks#files = []

" A list of |regexp| patterns for filenames that should not be
" scanned.
TLet g:vikitasks#files_ignored = []
let s:files_ignored = join(g:vikitasks#files_ignored, '\|')

" If non-null, automatically add the homepages of your intervikis to
" |g:vikitasks#files|.
" If the value is 2, scan all files (taking into account the interviki
" suffix) in the interviki's top directory.
" Can be buffer-local.
TLet g:vikitasks#intervikis = 0

" If true, completely ignore completed tasks.
TLet g:vikitasks#ignore_completed_tasks = 1

" A list of ignored intervikis.
" Can be buffer-local.
TLet g:vikitasks#intervikis_ignored = []

" If you use todo.txt (http://todotxt.com), set this variable to the
" full name of base directory where todo.txt is kept.
" For display, lines in todo.txt are converted to viki task list syntax.
TLet g:vikitasks#todotxt_dir = ''

" If true, provide tighter integration with the vim viki plugin.
TLet g:vikitasks#sources = {
            \ 'viki': exists('g:loaded_viki'),
            \ 'todotxt': !empty(g:vikitasks#todotxt_dir)
            \ }

" Default category/priority when converting tasks without priorities.
TLet g:vikitasks#default_priority = 'F'

" The viewer for the quickfix list. If empty, use |:TRagcw|.
TLet g:vikitasks#qfl_viewer = ''

" Item classes that should be included in the list when calling
" |:VikiTasks|.
" A user-defined value must be set in |vimrc| before the plugin is
" loaded.
TLet g:vikitasks#rx_letters = 'A-W'

" Item levels that should be included in the list when calling
" |:VikiTasks|.
" A user-defined value must be set in |vimrc| before the plugin is
" loaded.
TLet g:vikitasks#rx_levels = '1-5'

" If non-empty, vikitasks will insert a break line when displaying a
" list in the background.
TLet g:vikitasks#today = 'DUE'

" Cache file name.
" By default, use |tlib#cache#Filename()| to determine the file name.
TLet g:vikitasks#cache = tlib#cache#Filename('vikitasks', 'files', 1)
call add(g:tlib#cache#dont_purge, '[\/]vikitasks[\/]files$')

" Definition of the tasks that should be included in the Alarms list.
" Fields:
" all_tasks ... If non-null, also display tasks with no due-date
" tasks ... Either 'tasks' or 'sometasks'
" constraint ... See |:VikiTasks|
TLet g:vikitasks#alarms = {'all_tasks': 0, 'tasks': 'sometasks', 'constraint': 14}

" If true, the end-date of date ranges (FROM..TO) is significant.
TLet g:vikitasks#use_end_date = 1

" Interpret entries with an unspecified date ("_") as current tasks.
TLet g:vikitasks#use_unspecified_dates = 0

" If true, remove unreadable files from the tasks list.
TLet g:vikitasks#remove_unreadable_files = 1

" The parameters for |:TRagcw| when |g:vikitasks#qfl_viewer| is empty.
" :read: TLet g:vikitasks#inputlist_params = {...}
" :nodoc:
TLet g:vikitasks#inputlist_params = {
            \ 'trag_list_syntax': g:vikitasks#sources.viki ? 'viki' : '',
            \ 'trag_list_syntax_nextgroup': '@vikiPriorityListTodo',
            \ 'trag_short_filename': 1,
            \ 'scratch': '__VikiTasks__',
            \ 'key_map': {
            \ 'default': {
            \ "\<f2>" : {'key': "\<f2>", 'agent': 'vikitasks#AgentKeymap', 'key_name': '<f2>', 'help': 'Switch to vikitasks keymap'},
            \ 24 : {'key': 24, 'agent': 'vikitasks#AgentMarkDone', 'key_name': '<c-x>', 'help': 'Mark done'},
            \ 4 : {'key': 4, 'agent': 'vikitasks#AgentDueDays', 'key_name': '<c-d>', 'help': 'Mark as due in N days'},
            \ 23 : {'key': 23, 'agent': 'vikitasks#AgentDueWeeks', 'key_name': '<c-w>', 'help': 'Mark as due in N weeks'},
            \ 3 : {'key': 3, 'agent': 'vikitasks#AgentItemChangeCategory', 'key_name': '<c-c>', 'help': 'Change task category'},
            \ },
            \ 'vikitasks': extend(copy(g:tlib_keyagents_InputList_s),
            \ {
            \ char2nr('x') : {'agent': 'vikitasks#AgentMarkDone', 'key_name': 'x', 'help': 'Mark done'},
            \ char2nr('d') : {'agent': 'vikitasks#AgentDueDays', 'key_name': 'd', 'help': 'Mark as due in N days'},
            \ char2nr('w') : {'agent': 'vikitasks#AgentDueWeeks', 'key_name': 'w', 'help': 'Mark as due in N weeks'},
            \ char2nr('c') : {'agent': 'vikitasks#AgentItemChangeCategory', 'key_name': 'c', 'help': 'Change task category'},
            \ 'unknown_key': {'agent': 'tlib#agent#Null', 'key_name': 'other keys', 'help': 'ignore key'},
            \ }
            \ )
            \ }
            \ }

" Mapleader for some vikitasks related maps.
TLet g:vikitasks#mapleader = '<LocalLeader>t'

" If true, add a date tag when marking a task done with |vikitasks#ItemMarkDone()|.
TLet g:vikitasks#done_add_date = 1

" A vim expression that returns the filename of the archive where
" archived tasks should be moved to.
TLet g:vikitasks#archive_filename_expr = 'expand("%:p:r") ."_archived". g:vikiNameSuffix'

" A list of strings. The header for newly created tasks archives.
TLet g:vikitasks#archive_header = ['* Archived tasks']

" The date format string (see |strftime()|) for archived entries.
TLet g:vikitasks#archive_date_fmt = '** %Y-%m-%d'

" Letters of final categories, i.e. tasks that should not be altered
" after assigning them to one of these categories.
"
" Tasks in these categories will be considered suitable candidates for
" automatic archival by |vikitasks#ItemArchiveFinal()|.
"
" By default the following categories are considered final:
" X ... done
" Y ... cancelled
" Z ... ???
TLet g:vikitasks#final_categories = 'XYZ'

" If non-empty, use |:Calendar| as date picker when marking an item as
" due in N days.
"
" NOTE: This experimental feature is disabled by default. Enable it by
" setting this variable to, e.g., "Calendar" in your |vimrc| file.
TLet g:vikitasks#use_calendar = ''
" TLet g:vikitasks#use_calendar = exists(':Calendar') ? 'Calendar' : ''


function! s:TaskLineRx(filetype, inline, sometasks, letters, levels) "{{{3
    if a:filetype == 'todotxt'
        let val = '\C^\zs\(('. a:letters .')\s\+\|x\s\+\)\?\([0-9-]\+\s\+\)\{,2}.\+$'
    else
        let val = '\C^[[:blank:]]'. (a:inline ? '*' : '\+') .'\zs'.
                    \ '#\(T: \+.\{-}'. a:letters .'.\{-}:\|'.
                    \ '['. a:levels .']\?['. a:letters .']['. a:levels .']\?'.
                    \ '\( \+\(_\|x\?[0-9%-]\+\)\)\?\)\(\s%s\|$\)'
    endif
    return val
endf


function! s:TasksRx(which_tasks, ...) "{{{3
    TVarArg ['filetype', 'viki']
    let fmt = s:{a:which_tasks}_{filetype}_rx
    if fmt =~ '%s'
        return printf(fmt, '.*')
    else
        return fmt
    endif
endf


let s:sometasks_viki_rx = s:TaskLineRx('viki', 1, 1, g:vikitasks#rx_letters, g:vikitasks#rx_levels)
let s:tasks_viki_rx = s:TaskLineRx('viki', 0, 0, 'A-Z', '0-9')
if g:vikitasks#sources.viki
    exec 'TRagDefKind tasks viki /'. s:tasks_viki_rx .'/'
    exec 'TRagDefKind sometasks viki /'. s:sometasks_viki_rx .'/'
endif
if g:vikitasks#sources.todotxt
    let s:sometasks_todotxt_rx = s:TaskLineRx('todotxt', 1, 1, g:vikitasks#rx_letters, g:vikitasks#rx_levels)
    let s:tasks_todotxt_rx = s:TaskLineRx('todotxt', 0, 0, 'A-Z', '0-9')
    exec 'TRagDefKind tasks todotxt /'. s:tasks_todotxt_rx .'/'
    exec 'TRagDefKind sometasks todotxt /'. s:sometasks_todotxt_rx .'/'
endif
exec 'TRagDefKind tasks * /'. s:tasks_viki_rx .'/'
exec 'TRagDefKind sometasks * /'. s:sometasks_viki_rx .'/'


let s:date_rx = '\C^\s*#[A-Z0-9]\+\s\+\zs\(x\?\)\(_\|\d\+-\d\+-\d\+\)\(\.\.\(\(_\|\d\+-\d\+-\d\+\)\)\)\?\ze\s'
let s:filetypes = {}


" :nodoc:
function! vikitasks#GetArgs(bang, list) "{{{3
    let args = {}
    let args.cached = !a:bang
    let a0 = get(a:list, 0, '.')
    let files_idx = 2
" TLogVAR a0
    if a0 =~ '^\(t\%[oday]\|c\%[urrent]\|w\%[eek]\|m\%[onth]\|[+-]\?\d\+[dwm]\?\|[.*]\)$'
        let args.all_tasks = a0 =~ '^[.*]$'
        let args.tasks = a0 =~ '^[*-]' ? 'tasks' : 'sometasks'
        let args.constraint = a0
        if !empty(a:list)
            call remove(a:list, 0)
            let files_idx -= 1
        endif
    else
        let args.all_tasks = 1
        let args.tasks = 'sometasks'
        let args.constraint = '.'
    endif
    let args.rx = s:MakePattern(get(a:list, 0, '.'))
    let args.files = a:list[files_idx : -1]
    let args.ignore_completed = g:vikitasks#ignore_completed_tasks
" TLogVAR args
    return args
endf


" :display: vikitasks#Tasks(?{'all_tasks': 0, 'cached': 1, 'files': [], 'constraint': '', 'rx': ''}, ?suspend=0)
" If files is non-empty, use these files (glob patterns actually)
" instead of those defined in |g:vikitasks#files|.
function! vikitasks#Tasks(...) "{{{3
    TVarArg ['args', {}], ['suspend', 0]

    if get(args, 'cached', 1)

        let qfl = copy(s:Tasks())
        let files = get(args, 'files', [])
        if !empty(files)
            for file in files
                let file_rx = substitute(file, '\*', '.\\{-}', 'g')
                let file_rx = substitute(file_rx, '?', '.', 'g')
                call filter(qfl, '(has_key(v:val, "filename") ? v:val.filename : bufname(v:val.bufnr)) =~ file_rx')
            endfor
        endif
        call s:TasksList(qfl, args, suspend)

    else

        if g:vikitasks#sources.viki && &filetype != 'viki' && !viki#HomePage()
            echoerr "VikiTasks: Not a viki buffer and cannot open the homepage"
            return
        endif

" TLogVAR args
        let files = get(args, 'files', [])
" TLogVAR files
        if empty(files)
            let files = s:MyFiles()
" TLogVAR files
        endif
" TAssertType files, 'list'

" TLogVAR files
        call map(files, 'glob(v:val)')
        let files = split(join(files, "\n"), '\n')
        let files = map(files, 's:CanonicFilename(v:val)')
        let files = tlib#list#Uniq(files)
" TLogVAR files
        if !empty(files)
            let qfl = trag#Grep('tasks', 1, files)
" TLogVAR qfl
" TLogVAR filter(copy(qfl), 'v:val.text =~ "#D7"')

            let tasks = copy(qfl)
            for i in range(len(tasks))
                let bufnr = tasks[i].bufnr
                let filename = s:CanonicFilename(fnamemodify(bufname(bufnr), ':p'))
                let tasks[i].filename = filename
                let filetype = get(s:filetypes, filename, '')
                if filetype != 'viki'
                    let tasks[i].text = s:Convert(tasks[i].text, filetype)
                endif
                call remove(tasks[i], 'bufnr')
            endfor
            call s:SaveInfo(s:Files(), tasks)

            call s:TasksList(qfl, args, suspend)
        else
            echom "VikiTasks: No task files"
        endif

    endif
endf


function! s:TasksList(qfl, args, suspend) "{{{3
    let qfl = a:qfl
    call s:FilterTasks(qfl, a:args)
    call sort(qfl, "s:SortTasks")
    let i = s:GetCurrentTask(qfl, 0)
    call s:Setqflist(qfl, a:suspend ? i : -1)
    call s:View(i, a:suspend)
endf


function! s:Setqflist(qfl, today) "{{{3
" TLogVAR a:today
    if !empty(g:vikitasks#today) && len(a:qfl) > 1 && a:today > 1 && a:today < len(a:qfl) - 1
        let break = repeat('^', (&columns - 20 - len(g:vikitasks#today)) / 2)
        let text = join([break, g:vikitasks#today, break])
        let qfl = insert(a:qfl, {'bufnr': 0, 'text': text}, a:today - 1)
        call setqflist(qfl)
    else
        call setqflist(a:qfl)
    endif
endf


function! s:FileReadable(filename, cache) "{{{3
    let readable = get(a:cache, a:filename, -1)
    if readable >= 0
        return readable
    else
        let a:cache[a:filename] = filereadable(a:filename)
        return a:cache[a:filename]
    endif
endf


function! s:FilterTasks(tasks, args) "{{{3
" TLogVAR a:args

    let rx = get(a:args, 'rx', '')
    if !empty(rx)
        call filter(a:tasks, 'v:val.text =~ rx')
    endif

    if g:vikitasks#remove_unreadable_files
        let filenames = {}
        call filter(a:tasks, 's:FileReadable(v:val.filename, filenames)')
    endif

    let ignore_completed = get(a:args, 'ignore_completed', g:vikitasks#ignore_completed_tasks)
    if ignore_completed
        call filter(a:tasks, 'empty(get(matchlist(v:val.text, s:date_rx), 1, ""))')
    endif

    let which_tasks = get(a:args, 'tasks', 'tasks')
" TLogVAR which_tasks
    if which_tasks == 'sometasks'
        let rx = s:TasksRx('sometasks')
" TLogVAR rx
" TLogVAR len(a:tasks)
        call filter(a:tasks, 'v:val.text =~ rx')
" TLogVAR len(a:tasks)
    endif

    if !get(a:args, 'all_tasks', 0)
        call filter(a:tasks, '!empty(s:GetTaskDueDate(v:val.text, 0, g:vikitasks#use_unspecified_dates))')
" TLogVAR len(a:tasks)
        let constraint = get(a:args, 'constraint', '.')
" TLogVAR constraint
        if constraint !~ '^[+-]\?\d\+'
            let future = ''
            let n = 1
        else
            let [m0, future, n; _] = matchlist(constraint, '^\([+-]\?\)\(\d\+\)')
        endif
        let from = empty(future) ? 0 : localtime()
        let to = 0
        if constraint =~ '^t\%[oday]'
            let from = localtime()
            let to = from
        elseif constraint =~ '^c\%[urrent]'
            let from = 0
            let to = localtime()
        elseif constraint =~ '^m\%[onth]'
            let to = localtime() + 86400 * 31
        elseif constraint == 'w\%[eek]'
            let to = localtime() + 86400 * 7
        elseif constraint =~ '^[+-]\?\d\+[dwm]\?$'
            let to = localtime()
            let delta = n * 86400
            if constraint =~ 'w$'
                let delta = delta * 7
            elseif constraint =~ 'm$'
                let delta = delta * 31
            endif
            if constraint =~ '^-'
                let from -= delta
            else
                let to += delta
            endif
        else
            echoerr "vikitasks: Malformed constraint: ". constraint
        endif
" TLogVAR from, to
        if from != 0 || to != 0
            call filter(a:tasks, 's:Select(v:val.text, from, to)')
        endif
    endif
endf


function! s:View(index, suspend) "{{{3
    if empty(g:vikitasks#qfl_viewer)
        let w = deepcopy(g:vikitasks#inputlist_params)
        if a:index > 1
            let w.initial_index = a:index
        endif
        call trag#QuickList(w, a:suspend)
    else
        exec g:vikitasks#qfl_viewer
    endif
endf


" The |regexp| PATTERN is prepended with |\<| if it seems to be a word.
" The PATTERN is made case sensitive if it contains an upper-case letter
" and if 'smartcase' is true.
function! s:MakePattern(pattern) "{{{3
    let pattern = a:pattern
    if empty(pattern)
        let pattern = '.'
    elseif pattern != '.'
        if pattern =~ '^\w'
            let pattern = '\<'. pattern
        endif
        if &smartcase && pattern =~ '\u'
            let pattern = '\C'. pattern
        endif
    endif
    return pattern
endf


function! s:GetTaskDueDate(task, use_end_date, use_unspecified) "{{{3
    let m = matchlist(a:task, s:date_rx)
    if a:use_end_date && g:vikitasks#use_end_date
        let rv = get(m, 4, '')
    else
        let rv = ''
    endif
    if empty(rv)
        let rv = get(m, 2, '')
    endif
    if rv == '_' && !a:use_unspecified
        let rv = ''
    endif
" TLogVAR a:task, m, rv
    return rv
endf


function! s:GetCurrentTask(qfl, daysdiff) "{{{3
" TLogVAR a:daysdiff
    let i = 1
    let today = strftime('%Y-%m-%d')
    for qi in a:qfl
        let qid = s:GetTaskDueDate(qi.text, 1, g:vikitasks#use_unspecified_dates)
" TLogVAR qid, today
        if !empty(qid) && qid != '_' && tlib#date#DiffInDays(qid, today, 1) <= a:daysdiff
            let i += 1
        else
            break
        endif
    endfor
    return i
endf


function! s:SortTasks(a, b) "{{{3
    let a = a:a.text
    let b = a:b.text
    let ad = s:GetTaskDueDate(a, 1, g:vikitasks#use_unspecified_dates)
    let bd = s:GetTaskDueDate(b, 1, g:vikitasks#use_unspecified_dates)
    if ad && !bd
        return -1
    elseif !ad && bd
        return 1
    elseif ad && bd && ad != bd
        return ad > bd ? 1 : -1
    else
        return a == b ? 0 : a > b ? 1 : -1
    endif
endf


function! s:Files() "{{{3
    if !exists('s:files')
        let s:files = get(tlib#cache#Get(g:vikitasks#cache), 'files', [])
        if !has('fname_case') || g:tlib_filename_sep == '\'
            call map(s:files, 's:CanonicFilename(v:val)')
        endif
" echom "DBG nfiles = ". len(s:files)
    endif
    return s:files
endf


function! s:Tasks() "{{{3
    if !exists('s:tasks')
        let s:tasks = get(tlib#cache#Get(g:vikitasks#cache), 'tasks', [])
" echom "DBG ntasks = ". len(s:tasks)
    endif
    return s:tasks
endf


function! s:SaveInfo(files, tasks) "{{{3
" TLogVAR len(a:files), len(a:tasks)
    let s:files = a:files
    let s:tasks = a:tasks
    call tlib#cache#Save(g:vikitasks#cache, {'files': a:files, 'tasks': a:tasks})
endf


function! s:CanonicFilename(filename) "{{{3
    let filename = a:filename
    if !has('fname_case')
        let filename = tolower(filename)
    endif
    if g:tlib_filename_sep == '\'
        let filename = substitute(filename, '\\', '/', 'g')
    endif
    return filename
endf


function! s:MyFiles() "{{{3
    let s:filetypes = {}
    let files = copy(tlib#var#Get('vikitasks#files', 'bg', []))
" TLogVAR files
    let files += s:Files()
" TLogVAR files
    if g:vikitasks#sources.viki
        if tlib#var#Get('vikitasks#intervikis', 'bg', 0) > 0
            call s:AddInterVikis(files)
        endif
    endif
" TLogVAR files
    if !empty(s:files_ignored)
        call filter(files, 'v:val !~ s:files_ignored')
    endif
" TLogVAR files
    if !has('fname_case') || g:tlib_filename_sep == '\'
        call map(files, 's:CanonicFilename(v:val)')
    endif
" TLogVAR g:vikitasks#sources.todotxt
    if g:vikitasks#sources.todotxt
        let todotxt = tlib#file#Join([g:vikitasks#todotxt_dir, 'todo.txt'])
        if !has('fname_case') || g:tlib_filename_sep == '\'
            let todotxt = s:CanonicFilename(todotxt)
        endif
        if filereadable(todotxt)
            call add(files, todotxt)
            let s:filetypes[todotxt] = 'todotxt'
            if !trag#HasFiletype(todotxt)
                call trag#SetFiletype('todotxt', todotxt)
            endif
" TLogVAR todotxt
        endif
    endif
    let files = tlib#list#Uniq(files)
" TLogVAR files
    return files
endf


function! s:AddInterVikis(files) "{{{3
    if g:vikitasks#sources.viki
" TLogVAR a:files
        let ivignored = tlib#var#Get('vikitasks#intervikis_ignored', 'bg', [])
        let glob = tlib#var#Get('vikitasks#intervikis', 'bg', 0) == 2
        for iv in viki#GetInterVikis()
            if index(ivignored, matchstr(iv, '^\u\+')) == -1
" TLogVAR iv
                let def = viki#GetLink(1, '[['. iv .']]', 0, '')
                let file = def[1]
" TLogVAR def
                if glob > 0
                    let suffix = viki#InterVikiSuffix(iv)
                    let dirpattern = tlib#file#Join([fnamemodify(file, ':p:h'), '**/*'. suffix], 1)
                    call s:AddDirPattern(a:files, dirpattern)
                else
                    if filereadable(file) && !isdirectory(file) && index(a:files, file) == -1
                        call add(a:files, file)
                    endif
                endif
            endif
        endfor
    endif
endf


function! s:AddDirPattern(files, dirpattern) "{{{3
    let files = split(glob(a:dirpattern), '\n')
    for hp in files
" TLogVAR hp, filereadable(hp), !isdirectory(hp), index(a:files, hp) == -1
        if filereadable(hp) && !isdirectory(hp) && index(a:files, hp) == -1
            call add(a:files, hp)
        endif
    endfor
endf


function! s:Select(text, from, to) "{{{3
    let sfrom = strftime('%Y-%m-%d', a:from)
    let sto = strftime('%Y-%m-%d', a:to)
    let date1 = s:GetTaskDueDate(a:text, 0, g:vikitasks#use_unspecified_dates)
    let date2 = s:GetTaskDueDate(a:text, 1, g:vikitasks#use_unspecified_dates)
    if date1 == '_'
        let rv = 1
    elseif date1 == date2
        let rv = date1 >= sfrom && date1 <= sto
    else
        let rv = (date1 >= sfrom && date1 <= sto) || (date2 >= sfrom && date2 <= sto)
    endif
" TLogVAR sfrom, sto, date1, date2, rv
    return rv
endf


" Register BUFFER as a file that should be scanned for task lists.
function! vikitasks#AddBuffer(buffer, ...) "{{{3
    TVarArg ['save', 1]
" TLogVAR a:buffer, save
    let fname = s:CanonicFilename(fnamemodify(a:buffer, ':p'))
    let files = s:Files()
    if filereadable(fname) && index(files, fname) == -1
        call add(files, fname)
        if save && !vikitasks#ScanCurrentBuffer(fname)
            call s:SaveInfo(files, s:Tasks())
        endif
    endif
endf


" Unregister BUFFER as a file that should be scanned for task lists.
function! vikitasks#RemoveBuffer(buffer, ...) "{{{3
    TVarArg ['save', 1]
" TLogVAR a:buffer, save
    let fname = s:CanonicFilename(fnamemodify(a:buffer, ':p'))
    let files = s:Files()
    let fidx = index(files, fname)
    if fidx != -1
        call remove(files, fidx)
        if save && !vikitasks#ScanCurrentBuffer(fname)
            call s:SaveInfo(files, s:Tasks())
        endif
    endif
endf


" Edit the list of files.
function! vikitasks#EditFiles() "{{{3
    let files = tlib#input#EditList('Edit task files:', copy(s:Files()))
    if files != s:files
        call s:SaveInfo(files, s:Tasks())
        call tlib#notify#Echo('Please update your task list by running :VikiTasks!', 'WarningMsg')
    endif
endf


" :nodoc:
" :display: vikitasks#Alarm(?ddays = -1)
" Display a list of alarms.
" If ddays >= 0, the constraint value in |g:vikitasks#alarms| is set to
" ddays days.
" If ddays is -1 and |g:vikitasks#alarms| is empty, not alarms will be
" listed.
function! vikitasks#Alarm(...) "{{{3
    TVarArg ['ddays', -1]
" TLogVAR ddays
    if ddays < 0 && empty(g:vikitasks#alarms)
        return
    endif
    let tasks = copy(s:Tasks())
    call sort(tasks, "s:SortTasks")
    let alarms = copy(g:vikitasks#alarms)
    if ddays >= 0
        let alarms.constraint = ddays
    endif
" TLogVAR alarms
    call s:FilterTasks(tasks, alarms)
    if !empty(tasks)
" TLogVAR tasks
        call s:Setqflist(tasks, s:GetCurrentTask(tasks, 0))
        call s:View(0, 1)
        redraw
    endif
endf


function! s:Convert(line, filetype) "{{{3
" <+TODO+>
    if a:filetype == 'todotxt'
        let line = substitute(a:line, '^\C(\([A-Z]\))\ze\s\+', '#\1', '')
        if line !~# '^#\u'
            let line = '#'. g:vikitasks#default_priority .' '. line
        endif
        for [rx, subst] in [
                    \ ['^#\u\d*\s\([0-9-]\+\s\([0-9-]\+\)\?\s\)\?\zs\(.\{-}\)\s\(@\S\+\)\ze\+\(\s\|$\)', '\4 \3'],
                    \ ['^#\u\d*\s\([0-9-]\+\s\([0-9-]\+\)\?\s\)\?\zs\(.\{-}\)\s+\(\S\+\)\ze\+\(\s\|$\)', ':\4 \3']
                    \ ]
            let line0 = ''
            let iterations = 5
            while line0 != line && iterations > 0
                let line0 = line
                let line = substitute(line, rx, subst, 'g')
                let iterations -= 1
            endwh
        endfor
        let line = substitute(line, '^#\u\d*\s\zs\(.\{-}\s\)\?t:\([0-9-]\+\)\ze\(\s\|$\)', '\2 \1', 'g')
" TLogVAR line
        return line
    else
        return a:line
    endif
endf


" :nodoc:
" Scan the current buffer for task lists.
function! vikitasks#ScanCurrentBuffer(...) "{{{3
    TVarArg ['filename', '']
    let use_buffer = empty(filename)
    if use_buffer
        let filename = s:CanonicFilename(fnamemodify(bufname('%'), ':p'))
    else
        let filename = s:CanonicFilename(filename)
    endif
" TLogVAR filename, use_buffer
    if &buftype =~ '\<nofile\>' || (!empty(s:files_ignored) && filename =~ s:files_ignored) || !filereadable(filename) || isdirectory(filename) || empty(filename)
        return 0
    endif
    let tasks0 = s:Tasks()
    let ntasks = len(tasks0)
    let tasks = []
    let buftasks = {}
    let filetype = get(s:filetypes, filename, 'viki')
    for task in tasks0
" TLogVAR task
        if s:CanonicFilename(task.filename) == filename
" TLogVAR task.lnum, task
            if has_key(task, 'text')
                let buftasks[task.lnum] = task
            endif
        else
            call add(tasks, task)
        endif
        unlet task
    endfor
" TLogVAR len(tasks)
    let rx = s:TasksRx('tasks', filetype)
    let def = {'inline': 0, 'sometasks': 0, 'letters': 'A-Z', 'levels': '0-9'}
    let @r = rx
    let update = 0
    let tasks_found = 0
    let lnum = 1
" echom "DBG ". string(keys(buftasks))
    if use_buffer
        let lines = getline(1, '$')
    else
        let lines = readfile(filename)
    endif
" call filter(tasks, 'v:val.filename != filename')
    for line in lines
        if filetype != 'viki'
            let line = s:Convert(line, filetype)
        endif
        if line =~ '^%\s*vikitasks:'
            let paramss = matchstr(line, '^%\s*vikitasks:\s*\zs.*$')
" TLogVAR paramss
            if paramss =~ '^\s*none\s*$'
                return
            else
                let paramsl = split(paramss, ':')
" TLogVAR paramsl
                call map(paramsl, 'split(v:val, "=", 1)')
" TLogVAR paramsl
                try
                    for [var, val] in paramsl
                        let def[var] = val
                    endfor
                catch
                    echoerr 'Vikitasks: Malformed vikitasks-mode-line parameters: '. paramss
                endtry
                unlet! var val
            endif
            let rx = printf(s:TaskLineRx(filetype, def.inline, def.sometasks, def.letters, def.levels), '.*')
        elseif line =~ rx
            let text = tlib#string#Strip(line)
" TLogVAR text
            let tasks_found = 1
            if get(get(buftasks, lnum, {}), 'text', '') != text
" TLogVAR lnum
" echom "DBG ". get(buftasks,lnum,'')
                let update = 1
" TLogVAR lnum, text
                call add(tasks, {
                            \ 'filename': filename,
                            \ 'lnum': lnum,
                            \ 'text': text
                            \ })
            else
                call add(tasks, buftasks[lnum])
            endif
        endif
        let lnum += 1
    endfor
" TLogVAR len(tasks)
    if update
" TLogVAR update
        call vikitasks#AddBuffer(filename, 0)
        call s:SaveInfo(s:Files(), tasks)
    elseif !tasks_found
        call vikitasks#RemoveBuffer(filename, 0)
        call s:SaveInfo(s:Files(), tasks)
    endif
    return update
endf


" Mark a N tasks as done, i.e. assign them to category X -- see also
" |g:vikitasks#final_categories|.
function! vikitasks#ItemMarkDone(count) "{{{3
    let rx = s:TasksRx('tasks')
" TLogVAR rx
    for lnum in range(line('.'), line('.') + a:count)
        let line = getline(lnum)
" TLogVAR lnum, line
        if line =~ rx && line !~ '^\C\s*#'. s:FinalRx()
            let line = substitute(line, '^\C\s*#\zs\u', 'X', '')
            if g:vikitasks#done_add_date
                let idx = matchend(line, s:date_rx)
                if idx == -1
                    let idx = matchend(line, '^\C\s*#\u\d*\ze\s')
                endif
                if idx != -1
                    let line = strpart(line, 0, idx)
                                \ . strftime(' :done:%Y-%m-%d')
                                \ . strpart(line, idx)
                endif
            endif
" TLogVAR line
            call setline(lnum, line)
        endif
    endfor
endf


" Archive final (see |g:vikitasks#final_categories|) tasks.
function! vikitasks#ItemArchiveFinal() "{{{3
    if empty(g:vikitasks#archive_filename_expr)
        echom "vikitasks: Cannot archive tasks: g:vikitasks#archive_filename_expr is empty"
    else
        let archive_filename = eval(g:vikitasks#archive_filename_expr)
        if filereadable(archive_filename)
            let archived = readfile(archive_filename)
        else
            let archived = copy(g:vikitasks#archive_header)
        endif
        let to_be_archived = []
        let clnum = line('.')
        for lnum in reverse(range(1, line('$')))
            let line = getline(lnum)
            if line =~ '\C^\s*#'. s:FinalRx() || line =~ '\C^\s\+#\(\u\d\|\d\u\)\s\+x[ [:digit:]]'
                call insert(to_be_archived, line)
                if lnum <= clnum
                    norm! k
                endif
                exec lnum 'delete'
            endif
        endfor
        if !empty(to_be_archived)
            if !empty(g:vikitasks#archive_date_fmt)
                let archived += ['', strftime(g:vikitasks#archive_date_fmt)]
            endif
            let archived += to_be_archived
            call writefile(archived, archive_filename)
        endif
    endif
endf


" Edit a file monitored by vikitasks.
function! vikitasks#ListTaskFiles() "{{{3
    let files = s:Files()
    let selected = tlib#input#List('m', 'Select files:', files)
    for file in selected
        exec 'edit' fnameescape(file)
    endfor
endf


" Mark a tasks as due in N days.
function! vikitasks#ItemMarkDueInDays(count, days) "{{{3
" TLogVAR a:count, a:days
    let duedate = strftime('%Y-%m-%d', localtime() + a:days * g:tlib#date#dayshift)
    for lnum in range(line('.'), line('.') + a:count)
        call vikitasks#MarkItemDuInDays(lnum, duedate)
    endfor
endf


" :nodoc:
function! vikitasks#MarkItemDuInDays(lnum, duedate) "{{{3
" TLogVAR bufname('%'), a:lnum, a:duedate
    let rx = s:TasksRx('tasks')
    let line = getline(a:lnum)
" TLogVAR line
    if line =~ rx && line =~ s:date_rx && line !~ '^\C\s*#'. s:FinalRx()
        let m = matchlist(line, s:date_rx)
        if !empty(get(m, 4, ''))
            let subst = '\1\2..'. a:duedate
        else
            let subst = '\1'. a:duedate
        endif
        let line1 = substitute(line, s:date_rx, subst, '')
" TLogVAR line1
        call setline(a:lnum, line1)
    endif
endf


" Mark a tasks as due in N weeks.
function! vikitasks#ItemMarkDueInWeeks(count, weeks) "{{{3
" TLogVAR a:count, a:weeks
    call vikitasks#ItemMarkDueInDays(a:count, a:weeks * 7)
endf


" Change the category for the current and the next a:count tasks.
function! vikitasks#ItemChangeCategory(count, ...) "{{{3
    if a:0 >= 1
        let category = a:1
    else
        call inputsave()
        let category = input('New task category [A-Z]: ')
        call inputrestore()
    endif
    let category = toupper(category)
    if category =~ '\C^[A-Z]$'
        let rx = s:TasksRx('tasks')
" TLogVAR rx
        for lnum in range(line('.'), line('.') + a:count)
            let line = getline(lnum)
" TLogVAR lnum, line
            if line =~ rx
                let line = substitute(line, '^\C\s*#\zs\u', category, '')
" TLogVAR line
                call setline(lnum, line)
            endif
        endfor
    else
        echohl WarningMsg
        echom 'Invalid category (must be A-Z):' category
        echohl NONE
    endif
endf


" :nodoc:
function! vikitasks#AgentItemChangeCategory(world, selected) "{{{3
    call inputsave()
    let category = toupper(input('New task category [A-Z]: '))
    call inputrestore()
    if category =~ '\C^[A-Z]$'
        return trag#AgentWithSelected(a:world, a:selected, 'call vikitasks#ItemChangeCategory(0,'. string(category) .')')
    else
        echohl WarningMsg
        echom 'Invalid category (must be A-Z):' category
        echohl NONE
        let a:world.state = 'redisplay'
        return a:world
    endif
endf


function! s:FinalRx() "{{{3
    return printf('\C[%s]', g:vikitasks#final_categories)
endf


" :nodoc:
function! vikitasks#AgentKeymap(world, selected) "{{{3
    let a:world.key_mode = 'vikitasks'
    let a:world.state = 'display'
    return a:world
endf


" :nodoc:
function! vikitasks#AgentMarkDone(world, selected) "{{{3
    return trag#AgentWithSelected(a:world, a:selected, 'VikiTasksDone')
endf


" :nodoc:
function! vikitasks#AgentDueDays(world, selected) "{{{3
    if !empty(g:vikitasks#use_calendar)
        let s:calendar_action = exists('g:calendar_action') ? g:calendar_action : "<SID>CalendarDiary"
        let s:calendar_callback_window = winnr()
        let s:calendar_callback_buffer = winbufnr(0)
        let s:calendar_callback_world = a:world
        let s:calendar_callback_selected = a:selected
        let g:calendar_action = 'vikitasks#CalendarCallback'
        exec g:vikitasks#use_calendar
        let s:calendar_window = winnr()
        let s:calendar_buffer = winbufnr(0)
        let world = tlib#agent#Suspend(a:world, a:selected)
        exec s:calendar_window 'wincmd w'
        return world
    else
        let val = input("Number of days: ", 1)
        return trag#AgentWithSelected(a:world, a:selected, 'VikiTasksDueInDays '. val)
    endif
endf


" :nodoc:
function! vikitasks#CalendarCallback(day, month, year, week, dir) "{{{3
" TLogVAR a:day, a:month, a:year, a:week, a:dir
    if winbufnr(s:calendar_window) == s:calendar_buffer
        silent! exec s:calendar_window 'wincmd c'
    endif
    let g:calendar_action = s:calendar_action
    let duedate = printf('%4d-%02d-%02d', a:year, a:month, a:day)
" TLogVAR duedate
    let world = trag#RunCmdOnSelected(s:calendar_callback_world, s:calendar_callback_selected,
                \ printf('call vikitasks#MarkItemDuInDays(line("."), %s)', string(duedate)), 0)
" TLogVAR world.state
    call setbufvar(s:calendar_callback_buffer, 'tlib_world', world)
    exec s:calendar_callback_window 'wincmd w'
endf


" :nodoc:
function! vikitasks#AgentDueWeeks(world, selected) "{{{3
    call inputsave()
    let val = input("Number of weeks: ", 1)
    call inputrestore()
    return trag#AgentWithSelected(a:world, a:selected, 'VikiTasksDueInWeeks '. val)
endf

Something went wrong with that request. Please try again.