From 8ba7502b9d5b464df065e6b8bbbace20f38e3549 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Wed, 20 Oct 2010 18:28:02 +0200 Subject: add new vim plugins Signed-off-by: Florian Pritz --- .vim/autoload/repeat.vim | 72 ++++++++++++++++++ .vim/plugin/qbuf.vim | 190 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 262 insertions(+) create mode 100644 .vim/autoload/repeat.vim create mode 100644 .vim/plugin/qbuf.vim (limited to '.vim') diff --git a/.vim/autoload/repeat.vim b/.vim/autoload/repeat.vim new file mode 100644 index 0000000..e6ec409 --- /dev/null +++ b/.vim/autoload/repeat.vim @@ -0,0 +1,72 @@ +" repeat.vim - Let the repeat command repeat plugin maps +" Maintainer: Tim Pope +" Version: 1.0 + +" Installation: +" Place in either ~/.vim/plugin/repeat.vim (to load at start up) or +" ~/.vim/autoload/repeat.vim (to load automatically as needed). +" +" Developers: +" Basic usage is as follows: +" +" silent! call repeat#set("\MappingToRepeatCommand",3) +" +" The first argument is the mapping that will be invoked when the |.| key is +" pressed. Typically, it will be the same as the mapping the user invoked. +" This sequence will be stuffed into the input queue literally. Thus you must +" encode special keys by prefixing them with a backslash inside double quotes. +" +" The second argument is the default count. This is the number that will be +" prefixed to the mapping if no explicit numeric argument was given. The +" value of the v:count variable is usually correct and it will be used if the +" second parameter is omitted. If your mapping doesn't accept a numeric +" argument and you never want to receive one, pass a value of -1. +" +" Make sure to call the repeat#set function _after_ making changes to the +" file. + +if exists("g:loaded_repeat") || &cp || v:version < 700 + finish +endif +let g:loaded_repeat = 1 + +let g:repeat_tick = -1 + +function! repeat#set(sequence,...) + silent exe "norm! \"=''\p" + let g:repeat_sequence = a:sequence + let g:repeat_count = a:0 ? a:1 : v:count + let g:repeat_tick = b:changedtick +endfunction + +function! s:repeat(count) + if g:repeat_tick == b:changedtick + let c = g:repeat_count + let s = g:repeat_sequence + let cnt = c == -1 ? "" : (a:count ? a:count : (c ? c : '')) + call feedkeys(cnt . s) + else + call feedkeys((a:count ? a:count : '') . '.', 'n') + endif +endfunction + +function! s:wrap(command,count) + let preserve = (g:repeat_tick == b:changedtick) + exe 'norm! '.(a:count ? a:count : '').a:command + if preserve + let g:repeat_tick = b:changedtick + endif +endfunction + +nnoremap . :call repeat(v:count) +nnoremap u :call wrap('u',v:count) +nnoremap U :call wrap('U',v:count) +nnoremap :call wrap("\C-R>",v:count) + +augroup repeatPlugin + autocmd! + autocmd BufLeave,BufWritePre,BufReadPre * let g:repeat_tick = (g:repeat_tick == b:changedtick || g:repeat_tick == 0) ? 0 : -1 + autocmd BufEnter,BufWritePost * if g:repeat_tick == 0|let g:repeat_tick = b:changedtick|endif +augroup END + +" vim:set ft=vim et sw=4 sts=4: diff --git a/.vim/plugin/qbuf.vim b/.vim/plugin/qbuf.vim new file mode 100644 index 0000000..db5678f --- /dev/null +++ b/.vim/plugin/qbuf.vim @@ -0,0 +1,190 @@ +if v:version < 700 + finish +endif + +if !exists("g:qb_hotkey") || g:qb_hotkey == "" + let g:qb_hotkey = "" +endif +exe "nnoremap " g:qb_hotkey " :cal init(1):cal SBRun()" +exe "cnoremap " g:qb_hotkey "" + +if exists("g:qb_loaded") && g:qb_loaded + finish +endif +let g:qb_loaded = 1 + +let s:action2cmd = {"z": 'call switchbuf(#,"")', "!z": 'call switchbuf(#,"!")', + \"u": "hid b #|let s:cursel = (s:cursel+1) % s:blen", + \"s": "sb #", + \"d": 'call qbufdcmd(#,"")', "!d": 'call qbufdcmd(#,"!")', + \"w": "bw #", "!w": "bw! #", + \"l": "let s:unlisted = 1 - s:unlisted", + \"c": 'call closewindow(#,"")'} + +function s:rebuild() + redir @y | silent ls! | redir END + let s:buflist = [] + let s:blen = 0 + + for l:theline in split(@y,"\n") + if s:unlisted && l:theline[3] == "u" && (l:theline[6] != "-" || l:theline[5] != " ") + \ || !s:unlisted && l:theline[3] != "u" + if s:unlisted + let l:moreinfo = substitute(l:theline[5], "[ah]", " [+]", "") + else + let l:moreinfo = substitute(l:theline[7], "+", " [+]", "") + endif + let s:blen += 1 + let l:fname = matchstr(l:theline, '"\zs[^"]*') + let l:bufnum = matchstr(l:theline, '^ *\zs\d*') + + if l:bufnum == bufnr('') + let l:active = '* ' + elseif bufwinnr(str2nr(l:bufnum)) > 0 + let l:active = '= ' + else + let l:active = ' ' + endif + + call add(s:buflist, s:blen . l:active + \.fnamemodify(l:fname,":t") . l:moreinfo + \." <" . l:bufnum . "> " + \.fnamemodify(l:fname,":h")) + endif + endfor + + let l:alignsize = max(map(copy(s:buflist),'stridx(v:val,">")')) + call map(s:buflist, 'substitute(v:val, " <", repeat(" ",l:alignsize-stridx(v:val,">"))." <", "")') + call map(s:buflist, 'strpart(v:val, 0, &columns-3)') +endfunc + +function SBRun() + if !exists("s:cursel") || (s:cursel >= s:blen) || (s:cursel < 0) + let s:cursel = s:blen-1 + endif + + if s:blen < 1 + echoh WarningMsg | echo "No" s:unlisted ? "unlisted" : "listed" "buffer!" | echoh None + call s:init(0) + return + endif + for l:idx in range(s:blen) + if l:idx != s:cursel + echo " " . s:buflist[l:idx] + else + echoh DiffText | echo "> " . s:buflist[l:idx] | echoh None + endif + endfor + + if s:unlisted + echoh WarningMsg + endif + let l:pkey = input(s:unlisted ? "UNLISTED ([+] loaded):" : "LISTED ([+] modified):" , " ") + if s:unlisted + echoh None + endif + if l:pkey =~ "j$" + let s:cursel = (s:cursel+1) % s:blen + elseif l:pkey =~ "k$" + if s:cursel == 0 + let s:cursel = s:blen - 1 + else + let s:cursel -= 1 + endif + elseif s:update_buf(l:pkey) + call s:init(0) + return + endif + call s:setcmdh(s:blen+1) +endfunc + +function s:init(onStart) + if a:onStart + set nolazyredraw + let s:unlisted = 1 - getbufvar("%", "&buflisted") + let s:cursorbg = synIDattr(hlID("Cursor"),"bg") + let s:cursorfg = synIDattr(hlID("Cursor"),"fg") + let s:cmdh = &cmdheight + hi Cursor guibg=NONE guifg=NONE + + let s:klist = ["j", "k", "u", "d", "w", "l", "s", "c"] + for l:key in s:klist + exe "cnoremap ".l:key." ".l:key.":cal SBRun()" + endfor + cmap k + cmap j + + call s:rebuild() + let s:cursel = match(s:buflist, '^\d*\*') + call s:setcmdh(s:blen+1) + else + call s:setcmdh(s:cmdh) + for l:key in s:klist + exe "cunmap ".l:key + endfor + cunmap + cunmap + exe "hi Cursor guibg=" . s:cursorbg . " guifg=".((s:cursorfg == "") ? "NONE" : s:cursorfg) + endif +endfunc + +" return true to indicate termination +function s:update_buf(cmd) + if a:cmd != "" && a:cmd =~ '^ *\d*!\?\a\?$' + let l:bufidx = str2nr(a:cmd) - 1 + if l:bufidx == -1 + let l:bufidx = s:cursel + endif + + let l:action = matchstr(a:cmd, '!\?\a\?$') + if l:action == "" || l:action == "!" + let l:action .= "z" + endif + + if l:bufidx >= 0 && l:bufidx < s:blen && has_key(s:action2cmd, l:action) + try + exe substitute(s:action2cmd[l:action], "#", matchstr(s:buflist[l:bufidx], '<\zs\d\+\ze>'), "g") + if l:action[-1:] != "z" + call s:rebuild() + endif + catch + echoh ErrorMsg | echo "\rVIM" matchstr(v:exception, '^Vim(\a*):\zs.*') | echoh None + if l:action[-1:] != "z" + call inputsave() | call getchar() | call inputrestore() + endif + endtry + endif + endif + return index(s:klist, a:cmd[-1:]) == -1 +endfunc + +function s:setcmdh(height) + if a:height > &lines - winnr('$') * (&winminheight+1) - 1 + call s:init(0) + echo "\r"|echoerr "QBuf E1: No room to display buffer list" + else + exe "set cmdheight=".a:height + endif +endfunc + +function s:switchbuf(bno, mod) + if bufwinnr(a:bno) == -1 + exe "b".a:mod a:bno + else + exe bufwinnr(a:bno) . "winc w" + endif +endfunc + +function s:qbufdcmd(bno, mod) + if s:unlisted + call setbufvar(a:bno, "&buflisted", 1) + else + exe "bd" . a:mod a:bno + endif +endfunc + +function s:closewindow(bno, mod) + if bufwinnr(a:bno) != -1 + exe bufwinnr(a:bno) . "winc w|close" . a:mod + endif +endfunc -- cgit v1.2.3-24-g4f1b