Autoloading Cscope

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

Autoloading Cscope

Sergey Doroshenko
By default, cscope script (the one at http://cscope.sourceforge.net/cscope_maps.vim) adds cscope.out from vim's current directory and from $CSCOPE_DB.
But if I'm starting Vim from say ~/proj/src/a/b/c/, while cscope.out is at ~/proj/src/, that cscope.out won't be loaded automatically.

For ctags, there's a nice trick: you do "set tags=tags;/" and Vim will look for tags file everywhere starting from the current dir up to the root.
I haven't found the same thing for cscope, so I came up with my own function:

----------------
function! LoadCscope()
    " get current buffer's path
    let path = getcwd()
    if (winbufnr(0) != -1)
        let path = fnamemodify(bufname(winbufnr(0)), ":p:h")
    endif
    " walk up the directory structure
    while (strlen(path) > 0)
        let db = path . "/cscope.out"
        if (filereadable(db))
            " echo "w00t " . db
            set nocsverb " suppress 'duplicate connection' error
            exe "cs add " . db . " " . path
            set csverb   " switch bach to verbose mode
            break
        endif
        let slash = match(path, "/[^/]*$")
        if (slash == -1)
            break
        endif
        let path = strpart(path, 0, slash)
    endwhile
endfunction

au BufEnter /* call LoadCscope()
----------------

What this basically does is it gets currently active buffer's path and walks up to the root trying to find cscope.out.

So my question is, haven't I reinvented the wheel -- maybe I missed some obvious cscope setting that would do the same? Google et al. doesn't seem to have a good answer on this.
If there's no such thing, I could add my function to Vim Tips Wiki.


P.S. Is it conceptually correct to use "au BufEnter /* <do something>" in order to <do something> upon entering any buffer, i.e. for any filetype?

--
Best regards,
Sergey Doroshenko: http://dorserg.com

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Autoloading Cscope

Sergey Doroshenko
By default, cscope script (the one at http://cscope.sourceforge.net/cscope_maps.vim) adds cscope.out from vim's current directory and from $CSCOPE_DB.
But if I'm starting Vim from say ~/proj/src/a/b/c/, while cscope.out is at ~/proj/src/, that cscope.out won't be loaded automatically.

For ctags, there's a nice trick: you do "set tags=tags;/" and Vim will look for tags file everywhere starting from the current dir up to the root.
I haven't found the same thing for cscope, so I came up with my own function:

----------------
function! LoadCscope()
    " get current buffer's path
    let path = getcwd()
    if (winbufnr(0) != -1)
        let path = fnamemodify(bufname(winbufnr(0)), ":p:h")
    endif
    " walk up the directory structure
    while (strlen(path) > 0)
        let db = path . "/cscope.out"
        if (filereadable(db))
            " echo "w00t " . db
            set nocsverb " suppress 'duplicate connection' error
            exe "cs add " . db . " " . path
            set csverb   " switch bach to verbose mode
            break
        endif
        let slash = match(path, "/[^/]*$")
        if (slash == -1)
            break
        endif
        let path = strpart(path, 0, slash)
    endwhile
endfunction

au BufEnter /* call LoadCscope()
----------------

What this basically does is it gets currently active buffer's path and walks up to the root trying to find cscope.out.

So my question is, haven't I reinvented the wheel -- maybe I missed some obvious cscope setting that would do the same? Google et al. doesn't seem to have a good answer on this.
If there's no such thing, I could add my function to Vim Tips Wiki.


P.S. Is it conceptually correct to use "au BufEnter /* <do something>" in order to <do something> upon entering any buffer, i.e. for any filetype?


--
Best regards,
Sergey Doroshenko: http://dorserg.com

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: Autoloading Cscope

Jan Larres
Hi Sergey,

Sergey Doroshenko <[hidden email]>:

> By default, cscope script (the one at
> http://cscope.sourceforge.net/cscope_maps.vim) adds cscope.out from vim's
> current directory and from $CSCOPE_DB.
> But if I'm starting Vim from say ~/proj/src/a/b/c/, while cscope.out is at
> ~/proj/src/, that cscope.out won't be loaded automatically.
>
> For ctags, there's a nice trick: you do "set tags=tags;/" and Vim will look
> for tags file everywhere starting from the current dir up to the root.
> I haven't found the same thing for cscope, so I came up with my own
> function:
>
> So my question is, haven't I reinvented the wheel -- maybe I missed some
> obvious cscope setting that would do the same? Google et al. doesn't seem to
> have a good answer on this.
> If there's no such thing, I could add my function to Vim Tips Wiki.

you can do the same thing with the findfile() function, see

  :h findfile()

Cheers,
        Jan

--
-[ OpenPGP key ID: 00A0FD5F ]-
Never offend people with style when you can offend them with substance.
                -- Sam Brown, "The Washington Post", January 26, 1977

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: Autoloading Cscope

Sergey Doroshenko
Hi Jan, thanks for pointing this out.

So now (assuming there's no ctags-like one-liner trick for cscope) it comes down to the following:

------------------
function! LoadCscope()
    let db = findfile("cscope.out", ".;")
    if (!empty(db))
        let path = strpart(db, 0, match(db, "/cscope.out$"))
        set nocsverb " suppress 'duplicate connection' error
        exe "cs add " . db . " " . path
        set csverb   " switch back to verbose mode
    endif
endfunction
au BufEnter /* call LoadCscope()
------------------

I created a tip with the same code: http://vim.wikia.com/wiki/Autoloading_Cscope_Database, hope it'll be helpful for someone.

On Fri, Feb 11, 2011 at 2:00 AM, Jan Larres <[hidden email]> wrote:
Hi Sergey,

Sergey Doroshenko <[hidden email]>:
> By default, cscope script (the one at
> http://cscope.sourceforge.net/cscope_maps.vim) adds cscope.out from vim's
> current directory and from $CSCOPE_DB.
> But if I'm starting Vim from say ~/proj/src/a/b/c/, while cscope.out is at
> ~/proj/src/, that cscope.out won't be loaded automatically.
>
> For ctags, there's a nice trick: you do "set tags=tags;/" and Vim will look
> for tags file everywhere starting from the current dir up to the root.
> I haven't found the same thing for cscope, so I came up with my own
> function:
>
> So my question is, haven't I reinvented the wheel -- maybe I missed some
> obvious cscope setting that would do the same? Google et al. doesn't seem to
> have a good answer on this.
> If there's no such thing, I could add my function to Vim Tips Wiki.

you can do the same thing with the findfile() function, see

 :h findfile()

Cheers,
       Jan

--
-[ OpenPGP key ID: 00A0FD5F ]-
Never offend people with style when you can offend them with substance.
               -- Sam Brown, "The Washington Post", January 26, 1977

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php



--
Best regards,
Sergey Doroshenko: http://dorserg.com

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php