On 2007-05-22, Natesh Kedlaya <[EMAIL PROTECTED]> wrote:
> Hi Gary,
>   I apologize for my earlier email. I should have given the full details.
>   I would like to provide it now.
> 
>   I have tried your suggestion of setting the cscopeprg to a script that 
> contained
>   /usr/local/bin/cscope "$@" | sed s@/dir1/@/dir2/@
>   But the cscope results still contained the directory /dir1 reference.
> 
>   While doing this test, I have observed a strange behavior. If the cscopeprg
>   is set to anything other than /usr/bin/cscope and /usr/local/bin/cscope, 
> somewhow
>   the vim resets them back to /usr/bin/cscope.
>   So, I tried replacing the /usr/bin/cscope itself with the script mentioned 
> above and
>   got the following errors while opening any source files.
> 
> Error detected while processing /etc/vimrc:
> line   35:
> cs_read_prompt EOF: Interrupted system call
> Interrupted
> Error detected while processing /home/myname/.vimrc:
> line    1:
> Interrupted
> Interrupt: Hit ENTER or type command to continue

That is indeed strange behavior.  Let's take one behavior at a time.

If setting 'cscopeprg' to the name of your script left the /dir1 
references unchanged, then either that setting is being overwritten 
or the sed command is failing.  You can check the former by 
executing

   :verbose set cscopeprg?

which will return the current value of 'cscopeprg' and where it was 
set last.  You can check the sed script by changing it to

   cat "$@" | sed s@/dir1/@/dir2/@

and executing it from a shell prompt with the name of your cscope 
database (cscope.out?) as the only argument.  This will ensure that 
the sed command is being executed in the same environment as when it 
filters the output of cscope, helps catch quoting issues and avoids 
copy-and-paste issues.

>From what you said about the errors and the renaming of 'cscopeprg',
though, it looks more like something changing 'cscopeprg' than a 
problem with the sed command.

I don't have an explanation for the errors from /etc/vimrc or 
/home/myname/.vimrc yet.  I don't know how the contents of 
/usr/bin/cscope would affect the behavior of vim so early in vim's 
startup.  Unless, of course, either of those files executed a 
:cscope command, which would also be unusual.  You might take a look 
at the lines where those errors are reported for a clue to what's 
happening.

>   Out of the three solutions that you have suggested, would you please
>   elaborate on the third one, ie. "execute cscope as a quickfix command 
> (instead of using the :cs 
>     commands) and filter its output before being read by vim."  This looks 
> very promising to me.

When the cscope feature was first added to vim, its results could 
not be sent to the quickfix error list as they can now.  I really 
wanted to use the quickfix interface with cscope, so I wrote a 
function to behave just like ":cs find" but to send the cscope 
output to the quickfix list by setting 'grepprg' to "cscope" plus 
some arguments.  I've attached the function (actually a pair of 
functions) to this message.  You can invoke it from the command line 
as, e.g.,

   :Csfind g somevar

or bind it to a mapping, e.g.,

   map <silent> ,g :Csfind g <C-R><C-W><CR>zv

to search for the definition of the symbol under the cursor.

Regards,
Gary

-- 
Gary Johnson                 | Agilent Technologies
[EMAIL PROTECTED]     | Mobile Broadband Division
                             | Spokane, Washington, USA
if v:version < 602

    " Csfind
    "
    " Usage:
    " 
    "     :Csfind {querytype} {name}
    " 
    " Example:
    " 
    "     :Csfind s myvar
    " 
    " finds all occurrences of the C symbol "myvar".

    command! -nargs=+ Csfind call Csfind(<f-args>)

    function! Csfind(querytype, name)

        " Check the validity of the querytype argument.
        "
        if strlen(a:querytype) != 1 || stridx("01234678sgdctefi", a:querytype) 
== -1
            echohl ErrorMsg
            echo "Usage: :Csfind {querytype} {name}"
            echo "See \":help cs\""
            echohl None
            return
        endif

        " Save previous 'grep' options.
        "
        let gf = &grepformat
        let gp = &grepprg

        " Set new 'grep' options for running cscope.
        "
        set grepformat=%f\ %[%^\ ]%#\ %l\ %m
        let &grepprg = &csprg . " -L -f " . s:cscope_database

        " Find the numerical form of the querytype.
        "
        if a:querytype =~ '\d'
            let num = a:querytype
        else
            let num = stridx("sgdct?efi", a:querytype)
        endif

        " Do it.
        "
        execute "grep -" . num . a:name

        " Restore previous 'grep' options.
        "
        let &grepprg = gp
        let &grepformat = gf

    endfunction

else

    " Csfind()
    "
    " This function uses :try and related commands which were first introduced
    " at vim-6.2, so any application should check for v:version >= 602 before
    " calling it.
    "
    " When errors occur within a function, vim prints the function name and
    " line number as well as the error message from the command that failed.
    " The user doesn't care about this, so this function catches _all_ errors
    " from :cscope so that the user will see only the error messages from
    " :cscope itself.

    command! -nargs=+ Csfind call Csfind("cscope", <f-args>)
    command! -nargs=+ SCsfind call Csfind("scscope", <f-args>)

    function! Csfind(cmd, querytype, name)
        try
            execute a:cmd . " find " . a:querytype . " " . a:name
        catch /E567/                    " 'no cscope connections'
            try
                execute "cscope add " . s:cscope_database
            catch /.*/
                " Note:  The perror() message
                "    cs_read_prompt EOF: Error 0
                " currently (2004-02-20) hides the more-informative message
                "    E609: Cscope error: cscope: cannot read file version from 
file cscope.out
                "
                call s:EchoException()
                return
            endtry
            try
                execute a:cmd . " find " . a:querytype . " " . a:name
            catch /.*/                  " Any error.
                call s:EchoException()
            endtry
        catch /.*/                      " Any other error.
            call s:EchoException()
        endtry
    endfunction

    function! s:EchoException()
        if &errorbells
            normal \<Esc>               " Generate a bell
        endif
        echohl ErrorMsg
        echo matchstr(v:exception, ':\zs.*')
        echohl None
    endfunction

endif

Reply via email to