" File: todo.txt.vim " Description: Todo.txt sorting plugin " Author: David Beniamine " Licence: Vim licence " Website: http://github.com/dbeniamine/todo.txt.vim " Version: 0.7 " vim: ts=4 sw=4 :help tw=78 cc=80 " These two variables are parameters for the successive calls the vim sort " '' means no flags " '! i' means reverse and ignore case " for more information on flags, see :help sort if (! exists("g:Todo_txt_first_level_sort_mode")) let g:Todo_txt_first_level_sort_mode='i' endif if (! exists("g:Todo_txt_second_level_sort_mode")) let g:Todo_txt_second_level_sort_mode='i' endif if (! exists("g:Todo_txt_third_level_sort_mode")) let g:Todo_txt_third_level_sort_mode='i' endif " Sort todo by (first) context if !hasmapto("sc",'n') noremap sc :call Todo_txt_HierarchicalSort('@', '', 1) endif if !hasmapto("scp",'n') noremap scp :call Todo_txt_HierarchicalSort('@', '+', 1) endif " Sort todo by (first) project if !hasmapto("sp",'n') noremap sp :call Todo_txt_HierarchicalSort('+', '',1) endif if !hasmapto("spc",'n') noremap spc :call Todo_txt_HierarchicalSort('+', '@',1) endif " This is a Hierarchical sort designed for todo.txt todo lists, however it " might be used for other files types " At the first level, lines are sorted by the word right after the first " occurence of a:symbol, there must be no space between the symbol and the " word. At the second level, the same kind of sort is done based on " a:symbolsub, is a:symbol==' ', the second sort doesn't occurs " Therefore, according to todo.txt syntaxt, if " a:symbol is a '+' it sort by the first project " a:symbol is an '@' it sort by the first context " The last level of sort is done directly on the line, so according to " todo.txt syntax, it means by priority. This sort is done if and only if the " las argument is not 0 function! Todo_txt_HierarchicalSort(symbol, symbolsub, dolastsort) if v:statusmsg =~ '--No lines in buffer--' "Empty buffer do nothing return endif "if the sort modes doesn't start by '!' it must start with a space let l:sortmode=Todo_txt_InsertSpaceIfNeeded(g:Todo_txt_first_level_sort_mode) let l:sortmodesub=Todo_txt_InsertSpaceIfNeeded(g:Todo_txt_second_level_sort_mode) let l:sortmodefinal=Todo_txt_InsertSpaceIfNeeded(g:Todo_txt_third_level_sort_mode) " Count the number of lines let l:position= getpos(".") execute "silent normal g\" let l:linecount=str2nr(split(v:statusmsg)[7]) " Get all the groups names let l:groups=GetGroups(a:symbol,1,l:linecount) " Sort by groups execute 'sort'.l:sortmode.' /.\{-}\ze'.a:symbol.'/' for l:g in l:groups let l:pat=a:symbol.l:g.'.*$' normal gg " Find the beginning of the group let l:groupBegin=search(l:pat,'c') " Find the end of the group let l:groupEnd=search(l:pat,'b') " I'm too lazy to sort groups of one line if(l:groupEnd==l:groupBegin) continue endif if a:dolastsort if( a:symbolsub!='') " Sort by subgroups let l:subgroups=GetGroups(a:symbolsub,l:groupBegin,l:groupEnd) " Go before the first line of the group " Sort the group using the second symbol for l:sg in l:subgroups normal gg let l:pat=a:symbol.l:g.'.*'.a:symbolsub.l:sg.'.*$\|'.a:symbolsub.l:sg.'.*'.a:symbol.l:g.'.*$' " Find the beginning of the subgroup let l:subgroupBegin=search(l:pat,'c') " Find the end of the subgroup let l:subgroupEnd=search(l:pat,'b') " Sort by priority execute l:subgroupBegin.','.l:subgroupEnd.'sort'.l:sortmodefinal endfor else " Sort by priority execute l:groupBegin.','.l:groupEnd.'sort'.l:sortmodefinal endif endif endfor " Restore the cursor position call setpos('.', position) endfunction " Returns the list of groups starting by a:symbol between lines a:begin and " a:end function! GetGroups(symbol,begin, end) let l:curline=a:begin let l:groups=[] while l:curline <= a:end let l:curproj=strpart(matchstr(getline(l:curline),a:symbol.'\a*'),1) if l:curproj != "" && index(l:groups,l:curproj) == -1 let l:groups=add(l:groups , l:curproj) endif let l:curline += 1 endwhile return l:groups endfunction " Insert a space if needed (the first char isn't '!' or ' ') in front of " sort parameters function! Todo_txt_InsertSpaceIfNeeded(str) let l:c=strpart(a:str,1,1) if( l:c != '!' && l:c !=' ') return " ".a:str endif retur a:str endfunction