On 2009-02-26, Gilad Benjamini wrote:
> Hi,
> A while ago I found a function that easily switches between source and
> header files.
> 
> function! SwitchSourceHeader()
>     "update!
>     let base=expand("%:r")
>     let extension=expand("%:t:e")
>     if ( extension == "cpp" )
>       let extension="h"
>       let alt=base . "." . extension
>     else
>       let alt=base . ".cpp"
>     endif
>     find `=alt`
> endfunction
> 
> I have been using it for a while, and it's doing what it is supposed to.
> Now I have a new requirement. The source file is now either a ".c" file or a
> ".cpp" file (never a situation where both files exist).
> I tried changing the function to match that but wasn't able to find a
> working solution.

Here's what I've done in my c.vim ftplugin.

----------------------------------------------------------------

if !exists("no_plugin_maps") && !exists("no_c_maps")
    " Edit the "other" file.
    "
    map <buffer> <silent> ,o :call FindOther("edit")<CR>
    map <buffer> <silent> <C-W>,o :call FindOther("split")<CR>
endif

if !exists("*FindOther")
    function FindOther(cmd)
        let ext = expand("%:e")
        let root = expand("%:t:r")
        let file = ""
        if ext == "c" || ext == "C" || ext == "cpp"
            let target = root.".h"
            let file = findfile(target)
        elseif ext == "h"
            let target = root.".c"
            let file = findfile(target)
            if file == ""
                let target = root.".C"
                let file = findfile(target)
                if file == ""
                    let target = root.".cpp"
                    let file = findfile(target)
                endif
            endif
        endif
        if file != ""
            "let file = substitute(file, "\n.*", "", "")
            exe a:cmd file
        else
            echohl ErrorMsg
            " Echo same error message as from :find.
            echo "E345: Can't find file \"".target."\" in path"
            echohl None
        endif
    endfunction
endif

----------------------------------------------------------------

Regards,
Gary



--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply via email to