From d247768f2669f56bc40d4aa09311e9dc55773292 Mon Sep 17 00:00:00 2001 From: Matthias Braendli Date: Wed, 28 Oct 2015 11:30:22 +0100 Subject: Update header to code flip script --- dotvimrc | 68 +++++++++++++++++++++++++++++++++------------------------------- 1 file changed, 35 insertions(+), 33 deletions(-) (limited to 'dotvimrc') diff --git a/dotvimrc b/dotvimrc index 3d108bd..86f3694 100644 --- a/dotvimrc +++ b/dotvimrc @@ -173,43 +173,45 @@ autocmd BufNewFile,BufRead *.md set filetype=markdown set nostartofline -"F4 switches from C source .c to header .h file -function! MPB_LoadFile(filename) - let s:bufname = bufname(a:filename) - if (strlen(s:bufname)) > 0 - " File already in a buffer - exe ":buffer" s:bufname - else - " Must open file first - exe ":e " a:filename - endif -endfun - - +"Leader-f switches from C source .c to header .h file +"It automatically detects .cpp <-> .h +" .c <-> .h +" .cpp <-> .hpp function! MPB_Flip_Ext() - " Switch editing between .c(XYZ) and .h(XYZ) files. - if match(expand("%"),'\.c') > 0 - let s:flipname = substitute(expand("%"),'\.c\(.*\)','.h\1',"") - exe ":call MPB_LoadFile(s:flipname)" - elseif match(expand("%"),'\.h') > 0 - let s:flipname = substitute(expand("%"),'\.h\(.*\)','.c\1',"") - exe ":call MPB_LoadFile(s:flipname)" - endif -endfun - -function! MPB_Flip_Cpp_H() - " Switch editing between .c(XYZ) and .h(XYZ) files. - if match(expand("%"),'\.cpp') > 0 - let s:flipname = substitute(expand("%"),'\.cpp','.h',"") - exe ":call MPB_LoadFile(s:flipname)" - elseif match(expand("%"),'\.h') > 0 - let s:flipname = substitute(expand("%"),'\.h','.cpp',"") - exe ":call MPB_LoadFile(s:flipname)" - endif +python << endpython +import vim +import os.path + +def loadfile(filename): + vim.command("e {}".format(filename)) + +currentfile = vim.eval('expand("%")') + +print("Current file is {}".format(currentfile)) + +if currentfile.endswith(".c"): + loadfile(currentfile[:-2] + ".h") +elif currentfile.endswith(".h"): + basename = currentfile[:-2] + if os.path.exists(basename + ".cpp"): + loadfile(basename + ".cpp") + else: + loadfile(basename + ".c") +elif currentfile.endswith(".cpp"): + basename = currentfile[:-4] + if os.path.exists(basename + ".h"): + loadfile(basename + ".h") + else: + loadfile(basename + ".hpp") +elif currentfile.endswith(".hpp"): + basename = currentfile[:-4] + loadfile(basename + ".cpp") + +endpython endfun noremap :call MPB_Flip_Ext() -noremap :call MPB_Flip_Cpp_H() +noremap f :call MPB_Flip_Ext() "Show trailing whitespace highlight ExtraWhitespace ctermbg=darkgreen guibg=#344011 -- cgit v1.2.3