Mikolaj Machowski wrote:
On sob gru 30 2006, Mikolaj Machowski wrote:
Do you mean create tags files for every code file, and change tags
file when switching between different code files?
But how to combine all of these actions to a hotkey?
If ctags can provide an update option, the problem can be solved very
easily. But it seems ctags doesn't provide this option any longer.

No. It should looks like this:

1. Map common key to action. For language like C++ ";" (semicolon) is
   good choice (eg. for Python it should be <CR>).
2. inoremap it to function which will insert ; and perform some
   function.
3. This function should:
a) get contents of file (:help getline())
   b) write it to temporary file (:help writefile, :help tempname; wide
      workaround but we don't want to write current file, it could break
      some things)
   c) execute ctags on that file (:help system(), man ctags - you can
      get tags directly into variable (note: this is string)::
:let a = system('ctags -f - '.file)

      Good thing would be to remove header of tags file (lines beginning
      with '!').
Now we have file with updated definitions of tags for current file.
   Rest depends on how do you organize your tags files. Lets assume you
   have one big tags file for whole project.

   d) find tags file (:help 'tags' and check how omnicomplete functions
      are finding tags files)
   e) read this file (:help readfile())
   f) delete lines containing definitions from current file (:help :for,
      :help remove())
   g) connect your new tags and global tags
   h) overwrite old tags file.

   Note: much faster, noticeable on big files, would be reading of tags
   file into buffer and just g//d proper lines and add tags at the end
how to read tags into the buffer and what does "g//d proper lines" mean?
   - but I hate buffer management in scripts, YMMV.
Sounds complicated but Vim should be able to perform all actions with
reasonable speed. The most expensive is f) .

You should carefully consider when this function should be performed.
I would vote for ; and BufLeave and BufWritePre autocommands.

m.
I write the function like this, but there are some problems.
This is my first time to write vim script, so please give me a hand.

inoremap <expr> ; UpdateTags()
func UpdateTags()
   "get the file name of the current buffer
   let currentfilename=bufname("%")
   "get the last line number in the current buffer
   let lastline=line('$')
   let currentfile=getline(1 , lastline)
   "how to define the temporary file name
   call writefile(currentfile , ".tmp.cc" , "b")
call system('ctags --c++-kinds=+p --fields=+iaS --extra=+q -f .tags .tmp.cc')
   let currenttags=readfile(".tags" , "b")
   "remove the comment in the tags file
   call remove(currenttags , "^!")
   let alltags=readfile("tags" , "b")
   call remove(alltags , currentfilename)
   echo currenttags
   substitute(currenttags , ".tmp.cc" , currentfilename , "g")
"here I want to replace ".tmp.cc" with currentfilename in the new created tags file, "but vim always gives me an error "E486: Pattern not found: currenttags , ".tmp.cc" , currentfilename , "g")"
"echo currenttags has showed me currenttags contains ".tmp.cc".
   call add(alltags , currenttags)
   call writefile(alltags , "tags" , "b")
"I want to write the updated tags into the file "tags" , but failed.
"The error is "E730: using List as a String"
"I don't understand
endfunc

Zheng Da

Reply via email to