Implemented the compare method, but the sorting is not working

This commit is contained in:
Leandro Freitas
2011-06-03 00:33:56 -03:00
parent 9ce565deb0
commit 5357d75e0d
2 changed files with 77 additions and 52 deletions

View File

@@ -4,7 +4,7 @@
(C) 2011-06-01 Improve syntax file @syntax (C) 2011-06-01 Improve syntax file @syntax
2011-05-30 Map commands to add, rm, ls, pri, depri etc @ftplugin 2011-05-30 Map commands to add, rm, ls, pri, depri etc @ftplugin
2011-05-30 Contact main project for reference 2011-05-30 Contact main project for reference
X 2011-05-31 Stop breaking lines automatically @ftplugin
X 2011-05-30 Implement filetype detection @ftdetect X 2011-05-30 Implement filetype detection @ftdetect
X 2011-05-30 Implement colorized priorities @syntax X 2011-05-30 Implement colorized priorities @syntax
X 2011-05-31 Highlight date, project and context of tasks with no priority @syntax X 2011-05-31 Highlight date, project and context of tasks with no priority @syntax
X 2011-05-31 Stop breaking lines automatically @ftplugin

View File

@@ -1,57 +1,82 @@
" Vim Todo.txt plugin " File: todo.txt.vim
" Last Change: 2011 Jun 01 " Description: Todo.txt filetype detection
" Maintainer: Leandro Freitas <freitass@gmail.com> " Author: Leandro Freitas <freitass@gmail.com>
" License: This file is placed in the public domain. " Licence: Vim licence
" Website: http://github.com/freitass/todo.txt.vim
" Version: 0.2
if exists("g:loaded_todo") " TODO uncomment
finish "if exists("g:loaded_todo")
endif " finish
"endif
"let g:loaded_todo = 1
let s:save_cpo = &cpo
set cpo&vim
" Stop breaking lines automatically
setlocal textwidth=0 setlocal textwidth=0
setlocal wrapmargin=0 setlocal wrapmargin=0
"" Vim global plugin for correcting typing mistakes if !hasmapto('<Plug>TodoSortlines')
"" Last Change: 2000 Oct 15 map <unique> <leader>s <Plug>TodoSortlines
"" Maintainer: Bram Moolenaar <Bram@vim.org> endif
"" License: This file is placed in the public domain.
"
"if exists("g:loaded_typecorr")
" finish
"endif
"let g:loaded_typecorr = 1
"
"let s:save_cpo = &cpo
"set cpo&vim
"
"iabbrev teh the
"iabbrev otehr other
"iabbrev wnat want
"iabbrev synchronisation
" \ synchronization
"let s:count = 4
"
"if !hasmapto('<Plug>TypecorrAdd')
" map <unique> <Leader>a <Plug>TypecorrAdd
"endif
"noremap <unique> <script> <Plug>TypecorrAdd <SID>Add
"
"noremenu <script> Plugin.Add\ Correction <SID>Add
"
"noremap <SID>Add :call <SID>Add(expand("<cword>"), 1)<CR>
"
"function s:Add(from, correct)
" let to = input("type the correction for " . a:from . ": ")
" exe ":iabbrev " . a:from . " " . to
" if a:correct | exe "normal viws\<C-R>\" \b\e" | endif
" let s:count = s:count + 1
" echo s:count . " corrections now"
"endfunction
"
"if !exists(":Correct")
" command -nargs=1 Correct :call s:Add(<q-args>, 0)
"endif
"
"let &cpo = s:save_cpo
let g:loaded_todo = 1 "TODO replace
"noremap <unique> <script> <Plug>TodoSortlines <SID>SortLines
noremap <script> <Plug>TodoSortlines <SID>SortLines
noremap <SID>SortLines :call <SID>SortLines()<CR>
function! s:Compare(line1,line2)
" Matches a line starting with 'X '.
let done_pattern = '\m^X\s\+.*'
" Matches a line starting with '(\a) ' where \a is a letter.
let priority_pattern = '\m^(\a)\s\+.*'
" Matches date YYYY-MM-DD
let date_pattern = '\m\d\{4\}-\d\{2\}-\d\{2\}'
" Check PRIORITY first.
let done_1 = match(a:line1,done_pattern)
let done_2 = match(a:line2,done_pattern)
if done_1 != done_2
" Swap if only the first has been done.
return -done_2
endif
let pri_set_1 = match(a:line1,priority_pattern)
let pri_set_2 = match(a:line2,priority_pattern)
if pri_set_1 != pri_set_2
" Swap if only the second has priority set.
return -pri_set_1
elseif pri_set_1 >= 0
" Swap if the second has higher proprity.
return a:line2[1] <? a:line1[1]
endif
" If the execution reached this code, both tasks have the same priority.
" Go for the date, sorting older tasks first.
let x1 = match(a:line1,date_pattern)
let x2 = match(a:line2,date_pattern)
if x1 != x2
" Swap if only the second has creation date.
return -x1
endif
if x2 == -1
" If none has date, keep the order.
return 0
endif
" Calculate which date should come first
let date_1 = a:line1[(x1):(x1+9)]
let date_1 = substitute(date_1,"-","","g")
let date_2 = a:line2[(x2):(x2+9)]
let date_2 = substitute(date_2,"-","","g")
return date_1 > date_2
endfunction
function! s:SortLines()
let list_of_lines = getline(0,'$')
let sorted_list = sort(copy(list_of_lines),'s:Compare')
"%delete
for line in sorted_list
echo line
endfor
endfunction
let &cpo = s:save_cpo