* On Thu, May 19, 2011 at 03:20:44PM +0200, Bram Moolenaar <[email protected]> 
wrote:

Raghavendra D Prabhu wrote:

>> Currently when using cscope with vim and being several levels deep from
>> project root directory (with cscope.out located in this directory), the
>> cscope queries fail because of lack of prefix. Using a cscope prefix on
>> a per-project basis is hard. So, this patch introduces a boolean
>> variable setting which vim will be able use (in the absence of an
>> already existing prefix path) the prefix of cscope.out to canonicalize
>> the path obtained from 'cs find'. I have also tested it and it works
>> fine.

>Isn't this similar to what 'tagrelative' does to file names in tags
>files?  Then perhaps we should use a similar name: 'cscoperelative'?

Yeah, 'cscoperelative' name makes more sense and it will be easier for
people to find it too. I have attached patch with the name change.

A few remarks about the patch.  Please fix this and send a new patch.

The strdup() calls are leaking memory.  You can't use dirname(), it's
not on all systems.

I don't think it works when the directory name is empty, it would result
in a leading slash.

You can use gettail().  You probably want to compute the size of the
directory name and concate the name when it's not zero.  Search for
"p_tr" in src/tag.c for an example.  The use of vim_isAbsName() might
also apply.

Please use coding style as in the rest of the Vim sources.

The documentation in if_cscop.txt isn't very clear.  Please mention what
happens to a path that cscope returns.  I don't know what "prefix path"
means.  Either explain that or use some other wording.
Hi,
    Thanks for all the comments. I have made appropriate changes. I have
    attached the patch. I have also improved the documentation.
    Suggestions welcome.

Regards,
--------------------------
Raghavendra Prabhu
GPG Id : 0xD72BE977
Fingerprint: B93F EBCB 8E05 7039 CD3C A4B8 A616 DCA1 D72B E977
www: wnohang.net
From be60e1dfbbb87f723f16a770437b0d7ea33e95b8 Mon Sep 17 00:00:00 2001
Message-Id: 
<be60e1dfbbb87f723f16a770437b0d7ea33e95b8.1305835872.git.rpra...@wnohang.net>
From: Raghavendra D Prabhu <[email protected]>
Date: Fri, 20 May 2011 01:38:27 +0530
Subject: [PATCH] [FEATURE] Add cscoperelative

Currently, the only way to obtain canonical path for cscope is to use
ppath (-P to cscopeprg). However, using it on per project basis can be 
annoying. So this
introduces a boolean flag - csre - setting which makes vim use the
basename of path of cscope.out file (usually located in the root of
project directory) as the prefix in the absence of an already existing
ppath. This simplifies project handling greatly.

Signed-off-by: Raghavendra D Prabhu <[email protected]>
---
 runtime/doc/if_cscop.txt |    9 +++++++++
 runtime/doc/options.txt  |    9 +++++++++
 runtime/doc/quickref.txt |    1 +
 runtime/doc/tags         |    1 +
 src/if_cscope.c          |   24 +++++++++++++++++++++++-
 src/if_cscope.h          |    1 -
 src/option.c             |    7 +++++++
 src/option.h             |    1 +
 8 files changed, 51 insertions(+), 2 deletions(-)

diff --git a/runtime/doc/if_cscop.txt b/runtime/doc/if_cscop.txt
index 91dd4c6..52cfc5c 100644
--- a/runtime/doc/if_cscop.txt
+++ b/runtime/doc/if_cscop.txt
@@ -271,6 +271,15 @@ tag files.  The default is off.  Examples: >
        :set cst
        :set nocst
 <
+                                                       *cscoperelative* *csre*
+If 'cscoperelative' set, then in absence of a prefix given to cscope (prefix
+is the argument to -P option of cscope), basename of cscope.out location
+(usually the project root directory) will be used as the prefix to construct
+absolute path.The default is off. Note: This option is only effective when
+cscope (cscopeprg) is initialized without a prefix path (-P). Examples: >
+       :set csre
+       :set nocsre
+<
                                                        *cscopetagorder* *csto*
 The value of 'csto' determines the order in which |:cstag| performs a search.
 If 'csto' is set to zero, cscope database(s) are searched first, followed
diff --git a/runtime/doc/options.txt b/runtime/doc/options.txt
index a5f6148..68126e5 100644
--- a/runtime/doc/options.txt
+++ b/runtime/doc/options.txt
@@ -2190,6 +2190,15 @@ A jump table for the options with a short description 
can be found at |Q_op|.
        Determines how many components of the path to show in a list of tags.
        See |cscopepathcomp|.
 
+                                               *'cscoperelative'* *'csre'*
+'cscoperelative' 'csre' boolean (default off)
+                        global
+                       {not available when compiled without the |+cscope|
+                       feature}
+                       {not in Vi}
+       In the absence of a prefix (-P) for cscope. setting this option enables
+       to use the basename of cscope.out path as the prefix.
+
                                                *'cscopeprg'* *'csprg'*
 'cscopeprg' 'csprg'    string  (default "cscope")
                        global
diff --git a/runtime/doc/quickref.txt b/runtime/doc/quickref.txt
index 5bf410e..59a756b 100644
--- a/runtime/doc/quickref.txt
+++ b/runtime/doc/quickref.txt
@@ -655,6 +655,7 @@ Short explanation of each option:           *option-list*
 'cpoptions'      'cpo'     flags for Vi-compatible behavior
 'cryptmethod'    'cm'      type of encryption to use for file writing
 'cscopepathcomp'  'cspc'    how many components of the path to show
+'cscoperelative'  'csre'    Use cscope.out path basename as prefix
 'cscopeprg'       'csprg'   command to execute cscope
 'cscopequickfix'  'csqf'    use quickfix window for cscope results
 'cscopetag'       'cst'     use cscope for tag commands
diff --git a/runtime/doc/tags b/runtime/doc/tags
index 50a1d4d..83a76bd 100644
--- a/runtime/doc/tags
+++ b/runtime/doc/tags
@@ -152,6 +152,7 @@ $VIMRUNTIME starting.txt    /*$VIMRUNTIME*
 'crb'  options.txt     /*'crb'*
 'cryptmethod'  options.txt     /*'cryptmethod'*
 'cscopepathcomp'       options.txt     /*'cscopepathcomp'*
+'cscoperelative'       options.txt     /*'cscoperelative'*
 'cscopeprg'    options.txt     /*'cscopeprg'*
 'cscopequickfix'       options.txt     /*'cscopequickfix'*
 'cscopetag'    options.txt     /*'cscopetag'*
diff --git a/src/if_cscope.c b/src/if_cscope.c
index a966226..5c6e00f 100644
--- a/src/if_cscope.c
+++ b/src/if_cscope.c
@@ -2475,7 +2475,9 @@ cs_resolve_file(i, name)
     char *name;
 {
     char *fullname;
+    char *prefix;
     int len;
+    char_u *csdir = NULL;
 
     /*
      * ppath is freed when we destroy the cscope connection.
@@ -2485,6 +2487,17 @@ cs_resolve_file(i, name)
     len = (int)(strlen(name) + 2);
     if (csinfo[i].ppath != NULL)
        len += (int)strlen(csinfo[i].ppath);
+    /*
+     *  If cscoperelative is set, use cscope.out path in path resolution.
+     * cscoperelative is checked only when ppath is not set.
+     */
+    else if (p_csre && csinfo[i].fname != NULL)
+    {
+       csdir = alloc(MAXPATHL);
+       vim_strncpy(csdir, (char_u *)csinfo[i].fname, 
+               gettail((char_u *)csinfo[i].fname) - 1 - (char_u 
*)csinfo[i].fname);
+       len += (int)strlen(csdir);
+    }
 
     if ((fullname = (char *)alloc(len)) == NULL)
        return NULL;
@@ -2503,8 +2516,17 @@ cs_resolve_file(i, name)
        )
        (void)sprintf(fullname, "%s/%s", csinfo[i].ppath, name);
     else
-       (void)sprintf(fullname, "%s", name);
+       /*
+       * Check for csdir to be non empty to
+       * avoid empty path concatenated to cscope output
+       */
+       if (p_csre && csinfo[i].fname != NULL && (strlen(csdir) > 0)) {
+           (void)sprintf(fullname, "%s", concat_fnames(csdir, name, 1));
+       }
+       else
+           (void)sprintf(fullname, "%s", name);
 
+    vim_free(csdir);
     return fullname;
 } /* cs_resolve_file */
 
diff --git a/src/if_cscope.h b/src/if_cscope.h
index 5620eb3..b2f7324 100644
--- a/src/if_cscope.h
+++ b/src/if_cscope.h
@@ -10,7 +10,6 @@
  */
 
 #if defined(FEAT_CSCOPE) || defined(PROTO)
-
 #if defined(UNIX)
 # include <sys/types.h>                /* pid_t */
 # include <sys/stat.h>         /* dev_t, ino_t */
diff --git a/src/option.c b/src/option.c
index 7184649..bd8ce3f 100644
--- a/src/option.c
+++ b/src/option.c
@@ -892,6 +892,13 @@ static struct vimoption
                            {(char_u *)0L, (char_u *)0L}
 #endif
                            SCRIPTID_INIT},
+    {"cscoperelative", "csre", P_BOOL|P_VI_DEF|P_VIM,
+#ifdef FEAT_CSCOPE
+                           (char_u *)&p_csre, PV_NONE,
+#else
+                           (char_u *)NULL, PV_NONE,
+#endif
+                           {(char_u *)0L, (char_u *)0L} SCRIPTID_INIT},
     {"cscopequickfix", "csqf", P_STRING|P_VI_DEF|P_COMMA|P_NODUP,
 #if defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)
                            (char_u *)&p_csqf, PV_NONE,
diff --git a/src/option.h b/src/option.h
index b5f7d47..c7df4a6 100644
--- a/src/option.h
+++ b/src/option.h
@@ -391,6 +391,7 @@ EXTERN long p_ph;           /* 'pumheight' */
 EXTERN char_u  *p_cpo;         /* 'cpoptions' */
 #ifdef FEAT_CSCOPE
 EXTERN char_u  *p_csprg;       /* 'cscopeprg' */
+EXTERN int       p_csre;        /*  'cscoperelative' */
 # ifdef FEAT_QUICKFIX
 EXTERN char_u  *p_csqf;        /* 'cscopequickfix' */
 #  define      CSQF_CMDS   "sgdctefi"
-- 
1.7.5.1

Attachment: pgpt1dvZJUOzL.pgp
Description: PGP signature

Raspunde prin e-mail lui