From 0e3c5cb2b62281c2915a291fe96f553c4b3be67f Mon Sep 17 00:00:00 2001 From: Leandro Freitas Date: Thu, 16 Apr 2015 22:56:09 -0300 Subject: [PATCH] Major refactoring --- autoload/todo/txt.vim | 106 +++++++++++++++++++++++++++ ftplugin/todo.vim | 154 ++++++++------------------------------- test/tc_date.vim | 6 +- test/tc_sort_context.vim | 2 +- test/tc_sort_date.vim | 2 +- test/tc_sort_project.vim | 2 +- 6 files changed, 141 insertions(+), 131 deletions(-) create mode 100644 autoload/todo/txt.vim diff --git a/autoload/todo/txt.vim b/autoload/todo/txt.vim new file mode 100644 index 0000000..b2b3a57 --- /dev/null +++ b/autoload/todo/txt.vim @@ -0,0 +1,106 @@ +" File: todo.txt.vim +" Description: Todo.txt filetype detection +" Author: Leandro Freitas +" License: Vim license +" Website: http://github.com/freitass/todo.txt-vim +" Version: 0.4 + +" Export Context Dictionary for unit testing {{{1 +function! s:get_SID() + return matchstr(expand(''), '\d\+_') +endfunction +let s:SID = s:get_SID() +delfunction s:get_SID + +function! todo#txt#__context__() + return { 'sid': s:SID, 'scope': s: } +endfunction + +" Functions {{{1 +function! s:remove_priority() + :s/^(\w)\s\+//ge +endfunction + +function! s:get_current_date() + return strftime('%Y-%m-%d') +endfunction + +function! todo#txt#prepend_date() + execute 'normal! 0"='.string(s:get_current_date().' ').' P' +endfunction + +function! todo#txt#mark_as_done() + call s:remove_priority() + call todo#txt#prepend_date() + normal! Ix +endfunction + +function! todo#txt#mark_all_as_done() + :g!/^x /:call todo#txt#mark_as_done() +endfunction + +function! s:append_to_file(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! todo#txt#remove_completed() + " Check if we can write to done.txt before proceeding. + + let l:target_dir = expand('%:p:h') + let l:todo_file = expand('%:p') + let l:done_file = substitute(substitute(l:todo_file, 'todo.txt$', 'done.txt', ''), 'Todo.txt$', '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:append_to_file(l:done_file, l:completed) +endfunction + +function! todo#txt#sort_by_context() range + execute a:firstline . "," . a:lastline . "sort /\\(^\\| \\)\\zs@[^[:blank:]]\\+/ r" +endfunction + +function! todo#txt#sort_by_project() range + execute a:firstline . "," . a:lastline . "sort /\\(^\\| \\)\\zs+[^[:blank:]]\\+/ r" +endfunction + +function! todo#txt#sort_by_date() range + execute a:firstline . "," . a:lastline . "sort! /\\d\\{2,4\\}-\\d\\{2\\}-\\d\\{2\\}/ r" +endfunction + +" Increment and Decrement The Priority +:set nf=octal,hex,alpha + +function! todo#txt#prioritize_increase() + normal! 0f)h +endfunction + +function! todo#txt#prioritize_decrease() + normal! 0f)h +endfunction + +function! todo#txt#prioritize_add(priority) + " Need to figure out how to only do this if the first visible letter in a line is not ( + :call todo#txt#prioritize_add_action(a:priority) +endfunction + +function! todo#txt#prioritize_add_action(priority) + execute "normal! mq0i(".a:priority.") \`q" +endfunction + +" Modeline {{{1 +" vim: ts=8 sw=4 sts=4 et foldenable foldmethod=marker foldcolumn=1 diff --git a/ftplugin/todo.vim b/ftplugin/todo.vim index fe2afbe..09eba55 100644 --- a/ftplugin/todo.vim +++ b/ftplugin/todo.vim @@ -9,17 +9,6 @@ let s:save_cpo = &cpo set cpo&vim -" Export Context Dictionary for unit testing {{{1 -function! s:get_SID() - return matchstr(expand(''), '\d\+_') -endfunction -let s:SID = s:get_SID() -delfunction s:get_SID - -function! todo#__context__() - return { 'sid': s:SID, 'scope': s: } -endfunction - " 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 @@ -27,72 +16,6 @@ endfunction setlocal textwidth=0 setlocal wrapmargin=0 -" Functions {{{1 -function! s:TodoTxtRemovePriority() - :s/^(\w)\s\+//ge -endfunction - -function! s:TodoTxtGetCurrentDate() - return strftime('%Y-%m-%d') -endfunction - -function! TodoTxtPrependDate() - execute 'normal! 0"='.string(s:TodoTxtGetCurrentDate().' ').' P' -endfunction - -function! TodoTxtMarkAsDone() - call s:TodoTxtRemovePriority() - 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:todo_file = expand('%:p') - let l:done_file = substitute(substitute(l:todo_file, 'todo.txt$', 'done.txt', ''), 'Todo.txt$', '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! TodoTxtSortByContext() range - execute a:firstline . "," . a:lastline . "sort /\\(^\\| \\)\\zs@[^[:blank:]]\\+/ r" -endfunction - -function! TodoTxtSortByProject() range - execute a:firstline . "," . a:lastline . "sort /\\(^\\| \\)\\zs+[^[:blank:]]\\+/ r" -endfunction - -function! TodoTxtSortByDate() range - execute a:firstline . "," . a:lastline . "sort! /\\d\\{2,4\\}-\\d\\{2\\}-\\d\\{2\\}/ r" -endfunction - " Mappings {{{1 " Sort tasks {{{2 if !hasmapto("s",'n') @@ -104,87 +27,68 @@ if !hasmapto("s",'v') endif if !hasmapto("s@",'n') - nnoremap