bgneal@7: " ============================================================================= bgneal@7: " File: autoload/ctrlp/bookmarkdir.vim bgneal@7: " Description: Bookmarked directories extension bgneal@7: " Author: Kien Nguyen bgneal@7: " ============================================================================= bgneal@7: bgneal@7: " Init {{{1 bgneal@7: if exists('g:loaded_ctrlp_bookmarkdir') && g:loaded_ctrlp_bookmarkdir bgneal@7: fini bgneal@7: en bgneal@7: let g:loaded_ctrlp_bookmarkdir = 1 bgneal@7: bgneal@7: cal add(g:ctrlp_ext_vars, { bgneal@7: \ 'init': 'ctrlp#bookmarkdir#init()', bgneal@7: \ 'accept': 'ctrlp#bookmarkdir#accept', bgneal@7: \ 'lname': 'bookmarked dirs', bgneal@7: \ 'sname': 'bkd', bgneal@7: \ 'type': 'tabs', bgneal@7: \ 'opmul': 1, bgneal@7: \ 'nolim': 1, bgneal@7: \ 'wipe': 'ctrlp#bookmarkdir#remove', bgneal@7: \ }) bgneal@7: bgneal@7: let s:id = g:ctrlp_builtins + len(g:ctrlp_ext_vars) bgneal@7: " Utilities {{{1 bgneal@7: fu! s:getinput(str, ...) bgneal@7: echoh Identifier bgneal@7: cal inputsave() bgneal@7: let input = call('input', a:0 ? [a:str] + a:000 : [a:str]) bgneal@7: cal inputrestore() bgneal@7: echoh None bgneal@7: retu input bgneal@7: endf bgneal@7: bgneal@7: fu! s:cachefile() bgneal@7: if !exists('s:cadir') || !exists('s:cafile') bgneal@7: let s:cadir = ctrlp#utils#cachedir().ctrlp#utils#lash().'bkd' bgneal@7: let s:cafile = s:cadir.ctrlp#utils#lash().'cache.txt' bgneal@7: en bgneal@7: retu s:cafile bgneal@7: endf bgneal@7: bgneal@7: fu! s:writecache(lines) bgneal@7: cal ctrlp#utils#writecache(a:lines, s:cadir, s:cafile) bgneal@7: endf bgneal@7: bgneal@7: fu! s:getbookmarks() bgneal@7: retu ctrlp#utils#readfile(s:cachefile()) bgneal@7: endf bgneal@7: bgneal@7: fu! s:savebookmark(name, cwd) bgneal@7: let entries = filter(s:getbookmarks(), 's:parts(v:val)[1] != a:cwd') bgneal@7: cal s:writecache(insert(entries, a:name.' '.a:cwd)) bgneal@7: endf bgneal@7: bgneal@7: fu! s:setentries() bgneal@7: let time = getftime(s:cachefile()) bgneal@7: if !( exists('s:bookmarks') && time == s:bookmarks[0] ) bgneal@7: let s:bookmarks = [time, s:getbookmarks()] bgneal@7: en bgneal@7: endf bgneal@7: bgneal@7: fu! s:parts(str) bgneal@7: let mlist = matchlist(a:str, '\v([^\t]+)\t(.*)$') bgneal@7: retu mlist != [] ? mlist[1:2] : ['', ''] bgneal@7: endf bgneal@7: bgneal@7: fu! s:process(entries, type) bgneal@7: retu map(a:entries, 's:modify(v:val, a:type)') bgneal@7: endf bgneal@7: bgneal@7: fu! s:modify(entry, type) bgneal@7: let [name, dir] = s:parts(a:entry) bgneal@7: let dir = fnamemodify(dir, a:type) bgneal@7: retu name.' '.( dir == '' ? '.' : dir ) bgneal@7: endf bgneal@7: bgneal@7: fu! s:msg(name, cwd) bgneal@7: redr bgneal@7: echoh Identifier | echon 'Bookmarked ' | echoh Constant bgneal@7: echon a:name.' ' | echoh Directory | echon a:cwd bgneal@7: echoh None bgneal@7: endf bgneal@7: bgneal@7: fu! s:syntax() bgneal@7: if !ctrlp#nosy() bgneal@7: cal ctrlp#hicheck('CtrlPBookmark', 'Identifier') bgneal@7: cal ctrlp#hicheck('CtrlPTabExtra', 'Comment') bgneal@7: sy match CtrlPBookmark '^> [^\t]\+' contains=CtrlPLinePre bgneal@7: sy match CtrlPTabExtra '\zs\t.*\ze$' bgneal@7: en bgneal@7: endf bgneal@7: " Public {{{1 bgneal@7: fu! ctrlp#bookmarkdir#init() bgneal@7: cal s:setentries() bgneal@7: cal s:syntax() bgneal@7: retu s:process(copy(s:bookmarks[1]), ':.') bgneal@7: endf bgneal@7: bgneal@7: fu! ctrlp#bookmarkdir#accept(mode, str) bgneal@7: let parts = s:parts(s:modify(a:str, ':p')) bgneal@7: cal call('s:savebookmark', parts) bgneal@7: if a:mode =~ 't\|v\|h' bgneal@7: cal ctrlp#exit() bgneal@7: en bgneal@7: cal ctrlp#setdir(parts[1], a:mode =~ 't\|h' ? 'chd!' : 'lc!') bgneal@7: if a:mode == 'e' bgneal@7: cal ctrlp#switchtype(0) bgneal@7: cal ctrlp#recordhist() bgneal@7: cal ctrlp#prtclear() bgneal@7: en bgneal@7: endf bgneal@7: bgneal@7: fu! ctrlp#bookmarkdir#add(dir) bgneal@7: let str = 'Directory to bookmark: ' bgneal@7: let cwd = a:dir != '' ? a:dir : s:getinput(str, getcwd(), 'dir') bgneal@7: if cwd == '' | retu | en bgneal@7: let cwd = fnamemodify(cwd, ':p') bgneal@7: let name = s:getinput('Bookmark as: ', cwd) bgneal@7: if name == '' | retu | en bgneal@7: let name = tr(name, ' ', ' ') bgneal@7: cal s:savebookmark(name, cwd) bgneal@7: cal s:msg(name, cwd) bgneal@7: endf bgneal@7: bgneal@7: fu! ctrlp#bookmarkdir#remove(entries) bgneal@7: cal s:process(a:entries, ':p') bgneal@7: cal s:writecache(a:entries == [] ? [] : bgneal@7: \ filter(s:getbookmarks(), 'index(a:entries, v:val) < 0')) bgneal@7: cal s:setentries() bgneal@7: retu s:process(copy(s:bookmarks[1]), ':.') bgneal@7: endf bgneal@7: bgneal@7: fu! ctrlp#bookmarkdir#id() bgneal@7: retu s:id bgneal@7: endf bgneal@7: "}}} bgneal@7: bgneal@7: " vim:fen:fdm=marker:fmr={{{,}}}:fdl=0:fdc=1:ts=2:sw=2:sts=2