Switch Files

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Switch Files

Gilad Benjamini-4

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.

Any help is appreciated.
 


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

Reply | Threaded
Open this post in threaded view
|

Re: Switch Files

Gary Johnson-4

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 | Threaded
Open this post in threaded view
|

Re: Switch Files

Anton-3

I was quite happy with a.vim plugin - "Alternate Files quickly (.c --> .h etc)"

http://www.vim.org/scripts/script.php?script_id=31

--
Anton

2009/2/26, Gary Johnson <[hidden email]>:

>
> 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
-~----------~----~----~----~------~----~------~--~---