Vim Tips Wiki
Advertisement

Project.vim plugin has special functionality allowing to run custom script on file opening. This tip shows how to automate tags generation using this plugin.

Introduction

This tip assumes using of Project.vim plugin and GNU Make build system. GNU Make will search for source files changes and rebuild tags when user opens a file from project window.

Prerequisites

  • Project.vim plugin to set up a list of frequently-accessed files for easy navigation.
  • GNU Make a tool which controls the generation of executables and other non-source files of a program from the program's source files. Windows version of GNU Make.

For comfortable use, add Make.exe to your PATH in order to run Make without typing full path:

D:\example\project>which make
C:\Program Files\GnuWin32\bin\make.EXE

D:\example\project>make -v
GNU Make 3.81
Copyright (C) 2006  Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.

Project layout

Let's assume the following directory structure.

D:\example\project>tree /f
Folder PATH listing
Volume serial number is F418-2E27
D:.
├─common
│      common1.c
│      common2.c
│
├─include
│      project.h
│
├─lib
│      libproject.lib
│
└─source
        src1.c
        src2.c

The correspondent .vimprojects file:

example=d:/example {
  project=project filter="*.c *.h" {
    source/src1.c
    source/src2.c
    include/project.h
    common/common1.c
    common/common2.c
 }
}

Next, let's write Makefile for tags generation:

DIRS := source include common
SRC := $(foreach dir,$(DIRS),$(wildcard $(dir)/*.[ch]))

CTAGS_FLAGS=--c++-kinds=+p --fields=+imaS --extra=+q

tags: $(SRC)
	ctags $(CTAGS_FLAGS) $(SRC)

clean: 
	rm tags

DIRS are all the directories for make to look into, *.[ch] are source .c and include .h files to generate tags from. Now, you can try command make tags to generate tags from your source files:

D:\example\project>make tags
ctags --c++-kinds=+p --fields=+imaS --extra=+q source/src1.c source/src2.c include/project.h common/common1.c common/common2.c

D:\example\project>dir tags
 Volume in drive D has no label.
 Volume Serial Number is F418-2E27

 Directory of D:\example\project

02/24/2009  03:53 PM               416 tags
               1 File(s)            416 bytes

Tags have been generated. Now we can use Project.vim in= or out= option to automate tags generation. Let's add small maketags.vim script to the project folder:

D:\example\project>echo silent execute "make -f Makefile tags" > maketags.vim

This script will call make to generate tags every time you open some source file from the project. To do this, let's modify project.vimprojects file a little bit:

example=d:/example {
  project=project CD=. in=maketags.vim filter="*.c *.h" {
    source/src1.c
    ...
  }
}

Alternatively, you could update tags on file closing:

  ...
  project=project CD=. out=maketags.vim filter="*.c *.h" {
  ...
Advertisement