" File: todo.txt.vim " Description: Todo.txt filetype detection " Author: David Beniamine , Leandro Freitas " License: Vim license " Website: http://github.com/dbeniamine/todo.txt-vim " Version: 0.7 " 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 invocated, which is whenever a file of this type is " created or edited. setlocal textwidth=0 setlocal wrapmargin=0 " Functions {{{1 function! s:TodoTxtRemovePriority() :s/^(\w)\s\+//ge endfunction function! TodoTxtPrependDate() normal! 0"=strftime("%Y-%m-%d ") P endfunction function! TodoTxtToggleMarkAsDone() if (getline(".") =~ 'x\s*\d\{4\}') :call TodoTxtUnMarkAsDone() else :call TodoTxtMarkAsDone() endif endfunction function! TodoTxtUnMarkAsDone() :s/\s*x\s*\d\{4}-\d\{1,2}-\d\{1,2}\s*//g endfunction function! TodoTxtMarkAsDone() call TodoTxtPrependDate() normal! Ix endfunction function! TodoTxtMarkAllAsDone() :g!/^x /:call TodoTxtMarkAsDone() endfunction function! s:AppendToFile(file, lines) let l:lines = [] " Place existing tasks in done.txt at the beggining of the list. if filereadable(a:file) call extend(l:lines, readfile(a:file)) endif " Append new completed tasks to the list. call extend(l:lines, a:lines) " Write to file. call writefile(l:lines, a:file) endfunction function! TodoTxtRemoveCompleted() " Check if we can write to done.txt before proceeding. let l:target_dir = expand('%:p:h') let l:done_file = l:target_dir.'/done.txt' if !filewritable(l:done_file) && !filewritable(l:target_dir) echoerr "Can't write to file 'done.txt'" return endif let l:completed = [] :g/^x /call add(l:completed, getline(line(".")))|d call s:AppendToFile(l:done_file, l:completed) endfunction function! TodoTxtSort() " vim :sort is usually stable " we sort first on contexts, then on projects and then on priority sort /@[a-zA-Z]*/ r ssort /+[a-zA-Z]*/ r sort /\v\([A-Z]\)/ r endfunction function! TodoTxtSortDue() silent! %s/\([dD][uU][eE]:\d\{4}\)-\(\d\{2}\)-\(\d\{2}\)/\1\2\3/g " Sort adding entries with due dates add the beginning sort n /[dD][uU][eE]:/ " Count the number of lines silent normal gg execute "/[dD][uU][eE]:" let l:first=getpos(".")[1] silent normal N let l:last=getpos(".")[1] let l:diff=l:last-l:first+1 " Put the sorted lines at the beginning of the file execute ':'.l:first execute ':d'.l:diff silent normal gg silent normal P silent! %s/\([dD][uU][eE]:\d\{4}\)\(\d\{2}\)/\1-\2-/g " TODO: add time sorting (YYYY-MM-DD HH:MM) endfunction " Mappings {{{1 " Sort tasks {{{2 if !hasmapto("s",'n') nnoremap