1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
"######################################################################
"# VimOutliner Hoisting
"# Copyright (C) 2003 by Noel Henson noel@noels-lab.com
"# The file is currently an experimental part of Vim Outliner.
"#
"# This program is free software; you can redistribute it and/or modify
"# it under the terms of the GNU General Public License as published by
"# the Free Software Foundation; either version 2 of the License, or
"# (at your option) any later version.
"#
"# This program is distributed in the hope that it will be useful,
"# but WITHOUT ANY WARRANTY; without even the implied warranty of
"# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
"# GNU General Public License for more details.
"######################################################################
" Detailed Revision Log {{{1
"vo_hoist.vim
"Internal RCS
"$Revision: 1.10 $"
"$Date: 2005/06/12 15:53:54 $
"$Log: vo_hoist.vim,v $
"Revision 1.10 2005/06/12 15:53:54 noel
"Moved key mappings so they work with Matej' new way to load plugins.
"
"Revision 1.9 2003/11/12 17:26:09 noel
"Added a command to place the cursor on the first line of
"a hoisted outline.
"
"Revision 1.8 2003/11/12 17:10:51 noel
"Fixed a bug that occurs on a level 1 heading with no children.
"
"Revision 1.7 2003/10/23 22:14:14 noel
"Minor changes to DeHoist() to compensate for current foldlevel settings.
"
"Revision 1.6 2003/08/17 15:35:24 noel
"Put the new mappings in the correct place this time.
"Added a : and <cr> to the ZZ command.
"
"Revision 1.5 2003/08/17 14:47:42 noel
"Added ZZ, qa, and x to the list of commands that de-hoist the current
"outline.
"
"Revision 1.4 2003/08/17 00:07:31 noel
"Added "silent" to commands generating tedious messages.
"
"Revision 1.3 2003/08/16 20:08:06 noel
"Removed a need to exclude fold level 1 headings.
"
"Revision 1.2 2003/08/16 19:02:44 noel
"First fully functional version. May need some tweaks but it works and is
"quite easy to use.
"
"Revision 1.1 2003/08/14 21:05:05 noel
"First publicly available, experiment verison
"
"}}}2
" Load the plugin {{{1
" mappings {{{1
map <silent> <buffer> <localleader>h :call Hoist(line("."))<cr>
map <silent> <buffer> <localleader>H :call DeHoistThis(line("."))<cr>
"}}}1
if exists("g:did_vo_hoist")
finish
endif
if !exists("g:hoistParanoia")
let g:hoistParanoia=0
endif
let g:did_vo_hoist = 1
" Functions {{{1
" RemoveTabsLine(line,tabs) {{{2
" remove specified number of tabs from the begining of line
function! RemoveTabsLine(line,tabs)
return substitute(getline(a:line),"^\\(\\t\\)\\{".a:tabs."}", "", "")
endfunction
"}}}2
" MakeTempFilename(line) {{{2
" return a string to use as the temporary filename for the hoisted area
function! MakeTempFilename(line)
return "vo_hoist.".a:line.strftime(".%Y%m%d%H%M%S").".otl"
endfunction
"}}}2
" AddHoistFilename(line) {{{2
" Add a temporary filename to a parent line to indicate hoisting
function! AddHoistFilename(line)
let l:newparent = getline(a:line)." __hoist:".MakeTempFilename(a:line)
call setline(a:line,l:newparent)
endfunction
"}}}2
"}}}2
" DeleteHoistFilename(line) {{{2
" Delete a temporary filename from a parent line
function! DeleteHoistFilename(line)
call setline(a:line,substitute(getline(a:line)," __hoist:.*","",""))
endfunction
"}}}2
" ExtractHoistFilename(line) {{{2
" Extract a filename from a hoisted parent
function! ExtractHoistFilename(line)
return substitute(getline(a:line),".* __hoist:","","")
endfunction
"}}}2
" IsParent(line) {{{2
" Return 1 if this line is a parent
function! IsParent(line)
return (Ind(a:line)+1) == Ind(a:line+1)
endfunction
"}}}2
" IsHoistedParent(line) {{{2
" Return 1 if this line is a parent with hoisted children
function! IsHoistParent(line)
return match(getline(a:line)," __hoist:","") != -1
endfunction
"}}}2
" FindParent(line) {{{2
" Return line if parent, parent line if not
function! FindParent(line)
if IsParent(a:line)
return a:line
else
let l:parentindent = Ind(a:line)-1
let l:searchline = a:line
while (Ind(l:searchline) != l:parentindent) && (l:searchline > 0)
let l:searchline = l:searchline-1
endwhile
return l:searchline
endif
endfunction
"}}}2
" FindLastChild(line) {{{2
" Return the line number of the last decendent of parent line
function! FindLastChild(line)
let l:parentindent = Ind(a:line)
let l:searchline = a:line+1
while Ind(l:searchline) > l:parentindent
let l:searchline = l:searchline+1
endwhile
return l:searchline-1
endfunction
"}}}2
"}}}2
" Hoist(line) {{{2
" Write the offspring of a parent to a new file, open it and remove the
" leading tabs.
function! Hoist(line)
let l:parent = FindParent(a:line)
if l:parent == 0
return
endif
call cursor(l:parent,1)
let l:firstline = l:parent+1
let l:childindent = Ind(l:firstline)
let l:lastline = FindLastChild(l:parent)
let l:filename = MakeTempFilename(l:parent)
echo l:firstline.",".l:lastline."w! ".l:filename
let l:folded = foldclosed(l:parent)
call cursor(l:parent,1)
normal zo
exe l:firstline.",".l:lastline."w! ".l:filename
call AddHoistFilename(l:parent)
silent write
" log what we did incase we need to recover manually
let l:doit = l:parent."write! >> .vo_hoist.".bufname(bufnr("%")).".log"
exe l:doit
let l:parentbuffer = bufnr("%")
"WARNING: switching files
let l:doit = "silent e +%s/^\\\\(\\\t\\\\)\\\\{"
let l:doit = l:doit.l:childindent."}// ".l:filename." | "
let l:doit = l:doit."let b:myParentBuffer = ".l:parentbuffer." | "
let l:doit = l:doit."let b:myParentLine = ".l:parent." | "
let l:doit = l:doit."call cursor(1,1)|"
let l:doit = l:doit."let b:hoisted = 1"
exe l:doit
silent write
endfunction
"}}}2
" DeleteChildren(line) {{{2
" Delete the existing offspring of a parent
function! DeleteChildren(line)
let l:parent = FindParent(a:line)
let l:firstline = l:parent+1
let l:lastline = FindLastChild(l:parent)
exe l:firstline.",".l:lastline."d"
endfunction
"}}}2
" MakeTabString(n) {{{2
" Return a string of n tabs
function! MakeTabString(n)
let l:string = ""
let l:i = 0
while l:i < a:n
let l:string = l:string."\t"
let l:i = l:i +1
endwhile
return l:string
endfunction
"}}}2
" AddChildren(line) {{{2
" Add left-justified children to parent. The filename is extracted from the
" end of the parent line. The parent is assumed to have no children at this
" point.
function! AddChildren(line)
let l:filename = ExtractHoistFilename(a:line)
if filereadable(l:filename) == 1
if a:line == line("$")
exe "read ".l:filename
if a:line != line("$")
exe a:line+1.",$"." s/^/".MakeTabString(Ind(a:line)+1)."/"
endif
else
exe a:line+1."ma v"
call cursor(a:line,1)
exe "read ".l:filename
if a:line+1 != line("'v")
exe a:line+1.",'v-1"." s/^/".MakeTabString(Ind(a:line)+1)."/"
endif
endif
endif
endfunction
"}}}2
" DeleteHoistFile(line) {{{2
" Delete a temporary filename from a parent line
function! DeleteHoistFile(line)
if g:hoistParanoia
return
endif
let l:filename = ExtractHoistFilename(a:line)
call delete(l:filename)
let l:filename = l:filename."~"
call delete(l:filename)
endfunction
"}}}2
" DeHoistThis(line) {{{2
" Remove the old children, add the new children and remove the __hoist data
" leading tabs from this file.
function! DeHoistThis(line)
let l:parent = FindParent(a:line)
let l:folded = foldclosed(l:parent)
call cursor(l:parent,1)
if l:folded == l:parent
normal zo
endif
call DeleteChildren(l:parent)
call AddChildren(l:parent)
call DeleteHoistFile(l:parent)
call DeleteHoistFilename(l:parent)
if l:folded == l:parent
normal zc
endif
endfunction
"}}}2
" DeHoist() {{{2
" Remove the old children, add the new children and remove the __hoist data
" leading tabs from the calling file.
function! DeHoist()
silent write
if bufexists(b:myParentBuffer) == 0
return
endif
let l:myParentBuffer = b:myParentBuffer
let l:myParentLine = b:myParentLine
bdelete
" Warning switching files
exe "buffer ".l:myParentBuffer
call cursor(l:myParentLine,1)
let l:parent = FindParent(l:myParentLine)
let l:folded = foldclosed(l:parent)
call cursor(l:parent,1)
" if l:folded == l:parent
" normal zo
" endif
normal zv
silent call DeleteChildren(l:parent)
silent call AddChildren(l:parent)
silent call DeleteHoistFile(l:parent)
silent call DeleteHoistFilename(l:parent)
if l:folded == l:parent
call cursor(l:parent,1)
normal zc
endif
silent write
endfunction
"}}}2
"}}}1
" Autocommands {{{1
au BufReadPost vo_hoist.*.otl cmap <buffer> wq call DeHoist()
au BufReadPost vo_hoist.*.otl cmap <buffer> qa call DeHoist()
au BufReadPost vo_hoist.*.otl cmap <buffer> q call DeHoist()
au BufReadPost vo_hoist.*.otl cmap <buffer> x call DeHoist()
au BufReadPost vo_hoist.*.otl nmap <buffer> ZZ :call DeHoist()<cr>
"}}}1
" vim600: set foldlevel=0 foldmethod=marker:
|