added .vim folder
[dotfiles.git] / .vim / ftplugin / todo.vim
1 " File: todo.txt.vim
2 " Description: Todo.txt filetype detection
3 " Author: Leandro Freitas <freitass@gmail.com>
4 " Licence: Vim licence
5 " Website: http://github.com/freitass/todo.txt.vim
6 " Version: 0.4
7
8 " Save context {{{1
9 let s:save_cpo = &cpo
10 set cpo&vim
11
12 " General options {{{1
13 " Some options lose their values when window changes. They will be set every
14 " time this script is invocated, which is whenever a file of this type is
15 " created or edited.
16 setlocal textwidth=0
17 setlocal wrapmargin=0
18
19 " Mappings {{{1
20 " Sort tasks {{{2
21 if !hasmapto("<leader>s",'n')
22 nnoremap <script> <silent> <buffer> <leader>s :sort<CR>
23 endif
24
25 " Insert date {{{2
26 if !hasmapto("<leader>d",'n')
27 nnoremap <script> <silent> <buffer> <leader>d 0"=strftime("%Y-%m-%d ")<CR>P
28 endif
29
30 if !hasmapto("date<Tab>",'i')
31 inoremap <script> <silent> <buffer> date<Tab> <C-R>=strftime("%Y-%m-%d")<CR>
32 endif
33
34 if !hasmapto("<leader>d",'v')
35 vnoremap <script> <silent> <buffer> <leader>d c<C-R>=strftime("%Y-%m-%d")<CR><Esc>
36 endif
37
38 " Mark done {{{2
39 if !hasmapto("<leader>D",'n')
40 nnoremap <script> <silent> <buffer> <leader>D 0"=strftime("x %Y-%m-%d ")<CR>P
41 endif
42
43 " Folding {{{1
44 " Options {{{2
45 setlocal foldmethod=expr
46 setlocal foldexpr=TodoFoldLevel(v:lnum)
47 setlocal foldtext=TodoFoldText()
48
49 " TodoFoldLevel(lnum) {{{2
50 function! TodoFoldLevel(lnum)
51 " The match function returns the index of the matching pattern or -1 if
52 " the pattern doesn't match. In this case, we always try to match a
53 " completed task from the beginning of the line so that the matching
54 " function will always return -1 if the pattern doesn't match or 0 if the
55 " pattern matches. Incrementing by one the value returned by the matching
56 " function we will return 1 for the completed tasks (they will be at the
57 " first folding level) while for the other lines 0 will be returned,
58 " indicating that they do not fold.
59 return match(getline(a:lnum),'^[xX]\s.\+$') + 1
60 endfunction
61
62 " TodoFoldText() {{{2
63 function! TodoFoldText()
64 " The text displayed at the fold is formatted as '+- N Completed tasks'
65 " where N is the number of lines folded.
66 return '+' . v:folddashes . ' '
67 \ . (v:foldend - v:foldstart + 1)
68 \ . ' Completed tasks '
69 endfunction
70
71 " Restore context {{{1
72 let &cpo = s:save_cpo
73 " Modeline {{{1
74 " vim: ts=8 sw=4 sts=4 et foldenable foldmethod=marker foldcolumn=1