Friday, August 3, 2007

"Perfect"??? ctags DB for a .CPP file

Ok, I figure out a way to create at least a good ctags file to a .cpp/.h file being edited.

I use the C Pre-Processor (cpp) that came with GCC but I believe that should be very easy to adapt this to other pre processor. Well, let me show you some code:

function! CreateTag()
  let inc_list = ["/myproject/include", "/otherproject/include"]

  if !has("unix")
    let redirect_null="> nul"
  else
    let redirect_null="2>&1 1> /dev/null"
  endif
  
  let inc_cmd = " " 
  for inc in inc_list
    let inc_cmd = inc_cmd."-I".inc." "
  endfor

  let pp_cmd="cpp -H -M" . inc_cmd . expand('%') . " " . redirect_null
  let register_a = @a
  redir @a>
  exec ":silent !".pp_cmd
  redir END

  " remove every line that doesn't start with a '.'
  let @a = substitute(@a, "^.\\{-}\n\\(\\..\\{-}\n\\)[^\\.].*$", "\\1","g") 
  " remove '.' from beginning
  let @a = substitute(@a, "\\.\\{-1,} ", "", "g")
  " prepare it to be evaluated as an array
  let @a = "[\"" . substitute(@a, "\r\n", "\",\"", "g") . "\"]"

  " convert the founded files into an array
  let file_array = eval(@a)
  " restore register
  let @a = register_a

  " mount the ctags command
  let ctags_cmd="ctags --c++-kinds=+p --fields=+iaS --extra=+q -f /tmp/" . expand('%') . ".tags "
  for inc in file_array
    let ctags_cmd = ctags_cmd . " " . inc
  endfor
  let ctags_cmd = ctags_cmd . " " . expand('%')

  exec ":silent !" . ctags_cmd
  exec ":set tags=/tmp/" . expand('%') . ".tags"
endfunction

I know, I know, its not clean but it was quickly made just for test purpose, but I think that already is a good start!

ps.: If you want to do a plugin with this code, feel free to do it, but just give me some credit ;-)
pps.: I need to find a better way to put codes here, specially with highlighting
ppps.: Stupid me! There is a TOhtml command in Vim 7.1 that does exactly what I want :P

1 comment:

Anonymous said...

It's very effortless to find out any matter on net as compared to textbooks, as I found this paragraph at this web page.

Look into my web-site :: http://puracleansereview.com
My web page :: http://puracleansereview.com