" TabFind() {{{ " Local function, does the magic of reusing existing tabs. function! TabFind(filename, ...) if !filereadable(a:filename) | echoerr "File '".a:filename."' not found!" | endif let bufnr = bufnr(a:filename) " Is the file in the current tab? Avoid jumping to another tab containg " the same file. if bufwinnr(a:filename) != -1 exec bufwinnr(bufnr)."wincmd w" if a:0 >= 1 | silent! exec a:1 | endif return endif " Open a new tab if no other tabs contain the file if bufnr == -1 exec "tabedit ".escape(a:filename, " ") exec bufwinnr(bufnr)."wincmd w" if a:0 >= 1 | silent! exec a:1 | endif return endif " Select the most recent tab with the file for tab in range(1, tabpagenr("$")) if index(tabpagebuflist(tab), bufnr) == -1 | continue | endif exec "tabnext ".tab exec bufwinnr(bufnr)."wincmd w" if a:0 >= 1 | silent! exec a:1 | endif return endfor " There must be hidden buffers then. We don't care :-) exec "tabedit ".escape(a:filename, " ") exec bufwinnr(bufnr)."wincmd w" if a:0 >= 1 | silent! exec a:1 | endif return endfunction " }}} " TabTag() {{{ " Like , but open a new tab or reuse another tab if it already contains " the file. function! TabTag() let cword = expand("") if cword == "" | return | endif let taglist = taglist(cword) if len(taglist) == 0 echoerr "Tag matching '".cword."' not found!" return endif let filename = "" " Select the first of the available tags. " TODO: Support choosing amongst the tags. for tag in taglist if tag["name"] == cword let filename = tag["filename"] break endif endfor if filename == "" print "Tag '".cword."' not found!" return endif call TabFind(filename, taglist[0]["cmd"]) endfunction " }}} nnoremap :call TabTag() " TabFile() {{{ " Open the file under the cursor in a new tab, or reuse another tab if it " already contains the file. Supports &path (with wildcards), &includeexpr and " &suffixesadd. function! TabFile() let cfile = expand("") " No need to do magic with absolute paths if cfile[0] == "/" call TabFind(cfile) return endif let sufadd = split(&suffixesadd, ",") " be sure to try with no extension first call insert(sufadd, "") " try with the original name first let files = [cfile] " and then with includeexpr applied if &includeexpr != "" execute "let inex_file = ".substitute(&includeexpr, "v:fname", "'".cfile."'", "") call add(files, inex_file) endif for exp_file in files let path = split(&path, ",") " expand wildcards etc let glob_path = [] for dir in path " we can't use globpath() due to the prefixes let glob_path = glob_path + split(glob(dir),"\") endfor " ensure a default path if empty(glob_path) | call insert(glob_path, ".") | endif for dir in glob_path if dir == "" | continue | endif for suf in sufadd let file_suf = join([dir, exp_file], "/") . suf if filereadable(file_suf) call TabFind(file_suf) return endif endfor endfor endfor echo "File matching '".cfile."' not found!" endfunction " }}} nnoremap :call TabFile()