sponsor Vim development Vim logo Vim Book Ad

advanced Tip #1276: Use taglist and tags with VIM 7.0

 tip karma   Rating 15/5, Viewed by 2491 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   July 4, 2006 5:28      complexity:   advanced
author:   shvarts@akmosoft.com      as of Vim:  

VIM 7.0 is known have been extended with rather advanced scripting capabilities such as complex data structure and object-oriented programming in embryo. So, now we can define methods for some object type in such a way:

let MyObjectType = {}
function! MyObjectType.method1()
        echo 'Hello world from method1!'
endf
function! MyObjectType.method2()
        echo 'Hello world from method2!'
endf

However, if one try to see taglist for such a code he or she will see:

|                            
|-   function                
||         MyObjectType
||         MyObjectType

Such a behaviour most likely is far from one's expectations. Though, there is no any riddle here, just last version of exuberant ctags is older than VIM 7.0, so it cannot parse new extensions to VIM script.

I've found a way to extend existing version of ctags to make it more suitable for using with new VIM scripts.
With this extension taglist for code above will look so:

|-   class
||         MyObjectType
|                            
|-   function                
||         MyObjectType.method1
||         MyObjectType.method2

Here (http://community.livejournal.com/screenshot_vim/4893.html) once more example.

Note that you will not be able to jump to method definitions using <C-]> neither with original ctags nor with patched. Though, with patched version you will be able to jump to method definition through taglist.

To obtain such a behaviour you need to patch latest version of exuberant ctags, namely file vim.c

1)Download latest version of ctags (5.6)  here: http://sourceforge.net/project/showfiles.php?group_id=6556
2) Unpack it and go to directory created: tar zxvf ctags-5.6.tar.gz; cd ctags-5.6
3)Patch file vim.c with this patch:
patch -l vim.c <<'__END'
32c32,34
<       K_VARIABLE
---
>       K_VARIABLE,
>       K_CLASS
>      
38a41
>       { TRUE,  'c', "class", "class definitions" },
105c108
<                                       } while (isalnum ((int) *cp)  ||  *cp == '_');
---
>                                       } while (isalnum ((int) *cp)  ||  *cp == '_' || *cp == '.' || *cp == ''' || *cp == '"' || *cp == '[' || *cp == ']');
157c160
<                               } while (isalnum ((int) *cp)  ||  *cp == '_');
---
>                               } while (isalnum ((int) *cp)  ||  *cp == '_'|| *cp == '.' || *cp == ''' || *cp == '"' || *cp == '[' || *cp == ']');
159c162,176
<                               makeSimpleTag (name, VimKinds, K_VARIABLE);
---
>                               while(isspace((int) *cp))
>                                       ++cp;
>                               if(*cp == '=')
>                                       {
>                                       ++cp;
>                                       while(isspace((int) *cp))
>                                               ++cp;
>                                       if(*cp == '{' || strncmp ((const char*) cp, "extend", (size_t) 6) == 0 ||
>                                               strncmp ((const char*) cp, "copy", (size_t) 4) == 0 ||
>                                               strncmp ((const char*) cp, "deepcopy", (size_t) 8) == 0 ||
>                                               strncmp ((const char*) cp, "filter", (size_t) 6) == 0 ||
>                                               strncmp ((const char*) cp, "map", (size_t) 3) == 0)
>                                               makeSimpleTag(name, VimKinds, K_CLASS);
>                                       else makeSimpleTag (name, VimKinds, K_VARIABLE);
>                                       }
__END

4) Buid ctags and install:
./configure
make; make install

 rate this tip  Life Changing Helpful Unfulfilling 

<< Toggle between Insert and Normal Mode with the "ESC" key | Highlight current line in Insert Mode using "cursorline" >>

Additional Notes

shvarts@akmosoft.com, July 4, 2006 5:34
Code was broken after publishing(somewhere slashes disappeared), one more attempt:

patch -l vim.c <<'__END'
32c32,34
<       K_VARIABLE
---
>       K_VARIABLE,
>       K_CLASS
>      
38a41
>       { TRUE,  'c', "class", "class definitions" },
105c108
<                                       } while (isalnum ((int) *cp)  ||  *cp == '_');
---
>                                       } while (isalnum ((int) *cp)  ||  *cp == '_' || *cp == '.' || *cp == '\'' || *cp == '"' || *cp == '[' || *cp == ']');
157c160
<                               } while (isalnum ((int) *cp)  ||  *cp == '_');
---
>                               } while (isalnum ((int) *cp)  ||  *cp == '_'|| *cp == '.' || *cp == '\'' || *cp == '"' || *cp == '[' || *cp == ']');
159c162,176
<                               makeSimpleTag (name, VimKinds, K_VARIABLE);
---
>                               while(isspace((int) *cp))
>                                       ++cp;
>                               if(*cp == '=')
>                                       {
>                                       ++cp;
>                                       while(isspace((int) *cp))
>                                               ++cp;
>                                       if(*cp == '{' || strncmp ((const char*) cp, "extend", (size_t) 6) == 0 ||
>                                               strncmp ((const char*) cp, "copy", (size_t) 4) == 0 ||
>                                               strncmp ((const char*) cp, "deepcopy", (size_t) 8) == 0 ||
>                                               strncmp ((const char*) cp, "filter", (size_t) 6) == 0 ||
>                                               strncmp ((const char*) cp, "map", (size_t) 3) == 0)
>                                               makeSimpleTag(name, VimKinds, K_CLASS);
>                                       else makeSimpleTag (name, VimKinds, K_VARIABLE);
>                                       }
__END
shvarts@akmosoft.com, July 4, 2006 6:00
If this patch will not work because HTML distortion, you may download file from here: http://phenix.rootshell.be/~brndshmg/vim.c.diff

patch -l vim.c vim.c.diff
Jaggit., July 9, 2006 10:13
Mail the patch to vim-dev mailing list (see vim.org). Average user here cannot build vim.
krischik@users.sourceforge.net, July 20, 2006 3:04
Actualy, pathes to ctags should be posted here:

http://sourceforge.net/tracker/?group_id=6556&atid=306556

Martin
krischik@users.sourceforge.net, August 23, 2006 2:36
Of corse it will only work when you define the function

function MyObjectType.method1 ()

Which is the simplest / static way only suitable for meta objects - as you can only create one instance of for each class.

Real objects live from beeing dynamic in which case you need to create your object like this:

function class#Method1 () dict
...

function class#New ()
   return = {
       \ 'Method1' : function ('class#Method1'),
...

Martin
krischik@users.sourceforge.net, August 23, 2006 3:36
I took the liberty extened to patch to detect autoload functions (which start lowercase and have a # inside the name) and submited the patch upstream:

http://sourceforge.net/tracker/index.php?func=detail&aid=1545197&group_id=6556&atid=306556

Martin
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
Sponsored by Web Concept Group Inc. SourceForge.net Logo