Skip to content

Commit

Permalink
vimoutlinerrc: Rewrote JumpToTargetID(); allow multiple links per line.
Browse files Browse the repository at this point in the history
In addition, got rid of all the UUID stuff.
  • Loading branch information
hfuchs committed Mar 29, 2012
1 parent 1bbfac3 commit 606a2af
Showing 1 changed file with 73 additions and 65 deletions.
138 changes: 73 additions & 65 deletions vimoutlinerrc
@@ -1,88 +1,96 @@
" 2009-08-11, Created by H Fuchs <hagen.fuchs@physik.tu-dresden.de>
" Adding UUID-functionality to vimoutliner
" 2010-02-26, Adding complete jump-suite. To define a jump target, just
" place square brackets around it: '[target]'. To define a jump, use
" '[-> target]' or just '[> target]'. Nifty, I think.
" 2010-03-24, Deactivated the UUID-parts - I'm not using the anymore.
" 2009-08-11, Created by Hagen Fuchs <code@hfuchs.net>
"
" The definitions here define a complete link-suite (whatever /that/ is)
" for Vimoutliner. To define a target, just place square brackets
" around it: '[target]'. To link to it, simply write '[-> target]' or
" just '[> target]' and hit '<localleader>j'. Nifty, I think.
"
" In addition, there's a slightly hacky autocommand definition here that
" I need to protect myself from the constant re-evaluation of foldexpr.


" --- Command Bindings
" ,,u - Insert UUID (as jump target)
" ,,U - Convert UUID (as jump origin)
" ,,j - Jump from origin to target
"
" Note: The '~<esc>[...]x' gives a known position, as leaving insert
" mode with ESC jumps one position back - unless at the beginning of the
" line.
"nmap <buffer> <localleader>u :call InsertTargetID()<CR>
"imap <buffer> <localleader>u ~<ESC>:call InsertTargetID()<CR>s
"nmap <buffer> <localleader>U :call ConvertTargetIDtoJumpID()<CR>
"imap <buffer> <localleader>U ~<ESC>:call ConvertTargetIDtoJumpID()<CR>s
nmap <buffer> <localleader>j :call JumpToTargetID()<CR>
" 2011-08-08, Add mappings to my own abbreviations.
"nunmap <buffer> <localleader>t
"nunmap <buffer> <localleader>d
nmap <buffer> <localleader>t tme<CR>
nmap <buffer> <localleader>d tdy<CR>
" 2010-03-01, Convenience-definition: Open file in window with ,,o
" 2010-03-24, Useless. Plus, I might need the o-binding for cycling
" jump origins. :)
"nmap <buffer> <localleader>o <c-w>f
" 2012-03-16, Autocommand to update folds on /serious/ idle time (one
" hour).
set updatetime=3600000
autocmd CursorHold * call SwitchFoldmethod()


" --- Function Definitions
" InsertUUID {{{1
" TODO Descriptoi
function! InsertUUID()
execute "normal! i" . substitute(system('uuid'), '\n', '', '')
endfunction
" Hint: Reload in running Vimoutliner session with ':source ~/.vimoutlinerrc'.

function! InsertTargetID()
normal! i[]
call InsertUUID()
normal! ll
endfunction
"}}}1

" ConvertTargetIDtoJumpID - TODO Description {{{1
function! ConvertTargetIDtoJumpID()
let line = getline(".") " Contents of the current line
" TODO Generalize or Delete.
let uuid_pattern = '\m\[\(\x\{8}-\x\{4}-\x\{4}-\x\{4}-\x\{12}\)\]'
if line =~ uuid_pattern
call setline(".", substitute(line, uuid_pattern, '\[-> \1\]', ""))
else
echo "No ID found on this line."
endif
" SwitchFoldmethod {{{1
" Hacky method of avoiding the costly evaluation foldexpr on every
" keystroke by switching to manual folding for most of the time (folds
" will /not/ be updated) and update all folds after a certain idle
" period (see updatetime above).
function! SwitchFoldmethod()
echomsg "Switching to expression-based folding."
set foldmethod=expr
" update folds - necessary to kickstart reevaluation.
normal! zx
echomsg "Switching back to manual folding."
set foldmethod=manual
endfunction
"}}}1
" }}}1

" JumpToTargetID - TODO Description {{{1
" JumpToTargetID {{{1
" Implements a interlinking feature for Vimoutliner. If you are on
" a line containing [> string], cursor positioned inside the brackets,
" then calling this function will jump to the (nearest) occurence of
" [string]. Really simple but rather effective.
function! JumpToTargetID()
" Note: If there are two jump-statements on the same line, the last
" one will be used - silently.
let timeout = 1000 " Stop search after a second
let line = getline('.')
" The pattern matches the complete line and saves the jump target
" TODO 2011-09-05, Add ability to wrap around newlines! See VIM's
" '\_' modifier.
let pattern = '\m^.*\[-\?>\s\+\([^\[\]]\+\)\].*$'
" Triple backslash to protect the backslash *and* the bracket.
" TODO Could've just \M, right?
let uuid = substitute(line, pattern, '\\\[\1\\\]', '')
if uuid == line " TODO Use match() instead of substitute().
let l:line = getline('.')
let l:pattern = '\m\[-\?>\s\+\([^\[\]]\+\)\]'
let l:links = [[]]
let l:timeout = 1000

" Find all occurences of pattern in string and note their position.
let l:i = 1
while l:i
let l:begin = match(l:line, l:pattern, 0, l:i)
if l:begin != -1
let l:end = matchend(l:line, l:pattern, 0, l:i)
let l:id = matchstr(l:line, l:pattern, 0, l:i)
let l:target = substitute(l:id, l:pattern, '\\\[\1\\\]', '')
let l:links += [[ l:begin, l:end, target ]]
let l:i += 1
else
break
endif
endwhile

" If the list is still empty, quit now.
if l:links == [[]]
echo "No ID found on this line."
return
else
" 2010-03-24, Don't need setpos(), search() itsself already sets
" the cursor (parameter 's' sets a jump mark)!
if search(uuid, 'sw', 0, timeout) == 0
echo "Target ID not found!"
call remove(l:links, 0)
endif

" Check whether the cursor is placed inside of one of the links and
" jump there if so.
let cursorpos = getpos(".")[2]
for l:link in l:links
if index(range(l:link[0]+1, l:link[1]), cursorpos) != -1
" 2010-03-24, Don't need setpos(), search() itsself already
" sets the cursor (parameter 's' sets a jump mark)!
if search(l:link[2], 'sw', 0, l:timeout) == 0
echo "Target ID not found!"
endif
return
endif
endfor

" If the cursor's positioned elsewhere, use the last jumpID.
if search(l:links[-1][2], 'sw', 0, l:timeout) == 0
echo "Target ID not found!"
endif
endfunction
" }}}1


" vim:ft=vim:foldmethod=marker

0 comments on commit 606a2af

Please sign in to comment.