summaryrefslogtreecommitdiff
path: root/ftplugin/todo.vim
blob: abaad7b87638c9f06e5b6f9a5b1c51d1b3203682 (plain)
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
" File:        todo.txt.vim
" Description: Todo.txt filetype detection
" Author:      David Beniamine <David@Beniamine.net>, Leandro Freitas <freitass@gmail.com>
" License:     Vim license
" Website:     http://github.com/dbeniamine/todo.txt-vim

if ! exists("g:Todo_txt_loaded")
    let g:Todo_txt_loaded=0.8.2
endif

" Save context {{{1
let s:save_cpo = &cpo
set cpo&vim

" General options {{{1
" Some options lose their values when window changes. They will be set every
" time this script is invoked, which is whenever a file of this type is
" created or edited.
setlocal textwidth=0
setlocal wrapmargin=0

" Increment and decrement the priority use <C-A> and <C-X> on alpha
setlocal nrformats+=alpha

" Mappings {{{1

" <Plug> mappings that users can map alternate keys to {{{2
" if they choose not to map default keys (or otherwise)
nnoremap <script> <silent> <buffer> <Plug>TodotxtIncrementDueDateNormal :<C-u>call <SID>ChangeDueDateWrapper(1, "\<Plug>TodotxtIncrementDueDateNormal")<CR>
vnoremap <script> <silent> <buffer> <Plug>TodotxtIncrementDueDateVisual :call <SID>ChangeDueDateWrapper(1, "\<Plug>TodotxtIncrementDueDateVisual")<CR>
nnoremap <script> <silent> <buffer> <Plug>TodotxtDecrementDueDateNormal :<C-u>call <SID>ChangeDueDateWrapper(-1, "\<Plug>TodotxtDecrementDueDateNormal")<CR>
vnoremap <script> <silent> <buffer> <Plug>TodotxtDecrementDueDateVisual :call <SID>ChangeDueDateWrapper(-1, "\<Plug>TodotxtDecrementDueDateVisual")<CR>

noremap  <script> <silent> <buffer> <Plug>DoToggleMarkAsDone :call todo#ToggleMarkAsDone('')<CR>
                \:silent! call repeat#set("\<Plug>DoToggleMarkAsDone")<CR>
noremap  <script> <silent> <buffer> <Plug>DoCancel :call todo#ToggleMarkAsDone('Cancelled')<CR>
                \:silent! call repeat#set("\<Plug>DoCancel")<CR>

" Default key mappings {{{2
if !exists("g:Todo_txt_do_not_map") || ! g:Todo_txt_do_not_map
" Sort todo by (first) context {{{3
    noremap  <script> <silent> <buffer> <localleader>sc :call todo#HierarchicalSort('@', '', 1)<CR>
    noremap  <script> <silent> <buffer> <localleader>scp :call todo#HierarchicalSort('@', '+', 1)<CR>

" Sort todo by (first) project {{{3
    noremap  <script> <silent> <buffer> <localleader>sp :call todo#HierarchicalSort('+', '',1)<CR>
    noremap  <script> <silent> <buffer> <localleader>spc :call todo#HierarchicalSort('+', '@',1)<CR>

" Sort tasks {{{3
    nnoremap <script> <silent> <buffer> <localleader>s :call todo#Sort("")<CR>
    nnoremap <script> <silent> <buffer> <localleader>s@ :call todo#Sort("@")<CR>
    nnoremap <script> <silent> <buffer> <localleader>s+ :call todo#Sort("+")<CR>

" Priorities {{{3
    " TODO: Make vim-repeat work on inc/dec priority
    noremap  <script> <silent> <buffer> <localleader>j :call todo#PrioritizeIncrease()<CR>
    noremap  <script> <silent> <buffer> <localleader>k :call todo#PrioritizeDecrease()<CR>

    noremap  <script> <silent> <buffer> <localleader>a :call todo#PrioritizeAdd('A')<CR>
    noremap  <script> <silent> <buffer> <localleader>b :call todo#PrioritizeAdd('B')<CR>
    noremap  <script> <silent> <buffer> <localleader>c :call todo#PrioritizeAdd('C')<CR>

" Insert date {{{3
    inoremap <script> <silent> <buffer> date<Tab> <C-R>=strftime("%Y-%m-%d")<CR>

    inoremap <script> <silent> <buffer> due: due:<C-R>=strftime("%Y-%m-%d")<CR>
    inoremap <script> <silent> <buffer> DUE: DUE:<C-R>=strftime("%Y-%m-%d")<CR>

    noremap  <script> <silent> <buffer> <localleader>d :call todo#PrependDate()<CR>

" Mark done {{{3
    nmap              <silent> <buffer> <localleader>x <Plug>DoToggleMarkAsDone

" Mark cancelled {{{3
    nmap              <silent> <buffer> <localleader>C <Plug>DoCancel

" Mark all done {{{3
    noremap  <script> <silent> <buffer> <localleader>X :call todo#MarkAllAsDone()<CR>

" Remove completed {{{3
    nnoremap <script> <silent> <buffer> <localleader>D :call todo#RemoveCompleted()<CR>

" Sort by due: date {{{3
    nnoremap <script> <silent> <buffer> <localleader>sd :call todo#SortDue()<CR>
" try fix format {{{3
    nnoremap <script> <silent> <buffer> <localleader>ff :call todo#FixFormat()<CR>

" increment and decrement due:date {{{3
    nmap              <silent> <buffer> <localleader>p <Plug>TodotxtIncrementDueDateNormal
    vmap              <silent> <buffer> <localleader>p <Plug>TodotxtIncrementDueDateVisual
    nmap              <silent> <buffer> <localleader>P <Plug>TodotxtDecrementDueDateNormal
    vmap              <silent> <buffer> <localleader>P <Plug>TodotxtDecrementDueDateVisual

endif

" Additional options {{{2
" Prefix creation date when opening a new line {{{3
if exists("g:Todo_txt_prefix_creation_date")
    nnoremap <script> <silent> <buffer> o o<C-R>=strftime("%Y-%m-%d")<CR> 
    nnoremap <script> <silent> <buffer> O O<C-R>=strftime("%Y-%m-%d")<CR> 
    inoremap <script> <silent> <buffer> <CR> <CR><C-R>=strftime("%Y-%m-%d")<CR> 
endif

" Functions for maps {{{1
function! s:ChangeDueDateWrapper(by_days, repeat_mapping)
    call todo#CreateNewRecurrence(0)
    call todo#ChangeDueDate(a:by_days, 'd', '')
    silent! call repeat#set(a:repeat_mapping, v:count)
endfunction

" Folding {{{1
" Options {{{2
setlocal foldmethod=expr
setlocal foldexpr=TodoFoldLevel(v:lnum)
setlocal foldtext=TodoFoldText()

" Go to first completed task
let oldpos=getcurpos()
if(!exists("g:Todo_fold_char"))
    let g:Todo_fold_char='@'
    let base_pos=search('^x\s', 'ce')
    " Get next completed task
    let first_incomplete = search('^\s*[^<x\s>]')
    if (first_incomplete < base_pos)
        " Check if all tasks from
        let g:Todo_fold_char='x'
    endif
    call setpos('.', oldpos)
endif

function! s:get_contextproject(line) abort "{{{2
    return matchstr(getline(a:line), g:Todo_fold_char.'[^ ]\+')
endfunction "}}}3

" TodoFoldLevel(lnum) {{{2
function! TodoFoldLevel(lnum)
    let this_context = s:get_contextproject(a:lnum)
    let next_context = s:get_contextproject(a:lnum - 1)

    if g:Todo_fold_char == 'x'
        " fold on cmpleted task
        return  match(getline(a:lnum),'\C^x\s') + 1
    endif

    let fold_level = 0

    if this_context ==# next_context
        let fold_level = '1'
    else
        let fold_level = '>1'
    endif

    return fold_level
endfunction

" TodoFoldText() {{{2
function! TodoFoldText()
    let this_context = s:get_contextproject(v:foldstart)
    if g:Todo_fold_char == 'x'
        let this_context = 'Completed tasks'
    endif
    " The text displayed at the fold is formatted as '+- N Completed tasks'
    " where N is the number of lines folded.
    return '+' . v:folddashes . ' '
                \ . (v:foldend - v:foldstart + 1)
                \ .' '. this_context.' '
endfunction

" Restore context {{{1
let &cpo = s:save_cpo

" vim: tabstop=4 shiftwidth=4 softtabstop=4 expandtab foldmethod=marker