On Thu, Sep 3, 2009 at 5:47 PM, David Liang<[email protected]> wrote:
>
> The :sort command is a line-wise sort. What I want to do is sort
> sections of a file as defined by folds. For example:
>
> # {{{
> a
> z
> # }}}
>
> # {{{
> a
> bcd
> # }}}
>
> Here the entire second fold would be moved before the first fold. Is
> this possible?
>
If you collect the folds as lists, then you can sort them and spit
them out. Sorting in place is doable, but a little more complicated,
however a 3 pronged approach is easier. Here is one way to do it.
First write a function that can collect all lines in folds separately.
function! CollectFoldList(foldList)
0
while line('.') < line('$')
if foldclosed(line('.')) != -1
call add(a:foldList, getline(foldclosed(line('.')),
foldclosedend(line('.'))))
exec (foldclosedend(line('.'))+1)
else
+
endif
endwhile
endfunction
Then write another function that can take this list and dump into a buffer.
function! PutFoldListIntoNewBuf(foldList)
for fold in a:foldList
call append('$', fold)
" Optionally add a line between them.
call append('$', "")
endfor
endfunction
You can then glue these function together like this:
function! CollectFoldsSortAndPutIntoNewBuf()
let foldList = []
call CollectFoldList(foldList)
call sort(foldList)
new
call PutFoldListIntoNewBuf(foldList)
endfunction
We are simply relying on Vim's ability to sort the folds by using
their string representation, which I think what you want. Otherwise,
you can pass in a comparator function and change the behavior.
--
HTH,
Hari
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---