mirror of
https://github.com/freitass/todo.txt-vim.git
synced 2025-11-17 06:13:38 -05:00
Major refactoring
This commit is contained in:
106
autoload/todo/txt.vim
Normal file
106
autoload/todo/txt.vim
Normal file
@@ -0,0 +1,106 @@
|
||||
" File: todo.txt.vim
|
||||
" Description: Todo.txt filetype detection
|
||||
" Author: Leandro Freitas <freitass@gmail.com>
|
||||
" 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('<sfile>'), '<SNR>\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.") \<esc>`q"
|
||||
endfunction
|
||||
|
||||
" Modeline {{{1
|
||||
@@ -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('<sfile>'), '<SNR>\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("<leader>s",'n')
|
||||
@@ -104,87 +27,68 @@ if !hasmapto("<leader>s",'v')
|
||||
endif
|
||||
|
||||
if !hasmapto("<leader>s@",'n')
|
||||
if !hasmapto("<leader>s@",'n')
|
||||
nnoremap <script> <silent> <buffer> <leader>s@ :%call todo#txt#sort_by_context()<CR>
|
||||
endif
|
||||
|
||||
if !hasmapto("<leader>s@",'v')
|
||||
if !hasmapto("<leader>s@",'v')
|
||||
vnoremap <script> <silent> <buffer> <leader>s@ :call todo#txt#sort_by_context()<CR>
|
||||
endif
|
||||
|
||||
if !hasmapto("<leader>s+",'n')
|
||||
if !hasmapto("<leader>s+",'n')
|
||||
nnoremap <script> <silent> <buffer> <leader>s+ :%call todo#txt#sort_by_project()<CR>
|
||||
endif
|
||||
|
||||
if !hasmapto("<leader>s+",'v')
|
||||
if !hasmapto("<leader>s+",'v')
|
||||
vnoremap <script> <silent> <buffer> <leader>s+ :call todo#txt#sort_by_project()<CR>
|
||||
endif
|
||||
|
||||
if !hasmapto("<leader>sd",'n')
|
||||
if !hasmapto("<leader>sd",'n')
|
||||
nnoremap <script> <silent> <buffer> <leader>sd :%call todo#txt#sort_by_date()<CR>
|
||||
endif
|
||||
|
||||
if !hasmapto("<leader>sd",'v')
|
||||
if !hasmapto("<leader>sd",'v')
|
||||
vnoremap <script> <silent> <buffer> <leader>sd :call todo#txt#sort_by_date()<CR>
|
||||
endif
|
||||
|
||||
|
||||
" Increment and Decrement The Priority
|
||||
:set nf=octal,hex,alpha
|
||||
|
||||
function! TodoTxtPrioritizeIncrease()
|
||||
normal! 0f)h
|
||||
endfunction
|
||||
|
||||
function! TodoTxtPrioritizeDecrease()
|
||||
normal! 0f)h
|
||||
endfunction
|
||||
|
||||
function! TodoTxtPrioritizeAdd (priority)
|
||||
" Need to figure out how to only do this if the first visible letter in a line is not (
|
||||
:call TodoTxtPrioritizeAddAction(a:priority)
|
||||
endfunction
|
||||
|
||||
function! TodoTxtPrioritizeAddAction (priority)
|
||||
execute "normal! mq0i(".a:priority.") \<esc>`q"
|
||||
endfunction
|
||||
" Change priority {{{2
|
||||
if !hasmapto("<leader>j",'n')
|
||||
if !hasmapto("<leader>j",'n')
|
||||
nnoremap <script> <silent> <buffer> <leader>j :call todo#txt#prioritize_increase()<CR>
|
||||
endif
|
||||
|
||||
if !hasmapto("<leader>j",'v')
|
||||
if !hasmapto("<leader>j",'v')
|
||||
vnoremap <script> <silent> <buffer> <leader>j :call todo#txt#prioritize_increase()<CR>
|
||||
endif
|
||||
|
||||
if !hasmapto("<leader>k",'n')
|
||||
if !hasmapto("<leader>k",'n')
|
||||
nnoremap <script> <silent> <buffer> <leader>k :call todo#txt#prioritize_decrease()<CR>
|
||||
endif
|
||||
|
||||
if !hasmapto("<leader>k",'v')
|
||||
if !hasmapto("<leader>k",'v')
|
||||
vnoremap <script> <silent> <buffer> <leader>k :call todo#txt#prioritize_decrease()<CR>
|
||||
endif
|
||||
|
||||
if !hasmapto("<leader>a",'n')
|
||||
if !hasmapto("<leader>a",'n')
|
||||
nnoremap <script> <silent> <buffer> <leader>a :call todo#txt#prioritize_add('A')<CR>
|
||||
endif
|
||||
|
||||
if !hasmapto("<leader>a",'v')
|
||||
if !hasmapto("<leader>a",'v')
|
||||
vnoremap <script> <silent> <buffer> <leader>a :call todo#txt#prioritize_add('A')<CR>
|
||||
endif
|
||||
|
||||
if !hasmapto("<leader>b",'n')
|
||||
if !hasmapto("<leader>b",'n')
|
||||
nnoremap <script> <silent> <buffer> <leader>b :call todo#txt#prioritize_add('B')<CR>
|
||||
endif
|
||||
|
||||
if !hasmapto("<leader>b",'v')
|
||||
if !hasmapto("<leader>b",'v')
|
||||
vnoremap <script> <silent> <buffer> <leader>b :call todo#txt#prioritize_add('B')<CR>
|
||||
endif
|
||||
|
||||
if !hasmapto("<leader>c",'n')
|
||||
if !hasmapto("<leader>c",'n')
|
||||
nnoremap <script> <silent> <buffer> <leader>c :call todo#txt#prioritize_add('C')<CR>
|
||||
endif
|
||||
|
||||
if !hasmapto("<leader>c",'v')
|
||||
if !hasmapto("<leader>c",'v')
|
||||
vnoremap <script> <silent> <buffer> <leader>c :call todo#txt#prioritize_add('C')<CR>
|
||||
endif
|
||||
|
||||
" Insert date {{{2
|
||||
@@ -193,40 +97,40 @@ if !hasmapto("date<Tab>",'i')
|
||||
endif
|
||||
|
||||
if !hasmapto("<leader>d",'n')
|
||||
if !hasmapto("<leader>d",'n')
|
||||
nnoremap <script> <silent> <buffer> <leader>d :call todo#txt#prepend_date()<CR>
|
||||
endif
|
||||
|
||||
if !hasmapto("<leader>d",'v')
|
||||
if !hasmapto("<leader>d",'v')
|
||||
vnoremap <script> <silent> <buffer> <leader>d :call todo#txt#prepend_date()<CR>
|
||||
endif
|
||||
|
||||
" Mark done {{{2
|
||||
if !hasmapto("<leader>x",'n')
|
||||
if !hasmapto("<leader>x",'n')
|
||||
nnoremap <script> <silent> <buffer> <leader>x :call todo#txt#mark_as_done()<CR>
|
||||
endif
|
||||
|
||||
if !hasmapto("<leader>x",'v')
|
||||
if !hasmapto("<leader>x",'v')
|
||||
vnoremap <script> <silent> <buffer> <leader>x :call todo#txt#mark_as_done()<CR>
|
||||
endif
|
||||
|
||||
" Mark all done {{{2
|
||||
if !hasmapto("<leader>X",'n')
|
||||
if !hasmapto("<leader>X",'n')
|
||||
nnoremap <script> <silent> <buffer> <leader>X :call todo#txt#mark_all_as_done()<CR>
|
||||
endif
|
||||
|
||||
" Remove completed {{{2
|
||||
if !hasmapto("<leader>D",'n')
|
||||
if !hasmapto("<leader>D",'n')
|
||||
nnoremap <script> <silent> <buffer> <leader>D :call todo#txt#remove_completed()<CR>
|
||||
endif
|
||||
|
||||
" Folding {{{1
|
||||
" Options {{{2
|
||||
setlocal foldmethod=expr
|
||||
setlocal foldmethod=expr
|
||||
setlocal foldexpr=TodoFoldLevel(v:lnum)
|
||||
setlocal foldexpr=s:todo_fold_level(v:lnum)
|
||||
setlocal foldtext=s:todo_fold_text()
|
||||
|
||||
|
||||
" TodoFoldLevel(lnum) {{{2
|
||||
" s:todo_fold_level(lnum) {{{2
|
||||
function! s:todo_fold_level(lnum)
|
||||
" The match function returns the index of the matching pattern or -1 if
|
||||
" the pattern doesn't match. In this case, we always try to match a
|
||||
" completed task from the beginning of the line so that the matching
|
||||
@@ -238,8 +142,8 @@ function! TodoFoldLevel(lnum)
|
||||
return match(getline(a:lnum),'^[xX]\s.\+$') + 1
|
||||
endfunction
|
||||
|
||||
|
||||
" TodoFoldText() {{{2
|
||||
" s:todo_fold_text() {{{2
|
||||
function! s:todo_fold_text()
|
||||
" The text displayed at the fold is formatted as '+- N Completed tasks'
|
||||
" where N is the number of lines folded.
|
||||
return '+' . v:folddashes . ' '
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
let s:here = expand('<sfile>:p:h')
|
||||
let s:context = todo#__context__()
|
||||
let s:context = todo#txt#__context__()
|
||||
let s:context['data'] = s:here . '/tc_date.todo.txt'
|
||||
let s:tc = unittest#testcase#new('Date', s:context)
|
||||
|
||||
@@ -7,7 +7,7 @@ let s:LEADER = mapleader
|
||||
let s:TODAY = strftime("%Y-%m-%d")
|
||||
|
||||
function! s:tc.test_current_date()
|
||||
call self.assert_equal(s:TODAY, self.call('s:TodoTxtGetCurrentDate', []))
|
||||
call self.assert_equal(s:TODAY, self.call('s:get_current_date', []))
|
||||
endfunction
|
||||
|
||||
let s:DATE_INSERTED = [
|
||||
@@ -31,7 +31,7 @@ function! s:tc.test_insert_date_insert_mode()
|
||||
endfunction
|
||||
|
||||
function! s:tc.test_insert_date_visual_mode()
|
||||
call self.data.visual_execute('call TodoTxtPrependDate()', 'lorem_ipsum')
|
||||
call self.data.visual_execute('call todo#txt#prepend_date()', 'lorem_ipsum')
|
||||
call self.assert_equal(s:DATE_INSERTED_VISUAL, self.data.get('lorem_ipsum'))
|
||||
endfunction
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ let s:SORTED_TASKS = [
|
||||
\ ]
|
||||
|
||||
function! s:tc.test_sort_by_context()
|
||||
call self.data.visual_execute('call TodoTxtSortByContext()', 'lorem_ipsum')
|
||||
call self.data.visual_execute('call todo#txt#sort_by_context()', 'lorem_ipsum')
|
||||
call self.assert_equal(s:SORTED_TASKS, self.data.get('lorem_ipsum'))
|
||||
endfunction
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ let s:SORTED_TASKS = [
|
||||
\ ]
|
||||
|
||||
function! s:tc.test_sort_by_context()
|
||||
call self.data.visual_execute('call TodoTxtSortByDate()', 'lorem_ipsum')
|
||||
call self.data.visual_execute('call todo#txt#sort_by_date()', 'lorem_ipsum')
|
||||
call self.assert_equal(s:SORTED_TASKS, self.data.get('lorem_ipsum'))
|
||||
endfunction
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ let s:SORTED_TASKS = [
|
||||
\ ]
|
||||
|
||||
function! s:tc.test_sort_by_project()
|
||||
call self.data.visual_execute('call TodoTxtSortByProject()', 'lorem_ipsum')
|
||||
call self.data.visual_execute('call todo#txt#sort_by_project()', 'lorem_ipsum')
|
||||
call self.assert_equal(s:SORTED_TASKS, self.data.get('lorem_ipsum'))
|
||||
endfunction
|
||||
|
||||
|
||||
Reference in New Issue
Block a user