]>
Commit | Line | Data |
---|---|---|
189288f7 | 1 | " File: todo.txt.vim |
2 | " Description: Todo.txt filetype detection | |
3 | " Author: Leandro Freitas <freitass@gmail.com> | |
4 | " License: Vim license | |
5 | " Website: http://github.com/freitass/todo.txt-vim | |
6 | " Version: 0.4 | |
7 | ||
8 | " Export Context Dictionary for unit testing {{{1 | |
9 | function! s:get_SID() | |
10 | return matchstr(expand('<sfile>'), '<SNR>\d\+_') | |
11 | endfunction | |
12 | let s:SID = s:get_SID() | |
13 | delfunction s:get_SID | |
14 | ||
15 | function! todo#txt#__context__() | |
16 | return { 'sid': s:SID, 'scope': s: } | |
17 | endfunction | |
18 | ||
19 | " Functions {{{1 | |
20 | function! s:remove_priority() | |
21 | :s/^(\w)\s\+//ge | |
22 | endfunction | |
23 | ||
24 | function! s:get_current_date() | |
25 | return strftime('%Y-%m-%d') | |
26 | endfunction | |
27 | ||
28 | function! todo#txt#prepend_date() | |
29 | execute 'normal! I' . s:get_current_date() . ' ' | |
30 | endfunction | |
31 | ||
32 | function! todo#txt#replace_date() | |
33 | let current_line = getline('.') | |
34 | if (current_line =~ '^\(([a-zA-Z]) \)\?\d\{2,4\}-\d\{2\}-\d\{2\} ') && | |
35 | \ exists('g:todo_existing_date') && g:todo_existing_date == 'n' | |
36 | return | |
37 | endif | |
38 | execute 's/^\(([a-zA-Z]) \)\?\(\d\{2,4\}-\d\{2\}-\d\{2\} \)\?/\1' . s:get_current_date() . ' /' | |
39 | endfunction | |
40 | ||
41 | function! todo#txt#mark_as_done() | |
42 | call s:remove_priority() | |
43 | call todo#txt#prepend_date() | |
44 | execute 'normal! Ix ' | |
45 | endfunction | |
46 | ||
47 | function! todo#txt#mark_all_as_done() | |
48 | :g!/^x /:call todo#txt#mark_as_done() | |
49 | endfunction | |
50 | ||
51 | function! s:append_to_file(file, lines) | |
52 | let l:lines = [] | |
53 | ||
54 | " Place existing tasks in done.txt at the beggining of the list. | |
55 | if filereadable(a:file) | |
56 | call extend(l:lines, readfile(a:file)) | |
57 | endif | |
58 | ||
59 | " Append new completed tasks to the list. | |
60 | call extend(l:lines, a:lines) | |
61 | ||
62 | " Write to file. | |
63 | call writefile(l:lines, a:file) | |
64 | endfunction | |
65 | ||
66 | function! todo#txt#remove_completed() | |
67 | " Check if we can write to done.txt before proceeding. | |
68 | ||
69 | let l:target_dir = expand('%:p:h') | |
70 | let l:todo_file = expand('%:p') | |
71 | let l:done_file = substitute(substitute(l:todo_file, 'todo.txt$', 'done.txt', ''), 'Todo.txt$', 'Done.txt', '') | |
72 | if !filewritable(l:done_file) && !filewritable(l:target_dir) | |
73 | echoerr "Can't write to file 'done.txt'" | |
74 | return | |
75 | endif | |
76 | ||
77 | let l:completed = [] | |
78 | :g/^x /call add(l:completed, getline(line(".")))|d | |
79 | call s:append_to_file(l:done_file, l:completed) | |
80 | endfunction | |
81 | ||
82 | function! todo#txt#sort_by_context() range | |
83 | execute a:firstline . "," . a:lastline . "sort /\\(^\\| \\)\\zs@[^[:blank:]]\\+/ r" | |
84 | endfunction | |
85 | ||
86 | function! todo#txt#sort_by_project() range | |
87 | execute a:firstline . "," . a:lastline . "sort /\\(^\\| \\)\\zs+[^[:blank:]]\\+/ r" | |
88 | endfunction | |
89 | ||
90 | function! todo#txt#sort_by_date() range | |
91 | let l:date_regex = "\\d\\{2,4\\}-\\d\\{2\\}-\\d\\{2\\}" | |
92 | execute a:firstline . "," . a:lastline . "sort /" . l:date_regex . "/ r" | |
93 | execute a:firstline . "," . a:lastline . "g!/" . l:date_regex . "/m" . a:lastline | |
94 | endfunction | |
95 | ||
96 | function! todo#txt#sort_by_due_date() range | |
97 | let l:date_regex = "due:\\d\\{2,4\\}-\\d\\{2\\}-\\d\\{2\\}" | |
98 | execute a:firstline . "," . a:lastline . "sort /" . l:date_regex . "/ r" | |
99 | execute a:firstline . "," . a:lastline . "g!/" . l:date_regex . "/m" . a:lastline | |
100 | endfunction | |
101 | ||
102 | " Increment and Decrement The Priority | |
103 | :set nf=octal,hex,alpha | |
104 | ||
105 | function! todo#txt#prioritize_increase() | |
106 | normal! 0f)h\ 1 | |
107 | endfunction | |
108 | ||
109 | function! todo#txt#prioritize_decrease() | |
110 | normal! 0f)h\18 | |
111 | endfunction | |
112 | ||
113 | function! todo#txt#prioritize_add(priority) | |
114 | " Need to figure out how to only do this if the first visible letter in a line is not ( | |
115 | :call todo#txt#prioritize_add_action(a:priority) | |
116 | endfunction | |
117 | ||
118 | function! todo#txt#prioritize_add_action(priority) | |
119 | execute 's/^\(([a-zA-Z]) \)\?/(' . a:priority . ') /' | |
120 | endfunction | |
121 | ||
122 | " Modeline {{{1 | |
123 | " vim: ts=8 sw=4 sts=4 et foldenable foldmethod=marker foldcolumn=1 |